divesoft_freedom_parser.c (32398B)
1 /* 2 * libdivecomputer 3 * 4 * Copyright (C) 2023 Jan MatouĊĦek, 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 "divesoft_freedom.h" 28 #include "context-private.h" 29 #include "parser-private.h" 30 #include "checksum.h" 31 #include "array.h" 32 33 #define UNDEFINED 0xFFFFFFFF 34 35 #define EPOCH 946684800 // 2000-01-01 00:00:00 UTC 36 37 #define OC 0 38 #define OXYGEN 1 39 #define DILUENT 2 40 41 #define NSENSORS 4 42 #define NGASMIXES 12 43 #define NTANKS 12 44 45 #define HEADER_SIGNATURE_V1 0x45766944 // "DivE" 46 #define HEADER_SIGNATURE_V2 0x45566944 // "DiVE" 47 48 #define HEADER_SIZE_V1 32 49 #define HEADER_SIZE_V2 64 50 51 #define RECORD_SIZE 16 52 53 #define SEAWATER 1028 54 #define FRESHWATER 1000 55 56 typedef enum logrecord_t { 57 LREC_POINT = 0, 58 LREC_MANIPULATION = 1, 59 LREC_AUTO = 2, 60 LREC_DIVER_ERROR = 3, 61 LREC_INTERNAL_ERROR = 4, 62 LREC_ACTIVITY = 5, 63 LREC_CONFIGURATION = 6, 64 LREC_MEASURE = 7, 65 LREC_STATE = 8, 66 LREC_INFO = 9, 67 } logrecord_t; 68 69 typedef enum point_id_t { 70 POINT_1 = 0, 71 POINT_2 = 1, 72 POINT_1_OLD = 0x3FF, 73 } point_id_t; 74 75 typedef enum configuration_id_t { 76 CFG_ID_TEST_CCR_FULL = 0, 77 CFG_ID_TEST_CCR_PARTIAL = 1, 78 CFG_ID_OXYGEN_CALIBRATION = 2, 79 CFG_ID_SERIAL = 3, 80 CFG_ID_DECO = 4, 81 CFG_ID_VERSION = 5, 82 CFG_ID_ASCENT = 6, 83 CFG_ID_AI = 7, 84 CFG_ID_CCR = 8, 85 CFG_ID_DILUENTS = 9, 86 } configuration_id_t; 87 88 typedef enum measure_id_t { 89 MEASURE_ID_OXYGEN = 0, 90 MEASURE_ID_BATTERY = 1, 91 MEASURE_ID_HELIUM = 2, 92 MEASURE_ID_OXYGEN_MV = 3, 93 MEASURE_ID_GPS = 4, 94 MEASURE_ID_PRESSURE = 5, 95 MEASURE_ID_AI_SAC = 6, 96 MEASURE_ID_AI_PRESSURE = 7, 97 MEASURE_ID_BRIGHTNESS = 8, 98 MEASURE_ID_AI_STAT = 9, 99 } measure_id_t; 100 101 typedef enum state_id_t { 102 STATE_ID_DECO_N2LOW = 0, 103 STATE_ID_DECO_N2HIGH = 1, 104 STATE_ID_DECO_HELOW = 2, 105 STATE_ID_DECO_HEHIGH = 3, 106 STATE_ID_PLAN_STEPS = 4, 107 } state_id_t; 108 109 typedef enum event_t { 110 EVENT_DUMMY = 0, 111 EVENT_SETPOINT_MANUAL = 1, 112 EVENT_SETPOINT_AUTO = 2, 113 EVENT_OC = 3, 114 EVENT_CCR = 4, 115 EVENT_MIX_CHANGED = 5, 116 EVENT_START = 6, 117 EVENT_TOO_FAST = 7, 118 EVENT_ABOVE_CEILING = 8, 119 EVENT_TOXIC = 9, 120 EVENT_HYPOX = 10, 121 EVENT_CRITICAL = 11, 122 EVENT_SENSOR_DISABLED = 12, 123 EVENT_SENSOR_ENABLED = 13, 124 EVENT_O2_BACKUP = 14, 125 EVENT_PEER_DOWN = 15, 126 EVENT_HS_DOWN = 16, 127 EVENT_INCONSISTENT = 17, 128 EVENT_KEYDOWN = 18, 129 EVENT_SCR = 19, 130 EVENT_ABOVE_STOP = 20, 131 EVENT_SAFETY_MISS = 21, 132 EVENT_FATAL = 22, 133 EVENT_DILUENT = 23, 134 EVENT_CHANGE_MODE = 24, 135 EVENT_SOLENOID = 25, 136 EVENT_BOOKMARK = 26, 137 EVENT_GF_SWITCH = 27, 138 EVENT_PEER_UP = 28, 139 EVENT_HS_UP = 29, 140 EVENT_CNS = 30, 141 EVENT_BATTERY_LOW = 31, 142 EVENT_PPO2_LOST = 32, 143 EVENT_SENSOR_VALUE_BAD = 33, 144 EVENT_SAFETY_STOP_END = 34, 145 EVENT_DECO_STOP_END = 35, 146 EVENT_DEEP_STOP_END = 36, 147 EVENT_NODECO_END = 37, 148 EVENT_DEPTH_REACHED = 38, 149 EVENT_TIME_ELAPSED = 39, 150 EVENT_STACK_USAGE = 40, 151 EVENT_GAS_SWITCH_INFO = 41, 152 EVENT_PRESSURE_SENS_WARN = 42, 153 EVENT_PRESSURE_SENS_FAIL = 43, 154 EVENT_CHECK_O2_SENSORS = 44, 155 EVENT_SWITCH_TO_COMP_SCR = 45, 156 EVENT_GAS_LOST = 46, 157 EVENT_AIRBREAK = 47, 158 EVENT_AIRBREAK_END = 48, 159 EVENT_AIRBREAK_MISSED = 49, 160 EVENT_BORMT_EXPIRATION = 50, 161 EVENT_BORMT_EXPIRED = 51, 162 EVENT_SENSOR_EXCLUDED = 52, 163 EVENT_PREBR_SKIPPED = 53, 164 EVENT_BOCCR_BORMT_EXPIRED = 54, 165 EVENT_WAYPOINT = 55, 166 EVENT_TURNAROUND = 56, 167 EVENT_SOLENOID_FAILURE = 57, 168 EVENT_SM_CYL_PRESS_DIFF = 58, 169 EVENT_BAILOUT_MOD_EXCEEDED = 59, 170 } event_t; 171 172 typedef enum divemode_t { 173 STMODE_UNKNOWN = 0, 174 STMODE_OC = 1, 175 STMODE_CCR = 2, 176 STMODE_MCCR = 3, 177 STMODE_FREE = 4, 178 STMODE_GAUGE = 5, 179 STMODE_ASCR = 6, 180 STMODE_PSCR = 7, 181 STMODE_BOCCR = 8, 182 } divemode_t; 183 184 typedef enum setpoint_change_t { 185 SP_MANUAL = 0, 186 SP_AUTO_START = 1, 187 SP_AUTO_HYPOX = 2, 188 SP_AUTO_TIMEOUT = 3, 189 SP_AUTO_ASCENT = 4, 190 SP_AUTO_STALL = 5, 191 SP_AUTO_SPLOW = 6, 192 SP_AUTO_DEPTH_DESC = 7, 193 SP_AUTO_DEPTH_ASC = 8, 194 } setpoint_change_t; 195 196 typedef enum sensor_state_t { 197 SENSTAT_NORMAL = 0, 198 SENSTAT_OVERRANGE = 1, 199 SENSTAT_DISABLED = 2, 200 SENSTAT_EXCLUDED = 3, 201 SENSTAT_UNCALIBRATED = 4, 202 SENSTAT_ERROR = 5, 203 SENSTAT_OFFLINE = 6, 204 SENSTAT_INHIBITED = 7, 205 SENSTAT_NOT_EXIST = 8, 206 } sensor_state_t; 207 208 typedef enum battery_state_t { 209 BATSTATE_NO_BATTERY = 0, 210 BATSTATE_UNKNOWN = 1, 211 BATSTATE_DISCHARGING = 2, 212 BATSTATE_CHARGING = 3, 213 BATSTATE_FULL = 4, 214 } battery_state_t; 215 216 typedef struct divesoft_freedom_gasmix_t { 217 unsigned int oxygen; 218 unsigned int helium; 219 unsigned int type; 220 unsigned int id; 221 } divesoft_freedom_gasmix_t; 222 223 typedef struct divesoft_freedom_tank_t { 224 unsigned int volume; 225 unsigned int workpressure; 226 unsigned int beginpressure; 227 unsigned int endpressure; 228 unsigned int transmitter; 229 unsigned int active; 230 } divesoft_freedom_tank_t; 231 232 typedef struct divesoft_freedom_parser_t { 233 dc_parser_t base; 234 // Cached fields. 235 unsigned int cached; 236 unsigned int version; 237 unsigned int headersize; 238 unsigned int divetime; 239 unsigned int divemode; 240 int temperature_min; 241 unsigned int maxdepth; 242 unsigned int atmospheric; 243 unsigned int avgdepth; 244 unsigned int ngasmixes; 245 divesoft_freedom_gasmix_t gasmix[NGASMIXES]; 246 unsigned int diluent; 247 unsigned int ntanks; 248 divesoft_freedom_tank_t tank[NTANKS]; 249 unsigned int vpm; 250 unsigned int gf_lo; 251 unsigned int gf_hi; 252 unsigned int seawater; 253 unsigned int calibration[NSENSORS]; 254 unsigned int calibrated; 255 } divesoft_freedom_parser_t; 256 257 static dc_status_t divesoft_freedom_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime); 258 static dc_status_t divesoft_freedom_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value); 259 static dc_status_t divesoft_freedom_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata); 260 261 static const dc_parser_vtable_t divesoft_freedom_parser_vtable = { 262 sizeof(divesoft_freedom_parser_t), 263 DC_FAMILY_DIVESOFT_FREEDOM, 264 NULL, /* set_clock */ 265 NULL, /* set_atmospheric */ 266 NULL, /* set_density */ 267 divesoft_freedom_parser_get_datetime, /* datetime */ 268 divesoft_freedom_parser_get_field, /* fields */ 269 divesoft_freedom_parser_samples_foreach, /* samples_foreach */ 270 NULL /* destroy */ 271 }; 272 273 static unsigned int 274 divesoft_freedom_find_gasmix (divesoft_freedom_gasmix_t gasmix[], unsigned int count, unsigned int oxygen, unsigned int helium, unsigned int type) 275 { 276 unsigned int i = 0; 277 while (i < count) { 278 if (oxygen == gasmix[i].oxygen && 279 helium == gasmix[i].helium && 280 type == gasmix[i].type) 281 break; 282 i++; 283 } 284 285 return i; 286 } 287 288 static unsigned int 289 divesoft_freedom_find_tank (divesoft_freedom_tank_t tank[], unsigned int count, unsigned int transmitter) 290 { 291 unsigned int i = 0; 292 while (i < count) { 293 if (transmitter == tank[i].transmitter) 294 break; 295 i++; 296 } 297 298 return i; 299 } 300 301 static unsigned int 302 divesoft_freedom_is_ccr (unsigned int divemode) 303 { 304 return divemode == STMODE_CCR || divemode == STMODE_MCCR || 305 divemode == STMODE_ASCR || divemode == STMODE_PSCR || 306 divemode == STMODE_BOCCR; 307 } 308 309 static dc_status_t 310 divesoft_freedom_cache (divesoft_freedom_parser_t *parser) 311 { 312 dc_parser_t *abstract = (dc_parser_t *) parser; 313 const unsigned char *data = abstract->data; 314 unsigned int size = abstract->size; 315 316 if (parser->cached) { 317 return DC_STATUS_SUCCESS; 318 } 319 320 unsigned int headersize = 4; 321 if (size < headersize) { 322 ERROR (abstract->context, "Unexpected header size (%u).", size); 323 return DC_STATUS_DATAFORMAT; 324 } 325 326 unsigned int version = array_uint32_le (data); 327 if (version == HEADER_SIGNATURE_V1) { 328 headersize = HEADER_SIZE_V1; 329 } else if (version == HEADER_SIGNATURE_V2) { 330 headersize = HEADER_SIZE_V2; 331 } else { 332 ERROR (abstract->context, "Unexpected header version (%08x).", version); 333 return DC_STATUS_DATAFORMAT; 334 } 335 336 if (size < headersize) { 337 ERROR (abstract->context, "Unexpected header size (%u).", size); 338 return DC_STATUS_DATAFORMAT; 339 } 340 341 unsigned short crc = array_uint16_le (data + 4); 342 unsigned short ccrc = checksum_crc16r_ansi (data + 6, headersize - 6, 0xFFFF, 0x0000); 343 if (crc != ccrc) { 344 ERROR (abstract->context, "Invalid header checksum (%04x %04x).", crc, ccrc); 345 return DC_STATUS_DATAFORMAT; 346 } 347 348 // Parse the dive header. 349 unsigned int divetime = 0; 350 unsigned int divemode = 0; 351 unsigned int temperature_min = 0; 352 unsigned int maxdepth = 0; 353 unsigned int atmospheric = 0; 354 unsigned int avgdepth = 0; 355 unsigned int diluent_o2 = 0, diluent_he = 0; 356 if (version == HEADER_SIGNATURE_V1) { 357 unsigned int misc1 = array_uint32_le (data + 12); 358 unsigned int misc2 = array_uint32_le (data + 16); 359 divetime = misc1 & 0x1FFFF; 360 divemode = (misc1 & 0x38000000) >> 27; 361 temperature_min = (signed int) signextend ((misc2 & 0xFFC0000) >> 18, 10); 362 maxdepth = array_uint16_le (data + 20); 363 atmospheric = array_uint16_le (data + 24); 364 avgdepth = 0; 365 diluent_o2 = data[26]; 366 diluent_he = data[27]; 367 } else { 368 divetime = array_uint32_le (data + 12); 369 divemode = data[18]; 370 temperature_min = (signed short) array_uint16_le (data + 24); 371 maxdepth = array_uint16_le (data + 28); 372 atmospheric = array_uint16_le (data + 32); 373 avgdepth = array_uint16_le (data + 38); 374 diluent_o2 = 0; 375 diluent_he = 0; 376 377 DEBUG (abstract->context, "Device: serial=%.4s-%.8s", 378 data + 52, data + 56); 379 } 380 381 divesoft_freedom_gasmix_t gasmix_ai[NGASMIXES] = {0}, 382 gasmix_diluent[NGASMIXES] = {0}, 383 gasmix_event[NGASMIXES] = {0}; 384 unsigned int ngasmix_ai = 0, 385 ngasmix_diluent = 0, 386 ngasmix_event = 0; 387 divesoft_freedom_tank_t tank[NTANKS] = {0}; 388 unsigned int ntanks = 0; 389 390 unsigned int vpm = 0, gf_lo = 0, gf_hi = 0; 391 unsigned int seawater = 0; 392 unsigned int calibration[NSENSORS] = {0}; 393 unsigned int calibrated = 0; 394 395 unsigned int gasmixid_previous = UNDEFINED; 396 397 // Parse the dive profile. 398 unsigned int offset = headersize; 399 while (offset + RECORD_SIZE <= size) { 400 if (array_isequal(data + offset, RECORD_SIZE, 0xFF)) { 401 WARNING (abstract->context, "Skipping empty sample."); 402 offset += RECORD_SIZE; 403 continue; 404 } 405 406 unsigned int flags = array_uint32_le (data + offset); 407 unsigned int type = (flags & 0x0000000F) >> 0; 408 unsigned int id = (flags & 0x7FE00000) >> 21; 409 410 if (type == LREC_CONFIGURATION) { 411 // Configuration record. 412 if (id == CFG_ID_DECO) { 413 unsigned int misc = array_uint16_le (data + offset + 4); 414 gf_lo = data[offset + 8]; 415 gf_hi = data[offset + 9]; 416 seawater = misc & 0x02; 417 vpm = misc & 0x20; 418 } else if (id == CFG_ID_VERSION) { 419 DEBUG (abstract->context, "Device: type=%u, hw=%u.%u, sw=%u.%u.%u.%u flags=%u", 420 data[offset + 4], 421 data[offset + 5], data[offset + 6], 422 data[offset + 7], data[offset + 8], data[offset + 9], 423 array_uint32_le (data + offset + 12), 424 array_uint16_le (data + offset + 10)); 425 } else if (id == CFG_ID_SERIAL) { 426 DEBUG (abstract->context, "Device: serial=%.4s-%.8s", 427 data + offset + 4, data + offset + 8); 428 } else if (id == CFG_ID_DILUENTS) { 429 for (unsigned int i = 0; i < 4; ++i) { 430 unsigned int o2 = data[offset + 4 + i * 3 + 0]; 431 unsigned int he = data[offset + 4 + i * 3 + 1]; 432 unsigned int state = data[offset + 4 + i * 3 + 2]; 433 if (state & 0x01) { 434 if (ngasmix_diluent >= NGASMIXES) { 435 ERROR (abstract->context, "Maximum number of gas mixes reached."); 436 return DC_STATUS_NOMEMORY; 437 } 438 gasmix_diluent[ngasmix_diluent].oxygen = o2; 439 gasmix_diluent[ngasmix_diluent].helium = he; 440 gasmix_diluent[ngasmix_diluent].type = DILUENT; 441 gasmix_diluent[ngasmix_diluent].id = (state & 0xFE) >> 1; 442 ngasmix_diluent++; 443 } 444 } 445 } else if (id == CFG_ID_OXYGEN_CALIBRATION) { 446 for (unsigned int i = 0; i < NSENSORS; ++i) { 447 calibration[i] = array_uint16_le (data + offset + 4 + i * 2); 448 } 449 calibrated = 1; 450 } else if (id == CFG_ID_AI) { 451 unsigned int o2 = data[offset + 4]; 452 unsigned int he = data[offset + 5]; 453 unsigned int volume = array_uint16_le (data + offset + 6); 454 unsigned int workpressure = array_uint16_le (data + offset + 8); 455 unsigned int transmitter = data[offset + 10]; 456 unsigned int gasmixid = data[offset + 11]; 457 458 // Workaround for a bug in some pre-release firmware versions, 459 // where the ID of the CCR gas mixes (oxygen and diluent) is 460 // not stored correctly. 461 if (gasmixid < 10 && gasmixid <= gasmixid_previous && gasmixid_previous != UNDEFINED) { 462 WARNING (abstract->context, "Fixed the CCR gas mix id (%u -> %u) for tank %u.", 463 gasmixid, gasmixid + 10, ntanks); 464 gasmixid += 10; 465 } 466 gasmixid_previous = gasmixid; 467 468 // Add the gas mix. 469 if (ngasmix_ai >= NGASMIXES) { 470 ERROR (abstract->context, "Maximum number of gas mixes reached."); 471 return DC_STATUS_NOMEMORY; 472 } 473 gasmix_ai[ngasmix_ai].oxygen = o2; 474 gasmix_ai[ngasmix_ai].helium = he; 475 if (gasmixid == 10) { 476 gasmix_ai[ngasmix_ai].type = OXYGEN; 477 } else if (gasmixid == 11) { 478 gasmix_ai[ngasmix_ai].type = DILUENT; 479 } else { 480 gasmix_ai[ngasmix_ai].type = OC; 481 } 482 gasmix_ai[ngasmix_ai].id = gasmixid; 483 ngasmix_ai++; 484 485 // Add the tank. 486 if (ntanks >= NTANKS) { 487 ERROR (abstract->context, "Maximum number of tanks reached."); 488 return DC_STATUS_NOMEMORY; 489 } 490 tank[ntanks].volume = volume; 491 tank[ntanks].workpressure = workpressure; 492 tank[ntanks].transmitter = transmitter; 493 ntanks++; 494 } 495 } else if ((type >= LREC_MANIPULATION && type <= LREC_ACTIVITY) || type == LREC_INFO) { 496 // Event record. 497 unsigned int event = array_uint16_le (data + offset + 4); 498 499 if (event == EVENT_MIX_CHANGED || event == EVENT_DILUENT || event == EVENT_CHANGE_MODE) { 500 unsigned int o2 = data[offset + 6]; 501 unsigned int he = data[offset + 7]; 502 unsigned int mixtype = OC; 503 if (event == EVENT_DILUENT) { 504 mixtype = DILUENT; 505 } else if (event == EVENT_CHANGE_MODE) { 506 unsigned int mode = data[offset + 8]; 507 if (divesoft_freedom_is_ccr (mode)) { 508 mixtype = DILUENT; 509 } 510 } 511 512 unsigned int idx = divesoft_freedom_find_gasmix (gasmix_event, ngasmix_event, o2, he, mixtype); 513 if (idx >= ngasmix_event) { 514 if (ngasmix_event >= NGASMIXES) { 515 ERROR (abstract->context, "Maximum number of gas mixes reached."); 516 return DC_STATUS_NOMEMORY; 517 } 518 gasmix_event[ngasmix_event].oxygen = o2; 519 gasmix_event[ngasmix_event].helium = he; 520 gasmix_event[ngasmix_event].type = mixtype; 521 gasmix_event[ngasmix_event].id = UNDEFINED; 522 ngasmix_event++; 523 } 524 } 525 } else if (type == LREC_MEASURE) { 526 // Measurement record. 527 if (id == MEASURE_ID_AI_PRESSURE) { 528 for (unsigned int i = 0; i < NTANKS; ++i) { 529 unsigned int pressure = data[offset + 4 + i]; 530 if (pressure == 0 || pressure == 0xFF) 531 continue; 532 533 unsigned int idx = divesoft_freedom_find_tank (tank, ntanks, i); 534 if (idx >= ntanks) { 535 ERROR (abstract->context, "Tank %u not found.", idx); 536 return DC_STATUS_DATAFORMAT; 537 } 538 539 if (!tank[idx].active) { 540 tank[idx].active = 1; 541 tank[idx].beginpressure = pressure; 542 tank[idx].endpressure = pressure; 543 } 544 tank[idx].endpressure = pressure; 545 } 546 } 547 } 548 549 offset += RECORD_SIZE; 550 } 551 552 unsigned int ngasmixes = 0; 553 divesoft_freedom_gasmix_t gasmix[NGASMIXES] = {0}; 554 unsigned int diluent = UNDEFINED; 555 556 // Add the gas mixes from the AI integration records. 557 for (unsigned int i = 0; i < ngasmix_ai; ++i) { 558 gasmix[ngasmixes] = gasmix_ai[i]; 559 ngasmixes++; 560 } 561 562 // Add the gas mixes from the diluent records. 563 for (unsigned int i = 0; i < ngasmix_diluent; ++i) { 564 unsigned int idx = divesoft_freedom_find_gasmix (gasmix, ngasmixes, 565 gasmix_diluent[i].oxygen, gasmix_diluent[i].helium, gasmix_diluent[i].type); 566 if (idx >= ngasmixes) { 567 if (ngasmixes >= NGASMIXES) { 568 ERROR (abstract->context, "Maximum number of gas mixes reached."); 569 return DC_STATUS_NOMEMORY; 570 } 571 gasmix[ngasmixes] = gasmix_diluent[i]; 572 ngasmixes++; 573 } 574 } 575 576 // Add the initial diluent. 577 if (divesoft_freedom_is_ccr (divemode) && 578 (diluent_o2 != 0 || diluent_he != 0)) { 579 unsigned int idx = divesoft_freedom_find_gasmix (gasmix, ngasmixes, 580 diluent_o2, diluent_he, DILUENT); 581 if (idx >= ngasmixes) { 582 if (ngasmixes >= NGASMIXES) { 583 ERROR (abstract->context, "Maximum number of gas mixes reached."); 584 return DC_STATUS_NOMEMORY; 585 } 586 gasmix[ngasmixes].oxygen = diluent_o2; 587 gasmix[ngasmixes].helium = diluent_he; 588 gasmix[ngasmixes].type = DILUENT; 589 gasmix[ngasmixes].id = UNDEFINED; 590 ngasmixes++; 591 } 592 593 // Index of the initial diluent. 594 diluent = idx; 595 } 596 597 // Add the gas mixes from the gas change events. 598 for (unsigned int i = 0; i < ngasmix_event; ++i) { 599 unsigned int idx = divesoft_freedom_find_gasmix (gasmix, ngasmixes, 600 gasmix_event[i].oxygen, gasmix_event[i].helium, gasmix_event[i].type); 601 if (idx >= ngasmixes) { 602 if (ngasmixes >= NGASMIXES) { 603 ERROR (abstract->context, "Maximum number of gas mixes reached."); 604 return DC_STATUS_NOMEMORY; 605 } 606 gasmix[ngasmixes] = gasmix_event[i]; 607 ngasmixes++; 608 } 609 } 610 611 // Cache the data for later use. 612 parser->cached = 1; 613 parser->version = version; 614 parser->headersize = headersize; 615 parser->divetime = divetime; 616 parser->divemode = divemode; 617 parser->temperature_min = temperature_min; 618 parser->maxdepth = maxdepth; 619 parser->atmospheric = atmospheric; 620 parser->avgdepth = avgdepth; 621 parser->ngasmixes = ngasmixes; 622 for (unsigned int i = 0; i < ngasmixes; ++i) { 623 parser->gasmix[i] = gasmix[i]; 624 } 625 parser->diluent = diluent; 626 parser->ntanks = ntanks; 627 for (unsigned int i = 0; i < ntanks; ++i) { 628 parser->tank[i] = tank[i]; 629 } 630 parser->vpm = vpm; 631 parser->gf_lo = gf_lo; 632 parser->gf_hi = gf_hi; 633 parser->seawater = seawater; 634 for (unsigned int i = 0; i < NSENSORS; ++i) { 635 parser->calibration[i] = calibration[i]; 636 } 637 parser->calibrated = calibrated; 638 639 return DC_STATUS_SUCCESS; 640 } 641 642 dc_status_t 643 divesoft_freedom_parser_create (dc_parser_t **out, dc_context_t *context, const unsigned char data[], size_t size) 644 { 645 divesoft_freedom_parser_t *parser = NULL; 646 647 if (out == NULL) 648 return DC_STATUS_INVALIDARGS; 649 650 // Allocate memory. 651 parser = (divesoft_freedom_parser_t *) dc_parser_allocate (context, &divesoft_freedom_parser_vtable, data, size); 652 if (parser == NULL) { 653 ERROR (context, "Failed to allocate memory."); 654 return DC_STATUS_NOMEMORY; 655 } 656 657 // Set the default values. 658 parser->cached = 0; 659 parser->version = 0; 660 parser->headersize = 0; 661 parser->divetime = 0; 662 parser->divemode = 0; 663 parser->temperature_min = 0; 664 parser->maxdepth = 0; 665 parser->atmospheric = 0; 666 parser->avgdepth = 0; 667 parser->ngasmixes = 0; 668 for (unsigned int i = 0; i < NGASMIXES; ++i) { 669 parser->gasmix[i].oxygen = 0; 670 parser->gasmix[i].helium = 0; 671 parser->gasmix[i].type = 0; 672 parser->gasmix[i].id = 0; 673 } 674 parser->diluent = UNDEFINED; 675 parser->ntanks = 0; 676 for (unsigned int i = 0; i < NTANKS; ++i) { 677 parser->tank[i].volume = 0; 678 parser->tank[i].workpressure = 0; 679 parser->tank[i].beginpressure = 0; 680 parser->tank[i].endpressure = 0; 681 parser->tank[i].transmitter = 0; 682 parser->tank[i].active = 0; 683 } 684 parser->vpm = 0; 685 parser->gf_lo = 0; 686 parser->gf_hi = 0; 687 parser->seawater = 0; 688 for (unsigned int i = 0; i < NSENSORS; ++i) { 689 parser->calibration[i] = 0; 690 } 691 parser->calibrated = 0; 692 693 *out = (dc_parser_t *) parser; 694 695 return DC_STATUS_SUCCESS; 696 } 697 698 static dc_status_t 699 divesoft_freedom_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime) 700 { 701 dc_status_t status = DC_STATUS_SUCCESS; 702 divesoft_freedom_parser_t *parser = (divesoft_freedom_parser_t *) abstract; 703 const unsigned char *data = abstract->data; 704 705 // Cache the header data. 706 status = divesoft_freedom_cache (parser); 707 if (status != DC_STATUS_SUCCESS) 708 return status; 709 710 unsigned int timestamp = array_uint32_le (data + 8); 711 712 int timezone = 0; 713 if (parser->version == HEADER_SIGNATURE_V2) { 714 timezone = ((signed short) array_uint16_le (data + 40)) * 60; 715 } else { 716 timezone = 0; 717 } 718 719 dc_ticks_t ticks = (dc_ticks_t) timestamp + EPOCH + timezone; 720 721 if (!dc_datetime_gmtime (datetime, ticks)) 722 return DC_STATUS_DATAFORMAT; 723 724 if (parser->version == HEADER_SIGNATURE_V2) { 725 datetime->timezone = timezone; 726 } else { 727 datetime->timezone = DC_TIMEZONE_NONE; 728 } 729 730 return DC_STATUS_SUCCESS; 731 } 732 733 static dc_status_t 734 divesoft_freedom_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value) 735 { 736 dc_status_t status = DC_STATUS_SUCCESS; 737 divesoft_freedom_parser_t *parser = (divesoft_freedom_parser_t *) abstract; 738 739 // Cache the header data. 740 status = divesoft_freedom_cache (parser); 741 if (status != DC_STATUS_SUCCESS) 742 return status; 743 744 dc_salinity_t *water = (dc_salinity_t *) value; 745 dc_gasmix_t *gasmix = (dc_gasmix_t *) value; 746 dc_tank_t *tank = (dc_tank_t *) value; 747 dc_decomodel_t *decomodel = (dc_decomodel_t *) value; 748 749 if (value) { 750 switch (type) { 751 case DC_FIELD_DIVETIME: 752 *((unsigned int *) value) = parser->divetime; 753 break; 754 case DC_FIELD_MAXDEPTH: 755 *((double *) value) = parser->maxdepth / 100.0; 756 break; 757 case DC_FIELD_AVGDEPTH: 758 if (parser->version != HEADER_SIGNATURE_V2) 759 return DC_STATUS_UNSUPPORTED; 760 *((double *) value) = parser->avgdepth / 100.0; 761 break; 762 case DC_FIELD_TEMPERATURE_MINIMUM: 763 *((double *) value) = parser->temperature_min / 10.0; 764 break; 765 case DC_FIELD_ATMOSPHERIC: 766 *((double *) value) = parser->atmospheric * 10.0 / BAR; 767 break; 768 case DC_FIELD_SALINITY: 769 water->type = parser->seawater ? DC_WATER_SALT : DC_WATER_FRESH; 770 water->density = parser->seawater ? SEAWATER : FRESHWATER; 771 break; 772 case DC_FIELD_DIVEMODE: 773 switch (parser->divemode) { 774 case STMODE_OC: 775 *((dc_divemode_t *) value) = DC_DIVEMODE_OC; 776 break; 777 case STMODE_CCR: 778 case STMODE_MCCR: 779 case STMODE_BOCCR: 780 *((dc_divemode_t *) value) = DC_DIVEMODE_CCR; 781 break; 782 case STMODE_FREE: 783 *((dc_divemode_t *) value) = DC_DIVEMODE_FREEDIVE; 784 break; 785 case STMODE_GAUGE: 786 *((dc_divemode_t *) value) = DC_DIVEMODE_GAUGE; 787 break; 788 case STMODE_ASCR: 789 case STMODE_PSCR: 790 *((dc_divemode_t *) value) = DC_DIVEMODE_SCR; 791 break; 792 case STMODE_UNKNOWN: 793 return DC_STATUS_UNSUPPORTED; 794 default: 795 return DC_STATUS_DATAFORMAT; 796 } 797 break; 798 case DC_FIELD_GASMIX_COUNT: 799 *((unsigned int *) value) = parser->ngasmixes; 800 break; 801 case DC_FIELD_GASMIX: 802 if (parser->gasmix[flags].type == OXYGEN) { 803 gasmix->usage = DC_USAGE_OXYGEN; 804 } else if (parser->gasmix[flags].type == DILUENT) { 805 gasmix->usage = DC_USAGE_DILUENT; 806 } else { 807 gasmix->usage = DC_USAGE_OPEN_CIRCUIT; 808 } 809 gasmix->helium = parser->gasmix[flags].helium / 100.0; 810 gasmix->oxygen = parser->gasmix[flags].oxygen / 100.0; 811 gasmix->nitrogen = 1.0 - gasmix->oxygen - gasmix->helium; 812 break; 813 case DC_FIELD_TANK_COUNT: 814 *((unsigned int *) value) = parser->ntanks; 815 break; 816 case DC_FIELD_TANK: 817 if (parser->tank[flags].volume > 990 || 818 parser->tank[flags].workpressure > 400) { 819 tank->type = DC_TANKVOLUME_NONE; 820 tank->volume = 0.0; 821 tank->workpressure = 0.0; 822 } else { 823 tank->type = DC_TANKVOLUME_METRIC; 824 tank->volume = parser->tank[flags].volume / 10.0; 825 tank->workpressure = parser->tank[flags].workpressure; 826 } 827 tank->beginpressure = parser->tank[flags].beginpressure * 2.0; 828 tank->endpressure = parser->tank[flags].endpressure * 2.0; 829 tank->gasmix = flags; 830 tank->usage = DC_TANK_USAGE_NONE; 831 break; 832 case DC_FIELD_DECOMODEL: 833 if (parser->vpm) { 834 decomodel->type = DC_DECOMODEL_VPM; 835 decomodel->conservatism = 0; 836 } else { 837 decomodel->type = DC_DECOMODEL_BUHLMANN; 838 decomodel->conservatism = 0; 839 decomodel->params.gf.low = parser->gf_lo; 840 decomodel->params.gf.high = parser->gf_hi; 841 } 842 break; 843 default: 844 return DC_STATUS_UNSUPPORTED; 845 } 846 } 847 848 return DC_STATUS_SUCCESS; 849 } 850 851 static dc_status_t 852 divesoft_freedom_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata) 853 { 854 dc_status_t status = DC_STATUS_SUCCESS; 855 divesoft_freedom_parser_t *parser = (divesoft_freedom_parser_t *) abstract; 856 const unsigned char *data = abstract->data; 857 unsigned int size = abstract->size; 858 859 // Cache the header data. 860 status = divesoft_freedom_cache (parser); 861 if (status != DC_STATUS_SUCCESS) 862 return status; 863 864 unsigned int time = UNDEFINED; 865 unsigned int initial = 0; 866 unsigned int offset = parser->headersize; 867 while (offset + RECORD_SIZE <= size) { 868 dc_sample_value_t sample = {0}; 869 870 if (array_isequal(data + offset, RECORD_SIZE, 0xFF)) { 871 WARNING (abstract->context, "Skipping empty sample."); 872 offset += RECORD_SIZE; 873 continue; 874 } 875 876 unsigned int flags = array_uint32_le (data + offset); 877 unsigned int type = (flags & 0x0000000F) >> 0; 878 unsigned int timestamp = (flags & 0x001FFFF0) >> 4; 879 unsigned int id = (flags & 0x7FE00000) >> 21; 880 881 if (timestamp != time) { 882 if (timestamp < time && time != UNDEFINED) { 883 // The timestamp are supposed to be monotonically increasing, 884 // but occasionally there are small jumps back in time with just 885 // 1 or 2 seconds. To get back in sync, those samples are 886 // skipped. Larger jumps are treated as errors. 887 if (time - timestamp > 5) { 888 ERROR (abstract->context, "Timestamp moved backwards (%u %u).", timestamp, time); 889 return DC_STATUS_DATAFORMAT; 890 } 891 WARNING (abstract->context, "Timestamp moved backwards (%u %u).", timestamp, time); 892 offset += RECORD_SIZE; 893 continue; 894 } 895 time = timestamp; 896 sample.time = time * 1000; 897 if (callback) callback(DC_SAMPLE_TIME, &sample, userdata); 898 } 899 900 // Initial diluent. 901 if (!initial) { 902 if (parser->diluent != UNDEFINED) { 903 sample.gasmix = parser->diluent; 904 if (callback) callback(DC_SAMPLE_GASMIX, &sample, userdata); 905 } 906 initial = 1; 907 } 908 909 if (type == LREC_POINT) { 910 // General log record. 911 unsigned int depth = array_uint16_le (data + offset + 4); 912 unsigned int ppo2 = array_uint16_le (data + offset + 6); 913 914 sample.depth = depth / 100.0; 915 if (callback) callback(DC_SAMPLE_DEPTH, &sample, userdata); 916 917 if (ppo2) { 918 sample.ppo2.sensor = DC_SENSOR_NONE; 919 sample.ppo2.value = ppo2 * 10.0 / BAR; 920 if (callback) callback(DC_SAMPLE_PPO2, &sample, userdata); 921 } 922 923 if (id == POINT_1 || id == POINT_1_OLD) { 924 unsigned int misc = array_uint32_le (data + offset + 8); 925 unsigned int ceiling = array_uint16_le (data + offset + 12); 926 unsigned int setpoint = data[offset + 15]; 927 unsigned int ndl = (misc & 0x000003FF); 928 unsigned int tts = (misc & 0x000FFC00) >> 10; 929 unsigned int temp = (misc & 0x3FF00000) >> 20; 930 931 // Temperature 932 sample.temperature = (signed int) signextend (temp, 10) / 10.0; 933 if (callback) callback(DC_SAMPLE_TEMPERATURE, &sample, userdata); 934 935 // Deco / NDL 936 if (ceiling) { 937 sample.deco.type = DC_DECO_DECOSTOP; 938 sample.deco.time = 0; 939 sample.deco.depth = ceiling / 100.0; 940 } else { 941 sample.deco.type = DC_DECO_NDL; 942 sample.deco.time = ndl * 60; 943 sample.deco.depth = 0.0; 944 } 945 sample.deco.tts = tts * 60; 946 if (callback) callback(DC_SAMPLE_DECO, &sample, userdata); 947 948 // Setpoint 949 if (setpoint) { 950 sample.setpoint = setpoint / 100.0; 951 if (callback) callback(DC_SAMPLE_SETPOINT, &sample, userdata); 952 } 953 } 954 } else if ((type >= LREC_MANIPULATION && type <= LREC_ACTIVITY) || type == LREC_INFO) { 955 // Event record. 956 unsigned int event = array_uint16_le (data + offset + 4); 957 958 if (event == EVENT_BOOKMARK) { 959 sample.event.type = SAMPLE_EVENT_BOOKMARK; 960 sample.event.time = 0; 961 sample.event.flags = 0; 962 sample.event.value = 0; 963 if (callback) callback(DC_SAMPLE_EVENT, &sample, userdata); 964 } else if (event == EVENT_MIX_CHANGED || event == EVENT_DILUENT || event == EVENT_CHANGE_MODE) { 965 unsigned int o2 = data[offset + 6]; 966 unsigned int he = data[offset + 7]; 967 unsigned int mixtype = OC; 968 if (event == EVENT_DILUENT) { 969 mixtype = DILUENT; 970 } else if (event == EVENT_CHANGE_MODE) { 971 unsigned int mode = data[offset + 8]; 972 if (divesoft_freedom_is_ccr (mode)) { 973 mixtype = DILUENT; 974 } 975 } 976 977 unsigned int idx = divesoft_freedom_find_gasmix (parser->gasmix, parser->ngasmixes, o2, he, mixtype); 978 if (idx >= parser->ngasmixes) { 979 ERROR (abstract->context, "Gas mix (%u/%u) not found.", o2, he); 980 return DC_STATUS_DATAFORMAT; 981 } 982 sample.gasmix = idx; 983 if (callback) callback(DC_SAMPLE_GASMIX, &sample, userdata); 984 } else if (event == EVENT_CNS) { 985 sample.cns = array_uint16_le (data + offset + 6) / 100.0; 986 if (callback) callback(DC_SAMPLE_CNS, &sample, userdata); 987 } else if (event == EVENT_SETPOINT_MANUAL || event == EVENT_SETPOINT_AUTO) { 988 sample.setpoint = data[offset + 6] / 100.0; 989 if (callback) callback(DC_SAMPLE_SETPOINT, &sample, userdata); 990 } else if (event == EVENT_WAYPOINT) { 991 unsigned int heading = array_uint16_le (data + offset + 8); 992 if ((heading & 0x8000) == 0) { 993 sample.bearing = heading; 994 if (callback) callback (DC_SAMPLE_BEARING, &sample, userdata); 995 } 996 } 997 } else if (type == LREC_MEASURE) { 998 // Measurement record. 999 if (id == MEASURE_ID_AI_PRESSURE) { 1000 for (unsigned int i = 0; i < NTANKS; ++i) { 1001 unsigned int pressure = data[offset + 4 + i]; 1002 if (pressure == 0 || pressure == 0xFF) 1003 continue; 1004 1005 unsigned int idx = divesoft_freedom_find_tank (parser->tank, parser->ntanks, i); 1006 if (idx >= parser->ntanks) { 1007 ERROR (abstract->context, "Tank %u not found.", idx); 1008 return DC_STATUS_DATAFORMAT; 1009 } 1010 1011 sample.pressure.tank = idx; 1012 sample.pressure.value = pressure * 2.0; 1013 if (callback) callback(DC_SAMPLE_PRESSURE, &sample, userdata); 1014 } 1015 } else if (id == MEASURE_ID_OXYGEN) { 1016 for (unsigned int i = 0; i < NSENSORS; ++i) { 1017 unsigned int ppo2 = array_uint16_le (data + offset + 4 + i * 2); 1018 if (ppo2 == 0 || ppo2 == 0xFFFF) 1019 continue; 1020 sample.ppo2.sensor = i; 1021 sample.ppo2.value = ppo2 * 10.0 / BAR; 1022 if (callback) callback(DC_SAMPLE_PPO2, &sample, userdata); 1023 } 1024 } else if (id == MEASURE_ID_OXYGEN_MV) { 1025 for (unsigned int i = 0; i < NSENSORS; ++i) { 1026 unsigned int value = array_uint16_le (data + offset + 4 + i * 2); 1027 unsigned int state = data[offset + 12 + i]; 1028 if (!parser->calibrated || parser->calibration[i] == 0 || 1029 state == SENSTAT_UNCALIBRATED || state == SENSTAT_NOT_EXIST) 1030 continue; 1031 sample.ppo2.sensor = i; 1032 sample.ppo2.value = value / 100.0 * parser->calibration[i] / BAR; 1033 if (callback) callback(DC_SAMPLE_PPO2, &sample, userdata); 1034 } 1035 } else if (id == MEASURE_ID_GPS) { 1036 int latitude = (signed int) array_uint32_le (data + offset + 4); 1037 int longitude = (signed int) array_uint32_le (data + offset + 8); 1038 sample.location.latitude = latitude / 1000000.0; 1039 sample.location.longitude = longitude / 1000000.0; 1040 sample.location.altitude = 0.0; 1041 if (callback) callback (DC_SAMPLE_LOCATION, &sample, userdata); 1042 } 1043 } else if (type == LREC_STATE) { 1044 // Tissue saturation record. 1045 } 1046 1047 offset += RECORD_SIZE; 1048 } 1049 1050 return DC_STATUS_SUCCESS; 1051 }