forked from bipropellant/bipropellant-protocol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
machine_protocol.c
567 lines (474 loc) · 20.6 KB
/
machine_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
/*
* 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/>.
*/
/*
* usage:
* call void protocol_byte( unsigned char byte ); with incoming bytes from main.call
* will call protocol_process_message when message received (protocol.c)
* call protocol_post to send a message
*/
#include "stm32f1xx_hal.h"
#include "defines.h"
#include "config.h"
#ifdef CONTROL_SENSOR
#include "sensorcoms.h"
#endif
#include "protocol.h"
#ifdef HALL_INTERRUPTS
#include "hallinterrupts.h"
#endif
#ifdef SOFTWARE_SERIAL
#include "softwareserial.h"
#endif
#include "bldc.h"
#ifdef FLASH_STORAGE
#include "flashcontent.h"
#include "flashaccess.h"
#endif
#include "comms.h"
#include <string.h>
#include <stdlib.h>
#if (INCLUDE_PROTOCOL == INCLUDE_PROTOCOL2)
static unsigned char mpGetTxByte(MACHINE_PROTOCOL_TX_BUFFER *buf);
static char mpGetTxMsg(MACHINE_PROTOCOL_TX_BUFFER *buf, unsigned char *dest);
static void mpPutTx(MACHINE_PROTOCOL_TX_BUFFER *buf, unsigned char value);
///////////////////////////////////////////////////
// from protocol.c
extern PARAMSTAT params[];
extern int paramcount;
//////////////////////////////////////////////////////////////////
#define PROTOCOL_STATE_IDLE 0
#define PROTOCOL_STATE_WAIT_LEN 1
#define PROTOCOL_STATE_WAIT_CI 2
#define PROTOCOL_STATE_WAIT_END 3
#define PROTOCOL_STATE_BADCHAR 4
#define PROTOCOL_ACK_TX_IDLE 0
#define PROTOCOL_ACK_TX_WAITING 1
// private to us
static void protocol_send_nack(int (*send_serial_data)( unsigned char *data, int len ), unsigned char CI, unsigned char som);
static void protocol_send_ack(int (*send_serial_data)( unsigned char *data, int len ), unsigned char CI);
static int protocol_send(PROTOCOL_STAT *s, PROTOCOL_MSG2 *msg);
static void protocol_send_raw(int (*send_serial_data)( unsigned char *data, int len ), PROTOCOL_MSG2 *msg);
int nosend( unsigned char *data, int len ){ return 0; };
// called from main.c
// externed in protocol.h
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;
// Check if all lengths in params can actually be received
for (int i = 0; i < paramcount; i++) {
if( params[i].len > sizeof( ((PROTOCOL_BYTES_WRITEVALS *)0)->content ) ) {
params[i].len = 0; // Set to 0, so nothing can be received
return 1; // Failure
}
}
return 0; // Success
}
// called from main.c
// externed in protocol.h
void protocol_byte(PROTOCOL_STAT *s, unsigned char byte ){
switch(s->state){
case PROTOCOL_STATE_BADCHAR:
case PROTOCOL_STATE_IDLE:
if ((byte == PROTOCOL_SOM_ACK) || (byte == PROTOCOL_SOM_NOACK)){
s->curr_msg.SOM = byte;
s->last_char_time = HAL_GetTick();
s->state = PROTOCOL_STATE_WAIT_CI;
s->CS = 0;
} else {
if (s->allow_ascii){
//////////////////////////////////////////////////////
// if the byte was NOT SOM (02), then treat it as an
// ascii protocol byte. BOTH protocol can co-exist
ascii_byte(s, byte );
//////////////////////////////////////////////////////
} else {
s->last_char_time = HAL_GetTick();
s->state = PROTOCOL_STATE_BADCHAR;
}
}
break;
case PROTOCOL_STATE_WAIT_CI:
s->last_char_time = HAL_GetTick();
s->curr_msg.CI = byte;
s->CS += byte;
s->state = PROTOCOL_STATE_WAIT_LEN;
break;
case PROTOCOL_STATE_WAIT_LEN:
s->last_char_time = HAL_GetTick();
s->curr_msg.len = byte;
s->count = 0;
s->CS += byte;
s->state = PROTOCOL_STATE_WAIT_END;
break;
case PROTOCOL_STATE_WAIT_END:
s->last_char_time = HAL_GetTick();
s->curr_msg.bytes[s->count++] = byte;
s->CS += byte;
if (s->count == s->curr_msg.len+1){
s->last_char_time = 0;
switch(s->curr_msg.SOM) {
case PROTOCOL_SOM_ACK:
switch(s->curr_msg.bytes[0]) {
case PROTOCOL_CMD_ACK:
if (s->send_state == PROTOCOL_ACK_TX_WAITING){
if (s->curr_msg.CI == s->ack.curr_send_msg.CI){
s->ack.last_send_time = 0;
s->send_state = PROTOCOL_ACK_TX_IDLE;
// if we got ack, then try to send a next message
int txcount = mpTxQueued(&s->ack.TxBuffer);
if (txcount){
// send from tx queue
protocol_send(s, NULL);
}
} else {
// ignore
s->ack.counters.unwantedacks++;
// 'if an ACK is received which contains a CI different to the last sent message, the ACK will be ignored. (?? implications??)'
}
} else {
// ignore, sort of:
// 'if an ACK is received which contains the same CI as the last ACK, the ACK will be ignored.'
s->ack.counters.unwantedacks++;
}
break;
case PROTOCOL_CMD_NACK:
// 'If an end receives a NACK, it should resend the last message with the same CI, up to 2 retries'
if (s->send_state == PROTOCOL_ACK_TX_WAITING){
// ignore CI
if (s->ack.retries > 0){
s->ack.counters.txRetries++;
protocol_send_raw(s->send_serial_data, &s->ack.curr_send_msg);
s->ack.last_send_time = HAL_GetTick();
s->ack.retries--;
} else {
s->send_state = PROTOCOL_ACK_TX_IDLE;
s->ack.counters.txFailed++;
// if we run out of retries, then try to send a next message
int txcount = mpTxQueued(&s->ack.TxBuffer);
if (txcount){
// send from tx queue
protocol_send(s, NULL);
}
}
} else {
// unsolicited NACK received?
s->ack.counters.unwantednacks++;
// ignore
}
break;
default:
if (s->CS != 0){
// Checksum invalid. Complain and Discard.
protocol_send_nack(s->send_serial_data, s->curr_msg.CI, s->curr_msg.SOM);
break;
}
if (s->ack.lastRXCI == s->curr_msg.CI) {
// 'if a message is received with the same CI as the last received message, ACK will be sent, but the message discarded.'
protocol_send_ack(s->send_serial_data, s->curr_msg.CI);
break;
}
if( s->curr_msg.CI < s->ack.lastRXCI) {
// CI is smaller than a received message. Probably Overflow of CI. (Other case would resending of previous lost message. Shouldn't happen.)
s->ack.lastRXCI = s->ack.lastRXCI - 256; // Max value of char is 255
}
// Add to rxMissing Counter, when CIs where skipped
s->ack.counters.rxMissing += s->curr_msg.CI - (s->ack.lastRXCI + 1);
// process message
protocol_send_ack(s->send_serial_data, s->curr_msg.CI);
s->ack.lastRXCI = s->curr_msg.CI;
s->ack.counters.rx++;
protocol_process_message(s, &(s->curr_msg));
break;
}
break;
case PROTOCOL_SOM_NOACK:
switch(s->curr_msg.bytes[0]) {
case PROTOCOL_CMD_ACK:
// We shouldn't get ACKs in the NoACK protocol..
s->noack.counters.unwantedacks++;
break;
case PROTOCOL_CMD_NACK:
// 'If an end receives a NACK, it should resend the last message with the same CI, up to 2 retries'
if (s->noack.retries > 0){
s->noack.counters.txRetries++;
protocol_send_raw(s->send_serial_data, &s->noack.curr_send_msg);
s->noack.retries--;
} else {
s->noack.counters.txFailed++;
// if we run out of retries, then try to send a next message
int txcount = mpTxQueued(&s->noack.TxBuffer);
if (txcount){
// send from tx queue
protocol_send(s, NULL);
}
}
break;
default:
if (s->CS != 0){
// Checksum invalid. Complain and Discard.
protocol_send_nack(s->send_serial_data, s->curr_msg.CI, s->curr_msg.SOM);
break;
}
if (s->noack.lastRXCI == s->curr_msg.CI) {
// if a message is received with the same CI as the last received message, the message is discarded.
break;
}
if( s->curr_msg.CI < s->noack.lastRXCI) {
// CI is smaller than a received message. Probably Overflow of CI. (Other case would resending of previous lost message. Shouldn't happen.)
s->noack.lastRXCI = s->noack.lastRXCI - 256; // Max value of char is 255
}
// Add to rxMissing Counter, when CIs where skipped
s->noack.counters.rxMissing += s->curr_msg.CI - (s->noack.lastRXCI + 1);
// process message
s->noack.lastRXCI = s->curr_msg.CI;
s->noack.counters.rx++;
protocol_process_message(s, &(s->curr_msg));
break;
}
break;
}
s->state = PROTOCOL_STATE_IDLE;
}
break;
}
}
// private
void protocol_send_nack(int (*send_serial_data)( unsigned char *data, int len ), unsigned char CI, unsigned char som){
// Enforce valid SOM, otherwise Message is discarded by protocol_send_raw.
// PROTOCOL_SOM_ACK is chosen to ensure backwards compatibilty.
// If ANY SOM could be send, it would be identified as a badchar, which would trigger a NACK with the same SOM.
// That's an infinite loop of NACKs with invalid SOM.
if(som != PROTOCOL_SOM_ACK && som != PROTOCOL_SOM_NOACK) {
som = PROTOCOL_SOM_ACK;
}
char tmp[] = { som, CI, 1, PROTOCOL_CMD_NACK, 0 };
protocol_send_raw(send_serial_data, (PROTOCOL_MSG2 *)tmp);
}
// private
void protocol_send_ack(int (*send_serial_data)( unsigned char *data, int len ), unsigned char CI){
char tmp[] = { PROTOCOL_SOM_ACK, CI, 1, PROTOCOL_CMD_ACK, 0 };
protocol_send_raw(send_serial_data, (PROTOCOL_MSG2 *)tmp);
}
// called to send a message.
// just supply len|bytes - no SOM, CI, or CS
// returns:
// -1 - cannot even queue
// 0 sent immediately
// 1 queued for later TX
int protocol_post(PROTOCOL_STAT *s, PROTOCOL_MSG2 *msg){
if(msg->SOM == PROTOCOL_SOM_ACK) {
int txcount = mpTxQueued(&s->ack.TxBuffer);
if ((s->send_state != PROTOCOL_ACK_TX_WAITING) && !txcount){
return protocol_send(s, msg);
}
// add to tx queue
int total = msg->len + 1; // +1 len
if (txcount + total >= MACHINE_PROTOCOL_TX_BUFFER_SIZE-2) {
s->ack.TxBuffer.overflow++;
return -1;
}
char *src = (char *) &(msg->len);
for (int i = 0; i < total; i++) {
mpPutTx(&s->ack.TxBuffer, *(src++));
}
return 1; // added to queue
} else if(msg->SOM == PROTOCOL_SOM_NOACK) {
return protocol_send(s, msg);
}
return -1;
}
// private
// note: if NULL in, send one message from queue
int protocol_send(PROTOCOL_STAT *s, PROTOCOL_MSG2 *msg){
if(msg) {
if(msg->SOM == PROTOCOL_SOM_ACK && s->send_state == PROTOCOL_ACK_TX_WAITING) {
// Tried to Send Message which requires ACK, but a message is still pending.
return -1;
} else if(msg->SOM == PROTOCOL_SOM_ACK && s->send_state == PROTOCOL_ACK_TX_IDLE) {
// Idling (not waiting for ACK), send the Message directly
memcpy(&s->ack.curr_send_msg, msg, 1 + 1 + 1 + msg->len); // SOM + CI + Len + Payload
s->ack.curr_send_msg.CI = ++(s->ack.lastTXCI);
s->ack.counters.tx++;
protocol_send_raw(s->send_serial_data, &s->ack.curr_send_msg);
s->send_state = PROTOCOL_ACK_TX_WAITING;
s->ack.last_send_time = HAL_GetTick();
s->ack.retries = 2;
return 0;
} else if (msg->SOM == PROTOCOL_SOM_NOACK) {
// Send Message without ACK immediately
memcpy(&s->noack.curr_send_msg, msg, 1 + 1 + 1 + msg->len); // SOM + CI + Len + Payload
s->noack.curr_send_msg.CI = ++(s->noack.lastTXCI);
s->noack.counters.tx++;
protocol_send_raw(s->send_serial_data, &s->noack.curr_send_msg);
return 0;
} else {
// Shouldn't happen, unknowm SOM
return -1;
}
} else {
// No Message was given, work on Buffers then..
if(s->send_state == PROTOCOL_STATE_IDLE && mpTxQueued(&s->ack.TxBuffer)) {
// Make sure we are not waiting for another ACK. Check if There is something in the buffer.
mpGetTxMsg(&s->ack.TxBuffer, &s->ack.curr_send_msg.len);
s->ack.curr_send_msg.SOM = PROTOCOL_SOM_ACK;
s->ack.curr_send_msg.CI = ++(s->ack.lastTXCI);
s->ack.counters.tx++;
protocol_send_raw(s->send_serial_data, &s->ack.curr_send_msg);
s->send_state = PROTOCOL_ACK_TX_WAITING;
s->ack.last_send_time = HAL_GetTick();
s->ack.retries = 2;
return 0;
} else {
// Do the other queue
int ismsg = mpGetTxMsg(&s->noack.TxBuffer, &s->noack.curr_send_msg.len);
if (ismsg){
// Queue has message waiting
s->noack.curr_send_msg.SOM = PROTOCOL_SOM_NOACK;
s->noack.curr_send_msg.CI = ++(s->noack.lastTXCI);
s->noack.counters.tx++;
protocol_send_raw(s->send_serial_data, &s->noack.curr_send_msg);
return 0;
}
}
}
return -1; // nothing to send
}
// private
void protocol_send_raw(int (*send_serial_data)( unsigned char *data, int len ), PROTOCOL_MSG2 *msg){
// Enforce validity of SOM.
if(msg->SOM == PROTOCOL_SOM_ACK || msg->SOM == PROTOCOL_SOM_NOACK) {
unsigned char CS = 0;
int i;
CS -= msg->CI;
CS -= msg->len;
for (i = 0; i < msg->len; i++){
CS -= msg->bytes[i];
}
msg->bytes[i] = CS;
send_serial_data((unsigned char *) msg, msg->len+4);
}
}
// called regularly from main.c
// externed from protocol.h
void protocol_tick(PROTOCOL_STAT *s){
s->last_tick_time = HAL_GetTick();
if(s->send_state == PROTOCOL_ACK_TX_WAITING) {
if ((s->last_tick_time - s->ack.last_send_time) > s->timeout1){
// 'If an end does not receive an ACK response within (TIMEOUT1), it should resend the last message with the same CI, up to 2 retries'
if (s->ack.retries > 0){
s->ack.counters.txRetries++;
protocol_send_raw(s->send_serial_data, &s->ack.curr_send_msg);
s->ack.last_send_time = HAL_GetTick();
s->ack.retries--;
} else {
// if we run out of retries, then try to send a next message
s->send_state = PROTOCOL_ACK_TX_IDLE;
}
}
}
// send from tx queue
protocol_send(s, NULL);
switch(s->state){
case PROTOCOL_STATE_IDLE:
break;
case PROTOCOL_STATE_BADCHAR:
// 'normally, characters received BETWEEN messages which are not SOM should be treated as ASCII commands.'
// 'implement a mode where non SOM characters between messages cause (TIMEOUT2) to be started,
// resulting in a _NACK with CI of last received message + 1_.'
if ((s->last_tick_time - s->last_char_time) > s->timeout2){
protocol_send_nack(s->send_serial_data, s->curr_msg.CI+1, s->curr_msg.SOM);
s->last_char_time = 0;
s->state = PROTOCOL_STATE_IDLE;
}
break;
default:
// in a message
// 'In receive, if in a message (SOM has been received) and the time since the last character
// exceeds (TIMEOUT2), the incomming message should be discarded,
// and a NACK should be sent with the CI of the message in progress or zero if no CI received yet'
if ((s->last_tick_time - s->last_char_time) > s->timeout2){
protocol_send_nack(s->send_serial_data, s->curr_msg.CI, s->curr_msg.SOM);
s->last_char_time = 0;
s->state = PROTOCOL_STATE_IDLE;
}
break;
}
// process subscriptions
int len = sizeof(s->subscriptions)/sizeof(s->subscriptions[0]);
int index = 0;
// Check if subscription exists.
for (index = 0; index < len; index++) {
if( s->subscriptions[index].code != 0 && s->subscriptions[index].count != 0 ) {
// Check if message is due
if( s->subscriptions[index].next_send_time <= s->last_tick_time) {
//If so, pretend that a Read request has arrived.
PROTOCOL_MSG2 newMsg;
memset((void*)&newMsg,0x00,sizeof(PROTOCOL_MSG2));
PROTOCOL_BYTES_READVALS *readvals = (PROTOCOL_BYTES_READVALS *) &(newMsg.bytes);
readvals->cmd = PROTOCOL_CMD_READVAL;
readvals->code = s->subscriptions[index].code;
newMsg.SOM = s->subscriptions[index].som;
newMsg.len = sizeof(readvals->cmd) + sizeof(readvals->code) + 1; // 1 for Checksum
protocol_process_message(s, &newMsg);
//reschedule job
s->subscriptions[index].next_send_time = s->last_tick_time + s->subscriptions[index].period;
if(s->subscriptions[index].count > 0) {
s->subscriptions[index].count = s->subscriptions[index].count - 1;
}
}
}
}
}
int mpTxQueued(MACHINE_PROTOCOL_TX_BUFFER *buf){
if (buf->head != buf->tail){
int count = buf->head - buf->tail;
if (count < 0){
count += MACHINE_PROTOCOL_TX_BUFFER_SIZE;
}
return count;
}
return 0;
}
unsigned char mpGetTxByte(MACHINE_PROTOCOL_TX_BUFFER *buf){
short t = -1;
if (buf->head != buf->tail){
t = buf->buff[buf->tail];
buf->tail = ((buf->tail + 1 ) % MACHINE_PROTOCOL_TX_BUFFER_SIZE);
}
return t;
}
char mpGetTxMsg(MACHINE_PROTOCOL_TX_BUFFER *buf, unsigned char *dest){
if (mpTxQueued(buf)) {
unsigned char len = *(dest++) = mpGetTxByte(buf); // len of bytes to follow
for (int i = 0; i < len; i++) {
*(dest++) = mpGetTxByte(buf); // data
}
return 1; // we got a message
}
return 0;
}
void mpPutTx(MACHINE_PROTOCOL_TX_BUFFER *buf, unsigned char value){
buf->buff[buf->head] = value;
buf->head = ((buf->head + 1 ) % MACHINE_PROTOCOL_TX_BUFFER_SIZE);
}
#endif // INCLUDE_PROTOCOL2