summaryrefslogtreecommitdiffstats
path: root/libdivecomputer.c
diff options
context:
space:
mode:
authorGravatar Linus Torvalds <torvalds@linux-foundation.org>2013-01-09 16:14:21 -0800
committerGravatar Dirk Hohndel <dirk@hohndel.org>2013-01-09 16:38:21 -0800
commit1aa3c0d5147df7ef8846c955dffe7227257c5ca1 (patch)
tree43899c0a4a04e53c26bea707e7364035a7d0c3fb /libdivecomputer.c
parentec38d3708df24a23a46f19707f0f5afd44969c92 (diff)
downloadsubsurface-1aa3c0d5147df7ef8846c955dffe7227257c5ca1.tar.gz
Assemble the actual Suunto serial number
It turns out that the serial number returned by libdivecomputer isn't really the serial number as interpreted by the vendor. Those tend to be strings, but libdivecomputer gives us a 32bit number. Some experimenting showed that for the Suunto devies tested the serial number is encoded in that 32bit number: It so happens that the Suunto serial number strings are strings that have all numbers, but they aren't *one* number. They are four bytes representing two numbers each, and the "23500027" string is actually the four bytes 23 50 00 27 (0x17 0x32 0x00 0x1b). And libdivecomputer has incorrectly parsed those four bytes as one number, not as the encoded serial number string it is. So the value 389152795 is actually hex 0x1732001b, which is 0x17 0x32 0x00 0x1b, which is - 23 50 00 27. This should be done by libdivecomputer, but hey, in the meantime this at least shows the concept. And helps test the XML save/restore code. It depends on the two patches that create the whole "device.c" infrastructure, of course. With this, my dive file ends up having the settings section look like this: <divecomputerid model='Suunto Vyper Air' deviceid='d4629110' serial='01201094' firmware='1.1.22'/> <divecomputerid model='Suunto HelO2' deviceid='995dd566' serial='23500027' firmware='1.0.4'/> where the format of the firmware version is something I guessed at, but it was the obvious choice (again, it's byte-based, I'm ignoring the high byte that is zero for both of my Suuntos). Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'libdivecomputer.c')
-rw-r--r--libdivecomputer.c41
1 files changed, 40 insertions, 1 deletions
diff --git a/libdivecomputer.c b/libdivecomputer.c
index 47c7b6367..9529861dc 100644
--- a/libdivecomputer.c
+++ b/libdivecomputer.c
@@ -5,6 +5,7 @@
#include <glib/gi18n.h>
#include "dive.h"
+#include "device.h"
#include "divelist.h"
#include "display.h"
#include "display-gtk.h"
@@ -418,7 +419,7 @@ static int dive_cb(const unsigned char *data, unsigned int size,
dc_parser_destroy(parser);
return rc;
}
- dive->dc.model = str_printf("%s %s", devdata->vendor, devdata->product);
+ dive->dc.model = devdata->model;
dive->dc.deviceid = devdata->deviceid;
dive->dc.diveid = calculate_diveid(fingerprint, fsize);
@@ -526,6 +527,35 @@ static uint32_t calculate_sha1(unsigned int model, unsigned int firmware, unsign
return csum[0];
}
+static void fixup_suunto_versions(device_data_t *devdata, const dc_event_devinfo_t *devinfo)
+{
+ struct device_info *info;
+
+ info = create_device_info(devdata->model, devdata->deviceid);
+ if (!info)
+ return;
+
+ if (!info->serial_nr && devinfo->serial) {
+ char serial_nr[13];
+
+ snprintf(serial_nr, sizeof(serial_nr), "%02d%02d%02d%02d",
+ (devinfo->serial >> 24) & 0xff,
+ (devinfo->serial >> 16) & 0xff,
+ (devinfo->serial >> 8) & 0xff,
+ (devinfo->serial >> 0) & 0xff);
+ info->serial_nr = strdup(serial_nr);
+ }
+
+ if (!info->firmware && devinfo->firmware) {
+ char firmware[13];
+ snprintf(firmware, sizeof(firmware), "%d.%d.%d",
+ (devinfo->firmware >> 16) & 0xff,
+ (devinfo->firmware >> 8) & 0xff,
+ (devinfo->firmware >> 0) & 0xff);
+ info->firmware = strdup(firmware);
+ }
+}
+
static void event_cb(dc_device_t *device, dc_event_type_t event, const void *data, void *userdata)
{
const dc_event_progress_t *progress = data;
@@ -548,6 +578,13 @@ static void event_cb(dc_device_t *device, dc_event_type_t event, const void *dat
devinfo->firmware, devinfo->firmware,
devinfo->serial, devinfo->serial);
devdata->deviceid = calculate_sha1(devinfo->model, devinfo->firmware, devinfo->serial);
+
+ /*
+ * libdivecomputer doesn't give serial numbers in the proper string form,
+ * so we have to see if we can do some vendor-specific munging.
+ */
+ if (!strcmp(devdata->vendor, "Suunto"))
+ fixup_suunto_versions(devdata, devinfo);
break;
case DC_EVENT_CLOCK:
dev_info(devdata, _("Event: systime=%"PRId64", devtime=%u\n"),
@@ -571,6 +608,8 @@ static const char *do_device_import(device_data_t *data)
dc_status_t rc;
dc_device_t *device = data->device;
+ data->model = str_printf("%s %s", data->vendor, data->product);
+
// Register the event handler.
int events = DC_EVENT_WAITING | DC_EVENT_PROGRESS | DC_EVENT_DEVINFO | DC_EVENT_CLOCK;
rc = dc_device_set_events(device, events, event_cb, data);