Commit fed64770040fdf0d92753720d5a0acf52b04d622

Authored by Peter M. Groen
1 parent e4b1f579

Added Request Test

include/request.h
@@ -11,6 +11,10 @@ @@ -11,6 +11,10 @@
11 #include <variant> 11 #include <variant>
12 #include <vector> 12 #include <vector>
13 13
  14 +namespace osdev {
  15 +namespace components {
  16 +namespace modbus {
  17 +
14 /// The response based on the \ref Request made. 18 /// The response based on the \ref Request made.
15 struct Response 19 struct Response
16 { 20 {
@@ -33,6 +37,7 @@ public: @@ -33,6 +37,7 @@ public:
33 /// We only support a small subset of the ModBus Standard. 37 /// We only support a small subset of the ModBus Standard.
34 enum class FunctionCode 38 enum class FunctionCode
35 { 39 {
  40 + FC_UNKNOWN,
36 READ_COILS, 41 READ_COILS,
37 READ_DISCRETE_INPUTS, 42 READ_DISCRETE_INPUTS,
38 READ_HOLDING_REGISTERS, 43 READ_HOLDING_REGISTERS,
@@ -44,6 +49,13 @@ public: @@ -44,6 +49,13 @@ public:
44 WRITE_FILE_RECORD, 49 WRITE_FILE_RECORD,
45 }; 50 };
46 51
  52 + explicit Request()
  53 + : m_functionCode(Request::FunctionCode::FC_UNKNOWN)
  54 + , m_slaveId(0)
  55 + , m_startAddress(0x00)
  56 + , m_numberOfRegisters(0x00)
  57 + {}
  58 +
47 /// Constructor taking all necessary information to create a request. 59 /// Constructor taking all necessary information to create a request.
48 /// 60 ///
49 /// @param functionCode - The code of the action this request wants to perform. 61 /// @param functionCode - The code of the action this request wants to perform.
@@ -51,34 +63,53 @@ public: @@ -51,34 +63,53 @@ public:
51 /// @param startAddress - Register number to start reading from or writing to. 63 /// @param startAddress - Register number to start reading from or writing to.
52 /// @param numberOfItems - Number of items to read or write from start_address. 64 /// @param numberOfItems - Number of items to read or write from start_address.
53 /// @param callback - Function to call when this request returns a response. 65 /// @param callback - Function to call when this request returns a response.
54 - Request(Request::FunctionCode functionCode,  
55 - int slaveId,  
56 - int startAddress,  
57 - int numberOfItems, 66 + Request(Request::FunctionCode function_code,
  67 + int slave_id,
  68 + int start_address,
  69 + int num_of_regs,
58 std::function<bool(Response)> callback) 70 std::function<bool(Response)> callback)
59 - : m_functionCode(functionCode)  
60 - , m_slaveId(slaveId)  
61 - , m_startAddress(startAddress)  
62 - , m_numberOfItems(numberOfItems) 71 + : m_functionCode(function_code)
  72 + , m_slaveId(slave_id)
  73 + , m_startAddress(start_address)
  74 + , m_numberOfRegisters(num_of_regs)
63 { 75 {
64 callbacks.push_back(callback); 76 callbacks.push_back(callback);
65 } 77 }
66 78
  79 + // Begin Getters and setters.
  80 + /// Sets the functioncode for this request
  81 + /// @param function_code - The functioncode enum.
  82 + void setFunctionCode(FunctionCode function_code) {m_functionCode = function_code;}
  83 +
67 /// @return The functioncode this request handles. 84 /// @return The functioncode this request handles.
68 - FunctionCode GetFunctionCode() const {return m_functionCode;} 85 + FunctionCode getFunctionCode() const {return m_functionCode;}
  86 +
  87 + /// Sets the slaveId of the device this request is intended for.
  88 + /// @param slave_id - The slave Id of the device.
  89 + void setSlaveId(const int slave_id) {m_slaveId = slave_id;}
69 90
70 /// @return The slaveID of the device this request is intended for. 91 /// @return The slaveID of the device this request is intended for.
71 - int GetSlaveId() const {return m_slaveId;} 92 + uint8_t getSlaveId() const {return m_slaveId;}
  93 +
  94 + /// Sets the start address of the register(s) we want to read from.
  95 + /// @param start_address - The start address
  96 + void setStartAddress(const uint8_t start_address) {m_startAddress = start_address;}
72 97
73 /// @return The start Address of the register(s) we're reading. 98 /// @return The start Address of the register(s) we're reading.
74 - int GetStartAddress() const {return m_startAddress;} 99 + uint8_t getStartAddress() const {return m_startAddress;}
  100 +
  101 + /// Sets the number of registers we want to read from the start address.
  102 + /// @param num_of_regs - The number of registers to read from the start address
  103 + void setNumberOfRegisters(const uint8_t num_of_regs) {m_numberOfRegisters = num_of_regs;}
75 104
76 /// @return The number of registers we read from the \ref m_startAddress. 105 /// @return The number of registers we read from the \ref m_startAddress.
77 - int GetNumberOfItems() const {return m_numberOfItems;} 106 + uint8_t getNumberOfRegisters() const {return m_numberOfRegisters;}
  107 +
  108 + // End Getters and setters.
78 109
79 /// @return The values read from, or to be written to, the device. 110 /// @return The values read from, or to be written to, the device.
80 /// @note never change the length of this vector. 111 /// @note never change the length of this vector.
81 - std::vector<int> GetDataBuffer() const {return m_dataBuffer;} 112 + std::vector<uint8_t> getDataBuffer() const {return m_dataBuffer;}
82 113
83 /// @return Callbacks registered to call when new data is available. 114 /// @return Callbacks registered to call when new data is available.
84 /// @note Functionn pointer returns False if the device state is OFFLINE. True, otherwise. 115 /// @note Functionn pointer returns False if the device state is OFFLINE. True, otherwise.
@@ -86,17 +117,22 @@ public: @@ -86,17 +117,22 @@ public:
86 117
87 private: 118 private:
88 /// Function code of this request. 119 /// Function code of this request.
89 - FunctionCode m_functionCode; 120 + FunctionCode m_functionCode;
90 121
91 /// SlaveID of this request. 122 /// SlaveID of this request.
92 - int m_slaveId; 123 + uint8_t m_slaveId;
93 124
94 /// Start address of the register for this request 125 /// Start address of the register for this request
95 - int m_startAddress; 126 + uint8_t m_startAddress;
96 127
97 /// Amount of registers to read/write in this request 128 /// Amount of registers to read/write in this request
98 - int m_numberOfItems; 129 + uint8_t m_numberOfRegisters;
99 130
100 /// Data to send to the device; must be the same size as \ref m_numberOfItems. 131 /// Data to send to the device; must be the same size as \ref m_numberOfItems.
101 - std::vector<int> m_dataBuffer; 132 + std::vector<uint8_t> m_dataBuffer;
102 }; 133 };
  134 +
  135 +} /* End namespace modbus */
  136 +} /* End namespace components */
  137 +} /* End namespace osdev */
  138 +
