#include <string.h>
#include <stdlib.h>
#include "suunto_vyper2.h"
#include "suunto_common2.h"
#include "context-private.h"
#include "checksum.h"
#include "array.h"
#include "timer.h"
#define ISINSTANCE(device) dc_device_isinstance((device), (const dc_device_vtable_t *) &suunto_vyper2_device_vtable)
#define HELO2 0x15
typedef struct suunto_vyper2_device_t {
suunto_common2_device_t base;
dc_iostream_t *iostream;
dc_timer_t *timer;
} suunto_vyper2_device_t;
static dc_status_t suunto_vyper2_device_packet (dc_device_t *abstract, const unsigned char command[], unsigned int csize, unsigned char answer[], unsigned int asize, unsigned int size);
static dc_status_t suunto_vyper2_device_close (dc_device_t *abstract);
static const suunto_common2_device_vtable_t suunto_vyper2_device_vtable = {
{
sizeof(suunto_vyper2_device_t),
DC_FAMILY_SUUNTO_VYPER2,
suunto_common2_device_set_fingerprint,
suunto_common2_device_read,
suunto_common2_device_write,
suunto_common2_device_dump,
suunto_common2_device_foreach,
NULL,
suunto_vyper2_device_close
},
suunto_vyper2_device_packet
};
static const suunto_common2_layout_t suunto_vyper2_layout = {
0x8000,
0x0011,
0x0023,
0x019A,
0x7FFE
};
static const suunto_common2_layout_t suunto_helo2_layout = {
0x8000,
0x0017,
0x0023,
0x019A,
0x7FFE
};
dc_status_t
suunto_vyper2_device_open (dc_device_t **out, dc_context_t *context, dc_iostream_t *iostream)
{
dc_status_t status = DC_STATUS_SUCCESS;
suunto_vyper2_device_t *device = NULL;
if (out == NULL)
return DC_STATUS_INVALIDARGS;
device = (suunto_vyper2_device_t *) dc_device_allocate (context, &suunto_vyper2_device_vtable.base);
if (device == NULL) {
ERROR (context, "Failed to allocate memory.");
return DC_STATUS_NOMEMORY;
}
suunto_common2_device_init (&device->base);
device->iostream = iostream;
status = dc_timer_new (&device->timer);
if (status != DC_STATUS_SUCCESS) {
ERROR (context, "Failed to create a high resolution timer.");
goto error_free;
}
status = dc_iostream_configure (device->iostream, 9600, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
if (status != DC_STATUS_SUCCESS) {
ERROR (context, "Failed to set the terminal attributes.");
goto error_timer_free;
}
status = dc_iostream_set_timeout (device->iostream, 3000);
if (status != DC_STATUS_SUCCESS) {
ERROR (context, "Failed to set the timeout.");
goto error_timer_free;
}
status = dc_iostream_set_dtr (device->iostream, 1);
if (status != DC_STATUS_SUCCESS) {
ERROR (context, "Failed to set the DTR line.");
goto error_timer_free;
}
dc_iostream_sleep (device->iostream, 100);
status = dc_iostream_purge (device->iostream, DC_DIRECTION_ALL);
if (status != DC_STATUS_SUCCESS) {
ERROR (context, "Failed to reset IO state.");
goto error_timer_free;
}
status = suunto_common2_device_version ((dc_device_t *) device, device->base.version, sizeof (device->base.version));
if (status != DC_STATUS_SUCCESS) {
ERROR (context, "Failed to read the version info.");
goto error_timer_free;
}
unsigned int model = device->base.version[0];
if (model == HELO2)
device->base.layout = &suunto_helo2_layout;
else
device->base.layout = &suunto_vyper2_layout;
*out = (dc_device_t*) device;
return DC_STATUS_SUCCESS;
error_timer_free:
dc_timer_free (device->timer);
error_free:
dc_device_deallocate ((dc_device_t *) device);
return status;
}
static dc_status_t
suunto_vyper2_device_close (dc_device_t *abstract)
{
suunto_vyper2_device_t *device = (suunto_vyper2_device_t*) abstract;
dc_timer_free (device->timer);
return DC_STATUS_SUCCESS;
}
static dc_status_t
suunto_vyper2_device_packet (dc_device_t *abstract, const unsigned char command[], unsigned int csize, unsigned char answer[], unsigned int asize, unsigned int size)
{
dc_status_t status = DC_STATUS_SUCCESS;
suunto_vyper2_device_t *device = (suunto_vyper2_device_t *) abstract;
if (device_is_cancelled (abstract))
return DC_STATUS_CANCELLED;
dc_iostream_sleep (device->iostream, 600);
status = dc_iostream_set_rts (device->iostream, 1);
if (status != DC_STATUS_SUCCESS) {
ERROR (abstract->context, "Failed to set the RTS line.");
return status;
}
dc_usecs_t begin = 0;
status = dc_timer_now (device->timer, &begin);
if (status != DC_STATUS_SUCCESS) {
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_usecs_t end = 0;
status = dc_timer_now (device->timer, &end);
if (status != DC_STATUS_SUCCESS) {
return status;
}
dc_usecs_t elapsed = end - begin;
unsigned int baudrate = 9600;
unsigned int nbits = 1 + 8 + 1 + 0 ;
dc_usecs_t expected = (dc_usecs_t) csize * 1000000 * nbits / baudrate + 2000;
if (elapsed < expected) {
dc_usecs_t remaining = expected - elapsed;
dc_iostream_sleep (device->iostream, (remaining + 999) / 1000);
}
status = dc_iostream_set_rts (device->iostream, 0);
if (status != DC_STATUS_SUCCESS) {
ERROR (abstract->context, "Failed to set the RTS line.");
return status;
}
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 (answer[0] != command[0]) {
ERROR (abstract->context, "Unexpected answer header.");
return DC_STATUS_PROTOCOL;
}
unsigned int len = array_uint16_be (answer + 1);
if (len + 4 != asize) {
ERROR (abstract->context, "Unexpected answer size.");
return DC_STATUS_PROTOCOL;
}
if (memcmp (command + 3, answer + 3, asize - size - 4) != 0) {
ERROR (abstract->context, "Unexpected answer parameters.");
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;
}
dc_status_t
suunto_vyper2_device_version (dc_device_t *abstract, unsigned char data[], unsigned int size)
{
if (!ISINSTANCE (abstract))
return DC_STATUS_INVALIDARGS;
return suunto_common2_device_version (abstract, data, size);
}
dc_status_t
suunto_vyper2_device_reset_maxdepth (dc_device_t *abstract)
{
if (!ISINSTANCE (abstract))
return DC_STATUS_INVALIDARGS;
return suunto_common2_device_reset_maxdepth (abstract);
}