#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include "oceans_s1.h"
#include "oceans_s1_common.h"
#include "context-private.h"
#include "device-private.h"
#include "platform.h"
#include "checksum.h"
#include "array.h"
#define SOH 0x01
#define EOT 0x04
#define ACK 0x06
#define NAK 0x15
#define CAN 0x18
#define CRC 0x43
#define SZ_PACKET 256
#define SZ_XMODEM 512
#define SZ_FINGERPRINT 8
typedef struct oceans_s1_dive_t {
struct oceans_s1_dive_t *next;
dc_ticks_t timestamp;
unsigned int number;
} oceans_s1_dive_t;
typedef struct oceans_s1_device_t {
dc_device_t base;
dc_iostream_t *iostream;
dc_ticks_t timestamp;
} oceans_s1_device_t;
static dc_status_t oceans_s1_device_set_fingerprint(dc_device_t *abstract, const unsigned char data[], unsigned int size);
static dc_status_t oceans_s1_device_foreach(dc_device_t *abstract, dc_dive_callback_t callback, void *userdata);
static dc_status_t oceans_s1_device_timesync(dc_device_t *abstract, const dc_datetime_t *datetime);
static const dc_device_vtable_t oceans_s1_device_vtable = {
sizeof(oceans_s1_device_t),
DC_FAMILY_OCEANS_S1,
oceans_s1_device_set_fingerprint,
NULL,
NULL,
NULL,
oceans_s1_device_foreach,
oceans_s1_device_timesync,
NULL,
};
static void
oceans_s1_list_add (oceans_s1_dive_t **head, oceans_s1_dive_t *dive)
{
if (head == NULL)
return;
oceans_s1_dive_t *current = *head, *previous = NULL;
while (current) {
if (dive->number >= current->number)
break;
previous = current;
current = current->next;
}
if (previous) {
dive->next = previous->next;
previous->next = dive;
} else {
dive->next = *head;
*head = dive;
}
}
static void
oceans_s1_list_free (oceans_s1_dive_t *head)
{
oceans_s1_dive_t *current = head;
while (current) {
oceans_s1_dive_t *next = current->next;
free (current);
current = next;
}
}
static dc_status_t
oceans_s1_xmodem_packet (oceans_s1_device_t *device, unsigned char seq, unsigned char data[], size_t size)
{
dc_status_t status = DC_STATUS_SUCCESS;
unsigned char packet[3 + SZ_XMODEM + 2] = {0};
size_t nbytes = 0;
if (size < SZ_XMODEM)
return DC_STATUS_INVALIDARGS;
status = dc_iostream_read (device->iostream, packet, sizeof(packet), &nbytes);
if (status != DC_STATUS_SUCCESS) {
ERROR (device->base.context, "Failed to receive the packet.");
return status;
}
if (nbytes < 1) {
ERROR (device->base.context, "Unexpected packet length (" DC_PRINTF_SIZE ").", nbytes);
return DC_STATUS_PROTOCOL;
}
if (packet[0] == EOT) {
return DC_STATUS_DONE;
}
if (nbytes < 3) {
ERROR (device->base.context, "Unexpected packet length (" DC_PRINTF_SIZE ").", nbytes);
return DC_STATUS_PROTOCOL;
}
if (packet[0] != SOH || packet[1] != seq || packet[1] + packet[2] != 0xFF) {
ERROR (device->base.context, "Unexpected packet header.");
return DC_STATUS_PROTOCOL;
}
while (nbytes < sizeof(packet)) {
size_t received = 0;
status = dc_iostream_read (device->iostream, packet + nbytes, sizeof(packet) - nbytes, &received);
if (status != DC_STATUS_SUCCESS) {
ERROR (device->base.context, "Failed to receive the packet.");
return status;
}
nbytes += received;
}
unsigned short crc = array_uint16_be (packet + nbytes - 2);
unsigned short ccrc = checksum_crc16_ccitt (packet + 3, nbytes - 5, 0x0000, 0x0000);
if (crc != ccrc) {
ERROR (device->base.context, "Unexpected answer checksum (%04x %04x).", crc, ccrc);
return DC_STATUS_PROTOCOL;
}
memcpy (data, packet + 3, SZ_XMODEM);
return DC_STATUS_SUCCESS;
}
static dc_status_t
oceans_s1_xmodem_recv (oceans_s1_device_t *device, dc_buffer_t *buffer)
{
dc_status_t status = DC_STATUS_SUCCESS;
const unsigned char crc = CRC;
const unsigned char ack = ACK;
dc_buffer_clear (buffer);
status = dc_iostream_write (device->iostream, &crc, 1, NULL);
if (status != DC_STATUS_SUCCESS)
return status;
unsigned char seq = 1;
while (1) {
unsigned char packet[SZ_XMODEM] = {0};
status = oceans_s1_xmodem_packet (device, seq, packet, sizeof(packet));
if (status != DC_STATUS_SUCCESS) {
if (status == DC_STATUS_DONE)
break;
return status;
}
dc_buffer_append (buffer, packet, sizeof(packet));
status = dc_iostream_write (device->iostream, &ack, 1, NULL);
if (status != DC_STATUS_SUCCESS)
return status;
seq++;
}
status = dc_iostream_write (device->iostream, &ack, 1, NULL);
if (status != DC_STATUS_SUCCESS) {
return status;
}
size_t size = dc_buffer_get_size (buffer);
unsigned char *data = dc_buffer_get_data (buffer);
while (size > 1 && (data[size - 2] == '\r' || data[size - 2] == '\n'))
size--;
dc_buffer_slice (buffer, 0, size);
return DC_STATUS_SUCCESS;
}
static dc_status_t DC_ATTR_FORMAT_PRINTF(6, 7)
oceans_s1_transfer (oceans_s1_device_t *device, dc_buffer_t *buffer, char data[], size_t size, const char *cmd, const char *params, ...)
{
dc_status_t status = DC_STATUS_SUCCESS;
char buf[SZ_PACKET + 1] = {0};
size_t buflen = 0;
if (device_is_cancelled (&device->base))
return DC_STATUS_CANCELLED;
size_t cmdlen = strlen (cmd);
if (buflen + cmdlen > sizeof(buf) - 1) {
ERROR (device->base.context, "Not enough space for the command string.");
return DC_STATUS_NOMEMORY;
}
memcpy (buf, cmd, cmdlen);
buflen += cmdlen;
buf[buflen] = 0;
if (params) {
if (buflen + 1 > sizeof(buf) - 1) {
ERROR (device->base.context, "Not enough space for the separator.");
return DC_STATUS_NOMEMORY;
}
buf[buflen++] = ' ';
buf[buflen] = 0;
va_list ap;
va_start (ap, params);
int n = dc_platform_vsnprintf (buf + buflen, sizeof(buf) - buflen, params, ap);
va_end (ap);
if (n < 0) {
ERROR (device->base.context, "Not enough space for the arguments.");
return DC_STATUS_NOMEMORY;
}
buflen += n;
}
DEBUG(device->base.context, "cmd: %s", buf);
if (buflen + 1 > sizeof(buf) - 1) {
ERROR (device->base.context, "Not enough space for the newline.");
return DC_STATUS_NOMEMORY;
}
buf[buflen++] = '\n';
buf[buflen] = 0;
status = dc_iostream_write (device->iostream, buf, buflen, NULL);
if (status != DC_STATUS_SUCCESS) {
ERROR (device->base.context, "Failed to send the command.");
return status;
}
size_t nbytes = 0;
status = dc_iostream_read (device->iostream, buf, sizeof(buf) - 1, &nbytes);
if (status != DC_STATUS_SUCCESS) {
ERROR (device->base.context, "Failed to receive the response.");
return status;
}
while (nbytes && (buf[nbytes - 1] == '\r' || buf[nbytes - 1] == '\n'))
nbytes--;
buf[nbytes] = 0;
DEBUG (device->base.context, "rcv: %s", buf);
if (strncmp (buf, cmd, cmdlen) != 0) {
ERROR (device->base.context, "Received unexpected packet data ('%s').", buf);
return DC_STATUS_PROTOCOL;
}
if (strncmp (buf + cmdlen, ">ok", 3) == 0) {
const char *line = buf + cmdlen + 3;
while (*line == ' ')
line++;
size_t len = nbytes - (line - buf);
if (size) {
if (len + 1 > size) {
ERROR (device->base.context, "Unexpected packet length (" DC_PRINTF_SIZE ").", len);
return DC_STATUS_PROTOCOL;
}
memcpy (data, line, len + 1);
} else {
if (len != 0) {
ERROR (device->base.context, "Unexpected packet length (" DC_PRINTF_SIZE ").", len);
return DC_STATUS_PROTOCOL;
}
}
} else if (strncmp (buf + cmdlen, ">xmr", 4) == 0) {
if (nbytes > cmdlen + 4) {
WARNING (device->base.context, "Packet contains extra data ('%s').", buf + cmdlen + 4);
}
return oceans_s1_xmodem_recv (device, buffer);
} else {
ERROR (device->base.context, "Received unexpected packet data ('%s').", buf);
return DC_STATUS_PROTOCOL;
}
return status;
}
dc_status_t
oceans_s1_device_open(dc_device_t **out, dc_context_t *context, dc_iostream_t *iostream)
{
dc_status_t status = DC_STATUS_SUCCESS;
oceans_s1_device_t *device = NULL;
if (out == NULL)
return DC_STATUS_INVALIDARGS;
device = (oceans_s1_device_t *) dc_device_allocate (context, &oceans_s1_device_vtable);
if (device == NULL) {
ERROR(context, "Failed to allocate memory.");
return DC_STATUS_NOMEMORY;
}
device->iostream = iostream;
device->timestamp = 0;
status = dc_iostream_set_timeout (device->iostream, 4000);
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
oceans_s1_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size)
{
oceans_s1_device_t *device = (oceans_s1_device_t *) abstract;
if (size && size != SZ_FINGERPRINT)
return DC_STATUS_INVALIDARGS;
if (size)
device->timestamp = array_uint64_be (data);
else
device->timestamp = 0;
return DC_STATUS_SUCCESS;
}
static dc_status_t
oceans_s1_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata)
{
dc_status_t status = DC_STATUS_SUCCESS;
oceans_s1_device_t *device = (oceans_s1_device_t *) abstract;
dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER;
device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);
char version[SZ_PACKET] = {0};
status = oceans_s1_transfer (device, NULL, version, sizeof(version), "version", NULL);
if (status != DC_STATUS_SUCCESS) {
ERROR (abstract->context, "Failed to read the version.");
return status;
}
unsigned int major = 0, minor = 0, unknown = 0;
if (sscanf (version, "%u.%u %x", &major, &minor, &unknown) != 3) {
ERROR (abstract->context, "Failed to parse the version response.");
return DC_STATUS_PROTOCOL;
}
dc_event_devinfo_t devinfo;
devinfo.model = 0;
devinfo.firmware = major << 16 | minor;
devinfo.serial = 0;
device_event_emit (abstract, DC_EVENT_DEVINFO, &devinfo);
dc_buffer_t *buffer = dc_buffer_new (4096);
if (buffer == NULL) {
ERROR (abstract->context, "Failed to allocate memory.");
status = DC_STATUS_NOMEMORY;
goto error_exit;
}
status = oceans_s1_transfer (device, buffer, NULL, 0, "dllist", NULL);
if (status != DC_STATUS_SUCCESS) {
ERROR (abstract->context, "Failed to download the dive list.");
goto error_free_buffer;
}
const unsigned char *data = dc_buffer_get_data (buffer);
size_t size = dc_buffer_get_size (buffer);
oceans_s1_dive_t *logbook = NULL, *dive = NULL;
unsigned int ndives = 0;
char *ptr = NULL;
size_t len = 0;
int n = 0;
while ((n = oceans_s1_getline (&ptr, &len, &data, &size)) != -1) {
if (n == 0)
continue;
const char *line = ptr;
while (*line == ' ')
line++;
if (strncmp (line, "divelog", 7) == 0 ||
strncmp (line, "endlog", 6) == 0 ||
strncmp (line, "continue", 8) == 0) {
} else if (strncmp (line, "dive", 4) == 0) {
if (dive != NULL) {
ERROR (abstract->context, "Skipping dive without 'enddive' line.");
free (dive);
dive = NULL;
}
unsigned int number = 0, divemode = 0, o2 = 0;
dc_ticks_t timestamp = 0;
if (sscanf (line, "dive %u,%u,%u," DC_FORMAT_INT64, &number, &divemode, &o2, ×tamp) != 4) {
ERROR (abstract->context, "Failed to parse the line '%s'.", line);
status = DC_STATUS_DATAFORMAT;
goto error_free_list;
}
dive = (oceans_s1_dive_t *) malloc (sizeof (oceans_s1_dive_t));
if (dive == NULL) {
ERROR (abstract->context, "Failed to allocate memory.");
status = DC_STATUS_NOMEMORY;
goto error_free_list;
}
dive->next = NULL;
dive->timestamp = timestamp;
dive->number = number;
} else if (strncmp (line, "enddive", 7) == 0) {
if (dive) {
if (dive->timestamp > device->timestamp) {
oceans_s1_list_add (&logbook, dive);
ndives++;
} else {
free (dive);
}
dive = NULL;
} else {
WARNING (abstract->context, "Unexpected line '%s'.", line);
}
} else {
WARNING (abstract->context, "Unexpected line '%s'.", line);
}
}
if (dive != NULL) {
WARNING (abstract->context, "Skipping dive without 'enddive' line.");
free (dive);
dive = NULL;
}
progress.current = 1;
progress.maximum = 1 + ndives;
device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);
for (dive = logbook; dive; dive = dive->next) {
status = oceans_s1_transfer (device, buffer, NULL, 0, "dlget", "%u %u", dive->number, dive->number + 1);
if (status != DC_STATUS_SUCCESS) {
ERROR (abstract->context, "Failed to download the dive.");
goto error_free_list;
}
progress.current++;
device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);
unsigned char fingerprint[SZ_FINGERPRINT] = {0};
array_uint64_be_set (fingerprint, dive->timestamp);
if (callback && !callback (dc_buffer_get_data (buffer), dc_buffer_get_size (buffer), fingerprint, sizeof(fingerprint), userdata))
break;
}
error_free_list:
oceans_s1_list_free (logbook);
free (ptr);
error_free_buffer:
dc_buffer_free (buffer);
error_exit:
return status;
}
static dc_status_t
oceans_s1_device_timesync (dc_device_t *abstract, const dc_datetime_t *datetime)
{
dc_status_t status = DC_STATUS_SUCCESS;
oceans_s1_device_t *device = (oceans_s1_device_t *) abstract;
dc_datetime_t dt = *datetime;
dt.timezone = DC_TIMEZONE_NONE;
dc_ticks_t timestamp = dc_datetime_mktime (&dt);
if (timestamp < 0) {
ERROR (abstract->context, "Invalid date/time value specified.");
return DC_STATUS_INVALIDARGS;
}
status = oceans_s1_transfer (device, NULL, NULL, 0, "utc", DC_FORMAT_INT64, timestamp);
if (status != DC_STATUS_SUCCESS) {
ERROR (abstract->context, "Failed to set the date/time.");
return status;
}
return status;
}