1*7f9d41ddSJagpal Singh Gill #include "base_port.hpp"
2*7f9d41ddSJagpal Singh Gill
3*7f9d41ddSJagpal Singh Gill #include "common/entity_manager_interface.hpp"
4*7f9d41ddSJagpal Singh Gill
5*7f9d41ddSJagpal Singh Gill #include <fcntl.h>
6*7f9d41ddSJagpal Singh Gill
7*7f9d41ddSJagpal Singh Gill #include <phosphor-logging/lg2.hpp>
8*7f9d41ddSJagpal Singh Gill #include <xyz/openbmc_project/Configuration/USBPort/client.hpp>
9*7f9d41ddSJagpal Singh Gill
10*7f9d41ddSJagpal Singh Gill #include <filesystem>
11*7f9d41ddSJagpal Singh Gill #include <format>
12*7f9d41ddSJagpal Singh Gill #include <optional>
13*7f9d41ddSJagpal Singh Gill #include <regex>
14*7f9d41ddSJagpal Singh Gill
15*7f9d41ddSJagpal Singh Gill namespace phosphor::modbus::rtu::port
16*7f9d41ddSJagpal Singh Gill {
17*7f9d41ddSJagpal Singh Gill
18*7f9d41ddSJagpal Singh Gill PHOSPHOR_LOG2_USING;
19*7f9d41ddSJagpal Singh Gill
BasePort(sdbusplus::async::context & ctx,const config::Config & config,const std::string & devicePath)20*7f9d41ddSJagpal Singh Gill BasePort::BasePort(sdbusplus::async::context& ctx, const config::Config& config,
21*7f9d41ddSJagpal Singh Gill const std::string& devicePath) :
22*7f9d41ddSJagpal Singh Gill name(config.name), mutex(config.name)
23*7f9d41ddSJagpal Singh Gill {
24*7f9d41ddSJagpal Singh Gill fd = open(devicePath.c_str(), O_RDWR | O_NOCTTY);
25*7f9d41ddSJagpal Singh Gill if (fd == -1)
26*7f9d41ddSJagpal Singh Gill {
27*7f9d41ddSJagpal Singh Gill throw("Failed to open serial port " + devicePath +
28*7f9d41ddSJagpal Singh Gill " with error: " + strerror(errno));
29*7f9d41ddSJagpal Singh Gill }
30*7f9d41ddSJagpal Singh Gill
31*7f9d41ddSJagpal Singh Gill modbus =
32*7f9d41ddSJagpal Singh Gill std::make_unique<ModbusIntf>(ctx, fd, config.baudRate, config.rtsDelay);
33*7f9d41ddSJagpal Singh Gill if (!modbus)
34*7f9d41ddSJagpal Singh Gill {
35*7f9d41ddSJagpal Singh Gill throw std::runtime_error("Failed to create Modbus interface");
36*7f9d41ddSJagpal Singh Gill }
37*7f9d41ddSJagpal Singh Gill
38*7f9d41ddSJagpal Singh Gill info("Serial port {NAME} created successfully", "NAME", config.name);
39*7f9d41ddSJagpal Singh Gill }
40*7f9d41ddSJagpal Singh Gill
readHoldingRegisters(uint8_t deviceAddress,uint16_t registerOffset,uint32_t baudRate,Parity parity,std::vector<uint16_t> & registers)41*7f9d41ddSJagpal Singh Gill auto BasePort::readHoldingRegisters(
42*7f9d41ddSJagpal Singh Gill uint8_t deviceAddress, uint16_t registerOffset, uint32_t baudRate,
43*7f9d41ddSJagpal Singh Gill Parity parity, std::vector<uint16_t>& registers)
44*7f9d41ddSJagpal Singh Gill -> sdbusplus::async::task<bool>
45*7f9d41ddSJagpal Singh Gill {
46*7f9d41ddSJagpal Singh Gill sdbusplus::async::lock_guard lg{mutex};
47*7f9d41ddSJagpal Singh Gill co_await lg.lock();
48*7f9d41ddSJagpal Singh Gill
49*7f9d41ddSJagpal Singh Gill if (!modbus->setProperties(baudRate, parity))
50*7f9d41ddSJagpal Singh Gill {
51*7f9d41ddSJagpal Singh Gill error("Failed to set serial port properties");
52*7f9d41ddSJagpal Singh Gill co_return false;
53*7f9d41ddSJagpal Singh Gill }
54*7f9d41ddSJagpal Singh Gill
55*7f9d41ddSJagpal Singh Gill debug(
56*7f9d41ddSJagpal Singh Gill "Reading holding registers from device {ADDRESS} {PORT} at offset {OFFSET}",
57*7f9d41ddSJagpal Singh Gill "ADDRESS", deviceAddress, "PORT", name, "OFFSET", registerOffset);
58*7f9d41ddSJagpal Singh Gill
59*7f9d41ddSJagpal Singh Gill auto ret = co_await modbus->readHoldingRegisters(deviceAddress,
60*7f9d41ddSJagpal Singh Gill registerOffset, registers);
61*7f9d41ddSJagpal Singh Gill if (!ret)
62*7f9d41ddSJagpal Singh Gill {
63*7f9d41ddSJagpal Singh Gill error(
64*7f9d41ddSJagpal Singh Gill "Failed to read holding registers from device {ADDRESS} {PORT} at offset "
65*7f9d41ddSJagpal Singh Gill "{OFFSET}",
66*7f9d41ddSJagpal Singh Gill "ADDRESS", deviceAddress, "PORT", name, "OFFSET", registerOffset);
67*7f9d41ddSJagpal Singh Gill }
68*7f9d41ddSJagpal Singh Gill
69*7f9d41ddSJagpal Singh Gill co_return ret;
70*7f9d41ddSJagpal Singh Gill }
71*7f9d41ddSJagpal Singh Gill
72*7f9d41ddSJagpal Singh Gill } // namespace phosphor::modbus::rtu::port
73