subrascasse

Sub Rascasse: an iPhone dive log for Subsurface cloud storage, with a pure-Swift git client (GPL-2.0)
git clone https://git.schurger.org/subrascasse.git
Log | Files | Refs | README | LICENSE

cressi_leonardo_parser.c (6566B)


      1 /*
      2  * libdivecomputer
      3  *
      4  * Copyright (C) 2013 Jef Driesen
      5  *
      6  * This library is free software; you can redistribute it and/or
      7  * modify it under the terms of the GNU Lesser General Public
      8  * License as published by the Free Software Foundation; either
      9  * version 2.1 of the License, or (at your option) any later version.
     10  *
     11  * This library is distributed in the hope that it will be useful,
     12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     14  * Lesser General Public License for more details.
     15  *
     16  * You should have received a copy of the GNU Lesser General Public
     17  * License along with this library; if not, write to the Free Software
     18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
     19  * MA 02110-1301 USA
     20  */
     21 
     22 #include <stdlib.h>
     23 
     24 #include "cressi_leonardo.h"
     25 #include "context-private.h"
     26 #include "parser-private.h"
     27 #include "array.h"
     28 
     29 #define ISINSTANCE(parser) dc_device_isinstance((parser), &cressi_leonardo_parser_vtable)
     30 
     31 #define SZ_HEADER 82
     32 
     33 #define DRAKE 6
     34 
     35 typedef struct cressi_leonardo_parser_t cressi_leonardo_parser_t;
     36 
     37 struct cressi_leonardo_parser_t {
     38 	dc_parser_t base;
     39 	unsigned int model;
     40 };
     41 
     42 static dc_status_t cressi_leonardo_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime);
     43 static dc_status_t cressi_leonardo_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value);
     44 static dc_status_t cressi_leonardo_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata);
     45 
     46 static const dc_parser_vtable_t cressi_leonardo_parser_vtable = {
     47 	sizeof(cressi_leonardo_parser_t),
     48 	DC_FAMILY_CRESSI_LEONARDO,
     49 	NULL, /* set_clock */
     50 	NULL, /* set_atmospheric */
     51 	NULL, /* set_density */
     52 	cressi_leonardo_parser_get_datetime, /* datetime */
     53 	cressi_leonardo_parser_get_field, /* fields */
     54 	cressi_leonardo_parser_samples_foreach, /* samples_foreach */
     55 	NULL /* destroy */
     56 };
     57 
     58 
     59 dc_status_t
     60 cressi_leonardo_parser_create (dc_parser_t **out, dc_context_t *context, const unsigned char data[], size_t size, unsigned int model)
     61 {
     62 	cressi_leonardo_parser_t *parser = NULL;
     63 
     64 	if (out == NULL)
     65 		return DC_STATUS_INVALIDARGS;
     66 
     67 	// Allocate memory.
     68 	parser = (cressi_leonardo_parser_t *) dc_parser_allocate (context, &cressi_leonardo_parser_vtable, data, size);
     69 	if (parser == NULL) {
     70 		ERROR (context, "Failed to allocate memory.");
     71 		return DC_STATUS_NOMEMORY;
     72 	}
     73 
     74 	parser->model = model;
     75 
     76 	*out = (dc_parser_t*) parser;
     77 
     78 	return DC_STATUS_SUCCESS;
     79 }
     80 
     81 
     82 static dc_status_t
     83 cressi_leonardo_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime)
     84 {
     85 	if (abstract->size < SZ_HEADER)
     86 		return DC_STATUS_DATAFORMAT;
     87 
     88 	const unsigned char *p = abstract->data;
     89 
     90 	if (datetime) {
     91 		datetime->year = p[8] + 2000;
     92 		datetime->month = p[9];
     93 		datetime->day = p[10];
     94 		datetime->hour = p[11];
     95 		datetime->minute = p[12];
     96 		datetime->second = 0;
     97 		datetime->timezone = DC_TIMEZONE_NONE;
     98 	}
     99 
    100 	return DC_STATUS_SUCCESS;
    101 }
    102 
    103 
    104 static dc_status_t
    105 cressi_leonardo_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value)
    106 {
    107 	cressi_leonardo_parser_t *parser = (cressi_leonardo_parser_t *) abstract;
    108 	if (abstract->size < SZ_HEADER)
    109 		return DC_STATUS_DATAFORMAT;
    110 
    111 	const unsigned char *data = abstract->data;
    112 
    113 	unsigned int interval = 20;
    114 	if (parser->model == DRAKE) {
    115 		interval = data[0x17];
    116 	}
    117 	if (interval == 0) {
    118 		ERROR(abstract->context, "Invalid sample interval");
    119 		return DC_STATUS_DATAFORMAT;
    120 	}
    121 
    122 	dc_gasmix_t *gasmix = (dc_gasmix_t *) value;
    123 
    124 	if (value) {
    125 		switch (type) {
    126 		case DC_FIELD_DIVETIME:
    127 			*((unsigned int *) value) = array_uint16_le (data + 0x06) * interval;
    128 			break;
    129 		case DC_FIELD_MAXDEPTH:
    130 			*((double *) value) = array_uint16_le (data + 0x20) / 10.0;
    131 			break;
    132 		case DC_FIELD_GASMIX_COUNT:
    133 			if (parser->model == DRAKE) {
    134 				*((unsigned int *) value) = 0;
    135 			} else {
    136 				*((unsigned int *) value) = 1;
    137 			}
    138 			break;
    139 		case DC_FIELD_GASMIX:
    140 			gasmix->usage = DC_USAGE_NONE;
    141 			gasmix->helium = 0.0;
    142 			gasmix->oxygen = data[0x19] / 100.0;
    143 			gasmix->nitrogen = 1.0 - gasmix->oxygen - gasmix->helium;
    144 			break;
    145 		case DC_FIELD_TEMPERATURE_MINIMUM:
    146 			*((double *) value) = data[0x22];
    147 			break;
    148 		default:
    149 			return DC_STATUS_UNSUPPORTED;
    150 		}
    151 	}
    152 
    153 	return DC_STATUS_SUCCESS;
    154 }
    155 
    156 
    157 static dc_status_t
    158 cressi_leonardo_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata)
    159 {
    160 	cressi_leonardo_parser_t *parser = (cressi_leonardo_parser_t *) abstract;
    161 	const unsigned char *data = abstract->data;
    162 	unsigned int size = abstract->size;
    163 
    164 	unsigned int time = 0;
    165 	unsigned int interval = 20;
    166 	if (parser->model == DRAKE) {
    167 		interval = data[0x17];
    168 	}
    169 	if (interval == 0) {
    170 		ERROR(abstract->context, "Invalid sample interval");
    171 		return DC_STATUS_DATAFORMAT;
    172 	}
    173 
    174 	unsigned int gasmix_previous = 0xFFFFFFFF;
    175 	unsigned int gasmix = 0;
    176 	if (parser->model == DRAKE) {
    177 		gasmix = gasmix_previous;
    178 	}
    179 
    180 	unsigned int offset = SZ_HEADER;
    181 	while (offset + 2 <= size) {
    182 		dc_sample_value_t sample = {0};
    183 
    184 		if (offset + 4 <= size &&
    185 			array_uint16_le (data + offset + 2) == 0xFF00)
    186 		{
    187 			unsigned int surftime = data[offset] + (data[offset + 1] & 0x07) * 60;
    188 
    189 			// Time (seconds).
    190 			time += surftime;
    191 			sample.time = time * 1000;
    192 			if (callback) callback (DC_SAMPLE_TIME, &sample, userdata);
    193 
    194 			// Depth (1/10 m).
    195 			sample.depth = 0.0;
    196 			if (callback) callback (DC_SAMPLE_DEPTH, &sample, userdata);
    197 
    198 			offset += 4;
    199 		} else {
    200 			unsigned int value = array_uint16_le (data + offset);
    201 			unsigned int depth = value & 0x07FF;
    202 			unsigned int ascent = (value & 0xC000) >> 14;
    203 
    204 			// Time (seconds).
    205 			time += interval;
    206 			sample.time = time * 1000;
    207 			if (callback) callback (DC_SAMPLE_TIME, &sample, userdata);
    208 
    209 			// Depth (1/10 m).
    210 			sample.depth = depth / 10.0;
    211 			if (callback) callback (DC_SAMPLE_DEPTH, &sample, userdata);
    212 
    213 			// Gas change.
    214 			if (gasmix != gasmix_previous) {
    215 				sample.gasmix = gasmix;
    216 				if (callback) callback (DC_SAMPLE_GASMIX, &sample, userdata);
    217 				gasmix_previous = gasmix;
    218 			}
    219 
    220 			// Ascent rate
    221 			if (ascent) {
    222 				sample.event.type = SAMPLE_EVENT_ASCENT;
    223 				sample.event.time = 0;
    224 				if (ascent < 3)
    225 					// Turn the overly sensitive ascent warnings of this dive computer into info events
    226 					sample.event.flags = SAMPLE_FLAGS_SEVERITY_INFO;
    227 				else
    228 					sample.event.flags = 0;
    229 				sample.event.value = ascent;
    230 				if (callback) callback (DC_SAMPLE_EVENT, &sample, userdata);
    231 			}
    232 
    233 			offset += 2;
    234 		}
    235 	}
    236 
    237 	return DC_STATUS_SUCCESS;
    238 }