tests/CMakeLists.txt
@@ -5,6 +5,7 @@ @@ -5,6 +5,7 @@
5 # **************************************************************** 5 # ****************************************************************
6 add_executable(modbustest 6 add_executable(modbustest
7 connectionconfigtest.cpp 7 connectionconfigtest.cpp
  8 + requesttest.cpp
8 ) 9 )
9 10
10 target_include_directories(modbustest PRIVATE 11 target_include_directories(modbustest PRIVATE
tests/requesttest.cpp 0 โ†’ 100644
  1 +/****************************************************************
  2 + * Copyright (c)2022 Peter M. Groen
  3 + * This file is licensed under the MIT license found in the
  4 + * LICENSE file in the root directory of this source tree.
  5 + ****************************************************************/
  6 +
  7 +#include <gmock/gmock.h>
  8 +#include <gtest/gtest.h>
  9 +
  10 +#include "request.h"
  11 +
  12 +using namespace osdev::components::modbus;
  13 +
  14 +TEST(RequestTest, RequestDefaults)
  15 +{
  16 + Request oRequest;
  17 +
  18 + // Check for all the default values
  19 + EXPECT_EQ(oRequest.getFunctionCode(), Request::FunctionCode::FC_UNKNOWN);
  20 + EXPECT_EQ(oRequest.getSlaveId(), 0x00);
  21 + EXPECT_EQ(oRequest.getStartAddress(), 0x00);
  22 + EXPECT_EQ(oRequest.getNumberOfRegisters(), 0x00);
  23 +
  24 +}
  25 +
  26 +TEST(RequestTest, RequestSlaveId)
  27 +{
  28 + Request oRequest;
  29 + oRequest.setSlaveId(1);
  30 +
  31 + EXPECT_EQ(oRequest.getSlaveId(), 1);
  32 +}
  33 +
  34 +TEST(RequestTest, RequestStartAddress)
  35 +{
  36 + Request oRequest;
  37 + oRequest.setStartAddress(0x04);
  38 +
  39 + EXPECT_EQ(oRequest.getStartAddress(), 0x04);
  40 +}
  41 +
  42 +TEST(RequestTest, RequestNumberOfRegisters)
  43 +{
  44 + Request oRequest;
  45 + oRequest.setNumberOfRegisters(0x02);
  46 +
  47 + EXPECT_EQ(oRequest.getNumberOfRegisters(), 0x02);
  48 +}
  49 +
  50 +TEST(RequestTest, RequestReadCoils)
  51 +{
  52 + Request oRequest;
  53 + oRequest.setFunctionCode(Request::FunctionCode::READ_COILS);
  54 +
  55 + EXPECT_EQ(oRequest.getFunctionCode(), Request::FunctionCode::READ_COILS);
  56 +}
  57 +
  58 +TEST(RequestTest, RequestReadDiscreteInputs)
  59 +{
  60 + Request oRequest;
  61 + oRequest.setFunctionCode(Request::FunctionCode::READ_DISCRETE_INPUTS);
  62 +
  63 + EXPECT_EQ(oRequest.getFunctionCode(), Request::FunctionCode::READ_DISCRETE_INPUTS);
  64 +}
  65 +
  66 +TEST(RequestTest, RequestReadHoldingRegisters)
  67 +{
  68 + Request oRequest;
  69 + oRequest.setFunctionCode(Request::FunctionCode::READ_HOLDING_REGISTERS);
  70 +
  71 + EXPECT_EQ(oRequest.getFunctionCode(), Request::FunctionCode::READ_HOLDING_REGISTERS);
  72 +}
  73 +
  74 +TEST(RequestTest, RequestReadInputRegisters)
  75 +{
  76 + Request oRequest;
  77 + oRequest.setFunctionCode(Request::FunctionCode::READ_INPUT_REGISTERS);
  78 +
  79 + EXPECT_EQ(oRequest.getFunctionCode(), Request::FunctionCode::READ_INPUT_REGISTERS);
  80 +}
  81 +
  82 +TEST(RequestTest, RequestWriteSingleCoil)
  83 +{
  84 + Request oRequest;
  85 + oRequest.setFunctionCode(Request::FunctionCode::WRITE_SINGLE_COIL);
  86 +
  87 + EXPECT_EQ(oRequest.getFunctionCode(), Request::FunctionCode::WRITE_SINGLE_COIL);
  88 +}
  89 +
  90 +TEST(RequestTest, RequestWriteSingleRegister)
  91 +{
  92 + Request oRequest;
  93 + oRequest.setFunctionCode(Request::FunctionCode::WRITE_SINGLE_REGISTER);
  94 +
  95 + EXPECT_EQ(oRequest.getFunctionCode(), Request::FunctionCode::WRITE_SINGLE_REGISTER);
  96 +}
  97 +
  98 +TEST(RequestTest, RequestWriteMultipleCoils)
  99 +{
  100 + Request oRequest;
  101 + oRequest.setFunctionCode(Request::FunctionCode::WRITE_MULTIPLE_COILS);
  102 +
  103 + EXPECT_EQ(oRequest.getFunctionCode(), Request::FunctionCode::WRITE_MULTIPLE_COILS);
  104 +}
  105 +
  106 +TEST(RequestTest, RequestWriteMultipleRegisters)
  107 +{
  108 + Request oRequest;
  109 + oRequest.setFunctionCode(Request::FunctionCode::WRITE_MULTIPLE_REGISTERS);
  110 +
  111 + EXPECT_EQ(oRequest.getFunctionCode(), Request::FunctionCode::WRITE_MULTIPLE_REGISTERS);
  112 +}
  113 +
  114 +TEST(RequestTest, RequestWriteFileRecord)
  115 +{
  116 + Request oRequest;
  117 + oRequest.setFunctionCode(Request::FunctionCode::WRITE_FILE_RECORD);
  118 +
  119 + EXPECT_EQ(oRequest.getFunctionCode(), Request::FunctionCode::WRITE_FILE_RECORD);
  120 +}