diff --git a/slsDetectorServers/matterhornServer/CMakeLists.txt b/slsDetectorServers/matterhornServer/CMakeLists.txt index c72e8fa6f..d99cc6e91 100644 --- a/slsDetectorServers/matterhornServer/CMakeLists.txt +++ b/slsDetectorServers/matterhornServer/CMakeLists.txt @@ -30,7 +30,11 @@ if(SLS_USE_SIMULATOR) target_link_libraries(matterhornDetectorServer_virtual PUBLIC slsSupportStatic - slsServerStatic) + slsServerStatic + slsProjectOptions + PRIVATE + slsProjectWarnings + ) set_target_properties(matterhornDetectorServer_virtual PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin @@ -74,7 +78,11 @@ if(SLS_USE_MATTERHORN) target_link_libraries(matterhornDetectorServer PUBLIC slsSupportStatic - slsServerStatic) + slsServerStatic + slsProjectOptions + PRIVATE + slsProjectWarnings + ) if(${CROSS_COMPILE}) # change output directory to stay consitent with c server binaries when cross compiling set(RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin) diff --git a/slsDetectorServers/matterhornServer/include/BaseMatterhornServer.h b/slsDetectorServers/matterhornServer/include/BaseMatterhornServer.h index bd86d59cb..c69269ce5 100644 --- a/slsDetectorServers/matterhornServer/include/BaseMatterhornServer.h +++ b/slsDetectorServers/matterhornServer/include/BaseMatterhornServer.h @@ -53,9 +53,13 @@ class BaseMatterhornServer using ImplType = typename implementation_typetrait::ImplType; protected: - auto *const getImpl() const { return this->getDerivedImpl(); } + auto *getImpl() { return this->getDerivedImpl(); } + + const auto *getImpl() const { return this->getDerivedImpl(); } private: + DerivedServer *getDerived() { return static_cast(this); } + const DerivedServer *getDerived() const { return static_cast(this); } diff --git a/slsDetectorServers/matterhornServer/include/MatterhornServer.h b/slsDetectorServers/matterhornServer/include/MatterhornServer.h index 8240634eb..e568c5e30 100644 --- a/slsDetectorServers/matterhornServer/include/MatterhornServer.h +++ b/slsDetectorServers/matterhornServer/include/MatterhornServer.h @@ -25,7 +25,9 @@ class MatterhornServer : public BaseMatterhornServer { ~MatterhornServer() = default; private: - ImplType *const getImpl() const { return this->getDerivedImpl(); } + 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/include/SPICommunication.h b/slsDetectorServers/matterhornServer/include/SPICommunication.h index ff8a07df7..e10225c92 100644 --- a/slsDetectorServers/matterhornServer/include/SPICommunication.h +++ b/slsDetectorServers/matterhornServer/include/SPICommunication.h @@ -27,8 +27,8 @@ template class SPICommunication { void open_spi(); private: - DerivedSPIModel *const getDerived() { - return static_cast(this); + DerivedSPIModel *getDerived() { + return static_cast(this); } }; @@ -42,7 +42,7 @@ 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) { + if (chip_id >= MatterhornDefs::NUM_CHIPS_PER_MODULE) { throw RuntimeError( fmt::format("Chip id {} is out of range (0-{})", chip_id, MatterhornDefs::NUM_CHIPS_PER_MODULE - 1)); @@ -56,7 +56,7 @@ 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) { + if (chip_id >= MatterhornDefs::NUM_CHIPS_PER_MODULE) { throw RuntimeError( fmt::format("Chip id {} is out of range (0-{})", chip_id, MatterhornDefs::NUM_CHIPS_PER_MODULE - 1)); diff --git a/slsDetectorServers/matterhornServer/include/VirtualMatterhornServer.h b/slsDetectorServers/matterhornServer/include/VirtualMatterhornServer.h index 8c03628b8..b1b45e3a0 100644 --- a/slsDetectorServers/matterhornServer/include/VirtualMatterhornServer.h +++ b/slsDetectorServers/matterhornServer/include/VirtualMatterhornServer.h @@ -21,7 +21,9 @@ class VirtualMatterhornServer ~VirtualMatterhornServer() = default; private: - ImplType *const getImpl() { return this->getDerivedImpl(); } + 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/SPICommunication.cpp b/slsDetectorServers/matterhornServer/src/SPICommunication.cpp index 6f68fadd2..8460714e0 100644 --- a/slsDetectorServers/matterhornServer/src/SPICommunication.cpp +++ b/slsDetectorServers/matterhornServer/src/SPICommunication.cpp @@ -35,7 +35,12 @@ HardwareSPICommunication::spi_read(const size_t n_bytes, const uint8_t chip_id, const uint8_t register_id) const { // allocate dummy data to shift out the data (first byte is command byte) - std::vector dummy_data(n_bytes + 1); + if (n_bytes == std::numeric_limits::max()) { + throw RuntimeError("SPI read size overflow"); + } + + std::vector dummy_data( + n_bytes + 1, std::byte{0x00}); // +1 for the command byte // First byte of the message is 4 bits chip_id then 4 bits register_id dummy_data[0] = @@ -66,13 +71,6 @@ 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; @@ -93,6 +91,10 @@ void HardwareSPICommunication::spi_write(const uint8_t chip_id, const size_t n_bytes = data.size(); + if (n_bytes == std::numeric_limits::max()) { + throw RuntimeError("SPI read size overflow"); + } + // First byte of the message is 4 bits chip_id then 4 bits register_id std::vector write_data(n_bytes + 1); // +1 for the command byte @@ -101,13 +103,6 @@ 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)); - }); - */ - 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()); diff --git a/slsDetectorServers/slsDetectorServer_cpp/include/DetectorServer.h b/slsDetectorServers/slsDetectorServer_cpp/include/DetectorServer.h index e7a557c59..ca934d8b5 100644 --- a/slsDetectorServers/slsDetectorServer_cpp/include/DetectorServer.h +++ b/slsDetectorServers/slsDetectorServer_cpp/include/DetectorServer.h @@ -38,15 +38,24 @@ template class DetectorServer { std::unique_ptr impl; - auto *const getDerivedImpl() const { - return static_cast( + auto *getDerivedImpl() { + return static_cast( + impl.get()); + } + + const auto *getDerivedImpl() const { + return static_cast( impl.get()); } private: /// @brief get derived class - DerivedDetectorServer *const getDerived() { - return static_cast(this); + DerivedDetectorServer *getDerived() { + return static_cast(this); + } + + const DerivedDetectorServer *getDerived() const { + return static_cast(this); } ProcessedResult processFunction(const detFuncs function_id, @@ -423,7 +432,8 @@ ProcessedResult DetectorServer::get_version( ->get_server_version(); // TODO: get Impl from derived server char version_cstr[MAX_STR_LENGTH]{}; - strncpy(version_cstr, version.c_str(), version.size()); + 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!!! diff --git a/slsDetectorServers/slsDetectorServer_cpp/include/RegisterHelperStructs.hpp b/slsDetectorServers/slsDetectorServer_cpp/include/RegisterHelperStructs.hpp index a9421261c..2c3265075 100644 --- a/slsDetectorServers/slsDetectorServer_cpp/include/RegisterHelperStructs.hpp +++ b/slsDetectorServers/slsDetectorServer_cpp/include/RegisterHelperStructs.hpp @@ -35,7 +35,7 @@ template void setRegisterField(uint32_t ®istervalue, const RegisterField ®_field, T field_value) { - if (field_value > reg_field.bitmask) { + if (field_value > static_cast(reg_field.bitmask)) { throw std::invalid_argument( fmt::format("Value {} cannot fit in field with bitmask {}", field_value, reg_field.bitmask));