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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
/*
 * libdivecomputer
 *
 * Copyright (C) 2021 Ryan Gardner, 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 <stdio.h>
#include <stdarg.h>
#include <string.h>

#include <libdivecomputer/units.h>

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

#define HEADERSIZE_MIN 128

#define MAX_SAMPLES   7
#define MAX_EVENTS    7
#define MAX_GASMIXES  20

#define ALARM        0x0001
#define TEMPERATURE  0x0002
#define DECO         0x0003
#define CEILING      0x0004
#define CNS          0x0005

#define SAMPLE_TEMPERATURE 0
#define SAMPLE_DECO_NDL    1
#define SAMPLE_CNS         2

#define EVENT_CHANGE_GAS            7
#define EVENT_ALARMS                8
#define EVENT_CHANGE_SETPOINT       9
#define EVENT_SAMPLES_MISSED       10
#define EVENT_RESERVED             15

#define ALARM_ASCENTRATE  0
#define ALARM_CEILING     1
#define ALARM_PO2         2
#define ALARM_MAXDEPTH    3
#define ALARM_DIVETIME    4
#define ALARM_CNS         5

#define DECOSTOP   0x02
#define SAFETYSTOP 0x04

#define DENSITY 1024

#define UNDEFINED 0xFFFFFFFF

#define FWVERSION(major,minor) ( \
		((((major) + '0') & 0xFF) << 8) | \
		((minor) & 0xFF))

typedef struct deepsix_excursion_layout_t {
	unsigned int headersize;
	unsigned int version;
	unsigned int divemode;
	unsigned int samplerate;
	unsigned int salinity;
	unsigned int datetime;
	unsigned int divetime;
	unsigned int maxdepth;
	unsigned int temperature_min;
	unsigned int avgdepth;
	unsigned int firmware;
	unsigned int temperature_surf;
	unsigned int atmospheric;
	unsigned int gf;
} deepsix_excursion_layout_t;

typedef struct deepsix_excursion_gasmix_t {
	unsigned int id;
	unsigned int oxygen;
	unsigned int helium;
} deepsix_excursion_gasmix_t;

typedef struct deepsix_excursion_sample_info_t {
	unsigned int type;
	unsigned int divisor;
	unsigned int size;
} deepsix_excursion_sample_info_t;

typedef struct deepsix_excursion_event_info_t {
	unsigned int type;
	unsigned int size;
} deepsix_excursion_event_info_t;

typedef struct deepsix_excursion_parser_t {
	dc_parser_t base;
	unsigned int cached;
	unsigned int ngasmixes;
	deepsix_excursion_gasmix_t gasmix[MAX_GASMIXES];
} deepsix_excursion_parser_t;

static dc_status_t deepsix_excursion_parser_set_data (dc_parser_t *abstract, const unsigned char *data, unsigned int size);
static dc_status_t deepsix_excursion_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime);
static dc_status_t deepsix_excursion_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value);
static dc_status_t deepsix_excursion_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata);
static dc_status_t deepsix_excursion_parser_samples_foreach_v0 (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata);
static dc_status_t deepsix_excursion_parser_samples_foreach_v1 (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata);

static const dc_parser_vtable_t deepsix_parser_vtable = {
	sizeof(deepsix_excursion_parser_t),
	DC_FAMILY_DEEPSIX_EXCURSION,
	deepsix_excursion_parser_set_data, /* set_data */
	NULL, /* set_clock */
	NULL, /* set_atmospheric */
	NULL, /* set_density */
	deepsix_excursion_parser_get_datetime, /* datetime */
	deepsix_excursion_parser_get_field, /* fields */
	deepsix_excursion_parser_samples_foreach, /* samples_foreach */
	NULL /* destroy */
};

static const deepsix_excursion_layout_t deepsix_excursion_layout_v0 = {
	156,/* headersize */
	UNDEFINED, /* version */
	4,  /* divemode */
	24, /* samplerate */
	UNDEFINED, /* salinity */
	12, /* datetime */
	20, /* divetime */
	28, /* maxdepth */
	32, /* temperature_min */
	UNDEFINED, /* avgdepth */
	48, /* firmware */
	UNDEFINED, /* temperature_surf */
	56, /* atmospheric */
	UNDEFINED, /* gf */
};

