libdivecomputer/src/shearwater_predator_parser.c
Dirk Hohndel 3917ae15b9 Convert decostop / ndl to samples
Having these as events seems less useful since for many dive computers
there are data with every sample - so it makes much more sense to have
these as part of the sample.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2012-12-11 20:38:08 +01:00

271 lines
7.4 KiB
C

/*
* libdivecomputer
*
* Copyright (C) 2012 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 <libdivecomputer/shearwater_predator.h>
#include <libdivecomputer/units.h>
#include "context-private.h"
#include "parser-private.h"
#include "array.h"
#define SZ_BLOCK 0x80
#define SZ_SAMPLE 0x10
#define METRIC 0
#define IMPERIAL 1
typedef struct shearwater_predator_parser_t shearwater_predator_parser_t;
struct shearwater_predator_parser_t {
dc_parser_t base;
};
static dc_status_t shearwater_predator_parser_set_data (dc_parser_t *abstract, const unsigned char *data, unsigned int size);
static dc_status_t shearwater_predator_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime);
static dc_status_t shearwater_predator_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value);
static dc_status_t shearwater_predator_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata);
static dc_status_t shearwater_predator_parser_destroy (dc_parser_t *abstract);
static const parser_backend_t shearwater_predator_parser_backend = {
DC_FAMILY_SHEARWATER_PREDATOR,
shearwater_predator_parser_set_data, /* set_data */
shearwater_predator_parser_get_datetime, /* datetime */
shearwater_predator_parser_get_field, /* fields */
shearwater_predator_parser_samples_foreach, /* samples_foreach */
shearwater_predator_parser_destroy /* destroy */
};
static int
parser_is_shearwater_predator (dc_parser_t *abstract)
{
if (abstract == NULL)
return 0;
return abstract->backend == &shearwater_predator_parser_backend;
}
dc_status_t
shearwater_predator_parser_create (dc_parser_t **out, dc_context_t *context)
{
if (out == NULL)
return DC_STATUS_INVALIDARGS;
// Allocate memory.
shearwater_predator_parser_t *parser = (shearwater_predator_parser_t *) malloc (sizeof (shearwater_predator_parser_t));
if (parser == NULL) {
ERROR (context, "Failed to allocate memory.");
return DC_STATUS_NOMEMORY;
}
// Initialize the base class.
parser_init (&parser->base, context, &shearwater_predator_parser_backend);
*out = (dc_parser_t *) parser;
return DC_STATUS_SUCCESS;
}
static dc_status_t
shearwater_predator_parser_destroy (dc_parser_t *abstract)
{
if (! parser_is_shearwater_predator (abstract))
return DC_STATUS_INVALIDARGS;
// Free memory.
free (abstract);
return DC_STATUS_SUCCESS;
}
static dc_status_t
shearwater_predator_parser_set_data (dc_parser_t *abstract, const unsigned char *data, unsigned int size)
{
if (! parser_is_shearwater_predator (abstract))
return DC_STATUS_INVALIDARGS;
return DC_STATUS_SUCCESS;
}
static dc_status_t
shearwater_predator_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime)
{
const unsigned char *data = abstract->data;
unsigned int size = abstract->size;
if (size < 2 * SZ_BLOCK)
return DC_STATUS_DATAFORMAT;
unsigned int ticks = array_uint32_be (data + 12);
if (!dc_datetime_localtime (datetime, ticks))
return DC_STATUS_DATAFORMAT;
return DC_STATUS_SUCCESS;
}
static dc_status_t
shearwater_predator_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value)
{
const unsigned char *data = abstract->data;
unsigned int size = abstract->size;
if (size < 2 * SZ_BLOCK)
return DC_STATUS_DATAFORMAT;
// Get the unit system.
unsigned int units = data[8];
dc_gasmix_t *gasmix = (dc_gasmix_t *) value;
dc_salinity_t *water = (dc_salinity_t *) value;
unsigned int density = 0;
if (value) {
switch (type) {
case DC_FIELD_DIVETIME:
*((unsigned int *) value) = array_uint16_be (data + size - SZ_BLOCK + 6) * 60;
break;
case DC_FIELD_MAXDEPTH:
if (units == IMPERIAL)
*((double *) value) = array_uint16_be (data + size - SZ_BLOCK + 4) * FEET;
else
*((double *) value) = array_uint16_be (data + size - SZ_BLOCK + 4);
break;
case DC_FIELD_GASMIX_COUNT:
*((unsigned int *) value) = 10;
break;
case DC_FIELD_GASMIX:
gasmix->oxygen = data[20 + flags] / 100.0;
gasmix->helium = data[30 + flags] / 100.0;
gasmix->nitrogen = 1.0 - gasmix->oxygen - gasmix->helium;
break;
case DC_FIELD_SALINITY:
density = array_uint16_be (data + 83);
if (density == 1000)
water->type = DC_WATER_FRESH;
else
water->type = DC_WATER_SALT;
water->density = density;
break;
case DC_FIELD_ATMOSPHERIC:
*((double *) value) = array_uint16_be (data + 47) / 1000.0;
break;
default:
return DC_STATUS_UNSUPPORTED;
}
}
return DC_STATUS_SUCCESS;
}
static dc_status_t
shearwater_predator_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata)
{
const unsigned char *data = abstract->data;
unsigned int size = abstract->size;
if (size < 2 * SZ_BLOCK)
return DC_STATUS_DATAFORMAT;
// Get the unit system.
unsigned int units = data[8];
// Previous gas mix.
unsigned int o2_previous = 0, he_previous = 0;
unsigned int time = 0;
unsigned int offset = SZ_BLOCK;
while (offset + SZ_BLOCK < size) {
dc_sample_value_t sample = {0};
// Ignore empty samples.
if (array_isequal (data + offset, SZ_SAMPLE, 0x00)) {
offset += SZ_SAMPLE;
continue;
}
// Time (seconds).
time += 10;
sample.time = time;
if (callback) callback (DC_SAMPLE_TIME, sample, userdata);
// Depth (1/10 m or ft).
unsigned int depth = array_uint16_be (data + offset);
if (units == IMPERIAL)
sample.depth = depth * FEET / 10.0;
else
sample.depth = depth / 10.0;
if (callback) callback (DC_SAMPLE_DEPTH, sample, userdata);
// Temperature (°C or °F).
unsigned int temperature = data[offset + 13];
if (units == IMPERIAL)
sample.temperature = (temperature - 32.0) * (5.0 / 9.0);
else
sample.temperature = temperature;
if (callback) callback (DC_SAMPLE_TEMPERATURE, sample, userdata);
// PPO2
sample.ppo2 = data[offset + 6] / 100.0;
if (callback) callback (DC_SAMPLE_PPO2, sample, userdata);
// Gaschange.
unsigned int o2 = data[offset + 7];
unsigned int he = data[offset + 8];
if (o2 != o2_previous || he != he_previous) {
sample.event.type = SAMPLE_EVENT_GASCHANGE2;
sample.event.time = 0;
sample.event.flags = 0;
sample.event.value = o2 | (he << 16);
if (callback) callback (DC_SAMPLE_EVENT, sample, userdata);
o2_previous = o2;
he_previous = he;
}
// Deco stop / NDL.
unsigned int decostop = array_uint16_be (data + offset + 2);
if (decostop) {
sample.deco.type = DC_DECO_DECOSTOP;
if (units == IMPERIAL)
sample.deco.depth = decostop * FEET;
else
sample.deco.depth = decostop;
} else {
sample.deco.type = DC_DECO_NDL;
sample.deco.depth = 0.0;
}
sample.deco.time = data[offset + 9] * 60;
if (callback) callback (DC_SAMPLE_DECO, sample, userdata);
offset += SZ_SAMPLE;
}
return DC_STATUS_SUCCESS;
}