#include <string.h>
#include <stdlib.h>
#include "oceanic_veo250.h"
#include "oceanic_common.h"
#include "context-private.h"
#include "device-private.h"
#include "ringbuffer.h"
#include "checksum.h"
#define ISINSTANCE(device) dc_device_isinstance((device), &oceanic_veo250_device_vtable.base)
#define MAXRETRIES 2
#define MULTIPAGE 4
#define ACK 0x5A
#define NAK 0xA5
typedef struct oceanic_veo250_device_t {
oceanic_common_device_t base;
dc_iostream_t *iostream;
unsigned int last;
} oceanic_veo250_device_t;
static dc_status_t oceanic_veo250_device_read (dc_device_t *abstract, unsigned int address, unsigned char data[], unsigned int size);
static dc_status_t oceanic_veo250_device_close (dc_device_t *abstract);
static const oceanic_common_device_vtable_t oceanic_veo250_device_vtable = {
{
sizeof(oceanic_veo250_device_t),
DC_FAMILY_OCEANIC_VEO250,
oceanic_common_device_set_fingerprint,
oceanic_veo250_device_read,
NULL,
oceanic_common_device_dump,
oceanic_common_device_foreach,
NULL,
oceanic_veo250_device_close
},
oceanic_common_device_logbook,
oceanic_common_device_profile,
};
static const oceanic_common_layout_t oceanic_veo250_layout = {
0x8000,
0,
0x0000,
0x0040,
0x0400,
0x0600,
8,
0x0600,
0x8000,
1,
1,
1,
};
static const oceanic_common_version_t versions[] = {
{"GENREACT \0\0 256K", 0, REACTPRO, &oceanic_veo250_layout},
{"VEO 200 R\0\0 256K", 0, VEO200, &oceanic_veo250_layout},
{"VEO 250 R\0\0 256K", 0, VEO250, &oceanic_veo250_layout},
{"SEEMANN R\0\0 256K", 0, XP5, &oceanic_veo250_layout},
{"VEO 180 R\0\0 256K", 0, VEO180, &oceanic_veo250_layout},
{"AERISXR2 \0\0 256K", 0, XR2, &oceanic_veo250_layout},
{"INSIGHT R\0\0 256K", 0, INSIGHT, &oceanic_veo250_layout},
{"HO DGO2 R\0\0 256K", 0, DG02, &oceanic_veo250_layout},
};
static dc_status_t
oceanic_veo250_send (oceanic_veo250_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;
dc_iostream_purge (device->iostream, DC_DIRECTION_INPUT);
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_veo250_transfer (oceanic_veo250_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_veo250_send (device, command, csize)) != DC_STATUS_SUCCESS) {
if (rc != DC_STATUS_TIMEOUT && rc != DC_STATUS_PROTOCOL)
return rc;
if (nretries++ >= MAXRETRIES)
return rc;
dc_iostream_sleep (device->iostream, 100);
}
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[asize - 1] != NAK) {
ERROR (abstract->context, "Unexpected answer byte.");
return DC_STATUS_PROTOCOL;
}
return DC_STATUS_SUCCESS;
}
static dc_status_t
oceanic_veo250_init (oceanic_veo250_device_t *device)
{
dc_status_t status = DC_STATUS_SUCCESS;
dc_device_t *abstract = (dc_device_t *) device;
unsigned char command[2] = {0x55, 0x00};
status = dc_iostream_write (device->iostream, command, sizeof (command), NULL);
if (status != DC_STATUS_SUCCESS) {
ERROR (abstract->context, "Failed to send the command.");
return status;
}
size_t n = 0;
unsigned char answer[13] = {0};
status = dc_iostream_read (device->iostream, answer, sizeof (answer), &n);
if (status != DC_STATUS_SUCCESS) {
ERROR (abstract->context, "Failed to receive the answer.");
if (n == 0)
return DC_STATUS_SUCCESS;
return status;
}
const unsigned char response[13] = {
0x50, 0x50, 0x53, 0x2D, 0x2D, 0x4F, 0x4B,
0x5F, 0x56, 0x32, 0x2E, 0x30, 0x30};
if (memcmp (answer, response, sizeof (response)) != 0) {
ERROR (abstract->context, "Unexpected answer byte(s).");
return DC_STATUS_PROTOCOL;
}
return DC_STATUS_SUCCESS;
}
static dc_status_t
oceanic_veo250_quit (oceanic_veo250_device_t *device)
{
dc_status_t status = DC_STATUS_SUCCESS;
dc_device_t *abstract = (dc_device_t *) device;
unsigned char command[2] = {0x98, 0x00};
status = dc_iostream_write (device->iostream, command, sizeof (command), NULL);
if (status != DC_STATUS_SUCCESS) {
ERROR (abstract->context, "Failed to send the command.");
return status;
}
return DC_STATUS_SUCCESS;
}
dc_status_t
oceanic_veo250_device_open (dc_device_t **out, dc_context_t *context, dc_iostream_t *iostream)
{
dc_status_t status = DC_STATUS_SUCCESS;
oceanic_veo250_device_t *device = NULL;
if (out == NULL)
return DC_STATUS_INVALIDARGS;
device = (oceanic_veo250_device_t *) dc_device_allocate (context, &oceanic_veo250_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;
device->last = 0;
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, 100);
dc_iostream_purge (device->iostream, DC_DIRECTION_ALL);
status = oceanic_veo250_init (device);
if (status != DC_STATUS_SUCCESS) {
goto error_free;
}
dc_iostream_sleep (device->iostream, 100);
status = oceanic_veo250_device_version ((dc_device_t *) device, device->base.version, sizeof (device->base.version));
if (status != DC_STATUS_SUCCESS) {
goto error_free;
}
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_veo250_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_veo250_device_close (dc_device_t *abstract)
{
dc_status_t status = DC_STATUS_SUCCESS;
oceanic_veo250_device_t *device = (oceanic_veo250_device_t*) abstract;
dc_status_t rc = DC_STATUS_SUCCESS;
rc = oceanic_veo250_quit (device);
if (rc != DC_STATUS_SUCCESS) {
dc_status_set_error(&status, rc);
}
return status;
}
dc_status_t
oceanic_veo250_device_keepalive (dc_device_t *abstract)
{
oceanic_veo250_device_t *device = (oceanic_veo250_device_t*) abstract;
if (!ISINSTANCE (abstract))
return DC_STATUS_INVALIDARGS;
unsigned char answer[2] = {0};
unsigned char command[4] = {0x91,
(device->last ) & 0xFF, (device->last >> 8) & 0xFF, 0x00};
dc_status_t rc = oceanic_veo250_transfer (device, command, sizeof (command), answer, sizeof (answer));
if (rc != DC_STATUS_SUCCESS)
return rc;
if (answer[0] != NAK) {
ERROR (abstract->context, "Unexpected answer byte(s).");
return DC_STATUS_PROTOCOL;
}
return DC_STATUS_SUCCESS;
}
dc_status_t
oceanic_veo250_device_version (dc_device_t *abstract, unsigned char data[], unsigned int size)
{
oceanic_veo250_device_t *device = (oceanic_veo250_device_t*) abstract;
if (!ISINSTANCE (abstract))
return DC_STATUS_INVALIDARGS;
if (size < PAGESIZE)
return DC_STATUS_INVALIDARGS;
unsigned char answer[PAGESIZE + 2] = {0};
unsigned char command[2] = {0x90, 0x00};
dc_status_t rc = oceanic_veo250_transfer (device, command, sizeof (command), answer, sizeof (answer));
if (rc != DC_STATUS_SUCCESS)
return rc;
unsigned char crc = answer[PAGESIZE];
unsigned char ccrc = checksum_add_uint8 (answer, PAGESIZE, 0x00);
if (crc != ccrc) {
ERROR (abstract->context, "Unexpected answer checksum.");
return DC_STATUS_PROTOCOL;
}
memcpy (data, answer, PAGESIZE);
return DC_STATUS_SUCCESS;
}
static dc_status_t
oceanic_veo250_device_read (dc_device_t *abstract, unsigned int address, unsigned char data[], unsigned int size)
{
oceanic_veo250_device_t *device = (oceanic_veo250_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 + 1] = {0};
unsigned char command[6] = {0x20,
(first ) & 0xFF, (first >> 8) & 0xFF, (last ) & 0xFF, (last >> 8) & 0xFF, 0};
dc_status_t rc = oceanic_veo250_transfer (device, command, sizeof (command), answer, (PAGESIZE + 1) * npackets + 1);
if (rc != DC_STATUS_SUCCESS)
return rc;
device->last = last;
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;
}