diff --git a/examples/universal.c b/examples/universal.c index 136454d..1e80bb8 100644 --- a/examples/universal.c +++ b/examples/universal.c @@ -104,6 +104,7 @@ static const backend_table_t g_backends[] = { {"petrel", DC_FAMILY_SHEARWATER_PETREL}, {"nitekq", DC_FAMILY_DIVERITE_NITEKQ}, {"aqualand", DC_FAMILY_CITIZEN_AQUALAND}, + {"idive", DC_FAMILY_DIVESYSTEM_IDIVE}, }; static dc_family_t diff --git a/include/libdivecomputer/Makefile.am b/include/libdivecomputer/Makefile.am index 3221d22..71887d5 100644 --- a/include/libdivecomputer/Makefile.am +++ b/include/libdivecomputer/Makefile.am @@ -51,4 +51,6 @@ libdivecomputer_HEADERS = \ diverite.h \ diverite_nitekq.h \ citizen.h \ - citizen_aqualand.h + citizen_aqualand.h \ + divesystem.h \ + divesystem_idive.h diff --git a/include/libdivecomputer/common.h b/include/libdivecomputer/common.h index 708fd00..8bede4d 100644 --- a/include/libdivecomputer/common.h +++ b/include/libdivecomputer/common.h @@ -86,6 +86,8 @@ typedef enum dc_family_t { DC_FAMILY_DIVERITE_NITEKQ = (11 << 16), /* Citizen */ DC_FAMILY_CITIZEN_AQUALAND = (12 << 16), + /* DiveSystem */ + DC_FAMILY_DIVESYSTEM_IDIVE = (13 << 16), } dc_family_t; #ifdef __cplusplus diff --git a/include/libdivecomputer/divesystem.h b/include/libdivecomputer/divesystem.h new file mode 100644 index 0000000..080fa52 --- /dev/null +++ b/include/libdivecomputer/divesystem.h @@ -0,0 +1,27 @@ +/* + * libdivecomputer + * + * Copyright (C) 2014 Jef Driesen + * + * 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 DIVESYSTEM_H +#define DIVESYSTEM_H + +#include "divesystem_idive.h" + +#endif /* DIVESYSTEM_H */ diff --git a/include/libdivecomputer/divesystem_idive.h b/include/libdivecomputer/divesystem_idive.h new file mode 100644 index 0000000..e1e66d5 --- /dev/null +++ b/include/libdivecomputer/divesystem_idive.h @@ -0,0 +1,42 @@ +/* + * libdivecomputer + * + * Copyright (C) 2014 Jef Driesen + * + * 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 DIVESYSTEM_IDIVE_H +#define DIVESYSTEM_IDIVE_H + +#include "context.h" +#include "device.h" +#include "parser.h" + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +dc_status_t +divesystem_idive_device_open (dc_device_t **device, dc_context_t *context, const char *name); + +dc_status_t +divesystem_idive_parser_create (dc_parser_t **parser, dc_context_t *context); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* DIVESYSTEM_IDIVE_H */ diff --git a/src/Makefile.am b/src/Makefile.am index 821bc1b..abbaa48 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -55,6 +55,7 @@ libdivecomputer_la_SOURCES = \ shearwater_petrel.c \ diverite_nitekq.c diverite_nitekq_parser.c \ citizen_aqualand.c citizen_aqualand_parser.c \ + divesystem_idive.c divesystem_idive_parser.c \ ringbuffer.h ringbuffer.c \ checksum.h checksum.c \ array.h array.c \ diff --git a/src/descriptor.c b/src/descriptor.c index 93525e9..187d92e 100644 --- a/src/descriptor.c +++ b/src/descriptor.c @@ -232,6 +232,13 @@ static const dc_descriptor_t g_descriptors[] = { {"Dive Rite", "NiTek Q", DC_FAMILY_DIVERITE_NITEKQ, 0}, /* Citizen Hyper Aqualand */ {"Citizen", "Hyper Aqualand", DC_FAMILY_CITIZEN_AQUALAND, 0}, + /* DiveSystem iDive */ + {"DiveSystem", "Orca", DC_FAMILY_DIVESYSTEM_IDIVE, 2}, + {"DiveSystem", "iDive Pro", DC_FAMILY_DIVESYSTEM_IDIVE, 3}, + {"DiveSystem", "iDive DAN", DC_FAMILY_DIVESYSTEM_IDIVE, 4}, + {"DiveSystem", "iDive Tech", DC_FAMILY_DIVESYSTEM_IDIVE, 5}, + {"DiveSystem", "iDive Reb", DC_FAMILY_DIVESYSTEM_IDIVE, 6}, + {"DiveSystem", "iDive Free", DC_FAMILY_DIVESYSTEM_IDIVE, 8}, }; typedef struct dc_descriptor_iterator_t { diff --git a/src/device.c b/src/device.c index 909bac3..1bac4f4 100644 --- a/src/device.c +++ b/src/device.c @@ -35,6 +35,7 @@ #include #include #include +#include #include "device-private.h" #include "context-private.h" @@ -161,6 +162,9 @@ dc_device_open (dc_device_t **out, dc_context_t *context, dc_descriptor_t *descr case DC_FAMILY_CITIZEN_AQUALAND: rc = citizen_aqualand_device_open (&device, context, name); break; + case DC_FAMILY_DIVESYSTEM_IDIVE: + rc = divesystem_idive_device_open (&device, context, name); + break; default: return DC_STATUS_INVALIDARGS; } diff --git a/src/divesystem_idive.c b/src/divesystem_idive.c new file mode 100644 index 0000000..a23237f --- /dev/null +++ b/src/divesystem_idive.c @@ -0,0 +1,438 @@ +/* + * libdivecomputer + * + * Copyright (C) 2014 Jef Driesen + * + * 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 // memcmp, memcpy +#include // malloc, free + +#include + +#include "context-private.h" +#include "device-private.h" +#include "serial.h" +#include "checksum.h" +#include "array.h" + +#define ISINSTANCE(device) dc_device_isinstance((device), &divesystem_idive_device_vtable) + +#define EXITCODE(rc) \ +( \ + rc == -1 ? DC_STATUS_IO : DC_STATUS_TIMEOUT \ +) + +#define MAXRETRIES 9 + +#define MAXPACKET 0xFF +#define START 0x55 +#define ACK 0x06 +#define NAK 0x15 +#define BUSY 0x60 + +#define CMD_ID 0x10 +#define CMD_RANGE 0x98 +#define CMD_HEADER 0xA0 +#define CMD_SAMPLE 0xA8 + +#define SZ_ID 0x0A +#define SZ_RANGE 0x04 +#define SZ_HEADER 0x33 +#define SZ_SAMPLE 0x2A + +#define NSTEPS 1000 +#define STEP(i,n) (NSTEPS * (i) / (n)) + +typedef struct divesystem_idive_device_t { + dc_device_t base; + serial_t *port; + unsigned char fingerprint[4]; +} divesystem_idive_device_t; + +static dc_status_t divesystem_idive_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size); +static dc_status_t divesystem_idive_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata); +static dc_status_t divesystem_idive_device_close (dc_device_t *abstract); + +static const dc_device_vtable_t divesystem_idive_device_vtable = { + DC_FAMILY_DIVESYSTEM_IDIVE, + divesystem_idive_device_set_fingerprint, /* set_fingerprint */ + NULL, /* read */ + NULL, /* write */ + NULL, /* dump */ + divesystem_idive_device_foreach, /* foreach */ + divesystem_idive_device_close /* close */ +}; + + +dc_status_t +divesystem_idive_device_open (dc_device_t **out, dc_context_t *context, const char *name) +{ + if (out == NULL) + return DC_STATUS_INVALIDARGS; + + // Allocate memory. + divesystem_idive_device_t *device = (divesystem_idive_device_t *) malloc (sizeof (divesystem_idive_device_t)); + if (device == NULL) { + ERROR (context, "Failed to allocate memory."); + return DC_STATUS_NOMEMORY; + } + + // Initialize the base class. + device_init (&device->base, context, &divesystem_idive_device_vtable); + + // Set the default values. + device->port = NULL; + memset (device->fingerprint, 0, sizeof (device->fingerprint)); + + // Open the device. + int rc = serial_open (&device->port, context, name); + if (rc == -1) { + ERROR (context, "Failed to open the serial port."); + free (device); + return DC_STATUS_IO; + } + + // Set the serial communication protocol (115200 8N1). + rc = serial_configure (device->port, 115200, 8, SERIAL_PARITY_NONE, 1, SERIAL_FLOWCONTROL_NONE); + if (rc == -1) { + ERROR (context, "Failed to set the terminal attributes."); + serial_close (device->port); + free (device); + return DC_STATUS_IO; + } + + // Set the timeout for receiving data (1000ms). + if (serial_set_timeout (device->port, 1000) == -1) { + ERROR (context, "Failed to set the timeout."); + serial_close (device->port); + free (device); + return DC_STATUS_IO; + } + + // Make sure everything is in a sane state. + serial_sleep (device->port, 300); + serial_flush (device->port, SERIAL_QUEUE_BOTH); + + *out = (dc_device_t *) device; + + return DC_STATUS_SUCCESS; +} + + +static dc_status_t +divesystem_idive_device_close (dc_device_t *abstract) +{ + divesystem_idive_device_t *device = (divesystem_idive_device_t*) abstract; + + // Close the device. + if (serial_close (device->port) == -1) { + free (device); + return DC_STATUS_IO; + } + + // Free memory. + free (device); + + return DC_STATUS_SUCCESS; +} + + +static dc_status_t +divesystem_idive_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size) +{ + divesystem_idive_device_t *device = (divesystem_idive_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 +divesystem_idive_send (divesystem_idive_device_t *device, const unsigned char command[], unsigned int csize) +{ + dc_device_t *abstract = (dc_device_t *) device; + unsigned char packet[MAXPACKET + 4]; + unsigned short crc = 0; + + if (device_is_cancelled (abstract)) + return DC_STATUS_CANCELLED; + + if (csize < 1 || csize > MAXPACKET) + return DC_STATUS_INVALIDARGS; + + // Setup the data packet + packet[0] = START; + packet[1] = csize; + memcpy(packet + 2, command, csize); + crc = checksum_crc_ccitt_uint16 (packet, csize + 2); + packet[csize + 2] = (crc >> 8) & 0xFF; + packet[csize + 3] = (crc ) & 0xFF; + + // Send the data packet. + int n = serial_write (device->port, packet, csize + 4); + if (n != csize + 4) { + ERROR (abstract->context, "Failed to send the command."); + return EXITCODE (n); + } + + return DC_STATUS_SUCCESS; +} + + +static dc_status_t +divesystem_idive_receive (divesystem_idive_device_t *device, unsigned char answer[], unsigned int *asize) +{ + dc_device_t *abstract = (dc_device_t *) device; + unsigned char packet[MAXPACKET + 4]; + int n = 0; + + if (asize == NULL || *asize < MAXPACKET) { + ERROR (abstract->context, "Invalid arguments."); + return DC_STATUS_INVALIDARGS; + } + + // Read the packet start byte. + while (1) { + n = serial_read (device->port, packet + 0, 1); + if (n != 1) { + ERROR (abstract->context, "Failed to receive the packet start byte."); + return EXITCODE (n); + } + + if (packet[0] == START) + break; + } + + // Read the packet length. + n = serial_read (device->port, packet + 1, 1); + if (n != 1) { + ERROR (abstract->context, "Failed to receive the packet length."); + return EXITCODE (n); + } + + unsigned int len = packet[1]; + if (len < 2 || len > MAXPACKET) { + ERROR (abstract->context, "Invalid packet length."); + return DC_STATUS_PROTOCOL; + } + + // Read the packet payload and checksum. + n = serial_read (device->port, packet + 2, len + 2); + if (n != len + 2) { + ERROR (abstract->context, "Failed to receive the packet payload and checksum."); + return EXITCODE (n); + } + + // Verify the checksum. + unsigned short crc = array_uint16_be (packet + len + 2); + unsigned short ccrc = checksum_crc_ccitt_uint16 (packet, len + 2); + if (crc != ccrc) { + ERROR (abstract->context, "Unexpected packet checksum."); + return DC_STATUS_PROTOCOL; + } + + memcpy(answer, packet + 2, len); + *asize = len; + + return DC_STATUS_SUCCESS; +} + + +static dc_status_t +divesystem_idive_transfer (divesystem_idive_device_t *device, const unsigned char command[], unsigned int csize, unsigned char answer[], unsigned int asize) +{ + dc_status_t rc = DC_STATUS_SUCCESS; + dc_device_t *abstract = (dc_device_t *) device; + unsigned char packet[MAXPACKET] = {0}; + unsigned int length = 0; + unsigned int nretries = 0; + + while (1) { + // Send the command. + rc = divesystem_idive_send (device, command, csize); + if (rc != DC_STATUS_SUCCESS) + return rc; + + // Receive the answer. + length = sizeof(packet); + rc = divesystem_idive_receive (device, packet, &length); + if (rc != DC_STATUS_SUCCESS) + return rc; + + // Verify the command byte. + if (packet[0] != command[0]) { + ERROR (abstract->context, "Unexpected packet header."); + return DC_STATUS_PROTOCOL; + } + + // Check the ACK byte. + if (packet[length - 1] == ACK) + break; + + // Verify the NAK byte. + if (packet[length - 1] != NAK) { + ERROR (abstract->context, "Unexpected ACK/NAK byte."); + return DC_STATUS_PROTOCOL; + } + + // Verify the length of the packet. + if (length != 3) { + ERROR (abstract->context, "Unexpected packet length."); + return DC_STATUS_PROTOCOL; + } + + // Verify the error code. + unsigned int errcode = packet[1]; + if (errcode != BUSY) { + ERROR (abstract->context, "Received NAK packet with error code %02x.", errcode); + return DC_STATUS_PROTOCOL; + } + + // Abort if the maximum number of retries is reached. + if (nretries++ >= MAXRETRIES) + return DC_STATUS_PROTOCOL; + + // Delay the next attempt. + serial_sleep(device->port, 100); + } + + // Verify the length of the packet. + if (asize != length - 2) { + ERROR (abstract->context, "Unexpected packet length."); + return DC_STATUS_PROTOCOL; + } + + memcpy(answer, packet + 1, length - 2); + + return DC_STATUS_SUCCESS; +} + +static dc_status_t +divesystem_idive_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata) +{ + dc_status_t rc = DC_STATUS_SUCCESS; + divesystem_idive_device_t *device = (divesystem_idive_device_t *) abstract; + + // Enable progress notifications. + dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER; + device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); + + unsigned char cmd_id[] = {CMD_ID, 0xED}; + unsigned char id[SZ_ID]; + rc = divesystem_idive_transfer (device, cmd_id, sizeof(cmd_id), id, sizeof(id)); + if (rc != DC_STATUS_SUCCESS) + return rc; + + // Emit a device info event. + dc_event_devinfo_t devinfo; + devinfo.model = array_uint16_le (id); + devinfo.firmware = 0; + devinfo.serial = array_uint32_le (id + 6); + device_event_emit (abstract, DC_EVENT_DEVINFO, &devinfo); + + // Emit a vendor event. + dc_event_vendor_t vendor; + vendor.data = id; + vendor.size = sizeof (id); + device_event_emit (abstract, DC_EVENT_VENDOR, &vendor); + + unsigned char cmd_range[] = {CMD_RANGE, 0x8D}; + unsigned char range[4]; + rc = divesystem_idive_transfer (device, cmd_range, sizeof(cmd_range), range, sizeof(range)); + if (rc != DC_STATUS_SUCCESS) + return rc; + + // Get the range of the available dive numbers. + unsigned int first = array_uint16_le (range + 0); + unsigned int last = array_uint16_le (range + 2); + if (first > last) { + ERROR(abstract->context, "Invalid dive numbers."); + return DC_STATUS_DATAFORMAT; + } + + // Calculate the number of dives. + unsigned int ndives = last - first + 1; + + // Update and emit a progress event. + progress.maximum = ndives * NSTEPS; + device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); + + dc_buffer_t *buffer = dc_buffer_new(0); + if (buffer == NULL) { + return DC_STATUS_NOMEMORY; + } + + for (unsigned int i = 0; i < ndives; ++i) { + unsigned int number = last - i; + unsigned char cmd_header[] = {CMD_HEADER, + (number ) & 0xFF, + (number >> 8) & 0xFF}; + unsigned char header[SZ_HEADER]; + rc = divesystem_idive_transfer (device, cmd_header, sizeof(cmd_header), header, sizeof(header)); + if (rc != DC_STATUS_SUCCESS) + return rc; + + if (memcmp(header + 7, device->fingerprint, sizeof(device->fingerprint)) == 0) + break; + + unsigned int nsamples = array_uint16_le (header + 1); + + // Update and emit a progress event. + progress.current = i * NSTEPS + STEP(1, nsamples + 1); + device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); + + dc_buffer_clear(buffer); + dc_buffer_reserve(buffer, SZ_HEADER + SZ_SAMPLE * nsamples); + dc_buffer_append(buffer, header, sizeof(header)); + + for (unsigned int j = 0; j < nsamples; ++j) { + unsigned int idx = j + 1; + unsigned char cmd_sample[] = {CMD_SAMPLE, + (idx ) & 0xFF, + (idx >> 8) & 0xFF}; + unsigned char sample[SZ_SAMPLE]; + rc = divesystem_idive_transfer (device, cmd_sample, sizeof(cmd_sample), sample, sizeof(sample)); + if (rc != DC_STATUS_SUCCESS) + return rc; + + // Update and emit a progress event. + progress.current = i * NSTEPS + STEP(j + 2, nsamples + 1); + device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); + + dc_buffer_append(buffer, sample, sizeof(sample)); + } + + unsigned char *data = dc_buffer_get_data(buffer); + unsigned int size = dc_buffer_get_size(buffer); + if (callback && !callback (data, size, data + 7, sizeof(device->fingerprint), userdata)) { + dc_buffer_free (buffer); + return DC_STATUS_SUCCESS; + } + } + + dc_buffer_free(buffer); + + return DC_STATUS_SUCCESS; +} diff --git a/src/divesystem_idive_parser.c b/src/divesystem_idive_parser.c new file mode 100644 index 0000000..751f9df --- /dev/null +++ b/src/divesystem_idive_parser.c @@ -0,0 +1,296 @@ +/* + * libdivecomputer + * + * Copyright (C) 2014 Jef Driesen + * + * 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 + +#include + +#include "context-private.h" +#include "parser-private.h" +#include "array.h" + +#define ISINSTANCE(parser) dc_device_isinstance((parser), &divesystem_idive_parser_vtable) + +#define SZ_HEADER 0x33 +#define SZ_SAMPLE 0x2A + +#define NGASMIXES 8 + +#define EPOCH 1199145600 /* 2008-01-01 00:00:00 */ + +typedef struct divesystem_idive_parser_t divesystem_idive_parser_t; + +struct divesystem_idive_parser_t { + dc_parser_t base; + // Cached fields. + unsigned int cached; + unsigned int divetime; + unsigned int maxdepth; + unsigned int ngasmixes; + unsigned int oxygen[NGASMIXES]; + unsigned int helium[NGASMIXES]; +}; + +static dc_status_t divesystem_idive_parser_set_data (dc_parser_t *abstract, const unsigned char *data, unsigned int size); +static dc_status_t divesystem_idive_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime); +static dc_status_t divesystem_idive_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value); +static dc_status_t divesystem_idive_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata); +static dc_status_t divesystem_idive_parser_destroy (dc_parser_t *abstract); + +static const dc_parser_vtable_t divesystem_idive_parser_vtable = { + DC_FAMILY_DIVESYSTEM_IDIVE, + divesystem_idive_parser_set_data, /* set_data */ + divesystem_idive_parser_get_datetime, /* datetime */ + divesystem_idive_parser_get_field, /* fields */ + divesystem_idive_parser_samples_foreach, /* samples_foreach */ + divesystem_idive_parser_destroy /* destroy */ +}; + + +dc_status_t +divesystem_idive_parser_create (dc_parser_t **out, dc_context_t *context) +{ + if (out == NULL) + return DC_STATUS_INVALIDARGS; + + // Allocate memory. + divesystem_idive_parser_t *parser = (divesystem_idive_parser_t *) malloc (sizeof (divesystem_idive_parser_t)); + if (parser == NULL) { + ERROR (context, "Failed to allocate memory."); + return DC_STATUS_NOMEMORY; + } + + // Initialize the base class. + parser_init (&parser->base, context, &divesystem_idive_parser_vtable); + + // Set the default values. + parser->cached = 0; + parser->divetime = 0; + parser->maxdepth = 0; + parser->ngasmixes = 0; + for (unsigned int i = 0; i < NGASMIXES; ++i) { + parser->oxygen[i] = 0; + parser->helium[i] = 0; + } + + *out = (dc_parser_t*) parser; + + return DC_STATUS_SUCCESS; +} + + +static dc_status_t +divesystem_idive_parser_destroy (dc_parser_t *abstract) +{ + // Free memory. + free (abstract); + + return DC_STATUS_SUCCESS; +} + + +static dc_status_t +divesystem_idive_parser_set_data (dc_parser_t *abstract, const unsigned char *data, unsigned int size) +{ + divesystem_idive_parser_t *parser = (divesystem_idive_parser_t *) abstract; + + // Reset the cache. + parser->cached = 0; + parser->divetime = 0; + parser->maxdepth = 0; + parser->ngasmixes = 0; + for (unsigned int i = 0; i < NGASMIXES; ++i) { + parser->oxygen[i] = 0; + parser->helium[i] = 0; + } + + return DC_STATUS_SUCCESS; +} + + +static dc_status_t +divesystem_idive_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime) +{ + if (abstract->size < SZ_HEADER) + return DC_STATUS_DATAFORMAT; + + dc_ticks_t ticks = array_uint32_le(abstract->data + 7) + EPOCH; + + if (!dc_datetime_localtime (datetime, ticks)) + return DC_STATUS_DATAFORMAT; + + return DC_STATUS_SUCCESS; +} + + +static dc_status_t +divesystem_idive_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value) +{ + divesystem_idive_parser_t *parser = (divesystem_idive_parser_t *) abstract; + const unsigned char *data = abstract->data; + + if (abstract->size < SZ_HEADER) + return DC_STATUS_DATAFORMAT; + + if (!parser->cached) { + dc_status_t rc = divesystem_idive_parser_samples_foreach (abstract, NULL, NULL); + if (rc != DC_STATUS_SUCCESS) + return rc; + } + + dc_gasmix_t *gasmix = (dc_gasmix_t *) value; + + if (value) { + switch (type) { + case DC_FIELD_DIVETIME: + *((unsigned int *) value) = parser->divetime; + break; + case DC_FIELD_MAXDEPTH: + *((double *) value) = parser->maxdepth / 10.0; + break; + case DC_FIELD_GASMIX_COUNT: + *((unsigned int *) value) = parser->ngasmixes; + break; + case DC_FIELD_GASMIX: + gasmix->helium = parser->helium[flags] / 100.0; + gasmix->oxygen = parser->oxygen[flags] / 100.0; + gasmix->nitrogen = 1.0 - gasmix->oxygen - gasmix->helium; + break; + case DC_FIELD_ATMOSPHERIC: + *((double *) value) = array_uint16_le (data + 11) / 1000.0; + break; + default: + return DC_STATUS_UNSUPPORTED; + } + } + + return DC_STATUS_SUCCESS; +} + + +static dc_status_t +divesystem_idive_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata) +{ + divesystem_idive_parser_t *parser = (divesystem_idive_parser_t *) abstract; + const unsigned char *data = abstract->data; + unsigned int size = abstract->size; + + unsigned int time = 0; + unsigned int maxdepth = 0; + unsigned int ngasmixes = 0; + unsigned int oxygen[NGASMIXES]; + unsigned int helium[NGASMIXES]; + unsigned int o2_previous = 0xFFFFFFFF; + unsigned int he_previous = 0xFFFFFFFF; + + unsigned int offset = SZ_HEADER; + while (offset + SZ_SAMPLE <= size) { + dc_sample_value_t sample = {0}; + + // Time (seconds). + unsigned int timestamp = array_uint32_le (data + offset + 2); + if (timestamp <= time) { + ERROR (abstract->context, "Timestamp moved backwards."); + return DC_STATUS_DATAFORMAT; + } + time = timestamp; + sample.time = timestamp; + if (callback) callback (DC_SAMPLE_TIME, sample, userdata); + + // Depth (1/10 m). + unsigned int depth = array_uint16_le (data + offset + 6); + if (maxdepth < depth) + maxdepth = depth; + sample.depth = depth / 10.0; + if (callback) callback (DC_SAMPLE_DEPTH, sample, userdata); + + // Temperature (Celsius). + signed int temperature = (signed short) array_uint16_le (data + offset + 8); + sample.temperature = temperature / 10.0; + if (callback) callback (DC_SAMPLE_TEMPERATURE, sample, userdata); + + // Gaschange. + unsigned int o2 = data[offset + 10]; + unsigned int he = data[offset + 11]; + if (o2 != o2_previous || he != he_previous) { + // Find the gasmix in the list. + unsigned int i = 0; + while (i < ngasmixes) { + if (o2 == oxygen[i] && he == helium[i]) + break; + i++; + } + + // Add it to list if not found. + if (i >= ngasmixes) { + if (i >= NGASMIXES) { + ERROR (abstract->context, "Maximum number of gas mixes reached."); + return DC_STATUS_DATAFORMAT; + } + oxygen[i] = o2; + helium[i] = he; + ngasmixes = i + 1; + } + + sample.event.type = SAMPLE_EVENT_GASCHANGE2; + sample.event.time = 0; + sample.event.flags = 0; + sample.event.value = o2 | (he << 16); + if (callback) callback (DC_SAMPLE_EVENT, sample, userdata); + o2_previous = o2; + he_previous = he; + } + + // Deco stop / NDL. + unsigned int deco = array_uint16_le (data + offset + 21); + unsigned int tts = array_uint16_le (data + offset + 23); + if (tts != 0xFFFF) { + if (deco) { + sample.deco.type = DC_DECO_DECOSTOP; + sample.deco.depth = deco / 10.0; + } else { + sample.deco.type = DC_DECO_NDL; + sample.deco.depth = 0.0; + } + sample.deco.time = tts; + if (callback) callback (DC_SAMPLE_DECO, sample, userdata); + } + + // CNS + unsigned int cns = array_uint16_le (data + offset + 29); + sample.cns = cns; + if (callback) callback (DC_SAMPLE_CNS, sample, userdata); + + offset += SZ_SAMPLE; + } + + // Cache the data for later use. + for (unsigned int i = 0; i < ngasmixes; ++i) { + parser->helium[i] = helium[i]; + parser->oxygen[i] = oxygen[i]; + } + parser->ngasmixes = ngasmixes; + parser->maxdepth = maxdepth; + parser->divetime = time; + parser->cached = 1; + + return DC_STATUS_SUCCESS; +} diff --git a/src/parser.c b/src/parser.c index 718682a..4d0d97e 100644 --- a/src/parser.c +++ b/src/parser.c @@ -33,6 +33,7 @@ #include #include #include +#include #include "parser-private.h" #include "device-private.h" @@ -138,6 +139,9 @@ dc_parser_new (dc_parser_t **out, dc_device_t *device) case DC_FAMILY_CITIZEN_AQUALAND: rc = citizen_aqualand_parser_create (&parser, context); break; + case DC_FAMILY_DIVESYSTEM_IDIVE: + rc = divesystem_idive_parser_create (&parser, context); + break; default: return DC_STATUS_INVALIDARGS; }