static const deepsix_excursion_layout_t deepsix_excursion_layout_v1 = {
	129,/* headersize */
	3,  /* version */
	4,  /* divemode */
	5,  /* samplerate */
	7,  /* salinity */
	12, /* datetime */
	19, /* divetime */
	29, /* maxdepth */
	31, /* temperature_min */
	33, /* avgdepth */
	35, /* firmware */
	43, /* temperature_surf */
	45, /* atmospheric */
	127, /* gf */
};

static double
pressure_to_depth(unsigned int depth, unsigned int atmospheric, unsigned int density)
{
	return ((signed int)(depth - atmospheric)) * (BAR / 1000.0) / (density * GRAVITY);
}

static unsigned int
deepsix_excursion_find_gasmix(deepsix_excursion_parser_t *parser, unsigned int o2, unsigned int he, unsigned int id)
{
	unsigned int i = 0;
	while (i < parser->ngasmixes) {
		if (o2 == parser->gasmix[i].oxygen && he == parser->gasmix[i].helium && id == parser->gasmix[i].id)
			break;
		i++;
	}
	return i;
}

dc_status_t
deepsix_excursion_parser_create (dc_parser_t **out, dc_context_t *context)
{
	deepsix_excursion_parser_t *parser = NULL;

	if (out == NULL)
		return DC_STATUS_INVALIDARGS;

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

	// Set the default values.
	parser->cached = 0;
	parser->ngasmixes = 0;
	for (unsigned int i = 0; i < MAX_GASMIXES; ++i) {
		parser->gasmix[i].id = 0;
		parser->gasmix[i].oxygen = 0;
		parser->gasmix[i].helium = 0;
	}

	*out = (dc_parser_t *) parser;

	return DC_STATUS_SUCCESS;
}

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

	// Reset the cache.
	parser->cached = 0;
	parser->ngasmixes = 0;
	for (unsigned int i = 0; i < MAX_GASMIXES; ++i) {
		parser->gasmix[i].id = 0;
		parser->gasmix[i].oxygen = 0;
		parser->gasmix[i].helium = 0;
	}

	return DC_STATUS_SUCCESS;
}

