diff --git a/examples/Makefile.am b/examples/Makefile.am index 2baee96..7419b27 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -7,6 +7,7 @@ bin_PROGRAMS = \ vyper \ vyper2 \ d9 \ + sensus \ sensuspro \ sensusultra \ aladin \ @@ -30,6 +31,8 @@ vyper2_SOURCES = suunto_vyper2_test.c d9_SOURCES = suunto_d9_test.c +sensus_SOURCES = reefnet_sensus_test.c + sensuspro_SOURCES = reefnet_sensuspro_test.c sensusultra_SOURCES = reefnet_sensusultra_test.c diff --git a/examples/reefnet_sensus_test.c b/examples/reefnet_sensus_test.c new file mode 100644 index 0000000..3c2f886 --- /dev/null +++ b/examples/reefnet_sensus_test.c @@ -0,0 +1,138 @@ +/* + * 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 // fopen, fwrite, fclose +#include // time + +#include "reefnet_sensus.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[REEFNET_SENSUS_MEMORY_SIZE] = {0}; + unsigned char handshake[REEFNET_SENSUS_HANDSHAKE_SIZE] = {0}; + + message ("reefnet_sensus_device_open\n"); + device_status_t rc = reefnet_sensus_device_open (&device, name); + if (rc != DEVICE_STATUS_SUCCESS) { + WARNING ("Error opening serial port."); + return rc; + } + + message ("device_handshake\n"); + rc = device_handshake (device, handshake, sizeof (handshake)); + if (rc != DEVICE_STATUS_SUCCESS) { + WARNING ("Cannot read handshake."); + device_close (device); + return rc; + } + + time_t now = time (NULL); + char datetime[21] = {0}; + strftime (datetime, sizeof (datetime), "%Y-%m-%dT%H:%M:%SZ", gmtime (&now)); + message ("time=%lu (%s)\n", (unsigned long)now, datetime); + + 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 ("SENSUS.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, "SENSUS.DMP"); + + message ("SUMMARY\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 2cfa98d..b57bc76 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -15,6 +15,7 @@ libdivecomputer_HEADERS = \ suunto_vyper2.h \ suunto_d9.h \ reefnet.h \ + reefnet_sensus.h \ reefnet_sensuspro.h \ reefnet_sensusultra.h \ uwatec.h \ @@ -49,6 +50,7 @@ libdivecomputer_la_SOURCES = \ suunto_vyper2.h suunto_vyper2.c \ suunto_d9.h suunto_d9.c suunto_d9_parser.c \ reefnet.h \ + reefnet_sensus.h reefnet_sensus.c \ reefnet_sensuspro.h reefnet_sensuspro.c reefnet_sensuspro_parser.c \ reefnet_sensusultra.h reefnet_sensusultra.c reefnet_sensusultra_parser.c \ uwatec.h \ diff --git a/src/device.h b/src/device.h index 8db03e9..b39a0e1 100644 --- a/src/device.h +++ b/src/device.h @@ -33,6 +33,7 @@ typedef enum device_type_t { DEVICE_TYPE_SUUNTO_VYPER, DEVICE_TYPE_SUUNTO_VYPER2, DEVICE_TYPE_SUUNTO_D9, + DEVICE_TYPE_REEFNET_SENSUS, DEVICE_TYPE_REEFNET_SENSUSPRO, DEVICE_TYPE_REEFNET_SENSUSULTRA, DEVICE_TYPE_UWATEC_ALADIN, diff --git a/src/libdivecomputer.symbols b/src/libdivecomputer.symbols index 76828a4..c3debde 100644 --- a/src/libdivecomputer.symbols +++ b/src/libdivecomputer.symbols @@ -38,6 +38,8 @@ oceanic_veo250_device_keepalive oceanic_vtpro_device_open oceanic_vtpro_device_keepalive oceanic_vtpro_device_calibrate +reefnet_sensus_device_open +reefnet_sensus_device_cancel reefnet_sensuspro_device_open reefnet_sensuspro_device_set_timestamp reefnet_sensuspro_device_write_interval diff --git a/src/reefnet.h b/src/reefnet.h index 87bdfc9..e574c60 100644 --- a/src/reefnet.h +++ b/src/reefnet.h @@ -22,6 +22,7 @@ #ifndef REEFNET_H #define REEFNET_H +#include "reefnet_sensus.h" #include "reefnet_sensuspro.h" #include "reefnet_sensusultra.h" diff --git a/src/reefnet_sensus.c b/src/reefnet_sensus.c new file mode 100644 index 0000000..6cf9353 --- /dev/null +++ b/src/reefnet_sensus.c @@ -0,0 +1,297 @@ +/* + * 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 // memcmp, memcpy +#include // malloc, free + +#include "device-private.h" +#include "reefnet_sensus.h" +#include "serial.h" +#include "checksum.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 reefnet_sensus_device_t reefnet_sensus_device_t; + +struct reefnet_sensus_device_t { + device_t base; + struct serial *port; +}; + +static device_status_t reefnet_sensus_device_handshake (device_t *abstract, unsigned char *data, unsigned int size); +static device_status_t reefnet_sensus_device_dump (device_t *abstract, unsigned char *data, unsigned int size, unsigned int *result); +static device_status_t reefnet_sensus_device_close (device_t *abstract); + +static const device_backend_t reefnet_sensus_device_backend = { + DEVICE_TYPE_REEFNET_SENSUS, + reefnet_sensus_device_handshake, /* handshake */ + NULL, /* version */ + NULL, /* read */ + NULL, /* write */ + reefnet_sensus_device_dump, /* dump */ + NULL, /* foreach */ + reefnet_sensus_device_close /* close */ +}; + +static int +device_is_reefnet_sensus (device_t *abstract) +{ + if (abstract == NULL) + return 0; + + return abstract->backend == &reefnet_sensus_device_backend; +} + + +device_status_t +reefnet_sensus_device_open (device_t **out, const char* name) +{ + if (out == NULL) + return DEVICE_STATUS_ERROR; + + // Allocate memory. + reefnet_sensus_device_t *device = (reefnet_sensus_device_t *) malloc (sizeof (reefnet_sensus_device_t)); + if (device == NULL) { + WARNING ("Failed to allocate memory."); + return DEVICE_STATUS_MEMORY; + } + + // Initialize the base class. + device_init (&device->base, &reefnet_sensus_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 (19200 8N1). + rc = serial_configure (device->port, 19200, 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 (3000 ms). + if (serial_set_timeout (device->port, 3000) == -1) { + WARNING ("Failed to set the timeout."); + 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 +reefnet_sensus_device_close (device_t *abstract) +{ + reefnet_sensus_device_t *device = (reefnet_sensus_device_t*) abstract; + + if (! device_is_reefnet_sensus (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 +reefnet_sensus_device_handshake (device_t *abstract, unsigned char *data, unsigned int size) +{ + reefnet_sensus_device_t *device = (reefnet_sensus_device_t*) abstract; + + if (! device_is_reefnet_sensus (abstract)) + return DEVICE_STATUS_TYPE_MISMATCH; + + // Send the command to the device. + unsigned char command = 0x0A; + int n = serial_write (device->port, &command, 1); + if (n != 1) { + WARNING ("Failed to send the handshake command."); + return EXITCODE (n); + } + + // Receive the answer from the device. + unsigned char handshake[REEFNET_SENSUS_HANDSHAKE_SIZE + 2] = {0}; + n = serial_read (device->port, handshake, sizeof (handshake)); + if (n != sizeof (handshake)) { + WARNING ("Failed to receive the handshake."); + return EXITCODE (n); + } + + // Verify the header of the packet. + if (handshake[0] != 'O' || handshake[1] != 'K') { + WARNING ("Unexpected answer header."); + return DEVICE_STATUS_PROTOCOL; + } + +#ifndef NDEBUG + message ( + "Response Header: %c%c\n" + "Product Code: %d\n" + "Product Version: %d\n" + "Battery: %d\n" + "Interval: %d\n" + "Device ID: %d\n" + "Current Time: %d\n", + handshake[0], handshake[1], + handshake[2], handshake[3], + handshake[4], handshake[5], + handshake[6] + (handshake[7] << 8), + handshake[8] + (handshake[9] << 8) + (handshake[10] << 16) + (handshake[11] << 24)); +#endif + + if (size >= REEFNET_SENSUS_HANDSHAKE_SIZE) { + memcpy (data, handshake + 2, REEFNET_SENSUS_HANDSHAKE_SIZE); + } else { + WARNING ("Insufficient buffer space available."); + return DEVICE_STATUS_MEMORY; + } + + // Wait at least 10 ms to ensures the data line is + // clear before transmission from the host begins. + + serial_sleep (10); + + return DEVICE_STATUS_SUCCESS; +} + + +device_status_t +reefnet_sensus_device_cancel (device_t *abstract) +{ + reefnet_sensus_device_t *device = (reefnet_sensus_device_t*) abstract; + + if (! device_is_reefnet_sensus (abstract)) + return DEVICE_STATUS_TYPE_MISMATCH; + + // Send the command to the device. + unsigned char command = 0x00; + int n = serial_write (device->port, &command, 1); + if (n != 1) { + WARNING ("Failed to send the cancel command."); + return EXITCODE (n); + } + + return DEVICE_STATUS_SUCCESS; +} + + +static device_status_t +reefnet_sensus_device_dump (device_t *abstract, unsigned char *data, unsigned int size, unsigned int *result) +{ + reefnet_sensus_device_t *device = (reefnet_sensus_device_t*) abstract; + + if (! device_is_reefnet_sensus (abstract)) + return DEVICE_STATUS_TYPE_MISMATCH; + + // Enable progress notifications. + device_progress_state_t progress; + progress_init (&progress, abstract, 4 + REEFNET_SENSUS_MEMORY_SIZE + 2 + 3); + + // Send the command to the device. + unsigned char command = 0x40; + int n = serial_write (device->port, &command, 1); + if (n != 1) { + WARNING ("Failed to send the command."); + return EXITCODE (n); + } + + // Receive the answer from the device. + unsigned int nbytes = 0; + unsigned char answer[4 + REEFNET_SENSUS_MEMORY_SIZE + 2 + 3] = {0}; + while (nbytes < sizeof (answer)) { + unsigned int len = sizeof (answer) - nbytes; + if (len > 128) + len = 128; + + n = serial_read (device->port, answer + nbytes, len); + if (n != len) { + WARNING ("Failed to receive the answer."); + return EXITCODE (n); + } + + progress_event (&progress, DEVICE_EVENT_PROGRESS, len); + + nbytes += len; + } + + // Verify the headers of the package. + if (memcmp (answer, "DATA", 4) != 0 || + memcmp (answer + sizeof (answer) - 3, "END", 3) != 0) { + WARNING ("Unexpected answer start or end byte(s)."); + return DEVICE_STATUS_PROTOCOL; + } + + // Verify the checksum of the package. + unsigned short crc = + answer[4 + REEFNET_SENSUS_MEMORY_SIZE + 0] + + (answer[4 + REEFNET_SENSUS_MEMORY_SIZE + 1] << 8); + unsigned short ccrc = checksum_add_uint16 (answer + 4, REEFNET_SENSUS_MEMORY_SIZE, 0x00); + if (crc != ccrc) { + WARNING ("Unexpected answer CRC."); + return DEVICE_STATUS_PROTOCOL; + } + + if (size >= REEFNET_SENSUS_MEMORY_SIZE) { + memcpy (data, answer + 4, REEFNET_SENSUS_MEMORY_SIZE); + } else { + WARNING ("Insufficient buffer space available."); + return DEVICE_STATUS_MEMORY; + } + + if (result) + *result = REEFNET_SENSUS_MEMORY_SIZE; + + return DEVICE_STATUS_SUCCESS; +} diff --git a/src/reefnet_sensus.h b/src/reefnet_sensus.h new file mode 100644 index 0000000..0c21e62 --- /dev/null +++ b/src/reefnet_sensus.h @@ -0,0 +1,43 @@ +/* + * 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 REEFNET_SENSUS_H +#define REEFNET_SENSUS_H + +#include "device.h" + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +#define REEFNET_SENSUS_MEMORY_SIZE 32768 +#define REEFNET_SENSUS_HANDSHAKE_SIZE 10 + +device_status_t +reefnet_sensus_device_open (device_t **device, const char* name); + +device_status_t +reefnet_sensus_device_cancel (device_t *device); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* REEFNET_SENSUS_H */