Port the Suunto Vyper to the new api.

This commit is contained in:
Jef Driesen 2008-07-04 13:35:57 +00:00
parent ea01b66dec
commit a5fb93d199
5 changed files with 182 additions and 141 deletions

View File

@ -1,8 +1,7 @@
#include <stdio.h> // fopen, fwrite, fclose #include <stdio.h> // fopen, fwrite, fclose
#include <stdlib.h> // atoi #include <stdlib.h> // atoi
#include "suunto.h" #include "suunto_vyper.h"
#include "serial.h"
#include "utils.h" #include "utils.h"
#define WARNING(expr) \ #define WARNING(expr) \
@ -12,70 +11,70 @@
int test_dump_sdm (const char* name, unsigned int delay) int test_dump_sdm (const char* name, unsigned int delay)
{ {
vyper *device = NULL; device_t *device = NULL;
message ("suunto_vyper_open\n"); message ("suunto_vyper_device_open\n");
int rc = suunto_vyper_open (&device, name); int rc = suunto_vyper_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;
} }
suunto_vyper_set_delay (device, delay); suunto_vyper_device_set_delay (device, delay);
message ("suunto_vyper_detect_interface\n"); message ("suunto_vyper_device_detect_interface\n");
rc = suunto_vyper_detect_interface (device); rc = suunto_vyper_device_detect_interface (device);
if (rc != SUUNTO_SUCCESS) { if (rc != DEVICE_STATUS_SUCCESS) {
WARNING ("Interface not found."); WARNING ("Interface not found.");
suunto_vyper_close (device); device_close (device);
return rc; return rc;
} }
message ("suunto_vyper_read_dives\n"); message ("device_foreach\n");
rc = suunto_vyper_read_dives (device, NULL, NULL); rc = device_foreach (device, NULL, NULL);
if (rc != SUUNTO_SUCCESS) { if (rc != DEVICE_STATUS_SUCCESS) {
WARNING ("Cannot read dives."); WARNING ("Cannot read dives.");
suunto_vyper_close (device); device_close (device);
return rc; return rc;
} }
message ("suunto_vyper_close\n"); message ("device_close\n");
rc = suunto_vyper_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;
} }
int test_dump_memory (const char* name, unsigned int delay, const char* filename) int test_dump_memory (const char* name, unsigned int delay, const char* filename)
{ {
unsigned char data[SUUNTO_VYPER_MEMORY_SIZE] = {0}; unsigned char data[SUUNTO_VYPER_MEMORY_SIZE] = {0};
vyper *device = NULL; device_t *device = NULL;
message ("suunto_vyper_open\n"); message ("suunto_vyper_device_open\n");
int rc = suunto_vyper_open (&device, name); int rc = suunto_vyper_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;
} }
suunto_vyper_set_delay (device, delay); suunto_vyper_device_set_delay (device, delay);
message ("suunto_vyper_detect_interface\n"); message ("suunto_vyper_device_detect_interface\n");
rc = suunto_vyper_detect_interface (device); rc = suunto_vyper_device_detect_interface (device);
if (rc != SUUNTO_SUCCESS) { if (rc != DEVICE_STATUS_SUCCESS) {
WARNING ("Interface not found."); WARNING ("Interface not found.");
suunto_vyper_close (device); device_close (device);
return rc; return rc;
} }
message ("suunto_vyper_read_memory\n"); message ("device_read\n");
rc = suunto_vyper_read_memory (device, 0x00, data, sizeof (data)); rc = device_read (device, 0x00, data, sizeof (data));
if (rc != SUUNTO_SUCCESS) { if (rc != DEVICE_STATUS_SUCCESS) {
WARNING ("Cannot read memory."); WARNING ("Cannot read memory.");
suunto_vyper_close (device); device_close (device);
return rc; return rc;
} }
@ -86,30 +85,34 @@ int test_dump_memory (const char* name, unsigned int delay, const char* filename
fclose (fp); fclose (fp);
} }
message ("suunto_vyper_close\n"); message ("device_close\n");
rc = suunto_vyper_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

