libdivecomputer-sys 0.1.0

Unsafe bindings for libdivecomputer
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
/*
 * libdivecomputer
 *
 * Copyright (C) 2014 Jef Driesen
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301 USA
 */

#include <stdlib.h>

#include "divesystem_idive.h"
#include "context-private.h"
#include "parser-private.h"
#include "array.h"

#define ISINSTANCE(parser) dc_device_isinstance((parser), &divesystem_idive_parser_vtable)

#define ISIX3M(model) ((model) >= 0x21)
#define ISIX3M2(model) ((model) >= 0x60 && (model) < 0x1000)

#define SZ_HEADER_IDIVE 0x32
#define SZ_SAMPLE_IDIVE 0x2A
#define SZ_HEADER_IX3M  0x36
#define SZ_SAMPLE_IX3M  0x36
#define SZ_SAMPLE_IX3M_APOS4 0x40

#define NGASMIXES 8
#define NTANKS    10

#define EPOCH 1199145600 /* 2008-01-01 00:00:00 */

#define OC       0
#define SCR      1
#define CCR      2
#define GAUGE    3
#define FREEDIVE 4
#define INVALID  0xFFFFFFFF

#define BUHLMANN 0
#define VPM      1
#define DUAL     2

#define IX3M2_BUHLMANN 0
#define IX3M2_ZHL16B   1
#define IX3M2_ZHL16C   2
#define IX3M2_VPM      3

typedef struct divesystem_idive_parser_t divesystem_idive_parser_t;

typedef struct divesystem_idive_gasmix_t {
	unsigned int oxygen;
	unsigned int helium;
} divesystem_idive_gasmix_t;

typedef struct divesystem_idive_tank_t {
	unsigned int id;
	unsigned int beginpressure;
	unsigned int endpressure;
} divesystem_idive_tank_t;

struct divesystem_idive_parser_t {
	dc_parser_t base;
	unsigned int model;
	unsigned int headersize;
	// Cached fields.
	unsigned int cached;
	unsigned int divemode;
	unsigned int divetime;
	unsigned int maxdepth;
	unsigned int ngasmixes;
	unsigned int ntanks;
	divesystem_idive_gasmix_t gasmix[NGASMIXES];
	divesystem_idive_tank_t tank[NTANKS];
	unsigned int algorithm;
	unsigned int gf_low;
	unsigned int gf_high;
};

static dc_status_t divesystem_idive_parser_set_data (dc_parser_t *abstract, const unsigned char *data, unsigned int size);
static dc_status_t divesystem_idive_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime);
static dc_status_t divesystem_idive_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value);
static dc_status_t divesystem_idive_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata);

static const dc_parser_vtable_t divesystem_idive_parser_vtable = {
	sizeof(divesystem_idive_parser_t),
	DC_FAMILY_DIVESYSTEM_IDIVE,
	divesystem_idive_parser_set_data, /* set_data */
	NULL, /* set_clock */
	NULL, /* set_atmospheric */
	NULL, /* set_density */
	divesystem_idive_parser_get_datetime, /* datetime */
	divesystem_idive_parser_get_field, /* fields */
	divesystem_idive_parser_samples_foreach, /* samples_foreach */
	NULL /* destroy */
};


