/
fitbit.c
1689 lines (1413 loc) · 32.7 KB
/
fitbit.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
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <memory.h>
#include <fcntl.h>
#include <stdarg.h>
#include <signal.h>
#include <libusb.h>
double get_secs (void);
unsigned char bank0ref[] = {
0x49, 0x5c, 0x07, 0xbc, 0x81, 0x1a, 0x00, 0x49,
0x5c, 0x0a, 0xc8, 0x85, 0x1a, 0x00, 0x49, 0x5e,
0xd1, 0xa4, 0x85, 0x2e, 0x2c, 0x85, 0x36, 0x39,
0x86, 0x2a, 0x1e, 0x87, 0x1e, 0x13, 0x49, 0x5f,
0x84, 0xf0, 0x85, 0x0a, 0x00, 0x85, 0x18, 0x00,
0x49, 0x5f, 0xc7, 0xf8, 0x85, 0x0a, 0x00, 0x85,
0x1a, 0x00
};
unsigned char bank1ref[] = {
0x00, 0x59, 0x5d, 0x49, 0x60, 0x38, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x80, 0xaa, 0x5e, 0x49, 0x40, 0x38, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x20, 0xd4, 0x5f, 0x49, 0x24, 0x33, 0x5b, 0x01,
0x00, 0x00, 0x2a, 0x8c, 0x03, 0x00, 0x14, 0x00,
0x36, 0xd4, 0x5f, 0x49, 0x2e, 0x33, 0x5b, 0x01,
0x00, 0x00, 0x2a, 0x8c, 0x03, 0x00, 0x14, 0x00,
0x5e, 0xd4, 0x5f, 0x49, 0x2e, 0x33, 0x5b, 0x01,
0x00, 0x00, 0x2a, 0x8c, 0x03, 0x00, 0x14, 0x00
};
int current_packet_id;
void print_bank2 (unsigned char *rbuf, int n);
void dbg (char const *fmt, ...) __attribute__ ((format (printf, 1, 2)));
void vdbg (char const *fmt, va_list args);
int xflag;
int vflag;
void dump_msg (unsigned char *msg, int msglen);
int getrandom (void);
int device_number, pairing_request, device_type, transmission_type;
void reset_tracker (void);
void ping_tracker (void);
void get_tracker_info (void);
int run_opcode (unsigned char *op, int oplen,
unsigned char *payload, int plen,
unsigned char *retbuf, int retlen);
void send_tracker_packet (unsigned char *buf, int len);
int get_data_bank (unsigned char *retbuf, int retlen);
int check_tracker_data_bank (int bank_id, int cmd, unsigned char *buf, int len);
int get_tracker_burst (unsigned char *retbuf, int retlen);
int run_data_bank_opcode (int idx, unsigned char *rbuf, int rbufsize);
int receive_message (unsigned char *msg, int avail, int timeout);
void soak (double duration);
void fitbit_ant_init (void);
void init_tracker_for_transfer (void);
void init_fitbit (void);
void init_device_channel (int device_number,
int pairing_request, int device_type,
int transmission_type);
void send_message (unsigned char *data, int n);
void protocol_reset (void);
void protocol_set_network_key (int network, unsigned char *key, int keylen);
void protocol_assign_channel (int channel_type, int network_number);
void protocol_set_channel_period (double secs);
void protocol_set_channel_frequency (int channel_freq);
void protocol_set_transmit_power (int power);
void protocol_set_search_timeout (double secs);
void protocol_set_channel_id (int device_number, int pairing_request,
int device_type, int transmission_type);
void protocol_open_channel (void);
int protocol_send_acknowledged_data (unsigned char *msg, int len);
int protocol_check_tx_response (void);
void protocol_close_channel (void);
int protocol_receive_acknowledged_reply (unsigned char *rbuf, int rbuflen);
int protocol_send_acknowledged_data (unsigned char *msg, int len);
int protocol_check_burst_response (unsigned char *retbuf, int retlen);
void check_ok_response (int op);
void wait_for_beacon (void);
int protocol_channel; /* self._chan */
void
dump (void *buf, int n)
{
int i;
int j;
int c;
for (i = 0; i < n; i += 16) {
printf ("%04x: ", i);
for (j = 0; j < 16; j++) {
if (i+j < n)
printf ("%02x ", ((unsigned char *)buf)[i+j]);
else
printf (" ");
}
printf (" ");
for (j = 0; j < 16; j++) {
c = ((unsigned char *)buf)[i+j] & 0x7f;
if (i+j >= n)
putchar (' ');
else if (c < ' ' || c == 0x7f)
putchar ('.');
else
putchar (c);
}
printf ("\n");
}
}
void try_requests (void);
void
usage (void)
{
fprintf (stderr, "usage: fitbit\n");
exit (1);
}
libusb_context *ctx;
libusb_device *device;
struct libusb_device_descriptor desc;
libusb_device_handle *dev;
struct libusb_device_descriptor desc;
char *
errstr (int val)
{
switch (val) {
case LIBUSB_SUCCESS: return ("(no error)");
case LIBUSB_ERROR_IO: return ("LIBUSB_ERROR_IO");
case LIBUSB_ERROR_INVALID_PARAM: return ("LIBUSB_ERROR_INVALID_PARAM");
case LIBUSB_ERROR_ACCESS: return ("LIBUSB_ERROR_ACCESS");
case LIBUSB_ERROR_NO_DEVICE: return ("LIBUSB_ERROR_NO_DEVICE");
case LIBUSB_ERROR_NOT_FOUND: return ("LIBUSB_ERROR_NOT_FOUND");
case LIBUSB_ERROR_BUSY: return ("LIBUSB_ERROR_BUSY");
case LIBUSB_ERROR_TIMEOUT: return ("LIBUSB_ERROR_TIMEOUT");
case LIBUSB_ERROR_OVERFLOW: return ("LIBUSB_ERROR_OVERFLOW");
case LIBUSB_ERROR_PIPE: return ("LIBUSB_ERROR_PIPE");
case LIBUSB_ERROR_INTERRUPTED: return ("LIBUSB_ERROR_INTERRUPTED");
case LIBUSB_ERROR_NO_MEM: return ("LIBUSB_ERROR_NO_MEM");
case LIBUSB_ERROR_NOT_SUPPORTED: return ("LIBUSB_ERROR_NOT_SUPPORTED");
case LIBUSB_ERROR_OTHER: return ("LIBUSB_ERROR_OTHER");
default: return ("unknown error");
}
}
void
die (char *str, int rc)
{
fprintf (stderr, "die %s %d: %s\n", str, rc, errstr (rc));
exit (1);
}
int intarg;
void
print_bank0 (unsigned char *data, int len)
{
int i;
time_t last_date_time, record_date;
int time_index;
struct tm tm;
int steps, not_sure;
double active_score;
i = 0;
last_date_time = 0;
time_index = 0;
while (i < len) {
if ((data[i] & 0x80) == 0) {
last_date_time = data[i+3]
| (data[i+2]<<8)
| (data[i+1]<<16)
| (data[i]<<24);
tm = *localtime (&last_date_time);
printf ("Time: %d-%02d-%02d %02d:%02d:%02d\n",
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
tm.tm_hour, tm.tm_min, tm.tm_sec);
i += 4;
time_index = 0;
} else {
record_date = last_date_time + 60 * time_index;
tm = *localtime (&record_date);
steps = data[i+2];
active_score = (data[i+1] - 10) / 10.0;
not_sure = data[i] - 0x81;
printf ("%04d-%02d-%02d %02d:%02d:%02d ",
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
tm.tm_hour, tm.tm_min, tm.tm_sec);
printf ("unk %d; active %g; steps %d\n",
not_sure, active_score, steps);
i += 3;
time_index++;
}
}
}
void
print_bank1 (unsigned char *data, int len)
{
int off;
time_t t;
unsigned char *up;
struct tm tm;
int i;
int steps;
dump (data, len);
off = 0;
while (off + 16 < len) {
up = data + off;
t = up[0] | (up[1] << 8) | (up[2] << 16) | (up[3] << 24);
up += 4;
tm = *localtime (&t);
printf ("%04d-%02d-%02d %02d:%02d:%02d ",
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
tm.tm_hour, tm.tm_min, tm.tm_sec);
printf (" %02x", *up++);
printf (" %02x", *up++);
steps = up[0] | (up[1] << 8) | (up[2] << 16) | (up[3] << 24);
up += 4;
printf (" [%d steps] ", steps);
for (i = 0; i < 6; i++)
printf (" %02x", *up++);
printf ("\n");
off += 16;
}
}
void
print_bank4 (unsigned char *data, int len)
{
/* strings for front panel display */
dump (data, len);
}
FILE *outf;
void
output_bank (int bank, unsigned char *data, int len)
{
int i;
fprintf (outf, "bank%d\n", bank);
for (i = 0; i < len; i++) {
fprintf (outf, "%02x", data[i]);
if ((i % 32) == 31)
fprintf (outf, "\n");
}
if (i % 32)
fprintf (outf, "\n");
fprintf (outf, "\n");
}
void
intr (int sig)
{
dbg ("alarm timeout\n");
exit (1);
}
int
main (int argc, char **argv)
{
int c;
int rc;
unsigned char rbuf[10000];
int n;
char filename[1000];
time_t t;
struct tm tm;
char cmd[1000];
if (0) {
print_bank1 (bank1ref, sizeof bank1ref);
exit (0);
}
while ((c = getopt (argc, argv, "xv")) != EOF) {
switch (c) {
case 'v':
vflag = 1;
break;
case 'x':
xflag = 1;
break;
default:
usage ();
}
}
if (optind < argc)
intarg = atoi (argv[optind++]);
if (optind != argc)
usage ();
signal (SIGALRM, intr);
alarm (14 * 60);
t = time (NULL);
tm = *localtime (&t);
sprintf (filename, "fitbit-log-%04d-%02d-%02d-%02d%02d%02d",
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
tm.tm_hour, tm.tm_min, tm.tm_sec);
if (libusb_init (&ctx) != 0) {
fprintf (stderr, "libusb_init error\n");
exit (1);
}
libusb_set_debug (ctx, 3);
dev = libusb_open_device_with_vid_pid (ctx, 0x10c4, 0x84c4);
device = libusb_get_device (dev);
if ((rc = libusb_get_device_descriptor (device, &desc)) != 0)
die ("get_desc", rc);
if (0) {
printf ("bLength = %d\n", desc.bLength);
printf ("bDescriptorType = %d\n", desc.bDescriptorType);
/* 0x110 = USB 1.1 */
printf ("bcdUSB = 0x%x\n", desc.bcdUSB);
/* class = 0 means class per interface */
printf ("bDeviceClass = %d\n", desc.bDeviceClass);
printf ("bDeviceSubClass = %d\n", desc.bDeviceSubClass);
printf ("bDeviceProtocol = %d\n", desc.bDeviceProtocol);
printf ("bMaxPacketSize0 = %d\n", desc.bMaxPacketSize0);
printf ("idVendor = 0x%x\n", desc.idVendor);
printf ("idProduct = 0x%x\n", desc.idProduct);
printf ("bcdDevice = 0x%x\n", desc.bcdDevice);
printf ("iManufacturer = %d\n", desc.iManufacturer);
printf ("iProduct = %d\n", desc.iProduct);
printf ("iSerialNumber = %d\n", desc.iSerialNumber);
printf ("bNumConfigurations = %d\n", desc.bNumConfigurations);
}
if ((rc = libusb_set_configuration (dev, -1)) != 0)
die ("set_config", rc);
if ((rc = libusb_set_configuration (dev, 1)) != 0)
die ("set_config", rc);
if ((rc = libusb_claim_interface (dev, 0)) != 0)
die ("claim", rc);
// try_requests ();
dbg ("*** fitbit_ant_init\n");
fitbit_ant_init ();
init_tracker_for_transfer ();
get_tracker_info ();
if ((outf = fopen (filename, "w")) == NULL) {
fprintf (stderr, "can't create %s\n", filename);
exit (1);
}
fprintf (outf, "%s\n", filename);
n = run_data_bank_opcode (0, rbuf, sizeof rbuf);
print_bank0 (rbuf, n);
output_bank (0, rbuf, n);
n = run_data_bank_opcode (1, rbuf, sizeof rbuf);
print_bank1 (rbuf, n);
output_bank (1, rbuf, n);
n = run_data_bank_opcode (2, rbuf, sizeof rbuf);
print_bank2 (rbuf, n);
output_bank (2, rbuf, n);
dbg ("resetting\n");
if ((rc = libusb_reset_device (dev)) != 0)
die ("libusb_reset_device", rc);
fclose (outf);
sprintf (cmd, "./parsedata %s", filename);
dbg ("running: %s\n", cmd);
rc = system (cmd);
if (rc)
dbg ("error: 0x%x\n", rc);
dbg ("ok\n");
return (0);
}
void
print_bank2 (unsigned char *rbuf, int n)
{
int off;
unsigned char *up;
time_t start;
time_t t;
struct tm tm;
int i;
int val;
printf ("bank2\n");
off = 0;
while (off + 15 <= n) {
up = rbuf + off;
t = up[0] | (up[1] << 8) | (up[2] << 16) | (up[3] << 24);
if (off == 0)
start = t;
up += 4;
tm = *localtime (&t);
printf ("%04d-%02d-%02d %02d:%02d:%02d (%8.3f) ",
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
tm.tm_hour, tm.tm_min, tm.tm_sec,
(t - start) / 3600.0);
for (i = 0; i < 11; i++) {
val = *up++;
if (val == 0)
printf (" %2s", ".");
else
printf (" %02x", val);
}
printf ("\n");
off += 15;
}
if (off != n)
printf ("dregs %d\n", n - off);
}
int
control_read (int request, int val, int idx, void *data, int datalen)
{
int request_type;
int n;
request_type = LIBUSB_ENDPOINT_IN
| LIBUSB_REQUEST_TYPE_VENDOR;
n = libusb_control_transfer (dev, request_type,
request, val, idx,
data, datalen,
100);
return (n);
}
int
control_write (int request, int val, int idx, void *data, int datalen)
{
int request_type;
int n;
request_type = LIBUSB_ENDPOINT_OUT
| LIBUSB_REQUEST_TYPE_VENDOR;
n = libusb_control_transfer (dev, request_type,
request, val, idx,
data, datalen,
100);
if (n != datalen)
dbg ("control_write: wrote %d, ret %d\n", datalen, n);
return (n);
}
void
try_requests ()
{
int request, val, idx;
unsigned char data[128];
int timeout;
int n;
for (request = 0; request < 256; request++) {
val = 0;
idx = 0;
timeout = 100;
n = control_read (request, val, idx, data, sizeof data);
if (n >= 0) {
dbg ("request 0x%x got %d\n", request, n);
dump (data, n);
}
}
}
int
bulk_read (unsigned char *buf, int len, int timeout)
{
int endpoint, transferred;
int rc;
/* wMaxPacketSize from lsusb -vv */
if (len > 64)
len = 64;
endpoint = LIBUSB_ENDPOINT_IN | 1;
transferred = 0;
rc = libusb_bulk_transfer (dev, endpoint, buf, len,
&transferred, timeout);
if (rc < 0)
return (rc);
return (transferred);
}
int
bulk_write (unsigned char *buf, int len)
{
int endpoint, transferred, timeout;
int rc;
/* wMaxPacketSize from lsusb -vv */
if (len > 64)
len = 64;
endpoint = LIBUSB_ENDPOINT_OUT | 1;
transferred = 0;
timeout = 100;
rc = libusb_bulk_transfer (dev, endpoint, buf, len,
&transferred, timeout);
if (rc < 0) {
dbg ("bulk write error: %d %s\n", rc, errstr (rc));
exit (1);
}
if (transferred != len) {
dbg ("bulk write: wanted %d only transferred %d\n",
len, transferred);
exit (1);
}
return (transferred);
}
/* from bases.py FitBitAnt.init */
void
fitbit_ant_init (void)
{
int n;
unsigned char buf[128];
control_write (0x00, 0xFFFF, 0x0, NULL, 0);
control_write (0x01, 0x2000, 0x0, NULL, 0);
/*
* At this point, we get a 4096 buffer, then start all over
* again? Apparently doesn't require an explicit receive
*/
control_write (0x00, 0x0, 0x0, NULL, 0);
control_write (0x00, 0xFFFF, 0x0, NULL, 0);
control_write (0x01, 0x2000, 0x0, NULL, 0);
control_write (0x01, 0x4A, 0x0, NULL, 0);
/* Receive 1 byte, should be 0x2 */
n = control_read (0xff, 0x370b, 0, buf, sizeof buf);
if (n < 1) {
dbg ("fitbit_ant_init: first read %d %s\n", n, errstr (n));
exit (1);
}
if (buf[0] != 2) {
dbg ("fitbit_ant_init: first read expected 2 got 0x%x\n",
buf[0]);
exit (1);
}
control_write (0x03, 0x800, 0x0, NULL, 0);
memset (buf, 0, sizeof buf);
buf[0] = 0x08;
buf[4] = 0x40;
control_write (0x13, 0x0, 0x0, buf, 16);
control_write (0x12, 0x0C, 0x0, NULL, 0);
dbg ("initial soak...\n");
soak (0.010);
dbg ("done\n");
}
void
init_tracker_for_transfer (void)
{
unsigned char data[100];
int n;
protocol_channel = 0;
init_fitbit ();
wait_for_beacon ();
reset_tracker ();
while (1) {
device_number = getrandom () & 0xffff;
if (device_number != 0 && device_number != 0xffff)
break;
}
pairing_request = 0;
device_type = 1;
transmission_type = 1;
dbg ("init_tracker_for_transfer: tell device to change dev num\n");
n = 0;
data[n++] = 0x78;
data[n++] = 0x02;
data[n++] = device_number;
data[n++] = device_number >> 8;
data[n++] = 0x00;
data[n++] = 0x00;
data[n++] = 0x00;
data[n++] = 0x00;
protocol_send_acknowledged_data (data, n);
protocol_close_channel ();
/*
* we're supposed to wait for EVENT_CHANNEL_CLOSED but it never
* seems to come
*/
soak (.2);
dbg ("hoping channel close has happened...\n");
init_device_channel (device_number,
pairing_request, device_type,
transmission_type);
wait_for_beacon ();
ping_tracker ();
}
void
reset_tracker (void)
{
unsigned char data[]
= { 0x78, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
dbg ("reset_tracker\n");
protocol_send_acknowledged_data (data, sizeof data);
}
void
ping_tracker (void)
{
unsigned char data[]
= { 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
dbg ("ping_tracker\n");
protocol_send_acknowledged_data (data, sizeof data);
}
void
get_tracker_info (void)
{
unsigned char data[]
= { 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
unsigned char buf[1000];
int n;
dbg ("\n");
dbg ("\n");
dbg ("get_tracker_info\n");
n = run_opcode (data, sizeof data,
NULL, 0,
buf, sizeof buf);
dbg ("tracker info %d\n", n);
dump (buf, n);
dbg ("serial %02x:%02x:%02x:%02x:%02x\n",
buf[0], buf[1], buf[2], buf[3], buf[4]);
dbg ("firmware version %d (0x%x)\n", buf[5], buf[5]);
dbg ("bsl major %d (0x%x)\n", buf[6], buf[6]);
dbg ("bsl minor %d (0x%x)\n", buf[7], buf[7]);
dbg ("app major %d (0x%x)\n", buf[8], buf[8]);
dbg ("app minor %d (0x%x)\n", buf[9], buf[9]);
dbg ("in_mode_bsl %d (0x%x)\n", buf[10], buf[10]);
dbg ("on_charger %d (0x%x)\n", buf[11], buf[11]);
}
int
run_data_bank_opcode (int idx, unsigned char *rbuf, int rbufsize)
{
unsigned char data[100];
int n;
dbg ("run_data_bank_opcode %d\n", idx);
n = 0;
data[n++] = 0x22;
data[n++] = idx;
data[n++] = 0x00;
data[n++] = 0x00;
data[n++] = 0x00;
data[n++] = 0x00;
data[n++] = 0x00;
n = run_opcode (data, n,
NULL, 0,
rbuf, rbufsize);
return (n);
}
int
run_opcode (unsigned char *op, int oplen,
unsigned char *payload, int plen,
unsigned char *retbuf, int retlen)
{
int try;
unsigned char rbuf[100];
int n;
char logbuf[1000], *logp;
int i;
logp = logbuf;
logp += sprintf (logp, "run_opcode");
for (i = 0; i < oplen; i++)
logp += sprintf (logp, " %02x", op[i]);
dbg ("%s\n", logbuf);
try = 0;
while (1) {
try++;
if (try > 4) {
dbg ("run_opcode failed\n");
exit (1);
}
send_tracker_packet (op, oplen);
n = protocol_receive_acknowledged_reply (rbuf, sizeof rbuf);
if (n > 0) {
if ((rbuf[0] & 0x07) != (current_packet_id & 0x07))
continue;
switch (rbuf[1]) {
case 0x42:
return (get_data_bank (retbuf, retlen));
break;
case 0x61:
case 0x41:
dbg ("can't handle run_opcode response\n");
exit (1);
default:
dbg ("ignore respose 0x%x\n", rbuf[1]);
break;
}
}
}
}
int
get_data_bank (unsigned char *retbuf, int retlen)
{
int used, togo, cmd;
int bank_id, n;
dbg ("get_data_bank\n");
used = 0;
togo = retlen;
cmd = 0x70;
for (bank_id = 0; bank_id < 1; bank_id++) {
dbg( "get_data_bank %d\n", bank_id);
cmd = (bank_id == 0) ? 0x70 : 0x60;
n = check_tracker_data_bank (bank_id, cmd, retbuf + used, togo);
dbg ("got %d\n", n);
if (n < 0) {
dbg ("check_tracker_data_bank got error\n");
exit (1);
}
if (n == 0)
break;
used += n;
togo -= n;
}
dbg ("data bank size %d\n", used);
return (used);
}
int
check_tracker_data_bank (int bank_id, int cmd,
unsigned char *retbuf, int retlen)
{
unsigned char data[100];
int n;
n = 0;
data[n++] = cmd;
data[n++] = 0x00;
data[n++] = 0x02;
data[n++] = bank_id;
data[n++] = 0x00;
data[n++] = 0x00;
data[n++] = 0x00;
send_tracker_packet (data, n);
return (get_tracker_burst (retbuf, retlen));
}
int
get_tracker_burst (unsigned char *retbuf, int retlen)
{
unsigned char buf[1000];
int n;
int size;
n = protocol_check_burst_response (buf, sizeof buf);
if (n < 8) {
dbg ("get_tracker_burst runt\n");
exit (1);
}
if (buf[1] != 0x81) {
dbg ("get_tracker_burst: buf[1] != 0x81\n");
exit (1);
}
size = buf[2] | (buf[3] << 8);
if (size == 0)
return (0);
if (size + 8 > n) {
dbg ("get_tracker_burst: invalid size\n");
exit (1);
}
memcpy (retbuf, buf + 8, size);
return (size);
}
int
protocol_check_burst_response (unsigned char *retbuf, int retlen)
{
unsigned char buf[100];
int n;
int timeout;
int used, togo;
int try;
int thistime;
timeout = 100;
used = 0;
togo = retlen;
try = 0;
while (1) {
try++;
if (try >= 128) {
dbg ("protocol_check_burst_response: no end\n");
exit (1);
}
n = receive_message (buf, sizeof buf, timeout);
if (n > 5 && buf[2] == 0x40 && buf[5] == 0x4) {
dbg ("protocol_check_burst_response got 0x40\n");
exit (1);
} else if (0 && n > 4 && buf[2] == 0x4f) {
thistime = n - 4 - 1;
if (thistime > togo)
thistime = togo;
memcpy (retbuf + used, buf + 4, thistime);
used += thistime;
togo -= thistime;
dbg ("check_burst_response failed because 0x4f\n");
return (used);
} else if (n > 4 && buf[2] == 0x50) {
thistime = n - 4 - 1;
if (thistime > togo)
thistime = togo;
memcpy (retbuf + used, buf + 4, thistime);
used += thistime;
togo -= thistime;
if (buf[3] & 0x80) {
dbg ("check_burst_response success\n");
return (used);
}
}
}
}
int
gen_packet_id (void)
{
int seq;
seq = ++current_packet_id;
return (0x38 | (seq & 0x07));
}
void
send_tracker_packet (unsigned char *buf, int len)
{
unsigned char data[100];
int n;
n = 0;
data[n++] = gen_packet_id ();
memcpy (data + n, buf, len);
n += len;
protocol_send_acknowledged_data (data, n);
}
int
protocol_send_acknowledged_data (unsigned char *msg, int len)
{
unsigned char data[100];
int n;
int try;
n = 0;
data[n++] = 0x4f;
data[n++] = protocol_channel;
memcpy (data + n, msg, len);
n += len;
try = 0;
while (1) {
try++;
if (try > 8) {
dbg ("send_acknoledged_data failed\n");
exit (1);
return (-1);
}
send_message (data, n);
if (protocol_check_tx_response () >= 0)
break;
}
return (0);
}
int
protocol_check_tx_response (void)
{
int try;
int timeout;
int n;
unsigned char msg[100];
int code;
try = 0;
while (1) {
try++;
if (try > 16)
return (-1);
timeout = 100;
n = receive_message (msg, sizeof msg, timeout);
if (n >= 6 && msg[2] == 0x40) {
dump_msg (msg, n);
code = msg[5];
if (code == 5) /* EVENT_TRANSFER_TX_COMPLETED */
break;
if (code == 6) {
dbg ("tx failed\n");
exit (1);
}
}
}
return (0);
}
void
protocol_close_channel (void)