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_edy_parser.c (8078B)


      1 /*
      2  * libdivecomputer
      3  *
      4  * Copyright (C) 2010 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_edy.h"
     25 #include "context-private.h"
     26 #include "parser-private.h"
     27 #include "array.h"
     28 
     29 #define ISINSTANCE(parser) dc_parser_isinstance((parser), &cressi_edy_parser_vtable)
     30 
     31 #define ARCHIMEDE 0x01
     32 #define IQ700 0x05
     33 #define EDY   0x08
     34 
     35 #define SZ_HEADER 32
     36 
     37 typedef struct cressi_edy_parser_t cressi_edy_parser_t;
     38 
     39 typedef struct cressi_edy_layout_t {
     40 	unsigned int datetime_y;
     41 	unsigned int datetime_md;
     42 	unsigned int datetime_hm;
     43 	unsigned int avgdepth;
     44 	unsigned int maxdepth;
     45 	unsigned int temperature;
     46 	unsigned int divetime;
     47 	unsigned int gasmix;
     48 	unsigned int gasmix_count;
     49 } cressi_edy_layout_t;
     50 
     51 struct cressi_edy_parser_t {
     52 	dc_parser_t base;
     53 	unsigned int model;
     54 	const cressi_edy_layout_t *layout;
     55 };
     56 
     57 static dc_status_t cressi_edy_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime);
     58 static dc_status_t cressi_edy_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value);
     59 static dc_status_t cressi_edy_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata);
     60 
     61 static const dc_parser_vtable_t cressi_edy_parser_vtable = {
     62 	sizeof(cressi_edy_parser_t),
     63 	DC_FAMILY_CRESSI_EDY,
     64 	NULL, /* set_clock */
     65 	NULL, /* set_atmospheric */
     66 	NULL, /* set_density */
     67 	cressi_edy_parser_get_datetime, /* datetime */
     68 	cressi_edy_parser_get_field, /* fields */
     69 	cressi_edy_parser_samples_foreach, /* samples_foreach */
     70 	NULL /* destroy */
     71 };
     72 
     73 static const cressi_edy_layout_t edy = {
     74 	 8, /* datetime_y */
     75 	10, /* datetime_md */
     76 	28, /* datetime_hm */
     77 	 1, /* avgdepth */
     78 	 5, /* maxdepth */
     79 	22, /* temperature */
     80 	25, /* divetime */
     81 	46, 3, /* gasmix */
     82 };
     83 
     84 static const cressi_edy_layout_t archimede = {
     85 	 2, /* datetime_y */
     86 	 5, /* datetime_md */
     87 	25, /* datetime_hm */
     88 	22, /* avgdepth */
     89 	 9, /* maxdepth */
     90 	45, /* temperature */
     91 	29, /* divetime */
     92 	43, 1, /* gasmix */
     93 };
     94 
     95 static unsigned int
     96 decode (const unsigned char data[], unsigned int offset, unsigned int n)
     97 {
     98 	unsigned int result = 0;
     99 
    100 	for (unsigned int i = 0; i < n; ++i) {
    101 		unsigned char byte = data[offset / 2];
    102 
    103 		unsigned char nibble = 0;
    104 		if ((offset & 1) == 0) {
    105 			nibble = (byte >> 4) & 0x0F;
    106 		} else {
    107 			nibble = byte & 0x0F;
    108 		}
    109 
    110 		result *= 10;
    111 		result += nibble;
    112 		offset++;
    113 	}
    114 
    115 	return result;
    116 }
    117 
    118 static unsigned int
    119 cressi_edy_parser_count_gasmixes (const unsigned char data[], const cressi_edy_layout_t *layout)
    120 {
    121 	// Count the number of active gas mixes. The active gas
    122 	// mixes are always first, so we stop counting as soon
    123 	// as the first gas marked as disabled is found.
    124 	unsigned int i = 0;
    125 	while (i < layout->gasmix_count) {
    126 		unsigned int state = decode(data, layout->gasmix - i * 2, 1);
    127 		if (state == 0x0F)
    128 			break;
    129 		i++;
    130 	}
    131 	return i;
    132 }
    133 
    134 dc_status_t
    135 cressi_edy_parser_create (dc_parser_t **out, dc_context_t *context, const unsigned char data[], size_t size, unsigned int model)
    136 {
    137 	cressi_edy_parser_t *parser = NULL;
    138 
    139 	if (out == NULL)
    140 		return DC_STATUS_INVALIDARGS;
    141 
    142 	// Allocate memory.
    143 	parser = (cressi_edy_parser_t *) dc_parser_allocate (context, &cressi_edy_parser_vtable, data, size);
    144 	if (parser == NULL) {
    145 		ERROR (context, "Failed to allocate memory.");
    146 		return DC_STATUS_NOMEMORY;
    147 	}
    148 
    149 	// Set the default values.
    150 	parser->model = model;
    151 
    152 	if (model == ARCHIMEDE) {
    153 		parser->layout = &archimede;
    154 	} else {
    155 		parser->layout = &edy;
    156 	}
    157 
    158 	*out = (dc_parser_t*) parser;
    159 
    160 	return DC_STATUS_SUCCESS;
    161 }
    162 
    163 
    164 static dc_status_t
    165 cressi_edy_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime)
    166 {
    167 	cressi_edy_parser_t *parser = (cressi_edy_parser_t *) abstract;
    168 	const cressi_edy_layout_t *layout = parser->layout;
    169 	const unsigned char *data = abstract->data;
    170 
    171 	if (abstract->size < SZ_HEADER)
    172 		return DC_STATUS_DATAFORMAT;
    173 
    174 	if (datetime) {
    175 		datetime->year   = decode(data, layout->datetime_y, 2) + 2000;
    176 		datetime->month  = decode(data, layout->datetime_md, 1);
    177 		datetime->day    = decode(data, layout->datetime_md + 1, 2);
    178 		datetime->hour   = decode(data, layout->datetime_hm, 2);
    179 		datetime->minute = decode(data, layout->datetime_hm + 2, 2);
    180 		datetime->second = 0;
    181 		datetime->timezone = DC_TIMEZONE_NONE;
    182 	}
    183 
    184 	return DC_STATUS_SUCCESS;
    185 }
    186 
    187 
    188 static dc_status_t
    189 cressi_edy_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value)
    190 {
    191 	cressi_edy_parser_t *parser = (cressi_edy_parser_t *) abstract;
    192 	const cressi_edy_layout_t *layout = parser->layout;
    193 	const unsigned char *data = abstract->data;
    194 
    195 	if (abstract->size < SZ_HEADER)
    196 		return DC_STATUS_DATAFORMAT;
    197 
    198 	dc_gasmix_t *gasmix = (dc_gasmix_t *) value;
    199 
    200 	if (value) {
    201 		switch (type) {
    202 		case DC_FIELD_DIVETIME:
    203 			if (parser->model == EDY)
    204 				*((unsigned int *) value) = decode(data, layout->divetime, 1) * 60 + decode(data, layout->divetime + 1, 2);
    205 			else
    206 				*((unsigned int *) value) = decode(data, layout->divetime, 3) * 60;
    207 			break;
    208 		case DC_FIELD_MAXDEPTH:
    209 			*((double *) value) = decode(data, layout->maxdepth, 3) / 10.0;
    210 			break;
    211 		case DC_FIELD_AVGDEPTH:
    212 			*((double *) value) = decode(data, layout->avgdepth, 3) / 10.0;
    213 			break;
    214 		case DC_FIELD_GASMIX_COUNT:
    215 			*((unsigned int *) value) = cressi_edy_parser_count_gasmixes(data, layout);
    216 			break;
    217 		case DC_FIELD_GASMIX:
    218 			gasmix->usage = DC_USAGE_NONE;
    219 			gasmix->helium = 0.0;
    220 			gasmix->oxygen = decode(data, layout->gasmix - flags * 2, 2) / 100.0;
    221 			gasmix->nitrogen = 1.0 - gasmix->oxygen - gasmix->helium;
    222 			break;
    223 		case DC_FIELD_TEMPERATURE_MINIMUM:
    224 			*((double *) value) = decode(data, layout->temperature, 3) / 10.0;
    225 			break;
    226 		default:
    227 			return DC_STATUS_UNSUPPORTED;
    228 		}
    229 	}
    230 
    231 	return DC_STATUS_SUCCESS;
    232 }
    233 
    234 
    235 static dc_status_t
    236 cressi_edy_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata)
    237 {
    238 	cressi_edy_parser_t *parser = (cressi_edy_parser_t *) abstract;
    239 	const cressi_edy_layout_t *layout = parser->layout;
    240 
    241 	const unsigned char *data = abstract->data;
    242 	unsigned int size = abstract->size;
    243 
    244 	unsigned int time = 0;
    245 	unsigned int interval = 30;
    246 	if (parser->model == EDY) {
    247 		interval = 1;
    248 	} else if (parser->model == IQ700) {
    249 		if (data[0x07] & 0x40)
    250 			interval = 15;
    251 	}
    252 
    253 	unsigned int ngasmixes = cressi_edy_parser_count_gasmixes(data, layout);
    254 	unsigned int gasmix = 0xFFFFFFFF;
    255 
    256 	unsigned int offset = SZ_HEADER;
    257 	while (offset + 2 <= size) {
    258 		dc_sample_value_t sample = {0};
    259 
    260 		if (data[offset] == 0xFF)
    261 			break;
    262 
    263 		unsigned int extra = 0;
    264 		if (data[offset] & 0x80)
    265 			extra = 4;
    266 
    267 		// Time (seconds).
    268 		time += interval;
    269 		sample.time = time * 1000;
    270 		if (callback) callback (DC_SAMPLE_TIME, &sample, userdata);
    271 
    272 		// Depth (1/10 m).
    273 		unsigned int depth = decode(data + offset, 1, 3);
    274 		sample.depth = depth / 10.0;
    275 		if (callback) callback (DC_SAMPLE_DEPTH, &sample, userdata);
    276 
    277 		// Current gasmix
    278 		if (ngasmixes) {
    279 			unsigned int idx = (data[offset + 0] & 0x60) >> 5;
    280 			if (parser->model == IQ700 || parser->model == ARCHIMEDE)
    281 				idx = 0; /* FIXME */
    282 			if (idx >= ngasmixes) {
    283 				ERROR (abstract->context, "Invalid gas mix index.");
    284 				return DC_STATUS_DATAFORMAT;
    285 			}
    286 			if (idx != gasmix) {
    287 				sample.gasmix = idx;
    288 				if (callback) callback (DC_SAMPLE_GASMIX, &sample, userdata);
    289 				gasmix = idx;
    290 			}
    291 		}
    292 
    293 		offset += 2 + extra;
    294 	}
    295 
    296 	return DC_STATUS_SUCCESS;
    297 }