#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "suunto_vyper.h"
#include "suunto_common.h"
#include "context-private.h"
#include "device-private.h"
#include "checksum.h"
#include "array.h"
#define ISINSTANCE(device) dc_device_isinstance((device), &suunto_vyper_device_vtable)
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
#define SZ_MEMORY 0x2000
#define SZ_PACKET 32
#define HDR_DEVINFO_VYPER 0x24
#define HDR_DEVINFO_SPYDER 0x16
#define HDR_DEVINFO_BEGIN (HDR_DEVINFO_SPYDER)
#define HDR_DEVINFO_END (HDR_DEVINFO_VYPER + 6)
typedef struct suunto_vyper_device_t {
suunto_common_device_t base;
dc_iostream_t *iostream;
} suunto_vyper_device_t;
static dc_status_t suunto_vyper_device_read (dc_device_t *abstract, unsigned int address, unsigned char data[], unsigned int size);
static dc_status_t suunto_vyper_device_write (dc_device_t *abstract, unsigned int address, const unsigned char data[], unsigned int size);
static dc_status_t suunto_vyper_device_dump (dc_device_t *abstract, dc_buffer_t *buffer);
static dc_status_t suunto_vyper_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata);
static const dc_device_vtable_t suunto_vyper_device_vtable = {
sizeof(suunto_vyper_device_t),
DC_FAMILY_SUUNTO_VYPER,
suunto_common_device_set_fingerprint,
suunto_vyper_device_read,
suunto_vyper_device_write,
suunto_vyper_device_dump,
suunto_vyper_device_foreach,
NULL,
NULL
};
static const suunto_common_layout_t suunto_vyper_layout = {
0x51,
0x71,
SZ_MEMORY,
9,
5
};
static const suunto_common_layout_t suunto_spyder_layout = {
0x1C,
0x4C,
SZ_MEMORY,
6,
3
};
dc_status_t
suunto_vyper_device_open (dc_device_t **out, dc_context_t *context, dc_iostream_t *iostream)
{
dc_status_t status = DC_STATUS_SUCCESS;
suunto_vyper_device_t *device = NULL;
if (out == NULL)
return DC_STATUS_INVALIDARGS;
device = (suunto_vyper_device_t *) dc_device_allocate (context, &suunto_vyper_device_vtable);
if (device == NULL) {
ERROR (context, "Failed to allocate memory.");
return DC_STATUS_NOMEMORY;
}
suunto_common_device_init (&device->base);
device->iostream = iostream;
status = dc_iostream_configure (device->iostream, 2400, 8, DC_PARITY_ODD, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
if (status != DC_STATUS_SUCCESS) {
ERROR (context, "Failed to set the terminal attributes.");
goto error_free;
}
status = dc_iostream_set_timeout (device->iostream, 1000);
if (status != DC_STATUS_SUCCESS) {
ERROR (context, "Failed to set the timeout.");
goto error_free;
}
status = dc_iostream_set_dtr (device->iostream, 1);
if (status != DC_STATUS_SUCCESS) {
ERROR (context, "Failed to set the DTR line.");
goto error_free;
}
dc_iostream_sleep (device->iostream, 100);
dc_iostream_purge (device->iostream, DC_DIRECTION_ALL);
*out = (dc_device_t*) device;
return DC_STATUS_SUCCESS;
error_free:
dc_device_deallocate ((dc_device_t *) device);
return status;
}
static dc_status_t
suunto_vyper_send (suunto_vyper_device_t *device, const unsigned char command[], unsigned int csize)
{
dc_status_t status = DC_STATUS_SUCCESS;
dc_device_t *abstract = (dc_device_t *) device;
dc_iostream_sleep (device->iostream, 500);
status = dc_iostream_set_rts (device->iostream, 1);
if (status != DC_STATUS_SUCCESS) {
ERROR (abstract->context, "Failed to set RTS.");
return status;
}
status = dc_iostream_write (device->iostream, command, csize, NULL);
if (status != DC_STATUS_SUCCESS) {
ERROR (abstract->context, "Failed to send the command.");
return status;
}
dc_iostream_sleep (device->iostream, 200);
dc_iostream_purge (device->iostream, DC_DIRECTION_INPUT);
status = dc_iostream_set_rts (device->iostream, 0);
if (status != DC_STATUS_SUCCESS) {
ERROR (abstract->context, "Failed to clear RTS.");
return status;
}
return DC_STATUS_SUCCESS;
}
static dc_status_t
suunto_vyper_transfer (suunto_vyper_device_t *device, const unsigned char command[], unsigned int csize, unsigned char answer[], unsigned int asize, unsigned int size)
{
dc_status_t status = DC_STATUS_SUCCESS;
dc_device_t *abstract = (dc_device_t *) device;
assert (asize >= size + 2);
if (device_is_cancelled (abstract))
return DC_STATUS_CANCELLED;
dc_status_t rc = suunto_vyper_send (device, command, csize);
if (rc != DC_STATUS_SUCCESS) {
ERROR (abstract->context, "Failed to send the command.");
return rc;
}
status = dc_iostream_read (device->iostream, answer, asize, NULL);
if (status != DC_STATUS_SUCCESS) {
ERROR (abstract->context, "Failed to receive the answer.");
return status;
}
if (memcmp (command, answer, asize - size - 1) != 0) {
ERROR (abstract->context, "Unexpected answer start byte(s).");
return DC_STATUS_PROTOCOL;
}
unsigned char crc = answer[asize - 1];
unsigned char ccrc = checksum_xor_uint8 (answer, asize - 1, 0x00);
if (crc != ccrc) {
ERROR (abstract->context, "Unexpected answer checksum.");
return DC_STATUS_PROTOCOL;
}
return DC_STATUS_SUCCESS;
}
static dc_status_t
suunto_vyper_device_read (dc_device_t *abstract, unsigned int address, unsigned char data[], unsigned int size)
{
suunto_vyper_device_t *device = (suunto_vyper_device_t*) abstract;
unsigned int nbytes = 0;
while (nbytes < size) {
unsigned int len = MIN (size - nbytes, SZ_PACKET);
unsigned char answer[SZ_PACKET + 5] = {0};
unsigned char command[5] = {0x05,
(address >> 8) & 0xFF, (address ) & 0xFF, len, 0}; command[4] = checksum_xor_uint8 (command, 4, 0x00);
dc_status_t rc = suunto_vyper_transfer (device, command, sizeof (command), answer, len + 5, len);
if (rc != DC_STATUS_SUCCESS)
return rc;
memcpy (data, answer + 4, len);
nbytes += len;
address += len;
data += len;
}
return DC_STATUS_SUCCESS;
}
static dc_status_t
suunto_vyper_device_write (dc_device_t *abstract, unsigned int address, const unsigned char data[], unsigned int size)
{
suunto_vyper_device_t *device = (suunto_vyper_device_t*) abstract;
unsigned int nbytes = 0;
while (nbytes < size) {
unsigned int len = MIN (size - nbytes, SZ_PACKET);
unsigned char panswer[3] = {0};
unsigned char pcommand[3] = {0x07, 0xA5, 0xA2};
dc_status_t rc = suunto_vyper_transfer (device, pcommand, sizeof (pcommand), panswer, sizeof (panswer), 0);
if (rc != DC_STATUS_SUCCESS)
return rc;
unsigned char wanswer[5] = {0};
unsigned char wcommand[SZ_PACKET + 5] = {0x06,
(address >> 8) & 0xFF, (address ) & 0xFF, len, 0}; memcpy (wcommand + 4, data, len);
wcommand[len + 4] = checksum_xor_uint8 (wcommand, len + 4, 0x00);
rc = suunto_vyper_transfer (device, wcommand, len + 5, wanswer, sizeof (wanswer), 0);
if (rc != DC_STATUS_SUCCESS)
return rc;
nbytes += len;
address += len;
data += len;
}
return DC_STATUS_SUCCESS;
}
static dc_status_t
suunto_vyper_read_dive (dc_device_t *abstract, dc_buffer_t *buffer, int init, dc_event_progress_t *progress)
{
dc_status_t status = DC_STATUS_SUCCESS;
suunto_vyper_device_t *device = (suunto_vyper_device_t*) abstract;
if (device_is_cancelled (abstract))
return DC_STATUS_CANCELLED;
if (!dc_buffer_clear (buffer)) {
ERROR (abstract->context, "Insufficient buffer space available.");
return DC_STATUS_NOMEMORY;
}
unsigned char command[3] = {init ? 0x08 : 0x09, 0xA5, 0x00};
command[2] = checksum_xor_uint8 (command, 2, 0x00);
dc_status_t rc = suunto_vyper_send (device, command, 3);
if (rc != DC_STATUS_SUCCESS) {
ERROR (abstract->context, "Failed to send the command.");
return rc;
}
unsigned int nbytes = 0;
for (unsigned int npackages = 0;; ++npackages) {
size_t n = 0;
unsigned char answer[SZ_PACKET + 3] = {0};
status = dc_iostream_read (device->iostream, answer, 2, &n);
if (status != DC_STATUS_SUCCESS) {
if (n == 0 && npackages != 0)
break;
ERROR (abstract->context, "Failed to receive the answer.");
return status;
}
if (answer[0] != command[0] ||
answer[1] > SZ_PACKET) {
ERROR (abstract->context, "Unexpected answer start byte(s).");
return DC_STATUS_PROTOCOL;
}
unsigned char len = answer[1];
status = dc_iostream_read (device->iostream, answer + 2, len + 1, NULL);
if (status != DC_STATUS_SUCCESS) {
ERROR (abstract->context, "Failed to receive the answer.");
return status;
}
unsigned char crc = answer[len + 2];
unsigned char ccrc = checksum_xor_uint8 (answer, len + 2, 0x00);
if (crc != ccrc) {
ERROR (abstract->context, "Unexpected answer checksum.");
return DC_STATUS_PROTOCOL;
}
if (len == 0) {
dc_buffer_clear (buffer);
return DC_STATUS_SUCCESS;
}
if (progress) {
progress->current += len;
if (progress->current > progress->maximum)
progress->current = progress->maximum;
device_event_emit (abstract, DC_EVENT_PROGRESS, progress);
}
dc_buffer_append (buffer, answer + 2, len);
nbytes += len;
#if 0#endif
}
if (dc_buffer_get_size (buffer) != nbytes) {
ERROR (abstract->context, "Insufficient buffer space available.");
return DC_STATUS_NOMEMORY;
}
array_reverse_bytes (dc_buffer_get_data (buffer), dc_buffer_get_size (buffer));
return DC_STATUS_SUCCESS;
}
static dc_status_t
suunto_vyper_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
{
dc_status_t status = DC_STATUS_SUCCESS;
if (!dc_buffer_resize (buffer, SZ_MEMORY)) {
ERROR (abstract->context, "Insufficient buffer space available.");
return DC_STATUS_NOMEMORY;
}
status = device_dump_read (abstract, 0, dc_buffer_get_data (buffer),
dc_buffer_get_size (buffer), SZ_PACKET);
if (status != DC_STATUS_SUCCESS) {
return status;
}
unsigned char *data = dc_buffer_get_data (buffer);
unsigned int hoffset = HDR_DEVINFO_VYPER;
if (data[hoffset] == 20 || data[hoffset] == 30 || data[hoffset] == 60) {
hoffset = HDR_DEVINFO_SPYDER;
}
dc_event_devinfo_t devinfo;
devinfo.model = data[hoffset + 0];
devinfo.firmware = data[hoffset + 1];
devinfo.serial = 0;
devinfo.serial = array_convert_bin2dec (data + hoffset + 2, 4);
device_event_emit (abstract, DC_EVENT_DEVINFO, &devinfo);
return status;
}
static dc_status_t
suunto_vyper_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata)
{
suunto_common_device_t *device = (suunto_common_device_t*) abstract;
dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER;
progress.maximum = SZ_MEMORY;
device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);
unsigned char header[HDR_DEVINFO_END - HDR_DEVINFO_BEGIN] = {0};
dc_status_t rc = suunto_vyper_device_read (abstract, HDR_DEVINFO_BEGIN, header, sizeof (header));
if (rc != DC_STATUS_SUCCESS)
return rc;
unsigned int hoffset = HDR_DEVINFO_VYPER - HDR_DEVINFO_BEGIN;
const suunto_common_layout_t *layout = &suunto_vyper_layout;
if (header[hoffset] == 20 || header[hoffset] == 30 || header[hoffset] == 60) {
hoffset = HDR_DEVINFO_SPYDER - HDR_DEVINFO_BEGIN;
layout = &suunto_spyder_layout;
}
progress.maximum = sizeof (header) +
(layout->rb_profile_end - layout->rb_profile_begin);
progress.current += sizeof (header);
device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);
dc_event_devinfo_t devinfo;
devinfo.model = header[hoffset + 0];
devinfo.firmware = header[hoffset + 1];
devinfo.serial = array_convert_bin2dec (header + hoffset + 2, 4);
device_event_emit (abstract, DC_EVENT_DEVINFO, &devinfo);
dc_buffer_t *buffer = dc_buffer_new (layout->rb_profile_end - layout->rb_profile_begin);
if (buffer == NULL)
return DC_STATUS_NOMEMORY;
unsigned int ndives = 0;
unsigned int remaining = layout->rb_profile_end - layout->rb_profile_begin;
while ((rc = suunto_vyper_read_dive (abstract, buffer, (ndives == 0), &progress)) == DC_STATUS_SUCCESS) {
unsigned char *data = dc_buffer_get_data (buffer);
unsigned int size = dc_buffer_get_size (buffer);
if (size > remaining) {
ERROR (abstract->context, "Unexpected number of bytes received.");
dc_buffer_free (buffer);
return DC_STATUS_DATAFORMAT;
}
if (size == 0) {
dc_buffer_free (buffer);
return DC_STATUS_SUCCESS;
}
if (memcmp (data + layout->fp_offset, device->fingerprint, sizeof (device->fingerprint)) == 0) {
dc_buffer_free (buffer);
return DC_STATUS_SUCCESS;
}
if (callback && !callback (data, size, data + layout->fp_offset, sizeof (device->fingerprint), userdata)) {
dc_buffer_free (buffer);
return DC_STATUS_SUCCESS;
}
remaining -= size;
ndives++;
}
dc_buffer_free (buffer);
return rc;
}