subrascasse

Sub Rascasse: an iPhone dive log for Subsurface cloud storage, with a pure-Swift git client (GPL-2.0)
git clone https://git.schurger.org/subrascasse.git
Log | Files | Refs | README | LICENSE

uwatec_smart_parser.c (35889B)


      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
     24 
     25 #include <libdivecomputer/units.h>
     26 
     27 #include "uwatec_smart.h"
     28 #include "context-private.h"
     29 #include "parser-private.h"
     30 #include "array.h"
     31 
     32 #define ISINSTANCE(parser) dc_parser_isinstance((parser), &uwatec_smart_parser_vtable)
     33 
     34 #define NBITS 8
     35 
     36 #define SMARTPRO          0x10
     37 #define GALILEO           0x11
     38 #define ALADINTEC         0x12
     39 #define ALADINTEC2G       0x13
     40 #define SMARTCOM          0x14
     41 #define ALADIN2G          0x15
     42 #define ALADINSPORTMATRIX 0x17
     43 #define SMARTTEC          0x18
     44 #define GALILEOTRIMIX     0x19
     45 #define SMARTZ            0x1C
     46 #define MERIDIAN          0x20
     47 #define ALADINSQUARE      0x22
     48 #define CHROMIS           0x24
     49 #define ALADINA1          0x25
     50 #define MANTIS2           0x26
     51 #define ALADINA2          0x28
     52 #define G2TEK             0x31
     53 #define G2                0x32
     54 #define G3                0x34
     55 #define G2HUD             0x42
     56 #define LUNA2AI           0x50
     57 #define LUNA2             0x51
     58 
     59 #define UNSUPPORTED 0xFFFFFFFF
     60 
     61 #define NEVENTS   3
     62 #define NGASMIXES 10
     63 
     64 #define HEADER  1
     65 #define PROFILE 2
     66 
     67 #define FRESH 1000.0
     68 #define SALT  1025.0
     69 
     70 #define FREEDIVE  0x00000080
     71 #define GAUGE     0x00001000
     72 #define SALINITY  0x00100000
     73 
     74 #define EPOCH 946684800 // 2000-01-01 00:00:00 UTC
     75 
     76 typedef enum {
     77 	PRESSURE_DEPTH,
     78 	RBT,
     79 	TEMPERATURE,
     80 	PRESSURE,
     81 	DEPTH,
     82 	HEARTRATE,
     83 	BEARING,
     84 	ALARMS,
     85 	TIME,
     86 	APNEA,
     87 	MISC,
     88 } uwatec_smart_sample_t;
     89 
     90 typedef enum {
     91 	EV_WARNING,          /* Warning (yellow buzzer) */
     92 	EV_ALARM,            /* Alarm (red buzzer) */
     93 	EV_WORKLOAD,         /* Workload */
     94 	EV_WORKLOAD_WARNING, /* Increased workload (lung symbol) */
     95 	EV_BOOKMARK,         /* Bookmark / safety stop timer started */
     96 	EV_GASMIX,           /* Active gasmix */
     97 	EV_UNKNOWN,
     98 } uwatec_smart_event_t;
     99 
    100 typedef struct uwatec_smart_header_info_t {
    101 	unsigned int maxdepth;
    102 	unsigned int divetime;
    103 	unsigned int gasmix;
    104 	unsigned int ngases;
    105 	unsigned int temp_minimum;
    106 	unsigned int temp_maximum;
    107 	unsigned int temp_surface;
    108 	unsigned int tankpressure;
    109 	unsigned int timezone;
    110 	unsigned int settings;
    111 } uwatec_smart_header_info_t;
    112 
    113 typedef struct uwatec_smart_sample_info_t {
    114 	uwatec_smart_sample_t type;
    115 	unsigned int absolute;
    116 	unsigned int index;
    117 	unsigned int ntypebits;
    118 	unsigned int ignoretype;
    119 	unsigned int extrabytes;
    120 } uwatec_smart_sample_info_t;
    121 
    122 typedef struct uwatec_smart_event_info_t {
    123 	uwatec_smart_event_t type;
    124 	unsigned int mask;
    125 	unsigned int shift;
    126 } uwatec_smart_event_info_t;
    127 
    128 typedef struct uwatec_smart_gasmix_t {
    129 	unsigned int id;
    130 	unsigned int oxygen;
    131 	unsigned int helium;
    132 } uwatec_smart_gasmix_t;
    133 
    134 typedef struct uwatec_smart_tank_t {
    135 	unsigned int id;
    136 	unsigned int beginpressure;
    137 	unsigned int endpressure;
    138 	unsigned int gasmix;
    139 } uwatec_smart_tank_t;
    140 
    141 typedef struct uwatec_smart_parser_t uwatec_smart_parser_t;
    142 
    143 struct uwatec_smart_parser_t {
    144 	dc_parser_t base;
    145 	unsigned int model;
    146 	const uwatec_smart_sample_info_t *samples;
    147 	const uwatec_smart_header_info_t *header;
    148 	unsigned int headersize;
    149 	unsigned int nsamples;
    150 	const uwatec_smart_event_info_t *events[NEVENTS];
    151 	unsigned int nevents[NEVENTS];
    152 	unsigned int trimix;
    153 	// Cached fields.
    154 	unsigned int cached;
    155 	unsigned int ngasmixes;
    156 	uwatec_smart_gasmix_t gasmix[NGASMIXES];
    157 	unsigned int ntanks;
    158 	uwatec_smart_tank_t tank[NGASMIXES];
    159 	dc_water_t watertype;
    160 	dc_divemode_t divemode;
    161 };
    162 
    163 static dc_status_t uwatec_smart_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime);
    164 static dc_status_t uwatec_smart_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value);
    165 static dc_status_t uwatec_smart_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata);
    166 
    167 static dc_status_t uwatec_smart_parse (uwatec_smart_parser_t *parser, dc_sample_callback_t callback, void *userdata);
    168 
    169 static const dc_parser_vtable_t uwatec_smart_parser_vtable = {
    170 	sizeof(uwatec_smart_parser_t),
    171 	DC_FAMILY_UWATEC_SMART,
    172 	NULL, /* set_clock */
    173 	NULL, /* set_atmospheric */
    174 	NULL, /* set_density */
    175 	uwatec_smart_parser_get_datetime, /* datetime */
    176 	uwatec_smart_parser_get_field, /* fields */
    177 	uwatec_smart_parser_samples_foreach, /* samples_foreach */
    178 	NULL /* destroy */
    179 };
    180 
    181 static const
    182 uwatec_smart_header_info_t uwatec_smart_pro_header = {
    183 	18,
    184 	20,
    185 	24, 1,
    186 	22, /* temp_minimum */
    187 	UNSUPPORTED, /* temp_maximum */
    188 	UNSUPPORTED, /* temp_surface */
    189 	UNSUPPORTED, /* tankpressure */
    190 	UNSUPPORTED, /* timezone */
    191 	UNSUPPORTED, /* settings */
    192 };
    193 
    194 static const
    195 uwatec_smart_header_info_t uwatec_smart_galileo_header = {
    196 	22,
    197 	26,
    198 	44, 3,
    199 	30, /* temp_minimum */
    200 	28, /* temp_maximum */
    201 	32, /* temp_surface */
    202 	50, /* tankpressure */
    203 	16, /* timezone */
    204 	92, /* settings */
    205 };
    206 
    207 static const
    208 uwatec_smart_header_info_t uwatec_smart_trimix_header = {
    209 	22, /* maxdepth */
    210 	26, /* divetime */
    211 	UNSUPPORTED, 0, /* gasmixes */
    212 	30, /* temp_minimum */
    213 	28, /* temp_maximum */
    214 	32, /* temp_surface */
    215 	UNSUPPORTED, /* tankpressure */
    216 	16, /* timezone */
    217 	68, /* settings */
    218 };
    219 
    220 static const
    221 uwatec_smart_header_info_t uwatec_smart_aladin_tec_header = {
    222 	22,
    223 	24,
    224 	30, 1,
    225 	26, /* temp_minimum */
    226 	28, /* temp_maximum */
    227 	32, /* temp_surface */
    228 	UNSUPPORTED, /* tankpressure */
    229 	16, /* timezone */
    230 	52, /* settings */
    231 };
    232 
    233 static const
    234 uwatec_smart_header_info_t uwatec_smart_aladin_tec2g_header = {
    235 	22,
    236 	26,
    237 	34, 3,
    238 	30, /* temp_minimum */
    239 	28, /* temp_maximum */
    240 	32, /* temp_surface */
    241 	UNSUPPORTED, /* tankpressure */
    242 	16, /* timezone */
    243 	60, /* settings */
    244 };
    245 
    246 static const
    247 uwatec_smart_header_info_t uwatec_smart_com_header = {
    248 	18,
    249 	20,
    250 	24, 1,
    251 	22, /* temp_minimum */
    252 	UNSUPPORTED, /* temp_maximum */
    253 	UNSUPPORTED, /* temp_surface */
    254 	30, /* tankpressure */
    255 	UNSUPPORTED, /* timezone */
    256 	UNSUPPORTED, /* settings */
    257 };
    258 
    259 static const
    260 uwatec_smart_header_info_t uwatec_smart_tec_header = {
    261 	18,
    262 	20,
    263 	28, 3,
    264 	22, /* temp_minimum */
    265 	UNSUPPORTED, /* temp_maximum */
    266 	UNSUPPORTED, /* temp_surface */
    267 	34, /* tankpressure */
    268 	UNSUPPORTED, /* timezone */
    269 	UNSUPPORTED, /* settings */
    270 };
    271 
    272 static const
    273 uwatec_smart_sample_info_t uwatec_smart_pro_samples[] = {
    274 	{DEPTH,          0, 0, 1, 0, 0}, // 0ddddddd
    275 	{TEMPERATURE,    0, 0, 2, 0, 0}, // 10dddddd
    276 	{TIME,           1, 0, 3, 0, 0}, // 110ddddd
    277 	{ALARMS,         1, 0, 4, 0, 0}, // 1110dddd
    278 	{DEPTH,          0, 0, 5, 0, 1}, // 11110ddd dddddddd
    279 	{TEMPERATURE,    0, 0, 6, 0, 1}, // 111110dd dddddddd
    280 	{DEPTH,          1, 0, 7, 1, 2}, // 1111110d dddddddd dddddddd
    281 	{TEMPERATURE,    1, 0, 8, 0, 2}, // 11111110 dddddddd dddddddd
    282 };
    283 
    284 static const
    285 uwatec_smart_sample_info_t uwatec_smart_galileo_samples[] = {
    286 	{DEPTH,          0, 0, 1, 0, 0}, // 0ddd dddd
    287 	{RBT,            0, 0, 3, 0, 0}, // 100d dddd
    288 	{PRESSURE,       0, 0, 4, 0, 0}, // 1010 dddd
    289 	{TEMPERATURE,    0, 0, 4, 0, 0}, // 1011 dddd
    290 	{TIME,           1, 0, 4, 0, 0}, // 1100 dddd
    291 	{HEARTRATE,      0, 0, 4, 0, 0}, // 1101 dddd
    292 	{ALARMS,         1, 0, 4, 0, 0}, // 1110 dddd
    293 	{ALARMS,         1, 1, 8, 0, 1}, // 1111 0000 dddddddd
    294 	{DEPTH,          1, 0, 8, 0, 2}, // 1111 0001 dddddddd dddddddd
    295 	{RBT,            1, 0, 8, 0, 1}, // 1111 0010 dddddddd
    296 	{TEMPERATURE,    1, 0, 8, 0, 2}, // 1111 0011 dddddddd dddddddd
    297 	{PRESSURE,       1, 0, 8, 0, 2}, // 1111 0100 dddddddd dddddddd
    298 	{PRESSURE,       1, 1, 8, 0, 2}, // 1111 0101 dddddddd dddddddd
    299 	{PRESSURE,       1, 2, 8, 0, 2}, // 1111 0110 dddddddd dddddddd
    300 	{HEARTRATE,      1, 0, 8, 0, 1}, // 1111 0111 dddddddd
    301 	{BEARING,        1, 0, 8, 0, 2}, // 1111 1000 dddddddd dddddddd
    302 	{ALARMS,         1, 2, 8, 0, 1}, // 1111 1001 dddddddd
    303 	{APNEA,          1, 0, 8, 0, 0}, // 1111 1010 (8 bytes)
    304 	{MISC,           1, 0, 8, 0, 1}, // 1111 1011 dddddddd (n-1 bytes)
    305 };
    306 
    307 
    308 static const
    309 uwatec_smart_sample_info_t uwatec_smart_aladin_samples[] = {
    310 	{DEPTH,          0, 0, 1, 0, 0}, // 0ddddddd
    311 	{TEMPERATURE,    0, 0, 2, 0, 0}, // 10dddddd
    312 	{TIME,           1, 0, 3, 0, 0}, // 110ddddd
    313 	{ALARMS,         1, 0, 4, 0, 0}, // 1110dddd
    314 	{DEPTH,          0, 0, 5, 0, 1}, // 11110ddd dddddddd
    315 	{TEMPERATURE,    0, 0, 6, 0, 1}, // 111110dd dddddddd
    316 	{DEPTH,          1, 0, 7, 1, 2}, // 1111110d dddddddd dddddddd
    317 	{TEMPERATURE,    1, 0, 8, 0, 2}, // 11111110 dddddddd dddddddd
    318 	{ALARMS,         1, 1, 9, 0, 0}, // 11111111 0ddddddd
    319 };
    320 
    321 static const
    322 uwatec_smart_sample_info_t uwatec_smart_com_samples[] = {
    323 	{PRESSURE_DEPTH, 0, 0,  1, 0, 1}, // 0ddddddd dddddddd
    324 	{RBT,            0, 0,  2, 0, 0}, // 10dddddd
    325 	{TEMPERATURE,    0, 0,  3, 0, 0}, // 110ddddd
    326 	{PRESSURE,       0, 0,  4, 0, 1}, // 1110dddd dddddddd
    327 	{DEPTH,          0, 0,  5, 0, 1}, // 11110ddd dddddddd
    328 	{TEMPERATURE,    0, 0,  6, 0, 1}, // 111110dd dddddddd
    329 	{ALARMS,         1, 0,  7, 1, 1}, // 1111110d dddddddd
    330 	{TIME,           1, 0,  8, 0, 1}, // 11111110 dddddddd
    331 	{DEPTH,          1, 0,  9, 1, 2}, // 11111111 0ddddddd dddddddd dddddddd
    332 	{PRESSURE,       1, 0, 10, 1, 2}, // 11111111 10dddddd dddddddd dddddddd
    333 	{TEMPERATURE,    1, 0, 11, 1, 2}, // 11111111 110ddddd dddddddd dddddddd
    334 	{RBT,            1, 0, 12, 1, 1}, // 11111111 1110dddd dddddddd
    335 };
    336 
    337 static const
    338 uwatec_smart_sample_info_t uwatec_smart_tec_samples[] = {
    339 	{PRESSURE_DEPTH, 0, 0,  1, 0, 1}, // 0ddddddd dddddddd
    340 	{RBT,            0, 0,  2, 0, 0}, // 10dddddd
    341 	{TEMPERATURE,    0, 0,  3, 0, 0}, // 110ddddd
    342 	{PRESSURE,       0, 0,  4, 0, 1}, // 1110dddd dddddddd
    343 	{DEPTH,          0, 0,  5, 0, 1}, // 11110ddd dddddddd
    344 	{TEMPERATURE,    0, 0,  6, 0, 1}, // 111110dd dddddddd
    345 	{ALARMS,         1, 0,  7, 1, 1}, // 1111110d dddddddd
    346 	{TIME,           1, 0,  8, 0, 1}, // 11111110 dddddddd
    347 	{DEPTH,          1, 0,  9, 1, 2}, // 11111111 0ddddddd dddddddd dddddddd
    348 	{TEMPERATURE,    1, 0, 10, 1, 2}, // 11111111 10dddddd dddddddd dddddddd
    349 	{PRESSURE,       1, 0, 11, 1, 2}, // 11111111 110ddddd dddddddd dddddddd
    350 	{PRESSURE,       1, 1, 12, 1, 2}, // 11111111 1110dddd dddddddd dddddddd
    351 	{PRESSURE,       1, 2, 13, 1, 2}, // 11111111 11110ddd dddddddd dddddddd
    352 	{RBT,            1, 0, 14, 1, 1}, // 11111111 111110dd dddddddd
    353 };
    354 
    355 static const
    356 uwatec_smart_event_info_t uwatec_smart_tec_events_0[] = {
    357 	{EV_WARNING,          0x01, 0},
    358 	{EV_ALARM,            0x02, 1},
    359 	{EV_WORKLOAD_WARNING, 0x04, 2},
    360 	{EV_WORKLOAD,         0x38, 3},
    361 	{EV_UNKNOWN,          0xC0, 6},
    362 };
    363 
    364 static const
    365 uwatec_smart_event_info_t uwatec_smart_aladintec_events_0[] = {
    366 	{EV_WARNING,          0x01, 0},
    367 	{EV_ALARM,            0x02, 1},
    368 	{EV_BOOKMARK,         0x04, 2},
    369 	{EV_UNKNOWN,          0x08, 3},
    370 };
    371 
    372 static const
    373 uwatec_smart_event_info_t uwatec_smart_aladintec_events_1[] = {
    374 	{EV_UNKNOWN,          0xFF, 0},
    375 };
    376 
    377 static const
    378 uwatec_smart_event_info_t uwatec_smart_aladintec2g_events_0[] = {
    379 	{EV_WARNING,          0x01, 0},
    380 	{EV_ALARM,            0x02, 1},
    381 	{EV_BOOKMARK,         0x04, 2},
    382 	{EV_UNKNOWN,          0x08, 3},
    383 };
    384 
    385 static const
    386 uwatec_smart_event_info_t uwatec_smart_aladintec2g_events_1[] = {
    387 	{EV_UNKNOWN,          0x07, 0},
    388 	{EV_GASMIX,           0x18, 3},
    389 };
    390 
    391 static const
    392 uwatec_smart_event_info_t uwatec_smart_galileo_events_0[] = {
    393 	{EV_WARNING,          0x01, 0},
    394 	{EV_ALARM,            0x02, 1},
    395 	{EV_WORKLOAD_WARNING, 0x04, 2},
    396 	{EV_BOOKMARK,         0x08, 3},
    397 };
    398 
    399 static const
    400 uwatec_smart_event_info_t uwatec_smart_galileo_events_1[] = {
    401 	{EV_WORKLOAD,         0x07, 0},
    402 	{EV_UNKNOWN,          0x18, 3},
    403 	{EV_GASMIX,           0x60, 5},
    404 	{EV_UNKNOWN,          0x80, 7},
    405 };
    406 
    407 static const
    408 uwatec_smart_event_info_t uwatec_smart_galileo_events_2[] = {
    409 	{EV_UNKNOWN,          0xFF, 0},
    410 };
    411 
    412 static const
    413 uwatec_smart_event_info_t uwatec_smart_trimix_events_2[] = {
    414 	{EV_UNKNOWN,          0x0F, 0},
    415 	{EV_GASMIX,           0xF0, 4},
    416 };
    417 
    418 static unsigned int
    419 uwatec_smart_find_gasmix (uwatec_smart_parser_t *parser, unsigned int id)
    420 {
    421        unsigned int i = 0;
    422        while (i < parser->ngasmixes) {
    423                if (id == parser->gasmix[i].id)
    424                        break;
    425                i++;
    426        }
    427 
    428        return i;
    429 }
    430 
    431 static unsigned int
    432 uwatec_smart_find_tank (uwatec_smart_parser_t *parser, unsigned int id)
    433 {
    434        unsigned int i = 0;
    435        while (i < parser->ntanks) {
    436                if (id == parser->tank[i].id)
    437                        break;
    438                i++;
    439        }
    440 
    441        return i;
    442 }
    443 
    444 static dc_status_t
    445 uwatec_smart_parser_cache (uwatec_smart_parser_t *parser)
    446 {
    447 	const unsigned char *data = parser->base.data;
    448 	unsigned int size = parser->base.size;
    449 
    450 	if (parser->cached) {
    451 		return DC_STATUS_SUCCESS;
    452 	}
    453 
    454 	if (parser->model == GALILEO || parser->model == GALILEOTRIMIX) {
    455 		if (size < 44)
    456 			return DC_STATUS_DATAFORMAT;
    457 
    458 		if (data[43] & 0x80) {
    459 			parser->trimix = 1;
    460 			parser->headersize = 84;
    461 			parser->header = &uwatec_smart_trimix_header;
    462 			parser->events[2] = uwatec_smart_trimix_events_2;
    463 			parser->nevents[2] = C_ARRAY_SIZE (uwatec_smart_trimix_events_2);
    464 		} else {
    465 			parser->trimix = 0;
    466 			parser->headersize = 152;
    467 			parser->header = &uwatec_smart_galileo_header;
    468 			parser->events[2] = uwatec_smart_galileo_events_2;
    469 			parser->nevents[2] = C_ARRAY_SIZE (uwatec_smart_galileo_events_2);
    470 		}
    471 	}
    472 
    473 	const uwatec_smart_header_info_t *header = parser->header;
    474 
    475 	// Get the settings.
    476 	dc_divemode_t divemode = DC_DIVEMODE_OC;
    477 	dc_water_t watertype = DC_WATER_FRESH;
    478 	if (header->settings != UNSUPPORTED) {
    479 		unsigned int settings = array_uint32_le (data + header->settings);
    480 
    481 		// Get the freedive/gauge bits.
    482 		unsigned int freedive = 0;
    483 		unsigned int gauge = (settings & GAUGE) != 0;
    484 		if (parser->model != ALADINTEC && parser->model != ALADINTEC2G) {
    485 			freedive = (settings & FREEDIVE) != 0;
    486 		}
    487 
    488 		// Get the dive mode. The freedive bit needs to be checked
    489 		// first, because freedives have both the freedive and gauge
    490 		// bits set.
    491 		if (freedive) {
    492 			divemode = DC_DIVEMODE_FREEDIVE;
    493 		} else if (gauge) {
    494 			divemode = DC_DIVEMODE_GAUGE;
    495 		} else {
    496 			divemode = DC_DIVEMODE_OC;
    497 		}
    498 
    499 		// Get the water type.
    500 		if (settings & SALINITY) {
    501 			watertype = DC_WATER_SALT;
    502 		}
    503 	}
    504 
    505 	// Get the gas mixes and tanks.
    506 	unsigned int ntanks = 0;
    507 	unsigned int ngasmixes = 0;
    508 	uwatec_smart_tank_t tank[NGASMIXES] = {{0}};
    509 	uwatec_smart_gasmix_t gasmix[NGASMIXES] = {{0}};
    510 	if (header->gasmix != UNSUPPORTED) {
    511 		for (unsigned int i = 0; i < header->ngases; ++i) {
    512 			unsigned int idx = DC_GASMIX_UNKNOWN;
    513 			unsigned int o2 = 0;
    514 			if (parser->model == ALADINTEC2G) {
    515 				o2 = data[header->gasmix + i];
    516 			} else {
    517 				o2 = array_uint16_le (data + header->gasmix + i * 2);
    518 			}
    519 
    520 			if (o2 != 0) {
    521 				idx = ngasmixes;
    522 				gasmix[ngasmixes].id = i;
    523 				gasmix[ngasmixes].oxygen = o2;
    524 				gasmix[ngasmixes].helium = 0;
    525 				ngasmixes++;
    526 			}
    527 
    528 			unsigned int beginpressure = 0;
    529 			unsigned int endpressure = 0;
    530 			if (header->tankpressure != UNSUPPORTED &&
    531 				divemode != DC_DIVEMODE_FREEDIVE) {
    532 				if (parser->model == GALILEO || parser->model == GALILEOTRIMIX ||
    533 					parser->model == ALADIN2G || parser->model == MERIDIAN ||
    534 					parser->model == CHROMIS || parser->model == MANTIS2 ||
    535 					parser->model == G2 || parser->model == ALADINSPORTMATRIX ||
    536 					parser->model == ALADINSQUARE || parser->model == G2HUD ||
    537 					parser->model == ALADINA1 || parser->model == ALADINA2 ||
    538 					parser->model == G2TEK || parser->model == G3 ||
    539 					parser->model == LUNA2AI || parser->model == LUNA2) {
    540 					unsigned int offset = header->tankpressure + 2 * i;
    541 					endpressure   = array_uint16_le(data + offset);
    542 					beginpressure = array_uint16_le(data + offset + 2 * header->ngases);
    543 				} else {
    544 					unsigned int offset = header->tankpressure + 4 * i;
    545 					beginpressure = array_uint16_le(data + offset);
    546 					endpressure   = array_uint16_le(data + offset + 2);
    547 				}
    548 			}
    549 			if ((beginpressure != 0 || endpressure != 0) &&
    550 				(beginpressure != 0xFFFF) && (endpressure != 0xFFFF)) {
    551 				tank[ntanks].id = i;
    552 				tank[ntanks].beginpressure = beginpressure;
    553 				tank[ntanks].endpressure = endpressure;
    554 				tank[ntanks].gasmix = idx;
    555 				ntanks++;
    556 			}
    557 		}
    558 	}
    559 
    560 	// Cache the data for later use.
    561 	parser->ngasmixes = ngasmixes;
    562 	for (unsigned int i = 0; i < ngasmixes; ++i) {
    563 		parser->gasmix[i] = gasmix[i];
    564 	}
    565 	parser->ntanks = ntanks;
    566 	for (unsigned int i = 0; i < ntanks; ++i) {
    567 		parser->tank[i] = tank[i];
    568 	}
    569 	parser->watertype = watertype;
    570 	parser->divemode = divemode;
    571 	parser->cached = HEADER;
    572 
    573 	return DC_STATUS_SUCCESS;
    574 }
    575 
    576 
    577 dc_status_t
    578 uwatec_smart_parser_create (dc_parser_t **out, dc_context_t *context, const unsigned char data[], size_t size, unsigned int model)
    579 {
    580 	dc_status_t status = DC_STATUS_SUCCESS;
    581 	uwatec_smart_parser_t *parser = NULL;
    582 
    583 	if (out == NULL)
    584 		return DC_STATUS_INVALIDARGS;
    585 
    586 	// Allocate memory.
    587 	parser = (uwatec_smart_parser_t *) dc_parser_allocate (context, &uwatec_smart_parser_vtable, data, size);
    588 	if (parser == NULL) {
    589 		ERROR (context, "Failed to allocate memory.");
    590 		return DC_STATUS_NOMEMORY;
    591 	}
    592 
    593 	// Set the default values.
    594 	parser->model = model;
    595 	parser->trimix = 0;
    596 	for (unsigned int i = 0; i < NEVENTS; ++i) {
    597 		parser->events[i] = NULL;
    598 		parser->nevents[i] = 0;
    599 	}
    600 	switch (model) {
    601 	case SMARTPRO:
    602 		parser->headersize = 92;
    603 		parser->header = &uwatec_smart_pro_header;
    604 		parser->samples = uwatec_smart_pro_samples;
    605 		parser->nsamples = C_ARRAY_SIZE (uwatec_smart_pro_samples);
    606 		parser->events[0] = uwatec_smart_tec_events_0;
    607 		parser->nevents[0] = C_ARRAY_SIZE (uwatec_smart_tec_events_0);
    608 		break;
    609 	case GALILEO:
    610 	case GALILEOTRIMIX:
    611 	case ALADIN2G:
    612 	case MERIDIAN:
    613 	case CHROMIS:
    614 	case MANTIS2:
    615 	case ALADINSQUARE:
    616 		parser->headersize = 152;
    617 		parser->header = &uwatec_smart_galileo_header;
    618 		parser->samples = uwatec_smart_galileo_samples;
    619 		parser->nsamples = C_ARRAY_SIZE (uwatec_smart_galileo_samples);
    620 		parser->events[0] = uwatec_smart_galileo_events_0;
    621 		parser->events[1] = uwatec_smart_galileo_events_1;
    622 		parser->events[2] = uwatec_smart_galileo_events_2;
    623 		parser->nevents[0] = C_ARRAY_SIZE (uwatec_smart_galileo_events_0);
    624 		parser->nevents[1] = C_ARRAY_SIZE (uwatec_smart_galileo_events_1);
    625 		parser->nevents[2] = C_ARRAY_SIZE (uwatec_smart_galileo_events_2);
    626 		break;
    627 	case G2:
    628 	case G2HUD:
    629 	case G2TEK:
    630 	case G3:
    631 	case ALADINSPORTMATRIX:
    632 	case ALADINA1:
    633 	case ALADINA2:
    634 	case LUNA2AI:
    635 	case LUNA2:
    636 		parser->headersize = 84;
    637 		parser->header = &uwatec_smart_trimix_header;
    638 		parser->samples = uwatec_smart_galileo_samples;
    639 		parser->nsamples = C_ARRAY_SIZE (uwatec_smart_galileo_samples);
    640 		parser->events[0] = uwatec_smart_galileo_events_0;
    641 		parser->events[1] = uwatec_smart_galileo_events_1;
    642 		parser->events[2] = uwatec_smart_trimix_events_2;
    643 		parser->nevents[0] = C_ARRAY_SIZE (uwatec_smart_galileo_events_0);
    644 		parser->nevents[1] = C_ARRAY_SIZE (uwatec_smart_galileo_events_1);
    645 		parser->nevents[2] = C_ARRAY_SIZE (uwatec_smart_trimix_events_2);
    646 		parser->trimix = 1;
    647 		break;
    648 	case ALADINTEC:
    649 		parser->headersize = 108;
    650 		parser->header = &uwatec_smart_aladin_tec_header;
    651 		parser->samples = uwatec_smart_aladin_samples;
    652 		parser->nsamples = C_ARRAY_SIZE (uwatec_smart_aladin_samples);
    653 		parser->events[0] = uwatec_smart_aladintec_events_0;
    654 		parser->events[1] = uwatec_smart_aladintec_events_1;
    655 		parser->nevents[0] = C_ARRAY_SIZE (uwatec_smart_aladintec_events_0);
    656 		parser->nevents[1] = C_ARRAY_SIZE (uwatec_smart_aladintec_events_1);
    657 		break;
    658 	case ALADINTEC2G:
    659 		parser->headersize = 116;
    660 		parser->header = &uwatec_smart_aladin_tec2g_header;
    661 		parser->samples = uwatec_smart_aladin_samples;
    662 		parser->nsamples = C_ARRAY_SIZE (uwatec_smart_aladin_samples);
    663 		parser->events[0] = uwatec_smart_aladintec2g_events_0;
    664 		parser->events[1] = uwatec_smart_aladintec2g_events_1;
    665 		parser->nevents[0] = C_ARRAY_SIZE (uwatec_smart_aladintec2g_events_0);
    666 		parser->nevents[1] = C_ARRAY_SIZE (uwatec_smart_aladintec2g_events_1);
    667 		break;
    668 	case SMARTCOM:
    669 		parser->headersize = 100;
    670 		parser->header = &uwatec_smart_com_header;
    671 		parser->samples = uwatec_smart_com_samples;
    672 		parser->nsamples = C_ARRAY_SIZE (uwatec_smart_com_samples);
    673 		parser->events[0] = uwatec_smart_tec_events_0;
    674 		parser->nevents[0] = C_ARRAY_SIZE (uwatec_smart_tec_events_0);
    675 		break;
    676 	case SMARTTEC:
    677 	case SMARTZ:
    678 		parser->headersize = 132;
    679 		parser->header = &uwatec_smart_tec_header;
    680 		parser->samples = uwatec_smart_tec_samples;
    681 		parser->nsamples = C_ARRAY_SIZE (uwatec_smart_tec_samples);
    682 		parser->events[0] = uwatec_smart_tec_events_0;
    683 		parser->nevents[0] = C_ARRAY_SIZE (uwatec_smart_tec_events_0);
    684 		break;
    685 	default:
    686 		status = DC_STATUS_INVALIDARGS;
    687 		goto error_free;
    688 	}
    689 
    690 	parser->cached = 0;
    691 	parser->ngasmixes = 0;
    692 	parser->ntanks = 0;
    693 	for (unsigned int i = 0; i < NGASMIXES; ++i) {
    694 		parser->gasmix[i].id = 0;
    695 		parser->gasmix[i].oxygen = 0;
    696 		parser->gasmix[i].helium = 0;
    697 		parser->tank[i].id = 0;
    698 		parser->tank[i].beginpressure = 0;
    699 		parser->tank[i].endpressure = 0;
    700 		parser->tank[i].gasmix = 0;
    701 	}
    702 	parser->watertype = DC_WATER_FRESH;
    703 	parser->divemode = DC_DIVEMODE_OC;
    704 
    705 	*out = (dc_parser_t*) parser;
    706 
    707 	return DC_STATUS_SUCCESS;
    708 
    709 error_free:
    710 	dc_parser_deallocate ((dc_parser_t *) parser);
    711 	return status;
    712 }
    713 
    714 
    715 static dc_status_t
    716 uwatec_smart_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime)
    717 {
    718 	uwatec_smart_parser_t *parser = (uwatec_smart_parser_t *) abstract;
    719 	const uwatec_smart_header_info_t *table = parser->header;
    720 	const unsigned char *data = abstract->data;
    721 
    722 	if (abstract->size < parser->headersize)
    723 		return DC_STATUS_DATAFORMAT;
    724 
    725 	unsigned int timestamp = array_uint32_le (abstract->data + 8);
    726 
    727 	dc_ticks_t ticks = EPOCH + timestamp / 2;
    728 
    729 	if (table->timezone != UNSUPPORTED) {
    730 		// For devices with timezone support, the UTC offset of the
    731 		// device is used. The UTC offset is stored in units of 15
    732 		// minutes (or 900 seconds).
    733 		int utc_offset = (signed char) data[table->timezone];
    734 
    735 		ticks += utc_offset * 900;
    736 
    737 		if (!dc_datetime_gmtime (datetime, ticks))
    738 			return DC_STATUS_DATAFORMAT;
    739 
    740 		datetime->timezone = utc_offset * 900;
    741 	} else {
    742 		// For devices without timezone support, the current timezone of
    743 		// the host system is used.
    744 		if (!dc_datetime_localtime (datetime, ticks))
    745 			return DC_STATUS_DATAFORMAT;
    746 	}
    747 
    748 	return DC_STATUS_SUCCESS;
    749 }
    750 
    751 
    752 static dc_status_t
    753 uwatec_smart_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value)
    754 {
    755 	uwatec_smart_parser_t *parser = (uwatec_smart_parser_t *) abstract;
    756 
    757 	// Cache the parser data.
    758 	dc_status_t rc = uwatec_smart_parser_cache (parser);
    759 	if (rc != DC_STATUS_SUCCESS)
    760 		return rc;
    761 
    762 	// Cache the profile data.
    763 	if (parser->cached < PROFILE) {
    764 		rc = uwatec_smart_parse (parser, NULL, NULL);
    765 		if (rc != DC_STATUS_SUCCESS)
    766 			return rc;
    767 	}
    768 
    769 	const uwatec_smart_header_info_t *table = parser->header;
    770 	const unsigned char *data = abstract->data;
    771 
    772 	double density = (parser->watertype == DC_WATER_SALT ? SALT : FRESH);
    773 
    774 	dc_gasmix_t *gasmix = (dc_gasmix_t *) value;
    775 	dc_tank_t *tank = (dc_tank_t *) value;
    776 	dc_salinity_t *water = (dc_salinity_t *) value;
    777 
    778 	if (value) {
    779 		switch (type) {
    780 		case DC_FIELD_DIVETIME:
    781 			*((unsigned int *) value) = array_uint16_le (data + table->divetime) * 60;
    782 			break;
    783 		case DC_FIELD_MAXDEPTH:
    784 			*((double *) value) = array_uint16_le (data + table->maxdepth) * (BAR / 1000.0) / (density * 10.0);
    785 			break;
    786 		case DC_FIELD_GASMIX_COUNT:
    787 			*((unsigned int *) value) = parser->ngasmixes;
    788 			break;
    789 		case DC_FIELD_GASMIX:
    790 			gasmix->usage = DC_USAGE_NONE;
    791 			gasmix->helium = parser->gasmix[flags].helium / 100.0;
    792 			gasmix->oxygen = parser->gasmix[flags].oxygen / 100.0;
    793 			gasmix->nitrogen = 1.0 - gasmix->oxygen - gasmix->helium;
    794 			break;
    795 		case DC_FIELD_TANK_COUNT:
    796 			*((unsigned int *) value) = parser->ntanks;
    797 			break;
    798 		case DC_FIELD_TANK:
    799 			tank->type = DC_TANKVOLUME_NONE;
    800 			tank->volume = 0.0;
    801 			tank->workpressure = 0.0;
    802 			tank->beginpressure = parser->tank[flags].beginpressure / 128.0;
    803 			tank->endpressure   = parser->tank[flags].endpressure   / 128.0;
    804 			tank->gasmix = parser->tank[flags].gasmix;
    805 			tank->usage = DC_TANK_USAGE_NONE;
    806 			break;
    807 		case DC_FIELD_TEMPERATURE_MINIMUM:
    808 			*((double *) value) = (signed short) array_uint16_le (data + table->temp_minimum) / 10.0;
    809 			break;
    810 		case DC_FIELD_TEMPERATURE_MAXIMUM:
    811 			if (table->temp_maximum == UNSUPPORTED)
    812 				return DC_STATUS_UNSUPPORTED;
    813 			*((double *) value) = (signed short) array_uint16_le (data + table->temp_maximum) / 10.0;
    814 			break;
    815 		case DC_FIELD_TEMPERATURE_SURFACE:
    816 			if (table->temp_surface == UNSUPPORTED)
    817 				return DC_STATUS_UNSUPPORTED;
    818 			*((double *) value) = (signed short) array_uint16_le (data + table->temp_surface) / 10.0;
    819 			break;
    820 		case DC_FIELD_DIVEMODE:
    821 			if (table->settings == UNSUPPORTED)
    822 				return DC_STATUS_UNSUPPORTED;
    823 			*((dc_divemode_t *) value) = parser->divemode;
    824 			break;
    825 		case DC_FIELD_SALINITY:
    826 			if (table->settings == UNSUPPORTED)
    827 				return DC_STATUS_UNSUPPORTED;
    828 			water->type = parser->watertype;
    829 			water->density = density;
    830 			break;
    831 		default:
    832 			return DC_STATUS_UNSUPPORTED;
    833 		}
    834 	}
    835 
    836 	return DC_STATUS_SUCCESS;
    837 }
    838 
    839 
    840 static unsigned int
    841 uwatec_smart_identify (const unsigned char data[], unsigned int size)
    842 {
    843 	unsigned int count = 0;
    844 	for (unsigned int i = 0; i < size; ++i) {
    845 		unsigned char value = data[i];
    846 		for (unsigned int j = 0; j < NBITS; ++j) {
    847 			unsigned char mask = 1 << (NBITS - 1 - j);
    848 			if ((value & mask) == 0)
    849 				return count;
    850 			count++;
    851 		}
    852 	}
    853 
    854 	return (unsigned int) -1;
    855 }
    856 
    857 
    858 static unsigned int
    859 uwatec_galileo_identify (unsigned char value)
    860 {
    861 	// Bits: 0ddd dddd
    862 	if ((value & 0x80) == 0)
    863 		return 0;
    864 
    865 	// Bits: 100d dddd
    866 	if ((value & 0xE0) == 0x80)
    867 		return 1;
    868 
    869 	// Bits: 1XXX dddd
    870 	if ((value & 0xF0) != 0xF0)
    871 		return (value & 0x70) >> 4;
    872 
    873 	// Bits: 1111 XXXX
    874 	return (value & 0x0F) + 7;
    875 }
    876 
    877 
    878 static dc_status_t
    879 uwatec_smart_parse (uwatec_smart_parser_t *parser, dc_sample_callback_t callback, void *userdata)
    880 {
    881 	dc_parser_t *abstract = (dc_parser_t *) parser;
    882 
    883 	const unsigned char *data = abstract->data;
    884 	unsigned int size = abstract->size;
    885 
    886 	const uwatec_smart_sample_info_t *table = parser->samples;
    887 	unsigned int entries = parser->nsamples;
    888 
    889 	// Get the maximum number of alarm bytes.
    890 	unsigned int nalarms = 0;
    891 	for (unsigned int i = 0; i < entries; ++i) {
    892 		if (table[i].type == ALARMS &&
    893 			table[i].index >= nalarms)
    894 		{
    895 			nalarms = table[i].index + 1;
    896 		}
    897 	}
    898 
    899 	int complete = 0;
    900 	int calibrated = 0;
    901 
    902 	unsigned int time = 0;
    903 	unsigned int rbt = 99;
    904 	unsigned int tank = 0;
    905 	unsigned int gasmix = 0;
    906 	unsigned int depth = 0, depth_calibration = 0;
    907 	int temperature = 0;
    908 	unsigned int pressure = 0;
    909 	unsigned int heartrate = 0;
    910 	unsigned int bearing = 0;
    911 	unsigned int bookmark = 0;
    912 
    913 	// Previous gas mix - initialize with impossible value
    914 	unsigned int gasmix_previous = 0xFFFFFFFF;
    915 
    916 	double density = (parser->watertype == DC_WATER_SALT ? SALT : FRESH);
    917 
    918 	unsigned int interval = 4;
    919 	if (parser->divemode == DC_DIVEMODE_FREEDIVE) {
    920 		interval = 1;
    921 	}
    922 
    923 	int have_depth = 0, have_temperature = 0, have_pressure = 0, have_rbt = 0,
    924 		have_heartrate = 0, have_bearing = 0;
    925 
    926 	unsigned int offset = parser->headersize;
    927 	while (offset < size) {
    928 		dc_sample_value_t sample = {0};
    929 
    930 		// Process the type bits in the bitstream.
    931 		unsigned int id = 0;
    932 		if (parser->model == GALILEO || parser->model == GALILEOTRIMIX ||
    933 			parser->model == ALADIN2G || parser->model == MERIDIAN ||
    934 			parser->model == CHROMIS || parser->model == MANTIS2 ||
    935 			parser->model == G2 || parser->model == ALADINSPORTMATRIX ||
    936 			parser->model == ALADINSQUARE || parser->model == G2HUD ||
    937 			parser->model == ALADINA1 || parser->model == ALADINA2 ||
    938 			parser->model == G2TEK || parser->model == G3 ||
    939 			parser->model == LUNA2AI || parser->model == LUNA2) {
    940 			// Uwatec Galileo
    941 			id = uwatec_galileo_identify (data[offset]);
    942 		} else {
    943 			// Uwatec Smart
    944 			id = uwatec_smart_identify (data + offset, size - offset);
    945 		}
    946 		if (id >= entries) {
    947 			ERROR (abstract->context, "Invalid type bits.");
    948 			return DC_STATUS_DATAFORMAT;
    949 		}
    950 
    951 		// Skip the processed type bytes.
    952 		offset += table[id].ntypebits / NBITS;
    953 
    954 		// Process the remaining data bits.
    955 		unsigned int nbits = 0;
    956 		unsigned int value = 0;
    957 		unsigned int n = table[id].ntypebits % NBITS;
    958 		if (n > 0) {
    959 			nbits = NBITS - n;
    960 			value = data[offset] & (0xFF >> n);
    961 			if (table[id].ignoretype) {
    962 				// Ignore any data bits that are stored in
    963 				// the last type byte for certain samples.
    964 				nbits = 0;
    965 				value = 0;
    966 			}
    967 			offset++;
    968 		}
    969 
    970 		// Check for buffer overflows.
    971 		if (offset + table[id].extrabytes > size) {
    972 			ERROR (abstract->context, "Incomplete sample data.");
    973 			return DC_STATUS_DATAFORMAT;
    974 		}
    975 
    976 		// Process the extra data bytes.
    977 		for (unsigned int i = 0; i < table[id].extrabytes; ++i) {
    978 			nbits += NBITS;
    979 			value <<= NBITS;
    980 			value += data[offset];
    981 			offset++;
    982 		}
    983 
    984 		// Fix the sign bit.
    985 		signed int svalue = signextend (value, nbits);
    986 
    987 		// Parse the value.
    988 		unsigned int idx = 0;
    989 		unsigned int subtype = 0;
    990 		unsigned int nevents = 0;
    991 		const uwatec_smart_event_info_t *events = NULL;
    992 		switch (table[id].type) {
    993 		case PRESSURE_DEPTH:
    994 			pressure += (signed char) ((svalue >> NBITS) & 0xFF);
    995 			depth += (signed char) (svalue & 0xFF);
    996 			complete = 1;
    997 			break;
    998 		case RBT:
    999 			if (table[id].absolute) {
   1000 				rbt = value;
   1001 				have_rbt = 1;
   1002 			} else {
   1003 				rbt += svalue;
   1004 			}
   1005 			break;
   1006 		case TEMPERATURE:
   1007 			if (table[id].absolute) {
   1008 				temperature = svalue;
   1009 				have_temperature = 1;
   1010 			} else {
   1011 				temperature += svalue;
   1012 			}
   1013 			break;
   1014 		case PRESSURE:
   1015 			if (table[id].absolute) {
   1016 				if (parser->trimix) {
   1017 					tank = (value & 0xF000) >> 12;
   1018 					pressure = (value & 0x0FFF);
   1019 				} else {
   1020 					tank = table[id].index;
   1021 					pressure = value;
   1022 				}
   1023 				have_pressure = 1;
   1024 				gasmix = tank;
   1025 			} else {
   1026 				pressure += svalue;
   1027 			}
   1028 			break;
   1029 		case DEPTH:
   1030 			if (table[id].absolute) {
   1031 				depth = value;
   1032 				if (!calibrated) {
   1033 					calibrated = 1;
   1034 					depth_calibration = depth;
   1035 				}
   1036 				have_depth = 1;
   1037 			} else {
   1038 				depth += svalue;
   1039 			}
   1040 			complete = 1;
   1041 			break;
   1042 		case HEARTRATE:
   1043 			if (table[id].absolute) {
   1044 				heartrate = value;
   1045 				have_heartrate = 1;
   1046 			} else {
   1047 				heartrate += svalue;
   1048 			}
   1049 			break;
   1050 		case BEARING:
   1051 			bearing = value;
   1052 			have_bearing = 1;
   1053 			break;
   1054 		case ALARMS:
   1055 			idx = table[id].index;
   1056 			if (idx >= NEVENTS || parser->events[idx] == NULL) {
   1057 				ERROR (abstract->context, "Unexpected event index.");
   1058 				return DC_STATUS_DATAFORMAT;
   1059 			}
   1060 
   1061 			events = parser->events[idx];
   1062 			nevents = parser->nevents[idx];
   1063 
   1064 			for (unsigned int i = 0; i < nevents; ++i) {
   1065 				uwatec_smart_event_t ev_type = events[i].type;
   1066 				unsigned int ev_value = (value & events[i].mask) >> events[i].shift;
   1067 				switch (ev_type) {
   1068 				case EV_BOOKMARK:
   1069 					bookmark = ev_value;
   1070 					break;
   1071 				case EV_GASMIX:
   1072 					gasmix = ev_value;
   1073 					break;
   1074 				default:
   1075 					break;
   1076 				}
   1077 			}
   1078 			break;
   1079 		case TIME:
   1080 			complete = value;
   1081 			break;
   1082 		case APNEA:
   1083 			if (offset + 8 > size) {
   1084 				ERROR (abstract->context, "Incomplete sample data.");
   1085 				return DC_STATUS_DATAFORMAT;
   1086 			}
   1087 			offset += 8;
   1088 			break;
   1089 		case MISC:
   1090 			if (value < 1 || offset + value - 1 > size) {
   1091 				ERROR (abstract->context, "Incomplete sample data.");
   1092 				return DC_STATUS_DATAFORMAT;
   1093 			}
   1094 
   1095 			subtype = data[offset];
   1096 			if (subtype >= 32 && subtype <= 41) {
   1097 				if (value < 16) {
   1098 					ERROR (abstract->context, "Incomplete sample data.");
   1099 					return DC_STATUS_DATAFORMAT;
   1100 				}
   1101 				unsigned int mixid = subtype - 32;
   1102 				unsigned int mixidx = DC_GASMIX_UNKNOWN;
   1103 				unsigned int o2 = array_uint16_le (data + offset + 1);
   1104 				unsigned int he = array_uint16_le (data + offset + 3);
   1105 				unsigned int beginpressure = array_uint16_le (data + offset + 5);
   1106 				unsigned int endpressure   = array_uint16_le (data + offset + 7);
   1107 
   1108 				if (o2 != 0 || he != 0) {
   1109 					idx = uwatec_smart_find_gasmix (parser, mixid);
   1110 					if (idx >= parser->ngasmixes) {
   1111 						if (idx >= NGASMIXES) {
   1112 							ERROR (abstract->context, "Maximum number of gas mixes reached.");
   1113 							return DC_STATUS_NOMEMORY;
   1114 						}
   1115 						parser->gasmix[idx].id = mixid;
   1116 						parser->gasmix[idx].oxygen = o2;
   1117 						parser->gasmix[idx].helium = he;
   1118 						parser->ngasmixes++;
   1119 					}
   1120 					mixidx = idx;
   1121 				}
   1122 
   1123 				if ((beginpressure != 0 || endpressure != 0) &&
   1124 					(beginpressure != 0xFFFF) && (endpressure != 0xFFFF)) {
   1125 					idx = uwatec_smart_find_tank (parser, mixid);
   1126 					if (idx >= parser->ntanks) {
   1127 						if (idx >= NGASMIXES) {
   1128 							ERROR (abstract->context, "Maximum number of tanks reached.");
   1129 							return DC_STATUS_NOMEMORY;
   1130 						}
   1131 						parser->tank[idx].id = mixid;
   1132 						parser->tank[idx].beginpressure = beginpressure;
   1133 						parser->tank[idx].endpressure = endpressure;
   1134 						parser->tank[idx].gasmix = mixidx;
   1135 						parser->ntanks++;
   1136 					}
   1137 				}
   1138 			}
   1139 
   1140 			offset += value - 1;
   1141 			break;
   1142 		default:
   1143 			WARNING (abstract->context, "Unknown sample type.");
   1144 			break;
   1145 		}
   1146 
   1147 		while (complete) {
   1148 			sample.time = time * 1000;
   1149 			if (callback) callback (DC_SAMPLE_TIME, &sample, userdata);
   1150 
   1151 			if (parser->ngasmixes && gasmix != gasmix_previous) {
   1152 				idx = uwatec_smart_find_gasmix (parser, gasmix);
   1153 				if (idx >= parser->ngasmixes) {
   1154 					ERROR (abstract->context, "Invalid gas mix index.");
   1155 					return DC_STATUS_DATAFORMAT;
   1156 				}
   1157 				sample.gasmix = idx;
   1158 				if (callback) callback (DC_SAMPLE_GASMIX, &sample, userdata);
   1159 				gasmix_previous = gasmix;
   1160 			}
   1161 
   1162 			if (have_temperature) {
   1163 				sample.temperature = temperature / 2.5;
   1164 				if (callback) callback (DC_SAMPLE_TEMPERATURE, &sample, userdata);
   1165 			}
   1166 
   1167 			if (bookmark) {
   1168 				sample.event.type = SAMPLE_EVENT_BOOKMARK;
   1169 				sample.event.time = 0;
   1170 				sample.event.flags = 0;
   1171 				sample.event.value = 0;
   1172 				if (callback) callback (DC_SAMPLE_EVENT, &sample, userdata);
   1173 			}
   1174 
   1175 			if (have_rbt || have_pressure) {
   1176 				sample.rbt = rbt;
   1177 				if (callback) callback (DC_SAMPLE_RBT, &sample, userdata);
   1178 			}
   1179 
   1180 			if (have_pressure) {
   1181 				idx = uwatec_smart_find_tank(parser, tank);
   1182 				if (idx < parser->ntanks) {
   1183 					sample.pressure.tank = idx;
   1184 					sample.pressure.value = pressure / 4.0;
   1185 					if (callback) callback (DC_SAMPLE_PRESSURE, &sample, userdata);
   1186 				}
   1187 			}
   1188 
   1189 			if (have_heartrate) {
   1190 				sample.heartbeat = heartrate;
   1191 				if (callback) callback (DC_SAMPLE_HEARTBEAT, &sample, userdata);
   1192 			}
   1193 
   1194 			if (have_bearing) {
   1195 				sample.event.type = SAMPLE_EVENT_HEADING;
   1196 				sample.event.time = 0;
   1197 				sample.event.flags = SAMPLE_FLAGS_SEVERITY_INFO;
   1198 				sample.event.value = bearing;
   1199 				sample.event.name = NULL;
   1200 
   1201 				if (callback) callback(DC_SAMPLE_EVENT, &sample, userdata);
   1202 				have_bearing = 0;
   1203 			}
   1204 
   1205 			if (have_depth) {
   1206 				sample.depth = (signed int)(depth - depth_calibration) * (2.0 * BAR / 1000.0) / (density * 10.0);
   1207 				if (callback) callback (DC_SAMPLE_DEPTH, &sample, userdata);
   1208 			}
   1209 
   1210 			time += interval;
   1211 			complete--;
   1212 		}
   1213 	}
   1214 
   1215 	parser->cached = PROFILE;
   1216 
   1217 	return DC_STATUS_SUCCESS;
   1218 }
   1219 
   1220 
   1221 static dc_status_t
   1222 uwatec_smart_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata)
   1223 {
   1224 	uwatec_smart_parser_t *parser = (uwatec_smart_parser_t *) abstract;
   1225 
   1226 	// Cache the parser data.
   1227 	dc_status_t rc = uwatec_smart_parser_cache (parser);
   1228 	if (rc != DC_STATUS_SUCCESS)
   1229 		return rc;
   1230 
   1231 	// Cache the profile data.
   1232 	if (parser->cached < PROFILE) {
   1233 		rc = uwatec_smart_parse (parser, NULL, NULL);
   1234 		if (rc != DC_STATUS_SUCCESS)
   1235 			return rc;
   1236 	}
   1237 
   1238 	return uwatec_smart_parse (parser, callback, userdata);
   1239 }