-
Notifications
You must be signed in to change notification settings - Fork 4
/
amlprocess.c
718 lines (631 loc) · 23.3 KB
/
amlprocess.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
/*
* Amiigo Link data processing
*
* @date March 8, 2014
* @author: dashesy
* @copyright Amiigo Inc.
*/
#include <errno.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <termios.h>
#include <getopt.h>
#include <time.h>
#include "common.h"
#include "amidefs.h"
#include "amdev.h"
#include "amlprocess.h"
#include "amchar.h"
#include "gapproto.h"
#include "fwupdate.h"
/******************************************************************************/
typedef struct {
uint8* buf;
uint8 pos;
} GetBits;
/******************************************************************************/
static inline void cmpGetBitsInit(GetBits* gb, void* buf) {
gb->buf = buf;
gb->pos = 0;
}
/******************************************************************************/
static int8 cmpGetBits(GetBits* gb, uint8 nbits) {
int8 val = gb->buf[0] << gb->pos;
uint8 buf0bits = 8 - gb->pos;
if (buf0bits < nbits)
val |= gb->buf[1] >> buf0bits;
gb->pos += nbits;
if (gb->pos >= 8) {
gb->buf++;
gb->pos -= 8;
}
return val >> (8 - nbits);
}
char g_szBaseName[256] = {0};
// Open file for logging
// Inputs:
// szBase - the base name of the log
FILE * log_file_open(amdev_t * dev) {
// Downloaded file
char szFullName[1024] = { 0 };
time_t now = time(NULL);
// Use date-time to avoid overwriting logs
if (g_szBaseName[0] == 0)
strftime(g_szBaseName, 256, "Log_%Y-%m-%d-%H-%M-%S", localtime(&now));
// Use other metadata to distinguish each log
if (dev->dev_idx == 0) {
if (strchr(g_szBaseName, '.'))
sprintf(szFullName, "%s", g_szBaseName);
else
sprintf(szFullName, "%s.log", g_szBaseName);
} else {
if (strchr(g_szBaseName, '.'))
sprintf(szFullName, "d%d_%s", dev->dev_idx, g_szBaseName);
else
sprintf(szFullName, "d%d_%s.log", dev->dev_idx, g_szBaseName);
}
printf("\ndownloading %s ...\n", szFullName);
FILE * fp = fopen(szFullName, g_opt.append ? "a" : "w");
return fp;
}
// Dump content of the buffer
int dump_buffer(uint8_t * buf, ssize_t buflen) {
int i;
printf("Unhandled (%s): \t", att_op2str(buf[0]));
for (i = 0; i < buflen; ++i)
printf("%02x ", buf[i]);
printf("\n");
return 0;
}
// Add a single accel log line to the file (and optionally to console)
void add_accel_line(FILE * logFile, const WEDLogAccel * logAccel) {
char log_line[512] = {0};
sprintf(log_line, "[\"accelerometer\",[%d,%d,%d]]\n", logAccel->accel[0], logAccel->accel[1], logAccel->accel[2]);
fprintf(logFile, log_line);
if (g_opt.console) {
printf(log_line);
fflush(stdout);
}
}
uint8 cmpNbits(int16 diff) {
uint8 v = (diff < 0) ? ~diff : diff;
uint8 nbits = 1;
while (v) {
nbits++;
v >>= 1;
}
return nbits;
}
int8 decode_accel(int8 old_accel, int8 diff, uint8 nbits) {
int8 accel = old_accel + diff; // Yes this may result in integer overflow!
int16 diff16 = accel - old_accel;
uint8 enc_nbits = cmpNbits(diff16);
if (enc_nbits > nbits) {
printf("err: %d > %d (%d -> %d)\n", enc_nbits, nbits, old_accel, accel);
// some packet must be lost! try to recover
if (enc_nbits > 6) {
if (old_accel < 0 && diff < 0)
accel = -128;
else if (old_accel > 0 && diff > 0)
accel = 127;
} else {
accel = old_accel;
}
}
return accel;
}
// Continue downloading packets
int process_download(amdev_t * dev, uint8_t * buf, ssize_t buflen) {
int i;
uint16_t handle = 0;
handle = att_get_u16(&buf[1]);
if (buflen < 4) {
printf("Last notification handle = 0x%04x\n", handle);
dev->state = STATE_COUNT;
return 0;
}
// Note: Each packet starts a log entry
WEDLogAccelCmp logAccelCmp;
WEDLogLSData logLSData;
WEDLogTemp logTemp;
WEDLogLSConfig logLSConfig;
WEDLogCount logCount;
WEDLogEvent logEvent;
// TODO: check packet sizes
// TODO: check data integrity
int packet_len;
int payload = 3; // Payload starting position
WED_LOG_TYPE log_type = buf[payload] & WED_TAG_BITS;
while (payload < buflen) {
if (log_type != WED_LOG_ACCEL_CMP)
dev->read_logs++; // Total number of log points downloaded so far
switch (log_type) {
uint16_t val16;
uint8_t field_count, reset_detected;
uint8_t * pdu;
int nbits;
case WED_LOG_TIME:
packet_len = sizeof(dev->logTime);
if (dev->logFile == NULL)
dev->logFile = log_file_open(dev);
dev->logTime.type = log_type;
dev->logTime.timestamp = att_get_u32(&buf[payload + 1]);
dev->logTime.flags = buf[payload + 5];
reset_detected = dev->logTime.flags & TIMESTAMP_REBOOTED;
if (reset_detected)
printf(" (reboot detected)\n");
fprintf(dev->logFile, "[\"timestamp\",[%u,%u]]\n", dev->logTime.timestamp, dev->logTime.flags);
break;
case WED_LOG_EVENT:
packet_len = sizeof(logEvent);
if (dev->logFile == NULL)
dev->logFile = log_file_open(dev);
logEvent.type = log_type;
logEvent.flags = buf[payload + 1];
fprintf(dev->logFile, "[\"event\",[\"flags\",%u]]\n", logEvent.flags);
break;
case WED_LOG_COUNT:
packet_len = sizeof(logCount);
if (dev->logFile == NULL)
dev->logFile = log_file_open(dev);
logCount.type = log_type;
logCount.log_timestamp = att_get_u32(&buf[payload + 1]);
logCount.log_accel_count = att_get_u32(&buf[payload + 5]);
logCount.old_timestamp = att_get_u32(&buf[payload + 7]);
logCount.timestamp = att_get_u32(&buf[payload + 11]);
fprintf(dev->logFile, "[\"log_count\",[\"log_timestamp\",%u],"
"[\"log_accel_count\",%u],[\"old_timestamp\",%u],[\"timestamp\",%u]]\n", logCount.log_timestamp,
logCount.log_accel_count, logCount.old_timestamp, logCount.timestamp);
break;
case WED_LOG_ACCEL:
packet_len = sizeof(dev->logAccel);
if (dev->logFile == NULL)
dev->logFile = log_file_open(dev);
dev->bValidAccel = 1;
dev->logAccel.type = log_type;
for (i = 0; i < 3; ++i)
dev->logAccel.accel[i] = buf[payload + 1 + i];
add_accel_line(dev->logFile, &dev->logAccel);
break;
case WED_LOG_LS_CONFIG:
packet_len = sizeof(logLSConfig);
if (dev->logFile == NULL)
dev->logFile = log_file_open(dev);
logLSConfig.type = log_type;
logLSConfig.dac_on = buf[payload + 1];
logLSConfig.flags = buf[payload + 2];
logLSConfig.level_led = buf[payload + 3];
logLSConfig.gain = buf[payload + 4];
logLSConfig.log_size = buf[payload + 5];
fprintf(dev->logFile, "[\"lightsensor_config\",[\"dac_on\",%u],"
"[\"flags\",%u],[\"level_led\",%u],[\"gain\",%u],[\"log_size\",%u]]\n", logLSConfig.dac_on, logLSConfig.flags,
logLSConfig.level_led, logLSConfig.gain, logLSConfig.log_size);
break;
case WED_LOG_LS_DATA:
logLSData.type = log_type;
if (dev->ver_flat < FW_VERSION(1,8,84))
{
packet_len = 3 + (sizeof(uint16) * (((WEDLogLSData*)&buf[payload])->val[0] >> 14));
val16 = att_get_u16(&buf[payload + 1]);
field_count = ((val16 & 0xC000) >> 14) + 1;
if (field_count > 3) {
fprintf(stderr, "Invalid LS_DATA ignored\n");
break;
}
logLSData.val[0] = val16 & 0x3FFF;
for (i = 1; i < field_count; ++i)
logLSData.val[i] = att_get_u16(&buf[payload + 1 + i * 2]);
val16 = 0;
switch(field_count)
{
case 1:
val16 = 2;
break;
case 2:
val16 = 6;
break;
case 3:
val16 = 7;
break;
}
} else {
packet_len = WEDLogLSDataSize(&buf[payload]);
val16 = (buf[payload] & 0xE0) >> 5;
field_count = (val16 & 1 ? 1 : 0) + (val16 & 2 ? 1 : 0) + (val16 & 4 ? 1 : 0);
for (i = 0; i < field_count; ++i)
logLSData.val[i] = att_get_u16(&buf[payload + 1 + i * 2]);
}
if (dev->logFile == NULL)
dev->logFile = log_file_open(dev);
if (field_count)
{
int cnt = 0;
fprintf(dev->logFile, "[\"lightsensor\"");
if (val16 & 1 ? 1 : 0)
fprintf(dev->logFile, ",[\"red\",%u]", logLSData.val[cnt++]);
if (val16 & 2 ? 1 : 0)
fprintf(dev->logFile, ",[\"ir\",%u]", logLSData.val[cnt++]);
if (val16 & 4 ? 1 : 0)
fprintf(dev->logFile, ",[\"off\",%u]", logLSData.val[cnt++]);
fprintf(dev->logFile, "]\n");
}
break;
case WED_LOG_TEMP:
packet_len = sizeof(logTemp);
if (dev->logFile == NULL)
dev->logFile = log_file_open(dev);
logTemp.type = log_type;
logTemp.temperature = att_get_u16(&buf[payload + 1]);
fprintf(dev->logFile, "[\"temperature\",%d]\n", logTemp.temperature);
break;
case WED_LOG_TAG:
packet_len = sizeof(dev->logTag);
if (dev->logFile == NULL)
dev->logFile = log_file_open(dev);
dev->logTag.type = log_type;
memcpy(&dev->logTag.tag, &buf[payload + 1], 4);
fprintf(dev->logFile, "[\"tag\",%u]\n", dev->logTag.tag);
break;
case WED_LOG_ACCEL_CMP:
packet_len = WEDLogAccelCmpSize(&buf[payload]);
if (packet_len < 2) {
fprintf(stderr, "ACCEL_CMP with invalid length ignored\n");
break;
}
if (dev->logFile == NULL)
dev->logFile = log_file_open(dev);
logAccelCmp.type = log_type;
logAccelCmp.count_bits = buf[payload + 1];
field_count = (logAccelCmp.count_bits & 0xF) + 1;
dev->read_logs += field_count;
if (g_opt.leave_compressed) {
fprintf(dev->logFile, "[\"accelerometer_compressed\",[\"count_bits\",%u],[\"data\",[",logAccelCmp.count_bits);
for (i = 0; i < packet_len - 2; ++i) {
fprintf(dev->logFile, "%u", buf[payload + 2 + i]);
if (i < packet_len - 3)
fprintf(dev->logFile, ",");
}
fprintf(dev->logFile, "]]]\n");
break;
}
nbits = -1;
switch ((logAccelCmp.count_bits & 0x70) >> 4) {
case WED_LOG_ACCEL_CMP_3_BIT:
nbits = 3;
break;
case WED_LOG_ACCEL_CMP_4_BIT:
nbits = 4;
break;
case WED_LOG_ACCEL_CMP_5_BIT:
nbits = 5;
break;
case WED_LOG_ACCEL_CMP_6_BIT:
nbits = 6;
break;
case WED_LOG_ACCEL_CMP_8_BIT:
nbits = 8;
dev->bValidAccel = 1;
break;
case WED_LOG_ACCEL_CMP_STILL:
if (logAccelCmp.count_bits & 0x80)
nbits = 0;
break;
default:
break;
}
if (nbits < 0) {
fprintf(stderr, "Invalid ACCEL_CMP ignored\n");
break;
}
// We must first get at least one uncompressed accel log
if (!dev->bValidAccel)
break;
if (dev->logFile == NULL)
dev->logFile = log_file_open(dev);
if (nbits == 0) {
// It is still, just replicate
while (field_count--)
add_accel_line(dev->logFile, &dev->logAccel);
break;
}
pdu = &buf[payload];
pdu += 2;
if (nbits == 8) {
while (field_count--) {
for (i = 0; i < 3; ++i)
dev->logAccel.accel[i] = pdu[i];
pdu += 3;
add_accel_line(dev->logFile, &dev->logAccel);
}
} else {
GetBits gb;
cmpGetBitsInit(&gb, pdu);
while (field_count--) {
uint8 i;
for (i = 0; i < 3; i++) {
int8 diff = cmpGetBits(&gb, nbits);
dev->logAccel.accel[i] = decode_accel(dev->logAccel.accel[i], diff, nbits);
}
add_accel_line(dev->logFile, &dev->logAccel);
}
}
break;
default:
packet_len = (buflen - payload);
printf("Notification handle: 0x%04x total: %u value: ", handle, (uint32_t)buflen);
for (i = payload; i < buflen; ++i)
printf("%02x ", buf[i]);
printf("\n");
break;
}
// Move forward within aggregate packet
payload += packet_len;
//printf("Type: %u, len: %u, total: %u\n", log_type, packet_len, buflen);
if (payload < buflen)
log_type = buf[payload] & WED_TAG_BITS;
} // end while (payload < buflen
if (!g_opt.live) {
if (dev->dev_idx == 0)
printf("\rdownloading ... %u out of %u (%2.0f%%)", dev->read_logs,
dev->total_logs, (100.0 * dev->read_logs) / dev->total_logs);
else
printf("\r\t\t\t\t\t\t%u out of %u (%2.0f%% of %d)", dev->read_logs,
dev->total_logs, (100.0 * dev->read_logs) / dev->total_logs, dev->dev_idx);
fflush(stdout);
if (dev->read_logs >= dev->total_logs || dev->status.num_log_entries == 0)
dev->state = STATE_COUNT; // Done with command
}
return 0;
}
// Print status
void print_status(uint8_t status) {
if (status & STATUS_UPDATE)
printf(" (Updating) ");
if (status & STATUS_FASTMODE)
printf(" (Fast Mode) ");
else if (status & STATUS_SLEEPMODE)
printf(" (Sleep Mode) ");
else
printf(" (Slow Mode) ");
if (status & STATUS_CHARGING)
printf(" (Charging) ");
if (status & STATUS_LS_INPROGRESS)
printf(" (Light Capture) ");
if (status & STATUS_WORN)
printf(" (Worn) ");
if (status & STATUS_RECORDING)
printf(" (Rec) ");
if (status & STATUS_WRISTBAND)
printf(" (Wristband) ");
}
// device status
int process_status(amdev_t * dev, uint8_t * buf, ssize_t buflen) {
if (buflen < sizeof(WEDStatus))
return -1;
uint8_t * pdu = &buf[1];
dev->status.num_log_entries = att_get_u32(&pdu[0]);
dev->status.battery_level = pdu[4];
dev->status.status = pdu[5];
dev->status.cur_time = att_get_u32(&pdu[6]);
memcpy(&dev->status.cur_tag, &pdu[10], WED_TAG_SIZE);
if (buflen >= sizeof(WEDStatus) + 1)
dev->status.reboot_count = pdu[10 + WED_TAG_SIZE];
uint32_t tag;
memcpy(&tag, &dev->status.cur_tag[0], 4);
if (dev->dev_idx == 0)
printf("\nStatus: Build: %s\t Version: %s \n\t Logs: %u\t Battery: %u%%\t Time: %10.3f s\tReboots: %u\tTag: %10u",
dev->szBuild, dev->szVersion,
dev->status.num_log_entries, dev->status.battery_level,
dev->status.cur_time * 1.0 / WED_TIME_TICKS_PER_SEC, dev->status.reboot_count, tag);
else
printf("\nStatus: Build: %s\t Version: %s \t Device: %d\n\t Logs: %u\t Battery: %u%%\t Time: %10.3f s\tReboots: %u\tTag: %10u",
dev->szBuild, dev->szVersion, dev->dev_idx,
dev->status.num_log_entries, dev->status.battery_level,
dev->status.cur_time * 1.0 / WED_TIME_TICKS_PER_SEC, dev->status.reboot_count, tag);
print_status(dev->status.status);
printf("\n");
return 0;
}
// Extended device status
int process_extstatus(amdev_t * dev, uint8_t * buf, ssize_t buflen) {
dev->state = STATE_COUNT;
if (buflen < sizeof(WEDCurrentConfig) + 1)
return -1;
uint8_t * pdu = &buf[1];
WEDCurrentConfig extstatus;
memcpy(&extstatus, &pdu[0], sizeof(extstatus));
const char log_names[LogNum-1][10] = {"Accel", "Oxygen"};
const char rate_names[RATES][10] = {"Slow", "Fast", "Sleep"};
int i, j;
// title of rates
for (i = 0; i < LogNum-1; ++i)
printf("\t%s", log_names[i]);
printf("\n");
for (j = 0; j < RATES; ++j) {
printf("%s\t", rate_names[j]);
for (i = 0; i < LogNum-1; ++i) {
printf("%u\t", extstatus.rates[j][i]);
}
printf("\n");
}
printf("Pulse capture durations:\t");
for (i = 0; i < RATES; ++i)
printf("%s: %u\t", rate_names[i], extstatus.pulse_durations[i]);
printf("\nExtended status: ");
if (extstatus.status & EXTSTATUS_LOG_READY)
printf(" (log ready) ");
uint8 mem_id = (extstatus.status & EXTSTATUS_MEM_ID_MASK) >> EXTSTATUS_MEM_ID_SHIFT;
uint8 ver = (extstatus.status & EXTSTATUS_VER_MASK) >> EXTSTATUS_VER_SHIFT;
printf("mem: %d rev: %d.%d\n", mem_id, ver / 10 + 1, ver % 10);
return 0;
}
// i2c result
int process_debug_i2c(amdev_t * dev, uint8_t * buf, ssize_t buflen) {
dev->state = STATE_COUNT;
if (buflen < sizeof(WEDDebugI2CResult) + 1)
return -1;
uint8_t * pdu = &buf[1];
WEDDebugI2CResult i2c_res;
i2c_res.status = att_get_u8(&pdu[0]);
i2c_res.data = att_get_u8(&pdu[1]);
if (i2c_res.status) {
printf("failed (%d)\n", i2c_res.status);
return 0;
}
printf(" %x\n", i2c_res.data);
return 0;
}
// State machine to get necessary information.
// Discover device current status, and running firmware
int discover_device(amdev_t * dev) {
uint16_t handle;
DISCOVERY_STATE new_state = STATE_NONE;
switch (dev->state) {
case STATE_NONE:
handle = g_char[AMIIGO_UUID_BUILD].value_handle;
new_state = STATE_BUILD;
if (handle == 0) {
// Ignore information
dev->state = new_state;
return discover_device(dev);
}
break;
case STATE_BUILD:
handle = g_char[AMIIGO_UUID_VERSION].value_handle;
new_state = STATE_VERSION;
if (handle == 0) {
// Ignore information
dev->state = new_state;
return discover_device(dev);
}
break;
case STATE_VERSION:
handle = g_char[AMIIGO_UUID_STATUS].value_handle;
new_state = STATE_STATUS;
if (handle == 0) {
fprintf(stderr, "No device status to proceed!\n");
return -1; // Not ready yet
}
break;
default:
return 0; // already handled
break;
}
dev->state = new_state;
// Read the handle of interest
int ret = exec_read(dev->sock, handle);
return ret;
}
//----------------------------------------------------------------------------------------
// Process incoming raw data
int process_data(amdev_t * dev, uint8_t * buf, ssize_t buflen) {
int ret = 0;
struct att_data_list *list = NULL;
uint16_t handle = 0;
uint8_t err = ATT_ECODE_IO;
int i, j;
switch (buf[0]) {
case ATT_OP_ERROR:
if (buflen > 4)
err = buf[4];
if (err == ATT_ECODE_ATTR_NOT_FOUND) {
// If no battery information, we do not have status
if (dev->status.battery_level == 0)
discover_device(dev);
} else {
if (buflen > 1)
handle = att_get_u16(&buf[1]);
fprintf(stderr, "Error (%s) on handle (0x%4.4x)\n",
att_ecode2str(err), handle);
}
break;
case ATT_OP_HANDLE_NOTIFY:
// Proceed with download
process_download(dev, buf, buflen);
break;
case ATT_OP_READ_BY_TYPE_RESP:
list = dec_read_by_type_resp((uint8_t *) buf, buflen);
if (list == NULL)
return -1;
handle = 0;
for (i = 0; i < list->num; i++) {
uint8_t *value = list->data[i];
bt_uuid_t uuid;
handle = att_get_u16(value);
if (list->len == 7) {
bt_uuid_t uuid16 = att_get_uuid16(&value[5]);
bt_uuid_to_uuid128(&uuid16, &uuid);
} else {
uuid = att_get_uuid128(&value[5]);
}
uint8_t properties = value[2];
uint16_t value_handle = att_get_u16(&value[3]);
for (j = 0; j < AMIIGO_UUID_COUNT; ++j) {
if (bt_uuid_cmp(&uuid, &g_char[j].uuid) == 0) {
g_char[j].handle = handle;
g_char[j].properties = properties;
g_char[j].value_handle = value_handle;
break;
}
}
char str_uuid[MAX_LEN_UUID_STR + 1] = { 0 };
bt_uuid_to_string(&uuid, str_uuid, sizeof(str_uuid));
printf("handle: 0x%04x\t properties: 0x%04x\t value handle: 0x%04x\t UUID: %s \n",
handle, properties, value_handle, str_uuid);
} // end for(i = 0
// Get the rest of handles
if (handle != 0 && (handle + 1) < OPT_END_HANDLE) {
discover_handles(dev->sock, handle + 1, OPT_END_HANDLE);
}
break;
case ATT_OP_READ_RESP:
switch (dev->state) {
case STATE_BUILD:
strncpy(dev->szBuild, (const char *) &buf[1], buflen - 1);
// More to discover
discover_device(dev);
break;
case STATE_VERSION:
dev->ver.Major = buf[1];
dev->ver.Minor = buf[2];
dev->ver.Build = att_get_u16(&buf[3]);
dev->ver_flat = FW_VERSION(dev->ver.Major, dev->ver.Minor, dev->ver.Build);
sprintf(dev->szVersion, "%u.%u.%u%s", dev->ver.Major, dev->ver.Minor, dev->ver.Build,
dev->ver_flat < FW_VERSION(1,8,89) ? " (< 1.8.89: incompatible config)" : "");
// More to discover
discover_device(dev);
break;
case STATE_STATUS:
process_status(dev, buf, buflen);
break;
case STATE_DOWNLOAD:
// This must be keep-alive
process_status(dev, buf, buflen);
break;
case STATE_FWSTATUS:
case STATE_FWSTATUS_WAIT:
// Act upon new firmware update status
ret = process_fwstatus(dev, buf, buflen);
break;
case STATE_I2C:
// i2c result
ret = process_debug_i2c(dev, buf, buflen);
break;
case STATE_EXTSTATUS:
// extended status result
ret = process_extstatus(dev, buf, buflen);
break;
default:
dump_buffer(buf, buflen);
break;
}
break;
default:
dump_buffer(buf, buflen);
break;
}
if (list != NULL)
att_data_list_free(list);
return ret;
}