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
14{
15 if (inst < 0 || inst >= MAX_DESCRIPTORS)
16 {
17 return -ENOMEM;
18 }
19
20 if (inst > 0 && (reg.ptr == nullptr || reg.len == 0))
21 {
22 return resetSlot(inst);
23 }
24
25 regDesc[inst].ptr = reg.ptr;
26 regDesc[inst].len = reg.len;
27#if CONFIG_DAWN_DESC_SLOTS > 1
28 if (inst > 0)
29 {
30 slotWritten[static_cast<size_t>(inst - 1)] = reg.len;
31 }
32#endif
33
34 return OK;
35}
36
38{
39 if (inst < 0 || inst >= MAX_DESCRIPTORS)
40 {
41 return -ENOMEM;
42 }
43
44 reg.ptr = regDesc[inst].ptr;
45 reg.len = regDesc[inst].len;
46
47 return OK;
48}
49
50int CDevDescriptor::writeSlotData(int inst, const void *data, size_t offset, size_t len)
51{
52#if CONFIG_DAWN_DESC_SLOTS <= 1
53 (void)inst;
54 (void)data;
55 (void)offset;
56 (void)len;
57 return -ENOTSUP;
58#else
59 size_t slot_idx;
60 size_t end;
61
62 if (inst <= 0 || inst >= MAX_DESCRIPTORS)
63 {
64 return -EINVAL;
65 }
66
67 if (len > 0 && data == nullptr)
68 {
69 return -EINVAL;
70 }
71
72 if (offset > SLOT_SIZE || len > SLOT_SIZE - offset)
73 {
74 return -ENOSPC;
75 }
76
77 slot_idx = static_cast<size_t>(inst - 1);
78 end = offset + len;
79
80 if (len > 0)
81 {
82 std::memcpy(&slotBuf[slot_idx][offset], data, len);
83 }
84
85 if (offset == 0)
86 {
87 slotWritten[slot_idx] = len;
88 }
89 else if (end > slotWritten[slot_idx])
90 {
91 slotWritten[slot_idx] = end;
92 }
93
94 regDesc[inst].ptr = slotBuf[slot_idx].data();
95 regDesc[inst].len = slotWritten[slot_idx];
96
97 return OK;
98#endif
99}
100
101size_t CDevDescriptor::getSlotWritten(int inst) const
102{
103#if CONFIG_DAWN_DESC_SLOTS <= 1
104 (void)inst;
105 return 0;
106#else
107 if (inst <= 0 || inst >= MAX_DESCRIPTORS)
108 {
109 return 0;
110 }
111
112 return slotWritten[static_cast<size_t>(inst - 1)];
113#endif
114}
115
117{
118#if CONFIG_DAWN_DESC_SLOTS <= 1
119 (void)inst;
120 return -ENOTSUP;
121#else
122 size_t slot_idx;
123
124 if (inst <= 0 || inst >= MAX_DESCRIPTORS)
125 {
126 return -EINVAL;
127 }
128
129 slot_idx = static_cast<size_t>(inst - 1);
130 std::memset(slotBuf[slot_idx].data(), 0, SLOT_SIZE);
131 slotWritten[slot_idx] = 0;
132 regDesc[inst].ptr = nullptr;
133 regDesc[inst].len = 0;
134
135 return OK;
136#endif
137}
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.