cressi_goa_parser.c (17311B)
1 /* 2 * libdivecomputer 3 * 4 * Copyright (C) 2018 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 24 #include "cressi_goa.h" 25 #include "context-private.h" 26 #include "parser-private.h" 27 #include "checksum.h" 28 #include "array.h" 29 30 #define ISINSTANCE(parser) dc_device_isinstance((parser), &cressi_goa_parser_vtable) 31 32 #define DEPTH_SCUBA 0 33 #define DEPTH_FREE 1 34 #define SURFACE 2 35 #define TEMPERATURE 3 36 37 #define SCUBA 0 38 #define NITROX 1 39 #define FREEDIVE 2 40 #define GAUGE 3 41 #define FREEDIVE_ADV 5 42 43 #define NGASMIXES 3 44 #define NVERSIONS 6 45 #define NDIVEMODES 6 46 47 #define UNDEFINED 0xFFFFFFFF 48 49 typedef struct cressi_goa_parser_t cressi_goa_parser_t; 50 51 typedef struct cressi_goa_layout_t { 52 unsigned int headersize; 53 unsigned int nsamples; 54 unsigned int samplerate; 55 unsigned int datetime; 56 unsigned int divetime; 57 unsigned int gasmix[NGASMIXES]; 58 unsigned int atmospheric; 59 unsigned int maxdepth; 60 unsigned int avgdepth; 61 unsigned int temperature; 62 } cressi_goa_layout_t; 63 64 struct cressi_goa_parser_t { 65 dc_parser_t base; 66 const cressi_goa_layout_t *layout; 67 unsigned int headersize; 68 unsigned int version; 69 unsigned int divemode; 70 }; 71 72 static dc_status_t cressi_goa_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime); 73 static dc_status_t cressi_goa_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value); 74 static dc_status_t cressi_goa_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata); 75 76 static const dc_parser_vtable_t cressi_goa_parser_vtable = { 77 sizeof(cressi_goa_parser_t), 78 DC_FAMILY_CRESSI_GOA, 79 NULL, /* set_clock */ 80 NULL, /* set_atmospheric */ 81 NULL, /* set_density */ 82 cressi_goa_parser_get_datetime, /* datetime */ 83 cressi_goa_parser_get_field, /* fields */ 84 cressi_goa_parser_samples_foreach, /* samples_foreach */ 85 NULL /* destroy */ 86 }; 87 88 static const cressi_goa_layout_t scuba_nitrox_layout_v0 = { 89 90, /* headersize */ 90 10, /* nsamples */ 91 UNDEFINED, /* samplerate */ 92 12, /* datetime */ 93 20, /* divetime */ 94 { 26, 28, UNDEFINED }, /* gasmix */ 95 30, /* atmospheric */ 96 73, /* maxdepth */ 97 75, /* avgdepth */ 98 77, /* temperature */ 99 }; 100 101 static const cressi_goa_layout_t scuba_nitrox_layout_v1v2 = { 102 92, /* headersize */ 103 10, /* nsamples */ 104 UNDEFINED, /* samplerate */ 105 12, /* datetime */ 106 20, /* divetime */ 107 { 26, 28, UNDEFINED }, /* gasmix */ 108 30, /* atmospheric */ 109 73, /* maxdepth */ 110 75, /* avgdepth */ 111 77, /* temperature */ 112 }; 113 114 static const cressi_goa_layout_t scuba_nitrox_layout_v3 = { 115 92, /* headersize */ 116 10, /* nsamples */ 117 UNDEFINED, /* samplerate */ 118 12, /* datetime */ 119 20, /* divetime */ 120 { 26, 28, 87 }, /* gasmix */ 121 30, /* atmospheric */ 122 73, /* maxdepth */ 123 75, /* avgdepth */ 124 77, /* temperature */ 125 }; 126 127 static const cressi_goa_layout_t scuba_nitrox_layout_v4 = { 128 82, /* headersize */ 129 10, /* nsamples */ 130 2, /* samplerate */ 131 4, /* datetime */ 132 11, /* divetime */ 133 { 17, 19, 21 }, /* gasmix */ 134 23, /* atmospheric */ 135 66, /* maxdepth */ 136 68, /* avgdepth */ 137 70, /* temperature */ 138 }; 139 140 static const cressi_goa_layout_t scuba_nitrox_layout_v5 = { 141 83, /* headersize */ 142 2, /* nsamples */ 143 UNDEFINED, /* samplerate */ 144 4, /* datetime */ 145 11, /* divetime */ 146 { 17, 19, 21 }, /* gasmix */ 147 23, /* atmospheric */ 148 66, /* maxdepth */ 149 68, /* avgdepth */ 150 70, /* temperature */ 151 }; 152 153 static const cressi_goa_layout_t freedive_layout_v0 = { 154 34, /* headersize */ 155 10, /* nsamples */ 156 UNDEFINED, /* samplerate */ 157 12, /* datetime */ 158 20, /* divetime */ 159 { UNDEFINED, UNDEFINED, UNDEFINED }, /* gasmix */ 160 UNDEFINED, /* atmospheric */ 161 23, /* maxdepth */ 162 UNDEFINED, /* avgdepth */ 163 25, /* temperature */ 164 }; 165 166 static const cressi_goa_layout_t freedive_layout_v1v2v3 = { 167 38, /* headersize */ 168 10, /* nsamples */ 169 UNDEFINED, /* samplerate */ 170 12, /* datetime */ 171 20, /* divetime */ 172 { UNDEFINED, UNDEFINED, UNDEFINED }, /* gasmix */ 173 UNDEFINED, /* atmospheric */ 174 23, /* maxdepth */ 175 UNDEFINED, /* avgdepth */ 176 25, /* temperature */ 177 }; 178 179 static const cressi_goa_layout_t freedive_layout_v4 = { 180 27, /* headersize */ 181 2, /* nsamples */ 182 10, /* samplerate */ 183 4, /* datetime */ 184 11, /* divetime */ 185 { UNDEFINED, UNDEFINED, UNDEFINED }, /* gasmix */ 186 UNDEFINED, /* atmospheric */ 187 15, /* maxdepth */ 188 UNDEFINED, /* avgdepth */ 189 17, /* temperature */ 190 }; 191 192 static const cressi_goa_layout_t gauge_layout_v0 = { 193 38, /* headersize */ 194 10, /* nsamples */ 195 UNDEFINED, /* samplerate */ 196 12, /* datetime */ 197 20, /* divetime */ 198 { UNDEFINED, UNDEFINED, UNDEFINED }, /* gasmix */ 199 22, /* atmospheric */ 200 24, /* maxdepth */ 201 26, /* avgdepth */ 202 28, /* temperature */ 203 }; 204 205 static const cressi_goa_layout_t gauge_layout_v1v2v3 = { 206 40, /* headersize */ 207 10, /* nsamples */ 208 UNDEFINED, /* samplerate */ 209 12, /* datetime */ 210 20, /* divetime */ 211 { UNDEFINED, UNDEFINED, UNDEFINED }, /* gasmix */ 212 22, /* atmospheric */ 213 24, /* maxdepth */ 214 26, /* avgdepth */ 215 28, /* temperature */ 216 }; 217 218 static const cressi_goa_layout_t gauge_layout_v4 = { 219 28, /* headersize */ 220 2, /* nsamples */ 221 10, /* samplerate */ 222 4, /* datetime */ 223 11, /* divetime */ 224 { UNDEFINED, UNDEFINED, UNDEFINED }, /* gasmix */ 225 13, /* atmospheric */ 226 15, /* maxdepth */ 227 17, /* avgdepth */ 228 19, /* temperature */ 229 }; 230 231 static const cressi_goa_layout_t advanced_freedive_layout_v4 = { 232 28, /* headersize */ 233 2, /* nsamples */ 234 10, /* samplerate */ 235 4, /* datetime */ 236 22, /* divetime */ 237 { UNDEFINED, UNDEFINED, UNDEFINED }, /* gasmix */ 238 UNDEFINED, /* atmospheric */ 239 16, /* maxdepth */ 240 UNDEFINED, /* avgdepth */ 241 18, /* temperature */ 242 }; 243 244 static const cressi_goa_layout_t * const layouts[NVERSIONS][NDIVEMODES] = { 245 { 246 &scuba_nitrox_layout_v0, /* SCUBA */ 247 &scuba_nitrox_layout_v0, /* NITROX */ 248 &freedive_layout_v0, /* FREEDIVE */ 249 &gauge_layout_v0, /* GAUGE */ 250 NULL, /* UNUSED */ 251 NULL, /* FREEDIVE_ADV */ 252 }, 253 { 254 &scuba_nitrox_layout_v1v2, /* SCUBA */ 255 &scuba_nitrox_layout_v1v2, /* NITROX */ 256 &freedive_layout_v1v2v3, /* FREEDIVE */ 257 &gauge_layout_v1v2v3, /* GAUGE */ 258 NULL, /* UNUSED */ 259 NULL, /* FREEDIVE_ADV */ 260 }, 261 { 262 &scuba_nitrox_layout_v1v2, /* SCUBA */ 263 &scuba_nitrox_layout_v1v2, /* NITROX */ 264 &freedive_layout_v1v2v3, /* FREEDIVE */ 265 &gauge_layout_v1v2v3, /* GAUGE */ 266 NULL, /* UNUSED */ 267 NULL, /* FREEDIVE_ADV */ 268 }, 269 { 270 &scuba_nitrox_layout_v3, /* SCUBA */ 271 &scuba_nitrox_layout_v3, /* NITROX */ 272 &freedive_layout_v1v2v3, /* FREEDIVE */ 273 &gauge_layout_v1v2v3, /* GAUGE */ 274 NULL, /* UNUSED */ 275 NULL, /* FREEDIVE_ADV */ 276 }, 277 { 278 &scuba_nitrox_layout_v4, /* SCUBA */ 279 &scuba_nitrox_layout_v4, /* NITROX */ 280 &freedive_layout_v4, /* FREEDIVE */ 281 &gauge_layout_v4, /* GAUGE */ 282 NULL, /* UNUSED */ 283 &advanced_freedive_layout_v4, /* FREEDIVE_ADV */ 284 }, 285 { 286 &scuba_nitrox_layout_v5, /* SCUBA */ 287 &scuba_nitrox_layout_v5, /* NITROX */ 288 &freedive_layout_v4, /* FREEDIVE */ 289 &gauge_layout_v4, /* GAUGE */ 290 NULL, /* UNUSED */ 291 NULL, /* FREEDIVE_ADV */ 292 }, 293 }; 294 295 static dc_status_t 296 cressi_goa_init(cressi_goa_parser_t *parser) 297 { 298 dc_parser_t *abstract = (dc_parser_t *) parser; 299 const unsigned char *data = abstract->data; 300 unsigned int size = abstract->size; 301 302 if (size < 2) { 303 ERROR (abstract->context, "Invalid dive length (%u).", size); 304 return DC_STATUS_DATAFORMAT; 305 } 306 307 unsigned int id_len = data[0]; 308 unsigned int logbook_len = data[1]; 309 if (id_len < 9 || logbook_len < 15) { 310 ERROR (abstract->context, "Invalid id or logbook length (%u %u).", id_len, logbook_len); 311 return DC_STATUS_DATAFORMAT; 312 } 313 314 if (size < 2 + id_len + logbook_len) { 315 ERROR (abstract->context, "Invalid dive length (%u).", size); 316 return DC_STATUS_DATAFORMAT; 317 } 318 319 const unsigned char *id = data + 2; 320 const unsigned char *logbook = data + 2 + id_len; 321 322 // Get the data format version. 323 unsigned int version = 0; 324 unsigned int firmware = array_uint16_le (id + 5); 325 if (id_len == 11) { 326 version = array_uint16_le (id + 9); 327 } else { 328 if (firmware >= 161 && firmware <= 165) { 329 version = 0; 330 } else if (firmware >= 166 && firmware <= 169) { 331 version = 1; 332 } else if (firmware >= 170 && firmware <= 179) { 333 version = 2; 334 } else if (firmware >= 100 && firmware <= 110) { 335 version = 3; 336 } else if (firmware >= 200 && firmware <= 299) { 337 version = 4; 338 } else if (firmware >= 300) { 339 version = 5; 340 } else { 341 ERROR (abstract->context, "Unknown firmware version (%u).", firmware); 342 return DC_STATUS_DATAFORMAT; 343 } 344 } 345 if (version >= NVERSIONS) { 346 ERROR (abstract->context, "Invalid data format version (%u).", version); 347 return DC_STATUS_DATAFORMAT; 348 } 349 350 // Get the dive mode. 351 unsigned int divemode = logbook[2]; 352 if (divemode >= NDIVEMODES) { 353 ERROR (abstract->context, "Invalid dive mode (%u).", divemode); 354 return DC_STATUS_DATAFORMAT; 355 } 356 357 // Get the layout. 358 const cressi_goa_layout_t *layout = layouts[version][divemode]; 359 if (layout == NULL) { 360 ERROR (abstract->context, "Unsupported dive mode for data format version %u (%u).", version, divemode); 361 return DC_STATUS_DATAFORMAT; 362 } 363 364 unsigned int headersize = 2 + id_len + logbook_len; 365 if (size < headersize + layout->headersize) { 366 ERROR (abstract->context, "Invalid dive length (%u).", size); 367 return DC_STATUS_DATAFORMAT; 368 } 369 370 unsigned short crc = array_uint16_le (data + headersize + layout->headersize - 2); 371 unsigned short ccrc = checksum_crc16_ccitt (data + headersize, layout->headersize - 2, 0xffff, 0x0000); 372 if (crc != ccrc) { 373 ERROR (abstract->context, "Unexpected header checksum (%04x %04x).", crc, ccrc); 374 return DC_STATUS_DATAFORMAT; 375 } 376 377 parser->layout = layout; 378 parser->headersize = headersize; 379 parser->divemode = divemode; 380 parser->version = version; 381 382 return DC_STATUS_SUCCESS; 383 } 384 385 dc_status_t 386 cressi_goa_parser_create (dc_parser_t **out, dc_context_t *context, const unsigned char data[], size_t size) 387 { 388 dc_status_t status = DC_STATUS_SUCCESS; 389 cressi_goa_parser_t *parser = NULL; 390 391 if (out == NULL) 392 return DC_STATUS_INVALIDARGS; 393 394 // Allocate memory. 395 parser = (cressi_goa_parser_t *) dc_parser_allocate (context, &cressi_goa_parser_vtable, data, size); 396 if (parser == NULL) { 397 ERROR (context, "Failed to allocate memory."); 398 return DC_STATUS_NOMEMORY; 399 } 400 401 status = cressi_goa_init(parser); 402 if (status != DC_STATUS_SUCCESS) 403 goto error_free; 404 405 *out = (dc_parser_t*) parser; 406 407 return DC_STATUS_SUCCESS; 408 409 error_free: 410 dc_parser_deallocate ((dc_parser_t *) parser); 411 return status; 412 } 413 414 static dc_status_t 415 cressi_goa_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime) 416 { 417 cressi_goa_parser_t *parser = (cressi_goa_parser_t *) abstract; 418 419 const unsigned char *p = abstract->data + parser->headersize + parser->layout->datetime; 420 421 if (datetime) { 422 datetime->year = array_uint16_le(p); 423 datetime->month = p[2]; 424 datetime->day = p[3]; 425 datetime->hour = p[4]; 426 datetime->minute = p[5]; 427 datetime->second = 0; 428 datetime->timezone = DC_TIMEZONE_NONE; 429 } 430 431 return DC_STATUS_SUCCESS; 432 } 433 434 static dc_status_t 435 cressi_goa_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value) 436 { 437 cressi_goa_parser_t *parser = (cressi_goa_parser_t *) abstract; 438 const cressi_goa_layout_t *layout = parser->layout; 439 const unsigned char *data = abstract->data + parser->headersize; 440 441 unsigned int ngasmixes = 0; 442 for (unsigned int i = 0; i < NGASMIXES; ++i) { 443 if (layout->gasmix[i] == UNDEFINED) 444 break; 445 if (data[layout->gasmix[i] + 1] == 0) 446 break; 447 ngasmixes++; 448 } 449 450 dc_gasmix_t *gasmix = (dc_gasmix_t *) value; 451 452 if (value) { 453 switch (type) { 454 case DC_FIELD_DIVETIME: 455 if (layout->divetime == UNDEFINED) 456 return DC_STATUS_UNSUPPORTED; 457 *((unsigned int *) value) = array_uint16_le (data + layout->divetime); 458 break; 459 case DC_FIELD_MAXDEPTH: 460 if (layout->maxdepth == UNDEFINED) 461 return DC_STATUS_UNSUPPORTED; 462 *((double *) value) = array_uint16_le (data + layout->maxdepth) / 10.0; 463 break; 464 case DC_FIELD_AVGDEPTH: 465 if (layout->avgdepth == UNDEFINED) 466 return DC_STATUS_UNSUPPORTED; 467 *((double *) value) = array_uint16_le (data + layout->avgdepth) / 10.0; 468 break; 469 case DC_FIELD_TEMPERATURE_MINIMUM: 470 if (layout->temperature == UNDEFINED) 471 return DC_STATUS_UNSUPPORTED; 472 *((double *) value) = array_uint16_le (data + layout->temperature) / 10.0; 473 break; 474 case DC_FIELD_ATMOSPHERIC: 475 if (layout->atmospheric == UNDEFINED) 476 return DC_STATUS_UNSUPPORTED; 477 *((double *) value) = array_uint16_le (data + layout->atmospheric) / 1000.0; 478 break; 479 case DC_FIELD_GASMIX_COUNT: 480 *((unsigned int *) value) = ngasmixes; 481 break; 482 case DC_FIELD_GASMIX: 483 if (ngasmixes <= flags) 484 return DC_STATUS_INVALIDARGS; 485 gasmix->usage = DC_USAGE_NONE; 486 gasmix->helium = 0.0; 487 gasmix->oxygen = data[layout->gasmix[flags] + 1] / 100.0; 488 gasmix->nitrogen = 1.0 - gasmix->oxygen - gasmix->helium; 489 break; 490 case DC_FIELD_DIVEMODE: 491 switch (parser->divemode) { 492 case SCUBA: 493 case NITROX: 494 *((dc_divemode_t *) value) = DC_DIVEMODE_OC; 495 break; 496 case GAUGE: 497 *((dc_divemode_t *) value) = DC_DIVEMODE_GAUGE; 498 break; 499 case FREEDIVE: 500 case FREEDIVE_ADV: 501 *((dc_divemode_t *) value) = DC_DIVEMODE_FREEDIVE; 502 break; 503 default: 504 return DC_STATUS_DATAFORMAT; 505 } 506 break; 507 default: 508 return DC_STATUS_UNSUPPORTED; 509 } 510 } 511 512 return DC_STATUS_SUCCESS; 513 } 514 515 static dc_status_t 516 cressi_goa_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata) 517 { 518 cressi_goa_parser_t *parser = (cressi_goa_parser_t *) abstract; 519 const cressi_goa_layout_t *layout = parser->layout; 520 const unsigned char *data = abstract->data + parser->headersize; 521 unsigned int size = abstract->size - parser->headersize; 522 523 unsigned int interval = parser->divemode == FREEDIVE ? 2000 : 5000; 524 if (layout->samplerate != UNDEFINED) { 525 const unsigned int samplerates[] = { 526 500, 1000, 2000, 5000 527 }; 528 unsigned int samplerate = data[layout->samplerate]; 529 if (samplerate == 0 || samplerate > C_ARRAY_SIZE(samplerates)) { 530 ERROR (abstract->context, "Unknown sample rate index (%u).", samplerate); 531 return DC_STATUS_DATAFORMAT; 532 } 533 interval = samplerates[samplerate - 1]; 534 } 535 536 // In advanced freedive mode, there is an extra header present after the 537 // samples containing the advanced freedive dip stats. 538 unsigned int trailer = parser->divemode == FREEDIVE_ADV ? 13 : 0; 539 540 unsigned int nsamples = array_uint16_le (data + layout->nsamples); 541 542 unsigned int time = 0; 543 unsigned int depth = 0; 544 unsigned int depth_mask = ((parser->version < 4) ? 0x07FF : 0x0FFF); 545 unsigned int gasmix = 0, gasmix_previous = 0xFFFFFFFF; 546 unsigned int gasmix_mask = ((parser->version < 3) ? 0x0800 : 0x1800); 547 unsigned int temperature = 0; 548 unsigned int have_temperature = 0; 549 unsigned int complete = 0; 550 551 unsigned int offset = layout->headersize; 552 for (unsigned int i = 0; i < nsamples; ++i) { 553 dc_sample_value_t sample = {0}; 554 555 if (offset + 2 + trailer > size) { 556 ERROR (abstract->context, "Buffer overflow detected!"); 557 return DC_STATUS_DATAFORMAT; 558 } 559 560 // Get the sample type and value. 561 unsigned int raw = array_uint16_le (data + offset); 562 unsigned int type = (raw & 0x0003); 563 unsigned int value = (raw & 0xFFFC) >> 2; 564 565 if (type == DEPTH_SCUBA) { 566 depth = value & 0x07FF; 567 gasmix = (value & gasmix_mask) >> 11; 568 time += interval; 569 complete = 1; 570 } else if (type == DEPTH_FREE) { 571 depth = value & depth_mask; 572 time += interval; 573 complete = 1; 574 } else if (type == TEMPERATURE) { 575 temperature = value; 576 have_temperature = 1; 577 } else if (type == SURFACE) { 578 unsigned int surftime = value * 1000; 579 if (surftime > interval) { 580 surftime -= interval; 581 time += interval; 582 583 // Time (seconds). 584 sample.time = time; 585 if (callback) callback (DC_SAMPLE_TIME, &sample, userdata); 586 // Depth (1/10 m). 587 sample.depth = 0.0; 588 if (callback) callback (DC_SAMPLE_DEPTH, &sample, userdata); 589 } 590 time += surftime; 591 depth = 0; 592 complete = 1; 593 } 594 595 if (complete) { 596 // Time (seconds). 597 sample.time = time; 598 if (callback) callback (DC_SAMPLE_TIME, &sample, userdata); 599 600 // Temperature (1/10 °C). 601 if (have_temperature) { 602 sample.temperature = temperature / 10.0; 603 if (callback) callback (DC_SAMPLE_TEMPERATURE, &sample, userdata); 604 have_temperature = 0; 605 } 606 607 // Depth (1/10 m). 608 sample.depth = depth / 10.0; 609 if (callback) callback (DC_SAMPLE_DEPTH, &sample, userdata); 610 611 // Gas change 612 if (parser->divemode == SCUBA || parser->divemode == NITROX) { 613 if (gasmix != gasmix_previous) { 614 sample.gasmix = gasmix; 615 if (callback) callback (DC_SAMPLE_GASMIX, &sample, userdata); 616 gasmix_previous = gasmix; 617 } 618 } 619 620 complete = 0; 621 } 622 623 offset += 2; 624 } 625 626 return DC_STATUS_SUCCESS; 627 }