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

suunto_d9_parser.c (24099B)


      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, strdup
     24 #include <stdio.h>	// snprintf
     25 
     26 #include "suunto_d9.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), &suunto_d9_parser_vtable)
     33 
     34 #define MAXPARAMS 3
     35 #define NGASMIXES 11
     36 
     37 #define D9          0x0E
     38 #define D6          0x0F
     39 #define VYPER2      0x10
     40 #define COBRA2      0x11
     41 #define D4          0x12
     42 #define VYPERAIR    0x13
     43 #define COBRA3      0x14
     44 #define HELO2       0x15
     45 #define D4i         0x19
     46 #define D6i         0x1A
     47 #define D9tx        0x1B
     48 #define DX          0x1C
     49 #define VYPERNOVO   0x1D
     50 #define ZOOPNOVO_A  0x1E
     51 #define ZOOPNOVO_B  0x1F
     52 #define D4F         0x20
     53 
     54 #define ID_D6I_V1_MIX2 0x1871C062
     55 #define ID_D6I_V1_MIX3 0x1871C063
     56 #define ID_D6I_V2      0x18724062
     57 #define ID_D4I_V1      ID_D6I_V1_MIX2
     58 #define ID_D4I_V2      ID_D6I_V2
     59 #define ID_DX_V1       0x18922062
     60 #define ID_DX_V2       0x18924062
     61 
     62 #define AIR      0
     63 #define NITROX   1
     64 #define GAUGE    2
     65 #define FREEDIVE 3
     66 #define MIXED    4
     67 #define CCR      5
     68 
     69 #define SAFETYSTOP   (1 << 0)
     70 #define DECOSTOP     (1 << 1)
     71 #define DEEPSTOP     (1 << 2)
     72 
     73 typedef struct suunto_d9_parser_t suunto_d9_parser_t;
     74 
     75 struct suunto_d9_parser_t {
     76 	dc_parser_t base;
     77 	unsigned int model;
     78 	unsigned int serial;
     79 	// Cached fields.
     80 	unsigned int cached;
     81 	unsigned int id;
     82 	unsigned int mode;
     83 	unsigned int ngasmixes;
     84 	unsigned int nccr;
     85 	unsigned int oxygen[NGASMIXES];
     86 	unsigned int helium[NGASMIXES];
     87 	unsigned int gasmix;
     88 	unsigned int config;
     89 };
     90 
     91 typedef struct sample_info_t {
     92 	unsigned int type;
     93 	unsigned int size;
     94 	unsigned int interval;
     95 	unsigned int divisor;
     96 } sample_info_t;
     97 
     98 static dc_status_t suunto_d9_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime);
     99 static dc_status_t suunto_d9_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value);
    100 static dc_status_t suunto_d9_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata);
    101 
    102 static const dc_parser_vtable_t suunto_d9_parser_vtable = {
    103 	sizeof(suunto_d9_parser_t),
    104 	DC_FAMILY_SUUNTO_D9,
    105 	NULL, /* set_clock */
    106 	NULL, /* set_atmospheric */
    107 	NULL, /* set_density */
    108 	suunto_d9_parser_get_datetime, /* datetime */
    109 	suunto_d9_parser_get_field, /* fields */
    110 	suunto_d9_parser_samples_foreach, /* samples_foreach */
    111 	NULL /* destroy */
    112 };
    113 
    114 static unsigned int
    115 suunto_d9_parser_find_gasmix (suunto_d9_parser_t *parser, unsigned int o2, unsigned int he)
    116 {
    117 	// Find the gasmix in the list.
    118 	unsigned int i = parser->nccr;
    119 	while (i < parser->ngasmixes) {
    120 		if (o2 == parser->oxygen[i] && he == parser->helium[i])
    121 			break;
    122 		i++;
    123 	}
    124 
    125 	return i;
    126 }
    127 
    128 static dc_status_t
    129 suunto_d9_parser_cache (suunto_d9_parser_t *parser)
    130 {
    131 	const unsigned char *data = parser->base.data;
    132 	unsigned int size = parser->base.size;
    133 
    134 	if (parser->cached) {
    135 		return DC_STATUS_SUCCESS;
    136 	}
    137 
    138 	// Get the logbook id tag.
    139 	unsigned int id = array_uint32_le (data + 1);
    140 
    141 	// Gasmix information.
    142 	unsigned int gasmode_offset = 0x19;
    143 	unsigned int gasmix_offset = 0x21;
    144 	unsigned int gasmix_count = 3;
    145 	unsigned int ccr_count = 0;
    146 	if (parser->model == HELO2) {
    147 		gasmode_offset = 0x1F;
    148 		gasmix_offset = 0x54;
    149 		gasmix_count = 8;
    150 	} else if (parser->model == D4i || parser->model == ZOOPNOVO_A ||
    151 		parser->model == ZOOPNOVO_B || parser->model == D4F) {
    152 		gasmode_offset = 0x1D;
    153 		if (id == ID_D4I_V2)
    154 			gasmix_offset = 0x67;
    155 		else
    156 			gasmix_offset = 0x5F;
    157 		gasmix_count = 1;
    158 	} else if (parser->model == D6i || parser->model == VYPERNOVO) {
    159 		gasmode_offset = 0x1D;
    160 		if (id == ID_D6I_V2)
    161 			gasmix_offset = 0x67;
    162 		else
    163 			gasmix_offset = 0x5F;
    164 		if (id == ID_D6I_V1_MIX3 || id == ID_D6I_V2)
    165 			gasmix_count = 3;
    166 		else
    167 			gasmix_count = 2;
    168 	} else if (parser->model == D9tx) {
    169 		gasmode_offset = 0x1D;
    170 		gasmix_offset = 0x87;
    171 		gasmix_count = 8;
    172 	} else if (parser->model == DX) {
    173 		gasmode_offset = 0x21;
    174 		if (id == ID_DX_V2)
    175 			gasmix_offset = 0xC3;
    176 		else
    177 			gasmix_offset = 0xC1;
    178 		gasmix_count = 11;
    179 		ccr_count = 3;
    180 	}
    181 
    182 	// Offset to the configuration data.
    183 	unsigned int config = 0x3A;
    184 	if (parser->model == D4) {
    185 		config += 1;
    186 	} else if (parser->model == HELO2 || parser->model == D4i ||
    187 		parser->model == D6i || parser->model == D9tx ||
    188 		parser->model == DX || parser->model == ZOOPNOVO_A ||
    189 		parser->model == ZOOPNOVO_B || parser->model == VYPERNOVO ||
    190 		parser->model == D4F) {
    191 		config = gasmix_offset + gasmix_count * 6;
    192 	}
    193 	if (config + 1 > size)
    194 		return DC_STATUS_DATAFORMAT;
    195 
    196 	// Cache the data for later use.
    197 	parser->mode = data[gasmode_offset];
    198 	parser->gasmix = 0;
    199 	if (parser->mode == GAUGE || parser->mode == FREEDIVE) {
    200 		parser->ngasmixes = 0;
    201 		parser->nccr = 0;
    202 	} else if (parser->mode == AIR) {
    203 		parser->oxygen[0] = 21;
    204 		parser->helium[0] = 0;
    205 		parser->ngasmixes = 1;
    206 		parser->nccr = 0;
    207 	} else {
    208 		parser->ngasmixes = 0;
    209 		parser->nccr = ccr_count;
    210 		for (unsigned int i = 0; i < gasmix_count; ++i) {
    211 			if (parser->model == HELO2 || parser->model == D4i ||
    212 				parser->model == D6i || parser->model == D9tx ||
    213 				parser->model == DX || parser->model == ZOOPNOVO_A ||
    214 				parser->model == ZOOPNOVO_B || parser->model == VYPERNOVO ||
    215 				parser->model == D4F) {
    216 				parser->oxygen[i] = data[gasmix_offset + 6 * i + 1];
    217 				parser->helium[i] = data[gasmix_offset + 6 * i + 2];
    218 			} else {
    219 				unsigned int oxygen = data[gasmix_offset + i];
    220 				if (oxygen == 0x00 || oxygen == 0xFF)
    221 					break;
    222 				parser->oxygen[i] = oxygen;
    223 				parser->helium[i] = 0;
    224 			}
    225 			parser->ngasmixes++;
    226 		}
    227 
    228 		// Initial gasmix.
    229 		if (parser->model == HELO2) {
    230 			parser->gasmix = data[0x26];
    231 		} else if (parser->model == D4i || parser->model == D6i ||
    232 			parser->model == D9tx || parser->model == ZOOPNOVO_A ||
    233 			parser->model == ZOOPNOVO_B || parser->model == VYPERNOVO ||
    234 			parser->model == D4F) {
    235 			if (id == ID_D4I_V2 || id == ID_D6I_V2) {
    236 				parser->gasmix = data[0x2D];
    237 			} else {
    238 				parser->gasmix = data[0x28];
    239 			}
    240 		} else if (parser->model == DX) {
    241 			parser->gasmix = data[0x31] & 0x7F;
    242 			if ((data[0x31] & 0x80) == 0) {
    243 				parser->gasmix += parser->nccr;
    244 			}
    245 		}
    246 	}
    247 	parser->config = config;
    248 	parser->id = id;
    249 	parser->cached = 1;
    250 
    251 	return DC_STATUS_SUCCESS;
    252 }
    253 
    254 dc_status_t
    255 suunto_d9_parser_create (dc_parser_t **out, dc_context_t *context, const unsigned char data[], size_t size, unsigned int model, unsigned int serial)
    256 {
    257 	suunto_d9_parser_t *parser = NULL;
    258 
    259 	if (out == NULL)
    260 		return DC_STATUS_INVALIDARGS;
    261 
    262 	// Allocate memory.
    263 	parser = (suunto_d9_parser_t *) dc_parser_allocate (context, &suunto_d9_parser_vtable, data, size);
    264 	if (parser == NULL) {
    265 		ERROR (context, "Failed to allocate memory.");
    266 		return DC_STATUS_NOMEMORY;
    267 	}
    268 
    269 	// Set the default values.
    270 	parser->model = model;
    271 	parser->serial = serial;
    272 	parser->cached = 0;
    273 	parser->id = 0;
    274 	parser->mode = AIR;
    275 	parser->ngasmixes = 0;
    276 	parser->nccr = 0;
    277 	for (unsigned int i = 0; i < NGASMIXES; ++i) {
    278 		parser->oxygen[i] = 0;
    279 		parser->helium[i] = 0;
    280 	}
    281 	parser->gasmix = 0;
    282 	parser->config = 0;
    283 
    284 	*out = (dc_parser_t*) parser;
    285 
    286 	return DC_STATUS_SUCCESS;
    287 }
    288 
    289 
    290 static dc_status_t
    291 suunto_d9_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime)
    292 {
    293 	suunto_d9_parser_t *parser = (suunto_d9_parser_t*) abstract;
    294 
    295 	unsigned int offset = 0x11;
    296 	if (parser->model == HELO2 || parser->model == DX)
    297 		offset = 0x17;
    298 	else if (parser->model == D4i || parser->model == D6i ||
    299 		parser->model == D9tx || parser->model == ZOOPNOVO_A ||
    300 		parser->model == ZOOPNOVO_B || parser->model == VYPERNOVO ||
    301 		parser->model == D4F)
    302 		offset = 0x13;
    303 
    304 	if (abstract->size < offset + 7)
    305 		return DC_STATUS_DATAFORMAT;
    306 
    307 	const unsigned char *p = abstract->data + offset;
    308 
    309 	if (datetime) {
    310 		if (parser->model == D4i || parser->model == D6i ||
    311 			parser->model == D9tx || parser->model == DX ||
    312 			parser->model == ZOOPNOVO_A || parser->model == ZOOPNOVO_B ||
    313 			parser->model == VYPERNOVO || parser->model == D4F) {
    314 			datetime->year   = p[0] + (p[1] << 8);
    315 			datetime->month  = p[2];
    316 			datetime->day    = p[3];
    317 			datetime->hour   = p[4];
    318 			datetime->minute = p[5];
    319 			datetime->second = p[6];
    320 		} else {
    321 			datetime->hour   = p[0];
    322 			datetime->minute = p[1];
    323 			datetime->second = p[2];
    324 			datetime->year   = p[3] + (p[4] << 8);
    325 			datetime->month  = p[5];
    326 			datetime->day    = p[6];
    327 		}
    328 		datetime->timezone = DC_TIMEZONE_NONE;
    329 	}
    330 
    331 	return DC_STATUS_SUCCESS;
    332 }
    333 
    334 #define BUFLEN 16
    335 
    336 static dc_status_t
    337 suunto_d9_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value)
    338 {
    339 	suunto_d9_parser_t *parser = (suunto_d9_parser_t*) abstract;
    340 	const unsigned char *data = abstract->data;
    341 
    342 	// Cache the gas mix data.
    343 	dc_status_t rc = suunto_d9_parser_cache (parser);
    344 	if (rc != DC_STATUS_SUCCESS)
    345 		return rc;
    346 
    347 	dc_gasmix_t *gasmix = (dc_gasmix_t *) value;
    348 	dc_decomodel_t *decomodel = (dc_decomodel_t *) value;
    349 	dc_field_string_t *string = (dc_field_string_t *) value;
    350 
    351 	char buf[BUFLEN];
    352 
    353 	if (value) {
    354 		switch (type) {
    355 		case DC_FIELD_DIVETIME:
    356 			if (parser->model == D4)
    357 				*((unsigned int *) value) = array_uint16_le (data + 0x0B);
    358 			else if (parser->model == D4i || parser->model == D6i ||
    359 				parser->model == D9tx || parser->model == DX ||
    360 				parser->model == ZOOPNOVO_A || parser->model == ZOOPNOVO_B ||
    361 				parser->model == VYPERNOVO || parser->model == D4F)
    362 				*((unsigned int *) value) = array_uint16_le (data + 0x0D);
    363 			else if (parser->model == HELO2)
    364 				*((unsigned int *) value) = array_uint16_le (data + 0x0D) * 60;
    365 			else
    366 				*((unsigned int *) value) = array_uint16_le (data + 0x0B) * 60;
    367 			break;
    368 		case DC_FIELD_MAXDEPTH:
    369 			*((double *) value) = array_uint16_le (data + 0x09) / 100.0;
    370 			break;
    371 		case DC_FIELD_GASMIX_COUNT:
    372 			*((unsigned int *) value) = parser->ngasmixes;
    373 			break;
    374 		case DC_FIELD_GASMIX:
    375 			gasmix->usage = DC_USAGE_NONE;
    376 			gasmix->helium = parser->helium[flags] / 100.0;
    377 			gasmix->oxygen = parser->oxygen[flags] / 100.0;
    378 			gasmix->nitrogen = 1.0 - gasmix->oxygen - gasmix->helium;
    379 			break;
    380 		case DC_FIELD_DIVEMODE:
    381 			switch (parser->mode) {
    382 			case AIR:
    383 			case NITROX:
    384 			case MIXED:
    385 				*((dc_divemode_t *) value) = DC_DIVEMODE_OC;
    386 				break;
    387 			case GAUGE:
    388 				*((dc_divemode_t *) value) = DC_DIVEMODE_GAUGE;
    389 				break;
    390 			case FREEDIVE:
    391 				*((dc_divemode_t *) value) = DC_DIVEMODE_FREEDIVE;
    392 				break;
    393 			case CCR:
    394 				*((dc_divemode_t *) value) = DC_DIVEMODE_CCR;
    395 				break;
    396 			default:
    397 				return DC_STATUS_DATAFORMAT;
    398 			}
    399 			break;
    400 		case DC_FIELD_DECOMODEL:
    401 			decomodel->type = DC_DECOMODEL_RGBM;
    402 			if (parser->model == D4i ||parser->model == D6i ||
    403 				parser->model == D9tx || parser->model == ZOOPNOVO_A ||
    404 				parser->model == ZOOPNOVO_B || parser->model == VYPERNOVO ||
    405 				parser->model == D4F)
    406 				decomodel->conservatism = data[0x21] - 2;
    407 			else if (parser->model == HELO2)
    408 				decomodel->conservatism = data[0x23] - 2;
    409 			else if (parser->model == DX)
    410 				decomodel->conservatism = data[0x25] - 2;
    411 			else
    412 				decomodel->conservatism = data[0x1E];
    413 			break;
    414 		case DC_FIELD_STRING:
    415 			switch (flags) {
    416 			case 0: /* serial */
    417 				string->desc = STRING_KEY_SERIAL_NUMBER;
    418 				snprintf(buf, BUFLEN, "%08u", parser->serial);
    419 				break;
    420 			default:
    421 				return DC_STATUS_UNSUPPORTED;
    422 			}
    423 			string->value = strdup(buf);
    424 			break;
    425 		default:
    426 			return DC_STATUS_UNSUPPORTED;
    427 		}
    428 	}
    429 
    430 	return DC_STATUS_SUCCESS;
    431 }
    432 
    433 
    434 static dc_status_t
    435 suunto_d9_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata)
    436 {
    437 	suunto_d9_parser_t *parser = (suunto_d9_parser_t*) abstract;
    438 
    439 	const unsigned char *data = abstract->data;
    440 	unsigned int size = abstract->size;
    441 
    442 	// Cache the gas mix data.
    443 	dc_status_t rc = suunto_d9_parser_cache (parser);
    444 	if (rc != DC_STATUS_SUCCESS)
    445 		return rc;
    446 
    447 	// Number of parameters in the configuration data.
    448 	unsigned int nparams = data[parser->config];
    449 	if (nparams == 0 || nparams > MAXPARAMS) {
    450 		ERROR (abstract->context, "Invalid number of parameters.");
    451 		return DC_STATUS_DATAFORMAT;
    452 	}
    453 
    454 	// Available divisor values.
    455 	const unsigned int divisors[] = {1, 2, 4, 5, 10, 50, 100, 1000};
    456 
    457 	// Get the sample configuration.
    458 	sample_info_t info[MAXPARAMS] = {{0}};
    459 	for (unsigned int i = 0; i < nparams; ++i) {
    460 		unsigned int idx = parser->config + 2 + i * 3;
    461 		info[i].type     = data[idx + 0];
    462 		info[i].interval = data[idx + 1];
    463 		info[i].divisor  = divisors[(data[idx + 2] & 0x1C) >> 2];
    464 		switch (info[i].type) {
    465 		case 0x64: // Depth
    466 		case 0x68: // Pressure
    467 			info[i].size = 2;
    468 			break;
    469 		case 0x74: // Temperature
    470 			info[i].size = 1;
    471 			break;
    472 		default: // Unknown sample type
    473 			ERROR (abstract->context, "Unknown sample type 0x%02x.", info[i].type);
    474 			return DC_STATUS_DATAFORMAT;
    475 		}
    476 	}
    477 
    478 	// Offset to the profile data.
    479 	unsigned int profile = parser->config + 2 + nparams * 3;
    480 	if (profile + 5 > size) {
    481 		ERROR (abstract->context, "Buffer overflow detected!");
    482 		return DC_STATUS_DATAFORMAT;
    483 	}
    484 
    485 	// HelO2 dives can have an additional data block.
    486 	const unsigned char sequence[] = {0x01, 0x00, 0x00};
    487 	if (parser->model == HELO2 && memcmp (data + profile, sequence, sizeof (sequence)) != 0)
    488 		profile += 12;
    489 	if (profile + 5 > size) {
    490 		ERROR (abstract->context, "Buffer overflow detected!");
    491 		return DC_STATUS_DATAFORMAT;
    492 	}
    493 
    494 	// Sample recording interval.
    495 	unsigned int interval_sample_offset = 0x18;
    496 	if (parser->model == HELO2 || parser->model == D4i ||
    497 		parser->model == D6i || parser->model == D9tx ||
    498 		parser->model == ZOOPNOVO_A || parser->model == ZOOPNOVO_B ||
    499 		parser->model == VYPERNOVO || parser->model == D4F)
    500 		interval_sample_offset = 0x1E;
    501 	else if (parser->model == DX)
    502 		interval_sample_offset = 0x22;
    503 	unsigned int interval_sample = data[interval_sample_offset];
    504 	if (interval_sample == 0) {
    505 		ERROR (abstract->context, "Invalid sample interval.");
    506 		return DC_STATUS_DATAFORMAT;
    507 	}
    508 
    509 	// Offset to the first marker position.
    510 	unsigned int marker = array_uint16_le (data + profile + 3);
    511 
    512 	unsigned int in_deco = 0;
    513 	unsigned int time = 0;
    514 	unsigned int nsamples = 0;
    515 	unsigned int offset = profile + 5;
    516 	while (offset < size) {
    517 		dc_sample_value_t sample = {0};
    518 
    519 		// Time (seconds).
    520 		sample.time = time * 1000;
    521 		if (callback) callback (DC_SAMPLE_TIME, &sample, userdata);
    522 
    523 		// Sample data.
    524 		for (unsigned int i = 0; i < nparams; ++i) {
    525 			if (info[i].interval && (nsamples % info[i].interval) == 0) {
    526 				if (offset + info[i].size > size) {
    527 					ERROR (abstract->context, "Buffer overflow detected!");
    528 					return DC_STATUS_DATAFORMAT;
    529 				}
    530 
    531 				unsigned int value = 0;
    532 				switch (info[i].type) {
    533 				case 0x64: // Depth
    534 					value = array_uint16_le (data + offset);
    535 					sample.depth = value / (double) info[i].divisor;
    536 					if (callback) callback (DC_SAMPLE_DEPTH, &sample, userdata);
    537 					break;
    538 				case 0x68: // Pressure
    539 					value = array_uint16_le (data + offset);
    540 					if (value != 0xFFFF) {
    541 						sample.pressure.tank = 0;
    542 						sample.pressure.value = value / (double) info[i].divisor;
    543 						if (callback) callback (DC_SAMPLE_PRESSURE, &sample, userdata);
    544 					}
    545 					break;
    546 				case 0x74: // Temperature
    547 					sample.temperature = (signed char) data[offset] / (double) info[i].divisor;
    548 					if (callback) callback (DC_SAMPLE_TEMPERATURE, &sample, userdata);
    549 					break;
    550 				default: // Unknown sample type
    551 					ERROR (abstract->context, "Unknown sample type 0x%02x.", info[i].type);
    552 					return DC_STATUS_DATAFORMAT;
    553 				}
    554 
    555 				offset += info[i].size;
    556 			}
    557 		}
    558 
    559 		// Initial gasmix.
    560 		if (time == 0 && parser->ngasmixes > 0) {
    561 			if (parser->gasmix >= parser->ngasmixes) {
    562 				ERROR (abstract->context, "Invalid initial gas mix.");
    563 				return DC_STATUS_DATAFORMAT;
    564 			}
    565 			sample.gasmix = parser->gasmix;
    566 			if (callback) callback (DC_SAMPLE_GASMIX, &sample, userdata);
    567 		}
    568 
    569 		// Events
    570 		if ((nsamples + 1) == marker) {
    571 			while (offset < size) {
    572 				unsigned int event = data[offset++];
    573 				unsigned int seconds, type, DC_ATTR_UNUSED state, DC_ATTR_UNUSED number, heading;
    574 				unsigned int current, next;
    575 				unsigned int he, o2, ppo2, idx;
    576 				unsigned int length;
    577 
    578 				sample.event.type = SAMPLE_EVENT_NONE;
    579 				sample.event.time = 0;
    580 				sample.event.flags = 0;
    581 				sample.event.value = 0;
    582 				switch (event) {
    583 				case 0x01: // Next Event Marker
    584 					if (offset + 4 > size) {
    585 						ERROR (abstract->context, "Buffer overflow detected!");
    586 						return DC_STATUS_DATAFORMAT;
    587 					}
    588 					current = array_uint16_le (data + offset + 0);
    589 					next    = array_uint16_le (data + offset + 2);
    590 					if (marker != current) {
    591 						ERROR (abstract->context, "Unexpected event marker!");
    592 						return DC_STATUS_DATAFORMAT;
    593 					}
    594 					marker += next;
    595 					offset += 4;
    596 					break;
    597 				case 0x02: // Surfaced
    598 					if (offset + 2 > size) {
    599 						ERROR (abstract->context, "Buffer overflow detected!");
    600 						return DC_STATUS_DATAFORMAT;
    601 					}
    602 					state   = data[offset + 0];
    603 					seconds = data[offset + 1];
    604 					sample.event.type = SAMPLE_EVENT_SURFACE;
    605 					sample.event.time = seconds;
    606 					if (callback) callback (DC_SAMPLE_EVENT, &sample, userdata);
    607 					offset += 2;
    608 					break;
    609 				case 0x03: // Event
    610 					if (offset + 2 > size) {
    611 						ERROR (abstract->context, "Buffer overflow detected!");
    612 						return DC_STATUS_DATAFORMAT;
    613 					}
    614 					type    = data[offset + 0];
    615 					seconds = data[offset + 1];
    616 					switch (type & 0x7F) {
    617 					case 0x00: // Voluntary Safety Stop
    618 						sample.event.type = SAMPLE_EVENT_NONE;
    619 						if (type & 0x80)
    620 							in_deco &= ~SAFETYSTOP;
    621 						else
    622 							in_deco |= SAFETYSTOP;
    623 						break;
    624 					case 0x01: // Mandatory Safety Stop - odd concept; model as deco stop
    625 						sample.event.type = SAMPLE_EVENT_NONE;
    626 						if (type & 0x80)
    627 							in_deco &= ~DECOSTOP;
    628 						else
    629 							in_deco |= DECOSTOP;
    630 						break;
    631 					case 0x02: // Deep Safety Stop
    632 						sample.event.type = SAMPLE_EVENT_NONE;
    633 						if (type & 0x80)
    634 							in_deco &= ~DEEPSTOP;
    635 						else
    636 							in_deco |= DEEPSTOP;
    637 						break;
    638 					case 0x03: // Deco
    639 						sample.event.type = SAMPLE_EVENT_NONE;
    640 						if (type & 0x80)
    641 							in_deco &= ~DECOSTOP;
    642 						else
    643 							in_deco |= DECOSTOP;
    644 						break;
    645 					case 0x04: // Ascent Rate Warning
    646 						sample.event.type = SAMPLE_EVENT_ASCENT;
    647 						break;
    648 					case 0x05: // Ceiling Broken
    649 						sample.event.type = SAMPLE_EVENT_CEILING;
    650 						break;
    651 					case 0x06: // Mandatory Safety Stop Ceiling Error
    652 						sample.event.type = SAMPLE_EVENT_CEILING_SAFETYSTOP;
    653 						break;
    654 					case 0x07: // Below Deco Floor
    655 						sample.event.type = SAMPLE_EVENT_FLOOR;
    656 						break;
    657 					case 0x08: // Dive Time
    658 						sample.event.type = SAMPLE_EVENT_DIVETIME;
    659 						break;
    660 					case 0x09: // Depth Alarm
    661 						sample.event.type = SAMPLE_EVENT_MAXDEPTH;
    662 						break;
    663 					case 0x0A: // OLF 80
    664 						sample.event.type = SAMPLE_EVENT_OLF;
    665 						sample.event.value = 80;
    666 						break;
    667 					case 0x0B: // OLF 100
    668 						sample.event.type = SAMPLE_EVENT_OLF;
    669 						sample.event.value = 100;
    670 						break;
    671 					case 0x0C: // PO2
    672 						sample.event.type = SAMPLE_EVENT_PO2;
    673 						break;
    674 					case 0x0D: // Air Time Warning
    675 						sample.event.type = SAMPLE_EVENT_AIRTIME;
    676 						break;
    677 					case 0x0E: // RGBM Warning
    678 						sample.event.type = SAMPLE_EVENT_RGBM;
    679 						break;
    680 					case 0x0F: // PO2 High
    681 					case 0x10: // PO2 Low
    682 						sample.event.type = SAMPLE_EVENT_PO2;
    683 						break;
    684 					case 0x11: // Tissue Level Warning
    685 					case 0x12: // Tissue Calc Overflow
    686 						sample.event.type = SAMPLE_EVENT_TISSUELEVEL;
    687 						break;
    688 					case 0x13: // Deep Safety Stop
    689 						sample.event.type = SAMPLE_EVENT_NONE;
    690 						if (type & 0x80)
    691 							in_deco &= ~DEEPSTOP;
    692 						else
    693 							in_deco |= DEEPSTOP;
    694 						break;
    695 					case 0x14: // Mandatory Safety Stop - again, model as deco stop
    696 						sample.event.type = SAMPLE_EVENT_NONE;
    697 						if (type & 0x80)
    698 							in_deco &= ~DECOSTOP;
    699 						else
    700 							in_deco |= DECOSTOP;
    701 						break;
    702 					default: // Unknown
    703 						WARNING (abstract->context, "Unknown event type 0x%02x.", type);
    704 						break;
    705 					}
    706 					if (type & 0x80)
    707 						sample.event.flags = SAMPLE_FLAGS_END;
    708 					else
    709 						sample.event.flags = SAMPLE_FLAGS_BEGIN;
    710 					sample.event.time = seconds;
    711 					if (sample.event.type != SAMPLE_EVENT_NONE) {
    712 						if (callback) callback (DC_SAMPLE_EVENT, &sample, userdata);
    713 					}
    714 					offset += 2;
    715 					break;
    716 				case 0x04: // Bookmark/Heading
    717 					if (offset + 4 > size) {
    718 						ERROR (abstract->context, "Buffer overflow detected!");
    719 						return DC_STATUS_DATAFORMAT;
    720 					}
    721 					number  = data[offset + 0];
    722 					seconds = data[offset + 1];
    723 					heading = array_uint16_le (data + offset + 2);
    724 					if (heading == 0xFFFF) {
    725 						sample.event.type = SAMPLE_EVENT_BOOKMARK;
    726 						sample.event.value = 0;
    727 					} else {
    728 						sample.event.type = SAMPLE_EVENT_HEADING;
    729 						sample.event.value = heading / 2;
    730 					}
    731 					sample.event.time = seconds;
    732 					if (callback) callback (DC_SAMPLE_EVENT, &sample, userdata);
    733 					offset += 4;
    734 					break;
    735 				case 0x05: // Gas Change
    736 					if (offset + 2 > size) {
    737 						ERROR (abstract->context, "Buffer overflow detected!");
    738 						return DC_STATUS_DATAFORMAT;
    739 					}
    740 					o2 = data[offset + 0];
    741 					seconds = data[offset + 1];
    742 					idx = suunto_d9_parser_find_gasmix(parser, o2, 0);
    743 					if (idx >= parser->ngasmixes) {
    744 						ERROR (abstract->context, "Invalid gas mix.");
    745 						return DC_STATUS_DATAFORMAT;
    746 					}
    747 					sample.gasmix = idx;
    748 					if (callback) callback (DC_SAMPLE_GASMIX, &sample, userdata);
    749 					offset += 2;
    750 					break;
    751 				case 0x06: // Gas Change
    752 					if (parser->model == DX || parser->model == VYPERNOVO ||
    753 						(parser->model == D6i && parser->id == ID_D6I_V2)) {
    754 						length = 5;
    755 					} else {
    756 						length = 4;
    757 					}
    758 					if (offset + length > size) {
    759 						ERROR (abstract->context, "Buffer overflow detected!");
    760 						return DC_STATUS_DATAFORMAT;
    761 					}
    762 					type = data[offset + 0];
    763 					he = data[offset + 1];
    764 					o2 = data[offset + 2];
    765 					if (parser->model == DX || parser->model == VYPERNOVO ||
    766 						(parser->model == D6i && parser->id == ID_D6I_V2)) {
    767 						ppo2 = data[offset + 3];
    768 						seconds = data[offset + 4];
    769 					} else {
    770 						ppo2 = 0;
    771 						seconds = data[offset + 3];
    772 					}
    773 					idx = type & 0x0F;
    774 					if ((type & 0x80) == 0) {
    775 						idx += parser->nccr;
    776 					}
    777 					if (idx >= parser->ngasmixes || o2 != parser->oxygen[idx] || he != parser->helium[idx]) {
    778 						ERROR (abstract->context, "Invalid gas mix.");
    779 						return DC_STATUS_DATAFORMAT;
    780 					}
    781 					sample.gasmix = idx;
    782 					if (callback) callback (DC_SAMPLE_GASMIX, &sample, userdata);
    783 					if (type & 0x80) {
    784 						sample.setpoint = ppo2 / 10.0;
    785 						if (callback) callback (DC_SAMPLE_SETPOINT, &sample, userdata);
    786 					}
    787 					offset += length;
    788 					break;
    789 				default:
    790 					WARNING (abstract->context, "Unknown event 0x%02x.", event);
    791 					break;
    792 				}
    793 
    794 				if (event == 0x01)
    795 					break;
    796 			}
    797 		}
    798 
    799 		if (in_deco & DEEPSTOP) {
    800 			sample.deco.type = DC_DECO_DEEPSTOP;
    801 		} else if (in_deco & DECOSTOP) {
    802 			sample.deco.type = DC_DECO_DECOSTOP;
    803 		} else if (in_deco & SAFETYSTOP) {
    804 			sample.deco.type = DC_DECO_SAFETYSTOP;
    805 		} else {
    806 			sample.deco.type = DC_DECO_NDL;
    807 		}
    808 		sample.deco.time = 0;
    809 		sample.deco.depth = 0.0;
    810 		sample.deco.tts = 0;
    811 		if (callback) callback (DC_SAMPLE_DECO, &sample, userdata);
    812 
    813 		time += interval_sample;
    814 		nsamples++;
    815 	}
    816 
    817 	return DC_STATUS_SUCCESS;
    818 }