suunto_eonsteel_parser.c (43101B)
1 /* 2 * libdivecomputer 3 * 4 * Copyright (C) 2014 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 <stdio.h> // snprintf 23 #include <stdlib.h> 24 #include <string.h> 25 #include <ctype.h> 26 #include <math.h> 27 28 #include "suunto_eonsteel.h" 29 #include "context-private.h" 30 #include "parser-private.h" 31 #include "array.h" 32 #include "platform.h" 33 #include "field-cache.h" 34 35 enum eon_sample { 36 ES_none = 0, 37 ES_dtime, // duint16,precision=3 (time delta in ms) 38 ES_depth, // uint16,precision=2,nillable=65535 (depth in cm) 39 ES_temp, // int16,precision=2,nillable=-3000 (temp in deci-Celsius) 40 ES_ndl, // int16,nillable=-1 (ndl in minutes) 41 ES_ceiling, // uint16,precision=2,nillable=65535 (ceiling in cm) 42 ES_tts, // uint16,nillable=65535 (time to surface) 43 ES_heading, // uint16,precision=4,nillable=65535 (heading in degrees) 44 ES_abspressure, // uint16,precision=0,nillable=65535 (abs presure in centibar) 45 ES_gastime, // int16,nillable=-1 (remaining gas time in minutes) 46 ES_ventilation, // uint16,precision=6,nillable=65535 ("x/6000000,x"? No idea) 47 ES_gasnr, // uint8 48 ES_pressure, // uint16,nillable=65535 (cylinder pressure in centibar) 49 ES_state, // enum:0=Wet Outside,1=Below Wet Activation Depth,2=Below Surface,3=Dive Active,4=Surface Calculation,5=Tank pressure available,6=Closed Circuit Mode 50 ES_state_active, // bool 51 ES_notify, // enum:0=NoFly Time,1=Depth,2=Surface Time,3=Tissue Level,4=Deco,5=Deco Window,6=Safety Stop Ahead,7=Safety Stop,8=Safety Stop Broken,9=Deep Stop Ahead,10=Deep Stop,11=Dive Time,12=Gas Available,13=SetPoint Switch,14=Diluent Hypoxia,15=Air Time,16=Tank Pressure 52 ES_notify_active, // bool 53 ES_warning, // enum:0=ICD Penalty,1=Deep Stop Penalty,2=Mandatory Safety Stop,3=OTU250,4=OTU300,5=CNS80%,6=CNS100%,7=Max.Depth,8=Air Time,9=Tank Pressure,10=Safety Stop Broken,11=Deep Stop Broken,12=Ceiling Broken,13=PO2 High 54 ES_warning_active, // bool 55 ES_alarm, 56 ES_alarm_active, 57 ES_gasswitch, // uint16 58 ES_setpoint_type, // enum:0=Low,1=High,2=Custom 59 ES_setpoint_po2, // uint32 60 ES_setpoint_automatic, // bool 61 ES_bookmark, 62 ES_insertgas, // uint16 63 ES_removegas, // uint16 64 }; 65 66 #define EON_MAX_GROUP 16 67 68 struct type_desc { 69 char *desc, *format, *mod; 70 unsigned int size; 71 enum eon_sample type[EON_MAX_GROUP]; 72 }; 73 74 #define MAXTYPE 512 75 76 typedef struct suunto_eonsteel_parser_t { 77 dc_parser_t base; 78 struct type_desc type_desc[MAXTYPE]; 79 struct dc_field_cache cache; 80 } suunto_eonsteel_parser_t; 81 82 typedef int (*eon_data_cb_t)(unsigned short type, const struct type_desc *desc, const unsigned char *data, unsigned int len, void *user); 83 84 static const struct { 85 const char *name; 86 enum eon_sample type; 87 } type_translation[] = { 88 { "+Time", ES_dtime }, 89 { "Depth", ES_depth }, 90 { "Temperature", ES_temp }, 91 { "NoDecTime", ES_ndl }, 92 { "Ceiling", ES_ceiling }, 93 { "TimeToSurface", ES_tts }, 94 { "Heading", ES_heading }, 95 { "DeviceInternalAbsPressure", ES_abspressure }, 96 { "GasTime", ES_gastime }, 97 { "Ventilation", ES_ventilation }, 98 { "Cylinders+Cylinder.GasNumber", ES_gasnr }, 99 { "Cylinders.Cylinder.Pressure", ES_pressure }, 100 { "Events+State.Type", ES_state }, 101 { "Events.State.Active", ES_state_active }, 102 { "Events+Notify.Type", ES_notify }, 103 { "Events.Notify.Active", ES_notify_active }, 104 { "Events+Warning.Type", ES_warning }, 105 { "Events.Warning.Active", ES_warning_active }, 106 { "Events+Alarm.Type", ES_alarm }, 107 { "Events.Alarm.Active", ES_alarm_active }, 108 { "Events.Bookmark.Name", ES_bookmark }, 109 { "Events.GasSwitch.GasNumber", ES_gasswitch }, 110 { "Events.SetPoint.Type", ES_setpoint_type }, 111 { "Events.Events.SetPoint.PO2", ES_setpoint_po2 }, 112 { "Events.SetPoint.Automatic", ES_setpoint_automatic }, 113 { "Events.DiveTimer.Active", ES_none }, 114 { "Events.DiveTimer.Time", ES_none }, 115 { "Events+GasSwitch.GasNumber", ES_gasswitch }, 116 { "Events+GasEdit.InsertGasNumber", ES_insertgas }, 117 { "Events+GasEdit.RemoveGasNumber", ES_removegas }, 118 }; 119 120 static enum eon_sample lookup_descriptor_type(suunto_eonsteel_parser_t *eon, struct type_desc *desc) 121 { 122 const char *name = desc->desc; 123 124 // Not a sample type? Skip it 125 if (strncmp(name, "sml.DeviceLog.Samples", 21)) 126 return ES_none; 127 128 // Skip the common base 129 name += 21; 130 131 // We have a "+Sample.Time", which starts a new 132 // sample and contains the time delta 133 if (!strcmp(name, "+Sample.Time")) 134 return ES_dtime; 135 136 // .. the rest should start with ".Sample." 137 if (strncmp(name, ".Sample.", 8)) 138 return ES_none; 139 140 // Skip the ".Sample." 141 name += 8; 142 143 // .. and look it up in the table of sample type strings 144 for (size_t i = 0; i < C_ARRAY_SIZE(type_translation); i++) { 145 if (!strcmp(name, type_translation[i].name)) 146 return type_translation[i].type; 147 } 148 return ES_none; 149 } 150 151 static const char *desc_type_name(enum eon_sample type) 152 { 153 for (size_t i = 0; i < C_ARRAY_SIZE(type_translation); i++) { 154 if (type == type_translation[i].type) 155 return type_translation[i].name; 156 } 157 return "Unknown"; 158 } 159 160 static int lookup_descriptor_size(suunto_eonsteel_parser_t *eon, struct type_desc *desc) 161 { 162 const char *format = desc->format; 163 unsigned char c; 164 165 if (!format) 166 return 0; 167 168 if (!strncmp(format, "bool", 4)) 169 return 1; 170 if (!strncmp(format, "enum", 4)) 171 return 1; 172 if (!strncmp(format, "utf8", 4)) 173 return 0; 174 175 // find the byte size (eg "float32" -> 4 bytes) 176 while ((c = *format) != 0) { 177 if (isdigit(c)) 178 return atoi(format)/8; 179 format++; 180 } 181 return 0; 182 } 183 184 static int fill_in_group_details(suunto_eonsteel_parser_t *eon, struct type_desc *desc) 185 { 186 int subtype = 0; 187 const char *grp = desc->desc; 188 189 for (;;) { 190 struct type_desc *base; 191 char *end; 192 long index; 193 194 index = strtol(grp, &end, 10); 195 if (index < 0 || index >= MAXTYPE || end == grp) { 196 ERROR(eon->base.context, "Group type descriptor '%s' does not parse", desc->desc); 197 break; 198 } 199 base = eon->type_desc + index; 200 if (!base->desc) { 201 ERROR(eon->base.context, "Group type descriptor '%s' has undescribed index %ld", desc->desc, index); 202 break; 203 } 204 if (!base->size) { 205 ERROR(eon->base.context, "Group type descriptor '%s' uses unsized sub-entry '%s'", desc->desc, base->desc); 206 break; 207 } 208 if (!base->type[0]) { 209 ERROR(eon->base.context, "Group type descriptor '%s' has non-enumerated sub-entry '%s'", desc->desc, base->desc); 210 break; 211 } 212 if (base->type[1]) { 213 ERROR(eon->base.context, "Group type descriptor '%s' has a recursive group sub-entry '%s'", desc->desc, base->desc); 214 break; 215 } 216 if (subtype >= EON_MAX_GROUP-1) { 217 ERROR(eon->base.context, "Group type descriptor '%s' has too many sub-entries", desc->desc); 218 break; 219 } 220 desc->size += base->size; 221 desc->type[subtype++] = base->type[0]; 222 switch (*end) { 223 case 0: 224 return 0; 225 case ',': 226 grp = end+1; 227 continue; 228 default: 229 ERROR(eon->base.context, "Group type descriptor '%s' has unparseable index %ld", desc->desc, index); 230 return -1; 231 } 232 } 233 return -1; 234 } 235 236 /* 237 * Here we cache descriptor data so that we don't have 238 * to re-parse the string all the time. That way we can 239 * do it just once per type. 240 * 241 * Right now we only bother with the sample descriptors, 242 * which all start with "sml.DeviceLog.Samples" (for the 243 * base types) or are "GRP" types that are a group of said 244 * types and are a set of numbers. 245 */ 246 static int fill_in_desc_details(suunto_eonsteel_parser_t *eon, struct type_desc *desc) 247 { 248 if (!desc->desc) 249 return 0; 250 251 if (isdigit(desc->desc[0])) 252 return fill_in_group_details(eon, desc); 253 254 desc->size = lookup_descriptor_size(eon, desc); 255 desc->type[0] = lookup_descriptor_type(eon, desc); 256 return 0; 257 } 258 259 static void 260 desc_free (struct type_desc desc[], unsigned int count) 261 { 262 for (unsigned int i = 0; i < count; ++i) { 263 free(desc[i].desc); 264 free(desc[i].format); 265 free(desc[i].mod); 266 } 267 } 268 269 static int record_type(suunto_eonsteel_parser_t *eon, unsigned short type, const char *name, int namelen) 270 { 271 struct type_desc desc; 272 const char *next; 273 274 memset(&desc, 0, sizeof(desc)); 275 do { 276 int len; 277 char *p; 278 279 next = strchr(name, '\n'); 280 if (next) { 281 len = next - name; 282 next++; 283 } else { 284 len = strlen(name); 285 if (!len) 286 break; 287 } 288 289 if (len < 5 || name[0] != '<' || name[4] != '>') { 290 ERROR(eon->base.context, "Unexpected type description: %.*s", len, name); 291 return -1; 292 } 293 p = (char *) malloc(len-4); 294 if (!p) { 295 ERROR(eon->base.context, "out of memory"); 296 desc_free(&desc, 1); 297 return -1; 298 } 299 memcpy(p, name+5, len-5); 300 p[len-5] = 0; 301 302 // PTH, GRP, FRM, MOD 303 switch (name[1]) { 304 case 'P': 305 case 'G': 306 desc.desc = p; 307 break; 308 case 'F': 309 desc.format = p; 310 break; 311 case 'M': 312 desc.mod = p; 313 break; 314 default: 315 ERROR(eon->base.context, "Unknown type descriptor: %.*s", len, name); 316 desc_free(&desc, 1); 317 free(p); 318 return -1; 319 } 320 } while ((name = next) != NULL); 321 322 if (type >= MAXTYPE) { 323 ERROR(eon->base.context, "Type out of range (%04x: '%s' '%s' '%s')", 324 type, 325 desc.desc ? desc.desc : "", 326 desc.format ? desc.format : "", 327 desc.mod ? desc.mod : ""); 328 desc_free(&desc, 1); 329 return -1; 330 } 331 332 fill_in_desc_details(eon, &desc); 333 334 desc_free(eon->type_desc + type, 1); 335 eon->type_desc[type] = desc; 336 return 0; 337 } 338 339 static int traverse_entry(suunto_eonsteel_parser_t *eon, const unsigned char *p, int size, eon_data_cb_t callback, void *user) 340 { 341 const unsigned char *name, *data, *end, *last, *one_past_end = p + size; 342 int textlen, id; 343 int rc; 344 345 // First two bytes: zero and text length 346 if (p[0]) { 347 HEXDUMP(eon->base.context, DC_LOGLEVEL_DEBUG, "next", p, 8); 348 ERROR(eon->base.context, "Bad dive entry (%02x)", p[0]); 349 return -1; 350 } 351 textlen = p[1]; 352 353 name = p + 2; 354 if (textlen == 0xff) { 355 textlen = array_uint32_le(name); 356 name += 4; 357 } 358 359 // Two bytes of 'type' followed by the name/descriptor, followed by the data 360 data = name + textlen; 361 id = array_uint16_le(name); 362 name += 2; 363 364 if (*name != '<') { 365 HEXDUMP(eon->base.context, DC_LOGLEVEL_DEBUG, "bad", p, 16); 366 return -1; 367 } 368 369 record_type(eon, id, (const char *) name, textlen-3); 370 371 end = data; 372 last = data; 373 while (end < one_past_end && *end) { 374 const unsigned char *begin = end; 375 unsigned int type = *end++; 376 unsigned int len; 377 if (type == 0xff) { 378 type = array_uint16_le(end); 379 end += 2; 380 } 381 len = *end++; 382 383 // I've never actually seen this case yet.. 384 // Just assuming from the other cases. 385 if (len == 0xff) { 386 HEXDUMP(eon->base.context, DC_LOGLEVEL_DEBUG, "len-ff", end, 8); 387 len = array_uint32_le(end); 388 end += 4; 389 } 390 391 if (type >= MAXTYPE || !eon->type_desc[type].desc) { 392 HEXDUMP(eon->base.context, DC_LOGLEVEL_DEBUG, "last", last, 16); 393 HEXDUMP(eon->base.context, DC_LOGLEVEL_DEBUG, "this", begin, 16); 394 } else { 395 rc = callback(type, eon->type_desc+type, end, len, user); 396 if (rc < 0) 397 return rc; 398 } 399 400 last = begin; 401 end += len; 402 } 403 404 return end - p; 405 } 406 407 static int traverse_data(suunto_eonsteel_parser_t *eon, eon_data_cb_t callback, void *user) 408 { 409 const unsigned char *data = eon->base.data; 410 int len = eon->base.size; 411 412 // Dive files start with "SBEM" and four NUL characters 413 // Additionally, we've prepended the time as an extra 414 // 4-byte pre-header 415 if (len < 12 || memcmp(data+4, "SBEM", 4)) 416 return 0; 417 418 data += 12; 419 len -= 12; 420 421 while (len > 4) { 422 int i = traverse_entry(eon, data, len, callback, user); 423 if (i < 0) 424 return 1; 425 len -= i; 426 data += i; 427 } 428 return 0; 429 } 430 431 struct sample_data { 432 suunto_eonsteel_parser_t *eon; 433 dc_sample_callback_t callback; 434 void *userdata; 435 unsigned int time; 436 char *state_type, *notify_type; 437 char *warning_type, *alarm_type; 438 439 /* We gather up deco and cylinder pressure information */ 440 int gasnr; 441 }; 442 443 static void sample_time(struct sample_data *info, unsigned short time_delta) 444 { 445 dc_sample_value_t sample = {0}; 446 447 info->time += time_delta; 448 sample.time = info->time; 449 if (info->callback) info->callback(DC_SAMPLE_TIME, &sample, info->userdata); 450 } 451 452 static void sample_depth(struct sample_data *info, unsigned short depth) 453 { 454 dc_sample_value_t sample = {0}; 455 456 if (depth == 0xffff) 457 return; 458 459 sample.depth = depth / 100.0; 460 if (info->callback) info->callback(DC_SAMPLE_DEPTH, &sample, info->userdata); 461 } 462 463 static void sample_temp(struct sample_data *info, short temp) 464 { 465 dc_sample_value_t sample = {0}; 466 467 if (temp <= -3000) 468 return; 469 470 sample.temperature = temp / 10.0; 471 if (info->callback) info->callback(DC_SAMPLE_TEMPERATURE, &sample, info->userdata); 472 } 473 474 static void sample_ndl(struct sample_data *info, short ndl) 475 { 476 dc_sample_value_t sample = {0}; 477 478 if (ndl < 0) 479 return; 480 481 sample.deco.type = DC_DECO_NDL; 482 sample.deco.time = ndl; 483 sample.deco.tts = 0; 484 if (info->callback) info->callback(DC_SAMPLE_DECO, &sample, info->userdata); 485 } 486 487 static void sample_tts(struct sample_data *info, unsigned short tts) 488 { 489 if (tts != 0xffff) { 490 dc_sample_value_t sample = {0}; 491 sample.time = tts; 492 if (info->callback) info->callback(DC_SAMPLE_TTS, &sample, info->userdata); 493 } 494 } 495 496 static void sample_ceiling(struct sample_data *info, unsigned short ceiling) 497 { 498 if (ceiling != 0xffff) { 499 dc_sample_value_t sample = {0}; 500 501 // We don't actually have a time for the 502 // deco stop, we just have a ceiling. 503 // 504 // We'll just say it's one minute. 505 sample.deco.type = DC_DECO_DECOSTOP; 506 sample.deco.time = ceiling ? 60 : 0; 507 sample.deco.depth = ceiling / 100.0; 508 sample.deco.tts = 0; // Fixme? DC_SAMPLE_TTS? 509 if (info->callback) info->callback(DC_SAMPLE_DECO, &sample, info->userdata); 510 } 511 } 512 513 static void sample_heading(struct sample_data *info, unsigned short heading) 514 { 515 dc_sample_value_t sample = {0}; 516 517 if (heading == 0xffff) 518 return; 519 520 sample.event.type = SAMPLE_EVENT_HEADING; 521 sample.event.value = heading; 522 if (info->callback) info->callback(DC_SAMPLE_EVENT, &sample, info->userdata); 523 } 524 525 static void sample_abspressure(struct sample_data *info, unsigned short pressure) 526 { 527 } 528 529 static void sample_gastime(struct sample_data *info, short gastime) 530 { 531 dc_sample_value_t sample = {0}; 532 533 if (gastime < 0) 534 return; 535 536 sample.rbt = gastime / 60; 537 if (info->callback) info->callback (DC_SAMPLE_RBT, &sample, info->userdata); 538 } 539 540 /* 541 * Per-sample "ventilation" data. 542 * 543 * It's described as: 544 * - "uint16,precision=6,nillable=65535" 545 * - "x/6000000,x" 546 */ 547 static void sample_ventilation(struct sample_data *info, unsigned short unk) 548 { 549 } 550 551 static void sample_gasnr(struct sample_data *info, unsigned char idx) 552 { 553 info->gasnr = idx; 554 } 555 556 static void sample_pressure(struct sample_data *info, unsigned short pressure) 557 { 558 dc_sample_value_t sample = {0}; 559 560 if (pressure == 0xffff) 561 return; 562 563 sample.pressure.tank = info->gasnr-1; 564 sample.pressure.value = pressure / 100.0; 565 if (info->callback) info->callback(DC_SAMPLE_PRESSURE, &sample, info->userdata); 566 } 567 568 static void sample_bookmark_event(struct sample_data *info, unsigned short idx) 569 { 570 dc_sample_value_t sample = {0}; 571 572 sample.event.type = SAMPLE_EVENT_BOOKMARK; 573 sample.event.value = idx; 574 575 if (info->callback) info->callback(DC_SAMPLE_EVENT, &sample, info->userdata); 576 } 577 578 static void sample_gas_switch_event(struct sample_data *info, unsigned short idx) 579 { 580 suunto_eonsteel_parser_t *eon = info->eon; 581 dc_sample_value_t sample = {0}; 582 583 if (idx < 1 || idx > eon->cache.GASMIX_COUNT) 584 return; 585 586 sample.gasmix = idx - 1; 587 if (info->callback) info->callback(DC_SAMPLE_GASMIX, &sample, info->userdata); 588 } 589 590 static const char *mixname(suunto_eonsteel_parser_t *eon, int idx) 591 { 592 dc_gasmix_t *mix; 593 static char name[32]; 594 int o2, he; 595 596 if (idx < 1 || idx > MAXGASES) 597 return "invalid"; 598 599 mix = &eon->cache.GASMIX[idx-1]; 600 o2 = lrint(mix->oxygen * 100); 601 he = lrint(mix->helium * 100); 602 if (he) { 603 snprintf(name, sizeof(name), "%d/%d", o2, he); 604 return name; 605 } 606 if (o2 && o2 != 21) { 607 snprintf(name, sizeof(name), "NX%d", o2); 608 return name; 609 } 610 return "air"; 611 } 612 613 static void sample_insert_gas_event(struct sample_data *info, unsigned short idx) 614 { 615 suunto_eonsteel_parser_t *eon = info->eon; 616 dc_sample_value_t sample = {0}; 617 char event[32]; 618 619 if (!info->callback) 620 return; 621 622 snprintf(event, sizeof(event), "Create gas %d (%s)", idx, mixname(eon, idx)); 623 sample.event.type = SAMPLE_EVENT_STRING; 624 sample.event.name = strdup(event); 625 sample.event.flags = SAMPLE_FLAGS_SEVERITY_INFO; 626 627 info->callback(DC_SAMPLE_EVENT, &sample, info->userdata); 628 } 629 630 static void sample_remove_gas_event(struct sample_data *info, unsigned short idx) 631 { 632 suunto_eonsteel_parser_t *eon = info->eon; 633 dc_sample_value_t sample = {0}; 634 char event[32]; 635 636 if (!info->callback) 637 return; 638 639 snprintf(event, sizeof(event), "Remove gas %d (%s)", idx, mixname(eon, idx)); 640 sample.event.type = SAMPLE_EVENT_STRING; 641 sample.event.name = strdup(event); 642 sample.event.flags = SAMPLE_FLAGS_SEVERITY_INFO; 643 644 info->callback(DC_SAMPLE_EVENT, &sample, info->userdata); 645 } 646 647 /* 648 * Look up the string from an enumeration. 649 * 650 * Enumerations have the enum values in the "format" string, 651 * and all start with "enum:" followed by a comma-separated list 652 * of enumeration values and strings. Example: 653 * 654 * "enum:0=NoFly Time,1=Depth,2=Surface Time,3=..." 655 */ 656 static char *lookup_enum(const struct type_desc *desc, unsigned char value) 657 { 658 const char *str = desc->format; 659 unsigned char c; 660 661 if (!str) 662 return NULL; 663 if (strncmp(str, "enum:", 5)) 664 return NULL; 665 str += 5; 666 667 while ((c = *str) != 0) { 668 unsigned char n; 669 const char *begin, *end; 670 char *ret; 671 672 str++; 673 if (!isdigit(c)) 674 continue; 675 n = c - '0'; 676 677 // We only handle one or two digits 678 if (isdigit(*str)) { 679 n = n*10 + *str - '0'; 680 str++; 681 } 682 683 begin = end = str; 684 while ((c = *str) != 0) { 685 str++; 686 if (c == ',') 687 break; 688 end = str; 689 } 690 691 // Verify that it has the 'n=string' format and skip the equals sign 692 if (*begin != '=') 693 continue; 694 begin++; 695 696 // Is it the value we're looking for? 697 if (n != value) 698 continue; 699 700 ret = (char *)malloc(end - begin + 1); 701 if (!ret) 702 break; 703 704 memcpy(ret, begin, end-begin); 705 ret[end-begin] = 0; 706 return ret; 707 } 708 return NULL; 709 } 710 711 /* 712 * The EON Steel has four different sample events: "state", "notification", 713 * "warning" and "alarm". All end up having two fields: type and a boolean value. 714 */ 715 static void sample_event_state_type(const struct type_desc *desc, struct sample_data *info, unsigned char type) 716 { 717 free(info->state_type); 718 info->state_type = lookup_enum(desc, type); 719 } 720 721 static void sample_event_state_value(const struct type_desc *desc, struct sample_data *info, unsigned char value) 722 { 723 dc_sample_value_t sample = {0}; 724 const char *name; 725 726 name = info->state_type; 727 if (!name) 728 return; 729 730 sample.event.type = SAMPLE_EVENT_STRING; 731 sample.event.name = name; 732 sample.event.flags = value ? SAMPLE_FLAGS_BEGIN : SAMPLE_FLAGS_END; 733 sample.event.flags |= 1 << SAMPLE_FLAGS_SEVERITY_SHIFT; 734 735 if (info->callback) info->callback(DC_SAMPLE_EVENT, &sample, info->userdata); 736 } 737 738 static void sample_event_notify_type(const struct type_desc *desc, struct sample_data *info, unsigned char type) 739 { 740 free(info->notify_type); 741 info->notify_type = lookup_enum(desc, type); 742 } 743 744 static void sample_event_notify_value(const struct type_desc *desc, struct sample_data *info, unsigned char value) 745 { 746 dc_sample_value_t sample = {0}; 747 const char *name; 748 749 name = info->notify_type; 750 if (!name) 751 return; 752 753 sample.event.type = SAMPLE_EVENT_STRING; 754 sample.event.name = name; 755 sample.event.flags = value ? SAMPLE_FLAGS_BEGIN : SAMPLE_FLAGS_END; 756 sample.event.flags |= 2 << SAMPLE_FLAGS_SEVERITY_SHIFT; 757 758 if (info->callback) info->callback(DC_SAMPLE_EVENT, &sample, info->userdata); 759 } 760 761 762 static void sample_event_warning_type(const struct type_desc *desc, struct sample_data *info, unsigned char type) 763 { 764 free(info->warning_type); 765 info->warning_type = lookup_enum(desc, type); 766 } 767 768 static void sample_event_warning_value(const struct type_desc *desc, struct sample_data *info, unsigned char value) 769 { 770 dc_sample_value_t sample = {0}; 771 const char *name; 772 773 name = info->warning_type; 774 if (!name) 775 return; 776 777 sample.event.type = SAMPLE_EVENT_STRING; 778 sample.event.name = name; 779 sample.event.flags = value ? SAMPLE_FLAGS_BEGIN : SAMPLE_FLAGS_END; 780 sample.event.flags |= 3 << SAMPLE_FLAGS_SEVERITY_SHIFT; 781 782 if (info->callback) info->callback(DC_SAMPLE_EVENT, &sample, info->userdata); 783 } 784 785 static void sample_event_alarm_type(const struct type_desc *desc, struct sample_data *info, unsigned char type) 786 { 787 free(info->alarm_type); 788 info->alarm_type = lookup_enum(desc, type); 789 } 790 791 792 static void sample_event_alarm_value(const struct type_desc *desc, struct sample_data *info, unsigned char value) 793 { 794 const char *name; 795 dc_sample_value_t sample = {0}; 796 797 name = info->alarm_type; 798 if (!name) 799 return; 800 801 sample.event.type = SAMPLE_EVENT_STRING; 802 sample.event.name = name; 803 sample.event.flags = value ? SAMPLE_FLAGS_BEGIN : SAMPLE_FLAGS_END; 804 sample.event.flags |= 4 << SAMPLE_FLAGS_SEVERITY_SHIFT; 805 806 if (info->callback) info->callback(DC_SAMPLE_EVENT, &sample, info->userdata); 807 } 808 809 // enum:0=Low,1=High,2=Custom 810 static void sample_setpoint_type(const struct type_desc *desc, struct sample_data *info, unsigned char value) 811 { 812 dc_sample_value_t sample = {0}; 813 char *type = lookup_enum(desc, value); 814 815 if (!type) { 816 DEBUG(info->eon->base.context, "sample_setpoint_type(%u) did not match anything in %s", value, desc->format); 817 return; 818 } 819 820 if (!strcasecmp(type, "Low")) 821 sample.setpoint = info->eon->cache.lowsetpoint; 822 else if (!strcasecmp(type, "High")) 823 sample.setpoint = info->eon->cache.highsetpoint; 824 else if (!strcasecmp(type, "Custom")) 825 sample.setpoint = info->eon->cache.customsetpoint; 826 else { 827 DEBUG(info->eon->base.context, "sample_setpoint_type(%u) unknown type '%s'", value, type); 828 free(type); 829 return; 830 } 831 832 if (info->callback) info->callback(DC_SAMPLE_SETPOINT, &sample, info->userdata); 833 free(type); 834 } 835 836 // uint32 837 static void sample_setpoint_po2(struct sample_data *info, unsigned int pressure) 838 { 839 // I *think* this just sets the custom SP, and then 840 // we'll get a setpoint_type(2) later. 841 info->eon->cache.customsetpoint = pressure / 100000.0; // Pascal to bar 842 } 843 844 static void sample_setpoint_automatic(struct sample_data *info, unsigned char value) 845 { 846 DEBUG(info->eon->base.context, "sample_setpoint_automatic(%u)", value); 847 } 848 849 static unsigned int handle_sample_type(const struct type_desc *desc, struct sample_data *info, enum eon_sample type, const unsigned char *data) 850 { 851 switch (type) { 852 case ES_dtime: 853 sample_time(info, array_uint16_le(data)); 854 return 2; 855 856 case ES_depth: 857 sample_depth(info, array_uint16_le(data)); 858 return 2; 859 860 case ES_temp: 861 sample_temp(info, array_uint16_le(data)); 862 return 2; 863 864 case ES_ndl: 865 sample_ndl(info, array_uint16_le(data)); 866 return 2; 867 868 case ES_ceiling: 869 sample_ceiling(info, array_uint16_le(data)); 870 return 2; 871 872 case ES_tts: 873 sample_tts(info, array_uint16_le(data)); 874 return 2; 875 876 case ES_heading: 877 sample_heading(info, array_uint16_le(data)); 878 return 2; 879 880 case ES_abspressure: 881 sample_abspressure(info, array_uint16_le(data)); 882 return 2; 883 884 case ES_gastime: 885 sample_gastime(info, array_uint16_le(data)); 886 return 2; 887 888 case ES_ventilation: 889 sample_ventilation(info, array_uint16_le(data)); 890 return 2; 891 892 case ES_gasnr: 893 sample_gasnr(info, *data); 894 return 1; 895 896 case ES_pressure: 897 sample_pressure(info, array_uint16_le(data)); 898 return 2; 899 900 case ES_state: 901 sample_event_state_type(desc, info, data[0]); 902 return 1; 903 904 case ES_state_active: 905 sample_event_state_value(desc, info, data[0]); 906 return 1; 907 908 case ES_notify: 909 sample_event_notify_type(desc, info, data[0]); 910 return 1; 911 912 case ES_notify_active: 913 sample_event_notify_value(desc, info, data[0]); 914 return 1; 915 916 case ES_warning: 917 sample_event_warning_type(desc, info, data[0]); 918 return 1; 919 920 case ES_warning_active: 921 sample_event_warning_value(desc, info, data[0]); 922 return 1; 923 924 case ES_alarm: 925 sample_event_alarm_type(desc, info, data[0]); 926 return 1; 927 928 case ES_alarm_active: 929 sample_event_alarm_value(desc, info, data[0]); 930 return 1; 931 932 case ES_bookmark: 933 sample_bookmark_event(info, array_uint16_le(data)); 934 return 2; 935 936 case ES_gasswitch: 937 sample_gas_switch_event(info, array_uint16_le(data)); 938 return 2; 939 940 case ES_setpoint_type: 941 sample_setpoint_type(desc, info, data[0]); 942 return 1; 943 944 case ES_setpoint_po2: 945 sample_setpoint_po2(info, array_uint32_le(data)); 946 return 4; 947 948 case ES_setpoint_automatic: // bool 949 sample_setpoint_automatic(info, data[0]); 950 return 1; 951 952 case ES_insertgas: 953 sample_insert_gas_event(info, array_uint16_le(data)); 954 return 2; 955 956 case ES_removegas: 957 sample_remove_gas_event(info, array_uint16_le(data)); 958 return 2; 959 960 default: 961 return 0; 962 } 963 } 964 965 static int traverse_samples(unsigned short type, const struct type_desc *desc, const unsigned char *data, unsigned int len, void *user) 966 { 967 struct sample_data *info = (struct sample_data *) user; 968 suunto_eonsteel_parser_t *eon = info->eon; 969 int i, used = 0; 970 971 if (desc->size > len) 972 ERROR(eon->base.context, "Got %d bytes of data for '%s' that wants %d bytes", len, desc->desc, desc->size); 973 974 for (i = 0; i < EON_MAX_GROUP; i++) { 975 unsigned int bytes = handle_sample_type(desc, info, desc->type[i], data); 976 977 if (!bytes) 978 break; 979 if (bytes > len) { 980 ERROR(eon->base.context, "Wanted %d bytes of data, only had %d bytes ('%s' idx %d)", bytes, len, desc->desc, i); 981 break; 982 } 983 data += bytes; 984 len -= bytes; 985 used += bytes; 986 } 987 988 // Warn if there are left-over bytes for something we did use part of 989 if (used && len) 990 ERROR(eon->base.context, "Entry for '%s' had %d bytes, only used %d", desc->desc, len+used, used); 991 return 0; 992 } 993 994 static dc_status_t 995 suunto_eonsteel_parser_samples_foreach(dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata) 996 { 997 suunto_eonsteel_parser_t *eon = (suunto_eonsteel_parser_t *) abstract; 998 struct sample_data data = { eon, callback, userdata, 0 }; 999 1000 traverse_data(eon, traverse_samples, &data); 1001 1002 free(data.state_type); 1003 free(data.notify_type); 1004 free(data.warning_type); 1005 free(data.alarm_type); 1006 1007 return DC_STATUS_SUCCESS; 1008 } 1009 1010 static dc_status_t 1011 suunto_eonsteel_parser_get_field(dc_parser_t *parser, dc_field_type_t type, unsigned int flags, void *value) 1012 { 1013 suunto_eonsteel_parser_t *eon = (suunto_eonsteel_parser_t *)parser; 1014 1015 if (!(eon->cache.initialized & (1 << type))) 1016 return DC_STATUS_UNSUPPORTED; 1017 1018 /* Fix up the cylinder info before using dc_field_get() */ 1019 if (type == DC_FIELD_TANK) { 1020 if (flags >= MAXGASES) 1021 return DC_STATUS_UNSUPPORTED; 1022 1023 dc_tank_t *tank = (dc_tank_t *) value; 1024 1025 /* 1026 * Sadly it seems that the EON Steel doesn't tell us whether 1027 * we get imperial or metric data - the only indication is 1028 * that metric is (at least so far) always whole liters 1029 */ 1030 tank->volume = eon->cache.tanksize[flags]; 1031 tank->gasmix = flags; 1032 1033 /* 1034 * The pressure reported is NOT the pressure the user enters. 1035 * 1036 * So 3000psi turns into 206.700 bar instead of 206.843 bar; 1037 * We report it as we get it and let the application figure out 1038 * what to do with that 1039 */ 1040 tank->workpressure = eon->cache.tankworkingpressure[flags]; 1041 tank->type = eon->cache.tankinfo[flags]; 1042 1043 /* 1044 * See if we should call this imperial instead. 1045 * 1046 * We need to have workpressure and a valid tank. In that case, 1047 * a fractional tank size implies imperial. 1048 */ 1049 if (tank->workpressure && (tank->type & DC_TANKINFO_METRIC)) { 1050 if (fabs(tank->volume - rint(tank->volume)) > 0.001) 1051 tank->type += DC_TANKINFO_IMPERIAL - DC_TANKINFO_METRIC; 1052 } 1053 tank->usage = eon->cache.tankusage[flags]; 1054 } 1055 1056 return dc_field_get(&eon->cache, type, flags, value); 1057 } 1058 1059 /* 1060 * The time of the dive is encoded in the filename, 1061 * and we've saved it off as the four first bytes 1062 * of the dive data (in little-endian format). 1063 */ 1064 static dc_status_t 1065 suunto_eonsteel_parser_get_datetime(dc_parser_t *parser, dc_datetime_t *datetime) 1066 { 1067 if (parser->size < 4) 1068 return DC_STATUS_UNSUPPORTED; 1069 1070 if (!dc_datetime_gmtime(datetime, array_uint32_le(parser->data))) 1071 return DC_STATUS_DATAFORMAT; 1072 1073 datetime->timezone = DC_TIMEZONE_NONE; 1074 1075 return DC_STATUS_SUCCESS; 1076 } 1077 1078 // time in ms 1079 static void add_time_field(suunto_eonsteel_parser_t *eon, unsigned short time_delta_ms) 1080 { 1081 eon->cache.DIVETIME += time_delta_ms; 1082 } 1083 1084 // depth in cm 1085 static void set_depth_field(suunto_eonsteel_parser_t *eon, unsigned short d) 1086 { 1087 if (d != 0xffff) { 1088 double depth = d / 100.0; 1089 if (depth > eon->cache.MAXDEPTH) 1090 eon->cache.MAXDEPTH = depth; 1091 eon->cache.initialized |= 1 << DC_FIELD_MAXDEPTH; 1092 } 1093 } 1094 1095 // new gas: 1096 // "sml.DeviceLog.Header.Diving.Gases+Gas.State" 1097 // 1098 // We eventually need to parse the descriptor for that 'enum type'. 1099 // Two versions so far: 1100 // "enum:0=Off,1=Primary,2=?,3=Diluent" 1101 // "enum:0=Off,1=Primary,3=Diluent,4=Oxygen" 1102 // 1103 // We turn that into the DC_TANKINFO data here, but 1104 // initially consider all non-off tanks to me METRIC. 1105 // 1106 // We may later turn the METRIC tank size into IMPERIAL if we 1107 // get a working pressure and non-integral size 1108 static dc_status_t add_gas_type(suunto_eonsteel_parser_t *eon, const struct type_desc *desc, unsigned char type) 1109 { 1110 int idx = eon->cache.GASMIX_COUNT; 1111 dc_tankinfo_t tankinfo = DC_TANKINFO_METRIC; 1112 dc_usage_t usage = DC_USAGE_NONE; 1113 char *name; 1114 1115 if (idx >= MAXGASES) 1116 return DC_STATUS_SUCCESS; 1117 1118 eon->cache.GASMIX_COUNT = idx+1; 1119 name = lookup_enum(desc, type); 1120 if (!name) 1121 DEBUG(eon->base.context, "Unable to look up gas type %u in %s", type, desc->format); 1122 else if (!strcasecmp(name, "Diluent")) 1123 usage = DC_USAGE_DILUENT; 1124 else if (!strcasecmp(name, "Oxygen")) 1125 usage = DC_USAGE_OXYGEN; 1126 else if (!strcasecmp(name, "None")) 1127 tankinfo = DC_TANKVOLUME_NONE; 1128 else if (strcasecmp(name, "Primary")) 1129 DEBUG(eon->base.context, "Unknown gas type %u (%s)", type, name); 1130 if (name && strcasecmp(name, "Diluent") && strcasecmp(name, "Oxygen")) 1131 usage = DC_USAGE_OPEN_CIRCUIT; 1132 1133 eon->cache.tankinfo[idx] = tankinfo; 1134 eon->cache.tankusage[idx] = DC_TANK_USAGE_NONE; 1135 eon->cache.GASMIX[idx].usage = usage; 1136 1137 eon->cache.initialized |= 1 << DC_FIELD_GASMIX_COUNT; 1138 eon->cache.initialized |= 1 << DC_FIELD_TANK_COUNT; 1139 free(name); 1140 return DC_STATUS_SUCCESS; 1141 } 1142 1143 // "sml.DeviceLog.Header.Diving.Gases.Gas.Oxygen" 1144 // O2 percentage as a byte 1145 static dc_status_t add_gas_o2(suunto_eonsteel_parser_t *eon, unsigned char o2) 1146 { 1147 int idx = eon->cache.GASMIX_COUNT-1; 1148 if (idx >= 0) 1149 eon->cache.GASMIX[idx].oxygen = o2 / 100.0; 1150 eon->cache.initialized |= 1 << DC_FIELD_GASMIX; 1151 return DC_STATUS_SUCCESS; 1152 } 1153 1154 // "sml.DeviceLog.Header.Diving.Gases.Gas.Helium" 1155 // He percentage as a byte 1156 static dc_status_t add_gas_he(suunto_eonsteel_parser_t *eon, unsigned char he) 1157 { 1158 int idx = eon->cache.GASMIX_COUNT-1; 1159 if (idx >= 0) 1160 eon->cache.GASMIX[idx].helium = he / 100.0; 1161 eon->cache.initialized |= 1 << DC_FIELD_GASMIX; 1162 return DC_STATUS_SUCCESS; 1163 } 1164 1165 static dc_status_t add_gas_size(suunto_eonsteel_parser_t *eon, float l) 1166 { 1167 int idx = eon->cache.GASMIX_COUNT-1; 1168 if (idx >= 0) 1169 eon->cache.tanksize[idx] = l; 1170 eon->cache.initialized |= 1 << DC_FIELD_TANK; 1171 return DC_STATUS_SUCCESS; 1172 } 1173 1174 static dc_status_t add_gas_workpressure(suunto_eonsteel_parser_t *eon, float wp) 1175 { 1176 int idx = eon->cache.GASMIX_COUNT-1; 1177 if (idx >= 0) 1178 eon->cache.tankworkingpressure[idx] = wp; 1179 return DC_STATUS_SUCCESS; 1180 } 1181 1182 static float get_le32_float(const unsigned char *src) 1183 { 1184 union { 1185 unsigned int val; 1186 float result; 1187 } u; 1188 1189 u.val = array_uint32_le(src); 1190 return u.result; 1191 } 1192 1193 // "Device" fields are all utf8: 1194 // Info.BatteryAtEnd 1195 // Info.BatteryAtStart 1196 // Info.BSL 1197 // Info.HW 1198 // Info.SW 1199 // Name 1200 // SerialNumber 1201 static dc_status_t traverse_device_fields(suunto_eonsteel_parser_t *eon, 1202 const struct type_desc *desc, 1203 const unsigned char *data, int len) 1204 { 1205 const char *name = desc->desc + strlen("sml.DeviceLog.Device."); 1206 if (!strcmp(name, "SerialNumber")) 1207 return dc_field_add_string(&eon->cache, "Serial", data); 1208 if (!strcmp(name, "Info.HW")) 1209 return dc_field_add_string(&eon->cache, "HW Version", data); 1210 if (!strcmp(name, "Info.SW")) 1211 return dc_field_add_string(&eon->cache, STRING_KEY_FIRMWARE_VERSION, data); 1212 if (!strcmp(name, "Info.BatteryAtStart")) 1213 return dc_field_add_string(&eon->cache, "Battery at start", data); 1214 if (!strcmp(name, "Info.BatteryAtEnd")) 1215 return dc_field_add_string(&eon->cache, "Battery at end", data); 1216 return DC_STATUS_SUCCESS; 1217 } 1218 1219 // "sml.DeviceLog.Header.Diving.Gases" 1220 // 1221 // +Gas.State (enum:0=Off,1=Primary,3=Diluent,4=Oxygen) 1222 // .Gas.Oxygen (uint8,precision=2) 1223 // .Gas.Helium (uint8,precision=2) 1224 // .Gas.PO2 (uint32) 1225 // .Gas.TransmitterID (utf8) 1226 // .Gas.TankSize (float32,precision=5) 1227 // .Gas.TankFillPressure (float32,precision=0) 1228 // .Gas.StartPressure (float32,precision=0) 1229 // .Gas.EndPressure (float32,precision=0) 1230 // .Gas.TransmitterStartBatteryCharge (int8,precision=2) 1231 // .Gas.TransmitterEndBatteryCharge (int8,precision=2) 1232 static dc_status_t traverse_gas_fields(suunto_eonsteel_parser_t *eon, 1233 const struct type_desc *desc, 1234 const unsigned char *data, int len) 1235 { 1236 const char *name = desc->desc + strlen("sml.DeviceLog.Header.Diving.Gases"); 1237 1238 if (!strcmp(name, "+Gas.State")) 1239 return add_gas_type(eon, desc, data[0]); 1240 1241 if (!strcmp(name, ".Gas.Oxygen")) 1242 return add_gas_o2(eon, data[0]); 1243 1244 if (!strcmp(name, ".Gas.Helium")) 1245 return add_gas_he(eon, data[0]); 1246 1247 if (!strcmp(name, ".Gas.TransmitterID")) 1248 return dc_field_add_string(&eon->cache, "Transmitter ID", data); 1249 1250 if (!strcmp(name, ".Gas.TankSize")) 1251 return add_gas_size(eon, get_le32_float(data)); 1252 1253 if (!strcmp(name, ".Gas.TankFillPressure")) 1254 return add_gas_workpressure(eon, get_le32_float(data)); 1255 1256 // There is a bug with older transmitters, where the transmitter 1257 // battery charge returns zero. Rather than returning that bogus 1258 // data, just don't return any battery charge information at all. 1259 // 1260 // Make sure to add all non-battery-charge field checks above this 1261 // test, so that it doesn't trigger for anything else. 1262 if (!data[0]) 1263 return 0; 1264 1265 if (!strcmp(name, ".Gas.TransmitterStartBatteryCharge")) 1266 return dc_field_add_string_fmt(&eon->cache, "Transmitter Battery at start", "%d %%", data[0]); 1267 1268 if (!strcmp(name, ".Gas.TransmitterEndBatteryCharge")) 1269 return dc_field_add_string_fmt(&eon->cache, "Transmitter Battery at end", "%d %%", data[0]); 1270 1271 return DC_STATUS_SUCCESS; 1272 } 1273 1274 1275 // "sml.DeviceLog.Header.Diving." 1276 // 1277 // SurfaceTime (uint32) 1278 // NumberInSeries (uint32) 1279 // Algorithm (utf8) 1280 // SurfacePressure (uint32) 1281 // Conservatism (int8) 1282 // Altitude (uint16) 1283 // AlgorithmTransitionDepth (uint8) 1284 // DaysInSeries (uint32) 1285 // PreviousDiveDepth (float32,precision=2) 1286 // LowSetPoint (uint32) 1287 // HighSetPoint (uint32) 1288 // SwitchHighSetPoint.Enabled (bool) 1289 // SwitchHighSetPoint.Depth (float32,precision=1) 1290 // SwitchLowSetPoint.Enabled (bool) 1291 // SwitchLowSetPoint.Depth (float32,precision=1) 1292 // StartTissue.CNS (float32,precision=3) 1293 // StartTissue.OTU (float32) 1294 // StartTissue.OLF (float32,precision=3) 1295 // StartTissue.Nitrogen+Pressure (uint32) 1296 // StartTissue.Helium+Pressure (uint32) 1297 // StartTissue.RgbmNitrogen (float32,precision=3) 1298 // StartTissue.RgbmHelium (float32,precision=3) 1299 // DiveMode (utf8) 1300 // AlgorithmBottomTime (uint32) 1301 // AlgorithmAscentTime (uint32) 1302 // AlgorithmBottomMixture.Oxygen (uint8,precision=2) 1303 // AlgorithmBottomMixture.Helium (uint8,precision=2) 1304 // DesaturationTime (uint32) 1305 // EndTissue.CNS (float32,precision=3) 1306 // EndTissue.OTU (float32) 1307 // EndTissue.OLF (float32,precision=3) 1308 // EndTissue.Nitrogen+Pressure (uint32) 1309 // EndTissue.Helium+Pressure (uint32) 1310 // EndTissue.RgbmNitrogen (float32,precision=3) 1311 // EndTissue.RgbmHelium (float32,precision=3) 1312 static dc_status_t traverse_diving_fields(suunto_eonsteel_parser_t *eon, 1313 const struct type_desc *desc, 1314 const unsigned char *data, int len) 1315 { 1316 const char *name = desc->desc + strlen("sml.DeviceLog.Header.Diving."); 1317 1318 if (!strncmp(name, "Gases", 5)) 1319 return traverse_gas_fields(eon, desc, data, len); 1320 1321 if (!strcmp(name, "SurfacePressure")) { 1322 unsigned int pressure = array_uint32_le(data); // in SI units - Pascal 1323 DC_ASSIGN_FIELD(eon->cache, ATMOSPHERIC, pressure / 100000.0); // bar 1324 return DC_STATUS_SUCCESS; 1325 } 1326 1327 if (!strcmp(name, "Algorithm")) 1328 return dc_field_add_string(&eon->cache, "Deco algorithm", data); 1329 1330 if (!strcmp(name, "DiveMode")) { 1331 if (!strncmp((const char *)data, "CCR", 3)) { 1332 DC_ASSIGN_FIELD(eon->cache, DIVEMODE, DC_DIVEMODE_CCR); 1333 } 1334 return dc_field_add_string(&eon->cache, "Dive Mode", data); 1335 } 1336 1337 /* Signed byte of conservatism (-2 .. +2) */ 1338 if (!strcmp(name, "Conservatism")) { 1339 int val = *(signed char *)data; 1340 1341 return dc_field_add_string_fmt(&eon->cache, "Personal Adjustment", "P%d", val); 1342 } 1343 1344 if (!strcmp(name, "LowSetPoint")) { 1345 unsigned int pressure = array_uint32_le(data); // in SI units - Pascal 1346 eon->cache.lowsetpoint = pressure / 100000.0; // bar 1347 return 0; 1348 } 1349 1350 if (!strcmp(name, "HighSetPoint")) { 1351 unsigned int pressure = array_uint32_le(data); // in SI units - Pascal 1352 eon->cache.highsetpoint = pressure / 100000.0; // bar 1353 return 0; 1354 } 1355 1356 // Time recoded in seconds. 1357 // Let's just agree to ignore seconds 1358 if (!strcmp(name, "DesaturationTime")) { 1359 unsigned int time = array_uint32_le(data) / 60; 1360 return dc_field_add_string_fmt(&eon->cache, "Desaturation Time", "%d:%02d", time / 60, time % 60); 1361 } 1362 1363 if (!strcmp(name, "SurfaceTime")) { 1364 unsigned int time = array_uint32_le(data) / 60; 1365 return dc_field_add_string_fmt(&eon->cache, "Surface Time", "%d:%02d", time / 60, time % 60); 1366 } 1367 1368 return DC_STATUS_SUCCESS; 1369 } 1370 1371 // "Header" fields are: 1372 // Activity (utf8) 1373 // DateTime (utf8) 1374 // Depth.Avg (float32,precision=2) 1375 // Depth.Max (float32,precision=2) 1376 // Diving.* 1377 // Duration (uint32) 1378 // PauseDuration (uint32) 1379 // SampleInterval (uint8) 1380 static dc_status_t traverse_header_fields(suunto_eonsteel_parser_t *eon, 1381 const struct type_desc *desc, 1382 const unsigned char *data, int len) 1383 { 1384 const char *name = desc->desc + strlen("sml.DeviceLog.Header."); 1385 1386 if (!strncmp(name, "Diving.", 7)) 1387 return traverse_diving_fields(eon, desc, data, len); 1388 1389 if (!strcmp(name, "Depth.Max")) { 1390 double d = get_le32_float(data); 1391 if (d > eon->cache.MAXDEPTH) 1392 DC_ASSIGN_FIELD(eon->cache, MAXDEPTH, d); 1393 return DC_STATUS_SUCCESS; 1394 } 1395 if (!strcmp(name, "DateTime")) 1396 return dc_field_add_string(&eon->cache, "Dive ID", data); 1397 1398 return DC_STATUS_SUCCESS; 1399 } 1400 1401 static dc_status_t traverse_dynamic_fields(suunto_eonsteel_parser_t *eon, const struct type_desc *desc, const unsigned char *data, int len) 1402 { 1403 const char *name = desc->desc; 1404 1405 if (!strncmp(name, "sml.", 4)) { 1406 name += 4; 1407 if (!strncmp(name, "DeviceLog.", 10)) { 1408 name += 10; 1409 if (!strncmp(name, "Device.", 7)) 1410 return traverse_device_fields(eon, desc, data, len); 1411 if (!strncmp(name, "Header.", 7)) { 1412 return traverse_header_fields(eon, desc, data, len); 1413 } 1414 } 1415 } 1416 return DC_STATUS_SUCCESS; 1417 } 1418 1419 /* 1420 * This is a simplified sample parser that only parses the depth and time 1421 * samples. It also depends on the GRP entries always starting with time/depth, 1422 * and just stops on anything else. 1423 */ 1424 static int traverse_sample_fields(suunto_eonsteel_parser_t *eon, const struct type_desc *desc, const unsigned char *data, int len) 1425 { 1426 int i; 1427 1428 for (i = 0; i < EON_MAX_GROUP; i++) { 1429 enum eon_sample type = desc->type[i]; 1430 1431 switch (type) { 1432 case ES_dtime: 1433 add_time_field(eon, array_uint16_le(data)); 1434 data += 2; 1435 continue; 1436 case ES_depth: 1437 set_depth_field(eon, array_uint16_le(data)); 1438 data += 2; 1439 continue; 1440 default: 1441 break; 1442 } 1443 break; 1444 } 1445 return 0; 1446 } 1447 1448 static int traverse_fields(unsigned short type, const struct type_desc *desc, const unsigned char *data, unsigned int len, void *user) 1449 { 1450 suunto_eonsteel_parser_t *eon = (suunto_eonsteel_parser_t *) user; 1451 1452 // Sample type? Do basic maxdepth and time parsing 1453 if (desc->type[0]) 1454 traverse_sample_fields(eon, desc, data, len); 1455 else 1456 traverse_dynamic_fields(eon, desc, data, len); 1457 1458 return 0; 1459 } 1460 1461 1462 static void initialize_field_caches(suunto_eonsteel_parser_t *eon) 1463 { 1464 memset(&eon->cache, 0, sizeof(eon->cache)); 1465 eon->cache.initialized = 1 << DC_FIELD_DIVETIME; 1466 1467 traverse_data(eon, traverse_fields, eon); 1468 1469 // The internal time fields are in ms and have to be added up 1470 // like that. At the end, we translate it back to seconds. 1471 eon->cache.DIVETIME /= 1000; 1472 } 1473 1474 static void show_descriptor(suunto_eonsteel_parser_t *eon, int nr, struct type_desc *desc) 1475 { 1476 int i; 1477 1478 if (!desc->desc) 1479 return; 1480 DEBUG(eon->base.context, "Descriptor %d: '%s', size %d bytes", nr, desc->desc, desc->size); 1481 if (desc->format) 1482 DEBUG(eon->base.context, " format '%s'", desc->format); 1483 if (desc->mod) 1484 DEBUG(eon->base.context, " mod '%s'", desc->mod); 1485 for (i = 0; i < EON_MAX_GROUP; i++) { 1486 enum eon_sample type = desc->type[i]; 1487 if (!type) 1488 continue; 1489 DEBUG(eon->base.context, " %d: %d (%s)", i, type, desc_type_name(type)); 1490 } 1491 } 1492 1493 static void show_all_descriptors(suunto_eonsteel_parser_t *eon) 1494 { 1495 for (unsigned int i = 0; i < MAXTYPE; ++i) 1496 show_descriptor(eon, i, eon->type_desc+i); 1497 } 1498 1499 static dc_status_t 1500 suunto_eonsteel_parser_destroy(dc_parser_t *parser) 1501 { 1502 suunto_eonsteel_parser_t *eon = (suunto_eonsteel_parser_t *) parser; 1503 1504 dc_field_cache_free (&eon->cache); 1505 desc_free(eon->type_desc, MAXTYPE); 1506 1507 return DC_STATUS_SUCCESS; 1508 } 1509 1510 static const dc_parser_vtable_t suunto_eonsteel_parser_vtable = { 1511 sizeof(suunto_eonsteel_parser_t), 1512 DC_FAMILY_SUUNTO_EONSTEEL, 1513 NULL, /* set_clock */ 1514 NULL, /* set_atmospheric */ 1515 NULL, /* set_density */ 1516 suunto_eonsteel_parser_get_datetime, /* datetime */ 1517 suunto_eonsteel_parser_get_field, /* fields */ 1518 suunto_eonsteel_parser_samples_foreach, /* samples_foreach */ 1519 suunto_eonsteel_parser_destroy /* destroy */ 1520 }; 1521 1522 dc_status_t 1523 suunto_eonsteel_parser_create(dc_parser_t **out, dc_context_t *context, const unsigned char data[], size_t size, unsigned int model) 1524 { 1525 suunto_eonsteel_parser_t *parser = NULL; 1526 1527 if (out == NULL) 1528 return DC_STATUS_INVALIDARGS; 1529 1530 parser = (suunto_eonsteel_parser_t *) dc_parser_allocate (context, &suunto_eonsteel_parser_vtable, data, size); 1531 if (parser == NULL) { 1532 ERROR (context, "Failed to allocate memory."); 1533 return DC_STATUS_NOMEMORY; 1534 } 1535 1536 memset(&parser->type_desc, 0, sizeof(parser->type_desc)); 1537 memset(&parser->cache, 0, sizeof(parser->cache)); 1538 1539 initialize_field_caches(parser); 1540 show_all_descriptors(parser); 1541 1542 *out = (dc_parser_t *) parser; 1543 1544 return DC_STATUS_SUCCESS; 1545 }