aboutsummaryrefslogtreecommitdiff
path: root/fw/protocol.c
blob: 2940acae73de887922ece53af911392c8502539f (plain)
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
#include "protocol.h"
#include "8b10b.h"

void receive_symbol(struct proto_rx_st *st, int symbol) {
    if (symbol == -K28_1) { /* Comma/frame delimiter */
        st->rxpos = 0;
        /* Fall through and return and just ignore incomplete packets */

    } else if (symbol == -DECODING_ERROR) {
        st->rxpos = -1;

    } else if (symbol < 0) { /* Unknown comma symbol or error */
        st->rxpos = -1;

    } else if (st->rxpos == -1) {
        return;

    } else if (st->rxpos == 0) { /* First data symbol, and not an error or comma symbol */
        st->packet_type = symbol & ~PKT_TYPE_BULK_FLAG;
        if (st->packet_type >= st->cmd_if->packet_type_max) {
            st->rxpos = -1;
            return;
        }

        st->is_bulk = symbol & PKT_TYPE_BULK_FLAG;
        st->offset = (st->is_bulk) ? st->address*st->cmd_if->payload_len[st->packet_type]+1 : 2;
        st->rxpos++;

    } else if (!st->is_bulk && st->rxpos == 1) {
        st->rxpos = (symbol == st->address) ? 2 : -1;

    } else {
        st->argbuf[st->rxpos - st->offset] = symbol;
        st->rxpos++;

        if (st->rxpos - st->offset == st->cmd_if->payload_len[st->packet_type]) {
            handle_command(st->packet_type, (uint8_t *)st->argbuf);
            st->rxpos = -1;
        }
    }
}