@ -6,7 +6,8 @@ extern "C" {
#endif /* __cplusplus */ #endif /* __cplusplus */
typedef enum device_type_t { typedef enum device_type_t {
DEVICE_TYPE_NULL = 0 DEVICE_TYPE_NULL = 0,
DEVICE_TYPE_SUUNTO_VYPER
} device_type_t; } device_type_t;
typedef enum device_status_t { typedef enum device_status_t {

View File

@ -1,6 +1,8 @@
#ifndef SUUNTO_H #ifndef SUUNTO_H
#define SUUNTO_H #define SUUNTO_H
#include "device.h"
#define SUUNTO_SUCCESS 0 #define SUUNTO_SUCCESS 0
#define SUUNTO_ERROR -1 #define SUUNTO_ERROR -1
#define SUUNTO_ERROR_IO -2 #define SUUNTO_ERROR_IO -2
@ -8,8 +10,6 @@
#define SUUNTO_ERROR_PROTOCOL -4 #define SUUNTO_ERROR_PROTOCOL -4
#define SUUNTO_ERROR_TIMEOUT -5 #define SUUNTO_ERROR_TIMEOUT -5
typedef void (*dive_callback_t) (const unsigned char *data, unsigned int size, void *userdata);
#include "suunto_eon.h" #include "suunto_eon.h"
#include "suunto_vyper.h" #include "suunto_vyper.h"
#include "suunto_vyper2.h" #include "suunto_vyper2.h"

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_vyper.h"
#include "suunto_common.h" #include "suunto_common.h"
#include "serial.h" #include "serial.h"
#include "utils.h" #include "utils.h"
@ -15,8 +16,10 @@
message ("%s:%d: %s\n", __FILE__, __LINE__, expr); \ message ("%s:%d: %s\n", __FILE__, __LINE__, expr); \
} }
typedef struct suunto_vyper_device_t suunto_vyper_device_t;
struct vyper { struct suunto_vyper_device_t {
device_t base;
struct serial *port; struct serial *port;
int extraanswertime; int extraanswertime;
int ifacealwaysechos; int ifacealwaysechos;
@ -24,21 +27,33 @@ struct vyper {
unsigned int delay; unsigned int delay;
}; };
static const device_backend_t suunto_vyper_device_backend;
int static int
suunto_vyper_open (vyper **out, const char* name) device_is_suunto_vyper (device_t *abstract)
{
if (abstract == NULL)
return 0;
return abstract->backend == &suunto_vyper_device_backend;
}
device_status_t
suunto_vyper_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 vyper *device = malloc (sizeof (struct vyper)); suunto_vyper_device_t *device = malloc (sizeof (suunto_vyper_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_vyper_device_backend;
device->port = NULL; device->port = NULL;
device->extraanswertime = 0; device->extraanswertime = 0;
device->ifacealwaysechos = 0; device->ifacealwaysechos = 0;
@ -50,7 +65,7 @@ suunto_vyper_open (vyper **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 (2400 8O1). // Set the serial communication protocol (2400 8O1).
@ -59,7 +74,7 @@ suunto_vyper_open (vyper **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 (1000 ms). // Set the timeout for receiving data (1000 ms).
@ -67,7 +82,7 @@ suunto_vyper_open (vyper **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;
} }
// Set the DTR line (power supply for the interface). // Set the DTR line (power supply for the interface).
@ -75,7 +90,7 @@ suunto_vyper_open (vyper **out, const char* name)
WARNING ("Failed to set the DTR line."); WARNING ("Failed to set the DTR line.");
serial_close (device->port); serial_close (device->port);
free (device); free (device);
return SUUNTO_ERROR_IO; return DEVICE_STATUS_IO;
} }
// Give the interface 100 ms to settle and draw power up. // Give the interface 100 ms to settle and draw power up.
@ -84,28 +99,30 @@ suunto_vyper_open (vyper **out, const char* name)
// Make sure everything is in a sane state. // Make sure everything is in a sane state.
serial_flush (device->port, SERIAL_QUEUE_BOTH); serial_flush (device->port, SERIAL_QUEUE_BOTH);
*out = device; *out = (device_t*) device;
return SUUNTO_SUCCESS; return DEVICE_STATUS_SUCCESS;
} }
int static device_status_t
suunto_vyper_close (vyper *device) suunto_vyper_device_close (device_t *abstract)
{ {
if (device == NULL) suunto_vyper_device_t *device = (suunto_vyper_device_t*) abstract;
return SUUNTO_SUCCESS;
if (! device_is_suunto_vyper (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;
} }
@ -131,27 +148,29 @@ suunto_vyper_reverse (unsigned char data[], unsigned int size)
} }
static int static device_status_t
suunto_vyper_send_testcmd (vyper *device, const unsigned char* data, unsigned int size) suunto_vyper_send_testcmd (suunto_vyper_device_t *device, const unsigned char* data, unsigned int size)
{ {
if (serial_write (device->port, data, size) != size) { if (serial_write (device->port, data, size) != size) {
WARNING ("Failed to send the test sequence."); WARNING ("Failed to send the test sequence.");
return SUUNTO_ERROR_IO; return DEVICE_STATUS_IO;
} }
serial_drain (device->port); serial_drain (device->port);
return SUUNTO_SUCCESS; return DEVICE_STATUS_SUCCESS;
} }
int device_status_t
suunto_vyper_detect_interface (vyper *device) suunto_vyper_device_detect_interface (device_t *abstract)
{ {
suunto_vyper_device_t *device = (suunto_vyper_device_t*) abstract;
if (! device_is_suunto_vyper (abstract))
return DEVICE_STATUS_TYPE_MISMATCH;
int rc, detectmode_worked = 1; int rc, detectmode_worked = 1;
unsigned char command[3] = {'A', 'T', '\r'}, reply[3] = {0}, extra = 0; unsigned char command[3] = {'A', 'T', '\r'}, reply[3] = {0}, extra = 0;
if (device == NULL)
return SUUNTO_ERROR;
// Make sure everything is in a sane state. // Make sure everything is in a sane state.
serial_flush (device->port, SERIAL_QUEUE_BOTH); serial_flush (device->port, SERIAL_QUEUE_BOTH);
@ -163,7 +182,7 @@ suunto_vyper_detect_interface (vyper *device)
serial_set_rts (device->port, 0); serial_set_rts (device->port, 0);
rc = suunto_vyper_send_testcmd (device, command, 3); rc = suunto_vyper_send_testcmd (device, command, 3);
if (rc != SUUNTO_SUCCESS) if (rc != DEVICE_STATUS_SUCCESS)
return rc; return rc;
rc = serial_read (device->port, reply, 3); rc = serial_read (device->port, reply, 3);
if (rc != 3 || memcmp (command, reply, 3) != 0) { if (rc != 3 || memcmp (command, reply, 3) != 0) {
@ -178,7 +197,7 @@ suunto_vyper_detect_interface (vyper *device)
serial_set_rts (device->port, 1); serial_set_rts (device->port, 1);
rc = suunto_vyper_send_testcmd (device, command, 3); rc = suunto_vyper_send_testcmd (device, command, 3);
if (rc != SUUNTO_SUCCESS) if (rc != DEVICE_STATUS_SUCCESS)
return rc; return rc;
serial_set_rts (device->port, 0); serial_set_rts (device->port, 0);
rc = serial_read (device->port, reply, 3); rc = serial_read (device->port, reply, 3);
@ -189,7 +208,7 @@ suunto_vyper_detect_interface (vyper *device)
WARNING ("Can't detect the interface. Hoping it's an original suunto interface with the DC already attached."); WARNING ("Can't detect the interface. Hoping it's an original suunto interface with the DC already attached.");
} }
device->ifacealwaysechos = 0; device->ifacealwaysechos = 0;
return SUUNTO_SUCCESS; return DEVICE_STATUS_SUCCESS;
} }
if (rc != 3 || memcmp (command, reply, 3) != 0) { if (rc != 3 || memcmp (command, reply, 3) != 0) {
WARNING ("Interface not responding in transfer mode."); WARNING ("Interface not responding in transfer mode.");
@ -199,24 +218,26 @@ suunto_vyper_detect_interface (vyper *device)
} }
WARNING ("Detected a clone interface without RTS-switching."); WARNING ("Detected a clone interface without RTS-switching.");
device->ifacealwaysechos = 1; device->ifacealwaysechos = 1;
return SUUNTO_SUCCESS; return DEVICE_STATUS_SUCCESS;
} }
int device_status_t
suunto_vyper_set_delay (vyper *device, unsigned int delay) suunto_vyper_device_set_delay (device_t *abstract, unsigned int delay)
{ {
if (device == NULL) suunto_vyper_device_t *device = (suunto_vyper_device_t*) abstract;
return SUUNTO_ERROR;
if (! device_is_suunto_vyper (abstract))
return DEVICE_STATUS_TYPE_MISMATCH;
device->delay = delay; device->delay = delay;
return SUUNTO_SUCCESS; return DEVICE_STATUS_SUCCESS;
} }
static int static device_status_t
suunto_vyper_send (vyper *device, const unsigned char command[], unsigned int csize) suunto_vyper_send (suunto_vyper_device_t *device, const unsigned char command[], unsigned int csize)
{ {
serial_sleep (device->delay); serial_sleep (device->delay);
@ -245,18 +266,18 @@ suunto_vyper_send (vyper *device, const unsigned char command[], unsigned int cs
// Clear RTS to receive the reply. // Clear RTS to receive the reply.
serial_set_rts (device->port, 0); serial_set_rts (device->port, 0);
return SUUNTO_SUCCESS; return DEVICE_STATUS_SUCCESS;
} }
static int static device_status_t
suunto_vyper_transfer (vyper *device, const unsigned char command[], unsigned int csize, unsigned char answer[], unsigned int asize, unsigned int size) suunto_vyper_transfer (suunto_vyper_device_t *device, const unsigned char command[], unsigned int csize, unsigned char answer[], unsigned int asize, unsigned int size)
{ {
assert (asize >= size + 2); assert (asize >= size + 2);
// Send the command to the dive computer. // Send the command to the dive computer.
int rc = suunto_vyper_send (device, command, csize); int rc = suunto_vyper_send (device, command, csize);
if (rc != SUUNTO_SUCCESS) { if (rc != DEVICE_STATUS_SUCCESS) {
WARNING ("Failed to send the command."); WARNING ("Failed to send the command.");
return rc; return rc;
} }
@ -266,14 +287,14 @@ suunto_vyper_transfer (vyper *device, const unsigned char command[], unsigned in
if (rc != asize) { if (rc != asize) {
WARNING ("Failed to receive the answer."); WARNING ("Failed to receive the answer.");
if (rc == -1) if (rc == -1)
return SUUNTO_ERROR_IO; return DEVICE_STATUS_IO;
return SUUNTO_ERROR_TIMEOUT; return DEVICE_STATUS_TIMEOUT;
} }
// Verify the header of the package. // Verify the header of the package.
if (memcmp (command, answer, asize - size - 1) != 0) { if (memcmp (command, answer, asize - size - 1) != 0) {
WARNING ("Unexpected answer start byte(s)."); WARNING ("Unexpected answer start byte(s).");
return SUUNTO_ERROR_PROTOCOL; return DEVICE_STATUS_PROTOCOL;
} }
// Verify the checksum of the package. // Verify the checksum of the package.
@ -281,18 +302,20 @@ suunto_vyper_transfer (vyper *device, const unsigned char command[], unsigned in
unsigned char ccrc = suunto_vyper_checksum (answer, asize - 1, 0x00); unsigned char ccrc = suunto_vyper_checksum (answer, asize - 1, 0x00);
if (crc != ccrc) { if (crc != ccrc) {
WARNING ("Unexpected answer CRC."); WARNING ("Unexpected answer CRC.");
return SUUNTO_ERROR_PROTOCOL; return DEVICE_STATUS_PROTOCOL;
} }
return SUUNTO_SUCCESS; return DEVICE_STATUS_SUCCESS;
} }
int static device_status_t
suunto_vyper_read_memory (vyper *device, unsigned int address, unsigned char data[], unsigned int size) suunto_vyper_device_read (device_t *abstract, unsigned int address, unsigned char data[], unsigned int size)
{ {
if (device == NULL) suunto_vyper_device_t *device = (suunto_vyper_device_t*) abstract;
return SUUNTO_ERROR;
if (! device_is_suunto_vyper (abstract))
return DEVICE_STATUS_TYPE_MISMATCH;
// The data transmission is split in packages // The data transmission is split in packages
// of maximum $SUUNTO_VYPER_PACKET_SIZE bytes. // of maximum $SUUNTO_VYPER_PACKET_SIZE bytes.
@ -311,7 +334,7 @@ suunto_vyper_read_memory (vyper *device, unsigned int address, unsigned char dat
0}; // CRC 0}; // CRC
command[4] = suunto_vyper_checksum (command, 4, 0x00); command[4] = suunto_vyper_checksum (command, 4, 0x00);
int rc = suunto_vyper_transfer (device, command, sizeof (command), answer, len + 5, len); int rc = suunto_vyper_transfer (device, command, sizeof (command), answer, len + 5, len);
if (rc != SUUNTO_SUCCESS) if (rc != DEVICE_STATUS_SUCCESS)
return rc; return rc;
memcpy (data, answer + 4, len); memcpy (data, answer + 4, len);
@ -329,15 +352,17 @@ suunto_vyper_read_memory (vyper *device, unsigned int address, unsigned char dat
data += len; data += len;
} }
return SUUNTO_SUCCESS; return DEVICE_STATUS_SUCCESS;
} }
int static device_status_t
suunto_vyper_write_memory (vyper *device, unsigned int address, const unsigned char data[], unsigned int size) suunto_vyper_device_write (device_t *abstract, unsigned int address, const unsigned char data[], unsigned int size)
{ {
if (device == NULL) suunto_vyper_device_t *device = (suunto_vyper_device_t*) abstract;
return SUUNTO_ERROR;
if (! device_is_suunto_vyper (abstract))
return DEVICE_STATUS_TYPE_MISMATCH;
// The data transmission is split in packages // The data transmission is split in packages
// of maximum $SUUNTO_VYPER_PACKET_SIZE bytes. // of maximum $SUUNTO_VYPER_PACKET_SIZE bytes.
@ -351,7 +376,7 @@ suunto_vyper_write_memory (vyper *device, unsigned int address, const unsigned c
unsigned char panswer[3] = {0}; unsigned char panswer[3] = {0};
unsigned char pcommand[3] = {0x07, 0xA5, 0xA2}; unsigned char pcommand[3] = {0x07, 0xA5, 0xA2};
int rc = suunto_vyper_transfer (device, pcommand, sizeof (pcommand), panswer, sizeof (panswer), 0); int rc = suunto_vyper_transfer (device, pcommand, sizeof (pcommand), panswer, sizeof (panswer), 0);
if (rc != SUUNTO_SUCCESS) if (rc != DEVICE_STATUS_SUCCESS)
return rc; return rc;
#ifndef NDEBUG #ifndef NDEBUG
@ -368,7 +393,7 @@ suunto_vyper_write_memory (vyper *device, unsigned int address, const unsigned c
memcpy (wcommand + 4, data, len); memcpy (wcommand + 4, data, len);
wcommand[len + 4] = suunto_vyper_checksum (wcommand, len + 4, 0x00); wcommand[len + 4] = suunto_vyper_checksum (wcommand, len + 4, 0x00);
rc = suunto_vyper_transfer (device, wcommand, len + 5, wanswer, sizeof (wanswer), 0); rc = suunto_vyper_transfer (device, wcommand, len + 5, wanswer, sizeof (wanswer), 0);
if (rc != SUUNTO_SUCCESS) if (rc != DEVICE_STATUS_SUCCESS)
return rc; return rc;
#ifndef NDEBUG #ifndef NDEBUG
@ -384,21 +409,23 @@ suunto_vyper_write_memory (vyper *device, unsigned int address, const unsigned c
data += len; data += len;
} }
return SUUNTO_SUCCESS; return DEVICE_STATUS_SUCCESS;
} }
int device_status_t
suunto_vyper_read_dive (vyper *device, unsigned char data[], unsigned int size, int init) suunto_vyper_device_read_dive (device_t *abstract, unsigned char data[], unsigned int size, int init)
{ {
if (device == NULL) suunto_vyper_device_t *device = (suunto_vyper_device_t*) abstract;
return SUUNTO_ERROR;
if (! device_is_suunto_vyper (abstract))
return DEVICE_STATUS_TYPE_MISMATCH;
// Send the command to the dive computer. // Send the command to the dive computer.
unsigned char command[3] = {init ? 0x08 : 0x09, 0xA5, 0x00}; unsigned char command[3] = {init ? 0x08 : 0x09, 0xA5, 0x00};
command[2] = suunto_vyper_checksum (command, 2, 0x00); command[2] = suunto_vyper_checksum (command, 2, 0x00);
int rc = suunto_vyper_send (device, command, 3); int rc = suunto_vyper_send (device, command, 3);
if (rc != SUUNTO_SUCCESS) { if (rc != DEVICE_STATUS_SUCCESS) {
WARNING ("Failed to send the command."); WARNING ("Failed to send the command.");
return rc; return rc;
} }
@ -424,15 +451,15 @@ suunto_vyper_read_dive (vyper *device, unsigned char data[], unsigned int size,
break; break;
WARNING ("Failed to receive the answer."); WARNING ("Failed to receive the answer.");
if (rc == -1) if (rc == -1)
return SUUNTO_ERROR_IO; return DEVICE_STATUS_IO;
return SUUNTO_ERROR_TIMEOUT; return DEVICE_STATUS_TIMEOUT;
} }
// Verify the header of the package. // Verify the header of the package.
if (answer[0] != command[0] || if (answer[0] != command[0] ||
answer[1] > SUUNTO_VYPER_PACKET_SIZE) { answer[1] > SUUNTO_VYPER_PACKET_SIZE) {
WARNING ("Unexpected answer start byte(s)."); WARNING ("Unexpected answer start byte(s).");
return SUUNTO_ERROR_PROTOCOL; return DEVICE_STATUS_PROTOCOL;
} }
// Receive the remaining part of the package. // Receive the remaining part of the package.
@ -441,8 +468,8 @@ suunto_vyper_read_dive (vyper *device, unsigned char data[], unsigned int size,
if (rc != len + 1) { if (rc != len + 1) {
WARNING ("Failed to receive the answer."); WARNING ("Failed to receive the answer.");
if (rc == -1) if (rc == -1)
return SUUNTO_ERROR_IO; return DEVICE_STATUS_IO;
return SUUNTO_ERROR_TIMEOUT; return DEVICE_STATUS_TIMEOUT;
} }
// Verify the checksum of the package. // Verify the checksum of the package.
@ -450,7 +477,7 @@ suunto_vyper_read_dive (vyper *device, unsigned char data[], unsigned int size,
unsigned char ccrc = suunto_vyper_checksum (answer, len + 2, 0x00); unsigned char ccrc = suunto_vyper_checksum (answer, len + 2, 0x00);
if (crc != ccrc) { if (crc != ccrc) {
WARNING ("Unexpected answer CRC."); WARNING ("Unexpected answer CRC.");
return SUUNTO_ERROR_PROTOCOL; return DEVICE_STATUS_PROTOCOL;
} }
// Append the package to the output buffer. // Append the package to the output buffer.
@ -459,7 +486,7 @@ suunto_vyper_read_dive (vyper *device, unsigned char data[], unsigned int size,
nbytes += len; nbytes += len;
} else { } else {
WARNING ("Insufficient buffer space available."); WARNING ("Insufficient buffer space available.");
return SUUNTO_ERROR_MEMORY; return DEVICE_STATUS_MEMORY;
} }
// The DC sends a null package (a package with length zero) when it // The DC sends a null package (a package with length zero) when it
@ -505,11 +532,11 @@ suunto_vyper_read_dive (vyper *device, unsigned char data[], unsigned int size,
} }
int static device_status_t
suunto_vyper_read_dives (vyper *device, dive_callback_t callback, void *userdata) suunto_vyper_device_foreach (device_t *abstract, dive_callback_t callback, void *userdata)
{ {
if (device == NULL) if (! device_is_suunto_vyper (abstract))
return SUUNTO_ERROR; return DEVICE_STATUS_TYPE_MISMATCH;
// The memory layout of the Spyder is different from the Vyper // The memory layout of the Spyder is different from the Vyper
// (and all other compatible dive computers). The Spyder has // (and all other compatible dive computers). The Spyder has
@ -521,7 +548,7 @@ suunto_vyper_read_dives (vyper *device, dive_callback_t callback, void *userdata
int rc = 0; int rc = 0;
unsigned int ndives = 0; unsigned int ndives = 0;
unsigned int offset = 0; unsigned int offset = 0;
while ((rc = suunto_vyper_read_dive (device, data + offset, sizeof (data) - offset, (ndives == 0))) > 0) { while ((rc = suunto_vyper_device_read_dive (abstract, data + offset, sizeof (data) - offset, (ndives == 0))) > 0) {
if (callback) if (callback)
callback (data + offset, rc, userdata); callback (data + offset, rc, userdata);
@ -532,11 +559,11 @@ suunto_vyper_read_dives (vyper *device, dive_callback_t callback, void *userdata
if (rc != 0) if (rc != 0)
return rc; return rc;
return SUUNTO_SUCCESS; return DEVICE_STATUS_SUCCESS;
} }
int device_status_t
suunto_vyper_extract_dives (const unsigned char data[], unsigned int size, dive_callback_t callback, void *userdata) suunto_vyper_extract_dives (const unsigned char data[], unsigned int size, dive_callback_t callback, void *userdata)
{ {
assert (size >= SUUNTO_VYPER_MEMORY_SIZE); assert (size >= SUUNTO_VYPER_MEMORY_SIZE);
@ -547,7 +574,7 @@ suunto_vyper_extract_dives (const unsigned char data[], unsigned int size, dive_
} }
int device_status_t
suunto_spyder_extract_dives (const unsigned char data[], unsigned int size, dive_callback_t callback, void *userdata) suunto_spyder_extract_dives (const unsigned char data[], unsigned int size, dive_callback_t callback, void *userdata)
{ {
assert (size >= SUUNTO_VYPER_MEMORY_SIZE); assert (size >= SUUNTO_VYPER_MEMORY_SIZE);
@ -556,3 +583,15 @@ suunto_spyder_extract_dives (const unsigned char data[], unsigned int size, dive
return suunto_common_extract_dives (data, 0x4C, SUUNTO_VYPER_MEMORY_SIZE, eop, 3, callback, userdata); return suunto_common_extract_dives (data, 0x4C, SUUNTO_VYPER_MEMORY_SIZE, eop, 3, callback, userdata);
} }
static const device_backend_t suunto_vyper_device_backend = {
DEVICE_TYPE_SUUNTO_VYPER,
NULL, /* handshake */
NULL, /* version */
suunto_vyper_device_read, /* read */
suunto_vyper_device_write, /* write */
NULL, /* download */
suunto_vyper_device_foreach, /* foreach */
suunto_vyper_device_close /* close */
};

View File

@ -5,30 +5,28 @@
extern "C" { extern "C" {
#endif /* __cplusplus */ #endif /* __cplusplus */
typedef struct vyper vyper; #include "device.h"
#define SUUNTO_VYPER_MEMORY_SIZE 0x2000 #define SUUNTO_VYPER_MEMORY_SIZE 0x2000
#define SUUNTO_VYPER_PACKET_SIZE 32 #define SUUNTO_VYPER_PACKET_SIZE 32
int suunto_vyper_open (vyper **device, const char* name); device_status_t
suunto_vyper_device_open (device_t **device, const char* name);
int suunto_vyper_close (vyper *device); device_status_t
suunto_vyper_device_set_delay (device_t *device, unsigned int delay);
int suunto_vyper_set_delay (vyper *device, unsigned int delay); device_status_t
suunto_vyper_device_detect_interface (device_t *device);
int suunto_vyper_detect_interface (vyper *device); device_status_t
suunto_vyper_device_read_dive (device_t *device, unsigned char data[], unsigned int size, int init);
int suunto_vyper_read_dive (vyper *device, unsigned char data[], unsigned int size, int init); device_status_t
suunto_vyper_extract_dives (const unsigned char data[], unsigned int size, dive_callback_t callback, void *userdata);
int suunto_vyper_read_memory (vyper *device, unsigned int address, unsigned char data[], unsigned int size); device_status_t
suunto_spyder_extract_dives (const unsigned char data[], unsigned int size, dive_callback_t callback, void *userdata);
int suunto_vyper_write_memory (vyper *device, unsigned int address, const unsigned char data[], unsigned int size);
int suunto_vyper_read_dives (vyper *device, dive_callback_t callback, void *userdata);
int suunto_vyper_extract_dives (const unsigned char data[], unsigned int size, dive_callback_t callback, void *userdata);
int suunto_spyder_extract_dives (const unsigned char data[], unsigned int size, dive_callback_t callback, void *userdata);
#ifdef __cplusplus #ifdef __cplusplus
} }