diff --git a/examples/Makefile.am b/examples/Makefile.am index 0a62d80..2baee96 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -2,6 +2,7 @@ AM_CFLAGS = -I$(top_srcdir)/src LDADD = $(top_builddir)/src/libdivecomputer.la bin_PROGRAMS = \ + solution \ eon \ vyper \ vyper2 \ @@ -19,6 +20,8 @@ if IRDA bin_PROGRAMS += smart endif +solution_SOURCES = suunto_solution_test.c + eon_SOURCES = suunto_eon_test.c vyper_SOURCES = suunto_vyper_test.c diff --git a/examples/suunto_solution_test.c b/examples/suunto_solution_test.c new file mode 100644 index 0000000..781bfe3 --- /dev/null +++ b/examples/suunto_solution_test.c @@ -0,0 +1,103 @@ +#include // fopen, fwrite, fclose + +#include "suunto.h" +#include "utils.h" + +#define WARNING(expr) \ +{ \ + message ("%s:%d: %s\n", __FILE__, __LINE__, expr); \ +} + +device_status_t +test_dump_memory (const char* name, const char* filename) +{ + device_t *device = NULL; + unsigned char data[SUUNTO_SOLUTION_MEMORY_SIZE] = {0}; + + message ("suunto_solution_device_open\n"); + int rc = suunto_solution_device_open (&device, name); + if (rc != DEVICE_STATUS_SUCCESS) { + WARNING ("Error opening serial port."); + return rc; + } + + message ("device_dump\n"); + unsigned int nbytes = 0; + rc = device_dump (device, data, sizeof (data), &nbytes); + if (rc != DEVICE_STATUS_SUCCESS) { + WARNING ("Cannot read memory."); + device_close (device); + return rc; + } + + message ("Dumping data\n"); + FILE* fp = fopen (filename, "wb"); + if (fp != NULL) { + fwrite (data, sizeof (unsigned char), nbytes, fp); + fclose (fp); + } + + 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 ("SOLUTION.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, "SOLUTION.DMP"); + + message ("\nSUMMARY\n"); + message ("-------\n"); + message ("test_dump_memory: %s\n", errmsg (a)); + + message_set_logfile (NULL); + + return 0; +} diff --git a/src/Makefile.am b/src/Makefile.am index 33a4155..9d1eb73 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -9,6 +9,7 @@ libdivecomputer_HEADERS = \ parser.h \ units.h \ suunto.h \ + suunto_solution.h \ suunto_eon.h \ suunto_vyper.h \ suunto_vyper2.h \ @@ -42,6 +43,7 @@ libdivecomputer_la_SOURCES = \ parser.h parser-private.h parser.c \ suunto.h \ suunto_common.h suunto_common.c \ + suunto_solution.h suunto_solution.c \ suunto_eon.h suunto_eon.c \ suunto_vyper.h suunto_vyper.c suunto_vyper_parser.c suunto_spyder_parser.c \ suunto_vyper2.h suunto_vyper2.c \ diff --git a/src/device.h b/src/device.h index e5c81e6..8db03e9 100644 --- a/src/device.h +++ b/src/device.h @@ -28,6 +28,7 @@ extern "C" { typedef enum device_type_t { DEVICE_TYPE_NULL = 0, + DEVICE_TYPE_SUUNTO_SOLUTION, DEVICE_TYPE_SUUNTO_EON, DEVICE_TYPE_SUUNTO_VYPER, DEVICE_TYPE_SUUNTO_VYPER2, diff --git a/src/libdivecomputer.symbols b/src/libdivecomputer.symbols index 166413b..a808dde 100644 --- a/src/libdivecomputer.symbols +++ b/src/libdivecomputer.symbols @@ -55,6 +55,7 @@ reefnet_sensusultra_device_write_user reefnet_sensusultra_extract_dives suunto_d9_device_open suunto_d9_device_reset_maxdepth +suunto_solution_device_open suunto_eon_device_open suunto_eon_device_write_interval suunto_eon_device_write_name diff --git a/src/suunto.h b/src/suunto.h index 0191e25..8872ea0 100644 --- a/src/suunto.h +++ b/src/suunto.h @@ -22,6 +22,7 @@ #ifndef SUUNTO_H #define SUUNTO_H +#include "suunto_solution.h" #include "suunto_eon.h" #include "suunto_vyper.h" #include "suunto_vyper2.h" diff --git a/src/suunto_solution.c b/src/suunto_solution.c new file mode 100644 index 0000000..54b54f8 --- /dev/null +++ b/src/suunto_solution.c @@ -0,0 +1,230 @@ +/* + * 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 + */ + +#include // malloc, free + +#include "device-private.h" +#include "suunto_solution.h" +#include "serial.h" +#include "utils.h" + +#define WARNING(expr) \ +{ \ + message ("%s:%d: %s\n", __FILE__, __LINE__, expr); \ +} + +#define EXITCODE(rc) \ +( \ + rc == -1 ? DEVICE_STATUS_IO : DEVICE_STATUS_TIMEOUT \ +) + +typedef struct suunto_solution_device_t suunto_solution_device_t; + +struct suunto_solution_device_t { + device_t base; + struct serial *port; +}; + +static device_status_t suunto_solution_device_dump (device_t *abstract, unsigned char data[], unsigned int size, unsigned int *result); +static device_status_t suunto_solution_device_close (device_t *abstract); + +static const device_backend_t suunto_solution_device_backend = { + DEVICE_TYPE_SUUNTO_SOLUTION, + NULL, /* handshake */ + NULL, /* version */ + NULL, /* read */ + NULL, /* write */ + suunto_solution_device_dump, /* dump */ + NULL, /* foreach */ + suunto_solution_device_close /* close */ +}; + +static int +device_is_suunto_solution (device_t *abstract) +{ + if (abstract == NULL) + return 0; + + return abstract->backend == &suunto_solution_device_backend; +} + + +device_status_t +suunto_solution_device_open (device_t **out, const char* name) +{ + if (out == NULL) + return DEVICE_STATUS_ERROR; + + // Allocate memory. + suunto_solution_device_t *device = (suunto_solution_device_t *) malloc (sizeof (suunto_solution_device_t)); + if (device == NULL) { + WARNING ("Failed to allocate memory."); + return DEVICE_STATUS_MEMORY; + } + + // Initialize the base class. + device_init (&device->base, &suunto_solution_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 (1200 8N2). + rc = serial_configure (device->port, 1200, 8, SERIAL_PARITY_NONE, 2, 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 (1000ms). + if (serial_set_timeout (device->port, 1000) == -1) { + WARNING ("Failed to set the timeout."); + serial_close (device->port); + free (device); + return DEVICE_STATUS_IO; + } + + // Clear the RTS line. + if (serial_set_rts (device->port, 0)) { + WARNING ("Failed to set the DTR/RTS line."); + serial_close (device->port); + free (device); + return DEVICE_STATUS_IO; + } + + *out = (device_t*) device; + + return DEVICE_STATUS_SUCCESS; +} + + +static device_status_t +suunto_solution_device_close (device_t *abstract) +{ + suunto_solution_device_t *device = (suunto_solution_device_t*) abstract; + + if (! device_is_suunto_solution (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 +suunto_solution_device_dump (device_t *abstract, unsigned char data[], unsigned int size, unsigned int *result) +{ + suunto_solution_device_t *device = (suunto_solution_device_t*) abstract; + + if (! device_is_suunto_solution (abstract)) + return DEVICE_STATUS_TYPE_MISMATCH; + + if (size < SUUNTO_SOLUTION_MEMORY_SIZE) + return DEVICE_STATUS_MEMORY; + + int n = 0; + unsigned char command[3] = {0}; + unsigned char answer[3] = {0}; + + // Assert DTR + serial_set_dtr (device->port, 1); + + // Send: 0xFF + command[0] = 0xFF; + serial_write (device->port, command, 1); + + // Receive: 0x3F + n = serial_read (device->port, answer, 1); + if (n != 1) return EXITCODE (n); + if (answer[0] != 0x3F) WARNING ("Unexpected answer byte."); + + // Send: 0x4D, 0x01, 0x01 + command[0] = 0x4D; + command[1] = 0x01; + command[2] = 0x01; + serial_write (device->port, command, 3); + + data[0] = 0x00; + for (unsigned int i = 1; i < SUUNTO_SOLUTION_MEMORY_SIZE; ++i) { + // Receive: 0x01, i, data[i] + n = serial_read (device->port, answer, 3); + if (n != 3) return EXITCODE (n); + if (answer[0] != 0x01 || answer[1] != i) WARNING ("Unexpected answer byte."); + + // Send: i + command[0] = i; + serial_write (device->port, command, 1); + + // Receive: data[i] + n = serial_read (device->port, data + i, 1); + if (n != 1) return EXITCODE (n); + if (data[i] != answer[2]) WARNING ("Unexpected answer byte."); + + // Send: 0x0D + command[0] = 0x0D; + serial_write (device->port, command, 1); + } + + // Receive: 0x02, 0x00, 0x80 + n = serial_read (device->port, answer, 3); + if (n != 3) return EXITCODE (n); + if (answer[0] != 0x02 || answer[1] != 0x00 || answer[2] != 0x80) WARNING ("Unexpected answer byte."); + + // Send: 0x80 + command[0] = 0x80; + serial_write (device->port, command, 1); + + // Receive: 0x80 + n = serial_read (device->port, answer, 1); + if (n != 1) return EXITCODE (n); + if (answer[0] != 0x80) WARNING ("Unexpected answer byte."); + + // Send: 0x20 + command[0] = 0x20; + serial_write (device->port, command, 1); + + // Receive: 0x3F + n = serial_read (device->port, answer, 1); + if (n != 1) return EXITCODE (n); + if (answer[0] != 0x3F) WARNING ("Unexpected answer byte."); + + if (result) + *result = SUUNTO_SOLUTION_MEMORY_SIZE; + + return DEVICE_STATUS_SUCCESS; +} diff --git a/src/suunto_solution.h b/src/suunto_solution.h new file mode 100644 index 0000000..87c6e16 --- /dev/null +++ b/src/suunto_solution.h @@ -0,0 +1,39 @@ +/* + * 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 SUUNTO_SOLUTION_H +#define SUUNTO_SOLUTION_H + +#include "device.h" + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +#define SUUNTO_SOLUTION_MEMORY_SIZE 256 + +device_status_t +suunto_solution_device_open (device_t **device, const char* name); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* SUUNTO_SOLUTION_H */