dc_status_t
divesystem_idive_parser_create (dc_parser_t **out, dc_context_t *context, unsigned int model)
{
	divesystem_idive_parser_t *parser = NULL;

	if (out == NULL)
		return DC_STATUS_INVALIDARGS;

	// Allocate memory.
	parser = (divesystem_idive_parser_t *) dc_parser_allocate (context, &divesystem_idive_parser_vtable);
	if (parser == NULL) {
		ERROR (context, "Failed to allocate memory.");
		return DC_STATUS_NOMEMORY;
	}

	// Set the default values.
	parser->model = model;
	if (ISIX3M(model)) {
		parser->headersize = SZ_HEADER_IX3M;
	} else {
		parser->headersize = SZ_HEADER_IDIVE;
	}
	parser->cached = 0;
	parser->divemode = INVALID;
	parser->divetime = 0;
	parser->maxdepth = 0;
	parser->ngasmixes = 0;
	parser->ntanks = 0;
	for (unsigned int i = 0; i < NGASMIXES; ++i) {
		parser->gasmix[i].oxygen = 0;
		parser->gasmix[i].helium = 0;
	}
	for (unsigned int i = 0; i < NTANKS; ++i) {
		parser->tank[i].id = 0;
		parser->tank[i].beginpressure = 0;
		parser->tank[i].endpressure = 0;
	}
	parser->algorithm = INVALID;
	parser->gf_low = INVALID;
	parser->gf_high = INVALID;


	*out = (dc_parser_t*) parser;

	return DC_STATUS_SUCCESS;
}


static dc_status_t
divesystem_idive_parser_set_data (dc_parser_t *abstract, const unsigned char *data, unsigned int size)
{
	divesystem_idive_parser_t *parser = (divesystem_idive_parser_t *) abstract;

	// Reset the cache.
	parser->cached = 0;
	parser->divemode = INVALID;
	parser->divetime = 0;
	parser->maxdepth = 0;
	parser->ngasmixes = 0;
	parser->ntanks = 0;
	for (unsigned int i = 0; i < NGASMIXES; ++i) {
		parser->gasmix[i].oxygen = 0;
		parser->gasmix[i].helium = 0;
	}
	for (unsigned int i = 0; i < NTANKS; ++i) {
		parser->tank[i].id = 0;
		parser->tank[i].beginpressure = 0;
		parser->tank[i].endpressure = 0;
	}
	parser->algorithm = INVALID;
	parser->gf_low = INVALID;
	parser->gf_high = INVALID;

	return DC_STATUS_SUCCESS;
}


static dc_status_t
divesystem_idive_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime)
{
	divesystem_idive_parser_t *parser = (divesystem_idive_parser_t *) abstract;

	static const signed char tz_array[] = {
		-12,  0,    /* UTC-12    */
		-11,  0,    /* UTC-11    */
		-10,  0,    /* UTC-10    */
		 -9, 30,    /* UTC-9:30  */
		 -9,  0,    /* UTC-9     */
		 -8,  0,    /* UTC-8     */
		 -7,  0,    /* UTC-7     */
		 -6,  0,    /* UTC-6     */
		 -5,  0,    /* UTC-5     */
		 -4, 30,    /* UTC-4:30  */
		 -4,  0,    /* UTC-4     */
		 -3, 30,    /* UTC-3:30  */
		 -3,  0,    /* UTC-3     */
		 -2,  0,    /* UTC-2     */
		 -1,  0,    /* UTC-1     */
		  0,  0,    /* UTC       */
		  1,  0,    /* UTC+1     */
		  2,  0,    /* UTC+2     */
		  3,  0,    /* UTC+3     */
		  3, 30,    /* UTC+3:30  */
		  4,  0,    /* UTC+4     */
		  4, 30,    /* UTC+4:30  */
		  5,  0,    /* UTC+5     */
		  5, 30,    /* UTC+5:30  */
		  5, 45,    /* UTC+5:45  */
		  6,  0,    /* UTC+6     */
		  6, 30,    /* UTC+6:30  */
		  7,  0,    /* UTC+7     */
		  8,  0,    /* UTC+8     */
		  8, 45,    /* UTC+8:45  */
		  9,  0,    /* UTC+9     */
		  9, 30,    /* UTC+9:30  */
		  9, 45,    /* UTC+9:45  */
		 10,  0,    /* UTC+10    */
		 10, 30,    /* UTC+10:30 */
		 11,  0,    /* UTC+11    */
		 11, 30,    /* UTC+11:30 */
		 12,  0,    /* UTC+12    */
		 12, 45,    /* UTC+12:45 */
		 13,  0,    /* UTC+13    */
		 13, 45,    /* UTC+13:45 */
		 14,  0     /* UTC+14    */
	};

	if (abstract->size < parser->headersize)
		return DC_STATUS_DATAFORMAT;

	dc_ticks_t ticks = array_uint32_le(abstract->data + 7) + EPOCH;

	// Detect the APOS4 firmware.
	unsigned int firmware = 0;
	unsigned int apos4 = 0;
	if (ISIX3M(parser->model)) {
		firmware = array_uint32_le(abstract->data + 0x2A);
		apos4 = (firmware / 10000000) >= 4;
	} else {
		firmware = array_uint32_le(abstract->data + 0x2E);
		apos4 = 0;
	}

	if (apos4) {
		// For devices with timezone support, the UTC offset of the
		// device is used. The UTC offset is stored as an index in the
		// timezone table.
		unsigned int tz_idx = abstract->data[48];
		if ((tz_idx % 2) != 0 || tz_idx >= C_ARRAY_SIZE(tz_array)) {
			ERROR (abstract->context, "Invalid timezone index (%u).", tz_idx);
			return DC_STATUS_DATAFORMAT;
		}

		int timezone = tz_array[tz_idx] * 3600;
		if (timezone < 0) {
			timezone -= tz_array[tz_idx + 1] * 60;
		} else {
			timezone += tz_array[tz_idx + 1] * 60;
		}

		ticks += timezone;

		if (!dc_datetime_gmtime (datetime, ticks))
			return DC_STATUS_DATAFORMAT;

		datetime->timezone = timezone;
	} else {
		// For devices without timezone support, the current timezone of
		// the host system is used.
		if (!dc_datetime_localtime (datetime, ticks))
			return DC_STATUS_DATAFORMAT;
	}

	return DC_STATUS_SUCCESS;
}


