#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "reefnet_sensusultra.h"
#include "context-private.h"
#include "device-private.h"
#include "checksum.h"
#include "array.h"
#define ISINSTANCE(device) dc_device_isinstance((device), &reefnet_sensusultra_device_vtable)
#define SZ_PACKET 512
#define SZ_MEMORY 2080768
#define SZ_USER 16384
#define SZ_HANDSHAKE 24
#define SZ_SENSE 6
#define MAXRETRIES 2
#define PROMPT 0xA5
#define ACCEPT PROMPT
#define REJECT 0x00
typedef struct reefnet_sensusultra_device_t {
dc_device_t base;
dc_iostream_t *iostream;
unsigned char handshake[SZ_HANDSHAKE];
unsigned int timestamp;
unsigned int devtime;
dc_ticks_t systime;
} reefnet_sensusultra_device_t;
static dc_status_t reefnet_sensusultra_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size);
static dc_status_t reefnet_sensusultra_device_dump (dc_device_t *abstract, dc_buffer_t *buffer);
static dc_status_t reefnet_sensusultra_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata);
static const dc_device_vtable_t reefnet_sensusultra_device_vtable = {
sizeof(reefnet_sensusultra_device_t),
DC_FAMILY_REEFNET_SENSUSULTRA,
reefnet_sensusultra_device_set_fingerprint,
NULL,
NULL,
reefnet_sensusultra_device_dump,
reefnet_sensusultra_device_foreach,
NULL,
NULL
};
dc_status_t
reefnet_sensusultra_device_open (dc_device_t **out, dc_context_t *context, dc_iostream_t *iostream)
{
dc_status_t status = DC_STATUS_SUCCESS;
reefnet_sensusultra_device_t *device = NULL;
if (out == NULL)
return DC_STATUS_INVALIDARGS;
device = (reefnet_sensusultra_device_t *) dc_device_allocate (context, &reefnet_sensusultra_device_vtable);
if (device == NULL) {
ERROR (context, "Failed to allocate memory.");
return DC_STATUS_NOMEMORY;
}
device->iostream = iostream;
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, 115200, 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;
}
dc_status_t
reefnet_sensusultra_device_get_handshake (dc_device_t *abstract, unsigned char data[], unsigned int size)
{
reefnet_sensusultra_device_t *device = (reefnet_sensusultra_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_sensusultra_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size)
{
reefnet_sensusultra_device_t *device = (reefnet_sensusultra_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_sensusultra_send_uchar (reefnet_sensusultra_device_t *device, unsigned char value)
{
dc_status_t status = DC_STATUS_SUCCESS;
dc_device_t *abstract = (dc_device_t *) device;
unsigned char prompt = 0;
status = dc_iostream_read (device->iostream, &prompt, 1, NULL);
if (status != DC_STATUS_SUCCESS) {
ERROR (abstract->context, "Failed to receive the prompt byte");
return status;
}
if (prompt != PROMPT) {
ERROR (abstract->context, "Unexpected answer data.");
return DC_STATUS_PROTOCOL;
}
status = dc_iostream_write (device->iostream, &value, 1, NULL);
if (status != DC_STATUS_SUCCESS) {
ERROR (abstract->context, "Failed to send the value.");
return status;
}
return DC_STATUS_SUCCESS;
}
static dc_status_t
reefnet_sensusultra_send_ushort (reefnet_sensusultra_device_t *device, unsigned short value)
{
unsigned char lsb = value & 0xFF;
dc_status_t rc = reefnet_sensusultra_send_uchar (device, lsb);
if (rc != DC_STATUS_SUCCESS)
return rc;
unsigned char msb = (value >> 8) & 0xFF;
rc = reefnet_sensusultra_send_uchar (device, msb);
if (rc != DC_STATUS_SUCCESS)
return rc;
return DC_STATUS_SUCCESS;
}
static dc_status_t
reefnet_sensusultra_packet (reefnet_sensusultra_device_t *device, unsigned char *data, unsigned int size, unsigned int header)
{
dc_status_t status = DC_STATUS_SUCCESS;
dc_device_t *abstract = (dc_device_t *) device;
assert (size >= header + 2);
if (device_is_cancelled (abstract))
return DC_STATUS_CANCELLED;
status = dc_iostream_read (device->iostream, data, size, NULL);
if (status != DC_STATUS_SUCCESS) {
ERROR (abstract->context, "Failed to receive the packet.");
return status;
}
unsigned short crc = array_uint16_le (data + size - 2);
unsigned short ccrc = checksum_crc16_ccitt (data + header, size - header - 2, 0xffff, 0x0000);
if (crc != ccrc) {
ERROR (abstract->context, "Unexpected answer checksum.");
return DC_STATUS_PROTOCOL;
}
return DC_STATUS_SUCCESS;
}
static dc_status_t
reefnet_sensusultra_handshake (reefnet_sensusultra_device_t *device, unsigned short value)
{
unsigned char handshake[SZ_HANDSHAKE + 2] = {0};
dc_status_t rc = reefnet_sensusultra_packet (device, handshake, sizeof (handshake), 0);
if (rc != DC_STATUS_SUCCESS)
return rc;
device->systime = dc_datetime_now ();
device->devtime = array_uint32_le (handshake + 4);
memcpy (device->handshake, handshake, 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[1];
devinfo.firmware = handshake[0];
devinfo.serial = array_uint16_le (handshake + 2);
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 (&device->base, DC_EVENT_VENDOR, &vendor);
rc = reefnet_sensusultra_send_ushort (device, value);
if (rc != DC_STATUS_SUCCESS)
return rc;
return DC_STATUS_SUCCESS;
}
static dc_status_t
reefnet_sensusultra_page (reefnet_sensusultra_device_t *device, unsigned char *data, unsigned int size, unsigned int pagenum)
{
dc_device_t *abstract = (dc_device_t *) device;
assert (size >= SZ_PACKET + 4);
unsigned int nretries = 0;
dc_status_t rc = DC_STATUS_SUCCESS;
while ((rc = reefnet_sensusultra_packet (device, data, size, 2)) != DC_STATUS_SUCCESS) {
if (rc != DC_STATUS_PROTOCOL)
return rc;
if (nretries++ >= MAXRETRIES)
return rc;
rc = reefnet_sensusultra_send_uchar (device, REJECT);
if (rc != DC_STATUS_SUCCESS)
return rc;
}
unsigned int page = array_uint16_le (data);
if (page != pagenum) {
ERROR (abstract->context, "Unexpected page number.");
return DC_STATUS_PROTOCOL;
}
return DC_STATUS_SUCCESS;
}
static dc_status_t
reefnet_sensusultra_send (reefnet_sensusultra_device_t *device, unsigned short command)
{
dc_iostream_purge (device->iostream, DC_DIRECTION_ALL);
unsigned int nretries = 0;
dc_status_t rc = DC_STATUS_SUCCESS;
while ((rc = reefnet_sensusultra_handshake (device, command)) != DC_STATUS_SUCCESS) {
if (rc != DC_STATUS_PROTOCOL && rc != DC_STATUS_TIMEOUT)
return rc;
if (nretries++ >= MAXRETRIES)
return rc;
dc_iostream_sleep (device->iostream, 250);
dc_iostream_purge (device->iostream, DC_DIRECTION_ALL);
}
return DC_STATUS_SUCCESS;
}
static dc_status_t
reefnet_sensusultra_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
{
reefnet_sensusultra_device_t *device = (reefnet_sensusultra_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 = SZ_MEMORY;
device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);
dc_status_t rc = reefnet_sensusultra_send (device, 0xB421);
if (rc != DC_STATUS_SUCCESS)
return rc;
unsigned int nbytes = 0;
unsigned int npages = 0;
while (nbytes < SZ_MEMORY) {
unsigned char packet[SZ_PACKET + 4] = {0};
rc = reefnet_sensusultra_page (device, packet, sizeof (packet), npages);
if (rc != DC_STATUS_SUCCESS)
return rc;
progress.current += SZ_PACKET;
device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);
if (!dc_buffer_prepend (buffer, packet + 2, SZ_PACKET)) {
ERROR (abstract->context, "Insufficient buffer space available.");
return DC_STATUS_NOMEMORY;
}
rc = reefnet_sensusultra_send_uchar (device, ACCEPT);
if (rc != DC_STATUS_SUCCESS)
return rc;
nbytes += SZ_PACKET;
npages++;
}
return DC_STATUS_SUCCESS;
}
dc_status_t
reefnet_sensusultra_device_read_user (dc_device_t *abstract, unsigned char *data, unsigned int size)
{
reefnet_sensusultra_device_t *device = (reefnet_sensusultra_device_t*) abstract;
if (!ISINSTANCE (abstract))
return DC_STATUS_INVALIDARGS;
if (size < SZ_USER) {
ERROR (abstract->context, "Insufficient buffer space available.");
return DC_STATUS_INVALIDARGS;
}
dc_status_t rc = reefnet_sensusultra_send (device, 0xB420);
if (rc != DC_STATUS_SUCCESS)
return rc;
unsigned int nbytes = 0;
unsigned int npages = 0;
while (nbytes < SZ_USER) {
unsigned char packet[SZ_PACKET + 4] = {0};
rc = reefnet_sensusultra_page (device, packet, sizeof (packet), npages);
if (rc != DC_STATUS_SUCCESS)
return rc;
memcpy (data + nbytes, packet + 2, SZ_PACKET);
rc = reefnet_sensusultra_send_uchar (device, ACCEPT);
if (rc != DC_STATUS_SUCCESS)
return rc;
nbytes += SZ_PACKET;
npages++;
}
return DC_STATUS_SUCCESS;
}
dc_status_t
reefnet_sensusultra_device_write_user (dc_device_t *abstract, const unsigned char *data, unsigned int size)
{
reefnet_sensusultra_device_t *device = (reefnet_sensusultra_device_t*) abstract;
if (!ISINSTANCE (abstract))
return DC_STATUS_INVALIDARGS;
if (size < SZ_USER) {
ERROR (abstract->context, "Insufficient buffer space available.");
return DC_STATUS_INVALIDARGS;
}
dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER;
progress.maximum = SZ_USER + 2;
device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);
dc_status_t rc = reefnet_sensusultra_send (device, 0xB430);
if (rc != DC_STATUS_SUCCESS)
return rc;
for (unsigned int i = 0; i < SZ_USER; ++i) {
rc = reefnet_sensusultra_send_uchar (device, data[i]);
if (rc != DC_STATUS_SUCCESS)
return rc;
progress.current += 1;
device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);
}
unsigned short crc = checksum_crc16_ccitt (data, SZ_USER, 0xffff, 0x0000);
rc = reefnet_sensusultra_send_ushort (device, crc);
if (rc != DC_STATUS_SUCCESS)
return rc;
progress.current += 2;
device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);
return DC_STATUS_SUCCESS;
}
dc_status_t
reefnet_sensusultra_device_write_parameter (dc_device_t *abstract, reefnet_sensusultra_parameter_t parameter, unsigned int value)
{
reefnet_sensusultra_device_t *device = (reefnet_sensusultra_device_t*) abstract;
if (!ISINSTANCE (abstract))
return DC_STATUS_INVALIDARGS;
unsigned short code = 0;
switch (parameter) {
case REEFNET_SENSUSULTRA_PARAMETER_INTERVAL:
code = 0xB410;
if (value < 1 || value > 65535)
return DC_STATUS_INVALIDARGS;
break;
case REEFNET_SENSUSULTRA_PARAMETER_THRESHOLD:
code = 0xB411;
if (value < 1 || value > 65535)
return DC_STATUS_INVALIDARGS;
break;
case REEFNET_SENSUSULTRA_PARAMETER_ENDCOUNT:
code = 0xB412;
if (value < 1 || value > 65535)
return DC_STATUS_INVALIDARGS;
break;
case REEFNET_SENSUSULTRA_PARAMETER_AVERAGING:
code = 0xB413;
if (value != 1 && value != 2 && value != 4)
return DC_STATUS_INVALIDARGS;
break;
default:
return DC_STATUS_INVALIDARGS;
}
dc_status_t rc = reefnet_sensusultra_send (device, code);
if (rc != DC_STATUS_SUCCESS)
return rc;
rc = reefnet_sensusultra_send_ushort (device, value);
if (rc != DC_STATUS_SUCCESS)
return rc;
return DC_STATUS_SUCCESS;
}
dc_status_t
reefnet_sensusultra_device_sense (dc_device_t *abstract, unsigned char *data, unsigned int size)
{
reefnet_sensusultra_device_t *device = (reefnet_sensusultra_device_t*) abstract;
if (!ISINSTANCE (abstract))
return DC_STATUS_INVALIDARGS;
if (size < SZ_SENSE) {
ERROR (abstract->context, "Insufficient buffer space available.");
return DC_STATUS_INVALIDARGS;
}
dc_status_t rc = reefnet_sensusultra_send (device, 0xB440);
if (rc != DC_STATUS_SUCCESS)
return rc;
unsigned char package[SZ_SENSE + 2] = {0};
rc = reefnet_sensusultra_packet (device, package, sizeof (package), 0);
if (rc != DC_STATUS_SUCCESS)
return rc;
memcpy (data, package, SZ_SENSE);
return DC_STATUS_SUCCESS;
}
static dc_status_t
reefnet_sensusultra_parse (reefnet_sensusultra_device_t *device,
const unsigned char data[], unsigned int *premaining, unsigned int *pprevious,
int *aborted, dc_dive_callback_t callback, void *userdata)
{
const unsigned char header[4] = {0x00, 0x00, 0x00, 0x00};
const unsigned char footer[4] = {0xFF, 0xFF, 0xFF, 0xFF};
const unsigned char *current = data + *premaining;
const unsigned char *previous = data + *pprevious;
while ((current = array_search_backward (data, current - data, header, sizeof (header))) != NULL) {
current -= sizeof (header);
while (current > data && current[-1] == 0x00)
current--;
if (previous - current >= 16) {
previous = array_search_forward (current + 16, previous - current - 16, footer, sizeof (footer));
} else {
previous = NULL;
}
if (previous) {
previous += sizeof (footer);
unsigned int timestamp = array_uint32_le (current + 4);
if (device && timestamp <= device->timestamp) {
if (aborted)
*aborted = 1;
return DC_STATUS_SUCCESS;
}
if (callback && !callback (current, previous - current, current + 4, 4, userdata)) {
if (aborted)
*aborted = 1;
return DC_STATUS_SUCCESS;
}
}
previous = current;
*premaining = *pprevious = current - data;
}
*premaining = sizeof (header) - 1;
if (*premaining > *pprevious)
*premaining = *pprevious;
if (aborted)
*aborted = 0;
return DC_STATUS_SUCCESS;
}
static dc_status_t
reefnet_sensusultra_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata)
{
reefnet_sensusultra_device_t *device = (reefnet_sensusultra_device_t*) abstract;
dc_buffer_t *buffer = dc_buffer_new (SZ_MEMORY);
if (buffer == NULL) {
ERROR (abstract->context, "Failed to allocate memory.");
return DC_STATUS_NOMEMORY;
}
dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER;
progress.maximum = SZ_MEMORY;
device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);
dc_status_t rc = reefnet_sensusultra_send (device, 0xB421);
if (rc != DC_STATUS_SUCCESS) {
dc_buffer_free (buffer);
return rc;
}
unsigned int remaining = 0;
unsigned int previous = 0;
unsigned int nbytes = 0;
unsigned int npages = 0;
while (nbytes < SZ_MEMORY) {
unsigned char packet[SZ_PACKET + 4] = {0};
rc = reefnet_sensusultra_page (device, packet, sizeof (packet), npages);
if (rc != DC_STATUS_SUCCESS) {
dc_buffer_free (buffer);
return rc;
}
progress.current += SZ_PACKET;
device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);
if (array_isequal (packet + 2, SZ_PACKET, 0xFF) && nbytes != 0)
break;
if (!dc_buffer_prepend (buffer, packet + 2, SZ_PACKET)) {
dc_buffer_free (buffer);
ERROR (abstract->context, "Insufficient buffer space available.");
return DC_STATUS_NOMEMORY;
}
remaining += SZ_PACKET;
previous += SZ_PACKET;
int aborted = 0;
rc = reefnet_sensusultra_parse (device, dc_buffer_get_data (buffer),
&remaining, &previous, &aborted, callback, userdata);
if (rc != DC_STATUS_SUCCESS) {
dc_buffer_free (buffer);
return rc;
}
if (aborted)
break;
rc = reefnet_sensusultra_send_uchar (device, ACCEPT);
if (rc != DC_STATUS_SUCCESS) {
dc_buffer_free (buffer);
return rc;
}
nbytes += SZ_PACKET;
npages++;
}
dc_buffer_free (buffer);
return DC_STATUS_SUCCESS;
}