Port the Suunto Eon to the new api.

This commit is contained in:
Jef Driesen 2008-07-04 15:02:16 +00:00
parent d0823f6a9d
commit 630f32ff8e
4 changed files with 109 additions and 68 deletions

View File

@ -1,6 +1,6 @@
#include <stdio.h> // fopen, fwrite, fclose #include <stdio.h> // fopen, fwrite, fclose
#include "suunto.h" #include "suunto_eon.h"
#include "utils.h" #include "utils.h"
#define WARNING(expr) \ #define WARNING(expr) \
@ -10,21 +10,21 @@
int test_dump_memory (const char* name, const char* filename) int test_dump_memory (const char* name, const char* filename)
{ {
eon *device = NULL; device_t *device = NULL;
unsigned char data[SUUNTO_EON_MEMORY_SIZE] = {0}; unsigned char data[SUUNTO_EON_MEMORY_SIZE] = {0};
message ("suunto_eon_open\n"); message ("suunto_eon_device_open\n");
int rc = suunto_eon_open (&device, name); int rc = suunto_eon_device_open (&device, name);
if (rc != SUUNTO_SUCCESS) { if (rc != DEVICE_STATUS_SUCCESS) {
WARNING ("Error opening serial port."); WARNING ("Error opening serial port.");
return rc; return rc;
} }
message ("suunto_eon_read\n"); message ("device_download\n");
rc = suunto_eon_read (device, data, sizeof (data)); rc = device_download (device, data, sizeof (data));
if (rc != SUUNTO_SUCCESS) { if (rc != DEVICE_STATUS_SUCCESS) {
WARNING ("Cannot read memory."); WARNING ("Cannot read memory.");
suunto_eon_close (device); device_close (device);
return rc; return rc;
} }
@ -35,30 +35,34 @@ int test_dump_memory (const char* name, const char* filename)
fclose (fp); fclose (fp);
} }
message ("suunto_eon_close\n"); message ("device_close\n");
rc = suunto_eon_close (device); rc = device_close (device);
if (rc != SUUNTO_SUCCESS) { if (rc != DEVICE_STATUS_SUCCESS) {
WARNING ("Cannot close device."); WARNING ("Cannot close device.");
return rc; return rc;
} }
return SUUNTO_SUCCESS; return DEVICE_STATUS_SUCCESS;
} }
const char* errmsg (int rc) const char* errmsg (int rc)
{ {
switch (rc) { switch (rc) {
case SUUNTO_SUCCESS: case DEVICE_STATUS_SUCCESS:
return "Success"; return "Success";
case SUUNTO_ERROR: case DEVICE_STATUS_UNSUPPORTED:
return "Unsupported operation";
case DEVICE_STATUS_TYPE_MISMATCH:
return "Device type mismatch";
case DEVICE_STATUS_ERROR:
return "Generic error"; return "Generic error";
case SUUNTO_ERROR_IO: case DEVICE_STATUS_IO:
return "Input/output error"; return "Input/output error";
case SUUNTO_ERROR_MEMORY: case DEVICE_STATUS_MEMORY:
return "Memory error"; return "Memory error";
case SUUNTO_ERROR_PROTOCOL: case DEVICE_STATUS_PROTOCOL:
return "Protocol error"; return "Protocol error";
case SUUNTO_ERROR_TIMEOUT: case DEVICE_STATUS_TIMEOUT:
return "Timeout"; return "Timeout";
default: default:
return "Unknown error"; return "Unknown error";

View File

@ -7,6 +7,7 @@ extern "C" {
typedef enum device_type_t { typedef enum device_type_t {
DEVICE_TYPE_NULL = 0, DEVICE_TYPE_NULL = 0,
DEVICE_TYPE_SUUNTO_EON,
DEVICE_TYPE_SUUNTO_VYPER, DEVICE_TYPE_SUUNTO_VYPER,
DEVICE_TYPE_SUUNTO_VYPER2, DEVICE_TYPE_SUUNTO_VYPER2,
DEVICE_TYPE_SUUNTO_D9 DEVICE_TYPE_SUUNTO_D9

View File

@ -2,7 +2,8 @@
#include <stdlib.h> // malloc, free #include <stdlib.h> // malloc, free
#include <assert.h> // assert #include <assert.h> // assert
#include "suunto.h" #include "device-private.h"
#include "suunto_eon.h"
#include "suunto_common.h" #include "suunto_common.h"
#include "serial.h" #include "serial.h"
#include "utils.h" #include "utils.h"
@ -14,28 +15,43 @@
#define EXITCODE(rc) \ #define EXITCODE(rc) \
( \ ( \
rc == -1 ? SUUNTO_ERROR_IO : SUUNTO_ERROR_TIMEOUT \ rc == -1 ? DEVICE_STATUS_IO : DEVICE_STATUS_TIMEOUT \
) )
struct eon { typedef struct suunto_eon_device_t suunto_eon_device_t;
struct suunto_eon_device_t {
device_t base;
struct serial *port; struct serial *port;
}; };
static const device_backend_t suunto_eon_device_backend;
int static int
suunto_eon_open (eon **out, const char* name) device_is_suunto_eon (device_t *abstract)
{
if (abstract == NULL)
return 0;
return abstract->backend == &suunto_eon_device_backend;
}
device_status_t
suunto_eon_device_open (device_t **out, const char* name)
{ {
if (out == NULL) if (out == NULL)
return SUUNTO_ERROR; return DEVICE_STATUS_ERROR;
// Allocate memory. // Allocate memory.
struct eon *device = malloc (sizeof (struct eon)); suunto_eon_device_t *device = malloc (sizeof (suunto_eon_device_t));
if (device == NULL) { if (device == NULL) {
WARNING ("Failed to allocate memory."); WARNING ("Failed to allocate memory.");
return SUUNTO_ERROR_MEMORY; return DEVICE_STATUS_MEMORY;
} }
// Set the default values. // Set the default values.
device->base.backend = &suunto_eon_device_backend;
device->port = NULL; device->port = NULL;
// Open the device. // Open the device.
@ -43,7 +59,7 @@ suunto_eon_open (eon **out, const char* name)
if (rc == -1) { if (rc == -1) {
WARNING ("Failed to open the serial port."); WARNING ("Failed to open the serial port.");
free (device); free (device);
return SUUNTO_ERROR_IO; return DEVICE_STATUS_IO;
} }
// Set the serial communication protocol (1200 8N2). // Set the serial communication protocol (1200 8N2).
@ -52,7 +68,7 @@ suunto_eon_open (eon **out, const char* name)
WARNING ("Failed to set the terminal attributes."); WARNING ("Failed to set the terminal attributes.");
serial_close (device->port); serial_close (device->port);
free (device); free (device);
return SUUNTO_ERROR_IO; return DEVICE_STATUS_IO;
} }
// Set the timeout for receiving data (1000ms). // Set the timeout for receiving data (1000ms).
@ -60,7 +76,7 @@ suunto_eon_open (eon **out, const char* name)
WARNING ("Failed to set the timeout."); WARNING ("Failed to set the timeout.");
serial_close (device->port); serial_close (device->port);
free (device); free (device);
return SUUNTO_ERROR_IO; return DEVICE_STATUS_IO;
} }
// Clear the RTS line. // Clear the RTS line.
@ -68,35 +84,37 @@ suunto_eon_open (eon **out, const char* name)
WARNING ("Failed to set the DTR/RTS line."); WARNING ("Failed to set the DTR/RTS line.");
serial_close (device->port); serial_close (device->port);
free (device); free (device);
return SUUNTO_ERROR_IO; return DEVICE_STATUS_IO;
} }
*out = device; *out = (device_t*) device;
return SUUNTO_SUCCESS; return DEVICE_STATUS_SUCCESS;
} }
int static device_status_t
suunto_eon_close (eon *device) suunto_eon_device_close (device_t *abstract)
{ {
if (device == NULL) suunto_eon_device_t *device = (suunto_eon_device_t*) abstract;
return SUUNTO_SUCCESS;
if (! device_is_suunto_eon (abstract))
return DEVICE_STATUS_TYPE_MISMATCH;
// Close the device. // Close the device.
if (serial_close (device->port) == -1) { if (serial_close (device->port) == -1) {
free (device); free (device);
return SUUNTO_ERROR_IO; return DEVICE_STATUS_IO;
} }
// Free memory. // Free memory.
free (device); free (device);
return SUUNTO_SUCCESS; return DEVICE_STATUS_SUCCESS;
} }
unsigned char static unsigned char
suunto_eon_checksum (unsigned char data[], unsigned int size) suunto_eon_checksum (unsigned char data[], unsigned int size)
{ {
unsigned char crc = 0x00; unsigned char crc = 0x00;
@ -107,11 +125,13 @@ suunto_eon_checksum (unsigned char data[], unsigned int size)
} }
int static device_status_t
suunto_eon_read (eon *device, unsigned char data[], unsigned int size) suunto_eon_device_download (device_t *abstract, unsigned char data[], unsigned int size)
{ {
if (device == NULL) suunto_eon_device_t *device = (suunto_eon_device_t*) abstract;
return SUUNTO_ERROR;
if (! device_is_suunto_eon (abstract))
return DEVICE_STATUS_TYPE_MISMATCH;
// Send the command. // Send the command.
unsigned char command[1] = {'P'}; unsigned char command[1] = {'P'};
@ -134,28 +154,30 @@ suunto_eon_read (eon *device, unsigned char data[], unsigned int size)
unsigned char ccrc = suunto_eon_checksum (answer, sizeof (answer) - 1); unsigned char ccrc = suunto_eon_checksum (answer, sizeof (answer) - 1);
if (crc != ccrc) { if (crc != ccrc) {
WARNING ("Unexpected answer CRC."); WARNING ("Unexpected answer CRC.");
return SUUNTO_ERROR_PROTOCOL; return DEVICE_STATUS_PROTOCOL;
} }
if (size >= SUUNTO_EON_MEMORY_SIZE) { if (size >= SUUNTO_EON_MEMORY_SIZE) {
memcpy (data, answer, SUUNTO_EON_MEMORY_SIZE); memcpy (data, answer, SUUNTO_EON_MEMORY_SIZE);
} else { } else {
WARNING ("Insufficient buffer space available."); WARNING ("Insufficient buffer space available.");
return SUUNTO_ERROR_MEMORY; return DEVICE_STATUS_MEMORY;
} }
return SUUNTO_SUCCESS; return DEVICE_STATUS_SUCCESS;
} }
int device_status_t
suunto_eon_write_name (eon *device, unsigned char data[], unsigned int size) suunto_eon_device_write_name (device_t *abstract, unsigned char data[], unsigned int size)
{ {
if (device == NULL) suunto_eon_device_t *device = (suunto_eon_device_t*) abstract;
return SUUNTO_ERROR;
if (! device_is_suunto_eon (abstract))
return DEVICE_STATUS_TYPE_MISMATCH;
if (size > 20) if (size > 20)
return SUUNTO_ERROR; return DEVICE_STATUS_ERROR;
// Send the command. // Send the command.
unsigned char command[21] = {'N'}; unsigned char command[21] = {'N'};
@ -166,15 +188,17 @@ suunto_eon_write_name (eon *device, unsigned char data[], unsigned int size)
return EXITCODE (rc); return EXITCODE (rc);
} }
return SUUNTO_SUCCESS; return DEVICE_STATUS_SUCCESS;
} }
int device_status_t
suunto_eon_write_interval (eon *device, unsigned char interval) suunto_eon_device_write_interval (device_t *abstract, unsigned char interval)
{ {
if (device == NULL) suunto_eon_device_t *device = (suunto_eon_device_t*) abstract;
return SUUNTO_ERROR;
if (! device_is_suunto_eon (abstract))
return DEVICE_STATUS_TYPE_MISMATCH;
// Send the command. // Send the command.
unsigned char command[2] = {'T', interval}; unsigned char command[2] = {'T', interval};
@ -184,11 +208,11 @@ suunto_eon_write_interval (eon *device, unsigned char interval)
return EXITCODE (rc); return EXITCODE (rc);
} }
return SUUNTO_SUCCESS; return DEVICE_STATUS_SUCCESS;
} }
int device_status_t
suunto_eon_extract_dives (const unsigned char data[], unsigned int size, dive_callback_t callback, void *userdata) suunto_eon_extract_dives (const unsigned char data[], unsigned int size, dive_callback_t callback, void *userdata)
{ {
assert (size >= SUUNTO_EON_MEMORY_SIZE); assert (size >= SUUNTO_EON_MEMORY_SIZE);
@ -204,3 +228,15 @@ suunto_eon_extract_dives (const unsigned char data[], unsigned int size, dive_ca
return suunto_common_extract_dives (data, 0x100, SUUNTO_EON_MEMORY_SIZE, eop, 3, callback, userdata); return suunto_common_extract_dives (data, 0x100, SUUNTO_EON_MEMORY_SIZE, eop, 3, callback, userdata);
} }
static const device_backend_t suunto_eon_device_backend = {
DEVICE_TYPE_SUUNTO_EON,
NULL, /* handshake */
NULL, /* version */
NULL, /* read */
NULL, /* write */
suunto_eon_device_download, /* download */
NULL, /* foreach */
suunto_eon_device_close /* close */
};

View File

@ -5,21 +5,21 @@
extern "C" { extern "C" {
#endif /* __cplusplus */ #endif /* __cplusplus */
typedef struct eon eon; #include "device.h"
#define SUUNTO_EON_MEMORY_SIZE 0x900 #define SUUNTO_EON_MEMORY_SIZE 0x900
int suunto_eon_open (eon **device, const char* name); device_status_t
suunto_eon_device_open (device_t **device, const char* name);
int suunto_eon_close (eon *device); device_status_t
suunto_eon_device_write_name (device_t *device, unsigned char data[], unsigned int size);
int suunto_eon_read (eon *device, unsigned char data[], unsigned int size); device_status_t
suunto_eon_device_write_interval (device_t *device, unsigned char interval);
int suunto_eon_write_name (eon *device, unsigned char data[], unsigned int size); device_status_t
suunto_eon_extract_dives (const unsigned char data[], unsigned int size, dive_callback_t callback, void *userdata);
int suunto_eon_write_interval (eon *device, unsigned char interval);
int suunto_eon_extract_dives (const unsigned char data[], unsigned int size, dive_callback_t callback, void *userdata);
#ifdef __cplusplus #ifdef __cplusplus
} }