subrascasse

Sub Rascasse: an iPhone dive log for Subsurface cloud storage, with a pure-Swift git client (GPL-2.0)
git clone https://git.schurger.org/subrascasse.git
Log | Files | Refs | README | LICENSE

garmin.c (20386B)


      1 /*
      2  * Garmin Descent Mk1 USB storage downloading
      3  *
      4  * Copyright (C) 2018 Linus Torvalds
      5  *
      6  * This library is free software; you can redistribute it and/or
      7  * modify it under the terms of the GNU Lesser General Public
      8  * License as published by the Free Software Foundation; either
      9  * version 2.1 of the License, or (at your option) any later version.
     10  *
     11  * This library is distributed in the hope that it will be useful,
     12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     14  * Lesser General Public License for more details.
     15  *
     16  * You should have received a copy of the GNU Lesser General Public
     17  * License along with this library; if not, write to the Free Software
     18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
     19  * MA 02110-1301 USA
     20  */
     21 
     22 #include <stdio.h>
     23 #include <stdlib.h>
     24 #include <string.h>
     25 
     26 #ifdef _WIN32
     27 #define WIN32_LEAN_AND_MEAN
     28 #define NOGDI
     29 #include <windows.h>
     30 #include <io.h>
     31 #include <fcntl.h>
     32 #ifndef PATH_MAX
     33 #define PATH_MAX MAX_PATH
     34 #endif
     35 #define dc_open _open
     36 #define dc_read _read
     37 #define dc_close _close
     38 #define DC_O_RDONLY _O_RDONLY
     39 #define DC_O_BINARY _O_BINARY
     40 #else
     41 #include <dirent.h>
     42 #include <sys/types.h>
     43 #include <sys/stat.h>
     44 #include <fcntl.h>
     45 #include <unistd.h>
     46 #define dc_open open
     47 #define dc_read read
     48 #define dc_close close
     49 #define DC_O_RDONLY O_RDONLY
     50 #ifdef O_BINARY
     51 #define DC_O_BINARY O_BINARY
     52 #else
     53 #define DC_O_BINARY 0
     54 #endif
     55 #endif
     56 
     57 #include "platform.h"
     58 #include "garmin.h"
     59 #include "garmin-models.h"
     60 #include "context-private.h"
     61 #include "device-private.h"
     62 #include "array.h"
     63 
     64 #ifdef HAVE_LIBMTP
     65 #include "libmtp.h"
     66 
     67 #define GARMIN_VENDOR      0x091E
     68 
     69 // deal with ancient libmpt found on older Linux distros
     70 #ifndef LIBMTP_FILES_AND_FOLDERS_ROOT
     71 #define LIBMTP_FILES_AND_FOLDERS_ROOT 0xffffffff
     72 #endif
     73 #endif
     74 
     75 typedef struct garmin_device_t {
     76 	dc_device_t base;
     77 	dc_iostream_t *iostream;
     78 	unsigned char fingerprint[FIT_NAME_SIZE];
     79 	unsigned int model;
     80 #ifdef HAVE_LIBMTP
     81 	unsigned char use_mtp;
     82 	LIBMTP_mtpdevice_t *mtp_device;
     83 #endif
     84 } garmin_device_t;
     85 
     86 #define GARMIN_MODEL(name, id, mtp_capable) {name, id, mtp_capable},
     87 const garmin_model_t garmin_models[] = {
     88 	GARMIN_MODEL_LIST(GARMIN_MODEL)
     89 	{ NULL, 0, false }
     90 };
     91 #undef GARMIN_MODEL
     92 
     93 // AI-generated (Claude)
     94 #ifdef HAVE_LIBMTP
     95 static const garmin_model_t *garmin_model_find(unsigned int id)
     96 {
     97 	unsigned int i;
     98 
     99 	for (i = 0; garmin_models[i].name; i++)
    100 		if (garmin_models[i].id == id)
    101 			return &garmin_models[i];
    102 
    103 	return NULL;
    104 }
    105 
    106 static bool garmin_model_is_mtp_capable(unsigned int id)
    107 {
    108 	const garmin_model_t *model = garmin_model_find(id);
    109 
    110 	return model && model->mtp_capable;
    111 }
    112 #endif
    113 
    114 static dc_status_t garmin_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size);
    115 static dc_status_t garmin_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata);
    116 static dc_status_t garmin_device_close (dc_device_t *abstract);
    117 
    118 static const dc_device_vtable_t garmin_device_vtable = {
    119 	sizeof(garmin_device_t),
    120 	DC_FAMILY_GARMIN,
    121 	garmin_device_set_fingerprint, /* set_fingerprint */
    122 	NULL, /* read */
    123 	NULL, /* write */
    124 	NULL, /* dump */
    125 	garmin_device_foreach, /* foreach */
    126 	NULL, /* timesync */
    127 	garmin_device_close, /* close */
    128 };
    129 
    130 dc_status_t
    131 garmin_device_open (dc_device_t **out, dc_context_t *context, dc_iostream_t *iostream, unsigned int model)
    132 {
    133 	dc_status_t status = DC_STATUS_SUCCESS;
    134 	garmin_device_t *device = NULL;
    135 
    136 	if (out == NULL)
    137 		return DC_STATUS_INVALIDARGS;
    138 
    139 	// Allocate memory.
    140 	device = (garmin_device_t *) dc_device_allocate (context, &garmin_device_vtable);
    141 	if (device == NULL) {
    142 		ERROR (context, "Failed to allocate memory.");
    143 		return DC_STATUS_NOMEMORY;
    144 	}
    145 	// Set the default values.
    146 	device->iostream = iostream;
    147 	memset(device->fingerprint, 0, sizeof(device->fingerprint));
    148 	device->model = model;
    149 
    150 #ifdef HAVE_LIBMTP
    151 	// Garmin USB product IDs are 0x4000 | FIT model IDs.
    152 	device->use_mtp = garmin_model_is_mtp_capable(model);
    153 	device->mtp_device = NULL;
    154 #endif
    155 
    156 	*out = (dc_device_t *) device;
    157 
    158 	return DC_STATUS_SUCCESS;
    159 }
    160 
    161 static dc_status_t
    162 garmin_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size)
    163 {
    164 	garmin_device_t *device = (garmin_device_t *)abstract;
    165 
    166 	if (size && size != sizeof (device->fingerprint))
    167 		return DC_STATUS_INVALIDARGS;
    168 
    169 	if (size)
    170 		memcpy (device->fingerprint, data, sizeof (device->fingerprint));
    171 	else
    172 		memset (device->fingerprint, 0, sizeof (device->fingerprint));
    173 
    174 	return DC_STATUS_SUCCESS;
    175 }
    176 
    177 
    178 static dc_status_t
    179 garmin_device_close (dc_device_t *abstract)
    180 {
    181 	dc_status_t status = DC_STATUS_SUCCESS;
    182 	garmin_device_t *device = (garmin_device_t *) abstract;
    183 #ifdef HAVE_LIBMTP
    184 	if (device->use_mtp && device->mtp_device)
    185 		LIBMTP_Release_Device(device->mtp_device);
    186 #endif
    187 	return DC_STATUS_SUCCESS;
    188 }
    189 
    190 /*
    191  * NOTE! The fingerprint is only the 24 first bytes of this,
    192  * aka FIT_NAME_SIZE.
    193  */
    194 #define FILE_NAME_SIZE 64
    195 
    196 struct fit_file {
    197 	char name[FILE_NAME_SIZE + 1];
    198 	unsigned int mtp_id;
    199 };
    200 
    201 struct file_list {
    202 	int nr, allocated;
    203 	struct fit_file *array;
    204 };
    205 
    206 static int
    207 char_to_int(char c)
    208 {
    209 	if (c >= '0' && c <= '9')
    210 		return c - '0';
    211 	if (c >= 'a' && c <= 'z')
    212 		return c - 'a' + 10;
    213 	if (c >= 'A' && c <= 'Z')
    214 		return c - 'A' + 10;
    215 	return 0;
    216 }
    217 
    218 /* C4ND0302.fit -> 2022-04-23-13-03-02.fit */
    219 static void
    220 parse_short_name(const char *name, char *output)
    221 {
    222 	sprintf(output, "%d-%02d-%02d-%02d-%02d-%02d.fit", char_to_int(name[0]) + 2010, // Year
    223 		char_to_int(name[1]), // Month
    224 		char_to_int(name[2]), // Day
    225 		char_to_int(name[3]), // Hour
    226 		char_to_int(name[4]) * 10 + char_to_int(name[5]), // Minute
    227 		char_to_int(name[6]) * 10 + char_to_int(name[7])); // Second
    228 }
    229 
    230 static int name_cmp(const void *_a, const void *_b)
    231 {
    232 	const struct fit_file *a = _a;
    233 	const struct fit_file *b = _b;
    234 
    235 	const char *a_name = a->name;
    236 	const char *b_name = b->name;
    237 
    238 	char a_buffer[FILE_NAME_SIZE];
    239 	char b_buffer[FILE_NAME_SIZE];
    240 
    241 	if (strlen(a_name) == 12) {
    242 		parse_short_name(a_name, a_buffer);
    243 		a_name = a_buffer;
    244 	}
    245 
    246 	if (strlen(b_name) == 12) {
    247 		parse_short_name(b_name, b_buffer);
    248 		b_name = b_buffer;
    249 	}
    250 
    251 	// Sort reverse string ordering (newest first), so use 'b,a'
    252 	return strcmp(b_name, a_name);
    253 }
    254 
    255 /*
    256  * Get the FIT file list and sort it.
    257  *
    258  * Return number of files found.
    259 */
    260 
    261 static int
    262 check_filename(dc_device_t *abstract, const char *name)
    263 {
    264 	int len = strlen(name);
    265 
    266 	if (len < 5)
    267 		return 0;
    268 	if (len >= FILE_NAME_SIZE)
    269 		return 0;
    270 	if (strncasecmp(name + len - 4, ".FIT", 4))
    271 		return 0;
    272 
    273 	DEBUG(abstract->context, "  %s - adding to list", name);
    274 	return 1;
    275 }
    276 
    277 static dc_status_t
    278 make_space(struct file_list *files)
    279 {
    280 	if (files->nr == files->allocated) {
    281 		struct fit_file *array;
    282 		int n = 3*(files->allocated + 8)/2;
    283 		size_t new_size;
    284 
    285 		new_size = n * sizeof(array[0]);
    286 		array = realloc(files->array, new_size);
    287 		if (!array)
    288 			return DC_STATUS_NOMEMORY;
    289 		files->array = array;
    290 		files->allocated = n;
    291 	}
    292 	return DC_STATUS_SUCCESS;
    293 }
    294 
    295 static void
    296 add_name(struct file_list *files, const char *name, unsigned int mtp_id)
    297 {
    298 	/*
    299 	 * NOTE! This depends on the zero-padding that strncpy does.
    300 	 *
    301 	 * strncpy() doesn't just limit the size of the copy, it
    302 	 * will zero-pad the end of the result buffer.
    303 	 */
    304 	struct fit_file *entry = files->array + files->nr++;
    305 	strncpy(entry->name, name, FILE_NAME_SIZE);
    306 	entry->name[FILE_NAME_SIZE] = 0; // ensure it's null-terminated
    307 	entry->mtp_id = mtp_id;
    308 }
    309 
    310 #ifdef _WIN32
    311 static dc_status_t
    312 get_file_list(dc_device_t *abstract, const char *pathname, struct file_list *files)
    313 {
    314 	WIN32_FIND_DATAA findData;
    315 	HANDLE hFind;
    316 	char searchPath[PATH_MAX];
    317 
    318 	DEBUG(abstract->context, "Iterating over Garmin files");
    319 
    320 	// Create search pattern (pathname\*.fit)
    321 	snprintf(searchPath, sizeof(searchPath), "%s\\*", pathname);
    322 
    323 	hFind = FindFirstFileA(searchPath, &findData);
    324 	if (hFind == INVALID_HANDLE_VALUE) {
    325 		return DC_STATUS_IO;
    326 	}
    327 
    328 	do {
    329 		// Skip directories
    330 		if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
    331 			continue;
    332 
    333 		if (!check_filename(abstract, findData.cFileName))
    334 			continue;
    335 
    336 		dc_status_t rc = make_space(files);
    337 		if (rc != DC_STATUS_SUCCESS) {
    338 			FindClose(hFind);
    339 			return rc;
    340 		}
    341 		add_name(files, findData.cFileName, 0);
    342 	} while (FindNextFileA(hFind, &findData));
    343 
    344 	if (GetLastError() != ERROR_NO_MORE_FILES) {
    345 		FindClose(hFind);
    346 		return DC_STATUS_IO;
    347 	}
    348 
    349 	FindClose(hFind);
    350 
    351 	DEBUG(abstract->context, "Found %d files", files->nr);
    352 
    353 	if (files->array)
    354 		qsort(files->array, files->nr, sizeof(struct fit_file), name_cmp);
    355 	return DC_STATUS_SUCCESS;
    356 }
    357 #else
    358 static dc_status_t
    359 get_file_list(dc_device_t *abstract, DIR *dir, struct file_list *files)
    360 {
    361 	struct dirent *de;
    362 
    363 	DEBUG(abstract->context, "Iterating over Garmin files");
    364 	while ((de = readdir(dir)) != NULL) {
    365 		if (!check_filename(abstract, de->d_name))
    366 			continue;
    367 
    368 		dc_status_t rc = make_space(files);
    369 		if (rc != DC_STATUS_SUCCESS)
    370 			return rc;
    371 		add_name(files, de->d_name, 0);
    372 	}
    373 	DEBUG(abstract->context, "Found %d files", files->nr);
    374 
    375 	if (files->array)
    376 		qsort(files->array, files->nr, sizeof(struct fit_file), name_cmp);
    377 	return DC_STATUS_SUCCESS;
    378 }
    379 #endif
    380 
    381 #ifdef HAVE_LIBMTP
    382 static unsigned int
    383 mtp_get_folder_id(dc_device_t *abstract, LIBMTP_mtpdevice_t *device, LIBMTP_devicestorage_t *storage, const char *folder, unsigned int parent_id)
    384 {
    385 	DEBUG(abstract->context, "Garmin/mtp: looking for folder %s under parent id %d", folder, parent_id);
    386 	// memory management is interesting here - we have to always walk the list returned and destroy them one by one
    387 	unsigned int folder_id = LIBMTP_FILES_AND_FOLDERS_ROOT;
    388 	LIBMTP_file_t* files = LIBMTP_Get_Files_And_Folders (device, storage->id, parent_id);
    389 	while (files != NULL) {
    390 		LIBMTP_file_t* mtp_file = files;
    391 		if (mtp_file->filetype == LIBMTP_FILETYPE_FOLDER && mtp_file->filename && !strncasecmp(mtp_file->filename, folder, strlen(folder))) {
    392 			folder_id = mtp_file->item_id;
    393 		}
    394 		files = files->next;
    395 		LIBMTP_destroy_file_t(mtp_file);
    396 	}
    397 	return folder_id;
    398 }
    399 
    400 static dc_status_t
    401 mtp_get_file_list(dc_device_t *abstract, struct file_list *files)
    402 {
    403 	garmin_device_t *device = (garmin_device_t *)abstract;
    404 	LIBMTP_raw_device_t *rawdevices;
    405 	int numrawdevices;
    406 	int i;
    407 
    408 	LIBMTP_Init();
    409 	DEBUG(abstract->context, "Attempting to connect to mtp device");
    410 
    411 	switch (LIBMTP_Detect_Raw_Devices(&rawdevices, &numrawdevices)) {
    412 	case LIBMTP_ERROR_NO_DEVICE_ATTACHED:
    413 		DEBUG(abstract->context, "Garmin/mtp: no device found");
    414 		return DC_STATUS_NODEVICE;
    415 	case LIBMTP_ERROR_CONNECTING:
    416 		DEBUG(abstract->context, "Garmin/mtp: error connecting");
    417 		return DC_STATUS_NOACCESS;
    418 	case LIBMTP_ERROR_MEMORY_ALLOCATION:
    419 		DEBUG(abstract->context, "Garmin/mtp: memory allocation error");
    420 		return DC_STATUS_NOMEMORY;
    421 	case LIBMTP_ERROR_GENERAL: // Unknown general errors - that's bad
    422 	default:
    423 		DEBUG(abstract->context, "Garmin/mtp: unknown error");
    424 		return DC_STATUS_UNSUPPORTED;
    425 	case LIBMTP_ERROR_NONE:
    426 		DEBUG(abstract->context, "Garmin/mtp: successfully connected with %d raw devices", numrawdevices);
    427 	}
    428 	/* iterate through connected MTP devices */
    429 	for (i = 0; i < numrawdevices; i++) {
    430 		LIBMTP_devicestorage_t *storage;
    431 		// We only read supported Garmin devices at this point.
    432 		if (rawdevices[i].device_entry.vendor_id != GARMIN_VENDOR) {
    433 			DEBUG(abstract->context, "Garmin/mtp: skipping raw device %04x/%04x",
    434 			      rawdevices[i].device_entry.vendor_id, rawdevices[i].device_entry.product_id);
    435 			continue;
    436 		}
    437 
    438 		if (!(rawdevices[i].device_entry.product_id & 0x4000u) ||
    439 		    !garmin_model_is_mtp_capable(rawdevices[i].device_entry.product_id & ~0x4000u)) {
    440 			DEBUG(abstract->context, "Garmin/mtp: skipping Garmin raw device %04x/%04x, as it is not a dive computer / does not support MTP",
    441 			      rawdevices[i].device_entry.vendor_id, rawdevices[i].device_entry.product_id);
    442 			continue;
    443 		}
    444 		device->mtp_device = LIBMTP_Open_Raw_Device_Uncached(&rawdevices[i]);
    445 		if (device->mtp_device == NULL) {
    446 			DEBUG(abstract->context, "Garmin/mtp: unable to open raw device %d", i);
    447 			continue;
    448 		}
    449 		DEBUG(abstract->context, "Garmin/mtp: succcessfully opened device");
    450 		for (storage = device->mtp_device->storage; storage != 0; storage = storage->next) {
    451 			unsigned int garmin_id = mtp_get_folder_id(abstract, device->mtp_device, storage, "Garmin", LIBMTP_FILES_AND_FOLDERS_ROOT);
    452 			DEBUG(abstract->context, "Garmin/mtp: Garmin folder at file_id %d", garmin_id);
    453 			if (garmin_id == LIBMTP_FILES_AND_FOLDERS_ROOT)
    454 				continue; // this storage partition didn't have a Garmin folder
    455 			unsigned int activity_id = mtp_get_folder_id(abstract, device->mtp_device, storage, "Activity", garmin_id);
    456 			DEBUG(abstract->context, "Garmin/mtp: Activity folder at file_id %d", activity_id);
    457 			if (activity_id == LIBMTP_FILES_AND_FOLDERS_ROOT)
    458 				continue; // no Activity folder
    459 
    460 			// now walk that folder to create our file_list
    461 			LIBMTP_file_t* activity_files = LIBMTP_Get_Files_And_Folders (device->mtp_device, storage->id, activity_id);
    462 			while (activity_files != NULL) {
    463 				LIBMTP_file_t* mtp_file = activity_files;
    464 				if (mtp_file->filetype != LIBMTP_FILETYPE_FOLDER && mtp_file->filename) {
    465 					if (check_filename(abstract, mtp_file->filename)) {
    466 						dc_status_t rc = make_space(files);
    467 						if (rc != DC_STATUS_SUCCESS)
    468 							return rc;
    469 						add_name(files, mtp_file->filename, mtp_file->item_id);
    470 					}
    471 				}
    472 				activity_files = activity_files->next;
    473 				LIBMTP_destroy_file_t(mtp_file);
    474 			}
    475 		}
    476 	}
    477 	free(rawdevices);
    478 	DEBUG(abstract->context, "Found %d files", files->nr);
    479 
    480 	if (files->array)
    481 		qsort(files->array, files->nr, sizeof(struct fit_file), name_cmp);
    482 	return DC_STATUS_SUCCESS;
    483 }
    484 
    485 // MTP hands us the file data in chunks which we then just add to our data buffer
    486 static uint16_t
    487 mtp_put_func(void* params, void* priv, uint32_t sendlen, unsigned char *data, uint32_t *putlen)
    488 {
    489 	dc_buffer_t *file = (dc_buffer_t *)priv;
    490 	dc_buffer_append(file, data, sendlen);
    491 	if (putlen)
    492 		*putlen = sendlen;
    493 	return 0;
    494 }
    495 
    496 // read the file from the MTP device and store the content in the data buffer
    497 static dc_status_t
    498 mtp_read_file(garmin_device_t *device, unsigned int file_id, dc_buffer_t *file)
    499 {
    500 	dc_status_t rc = DC_STATUS_SUCCESS;
    501 	if (!device->mtp_device) {
    502 		DEBUG(device->base.context, "Garmin/mtp: cannot read file without MTP device");
    503 		return DC_STATUS_NODEVICE;
    504 	}
    505 	DEBUG(device->base.context, "Garmin/mtp: call Get_File_To_Handler");
    506 	if (LIBMTP_Get_File_To_Handler(device->mtp_device, file_id, &mtp_put_func, (void *) file, NULL, NULL) != 0) {
    507 		LIBMTP_Dump_Errorstack(device->mtp_device);
    508 		return DC_STATUS_IO;
    509 	}
    510 	return rc;
    511 }
    512 #endif /* HAVE_LIBMTP */
    513 
    514 static dc_status_t
    515 read_file(char *pathname, int pathlen, const char *name, dc_buffer_t *file)
    516 {
    517 	int fd, rc;
    518 
    519 	pathname[pathlen] = '/';
    520 	memcpy(pathname+pathlen+1, name, FILE_NAME_SIZE);
    521 	fd = dc_open(pathname, DC_O_RDONLY | DC_O_BINARY);
    522 
    523 	if (fd < 0)
    524 		return DC_STATUS_IO;
    525 
    526 	rc = DC_STATUS_SUCCESS;
    527 	for (;;) {
    528 		char buffer[4096];
    529 		int n;
    530 
    531 		n = dc_read(fd, buffer, sizeof(buffer));
    532 		if (!n)
    533 			break;
    534 		if (n > 0) {
    535 			dc_buffer_append(file, buffer, n);
    536 			continue;
    537 		}
    538 		rc = DC_STATUS_IO;
    539 		break;
    540 	}
    541 
    542 	dc_close(fd);
    543 	return rc;
    544 }
    545 
    546 static dc_status_t
    547 garmin_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata)
    548 {
    549 	dc_status_t status = DC_STATUS_SUCCESS;
    550 	garmin_device_t *device = (garmin_device_t *) abstract;
    551 	char pathname[PATH_MAX];
    552 	char pathname_input[PATH_MAX];
    553 	size_t pathlen;
    554 	struct file_list files = {
    555 		0,     // nr
    556 		0,     // allocated
    557 		NULL   // array of file names / ids
    558 	};
    559 	dc_buffer_t *file;
    560 #ifndef _WIN32
    561 	DIR *dir;
    562 #endif
    563 	dc_status_t rc;
    564 
    565 	// Read the directory name from the iostream
    566 	rc = dc_iostream_read(device->iostream, &pathname_input, sizeof(pathname_input)-1, &pathlen);
    567 	if (rc != DC_STATUS_SUCCESS)
    568 		return rc;
    569 	pathname_input[pathlen] = 0;
    570 
    571 #ifdef HAVE_LIBMTP
    572 	// if the user passes in a path, don't try to read via MTP
    573 	if (pathlen)
    574 		device->use_mtp = 0;
    575 #endif
    576 
    577 	// The actual dives are under the "Garmin/Activity/" directory
    578 	// as FIT files, with names like "2018-08-20-10-23-30.fit".
    579 	// Make sure our buffer is big enough.
    580 	if (pathlen + strlen("/Garmin/Activity/") + FILE_NAME_SIZE + 2 > PATH_MAX) {
    581 		ERROR (abstract->context, "Invalid Garmin base directory '%s'", pathname_input);
    582 		return DC_STATUS_IO;
    583 	}
    584 	strcpy(pathname, pathname_input);
    585 	if (pathlen && pathname[pathlen-1] != '/')
    586 		pathname[pathlen++] = '/';
    587 	strcpy(pathname + pathlen, "Garmin/Activity");
    588 	pathlen += strlen("Garmin/Activity");
    589 
    590 #ifdef HAVE_LIBMTP
    591 	if (device->use_mtp) {
    592 		rc = mtp_get_file_list(abstract, &files);
    593 		if (rc != DC_STATUS_SUCCESS || !files.nr) {
    594 			free(files.array);
    595 			return rc;
    596 		}
    597 	} else
    598 #endif
    599 	{ // slight coding style violation to deal with the non-MTP case
    600 #ifdef _WIN32
    601 		// On Windows, get_file_list takes the pathname directly
    602 		rc = get_file_list(abstract, pathname, &files);
    603 		if (rc != DC_STATUS_SUCCESS) {
    604 			if (rc == DC_STATUS_NOMEMORY) {
    605 				free(files.array);
    606 				return rc;
    607 			}
    608 
    609 			free(files.array);
    610 			files.nr = 0;
    611 			files.allocated = 0;
    612 			files.array = NULL;
    613 
    614 			// Try the input path directly
    615 			rc = get_file_list(abstract, pathname_input, &files);
    616 			if (rc != DC_STATUS_SUCCESS) {
    617 				ERROR (abstract->context, "Failed to open directory '%s' or '%s'.", pathname, pathname_input);
    618 				free(files.array);
    619 				return rc;
    620 			}
    621 			strcpy(pathname, pathname_input);
    622 			pathlen = strlen(pathname);
    623 		}
    624 		if (!files.nr) {
    625 			free(files.array);
    626 			return rc;
    627 		}
    628 #else
    629 		dir = opendir(pathname);
    630 		if (!dir) {
    631 			dir = opendir(pathname_input);
    632 			if (!dir) {
    633 				ERROR (abstract->context, "Failed to open directory '%s' or '%s'.", pathname, pathname_input);
    634 				return DC_STATUS_IO;
    635 			}
    636 			strcpy(pathname, pathname_input);
    637 			pathlen = strlen(pathname);
    638 		}
    639 		// Get the list of FIT files
    640 		rc = get_file_list(abstract, dir, &files);
    641 		closedir(dir);
    642 		if (rc != DC_STATUS_SUCCESS || !files.nr) {
    643 			free(files.array);
    644 			return rc;
    645 		}
    646 #endif
    647 	}
    648 	// We found at least one file
    649 	// Can we find the fingerprint entry?
    650 	for (int i = 0; i < files.nr; i++) {
    651 		const char *name = files.array[i].name;
    652 
    653 		if (memcmp(name, device->fingerprint, sizeof (device->fingerprint)))
    654 			continue;
    655 
    656 		// Found fingerprint, just cut the array short here
    657 		files.nr = i;
    658 		DEBUG(abstract->context, "Ignoring '%s' and older", name);
    659 		break;
    660 	}
    661 	// Enable progress notifications.
    662 	dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER;
    663 	progress.maximum = files.nr;
    664 	progress.current = 0;
    665 	device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);
    666 
    667 	file = dc_buffer_new (16384);
    668 	if (file == NULL) {
    669 		ERROR (abstract->context, "Insufficient buffer space available.");
    670 		free(files.array);
    671 		return DC_STATUS_NOMEMORY;
    672 	}
    673 
    674 	dc_event_devinfo_t devinfo;
    675 	dc_event_devinfo_t *devinfo_p = &devinfo;
    676 	for (int i = 0; i < files.nr; i++) {
    677 		const char *name = files.array[i].name;
    678 		dc_parser_t *parser;
    679 		const unsigned char *data;
    680 		unsigned int size;
    681 		short is_dive = 0;
    682 
    683 		if (device_is_cancelled(abstract)) {
    684 			status = DC_STATUS_CANCELLED;
    685 			break;
    686 		}
    687 
    688 		// Reset the membuffer, read the data
    689 		dc_buffer_clear(file);
    690 #ifdef HAVE_LIBMTP
    691 		if (device->use_mtp)
    692 			status = mtp_read_file(device, files.array[i].mtp_id, file);
    693 		else
    694 #endif
    695 			status = read_file(pathname, pathlen, name, file);
    696 
    697 		if (status != DC_STATUS_SUCCESS)
    698 			break;
    699 
    700 		data = dc_buffer_get_data(file);
    701 		size = dc_buffer_get_size(file);
    702 
    703 		status = garmin_parser_create(&parser, abstract->context, data, size);
    704 		if (status != DC_STATUS_SUCCESS) {
    705 			ERROR (abstract->context, "Failed to create parser for dive verification.");
    706 			free(files.array);
    707 			return rc;
    708 		}
    709 
    710 		is_dive = !device->model || garmin_parser_is_dive(parser, devinfo_p);
    711 		if (devinfo_p) {
    712 			// first time we came through here, let's emit the
    713 			// devinfo and vendor events
    714 			device_event_emit (abstract, DC_EVENT_DEVINFO, devinfo_p);
    715 			devinfo_p = NULL;
    716 		}
    717 		if (!is_dive) {
    718 			DEBUG(abstract->context, "decided %s isn't a dive.", name);
    719 			dc_parser_destroy(parser);
    720 			continue;
    721 		}
    722 
    723 		if (callback && !callback(data, size, name, FIT_NAME_SIZE, userdata))
    724 			break;
    725 
    726 		progress.current++;
    727 		device_event_emit(abstract, DC_EVENT_PROGRESS, &progress);
    728 		dc_parser_destroy(parser);
    729 	}
    730 
    731 	free(files.array);
    732 	dc_buffer_free(file);
    733 	return status;
    734 }