diff --git a/suunto.h b/suunto.h index 2a51d4d..5b20cbd 100644 --- a/suunto.h +++ b/suunto.h @@ -10,6 +10,7 @@ typedef void (*dive_callback_t) (const unsigned char *data, unsigned int size, void *userdata); +#include "suunto_eon.h" #include "suunto_vyper.h" #include "suunto_vyper2.h" #include "suunto_d9.h" diff --git a/suunto_eon.c b/suunto_eon.c new file mode 100644 index 0000000..733a67d --- /dev/null +++ b/suunto_eon.c @@ -0,0 +1,186 @@ +#include // memcmp, memcpy +#include // malloc, free + +#include "suunto.h" +#include "serial.h" +#include "utils.h" + +#define WARNING(expr) \ +{ \ + message ("%s:%d: %s\n", __FILE__, __LINE__, expr); \ +} + +#define EXITCODE(rc) \ +( \ + rc == -1 ? SUUNTO_ERROR_IO : SUUNTO_ERROR_TIMEOUT \ +) + +struct eon { + struct serial *port; +}; + + +int +suunto_eon_open (eon **out, const char* name) +{ + if (out == NULL) + return SUUNTO_ERROR; + + // Allocate memory. + struct eon *device = malloc (sizeof (struct eon)); + if (device == NULL) { + WARNING ("Failed to allocate memory."); + return SUUNTO_ERROR_MEMORY; + } + + // 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 SUUNTO_ERROR_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 SUUNTO_ERROR_IO; + } + + // Set the timeout for receiving data (1000ms). + if (serial_set_timeout (device->port, -1) == -1) { + WARNING ("Failed to set the timeout."); + serial_close (device->port); + free (device); + return SUUNTO_ERROR_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 SUUNTO_ERROR_IO; + } + + *out = device; + + return SUUNTO_SUCCESS; +} + + +int +suunto_eon_close (eon *device) +{ + if (device == NULL) + return SUUNTO_SUCCESS; + + // Close the device. + if (serial_close (device->port) == -1) { + free (device); + return SUUNTO_ERROR_IO; + } + + // Free memory. + free (device); + + return SUUNTO_SUCCESS; +} + + +unsigned char +suunto_eon_checksum (unsigned char data[], unsigned int size) +{ + unsigned char crc = 0x00; + for (unsigned int i = 0; i < size; ++i) + crc += data[i]; + + return crc; +} + + +int +suunto_eon_read (eon *device, unsigned char data[], unsigned int size) +{ + if (device == NULL) + return SUUNTO_ERROR; + + // Send the command. + unsigned char command[1] = {'P'}; + int rc = serial_write (device->port, command, sizeof (command)); + if (rc != sizeof (command)) { + WARNING ("Failed to send the command."); + return EXITCODE (rc); + } + + // Receive the answer. + unsigned char answer[SUUNTO_EON_MEMORY_SIZE + 1] = {0}; + rc = serial_read (device->port, answer, sizeof (answer)); + if (rc != sizeof (answer)) { + WARNING ("Failed to receive the answer."); + return EXITCODE (rc); + } + + // Verify the checksum of the package. + unsigned char crc = answer[sizeof (answer) - 1]; + unsigned char ccrc = suunto_eon_checksum (answer, sizeof (answer) - 1); + if (crc != ccrc) { + WARNING ("Unexpected answer CRC."); + return SUUNTO_ERROR_PROTOCOL; + } + + if (size >= SUUNTO_EON_MEMORY_SIZE) { + memcpy (data, answer, SUUNTO_EON_MEMORY_SIZE); + } else { + WARNING ("Insufficient buffer space available."); + return SUUNTO_ERROR_MEMORY; + } + + return SUUNTO_SUCCESS; +} + + +int +suunto_eon_write_name (eon *device, unsigned char data[], unsigned int size) +{ + if (device == NULL) + return SUUNTO_ERROR; + + if (size > 20) + return SUUNTO_ERROR; + + // Send the command. + unsigned char command[21] = {'N'}; + memcpy (command + 1, data, size); + int rc = serial_write (device->port, command, sizeof (command)); + if (rc != sizeof (command)) { + WARNING ("Failed to send the command."); + return EXITCODE (rc); + } + + return SUUNTO_SUCCESS; +} + + +int +suunto_eon_write_interval (eon *device, unsigned char interval) +{ + if (device == NULL) + return SUUNTO_ERROR; + + // Send the command. + unsigned char command[2] = {'T', interval}; + int rc = serial_write (device->port, command, sizeof (command)); + if (rc != sizeof (command)) { + WARNING ("Failed to send the command."); + return EXITCODE (rc); + } + + return SUUNTO_SUCCESS; +} diff --git a/suunto_eon.h b/suunto_eon.h new file mode 100644 index 0000000..9457a01 --- /dev/null +++ b/suunto_eon.h @@ -0,0 +1,25 @@ +#ifndef SUUNTO_EON_H +#define SUUNTO_EON_H + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +typedef struct eon eon; + +#define SUUNTO_EON_MEMORY_SIZE 0x900 + +int suunto_eon_open (eon **device, const char* name); + +int suunto_eon_close (eon *device); + +int suunto_eon_read (eon *device, unsigned char data[], unsigned int size); + +int suunto_eon_write_name (eon *device, unsigned char data[], unsigned int size); + +int suunto_eon_write_interval (eon *device, unsigned char interval); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* SUUNTO_EON_H */ diff --git a/suunto_eon_test.c b/suunto_eon_test.c new file mode 100644 index 0000000..cfe9bfe --- /dev/null +++ b/suunto_eon_test.c @@ -0,0 +1,94 @@ +#include // fopen, fwrite, fclose + +#include "suunto.h" +#include "utils.h" + +#define WARNING(expr) \ +{ \ + message ("%s:%d: %s\n", __FILE__, __LINE__, expr); \ +} + +int test_dump_memory (const char* name, const char* filename) +{ + eon *device = NULL; + unsigned char data[SUUNTO_EON_MEMORY_SIZE] = {0}; + + message ("suunto_eon_open\n"); + int rc = suunto_eon_open (&device, name); + if (rc != SUUNTO_SUCCESS) { + WARNING ("Error opening serial port."); + return rc; + } + + message ("suunto_eon_read\n"); + rc = suunto_eon_read (device, data, sizeof (data)); + if (rc != SUUNTO_SUCCESS) { + WARNING ("Cannot read memory."); + suunto_eon_close (device); + return rc; + } + + message ("Dumping data\n"); + FILE* fp = fopen (filename, "wb"); + if (fp != NULL) { + fwrite (data, sizeof (unsigned char), sizeof (data), fp); + fclose (fp); + } + + message ("suunto_eon_close\n"); + rc = suunto_eon_close (device); + if (rc != SUUNTO_SUCCESS) { + WARNING ("Cannot close device."); + return rc; + } + + return SUUNTO_SUCCESS; +} + +const char* errmsg (int rc) +{ + switch (rc) { + case SUUNTO_SUCCESS: + return "Success"; + case SUUNTO_ERROR: + return "Generic error"; + case SUUNTO_ERROR_IO: + return "Input/output error"; + case SUUNTO_ERROR_MEMORY: + return "Memory error"; + case SUUNTO_ERROR_PROTOCOL: + return "Protocol error"; + case SUUNTO_ERROR_TIMEOUT: + return "Timeout"; + default: + return "Unknown error"; + } +} + + +int main(int argc, char *argv[]) +{ + message_set_logfile ("EON.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); + + int a = test_dump_memory (name, "EON.DMP"); + + message ("SUMMARY\n"); + message ("-------\n"); + message ("test_dump_memory: %s\n", errmsg (a)); + + message_set_logfile (NULL); + + return 0; +}