diverite_nitekq_parser.c (8366B)
1 /* 2 * libdivecomputer 3 * 4 * Copyright (C) 2013 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 "diverite_nitekq.h" 27 #include "context-private.h" 28 #include "parser-private.h" 29 #include "array.h" 30 31 #define ISINSTANCE(parser) dc_device_isinstance((parser), &diverite_nitekq_parser_vtable) 32 33 #define SZ_LOGBOOK 6 34 35 #define NGASMIXES 7 36 37 typedef struct diverite_nitekq_parser_t diverite_nitekq_parser_t; 38 39 struct diverite_nitekq_parser_t { 40 dc_parser_t base; 41 // Cached fields. 42 unsigned int cached; 43 dc_divemode_t divemode; 44 unsigned int metric; 45 unsigned int ngasmixes; 46 unsigned int o2[NGASMIXES]; 47 unsigned int he[NGASMIXES]; 48 unsigned int divetime; 49 double maxdepth; 50 }; 51 52 static dc_status_t diverite_nitekq_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime); 53 static dc_status_t diverite_nitekq_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value); 54 static dc_status_t diverite_nitekq_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata); 55 56 static const dc_parser_vtable_t diverite_nitekq_parser_vtable = { 57 sizeof(diverite_nitekq_parser_t), 58 DC_FAMILY_DIVERITE_NITEKQ, 59 NULL, /* set_clock */ 60 NULL, /* set_atmospheric */ 61 NULL, /* set_density */ 62 diverite_nitekq_parser_get_datetime, /* datetime */ 63 diverite_nitekq_parser_get_field, /* fields */ 64 diverite_nitekq_parser_samples_foreach, /* samples_foreach */ 65 NULL /* destroy */ 66 }; 67 68 69 dc_status_t 70 diverite_nitekq_parser_create (dc_parser_t **out, dc_context_t *context, const unsigned char data[], size_t size) 71 { 72 diverite_nitekq_parser_t *parser = NULL; 73 74 if (out == NULL) 75 return DC_STATUS_INVALIDARGS; 76 77 // Allocate memory. 78 parser = (diverite_nitekq_parser_t *) dc_parser_allocate (context, &diverite_nitekq_parser_vtable, data, size); 79 if (parser == NULL) { 80 ERROR (context, "Failed to allocate memory."); 81 return DC_STATUS_NOMEMORY; 82 } 83 84 // Set the default values. 85 parser->cached = 0; 86 parser->divemode = DC_DIVEMODE_OC; 87 parser->metric = 0; 88 parser->divetime = 0; 89 parser->maxdepth = 0.0; 90 parser->ngasmixes = 0; 91 for (unsigned int i = 0; i < NGASMIXES; ++i) { 92 parser->o2[i] = 0; 93 parser->he[i] = 0; 94 } 95 96 *out = (dc_parser_t*) parser; 97 98 return DC_STATUS_SUCCESS; 99 } 100 101 102 static dc_status_t 103 diverite_nitekq_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime) 104 { 105 if (abstract->size < SZ_LOGBOOK) 106 return DC_STATUS_DATAFORMAT; 107 108 const unsigned char *p = abstract->data; 109 110 if (datetime) { 111 datetime->year = p[0] + 2000; 112 datetime->month = p[1]; 113 datetime->day = p[2]; 114 datetime->hour = p[3]; 115 datetime->minute = p[4]; 116 datetime->second = p[5]; 117 datetime->timezone = DC_TIMEZONE_NONE; 118 } 119 120 return DC_STATUS_SUCCESS; 121 } 122 123 124 static dc_status_t 125 diverite_nitekq_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value) 126 { 127 diverite_nitekq_parser_t *parser = (diverite_nitekq_parser_t *) abstract; 128 129 if (abstract->size < SZ_LOGBOOK) 130 return DC_STATUS_DATAFORMAT; 131 132 dc_gasmix_t *gasmix = (dc_gasmix_t *) value; 133 134 if (!parser->cached) { 135 dc_status_t rc = diverite_nitekq_parser_samples_foreach (abstract, NULL, NULL); 136 if (rc != DC_STATUS_SUCCESS) 137 return rc; 138 } 139 140 if (value) { 141 switch (type) { 142 case DC_FIELD_DIVETIME: 143 *((unsigned int *) value) = parser->divetime; 144 break; 145 case DC_FIELD_MAXDEPTH: 146 if (parser->metric) 147 *((double *) value) = parser->maxdepth / 10.0; 148 else 149 *((double *) value) = parser->maxdepth * FEET / 10.0; 150 break; 151 case DC_FIELD_GASMIX_COUNT: 152 *((unsigned int *) value) = parser->ngasmixes; 153 break; 154 case DC_FIELD_GASMIX: 155 gasmix->usage = DC_USAGE_NONE; 156 gasmix->helium = parser->he[flags] / 100.0; 157 gasmix->oxygen = parser->o2[flags] / 100.0; 158 gasmix->nitrogen = 1.0 - gasmix->oxygen - gasmix->helium; 159 break; 160 case DC_FIELD_DIVEMODE: 161 *((dc_divemode_t *) value) = parser->divemode; 162 break; 163 default: 164 return DC_STATUS_UNSUPPORTED; 165 } 166 } 167 168 return DC_STATUS_SUCCESS; 169 } 170 171 172 static dc_status_t 173 diverite_nitekq_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata) 174 { 175 diverite_nitekq_parser_t *parser = (diverite_nitekq_parser_t *) abstract; 176 177 if (abstract->size < SZ_LOGBOOK) 178 return DC_STATUS_DATAFORMAT; 179 180 const unsigned char *data = abstract->data + SZ_LOGBOOK; 181 unsigned int size = abstract->size - SZ_LOGBOOK; 182 183 unsigned int type = 0; 184 unsigned int metric = 0; 185 unsigned int interval = 0; 186 unsigned int maxdepth = 0; 187 unsigned int oxygen[NGASMIXES]; 188 unsigned int helium[NGASMIXES]; 189 unsigned int ngasmixes = 0; 190 unsigned int gasmix = 0xFFFFFFFF; /* initialize with impossible value */ 191 unsigned int gasmix_previous = gasmix; 192 dc_divemode_t divemode = DC_DIVEMODE_OC; 193 194 unsigned int time = 0; 195 unsigned int offset = 0; 196 while (offset + 2 <= size) { 197 if (data[offset] == 0xFF) { 198 unsigned int o2 = 0, he = 0; 199 unsigned int i = 0; 200 201 type = data[offset + 1]; 202 switch (type) { 203 case 0x01: // Settings 204 if (offset + 27 > size) 205 return DC_STATUS_DATAFORMAT; 206 metric = (data[offset + 0x10] & 0x04) >> 2; 207 interval = data[offset + 0x11]; 208 offset += 27; 209 break; 210 case 0x02: // OC Samples 211 case 0x03: // CC Samples 212 offset += 2; 213 break; 214 case 0x04: // Gas Change 215 if (offset + 7 > size) 216 return DC_STATUS_DATAFORMAT; 217 218 // Get the new gas mix. 219 o2 = data[offset + 5]; 220 he = data[offset + 6]; 221 222 // Find the gasmix in the list. 223 i = 0; 224 while (i < ngasmixes) { 225 if (o2 == oxygen[i] && he == helium[i]) 226 break; 227 i++; 228 } 229 230 // Add it to list if not found. 231 if (i >= ngasmixes) { 232 if (i >= NGASMIXES) { 233 ERROR (abstract->context, "Maximum number of gas mixes reached."); 234 return DC_STATUS_DATAFORMAT; 235 } 236 oxygen[i] = o2; 237 helium[i] = he; 238 ngasmixes = i + 1; 239 } 240 241 // Remember the index. 242 gasmix = i; 243 offset += 7; 244 break; 245 default: 246 ERROR (abstract->context, "Unknown type %02x", type); 247 return DC_STATUS_DATAFORMAT; 248 } 249 } else if (type == 2 || type == 3) { 250 dc_sample_value_t sample = {0}; 251 252 if (interval == 0) { 253 ERROR (abstract->context, "No sample interval present."); 254 return DC_STATUS_DATAFORMAT; 255 } 256 257 // Time (seconds). 258 time += interval; 259 sample.time = time * 1000; 260 if (callback) callback (DC_SAMPLE_TIME, &sample, userdata); 261 262 // Gas change 263 if (gasmix != gasmix_previous) { 264 sample.gasmix = gasmix; 265 if (callback) callback (DC_SAMPLE_GASMIX, &sample, userdata); 266 gasmix_previous = gasmix; 267 } 268 269 // Depth (1/10 m or ft). 270 unsigned int depth = array_uint16_be (data + offset); 271 if (maxdepth < depth) 272 maxdepth = depth; 273 if (metric) 274 sample.depth = depth / 10.0; 275 else 276 sample.depth = depth * FEET / 10.0; 277 if (callback) callback (DC_SAMPLE_DEPTH, &sample, userdata); 278 offset += 2; 279 280 if (type == 3) { 281 // Dive mode 282 divemode = DC_DIVEMODE_CCR; 283 284 // PPO2 285 if (offset + 1 > size) 286 return DC_STATUS_DATAFORMAT; 287 unsigned int ppo2 = data[offset]; 288 sample.ppo2.sensor = DC_SENSOR_NONE; 289 sample.ppo2.value = ppo2 / 100.0; 290 if (callback) callback (DC_SAMPLE_PPO2, &sample, userdata); 291 offset++; 292 } 293 } else { 294 ERROR (abstract->context, "Invalid sample type %02x.", type); 295 return DC_STATUS_DATAFORMAT; 296 } 297 } 298 299 // Cache the data for later use. 300 for (unsigned int i = 0; i < ngasmixes; ++i) { 301 parser->he[i] = helium[i]; 302 parser->o2[i] = oxygen[i]; 303 } 304 parser->ngasmixes = ngasmixes; 305 parser->maxdepth = maxdepth; 306 parser->divetime = time; 307 parser->metric = metric; 308 parser->divemode = divemode; 309 parser->cached = 1; 310 311 return DC_STATUS_SUCCESS; 312 }