Add backend for Garmin Descent Mk1
This uses pretty much all of our new infrastructure: the USB storage iostream for the actual IO, the field-cache for the divecomputer fields, and the string interface for the events. It's also a very fast downloader. Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
This commit is contained in:
parent
99c2ca7205
commit
4e9e94d9f8
@ -90,6 +90,7 @@ static const backend_table_t g_backends[] = {
|
||||
{"idive", DC_FAMILY_DIVESYSTEM_IDIVE, 0x03},
|
||||
{"cochran", DC_FAMILY_COCHRAN_COMMANDER, 0},
|
||||
{"divecomputereu", DC_FAMILY_TECDIVING_DIVECOMPUTEREU, 0},
|
||||
{"descentmk1", DC_FAMILY_GARMIN, 0},
|
||||
};
|
||||
|
||||
static const transport_table_t g_transports[] = {
|
||||
|
||||
@ -108,6 +108,8 @@ typedef enum dc_family_t {
|
||||
DC_FAMILY_COCHRAN_COMMANDER = (14 << 16),
|
||||
/* Tecdiving */
|
||||
DC_FAMILY_TECDIVING_DIVECOMPUTEREU = (15 << 16),
|
||||
/* Garmin */
|
||||
DC_FAMILY_GARMIN = (16 << 16),
|
||||
} dc_family_t;
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
@ -498,6 +498,14 @@
|
||||
RelativePath="..\src\tecdiving_divecomputereu_parser.c"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\src\garmin.c"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\src\garmin_parser.c"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\src\field-cache.c"
|
||||
>
|
||||
@ -844,6 +852,10 @@
|
||||
RelativePath="..\src\tecdiving_divecomputereu.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\src\garmin.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\src\timer.h"
|
||||
>
|
||||
|
||||
@ -73,6 +73,7 @@ libdivecomputer_la_SOURCES = \
|
||||
buffer.c \
|
||||
cochran_commander.h cochran_commander.c cochran_commander_parser.c \
|
||||
tecdiving_divecomputereu.h tecdiving_divecomputereu.c tecdiving_divecomputereu_parser.c \
|
||||
garmin.h garmin.c garmin_parser.c \
|
||||
socket.h socket.c \
|
||||
irda.c \
|
||||
usbhid.c \
|
||||
|
||||
@ -45,6 +45,7 @@ static int dc_filter_suunto (dc_transport_t transport, const void *userdata);
|
||||
static int dc_filter_shearwater (dc_transport_t transport, const void *userdata);
|
||||
static int dc_filter_hw (dc_transport_t transport, const void *userdata);
|
||||
static int dc_filter_tecdiving (dc_transport_t transport, const void *userdata);
|
||||
static int dc_filter_garmin (dc_transport_t transport, const void *userdata);
|
||||
static int dc_filter_mares (dc_transport_t transport, const void *userdata);
|
||||
static int dc_filter_divesystem (dc_transport_t transport, const void *userdata);
|
||||
static int dc_filter_oceanic (dc_transport_t transport, const void *userdata);
|
||||
@ -376,6 +377,8 @@ static const dc_descriptor_t g_descriptors[] = {
|
||||
{"Cochran", "EMC-20H", DC_FAMILY_COCHRAN_COMMANDER, 5, DC_TRANSPORT_SERIAL, NULL},
|
||||
/* Tecdiving DiveComputer.eu */
|
||||
{"Tecdiving", "DiveComputer.eu", DC_FAMILY_TECDIVING_DIVECOMPUTEREU, 0, DC_TRANSPORT_SERIAL | DC_TRANSPORT_BLUETOOTH, dc_filter_tecdiving},
|
||||
/* Garmin */
|
||||
{"Garmin", "Descent Mk1", DC_FAMILY_GARMIN, 2859, DC_TRANSPORT_USBSTORAGE, dc_filter_garmin},
|
||||
};
|
||||
|
||||
static int
|
||||
@ -581,6 +584,19 @@ static int dc_filter_tecdiving (dc_transport_t transport, const void *userdata)
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int dc_filter_garmin (dc_transport_t transport, const void *userdata)
|
||||
{
|
||||
static const dc_usb_desc_t usbhid[] = {
|
||||
{0x091e, 0x2b2b}, // Garmin Descent Mk1
|
||||
};
|
||||
|
||||
if (transport == DC_TRANSPORT_USBSTORAGE) {
|
||||
return DC_FILTER_INTERNAL (userdata, usbhid, 0, dc_match_usb);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int dc_filter_mares (dc_transport_t transport, const void *userdata)
|
||||
{
|
||||
static const char * const bluetooth[] = {
|
||||
|
||||
@ -57,6 +57,7 @@
|
||||
#include "divesystem_idive.h"
|
||||
#include "cochran_commander.h"
|
||||
#include "tecdiving_divecomputereu.h"
|
||||
#include "garmin.h"
|
||||
|
||||
#include "device-private.h"
|
||||
#include "context-private.h"
|
||||
@ -211,6 +212,9 @@ dc_device_open (dc_device_t **out, dc_context_t *context, dc_descriptor_t *descr
|
||||
case DC_FAMILY_TECDIVING_DIVECOMPUTEREU:
|
||||
rc = tecdiving_divecomputereu_device_open (&device, context, iostream);
|
||||
break;
|
||||
case DC_FAMILY_GARMIN:
|
||||
rc = garmin_device_open (&device, context, iostream);
|
||||
break;
|
||||
default:
|
||||
return DC_STATUS_INVALIDARGS;
|
||||
}
|
||||
|
||||
338
src/garmin.c
Normal file
338
src/garmin.c
Normal file
@ -0,0 +1,338 @@
|
||||
/*
|
||||
* 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>
|
||||
#include <dirent.h>
|
||||
#include <sys/types.h>
|
||||
#include <dirent.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "garmin.h"
|
||||
#include "context-private.h"
|
||||
#include "device-private.h"
|
||||
#include "array.h"
|
||||
|
||||
typedef struct garmin_device_t {
|
||||
dc_device_t base;
|
||||
dc_iostream_t *iostream;
|
||||
unsigned char fingerprint[FIT_NAME_SIZE];
|
||||
} garmin_device_t;
|
||||
|
||||
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)
|
||||
{
|
||||
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));
|
||||
|
||||
*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;
|
||||
|
||||
return DC_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
struct file_list {
|
||||
int nr, allocated;
|
||||
struct fit_name *array;
|
||||
};
|
||||
|
||||
static int name_cmp(const void *a, const void *b)
|
||||
{
|
||||
// Sort reverse string ordering (newest first), so use 'b,a'
|
||||
return strcmp(b,a);
|
||||
}
|
||||
|
||||
/*
|
||||
* Get the FIT file list and sort it.
|
||||
*
|
||||
* Return number of files found.
|
||||
*/
|
||||
static int 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) {
|
||||
int len = strlen(de->d_name);
|
||||
struct fit_name *entry;
|
||||
const char *explain = NULL;
|
||||
|
||||
DEBUG (abstract->context, " %s", de->d_name);
|
||||
|
||||
if (len < 5)
|
||||
explain = "name too short";
|
||||
if (len >= FIT_NAME_SIZE)
|
||||
explain = "name too long";
|
||||
if (strncasecmp(de->d_name + len - 4, ".FIT", 4))
|
||||
explain = "name lacks FIT suffix";
|
||||
|
||||
DEBUG (abstract->context, " %s - %s", de->d_name, explain ? explain : "adding to list");
|
||||
if (explain)
|
||||
continue;
|
||||
|
||||
if (files->nr == files->allocated) {
|
||||
struct fit_name *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;
|
||||
}
|
||||
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
entry = files->array + files->nr++;
|
||||
strncpy(entry->name, de->d_name, FIT_NAME_SIZE);
|
||||
}
|
||||
DEBUG (abstract->context, "Found %d files", files->nr);
|
||||
|
||||
qsort(files->array, files->nr, sizeof(struct fit_name), name_cmp);
|
||||
return DC_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
#ifndef O_BINARY
|
||||
#define O_BINARY 0
|
||||
#endif
|
||||
|
||||
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, FIT_NAME_SIZE);
|
||||
fd = open(pathname, O_RDONLY | O_BINARY);
|
||||
|
||||
if (fd < 0)
|
||||
return DC_STATUS_IO;
|
||||
|
||||
rc = DC_STATUS_SUCCESS;
|
||||
for (;;) {
|
||||
char buffer[4096];
|
||||
int n;
|
||||
|
||||
n = read(fd, buffer, sizeof(buffer));
|
||||
if (!n)
|
||||
break;
|
||||
if (n > 0) {
|
||||
dc_buffer_append(file, buffer, n);
|
||||
continue;
|
||||
}
|
||||
rc = DC_STATUS_IO;
|
||||
break;
|
||||
}
|
||||
|
||||
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;
|
||||
dc_parser_t *parser;
|
||||
char pathname[PATH_MAX];
|
||||
size_t pathlen;
|
||||
struct file_list files = { 0, 0, NULL };
|
||||
dc_buffer_t *file;
|
||||
DIR *dir;
|
||||
int rc;
|
||||
|
||||
// Read the directory name from the iostream
|
||||
rc = dc_iostream_read(device->iostream, &pathname, sizeof(pathname)-1, &pathlen);
|
||||
if (rc != DC_STATUS_SUCCESS)
|
||||
return rc;
|
||||
pathname[pathlen] = 0;
|
||||
|
||||
// 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/") + FIT_NAME_SIZE + 2 > PATH_MAX) {
|
||||
ERROR (abstract->context, "Invalid Garmin base directory '%s'", pathname);
|
||||
return DC_STATUS_IO;
|
||||
}
|
||||
|
||||
if (pathlen && pathname[pathlen-1] != '/')
|
||||
pathname[pathlen++] = '/';
|
||||
strcpy(pathname + pathlen, "Garmin/Activity");
|
||||
pathlen += strlen("Garmin/Activity");
|
||||
|
||||
dir = opendir(pathname);
|
||||
if (!dir) {
|
||||
ERROR (abstract->context, "Failed to open directory '%s'.", pathname);
|
||||
return DC_STATUS_IO;
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
if ((rc = garmin_parser_create(&parser, abstract->context) != DC_STATUS_SUCCESS)) {
|
||||
ERROR (abstract->context, "Failed to create parser for dive verification.");
|
||||
free(files.array);
|
||||
return rc;
|
||||
}
|
||||
|
||||
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;
|
||||
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);
|
||||
dc_buffer_append(file, name, FIT_NAME_SIZE);
|
||||
|
||||
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);
|
||||
|
||||
is_dive = garmin_parser_is_dive(parser, data, size, 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);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (callback && !callback(data, size, name, FIT_NAME_SIZE, userdata))
|
||||
break;
|
||||
|
||||
progress.current++;
|
||||
device_event_emit(abstract, DC_EVENT_PROGRESS, &progress);
|
||||
}
|
||||
|
||||
free(files.array);
|
||||
dc_parser_destroy(parser);
|
||||
return status;
|
||||
}
|
||||
59
src/garmin.h
Normal file
59
src/garmin.h
Normal file
@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Garmin Descent Mk1
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
|
||||
#ifndef GARMIN_H
|
||||
#define GARMIN_H
|
||||
|
||||
#include <libdivecomputer/context.h>
|
||||
#include <libdivecomputer/iostream.h>
|
||||
#include <libdivecomputer/device.h>
|
||||
#include <libdivecomputer/parser.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif /* __cplusplus */
|
||||
|
||||
dc_status_t
|
||||
garmin_device_open (dc_device_t **device, dc_context_t *context, dc_iostream_t *iostream);
|
||||
|
||||
dc_status_t
|
||||
garmin_parser_create (dc_parser_t **parser, dc_context_t *context);
|
||||
|
||||
// we need to be able to call into the parser to check if the
|
||||
// files that we find are actual dives
|
||||
int
|
||||
garmin_parser_is_dive (dc_parser_t *abstract, const unsigned char *data, unsigned int size, dc_event_devinfo_t *devinfo_p);
|
||||
|
||||
// The dive names are of the form "2018-08-20-10-23-30.fit"
|
||||
// With the terminating zero, that's 24 bytes.
|
||||
//
|
||||
// We use this as the fingerprint, but it ends up being a
|
||||
// special fixed header in the parser data too.
|
||||
#define FIT_NAME_SIZE 24
|
||||
|
||||
struct fit_name {
|
||||
char name[FIT_NAME_SIZE];
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif /* __cplusplus */
|
||||
#endif /* GARMIN_H */
|
||||
1318
src/garmin_parser.c
Normal file
1318
src/garmin_parser.c
Normal file
File diff suppressed because it is too large
Load Diff
@ -57,6 +57,7 @@
|
||||
#include "divesystem_idive.h"
|
||||
#include "cochran_commander.h"
|
||||
#include "tecdiving_divecomputereu.h"
|
||||
#include "garmin.h"
|
||||
|
||||
#include "context-private.h"
|
||||
#include "parser-private.h"
|
||||
@ -172,6 +173,9 @@ dc_parser_new_internal (dc_parser_t **out, dc_context_t *context, dc_family_t fa
|
||||
case DC_FAMILY_TECDIVING_DIVECOMPUTEREU:
|
||||
rc = tecdiving_divecomputereu_parser_create (&parser, context);
|
||||
break;
|
||||
case DC_FAMILY_GARMIN:
|
||||
rc = garmin_parser_create (&parser, context);
|
||||
break;
|
||||
default:
|
||||
return DC_STATUS_INVALIDARGS;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user