Dawn Framework 1.0
Universal data acquisition framework for embedded systems
dawn.cxx
1// dawn/src/dawn.cxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#include "dawn/dawn.hxx"
7#include "dawn/oot.hxx"
8#ifdef CONFIG_DAWN_DESC_SWITCH
9# include "dawn/dev/descriptor.hxx"
10# include "dawn/dev/descswitch.hxx"
11#endif
12#ifdef CONFIG_DAWN_LIFECYCLE_TEARDOWN
13# include "dawn/dev/shutdown.hxx"
14#endif
15
16using namespace dawn;
17
18CDawn::CDawn(IIOFactory *iofactory, IProgFactory *progfactory, IProtoFactory *protofactory)
19 : userIOFactory(iofactory)
20 , userProgFactory(progfactory)
21 , userProtoFactory(protofactory)
22{
23}
24
25int CDawn::load_descriptor(uint32_t *bin, size_t len)
26{
27 int ret = OK;
28
29 DAWNASSERT(bin != nullptr && len > 0, "invalid input");
30
31 // Load binary data
32
33 ret = desc.loadBin(bin, len, false);
34 if (ret < 0)
35 {
36 DAWNERR("ERROR: _desc.loadBin failed %d\n", ret);
37 return ret;
38 }
39
40 // Initialize IO handler
41
42 ret = io.init(desc, userIOFactory);
43 if (ret < 0)
44 {
45 DAWNERR("failed to initialize IO handler\n");
46 return ret;
47 }
48
49 // Wire descriptor registry into IO handler before initAll
50
51 io.setDevDescriptor(&devDesc);
52 if (ret < 0)
53 {
54 DAWNERR("failed to initialize IO handler\n");
55 return ret;
56 }
57
58 // Initialize IOs
59
60 ret = io.initAll();
61 if (ret < 0)
62 {
63 DAWNERR("failed to initialize IOs\n");
64 return ret;
65 }
66
67 // Initialize PROG handler
68
69 ret = prog.init(desc, &io, userProgFactory);
70 if (ret < 0)
71 {
72 DAWNERR("failed to initialize PROG handler\n");
73 return ret;
74 }
75
76 // Initialize PROGs
77
78 ret = prog.initAll();
79 if (ret < 0)
80 {
81 DAWNERR("failed to initialize PROGs\n");
82 return ret;
83 }
84
85 // Initialize PROTO handler
86
87 ret = proto.init(desc, &io, userProtoFactory);
88 if (ret < 0)
89 {
90 DAWNERR("failed to initialize PROTO handler\n");
91 return ret;
92 }
93
94 // Initialize PROTOs
95
96 ret = proto.initAll();
97 if (ret < 0)
98 {
99 DAWNERR("failed to initialize PROTOs\n");
100 return ret;
101 }
102
103 // Initialize SYSTEM handler
104
105 ret = system.init(desc, nullptr);
106 if (ret < 0)
107 {
108 DAWNERR("failed to initialize SYSTEM handler\n");
109 return ret;
110 }
111
112 // Initialize SYSTEM objects
113
114 ret = system.initAll();
115 if (ret < 0)
116 {
117 DAWNERR("failed to initialize SYSTEM objects\n");
118 return ret;
119 }
120
121 // Bind special IO objects (Config, Control, Trigger) to their targets
122
123 ret = io.bindObjects(io, prog, proto, system);
124 if (ret < 0)
125 {
126 DAWNERR("failed to bind special IO objects\n");
127 return ret;
128 }
129
130 return OK;
131}
132
133#ifdef CONFIG_DAWN_DESC_SWITCH
134int CDawn::load_descriptor_from_slot(uint8_t slot)
135{
137 int ret;
138
139 ret = devDesc.getDescriptor(slot, reg);
140 if (ret < 0)
141 {
142 return ret;
143 }
144
145 if (reg.ptr == nullptr || reg.len == 0)
146 {
147 return -ENODATA;
148 }
149
150 return load_descriptor(static_cast<uint32_t *>(reg.ptr), reg.len);
151}
152#endif
153
154int CDawn::start(bool no_loop)
155{
156 int ret = OK;
157#ifdef CONFIG_DAWN_LIFECYCLE_TEARDOWN
158 int tmp;
159#endif
160#ifdef CONFIG_DAWN_DESC_SWITCH
161 uint8_t newSlot;
162
165#endif
166
167 for (;;)
168 {
169#ifdef CONFIG_DAWN_LIFECYCLE_TEARDOWN
171#endif
172 // Start IO
173
174 ret = io.startAll();
175 if (ret < 0)
176 {
177#ifdef CONFIG_DAWN_LIFECYCLE_TEARDOWN
178 goto deinit_all;
179#else
180 return ret;
181#endif
182 }
183
184 // Start SYSTEM objects (e.g. bring up LTE before protocols need the network)
185
186 ret = system.startAll();
187 if (ret < 0)
188 {
189#ifdef CONFIG_DAWN_LIFECYCLE_TEARDOWN
190 goto stop_io;
191#else
192 return ret;
193#endif
194 }
195
196 // Start programs
197
198 ret = prog.startAll();
199 if (ret < 0)
200 {
201#ifdef CONFIG_DAWN_LIFECYCLE_TEARDOWN
202 goto stop_sys;
203#else
204 return ret;
205#endif
206 }
207
208 // Start protocols
209
210 ret = proto.startAll();
211 if (ret < 0)
212 {
213#ifdef CONFIG_DAWN_LIFECYCLE_TEARDOWN
214 goto stop_prog;
215#else
216 return ret;
217#endif
218 }
219
220 DAWNINFO("DAWN: START SUCCESS!\n");
221
222 while (!no_loop)
223 {
224#ifdef CONFIG_DAWN_LIFECYCLE_TEARDOWN
226 {
227 DAWNINFO("shutdown requested - quit\n");
228 break;
229 }
230#endif
231
232#ifdef CONFIG_DAWN_DESC_SWITCH
234 {
235 DAWNINFO("descriptor switch requested\n");
236 break;
237 }
238#endif
239
240 if (!desc.getNoIdleQuit() && !(proto.hasThread() || prog.hasThread()))
241 {
242 DAWNINFO("no active protocols - quit\n");
243 break;
244 }
245
246 // Idle: protocols and progs run their own threads; the main loop
247 // hands the wait to the user idle hook (default is sleep(1)).
248 // The hook owns cadence and must return promptly so the
249 // shutdown / descriptor-switch / no-thread checks above run
250 // each iteration.
251
252 dawn::oot::user_on_idle(this);
253 }
254
255#ifdef CONFIG_DAWN_LIFECYCLE_TEARDOWN
256 // User-supplied pre-shutdown hook (weak default is a no-op).
257 // A negative return is logged but does not abort teardown.
258
259 tmp = dawn::oot::user_pre_shutdown(this);
260 if (tmp < 0)
261 {
262 DAWNWARN("user_pre_shutdown returned %d\n", tmp);
263 }
264
265 tmp = proto.stopAll();
266 if (tmp < 0)
267 {
268 DAWNERR("failed to stop protos %d\n", tmp);
269 if (ret == OK)
270 {
271 ret = tmp;
272 }
273 }
274
275 stop_prog:
276 tmp = prog.stopAll();
277 if (tmp < 0)
278 {
279 DAWNERR("failed to stop programs %d\n", tmp);
280 if (ret == OK)
281 {
282 ret = tmp;
283 }
284 }
285
286 stop_sys:
287 tmp = system.stopAll();
288 if (tmp < 0)
289 {
290 DAWNERR("failed to stop SYSTEM objects %d\n", tmp);
291 if (ret == OK)
292 {
293 ret = tmp;
294 }
295 }
296
297 stop_io:
298 tmp = io.stopAll();
299 if (tmp < 0)
300 {
301 DAWNERR("failed to stop IO %d\n", tmp);
302 if (ret == OK)
303 {
304 ret = tmp;
305 }
306 }
307
308 deinit_all:
309 tmp = proto.deinitAll();
310 if (tmp < 0)
311 {
312 DAWNERR("failed to deinit protos %d\n", tmp);
313 if (ret == OK)
314 {
315 ret = tmp;
316 }
317 }
318
319 tmp = prog.deinitAll();
320 if (tmp < 0)
321 {
322 DAWNERR("failed to deinit programs %d\n", tmp);
323 if (ret == OK)
324 {
325 ret = tmp;
326 }
327 }
328
329 tmp = system.deinitAll();
330 if (tmp < 0)
331 {
332 DAWNERR("failed to deinit SYSTEM objects %d\n", tmp);
333 if (ret == OK)
334 {
335 ret = tmp;
336 }
337 }
338
339 tmp = io.deinitAll();
340 if (tmp < 0)
341 {
342 DAWNERR("failed to deinit IO %d\n", tmp);
343 if (ret == OK)
344 {
345 ret = tmp;
346 }
347 }
348#else
349 return ret;
350#endif
351
352#ifdef CONFIG_DAWN_DESC_SWITCH
354 {
355 newSlot = CDescSwitch::getSwitchSlot();
358
359 ret = load_descriptor_from_slot(newSlot);
360 if (ret < 0)
361 {
362 DAWNERR("descriptor switch failed for slot %d\n", newSlot);
363 return ret;
364 }
365
366 continue;
367 }
368#endif
369
370 return ret;
371 }
372}
int load_descriptor(uint32_t *bin, size_t len)
Load and initialize descriptor.
Definition dawn.cxx:25
int start(bool no_loop=false)
Start Dawn framework.
Definition dawn.cxx:154
CDawn(IIOFactory *iofactory=nullptr, IProgFactory *progfactory=nullptr, IProtoFactory *protofactory=nullptr)
Construct CDawn framework.
Definition dawn.cxx:18
static void setActiveSlot(uint8_t slot)
Set currently active descriptor slot.
static void clear()
Clear pending switch request.
static uint8_t getSwitchSlot()
Get pending switch target slot.
static bool isSwitchRequested()
Check if descriptor switch is pending.
int getDescriptor(int inst, SDescriptorReg &reg)
Get registered descriptor data for an instance.
static void clear()
Clear pending shutdown request.
Definition shutdown.cxx:22
static bool isRequested()
Check if shutdown has been requested.
Definition shutdown.cxx:27
Abstract factory interface for extensible I/O object creation.
Definition factory.hxx:20
Abstract factory interface for PROG object creation.
Definition factory.hxx:20
Abstract factory interface for protocol creation.
Definition factory.hxx:17
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.