deepsix_excursion_parser.c (23653B)
1 /* 2 * libdivecomputer 3 * 4 * Copyright (C) 2021 Ryan Gardner, 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 <stdio.h> 24 #include <stdarg.h> 25 #include <string.h> 26 27 #include <libdivecomputer/units.h> 28 29 #include "deepsix_excursion.h" 30 #include "context-private.h" 31 #include "parser-private.h" 32 #include "platform.h" 33 #include "array.h" 34 35 #define HEADERSIZE_MIN 128 36 37 #define MAX_SAMPLES 7 38 #define MAX_EVENTS 7 39 #define MAX_GASMIXES 20 40 41 #define ALARM 0x0001 42 #define TEMPERATURE 0x0002 43 #define DECO 0x0003 44 #define CEILING 0x0004 45 #define CNS 0x0005 46 47 #define SAMPLE_TEMPERATURE 0 48 #define SAMPLE_DECO_NDL 1 49 #define SAMPLE_CNS 2 50 51 #define EVENT_CHANGE_GAS 7 52 #define EVENT_ALARMS 8 53 #define EVENT_CHANGE_SETPOINT 9 54 #define EVENT_SAMPLES_MISSED 10 55 #define EVENT_RESERVED 15 56 57 #define ALARM_ASCENTRATE 0 58 #define ALARM_CEILING 1 59 #define ALARM_PO2 2 60 #define ALARM_MAXDEPTH 3 61 #define ALARM_DIVETIME 4 62 #define ALARM_CNS 5 63 64 #define DECOSTOP 0x02 65 #define SAFETYSTOP 0x04 66 67 #define DENSITY 1024 68 69 #define UNDEFINED 0xFFFFFFFF 70 71 #define FWVERSION(major,minor) ( \ 72 ((((major) + '0') & 0xFF) << 8) | \ 73 ((minor) & 0xFF)) 74 75 typedef struct deepsix_excursion_layout_t { 76 unsigned int headersize; 77 unsigned int version; 78 unsigned int divemode; 79 unsigned int samplerate; 80 unsigned int salinity; 81 unsigned int datetime; 82 unsigned int divetime; 83 unsigned int maxdepth; 84 unsigned int temperature_min; 85 unsigned int avgdepth; 86 unsigned int firmware; 87 unsigned int temperature_surf; 88 unsigned int atmospheric; 89 unsigned int gf; 90 } deepsix_excursion_layout_t; 91 92 typedef struct deepsix_excursion_gasmix_t { 93 unsigned int id; 94 unsigned int oxygen; 95 unsigned int helium; 96 } deepsix_excursion_gasmix_t; 97 98 typedef struct deepsix_excursion_sample_info_t { 99 unsigned int type; 100 unsigned int divisor; 101 unsigned int size; 102 } deepsix_excursion_sample_info_t; 103 104 typedef struct deepsix_excursion_event_info_t { 105 unsigned int type; 106 unsigned int size; 107 } deepsix_excursion_event_info_t; 108 109 typedef struct deepsix_excursion_parser_t { 110 dc_parser_t base; 111 unsigned int cached; 112 unsigned int ngasmixes; 113 deepsix_excursion_gasmix_t gasmix[MAX_GASMIXES]; 114 } deepsix_excursion_parser_t; 115 116 static dc_status_t deepsix_excursion_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime); 117 static dc_status_t deepsix_excursion_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value); 118 static dc_status_t deepsix_excursion_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata); 119 static dc_status_t deepsix_excursion_parser_samples_foreach_v0 (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata); 120 static dc_status_t deepsix_excursion_parser_samples_foreach_v1 (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata); 121 122 static const dc_parser_vtable_t deepsix_parser_vtable = { 123 sizeof(deepsix_excursion_parser_t), 124 DC_FAMILY_DEEPSIX_EXCURSION, 125 NULL, /* set_clock */ 126 NULL, /* set_atmospheric */ 127 NULL, /* set_density */ 128 deepsix_excursion_parser_get_datetime, /* datetime */ 129 deepsix_excursion_parser_get_field, /* fields */ 130 deepsix_excursion_parser_samples_foreach, /* samples_foreach */ 131 NULL /* destroy */ 132 }; 133 134 static const deepsix_excursion_layout_t deepsix_excursion_layout_v0 = { 135 156,/* headersize */ 136 UNDEFINED, /* version */ 137 4, /* divemode */ 138 24, /* samplerate */ 139 UNDEFINED, /* salinity */ 140 12, /* datetime */ 141 20, /* divetime */ 142 28, /* maxdepth */ 143 32, /* temperature_min */ 144 UNDEFINED, /* avgdepth */ 145 48, /* firmware */ 146 UNDEFINED, /* temperature_surf */ 147 56, /* atmospheric */ 148 UNDEFINED, /* gf */ 149 }; 150 151 static const deepsix_excursion_layout_t deepsix_excursion_layout_v1 = { 152 129,/* headersize */ 153 3, /* version */ 154 4, /* divemode */ 155 5, /* samplerate */ 156 7, /* salinity */ 157 12, /* datetime */ 158 19, /* divetime */ 159 29, /* maxdepth */ 160 31, /* temperature_min */ 161 33, /* avgdepth */ 162 35, /* firmware */ 163 43, /* temperature_surf */ 164 45, /* atmospheric */ 165 127, /* gf */ 166 }; 167 168 static double 169 pressure_to_depth(unsigned int depth, unsigned int atmospheric, unsigned int density) 170 { 171 return ((signed int)(depth - atmospheric)) * (BAR / 1000.0) / (density * GRAVITY); 172 } 173 174 static unsigned int 175 deepsix_excursion_find_gasmix(deepsix_excursion_parser_t *parser, unsigned int o2, unsigned int he, unsigned int id) 176 { 177 unsigned int i = 0; 178 while (i < parser->ngasmixes) { 179 if (o2 == parser->gasmix[i].oxygen && he == parser->gasmix[i].helium && id == parser->gasmix[i].id) 180 break; 181 i++; 182 } 183 return i; 184 } 185 186 dc_status_t 187 deepsix_excursion_parser_create (dc_parser_t **out, dc_context_t *context, const unsigned char data[], size_t size) 188 { 189 deepsix_excursion_parser_t *parser = NULL; 190 191 if (out == NULL) 192 return DC_STATUS_INVALIDARGS; 193 194 // Allocate memory. 195 parser = (deepsix_excursion_parser_t *) dc_parser_allocate (context, &deepsix_parser_vtable, data, size); 196 if (parser == NULL) { 197 ERROR (context, "Failed to allocate memory."); 198 return DC_STATUS_NOMEMORY; 199 } 200 201 // Set the default values. 202 parser->cached = 0; 203 parser->ngasmixes = 0; 204 for (unsigned int i = 0; i < MAX_GASMIXES; ++i) { 205 parser->gasmix[i].id = 0; 206 parser->gasmix[i].oxygen = 0; 207 parser->gasmix[i].helium = 0; 208 } 209 210 *out = (dc_parser_t *) parser; 211 212 return DC_STATUS_SUCCESS; 213 } 214 215 static dc_status_t 216 deepsix_excursion_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime) 217 { 218 const unsigned char *data = abstract->data; 219 unsigned int size = abstract->size; 220 221 if (size < HEADERSIZE_MIN) 222 return DC_STATUS_DATAFORMAT; 223 224 unsigned int version = data[3]; 225 const deepsix_excursion_layout_t *layout = version == 0 ? 226 &deepsix_excursion_layout_v0 : &deepsix_excursion_layout_v1; 227 228 if (size < layout->headersize) 229 return DC_STATUS_DATAFORMAT; 230 231 unsigned int firmware = array_uint16_be (data + layout->firmware + 4); 232 233 const unsigned char *p = data + layout->datetime; 234 235 if (datetime) { 236 datetime->year = p[0] + 2000; 237 datetime->month = p[1]; 238 datetime->day = p[2]; 239 datetime->hour = p[3]; 240 datetime->minute = p[4]; 241 datetime->second = p[5]; 242 243 if (version == 0) { 244 if (firmware >= FWVERSION(5, 'B')) { 245 datetime->timezone = (p[6] - 12) * 3600; 246 } else { 247 datetime->timezone = DC_TIMEZONE_NONE; 248 } 249 } else { 250 datetime->timezone = ((signed char) p[6]) * 900; 251 } 252 } 253 254 return DC_STATUS_SUCCESS; 255 } 256 257 static dc_status_t 258 deepsix_excursion_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value) 259 { 260 deepsix_excursion_parser_t *parser = (deepsix_excursion_parser_t *) abstract; 261 const unsigned char *data = abstract->data; 262 unsigned int size = abstract->size; 263 264 dc_gasmix_t *gasmix = (dc_gasmix_t *) value; 265 dc_salinity_t *water = (dc_salinity_t *) value; 266 dc_decomodel_t *decomodel = (dc_decomodel_t *) value; 267 268 if (size < HEADERSIZE_MIN) 269 return DC_STATUS_DATAFORMAT; 270 271 unsigned int version = data[3]; 272 const deepsix_excursion_layout_t *layout = version == 0 ? 273 &deepsix_excursion_layout_v0 : &deepsix_excursion_layout_v1; 274 275 if (size < layout->headersize) 276 return DC_STATUS_DATAFORMAT; 277 278 if (version != 0 && !parser->cached) { 279 dc_status_t rc = deepsix_excursion_parser_samples_foreach_v1(abstract, NULL, NULL); 280 if (rc != DC_STATUS_SUCCESS) 281 return rc; 282 } 283 284 unsigned int atmospheric = array_uint16_le(data + layout->atmospheric); 285 unsigned int density = DENSITY; 286 if (layout->salinity != UNDEFINED) { 287 density = 1000 + data[layout->salinity] * 10; 288 } 289 290 if (value) { 291 switch (type) { 292 case DC_FIELD_DIVETIME: 293 *((unsigned int *) value) = array_uint32_le(data + layout->divetime); 294 break; 295 case DC_FIELD_MAXDEPTH: 296 *((double *) value) = pressure_to_depth(array_uint16_le(data + layout->maxdepth), atmospheric, density); 297 break; 298 case DC_FIELD_AVGDEPTH: 299 if (layout->avgdepth == UNDEFINED) 300 return DC_STATUS_UNSUPPORTED; 301 *((double *) value) = pressure_to_depth(array_uint16_le(data + layout->avgdepth), atmospheric, density); 302 break; 303 case DC_FIELD_GASMIX_COUNT: 304 *((unsigned int *) value) = parser->ngasmixes; 305 break; 306 case DC_FIELD_GASMIX: 307 gasmix->usage = DC_USAGE_NONE; 308 gasmix->oxygen = parser->gasmix[flags].oxygen / 100.0; 309 gasmix->helium = parser->gasmix[flags].helium / 100.0; 310 gasmix->nitrogen = 1.0 - gasmix->oxygen - gasmix->helium; 311 break; 312 case DC_FIELD_TEMPERATURE_MINIMUM: 313 *((double *) value) = (signed int) array_uint16_le(data + layout->temperature_min) / 10.0; 314 break; 315 case DC_FIELD_TEMPERATURE_SURFACE: 316 if (layout->temperature_surf == UNDEFINED) 317 return DC_STATUS_UNSUPPORTED; 318 *((double *) value) = (signed int) array_uint16_le(data + layout->temperature_surf) / 10.0; 319 break; 320 case DC_FIELD_ATMOSPHERIC: 321 *((double *) value) = atmospheric / 1000.0; 322 break; 323 case DC_FIELD_SALINITY: 324 water->type = (density == 1000) ? DC_WATER_FRESH : DC_WATER_SALT; 325 water->density = density; 326 break; 327 case DC_FIELD_DIVEMODE: 328 switch (data[layout->divemode]) { 329 case 0: 330 *((dc_divemode_t *) value) = DC_DIVEMODE_OC; 331 break; 332 case 1: 333 *((dc_divemode_t *) value) = DC_DIVEMODE_GAUGE; 334 break; 335 case 2: 336 *((dc_divemode_t *) value) = DC_DIVEMODE_FREEDIVE; 337 break; 338 case 3: 339 *((dc_divemode_t *) value) = DC_DIVEMODE_CCR; 340 break; 341 default: 342 return DC_STATUS_DATAFORMAT; 343 } 344 break; 345 case DC_FIELD_DECOMODEL: 346 decomodel->type = DC_DECOMODEL_BUHLMANN; 347 decomodel->conservatism = 0; 348 if (layout->gf != UNDEFINED) { 349 decomodel->params.gf.low = data[layout->gf + 0]; 350 decomodel->params.gf.high = data[layout->gf + 1]; 351 } else { 352 decomodel->params.gf.low = 0; 353 decomodel->params.gf.high = 0; 354 } 355 break; 356 default: 357 return DC_STATUS_UNSUPPORTED; 358 } 359 } 360 361 return DC_STATUS_SUCCESS; 362 } 363 364 static dc_status_t 365 deepsix_excursion_parser_samples_foreach_v0 (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata) 366 { 367 const unsigned char *data = abstract->data; 368 unsigned int size = abstract->size; 369 const deepsix_excursion_layout_t *layout = &deepsix_excursion_layout_v0; 370 371 if (size < layout->headersize) 372 return DC_STATUS_DATAFORMAT; 373 374 int firmware4c = memcmp(data + layout->firmware, "D01-4C", 6) == 0; 375 376 unsigned int maxtype = firmware4c ? TEMPERATURE : CNS; 377 378 unsigned int interval = array_uint32_le(data + layout->samplerate); 379 unsigned int atmospheric = array_uint32_le(data + layout->atmospheric); 380 381 unsigned int time = 0; 382 unsigned int offset = layout->headersize; 383 while (offset + 1 < size) { 384 dc_sample_value_t sample = {0}; 385 386 // Get the sample type. 387 unsigned int type = data[offset]; 388 if (type < 1 || type > maxtype) { 389 ERROR (abstract->context, "Unknown sample type (%u).", type); 390 return DC_STATUS_DATAFORMAT; 391 } 392 393 // Get the sample length. 394 unsigned int length = 1; 395 if (type == ALARM || type == CEILING) { 396 length = 8; 397 } else if (type == TEMPERATURE || type == DECO || type == CNS) { 398 length = 6; 399 } 400 401 // Verify the length. 402 if (offset + length > size) { 403 WARNING (abstract->context, "Unexpected end of data."); 404 break; 405 } 406 407 unsigned int DC_ATTR_UNUSED misc = data[offset + 1]; 408 unsigned int depth = array_uint16_le(data + offset + 2); 409 410 if (type == TEMPERATURE) { 411 time += interval; 412 sample.time = time * 1000; 413 if (callback) callback(DC_SAMPLE_TIME, &sample, userdata); 414 415 sample.depth = pressure_to_depth(depth, atmospheric, DENSITY); 416 if (callback) callback(DC_SAMPLE_DEPTH, &sample, userdata); 417 } 418 419 if (type == ALARM) { 420 unsigned int DC_ATTR_UNUSED alarm_time = array_uint16_le(data + offset + 4); 421 unsigned int DC_ATTR_UNUSED alarm_value = array_uint16_le(data + offset + 6); 422 } else if (type == TEMPERATURE) { 423 unsigned int temperature = array_uint16_le(data + offset + 4); 424 if (firmware4c) { 425 if (temperature > 1300) { 426 length = 8; 427 } else if (temperature >= 10) { 428 sample.temperature = temperature / 10.0; 429 if (callback) callback(DC_SAMPLE_TEMPERATURE, &sample, userdata); 430 } 431 } else { 432 sample.temperature = temperature / 10.0; 433 if (callback) callback(DC_SAMPLE_TEMPERATURE, &sample, userdata); 434 } 435 } else if (type == DECO) { 436 unsigned int DC_ATTR_UNUSED deco = array_uint16_le(data + offset + 4); 437 } else if (type == CEILING) { 438 unsigned int DC_ATTR_UNUSED ceiling_depth = array_uint16_le(data + offset + 4); 439 unsigned int DC_ATTR_UNUSED ceiling_time = array_uint16_le(data + offset + 6); 440 } else if (type == CNS) { 441 unsigned int cns = array_uint16_le(data + offset + 4); 442 sample.cns = cns / 100.0; 443 if (callback) callback(DC_SAMPLE_CNS, &sample, userdata); 444 } 445 446 offset += length; 447 } 448 449 return DC_STATUS_SUCCESS; 450 } 451 452 static dc_status_t 453 deepsix_excursion_parser_samples_foreach_v1 (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata) 454 { 455 deepsix_excursion_parser_t *parser = (deepsix_excursion_parser_t *) abstract; 456 const unsigned char *data = abstract->data; 457 unsigned int size = abstract->size; 458 const deepsix_excursion_layout_t *layout = &deepsix_excursion_layout_v1; 459 460 if (size < layout->headersize) 461 return DC_STATUS_DATAFORMAT; 462 463 unsigned int headersize = data[2]; 464 if (headersize < layout->headersize) 465 return DC_STATUS_DATAFORMAT; 466 467 unsigned int samplerate = data[layout->samplerate]; 468 unsigned int atmospheric = array_uint16_le(data + layout->atmospheric); 469 unsigned int density = 1000 + data[layout->salinity] * 10; 470 471 unsigned int offset = headersize; 472 if (offset + 1 > size) { 473 ERROR (abstract->context, "Buffer overflow detected!"); 474 return DC_STATUS_DATAFORMAT; 475 } 476 477 unsigned int nconfig = data[offset]; 478 if (nconfig > MAX_SAMPLES) { 479 ERROR(abstract->context, "Too many sample descriptors (%u).", nconfig); 480 return DC_STATUS_DATAFORMAT; 481 } 482 483 offset += 1; 484 485 if (offset + 3 * nconfig > size) { 486 ERROR (abstract->context, "Buffer overflow detected!"); 487 return DC_STATUS_DATAFORMAT; 488 } 489 490 deepsix_excursion_sample_info_t sample_info[MAX_SAMPLES] = {{0}}; 491 for (unsigned int i = 0; i < nconfig; i++) { 492 sample_info[i].type = data[offset + 3 * i + 0]; 493 sample_info[i].size = data[offset + 3 * i + 1]; 494 sample_info[i].divisor = data[offset + 3 * i + 2]; 495 496 if (sample_info[i].divisor) { 497 switch (sample_info[i].type) { 498 case SAMPLE_CNS: 499 case SAMPLE_TEMPERATURE: 500 if (sample_info[i].size != 2) { 501 ERROR(abstract->context, "Unexpected sample size (%u).", sample_info[i].size); 502 return DC_STATUS_DATAFORMAT; 503 } 504 break; 505 case SAMPLE_DECO_NDL: 506 if (sample_info[i].size != 7) { 507 ERROR(abstract->context, "Unexpected sample size (%u).", sample_info[i].size); 508 return DC_STATUS_DATAFORMAT; 509 } 510 break; 511 default: 512 WARNING (abstract->context, "Unknown sample descriptor (%u %u %u).", 513 sample_info[i].type, sample_info[i].size, sample_info[i].divisor); 514 break; 515 } 516 } 517 } 518 519 offset += 3 * nconfig; 520 521 if (offset + 1 > size) { 522 ERROR (abstract->context, "Buffer overflow detected!"); 523 return DC_STATUS_DATAFORMAT; 524 } 525 526 unsigned int nevents = data[offset]; 527 if (nevents > MAX_EVENTS) { 528 ERROR(abstract->context, "Too many event descriptors (%u).", nevents); 529 return DC_STATUS_DATAFORMAT; 530 } 531 532 offset += 1; 533 534 if (offset + 2 * nevents > size) { 535 ERROR (abstract->context, "Buffer overflow detected!"); 536 return DC_STATUS_DATAFORMAT; 537 } 538 539 deepsix_excursion_event_info_t event_info[MAX_EVENTS] = {{0}}; 540 for (unsigned int i = 0; i < nevents; i++) { 541 event_info[i].type = data[offset + 2 * i]; 542 event_info[i].size = data[offset + 2 * i + 1]; 543 544 switch (event_info[i].type) { 545 case EVENT_ALARMS: 546 if (event_info[i].size != 1) { 547 ERROR(abstract->context, "Unexpected event size (%u).", event_info[i].size); 548 return DC_STATUS_DATAFORMAT; 549 } 550 break; 551 case EVENT_CHANGE_GAS: 552 if (event_info[i].size != 3) { 553 ERROR(abstract->context, "Unexpected event size (%u).", event_info[i].size); 554 return DC_STATUS_DATAFORMAT; 555 } 556 break; 557 case EVENT_CHANGE_SETPOINT: 558 if (event_info[i].size != 4 && event_info[i].size != 2) { 559 ERROR(abstract->context, "Unexpected event size (%u).", event_info[i].size); 560 return DC_STATUS_DATAFORMAT; 561 } 562 break; 563 case EVENT_SAMPLES_MISSED: 564 if (event_info[i].size != 6) { 565 ERROR(abstract->context, "Unexpected event size (%u).", event_info[i].size); 566 return DC_STATUS_DATAFORMAT; 567 } 568 break; 569 default: 570 WARNING (abstract->context, "Unknown event descriptor (%u %u).", 571 event_info[i].type, event_info[i].size); 572 break; 573 } 574 } 575 576 offset += 2 * nevents; 577 578 unsigned int time = 0; 579 unsigned int nsamples = 0; 580 while (offset + 3 <= size) { 581 dc_sample_value_t sample = {0}; 582 nsamples++; 583 584 // Time (seconds). 585 time += samplerate; 586 sample.time = time * 1000; 587 if (callback) callback (DC_SAMPLE_TIME, &sample, userdata); 588 589 unsigned int depth = array_uint16_le (data + offset); 590 sample.depth = pressure_to_depth(depth, atmospheric, density); 591 if (callback) callback (DC_SAMPLE_DEPTH, &sample, userdata); 592 offset += 2; 593 594 // event info 595 unsigned int length = data[offset]; 596 offset += 1; 597 598 if (offset + length > size) { 599 ERROR (abstract->context, "Buffer overflow detected!"); 600 return DC_STATUS_DATAFORMAT; 601 } 602 603 if (length) { 604 if (length < 2) { 605 ERROR (abstract->context, "Buffer overflow detected!"); 606 return DC_STATUS_DATAFORMAT; 607 } 608 609 unsigned int events = array_uint16_le (data + offset); 610 unsigned int event_offset = 2; 611 612 for (unsigned int i = 0; i < nevents; i++) { 613 if ((events & (1 << event_info[i].type)) == 0) 614 continue; 615 616 if (event_offset + event_info[i].size > length) { 617 ERROR (abstract->context, "Buffer overflow detected!"); 618 return DC_STATUS_DATAFORMAT; 619 } 620 621 unsigned int alarms = 0; 622 unsigned int id = 0, o2 = 0, he = 0; 623 unsigned int mix_idx = 0; 624 unsigned int count = 0, timestamp = 0; 625 switch (event_info[i].type) { 626 case EVENT_ALARMS: 627 alarms = data[offset + event_offset]; 628 for (unsigned int v = alarms, j = 0; v; v >>= 1, ++j) { 629 if ((v & 1) == 0) 630 continue; 631 632 sample.event.type = SAMPLE_EVENT_NONE; 633 sample.event.time = 0; 634 sample.event.flags = 0; 635 sample.event.value = 0; 636 switch (j) { 637 case ALARM_ASCENTRATE: 638 sample.event.type = SAMPLE_EVENT_ASCENT; 639 break; 640 case ALARM_CEILING: 641 sample.event.type = SAMPLE_EVENT_CEILING; 642 break; 643 case ALARM_PO2: 644 sample.event.type = SAMPLE_EVENT_PO2; 645 break; 646 case ALARM_MAXDEPTH: 647 sample.event.type = SAMPLE_EVENT_MAXDEPTH; 648 break; 649 case ALARM_DIVETIME: 650 sample.event.type = SAMPLE_EVENT_DIVETIME; 651 break; 652 case ALARM_CNS: 653 break; 654 default: 655 WARNING (abstract->context, "Unknown event (%u).", j); 656 break; 657 } 658 if (sample.event.type != SAMPLE_EVENT_NONE) { 659 if (callback) callback (DC_SAMPLE_EVENT, &sample, userdata); 660 } 661 } 662 break; 663 case EVENT_CHANGE_GAS: 664 id = data[offset + event_offset]; 665 o2 = data[offset + event_offset + 1]; 666 he = data[offset + event_offset + 2]; 667 668 mix_idx = deepsix_excursion_find_gasmix(parser, o2, he, id); 669 if (mix_idx >= parser->ngasmixes) { 670 if (mix_idx >= MAX_GASMIXES) { 671 ERROR (abstract->context, "Maximum number of gas mixes reached."); 672 return DC_STATUS_NOMEMORY; 673 } 674 parser->gasmix[mix_idx].oxygen = o2; 675 parser->gasmix[mix_idx].helium = he; 676 parser->gasmix[mix_idx].id = id; 677 parser->ngasmixes = mix_idx + 1; 678 } 679 680 sample.gasmix = mix_idx; 681 if (callback) callback(DC_SAMPLE_GASMIX, &sample, userdata); 682 break; 683 case EVENT_CHANGE_SETPOINT: 684 // Ignore the 4 byte variant because it's not 685 // supposed to be present in the sample data. 686 if (event_info[i].size == 2) { 687 sample.setpoint = data[offset + event_offset] / 10.0; 688 if (callback) callback(DC_SAMPLE_SETPOINT, &sample, userdata); 689 } 690 break; 691 case EVENT_SAMPLES_MISSED: 692 count = array_uint16_le(data + offset + event_offset); 693 timestamp = array_uint32_le(data + offset + event_offset + 2); 694 if (timestamp < time) { 695 ERROR (abstract->context, "Timestamp moved backwards (%u %u).", timestamp, time); 696 return DC_STATUS_DATAFORMAT; 697 } 698 nsamples += count; 699 time = timestamp; 700 break; 701 default: 702 WARNING (abstract->context, "Unknown event (%u %u).", 703 event_info[i].type, event_info[i].size); 704 break; 705 } 706 event_offset += event_info[i].size; 707 } 708 709 // Skip remaining sample bytes (if any). 710 if (event_offset < length) { 711 WARNING (abstract->context, "Remaining %u bytes skipped.", length - event_offset); 712 } 713 offset += length; 714 } 715 716 for (unsigned int i = 0; i < nconfig; ++i) { 717 if (sample_info[i].divisor && (nsamples % sample_info[i].divisor) == 0) { 718 if (offset + sample_info[i].size > size) { 719 ERROR (abstract->context, "Buffer overflow detected!"); 720 return DC_STATUS_DATAFORMAT; 721 } 722 723 unsigned int value = 0; 724 unsigned int deco_flags = 0, deco_ndl_tts = 0; 725 unsigned int deco_depth = 0, deco_time = 0; 726 switch (sample_info[i].type) { 727 case SAMPLE_TEMPERATURE: 728 value = array_uint16_le(data + offset); 729 sample.temperature = value / 10.0; 730 if (callback) callback(DC_SAMPLE_TEMPERATURE, &sample, userdata); 731 break; 732 case SAMPLE_CNS: 733 value = array_uint16_le(data + offset); 734 sample.cns = value / 10000.0; 735 if (callback) callback (DC_SAMPLE_CNS, &sample, userdata); 736 break; 737 case SAMPLE_DECO_NDL: 738 deco_flags = data[offset]; 739 deco_ndl_tts = array_uint16_le(data + offset + 1); 740 deco_depth = array_uint16_le(data + offset + 3); 741 deco_time = array_uint16_le(data + offset + 5); 742 if (deco_flags & DECOSTOP) { 743 sample.deco.type = DC_DECO_DECOSTOP; 744 sample.deco.depth = pressure_to_depth(deco_depth, atmospheric, density); 745 sample.deco.time = deco_time; 746 sample.deco.tts = deco_ndl_tts; 747 } else if (deco_flags & SAFETYSTOP) { 748 sample.deco.type = DC_DECO_SAFETYSTOP; 749 sample.deco.depth = pressure_to_depth(deco_depth, atmospheric, density); 750 sample.deco.time = deco_time; 751 sample.deco.tts = 0; 752 } else { 753 sample.deco.type = DC_DECO_NDL; 754 sample.deco.depth = 0; 755 sample.deco.time = deco_ndl_tts; 756 sample.deco.tts = 0; 757 } 758 if (callback) callback (DC_SAMPLE_DECO, &sample, userdata); 759 break; 760 default: 761 break; 762 } 763 offset += sample_info[i].size; 764 } 765 } 766 } 767 768 parser->cached = 1; 769 770 return DC_STATUS_SUCCESS; 771 } 772 773 static dc_status_t 774 deepsix_excursion_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata) 775 { 776 const unsigned char *data = abstract->data; 777 unsigned int size = abstract->size; 778 779 if (size < HEADERSIZE_MIN) 780 return DC_STATUS_DATAFORMAT; 781 782 unsigned int version = data[3]; 783 784 if (version == 0) { 785 return deepsix_excursion_parser_samples_foreach_v0(abstract, callback, userdata); 786 } else { 787 return deepsix_excursion_parser_samples_foreach_v1(abstract, callback, userdata); 788 } 789 }