Dawn Framework 1.0
Universal data acquisition framework for embedded systems
descriptor.cxx
1// dawn/src/dev/descriptor.cxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#include "dawn/dev/descriptor.hxx"
7
8#include <cerrno>
9#include <cstring>
10
11using namespace dawn;
12
13CDevDescriptor *CDevDescriptor::singleton = nullptr;
14
16{
17 if (inst < 0 || inst >= MAX_DESCRIPTORS)
18 {
19 return -ENOMEM;
20 }
21
22 if (inst > 0 && (reg.ptr == nullptr || reg.len == 0))
23 {
24 return resetSlot(inst);
25 }
26
27 regDesc[inst].ptr = reg.ptr;
28 regDesc[inst].len = reg.len;
29#if CONFIG_DAWN_DESC_SLOTS > 1
30 if (inst > 0)
31 {
32 slotWritten[static_cast<size_t>(inst - 1)] = reg.len;
33 }
34#endif
35
36 return OK;
37}
38
40{
41 if (inst < 0 || inst >= MAX_DESCRIPTORS)
42 {
43 return -ENOMEM;
44 }
45
46 reg.ptr = regDesc[inst].ptr;
47 reg.len = regDesc[inst].len;
48
49 return OK;
50}
51
52int CDevDescriptor::writeSlotData(int inst, const void *data, size_t offset, size_t len)
53{
54#if CONFIG_DAWN_DESC_SLOTS <= 1
55 (void)inst;
56 (void)data;
57 (void)offset;
58 (void)len;
59 return -ENOTSUP;
60#else
61 size_t slot_idx;
62 size_t end;
63
64 if (inst <= 0 || inst >= MAX_DESCRIPTORS)
65 {
66 return -EINVAL;
67 }
68
69 if (len > 0 && data == nullptr)
70 {
71 return -EINVAL;
72 }
73
74 if (offset > SLOT_SIZE || len > SLOT_SIZE - offset)
75 {
76 return -ENOSPC;
77 }
78
79 slot_idx = static_cast<size_t>(inst - 1);
80 end = offset + len;
81
82 if (len > 0)
83 {
84 std::memcpy(&slotBuf[slot_idx][offset], data, len);
85 }
86
87 if (offset == 0)
88 {
89 slotWritten[slot_idx] = len;
90 }
91 else if (end > slotWritten[slot_idx])
92 {
93 slotWritten[slot_idx] = end;
94 }
95
96 regDesc[inst].ptr = slotBuf[slot_idx].data();
97 regDesc[inst].len = slotWritten[slot_idx];
98
99 return OK;
100#endif
101}
102
103size_t CDevDescriptor::getSlotWritten(int inst) const
104{
105#if CONFIG_DAWN_DESC_SLOTS <= 1
106 (void)inst;
107 return 0;
108#else
109 if (inst <= 0 || inst >= MAX_DESCRIPTORS)
110 {
111 return 0;
112 }
113
114 return slotWritten[static_cast<size_t>(inst - 1)];
115#endif
116}
117
119{
120#if CONFIG_DAWN_DESC_SLOTS <= 1
121 (void)inst;
122 return -ENOTSUP;
123#else
124 size_t slot_idx;
125
126 if (inst <= 0 || inst >= MAX_DESCRIPTORS)
127 {
128 return -EINVAL;
129 }
130
131 slot_idx = static_cast<size_t>(inst - 1);
132 std::memset(slotBuf[slot_idx].data(), 0, SLOT_SIZE);
133 slotWritten[slot_idx] = 0;
134 regDesc[inst].ptr = nullptr;
135 regDesc[inst].len = 0;
136
137 return OK;
138#endif
139}
Descriptor interface available for IO.
int resetSlot(int inst)
Reset a RAM slot.
int getDescriptor(int inst, SDescriptorReg &reg)
Get registered descriptor data for an instance.
int regDescriptor(int inst, const SDescriptorReg &reg)
Register descriptor data for an instance.
int writeSlotData(int inst, const void *data, size_t offset, size_t len)
Write descriptor bytes into a RAM slot.
static int MAX_DESCRIPTORS
Maximum number of descriptors that can be registered.
size_t getSlotWritten(int inst) const
Get valid byte count currently stored in a RAM slot.
Out-of-tree user-extension hooks for Dawn.
Definition bindable.hxx:13
Registered descriptor information.
size_t len
Descriptor length in bytes.
void * ptr
Pointer to descriptor data.