Dawn Framework 1.0
Universal data acquisition framework for embedded systems
nxscope.hxx
1// dawn/include/dawn/proto/nxscope/nxscope.hxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#pragma once
7
8#include "dawn/common/thread.hxx"
9#include "dawn/porting/config.hxx"
10#include "dawn/proto/common.hxx"
11#include "logging/nxscope/nxscope.h"
12#include <cstddef>
13#include <mutex>
14#include <semaphore.h>
15
16// Recv is non blocking and uses usleep.
17// TODO: blocking recv doesn't work for some reason - investigate per
18// transport (UDP/serial). Until resolved, NXSCOPE_RECV_NONBLOCK is
19// required to keep threadRecv() responsive to stop requests.
20
21#define NXSCOPE_RECV_NONBLOCK (1)
22
23// GET_IO response header: objid (u32 LE) + size (u16 LE)
24
25#define NXSCOPE_USER_DATA_HDR (6)
26
27namespace dawn
28{
29// Forward declaration
30
31class IIOCommon;
32class CIOCommon;
33struct io_ddata_t;
34
43{
44public:
45 struct
46 {
48 } typedef SProtoNxscopeIOBind;
49
50 struct
51 {
52 const char name[12];
53 } typedef SProtoNxscopeNames;
54
55 struct
56 {
57 SProtoNxscopeIOBind bind;
58 SProtoNxscopeNames name;
59 } typedef SProtoNxscopeIOBind2;
60
61 explicit CProtoNxscope(CDescObject &desc)
62 : CProtoCommon(desc)
63 , nxsIntf()
64 , threadRecvMember()
65#ifdef CONFIG_DAWN_PROTO_NXSCOPE_SAMPLE_THREAD
66 , threadSampleMember()
67#endif
68 , nxs()
69 , nxsCfg()
70 , nxsProto()
71 , nxsCbs()
72 , userTxBuf(nullptr)
73 , userTxLen(0)
74 {
75#ifdef CONFIG_DAWN_IO_NOTIFY
76 sem_init(&streamSem, 0, 0);
77#endif
78 }
79
80 ~CProtoNxscope() override;
81
82 int configure() override;
83 int init() override;
84 int deinit() override;
85 int doStart() override;
86 int doStop() override;
87 bool hasThread() const override;
88
89protected:
90 struct nxscope_intf_s nxsIntf;
91
92 int allocObject(SProtoNxscopeIOBind *alloc);
93 int allocNames(size_t j, SProtoNxscopeNames *names);
94 int configureNxscope();
95
96 virtual int initPriv() = 0;
97 virtual int deinitPriv() = 0;
98 virtual int startPriv() = 0;
99 virtual int stopPriv() = 0;
100
101private:
102#ifdef CONFIG_DAWN_IO_NOTIFY
103 typedef int (*nxscope_put_cb_t)(struct nxscope_s *nxs,
104 uint8_t chan,
105 io_ddata_t *data,
106 uint8_t dim);
107#endif
108
109#ifdef CONFIG_DAWN_PROTO_NXSCOPE_SAMPLE_THREAD
110 struct proto_nxscope_iochan_s;
111
112 typedef int (*nxscope_sample_cb_t)(CProtoNxscope *obj, struct proto_nxscope_iochan_s *iochan);
113#endif
114
115 struct proto_nxscope_iochan_s
116 {
117 uint8_t chan;
118 uint8_t dim;
119 bool stream;
120 CIOCommon *io;
121 CProtoNxscope *obj;
122 io_ddata_t *setData;
123 io_ddata_t *getData;
124
125#ifdef CONFIG_DAWN_IO_NOTIFY
126 nxscope_put_cb_t put;
127#endif
128
129#ifdef CONFIG_DAWN_PROTO_NXSCOPE_SAMPLE_THREAD
130 nxscope_sample_cb_t sample;
131 io_ddata_t *data;
132#endif
133 } typedef SProtoNxscopeIochan;
134
135 enum
136 {
137 NXSCOPE_USER_SET_IO = NXSCOPE_HDRID_USER,
138 NXSCOPE_USER_SET_IO_SEEK = NXSCOPE_HDRID_USER + 1,
139 NXSCOPE_USER_GET_IO = NXSCOPE_HDRID_USER + 2,
140 NXSCOPE_USER_GET_IO_SEEK = NXSCOPE_HDRID_USER + 3
141 };
142
143 CThreadedObject threadRecvMember;
144
145#ifdef CONFIG_DAWN_PROTO_NXSCOPE_SAMPLE_THREAD
146 CThreadedObject threadSampleMember;
147#endif
148
149 struct nxscope_s nxs;
150 struct nxscope_cfg_s nxsCfg;
151 struct nxscope_proto_s nxsProto;
152 struct nxscope_callbacks_s nxsCbs;
153
158 std::recursive_mutex streamLock;
159
160#ifdef CONFIG_DAWN_IO_NOTIFY
165 sem_t streamSem;
166#endif
167
168 uint8_t *userTxBuf;
169 size_t userTxLen;
170
171 std::vector<const SProtoNxscopeIOBind *> vchannels;
172 std::vector<const SProtoNxscopeNames *> vnames;
173 std::vector<SProtoNxscopeIochan> vio;
174 bool hasStreamChannels = false;
175
176 SProtoNxscopeIochan *findIochan(SObjectId::ObjectId objid);
177
178 int handleUserCommand(uint8_t id, uint8_t *buff);
179 int userSetIO(uint8_t *buff);
180 int userSetIOSeek(uint8_t *buff);
181 int userGetIO(uint8_t *buff);
182 int userGetIOSeek(uint8_t *buff);
183 int sendAck(int ret);
184 int sendUserData(SObjectId::ObjectId objid, const void *data, uint16_t size);
185
186 static int userIdCb(void *priv, uint8_t id, uint8_t *buff);
187
188#ifdef CONFIG_DAWN_IO_NOTIFY
189 static int ioNotifierCb(void *priv, io_ddata_t *data);
190#endif
191
192 uint8_t getChannelDim(const CIOCommon &io);
193 uint8_t getChannelDtype(const CIOCommon &io);
194 int nxscopeChannelsCreate();
195 int bindChannelCallbacks(SProtoNxscopeIochan &iochan, uint8_t dtype);
196
198 template<typename T>
199 using nxscope_put_t = int (*)(struct nxscope_s *, uint8_t, T *, uint8_t);
200
201 template<typename T, nxscope_put_t<T> Put>
202 void bindTyped(SProtoNxscopeIochan &iochan);
203
204#ifdef CONFIG_DAWN_IO_NOTIFY
205 template<typename T, nxscope_put_t<T> Put>
206 static int putBatch(struct nxscope_s *nxs, uint8_t chan, io_ddata_t *data, uint8_t dim);
207#endif
208
209#ifdef CONFIG_DAWN_PROTO_NXSCOPE_SAMPLE_THREAD
210 void getAndPut(SProtoNxscopeIochan *iochan);
211
212 template<typename T, nxscope_put_t<T> Put>
213 static int sampleTyped(CProtoNxscope *obj, SProtoNxscopeIochan *iochan);
214#endif
215
216 void threadRecv();
217
218#ifdef CONFIG_DAWN_PROTO_NXSCOPE_SAMPLE_THREAD
219 void threadSample();
220#endif
221};
222} // 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
Real-time data visualization protocol (base class).
Definition nxscope.hxx:43
int doStop()
Stop implementation hook.
Definition nxscope.cxx:1201
bool hasThread() const
Check if a background thread is active.
Definition nxscope.cxx:1223
struct nxscope_intf_s nxsIntf
NXScope interface structure (from logging library).
Definition nxscope.hxx:90
int configure()
Configure object from descriptor data.
Definition nxscope.cxx:1117
int doStart()
Start implementation hook.
Definition nxscope.cxx:1149
int init()
One-time initialize object after bindings are resolved.
Definition nxscope.cxx:1130
int deinit()
De-initialize object.
Definition nxscope.cxx:1136
Out-of-tree user-extension hooks for Dawn.
Definition bindable.hxx:13
uint32_t ObjectId
ObjectID type - single 32-bit value.
Definition objectid.hxx:44
Heap-allocated dynamic I/O data buffer.
Definition ddata.hxx:21
32-bit encoded object identifier (union with bit field).
Definition objectid.hxx:218