cochran_commander_parser.c (30177B)
1 /* 2 * libdivecomputer 3 * 4 * Copyright (C) 2014 John Van Ostrand 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 <math.h> 24 25 #include <libdivecomputer/units.h> 26 27 #include "cochran_commander.h" 28 #include "context-private.h" 29 #include "parser-private.h" 30 #include "platform.h" 31 #include "array.h" 32 33 #define COCHRAN_MODEL_COMMANDER_TM 0 34 #define COCHRAN_MODEL_COMMANDER_PRE21000 1 35 #define COCHRAN_MODEL_COMMANDER_AIR_NITROX 2 36 #define COCHRAN_MODEL_EMC_14 3 37 #define COCHRAN_MODEL_EMC_16 4 38 #define COCHRAN_MODEL_EMC_20 5 39 40 // Cochran time stamps start at Jan 1, 1992 41 #define COCHRAN_EPOCH 694242000 42 43 #define UNSUPPORTED 0xFFFFFFFF 44 45 typedef enum cochran_sample_format_t { 46 SAMPLE_TM, 47 SAMPLE_CMDR, 48 SAMPLE_EMC, 49 } cochran_sample_format_t; 50 51 52 typedef enum cochran_date_encoding_t { 53 DATE_ENCODING_MSDHYM, 54 DATE_ENCODING_SMHDMY, 55 DATE_ENCODING_TICKS, 56 } cochran_date_encoding_t; 57 58 typedef struct cochran_parser_layout_t { 59 cochran_sample_format_t format; 60 unsigned int headersize; 61 unsigned int samplesize; 62 unsigned int pt_sample_interval; 63 cochran_date_encoding_t date_encoding; 64 unsigned int datetime; 65 unsigned int pt_profile_begin; 66 unsigned int water_conductivity; 67 unsigned int pt_profile_pre; 68 unsigned int start_temp; 69 unsigned int start_depth; 70 unsigned int dive_number; 71 unsigned int altitude; 72 unsigned int pt_profile_end; 73 unsigned int end_temp; 74 unsigned int divetime; 75 unsigned int max_depth; 76 unsigned int avg_depth; 77 unsigned int oxygen; 78 unsigned int helium; 79 unsigned int min_temp; 80 unsigned int max_temp; 81 } cochran_parser_layout_t; 82 83 typedef struct cochran_events_t { 84 unsigned char code; 85 unsigned int data_bytes; 86 parser_sample_event_t type; 87 parser_sample_flags_t flag; 88 } cochran_events_t; 89 90 typedef struct event_size_t { 91 unsigned int code; 92 unsigned int size; 93 } event_size_t; 94 95 typedef struct cochran_commander_parser_t { 96 dc_parser_t base; 97 unsigned int model; 98 const cochran_parser_layout_t *layout; 99 const event_size_t *events; 100 unsigned int nevents; 101 } cochran_commander_parser_t ; 102 103 static dc_status_t cochran_commander_parser_get_datetime (dc_parser_t *parser, dc_datetime_t *datetime); 104 static dc_status_t cochran_commander_parser_get_field (dc_parser_t *parser, dc_field_type_t type, unsigned int flags, void *value); 105 static dc_status_t cochran_commander_parser_samples_foreach (dc_parser_t *parser, dc_sample_callback_t callback, void *userdata); 106 107 static const dc_parser_vtable_t cochran_commander_parser_vtable = { 108 sizeof(cochran_commander_parser_t), 109 DC_FAMILY_COCHRAN_COMMANDER, 110 NULL, /* set_clock */ 111 NULL, /* set_atmospheric */ 112 NULL, /* set_density */ 113 cochran_commander_parser_get_datetime, /* datetime */ 114 cochran_commander_parser_get_field, /* fields */ 115 cochran_commander_parser_samples_foreach, /* samples_foreach */ 116 NULL /* destroy */ 117 }; 118 119 static const cochran_parser_layout_t cochran_cmdr_tm_parser_layout = { 120 SAMPLE_TM, // format 121 90, // headersize 122 1, // samplesize 123 72, // pt_sample_interval 124 DATE_ENCODING_TICKS, // date_encoding 125 15, // datetime, 4 bytes 126 0, // pt_profile_begin, 4 bytes 127 UNSUPPORTED, // water_conductivity, 1 byte, 0=low(fresh), 2=high(sea) 128 0, // pt_profile_pre, 4 bytes 129 83, // start_temp, 1 byte, F 130 UNSUPPORTED, // start_depth, 2 bytes, /4=ft 131 20, // dive_number, 2 bytes 132 UNSUPPORTED, // altitude, 1 byte, /4=kilofeet 133 UNSUPPORTED, // pt_profile_end, 4 bytes 134 UNSUPPORTED, // end_temp, 1 byte F 135 57, // divetime, 2 bytes, minutes 136 49, // max_depth, 2 bytes, /4=ft 137 51, // avg_depth, 2 bytes, /4=ft 138 74, // oxygen, 4 bytes (2 of) 2 bytes, /256=% 139 UNSUPPORTED, // helium, 4 bytes (2 of) 2 bytes, /256=% 140 82, // min_temp, 1 byte, /2+20=F 141 UNSUPPORTED, // max_temp, 1 byte, /2+20=F 142 }; 143 144 static const cochran_parser_layout_t cochran_cmdr_1_parser_layout = { 145 SAMPLE_CMDR, // format 146 256, // headersize 147 2, // samplesize 148 UNSUPPORTED, // pt_sample_interval 149 DATE_ENCODING_TICKS, // date_encoding 150 8, // datetime, 4 bytes 151 0, // pt_profile_begin, 4 bytes 152 24, // water_conductivity, 1 byte, 0=low(fresh), 2=high(sea) 153 28, // pt_profile_pre, 4 bytes 154 43, // start_temp, 1 byte, F 155 54, // start_depth, 2 bytes, /4=ft 156 68, // dive_number, 2 bytes 157 73, // altitude, 1 byte, /4=kilofeet 158 128, // pt_profile_end, 4 bytes 159 153, // end_temp, 1 byte F 160 166, // divetime, 2 bytes, minutes 161 168, // max_depth, 2 bytes, /4=ft 162 170, // avg_depth, 2 bytes, /4=ft 163 210, // oxygen, 4 bytes (2 of) 2 bytes, /256=% 164 UNSUPPORTED, // helium, 4 bytes (2 of) 2 bytes, /256=% 165 232, // min_temp, 1 byte, /2+20=F 166 233, // max_temp, 1 byte, /2+20=F 167 }; 168 169 static const cochran_parser_layout_t cochran_cmdr_parser_layout = { 170 SAMPLE_CMDR, // format 171 256, // headersize 172 2, // samplesize 173 UNSUPPORTED, // pt_sample_interval 174 DATE_ENCODING_MSDHYM, // date_encoding 175 0, // datetime, 6 bytes 176 6, // pt_profile_begin, 4 bytes 177 24, // water_conductivity, 1 byte, 0=low(fresh), 2=high(sea) 178 30, // pt_profile_pre, 4 bytes 179 45, // start_temp, 1 byte, F 180 56, // start_depth, 2 bytes, /4=ft 181 70, // dive_number, 2 bytes 182 73, // altitude, 1 byte, /4=kilofeet 183 128, // pt_profile_end, 4 bytes 184 153, // end_temp, 1 byte F 185 166, // divetime, 2 bytes, minutes 186 168, // max_depth, 2 bytes, /4=ft 187 170, // avg_depth, 2 bytes, /4=ft 188 210, // oxygen, 4 bytes (2 of) 2 bytes, /256=% 189 UNSUPPORTED, // helium, 4 bytes (2 of) 2 bytes, /256=% 190 232, // min_temp, 1 byte, /2+20=F 191 233, // max_temp, 1 byte, /2+20=F 192 }; 193 194 static const cochran_parser_layout_t cochran_emc_parser_layout = { 195 SAMPLE_EMC, // format 196 512, // headersize 197 3, // samplesize 198 UNSUPPORTED, // pt_sample_interval 199 DATE_ENCODING_SMHDMY, // date_encoding 200 0, // datetime, 6 bytes 201 6, // pt_profile_begin, 4 bytes 202 24, // water_conductivity, 1 byte 0=low(fresh), 2=high(sea) 203 30, // pt_profile_pre, 4 bytes 204 55, // start_temp, 1 byte, F 205 42, // start_depth, 2 bytes, /256=ft 206 86, // dive_number, 2 bytes, 207 89, // altitude, 1 byte /4=kilofeet 208 256, // pt_profile_end, 4 bytes 209 293, // end_temp, 1 byte, F 210 304, // divetime, 2 bytes, minutes 211 306, // max_depth, 2 bytes, /4=ft 212 310, // avg_depth, 2 bytes, /4=ft 213 144, // oxygen, 6 bytes (3 of) 2 bytes, /256=% 214 164, // helium, 6 bytes (3 of) 2 bytes, /256=% 215 403, // min_temp, 1 byte, /2+20=F 216 407, // max_temp, 1 byte, /2+20=F 217 }; 218 219 static const cochran_events_t cochran_events[] = { 220 {0xA8, 1, SAMPLE_EVENT_SURFACE, SAMPLE_FLAGS_BEGIN}, // Entered PDI mode 221 {0xA9, 1, SAMPLE_EVENT_SURFACE, SAMPLE_FLAGS_END}, // Exited PDI mode 222 {0xAB, 5, SAMPLE_EVENT_NONE, SAMPLE_FLAGS_NONE}, // Ceiling decrease 223 {0xAD, 5, SAMPLE_EVENT_NONE, SAMPLE_FLAGS_NONE}, // Ceiling increase 224 {0xB5, 1, SAMPLE_EVENT_AIRTIME, SAMPLE_FLAGS_BEGIN}, // Air < 5 mins deco 225 {0xBD, 1, SAMPLE_EVENT_NONE, SAMPLE_FLAGS_NONE}, // Switched to nomal PO2 setting 226 {0xBE, 1, SAMPLE_EVENT_NONE, SAMPLE_FLAGS_NONE}, // Ceiling > 60 ft 227 {0xC0, 1, SAMPLE_EVENT_NONE, SAMPLE_FLAGS_NONE}, // Switched to FO2 21% mode 228 {0xC1, 1, SAMPLE_EVENT_ASCENT, SAMPLE_FLAGS_BEGIN}, // Ascent rate greater than limit 229 {0xC2, 1, SAMPLE_EVENT_NONE, SAMPLE_FLAGS_NONE}, // Low battery warning 230 {0xC3, 1, SAMPLE_EVENT_OLF, SAMPLE_FLAGS_NONE}, // CNS Oxygen toxicity warning 231 {0xC4, 1, SAMPLE_EVENT_MAXDEPTH, SAMPLE_FLAGS_NONE}, // Depth exceeds user set point 232 {0xC5, 1, SAMPLE_EVENT_NONE, SAMPLE_FLAGS_BEGIN}, // Entered decompression mode 233 {0xC7, 1, SAMPLE_EVENT_VIOLATION,SAMPLE_FLAGS_BEGIN}, // Entered Gauge mode (e.g. locked out) 234 {0xC8, 1, SAMPLE_EVENT_PO2, SAMPLE_FLAGS_BEGIN}, // PO2 too high 235 {0xCC, 1, SAMPLE_EVENT_NONE, SAMPLE_FLAGS_BEGIN}, // Low Cylinder 1 pressure 236 {0xCE, 1, SAMPLE_EVENT_NONE, SAMPLE_FLAGS_BEGIN}, // Non-decompression warning 237 {0xCF, 1, SAMPLE_EVENT_OLF, SAMPLE_FLAGS_BEGIN}, // O2 Toxicity 238 {0xCD, 1, SAMPLE_EVENT_NONE, SAMPLE_FLAGS_NONE}, // Switched to deco blend 239 {0xD0, 1, SAMPLE_EVENT_WORKLOAD, SAMPLE_FLAGS_BEGIN}, // Breathing rate alarm 240 {0xD3, 1, SAMPLE_EVENT_NONE, SAMPLE_FLAGS_NONE}, // Low gas 1 flow rate 241 {0xD6, 1, SAMPLE_EVENT_CEILING, SAMPLE_FLAGS_BEGIN}, // Depth is less than ceiling 242 {0xD8, 1, SAMPLE_EVENT_NONE, SAMPLE_FLAGS_END}, // End decompression mode 243 {0xE1, 1, SAMPLE_EVENT_ASCENT, SAMPLE_FLAGS_END}, // End ascent rate warning 244 {0xE2, 1, SAMPLE_EVENT_NONE, SAMPLE_FLAGS_NONE}, // Low SBAT battery warning 245 {0xE3, 1, SAMPLE_EVENT_NONE, SAMPLE_FLAGS_NONE}, // Switched to FO2 mode 246 {0xE5, 1, SAMPLE_EVENT_NONE, SAMPLE_FLAGS_NONE}, // Switched to PO2 mode 247 {0xEE, 1, SAMPLE_EVENT_NONE, SAMPLE_FLAGS_END}, // End non-decompresison warning 248 {0xEF, 1, SAMPLE_EVENT_NONE, SAMPLE_FLAGS_NONE}, // Switch to blend 2 249 {0xF0, 1, SAMPLE_EVENT_WORKLOAD, SAMPLE_FLAGS_END}, // Breathing rate alarm 250 {0xF3, 1, SAMPLE_EVENT_NONE, SAMPLE_FLAGS_NONE}, // Switch to blend 1 251 {0xF6, 1, SAMPLE_EVENT_CEILING, SAMPLE_FLAGS_END}, // End Depth is less than ceiling 252 }; 253 254 static const event_size_t cochran_cmdr_event_bytes[] = { 255 {0x00, 17}, {0x01, 21}, {0x02, 18}, 256 {0x03, 17}, {0x06, 19}, {0x07, 19}, 257 {0x08, 19}, {0x09, 19}, {0x0a, 19}, 258 {0x0b, 21}, {0x0c, 19}, {0x0d, 19}, 259 {0x0e, 19}, {0x10, 21}, 260 }; 261 262 static const event_size_t cochran_emc_event_bytes[] = { 263 {0x00, 19}, {0x01, 23}, {0x02, 20}, 264 {0x03, 19}, {0x06, 21}, {0x07, 21}, 265 {0x0a, 21}, {0x0b, 21}, {0x0f, 19}, 266 {0x10, 21}, 267 }; 268 269 270 static unsigned int 271 cochran_commander_handle_event (cochran_commander_parser_t *parser, unsigned char code, dc_sample_callback_t callback, void *userdata) 272 { 273 dc_parser_t *abstract = (dc_parser_t *) parser; 274 275 const cochran_events_t *event = NULL; 276 for (unsigned int i = 0; i < C_ARRAY_SIZE(cochran_events); ++i) { 277 if (cochran_events[i].code == code) { 278 event = cochran_events + i; 279 break; 280 } 281 } 282 283 if (event == NULL) { 284 // Unknown event, send warning so we know we missed something 285 WARNING(abstract->context, "Unknown event 0x%02x", code); 286 return 1; 287 } 288 289 switch (code) { 290 case 0xAB: // Ceiling decrease 291 // Indicated to lower ceiling by 10 ft (deeper) 292 // Bytes 1-2: first stop duration (min) 293 // Bytes 3-4: total stop duration (min) 294 // Handled in calling function 295 break; 296 case 0xAD: // Ceiling increase 297 // Indicates to raise ceiling by 10 ft (shallower) 298 // Handled in calling function 299 break; 300 case 0xC0: // Switched to FO2 21% mode (surface) 301 // Event seen upon surfacing 302 // handled in calling function 303 break; 304 case 0xCD: // Switched to deco blend 305 case 0xEF: // Switched to gas blend 2 306 case 0xF3: // Switched to gas blend 1 307 // handled in calling function 308 break; 309 default: 310 // Don't send known events of type NONE 311 if (event->type != SAMPLE_EVENT_NONE) { 312 dc_sample_value_t sample = {0}; 313 sample.event.type = event->type; 314 sample.event.time = 0; 315 sample.event.value = 0; 316 sample.event.flags = event->flag; 317 if (callback) callback (DC_SAMPLE_EVENT, &sample, userdata); 318 } 319 } 320 321 return event->data_bytes; 322 } 323 324 325 /* 326 * Used to find the end of a dive that has an incomplete dive-end 327 * block. It parses backwards past inter-dive events. 328 */ 329 static int 330 cochran_commander_backparse(cochran_commander_parser_t *parser, const unsigned char *samples, int size) 331 { 332 int result = size, best_result = size; 333 334 for (unsigned int i = 0; i < parser->nevents; i++) { 335 int ptr = size - parser->events[i].size; 336 if (ptr > 0 && samples[ptr] == parser->events[i].code) { 337 // Recurse to find the largest match. Because we are parsing backwards 338 // and the events vary in size we can't be sure the byte that matches 339 // the event code is an event code or data from inside a longer or shorter 340 // event. 341 result = cochran_commander_backparse(parser, samples, ptr); 342 } 343 344 if (result < best_result) { 345 best_result = result; 346 } 347 } 348 349 return best_result; 350 } 351 352 353 dc_status_t 354 cochran_commander_parser_create (dc_parser_t **out, dc_context_t *context, const unsigned char data[], size_t size, unsigned int model) 355 { 356 cochran_commander_parser_t *parser = NULL; 357 dc_status_t status = DC_STATUS_SUCCESS; 358 359 if (out == NULL) 360 return DC_STATUS_INVALIDARGS; 361 362 // Allocate memory. 363 parser = (cochran_commander_parser_t *) dc_parser_allocate (context, &cochran_commander_parser_vtable, data, size); 364 if (parser == NULL) { 365 ERROR (context, "Failed to allocate memory."); 366 return DC_STATUS_NOMEMORY; 367 } 368 369 parser->model = model; 370 371 switch (model) { 372 case COCHRAN_MODEL_COMMANDER_TM: 373 parser->layout = &cochran_cmdr_tm_parser_layout; 374 parser->events = NULL; // No inter-dive events on this model 375 parser->nevents = 0; 376 break; 377 case COCHRAN_MODEL_COMMANDER_PRE21000: 378 parser->layout = &cochran_cmdr_1_parser_layout; 379 parser->events = cochran_cmdr_event_bytes; 380 parser->nevents = C_ARRAY_SIZE(cochran_cmdr_event_bytes); 381 break; 382 case COCHRAN_MODEL_COMMANDER_AIR_NITROX: 383 parser->layout = &cochran_cmdr_parser_layout; 384 parser->events = cochran_cmdr_event_bytes; 385 parser->nevents = C_ARRAY_SIZE(cochran_cmdr_event_bytes); 386 break; 387 case COCHRAN_MODEL_EMC_14: 388 case COCHRAN_MODEL_EMC_16: 389 case COCHRAN_MODEL_EMC_20: 390 parser->layout = &cochran_emc_parser_layout; 391 parser->events = cochran_emc_event_bytes; 392 parser->nevents = C_ARRAY_SIZE(cochran_emc_event_bytes); 393 break; 394 default: 395 status = DC_STATUS_UNSUPPORTED; 396 goto error_free; 397 } 398 399 *out = (dc_parser_t *) parser; 400 401 return DC_STATUS_SUCCESS; 402 403 error_free: 404 dc_parser_deallocate ((dc_parser_t *) parser); 405 return status; 406 } 407 408 409 static dc_status_t 410 cochran_commander_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime) 411 { 412 cochran_commander_parser_t *parser = (cochran_commander_parser_t *) abstract; 413 const cochran_parser_layout_t *layout = parser->layout; 414 const unsigned char *data = abstract->data; 415 416 if (abstract->size < layout->headersize) 417 return DC_STATUS_DATAFORMAT; 418 419 dc_ticks_t ts = 0; 420 421 if (datetime) { 422 switch (layout->date_encoding) 423 { 424 case DATE_ENCODING_MSDHYM: 425 datetime->second = data[layout->datetime + 1]; 426 datetime->minute = data[layout->datetime + 0]; 427 datetime->hour = data[layout->datetime + 3]; 428 datetime->day = data[layout->datetime + 2]; 429 datetime->month = data[layout->datetime + 5]; 430 datetime->year = data[layout->datetime + 4] + (data[layout->datetime + 4] > 91 ? 1900 : 2000); 431 datetime->timezone = DC_TIMEZONE_NONE; 432 break; 433 case DATE_ENCODING_SMHDMY: 434 datetime->second = data[layout->datetime + 0]; 435 datetime->minute = data[layout->datetime + 1]; 436 datetime->hour = data[layout->datetime + 2]; 437 datetime->day = data[layout->datetime + 3]; 438 datetime->month = data[layout->datetime + 4]; 439 datetime->year = data[layout->datetime + 5] + (data[layout->datetime + 5] > 91 ? 1900 : 2000); 440 datetime->timezone = DC_TIMEZONE_NONE; 441 break; 442 case DATE_ENCODING_TICKS: 443 ts = array_uint32_le(data + layout->datetime) + COCHRAN_EPOCH; 444 dc_datetime_localtime(datetime, ts); 445 break; 446 } 447 } 448 449 return DC_STATUS_SUCCESS; 450 } 451 452 453 static dc_status_t 454 cochran_commander_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value) 455 { 456 const cochran_commander_parser_t *parser = (cochran_commander_parser_t *) abstract; 457 const cochran_parser_layout_t *layout = parser->layout; 458 const unsigned char *data = abstract->data; 459 unsigned int minutes = 0, qfeet = 0; 460 461 dc_gasmix_t *gasmix = (dc_gasmix_t *) value; 462 dc_salinity_t *water = (dc_salinity_t *) value; 463 464 if (abstract->size < layout->headersize) 465 return DC_STATUS_DATAFORMAT; 466 467 if (value) { 468 switch (type) { 469 case DC_FIELD_TEMPERATURE_SURFACE: 470 *((double *) value) = (data[layout->start_temp] - 32.0) / 1.8; 471 break; 472 case DC_FIELD_TEMPERATURE_MINIMUM: 473 if (data[layout->min_temp] == 0xFF) 474 return DC_STATUS_UNSUPPORTED; 475 *((double *) value) = (data[layout->min_temp] / 2.0 + 20 - 32) / 1.8; 476 break; 477 case DC_FIELD_TEMPERATURE_MAXIMUM: 478 if (layout->max_temp == UNSUPPORTED) 479 return DC_STATUS_UNSUPPORTED; 480 if (data[layout->max_temp] == 0xFF) 481 return DC_STATUS_UNSUPPORTED; 482 *((double *) value) = (data[layout->max_temp] / 2.0 + 20 - 32) / 1.8; 483 break; 484 case DC_FIELD_DIVETIME: 485 minutes = array_uint16_le(data + layout->divetime); 486 if (minutes == 0xFFFF) 487 return DC_STATUS_UNSUPPORTED; 488 *((unsigned int *) value) = minutes * 60; 489 break; 490 case DC_FIELD_MAXDEPTH: 491 qfeet = array_uint16_le(data + layout->max_depth); 492 if (qfeet == 0xFFFF) 493 return DC_STATUS_UNSUPPORTED; 494 *((double *) value) = qfeet / 4.0 * FEET; 495 break; 496 case DC_FIELD_AVGDEPTH: 497 qfeet = array_uint16_le(data + layout->avg_depth); 498 if (qfeet == 0xFFFF) 499 return DC_STATUS_UNSUPPORTED; 500 *((double *) value) = qfeet / 4.0 * FEET; 501 break; 502 case DC_FIELD_GASMIX_COUNT: 503 *((unsigned int *) value) = 2; 504 break; 505 case DC_FIELD_GASMIX: 506 // Gas percentages are decimal and encoded as 507 // highbyte = integer portion 508 // lowbyte = decimal portion, divide by 256 to get decimal value 509 gasmix->usage = DC_USAGE_NONE; 510 gasmix->oxygen = array_uint16_le (data + layout->oxygen + 2 * flags) / 256.0 / 100; 511 if (layout->helium == UNSUPPORTED) { 512 gasmix->helium = 0; 513 } else { 514 gasmix->helium = array_uint16_le (data + layout->helium + 2 * flags) / 256.0 / 100; 515 } 516 gasmix->nitrogen = 1.0 - gasmix->oxygen - gasmix->helium; 517 break; 518 case DC_FIELD_SALINITY: 519 // 0x00 = low conductivity, 0x10 = high, maybe there's a 0x01 and 0x11? 520 // Assume Cochran's conductivity ranges from 0 to 3 521 // 0 is fresh water, anything else is sea water 522 // for density assume 523 // 0 = 1000kg/m³, 2 = 1025kg/m³ 524 // and other values are linear 525 if (layout->water_conductivity == UNSUPPORTED) 526 return DC_STATUS_UNSUPPORTED; 527 if ((data[layout->water_conductivity] & 0x3) == 0) 528 water->type = DC_WATER_FRESH; 529 else 530 water->type = DC_WATER_SALT; 531 water->density = 1000.0 + 12.5 * (data[layout->water_conductivity] & 0x3); 532 break; 533 case DC_FIELD_ATMOSPHERIC: 534 // Cochran measures air pressure and stores it as altitude. 535 // Convert altitude (measured in 1/4 kilofeet) back to pressure. 536 if (layout->altitude == UNSUPPORTED) 537 return DC_STATUS_UNSUPPORTED; 538 *(double *) value = ATM / BAR * pow(1 - 0.0000225577 * data[layout->altitude] * 250.0 * FEET, 5.25588); 539 break; 540 default: 541 return DC_STATUS_UNSUPPORTED; 542 } 543 } 544 545 return DC_STATUS_SUCCESS; 546 } 547 548 549 /* 550 * Parse early Commander computers 551 */ 552 static dc_status_t 553 cochran_commander_parser_samples_foreach_tm (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata) 554 { 555 cochran_commander_parser_t *parser = (cochran_commander_parser_t *) abstract; 556 const cochran_parser_layout_t *layout = parser->layout; 557 const unsigned char *data = abstract->data; 558 const unsigned char *samples = data + layout->headersize; 559 560 if (abstract->size < layout->headersize) 561 return DC_STATUS_DATAFORMAT; 562 563 unsigned int size = abstract->size - layout->headersize; 564 unsigned int sample_interval = data[layout->pt_sample_interval]; 565 566 dc_sample_value_t sample = {0}; 567 unsigned int time = 0, last_sample_time = 0; 568 unsigned int offset = 2; 569 unsigned int deco_ceiling = 0; 570 571 unsigned int temp = samples[0]; // Half degrees F 572 unsigned int depth = samples[1]; // Half feet 573 574 last_sample_time = sample.time = time * 1000; 575 if (callback) callback (DC_SAMPLE_TIME, &sample, userdata); 576 577 sample.depth = (depth / 2.0) * FEET; 578 if (callback) callback (DC_SAMPLE_DEPTH, &sample, userdata); 579 580 sample.temperature = (temp / 2.0 - 32.0) / 1.8; 581 if (callback) callback (DC_SAMPLE_TEMPERATURE, &sample, userdata); 582 583 sample.gasmix = 0; 584 if (callback) callback(DC_SAMPLE_GASMIX, &sample, userdata); 585 586 while (offset < size) { 587 const unsigned char *s = samples + offset; 588 589 sample.time = time * 1000; 590 if (last_sample_time != sample.time) { 591 // We haven't issued this time yet. 592 last_sample_time = sample.time; 593 if (callback) callback (DC_SAMPLE_TIME, &sample, userdata); 594 } 595 596 if (*s & 0x80) { 597 // Event or temperate change byte 598 if (*s & 0x60) { 599 // Event byte 600 switch (*s) { 601 case 0xC5: // Deco obligation begins 602 break; 603 case 0xD8: // Deco obligation ends 604 break; 605 case 0xAB: // Decrement ceiling (deeper) 606 deco_ceiling += 10; // feet 607 608 sample.deco.type = DC_DECO_DECOSTOP; 609 sample.deco.time = 60; // We don't know the duration 610 sample.deco.depth = deco_ceiling * FEET; 611 sample.deco.tts = 0; 612 if (callback) callback(DC_SAMPLE_DECO, &sample, userdata); 613 break; 614 case 0xAD: // Increment ceiling (shallower) 615 deco_ceiling -= 10; // feet 616 617 sample.deco.type = DC_DECO_DECOSTOP; 618 sample.deco.depth = deco_ceiling * FEET; 619 sample.deco.time = 60; // We don't know the duration 620 sample.deco.tts = 0; 621 if (callback) callback(DC_SAMPLE_DECO, &sample, userdata); 622 break; 623 default: 624 cochran_commander_handle_event(parser, s[0], callback, userdata); 625 break; 626 } 627 } else { 628 // Temp change 629 if (*s & 0x10) 630 temp -= (*s & 0x0f); 631 else 632 temp += (*s & 0x0f); 633 sample.temperature = (temp / 2.0 - 32.0) / 1.8; 634 if (callback) callback (DC_SAMPLE_TEMPERATURE, &sample, userdata); 635 } 636 637 offset++; 638 continue; 639 } 640 641 // Depth sample 642 if (s[0] & 0x40) 643 depth -= s[0] & 0x3f; 644 else 645 depth += s[0] & 0x3f; 646 647 sample.depth = (depth / 2.0) * FEET; 648 if (callback) callback (DC_SAMPLE_DEPTH, &sample, userdata); 649 650 offset++; 651 time += sample_interval; 652 } 653 return DC_STATUS_SUCCESS; 654 } 655 656 657 /* 658 * Parse Commander I (Pre-21000 s/n), II and EMC computers 659 */ 660 static dc_status_t 661 cochran_commander_parser_samples_foreach_emc (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata) 662 { 663 cochran_commander_parser_t *parser = (cochran_commander_parser_t *) abstract; 664 const cochran_parser_layout_t *layout = parser->layout; 665 const unsigned char *data = abstract->data; 666 const unsigned char *samples = data + layout->headersize; 667 const unsigned char *last_sample = NULL; 668 669 if (abstract->size < layout->headersize) 670 return DC_STATUS_DATAFORMAT; 671 672 unsigned int size = abstract->size - layout->headersize; 673 674 dc_sample_value_t sample = {0}; 675 unsigned int time = 0, last_sample_time = 0; 676 unsigned int offset = 0; 677 double start_depth = 0; 678 int depth = 0; 679 unsigned int deco_obligation = 0; 680 unsigned int deco_ceiling = 0; 681 unsigned int corrupt_dive = 0; 682 683 // In rare circumstances Cochran computers won't record the end-of-dive 684 // log entry block. When the end-sample pointer is 0xFFFFFFFF it's corrupt. 685 // That means we don't really know where the dive samples end and we don't 686 // know what the dive summary values are (i.e. max depth, min temp) 687 if (array_uint32_le(data + layout->pt_profile_end) == 0xFFFFFFFF) { 688 corrupt_dive = 1; 689 dc_datetime_t d; 690 cochran_commander_parser_get_datetime(abstract, &d); 691 692 WARNING(abstract->context, "Incomplete dive on %02d/%02d/%02d at %02d:%02d:%02d, trying to parse samples", 693 d.year, d.month, d.day, d.hour, d.minute, d.second); 694 695 // Eliminate inter-dive events 696 size = cochran_commander_backparse(parser, samples, size); 697 } 698 699 // Cochran samples depth every second and varies between ascent rate 700 // and temp every other second. 701 702 // Prime values from the dive log section 703 if (parser->model == COCHRAN_MODEL_COMMANDER_AIR_NITROX || 704 parser->model == COCHRAN_MODEL_COMMANDER_PRE21000) { 705 // Commander stores start depth in quarter-feet 706 start_depth = array_uint16_le (data + layout->start_depth) / 4.0; 707 } else { 708 // EMC stores start depth in 256ths of a foot. 709 start_depth = array_uint16_le (data + layout->start_depth) / 256.0; 710 } 711 712 last_sample_time = sample.time = time * 1000; 713 if (callback) callback (DC_SAMPLE_TIME, &sample, userdata); 714 715 sample.depth = start_depth * FEET; 716 if (callback) callback (DC_SAMPLE_DEPTH, &sample, userdata); 717 718 sample.temperature = (data[layout->start_temp] - 32.0) / 1.8; 719 if (callback) callback (DC_SAMPLE_TEMPERATURE, &sample, userdata); 720 721 sample.gasmix = 0; 722 if (callback) callback(DC_SAMPLE_GASMIX, &sample, userdata); 723 unsigned int last_gasmix = sample.gasmix; 724 725 while (offset < size) { 726 const unsigned char *s = samples + offset; 727 728 sample.time = time * 1000; 729 if (last_sample_time != sample.time) { 730 // We haven't issued this time yet. 731 last_sample_time = sample.time; 732 if (callback) callback (DC_SAMPLE_TIME, &sample, userdata); 733 } 734 735 // If corrupt_dive end before offset 736 if (corrupt_dive) { 737 // When we aren't sure where the sample data ends we can 738 // look for events that shouldn't be in the sample data. 739 // 0xFF is unwritten memory 740 // 0xA8 indicates start of post-dive interval 741 // 0xE3 (switch to FO2 mode) and 0xF3 (switch to blend 1) occur 742 // at dive start so when we see them after the first second we 743 // found the beginning of the next dive. 744 if (s[0] == 0xFF || s[0] == 0xA8) { 745 DEBUG(abstract->context, "Used corrupt dive breakout 1 on event %02x", s[0]); 746 break; 747 } 748 if (time > 1 && (s[0] == 0xE3 || s[0] == 0xF3)) { 749 DEBUG(abstract->context, "Used corrupt dive breakout 2 on event %02x", s[0]); 750 break; 751 } 752 } 753 754 // Check for event 755 if (s[0] & 0x80) { 756 offset += cochran_commander_handle_event(parser, s[0], callback, userdata); 757 758 // Events indicating change in deco status 759 switch (s[0]) { 760 case 0xC5: // Deco obligation begins 761 deco_obligation = 1; 762 break; 763 case 0xD8: // Deco obligation ends 764 deco_obligation = 0; 765 break; 766 case 0xAB: // Decrement ceiling (deeper) 767 deco_ceiling += 10; // feet 768 769 sample.deco.type = DC_DECO_DECOSTOP; 770 sample.deco.time = (array_uint16_le(s + 3) + 1) * 60; 771 sample.deco.depth = deco_ceiling * FEET; 772 sample.deco.tts = 0; 773 if (callback) callback(DC_SAMPLE_DECO, &sample, userdata); 774 break; 775 case 0xAD: // Increment ceiling (shallower) 776 deco_ceiling -= 10; // feet 777 778 sample.deco.type = DC_DECO_DECOSTOP; 779 sample.deco.depth = deco_ceiling * FEET; 780 sample.deco.time = (array_uint16_le(s + 3) + 1) * 60; 781 sample.deco.tts = 0; 782 if (callback) callback(DC_SAMPLE_DECO, &sample, userdata); 783 break; 784 case 0xC0: // Switched to FO2 21% mode (surface) 785 // Event seen upon surfacing 786 break; 787 case 0xCD: // Switched to deco blend 788 case 0xEF: // Switched to gas blend 2 789 if (last_gasmix != 1) { 790 sample.gasmix = 1; 791 if (callback) callback(DC_SAMPLE_GASMIX, &sample, userdata); 792 last_gasmix = sample.gasmix; 793 } 794 break; 795 case 0xF3: // Switched to gas blend 1 796 if (last_gasmix != 0) { 797 sample.gasmix = 0; 798 if (callback) callback(DC_SAMPLE_GASMIX, &sample, userdata); 799 last_gasmix = sample.gasmix; 800 } 801 break; 802 } 803 804 continue; 805 } 806 807 // Make sure we have a full sample 808 if (offset + layout->samplesize > size) 809 break; 810 811 // Depth is logged as change in feet, bit 0x40 means negative depth 812 if (s[0] & 0x40) 813 depth -= (s[0] & 0x3f); 814 else 815 depth += (s[0] & 0x3f); 816 817 sample.depth = (start_depth + depth / 4.0) * FEET; 818 if (callback) callback (DC_SAMPLE_DEPTH, &sample, userdata); 819 820 // Ascent rate is logged in the 0th sample, temp in the 1st, repeat. 821 if (time % 2 == 0) { 822 // Ascent rate 823 double DC_ATTR_UNUSED ascent_rate = 0.0; 824 if (s[1] & 0x80) 825 ascent_rate = (s[1] & 0x7f); 826 else 827 ascent_rate = -(s[1] & 0x7f); 828 ascent_rate *= FEET / 4.0; 829 } else { 830 // Temperature logged in half degrees F above 20 831 double temperature = s[1] / 2.0 + 20.0; 832 sample.temperature = (temperature - 32.0) / 1.8; 833 834 if (callback) callback (DC_SAMPLE_TEMPERATURE, &sample, userdata); 835 } 836 837 // Cochran EMC models store NDL and deco stop time 838 // in the 20th to 23rd sample 839 if (layout->format == SAMPLE_EMC) { 840 // Tissue load is recorded across 20 samples, we ignore them 841 // NDL and deco stop time is recorded across the next 4 samples 842 // The first 2 are either NDL or stop time at deepest stop (if in deco) 843 // The next 2 are total deco stop time. 844 unsigned int deco_time = 0; 845 switch (time % 24) { 846 case 21: 847 deco_time = last_sample[2] + s[2] * 256 + 1; 848 if (deco_obligation) { 849 /* Deco time for deepest stop, unused */ 850 } else { 851 /* Send deco NDL sample */ 852 sample.deco.type = DC_DECO_NDL; 853 sample.deco.time = deco_time * 60; 854 sample.deco.depth = 0; 855 sample.deco.tts = 0; 856 if (callback) callback (DC_SAMPLE_DECO, &sample, userdata); 857 } 858 break; 859 case 23: 860 /* Deco time, total obligation */ 861 deco_time = last_sample[2] + s[2] * 256 + 1; 862 if (deco_obligation) { 863 sample.deco.type = DC_DECO_DECOSTOP; 864 sample.deco.depth = deco_ceiling * FEET; 865 sample.deco.time = deco_time * 60; 866 sample.deco.tts = 0; 867 if (callback) callback (DC_SAMPLE_DECO, &sample, userdata); 868 } 869 break; 870 } 871 last_sample = s; 872 } 873 874 time++; 875 offset += layout->samplesize; 876 } 877 878 return DC_STATUS_SUCCESS; 879 } 880 881 882 static dc_status_t 883 cochran_commander_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata) 884 { 885 cochran_commander_parser_t *parser = (cochran_commander_parser_t *) abstract; 886 887 if (parser->model == COCHRAN_MODEL_COMMANDER_TM) 888 return cochran_commander_parser_samples_foreach_tm (abstract, callback, userdata); 889 else 890 return cochran_commander_parser_samples_foreach_emc (abstract, callback, userdata); 891 }