static dc_status_t
deepsix_excursion_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime)
{
	const unsigned char *data = abstract->data;
	unsigned int size = abstract->size;

	if (size < HEADERSIZE_MIN)
		return DC_STATUS_DATAFORMAT;

	unsigned int version = data[3];
	const deepsix_excursion_layout_t *layout = version == 0 ?
		&deepsix_excursion_layout_v0 : &deepsix_excursion_layout_v1;

	if (size < layout->headersize)
		return DC_STATUS_DATAFORMAT;

	unsigned int firmware = array_uint16_be (data + layout->firmware + 4);

	const unsigned char *p = data + layout->datetime;

	if (datetime) {
		datetime->year   = p[0] + 2000;
		datetime->month  = p[1];
		datetime->day    = p[2];
		datetime->hour   = p[3];
		datetime->minute = p[4];
		datetime->second = p[5];

		if (version == 0) {
			if (firmware >= FWVERSION(5, 'B')) {
				datetime->timezone = (p[6] - 12) * 3600;
			} else {
				datetime->timezone = DC_TIMEZONE_NONE;
			}
		} else {
			datetime->timezone = ((signed char) p[6]) * 900;
		}
	}

	return DC_STATUS_SUCCESS;
}

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

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

	if (size < HEADERSIZE_MIN)
		return DC_STATUS_DATAFORMAT;

	unsigned int version = data[3];
	const deepsix_excursion_layout_t *layout = version == 0 ?
		&deepsix_excursion_layout_v0 : &deepsix_excursion_layout_v1;

	if (size < layout->headersize)
		return DC_STATUS_DATAFORMAT;

	if (version != 0 && !parser->cached) {
		dc_status_t rc = deepsix_excursion_parser_samples_foreach_v1(abstract, NULL, NULL);
		if (rc != DC_STATUS_SUCCESS)
			return rc;
	}

	unsigned int atmospheric = array_uint16_le(data + layout->atmospheric);
	unsigned int density = DENSITY;
	if (layout->salinity != UNDEFINED) {
		density = 1000 + data[layout->salinity] * 10;
	}

	if (value) {
		switch (type) {
		case DC_FIELD_DIVETIME:
			*((unsigned int *) value) = array_uint32_le(data + layout->divetime);
			break;
		case DC_FIELD_MAXDEPTH:
			*((double *) value) = pressure_to_depth(array_uint16_le(data + layout->maxdepth), atmospheric, density);
			break;
		case DC_FIELD_AVGDEPTH:
			if (layout->avgdepth == UNDEFINED)
				return DC_STATUS_UNSUPPORTED;
			*((double *) value) = pressure_to_depth(array_uint16_le(data + layout->avgdepth), atmospheric, density);
			break;
		case DC_FIELD_GASMIX_COUNT:
			*((unsigned int *) value) = parser->ngasmixes;
			break;
		case DC_FIELD_GASMIX:
			gasmix->oxygen   = parser->gasmix[flags].oxygen / 100.0;
			gasmix->helium   = parser->gasmix[flags].helium / 100.0;
			gasmix->nitrogen = 1.0 - gasmix->oxygen - gasmix->helium;
			break;
		case DC_FIELD_TEMPERATURE_MINIMUM:
			*((double *) value) = (signed int) array_uint16_le(data + layout->temperature_min) / 10.0;
			break;
		case DC_FIELD_TEMPERATURE_SURFACE:
			if (layout->temperature_surf == UNDEFINED)
				return DC_STATUS_UNSUPPORTED;
			*((double *) value) = (signed int) array_uint16_le(data + layout->temperature_surf) / 10.0;
			break;
		case DC_FIELD_ATMOSPHERIC:
			*((double *) value) = atmospheric / 1000.0;
			break;
		case DC_FIELD_SALINITY:
			water->type	= (density == 1000) ? DC_WATER_FRESH : DC_WATER_SALT;
			water->density = density;
			break;
		case DC_FIELD_DIVEMODE:
			switch (data[layout->divemode]) {
			case 0:
				*((dc_divemode_t *) value) = DC_DIVEMODE_OC;
				break;
			case 1:
				*((dc_divemode_t *) value) = DC_DIVEMODE_GAUGE;
				break;
			case 2:
				*((dc_divemode_t *) value) = DC_DIVEMODE_FREEDIVE;
				break;
			default:
				return DC_STATUS_DATAFORMAT;
			}
			break;
		case DC_FIELD_DECOMODEL:
			decomodel->type = DC_DECOMODEL_BUHLMANN;
			decomodel->conservatism = 0;
			if (layout->gf != UNDEFINED) {
				decomodel->params.gf.low  = data[layout->gf + 0];
				decomodel->params.gf.high = data[layout->gf + 1];
			} else {
				decomodel->params.gf.low  = 0;
				decomodel->params.gf.high = 0;
			}
			break;
		default:
			return DC_STATUS_UNSUPPORTED;
		}
	}

	return DC_STATUS_SUCCESS;
}

