/
orchard-app.c
497 lines (381 loc) · 12 KB
/
orchard-app.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "ch.h"
#include "hal.h"
#include "orchard-app.h"
#include "orchard-events.h"
#include "orchard-ui.h"
#include "nrf52i2s_lld.h"
#include "joypad_lld.h"
extern OrchardAppEvent joyEvent;
orchard_app_start();
orchard_app_end();
bool (*app_radio_notify)(void *);
const OrchardApp *orchard_app_list;
/* the one and in fact only instance of any orchard app */
orchard_app_instance instance;
/* graphics event handle */
static OrchardAppEvent ugfx_evt;
static thread_reference_t ugfxThreadReference;
static int ugfxPend;
/* orchard event sources */
event_source_t unlocks_updated;
event_source_t orchard_app_terminated;
event_source_t orchard_app_terminate;
event_source_t timer_expired;
event_source_t ui_completed;
event_source_t orchard_app_gfx;
event_source_t orchard_app_radio;
event_source_t orchard_app_key;
static uint8_t ui_override = 0;
#define RADIO_QUEUE_LEN 64
static uint8_t prod_idx;
static uint8_t cons_idx;
static uint8_t queue_cnt;
static OrchardAppRadioEvent * radio_evt[RADIO_QUEUE_LEN];
static uint8_t * radio_pkt[RADIO_QUEUE_LEN];
void orchardAppUgfxCallback (void * arg, GEvent * pe)
{
GListener * gl;
/*
* If there's currently an event being processed, wait
* for the app to finish servicing it. If we don't we might
* overwrite it with another event, which would mean we'd
* miss one.
*/
osalSysLock ();
if (ugfxPend)
osalThreadSuspendS (&ugfxThreadReference);
else
ugfxPend = 1;
osalSysUnlock ();
gl = (GListener *)arg;
ugfx_evt.type = ugfxEvent;
ugfx_evt.ugfx.pListener = gl;
ugfx_evt.ugfx.pEvent = pe;
chEvtBroadcast (&orchard_app_gfx);
return;
}
static void ugfx_event(eventid_t id) {
(void) id;
instance.app->event (instance.context, &ugfx_evt);
geventEventComplete (ugfx_evt.ugfx.pListener);
osalSysLock ();
ugfxPend = 0;
osalThreadResumeS (&ugfxThreadReference, MSG_OK);
osalSysUnlock ();
return;
}
static void flush_radio_queue (void) {
int i;
for (i = 0; i < RADIO_QUEUE_LEN; i++) {
if (radio_evt[i] != NULL)
free (radio_evt[i]);
radio_evt[i] = NULL;
if (radio_pkt[i] != NULL)
free (radio_pkt[i]);
radio_pkt[i] = NULL;
}
prod_idx = 0;
cons_idx = 0;
queue_cnt = 0;
return;
}
void orchardAppRadioCallback (OrchardAppRadioEventType type,
ble_evt_t * evt, void * pkt, uint16_t len) {
OrchardAppRadioEvent * r_evt;
if (instance.context == NULL)
return;
/*
* We buffer the current frame. If another frame arrives before
* the app has processed it, then we drop the new frame. We could
* expand this to permit buffering of more frames, but for now
* this seems sufficient.
*/
if (queue_cnt < RADIO_QUEUE_LEN) {
r_evt = malloc (sizeof(OrchardAppRadioEvent));
memset (r_evt, 0, sizeof(OrchardAppRadioEvent));
radio_evt[prod_idx] = r_evt;
r_evt->type = type;
if (pkt != NULL && len != 0)
{
radio_pkt[prod_idx] = malloc (len);
r_evt->pkt = radio_pkt[prod_idx];
memcpy (r_evt->pkt, pkt, len);
r_evt->pktlen = len;
}
if (evt != NULL)
memcpy (&r_evt->evt, evt, sizeof(ble_evt_t));
queue_cnt++;
prod_idx++;
if (prod_idx == RADIO_QUEUE_LEN)
prod_idx = 0;
} else
return;
chEvtBroadcast (&orchard_app_radio);
return;
}
static void radio_event(eventid_t id) {
OrchardAppEvent evt;
OrchardAppRadioEvent * r_evt;
bool r;
(void) id;
if (instance.context != NULL) {
evt.type = radioEvent;
while (queue_cnt) {
r_evt = radio_evt[cons_idx];
/*
* When someone writes to one of our characteristics, it could
* be a notification of some kind (e.g. chat request). If no
* other app is running besides the launcher, run the notify
* app to announce what happened.
*/
r = FALSE;
if ((strcmp (instance.app->name, "Badge") == 0 ||
strcmp (instance.app->name, "Launcher") == 0) &&
(r_evt->type == gattsReadWriteAuthEvent ||
r_evt->type == gattsWriteEvent))
r = app_radio_notify (r_evt);
if (r == FALSE) {
memcpy (&evt.radio, r_evt, sizeof(OrchardAppRadioEvent));
instance.app->event (instance.context, &evt);
}
free (radio_evt[cons_idx]);
if (radio_pkt[cons_idx] != NULL)
free (radio_pkt[cons_idx]);
radio_evt[cons_idx] = NULL;
radio_pkt[cons_idx] = NULL;
queue_cnt--;
cons_idx++;
if (cons_idx == RADIO_QUEUE_LEN)
cons_idx = 0;
}
}
return;
}
static void key_event(eventid_t id) {
(void) id;
if (joyEvent.key.code == keyPuz) {
orchardAppRun (orchardAppByName ("Puzzle Watch"));
return;
}
if (instance.context != NULL) {
// if (strcmp (instance.app->name, "Launcher") == 0 &&
// joyEvent.key.flags == keyPress)
// i2sPlay ("sound/ping.snd");
instance.app->event(instance.context, &joyEvent);
}
return;
}
static void ui_complete_cleanup(eventid_t id) {
(void)id;
OrchardAppEvent evt;
evt.type = uiEvent;
evt.ui.code = uiComplete;
evt.ui.flags = uiOK;
instance.app->event(instance.context, &evt);
return;
}
static void terminate(eventid_t id) {
OrchardAppEvent evt;
(void)id;
if (!instance.app->event)
return;
evt.type = appEvent;
evt.app.event = appTerminate;
instance.app->event(instance.context, &evt);
flush_radio_queue ();
/* Clear any pending uGFX event references */
osalSysLock ();
ugfxPend = 0;
osalThreadResumeS (&ugfxThreadReference, MSG_OK);
osalSysUnlock ();
chThdTerminate(instance.thr);
}
static void timer_event(eventid_t id) {
(void)id;
OrchardAppEvent evt;
if (!instance.app->event)
return;
evt.type = timerEvent;
evt.timer.usecs = instance.timer_usecs;
if( !ui_override )
{
instance.app->event(instance.context, &evt);
}
if (instance.timer_repeating)
orchardAppTimer(instance.context, instance.timer_usecs, true);
}
static void timer_do_send_message(void *arg) {
(void)arg;
chSysLockFromISR();
chEvtBroadcastI(&timer_expired);
chSysUnlockFromISR();
}
const OrchardApp *orchardAppByName(const char *name) {
const OrchardApp *current;
current = orchard_apps();
while(current->name) {
if( !strncmp(name, current->name, 16) ) {
return current;
}
current++;
}
return NULL;
}
void orchardAppRun(const OrchardApp *app) {
instance.next_app = app;
chThdTerminate(instance.thr);
chEvtBroadcast(&orchard_app_terminate);
}
void orchardAppExit(void) {
instance.next_app = orchard_app_list; // the first app is the launcher
/*
* The call to terminate the app thread below seems to be wrong. The
* terminate() handler will be invoked by broadcasting the app
* terminate event, and it already calls chThdTerminate() once it sends
* the app terminate event to the app's event handler, and that can't
* happen if the thread has already exited from here.
*/
/*chThdTerminate(instance.thr);*/
chEvtBroadcast(&orchard_app_terminate);
}
void orchardAppTimer(const OrchardAppContext *context,
uint32_t usecs,
bool repeating) {
if (!usecs) {
chVTReset(&context->instance->timer);
context->instance->timer_usecs = 0;
return;
}
context->instance->timer_usecs = usecs;
context->instance->timer_repeating = repeating;
/*
* The TIME_US2I() macro used by ChibiOS does not work with
* large microsecond counts when the system tick resolution is
* fairly high. With a resolutio of 10000, the TIME_US2I() macro
* macro wraps at 429496. So for values above this, we need to
* base the calculation on milliseconds instead.
*/
if (usecs > 429496)
chVTSet(&context->instance->timer, TIME_MS2I(usecs / 1000),
timer_do_send_message, NULL);
else
chVTSet(&context->instance->timer, TIME_US2I(usecs),
timer_do_send_message, NULL);
return;
}
static THD_WORKING_AREA(waOrchardAppThread, 2048); // was 1280
static THD_FUNCTION(orchard_app_thread, arg) {
struct orchard_app_instance *instance = arg;
struct evt_table orchard_app_events;
OrchardAppEvent evt;
OrchardAppContext app_context;
(void)arg;
ui_override = 0;
memset(&app_context, 0, sizeof(app_context));
instance->context = &app_context;
app_context.instance = instance;
// set UI elements to null
instance->ui = NULL;
instance->uicontext = NULL;
instance->ui_result = 0;
evtTableInit(orchard_app_events, 6);
evtTableHook(orchard_app_events, ui_completed, ui_complete_cleanup);
evtTableHook(orchard_app_events, orchard_app_terminate, terminate);
evtTableHook(orchard_app_events, orchard_app_gfx, ugfx_event);
evtTableHook(orchard_app_events, orchard_app_radio, radio_event);
evtTableHook(orchard_app_events, orchard_app_key, key_event);
evtTableHook(orchard_app_events, timer_expired, timer_event);
// if APP is null, the system will crash here.
if (instance->app->init)
app_context.priv_size = instance->app->init(&app_context);
else
app_context.priv_size = 0;
chRegSetThreadName(instance->app->name);
/* Allocate private data on the stack (word-aligned) */
uint32_t priv_data[app_context.priv_size / 4];
if (app_context.priv_size) {
memset(priv_data, 0, sizeof(priv_data));
app_context.priv = priv_data;
} else {
app_context.priv = NULL;
}
/*
* Initialize the radio event queue.
* Note: Any events left unserviced by previous app
* are discarded.
*/
flush_radio_queue ();
if (instance->app->start)
instance->app->start(&app_context);
if (instance->app->event) {
{
evt.type = appEvent;
evt.app.event = appStart;
instance->app->event(instance->context, &evt);
}
while (!chThdShouldTerminateX())
chEvtDispatch(evtHandlers(orchard_app_events), chEvtWaitOne(ALL_EVENTS));
}
/* Clear any pending uGFX event references */
osalSysLock ();
ugfxPend = 0;
osalThreadResumeS (&ugfxThreadReference, MSG_OK);
osalSysUnlock ();
chVTReset(&instance->timer);
if (instance->app->exit)
instance->app->exit(&app_context);
instance->context = NULL;
/* Set up the next app to run when the orchard_app_terminated message is
acted upon.*/
if (instance->next_app)
instance->app = instance->next_app;
else
instance->app = orchard_app_list;
instance->next_app = NULL;
evtTableUnhook(orchard_app_events, timer_expired, timer_event);
evtTableUnhook(orchard_app_events, orchard_app_terminate, terminate);
evtTableUnhook(orchard_app_events, ui_completed, ui_complete_cleanup);
evtTableUnhook(orchard_app_events, orchard_app_gfx, ugfx_event);
evtTableUnhook(orchard_app_events, orchard_app_radio, radio_event);
evtTableUnhook(orchard_app_events, orchard_app_key, key_event);
flush_radio_queue ();
/* Atomically broadcasting the event source and terminating the thread,
there is not a chSysUnlock() because the thread terminates upon return.*/
chSysLock();
chEvtBroadcastI(&orchard_app_terminated);
chThdExitS(MSG_OK);
}
void orchardAppInit(void) {
const OrchardApp * current;
orchard_app_list = orchard_apps();
instance.app = orchard_app_list;
chEvtObjectInit(&orchard_app_terminate);
chEvtObjectInit(&timer_expired);
chEvtObjectInit(&ui_completed);
chEvtObjectInit(&orchard_app_gfx);
chEvtObjectInit(&orchard_app_radio);
chEvtObjectInit(&orchard_app_key);
chVTReset(&instance.timer);
current = orchard_app_list;
while (current->name) {
if (current->flags & APP_FLAG_AUTOINIT)
current->init(NULL);
current++;
}
}
void orchardAppRestart(void) {
/* Recovers memory of the previous application. */
if (instance.thr) {
osalDbgAssert(chThdTerminatedX(instance.thr), "App thread still running");
chThdRelease(instance.thr);
instance.thr = NULL;
}
instance.thr = chThdCreateStatic(waOrchardAppThread,
sizeof(waOrchardAppThread),
ORCHARD_APP_PRIO,
orchard_app_thread,
(void *)&instance);
}