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 , initialized(false)
104 {
105 }
106
107protected:
110 struct
111 {
112 const SProtoSimpleIOBind *cfg;
113 io_ddata_t *iodata;
114 } typedef SProtoSimpleData;
115
116#ifdef CONFIG_DAWN_IO_NOTIFY
119 struct
120 {
122 CIOCommon *io;
123 CProtoSimpleBase *proto;
124 } typedef SProtoSimpleNotify;
125#endif
126
127 uint8_t rxbuffer[FRAME_MAX_PAYLOAD + FRAME_MIN_LEN]; //< Shared receive frame buffer
128 bool initialized; //< Initialization completion flag
129 std::vector<SProtoSimpleIOBind *> iobinds; //< Vector of I/O bind configurations (from descriptor)
130 std::vector<SProtoSimpleData *> iobuffers; //< Vector of runtime I/O data structures
131
132#ifdef CONFIG_DAWN_IO_NOTIFY
133 std::mutex subsMutex; //< Mutex for subscription set access
134 std::set<SObjectId::ObjectId> subscriptions; //< Set of subscribed IO object IDs
135 std::vector<SProtoSimpleNotify *> notifyContexts; //< Vector of notification callback contexts
136#endif
137
146 uint16_t calculateCrc(const uint8_t *data, size_t len);
147
156 int handleFrame(const uint8_t *frame, size_t len);
157
164 void allocObject(SProtoSimpleIOBind *cfg);
165
172 int createBuffers();
173
180 int destroyBuffers();
181
182#ifdef CONFIG_DAWN_IO_NOTIFY
189 int setupNotifications();
190
195 void cleanupNotifications();
196
201 void destroyNotifications();
202
211 static int notifierCb(void *priv, io_ddata_t *data);
212#endif
213
223 virtual int sendFrame(uint8_t cmd, const uint8_t *payload, size_t len) = 0;
224
225private:
233 io_ddata_t *findIOBuffer(SObjectId::ObjectId objid);
234
235 void sendError(uint8_t error_code, uint8_t context);
236
239 void cmdPing();
240
248 void cmdGetIO(const uint8_t *payload, size_t len);
249
257 void cmdSetIO(const uint8_t *payload, size_t len);
258
266 void cmdGetIOSeek(const uint8_t *payload, size_t len);
267
274 void cmdSetIOSeek(const uint8_t *payload, size_t len);
275
283 void cmdGetCfg(const uint8_t *payload, size_t len);
284
292 void cmdSetCfg(const uint8_t *payload, size_t len);
293
301 void cmdGetInfo(const uint8_t *payload, size_t len);
302
307 void cmdListIOs();
308
309#ifdef CONFIG_DAWN_IO_NOTIFY
317 void cmdSubscribe(const uint8_t *payload, size_t len);
318
326 void cmdUnsubscribe(const uint8_t *payload, size_t len);
327#endif
328};
329} // 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