mares_nemo_parser.c (14772B)
1 /* 2 * libdivecomputer 3 * 4 * Copyright (C) 2008 Jef Driesen 5 * 6 * This library is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Lesser General Public 8 * License as published by the Free Software Foundation; either 9 * version 2.1 of the License, or (at your option) any later version. 10 * 11 * This library is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with this library; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 19 * MA 02110-1301 USA 20 */ 21 22 #include <stdlib.h> 23 #include <string.h> 24 25 #include <libdivecomputer/units.h> 26 27 #include "mares_nemo.h" 28 #include "context-private.h" 29 #include "parser-private.h" 30 #include "array.h" 31 32 #define ISINSTANCE(parser) dc_parser_isinstance((parser), &mares_nemo_parser_vtable) 33 34 #define NEMO 0 35 #define NEMOWIDE 1 36 #define NEMOAIR 4 37 #define PUCK 7 38 #define NEMOEXCEL 17 39 #define NEMOAPNEIST 18 40 #define PUCKAIR 19 41 42 #define AIR 0 43 #define NITROX 1 44 #define FREEDIVE 2 45 #define GAUGE 3 46 47 typedef struct mares_nemo_parser_t mares_nemo_parser_t; 48 49 struct mares_nemo_parser_t { 50 dc_parser_t base; 51 unsigned int model; 52 unsigned int freedive; 53 /* Internal state */ 54 unsigned int mode; 55 unsigned int length; 56 unsigned int sample_count; 57 unsigned int sample_size; 58 unsigned int header; 59 unsigned int extra; 60 }; 61 62 static dc_status_t mares_nemo_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime); 63 static dc_status_t mares_nemo_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value); 64 static dc_status_t mares_nemo_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata); 65 66 static const dc_parser_vtable_t mares_nemo_parser_vtable = { 67 sizeof(mares_nemo_parser_t), 68 DC_FAMILY_MARES_NEMO, 69 NULL, /* set_clock */ 70 NULL, /* set_atmospheric */ 71 NULL, /* set_density */ 72 mares_nemo_parser_get_datetime, /* datetime */ 73 mares_nemo_parser_get_field, /* fields */ 74 mares_nemo_parser_samples_foreach, /* samples_foreach */ 75 NULL /* destroy */ 76 }; 77 78 79 dc_status_t 80 mares_nemo_parser_create (dc_parser_t **out, dc_context_t *context, const unsigned char data[], size_t size, unsigned int model) 81 { 82 dc_status_t status = DC_STATUS_SUCCESS; 83 mares_nemo_parser_t *parser = NULL; 84 85 if (out == NULL) 86 return DC_STATUS_INVALIDARGS; 87 88 // Allocate memory. 89 parser = (mares_nemo_parser_t *) dc_parser_allocate (context, &mares_nemo_parser_vtable, data, size); 90 if (parser == NULL) { 91 ERROR (context, "Failed to allocate memory."); 92 return DC_STATUS_NOMEMORY; 93 } 94 95 // Get the freedive mode for this model. 96 unsigned int freedive = FREEDIVE; 97 if (model == NEMOWIDE || model == NEMOAIR || model == PUCK || model == PUCKAIR) 98 freedive = GAUGE; 99 100 if (size < 2 + 3) { 101 status = DC_STATUS_DATAFORMAT; 102 goto error_free; 103 } 104 105 unsigned int length = array_uint16_le (data); 106 if (length > size) { 107 status = DC_STATUS_DATAFORMAT; 108 goto error_free; 109 } 110 111 unsigned int extra = 0; 112 const unsigned char marker[3] = {0xAA, 0xBB, 0xCC}; 113 if (memcmp (data + length - 3, marker, sizeof (marker)) == 0) { 114 if (model == PUCKAIR) 115 extra = 7; 116 else 117 extra = 12; 118 } 119 120 if (length < 2 + extra + 3) { 121 status = DC_STATUS_DATAFORMAT; 122 goto error_free; 123 } 124 125 unsigned int mode = data[length - extra - 1]; 126 127 unsigned int header_size = 53; 128 unsigned int sample_size = 2; 129 if (extra) { 130 if (model == PUCKAIR) 131 sample_size = 3; 132 else 133 sample_size = 5; 134 } 135 if (mode == freedive) { 136 header_size = 28; 137 sample_size = 6; 138 } 139 140 unsigned int nsamples = array_uint16_le (data + length - extra - 3); 141 142 unsigned int nbytes = 2 + nsamples * sample_size + header_size + extra; 143 if (length != nbytes) { 144 status = DC_STATUS_DATAFORMAT; 145 goto error_free; 146 } 147 148 // Set the default values. 149 parser->model = model; 150 parser->freedive = freedive; 151 parser->mode = mode; 152 parser->length = length; 153 parser->sample_count = nsamples; 154 parser->sample_size = sample_size; 155 parser->header = header_size; 156 parser->extra = extra; 157 158 *out = (dc_parser_t*) parser; 159 160 return DC_STATUS_SUCCESS; 161 162 error_free: 163 dc_parser_deallocate ((dc_parser_t *) parser); 164 return status; 165 } 166 167 168 static dc_status_t 169 mares_nemo_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime) 170 { 171 mares_nemo_parser_t *parser = (mares_nemo_parser_t *) abstract; 172 173 if (abstract->size == 0) 174 return DC_STATUS_DATAFORMAT; 175 176 const unsigned char *p = abstract->data + parser->length - parser->extra - 8; 177 178 if (datetime) { 179 datetime->year = p[0] + 2000; 180 datetime->month = p[1]; 181 datetime->day = p[2]; 182 datetime->hour = p[3]; 183 datetime->minute = p[4]; 184 datetime->second = 0; 185 datetime->timezone = DC_TIMEZONE_NONE; 186 } 187 188 return DC_STATUS_SUCCESS; 189 } 190 191 192 static dc_status_t 193 mares_nemo_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value) 194 { 195 mares_nemo_parser_t *parser = (mares_nemo_parser_t *) abstract; 196 197 if (abstract->size == 0) 198 return DC_STATUS_DATAFORMAT; 199 200 const unsigned char *data = abstract->data; 201 const unsigned char *p = abstract->data + 2 + parser->sample_count * parser->sample_size; 202 203 if (value) { 204 if (parser->mode != parser->freedive) { 205 dc_gasmix_t *gasmix = (dc_gasmix_t *) value; 206 dc_tank_t *tank = (dc_tank_t *) value; 207 switch (type) { 208 case DC_FIELD_DIVETIME: 209 *((unsigned int *) value) = parser->sample_count * 20; 210 break; 211 case DC_FIELD_MAXDEPTH: 212 *((double *) value) = array_uint16_le (p + 53 - 10) / 10.0; 213 break; 214 case DC_FIELD_GASMIX_COUNT: 215 if (parser->mode == AIR || parser->mode == NITROX) 216 *((unsigned int *) value) = 1; 217 else 218 *((unsigned int *) value) = 0; 219 break; 220 case DC_FIELD_GASMIX: 221 switch (parser->mode) { 222 case AIR: 223 gasmix->oxygen = 0.21; 224 break; 225 case NITROX: 226 gasmix->oxygen = p[53 - 43] / 100.0; 227 break; 228 default: 229 return DC_STATUS_UNSUPPORTED; 230 } 231 gasmix->helium = 0.0; 232 gasmix->nitrogen = 1.0 - gasmix->oxygen - gasmix->helium; 233 gasmix->usage = DC_USAGE_NONE; 234 break; 235 case DC_FIELD_TANK_COUNT: 236 if (parser->extra) 237 *((unsigned int *) value) = 1; 238 else 239 *((unsigned int *) value) = 0; 240 break; 241 case DC_FIELD_TANK: 242 if (parser->extra == 12) { 243 unsigned int volume = array_uint16_le(p + parser->header + 0); 244 unsigned int workpressure = array_uint16_le(p + parser->header + 2); 245 if (workpressure == 0xFFFF) { 246 tank->type = DC_TANKVOLUME_METRIC; 247 tank->volume = volume / 10.0; 248 tank->workpressure = 0.0; 249 } else { 250 if (workpressure == 0) 251 return DC_STATUS_DATAFORMAT; 252 tank->type = DC_TANKVOLUME_IMPERIAL; 253 tank->volume = volume * CUFT * 1000.0; 254 tank->volume /= workpressure * PSI / ATM; 255 tank->workpressure = workpressure * PSI / BAR; 256 } 257 tank->beginpressure = array_uint16_le(p + parser->header + 4) / 100.0; 258 tank->endpressure = array_uint16_le(p + parser->header + 6) / 100.0; 259 } else if (parser->extra == 7) { 260 tank->type = DC_TANKVOLUME_NONE; 261 tank->volume = 0.0; 262 tank->workpressure = 0.0; 263 tank->beginpressure = array_uint16_le(p + parser->header + 0); 264 tank->endpressure = array_uint16_le(p + parser->header + 2); 265 } else { 266 return DC_STATUS_UNSUPPORTED; 267 } 268 if (parser->mode == AIR || parser->mode == NITROX) { 269 tank->gasmix = 0; 270 } else { 271 tank->gasmix = DC_GASMIX_UNKNOWN; 272 } 273 tank->usage = DC_TANK_USAGE_NONE; 274 break; 275 case DC_FIELD_TEMPERATURE_MINIMUM: 276 *((double *) value) = (signed char) p[53 - 11]; 277 break; 278 case DC_FIELD_DIVEMODE: 279 switch (parser->mode) { 280 case AIR: 281 case NITROX: 282 *((dc_divemode_t *) value) = DC_DIVEMODE_OC; 283 break; 284 case FREEDIVE: 285 case GAUGE: 286 *((dc_divemode_t *) value) = DC_DIVEMODE_GAUGE; 287 break; 288 default: 289 return DC_STATUS_DATAFORMAT; 290 } 291 break; 292 default: 293 return DC_STATUS_UNSUPPORTED; 294 } 295 } else { 296 unsigned int divetime = 0; 297 switch (type) { 298 case DC_FIELD_DIVETIME: 299 for (unsigned int i = 0; i < parser->sample_count; ++i) { 300 unsigned int idx = 2 + parser->sample_size * i; 301 divetime += data[idx + 2] + data[idx + 3] * 60; 302 } 303 *((unsigned int *) value) = divetime; 304 break; 305 case DC_FIELD_MAXDEPTH: 306 *((double *) value) = array_uint16_le (p + 28 - 10) / 10.0; 307 break; 308 case DC_FIELD_GASMIX_COUNT: 309 *((unsigned int *) value) = 0; 310 break; 311 case DC_FIELD_TEMPERATURE_MINIMUM: 312 *((double *) value) = (signed char) p[28 - 11]; 313 break; 314 case DC_FIELD_DIVEMODE: 315 *((dc_divemode_t *) value) = DC_DIVEMODE_FREEDIVE; 316 break; 317 default: 318 return DC_STATUS_UNSUPPORTED; 319 } 320 } 321 } 322 323 return DC_STATUS_SUCCESS; 324 } 325 326 327 static dc_status_t 328 mares_nemo_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata) 329 { 330 mares_nemo_parser_t *parser = (mares_nemo_parser_t *) abstract; 331 332 if (abstract->size == 0) 333 return DC_STATUS_DATAFORMAT; 334 335 const unsigned char *data = abstract->data; 336 unsigned int size = abstract->size; 337 338 if (parser->mode != parser->freedive) { 339 // Initial tank pressure. 340 unsigned int pressure = 0; 341 if (parser->extra == 12) { 342 const unsigned char *p = data + 2 + parser->sample_count * parser->sample_size; 343 pressure = array_uint16_le(p + parser->header + 4); 344 } 345 346 // Initial gas mix. 347 unsigned int gasmix_previous = 0xFFFFFFFF; 348 unsigned int gasmix = gasmix_previous; 349 if (parser->mode == AIR || parser->mode == NITROX) { 350 gasmix = 0; 351 } 352 353 unsigned int time = 0; 354 for (unsigned int i = 0; i < parser->sample_count; ++i) { 355 dc_sample_value_t sample = {0}; 356 357 unsigned int idx = 2 + parser->sample_size * i; 358 unsigned int value = array_uint16_le (data + idx); 359 unsigned int depth = value & 0x07FF; 360 unsigned int ascent = (value & 0xC000) >> 14; 361 unsigned int violation = (value & 0x2000) >> 13; 362 unsigned int deco = (value & 0x1000) >> 12; 363 364 // Time (seconds). 365 time += 20; 366 sample.time = time * 1000; 367 if (callback) callback (DC_SAMPLE_TIME, &sample, userdata); 368 369 // Depth (1/10 m). 370 sample.depth = depth / 10.0; 371 if (callback) callback (DC_SAMPLE_DEPTH, &sample, userdata); 372 373 // Gas change. 374 if (gasmix != gasmix_previous) { 375 sample.gasmix = gasmix; 376 if (callback) callback (DC_SAMPLE_GASMIX, &sample, userdata); 377 gasmix_previous = gasmix; 378 } 379 380 // Ascent rate 381 if (ascent) { 382 sample.event.type = SAMPLE_EVENT_ASCENT; 383 sample.event.time = 0; 384 sample.event.flags = 0; 385 sample.event.value = ascent; 386 if (callback) callback (DC_SAMPLE_EVENT, &sample, userdata); 387 } 388 389 // Deco violation 390 if (violation) { 391 sample.event.type = SAMPLE_EVENT_CEILING; 392 sample.event.time = 0; 393 sample.event.flags = 0; 394 sample.event.value = 0; 395 if (callback) callback (DC_SAMPLE_EVENT, &sample, userdata); 396 } 397 398 // Deco stop 399 if (deco) { 400 sample.deco.type = DC_DECO_DECOSTOP; 401 } else { 402 sample.deco.type = DC_DECO_NDL; 403 } 404 sample.deco.time = 0; 405 sample.deco.depth = 0.0; 406 sample.deco.tts = 0; 407 if (callback) callback (DC_SAMPLE_DECO, &sample, userdata); 408 409 // Pressure (1 bar). 410 if (parser->sample_size == 3) { 411 sample.pressure.tank = 0; 412 sample.pressure.value = data[idx + 2]; 413 if (callback) callback (DC_SAMPLE_PRESSURE, &sample, userdata); 414 } else if (parser->sample_size == 5) { 415 unsigned int type = (time / 20) % 3; 416 if (type == 0) { 417 pressure -= data[idx + 2] * 100; 418 sample.pressure.tank = 0; 419 sample.pressure.value = pressure / 100.0; 420 if (callback) callback (DC_SAMPLE_PRESSURE, &sample, userdata); 421 } 422 } 423 } 424 } else { 425 // A freedive session contains only summaries for each individual 426 // freedive. The detailed profile data (if present) is stored after 427 // the normal dive data. We assume a freedive has a detailed profile 428 // when the buffer contains more data than the size indicated in the 429 // header. 430 int profiles = (size > parser->length); 431 432 unsigned int time = 0; 433 unsigned int offset = parser->length; 434 for (unsigned int i = 0; i < parser->sample_count; ++i) { 435 dc_sample_value_t sample = {0}; 436 437 unsigned int idx = 2 + parser->sample_size * i; 438 unsigned int maxdepth = array_uint16_le (data + idx); 439 unsigned int divetime = data[idx + 2] + data[idx + 3] * 60; 440 unsigned int surftime = data[idx + 4] + data[idx + 5] * 60; 441 442 // Surface Time (seconds). 443 time += surftime; 444 sample.time = time * 1000; 445 if (callback) callback (DC_SAMPLE_TIME, &sample, userdata); 446 447 // Surface Depth (0 m). 448 sample.depth = 0.0; 449 if (callback) callback (DC_SAMPLE_DEPTH, &sample, userdata); 450 451 if (profiles) { 452 // Get the freedive sample interval for this model. 453 unsigned int interval = 4; 454 if (parser->model == NEMOAPNEIST) 455 interval = 1; 456 457 // Calculate the number of samples that should be present 458 // in the profile data, based on the divetime in the summary. 459 unsigned int n = (divetime + interval - 1) / interval; 460 461 // The last sample interval can be smaller than the normal 462 // 4 seconds. We keep track of the maximum divetime, to be 463 // able to adjust that last sample interval. 464 unsigned int maxtime = time + divetime; 465 466 // Process all depth samples. Once a zero depth sample is 467 // reached, the current freedive profile is complete. 468 unsigned int count = 0; 469 while (offset + 2 <= size) { 470 unsigned int depth = array_uint16_le (data + offset); 471 offset += 2; 472 473 if (depth == 0) 474 break; 475 476 count++; 477 478 if (count > n) 479 break; 480 481 // Time (seconds). 482 time += interval; 483 if (time > maxtime) 484 time = maxtime; // Adjust the last sample. 485 sample.time = time * 1000; 486 if (callback) callback (DC_SAMPLE_TIME, &sample, userdata); 487 488 // Depth (1/10 m). 489 sample.depth = depth / 10.0; 490 if (callback) callback (DC_SAMPLE_DEPTH, &sample, userdata); 491 } 492 493 // Verify that the number of samples in the profile data 494 // equals the predicted number of samples (from the divetime 495 // in the summary entry). If both values are different, the 496 // the profile data is probably incorrect. 497 if (count != n) { 498 ERROR (abstract->context, "Unexpected number of samples."); 499 return DC_STATUS_DATAFORMAT; 500 } 501 } else { 502 // Dive Time (seconds). 503 time += divetime; 504 sample.time = time * 1000; 505 if (callback) callback (DC_SAMPLE_TIME, &sample, userdata); 506 507 // Maximum Depth (1/10 m). 508 sample.depth = maxdepth / 10.0; 509 if (callback) callback (DC_SAMPLE_DEPTH, &sample, userdata); 510 } 511 } 512 } 513 514 return DC_STATUS_SUCCESS; 515 }