mclean_extreme_parser.c (8911B)
1 /* 2 * libdivecomputer 3 * 4 * Copyright (C) 2020 Jef Driesen, David Carron 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 <string.h> 23 #include <stdlib.h> 24 25 #include <libdivecomputer/units.h> 26 27 #include "mclean_extreme.h" 28 #include "context-private.h" 29 #include "parser-private.h" 30 #include "array.h" 31 32 #define ISINSTANCE(parser) dc_device_isinstance((parser), &mclean_extreme_parser_vtable) 33 34 #define SZ_CFG 0x002D 35 #define SZ_COMPUTER (SZ_CFG + 0x6A) 36 #define SZ_HEADER (SZ_CFG + 0x31) 37 #define SZ_SAMPLE 0x0004 38 39 #define EPOCH 946684800 // 2000-01-01 00:00:00 UTC 40 41 #define REC 0 42 #define TEC 1 43 #define CCR 2 44 #define GAUGE 3 45 46 #define INVALID 0xFFFFFFFF 47 48 #define NGASMIXES 8 49 50 typedef struct mclean_extreme_parser_t mclean_extreme_parser_t; 51 52 struct mclean_extreme_parser_t { 53 dc_parser_t base; 54 55 // Cached fields. 56 unsigned int cached; 57 unsigned int ngasmixes; 58 unsigned int gasmix[NGASMIXES]; 59 }; 60 61 static dc_status_t mclean_extreme_parser_get_datetime(dc_parser_t *abstract, dc_datetime_t *datetime); 62 static dc_status_t mclean_extreme_parser_get_field(dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value); 63 static dc_status_t mclean_extreme_parser_samples_foreach(dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata); 64 65 static const dc_parser_vtable_t mclean_extreme_parser_vtable = { 66 sizeof(mclean_extreme_parser_t), 67 DC_FAMILY_MCLEAN_EXTREME, 68 NULL, /* set_clock */ 69 NULL, /* set_atmospheric */ 70 NULL, /* set_density */ 71 mclean_extreme_parser_get_datetime, /* datetime */ 72 mclean_extreme_parser_get_field, /* fields */ 73 mclean_extreme_parser_samples_foreach, /* samples_foreach */ 74 NULL /* destroy */ 75 }; 76 77 dc_status_t 78 mclean_extreme_parser_create(dc_parser_t **out, dc_context_t *context, const unsigned char data[], size_t size) 79 { 80 mclean_extreme_parser_t *parser = NULL; 81 82 if (out == NULL) { 83 return DC_STATUS_INVALIDARGS; 84 } 85 86 // Allocate memory. 87 parser = (mclean_extreme_parser_t *)dc_parser_allocate(context, &mclean_extreme_parser_vtable, data, size); 88 if (parser == NULL) { 89 ERROR(context, "Failed to allocate memory."); 90 return DC_STATUS_NOMEMORY; 91 } 92 93 // Set the default values. 94 parser->cached = 0; 95 parser->ngasmixes = 0; 96 for (unsigned int i = 0; i < NGASMIXES; ++i) { 97 parser->gasmix[i] = INVALID; 98 } 99 100 *out = (dc_parser_t *)parser; 101 102 return DC_STATUS_SUCCESS; 103 } 104 105 static dc_status_t 106 mclean_extreme_parser_get_datetime(dc_parser_t *abstract, dc_datetime_t *datetime) 107 { 108 if (abstract->size < SZ_HEADER) { 109 ERROR(abstract->context, "Corrupt dive data"); 110 return DC_STATUS_DATAFORMAT; 111 } 112 113 unsigned int timestamp = array_uint32_le(abstract->data + SZ_CFG + 0x0000); 114 dc_ticks_t ticks = (dc_ticks_t) timestamp + EPOCH; 115 116 if (!dc_datetime_gmtime (datetime, ticks)) 117 return DC_STATUS_DATAFORMAT; 118 119 datetime->timezone = DC_TIMEZONE_NONE; 120 121 return DC_STATUS_SUCCESS; 122 } 123 124 static dc_status_t 125 mclean_extreme_parser_get_field(dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value) 126 { 127 mclean_extreme_parser_t *parser = (mclean_extreme_parser_t *)abstract; 128 129 if (abstract->size < SZ_HEADER) { 130 ERROR(abstract->context, "Corrupt dive data"); 131 return DC_STATUS_DATAFORMAT; 132 } 133 134 if (!parser->cached) { 135 dc_status_t rc = mclean_extreme_parser_samples_foreach (abstract, NULL, NULL); 136 if (rc != DC_STATUS_SUCCESS) 137 return rc; 138 } 139 140 dc_gasmix_t *gasmix = (dc_gasmix_t *)value; 141 dc_salinity_t *salinity = (dc_salinity_t *)value; 142 143 const unsigned int atmospheric = array_uint16_le(abstract->data + 0x001E); 144 const unsigned int density_index = abstract->data[0x0023]; 145 double density = 0; 146 147 switch (density_index) { 148 case 0: 149 density = 1000.0; 150 break; 151 case 1: 152 density = 1020.0; 153 break; 154 case 2: 155 density = 1030.0; 156 break; 157 default: 158 ERROR(abstract->context, "Corrupt density index in dive data"); 159 return DC_STATUS_DATAFORMAT; 160 } 161 162 if (value) { 163 switch (type) { 164 case DC_FIELD_DIVETIME: 165 *((unsigned int *)value) = array_uint32_le(abstract->data + SZ_CFG + 0x000C) - array_uint32_le(abstract->data + SZ_CFG + 0x0000); 166 break; 167 case DC_FIELD_MAXDEPTH: 168 *((double *)value) = (signed int)(array_uint16_le(abstract->data + SZ_CFG + 0x0016) - atmospheric) * (BAR / 1000.0) / (density * GRAVITY); 169 break; 170 case DC_FIELD_AVGDEPTH: 171 *((double *)value) = (signed int)(array_uint16_le(abstract->data + SZ_CFG + 0x0018) - atmospheric) * (BAR / 1000.0) / (density * GRAVITY); 172 break; 173 case DC_FIELD_SALINITY: 174 salinity->density = density; 175 salinity->type = density_index == 0 ? DC_WATER_FRESH : DC_WATER_SALT; 176 break; 177 case DC_FIELD_ATMOSPHERIC: 178 *((double *)value) = atmospheric / 1000.0; 179 break; 180 case DC_FIELD_TEMPERATURE_MINIMUM: 181 *((double *)value) = (double)abstract->data[SZ_CFG + 0x0010]; 182 break; 183 case DC_FIELD_TEMPERATURE_MAXIMUM: 184 *((double *)value) = (double)abstract->data[SZ_CFG + 0x0011]; 185 break; 186 case DC_FIELD_DIVEMODE: 187 switch (abstract->data[0x002C]) { 188 case REC: 189 case TEC: 190 *((dc_divemode_t *)value) = DC_DIVEMODE_OC; 191 break; 192 case CCR: 193 *((dc_divemode_t *)value) = DC_DIVEMODE_CCR; 194 break; 195 case GAUGE: 196 *((dc_divemode_t *)value) = DC_DIVEMODE_GAUGE; 197 break; 198 default: 199 ERROR(abstract->context, "Corrupt dive mode in dive data"); 200 return DC_STATUS_DATAFORMAT; 201 } 202 break; 203 case DC_FIELD_GASMIX_COUNT: 204 *((unsigned int *)value) = parser->ngasmixes; 205 break; 206 case DC_FIELD_GASMIX: 207 gasmix->usage = DC_USAGE_NONE; 208 gasmix->helium = 0.01 * abstract->data[0x0001 + 1 + 2 * parser->gasmix[flags]]; 209 gasmix->oxygen = 0.01 * abstract->data[0x0001 + 0 + 2 * parser->gasmix[flags]]; 210 gasmix->nitrogen = 1.0 - gasmix->oxygen - gasmix->helium; 211 break; 212 default: 213 return DC_STATUS_UNSUPPORTED; 214 } 215 } 216 217 return DC_STATUS_SUCCESS; 218 } 219 220 static dc_status_t 221 mclean_extreme_parser_samples_foreach(dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata) 222 { 223 mclean_extreme_parser_t *parser = (mclean_extreme_parser_t *)abstract; 224 225 if (abstract->size < SZ_HEADER) { 226 ERROR(abstract->context, "Corrupt dive data"); 227 return DC_STATUS_DATAFORMAT; 228 } 229 230 const unsigned int nsamples = array_uint16_le(abstract->data + 0x005C); 231 232 if (abstract->size != SZ_HEADER + nsamples * SZ_SAMPLE) { 233 ERROR(abstract->context, "Corrupt dive data"); 234 return DC_STATUS_DATAFORMAT; 235 } 236 237 unsigned int ngasmixes = 0; 238 unsigned int gasmix[NGASMIXES] = {0}; 239 unsigned int gasmix_previous = INVALID; 240 241 const unsigned int interval = 10; 242 unsigned int time = 0; 243 size_t offset = SZ_HEADER; 244 for (unsigned int i = 0; i < nsamples; ++i) { 245 dc_sample_value_t sample = { 0 }; 246 247 const unsigned int depth = array_uint16_le(abstract->data + offset + 0); 248 const unsigned int temperature = abstract->data[offset + 2]; 249 const unsigned int flags = abstract->data[offset + 3]; 250 const unsigned int ccr = flags & 0x80; 251 const unsigned int gasmix_id = (flags & 0x1C) >> 2; 252 const unsigned int sp_index = (flags & 0x60) >> 5; 253 const unsigned int setpoint = abstract->data[0x0013 + sp_index]; 254 255 time += interval; 256 sample.time = time * 1000; 257 if (callback) callback(DC_SAMPLE_TIME, &sample, userdata); 258 259 sample.depth = 0.1 * depth; 260 if (callback) callback(DC_SAMPLE_DEPTH, &sample, userdata); 261 262 sample.temperature = temperature; 263 if (callback) callback(DC_SAMPLE_TEMPERATURE, &sample, userdata); 264 265 if (gasmix_id != gasmix_previous) { 266 // Find the gasmix in the list. 267 unsigned int idx = 0; 268 while (idx < ngasmixes) { 269 if (gasmix_id == gasmix[idx]) 270 break; 271 idx++; 272 } 273 274 // Add it to list if not found. 275 if (idx >= ngasmixes) { 276 if (idx >= NGASMIXES) { 277 ERROR (abstract->context, "Maximum number of gas mixes reached."); 278 return DC_STATUS_DATAFORMAT; 279 } 280 gasmix[idx] = gasmix_id; 281 ngasmixes = idx + 1; 282 } 283 284 sample.gasmix = idx; 285 if (callback) callback(DC_SAMPLE_GASMIX, &sample, userdata); 286 gasmix_previous = gasmix_id; 287 } 288 289 if (ccr) { 290 sample.setpoint = 0.01 * setpoint; 291 if (callback) callback(DC_SAMPLE_SETPOINT, &sample, userdata); 292 } 293 294 offset += SZ_SAMPLE; 295 } 296 297 // Cache the data for later use. 298 for (unsigned int i = 0; i < ngasmixes; ++i) { 299 parser->gasmix[i] = gasmix[i]; 300 } 301 parser->ngasmixes = ngasmixes; 302 parser->cached = 1; 303 304 return DC_STATUS_SUCCESS; 305 }