suunto_vyper_parser.c (11415B)
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 <stdio.h> /* for snprintf */ 24 #include <string.h> /* for strdup */ 25 26 #include <libdivecomputer/units.h> 27 28 #include "suunto_vyper.h" 29 #include "context-private.h" 30 #include "parser-private.h" 31 32 #define ISINSTANCE(parser) dc_parser_isinstance((parser), &suunto_vyper_parser_vtable) 33 34 #define NGASMIXES 3 35 36 typedef struct suunto_vyper_parser_t suunto_vyper_parser_t; 37 38 struct suunto_vyper_parser_t { 39 dc_parser_t base; 40 // Cached fields. 41 unsigned int cached; 42 unsigned int divetime; 43 unsigned int maxdepth; 44 unsigned int marker; 45 unsigned int ngasmixes; 46 unsigned int serial; 47 unsigned int oxygen[NGASMIXES]; 48 }; 49 50 static dc_status_t suunto_vyper_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime); 51 static dc_status_t suunto_vyper_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value); 52 static dc_status_t suunto_vyper_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata); 53 54 static const dc_parser_vtable_t suunto_vyper_parser_vtable = { 55 sizeof(suunto_vyper_parser_t), 56 DC_FAMILY_SUUNTO_VYPER, 57 NULL, /* set_clock */ 58 NULL, /* set_atmospheric */ 59 NULL, /* set_density */ 60 suunto_vyper_parser_get_datetime, /* datetime */ 61 suunto_vyper_parser_get_field, /* fields */ 62 suunto_vyper_parser_samples_foreach, /* samples_foreach */ 63 NULL /* destroy */ 64 }; 65 66 static unsigned int 67 suunto_vyper_parser_find_gasmix (suunto_vyper_parser_t *parser, unsigned int o2) 68 { 69 unsigned int i = 0; 70 while (i < parser->ngasmixes) { 71 if (o2 == parser->oxygen[i]) 72 break; 73 i++; 74 } 75 76 return i; 77 } 78 79 static dc_status_t 80 suunto_vyper_parser_cache (suunto_vyper_parser_t *parser) 81 { 82 dc_parser_t *abstract = (dc_parser_t *) parser; 83 const unsigned char *data = parser->base.data; 84 unsigned int size = parser->base.size; 85 86 if (parser->cached) { 87 return DC_STATUS_SUCCESS; 88 } 89 90 if (size < 18) { 91 return DC_STATUS_DATAFORMAT; 92 } 93 94 unsigned int ngasmixes = 1; 95 unsigned int oxygen[NGASMIXES] = {0}; 96 if (data[6]) 97 oxygen[0] = data[6]; 98 else 99 oxygen[0] = 21; 100 101 // Parse the samples. 102 unsigned int interval = data[3]; 103 unsigned int nsamples = 0; 104 unsigned int depth = 0, maxdepth = 0; 105 unsigned int offset = 14; 106 while (offset < size && data[offset] != 0x80) { 107 unsigned char value = data[offset++]; 108 if (value < 0x79 || value > 0x87) { 109 // Delta depth. 110 depth += (signed char) value; 111 if (depth > maxdepth) 112 maxdepth = depth; 113 nsamples++; 114 } else if (value == 0x87) { 115 // Gas change event. 116 if (offset + 1 > size) { 117 ERROR (abstract->context, "Buffer overflow detected!"); 118 return DC_STATUS_DATAFORMAT; 119 } 120 121 // Get the new gas mix. 122 unsigned int o2 = data[offset++]; 123 124 // Find the gasmix in the list. 125 unsigned int i = 0; 126 while (i < ngasmixes) { 127 if (o2 == oxygen[i]) 128 break; 129 i++; 130 } 131 132 // Add it to list if not found. 133 if (i >= ngasmixes) { 134 if (i >= NGASMIXES) { 135 ERROR (abstract->context, "Maximum number of gas mixes reached."); 136 return DC_STATUS_DATAFORMAT; 137 } 138 oxygen[i] = o2; 139 ngasmixes = i + 1; 140 } 141 } 142 } 143 144 // Check the end marker. 145 unsigned int marker = offset; 146 if (marker + 4 >= size || data[marker] != 0x80) { 147 ERROR (abstract->context, "No valid end marker found!"); 148 return DC_STATUS_DATAFORMAT; 149 } 150 151 // Cache the data for later use. 152 parser->divetime = nsamples * interval; 153 parser->maxdepth = maxdepth; 154 parser->marker = marker; 155 parser->ngasmixes = ngasmixes; 156 for (unsigned int i = 0; i < ngasmixes; ++i) { 157 parser->oxygen[i] = oxygen[i]; 158 } 159 parser->cached = 1; 160 161 return DC_STATUS_SUCCESS; 162 } 163 164 165 dc_status_t 166 suunto_vyper_parser_create (dc_parser_t **out, dc_context_t *context, const unsigned char data[], size_t size, unsigned int serial) 167 { 168 suunto_vyper_parser_t *parser = NULL; 169 170 if (out == NULL) 171 return DC_STATUS_INVALIDARGS; 172 173 // Allocate memory. 174 parser = (suunto_vyper_parser_t *) dc_parser_allocate (context, &suunto_vyper_parser_vtable, data, size); 175 if (parser == NULL) { 176 ERROR (context, "Failed to allocate memory."); 177 return DC_STATUS_NOMEMORY; 178 } 179 180 // Set the default values. 181 parser->cached = 0; 182 parser->divetime = 0; 183 parser->maxdepth = 0; 184 parser->marker = 0; 185 parser->ngasmixes = 0; 186 parser->serial = serial; 187 for (unsigned int i = 0; i < NGASMIXES; ++i) { 188 parser->oxygen[i] = 0; 189 } 190 191 *out = (dc_parser_t*) parser; 192 193 return DC_STATUS_SUCCESS; 194 } 195 196 197 static dc_status_t 198 suunto_vyper_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime) 199 { 200 if (abstract->size < 9 + 5) 201 return DC_STATUS_DATAFORMAT; 202 203 const unsigned char *p = abstract->data + 9; 204 205 if (datetime) { 206 datetime->year = p[0] + (p[0] < 90 ? 2000 : 1900); 207 datetime->month = p[1]; 208 datetime->day = p[2]; 209 datetime->hour = p[3]; 210 datetime->minute = p[4]; 211 datetime->second = 0; 212 datetime->timezone = DC_TIMEZONE_NONE; 213 } 214 215 return DC_STATUS_SUCCESS; 216 } 217 218 #define BUFLEN 16 219 220 static dc_status_t 221 suunto_vyper_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value) 222 { 223 suunto_vyper_parser_t *parser = (suunto_vyper_parser_t *) abstract; 224 const unsigned char *data = abstract->data; 225 226 dc_gasmix_t *gas = (dc_gasmix_t *) value; 227 dc_tank_t *tank = (dc_tank_t *) value; 228 dc_decomodel_t *decomodel = (dc_decomodel_t *) value; 229 dc_field_string_t *string = (dc_field_string_t *) value; 230 char buf[BUFLEN]; 231 232 // Cache the data. 233 dc_status_t rc = suunto_vyper_parser_cache (parser); 234 if (rc != DC_STATUS_SUCCESS) 235 return rc; 236 237 unsigned int gauge = data[4] & 0x40; 238 unsigned int beginpressure = data[5] * 2; 239 unsigned int endpressure = data[parser->marker + 3] * 2; 240 241 if (value) { 242 switch (type) { 243 case DC_FIELD_DIVETIME: 244 *((unsigned int *) value) = parser->divetime; 245 break; 246 case DC_FIELD_MAXDEPTH: 247 *((double *) value) = parser->maxdepth * FEET; 248 break; 249 case DC_FIELD_GASMIX_COUNT: 250 if (gauge) 251 *((unsigned int *) value) = 0; 252 else 253 *((unsigned int *) value) = parser->ngasmixes; 254 break; 255 case DC_FIELD_GASMIX: 256 gas->usage = DC_USAGE_NONE; 257 gas->helium = 0.0; 258 gas->oxygen = parser->oxygen[flags] / 100.0; 259 gas->nitrogen = 1.0 - gas->oxygen - gas->helium; 260 break; 261 case DC_FIELD_TANK_COUNT: 262 if (beginpressure == 0 && endpressure == 0) 263 *((unsigned int *) value) = 0; 264 else 265 *((unsigned int *) value) = 1; 266 break; 267 case DC_FIELD_TANK: 268 tank->type = DC_TANKVOLUME_NONE; 269 tank->volume = 0.0; 270 tank->workpressure = 0.0; 271 if (gauge) 272 tank->gasmix = DC_GASMIX_UNKNOWN; 273 else 274 tank->gasmix = 0; 275 tank->beginpressure = beginpressure; 276 tank->endpressure = endpressure; 277 tank->usage = DC_TANK_USAGE_NONE; 278 break; 279 case DC_FIELD_TEMPERATURE_SURFACE: 280 *((double *) value) = (signed char) data[8]; 281 break; 282 case DC_FIELD_TEMPERATURE_MINIMUM: 283 *((double *) value) = (signed char) data[parser->marker + 1]; 284 break; 285 case DC_FIELD_DIVEMODE: 286 if (gauge) { 287 *((dc_divemode_t *) value) = DC_DIVEMODE_GAUGE; 288 } else { 289 *((dc_divemode_t *) value) = DC_DIVEMODE_OC; 290 } 291 break; 292 case DC_FIELD_DECOMODEL: 293 decomodel->type = DC_DECOMODEL_RGBM; 294 decomodel->conservatism = (data[4] & 0x0F) / 3; 295 break; 296 case DC_FIELD_STRING: 297 switch(flags) { 298 case 0: /* serial */ 299 string->desc = STRING_KEY_SERIAL_NUMBER; 300 snprintf(buf, BUFLEN, "%u", parser->serial); 301 break; 302 default: 303 return DC_STATUS_UNSUPPORTED; 304 } 305 string->value = strdup(buf); 306 break; 307 default: 308 return DC_STATUS_UNSUPPORTED; 309 } 310 } 311 312 return DC_STATUS_SUCCESS; 313 } 314 315 static dc_status_t 316 suunto_vyper_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata) 317 { 318 suunto_vyper_parser_t *parser = (suunto_vyper_parser_t *) abstract; 319 const unsigned char *data = abstract->data; 320 unsigned int size = abstract->size; 321 dc_sample_value_t sample = {0}; 322 323 // Cache the data. 324 dc_status_t rc = suunto_vyper_parser_cache (parser); 325 if (rc != DC_STATUS_SUCCESS) 326 return rc; 327 328 unsigned int gauge = data[4] & 0x40; 329 330 // Time 331 sample.time = 0; 332 if (callback) callback (DC_SAMPLE_TIME, &sample, userdata); 333 334 // Depth (0 ft) 335 sample.depth = 0; 336 if (callback) callback (DC_SAMPLE_DEPTH, &sample, userdata); 337 338 // Initial gas mix 339 if (!gauge) { 340 sample.gasmix = 0; 341 if (callback) callback (DC_SAMPLE_GASMIX, &sample, userdata); 342 } 343 344 unsigned int depth = 0; 345 unsigned int time = 0; 346 unsigned int interval = data[3]; 347 unsigned int complete = 1; 348 unsigned int offset = 14; 349 while (offset < size && data[offset] != 0x80) { 350 unsigned char value = data[offset++]; 351 352 if (complete) { 353 // Time (seconds). 354 time += interval; 355 sample.time = time * 1000; 356 if (callback) callback (DC_SAMPLE_TIME, &sample, userdata); 357 complete = 0; 358 } 359 360 if (value < 0x79 || value > 0x87) { 361 // Delta depth. 362 depth += (signed char) value; 363 364 // Depth (ft). 365 sample.depth = depth * FEET; 366 if (callback) callback (DC_SAMPLE_DEPTH, &sample, userdata); 367 368 complete = 1; 369 } else { 370 // Event. 371 unsigned int o2 = 0, idx = 0; 372 sample.event.type = SAMPLE_EVENT_NONE; 373 sample.event.time = 0; 374 sample.event.flags = 0; 375 sample.event.value = 0; 376 switch (value) { 377 case 0x7a: // Slow 378 sample.event.type = SAMPLE_EVENT_ASCENT; 379 break; 380 case 0x7b: // Violation 381 sample.event.type = SAMPLE_EVENT_VIOLATION; 382 break; 383 case 0x7c: // Bookmark 384 sample.event.type = SAMPLE_EVENT_BOOKMARK; 385 break; 386 case 0x7d: // Surface 387 sample.event.type = SAMPLE_EVENT_SURFACE; 388 break; 389 case 0x7e: // Deco 390 sample.event.type = SAMPLE_EVENT_DECOSTOP; 391 break; 392 case 0x7f: // Ceiling (Deco Violation) 393 sample.event.type = SAMPLE_EVENT_CEILING; 394 break; 395 case 0x81: // Safety Stop 396 sample.event.type = SAMPLE_EVENT_SAFETYSTOP; 397 break; 398 case 0x87: // Gas Change 399 if (offset + 1 > size) 400 return DC_STATUS_DATAFORMAT; 401 402 o2 = data[offset++]; 403 idx = suunto_vyper_parser_find_gasmix (parser, o2); 404 if (idx >= parser->ngasmixes) { 405 ERROR (abstract->context, "Maximum number of gas mixes reached."); 406 return DC_STATUS_DATAFORMAT; 407 } 408 409 sample.gasmix = idx; 410 if (callback) callback (DC_SAMPLE_GASMIX, &sample, userdata); 411 sample.event.type = SAMPLE_EVENT_NONE; 412 break; 413 default: // Unknown 414 WARNING (abstract->context, "Unknown event"); 415 break; 416 } 417 418 if (sample.event.type != SAMPLE_EVENT_NONE) { 419 if (callback) callback (DC_SAMPLE_EVENT, &sample, userdata); 420 } 421 } 422 } 423 424 // Time 425 if (complete) { 426 time += interval; 427 sample.time = time * 1000; 428 if (callback) callback (DC_SAMPLE_TIME, &sample, userdata); 429 } 430 431 // Depth (0 ft) 432 sample.depth = 0; 433 if (callback) callback (DC_SAMPLE_DEPTH, &sample, userdata); 434 435 return DC_STATUS_SUCCESS; 436 }