forked from ryanbinns/ttwatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibttwatch.cpp
1582 lines (1307 loc) · 47.7 KB
/
libttwatch.cpp
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
//------------------------------------------------------------------------------
// libttwatch.cpp
// implementation file for the TomTom watch methods
//------------------------------------------------------------------------------
#include "libttwatch.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "unistd.h"
#ifdef TT_BIG_ENDIAN
# define TT_BIGENDIAN(x) (x)
#else /* TT_LITTLE_ENDIAN */
# define TT_BIGENDIAN(x) ( ((((x) >> 24) & 0xff) << 0) | \
((((x) >> 16) & 0xff) << 8) | \
((((x) >> 8) & 0xff) << 16) | \
((((x) >> 0) & 0xff) << 24) )
#endif
#include <string>
#include <vector>
#include <sys/time.h>
//------------------------------------------------------------------------------
// macros
#define RETURN_ERROR(err) \
do \
{ \
int result = (err); \
if (result != TTWATCH_NoError) \
return result; \
} while (0) \
#define foreach(var, container) for (__decltype(container)::iterator var = container.begin(); var != container.end(); ++var)
// message IDs
#define MSG_OPEN_FILE_WRITE (0x02)
#define MSG_DELETE_FILE (0x03)
#define MSG_WRITE_FILE_DATA (0x04)
#define MSG_GET_FILE_SIZE (0x05)
#define MSG_OPEN_FILE_READ (0x06)
#define MSG_READ_FILE_DATA_REQUEST (0x07)
#define MSG_READ_FILE_DATA_RESPONSE (0x09)
#define MSG_FIND_CLOSE (0x0a)
#define MSG_CLOSE_FILE (0x0c)
#define MSG_UNKNOWN_0D (0x0d)
#define MSG_FORMAT_WATCH (0x0e)
#define MSG_RESET_DEVICE (0x10) /* issued after updating firmware,
causes USB disconnect and reconnect
after approximately 90 seconds */
#define MSG_FIND_FIRST_FILE (0x11)
#define MSG_FIND_NEXT_FILE (0x12)
#define MSG_GET_CURRENT_TIME (0x14)
#define MSG_UNKNOWN_1A (0x1a)
#define MSG_RESET_GPS_PROCESSOR (0x1d)
#define MSG_UNKNOWN_1F (0x1f)
#define MSG_GET_PRODUCT_ID (0x20)
#define MSG_GET_FIRMWARE_VERSION (0x21)
#define MSG_UNKNOWN_22 (0x22)
#define MSG_UNKNOWN_23 (0x23)
#define MSG_GET_BLE_VERSION (0x28)
// interesting information:
// 1. messages 0x1a, 0x0a, 0x23, 0x23 are always sent after:
// - file list is read
// - main firmware is written
// - gps firmware is written
// 2. message group 0x0d, 0x0a, 0x22, 0x22, 0x20, 0x0a, 0x28, 0x28, 0x1f, 0x21
// is always sent at startup
typedef struct
{
uint8_t direction; // 0x09 = transmit, 0x01 = receive
uint8_t length; // of remaining packet (excluding direction and length)
uint8_t counter;
uint8_t message_id;
} PacketHeader;
// TX packets
typedef struct
{
uint32_t _unk[2];
} TXFindFirstFilePacket;
typedef struct
{
uint32_t id;
} TXFileOperationPacket;
typedef struct
{
uint32_t id;
uint32_t length;
} TXReadFileDataPacket;
typedef struct
{
uint32_t id;
uint8_t data[246];
} TXWriteFileDataPacket;
// RX packets
typedef struct
{
uint32_t _unk1;
uint32_t id;
uint32_t _unk2;
uint32_t file_size;
uint32_t end_of_list;
} RXFindFilePacket;
typedef struct
{
uint32_t _unk1;
} RXFindClosePacket;
typedef struct
{
uint32_t _unk1;
uint32_t id;
uint32_t _unk2[2];
uint32_t error;
} RXFileOperationPacket;
typedef struct
{
uint32_t _unk1;
uint32_t id;
uint32_t _unk2;
uint32_t file_size;
uint32_t _unk3;
} RXGetFileSizePacket;
typedef struct
{
uint32_t id;
uint32_t data_length;
uint8_t data[242];
} RXReadFileDataPacket;
typedef struct
{
uint32_t _unk1;
uint32_t id;
uint32_t _unk2[3];
} RXWriteFileDataPacket;
typedef struct
{
uint32_t utc_time;
uint32_t _unk[4];
} RXGetCurrentTimePacket;
typedef struct
{
char version[60];
} RXGetFirmwareVersionPacket;
typedef struct
{
uint32_t product_id;
} RXGetProductIDPacket;
typedef struct
{
uint32_t ble_version;
} RXGetBLEVersionPacket;
typedef struct
{
char message[60];
} RXRebootWatchPacket;
typedef struct
{
uint32_t _unk1[4];
uint32_t error;
} RXFormatWatchPacket;
typedef std::vector<std::pair<uint32_t,uint32_t> > FileList;
//------------------------------------------------------------------------------
// variables
static int s_show_packets;
//------------------------------------------------------------------------------
void print_packet(uint8_t *packet, uint8_t size)
{
int i;
if (s_show_packets)
{
struct timespec tmspec;
clock_gettime(CLOCK_MONOTONIC, &tmspec);
printf("%lu.%03lu: ", tmspec.tv_sec, tmspec.tv_nsec / 1000000);
for (i = 0; i < size; ++i)
printf("%02X ", packet[i]);
printf("\n");
}
}
//------------------------------------------------------------------------------
int send_packet(TTWATCH *watch, uint8_t msg, uint8_t tx_length,
const uint8_t *tx_data, uint8_t rx_length, uint8_t *rx_data)
{
static uint8_t message_counter = 0;
uint8_t packet[256] = {0};
int count = 0;
int result = 0;
// create the tx packet
packet[0] = 0x09;
packet[1] = tx_length + 2;
packet[2] = message_counter++;
packet[3] = msg;
memcpy(packet + 4, tx_data, tx_length);
uint16_t packet_size;
uint8_t write_usb_endpoint;
uint8_t read_usb_endpoint;
if (watch->usb_product_id == TOMTOM_MULTISPORT_PRODUCT_ID)
{
packet_size = tx_length + 4;
write_usb_endpoint = 0x05;
read_usb_endpoint = 0x84;
}
else if (IS_SPARK(watch->usb_product_id))
{
packet_size = 256;
write_usb_endpoint = 0x02;
read_usb_endpoint = 0x81;
}
else
return TTWATCH_UnableToSendPacket;
print_packet(packet, packet_size);
// send the packet
result = libusb_interrupt_transfer(watch->device, write_usb_endpoint, packet, packet_size, &count, 10000);
if (result || (count != packet_size))
return TTWATCH_UnableToSendPacket;
if (watch->usb_product_id == TOMTOM_MULTISPORT_PRODUCT_ID)
packet_size = 64;
// read the reply
unsigned timeout = 20000; // 20 seconds for most message types
if (msg == MSG_FORMAT_WATCH)
timeout = 120000; // formatting takes about 60 seconds, so make the timeout 120 seconds
result = libusb_interrupt_transfer(watch->device, read_usb_endpoint, packet, packet_size, &count, timeout);
if (result)
return TTWATCH_UnableToReceivePacket;
print_packet(packet, packet[1] + 2);
// check that the reply is valid
if (packet[0] != 0x01)
return TTWATCH_InvalidResponse;
if ((rx_length < 60) && (packet[1] != (rx_length + 2)))
return TTWATCH_IncorrectResponseLength;
if (packet[2] != (uint8_t)(message_counter - 1))
return TTWATCH_OutOfSyncResponse;
if (msg == MSG_READ_FILE_DATA_REQUEST)
{
if (packet[3] != MSG_READ_FILE_DATA_RESPONSE)
return TTWATCH_UnexpectedResponse;
}
else
{
if (packet[3] != msg)
return TTWATCH_UnexpectedResponse;
}
// copy the back data to the caller
if (rx_data)
memcpy(rx_data, packet + 4, packet[1] - 2);
return TTWATCH_NoError;
}
//------------------------------------------------------------------------------
int enum_files(TTWATCH *watch, uint32_t type, FileList &files)
{
uint32_t id, length;
int result;
RETURN_ERROR(ttwatch_find_first_file(watch, &id, &length));
do
{
if ((type == 0) || ((id & TTWATCH_FILE_TYPE_MASK) == type))
files.push_back(std::pair<uint32_t,uint32_t>(id, length));
}
while ((result = ttwatch_find_next_file(watch, &id, &length)) == TTWATCH_NoError);
RETURN_ERROR(ttwatch_find_close(watch));
return (result == TTWATCH_NoMoreFiles) ? TTWATCH_NoError : result;
}
extern "C"
{
//------------------------------------------------------------------------------
// device functions
void ttwatch_enumerate_devices(TTWATCH_DEVICE_ENUMERATOR enumerator, void *data)
{
libusb_device **list = 0;
ssize_t count = libusb_get_device_list(NULL, &list);
for (ssize_t i = 0; i < count; ++i)
{
TTWATCH *watch;
if (ttwatch_open_device(list[i], 0, &watch) == TTWATCH_NoError)
{
if (enumerator(watch, data))
ttwatch_close(watch);
else
break;
}
}
libusb_free_device_list(list, 1);
}
//------------------------------------------------------------------------------
int ttwatch_open(const char *serial_or_name, TTWATCH **watch)
{
libusb_device **list = 0;
ssize_t count = libusb_get_device_list(NULL, &list);
for (ssize_t i = 0; i < count; ++i)
{
if (ttwatch_open_device(list[i], serial_or_name, watch) == TTWATCH_NoError)
break;
}
libusb_free_device_list(list, 1);
return *watch ? TTWATCH_NoError : TTWATCH_NoMatchingWatch;
}
//------------------------------------------------------------------------------
int ttwatch_open_device(libusb_device *device, const char *serial_or_name, TTWATCH **watch)
{
struct libusb_device_descriptor desc;
int result;
libusb_device_handle *handle;
char serial[64];
char name[64];
int count;
int attempts = 0;
// get the device descriptor
libusb_get_device_descriptor(device, &desc);
// ignore any non-TomTom devices
// PID 0x7474 is Multisport and Multisport Cardio
if ((desc.idVendor != TOMTOM_VENDOR_ID) ||
((desc.idProduct != TOMTOM_MULTISPORT_PRODUCT_ID) &&
!IS_SPARK(desc.idProduct)))
{
*watch = 0;
return TTWATCH_NotAWatch;
}
// open the device so we can read the serial number
if (libusb_open(device, &handle))
{
*watch = 0;
return TTWATCH_UnableToOpenDevice;
}
*watch = (TTWATCH*)calloc(1, sizeof(TTWATCH));
(*watch)->device = handle;
(*watch)->usb_product_id = desc.idProduct;
// Claim the device interface. If the device is busy (such as opened
// by a daemon), wait up to 60 seconds for it to become available
while (attempts++ < 60)
{
// detach the kernel HID driver, otherwise we can't do anything
if (libusb_kernel_driver_active(handle, 0))
{
if (!libusb_detach_kernel_driver(handle, 0))
(*watch)->attach_kernel_driver = 1;
}
if ((result = libusb_claim_interface(handle, 0)) != 0)
{
if (result == LIBUSB_ERROR_BUSY)
sleep(1);
else
{
ttwatch_close(*watch);
*watch = 0;
return TTWATCH_UnableToOpenDevice;
}
}
else
break;
}
// if we have finished the attempts and it's still busy, abort
if (result)
{
ttwatch_close(*watch);
*watch = 0;
return TTWATCH_UnableToOpenDevice;
}
// get the watch serial number
count = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber,
(unsigned char*)(*watch)->serial_number, sizeof((*watch)->serial_number));
if (count > 0)
((char*)(*watch)->serial_number)[count] = 0;
RETURN_ERROR(ttwatch_send_startup_message_group(*watch));
// get the watch name
if (ttwatch_get_watch_name(*watch, name, sizeof(name)) != TTWATCH_NoError)
name[0] = 0;
// see if we can match the watch serial number or name
if (!serial_or_name ||
(strcasecmp(serial_or_name, serial) == 0) ||
(strcasecmp(serial_or_name, name) == 0))
{
ttwatch_get_product_id(*watch, &(*watch)->product_id);
ttwatch_get_firmware_version(*watch, &(*watch)->firmware_version);
ttwatch_get_ble_version(*watch, &(*watch)->ble_version);
return TTWATCH_NoError;
}
else
{
ttwatch_close(*watch);
*watch = 0;
return TTWATCH_NoMatchingWatch;
}
}
//------------------------------------------------------------------------------
int ttwatch_close(TTWATCH *watch)
{
if (!watch)
return TTWATCH_InvalidParameter;
if (watch->preferences_changed)
RETURN_ERROR(ttwatch_write_preferences(watch));
if (watch->manifest_changed)
RETURN_ERROR(ttwatch_write_manifest(watch));
libusb_release_interface(watch->device, 0);
if (watch->attach_kernel_driver)
libusb_attach_kernel_driver(watch->device, 0);
libusb_close(watch->device);
if (watch->preferences_file)
free(watch->preferences_file);
if (watch->manifest_file)
free(watch->manifest_file);
free(watch);
return TTWATCH_NoError;
}
//------------------------------------------------------------------------------
// file functions
int ttwatch_open_file(TTWATCH *watch, uint32_t id, int read, TTWATCH_FILE **file)
{
if (!watch)
return TTWATCH_InvalidParameter;
if (watch->current_file)
return TTWATCH_FileOpen;
TXFileOperationPacket request = { TT_BIGENDIAN(id) };
RXFileOperationPacket response = { 0, 0, { 0, 0 }, 0 };
RETURN_ERROR(send_packet(watch, read ? MSG_OPEN_FILE_READ : MSG_OPEN_FILE_WRITE,
sizeof(request), (uint8_t*)&request, sizeof(response), (uint8_t*)&response));
if ((request.id != response.id) || (TT_BIGENDIAN(response.error) != 0))
return TTWATCH_InvalidResponse;
*file = (TTWATCH_FILE*)malloc(sizeof(TTWATCH_FILE));
(*file)->watch = watch;
(*file)->file_id = id;
watch->current_file = id;
return TTWATCH_NoError;
}
//------------------------------------------------------------------------------
int ttwatch_close_file(TTWATCH_FILE *file)
{
if (!file->watch)
return TTWATCH_InvalidParameter;
if (!file->watch->current_file)
return TTWATCH_FileNotOpen;
TXFileOperationPacket request = { TT_BIGENDIAN(file->file_id) };
RXFileOperationPacket response = { 0, 0, { 0, 0 }, 0 };
RETURN_ERROR(send_packet(file->watch, MSG_CLOSE_FILE, sizeof(request),
(uint8_t*)&request, sizeof(response), (uint8_t*)&response));
file->watch->current_file = 0;
return TTWATCH_NoError;
}
//------------------------------------------------------------------------------
int ttwatch_delete_file(TTWATCH *watch, uint32_t id)
{
if (!watch)
return TTWATCH_InvalidParameter;
if (watch->current_file)
return TTWATCH_FileOpen;
TXFileOperationPacket request = { TT_BIGENDIAN(id) };
RXFileOperationPacket response = { 0, 0, { 0, 0 }, 0 };
return send_packet(watch, MSG_DELETE_FILE, sizeof(request),
(uint8_t*)&request, sizeof(response), (uint8_t*)&response);
}
//------------------------------------------------------------------------------
int ttwatch_get_file_size(TTWATCH_FILE *file, uint32_t *size)
{
if (!file)
return TTWATCH_InvalidParameter;
if (!file->watch->current_file)
return TTWATCH_FileNotOpen;
TXFileOperationPacket request = { TT_BIGENDIAN(file->file_id) };
RXGetFileSizePacket response = { 0, 0, 0, 0, 0 };
RETURN_ERROR(send_packet(file->watch, MSG_GET_FILE_SIZE, sizeof(request),
(uint8_t*)&request, sizeof(response), (uint8_t*)&response));
*size = TT_BIGENDIAN(response.file_size);
return TTWATCH_NoError;
}
//------------------------------------------------------------------------------
int ttwatch_read_file_data(TTWATCH_FILE *file, void *data, uint32_t length)
{
if (!file)
return TTWATCH_InvalidParameter;
if (!file->watch->current_file)
return TTWATCH_FileNotOpen;
TXReadFileDataPacket request = { TT_BIGENDIAN(file->file_id), TT_BIGENDIAN(length) };
RXReadFileDataPacket response = { 0, 0, { 0 } };
RETURN_ERROR(send_packet(file->watch, MSG_READ_FILE_DATA_REQUEST, sizeof(request),
(uint8_t*)&request, length + 8, (uint8_t*)&response));
if (request.id != response.id)
return TTWATCH_InvalidResponse;
memcpy(data, response.data, length);
return TTWATCH_NoError;
}
//------------------------------------------------------------------------------
int ttwatch_write_file_data(TTWATCH_FILE *file, const void *data, uint32_t length)
{
if (!file)
return TTWATCH_InvalidParameter;
if (!file->watch->current_file)
return TTWATCH_FileNotOpen;
TXWriteFileDataPacket request = { TT_BIGENDIAN(file->file_id), { 0 } };
RXWriteFileDataPacket response = { 0, 0, { 0 } };
memcpy(request.data, data, length);
RETURN_ERROR(send_packet(file->watch, MSG_WRITE_FILE_DATA, length + 4,
(uint8_t*)&request, sizeof(response), (uint8_t*)&response));
return (request.id != response.id) ? TTWATCH_InvalidResponse : TTWATCH_NoError;
}
//------------------------------------------------------------------------------
int ttwatch_find_first_file(TTWATCH *watch, uint32_t *file_id, uint32_t *length)
{
if (!watch)
return TTWATCH_InvalidParameter;
if (watch->current_file)
return TTWATCH_FileOpen;
TXFindFirstFilePacket request = { { 0, 0 }};
RXFindFilePacket response = { 0, 0, 0, 0, 0 };
RETURN_ERROR(send_packet(watch, MSG_FIND_FIRST_FILE, sizeof(request),
(uint8_t*)&request, sizeof(response), (uint8_t*)&response));
if (file_id) *file_id = TT_BIGENDIAN(response.id);
if (length) *length = TT_BIGENDIAN(response.file_size);
return TT_BIGENDIAN(response.end_of_list) ? TTWATCH_NoMoreFiles : TTWATCH_NoError;
}
//------------------------------------------------------------------------------
int ttwatch_find_next_file(TTWATCH *watch, uint32_t *file_id, uint32_t *length)
{
if (!watch)
return TTWATCH_InvalidParameter;
if (watch->current_file)
return TTWATCH_FileOpen;
RXFindFilePacket response = { 0, 0, 0, 0, 0 };
RETURN_ERROR(send_packet(watch, MSG_FIND_NEXT_FILE, 0, 0,
sizeof(response), (uint8_t*)&response));
if (file_id) *file_id = TT_BIGENDIAN(response.id);
if (length) *length = TT_BIGENDIAN(response.file_size);
return TT_BIGENDIAN(response.end_of_list) ? TTWATCH_NoMoreFiles : TTWATCH_NoError;
}
//------------------------------------------------------------------------------
int ttwatch_find_close(TTWATCH *watch)
{
if (!watch)
return TTWATCH_InvalidParameter;
if (watch->current_file)
return TTWATCH_FileOpen;
return ttwatch_send_message_group_1(watch);
}
//------------------------------------------------------------------------------
int ttwatch_enumerate_files(TTWATCH *watch, uint32_t type, TTWATCH_FILE_ENUMERATOR enumerator, void *data)
{
FileList files;
RETURN_ERROR(enum_files(watch, type, files));
foreach (it, files)
enumerator(it->first, it->second, data);
return TTWATCH_NoError;
}
//------------------------------------------------------------------------------
int ttwatch_read_whole_file(TTWATCH *watch, uint32_t id, void **data, uint32_t *length)
{
uint8_t *ptr;
uint32_t size;
TTWATCH_FILE *file;
int result = TTWATCH_NoData;
RETURN_ERROR(ttwatch_open_file(watch, id, true, &file));
RETURN_ERROR(ttwatch_get_file_size(file, &size));
if (length)
*length = size;
if (size > 0)
{
uint16_t packet_size;
if (watch->usb_product_id == TOMTOM_MULTISPORT_PRODUCT_ID)
packet_size = 50;
else if (IS_SPARK(watch->usb_product_id))
packet_size = 242;
*data = malloc(size);
ptr = (uint8_t*)*data;
while (size > 0)
{
int len = (size > packet_size) ? packet_size : size;
if ((result = ttwatch_read_file_data(file, ptr, len)) != TTWATCH_NoError)
{
free(*data);
*data = 0;
break;
}
ptr += len;
size -= len;
}
}
RETURN_ERROR(ttwatch_close_file(file));
return result;
}
//------------------------------------------------------------------------------
int ttwatch_write_whole_file(TTWATCH *watch, uint32_t id, const void *data, uint32_t length)
{
TTWATCH_FILE *file;
if (ttwatch_open_file(watch, id, true, &file) == TTWATCH_NoError)
{
RETURN_ERROR(ttwatch_close_file(file));
RETURN_ERROR(ttwatch_delete_file(watch, id));
}
RETURN_ERROR(ttwatch_open_file(watch, id, false, &file));
uint16_t packet_size;
if (watch->usb_product_id == TOMTOM_MULTISPORT_PRODUCT_ID)
packet_size = 54;
else if (IS_SPARK(watch->usb_product_id))
packet_size = 246;
uint8_t *ptr = (uint8_t*)data;
int result = TTWATCH_NoError;
while (length > 0)
{
int len = (length > packet_size) ? packet_size : length;
if ((result = ttwatch_write_file_data(file, ptr, len)) != TTWATCH_NoError)
break;
length -= len;
ptr += len;
}
RETURN_ERROR(ttwatch_close_file(file));
return result;
}
//------------------------------------------------------------------------------
int ttwatch_write_verify_whole_file(TTWATCH *watch, uint32_t id, const void *data, uint32_t length)
{
uint32_t read_size;
void *read_data;
RETURN_ERROR(ttwatch_write_whole_file(watch, id, data, length));
RETURN_ERROR(ttwatch_read_whole_file(watch, id, &read_data, &read_size));
if ((read_size != length) || memcmp(data, read_data, length))
return TTWATCH_VerifyError;
return TTWATCH_NoError;
}
//------------------------------------------------------------------------------
// general functions
int ttwatch_reset_gps_processor(TTWATCH *watch)
{
if (!watch)
return TTWATCH_InvalidParameter;
if (watch->current_file)
return TTWATCH_FileOpen;
RXRebootWatchPacket response = { { 0 } };
return send_packet(watch, MSG_RESET_GPS_PROCESSOR, 0, 0, 60, (uint8_t*)&response);
}
//------------------------------------------------------------------------------
int ttwatch_reset_watch(TTWATCH *watch)
{
if (!watch)
return TTWATCH_InvalidParameter;
if (watch->current_file)
return TTWATCH_FileOpen;
// this message has no reply
send_packet(watch, MSG_RESET_DEVICE, 0, 0, 0, 0);
return TTWATCH_NoError;
}
//------------------------------------------------------------------------------
int ttwatch_get_watch_time(TTWATCH *watch, time_t *time)
{
if (!watch)
return TTWATCH_InvalidParameter;
if (watch->current_file)
return TTWATCH_FileOpen;
RXGetCurrentTimePacket response = { 0, { 0 } };
RETURN_ERROR(send_packet(watch, MSG_GET_CURRENT_TIME, 0, 0, sizeof(response), (uint8_t*)&response));
*time = TT_BIGENDIAN(response.utc_time);
return TTWATCH_NoError;
}
//------------------------------------------------------------------------------
int ttwatch_get_serial_number(TTWATCH *watch, char *serial, size_t max_length)
{
if (!watch)
return TTWATCH_InvalidParameter;
if (watch->current_file)
return TTWATCH_FileOpen;
libusb_device_descriptor desc;
libusb_get_device_descriptor(libusb_get_device(watch->device), &desc);
size_t count = libusb_get_string_descriptor_ascii(watch->device, desc.iSerialNumber, (unsigned char*)serial, max_length);
if (count < max_length)
serial[count] = 0;
return TTWATCH_NoError;
}
//------------------------------------------------------------------------------
int ttwatch_get_product_id(TTWATCH *watch, uint32_t *product_id)
{
if (!watch)
return TTWATCH_InvalidParameter;
if (watch->current_file)
return TTWATCH_FileOpen;
RXGetProductIDPacket response = {0};
RETURN_ERROR(send_packet(watch, MSG_GET_PRODUCT_ID, 0, 0, sizeof(response), (uint8_t*)&response));
*product_id = TT_BIGENDIAN(response.product_id);
return TTWATCH_NoError;
}
//------------------------------------------------------------------------------
int ttwatch_get_firmware_version(TTWATCH *watch, uint32_t *firmware_version)
{
if (!watch)
return TTWATCH_InvalidParameter;
if (watch->current_file)
return TTWATCH_FileOpen;
RXGetFirmwareVersionPacket response = {0};
RETURN_ERROR(send_packet(watch, MSG_GET_FIRMWARE_VERSION, 0, 0, 64, (uint8_t*)&response));
unsigned major, minor, build;
if (sscanf(response.version, "%u.%u.%u", &major, &minor, &build) != 3)
return TTWATCH_ParseError;
if (firmware_version)
*firmware_version = (major << 16) | (minor << 8) | build;
return TTWATCH_NoError;
}
//------------------------------------------------------------------------------
int ttwatch_get_ble_version(TTWATCH *watch, uint32_t *ble_version)
{
if (!watch)
return TTWATCH_InvalidParameter;
if (watch->current_file)
return TTWATCH_FileOpen;
RXGetBLEVersionPacket response;
RETURN_ERROR(send_packet(watch, MSG_GET_BLE_VERSION, 0, 0, sizeof(response), (uint8_t*)&response));
if (ble_version)
*ble_version = TT_BIGENDIAN(response.ble_version);
return TTWATCH_NoError;
}
//------------------------------------------------------------------------------
int ttwatch_send_message_group_1(TTWATCH *watch)
{
if (!watch)
return TTWATCH_InvalidParameter;
if (watch->current_file)
return TTWATCH_FileOpen;
RETURN_ERROR(send_packet(watch, MSG_UNKNOWN_1A, 0, 0, 4, 0));
RETURN_ERROR(send_packet(watch, MSG_FIND_CLOSE, 0, 0, 0, 0));
RETURN_ERROR(send_packet(watch, MSG_UNKNOWN_23, 0, 0, 3, 0));
RETURN_ERROR(send_packet(watch, MSG_UNKNOWN_23, 0, 0, 3, 0));
return TTWATCH_NoError;
}
//------------------------------------------------------------------------------
int ttwatch_send_startup_message_group(TTWATCH *watch)
{
if (!watch)
return TTWATCH_InvalidParameter;
if (watch->current_file)
return TTWATCH_FileOpen;
RETURN_ERROR(send_packet(watch, MSG_UNKNOWN_0D, 0, 0, 20, 0));
RETURN_ERROR(send_packet(watch, MSG_FIND_CLOSE, 0, 0, 0, 0));
RETURN_ERROR(send_packet(watch, MSG_UNKNOWN_22, 0, 0, 1, 0));
RETURN_ERROR(send_packet(watch, MSG_UNKNOWN_22, 0, 0, 1, 0));
RETURN_ERROR(send_packet(watch, MSG_GET_PRODUCT_ID, 0, 0, 4, 0));
RETURN_ERROR(send_packet(watch, MSG_FIND_CLOSE, 0, 0, 0, 0));
RETURN_ERROR(send_packet(watch, MSG_GET_BLE_VERSION, 0, 0, 4, 0));
RETURN_ERROR(send_packet(watch, MSG_GET_BLE_VERSION, 0, 0, 4, 0));
RETURN_ERROR(send_packet(watch, MSG_UNKNOWN_1F, 0, 0, 4, 0));
return TTWATCH_NoError;
}
//------------------------------------------------------------------------------
int ttwatch_clear_data(TTWATCH *watch)
{
if (!watch)
return TTWATCH_InvalidParameter;
if (watch->current_file)
return TTWATCH_FileOpen;
FileList files;
enum_files(watch, 0, files);
foreach (it, files)
{
uint32_t length;
TTWATCH_HISTORY_FILE *history;
switch (it->first & TTWATCH_FILE_TYPE_MASK)
{
case TTWATCH_FILE_TTBIN_DATA:
case TTWATCH_FILE_RACE_HISTORY_DATA:
case TTWATCH_FILE_HISTORY_DATA:
RETURN_ERROR(ttwatch_delete_file(watch, it->first));
break;
case TTWATCH_FILE_HISTORY_SUMMARY:
RETURN_ERROR(ttwatch_read_whole_file(watch, it->first, (void**)&history, &length));
{
history->entry_count = 0;
length = sizeof(TTWATCH_HISTORY_FILE) - 1;
int res = ttwatch_write_verify_whole_file(watch, it->first, history, length);
free(history);
RETURN_ERROR(res);
}
break;
}
}
return TTWATCH_NoError;
}
//------------------------------------------------------------------------------
int ttwatch_format(TTWATCH *watch)
{
if (!watch)
return TTWATCH_InvalidParameter;
RXFormatWatchPacket response;
RETURN_ERROR(send_packet(watch, MSG_FORMAT_WATCH, 0, 0, sizeof(response), (uint8_t*)&response));
if (TT_BIGENDIAN(response.error) != 0)
return TTWATCH_InvalidResponse;
return TTWATCH_NoError;
}
//------------------------------------------------------------------------------
// preferences file functions
int ttwatch_create_default_preferences_file(TTWATCH *watch)
{
static const char *DEFAULT_PREFERENCES_FILE =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
"<preferences version=\"1\" modified=\"\">\r\n"
" <watchName></watchName>\r\n"
" <SyncTimeToPC>1</SyncTimeToPC>\r\n"
" <SendAnonymousData>0</SendAnonymousData>\r\n"
" <WatchWindowMinimized>0</WatchWindowMinimized>\r\n"
" <ConfigURL>https://mysports.tomtom.com/service/config/config.json</ConfigURL>\r\n"
" <exporters>\r\n"
" <offline>\r\n"
" </offline>\r\n"
" </exporters>\r\n"
"</preferences>\r\n";
if (!watch)
return TTWATCH_InvalidParameter;
if (watch->preferences_file)
free(watch->preferences_file);
watch->preferences_file = strdup(DEFAULT_PREFERENCES_FILE);
watch->preferences_file_length = strlen(DEFAULT_PREFERENCES_FILE);
watch->preferences_changed = true;
return ttwatch_update_preferences_modified_time(watch);
}
//------------------------------------------------------------------------------
int ttwatch_reload_preferences(TTWATCH *watch)
{
void *data;
uint32_t length;
RETURN_ERROR(ttwatch_read_whole_file(watch, TTWATCH_FILE_PREFERENCES_XML, &data, &length));
if (watch->preferences_file)
free(watch->preferences_file);
watch->preferences_file = (char*)realloc(data, length + 1);
watch->preferences_file[length] = 0;
watch->preferences_file_length = length;
watch->preferences_changed = false;
return TTWATCH_NoError;
}
//------------------------------------------------------------------------------
int ttwatch_write_preferences(TTWATCH *watch)
{
if (!watch)
return TTWATCH_InvalidParameter;
if (!watch->preferences_file)
return TTWATCH_NoData;
RETURN_ERROR(ttwatch_write_whole_file(watch, TTWATCH_FILE_PREFERENCES_XML,
(uint8_t*)watch->preferences_file, watch->preferences_file_length));
watch->preferences_changed = false;
return TTWATCH_NoError;
}
//------------------------------------------------------------------------------
int ttwatch_update_preferences_modified_time(TTWATCH *watch)
{
static const char *const DAYNAMES[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
static const char *const MONTHNAMES[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
char timestr[64];
time_t rawtime;
struct tm *timeinfo;
if (!watch)
return TTWATCH_InvalidParameter;
if (!watch->preferences_file)
return TTWATCH_NoData;
std::string file(watch->preferences_file, watch->preferences_file_length);
size_t start = file.find("modified=\"");
if (start == std::string::npos)
return TTWATCH_ParseError;