-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathgarmin.c
More file actions
733 lines (642 loc) · 20.5 KB
/
garmin.c
File metadata and controls
733 lines (642 loc) · 20.5 KB
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
/*
* Garmin Descent Mk1 USB storage downloading
*
* Copyright (C) 2018 Linus Torvalds
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#define NOGDI
#include <windows.h>
#include <io.h>
#include <fcntl.h>
#ifndef PATH_MAX
#define PATH_MAX MAX_PATH
#endif
#define dc_open _open
#define dc_read _read
#define dc_close _close
#define DC_O_RDONLY _O_RDONLY
#define DC_O_BINARY _O_BINARY
#else
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define dc_open open
#define dc_read read
#define dc_close close
#define DC_O_RDONLY O_RDONLY
#ifdef O_BINARY
#define DC_O_BINARY O_BINARY
#else
#define DC_O_BINARY 0
#endif
#endif
#include "platform.h"
#include "garmin.h"
#include "context-private.h"
#include "device-private.h"
#include "array.h"
#ifdef HAVE_LIBMTP
#include "libmtp.h"
#define GARMIN_VENDOR 0x091E
#define DESCENT_MK2 3258
// deal with ancient libmpt found on older Linux distros
#ifndef LIBMTP_FILES_AND_FOLDERS_ROOT
#define LIBMTP_FILES_AND_FOLDERS_ROOT 0xffffffff
#endif
#endif
typedef struct garmin_device_t {
dc_device_t base;
dc_iostream_t *iostream;
unsigned char fingerprint[FIT_NAME_SIZE];
unsigned int model;
#ifdef HAVE_LIBMTP
unsigned char use_mtp;
LIBMTP_mtpdevice_t *mtp_device;
#endif
} garmin_device_t;
// Ids can be found at https://developer.garmin.com/connect-iq/reference-guides/devices-reference/
// (look for 'Part Number')
const garmin_model_t garmin_models[] = {
{ "Descent™ G1 / G1 Solar", 4005, true },
{ "Descent™ G2", 4588, true },
{ "Descent™ Mk1", 2859, false },
{ "Descent™ Mk1 APAC", 2991, false },
{ "Descent™ Mk2(i)", 3258, true },
{ "Descent™ Mk2(i) APAC", 3702, true },
{ "Descent™ Mk2 S", 3542, true },
{ "Descent™ Mk2 S APAC", 3930, true },
{ "Descent™ Mk3(i) 43mm", 4222, true },
{ "Descent™ Mk3(i) 51mm", 4223, true },
{ "Descent™ X50i", 4518, true },
{ NULL, 0, false }
};
static dc_status_t garmin_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size);
static dc_status_t garmin_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata);
static dc_status_t garmin_device_close (dc_device_t *abstract);
static const dc_device_vtable_t garmin_device_vtable = {
sizeof(garmin_device_t),
DC_FAMILY_GARMIN,
garmin_device_set_fingerprint, /* set_fingerprint */
NULL, /* read */
NULL, /* write */
NULL, /* dump */
garmin_device_foreach, /* foreach */
NULL, /* timesync */
garmin_device_close, /* close */
};
dc_status_t
garmin_device_open (dc_device_t **out, dc_context_t *context, dc_iostream_t *iostream, unsigned int model)
{
dc_status_t status = DC_STATUS_SUCCESS;
garmin_device_t *device = NULL;
if (out == NULL)
return DC_STATUS_INVALIDARGS;
// Allocate memory.
device = (garmin_device_t *) dc_device_allocate (context, &garmin_device_vtable);
if (device == NULL) {
ERROR (context, "Failed to allocate memory.");
return DC_STATUS_NOMEMORY;
}
// Set the default values.
device->iostream = iostream;
memset(device->fingerprint, 0, sizeof(device->fingerprint));
device->model = model;
#ifdef HAVE_LIBMTP
// for a Descent Mk2/Mk2i, we have to use MTP to access its storage;
// for Garmin devices, the model number corresponds to the lower three nibbles of the USB product ID
// in order to have only one entry for the Mk2, we don't use the Mk2/APAC model number in our code
device->use_mtp = model == DESCENT_MK2;
device->mtp_device = NULL;
#endif
*out = (dc_device_t *) device;
return DC_STATUS_SUCCESS;
}
static dc_status_t
garmin_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size)
{
garmin_device_t *device = (garmin_device_t *)abstract;
if (size && size != sizeof (device->fingerprint))
return DC_STATUS_INVALIDARGS;
if (size)
memcpy (device->fingerprint, data, sizeof (device->fingerprint));
else
memset (device->fingerprint, 0, sizeof (device->fingerprint));
return DC_STATUS_SUCCESS;
}
static dc_status_t
garmin_device_close (dc_device_t *abstract)
{
dc_status_t status = DC_STATUS_SUCCESS;
garmin_device_t *device = (garmin_device_t *) abstract;
#ifdef HAVE_LIBMTP
if (device->use_mtp && device->mtp_device)
LIBMTP_Release_Device(device->mtp_device);
#endif
return DC_STATUS_SUCCESS;
}
/*
* NOTE! The fingerprint is only the 24 first bytes of this,
* aka FIT_NAME_SIZE.
*/
#define FILE_NAME_SIZE 64
struct fit_file {
char name[FILE_NAME_SIZE + 1];
unsigned int mtp_id;
};
struct file_list {
int nr, allocated;
struct fit_file *array;
};
static int
char_to_int(char c)
{
if (c >= '0' && c <= '9')
return c - '0';
if (c >= 'a' && c <= 'z')
return c - 'a' + 10;
if (c >= 'A' && c <= 'Z')
return c - 'A' + 10;
return 0;
}
/* C4ND0302.fit -> 2022-04-23-13-03-02.fit */
static void
parse_short_name(const char *name, char *output)
{
sprintf(output, "%d-%02d-%02d-%02d-%02d-%02d.fit", char_to_int(name[0]) + 2010, // Year
char_to_int(name[1]), // Month
char_to_int(name[2]), // Day
char_to_int(name[3]), // Hour
char_to_int(name[4]) * 10 + char_to_int(name[5]), // Minute
char_to_int(name[6]) * 10 + char_to_int(name[7])); // Second
}
static int name_cmp(const void *_a, const void *_b)
{
const struct fit_file *a = _a;
const struct fit_file *b = _b;
const char *a_name = a->name;
const char *b_name = b->name;
char a_buffer[FILE_NAME_SIZE];
char b_buffer[FILE_NAME_SIZE];
if (strlen(a_name) == 12) {
parse_short_name(a_name, a_buffer);
a_name = a_buffer;
}
if (strlen(b_name) == 12) {
parse_short_name(b_name, b_buffer);
b_name = b_buffer;
}
// Sort reverse string ordering (newest first), so use 'b,a'
return strcmp(b_name, a_name);
}
/*
* Get the FIT file list and sort it.
*
* Return number of files found.
*/
static int
check_filename(dc_device_t *abstract, const char *name)
{
int len = strlen(name);
if (len < 5)
return 0;
if (len >= FILE_NAME_SIZE)
return 0;
if (strncasecmp(name + len - 4, ".FIT", 4))
return 0;
DEBUG(abstract->context, " %s - adding to list", name);
return 1;
}
static dc_status_t
make_space(struct file_list *files)
{
if (files->nr == files->allocated) {
struct fit_file *array;
int n = 3*(files->allocated + 8)/2;
size_t new_size;
new_size = n * sizeof(array[0]);
array = realloc(files->array, new_size);
if (!array)
return DC_STATUS_NOMEMORY;
files->array = array;
files->allocated = n;
}
return DC_STATUS_SUCCESS;
}
static void
add_name(struct file_list *files, const char *name, unsigned int mtp_id)
{
/*
* NOTE! This depends on the zero-padding that strncpy does.
*
* strncpy() doesn't just limit the size of the copy, it
* will zero-pad the end of the result buffer.
*/
struct fit_file *entry = files->array + files->nr++;
strncpy(entry->name, name, FILE_NAME_SIZE);
entry->name[FILE_NAME_SIZE] = 0; // ensure it's null-terminated
entry->mtp_id = mtp_id;
}
#ifdef _WIN32
static dc_status_t
get_file_list(dc_device_t *abstract, const char *pathname, struct file_list *files)
{
WIN32_FIND_DATAA findData;
HANDLE hFind;
char searchPath[PATH_MAX];
DEBUG(abstract->context, "Iterating over Garmin files");
// Create search pattern (pathname\*.fit)
snprintf(searchPath, sizeof(searchPath), "%s\\*", pathname);
hFind = FindFirstFileA(searchPath, &findData);
if (hFind == INVALID_HANDLE_VALUE) {
/* Directory does not exist; caller may retry with a fallback path.
* Nothing has been added to 'files' yet, so no cleanup needed. */
return DC_STATUS_NODEVICE;
}
do {
// Skip directories
if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
continue;
if (!check_filename(abstract, findData.cFileName))
continue;
dc_status_t rc = make_space(files);
if (rc != DC_STATUS_SUCCESS) {
FindClose(hFind);
return rc;
}
add_name(files, findData.cFileName, 0);
} while (FindNextFileA(hFind, &findData));
if (GetLastError() != ERROR_NO_MORE_FILES) {
FindClose(hFind);
return DC_STATUS_IO;
}
FindClose(hFind);
DEBUG(abstract->context, "Found %d files", files->nr);
if (files->array)
qsort(files->array, files->nr, sizeof(struct fit_file), name_cmp);
return DC_STATUS_SUCCESS;
}
#else
static dc_status_t
get_file_list(dc_device_t *abstract, DIR *dir, struct file_list *files)
{
struct dirent *de;
DEBUG(abstract->context, "Iterating over Garmin files");
while ((de = readdir(dir)) != NULL) {
if (!check_filename(abstract, de->d_name))
continue;
dc_status_t rc = make_space(files);
if (rc != DC_STATUS_SUCCESS)
return rc;
add_name(files, de->d_name, 0);
}
DEBUG(abstract->context, "Found %d files", files->nr);
if (files->array)
qsort(files->array, files->nr, sizeof(struct fit_file), name_cmp);
return DC_STATUS_SUCCESS;
}
#endif
#ifdef HAVE_LIBMTP
static unsigned int
mtp_get_folder_id(dc_device_t *abstract, LIBMTP_mtpdevice_t *device, LIBMTP_devicestorage_t *storage, const char *folder, unsigned int parent_id)
{
DEBUG(abstract->context, "Garmin/mtp: looking for folder %s under parent id %d", folder, parent_id);
// memory management is interesting here - we have to always walk the list returned and destroy them one by one
unsigned int folder_id = LIBMTP_FILES_AND_FOLDERS_ROOT;
LIBMTP_file_t* files = LIBMTP_Get_Files_And_Folders (device, storage->id, parent_id);
while (files != NULL) {
LIBMTP_file_t* mtp_file = files;
if (mtp_file->filetype == LIBMTP_FILETYPE_FOLDER && mtp_file->filename && !strncasecmp(mtp_file->filename, folder, strlen(folder))) {
folder_id = mtp_file->item_id;
}
files = files->next;
LIBMTP_destroy_file_t(mtp_file);
}
return folder_id;
}
static dc_status_t
mtp_get_file_list(dc_device_t *abstract, struct file_list *files)
{
garmin_device_t *device = (garmin_device_t *)abstract;
LIBMTP_raw_device_t *rawdevices;
int numrawdevices;
int i;
LIBMTP_Init();
DEBUG(abstract->context, "Attempting to connect to mtp device");
switch (LIBMTP_Detect_Raw_Devices(&rawdevices, &numrawdevices)) {
case LIBMTP_ERROR_NO_DEVICE_ATTACHED:
DEBUG(abstract->context, "Garmin/mtp: no device found");
return DC_STATUS_NODEVICE;
case LIBMTP_ERROR_CONNECTING:
DEBUG(abstract->context, "Garmin/mtp: error connecting");
return DC_STATUS_NOACCESS;
case LIBMTP_ERROR_MEMORY_ALLOCATION:
DEBUG(abstract->context, "Garmin/mtp: memory allocation error");
return DC_STATUS_NOMEMORY;
case LIBMTP_ERROR_GENERAL: // Unknown general errors - that's bad
default:
DEBUG(abstract->context, "Garmin/mtp: unknown error");
return DC_STATUS_UNSUPPORTED;
case LIBMTP_ERROR_NONE:
DEBUG(abstract->context, "Garmin/mtp: successfully connected with %d raw devices", numrawdevices);
}
/* iterate through connected MTP devices */
for (i = 0; i < numrawdevices; i++) {
LIBMTP_devicestorage_t *storage;
// we only want to read from a Garmin Descent Mk2 device at this point
if (rawdevices[i].device_entry.vendor_id != GARMIN_VENDOR) {
DEBUG(abstract->context, "Garmin/mtp: skipping raw device %04x/%04x",
rawdevices[i].device_entry.vendor_id, rawdevices[i].device_entry.product_id);
continue;
}
bool mtp_capable = false;
for (unsigned j = 0; garmin_models[j].name; j++) {
if ((garmin_models[j].id | 0x4000) == rawdevices[i].device_entry.product_id) {
mtp_capable = garmin_models[j].mtp_capable;
break;
}
}
if (!mtp_capable) {
DEBUG(abstract->context, "Garmin/mtp: skipping Garmin raw device %04x/%04x, as it is not a dive computer / does not support MTP",
rawdevices[i].device_entry.vendor_id, rawdevices[i].device_entry.product_id);
continue;
}
device->mtp_device = LIBMTP_Open_Raw_Device_Uncached(&rawdevices[i]);
if (device->mtp_device == NULL) {
DEBUG(abstract->context, "Garmin/mtp: unable to open raw device %d", i);
continue;
}
DEBUG(abstract->context, "Garmin/mtp: succcessfully opened device");
for (storage = device->mtp_device->storage; storage != 0; storage = storage->next) {
unsigned int garmin_id = mtp_get_folder_id(abstract, device->mtp_device, storage, "Garmin", LIBMTP_FILES_AND_FOLDERS_ROOT);
DEBUG(abstract->context, "Garmin/mtp: Garmin folder at file_id %d", garmin_id);
if (garmin_id == LIBMTP_FILES_AND_FOLDERS_ROOT)
continue; // this storage partition didn't have a Garmin folder
unsigned int activity_id = mtp_get_folder_id(abstract, device->mtp_device, storage, "Activity", garmin_id);
DEBUG(abstract->context, "Garmin/mtp: Activity folder at file_id %d", activity_id);
if (activity_id == LIBMTP_FILES_AND_FOLDERS_ROOT)
continue; // no Activity folder
// now walk that folder to create our file_list
LIBMTP_file_t* activity_files = LIBMTP_Get_Files_And_Folders (device->mtp_device, storage->id, activity_id);
while (activity_files != NULL) {
LIBMTP_file_t* mtp_file = activity_files;
if (mtp_file->filetype != LIBMTP_FILETYPE_FOLDER && mtp_file->filename) {
if (check_filename(abstract, mtp_file->filename)) {
dc_status_t rc = make_space(files);
if (rc != DC_STATUS_SUCCESS)
return rc;
add_name(files, mtp_file->filename, mtp_file->item_id);
}
}
activity_files = activity_files->next;
LIBMTP_destroy_file_t(mtp_file);
}
}
}
free(rawdevices);
DEBUG(abstract->context, "Found %d files", files->nr);
if (files->array)
qsort(files->array, files->nr, sizeof(struct fit_file), name_cmp);
return DC_STATUS_SUCCESS;
}
// MTP hands us the file data in chunks which we then just add to our data buffer
static uint16_t
mtp_put_func(void* params, void* priv, uint32_t sendlen, unsigned char *data, uint32_t *putlen)
{
dc_buffer_t *file = (dc_buffer_t *)priv;
dc_buffer_append(file, data, sendlen);
if (putlen)
*putlen = sendlen;
return 0;
}
// read the file from the MTP device and store the content in the data buffer
static dc_status_t
mtp_read_file(garmin_device_t *device, unsigned int file_id, dc_buffer_t *file)
{
dc_status_t rc = DC_STATUS_SUCCESS;
if (!device->mtp_device) {
DEBUG(device->base.context, "Garmin/mtp: cannot read file without MTP device");
return DC_STATUS_NODEVICE;
}
DEBUG(device->base.context, "Garmin/mtp: call Get_File_To_Handler");
if (LIBMTP_Get_File_To_Handler(device->mtp_device, file_id, &mtp_put_func, (void *) file, NULL, NULL) != 0) {
LIBMTP_Dump_Errorstack(device->mtp_device);
return DC_STATUS_IO;
}
return rc;
}
#endif /* HAVE_LIBMTP */
static dc_status_t
read_file(char *pathname, int pathlen, const char *name, dc_buffer_t *file)
{
int fd, rc;
pathname[pathlen] = '/';
memcpy(pathname+pathlen+1, name, FILE_NAME_SIZE);
fd = dc_open(pathname, DC_O_RDONLY | DC_O_BINARY);
if (fd < 0)
return DC_STATUS_IO;
rc = DC_STATUS_SUCCESS;
for (;;) {
char buffer[4096];
int n;
n = dc_read(fd, buffer, sizeof(buffer));
if (!n)
break;
if (n > 0) {
dc_buffer_append(file, buffer, n);
continue;
}
rc = DC_STATUS_IO;
break;
}
dc_close(fd);
return rc;
}
static dc_status_t
garmin_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata)
{
dc_status_t status = DC_STATUS_SUCCESS;
garmin_device_t *device = (garmin_device_t *) abstract;
char pathname[PATH_MAX];
char pathname_input[PATH_MAX];
size_t pathlen;
struct file_list files = {
0, // nr
0, // allocated
NULL // array of file names / ids
};
dc_buffer_t *file;
#ifndef _WIN32
DIR *dir;
#endif
dc_status_t rc;
// Read the directory name from the iostream
rc = dc_iostream_read(device->iostream, &pathname_input, sizeof(pathname_input)-1, &pathlen);
if (rc != DC_STATUS_SUCCESS)
return rc;
pathname_input[pathlen] = 0;
#ifdef HAVE_LIBMTP
// if the user passes in a path, don't try to read via MTP
if (pathlen)
device->use_mtp = 0;
#endif
// The actual dives are under the "Garmin/Activity/" directory
// as FIT files, with names like "2018-08-20-10-23-30.fit".
// Make sure our buffer is big enough.
if (pathlen + strlen("/Garmin/Activity/") + FILE_NAME_SIZE + 2 > PATH_MAX) {
ERROR (abstract->context, "Invalid Garmin base directory '%s'", pathname_input);
return DC_STATUS_IO;
}
strcpy(pathname, pathname_input);
if (pathlen && pathname[pathlen-1] != '/')
pathname[pathlen++] = '/';
strcpy(pathname + pathlen, "Garmin/Activity");
pathlen += strlen("Garmin/Activity");
#ifdef HAVE_LIBMTP
if (device->use_mtp) {
rc = mtp_get_file_list(abstract, &files);
if (rc != DC_STATUS_SUCCESS || !files.nr) {
free(files.array);
return rc;
}
} else
#endif
{ // slight coding style violation to deal with the non-MTP case
#ifdef _WIN32
// On Windows, get_file_list takes the pathname directly
rc = get_file_list(abstract, pathname, &files);
if (rc == DC_STATUS_NODEVICE) {
/* Garmin/Activity directory not found; 'files' is untouched.
* Try the input path directly as a fallback. */
rc = get_file_list(abstract, pathname_input, &files);
if (rc != DC_STATUS_SUCCESS) {
ERROR (abstract->context, "Failed to open directory '%s' or '%s'.", pathname, pathname_input);
free(files.array);
return rc;
}
strcpy(pathname, pathname_input);
pathlen = strlen(pathname);
} else if (rc != DC_STATUS_SUCCESS) {
/* Real error (e.g. DC_STATUS_NOMEMORY or mid-enumeration IO
* error): files may be partially populated, don't retry. */
free(files.array);
return rc;
}
if (!files.nr) {
free(files.array);
return rc;
}
#else
dir = opendir(pathname);
if (!dir) {
dir = opendir(pathname_input);
if (!dir) {
ERROR (abstract->context, "Failed to open directory '%s' or '%s'.", pathname, pathname_input);
return DC_STATUS_IO;
}
strcpy(pathname, pathname_input);
pathlen = strlen(pathname);
}
// Get the list of FIT files
rc = get_file_list(abstract, dir, &files);
closedir(dir);
if (rc != DC_STATUS_SUCCESS || !files.nr) {
free(files.array);
return rc;
}
#endif
}
// We found at least one file
// Can we find the fingerprint entry?
for (int i = 0; i < files.nr; i++) {
const char *name = files.array[i].name;
if (memcmp(name, device->fingerprint, sizeof (device->fingerprint)))
continue;
// Found fingerprint, just cut the array short here
files.nr = i;
DEBUG(abstract->context, "Ignoring '%s' and older", name);
break;
}
// Enable progress notifications.
dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER;
progress.maximum = files.nr;
progress.current = 0;
device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);
file = dc_buffer_new (16384);
if (file == NULL) {
ERROR (abstract->context, "Insufficient buffer space available.");
free(files.array);
return DC_STATUS_NOMEMORY;
}
dc_event_devinfo_t devinfo;
dc_event_devinfo_t *devinfo_p = &devinfo;
for (int i = 0; i < files.nr; i++) {
const char *name = files.array[i].name;
dc_parser_t *parser;
const unsigned char *data;
unsigned int size;
short is_dive = 0;
if (device_is_cancelled(abstract)) {
status = DC_STATUS_CANCELLED;
break;
}
// Reset the membuffer, read the data
dc_buffer_clear(file);
#ifdef HAVE_LIBMTP
if (device->use_mtp)
status = mtp_read_file(device, files.array[i].mtp_id, file);
else
#endif
status = read_file(pathname, pathlen, name, file);
if (status != DC_STATUS_SUCCESS)
break;
data = dc_buffer_get_data(file);
size = dc_buffer_get_size(file);
status = garmin_parser_create(&parser, abstract->context, data, size);
if (status != DC_STATUS_SUCCESS) {
ERROR (abstract->context, "Failed to create parser for dive verification.");
free(files.array);
return rc;
}
is_dive = !device->model || garmin_parser_is_dive(parser, devinfo_p);
if (devinfo_p) {
// first time we came through here, let's emit the
// devinfo and vendor events
device_event_emit (abstract, DC_EVENT_DEVINFO, devinfo_p);
devinfo_p = NULL;
}
if (!is_dive) {
DEBUG(abstract->context, "decided %s isn't a dive.", name);
dc_parser_destroy(parser);
continue;
}
if (callback && !callback(data, size, name, FIT_NAME_SIZE, userdata))
break;
progress.current++;
device_event_emit(abstract, DC_EVENT_PROGRESS, &progress);
dc_parser_destroy(parser);
}
free(files.array);
dc_buffer_free(file);
return status;
}