From e363e5b1fd459fe8d79854ef63f3f561a29a7401 Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Sat, 3 Nov 2018 21:23:26 +0100 Subject: [PATCH] Add support for the Cressi Goa and Cartesio --- examples/common.c | 1 + include/libdivecomputer/common.h | 1 + msvc/libdivecomputer.vcproj | 12 + src/Makefile.am | 1 + src/cressi_goa.c | 498 +++++++++++++++++++++++++++++++ src/cressi_goa.h | 43 +++ src/cressi_goa_parser.c | 209 +++++++++++++ src/descriptor.c | 3 + src/device.c | 4 + src/parser.c | 4 + 10 files changed, 776 insertions(+) create mode 100644 src/cressi_goa.c create mode 100644 src/cressi_goa.h create mode 100644 src/cressi_goa_parser.c diff --git a/examples/common.c b/examples/common.c index b97343f..d8c7262 100644 --- a/examples/common.c +++ b/examples/common.c @@ -80,6 +80,7 @@ static const backend_table_t g_backends[] = { {"ostc3", DC_FAMILY_HW_OSTC3, 0x0A}, {"edy", DC_FAMILY_CRESSI_EDY, 0x08}, {"leonardo", DC_FAMILY_CRESSI_LEONARDO, 1}, + {"goa", DC_FAMILY_CRESSI_GOA, 2}, {"n2ition3", DC_FAMILY_ZEAGLE_N2ITION3, 0}, {"cobalt", DC_FAMILY_ATOMICS_COBALT, 0}, {"predator", DC_FAMILY_SHEARWATER_PREDATOR, 2}, diff --git a/include/libdivecomputer/common.h b/include/libdivecomputer/common.h index 1058b01..7f94f14 100644 --- a/include/libdivecomputer/common.h +++ b/include/libdivecomputer/common.h @@ -86,6 +86,7 @@ typedef enum dc_family_t { /* Cressi */ DC_FAMILY_CRESSI_EDY = (7 << 16), DC_FAMILY_CRESSI_LEONARDO, + DC_FAMILY_CRESSI_GOA, /* Zeagle */ DC_FAMILY_ZEAGLE_N2ITION3 = (8 << 16), /* Atomic Aquatics */ diff --git a/msvc/libdivecomputer.vcproj b/msvc/libdivecomputer.vcproj index 1a88ff8..507140e 100644 --- a/msvc/libdivecomputer.vcproj +++ b/msvc/libdivecomputer.vcproj @@ -238,6 +238,14 @@ RelativePath="..\src\cressi_edy_parser.c" > + + + + @@ -584,6 +592,10 @@ RelativePath="..\src\cressi_edy.h" > + + diff --git a/src/Makefile.am b/src/Makefile.am index 6afa24e..4d19b48 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -55,6 +55,7 @@ libdivecomputer_la_SOURCES = \ aes.h aes.c \ cressi_edy.h cressi_edy.c cressi_edy_parser.c \ cressi_leonardo.h cressi_leonardo.c cressi_leonardo_parser.c \ + cressi_goa.h cressi_goa.c cressi_goa_parser.c \ zeagle_n2ition3.h zeagle_n2ition3.c \ atomics_cobalt.h atomics_cobalt.c atomics_cobalt_parser.c \ shearwater_common.h shearwater_common.c \ diff --git a/src/cressi_goa.c b/src/cressi_goa.c new file mode 100644 index 0000000..f6df023 --- /dev/null +++ b/src/cressi_goa.c @@ -0,0 +1,498 @@ +/* + * libdivecomputer + * + * Copyright (C) 2018 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 // memcpy, memcmp +#include // malloc, free +#include // assert + +#include "cressi_goa.h" +#include "context-private.h" +#include "device-private.h" +#include "checksum.h" +#include "array.h" +#include "platform.h" + +#define ISINSTANCE(device) dc_device_isinstance((device), &cressi_goa_device_vtable) + +#define CMD_VERSION 0x00 +#define CMD_LOGBOOK 0x21 +#define CMD_DIVE 0x22 + +#define HEADER 0xAA +#define TRAILER 0x55 +#define END 0x04 +#define ACK 0x06 + +#define SZ_DATA 512 +#define SZ_PACKET 10 +#define SZ_HEADER 23 + +#define FP_OFFSET 0x11 +#define FP_SIZE 6 + +#define NSTEPS 1000 +#define STEP(i,n) (NSTEPS * (i) / (n)) + +typedef struct cressi_goa_device_t { + dc_device_t base; + dc_iostream_t *iostream; + unsigned char fingerprint[FP_SIZE]; +} cressi_goa_device_t; + +static dc_status_t cressi_goa_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size); +static dc_status_t cressi_goa_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata); + +static const dc_device_vtable_t cressi_goa_device_vtable = { + sizeof(cressi_goa_device_t), + DC_FAMILY_CRESSI_GOA, + cressi_goa_device_set_fingerprint, /* set_fingerprint */ + NULL, /* read */ + NULL, /* write */ + NULL, /* dump */ + cressi_goa_device_foreach, /* foreach */ + NULL, /* timesync */ + NULL /* close */ +}; + +static dc_status_t +cressi_goa_device_send (cressi_goa_device_t *device, unsigned char cmd, const unsigned char data[], unsigned int size) +{ + dc_status_t status = DC_STATUS_SUCCESS; + dc_device_t *abstract = (dc_device_t *) device; + + if (size > SZ_PACKET) { + ERROR (abstract->context, "Unexpected payload size (%u).", size); + return DC_STATUS_INVALIDARGS; + } + + // Setup the data packet. + unsigned short crc = 0; + unsigned char packet[SZ_PACKET + 8] = { + HEADER, HEADER, HEADER, + size, + cmd + }; + if (size) { + memcpy (packet + 5, data, size); + } + crc = checksum_crc16_ccitt (packet + 3, size + 2, 0x000); + packet[5 + size + 0] = (crc ) & 0xFF; // Low + packet[5 + size + 1] = (crc >> 8) & 0xFF; // High + packet[5 + size + 2] = TRAILER; + + // Wait a small amount of time before sending the command. Without + // this delay, the transfer will fail most of the time. + dc_iostream_sleep (device->iostream, 100); + + // Send the command to the device. + status = dc_iostream_write (device->iostream, packet, size + 8, NULL); + if (status != DC_STATUS_SUCCESS) { + ERROR (abstract->context, "Failed to send the command."); + return status; + } + + return status; +} + +static dc_status_t +cressi_goa_device_receive (cressi_goa_device_t *device, unsigned char data[], unsigned int size) +{ + dc_status_t status = DC_STATUS_SUCCESS; + dc_device_t *abstract = (dc_device_t *) device; + + unsigned char packet[SZ_PACKET + 8]; + + // Read the header of the data packet. + status = dc_iostream_read (device->iostream, packet, 4, NULL); + if (status != DC_STATUS_SUCCESS) { + ERROR (abstract->context, "Failed to receive the answer."); + return status; + } + + // Verify the header of the packet. + if (packet[0] != HEADER || packet[1] != HEADER || packet[2] != HEADER) { + ERROR (abstract->context, "Unexpected answer header byte."); + return DC_STATUS_PROTOCOL; + } + + // Get the payload length. + unsigned int length = packet[3]; + if (length > SZ_PACKET) { + ERROR (abstract->context, "Unexpected payload size (%u).", length); + return DC_STATUS_PROTOCOL; + } + + // Read the remainder of the data packet. + status = dc_iostream_read (device->iostream, packet + 4, length + 4, NULL); + if (status != DC_STATUS_SUCCESS) { + ERROR (abstract->context, "Failed to receive the answer."); + return status; + } + + // Verify the trailer of the packet. + if (packet[length + 7] != TRAILER) { + ERROR (abstract->context, "Unexpected answer trailer byte."); + return DC_STATUS_PROTOCOL; + } + + // Verify the checksum of the packet. + unsigned short crc = array_uint16_le (packet + length + 5); + unsigned short ccrc = checksum_crc16_ccitt (packet + 3, length + 2, 0x0000); + if (crc != ccrc) { + ERROR (abstract->context, "Unexpected answer checksum."); + return DC_STATUS_PROTOCOL; + } + + // Verify the payload length. + if (length != size) { + ERROR (abstract->context, "Unexpected payload size (%u).", length); + return DC_STATUS_PROTOCOL; + } + + if (length) { + memcpy (data, packet + 5, length); + } + + return status; +} + +static dc_status_t +cressi_goa_device_download (cressi_goa_device_t *device, dc_buffer_t *buffer, dc_event_progress_t *progress) +{ + dc_status_t status = DC_STATUS_SUCCESS; + dc_device_t *abstract = (dc_device_t *) device; + + const unsigned char ack[] = {ACK}; + const unsigned int initial = progress ? progress->current : 0; + + // Erase the contents of the buffer. + if (!dc_buffer_clear (buffer)) { + ERROR (abstract->context, "Insufficient buffer space available."); + return DC_STATUS_NOMEMORY; + } + + unsigned int skip = 2; + unsigned int size = 2; + unsigned int nbytes = 0; + while (nbytes < size) { + // Read the data packet. + unsigned char packet[3 + SZ_DATA + 2]; + status = dc_iostream_read (device->iostream, packet, sizeof(packet), NULL); + if (status != DC_STATUS_SUCCESS) { + ERROR (abstract->context, "Failed to receive the answer."); + return status; + } + + // Verify the checksum of the packet. + unsigned short crc = array_uint16_le (packet + sizeof(packet) - 2); + unsigned short ccrc = checksum_crc16_ccitt (packet + 3, sizeof(packet) - 5, 0x0000); + if (crc != ccrc) { + ERROR (abstract->context, "Unexpected answer checksum."); + return DC_STATUS_PROTOCOL; + } + + // Send the ack byte to the device. + status = dc_iostream_write (device->iostream, ack, sizeof(ack), NULL); + if (status != DC_STATUS_SUCCESS) { + ERROR (abstract->context, "Failed to send the ack byte."); + return status; + } + + // Get the total size from the first data packet. + if (nbytes == 0) { + size += array_uint16_le (packet + 3); + } + + // Calculate the payload size of the packet. + unsigned int length = size - nbytes; + if (length > SZ_DATA) { + length = SZ_DATA; + } + + // Append the payload to the output buffer. + if (!dc_buffer_append (buffer, packet + 3 + skip, length - skip)) { + ERROR (abstract->context, "Insufficient buffer space available."); + return DC_STATUS_NOMEMORY; + } + + nbytes += length; + skip = 0; + + // Update and emit a progress event. + if (progress) { + progress->current = initial + STEP(nbytes, size); + device_event_emit (abstract, DC_EVENT_PROGRESS, progress); + } + } + + // Read the end byte. + unsigned char end = 0; + status = dc_iostream_read (device->iostream, &end, 1, NULL); + if (status != DC_STATUS_SUCCESS) { + ERROR (abstract->context, "Failed to receive the end byte."); + return status; + } + + // Verify the end byte. + if (end != END) { + ERROR (abstract->context, "Unexpected end byte (%02x).", end); + return DC_STATUS_PROTOCOL; + } + + // Send the ack byte to the device. + status = dc_iostream_write (device->iostream, ack, sizeof(ack), NULL); + if (status != DC_STATUS_SUCCESS) { + ERROR (abstract->context, "Failed to send the ack byte."); + return status; + } + + return status; +} + +static dc_status_t +cressi_goa_device_transfer (cressi_goa_device_t *device, + unsigned char cmd, + const unsigned char input[], unsigned int isize, + unsigned char output[], unsigned int osize, + dc_buffer_t *buffer, + dc_event_progress_t *progress) +{ + dc_status_t status = DC_STATUS_SUCCESS; + + // Send the command to the dive computer. + status = cressi_goa_device_send (device, cmd, input, isize); + if (status != DC_STATUS_SUCCESS) { + return status; + } + + // Receive the answer from the dive computer. + status = cressi_goa_device_receive (device, output, osize); + if (status != DC_STATUS_SUCCESS) { + return status; + } + + // Download the optional and variable sized payload. + if (buffer) { + status = cressi_goa_device_download (device, buffer, progress); + if (status != DC_STATUS_SUCCESS) { + return status; + } + } + + return status; +} + + +dc_status_t +cressi_goa_device_open (dc_device_t **out, dc_context_t *context, dc_iostream_t *iostream) +{ + dc_status_t status = DC_STATUS_SUCCESS; + cressi_goa_device_t *device = NULL; + + if (out == NULL) + return DC_STATUS_INVALIDARGS; + + // Allocate memory. + device = (cressi_goa_device_t *) dc_device_allocate (context, &cressi_goa_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)); + + // Set the serial communication protocol (115200 8N1). + status = dc_iostream_configure (device->iostream, 115200, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE); + if (status != DC_STATUS_SUCCESS) { + ERROR (context, "Failed to set the terminal attributes."); + goto error_free; + } + + // Set the timeout for receiving data (3000 ms). + status = dc_iostream_set_timeout (device->iostream, 3000); + if (status != DC_STATUS_SUCCESS) { + ERROR (context, "Failed to set the timeout."); + goto error_free; + } + + // Clear the RTS line. + status = dc_iostream_set_rts (device->iostream, 0); + if (status != DC_STATUS_SUCCESS) { + ERROR (context, "Failed to clear the RTS line."); + goto error_free; + } + + // Clear the DTR line. + status = dc_iostream_set_dtr (device->iostream, 0); + if (status != DC_STATUS_SUCCESS) { + ERROR (context, "Failed to clear the DTR line."); + goto error_free; + } + + dc_iostream_sleep (device->iostream, 100); + dc_iostream_purge (device->iostream, DC_DIRECTION_ALL); + + *out = (dc_device_t *) device; + + return DC_STATUS_SUCCESS; + +error_free: + dc_device_deallocate ((dc_device_t *) device); + return status; +} + +static dc_status_t +cressi_goa_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size) +{ + cressi_goa_device_t *device = (cressi_goa_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 +cressi_goa_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata) +{ + dc_status_t status = DC_STATUS_SUCCESS; + cressi_goa_device_t *device = (cressi_goa_device_t *) abstract; + dc_buffer_t *logbook = NULL; + dc_buffer_t *dive = NULL; + + // Enable progress notifications. + dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER; + device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); + + // Read the version information. + unsigned char id[9] = {0}; + status = cressi_goa_device_transfer (device, CMD_VERSION, NULL, 0, id, sizeof(id), NULL, NULL); + if (status != DC_STATUS_SUCCESS) { + ERROR (abstract->context, "Failed to read the version information."); + goto error_exit; + } + + // Emit a vendor event. + dc_event_vendor_t vendor; + vendor.data = id; + vendor.size = sizeof (id); + device_event_emit (abstract, DC_EVENT_VENDOR, &vendor); + + // Emit a device info event. + dc_event_devinfo_t devinfo; + devinfo.model = id[4]; + devinfo.firmware = array_uint16_le (id + 5); + devinfo.serial = array_uint32_le (id + 0); + device_event_emit (abstract, DC_EVENT_DEVINFO, &devinfo); + + // Allocate memory for the logbook data. + logbook = dc_buffer_new(4096); + if (logbook == NULL) { + ERROR (abstract->context, "Failed to allocate memory."); + goto error_exit; + } + + // Read the logbook data. + status = cressi_goa_device_transfer (device, CMD_LOGBOOK, NULL, 0, NULL, 0, logbook, &progress); + if (status != DC_STATUS_SUCCESS) { + ERROR (abstract->context, "Failed to read the logbook data."); + goto error_free_logbook; + } + + const unsigned char *logbook_data = dc_buffer_get_data(logbook); + size_t logbook_size = dc_buffer_get_size(logbook); + + // Count the number of dives. + unsigned int count = 0; + unsigned int offset = logbook_size; + while (offset > SZ_HEADER) { + // Move to the start of the logbook entry. + offset -= SZ_HEADER; + + // Get the dive number. + unsigned int number= array_uint16_le (logbook_data + offset); + if (number == 0) + break; + + // Compare the fingerprint to identify previously downloaded entries. + if (memcmp (logbook_data + offset + FP_OFFSET, device->fingerprint, sizeof(device->fingerprint)) == 0) { + break; + } + + count++; + } + + // Update and emit a progress event. + progress.maximum = (count + 1) * NSTEPS; + device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); + + // Allocate memory for the dive data. + dive = dc_buffer_new(4096); + if (dive == NULL) { + ERROR (abstract->context, "Failed to allocate memory."); + goto error_free_logbook; + } + + // Download the dives. + offset = logbook_size; + for (unsigned int i = 0; i < count; ++i) { + // Move to the start of the logbook entry. + offset -= SZ_HEADER; + + // Read the dive data. + status = cressi_goa_device_transfer (device, CMD_DIVE, logbook_data + offset, 2, NULL, 0, dive, &progress); + if (status != DC_STATUS_SUCCESS) { + ERROR (abstract->context, "Failed to read the dive data."); + goto error_free_dive; + } + + const unsigned char *dive_data = dc_buffer_get_data (dive); + size_t dive_size = dc_buffer_get_size (dive); + + // Verify the header in the logbook and dive data are identical. + // After the 2 byte dive number, the logbook header has 5 bytes + // extra, which are not present in the dive header. + if (dive_size < SZ_HEADER - 5 || + memcmp (dive_data + 0, logbook_data + offset + 0, 2) != 0 || + memcmp (dive_data + 2, logbook_data + offset + 7, SZ_HEADER - 7) != 0) { + ERROR (abstract->context, "Unexpected dive header."); + status = DC_STATUS_DATAFORMAT; + goto error_free_dive; + } + + if (callback && !callback(dive_data, dive_size, dive_data + FP_OFFSET - 5, sizeof(device->fingerprint), userdata)) + break; + } + +error_free_dive: + dc_buffer_free(dive); +error_free_logbook: + dc_buffer_free(logbook); +error_exit: + return status; +} diff --git a/src/cressi_goa.h b/src/cressi_goa.h new file mode 100644 index 0000000..0a92512 --- /dev/null +++ b/src/cressi_goa.h @@ -0,0 +1,43 @@ +/* + * libdivecomputer + * + * Copyright (C) 2018 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 CRESSI_GOA_H +#define CRESSI_GOA_H + +#include +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +dc_status_t +cressi_goa_device_open (dc_device_t **device, dc_context_t *context, dc_iostream_t *iostream); + +dc_status_t +cressi_goa_parser_create (dc_parser_t **parser, dc_context_t *context, unsigned int model); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* CRESSI_GOA_H */ diff --git a/src/cressi_goa_parser.c b/src/cressi_goa_parser.c new file mode 100644 index 0000000..cc99d2c --- /dev/null +++ b/src/cressi_goa_parser.c @@ -0,0 +1,209 @@ +/* + * libdivecomputer + * + * Copyright (C) 2018 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 "cressi_goa.h" +#include "context-private.h" +#include "parser-private.h" +#include "array.h" + +#define ISINSTANCE(parser) dc_device_isinstance((parser), &cressi_goa_parser_vtable) + +#define SZ_HEADER 0x5C + +#define DEPTH 0 +#define TEMPERATURE 3 + +typedef struct cressi_goa_parser_t cressi_goa_parser_t; + +struct cressi_goa_parser_t { + dc_parser_t base; + unsigned int model; + // Cached fields. + unsigned int cached; + double maxdepth; +}; + +static dc_status_t cressi_goa_parser_set_data (dc_parser_t *abstract, const unsigned char *data, unsigned int size); +static dc_status_t cressi_goa_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime); +static dc_status_t cressi_goa_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value); +static dc_status_t cressi_goa_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata); + +static const dc_parser_vtable_t cressi_goa_parser_vtable = { + sizeof(cressi_goa_parser_t), + DC_FAMILY_CRESSI_GOA, + cressi_goa_parser_set_data, /* set_data */ + cressi_goa_parser_get_datetime, /* datetime */ + cressi_goa_parser_get_field, /* fields */ + cressi_goa_parser_samples_foreach, /* samples_foreach */ + NULL /* destroy */ +}; + +dc_status_t +cressi_goa_parser_create (dc_parser_t **out, dc_context_t *context, unsigned int model) +{ + cressi_goa_parser_t *parser = NULL; + + if (out == NULL) + return DC_STATUS_INVALIDARGS; + + // Allocate memory. + parser = (cressi_goa_parser_t *) dc_parser_allocate (context, &cressi_goa_parser_vtable); + if (parser == NULL) { + ERROR (context, "Failed to allocate memory."); + return DC_STATUS_NOMEMORY; + } + + parser->model = model; + parser->cached = 0; + parser->maxdepth = 0.0; + + *out = (dc_parser_t*) parser; + + return DC_STATUS_SUCCESS; +} + +static dc_status_t +cressi_goa_parser_set_data (dc_parser_t *abstract, const unsigned char *data, unsigned int size) +{ + cressi_goa_parser_t *parser = (cressi_goa_parser_t *) abstract; + + // Reset the cache. + parser->cached = 0; + parser->maxdepth = 0.0; + + return DC_STATUS_SUCCESS; +} + +static dc_status_t +cressi_goa_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime) +{ + if (abstract->size < SZ_HEADER) + return DC_STATUS_DATAFORMAT; + + const unsigned char *p = abstract->data; + + if (datetime) { + datetime->year = array_uint16_le(p + 0x0C); + datetime->month = p[0x0E]; + datetime->day = p[0x0F]; + datetime->hour = p[0x10]; + datetime->minute = p[0x11]; + datetime->second = 0; + datetime->timezone = DC_TIMEZONE_NONE; + } + + return DC_STATUS_SUCCESS; +} + +static dc_status_t +cressi_goa_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value) +{ + cressi_goa_parser_t *parser = (cressi_goa_parser_t *) abstract; + if (abstract->size < SZ_HEADER) + return DC_STATUS_DATAFORMAT; + + const unsigned char *data = abstract->data; + + if (!parser->cached) { + sample_statistics_t statistics = SAMPLE_STATISTICS_INITIALIZER; + dc_status_t rc = cressi_goa_parser_samples_foreach ( + abstract, sample_statistics_cb, &statistics); + if (rc != DC_STATUS_SUCCESS) + return rc; + + parser->cached = 1; + parser->maxdepth = statistics.maxdepth; + } + + dc_gasmix_t *gasmix = (dc_gasmix_t *) value; + + if (value) { + switch (type) { + case DC_FIELD_DIVETIME: + *((unsigned int *) value) = array_uint16_le (data + 0x14); + break; + case DC_FIELD_MAXDEPTH: + *((double *) value) = parser->maxdepth; + break; + case DC_FIELD_GASMIX_COUNT: + *((unsigned int *) value) = 2; + break; + case DC_FIELD_GASMIX: + gasmix->helium = 0.0; + gasmix->oxygen = data[0x1B + 2 * flags] / 100.0; + gasmix->nitrogen = 1.0 - gasmix->oxygen - gasmix->helium; + break; + default: + return DC_STATUS_UNSUPPORTED; + } + } + + return DC_STATUS_SUCCESS; +} + +static dc_status_t +cressi_goa_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata) +{ + const unsigned char *data = abstract->data; + unsigned int size = abstract->size; + + unsigned int time = 0; + unsigned int interval = 5; + unsigned int complete = 1; + + unsigned int offset = SZ_HEADER; + while (offset + 2 <= size) { + dc_sample_value_t sample = {0}; + + // Get the sample type and value. + unsigned int raw = array_uint16_le (data + offset); + unsigned int type = (raw & 0x0003); + unsigned int value = (raw & 0xFFFC) >> 2; + + if (complete) { + // Time (seconds). + time += interval; + sample.time = time; + if (callback) callback (DC_SAMPLE_TIME, sample, userdata); + complete = 0; + } + + if (type == DEPTH) { + // Depth (1/10 m). + unsigned int depth = value & 0x0FFF; + sample.depth = depth / 10.0; + if (callback) callback (DC_SAMPLE_DEPTH, sample, userdata); + complete = 1; + } else if (type == TEMPERATURE) { + // Temperature (1/10 °C). + sample.temperature = value / 10.0; + if (callback) callback (DC_SAMPLE_TEMPERATURE, sample, userdata); + } else { + WARNING(abstract->context, "Unknown sample type %u.", type); + } + + offset += 2; + } + + return DC_STATUS_SUCCESS; +} diff --git a/src/descriptor.c b/src/descriptor.c index dc7ec4c..1c533db 100644 --- a/src/descriptor.c +++ b/src/descriptor.c @@ -277,6 +277,9 @@ static const dc_descriptor_t g_descriptors[] = { {"Cressi", "Giotto", DC_FAMILY_CRESSI_LEONARDO, 4, DC_TRANSPORT_SERIAL, NULL}, {"Cressi", "Newton", DC_FAMILY_CRESSI_LEONARDO, 5, DC_TRANSPORT_SERIAL, NULL}, {"Cressi", "Drake", DC_FAMILY_CRESSI_LEONARDO, 6, DC_TRANSPORT_SERIAL, NULL}, + /* Cressi Goa */ + {"Cressi", "Cartesio", DC_FAMILY_CRESSI_GOA, 1, DC_TRANSPORT_SERIAL, NULL}, + {"Cressi", "Goa", DC_FAMILY_CRESSI_GOA, 2, DC_TRANSPORT_SERIAL, NULL}, /* Zeagle N2iTiON3 */ {"Zeagle", "N2iTiON3", DC_FAMILY_ZEAGLE_N2ITION3, 0, DC_TRANSPORT_SERIAL, NULL}, {"Apeks", "Quantum X", DC_FAMILY_ZEAGLE_N2ITION3, 0, DC_TRANSPORT_SERIAL, NULL}, diff --git a/src/device.c b/src/device.c index fc59464..9da9cd9 100644 --- a/src/device.c +++ b/src/device.c @@ -47,6 +47,7 @@ #include "hw_ostc3.h" #include "cressi_edy.h" #include "cressi_leonardo.h" +#include "cressi_goa.h" #include "zeagle_n2ition3.h" #include "atomics_cobalt.h" #include "shearwater_petrel.h" @@ -180,6 +181,9 @@ dc_device_open (dc_device_t **out, dc_context_t *context, dc_descriptor_t *descr case DC_FAMILY_CRESSI_LEONARDO: rc = cressi_leonardo_device_open (&device, context, iostream); break; + case DC_FAMILY_CRESSI_GOA: + rc = cressi_goa_device_open (&device, context, iostream); + break; case DC_FAMILY_ZEAGLE_N2ITION3: rc = zeagle_n2ition3_device_open (&device, context, iostream); break; diff --git a/src/parser.c b/src/parser.c index 064328d..6a236e6 100644 --- a/src/parser.c +++ b/src/parser.c @@ -47,6 +47,7 @@ #include "hw_ostc3.h" #include "cressi_edy.h" #include "cressi_leonardo.h" +#include "cressi_goa.h" #include "zeagle_n2ition3.h" #include "atomics_cobalt.h" #include "shearwater_petrel.h" @@ -144,6 +145,9 @@ dc_parser_new_internal (dc_parser_t **out, dc_context_t *context, dc_family_t fa case DC_FAMILY_CRESSI_LEONARDO: rc = cressi_leonardo_parser_create (&parser, context, model); break; + case DC_FAMILY_CRESSI_GOA: + rc = cressi_goa_parser_create (&parser, context, model); + break; case DC_FAMILY_ATOMICS_COBALT: rc = atomics_cobalt_parser_create (&parser, context); break;