refactoring

This commit is contained in:
maliakal_d 2021-11-10 15:56:01 +01:00
parent adc6cf214a
commit 5190e2ab30
11 changed files with 72 additions and 52 deletions

View File

@ -2933,26 +2933,37 @@ std::string CmdProxy::UpdateFirmwareAndDetectorServer(int action) {
std::ostringstream os;
os << cmd << ' ';
if (action == defs::HELP_ACTION) {
os << "[server_name (in tftp folder)] [pc_host_name] [fname.pof (incl "
"full path)]\n\t[Jungfrau][Gotthard][CTB][Moench] Updates the "
os << "\n\tDeprecated!! Replaced without using tftp (as shown next)[server_name"
" (in tftp folder)] [pc_host_name] [fname.pof (incl full path)]";
os << "\n\t[server_name (incl fullpath)] [fname.pof (incl full path)] "
"This does not use tftp."
"\n\t\t[Jungfrau][Gotthard][CTB][Moench] Updates the "
"firmware, detector server, creates the symbolic link and then "
"reboots detector controller. \n\t[Mythen3][Gotthard2] will "
"require a script to start up the shorter named server link at "
"start up. \n\tsname is name of detector server binary found on "
"tftp folder of host pc \n\thostname is name of pc to tftp from "
"\n\tfname is programming file name"
"start up. \n\t\tsname is full path name of detector server binary"
"\n\t\tfname is full path of programming file"
<< '\n';
} else if (action == defs::GET_ACTION) {
throw sls::RuntimeError("Cannot get");
} else if (action == defs::PUT_ACTION) {
if (args.size() != 3) {
WrongNumberOfParameters(3);
if (args.size() != 3 && args.size() != 2) {
WrongNumberOfParameters(2);
}
if (args[2].find(".pof") == std::string::npos) {
throw sls::RuntimeError("Programming file must be a pof file.");
int fpos = args.size() - 1;
if (args[fpos].find(".pof") == std::string::npos && args[fpos].find(".rbf") == std::string::npos) {
throw sls::RuntimeError("Programming file must be a pof/rbf file.");
}
det->updateFirmwareAndServer(args[0], args[1], args[2],
if (args.size() == 3) {
LOG(logWARNING) << "Deprecated! Recommend to use same command without tftp (no pc name) and using full path to the server binary";
det->updateFirmwareAndServer(args[0], args[1], args[2],
std::vector<int>{det_id});
} else {
det->updateFirmwareAndServer(args[0], args[1],
std::vector<int>{det_id});
}
os << "successful\n";
} else {
throw sls::RuntimeError("Unknown action");

View File

@ -2154,18 +2154,9 @@ void Detector::copyDetectorServer(const std::string &fname,
}
void Detector::updateDetectorServer(const std::string &fname, Positions pos) {
LOG(logINFO) << "Updating Detector Server...";
std::vector<char> buffer =
readBinaryFile(fname, "Update Detector Server");
// get only the file name
std::string filename(fname);
std::size_t slashPos = fname.rfind('/');
if (slashPos != std::string::npos) {
filename = fname.substr(slashPos + 1, fname.size() - 1);
}
LOG(logINFO) << "Updating Detector Server (no tftp)...";
std::vector<char> buffer = readBinaryFile(fname, "Update Detector Server");
std::string filename = sls::getFileNameFromFilePath(fname);
pimpl->Parallel(&Module::updateDetectorServer, pos, buffer, filename);
if (getDetectorType().squash() != defs::EIGER) {
rebootController(pos);
@ -2174,7 +2165,7 @@ void Detector::updateDetectorServer(const std::string &fname, Positions pos) {
void Detector::updateKernel(const std::string &fname, Positions pos) {
LOG(logINFO) << "Updating Kernel...";
std::vector<char> buffer = readBinaryFile(fname, "Update Kernel");
std::vector<char> buffer = sls::readBinaryFile(fname, "Update Kernel");
pimpl->Parallel(&Module::updateKernel, pos, buffer);
rebootController(pos);
}
@ -2187,7 +2178,8 @@ void Detector::updateFirmwareAndServer(const std::string &sname,
const std::string &hostname,
const std::string &fname,
Positions pos) {
LOG(logINFO) << "Updating Firmware and Detector Server...";
LOG(logINFO) << "Updating Firmware and Detector Server (with tftp)...";
LOG(logINFO) << "Updating Detector Server (via tftp)...";
pimpl->Parallel(&Module::copyDetectorServer, pos, sname, hostname);
programFPGA(fname, pos);
}
@ -2196,7 +2188,10 @@ void Detector::updateFirmwareAndServer(const std::string &sname,
const std::string &fname,
Positions pos) {
LOG(logINFO) << "Updating Firmware and Detector Server (no tftp)...";
updateDetectorServer(sname, pos);
LOG(logINFO) << "Updating Detector Server (no tftp)...";
std::vector<char> buffer = readBinaryFile(fname, "Update Detector Server");
std::string filename = sls::getFileNameFromFilePath(fname);
pimpl->Parallel(&Module::updateDetectorServer, pos, buffer, filename);
programFPGA(fname, pos);
}

View File

@ -1280,14 +1280,7 @@ std::vector<char> DetectorImpl::readProgrammingFile(const std::string &fname) {
}
// get srcSize to print progress
if (fseek(src, 0, SEEK_END) != 0) {
throw RuntimeError("Program FPGA: Seek error in src file");
}
size_t srcSize = ftell(src);
if (srcSize <= 0) {
throw RuntimeError("Program FPGA: Could not get length of source file");
}
rewind(src);
ssize_t srcSize = sls::getFileSize(src, "Program FPGA");
// create temp destination file
char destfname[] = "/tmp/SLS_DET_MCB.XXXXXX";

View File

@ -355,7 +355,7 @@ std::string Implementation::getFilePath() const { return filePath; }
void Implementation::setFilePath(const std::string &c) {
if (!c.empty()) {
mkdir_p(c); // throws if it can't create
sls::mkdir_p(c); // throws if it can't create
filePath = c;
}
LOG(logINFO) << "File path: " << filePath;

View File

@ -8,6 +8,9 @@
#include <fstream>
#include <string>
namespace sls {
/**
* @param data array of data values
* @param nch number of channels
@ -22,6 +25,7 @@ int readDataFile(std::ifstream &infile, short int *data, int nch,
*/
int readDataFile(std::string fname, short int *data, int nch);
std::vector<char> readBinaryFile(const std::string &fname,
const std::string &errorPrefix);
@ -42,6 +46,8 @@ int writeDataFile(std::string fname, int nch, short int *data);
// mkdir -p path implemented by recursive calls
void mkdir_p(const std::string &path, std::string dir = "");
namespace sls {
int getFileSize(std::ifstream &ifs);
ssize_t getFileSize(FILE* fd, const std::string &prependErrorString);
std::string getFileNameFromFilePath(const std::string &fpath);
}

View File

@ -9,7 +9,7 @@
#define APICTB 0x211110
#define APIGOTTHARD 0x211110
#define APIGOTTHARD2 0x211110
#define APIJUNGFRAU 0x211110
#define APIMYTHEN3 0x211110
#define APIMOENCH 0x211110
#define APIEIGER 0x211110
#define APIJUNGFRAU 0x211110

View File

@ -11,6 +11,8 @@
#include <sys/stat.h>
#include <sys/types.h>
namespace sls {
int readDataFile(std::ifstream &infile, short int *data, int nch, int offset) {
int ichan, iline = 0;
short int idata;
@ -59,36 +61,27 @@ std::vector<char> readBinaryFile(const std::string &fname,
struct stat st;
if (stat(fname.c_str(), &st) != 0) {
throw sls::RuntimeError(errorPrefix +
std::string(": file does not exist"));
std::string(" (file does not exist)"));
}
FILE *fp = fopen(fname.c_str(), "rb");
if (fp == nullptr) {
throw sls::RuntimeError(errorPrefix +
std::string(": Could not open file: ") + fname);
std::string(" (Could not open file: ") + fname + std::string(")"));
}
// get file size to print progress
if (fseek(fp, 0, SEEK_END) != 0) {
throw sls::RuntimeError(errorPrefix +
std::string(": Seek error in src file"));
}
size_t filesize = ftell(fp);
if (filesize <= 0) {
throw sls::RuntimeError(errorPrefix +
std::string(": Could not get length of file"));
}
rewind(fp);
ssize_t filesize = sls::getFileSize(fp, errorPrefix);
std::vector<char> buffer(filesize, 0);
if (fread(buffer.data(), sizeof(char), filesize, fp) != filesize) {
if ((ssize_t)fread(buffer.data(), sizeof(char), filesize, fp) != filesize) {
throw sls::RuntimeError(errorPrefix +
std::string(": Could not read file"));
std::string(" (Could not read file)"));
}
if (fclose(fp) != 0) {
throw sls::RuntimeError(errorPrefix +
std::string(": Could not close file"));
std::string(" (Could not close file)"));
}
LOG(logDEBUG1) << "Read file into memory";
@ -138,7 +131,6 @@ void mkdir_p(const std::string &path, std::string dir) {
mkdir_p(path.substr(i + 1), dir);
}
namespace sls {
int getFileSize(std::ifstream &ifs) {
auto current_pos = ifs.tellg();
ifs.seekg(0, std::ios::end);
@ -146,4 +138,27 @@ int getFileSize(std::ifstream &ifs) {
ifs.seekg(current_pos);
return file_size;
}
std::string getFileNameFromFilePath(const std::string &fpath) {
std::string fname(fpath);
std::size_t slashPos = fpath.rfind('/');
if (slashPos != std::string::npos) {
fname = fpath.substr(slashPos + 1, fpath.size() - 1);
}
return fname;
}
ssize_t getFileSize(FILE* fd, const std::string &prependErrorString) {
if (fseek(fd, 0, SEEK_END) != 0) {
throw RuntimeError(prependErrorString + std::string(" (Seek error in src file)"));
}
size_t fileSize = ftell(fd);
if (fileSize <= 0) {
throw RuntimeError(prependErrorString + std::string(" (Could not get length of source file)"));
}
rewind(fd);
return fileSize;
}
} // namespace sls