333 lines
7.7 KiB
C
333 lines
7.7 KiB
C
#include <string.h> // memcmp, memcpy
|
|
#include <stdlib.h> // malloc, free
|
|
#include <assert.h> // assert
|
|
|
|
#include "suunto.h"
|
|
#include "serial.h"
|
|
#include "utils.h"
|
|
|
|
#define MAXRETRIES 2
|
|
|
|
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
|
|
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
|
|
|
|
#define WARNING(expr) \
|
|
{ \
|
|
message ("%s:%d: %s\n", __FILE__, __LINE__, expr); \
|
|
}
|
|
|
|
|
|
struct d9 {
|
|
struct serial *port;
|
|
};
|
|
|
|
|
|
int
|
|
suunto_d9_open (d9 **out, const char* name)
|
|
{
|
|
if (out == NULL)
|
|
return SUUNTO_ERROR;
|
|
|
|
// Allocate memory.
|
|
struct d9 *device = malloc (sizeof (struct d9));
|
|
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 (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 SUUNTO_ERROR_IO;
|
|
}
|
|
|
|
// Set the timeout for receiving data (3000 ms).
|
|
if (serial_set_timeout (device->port, 3000) == -1) {
|
|
WARNING ("Failed to set the timeout.");
|
|
serial_close (device->port);
|
|
free (device);
|
|
return SUUNTO_ERROR_IO;
|
|
}
|
|
|
|
// Set the DTR line (power supply for the interface).
|
|
if (serial_set_dtr (device->port, 1) == -1) {
|
|
WARNING ("Failed to set the DTR line.");
|
|
serial_close (device->port);
|
|
free (device);
|
|
return SUUNTO_ERROR_IO;
|
|
}
|
|
|
|
// Give the interface 100 ms to settle and draw power up.
|
|
serial_sleep (100);
|
|
|
|
// Make sure everything is in a sane state.
|
|
serial_flush (device->port, SERIAL_QUEUE_BOTH);
|
|
|
|
*out = device;
|
|
|
|
return SUUNTO_SUCCESS;
|
|
}
|
|
|
|
|
|
int
|
|
suunto_d9_close (d9 *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;
|
|
}
|
|
|
|
|
|
static unsigned char
|
|
suunto_d9_checksum (const unsigned char data[], unsigned int size, unsigned char init)
|
|
{
|
|
unsigned char crc = init;
|
|
for (unsigned int i = 0; i < size; ++i)
|
|
crc ^= data[i];
|
|
|
|
return crc;
|
|
}
|
|
|
|
|
|
static int
|
|
suunto_d9_send (d9 *device, const unsigned char command[], unsigned int csize)
|
|
{
|
|
// Clear RTS to send the command.
|
|
serial_set_rts (device->port, 0);
|
|
|
|
// Send the command to the dive computer and
|
|
// wait until all data has been transmitted.
|
|
serial_write (device->port, command, csize);
|
|
serial_drain (device->port);
|
|
|
|
// Receive the echo.
|
|
unsigned char echo[128] = {0};
|
|
assert (sizeof (echo) >= csize);
|
|
int rc = serial_read (device->port, echo, csize);
|
|
if (rc != csize) {
|
|
WARNING ("Failed to receive the echo.");
|
|
if (rc == -1)
|
|
return SUUNTO_ERROR_IO;
|
|
return SUUNTO_ERROR_TIMEOUT;
|
|
}
|
|
|
|
// Verify the echo.
|
|
if (memcmp (command, echo, csize) != 0) {
|
|
WARNING ("Unexpected echo.");
|
|
return SUUNTO_ERROR_PROTOCOL;
|
|
}
|
|
|
|
// Set RTS to receive the reply.
|
|
serial_set_rts (device->port, 1);
|
|
|
|
return SUUNTO_SUCCESS;
|
|
}
|
|
|
|
|
|
static int
|
|
suunto_d9_transfer (d9 *device, const unsigned char command[], unsigned int csize, unsigned char answer[], unsigned int asize, unsigned int size)
|
|
{
|
|
assert (asize >= size + 4);
|
|
|
|
// Occasionally, the dive computer does not respond to a command.
|
|
// In that case we retry the command a number of times before
|
|
// returning an error. Usually the dive computer will respond
|
|
// again during one of the retries.
|
|
|
|
for (unsigned int i = 0;; ++i) {
|
|
// Send the command to the dive computer.
|
|
int rc = suunto_d9_send (device, command, csize);
|
|
if (rc != SUUNTO_SUCCESS) {
|
|
WARNING ("Failed to send the command.");
|
|
return rc;
|
|
}
|
|
|
|
// Receive the answer of the dive computer.
|
|
rc = serial_read (device->port, answer, asize);
|
|
if (rc != asize) {
|
|
WARNING ("Failed to receive the answer.");
|
|
if (rc == -1)
|
|
return SUUNTO_ERROR_IO;
|
|
if (i < MAXRETRIES)
|
|
continue; // Retry.
|
|
return SUUNTO_ERROR_TIMEOUT;
|
|
}
|
|
|
|
// Verify the header of the package.
|
|
answer[2] -= size; // Adjust the package size for the comparision.
|
|
if (memcmp (command, answer, asize - size - 1) != 0) {
|
|
WARNING ("Unexpected answer start byte(s).");
|
|
return SUUNTO_ERROR_PROTOCOL;
|
|
}
|
|
answer[2] += size; // Restore the package size again.
|
|
|
|
// Verify the checksum of the package.
|
|
unsigned char crc = answer[asize - 1];
|
|
unsigned char ccrc = suunto_d9_checksum (answer, asize - 1, 0x00);
|
|
if (crc != ccrc) {
|
|
WARNING ("Unexpected answer CRC.");
|
|
return SUUNTO_ERROR_PROTOCOL;
|
|
}
|
|
|
|
return SUUNTO_SUCCESS;
|
|
}
|
|
}
|
|
|
|
|
|
int
|
|
suunto_d9_read_version (d9 *device, unsigned char data[], unsigned int size)
|
|
{
|
|
if (device == NULL)
|
|
return SUUNTO_ERROR;
|
|
|
|
if (size < 4)
|
|
return SUUNTO_ERROR_MEMORY;
|
|
|
|
unsigned char answer[4 + 4] = {0};
|
|
unsigned char command[4] = {0x0F, 0x00, 0x00, 0x0F};
|
|
int rc = suunto_d9_transfer (device, command, sizeof (command), answer, sizeof (answer), 4);
|
|
if (rc != SUUNTO_SUCCESS)
|
|
return rc;
|
|
|
|
memcpy (data, answer + 3, 4);
|
|
|
|
#ifndef NDEBUG
|
|
message ("D9ReadVersion()=\"%02x %02x %02x %02x\"\n", data[0], data[1], data[2], data[3]);
|
|
#endif
|
|
|
|
return SUUNTO_SUCCESS;
|
|
}
|
|
|
|
|
|
int
|
|
suunto_d9_reset_maxdepth (d9 *device)
|
|
{
|
|
if (device == NULL)
|
|
return SUUNTO_ERROR;
|
|
|
|
unsigned char answer[4] = {0};
|
|
unsigned char command[4] = {0x20, 0x00, 0x00, 0x20};
|
|
int rc = suunto_d9_transfer (device, command, sizeof (command), answer, sizeof (answer), 0);
|
|
if (rc != SUUNTO_SUCCESS)
|
|
return rc;
|
|
|
|
#ifndef NDEBUG
|
|
message ("D9ResetMaxDepth()\n");
|
|
#endif
|
|
|
|
return SUUNTO_SUCCESS;
|
|
}
|
|
|
|
|
|
int
|
|
suunto_d9_read_memory (d9 *device, unsigned int address, unsigned char data[], unsigned int size)
|
|
{
|
|
if (device == NULL)
|
|
return SUUNTO_ERROR;
|
|
|
|
// The data transmission is split in packages
|
|
// of maximum $SUUNTO_D9_PACKET_SIZE bytes.
|
|
|
|
unsigned int nbytes = 0;
|
|
while (nbytes < size) {
|
|
// Calculate the package size.
|
|
unsigned int len = MIN (size - nbytes, SUUNTO_D9_PACKET_SIZE);
|
|
|
|
// Read the package.
|
|
unsigned char answer[SUUNTO_D9_PACKET_SIZE + 7] = {0};
|
|
unsigned char command[7] = {0x05, 0x00, 0x03,
|
|
(address >> 8) & 0xFF, // high
|
|
(address ) & 0xFF, // low
|
|
len, // count
|
|
0}; // CRC
|
|
command[6] = suunto_d9_checksum (command, 6, 0x00);
|
|
int rc = suunto_d9_transfer (device, command, sizeof (command), answer, len + 7, len);
|
|
if (rc != SUUNTO_SUCCESS)
|
|
return rc;
|
|
|
|
memcpy (data, answer + 6, len);
|
|
|
|
#ifndef NDEBUG
|
|
message ("D9Read(0x%04x,%d)=\"", address, len);
|
|
for (unsigned int i = 0; i < len; ++i) {
|
|
message("%02x", data[i]);
|
|
}
|
|
message("\"\n");
|
|
#endif
|
|
|
|
nbytes += len;
|
|
address += len;
|
|
data += len;
|
|
}
|
|
|
|
return SUUNTO_SUCCESS;
|
|
}
|
|
|
|
|
|
int
|
|
suunto_d9_write_memory (d9 *device, unsigned int address, const unsigned char data[], unsigned int size)
|
|
{
|
|
if (device == NULL)
|
|
return SUUNTO_ERROR;
|
|
|
|
// The data transmission is split in packages
|
|
// of maximum $SUUNTO_D9_PACKET_SIZE bytes.
|
|
|
|
unsigned int nbytes = 0;
|
|
while (nbytes < size) {
|
|
// Calculate the package size.
|
|
unsigned int len = MIN (size - nbytes, SUUNTO_D9_PACKET_SIZE);
|
|
|
|
// Write the package.
|
|
unsigned char answer[7] = {0};
|
|
unsigned char command[SUUNTO_D9_PACKET_SIZE + 7] = {0x06, 0x00, 0x03,
|
|
(address >> 8) & 0xFF, // high
|
|
(address ) & 0xFF, // low
|
|
len, // count
|
|
0}; // data + CRC
|
|
memcpy (command + 6, data, len);
|
|
command[len + 6] = suunto_d9_checksum (command, len + 6, 0x00);
|
|
int rc = suunto_d9_transfer (device, command, len + 7, answer, sizeof (answer), 0);
|
|
if (rc != SUUNTO_SUCCESS)
|
|
return rc;
|
|
|
|
#ifndef NDEBUG
|
|
message ("D9Write(0x%04x,%d,\"", address, len);
|
|
for (unsigned int i = 0; i < len; ++i) {
|
|
message ("%02x", data[i]);
|
|
}
|
|
message ("\");\n");
|
|
#endif
|
|
|
|
nbytes += len;
|
|
address += len;
|
|
data += len;
|
|
}
|
|
|
|
return SUUNTO_SUCCESS;
|
|
}
|