cleaning up tcp

This commit is contained in:
Erik Frojdh 2019-06-07 16:13:34 +02:00
parent 893805e6fd
commit 1fed3553b9
7 changed files with 160 additions and 187 deletions

View File

@ -3216,10 +3216,6 @@ int slsDetector::setReceiverOnline(int value) {
} else { } else {
shm()->rxOnlineFlag = OFFLINE_FLAG; shm()->rxOnlineFlag = OFFLINE_FLAG;
if (value == ONLINE_FLAG) { if (value == ONLINE_FLAG) {
// Connect and ask for receiver id to verify that
// it's online and working
int64_t retval{0};
sendToReceiver(F_GET_RECEIVER_ID, nullptr, retval);
shm()->rxOnlineFlag = ONLINE_FLAG; shm()->rxOnlineFlag = ONLINE_FLAG;
if (shm()->receiverAPIVersion == 0) { if (shm()->receiverAPIVersion == 0) {
checkReceiverVersionCompatibility(); checkReceiverVersionCompatibility();

View File

@ -120,7 +120,7 @@ class slsReceiverTCPIPInterface : private virtual slsDetectorDefs {
void functionNotImplemented(); void functionNotImplemented();
/** mode not implemented for specific detector */ /** mode not implemented for specific detector */
void modeNotImplemented(std::string modename, int mode); void modeNotImplemented(const std::string& modename, int mode);
/** validate and set error */ /** validate and set error */
template <typename T> template <typename T>

View File

@ -1424,17 +1424,12 @@ void slsReceiverImplementation::closeFiles() {
int slsReceiverImplementation::restreamStop() { int slsReceiverImplementation::restreamStop() {
FILE_LOG(logDEBUG3) << __SHORT_AT__ << " called"; FILE_LOG(logDEBUG3) << __SHORT_AT__ << " called";
bool ret = OK;
for (const auto &it : dataStreamer) { for (const auto &it : dataStreamer) {
if (it->RestreamStop() == FAIL) if (it->RestreamStop() == FAIL)
ret = FAIL; throw sls::RuntimeError("Could not restream stop packet");
} }
// if fail, prints in datastreamer
if (ret == OK) {
FILE_LOG(logINFO) << "Restreaming Dummy Header via ZMQ successful"; FILE_LOG(logINFO) << "Restreaming Dummy Header via ZMQ successful";
} return OK;
return ret;
} }
/***callback functions***/ /***callback functions***/

View File

@ -1,4 +1,4 @@
/********************************************/ /** /**********************************************
* @file * @file
*slsReceiverTCPIPInterface.cpp *slsReceiverTCPIPInterface.cpp
* @short interface between * @short interface between
@ -111,10 +111,9 @@ void slsReceiverTCPIPInterface::startTCPServer() {
ret = decode_function(socket); ret = decode_function(socket);
} catch (const RuntimeError &e) { } catch (const RuntimeError &e) {
// We had an error needs to be sent to client // We had an error needs to be sent to client
int r = FAIL; sls::strcpy_safe(mess, e.what());
strcpy(mess, e.what()); socket.Send(FAIL);
socket.write(&r, sizeof(r)); socket.Send(mess);
socket.write(mess, sizeof(mess));
} }
// if tcp command was to exit server // if tcp command was to exit server
@ -229,31 +228,29 @@ int slsReceiverTCPIPInterface::decode_function(Interface &socket) {
} }
void slsReceiverTCPIPInterface::functionNotImplemented() { void slsReceiverTCPIPInterface::functionNotImplemented() {
char message[MAX_STR_LENGTH]; std::ostringstream os;
sprintf(message, "Function (%s) is not implemented for this detector\n", os << "Function: " << getFunctionNameFromEnum((enum detFuncs)fnum)
getFunctionNameFromEnum((enum detFuncs)fnum)); << ", is is not implemented for this detector";
throw RuntimeError(mess); throw RuntimeError(os.str());
} }
void slsReceiverTCPIPInterface::modeNotImplemented(std::string modename, void slsReceiverTCPIPInterface::modeNotImplemented(const std::string &modename,
int mode) { int mode) {
char message[MAX_STR_LENGTH]; std::ostringstream os;
sprintf(message, "%s (%d) is not implemented for this detector\n", os << modename << " (" << mode << ") is not implemented for this detector";
modename.c_str(), mode); throw RuntimeError(os.str());
throw RuntimeError(message);
} }
template <typename T> template <typename T>
void slsReceiverTCPIPInterface::validate(T arg, T retval, std::string modename, void slsReceiverTCPIPInterface::validate(T arg, T retval, std::string modename,
numberMode hex) { numberMode hex) {
if (ret == OK && arg != -1 && retval != arg) { if (ret == OK && arg != -1 && retval != arg) {
if (hex) auto format = (hex == HEX) ? std::hex : std::dec;
sprintf(mess, "Could not %s. Set 0x%x, but read 0x%x\n", auto prefix = (hex == HEX) ? "0x" : "";
modename.c_str(), (unsigned int)arg, (unsigned int)retval); std::ostringstream os;
else os << "Could not " << modename << ". Set " << prefix << format << arg
sprintf(mess, "Could not %s. Set %d, but read %d\n", << ", but read " << prefix << retval << '\n';
modename.c_str(), (unsigned int)arg, (unsigned int)retval); throw RuntimeError(os.str());
throw RuntimeError(mess);
} }
} }
@ -277,7 +274,7 @@ int slsReceiverTCPIPInterface::exec_command(Interface &socket) {
socket.Receive(cmd); socket.Receive(cmd);
FILE_LOG(logINFO) << "Executing command (" << cmd << ")"; FILE_LOG(logINFO) << "Executing command (" << cmd << ")";
const size_t tempsize = 256; const size_t tempsize = 256;
std::array<char, tempsize> temp; std::array<char, tempsize> temp{};
std::string sresult; std::string sresult;
std::shared_ptr<FILE> pipe(popen(cmd, "r"), pclose); std::shared_ptr<FILE> pipe(popen(cmd, "r"), pclose);
if (!pipe) { if (!pipe) {
@ -315,11 +312,7 @@ int slsReceiverTCPIPInterface::lock_receiver(Interface &socket) {
} }
int slsReceiverTCPIPInterface::get_last_client_ip(Interface &socket) { int slsReceiverTCPIPInterface::get_last_client_ip(Interface &socket) {
ret = OK; return socket.sendResult(server->getLastClient().arr());
memset(mess, 0, sizeof(mess));
char ip[INET_ADDRSTRLEN]{};
sls::strcpy_safe(ip, server->getLastClient().str().c_str());
return socket.sendResult(ret, &ip, sizeof(ip));
} }
int slsReceiverTCPIPInterface::set_port(Interface &socket) { int slsReceiverTCPIPInterface::set_port(Interface &socket) {
@ -355,11 +348,11 @@ int slsReceiverTCPIPInterface::send_update(Interface &socket) {
n += socket.Send(ip, sizeof(ip)); n += socket.Send(ip, sizeof(ip));
// filepath // filepath
strcpy(cstring, receiver->getFilePath().c_str()); sls::strcpy_safe(cstring, receiver->getFilePath().c_str());
n += socket.Send(cstring, sizeof(cstring)); n += socket.Send(cstring, sizeof(cstring));
// filename // filename
strcpy(cstring, receiver->getFileName().c_str()); sls::strcpy_safe(cstring, receiver->getFileName().c_str());
n += socket.Send(cstring, sizeof(cstring)); n += socket.Send(cstring, sizeof(cstring));
// index // index
@ -407,11 +400,11 @@ int slsReceiverTCPIPInterface::send_update(Interface &socket) {
n += socket.Send(&i32, sizeof(i32)); n += socket.Send(&i32, sizeof(i32));
// streaming source ip // streaming source ip
strcpy(cstring, receiver->getStreamingSourceIP().c_str()); sls::strcpy_safe(cstring, receiver->getStreamingSourceIP().c_str());
n += socket.Send(cstring, sizeof(cstring)); n += socket.Send(cstring, sizeof(cstring));
// additional json header // additional json header
strcpy(cstring, receiver->getAdditionalJsonHeader().c_str()); sls::strcpy_safe(cstring, receiver->getAdditionalJsonHeader().c_str());
n += socket.Send(cstring, sizeof(cstring)); n += socket.Send(cstring, sizeof(cstring));
// data streaming enable // data streaming enable
@ -481,16 +474,16 @@ int slsReceiverTCPIPInterface::set_detector_type(Interface &socket) {
} }
// callbacks after (in setdetectortype, the object is reinitialized) // callbacks after (in setdetectortype, the object is reinitialized)
if (startAcquisitionCallBack) if (startAcquisitionCallBack != nullptr)
impl()->registerCallBackStartAcquisition(startAcquisitionCallBack, impl()->registerCallBackStartAcquisition(startAcquisitionCallBack,
pStartAcquisition); pStartAcquisition);
if (acquisitionFinishedCallBack) if (acquisitionFinishedCallBack != nullptr)
impl()->registerCallBackAcquisitionFinished( impl()->registerCallBackAcquisitionFinished(
acquisitionFinishedCallBack, pAcquisitionFinished); acquisitionFinishedCallBack, pAcquisitionFinished);
if (rawDataReadyCallBack) if (rawDataReadyCallBack != nullptr)
impl()->registerCallBackRawDataReady(rawDataReadyCallBack, impl()->registerCallBackRawDataReady(rawDataReadyCallBack,
pRawDataReady); pRawDataReady);
if (rawDataModifyReadyCallBack) if (rawDataModifyReadyCallBack != nullptr)
impl()->registerCallBackRawDataModifyReady( impl()->registerCallBackRawDataModifyReady(
rawDataModifyReadyCallBack, pRawDataReady); rawDataModifyReadyCallBack, pRawDataReady);
} }
@ -502,12 +495,12 @@ int slsReceiverTCPIPInterface::set_detector_hostname(Interface &socket) {
char retval[MAX_STR_LENGTH]{}; char retval[MAX_STR_LENGTH]{};
socket.Receive(hostname); socket.Receive(hostname);
if (strlen(hostname)) { if (strlen(hostname) != 0) {
VerifyIdle(socket); VerifyIdle(socket);
impl()->setDetectorHostname(hostname); impl()->setDetectorHostname(hostname);
} }
auto s = impl()->getDetectorHostname(); auto s = impl()->getDetectorHostname();
strcpy(retval, s.c_str()); sls::strcpy_safe(retval, s.c_str());
if (s.empty()) { if (s.empty()) {
throw RuntimeError("Hostname not set"); throw RuntimeError("Hostname not set");
} }
@ -565,13 +558,13 @@ int slsReceiverTCPIPInterface::setup_udp(Interface &socket) {
throw RuntimeError("Failed to get ethernet interface or IP"); throw RuntimeError("Failed to get ethernet interface or IP");
} else { } else {
char eth[MAX_STR_LENGTH]{}; char eth[MAX_STR_LENGTH]{};
strcpy(eth, temp.c_str()); sls::strcpy_safe(eth, temp.c_str());
// if there is a dot in eth name // if there is a dot in eth name
if (strchr(eth, '.') != nullptr) { if (strchr(eth, '.') != nullptr) {
strcpy(eth, ""); sls::strcpy_safe(eth, "");
sprintf(mess, "Failed to get ethernet interface from IP. Got %s\n", FILE_LOG(logERROR)
temp.c_str()); << "Failed to get ethernet interface from IP. Got " << temp
FILE_LOG(logERROR) << mess; << '\n';
} }
impl()->setEthernetInterface(eth); impl()->setEthernetInterface(eth);
if (myDetectorType == EIGER) { if (myDetectorType == EIGER) {
@ -583,7 +576,7 @@ int slsReceiverTCPIPInterface::setup_udp(Interface &socket) {
if (temp == "00:00:00:00:00:00") { if (temp == "00:00:00:00:00:00") {
throw RuntimeError("failed to get mac adddress to listen to\n"); throw RuntimeError("failed to get mac adddress to listen to\n");
} else { } else {
strcpy(retvals[0], temp.c_str()); sls::strcpy_safe(retvals[0], temp.c_str());
FILE_LOG(logINFO) << "Receiver MAC Address: " << retvals[0]; FILE_LOG(logINFO) << "Receiver MAC Address: " << retvals[0];
} }
} }
@ -598,17 +591,15 @@ int slsReceiverTCPIPInterface::setup_udp(Interface &socket) {
if (temp == "none") { if (temp == "none") {
throw RuntimeError("Failed to get 2nd ethernet interface or IP"); throw RuntimeError("Failed to get 2nd ethernet interface or IP");
} else { } else {
char eth[MAX_STR_LENGTH] = {""}; char eth[MAX_STR_LENGTH]{};
memset(eth, 0, MAX_STR_LENGTH); memset(eth, 0, MAX_STR_LENGTH);
strcpy(eth, temp.c_str()); sls::strcpy_safe(eth, temp.c_str());
// if there is a dot in eth name // if there is a dot in eth name
if (strchr(eth, '.') != nullptr) { if (strchr(eth, '.') != nullptr) {
strcpy(eth, ""); sls::strcpy_safe(eth, "");
sprintf( FILE_LOG(logERROR)
mess, << "Failed to get 2nd ethernet interface from IP. Got "
"Failed to get 2nd ethernet interface from IP. Got %s\n", << temp << '\n';
temp.c_str());
FILE_LOG(logERROR) << mess;
} }
impl()->setEthernetInterface2(eth); impl()->setEthernetInterface2(eth);
@ -620,7 +611,7 @@ int slsReceiverTCPIPInterface::setup_udp(Interface &socket) {
"failed to get 2nd mac adddress to listen to"); "failed to get 2nd mac adddress to listen to");
FILE_LOG(logERROR) << mess; FILE_LOG(logERROR) << mess;
} else { } else {
strcpy(retvals[1], temp.c_str()); sls::strcpy_safe(retvals[1], temp.c_str());
FILE_LOG(logINFO) FILE_LOG(logINFO)
<< "Receiver MAC Address 2: " << retvals[1]; << "Receiver MAC Address 2: " << retvals[1];
} }
@ -638,54 +629,53 @@ int slsReceiverTCPIPInterface::setup_udp(Interface &socket) {
} }
int slsReceiverTCPIPInterface::set_timer(Interface &socket) { int slsReceiverTCPIPInterface::set_timer(Interface &socket) {
memset(mess, 0, sizeof(mess)); auto index = socket.Receive<int64_t>();
int64_t index[2] = {-1, -1}; auto value = socket.Receive<int64_t>();
int64_t retval = -1; if (value >= 0) {
socket.Receive(index);
if (index[1] >= 0) {
FILE_LOG(logDEBUG1) FILE_LOG(logDEBUG1)
<< "Setting timer index " << index[0] << " to " << index[1]; << "Setting timer index " << index << " to " << value;
switch (index[0]) { switch (index) {
case ACQUISITION_TIME: case ACQUISITION_TIME:
ret = impl()->setAcquisitionTime(index[1]); ret = impl()->setAcquisitionTime(value);
break; break;
case FRAME_PERIOD: case FRAME_PERIOD:
ret = impl()->setAcquisitionPeriod(index[1]); ret = impl()->setAcquisitionPeriod(value);
break; break;
case FRAME_NUMBER: case FRAME_NUMBER:
case CYCLES_NUMBER: case CYCLES_NUMBER:
case STORAGE_CELL_NUMBER: case STORAGE_CELL_NUMBER:
impl()->setNumberOfFrames(index[1]); impl()->setNumberOfFrames(value);
break; break;
case SUBFRAME_ACQUISITION_TIME: case SUBFRAME_ACQUISITION_TIME:
impl()->setSubExpTime(index[1]); impl()->setSubExpTime(value);
break; break;
case SUBFRAME_DEADTIME: case SUBFRAME_DEADTIME:
impl()->setSubPeriod(index[1] + impl()->getSubExpTime()); impl()->setSubPeriod(value + impl()->getSubExpTime());
break; break;
case ANALOG_SAMPLES: case ANALOG_SAMPLES:
if (myDetectorType != CHIPTESTBOARD && myDetectorType != MOENCH) { if (myDetectorType != CHIPTESTBOARD && myDetectorType != MOENCH) {
modeNotImplemented("(Analog Samples) Timer index", modeNotImplemented("(Analog Samples) Timer index",
(int)index[0]); static_cast<int>(index));
break; break;
} }
impl()->setNumberofAnalogSamples(index[1]); impl()->setNumberofAnalogSamples(value);
break; break;
case DIGITAL_SAMPLES: case DIGITAL_SAMPLES:
if (myDetectorType != CHIPTESTBOARD && myDetectorType != MOENCH) { if (myDetectorType != CHIPTESTBOARD && myDetectorType != MOENCH) {
modeNotImplemented("(Digital Samples) Timer index", modeNotImplemented("(Digital Samples) Timer index",
(int)index[0]); static_cast<int>(index));
break; break;
} }
impl()->setNumberofDigitalSamples(index[1]); impl()->setNumberofDigitalSamples(value);
break; break;
default: default:
modeNotImplemented("Timer index", (int)index[0]); modeNotImplemented("Timer index", static_cast<int>(value));
break; break;
} }
} }
// get // get
switch (index[0]) { int64_t retval = -1;
switch (index) {
case ACQUISITION_TIME: case ACQUISITION_TIME:
retval = impl()->getAcquisitionTime(); retval = impl()->getAcquisitionTime();
break; break;
@ -701,34 +691,28 @@ int slsReceiverTCPIPInterface::set_timer(Interface &socket) {
retval = impl()->getSubExpTime(); retval = impl()->getSubExpTime();
break; break;
case SUBFRAME_DEADTIME: case SUBFRAME_DEADTIME:
retval = (impl()->getSubPeriod() - impl()->getSubExpTime()); retval = impl()->getSubPeriod() - impl()->getSubExpTime();
break; break;
case ANALOG_SAMPLES: case ANALOG_SAMPLES:
if (myDetectorType != CHIPTESTBOARD && myDetectorType != MOENCH) { if (myDetectorType != CHIPTESTBOARD && myDetectorType != MOENCH) {
sprintf(mess, throw RuntimeError("This timer mode (" + std::to_string(index) +
"This timer mode (%lld) does not exist for this receiver " ") does not exist for this receiver type");
"type\n",
(long long int)index[0]);
throw RuntimeError(mess);
} }
retval = impl()->getNumberofAnalogSamples(); retval = impl()->getNumberofAnalogSamples();
break; break;
case DIGITAL_SAMPLES: case DIGITAL_SAMPLES:
if (myDetectorType != CHIPTESTBOARD && myDetectorType != MOENCH) { if (myDetectorType != CHIPTESTBOARD && myDetectorType != MOENCH) {
sprintf(mess, throw RuntimeError("This timer mode (" + std::to_string(index) +
"This timer mode (%lld) does not exist for this receiver " ") does not exist for this receiver type");
"type\n",
(long long int)index[0]);
throw RuntimeError(mess);
} }
retval = impl()->getNumberofDigitalSamples(); retval = impl()->getNumberofDigitalSamples();
break; break;
default: default:
modeNotImplemented("Timer index", (int)index[0]); modeNotImplemented("Timer index", static_cast<int>(index));
break; break;
} }
validate((int)index[1], (int)retval, std::string("set timer"), DEC); validate(value, retval, "set timer", DEC);
FILE_LOG(logDEBUG1) << slsDetectorDefs::getTimerType((timerIndex)(index[0])) FILE_LOG(logDEBUG1) << slsDetectorDefs::getTimerType((timerIndex)(index))
<< ":" << retval; << ":" << retval;
return socket.sendResult(retval); return socket.sendResult(retval);
} }
@ -763,7 +747,7 @@ int slsReceiverTCPIPInterface::set_dynamic_range(Interface &socket) {
} }
} }
int retval = impl()->getDynamicRange(); int retval = impl()->getDynamicRange();
validate(dr, retval, std::string("set dynamic range"), DEC); validate(dr, retval, "set dynamic range", DEC);
FILE_LOG(logDEBUG1) << "dynamic range: " << retval; FILE_LOG(logDEBUG1) << "dynamic range: " << retval;
return socket.sendResult(retval); return socket.sendResult(retval);
} }
@ -779,7 +763,7 @@ int slsReceiverTCPIPInterface::set_streaming_frequency(Interface &socket) {
} }
} }
int retval = impl()->getStreamingFrequency(); int retval = impl()->getStreamingFrequency();
validate(index, retval, std::string("set streaming frequency"), DEC); validate(index, retval, "set streaming frequency", DEC);
return socket.sendResult(retval); return socket.sendResult(retval);
} }
@ -822,13 +806,13 @@ int slsReceiverTCPIPInterface::set_file_dir(Interface &socket) {
char retval[MAX_STR_LENGTH]{}; char retval[MAX_STR_LENGTH]{};
socket.Receive(fPath); socket.Receive(fPath);
if (strlen(fPath)) { if (strlen(fPath) != 0) {
FILE_LOG(logDEBUG1) << "Setting file path: " << fPath; FILE_LOG(logDEBUG1) << "Setting file path: " << fPath;
impl()->setFilePath(fPath); impl()->setFilePath(fPath);
} }
std::string s = impl()->getFilePath(); std::string s = impl()->getFilePath();
strcpy(retval, s.c_str()); sls::strcpy_safe(retval, s.c_str());
if ((!s.length()) || (strlen(fPath) && strcasecmp(fPath, retval))) if ((s.empty()) || (strlen(fPath) && strcasecmp(fPath, retval)))
throw RuntimeError("Receiver file path does not exist"); throw RuntimeError("Receiver file path does not exist");
else else
FILE_LOG(logDEBUG1) << "file path:" << retval; FILE_LOG(logDEBUG1) << "file path:" << retval;
@ -840,7 +824,7 @@ int slsReceiverTCPIPInterface::set_file_name(Interface &socket) {
char fName[MAX_STR_LENGTH]{}; char fName[MAX_STR_LENGTH]{};
char retval[MAX_STR_LENGTH]{}; char retval[MAX_STR_LENGTH]{};
socket.Receive(fName); socket.Receive(fName);
if (strlen(fName)) { if (strlen(fName) != 0) {
FILE_LOG(logDEBUG1) << "Setting file name: " << fName; FILE_LOG(logDEBUG1) << "Setting file name: " << fName;
impl()->setFileName(fName); impl()->setFileName(fName);
} }
@ -848,7 +832,7 @@ int slsReceiverTCPIPInterface::set_file_name(Interface &socket) {
if (s.empty()) if (s.empty())
throw RuntimeError("file name is empty"); throw RuntimeError("file name is empty");
strcpy(retval, s.c_str()); sls::strcpy_safe(retval, s.c_str());
FILE_LOG(logDEBUG1) << "file name:" << retval; FILE_LOG(logDEBUG1) << "file name:" << retval;
return socket.sendResult(retval); return socket.sendResult(retval);
} }
@ -861,7 +845,7 @@ int slsReceiverTCPIPInterface::set_file_index(Interface &socket) {
impl()->setFileIndex(index); impl()->setFileIndex(index);
} }
int retval = impl()->getFileIndex(); int retval = impl()->getFileIndex();
validate(index, retval, std::string("set file index"), DEC); validate(index, retval, "set file index", DEC);
FILE_LOG(logDEBUG1) << "file index:" << retval; FILE_LOG(logDEBUG1) << "file index:" << retval;
return socket.sendResult(retval); return socket.sendResult(retval);
} }
@ -892,7 +876,7 @@ int slsReceiverTCPIPInterface::enable_file_write(Interface &socket) {
impl()->setFileWriteEnable(enable); impl()->setFileWriteEnable(enable);
} }
int retval = impl()->getFileWriteEnable(); int retval = impl()->getFileWriteEnable();
validate(enable, retval, std::string("set file write enable"), DEC); validate(enable, retval, "set file write enable", DEC);
FILE_LOG(logDEBUG1) << "file write enable:" << retval; FILE_LOG(logDEBUG1) << "file write enable:" << retval;
return socket.sendResult(retval); return socket.sendResult(retval);
} }
@ -905,7 +889,7 @@ int slsReceiverTCPIPInterface::enable_master_file_write(Interface &socket) {
impl()->setMasterFileWriteEnable(enable); impl()->setMasterFileWriteEnable(enable);
} }
int retval = impl()->getMasterFileWriteEnable(); int retval = impl()->getMasterFileWriteEnable();
validate(enable, retval, std::string("set master file write enable"), DEC); validate(enable, retval, "set master file write enable", DEC);
FILE_LOG(logDEBUG1) << "master file write enable:" << retval; FILE_LOG(logDEBUG1) << "master file write enable:" << retval;
return socket.sendResult(retval); return socket.sendResult(retval);
} }
@ -918,7 +902,7 @@ int slsReceiverTCPIPInterface::enable_overwrite(Interface &socket) {
impl()->setOverwriteEnable(index); impl()->setOverwriteEnable(index);
} }
int retval = impl()->getOverwriteEnable(); int retval = impl()->getOverwriteEnable();
validate(index, retval, std::string("set file overwrite enable"), DEC); validate(index, retval, "set file overwrite enable", DEC);
FILE_LOG(logDEBUG1) << "file overwrite enable:" << retval; FILE_LOG(logDEBUG1) << "file overwrite enable:" << retval;
return socket.sendResult(retval); return socket.sendResult(retval);
} }
@ -935,7 +919,7 @@ int slsReceiverTCPIPInterface::enable_tengiga(Interface &socket) {
ret = impl()->setTenGigaEnable(val); ret = impl()->setTenGigaEnable(val);
} }
int retval = impl()->getTenGigaEnable(); int retval = impl()->getTenGigaEnable();
validate(val, retval, std::string("set 10GbE"), DEC); validate(val, retval, "set 10GbE", DEC);
FILE_LOG(logDEBUG1) << "10Gbe:" << retval; FILE_LOG(logDEBUG1) << "10Gbe:" << retval;
return socket.sendResult(retval); return socket.sendResult(retval);
} }
@ -961,10 +945,10 @@ int slsReceiverTCPIPInterface::set_activate(Interface &socket) {
if (enable >= 0) { if (enable >= 0) {
VerifyIdle(socket); VerifyIdle(socket);
FILE_LOG(logDEBUG1) << "Setting activate:" << enable; FILE_LOG(logDEBUG1) << "Setting activate:" << enable;
impl()->setActivate(enable > 0 ? true : false); impl()->setActivate(static_cast<bool>(enable));
} }
auto retval = static_cast<int>(impl()->getActivate()); auto retval = static_cast<int>(impl()->getActivate());
validate(enable, retval, std::string("set activate"), DEC); validate(enable, retval, "set activate", DEC);
FILE_LOG(logDEBUG1) << "Activate: " << retval; FILE_LOG(logDEBUG1) << "Activate: " << retval;
return socket.sendResult(retval); return socket.sendResult(retval);
} }
@ -974,10 +958,10 @@ int slsReceiverTCPIPInterface::set_data_stream_enable(Interface &socket) {
if (index >= 0) { if (index >= 0) {
VerifyIdle(socket); VerifyIdle(socket);
FILE_LOG(logDEBUG1) << "Setting data stream enable:" << index; FILE_LOG(logDEBUG1) << "Setting data stream enable:" << index;
ret = impl()->setDataStreamEnable(index); impl()->setDataStreamEnable(index);
} }
int retval = impl()->getDataStreamEnable(); auto retval = static_cast<int>(impl()->getDataStreamEnable());
validate(index, retval, std::string("set data stream enable"), DEC); validate(index, retval, "set data stream enable", DEC);
FILE_LOG(logDEBUG1) << "data streaming enable:" << retval; FILE_LOG(logDEBUG1) << "data streaming enable:" << retval;
return socket.sendResult(retval); return socket.sendResult(retval);
} }
@ -990,7 +974,7 @@ int slsReceiverTCPIPInterface::set_streaming_timer(Interface &socket) {
impl()->setStreamingTimer(index); impl()->setStreamingTimer(index);
} }
int retval = impl()->getStreamingTimer(); int retval = impl()->getStreamingTimer();
validate(index, retval, std::string("set data stream timer"), DEC); validate(index, retval, "set data stream timer", DEC);
FILE_LOG(logDEBUG1) << "Streaming timer:" << retval; FILE_LOG(logDEBUG1) << "Streaming timer:" << retval;
return socket.sendResult(retval); return socket.sendResult(retval);
} }
@ -1024,7 +1008,7 @@ int slsReceiverTCPIPInterface::set_file_format(Interface &socket) {
impl()->setFileFormat(f); impl()->setFileFormat(f);
} }
auto retval = impl()->getFileFormat(); auto retval = impl()->getFileFormat();
validate(f, retval, std::string("set file format"), DEC); validate(f, retval, "set file format", DEC);
FILE_LOG(logDEBUG1) << "File Format: " << retval; FILE_LOG(logDEBUG1) << "File Format: " << retval;
return socket.sendResult(retval); return socket.sendResult(retval);
} }
@ -1037,7 +1021,7 @@ int slsReceiverTCPIPInterface::set_detector_posid(Interface &socket) {
impl()->setDetectorPositionId(arg); impl()->setDetectorPositionId(arg);
} }
auto retval = impl()->getDetectorPositionId(); auto retval = impl()->getDetectorPositionId();
validate(arg, retval, std::string("set detector position id"), DEC); validate(arg, retval, "set detector position id", DEC);
FILE_LOG(logDEBUG1) << "Position Id:" << retval; FILE_LOG(logDEBUG1) << "Position Id:" << retval;
return socket.sendResult(retval); return socket.sendResult(retval);
} }
@ -1065,7 +1049,7 @@ int slsReceiverTCPIPInterface::set_streaming_port(Interface &socket) {
impl()->setStreamingPort(port); impl()->setStreamingPort(port);
} }
int retval = impl()->getStreamingPort(); int retval = impl()->getStreamingPort();
validate(port, retval, std::string("set streaming port"), DEC); validate(port, retval, "set streaming port", DEC);
FILE_LOG(logDEBUG1) << "streaming port:" << retval; FILE_LOG(logDEBUG1) << "streaming port:" << retval;
return socket.sendResult(retval); return socket.sendResult(retval);
} }
@ -1077,7 +1061,7 @@ int slsReceiverTCPIPInterface::set_streaming_source_ip(Interface &socket) {
VerifyIdle(socket); VerifyIdle(socket);
FILE_LOG(logDEBUG1) << "Setting streaming source ip:" << arg; FILE_LOG(logDEBUG1) << "Setting streaming source ip:" << arg;
impl()->setStreamingSourceIP(arg); impl()->setStreamingSourceIP(arg);
strcpy(retval, impl()->getStreamingSourceIP().c_str()); sls::strcpy_safe(retval, impl()->getStreamingSourceIP().c_str());
FILE_LOG(logDEBUG1) << "streaming source ip:" << retval; FILE_LOG(logDEBUG1) << "streaming source ip:" << retval;
return socket.sendResult(retval); return socket.sendResult(retval);
} }
@ -1090,7 +1074,7 @@ int slsReceiverTCPIPInterface::set_silent_mode(Interface &socket) {
impl()->setSilentMode(value); impl()->setSilentMode(value);
} }
auto retval = static_cast<int>(impl()->getSilentMode()); auto retval = static_cast<int>(impl()->getSilentMode());
validate(value, retval, std::string("set silent mode"), DEC); validate(value, retval, "set silent mode", DEC);
FILE_LOG(logDEBUG1) << "silent mode:" << retval; FILE_LOG(logDEBUG1) << "silent mode:" << retval;
return socket.sendResult(retval); return socket.sendResult(retval);
} }
@ -1103,24 +1087,22 @@ int slsReceiverTCPIPInterface::enable_gap_pixels(Interface &socket) {
if (enable >= 0) { if (enable >= 0) {
VerifyIdle(socket); VerifyIdle(socket);
FILE_LOG(logDEBUG1) << "Setting gap pixels enable:" << enable; FILE_LOG(logDEBUG1) << "Setting gap pixels enable:" << enable;
impl()->setGapPixelsEnable(enable); impl()->setGapPixelsEnable(static_cast<bool>(enable));
} }
int retval = impl()->getGapPixelsEnable(); auto retval = static_cast<int>(impl()->getGapPixelsEnable());
validate(enable, retval, std::string("set gap pixels enable"), DEC); validate(enable, retval, "set gap pixels enable", DEC);
FILE_LOG(logDEBUG1) << "Gap Pixels Enable: " << retval; FILE_LOG(logDEBUG1) << "Gap Pixels Enable: " << retval;
return socket.sendResult(retval); return socket.sendResult(retval);
} }
int slsReceiverTCPIPInterface::restream_stop(Interface &socket) { int slsReceiverTCPIPInterface::restream_stop(Interface &socket) {
VerifyIdle(socket); VerifyIdle(socket);
if (impl()->getDataStreamEnable() == false) { if (!impl()->getDataStreamEnable()) {
throw RuntimeError( throw RuntimeError(
"Could not restream stop packet as data Streaming is disabled"); "Could not restream stop packet as data Streaming is disabled");
} else { } else {
FILE_LOG(logDEBUG1) << "Restreaming stop"; FILE_LOG(logDEBUG1) << "Restreaming stop";
ret = impl()->restreamStop(); impl()->restreamStop();
if (ret == FAIL)
throw RuntimeError("Could not restream stop packet");
} }
return socket.Send(OK); return socket.Send(OK);
} }
@ -1133,14 +1115,14 @@ int slsReceiverTCPIPInterface::set_additional_json_header(Interface &socket) {
VerifyIdle(socket); VerifyIdle(socket);
FILE_LOG(logDEBUG1) << "Setting additional json header: " << arg; FILE_LOG(logDEBUG1) << "Setting additional json header: " << arg;
impl()->setAdditionalJsonHeader(arg); impl()->setAdditionalJsonHeader(arg);
strcpy(retval, impl()->getAdditionalJsonHeader().c_str()); sls::strcpy_safe(retval, impl()->getAdditionalJsonHeader().c_str());
FILE_LOG(logDEBUG1) << "additional json header:" << retval; FILE_LOG(logDEBUG1) << "additional json header:" << retval;
return socket.sendResult(retval); return socket.sendResult(retval);
} }
int slsReceiverTCPIPInterface::get_additional_json_header(Interface &socket) { int slsReceiverTCPIPInterface::get_additional_json_header(Interface &socket) {
char retval[MAX_STR_LENGTH]{}; char retval[MAX_STR_LENGTH]{};
strcpy(retval, impl()->getAdditionalJsonHeader().c_str()); sls::strcpy_safe(retval, impl()->getAdditionalJsonHeader().c_str());
FILE_LOG(logDEBUG1) << "additional json header:" << retval; FILE_LOG(logDEBUG1) << "additional json header:" << retval;
return socket.sendResult(retval); return socket.sendResult(retval);
} }
@ -1157,10 +1139,8 @@ int slsReceiverTCPIPInterface::set_udp_socket_buffer_size(Interface &socket) {
} }
int64_t retval = impl()->getUDPSocketBufferSize(); int64_t retval = impl()->getUDPSocketBufferSize();
if (index != 0) if (index != 0)
validate( validate(index, retval,
index, retval, "set udp socket buffer size (No CAP_NET_ADMIN privileges?)",
std::string(
"set udp socket buffer size (No CAP_NET_ADMIN privileges?)"),
DEC); DEC);
FILE_LOG(logDEBUG1) << "UDP Socket Buffer Size:" << retval; FILE_LOG(logDEBUG1) << "UDP Socket Buffer Size:" << retval;
return socket.sendResult(retval); return socket.sendResult(retval);
@ -1181,7 +1161,7 @@ int slsReceiverTCPIPInterface::set_frames_per_file(Interface &socket) {
impl()->setFramesPerFile(index); impl()->setFramesPerFile(index);
} }
auto retval = static_cast<int>(impl()->getFramesPerFile()); auto retval = static_cast<int>(impl()->getFramesPerFile());
validate(index, retval, std::string("set frames per file"), DEC); validate(index, retval, "set frames per file", DEC);
FILE_LOG(logDEBUG1) << "frames per file:" << retval; FILE_LOG(logDEBUG1) << "frames per file:" << retval;
return socket.sendResult(retval); return socket.sendResult(retval);
} }
@ -1195,27 +1175,19 @@ int slsReceiverTCPIPInterface::check_version_compatibility(Interface &socket) {
int64_t rx_version = getReceiverVersion(); int64_t rx_version = getReceiverVersion();
if (rx_apiVersion > client_requiredVersion) { if (rx_apiVersion > client_requiredVersion) {
// old client std::ostringstream os;
ret = FAIL; os << "Incompatible versions.\n Client's receiver API Version: (0x"
sprintf(mess, << std::hex << client_requiredVersion
"This client is incompatible.\n" << "). Receiver API Version: (0x" << std::hex
"Client's receiver API Version: (0x%llx). Receiver API " << ").\n Please update the client!\n";
"Version: (0x%llx).\n" throw RuntimeError(os.str());
"Incompatible, update client!\n",
(long long unsigned int)client_requiredVersion,
(long long unsigned int)rx_apiVersion);
throw RuntimeError(mess);
} else if (client_requiredVersion > rx_version) { } else if (client_requiredVersion > rx_version) {
// old software std::ostringstream os;
ret = FAIL; os << "This receiver is incompatible.\n Receiver Version: (0x"
sprintf(mess, << std::hex << rx_version << "). Client's receiver API Version: (0x"
"This receiver is incompatible.\n" << std::hex << client_requiredVersion
"Receiver Version: (0x%llx). Client's receiver API Version: " << ").\n Please update the receiver";
"(0x%llx).\n" throw RuntimeError(os.str());
"Incompatible, update receiver!\n",
(long long unsigned int)rx_version,
(long long unsigned int)client_requiredVersion);
throw RuntimeError(mess);
} else { } else {
FILE_LOG(logINFO) << "Compatibility with Client: Successful"; FILE_LOG(logINFO) << "Compatibility with Client: Successful";
} }
@ -1227,10 +1199,10 @@ int slsReceiverTCPIPInterface::set_discard_policy(Interface &socket) {
if (index >= 0) { if (index >= 0) {
VerifyIdle(socket); VerifyIdle(socket);
FILE_LOG(logDEBUG1) << "Setting frames discard policy: " << index; FILE_LOG(logDEBUG1) << "Setting frames discard policy: " << index;
impl()->setFrameDiscardPolicy((frameDiscardPolicy)index); impl()->setFrameDiscardPolicy(static_cast<frameDiscardPolicy>(index));
} }
int retval = impl()->getFrameDiscardPolicy(); int retval = impl()->getFrameDiscardPolicy();
validate(index, retval, std::string("set discard policy"), DEC); validate(index, retval, "set discard policy", DEC);
FILE_LOG(logDEBUG1) << "frame discard policy:" << retval; FILE_LOG(logDEBUG1) << "frame discard policy:" << retval;
return socket.sendResult(retval); return socket.sendResult(retval);
} }
@ -1239,12 +1211,11 @@ int slsReceiverTCPIPInterface::set_padding_enable(Interface &socket) {
auto index = socket.Receive<int>(); auto index = socket.Receive<int>();
if (index >= 0) { if (index >= 0) {
VerifyIdle(socket); VerifyIdle(socket);
index = (index == 0) ? 0 : 1;
FILE_LOG(logDEBUG1) << "Setting frames padding enable: " << index; FILE_LOG(logDEBUG1) << "Setting frames padding enable: " << index;
impl()->setFramePaddingEnable(index); impl()->setFramePaddingEnable(static_cast<bool>(index));
} }
auto retval = static_cast<int>(impl()->getFramePaddingEnable()); auto retval = static_cast<int>(impl()->getFramePaddingEnable());
validate(index, retval, std::string("set frame padding enable"), DEC); validate(index, retval, "set frame padding enable", DEC);
FILE_LOG(logDEBUG1) << "Frame Padding Enable:" << retval; FILE_LOG(logDEBUG1) << "Frame Padding Enable:" << retval;
return socket.sendResult(retval); return socket.sendResult(retval);
} }
@ -1258,11 +1229,10 @@ int slsReceiverTCPIPInterface::set_deactivated_padding_enable(
if (enable >= 0) { if (enable >= 0) {
VerifyIdle(socket); VerifyIdle(socket);
FILE_LOG(logDEBUG1) << "Setting deactivated padding enable: " << enable; FILE_LOG(logDEBUG1) << "Setting deactivated padding enable: " << enable;
impl()->setDeactivatedPadding(enable > 0 ? true : false); impl()->setDeactivatedPadding(enable > 0);
} }
auto retval = static_cast<int>(impl()->getDeactivatedPadding()); auto retval = static_cast<int>(impl()->getDeactivatedPadding());
validate(enable, retval, std::string("set deactivated padding enable"), validate(enable, retval, "set deactivated padding enable", DEC);
DEC);
FILE_LOG(logDEBUG1) << "Deactivated Padding Enable: " << retval; FILE_LOG(logDEBUG1) << "Deactivated Padding Enable: " << retval;
return socket.sendResult(retval); return socket.sendResult(retval);
} }
@ -1277,11 +1247,11 @@ int slsReceiverTCPIPInterface::set_readout_flags(Interface &socket) {
if (arg >= 0) { if (arg >= 0) {
VerifyIdle(socket); VerifyIdle(socket);
FILE_LOG(logDEBUG1) << "Setting readout flag: " << arg; FILE_LOG(logDEBUG1) << "Setting readout flag: " << arg;
ret = impl()->setReadOutFlags(arg); impl()->setReadOutFlags(arg);
} }
auto retval = impl()->getReadOutFlags(); auto retval = impl()->getReadOutFlags();
validate((int)arg, (int)(retval & arg), std::string("set readout flags"), validate(static_cast<int>(arg), static_cast<int>(retval & arg),
HEX); "set readout flags", HEX);
FILE_LOG(logDEBUG1) << "Readout flags: " << retval; FILE_LOG(logDEBUG1) << "Readout flags: " << retval;
return socket.sendResult(retval); return socket.sendResult(retval);
} }
@ -1293,9 +1263,10 @@ int slsReceiverTCPIPInterface::set_adc_mask(Interface &socket) {
impl()->setADCEnableMask(arg); impl()->setADCEnableMask(arg);
auto retval = impl()->getADCEnableMask(); auto retval = impl()->getADCEnableMask();
if (retval != arg) { if (retval != arg) {
sprintf(mess, "Could not ADC enable mask. Set 0x%x, but read 0x%x\n", std::ostringstream os;
arg, retval); os << "Could not ADC enable mask. Set 0x" << std::hex << arg
throw RuntimeError(mess); << " but read 0x" << std::hex << retval;
throw RuntimeError(os.str());
} }
FILE_LOG(logDEBUG1) << "ADC enable mask retval: " << retval; FILE_LOG(logDEBUG1) << "ADC enable mask retval: " << retval;
return socket.sendResult(retval); return socket.sendResult(retval);
@ -1329,7 +1300,7 @@ int slsReceiverTCPIPInterface::set_dbit_offset(Interface &socket) {
impl()->setDbitOffset(arg); impl()->setDbitOffset(arg);
} }
int retval = impl()->getDbitOffset(); int retval = impl()->getDbitOffset();
validate(arg, retval, std::string("set dbit offset"), DEC); validate(arg, retval, "set dbit offset", DEC);
FILE_LOG(logDEBUG1) << "Dbit offset retval: " << retval; FILE_LOG(logDEBUG1) << "Dbit offset retval: " << retval;
return socket.sendResult(retval); return socket.sendResult(retval);
} }

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <array>
namespace sls { namespace sls {
@ -15,6 +16,7 @@ class IpAddr {
IpAddr(const char *address); IpAddr(const char *address);
std::string str() const; std::string str() const;
std::string hex() const; std::string hex() const;
std::array<char, 16u> arr() const;
constexpr bool operator==(const IpAddr &other) const noexcept { constexpr bool operator==(const IpAddr &other) const noexcept {
return addr_ == other.addr_; return addr_ == other.addr_;
} }

View File

@ -27,10 +27,15 @@ IpAddr::IpAddr(const std::string &address) {
IpAddr::IpAddr(const char *address) { inet_pton(AF_INET, address, &addr_); } IpAddr::IpAddr(const char *address) { inet_pton(AF_INET, address, &addr_); }
std::string IpAddr::str() const { std::string IpAddr::str() const {
char ipstring[INET_ADDRSTRLEN]{}; return arr().data();
inet_ntop(AF_INET, &addr_, ipstring, INET_ADDRSTRLEN); }
std::array<char, INET_ADDRSTRLEN> IpAddr::arr() const{
std::array<char, INET_ADDRSTRLEN> ipstring{};
inet_ntop(AF_INET, &addr_, ipstring.data(), INET_ADDRSTRLEN);
return ipstring; return ipstring;
} }
std::string IpAddr::hex() const { std::string IpAddr::hex() const {
std::ostringstream ss; std::ostringstream ss;
ss << std::hex << std::setfill('0'); ss << std::hex << std::setfill('0');

View File

@ -11,9 +11,11 @@ using namespace sls;
TEST_CASE("Convert mac address using classes", "[support]") { TEST_CASE("Convert mac address using classes", "[support]") {
std::vector<uint64_t> vec_addr{346856806822, 346856806852, 262027939863028,0, 281474976710655}; std::vector<uint64_t> vec_addr{346856806822, 346856806852, 262027939863028,
0, 281474976710655};
std::vector<std::string> vec_ans{"00:50:c2:46:d9:a6", "00:50:c2:46:d9:c4", std::vector<std::string> vec_ans{"00:50:c2:46:d9:a6", "00:50:c2:46:d9:c4",
"ee:50:22:46:d9:f4", "00:00:00:00:00:00", "ff:ff:ff:ff:ff:ff"}; "ee:50:22:46:d9:f4", "00:00:00:00:00:00",
"ff:ff:ff:ff:ff:ff"};
for (size_t i = 0; i != vec_addr.size(); ++i) { for (size_t i = 0; i != vec_addr.size(); ++i) {
auto mac0 = MacAddr(vec_addr[i]); auto mac0 = MacAddr(vec_addr[i]);
auto mac1 = MacAddr(vec_ans[i]); auto mac1 = MacAddr(vec_ans[i]);
@ -42,12 +44,13 @@ TEST_CASE("Hex representation of MAC", "[support]") {
} }
TEST_CASE("Convert IP using classes ", "[support]") { TEST_CASE("Convert IP using classes ", "[support]") {
std::vector<uint32_t> vec_addr{4073554305, 2747957633, 2697625985, 2566979594, 0}; std::vector<uint32_t> vec_addr{4073554305, 2747957633, 2697625985,
2566979594, 0};
std::vector<std::string> vec_ans{"129.129.205.242", "129.129.202.163", std::vector<std::string> vec_ans{"129.129.205.242", "129.129.202.163",
"129.129.202.160", "10.0.1.153", "0.0.0.0"}; "129.129.202.160", "10.0.1.153",
std::vector<std::string> vec_hex{"8181cdf2", "8181caa3", "0.0.0.0"};
"8181caa0", "0a000199","00000000"}; std::vector<std::string> vec_hex{"8181cdf2", "8181caa3", "8181caa0",
"0a000199", "00000000"};
for (size_t i = 0; i != vec_addr.size(); ++i) { for (size_t i = 0; i != vec_addr.size(); ++i) {
auto ip0 = IpAddr(vec_addr[i]); auto ip0 = IpAddr(vec_addr[i]);
@ -59,7 +62,9 @@ TEST_CASE("Convert IP using classes ", "[support]") {
CHECK(ip0 == vec_ans[i]); CHECK(ip0 == vec_ans[i]);
CHECK(ip1 == vec_ans[i]); CHECK(ip1 == vec_ans[i]);
CHECK(ip0.str() == vec_ans[i]); CHECK(ip0.str() == vec_ans[i]);
CHECK(ip0.arr().data() == vec_ans[i]);
CHECK(ip1.str() == vec_ans[i]); CHECK(ip1.str() == vec_ans[i]);
CHECK(ip1.arr().data() == vec_ans[i]);
CHECK(ip0.hex() == vec_hex[i]); CHECK(ip0.hex() == vec_hex[i]);
CHECK(ip1.hex() == vec_hex[i]); CHECK(ip1.hex() == vec_hex[i]);
} }
@ -90,7 +95,6 @@ TEST_CASE("IP Output operator gives same result as string", "[support]") {
os << addr; os << addr;
CHECK(os.str() == "129.129.205.242"); CHECK(os.str() == "129.129.205.242");
CHECK(os.str() == addr.str()); CHECK(os.str() == addr.str());
} }
TEST_CASE("MAC Output operator gives same result as string", "[support]") { TEST_CASE("MAC Output operator gives same result as string", "[support]") {