atomics_cobalt_parser.c (10700B)
1 /* 2 * libdivecomputer 3 * 4 * Copyright (C) 2011 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> 24 #include <string.h> 25 26 #ifdef _MSC_VER 27 #define snprintf _snprintf 28 #endif 29 30 #include <libdivecomputer/units.h> 31 32 #include "atomics_cobalt.h" 33 #include "context-private.h" 34 #include "parser-private.h" 35 #include "array.h" 36 37 #define ISINSTANCE(parser) dc_parser_isinstance((parser), &atomics_cobalt_parser_vtable) 38 39 #define SZ_HEADER 228 40 #define SZ_GASMIX 18 41 #define SZ_GASSWITCH 6 42 #define SZ_SEGMENT 16 43 44 typedef struct atomics_cobalt_parser_t atomics_cobalt_parser_t; 45 46 struct atomics_cobalt_parser_t { 47 dc_parser_t base; 48 // Depth calibration. 49 double hydrostatic; 50 }; 51 52 static dc_status_t atomics_cobalt_parser_set_density (dc_parser_t *abstract, double density); 53 static dc_status_t atomics_cobalt_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime); 54 static dc_status_t atomics_cobalt_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value); 55 static dc_status_t atomics_cobalt_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata); 56 57 static const dc_parser_vtable_t atomics_cobalt_parser_vtable = { 58 sizeof(atomics_cobalt_parser_t), 59 DC_FAMILY_ATOMICS_COBALT, 60 NULL, /* set_clock */ 61 NULL, /* set_atmospheric */ 62 atomics_cobalt_parser_set_density, /* set_density */ 63 atomics_cobalt_parser_get_datetime, /* datetime */ 64 atomics_cobalt_parser_get_field, /* fields */ 65 atomics_cobalt_parser_samples_foreach, /* samples_foreach */ 66 NULL /* destroy */ 67 }; 68 69 70 dc_status_t 71 atomics_cobalt_parser_create (dc_parser_t **out, dc_context_t *context, const unsigned char data[], size_t size) 72 { 73 atomics_cobalt_parser_t *parser = NULL; 74 75 if (out == NULL) 76 return DC_STATUS_INVALIDARGS; 77 78 // Allocate memory. 79 parser = (atomics_cobalt_parser_t *) dc_parser_allocate (context, &atomics_cobalt_parser_vtable, data, size); 80 if (parser == NULL) { 81 ERROR (context, "Failed to allocate memory."); 82 return DC_STATUS_NOMEMORY; 83 } 84 85 // Set the default values. 86 parser->hydrostatic = DEF_DENSITY_SALT * GRAVITY; 87 88 *out = (dc_parser_t*) parser; 89 90 return DC_STATUS_SUCCESS; 91 } 92 93 94 static dc_status_t 95 atomics_cobalt_parser_set_density (dc_parser_t *abstract, double density) 96 { 97 atomics_cobalt_parser_t *parser = (atomics_cobalt_parser_t *) abstract; 98 99 parser->hydrostatic = density * GRAVITY; 100 101 return DC_STATUS_SUCCESS; 102 } 103 104 105 static dc_status_t 106 atomics_cobalt_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime) 107 { 108 if (abstract->size < SZ_HEADER) 109 return DC_STATUS_DATAFORMAT; 110 111 const unsigned char *p = abstract->data; 112 113 if (datetime) { 114 datetime->year = array_uint16_le (p + 0x14); 115 datetime->month = p[0x16]; 116 datetime->day = p[0x17]; 117 datetime->hour = p[0x18]; 118 datetime->minute = p[0x19]; 119 datetime->second = 0; 120 datetime->timezone = DC_TIMEZONE_NONE; 121 } 122 123 return DC_STATUS_SUCCESS; 124 } 125 126 127 #define BUFLEN 16 128 129 130 static dc_status_t 131 atomics_cobalt_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value) 132 { 133 atomics_cobalt_parser_t *parser = (atomics_cobalt_parser_t *) abstract; 134 135 if (abstract->size < SZ_HEADER) 136 return DC_STATUS_DATAFORMAT; 137 138 const unsigned char *p = abstract->data; 139 140 dc_gasmix_t *gasmix = (dc_gasmix_t *) value; 141 dc_tank_t *tank = (dc_tank_t *) value; 142 143 unsigned int atmospheric = array_uint16_le (p + 0x26); 144 char buf[BUFLEN]; 145 dc_field_string_t *string = (dc_field_string_t *) value; 146 147 unsigned int workpressure = 0; 148 149 if (value) { 150 switch (type) { 151 case DC_FIELD_DIVETIME: 152 *((unsigned int *) value) = array_uint16_le (p + 0x58) * 60; 153 break; 154 case DC_FIELD_MAXDEPTH: 155 *((double *) value) = (signed int)(array_uint16_le (p + 0x56) - atmospheric) * (BAR / 1000.0) / parser->hydrostatic; 156 break; 157 case DC_FIELD_GASMIX_COUNT: 158 case DC_FIELD_TANK_COUNT: 159 *((unsigned int *) value) = p[0x2a]; 160 break; 161 case DC_FIELD_GASMIX: 162 gasmix->usage = DC_USAGE_NONE; 163 gasmix->helium = p[SZ_HEADER + SZ_GASMIX * flags + 5] / 100.0; 164 gasmix->oxygen = p[SZ_HEADER + SZ_GASMIX * flags + 4] / 100.0; 165 gasmix->nitrogen = 1.0 - gasmix->oxygen - gasmix->helium; 166 break; 167 case DC_FIELD_TEMPERATURE_SURFACE: 168 *((double *) value) = (p[0x1B] - 32.0) * (5.0 / 9.0); 169 break; 170 case DC_FIELD_TANK: 171 p += SZ_HEADER + SZ_GASMIX * flags; 172 switch (p[2]) { 173 case 1: // Cuft at psi 174 case 2: // Cuft at bar 175 workpressure = array_uint16_le(p + 10); 176 if (workpressure == 0) 177 return DC_STATUS_DATAFORMAT; 178 tank->type = DC_TANKVOLUME_IMPERIAL; 179 tank->volume = array_uint16_le(p + 8) * CUFT * 1000.0; 180 tank->volume /= workpressure * PSI / ATM; 181 tank->workpressure = workpressure * PSI / BAR; 182 break; 183 case 3: // Wet volume in 1/10 liter 184 tank->type = DC_TANKVOLUME_METRIC; 185 tank->volume = array_uint16_le(p + 8) / 10.0; 186 tank->workpressure = 0.0; 187 break; 188 default: 189 return DC_STATUS_DATAFORMAT; 190 } 191 tank->gasmix = flags; 192 tank->beginpressure = array_uint16_le(p + 6) * PSI / BAR; 193 tank->endpressure = array_uint16_le(p + 14) * PSI / BAR; 194 tank->usage = DC_TANK_USAGE_NONE; 195 break; 196 case DC_FIELD_DIVEMODE: 197 switch(p[0x24]) { 198 case 0: // Open Circuit Trimix 199 case 2: // Open Circuit Nitrox 200 *((dc_divemode_t *) value) = DC_DIVEMODE_OC; 201 break; 202 case 1: // Closed Circuit 203 *((dc_divemode_t *) value) = DC_DIVEMODE_CCR; 204 break; 205 default: 206 return DC_STATUS_DATAFORMAT; 207 } 208 break; 209 case DC_FIELD_ATMOSPHERIC: 210 *((double *) value) = atmospheric / 1000.0; 211 break; 212 case DC_FIELD_STRING: 213 switch(flags) { 214 case 0: // Serialnr 215 string->desc = STRING_KEY_SERIAL_NUMBER; 216 snprintf(buf, BUFLEN, "%c%c%c%c-%c%c%c%c", p[4], p[5], p[6], p[7], p[8], p[9], p[10], p[11]); 217 break; 218 case 1: // Program Version 219 string->desc = "Program Version"; 220 snprintf(buf, BUFLEN, "%.2f", array_uint16_le(p + 30) / 100.0); 221 break; 222 case 2: // Boot Version 223 string->desc = "Boot Version"; 224 snprintf(buf, BUFLEN, "%.2f", array_uint16_le(p + 32) / 100.0); 225 break; 226 case 3: // Nofly 227 string->desc = "NoFly Time"; 228 snprintf(buf, BUFLEN, "%0u:%02u", p[0x52], p[0x53]); 229 break; 230 default: 231 return DC_STATUS_UNSUPPORTED; 232 } 233 string->value = strdup(buf); 234 break; 235 default: 236 return DC_STATUS_UNSUPPORTED; 237 } 238 } 239 240 return DC_STATUS_SUCCESS; 241 } 242 243 244 static dc_status_t 245 atomics_cobalt_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata) 246 { 247 atomics_cobalt_parser_t *parser = (atomics_cobalt_parser_t *) abstract; 248 249 const unsigned char *data = abstract->data; 250 unsigned int size = abstract->size; 251 252 if (size < SZ_HEADER) 253 return DC_STATUS_DATAFORMAT; 254 255 unsigned int interval = data[0x1a]; 256 unsigned int ngasmixes = data[0x2a]; 257 unsigned int nswitches = data[0x2b]; 258 unsigned int nsegments = array_uint16_le (data + 0x50); 259 260 unsigned int header = SZ_HEADER + SZ_GASMIX * ngasmixes + 261 SZ_GASSWITCH * nswitches; 262 263 if (size < header + SZ_SEGMENT * nsegments) 264 return DC_STATUS_DATAFORMAT; 265 266 unsigned int atmospheric = array_uint16_le (data + 0x26); 267 268 // Previous gas mix - initialize with impossible value 269 unsigned int gasmix_previous = 0xFFFFFFFF; 270 271 // Get the primary tank. 272 unsigned int tank = 0; 273 while (tank < ngasmixes) { 274 unsigned int sensor = array_uint16_le(data + SZ_HEADER + SZ_GASMIX * tank + 12); 275 if (sensor == 1) 276 break; 277 tank++; 278 } 279 if (tank >= ngasmixes) { 280 ERROR (abstract->context, "Invalid primary tank index."); 281 return DC_STATUS_DATAFORMAT; 282 } 283 284 unsigned int time = 0; 285 unsigned int in_deco = 0; 286 unsigned int offset = header; 287 while (offset + SZ_SEGMENT <= size) { 288 dc_sample_value_t sample = {0}; 289 290 // Time (seconds). 291 time += interval; 292 sample.time = time * 1000; 293 if (callback) callback (DC_SAMPLE_TIME, &sample, userdata); 294 295 // Depth (1/1000 bar). 296 unsigned int depth = array_uint16_le (data + offset + 0); 297 sample.depth = (signed int)(depth - atmospheric) * (BAR / 1000.0) / parser->hydrostatic; 298 if (callback) callback (DC_SAMPLE_DEPTH, &sample, userdata); 299 300 // Pressure (1 psi). 301 unsigned int pressure = array_uint16_le (data + offset + 2); 302 sample.pressure.tank = tank; 303 sample.pressure.value = pressure * PSI / BAR; 304 if (callback) callback (DC_SAMPLE_PRESSURE, &sample, userdata); 305 306 // Current gas mix 307 unsigned int gasmix = data[offset + 4]; 308 if (gasmix != gasmix_previous) { 309 unsigned int idx = 0; 310 while (idx < ngasmixes) { 311 if (data[SZ_HEADER + SZ_GASMIX * idx + 0] == gasmix) 312 break; 313 idx++; 314 } 315 if (idx >= ngasmixes) { 316 ERROR (abstract->context, "Invalid gas mix index."); 317 return DC_STATUS_DATAFORMAT; 318 } 319 sample.gasmix = idx; 320 if (callback) callback (DC_SAMPLE_GASMIX, &sample, userdata); 321 gasmix_previous = gasmix; 322 } 323 324 // Temperature (1 °F). 325 unsigned int temperature = data[offset + 8]; 326 sample.temperature = (temperature - 32.0) * (5.0 / 9.0); 327 if (callback) callback (DC_SAMPLE_TEMPERATURE, &sample, userdata); 328 329 // violation status 330 sample.event.type = 0; 331 sample.event.time = 0; 332 sample.event.value = 0; 333 sample.event.flags = 0; 334 unsigned int violation = data[offset + 11]; 335 if (violation & 0x01) { 336 sample.event.type = SAMPLE_EVENT_ASCENT; 337 if (callback) callback (DC_SAMPLE_EVENT, &sample, userdata); 338 } 339 if (violation & 0x04) { 340 sample.event.type = SAMPLE_EVENT_CEILING; 341 if (callback) callback (DC_SAMPLE_EVENT, &sample, userdata); 342 } 343 if (violation & 0x08) { 344 sample.event.type = SAMPLE_EVENT_PO2; 345 if (callback) callback (DC_SAMPLE_EVENT, &sample, userdata); 346 } 347 348 // NDL & deco 349 unsigned int ndl = data[offset + 5] * 60; 350 if (ndl > 0) 351 in_deco = 0; 352 else if (ndl == 0 && (violation & 0x02)) 353 in_deco = 1; 354 if (in_deco) 355 sample.deco.type = DC_DECO_DECOSTOP; 356 else 357 sample.deco.type = DC_DECO_NDL; 358 sample.deco.time = ndl; 359 sample.deco.depth = 0.0; 360 sample.deco.tts = 0; 361 if (callback) callback (DC_SAMPLE_DECO, &sample, userdata); 362 363 offset += SZ_SEGMENT; 364 } 365 366 return DC_STATUS_SUCCESS; 367 }