diff --git a/slsDetectorServers/slsDetectorServer/include/programFpgaBlackfin.h b/slsDetectorServers/slsDetectorServer/include/programFpgaBlackfin.h index 6b9434327..6e5a2a679 100644 --- a/slsDetectorServers/slsDetectorServer/include/programFpgaBlackfin.h +++ b/slsDetectorServers/slsDetectorServer/include/programFpgaBlackfin.h @@ -1,6 +1,5 @@ #pragma once -#include #include #include /** diff --git a/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c b/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c index f03319a5c..11af81222 100644 --- a/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c +++ b/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c @@ -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); diff --git a/slsDetectorSoftware/src/Detector.cpp b/slsDetectorSoftware/src/Detector.cpp index 3e316e984..60cc07e4c 100644 --- a/slsDetectorSoftware/src/Detector.cpp +++ b/slsDetectorSoftware/src/Detector.cpp @@ -2140,8 +2140,9 @@ void Detector::setAdditionalJsonParameter(const std::string &key, // Advanced void Detector::programFPGA(const std::string &fname, Positions pos) { - std::vector buffer = pimpl->readProgrammingFile(fname); - pimpl->Parallel(&Module::programFPGA, pos, buffer); + std::string checksum; + std::vector buffer = pimpl->readProgrammingFile(fname, checksum); + pimpl->Parallel(&Module::programFPGA, pos, buffer, checksum); } void Detector::resetFPGA(Positions pos) { diff --git a/slsDetectorSoftware/src/DetectorImpl.cpp b/slsDetectorSoftware/src/DetectorImpl.cpp index 5ae8db8f6..7fa266ff4 100644 --- a/slsDetectorSoftware/src/DetectorImpl.cpp +++ b/slsDetectorSoftware/src/DetectorImpl.cpp @@ -1240,7 +1240,8 @@ int DetectorImpl::kbhit() { return FD_ISSET(STDIN_FILENO, &fds); } -std::vector DetectorImpl::readProgrammingFile(const std::string &fname) { +std::vector 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 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 DetectorImpl::getNumberofUDPInterfaces(Positions pos) const { return Parallel(&Module::getNumberofUDPInterfaces, pos); } diff --git a/slsDetectorSoftware/src/DetectorImpl.h b/slsDetectorSoftware/src/DetectorImpl.h index 8dd8f519b..319f5153c 100644 --- a/slsDetectorSoftware/src/DetectorImpl.h +++ b/slsDetectorSoftware/src/DetectorImpl.h @@ -289,7 +289,9 @@ class DetectorImpl : public virtual slsDetectorDefs { * @param fname name of pof/rbf file * @returns binary of the program */ - std::vector readProgrammingFile(const std::string &fname); + std::vector readProgrammingFile(const std::string &fname, + std::string &checksum); + std::string getMd5Checksum(const std::string &fname); sls::Result getNumberofUDPInterfaces(Positions pos) const; void setNumberofUDPInterfaces(int n, Positions pos); diff --git a/slsDetectorSoftware/src/Module.cpp b/slsDetectorSoftware/src/Module.cpp index d7ff59e0b..74c171ff0 100644 --- a/slsDetectorSoftware/src/Module.cpp +++ b/slsDetectorSoftware/src/Module.cpp @@ -23,7 +23,6 @@ #include #include #include -#include namespace sls { @@ -2486,12 +2485,13 @@ void Module::setAdditionalJsonParameter(const std::string &key, } // Advanced -void Module::programFPGA(std::vector buffer) { +void Module::programFPGA(std::vector 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 buffer) { - // calculate checksum +void Module::programFPGAviaBlackfin(std::vector 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() == FAIL) { diff --git a/slsDetectorSoftware/src/Module.h b/slsDetectorSoftware/src/Module.h index f44d57223..89d9bfd77 100644 --- a/slsDetectorSoftware/src/Module.h +++ b/slsDetectorSoftware/src/Module.h @@ -535,7 +535,7 @@ class Module : public virtual slsDetectorDefs { * Advanced * * * * ************************************************/ - void programFPGA(std::vector buffer); + void programFPGA(std::vector 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 buffer); + void programFPGAviaBlackfin(std::vector buffer, + const std::string &checksum); void programFPGAviaNios(std::vector buffer); const int moduleId;