-
Notifications
You must be signed in to change notification settings - Fork 17
/
protocol.c
706 lines (589 loc) · 29.1 KB
/
protocol.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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
/*
* This file is part of the hoverboard-firmware-hack project.
*
* Copyright (C) 2018 Simon Hailes <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "protocol_private.h"
#include <string.h>
#include <stdlib.h>
///////////////////////////////////////////////////////
// Function Pointers to system functions
//////////////////////////////////////////////////////////
// Need to be assigned to functions "real" system fucntions
uint32_t noTick(void) { return 0; };
uint32_t (*protocol_GetTick)() = noTick;
void noDelay(uint32_t Delay) {};
void (*protocol_Delay)(uint32_t Delay) = noDelay;
void noReset(void) {};
void (*protocol_SystemReset)() = noReset;
//////////////////////////////////////////////
// variables you want to read/write here. Only common used ones, specific ones below.
// Default temporary storage for received values
unsigned char contentbuf[sizeof( ((PROTOCOL_MSG3full *)0)->content )];
void protocol_process_ReadValue(PROTOCOL_STAT *s, PROTOCOL_MSG3full *msg) {
if(msg) {
unsigned char *src = s->params[msg->code]->ptr;
for (int j = 0; j < s->params[msg->code]->len; j++){
msg->content[j] = *(src++);
}
}
}
void protocol_process_ReadAndSendValue(PROTOCOL_STAT *s, PROTOCOL_MSG3full *msg) {
if(msg) {
protocol_process_ReadValue(s, msg);
PROTOCOL_MSG3full newMsg;
memcpy(&newMsg, msg, sizeof(PROTOCOL_MSG3full));
newMsg.lenPayload = s->params[msg->code]->len; // command + code + data len only
newMsg.cmd = PROTOCOL_CMD_READVALRESPONSE; // mark as response
// send back with 'read' command plus data like write.
protocol_post(s, &newMsg);
}
}
void protocol_process_WriteValue(PROTOCOL_STAT *s, PROTOCOL_MSG3full *msg) {
if(msg) {
unsigned char *dest = s->params[msg->code]->ptr;
// ONLY copy what we have, else we're stuffing random data in.
// e.g. is setting posn, structure is 8 x 4 bytes,
// but we often only want to set the first 8
for (int j = 0; ((j < s->params[msg->code]->len) && (j < (msg->lenPayload))); j++){
*(dest++) = msg->content[j];
}
}
}
void protocol_process_cmdWritevalAndRespond(PROTOCOL_STAT *s, PROTOCOL_MSG3full *msg) {
if(msg) {
protocol_process_WriteValue(s, msg);
PROTOCOL_MSG3full newMsg;
memcpy(&newMsg, msg, sizeof(PROTOCOL_MSG3full));
newMsg.lenPayload = 1; // 1 only
newMsg.cmd = PROTOCOL_CMD_WRITEVALRESPONSE; // mark as response
newMsg.content[0] = 1; // say we wrote it
// send back with 'write' command with no data.
protocol_post(s, &newMsg);
}
}
void fn_defaultProcessing ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROTOCOL_MSG3full *msg ) {
switch (cmd) {
case PROTOCOL_CMD_READVAL:
protocol_process_ReadAndSendValue(s, msg);
break;
case PROTOCOL_CMD_SILENTREAD:
protocol_process_ReadValue(s, msg);
break;
case PROTOCOL_CMD_READVALRESPONSE:
protocol_process_WriteValue(s, msg);
break;
case PROTOCOL_CMD_WRITEVALRESPONSE:
// Should never get here..
break;
case PROTOCOL_CMD_WRITEVAL:
protocol_process_cmdWritevalAndRespond(s, msg);
break;
}
}
////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
// Default function, wipes receive memory before writing (and readresponse is just a differenct type of writing)
void fn_defaultProcessingPreWriteClear ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROTOCOL_MSG3full *msg ) {
switch (cmd) {
case PROTOCOL_CMD_WRITEVAL:
case PROTOCOL_CMD_READVALRESPONSE:
// ensure clear in case of short write
memset(param->ptr, 0, param->len);
break;
}
fn_defaultProcessing(s, param, cmd, msg);
}
////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
// Default function, wipes receive memory before writing (and readresponse is just a differenct type of writing)
void fn_defaultProcessingReadOnly ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROTOCOL_MSG3full *msg ) {
switch (cmd) {
case PROTOCOL_CMD_READVAL:
case PROTOCOL_CMD_SILENTREAD:
fn_defaultProcessing(s, param, cmd, msg);
break;
case PROTOCOL_CMD_READVALRESPONSE:
case PROTOCOL_CMD_WRITEVALRESPONSE:
case PROTOCOL_CMD_WRITEVAL:
// Do nothing
break;
}
}
////////////////////////////////////////////////////////////////////////////////////////////
// Variable & Functions for 0xFE version
static uint32_t version = 3;
////////////////////////////////////////////////////////////////////////////////////////////
// Variable & Functions for 0x27 Ping
// Sends back the received Message as a readresponse. (Sender can send a timestamp to measure latency)
void fn_ping ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROTOCOL_MSG3full *msg ) {
switch (cmd) {
case PROTOCOL_CMD_WRITEVAL:
{
if(msg) {
msg->cmd = PROTOCOL_CMD_READVALRESPONSE;
protocol_post(s, msg);
}
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////
// Variable & Functions for 0x22 SubscribeData
void fn_SubscribeData ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROTOCOL_MSG3full *msg ) {
fn_defaultProcessingPreWriteClear(s, param, cmd, msg); // Wipes memory before write (and readresponse is just a differenct type of writing)
switch (cmd) {
case PROTOCOL_CMD_WRITEVAL:
case PROTOCOL_CMD_READVALRESPONSE:
{
if(msg) {
// Check if length of received data is plausible.
if(msg->lenPayload != sizeof(PROTOCOL_SUBSCRIBEDATA)) {
break;
}
int subscriptions_len = sizeof(s->subscriptions)/sizeof(s->subscriptions[0]);
int index = 0;
// Check if subscription already exists for this code
for (index = 0; index < subscriptions_len; index++) {
if(s->subscriptions[index].code == ((PROTOCOL_SUBSCRIBEDATA*) msg->content)->code) {
break;
}
}
// If code was not found, look for vacant subscription slot
if(index == subscriptions_len) {
for (index = 0; index < subscriptions_len; index++) {
// NOTE: if you set a count of 0, or the count runs out, then
// the subscription will be overwritten later -
// i.e. you effectively delete it....
if( s->subscriptions[index].code == 0 || s->subscriptions[index].count == 0 ) {
break;
}
}
}
// Fill in new subscription when possible; Plausibility check for period
if(index < subscriptions_len && ((PROTOCOL_SUBSCRIBEDATA*) msg->content)->period >= 10) {
s->subscriptions[index] = *((PROTOCOL_SUBSCRIBEDATA*) msg->content);
//char tmp[100];
//sprintf(tmp, "subscription added at %d for 0x%x, period %d, count %d, som %d\n", index, ((SUBSCRIBEDATA*) (param->ptr))->code, ((SUBSCRIBEDATA*) (param->ptr))->period, ((SUBSCRIBEDATA*) (param->ptr))->count, ((SUBSCRIBEDATA*) (param->ptr))->som);
//consoleLog(tmp);
} else {
// TODO. Inform sender??
// consoleLog("no subscriptions available\n");
}
}
break;
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////
// Variable & Functions for 0x22 and 0x23 ProtocolcountData
PROTOCOLCOUNT ProtocolcountData = { .rx = 0 };
void fn_ProtocolcountDataSum ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROTOCOL_MSG3full *msg ) {
switch (cmd) {
case PROTOCOL_CMD_READVAL:
case PROTOCOL_CMD_SILENTREAD:
ProtocolcountData.rx = s->ack.counters.rx + s->noack.counters.rx;
ProtocolcountData.rxMissing = s->ack.counters.rxMissing + s->noack.counters.rxMissing;
ProtocolcountData.tx = s->ack.counters.tx + s->noack.counters.tx;
ProtocolcountData.txFailed = s->ack.counters.txFailed + s->noack.counters.txFailed;
ProtocolcountData.txRetries = s->ack.counters.txRetries + s->noack.counters.txRetries;
ProtocolcountData.unwantedacks = s->ack.counters.unwantedacks + s->noack.counters.unwantedacks;
ProtocolcountData.unknowncommands = s->ack.counters.unknowncommands + s->noack.counters.unknowncommands;
ProtocolcountData.unplausibleresponse = s->ack.counters.unplausibleresponse + s->noack.counters.unplausibleresponse;
ProtocolcountData.unwantednacks = s->ack.counters.unwantednacks + s->noack.counters.unwantednacks;
break;
}
fn_defaultProcessingPreWriteClear(s, param, cmd, msg); // Wipes memory before write (and readresponse is just a differenct type of writing)
switch (cmd) {
case PROTOCOL_CMD_WRITEVAL:
case PROTOCOL_CMD_READVALRESPONSE:
s->ack.counters = ProtocolcountData;
s->noack.counters = ProtocolcountData;
break;
}
}
void fn_ProtocolcountDataAck ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROTOCOL_MSG3full *msg ) {
switch (cmd) {
case PROTOCOL_CMD_READVAL:
case PROTOCOL_CMD_SILENTREAD:
ProtocolcountData = s->ack.counters;
break;
}
fn_defaultProcessingPreWriteClear(s, param, cmd, msg); // Wipes memory before write (and readresponse is just a differenct type of writing)
switch (cmd) {
case PROTOCOL_CMD_WRITEVAL:
case PROTOCOL_CMD_READVALRESPONSE:
s->ack.counters = ProtocolcountData;
break;
}
}
void fn_ProtocolcountDataNoack ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROTOCOL_MSG3full *msg ) {
switch (cmd) {
case PROTOCOL_CMD_READVAL:
case PROTOCOL_CMD_SILENTREAD:
ProtocolcountData = s->noack.counters;
break;
}
fn_defaultProcessingPreWriteClear(s, param, cmd, msg); // Wipes memory before write (and readresponse is just a differenct type of writing)
switch (cmd) {
case PROTOCOL_CMD_WRITEVAL:
case PROTOCOL_CMD_READVALRESPONSE:
s->noack.counters = ProtocolcountData;
break;
}
}
////////////////////////////////////////////////////////////
// allows read of parameter descritpions and variable length
// accepts (unsigned char first, unsigned char count) in read message!
// data returned
typedef struct tag_descriptions {
unsigned char first;
unsigned char count_read;
char descriptions[251];
} DESCRIPTIONS;
DESCRIPTIONS paramstat_descriptions;
typedef struct tag_description {
unsigned char len; // overall length of THIS
unsigned char code; // code from params
unsigned char var_len;// length of variable referenced
unsigned char var_type;// UI_NONE or UI_SHORT for the moment
char description[249];// nul term description
// could be expanded here, as len at the top.
// but 'description' above will be shorter than 249, so strucutre variabls can;t be used?
} DESCRIPTION;
void fn_paramstat_descriptions ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROTOCOL_MSG3full *msg ) {
switch (cmd) {
case PROTOCOL_CMD_READVAL:
case PROTOCOL_CMD_SILENTREAD:
{
if(msg) {
// content[0] is the first entry to read.
// content[1] is max count of entries to read
// we prepare a buffer here, an MODIFY the paramstat length to tell it how much to send! (evil!?).
int first = 0;
int count = 100;
if (msg->lenPayload > 0) {
first = msg->content[0];
}
if (msg->lenPayload > 1) {
count = msg->content[1];
}
if (first >= (sizeof(s->params)/sizeof(s->params[0]))) {
s->params[0]->len = 0;
return;
}
if (first + count >= (sizeof(s->params)/sizeof(s->params[0]))){
count = (sizeof(s->params)/sizeof(s->params[0])) - first;
}
// now loop over requested entries until no more will fit in buffer,
// informing on start and count done.
int actual_count = 0;
int len_out = 0;
char *p = paramstat_descriptions.descriptions;
for (int i = first; i < first+count; i++){
if(s->params[i] != NULL) {
int desc_len = 0;
if (s->params[i]->description) {
desc_len = strlen(s->params[i]->description);
}
DESCRIPTION *d = (DESCRIPTION *)p;
if (len_out+sizeof(*d)-sizeof(d->description)+desc_len+1 > sizeof(paramstat_descriptions.descriptions)){
break;
}
d->len = sizeof(*d)-sizeof(d->description)+desc_len+1;
d->code = s->params[i]->code;
d->var_len = s->params[i]->len;
d->var_type = s->params[i]->ui_type;
if (desc_len) {
strcpy(d->description, s->params[i]->description);
} else {
d->description[0] = 0;
}
p += d->len;
len_out = p - paramstat_descriptions.descriptions;
actual_count++;
}
}
paramstat_descriptions.first = first;
paramstat_descriptions.count_read = actual_count;
param->len = sizeof(DESCRIPTIONS) - sizeof(paramstat_descriptions.descriptions) + len_out;
}
break;
}
}
fn_defaultProcessingReadOnly(s, param, cmd, msg);
switch (cmd) {
case PROTOCOL_CMD_READVAL:
case PROTOCOL_CMD_SILENTREAD:
if(msg) {
param->len = 0; // reset to zero
}
break;
}
}
////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
// NOTE: Don't start uistr with 'a'
const static PARAMSTAT initialparams[] = {
// 0x00 is reserved, can not be used.
// Protocol Relevant Parameters
{ 0xFF, "descriptions", NULL, UI_NONE, ¶mstat_descriptions, 0, fn_paramstat_descriptions },
{ 0xFE, "version", NULL, UI_LONG, &version, sizeof(uint32_t), fn_defaultProcessingReadOnly },
{ 0x22, "subscribe data", NULL, UI_NONE, &contentbuf, sizeof(PROTOCOL_SUBSCRIBEDATA), fn_SubscribeData },
{ 0x23, "protocol stats ack+noack",NULL, UI_NONE, &ProtocolcountData, sizeof(PROTOCOLCOUNT), fn_ProtocolcountDataSum },
{ 0x24, "protocol stats ack", NULL, UI_NONE, &ProtocolcountData, sizeof(PROTOCOLCOUNT), fn_ProtocolcountDataAck },
{ 0x25, "protocol stats noack", NULL, UI_NONE, &ProtocolcountData, sizeof(PROTOCOLCOUNT), fn_ProtocolcountDataNoack },
{ 0x26, "text", NULL, UI_NONE, &contentbuf, sizeof(contentbuf), fn_defaultProcessing },
{ 0x27, "ping", NULL, UI_NONE, &contentbuf, sizeof(contentbuf), fn_ping },
// Sensor (Hoverboard mode)
{ 0x01, "sensor data", NULL, UI_NONE, &contentbuf, sizeof(PROTOCOL_SENSOR_FRAME), fn_defaultProcessingReadOnly },
// Control and Measurements
{ 0x02, "hall data", NULL, UI_NONE, &contentbuf, 2*sizeof(PROTOCOL_HALL_DATA_STRUCT), fn_defaultProcessingReadOnly },
{ 0x03, "speed control mm/s", NULL, UI_NONE, &contentbuf, sizeof(PROTOCOL_SPEED_DATA), fn_defaultProcessingPreWriteClear },
{ 0x04, "hall position mm", NULL, UI_NONE, &contentbuf, sizeof(PROTOCOL_POSN), fn_defaultProcessingPreWriteClear },
{ 0x05, "position control increment mm",NULL,UI_NONE,&contentbuf,sizeof(PROTOCOL_POSN_INCR), fn_defaultProcessingPreWriteClear },
{ 0x06, "position control mm", NULL, UI_NONE, &contentbuf, sizeof(PROTOCOL_POSN_DATA), fn_defaultProcessingPreWriteClear },
{ 0x07, "hall position steps", NULL, UI_NONE, &contentbuf, sizeof(PROTOCOL_POSN), fn_defaultProcessingPreWriteClear },
{ 0x08, "electrical measurements", NULL, UI_NONE, &contentbuf, sizeof(PROTOCOL_ELECTRICAL_PARAMS), fn_defaultProcessingReadOnly },
{ 0x09, "enable motors", NULL, UI_CHAR, &contentbuf, sizeof(uint8_t), fn_defaultProcessingPreWriteClear },
{ 0x0A, "disable poweroff timer", NULL, UI_CHAR, &contentbuf, sizeof(uint8_t), fn_defaultProcessingPreWriteClear },
{ 0x0B, "enable console logs", NULL, UI_CHAR, &contentbuf, sizeof(uint8_t ), fn_defaultProcessingPreWriteClear },
{ 0x0C, "read/clear xyt position", NULL, UI_3LONG, &contentbuf, sizeof(PROTOCOL_INTEGER_XYT_POSN), fn_defaultProcessingPreWriteClear },
{ 0x0D, "PWM control", NULL, UI_NONE, &contentbuf, sizeof(PROTOCOL_PWM_DATA), fn_defaultProcessingPreWriteClear },
{ 0x0E, "simpler PWM", NULL, UI_2LONG, &contentbuf, sizeof( ((PROTOCOL_PWM_DATA *)0)->pwm), fn_defaultProcessingPreWriteClear },
{ 0x21, "buzzer", NULL, UI_NONE, &contentbuf, sizeof(PROTOCOL_BUZZER_DATA), fn_defaultProcessingPreWriteClear },
// Flash Storage
{ 0x80, "flash magic", "m", UI_SHORT, &contentbuf, sizeof(short), fn_defaultProcessingPreWriteClear }, // write this with CURRENT_MAGIC to commit to flash
{ 0x81, "posn kp x 100", "pkp", UI_SHORT, &contentbuf, sizeof(short), fn_defaultProcessingPreWriteClear },
{ 0x82, "posn ki x 100", "pki", UI_SHORT, &contentbuf, sizeof(short), fn_defaultProcessingPreWriteClear }, // pid params for Position
{ 0x83, "posn kd x 100", "pkd", UI_SHORT, &contentbuf, sizeof(short), fn_defaultProcessingPreWriteClear },
{ 0x84, "posn pwm lim", "pl", UI_SHORT, &contentbuf, sizeof(short), fn_defaultProcessingPreWriteClear }, // e.g. 200
{ 0x85, "speed kp x 100", "skp", UI_SHORT, &contentbuf, sizeof(short), fn_defaultProcessingPreWriteClear },
{ 0x86, "speed ki x 100", "ski", UI_SHORT, &contentbuf, sizeof(short), fn_defaultProcessingPreWriteClear }, // pid params for Speed
{ 0x87, "speed kd x 100", "skd", UI_SHORT, &contentbuf, sizeof(short), fn_defaultProcessingPreWriteClear },
{ 0x88, "speed pwm incr lim", "sl", UI_SHORT, &contentbuf, sizeof(short), fn_defaultProcessingPreWriteClear }, // e.g. 20
{ 0x89, "max current limit x 100", "cl", UI_SHORT, &contentbuf, sizeof(short), fn_defaultProcessingPreWriteClear }, // by default 1500 (=15 amps), limited by DC_CUR_LIMIT
{ 0x90, "adc settings", NULL, UI_NONE, &contentbuf, sizeof(PROTOCOL_ADC_SETTINGS), fn_defaultProcessingPreWriteClear },
{ 0xA0, "hoverboard enable", "he", UI_SHORT, &contentbuf, sizeof(short), fn_defaultProcessingPreWriteClear } // e.g. 20
};
/////////////////////////////////////////////
// Set entry in params
int setParam(PROTOCOL_STAT *s, PARAMSTAT *param) {
if(param == NULL) return 1; // Failure, got NULL pointer
// Check if len can actually be received
if( param->len > sizeof( ((PROTOCOL_MSG3full *)0)->content ) ) {
return 1; // Too long, Failure
}
if( param->code < (sizeof(s->params)/sizeof(s->params[0])) ) {
s->params[param->code] = param;
return 0; // Successfully assigned
}
return 1; // Failure, index too big.
}
int setParams(PROTOCOL_STAT *s, PARAMSTAT params[], int len) {
int error = 0;
for (int i = 0; i < len; i++) {
error += setParam(s, ¶ms[i]);
}
return error;
}
int setParamsCopy(PROTOCOL_STAT *s, const PARAMSTAT params[], int len) {
int error = 0;
for (int i = 0; i < len; i++) {
PARAMSTAT *newParam;
newParam = (PARAMSTAT *) malloc( sizeof(PARAMSTAT) );
memcpy(newParam, ¶ms[i], sizeof(PARAMSTAT));
error += setParam(s, newParam);
}
return error;
}
/////////////////////////////////////////////
// Change variable at runtime
int setParamVariable(PROTOCOL_STAT *s, unsigned char code, char ui_type, void *ptr, int len) {
// Check if len can actually be received
if( len > sizeof( ((PROTOCOL_MSG3full *)0)->content ) ) {
return 1; // Too long, Failure
}
if( code < (sizeof(s->params)/sizeof(s->params[0])) ) {
if(s->params[code] != NULL) {
s->params[code]->ui_type = ui_type;
s->params[code]->ptr = ptr;
s->params[code]->len = len;
return 0; // Success
}
}
return 1; // Not found, Failure
}
/////////////////////////////////////////////
// Register new function handler at runtime
int setParamHandler(PROTOCOL_STAT *s, unsigned char code, PARAMSTAT_FN callback) {
if( code < (sizeof(s->params)/sizeof(s->params[0])) ) {
if(s->params[code] == NULL) return 1;
s->params[code]->fn = callback;
return 0; // Successfully assigned
}
return 1; // Failure, index too big.
}
/////////////////////////////////////////////
// get param function handler
PARAMSTAT_FN getParamHandler(PROTOCOL_STAT *s, unsigned char code) {
if( code < (sizeof(s->params)/sizeof(s->params[0])) ) {
if(s->params[code] != NULL) {
return s->params[code]->fn;
}
}
return NULL;
}
/////////////////////////////////////////////
// initialize protocol
// called from main.c
int nosend( unsigned char *data, int len ){ return 0; };
int protocol_init(PROTOCOL_STAT *s) {
memset(s, 0, sizeof(*s));
s->timeout1 = 500;
s->timeout2 = 100;
s->allow_ascii = 1;
s->send_serial_data = nosend;
s->send_serial_data_wait = nosend;
s->ack.lastTXCI = 1;
s->ack.lastRXCI = 1;
s->noack.lastTXCI = 1;
s->noack.lastRXCI = 1;
// Initialize params array
int error = 0;
if (!s->initialised_functions) {
error += setParamsCopy(s, initialparams, sizeof(initialparams)/sizeof(initialparams[0]));
s->initialised_functions = 1;
// yes, may be called multiple times, but checks internally.
ascii_init(s);
}
// Send version as "welcome" message over protocol.
// First try will fail, as send_serial_data is set to dummy function. But the next try should
// succeed as it is expected to set send_serial_data to something useful before protocol_tick is called.
PROTOCOL_MSG3full newMsg;
memset((void*)&newMsg,0x00,sizeof(PROTOCOL_MSG3full));
uint32_t *writeint = (uint32_t *) newMsg.content;
newMsg.SOM = PROTOCOL_SOM_ACK; // Retry to send when no ACK was received
newMsg.lenPayload = sizeof(uint32_t);
newMsg.cmd = PROTOCOL_CMD_READVALRESPONSE; // Pretend someone requested this.
newMsg.code = 0xFE; // 0xFE for version
*writeint = version;
protocol_post(s, &newMsg);
return error;
}
/////////////////////////////////////////////
// a complete machineprotocol message has been
// received without error
void protocol_process_message(PROTOCOL_STAT *s, PROTOCOL_MSG3full *msg) {
switch (msg->cmd){
case PROTOCOL_CMD_SILENTREAD: {
if( msg->code < (sizeof(s->params)/sizeof(s->params[0])) ) {
if(s->params[msg->code] != NULL) {
if (s->params[msg->code]->fn) s->params[msg->code]->fn( s, s->params[msg->code], msg->cmd, msg ); // NOTE: re-uses the msg object (part of stats)
break;
}
}
break;
}
case PROTOCOL_CMD_READVAL: {
if( msg->code < (sizeof(s->params)/sizeof(s->params[0])) ) {
if(s->params[msg->code] != NULL) {
if (s->params[msg->code]->fn) s->params[msg->code]->fn( s, s->params[msg->code], msg->cmd, msg ); // NOTE: re-uses the msg object (part of stats)
break;
}
}
// parameter code not found
msg->lenPayload = 0;
msg->cmd = PROTOCOL_CMD_READVALRESPONSE; // mark as response
// send back with 'read' command plus data like write.
protocol_post(s, msg);
break;
}
case PROTOCOL_CMD_READVALRESPONSE: {
if( msg->code < (sizeof(s->params)/sizeof(s->params[0])) ) {
if(s->params[msg->code] != NULL) {
if (s->params[msg->code]->fn) s->params[msg->code]->fn( s, s->params[msg->code], msg->cmd, msg ); // NOTE: re-uses the msg object (part of stats)
break;
}
}
// parameter code not found
if(msg->SOM == PROTOCOL_SOM_ACK) {
s->ack.counters.unplausibleresponse++;
} else {
s->noack.counters.unplausibleresponse++;
}
break;
}
case PROTOCOL_CMD_WRITEVALRESPONSE:{
if( msg->code < (sizeof(s->params)/sizeof(s->params[0])) ) {
if(s->params[msg->code] != NULL) {
break;
}
}
// parameter code not found
if(msg->SOM == PROTOCOL_SOM_ACK) {
s->ack.counters.unplausibleresponse++;
} else {
s->noack.counters.unplausibleresponse++;
}
break;
}
case PROTOCOL_CMD_WRITEVAL:{
if( msg->code < (sizeof(s->params)/sizeof(s->params[0])) ) {
if(s->params[msg->code] != NULL) {
if (s->params[msg->code]->fn) s->params[msg->code]->fn( s, s->params[msg->code], msg->cmd, msg ); // NOTE: re-uses the msg object (part of stats)
break;
}
}
// parameter code not found
msg->lenPayload = 1; // '0' only
msg->cmd = PROTOCOL_CMD_WRITEVALRESPONSE; // mark as response
msg->content[0] = 0; // say we did not write it
// send back with 'write' command plus data like write.
protocol_post(s, msg);
break;
}
case PROTOCOL_CMD_REBOOT:
//protocol_send_ack(); // we no longer ack from here
protocol_Delay(500);
protocol_SystemReset();
break;
case PROTOCOL_CMD_TEST:
// just send it back!
msg->cmd = PROTOCOL_CMD_TESTRESPONSE;
// note: original 'bytes' sent back, so leave len as is
protocol_post(s, msg);
// post second immediately to test buffering
// protocol_post(s, msg);
break;
case PROTOCOL_CMD_UNKNOWN:
// Do nothing, otherwise endless loop is entered.
if(msg->SOM == PROTOCOL_SOM_ACK) {
s->ack.counters.unknowncommands++;
} else {
s->noack.counters.unknowncommands++;
}
break;
default:
if(msg->SOM == PROTOCOL_SOM_ACK) {
s->ack.counters.unknowncommands++;
} else {
s->noack.counters.unknowncommands++;
} msg->cmd = PROTOCOL_CMD_UNKNOWN;
msg->lenPayload = 0;
protocol_post(s, msg);
break;
}
}