-
Notifications
You must be signed in to change notification settings - Fork 29
/
dispatch.c
495 lines (396 loc) · 11.5 KB
/
dispatch.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
#include "dispatch.h"
#include "util.h"
#include "application.h"
#include "io.h"
#include "stats.h"
#include "i2c.h"
#include "display.h"
#include "sys_time.h"
#include "i2c_sensor.h"
#include "sequencer.h"
#include "init.h"
#include "lwip-interface.h"
#include <stdint.h>
#include <stdbool.h>
typedef enum
{
ts_copy,
ts_dodont,
ts_data,
} telnet_strip_state_t;
_Static_assert(sizeof(telnet_strip_state_t) == 4, "sizeof(telnet_strip_state) != 4");
enum
{
uart_task_id = USER_TASK_PRIO_2,
uart_task_queue_length = 3,
command_task_id = USER_TASK_PRIO_1,
command_task_queue_length = 12,
timer_task_id = USER_TASK_PRIO_0,
timer_task_queue_length = 2,
};
static os_event_t uart_task_queue[uart_task_queue_length];
static os_event_t command_task_queue[command_task_queue_length];
static os_event_t timer_task_queue[timer_task_queue_length];
flash_sector_buffer_use_t flash_sector_buffer_use;
string_new(attr_flash_align, flash_sector_buffer, 4096);
string_new(static attr_flash_align, command_socket_receive_buffer, 4096 + 64);
string_new(static attr_flash_align, command_socket_send_buffer, 4096 + 64);
unsigned int command_left_to_read;
static lwip_if_socket_t command_socket;
string_new(static, uart_socket_receive_buffer, 128);
string_new(static, uart_socket_send_buffer, 128);
static lwip_if_socket_t uart_socket;
bool uart_bridge_active = false;
static os_timer_t fast_timer;
static os_timer_t slow_timer;
typedef struct
{
int io;
int pin;
} trigger_t;
static trigger_t trigger_alert = { -1, -1 };
static trigger_t assoc_alert = { -1, -1 };
iram void dispatch_post_uart(task_command_t command)
{
if(system_os_post(uart_task_id, command, 0))
stat_task_uart_posted++;
else
stat_task_uart_failed++;
}
iram void dispatch_post_command(task_command_t command)
{
if(system_os_post(command_task_id, command, 0))
stat_task_command_posted++;
else
stat_task_command_failed++;
}
iram void dispatch_post_timer(task_command_t command)
{
if(system_os_post(timer_task_id, command, 0))
stat_task_timer_posted++;
else
stat_task_timer_failed++;
}
static void background_task_bridge_uart(void)
{
if(uart_empty(0) || lwip_if_send_buffer_locked(&uart_socket))
return;
string_clear(&uart_socket_send_buffer);
while(!uart_empty(0) && string_space(&uart_socket_send_buffer))
string_append_byte(&uart_socket_send_buffer, uart_receive(0));
if(string_empty(&uart_socket_send_buffer))
return;
if(!lwip_if_send(&uart_socket))
{
stat_uart_send_buffer_overflow++;
log("lwip uart send failed\n");
}
}
static void command_task(struct ETSEventTag *event)
{
switch(event->sig)
{
case(command_task_reset):
{
reset();
break;
}
case(command_task_uart_bridge):
{
background_task_bridge_uart();
stat_update_uart++;
break;
}
case(command_task_init_i2c_sensors):
{
if(i2c_sensors_init())
dispatch_post_command(command_task_init_i2c_sensors);
break;
}
case(command_task_init_displays):
{
uint32_t now = system_get_time();
display_init();
stat_display_init_time_us = system_get_time() - now;
break;
}
case(command_task_received_command):
{
app_action_t action;
if(lwip_if_received_tcp(&command_socket))
stat_update_command_tcp++;
if(lwip_if_received_udp(&command_socket))
stat_update_command_udp++;
if(lwip_if_send_buffer_locked(&command_socket))
{
stat_cmd_send_buffer_overflow++;
string_clear(&command_socket_receive_buffer);
lwip_if_receive_buffer_unlock(&command_socket);
break;
}
string_clear(&command_socket_send_buffer);
action = application_content(&command_socket_receive_buffer, &command_socket_send_buffer);
string_clear(&command_socket_receive_buffer);
lwip_if_receive_buffer_unlock(&command_socket);
if(action == app_action_empty)
{
string_clear(&command_socket_send_buffer);
string_append(&command_socket_send_buffer, "> empty command\n");
}
if(action == app_action_disconnect)
{
string_clear(&command_socket_send_buffer);
string_append(&command_socket_send_buffer, "> disconnect\n");
}
if(action == app_action_reset)
{
string_clear(&command_socket_send_buffer);
string_append(&command_socket_send_buffer, "> reset\n");
}
if(!lwip_if_send(&command_socket))
log("lwip send failed\n");
if(action == app_action_disconnect)
lwip_if_close(&command_socket);
/*
* === ugly workaround ===
*
* For tcp connections we can use the "sent" callback to make sure all
* of our data has been sent before rebooting. For udp there is no such
* callback and waiting for it to happen does not work (need a return to
* SDK code to achieve it). So lwip_if_reboot will take care for it itself
* when possible (tcp), otherwise (udp) it will return false here and the
* application needs to finish the operation via a task call.
*/
if(action == app_action_reset)
if(!lwip_if_reboot(&command_socket))
dispatch_post_command(command_task_reset);
break;
}
case(command_task_display_update):
{
stat_update_display++;
if(display_periodic())
dispatch_post_command(command_task_display_update);
break;
}
case(command_task_fallback_wlan):
{
config_wlan_mode_t wlan_mode;
unsigned int wlan_mode_int;
if(config_get_uint("wlan.mode", &wlan_mode_int, -1, -1))
wlan_mode = (config_wlan_mode_t)wlan_mode_int;
else
wlan_mode = config_wlan_mode_client;
if(wlan_mode == config_wlan_mode_client)
{
wlan_mode_int = config_wlan_mode_ap;
config_open_write();
config_set_uint("wlan.mode", wlan_mode_int, -1, -1);
config_close_write();
config_get_uint("wlan.mode", &wlan_mode_int, -1, -1);
wlan_init();
}
break;
}
case(command_task_update_time):
{
time_periodic();
break;
}
case(command_task_run_sequencer):
{
sequencer_run();
break;
}
case(command_task_alert_association):
{
if((assoc_alert.io >= 0) && (assoc_alert.pin >= 0))
io_trigger_pin((string_t *)0, assoc_alert.io, assoc_alert.pin, io_trigger_on);
break;
}
case(command_task_alert_disassociation):
{
if((assoc_alert.io >= 0) && (assoc_alert.pin >= 0))
io_trigger_pin((string_t *)0, assoc_alert.io, assoc_alert.pin, io_trigger_off);
break;
}
case(command_task_alert_status):
{
if((trigger_alert.io >= 0) && (trigger_alert.pin >= 0))
io_trigger_pin((string_t *)0, trigger_alert.io, trigger_alert.pin, io_trigger_on);
break;
}
}
}
iram static void timer_task(struct ETSEventTag *event)
{
switch(event->sig)
{
case(timer_task_io_periodic_fast):
{
io_periodic_fast();
break;
}
case(timer_task_io_periodic_slow):
{
io_periodic_slow();
break;
}
}
}
iram static void fast_timer_callback(void *arg)
{
// timer runs every 10 ms = 100 Hz
stat_fast_timer++;
dispatch_post_timer(timer_task_io_periodic_fast);
}
iram static void slow_timer_callback(void *arg)
{
// run background task every ~100 ms = ~10 Hz
stat_slow_timer++;
dispatch_post_command(command_task_update_time);
if(uart_bridge_active)
dispatch_post_command(command_task_uart_bridge);
if(display_detected())
dispatch_post_command(command_task_display_update);
// fallback to config-ap-mode when not connected or no ip within 30 seconds
if((stat_slow_timer == 300) && (wifi_station_get_connect_status() != STATION_GOT_IP))
dispatch_post_command(command_task_fallback_wlan);
dispatch_post_timer(timer_task_io_periodic_slow);
}
static void wlan_event_handler(System_Event_t *event)
{
switch(event->event)
{
case(EVENT_STAMODE_GOT_IP):
{
if(!lwip_if_join_mc(239, 255, 255, 254))
log("join mc group failed\n");
time_sntp_start();
fallthrough;
}
case(EVENT_SOFTAPMODE_STACONNECTED):
{
dispatch_post_command(command_task_alert_association);
dispatch_post_command(command_task_init_i2c_sensors);
break;
}
case(EVENT_STAMODE_DISCONNECTED):
{
fallthrough;
}
case(EVENT_SOFTAPMODE_STADISCONNECTED):
{
dispatch_post_command(command_task_alert_disassociation);
break;
}
}
}
static void socket_command_callback_data_received(lwip_if_socket_t *socket, unsigned int length)
{
static const char command_string[] = "flash-send ";
unsigned int chunk_length;
int chunk_offset;
if((command_left_to_read == 0) &&
string_nmatch_cstr(&command_socket_receive_buffer, command_string, sizeof(command_string) - 1) &&
(parse_uint(2, &command_socket_receive_buffer, &chunk_length, 10, ' ') == parse_ok) &&
((chunk_offset = string_sep(&command_socket_receive_buffer, 0, 3, ' ')) >= 0))
command_left_to_read = chunk_offset + chunk_length;
if(command_left_to_read > 0)
{
if(length > command_left_to_read)
command_left_to_read = 0;
else
command_left_to_read -= length;
}
if((command_left_to_read == 0) && (string_trim_nl(&command_socket_receive_buffer) || lwip_if_received_udp(socket)))
dispatch_post_command(command_task_received_command);
else
lwip_if_receive_buffer_unlock(&command_socket);
}
static void socket_uart_callback_data_received(lwip_if_socket_t *socket, unsigned int received)
{
int current, length;
uint8_t byte;
bool strip_telnet;
telnet_strip_state_t telnet_strip_state;
length = string_length(&uart_socket_receive_buffer);
strip_telnet = config_flags_match(flag_strip_telnet);
telnet_strip_state = ts_copy;
for(current = 0; current < length; current++)
{
byte = string_at(&uart_socket_receive_buffer, current);
switch(telnet_strip_state)
{
case(ts_copy):
{
if(strip_telnet && (byte == 0xff))
telnet_strip_state = ts_dodont;
else
{
if(uart_full(0))
stat_uart_receive_buffer_overflow++;
else
uart_send(0, byte);
}
break;
}
case(ts_dodont):
{
telnet_strip_state = ts_data;
break;
}
case(ts_data):
{
telnet_strip_state = ts_copy;
break;
}
}
}
string_clear(&uart_socket_receive_buffer);
lwip_if_receive_buffer_unlock(socket);
uart_flush(0);
}
void dispatch_init1(void)
{
flash_sector_buffer_use = fsb_free;
system_os_task(uart_task, uart_task_id, uart_task_queue, uart_task_queue_length);
system_os_task(command_task, command_task_id, command_task_queue, command_task_queue_length);
system_os_task(timer_task, timer_task_id, timer_task_queue, timer_task_queue_length);
}
void dispatch_init2(void)
{
int io, pin;
unsigned int cmd_port, uart_port;
if(config_get_int("trigger.status.io", &io, -1, -1) &&
config_get_int("trigger.status.pin", &pin, -1, -1))
{
trigger_alert.io = io;
trigger_alert.pin = pin;
}
if(config_get_int("trigger.assoc.io", &io, -1, -1) &&
config_get_int("trigger.assoc.pin", &pin, -1, -1))
{
assoc_alert.io = io;
assoc_alert.pin = pin;
}
if(!config_get_uint("cmd.port", &cmd_port, -1, -1))
cmd_port = 24;
if(!config_get_uint("bridge.port", &uart_port, -1, -1))
uart_port = 0;
wifi_set_event_handler_cb(wlan_event_handler);
command_left_to_read = 0;
lwip_if_socket_create(&command_socket, &command_socket_receive_buffer, &command_socket_send_buffer, cmd_port,
true, config_flags_match(flag_udp_term_empty), socket_command_callback_data_received);
if(uart_port > 0)
{
lwip_if_socket_create(&uart_socket, &uart_socket_receive_buffer, &uart_socket_send_buffer, uart_port,
true, config_flags_match(flag_udp_term_empty), socket_uart_callback_data_received);
uart_bridge_active = true;
}
os_timer_setfn(&slow_timer, slow_timer_callback, (void *)0);
os_timer_arm(&slow_timer, 100, 1); // slow system timer / 10 Hz / 100 ms
os_timer_setfn(&fast_timer, fast_timer_callback, (void *)0);
os_timer_arm(&fast_timer, 10, 1); // fast system timer / 100 Hz / 10 ms
dispatch_post_command(command_task_init_displays);
}