From e818c30c8a7b376c5f60df378bbe9403be584389 Mon Sep 17 00:00:00 2001 From: Mateusz Mazur Date: Sun, 10 Nov 2024 13:42:06 +0100 Subject: [PATCH] Clang format in the CI Signed-off-by: Mateusz Mazur --- .clang-format | 2 -- .github/workflows/clang-format.yml | 27 +++++++++++++++++++ example/main.cpp | 2 +- include/MB/TCP/connection.hpp | 8 +++--- include/MB/TCP/server.hpp | 8 +++--- include/MB/modbusCell.hpp | 4 +-- include/MB/modbusException.hpp | 2 +- include/MB/modbusRequest.hpp | 2 +- include/MB/modbusResponse.hpp | 2 +- include/MB/modbusUtils.hpp | 42 +++++++++++++++--------------- reformat-all.sh | 12 ++++++++- src/Serial/connection.cpp | 8 +++--- src/TCP/connection.cpp | 22 ++++++++-------- src/TCP/server.cpp | 6 ++--- src/modbusException.cpp | 14 +++++----- src/modbusRequest.cpp | 34 ++++++++++++------------ src/modbusResponse.cpp | 34 ++++++++++++------------ tests/MB/ModbusFunctionalTests.cpp | 32 +++++++++++------------ tests/MB/ModbusRequestTests.cpp | 12 ++++----- tests/MB/ModbusResponseTests.cpp | 12 ++++----- 20 files changed, 160 insertions(+), 125 deletions(-) create mode 100644 .github/workflows/clang-format.yml diff --git a/.clang-format b/.clang-format index 7295fd4..99d8d06 100644 --- a/.clang-format +++ b/.clang-format @@ -5,7 +5,6 @@ AccessModifierOffset: -2 AlignAfterOpenBracket: Align AlignArrayOfStructures: None AlignConsecutiveAssignments: - Enabled: true AcrossEmptyLines: false AcrossComments: false AlignCompound: false @@ -26,7 +25,6 @@ AlignConsecutiveDeclarations: AlignFunctionPointers: false PadOperators: false AlignConsecutiveMacros: - Enabled: false AcrossEmptyLines: false AcrossComments: false AlignCompound: false diff --git a/.github/workflows/clang-format.yml b/.github/workflows/clang-format.yml new file mode 100644 index 0000000..c8d77ac --- /dev/null +++ b/.github/workflows/clang-format.yml @@ -0,0 +1,27 @@ +name: Clang formatting checker + +on: + push: + branches: ["master"] + pull_request: + branches: ["master"] + +jobs: + build: + runs-on: ubuntu-latest + env: + DRY: "TRUE" + + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 1 + + # Dependecies needed only for Linux - Modbus Communication + - name: Install dependencies + run: | + sudo apt-get update + sudo apt-get install -y clang-format + + - name: Build + run: DRY=TRUE bash reformat-all.sh diff --git a/example/main.cpp b/example/main.cpp index 4d5a361..1228fd9 100644 --- a/example/main.cpp +++ b/example/main.cpp @@ -33,7 +33,7 @@ void createRequest() { // Create CRC and pointer to its bytes uint16_t CRC = MB::utils::calculateCRC(rawed); - auto CRCptr = reinterpret_cast(&CRC); + auto CRCptr = reinterpret_cast(&CRC); // Show byted CRC for request std::cout << "CRC for the above code: "; diff --git a/include/MB/TCP/connection.hpp b/include/MB/TCP/connection.hpp index fc86f8f..cfda15a 100644 --- a/include/MB/TCP/connection.hpp +++ b/include/MB/TCP/connection.hpp @@ -23,9 +23,9 @@ class Connection { static const unsigned int DefaultTCPTimeout = 500; private: - int _sockfd = -1; + int _sockfd = -1; uint16_t _messageID = 0; - int _timeout = Connection::DefaultTCPTimeout; + int _timeout = Connection::DefaultTCPTimeout; public: explicit Connection() noexcept : _sockfd(-1), _messageID(0) {}; @@ -39,8 +39,8 @@ class Connection { if (_sockfd != -1 && _sockfd != other._sockfd) ::close(_sockfd); - _sockfd = other._sockfd; - _messageID = other._messageID; + _sockfd = other._sockfd; + _messageID = other._messageID; other._sockfd = -1; return *this; diff --git a/include/MB/TCP/server.hpp b/include/MB/TCP/server.hpp index 77ae6df..56517b7 100644 --- a/include/MB/TCP/server.hpp +++ b/include/MB/TCP/server.hpp @@ -27,16 +27,16 @@ class Server { Server(const Server &) = delete; Server(Server &&moved) { - _serverfd = moved._serverfd; - _port = moved._port; + _serverfd = moved._serverfd; + _port = moved._port; moved._serverfd = -1; } Server &operator=(Server &&moved) { if (this == &moved) return *this; - _serverfd = moved._serverfd; - _port = moved._port; + _serverfd = moved._serverfd; + _port = moved._port; moved._serverfd = -1; return *this; } diff --git a/include/MB/modbusCell.hpp b/include/MB/modbusCell.hpp index b57faba..6bfa72f 100644 --- a/include/MB/modbusCell.hpp +++ b/include/MB/modbusCell.hpp @@ -38,9 +38,9 @@ class ModbusCell { */ constexpr ModbusCell(bool coil) : _value(coil) {}; - ModbusCell(const ModbusCell &other) noexcept = default; + ModbusCell(const ModbusCell &other) noexcept = default; ModbusCell &operator=(const ModbusCell &other) noexcept = default; - ModbusCell &operator=(ModbusCell &&other) noexcept = default; + ModbusCell &operator=(ModbusCell &&other) noexcept = default; /** * @brief Static wrapper for cell construction. diff --git a/include/MB/modbusException.hpp b/include/MB/modbusException.hpp index e1646f2..14b9d6d 100644 --- a/include/MB/modbusException.hpp +++ b/include/MB/modbusException.hpp @@ -74,7 +74,7 @@ class ModbusException : public std::exception { //! Sets SlaveID void setSlaveID(uint8_t slaveId) noexcept { _validSlave = true; - _slaveId = slaveId; + _slaveId = slaveId; } //! Returns detected error code diff --git a/include/MB/modbusRequest.hpp b/include/MB/modbusRequest.hpp index 8f1487b..09f1bd0 100644 --- a/include/MB/modbusRequest.hpp +++ b/include/MB/modbusRequest.hpp @@ -74,7 +74,7 @@ class ModbusRequest { * object. May be useful in some cases. */ explicit ModbusRequest( - uint8_t slaveId = 0, + uint8_t slaveId = 0, utils::MBFunctionCode functionCode = static_cast(0), uint16_t address = 0, uint16_t registersNumber = 0, std::vector values = {}) noexcept; diff --git a/include/MB/modbusResponse.hpp b/include/MB/modbusResponse.hpp index 5ee74a8..ba5d2a8 100644 --- a/include/MB/modbusResponse.hpp +++ b/include/MB/modbusResponse.hpp @@ -76,7 +76,7 @@ class ModbusResponse { * object. May be useful in some cases. */ ModbusResponse( - uint8_t slaveId = 0, + uint8_t slaveId = 0, utils::MBFunctionCode functionCode = static_cast(0), uint16_t address = 0, uint16_t registersNumber = 0, std::vector values = {}); diff --git a/include/MB/modbusUtils.hpp b/include/MB/modbusUtils.hpp index ec17edd..c137ee8 100644 --- a/include/MB/modbusUtils.hpp +++ b/include/MB/modbusUtils.hpp @@ -24,29 +24,29 @@ namespace MB::utils { */ enum MBErrorCode : uint8_t { // Documentation modbus errors - IllegalFunction = 0x01, - IllegalDataAddress = 0x02, - IllegalDataValue = 0x03, - SlaveDeviceFailure = 0x04, - Acknowledge = 0x05, - SlaveDeviceBusy = 0x06, - NegativeAcknowledge = 0x07, - MemoryParityError = 0x08, - GatewayPathUnavailable = 0x10, + IllegalFunction = 0x01, + IllegalDataAddress = 0x02, + IllegalDataValue = 0x03, + SlaveDeviceFailure = 0x04, + Acknowledge = 0x05, + SlaveDeviceBusy = 0x06, + NegativeAcknowledge = 0x07, + MemoryParityError = 0x08, + GatewayPathUnavailable = 0x10, GatewayTargetDeviceFailedToRespond = 0x11, // Custom modbus errors for Modbus for C++ library ErrorCodeCRCError = 0b0111111, - InvalidCRC = 0b01111110, - InvalidByteOrder = 0b01111101, - InvalidMessageID = 0b01111100, - ProtocolError = 0b01111011, - ConnectionClosed = 0b01111010, - Timeout = 0b01111001, + InvalidCRC = 0b01111110, + InvalidByteOrder = 0b01111101, + InvalidMessageID = 0b01111100, + ProtocolError = 0b01111011, + ConnectionClosed = 0b01111010, + Timeout = 0b01111001, // Specific for modbus response, when number of registers is too big // See issue: https://github.com/Mazurel/Modbus/issues/3 NumberOfRegistersInvalid = 0b01111000, - NumberOfValuesInvalid = 0b01110111, + NumberOfValuesInvalid = 0b01110111, }; //! Checks if error code is modbus standard error code @@ -125,17 +125,17 @@ inline std::string mbErrorCodeToStr(MBErrorCode code) noexcept { //! All modbus standard function codes + Undefined one enum MBFunctionCode : uint8_t { // Reading functions - ReadDiscreteOutputCoils = 0x01, - ReadDiscreteInputContacts = 0x02, + ReadDiscreteOutputCoils = 0x01, + ReadDiscreteInputContacts = 0x02, ReadAnalogOutputHoldingRegisters = 0x03, - ReadAnalogInputRegisters = 0x04, + ReadAnalogInputRegisters = 0x04, // Single write functions - WriteSingleDiscreteOutputCoil = 0x05, + WriteSingleDiscreteOutputCoil = 0x05, WriteSingleAnalogOutputRegister = 0x06, // Multiple write functions - WriteMultipleDiscreteOutputCoils = 0x0F, + WriteMultipleDiscreteOutputCoils = 0x0F, WriteMultipleAnalogOutputHoldingRegisters = 0x10, // User defined diff --git a/reformat-all.sh b/reformat-all.sh index bb42286..8be23e0 100644 --- a/reformat-all.sh +++ b/reformat-all.sh @@ -1,3 +1,13 @@ #!/bin/sh -find . -name '*.cpp' -o -name '*.hpp' | xargs clang-format -i -style=file +ALL_FORMATABLE_FILES=$(find src include tests example -name '*.cpp' -o -name '*.hpp') +CLANG_FORMAT_ARGS="-i -style=file --verbose" + +echo "*** Modbus Clang Format Wrapper **" +clang-format --version + +if [[ "$DRY" = "TRUE" ]]; then + exec clang-format --dry-run $CLANG_FORMAT_ARGS $ALL_FORMATABLE_FILES +else + exec clang-format $CLANG_FORMAT_ARGS $ALL_FORMATABLE_FILES +fi diff --git a/src/Serial/connection.cpp b/src/Serial/connection.cpp index fa6a251..01a8ae8 100644 --- a/src/Serial/connection.cpp +++ b/src/Serial/connection.cpp @@ -55,8 +55,8 @@ std::vector Connection::awaitRawMessage() { std::vector data(1024); pollfd waitingFD; - waitingFD.fd = this->_fd; - waitingFD.events = POLLIN; + waitingFD.fd = this->_fd; + waitingFD.events = POLLIN; waitingFD.revents = POLLIN; if (::poll(&waitingFD, 1, _timeout) <= 0) { @@ -148,8 +148,8 @@ std::vector Connection::send(std::vector data) { } Connection::Connection(Connection &&moved) noexcept { - _fd = moved._fd; - _termios = moved._termios; + _fd = moved._fd; + _termios = moved._termios; moved._fd = -1; } diff --git a/src/TCP/connection.cpp b/src/TCP/connection.cpp index 34bcb2a..137a09e 100644 --- a/src/TCP/connection.cpp +++ b/src/TCP/connection.cpp @@ -10,7 +10,7 @@ using namespace MB::TCP; Connection::Connection(const int sockfd) noexcept { - _sockfd = sockfd; + _sockfd = sockfd; _messageID = 0; } @@ -96,8 +96,8 @@ std::vector Connection::sendException(const MB::ModbusException &ex) { std::vector Connection::awaitRawMessage() { pollfd pfd; - pfd.fd = this->_sockfd; - pfd.events = POLLIN; + pfd.fd = this->_sockfd; + pfd.events = POLLIN; pfd.revents = POLLIN; if (::poll(&pfd, 1, 60 * 1000 /* 1 minute means the connection has died */) <= 0) { throw MB::ModbusException(MB::utils::ConnectionClosed); @@ -121,8 +121,8 @@ std::vector Connection::awaitRawMessage() { MB::ModbusRequest Connection::awaitRequest() { pollfd pfd; - pfd.fd = this->_sockfd; - pfd.events = POLLIN; + pfd.fd = this->_sockfd; + pfd.events = POLLIN; pfd.revents = POLLIN; if (::poll(&pfd, 1, 60 * 1000 /* 1 minute means the connection has died */) <= 0) { throw MB::ModbusException(MB::utils::Timeout); @@ -152,8 +152,8 @@ MB::ModbusRequest Connection::awaitRequest() { MB::ModbusResponse Connection::awaitResponse() { pollfd pfd; - pfd.fd = this->_sockfd; - pfd.events = POLLIN; + pfd.fd = this->_sockfd; + pfd.events = POLLIN; pfd.revents = POLLIN; if (::poll(&pfd, 1, this->_timeout) <= 0) { @@ -189,8 +189,8 @@ Connection::Connection(Connection &&moved) noexcept { if (_sockfd != -1 && moved._sockfd != _sockfd) ::close(_sockfd); - _sockfd = moved._sockfd; - _messageID = moved._messageID; + _sockfd = moved._sockfd; + _messageID = moved._messageID; moved._sockfd = -1; } @@ -201,8 +201,8 @@ Connection Connection::with(std::string addr, int port) { sockaddr_in server; server.sin_family = AF_INET; - server.sin_port = ::htons(port); - server.sin_addr = {inet_addr(addr.c_str())}; + server.sin_port = ::htons(port); + server.sin_addr = {inet_addr(addr.c_str())}; if (::connect(sock, reinterpret_cast(&server), sizeof(server)) < 0) throw std::runtime_error("Cannot connect, errno = " + std::to_string(errno)); diff --git a/src/TCP/server.cpp b/src/TCP/server.cpp index 56fac96..2db852a 100644 --- a/src/TCP/server.cpp +++ b/src/TCP/server.cpp @@ -7,7 +7,7 @@ using namespace MB::TCP; Server::Server(int port) { - _port = port; + _port = port; _serverfd = socket(AF_INET, SOCK_STREAM, 0); if (_serverfd == -1) @@ -18,9 +18,9 @@ Server::Server(int port) { _server = {}; - _server.sin_family = AF_INET; + _server.sin_family = AF_INET; _server.sin_addr.s_addr = INADDR_ANY; - _server.sin_port = ::htons(_port); + _server.sin_port = ::htons(_port); if (::bind(_serverfd, reinterpret_cast(&_server), sizeof(_server)) < 0) diff --git a/src/modbusException.cpp b/src/modbusException.cpp index afa7c52..56873a3 100644 --- a/src/modbusException.cpp +++ b/src/modbusException.cpp @@ -15,21 +15,21 @@ using namespace MB; ModbusException::ModbusException(const std::vector &inputData, bool checkCRC) noexcept { const std::size_t PACKET_SIZE_WITHOUT_CRC = 3; - const std::size_t PACKET_SIZE_WITH_CRC = 5; + const std::size_t PACKET_SIZE_WITH_CRC = 5; if (inputData.size() != ((checkCRC) ? PACKET_SIZE_WITH_CRC : PACKET_SIZE_WITHOUT_CRC)) { - _slaveId = 0xFF; + _slaveId = 0xFF; _functionCode = utils::Undefined; - _validSlave = false; - _errorCode = utils::InvalidByteOrder; + _validSlave = false; + _errorCode = utils::InvalidByteOrder; return; } - _slaveId = inputData[0]; + _slaveId = inputData[0]; _functionCode = static_cast(0b01111111 & inputData[1]); - _validSlave = true; - _errorCode = static_cast(inputData[2]); + _validSlave = true; + _errorCode = static_cast(inputData[2]); if (checkCRC) { const auto actualCrc = *reinterpret_cast(&inputData[3]); diff --git a/src/modbusRequest.cpp b/src/modbusRequest.cpp index 2c62b0a..a97ed1e 100644 --- a/src/modbusRequest.cpp +++ b/src/modbusRequest.cpp @@ -39,11 +39,11 @@ ModbusRequest::ModbusRequest(const ModbusRequest &reference) _values(reference.registerValues()) {} ModbusRequest &ModbusRequest::operator=(const ModbusRequest &reference) { - this->_slaveID = reference.slaveID(); - this->_functionCode = reference.functionCode(); - this->_address = reference.registerAddress(); + this->_slaveID = reference.slaveID(); + this->_functionCode = reference.functionCode(); + this->_address = reference.registerAddress(); this->_registersNumber = reference.numberOfRegisters(); - this->_values = reference.registerValues(); + this->_values = reference.registerValues(); return *this; } @@ -52,9 +52,9 @@ ModbusRequest::ModbusRequest(const std::vector &inputData, bool CRC) { if (inputData.size() < 3) throw ModbusException(utils::InvalidByteOrder); - _slaveID = inputData[0]; + _slaveID = inputData[0]; _functionCode = static_cast(inputData[1]); - _address = utils::bigEndianConv(&inputData[2]); + _address = utils::bigEndianConv(&inputData[2]); int crcIndex = -1; int8_t follow; @@ -65,23 +65,23 @@ ModbusRequest::ModbusRequest(const std::vector &inputData, bool CRC) { case utils::ReadAnalogOutputHoldingRegisters: case utils::ReadAnalogInputRegisters: _registersNumber = utils::bigEndianConv(&inputData[4]); - _values = {}; - crcIndex = 3 * 2; + _values = {}; + crcIndex = 3 * 2; break; case utils::WriteSingleDiscreteOutputCoil: _registersNumber = 1; - _values = {ModbusCell::initCoil(inputData[4] == 0xFF)}; - crcIndex = 3 * 2; + _values = {ModbusCell::initCoil(inputData[4] == 0xFF)}; + crcIndex = 3 * 2; break; case utils::WriteSingleAnalogOutputRegister: _registersNumber = 1; - _values = {utils::bigEndianConv(&inputData[4])}; - crcIndex = 3 * 2; + _values = {utils::bigEndianConv(&inputData[4])}; + crcIndex = 3 * 2; break; case utils::WriteMultipleDiscreteOutputCoils: _registersNumber = utils::bigEndianConv(&inputData[4]); - follow = inputData[6]; - _values = std::vector(_registersNumber); + follow = inputData[6]; + _values = std::vector(_registersNumber); for (int8_t i = 0; i < _registersNumber; i++) { _values[i].coil() = inputData[7 + (i / 8)] & (1 << (i % 8)); } @@ -89,8 +89,8 @@ ModbusRequest::ModbusRequest(const std::vector &inputData, bool CRC) { break; case utils::WriteMultipleAnalogOutputHoldingRegisters: _registersNumber = utils::bigEndianConv(&inputData[4]); - follow = inputData[6]; - _values = std::vector(_registersNumber); + follow = inputData[6]; + _values = std::vector(_registersNumber); for (int8_t i = 0; i < _registersNumber; i++) { _values[i].reg() = utils::bigEndianConv(&inputData[i * 2 + 7]); } @@ -108,7 +108,7 @@ ModbusRequest::ModbusRequest(const std::vector &inputData, bool CRC) { auto receivedCRC = *reinterpret_cast(&inputData[crcIndex]); const auto inputDataLen = static_cast(crcIndex); - auto calculatedCRC = MB::CRC::calculateCRC(inputData, inputDataLen); + auto calculatedCRC = MB::CRC::calculateCRC(inputData, inputDataLen); if (receivedCRC != calculatedCRC) { throw ModbusException(utils::InvalidCRC, _slaveID); diff --git a/src/modbusResponse.cpp b/src/modbusResponse.cpp index 5b85a0f..51a8194 100644 --- a/src/modbusResponse.cpp +++ b/src/modbusResponse.cpp @@ -39,11 +39,11 @@ ModbusResponse::ModbusResponse(const ModbusResponse &reference) _values(reference.registerValues()) {} ModbusResponse &ModbusResponse::operator=(const ModbusResponse &reference) { - this->_slaveID = reference.slaveID(); - this->_functionCode = reference.functionCode(); - this->_address = reference.registerAddress(); + this->_slaveID = reference.slaveID(); + this->_functionCode = reference.functionCode(); + this->_address = reference.registerAddress(); this->_registersNumber = reference.numberOfRegisters(); - this->_values = reference.registerValues(); + this->_values = reference.registerValues(); return *this; } @@ -52,7 +52,7 @@ ModbusResponse::ModbusResponse(std::vector inputData, bool CRC) { if (inputData.size() < 3) throw ModbusException(utils::InvalidByteOrder); - _slaveID = inputData[0]; + _slaveID = inputData[0]; _functionCode = static_cast(inputData[1]); if (functionType() != utils::Read) @@ -64,9 +64,9 @@ ModbusResponse::ModbusResponse(std::vector inputData, bool CRC) { switch (_functionCode) { case utils::ReadDiscreteOutputCoils: case utils::ReadDiscreteInputContacts: - bytes = inputData[2]; + bytes = inputData[2]; _registersNumber = bytes * 8; - _values = std::vector(_registersNumber); + _values = std::vector(_registersNumber); for (auto i = 0; i < _registersNumber; i++) { _values[i].coil() = inputData[3 + (i / 8)] & (1 << (i % 8)); } @@ -74,7 +74,7 @@ ModbusResponse::ModbusResponse(std::vector inputData, bool CRC) { break; case utils::ReadAnalogOutputHoldingRegisters: case utils::ReadAnalogInputRegisters: - bytes = inputData[2]; + bytes = inputData[2]; _registersNumber = bytes / 2; for (auto i = 0; i < bytes / 2; i++) { _values.emplace_back(utils::bigEndianConv(&inputData[3 + (i * 2)])); @@ -83,21 +83,21 @@ ModbusResponse::ModbusResponse(std::vector inputData, bool CRC) { break; case utils::WriteSingleDiscreteOutputCoil: _registersNumber = 1; - _address = utils::bigEndianConv(&inputData[2]); - _values = {ModbusCell::initCoil(inputData[4] == 0xFF)}; - crcIndex = 6; + _address = utils::bigEndianConv(&inputData[2]); + _values = {ModbusCell::initCoil(inputData[4] == 0xFF)}; + crcIndex = 6; break; case utils::WriteSingleAnalogOutputRegister: _registersNumber = 1; - _address = utils::bigEndianConv(&inputData[2]); - _values = {ModbusCell::initReg(utils::bigEndianConv(&inputData[4]))}; - crcIndex = 6; + _address = utils::bigEndianConv(&inputData[2]); + _values = {ModbusCell::initReg(utils::bigEndianConv(&inputData[4]))}; + crcIndex = 6; break; case utils::WriteMultipleDiscreteOutputCoils: case utils::WriteMultipleAnalogOutputHoldingRegisters: - _address = utils::bigEndianConv(&inputData[2]); + _address = utils::bigEndianConv(&inputData[2]); _registersNumber = utils::bigEndianConv(&inputData[4]); - crcIndex = 6; + crcIndex = 6; break; default: throw ModbusException(utils::InvalidByteOrder); @@ -111,7 +111,7 @@ ModbusResponse::ModbusResponse(std::vector inputData, bool CRC) { const auto receivedCRC = *reinterpret_cast(&inputData[crcIndex]); - const auto inputDataLen = static_cast(crcIndex); + const auto inputDataLen = static_cast(crcIndex); const auto calculatedCRC = MB::CRC::calculateCRC(inputData, inputDataLen); if (receivedCRC != calculatedCRC) { diff --git a/tests/MB/ModbusFunctionalTests.cpp b/tests/MB/ModbusFunctionalTests.cpp index 78d6417..9e98c17 100644 --- a/tests/MB/ModbusFunctionalTests.cpp +++ b/tests/MB/ModbusFunctionalTests.cpp @@ -39,9 +39,9 @@ std::string ModBusFunctional::vectorIntoString(const std::vector &packe } TEST_F(ModBusFunctional, RequestWithBigAddress) { - constexpr auto TEST_SLAVE_ID = 1; - constexpr auto TEST_FUNCTION_CODE = MB::utils::ReadDiscreteOutputCoils; - constexpr auto TEST_ADDRESS = 301; + constexpr auto TEST_SLAVE_ID = 1; + constexpr auto TEST_FUNCTION_CODE = MB::utils::ReadDiscreteOutputCoils; + constexpr auto TEST_ADDRESS = 301; constexpr auto TEST_REGISTER_NUMBER = 10; std::vector packedRequest = ModbusRequest(TEST_SLAVE_ID, TEST_FUNCTION_CODE, @@ -68,9 +68,9 @@ TEST_F(ModBusFunctional, RequestWithBigAddress) { } TEST_F(ModBusFunctional, InvalidRequest) { - constexpr auto TEST_SLAVE_ID = 1; - constexpr auto TEST_FUNCTION_CODE = MB::utils::WriteMultipleDiscreteOutputCoils; - constexpr auto TEST_ADDRESS = 10; + constexpr auto TEST_SLAVE_ID = 1; + constexpr auto TEST_FUNCTION_CODE = MB::utils::WriteMultipleDiscreteOutputCoils; + constexpr auto TEST_ADDRESS = 10; constexpr auto TEST_REGISTER_NUMBER = 2; constexpr auto TEST_REGISTER_NUMBER_TOO_BIG = 10; const auto TEST_VALUES = {ModbusCell::initCoil(false), ModbusCell::initCoil(true)}; @@ -103,12 +103,12 @@ TEST_F(ModBusFunctional, InvalidRequest) { } TEST_F(ModBusFunctional, InvalidResponse) { - constexpr auto TEST_SLAVE_ID = 1; - constexpr auto TEST_FUNCTION_CODE = MB::utils::ReadAnalogInputRegisters; - constexpr auto TEST_ADDRESS = 10; - constexpr auto TEST_REGISTER_NUMBER = 10; + constexpr auto TEST_SLAVE_ID = 1; + constexpr auto TEST_FUNCTION_CODE = MB::utils::ReadAnalogInputRegisters; + constexpr auto TEST_ADDRESS = 10; + constexpr auto TEST_REGISTER_NUMBER = 10; constexpr auto TEST_REGISTER_NUMBER_TOO_BIG = 300; - const auto TEST_VALUES = {ModbusCell::initCoil(false)}; + const auto TEST_VALUES = {ModbusCell::initCoil(false)}; // With some values but too big register number const auto correctResponse = @@ -151,13 +151,13 @@ TEST_F(ModBusFunctional, InvalidResponse) { } TEST_F(ModBusFunctional, RequestToResponse) { - constexpr auto TEST_SLAVE_ID = 1; - constexpr auto TEST_FUNCTION_CODE = MB::utils::ReadDiscreteOutputCoils; - constexpr auto TEST_ADDRESS = 301; + constexpr auto TEST_SLAVE_ID = 1; + constexpr auto TEST_FUNCTION_CODE = MB::utils::ReadDiscreteOutputCoils; + constexpr auto TEST_ADDRESS = 301; constexpr auto TEST_REGISTER_NUMBER = 10; - auto request = ModbusRequest(TEST_SLAVE_ID, TEST_FUNCTION_CODE, TEST_ADDRESS, - TEST_REGISTER_NUMBER); + auto request = ModbusRequest(TEST_SLAVE_ID, TEST_FUNCTION_CODE, TEST_ADDRESS, + TEST_REGISTER_NUMBER); auto response = ModbusResponse::from(request); ASSERT_EQ(request.functionCode(), response.functionCode()); diff --git a/tests/MB/ModbusRequestTests.cpp b/tests/MB/ModbusRequestTests.cpp index 6570f8c..e020dfc 100644 --- a/tests/MB/ModbusRequestTests.cpp +++ b/tests/MB/ModbusRequestTests.cpp @@ -13,12 +13,12 @@ class ModBusRequest : public ::testing::Test { // Testing data from https://www.simplymodbus.ca/ virtual void SetUp() { - fn1Data = {0x11, 0x01, 0x00, 0x13, 0x00, 0x25, 0x0E, 0x84}; - fn2Data = {0x11, 0x02, 0x00, 0xC4, 0x00, 0x16, 0xBA, 0xA9}; - fn3Data = {0x11, 0x03, 0x00, 0x6B, 0x00, 0x03, 0x76, 0x87}; - fn4Data = {0x11, 0x04, 0x00, 0x08, 0x00, 0x01, 0xB2, 0x98}; - fn5Data = {0x11, 0x05, 0x00, 0xAC, 0xFF, 0x00, 0x4E, 0x8B}; - fn6Data = {0x11, 0x06, 0x00, 0x01, 0x00, 0x03, 0x9A, 0x9B}; + fn1Data = {0x11, 0x01, 0x00, 0x13, 0x00, 0x25, 0x0E, 0x84}; + fn2Data = {0x11, 0x02, 0x00, 0xC4, 0x00, 0x16, 0xBA, 0xA9}; + fn3Data = {0x11, 0x03, 0x00, 0x6B, 0x00, 0x03, 0x76, 0x87}; + fn4Data = {0x11, 0x04, 0x00, 0x08, 0x00, 0x01, 0xB2, 0x98}; + fn5Data = {0x11, 0x05, 0x00, 0xAC, 0xFF, 0x00, 0x4E, 0x8B}; + fn6Data = {0x11, 0x06, 0x00, 0x01, 0x00, 0x03, 0x9A, 0x9B}; fn15Data = {0x11, 0x0F, 0x00, 0x13, 0x00, 0x0A, 0x02, 0xCD, 0x01, 0xBF, 0x0B}; fn16Data = {0x11, 0x10, 0x00, 0x01, 0x00, 0x02, 0x04, 0x00, 0x0A, 0x01, 0x02, 0xC6, 0xF0}; diff --git a/tests/MB/ModbusResponseTests.cpp b/tests/MB/ModbusResponseTests.cpp index 7389c69..123afb9 100644 --- a/tests/MB/ModbusResponseTests.cpp +++ b/tests/MB/ModbusResponseTests.cpp @@ -13,12 +13,12 @@ class ModBusResponse : public ::testing::Test { // Testing data from https://www.simplymodbus.ca/ virtual void SetUp() { - fn1Data = {0x11, 0x01, 0x05, 0xCD, 0x6B, 0xB2, 0x0E, 0x1B, 0x45, 0xE6}; - fn2Data = {0x11, 0x02, 0x03, 0xAC, 0xDB, 0x35, 0x20, 0x18}; - fn3Data = {0x11, 0x03, 0x06, 0xAE, 0x41, 0x56, 0x52, 0x43, 0x40, 0x49, 0xAD}; - fn4Data = {0x11, 0x04, 0x02, 0x00, 0x0A, 0xF8, 0xF4}; - fn5Data = {0x11, 0x05, 0x00, 0xAC, 0xFF, 0x00, 0x4E, 0x8B}; - fn6Data = {0x11, 0x06, 0x00, 0x01, 0x00, 0x03, 0x9A, 0x9B}; + fn1Data = {0x11, 0x01, 0x05, 0xCD, 0x6B, 0xB2, 0x0E, 0x1B, 0x45, 0xE6}; + fn2Data = {0x11, 0x02, 0x03, 0xAC, 0xDB, 0x35, 0x20, 0x18}; + fn3Data = {0x11, 0x03, 0x06, 0xAE, 0x41, 0x56, 0x52, 0x43, 0x40, 0x49, 0xAD}; + fn4Data = {0x11, 0x04, 0x02, 0x00, 0x0A, 0xF8, 0xF4}; + fn5Data = {0x11, 0x05, 0x00, 0xAC, 0xFF, 0x00, 0x4E, 0x8B}; + fn6Data = {0x11, 0x06, 0x00, 0x01, 0x00, 0x03, 0x9A, 0x9B}; fn15Data = {0x11, 0x0F, 0x00, 0x13, 0x00, 0x0A, 0x26, 0x99}; fn16Data = {0x11, 0x10, 0x00, 0x01, 0x00, 0x02, 0x12, 0x98}; }