#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <libdivecomputer/units.h>
#include "mares_darwin.h"
#include "mares_common.h"
#include "context-private.h"
#include "device-private.h"
#include "array.h"
#define ISINSTANCE(device) dc_device_isinstance((device), &mares_darwin_device_vtable)
#define DARWIN 0
#define DARWINAIR 1
typedef struct mares_darwin_layout_t {
unsigned int memsize;
unsigned int rb_logbook_offset;
unsigned int rb_logbook_size;
unsigned int rb_logbook_count;
unsigned int rb_profile_begin;
unsigned int rb_profile_end;
unsigned int samplesize;
} mares_darwin_layout_t;
typedef struct mares_darwin_device_t {
mares_common_device_t base;
const mares_darwin_layout_t *layout;
unsigned int model;
unsigned char fingerprint[6];
} mares_darwin_device_t;
static dc_status_t mares_darwin_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size);
static dc_status_t mares_darwin_device_dump (dc_device_t *abstract, dc_buffer_t *buffer);
static dc_status_t mares_darwin_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata);
static const dc_device_vtable_t mares_darwin_device_vtable = {
sizeof(mares_darwin_device_t),
DC_FAMILY_MARES_DARWIN,
mares_darwin_device_set_fingerprint,
mares_common_device_read,
NULL,
mares_darwin_device_dump,
mares_darwin_device_foreach,
NULL,
NULL
};
static const mares_darwin_layout_t mares_darwin_layout = {
0x4000,
0x0100,
52,
50,
0x0B30,
0x4000,
2
};
static const mares_darwin_layout_t mares_darwinair_layout = {
0x4000,
0x0100,
60,
50,
0x0CC0,
0x3FFF,
3
};
static dc_status_t
mares_darwin_extract_dives (dc_device_t *device, const unsigned char data[], unsigned int size, dc_dive_callback_t callback, void *userdata);
dc_status_t
mares_darwin_device_open (dc_device_t **out, dc_context_t *context, dc_iostream_t *iostream, unsigned int model)
{
dc_status_t status = DC_STATUS_SUCCESS;
mares_darwin_device_t *device = NULL;
if (out == NULL)
return DC_STATUS_INVALIDARGS;
device = (mares_darwin_device_t *) dc_device_allocate (context, &mares_darwin_device_vtable);
if (device == NULL) {
ERROR (context, "Failed to allocate memory.");
return DC_STATUS_NOMEMORY;
}
mares_common_device_init (&device->base, iostream);
memset (device->fingerprint, 0, sizeof (device->fingerprint));
device->model = model;
if (model == DARWINAIR)
device->layout = &mares_darwinair_layout;
else
device->layout = &mares_darwin_layout;
status = dc_iostream_configure (device->base.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->base.iostream, 1000);
if (status != DC_STATUS_SUCCESS) {
ERROR (context, "Failed to set the timeout.");
goto error_free;
}
status = dc_iostream_set_dtr (device->base.iostream, 1);
if (status != DC_STATUS_SUCCESS) {
ERROR (context, "Failed to set the DTR line.");
goto error_free;
}
status = dc_iostream_set_rts (device->base.iostream, 1);
if (status != DC_STATUS_SUCCESS) {
ERROR (context, "Failed to set the RTS line.");
goto error_free;
}
dc_iostream_sleep (device->base.iostream, 100);
dc_iostream_purge (device->base.iostream, DC_DIRECTION_ALL);
device->base.echo = 1;
device->base.delay = 50;
*out = (dc_device_t *) device;
return DC_STATUS_SUCCESS;
error_free:
dc_device_deallocate ((dc_device_t *) device);
return status;
}
static dc_status_t
mares_darwin_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size)
{
mares_darwin_device_t *device = (mares_darwin_device_t *) abstract;
if (size && size != sizeof (device->fingerprint))
return DC_STATUS_INVALIDARGS;
if (size)
memcpy (device->fingerprint, data, sizeof (device->fingerprint));
else
memset (device->fingerprint, 0, sizeof (device->fingerprint));
return DC_STATUS_SUCCESS;
}
static dc_status_t
mares_darwin_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
{
dc_status_t status = DC_STATUS_SUCCESS;
mares_darwin_device_t *device = (mares_darwin_device_t *) abstract;
assert (device->layout != NULL);
if (!dc_buffer_resize (buffer, device->layout->memsize)) {
ERROR (abstract->context, "Insufficient buffer space available.");
return DC_STATUS_NOMEMORY;
}
status = device_dump_read (abstract, 0, dc_buffer_get_data (buffer),
dc_buffer_get_size (buffer), PACKETSIZE);
if (status != DC_STATUS_SUCCESS) {
return status;
}
unsigned char *data = dc_buffer_get_data (buffer);
dc_event_devinfo_t devinfo;
devinfo.model = device->model;
devinfo.firmware = 0;
devinfo.serial = array_uint16_be (data + 8);
device_event_emit (abstract, DC_EVENT_DEVINFO, &devinfo);
return status;
}
static dc_status_t
mares_darwin_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata)
{
mares_darwin_device_t *device = (mares_darwin_device_t *) abstract;
assert (device->layout != NULL);
dc_buffer_t *buffer = dc_buffer_new (device->layout->memsize);
if (buffer == NULL)
return DC_STATUS_NOMEMORY;
dc_status_t rc = mares_darwin_device_dump (abstract, buffer);
if (rc != DC_STATUS_SUCCESS) {
dc_buffer_free (buffer);
return rc;
}
rc = mares_darwin_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
mares_darwin_extract_dives (dc_device_t *abstract, const unsigned char data[], unsigned int size, dc_dive_callback_t callback, void *userdata)
{
mares_darwin_device_t *device = (mares_darwin_device_t *) abstract;
if (!ISINSTANCE (abstract))
return DC_STATUS_INVALIDARGS;
assert (device->layout != NULL);
const mares_darwin_layout_t *layout = device->layout;
unsigned int eop = array_uint16_be (data + 0x8A);
if (eop < layout->rb_profile_begin || eop >= layout->rb_profile_end) {
ERROR (abstract->context, "Invalid ringbuffer pointer detected (0x%04x).", eop);
return DC_STATUS_DATAFORMAT;
}
unsigned int last = data[0x8C];
if (last >= layout->rb_logbook_count) {
ERROR (abstract->context, "Invalid ringbuffer pointer detected (0x%02x).", last);
return DC_STATUS_DATAFORMAT;
}
unsigned char *buffer = (unsigned char *) malloc (layout->rb_logbook_size + layout->rb_profile_end - layout->rb_profile_begin);
if (buffer == NULL) {
ERROR (abstract->context, "Failed to allocate memory.");
return DC_STATUS_NOMEMORY;
}
unsigned int remaining = layout->rb_profile_end - layout->rb_profile_begin;
unsigned int current = eop;
for (unsigned int i = 0; i < layout->rb_logbook_count; ++i) {
unsigned int idx = (layout->rb_logbook_count + last - i) % layout->rb_logbook_count;
unsigned int offset = layout->rb_logbook_offset + idx * layout->rb_logbook_size;
unsigned int nsamples = array_uint16_be (data + offset + 6);
unsigned int length = nsamples * layout->samplesize;
if (nsamples == 0xFFFF || length > remaining)
break;
memcpy (buffer, data + offset, layout->rb_logbook_size);
if (current < layout->rb_profile_begin + length) {
unsigned int a = current - layout->rb_profile_begin;
unsigned int b = length - a;
memcpy (buffer + layout->rb_logbook_size, data + layout->rb_profile_end - b, b);
memcpy (buffer + layout->rb_logbook_size + b, data + layout->rb_profile_begin, a);
current = layout->rb_profile_end - b;
} else {
memcpy (buffer + layout->rb_logbook_size, data + current - length, length);
current -= length;
}
if (memcmp (buffer, device->fingerprint, sizeof (device->fingerprint)) == 0) {
free (buffer);
return DC_STATUS_SUCCESS;
}
if (callback && !callback (buffer, layout->rb_logbook_size + length, buffer, 6, userdata)) {
free (buffer);
return DC_STATUS_SUCCESS;
}
remaining -= length;
}
free (buffer);
return DC_STATUS_SUCCESS;
}