xref: /openbmc/phosphor-modbus/tests/modbus_server_tester.hpp (revision e92aba4516471f5a01d4ab1f93eb9919ec05c21f)
1a32d241bSJagpal Singh Gill #pragma once
2a32d241bSJagpal Singh Gill 
3a32d241bSJagpal Singh Gill #include "modbus/modbus_message.hpp"
4a32d241bSJagpal Singh Gill 
5a32d241bSJagpal Singh Gill #include <sdbusplus/async.hpp>
6a32d241bSJagpal Singh Gill 
7a32d241bSJagpal Singh Gill using MessageBase = phosphor::modbus::rtu::Message;
8a32d241bSJagpal Singh Gill 
9a32d241bSJagpal Singh Gill namespace phosphor::modbus::test
10a32d241bSJagpal Singh Gill {
11a32d241bSJagpal Singh Gill 
12a32d241bSJagpal Singh Gill class MessageIntf : public MessageBase
13a32d241bSJagpal Singh Gill {
14a32d241bSJagpal Singh Gill     friend class ServerTester;
15a32d241bSJagpal Singh Gill };
16a32d241bSJagpal Singh Gill 
17cad9ecf6SJagpal Singh Gill // Read Holding Registers Testing Constants
18a32d241bSJagpal Singh Gill static constexpr uint8_t testDeviceAddress = 0xa;
19a32d241bSJagpal Singh Gill constexpr uint16_t testSuccessReadHoldingRegisterOffset = 0x0102;
20a32d241bSJagpal Singh Gill constexpr uint16_t testSuccessReadHoldingRegisterCount = 0x2;
21a32d241bSJagpal Singh Gill constexpr uint16_t testSuccessReadHoldingRegisterSegmentedOffset = 0x0103;
22*e92aba45SJagpal Singh Gill const std::vector<uint16_t> testSuccessReadHoldingRegisterResponse = {
23*e92aba45SJagpal Singh Gill     0x1234, 0x5678};
24a32d241bSJagpal Singh Gill constexpr uint16_t testFailureReadHoldingRegister = 0x0105;
25a32d241bSJagpal Singh Gill 
26cad9ecf6SJagpal Singh Gill // Device Inventory Testing Constants
27cad9ecf6SJagpal Singh Gill constexpr uint16_t testReadHoldingRegisterModelOffset = 0x0112;
28cad9ecf6SJagpal Singh Gill constexpr uint16_t testReadHoldingRegisterModelCount = 0x8;
29*e92aba45SJagpal Singh Gill const std::vector<uint16_t> testReadHoldingRegisterModel = {
30*e92aba45SJagpal Singh Gill     0x5244, 0x4630, 0x3430, 0x4453, 0x5335, 0x3139, 0x0000, 0x3000};
31cad9ecf6SJagpal Singh Gill constexpr std::string testReadHoldingRegisterModelStr = "RDF040DSS519";
32cad9ecf6SJagpal Singh Gill 
33*e92aba45SJagpal Singh Gill // Device Sensors Testing Constants
34*e92aba45SJagpal Singh Gill constexpr uint16_t testReadHoldingRegisterTempCount = 0x1;
35*e92aba45SJagpal Singh Gill constexpr uint16_t testReadHoldingRegisterTempUnsignedOffset = 0x0113;
36*e92aba45SJagpal Singh Gill const std::vector<uint16_t> testReadHoldingRegisterTempUnsigned = {
37*e92aba45SJagpal Singh Gill     0x0050}; // 80.0
38*e92aba45SJagpal Singh Gill constexpr uint16_t testReadHoldingRegisterTempSignedOffset = 0x0114;
39*e92aba45SJagpal Singh Gill const std::vector<uint16_t> testReadHoldingRegisterTempSigned = {
40*e92aba45SJagpal Singh Gill     0xFFB0}; // -80.0
41*e92aba45SJagpal Singh Gill 
42*e92aba45SJagpal Singh Gill static const std::map<uint16_t, std::tuple<uint16_t, std::vector<uint16_t>>>
43*e92aba45SJagpal Singh Gill     testReadHoldingRegisterMap = {
44*e92aba45SJagpal Singh Gill         {testSuccessReadHoldingRegisterOffset,
45*e92aba45SJagpal Singh Gill          {testSuccessReadHoldingRegisterCount,
46*e92aba45SJagpal Singh Gill           testSuccessReadHoldingRegisterResponse}},
47*e92aba45SJagpal Singh Gill         {testSuccessReadHoldingRegisterSegmentedOffset,
48*e92aba45SJagpal Singh Gill          {testSuccessReadHoldingRegisterCount,
49*e92aba45SJagpal Singh Gill           testSuccessReadHoldingRegisterResponse}},
50*e92aba45SJagpal Singh Gill         {testReadHoldingRegisterModelOffset,
51*e92aba45SJagpal Singh Gill          {testReadHoldingRegisterModelCount, testReadHoldingRegisterModel}},
52*e92aba45SJagpal Singh Gill         {testReadHoldingRegisterTempUnsignedOffset,
53*e92aba45SJagpal Singh Gill          {testReadHoldingRegisterTempCount,
54*e92aba45SJagpal Singh Gill           testReadHoldingRegisterTempUnsigned}},
55*e92aba45SJagpal Singh Gill         {testReadHoldingRegisterTempSignedOffset,
56*e92aba45SJagpal Singh Gill          {testReadHoldingRegisterTempCount, testReadHoldingRegisterTempSigned}},
57*e92aba45SJagpal Singh Gill };
58*e92aba45SJagpal Singh Gill 
59a32d241bSJagpal Singh Gill class ServerTester
60a32d241bSJagpal Singh Gill {
61a32d241bSJagpal Singh Gill   public:
62a32d241bSJagpal Singh Gill     explicit ServerTester(sdbusplus::async::context& ctx, int fd);
63a32d241bSJagpal Singh Gill 
64a32d241bSJagpal Singh Gill     auto processRequests() -> sdbusplus::async::task<void>;
65a32d241bSJagpal Singh Gill 
66a32d241bSJagpal Singh Gill   private:
67a32d241bSJagpal Singh Gill     void processMessage(MessageIntf& request, size_t requestSize,
68a32d241bSJagpal Singh Gill                         MessageIntf& response, bool& segmentedResponse);
69a32d241bSJagpal Singh Gill 
70a32d241bSJagpal Singh Gill     void processReadHoldingRegisters(MessageIntf& request, size_t requestSize,
71a32d241bSJagpal Singh Gill                                      MessageIntf& response,
72a32d241bSJagpal Singh Gill                                      bool& segmentedResponse);
73a32d241bSJagpal Singh Gill 
74a32d241bSJagpal Singh Gill     int fd;
75a32d241bSJagpal Singh Gill     sdbusplus::async::fdio fdioInstance;
76cad9ecf6SJagpal Singh Gill     sdbusplus::async::mutex mutex;
77a32d241bSJagpal Singh Gill };
78a32d241bSJagpal Singh Gill } // namespace phosphor::modbus::test
79