garmin_parser.c (60411B)
1 /* 2 * Garmin Descent Mk1 parsing 3 * 4 * Copyright (C) 2018 Linus Torvalds 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 <stdio.h> 24 #include <stdarg.h> 25 #include <string.h> 26 27 #include "garmin.h" 28 #include "garmin-models.h" 29 #include "context-private.h" 30 #include "parser-private.h" 31 #include "array.h" 32 #include "field-cache.h" 33 34 #define MAXFIELDS 160 35 36 struct msg_desc; 37 38 // Local types 39 struct type_desc { 40 const char *msg_name; 41 const struct msg_desc *msg_desc; 42 unsigned char nrfields, devfields; 43 unsigned char fields[MAXFIELDS][3]; 44 }; 45 46 // Positions are signed 32-bit values, turning 47 // into 180 * val // 2**31 degrees. 48 struct pos { 49 int lat, lon; 50 }; 51 52 #define MAX_SENSORS 6 53 struct garmin_sensor { 54 unsigned int sensor_id; 55 const char *sensor_name; 56 unsigned char sensor_enabled, sensor_units, sensor_used_for_gas_rate; 57 // SENSOR_PROFILE field 78 (used_for_gas_rate) is only decoded when 58 // the log actually contains it: the field-decoding machinery drops 59 // absent/invalid (0xff) values before the handler runs, and this 60 // whole struct is memset to zero otherwise. So a raw 0 is ambiguous 61 // between "device said not counted" and "device said nothing", and 62 // we need this separate flag to tell the two apart. 63 unsigned char sensor_used_for_gas_rate_valid; 64 unsigned int sensor_rated_pressure, sensor_reserve_pressure, sensor_volume; 65 }; 66 67 #define MAXTYPE 16 68 #define MAXGASES 16 69 #define MAXSTRINGS 32 70 71 // Some record data needs to be bunched up 72 // and sent together. 73 struct record_data { 74 unsigned int pending; 75 unsigned int time; 76 unsigned int timestamp; 77 78 // RECORD_DECO 79 int stop_time; 80 double ceiling; 81 82 // RECORD_GASMIX 83 int index, gas_status; 84 dc_gasmix_t gasmix; 85 86 // RECORD_EVENT 87 unsigned char event_type, event_nr, event_group; 88 unsigned int event_data, event_unknown; 89 90 // RECORD_DEVICE_INFO 91 unsigned int device_index, firmware, serial, product; 92 93 // RECORD_DECO_MODEL 94 unsigned char model, gf_low, gf_high; 95 96 // RECORD_SENSOR_PROFILE has no data, fills in dive.sensor[nr_sensor] 97 98 // RECORD_TANK_UPDATE 99 unsigned int sensor, pressure; 100 101 // RECORD_SETPOINT_CHANGE 102 unsigned int setpoint_actual_cbar; 103 }; 104 105 #define RECORD_GASMIX 1 106 #define RECORD_DECO 2 107 #define RECORD_EVENT 4 108 #define RECORD_DEVICE_INFO 8 109 #define RECORD_DECO_MODEL 16 110 #define RECORD_SENSOR_PROFILE 32 111 #define RECORD_TANK_UPDATE 64 112 #define RECORD_SETPOINT_CHANGE 128 113 114 typedef struct garmin_parser_t { 115 dc_parser_t base; 116 117 dc_sample_callback_t callback; 118 void *userdata; 119 120 // Multi-value record data 121 struct record_data record_data; 122 123 struct type_desc type_desc[MAXTYPE]; 124 125 // Field cache 126 struct { 127 unsigned int sub_sport; 128 unsigned int serial; 129 unsigned int product; 130 unsigned int firmware; 131 unsigned int protocol; 132 unsigned int profile; 133 unsigned int time; 134 int utc_offset, time_offset; 135 unsigned int nr_sensor; 136 struct garmin_sensor sensor[MAX_SENSORS]; 137 unsigned int setpoint_low_cbar, setpoint_high_cbar; 138 unsigned int setpoint_low_switch_depth_mm, setpoint_high_switch_depth_mm; 139 unsigned int setpoint_low_switch_mode, setpoint_high_switch_mode; 140 } dive; 141 142 // I count nine (!) different GPS fields Hmm. 143 // Reporting all of them just to try to figure 144 // out what is what. 145 struct { 146 struct { 147 struct pos entry, exit; 148 struct pos NE, SW; // NE, SW corner 149 } SESSION; 150 struct { 151 struct pos entry, exit; 152 struct pos some, other; 153 } LAP; 154 struct pos RECORD; 155 } gps; 156 157 struct dc_field_cache cache; 158 unsigned char is_big_endian; // instead of bool 159 } garmin_parser_t; 160 161 typedef int (*garmin_data_cb_t)(unsigned char type, const unsigned char *data, int len, void *user); 162 163 static inline struct garmin_sensor *current_sensor(garmin_parser_t *garmin) 164 { 165 return garmin->dive.sensor + garmin->dive.nr_sensor; 166 } 167 168 static int find_tank_index(garmin_parser_t *garmin, unsigned int sensor_id) 169 { 170 for (int i = 0; i < garmin->dive.nr_sensor; i++) { 171 if (garmin->dive.sensor[i].sensor_id == sensor_id) 172 return i; 173 } 174 return 0; 175 } 176 177 /* 178 * Decode the event. Numbers from Wojtek's fit2subs python script 179 */ 180 static void garmin_event(struct garmin_parser_t *garmin, 181 unsigned char event, unsigned char type, unsigned char group, 182 unsigned int data, unsigned int unknown) 183 { 184 static const struct { 185 // 1 - state, 2 - notify, 3 - warning, 4 - alarm 186 int severity; 187 const char *name; 188 } event_desc[] = { 189 [0] = { 2, "Deco required" }, 190 [1] = { 2, "Gas Switch prompted" }, 191 [2] = { 1, "Surface" }, 192 [3] = { 2, "Approaching NDL" }, 193 [4] = { 3, "ppO2 warning" }, 194 [5] = { 4, "ppO2 critical high" }, 195 [6] = { 4, "ppO2 critical low" }, 196 [7] = { 2, "Time alert" }, 197 [8] = { 2, "Depth alert" }, 198 [9] = { 3, "Deco ceiling broken" }, 199 [10] = { 1, "Deco completed" }, 200 [11] = { 3, "Safety stop ceiling broken" }, 201 [12] = { 1, "Safety stop completed" }, 202 [13] = { 3, "CNS warning" }, 203 [14] = { 4, "CNS critical" }, 204 [15] = { 3, "OTU warning" }, 205 [16] = { 4, "OTU critical" }, 206 [17] = { 3, "Ascent speed critical" }, 207 [18] = { 1, "Alert dismissed" }, 208 [19] = { 1, "Alert timed out" }, 209 [20] = { 3, "Battry Low" }, 210 [21] = { 3, "Battry Critical" }, 211 [22] = { 1, "Safety stop begin" }, 212 [23] = { 1, "Approaching deco stop" }, 213 [24] = { 1, "Automatic switch to low setpoint" }, 214 [25] = { 1, "Automatic switch to high setpoint" }, 215 [26] = { 2, "Manual switch to low setpoint" }, 216 [27] = { 2, "Manual switch to high setpoint" }, 217 [32] = { 1, "Tank battery low" }, // No way to know which tank 218 }; 219 dc_sample_value_t sample = {0}; 220 221 switch (event) { 222 case 38: 223 break; 224 case 48: 225 break; 226 case 56: 227 if (data >= C_ARRAY_SIZE(event_desc)) 228 return; 229 230 sample.event.type = SAMPLE_EVENT_STRING; 231 sample.event.name = event_desc[data].name; 232 sample.event.flags = event_desc[data].severity << SAMPLE_FLAGS_SEVERITY_SHIFT; 233 234 if (data == 24 || data == 25 || data == 26 || data == 27) { 235 // Update the actual setpoint used during the dive and report it 236 garmin->record_data.setpoint_actual_cbar = (data == 24 || data == 26) ? garmin->dive.setpoint_low_cbar : garmin->dive.setpoint_high_cbar; 237 garmin->record_data.pending |= RECORD_SETPOINT_CHANGE; 238 } 239 240 if (!sample.event.name) 241 return; 242 garmin->callback(DC_SAMPLE_EVENT, &sample, garmin->userdata); 243 return; 244 245 case 57: 246 sample.gasmix = data; 247 garmin->callback(DC_SAMPLE_GASMIX, &sample, garmin->userdata); 248 249 return; 250 } 251 } 252 253 /* 254 * Some data isn't just something we can save off directly: it's a record with 255 * multiple fields where one field describes another. 256 * 257 * The solution is to just batch it up in the "garmin->record_data", and then 258 * this function gets called at the end of a record. 259 */ 260 static void flush_pending_record(struct garmin_parser_t *garmin) 261 { 262 struct record_data *record = &garmin->record_data; 263 unsigned int pending = record->pending; 264 265 record->pending = 0; 266 if (!garmin->callback) { 267 if (pending & RECORD_GASMIX) { 268 // 0 - disabled, 1 - enabled, 2 - backup 269 int enabled = record->gas_status > 0; 270 int index = record->index; 271 if (enabled && index < MAXGASES) { 272 DC_ASSIGN_IDX(garmin->cache, GASMIX, index, record->gasmix); 273 DC_ASSIGN_FIELD(garmin->cache, GASMIX_COUNT, index + 1); 274 } 275 } 276 if (pending & RECORD_DEVICE_INFO && record->device_index == 0) { 277 garmin->dive.firmware = record->firmware; 278 garmin->dive.serial = record->serial; 279 garmin->dive.product = record->product; 280 } 281 if (pending & RECORD_DECO_MODEL) 282 dc_field_add_string_fmt(&garmin->cache, "Deco model", "Buhlmann ZHL-16C %u/%u", record->gf_low, record->gf_high); 283 284 // End of sensor record just increments nr_sensor, 285 // so that the next sensor record will start 286 // filling in the next one. 287 // 288 // NOTE! This only happens for tank pods, other 289 // sensors will just overwrite each other. 290 // 291 // Also note that the last sensor is just for 292 // scratch use, so that the sensor record can 293 // always fill in dive.sensor[nr_sensor] with 294 // no checking. 295 if (pending & RECORD_SENSOR_PROFILE) { 296 if (garmin->dive.nr_sensor < MAX_SENSORS-1) 297 garmin->dive.nr_sensor++; 298 } 299 300 return; 301 } 302 303 if (pending & RECORD_DECO) { 304 dc_sample_value_t sample = {0}; 305 sample.deco.type = DC_DECO_DECOSTOP; 306 sample.deco.time = record->stop_time; 307 sample.deco.depth = record->ceiling; 308 sample.deco.tts = 0; 309 garmin->callback(DC_SAMPLE_DECO, &sample, garmin->userdata); 310 } 311 312 if (pending & RECORD_EVENT) { 313 garmin_event(garmin, record->event_nr, record->event_type, 314 record->event_group, record->event_data, record->event_unknown); 315 } 316 317 if (pending & RECORD_TANK_UPDATE) { 318 dc_sample_value_t sample = {0}; 319 320 sample.pressure.tank = find_tank_index(garmin, record->sensor); 321 sample.pressure.value = record->pressure / 100.0; 322 garmin->callback(DC_SAMPLE_PRESSURE, &sample, garmin->userdata); 323 } 324 325 if (pending & RECORD_SETPOINT_CHANGE) { 326 dc_sample_value_t sample = {0}; 327 328 sample.setpoint = record->setpoint_actual_cbar / 100.0; 329 garmin->callback(DC_SAMPLE_SETPOINT, &sample, garmin->userdata); 330 } 331 } 332 333 334 static dc_status_t garmin_parser_set_data (garmin_parser_t *garmin, const unsigned char *data, unsigned int size); 335 static dc_status_t garmin_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime); 336 static dc_status_t garmin_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value); 337 static dc_status_t garmin_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata); 338 static dc_status_t garmin_parser_destroy (dc_parser_t *abstract); 339 340 static const dc_parser_vtable_t garmin_parser_vtable = { 341 sizeof(garmin_parser_t), 342 DC_FAMILY_GARMIN, 343 NULL, /* set_clock */ 344 NULL, /* set_atmospheric */ 345 NULL, /* set_density */ 346 garmin_parser_get_datetime, /* datetime */ 347 garmin_parser_get_field, /* fields */ 348 garmin_parser_samples_foreach, /* samples_foreach */ 349 garmin_parser_destroy /* destroy */ 350 }; 351 352 static dc_status_t 353 garmin_parser_destroy (dc_parser_t *abstract) 354 { 355 garmin_parser_t *garmin = (garmin_parser_t *) abstract; 356 357 dc_field_cache_free (&garmin->cache); 358 359 return DC_STATUS_SUCCESS; 360 } 361 362 dc_status_t 363 garmin_parser_create (dc_parser_t **out, dc_context_t *context, const unsigned char data[], size_t size) 364 { 365 garmin_parser_t *parser = NULL; 366 367 if (out == NULL) 368 return DC_STATUS_INVALIDARGS; 369 370 // Allocate memory. 371 parser = (garmin_parser_t *) dc_parser_allocate (context, &garmin_parser_vtable, data, size); 372 if (parser == NULL) { 373 ERROR (context, "Failed to allocate memory."); 374 return DC_STATUS_NOMEMORY; 375 } 376 377 memset(&parser->cache, 0, sizeof(parser->cache)); 378 379 garmin_parser_set_data(parser, data, size); 380 381 *out = (dc_parser_t *) parser; 382 383 return DC_STATUS_SUCCESS; 384 } 385 386 /* 387 * We really shouldn't use array_uint_be/le, since they 388 * can't deal with 64-bit types. 389 * 390 * But we've not actually seen any yet, so.. 391 */ 392 static inline unsigned long long garmin_value(struct garmin_parser_t *g, const unsigned char *p, unsigned int type_size) 393 { 394 if (g->is_big_endian) 395 return array_uint_be(p, type_size); 396 else 397 return array_uint_le(p, type_size); 398 } 399 400 #define FMTSIZE 64 401 402 #define DECLARE_FIT_TYPE(name, ctype, inval, fmt) \ 403 typedef ctype name; \ 404 static const name name##_INVAL = inval; \ 405 static name name##_VALUE(garmin_parser_t *g, const void *p) \ 406 { return (name) garmin_value(g, p, sizeof(name)); } \ 407 static void name##_FORMAT(name val, char *buf) \ 408 { snprintf(buf, FMTSIZE, fmt, val); } 409 410 DECLARE_FIT_TYPE(ENUM, unsigned char, 0xff, "%u"); 411 DECLARE_FIT_TYPE(UINT8, unsigned char, 0xff, "%u"); 412 DECLARE_FIT_TYPE(UINT16, unsigned short, 0xffff, "%u"); 413 DECLARE_FIT_TYPE(UINT32, unsigned int, 0xffffffff, "%u"); 414 DECLARE_FIT_TYPE(UINT64, unsigned long long, 0xffffffffffffffffull, "%llu"); 415 416 DECLARE_FIT_TYPE(UINT8Z, unsigned char, 0, "%u"); 417 DECLARE_FIT_TYPE(UINT16Z, unsigned short, 0, "%u"); 418 DECLARE_FIT_TYPE(UINT32Z, unsigned int, 0, "%u"); 419 420 DECLARE_FIT_TYPE(SINT8, signed char, 0x7f, "%d"); 421 DECLARE_FIT_TYPE(SINT16, signed short, 0x7fff, "%d"); 422 DECLARE_FIT_TYPE(SINT32, signed int, 0x7fffffff, "%d"); 423 DECLARE_FIT_TYPE(SINT64, signed long long, 0x7fffffffffffffffll, "%lld"); 424 425 DECLARE_FIT_TYPE(FLOAT, unsigned int, 0xffffffff, "%u"); 426 DECLARE_FIT_TYPE(DOUBLE, unsigned long long, 0xffffffffffffffffll, "%llu"); 427 DECLARE_FIT_TYPE(STRING, char *, NULL, "\"%s\""); 428 429 // Override string value function - it's the pointer itself 430 #define STRING_VALUE(g, p) ((char *)(p)) 431 432 static const struct { 433 const char *type_name; 434 int type_size; 435 unsigned long long type_inval; 436 } base_type_info[17] = { 437 { "ENUM", 1, 0xff }, 438 { "SINT8", 1, 0x7f }, 439 { "UINT8", 1, 0xff }, 440 { "SINT16", 2, 0x7fff }, 441 { "UINT16", 2, 0xffff }, 442 { "SINT32", 4, 0x7fffffff }, 443 { "UINT32", 4, 0xffffffff }, 444 { "STRING", 1, 0 }, 445 { "FLOAT", 4, 0xffffffff }, 446 { "DOUBLE", 8, 0xfffffffffffffffful }, 447 { "UINT8Z", 1, 0x00 }, 448 { "UINT16Z", 2, 0x0000 }, 449 { "UINT32Z", 4, 0x00000000 }, 450 { "BYTE", 1, 0xff }, 451 { "SINT64", 8, 0x7fffffffffffffff }, 452 { "UINT64", 8, 0xffffffffffffffff }, 453 { "UINT64Z", 8, 0x0000000000000000 }, 454 }; 455 456 /* 457 * Garmin FIT events are described by tuples of "global mesg ID" and 458 * a "field number". There's lots of them, because you have events 459 * for pretty much anything ("cycling gear change") etc. 460 * 461 * There's a SDK that generates tables for you, but it looks nasty. 462 * 463 * So instead, we try to make sense of it manually. 464 */ 465 struct field_desc { 466 const char *name; 467 void (*parse)(struct garmin_parser_t *, unsigned char base_type, const unsigned char *data); 468 }; 469 470 #define DECLARE_FIELD(msg, name, type) __DECLARE_FIELD(msg##_##name, type) 471 #define __DECLARE_FIELD(name, type) \ 472 static void parse_##name(struct garmin_parser_t *, const type); \ 473 static void parse_##name##_##type(struct garmin_parser_t *g, unsigned char base_type, const unsigned char *p) \ 474 { \ 475 char fmtbuf[FMTSIZE]; \ 476 if (strcmp(#type, base_type_info[base_type].type_name)) \ 477 WARNING(g->base.context, "%s: %s should be %s\n", #name, #type, base_type_info[base_type].type_name); \ 478 type val = type##_VALUE(g, p); \ 479 if (val == type##_INVAL) return; \ 480 type##_FORMAT(val, fmtbuf); \ 481 DEBUG(g->base.context, "%s (%s): %s", #name, #type, fmtbuf); \ 482 parse_##name(g, val); \ 483 } \ 484 static const struct field_desc name##_field_##type = { #name, parse_##name##_##type }; \ 485 static void parse_##name(struct garmin_parser_t *garmin, type data) 486 487 // All msg formats can have a timestamp 488 // Garmin timestamps are in seconds since 00:00 Dec 31 1989 UTC 489 // Convert to "standard epoch time" by adding 631065600. 490 DECLARE_FIELD(ANY, timestamp, UINT32) 491 { 492 garmin->record_data.timestamp = data; 493 if (garmin->callback) { 494 // Turn the timestamp relative to the beginning of the dive 495 if (data < garmin->dive.time) { 496 DEBUG(garmin->base.context, "Timestamp before dive start: %d (dive start: %d)", data, garmin->dive.time); 497 498 return; 499 } 500 data -= garmin->dive.time - 1; 501 502 // Did we already do this? 503 if (data == garmin->record_data.time) 504 return; 505 506 garmin->record_data.time = data; 507 508 // Now we're ready to actually update the sample times 509 dc_sample_value_t sample = {0}; 510 sample.time = data * 1000; 511 garmin->callback(DC_SAMPLE_TIME, &sample, garmin->userdata); 512 } 513 } 514 DECLARE_FIELD(ANY, message_index, UINT16) { garmin->record_data.index = data; } 515 DECLARE_FIELD(ANY, part_index, UINT32) { garmin->record_data.index = data; } 516 517 // FILE msg 518 DECLARE_FIELD(FILE, file_type, ENUM) { } 519 DECLARE_FIELD(FILE, manufacturer, UINT16) { } 520 DECLARE_FIELD(FILE, product, UINT16) { } 521 DECLARE_FIELD(FILE, serial, UINT32Z) { } 522 DECLARE_FIELD(FILE, creation_time, UINT32) { } 523 DECLARE_FIELD(FILE, number, UINT16) { } 524 DECLARE_FIELD(FILE, other_time, UINT32) { } 525 DECLARE_FIELD(FILE, product_name, STRING) { } 526 527 // SESSION msg 528 DECLARE_FIELD(SESSION, start_time, UINT32) { garmin->dive.time = data; } 529 DECLARE_FIELD(SESSION, start_pos_lat, SINT32) { garmin->gps.SESSION.entry.lat = data; } 530 DECLARE_FIELD(SESSION, start_pos_long, SINT32) { garmin->gps.SESSION.entry.lon = data; } 531 DECLARE_FIELD(SESSION, nec_pos_lat, SINT32) { garmin->gps.SESSION.NE.lat = data; } 532 DECLARE_FIELD(SESSION, nec_pos_long, SINT32) { garmin->gps.SESSION.NE.lon = data; } 533 DECLARE_FIELD(SESSION, swc_pos_lat, SINT32) { garmin->gps.SESSION.SW.lat = data; } 534 DECLARE_FIELD(SESSION, swc_pos_long, SINT32) { garmin->gps.SESSION.SW.lon = data; } 535 DECLARE_FIELD(SESSION, exit_pos_lat, SINT32) { garmin->gps.SESSION.exit.lat = data; } 536 DECLARE_FIELD(SESSION, exit_pos_long, SINT32) { garmin->gps.SESSION.exit.lon = data; } 537 538 // LAP msg 539 DECLARE_FIELD(LAP, start_time, UINT32) { } 540 DECLARE_FIELD(LAP, start_pos_lat, SINT32) { garmin->gps.LAP.entry.lat = data; } 541 DECLARE_FIELD(LAP, start_pos_long, SINT32) { garmin->gps.LAP.entry.lon = data; } 542 DECLARE_FIELD(LAP, end_pos_lat, SINT32) { garmin->gps.LAP.exit.lat = data; } 543 DECLARE_FIELD(LAP, end_pos_long, SINT32) { garmin->gps.LAP.exit.lon = data; } 544 DECLARE_FIELD(LAP, some_pos_lat, SINT32) { garmin->gps.LAP.some.lat = data; } 545 DECLARE_FIELD(LAP, some_pos_long, SINT32) { garmin->gps.LAP.some.lon = data; } 546 DECLARE_FIELD(LAP, other_pos_lat, SINT32) { garmin->gps.LAP.other.lat = data; } 547 DECLARE_FIELD(LAP, other_pos_long, SINT32) { garmin->gps.LAP.other.lon = data; } 548 549 // RECORD msg 550 DECLARE_FIELD(RECORD, position_lat, SINT32) { garmin->gps.RECORD.lat = data; } 551 DECLARE_FIELD(RECORD, position_long, SINT32) { garmin->gps.RECORD.lon = data; } 552 DECLARE_FIELD(RECORD, altitude, UINT16) { } // 5 *m + 500 ? 553 DECLARE_FIELD(RECORD, heart_rate, UINT8) // bpm 554 { 555 if (garmin->callback) { 556 dc_sample_value_t sample = {0}; 557 sample.heartbeat = data; 558 garmin->callback(DC_SAMPLE_HEARTBEAT, &sample, garmin->userdata); 559 } 560 } 561 DECLARE_FIELD(RECORD, cadence, UINT8) { } // cadence 562 DECLARE_FIELD(RECORD, fract_cadence, UINT8) { } // fractional cadence 563 DECLARE_FIELD(RECORD, distance, UINT32) { } // Distance in 100 * m? WTF? 564 DECLARE_FIELD(RECORD, speed, UINT16) { } // Speed (m/s?) 565 DECLARE_FIELD(RECORD, temperature, SINT8) // degrees C 566 { 567 if (garmin->callback) { 568 dc_sample_value_t sample = {0}; 569 sample.temperature = data; 570 garmin->callback(DC_SAMPLE_TEMPERATURE, &sample, garmin->userdata); 571 } 572 } 573 DECLARE_FIELD(RECORD, abs_pressure, UINT32) {} // Pascal 574 DECLARE_FIELD(RECORD, depth, UINT32) // mm 575 { 576 if (garmin->callback) { 577 dc_sample_value_t sample = {0}; 578 sample.depth = data / 1000.0; 579 garmin->callback(DC_SAMPLE_DEPTH, &sample, garmin->userdata); 580 } 581 } 582 DECLARE_FIELD(RECORD, next_stop_depth, UINT32) // mm 583 { 584 garmin->record_data.pending |= RECORD_DECO; 585 garmin->record_data.ceiling = data / 1000.0; 586 } 587 DECLARE_FIELD(RECORD, next_stop_time, UINT32) // seconds 588 { 589 garmin->record_data.pending |= RECORD_DECO; 590 garmin->record_data.stop_time = data; 591 } 592 DECLARE_FIELD(RECORD, tts, UINT32) 593 { 594 if (garmin->callback) { 595 dc_sample_value_t sample = {0}; 596 sample.time = data; 597 garmin->callback(DC_SAMPLE_TTS, &sample, garmin->userdata); 598 } 599 } 600 DECLARE_FIELD(RECORD, ndl, UINT32) // s 601 { 602 if (garmin->callback) { 603 dc_sample_value_t sample = {0}; 604 sample.deco.type = DC_DECO_NDL; 605 sample.deco.time = data; 606 sample.deco.tts = 0; 607 garmin->callback(DC_SAMPLE_DECO, &sample, garmin->userdata); 608 } 609 } 610 DECLARE_FIELD(RECORD, cns_load, UINT8) 611 { 612 if (garmin->callback) { 613 dc_sample_value_t sample = {0}; 614 sample.cns = data / 100.0; 615 garmin->callback(DC_SAMPLE_CNS, &sample, garmin->userdata); 616 } 617 } 618 DECLARE_FIELD(RECORD, n2_load, UINT16) { } // percent 619 DECLARE_FIELD(RECORD, air_time_remaining, UINT32) { } // seconds 620 DECLARE_FIELD(RECORD, pressure_sac, UINT16) { } // 100 * bar/min/pressure 621 DECLARE_FIELD(RECORD, volume_sac, UINT16) { } // 100 * l/min/pressure 622 DECLARE_FIELD(RECORD, rmv, UINT16) { } // 100 * l/min 623 DECLARE_FIELD(RECORD, ascent_rate, SINT32) { } // mm/s (negative is down) 624 625 // DEVICE_SETTINGS 626 DECLARE_FIELD(DEVICE_SETTINGS, utc_offset, UINT32) 627 { 628 garmin->dive.utc_offset = (SINT32) data; // wrong type in FIT 629 } 630 DECLARE_FIELD(DEVICE_SETTINGS, time_offset, UINT32) 631 { 632 /* 633 * Crazy FIT files have a zero time offset in DEVICE_SETTINGS, 634 * but then have a local_timestamp in ACTIVITY and/or in the 635 * TIME_CORRELATION messages. 636 * 637 * So I have no idea what this field means then. 638 */ 639 if (!data) 640 return; 641 garmin->dive.time_offset = (SINT32) data; // wrong type in FIT 642 } 643 644 // DEVICE_INFO 645 // collect the data and then use the record if it is for device_index 0 646 DECLARE_FIELD(DEVICE_INFO, device_index, UINT8) 647 { 648 garmin->record_data.device_index = data; 649 garmin->record_data.pending |= RECORD_DEVICE_INFO; 650 } 651 DECLARE_FIELD(DEVICE_INFO, product, UINT16) 652 { 653 garmin->record_data.product = data; 654 garmin->record_data.pending |= RECORD_DEVICE_INFO; 655 } 656 DECLARE_FIELD(DEVICE_INFO, serial_nr, UINT32Z) 657 { 658 garmin->record_data.serial = data; 659 garmin->record_data.pending |= RECORD_DEVICE_INFO; 660 } 661 DECLARE_FIELD(DEVICE_INFO, firmware, UINT16) 662 { 663 garmin->record_data.firmware = data; 664 garmin->record_data.pending |= RECORD_DEVICE_INFO; 665 } 666 667 // ACTIVITY 668 DECLARE_FIELD(ACTIVITY, total_timer_time, UINT32) { } 669 DECLARE_FIELD(ACTIVITY, num_sessions, UINT16) { } 670 DECLARE_FIELD(ACTIVITY, type, ENUM) { } 671 DECLARE_FIELD(ACTIVITY, event, ENUM) { } 672 DECLARE_FIELD(ACTIVITY, event_type, ENUM) { } 673 DECLARE_FIELD(ACTIVITY, local_timestamp, UINT32) 674 { 675 int time_offset = data - garmin->record_data.timestamp; 676 garmin->dive.time_offset = time_offset; 677 } 678 DECLARE_FIELD(ACTIVITY, event_group, UINT8) { } 679 680 // SPORT 681 DECLARE_FIELD(SPORT, sub_sport, ENUM) { 682 garmin->dive.sub_sport = (ENUM) data; 683 dc_divemode_t val; 684 switch (data) { 685 case 55: val = DC_DIVEMODE_GAUGE; 686 break; 687 case 56: 688 case 57: val = DC_DIVEMODE_FREEDIVE; 689 break; 690 case 63: 691 val = DC_DIVEMODE_CCR; 692 693 break; 694 default: val = DC_DIVEMODE_OC; 695 } 696 DC_ASSIGN_FIELD(garmin->cache, DIVEMODE, val); 697 } 698 699 /* 700 * What is the difference between 'system_timestamp' and the 701 * actual timestamp of the message itself? Who designs these 702 * crazy things? What is the meaning of it all? These are the 703 * kinds of unanswerable questions that keep me up at night. 704 */ 705 DECLARE_FIELD(TIMESTAMP_CORRELATION, system_timestamp, UINT32) { } 706 DECLARE_FIELD(TIMESTAMP_CORRELATION, local_timestamp, UINT32) 707 { 708 int time_offset = data - garmin->record_data.timestamp; 709 garmin->dive.time_offset = time_offset; 710 } 711 712 // DIVE_GAS - uses msg index 713 DECLARE_FIELD(DIVE_GAS, helium, UINT8) 714 { 715 garmin->record_data.gasmix.helium = data / 100.0; 716 garmin->record_data.pending |= RECORD_GASMIX; 717 } 718 DECLARE_FIELD(DIVE_GAS, oxygen, UINT8) 719 { 720 garmin->record_data.gasmix.oxygen = data / 100.0; 721 garmin->record_data.pending |= RECORD_GASMIX; 722 } 723 DECLARE_FIELD(DIVE_GAS, status, ENUM) 724 { 725 // 0 - disabled, 1 - enabled, 2 - backup 726 garmin->record_data.gas_status = data; 727 } 728 DECLARE_FIELD(DIVE_GAS, type, ENUM) 729 { 730 // 0 - open circuit, 1 - CCR diluent 731 if (data == 1) 732 garmin->record_data.gasmix.usage = DC_USAGE_DILUENT; 733 else 734 garmin->record_data.gasmix.usage = DC_USAGE_OPEN_CIRCUIT; 735 garmin->record_data.pending |= RECORD_GASMIX; 736 } 737 738 // DIVE_SUMMARY 739 DECLARE_FIELD(DIVE_SUMMARY, avg_depth, UINT32) { DC_ASSIGN_FIELD(garmin->cache, AVGDEPTH, data / 1000.0); } 740 DECLARE_FIELD(DIVE_SUMMARY, max_depth, UINT32) { DC_ASSIGN_FIELD(garmin->cache, MAXDEPTH, data / 1000.0); } 741 DECLARE_FIELD(DIVE_SUMMARY, surface_interval, UINT32) { } // sec 742 DECLARE_FIELD(DIVE_SUMMARY, start_cns, UINT8) { } // percent 743 DECLARE_FIELD(DIVE_SUMMARY, end_cns, UINT8) { } // percent 744 DECLARE_FIELD(DIVE_SUMMARY, start_n2, UINT16) { } // percent 745 DECLARE_FIELD(DIVE_SUMMARY, end_n2, UINT16) { } // percent 746 DECLARE_FIELD(DIVE_SUMMARY, o2_toxicity, UINT16) { } // OTUs 747 DECLARE_FIELD(DIVE_SUMMARY, dive_number, UINT32) { } 748 DECLARE_FIELD(DIVE_SUMMARY, bottom_time, UINT32) { DC_ASSIGN_FIELD(garmin->cache, DIVETIME, data / 1000); } 749 DECLARE_FIELD(DIVE_SUMMARY, avg_pressure_sac, UINT16) { } // 100 * bar/min/pressure 750 DECLARE_FIELD(DIVE_SUMMARY, avg_volume_sac, UINT16) { } // 100 * L/min/pressure 751 DECLARE_FIELD(DIVE_SUMMARY, avg_rmv, UINT16) { } // 100 * L/min 752 753 // DIVE_SETTINGS 754 DECLARE_FIELD(DIVE_SETTINGS, name, STRING) { } 755 DECLARE_FIELD(DIVE_SETTINGS, model, ENUM) 756 { 757 garmin->record_data.model = data; 758 garmin->record_data.pending |= RECORD_DECO_MODEL; 759 } 760 DECLARE_FIELD(DIVE_SETTINGS, gf_low, UINT8) 761 { 762 garmin->record_data.gf_low = data; 763 garmin->record_data.pending |= RECORD_DECO_MODEL; 764 } 765 DECLARE_FIELD(DIVE_SETTINGS, gf_high, UINT8) 766 { 767 garmin->record_data.gf_high = data; 768 garmin->record_data.pending |= RECORD_DECO_MODEL; 769 } 770 DECLARE_FIELD(DIVE_SETTINGS, water_type, ENUM) 771 { 772 garmin->cache.SALINITY.type = data ? DC_WATER_SALT : DC_WATER_FRESH; 773 garmin->cache.initialized |= 1 << DC_FIELD_SALINITY; 774 } 775 DECLARE_FIELD(DIVE_SETTINGS, water_density, FLOAT) 776 { 777 union { unsigned int binary; float actual; } val; 778 val.binary = data; 779 garmin->cache.SALINITY.density = val.actual; 780 garmin->cache.initialized |= 1 << DC_FIELD_SALINITY; 781 } 782 DECLARE_FIELD(DIVE_SETTINGS, po2_warn, UINT8) { } 783 DECLARE_FIELD(DIVE_SETTINGS, po2_critical, UINT8) { } 784 DECLARE_FIELD(DIVE_SETTINGS, po2_deco, UINT8) { } 785 DECLARE_FIELD(DIVE_SETTINGS, safety_stop_enabled, ENUM) { } 786 DECLARE_FIELD(DIVE_SETTINGS, bottom_depth, FLOAT) { } 787 DECLARE_FIELD(DIVE_SETTINGS, bottom_time, UINT32) { } 788 DECLARE_FIELD(DIVE_SETTINGS, apnea_countdown_enabled, ENUM) { } 789 DECLARE_FIELD(DIVE_SETTINGS, apnea_countdown_time, UINT32) { } 790 DECLARE_FIELD(DIVE_SETTINGS, backlight_mode, ENUM) { } 791 DECLARE_FIELD(DIVE_SETTINGS, backlight_brightness, UINT8) { } 792 DECLARE_FIELD(DIVE_SETTINGS, backlight_timeout, UINT8) { } 793 DECLARE_FIELD(DIVE_SETTINGS, repeat_dive_interval, UINT16) { } 794 DECLARE_FIELD(DIVE_SETTINGS, safety_stop_time, UINT16) { } 795 DECLARE_FIELD(DIVE_SETTINGS, heart_rate_source_type, ENUM) { } 796 DECLARE_FIELD(DIVE_SETTINGS, heart_rate_device_type, UINT8) { } 797 DECLARE_FIELD(DIVE_SETTINGS, setpoint_low_switch_mode, ENUM) 798 { 799 // 0 - manual, 1 - auto 800 garmin->dive.setpoint_low_switch_mode = data; 801 } 802 DECLARE_FIELD(DIVE_SETTINGS, setpoint_low_cbar, UINT8) 803 { 804 garmin->dive.setpoint_low_cbar = data; 805 806 // The initial setpoint at the start of the dive is the low setpoint 807 garmin->record_data.setpoint_actual_cbar = garmin->dive.setpoint_low_cbar; 808 garmin->record_data.pending |= RECORD_SETPOINT_CHANGE; 809 } 810 DECLARE_FIELD(DIVE_SETTINGS, setpoint_low_switch_depth_mm, UINT32) 811 { 812 garmin->dive.setpoint_low_switch_depth_mm = data; 813 } 814 DECLARE_FIELD(DIVE_SETTINGS, setpoint_high_switch_mode, ENUM) 815 { 816 // 0 - manual, 1 - auto 817 garmin->dive.setpoint_high_switch_mode = data; 818 } 819 DECLARE_FIELD(DIVE_SETTINGS, setpoint_high_cbar, UINT8) 820 { 821 garmin->dive.setpoint_high_cbar = data; 822 } 823 DECLARE_FIELD(DIVE_SETTINGS, setpoint_high_switch_depth_mm, UINT32) 824 { 825 garmin->dive.setpoint_high_switch_depth_mm = data; 826 } 827 828 // SENSOR_PROFILE record for each ANT/BLE sensor. 829 // We only care about sensor type 28 - Garmin tank pod. 830 DECLARE_FIELD(SENSOR_PROFILE, ant_channel_id, UINT32Z) 831 { 832 current_sensor(garmin)->sensor_id = data; 833 } 834 DECLARE_FIELD(SENSOR_PROFILE, name, STRING) { } 835 DECLARE_FIELD(SENSOR_PROFILE, enabled, ENUM) 836 { 837 current_sensor(garmin)->sensor_enabled = data; 838 } 839 DECLARE_FIELD(SENSOR_PROFILE, sensor_type, ENUM) 840 { 841 // 28 is tank pod 842 // start filling in next sensor after this record 843 if (data == 28) 844 garmin->record_data.pending |= RECORD_SENSOR_PROFILE; 845 } 846 DECLARE_FIELD(SENSOR_PROFILE, pressure_units, ENUM) 847 { 848 // 0 is PSI, 1 is KPA (unused), 2 is Bar 849 current_sensor(garmin)->sensor_units = data; 850 } 851 DECLARE_FIELD(SENSOR_PROFILE, rated_pressure, UINT16) 852 { 853 current_sensor(garmin)->sensor_rated_pressure = data; 854 } 855 DECLARE_FIELD(SENSOR_PROFILE, reserve_pressure, UINT16) 856 { 857 current_sensor(garmin)->sensor_reserve_pressure = data; 858 } 859 DECLARE_FIELD(SENSOR_PROFILE, volume, UINT16) 860 { 861 current_sensor(garmin)->sensor_volume = data; 862 } 863 DECLARE_FIELD(SENSOR_PROFILE, used_for_gas_rate, ENUM) 864 { 865 struct garmin_sensor *sensor = current_sensor(garmin); 866 sensor->sensor_used_for_gas_rate = data; 867 sensor->sensor_used_for_gas_rate_valid = 1; 868 } 869 870 DECLARE_FIELD(TANK_UPDATE, sensor, UINT32Z) 871 { 872 garmin->record_data.sensor = data; 873 } 874 875 DECLARE_FIELD(TANK_UPDATE, pressure, UINT16) 876 { 877 garmin->record_data.pressure = data; 878 garmin->record_data.pending |= RECORD_TANK_UPDATE; 879 } 880 881 DECLARE_FIELD(TANK_SUMMARY, sensor, UINT32Z) { } // sensor ID 882 DECLARE_FIELD(TANK_SUMMARY, start_pressure, UINT16) { } // Bar * 100 883 DECLARE_FIELD(TANK_SUMMARY, end_pressure, UINT16) { } // Bar * 100 884 DECLARE_FIELD(TANK_SUMMARY, volume_used, UINT32) { } // L * 100 885 886 // EVENT 887 DECLARE_FIELD(EVENT, event, ENUM) 888 { 889 garmin->record_data.event_nr = data; 890 garmin->record_data.pending |= RECORD_EVENT; 891 } 892 DECLARE_FIELD(EVENT, type, ENUM) 893 { 894 garmin->record_data.event_type = data; 895 garmin->record_data.pending |= RECORD_EVENT; 896 } 897 DECLARE_FIELD(EVENT, data, UINT32) 898 { 899 garmin->record_data.event_data = data; 900 } 901 DECLARE_FIELD(EVENT, event_group, UINT8) 902 { 903 garmin->record_data.event_group = data; 904 } 905 DECLARE_FIELD(EVENT, unknown, UINT32) 906 { 907 garmin->record_data.event_unknown = data; 908 } 909 DECLARE_FIELD(EVENT, tank_pressure_reserve, UINT32Z) { } // sensor ID 910 DECLARE_FIELD(EVENT, tank_pressure_critical, UINT32Z) { } // sensor ID 911 DECLARE_FIELD(EVENT, tank_pressure_lost, UINT32Z) { } // sensor ID 912 913 // "Field description" (for developer fields) 914 DECLARE_FIELD(FIELD_DESCRIPTION, data_index, UINT8) { } 915 DECLARE_FIELD(FIELD_DESCRIPTION, field_definition, UINT8) { } 916 DECLARE_FIELD(FIELD_DESCRIPTION, base_type, UINT8) { } 917 DECLARE_FIELD(FIELD_DESCRIPTION, name, STRING) { } // "Depth" 918 DECLARE_FIELD(FIELD_DESCRIPTION, scale, UINT8) { } 919 DECLARE_FIELD(FIELD_DESCRIPTION, offset, SINT8) { } 920 DECLARE_FIELD(FIELD_DESCRIPTION, unit, STRING) { } // "feet" 921 DECLARE_FIELD(FIELD_DESCRIPTION, original_mesg, UINT16) { } 922 DECLARE_FIELD(FIELD_DESCRIPTION, original_field, UINT8) { } 923 924 struct msg_desc { 925 unsigned char maxfield; 926 const struct field_desc *field[]; 927 }; 928 929 #define SET_FIELD(msg, nr, name, type) \ 930 [nr] = &msg##_##name##_field_##type 931 932 #define DECLARE_MESG(name) \ 933 static const struct msg_desc name##_msg_desc 934 935 DECLARE_MESG(FILE) = { 936 .maxfield = 9, 937 .field = { 938 SET_FIELD(FILE, 0, file_type, ENUM), 939 SET_FIELD(FILE, 1, manufacturer, UINT16), 940 SET_FIELD(FILE, 2, product, UINT16), 941 SET_FIELD(FILE, 3, serial, UINT32Z), 942 SET_FIELD(FILE, 4, creation_time, UINT32), 943 SET_FIELD(FILE, 5, number, UINT16), 944 SET_FIELD(FILE, 7, other_time, UINT32), 945 SET_FIELD(FILE, 8, product_name, STRING), 946 } 947 }; 948 949 DECLARE_MESG(DEVICE_SETTINGS) = { 950 .maxfield = 3, 951 .field = { 952 SET_FIELD(DEVICE_SETTINGS, 1, utc_offset, UINT32), // Convert to UTC 953 SET_FIELD(DEVICE_SETTINGS, 2, time_offset, UINT32), // Convert to local 954 } 955 }; 956 DECLARE_MESG(USER_PROFILE) = { }; 957 DECLARE_MESG(HRM_PROFILE) = { }; 958 DECLARE_MESG(ZONES_TARGET) = { }; 959 960 DECLARE_MESG(SPORT) = { 961 .maxfield = 2, 962 .field = { 963 SET_FIELD(SPORT, 1, sub_sport, ENUM), // 53 - 57 and 63 are dive activities 964 } 965 }; 966 967 DECLARE_MESG(SESSION) = { 968 .maxfield = 40, 969 .field = { 970 SET_FIELD(SESSION, 2, start_time, UINT32), 971 SET_FIELD(SESSION, 3, start_pos_lat, SINT32), // 180 deg / 2**31 972 SET_FIELD(SESSION, 4, start_pos_long, SINT32), // 180 deg / 2**31 973 SET_FIELD(SESSION, 29, nec_pos_lat, SINT32), // 180 deg / 2**31 974 SET_FIELD(SESSION, 30, nec_pos_long, SINT32), // 180 deg / 2**31 975 SET_FIELD(SESSION, 31, swc_pos_lat, SINT32), // 180 deg / 2**31 976 SET_FIELD(SESSION, 32, swc_pos_long, SINT32), // 180 deg / 2**31 977 SET_FIELD(SESSION, 38, exit_pos_lat, SINT32), // 180 deg / 2**31 978 SET_FIELD(SESSION, 39, exit_pos_long, SINT32), // 180 deg / 2**31 979 } 980 }; 981 982 DECLARE_MESG(LAP) = { 983 .maxfield = 31, 984 .field = { 985 SET_FIELD(LAP, 2, start_time, UINT32), 986 SET_FIELD(LAP, 3, start_pos_lat, SINT32), // 180 deg / 2**31 987 SET_FIELD(LAP, 4, start_pos_long, SINT32), // 180 deg / 2**31 988 SET_FIELD(LAP, 5, end_pos_lat, SINT32), // 180 deg / 2**31 989 SET_FIELD(LAP, 6, end_pos_long, SINT32), // 180 deg / 2**31 990 SET_FIELD(LAP, 27, some_pos_lat, SINT32), // 180 deg / 2**31 991 SET_FIELD(LAP, 28, some_pos_long, SINT32), // 180 deg / 2**31 992 SET_FIELD(LAP, 29, other_pos_lat, SINT32), // 180 deg / 2**31 993 SET_FIELD(LAP, 30, other_pos_long, SINT32), // 180 deg / 2**31 994 } 995 }; 996 997 DECLARE_MESG(RECORD) = { 998 .maxfield = 128, 999 .field = { 1000 SET_FIELD(RECORD, 0, position_lat, SINT32), // 180 deg / 2**31 1001 SET_FIELD(RECORD, 1, position_long, SINT32), // 180 deg / 2**31 1002 SET_FIELD(RECORD, 2, altitude, UINT16), // 5 *m + 500 ? 1003 SET_FIELD(RECORD, 3, heart_rate, UINT8), // bpm 1004 SET_FIELD(RECORD, 4, cadence, UINT8), // cadence 1005 SET_FIELD(RECORD, 5, distance, UINT32), // Distance in 100 * m? WTF? 1006 SET_FIELD(RECORD, 6, speed, UINT16), // m/s? Who knows.. 1007 SET_FIELD(RECORD, 13, temperature, SINT8), // degrees C 1008 SET_FIELD(RECORD, 53, fract_cadence, UINT8), // fractional cadence 1009 SET_FIELD(RECORD, 91, abs_pressure, UINT32), // Pascal 1010 SET_FIELD(RECORD, 92, depth, UINT32), // mm 1011 SET_FIELD(RECORD, 93, next_stop_depth, UINT32), // mm 1012 SET_FIELD(RECORD, 94, next_stop_time, UINT32), // seconds 1013 SET_FIELD(RECORD, 95, tts, UINT32), // seconds 1014 SET_FIELD(RECORD, 96, ndl, UINT32), // s 1015 SET_FIELD(RECORD, 97, cns_load, UINT8), // percent 1016 SET_FIELD(RECORD, 98, n2_load, UINT16), // percent 1017 SET_FIELD(RECORD, 123, air_time_remaining, UINT32), // seconds 1018 SET_FIELD(RECORD, 124, pressure_sac, UINT16), // 100 * bar/min/pressure 1019 SET_FIELD(RECORD, 125, volume_sac, UINT16), // 100 * l/min/pressure 1020 SET_FIELD(RECORD, 126, rmv, UINT16), // 100 * l/min 1021 SET_FIELD(RECORD, 127, ascent_rate, SINT32), // mm/s (negative is down) 1022 } 1023 }; 1024 1025 DECLARE_MESG(TIMESTAMP_CORRELATION) = { 1026 .maxfield = 6, 1027 .field = { 1028 SET_FIELD(TIMESTAMP_CORRELATION, 1, system_timestamp, UINT32), 1029 SET_FIELD(TIMESTAMP_CORRELATION, 3, local_timestamp, UINT32), 1030 } 1031 }; 1032 1033 DECLARE_MESG(DIVE_GAS) = { 1034 .maxfield = 4, 1035 .field = { 1036 // This uses a "message index" field to set the gas index 1037 SET_FIELD(DIVE_GAS, 0, helium, UINT8), 1038 SET_FIELD(DIVE_GAS, 1, oxygen, UINT8), 1039 SET_FIELD(DIVE_GAS, 2, status, ENUM), 1040 SET_FIELD(DIVE_GAS, 3, type, ENUM), 1041 } 1042 }; 1043 1044 DECLARE_MESG(DIVE_SUMMARY) = { 1045 .maxfield = 15, 1046 .field = { 1047 SET_FIELD(DIVE_SUMMARY, 2, avg_depth, UINT32), // mm 1048 SET_FIELD(DIVE_SUMMARY, 3, max_depth, UINT32), // mm 1049 SET_FIELD(DIVE_SUMMARY, 4, surface_interval, UINT32), // sec 1050 SET_FIELD(DIVE_SUMMARY, 5, start_cns, UINT8), // percent 1051 SET_FIELD(DIVE_SUMMARY, 6, end_cns, UINT8), // percent 1052 SET_FIELD(DIVE_SUMMARY, 7, start_n2, UINT16), // percent 1053 SET_FIELD(DIVE_SUMMARY, 8, end_n2, UINT16), // percent 1054 SET_FIELD(DIVE_SUMMARY, 9, o2_toxicity, UINT16), // OTUs 1055 SET_FIELD(DIVE_SUMMARY, 10, dive_number, UINT32), 1056 SET_FIELD(DIVE_SUMMARY, 11, bottom_time, UINT32), // ms 1057 SET_FIELD(DIVE_SUMMARY, 12, avg_pressure_sac, UINT16), // 100 * bar/min/pressure 1058 SET_FIELD(DIVE_SUMMARY, 13, avg_volume_sac, UINT16), // 100 * L/min/pressure 1059 SET_FIELD(DIVE_SUMMARY, 14, avg_rmv, UINT16), // 100 * L/min 1060 } 1061 }; 1062 1063 DECLARE_MESG(EVENT) = { 1064 .maxfield = 74, 1065 .field = { 1066 SET_FIELD(EVENT, 0, event, ENUM), 1067 SET_FIELD(EVENT, 1, type, ENUM), 1068 SET_FIELD(EVENT, 3, data, UINT32), 1069 SET_FIELD(EVENT, 4, event_group, UINT8), 1070 SET_FIELD(EVENT, 15, unknown, UINT32), 1071 SET_FIELD(EVENT, 71, tank_pressure_reserve, UINT32Z), // sensor ID 1072 SET_FIELD(EVENT, 72, tank_pressure_critical, UINT32Z), // sensor ID 1073 SET_FIELD(EVENT, 73, tank_pressure_lost, UINT32Z), // sensor ID 1074 } 1075 }; 1076 1077 DECLARE_MESG(DEVICE_INFO) = { 1078 .maxfield = 6, 1079 .field = { 1080 SET_FIELD(DEVICE_INFO, 0, device_index, UINT8), 1081 SET_FIELD(DEVICE_INFO, 3, serial_nr, UINT32Z), 1082 SET_FIELD(DEVICE_INFO, 4, product, UINT16), 1083 SET_FIELD(DEVICE_INFO, 5, firmware, UINT16), 1084 } 1085 }; 1086 1087 DECLARE_MESG(ACTIVITY) = { 1088 .maxfield = 7, 1089 .field = { 1090 SET_FIELD(ACTIVITY, 0, total_timer_time, UINT32), 1091 SET_FIELD(ACTIVITY, 1, num_sessions, UINT16), 1092 SET_FIELD(ACTIVITY, 2, type, ENUM), 1093 SET_FIELD(ACTIVITY, 3, event, ENUM), 1094 SET_FIELD(ACTIVITY, 4, event_type, ENUM), 1095 SET_FIELD(ACTIVITY, 5, local_timestamp, UINT32), 1096 SET_FIELD(ACTIVITY, 6, event_group, UINT8), 1097 } 1098 }; 1099 1100 DECLARE_MESG(FILE_CREATOR) = { }; 1101 1102 DECLARE_MESG(DIVE_SETTINGS) = { 1103 .maxfield = 28, 1104 .field = { 1105 SET_FIELD(DIVE_SETTINGS, 0, name, STRING), // Unused except in dive plans 1106 SET_FIELD(DIVE_SETTINGS, 1, model, ENUM), // model - Always 0 for Buhlmann ZHL-16C 1107 SET_FIELD(DIVE_SETTINGS, 2, gf_low, UINT8), // 0 to 100 1108 SET_FIELD(DIVE_SETTINGS, 3, gf_high, UINT8), // 0 to 100 1109 SET_FIELD(DIVE_SETTINGS, 4, water_type, ENUM), // One of fresh (0), salt (1), or custom (3). 2 is en13319 which is unused. 1110 SET_FIELD(DIVE_SETTINGS, 5, water_density, FLOAT), // If water_type is custom, this will be the density. Fresh is usually 1000, salt is usually 1025 1111 SET_FIELD(DIVE_SETTINGS, 6, po2_warn, UINT8), // PO2 * 100, so typically 140 to 160. When the PO2 starts blinking yellow 1112 SET_FIELD(DIVE_SETTINGS, 7, po2_critical, UINT8), // See above; value when PO2 blinks red and you get a popup 1113 SET_FIELD(DIVE_SETTINGS, 8, po2_deco, UINT8), // See above; PO2 limited used for choosing which gas to suggest 1114 SET_FIELD(DIVE_SETTINGS, 9, safety_stop_enabled, ENUM), // Used in conjunction with safety_stop_time below 1115 SET_FIELD(DIVE_SETTINGS, 10, bottom_depth, FLOAT), // Unused except in dive plans 1116 SET_FIELD(DIVE_SETTINGS, 11, bottom_time, UINT32), // Unused except in dive plans 1117 SET_FIELD(DIVE_SETTINGS, 12, apnea_countdown_enabled, ENUM), // This and apnea_countdown_time are the "Apnea Surface Alert" setting 1118 SET_FIELD(DIVE_SETTINGS, 13, apnea_countdown_time, UINT32), // 1119 SET_FIELD(DIVE_SETTINGS, 14, backlight_mode, ENUM), // 0 is "At Depth" and 1 is "Always On" 1120 SET_FIELD(DIVE_SETTINGS, 15, backlight_brightness, UINT8), // 0 to 100 1121 SET_FIELD(DIVE_SETTINGS, 16, backlight_timeout, UINT8), // seconds; 0 is no timeout 1122 SET_FIELD(DIVE_SETTINGS, 17, repeat_dive_interval, UINT16), // seconds between surfacing and when the watch stops and saves your dive. Must be at least 20. 1123 SET_FIELD(DIVE_SETTINGS, 18, safety_stop_time, UINT16), // seconds; 180 or 300 are acceptable values 1124 SET_FIELD(DIVE_SETTINGS, 19, heart_rate_source_type, ENUM), // For now all you need to know is source_type_local means WHR and source_type_antplus means strap data or off. (We're reusing existing infrastructure here which is why this is complex.) 1125 SET_FIELD(DIVE_SETTINGS, 20, heart_rate_device_type, UINT8), // device type depending on heart_rate_source_type (ignorable for now) 1126 SET_FIELD(DIVE_SETTINGS, 22, setpoint_low_switch_mode, ENUM), // CCR low setpoint switching mode 1127 SET_FIELD(DIVE_SETTINGS, 23, setpoint_low_cbar, UINT8), // CCR low setpoint [centibar] 1128 SET_FIELD(DIVE_SETTINGS, 24, setpoint_low_switch_depth_mm, UINT32), // CCR low setpoint switch depth [mm] 1129 SET_FIELD(DIVE_SETTINGS, 25, setpoint_high_switch_mode, ENUM), // CCR high setpoint switching mode 1130 SET_FIELD(DIVE_SETTINGS, 26, setpoint_high_cbar, UINT8), // CCR high setpoint [centibar] 1131 SET_FIELD(DIVE_SETTINGS, 27, setpoint_high_switch_depth_mm, UINT32), // CCR high setpoint switch depth [mm] 1132 } 1133 }; 1134 DECLARE_MESG(DIVE_ALARM) = { }; 1135 1136 DECLARE_MESG(SENSOR_PROFILE) = { 1137 .maxfield = 79, 1138 .field = { 1139 SET_FIELD(SENSOR_PROFILE, 0, ant_channel_id, UINT32Z), // derived from the number engraved on the side 1140 SET_FIELD(SENSOR_PROFILE, 2, name, STRING), 1141 SET_FIELD(SENSOR_PROFILE, 3, enabled, ENUM), 1142 SET_FIELD(SENSOR_PROFILE, 52, sensor_type, ENUM), // 28 is tank pod 1143 SET_FIELD(SENSOR_PROFILE, 74, pressure_units, ENUM), // 0 is PSI, 1 is KPA (unused), 2 is Bar 1144 SET_FIELD(SENSOR_PROFILE, 75, rated_pressure, UINT16), 1145 SET_FIELD(SENSOR_PROFILE, 76, reserve_pressure, UINT16), 1146 SET_FIELD(SENSOR_PROFILE, 77, volume, UINT16), // CuFt * 10 (PSI) or L * 10 (Bar) 1147 SET_FIELD(SENSOR_PROFILE, 78, used_for_gas_rate, ENUM), // was used for SAC calculations? 1148 } 1149 }; 1150 1151 DECLARE_MESG(TANK_UPDATE) = { 1152 .maxfield = 2, 1153 .field = { 1154 SET_FIELD(TANK_UPDATE, 0, sensor, UINT32Z), // sensor ID 1155 SET_FIELD(TANK_UPDATE, 1, pressure, UINT16), // pressure in Bar * 100 1156 } 1157 }; 1158 1159 DECLARE_MESG(TANK_SUMMARY) = { 1160 .maxfield = 4, 1161 .field = { 1162 SET_FIELD(TANK_SUMMARY, 0, sensor, UINT32Z), // sensor ID 1163 SET_FIELD(TANK_SUMMARY, 1, start_pressure, UINT16), // Bar * 100 1164 SET_FIELD(TANK_SUMMARY, 2, end_pressure, UINT16), // Bar * 100 1165 SET_FIELD(TANK_SUMMARY, 3, volume_used, UINT32), // L * 100 1166 } 1167 }; 1168 1169 DECLARE_MESG(FIELD_DESCRIPTION) = { 1170 .maxfield = 16, 1171 .field = { 1172 SET_FIELD(FIELD_DESCRIPTION, 0, data_index, UINT8), 1173 SET_FIELD(FIELD_DESCRIPTION, 1, field_definition, UINT8), 1174 SET_FIELD(FIELD_DESCRIPTION, 2, base_type, UINT8), 1175 SET_FIELD(FIELD_DESCRIPTION, 3, name, STRING), // "Depth" 1176 SET_FIELD(FIELD_DESCRIPTION, 6, scale, UINT8), 1177 SET_FIELD(FIELD_DESCRIPTION, 7, offset, SINT8), 1178 SET_FIELD(FIELD_DESCRIPTION, 8, unit, STRING), // "feet" 1179 // Some kind of pointer to original field? 1180 SET_FIELD(FIELD_DESCRIPTION, 14, original_mesg, UINT16), 1181 SET_FIELD(FIELD_DESCRIPTION, 15, original_field, UINT8), 1182 } 1183 }; 1184 1185 // Unknown global message ID's.. 1186 DECLARE_MESG(WTF_13) = { }; 1187 DECLARE_MESG(WTF_22) = { }; 1188 DECLARE_MESG(WTF_79) = { }; 1189 DECLARE_MESG(WTF_104) = { }; 1190 DECLARE_MESG(WTF_125) = { }; 1191 DECLARE_MESG(WTF_140) = { }; 1192 DECLARE_MESG(WTF_141) = { }; 1193 DECLARE_MESG(WTF_216) = { }; 1194 DECLARE_MESG(WTF_233) = { }; 1195 1196 #define SET_MESG(nr, name) [nr] = { #name, &name##_msg_desc } 1197 1198 static const struct { 1199 const char *name; 1200 const struct msg_desc *desc; 1201 } message_array[] = { 1202 SET_MESG( 0, FILE), 1203 SET_MESG( 2, DEVICE_SETTINGS), 1204 SET_MESG( 3, USER_PROFILE), 1205 SET_MESG( 4, HRM_PROFILE), 1206 SET_MESG( 7, ZONES_TARGET), 1207 SET_MESG( 12, SPORT), 1208 SET_MESG( 13, WTF_13), 1209 SET_MESG( 18, SESSION), 1210 SET_MESG( 19, LAP), 1211 SET_MESG( 20, RECORD), 1212 SET_MESG( 21, EVENT), 1213 SET_MESG( 22, WTF_22), 1214 SET_MESG( 23, DEVICE_INFO), 1215 SET_MESG( 34, ACTIVITY), 1216 SET_MESG( 49, FILE_CREATOR), 1217 SET_MESG( 79, WTF_79), 1218 1219 SET_MESG(104, WTF_104), 1220 SET_MESG(125, WTF_125), 1221 SET_MESG(140, WTF_140), 1222 SET_MESG(141, WTF_141), 1223 1224 SET_MESG(147, SENSOR_PROFILE), 1225 1226 SET_MESG(162, TIMESTAMP_CORRELATION), 1227 1228 SET_MESG(206, FIELD_DESCRIPTION), 1229 1230 SET_MESG(216, WTF_216), 1231 SET_MESG(233, WTF_233), 1232 SET_MESG(258, DIVE_SETTINGS), 1233 SET_MESG(259, DIVE_GAS), 1234 SET_MESG(262, DIVE_ALARM), 1235 SET_MESG(268, DIVE_SUMMARY), 1236 SET_MESG(319, TANK_UPDATE), 1237 SET_MESG(323, TANK_SUMMARY), 1238 }; 1239 1240 #define MSG_NAME_LEN 16 1241 static const struct msg_desc *lookup_msg_desc(unsigned short msg, int local, const char **namep) 1242 { 1243 /* 1244 * msg_desc has a flexible array member, so we can't create an array of them. 1245 * Use a compatible struct for local "fake" descriptors that have no fields. 1246 */ 1247 struct msg_desc_stub { 1248 unsigned char maxfield; 1249 }; 1250 static struct msg_desc_stub local_array[16]; 1251 static char local_name[16][MSG_NAME_LEN]; 1252 struct msg_desc_stub *stub; 1253 char *name; 1254 1255 /* Do we have a real one? */ 1256 if (msg < C_ARRAY_SIZE(message_array) && message_array[msg].name) { 1257 *namep = message_array[msg].name; 1258 return message_array[msg].desc; 1259 } 1260 1261 /* If not, fake it */ 1262 stub = &local_array[local]; 1263 memset(stub, 0, sizeof(*stub)); 1264 1265 name = local_name[local]; 1266 snprintf(name, MSG_NAME_LEN, "msg-%d", msg); 1267 *namep = name; 1268 return (const struct msg_desc *)stub; 1269 } 1270 1271 static int all_data_inval(const unsigned char *data, int base_type, int len) 1272 { 1273 int base_size = base_type_info[base_type].type_size; 1274 unsigned long long invalid = base_type_info[base_type].type_inval; 1275 1276 while (len > 0) { 1277 unsigned long long val = 0; 1278 memcpy(&val, data, base_size); 1279 if (val != invalid) 1280 return 0; 1281 data += base_size; 1282 len -= base_size; 1283 } 1284 return 1; 1285 } 1286 1287 static void unknown_field(struct garmin_parser_t *garmin, const unsigned char *data, 1288 const char *msg_name, unsigned int field_nr, 1289 int base_type, int len) 1290 { 1291 char buffer[80]; 1292 const char *str = (const char *)data; 1293 1294 /* Skip empty strings */ 1295 if (base_type == 7 && !*str) 1296 return; 1297 1298 /* Turn non-string data into hex values */ 1299 if (base_type != 7) { 1300 int pos = 0; 1301 int base_size = base_type_info[base_type].type_size; 1302 const char *sep = ""; 1303 1304 /* Skip empty data */ 1305 if (all_data_inval(data, base_type, len)) 1306 return; 1307 1308 str = buffer; 1309 while (len > 0) { 1310 long long val; 1311 /* Space + hex + NUL */ 1312 int need = 2+base_size*2; 1313 1314 /* The "-4" is because we reserve that " ..\0" at the end */ 1315 if (pos + need >= sizeof(buffer)-4) { 1316 strcpy(buffer+pos, " .."); 1317 break; 1318 } 1319 1320 val = 0; 1321 memcpy(&val, data, base_size); 1322 1323 pos += sprintf(buffer+pos, "%s%0*llx", sep, base_size*2, val); 1324 sep = " "; 1325 1326 data += base_size; 1327 len -= base_size; 1328 } 1329 1330 /* Avoid debug printing limit */ 1331 len = sizeof(buffer); 1332 } 1333 1334 DEBUG(garmin->base.context, "%s/%d %s '%.*s'", msg_name, field_nr, base_type_info[base_type].type_name, len, str); 1335 } 1336 1337 1338 static int traverse_regular(struct garmin_parser_t *garmin, 1339 const unsigned char *data, unsigned int size, 1340 unsigned char type, unsigned int *timep) 1341 { 1342 unsigned int total_len = 0; 1343 struct type_desc *desc = garmin->type_desc + type; 1344 const struct msg_desc *msg_desc = desc->msg_desc; 1345 const char *msg_name = desc->msg_name; 1346 1347 if (!msg_desc) { 1348 ERROR(garmin->base.context, "Uninitialized type descriptor %d\n", type); 1349 return -1; 1350 } 1351 1352 for (int i = 0; i < desc->nrfields; i++) { 1353 const unsigned char *field = desc->fields[i]; 1354 unsigned int field_nr = field[0]; 1355 unsigned int len = field[1]; 1356 unsigned int base_type = field[2] & 0x7f; 1357 const struct field_desc *field_desc; 1358 unsigned int base_size; 1359 1360 if (!len) { 1361 ERROR(garmin->base.context, "field with zero length\n"); 1362 return total_len + size; 1363 } 1364 1365 if (size < len) { 1366 ERROR(garmin->base.context, "Data traversal size bigger than remaining data (%d vs %d)\n", len, size); 1367 return total_len + size; 1368 } 1369 1370 if (base_type > 16) { 1371 ERROR(garmin->base.context, "Unknown base type %d\n", base_type); 1372 return total_len + size; 1373 } 1374 base_size = base_type_info[base_type].type_size; 1375 if (len % base_size) { 1376 ERROR(garmin->base.context, "Data traversal size not a multiple of base size (%d vs %d)\n", len, base_size); 1377 return -1; 1378 } 1379 1380 // Certain field numbers have fixed meaning across all messages 1381 switch (field_nr) { 1382 case 250: 1383 field_desc = &ANY_part_index_field_UINT32; 1384 break; 1385 case 253: 1386 field_desc = &ANY_timestamp_field_UINT32; 1387 break; 1388 case 254: 1389 field_desc = &ANY_message_index_field_UINT16; 1390 break; 1391 default: 1392 field_desc = NULL; 1393 if (field_nr < msg_desc->maxfield) 1394 field_desc = msg_desc->field[field_nr]; 1395 } 1396 1397 if (field_desc) { 1398 if (field_nr == 253 && !msg_desc->maxfield) 1399 DEBUG(garmin->base.context, "Ignoring timestamp field for undefined message."); 1400 else 1401 field_desc->parse(garmin, base_type, data); 1402 } else { 1403 unknown_field(garmin, data, msg_name, field_nr, base_type, len); 1404 } 1405 1406 data += len; 1407 total_len += len; 1408 size -= len; 1409 } 1410 1411 for (int i = 0; i < desc->devfields; i++) { 1412 int desc_idx = i + desc->nrfields; 1413 const unsigned char *field = desc->fields[desc_idx]; 1414 unsigned int field_nr = field[0]; 1415 unsigned int len = field[1]; 1416 unsigned int type = field[2]; 1417 1418 DEBUG(garmin->base.context, "Developer field %d %02x type %02x", i, field_nr, type); 1419 1420 if (!len) { 1421 ERROR(garmin->base.context, " developer field with zero length\n"); 1422 return -1; 1423 } 1424 1425 if (size < len) { 1426 ERROR(garmin->base.context, " developer field bigger than remaining data (%d vs %d)\n", len, size); 1427 return -1; 1428 } 1429 1430 HEXDUMP(garmin->base.context, DC_LOGLEVEL_DEBUG, "data", data, len); 1431 data += len; 1432 total_len += len; 1433 size -= len; 1434 } 1435 1436 return total_len; 1437 } 1438 1439 /* 1440 * A definition record: 1441 * 1442 * 5 bytes of fixed header: 1443 * - 1x reserved byte 1444 * - 1x architecture byte (0 = LE) 1445 * - 2x msg number bytes 1446 * - 1x field number byte 1447 * 1448 * Followed by the specified number of field definitions: 1449 * 1450 * 3 bytes for each field definition: 1451 * - 1x "field definition number" (look up in the FIT profile) 1452 * - 1x field size in bytes (so you can know the size even if you don't know the definition) 1453 * - 1x base type bit field 1454 * 1455 * Followed *optionally* by developer definitions (if record header & 0x20): 1456 * 1457 * - 1x number of developer definitions 1458 * - 3 bytes each 1459 */ 1460 static int traverse_definition(struct garmin_parser_t *garmin, 1461 const unsigned char *data, unsigned int size, 1462 unsigned char record) 1463 { 1464 unsigned short msg; 1465 unsigned char type = record & 0xf; 1466 struct type_desc *desc = garmin->type_desc + type; 1467 int fields, devfields, len; 1468 1469 // data[1] tells us if this is big or little endian 1470 garmin->is_big_endian = data[1] != 0; 1471 msg = garmin_value(garmin, data + 2, 2); 1472 desc->msg_desc = lookup_msg_desc(msg, type, &desc->msg_name); 1473 fields = data[4]; 1474 DEBUG(garmin->base.context, "Define local type %d: %02x %s %04x %02x %s", 1475 type, data[0], data[1] ? "big-endian" : "little-endian", msg, fields, desc->msg_name); 1476 if (fields > MAXFIELDS) { 1477 ERROR(garmin->base.context, "Too many fields in description: %d (max %d)\n", fields, MAXFIELDS); 1478 return -1; 1479 } 1480 desc->nrfields = fields; 1481 len = 5 + fields*3; 1482 devfields = 0; 1483 1484 for (int i = 0; i < fields; i++) { 1485 unsigned char *field = desc->fields[i]; 1486 memcpy(field, data + (5+i*3), 3); 1487 DEBUG(garmin->base.context, " %d: %02x %02x %02x", i, field[0], field[1], field[2]); 1488 } 1489 1490 data += len; 1491 1492 /* Developer fields after the regular ones */ 1493 if (record & 0x20) { 1494 devfields = data[0]; 1495 DEBUG(garmin->base.context, "Developer field (rec=%02x len=%d, devfields=%d)", 1496 record, len, devfields); 1497 1498 /* 1499 * one byte of dev field numbers, each three bytes in size 1500 * (number/size/index) 1501 */ 1502 len += 1 + 3*devfields; 1503 1504 for (int i = 0; i < devfields; i++) { 1505 int idx = fields + i; 1506 unsigned char *field = desc->fields[idx]; 1507 if (idx > MAXFIELDS) { 1508 ERROR(garmin->base.context, "Too many dev fields in description: %d+%d (max %d)\n", fields, i, MAXFIELDS); 1509 return -1; 1510 } 1511 memcpy(field, data + (1+i*3), 3); 1512 DEBUG(garmin->base.context, " %d: %02x %02x %02x", i, field[0], field[1], field[2]); 1513 } 1514 } 1515 1516 desc->devfields = devfields; 1517 1518 return len; 1519 } 1520 1521 1522 static dc_status_t 1523 traverse_data(struct garmin_parser_t *garmin) 1524 { 1525 const unsigned char *data = garmin->base.data; 1526 int len = garmin->base.size; 1527 unsigned int hdrsize, protocol, profile, datasize; 1528 unsigned int time; 1529 1530 // Reset the time and type descriptors before walking 1531 memset(&garmin->record_data, 0, sizeof(garmin->record_data)); 1532 memset(garmin->type_desc, 0, sizeof(garmin->type_desc)); 1533 1534 // The FIT header 1535 if (len < 12) { 1536 ERROR(garmin->base.context, " file too short for FIT header"); 1537 1538 return DC_STATUS_IO; 1539 } 1540 1541 hdrsize = data[0]; 1542 protocol = data[1]; 1543 profile = array_uint16_le(data+2); // these two fields are always little endian 1544 datasize = array_uint32_le(data+4); 1545 if (memcmp(data+8, ".FIT", 4)) { 1546 ERROR(garmin->base.context, " missing .FIT marker"); 1547 1548 return DC_STATUS_IO; 1549 } 1550 if (hdrsize < 12 || datasize > len || datasize + hdrsize + 2 > len) { 1551 ERROR(garmin->base.context, " inconsistent size information hdrsize %d datasize %d len %d", hdrsize, datasize, len); 1552 1553 return DC_STATUS_IO; 1554 } 1555 garmin->dive.protocol = protocol; 1556 garmin->dive.profile = profile; 1557 1558 data += hdrsize; 1559 time = 0; 1560 1561 while (datasize > 0) { 1562 unsigned char record = data[0]; 1563 int len; 1564 1565 data++; 1566 datasize--; 1567 1568 if (record & 0x80) { // Compressed record? 1569 unsigned int newtime; 1570 unsigned char type; 1571 1572 type = (record >> 5) & 3; 1573 newtime = (record & 0x1f) | (time & ~0x1f); 1574 if (newtime < time) 1575 newtime += 0x20; 1576 time = newtime; 1577 1578 // Compressed records are like normal records 1579 // with that added relative timestamp 1580 DEBUG(garmin->base.context, "Compressed record for type %d", type); 1581 1582 if (!(garmin->type_desc + type)->msg_desc->maxfield) 1583 DEBUG(garmin->base.context, "Ignoring timestamp field for undefined message."); 1584 else 1585 parse_ANY_timestamp(garmin, time); 1586 1587 len = traverse_regular(garmin, data, datasize, type, &time); 1588 } else if (record & 0x40) { // Definition record? 1589 len = traverse_definition(garmin, data, datasize, record); 1590 } else { // Normal data record 1591 DEBUG(garmin->base.context, "Regular record for type %d", record); 1592 len = traverse_regular(garmin, data, datasize, record, &time); 1593 } 1594 if (len <= 0 || len > datasize) { 1595 ERROR(garmin->base.context, " traverse failed"); 1596 1597 return DC_STATUS_IO; 1598 } 1599 data += len; 1600 datasize -= len; 1601 1602 // Flush pending data on record boundaries 1603 if (garmin->record_data.pending) 1604 flush_pending_record(garmin); 1605 } 1606 1607 return DC_STATUS_SUCCESS; 1608 } 1609 1610 /* Don't use floating point printing, because of "," vs "." confusion */ 1611 static void add_gps_string(garmin_parser_t *garmin, const char *desc, struct pos *pos) 1612 { 1613 int lat = pos->lat, lon = pos->lon; 1614 1615 if (lat && lon) { 1616 int latsign = 0, lonsign = 0; 1617 int latfrac, lonfrac; 1618 long long tmp; 1619 1620 if (lat < 0) { 1621 lat = -lat; 1622 latsign = 1; 1623 } 1624 if (lon < 0) { 1625 lon = -lon; 1626 lonsign = 1; 1627 } 1628 1629 tmp = 360 * (long long) lat; 1630 lat = tmp >> 32; 1631 tmp &= 0xffffffff; 1632 tmp *= 1000000; 1633 latfrac = tmp >> 32; 1634 1635 tmp = 360 * (long long) lon; 1636 lon = tmp >> 32; 1637 tmp &= 0xffffffff; 1638 tmp *= 1000000; 1639 lonfrac = tmp >> 32; 1640 1641 dc_field_add_string_fmt(&garmin->cache, desc, "%s%d.%06d, %s%d.%06d", 1642 latsign ? "-" : "", lat, latfrac, 1643 lonsign ? "-" : "", lon, lonfrac); 1644 } 1645 } 1646 1647 int 1648 garmin_parser_is_dive (dc_parser_t *abstract, dc_event_devinfo_t *devinfo_p) 1649 { 1650 garmin_parser_t *garmin = (garmin_parser_t *) abstract; 1651 1652 if (devinfo_p) { 1653 devinfo_p->firmware = garmin->dive.firmware; 1654 devinfo_p->serial = garmin->dive.serial; 1655 devinfo_p->model = garmin->dive.product; 1656 } 1657 switch (garmin->dive.sub_sport) { 1658 case 53: // Single-gas 1659 case 54: // Multi-gas 1660 case 55: // Gauge 1661 case 56: // Apnea 1662 case 57: // Apnea Hunt 1663 case 63: // CCR 1664 return 1; 1665 default: 1666 // Even if we don't recognize the sub_sport, 1667 // let's assume it's a dive if we've seen 1668 // average depth in the DIVE_SUMMARY record. 1669 if (garmin->cache.AVGDEPTH) 1670 return 1; 1671 return 0; 1672 } 1673 } 1674 1675 // AI-generated (Claude) 1676 dc_status_t 1677 garmin_parser_get_device_info (dc_parser_t *abstract, dc_event_devinfo_t *devinfo) 1678 { 1679 garmin_parser_t *garmin = (garmin_parser_t *) abstract; 1680 1681 devinfo->firmware = garmin->dive.firmware; 1682 devinfo->serial = garmin->dive.serial; 1683 devinfo->model = garmin->dive.product; 1684 1685 return DC_STATUS_SUCCESS; 1686 } 1687 1688 static void add_sensor_string(garmin_parser_t *garmin, const char *desc, const struct garmin_sensor *sensor) 1689 { 1690 dc_field_add_string_fmt(&garmin->cache, desc, "%x", sensor->sensor_id); 1691 } 1692 1693 static dc_status_t 1694 garmin_parser_set_data (garmin_parser_t *garmin, const unsigned char *data, unsigned int size) 1695 { 1696 /* Walk the data once without a callback to set up the core fields */ 1697 garmin->callback = NULL; 1698 garmin->userdata = NULL; 1699 memset(&garmin->gps, 0, sizeof(garmin->gps)); 1700 memset(&garmin->dive, 0, sizeof(garmin->dive)); 1701 memset(&garmin->cache, 0, sizeof(garmin->cache)); 1702 1703 traverse_data(garmin); 1704 1705 // Device information 1706 if (garmin->dive.serial) 1707 dc_field_add_string_fmt(&garmin->cache, STRING_KEY_SERIAL_NUMBER, "%u", garmin->dive.serial); 1708 if (garmin->dive.firmware) 1709 dc_field_add_string_fmt(&garmin->cache, STRING_KEY_FIRMWARE_VERSION, "%u.%02u", 1710 garmin->dive.firmware / 100, garmin->dive.firmware % 100); 1711 if (garmin->dive.product) { 1712 unsigned i; 1713 for (i = 0; garmin_models[i].name; i++) 1714 if (garmin_models[i].id == garmin->dive.product) 1715 break; 1716 1717 if (garmin_models[i].name) 1718 dc_field_add_string_fmt(&garmin->cache, "Model", "%s", garmin_models[i].name); 1719 else 1720 dc_field_add_string_fmt(&garmin->cache, "Model", "Unknown model ID: %u", garmin->dive.product); 1721 } 1722 1723 // These seem to be the "real" GPS dive coordinates 1724 add_gps_string(garmin, "GPS1", &garmin->gps.SESSION.entry); 1725 add_gps_string(garmin, "GPS2", &garmin->gps.SESSION.exit); 1726 1727 add_gps_string(garmin, "Session NE corner GPS", &garmin->gps.SESSION.NE); 1728 add_gps_string(garmin, "Session SW corner GPS", &garmin->gps.SESSION.SW); 1729 1730 add_gps_string(garmin, "Lap entry GPS", &garmin->gps.LAP.entry); 1731 add_gps_string(garmin, "Lap exit GPS", &garmin->gps.LAP.exit); 1732 add_gps_string(garmin, "Lap some GPS", &garmin->gps.LAP.some); 1733 add_gps_string(garmin, "Lap other GPS", &garmin->gps.LAP.other); 1734 1735 add_gps_string(garmin, "Record GPS", &garmin->gps.RECORD); 1736 1737 // We have no idea about gas mixes vs tanks, so for now the only 1738 // DC_FIELD_TANK member we report is "usage": whether the dive 1739 // computer counted this tank pod's gas towards the diver's own 1740 // consumption (SENSOR_PROFILE field 78, "used for SAC calculations"). 1741 // Volume/type/workpressure are left at their field-cache defaults. 1742 // 1743 // The index here matches the one used for DC_SAMPLE_PRESSURE 1744 // (see find_tank_index()): both walk garmin->dive.sensor[]. 1745 for (int i = 0; i < garmin->dive.nr_sensor; i++) { 1746 const struct garmin_sensor *sensor = garmin->dive.sensor + i; 1747 dc_tank_usage_t usage = DC_TANK_USAGE_NONE; 1748 1749 // Field 78 is only decoded when it is actually present in the 1750 // log; a sensor that never saw it must stay NONE here, never 1751 // EXCLUDED. 1752 if (sensor->sensor_used_for_gas_rate_valid) 1753 usage = sensor->sensor_used_for_gas_rate ? DC_TANK_USAGE_INCLUDED : DC_TANK_USAGE_EXCLUDED; 1754 1755 garmin->cache.tankusage[i] = usage; 1756 } 1757 if (garmin->dive.nr_sensor) 1758 garmin->cache.initialized |= 1u << DC_FIELD_TANK; 1759 1760 // Hate hate hate gasmix vs tank counts. 1761 // 1762 // There's no way to match them up unless they are an identity 1763 // mapping, so having two different ones doesn't actually work. 1764 if (garmin->dive.nr_sensor > garmin->cache.TANK_COUNT) 1765 DC_ASSIGN_FIELD(garmin->cache, TANK_COUNT, garmin->dive.nr_sensor); 1766 1767 for (int i = 0; i < garmin->dive.nr_sensor; i++) { 1768 static const char *name[] = { "Sensor 1", "Sensor 2", "Sensor 3", "Sensor 4", "Sensor 5" }; 1769 add_sensor_string(garmin, name[i], garmin->dive.sensor+i); 1770 } 1771 1772 if (garmin->cache.DIVEMODE == DC_DIVEMODE_CCR) { 1773 dc_field_add_string_fmt(&garmin->cache, "Setpoint low [bar]", "%u.%02u", 1774 garmin->dive.setpoint_low_cbar / 100, (garmin->dive.setpoint_low_cbar % 100)); 1775 dc_field_add_string(&garmin->cache, "Setpoint low mode", garmin->dive.setpoint_low_switch_mode ? "auto" : "manual"); 1776 if (garmin->dive.setpoint_low_switch_mode) { 1777 dc_field_add_string_fmt(&garmin->cache, "Setpoint low auto switch depth [m]", "%u.%01u", 1778 garmin->dive.setpoint_low_switch_depth_mm / 1000, (garmin->dive.setpoint_low_switch_depth_mm % 1000) / 100); 1779 } 1780 1781 dc_field_add_string_fmt(&garmin->cache, "Setpoint high [bar]", "%u.%02u", 1782 garmin->dive.setpoint_high_cbar / 100, (garmin->dive.setpoint_high_cbar % 100)); 1783 dc_field_add_string(&garmin->cache, "Setpoint high mode", garmin->dive.setpoint_high_switch_mode ? "auto" : "manual"); 1784 if (garmin->dive.setpoint_high_switch_mode) { 1785 dc_field_add_string_fmt(&garmin->cache, "Setpoint high auto switch depth [m]", "%u.%01u", 1786 garmin->dive.setpoint_high_switch_depth_mm / 1000, (garmin->dive.setpoint_high_switch_depth_mm % 1000) / 100); 1787 } 1788 } 1789 1790 return DC_STATUS_SUCCESS; 1791 } 1792 1793 1794 static dc_status_t 1795 garmin_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime) 1796 { 1797 garmin_parser_t *garmin = (garmin_parser_t *) abstract; 1798 dc_ticks_t time = 631065600 + (dc_ticks_t) garmin->dive.time; 1799 int timezone = DC_TIMEZONE_NONE; 1800 1801 // Show local time (time_offset) 1802 dc_datetime_gmtime(datetime, time + garmin->dive.time_offset); 1803 1804 /* See if we might have a valid timezone offset */ 1805 if (garmin->dive.time_offset || garmin->dive.utc_offset) { 1806 int offset = garmin->dive.time_offset - garmin->dive.utc_offset; 1807 1808 /* 15-minute (900-second) offsets are real */ 1809 if ((offset % 900) == 0 && 1810 offset >= -12*60*60 && 1811 offset <= 14*60*60) 1812 timezone = offset; 1813 } 1814 datetime->timezone = timezone; 1815 1816 return DC_STATUS_SUCCESS; 1817 } 1818 1819 static dc_status_t get_string_field(dc_field_string_t *strings, unsigned idx, dc_field_string_t *value) 1820 { 1821 if (idx < MAXSTRINGS) { 1822 dc_field_string_t *res = strings+idx; 1823 if (res->desc && res->value) { 1824 *value = *res; 1825 return DC_STATUS_SUCCESS; 1826 } 1827 } 1828 return DC_STATUS_UNSUPPORTED; 1829 } 1830 1831 static dc_status_t 1832 garmin_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value) 1833 { 1834 garmin_parser_t *garmin = (garmin_parser_t *) abstract; 1835 1836 return dc_field_get(&garmin->cache, type, flags, value); 1837 } 1838 1839 static dc_status_t 1840 garmin_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata) 1841 { 1842 garmin_parser_t *garmin = (garmin_parser_t *) abstract; 1843 1844 garmin->callback = callback; 1845 garmin->userdata = userdata; 1846 return traverse_data(garmin); 1847 }