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
|
#include "hybbx/kiss.h"
#include <string.h>
void hybbx_kiss_decoder_init(hybbx_kiss_decoder_t *dec)
{
if (dec == NULL) {
return;
}
memset(dec, 0, sizeof(*dec));
}
static void deliver_frame(hybbx_kiss_decoder_t *dec,
hybbx_kiss_frame_cb cb, void *userdata)
{
uint8_t port;
const uint8_t *payload;
size_t payload_len;
if (dec->len < 1) {
return;
}
port = (uint8_t)((dec->buf[0] >> 4) & 0x0Fu);
payload = dec->buf + 1;
payload_len = dec->len - 1;
if (cb != NULL) {
cb(port, payload, payload_len, userdata);
}
}
void hybbx_kiss_decoder_feed(hybbx_kiss_decoder_t *dec,
const uint8_t *data, size_t len,
hybbx_kiss_frame_cb cb, void *userdata)
{
size_t i;
if (dec == NULL || data == NULL) {
return;
}
for (i = 0; i < len; i++) {
uint8_t byte = data[i];
if (byte == HYBBX_KISS_FEND) {
if (dec->in_frame && dec->len > 0) {
deliver_frame(dec, cb, userdata);
}
dec->in_frame = 1;
dec->escape = 0;
dec->len = 0;
continue;
}
if (!dec->in_frame) {
continue;
}
if (dec->escape) {
if (byte == HYBBX_KISS_TFEND) {
byte = HYBBX_KISS_FEND;
} else if (byte == HYBBX_KISS_TFESC) {
byte = HYBBX_KISS_FESC;
}
dec->escape = 0;
} else if (byte == HYBBX_KISS_FESC) {
dec->escape = 1;
continue;
}
if (dec->len >= HYBBX_KISS_MAX_FRAME) {
dec->len = 0;
dec->in_frame = 0;
continue;
}
dec->buf[dec->len++] = byte;
}
}
static size_t kiss_write_byte(uint8_t byte, uint8_t *out, size_t out_cap, size_t pos)
{
if (pos + 1 > out_cap) {
return 0;
}
if (byte == HYBBX_KISS_FEND) {
if (pos + 2 > out_cap) {
return 0;
}
out[pos++] = HYBBX_KISS_FESC;
out[pos++] = HYBBX_KISS_TFEND;
return pos;
}
if (byte == HYBBX_KISS_FESC) {
if (pos + 2 > out_cap) {
return 0;
}
out[pos++] = HYBBX_KISS_FESC;
out[pos++] = HYBBX_KISS_TFESC;
return pos;
}
out[pos++] = byte;
return pos;
}
size_t hybbx_kiss_encode(uint8_t port, hybbx_kiss_command_t cmd,
const uint8_t *payload, size_t payload_len,
uint8_t *out, size_t out_cap)
{
size_t pos = 0;
uint8_t cmd_byte;
size_t i;
if (out == NULL || out_cap < 3) {
return 0;
}
if (payload_len > 0 && payload == NULL) {
return 0;
}
if (payload_len + 4 > out_cap) {
return 0;
}
out[pos++] = HYBBX_KISS_FEND;
cmd_byte = (uint8_t)(((port & 0x0Fu) << 4) | ((uint8_t)cmd & 0x0Fu));
pos = kiss_write_byte(cmd_byte, out, out_cap, pos);
if (pos == 0) {
return 0;
}
for (i = 0; i < payload_len; i++) {
pos = kiss_write_byte(payload[i], out, out_cap, pos);
if (pos == 0) {
return 0;
}
}
if (pos + 1 > out_cap) {
return 0;
}
out[pos++] = HYBBX_KISS_FEND;
return pos;
}
|