#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "context-private.h"
#include "mares_common.h"
#include "checksum.h"
#include "array.h"
#define MAXRETRIES 4
#define FP_OFFSET 8
#define FP_SIZE 5
#define NEMO 0
#define NEMOWIDE 1
#define NEMOAIR 4
#define PUCK 7
#define NEMOEXCEL 17
#define NEMOAPNEIST 18
#define PUCKAIR 19
#define AIR 0
#define NITROX 1
#define FREEDIVE 2
#define GAUGE 3
void
mares_common_device_init (mares_common_device_t *device, dc_iostream_t *iostream)
{
assert (device != NULL);
device->iostream = iostream;
device->echo = 0;
device->delay = 0;
}
static void
mares_common_make_ascii (const unsigned char raw[], unsigned int rsize, unsigned char ascii[], unsigned int asize)
{
assert (asize == 2 * (rsize + 2));
ascii[0] = '<';
array_convert_bin2hex (raw, rsize, ascii + 1, 2 * rsize);
unsigned char checksum = checksum_add_uint8 (ascii + 1, 2 * rsize, 0x00);
array_convert_bin2hex (&checksum, 1, ascii + 1 + 2 * rsize, 2);
ascii[asize - 1] = '>';
}
static dc_status_t
mares_common_packet (mares_common_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;
if (device_is_cancelled (abstract))
return DC_STATUS_CANCELLED;
if (device->delay) {
dc_iostream_sleep (device->iostream, device->delay);
}
status = dc_iostream_write (device->iostream, command, csize, NULL);
if (status != DC_STATUS_SUCCESS) {
ERROR (abstract->context, "Failed to send the command.");
return status;
}
if (device->echo) {
unsigned char echo[PACKETSIZE] = {0};
status = dc_iostream_read (device->iostream, echo, csize, NULL);
if (status != DC_STATUS_SUCCESS) {
ERROR (abstract->context, "Failed to receive the echo.");
return status;
}
if (memcmp (echo, command, csize) != 0) {
WARNING (abstract->context, "Unexpected echo.");
}
}
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[0] != '<' || answer[asize - 1] != '>') {
ERROR (abstract->context, "Unexpected answer header/trailer byte.");
return DC_STATUS_PROTOCOL;
}
unsigned char crc = 0;
unsigned char ccrc = checksum_add_uint8 (answer + 1, asize - 4, 0x00);
array_convert_hex2bin (answer + asize - 3, 2, &crc, 1);
if (crc != ccrc) {
ERROR (abstract->context, "Unexpected answer checksum.");
return DC_STATUS_PROTOCOL;
}
return DC_STATUS_SUCCESS;
}
static dc_status_t
mares_common_transfer (mares_common_device_t *device, const unsigned char command[], unsigned int csize, unsigned char answer[], unsigned int asize)
{
unsigned int nretries = 0;
dc_status_t rc = DC_STATUS_SUCCESS;
while ((rc = mares_common_packet (device, command, csize, answer, asize)) != DC_STATUS_SUCCESS) {
if (rc != DC_STATUS_PROTOCOL && rc != DC_STATUS_TIMEOUT)
return rc;
if (nretries++ >= MAXRETRIES)
return rc;
dc_iostream_sleep (device->iostream, 100);
dc_iostream_purge (device->iostream, DC_DIRECTION_INPUT);
}
return rc;
}
dc_status_t
mares_common_device_read (dc_device_t *abstract, unsigned int address, unsigned char data[], unsigned int size)
{
mares_common_device_t *device = (mares_common_device_t*) abstract;
unsigned int nbytes = 0;
while (nbytes < size) {
unsigned int len = size - nbytes;
if (len > PACKETSIZE)
len = PACKETSIZE;
unsigned char raw[] = {0x51,
(address ) & 0xFF, (address >> 8) & 0xFF, len};
unsigned char command[2 * (sizeof (raw) + 2)] = {0};
mares_common_make_ascii (raw, sizeof (raw), command, sizeof (command));
unsigned char answer[2 * (PACKETSIZE + 2)] = {0};
dc_status_t rc = mares_common_transfer (device, command, sizeof (command), answer, 2 * (len + 2));
if (rc != DC_STATUS_SUCCESS)
return rc;
array_convert_hex2bin (answer + 1, 2 * len, data, len);
nbytes += len;
address += len;
data += len;
}
return DC_STATUS_SUCCESS;
}
dc_status_t
mares_common_extract_dives (dc_context_t *context, const mares_common_layout_t *layout, const unsigned char fingerprint[], const unsigned char data[], dc_dive_callback_t callback, void *userdata)
{
assert (layout != NULL);
unsigned int model = data[1];
unsigned int freedive = FREEDIVE;
if (model == NEMOWIDE || model == NEMOAIR || model == PUCK || model == PUCKAIR)
freedive = GAUGE;
unsigned int eop = array_uint16_le (data + 0x6B);
if (eop < layout->rb_profile_begin || eop >= layout->rb_profile_end) {
ERROR (context, "Ringbuffer pointer out of range (0x%04x).", eop);
return DC_STATUS_DATAFORMAT;
}
unsigned char *buffer = (unsigned char *) malloc (
layout->rb_profile_end - layout->rb_profile_begin +
layout->rb_freedives_end - layout->rb_freedives_begin);
if (buffer == NULL) {
ERROR (context, "Failed to allocate memory.");
return DC_STATUS_NOMEMORY;
}
memcpy (buffer + 0, data + eop, layout->rb_profile_end - eop);
memcpy (buffer + layout->rb_profile_end - eop, data + layout->rb_profile_begin, eop - layout->rb_profile_begin);
unsigned int nfreedives = 0;
unsigned int offset = layout->rb_profile_end - layout->rb_profile_begin;
while (offset >= 3) {
unsigned int extra = 0;
const unsigned char marker[3] = {0xAA, 0xBB, 0xCC};
if (memcmp (buffer + offset - 3, marker, sizeof (marker)) == 0) {
if (model == PUCKAIR)
extra = 7;
else
extra = 12;
}
if (offset < extra + 3)
break;
unsigned int mode = buffer[offset - extra - 1];
if (mode == 0xFF)
break;
unsigned int header_size = 53;
unsigned int sample_size = 2;
if (extra) {
if (model == PUCKAIR)
sample_size = 3;
else
sample_size = 5;
}
if (mode == freedive) {
header_size = 28;
sample_size = 6;
nfreedives++;
}
unsigned int nsamples = array_uint16_le (buffer + offset - extra - 3);
unsigned int nbytes = 2 + nsamples * sample_size + header_size + extra;
if (offset < nbytes)
break;
offset -= nbytes;
unsigned int length = array_uint16_le (buffer + offset);
if (length != nbytes) {
ERROR (context, "Calculated and stored size are not equal (%u %u).", length, nbytes);
free (buffer);
return DC_STATUS_DATAFORMAT;
}
if (mode == freedive && nfreedives == 1) {
unsigned int count = 0;
unsigned int idx = layout->rb_freedives_begin;
while (idx + 2 <= layout->rb_freedives_end &&
count != nsamples)
{
unsigned int sample = array_uint16_le (data + idx);
if (sample == 0)
count++;
idx += 2;
}
if (count != nsamples) {
ERROR (context, "Unexpected number of freedive sessions (%u %u).", count, nsamples);
free (buffer);
return DC_STATUS_DATAFORMAT;
}
memcpy (buffer + offset + nbytes, data + layout->rb_freedives_begin, idx - layout->rb_freedives_begin);
nbytes += idx - layout->rb_freedives_begin;
}
unsigned int fp_offset = offset + length - extra - FP_OFFSET;
if (fingerprint && memcmp (buffer + fp_offset, fingerprint, FP_SIZE) == 0) {
free (buffer);
return DC_STATUS_SUCCESS;
}
if (callback && !callback (buffer + offset, nbytes, buffer + fp_offset, FP_SIZE, userdata)) {
free (buffer);
return DC_STATUS_SUCCESS;
}
}
free (buffer);
return DC_STATUS_SUCCESS;
}