Dawn Framework 1.0
Universal data acquisition framework for embedded systems
simplebase.hxx
1// dawn/include/dawn/proto/simplebase.hxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#pragma once
7
8#include <cstddef>
9#include <cstdint>
10#include <mutex>
11#include <set>
12#include <vector>
13
14#include "dawn/porting/config.hxx"
15#include "dawn/proto/common.hxx"
16
17namespace dawn
18{
19// Forward declaration
20
21class CIOCommon;
22struct io_ddata_t;
23
32{
33public:
36 static constexpr uint8_t FRAME_SYNC = 0xAA;
37 static constexpr uint8_t FRAME_MIN_LEN = 6;
38 static constexpr size_t FRAME_MAX_PAYLOAD = 1024;
39
42 static constexpr size_t SEEK_HDR_SIZE = 8;
43 static constexpr size_t SEEK_CHUNK_CAP = FRAME_MAX_PAYLOAD - SEEK_HDR_SIZE;
44
47 enum
48 {
49 CMD_PING = 0x00,
50 CMD_PONG = 0x01,
51 CMD_GET_IO = 0x10,
52 CMD_SET_IO = 0x11,
53 CMD_GET_IO_SEEK = 0x14,
54 CMD_SET_IO_SEEK = 0x15,
55 CMD_GET_CFG = 0x12,
56 CMD_SET_CFG = 0x13,
57 CMD_GET_INFO = 0x20,
58 CMD_LIST_IOS = 0x21,
59 CMD_SUBSCRIBE = 0x30,
60 CMD_UNSUBSCRIBE = 0x31,
61 CMD_NOTIFY = 0xF0,
62 CMD_ERROR = 0xFF,
63 };
64
67 enum
68 {
69 STATUS_OK = 0x00,
70 STATUS_INVALID_CMD = 0x01,
71 STATUS_INVALID_OBJ = 0x02,
72 STATUS_INVALID_CFG = 0x03,
73 STATUS_READ_ONLY = 0x04,
74 STATUS_WRITE_ONLY = 0x05,
75 STATUS_INVALID_FORMAT = 0x06,
76 STATUS_ERROR = 0xFF,
77 };
78
79 /* @brief I/O direction codes returned by the GET_INFO response */
80
81 enum
82 {
83 IO_TYPE_READ_ONLY = 0x01,
84 IO_TYPE_WRITE_ONLY = 0x02,
85 IO_TYPE_READ_WRITE = 0x03,
86 };
87
90 struct
91 {
93 } typedef SProtoSimpleIOBind;
94
102 : CProtoCommon(desc)
103 , rxbuffer{}
104 , initialized(false)
105 {
106 }
107
108protected:
111 struct
112 {
113 const SProtoSimpleIOBind *cfg;
114 io_ddata_t *iodata;
115 } typedef SProtoSimpleData;
116
117#ifdef CONFIG_DAWN_IO_NOTIFY
120 struct
121 {
123 CIOCommon *io;
124 CProtoSimpleBase *proto;
125 } typedef SProtoSimpleNotify;
126#endif
127
128 uint8_t rxbuffer[FRAME_MAX_PAYLOAD + FRAME_MIN_LEN]; //< Shared receive frame buffer
129 bool initialized; //< Initialization completion flag
130 std::vector<SProtoSimpleIOBind *> iobinds; //< Vector of I/O bind configurations (from descriptor)
131 std::vector<SProtoSimpleData *> iobuffers; //< Vector of runtime I/O data structures
132
133#ifdef CONFIG_DAWN_IO_NOTIFY
134 std::mutex subsMutex; //< Mutex for subscription set access
135 std::set<SObjectId::ObjectId> subscriptions; //< Set of subscribed IO object IDs
136 std::vector<SProtoSimpleNotify *> notifyContexts; //< Vector of notification callback contexts
137#endif
138
147 uint16_t calculateCrc(const uint8_t *data, size_t len);
148
157 int handleFrame(const uint8_t *frame, size_t len);
158
165 void allocObject(SProtoSimpleIOBind *cfg);
166
173 int createBuffers();
174
181 int destroyBuffers();
182
183#ifdef CONFIG_DAWN_IO_NOTIFY
190 int setupNotifications();
191
196 void cleanupNotifications();
197
202 void destroyNotifications();
203
212 static int notifierCb(void *priv, io_ddata_t *data);
213#endif
214
224 virtual int sendFrame(uint8_t cmd, const uint8_t *payload, size_t len) = 0;
225
226private:
234 io_ddata_t *findIOBuffer(SObjectId::ObjectId objid);
235
236 void sendError(uint8_t error_code, uint8_t context);
237
240 void cmdPing();
241
249 void cmdGetIO(const uint8_t *payload, size_t len);
250
258 void cmdSetIO(const uint8_t *payload, size_t len);
259
267 void cmdGetIOSeek(const uint8_t *payload, size_t len);
268
275 void cmdSetIOSeek(const uint8_t *payload, size_t len);
276
284 void cmdGetCfg(const uint8_t *payload, size_t len);
285
293 void cmdSetCfg(const uint8_t *payload, size_t len);
294
302 void cmdGetInfo(const uint8_t *payload, size_t len);
303
308 void cmdListIOs();
309
310#ifdef CONFIG_DAWN_IO_NOTIFY
318 void cmdSubscribe(const uint8_t *payload, size_t len);
319
327 void cmdUnsubscribe(const uint8_t *payload, size_t len);
328#endif
329};
330} // Namespace dawn
Descriptor wrapper for individual object configuration.
Base class for all I/O objects.
Definition common.hxx:27
Base class for all protocol implementations.
Definition common.hxx:23
Shared base for simple framed protocols.
CProtoSimpleBase(CDescObject &desc)
Constructor.
static uint8_t FRAME_SYNC
Frame structure constants.
void allocObject(SProtoSimpleIOBind *cfg)
Store an allocated IO binding.
uint16_t calculateCrc(const uint8_t *data, size_t len)
Calculate 16-bit CRC checksum.
int handleFrame(const uint8_t *frame, size_t len)
Process a received frame.
int createBuffers()
Allocate shared per-IO data buffers.
static size_t SEEK_HDR_SIZE
Seek response constants.
int destroyBuffers()
Destroy shared per-IO data buffers.
virtual int sendFrame(uint8_t cmd, const uint8_t *payload, size_t len)=0
Send a framed response via the derived transport.
Out-of-tree user-extension hooks for Dawn.
Definition bindable.hxx:13
Internal I/O data mapping for simple protocol handlers.
Configuration for binding an I/O object to a simple protocol.
uint32_t ObjectId
ObjectID type - single 32-bit value.
Definition objectid.hxx:44
Heap-allocated dynamic I/O data buffer.
Definition ddata.hxx:21