6#include "dawn/proto/modbus/regs.hxx"
12#include "dawn/io/common.hxx"
13#include "dawn/io/viewdata.hxx"
17static inline void modbus_set_bit(uint8_t *buf, uint16_t bit, uint8_t value)
19 uint8_t *
byte = &buf[bit / 8];
20 uint8_t mask = (uint8_t)(1u << (bit % 8));
28 *
byte &= (uint8_t)~mask;
32static inline uint8_t modbus_get_bit(
const uint8_t *buf, uint16_t bit)
34 return (uint8_t)((buf[bit / 8] >> (bit % 8)) & 0x1u);
38CProtoModbusRegs::findGroup(uint16_t addr, uint16_t n,
const std::vector<SProtoModbusRegs *> ®s)
45 for (
auto it = regs.rbegin(); it != regs.rend(); ++it)
49 if (v ==
nullptr || v->cfg ==
nullptr)
56 if ((addr >= v->cfg->start) && ((addr + n) <= (v->cfg->start + v->regs)))
67 const std::vector<SProtoModbusRegs *> ®s,
71 uint32_t end = start +
static_cast<uint32_t
>(count);
73 for (
auto const *existing : regs)
75 if (existing ==
nullptr || existing->cfg ==
nullptr)
80 uint32_t ex_start = existing->cfg->start;
81 uint32_t ex_end = ex_start +
static_cast<uint32_t
>(existing->regs);
83 if (start < ex_end && ex_start < end)
92void CProtoModbusRegs::cleanupGroups(std::vector<SProtoModbusRegs *> &groups)
94 for (
auto *v : groups)
96 delete[]
reinterpret_cast<uint8_t *
>(v->data);
98 for (
auto *b : v->iodata)
112 return reg->
iodata[index]->getDataSize();
115static inline void modbusReverseBytes(uint8_t *dst,
const uint8_t *src,
size_t len)
117 for (
size_t i = 0; i < len; i++)
119 dst[i] = src[len - 1 - i];
131 size_t regOffset = 0;
133 for (
size_t j = 0; j < reg->
iodata.size(); j++)
135 size_t bytes = getIoRegBytes(reg, j);
137 if (index >=
static_cast<int>(regOffset) && index <
static_cast<int>(regOffset + bytes))
139 *offsetInIo =
static_cast<size_t>(index) - regOffset;
140 return static_cast<int>(j);
149#if defined(CONFIG_DAWN_PROTO_MODBUS_COIL) || defined(CONFIG_DAWN_PROTO_MODBUS_DISCRETE)
150int CProtoModbusRegs::regReadWriteCoil(uint8_t *buff,
154 SProtoModbusRegs *reg)
156 io_ddata_t *iodata =
nullptr;
157 CIOCommon *io =
nullptr;
158 uint8_t *ptr =
nullptr;
167 if (index >= reg->ios)
173 io = getIO_(reg->cfg->objid[index]);
174 iodata = reg->iodata[index];
175 ptr =
reinterpret_cast<uint8_t *
>(iodata->
getDataPtr());
196 for (
size_t i = 0; i < size; i++)
203 for (
size_t i = 0; i < size; i++)
216 index +=
static_cast<int>(size);
217 n -=
static_cast<uint16_t
>(size);
224int CProtoModbusRegs::regReadWriteCoilPacked(uint8_t *buff,
228 SProtoModbusRegs *reg)
230 io_ddata_t *iodata =
nullptr;
231 CIOCommon *io =
nullptr;
232 uint8_t *ptr =
nullptr;
240 memset(buff, 0, (n + 7) / 8);
243 for (uint16_t bit = 0; bit < n; bit++)
245 size_t offsetInIo = 0;
246 const int io_index = findIoIndexByByteOffset(reg, index + bit, &offsetInIo);
248 if (io_index < 0 || io_index >= reg->ios)
259 io = getIO_(reg->cfg->objid[io_index]);
260 iodata = reg->iodata[io_index];
261 ptr =
reinterpret_cast<uint8_t *
>(iodata->
getDataPtr());
264 if (offsetInIo >= size)
282 modbus_set_bit(buff, bit, (ptr[offsetInIo] == 0) ? 0 : 1);
296 ptr[offsetInIo] = modbus_get_bit(buff, bit) ? 1 : 0;
312#if defined(CONFIG_DAWN_PROTO_MODBUS_INPUT) || defined(CONFIG_DAWN_PROTO_MODBUS_HOLDING)
315 size_t regOffset = 0;
316 for (
size_t j = 0; j < reg->
iodata.size(); j++)
318 size_t words = getIoRegBytes(reg, j) /
sizeof(uint16_t);
320 if (index ==
static_cast<int>(regOffset))
322 return static_cast<int>(j);
331int CProtoModbusRegs::standardRegRW(uint8_t *buff,
335 SProtoModbusRegs *reg)
337 io_ddata_t *iodata =
nullptr;
338 CIOCommon *io =
nullptr;
339 uint8_t *ptr =
nullptr;
346 int ioIndex = findIoIndexByRegOffset(reg, index);
347 if (ioIndex < 0 || ioIndex >= reg->ios)
353 io = getIO_(reg->cfg->objid[ioIndex]);
354 iodata = reg->iodata[ioIndex];
355 ptr =
reinterpret_cast<uint8_t *
>(iodata->
getDataPtr());
358 if (words == 0 || words > n)
376 modbusReverseBytes(buff, ptr, words *
sizeof(uint16_t));
377 buff += words *
sizeof(uint16_t);
381 modbusReverseBytes(ptr, buff, words *
sizeof(uint16_t));
382 buff += words *
sizeof(uint16_t);
392 index +=
static_cast<int>(words);
393 n -=
static_cast<uint16_t
>(words);
399int CProtoModbusRegs::regReadWriteHolding(uint8_t *buff,
403 SProtoModbusRegs *reg)
406 int err = standardRegRW(buff, n, index, read, reg);
413CProtoModbusRegs::~CProtoModbusRegs()
417void CProtoModbusRegs::valloc_push_back(SProtoModbusIOBind *alloc)
419 valloc.push_back(alloc);
422static int validateRegisterIo(
CIOCommon *io, uint32_t type)
426#ifdef CONFIG_DAWN_PROTO_MODBUS_COIL
431 DAWNERR(
"seekable IO not supported for coil regs\n");
437 DAWNERR(
"coil reg doesn't support write\n");
443 DAWNERR(
"unsupported IO type for coil\n");
454 DAWNERR(
"seekable IO not supported for coil regs\n");
460 DAWNERR(
"coil reg doesn't support write\n");
466 DAWNERR(
"unsupported IO type for packed coil\n");
474#ifdef CONFIG_DAWN_PROTO_MODBUS_DISCRETE
479 DAWNERR(
"seekable IO not supported for discrete regs\n");
483 if (io->
isRead() ==
false)
485 DAWNERR(
"discrete reg doesn't support read\n");
491 DAWNERR(
"unsupported IO type for discrete\n");
502 DAWNERR(
"seekable IO not supported for discrete regs\n");
506 if (io->
isRead() ==
false)
508 DAWNERR(
"discrete reg doesn't support read\n");
514 DAWNERR(
"unsupported IO type for packed discrete\n");
522#ifdef CONFIG_DAWN_PROTO_MODBUS_INPUT
527 DAWNERR(
"seekable IO not supported for input regs\n");
531 if (io->
isRead() ==
false)
533 DAWNERR(
"input reg doesn't support read\n");
539 if (bytes == 0 || bytes %
sizeof(uint16_t) != 0)
541 DAWNERR(
"unsupported IO type for input\n");
545 return static_cast<int>(bytes);
549#ifdef CONFIG_DAWN_PROTO_MODBUS_HOLDING
554 DAWNERR(
"seekable IO requires file records\n");
560 DAWNERR(
"holding reg doesn't support write\n");
566 if (bytes == 0 || bytes %
sizeof(uint16_t) != 0)
568 DAWNERR(
"unsupported IO type for holding\n");
572 return static_cast<int>(bytes);
578 DAWNERR(
"Unsupported Modbus reg type %" PRIu32
"\n", type);
584int CProtoModbusRegs::finalizeRegGroup(SProtoModbusRegs *tmp,
const SProtoModbusIOBind *v)
588#ifdef CONFIG_DAWN_PROTO_MODBUS_COIL
592 tmp->regs = tmp->size /
sizeof(uint8_t);
593 auto *overlap = hasOverlap(vcoils, v->start, tmp->regs);
594 if (overlap !=
nullptr)
596 DAWNERR(
"overlapping coil ranges: start=%" PRIu32
" count=%zu overlaps start=%" PRIu32
605 vcoils.push_back(tmp);
610#ifdef CONFIG_DAWN_PROTO_MODBUS_DISCRETE
614 tmp->regs = tmp->size /
sizeof(uint8_t);
615 auto *overlap = hasOverlap(vdiscrete, v->start, tmp->regs);
616 if (overlap !=
nullptr)
618 DAWNERR(
"overlapping discrete ranges: start=%" PRIu32
619 " count=%zu overlaps start=%" PRIu32
" count=%zu\n",
627 vdiscrete.push_back(tmp);
632#ifdef CONFIG_DAWN_PROTO_MODBUS_INPUT
635 tmp->regs = tmp->size /
sizeof(uint16_t);
636 auto *overlap = hasOverlap(vinput, v->start, tmp->regs);
637 if (overlap !=
nullptr)
639 DAWNERR(
"overlapping input ranges: start=%" PRIu32
640 " count=%zu overlaps start=%" PRIu32
" count=%zu\n",
648 vinput.push_back(tmp);
653#ifdef CONFIG_DAWN_PROTO_MODBUS_HOLDING
656 tmp->regs = tmp->size /
sizeof(uint16_t);
657 auto *overlap = hasOverlap(vholding, v->start, tmp->regs);
659 if (overlap !=
nullptr)
661 DAWNERR(
"overlapping holding ranges: start=%" PRIu32
662 " count=%zu overlaps start=%" PRIu32
" count=%zu\n",
670 vholding.push_back(tmp);
678 DAWNERR(
"Unsupported Modbus reg type %" PRIu32
"\n", v->type);
686int CProtoModbusRegs::createRegs()
693 for (
auto const *v : valloc)
696#ifdef CONFIG_DAWN_PROTO_MODBUS_SEEKABLE
699 int ret = createFiles(v);
713 DAWNERR(
"failed to allocate register handler\n");
722 tmp->iodata.reserve(v->size);
724 for (
size_t i = 0; i < v->size; i++)
727 io_ddata_t *iobuffer;
729 io = getIO_(v->objid[i]);
732 DAWNERR(
"Failed to get IO 0x%08" PRIx32
"\n", v->objid[i]);
736 int sizeInc = validateRegisterIo(io, v->type);
742 tmp->size +=
static_cast<size_t>(sizeInc);
749 DAWNERR(
"failed to allocate register buffer\n");
753 tmp->iodata.push_back(iobuffer);
756 int ret = finalizeRegGroup(tmp, v);
767int CProtoModbusRegs::destroyRegs()
769#ifdef CONFIG_DAWN_PROTO_MODBUS_COIL
770 cleanupGroups(vcoils);
772#ifdef CONFIG_DAWN_PROTO_MODBUS_DISCRETE
773 cleanupGroups(vdiscrete);
775#ifdef CONFIG_DAWN_PROTO_MODBUS_INPUT
776 cleanupGroups(vinput);
778#ifdef CONFIG_DAWN_PROTO_MODBUS_HOLDING
779 cleanupGroups(vholding);
782#ifdef CONFIG_DAWN_PROTO_MODBUS_SEEKABLE
783 for (
auto *file : files)
797#ifdef CONFIG_DAWN_PROTO_MODBUS_COIL
798int CProtoModbusRegs::coilsCb(uint8_t *buf,
801 enum nxmb_regmode_e mode,
807 bool read = (mode == NXMB_REG_READ);
811 reg = findGroup(addr, ncoils, vcoils);
817 index = (int)(addr - reg->cfg->start);
819 DAWNINFO(
"coilsCb %d %d %d\n", addr, ncoils, index);
823 ret_helper = regReadWriteCoilPacked(buf, ncoils, index, read, reg);
827 ret_helper = regReadWriteCoil(buf, ncoils, index, read, reg);
834#ifdef CONFIG_DAWN_PROTO_MODBUS_DISCRETE
835int CProtoModbusRegs::discreteCb(uint8_t *buf, uint16_t addr, uint16_t ndiscrete,
void *priv)
843 reg = findGroup(addr, ndiscrete, vdiscrete);
849 index = (int)(addr - reg->cfg->start);
851 DAWNINFO(
"discreteCb %d %d %d\n", addr, ndiscrete, index);
855 ret_helper = regReadWriteCoilPacked(buf, ndiscrete, index,
true, reg);
859 ret_helper = regReadWriteCoil(buf, ndiscrete, index,
true, reg);
866#ifdef CONFIG_DAWN_PROTO_MODBUS_INPUT
867int CProtoModbusRegs::inputCb(uint8_t *buf, uint16_t addr, uint16_t nregs,
void *priv)
875 reg = findGroup(addr, nregs, vinput);
881 index = (int)(addr - reg->cfg->start);
883 DAWNINFO(
"inputCb %d %d %d\n", addr, nregs, index);
885 ret_helper = regReadWriteHolding(buf, nregs, index,
true, reg);
891#ifdef CONFIG_DAWN_PROTO_MODBUS_HOLDING
892int CProtoModbusRegs::holdingCb(uint8_t *buf,
895 enum nxmb_regmode_e mode,
901 bool read = (mode == NXMB_REG_READ);
905 reg = findGroup(addr, nregs, vholding);
911 index = (int)(addr - reg->cfg->start);
913 DAWNINFO(
"holdingCb %d %d %d\n", addr, nregs, index);
915 ret_helper = regReadWriteHolding(buf, nregs, index, read, reg);
Base class for all I/O objects.
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 getDataDim() const =0
Get data vector dimension.
io_ddata_t * ddata_alloc(size_t batch, size_t chunk_size=0)
Allocate data buffer for this I/O.
virtual size_t getDataSize() const =0
Get data size in bytes.
virtual bool isRead() const =0
Check if IO supports read operations.
@ MODBUS_TYPE_DISCRETE_PACKED
Read-only bits packed in bytes.
@ MODBUS_TYPE_INPUT
Read-only 16-bit words.
@ MODBUS_TYPE_SEEKABLE
Seekable IO via FC20/FC21 file records.
@ MODBUS_TYPE_COIL_PACKED
Read-write bits packed in bytes.
@ MODBUS_TYPE_COIL
Read-write bits (1 byte/bit).
@ MODBUS_TYPE_DISCRETE
Read-only bits (1 byte/bit).
@ MODBUS_TYPE_HOLDING
Read-write 16-bit words.
Out-of-tree user-extension hooks for Dawn.
uint32_t size
Number of I/O objects in this group.
Runtime register group state.
std::vector< io_ddata_t * > iodata
I/O data for each object.
const SProtoModbusIOBind * cfg
Reference to bind config.
size_t getDataSize()
Get data size in bytes.
void * getDataPtr(size_t batch=0)
Get pointer to data only (skips timestamp if present).