6#include "dawn/proto/modbus/regs.hxx"
13#include "dawn/io/common.hxx"
14#include "dawn/io/viewdata.hxx"
18static constexpr uint32_t MODBUS_FILE_RECORDS = 10000;
19static constexpr uint32_t MODBUS_FILE_BYTES = MODBUS_FILE_RECORDS *
sizeof(uint16_t);
20static constexpr uint32_t MODBUS_FILE_NUMBER_MAX = 65535;
21static constexpr uint32_t MODBUS_SEEKABLE_WINDOW_DEFAULT = 8;
23int CProtoModbusRegs::createFiles(
const SProtoModbusIOBind *cfg)
26 SProtoModbusFile *file;
34 fileCount = cfg->config == 0 ? MODBUS_SEEKABLE_WINDOW_DEFAULT : cfg->config;
36 end = cfg->start + fileCount * cfg->size;
38 if (cfg->size == 0 || fileCount > MODBUS_FILE_NUMBER_MAX || end > MODBUS_FILE_NUMBER_MAX)
40 DAWNERR(
"seekable allocation exceeds Modbus file number range\n");
44 for (
const auto *existing : files)
46 if (cfg->start + 1 <
static_cast<uint32_t
>(existing->start) + existing->fileCount &&
47 existing->start < end + 1)
49 DAWNERR(
"overlapping seekable file allocations\n");
54 for (i = 0; i < cfg->size; i++)
56 io = getIO_(cfg->objid[i]);
60 DAWNERR(
"invalid seekable IO 0x%08" PRIx32
"\n", cfg->objid[i]);
64 file =
new (std::nothrow) SProtoModbusFile();
67 DAWNERR(
"failed to allocate file handler\n");
74 file->
start = cfg->start + i * fileCount + 1;
75 file->fileCount = fileCount;
76 files.push_back(file);
82int CProtoModbusRegs::fileCb(uint8_t *buf,
86 enum nxmb_regmode_e mode)
92 bool read = (mode == NXMB_REG_READ);
94 if (nregs == 0 ||
static_cast<uint32_t
>(record) + nregs > MODBUS_FILE_RECORDS)
99 for (
auto *file : files)
101 if (number < file->start || number >=
static_cast<uint32_t
>(file->start) + file->fileCount)
106 std::lock_guard<std::mutex> lock(file->mutex);
114 offset = (number - file->start) * MODBUS_FILE_BYTES + record *
sizeof(uint16_t);
115 bytes = nregs *
sizeof(uint16_t);
121 std::memset(buf, 0, bytes);
128 bytes = std::min(bytes, size - offset);
129 io_data_view_t data(buf, bytes, bytes);
131 return io->
getData(data, 1, offset);
136 io_data_view_t data(buf, bytes, bytes);
138 return io->
setData(data, offset);
virtual bool isWrite() const =0
Check if IO supports write operations.
int setData(IODataCmn &data, size_t offset=0)
Set data for I/O (public interface with stats tracking).
int getData(IODataCmn &data, size_t len, size_t offset=0)
Get data from I/O (public interface with stats tracking).
virtual bool isSeekable() const
Check if IO supports partial (seekable) access.
virtual size_t getDataSize() const =0
Get data size in bytes.
virtual bool isRead() const =0
Check if IO supports read operations.
Out-of-tree user-extension hooks for Dawn.