liquivision_lynx_parser.c (15971B)
1 /* 2 * libdivecomputer 3 * 4 * Copyright (C) 2020 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 "liquivision_lynx.h" 28 #include "context-private.h" 29 #include "parser-private.h" 30 #include "platform.h" 31 #include "array.h" 32 33 #define ISINSTANCE(parser) dc_parser_isinstance((parser), &liquivision_lynx_parser_vtable) 34 35 #define XEN 0 36 #define XEO 1 37 #define LYNX 2 38 #define KAON 3 39 40 #define XEN_V1 0x83321485 // Not supported 41 #define XEN_V2 0x83321502 42 #define XEN_V3 0x83328401 43 44 #define XEO_V1_A 0x17485623 45 #define XEO_V1_B 0x27485623 46 #define XEO_V2_A 0x17488401 47 #define XEO_V2_B 0x27488401 48 #define XEO_V3_A 0x17488402 49 #define XEO_V3_B 0x27488402 50 51 #define LYNX_V1 0x67488403 52 #define LYNX_V2 0x67488404 53 #define LYNX_V3 0x67488405 54 55 #define KAON_V1 0x37488402 56 #define KAON_V2 0x47488402 57 58 #define SZ_HEADER_XEN 80 59 #define SZ_HEADER_OTHER 96 60 61 #define FRESH 0 62 #define BRACKISH 1 63 #define SALT 2 64 65 #define DECO 0 66 #define GAUGE 1 67 #define TEC 2 68 #define REC 3 69 70 #define ZHL16GF 0 71 #define RGBM 1 72 73 #define NORMAL 0 74 #define BOOKMARK 1 75 #define ALARM_DEPTH 2 76 #define ALARM_TIME 3 77 #define ALARM_VELOCITY 4 78 #define DECOSTOP 5 79 #define DECOSTOP_BREACHED 6 80 #define GASMIX 7 81 #define SETPOINT 8 82 #define BAILOUT_ON 9 83 #define BAILOUT_OFF 10 84 #define EMERGENCY_ON 11 85 #define EMERGENCY_OFF 12 86 #define LOST_GAS 13 87 #define SAFETY_STOP 14 88 #define TANK_PRESSURE 15 89 #define TANK_LIST 16 90 91 #define NGASMIXES 11 92 #define NTANKS 11 93 94 #define INVALID 0xFFFFFFFF 95 96 typedef struct liquivision_lynx_parser_t liquivision_lynx_parser_t; 97 98 typedef struct liquivision_lynx_gasmix_t { 99 unsigned int oxygen; 100 unsigned int helium; 101 } liquivision_lynx_gasmix_t; 102 103 typedef struct liquivision_lynx_tank_t { 104 unsigned int id; 105 unsigned int beginpressure; 106 unsigned int endpressure; 107 } liquivision_lynx_tank_t; 108 109 struct liquivision_lynx_parser_t { 110 dc_parser_t base; 111 unsigned int model; 112 unsigned int headersize; 113 // Cached fields. 114 unsigned int cached; 115 unsigned int ngasmixes; 116 unsigned int ntanks; 117 liquivision_lynx_gasmix_t gasmix[NGASMIXES]; 118 liquivision_lynx_tank_t tank[NTANKS]; 119 }; 120 121 static dc_status_t liquivision_lynx_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime); 122 static dc_status_t liquivision_lynx_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value); 123 static dc_status_t liquivision_lynx_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata); 124 125 static const dc_parser_vtable_t liquivision_lynx_parser_vtable = { 126 sizeof(liquivision_lynx_parser_t), 127 DC_FAMILY_LIQUIVISION_LYNX, 128 NULL, /* set_clock */ 129 NULL, /* set_atmospheric */ 130 NULL, /* set_density */ 131 liquivision_lynx_parser_get_datetime, /* datetime */ 132 liquivision_lynx_parser_get_field, /* fields */ 133 liquivision_lynx_parser_samples_foreach, /* samples_foreach */ 134 NULL /* destroy */ 135 }; 136 137 138 dc_status_t 139 liquivision_lynx_parser_create (dc_parser_t **out, dc_context_t *context, const unsigned char data[], size_t size, unsigned int model) 140 { 141 liquivision_lynx_parser_t *parser = NULL; 142 143 if (out == NULL) 144 return DC_STATUS_INVALIDARGS; 145 146 // Allocate memory. 147 parser = (liquivision_lynx_parser_t *) dc_parser_allocate (context, &liquivision_lynx_parser_vtable, data, size); 148 if (parser == NULL) { 149 ERROR (context, "Failed to allocate memory."); 150 return DC_STATUS_NOMEMORY; 151 } 152 153 // Set the default values. 154 parser->model = model; 155 parser->headersize = (model == XEN) ? SZ_HEADER_XEN : SZ_HEADER_OTHER; 156 parser->cached = 0; 157 parser->ngasmixes = 0; 158 parser->ntanks = 0; 159 for (unsigned int i = 0; i < NGASMIXES; ++i) { 160 parser->gasmix[i].oxygen = 0; 161 parser->gasmix[i].helium = 0; 162 } 163 for (unsigned int i = 0; i < NTANKS; ++i) { 164 parser->tank[i].id = 0; 165 parser->tank[i].beginpressure = 0; 166 parser->tank[i].endpressure = 0; 167 } 168 169 *out = (dc_parser_t *) parser; 170 171 return DC_STATUS_SUCCESS; 172 } 173 174 175 static dc_status_t 176 liquivision_lynx_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime) 177 { 178 liquivision_lynx_parser_t *parser = (liquivision_lynx_parser_t *) abstract; 179 180 if (abstract->size < parser->headersize) 181 return DC_STATUS_DATAFORMAT; 182 183 const unsigned char *p = abstract->data + 40; 184 185 if (datetime) { 186 datetime->year = array_uint16_le (p + 18); 187 datetime->month = array_uint16_le (p + 16) + 1; 188 datetime->day = array_uint16_le (p + 12) + 1; 189 datetime->hour = array_uint16_le (p + 8); 190 datetime->minute = array_uint16_le (p + 6); 191 datetime->second = array_uint16_le (p + 4); 192 datetime->timezone = DC_TIMEZONE_NONE; 193 } 194 195 return DC_STATUS_SUCCESS; 196 } 197 198 199 static dc_status_t 200 liquivision_lynx_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value) 201 { 202 liquivision_lynx_parser_t *parser = (liquivision_lynx_parser_t *) abstract; 203 204 if (abstract->size < parser->headersize) 205 return DC_STATUS_DATAFORMAT; 206 207 if (!parser->cached) { 208 dc_status_t rc = liquivision_lynx_parser_samples_foreach (abstract, NULL, NULL); 209 if (rc != DC_STATUS_SUCCESS) 210 return rc; 211 } 212 213 dc_gasmix_t *gasmix = (dc_gasmix_t *) value; 214 dc_tank_t *tank = (dc_tank_t *) value; 215 dc_salinity_t *water = (dc_salinity_t *) value; 216 dc_decomodel_t *decomodel = (dc_decomodel_t *) value; 217 218 if (value) { 219 switch (type) { 220 case DC_FIELD_DIVETIME: 221 *((unsigned int *) value) = array_uint32_le (abstract->data + 4); 222 break; 223 case DC_FIELD_MAXDEPTH: 224 *((double *) value) = array_uint16_le (abstract->data + 28) / 100.0; 225 break; 226 case DC_FIELD_AVGDEPTH: 227 *((double *) value) = array_uint16_le (abstract->data + 30) / 100.0; 228 break; 229 case DC_FIELD_TEMPERATURE_MINIMUM: 230 *((double *) value) = (signed short) array_uint16_le (abstract->data + 34) / 10.0; 231 break; 232 case DC_FIELD_TEMPERATURE_MAXIMUM: 233 *((double *) value) = (signed short) array_uint16_le (abstract->data + 36) / 10.0; 234 break; 235 case DC_FIELD_SALINITY: 236 switch (abstract->data[38]) { 237 case FRESH: 238 water->type = DC_WATER_FRESH; 239 water->density = 1000.0; 240 break; 241 case BRACKISH: 242 water->type = DC_WATER_SALT; 243 water->density = 1015.0; 244 break; 245 case SALT: 246 water->type = DC_WATER_SALT; 247 water->density = 1025.0; 248 break; 249 default: 250 return DC_STATUS_DATAFORMAT; 251 } 252 break; 253 case DC_FIELD_ATMOSPHERIC: 254 *((double *) value) = array_uint16_le (abstract->data + 26) / 1000.0; 255 break; 256 case DC_FIELD_DIVEMODE: 257 if (parser->model == XEN) { 258 *((unsigned int *) value) = DC_DIVEMODE_GAUGE; 259 } else { 260 switch (abstract->data[92] & 0x0F) { 261 case DECO: 262 case TEC: 263 case REC: 264 *((unsigned int *) value) = DC_DIVEMODE_OC; 265 break; 266 case GAUGE: 267 *((unsigned int *) value) = DC_DIVEMODE_GAUGE; 268 break; 269 default: 270 return DC_STATUS_DATAFORMAT; 271 } 272 } 273 break; 274 case DC_FIELD_DECOMODEL: 275 switch (abstract->data[93]) { 276 case ZHL16GF: 277 decomodel->type = DC_DECOMODEL_BUHLMANN; 278 decomodel->conservatism = 0; 279 decomodel->params.gf.low = 0; 280 decomodel->params.gf.high = 0; 281 break; 282 case RGBM: 283 decomodel->type = DC_DECOMODEL_RGBM; 284 decomodel->conservatism = 0; 285 break; 286 default: 287 return DC_STATUS_DATAFORMAT; 288 } 289 break; 290 case DC_FIELD_GASMIX_COUNT: 291 *((unsigned int *) value) = parser->ngasmixes; 292 break; 293 case DC_FIELD_GASMIX: 294 gasmix->usage = DC_USAGE_NONE; 295 gasmix->helium = parser->gasmix[flags].helium / 100.0; 296 gasmix->oxygen = parser->gasmix[flags].oxygen / 100.0; 297 gasmix->nitrogen = 1.0 - gasmix->oxygen - gasmix->helium; 298 break; 299 case DC_FIELD_TANK_COUNT: 300 *((unsigned int *) value) = parser->ntanks; 301 break; 302 case DC_FIELD_TANK: 303 tank->type = DC_TANKVOLUME_NONE; 304 tank->volume = 0.0; 305 tank->workpressure = 0.0; 306 tank->beginpressure = parser->tank[flags].beginpressure / 100.0; 307 tank->endpressure = parser->tank[flags].endpressure / 100.0; 308 tank->gasmix = DC_GASMIX_UNKNOWN; 309 tank->usage = DC_TANK_USAGE_NONE; 310 break; 311 default: 312 return DC_STATUS_UNSUPPORTED; 313 } 314 } 315 316 return DC_STATUS_SUCCESS; 317 } 318 319 static dc_status_t 320 liquivision_lynx_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata) 321 { 322 liquivision_lynx_parser_t *parser = (liquivision_lynx_parser_t *) abstract; 323 const unsigned char *data = abstract->data; 324 unsigned int size = abstract->size; 325 326 if (size < parser->headersize) 327 return DC_STATUS_DATAFORMAT; 328 329 // Get the version. 330 unsigned int version = array_uint32_le(data); 331 332 // Get the sample interval. 333 unsigned int interval_idx = data[39]; 334 const unsigned int intervals[] = {1, 2, 5, 10, 30, 60}; 335 if (interval_idx >= C_ARRAY_SIZE(intervals)) { 336 ERROR (abstract->context, "Invalid sample interval index %u", interval_idx); 337 return DC_STATUS_DATAFORMAT; 338 } 339 unsigned int interval = intervals[interval_idx]; 340 341 // Get the number of samples and events. 342 unsigned int nsamples = array_uint32_le (data + 8); 343 unsigned int nevents = array_uint32_le (data + 12); 344 345 unsigned int ngasmixes = 0; 346 unsigned int ntanks = 0; 347 liquivision_lynx_gasmix_t gasmix[NGASMIXES] = {0}; 348 liquivision_lynx_tank_t tank[NTANKS] = {0}; 349 unsigned int o2_previous = INVALID, he_previous = INVALID; 350 unsigned int gasmix_idx = INVALID; 351 unsigned int have_gasmix = 0; 352 unsigned int tank_id_previous = INVALID; 353 unsigned int tank_idx = INVALID; 354 unsigned int pressure[NTANKS] = {0}; 355 unsigned int have_pressure = 0; 356 unsigned int setpoint = 0, have_setpoint = 0; 357 unsigned int deco = 0, have_deco = 0; 358 359 unsigned int time = 0; 360 unsigned int samples = 0; 361 unsigned int events = 0; 362 unsigned int offset = parser->headersize; 363 while (offset + 2 <= size) { 364 dc_sample_value_t sample = {0}; 365 366 unsigned int value = array_uint16_le (data + offset); 367 offset += 2; 368 369 if (value & 0x8000) { 370 if (events >= nevents) { 371 break; 372 } 373 374 if (offset + 4 > size) { 375 ERROR (abstract->context, "Buffer overflow at offset %u", offset); 376 return DC_STATUS_DATAFORMAT; 377 } 378 379 unsigned int type = value & 0x7FFF; 380 unsigned int DC_ATTR_UNUSED timestamp = array_uint32_le (data + offset + 2); 381 offset += 4; 382 383 // Get the sample length. 384 unsigned int length = 0; 385 switch (type) { 386 case DECOSTOP: 387 case GASMIX: 388 length = 2; 389 break; 390 case SETPOINT: 391 length = 1; 392 break; 393 case TANK_LIST: 394 length = NTANKS * 2; 395 break; 396 case TANK_PRESSURE: 397 if (version == LYNX_V1) { 398 length = 4; 399 } else { 400 length = 6; 401 } 402 break; 403 default: 404 break; 405 } 406 407 if (offset + length > size) { 408 ERROR (abstract->context, "Buffer overflow at offset %u", offset); 409 return DC_STATUS_DATAFORMAT; 410 } 411 412 unsigned int o2 = 0, he = 0; 413 unsigned int tank_id = 0, tank_pressure = 0; 414 415 switch (type) { 416 case NORMAL: 417 case BOOKMARK: 418 case ALARM_DEPTH: 419 case ALARM_TIME: 420 case ALARM_VELOCITY: 421 case DECOSTOP_BREACHED: 422 case BAILOUT_ON: 423 case BAILOUT_OFF: 424 case EMERGENCY_ON: 425 case EMERGENCY_OFF: 426 case LOST_GAS: 427 case SAFETY_STOP: 428 break; 429 case DECOSTOP: 430 deco = array_uint16_le (data + offset); 431 have_deco = 1; 432 break; 433 case GASMIX: 434 o2 = data[offset + 0]; 435 he = data[offset + 1]; 436 if (o2 != o2_previous || he != he_previous) { 437 // Find the gasmix in the list. 438 unsigned int i = 0; 439 while (i < ngasmixes) { 440 if (o2 == gasmix[i].oxygen && he == gasmix[i].helium) 441 break; 442 i++; 443 } 444 445 // Add it to list if not found. 446 if (i >= ngasmixes) { 447 if (i >= NGASMIXES) { 448 ERROR (abstract->context, "Maximum number of gas mixes reached."); 449 return DC_STATUS_DATAFORMAT; 450 } 451 gasmix[i].oxygen = o2; 452 gasmix[i].helium = he; 453 ngasmixes = i + 1; 454 } 455 456 o2_previous = o2; 457 he_previous = he; 458 gasmix_idx = i; 459 have_gasmix = 1; 460 } 461 break; 462 case SETPOINT: 463 setpoint = data[offset]; 464 have_setpoint = 1; 465 break; 466 case TANK_PRESSURE: 467 tank_id = array_uint16_le (data + offset + 0); 468 tank_pressure = array_uint16_le (data + offset + 2); 469 if (tank_id != tank_id_previous) { 470 // Find the tank in the list. 471 unsigned int i = 0; 472 while (i < ntanks) { 473 if (tank_id == tank[i].id) 474 break; 475 i++; 476 } 477 478 // Add a new tank if necessary. 479 if (i >= ntanks) { 480 if (i >= NTANKS) { 481 ERROR (abstract->context, "Maximum number of tanks reached."); 482 return DC_STATUS_DATAFORMAT; 483 } 484 tank[i].id = tank_id; 485 tank[i].beginpressure = tank_pressure; 486 tank[i].endpressure = tank_pressure; 487 ntanks = i + 1; 488 } 489 490 tank_id_previous = tank_id; 491 tank_idx = i; 492 } 493 tank[tank_idx].endpressure = tank_pressure; 494 pressure[tank_idx] = tank_pressure; 495 have_pressure |= 1 << tank_idx; 496 break; 497 case TANK_LIST: 498 break; 499 default: 500 WARNING (abstract->context, "Unknown event %u", type); 501 break; 502 } 503 504 offset += length; 505 events++; 506 } else { 507 if (samples >= nsamples) { 508 break; 509 } 510 511 // Get the sample length. 512 unsigned int length = 2; 513 if (version == XEO_V1_A || version == XEO_V2_A || 514 version == XEO_V3_A || version == KAON_V1 || 515 version == KAON_V2) { 516 length += 14; 517 } 518 519 if (offset + length > size) { 520 ERROR (abstract->context, "Buffer overflow at offset %u", offset); 521 return DC_STATUS_DATAFORMAT; 522 } 523 524 // Time (seconds). 525 time += interval; 526 sample.time = time * 1000; 527 if (callback) callback (DC_SAMPLE_TIME, &sample, userdata); 528 529 // Depth (1/100 m). 530 sample.depth = value / 100.0; 531 if (callback) callback (DC_SAMPLE_DEPTH, &sample, userdata); 532 533 // Temperature (1/10 °C). 534 int temperature = (signed short) array_uint16_le (data + offset); 535 sample.temperature = temperature / 10.0; 536 if (callback) callback (DC_SAMPLE_TEMPERATURE, &sample, userdata); 537 538 // Gas mix 539 if (have_gasmix) { 540 sample.gasmix = gasmix_idx; 541 if (callback) callback (DC_SAMPLE_GASMIX, &sample, userdata); 542 have_gasmix = 0; 543 } 544 545 // Setpoint (1/10 bar). 546 if (have_setpoint) { 547 sample.setpoint = setpoint / 10.0; 548 if (callback) callback (DC_SAMPLE_SETPOINT, &sample, userdata); 549 have_setpoint = 0; 550 } 551 552 // Tank pressure (1/100 bar). 553 if (have_pressure) { 554 for (unsigned int i = 0; i < ntanks; ++i) { 555 if (have_pressure & (1 << i)) { 556 sample.pressure.tank = i; 557 sample.pressure.value = pressure[i] / 100.0; 558 if (callback) callback (DC_SAMPLE_PRESSURE, &sample, userdata); 559 } 560 } 561 have_pressure = 0; 562 } 563 564 // Deco/ndl 565 if (have_deco) { 566 if (deco) { 567 sample.deco.type = DC_DECO_DECOSTOP; 568 sample.deco.depth = deco / 100.0; 569 } else { 570 sample.deco.type = DC_DECO_NDL; 571 sample.deco.depth = 0.0; 572 } 573 sample.deco.time = 0; 574 sample.deco.tts = 0; 575 if (callback) callback (DC_SAMPLE_DECO, &sample, userdata); 576 have_deco = 0; 577 } 578 579 offset += length; 580 samples++; 581 } 582 } 583 584 // Cache the data for later use. 585 for (unsigned int i = 0; i < ntanks; ++i) { 586 parser->tank[i] = tank[i]; 587 } 588 for (unsigned int i = 0; i < ngasmixes; ++i) { 589 parser->gasmix[i] = gasmix[i]; 590 } 591 parser->ngasmixes = ngasmixes; 592 parser->ntanks = ntanks; 593 parser->cached = 1; 594 595 return DC_STATUS_SUCCESS; 596 }