oceanic_vtpro_parser.c (10791B)
1 /* 2 * libdivecomputer 3 * 4 * Copyright (C) 2009 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 24 #include <libdivecomputer/units.h> 25 26 #include "oceanic_vtpro.h" 27 #include "oceanic_common.h" 28 #include "context-private.h" 29 #include "parser-private.h" 30 #include "array.h" 31 32 #define ISINSTANCE(parser) dc_parser_isinstance((parser), &oceanic_vtpro_parser_vtable) 33 34 typedef struct oceanic_vtpro_parser_t oceanic_vtpro_parser_t; 35 36 struct oceanic_vtpro_parser_t { 37 dc_parser_t base; 38 unsigned int model; 39 // Cached fields. 40 unsigned int cached; 41 unsigned int divetime; 42 double maxdepth; 43 }; 44 45 static dc_status_t oceanic_vtpro_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime); 46 static dc_status_t oceanic_vtpro_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value); 47 static dc_status_t oceanic_vtpro_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata); 48 49 static const dc_parser_vtable_t oceanic_vtpro_parser_vtable = { 50 sizeof(oceanic_vtpro_parser_t), 51 DC_FAMILY_OCEANIC_VTPRO, 52 NULL, /* set_clock */ 53 NULL, /* set_atmospheric */ 54 NULL, /* set_density */ 55 oceanic_vtpro_parser_get_datetime, /* datetime */ 56 oceanic_vtpro_parser_get_field, /* fields */ 57 oceanic_vtpro_parser_samples_foreach, /* samples_foreach */ 58 NULL /* destroy */ 59 }; 60 61 62 dc_status_t 63 oceanic_vtpro_parser_create (dc_parser_t **out, dc_context_t *context, const unsigned char data[], size_t size, unsigned int model) 64 { 65 oceanic_vtpro_parser_t *parser = NULL; 66 67 if (out == NULL) 68 return DC_STATUS_INVALIDARGS; 69 70 // Allocate memory. 71 parser = (oceanic_vtpro_parser_t *) dc_parser_allocate (context, &oceanic_vtpro_parser_vtable, data, size); 72 if (parser == NULL) { 73 ERROR (context, "Failed to allocate memory."); 74 return DC_STATUS_NOMEMORY; 75 } 76 77 // Set the default values. 78 parser->model = model; 79 parser->cached = 0; 80 parser->divetime = 0; 81 parser->maxdepth = 0.0; 82 83 *out = (dc_parser_t*) parser; 84 85 return DC_STATUS_SUCCESS; 86 } 87 88 89 static dc_status_t 90 oceanic_vtpro_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime) 91 { 92 oceanic_vtpro_parser_t *parser = (oceanic_vtpro_parser_t *) abstract; 93 94 if (abstract->size < 8) 95 return DC_STATUS_DATAFORMAT; 96 97 const unsigned char *p = abstract->data; 98 99 if (datetime) { 100 // AM/PM bit of the 12-hour clock. 101 unsigned int pm = 0; 102 103 if (parser->model == AERIS500AI) { 104 datetime->year = (p[2] & 0x0F) + 1999; 105 datetime->month = (p[3] & 0xF0) >> 4; 106 datetime->day = ((p[2] & 0xF0) >> 4) | ((p[3] & 0x02) << 3); 107 datetime->hour = bcd2dec (p[1] & 0x0F) + 10 * (p[3] & 0x01); 108 pm = p[3] & 0x08; 109 } else { 110 // The logbook entry can only store the last digit of the year field, 111 // but the full year is also available in the dive header. 112 if (abstract->size < 40) 113 datetime->year = bcd2dec (p[4] & 0x0F) + 2000; 114 else 115 datetime->year = bcd2dec (((p[32 + 3] & 0xC0) >> 2) + ((p[32 + 2] & 0xF0) >> 4)) + 2000; 116 datetime->month = (p[4] & 0xF0) >> 4; 117 datetime->day = bcd2dec (p[3] & 0x7F); 118 datetime->hour = bcd2dec (p[1] & 0x7F); 119 pm = p[1] & 0x80; 120 } 121 datetime->minute = bcd2dec (p[0]); 122 datetime->second = 0; 123 datetime->timezone = DC_TIMEZONE_NONE; 124 125 // Convert to a 24-hour clock. 126 datetime->hour %= 12; 127 if (pm) 128 datetime->hour += 12; 129 } 130 131 return DC_STATUS_SUCCESS; 132 } 133 134 135 static dc_status_t 136 oceanic_vtpro_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value) 137 { 138 oceanic_vtpro_parser_t *parser = (oceanic_vtpro_parser_t *) abstract; 139 140 const unsigned char *data = abstract->data; 141 unsigned int size = abstract->size; 142 143 if (size < 7 * PAGESIZE / 2) 144 return DC_STATUS_DATAFORMAT; 145 146 if (!parser->cached) { 147 sample_statistics_t statistics = SAMPLE_STATISTICS_INITIALIZER; 148 dc_status_t rc = oceanic_vtpro_parser_samples_foreach ( 149 abstract, sample_statistics_cb, &statistics); 150 if (rc != DC_STATUS_SUCCESS) 151 return rc; 152 153 parser->cached = 1; 154 parser->divetime = statistics.divetime; 155 parser->maxdepth = statistics.maxdepth; 156 } 157 158 unsigned int footer = size - PAGESIZE; 159 160 unsigned int oxygen = 0; 161 unsigned int maxdepth = 0; 162 unsigned int beginpressure = array_uint16_le(data + 0x26) & 0x0FFF; 163 unsigned int endpressure = array_uint16_le(data + footer + 0x05) & 0x0FFF; 164 if (parser->model == AERIS500AI) { 165 oxygen = (array_uint16_le(data + footer + 2) & 0x0FF0) >> 4; 166 maxdepth = data[footer + 1]; 167 } else { 168 oxygen = data[footer + 3]; 169 maxdepth = array_uint16_le(data + footer + 0) & 0x01FF; 170 } 171 172 dc_gasmix_t *gasmix = (dc_gasmix_t *) value; 173 dc_tank_t *tank = (dc_tank_t *) value; 174 175 if (value) { 176 switch (type) { 177 case DC_FIELD_DIVETIME: 178 *((unsigned int *) value) = parser->divetime; 179 break; 180 case DC_FIELD_MAXDEPTH: 181 *((double *) value) = maxdepth * FEET; 182 break; 183 case DC_FIELD_GASMIX_COUNT: 184 *((unsigned int *) value) = 1; 185 break; 186 case DC_FIELD_GASMIX: 187 gasmix->usage = DC_USAGE_NONE; 188 gasmix->helium = 0.0; 189 if (oxygen) 190 gasmix->oxygen = oxygen / 100.0; 191 else 192 gasmix->oxygen = 0.21; 193 gasmix->nitrogen = 1.0 - gasmix->oxygen - gasmix->helium; 194 break; 195 case DC_FIELD_TANK_COUNT: 196 if (beginpressure == 0 && endpressure == 0) 197 *((unsigned int *) value) = 0; 198 else 199 *((unsigned int *) value) = 1; 200 break; 201 case DC_FIELD_TANK: 202 tank->type = DC_TANKVOLUME_NONE; 203 tank->volume = 0.0; 204 tank->workpressure = 0.0; 205 tank->gasmix = flags; 206 tank->beginpressure = beginpressure * 2 * PSI / BAR; 207 tank->endpressure = endpressure * 2 * PSI / BAR; 208 tank->usage = DC_TANK_USAGE_NONE; 209 break; 210 default: 211 return DC_STATUS_UNSUPPORTED; 212 } 213 } 214 215 return DC_STATUS_SUCCESS; 216 } 217 218 219 static dc_status_t 220 oceanic_vtpro_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata) 221 { 222 oceanic_vtpro_parser_t *parser = (oceanic_vtpro_parser_t *) abstract; 223 224 const unsigned char *data = abstract->data; 225 unsigned int size = abstract->size; 226 227 if (size < 7 * PAGESIZE / 2) 228 return DC_STATUS_DATAFORMAT; 229 230 unsigned int time = 0; 231 unsigned int interval = 0; 232 if (parser->model == AERIS500AI) { 233 const unsigned int intervals[] = {2, 5, 10, 15, 20, 25, 30}; 234 unsigned int samplerate = (data[0x27] >> 4); 235 if (samplerate >= 3 && samplerate <= 9) { 236 interval = intervals[samplerate - 3]; 237 } 238 } else { 239 const unsigned int intervals[] = {2, 15, 30, 60}; 240 unsigned int samplerate = (data[0x27] >> 4) & 0x07; 241 if (samplerate <= 3) { 242 interval = intervals[samplerate]; 243 } 244 } 245 246 // Initialize the state for the timestamp processing. 247 unsigned int timestamp = 0, count = 0, i = 0; 248 249 unsigned int offset = 5 * PAGESIZE / 2; 250 while (offset + PAGESIZE / 2 <= size - PAGESIZE) { 251 dc_sample_value_t sample = {0}; 252 253 // Ignore empty samples. 254 if (array_isequal (data + offset, PAGESIZE / 2, 0x00) || 255 array_isequal (data + offset, PAGESIZE / 2, 0xFF)) { 256 offset += PAGESIZE / 2; 257 continue; 258 } 259 260 // Get the current timestamp. 261 unsigned int current = bcd2dec (data[offset + 1] & 0x0F) * 60 + bcd2dec (data[offset + 0]); 262 if (current < timestamp) { 263 ERROR (abstract->context, "Timestamp moved backwards."); 264 return DC_STATUS_DATAFORMAT; 265 } 266 267 if (current != timestamp || count == 0) { 268 // A sample with a new timestamp. 269 i = 0; 270 if (interval) { 271 // With a time based sample interval, the maximum number 272 // of samples for a single timestamp is always fixed. 273 count = 60 / interval; 274 } else { 275 // With a depth based sample interval, the exact number 276 // of samples for a single timestamp needs to be counted. 277 count = 1; 278 unsigned int idx = offset + PAGESIZE / 2 ; 279 while (idx + PAGESIZE / 2 <= size - PAGESIZE) { 280 // Ignore empty samples. 281 if (array_isequal (data + idx, PAGESIZE / 2, 0x00) || 282 array_isequal (data + idx, PAGESIZE / 2, 0xFF)) { 283 idx += PAGESIZE / 2; 284 continue; 285 } 286 287 unsigned int next = bcd2dec (data[idx + 1] & 0x0F) * 60 + bcd2dec (data[idx + 0]); 288 if (next != current) 289 break; 290 291 idx += PAGESIZE / 2; 292 count++; 293 } 294 } 295 } else { 296 // A sample with the same timestamp. 297 i++; 298 } 299 300 if (interval) { 301 if (current > timestamp + 1) { 302 ERROR (abstract->context, "Unexpected timestamp jump."); 303 return DC_STATUS_DATAFORMAT; 304 } 305 if (i >= count) { 306 WARNING (abstract->context, "Unexpected sample with the same timestamp ignored."); 307 offset += PAGESIZE / 2; 308 continue; 309 } 310 } 311 312 // Store the current timestamp. 313 timestamp = current; 314 315 // Time. 316 if (interval) 317 time = timestamp * 60 + (i + 1) * interval; 318 else 319 time = timestamp * 60 + (i + 1) * 60.0 / count + 0.5; 320 sample.time = time * 1000; 321 if (callback) callback (DC_SAMPLE_TIME, &sample, userdata); 322 323 // Vendor specific data 324 sample.vendor.type = SAMPLE_VENDOR_OCEANIC_VTPRO; 325 sample.vendor.size = PAGESIZE / 2; 326 sample.vendor.data = data + offset; 327 if (callback) callback (DC_SAMPLE_VENDOR, &sample, userdata); 328 329 // Depth (ft) 330 unsigned int depth = 0; 331 if (parser->model == AERIS500AI) { 332 depth = (array_uint16_le(data + offset + 2) & 0x0FF0) >> 4; 333 } else { 334 depth = data[offset + 3]; 335 } 336 sample.depth = depth * FEET; 337 if (callback) callback (DC_SAMPLE_DEPTH, &sample, userdata); 338 339 // Temperature (°F) 340 unsigned int temperature = 0; 341 if (parser->model == AERIS500AI) { 342 temperature = (array_uint16_le(data + offset + 6) & 0x0FF0) >> 4; 343 } else { 344 temperature = data[offset + 6]; 345 } 346 sample.temperature = (temperature - 32.0) * (5.0 / 9.0); 347 if (callback) callback (DC_SAMPLE_TEMPERATURE, &sample, userdata); 348 349 // NDL / Deco 350 if (parser->model != AERIS500AI) { 351 unsigned int decostop = (data[offset + 5] & 0xF0) >> 4; 352 unsigned int decotime = array_uint16_le(data + offset + 4) & 0x0FFF; 353 if (decostop) { 354 sample.deco.type = DC_DECO_DECOSTOP; 355 sample.deco.depth = decostop * 10 * FEET; 356 } else { 357 sample.deco.type = DC_DECO_NDL; 358 sample.deco.depth = 0.0; 359 } 360 sample.deco.time = decotime * 60; 361 sample.deco.tts = 0; 362 if (callback) callback (DC_SAMPLE_DECO, &sample, userdata); 363 } 364 365 offset += PAGESIZE / 2; 366 } 367 368 return DC_STATUS_SUCCESS; 369 }