This commit is contained in:
maliakal_d 2021-09-03 17:29:27 +02:00
parent 0c7ff62d8d
commit a718d69368
7 changed files with 54 additions and 23 deletions

View File

@ -1,6 +1,5 @@
#pragma once
#include <openssl/md5.h>
#include <stdint.h>
#include <stdio.h>
/**

View File

@ -3725,6 +3725,14 @@ int program_fpga(int file_des) {
LOG(logDEBUG1,
("Program size is: %lld\n", (long long unsigned int)filesize));
// checksum
uint16_t checksum = 0;
if (receiveData(file_des, &checksum, sizeof(filesize), INT32) < 0)
return printSocketReadError();
LOG(logINFOBLUE,
("checksum: %d\n", checksum));
// open file and allocate memory for part program
FILE *fd = NULL;
ret = startCopyingFPGAProgram(&fd, filesize, mess);

View File

@ -2140,8 +2140,9 @@ void Detector::setAdditionalJsonParameter(const std::string &key,
// Advanced
void Detector::programFPGA(const std::string &fname, Positions pos) {
std::vector<char> buffer = pimpl->readProgrammingFile(fname);
pimpl->Parallel(&Module::programFPGA, pos, buffer);
std::string checksum;
std::vector<char> buffer = pimpl->readProgrammingFile(fname, checksum);
pimpl->Parallel(&Module::programFPGA, pos, buffer, checksum);
}
void Detector::resetFPGA(Positions pos) {

View File

@ -1240,7 +1240,8 @@ int DetectorImpl::kbhit() {
return FD_ISSET(STDIN_FILENO, &fds);
}
std::vector<char> DetectorImpl::readProgrammingFile(const std::string &fname) {
std::vector<char> DetectorImpl::readProgrammingFile(const std::string &fname,
std::string &checksum) {
// validate type of file
bool isPof = false;
switch (multi_shm()->multiDetectorType) {
@ -1384,12 +1385,39 @@ std::vector<char> DetectorImpl::readProgrammingFile(const std::string &fname) {
throw RuntimeError(
"Program FPGA: Could not close destination file after converting");
}
// unlink(destfname); // delete temporary file
// calculate checksum before deleting file
checksum = getMd5Checksum(destfname);
unlink(destfname); // delete temporary file
LOG(logDEBUG1) << "Successfully loaded the rawbin file to program memory";
LOG(logINFO) << "Read file into memory";
return buffer;
}
std::string DetectorImpl::getMd5Checksum(const std::string &fname) {
std::string cmd = "md5sum " + fname;
FILE *pipe = popen(cmd.c_str(), "r");
if (!pipe) {
throw RuntimeError("Could not get md5 checksum to program fpga");
}
char buffer[256];
memset(buffer, 0, sizeof(buffer));
std::string result;
try {
while (fgets(buffer, sizeof buffer, pipe) != NULL) {
result += buffer;
}
} catch (std::exception &e) {
pclose(pipe);
throw RuntimeError(
std::string("Could not get md5 checsum to program fpga. Threw ") +
std::string(e.what()));
}
pclose(pipe);
return result;
}
sls::Result<int> DetectorImpl::getNumberofUDPInterfaces(Positions pos) const {
return Parallel(&Module::getNumberofUDPInterfaces, pos);
}

View File

@ -289,7 +289,9 @@ class DetectorImpl : public virtual slsDetectorDefs {
* @param fname name of pof/rbf file
* @returns binary of the program
*/
std::vector<char> readProgrammingFile(const std::string &fname);
std::vector<char> readProgrammingFile(const std::string &fname,
std::string &checksum);
std::string getMd5Checksum(const std::string &fname);
sls::Result<int> getNumberofUDPInterfaces(Positions pos) const;
void setNumberofUDPInterfaces(int n, Positions pos);

View File

@ -23,7 +23,6 @@
#include <iterator>
#include <sstream>
#include <thread>
#include <openssl/md5.h>
namespace sls {
@ -2486,12 +2485,13 @@ void Module::setAdditionalJsonParameter(const std::string &key,
}
// Advanced
void Module::programFPGA(std::vector<char> buffer) {
void Module::programFPGA(std::vector<char> buffer,
const std::string &checksum) {
switch (shm()->myDetectorType) {
case JUNGFRAU:
case CHIPTESTBOARD:
case MOENCH:
programFPGAviaBlackfin(buffer);
programFPGAviaBlackfin(buffer, checksum);
break;
case MYTHEN3:
case GOTTHARD2:
@ -3412,25 +3412,18 @@ sls_detector_module Module::readSettingsFile(const std::string &fname,
return myMod;
}
int Module::calculateChecksum(char *buffer, size_t bsize) {
unsigned char checksum = 0;
for(size_t i = 0; i != bsize; ++i)) {
checksum ^= fgetc(fp);
}
return 0;
}
void Module::programFPGAviaBlackfin(std::vector<char> buffer) {
// calculate checksum
void Module::programFPGAviaBlackfin(std::vector<char> buffer,
const std::string &checksum) {
uint64_t filesize = buffer.size();
int checksum = calculateChecksum(&buffer[0], filesize);
LOG(logINFOBLUE) << "checksum:" << checksum;
// send program from memory to detector
LOG(logINFO) << "Sending programming binary (from pof) to detector "
<< moduleId << " (" << shm()->hostname << ")";
auto client = DetectorSocket(shm()->hostname, shm()->controlPort);
client.Send(F_PROGRAM_FPGA);
client.Send(filesize);
// client.Send(checksum);
// opening file fail
if (client.Receive<int>() == FAIL) {

View File

@ -535,7 +535,7 @@ class Module : public virtual slsDetectorDefs {
* Advanced *
* *
* ************************************************/
void programFPGA(std::vector<char> buffer);
void programFPGA(std::vector<char> buffer, const std::string &checksum);
void resetFPGA();
void copyDetectorServer(const std::string &fname,
const std::string &hostname);
@ -744,8 +744,8 @@ class Module : public virtual slsDetectorDefs {
std::string getTrimbitFilename(detectorSettings settings, int e_eV);
sls_detector_module readSettingsFile(const std::string &fname,
bool trimbits = true);
int calculateChecksum(char *buffer, size_t bsize);
void programFPGAviaBlackfin(std::vector<char> buffer);
void programFPGAviaBlackfin(std::vector<char> buffer,
const std::string &checksum);
void programFPGAviaNios(std::vector<char> buffer);
const int moduleId;