#include <string.h>
#include <stdlib.h>
#include "reefnet_sensus.h"
#include "context-private.h"
#include "device-private.h"
#include "checksum.h"
#include "array.h"
#define ISINSTANCE(device) dc_device_isinstance((device), &reefnet_sensus_device_vtable)
#define SZ_MEMORY 32768
#define SZ_HANDSHAKE 10
typedef struct reefnet_sensus_device_t {
dc_device_t base;
dc_iostream_t *iostream;
unsigned char handshake[SZ_HANDSHAKE];
unsigned int waiting;
unsigned int timestamp;
unsigned int devtime;
dc_ticks_t systime;
} reefnet_sensus_device_t;
static dc_status_t reefnet_sensus_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size);
static dc_status_t reefnet_sensus_device_dump (dc_device_t *abstract, dc_buffer_t *buffer);
static dc_status_t reefnet_sensus_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata);
static dc_status_t reefnet_sensus_device_close (dc_device_t *abstract);
static const dc_device_vtable_t reefnet_sensus_device_vtable = {
sizeof(reefnet_sensus_device_t),
DC_FAMILY_REEFNET_SENSUS,
reefnet_sensus_device_set_fingerprint,
NULL,
NULL,
reefnet_sensus_device_dump,
reefnet_sensus_device_foreach,
NULL,
reefnet_sensus_device_close
};
static dc_status_t
reefnet_sensus_extract_dives (dc_device_t *device, const unsigned char data[], unsigned int size, dc_dive_callback_t callback, void *userdata);
static dc_status_t
reefnet_sensus_cancel (reefnet_sensus_device_t *device)
{
dc_status_t status = DC_STATUS_SUCCESS;
dc_device_t *abstract = (dc_device_t *) device;
unsigned char command = 0x00;
status = dc_iostream_write (device->iostream, &command, 1, NULL);
if (status != DC_STATUS_SUCCESS) {
ERROR (abstract->context, "Failed to send the command.");
return status;
}
device->waiting = 0;
return DC_STATUS_SUCCESS;
}
dc_status_t
reefnet_sensus_device_open (dc_device_t **out, dc_context_t *context, dc_iostream_t *iostream)
{
dc_status_t status = DC_STATUS_SUCCESS;
reefnet_sensus_device_t *device = NULL;
if (out == NULL)
return DC_STATUS_INVALIDARGS;
device = (reefnet_sensus_device_t *) dc_device_allocate (context, &reefnet_sensus_device_vtable);
if (device == NULL) {
ERROR (context, "Failed to allocate memory.");
return DC_STATUS_NOMEMORY;
}
device->iostream = iostream;
device->waiting = 0;
device->timestamp = 0;
device->systime = (dc_ticks_t) -1;
device->devtime = 0;
memset (device->handshake, 0, sizeof (device->handshake));
status = dc_iostream_configure (device->iostream, 19200, 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;
}
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
reefnet_sensus_device_close (dc_device_t *abstract)
{
dc_status_t status = DC_STATUS_SUCCESS;
reefnet_sensus_device_t *device = (reefnet_sensus_device_t*) abstract;
dc_status_t rc = DC_STATUS_SUCCESS;
if (device->waiting) {
rc = reefnet_sensus_cancel (device);
if (rc != DC_STATUS_SUCCESS) {
dc_status_set_error(&status, rc);
}
}
return status;
}
dc_status_t
reefnet_sensus_device_get_handshake (dc_device_t *abstract, unsigned char data[], unsigned int size)
{
reefnet_sensus_device_t *device = (reefnet_sensus_device_t*) abstract;
if (!ISINSTANCE (abstract))
return DC_STATUS_INVALIDARGS;
if (size < SZ_HANDSHAKE) {
ERROR (abstract->context, "Insufficient buffer space available.");
return DC_STATUS_INVALIDARGS;
}
memcpy (data, device->handshake, SZ_HANDSHAKE);
return DC_STATUS_SUCCESS;
}
static dc_status_t
reefnet_sensus_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size)
{
reefnet_sensus_device_t *device = (reefnet_sensus_device_t*) abstract;
if (size && size != 4)
return DC_STATUS_INVALIDARGS;
if (size)
device->timestamp = array_uint32_le (data);
else
device->timestamp = 0;
return DC_STATUS_SUCCESS;
}
static dc_status_t
reefnet_sensus_handshake (reefnet_sensus_device_t *device)
{
dc_status_t status = DC_STATUS_SUCCESS;
dc_device_t *abstract = (dc_device_t *) device;
unsigned char command = 0x0A;
status = dc_iostream_write (device->iostream, &command, 1, NULL);
if (status != DC_STATUS_SUCCESS) {
ERROR (abstract->context, "Failed to send the command.");
return status;
}
unsigned char handshake[SZ_HANDSHAKE + 2] = {0};
status = dc_iostream_read (device->iostream, handshake, sizeof (handshake), NULL);
if (status != DC_STATUS_SUCCESS) {
ERROR (abstract->context, "Failed to receive the handshake.");
return status;
}
if (handshake[0] != 'O' || handshake[1] != 'K') {
ERROR (abstract->context, "Unexpected answer header.");
return DC_STATUS_PROTOCOL;
}
device->waiting = 1;
device->systime = dc_datetime_now ();
device->devtime = array_uint32_le (handshake + 8);
memcpy (device->handshake, handshake + 2, SZ_HANDSHAKE);
dc_event_clock_t clock;
clock.systime = device->systime;
clock.devtime = device->devtime;
device_event_emit (&device->base, DC_EVENT_CLOCK, &clock);
dc_event_devinfo_t devinfo;
devinfo.model = handshake[2] - '0';
devinfo.firmware = handshake[3] - '0';
devinfo.serial = array_uint16_le (handshake + 6);
device_event_emit (&device->base, DC_EVENT_DEVINFO, &devinfo);
dc_event_vendor_t vendor;
vendor.data = device->handshake;
vendor.size = sizeof (device->handshake);
device_event_emit (abstract, DC_EVENT_VENDOR, &vendor);
dc_iostream_sleep (device->iostream, 10);
return DC_STATUS_SUCCESS;
}
static dc_status_t
reefnet_sensus_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
{
dc_status_t status = DC_STATUS_SUCCESS;
reefnet_sensus_device_t *device = (reefnet_sensus_device_t*) abstract;
if (!dc_buffer_reserve (buffer, SZ_MEMORY)) {
ERROR (abstract->context, "Insufficient buffer space available.");
return DC_STATUS_NOMEMORY;
}
dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER;
progress.maximum = 4 + SZ_MEMORY + 2 + 3;
device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);
dc_status_t rc = reefnet_sensus_handshake (device);
if (rc != DC_STATUS_SUCCESS)
return rc;
unsigned char command = 0x40;
status = dc_iostream_write (device->iostream, &command, 1, NULL);
if (status != DC_STATUS_SUCCESS) {
ERROR (abstract->context, "Failed to send the command.");
return status;
}
device->waiting = 0;
unsigned int nbytes = 0;
unsigned char answer[4 + SZ_MEMORY + 2 + 3] = {0};
while (nbytes < sizeof (answer)) {
unsigned int len = sizeof (answer) - nbytes;
if (len > 128)
len = 128;
status = dc_iostream_read (device->iostream, answer + nbytes, len, NULL);
if (status != DC_STATUS_SUCCESS) {
ERROR (abstract->context, "Failed to receive the answer.");
return status;
}
progress.current += len;
device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);
nbytes += len;
}
if (memcmp (answer, "DATA", 4) != 0 ||
memcmp (answer + sizeof (answer) - 3, "END", 3) != 0) {
ERROR (abstract->context, "Unexpected answer start or end byte(s).");
return DC_STATUS_PROTOCOL;
}
unsigned short crc = array_uint16_le (answer + 4 + SZ_MEMORY);
unsigned short ccrc = checksum_add_uint16 (answer + 4, SZ_MEMORY, 0x00);
if (crc != ccrc) {
ERROR (abstract->context, "Unexpected answer checksum.");
return DC_STATUS_PROTOCOL;
}
dc_buffer_append (buffer, answer + 4, SZ_MEMORY);
return DC_STATUS_SUCCESS;
}
static dc_status_t
reefnet_sensus_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata)
{
dc_buffer_t *buffer = dc_buffer_new (SZ_MEMORY);
if (buffer == NULL)
return DC_STATUS_NOMEMORY;
dc_status_t rc = reefnet_sensus_device_dump (abstract, buffer);
if (rc != DC_STATUS_SUCCESS) {
dc_buffer_free (buffer);
return rc;
}
rc = reefnet_sensus_extract_dives (abstract,
dc_buffer_get_data (buffer), dc_buffer_get_size (buffer), callback, userdata);
dc_buffer_free (buffer);
return rc;
}
static dc_status_t
reefnet_sensus_extract_dives (dc_device_t *abstract, const unsigned char data[], unsigned int size, dc_dive_callback_t callback, void *userdata)
{
reefnet_sensus_device_t *device = (reefnet_sensus_device_t*) abstract;
dc_context_t *context = (abstract ? abstract->context : NULL);
if (abstract && !ISINSTANCE (abstract))
return DC_STATUS_INVALIDARGS;
unsigned int previous = size;
unsigned int current = (size >= 7 ? size - 7 : 0);
while (current > 0) {
current--;
if (data[current] == 0xFF && data[current + 6] == 0xFE) {
int found = 0;
unsigned int nsamples = 0, count = 0;
unsigned int offset = current + 7; while (offset + 1 <= previous) {
unsigned char depth = data[offset++];
if ((nsamples % 6) == 0) {
if (offset + 1 > previous)
break;
offset++;
}
nsamples++;
if (depth < 13 + 3) {
count++;
if (count == 17) {
found = 1;
break;
}
} else {
count = 0;
}
}
if (!found) {
ERROR (context, "No end of dive found.");
return DC_STATUS_DATAFORMAT;
}
unsigned int timestamp = array_uint32_le (data + current + 2);
if (device && timestamp <= device->timestamp)
return DC_STATUS_SUCCESS;
if (callback && !callback (data + current, offset - current, data + current + 2, 4, userdata))
return DC_STATUS_SUCCESS;
previous = current;
current = (current >= 7 ? current - 7 : 0);
}
}
return DC_STATUS_SUCCESS;
}