shearwater_predator_parser.c (49212B)
1 /* 2 * libdivecomputer 3 * 4 * Copyright (C) 2012 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 #include <stdbool.h> 25 26 #include <libdivecomputer/units.h> 27 28 #include "shearwater_predator.h" 29 #include "shearwater_petrel.h" 30 #include "context-private.h" 31 #include "parser-private.h" 32 #include "platform.h" 33 #include "array.h" 34 #include "field-cache.h" 35 36 #define ISINSTANCE(parser) ( \ 37 dc_parser_isinstance((parser), &shearwater_predator_parser_vtable) || \ 38 dc_parser_isinstance((parser), &shearwater_petrel_parser_vtable)) 39 40 #define LOG_RECORD_DIVE_SAMPLE 0x01 41 #define LOG_RECORD_FREEDIVE_SAMPLE 0x02 42 #define LOG_RECORD_AVELO_SAMPLE 0x03 43 #define LOG_RECORD_OPENING_0 0x10 44 #define LOG_RECORD_OPENING_1 0x11 45 #define LOG_RECORD_OPENING_2 0x12 46 #define LOG_RECORD_OPENING_3 0x13 47 #define LOG_RECORD_OPENING_4 0x14 48 #define LOG_RECORD_OPENING_5 0x15 49 #define LOG_RECORD_OPENING_6 0x16 50 #define LOG_RECORD_OPENING_7 0x17 51 #define LOG_RECORD_OPENING_8 0x18 52 #define LOG_RECORD_OPENING_9 0x19 53 #define LOG_RECORD_CLOSING_0 0x20 54 #define LOG_RECORD_CLOSING_1 0x21 55 #define LOG_RECORD_CLOSING_2 0x22 56 #define LOG_RECORD_CLOSING_3 0x23 57 #define LOG_RECORD_CLOSING_4 0x24 58 #define LOG_RECORD_CLOSING_5 0x25 59 #define LOG_RECORD_CLOSING_6 0x26 60 #define LOG_RECORD_CLOSING_7 0x27 61 #define LOG_RECORD_CLOSING_8 0x28 62 #define LOG_RECORD_CLOSING_9 0x29 63 #define LOG_RECORD_INFO_EVENT 0x30 64 #define LOG_RECORD_DIVE_SAMPLE_EXT 0xE1 65 #define LOG_RECORD_FINAL 0xFF 66 67 #define INFO_EVENT_TAG_LOG 38 68 69 #define SZ_BLOCK 0x80 70 #define SZ_SAMPLE_PREDATOR 0x10 71 #define SZ_SAMPLE_PETREL 0x20 72 #define SZ_SAMPLE_FREEDIVE 0x08 73 74 #define GASSWITCH 0x01 75 #define PPO2_EXTERNAL 0x02 76 #define SETPOINT_HIGH 0x04 77 #define SC 0x08 78 #define OC 0x10 79 #define SAMPLE_STATUS_BO_CCR 0x20 80 81 #define M_CC 0 82 #define M_OC_TEC 1 83 #define M_GAUGE 2 84 #define M_PPO2 3 85 #define M_SC 4 86 #define M_CC2 5 87 #define M_OC_REC 6 88 #define M_FREEDIVE 7 89 #define M_AVELO 12 90 91 #define SM_3_GAS_NX 0 92 #define SM_AIR 1 93 #define SM_NX 2 94 #define SM_OC_REC 3 95 96 #define AI_OFF 0 97 #define AI_HPCCR 4 98 #define AI_ON 5 99 #define AI_ON_GPS 6 100 101 #define GF 0 102 #define VPMB 1 103 #define VPMB_GFS 2 104 #define DCIEM 3 105 106 #define METRIC 0 107 #define IMPERIAL 1 108 109 #define NGASMIXES 20 110 #define NFIXED 10 111 #define NTANKS 6 112 #define NRECORDS 10 113 114 #define PREDATOR 2 115 #define PETREL 3 116 #define TERIC 8 117 118 #define SENSOR_CALIBRATION_DEFAULT 2100 119 120 #define GNSS_NOSAT 0 121 #define GNSS_NOFIX 1 122 #define GNSS_FIX_2D 2 123 #define GNSS_FIX_3D 3 124 #define GNSS_UNKNOWN 0xFF 125 #define GNSS_DISABLED 0x10 126 127 #define UNDEFINED 0xFFFFFFFF 128 129 typedef struct shearwater_predator_parser_t shearwater_predator_parser_t; 130 131 typedef struct shearwater_predator_gasmix_t { 132 unsigned int oxygen; 133 unsigned int helium; 134 unsigned int diluent; 135 unsigned int enabled; 136 unsigned int active; 137 } shearwater_predator_gasmix_t; 138 139 typedef struct shearwater_predator_tank_t { 140 unsigned int enabled; 141 unsigned int active; 142 unsigned int beginpressure; 143 unsigned int endpressure; 144 unsigned int pressure_max; 145 unsigned int pressure_reserve; 146 unsigned int serial; 147 char name[2]; 148 unsigned int battery; 149 dc_tank_usage_t usage; 150 } shearwater_predator_tank_t; 151 152 struct shearwater_predator_parser_t { 153 dc_parser_t base; 154 unsigned int model; 155 unsigned int petrel; 156 unsigned int samplesize; 157 // Cached fields. 158 unsigned int cached; 159 unsigned int pnf; 160 unsigned int logversion; 161 unsigned int headersize; 162 unsigned int footersize; 163 unsigned int opening[NRECORDS]; 164 unsigned int closing[NRECORDS]; 165 unsigned int final; 166 unsigned int nsamples; 167 unsigned int ngasmixes; 168 unsigned int ntanks; 169 shearwater_predator_gasmix_t gasmix[NGASMIXES]; 170 shearwater_predator_tank_t tank[NTANKS]; 171 unsigned int tankidx[NTANKS]; 172 unsigned int aimode; 173 unsigned int hpccr; 174 unsigned int calibrated; 175 double calibration[3]; 176 bool needs_divecan_calibration_estimate; 177 bool external_ppo2_used; 178 unsigned int divemode; 179 unsigned int serial; 180 unsigned int units; 181 unsigned int atmospheric; 182 unsigned int density; 183 184 /* Generic field cache */ 185 struct dc_field_cache cache; 186 }; 187 188 struct dc_parser_sensor_calibration_t { 189 double sum_ppo2; 190 double sum_calculated_ppo2; 191 unsigned int ppo2_sample_count; 192 }; 193 194 static dc_status_t shearwater_predator_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime); 195 static dc_status_t shearwater_predator_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value); 196 static dc_status_t shearwater_predator_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata); 197 static dc_status_t shearwater_predator_parser_destroy (dc_parser_t *abstract); 198 199 static dc_status_t shearwater_predator_parser_cache (shearwater_predator_parser_t *parser); 200 static dc_status_t shearwater_predator_parser_calibration_prepass (shearwater_predator_parser_t *parser, struct dc_parser_sensor_calibration_t *calibration); 201 static dc_status_t shearwater_predator_parser_samples_foreach_internal (shearwater_predator_parser_t *parser, dc_sample_callback_t callback, void *userdata, struct dc_parser_sensor_calibration_t *calibration); 202 203 static const dc_parser_vtable_t shearwater_predator_parser_vtable = { 204 sizeof(shearwater_predator_parser_t), 205 DC_FAMILY_SHEARWATER_PREDATOR, 206 NULL, /* set_clock */ 207 NULL, /* set_atmospheric */ 208 NULL, /* set_density */ 209 shearwater_predator_parser_get_datetime, /* datetime */ 210 shearwater_predator_parser_get_field, /* fields */ 211 shearwater_predator_parser_samples_foreach, /* samples_foreach */ 212 shearwater_predator_parser_destroy /* destroy */ 213 }; 214 215 static const dc_parser_vtable_t shearwater_petrel_parser_vtable = { 216 sizeof(shearwater_predator_parser_t), 217 DC_FAMILY_SHEARWATER_PETREL, 218 NULL, /* set_clock */ 219 NULL, /* set_atmospheric */ 220 NULL, /* set_density */ 221 shearwater_predator_parser_get_datetime, /* datetime */ 222 shearwater_predator_parser_get_field, /* fields */ 223 shearwater_predator_parser_samples_foreach, /* samples_foreach */ 224 shearwater_predator_parser_destroy /* destroy */ 225 }; 226 227 228 static unsigned int 229 shearwater_predator_is_ccr (unsigned int divemode) 230 { 231 return divemode == M_CC || divemode == M_CC2 || divemode == M_SC; 232 } 233 234 static unsigned int 235 shearwater_predator_find_gasmix (shearwater_predator_parser_t *parser, unsigned int o2, unsigned int he, unsigned int dil) 236 { 237 unsigned int i = 0; 238 while (i < parser->ngasmixes) { 239 if (o2 == parser->gasmix[i].oxygen && he == parser->gasmix[i].helium && dil == parser->gasmix[i].diluent) 240 break; 241 i++; 242 } 243 244 return i; 245 } 246 247 248 static dc_status_t 249 shearwater_predator_parser_destroy (dc_parser_t *abstract) 250 { 251 shearwater_predator_parser_t *parser = (shearwater_predator_parser_t *) abstract; 252 253 dc_field_cache_free (&parser->cache); 254 255 return DC_STATUS_SUCCESS; 256 } 257 258 259 static dc_status_t 260 shearwater_common_parser_create (dc_parser_t **out, dc_context_t *context, const unsigned char data[], size_t size, unsigned int model, unsigned int petrel, unsigned int serial) 261 { 262 shearwater_predator_parser_t *parser = NULL; 263 const dc_parser_vtable_t *vtable = NULL; 264 unsigned int samplesize = 0; 265 266 if (out == NULL) 267 return DC_STATUS_INVALIDARGS; 268 269 if (petrel) { 270 vtable = &shearwater_petrel_parser_vtable; 271 samplesize = SZ_SAMPLE_PETREL; 272 } else { 273 vtable = &shearwater_predator_parser_vtable; 274 samplesize = SZ_SAMPLE_PREDATOR; 275 } 276 277 // Allocate memory. 278 parser = (shearwater_predator_parser_t *) dc_parser_allocate (context, vtable, data, size); 279 if (parser == NULL) { 280 ERROR (context, "Failed to allocate memory."); 281 return DC_STATUS_NOMEMORY; 282 } 283 284 memset(&parser->cache, 0, sizeof(parser->cache)); 285 286 // Set the default values. 287 parser->model = model; 288 parser->petrel = petrel; 289 parser->samplesize = samplesize; 290 parser->serial = serial; 291 292 // Set the default values. 293 parser->cached = 0; 294 parser->pnf = 0; 295 parser->logversion = 0; 296 parser->headersize = 0; 297 parser->footersize = 0; 298 for (unsigned int i = 0; i < NRECORDS; ++i) { 299 parser->opening[i] = UNDEFINED; 300 parser->closing[i] = UNDEFINED; 301 } 302 parser->final = UNDEFINED; 303 parser->ngasmixes = 0; 304 for (unsigned int i = 0; i < NGASMIXES; ++i) { 305 parser->gasmix[i].oxygen = 0; 306 parser->gasmix[i].helium = 0; 307 parser->gasmix[i].diluent = 0; 308 parser->gasmix[i].enabled = 0; 309 parser->gasmix[i].active = 0; 310 } 311 parser->ntanks = 0; 312 for (unsigned int i = 0; i < NTANKS; ++i) { 313 parser->tank[i].enabled = 0; 314 parser->tank[i].active = 0; 315 parser->tank[i].beginpressure = 0; 316 parser->tank[i].endpressure = 0; 317 parser->tank[i].pressure_max = 0; 318 parser->tank[i].pressure_reserve = 0; 319 parser->tank[i].serial = 0; 320 memset (parser->tank[i].name, 0, sizeof (parser->tank[i].name)); 321 parser->tank[i].battery = 0; 322 parser->tank[i].usage = DC_TANK_USAGE_NONE; 323 parser->tankidx[i] = i; 324 } 325 parser->aimode = AI_OFF; 326 parser->hpccr = 0; 327 parser->calibrated = 0; 328 for (unsigned int i = 0; i < 3; ++i) { 329 parser->calibration[i] = 0.0; 330 } 331 parser->needs_divecan_calibration_estimate = false; 332 parser->external_ppo2_used = false; 333 parser->units = METRIC; 334 parser->density = DEF_DENSITY_SALT; 335 parser->atmospheric = DEF_ATMOSPHERIC / (BAR / 1000); 336 337 DC_ASSIGN_FIELD(parser->cache, DIVEMODE, DC_DIVEMODE_OC); 338 339 *out = (dc_parser_t *) parser; 340 341 return DC_STATUS_SUCCESS; 342 } 343 344 345 dc_status_t 346 shearwater_predator_parser_create (dc_parser_t **out, dc_context_t *context, const unsigned char data[], size_t size, unsigned int model, unsigned int serial) 347 { 348 return shearwater_common_parser_create (out, context, data, size, model, 0, serial); 349 } 350 351 352 dc_status_t 353 shearwater_petrel_parser_create (dc_parser_t **out, dc_context_t *context, const unsigned char data[], size_t size, unsigned int model, unsigned int serial) 354 { 355 return shearwater_common_parser_create (out, context, data, size, model, 1, serial); 356 } 357 358 359 static dc_status_t 360 shearwater_predator_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime) 361 { 362 shearwater_predator_parser_t *parser = (shearwater_predator_parser_t *) abstract; 363 const unsigned char *data = abstract->data; 364 365 // Cache the parser data. 366 dc_status_t rc = shearwater_predator_parser_cache (parser); 367 if (rc != DC_STATUS_SUCCESS) 368 return rc; 369 370 unsigned int ticks = array_uint32_be (data + parser->opening[0] + 12); 371 372 if (!dc_datetime_gmtime (datetime, ticks)) 373 return DC_STATUS_DATAFORMAT; 374 375 if (parser->model == TERIC && parser->logversion >= 9 && parser->opening[5] != UNDEFINED) { 376 int utc_offset = (int) array_uint32_be (data + parser->opening[5] + 26); 377 int dst = data[parser->opening[5] + 30]; 378 datetime->timezone = utc_offset * 60 + dst * 3600; 379 } else { 380 datetime->timezone = DC_TIMEZONE_NONE; 381 } 382 383 return DC_STATUS_SUCCESS; 384 } 385 386 // Show the battery state 387 // 388 // NOTE! Right now it only shows the most serious bit 389 // but the code is set up so that we could perhaps 390 // indicate that the battery is on the edge (ie it 391 // reported both "normal" _and_ "warning" during the 392 // dive - maybe that would be a "starting to warn") 393 // 394 // We could also report unpaired and comm errors. 395 static void 396 add_battery_info(shearwater_predator_parser_t *parser, const char *desc, unsigned int state) 397 { 398 // We don't know what other state bits than 0-2 mean 399 state &= 7; 400 if (state >= 1 && state <= 7) { 401 static const char *states[8] = { 402 "", // 000 - No state bits, not used 403 "normal", // 001 - only normal 404 "critical", // 010 - only critical 405 "critical", // 011 - both normal and critical 406 "warning", // 100 - only warning 407 "warning", // 101 - normal and warning 408 "critical", // 110 - warning and critical 409 "critical", // 111 - normal, warning and critical 410 }; 411 dc_field_add_string(&parser->cache, desc, states[state]); 412 } 413 } 414 415 static void 416 add_deco_model(shearwater_predator_parser_t *parser, const unsigned char *data) 417 { 418 unsigned int idx_deco_model = parser->pnf ? parser->opening[2] + 18 : 67; 419 unsigned int idx_gfs = parser->pnf ? parser->opening[3] + 5 : 85; 420 421 switch (data[idx_deco_model]) { 422 case 0: 423 dc_field_add_string_fmt(&parser->cache, "Deco model", "GF %u/%u", data[4], data[5]); 424 break; 425 case 1: 426 dc_field_add_string_fmt(&parser->cache, "Deco model", "VPM-B +%u", data[idx_deco_model + 1]); 427 break; 428 case 2: 429 dc_field_add_string_fmt(&parser->cache, "Deco model", "VPM-B/GFS +%u %u%%", data[idx_deco_model + 1], data[idx_gfs]); 430 break; 431 default: 432 dc_field_add_string_fmt(&parser->cache, "Deco model", "Unknown model %d", data[idx_deco_model]); 433 } 434 } 435 436 static void 437 add_battery_type(shearwater_predator_parser_t *parser, const unsigned char *data) 438 { 439 if (parser->logversion < 7) 440 return; 441 442 unsigned int idx_battery_type = parser->pnf ? parser->opening[4] + 9 : 120; 443 switch (data[idx_battery_type]) { 444 case 1: 445 dc_field_add_string(&parser->cache, "Battery type", "1.5V Alkaline"); 446 break; 447 case 2: 448 dc_field_add_string(&parser->cache, "Battery type", "1.5V Lithium"); 449 break; 450 case 3: 451 dc_field_add_string(&parser->cache, "Battery type", "1.2V NiMH"); 452 break; 453 case 4: 454 dc_field_add_string(&parser->cache, "Battery type", "3.6V Saft"); 455 break; 456 case 5: 457 dc_field_add_string(&parser->cache, "Battery type", "3.7V Li-Ion"); 458 break; 459 default: 460 dc_field_add_string_fmt(&parser->cache, "Battery type", "unknown type %d", data[idx_battery_type]); 461 break; 462 } 463 } 464 465 static void add_sensor_state(shearwater_predator_parser_t *parser, bool external_ppo2_used) 466 { 467 if (shearwater_predator_is_ccr(parser->divemode)) { 468 dc_field_add_string(&parser->cache, "ppO2 data source", external_ppo2_used ? "external" : "internal"); 469 470 if (external_ppo2_used) 471 for (size_t i = 0; i < 3; ++i) { 472 if (parser->calibrated & (1 << i)) { 473 static const char *names[] = { 474 "Sensor 1 calibration [bar / V]", 475 "Sensor 2 calibration [bar / V]", 476 "Sensor 3 calibration [bar / V]", 477 }; 478 dc_field_add_string_fmt(&parser->cache, names[i], "%.2f", parser->calibration[i] * 1000); 479 } 480 } 481 } 482 } 483 484 static dc_status_t 485 shearwater_predator_parser_cache (shearwater_predator_parser_t *parser) 486 { 487 dc_parser_t *abstract = (dc_parser_t *) parser; 488 const unsigned char *data = parser->base.data; 489 unsigned int size = parser->base.size; 490 491 if (parser->cached) { 492 return DC_STATUS_SUCCESS; 493 } 494 dc_field_cache_free (&parser->cache); 495 memset(&parser->cache, 0, sizeof(parser->cache)); 496 497 // Log versions before 6 weren't reliably stored in the data, but 498 // 6 is also the oldest version that we assume in our code 499 unsigned int logversion = 0; 500 501 // Verify the minimum length. 502 if (size < 2) { 503 ERROR (abstract->context, "Invalid data length."); 504 return DC_STATUS_DATAFORMAT; 505 } 506 507 // The Petrel Native Format (PNF) is very similar to the legacy 508 // Predator and Predator-like format. The samples are simply offset 509 // by one (so we can use pnf as the offset). For the header and 510 // footer data, it's more complicated because of the new 32 byte 511 // block structure. 512 unsigned int pnf = parser->petrel ? array_uint16_be (data) != 0xFFFF : 0; 513 unsigned int headersize = 0; 514 unsigned int footersize = 0; 515 if (!pnf) { 516 // Opening and closing blocks. 517 headersize = SZ_BLOCK; 518 footersize = SZ_BLOCK; 519 if (size < headersize + footersize) { 520 ERROR (abstract->context, "Invalid data length."); 521 return DC_STATUS_DATAFORMAT; 522 } 523 524 // Adjust the footersize for the final block. 525 if (parser->petrel || array_uint16_be (data + size - footersize) == 0xFFFD) { 526 footersize += SZ_BLOCK; 527 if (size < headersize + footersize) { 528 ERROR (abstract->context, "Invalid data length."); 529 return DC_STATUS_DATAFORMAT; 530 } 531 532 parser->final = size - SZ_BLOCK; 533 } 534 535 // The Predator and Predator-like format have just one large 128 536 // byte opening and closing block. To minimize the differences 537 // with the PNF format, all record offsets are assigned the same 538 // value here. 539 for (unsigned int i = 0; i <= 4; ++i) { 540 parser->opening[i] = 0; 541 parser->closing[i] = size - footersize; 542 } 543 544 // Log version 545 logversion = data[127]; 546 } 547 548 // Default dive mode. 549 unsigned int divemode = M_OC_TEC; 550 551 // Get the gas mixes. 552 unsigned int ngasmixes = NFIXED; 553 shearwater_predator_gasmix_t gasmix[NGASMIXES] = {0}; 554 shearwater_predator_tank_t tank[NTANKS] = {0}; 555 unsigned int o2_previous = UNDEFINED, he_previous = UNDEFINED, dil_previous = UNDEFINED; 556 unsigned int aimode = AI_OFF; 557 unsigned int hpccr = 0; 558 if (!pnf) { 559 for (unsigned int i = 0; i < NFIXED; ++i) { 560 gasmix[i].oxygen = data[20 + i]; 561 gasmix[i].helium = data[30 + i]; 562 gasmix[i].diluent = i >= 5; 563 gasmix[i].enabled = 1; 564 } 565 } 566 567 unsigned int nsamples = 0; 568 unsigned int offset = headersize; 569 unsigned int length = size - footersize; 570 unsigned int sub_mode = SM_OC_REC; 571 unsigned int stack_time_total_s = 0; 572 unsigned int stack_time_remaining_s; 573 while (offset + parser->samplesize <= length) { 574 // Ignore empty samples. 575 if (array_isequal (data + offset, parser->samplesize, 0x00)) { 576 offset += parser->samplesize; 577 continue; 578 } 579 580 // Get the record type. 581 unsigned int type = pnf ? data[offset] : LOG_RECORD_DIVE_SAMPLE; 582 583 if (type == LOG_RECORD_DIVE_SAMPLE || 584 type == LOG_RECORD_AVELO_SAMPLE) { 585 // Number of samples. 586 nsamples++; 587 588 // Status flags. 589 unsigned int status = 0; 590 unsigned int ccr = 0; 591 if (type != LOG_RECORD_AVELO_SAMPLE) { 592 status = data[offset + 11 + pnf]; 593 ccr = (status & OC) == 0; 594 } 595 if (ccr) { 596 divemode = status & SC ? M_SC : M_CC; 597 } 598 599 // Gaschange. 600 unsigned int o2 = data[offset + 7 + pnf]; 601 unsigned int he = data[offset + 8 + pnf]; 602 if ((o2 != o2_previous || he != he_previous || ccr != dil_previous) && 603 (o2 != 0 || he != 0)) { 604 // Find the gasmix in the list. 605 unsigned int idx = 0; 606 while (idx < ngasmixes) { 607 if (o2 == gasmix[idx].oxygen && he == gasmix[idx].helium && ccr == gasmix[idx].diluent) 608 break; 609 idx++; 610 } 611 612 // Add it to list if not found. 613 if (idx >= ngasmixes) { 614 if (idx >= NGASMIXES) { 615 ERROR (abstract->context, "Maximum number of gas mixes reached."); 616 return DC_STATUS_NOMEMORY; 617 } 618 gasmix[idx].oxygen = o2; 619 gasmix[idx].helium = he; 620 gasmix[idx].diluent = ccr; 621 ngasmixes = idx + 1; 622 } 623 624 gasmix[idx].active = 1; 625 626 o2_previous = o2; 627 he_previous = he; 628 dil_previous = ccr; 629 } 630 631 // Tank pressure 632 if (logversion >= 7) { 633 const unsigned int idx[2] = {27, 19}; 634 const unsigned int count = type == LOG_RECORD_AVELO_SAMPLE ? 1 : 2; 635 for (unsigned int i = 0; i < count; ++i) { 636 // Values above 0xFFF0 are special codes: 637 // 0xFFFF AI is off 638 // 0xFFFE No comms for 90 seconds+ 639 // 0xFFFD No comms for 30 seconds 640 // 0xFFFC Transmitter not paired 641 // For regular values, the top 4 bits contain the battery 642 // level (0=normal, 1=critical, 2=warning), and the lower 12 643 // bits the tank pressure in units of 2 psi. 644 unsigned int pressure = array_uint16_be (data + offset + pnf + idx[i]); 645 unsigned int id = (aimode == AI_HPCCR ? 4 : 0) + i; 646 if (pressure < 0xFFF0) { 647 unsigned int battery = 1u << (pressure >> 12); 648 pressure &= 0x0FFF; 649 if (pressure) { 650 if (!tank[id].active) { 651 tank[id].active = 1; 652 tank[id].beginpressure = pressure; 653 tank[id].endpressure = pressure; 654 } 655 tank[id].endpressure = pressure; 656 tank[id].battery |= battery; 657 } 658 } 659 } 660 } 661 } else if (type == LOG_RECORD_DIVE_SAMPLE_EXT) { 662 // Tank pressure 663 if (logversion >= 13) { 664 for (unsigned int i = 0; i < 2; ++i) { 665 unsigned int pressure = array_uint16_be (data + offset + pnf + i * 2); 666 unsigned int id = 2 + i; 667 if (pressure < 0xFFF0) { 668 pressure &= 0x0FFF; 669 if (pressure) { 670 if (!tank[id].active) { 671 tank[id].active = 1; 672 tank[id].beginpressure = pressure; 673 tank[id].endpressure = pressure; 674 } 675 tank[id].endpressure = pressure; 676 } 677 } 678 } 679 } 680 // Tank pressure (HP CCR) 681 if (logversion >= 14) { 682 for (unsigned int i = 0; i < 2; ++i) { 683 unsigned int pressure = array_uint16_be (data + offset + pnf + 4 + i * 2); 684 unsigned int id = 4 + i; 685 if (pressure) { 686 if (!tank[id].active) { 687 tank[id].active = 1; 688 tank[id].enabled = 1; 689 tank[id].beginpressure = pressure; 690 tank[id].endpressure = pressure; 691 //tank[id].usage = i == 0 ? DC_USAGE_DILUENT : DC_USAGE_OXYGEN; //TODO: Link this tank to the correct gasmix 692 hpccr = 1; 693 } 694 tank[id].endpressure = pressure; 695 } 696 } 697 } 698 } else if (type == LOG_RECORD_FREEDIVE_SAMPLE) { 699 // Freedive record 700 divemode = M_FREEDIVE; 701 } else if (type >= LOG_RECORD_OPENING_0 && type <= LOG_RECORD_OPENING_9) { 702 // Opening record 703 parser->opening[type - LOG_RECORD_OPENING_0] = offset; 704 705 if (type == LOG_RECORD_OPENING_0) { 706 for (unsigned int i = 0; i < NFIXED; ++i) { 707 gasmix[i].oxygen = data[offset + 20 + i]; 708 gasmix[i].diluent = i >= 5; 709 } 710 for (unsigned int i = 0; i < 2; ++i) { 711 gasmix[i].helium = data[offset + 30 + i]; 712 } 713 } else if (type == LOG_RECORD_OPENING_1) { 714 for (unsigned int i = 2; i < NFIXED; ++i) { 715 gasmix[i].helium = data[offset + 1 + i - 2]; 716 } 717 } else if (type == LOG_RECORD_OPENING_4) { 718 // Log version 719 logversion = data[offset + 16]; 720 721 // Air integration mode 722 if (logversion >= 7) { 723 aimode = data[offset + 28]; 724 if (logversion < 13) { 725 if (aimode == 1 || aimode == 2) { 726 tank[aimode - 1].enabled = 1; 727 } else if (aimode == 3) { 728 tank[0].enabled = 1; 729 tank[1].enabled = 1; 730 } 731 } 732 if (logversion < 14) { 733 if (aimode == AI_HPCCR) { 734 for (unsigned int i = 0; i < 2; ++i) { 735 tank[4 + i].enabled = 1; 736 //tank[4 + i].usage = i == 0 ? DC_USAGE_DILUENT : DC_USAGE_OXYGEN; //TODO: Link this tank to the correct gasmix 737 } 738 hpccr = 1; 739 } 740 } 741 } 742 743 // Gas mix on/off state. 744 unsigned int state = array_uint16_be (data + offset + 17); 745 for (unsigned int i = 0; i < NFIXED; ++i) { 746 gasmix[i].enabled = (state & (1 << i)) != 0; 747 } 748 749 // GTR (Gas Time Remaining) mode, used here only to detect 750 // sidemount tanks. This byte immediately follows the AI 751 // mode byte and, like it, requires the wireless air 752 // integration transmitters, so it is only meaningful once 753 // AI support appears (logversion >= 7). Before log version 754 // 13 the only defined values were 0/1/2 (off/T1/T2), which 755 // never satisfy the sidemount popcount check below; from 756 // version 13 onward, multiple transmitter bits can be 757 // combined to flag sidemount configurations. 758 if (logversion >= 7) { 759 unsigned int gtrmode = data[offset + 29]; 760 if (popcount(gtrmode) >= 2) { 761 for (unsigned int i = 0; i < 4; ++i) { 762 if (gtrmode & (1 << i)) { 763 tank[i].usage = DC_TANK_USAGE_SIDEMOUNT; 764 } 765 } 766 } 767 } 768 } else if (type == LOG_RECORD_OPENING_5) { 769 if (logversion >= 9) { 770 tank[0].serial = array_convert_bcd2dec (data + offset + 1, 3); 771 tank[0].pressure_max = array_uint16_be(data + offset + 6); 772 tank[0].pressure_reserve = array_uint16_be(data + offset + 8); 773 774 tank[1].serial = array_convert_bcd2dec(data + offset + 10, 3); 775 tank[1].pressure_max = array_uint16_be(data + offset + 15); 776 tank[1].pressure_reserve = array_uint16_be(data + offset + 17); 777 } 778 } else if (type == LOG_RECORD_OPENING_6) { 779 if (logversion >= 11) { 780 stack_time_total_s = array_uint16_be(data + offset + 1); 781 if (stack_time_total_s > 0) { 782 dc_field_add_string_fmt(&parser->cache, "Configured stack time [hh:mm:ss]", "%d:%02d:%02d", stack_time_total_s / 3600, (stack_time_total_s / 60) % 60, stack_time_total_s % 60); 783 stack_time_remaining_s = array_uint16_be(data + offset + 3); 784 dc_field_add_string_fmt(&parser->cache, "Remaining stack time at start [hh:mm:ss]", "%d:%02d:%02d", stack_time_remaining_s / 3600, (stack_time_remaining_s / 60) % 60, stack_time_remaining_s % 60); 785 } 786 } 787 if (logversion >= 12) { 788 if (divemode == M_OC_REC) { 789 sub_mode = data[offset + 5]; 790 } 791 } 792 if (logversion >= 13) { 793 tank[0].enabled = data[offset + 19]; 794 memcpy (tank[0].name, data + offset + 20, sizeof (tank[0].name)); 795 796 tank[1].enabled = data[offset + 22]; 797 memcpy (tank[1].name, data + offset + 23, sizeof (tank[1].name)); 798 799 tank[2].serial = array_convert_bcd2dec(data + offset + 25, 3); 800 tank[2].pressure_max = array_uint16_be(data + offset + 28); 801 tank[2].pressure_reserve = array_uint16_be(data + offset + 30); 802 } 803 } else if (type == LOG_RECORD_OPENING_7) { 804 if (logversion >= 13) { 805 tank[2].enabled = data[offset + 1]; 806 memcpy (tank[2].name, data + offset + 2, sizeof (tank[2].name)); 807 808 tank[3].serial = array_convert_bcd2dec(data + offset + 4, 3); 809 tank[3].pressure_max = array_uint16_be(data + offset + 7); 810 tank[3].pressure_reserve = array_uint16_be(data + offset + 9); 811 tank[3].enabled = data[offset + 11]; 812 memcpy (tank[3].name, data + offset + 12, sizeof (tank[3].name)); 813 } 814 } 815 } else if (type >= LOG_RECORD_CLOSING_0 && type <= LOG_RECORD_CLOSING_9) { 816 if (type == LOG_RECORD_CLOSING_6) { 817 if (logversion >= 11) { 818 if (stack_time_total_s > 0) { 819 stack_time_remaining_s = array_uint16_be(data + offset + 3); 820 dc_field_add_string_fmt(&parser->cache, "Remaining stack time at end [hh:mm:ss]", "%d:%02d:%02d", stack_time_remaining_s / 3600, (stack_time_remaining_s / 60) % 60, stack_time_remaining_s % 60); 821 } 822 } 823 } 824 // Closing record 825 parser->closing[type - LOG_RECORD_CLOSING_0] = offset; 826 } else if (type == LOG_RECORD_FINAL) { 827 // Final record 828 parser->final = offset; 829 } 830 831 offset += parser->samplesize; 832 } 833 834 // Verify the required opening/closing records. 835 // At least in firmware v71 and newer, Petrel and Petrel 2 also use PNF, 836 // and there opening/closing record 5 (which contains AI information plus 837 // the sample interval) don't appear to exist - so don't mark them as required 838 for (unsigned int i = 0; i <= 4; ++i) { 839 if (parser->opening[i] == UNDEFINED || parser->closing[i] == UNDEFINED) { 840 ERROR (abstract->context, "Opening or closing record %u not found.", i); 841 return DC_STATUS_DATAFORMAT; 842 } 843 } 844 845 dc_field_add_string_fmt(&parser->cache, "Logversion", "%d%s", logversion, pnf ? "(PNF)" : ""); 846 847 // Get the dive mode from the header (if available). 848 if (logversion >= 8) { 849 divemode = data[parser->opening[4] + (pnf ? 1 : 112)]; 850 } 851 852 if (divemode == M_CC || divemode == M_CC2 || divemode == M_SC) { 853 // Cache sensor calibration for later use 854 unsigned int base = parser->opening[3] + (pnf ? 6 : 86); 855 parser->calibrated = data[base]; 856 857 unsigned int calibration_count = 0; 858 unsigned int calibration_default_count = 0; 859 for (size_t i = 0; i < 3; ++i) { 860 if (parser->calibrated & (1 << i)) { 861 unsigned int calibration = array_uint16_be(data + base + 1 + i * 2); 862 863 calibration_count++; 864 if (calibration == SENSOR_CALIBRATION_DEFAULT) { 865 calibration_default_count++; 866 } 867 868 parser->calibration[i] = calibration / 100000.0; 869 if (parser->model == PREDATOR) { 870 // The Predator expects the mV output of the cells to be 871 // within 30mV to 70mV in 100% O2 at 1 atmosphere. If the 872 // calibration value is scaled with a factor 2.2, then the 873 // sensors lines up and matches the average. 874 parser->calibration[i] *= 2.2; 875 } 876 } 877 } 878 879 if (calibration_count > 0) { 880 if (calibration_default_count == calibration_count) { 881 // All calibrated sensors report the default calibration value 882 // so this could be a DiveCAN controller, where the calibration values 883 // are stored in the CCR's sensor module. 884 parser->needs_divecan_calibration_estimate = true; 885 } 886 } 887 } 888 889 // Get the correct model number from the final block. 890 // Product-Version byte from the device's final record — authoritative for 891 // the coarse product family. This value is not the FWID; stored logs do not 892 // carry the FWID, so sub-model distinctions (e.g. Petrel 1 vs Petrel 2) are 893 // not recoverable on re-parse. Consumer-side refinement uses the FWID 894 // exposed via devinfo.hw_id on live download. 895 if (parser->final != UNDEFINED) { 896 parser->model = data[parser->final + 13]; 897 DEBUG (abstract->context, "Device: model=%u, serial=%u, firmware=%u", 898 data[parser->final + 13], 899 array_uint32_be (data + parser->final + 2), 900 bcd2dec (data[parser->final + 10])); 901 } 902 903 // Fix the Teric tank serial number. 904 if (parser->model == TERIC) { 905 for (unsigned int i = 0; i < NTANKS; ++i) { 906 tank[i].serial = 907 ((tank[i].serial / 10000) % 100) + 908 ((tank[i].serial / 100) % 100) * 100 + 909 ((tank[i].serial ) % 100) * 10000; 910 } 911 } 912 913 // Cache the data for later use. 914 parser->pnf = pnf; 915 parser->logversion = logversion; 916 parser->headersize = headersize; 917 parser->footersize = footersize; 918 parser->ngasmixes = 0; 919 if (divemode != M_FREEDIVE) { 920 for (unsigned int i = 0; i < ngasmixes; ++i) { 921 if (gasmix[i].oxygen == 0 && gasmix[i].helium == 0) 922 continue; 923 if (!gasmix[i].enabled && !gasmix[i].active) 924 continue; 925 if (gasmix[i].diluent && !shearwater_predator_is_ccr (divemode)) 926 continue; 927 parser->gasmix[parser->ngasmixes] = gasmix[i]; 928 parser->ngasmixes++; 929 } 930 } 931 parser->ntanks = 0; 932 for (unsigned int i = 0; i < NTANKS; ++i) { 933 if (tank[i].active) { 934 parser->tankidx[i] = parser->ntanks; 935 parser->tank[parser->ntanks] = tank[i]; 936 parser->ntanks++; 937 } else { 938 parser->tankidx[i] = UNDEFINED; 939 } 940 } 941 parser->nsamples = nsamples; 942 parser->aimode = aimode; 943 parser->hpccr = hpccr; 944 parser->divemode = divemode; 945 parser->units = data[parser->opening[0] + 8]; 946 parser->atmospheric = array_uint16_be (data + parser->opening[1] + (parser->pnf ? 16 : 47)); 947 parser->density = array_uint16_be (data + parser->opening[3] + (parser->pnf ? 3 : 83)); 948 parser->cached = 1; 949 950 dc_field_add_string_fmt(&parser->cache, "Serial", "%08x", parser->serial); 951 // bytes 1-31 are identical in all formats 952 dc_field_add_string_fmt(&parser->cache, STRING_KEY_FIRMWARE_VERSION, "%u", bcd2dec(data[19])); 953 add_deco_model(parser, data); 954 add_battery_type(parser, data); 955 dc_field_add_string_fmt(&parser->cache, "Battery at end", "%.1f V", data[9] / 10.0); 956 add_battery_info(parser, "T1 battery", tank[0].battery); 957 add_battery_info(parser, "T2 battery", tank[1].battery); 958 959 switch (divemode) { 960 case M_CC: 961 case M_CC2: 962 DC_ASSIGN_FIELD(parser->cache, DIVEMODE, DC_DIVEMODE_CCR); 963 break; 964 case M_SC: 965 DC_ASSIGN_FIELD(parser->cache, DIVEMODE, DC_DIVEMODE_SCR); 966 break; 967 case M_OC_TEC: 968 case M_OC_REC: 969 DC_ASSIGN_FIELD(parser->cache, DIVEMODE, DC_DIVEMODE_OC); 970 break; 971 case M_GAUGE: 972 case M_PPO2: 973 DC_ASSIGN_FIELD(parser->cache, DIVEMODE, DC_DIVEMODE_GAUGE); 974 break; 975 case M_FREEDIVE: 976 DC_ASSIGN_FIELD(parser->cache, DIVEMODE, DC_DIVEMODE_FREEDIVE); 977 break; 978 default: 979 break; 980 } 981 982 struct dc_parser_sensor_calibration_t userdata = { 0 }; 983 dc_status_t rc = DC_STATUS_SUCCESS; 984 if (shearwater_predator_is_ccr(divemode)) 985 rc = shearwater_predator_parser_calibration_prepass(parser, &userdata); 986 987 bool calibrated = false; 988 if (parser->needs_divecan_calibration_estimate) { 989 if (rc == DC_STATUS_SUCCESS && userdata.sum_ppo2 != 0) { 990 double calibration = userdata.sum_calculated_ppo2 / userdata.sum_ppo2; 991 if (calibration < 0.98 || calibration > 1.02) { 992 // The calibration scaling is significant, use it. 993 calibration *= SENSOR_CALIBRATION_DEFAULT / 100000.0; 994 parser->calibration[0] = calibration; 995 parser->calibration[1] = calibration; 996 parser->calibration[2] = calibration; 997 998 dc_field_add_string_fmt(&parser->cache, "Estimated (DiveCAN?) sensor calibration [bar / V]", "%.2f", calibration * 1000); 999 1000 calibrated = true; 1001 } 1002 } 1003 1004 parser->needs_divecan_calibration_estimate = false; 1005 } 1006 1007 if (!calibrated) { 1008 add_sensor_state(parser, parser->external_ppo2_used); 1009 } 1010 1011 static const char *name = "Divemode"; 1012 if (divemode == M_OC_REC) { 1013 static const char *oc_rec_modes[] = { 1014 "3 Gas Nitrox", 1015 "Air", 1016 "Nitrox", 1017 "OC Recreational", 1018 }; 1019 dc_field_add_string_fmt(&parser->cache, name, "%s", sub_mode <= SM_OC_REC ? oc_rec_modes[sub_mode] : "Unknown divemode"); 1020 } else { 1021 static const char *modes[] = { 1022 "CC / BO", 1023 "OC Technical", 1024 "Gauge", 1025 "PPO2 Display", 1026 "SC / BO", 1027 "CC / BO 2", 1028 "OC Recreational", 1029 "Freedive", 1030 }; 1031 dc_field_add_string_fmt(&parser->cache, name, "%s", divemode <= M_FREEDIVE ? modes[divemode] : "Unknown divemode"); 1032 } 1033 1034 return DC_STATUS_SUCCESS; 1035 } 1036 1037 static dc_status_t 1038 shearwater_predator_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value) 1039 { 1040 shearwater_predator_parser_t *parser = (shearwater_predator_parser_t *) abstract; 1041 1042 const unsigned char *data = abstract->data; 1043 1044 // Cache the parser data. 1045 dc_status_t rc = shearwater_predator_parser_cache (parser); 1046 if (rc != DC_STATUS_SUCCESS) 1047 return rc; 1048 1049 unsigned int decomodel_idx = parser->pnf ? parser->opening[2] + 18 : 67; 1050 unsigned int gf_idx = parser->pnf ? parser->opening[0] + 4 : 4; 1051 1052 dc_gasmix_t *gasmix = (dc_gasmix_t *) value; 1053 dc_tank_t *tank = (dc_tank_t *) value; 1054 dc_salinity_t *water = (dc_salinity_t *) value; 1055 dc_decomodel_t *decomodel = (dc_decomodel_t *) value; 1056 dc_location_t *location = (dc_location_t *) value; 1057 dc_field_string_t *string = (dc_field_string_t *) value; 1058 1059 if (value) { 1060 switch (type) { 1061 case DC_FIELD_DIVETIME: 1062 if (parser->pnf) 1063 *((unsigned int *) value) = array_uint24_be (data + parser->closing[0] + 6); 1064 else 1065 *((unsigned int *) value) = array_uint16_be (data + parser->closing[0] + 6) * 60; 1066 break; 1067 case DC_FIELD_MAXDEPTH: 1068 if (parser->units == IMPERIAL) 1069 *((double *) value) = array_uint16_be (data + parser->closing[0] + 4) * FEET; 1070 else 1071 *((double *) value) = array_uint16_be (data + parser->closing[0] + 4); 1072 if (parser->pnf) 1073 *((double *)value) /= 10.0; 1074 break; 1075 case DC_FIELD_GASMIX_COUNT: 1076 *((unsigned int *) value) = parser->ngasmixes; 1077 break; 1078 case DC_FIELD_GASMIX: 1079 gasmix->usage = parser->gasmix[flags].diluent ? DC_USAGE_DILUENT : DC_USAGE_OPEN_CIRCUIT; 1080 gasmix->oxygen = parser->gasmix[flags].oxygen / 100.0; 1081 gasmix->helium = parser->gasmix[flags].helium / 100.0; 1082 gasmix->nitrogen = 1.0 - gasmix->oxygen - gasmix->helium; 1083 break; 1084 case DC_FIELD_TANK_COUNT: 1085 *((unsigned int *) value) = parser->ntanks; 1086 break; 1087 case DC_FIELD_TANK: 1088 tank->type = DC_TANKVOLUME_NONE; 1089 tank->volume = 0.0; 1090 tank->workpressure = 0.0; 1091 tank->beginpressure = parser->tank[flags].beginpressure * 2 * PSI / BAR; 1092 tank->endpressure = parser->tank[flags].endpressure * 2 * PSI / BAR; 1093 tank->gasmix = DC_GASMIX_UNKNOWN; 1094 tank->usage = parser->tank[flags].usage; 1095 break; 1096 case DC_FIELD_SALINITY: 1097 if (parser->density == 1000) 1098 water->type = DC_WATER_FRESH; 1099 else 1100 water->type = DC_WATER_SALT; 1101 water->density = parser->density; 1102 break; 1103 case DC_FIELD_ATMOSPHERIC: 1104 *((double *) value) = parser->atmospheric / 1000.0; 1105 break; 1106 case DC_FIELD_DIVEMODE: 1107 switch (parser->divemode) { 1108 case M_CC: 1109 case M_CC2: 1110 *((dc_divemode_t *) value) = DC_DIVEMODE_CCR; 1111 break; 1112 case M_SC: 1113 *((dc_divemode_t *) value) = DC_DIVEMODE_SCR; 1114 break; 1115 case M_OC_TEC: 1116 case M_OC_REC: 1117 case M_AVELO: 1118 *((dc_divemode_t *) value) = DC_DIVEMODE_OC; 1119 break; 1120 case M_GAUGE: 1121 case M_PPO2: 1122 *((dc_divemode_t *) value) = DC_DIVEMODE_GAUGE; 1123 break; 1124 case M_FREEDIVE: 1125 *((dc_divemode_t *) value) = DC_DIVEMODE_FREEDIVE; 1126 break; 1127 default: 1128 return DC_STATUS_DATAFORMAT; 1129 } 1130 break; 1131 case DC_FIELD_DECOMODEL: 1132 switch (data[decomodel_idx]) { 1133 case GF: 1134 decomodel->type = DC_DECOMODEL_BUHLMANN; 1135 decomodel->conservatism = 0; 1136 decomodel->params.gf.low = data[gf_idx + 0]; 1137 decomodel->params.gf.high = data[gf_idx + 1]; 1138 break; 1139 case VPMB: 1140 case VPMB_GFS: 1141 decomodel->type = DC_DECOMODEL_VPM; 1142 decomodel->conservatism = data[decomodel_idx + 1]; 1143 break; 1144 case DCIEM: 1145 decomodel->type = DC_DECOMODEL_DCIEM; 1146 decomodel->conservatism = 0; 1147 break; 1148 default: 1149 return DC_STATUS_DATAFORMAT; 1150 } 1151 break; 1152 case DC_FIELD_LOCATION: 1153 if (parser->opening[9] == UNDEFINED || parser->aimode != AI_ON_GPS) 1154 return DC_STATUS_UNSUPPORTED; 1155 1156 { 1157 unsigned int gnss_status = data[parser->opening[9] + 16]; 1158 if (!(gnss_status == GNSS_FIX_2D || gnss_status == GNSS_FIX_3D)) 1159 return DC_STATUS_UNSUPPORTED; 1160 1161 signed int latitude = (signed int) array_uint32_be (data + parser->opening[9] + 21); 1162 signed int longitude = (signed int) array_uint32_be (data + parser->opening[9] + 25); 1163 location->latitude = latitude / 100000.0; 1164 location->longitude = longitude / 100000.0; 1165 location->altitude = 0.0; 1166 } 1167 break; 1168 case DC_FIELD_STRING: 1169 return dc_field_get_string(&parser->cache, flags, string); 1170 default: 1171 return DC_STATUS_UNSUPPORTED; 1172 } 1173 } 1174 1175 return DC_STATUS_SUCCESS; 1176 } 1177 1178 1179 // AI-generated (Claude) 1180 static dc_status_t 1181 shearwater_predator_parser_samples_foreach_internal (shearwater_predator_parser_t *parser, dc_sample_callback_t callback, void *userdata, struct dc_parser_sensor_calibration_t *calibration) 1182 { 1183 const unsigned char *data = parser->base.data; 1184 unsigned int size = parser->base.size; 1185 1186 // Previous gas mix. 1187 unsigned int o2_previous = UNDEFINED, he_previous = UNDEFINED, dil_previous = UNDEFINED; 1188 1189 // Sample interval. 1190 unsigned int time = 0; 1191 unsigned int interval = 10000; 1192 if (parser->pnf && parser->logversion >= 9 && parser->opening[5] != UNDEFINED) { 1193 interval = array_uint16_be (data + parser->opening[5] + 23); 1194 } 1195 1196 unsigned int nsamples = 0; 1197 unsigned int pnf = parser->pnf; 1198 unsigned int offset = parser->headersize; 1199 unsigned int length = size - parser->footersize; 1200 while (offset + parser->samplesize <= length) { 1201 dc_sample_value_t sample = {0}; 1202 1203 // Ignore empty samples. 1204 if (array_isequal (data + offset, parser->samplesize, 0x00)) { 1205 offset += parser->samplesize; 1206 continue; 1207 } 1208 1209 // Get the record type. 1210 unsigned int type = pnf ? data[offset] : LOG_RECORD_DIVE_SAMPLE; 1211 1212 if (type == LOG_RECORD_DIVE_SAMPLE || 1213 type == LOG_RECORD_AVELO_SAMPLE) { 1214 // Number of samples. 1215 nsamples++; 1216 1217 // Time (seconds). 1218 time += interval; 1219 sample.time = time; 1220 if (callback) callback (DC_SAMPLE_TIME, &sample, userdata); 1221 1222 // Depth (1/10 m or ft). 1223 unsigned int depth = array_uint16_be (data + pnf + offset); 1224 if (parser->units == IMPERIAL) 1225 sample.depth = depth * FEET / 10.0; 1226 else 1227 sample.depth = depth / 10.0; 1228 if (callback) callback (DC_SAMPLE_DEPTH, &sample, userdata); 1229 1230 // Temperature (°C or °F). 1231 int temperature = (signed char) data[offset + pnf + 13]; 1232 if (temperature < 0) { 1233 // Fix negative temperatures. 1234 temperature += 102; 1235 if (temperature > 0) { 1236 temperature = 0; 1237 } 1238 } 1239 if (parser->units == IMPERIAL) 1240 sample.temperature = (temperature - 32.0) * (5.0 / 9.0); 1241 else 1242 sample.temperature = temperature; 1243 if (callback) callback (DC_SAMPLE_TEMPERATURE, &sample, userdata); 1244 1245 // Status flags. 1246 unsigned int status = 0; 1247 unsigned int ccr = 0; 1248 if (type != LOG_RECORD_AVELO_SAMPLE) { 1249 status = data[offset + 11 + pnf]; 1250 ccr = (status & OC) == 0; 1251 } 1252 1253 if (ccr) { 1254 if ((status & PPO2_EXTERNAL) == 0) { 1255 double calculated_ppo2 = data[offset + pnf + 6] / 100.0; 1256 1257 if (calibration) { 1258 parser->external_ppo2_used = true; 1259 1260 if (parser->needs_divecan_calibration_estimate) { 1261 double ppo2_sum = 0.0; 1262 unsigned int ppo2_count = 0; 1263 if (parser->calibrated & 0x01) { 1264 ppo2_sum += data[offset + pnf + 12] * SENSOR_CALIBRATION_DEFAULT / 100000.0; 1265 ppo2_count++; 1266 } 1267 1268 if (parser->calibrated & 0x02) { 1269 ppo2_sum += data[offset + pnf + 14] * SENSOR_CALIBRATION_DEFAULT / 100000.0; 1270 ppo2_count++; 1271 } 1272 1273 if (parser->calibrated & 0x04) { 1274 ppo2_sum += data[offset + pnf + 15] * SENSOR_CALIBRATION_DEFAULT / 100000.0; 1275 ppo2_count++; 1276 } 1277 1278 double ppo2 = ppo2_sum / ppo2_count; 1279 1280 calibration->sum_ppo2 += ppo2; 1281 calibration->sum_calculated_ppo2 += calculated_ppo2; 1282 calibration->ppo2_sample_count++; 1283 } 1284 } 1285 1286 if ((status & SAMPLE_STATUS_BO_CCR) == 0 && callback) { 1287 sample.ppo2.sensor = DC_SENSOR_NONE; 1288 sample.ppo2.value = calculated_ppo2; 1289 callback(DC_SAMPLE_PPO2, &sample, userdata); 1290 1291 if (parser->calibrated & 0x01) { 1292 sample.ppo2.sensor = 0; 1293 sample.ppo2.value = data[offset + pnf + 12] * parser->calibration[0]; 1294 callback(DC_SAMPLE_PPO2, &sample, userdata); 1295 } 1296 1297 if (parser->calibrated & 0x02) { 1298 sample.ppo2.sensor = 1; 1299 sample.ppo2.value = data[offset + pnf + 14] * parser->calibration[1]; 1300 callback(DC_SAMPLE_PPO2, &sample, userdata); 1301 } 1302 1303 if (parser->calibrated & 0x04) { 1304 sample.ppo2.sensor = 2; 1305 sample.ppo2.value = data[offset + pnf + 15] * parser->calibration[2]; 1306 callback(DC_SAMPLE_PPO2, &sample, userdata); 1307 } 1308 } 1309 } 1310 1311 // Setpoint 1312 if (parser->petrel) { 1313 sample.setpoint = data[offset + pnf + 18] / 100.0; 1314 } else { 1315 // this will only ever be called for the actual Predator, so no adjustment needed for PNF 1316 if (status & SETPOINT_HIGH) { 1317 sample.setpoint = data[18] / 100.0; 1318 } else { 1319 sample.setpoint = data[17] / 100.0; 1320 } 1321 } 1322 if (callback) callback (DC_SAMPLE_SETPOINT, &sample, userdata); 1323 } 1324 1325 // CNS 1326 if (parser->petrel) { 1327 sample.cns = data[offset + pnf + 22] / 100.0; 1328 if (callback) callback (DC_SAMPLE_CNS, &sample, userdata); 1329 } 1330 1331 // Gaschange. 1332 unsigned int o2 = data[offset + pnf + 7]; 1333 unsigned int he = data[offset + pnf + 8]; 1334 if ((o2 != o2_previous || he != he_previous || ccr != dil_previous) && 1335 (o2 != 0 || he != 0)) { 1336 unsigned int idx = shearwater_predator_find_gasmix (parser, o2, he, ccr); 1337 if (idx >= parser->ngasmixes) { 1338 ERROR (parser->base.context, "Invalid gas mix."); 1339 return DC_STATUS_DATAFORMAT; 1340 } 1341 1342 sample.gasmix = idx; 1343 if (callback) callback (DC_SAMPLE_GASMIX, &sample, userdata); 1344 o2_previous = o2; 1345 he_previous = he; 1346 dil_previous = ccr; 1347 } 1348 1349 // Deco stop / NDL. 1350 unsigned int decostop = array_uint16_be (data + offset + pnf + 2); 1351 if (decostop) { 1352 sample.deco.type = DC_DECO_DECOSTOP; 1353 if (parser->units == IMPERIAL) 1354 sample.deco.depth = decostop * FEET; 1355 else 1356 sample.deco.depth = decostop; 1357 } else { 1358 sample.deco.type = DC_DECO_NDL; 1359 sample.deco.depth = 0.0; 1360 } 1361 sample.deco.time = data[offset + pnf + 9] * 60; 1362 sample.deco.tts = array_uint16_be (data + offset + pnf + 4) * 60; 1363 if (callback) callback (DC_SAMPLE_DECO, &sample, userdata); 1364 1365 // for logversion 7 and newer (introduced for Perdix AI) 1366 // detect tank pressure 1367 if (parser->logversion >= 7) { 1368 const unsigned int idx[2] = {27, 19}; 1369 const unsigned int count = type == LOG_RECORD_AVELO_SAMPLE ? 1 : 2; 1370 for (unsigned int i = 0; i < count; ++i) { 1371 // Tank pressure 1372 // Values above 0xFFF0 are special codes: 1373 // 0xFFFF AI is off 1374 // 0xFFFE No comms for 90 seconds+ 1375 // 0xFFFD No comms for 30 seconds 1376 // 0xFFFC Transmitter not paired 1377 // For regular values, the top 4 bits contain the battery 1378 // level (0=normal, 1=critical, 2=warning), and the lower 12 1379 // bits the tank pressure in units of 2 psi. 1380 unsigned int pressure = array_uint16_be (data + offset + pnf + idx[i]); 1381 unsigned int id = (parser->aimode == AI_HPCCR ? 4 : 0) + i; 1382 if (pressure < 0xFFF0) { 1383 pressure &= 0x0FFF; 1384 if (pressure) { 1385 sample.pressure.tank = parser->tankidx[id]; 1386 sample.pressure.value = pressure * 2 * PSI / BAR; 1387 if (callback) callback (DC_SAMPLE_PRESSURE, &sample, userdata); 1388 } 1389 } 1390 } 1391 1392 // Gas time remaining in minutes 1393 // Values above 0xF0 are special codes: 1394 // 0xFF Not paired 1395 // 0xFE No communication 1396 // 0xFD Not available in current mode 1397 // 0xFC Not available because of DECO 1398 // 0xFB Tank size or max pressure haven’t been set up 1399 if (data[offset + pnf + 21] < 0xF0) { 1400 sample.rbt = data[offset + pnf + 21]; 1401 if (callback) callback (DC_SAMPLE_RBT, &sample, userdata); 1402 } 1403 } 1404 1405 if (nsamples == 1 || nsamples == parser->nsamples) { 1406 unsigned int record = nsamples == 1 ? parser->opening[9] : parser->closing[9]; 1407 if (record != UNDEFINED && parser->logversion >= 17) { 1408 unsigned int gnss = data[record + 16]; 1409 int latitude = (signed int) array_uint32_be (data + record + 21); 1410 int longitude = (signed int) array_uint32_be (data + record + 25); 1411 if (gnss == GNSS_FIX_2D || gnss == GNSS_FIX_3D) { 1412 sample.location.latitude = latitude / 100000.0; 1413 sample.location.longitude = longitude / 100000.0; 1414 sample.location.altitude = 0.0; 1415 if (callback) callback (DC_SAMPLE_LOCATION, &sample, userdata); 1416 } 1417 } 1418 } 1419 } else if (type == LOG_RECORD_DIVE_SAMPLE_EXT) { 1420 // Tank pressure 1421 if (parser->logversion >= 13) { 1422 for (unsigned int i = 0; i < 2; ++i) { 1423 unsigned int pressure = array_uint16_be (data + offset + pnf + i * 2); 1424 unsigned int id = 2 + i; 1425 if (pressure < 0xFFF0) { 1426 pressure &= 0x0FFF; 1427 if (pressure) { 1428 sample.pressure.tank = parser->tankidx[id]; 1429 sample.pressure.value = pressure * 2 * PSI / BAR; 1430 if (callback) callback (DC_SAMPLE_PRESSURE, &sample, userdata); 1431 } 1432 } 1433 } 1434 } 1435 // Tank pressure (HP CCR) 1436 if (parser->logversion >= 14) { 1437 for (unsigned int i = 0; i < 2; ++i) { 1438 unsigned int pressure = array_uint16_be (data + offset + pnf + 4 + i * 2); 1439 unsigned int id = 4 + i; 1440 if (pressure) { 1441 sample.pressure.tank = parser->tankidx[id]; 1442 sample.pressure.value = pressure * 2 * PSI / BAR; 1443 if (callback) callback (DC_SAMPLE_PRESSURE, &sample, userdata); 1444 } 1445 } 1446 } 1447 } else if (type == LOG_RECORD_FREEDIVE_SAMPLE) { 1448 // A freedive record is actually 4 samples, each 8-bytes, 1449 // packed into a standard 32-byte sized record. At the end 1450 // of a dive, unused partial records will be 0 padded. 1451 for (unsigned int i = 0; i < 4; ++i) { 1452 unsigned int idx = offset + i * SZ_SAMPLE_FREEDIVE; 1453 1454 // Ignore empty samples. 1455 if (array_isequal (data + idx, SZ_SAMPLE_FREEDIVE, 0x00)) { 1456 break; 1457 } 1458 1459 // Time (seconds). 1460 time += interval; 1461 sample.time = time; 1462 if (callback) callback (DC_SAMPLE_TIME, &sample, userdata); 1463 1464 // Depth (absolute pressure in millibar) 1465 unsigned int depth = array_uint16_be (data + idx + 1); 1466 sample.depth = (signed int)(depth - parser->atmospheric) * (BAR / 1000.0) / (parser->density * GRAVITY); 1467 if (callback) callback (DC_SAMPLE_DEPTH, &sample, userdata); 1468 1469 // Temperature (1/10 °C). 1470 int temperature = (signed short) array_uint16_be (data + idx + 3); 1471 sample.temperature = temperature / 10.0; 1472 if (callback) callback (DC_SAMPLE_TEMPERATURE, &sample, userdata); 1473 } 1474 } else if (type == LOG_RECORD_INFO_EVENT) { 1475 unsigned int event = data[offset + 1]; 1476 unsigned int DC_ATTR_UNUSED timestamp = array_uint32_be (data + offset + 4); 1477 unsigned int w1 = array_uint32_be (data + offset + 8); 1478 unsigned int w2 = array_uint32_be (data + offset + 12); 1479 1480 if (event == INFO_EVENT_TAG_LOG) { 1481 // Compass heading 1482 if (w1 != 0xFFFFFFFF) { 1483 sample.bearing = w1; 1484 if (callback) callback (DC_SAMPLE_BEARING, &sample, userdata); 1485 } 1486 1487 // Tag 1488 sample.event.type = SAMPLE_EVENT_BOOKMARK; 1489 sample.event.time = 0; 1490 sample.event.flags = 0; 1491 sample.event.value = w2; 1492 if (callback) callback (DC_SAMPLE_EVENT, &sample, userdata); 1493 } 1494 } 1495 1496 offset += parser->samplesize; 1497 } 1498 1499 return DC_STATUS_SUCCESS; 1500 } 1501 1502 1503 static dc_status_t 1504 shearwater_predator_parser_calibration_prepass (shearwater_predator_parser_t *parser, struct dc_parser_sensor_calibration_t *calibration) 1505 { 1506 return shearwater_predator_parser_samples_foreach_internal(parser, NULL, NULL, calibration); 1507 } 1508 1509 1510 static dc_status_t 1511 shearwater_predator_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata) 1512 { 1513 shearwater_predator_parser_t *parser = (shearwater_predator_parser_t *) abstract; 1514 1515 dc_status_t rc = shearwater_predator_parser_cache (parser); 1516 if (rc != DC_STATUS_SUCCESS) 1517 return rc; 1518 1519 return shearwater_predator_parser_samples_foreach_internal(parser, callback, userdata, NULL); 1520 }