static dc_status_t
deepsix_excursion_parser_samples_foreach_v0 (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata)
{
	const unsigned char *data = abstract->data;
	unsigned int size = abstract->size;
	const deepsix_excursion_layout_t *layout = &deepsix_excursion_layout_v0;

	if (size < layout->headersize)
		return DC_STATUS_DATAFORMAT;

	int firmware4c = memcmp(data + layout->firmware, "D01-4C", 6) == 0;

	unsigned int maxtype = firmware4c ? TEMPERATURE : CNS;

	unsigned int interval = array_uint32_le(data + layout->samplerate);
	unsigned int atmospheric = array_uint32_le(data + layout->atmospheric);

	unsigned int time = 0;
	unsigned int offset = layout->headersize;
	while (offset + 1 < size) {
		dc_sample_value_t sample = {0};

		// Get the sample type.
		unsigned int type = data[offset];
		if (type < 1 || type > maxtype) {
			ERROR (abstract->context, "Unknown sample type (%u).", type);
			return DC_STATUS_DATAFORMAT;
		}

		// Get the sample length.
		unsigned int length = 1;
		if (type == ALARM || type == CEILING) {
			length = 8;
		} else if (type == TEMPERATURE || type == DECO || type == CNS) {
			length = 6;
		}

		// Verify the length.
		if (offset + length > size) {
			WARNING (abstract->context, "Unexpected end of data.");
			break;
		}

		unsigned int misc = data[offset + 1];
		unsigned int depth = array_uint16_le(data + offset + 2);

		if (type == TEMPERATURE) {
			time += interval;
			sample.time = time;
			if (callback) callback(DC_SAMPLE_TIME, sample, userdata);

			sample.depth = pressure_to_depth(depth, atmospheric, DENSITY);
			if (callback) callback(DC_SAMPLE_DEPTH, sample, userdata);
		}

		if (type == ALARM) {
			unsigned int alarm_time  = array_uint16_le(data + offset + 4);
			unsigned int alarm_value = array_uint16_le(data + offset + 6);
		} else if (type == TEMPERATURE) {
			unsigned int temperature = array_uint16_le(data + offset + 4);
			if (firmware4c) {
				if (temperature > 1300) {
					length = 8;
				} else if (temperature >= 10) {
					sample.temperature = temperature / 10.0;
					if (callback) callback(DC_SAMPLE_TEMPERATURE, sample, userdata);
				}
			} else {
				sample.temperature = temperature / 10.0;
				if (callback) callback(DC_SAMPLE_TEMPERATURE, sample, userdata);
			}
		} else if (type == DECO) {
			unsigned int deco = array_uint16_le(data + offset + 4);
		} else if (type == CEILING) {
			unsigned int ceiling_depth = array_uint16_le(data + offset + 4);
			unsigned int ceiling_time  = array_uint16_le(data + offset + 6);
		} else if (type == CNS) {
			unsigned int cns = array_uint16_le(data + offset + 4);
			sample.cns = cns / 100.0;
			if (callback) callback(DC_SAMPLE_CNS, sample, userdata);
		}

		offset += length;
	}

	return DC_STATUS_SUCCESS;
}

