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

uwatec_memomouse_parser.c (8954B)


      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 
     24 #include <libdivecomputer/units.h>
     25 
     26 #include "uwatec_memomouse.h"
     27 #include "context-private.h"
     28 #include "parser-private.h"
     29 #include "platform.h"
     30 #include "array.h"
     31 
     32 #define ISINSTANCE(parser) dc_parser_isinstance((parser), &uwatec_memomouse_parser_vtable)
     33 
     34 typedef struct uwatec_memomouse_parser_t uwatec_memomouse_parser_t;
     35 
     36 struct uwatec_memomouse_parser_t {
     37 	dc_parser_t base;
     38 	unsigned int devtime;
     39 	dc_ticks_t systime;
     40 };
     41 
     42 static dc_status_t uwatec_memomouse_parser_set_clock (dc_parser_t *abstract, unsigned int devtime, dc_ticks_t systime);
     43 static dc_status_t uwatec_memomouse_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime);
     44 static dc_status_t uwatec_memomouse_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value);
     45 static dc_status_t uwatec_memomouse_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata);
     46 
     47 static const dc_parser_vtable_t uwatec_memomouse_parser_vtable = {
     48 	sizeof(uwatec_memomouse_parser_t),
     49 	DC_FAMILY_UWATEC_MEMOMOUSE,
     50 	uwatec_memomouse_parser_set_clock, /* set_clock */
     51 	NULL, /* set_atmospheric */
     52 	NULL, /* set_density */
     53 	uwatec_memomouse_parser_get_datetime, /* datetime */
     54 	uwatec_memomouse_parser_get_field, /* fields */
     55 	uwatec_memomouse_parser_samples_foreach, /* samples_foreach */
     56 	NULL /* destroy */
     57 };
     58 
     59 
     60 dc_status_t
     61 uwatec_memomouse_parser_create (dc_parser_t **out, dc_context_t *context, const unsigned char data[], size_t size)
     62 {
     63 	uwatec_memomouse_parser_t *parser = NULL;
     64 
     65 	if (out == NULL)
     66 		return DC_STATUS_INVALIDARGS;
     67 
     68 	// Allocate memory.
     69 	parser = (uwatec_memomouse_parser_t *) dc_parser_allocate (context, &uwatec_memomouse_parser_vtable, data, size);
     70 	if (parser == NULL) {
     71 		ERROR (context, "Failed to allocate memory.");
     72 		return DC_STATUS_NOMEMORY;
     73 	}
     74 
     75 	// Set the default values.
     76 	parser->devtime = 0;
     77 	parser->systime = 0;
     78 
     79 	*out = (dc_parser_t*) parser;
     80 
     81 	return DC_STATUS_SUCCESS;
     82 }
     83 
     84 
     85 static dc_status_t
     86 uwatec_memomouse_parser_set_clock (dc_parser_t *abstract, unsigned int devtime, dc_ticks_t systime)
     87 {
     88 	uwatec_memomouse_parser_t *parser = (uwatec_memomouse_parser_t *) abstract;
     89 
     90 	parser->devtime = devtime;
     91 	parser->systime = systime;
     92 
     93 	return DC_STATUS_SUCCESS;
     94 }
     95 
     96 
     97 static dc_status_t
     98 uwatec_memomouse_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime)
     99 {
    100 	uwatec_memomouse_parser_t *parser = (uwatec_memomouse_parser_t *) abstract;
    101 
    102 	if (abstract->size < 11 + 4)
    103 		return DC_STATUS_DATAFORMAT;
    104 
    105 	unsigned int timestamp = array_uint32_le (abstract->data + 11);
    106 
    107 	dc_ticks_t ticks = parser->systime;
    108 	if (timestamp < parser->devtime) {
    109 		ticks -= (parser->devtime - timestamp) / 2;
    110 	} else {
    111 		ticks += (timestamp - parser->devtime) / 2;
    112 	}
    113 
    114 	if (!dc_datetime_localtime (datetime, ticks))
    115 		return DC_STATUS_DATAFORMAT;
    116 
    117 	return DC_STATUS_SUCCESS;
    118 }
    119 
    120 
    121 static dc_status_t
    122 uwatec_memomouse_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value)
    123 {
    124 	const unsigned char *data = abstract->data;
    125 	unsigned int size = abstract->size;
    126 
    127 	if (size < 18)
    128 		return DC_STATUS_DATAFORMAT;
    129 
    130 	unsigned int model = data[3];
    131 
    132 	int is_nitrox = 0, is_oxygen = 0, DC_ATTR_UNUSED is_air = 0;
    133 	if ((model & 0xF0) == 0xF0)
    134 		is_nitrox = 1;
    135 	if ((model & 0xF0) == 0xA0)
    136 		is_oxygen = 1;
    137 	if ((model & 0xF0) % 4 == 0)
    138 		is_air = 1;
    139 
    140 	unsigned int header = 22;
    141 	if (is_nitrox)
    142 		header += 2;
    143 	if (is_oxygen)
    144 		header += 3;
    145 
    146 	dc_gasmix_t *gasmix = (dc_gasmix_t *) value;
    147 	dc_tank_t *tank = (dc_tank_t *) value;
    148 
    149 	if (value) {
    150 		switch (type) {
    151 		case DC_FIELD_DIVETIME:
    152 			*((unsigned int *) value) = ((data[4] & 0x04 ? 100 : 0) + bcd2dec (data[5])) * 60;
    153 			break;
    154 		case DC_FIELD_MAXDEPTH:
    155 			*((double *) value) = ((array_uint16_be (data + 6) & 0xFFC0) >> 6) * 10.0 / 64.0;
    156 			break;
    157 		case DC_FIELD_GASMIX_COUNT:
    158 			*((unsigned int *) value) = 1;
    159 			break;
    160 		case DC_FIELD_GASMIX:
    161 			gasmix->usage = DC_USAGE_NONE;
    162 			gasmix->helium = 0.0;
    163 			if (size >= header + 18) {
    164 				if (is_oxygen)
    165 					gasmix->oxygen = data[18 + 23] / 100.0;
    166 				else if (is_nitrox)
    167 					gasmix->oxygen = (data[18 + 23] & 0x0F ? 20.0 + 2 * (data[18 + 23] & 0x0F) : 21.0) / 100.0;
    168 				else
    169 					gasmix->oxygen = 0.21;
    170 			} else {
    171 				gasmix->oxygen = 0.21;
    172 			}
    173 			gasmix->nitrogen = 1.0 - gasmix->oxygen - gasmix->helium;
    174 			break;
    175 		case DC_FIELD_TANK_COUNT:
    176 			if (data[10]) {
    177 				*((unsigned int *) value) = 1;
    178 			} else {
    179 				*((unsigned int *) value) = 0;
    180 			}
    181 			break;
    182 		case DC_FIELD_TANK:
    183 			tank->type = DC_TANKVOLUME_NONE;
    184 			tank->volume = 0.0;
    185 			tank->workpressure = 0.0;
    186 			if (model == 0x1C) {
    187 				tank->beginpressure = data[10] * 20.0 * PSI / BAR;
    188 			} else {
    189 				tank->beginpressure = data[10];
    190 			}
    191 			tank->endpressure = 0.0;
    192 			tank->gasmix = 0;
    193 			tank->usage = DC_TANK_USAGE_NONE;
    194 			break;
    195 		case DC_FIELD_TEMPERATURE_MINIMUM:
    196 			*((double *) value) = (signed char) data[15] / 4.0;
    197 			break;
    198 		default:
    199 			return DC_STATUS_UNSUPPORTED;
    200 		}
    201 	}
    202 
    203 	return DC_STATUS_SUCCESS;
    204 }
    205 
    206 
    207 static dc_status_t
    208 uwatec_memomouse_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata)
    209 {
    210 	const unsigned char *data = abstract->data;
    211 	unsigned int size = abstract->size;
    212 
    213 	if (size < 18)
    214 		return DC_STATUS_DATAFORMAT;
    215 
    216 	unsigned int model = data[3];
    217 
    218 	int is_nitrox = 0, is_oxygen = 0, DC_ATTR_UNUSED is_air = 0;
    219 	if ((model & 0xF0) == 0xF0)
    220 		is_nitrox = 1;
    221 	if ((model & 0xF0) == 0xA0)
    222 		is_oxygen = 1;
    223 	if ((model & 0xF0) % 4 == 0)
    224 		is_air = 1;
    225 
    226 	unsigned int header = 22;
    227 	if (is_nitrox)
    228 		header += 2;
    229 	if (is_oxygen)
    230 		header += 3;
    231 
    232 	unsigned int time = 20;
    233 
    234 	unsigned int gasmix_previous = 0xFFFFFFFF;
    235 	unsigned int gasmix = 0;
    236 
    237 	unsigned int offset = header + 18;
    238 	while (offset + 2 <= size) {
    239 		dc_sample_value_t sample = {0};
    240 
    241 		unsigned int value = array_uint16_be (data + offset);
    242 		unsigned int depth = (value & 0xFFC0) >> 6;
    243 		unsigned int warnings = (value & 0x3F);
    244 		offset += 2;
    245 
    246 		// Time (seconds)
    247 		sample.time = time * 1000;
    248 		if (callback) callback (DC_SAMPLE_TIME, &sample, userdata);
    249 
    250 		// Depth (meters)
    251 		sample.depth = depth * 10.0 / 64.0;
    252 		if (callback) callback (DC_SAMPLE_DEPTH, &sample, userdata);
    253 
    254 		// Gas change.
    255 		if (gasmix != gasmix_previous) {
    256 			sample.gasmix = gasmix;
    257 			if (callback) callback (DC_SAMPLE_GASMIX, &sample, userdata);
    258 			gasmix_previous = gasmix;
    259 		}
    260 
    261 		// NDL / Deco
    262 		if (warnings & 0x01) {
    263 			sample.deco.type = DC_DECO_DECOSTOP;
    264 		} else {
    265 			sample.deco.type = DC_DECO_NDL;
    266 		}
    267 		sample.deco.time = 0;
    268 		sample.deco.depth = 0.0;
    269 		sample.deco.tts = 0;
    270 		if (callback) callback (DC_SAMPLE_DECO, &sample, userdata);
    271 
    272 		// Warnings
    273 		for (unsigned int i = 0; i < 6; ++i) {
    274 			if (warnings & (1 << i)) {
    275 				sample.event.time = 0;
    276 				sample.event.flags = 0;
    277 				sample.event.value = 0;
    278 				switch (i) {
    279 				case 0: // Deco stop
    280 					sample.event.type = SAMPLE_EVENT_NONE;
    281 					break;
    282 				case 1: // Remaining bottom time too short (Air series only)
    283 					sample.event.type = SAMPLE_EVENT_RBT;
    284 					break;
    285 				case 2: // Ascent too fast
    286 					sample.event.type = SAMPLE_EVENT_ASCENT;
    287 					break;
    288 				case 3: // Ceiling violation of deco stop
    289 					sample.event.type = SAMPLE_EVENT_CEILING;
    290 					break;
    291 				case 4: // Work too hard (Air series only)
    292 					sample.event.type = SAMPLE_EVENT_WORKLOAD;
    293 					break;
    294 				case 5: // Transmit error of air pressure (always 1 unless Air series)
    295 					sample.event.type = SAMPLE_EVENT_TRANSMITTER;
    296 					break;
    297 				}
    298 				if (sample.event.type != SAMPLE_EVENT_NONE) {
    299 					if (callback) callback (DC_SAMPLE_EVENT, &sample, userdata);
    300 				}
    301 			}
    302 		}
    303 
    304 		if (time % 60 == 0) {
    305 			sample.vendor.type = SAMPLE_VENDOR_UWATEC_ALADIN;
    306 			sample.vendor.size = 0;
    307 			sample.vendor.data = data + offset;
    308 
    309 			// Decompression information.
    310 			if (offset + 1 > size)
    311 				return DC_STATUS_DATAFORMAT;
    312 			sample.vendor.size++;
    313 			offset++;
    314 
    315 			// Oxygen percentage (O2 series only).
    316 			if (is_oxygen) {
    317 				if (offset + 1 > size)
    318 					return DC_STATUS_DATAFORMAT;
    319 				sample.vendor.size++;
    320 				offset++;
    321 			}
    322 
    323 			if (callback) callback (DC_SAMPLE_VENDOR, &sample, userdata);
    324 		}
    325 
    326 		time += 20;
    327 	}
    328 
    329 	return DC_STATUS_SUCCESS;
    330 }