This repository has been archived by the owner on Mar 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathge-rs232.c
655 lines (599 loc) · 13.4 KB
/
ge-rs232.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
#include "ge-rs232.h"
#include <strings.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#if __AVR__
#include <avr/pgmspace.h>
static char int_to_hex_digit(uint8_t x) {
return pgm_read_byte_near(PSTR(
"0123456789ABCDEF") + (x & 0xF));
}
#else
static char int_to_hex_digit(uint8_t x) {
return "0123456789ABCDEF"[x & 0xF];
}
#endif
#if !HAVE_STRLCPY && !defined(strlcpy)
#define strlcpy(...) ___smcp_strlcpy(__VA_ARGS__)
size_t ___smcp_strlcpy(char* dest, const char* src, size_t len);
#endif
#if !HAVE_STRLCAT && !defined(strlcat)
#define strlcat(...) ___smcp_strlcat(__VA_ARGS__)
size_t ___smcp_strlcat(char* dest, const char* src, size_t len);
#endif
#ifndef MIN
// I know it sucks, but whatever.
#define MIN(X, Y) (((X) < (Y)) ? (X) : (Y))
#endif
#if !HAVE_STRLCAT
size_t
___smcp_strlcat(char *dst, const char *src, size_t siz)
{
size_t dlen = strlen(dst);
if (dlen < siz - 1) {
return (dlen + strlcpy(dst + dlen, src, siz-dlen));
}
return dlen + strlen(src);
}
#endif
#if !HAVE_STRLCPY
size_t
___smcp_strlcpy(char *dst, const char *src, size_t siz)
{
size_t n;
size_t slen = strlen(src);
if (siz) {
if ((n = MIN(slen, siz - 1))) {
memcpy(dst, src, n);
}
dst[n] = '\0';
}
return slen;
}
#endif
static char
hex_digit_to_int(char c) {
switch(c) {
case '0': return 0; break;
case '1': return 1; break;
case '2': return 2; break;
case '3': return 3; break;
case '4': return 4; break;
case '5': return 5; break;
case '6': return 6; break;
case '7': return 7; break;
case '8': return 8; break;
case '9': return 9; break;
case 'A':
case 'a': return 10; break;
case 'B':
case 'b': return 11; break;
case 'C':
case 'c': return 12; break;
case 'D':
case 'd': return 13; break;
case 'E':
case 'e': return 14; break;
case 'F':
case 'f': return 15; break;
}
return 0;
}
ge_rs232_t
ge_rs232_init(ge_rs232_t self) {
bzero((void*)self,sizeof(*self));
self->last_response = GE_RS232_ACK;
return self;
}
ge_rs232_status_t
ge_rs232_receive_byte(ge_rs232_t self, uint8_t byte) {
ge_rs232_status_t ret = GE_RS232_STATUS_OK;
if(byte == GE_RS232_START_OF_MESSAGE) {
self->reading_message = true;
self->current_byte = 0;
self->message_len = 255;
self->nibble_buffer = 0;
self->buffer_sum = 0;
} else if(byte == GE_RS232_ACK && !self->last_response) {
self->last_response = GE_RS232_ACK;
if(self->got_response)
self->got_response(self->response_context,self,true);
} else if(byte == GE_RS232_NAK && !self->last_response) {
self->last_response = GE_RS232_NAK;
if(self->got_response)
self->got_response(self->response_context,self,false);
} else if(self->reading_message) {
if(!self->nibble_buffer) {
self->nibble_buffer = byte;
goto bail;
}
uint8_t value = (hex_digit_to_int(self->nibble_buffer)<<4)+hex_digit_to_int(byte);
self->nibble_buffer = 0;
if(self->message_len==255) {
if(value>GE_RS232_MAX_MESSAGE_SIZE) {
ret = GE_RS232_STATUS_MESSAGE_TOO_BIG;
self->reading_message = false;
goto bail;
}
if(value<2) {
ret = GE_RS232_STATUS_MESSAGE_TOO_SMALL;
self->reading_message = false;
goto bail;
}
self->message_len = value;
self->current_byte = 0;
} else {
self->buffer[self->current_byte++] = value;
}
if(self->current_byte>=self->message_len) {
self->reading_message = false;
if(self->buffer_sum == value) {
self->send_byte(self->context,GE_RS232_ACK,self);
ret = self->received_message(self->context,self->buffer,self->message_len-1,self);
} else {
fprintf(stderr,"[Bad checksum: calculated:0x%02X != indicated:0x%02X]\n",self->buffer_sum,value);
self->send_byte(self->context,GE_RS232_NAK,self);
ret = GE_RS232_STATUS_BAD_CHECKSUM;
}
} else {
self->buffer_sum += value;
}
} else {
// Just some junk byte we don't know what to do with.
ret = GE_RS232_STATUS_JUNK;
}
bail:
return ret;
}
ge_rs232_status_t
ge_rs232_ready_to_send(ge_rs232_t self) {
ge_rs232_status_t ret = GE_RS232_STATUS_WAIT;
time_t curr_time = time(&curr_time);
if(self->last_response == GE_RS232_ACK) {
ret = GE_RS232_STATUS_OK;
} else if(self->last_response == GE_RS232_NAK) {
ret = GE_RS232_STATUS_NAK;
} else if(curr_time-self->last_sent > 1) {
ret = GE_RS232_STATUS_TIMEOUT;
}
return ret;
}
ge_rs232_status_t
ge_rs232_resend_last_message(ge_rs232_t self) {
return ge_rs232_send_message(self,self->output_buffer,self->output_buffer_len);
}
ge_rs232_status_t
ge_rs232_send_message(ge_rs232_t self, const uint8_t* data, uint8_t len) {
ge_rs232_status_t ret = GE_RS232_STATUS_OK;
uint8_t checksum = len+1;
if(len>GE_RS232_MAX_MESSAGE_SIZE) {
ret = GE_RS232_STATUS_MESSAGE_TOO_BIG;
goto bail;
}
if(data!=self->output_buffer) {
memcpy(self->output_buffer,data,len);
self->output_buffer_len = len;
self->output_attempt_count = 0;
}
self->output_attempt_count++;
if(self->last_response == 0) {
ret = self->send_byte(self->context,0x0D,self);
if(ret) goto bail;
ret = self->send_byte(self->context,0x0A,self);
if(ret) goto bail;
}
self->last_response = 0;
ret = self->send_byte(self->context,GE_RS232_START_OF_MESSAGE,self);
if(ret) goto bail;
// Checksum has the length+1, which is what we want to write out.
ret = self->send_byte(self->context,int_to_hex_digit(checksum>>4),self);
if(ret) goto bail;
ret = self->send_byte(self->context,int_to_hex_digit(checksum),self);
if(ret) goto bail;
// Write out the data.
for(;len;len--,data++) {
checksum += *data;
ret = self->send_byte(self->context,int_to_hex_digit((*data)>>4),self);
if(ret) goto bail;
ret = self->send_byte(self->context,int_to_hex_digit((*data)),self);
if(ret) goto bail;
}
// Now write out the checksum.
ret = self->send_byte(self->context,int_to_hex_digit(checksum>>4),self);
if(ret) goto bail;
ret = self->send_byte(self->context,int_to_hex_digit(checksum),self);
if(ret) goto bail;
self->last_sent = time(&self->last_sent);
bail:
return ret;
}
/*
#ifndef GE_QUEUE_MAX_MESSAGES (8)
struct ge_message_s {
uint8_t msg[GE_RS232_MAX_MESSAGE_SIZE];
uint8_t msg_len;
void* context;
void (*finished)(void* context,ge_rs232_status_t status);
};
struct ge_queue_s {
ge_rs232_t interface;
struct ge_message_s queue[GE_QUEUE_MAX_MESSAGES];
uint8_t head, tail;
};
typedef struct ge_queue_s *ge_queue_t;
*/
ge_queue_t
ge_queue_init(ge_queue_t qinterface, ge_rs232_t interface) {
memset((void*)qinterface,sizeof(*qinterface),0);
qinterface->interface = interface;
return qinterface;
}
static void
ge_queue_got_response(void* context,struct ge_rs232_s* instance, bool didAck) {
ge_queue_t qinterface = context;
ge_rs232_status_t status = ge_rs232_ready_to_send(qinterface->interface);
struct ge_message_s *message = &qinterface->queue[qinterface->head];
if(status==GE_RS232_STATUS_OK || message->attempts>=3) {
if(NULL!=message->finished)
message->finished(
message->context,
status
);
qinterface->head = (qinterface->head+1)&(GE_QUEUE_MAX_MESSAGES-1);
}
qinterface->interface->got_response = NULL;
qinterface->interface->response_context = NULL;
}
ge_rs232_status_t
ge_queue_update(ge_queue_t qinterface) {
ge_rs232_status_t status = 0;
struct ge_message_s *message;
if(qinterface->head==qinterface->tail)
goto bail; // Empty.
if(ge_rs232_ready_to_send(qinterface->interface)==GE_RS232_STATUS_WAIT)
goto bail; // Busy.
if( ge_rs232_ready_to_send(qinterface->interface) == GE_RS232_STATUS_TIMEOUT
&& qinterface->interface->got_response == &ge_queue_got_response
) {
(*qinterface->interface->got_response)(qinterface->interface->response_context,qinterface->interface,0);
}
// RELEASE THE KRAKEN!
message = &qinterface->queue[qinterface->head];
message->attempts++;
qinterface->interface->got_response = &ge_queue_got_response;
qinterface->interface->response_context = qinterface;
status = ge_rs232_send_message(qinterface->interface, message->msg, message->msg_len);
bail:
return status;
}
ge_rs232_status_t ge_queue_message(
ge_queue_t qinterface,
const uint8_t* data,
uint8_t len,
void (*finished)(void* context,ge_rs232_status_t status),
void* context
) {
ge_rs232_status_t status = 0;
struct ge_message_s *message = &qinterface->queue[qinterface->tail];
// Stuff is added to the tail and removed from the head.
// Tail is always empty.
if((qinterface->tail-qinterface->head&(GE_QUEUE_MAX_MESSAGES-1)) == (GE_QUEUE_MAX_MESSAGES-1)) {
// Queue is full!
status = GE_RS232_STATUS_QUEUE_FULL;
}
message->context = context;
message->finished = finished;
memcpy(message->msg,data,len);
message->msg_len = len;
message->attempts = 0;
qinterface->tail = (qinterface->tail+1)&(GE_QUEUE_MAX_MESSAGES-1);
ge_queue_update(qinterface);
bail:
return status;
}
const char *ge_rs232_text_token_lookup[256] = {
"0",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
[0x0c]="#",
":",
"/",
"?",
".",
"A",
"B",
"C",
"D",
"E",
"F",
"G",
"H",
"I",
"J",
"K",
"L",
"M",
"N",
"O",
"P",
"Q",
"R",
"S",
"T",
"U",
"V",
"W",
"X",
"Y",
"Z",
" ",
"'",
"-",
"_",
"*",
"AC POWER ",
"ACCESS ",
"ACCOUNT ",
"ALARM ",
"ALL ",
"ARM ",
"ARMING ",
"AREA ",
"ATTIC ",
"AUTO ",
"AUXILIARY ",
"AWAY ",
"BACK ",
"BATTERY ",
"BEDROOM ",
"BEEPS ",
"BOTTOM ",
"BREEZEWAY ",
"BASEMENT ",
"BATHROOM ",
"BUS ",
"BYPASS ",
"BYPASSED ",
"CABINET ",
"CANCELED ",
"CARPET ",
"CHIME ",
"CLOSET ",
"CLOSING ",
"CODE ",
"CONTROL ",
"CPU ",
"DEGREES ",
"DEN ",
"DESK ",
"DELAY ",
"DELETE ",
"DINING ",
"DIRECT ",
"DOOR ",
"DOWN ",
"DOWNLOAD ",
"DOWNSTAIRS ",
"DRAWER ",
"DISPLAY ",
"DURESS ",
"EAST ",
"ENERGY SAVER ",
"ENTER ",
"ENTRY ",
"ERROR ",
"EXIT ",
"FAIL ",
"FAILURE ",
"FAMILY ",
"FEATURES ",
"FIRE ",
"FIRST ",
"FLOOR ",
"FORCE ",
"FORMAT ",
"FREEZE ",
"FRONT ",
"FURNACE ",
"GARAGE ",
"GALLERY ",
"GOODBYE ",
"GROUP ",
"HALL ",
"HEAT ",
"HELLO ",
"HELP ",
"HIGH ",
"HOURLY ",
"HOUSE ",
"IMMEDIATE ",
"IN SERVICE ",
"INTERIOR ",
"INTRUSION ",
"INVALID ",
"IS ",
[0x81] = "KEY ",
[0x82] = "KITCHEN ",
[0x83] = "LAUNDRY ",
[0x84] = "LEARN ",
[0x85] = "LEFT ",
[0x86] = "LIBRARY ",
[0x87] = "LEVEL ",
[0x88] = "LIGHT ",
[0x89] = "LIGHTS ",
[0x8A] = "LIVING ",
[0x8B] = "LOW ",
[0x8C] = "MAIN ",
[0x8D] = "MASTER ",
[0x8E] = "MEDICAL",
[0x8F] = "MEMORY ",
[0x90] = "MIN ",
"MODE ",
"MOTION ",
"NIGHT ",
"NORTH ",
"NOT ",
"NUMBER ",
"OFF ",
"OFFICE ",
"OK ",
"ON ",
"OPEN ",
"OPENING ",
"PANIC ",
"PARTITION ",
"PATIO ",
"PHONE ",
"POLICE ",
"POOL ",
"PORCH ",
"PRESS ",
"QUIET ",
"QUICK ",
"RECEIVER ",
"REAR ",
"REPORT ",
"REMOTE ",
"RESTORE ",
"RIGHT ",
"ROOM ",
"SCHEDULE ",
"SCRIPT ",
"SEC ",
"SECOND ",
"SET ",
"SENSOR ",
"SHOCK ",
"SIDE ",
"SIREN ",
"SLIDING ",
"SMOKE ",
"Sn ",
"SOUND ",
"SOUTH ",
"SPECIAL ",
"STAIRS ",
"START ",
"STATUS ",
"STAY ",
"STOP ",
"SUPERVISORY ",
"SYSTEM ",
"TAMPER ",
"TEMPERATURE ",
"TEMPORARY ",
"TEST ",
"TIME ",
"TIMEOUT ",
"TOUCHPAD ",
"TRIP ",
"TROUBLE ",
"UNBYPASS ",
"UNIT ",
"UP ",
"VERIFY ",
"VIOLATION ",
"WARNING ",
"WEST ",
"WINDOW ",
"MENU ",
"RETURN ",
"POUND ",
"HOME ",
[0xF9]="\n", // Carriage Return
[0xFA]=" ", // "pseudo space", whatever the hell that means.
[0xFB]="\n", // Another Carriage Return?
[0xFD]="\b", // Backspace...?
[0xFE]="[!]", // Indicates that the next token should blink.
};
const char*
ge_text_to_ascii_one_line(const uint8_t * bytes, uint8_t len) {
static char ret[1024];
ret[0] = 0;
// TODO: Optimize!
while(len--) {
const char* str = ge_rs232_text_token_lookup[*bytes++];
if(str) {
if(str[0]=='\n') {
if(len)
strlcat(ret,isspace(ret[strlen(ret)-1])?"| ":" | ",sizeof(ret));
} else if(str[0]=='\b') {
// Backspace
if(ret[0])
ret[strlen(ret)-1] = 0;
} else {
strlcat(ret,str,sizeof(ret));
}
} else {
strlcat(ret,"?",sizeof(ret));
}
}
// Remove trailing whitespace.
for(len=strlen(ret);len && isspace(ret[len-1]);len--)
ret[len-1] = 0;
return ret;
}
const char*
ge_text_to_ascii(const uint8_t * bytes, uint8_t len) {
static char ret[1024];
ret[0] = 0;
// TODO: Optimize!
while(len--) {
const char* str = ge_rs232_text_token_lookup[*bytes++];
if(str) {
if(str[0]=='\b') {
// Backspace
if(ret[0])
ret[strlen(ret)-1] = 0;
} else {
strlcat(ret,str,sizeof(ret));
}
} else {
strlcat(ret,"?",sizeof(ret));
}
}
// Remove trailing whitespace.
for(len=strlen(ret);len && isspace(ret[len-1]);len--)
ret[len-1] = 0;
return ret;
}
const char* ge_user_to_cstr(char* dest, int user) {
static char static_string[48];
if (dest == NULL) {
dest = static_string;
}
if (user >= 230 && user <= 237) {
snprintf(dest, sizeof(static_string), "P%d-MASTER-CODE", user - 230);
} else if (user >= 238 && user <= 245) {
snprintf(dest, sizeof(static_string), "P%d-DURESS-CODE", user - 238);
} else if (user == 246) {
strlcpy(dest, "SYSTEM-CODE", sizeof(static_string));
} else if (user == 247) {
strlcpy(dest, "INSTALLER", sizeof(static_string));
} else if (user == 248) {
strlcpy(dest, "DEALER", sizeof(static_string));
} else if (user == 249) {
strlcpy(dest, "AVM-CODE", sizeof(static_string));
} else if (user == 250) {
strlcpy(dest, "QUICK-ARM", sizeof(static_string));
} else if (user == 251) {
strlcpy(dest, "KEY-SWITCH", sizeof(static_string));
} else if (user == 252) {
strlcpy(dest, "SYSTEM", sizeof(static_string));
} else if (user == 255) {
strlcpy(dest, "AUTOMATION", sizeof(static_string));
} else if (user == 65535) {
strlcpy(dest, "SYSTEM/KEY-SWITCH", sizeof(static_string));
} else {
snprintf(dest, sizeof(static_string), "USER-%d", user);
}
return dest;
}