static dc_status_t
deepsix_excursion_parser_samples_foreach_v1 (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata)
{
	deepsix_excursion_parser_t *parser = (deepsix_excursion_parser_t *) abstract;
	const unsigned char *data = abstract->data;
	unsigned int size = abstract->size;
	const deepsix_excursion_layout_t *layout = &deepsix_excursion_layout_v1;

	if (size < layout->headersize)
		return DC_STATUS_DATAFORMAT;

	unsigned int headersize = data[2];
	if (headersize < layout->headersize)
		return DC_STATUS_DATAFORMAT;

	unsigned int samplerate = data[layout->samplerate];
	unsigned int atmospheric = array_uint16_le(data + layout->atmospheric);
	unsigned int density = 1000 + data[layout->salinity] * 10;

	unsigned int offset = headersize;
	if (offset + 1 > size) {
		ERROR (abstract->context, "Buffer overflow detected!");
		return DC_STATUS_DATAFORMAT;
	}

	unsigned int nconfig = data[offset];
	if (nconfig > MAX_SAMPLES) {
		ERROR(abstract->context, "Too many sample descriptors (%u).", nconfig);
		return DC_STATUS_DATAFORMAT;
	}

	offset += 1;

	if (offset + 3 * nconfig > size) {
		ERROR (abstract->context, "Buffer overflow detected!");
		return DC_STATUS_DATAFORMAT;
	}

	deepsix_excursion_sample_info_t sample_info[MAX_SAMPLES] = {{0}};
	for (unsigned int i = 0; i < nconfig; i++) {
		sample_info[i].type    = data[offset + 3 * i + 0];
		sample_info[i].size    = data[offset + 3 * i + 1];
		sample_info[i].divisor = data[offset + 3 * i + 2];

		if (sample_info[i].divisor) {
			switch (sample_info[i].type) {
			case SAMPLE_CNS:
			case SAMPLE_TEMPERATURE:
				if (sample_info[i].size != 2) {
					ERROR(abstract->context, "Unexpected sample size (%u).", sample_info[i].size);
					return DC_STATUS_DATAFORMAT;
				}
				break;
			case SAMPLE_DECO_NDL:
				if (sample_info[i].size != 7) {
					ERROR(abstract->context, "Unexpected sample size (%u).", sample_info[i].size);
					return DC_STATUS_DATAFORMAT;
				}
				break;
			default:
				WARNING (abstract->context, "Unknown sample descriptor (%u %u %u).",
					sample_info[i].type, sample_info[i].size, sample_info[i].divisor);
				break;
			}
		}
	}

	offset += 3 * nconfig;

	if (offset + 1 > size) {
		ERROR (abstract->context, "Buffer overflow detected!");
		return DC_STATUS_DATAFORMAT;
	}

	unsigned int nevents = data[offset];
	if (nevents > MAX_EVENTS) {
		ERROR(abstract->context, "Too many event descriptors (%u).", nevents);
		return DC_STATUS_DATAFORMAT;
	}

	offset += 1;

	if (offset + 2 * nevents > size) {
		ERROR (abstract->context, "Buffer overflow detected!");
		return DC_STATUS_DATAFORMAT;
	}

	deepsix_excursion_event_info_t event_info[MAX_EVENTS] = {{0}};
	for (unsigned int i = 0; i < nevents; i++) {
		event_info[i].type = data[offset + 2 * i];
		event_info[i].size = data[offset + 2 * i + 1];

		switch (event_info[i].type) {
		case EVENT_ALARMS:
			if (event_info[i].size != 1) {
				ERROR(abstract->context, "Unexpected event size (%u).", event_info[i].size);
				return DC_STATUS_DATAFORMAT;
			}
			break;
		case EVENT_CHANGE_GAS:
			if (event_info[i].size != 3) {
				ERROR(abstract->context, "Unexpected event size (%u).", event_info[i].size);
				return DC_STATUS_DATAFORMAT;
			}
			break;
		case EVENT_SAMPLES_MISSED:
			if (event_info[i].size != 6) {
				ERROR(abstract->context, "Unexpected event size (%u).", event_info[i].size);
				return DC_STATUS_DATAFORMAT;
			}
			break;
		default:
			WARNING (abstract->context, "Unknown event descriptor (%u %u).",
				event_info[i].type, event_info[i].size);
			break;
		}
	}

	offset += 2 * nevents;

	unsigned int time = 0;
	unsigned int nsamples = 0;
	while (offset + 3 <= size) {
		dc_sample_value_t sample = {0};
		nsamples++;

		// Time (seconds).
		time += samplerate;
		sample.time = time;
		if (callback) callback (DC_SAMPLE_TIME, sample, userdata);

		unsigned int depth = array_uint16_le (data + offset);
		sample.depth = pressure_to_depth(depth, atmospheric, density);
		if (callback) callback (DC_SAMPLE_DEPTH, sample, userdata);
		offset += 2;

		// event info
		unsigned int length = data[offset];
		offset += 1;

		if (offset + length > size) {
			ERROR (abstract->context, "Buffer overflow detected!");
			return DC_STATUS_DATAFORMAT;
		}

		if (length) {
			if (length < 2) {
				ERROR (abstract->context, "Buffer overflow detected!");
				return DC_STATUS_DATAFORMAT;
			}

			unsigned int events = array_uint16_le (data + offset);
			unsigned int event_offset = 2;

			for (unsigned int i = 0; i < nevents; i++) {
				if ((events & (1 << event_info[i].type)) == 0)
					continue;

				if (event_offset + event_info[i].size > length) {
					ERROR (abstract->context, "Buffer overflow detected!");
					return DC_STATUS_DATAFORMAT;
				}

				unsigned int alarms = 0;
				unsigned int id = 0, o2 = 0, he = 0;
				unsigned int mix_idx = 0;
				unsigned int count = 0, timestamp = 0;
				switch (event_info[i].type) {
				case EVENT_ALARMS:
					alarms = data[offset + event_offset];
					for (unsigned int v = alarms, j = 0; v; v >>= 1, ++j) {
						if ((v & 1) == 0)
							continue;

						sample.event.type = SAMPLE_EVENT_NONE;
						sample.event.time = 0;
						sample.event.flags = 0;
						sample.event.value = 0;
						switch (j) {
						case ALARM_ASCENTRATE:
							sample.event.type = SAMPLE_EVENT_ASCENT;
							break;
						case ALARM_CEILING:
							sample.event.type = SAMPLE_EVENT_CEILING;
							break;
						case ALARM_PO2:
							sample.event.type = SAMPLE_EVENT_PO2;
							break;
						case ALARM_MAXDEPTH:
							sample.event.type = SAMPLE_EVENT_MAXDEPTH;
							break;
						case ALARM_DIVETIME:
							sample.event.type = SAMPLE_EVENT_DIVETIME;
							break;
						case ALARM_CNS:
							break;
						default:
							WARNING (abstract->context, "Unknown event (%u).", j);
							break;
						}
						if (sample.event.type != SAMPLE_EVENT_NONE) {
							if (callback) callback (DC_SAMPLE_EVENT, sample, userdata);
						}
					}
					break;
				case EVENT_CHANGE_GAS:
					id = data[offset + event_offset];
					o2 = data[offset + event_offset + 1];
					he = data[offset + event_offset + 2];

					mix_idx = deepsix_excursion_find_gasmix(parser, o2, he, id);
					if (mix_idx >= parser->ngasmixes) {
						if (mix_idx >= MAX_GASMIXES) {
							ERROR (abstract->context, "Maximum number of gas mixes reached.");
							return DC_STATUS_NOMEMORY;
						}
						parser->gasmix[mix_idx].oxygen = o2;
						parser->gasmix[mix_idx].helium = he;
						parser->gasmix[mix_idx].id = id;
						parser->ngasmixes = mix_idx + 1;
					}

					sample.gasmix = mix_idx;
					if (callback) callback(DC_SAMPLE_GASMIX, sample, userdata);
					break;
				case EVENT_SAMPLES_MISSED:
					count     = array_uint16_le(data + offset + event_offset);
					timestamp = array_uint32_le(data + offset + event_offset + 2);
					if (timestamp < time) {
						ERROR (abstract->context, "Timestamp moved backwards (%u %u).", timestamp, time);
						return DC_STATUS_DATAFORMAT;
					}
					nsamples += count;
					time = timestamp;
					break;
				default:
					WARNING (abstract->context, "Unknown event (%u %u).",
						event_info[i].type, event_info[i].size);
					break;
				}
				event_offset += event_info[i].size;
			}

			// Skip remaining sample bytes (if any).
			if (event_offset < length) {
				WARNING (abstract->context, "Remaining %u bytes skipped.", length - event_offset);
			}
			offset += length;
		}

		for (unsigned int i = 0; i < nconfig; ++i) {
			if (sample_info[i].divisor && (nsamples % sample_info[i].divisor) == 0) {
				if (offset + sample_info[i].size > size) {
					ERROR (abstract->context, "Buffer overflow detected!");
					return DC_STATUS_DATAFORMAT;
				}

				unsigned int value = 0;
				unsigned int deco_flags = 0, deco_ndl_tts = 0;
				unsigned int deco_depth = 0, deco_time = 0;
				switch (sample_info[i].type) {
				case SAMPLE_TEMPERATURE:
					value = array_uint16_le(data + offset);
					sample.temperature = value / 10.0;
					if (callback) callback(DC_SAMPLE_TEMPERATURE, sample, userdata);
					break;
				case SAMPLE_CNS:
					value = array_uint16_le(data + offset);
					sample.cns = value / 10000.0;
					if (callback) callback (DC_SAMPLE_CNS, sample, userdata);
					break;
				case SAMPLE_DECO_NDL:
					deco_flags   = data[offset];
					deco_ndl_tts = array_uint16_le(data + offset + 1);
					deco_depth   = array_uint16_le(data + offset + 3);
					deco_time    = array_uint16_le(data + offset + 5);
					if (deco_flags & DECOSTOP) {
						sample.deco.type = DC_DECO_DECOSTOP;
						sample.deco.depth = pressure_to_depth(deco_depth, atmospheric, density);
						sample.deco.time = deco_time;
					} else if (deco_flags & SAFETYSTOP) {
						sample.deco.type = DC_DECO_SAFETYSTOP;
						sample.deco.depth = pressure_to_depth(deco_depth, atmospheric, density);
						sample.deco.time = deco_time;
					} else {
						sample.deco.type = DC_DECO_NDL;
						sample.deco.depth = 0;
						sample.deco.time = deco_ndl_tts;
					}
					if (callback) callback (DC_SAMPLE_DECO, sample, userdata);
					break;
				default:
					break;
				}
				offset += sample_info[i].size;
			}
		}
	}

	parser->cached = 1;

	return DC_STATUS_SUCCESS;
}

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

	if (size < HEADERSIZE_MIN)
		return DC_STATUS_DATAFORMAT;

	unsigned int version = data[3];

	if (version == 0) {
		return deepsix_excursion_parser_samples_foreach_v0(abstract, callback, userdata);
	} else {
		return deepsix_excursion_parser_samples_foreach_v1(abstract, callback, userdata);
	}
}