Use common error codes for all Suunto divecomputers.

This commit is contained in:
Jef Driesen 2007-12-19 09:24:09 +00:00
parent ec04cc516c
commit 50bc70d89a
4 changed files with 58 additions and 58 deletions

View File

@ -1,6 +1,13 @@
#ifndef SUUNTO_H #ifndef SUUNTO_H
#define SUUNTO_H #define SUUNTO_H
#define SUUNTO_SUCCESS 0
#define SUUNTO_ERROR -1
#define SUUNTO_ERROR_IO -2
#define SUUNTO_ERROR_MEMORY -3
#define SUUNTO_ERROR_PROTOCOL -4
#define SUUNTO_ERROR_TIMEOUT -5
#include "suunto_vyper.h" #include "suunto_vyper.h"
#endif /* SUUNTO_H */ #endif /* SUUNTO_H */

View File

@ -16,8 +16,8 @@
#define EXITCODE(rc, n) \ #define EXITCODE(rc, n) \
( \ ( \
rc == -1 ? \ rc == -1 ? \
SUUNTO_VYPER_ERROR_IO : \ SUUNTO_ERROR_IO : \
(rc != n ? SUUNTO_VYPER_ERROR_TIMEOUT : SUUNTO_VYPER_ERROR_PROTOCOL) \ (rc != n ? SUUNTO_ERROR_TIMEOUT : SUUNTO_ERROR_PROTOCOL) \
) )
@ -33,13 +33,13 @@ int
suunto_vyper_open (vyper **out, const char* name) suunto_vyper_open (vyper **out, const char* name)
{ {
if (out == NULL) if (out == NULL)
return SUUNTO_VYPER_ERROR; return SUUNTO_ERROR;
// Allocate memory. // Allocate memory.
struct vyper *device = malloc (sizeof (struct vyper)); struct vyper *device = malloc (sizeof (struct vyper));
if (device == NULL) { if (device == NULL) {
WARNING ("Failed to allocate memory."); WARNING ("Failed to allocate memory.");
return SUUNTO_VYPER_ERROR_MEMORY; return SUUNTO_ERROR_MEMORY;
} }
// Set the default values. // Set the default values.
@ -53,7 +53,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_VYPER_ERROR_IO; return SUUNTO_ERROR_IO;
} }
// Set the serial communication protocol (2400 8O1). // Set the serial communication protocol (2400 8O1).
@ -62,7 +62,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_VYPER_ERROR_IO; return SUUNTO_ERROR_IO;
} }
// Set the timeout for receiving data (1000 ms). // Set the timeout for receiving data (1000 ms).
@ -70,7 +70,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_VYPER_ERROR_IO; return SUUNTO_ERROR_IO;
} }
// Set the DTR line (power supply for the interface). // Set the DTR line (power supply for the interface).
@ -78,7 +78,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_VYPER_ERROR_IO; return SUUNTO_ERROR_IO;
} }
// Give the interface 100 ms to settle and draw power up. // Give the interface 100 ms to settle and draw power up.
@ -89,7 +89,7 @@ suunto_vyper_open (vyper **out, const char* name)
*out = device; *out = device;
return SUUNTO_VYPER_SUCCESS; return SUUNTO_SUCCESS;
} }
@ -97,18 +97,18 @@ int
suunto_vyper_close (vyper *device) suunto_vyper_close (vyper *device)
{ {
if (device == NULL) if (device == NULL)
return SUUNTO_VYPER_SUCCESS; return SUUNTO_SUCCESS;
// Close the device. // Close the device.
if (serial_close (device->port) == -1) { if (serial_close (device->port) == -1) {
free (device); free (device);
return SUUNTO_VYPER_ERROR_IO; return SUUNTO_ERROR_IO;
} }
// Free memory. // Free memory.
free (device); free (device);
return SUUNTO_VYPER_SUCCESS; return SUUNTO_SUCCESS;
} }
@ -139,10 +139,10 @@ suunto_vyper_send_testcmd (vyper *device, const unsigned char* data, unsigned in
{ {
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_VYPER_ERROR_IO; return SUUNTO_ERROR_IO;
} }
serial_drain (device->port); serial_drain (device->port);
return SUUNTO_VYPER_SUCCESS; return SUUNTO_SUCCESS;
} }
@ -153,7 +153,7 @@ suunto_vyper_detect_interface (vyper *device)
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) if (device == NULL)
return SUUNTO_VYPER_ERROR; 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);
@ -166,7 +166,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_VYPER_SUCCESS) if (rc != SUUNTO_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) {
@ -181,7 +181,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_VYPER_SUCCESS) if (rc != SUUNTO_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);
@ -192,7 +192,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_VYPER_SUCCESS; return SUUNTO_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.");
@ -202,7 +202,7 @@ 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_VYPER_SUCCESS; return SUUNTO_SUCCESS;
} }
@ -234,7 +234,7 @@ 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_VYPER_SUCCESS; return SUUNTO_SUCCESS;
} }
@ -243,7 +243,7 @@ suunto_vyper_transfer (vyper *device, const unsigned char command[], unsigned in
{ {
// 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_VYPER_SUCCESS) { if (rc != SUUNTO_SUCCESS) {
WARNING ("Failed to send the command."); WARNING ("Failed to send the command.");
return rc; return rc;
} }
@ -285,7 +285,7 @@ suunto_vyper_transfer (vyper *device, const unsigned char command[], unsigned in
return EXITCODE (rc, 1); return EXITCODE (rc, 1);
} }
return SUUNTO_VYPER_SUCCESS; return SUUNTO_SUCCESS;
} }
@ -293,7 +293,7 @@ int
suunto_vyper_read_memory (vyper *device, unsigned int address, unsigned char data[], unsigned int size) suunto_vyper_read_memory (vyper *device, unsigned int address, unsigned char data[], unsigned int size)
{ {
if (device == NULL) if (device == NULL)
return SUUNTO_VYPER_ERROR; return SUUNTO_ERROR;
// 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.
@ -312,7 +312,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, 5, header, 4, data, len); int rc = suunto_vyper_transfer (device, command, 5, header, 4, data, len);
if (rc != SUUNTO_VYPER_SUCCESS) if (rc != SUUNTO_SUCCESS)
return rc; return rc;
#ifndef NDEBUG #ifndef NDEBUG
@ -328,7 +328,7 @@ suunto_vyper_read_memory (vyper *device, unsigned int address, unsigned char dat
data += len; data += len;
} }
return SUUNTO_VYPER_SUCCESS; return SUUNTO_SUCCESS;
} }
@ -336,7 +336,7 @@ int
suunto_vyper_write_memory (vyper *device, unsigned int address, const unsigned char data[], unsigned int size) suunto_vyper_write_memory (vyper *device, unsigned int address, const unsigned char data[], unsigned int size)
{ {
if (device == NULL) if (device == NULL)
return SUUNTO_VYPER_ERROR; return SUUNTO_ERROR;
// 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 +351,7 @@ suunto_vyper_write_memory (vyper *device, unsigned int address, const unsigned c
unsigned char pcommand[3] = {0x07, 0xA5, 0}; unsigned char pcommand[3] = {0x07, 0xA5, 0};
pcommand[2] = suunto_vyper_checksum (pcommand, 2, 0x00); pcommand[2] = suunto_vyper_checksum (pcommand, 2, 0x00);
int rc = suunto_vyper_transfer (device, pcommand, 3, pheader, 2, NULL, 0); int rc = suunto_vyper_transfer (device, pcommand, 3, pheader, 2, NULL, 0);
if (rc != SUUNTO_VYPER_SUCCESS) if (rc != SUUNTO_SUCCESS)
return rc; return rc;
#ifndef NDEBUG #ifndef NDEBUG
@ -368,7 +368,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, wheader, 4, NULL, 0); rc = suunto_vyper_transfer (device, wcommand, len + 5, wheader, 4, NULL, 0);
if (rc != SUUNTO_VYPER_SUCCESS) if (rc != SUUNTO_SUCCESS)
return rc; return rc;
#ifndef NDEBUG #ifndef NDEBUG
@ -384,7 +384,7 @@ suunto_vyper_write_memory (vyper *device, unsigned int address, const unsigned c
data += len; data += len;
} }
return SUUNTO_VYPER_SUCCESS; return SUUNTO_SUCCESS;
} }
@ -392,7 +392,7 @@ int
suunto_vyper_read_dive (vyper *device, unsigned char data[], unsigned int size, int init) suunto_vyper_read_dive (vyper *device, unsigned char data[], unsigned int size, int init)
{ {
if (device == NULL) if (device == NULL)
return SUUNTO_VYPER_ERROR; return SUUNTO_ERROR;
// Prepare the command. // Prepare the command.
unsigned char command[3] = {init ? 0x08 : 0x09, 0xA5, 0x00}; unsigned char command[3] = {init ? 0x08 : 0x09, 0xA5, 0x00};
@ -400,7 +400,7 @@ suunto_vyper_read_dive (vyper *device, unsigned char data[], unsigned int size,
// Send the command to the dive computer. // Send the command to the dive computer.
int rc = suunto_vyper_send (device, command, 3); int rc = suunto_vyper_send (device, command, 3);
if (rc != SUUNTO_VYPER_SUCCESS) { if (rc != SUUNTO_SUCCESS) {
WARNING ("Failed to send the command."); WARNING ("Failed to send the command.");
return rc; return rc;
} }
@ -462,7 +462,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_VYPER_ERROR_MEMORY; return SUUNTO_ERROR_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

View File

@ -10,13 +10,6 @@ typedef struct vyper vyper;
#define SUUNTO_VYPER_MEMORY_SIZE 0x2000 #define SUUNTO_VYPER_MEMORY_SIZE 0x2000
#define SUUNTO_VYPER_PACKET_SIZE 32 #define SUUNTO_VYPER_PACKET_SIZE 32
#define SUUNTO_VYPER_SUCCESS 0
#define SUUNTO_VYPER_ERROR -1
#define SUUNTO_VYPER_ERROR_IO -2
#define SUUNTO_VYPER_ERROR_MEMORY -3
#define SUUNTO_VYPER_ERROR_PROTOCOL -4
#define SUUNTO_VYPER_ERROR_TIMEOUT -5
int suunto_vyper_open (vyper **device, const char* name); int suunto_vyper_open (vyper **device, const char* name);
int suunto_vyper_close (vyper *device); int suunto_vyper_close (vyper *device);

View File

@ -11,14 +11,14 @@ int test_dump_sdm16 (const char* name, const char* filename)
printf ("suunto_vyper_open\n"); printf ("suunto_vyper_open\n");
int rc = suunto_vyper_open (&device, name); int rc = suunto_vyper_open (&device, name);
if (rc != SUUNTO_VYPER_SUCCESS) { if (rc != SUUNTO_SUCCESS) {
fprintf (stderr, "%s:%d: Error opening serial port.\n",__FILE__,__LINE__); fprintf (stderr, "%s:%d: Error opening serial port.\n",__FILE__,__LINE__);
return rc; return rc;
} }
printf ("suunto_vyper_detect_interface\n"); printf ("suunto_vyper_detect_interface\n");
rc = suunto_vyper_detect_interface (device); rc = suunto_vyper_detect_interface (device);
if (rc != SUUNTO_VYPER_SUCCESS) { if (rc != SUUNTO_SUCCESS) {
fprintf (stderr, "%s:%d: Interface not found.\n",__FILE__,__LINE__); fprintf (stderr, "%s:%d: Interface not found.\n",__FILE__,__LINE__);
suunto_vyper_close (device); suunto_vyper_close (device);
return rc; return rc;
@ -26,25 +26,25 @@ int test_dump_sdm16 (const char* name, const char* filename)
printf ("suunto_vyper_read_memory\n"); printf ("suunto_vyper_read_memory\n");
rc = suunto_vyper_read_memory (device, 0x24, data + 0x24, 1); rc = suunto_vyper_read_memory (device, 0x24, data + 0x24, 1);
if (rc != SUUNTO_VYPER_SUCCESS) { if (rc != SUUNTO_SUCCESS) {
fprintf (stderr, "%s:%d: Cannot identify computer.\n",__FILE__,__LINE__); fprintf (stderr, "%s:%d: Cannot identify computer.\n",__FILE__,__LINE__);
suunto_vyper_close (device); suunto_vyper_close (device);
return rc; return rc;
} }
rc = suunto_vyper_read_memory (device, 0x1E, data + 0x1E, 14); rc = suunto_vyper_read_memory (device, 0x1E, data + 0x1E, 14);
if (rc != SUUNTO_VYPER_SUCCESS) { if (rc != SUUNTO_SUCCESS) {
fprintf (stderr, "Cannot read memory.\n"); fprintf (stderr, "Cannot read memory.\n");
suunto_vyper_close (device); suunto_vyper_close (device);
return rc; return rc;
} }
rc = suunto_vyper_read_memory (device, 0x2C, data + 0x2C, 32); rc = suunto_vyper_read_memory (device, 0x2C, data + 0x2C, 32);
if (rc != SUUNTO_VYPER_SUCCESS) { if (rc != SUUNTO_SUCCESS) {
fprintf (stderr, "Cannot read memory.\n"); fprintf (stderr, "Cannot read memory.\n");
suunto_vyper_close (device); suunto_vyper_close (device);
return rc; return rc;
} }
rc = suunto_vyper_read_memory (device, 0x53, data + 0x53, 30); rc = suunto_vyper_read_memory (device, 0x53, data + 0x53, 30);
if (rc != SUUNTO_VYPER_SUCCESS) { if (rc != SUUNTO_SUCCESS) {
fprintf (stderr, "Cannot read memory.\n"); fprintf (stderr, "Cannot read memory.\n");
suunto_vyper_close (device); suunto_vyper_close (device);
return rc; return rc;
@ -75,12 +75,12 @@ int test_dump_sdm16 (const char* name, const char* filename)
printf ("suunto_vyper_close\n"); printf ("suunto_vyper_close\n");
rc = suunto_vyper_close (device); rc = suunto_vyper_close (device);
if (rc != SUUNTO_VYPER_SUCCESS) { if (rc != SUUNTO_SUCCESS) {
fprintf (stderr, "Cannot close device."); fprintf (stderr, "Cannot close device.");
return rc; return rc;
} }
return SUUNTO_VYPER_SUCCESS; return SUUNTO_SUCCESS;
} }
int test_dump_memory (const char* name, const char* filename) int test_dump_memory (const char* name, const char* filename)
@ -90,14 +90,14 @@ int test_dump_memory (const char* name, const char* filename)
printf ("suunto_vyper_open\n"); printf ("suunto_vyper_open\n");
int rc = suunto_vyper_open (&device, name); int rc = suunto_vyper_open (&device, name);
if (rc != SUUNTO_VYPER_SUCCESS) { if (rc != SUUNTO_SUCCESS) {
fprintf (stderr, "%s:%d: Error opening serial port.\n",__FILE__,__LINE__); fprintf (stderr, "%s:%d: Error opening serial port.\n",__FILE__,__LINE__);
return rc; return rc;
} }
printf ("suunto_vyper_detect_interface\n"); printf ("suunto_vyper_detect_interface\n");
rc = suunto_vyper_detect_interface (device); rc = suunto_vyper_detect_interface (device);
if (rc != SUUNTO_VYPER_SUCCESS) { if (rc != SUUNTO_SUCCESS) {
fprintf (stderr, "%s:%d: Interface not found.\n",__FILE__,__LINE__); fprintf (stderr, "%s:%d: Interface not found.\n",__FILE__,__LINE__);
suunto_vyper_close (device); suunto_vyper_close (device);
return rc; return rc;
@ -105,7 +105,7 @@ int test_dump_memory (const char* name, const char* filename)
printf ("suunto_vyper_read_memory\n"); printf ("suunto_vyper_read_memory\n");
rc = suunto_vyper_read_memory (device, 0x00, data, sizeof (data)); rc = suunto_vyper_read_memory (device, 0x00, data, sizeof (data));
if (rc != SUUNTO_VYPER_SUCCESS) { if (rc != SUUNTO_SUCCESS) {
fprintf (stderr, "Cannot read memory.\n"); fprintf (stderr, "Cannot read memory.\n");
suunto_vyper_close (device); suunto_vyper_close (device);
return rc; return rc;
@ -120,28 +120,28 @@ int test_dump_memory (const char* name, const char* filename)
printf ("suunto_vyper_close\n"); printf ("suunto_vyper_close\n");
rc = suunto_vyper_close (device); rc = suunto_vyper_close (device);
if (rc != SUUNTO_VYPER_SUCCESS) { if (rc != SUUNTO_SUCCESS) {
fprintf (stderr, "Cannot close device."); fprintf (stderr, "Cannot close device.");
return rc; return rc;
} }
return SUUNTO_VYPER_SUCCESS; return SUUNTO_SUCCESS;
} }
const char* errmsg (int rc) const char* errmsg (int rc)
{ {
switch (rc) { switch (rc) {
case SUUNTO_VYPER_SUCCESS: case SUUNTO_SUCCESS:
return "Success"; return "Success";
case SUUNTO_VYPER_ERROR: case SUUNTO_ERROR:
return "Generic error"; return "Generic error";
case SUUNTO_VYPER_ERROR_IO: case SUUNTO_ERROR_IO:
return "Input/output error"; return "Input/output error";
case SUUNTO_VYPER_ERROR_MEMORY: case SUUNTO_ERROR_MEMORY:
return "Memory error"; return "Memory error";
case SUUNTO_VYPER_ERROR_PROTOCOL: case SUUNTO_ERROR_PROTOCOL:
return "Protocol error"; return "Protocol error";
case SUUNTO_VYPER_ERROR_TIMEOUT: case SUUNTO_ERROR_TIMEOUT:
return "Timeout"; return "Timeout";
default: default:
return "Unknown error"; return "Unknown error";