#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "oceanic_vtpro.h"
#include "oceanic_common.h"
#include "context-private.h"
#include "device-private.h"
#include "ringbuffer.h"
#include "checksum.h"
#include "array.h"
#define ISINSTANCE(device) dc_device_isinstance((device), &oceanic_vtpro_device_vtable.base)
#define MAXRETRIES 2
#define MULTIPAGE 4
#define ACK 0x5A
#define NAK 0xA5
#define END 0x51
typedef enum oceanic_vtpro_protocol_t {
MOD,
INTR,
} oceanic_vtpro_protocol_t;
typedef struct oceanic_vtpro_device_t {
oceanic_common_device_t base;
dc_iostream_t *iostream;
oceanic_vtpro_protocol_t protocol;
} oceanic_vtpro_device_t;
static dc_status_t oceanic_vtpro_device_logbook (dc_device_t *abstract, dc_event_progress_t *progress, dc_buffer_t *logbook);
static dc_status_t oceanic_vtpro_device_read (dc_device_t *abstract, unsigned int address, unsigned char data[], unsigned int size);
static dc_status_t oceanic_vtpro_device_close (dc_device_t *abstract);
static const oceanic_common_device_vtable_t oceanic_vtpro_device_vtable = {
{
sizeof(oceanic_vtpro_device_t),
DC_FAMILY_OCEANIC_VTPRO,
oceanic_common_device_set_fingerprint,
oceanic_vtpro_device_read,
NULL,
oceanic_common_device_dump,
oceanic_common_device_foreach,
NULL,
oceanic_vtpro_device_close
},
oceanic_vtpro_device_logbook,
oceanic_common_device_profile,
};
static const oceanic_common_layout_t oceanic_vtpro_layout = {
0x8000,
0,
0x0000,
0x0040,
0x0240,
0x0440,
8,
0x0440,
0x8000,
0,
0,
0,
};
static const oceanic_common_layout_t oceanic_wisdom_layout = {
0x8000,
0,
0x0000,
0x0040,
0x03D0,
0x05D0,
8,
0x05D0,
0x8000,
0,
0,
0,
};
static const oceanic_common_layout_t aeris_500ai_layout = {
0x20000,
0,
0x0000,
0x0110,
0x0200,
0x0200,
8,
0x00200,
0x20000,
0,
1,
2,
};
static const oceanic_common_version_t versions[] = {
{"VERSAPRO \0\0 256K", 0, VERSAPRO, &oceanic_vtpro_layout},
{"ATMOSTWO \0\0 256K", 0, ATMOS2, &oceanic_vtpro_layout},
{"PROPLUS2 \0\0 256K", 0, PROPLUS2, &oceanic_vtpro_layout},
{"ATMOSAIR \0\0 256K", 0, ATMOSAI, &oceanic_vtpro_layout},
{"VTPRO r\0\0 256K", 0, VTPRO, &oceanic_vtpro_layout},
{"ELITE r\0\0 256K", 0, ELITE, &oceanic_vtpro_layout},
{"WISDOM r\0\0 256K", 0, WISDOM, &oceanic_wisdom_layout},
};
static dc_status_t
oceanic_vtpro_send (oceanic_vtpro_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;
if (device_is_cancelled (abstract))
return DC_STATUS_CANCELLED;
status = dc_iostream_write (device->iostream, command, csize, NULL);
if (status != DC_STATUS_SUCCESS) {
ERROR (abstract->context, "Failed to send the command.");
return status;
}
unsigned char response = NAK;
status = dc_iostream_read (device->iostream, &response, 1, NULL);
if (status != DC_STATUS_SUCCESS) {
ERROR (abstract->context, "Failed to receive the answer.");
return status;
}
if (response != ACK) {
ERROR (abstract->context, "Unexpected answer start byte(s).");
return DC_STATUS_PROTOCOL;
}
return DC_STATUS_SUCCESS;
}
static dc_status_t
oceanic_vtpro_transfer (oceanic_vtpro_device_t *device, const unsigned char command[], unsigned int csize, unsigned char answer[], unsigned int asize)
{
dc_status_t status = DC_STATUS_SUCCESS;
dc_device_t *abstract = (dc_device_t *) device;
unsigned int nretries = 0;
dc_status_t rc = DC_STATUS_SUCCESS;
while ((rc = oceanic_vtpro_send (device, command, csize)) != DC_STATUS_SUCCESS) {
if (rc != DC_STATUS_TIMEOUT && rc != DC_STATUS_PROTOCOL)
return rc;
if (nretries++ >= MAXRETRIES)
return rc;
}
if (asize) {
status = dc_iostream_read (device->iostream, answer, asize, NULL);
if (status != DC_STATUS_SUCCESS) {
ERROR (abstract->context, "Failed to receive the answer.");
return status;
}
}
return DC_STATUS_SUCCESS;
}
static dc_status_t
oceanic_vtpro_init (oceanic_vtpro_device_t *device)
{
dc_status_t status = DC_STATUS_SUCCESS;
dc_device_t *abstract = (dc_device_t *) device;
unsigned char command[2][2] = {
{0xAA, 0x00},
{0x20, 0x00}};
status = dc_iostream_write (device->iostream, command[device->protocol], sizeof (command[device->protocol]), NULL);
if (status != DC_STATUS_SUCCESS) {
ERROR (abstract->context, "Failed to send the command.");
return status;
}
unsigned char answer[13] = {0};
status = dc_iostream_read (device->iostream, answer, sizeof (answer), NULL);
if (status != DC_STATUS_SUCCESS) {
ERROR (abstract->context, "Failed to receive the answer.");
return status;
}
const unsigned char response[2][13] = {
{0x4D, 0x4F, 0x44, 0x2D, 0x2D, 0x4F, 0x4B, 0x5F, 0x56, 0x32, 0x2E, 0x30, 0x30},
{0x49, 0x4E, 0x54, 0x52, 0x2D, 0x4F, 0x4B, 0x5F, 0x56, 0x31, 0x2E, 0x31, 0x31}};
if (memcmp (answer, response[device->protocol], sizeof (response[device->protocol])) != 0) {
ERROR (abstract->context, "Unexpected answer byte(s).");
return DC_STATUS_PROTOCOL;
}
return DC_STATUS_SUCCESS;
}
static dc_status_t
oceanic_vtpro_quit (oceanic_vtpro_device_t *device)
{
dc_device_t *abstract = (dc_device_t *) device;
unsigned char answer[1] = {0};
unsigned char command[4] = {0x6A, 0x05, 0xA5, 0x00};
dc_status_t rc = oceanic_vtpro_transfer (device, command, sizeof (command), answer, sizeof (answer));
if (rc != DC_STATUS_SUCCESS)
return rc;
if (answer[0] != END) {
ERROR (abstract->context, "Unexpected answer byte(s).");
return DC_STATUS_PROTOCOL;
}
return DC_STATUS_SUCCESS;
}
static dc_status_t
oceanic_vtpro_calibrate (oceanic_vtpro_device_t *device)
{
dc_device_t *abstract = (dc_device_t *) device;
unsigned char answer[2] = {0};
unsigned char command[2] = {0x18, 0x00};
dc_status_t rc = dc_iostream_set_timeout (device->iostream, 9000);
if (rc != DC_STATUS_SUCCESS)
return rc;
rc = oceanic_vtpro_transfer (device, command, sizeof (command), answer, sizeof (answer));
if (rc != DC_STATUS_SUCCESS)
return rc;
rc = dc_iostream_set_timeout (device->iostream, 3000);
if (rc != DC_STATUS_SUCCESS)
return rc;
if (answer[1] != 0x00) {
ERROR (abstract->context, "Unexpected answer byte(s).");
return DC_STATUS_PROTOCOL;
}
return DC_STATUS_SUCCESS;
}
static dc_status_t
oceanic_aeris500ai_device_logbook (dc_device_t *abstract, dc_event_progress_t *progress, dc_buffer_t *logbook)
{
dc_status_t rc = DC_STATUS_SUCCESS;
oceanic_vtpro_device_t *device = (oceanic_vtpro_device_t *) abstract;
assert (device != NULL);
assert (device->base.layout != NULL);
assert (device->base.layout->rb_logbook_entry_size == PAGESIZE / 2);
assert (device->base.layout->rb_logbook_begin == device->base.layout->rb_logbook_end);
assert (progress != NULL);
const oceanic_common_layout_t *layout = device->base.layout;
if (!dc_buffer_clear (logbook))
return DC_STATUS_NOMEMORY;
unsigned char pointers[PAGESIZE] = {0};
rc = oceanic_vtpro_device_read (abstract, layout->cf_pointers, pointers, sizeof (pointers));
if (rc != DC_STATUS_SUCCESS) {
ERROR (abstract->context, "Failed to read the memory page.");
return rc;
}
unsigned int last = pointers[0x03];
progress->current += PAGESIZE;
progress->maximum += PAGESIZE + (last + 1) * PAGESIZE / 2;
device_event_emit (abstract, DC_EVENT_PROGRESS, progress);
if (!dc_buffer_reserve (logbook, (last + 1) * PAGESIZE / 2))
return DC_STATUS_NOMEMORY;
unsigned char command[] = {0x52,
(last >> 8) & 0xFF, (last ) & 0xFF, 0x00};
rc = oceanic_vtpro_transfer (device, command, sizeof (command), NULL, 0);
if (rc != DC_STATUS_SUCCESS) {
ERROR (abstract->context, "Failed to send the logbook index command.");
return rc;
}
for (unsigned int i = 0; i < last + 1; ++i) {
unsigned char answer[PAGESIZE / 2 + 1] = {0};
rc = dc_iostream_read (device->iostream, answer, sizeof(answer), NULL);
if (rc != DC_STATUS_SUCCESS) {
ERROR (abstract->context, "Failed to receive the answer.");
return rc;
}
unsigned char crc = answer[PAGESIZE / 2];
unsigned char ccrc = checksum_add_uint4 (answer, PAGESIZE / 2, 0x00);
if (crc != ccrc) {
ERROR (abstract->context, "Unexpected answer checksum.");
return DC_STATUS_PROTOCOL;
}
progress->current += PAGESIZE / 2;
device_event_emit (abstract, DC_EVENT_PROGRESS, progress);
if (array_isequal (answer, PAGESIZE / 2, 0xFF)) {
WARNING (abstract->context, "Uninitialized logbook entries detected!");
continue;
}
if (memcmp (answer, device->base.fingerprint, PAGESIZE / 2) == 0) {
dc_buffer_clear (logbook);
} else {
dc_buffer_append (logbook, answer, PAGESIZE / 2);
}
}
return DC_STATUS_SUCCESS;
}
static dc_status_t
oceanic_vtpro_device_logbook (dc_device_t *abstract, dc_event_progress_t *progress, dc_buffer_t *logbook)
{
oceanic_vtpro_device_t *device = (oceanic_vtpro_device_t *) abstract;
if (device->base.model == AERIS500AI) {
return oceanic_aeris500ai_device_logbook (abstract, progress, logbook);
} else {
return oceanic_common_device_logbook (abstract, progress, logbook);
}
}
dc_status_t
oceanic_vtpro_device_open (dc_device_t **out, dc_context_t *context, dc_iostream_t *iostream, unsigned int model)
{
dc_status_t status = DC_STATUS_SUCCESS;
oceanic_vtpro_device_t *device = NULL;
if (out == NULL)
return DC_STATUS_INVALIDARGS;
device = (oceanic_vtpro_device_t *) dc_device_allocate (context, &oceanic_vtpro_device_vtable.base);
if (device == NULL) {
ERROR (context, "Failed to allocate memory.");
return DC_STATUS_NOMEMORY;
}
oceanic_common_device_init (&device->base);
device->base.multipage = MULTIPAGE;
device->iostream = iostream;
if (model == AERIS500AI) {
device->protocol = INTR;
} else {
device->protocol = MOD;
}
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_free;
}
status = dc_iostream_set_timeout (device->iostream, 3000);
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;
}
status = dc_iostream_set_rts (device->iostream, 0);
if (status != DC_STATUS_SUCCESS) {
ERROR (context, "Failed to clear the RTS line.");
goto error_free;
}
dc_iostream_sleep (device->iostream, 100);
status = dc_iostream_set_rts (device->iostream, 1);
if (status != DC_STATUS_SUCCESS) {
ERROR (context, "Failed to set the RTS line.");
goto error_free;
}
dc_iostream_sleep (device->iostream, device->protocol == MOD ? 100 : 1000);
dc_iostream_purge (device->iostream, DC_DIRECTION_ALL);
status = oceanic_vtpro_init (device);
if (status != DC_STATUS_SUCCESS) {
goto error_free;
}
status = oceanic_vtpro_device_version ((dc_device_t *) device, device->base.version, sizeof (device->base.version));
if (status != DC_STATUS_SUCCESS) {
goto error_free;
}
status = oceanic_vtpro_calibrate (device);
if (status != DC_STATUS_SUCCESS) {
goto error_free;
}
if (model == AERIS500AI) {
device->base.layout = &aeris_500ai_layout;
device->base.model = AERIS500AI;
} else {
const oceanic_common_version_t * version = OCEANIC_COMMON_MATCH(device->base.version, versions, &device->base.firmware);
if (version == NULL) {
WARNING (context, "Unsupported device detected!");
device->base.layout = &oceanic_vtpro_layout;
device->base.model = 0;
} else {
device->base.layout = version->layout;
device->base.model = version->model;
}
}
*out = (dc_device_t*) device;
return DC_STATUS_SUCCESS;
error_free:
dc_device_deallocate ((dc_device_t *) device);
return status;
}
static dc_status_t
oceanic_vtpro_device_close (dc_device_t *abstract)
{
dc_status_t status = DC_STATUS_SUCCESS;
oceanic_vtpro_device_t *device = (oceanic_vtpro_device_t*) abstract;
dc_status_t rc = DC_STATUS_SUCCESS;
rc = oceanic_vtpro_quit (device);
if (rc != DC_STATUS_SUCCESS) {
dc_status_set_error(&status, rc);
}
return status;
}
dc_status_t
oceanic_vtpro_device_keepalive (dc_device_t *abstract)
{
oceanic_vtpro_device_t *device = (oceanic_vtpro_device_t*) abstract;
if (!ISINSTANCE (abstract))
return DC_STATUS_INVALIDARGS;
unsigned char answer[1] = {0};
unsigned char command[4] = {0x6A, 0x08, 0x00, 0x00};
dc_status_t rc = oceanic_vtpro_transfer (device, command, sizeof (command), answer, sizeof (answer));
if (rc != DC_STATUS_SUCCESS)
return rc;
if (answer[0] != END) {
ERROR (abstract->context, "Unexpected answer byte(s).");
return DC_STATUS_PROTOCOL;
}
return DC_STATUS_SUCCESS;
}
dc_status_t
oceanic_vtpro_device_version (dc_device_t *abstract, unsigned char data[], unsigned int size)
{
oceanic_vtpro_device_t *device = (oceanic_vtpro_device_t*) abstract;
if (!ISINSTANCE (abstract))
return DC_STATUS_INVALIDARGS;
if (size < PAGESIZE)
return DC_STATUS_INVALIDARGS;
unsigned char cmd[2] = {0x88, 0x00};
unsigned char ans[PAGESIZE / 2 + 1] = {0};
dc_status_t rc = oceanic_vtpro_transfer (device, cmd, sizeof (cmd), ans, sizeof (ans));
if (rc != DC_STATUS_SUCCESS)
return rc;
unsigned char crc = ans[PAGESIZE / 2];
unsigned char ccrc = checksum_add_uint4 (ans, PAGESIZE / 2, 0x00);
if (crc != ccrc) {
ERROR (abstract->context, "Unexpected answer checksum.");
return DC_STATUS_PROTOCOL;
}
if (device->protocol == MOD) {
for (unsigned int i = 0; i < 2; ++i) {
unsigned char command[4] = {0x72, 0x03, i * 0x10, 0x00};
unsigned char answer[PAGESIZE / 2 + 2] = {0};
rc = oceanic_vtpro_transfer (device, command, sizeof (command), answer, sizeof (answer));
if (rc != DC_STATUS_SUCCESS)
return rc;
crc = answer[PAGESIZE / 2];
ccrc = checksum_add_uint4 (answer, PAGESIZE / 2, 0x00);
if (crc != ccrc) {
ERROR (abstract->context, "Unexpected answer checksum.");
return DC_STATUS_PROTOCOL;
}
if (answer[PAGESIZE / 2 + 1] != END) {
ERROR (abstract->context, "Unexpected answer byte.");
return DC_STATUS_PROTOCOL;
}
memcpy (data + i * PAGESIZE / 2, answer, PAGESIZE / 2);
}
} else {
memset (data, 0x00, PAGESIZE);
}
return DC_STATUS_SUCCESS;
}
static dc_status_t
oceanic_vtpro_device_read (dc_device_t *abstract, unsigned int address, unsigned char data[], unsigned int size)
{
oceanic_vtpro_device_t *device = (oceanic_vtpro_device_t*) abstract;
if ((address % PAGESIZE != 0) ||
(size % PAGESIZE != 0))
return DC_STATUS_INVALIDARGS;
unsigned int nbytes = 0;
while (nbytes < size) {
unsigned int npackets = (size - nbytes) / PAGESIZE;
if (npackets > MULTIPAGE)
npackets = MULTIPAGE;
unsigned int first = address / PAGESIZE;
unsigned int last = first + npackets - 1;
unsigned char answer[(PAGESIZE + 1) * MULTIPAGE] = {0};
unsigned char command[6] = {0x34,
(first >> 8) & 0xFF, (first ) & 0xFF, (last >> 8) & 0xFF, (last ) & 0xFF, 0x00};
dc_status_t rc = oceanic_vtpro_transfer (device, command, sizeof (command), answer, (PAGESIZE + 1) * npackets);
if (rc != DC_STATUS_SUCCESS)
return rc;
unsigned int offset = 0;
for (unsigned int i = 0; i < npackets; ++i) {
unsigned char crc = answer[offset + PAGESIZE];
unsigned char ccrc = checksum_add_uint8 (answer + offset, PAGESIZE, 0x00);
if (crc != ccrc) {
ERROR (abstract->context, "Unexpected answer checksum.");
return DC_STATUS_PROTOCOL;
}
memcpy (data, answer + offset, PAGESIZE);
offset += PAGESIZE + 1;
nbytes += PAGESIZE;
address += PAGESIZE;
data += PAGESIZE;
}
}
return DC_STATUS_SUCCESS;
}