From 02e0f3754697d81627d6a9602328832f2da14777 Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Tue, 23 Dec 2008 17:37:45 +0000 Subject: [PATCH] Added the initial implementation for the Mares Nemo. --- examples/Makefile.am | 5 +- examples/mares_nemo_test.c | 123 ++++++++++++++++++++ src/Makefile.am | 6 +- src/device.h | 3 +- src/libdivecomputer.symbols | 1 + src/mares.h | 27 +++++ src/mares_nemo.c | 222 ++++++++++++++++++++++++++++++++++++ src/mares_nemo.h | 40 +++++++ 8 files changed, 424 insertions(+), 3 deletions(-) create mode 100644 examples/mares_nemo_test.c create mode 100644 src/mares.h create mode 100644 src/mares_nemo.c create mode 100644 src/mares_nemo.h diff --git a/examples/Makefile.am b/examples/Makefile.am index d5359f9..0a62d80 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -12,7 +12,8 @@ bin_PROGRAMS = \ memomouse \ atom2 \ veo250 \ - vtpro + vtpro \ + nemo if IRDA bin_PROGRAMS += smart @@ -40,6 +41,8 @@ veo250_SOURCES = oceanic_veo250_test.c vtpro_SOURCES = oceanic_vtpro_test.c +nemo_SOURCES = mares_nemo_test.c + if IRDA smart_SOURCES = uwatec_smart_test.c endif diff --git a/examples/mares_nemo_test.c b/examples/mares_nemo_test.c new file mode 100644 index 0000000..cdb3649 --- /dev/null +++ b/examples/mares_nemo_test.c @@ -0,0 +1,123 @@ +/* + * 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 "mares_nemo.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[MARES_NEMO_MEMORY_SIZE] = {0}; + + message ("mares_nemo_device_open\n"); + device_status_t rc = mares_nemo_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 ("NEMO.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, "NEMO.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 8543594..fc1ad37 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -23,7 +23,9 @@ libdivecomputer_HEADERS = \ oceanic.h \ oceanic_atom2.h \ oceanic_veo250.h \ - oceanic_vtpro.h + oceanic_vtpro.h \ + mares.h \ + mares_nemo.h # # Source files. @@ -54,6 +56,8 @@ libdivecomputer_la_SOURCES = \ oceanic_atom2.h oceanic_atom2.c \ oceanic_veo250.h oceanic_veo250.c \ oceanic_vtpro.h oceanic_vtpro.c \ + mares.h \ + mares_nemo.h mares_nemo.c \ ringbuffer.h ringbuffer.c \ checksum.h checksum.c \ array.h array.c \ diff --git a/src/device.h b/src/device.h index 7c78a1e..e5c81e6 100644 --- a/src/device.h +++ b/src/device.h @@ -39,7 +39,8 @@ typedef enum device_type_t { DEVICE_TYPE_UWATEC_SMART, DEVICE_TYPE_OCEANIC_ATOM2, DEVICE_TYPE_OCEANIC_VEO250, - DEVICE_TYPE_OCEANIC_VTPRO + DEVICE_TYPE_OCEANIC_VTPRO, + DEVICE_TYPE_MARES_NEMO } device_type_t; typedef enum device_status_t { diff --git a/src/libdivecomputer.symbols b/src/libdivecomputer.symbols index 9ff0372..bec55f9 100644 --- a/src/libdivecomputer.symbols +++ b/src/libdivecomputer.symbols @@ -27,6 +27,7 @@ message message_set_logfile #ifdef SERIAL +mares_nemo_device_open oceanic_atom2_device_handshake oceanic_atom2_device_open oceanic_atom2_device_quit diff --git a/src/mares.h b/src/mares.h new file mode 100644 index 0000000..a3fd4f5 --- /dev/null +++ b/src/mares.h @@ -0,0 +1,27 @@ +/* + * 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 MARES_H +#define MARES_H + +#include "mares_nemo.h" + +#endif /* MARES_H */ diff --git a/src/mares_nemo.c b/src/mares_nemo.c new file mode 100644 index 0000000..664d5ab --- /dev/null +++ b/src/mares_nemo.c @@ -0,0 +1,222 @@ +/* + * 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 // memcpy, memcmp +#include // malloc, free +#include // assert + +#include "device-private.h" +#include "mares_nemo.h" +#include "serial.h" +#include "utils.h" +#include "checksum.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 mares_nemo_device_t mares_nemo_device_t; + +struct mares_nemo_device_t { + device_t base; + struct serial *port; +}; + +static device_status_t mares_nemo_device_dump (device_t *abstract, unsigned char data[], unsigned int size, unsigned int *result); +static device_status_t mares_nemo_device_close (device_t *abstract); + +static const device_backend_t mares_nemo_device_backend = { + DEVICE_TYPE_MARES_NEMO, + NULL, /* handshake */ + NULL, /* version */ + NULL, /* read */ + NULL, /* write */ + mares_nemo_device_dump, /* dump */ + NULL, /* foreach */ + mares_nemo_device_close /* close */ +}; + +static int +device_is_mares_nemo (device_t *abstract) +{ + if (abstract == NULL) + return 0; + + return abstract->backend == &mares_nemo_device_backend; +} + + +device_status_t +mares_nemo_device_open (device_t **out, const char* name) +{ + if (out == NULL) + return DEVICE_STATUS_ERROR; + + // Allocate memory. + mares_nemo_device_t *device = (mares_nemo_device_t *) malloc (sizeof (mares_nemo_device_t)); + if (device == NULL) { + WARNING ("Failed to allocate memory."); + return DEVICE_STATUS_MEMORY; + } + + // Initialize the base class. + device_init (&device->base, &mares_nemo_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 (9600 8N1). + rc = serial_configure (device->port, 9600, 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, -1) == -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, 1) == -1 || + serial_set_rts (device->port, 1) == -1) { + 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 +mares_nemo_device_close (device_t *abstract) +{ + mares_nemo_device_t *device = (mares_nemo_device_t*) abstract; + + if (! device_is_mares_nemo (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_nemo_device_dump (device_t *abstract, unsigned char data[], unsigned int size, unsigned int *result) +{ + mares_nemo_device_t *device = (mares_nemo_device_t *) abstract; + + if (! device_is_mares_nemo (abstract)) + return DEVICE_STATUS_TYPE_MISMATCH; + + if (size < MARES_NEMO_MEMORY_SIZE) + return DEVICE_STATUS_ERROR; + + // Receive the header of the package. + unsigned char header = 0x00; + for (unsigned int i = 0; i < 20;) { + int n = serial_read (device->port, &header, 1); + if (n != 1) { + WARNING ("Failed to receive the header."); + return EXITCODE (n); + } + if (header == 0xEE) { + i++; // Continue. + } else { + i = 0; // Reset. + } + } + + unsigned int nbytes = 0; + while (nbytes < MARES_NEMO_MEMORY_SIZE) { + // Read the packet. + unsigned char packet[(MARES_NEMO_PACKET_SIZE + 1) * 2] = {0}; + int n = serial_read (device->port, packet, sizeof (packet)); + if (n != sizeof (packet)) { + WARNING ("Failed to receive the answer."); + return EXITCODE (n); + } + + // Verify the checksums of the packet. + unsigned char crc1 = packet[MARES_NEMO_PACKET_SIZE]; + unsigned char crc2 = packet[MARES_NEMO_PACKET_SIZE * 2 + 1]; + unsigned char ccrc1 = checksum_add_uint8 (packet, MARES_NEMO_PACKET_SIZE, 0x00); + unsigned char ccrc2 = checksum_add_uint8 (packet + MARES_NEMO_PACKET_SIZE + 1, MARES_NEMO_PACKET_SIZE, 0x00); + if (crc1 == ccrc1 && crc2 == ccrc2) { + // Both packets have a correct checksum. + if (memcmp (packet, packet + MARES_NEMO_PACKET_SIZE + 1, MARES_NEMO_PACKET_SIZE) != 0) { + WARNING ("Both packets are not equal."); + return DEVICE_STATUS_PROTOCOL; + } + memcpy (data + nbytes, packet, MARES_NEMO_PACKET_SIZE); + } else if (crc1 == ccrc1) { + // Only the first packet has a correct checksum. + WARNING ("Only the first packet has a correct checksum."); + memcpy (data + nbytes, packet, MARES_NEMO_PACKET_SIZE); + } else if (crc2 == ccrc2) { + // Only the second packet has a correct checksum. + WARNING ("Only the second packet has a correct checksum."); + memcpy (data + nbytes, packet + MARES_NEMO_PACKET_SIZE + 1, MARES_NEMO_PACKET_SIZE); + } else { + WARNING ("Unexpected answer CRC."); + return DEVICE_STATUS_PROTOCOL; + } + + nbytes += MARES_NEMO_PACKET_SIZE; + } + + if (result) + *result = MARES_NEMO_MEMORY_SIZE; + + return DEVICE_STATUS_SUCCESS; +} diff --git a/src/mares_nemo.h b/src/mares_nemo.h new file mode 100644 index 0000000..dce662a --- /dev/null +++ b/src/mares_nemo.h @@ -0,0 +1,40 @@ +/* + * 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 MARES_NEMO_H +#define MARES_NEMO_H + +#include "device.h" + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +#define MARES_NEMO_MEMORY_SIZE 0x4000 +#define MARES_NEMO_PACKET_SIZE 0x20 + +device_status_t +mares_nemo_device_open (device_t **device, const char* name); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* MARES_NEMO_H */