diff --git a/CMakeLists.txt b/CMakeLists.txt index 70c68c892..df9d3de5d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -453,6 +453,7 @@ if (SLS_USE_GUI) endif (SLS_USE_GUI) if (SLS_USE_MATTERHORN) + # TODO cant use add_subdirectory twice - can only build matterhorn server or simulator add_subdirectory(slsDetectorServers/matterhornServer) endif() diff --git a/slsDetectorServers/matterhornServer/src/BaseMatterhornServer.hpp b/slsDetectorServers/matterhornServer/src/BaseMatterhornServer.hpp index 87a4aff1c..8c2a26c05 100644 --- a/slsDetectorServers/matterhornServer/src/BaseMatterhornServer.hpp +++ b/slsDetectorServers/matterhornServer/src/BaseMatterhornServer.hpp @@ -49,12 +49,8 @@ class BaseMatterhornServer ProcessedResult processFunction(const detFuncs function_id, ServerInterface &socket); - using ImplType = typename implementation_typetrait::ImplType; - - protected: - auto *getImpl() { return this->getDerivedImpl(); } - - const auto *getImpl() const { return this->getDerivedImpl(); } + using ImplType = + typename implementation_type_trait::ImplType; private: DerivedServer *getDerived() { return static_cast(this); } @@ -94,7 +90,8 @@ BaseMatterhornServer::set_counter_mask(ServerInterface &socket) { } try { - getImpl()->set_counter_mask(counter_mask); + DetectorServer>::getImpl() + ->set_counter_mask(counter_mask); } catch (const std::exception &e) { return_fail("Failed to set counter mask: " + std::string(e.what())); } @@ -109,13 +106,14 @@ ProcessedResult BaseMatterhornServer::get_counter_mask( uint32_t counter_mask{}; try { - counter_mask = getImpl()->get_counter_mask(); + counter_mask = + DetectorServer>::getImpl() + ->get_counter_mask(); } catch (const std::exception &e) { return_fail("Failed to get counter mask: " + std::string(e.what())); } - return ProcessedResult{ - static_cast(socket.sendResult(counter_mask))}; + return send_result(socket, counter_mask); } } // namespace sls \ No newline at end of file diff --git a/slsDetectorServers/matterhornServer/src/BaseMatterhornServerImpl.hpp b/slsDetectorServers/matterhornServer/src/BaseMatterhornServerImpl.hpp index 612b40b12..b9e1becdb 100644 --- a/slsDetectorServers/matterhornServer/src/BaseMatterhornServerImpl.hpp +++ b/slsDetectorServers/matterhornServer/src/BaseMatterhornServerImpl.hpp @@ -86,8 +86,11 @@ template void BaseMatterhornServerImpl::setupDetector() { // TODO: extend try { + // TODO: for a stop server command it should not talk to the board -> + // add flag. set_num_frames(1); set_num_triggers(1); + set_counter_mask(0xF); // enable counter all counters by default } catch (const std::exception &e) { LOG(logERROR) << "Failed to setup detector: " << e.what(); detectorSetupStatus.error_message = std::string(e.what()); @@ -141,12 +144,18 @@ void BaseMatterhornServerImpl::set_num_frames( try { busCommunication.writeRegister(Reg::MH_SM_Frames_Reg, static_cast(num_frames)); + auto written_num_frames = busCommunication.readRegister( + Reg::MH_SM_Frames_Reg); // check if write was successful + + if (num_frames != static_cast(written_num_frames)) { + throw std::runtime_error( + fmt::format("Requested {} frames, but set {}", num_frames, + static_cast(written_num_frames))); + } } catch (const std::exception &e) { LOG(logERROR) << "Failed to set number of frames: " << e.what(); throw; } - // TODO: maybe always check that the value is correctly set by reading back - // the register and comparing } template @@ -155,6 +164,13 @@ void BaseMatterhornServerImpl::set_num_triggers( try { busCommunication.writeRegister(Reg::MH_SM_Triggers_Reg, num_triggers); + auto written_num_triggers = busCommunication.readRegister( + Reg::MH_SM_Triggers_Reg); // check if write was successful + if (num_triggers != written_num_triggers) { + throw std::runtime_error( + fmt::format("Requested {} triggers, but set {}", num_triggers, + written_num_triggers)); + } } catch (const std::exception &e) { LOG(logERROR) << "Failed to set number of triggers: " << e.what(); throw; @@ -273,8 +289,24 @@ void BaseMatterhornServerImpl::set_module_position( try { busCommunication.writeRegister(Reg::Frame_HDR_ModCoord_LSB_Reg, register_value_LSB); + + auto written_register_value_LSB = busCommunication.readRegister( + Reg::Frame_HDR_ModCoord_LSB_Reg); // check if write was successful + busCommunication.writeRegister(Reg::Frame_HDR_ModCoord_MSB_Reg, register_value_MSB); + + auto written_register_value_MSB = busCommunication.readRegister( + Reg::Frame_HDR_ModCoord_MSB_Reg); // check if write was successful + + if (register_value_LSB != written_register_value_LSB || + register_value_MSB != written_register_value_MSB) { + throw std::runtime_error( + fmt::format("LSB: requested {}, but set {}. " + "MSB: requested {}, but set {}", + register_value_LSB, written_register_value_LSB, + register_value_MSB, written_register_value_MSB)); + } } catch (const std::exception &e) { LOG(logERROR) << "Failed to write module position register: " << e.what(); diff --git a/slsDetectorServers/matterhornServer/src/MatterhornApp.cpp b/slsDetectorServers/matterhornServer/src/MatterhornApp.cpp index 3fe751214..3cf1f6bf5 100644 --- a/slsDetectorServers/matterhornServer/src/MatterhornApp.cpp +++ b/slsDetectorServers/matterhornServer/src/MatterhornApp.cpp @@ -1,11 +1,13 @@ #include "CommandLineOptions.hpp" #include MATTERHORN_SERVER_HEADER +#include "helpers/Helpers.hpp" #include "sls/logger.h" #include "sls/sls_detector_exceptions.h" #include "sls/versionAPI.h" #include #include +#include #include #include #include @@ -62,6 +64,9 @@ int main(int argc, char *argv[]) { LOG(TLogLevel::logINFOMAGENTA) << cli.printOptions(); + // free shared memory from previous run (not removed if detector crashed) + freeSharedMemory(); + // Register Ctrl+C handler std::signal(SIGINT, sigInterruptHandler); diff --git a/slsDetectorServers/matterhornServer/src/MatterhornServer.hpp b/slsDetectorServers/matterhornServer/src/MatterhornServer.hpp index bcf1fa30f..edc51b8a8 100644 --- a/slsDetectorServers/matterhornServer/src/MatterhornServer.hpp +++ b/slsDetectorServers/matterhornServer/src/MatterhornServer.hpp @@ -11,8 +11,6 @@ namespace sls { class MatterhornServer : public BaseMatterhornServer { public: - using ImplType = MatterhornServerImpl; - /** * Constructor * Starts up a Matterhorn server. @@ -23,11 +21,6 @@ class MatterhornServer : public BaseMatterhornServer { explicit MatterhornServer(uint16_t port = DEFAULT_TCP_CNTRL_PORTNO); ~MatterhornServer() = default; - - private: - ImplType *getImpl() { return this->getDerivedImpl(); } - - const ImplType *getImpl() const { return this->getDerivedImpl(); } }; } // namespace sls \ No newline at end of file diff --git a/slsDetectorServers/matterhornServer/src/MatterhornServerImpl.cpp b/slsDetectorServers/matterhornServer/src/MatterhornServerImpl.cpp index d1de9a8f7..503760eef 100644 --- a/slsDetectorServers/matterhornServer/src/MatterhornServerImpl.cpp +++ b/slsDetectorServers/matterhornServer/src/MatterhornServerImpl.cpp @@ -4,6 +4,9 @@ namespace sls { slsDetectorDefs::runStatus MatterhornServerImpl::get_run_status() const { + // TODO: will also have a scanStatus - scanStatus should be in base + // implementation and shared between virtual and actual detector - split + // this function into two. return slsDetectorDefs::runStatus::IDLE; // TODO: implement } @@ -13,6 +16,9 @@ void MatterhornServerImpl::set_module_position_and_update_srcudpmac( // position_info = [num_modules_in_y, module_index] const size_t module_row = position_info[1] % position_info[0]; + if (position_info[0] <= 0) { + throw RuntimeError("Number of modules in y direction cannot be 0."); + } const size_t module_col = position_info[1] / position_info[0]; try { diff --git a/slsDetectorServers/matterhornServer/src/VirtualMatterhornServer.cpp b/slsDetectorServers/matterhornServer/src/VirtualMatterhornServer.cpp index 11471e281..6e6b7e1cd 100644 --- a/slsDetectorServers/matterhornServer/src/VirtualMatterhornServer.cpp +++ b/slsDetectorServers/matterhornServer/src/VirtualMatterhornServer.cpp @@ -17,9 +17,6 @@ VirtualMatterhornServer::VirtualMatterhornServer(uint16_t port) // TODO: no init_server function for now is it neccessary to set the init // flag getImpl()->setupDetector(); - - getImpl()->set_source_udp_ip( - LOCALHOSTIP_INT); // TODO: should this be done in setupDetector? } } // namespace sls \ No newline at end of file diff --git a/slsDetectorServers/matterhornServer/src/VirtualMatterhornServer.hpp b/slsDetectorServers/matterhornServer/src/VirtualMatterhornServer.hpp index 5d97923b5..9e8a24b19 100644 --- a/slsDetectorServers/matterhornServer/src/VirtualMatterhornServer.hpp +++ b/slsDetectorServers/matterhornServer/src/VirtualMatterhornServer.hpp @@ -8,7 +8,6 @@ class VirtualMatterhornServer : public BaseMatterhornServer { public: - using ImplType = VirtualMatterhornServerImpl; /** * Constructor * Starts up a virtual Matterhorn server. @@ -19,11 +18,6 @@ class VirtualMatterhornServer explicit VirtualMatterhornServer(uint16_t port = DEFAULT_TCP_CNTRL_PORTNO); ~VirtualMatterhornServer() = default; - - private: - ImplType *getImpl() { return this->getDerivedImpl(); } - - const ImplType *getImpl() const { return this->getDerivedImpl(); } }; } // namespace sls \ No newline at end of file diff --git a/slsDetectorServers/matterhornServer/src/VirtualMatterhornServerImpl.cpp b/slsDetectorServers/matterhornServer/src/VirtualMatterhornServerImpl.cpp index 5f02c420d..46346c6b3 100644 --- a/slsDetectorServers/matterhornServer/src/VirtualMatterhornServerImpl.cpp +++ b/slsDetectorServers/matterhornServer/src/VirtualMatterhornServerImpl.cpp @@ -6,6 +6,10 @@ namespace sls { +VirtualMatterhornServerImpl::VirtualMatterhornServerImpl() { + this->set_source_udp_ip(LOCALHOSTIP_INT); +} + slsDetectorDefs::runStatus VirtualMatterhornServerImpl::get_run_status() const { slsDetectorDefs::runStatus scanstatus{}; @@ -42,7 +46,7 @@ void VirtualMatterhornServerImpl::set_module_position_and_update_srcudpmac( if (this->udpDetails[0].srcmac == 0) { // only configure if source mac address is not set already uint64_t newSrcMac = - generate_mac_address_from_module_position(module_row, module_col); + generateMacAddressfromModulePosition(module_row, module_col); this->updateSrcMacAddress(newSrcMac); } @@ -51,8 +55,7 @@ void VirtualMatterhornServerImpl::set_module_position_and_update_srcudpmac( void VirtualMatterhornServerImpl::set_source_udp_mac( const uint64_t newsrcudpMac) { - if ((newsrcudpMac << INDIVIDUAL_GROUP_BIT_OFFSET) == 0 && - (newsrcudpMac << UNIVERSAL_LOCAL_BIT_OFFSET) == 1) { + if (!isValidMac(newsrcudpMac)) { throw RuntimeError("Invalid source MAC address: unicast bit or local " "administration bit is not set"); } diff --git a/slsDetectorServers/matterhornServer/src/VirtualMatterhornServerImpl.hpp b/slsDetectorServers/matterhornServer/src/VirtualMatterhornServerImpl.hpp index e26823449..84ffd68af 100644 --- a/slsDetectorServers/matterhornServer/src/VirtualMatterhornServerImpl.hpp +++ b/slsDetectorServers/matterhornServer/src/VirtualMatterhornServerImpl.hpp @@ -7,7 +7,7 @@ class VirtualMatterhornServerImpl : public BaseMatterhornServerImpl { public: - VirtualMatterhornServerImpl() = default; + VirtualMatterhornServerImpl(); ~VirtualMatterhornServerImpl() = default; slsDetectorDefs::runStatus get_run_status() const; diff --git a/slsDetectorServers/matterhornServer/src/utils/type_traits.hpp b/slsDetectorServers/matterhornServer/src/utils/type_traits.hpp index f345235e5..13a253ba9 100644 --- a/slsDetectorServers/matterhornServer/src/utils/type_traits.hpp +++ b/slsDetectorServers/matterhornServer/src/utils/type_traits.hpp @@ -9,13 +9,13 @@ class VirtualMatterhornServer; class MatterhornServerImpl; class VirtualMatterhornServerImpl; -template struct implementation_typetrait; +template struct implementation_type_trait; -template <> struct implementation_typetrait { +template <> struct implementation_type_trait { using ImplType = MatterhornServerImpl; }; -template <> struct implementation_typetrait { +template <> struct implementation_type_trait { using ImplType = VirtualMatterhornServerImpl; }; diff --git a/slsDetectorServers/slsDetectorServer_cpp/include/DetectorServer.hpp b/slsDetectorServers/slsDetectorServer_cpp/include/DetectorServer.hpp index 44c2ebe22..1b0c673ee 100644 --- a/slsDetectorServers/slsDetectorServer_cpp/include/DetectorServer.hpp +++ b/slsDetectorServers/slsDetectorServer_cpp/include/DetectorServer.hpp @@ -38,12 +38,12 @@ template class DetectorServer { std::unique_ptr impl; - auto *getDerivedImpl() { + auto *getImpl() { return static_cast( impl.get()); } - const auto *getDerivedImpl() const { + const auto *getImpl() const { return static_cast( impl.get()); } @@ -191,8 +191,7 @@ ProcessedResult DetectorServer::get_update_mode( // TODO: catch the socket error during Send and add error message to the // ProcessedResult but DatSocket shared with receiver - some refactoring - return ProcessedResult{static_cast( - socket.sendResult(static_cast(updateMode)))}; + return send_result(socket, static_cast(updateMode)); } template @@ -200,8 +199,7 @@ ProcessedResult DetectorServer::get_source_udp_mac( ServerInterface &socket) const { auto srcUdpMac = impl->get_source_udp_mac(); - return ProcessedResult{ - static_cast(socket.sendResult(srcUdpMac))}; + return send_result(socket, srcUdpMac); } template @@ -220,7 +218,7 @@ ProcessedResult DetectorServer::set_source_udp_mac( } try { - getDerivedImpl()->set_source_udp_mac(newsrcudpMac); + getImpl()->set_source_udp_mac(newsrcudpMac); } catch (const std::exception &e) { LOG(logERROR) << "Failed to set source UDP MAC address: " << e.what(); return_fail("Failed to set source UDP MAC address: " + @@ -254,8 +252,7 @@ template ProcessedResult DetectorServer::get_source_udp_ip( ServerInterface &socket) const { uint32_t src_UdpIp = impl->get_source_udp_ip(); - return ProcessedResult{ - static_cast(socket.sendResult(src_UdpIp))}; + return send_result(socket, src_UdpIp); } template @@ -283,8 +280,7 @@ ProcessedResult DetectorServer::get_destination_udp_mac( ServerInterface &socket) const { auto dstUdpMac = impl->get_destination_udp_mac(); - return ProcessedResult{ - static_cast(socket.sendResult(dstUdpMac))}; + return send_result(socket, dstUdpMac); } template @@ -312,8 +308,7 @@ ProcessedResult DetectorServer::get_destination_udp_ip( ServerInterface &socket) const { uint32_t dstUdpIp = impl->get_destination_udp_ip(); - return ProcessedResult{ - static_cast(socket.sendResult(dstUdpIp))}; + return send_result(socket, dstUdpIp); } template @@ -340,8 +335,7 @@ template ProcessedResult DetectorServer::get_destination_udp_port( ServerInterface &socket) const { uint16_t dstUdpPort = impl->get_destination_udp_port(); - return ProcessedResult{ - static_cast(socket.sendResult(dstUdpPort))}; + return send_result(socket, dstUdpPort); }; template @@ -349,15 +343,14 @@ ProcessedResult DetectorServer::get_num_frames( ServerInterface &socket) const { uint64_t num_frames{}; try { - num_frames = getDerivedImpl()->get_num_frames(); + num_frames = getImpl()->get_num_frames(); } catch (const std::exception &e) { auto error_message = "Failed to get number of frames: " + std::string(e.what()); LOG(logERROR) << error_message; return return_fail(error_message); } - return ProcessedResult{ - static_cast(socket.sendResult(num_frames))}; + return send_result(socket, num_frames); } template @@ -373,7 +366,7 @@ DetectorServer::set_num_frames(ServerInterface &socket) { return return_fail(error_message); } try { - getDerivedImpl()->set_num_frames(num_frames); + getImpl()->set_num_frames(num_frames); } catch (const std::exception &e) { auto error_message = "Failed to set number of frames: " + std::string(e.what()); @@ -388,16 +381,14 @@ ProcessedResult DetectorServer::get_num_triggers( ServerInterface &socket) const { uint64_t num_triggers{}; try { - num_triggers = - static_cast(getDerivedImpl()->get_num_triggers()); + num_triggers = static_cast(getImpl()->get_num_triggers()); } catch (const std::exception &e) { auto error_message = "Failed to get number of triggers: " + std::string(e.what()); LOG(logERROR) << error_message; return return_fail(error_message); } - return ProcessedResult{ - static_cast(socket.sendResult(num_triggers))}; + return send_result(socket, num_triggers); } template @@ -413,7 +404,7 @@ ProcessedResult DetectorServer::set_num_triggers( return return_fail(error_message); } try { - getDerivedImpl()->set_num_triggers(num_triggers); + getImpl()->set_num_triggers(num_triggers); } catch (const std::exception &e) { auto error_message = "Failed to set number of triggers: " + std::string(e.what()); @@ -428,32 +419,30 @@ ProcessedResult DetectorServer::get_version( ServerInterface &socket) const { auto version = - getDerivedImpl() - ->get_server_version(); // TODO: get Impl from derived server + getImpl()->get_server_version(); // TODO: get Impl from derived server char version_cstr[MAX_STR_LENGTH]{}; std::snprintf(version_cstr, sizeof(version_cstr), "%s", version.c_str()); // ensures temination LOG(TLogLevel::logDEBUG) << "Server Version: " << version; - return ProcessedResult{static_cast(socket.sendResult( - version_cstr))}; // TODO: check what would be possible return codes!!! + return send_result( + socket, + version_cstr); // TODO: check what would be possible return codes!!! } template ProcessedResult DetectorServer::get_num_udp_interfaces( ServerInterface &socket) const { - auto num_udp_interfaces = getDerivedImpl()->get_num_udp_interfaces(); + int num_udp_interfaces = getImpl()->get_num_udp_interfaces(); - return ProcessedResult{static_cast( - socket.sendResult(static_cast(num_udp_interfaces)))}; + return send_result(socket, num_udp_interfaces); } template ProcessedResult DetectorServer::get_detector_type( ServerInterface &socket) const { - uint32_t detectortype = getDerivedImpl()->get_detector_type(); - return ProcessedResult{ - static_cast(socket.sendResult(detectortype))}; + uint32_t detectortype = getImpl()->get_detector_type(); + return send_result(socket, detectortype); } template @@ -461,39 +450,37 @@ ProcessedResult DetectorServer::get_receiver_parameters( ServerInterface &socket) const { slsDetectorDefs::rxParameters rx_params = - getDerivedImpl()->get_receiver_parameters(); + getImpl()->get_receiver_parameters(); - return ProcessedResult{ - static_cast(socket.sendResult(rx_params))}; + return send_result(socket, rx_params); } template ProcessedResult DetectorServer::get_run_status( ServerInterface &socket) const { - slsDetectorDefs::runStatus status = getDerivedImpl()->get_run_status(); + slsDetectorDefs::runStatus status = getImpl()->get_run_status(); - return ProcessedResult{static_cast(socket.sendResult(status))}; + return send_result(socket, status); } template ProcessedResult DetectorServer::initial_checks( ServerInterface &socket) const { - auto detectorsetupstatus = getDerivedImpl()->initial_checks(); + auto detectorsetupstatus = getImpl()->get_detector_setup_status(); // TODO: should there be a time limit? while (detectorsetupstatus.setup_status == detector_setup_status::NOT_SETUP) { std::this_thread::sleep_for(std::chrono::seconds(1)); - detectorsetupstatus = getDerivedImpl()->initial_checks(); + detectorsetupstatus = getImpl()->get_detector_setup_status(); } if (detectorsetupstatus.setup_status == detector_setup_status::FAILED_SETUP) { return return_fail("Initial checks failed: " + detectorsetupstatus.error_message); } else { - return ProcessedResult{ - static_cast(socket.sendResult(true))}; + return send_result(socket, true); } } @@ -516,8 +503,7 @@ DetectorServer::set_module_position_and_update_srcudpmac( } try { - getDerivedImpl()->set_module_position_and_update_srcudpmac( - position_info); + getImpl()->set_module_position_and_update_srcudpmac(position_info); } catch (const std::exception &e) { return_fail("Failed to set module position: " + std::string(e.what())); } diff --git a/slsDetectorServers/slsDetectorServer_cpp/include/DetectorServerImpl.hpp b/slsDetectorServers/slsDetectorServer_cpp/include/DetectorServerImpl.hpp index 1287d4f91..98054cf3d 100644 --- a/slsDetectorServers/slsDetectorServer_cpp/include/DetectorServerImpl.hpp +++ b/slsDetectorServers/slsDetectorServer_cpp/include/DetectorServerImpl.hpp @@ -62,12 +62,12 @@ class DetectorServerImpl { void set_source_udp_ip(const uint32_t srcip); + uint32_t get_source_udp_ip() const; + void set_destination_udp_ip(const uint32_t dstip); uint32_t get_destination_udp_ip() const; - uint32_t get_source_udp_ip() const; - void set_destination_udp_mac(const uint64_t dstmac); uint64_t get_destination_udp_mac() const; @@ -76,7 +76,7 @@ class DetectorServerImpl { uint16_t get_destination_udp_port() const; - detector_setup_status initial_checks() const; + detector_setup_status get_detector_setup_status() const; protected: std::array diff --git a/slsDetectorServers/slsDetectorServer_cpp/include/TCPInterface.hpp b/slsDetectorServers/slsDetectorServer_cpp/include/TCPInterface.hpp index 0dfe32b27..deb337e62 100644 --- a/slsDetectorServers/slsDetectorServer_cpp/include/TCPInterface.hpp +++ b/slsDetectorServers/slsDetectorServer_cpp/include/TCPInterface.hpp @@ -30,6 +30,11 @@ inline ProcessedResult send_ok(ServerInterface &socket) { static_cast(socket.Send(ReturnCode::OK))}; } +template +inline ProcessedResult send_result(ServerInterface &socket, const T &value) { + return ProcessedResult{static_cast(socket.sendResult(value))}; +} + /** * @brief TCPInterface class handles communication and processing of commands * from Client to Server. diff --git a/slsDetectorServers/slsDetectorServer_cpp/include/helpers/Helpers.hpp b/slsDetectorServers/slsDetectorServer_cpp/include/helpers/Helpers.hpp index 2a516c56d..b17207003 100644 --- a/slsDetectorServers/slsDetectorServer_cpp/include/helpers/Helpers.hpp +++ b/slsDetectorServers/slsDetectorServer_cpp/include/helpers/Helpers.hpp @@ -1,4 +1,7 @@ #pragma once +#include "Defs.hpp" +#include "DetectorServerImpl.hpp" +#include "sls/SharedMemory.h" #include "sls/sls_detector_defs.h" #include #include @@ -6,13 +9,13 @@ namespace sls { constexpr uint64_t mac_mask = 0xffffffffffff0000; -constexpr uint8_t offset_row_position_in_mac = 16; // given in bits -constexpr uint8_t offset_col_position_in_mac = 0; // given in bits +constexpr uint8_t offset_row_position_in_mac = 8; // given in bits +constexpr uint8_t offset_col_position_in_mac = 0; // given in bits /// @brief generates a random locally administered unicast MAC address for the /// source UDP /// @return generated MAC address -inline uint64_t generaterandomMacAddress() { +inline uint64_t generateRandomMacAddress() { uint64_t mac = 0xAA0000000000; // locally administered unicast address (0xA: 0b1010) // // TODO maybe 0x02000000000 better? @@ -28,11 +31,10 @@ inline uint64_t generaterandomMacAddress() { /// @param module_row /// @param module_col /// @return generated MAC address -inline uint64_t -generate_mac_address_from_module_position(const size_t module_row, - const size_t module_col) { +inline uint64_t generateMacAddressfromModulePosition(const uint8_t module_row, + const uint8_t module_col) { - uint64_t newSrcMac = generaterandomMacAddress(); + uint64_t newSrcMac = generateRandomMacAddress(); newSrcMac = (newSrcMac & mac_mask) | (module_row << offset_row_position_in_mac) | (module_col << offset_col_position_in_mac); @@ -40,4 +42,23 @@ generate_mac_address_from_module_position(const size_t module_row, return newSrcMac; } -} // namespace sls \ No newline at end of file +/// @brief check that mac is unicast and locally administered +/// @param mac +/// @return true if mac is valid, false otherwise +inline bool isValidMac(const uint64_t mac) { + if ((mac << INDIVIDUAL_GROUP_BIT_OFFSET) == 0 && + (mac << UNIVERSAL_LOCAL_BIT_OFFSET) == 1) { + return true; + } + return false; +} + +inline void freeSharedMemory() { + SharedMemory shm(0, -1, "server"); + + if (shm.exists()) { + shm.removeSharedMemory(); + } +} + +} // namespace sls diff --git a/slsDetectorServers/slsDetectorServer_cpp/src/DetectorServerImpl.cpp b/slsDetectorServers/slsDetectorServer_cpp/src/DetectorServerImpl.cpp index bce897432..8804d9cd2 100644 --- a/slsDetectorServers/slsDetectorServer_cpp/src/DetectorServerImpl.cpp +++ b/slsDetectorServers/slsDetectorServer_cpp/src/DetectorServerImpl.cpp @@ -1,5 +1,6 @@ #include "DetectorServerImpl.hpp" #include "sls/logger.h" +#include "sls/sls_detector_exceptions.h" #include namespace sls { @@ -20,7 +21,12 @@ void DetectorServerImpl::createSharedMemory() { shm.openSharedMemory(true); // stop server } else { LOG(logINFOBLUE) << "Creating shared memory for acquisition status"; - shm.createSharedMemory(); + try { + shm.createSharedMemory(); + } catch (const SharedMemoryAlreadyExistsError &e) { + shm.openSharedMemory(true); // potential race conditions between + // stop and control server + } } } @@ -77,7 +83,7 @@ uint16_t DetectorServerImpl::get_destination_udp_port() const { return udpDetails[0].dstport; } -detector_setup_status DetectorServerImpl::initial_checks() const { +detector_setup_status DetectorServerImpl::get_detector_setup_status() const { return detectorSetupStatus; } diff --git a/slsDetectorServers/slsDetectorServer_cpp/src/MemoryModel.cpp b/slsDetectorServers/slsDetectorServer_cpp/src/MemoryModel.cpp index 0e3ca8967..273002f2d 100644 --- a/slsDetectorServers/slsDetectorServer_cpp/src/MemoryModel.cpp +++ b/slsDetectorServers/slsDetectorServer_cpp/src/MemoryModel.cpp @@ -46,9 +46,9 @@ void HardwareMemoryModel::unmapMemory() { if (munmap(reinterpret_cast( const_cast(mapped_memory_ptr)), size_memory_space) < 0) { - throw RuntimeError( - fmt::format("Failed to unmap memory for IP core: {}", - IPCore_base_address)); // TODO: needs ToString + LOG(logWARNING) + << fmt::format("Failed to unmap memory for IP core: {}", + IPCore_base_address); // TODO: needs ToString } mapped_memory_ptr = nullptr; } diff --git a/slsReceiverSoftware/src/GeneralData.h b/slsReceiverSoftware/src/GeneralData.h index 4a3d9f8cd..79d40a898 100644 --- a/slsReceiverSoftware/src/GeneralData.h +++ b/slsReceiverSoftware/src/GeneralData.h @@ -483,7 +483,6 @@ class MatterhornData : public GeneralData { dynamicRange = 16; // default SetCounterMask(0xf); // default all 4 counters enabled UpdateImageSize(); - CalculatefifoDepth(); }; void SetDynamicRange(int dr) { @@ -513,6 +512,8 @@ class MatterhornData : public GeneralData { LOG(logINFO) << "imageSize: " << imageSize; actualImageSize = imageSize; + CalculatefifoDepth(); + packetsPerFrame = imageSize / dataSize; LOG(logINFO) << "Packets Per Frame: " << packetsPerFrame; diff --git a/slsReceiverSoftware/src/Implementation.cpp b/slsReceiverSoftware/src/Implementation.cpp index 23ccead84..3d768d183 100644 --- a/slsReceiverSoftware/src/Implementation.cpp +++ b/slsReceiverSoftware/src/Implementation.cpp @@ -270,8 +270,6 @@ void Implementation::setModulePositionId(const int id) { streamingPort = DEFAULT_ZMQ_RX_PORTNO + modulePos * portGeometry.x; if (numModules.y == 0) { - LOG(logERROR) << "Number of modules in y direction is 0. Cannot set " - "module position."; throw RuntimeError("Number of modules in y direction is 0. Cannot set " "module position."); } diff --git a/slsSupportLib/include/sls/SharedMemory.h b/slsSupportLib/include/sls/SharedMemory.h index 4804634f1..006cfbcc9 100644 --- a/slsSupportLib/include/sls/SharedMemory.h +++ b/slsSupportLib/include/sls/SharedMemory.h @@ -168,7 +168,11 @@ template class SharedMemory { if (fd < 0) { std::string msg = "Create shared memory " + name + " failed: " + strerror(errno); - throw SharedMemoryError(msg); + if (errno == EEXIST) { + throw SharedMemoryAlreadyExistsError(msg); + } else { + throw SharedMemoryError(msg); + } } if (ftruncate(fd, sizeof(T)) < 0) { diff --git a/slsSupportLib/include/sls/sls_detector_exceptions.h b/slsSupportLib/include/sls/sls_detector_exceptions.h index 421b60549..38bc43ebf 100644 --- a/slsSupportLib/include/sls/sls_detector_exceptions.h +++ b/slsSupportLib/include/sls/sls_detector_exceptions.h @@ -17,6 +17,11 @@ class SharedMemoryError : public RuntimeError { explicit SharedMemoryError(const std::string &msg); }; +class SharedMemoryAlreadyExistsError : public SharedMemoryError { + public: + explicit SharedMemoryAlreadyExistsError(const std::string &msg); +}; + class SocketError : public RuntimeError { public: explicit SocketError(const std::string &msg); diff --git a/slsSupportLib/src/sls_detector_exceptions.cpp b/slsSupportLib/src/sls_detector_exceptions.cpp index d5a6ca645..369c19db8 100644 --- a/slsSupportLib/src/sls_detector_exceptions.cpp +++ b/slsSupportLib/src/sls_detector_exceptions.cpp @@ -14,6 +14,11 @@ RuntimeError::RuntimeError(const char *msg) : runtime_error(msg) { } SharedMemoryError::SharedMemoryError(const std::string &msg) : RuntimeError(msg) {} + +SharedMemoryAlreadyExistsError::SharedMemoryAlreadyExistsError( + const std::string &msg) + : SharedMemoryError(msg) {} + SocketError::SocketError(const std::string &msg) : RuntimeError(msg) {} ZmqSocketError::ZmqSocketError(const std::string &msg) : RuntimeError(msg) {} NotImplementedError::NotImplementedError(const std::string &msg)