Dawn Framework 1.0
Universal data acquisition framework for embedded systems
files.cxx
1// dawn/src/proto/modbus/files.cxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#include "dawn/proto/modbus/regs.hxx"
7
8#include <algorithm>
9#include <cstring>
10#include <inttypes.h>
11#include <new>
12
13#include "dawn/io/common.hxx"
14#include "dawn/io/viewdata.hxx"
15
16using namespace dawn;
17
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;
22
23int CProtoModbusRegs::createFiles(const SProtoModbusIOBind *cfg)
24{
25 CIOCommon *io;
26 SProtoModbusFile *file;
27 uint64_t fileCount;
28 uint64_t end;
29 size_t i;
30
31 // Each IO retains its window + 1 allocation. Consecutive files extend
32 // the byte offset range in blocks of 10000 records.
33
34 fileCount = cfg->config == 0 ? MODBUS_SEEKABLE_WINDOW_DEFAULT : cfg->config;
35 fileCount += 1;
36 end = cfg->start + fileCount * cfg->size;
37
38 if (cfg->size == 0 || fileCount > MODBUS_FILE_NUMBER_MAX || end > MODBUS_FILE_NUMBER_MAX)
39 {
40 DAWNERR("seekable allocation exceeds Modbus file number range\n");
41 return -EINVAL;
42 }
43
44 for (const auto *existing : files)
45 {
46 if (cfg->start + 1 < static_cast<uint32_t>(existing->start) + existing->fileCount &&
47 existing->start < end + 1)
48 {
49 DAWNERR("overlapping seekable file allocations\n");
50 return -EINVAL;
51 }
52 }
53
54 for (i = 0; i < cfg->size; i++)
55 {
56 io = getIO_(cfg->objid[i]);
57 if (io == nullptr || !io->isSeekable() || (!io->isRead() && !io->isWrite()) ||
58 io->getDataSize() > fileCount * MODBUS_FILE_BYTES)
59 {
60 DAWNERR("invalid seekable IO 0x%08" PRIx32 "\n", cfg->objid[i]);
61 return -EINVAL;
62 }
63
64 file = new (std::nothrow) SProtoModbusFile();
65 if (file == nullptr)
66 {
67 DAWNERR("failed to allocate file handler\n");
68 return -ENOMEM;
69 }
70
71 // Descriptor addresses are zero-based; Modbus file numbers start at 1.
72
73 file->io = io;
74 file->start = cfg->start + i * fileCount + 1;
75 file->fileCount = fileCount;
76 files.push_back(file);
77 }
78
79 return OK;
80}
81
82int CProtoModbusRegs::fileCb(uint8_t *buf,
83 uint16_t number,
84 uint16_t record,
85 uint16_t nregs,
86 enum nxmb_regmode_e mode)
87{
88 CIOCommon *io;
89 size_t offset;
90 size_t bytes;
91 size_t size;
92 bool read = (mode == NXMB_REG_READ);
93
94 if (nregs == 0 || static_cast<uint32_t>(record) + nregs > MODBUS_FILE_RECORDS)
95 {
96 return -ENOENT;
97 }
98
99 for (auto *file : files)
100 {
101 if (number < file->start || number >= static_cast<uint32_t>(file->start) + file->fileCount)
102 {
103 continue;
104 }
105
106 std::lock_guard<std::mutex> lock(file->mutex);
107
108 io = file->io;
109 if ((read && !io->isRead()) || (!read && !io->isWrite()))
110 {
111 return -ENOENT;
112 }
113
114 offset = (number - file->start) * MODBUS_FILE_BYTES + record * sizeof(uint16_t);
115 bytes = nregs * sizeof(uint16_t);
116
117 if (read)
118 {
119 // Zero-fill the part of the requested window beyond the IO's end.
120
121 std::memset(buf, 0, bytes);
122 size = io->getDataSize();
123 if (offset >= size)
124 {
125 return OK;
126 }
127
128 bytes = std::min(bytes, size - offset);
129 io_data_view_t data(buf, bytes, bytes);
130
131 return io->getData(data, 1, offset);
132 }
133
134 // Write complete records using the IO's existing offset semantics.
135
136 io_data_view_t data(buf, bytes, bytes);
137
138 return io->setData(data, offset);
139 }
140
141 return -ENOENT;
142}
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).
Definition common.hxx:421
int getData(IODataCmn &data, size_t len, size_t offset=0)
Get data from I/O (public interface with stats tracking).
Definition common.hxx:382
virtual bool isSeekable() const
Check if IO supports partial (seekable) access.
Definition common.hxx:531
virtual size_t getDataSize() const =0
Get data size in bytes.
virtual bool isRead() const =0
Check if IO supports read operations.
int start()
Start object.
Definition object.hxx:116
Out-of-tree user-extension hooks for Dawn.
Definition bindable.hxx:13