xref: /openbmc/phosphor-modbus/tests/modbus_server_tester.hpp (revision cf77ef540b925e10e3078bbdfbd795a0c1d9ff1f)
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;
22e92aba45SJagpal Singh Gill const std::vector<uint16_t> testSuccessReadHoldingRegisterResponse = {
23e92aba45SJagpal 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;
29e92aba45SJagpal Singh Gill const std::vector<uint16_t> testReadHoldingRegisterModel = {
30e92aba45SJagpal Singh Gill     0x5244, 0x4630, 0x3430, 0x4453, 0x5335, 0x3139, 0x0000, 0x3000};
31cad9ecf6SJagpal Singh Gill constexpr std::string testReadHoldingRegisterModelStr = "RDF040DSS519";
32cad9ecf6SJagpal Singh Gill 
33e92aba45SJagpal Singh Gill // Device Sensors Testing Constants
34e92aba45SJagpal Singh Gill constexpr uint16_t testReadHoldingRegisterTempCount = 0x1;
35e92aba45SJagpal Singh Gill constexpr uint16_t testReadHoldingRegisterTempUnsignedOffset = 0x0113;
36e92aba45SJagpal Singh Gill const std::vector<uint16_t> testReadHoldingRegisterTempUnsigned = {
37e92aba45SJagpal Singh Gill     0x0050}; // 80.0
38e92aba45SJagpal Singh Gill constexpr uint16_t testReadHoldingRegisterTempSignedOffset = 0x0114;
39e92aba45SJagpal Singh Gill const std::vector<uint16_t> testReadHoldingRegisterTempSigned = {
40e92aba45SJagpal Singh Gill     0xFFB0}; // -80.0
41e92aba45SJagpal Singh Gill 
42*cf77ef54SJagpal Singh Gill // Device Firmware Testing Constants
43*cf77ef54SJagpal Singh Gill constexpr uint16_t testReadHoldingRegisterFirmwareVersionOffset = 0x0115;
44*cf77ef54SJagpal Singh Gill constexpr uint16_t testReadHoldingRegisterFirmwareVersionCount = 0x2;
45*cf77ef54SJagpal Singh Gill const std::vector<uint16_t> testReadHoldingRegisterFirmwareVersion = {
46*cf77ef54SJagpal Singh Gill     0x5244, 0x4630};
47*cf77ef54SJagpal Singh Gill constexpr std::string testReadHoldingRegisterFirmwareVersionStr = "RDF0";
48*cf77ef54SJagpal Singh Gill 
49e92aba45SJagpal Singh Gill static const std::map<uint16_t, std::tuple<uint16_t, std::vector<uint16_t>>>
50e92aba45SJagpal Singh Gill     testReadHoldingRegisterMap = {
51e92aba45SJagpal Singh Gill         {testSuccessReadHoldingRegisterOffset,
52e92aba45SJagpal Singh Gill          {testSuccessReadHoldingRegisterCount,
53e92aba45SJagpal Singh Gill           testSuccessReadHoldingRegisterResponse}},
54e92aba45SJagpal Singh Gill         {testSuccessReadHoldingRegisterSegmentedOffset,
55e92aba45SJagpal Singh Gill          {testSuccessReadHoldingRegisterCount,
56e92aba45SJagpal Singh Gill           testSuccessReadHoldingRegisterResponse}},
57e92aba45SJagpal Singh Gill         {testReadHoldingRegisterModelOffset,
58e92aba45SJagpal Singh Gill          {testReadHoldingRegisterModelCount, testReadHoldingRegisterModel}},
59e92aba45SJagpal Singh Gill         {testReadHoldingRegisterTempUnsignedOffset,
60e92aba45SJagpal Singh Gill          {testReadHoldingRegisterTempCount,
61e92aba45SJagpal Singh Gill           testReadHoldingRegisterTempUnsigned}},
62e92aba45SJagpal Singh Gill         {testReadHoldingRegisterTempSignedOffset,
63e92aba45SJagpal Singh Gill          {testReadHoldingRegisterTempCount, testReadHoldingRegisterTempSigned}},
64*cf77ef54SJagpal Singh Gill         {testReadHoldingRegisterFirmwareVersionOffset,
65*cf77ef54SJagpal Singh Gill          {testReadHoldingRegisterFirmwareVersionCount,
66*cf77ef54SJagpal Singh Gill           testReadHoldingRegisterFirmwareVersion}},
67e92aba45SJagpal Singh Gill };
68e92aba45SJagpal Singh Gill 
69a32d241bSJagpal Singh Gill class ServerTester
70a32d241bSJagpal Singh Gill {
71a32d241bSJagpal Singh Gill   public:
72a32d241bSJagpal Singh Gill     explicit ServerTester(sdbusplus::async::context& ctx, int fd);
73a32d241bSJagpal Singh Gill 
74a32d241bSJagpal Singh Gill     auto processRequests() -> sdbusplus::async::task<void>;
75a32d241bSJagpal Singh Gill 
76a32d241bSJagpal Singh Gill   private:
77a32d241bSJagpal Singh Gill     void processMessage(MessageIntf& request, size_t requestSize,
78a32d241bSJagpal Singh Gill                         MessageIntf& response, bool& segmentedResponse);
79a32d241bSJagpal Singh Gill 
80a32d241bSJagpal Singh Gill     void processReadHoldingRegisters(MessageIntf& request, size_t requestSize,
81a32d241bSJagpal Singh Gill                                      MessageIntf& response,
82a32d241bSJagpal Singh Gill                                      bool& segmentedResponse);
83a32d241bSJagpal Singh Gill 
84a32d241bSJagpal Singh Gill     int fd;
85a32d241bSJagpal Singh Gill     sdbusplus::async::fdio fdioInstance;
86cad9ecf6SJagpal Singh Gill     sdbusplus::async::mutex mutex;
87a32d241bSJagpal Singh Gill };
88a32d241bSJagpal Singh Gill } // namespace phosphor::modbus::test
89