suunto_eon_parser.c (8943B)
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 24 #include <libdivecomputer/units.h> 25 26 #include "suunto_eon.h" 27 #include "context-private.h" 28 #include "parser-private.h" 29 #include "array.h" 30 31 #define ISINSTANCE(parser) dc_parser_isinstance((parser), &suunto_eon_parser_vtable) 32 33 typedef struct suunto_eon_parser_t suunto_eon_parser_t; 34 35 struct suunto_eon_parser_t { 36 dc_parser_t base; 37 int spyder; 38 // Cached fields. 39 unsigned int cached; 40 unsigned int divetime; 41 unsigned int maxdepth; 42 unsigned int marker; 43 unsigned int nitrox; 44 }; 45 46 static dc_status_t suunto_eon_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime); 47 static dc_status_t suunto_eon_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value); 48 static dc_status_t suunto_eon_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata); 49 50 static const dc_parser_vtable_t suunto_eon_parser_vtable = { 51 sizeof(suunto_eon_parser_t), 52 DC_FAMILY_SUUNTO_EON, 53 NULL, /* set_clock */ 54 NULL, /* set_atmospheric */ 55 NULL, /* set_density */ 56 suunto_eon_parser_get_datetime, /* datetime */ 57 suunto_eon_parser_get_field, /* fields */ 58 suunto_eon_parser_samples_foreach, /* samples_foreach */ 59 NULL /* destroy */ 60 }; 61 62 static dc_status_t 63 suunto_eon_parser_cache (suunto_eon_parser_t *parser) 64 { 65 dc_parser_t *abstract = (dc_parser_t *) parser; 66 const unsigned char *data = parser->base.data; 67 unsigned int size = parser->base.size; 68 69 if (parser->cached) { 70 return DC_STATUS_SUCCESS; 71 } 72 73 if (size < 13) { 74 return DC_STATUS_DATAFORMAT; 75 } 76 77 // The Solution Nitrox/Vario stores nitrox data, not tank pressure. 78 unsigned int nitrox = !parser->spyder && (data[4] & 0x80); 79 80 // Parse the samples. 81 unsigned int interval = data[3]; 82 unsigned int nsamples = 0; 83 unsigned int depth = 0, maxdepth = 0; 84 unsigned int offset = 11; 85 while (offset < size && data[offset] != 0x80) { 86 unsigned char value = data[offset++]; 87 if (value < 0x7d || value > 0x82) { 88 depth += (signed char) value; 89 if (depth > maxdepth) 90 maxdepth = depth; 91 nsamples++; 92 } 93 } 94 95 // Check the end marker. 96 unsigned int marker = offset; 97 if (marker + 2 >= size || data[marker] != 0x80) { 98 ERROR (abstract->context, "No valid end marker found!"); 99 return DC_STATUS_DATAFORMAT; 100 } 101 102 // Cache the data for later use. 103 parser->divetime = nsamples * interval; 104 parser->maxdepth = maxdepth; 105 parser->marker = marker; 106 parser->nitrox = nitrox; 107 parser->cached = 1; 108 109 return DC_STATUS_SUCCESS; 110 } 111 112 dc_status_t 113 suunto_eon_parser_create (dc_parser_t **out, dc_context_t *context, const unsigned char data[], size_t size, int spyder) 114 { 115 suunto_eon_parser_t *parser = NULL; 116 117 if (out == NULL) 118 return DC_STATUS_INVALIDARGS; 119 120 // Allocate memory. 121 parser = (suunto_eon_parser_t *) dc_parser_allocate (context, &suunto_eon_parser_vtable, data, size); 122 if (parser == NULL) { 123 ERROR (context, "Failed to allocate memory."); 124 return DC_STATUS_NOMEMORY; 125 } 126 127 // Set the default values. 128 parser->spyder = spyder; 129 parser->cached = 0; 130 parser->divetime = 0; 131 parser->maxdepth = 0; 132 parser->marker = 0; 133 parser->nitrox = 0; 134 135 *out = (dc_parser_t*) parser; 136 137 return DC_STATUS_SUCCESS; 138 } 139 140 141 static dc_status_t 142 suunto_eon_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime) 143 { 144 suunto_eon_parser_t *parser = (suunto_eon_parser_t *) abstract; 145 146 if (abstract->size < 6 + 5) 147 return DC_STATUS_DATAFORMAT; 148 149 const unsigned char *p = abstract->data + 6; 150 151 if (datetime) { 152 if (parser->spyder) { 153 datetime->year = p[0] + (p[0] < 90 ? 2000 : 1900); 154 datetime->month = p[1]; 155 datetime->day = p[2]; 156 datetime->hour = p[3]; 157 datetime->minute = p[4]; 158 } else { 159 datetime->year = bcd2dec (p[0]) + (bcd2dec (p[0]) < 85 ? 2000 : 1900); 160 datetime->month = bcd2dec (p[1]); 161 datetime->day = bcd2dec (p[2]); 162 datetime->hour = bcd2dec (p[3]); 163 datetime->minute = bcd2dec (p[4]); 164 } 165 datetime->second = 0; 166 datetime->timezone = DC_TIMEZONE_NONE; 167 } 168 169 return DC_STATUS_SUCCESS; 170 } 171 172 173 static dc_status_t 174 suunto_eon_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value) 175 { 176 suunto_eon_parser_t *parser = (suunto_eon_parser_t *) abstract; 177 178 const unsigned char *data = abstract->data; 179 180 // Cache the data. 181 dc_status_t rc = suunto_eon_parser_cache (parser); 182 if (rc != DC_STATUS_SUCCESS) 183 return rc; 184 185 dc_gasmix_t *gasmix = (dc_gasmix_t *) value; 186 dc_tank_t *tank = (dc_tank_t *) value; 187 188 unsigned int oxygen = 21; 189 unsigned int beginpressure = 0; 190 unsigned int endpressure = 0; 191 if (parser->nitrox) { 192 oxygen = data[0x05]; 193 } else { 194 beginpressure = data[5] * 2; 195 endpressure = data[parser->marker + 2] * 2; 196 } 197 198 if (value) { 199 switch (type) { 200 case DC_FIELD_DIVETIME: 201 *((unsigned int *) value) = parser->divetime; 202 break; 203 case DC_FIELD_MAXDEPTH: 204 *((double *) value) = parser->maxdepth * FEET; 205 break; 206 case DC_FIELD_GASMIX_COUNT: 207 *((unsigned int *) value) = 1; 208 break; 209 case DC_FIELD_GASMIX: 210 gasmix->usage = DC_USAGE_NONE; 211 gasmix->helium = 0.0; 212 gasmix->oxygen = oxygen / 100.0; 213 gasmix->nitrogen = 1.0 - gasmix->oxygen - gasmix->helium; 214 break; 215 case DC_FIELD_TANK_COUNT: 216 if (beginpressure == 0 && endpressure == 0) 217 *((unsigned int *) value) = 0; 218 else 219 *((unsigned int *) value) = 1; 220 break; 221 case DC_FIELD_TANK: 222 tank->type = DC_TANKVOLUME_NONE; 223 tank->volume = 0.0; 224 tank->workpressure = 0.0; 225 tank->gasmix = 0; 226 tank->beginpressure = beginpressure; 227 tank->endpressure = endpressure; 228 tank->usage = DC_TANK_USAGE_NONE; 229 break; 230 case DC_FIELD_TEMPERATURE_MINIMUM: 231 if (parser->spyder) 232 *((double *) value) = (signed char) data[parser->marker + 1]; 233 else 234 *((double *) value) = data[parser->marker + 1] - 40; 235 break; 236 default: 237 return DC_STATUS_UNSUPPORTED; 238 } 239 } 240 241 return DC_STATUS_SUCCESS; 242 } 243 244 245 static dc_status_t 246 suunto_eon_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata) 247 { 248 suunto_eon_parser_t *parser = (suunto_eon_parser_t *) abstract; 249 const unsigned char *data = abstract->data; 250 unsigned int size = abstract->size; 251 dc_sample_value_t sample = {0}; 252 253 // Cache the data. 254 dc_status_t rc = suunto_eon_parser_cache (parser); 255 if (rc != DC_STATUS_SUCCESS) 256 return rc; 257 258 // Time 259 sample.time = 0; 260 if (callback) callback (DC_SAMPLE_TIME, &sample, userdata); 261 262 // Depth (0 ft) 263 sample.depth = 0; 264 if (callback) callback (DC_SAMPLE_DEPTH, &sample, userdata); 265 266 // Initial gas mix. 267 sample.gasmix = 0; 268 if (callback) callback (DC_SAMPLE_GASMIX, &sample, userdata); 269 270 unsigned int depth = 0; 271 unsigned int time = 0; 272 unsigned int interval = data[3]; 273 unsigned int complete = 1; 274 unsigned int offset = 11; 275 while (offset < size && data[offset] != 0x80) { 276 unsigned char value = data[offset++]; 277 278 if (complete) { 279 // Time (seconds). 280 time += interval; 281 sample.time = time * 1000; 282 if (callback) callback (DC_SAMPLE_TIME, &sample, userdata); 283 complete = 0; 284 } 285 286 if (value < 0x7d || value > 0x82) { 287 // Delta depth. 288 depth += (signed char) value; 289 290 // Depth (ft). 291 sample.depth = depth * FEET; 292 if (callback) callback (DC_SAMPLE_DEPTH, &sample, userdata); 293 294 complete = 1; 295 } else { 296 // Event. 297 sample.event.type = SAMPLE_EVENT_NONE; 298 sample.event.time = 0; 299 sample.event.flags = 0; 300 sample.event.value = 0; 301 switch (value) { 302 case 0x7d: // Surface 303 sample.event.type = SAMPLE_EVENT_SURFACE; 304 break; 305 case 0x7e: // Deco, ASC 306 sample.event.type = SAMPLE_EVENT_DECOSTOP; 307 break; 308 case 0x7f: // Ceiling, ERR 309 sample.event.type = SAMPLE_EVENT_CEILING; 310 break; 311 case 0x81: // Slow 312 sample.event.type = SAMPLE_EVENT_ASCENT; 313 break; 314 default: // Unknown 315 WARNING (abstract->context, "Unknown event"); 316 break; 317 } 318 319 if (sample.event.type != SAMPLE_EVENT_NONE) { 320 if (callback) callback (DC_SAMPLE_EVENT, &sample, userdata); 321 } 322 } 323 } 324 325 // Time 326 if (complete) { 327 time += interval; 328 sample.time = time * 1000; 329 if (callback) callback (DC_SAMPLE_TIME, &sample, userdata); 330 } 331 332 // Depth (0 ft) 333 sample.depth = 0; 334 if (callback) callback (DC_SAMPLE_DEPTH, &sample, userdata); 335 336 return DC_STATUS_SUCCESS; 337 }