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

reefnet_sensuspro_parser.c (7833B)


      1 /*
      2  * libdivecomputer
      3  *
      4  * Copyright (C) 2008 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 #include <string.h>	// memcmp
     24 
     25 #include <libdivecomputer/units.h>
     26 
     27 #include "reefnet_sensuspro.h"
     28 #include "context-private.h"
     29 #include "parser-private.h"
     30 #include "array.h"
     31 
     32 #define ISINSTANCE(parser) dc_parser_isinstance((parser), &reefnet_sensuspro_parser_vtable)
     33 
     34 typedef struct reefnet_sensuspro_parser_t reefnet_sensuspro_parser_t;
     35 
     36 struct reefnet_sensuspro_parser_t {
     37 	dc_parser_t base;
     38 	// Depth calibration.
     39 	double atmospheric;
     40 	double hydrostatic;
     41 	// Clock synchronization.
     42 	unsigned int devtime;
     43 	dc_ticks_t systime;
     44 	// Cached fields.
     45 	unsigned int cached;
     46 	unsigned int divetime;
     47 	unsigned int maxdepth;
     48 };
     49 
     50 static dc_status_t reefnet_sensuspro_parser_set_clock (dc_parser_t *abstract, unsigned int devtime, dc_ticks_t systime);
     51 static dc_status_t reefnet_sensuspro_parser_set_atmospheric (dc_parser_t *abstract, double atmospheric);
     52 static dc_status_t reefnet_sensuspro_parser_set_density (dc_parser_t *abstract, double density);
     53 static dc_status_t reefnet_sensuspro_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime);
     54 static dc_status_t reefnet_sensuspro_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value);
     55 static dc_status_t reefnet_sensuspro_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata);
     56 
     57 static const dc_parser_vtable_t reefnet_sensuspro_parser_vtable = {
     58 	sizeof(reefnet_sensuspro_parser_t),
     59 	DC_FAMILY_REEFNET_SENSUSPRO,
     60 	reefnet_sensuspro_parser_set_clock, /* set_clock */
     61 	reefnet_sensuspro_parser_set_atmospheric, /* set_atmospheric */
     62 	reefnet_sensuspro_parser_set_density, /* set_density */
     63 	reefnet_sensuspro_parser_get_datetime, /* datetime */
     64 	reefnet_sensuspro_parser_get_field, /* fields */
     65 	reefnet_sensuspro_parser_samples_foreach, /* samples_foreach */
     66 	NULL /* destroy */
     67 };
     68 
     69 
     70 dc_status_t
     71 reefnet_sensuspro_parser_create (dc_parser_t **out, dc_context_t *context, const unsigned char data[], size_t size)
     72 {
     73 	reefnet_sensuspro_parser_t *parser = NULL;
     74 
     75 	if (out == NULL)
     76 		return DC_STATUS_INVALIDARGS;
     77 
     78 	// Allocate memory.
     79 	parser = (reefnet_sensuspro_parser_t *) dc_parser_allocate (context, &reefnet_sensuspro_parser_vtable, data, size);
     80 	if (parser == NULL) {
     81 		ERROR (context, "Failed to allocate memory.");
     82 		return DC_STATUS_NOMEMORY;
     83 	}
     84 
     85 	// Set the default values.
     86 	parser->atmospheric = DEF_ATMOSPHERIC;
     87 	parser->hydrostatic = DEF_DENSITY_SALT * GRAVITY;
     88 	parser->devtime = 0;
     89 	parser->systime = 0;
     90 	parser->cached = 0;
     91 	parser->divetime = 0;
     92 	parser->maxdepth = 0;
     93 
     94 	*out = (dc_parser_t*) parser;
     95 
     96 	return DC_STATUS_SUCCESS;
     97 }
     98 
     99 
    100 static dc_status_t
    101 reefnet_sensuspro_parser_set_clock (dc_parser_t *abstract, unsigned int devtime, dc_ticks_t systime)
    102 {
    103 	reefnet_sensuspro_parser_t *parser = (reefnet_sensuspro_parser_t *) abstract;
    104 
    105 	parser->devtime = devtime;
    106 	parser->systime = systime;
    107 
    108 	return DC_STATUS_SUCCESS;
    109 }
    110 
    111 
    112 static dc_status_t
    113 reefnet_sensuspro_parser_set_atmospheric (dc_parser_t *abstract, double atmospheric)
    114 {
    115 	reefnet_sensuspro_parser_t *parser = (reefnet_sensuspro_parser_t *) abstract;
    116 
    117 	parser->atmospheric = atmospheric;
    118 
    119 	return DC_STATUS_SUCCESS;
    120 }
    121 
    122 
    123 static dc_status_t
    124 reefnet_sensuspro_parser_set_density (dc_parser_t *abstract, double density)
    125 {
    126 	reefnet_sensuspro_parser_t *parser = (reefnet_sensuspro_parser_t *) abstract;
    127 
    128 	parser->hydrostatic = density * GRAVITY;
    129 
    130 	return DC_STATUS_SUCCESS;
    131 }
    132 
    133 
    134 static dc_status_t
    135 reefnet_sensuspro_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime)
    136 {
    137 	reefnet_sensuspro_parser_t *parser = (reefnet_sensuspro_parser_t *) abstract;
    138 
    139 	if (abstract->size < 6 + 4)
    140 		return DC_STATUS_DATAFORMAT;
    141 
    142 	unsigned int timestamp = array_uint32_le (abstract->data + 6);
    143 
    144 	dc_ticks_t ticks = parser->systime;
    145 	if (timestamp < parser->devtime) {
    146 		ticks -= parser->devtime - timestamp;
    147 	} else {
    148 		ticks += timestamp - parser->devtime;
    149 	}
    150 
    151 	if (!dc_datetime_localtime (datetime, ticks))
    152 		return DC_STATUS_DATAFORMAT;
    153 
    154 	return DC_STATUS_SUCCESS;
    155 }
    156 
    157 
    158 static dc_status_t
    159 reefnet_sensuspro_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value)
    160 {
    161 	reefnet_sensuspro_parser_t *parser = (reefnet_sensuspro_parser_t *) abstract;
    162 
    163 	if (abstract->size < 12)
    164 		return DC_STATUS_DATAFORMAT;
    165 
    166 	if (!parser->cached) {
    167 		const unsigned char footer[2] = {0xFF, 0xFF};
    168 
    169 		const unsigned char *data = abstract->data;
    170 		unsigned int size = abstract->size;
    171 
    172 		unsigned int interval = array_uint16_le (data + 4);
    173 
    174 		unsigned int maxdepth = 0;
    175 		unsigned int nsamples = 0;
    176 		unsigned int offset = 10;
    177 		while (offset + sizeof (footer) <= size &&
    178 			memcmp (data + offset, footer, sizeof (footer)) != 0)
    179 		{
    180 			unsigned int raw = array_uint16_le (data + offset);
    181 			unsigned int depth = (raw & 0x01FF);
    182 			if (depth > maxdepth)
    183 				maxdepth = depth;
    184 
    185 			nsamples++;
    186 
    187 			offset += 2;
    188 		}
    189 
    190 		parser->cached = 1;
    191 		parser->divetime = nsamples * interval;
    192 		parser->maxdepth = maxdepth;
    193 	}
    194 
    195 	if (value) {
    196 		switch (type) {
    197 		case DC_FIELD_DIVETIME:
    198 			*((unsigned int *) value) = parser->divetime;
    199 			break;
    200 		case DC_FIELD_MAXDEPTH:
    201 			*((double *) value) = (parser->maxdepth * FSW - parser->atmospheric) / parser->hydrostatic;
    202 			break;
    203 		case DC_FIELD_GASMIX_COUNT:
    204 			*((unsigned int *) value) = 0;
    205 			break;
    206 		case DC_FIELD_DIVEMODE:
    207 			*((dc_divemode_t *) value) = DC_DIVEMODE_GAUGE;
    208 			break;
    209 		default:
    210 			return DC_STATUS_UNSUPPORTED;
    211 		}
    212 	}
    213 
    214 	return DC_STATUS_SUCCESS;
    215 }
    216 
    217 
    218 static dc_status_t
    219 reefnet_sensuspro_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata)
    220 {
    221 	reefnet_sensuspro_parser_t *parser = (reefnet_sensuspro_parser_t*) abstract;
    222 
    223 	const unsigned char header[4] = {0x00, 0x00, 0x00, 0x00};
    224 	const unsigned char footer[2] = {0xFF, 0xFF};
    225 
    226 	const unsigned char *data = abstract->data;
    227 	unsigned int size = abstract->size;
    228 
    229 	unsigned int offset = 0;
    230 	while (offset + sizeof (header) <= size) {
    231 		if (memcmp (data + offset, header, sizeof (header)) == 0) {
    232 			if (offset + 10 > size)
    233 				return DC_STATUS_DATAFORMAT;
    234 
    235 			unsigned int time = 0;
    236 			unsigned int interval = array_uint16_le (data + offset + 4);
    237 
    238 			offset += 10;
    239 			while (offset + sizeof (footer) <= size &&
    240 				memcmp (data + offset, footer, sizeof (footer)) != 0)
    241 			{
    242 				unsigned int value = array_uint16_le (data + offset);
    243 				unsigned int depth = (value & 0x01FF);
    244 				unsigned int temperature = (value & 0xFE00) >> 9;
    245 
    246 				dc_sample_value_t sample = {0};
    247 
    248 				// Time (seconds)
    249 				time += interval;
    250 				sample.time = time * 1000;
    251 				if (callback) callback (DC_SAMPLE_TIME, &sample, userdata);
    252 
    253 				// Temperature (°F)
    254 				sample.temperature = (temperature - 32.0) * (5.0 / 9.0);
    255 				if (callback) callback (DC_SAMPLE_TEMPERATURE, &sample, userdata);
    256 
    257 				// Depth (absolute pressure in fsw)
    258 				sample.depth = (depth * FSW - parser->atmospheric) / parser->hydrostatic;
    259 				if (callback) callback (DC_SAMPLE_DEPTH, &sample, userdata);
    260 
    261 				offset += 2;
    262 			}
    263 			break;
    264 		} else {
    265 			offset++;
    266 		}
    267 	}
    268 
    269 	return DC_STATUS_SUCCESS;
    270 }