diff --git a/slsDetectorServers/matterhornServer/include/BaseMatterhornServer.h b/slsDetectorServers/matterhornServer/include/BaseMatterhornServer.h index 1d099404a..c007996ea 100644 --- a/slsDetectorServers/matterhornServer/include/BaseMatterhornServer.h +++ b/slsDetectorServers/matterhornServer/include/BaseMatterhornServer.h @@ -55,7 +55,13 @@ class BaseMatterhornServer ReturnCode get_receiver_parameters(ServerInterface &socket) const; - ReturnCode set_module_position(ServerInterface &socket); + ReturnCode set_source_udp_mac(ServerInterface &socket); + + ReturnCode + set_module_position_and_update_srcudpmac(ServerInterface &socket); + + void set_module_position(const size_t module_row, const size_t module_col, + const size_t module_index); ReturnCode set_counter_mask(ServerInterface &socket); @@ -266,22 +272,9 @@ uint32_t BaseMatterhornServer::getNumTriggers() const { } template -ReturnCode BaseMatterhornServer::set_module_position( - ServerInterface &socket) { - - std::array position_info{}; // [num_modules_in_y, module_index] - try { - int ret = socket.Receive(position_info.data(), - position_info.size() * sizeof(int)); - } catch (const SocketError &e) { - LOG(logERROR) - << "Failed to receive num modules in y dimension and module index: " - << e.what(); - return ReturnCode::FAIL; - } - - const size_t module_row = position_info[1] % position_info[0]; - const size_t module_col = position_info[1] / position_info[0]; +void BaseMatterhornServer::set_module_position( + const size_t module_row, const size_t module_col, + const size_t module_index) { // write to register uint32_t register_value_LSB{}; @@ -295,13 +288,13 @@ ReturnCode BaseMatterhornServer::set_module_position( } catch (const std::exception &e) { LOG(logERROR) << "Failed to read module position register: " << e.what(); - return ReturnCode::FAIL; + throw; } setRegisterField(register_value_LSB, Reg::ModuleRow, module_row); setRegisterField(register_value_LSB, Reg::ModuleCol, module_col); setRegisterField(register_value_MSB, Reg::ModuleCoordz, 0); - setRegisterField(register_value_MSB, Reg::ModuleIndex, position_info[1]); + setRegisterField(register_value_MSB, Reg::ModuleIndex, module_index); try { busCommunication.writeRegister(Reg::Frame_HDR_ModCoord_LSB_Reg, @@ -311,25 +304,8 @@ ReturnCode BaseMatterhornServer::set_module_position( } catch (const std::exception &e) { LOG(logERROR) << "Failed to write module position register: " << e.what(); - return ReturnCode::FAIL; + throw; } - - // configure mac address based on module position - if (this->udpDetails[0].srcmac == - 0) { // only configure if source mac address is not set already - this->udpDetails[0].srcmac = generaterandomMacAddress(); - uint64_t newSrcMac = (this->udpDetails[0].srcmac & 0xffffffffffff0000) | - (module_row << 16) | module_col; - try { - this->updateSrcMacAddress(newSrcMac); - } catch (const std::invalid_argument &e) { - LOG(logERROR) << "Failed to update source MAC address: " - << e.what(); - return ReturnCode::FAIL; - } - } - - return static_cast(socket.Send(ReturnCode::OK)); } template @@ -400,4 +376,18 @@ ReturnCode BaseMatterhornServer::get_counter_mask( return static_cast(socket.sendResult(actual_counter_mask)); } +template +ReturnCode BaseMatterhornServer::set_source_udp_mac( + ServerInterface &socket) { + return static_cast(this)->set_source_udp_mac(socket); +} + +template +ReturnCode +BaseMatterhornServer::set_module_position_and_update_srcudpmac( + ServerInterface &socket) { + return static_cast(this) + ->set_module_position_and_update_srcudpmac(socket); +} + } // namespace sls \ No newline at end of file diff --git a/slsDetectorServers/matterhornServer/include/MatterhornServer.h b/slsDetectorServers/matterhornServer/include/MatterhornServer.h index 586818b71..c3299123b 100644 --- a/slsDetectorServers/matterhornServer/include/MatterhornServer.h +++ b/slsDetectorServers/matterhornServer/include/MatterhornServer.h @@ -22,6 +22,11 @@ class MatterhornServer : public BaseMatterhornServer { ~MatterhornServer() = default; ReturnCode initial_checks(ServerInterface &socket); + + ReturnCode + set_module_position_and_update_srcudpmac(ServerInterface &socket); + + ReturnCode set_source_udp_mac(ServerInterface &socket); }; } // namespace sls \ No newline at end of file diff --git a/slsDetectorServers/matterhornServer/include/VirtualMatterhornServer.h b/slsDetectorServers/matterhornServer/include/VirtualMatterhornServer.h index 00377bf50..ff03ac67c 100644 --- a/slsDetectorServers/matterhornServer/include/VirtualMatterhornServer.h +++ b/slsDetectorServers/matterhornServer/include/VirtualMatterhornServer.h @@ -21,6 +21,11 @@ class VirtualMatterhornServer ReturnCode initial_checks(ServerInterface &socket); ReturnCode get_run_status(ServerInterface &socket) const; + + ReturnCode + set_module_position_and_update_srcudpmac(ServerInterface &socket); + + ReturnCode set_source_udp_mac(ServerInterface &socket); }; } // namespace sls \ No newline at end of file diff --git a/slsDetectorServers/matterhornServer/src/MatterhornServer.cpp b/slsDetectorServers/matterhornServer/src/MatterhornServer.cpp index a45043e81..92b65bcaf 100644 --- a/slsDetectorServers/matterhornServer/src/MatterhornServer.cpp +++ b/slsDetectorServers/matterhornServer/src/MatterhornServer.cpp @@ -7,6 +7,8 @@ MatterhornServer::MatterhornServer(uint16_t port) // map the IP core base addresses to memory busCommunication.mapToMemory(); // TODO: should this happen in constructor? + spiCommunication.mapToMemory(); // TODO: should this happen in constructor? + // should maybe be part of the constructor? tcpInterface->startTCPServer(); @@ -23,4 +25,55 @@ ReturnCode MatterhornServer::initial_checks(ServerInterface &socket) { return static_cast(socket.sendResult(initial_checks_passed)); } +ReturnCode MatterhornServer::set_module_position_and_update_srcudpmac( + ServerInterface &socket) { + + std::array position_info{}; // [num_modules_in_y, module_index] + try { + int ret = socket.Receive(position_info.data(), + position_info.size() * sizeof(int)); + } catch (const SocketError &e) { + LOG(logERROR) + << "Failed to receive num modules in y dimension and module index: " + << e.what(); + return ReturnCode::FAIL; + } + + const size_t module_row = position_info[1] % position_info[0]; + const size_t module_col = position_info[1] / position_info[0]; + + try { + this->set_module_position(module_row, module_col, position_info[1]); + } catch (const std::exception &e) { + LOG(logERROR) << "Failed to set module position: " << e.what(); + return ReturnCode::FAIL; + } + + // configure mac address based on module position + + // TODO: update + if (this->udpDetails[0].srcmac == + 0) { // only configure if source mac address is not set already + this->udpDetails[0].srcmac = generaterandomMacAddress(); + uint64_t newSrcMac = (this->udpDetails[0].srcmac & 0xffffffffffff0000) | + (module_row << 16) | module_col; + try { + this->updateSrcMacAddress(newSrcMac); + } catch (const std::invalid_argument &e) { + LOG(logERROR) << "Failed to update source MAC address: " + << e.what(); + return ReturnCode::FAIL; + } + } + + return static_cast(socket.Send(ReturnCode::OK)); +} + +ReturnCode MatterhornServer::set_source_udp_mac(ServerInterface &socket) { + + LOG(logERROR) << "Cannot overwrite vendor specific source UDP MAC address."; + + return static_cast(socket.Send(ReturnCode::FAIL)); +} + } // namespace sls \ No newline at end of file diff --git a/slsDetectorServers/matterhornServer/src/VirtualMatterhornServer.cpp b/slsDetectorServers/matterhornServer/src/VirtualMatterhornServer.cpp index e502d01a2..659761a39 100644 --- a/slsDetectorServers/matterhornServer/src/VirtualMatterhornServer.cpp +++ b/slsDetectorServers/matterhornServer/src/VirtualMatterhornServer.cpp @@ -51,4 +51,68 @@ VirtualMatterhornServer::get_run_status(ServerInterface &socket) const { return static_cast(socket.sendResult(status)); } +ReturnCode VirtualMatterhornServer::set_module_position_and_update_srcudpmac( + ServerInterface &socket) { + + std::array position_info{}; // [num_modules_in_y, module_index] + try { + int ret = socket.Receive(position_info.data(), + position_info.size() * sizeof(int)); + } catch (const SocketError &e) { + LOG(logERROR) + << "Failed to receive num modules in y dimension and module index: " + << e.what(); + return ReturnCode::FAIL; + } + + const size_t module_row = position_info[1] % position_info[0]; + const size_t module_col = position_info[1] / position_info[0]; + + try { + this->set_module_position(module_row, module_col, position_info[1]); + } catch (const std::exception &e) { + LOG(logERROR) << "Failed to set module position: " << e.what(); + return ReturnCode::FAIL; + } + + // configure mac address based on module position + if (this->udpDetails[0].srcmac == + 0) { // only configure if source mac address is not set already + uint64_t newSrcMac = generaterandomMacAddress(); + newSrcMac = + (newSrcMac & 0xffffffffffff0000) | (module_row << 16) | module_col; + try { + this->updateSrcMacAddress(newSrcMac); + } catch (const std::invalid_argument &e) { + LOG(logERROR) << "Failed to update source MAC address: " + << e.what(); + return ReturnCode::FAIL; + } + } + + return static_cast(socket.Send(ReturnCode::OK)); +} + +ReturnCode +VirtualMatterhornServer::set_source_udp_mac(ServerInterface &socket) { + uint64_t newsrcudpMac; + + try { + int ret = socket.Receive(newsrcudpMac); + } catch (const SocketError &e) { + LOG(logERROR) << "Failed to receive new source UDP MAC address: " + << e.what(); + return ReturnCode::FAIL; + } + + try { + updateSrcMacAddress(newsrcudpMac); + } catch (const std::invalid_argument &e) { + LOG(logERROR) << "Failed to update source MAC address: " << e.what(); + return ReturnCode::FAIL; + } + + return static_cast(socket.Send(ReturnCode::OK)); +} + } // namespace sls \ No newline at end of file diff --git a/slsDetectorServers/slsDetectorServer_cpp/include/DetectorServer.h b/slsDetectorServers/slsDetectorServer_cpp/include/DetectorServer.h index b77997150..8a7f5333b 100644 --- a/slsDetectorServers/slsDetectorServer_cpp/include/DetectorServer.h +++ b/slsDetectorServers/slsDetectorServer_cpp/include/DetectorServer.h @@ -103,8 +103,6 @@ template class DetectorServer { ReturnCode get_source_udp_mac(ServerInterface &socket) const; - ReturnCode set_source_udp_mac(ServerInterface &socket); - ReturnCode get_source_udp_ip(ServerInterface &socket) const; ReturnCode set_source_udp_ip(ServerInterface &socket); @@ -173,7 +171,8 @@ ReturnCode DetectorServer::processFunction( case detFuncs::F_GET_UPDATE_MODE: return get_update_mode(socket); case detFuncs::F_SET_SOURCE_UDP_MAC: - return set_source_udp_mac(socket); + return static_cast(this)->set_source_udp_mac( + socket); case detFuncs::F_GET_SOURCE_UDP_MAC: return get_source_udp_mac(socket); case detFuncs::F_SET_SOURCE_UDP_IP: @@ -207,8 +206,8 @@ ReturnCode DetectorServer::processFunction( return static_cast(this) ->get_receiver_parameters(socket); case detFuncs::F_SET_POSITION: - return static_cast(this)->set_module_position( - socket); + return static_cast(this) + ->set_module_position_and_update_srcudpmac(socket); default: LOG(logDEBUG) << "Checking specific server functions for function ID: " << function_id; @@ -265,33 +264,11 @@ ReturnCode DetectorServer::get_update_mode( socket.sendResult(static_cast(updateMode))); } -template -ReturnCode DetectorServer::set_source_udp_mac( - ServerInterface &socket) { - uint64_t newsrcudpMac; - - try { - int ret = socket.Receive(newsrcudpMac); - } catch (const SocketError &e) { - LOG(logERROR) << "Failed to receive new source UDP MAC address: " - << e.what(); - return ReturnCode::FAIL; - } - - try { - updateSrcMacAddress(newsrcudpMac); - } catch (const std::invalid_argument &e) { - LOG(logERROR) << "Failed to update source MAC address: " << e.what(); - return ReturnCode::FAIL; - } - - return static_cast(socket.Send(ReturnCode::OK)); -} - template ReturnCode DetectorServer::get_source_udp_mac( ServerInterface &socket) const { - return static_cast(socket.sendResult(udpDetails[0].srcmac)); + auto srcUdpMac = udpDetails[0].srcmac; + return static_cast(socket.sendResult(srcUdpMac)); } template