From 43fedb9b1b95db7022f7456866b5f2418294d80d Mon Sep 17 00:00:00 2001 From: Alice Date: Fri, 3 Jul 2026 15:59:56 +0200 Subject: [PATCH] second Review --- .../matterhornServer/CMakeLists.txt | 1 - .../include/BaseMatterhornServer.h | 16 +++-- .../include/SPICommunication.h | 59 +++++++++---------- .../include/SPIRegisterHelperStructs.hpp | 15 +++-- .../matterhornServer/src/MatterhornServer.cpp | 6 +- .../matterhornServer/src/SPICommunication.cpp | 43 ++++++-------- .../src/VirtualMatterhornServer.cpp | 12 ++-- .../include/DetectorServer.h | 24 ++------ .../include/MemoryModel.hpp | 12 ++-- .../include/RegisterHelperStructs.hpp | 8 +++ .../include/helpers/Defs.hpp | 19 ++++++ .../include/helpers/Helpers.hpp | 38 ++++++++++++ .../slsDetectorServer_cpp/src/MemoryModel.cpp | 19 +++--- .../src/TCPInterface.cpp | 2 +- 14 files changed, 164 insertions(+), 110 deletions(-) create mode 100644 slsDetectorServers/slsDetectorServer_cpp/include/helpers/Defs.hpp create mode 100644 slsDetectorServers/slsDetectorServer_cpp/include/helpers/Helpers.hpp diff --git a/slsDetectorServers/matterhornServer/CMakeLists.txt b/slsDetectorServers/matterhornServer/CMakeLists.txt index 885a0c974..c45b4be52 100644 --- a/slsDetectorServers/matterhornServer/CMakeLists.txt +++ b/slsDetectorServers/matterhornServer/CMakeLists.txt @@ -48,7 +48,6 @@ if(SLS_USE_MATTERHORN) set(CROSS_COMPILE TRUE) endif() - #mmh should slsSupportLib be considered as well when updating version? if(${CROSS_COMPILE}) # only update version if cross compiling binaries find_package(Python3 REQUIRED COMPONENTS Interpreter REQUIRED) add_custom_target(update_server_version ALL diff --git a/slsDetectorServers/matterhornServer/include/BaseMatterhornServer.h b/slsDetectorServers/matterhornServer/include/BaseMatterhornServer.h index b6773795b..9667e388d 100644 --- a/slsDetectorServers/matterhornServer/include/BaseMatterhornServer.h +++ b/slsDetectorServers/matterhornServer/include/BaseMatterhornServer.h @@ -290,10 +290,16 @@ void BaseMatterhornServer::set_module_position( 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, module_index); + try { + 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, module_index); + } catch (const std::exception &e) { + LOG(logERROR) << "Failed to set module position register fields: " + << e.what(); + throw; + } try { busCommunication.writeRegister(Reg::Frame_HDR_ModCoord_LSB_Reg, @@ -313,7 +319,7 @@ BaseMatterhornServer::set_counter_mask(ServerInterface &socket) { uint32_t counter_mask{}; try { - int ret = socket.Receive(counter_mask); + (void)socket.Receive(counter_mask); } catch (const SocketError &e) { LOG(logERROR) << "Failed to receive counter mask: " << e.what(); return ProcessedResult{ReturnCode::FAIL, diff --git a/slsDetectorServers/matterhornServer/include/SPICommunication.h b/slsDetectorServers/matterhornServer/include/SPICommunication.h index 6e3411949..0f68a63e1 100644 --- a/slsDetectorServers/matterhornServer/include/SPICommunication.h +++ b/slsDetectorServers/matterhornServer/include/SPICommunication.h @@ -1,8 +1,10 @@ +#include "MatterhornDefs.hpp" #include "MemoryModel.hpp" #include "SPIRegisterDefs.hpp" #include "SPIRegisterHelperStructs.hpp" #include "fmt/format.h" #include "sls/logger.h" +#include "sls/sls_detector_exceptions.h" #include #include @@ -14,7 +16,7 @@ template class SPICommunication { public: SPICommunication() = default; - ~SPICommunication(); + ~SPICommunication() = default; std::vector SPIread(const SPIRegister &spi_reg, const uint8_t chip_id) const; @@ -22,30 +24,24 @@ template class SPICommunication { void SPIwrite(const SPIRegister &spi_reg, const uint8_t chip_id, const std::vector &data); - void mapToMemory(); - - void unmapMemory(); + void open_spi(); }; template -SPICommunication::~SPICommunication() { - unmapMemory(); -} - -template -void SPICommunication::mapToMemory() { - static_cast(this)->map_to_memory(); -} - -template -void SPICommunication::unmapMemory() { - static_cast(this)->unmap_memory(); +void SPICommunication::open_spi() { + static_cast(this)->open_spi(); } template std::vector SPICommunication::SPIread(const SPIRegister &spi_reg, const uint8_t chip_id) const { + + if (chip_id >= MatterhornDefs::NUM_CHIPS_PER_MODULE || chip_id < 0) { + throw RuntimeError( + fmt::format("Chip id {} is out of range (0-{})", chip_id, + MatterhornDefs::NUM_CHIPS_PER_MODULE - 1)); + } return static_cast(this)->spi_read( spi_reg.n_bytes, chip_id, spi_reg.spi_register_id); } @@ -55,15 +51,17 @@ void SPICommunication::SPIwrite( const SPIRegister &spi_reg, const uint8_t chip_id, const std::vector &data) { + if (chip_id >= MatterhornDefs::NUM_CHIPS_PER_MODULE || chip_id < 0) { + throw RuntimeError( + fmt::format("Chip id {} is out of range (0-{})", chip_id, + MatterhornDefs::NUM_CHIPS_PER_MODULE - 1)); + } + if (data.size() != spi_reg.n_bytes) { - LOG(logERROR) << fmt::format("Data size {} does not match number of " - "bytes {} for SPI register {}", - data.size(), spi_reg.n_bytes, - spi_reg.spi_register_id); - throw std::runtime_error( - fmt::format("Data size {} does not match number of bytes {} for " - "SPI register {}", - data.size(), spi_reg.n_bytes, spi_reg.spi_register_id)); + throw RuntimeError(fmt::format("Data size {} does not match number of " + "bytes {} for SPI register {}", + data.size(), spi_reg.n_bytes, + spi_reg.spi_register_id)); } static_cast(this)->spi_write( @@ -87,9 +85,9 @@ class HardwareSPICommunication public: HardwareSPICommunication() = default; - void map_to_memory(); + ~HardwareSPICommunication(); - void unmap_memory(); + void open_spi(); void spi_write(const uint8_t chip_id, const uint8_t register_id, const std::vector &data); @@ -99,6 +97,8 @@ class HardwareSPICommunication private: int spi_filedescriptor = -1; + + void close_spi(); }; // template // non @@ -145,7 +145,9 @@ class VirtualSPICommunication } } - void map_to_memory() { + ~VirtualSPICommunication() = default; + + void open_spi() { // resize the virtual register memory to the correct size based on // the defined SPI registers @@ -157,9 +159,6 @@ class VirtualSPICommunication } } - void unmap_memory() { // do nothing - } - std::vector spi_read(const size_t n_bytes, const uint8_t chip_id, const uint8_t register_id) const { diff --git a/slsDetectorServers/matterhornServer/include/SPIRegisterHelperStructs.hpp b/slsDetectorServers/matterhornServer/include/SPIRegisterHelperStructs.hpp index ca9b78156..a0dc85afb 100644 --- a/slsDetectorServers/matterhornServer/include/SPIRegisterHelperStructs.hpp +++ b/slsDetectorServers/matterhornServer/include/SPIRegisterHelperStructs.hpp @@ -32,7 +32,7 @@ void inline setSPIRegisterField(std::vector ®ister_value, uint32_t field_value) { // check that the field value can fit in the bitmask - if (field_value > field.num_bits) { + if ((field_value >> field.num_bits) != 0) { throw std::invalid_argument(fmt::format( "Value {} cannot fit in field {}", field_value, field.num_bits)); } @@ -48,12 +48,15 @@ void inline setSPIRegisterField(std::vector ®ister_value, std::byte mask = std::byte(1) << bit_index; - register_value[byte_index] &= - ~mask; // clear the bit in the register value + const bool bit = (field_value >> i) & 0x1; - register_value[byte_index] |= std::byte( - ((field_value >> i) & 0x1) - << bit_index); // set the new field value bit in the register value + if (bit) { + register_value[byte_index] |= + mask; // set the bit in the register value + } else { + register_value[byte_index] &= + ~mask; // clear the bit in the register value + } } } diff --git a/slsDetectorServers/matterhornServer/src/MatterhornServer.cpp b/slsDetectorServers/matterhornServer/src/MatterhornServer.cpp index 401402076..1bd402fec 100644 --- a/slsDetectorServers/matterhornServer/src/MatterhornServer.cpp +++ b/slsDetectorServers/matterhornServer/src/MatterhornServer.cpp @@ -8,7 +8,7 @@ MatterhornServer::MatterhornServer(uint16_t port) busCommunication.mapToMemory(); // TODO: should this happen in constructor? // TODO: need to check if chip is attached - spiCommunication.mapToMemory(); // TODO: should this happen in constructor? + spiCommunication.open_spi(); // TODO: should this happen in constructor? // should maybe be part of the constructor? tcpInterface->startTCPServer(); @@ -32,8 +32,8 @@ ProcessedResult MatterhornServer::set_module_position_and_update_srcudpmac( std::array position_info{}; // [num_modules_in_y, module_index] try { - int ret = socket.Receive(position_info.data(), - position_info.size() * sizeof(int)); + (void)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: " diff --git a/slsDetectorServers/matterhornServer/src/SPICommunication.cpp b/slsDetectorServers/matterhornServer/src/SPICommunication.cpp index e5e423cfd..6f68fadd2 100644 --- a/slsDetectorServers/matterhornServer/src/SPICommunication.cpp +++ b/slsDetectorServers/matterhornServer/src/SPICommunication.cpp @@ -7,27 +7,29 @@ namespace sls { -void HardwareSPICommunication::map_to_memory() { +void HardwareSPICommunication::open_spi() { // TODO device can change spi_filedescriptor = open("/dev/spidev2.0", O_RDWR); // TODO use O_SYNC? + if (spi_filedescriptor < 0) { + throw RuntimeError("Could not open /dev/spidev2.0"); + } + LOG(logINFO) << fmt::format("SPI Read: opened spidev2.0 with fd={}", spi_filedescriptor); - - if (spi_filedescriptor < 0) { - LOG(logERROR) << "Could not open /dev/spidev2.0"; - throw std::runtime_error("Could not open /dev/spidev2.0"); - } } -void HardwareSPICommunication::unmap_memory() { +void HardwareSPICommunication::close_spi() { if (spi_filedescriptor >= 0) { close(spi_filedescriptor); LOG(logINFO) << "SPI Read: closed spidev2.0"; + spi_filedescriptor = -1; } } +HardwareSPICommunication::~HardwareSPICommunication() { close_spi(); } + std::vector HardwareSPICommunication::spi_read(const size_t n_bytes, const uint8_t chip_id, const uint8_t register_id) const { @@ -40,8 +42,7 @@ HardwareSPICommunication::spi_read(const size_t n_bytes, const uint8_t chip_id, static_cast(((chip_id & 0xF) << 4) | (register_id & 0xF)); // allocate data buffer to read out data into - std::vector read_data_buffer( - n_bytes + 1, std::byte{0x00}); // TODO: is it neccessary to initialize? + std::vector read_data_buffer(n_bytes + 1, std::byte{0x00}); spi_ioc_transfer send_cmd{}; send_cmd.len = n_bytes + 1; // +1 for the command byte @@ -51,15 +52,13 @@ HardwareSPICommunication::spi_read(const size_t n_bytes, const uint8_t chip_id, // 0 - Normal operation, 1 - CSN remains zero after operation // We use cs_change = 1 to not close the SPI transaction and // allow for shifting the read out data back in to restore the - // regitster + // register send_cmd.cs_change = 1; // transfer here if (ioctl(spi_filedescriptor, SPI_IOC_MESSAGE(1), &send_cmd) < 0) { - LOG(logERROR) << fmt::format("SPI write failed with {}:{}", errno, - strerror(errno)); - throw std::runtime_error( + throw RuntimeError( fmt::format("SPI write failed with {}:{}", errno, strerror(errno))); } @@ -67,10 +66,12 @@ HardwareSPICommunication::spi_read(const size_t n_bytes, const uint8_t chip_id, std::vector output_data(n_bytes); std::memcpy(output_data.data(), read_data_buffer.data() + 1, n_bytes); + /* LOG(logDEBUG1) << "Read data: "; std::for_each(output_data.begin(), output_data.end(), [](std::byte b) { LOG(logDEBUG1) << fmt::format("{} ", std::to_integer(b)); }); + */ // copy the read out data back to the dummy data buffer to shift it back in send_cmd.tx_buf = send_cmd.rx_buf; @@ -79,10 +80,7 @@ HardwareSPICommunication::spi_read(const size_t n_bytes, const uint8_t chip_id, 0; // end the SPI transaction after shifting back in the data if (ioctl(spi_filedescriptor, SPI_IOC_MESSAGE(1), &send_cmd) < 0) { - - LOG(logERROR) << fmt::format("SPI write failed with {}:{}", errno, - strerror(errno)); - throw std::runtime_error( + throw RuntimeError( fmt::format("SPI write failed with {}:{}", errno, strerror(errno))); } @@ -103,28 +101,23 @@ void HardwareSPICommunication::spi_write(const uint8_t chip_id, std::memcpy(write_data.data() + 1, data.data(), n_bytes); + /* LOG(logDEBUG1) << "Write data: "; std::for_each(write_data.begin(), write_data.end(), [](std::byte b) { LOG(logDEBUG1) << fmt::format("{} ", std::to_integer(b)); }); - - std::vector read_back_buffer(n_bytes + - 1); // TODO: do we need this? + */ spi_ioc_transfer send_cmd{}; send_cmd.len = n_bytes + 1; // +1 for the command byte send_cmd.tx_buf = reinterpret_cast(write_data.data()); - send_cmd.rx_buf = reinterpret_cast(read_back_buffer.data()); send_cmd.cs_change = 0; // end the SPI transaction after the write (we dont need to shift // back in data here since we are not doing a read) if (ioctl(spi_filedescriptor, SPI_IOC_MESSAGE(1), &send_cmd) < 0) { - - LOG(logERROR) << fmt::format("SPI write failed with {}:{}", errno, - strerror(errno)); - throw std::runtime_error( + throw RuntimeError( fmt::format("SPI write failed with {}:{}", errno, strerror(errno))); } } diff --git a/slsDetectorServers/matterhornServer/src/VirtualMatterhornServer.cpp b/slsDetectorServers/matterhornServer/src/VirtualMatterhornServer.cpp index 7916804da..c3c1e83d5 100644 --- a/slsDetectorServers/matterhornServer/src/VirtualMatterhornServer.cpp +++ b/slsDetectorServers/matterhornServer/src/VirtualMatterhornServer.cpp @@ -1,4 +1,6 @@ #include "VirtualMatterhornServer.h" +#include "helpers/Defs.hpp" +#include "helpers/Helpers.hpp" #include "sls/ToString.h" namespace sls { @@ -13,7 +15,7 @@ VirtualMatterhornServer::VirtualMatterhornServer(uint16_t port) // map the IP core base addresses to virtual memory busCommunication.mapToMemory(); - spiCommunication.mapToMemory(); + spiCommunication.open_spi(); // should maybe be part of the constructor? tcpInterface->startTCPServer(); @@ -87,9 +89,8 @@ VirtualMatterhornServer::set_module_position_and_update_srcudpmac( // 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; + uint64_t newSrcMac = + generate_mac_address_from_module_position(module_row, module_col); this->updateSrcMacAddress(newSrcMac); } @@ -112,7 +113,8 @@ VirtualMatterhornServer::set_source_udp_mac(ServerInterface &socket) { std::string(e.what())}; } - if ((newsrcudpMac & 0x020000000000) == 0) { + if ((newsrcudpMac << INDIVIDUAL_GROUP_BIT_OFFSET) == 0 && + (newsrcudpMac << UNIVERSAL_LOCAL_BIT_OFFSET) == 1) { LOG(logERROR) << "Invalid source MAC address: unicast bit or local " "administration bit is not set"; return ProcessedResult{ diff --git a/slsDetectorServers/slsDetectorServer_cpp/include/DetectorServer.h b/slsDetectorServers/slsDetectorServer_cpp/include/DetectorServer.h index 207f63dbe..3a73dc01b 100644 --- a/slsDetectorServers/slsDetectorServer_cpp/include/DetectorServer.h +++ b/slsDetectorServers/slsDetectorServer_cpp/include/DetectorServer.h @@ -26,18 +26,6 @@ struct UDPInfo { }; using ReturnCode = slsDetectorDefs::ReturnCode; -/// @brief generates a random locally administered unicast MAC address for the -/// source UDP -/// @return generated MAC address -inline uint64_t generaterandomMacAddress() { - uint64_t mac = - 0xAA0000000000; // locally administered unicast address (0xA: 0b1010) // - // TODO maybe 0x02000000000 better? - for (int i = 2; i < 5; ++i) { - mac |= (static_cast(rand() % 256) << (i * 8)); - } - return mac; -} /// @brief Shared memory structure for stop server to store run status struct acquisitionStatus { @@ -274,7 +262,7 @@ ProcessedResult DetectorServer::set_source_udp_ip( uint32_t newSrcIp; try { - int ret = socket.Receive(newSrcIp); + (void)socket.Receive(newSrcIp); } catch (const SocketError &e) { LOG(logERROR) << "Failed to receive new source UDP IP address: " << e.what(); @@ -301,7 +289,7 @@ ProcessedResult DetectorServer::set_destination_udp_mac( uint64_t newDstMac; try { - int ret = socket.Receive(newDstMac); + (void)socket.Receive(newDstMac); } catch (const SocketError &e) { LOG(logERROR) << "Failed to receive new destination UDP MAC address: " << e.what(); @@ -330,7 +318,7 @@ ProcessedResult DetectorServer::set_destination_udp_ip( uint32_t newDstIp; try { - int ret = socket.Receive(newDstIp); + (void)socket.Receive(newDstIp); } catch (const SocketError &e) { LOG(logERROR) << "Failed to receive new destination UDP IP address: " << e.what(); @@ -358,7 +346,7 @@ ProcessedResult DetectorServer::set_destination_udp_port( uint16_t newDstPort; try { - int ret = socket.Receive(newDstPort); + (void)socket.Receive(newDstPort); } catch (const SocketError &e) { LOG(logERROR) << "Failed to receive new destination UDP port number: " << e.what(); @@ -402,7 +390,7 @@ ProcessedResult DetectorServer::set_num_frames(ServerInterface &socket) { int64_t num_frames{}; try { - int ret = socket.Receive(num_frames); + (void)socket.Receive(num_frames); } catch (const SocketError &e) { LOG(logERROR) << "Failed to receive number of frames: " << e.what(); return ProcessedResult{ReturnCode::FAIL, @@ -443,7 +431,7 @@ ProcessedResult DetectorServer::set_num_triggers( ServerInterface &socket) { uint32_t num_triggers{}; try { - int ret = socket.Receive(num_triggers); + (void)socket.Receive(num_triggers); } catch (const SocketError &e) { LOG(logERROR) << "Failed to receive number of triggers: " << e.what(); return ProcessedResult{ReturnCode::FAIL, diff --git a/slsDetectorServers/slsDetectorServer_cpp/include/MemoryModel.hpp b/slsDetectorServers/slsDetectorServer_cpp/include/MemoryModel.hpp index 757edadfd..0c04d1b3f 100644 --- a/slsDetectorServers/slsDetectorServer_cpp/include/MemoryModel.hpp +++ b/slsDetectorServers/slsDetectorServer_cpp/include/MemoryModel.hpp @@ -1,6 +1,7 @@ #pragma once #include "fmt/format.h" #include "sls/logger.h" +#include "sls/sls_detector_exceptions.h" #include #include @@ -37,10 +38,11 @@ class HardwareMemoryModel { template class VirtualMemoryModel { public: - VirtualMemoryModel(const uint32_t IPcore_base_address, + // IPcore_base_address is not used for virtual memory model but kept for + // compatibility with HardwareMemoryModel interface + VirtualMemoryModel([[maybe_unused]] const uint32_t IPcore_base_address, const size_t size_memory_space_) - : IPCore_base_address(IPcore_base_address), - size_memory_space(size_memory_space_) {} + : size_memory_space(size_memory_space_) {} ~VirtualMemoryModel() = default; @@ -57,10 +59,6 @@ template class VirtualMemoryModel { private: std::vector mapped_memory{}; - /// @brief offset of the IP core base address in the memory space, used for - /// mapping - const size_t IPCore_base_address{0}; - /// @brief size mapped memory region [bytes] const size_t size_memory_space{0}; }; diff --git a/slsDetectorServers/slsDetectorServer_cpp/include/RegisterHelperStructs.hpp b/slsDetectorServers/slsDetectorServer_cpp/include/RegisterHelperStructs.hpp index 7847b0ce4..a9421261c 100644 --- a/slsDetectorServers/slsDetectorServer_cpp/include/RegisterHelperStructs.hpp +++ b/slsDetectorServers/slsDetectorServer_cpp/include/RegisterHelperStructs.hpp @@ -1,5 +1,7 @@ #pragma once #include +#include +#include #include namespace sls { @@ -32,6 +34,12 @@ struct RegisterField { template void setRegisterField(uint32_t ®istervalue, const RegisterField ®_field, T field_value) { + + if (field_value > reg_field.bitmask) { + throw std::invalid_argument( + fmt::format("Value {} cannot fit in field with bitmask {}", + field_value, reg_field.bitmask)); + } // Clear the bits corresponding to the field registervalue &= ~(reg_field.bitmask << reg_field.bit_position); // Set the new value for the field diff --git a/slsDetectorServers/slsDetectorServer_cpp/include/helpers/Defs.hpp b/slsDetectorServers/slsDetectorServer_cpp/include/helpers/Defs.hpp new file mode 100644 index 000000000..442a1df1d --- /dev/null +++ b/slsDetectorServers/slsDetectorServer_cpp/include/helpers/Defs.hpp @@ -0,0 +1,19 @@ +/** @file Defs.hpp + * @brief this file contains some definitions used in the slsDetectorServer_cpp + * project. + */ +#pragma once +#include + +namespace sls { + +/// @brief Individual/Group bit offset in a 48 bit MAC address - 0 indicates +/// unicast mac address +constexpr uint8_t INDIVIDUAL_GROUP_BIT_OFFSET = 40; // 1000 0000 + +/// @brief Universal/Local bit offset in a 48 bit MAC address - 1 indicates +/// locally administered mac address, 0 indicates universally administered mac +/// address +constexpr uint8_t UNIVERSAL_LOCAL_BIT_OFFSET = 41; // 0100 0000 + +} // namespace sls \ No newline at end of file diff --git a/slsDetectorServers/slsDetectorServer_cpp/include/helpers/Helpers.hpp b/slsDetectorServers/slsDetectorServer_cpp/include/helpers/Helpers.hpp new file mode 100644 index 000000000..cb5be6b26 --- /dev/null +++ b/slsDetectorServers/slsDetectorServer_cpp/include/helpers/Helpers.hpp @@ -0,0 +1,38 @@ +#pragma once +#include +#include + +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 + +/// @brief generates a random locally administered unicast MAC address for the +/// source UDP +/// @return generated MAC address +inline uint64_t generaterandomMacAddress() { + uint64_t mac = + 0xAA0000000000; // locally administered unicast address (0xA: 0b1010) // + // TODO maybe 0x02000000000 better? + for (int i = 2; i < 5; ++i) { + mac |= (static_cast(std::rand() % 256) << (i * 8)); + } + return mac; +} + +/// @brief generates a MAC address based on the module's row and column +/// position, last 32 bits of the MAC address are set to module_row and +/// module_col +/// @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) { + + uint64_t newSrcMac = generaterandomMacAddress(); + newSrcMac = (newSrcMac & mac_mask) | + (module_row << offset_row_position_in_mac) | + (module_col << offset_col_position_in_mac); + + return newSrcMac; +} \ No newline at end of file diff --git a/slsDetectorServers/slsDetectorServer_cpp/src/MemoryModel.cpp b/slsDetectorServers/slsDetectorServer_cpp/src/MemoryModel.cpp index 654bf46b1..0e3ca8967 100644 --- a/slsDetectorServers/slsDetectorServer_cpp/src/MemoryModel.cpp +++ b/slsDetectorServers/slsDetectorServer_cpp/src/MemoryModel.cpp @@ -18,21 +18,21 @@ void HardwareMemoryModel::mapToMemory() { int fd = open("/dev/mem", O_RDWR | O_SYNC, 0); if (fd == -1) { - LOG(logERROR) << "Can't open /dev/mem: " << strerror(errno); - throw std::runtime_error("Can't find /dev/mem"); + throw RuntimeError("Can't find /dev/mem"); } - mapped_memory_ptr = reinterpret_cast( + auto void_mmap_ptr = mmap(nullptr, size_memory_space, PROT_READ | PROT_WRITE, MAP_SHARED, fd, - IPCore_base_address)); + IPCore_base_address); - if (mapped_memory_ptr == - reinterpret_cast(MAP_FAILED)) { - throw std::runtime_error( + if (void_mmap_ptr == MAP_FAILED) { + throw RuntimeError( fmt::format("Failed to map base address: {}", IPCore_base_address)); // TODO: needs ToString } + mapped_memory_ptr = reinterpret_cast(void_mmap_ptr); + close(fd); } @@ -43,9 +43,10 @@ volatile uint32_t *HardwareMemoryModel::getMappedMemoryPtr() const { void HardwareMemoryModel::unmapMemory() { if (mapped_memory_ptr != nullptr) { - if (munmap(const_cast(mapped_memory_ptr), + if (munmap(reinterpret_cast( + const_cast(mapped_memory_ptr)), size_memory_space) < 0) { - throw std::runtime_error( + throw RuntimeError( fmt::format("Failed to unmap memory for IP core: {}", IPCore_base_address)); // TODO: needs ToString } diff --git a/slsDetectorServers/slsDetectorServer_cpp/src/TCPInterface.cpp b/slsDetectorServers/slsDetectorServer_cpp/src/TCPInterface.cpp index 14b18cf1b..665a7f0ac 100644 --- a/slsDetectorServers/slsDetectorServer_cpp/src/TCPInterface.cpp +++ b/slsDetectorServers/slsDetectorServer_cpp/src/TCPInterface.cpp @@ -44,7 +44,7 @@ void TCPInterface::startTCPServerClientConnection() { auto socket = server.accept(); try { - socket.Receive(function_id); + (void)socket.Receive(function_id); if (function_id < 0 || function_id >= NUM_DET_FUNCTIONS) { throw RuntimeError(fmt::format( "{}:{}", UNRECOGNIZED_FNUM_ENUM,