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

divesystem_idive_parser.c (19759B)


      1 /*
      2  * libdivecomputer
      3  *
      4  * Copyright (C) 2014 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 "divesystem_idive.h"
     25 #include "context-private.h"
     26 #include "parser-private.h"
     27 #include "array.h"
     28 
     29 #define ISINSTANCE(parser) dc_device_isinstance((parser), &divesystem_idive_parser_vtable)
     30 
     31 #define ISIX3M(model) ((model) >= 0x21)
     32 #define ISIX3M2(model) ((model) >= 0x60 && (model) < 0x1000)
     33 
     34 #define SZ_HEADER_IDIVE 0x32
     35 #define SZ_SAMPLE_IDIVE 0x2A
     36 #define SZ_HEADER_IX3M  0x36
     37 #define SZ_SAMPLE_IX3M  0x36
     38 #define SZ_SAMPLE_IX3M_APOS4 0x40
     39 
     40 #define NGASMIXES 8
     41 #define NTANKS    10
     42 
     43 #define EPOCH 1199145600 /* 2008-01-01 00:00:00 */
     44 
     45 #define OC       0
     46 #define SCR      1
     47 #define CCR      2
     48 #define GAUGE    3
     49 #define FREEDIVE 4
     50 #define INVALID  0xFFFFFFFF
     51 
     52 #define BUHLMANN 0
     53 #define VPM      1
     54 #define DUAL     2
     55 
     56 #define IX3M2_BUHLMANN 0
     57 #define IX3M2_ZHL16B   1
     58 #define IX3M2_ZHL16C   2
     59 #define IX3M2_VPM      3
     60 
     61 #define REC_INFO   1
     62 
     63 #define TANK_VALID        0x8000
     64 #define TANK_RFCHANNEL    0x001F
     65 #define TANK_PRESSURE_MSB 0x0020
     66 
     67 typedef struct divesystem_idive_parser_t divesystem_idive_parser_t;
     68 
     69 typedef struct divesystem_idive_gasmix_t {
     70 	unsigned int oxygen;
     71 	unsigned int helium;
     72 } divesystem_idive_gasmix_t;
     73 
     74 typedef struct divesystem_idive_tank_t {
     75 	unsigned int id;
     76 	unsigned int beginpressure;
     77 	unsigned int endpressure;
     78 } divesystem_idive_tank_t;
     79 
     80 struct divesystem_idive_parser_t {
     81 	dc_parser_t base;
     82 	unsigned int model;
     83 	unsigned int headersize;
     84 	// Cached fields.
     85 	unsigned int cached;
     86 	unsigned int divemode;
     87 	unsigned int divetime;
     88 	unsigned int maxdepth;
     89 	unsigned int ngasmixes;
     90 	unsigned int ntanks;
     91 	divesystem_idive_gasmix_t gasmix[NGASMIXES];
     92 	divesystem_idive_tank_t tank[NTANKS];
     93 	unsigned int algorithm;
     94 	unsigned int gf_low;
     95 	unsigned int gf_high;
     96 };
     97 
     98 static dc_status_t divesystem_idive_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime);
     99 static dc_status_t divesystem_idive_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value);
    100 static dc_status_t divesystem_idive_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata);
    101 
    102 static const dc_parser_vtable_t divesystem_idive_parser_vtable = {
    103 	sizeof(divesystem_idive_parser_t),
    104 	DC_FAMILY_DIVESYSTEM_IDIVE,
    105 	NULL, /* set_clock */
    106 	NULL, /* set_atmospheric */
    107 	NULL, /* set_density */
    108 	divesystem_idive_parser_get_datetime, /* datetime */
    109 	divesystem_idive_parser_get_field, /* fields */
    110 	divesystem_idive_parser_samples_foreach, /* samples_foreach */
    111 	NULL /* destroy */
    112 };
    113 
    114 
    115 dc_status_t
    116 divesystem_idive_parser_create (dc_parser_t **out, dc_context_t *context, const unsigned char data[], size_t size, unsigned int model)
    117 {
    118 	divesystem_idive_parser_t *parser = NULL;
    119 
    120 	if (out == NULL)
    121 		return DC_STATUS_INVALIDARGS;
    122 
    123 	// Allocate memory.
    124 	parser = (divesystem_idive_parser_t *) dc_parser_allocate (context, &divesystem_idive_parser_vtable, data, size);
    125 	if (parser == NULL) {
    126 		ERROR (context, "Failed to allocate memory.");
    127 		return DC_STATUS_NOMEMORY;
    128 	}
    129 
    130 	// Set the default values.
    131 	parser->model = model;
    132 	if (ISIX3M(model)) {
    133 		parser->headersize = SZ_HEADER_IX3M;
    134 	} else {
    135 		parser->headersize = SZ_HEADER_IDIVE;
    136 	}
    137 	parser->cached = 0;
    138 	parser->divemode = INVALID;
    139 	parser->divetime = 0;
    140 	parser->maxdepth = 0;
    141 	parser->ngasmixes = 0;
    142 	parser->ntanks = 0;
    143 	for (unsigned int i = 0; i < NGASMIXES; ++i) {
    144 		parser->gasmix[i].oxygen = 0;
    145 		parser->gasmix[i].helium = 0;
    146 	}
    147 	for (unsigned int i = 0; i < NTANKS; ++i) {
    148 		parser->tank[i].id = 0;
    149 		parser->tank[i].beginpressure = 0;
    150 		parser->tank[i].endpressure = 0;
    151 	}
    152 	parser->algorithm = INVALID;
    153 	parser->gf_low = INVALID;
    154 	parser->gf_high = INVALID;
    155 
    156 	*out = (dc_parser_t*) parser;
    157 
    158 	return DC_STATUS_SUCCESS;
    159 }
    160 
    161 
    162 static dc_status_t
    163 divesystem_idive_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime)
    164 {
    165 	divesystem_idive_parser_t *parser = (divesystem_idive_parser_t *) abstract;
    166 
    167 	static const signed char tz_array[] = {
    168 		-12,  0,    /* UTC-12    */
    169 		-11,  0,    /* UTC-11    */
    170 		-10,  0,    /* UTC-10    */
    171 		 -9, 30,    /* UTC-9:30  */
    172 		 -9,  0,    /* UTC-9     */
    173 		 -8,  0,    /* UTC-8     */
    174 		 -7,  0,    /* UTC-7     */
    175 		 -6,  0,    /* UTC-6     */
    176 		 -5,  0,    /* UTC-5     */
    177 		 -4, 30,    /* UTC-4:30  */
    178 		 -4,  0,    /* UTC-4     */
    179 		 -3, 30,    /* UTC-3:30  */
    180 		 -3,  0,    /* UTC-3     */
    181 		 -2,  0,    /* UTC-2     */
    182 		 -1,  0,    /* UTC-1     */
    183 		  0,  0,    /* UTC       */
    184 		  1,  0,    /* UTC+1     */
    185 		  2,  0,    /* UTC+2     */
    186 		  3,  0,    /* UTC+3     */
    187 		  3, 30,    /* UTC+3:30  */
    188 		  4,  0,    /* UTC+4     */
    189 		  4, 30,    /* UTC+4:30  */
    190 		  5,  0,    /* UTC+5     */
    191 		  5, 30,    /* UTC+5:30  */
    192 		  5, 45,    /* UTC+5:45  */
    193 		  6,  0,    /* UTC+6     */
    194 		  6, 30,    /* UTC+6:30  */
    195 		  7,  0,    /* UTC+7     */
    196 		  8,  0,    /* UTC+8     */
    197 		  8, 45,    /* UTC+8:45  */
    198 		  9,  0,    /* UTC+9     */
    199 		  9, 30,    /* UTC+9:30  */
    200 		  9, 45,    /* UTC+9:45  */
    201 		 10,  0,    /* UTC+10    */
    202 		 10, 30,    /* UTC+10:30 */
    203 		 11,  0,    /* UTC+11    */
    204 		 11, 30,    /* UTC+11:30 */
    205 		 12,  0,    /* UTC+12    */
    206 		 12, 45,    /* UTC+12:45 */
    207 		 13,  0,    /* UTC+13    */
    208 		 13, 45,    /* UTC+13:45 */
    209 		 14,  0     /* UTC+14    */
    210 	};
    211 
    212 	if (abstract->size < parser->headersize)
    213 		return DC_STATUS_DATAFORMAT;
    214 
    215 	dc_ticks_t ticks = array_uint32_le(abstract->data + 7) + EPOCH;
    216 
    217 	// Detect the APOS4 firmware.
    218 	unsigned int firmware = 0;
    219 	unsigned int apos4 = 0;
    220 	if (ISIX3M(parser->model)) {
    221 		firmware = array_uint32_le(abstract->data + 0x2A);
    222 		apos4 = (firmware / 10000000) >= 4;
    223 	} else {
    224 		firmware = array_uint32_le(abstract->data + 0x2E);
    225 		apos4 = 0;
    226 	}
    227 
    228 	if (apos4) {
    229 		// For devices with timezone support, the UTC offset of the
    230 		// device is used. The UTC offset is stored as an index in the
    231 		// timezone table.
    232 		unsigned int tz_idx = abstract->data[48];
    233 		if ((tz_idx % 2) != 0 || tz_idx >= C_ARRAY_SIZE(tz_array)) {
    234 			ERROR (abstract->context, "Invalid timezone index (%u).", tz_idx);
    235 			return DC_STATUS_DATAFORMAT;
    236 		}
    237 
    238 		int timezone = tz_array[tz_idx] * 3600;
    239 		if (timezone < 0) {
    240 			timezone -= tz_array[tz_idx + 1] * 60;
    241 		} else {
    242 			timezone += tz_array[tz_idx + 1] * 60;
    243 		}
    244 
    245 		ticks += timezone;
    246 
    247 		if (!dc_datetime_gmtime (datetime, ticks))
    248 			return DC_STATUS_DATAFORMAT;
    249 
    250 		datetime->timezone = timezone;
    251 	} else {
    252 		// For devices without timezone support, the current timezone of
    253 		// the host system is used.
    254 		if (!dc_datetime_localtime (datetime, ticks))
    255 			return DC_STATUS_DATAFORMAT;
    256 	}
    257 
    258 	return DC_STATUS_SUCCESS;
    259 }
    260 
    261 
    262 static dc_status_t
    263 divesystem_idive_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value)
    264 {
    265 	divesystem_idive_parser_t *parser = (divesystem_idive_parser_t *) abstract;
    266 	const unsigned char *data = abstract->data;
    267 
    268 	if (abstract->size < parser->headersize)
    269 		return DC_STATUS_DATAFORMAT;
    270 
    271 	if (!parser->cached) {
    272 		dc_status_t rc = divesystem_idive_parser_samples_foreach (abstract, NULL, NULL);
    273 		if (rc != DC_STATUS_SUCCESS)
    274 			return rc;
    275 	}
    276 
    277 	dc_gasmix_t *gasmix = (dc_gasmix_t *) value;
    278 	dc_tank_t *tank = (dc_tank_t *) value;
    279 	dc_salinity_t *water = (dc_salinity_t *) value;
    280 	dc_decomodel_t *decomodel = (dc_decomodel_t *) value;
    281 
    282 	if (value) {
    283 		switch (type) {
    284 		case DC_FIELD_DIVETIME:
    285 			*((unsigned int *) value) = parser->divetime;
    286 			break;
    287 		case DC_FIELD_MAXDEPTH:
    288 			*((double *) value) = parser->maxdepth / 10.0;
    289 			break;
    290 		case DC_FIELD_GASMIX_COUNT:
    291 			*((unsigned int *) value) = parser->ngasmixes;
    292 			break;
    293 		case DC_FIELD_GASMIX:
    294 			gasmix->usage = DC_USAGE_NONE;
    295 			gasmix->helium = parser->gasmix[flags].helium / 100.0;
    296 			gasmix->oxygen = parser->gasmix[flags].oxygen / 100.0;
    297 			gasmix->nitrogen = 1.0 - gasmix->oxygen - gasmix->helium;
    298 			break;
    299 		case DC_FIELD_TANK_COUNT:
    300 			*((unsigned int *) value) = parser->ntanks;
    301 			break;
    302 		case DC_FIELD_TANK:
    303 			tank->type = DC_TANKVOLUME_NONE;
    304 			tank->volume = 0.0;
    305 			tank->workpressure = 0.0;
    306 			tank->beginpressure = parser->tank[flags].beginpressure;
    307 			tank->endpressure   = parser->tank[flags].endpressure;
    308 			tank->gasmix = DC_GASMIX_UNKNOWN;
    309 			tank->usage = DC_TANK_USAGE_NONE;
    310 			break;
    311 		case DC_FIELD_ATMOSPHERIC:
    312 			if (ISIX3M(parser->model)) {
    313 				*((double *) value) = array_uint16_le (data + 11) / 10000.0;
    314 			} else {
    315 				*((double *) value) = array_uint16_le (data + 11) / 1000.0;
    316 			}
    317 			break;
    318 		case DC_FIELD_SALINITY:
    319 			water->type = data[34] == 0 ? DC_WATER_SALT : DC_WATER_FRESH;
    320 			water->density = 0.0;
    321 			break;
    322 		case DC_FIELD_DIVEMODE:
    323 			if (parser->divemode == INVALID)
    324 				return DC_STATUS_UNSUPPORTED;
    325 			switch (parser->divemode) {
    326 			case OC:
    327 				*((dc_divemode_t *) value) = DC_DIVEMODE_OC;
    328 				break;
    329 			case SCR:
    330 				*((dc_divemode_t *) value) = DC_DIVEMODE_SCR;
    331 				break;
    332 			case CCR:
    333 				*((dc_divemode_t *) value) = DC_DIVEMODE_CCR;
    334 				break;
    335 			case GAUGE:
    336 				*((dc_divemode_t *) value) = DC_DIVEMODE_GAUGE;
    337 				break;
    338 			case FREEDIVE:
    339 				*((dc_divemode_t *) value) = DC_DIVEMODE_FREEDIVE;
    340 				break;
    341 			default:
    342 				ERROR (abstract->context, "Unknown dive mode %02x.", parser->divemode);
    343 				return DC_STATUS_DATAFORMAT;
    344 			}
    345 			break;
    346 		case DC_FIELD_DECOMODEL:
    347 			if (parser->algorithm == INVALID)
    348 				return DC_STATUS_UNSUPPORTED;
    349 			if (ISIX3M2(parser->model)) {
    350 				switch (parser->algorithm) {
    351 				case IX3M2_BUHLMANN:
    352 				case IX3M2_ZHL16B:
    353 				case IX3M2_ZHL16C:
    354 					decomodel->type = DC_DECOMODEL_BUHLMANN;
    355 					decomodel->conservatism = 0;
    356 					decomodel->params.gf.low = parser->gf_low;
    357 					decomodel->params.gf.high = parser->gf_high;
    358 					break;
    359 				case IX3M2_VPM:
    360 					decomodel->type = DC_DECOMODEL_VPM;
    361 					decomodel->conservatism = 0;
    362 					break;
    363 				default:
    364 					ERROR (abstract->context, "Unknown deco algorithm %02x.", parser->algorithm);
    365 					return DC_STATUS_DATAFORMAT;
    366 				}
    367 			} else {
    368 				switch (parser->algorithm) {
    369 				case BUHLMANN:
    370 				case DUAL:
    371 					decomodel->type = DC_DECOMODEL_BUHLMANN;
    372 					decomodel->conservatism = 0;
    373 					decomodel->params.gf.low = parser->gf_low;
    374 					decomodel->params.gf.high = parser->gf_high;
    375 					break;
    376 				case VPM:
    377 					decomodel->type = DC_DECOMODEL_VPM;
    378 					decomodel->conservatism = 0;
    379 					break;
    380 				default:
    381 					ERROR (abstract->context, "Unknown deco algorithm %02x.", parser->algorithm);
    382 					return DC_STATUS_DATAFORMAT;
    383 				}
    384 			}
    385 			break;
    386 		default:
    387 			return DC_STATUS_UNSUPPORTED;
    388 		}
    389 	}
    390 
    391 	return DC_STATUS_SUCCESS;
    392 }
    393 
    394 
    395 static dc_status_t
    396 divesystem_idive_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata)
    397 {
    398 	divesystem_idive_parser_t *parser = (divesystem_idive_parser_t *) abstract;
    399 	const unsigned char *data = abstract->data;
    400 	unsigned int size = abstract->size;
    401 
    402 	unsigned int time = 0;
    403 	unsigned int maxdepth = 0;
    404 	unsigned int ngasmixes = 0;
    405 	unsigned int ntanks = 0;
    406 	divesystem_idive_gasmix_t gasmix[NGASMIXES] = {0};
    407 	divesystem_idive_tank_t tank[NTANKS] = {0};
    408 	unsigned int o2_previous = INVALID;
    409 	unsigned int he_previous = INVALID;
    410 	unsigned int mode_previous = INVALID;
    411 	unsigned int divemode = INVALID;
    412 	unsigned int tank_previous = INVALID;
    413 	unsigned int tank_idx = INVALID;
    414 	unsigned int algorithm = INVALID;
    415 	unsigned int algorithm_previous = INVALID;
    416 	unsigned int gf_low = INVALID;
    417 	unsigned int gf_high = INVALID;
    418 	unsigned int have_bearing = 0;
    419 
    420 	unsigned int firmware = 0;
    421 	unsigned int firmware_major = 0;
    422 	unsigned int apos4 = 0;
    423 	unsigned int nsamples = array_uint16_le (data + 1);
    424 	unsigned int samplesize = SZ_SAMPLE_IDIVE;
    425 	if (ISIX3M(parser->model)) {
    426 		// Detect the APOS4 firmware.
    427 		firmware = array_uint32_le(data + 0x2A);
    428 		firmware_major = firmware / 10000000;
    429 		apos4 = firmware_major >= 4;
    430 		if (apos4) {
    431 			// Dive downloaded and recorded with the APOS4 firmware.
    432 			samplesize = SZ_SAMPLE_IX3M_APOS4;
    433 		} else if (size == parser->headersize + nsamples * SZ_SAMPLE_IX3M_APOS4) {
    434 			// Dive downloaded with the APOS4 firmware, but recorded
    435 			// with an older firmware.
    436 			samplesize = SZ_SAMPLE_IX3M_APOS4;
    437 		} else {
    438 			// Dive downloaded and recorded with an older firmware.
    439 			samplesize = SZ_SAMPLE_IX3M;
    440 		}
    441 	} else {
    442 		firmware = array_uint32_le(data + 0x2E);
    443 	}
    444 
    445 	unsigned int have_location = 0;
    446 	int altitude = 0, longitude = 0, latitude = 0;
    447 
    448 	unsigned int offset = parser->headersize;
    449 	while (offset + samplesize <= size) {
    450 		dc_sample_value_t sample = {0};
    451 
    452 		// Get the record type.
    453 		unsigned int type = ISIX3M(parser->model) ?
    454 			array_uint16_le (data + offset + 52) :
    455 			0;
    456 		if (type == REC_INFO) {
    457 			altitude  = (signed int) array_uint32_le (data + offset + 40);
    458 			longitude = (signed int) array_uint32_le (data + offset + 44);
    459 			latitude  = (signed int) array_uint32_le (data + offset + 48);
    460 			have_location = 1;
    461 
    462 			// Skip non-sample records.
    463 			offset += samplesize;
    464 			continue;
    465 		}
    466 
    467 		// Time (seconds).
    468 		unsigned int timestamp = array_uint32_le (data + offset + 2);
    469 		if (timestamp <= time && time != 0) {
    470 			ERROR (abstract->context, "Timestamp moved backwards (%u %u).", timestamp, time);
    471 			return DC_STATUS_DATAFORMAT;
    472 		}
    473 		time = timestamp;
    474 		sample.time = timestamp * 1000;
    475 		if (callback) callback (DC_SAMPLE_TIME, &sample, userdata);
    476 
    477 		// Depth (1/10 m).
    478 		unsigned int depth = array_uint16_le (data + offset + 6);
    479 		if (maxdepth < depth)
    480 			maxdepth = depth;
    481 		sample.depth = depth / 10.0;
    482 		if (callback) callback (DC_SAMPLE_DEPTH, &sample, userdata);
    483 
    484 		// Temperature (Celsius).
    485 		signed int temperature = (signed short) array_uint16_le (data + offset + 8);
    486 		sample.temperature = temperature / 10.0;
    487 		if (callback) callback (DC_SAMPLE_TEMPERATURE, &sample, userdata);
    488 
    489 		// Dive mode
    490 		unsigned int mode = data[offset + 18];
    491 		if (mode != mode_previous) {
    492 			if (mode_previous != INVALID) {
    493 				WARNING (abstract->context, "Dive mode changed from %02x to %02x.", mode_previous, mode);
    494 			}
    495 			mode_previous = mode;
    496 		}
    497 		if (divemode == INVALID) {
    498 			divemode = mode;
    499 		}
    500 
    501 		// Deco model
    502 		unsigned int s_algorithm = data[offset + 14];
    503 		unsigned int s_gf_high = data[offset + 15];
    504 		unsigned int s_gf_low  = data[offset + 16];
    505 		if (s_algorithm != algorithm_previous) {
    506 			if (algorithm_previous != INVALID) {
    507 				WARNING (abstract->context, "Deco algorithm changed from %02x to %02x.", algorithm_previous, s_algorithm);
    508 			}
    509 			algorithm_previous = s_algorithm;
    510 		}
    511 		if (algorithm == INVALID) {
    512 			algorithm = s_algorithm;
    513 			gf_low = s_gf_low;
    514 			gf_high = s_gf_high;
    515 		}
    516 
    517 		// Setpoint
    518 		if (mode == SCR || mode == CCR) {
    519 			unsigned int setpoint = array_uint16_le (data + offset + 19);
    520 			sample.setpoint = setpoint / 1000.0;
    521 			if (callback) callback (DC_SAMPLE_SETPOINT, &sample, userdata);
    522 		}
    523 
    524 		// Gaschange.
    525 		unsigned int o2 = data[offset + 10];
    526 		unsigned int he = data[offset + 11];
    527 		if (o2 != o2_previous || he != he_previous) {
    528 			// Find the gasmix in the list.
    529 			unsigned int i = 0;
    530 			while (i < ngasmixes) {
    531 				if (o2 == gasmix[i].oxygen && he == gasmix[i].helium)
    532 					break;
    533 				i++;
    534 			}
    535 
    536 			// Add it to list if not found.
    537 			if (i >= ngasmixes) {
    538 				if (i >= NGASMIXES) {
    539 					ERROR (abstract->context, "Maximum number of gas mixes reached.");
    540 					return DC_STATUS_DATAFORMAT;
    541 				}
    542 				gasmix[i].oxygen = o2;
    543 				gasmix[i].helium = he;
    544 				ngasmixes = i + 1;
    545 			}
    546 
    547 			sample.gasmix = i;
    548 			if (callback) callback (DC_SAMPLE_GASMIX, &sample, userdata);
    549 			o2_previous = o2;
    550 			he_previous = he;
    551 		}
    552 
    553 		// Deco stop / NDL.
    554 		unsigned int decostop = 0, decotime = 0, tts = 0;
    555 		if (apos4) {
    556 			decostop = array_uint16_le (data + offset + 21);
    557 			decotime = array_uint16_le (data + offset + 23);
    558 			tts      = array_uint16_le (data + offset + 25);
    559 		} else {
    560 			decostop = array_uint16_le (data + offset + 21);
    561 			tts      = array_uint16_le (data + offset + 23);
    562 		}
    563 		if (decostop) {
    564 			sample.deco.type = DC_DECO_DECOSTOP;
    565 			sample.deco.depth = decostop / 10.0;
    566 			sample.deco.time = decotime;
    567 			sample.deco.tts = tts;
    568 		} else {
    569 			sample.deco.type = DC_DECO_NDL;
    570 			sample.deco.depth = 0.0;
    571 			sample.deco.time = tts;
    572 			sample.deco.tts = 0;
    573 		}
    574 		if (callback) callback (DC_SAMPLE_DECO, &sample, userdata);
    575 
    576 		// CNS
    577 		unsigned int cns = array_uint16_le (data + offset + 29);
    578 		sample.cns = cns / 100.0;
    579 		if (callback) callback (DC_SAMPLE_CNS, &sample, userdata);
    580 
    581 		// Tank Pressure
    582 		if (samplesize == SZ_SAMPLE_IX3M_APOS4) {
    583 			unsigned int id = data[offset + 47] & 0x0F;
    584 			unsigned int flags = data[offset + 47] & 0xF0;
    585 			unsigned int pressure = data[offset + 49];
    586 			unsigned int DC_ATTR_UNUSED rfchannel = 0;
    587 
    588 			if (type & TANK_VALID) {
    589 				rfchannel = type & TANK_RFCHANNEL;
    590 				if (type & TANK_PRESSURE_MSB) {
    591 					pressure |= 0x100;
    592 				}
    593 			}
    594 
    595 			if (flags & 0x20) {
    596 				// 300 bar transmitter.
    597 				pressure *= 2;
    598 			}
    599 
    600 			if (flags & 0x80) {
    601 				// No active transmitter available
    602 			} else if (flags & 0x40) {
    603 				// Transmitter connection lost
    604 				sample.event.type = SAMPLE_EVENT_TRANSMITTER;
    605 				sample.event.time = 0;
    606 				sample.event.flags = 0;
    607 				sample.event.value = 0;
    608 				if (callback) callback (DC_SAMPLE_EVENT, &sample, userdata);
    609 			} else {
    610 				// Get the index of the tank.
    611 				if (id != tank_previous) {
    612 					unsigned int i = 0;
    613 					while (i < ntanks) {
    614 						if (id == tank[i].id)
    615 							break;
    616 						i++;
    617 					}
    618 
    619 					tank_previous = id;
    620 					tank_idx = i;
    621 				}
    622 
    623 				// Add a new tank if necessary.
    624 				if (tank_idx >= ntanks && pressure != 0) {
    625 					if (tank_idx >= NTANKS) {
    626 						ERROR (abstract->context, "Maximum number of tanks reached.");
    627 						return DC_STATUS_DATAFORMAT;
    628 					}
    629 					tank[tank_idx].id = id;
    630 					tank[tank_idx].beginpressure = pressure;
    631 					tank[tank_idx].endpressure = pressure;
    632 					ntanks = tank_idx + 1;
    633 				}
    634 
    635 				if (tank_idx < ntanks) {
    636 					sample.pressure.tank = tank_idx;
    637 					sample.pressure.value = pressure;
    638 					if (callback) callback (DC_SAMPLE_PRESSURE, &sample, userdata);
    639 					tank[tank_idx].endpressure = pressure;
    640 				}
    641 			}
    642 
    643 			// Compass bearing
    644 			unsigned int bearing = array_uint16_le (data + offset + 50);
    645 			if (bearing != 0) {
    646 				have_bearing = 1; // Stop ignoring zero values.
    647 			}
    648 			if (have_bearing && bearing != 0xFFFF) {
    649 				sample.bearing = bearing;
    650 				if (callback) callback (DC_SAMPLE_BEARING, &sample, userdata);
    651 			}
    652 		}
    653 
    654 		// GPS location
    655 		if (have_location) {
    656 			sample.location.latitude  = latitude  / 10000000.0;
    657 			sample.location.longitude = longitude / 10000000.0;
    658 			sample.location.altitude  = altitude  / 1000.0;
    659 			if (callback) callback (DC_SAMPLE_LOCATION, &sample, userdata);
    660 			have_location = 0;
    661 		}
    662 
    663 		offset += samplesize;
    664 	}
    665 
    666 	// Cache the data for later use.
    667 	for (unsigned int i = 0; i < ntanks; ++i) {
    668 		parser->tank[i] = tank[i];
    669 	}
    670 	for (unsigned int i = 0; i < ngasmixes; ++i) {
    671 		parser->gasmix[i] = gasmix[i];
    672 	}
    673 	parser->ngasmixes = ngasmixes;
    674 	parser->ntanks = ntanks;
    675 	parser->maxdepth = maxdepth;
    676 	parser->divetime = time;
    677 	parser->divemode = divemode;
    678 	parser->algorithm = algorithm;
    679 	parser->gf_low = gf_low;
    680 	parser->gf_high = gf_high;
    681 	parser->cached = 1;
    682 
    683 	return DC_STATUS_SUCCESS;
    684 }