-
Notifications
You must be signed in to change notification settings - Fork 9
/
transcoder.c
386 lines (332 loc) · 8.88 KB
/
transcoder.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
#include <stdio.h>
#include <string.h>
#include "errors.h"
#include "transcoder.h"
#define S_HEADER 0
#define S_ADDR0 1
#define S_ADDR1 2
#define S_ADDR2 3
#define S_PARAM0 4
#define S_PARAM1 5
#define S_CMD 6
#define S_LEN 7
#define S_PAYLOAD 8
#define S_CHECKSUM 9
#define S_COMPLETE 10
#define S_ERROR 11
static const uint8_t HEADER_FLAGS[16] = {
0x0F, 0x0C, 0x0D, 0x0B,
0x27, 0x24, 0x25, 0x23,
0x47, 0x44, 0x45, 0x43,
0x17, 0x14, 0x15, 0x13
};
inline uint8_t is_information(uint8_t flags) { return flags & 0x20; }
inline uint8_t is_request(uint8_t flags) { return flags & 0x08; }
inline uint8_t is_response(uint8_t flags) { return flags & 0x10; }
inline uint8_t is_write(uint8_t flags) { return flags & 0x40; }
inline uint8_t has_addr0(uint8_t flags) { return flags & 0x01; }
inline uint8_t has_addr1(uint8_t flags) { return flags & 0x02; }
inline uint8_t has_addr2(uint8_t flags) { return flags & 0x04; }
inline uint8_t has_param0(uint8_t header) { return header & 0x02; }
inline uint8_t has_param1(uint8_t header) { return header & 0x01; }
inline void set_information(uint8_t *flags) { *flags |= 0x20; }
inline void set_request(uint8_t *flags) { *flags |= 0x08; }
inline void set_response(uint8_t *flags) { *flags |= 0x10; }
inline void set_write(uint8_t *flags) { *flags |= 0x40; }
static write_str_fn write_str;
static send_byte_fn send_byte;
void transcoder_init(write_str_fn w, send_byte_fn s) {
write_str = w;
send_byte = s;
}
void transcoder_accept_inbound_byte(uint8_t b, uint8_t status) {
static uint8_t checksum = 0;
static uint8_t state = S_HEADER;
static uint8_t multi_bytes = 0;
static union {
uint16_t word16;
uint32_t word32;
} minibuf;
static uint8_t header;
static uint8_t flags;
static char str[12];
if (status != 0) {
if (state != S_COMPLETE || status != ERR_NONE) {
switch (status) {
case ERR_BAD_START_BIT:
write_str("\x09*ERR_BAD_START_BIT*");
break;
case ERR_BAD_STOP_BIT:
write_str("\x09*ERR_BAD_STOP_BIT*");
break;
case ERR_UNEXPECTED_START_OF_FRAME:
write_str("\x09*ERR_UNEXPECTED_START_OF_FRAME*");
break;
case ERR_UNEXPECTED_END_OF_FRAME:
write_str("\x09*ERR_UNEXPECTED_END_OF_FRAME*");
break;
case ERR_MANCHESTER_ENCODING:
write_str("\x09*ERR_MANCHESTER_ENCODING*");
break;
default:
write_str("\x09*ERR_UNKNOWN*");
break;
}
}
write_str("\r\n");
state = S_HEADER;
return;
}
if (state == S_COMPLETE) {
// bytes after end of packet?
write_str("\x09*E-DATA*");
state = S_ERROR;
return;
}
if (state == S_ERROR) {
// ignore bytes while in error state
return;
}
if (state == S_HEADER) {
checksum = b;
header = b;
flags = HEADER_FLAGS[(b >> 2) & 0x0F];
state = S_ADDR0;
multi_bytes = 0;
minibuf.word32 = 0;
if (is_information(flags)) { write_str("--- I --- "); return; }
if (is_request(flags)) { write_str("--- RQ --- "); return; }
if (is_response(flags)) { write_str("--- RP --- "); return; }
if (is_write(flags)) { write_str("--- W --- "); return; }
write_str("\x09*HDR*");
state = S_ERROR;
return;
}
checksum += b;
if (state == S_ADDR0) {
if (has_addr0(flags)) {
minibuf.word32 <<= 8;
minibuf.word32 |= b;
multi_bytes++;
if (multi_bytes == 3) {
sprintf(str, "%02hu:%06lu ", (uint8_t)(minibuf.word32 >> 18) & 0x3F, minibuf.word32 & 0x3FFFF);
write_str(str);
state = S_ADDR1;
multi_bytes = 0;
minibuf.word32 = 0;
}
return;
} else {
write_str("--:------ ");
state = S_ADDR1; // and fall through
}
}
if (state == S_ADDR1) {
if (has_addr1(flags)) {
minibuf.word32 <<= 8;
minibuf.word32 |= b;
multi_bytes++;
if (multi_bytes == 3) {
sprintf(str, "%02hu:%06lu ", (uint8_t)(minibuf.word32 >> 18) & 0x3F, minibuf.word32 & 0x3FFFF);
write_str(str);
state = S_ADDR2;
multi_bytes = 0;
minibuf.word32 = 0;
}
return;
} else {
write_str("--:------ ");
state = S_ADDR2; // and fall through
}
}
if (state == S_ADDR2) {
if (has_addr2(flags)) {
minibuf.word32 <<= 8;
minibuf.word32 |= b;
multi_bytes++;
if (multi_bytes == 3) {
sprintf(str, "%02hu:%06lu ", (uint8_t)(minibuf.word32 >> 18) & 0x3F, minibuf.word32 & 0x3FFFF);
write_str(str);
state = S_PARAM0;
multi_bytes = 0;
minibuf.word32 = 0;
}
return;
} else {
write_str("--:------ ");
state = S_PARAM0; // and fall through
}
}
if (state == S_PARAM0) {
if (has_param0(header)) {
// we don't use params; ditch it and move on
state = S_PARAM1;
return;
} else {
state = S_PARAM1; // and fall through
}
}
if (state == S_PARAM1) {
if (has_param1(header)) {
// we don't use params; ditch it and move on
state = S_CMD;
return;
} else {
state = S_CMD; // and fall through
}
}
if (state == S_CMD) {
minibuf.word16 <<= 8;
minibuf.word16 |= b;
multi_bytes++;
if (multi_bytes == 2) {
sprintf(str, "%04X ", minibuf.word16);
write_str(str);
state = S_LEN;
multi_bytes = 0;
minibuf.word32 = 0;
}
return;
}
if (state == S_LEN) {
multi_bytes = b;
sprintf(str, "%03hu ", b);
write_str(str);
state = S_PAYLOAD;
return;
}
if (state == S_PAYLOAD) {
if (multi_bytes > 0) {
sprintf(str, "%02hX", b);
write_str(str);
multi_bytes--;
} else {
state = S_CHECKSUM; // and fall through
}
}
if (state == S_CHECKSUM) {
if (checksum != 0) {
write_str("\x09*CHK*");
state = S_ERROR;
} else {
state = S_COMPLETE;
}
return;
}
}
#define SEND(byte) { checksum += byte; send_byte(byte, 0); }
static uint8_t pack_flags(uint8_t flags) {
for (uint8_t i = 0; i < sizeof(HEADER_FLAGS); i++) {
if (HEADER_FLAGS[i] == flags) return i << 2;
}
return 0xFF;
}
void transcoder_accept_outbound_byte(uint8_t b) {
static uint8_t flags = 0;
static uint32_t addrs[3];
static uint8_t field = 0;
static uint8_t p = 0;
static uint8_t checksum = 0;
static char buff[12];
if (b == '\n' || b == '\r') {
if (field == 0 && p == 0) return; // Empty string; most likely CR-LF pair
if (field == 7) {
send_byte(-checksum, 1);
} else {
send_byte(0x11, 1);
}
// reset state
flags = 0;
field = 0;
p = 0;
checksum = 0;
return;
}
if (field == -1) {
// Something went wrong with the packet. Skip all further bytes until EOL
return;
}
if (field == 7) {
// Payload comes last; no further separators; buffer character pairs to
// convert to bytes and write bytes directly as they're ready
buff[p++] = b;
if (p == 2) {
buff[p] = 0; // null terminate
uint8_t payload_byte;
sscanf(buff, "%02hhX", &payload_byte);
SEND(payload_byte);
p = 0;
}
return;
}
// Fields before 7, buffer until whitespace
if (b != ' ') {
if (p < sizeof(buff) - 1) {
buff[p++] = b;
}
return;
}
if (!p) return; // Double whitespace
buff[p] = 0; // null terminate
if (field == 0) {
if (!strcmp(buff, "I")) {
set_information(&flags);
} else if(!strcmp(buff,"RQ")) {
set_request(&flags);
} else if(!strcmp(buff,"RP")) {
set_response(&flags);
} else if(!strcmp(buff,"W")) {
set_write(&flags);
}
}
if (field >= 2 && field <= 4 && buff[0] != '-') {
uint8_t head;
uint32_t tail;
sscanf(buff, "%02hhu:%06lu", &head, &tail);
if (head == 18 && tail == 730) { // blank marker from Domoticz
addrs[field - 2] = 0x48DADA;
} else {
addrs[field - 2] = (tail | ((uint32_t)head << 18));
}
flags |= (1 << (field - 2));
}
if (field == 4) {
// We have now seen enough to complete the packet header byte, and
// can start transmitting
uint8_t header = pack_flags(flags);
if (header != 0xFF) {
SEND(header);
} else {
field = -1;
return;
}
if (has_addr0(flags)) {
SEND((addrs[0] >> 16) & 0xFF);
SEND((addrs[0] >> 8) & 0xFF);
SEND(addrs[0] & 0xFF);
}
if (has_addr1(flags)) {
SEND((addrs[1] >> 16) & 0xFF);
SEND((addrs[1] >> 8) & 0xFF);
SEND(addrs[1] & 0xFF);
}
if (has_addr2(flags)) {
SEND((addrs[2] >> 16) & 0xFF);
SEND((addrs[2] >> 8) & 0xFF);
SEND(addrs[2] & 0xFF);
}
}
if (field == 5) {
uint16_t cmd;
sscanf(buff, "%04X", &cmd);
SEND((cmd >> 8) & 0xFF);
SEND(cmd & 0xFF);
}
if (field == 6) {
uint8_t len;
sscanf(buff, "%03hhu", &len);
SEND(len);
}
p = 0;
field++;
}