-
Notifications
You must be signed in to change notification settings - Fork 0
/
abfr.c
767 lines (604 loc) · 15.5 KB
/
abfr.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
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
/*
* Copyright (c) 2009, 2012 Alexander Schrijver
* All rights reserved.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <time.h>
#include <sys/queue.h>
#include <gtk/gtk.h>
#include "glucosemeter.h"
#define DEBUG
#ifdef DEBUG
#define DPRINTF(x) do { printf x; } while(0)
#else
#define DPRINTF(x)
#endif
static void abfr_line_dev(struct abfr_dev *dev, char *line);
static void abfr_line_soft(struct abfr_dev *dev, char *line);
static void abfr_line_date(struct abfr_dev *dev, char *line);
static void abfr_line_nresults(struct abfr_dev *dev, char *line);
static void abfr_line_result(struct abfr_dev *dev, char *line);
static void abfr_line_end(struct abfr_dev *dev, char *line);
static void abfr_line_empty(struct abfr_dev *dev, char *line);
static void abfr_parseline(struct abfr_dev *dev, char *line);
static int abfr_parsetime(char *p, struct tm *r);
static enum abfr_devtype abfr_parsedev(char *type);
static enum abfr_softrev abfr_parsesoft(char *rev);
static int abfr_nentries(char *);
static int abfr_parse_entry(char *p, struct abfr_entry *entry);
static uint16_t abfr_calc_checksum(char *line);
static int abfr_parse_checksum(char *line, uint16_t *checksum);
static int dev_cmp(const void *k, const void *e);
static int rev_cmp(const void *k, const void *e);
static int month_cmp(const void *k, const void *e);
static gboolean abfr_in(struct device *dev, GIOChannel *gio);
static gboolean abfr_out(struct device *dev, GIOChannel *gio);
static gboolean abfr_error(struct device *dev, GIOChannel *gio);
int abfr_start(struct device *);
int abfr_stop(struct device *);
struct driver abfr_driver = {
"abfr",
abfr_start,
abfr_stop,
abfr_in,
abfr_out,
abfr_error,
};
struct devlist {
char *devicename;
enum abfr_devtype devicetype;
};
struct softlist {
char *softwarename;
enum abfr_softrev softwaretype;
};
struct monthlist {
char *month;
int number;
};
struct abfr_dev *
abfr_init(char *device_file)
{
struct abfr_dev *dev;
dev = calloc(1, sizeof(*dev));
if (dev == NULL) {
return NULL;
}
return dev;
}
static int
abfr_open(char *dev)
{
int fd;
struct termios ts;
fd = open(dev, O_RDWR | O_NONBLOCK | O_NOCTTY);
if (fd < 0) {
perror("open");
return fd;
}
tcgetattr(fd, &ts);
tcflush(fd, TCIFLUSH);
int r;
r = tcflush(fd, TCOFLUSH);
if (r == -1) {
perror("tcflush");
}
bzero(&ts, sizeof(ts));
ts.c_lflag = 0;
ts.c_cflag = B19200 | CS8 |CREAD | CLOCAL | CRTSCTS;
ts.c_cc[VTIME] = 5;
ts.c_cc[VMIN] = 5;
cfsetospeed(&ts, B19200);
tcsetattr(fd, TCSANOW, &ts);
return fd;
}
int
abfr_start(struct device *dev)
{
struct abfr_dev *abfr_dev = (struct abfr_dev *)dev;
int fd;
guint r;
fd = abfr_open(abfr_dev->file);
if (fd < 0) {
goto fail;
}
dev->channel = g_io_channel_unix_new(fd);
if (dev->channel == NULL) {
g_error("Cannnot create GIOChannel");
goto fail;
}
r = g_io_add_watch(dev->channel, G_IO_IN | G_IO_HUP, devicemgmt_input, dev);
if (!r) {
g_error("Cannnot watch GIOChannel");
goto fail;
}
r = g_io_add_watch(dev->channel, G_IO_OUT | G_IO_HUP, devicemgmt_output, dev);
if (!r) {
g_error("Cannnot watch GIOChannel");
goto fail;
}
r = g_io_add_watch(dev->channel, G_IO_ERR | G_IO_HUP, devicemgmt_error, dev);
if (!r) {
g_error("Cannnot watch GIOChannel");
goto fail;
}
return 1;
fail:
/* XXX: cleanup fd/channels */
return (-1);
}
int
abfr_stop(struct device *dev)
{
/* XXX: close fd/channels */
return 1;
}
static int
dev_cmp(const void *k, const void *e)
{
return (strcmp(k, ((const struct devlist *)e)->devicename));
}
static enum abfr_devtype
abfr_parsedev(char *type)
{
const struct devlist *p;
/* keep sorted */
struct devlist abfr_typelist[] = {
{ "CDMK311-B0764", ABFR_DEV_CDMK311_B0764 }, // FreeStyle Freedom Lite
{ "DAMH359-63524", ABFR_DEV_DAMH359_63524 }, // FreeStyle Mini
{ "DBMN169-C4824", ABFR_DEV_DBMN169_C4824 } // FreeStyle Lite
};
p = bsearch(type, abfr_typelist, sizeof(abfr_typelist)/sizeof(abfr_typelist[0]),
sizeof(abfr_typelist[0]), dev_cmp);
if (p)
return p->devicetype;
return ABFR_DEV_UNKNOWN;
}
static int
rev_cmp(const void *k, const void *e)
{
return (strcmp(k, ((const struct softlist *)e)->softwarename));
}
static enum abfr_softrev
abfr_parsesoft(char *rev)
{
const struct softlist *p;
/* keep sorted */
struct softlist abfr_revlist[] = {
{ "0.31-P",ABFR_SOFT_0_31_P}, // On my FreeStyle Freedom Lite (The missing spaces isn't a mistake)
{ "0.31-P1-B0764", ABFR_SOFT_0_31_P1_B0764 },
{ "1.43 -P", ABFR_SOFT_1_43_P}, // On my FreeStyle Lite
{ "4.0100 -P", ABFR_SOFT_4_0100_P}, // On my FreeStyle Mini
};
p = bsearch(rev, abfr_revlist, sizeof(abfr_revlist)/sizeof(abfr_revlist[0]),
sizeof(abfr_revlist[0]), rev_cmp);
if (p)
return p->softwaretype;
return ABFR_SOFT_UNKNOWN;
}
static int
month_cmp(const void *k, const void *e)
{
return (strcmp(k, ((const struct monthlist *)e)->month));
}
static int
abfr_nentries(char *p)
{
int r;
const char *errstr;
errstr = NULL;
/* XXX: change upper boundary to some kind of constant */
r = strtonum(p, 1, 450, &errstr);
if (errstr)
return (-1);
return (r);
}
// 234 Jan 17 2010 00:39 00 0x00
static int
abfr_parse_entry(char *p, struct abfr_entry *entry)
{
char *p2;
const char *errstr;
struct monthlist *m;
int yearint;
/* Keep sorted */
struct monthlist mlist[] = {
{ "Apr", 3 },
{ "Aug", 7 },
{ "Dec", 11 },
{ "Feb", 1 },
{ "Jan", 0 },
{ "July", 6 },
{ "June", 5 },
{ "Mar", 2 },
{ "May", 4 },
{ "Nov", 10 },
{ "Oct", 9 },
{ "Sep", 8 },
};
p2 = p;
p = strsep(&p2, " ");
if (p2 == NULL)
goto fail;
errstr = NULL; /* XXX: only necessary one time */
entry->bloodglucose = strtonum(p, 0, 400, &errstr); /* XXX: change 400 to sth decent */
if (errstr)
goto fail;
/* Skip leading space */
if (*p2 == ' ')
p2++;
p = strsep(&p2, " ");
if (p2 == NULL)
goto fail;
m = bsearch(p, mlist, sizeof(mlist)/sizeof(mlist[0]),
sizeof(mlist[0]), month_cmp);
if (!m)
goto fail;
entry->ptm.tm_mon = m->number;
/* Skip leading space */
if (*p2 == ' ')
p2++;
p = strsep(&p2, " ");
if (p2 == NULL)
goto fail;
errstr = NULL;
entry->ptm.tm_mday = strtonum(p, 1, 31, &errstr);
if (errstr)
goto fail;
p = strsep(&p2, " ");
if (p2 == NULL)
goto fail;
errstr = NULL;
yearint = strtonum(p, 0, 9999, &errstr);
if (errstr)
goto fail;
entry->ptm.tm_year = yearint - 1900;
p = strsep(&p2, ":");
if (p2 == NULL)
goto fail;
errstr = NULL; /* XXX: only necessary one time */
entry->ptm.tm_hour = strtonum(p, 0, 23, &errstr);
if (errstr)
goto fail;
p = strsep(&p2, " ");
if (p2 == NULL)
goto fail;
errstr = NULL; /* XXX: only necessary one time */
entry->ptm.tm_min = strtonum(p, 0, 59, &errstr);
if (errstr)
goto fail;
return (0);
fail:
return (-1);
}
/* Jan 21 2010 20:40:00 */
static int
abfr_parsetime(char *p, struct tm *r)
{
int yearint;
struct monthlist *m;
const char *errstr;
char *p2;
/* Keep sorted */
struct monthlist mlist[] = {
{ "Apr", 3 },
{ "Aug", 7 },
{ "Dec", 11 },
{ "Feb", 1 },
{ "Jan", 0 },
{ "July", 6 },
{ "June", 5 },
{ "Mar", 2 },
{ "May", 4 },
{ "Nov", 10 },
{ "Oct", 9 },
{ "Sep", 8 },
};
p2 = p;
p = strsep(&p2, " ");
if (p2 == NULL)
goto fail;
m = bsearch(p, mlist, sizeof(mlist)/sizeof(mlist[0]),
sizeof(mlist[0]), month_cmp);
if (!m)
goto fail;
r->tm_mon = m->number;
/* Skip leading space */
if (*p2 == ' ')
p2++;
p = strsep(&p2, " ");
if (p2 == NULL)
goto fail;
errstr = NULL;
r->tm_mday = strtonum(p, 1, 31, &errstr);
if (errstr)
goto fail;
p = strsep(&p2, " ");
if (p2 == NULL)
goto fail;
errstr = NULL;
yearint = strtonum(p, 0, 9999, &errstr); /* XXX: 3000 should be correspond to what can be stored in tm_year (minus 1900) */
if (errstr)
goto fail;
p = strsep(&p2, ":");
if (p2 == NULL)
goto fail;
errstr = NULL; /* XXX: only necessary one time */
r->tm_hour = strtonum(p, 0, 23, &errstr);
if (errstr)
goto fail;
p = strsep(&p2, ":");
if (p2 == NULL)
goto fail;
errstr = NULL; /* XXX: only necessary one time */
r->tm_min = strtonum(p, 0, 59, &errstr);
if (errstr)
goto fail;
errstr = NULL; /* XXX: only necessary one time */
r->tm_sec = strtonum(p2, 0, 59, &errstr);
if (errstr)
goto fail;
return (0);
fail:
printf("fail!\n");
return (-1);
}
static uint16_t
abfr_calc_checksum(char *line)
{
int i;
uint16_t checksum = 0;
for (i = 0; line[i] != '\0'; i++)
checksum += line[i];
return checksum;
}
static int
abfr_parse_checksum(char *line, uint16_t *checksum)
{
char *end, *strchecksum, *endptr;
unsigned long ul;
end = line;
strchecksum = strsep(&end, " ");
if (end == NULL)
return -1;
if (*end == ' ')
end++;
if (strcmp(end, "END") != 0)
return -1;
ul = strtoul(strchecksum, &endptr, 16);
if (strchecksum[0] == '\0' || *endptr != '\0')
return -1;
if (errno == ERANGE && ul == ULONG_MAX)
return -1;
*checksum = ul;
return 0;
}
static void
abfr_parseline(struct abfr_dev *dev, char *line)
{
int old_state = dev->protocol_state;
switch(dev->protocol_state) {
case ABFR_DEVICE_TYPE:
abfr_line_dev(dev, line);
break;
case ABFR_SOFTWARE_REVISION:
abfr_line_soft(dev, line);
break;
case ABFR_CURRENTDATETIME:
abfr_line_date(dev, line);
break;
case ABFR_NUMBEROFRESULTS:
abfr_line_nresults(dev, line);
break;
case ABFR_RESULTLINE:
abfr_line_result(dev, line);
break;
case ABFR_END:
abfr_line_end(dev, line);
break;
case ABFR_EMPTY:
abfr_line_empty(dev, line);
break;
default:
/* XXX: this shouldn't happen */
break;
}
DPRINTF(("%s: state: %d -> %d\n", __func__, old_state, dev->protocol_state));
}
static void
abfr_line_dev(struct abfr_dev *dev, char *line)
{
enum abfr_devtype device_type;
device_type = abfr_parsedev(line);
DPRINTF(("%s: device_type: %d\n", __func__, device_type));
/* Don't continue parsing if the device type isn't known. */
if (device_type == ABFR_DEV_UNKNOWN)
dev->protocol_state = ABFR_FAIL;
else
dev->protocol_state++;
}
static void
abfr_line_soft(struct abfr_dev *dev, char *line)
{
enum abfr_softrev softrev;
softrev = abfr_parsesoft(line);
DPRINTF(("%s: softrev: %d\n", __func__, softrev));
/* Don't continue parsing if the software revision isn't known. */
if (softrev == ABFR_SOFT_UNKNOWN)
dev->protocol_state = ABFR_FAIL;
else
dev->protocol_state++;
}
static void
abfr_line_date(struct abfr_dev *dev, char *line)
{
struct tm device_tm;
int r;
r = abfr_parsetime(line, &device_tm);
if (r == -1) {
dev->protocol_state = ABFR_FAIL;
return;
}
DPRINTF(("%s: currentdatetime\n", __func__));
dev->protocol_state++;
return;
}
static void
abfr_line_nresults(struct abfr_dev *dev, char *line)
{
int nresults;
nresults = abfr_nentries(line);
if (nresults == -1)
dev->protocol_state = ABFR_FAIL;
else
dev->protocol_state++;
dev->nresults = nresults;
DPRINTF(("%s: numberofresults\n", __func__));
return;
}
static void
abfr_line_result(struct abfr_dev *dev, char *line)
{
int r;
struct abfr_entry *entry;
entry = calloc(1, sizeof(*entry));
if (entry == NULL) {
/* XXX: change to a fail state */
return;
}
r = abfr_parse_entry(line, entry);
/* We can't insert the entry into the database at this point because
* the checksum is calculated over all the messages thus we aren't sure
* yet if this entry is correct. Instead put all the entries in a
* linked list and insert them when the checksum can been verified. */
SLIST_INSERT_HEAD(&dev->entries, entry, next);
DPRINTF(("%s: glucose: %d\n", __func__, entry->bloodglucose));
DPRINTF(("%s: month: %d\n", __func__, entry->ptm.tm_mon));
DPRINTF(("%s: day: %d\n", __func__, entry->ptm.tm_mday));
DPRINTF(("%s: year: %d\n", __func__, entry->ptm.tm_year));
DPRINTF(("%s: hour: %d\n", __func__, entry->ptm.tm_hour));
DPRINTF(("%s: min: %d\n", __func__, entry->ptm.tm_min));
dev->results_processed++;
if (dev->results_processed >= dev->nresults)
dev->protocol_state = ABFR_END;
DPRINTF(("%s: result\n", __func__));
}
static void
abfr_line_end(struct abfr_dev *dev, char *line)
{
uint16_t checksum;
int r;
r = abfr_parse_checksum(line, &checksum);
if (r == -1) {
dev->protocol_state = ABFR_FAIL;
return;
}
if (dev->checksum == checksum) {
struct abfr_entry *e;
/* We are as sure as we can get that the entries are correct.
* Insert them into the database */
while (!SLIST_EMPTY(&dev->entries)) {
e = SLIST_FIRST(&dev->entries);
SLIST_REMOVE_HEAD(&dev->entries, next);
/*
meas_insert(dev->gm_state,
e->bloodglucose,
asctime(&e->ptm), "abfr");
*/
free(e);
}
DPRINTF(("%s: checksum verified!\n", __func__));
dev->protocol_state = ABFR_DONE;
return;
}
dev->protocol_state = ABFR_FAIL;
}
static void
abfr_line_empty(struct abfr_dev *dev, char *line)
{
}
static gboolean
abfr_in(struct device *dev, GIOChannel *gio)
{
GIOStatus status;
gsize terminator_pos;
GError *error = NULL;
struct abfr_dev *abfr_dev = (struct abfr_dev *)dev;
GString *line;
if (!dev->is_processing) {
DPRINTF(("%s: the device isn't processing events. This "
"should've never been hit\n", __func__));
return FALSE;
}
line = g_string_sized_new(100);
status = g_io_channel_read_line_string(gio, line, &terminator_pos, &error);
if (status == G_IO_STATUS_ERROR) {
DPRINTF(("%s: error occured\n", __func__));
dev->is_processing = 0;
return FALSE;
}
if (status == G_IO_STATUS_NORMAL) {
/* Calculate the checksum before the newline terminators are cut off */
if (abfr_dev->protocol_state != ABFR_END)
abfr_dev->checksum += abfr_calc_checksum(line->str);
/* Cut off the newline terminators */
line = g_string_truncate(line, terminator_pos);
DPRINTF(("%s: line(%zu): \"%s\"\n", __func__, line->len, line->str));
if (line->len > 0)
abfr_parseline(abfr_dev, line->str);
}
g_string_free(line, TRUE);
if (status == G_IO_STATUS_EOF || abfr_dev->protocol_state == ABFR_DONE ||
abfr_dev->protocol_state == ABFR_FAIL) {
dev->is_processing = 0;
return FALSE;
}
return TRUE;
}
static gboolean
abfr_out(struct device *dev, GIOChannel *gio)
{
gchar buf[] = "mem";
gsize wrote_len;
GError *error = NULL;
GIOStatus status;
struct abfr_dev *abfr_dev = (struct abfr_dev *)dev;
if (!dev->is_processing) {
DPRINTF(("%s: the device isn't processing events. This "
"should've never been hit\n", __func__));
return FALSE;
}
if (abfr_dev->protocol_state != ABFR_SEND_MEM) {
return FALSE;
}
status = g_io_channel_write_chars(gio, buf, -1, &wrote_len, &error);
g_io_channel_flush(gio, NULL);
DPRINTF(("%s: bytes written: %zu status: %d\n", __func__, wrote_len, status));
abfr_dev->protocol_state++;
return TRUE;
}
static gboolean
abfr_error(struct device *dev, GIOChannel *gio)
{
printf("error\n");
return TRUE;
}