modbus_rtu_lib: implement library APIs
Implement modbus-rtu library APIs which will be used by
phosphor-modbus-rtu service based on [1].
[1]: https://gerrit.openbmc.org/c/openbmc/docs/+/77318
Tested:
Added a Mock Modbus RTU server using socat which intercepts and replies
to modbus messages for testing.
```
> meson test -C builddir
ninja: Entering directory `/host/repos/Modbus/phosphor-modbus/builddir'
ninja: no work to do.
1/2 test_modbus_commands OK 0.01s
2/2 test_modbus OK 6.02s
Ok: 2
Fail: 0
```
Change-Id: I66cdc8fd930dd6f7ad6888116d1419ad8f8b8ed8
Signed-off-by: Jagpal Singh Gill <paligill@gmail.com>
diff --git a/tests/modbus_server_tester.hpp b/tests/modbus_server_tester.hpp
new file mode 100644
index 0000000..79955a0
--- /dev/null
+++ b/tests/modbus_server_tester.hpp
@@ -0,0 +1,43 @@
+#pragma once
+
+#include "modbus/modbus_message.hpp"
+
+#include <sdbusplus/async.hpp>
+
+using MessageBase = phosphor::modbus::rtu::Message;
+
+namespace phosphor::modbus::test
+{
+
+class MessageIntf : public MessageBase
+{
+ friend class ServerTester;
+};
+
+static constexpr uint8_t testDeviceAddress = 0xa;
+constexpr uint16_t testSuccessReadHoldingRegisterOffset = 0x0102;
+constexpr uint16_t testSuccessReadHoldingRegisterCount = 0x2;
+constexpr uint16_t testSuccessReadHoldingRegisterSegmentedOffset = 0x0103;
+constexpr std::array<uint16_t, testSuccessReadHoldingRegisterCount>
+ testSuccessReadHoldingRegisterResponse = {0x1234, 0x5678};
+constexpr uint16_t testFailureReadHoldingRegister = 0x0105;
+
+class ServerTester
+{
+ public:
+ explicit ServerTester(sdbusplus::async::context& ctx, int fd);
+
+ auto processRequests() -> sdbusplus::async::task<void>;
+
+ private:
+ void processMessage(MessageIntf& request, size_t requestSize,
+ MessageIntf& response, bool& segmentedResponse);
+
+ void processReadHoldingRegisters(MessageIntf& request, size_t requestSize,
+ MessageIntf& response,
+ bool& segmentedResponse);
+
+ int fd;
+ sdbusplus::async::fdio fdioInstance;
+};
+} // namespace phosphor::modbus::test