halcyon_symbios_parser.c (25992B)
1 /* 2 * libdivecomputer 3 * 4 * Copyright (C) 2023 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> 24 25 #include <libdivecomputer/units.h> 26 27 #include "halcyon_symbios.h" 28 #include "context-private.h" 29 #include "parser-private.h" 30 #include "platform.h" 31 #include "array.h" 32 33 #define ID_HEADER 0x01 34 #define ID_GAS_SWITCH 0x02 35 #define ID_DEPTH 0x03 36 #define ID_TEMPERATURE 0x04 37 #define ID_OC_CC_SWITCH 0x05 38 #define ID_GAS_TRANSMITTER 0x06 39 #define ID_COMPARTMENTS 0x07 40 #define ID_GPS 0x08 41 #define ID_PO2_BOARD 0x09 42 #define ID_DECO 0x0A 43 #define ID_GF 0x0B 44 #define ID_FOOTER 0x0C 45 #define ID_PO2_REBREATHER 0x0D 46 #define ID_COMPASS 0x0E 47 #define ID_LOG_VERSION 0x0F 48 #define ID_TRIM 0x10 49 #define ID_GAS_CONFIG 0x11 50 #define ID_TANK_TRANSMITTER 0x12 51 #define ID_GF_INFO 0x13 52 #define ID_SGC 0x14 53 #define ID_GF_DATA 0x15 54 55 #define ISCONFIG(type) ( \ 56 (type) == ID_LOG_VERSION || \ 57 (type) == ID_HEADER || \ 58 (type) == ID_FOOTER) 59 60 #define LOGVERSION(major,minor) ( \ 61 (((major) & 0xFF) << 8) | \ 62 ((minor) & 0xFF)) 63 64 #define UNDEFINED 0xFFFFFFFF 65 66 #define EPOCH 1609459200 /* 2021-01-01 00:00:00 */ 67 68 #define OC 0 69 #define CCR 1 70 #define CCR_FSP 2 71 #define SIDEMOUNT 3 72 #define GAUGE 4 73 74 #define NGASMIXES 10 75 #define NTANKS 10 76 #define NGAS_SLOTS 5 /* Record IDs 0-4 (GAS1-GAS5) are gas slots; IDs >= 5 are diluents. */ 77 78 #define TRANSMITTER_ID (1u << 16) 79 80 typedef struct halcyon_symbios_gasmix_t { 81 unsigned int id; 82 unsigned int oxygen; 83 unsigned int helium; 84 } halcyon_symbios_gasmix_t; 85 86 typedef struct halcyon_symbios_tank_t { 87 unsigned int id; 88 unsigned int beginpressure; 89 unsigned int endpressure; 90 unsigned int gasmix; 91 dc_tank_usage_t usage; 92 } halcyon_symbios_tank_t; 93 94 typedef struct halcyon_symbios_parser_t { 95 dc_parser_t base; 96 // Cached fields. 97 unsigned int cached; 98 unsigned int logversion; 99 unsigned int datetime; 100 int timezone; 101 unsigned int divetime; 102 unsigned int maxdepth; 103 unsigned int divemode; 104 unsigned int atmospheric; 105 unsigned int ngasmixes; 106 unsigned int ntanks; 107 halcyon_symbios_gasmix_t gasmix[NGASMIXES]; 108 halcyon_symbios_tank_t tank[NTANKS]; 109 unsigned int gf_lo; 110 unsigned int gf_hi; 111 } halcyon_symbios_parser_t; 112 113 static dc_status_t halcyon_symbios_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime); 114 static dc_status_t halcyon_symbios_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value); 115 static dc_status_t halcyon_symbios_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata); 116 117 static const dc_parser_vtable_t halcyon_symbios_parser_vtable = { 118 sizeof(halcyon_symbios_parser_t), 119 DC_FAMILY_HALCYON_SYMBIOS, 120 NULL, /* set_clock */ 121 NULL, /* set_atmospheric */ 122 NULL, /* set_density */ 123 halcyon_symbios_parser_get_datetime, /* datetime */ 124 halcyon_symbios_parser_get_field, /* fields */ 125 halcyon_symbios_parser_samples_foreach, /* samples_foreach */ 126 NULL /* destroy */ 127 }; 128 129 dc_status_t 130 halcyon_symbios_parser_create (dc_parser_t **out, dc_context_t *context, const unsigned char data[], size_t size) 131 { 132 halcyon_symbios_parser_t *parser = NULL; 133 134 if (out == NULL) 135 return DC_STATUS_INVALIDARGS; 136 137 // Allocate memory. 138 parser = (halcyon_symbios_parser_t *) dc_parser_allocate (context, &halcyon_symbios_parser_vtable, data, size); 139 if (parser == NULL) { 140 ERROR (context, "Failed to allocate memory."); 141 return DC_STATUS_NOMEMORY; 142 } 143 144 // Set the default values. 145 parser->cached = 0; 146 parser->logversion = 0; 147 parser->datetime = UNDEFINED; 148 parser->timezone = 0; 149 parser->divetime = 0; 150 parser->maxdepth = 0; 151 parser->divemode = UNDEFINED; 152 parser->atmospheric = UNDEFINED; 153 parser->gf_lo = UNDEFINED; 154 parser->gf_hi = UNDEFINED; 155 parser->ngasmixes = 0; 156 parser->ntanks = 0; 157 for (unsigned int i = 0; i < NGASMIXES; ++i) { 158 parser->gasmix[i].oxygen = 0; 159 parser->gasmix[i].helium = 0; 160 } 161 for (unsigned int i = 0; i < NTANKS; ++i) { 162 parser->tank[i].id = 0; 163 parser->tank[i].beginpressure = 0; 164 parser->tank[i].endpressure = 0; 165 parser->tank[i].gasmix = DC_GASMIX_UNKNOWN; 166 parser->tank[i].usage = DC_TANK_USAGE_NONE; 167 } 168 169 *out = (dc_parser_t *) parser; 170 171 return DC_STATUS_SUCCESS; 172 } 173 174 static dc_status_t 175 halcyon_symbios_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime) 176 { 177 dc_status_t status = DC_STATUS_SUCCESS; 178 halcyon_symbios_parser_t *parser = (halcyon_symbios_parser_t *) abstract; 179 180 // Cache the profile data. 181 if (!parser->cached) { 182 status = halcyon_symbios_parser_samples_foreach (abstract, NULL, NULL); 183 if (status != DC_STATUS_SUCCESS) 184 return status; 185 } 186 187 if (parser->datetime == UNDEFINED) 188 return DC_STATUS_UNSUPPORTED; 189 190 dc_ticks_t ticks = (dc_ticks_t) parser->datetime + EPOCH; 191 192 if (parser->logversion >= LOGVERSION(1,9)) { 193 // For firmware versions with timezone support, the UTC offset of the 194 // device is used. 195 int timezone = parser->timezone * 3600; 196 197 ticks += timezone; 198 199 if (!dc_datetime_gmtime (datetime, ticks)) 200 return DC_STATUS_DATAFORMAT; 201 202 datetime->timezone = timezone; 203 } else { 204 // For firmware versions without timezone support, the current timezone 205 // of the host system is used. 206 if (!dc_datetime_localtime (datetime, ticks)) 207 return DC_STATUS_DATAFORMAT; 208 } 209 210 return DC_STATUS_SUCCESS; 211 } 212 213 static dc_status_t 214 halcyon_symbios_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value) 215 { 216 dc_status_t status = DC_STATUS_SUCCESS; 217 halcyon_symbios_parser_t *parser = (halcyon_symbios_parser_t *) abstract; 218 219 // Cache the profile data. 220 if (!parser->cached) { 221 status = halcyon_symbios_parser_samples_foreach (abstract, NULL, NULL); 222 if (status != DC_STATUS_SUCCESS) 223 return status; 224 } 225 226 dc_gasmix_t *gasmix = (dc_gasmix_t *) value; 227 dc_tank_t *tank = (dc_tank_t *) value; 228 dc_decomodel_t *decomodel = (dc_decomodel_t *) value; 229 230 if (value) { 231 switch (type) { 232 case DC_FIELD_DIVETIME: 233 *((unsigned int *) value) = parser->divetime; 234 break; 235 case DC_FIELD_MAXDEPTH: 236 *((double *) value) = parser->maxdepth / 100.0; 237 break; 238 case DC_FIELD_ATMOSPHERIC: 239 if (parser->atmospheric == UNDEFINED) 240 return DC_STATUS_UNSUPPORTED; 241 *((double *) value) = parser->atmospheric / 1000.0; 242 break; 243 case DC_FIELD_DIVEMODE: 244 switch (parser->divemode) { 245 case OC: 246 case SIDEMOUNT: 247 *((dc_divemode_t *) value) = DC_DIVEMODE_OC; 248 break; 249 case CCR: 250 case CCR_FSP: 251 *((dc_divemode_t *) value) = DC_DIVEMODE_CCR; 252 break; 253 case GAUGE: 254 *((dc_divemode_t *) value) = DC_DIVEMODE_GAUGE; 255 break; 256 case UNDEFINED: 257 return DC_STATUS_UNSUPPORTED; 258 default: 259 return DC_STATUS_DATAFORMAT; 260 } 261 break; 262 case DC_FIELD_GASMIX_COUNT: 263 *((unsigned int *) value) = parser->ngasmixes; 264 break; 265 case DC_FIELD_GASMIX: 266 gasmix->helium = parser->gasmix[flags].helium / 100.0; 267 gasmix->oxygen = parser->gasmix[flags].oxygen / 100.0; 268 gasmix->nitrogen = 1.0 - gasmix->oxygen - gasmix->helium; 269 gasmix->usage = DC_USAGE_NONE; 270 break; 271 case DC_FIELD_TANK_COUNT: 272 *((unsigned int *) value) = parser->ntanks; 273 break; 274 case DC_FIELD_TANK: 275 tank->type = DC_TANKVOLUME_NONE; 276 tank->volume = 0.0; 277 tank->workpressure = 0.0; 278 tank->beginpressure = parser->tank[flags].beginpressure / 10.0; 279 tank->endpressure = parser->tank[flags].endpressure / 10.0; 280 tank->gasmix = parser->tank[flags].gasmix; 281 tank->usage = parser->tank[flags].usage; 282 break; 283 case DC_FIELD_DECOMODEL: 284 if (parser->gf_lo == UNDEFINED || parser->gf_hi == UNDEFINED) 285 return DC_STATUS_UNSUPPORTED; 286 decomodel->type = DC_DECOMODEL_BUHLMANN; 287 decomodel->conservatism = 0; 288 decomodel->params.gf.low = parser->gf_lo; 289 decomodel->params.gf.high = parser->gf_hi; 290 break; 291 default: 292 return DC_STATUS_UNSUPPORTED; 293 } 294 } 295 296 return DC_STATUS_SUCCESS; 297 } 298 299 static dc_status_t 300 halcyon_symbios_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata) 301 { 302 halcyon_symbios_parser_t *parser = (halcyon_symbios_parser_t *) abstract; 303 const unsigned char *data = abstract->data; 304 unsigned int size = abstract->size; 305 306 static const unsigned int lengths[] = { 307 4, /* ID_LOG_VERSION */ 308 64, /* ID_HEADER */ 309 4, /* ID_GAS_SWITCH */ 310 4, /* ID_DEPTH */ 311 4, /* ID_TEMPERATURE */ 312 4, /* ID_OC_CC_SWITCH */ 313 12, /* ID_GAS_TRANSMITTER */ 314 68, /* ID_COMPARTMENTS */ 315 12, /* ID_GPS */ 316 8, /* ID_PO2_BOARD */ 317 16, /* ID_DECO */ 318 4, /* ID_GF */ 319 16, /* ID_FOOTER */ 320 12, /* ID_PO2_REBREATHER */ 321 4, /* ID_COMPASS */ 322 4, /* ID_LOG_VERSION */ 323 4, /* ID_TRIM */ 324 8, /* ID_GAS_CONFIG */ 325 8, /* ID_TANK_TRANSMITTER */ 326 6, /* ID_GF_INFO */ 327 4, /* ID_SGC */ 328 8, /* ID_GF_DATA */ 329 }; 330 331 unsigned int logversion = 0; 332 unsigned int time_start = UNDEFINED, time_end = UNDEFINED; 333 int timezone = 0; 334 unsigned int maxdepth = 0; 335 unsigned int divemode = UNDEFINED; 336 unsigned int atmospheric = UNDEFINED; 337 unsigned int gf_lo = UNDEFINED; 338 unsigned int gf_hi = UNDEFINED; 339 unsigned int ngasmixes = 0; 340 unsigned int ntanks = 0; 341 halcyon_symbios_gasmix_t gasmix[NGASMIXES] = {0}; 342 // Every configured gas slot (enabled or disabled), kept so a later 343 // ID_GAS_TRANSMITTER record can still resolve a disabled slot instead 344 // of failing, even though disabled slots are not exposed in gasmix[]. 345 halcyon_symbios_gasmix_t gasconfig[NGASMIXES] = {0}; 346 unsigned int ngasconfig = 0; 347 halcyon_symbios_tank_t tank[NTANKS] = {0}; 348 unsigned int gasmix_id_previous = UNDEFINED; 349 unsigned int gasmix_idx = DC_GASMIX_UNKNOWN; 350 unsigned int tank_id_previous = UNDEFINED; 351 unsigned int tank_usage_previous = UNDEFINED; 352 unsigned int tank_idx = UNDEFINED; 353 unsigned int interval = 0; 354 355 unsigned int have_time = 0; 356 unsigned int have_depth = 0; 357 358 unsigned int time = 0; 359 unsigned int offset = 0; 360 while (offset + 2 <= size) { 361 dc_sample_value_t sample = {0}; 362 363 unsigned int type = data[offset + 0]; 364 unsigned int length = data[offset + 1]; 365 366 if (length < 2 || offset + length > size) { 367 ERROR (abstract->context, "Buffer overflow detected!"); 368 return DC_STATUS_DATAFORMAT; 369 } 370 371 // Since log version 1.9, the ID_GF_INFO record has been deprecated and 372 // replaced with the larger ID_GF_DATA record. Unfortunately some 373 // earlier firmware versions produced records with the new type, but 374 // with the old size. This has been fixed in log version 1.12. 375 // Correct the record type to workaround this bug. 376 if (type == ID_GF_DATA && length == lengths[ID_GF_INFO]) { 377 type = ID_GF_INFO; 378 } 379 380 if (type < C_ARRAY_SIZE(lengths)) { 381 if (length != lengths[type]) { 382 ERROR (abstract->context, "Unexpected record size (%u %u).", length, lengths[type]); 383 return DC_STATUS_DATAFORMAT; 384 } 385 } 386 387 // Generate a timestamp for the first non-config record and every 388 // depth record, except the first one. The first depth record must be 389 // excluded because the sample already has a timestamp from the first 390 // non-config record. 391 if ((!have_time && !ISCONFIG(type)) || 392 (have_depth && type == ID_DEPTH)) { 393 time += interval; 394 sample.time = time * 1000; 395 if (callback) callback(DC_SAMPLE_TIME, &sample, userdata); 396 have_time = 1; 397 } 398 399 if (type == ID_LOG_VERSION) { 400 logversion = array_uint16_be (data + offset + 2); 401 unsigned int version_major = data[offset + 2]; 402 unsigned int version_minor = data[offset + 3]; 403 DEBUG (abstract->context, "Version: %u.%u", 404 version_major, version_minor); 405 } else if (type == ID_HEADER) { 406 unsigned int model = data[offset + 2]; 407 unsigned int hw_major = data[offset + 3]; 408 unsigned int hw_minor = data[offset + 4]; 409 unsigned int fw_major = data[offset + 5]; 410 unsigned int fw_minor = data[offset + 6]; 411 unsigned int fw_bugfix = data[offset + 7]; 412 unsigned int deco_major = data[offset + 8]; 413 unsigned int deco_minor = data[offset + 9]; 414 interval = data[offset + 10]; 415 unsigned int DC_ATTR_UNUSED detection = data[offset + 11]; 416 unsigned int DC_ATTR_UNUSED noflytime = data[offset + 12]; 417 divemode = data[offset + 13]; 418 timezone = (signed char) data[offset + 14]; 419 atmospheric = array_uint16_le(data + offset + 16); 420 unsigned int DC_ATTR_UNUSED number = array_uint16_le(data + offset + 18); 421 unsigned int DC_ATTR_UNUSED battery = array_uint16_le(data + offset + 20); 422 time_start = array_uint32_le(data + offset + 24); 423 unsigned int serial = array_uint32_le(data + offset + 28); 424 DEBUG (abstract->context, "Device: model=%u, serial=%u, firmware=%u.%u.%u, hw=%u.%u, deco=%u.%u", 425 model, 426 serial, 427 fw_major, fw_minor, fw_bugfix, 428 hw_major, hw_minor, 429 deco_major, deco_minor); 430 } else if (type == ID_GAS_SWITCH) { 431 unsigned int id = UNDEFINED; 432 unsigned int o2 = data[offset + 2]; 433 unsigned int he = data[offset + 3]; 434 435 unsigned int idx = 0; 436 while (idx < ngasmixes) { 437 if (id == gasmix[idx].id && 438 o2 == gasmix[idx].oxygen && 439 he == gasmix[idx].helium) 440 break; 441 idx++; 442 } 443 if (idx >= ngasmixes) { 444 if (ngasmixes >= NGASMIXES) { 445 ERROR (abstract->context, "Maximum number of gas mixes reached."); 446 return DC_STATUS_NOMEMORY; 447 } 448 gasmix[ngasmixes].id = id; 449 gasmix[ngasmixes].oxygen = o2; 450 gasmix[ngasmixes].helium = he; 451 ngasmixes++; 452 } 453 sample.gasmix = idx; 454 if (callback) callback(DC_SAMPLE_GASMIX, &sample, userdata); 455 } else if (type == ID_DEPTH) { 456 unsigned int depth = array_uint16_le (data + offset + 2); 457 if (maxdepth < depth) 458 maxdepth = depth; 459 sample.depth = depth / 100.0; 460 if (callback) callback (DC_SAMPLE_DEPTH, &sample, userdata); 461 have_depth = 1; 462 } else if (type == ID_TEMPERATURE) { 463 unsigned int temperature = array_uint16_le (data + offset + 2); 464 sample.depth = temperature / 10.0; 465 if (callback) callback (DC_SAMPLE_TEMPERATURE, &sample, userdata); 466 } else if (type == ID_OC_CC_SWITCH) { 467 unsigned int DC_ATTR_UNUSED ccr = data[offset + 2]; 468 } else if (type == ID_GAS_TRANSMITTER) { 469 unsigned int gas_id = data[offset + 2]; 470 unsigned int DC_ATTR_UNUSED battery = array_uint16_le (data + offset + 4); 471 unsigned int pressure = array_uint16_le (data + offset + 6); 472 unsigned int transmitter = array_uint16_le (data + offset + 8); 473 dc_tank_usage_t usage = DC_TANK_USAGE_NONE; 474 475 // Every configured gas (enabled or not) is recorded in 476 // gasconfig[], so checking it covers both an already exposed 477 // gas and one only resolvable through this transmitter. 478 if (ngasconfig > 0 && gasmix_id_previous != gas_id) { 479 unsigned int idx = 0; 480 while (idx < ngasmixes) { 481 if (gas_id == gasmix[idx].id) 482 break; 483 idx++; 484 } 485 if (idx >= ngasmixes) { 486 // Not yet exposed, because the gas slot was disabled 487 // when its ID_GAS_CONFIG record was parsed. A 488 // transmitter still reporting it means it is in use 489 // after all, so resolve it from the retained 490 // configuration instead of failing outright. 491 unsigned int cfgidx = 0; 492 while (cfgidx < ngasconfig) { 493 if (gas_id == gasconfig[cfgidx].id) 494 break; 495 cfgidx++; 496 } 497 if (cfgidx >= ngasconfig) { 498 ERROR (abstract->context, "Invalid gas mix id (%u).", gas_id); 499 return DC_STATUS_DATAFORMAT; 500 } 501 if (ngasmixes >= NGASMIXES) { 502 ERROR (abstract->context, "Maximum number of gas mixes reached."); 503 return DC_STATUS_NOMEMORY; 504 } 505 idx = ngasmixes; 506 gasmix[idx] = gasconfig[cfgidx]; 507 ngasmixes++; 508 } 509 sample.gasmix = idx; 510 if (callback) callback(DC_SAMPLE_GASMIX, &sample, userdata); 511 gasmix_id_previous = gas_id; 512 gasmix_idx = idx; 513 } 514 515 if (tank_id_previous != transmitter || 516 tank_usage_previous != usage) { 517 // Find the tank in the list. 518 unsigned int idx = 0; 519 while (idx < ntanks) { 520 if (tank[idx].id == transmitter && 521 tank[idx].usage == usage) 522 break; 523 idx++; 524 } 525 526 // Add a new tank if necessary. 527 if (idx >= ntanks) { 528 if (ntanks >= NTANKS) { 529 ERROR (abstract->context, "Maximum number of tanks reached."); 530 return DC_STATUS_NOMEMORY; 531 } 532 tank[ntanks].id = transmitter; 533 tank[ntanks].beginpressure = pressure; 534 tank[ntanks].endpressure = pressure; 535 tank[ntanks].gasmix = gasmix_idx; 536 tank[ntanks].usage = usage; 537 ntanks++; 538 } 539 540 tank_id_previous = transmitter; 541 tank_usage_previous = usage; 542 tank_idx = idx; 543 } 544 tank[tank_idx].endpressure = pressure; 545 546 sample.pressure.tank = tank_idx; 547 sample.pressure.value = pressure / 10.0; 548 if (callback) callback(DC_SAMPLE_PRESSURE, &sample, userdata); 549 } else if (type == ID_COMPARTMENTS) { 550 for (unsigned int i = 0; i < 16; ++i) { 551 unsigned int DC_ATTR_UNUSED n2 = array_uint16_le (data + offset + 4 + i * 2); 552 unsigned int DC_ATTR_UNUSED he = array_uint16_le (data + offset + 36 + i * 2); 553 } 554 } else if (type == ID_GPS) { 555 int longitude = (signed int) array_uint32_le (data + offset + 4); 556 int latitude = (signed int) array_uint32_le (data + offset + 8); 557 sample.location.latitude = latitude / 1000000.0; 558 sample.location.longitude = longitude / 1000000.0; 559 sample.location.altitude = 0.0; 560 if (callback) callback (DC_SAMPLE_LOCATION, &sample, userdata); 561 } else if (type == ID_PO2_BOARD) { 562 unsigned int DC_ATTR_UNUSED serial = array_uint16_le (data + offset + 6); 563 for (unsigned int i = 0; i < 3; ++i) { 564 unsigned int ppo2 = data[offset + 2 + i]; 565 sample.ppo2.sensor = i; 566 sample.ppo2.value = ppo2 / 100.0; 567 if (callback) callback(DC_SAMPLE_PPO2, &sample, userdata); 568 } 569 } else if (type == ID_DECO) { 570 unsigned int ndt = data[offset + 2]; 571 unsigned int ceiling = data[offset + 3]; 572 unsigned int cns = data[offset + 4]; 573 unsigned int DC_ATTR_UNUSED safetystop = data[offset + 5]; 574 unsigned int DC_ATTR_UNUSED ceiling_max = array_uint16_le (data + offset + 6); 575 unsigned int tts = array_uint16_le (data + offset + 8); 576 unsigned int DC_ATTR_UNUSED otu = array_uint16_le (data + offset + 10); 577 578 // Deco / NDL 579 if (ceiling) { 580 sample.deco.type = DC_DECO_DECOSTOP; 581 sample.deco.time = 0; 582 sample.deco.depth = ceiling; 583 } else { 584 sample.deco.type = DC_DECO_NDL; 585 sample.deco.time = ndt * 60; 586 sample.deco.depth = 0.0; 587 } 588 sample.deco.tts = tts; 589 if (callback) callback(DC_SAMPLE_DECO, &sample, userdata); 590 591 sample.cns = cns / 100.0; 592 if (callback) callback(DC_SAMPLE_CNS, &sample, userdata); 593 } else if (type == ID_GF) { 594 if (gf_lo == UNDEFINED && gf_hi == UNDEFINED) { 595 gf_lo = data[offset + 2]; 596 gf_hi = data[offset + 3]; 597 } else { 598 WARNING (abstract->context, "Multiple GF values present."); 599 } 600 } else if (type == ID_FOOTER) { 601 unsigned int DC_ATTR_UNUSED cns = data[offset + 2]; 602 unsigned int DC_ATTR_UNUSED violations = data[offset + 3]; 603 unsigned int DC_ATTR_UNUSED otu = array_uint16_le (data + offset + 4); 604 unsigned int DC_ATTR_UNUSED battery = array_uint16_le (data + offset + 6); 605 time_end = array_uint32_le(data + offset + 8); 606 unsigned int DC_ATTR_UNUSED desaturation = array_uint32_le (data + offset + 12); 607 } else if (type == ID_PO2_REBREATHER) { 608 for (unsigned int i = 0; i < 3; ++i) { 609 unsigned int ppo2 = data[offset + 2 + i]; 610 sample.ppo2.sensor = i; 611 sample.ppo2.value = ppo2 / 100.0; 612 if (callback) callback(DC_SAMPLE_PPO2, &sample, userdata); 613 } 614 unsigned int pressure = array_uint16_le (data + offset + 8); 615 unsigned int serial = array_uint16_le (data + offset + 10); 616 dc_tank_usage_t usage = DC_TANK_USAGE_NONE; 617 618 if (tank_id_previous != serial || 619 tank_usage_previous != usage) { 620 // Find the tank in the list. 621 unsigned int idx = 0; 622 while (idx < ntanks) { 623 if (tank[idx].id == serial && 624 tank[idx].usage == usage) 625 break; 626 idx++; 627 } 628 629 // Add a new tank if necessary. 630 if (idx >= ntanks) { 631 if (ntanks >= NTANKS) { 632 ERROR (abstract->context, "Maximum number of tanks reached."); 633 return DC_STATUS_NOMEMORY; 634 } 635 tank[ntanks].id = serial; 636 tank[ntanks].beginpressure = pressure; 637 tank[ntanks].endpressure = pressure; 638 tank[ntanks].gasmix = DC_GASMIX_UNKNOWN; 639 tank[ntanks].usage = usage; 640 ntanks++; 641 } 642 643 tank_id_previous = serial; 644 tank_usage_previous = usage; 645 tank_idx = idx; 646 } 647 tank[tank_idx].endpressure = pressure; 648 649 sample.pressure.tank = tank_idx; 650 sample.pressure.value = pressure / 10.0; 651 if (callback) callback(DC_SAMPLE_PRESSURE, &sample, userdata); 652 } else if (type == ID_COMPASS) { 653 unsigned int heading = array_uint16_le (data + offset + 2); 654 sample.bearing = heading; 655 if (callback) callback(DC_SAMPLE_BEARING, &sample, userdata); 656 } else if (type == ID_TRIM) { 657 int DC_ATTR_UNUSED trim = (signed int) data[offset + 2]; 658 } else if (type == ID_GAS_CONFIG) { 659 unsigned int id = data[offset + 2]; 660 unsigned int o2 = data[offset + 3]; 661 unsigned int he = data[offset + 4]; 662 if (o2 != 0 || he != 0) { 663 // Record every configured slot, including disabled ones, 664 // so a later ID_GAS_TRANSMITTER record can still resolve 665 // it instead of failing with DC_STATUS_DATAFORMAT. 666 unsigned int cfgidx = 0; 667 while (cfgidx < ngasconfig) { 668 if (id == gasconfig[cfgidx].id) 669 break; 670 cfgidx++; 671 } 672 if (cfgidx >= ngasconfig) { 673 if (ngasconfig >= NGASMIXES) { 674 ERROR (abstract->context, "Maximum number of gas mixes reached."); 675 return DC_STATUS_NOMEMORY; 676 } 677 gasconfig[ngasconfig].id = id; 678 gasconfig[ngasconfig].oxygen = o2; 679 gasconfig[ngasconfig].helium = he; 680 ngasconfig++; 681 } else if (gasconfig[cfgidx].oxygen != o2 || 682 gasconfig[cfgidx].helium != he) { 683 ERROR (abstract->context, "Gas mix %u changed (%u/%u -> %u/%u).", 684 gasconfig[cfgidx].id, 685 gasconfig[cfgidx].oxygen, gasconfig[cfgidx].helium, 686 o2, he); 687 return DC_STATUS_DATAFORMAT; 688 } 689 } 690 if ((o2 != 0 || he != 0) && 691 (id >= NGAS_SLOTS || data[offset + 5] != 0)) { 692 unsigned int idx = 0; 693 while (idx < ngasmixes) { 694 if (id == gasmix[idx].id) 695 break; 696 idx++; 697 } 698 if (idx >= ngasmixes) { 699 if (ngasmixes >= NGASMIXES) { 700 ERROR (abstract->context, "Maximum number of gas mixes reached."); 701 return DC_STATUS_NOMEMORY; 702 } 703 gasmix[ngasmixes].id = id; 704 gasmix[ngasmixes].oxygen = o2; 705 gasmix[ngasmixes].helium = he; 706 ngasmixes++; 707 } else { 708 if (gasmix[idx].oxygen != o2 || 709 gasmix[idx].helium != he) { 710 ERROR (abstract->context, "Gas mix %u changed (%u/%u -> %u/%u).", 711 gasmix[idx].id, 712 gasmix[idx].oxygen, gasmix[idx].helium, 713 o2, he); 714 return DC_STATUS_DATAFORMAT; 715 } 716 717 sample.gasmix = idx; 718 if (callback) callback(DC_SAMPLE_GASMIX, &sample, userdata); 719 } 720 } 721 } else if (type == ID_TANK_TRANSMITTER) { 722 unsigned int id = data[offset + 2] | TRANSMITTER_ID; 723 unsigned int DC_ATTR_UNUSED battery = array_uint16_le (data + offset + 4); 724 unsigned int pressure = array_uint16_le (data + offset + 6) / 10; 725 dc_tank_usage_t usage = DC_TANK_USAGE_NONE; 726 727 if (tank_id_previous != id || 728 tank_usage_previous != usage) { 729 // Find the tank in the list. 730 unsigned int idx = 0; 731 while (idx < ntanks) { 732 if (tank[idx].id == id && 733 tank[idx].usage == usage) 734 break; 735 idx++; 736 } 737 738 // Add a new tank if necessary. 739 if (idx >= ntanks) { 740 if (ntanks >= NTANKS) { 741 ERROR (abstract->context, "Maximum number of tanks reached."); 742 return DC_STATUS_NOMEMORY; 743 } 744 tank[ntanks].id = id; 745 tank[ntanks].beginpressure = pressure; 746 tank[ntanks].endpressure = pressure; 747 tank[ntanks].gasmix = DC_GASMIX_UNKNOWN; 748 tank[ntanks].usage = usage; 749 ntanks++; 750 } 751 752 tank_id_previous = id; 753 tank_usage_previous = usage; 754 tank_idx = idx; 755 } 756 tank[tank_idx].endpressure = pressure; 757 758 sample.pressure.tank = tank_idx; 759 sample.pressure.value = pressure / 10.0; 760 if (callback) callback(DC_SAMPLE_PRESSURE, &sample, userdata); 761 } else if (type == ID_GF_INFO || type == ID_GF_DATA) { 762 unsigned int DC_ATTR_UNUSED gf_now = array_uint16_le (data + offset + 2); 763 unsigned int DC_ATTR_UNUSED gf_surface = array_uint16_le (data + offset + 4); 764 if (type == ID_GF_DATA) { 765 unsigned int DC_ATTR_UNUSED leading_tissue_gf_now = data[offset + 6]; 766 unsigned int DC_ATTR_UNUSED leading_tissue_gf_surface = data[offset + 7]; 767 } 768 } else if (type == ID_SGC) { 769 unsigned int DC_ATTR_UNUSED sgc = array_uint16_le (data + offset + 2); 770 } else { 771 WARNING (abstract->context, "Unknown record (type=%u, size=%u)", type, length); 772 } 773 774 offset += length; 775 } 776 777 parser->cached = 1; 778 parser->logversion = logversion; 779 parser->datetime = time_start; 780 parser->timezone = timezone; 781 if (time_start != UNDEFINED && time_end != UNDEFINED) { 782 parser->divetime = time_end - time_start; 783 } else { 784 parser->divetime = time; 785 } 786 parser->maxdepth = maxdepth; 787 parser->divemode = divemode; 788 parser->atmospheric = atmospheric; 789 parser->gf_lo = gf_lo; 790 parser->gf_hi = gf_hi; 791 parser->ngasmixes = ngasmixes; 792 parser->ntanks = ntanks; 793 for (unsigned int i = 0; i < NGASMIXES; ++i) { 794 parser->gasmix[i] = gasmix[i]; 795 } 796 for (unsigned int i = 0; i < NTANKS; ++i) { 797 parser->tank[i] = tank[i]; 798 } 799 800 return DC_STATUS_SUCCESS; 801 }