-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.c
512 lines (454 loc) · 15.8 KB
/
main.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
/*
* Amiigo offline utility
*
* @date Aug 25, 2013
* @author: dashesy
* @copyright Amiigo Inc.
*/
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <termios.h>
#include <getopt.h>
#include <time.h>
#include <ctype.h>
#include <string.h>
#include "amidefs.h"
#include "amdev.h"
#include "hcitool.h"
#include "common.h"
#include "gapproto.h"
#include "amproto.h"
#include "amoldproto.h"
#include "amlprocess.h"
#include "cmdparse.h"
#include "fwupdate.h"
extern void char_init(void);
extern char g_szBaseName[256];
int g_amver_major = 1;
int g_amver_minor = 5;
// TODO: have option of multiple sources
char g_src[512] = "hci0"; // Device to connect from
aml_options_t g_opt; // flags option
// Execute the requested command
int exec_command(amdev_t * dev) {
dev->started = 1;
switch (g_cmd) {
case AMIIGO_CMD_NONE:
dev->state = STATE_COUNT; // Done with command
break;
case AMIIGO_CMD_DOWNLOAD:
if (dev->status.num_log_entries == 0 && !g_opt.live) {
// Nothing to download!
dev->state = STATE_COUNT; // Done with command
return 0;
}
dev->state = STATE_DOWNLOAD; // Download in progress
dev->total_logs = dev->status.num_log_entries; // How many logs to download
return exec_download(dev->sock);
break;
case AMIIGO_CMD_CONFIGLS:
dev->state = STATE_COUNT; // Done with command
if (dev->ver_flat < FW_VERSION(1,8,117))
return exec_configls_18116(dev->sock);
else
return exec_configls(dev->sock);
break;
case AMIIGO_CMD_CONFIGACCEL:
dev->state = STATE_COUNT; // Done with command
return exec_configaccel(dev->sock);
break;
case AMIIGO_CMD_CONFIGTEMP:
dev->state = STATE_COUNT; // Done with command
return exec_configtemp(dev->sock);
break;
case AMIIGO_CMD_BLINK:
dev->state = STATE_COUNT; // Done with command
return exec_blink(dev->sock);
break;
case AMIIGO_CMD_DEEPSLEEP:
dev->state = STATE_COUNT; // Done with command
return exec_deepsleep(dev->sock);
break;
case AMIIGO_CMD_RESET_CPU:
case AMIIGO_CMD_RESET_LOGS:
case AMIIGO_CMD_RESET_CONFIGS:
dev->state = STATE_COUNT; // Done with command
return exec_reset(dev->sock, g_cmd);
break;
case AMIIGO_CMD_FWUPDATE:
dev->state = STATE_FWSTATUS;
return exec_fwupdate(dev->sock);
break;
case AMIIGO_CMD_I2C_READ:
case AMIIGO_CMD_I2C_WRITE:
dev->state = STATE_I2C;
return exec_debug_i2c(dev->sock);
break;
case AMIIGO_CMD_RENAME:
dev->state = STATE_COUNT; // Done with command
return exec_rename(dev->sock);
break;
case AMIIGO_CMD_TAG:
dev->state = STATE_COUNT; // Done with command
return exec_tag(dev->sock);
break;
case AMIIGO_CMD_TEST_SEQ:
dev->state = STATE_COUNT; // Done with command
return exec_test_seq(dev->sock);
break;
case AMIIGO_CMD_EXTSTATUS:
dev->state = STATE_EXTSTATUS;
return exec_extstatus(dev->sock);
break;
default:
return 0;
break;
}
return 0;
}
void show_usage_screen(void) {
printf("Amiigo Link command line utility version %d.%d\n", g_amver_major, g_amver_minor);
printf("Usage: amlink [options] [command] [input|output]\n"
"Options:\n"
" -v, --verbose Verbose mode \n"
" More messages are dumped to console.\n"
" --full Full characteristics discovery. \n"
" If specified handles are queried.\n"
" --i, --adapter uuid|hci<N>[,...]\n"
" Interface adapter(s) to use (default is hci0)\n"
" --b, --device uuid1[,...] \n"
" Amiigo device(s) to connect to, default is shoepod then wristband.\n"
" Example: --b 90:59:AF:04:32:82\n"
" Use --lescan to find the UUID list\n"
" --compressed Leave logs in compressed form.\n"
" --append append to end of file.\n"
" --raw Download logs in raw format (no compression).\n"
" -l, --live Live download as a stream.\n"
" Hit `q` to end the stream.\n"
" --print\n"
" Print accelerometer to console as well as the log file.\n"
"Command:\n"
" --lescan \n"
" Low energy scan (needs root priviledge)\n"
" --c, --command cmd \n"
" Command to execute:\n"
" status: (default) perform device discovery\n"
" download: download the logs\n"
" configls: configure light sensor\n"
" configaccel: configure acceleration sensors\n"
" configtemp: configure temperature sensor\n"
" blink: configure blink LED\n"
" deepsleep: go to deep sleep and only wake on hard double tap\n"
" resetlogs: reset buffered logs\n"
" resetcpu: restart the board\n"
" resetconfigs: set all configs to default\n"
" rename: rename the device\n"
" tag: set a tag\n"
" test_seq: set test sequence mode to test_mode\n"
" extstatus: read extended status\n"
" --mode fast|slow|sleep\n"
" Switch to slow or fast mode after tag\n"
" --input filename|param1=val1[,...]\n"
" Configuration file to use for given command.\n"
" Or input line sequence to specify parameters on command line.\n"
" --fwupdate file\n"
" Firmware image file to to use for update.\n"
" --i2c_read address:reg\n"
" Read i2c address and register (debugging only).\n"
" --i2c_write address:reg:value\n"
" Write value to i2c address and register (debugging only).\n"
" --help Display this usage screen\n"
"Parameters:\n"
" parameters in a file are in the form of <key> <value> on each line\n"
" parameters on a command line are in the form of <key>=<value> and are separated by comma.\n"
" light sensor parameters: ls_fast_interval, ls_slow_interval, ls_sleep_interval, ls_duration, "
"ls_fast_duration, ls_slow_duration, ls_sleep_duration, ls_debug, ls_flags, ls_movement, ls_duration_mul\n"
" accelerometer parameters: accel_slow_rate, accel_fast_rate, accel_sleep_rate, test_mode, mode\n"
" temperature parameters: temp_slow_rate, temp_fast_rate, temp_sleep_rate\n"
"Input Output: (optional) \n"
" If running download command, will be taken as output file\n"
" Otherwise will be taken as input file name or line sequence\n"
);
printf("\namlink is Copyright Amiigo inc\n");
}
void show_version(void) {
printf("Amiigo Link command line utility version %d.%d\n", g_amver_major, g_amver_minor);
printf("\namlink is Copyright Amiigo inc\n");
}
static void do_command_line(int argc, char * const argv[]) {
// Parse the input
while (1) {
int c;
int option_index = 0;
static struct option long_options[] = {
{ "verbose", 0, 0, 'v' },
{ "version", 0, 0, 'V' },
{ "live", 0, 0, 'l' },
{ "full", 0, 0, 'a' },
{ "compressed", 0, 0, 'p'},
{ "raw", 0, 0, 'r'},
{ "append", 0, 0, 'A' },
{ "lescan", 0, 0, 's' },
{ "i", 1, 0, 'i' },
{ "adapter", 1, 0, 'i' },
{ "b", 1, 0, 'b' },
{ "device", 1, 0, 'b' },
{ "c", 1, 0, 'x' },
{ "command", 1, 0, 'x' },
{ "input", 1, 0, 'f' },
{ "mode", 1, 0, 'm'},
{ "print", 0, 0, 'o' },
{ "i2c_read", 1, 0, 'd'},
{ "i2c_write", 1, 0, 'w'},
{ "fwupdate", 1, 0, 'u' },
{ "help", 0, 0, '?' },
{ 0, 0, 0, 0 } };
c = getopt_long(argc, argv, "vl?", long_options, &option_index);
if (c == -1)
break;
switch (c) {
case 0:
printf("option %s", long_options[option_index].name);
if (optarg)
printf(" with arg %s", optarg);
printf("unsupported\n");
break;
case 'i':
if (parse_adapter(optarg))
exit(1);
break;
case 's':
do_lescan();
exit(0);
break;
case 'o':
g_opt.console = 1;
break;
case 'l':
g_opt.live = 1;
break;
case 'v':
g_opt.verbosity = 1;
break;
case 'V':
show_version();
exit(0);
break;
case 'a':
g_opt.full = 1;
break;
case 'p':
g_opt.leave_compressed = 1;
break;
case 'r':
g_opt.raw = 1;
break;
case 'A':
g_opt.append = 1;
break;
case 'b':
if (parse_device(optarg))
exit(1);
break;
case 'x':
if (parse_command(optarg))
exit(1);
break;
case 'm':
if (parse_mode(optarg))
exit(1);
break;
case 'f':
if (parse_input_file(optarg))
exit(1);
break;
case 'u':
if (set_update_file(optarg))
exit(1);
break;
case 'd':
if (parse_i2c_read(optarg))
exit(1);
break;
case 'w':
if (parse_i2c_write(optarg))
exit(1);
break;
case '?':
show_usage_screen();
exit(0);
break;
default:
printf("?? getopt returned character code 0%o ??\n", c);
break;
}
}
if (argc == 1) {
show_version();
exit(0);
}
int err = 0;
if (optind == argc - 1) {
// Parse the last reamining argument
if (g_cmd == AMIIGO_CMD_DOWNLOAD) {
strcpy(&g_szBaseName[0], argv[optind]);
} else {
err = parse_input_file(argv[optind]);
}
} else if (optind < argc) {
printf("Unrecognized command line arguments: ");
while (optind < argc)
printf("%s ", argv[optind++]);
printf("\n");
err = 1;
}
if (err) {
printf("Use --help to get the list of valid commands and options\n");
exit(1);
}
}
char kbhit() {
struct termios oldt, newt;
int ch;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~( ICANON | ECHO);
newt.c_cc[VMIN] = 0;
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
int nread = read(STDIN_FILENO, &ch, 1);
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
if (nread == 1)
return ch;
else
return 0;
}
int main(int argc, char **argv) {
int ret, i;
time_t start_time[MAX_DEV_COUNT], stop_time[MAX_DEV_COUNT], download_time[MAX_DEV_COUNT];
// do not buffer output
setbuf(stdout, NULL);
// Initialize the characteristics
char_init();
// Initialize the command configs
cmd_init();
// Amiigo devices to interact with
amdev_t devices[MAX_DEV_COUNT];
// zero-fill the devices state
memset(&devices[0], 0, sizeof(devices));
// Set parameters based on command line
do_command_line(argc, argv);
for (i = 0; i < g_cfg.count_dst; ++i) {
amdev_t * dev = &devices[i];
dev->dev_idx = i; // Keep the index for reference
// Connect to all devices
dev->sock = gap_connect(g_src, g_cfg.dst[i]);
if (dev->sock < 0) {
return -1;
}
if (g_opt.full) {
// Start by discovering Amiigo handles
ret = discover_handles(dev->sock, OPT_START_HANDLE, OPT_END_HANDLE);
if (ret) {
fprintf(stderr, "discover_handles() error %d in %s\n", ret, g_cfg.dst[i]);
return -1;
}
} else {
// Use default handles and discover the device
ret = discover_device(dev);
if (ret) {
fprintf(stderr, "discover_device() error %d in %s\n", ret, g_cfg.dst[i]);
return -1;
}
}
// Timing of downloads
start_time[i]= time(NULL);
stop_time[i] = start_time[i];
download_time[i] = start_time[i];
}
int done_count = 0; // Number of devices done with their command
int dev_idx = g_cfg.count_dst - 1;
for (;;) {
// See if user ended the run
if (kbhit() == 'q')
break;
dev_idx--;
if (dev_idx < 0)
dev_idx = g_cfg.count_dst - 1;
amdev_t * dev = &devices[dev_idx];
// No need to keep-alive during firmware update
if (dev->state != STATE_FWSTATUS_WAIT) {
stop_time[dev_idx] = time(NULL);
double diff = difftime(stop_time[dev_idx], start_time[dev_idx]);
// Keep-alive by reading status every 60s
if (diff > 60) {
if (g_opt.verbosity)
printf(" (Keep alive %s)\n", g_cfg.dst[dev_idx]);
start_time[dev_idx] = stop_time[dev_idx];
// Read the status to keep conection alive
exec_status(dev->sock);
}
// If downloading (non-live) and not already done
if (dev->state == STATE_DOWNLOAD && !g_opt.live && !dev->done) {
diff = difftime(stop_time[dev_idx], download_time[dev_idx]);
// Download timeout reached
if (diff > 2)
{
printf(" (Timeout %s)\n", g_cfg.dst[dev_idx]);
break;
}
}
}
uint8_t buf[1024] = {0};
int len = gap_recv(dev->sock, &buf[0], sizeof(buf));
if (len < 0)
break;
if (len == 0)
continue;
// Last time apacket came
download_time[dev_idx] = stop_time[dev_idx];
// Process incoming data
ret = process_data(dev, buf, len);
if (ret) {
fprintf(stderr, "main process_data() error %d in %s\n", ret, g_cfg.dst[dev_idx]);
break;
}
// If all devices have their status read, execute the requested command
if (dev->status.battery_level > 0 && dev->state != STATE_COUNT && !dev->started) {
// Now that we have status (e.g. number of logs) of all devices
// Start execution of the requested command
ret = exec_command(dev);
if (ret) {
fprintf(stderr, "exec_command() error %d in %s\n", ret, g_cfg.dst[dev_idx]);
break;
}
}
if (dev->state == STATE_COUNT) {
if (!dev->done) {
dev->done = 1;
done_count++;
}
// Done the the command on all devices
if (done_count == g_cfg.count_dst)
break;
}
} //end for(;;
for (i = 0; i < g_cfg.count_dst; ++i) {
amdev_t * dev = &devices[i];
// Reset CPU if need to exit in the middle of firmware update
if (dev->state == STATE_FWSTATUS_WAIT)
exec_reset(dev->sock, AMIIGO_CMD_RESET_CPU);
// Close the socket
gap_shutdown(dev->sock);
// Close log files
if (dev->logFile != NULL)
{
fclose(dev->logFile);
dev->logFile = NULL;
}
} // } //end for(
printf("\n");
return 0;
}