libdivecomputer/src/mares_darwin_parser.c
Jef Driesen 53e9d72a40 Integrate the context object in the library.
The public api is changed to require a context object for all
operations. Because other library objects store the context pointer
internally, only the constructor functions need an explicit context
object as a parameter.
2012-08-27 23:08:38 +02:00

253 lines
6.7 KiB
C

/*
* libdivecomputer
*
* Copyright (C) 2011 Jef Driesen
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
#include <stdlib.h>
#include <string.h>
#include <libdivecomputer/mares_darwin.h>
#include <libdivecomputer/units.h>
#include "context-private.h"
#include "parser-private.h"
#include "array.h"
#define DARWIN 0
#define DARWINAIR 1
typedef struct mares_darwin_parser_t mares_darwin_parser_t;
struct mares_darwin_parser_t {
dc_parser_t base;
unsigned int headersize;
unsigned int samplesize;
};
static dc_status_t mares_darwin_parser_set_data (dc_parser_t *abstract, const unsigned char *data, unsigned int size);
static dc_status_t mares_darwin_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime);
static dc_status_t mares_darwin_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value);
static dc_status_t mares_darwin_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata);
static dc_status_t mares_darwin_parser_destroy (dc_parser_t *abstract);
static const parser_backend_t mares_darwin_parser_backend = {
DC_FAMILY_MARES_DARWIN,
mares_darwin_parser_set_data, /* set_data */
mares_darwin_parser_get_datetime, /* datetime */
mares_darwin_parser_get_field, /* fields */
mares_darwin_parser_samples_foreach, /* samples_foreach */
mares_darwin_parser_destroy /* destroy */
};
static int
parser_is_mares_darwin (dc_parser_t *abstract)
{
if (abstract == NULL)
return 0;
return abstract->backend == &mares_darwin_parser_backend;
}
dc_status_t
mares_darwin_parser_create (dc_parser_t **out, dc_context_t *context, unsigned int model)
{
if (out == NULL)
return DC_STATUS_INVALIDARGS;
// Allocate memory.
mares_darwin_parser_t *parser = (mares_darwin_parser_t *) malloc (sizeof (mares_darwin_parser_t));
if (parser == NULL) {
ERROR (context, "Failed to allocate memory.");
return DC_STATUS_NOMEMORY;
}
// Initialize the base class.
parser_init (&parser->base, &mares_darwin_parser_backend);
if (model == DARWINAIR) {
parser->headersize = 60;
parser->samplesize = 3;
} else {
parser->headersize = 52;
parser->samplesize = 2;
}
*out = (dc_parser_t *) parser;
return DC_STATUS_SUCCESS;
}
static dc_status_t
mares_darwin_parser_destroy (dc_parser_t *abstract)
{
if (! parser_is_mares_darwin (abstract))
return DC_STATUS_INVALIDARGS;
// Free memory.
free (abstract);
return DC_STATUS_SUCCESS;
}
static dc_status_t
mares_darwin_parser_set_data (dc_parser_t *abstract, const unsigned char *data, unsigned int size)
{
return DC_STATUS_SUCCESS;
}
static dc_status_t
mares_darwin_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime)
{
mares_darwin_parser_t *parser = (mares_darwin_parser_t *) abstract;
if (abstract->size < parser->headersize)
return DC_STATUS_DATAFORMAT;
const unsigned char *p = abstract->data;
if (datetime) {
datetime->year = array_uint16_be (p);
datetime->month = p[2];
datetime->day = p[3];
datetime->hour = p[4];
datetime->minute = p[5];
datetime->second = 0;
}
return DC_STATUS_SUCCESS;
}
static dc_status_t
mares_darwin_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value)
{
mares_darwin_parser_t *parser = (mares_darwin_parser_t *) abstract;
if (abstract->size < parser->headersize)
return DC_STATUS_DATAFORMAT;
const unsigned char *p = abstract->data;
dc_gasmix_t *gasmix = (dc_gasmix_t *) value;
if (value) {
switch (type) {
case DC_FIELD_DIVETIME:
*((unsigned int *) value) = array_uint16_be (p + 0x06) * 20;
break;
case DC_FIELD_MAXDEPTH:
*((double *) value) = array_uint16_be (p + 0x08) / 10.0;
break;
case DC_FIELD_GASMIX_COUNT:
*((unsigned int *) value) = 1;
break;
case DC_FIELD_GASMIX:
gasmix->helium = 0.0;
gasmix->oxygen = 0.21;
gasmix->nitrogen = 1.0 - gasmix->oxygen - gasmix->helium;
break;
default:
return DC_STATUS_UNSUPPORTED;
}
}
return DC_STATUS_SUCCESS;
}
static dc_status_t
mares_darwin_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata)
{
mares_darwin_parser_t *parser = (mares_darwin_parser_t *) abstract;
if (abstract->size < parser->headersize)
return DC_STATUS_DATAFORMAT;
unsigned int time = 0;
unsigned int pressure = array_uint16_be (abstract->data + 0x17);
unsigned int offset = parser->headersize;
while (offset + parser->samplesize <= abstract->size) {
dc_sample_value_t sample = {0};
unsigned int value = array_uint16_le (abstract->data + offset);
unsigned int depth = value & 0x07FF;
unsigned int ascent = (value & 0xE000) >> 13;
unsigned int violation = (value & 0x1000) >> 12;
unsigned int deco = (value & 0x0800) >> 11;
// Surface Time (seconds).
time += 20;
sample.time = time;
if (callback) callback (DC_SAMPLE_TIME, sample, userdata);
// Depth (1/10 m).
sample.depth = depth / 10.0;
if (callback) callback (DC_SAMPLE_DEPTH, sample, userdata);
// Ascent rate
if (ascent) {
sample.event.type = SAMPLE_EVENT_ASCENT;
sample.event.time = 0;
sample.event.flags = 0;
sample.event.value = ascent;
if (callback) callback (DC_SAMPLE_EVENT, sample, userdata);
}
// Deco violation
if (violation) {
sample.event.type = SAMPLE_EVENT_CEILING;
sample.event.time = 0;
sample.event.flags = 0;
sample.event.value = 0;
if (callback) callback (DC_SAMPLE_EVENT, sample, userdata);
}
// Deco stop
if (deco) {
sample.event.type = SAMPLE_EVENT_DECOSTOP;
sample.event.time = 0;
sample.event.flags = 0;
sample.event.value = 0;
if (callback) callback (DC_SAMPLE_EVENT, sample, userdata);
}
if (parser->samplesize == 3) {
unsigned int type = (time / 20 + 2) % 3;
if (type == 0) {
// Tank Pressure (bar)
pressure -= abstract->data[offset + 2];
sample.pressure.tank = 0;
sample.pressure.value = pressure;
if (callback) callback (DC_SAMPLE_PRESSURE, sample, userdata);
}
}
offset += parser->samplesize;
}
return DC_STATUS_SUCCESS;
}