From 620775af25b97ab9df84f0a7487b044f62deee00 Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Mon, 12 Apr 2010 10:19:38 +0200 Subject: [PATCH] Add support for the Mares Icon HD. --- examples/Makefile.am | 3 + examples/mares_iconhd_test.c | 121 +++++++++++++++++ examples/universal.c | 4 + src/Makefile.am | 2 + src/device.h | 1 + src/libdivecomputer.symbols | 1 + src/mares.h | 1 + src/mares_iconhd.c | 247 +++++++++++++++++++++++++++++++++++ src/mares_iconhd.h | 39 ++++++ 9 files changed, 419 insertions(+) create mode 100644 examples/mares_iconhd_test.c create mode 100644 src/mares_iconhd.c create mode 100644 src/mares_iconhd.h diff --git a/examples/Makefile.am b/examples/Makefile.am index 85ce161..278326c 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -18,6 +18,7 @@ bin_PROGRAMS = \ vtpro \ nemo \ puck \ + iconhd \ ostc \ edy @@ -57,6 +58,8 @@ nemo_SOURCES = mares_nemo_test.c puck_SOURCES = mares_puck_test.c +iconhd_SOURCES = mares_iconhd_test.c + ostc_SOURCES = hw_ostc_test.c edy_SOURCES = cressi_edy_test.c diff --git a/examples/mares_iconhd_test.c b/examples/mares_iconhd_test.c new file mode 100644 index 0000000..6fd5d13 --- /dev/null +++ b/examples/mares_iconhd_test.c @@ -0,0 +1,121 @@ +/* + * libdivecomputer + * + * Copyright (C) 2010 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 // fopen, fwrite, fclose + +#include "mares_iconhd.h" +#include "utils.h" + +device_status_t +test_dump_memory (const char* name, const char* filename) +{ + device_t *device = NULL; + + message ("mares_iconhd_device_open\n"); + device_status_t rc = mares_iconhd_device_open (&device, name); + if (rc != DEVICE_STATUS_SUCCESS) { + WARNING ("Error opening serial port."); + return rc; + } + + dc_buffer_t *buffer = dc_buffer_new (0); + + message ("device_dump\n"); + rc = device_dump (device, buffer); + if (rc != DEVICE_STATUS_SUCCESS) { + WARNING ("Cannot read memory."); + dc_buffer_free (buffer); + device_close (device); + return rc; + } + + message ("Dumping data\n"); + FILE* fp = fopen (filename, "wb"); + if (fp != NULL) { + fwrite (dc_buffer_get_data (buffer), sizeof (unsigned char), dc_buffer_get_size (buffer), fp); + fclose (fp); + } + + dc_buffer_free (buffer); + + message ("device_close\n"); + rc = device_close (device); + if (rc != DEVICE_STATUS_SUCCESS) { + WARNING ("Cannot close device."); + return rc; + } + + return DEVICE_STATUS_SUCCESS; +} + + +const char* +errmsg (device_status_t rc) +{ + switch (rc) { + case DEVICE_STATUS_SUCCESS: + return "Success"; + case DEVICE_STATUS_UNSUPPORTED: + return "Unsupported operation"; + case DEVICE_STATUS_TYPE_MISMATCH: + return "Device type mismatch"; + case DEVICE_STATUS_ERROR: + return "Generic error"; + case DEVICE_STATUS_IO: + return "Input/output error"; + case DEVICE_STATUS_MEMORY: + return "Memory error"; + case DEVICE_STATUS_PROTOCOL: + return "Protocol error"; + case DEVICE_STATUS_TIMEOUT: + return "Timeout"; + default: + return "Unknown error"; + } +} + + +int main(int argc, char *argv[]) +{ + message_set_logfile ("ICONHD.LOG"); + +#ifdef _WIN32 + const char* name = "COM1"; +#else + const char* name = "/dev/ttyS0"; +#endif + + if (argc > 1) { + name = argv[1]; + } + + message ("DEVICE=%s\n", name); + + device_status_t a = test_dump_memory (name, "ICONHD.DMP"); + + message ("\nSUMMARY\n"); + message ("-------\n"); + message ("test_dump_memory: %s\n", errmsg (a)); + + message_set_logfile (NULL); + + return 0; +} diff --git a/examples/universal.c b/examples/universal.c index e0fb470..82cd160 100644 --- a/examples/universal.c +++ b/examples/universal.c @@ -86,6 +86,7 @@ static const backend_table_t g_backends[] = { {"atom2", DEVICE_TYPE_OCEANIC_ATOM2}, {"nemo", DEVICE_TYPE_MARES_NEMO}, {"puck", DEVICE_TYPE_MARES_PUCK}, + {"iconhd", DEVICE_TYPE_MARES_ICONHD}, {"ostc", DEVICE_TYPE_HW_OSTC}, {"edy", DEVICE_TYPE_CRESSI_EDY} }; @@ -587,6 +588,9 @@ dowork (device_type_t backend, const char *devname, const char *rawfile, const c case DEVICE_TYPE_MARES_PUCK: rc = mares_puck_device_open (&device, devname); break; + case DEVICE_TYPE_MARES_ICONHD: + rc = mares_iconhd_device_open (&device, devname); + break; case DEVICE_TYPE_HW_OSTC: rc = hw_ostc_device_open (&device, devname); break; diff --git a/src/Makefile.am b/src/Makefile.am index 4f85569..86e7e33 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -32,6 +32,7 @@ libdivecomputer_HEADERS = \ mares.h \ mares_nemo.h \ mares_puck.h \ + mares_iconhd.h \ hw.h \ hw_ostc.h \ cressi.h \ @@ -78,6 +79,7 @@ libdivecomputer_la_SOURCES = \ mares_common.h mares_common.c \ mares_nemo.h mares_nemo.c mares_nemo_parser.c \ mares_puck.h mares_puck.c \ + mares_iconhd.h mares_iconhd.c \ hw.h \ hw_ostc.h hw_ostc.c hw_ostc_parser.c \ cressi.h \ diff --git a/src/device.h b/src/device.h index f75bce1..bbf6bbd 100644 --- a/src/device.h +++ b/src/device.h @@ -47,6 +47,7 @@ typedef enum device_type_t { DEVICE_TYPE_OCEANIC_VTPRO, DEVICE_TYPE_MARES_NEMO, DEVICE_TYPE_MARES_PUCK, + DEVICE_TYPE_MARES_ICONHD, DEVICE_TYPE_HW_OSTC, DEVICE_TYPE_CRESSI_EDY } device_type_t; diff --git a/src/libdivecomputer.symbols b/src/libdivecomputer.symbols index 42c9b99..63c05bd 100644 --- a/src/libdivecomputer.symbols +++ b/src/libdivecomputer.symbols @@ -60,6 +60,7 @@ mares_nemo_device_open mares_nemo_extract_dives mares_puck_device_open mares_puck_extract_dives +mares_iconhd_device_open oceanic_atom2_device_open oceanic_atom2_device_keepalive oceanic_veo250_device_open diff --git a/src/mares.h b/src/mares.h index a19bd84..0dcaa2e 100644 --- a/src/mares.h +++ b/src/mares.h @@ -24,5 +24,6 @@ #include "mares_nemo.h" #include "mares_puck.h" +#include "mares_iconhd.h" #endif /* MARES_H */ diff --git a/src/mares_iconhd.c b/src/mares_iconhd.c new file mode 100644 index 0000000..1aabdd2 --- /dev/null +++ b/src/mares_iconhd.c @@ -0,0 +1,247 @@ +/* + * libdivecomputer + * + * Copyright (C) 2010 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 "device-private.h" +#include "mares_iconhd.h" +#include "serial.h" +#include "utils.h" + +#define EXITCODE(rc) \ +( \ + rc == -1 ? DEVICE_STATUS_IO : DEVICE_STATUS_TIMEOUT \ +) + +typedef struct mares_iconhd_device_t { + device_t base; + struct serial *port; +} mares_iconhd_device_t; + +static device_status_t mares_iconhd_device_dump (device_t *abstract, dc_buffer_t *buffer); +static device_status_t mares_iconhd_device_close (device_t *abstract); + +static const device_backend_t mares_iconhd_device_backend = { + DEVICE_TYPE_MARES_ICONHD, + NULL, /* set_fingerprint */ + NULL, /* version */ + NULL, /* read */ + NULL, /* write */ + mares_iconhd_device_dump, /* dump */ + NULL, /* foreach */ + mares_iconhd_device_close /* close */ +}; + +static int +device_is_mares_iconhd (device_t *abstract) +{ + if (abstract == NULL) + return 0; + + return abstract->backend == &mares_iconhd_device_backend; +} + + +device_status_t +mares_iconhd_device_open (device_t **out, const char* name) +{ + if (out == NULL) + return DEVICE_STATUS_ERROR; + + // Allocate memory. + mares_iconhd_device_t *device = (mares_iconhd_device_t *) malloc (sizeof (mares_iconhd_device_t)); + if (device == NULL) { + WARNING ("Failed to allocate memory."); + return DEVICE_STATUS_MEMORY; + } + + // Initialize the base class. + device_init (&device->base, &mares_iconhd_device_backend); + + // Set the default values. + device->port = NULL; + + // Open the device. + int rc = serial_open (&device->port, name); + if (rc == -1) { + WARNING ("Failed to open the serial port."); + free (device); + return DEVICE_STATUS_IO; + } + + // Set the serial communication protocol (256000 8N1). + rc = serial_configure (device->port, 256000, 8, SERIAL_PARITY_NONE, 1, SERIAL_FLOWCONTROL_NONE); + if (rc == -1) { + WARNING ("Failed to set the terminal attributes."); + serial_close (device->port); + free (device); + return DEVICE_STATUS_IO; + } + + // Set the timeout for receiving data (1000 ms). + if (serial_set_timeout (device->port, 1000) == -1) { + WARNING ("Failed to set the timeout."); + serial_close (device->port); + free (device); + return DEVICE_STATUS_IO; + } + + // Set the DTR/RTS lines. + if (serial_set_dtr (device->port, 0) == -1 || + serial_set_rts (device->port, 0) == -1) + { + WARNING ("Failed to set the DTR/RTS line."); + serial_close (device->port); + free (device); + return DEVICE_STATUS_IO; + } + + // Make sure everything is in a sane state. + serial_flush (device->port, SERIAL_QUEUE_BOTH); + + *out = (device_t *) device; + + return DEVICE_STATUS_SUCCESS; +} + + +static device_status_t +mares_iconhd_device_close (device_t *abstract) +{ + mares_iconhd_device_t *device = (mares_iconhd_device_t*) abstract; + + if (! device_is_mares_iconhd (abstract)) + return DEVICE_STATUS_TYPE_MISMATCH; + + // Close the device. + if (serial_close (device->port) == -1) { + free (device); + return DEVICE_STATUS_IO; + } + + // Free memory. + free (device); + + return DEVICE_STATUS_SUCCESS; +} + + +static device_status_t +mares_iconhd_init (mares_iconhd_device_t *device) +{ + // Send the command to the dive computer. + unsigned char command[2] = {0xE7, 0x42}; + int n = serial_write (device->port, command, sizeof (command)); + if (n != sizeof (command)) { + WARNING ("Failed to send the command."); + return EXITCODE (n); + } + + // Receive the answer of the dive computer. + unsigned char answer[1] = {0}; + n = serial_read (device->port, answer, sizeof (answer)); + if (n != sizeof (answer)) { + WARNING ("Failed to receive the answer."); + return EXITCODE (n); + } + + return DEVICE_STATUS_SUCCESS; +} + + +static device_status_t +mares_iconhd_device_dump (device_t *abstract, dc_buffer_t *buffer) +{ + mares_iconhd_device_t *device = (mares_iconhd_device_t *) abstract; + + // Erase the current contents of the buffer and + // pre-allocate the required amount of memory. + if (!dc_buffer_clear (buffer) || !dc_buffer_resize (buffer, MARES_ICONHD_MEMORY_SIZE)) { + WARNING ("Insufficient buffer space available."); + return DEVICE_STATUS_MEMORY; + } + + // Enable progress notifications. + device_progress_t progress = DEVICE_PROGRESS_INITIALIZER; + progress.maximum = MARES_ICONHD_MEMORY_SIZE; + device_event_emit (abstract, DEVICE_EVENT_PROGRESS, &progress); + + // Send the init command. + device_status_t rc = mares_iconhd_init (device); + if (rc != DEVICE_STATUS_SUCCESS) + return rc; + + // Send the command to the dive computer. + unsigned char command[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00}; + int n = serial_write (device->port, command, sizeof (command)); + if (n != sizeof (command)) { + WARNING ("Failed to send the command."); + return EXITCODE (n); + } + + unsigned char *data = dc_buffer_get_data (buffer); + + unsigned int nbytes = 0; + while (nbytes < MARES_ICONHD_MEMORY_SIZE) { + // Set the minimum packet size. + unsigned int len = 1024; + + // Increase the packet size if more data is immediately available. + int available = serial_get_received (device->port); + if (available > len) + len = available; + + // Limit the packet size to the total size. + if (nbytes + len > MARES_ICONHD_MEMORY_SIZE) + len = MARES_ICONHD_MEMORY_SIZE - nbytes; + + // Read the packet. + n = serial_read (device->port, data + nbytes, len); + if (n != len) { + WARNING ("Failed to receive the answer."); + return EXITCODE (n); + } + + // Update and emit a progress event. + progress.current += len; + device_event_emit (abstract, DEVICE_EVENT_PROGRESS, &progress); + + nbytes += len; + } + + // Receive the last byte. + unsigned char answer[1] = {0}; + n = serial_read (device->port, answer, sizeof (answer)); + if (n != sizeof (answer)) { + WARNING ("Failed to receive the answer."); + return EXITCODE (n); + } + + // Verify the last byte. + if (answer[0] != 0xEA) { + WARNING ("Unexpected answer byte."); + return DEVICE_STATUS_PROTOCOL; + } + + return DEVICE_STATUS_SUCCESS; +} diff --git a/src/mares_iconhd.h b/src/mares_iconhd.h new file mode 100644 index 0000000..08c8a6d --- /dev/null +++ b/src/mares_iconhd.h @@ -0,0 +1,39 @@ +/* + * libdivecomputer + * + * Copyright (C) 2010 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 MARES_ICONHD_H +#define MARES_ICONHD_H + +#include "device.h" + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +#define MARES_ICONHD_MEMORY_SIZE 0x100000 + +device_status_t +mares_iconhd_device_open (device_t **device, const char* name); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* MARES_ICONHD_H */