From 8aef4a49a0d7e5fa106948a3aba2e4c3ae266e09 Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Mon, 9 Apr 2018 21:16:22 +0200 Subject: [PATCH] Unify the Uwatec Smart, Meridian and G2 backends The Uwatec Smart, Meridian and G2 backends are almost identical, except for the low-level packet sending and receiving code. With the new I/O layer, those three backends can easily be unified in a single backend. The Meridian and G2 are completely removed, only the family types are kept for backwards compatibility. --- examples/common.c | 2 - include/libdivecomputer/common.h | 4 +- msvc/libdivecomputer.vcproj | 16 -- src/Makefile.am | 2 - src/descriptor.c | 54 ++-- src/device.c | 8 - src/parser.c | 3 - src/uwatec_g2.c | 413 --------------------------- src/uwatec_g2.h | 40 --- src/uwatec_meridian.c | 469 ------------------------------- src/uwatec_meridian.h | 40 --- src/uwatec_smart.c | 270 +++++++++++++++++- 12 files changed, 290 insertions(+), 1031 deletions(-) delete mode 100644 src/uwatec_g2.c delete mode 100644 src/uwatec_g2.h delete mode 100644 src/uwatec_meridian.c delete mode 100644 src/uwatec_meridian.h diff --git a/examples/common.c b/examples/common.c index 09635f0..785c8d0 100644 --- a/examples/common.c +++ b/examples/common.c @@ -65,8 +65,6 @@ static const backend_table_t g_backends[] = { {"aladin", DC_FAMILY_UWATEC_ALADIN, 0x3F}, {"memomouse", DC_FAMILY_UWATEC_MEMOMOUSE, 0}, {"smart", DC_FAMILY_UWATEC_SMART, 0x10}, - {"meridian", DC_FAMILY_UWATEC_MERIDIAN, 0x20}, - {"g2", DC_FAMILY_UWATEC_G2, 0x11}, {"sensus", DC_FAMILY_REEFNET_SENSUS, 1}, {"sensuspro", DC_FAMILY_REEFNET_SENSUSPRO, 2}, {"sensusultra", DC_FAMILY_REEFNET_SENSUSULTRA, 3}, diff --git a/include/libdivecomputer/common.h b/include/libdivecomputer/common.h index db6d8eb..a82a6d2 100644 --- a/include/libdivecomputer/common.h +++ b/include/libdivecomputer/common.h @@ -68,8 +68,8 @@ typedef enum dc_family_t { DC_FAMILY_UWATEC_ALADIN = (3 << 16), DC_FAMILY_UWATEC_MEMOMOUSE, DC_FAMILY_UWATEC_SMART, - DC_FAMILY_UWATEC_MERIDIAN, - DC_FAMILY_UWATEC_G2, + DC_FAMILY_UWATEC_MERIDIAN, /* Deprecated: integrated into the Uwatec Smart family. */ + DC_FAMILY_UWATEC_G2, /* Deprecated: integrated into the Uwatec Smart family. */ /* Oceanic */ DC_FAMILY_OCEANIC_VTPRO = (4 << 16), DC_FAMILY_OCEANIC_VEO250, diff --git a/msvc/libdivecomputer.vcproj b/msvc/libdivecomputer.vcproj index 64e2966..915807b 100644 --- a/msvc/libdivecomputer.vcproj +++ b/msvc/libdivecomputer.vcproj @@ -494,10 +494,6 @@ RelativePath="..\src\uwatec_aladin.c" > - - @@ -506,10 +502,6 @@ RelativePath="..\src\uwatec_memomouse_parser.c" > - - @@ -840,18 +832,10 @@ RelativePath="..\src\uwatec_aladin.h" > - - - - diff --git a/src/Makefile.am b/src/Makefile.am index 730553a..5ed2dd4 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -39,8 +39,6 @@ libdivecomputer_la_SOURCES = \ uwatec_aladin.h uwatec_aladin.c \ uwatec_memomouse.h uwatec_memomouse.c uwatec_memomouse_parser.c \ uwatec_smart.h uwatec_smart.c uwatec_smart_parser.c \ - uwatec_meridian.h uwatec_meridian.c \ - uwatec_g2.h uwatec_g2.c \ oceanic_common.h oceanic_common.c \ oceanic_atom2.h oceanic_atom2.c oceanic_atom2_parser.c \ oceanic_veo250.h oceanic_veo250.c oceanic_veo250_parser.c \ diff --git a/src/descriptor.c b/src/descriptor.c index 6ec4031..6dd2a31 100644 --- a/src/descriptor.c +++ b/src/descriptor.c @@ -111,34 +111,32 @@ static const dc_descriptor_t g_descriptors[] = { /* Uwatec Memomouse */ {"Uwatec", "Memomouse", DC_FAMILY_UWATEC_MEMOMOUSE, 0, DC_TRANSPORT_SERIAL, NULL}, /* Uwatec Smart */ - {"Uwatec", "Smart Pro", DC_FAMILY_UWATEC_SMART, 0x10, DC_TRANSPORT_IRDA, dc_filter_uwatec}, - {"Uwatec", "Galileo Sol", DC_FAMILY_UWATEC_SMART, 0x11, DC_TRANSPORT_IRDA, dc_filter_uwatec}, - {"Uwatec", "Galileo Luna", DC_FAMILY_UWATEC_SMART, 0x11, DC_TRANSPORT_IRDA, dc_filter_uwatec}, - {"Uwatec", "Galileo Terra", DC_FAMILY_UWATEC_SMART, 0x11, DC_TRANSPORT_IRDA, dc_filter_uwatec}, - {"Uwatec", "Aladin Tec", DC_FAMILY_UWATEC_SMART, 0x12, DC_TRANSPORT_IRDA, dc_filter_uwatec}, - {"Uwatec", "Aladin Prime", DC_FAMILY_UWATEC_SMART, 0x12, DC_TRANSPORT_IRDA, dc_filter_uwatec}, - {"Uwatec", "Aladin Tec 2G", DC_FAMILY_UWATEC_SMART, 0x13, DC_TRANSPORT_IRDA, dc_filter_uwatec}, - {"Uwatec", "Aladin 2G", DC_FAMILY_UWATEC_SMART, 0x13, DC_TRANSPORT_IRDA, dc_filter_uwatec}, - {"Subgear","XP-10", DC_FAMILY_UWATEC_SMART, 0x13, DC_TRANSPORT_IRDA, dc_filter_uwatec}, - {"Uwatec", "Smart Com", DC_FAMILY_UWATEC_SMART, 0x14, DC_TRANSPORT_IRDA, dc_filter_uwatec}, - {"Uwatec", "Aladin 2G", DC_FAMILY_UWATEC_SMART, 0x15, DC_TRANSPORT_IRDA, dc_filter_uwatec}, - {"Uwatec", "Aladin Tec 3G", DC_FAMILY_UWATEC_SMART, 0x15, DC_TRANSPORT_IRDA, dc_filter_uwatec}, - {"Uwatec", "Aladin Sport", DC_FAMILY_UWATEC_SMART, 0x15, DC_TRANSPORT_IRDA, dc_filter_uwatec}, - {"Subgear","XP-3G", DC_FAMILY_UWATEC_SMART, 0x15, DC_TRANSPORT_IRDA, dc_filter_uwatec}, - {"Uwatec", "Smart Tec", DC_FAMILY_UWATEC_SMART, 0x18, DC_TRANSPORT_IRDA, dc_filter_uwatec}, - {"Uwatec", "Galileo Trimix",DC_FAMILY_UWATEC_SMART, 0x19, DC_TRANSPORT_IRDA, dc_filter_uwatec}, - {"Uwatec", "Smart Z", DC_FAMILY_UWATEC_SMART, 0x1C, DC_TRANSPORT_IRDA, dc_filter_uwatec}, - {"Subgear","XP Air", DC_FAMILY_UWATEC_SMART, 0x1C, DC_TRANSPORT_IRDA, dc_filter_uwatec}, - /* Scubapro/Uwatec Meridian */ - {"Scubapro", "Meridian", DC_FAMILY_UWATEC_MERIDIAN, 0x20, DC_TRANSPORT_SERIAL, NULL}, - {"Scubapro", "Mantis", DC_FAMILY_UWATEC_MERIDIAN, 0x20, DC_TRANSPORT_SERIAL, NULL}, - {"Scubapro", "Chromis", DC_FAMILY_UWATEC_MERIDIAN, 0x24, DC_TRANSPORT_SERIAL, NULL}, - {"Scubapro", "Mantis 2", DC_FAMILY_UWATEC_MERIDIAN, 0x26, DC_TRANSPORT_SERIAL, NULL}, - /* Scubapro G2 */ - {"Scubapro", "Aladin Sport Matrix", DC_FAMILY_UWATEC_G2, 0x17, DC_TRANSPORT_BLE, dc_filter_uwatec}, - {"Scubapro", "Aladin Square", DC_FAMILY_UWATEC_G2, 0x22, DC_TRANSPORT_USBHID, dc_filter_uwatec}, - {"Scubapro", "G2", DC_FAMILY_UWATEC_G2, 0x32, DC_TRANSPORT_USBHID | DC_TRANSPORT_BLE, dc_filter_uwatec}, - {"Scubapro", "G2 Console", DC_FAMILY_UWATEC_G2, 0x32, DC_TRANSPORT_USBHID | DC_TRANSPORT_BLE, dc_filter_uwatec}, + {"Uwatec", "Smart Pro", DC_FAMILY_UWATEC_SMART, 0x10, DC_TRANSPORT_IRDA, dc_filter_uwatec}, + {"Uwatec", "Galileo Sol", DC_FAMILY_UWATEC_SMART, 0x11, DC_TRANSPORT_IRDA, dc_filter_uwatec}, + {"Uwatec", "Galileo Luna", DC_FAMILY_UWATEC_SMART, 0x11, DC_TRANSPORT_IRDA, dc_filter_uwatec}, + {"Uwatec", "Galileo Terra", DC_FAMILY_UWATEC_SMART, 0x11, DC_TRANSPORT_IRDA, dc_filter_uwatec}, + {"Uwatec", "Aladin Tec", DC_FAMILY_UWATEC_SMART, 0x12, DC_TRANSPORT_IRDA, dc_filter_uwatec}, + {"Uwatec", "Aladin Prime", DC_FAMILY_UWATEC_SMART, 0x12, DC_TRANSPORT_IRDA, dc_filter_uwatec}, + {"Uwatec", "Aladin Tec 2G", DC_FAMILY_UWATEC_SMART, 0x13, DC_TRANSPORT_IRDA, dc_filter_uwatec}, + {"Uwatec", "Aladin 2G", DC_FAMILY_UWATEC_SMART, 0x13, DC_TRANSPORT_IRDA, dc_filter_uwatec}, + {"Subgear", "XP-10", DC_FAMILY_UWATEC_SMART, 0x13, DC_TRANSPORT_IRDA, dc_filter_uwatec}, + {"Uwatec", "Smart Com", DC_FAMILY_UWATEC_SMART, 0x14, DC_TRANSPORT_IRDA, dc_filter_uwatec}, + {"Uwatec", "Aladin 2G", DC_FAMILY_UWATEC_SMART, 0x15, DC_TRANSPORT_IRDA, dc_filter_uwatec}, + {"Uwatec", "Aladin Tec 3G", DC_FAMILY_UWATEC_SMART, 0x15, DC_TRANSPORT_IRDA, dc_filter_uwatec}, + {"Uwatec", "Aladin Sport", DC_FAMILY_UWATEC_SMART, 0x15, DC_TRANSPORT_IRDA, dc_filter_uwatec}, + {"Subgear", "XP-3G", DC_FAMILY_UWATEC_SMART, 0x15, DC_TRANSPORT_IRDA, dc_filter_uwatec}, + {"Scubapro", "Aladin Sport Matrix", DC_FAMILY_UWATEC_SMART, 0x17, DC_TRANSPORT_BLE, dc_filter_uwatec}, + {"Uwatec", "Smart Tec", DC_FAMILY_UWATEC_SMART, 0x18, DC_TRANSPORT_IRDA, dc_filter_uwatec}, + {"Uwatec", "Galileo Trimix", DC_FAMILY_UWATEC_SMART, 0x19, DC_TRANSPORT_IRDA, dc_filter_uwatec}, + {"Uwatec", "Smart Z", DC_FAMILY_UWATEC_SMART, 0x1C, DC_TRANSPORT_IRDA, dc_filter_uwatec}, + {"Subgear", "XP Air", DC_FAMILY_UWATEC_SMART, 0x1C, DC_TRANSPORT_IRDA, dc_filter_uwatec}, + {"Scubapro", "Meridian", DC_FAMILY_UWATEC_SMART, 0x20, DC_TRANSPORT_SERIAL, NULL}, + {"Scubapro", "Mantis", DC_FAMILY_UWATEC_SMART, 0x20, DC_TRANSPORT_SERIAL, NULL}, + {"Scubapro", "Aladin Square", DC_FAMILY_UWATEC_SMART, 0x22, DC_TRANSPORT_USBHID, dc_filter_uwatec}, + {"Scubapro", "Chromis", DC_FAMILY_UWATEC_SMART, 0x24, DC_TRANSPORT_SERIAL, NULL}, + {"Scubapro", "Mantis 2", DC_FAMILY_UWATEC_SMART, 0x26, DC_TRANSPORT_SERIAL, NULL}, + {"Scubapro", "G2", DC_FAMILY_UWATEC_SMART, 0x32, DC_TRANSPORT_USBHID | DC_TRANSPORT_BLE, dc_filter_uwatec}, + {"Scubapro", "G2 Console", DC_FAMILY_UWATEC_SMART, 0x32, DC_TRANSPORT_USBHID | DC_TRANSPORT_BLE, dc_filter_uwatec}, /* Reefnet */ {"Reefnet", "Sensus", DC_FAMILY_REEFNET_SENSUS, 1, DC_TRANSPORT_SERIAL, NULL}, {"Reefnet", "Sensus Pro", DC_FAMILY_REEFNET_SENSUSPRO, 2, DC_TRANSPORT_SERIAL, NULL}, diff --git a/src/device.c b/src/device.c index 885fd10..83c71dc 100644 --- a/src/device.c +++ b/src/device.c @@ -33,9 +33,7 @@ #include "reefnet_sensuspro.h" #include "reefnet_sensusultra.h" #include "uwatec_aladin.h" -#include "uwatec_g2.h" #include "uwatec_memomouse.h" -#include "uwatec_meridian.h" #include "uwatec_smart.h" #include "oceanic_atom2.h" #include "oceanic_veo250.h" @@ -136,12 +134,6 @@ dc_device_open (dc_device_t **out, dc_context_t *context, dc_descriptor_t *descr case DC_FAMILY_UWATEC_SMART: rc = uwatec_smart_device_open (&device, context, iostream); break; - case DC_FAMILY_UWATEC_MERIDIAN: - rc = uwatec_meridian_device_open (&device, context, iostream); - break; - case DC_FAMILY_UWATEC_G2: - rc = uwatec_g2_device_open (&device, context, iostream, dc_descriptor_get_model (descriptor)); - break; case DC_FAMILY_REEFNET_SENSUS: rc = reefnet_sensus_device_open (&device, context, iostream); break; diff --git a/src/parser.c b/src/parser.c index 29a5d41..366f8b7 100644 --- a/src/parser.c +++ b/src/parser.c @@ -33,7 +33,6 @@ #include "reefnet_sensusultra.h" #include "uwatec_aladin.h" #include "uwatec_memomouse.h" -#include "uwatec_meridian.h" #include "uwatec_smart.h" #include "oceanic_atom2.h" #include "oceanic_atom2.h" @@ -97,8 +96,6 @@ dc_parser_new_internal (dc_parser_t **out, dc_context_t *context, dc_family_t fa rc = uwatec_memomouse_parser_create (&parser, context, devtime, systime); break; case DC_FAMILY_UWATEC_SMART: - case DC_FAMILY_UWATEC_MERIDIAN: - case DC_FAMILY_UWATEC_G2: rc = uwatec_smart_parser_create (&parser, context, model, devtime, systime); break; case DC_FAMILY_REEFNET_SENSUS: diff --git a/src/uwatec_g2.c b/src/uwatec_g2.c deleted file mode 100644 index 798cc9a..0000000 --- a/src/uwatec_g2.c +++ /dev/null @@ -1,413 +0,0 @@ -/* - * libdivecomputer - * - * Copyright (C) 2008 Jef Driesen - * (C) 2017 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 // malloc, free -#include // strncmp, strstr - -#include "uwatec_g2.h" -#include "context-private.h" -#include "device-private.h" -#include "array.h" -#include "platform.h" - -#define ISINSTANCE(device) dc_device_isinstance((device), &uwatec_g2_device_vtable) - -#define RX_PACKET_SIZE 64 -#define TX_PACKET_SIZE 32 - -#define ALADINSQUARE 0x22 - -typedef struct uwatec_g2_device_t { - dc_device_t base; - dc_iostream_t *iostream; - unsigned int timestamp; - unsigned int devtime; - dc_ticks_t systime; -} uwatec_g2_device_t; - -static dc_status_t uwatec_g2_device_set_fingerprint (dc_device_t *device, const unsigned char data[], unsigned int size); -static dc_status_t uwatec_g2_device_dump (dc_device_t *abstract, dc_buffer_t *buffer); -static dc_status_t uwatec_g2_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata); - -static const dc_device_vtable_t uwatec_g2_device_vtable = { - sizeof(uwatec_g2_device_t), - DC_FAMILY_UWATEC_G2, - uwatec_g2_device_set_fingerprint, /* set_fingerprint */ - NULL, /* read */ - NULL, /* write */ - uwatec_g2_device_dump, /* dump */ - uwatec_g2_device_foreach, /* foreach */ - NULL, /* timesync */ - NULL /* close */ -}; - -static dc_status_t -uwatec_g2_extract_dives (dc_device_t *device, const unsigned char data[], unsigned int size, dc_dive_callback_t callback, void *userdata); - -static dc_status_t -receive_data (uwatec_g2_device_t *device, dc_event_progress_t *progress, unsigned char *data, unsigned int size) -{ - while (size) { - unsigned char buf[RX_PACKET_SIZE]; - size_t transferred = 0; - dc_status_t rc = DC_STATUS_SUCCESS; - unsigned int len = 0; - - rc = dc_iostream_read (device->iostream, buf, sizeof(buf), &transferred); - if (rc != DC_STATUS_SUCCESS) { - ERROR (device->base.context, "Failed to receive the packet."); - return rc; - } - - if (transferred < 1) { - ERROR (device->base.context, "Invalid packet length (" DC_PRINTF_SIZE ").", transferred); - return DC_STATUS_PROTOCOL; - } - - len = buf[0]; - if (len + 1 > transferred) { - ERROR (device->base.context, "Invalid payload length (%u).", len); - return DC_STATUS_PROTOCOL; - } - - HEXDUMP (device->base.context, DC_LOGLEVEL_DEBUG, "rcv", buf + 1, len); - - if (len > size) { - ERROR (device->base.context, "Insufficient buffer space available."); - return DC_STATUS_PROTOCOL; - } - - // Update and emit a progress event. - if (progress) { - progress->current += len; - device_event_emit (&device->base, DC_EVENT_PROGRESS, progress); - } - - memcpy(data, buf + 1, len); - size -= len; - data += len; - } - return DC_STATUS_SUCCESS; -} - - -static dc_status_t -uwatec_g2_transfer (uwatec_g2_device_t *device, const unsigned char command[], unsigned int csize, unsigned char answer[], unsigned int asize) -{ - unsigned char buf[TX_PACKET_SIZE + 1]; - dc_status_t status = DC_STATUS_SUCCESS; - size_t transferred = 0; - - if (csize + 2 > sizeof(buf)) { - ERROR (device->base.context, "command too big (%d)", csize); - return DC_STATUS_INVALIDARGS; - } - - HEXDUMP (device->base.context, DC_LOGLEVEL_DEBUG, "cmd", command, csize); - - buf[0] = 0; - buf[1] = csize; - memcpy(buf + 2, command, csize); - memset(buf + 2 + csize, 0, sizeof(buf) - (csize + 2)); - - if (dc_iostream_get_transport(device->iostream) == DC_TRANSPORT_BLE) { - status = dc_iostream_write(device->iostream, buf + 1, csize + 1, &transferred); - } else { - status = dc_iostream_write(device->iostream, buf, sizeof(buf), &transferred); - } - if (status != DC_STATUS_SUCCESS) { - ERROR (device->base.context, "Failed to send the command."); - return status; - } - - status = receive_data (device, NULL, answer, asize); - if (status != DC_STATUS_SUCCESS) { - ERROR (device->base.context, "Failed to receive the answer."); - return status; - } - - return DC_STATUS_SUCCESS; -} - - -static dc_status_t -uwatec_g2_handshake (uwatec_g2_device_t *device) -{ - dc_device_t *abstract = (dc_device_t *) device; - - // Command template. - unsigned char answer[1] = {0}; - unsigned char command[5] = {0x00, 0x10, 0x27, 0, 0}; - - // Handshake (stage 1). - command[0] = 0x1B; - dc_status_t rc = uwatec_g2_transfer (device, command, 1, answer, 1); - if (rc != DC_STATUS_SUCCESS) - return rc; - - // Verify the answer. - if (answer[0] != 0x01) { - ERROR (abstract->context, "Unexpected answer byte(s)."); - return DC_STATUS_PROTOCOL; - } - - // Handshake (stage 2). - command[0] = 0x1C; - rc = uwatec_g2_transfer (device, command, 5, answer, 1); - if (rc != DC_STATUS_SUCCESS) - return rc; - - // Verify the answer. - if (answer[0] != 0x01) { - ERROR (abstract->context, "Unexpected answer byte(s)."); - return DC_STATUS_PROTOCOL; - } - - return DC_STATUS_SUCCESS; -} - - -dc_status_t -uwatec_g2_device_open (dc_device_t **out, dc_context_t *context, dc_iostream_t *iostream, unsigned int model) -{ - dc_status_t status = DC_STATUS_SUCCESS; - uwatec_g2_device_t *device = NULL; - - if (out == NULL) - return DC_STATUS_INVALIDARGS; - - // Allocate memory. - device = (uwatec_g2_device_t *) dc_device_allocate (context, &uwatec_g2_device_vtable); - if (device == NULL) { - ERROR (context, "Failed to allocate memory."); - return DC_STATUS_NOMEMORY; - } - - // Set the default values. - device->iostream = iostream; - device->timestamp = 0; - device->systime = (dc_ticks_t) -1; - device->devtime = 0; - - // Perform the handshaking. - status = uwatec_g2_handshake (device); - if (status != DC_STATUS_SUCCESS) { - ERROR (context, "Failed to handshake with the device."); - goto error_free; - } - - *out = (dc_device_t*) device; - - return DC_STATUS_SUCCESS; - -error_free: - dc_device_deallocate ((dc_device_t *) device); - return status; -} - - -static dc_status_t -uwatec_g2_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size) -{ - uwatec_g2_device_t *device = (uwatec_g2_device_t*) abstract; - - if (size && size != 4) - return DC_STATUS_INVALIDARGS; - - if (size) - device->timestamp = array_uint32_le (data); - else - device->timestamp = 0; - - return DC_STATUS_SUCCESS; -} - - -static dc_status_t -uwatec_g2_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) -{ - uwatec_g2_device_t *device = (uwatec_g2_device_t*) abstract; - dc_status_t rc = DC_STATUS_SUCCESS; - - // Enable progress notifications. - dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER; - device_event_emit (&device->base, DC_EVENT_PROGRESS, &progress); - - // Read the model number. - unsigned char cmd_model[1] = {0x10}; - unsigned char model[1] = {0}; - rc = uwatec_g2_transfer (device, cmd_model, sizeof (cmd_model), model, sizeof (model)); - if (rc != DC_STATUS_SUCCESS) - return rc; - - // Read the serial number. - unsigned char cmd_serial[1] = {0x14}; - unsigned char serial[4] = {0}; - rc = uwatec_g2_transfer (device, cmd_serial, sizeof (cmd_serial), serial, sizeof (serial)); - if (rc != DC_STATUS_SUCCESS) - return rc; - - // Read the device clock. - unsigned char cmd_devtime[1] = {0x1A}; - unsigned char devtime[4] = {0}; - rc = uwatec_g2_transfer (device, cmd_devtime, sizeof (cmd_devtime), devtime, sizeof (devtime)); - if (rc != DC_STATUS_SUCCESS) - return rc; - - // Store the clock calibration values. - device->systime = dc_datetime_now (); - device->devtime = array_uint32_le (devtime); - - // Update and emit a progress event. - progress.current += 9; - device_event_emit (&device->base, DC_EVENT_PROGRESS, &progress); - - // Emit a clock event. - dc_event_clock_t clock; - clock.systime = device->systime; - clock.devtime = device->devtime; - device_event_emit (&device->base, DC_EVENT_CLOCK, &clock); - - // Emit a device info event. - dc_event_devinfo_t devinfo; - devinfo.model = model[0]; - devinfo.firmware = 0; - devinfo.serial = array_uint32_le (serial); - device_event_emit (&device->base, DC_EVENT_DEVINFO, &devinfo); - - // Command template. - unsigned char command[9] = {0x00, - (device->timestamp ) & 0xFF, - (device->timestamp >> 8 ) & 0xFF, - (device->timestamp >> 16) & 0xFF, - (device->timestamp >> 24) & 0xFF, - 0x10, - 0x27, - 0, - 0}; - - // Data Length. - command[0] = 0xC6; - unsigned char answer[4] = {0}; - rc = uwatec_g2_transfer (device, command, sizeof (command), answer, sizeof (answer)); - if (rc != DC_STATUS_SUCCESS) - return rc; - - unsigned int length = array_uint32_le (answer); - - // Update and emit a progress event. - progress.maximum = 4 + 9 + (length ? length + 4 : 0); - progress.current += 4; - device_event_emit (&device->base, DC_EVENT_PROGRESS, &progress); - - if (length == 0) - return DC_STATUS_SUCCESS; - - // Allocate the required amount of memory. - if (!dc_buffer_resize (buffer, length)) { - ERROR (abstract->context, "Insufficient buffer space available."); - return DC_STATUS_NOMEMORY; - } - - unsigned char *data = dc_buffer_get_data (buffer); - - // Data. - command[0] = 0xC4; - rc = uwatec_g2_transfer (device, command, sizeof (command), answer, sizeof (answer)); - if (rc != DC_STATUS_SUCCESS) - return rc; - - unsigned int total = array_uint32_le (answer); - - // Update and emit a progress event. - progress.current += 4; - device_event_emit (&device->base, DC_EVENT_PROGRESS, &progress); - - if (total != length + 4) { - ERROR (abstract->context, "Received an unexpected size."); - return DC_STATUS_PROTOCOL; - } - - rc = receive_data (device, &progress, data, length); - if (rc != DC_STATUS_SUCCESS) { - ERROR (abstract->context, "Failed to receive the answer."); - return rc; - } - - return DC_STATUS_SUCCESS; -} - - -static dc_status_t -uwatec_g2_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata) -{ - dc_buffer_t *buffer = dc_buffer_new (0); - if (buffer == NULL) - return DC_STATUS_NOMEMORY; - - dc_status_t rc = uwatec_g2_device_dump (abstract, buffer); - if (rc != DC_STATUS_SUCCESS) { - dc_buffer_free (buffer); - return rc; - } - - rc = uwatec_g2_extract_dives (abstract, - dc_buffer_get_data (buffer), dc_buffer_get_size (buffer), callback, userdata); - - dc_buffer_free (buffer); - - return rc; -} - - -static dc_status_t -uwatec_g2_extract_dives (dc_device_t *abstract, const unsigned char data[], unsigned int size, dc_dive_callback_t callback, void *userdata) -{ - if (abstract && !ISINSTANCE (abstract)) - return DC_STATUS_INVALIDARGS; - - const unsigned char header[4] = {0xa5, 0xa5, 0x5a, 0x5a}; - - // Search the data stream for start markers. - unsigned int previous = size; - unsigned int current = (size >= 4 ? size - 4 : 0); - while (current > 0) { - current--; - if (memcmp (data + current, header, sizeof (header)) == 0) { - // Get the length of the profile data. - unsigned int len = array_uint32_le (data + current + 4); - - // Check for a buffer overflow. - if (current + len > previous) - return DC_STATUS_DATAFORMAT; - - if (callback && !callback (data + current, len, data + current + 8, 4, userdata)) - return DC_STATUS_SUCCESS; - - // Prepare for the next dive. - previous = current; - current = (current >= 4 ? current - 4 : 0); - } - } - - return DC_STATUS_SUCCESS; -} diff --git a/src/uwatec_g2.h b/src/uwatec_g2.h deleted file mode 100644 index 72a4390..0000000 --- a/src/uwatec_g2.h +++ /dev/null @@ -1,40 +0,0 @@ -/* - * libdivecomputer - * - * Copyright (C) 2008 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 UWATEC_G2_H -#define UWATEC_G2_H - -#include -#include -#include -#include - -#ifdef __cplusplus -extern "C" { -#endif /* __cplusplus */ - -dc_status_t -uwatec_g2_device_open (dc_device_t **device, dc_context_t *context, dc_iostream_t *iostream, unsigned int model); - -#ifdef __cplusplus -} -#endif /* __cplusplus */ -#endif /* UWATEC_G2_H */ diff --git a/src/uwatec_meridian.c b/src/uwatec_meridian.c deleted file mode 100644 index 378facb..0000000 --- a/src/uwatec_meridian.c +++ /dev/null @@ -1,469 +0,0 @@ -/* - * libdivecomputer - * - * Copyright (C) 2013 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 - -#include "uwatec_meridian.h" -#include "context-private.h" -#include "device-private.h" -#include "checksum.h" -#include "array.h" - -#define ISINSTANCE(device) dc_device_isinstance((device), &uwatec_meridian_device_vtable) - -#define ACK 0x11 -#define NAK 0x66 - -typedef struct uwatec_meridian_device_t { - dc_device_t base; - dc_iostream_t *iostream; - unsigned int timestamp; - unsigned int devtime; - dc_ticks_t systime; -} uwatec_meridian_device_t; - -static dc_status_t uwatec_meridian_device_set_fingerprint (dc_device_t *device, const unsigned char data[], unsigned int size); -static dc_status_t uwatec_meridian_device_dump (dc_device_t *abstract, dc_buffer_t *buffer); -static dc_status_t uwatec_meridian_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata); - -static const dc_device_vtable_t uwatec_meridian_device_vtable = { - sizeof(uwatec_meridian_device_t), - DC_FAMILY_UWATEC_MERIDIAN, - uwatec_meridian_device_set_fingerprint, /* set_fingerprint */ - NULL, /* read */ - NULL, /* write */ - uwatec_meridian_device_dump, /* dump */ - uwatec_meridian_device_foreach, /* foreach */ - NULL, /* timesync */ - NULL /* close */ -}; - -static dc_status_t -uwatec_meridian_extract_dives (dc_device_t *device, const unsigned char data[], unsigned int size, dc_dive_callback_t callback, void *userdata); - -static dc_status_t -uwatec_meridian_transfer (uwatec_meridian_device_t *device, const unsigned char command[], unsigned int csize, unsigned char answer[], unsigned int asize) -{ - dc_status_t status = DC_STATUS_SUCCESS; - dc_device_t *abstract = (dc_device_t *) device; - - assert (csize > 0 && csize <= 255); - - // Build the packet. - unsigned char packet[255 + 12] = { - 0xFF, 0xFF, 0xFF, - 0xA6, 0x59, 0xBD, 0xC2, - 0x00, /* length */ - 0x00, 0x00, 0x00, - 0x00}; /* data and checksum */ - memcpy (packet + 11, command, csize); - packet[7] = csize; - packet[11 + csize] = checksum_xor_uint8 (packet + 7, csize + 4, 0x00); - - // Send the packet. - status = dc_iostream_write (device->iostream, packet, csize + 12, NULL); - if (status != DC_STATUS_SUCCESS) { - ERROR (abstract->context, "Failed to send the command."); - return status; - } - - // Read the echo. - unsigned char echo[sizeof(packet)]; - status = dc_iostream_read (device->iostream, echo, csize + 12, NULL); - if (status != DC_STATUS_SUCCESS) { - ERROR (abstract->context, "Failed to receive the echo."); - return status; - } - - // Verify the echo. - if (memcmp (echo, packet, csize + 12) != 0) { - WARNING (abstract->context, "Unexpected echo."); - return DC_STATUS_PROTOCOL; - } - - // Read the header. - unsigned char header[6]; - status = dc_iostream_read (device->iostream, header, sizeof (header), NULL); - if (status != DC_STATUS_SUCCESS) { - ERROR (abstract->context, "Failed to receive the header."); - return status; - } - - // Verify the header. - if (header[0] != ACK || array_uint32_le (header + 1) != asize + 1 || header[5] != packet[11]) { - WARNING (abstract->context, "Unexpected header."); - return DC_STATUS_PROTOCOL; - } - - // Read the packet. - status = dc_iostream_read (device->iostream, answer, asize, NULL); - if (status != DC_STATUS_SUCCESS) { - ERROR (abstract->context, "Failed to receive the packet."); - return status; - } - - // Read the checksum. - unsigned char csum = 0x00; - status = dc_iostream_read (device->iostream, &csum, sizeof (csum), NULL); - if (status != DC_STATUS_SUCCESS) { - ERROR (abstract->context, "Failed to receive the checksum."); - return status; - } - - // Verify the checksum. - unsigned char ccsum = 0x00; - ccsum = checksum_xor_uint8 (header + 1, sizeof (header) - 1, ccsum); - ccsum = checksum_xor_uint8 (answer, asize, ccsum); - if (csum != ccsum) { - ERROR (abstract->context, "Unexpected answer checksum."); - return DC_STATUS_PROTOCOL; - } - - return DC_STATUS_SUCCESS; -} - - -static dc_status_t -uwatec_meridian_handshake (uwatec_meridian_device_t *device) -{ - dc_device_t *abstract = (dc_device_t *) device; - - // Command template. - unsigned char answer[1] = {0}; - unsigned char command[5] = {0x00, 0x10, 0x27, 0, 0}; - - // Handshake (stage 1). - command[0] = 0x1B; - dc_status_t rc = uwatec_meridian_transfer (device, command, 1, answer, 1); - if (rc != DC_STATUS_SUCCESS) - return rc; - - // Verify the answer. - if (answer[0] != 0x01) { - ERROR (abstract->context, "Unexpected answer byte(s)."); - return DC_STATUS_PROTOCOL; - } - - // Handshake (stage 2). - command[0] = 0x1C; - rc = uwatec_meridian_transfer (device, command, 5, answer, 1); - if (rc != DC_STATUS_SUCCESS) - return rc; - - // Verify the answer. - if (answer[0] != 0x01) { - ERROR (abstract->context, "Unexpected answer byte(s)."); - return DC_STATUS_PROTOCOL; - } - - return DC_STATUS_SUCCESS; -} - - -dc_status_t -uwatec_meridian_device_open (dc_device_t **out, dc_context_t *context, dc_iostream_t *iostream) -{ - dc_status_t status = DC_STATUS_SUCCESS; - uwatec_meridian_device_t *device = NULL; - - if (out == NULL) - return DC_STATUS_INVALIDARGS; - - // Allocate memory. - device = (uwatec_meridian_device_t *) dc_device_allocate (context, &uwatec_meridian_device_vtable); - if (device == NULL) { - ERROR (context, "Failed to allocate memory."); - return DC_STATUS_NOMEMORY; - } - - // Set the default values. - device->iostream = iostream; - device->timestamp = 0; - device->systime = (dc_ticks_t) -1; - device->devtime = 0; - - // Set the serial communication protocol (57600 8N1). - status = dc_iostream_configure (device->iostream, 57600, 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 (3000ms). - status = dc_iostream_set_timeout (device->iostream, 3000); - if (status != DC_STATUS_SUCCESS) { - ERROR (context, "Failed to set the timeout."); - goto error_free; - } - - // Make sure everything is in a sane state. - dc_iostream_purge (device->iostream, DC_DIRECTION_ALL); - - // Perform the handshaking. - status = uwatec_meridian_handshake (device); - if (status != DC_STATUS_SUCCESS) { - ERROR (context, "Failed to handshake with the device."); - goto error_free; - } - - *out = (dc_device_t*) device; - - return DC_STATUS_SUCCESS; - -error_free: - dc_device_deallocate ((dc_device_t *) device); - return status; -} - - -static dc_status_t -uwatec_meridian_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size) -{ - uwatec_meridian_device_t *device = (uwatec_meridian_device_t*) abstract; - - if (size && size != 4) - return DC_STATUS_INVALIDARGS; - - if (size) - device->timestamp = array_uint32_le (data); - else - device->timestamp = 0; - - return DC_STATUS_SUCCESS; -} - - -static dc_status_t -uwatec_meridian_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) -{ - dc_status_t status = DC_STATUS_SUCCESS; - uwatec_meridian_device_t *device = (uwatec_meridian_device_t*) abstract; - dc_status_t rc = DC_STATUS_SUCCESS; - - // Enable progress notifications. - dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER; - device_event_emit (&device->base, DC_EVENT_PROGRESS, &progress); - - // Command template. - unsigned char command[9] = {0x00, - (device->timestamp ) & 0xFF, - (device->timestamp >> 8 ) & 0xFF, - (device->timestamp >> 16) & 0xFF, - (device->timestamp >> 24) & 0xFF, - 0x10, - 0x27, - 0, - 0}; - - // Read the model number. - command[0] = 0x10; - unsigned char model[1] = {0}; - rc = uwatec_meridian_transfer (device, command, 1, model, sizeof (model)); - if (rc != DC_STATUS_SUCCESS) - return rc; - - // Read the serial number. - command[0] = 0x14; - unsigned char serial[4] = {0}; - rc = uwatec_meridian_transfer (device, command, 1, serial, sizeof (serial)); - if (rc != DC_STATUS_SUCCESS) - return rc; - - // Read the device clock. - command[0] = 0x1A; - unsigned char devtime[4] = {0}; - rc = uwatec_meridian_transfer (device, command, 1, devtime, sizeof (devtime)); - if (rc != DC_STATUS_SUCCESS) - return rc; - - // Store the clock calibration values. - device->systime = dc_datetime_now (); - device->devtime = array_uint32_le (devtime); - - // Update and emit a progress event. - progress.current += 9; - device_event_emit (&device->base, DC_EVENT_PROGRESS, &progress); - - // Emit a clock event. - dc_event_clock_t clock; - clock.systime = device->systime; - clock.devtime = device->devtime; - device_event_emit (&device->base, DC_EVENT_CLOCK, &clock); - - // Emit a device info event. - dc_event_devinfo_t devinfo; - devinfo.model = model[0]; - devinfo.firmware = 0; - devinfo.serial = array_uint32_le (serial); - device_event_emit (&device->base, DC_EVENT_DEVINFO, &devinfo); - - // Data Length. - command[0] = 0xC6; - unsigned char answer[4] = {0}; - rc = uwatec_meridian_transfer (device, command, sizeof (command), answer, sizeof (answer)); - if (rc != DC_STATUS_SUCCESS) - return rc; - - unsigned int length = array_uint32_le (answer); - - // Update and emit a progress event. - progress.maximum = 4 + 9 + (length ? length + 4 : 0); - progress.current += 4; - device_event_emit (&device->base, DC_EVENT_PROGRESS, &progress); - - if (length == 0) - return DC_STATUS_SUCCESS; - - // Allocate the required amount of memory. - if (!dc_buffer_resize (buffer, length)) { - ERROR (abstract->context, "Insufficient buffer space available."); - return DC_STATUS_NOMEMORY; - } - - unsigned char *data = dc_buffer_get_data (buffer); - - // Data. - command[0] = 0xC4; - rc = uwatec_meridian_transfer (device, command, sizeof (command), answer, sizeof (answer)); - if (rc != DC_STATUS_SUCCESS) - return rc; - - unsigned int total = array_uint32_le (answer); - - // Update and emit a progress event. - progress.current += 4; - device_event_emit (&device->base, DC_EVENT_PROGRESS, &progress); - - if (total != length + 4) { - ERROR (abstract->context, "Received an unexpected size."); - return DC_STATUS_PROTOCOL; - } - - unsigned int nbytes = 0; - while (nbytes < length) { - - // Read the header. - unsigned char header[5]; - status = dc_iostream_read (device->iostream, header, sizeof (header), NULL); - if (status != DC_STATUS_SUCCESS) { - ERROR (abstract->context, "Failed to receive the header."); - return status; - } - - // Get the packet size. - unsigned int packetsize = array_uint32_le (header); - if (packetsize < 1 || nbytes + packetsize - 1 > length) { - WARNING (abstract->context, "Unexpected header."); - return DC_STATUS_PROTOCOL; - } - - // Read the packet data. - status = dc_iostream_read (device->iostream, data + nbytes, packetsize - 1, NULL); - if (status != DC_STATUS_SUCCESS) { - ERROR (abstract->context, "Failed to receive the packet."); - return status; - } - - // Read the checksum. - unsigned char csum = 0x00; - status = dc_iostream_read (device->iostream, &csum, sizeof (csum), NULL); - if (status != DC_STATUS_SUCCESS) { - ERROR (abstract->context, "Failed to receive the checksum."); - return status; - } - - // Verify the checksum. - unsigned char ccsum = 0x00; - ccsum = checksum_xor_uint8 (header, sizeof (header), ccsum); - ccsum = checksum_xor_uint8 (data + nbytes, packetsize - 1, ccsum); - if (csum != ccsum) { - ERROR (abstract->context, "Unexpected answer checksum."); - return DC_STATUS_PROTOCOL; - } - - - // Update and emit a progress event. - progress.current += packetsize - 1; - device_event_emit (&device->base, DC_EVENT_PROGRESS, &progress); - - nbytes += packetsize - 1; - } - - return DC_STATUS_SUCCESS; -} - - -static dc_status_t -uwatec_meridian_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata) -{ - dc_buffer_t *buffer = dc_buffer_new (0); - if (buffer == NULL) - return DC_STATUS_NOMEMORY; - - dc_status_t rc = uwatec_meridian_device_dump (abstract, buffer); - if (rc != DC_STATUS_SUCCESS) { - dc_buffer_free (buffer); - return rc; - } - - rc = uwatec_meridian_extract_dives (abstract, - dc_buffer_get_data (buffer), dc_buffer_get_size (buffer), callback, userdata); - - dc_buffer_free (buffer); - - return rc; -} - - -static dc_status_t -uwatec_meridian_extract_dives (dc_device_t *abstract, const unsigned char data[], unsigned int size, dc_dive_callback_t callback, void *userdata) -{ - if (abstract && !ISINSTANCE (abstract)) - return DC_STATUS_INVALIDARGS; - - const unsigned char header[4] = {0xa5, 0xa5, 0x5a, 0x5a}; - - // Search the data stream for start markers. - unsigned int previous = size; - unsigned int current = (size >= 4 ? size - 4 : 0); - while (current > 0) { - current--; - if (memcmp (data + current, header, sizeof (header)) == 0) { - // Get the length of the profile data. - unsigned int len = array_uint32_le (data + current + 4); - - // Check for a buffer overflow. - if (current + len > previous) - return DC_STATUS_DATAFORMAT; - - if (callback && !callback (data + current, len, data + current + 8, 4, userdata)) - return DC_STATUS_SUCCESS; - - // Prepare for the next dive. - previous = current; - current = (current >= 4 ? current - 4 : 0); - } - } - - return DC_STATUS_SUCCESS; -} diff --git a/src/uwatec_meridian.h b/src/uwatec_meridian.h deleted file mode 100644 index 4ac80d3..0000000 --- a/src/uwatec_meridian.h +++ /dev/null @@ -1,40 +0,0 @@ -/* - * libdivecomputer - * - * Copyright (C) 2013 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 UWATEC_MERIDIAN_H -#define UWATEC_MERIDIAN_H - -#include -#include -#include -#include - -#ifdef __cplusplus -extern "C" { -#endif /* __cplusplus */ - -dc_status_t -uwatec_meridian_device_open (dc_device_t **device, dc_context_t *context, dc_iostream_t *iostream); - -#ifdef __cplusplus -} -#endif /* __cplusplus */ -#endif /* UWATEC_MERIDIAN_H */ diff --git a/src/uwatec_smart.c b/src/uwatec_smart.c index 1f83434..47e8000 100644 --- a/src/uwatec_smart.c +++ b/src/uwatec_smart.c @@ -25,6 +25,7 @@ #include "uwatec_smart.h" #include "context-private.h" #include "device-private.h" +#include "checksum.h" #include "platform.h" #include "array.h" @@ -33,6 +34,8 @@ #define C_ARRAY_SIZE(array) (sizeof (array) / sizeof *(array)) #define DATASIZE 254 +#define PACKETSIZE_USBHID_RX 64 +#define PACKETSIZE_USBHID_TX 32 #define CMD_MODEL 0x10 #define CMD_SERIAL 0x14 @@ -43,14 +46,23 @@ #define CMD_SIZE 0xC6 #define OK 0x01 +#define ACK 0x11 +#define NAK 0x66 -typedef struct uwatec_smart_device_t { +typedef struct uwatec_smart_device_t uwatec_smart_device_t; + +typedef dc_status_t (*uwatec_smart_receive_t) (uwatec_smart_device_t *device, dc_event_progress_t *progress, unsigned char cmd, unsigned char data[], size_t size); +typedef dc_status_t (*uwatec_smart_send_t) (uwatec_smart_device_t *device, unsigned char cmd, const unsigned char data[], size_t size); + +struct uwatec_smart_device_t { dc_device_t base; dc_iostream_t *iostream; + uwatec_smart_send_t send; + uwatec_smart_receive_t receive; unsigned int timestamp; unsigned int devtime; dc_ticks_t systime; -} uwatec_smart_device_t; +}; static dc_status_t uwatec_smart_device_set_fingerprint (dc_device_t *device, const unsigned char data[], unsigned int size); static dc_status_t uwatec_smart_device_dump (dc_device_t *abstract, dc_buffer_t *buffer); @@ -72,7 +84,7 @@ static dc_status_t uwatec_smart_extract_dives (dc_device_t *device, const unsigned char data[], unsigned int size, dc_dive_callback_t callback, void *userdata); static dc_status_t -uwatec_smart_send (uwatec_smart_device_t *device, unsigned char cmd, const unsigned char data[], size_t size) +uwatec_smart_irda_send (uwatec_smart_device_t *device, unsigned char cmd, const unsigned char data[], size_t size) { dc_status_t rc = DC_STATUS_SUCCESS; dc_device_t *abstract = (dc_device_t *) device; @@ -100,7 +112,7 @@ uwatec_smart_send (uwatec_smart_device_t *device, unsigned char cmd, const unsig } static dc_status_t -uwatec_smart_receive (uwatec_smart_device_t *device, dc_event_progress_t *progress, unsigned char cmd, unsigned char data[], size_t size) +uwatec_smart_irda_receive (uwatec_smart_device_t *device, dc_event_progress_t *progress, unsigned char cmd, unsigned char data[], size_t size) { dc_status_t rc = DC_STATUS_SUCCESS; dc_device_t *abstract = (dc_device_t *) device; @@ -139,18 +151,221 @@ uwatec_smart_receive (uwatec_smart_device_t *device, dc_event_progress_t *progre } static dc_status_t -uwatec_smart_transfer (uwatec_smart_device_t *device, unsigned char cmd, const unsigned char command[], unsigned int csize, unsigned char answer[], unsigned int asize) +uwatec_smart_serial_send (uwatec_smart_device_t *device, unsigned char cmd, const unsigned char data[], size_t size) { dc_status_t status = DC_STATUS_SUCCESS; dc_device_t *abstract = (dc_device_t *) device; - status = uwatec_smart_send (device, cmd, command, csize); + if (size > DATASIZE) { + ERROR (abstract->context, "Command too large (" DC_PRINTF_SIZE ").", size); + return DC_STATUS_PROTOCOL; + } + + // Build the packet. + unsigned char packet[12 + DATASIZE + 1] = { + 0xFF, 0xFF, 0xFF, + 0xA6, 0x59, 0xBD, 0xC2, + size + 1, + 0x00, 0x00, 0x00, + cmd}; + if (size) { + memcpy (packet + 12, data, size); + } + packet[12 + size] = checksum_xor_uint8 (packet + 7, size + 5, 0x00); + + // Send the packet. + status = dc_iostream_write (device->iostream, packet, size + 13, NULL); if (status != DC_STATUS_SUCCESS) { ERROR (abstract->context, "Failed to send the command."); return status; } - status = uwatec_smart_receive (device, NULL, cmd, answer, asize); + // Read the echo and the ACK byte. + unsigned char echo[sizeof(packet) + 1]; + status = dc_iostream_read (device->iostream, echo, size + 13 + 1, NULL); + if (status != DC_STATUS_SUCCESS) { + ERROR (abstract->context, "Failed to receive the echo."); + return status; + } + + // Verify the echo. + if (memcmp (echo, packet, size + 13) != 0) { + WARNING (abstract->context, "Unexpected echo."); + return DC_STATUS_PROTOCOL; + } + + // Verify the ACK byte. + unsigned char ack = echo[size + 13]; + if (ack != ACK) { + WARNING (abstract->context, "Unexpected ACK byte (%02x).", ack); + return DC_STATUS_PROTOCOL; + } + + return DC_STATUS_SUCCESS; +} + +static dc_status_t +uwatec_smart_serial_receive (uwatec_smart_device_t *device, dc_event_progress_t *progress, unsigned char cmd, unsigned char data[], size_t size) +{ + dc_status_t status = DC_STATUS_SUCCESS; + dc_device_t *abstract = (dc_device_t *) device; + + size_t nbytes = 0; + while (nbytes < size) { + // Read the header. + unsigned char header[5]; + status = dc_iostream_read (device->iostream, header, sizeof (header), NULL); + if (status != DC_STATUS_SUCCESS) { + ERROR (abstract->context, "Failed to receive the header."); + return status; + } + + // Get the packet size. + unsigned int len = array_uint32_le (header); + if (len < 1 || nbytes + len - 1 > size) { + WARNING (abstract->context, "Unexpected header size (%u).", len); + return DC_STATUS_PROTOCOL; + } + + // Verify the command byte. + unsigned char rsp = header[4]; + if (rsp != cmd) { + ERROR (abstract->context, "Unexpected header command byte (%02x).", rsp); + return DC_STATUS_PROTOCOL; + } + + // Read the packet data. + status = dc_iostream_read (device->iostream, data + nbytes, len - 1, NULL); + if (status != DC_STATUS_SUCCESS) { + ERROR (abstract->context, "Failed to receive the packet."); + return status; + } + + // Read the checksum. + unsigned char csum = 0x00; + status = dc_iostream_read (device->iostream, &csum, sizeof (csum), NULL); + if (status != DC_STATUS_SUCCESS) { + ERROR (abstract->context, "Failed to receive the checksum."); + return status; + } + + // Verify the checksum. + unsigned char ccsum = 0x00; + ccsum = checksum_xor_uint8 (header, sizeof (header), ccsum); + ccsum = checksum_xor_uint8 (data + nbytes, len - 1, ccsum); + if (csum != ccsum) { + ERROR (abstract->context, "Unexpected answer checksum."); + return DC_STATUS_PROTOCOL; + } + + // Update and emit a progress event. + if (progress) { + progress->current += len - 1; + device_event_emit (abstract, DC_EVENT_PROGRESS, progress); + } + + nbytes += len - 1; + } + + return DC_STATUS_SUCCESS; +} + +static dc_status_t +uwatec_smart_usbhid_send (uwatec_smart_device_t *device, unsigned char cmd, const unsigned char data[], size_t size) +{ + dc_status_t rc = DC_STATUS_SUCCESS; + dc_device_t *abstract = (dc_device_t *) device; + unsigned char buf[PACKETSIZE_USBHID_TX + 1]; + + if (size + 3 > sizeof(buf)) { + ERROR (abstract->context, "Command too large (" DC_PRINTF_SIZE ").", size); + return DC_STATUS_INVALIDARGS; + } + + HEXDUMP (abstract->context, DC_LOGLEVEL_DEBUG, "cmd", data, size); + + buf[0] = 0; + buf[1] = size + 1; + buf[2] = cmd; + if (size) { + memcpy(buf + 3, data, size); + } + memset(buf + 3 + size, 0, sizeof(buf) - (size + 3)); + + if (dc_iostream_get_transport(device->iostream) == DC_TRANSPORT_BLE) { + rc = dc_iostream_write(device->iostream, buf + 1, size + 2, NULL); + } else { + rc = dc_iostream_write(device->iostream, buf, sizeof(buf), NULL); + } + if (rc != DC_STATUS_SUCCESS) { + ERROR (abstract->context, "Failed to send the command."); + return rc; + } + + return DC_STATUS_SUCCESS; +} + +static dc_status_t +uwatec_smart_usbhid_receive (uwatec_smart_device_t *device, dc_event_progress_t *progress, unsigned char cmd, unsigned char data[], size_t size) +{ + dc_status_t rc = DC_STATUS_SUCCESS; + dc_device_t *abstract = (dc_device_t *) device; + unsigned char buf[PACKETSIZE_USBHID_RX]; + + size_t nbytes = 0; + while (nbytes < size) { + size_t transferred = 0; + rc = dc_iostream_read (device->iostream, buf, sizeof(buf), &transferred); + if (rc != DC_STATUS_SUCCESS) { + ERROR (abstract->context, "Failed to receive the packet."); + return rc; + } + + if (transferred < 1) { + ERROR (abstract->context, "Invalid packet length (" DC_PRINTF_SIZE ").", transferred); + return DC_STATUS_PROTOCOL; + } + + unsigned int len = buf[0]; + if (len + 1 > transferred) { + ERROR (abstract->context, "Invalid payload length (%u).", len); + return DC_STATUS_PROTOCOL; + } + + HEXDUMP (abstract->context, DC_LOGLEVEL_DEBUG, "rcv", buf + 1, len); + + if (len > size) { + ERROR (abstract->context, "Insufficient buffer space available."); + return DC_STATUS_PROTOCOL; + } + + // Update and emit a progress event. + if (progress) { + progress->current += len; + device_event_emit (abstract, DC_EVENT_PROGRESS, progress); + } + + memcpy(data + nbytes, buf + 1, len); + nbytes += len; + } + + return DC_STATUS_SUCCESS; +} + + +static dc_status_t +uwatec_smart_transfer (uwatec_smart_device_t *device, unsigned char cmd, const unsigned char command[], unsigned int csize, unsigned char answer[], unsigned int asize) +{ + dc_status_t status = DC_STATUS_SUCCESS; + dc_device_t *abstract = (dc_device_t *) device; + + status = device->send (device, cmd, command, csize); + if (status != DC_STATUS_SUCCESS) { + ERROR (abstract->context, "Failed to send the command."); + return status; + } + + status = device->receive (device, NULL, cmd, answer, asize); if (status != DC_STATUS_SUCCESS) { ERROR (abstract->context, "Failed to receive the answer."); return status; @@ -215,6 +430,45 @@ uwatec_smart_device_open (dc_device_t **out, dc_context_t *context, dc_iostream_ device->systime = (dc_ticks_t) -1; device->devtime = 0; + // Set the serial communication protocol (57600 8N1). + status = dc_iostream_configure (device->iostream, 57600, 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 (3000ms). + status = dc_iostream_set_timeout (device->iostream, 3000); + if (status != DC_STATUS_SUCCESS) { + ERROR (context, "Failed to set the timeout."); + goto error_free; + } + + // Make sure everything is in a sane state. + dc_iostream_purge (device->iostream, DC_DIRECTION_ALL); + + // Select the correct send/receive function. + dc_transport_t transport = dc_iostream_get_transport(iostream); + switch (transport) { + case DC_TRANSPORT_IRDA: + device->send = uwatec_smart_irda_send; + device->receive = uwatec_smart_irda_receive; + break; + case DC_TRANSPORT_SERIAL: + device->send = uwatec_smart_serial_send; + device->receive = uwatec_smart_serial_receive; + break; + case DC_TRANSPORT_USBHID: + case DC_TRANSPORT_BLE: + device->send = uwatec_smart_usbhid_send; + device->receive = uwatec_smart_usbhid_receive; + break; + default: + ERROR (context, "Unsupported transport type (%u).", transport); + status = DC_STATUS_UNSUPPORTED; + goto error_free; + } + // Perform the handshaking. status = uwatec_smart_handshake (device); if (status != DC_STATUS_SUCCESS) { @@ -348,7 +602,7 @@ uwatec_smart_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) return DC_STATUS_PROTOCOL; } - rc = uwatec_smart_receive (device, &progress, CMD_DATA, data, length); + rc = device->receive (device, &progress, CMD_DATA, data, length); if (rc != DC_STATUS_SUCCESS) { ERROR (abstract->context, "Failed to receive the answer."); return rc;