static dc_status_t
divesystem_idive_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value)
{
	divesystem_idive_parser_t *parser = (divesystem_idive_parser_t *) abstract;
	const unsigned char *data = abstract->data;

	if (abstract->size < parser->headersize)
		return DC_STATUS_DATAFORMAT;

	if (!parser->cached) {
		dc_status_t rc = divesystem_idive_parser_samples_foreach (abstract, NULL, NULL);
		if (rc != DC_STATUS_SUCCESS)
			return rc;
	}

	dc_gasmix_t *gasmix = (dc_gasmix_t *) value;
	dc_tank_t *tank = (dc_tank_t *) value;
	dc_salinity_t *water = (dc_salinity_t *) value;
	dc_decomodel_t *decomodel = (dc_decomodel_t *) value;

	if (value) {
		switch (type) {
		case DC_FIELD_DIVETIME:
			*((unsigned int *) value) = parser->divetime;
			break;
		case DC_FIELD_MAXDEPTH:
			*((double *) value) = parser->maxdepth / 10.0;
			break;
		case DC_FIELD_GASMIX_COUNT:
			*((unsigned int *) value) = parser->ngasmixes;
			break;
		case DC_FIELD_GASMIX:
			gasmix->helium = parser->gasmix[flags].helium / 100.0;
			gasmix->oxygen = parser->gasmix[flags].oxygen / 100.0;
			gasmix->nitrogen = 1.0 - gasmix->oxygen - gasmix->helium;
			break;
		case DC_FIELD_TANK_COUNT:
			*((unsigned int *) value) = parser->ntanks;
			break;
		case DC_FIELD_TANK:
			tank->type = DC_TANKVOLUME_NONE;
			tank->volume = 0.0;
			tank->workpressure = 0.0;
			tank->beginpressure = parser->tank[flags].beginpressure;
			tank->endpressure   = parser->tank[flags].endpressure;
			tank->gasmix = DC_GASMIX_UNKNOWN;
			break;
		case DC_FIELD_ATMOSPHERIC:
			if (ISIX3M(parser->model)) {
				*((double *) value) = array_uint16_le (data + 11) / 10000.0;
			} else {
				*((double *) value) = array_uint16_le (data + 11) / 1000.0;
			}
			break;
		case DC_FIELD_SALINITY:
			water->type = data[34] == 0 ? DC_WATER_SALT : DC_WATER_FRESH;
			water->density = 0.0;
			break;
		case DC_FIELD_DIVEMODE:
			if (parser->divemode == INVALID)
				return DC_STATUS_UNSUPPORTED;
			switch (parser->divemode) {
			case OC:
				*((dc_divemode_t *) value) = DC_DIVEMODE_OC;
				break;
			case SCR:
				*((dc_divemode_t *) value) = DC_DIVEMODE_SCR;
				break;
			case CCR:
				*((dc_divemode_t *) value) = DC_DIVEMODE_CCR;
				break;
			case GAUGE:
				*((dc_divemode_t *) value) = DC_DIVEMODE_GAUGE;
				break;
			case FREEDIVE:
				*((dc_divemode_t *) value) = DC_DIVEMODE_FREEDIVE;
				break;
			default:
				ERROR (abstract->context, "Unknown dive mode %02x.", parser->divemode);
				return DC_STATUS_DATAFORMAT;
			}
			break;
		case DC_FIELD_DECOMODEL:
			if (parser->algorithm == INVALID)
				return DC_STATUS_UNSUPPORTED;
			if (ISIX3M2(parser->model)) {
				switch (parser->algorithm) {
				case IX3M2_BUHLMANN:
				case IX3M2_ZHL16B:
				case IX3M2_ZHL16C:
					decomodel->type = DC_DECOMODEL_BUHLMANN;
					decomodel->conservatism = 0;
					decomodel->params.gf.low = parser->gf_low;
					decomodel->params.gf.high = parser->gf_high;
					break;
				case IX3M2_VPM:
					decomodel->type = DC_DECOMODEL_VPM;
					decomodel->conservatism = 0;
					break;
				default:
					ERROR (abstract->context, "Unknown deco algorithm %02x.", parser->algorithm);
					return DC_STATUS_DATAFORMAT;
				}
			} else {
				switch (parser->algorithm) {
				case BUHLMANN:
				case DUAL:
					decomodel->type = DC_DECOMODEL_BUHLMANN;
					decomodel->conservatism = 0;
					decomodel->params.gf.low = parser->gf_low;
					decomodel->params.gf.high = parser->gf_high;
					break;
				case VPM:
					decomodel->type = DC_DECOMODEL_VPM;
					decomodel->conservatism = 0;
					break;
				default:
					ERROR (abstract->context, "Unknown deco algorithm %02x.", parser->algorithm);
					return DC_STATUS_DATAFORMAT;
				}
			}
			break;
		default:
			return DC_STATUS_UNSUPPORTED;
		}
	}

	return DC_STATUS_SUCCESS;
}


