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

oceanic_veo250_parser.c (7614B)


      1 /*
      2  * libdivecomputer
      3  *
      4  * Copyright (C) 2009 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 <libdivecomputer/units.h>
     25 
     26 #include "oceanic_veo250.h"
     27 #include "oceanic_common.h"
     28 #include "context-private.h"
     29 #include "parser-private.h"
     30 #include "array.h"
     31 
     32 #define ISINSTANCE(parser) dc_parser_isinstance((parser), &oceanic_veo250_parser_vtable)
     33 
     34 typedef struct oceanic_veo250_parser_t oceanic_veo250_parser_t;
     35 
     36 struct oceanic_veo250_parser_t {
     37 	dc_parser_t base;
     38 	unsigned int model;
     39 	// Cached fields.
     40 	unsigned int cached;
     41 	unsigned int divetime;
     42 	double maxdepth;
     43 };
     44 
     45 static dc_status_t oceanic_veo250_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime);
     46 static dc_status_t oceanic_veo250_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value);
     47 static dc_status_t oceanic_veo250_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata);
     48 
     49 static const dc_parser_vtable_t oceanic_veo250_parser_vtable = {
     50 	sizeof(oceanic_veo250_parser_t),
     51 	DC_FAMILY_OCEANIC_VEO250,
     52 	NULL, /* set_clock */
     53 	NULL, /* set_atmospheric */
     54 	NULL, /* set_density */
     55 	oceanic_veo250_parser_get_datetime, /* datetime */
     56 	oceanic_veo250_parser_get_field, /* fields */
     57 	oceanic_veo250_parser_samples_foreach, /* samples_foreach */
     58 	NULL /* destroy */
     59 };
     60 
     61 
     62 dc_status_t
     63 oceanic_veo250_parser_create (dc_parser_t **out, dc_context_t *context, const unsigned char data[], size_t size, unsigned int model)
     64 {
     65 	oceanic_veo250_parser_t *parser = NULL;
     66 
     67 	if (out == NULL)
     68 		return DC_STATUS_INVALIDARGS;
     69 
     70 	// Allocate memory.
     71 	parser = (oceanic_veo250_parser_t *) dc_parser_allocate (context, &oceanic_veo250_parser_vtable, data, size);
     72 	if (parser == NULL) {
     73 		ERROR (context, "Failed to allocate memory.");
     74 		return DC_STATUS_NOMEMORY;
     75 	}
     76 
     77 	// Set the default values.
     78 	parser->model = model;
     79 	parser->cached = 0;
     80 	parser->divetime = 0;
     81 	parser->maxdepth = 0.0;
     82 
     83 	*out = (dc_parser_t*) parser;
     84 
     85 	return DC_STATUS_SUCCESS;
     86 }
     87 
     88 
     89 static dc_status_t
     90 oceanic_veo250_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime)
     91 {
     92 	oceanic_veo250_parser_t *parser = (oceanic_veo250_parser_t *) abstract;
     93 
     94 	if (abstract->size < 8)
     95 		return DC_STATUS_DATAFORMAT;
     96 
     97 	const unsigned char *p = abstract->data;
     98 
     99 	if (datetime) {
    100 		datetime->year   = ((p[5] & 0xF0) >> 4) + ((p[1] & 0xE0) >> 1) + 2000;
    101 		datetime->month  = ((p[7] & 0xF0) >> 4);
    102 		datetime->day    = p[1] & 0x1F;
    103 		datetime->hour   = p[3];
    104 		datetime->minute = p[2];
    105 		datetime->second = 0;
    106 		datetime->timezone = DC_TIMEZONE_NONE;
    107 
    108 		if (parser->model == VEO200 || parser->model == VEO250)
    109 			datetime->year += 3;
    110 		else if (parser->model == REACTPRO)
    111 			datetime->year += 2;
    112 	}
    113 
    114 	return DC_STATUS_SUCCESS;
    115 }
    116 
    117 
    118 static dc_status_t
    119 oceanic_veo250_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value)
    120 {
    121 	oceanic_veo250_parser_t *parser = (oceanic_veo250_parser_t *) abstract;
    122 
    123 	const unsigned char *data = abstract->data;
    124 	unsigned int size = abstract->size;
    125 
    126 	if (size < 7 * PAGESIZE / 2)
    127 		return DC_STATUS_DATAFORMAT;
    128 
    129 	if (!parser->cached) {
    130 		sample_statistics_t statistics = SAMPLE_STATISTICS_INITIALIZER;
    131 		dc_status_t rc = oceanic_veo250_parser_samples_foreach (
    132 			abstract, sample_statistics_cb, &statistics);
    133 		if (rc != DC_STATUS_SUCCESS)
    134 			return rc;
    135 
    136 		parser->cached = 1;
    137 		parser->divetime = statistics.divetime;
    138 		parser->maxdepth = statistics.maxdepth;
    139 	}
    140 
    141 	unsigned int footer = size - PAGESIZE;
    142 
    143 	dc_gasmix_t *gasmix = (dc_gasmix_t *) value;
    144 
    145 	if (value) {
    146 		switch (type) {
    147 		case DC_FIELD_DIVETIME:
    148 			*((unsigned int *) value) = data[footer + 3] * 60 + data[footer + 4] * 3600;
    149 			break;
    150 		case DC_FIELD_MAXDEPTH:
    151 			*((double *) value) = parser->maxdepth;
    152 			break;
    153 		case DC_FIELD_GASMIX_COUNT:
    154 				*((unsigned int *) value) = 1;
    155 			break;
    156 		case DC_FIELD_GASMIX:
    157 			gasmix->usage = DC_USAGE_NONE;
    158 			gasmix->helium = 0.0;
    159 			if (data[footer + 6])
    160 				gasmix->oxygen = data[footer + 6] / 100.0;
    161 			else
    162 				gasmix->oxygen = 0.21;
    163 			gasmix->nitrogen = 1.0 - gasmix->oxygen - gasmix->helium;
    164 			break;
    165 		default:
    166 			return DC_STATUS_UNSUPPORTED;
    167 		}
    168 	}
    169 
    170 	return DC_STATUS_SUCCESS;
    171 }
    172 
    173 
    174 static dc_status_t
    175 oceanic_veo250_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata)
    176 {
    177 	oceanic_veo250_parser_t *parser = (oceanic_veo250_parser_t *) abstract;
    178 	const unsigned char *data = abstract->data;
    179 	unsigned int size = abstract->size;
    180 
    181 	if (size < 7 * PAGESIZE / 2)
    182 		return DC_STATUS_DATAFORMAT;
    183 
    184 	unsigned int time = 0;
    185 	unsigned int interval = 0;
    186 	unsigned int interval_idx = data[0x27] & 0x03;
    187 	if (parser->model == REACTPRO || parser->model == REACTPROWHITE) {
    188 		interval_idx += 1;
    189 		interval_idx %= 4;
    190 	}
    191 	switch (interval_idx) {
    192 	case 0:
    193 		interval = 2;
    194 		break;
    195 	case 1:
    196 		interval = 15;
    197 		break;
    198 	case 2:
    199 		interval = 30;
    200 		break;
    201 	case 3:
    202 		interval = 60;
    203 		break;
    204 	}
    205 
    206 	unsigned int offset = 5 * PAGESIZE / 2;
    207 	while (offset + PAGESIZE / 2 <= size - PAGESIZE) {
    208 		dc_sample_value_t sample = {0};
    209 
    210 		// Ignore empty samples.
    211 		if (array_isequal (data + offset, PAGESIZE / 2, 0x00)) {
    212 			offset += PAGESIZE / 2;
    213 			continue;
    214 		}
    215 
    216 		// Time.
    217 		time += interval;
    218 		sample.time = time * 1000;
    219 		if (callback) callback (DC_SAMPLE_TIME, &sample, userdata);
    220 
    221 		// Vendor specific data
    222 		sample.vendor.type = SAMPLE_VENDOR_OCEANIC_VEO250;
    223 		sample.vendor.size = PAGESIZE / 2;
    224 		sample.vendor.data = data + offset;
    225 		if (callback) callback (DC_SAMPLE_VENDOR, &sample, userdata);
    226 
    227 		// Depth (ft)
    228 		unsigned int depth = data[offset + 2];
    229 		sample.depth = depth * FEET;
    230 		if (callback) callback (DC_SAMPLE_DEPTH, &sample, userdata);
    231 
    232 		// Temperature (°F)
    233 		unsigned int temperature;
    234 		if (parser->model == REACTPRO || parser->model == REACTPROWHITE ||
    235 			parser->model == INSIGHT) {
    236 			temperature = data[offset + 6];
    237 		} else {
    238 			temperature = data[offset + 7];
    239 		}
    240 		sample.temperature = (temperature - 32.0) * (5.0 / 9.0);
    241 		if (callback) callback (DC_SAMPLE_TEMPERATURE, &sample, userdata);
    242 
    243 		// NDL / Deco
    244 		unsigned int have_deco = 0;
    245 		unsigned int decostop = 0, decotime = 0;
    246 		if (parser->model == REACTPRO || parser->model == REACTPROWHITE ||
    247 			parser->model == INSIGHT) {
    248 			decostop = (data[offset + 7] & 0xF0) >> 4;
    249 			decotime = ((data[offset + 3] & 0xC0) << 2) | data[offset + 4];
    250 			have_deco = 1;
    251 		} else {
    252 			decostop = (data[offset + 5] & 0xF0) >> 4;
    253 			decotime = array_uint16_le(data + offset + 4) & 0x0FFF;
    254 			have_deco = 1;
    255 		}
    256 		if (have_deco) {
    257 			if (decostop) {
    258 				sample.deco.type = DC_DECO_DECOSTOP;
    259 				sample.deco.depth = decostop * 10 * FEET;
    260 			} else {
    261 				sample.deco.type = DC_DECO_NDL;
    262 				sample.deco.depth = 0.0;
    263 			}
    264 			sample.deco.time = decotime * 60;
    265 			sample.deco.tts = 0;
    266 			if (callback) callback (DC_SAMPLE_DECO, &sample, userdata);
    267 		}
    268 
    269 		offset += PAGESIZE / 2;
    270 	}
    271 
    272 	return DC_STATUS_SUCCESS;
    273 }