static dc_status_t
divesystem_idive_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata)
{
	divesystem_idive_parser_t *parser = (divesystem_idive_parser_t *) abstract;
	const unsigned char *data = abstract->data;
	unsigned int size = abstract->size;

	unsigned int time = 0;
	unsigned int maxdepth = 0;
	unsigned int ngasmixes = 0;
	unsigned int ntanks = 0;
	divesystem_idive_gasmix_t gasmix[NGASMIXES] = {0};
	divesystem_idive_tank_t tank[NTANKS] = {0};
	unsigned int o2_previous = INVALID;
	unsigned int he_previous = INVALID;
	unsigned int mode_previous = INVALID;
	unsigned int divemode = INVALID;
	unsigned int tank_previous = INVALID;
	unsigned int tank_idx = INVALID;
	unsigned int algorithm = INVALID;
	unsigned int algorithm_previous = INVALID;
	unsigned int gf_low = INVALID;
	unsigned int gf_high = INVALID;

	unsigned int firmware = 0;
	unsigned int apos4 = 0;
	unsigned int nsamples = array_uint16_le (data + 1);
	unsigned int samplesize = SZ_SAMPLE_IDIVE;
	if (ISIX3M(parser->model)) {
		// Detect the APOS4 firmware.
		firmware = array_uint32_le(data + 0x2A);
		apos4 = (firmware / 10000000) >= 4;
		if (apos4) {
			// Dive downloaded and recorded with the APOS4 firmware.
			samplesize = SZ_SAMPLE_IX3M_APOS4;
		} else if (size == parser->headersize + nsamples * SZ_SAMPLE_IX3M_APOS4) {
			// Dive downloaded with the APOS4 firmware, but recorded
			// with an older firmware.
			samplesize = SZ_SAMPLE_IX3M_APOS4;
		} else {
			// Dive downloaded and recorded with an older firmware.
			samplesize = SZ_SAMPLE_IX3M;
		}
	} else {
		firmware = array_uint32_le(data + 0x2E);
	}

	unsigned int offset = parser->headersize;
	while (offset + samplesize <= size) {
		dc_sample_value_t sample = {0};

		// Time (seconds).
		unsigned int timestamp = array_uint32_le (data + offset + 2);
		if (timestamp <= time) {
			ERROR (abstract->context, "Timestamp moved backwards.");
			return DC_STATUS_DATAFORMAT;
		}
		time = timestamp;
		sample.time = timestamp;
		if (callback) callback (DC_SAMPLE_TIME, sample, userdata);

		// Depth (1/10 m).
		unsigned int depth = array_uint16_le (data + offset + 6);
		if (maxdepth < depth)
			maxdepth = depth;
		sample.depth = depth / 10.0;
		if (callback) callback (DC_SAMPLE_DEPTH, sample, userdata);

		// Temperature (Celsius).
		signed int temperature = (signed short) array_uint16_le (data + offset + 8);
		sample.temperature = temperature / 10.0;
		if (callback) callback (DC_SAMPLE_TEMPERATURE, sample, userdata);

		// Dive mode
		unsigned int mode = data[offset + 18];
		if (mode != mode_previous) {
			if (mode_previous != INVALID) {
				WARNING (abstract->context, "Dive mode changed from %02x to %02x.", mode_previous, mode);
			}
			mode_previous = mode;
		}
		if (divemode == INVALID) {
			divemode = mode;
		}

		// Deco model
		unsigned int s_algorithm = data[offset + 14];
		unsigned int s_gf_high = data[offset + 15];
		unsigned int s_gf_low  = data[offset + 16];
		if (s_algorithm != algorithm_previous) {
			if (algorithm_previous != INVALID) {
				WARNING (abstract->context, "Deco algorithm changed from %02x to %02x.", algorithm_previous, s_algorithm);
			}
			algorithm_previous = s_algorithm;
		}
		if (algorithm == INVALID) {
			algorithm = s_algorithm;
			gf_low = s_gf_low;
			gf_high = s_gf_high;
		}

		// Setpoint
		if (mode == SCR || mode == CCR) {
			unsigned int setpoint = array_uint16_le (data + offset + 19);
			sample.setpoint = setpoint / 1000.0;
			if (callback) callback (DC_SAMPLE_SETPOINT, sample, userdata);
		}

		// Gaschange.
		unsigned int o2 = data[offset + 10];
		unsigned int he = data[offset + 11];
		if (o2 != o2_previous || he != he_previous) {
			// Find the gasmix in the list.
			unsigned int i = 0;
			while (i < ngasmixes) {
				if (o2 == gasmix[i].oxygen && he == gasmix[i].helium)
					break;
				i++;
			}

			// Add it to list if not found.
			if (i >= ngasmixes) {
				if (i >= NGASMIXES) {
					ERROR (abstract->context, "Maximum number of gas mixes reached.");
					return DC_STATUS_DATAFORMAT;
				}
				gasmix[i].oxygen = o2;
				gasmix[i].helium = he;
				ngasmixes = i + 1;
			}

			sample.gasmix = i;
			if (callback) callback (DC_SAMPLE_GASMIX, sample, userdata);
			o2_previous = o2;
			he_previous = he;
		}

		// Deco stop / NDL.
		unsigned int decostop = 0, decotime = 0, tts = 0;
		if (apos4) {
			decostop = array_uint16_le (data + offset + 21);
			decotime = array_uint16_le (data + offset + 23);
			tts      = array_uint16_le (data + offset + 25);
		} else {
			decostop = array_uint16_le (data + offset + 21);
			tts      = array_uint16_le (data + offset + 23);
		}
		if (decostop) {
			sample.deco.type = DC_DECO_DECOSTOP;
			sample.deco.depth = decostop / 10.0;
			sample.deco.time = apos4 ? decotime : tts;
		} else {
			sample.deco.type = DC_DECO_NDL;
			sample.deco.depth = 0.0;
			sample.deco.time = tts;
		}
		if (callback) callback (DC_SAMPLE_DECO, sample, userdata);

		// CNS
		unsigned int cns = array_uint16_le (data + offset + 29);
		sample.cns = cns / 100.0;
		if (callback) callback (DC_SAMPLE_CNS, sample, userdata);

		// Tank Pressure
		if (samplesize == SZ_SAMPLE_IX3M_APOS4) {
			unsigned int id = data[offset + 47] & 0x0F;
			unsigned int flags = data[offset + 47] & 0xF0;
			unsigned int pressure = data[offset + 49];

			if (flags & 0x20) {
				// 300 bar transmitter.
				pressure *= 2;
			}

			if (flags & 0x80) {
				// No active transmitter available
			} else if (flags & 0x40) {
				// Transmitter connection lost
				sample.event.type = SAMPLE_EVENT_TRANSMITTER;
				sample.event.time = 0;
				sample.event.flags = 0;
				sample.event.value = 0;
				if (callback) callback (DC_SAMPLE_EVENT, sample, userdata);
			} else {
				// Get the index of the tank.
				if (id != tank_previous) {
					unsigned int i = 0;
					while (i < ntanks) {
						if (id == tank[i].id)
							break;
						i++;
					}

					tank_previous = id;
					tank_idx = i;
				}

				// Add a new tank if necessary.
				if (tank_idx >= ntanks && pressure != 0) {
					if (tank_idx >= NTANKS) {
						ERROR (abstract->context, "Maximum number of tanks reached.");
						return DC_STATUS_DATAFORMAT;
					}
					tank[tank_idx].id = id;
					tank[tank_idx].beginpressure = pressure;
					tank[tank_idx].endpressure = pressure;
					ntanks = tank_idx + 1;
				}

				if (tank_idx < ntanks) {
					sample.pressure.tank = tank_idx;
					sample.pressure.value = pressure;
					if (callback) callback (DC_SAMPLE_PRESSURE, sample, userdata);
					tank[tank_idx].endpressure = pressure;
				}
			}
		}

		offset += samplesize;
	}

	// Cache the data for later use.
	for (unsigned int i = 0; i < ntanks; ++i) {
		parser->tank[i] = tank[i];
	}
	for (unsigned int i = 0; i < ngasmixes; ++i) {
		parser->gasmix[i] = gasmix[i];
	}
	parser->ngasmixes = ngasmixes;
	parser->ntanks = ntanks;
	parser->maxdepth = maxdepth;
	parser->divetime = time;
	parser->divemode = divemode;
	parser->algorithm = algorithm;
	parser->gf_low = gf_low;
	parser->gf_high = gf_high;
	parser->cached = 1;

	return DC_STATUS_SUCCESS;
}