seac_screen_parser.c (12247B)
1 /* 2 * libdivecomputer 3 * 4 * Copyright (C) 2022 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 "seac_screen.h" 25 #include "seac_screen_common.h" 26 #include "context-private.h" 27 #include "parser-private.h" 28 #include "array.h" 29 30 #define ISINSTANCE(parser) dc_device_isinstance((parser), &seac_screen_parser_vtable) 31 32 #define NGASMIXES 2 33 34 #define INVALID 0xFFFFFFFF 35 36 #define ACTION 0x01 37 #define SCREEN 0x02 38 #define TABLET 0x10 39 40 typedef struct seac_screen_parser_t seac_screen_parser_t; 41 42 struct seac_screen_parser_t { 43 dc_parser_t base; 44 unsigned int model; 45 // Cached fields. 46 unsigned int cached; 47 unsigned int ngasmixes; 48 unsigned int oxygen[NGASMIXES]; 49 unsigned int gf_low; 50 unsigned int gf_high; 51 }; 52 53 static dc_status_t seac_screen_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime); 54 static dc_status_t seac_screen_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value); 55 static dc_status_t seac_screen_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata); 56 57 static const dc_parser_vtable_t seac_screen_parser_vtable = { 58 sizeof(seac_screen_parser_t), 59 DC_FAMILY_SEAC_SCREEN, 60 NULL, /* set_clock */ 61 NULL, /* set_atmospheric */ 62 NULL, /* set_density */ 63 seac_screen_parser_get_datetime, /* datetime */ 64 seac_screen_parser_get_field, /* fields */ 65 seac_screen_parser_samples_foreach, /* samples_foreach */ 66 NULL /* destroy */ 67 }; 68 69 dc_status_t 70 seac_screen_parser_create (dc_parser_t **out, dc_context_t *context, const unsigned char data[], size_t size, unsigned int model) 71 { 72 seac_screen_parser_t *parser = NULL; 73 74 if (out == NULL) 75 return DC_STATUS_INVALIDARGS; 76 77 // Allocate memory. 78 parser = (seac_screen_parser_t *) dc_parser_allocate (context, &seac_screen_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->model = model; 86 parser->cached = 0; 87 parser->ngasmixes = 0; 88 for (unsigned int i = 0; i < NGASMIXES; ++i) { 89 parser->oxygen[i] = 0; 90 } 91 parser->gf_low = 0; 92 parser->gf_high = 0; 93 94 *out = (dc_parser_t *) parser; 95 96 return DC_STATUS_SUCCESS; 97 } 98 99 static dc_status_t 100 seac_screen_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime) 101 { 102 const unsigned char *data = abstract->data; 103 104 static const signed int tz_array[] = { 105 -12 * 60, /* UTC-12 */ 106 -11 * 60, /* UTC-11 */ 107 -10 * 60, /* UTC-10 */ 108 -9 * 60 - 30, /* UTC-9:30 */ 109 -9 * 60, /* UTC-9 */ 110 -8 * 60, /* UTC-8 */ 111 -7 * 60, /* UTC-7 */ 112 -6 * 60, /* UTC-6 */ 113 -5 * 60, /* UTC-5 */ 114 -4 * 60 - 30, /* UTC-4:30 */ 115 -4 * 60, /* UTC-4 */ 116 -3 * 60 - 30, /* UTC-3:30 */ 117 -3 * 60, /* UTC-3 */ 118 -2 * 60, /* UTC-2 */ 119 -1 * 60, /* UTC-1 */ 120 0 * 60, /* UTC */ 121 1 * 60, /* UTC+1 */ 122 2 * 60, /* UTC+2 */ 123 3 * 60, /* UTC+3 */ 124 3 * 60 + 30, /* UTC+3:30 */ 125 4 * 60, /* UTC+4 */ 126 4 * 60 + 30, /* UTC+4:30 */ 127 5 * 60, /* UTC+5 */ 128 5 * 60 + 30, /* UTC+5:30 */ 129 5 * 60 + 45, /* UTC+5:45 */ 130 6 * 60, /* UTC+6 */ 131 6 * 60 + 30, /* UTC+6:30 */ 132 7 * 60, /* UTC+7 */ 133 8 * 60, /* UTC+8 */ 134 8 * 60 + 45, /* UTC+8:45 */ 135 9 * 60, /* UTC+9 */ 136 9 * 60 + 30, /* UTC+9:30 */ 137 9 * 60 + 45, /* UTC+9:45 */ 138 10 * 60, /* UTC+10 */ 139 10 * 60 + 30, /* UTC+10:30 */ 140 11 * 60, /* UTC+11 */ 141 11 * 60 + 30, /* UTC+11:30 */ 142 12 * 60, /* UTC+12 */ 143 12 * 60 + 45, /* UTC+12:45 */ 144 13 * 60, /* UTC+13 */ 145 13 * 60 + 45, /* UTC+13:45 */ 146 14 * 60, /* UTC+14 */ 147 }; 148 149 if (abstract->size < SZ_HEADER) 150 return DC_STATUS_DATAFORMAT; 151 152 // Get the dive ID. 153 unsigned int dive_id = array_uint32_le (data + 0x00); 154 155 // Check the header records. 156 for (unsigned int i = 0; i < 2; ++i) { 157 unsigned int type = i == 0 ? HEADER1 : HEADER2; 158 if (!seac_screen_record_isvalid (abstract->context, 159 data + i * SZ_HEADER / 2, SZ_HEADER / 2, 160 type, dive_id)) { 161 ERROR (abstract->context, "Invalid header record %u.", i); 162 return DC_STATUS_DATAFORMAT; 163 } 164 } 165 166 // The date/time is stored as UTC time with a timezone offset. To convert to 167 // local time, the UTC time is first converted to unix time (seconds since 168 // the epoch), then adjusted for the timezone offset, and finally converted 169 // back into the broken-down time format. 170 171 dc_datetime_t utc = {0}; 172 utc.year = data[0x10] + 2000; 173 utc.month = data[0x0F]; 174 utc.day = data[0x0E]; 175 utc.hour = data[0x0B]; 176 utc.minute = data[0x0C]; 177 utc.second = data[0x0D]; 178 utc.timezone = 0; 179 180 unsigned int tz_idx = data[0x0A]; 181 if (tz_idx >= C_ARRAY_SIZE(tz_array)) { 182 ERROR (abstract->context, "Invalid timezone index (%u).", tz_idx); 183 return DC_STATUS_DATAFORMAT; 184 } 185 int timezone = tz_array[tz_idx] * 60; 186 187 dc_ticks_t ticks = dc_datetime_mktime (&utc); 188 if (ticks == -1) 189 return DC_STATUS_DATAFORMAT; 190 191 ticks += timezone; 192 193 if (!dc_datetime_gmtime (datetime, ticks)) 194 return DC_STATUS_DATAFORMAT; 195 196 datetime->timezone = timezone; 197 198 return DC_STATUS_SUCCESS; 199 } 200 201 static dc_status_t 202 seac_screen_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value) 203 { 204 seac_screen_parser_t *parser = (seac_screen_parser_t *) abstract; 205 const unsigned char *data = abstract->data; 206 207 if (abstract->size < SZ_HEADER) 208 return DC_STATUS_DATAFORMAT; 209 210 if (!parser->cached) { 211 dc_status_t rc = seac_screen_parser_samples_foreach (abstract, NULL, NULL); 212 if (rc != DC_STATUS_SUCCESS) 213 return rc; 214 } 215 216 dc_gasmix_t *gasmix = (dc_gasmix_t *) value; 217 dc_decomodel_t *decomodel = (dc_decomodel_t *) value; 218 219 if (value) { 220 switch (type) { 221 case DC_FIELD_AVGDEPTH: 222 *((double *) value) = array_uint16_le (data + 0x72) / 100.0; 223 break; 224 case DC_FIELD_MAXDEPTH: 225 *((double *) value) = array_uint16_le (data + 0x62) / 100.0; 226 break; 227 case DC_FIELD_DIVETIME: 228 *((unsigned int *) value) = array_uint32_le (data + 0x64); 229 break; 230 case DC_FIELD_TEMPERATURE_MINIMUM: 231 *((double *) value) = array_uint16_le (data + 0x6A) / 100.0; 232 break; 233 case DC_FIELD_TEMPERATURE_MAXIMUM: 234 *((double *) value) = array_uint16_le (data + 0x68) / 100.0; 235 break; 236 case DC_FIELD_GASMIX_COUNT: 237 *((unsigned int *)value) = parser->ngasmixes; 238 break; 239 case DC_FIELD_GASMIX: 240 gasmix->usage = DC_USAGE_NONE; 241 gasmix->helium = 0.0; 242 gasmix->oxygen = parser->oxygen[flags] / 100.0; 243 gasmix->nitrogen = 1.0 - gasmix->oxygen - gasmix->helium; 244 break; 245 case DC_FIELD_DIVEMODE: 246 switch (data[0x26]) { 247 case 1: 248 *((dc_divemode_t *) value) = DC_DIVEMODE_OC; 249 break; 250 case 2: 251 *((dc_divemode_t *) value) = DC_DIVEMODE_GAUGE; 252 break; 253 case 3: 254 *((dc_divemode_t *) value) = DC_DIVEMODE_FREEDIVE; 255 break; 256 default: 257 ERROR (abstract->context, "Unknown dive mode %i", data[0x26]); 258 return DC_STATUS_DATAFORMAT; 259 } 260 break; 261 case DC_FIELD_DECOMODEL: 262 decomodel->type = DC_DECOMODEL_BUHLMANN; 263 decomodel->conservatism = 0; 264 decomodel->params.gf.low = parser->gf_low; 265 decomodel->params.gf.high = parser->gf_high; 266 break; 267 default: 268 return DC_STATUS_UNSUPPORTED; 269 } 270 } 271 272 return DC_STATUS_SUCCESS; 273 } 274 275 static dc_status_t 276 seac_screen_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata) 277 { 278 seac_screen_parser_t *parser = (seac_screen_parser_t *) abstract; 279 const unsigned char *data = abstract->data; 280 unsigned int size = abstract->size; 281 282 if (abstract->size < SZ_HEADER) 283 return DC_STATUS_DATAFORMAT; 284 285 // Get the dive ID. 286 unsigned int dive_id = array_uint32_le (data + 0x00); 287 288 // Check the header records. 289 for (unsigned int i = 0; i < 2; ++i) { 290 unsigned int type = i == 0 ? HEADER1 : HEADER2; 291 if (!seac_screen_record_isvalid (abstract->context, 292 data + i * SZ_HEADER / 2, SZ_HEADER / 2, 293 type, dive_id)) { 294 ERROR (abstract->context, "Invalid header record %u.", i); 295 return DC_STATUS_DATAFORMAT; 296 } 297 } 298 299 unsigned int ngasmixes = 0; 300 unsigned int oxygen[NGASMIXES] = {0}; 301 unsigned int o2_previous = INVALID; 302 303 unsigned int gf_low = 0; 304 unsigned int gf_high = 0; 305 306 unsigned int time = 0; 307 unsigned int offset = SZ_HEADER; 308 while (offset + SZ_SAMPLE <= size) { 309 dc_sample_value_t sample = {0}; 310 311 // Check the sample record. 312 if (!seac_screen_record_isvalid (abstract->context, data + offset, SZ_SAMPLE, SAMPLE, dive_id)) { 313 ERROR (abstract->context, "Invalid sample record."); 314 return DC_STATUS_DATAFORMAT; 315 } 316 317 unsigned int timestamp = array_uint32_le (data + offset + 0x04); 318 unsigned int depth = array_uint16_le (data + offset + 0x08); 319 unsigned int temperature = array_uint16_le (data + offset + 0x0A); 320 unsigned int o2 = data[offset + 0x0C]; 321 unsigned int decodepth = array_uint16_le (data + offset + 0x0E); 322 unsigned int decotime = array_uint16_le (data + offset + 0x10); 323 unsigned int ndl_tts = array_uint16_le (data + offset + 0x12); 324 unsigned int pressure = array_uint16_le (data + offset + 0x14); 325 unsigned int cns = array_uint16_le (data + offset + 0x16); 326 unsigned int gf_hi = data[offset + 0x3B]; 327 unsigned int gf_lo = data[offset + 0x3C]; 328 329 // Time (seconds). 330 if (timestamp < time) { 331 ERROR (abstract->context, "Timestamp moved backwards (%u %u).", timestamp, time); 332 return DC_STATUS_DATAFORMAT; 333 } 334 time = timestamp; 335 sample.time = time * 1000; 336 if (callback) callback (DC_SAMPLE_TIME, &sample, userdata); 337 338 // Depth (1/100 m). 339 sample.depth = depth / 100.0; 340 if (callback) callback (DC_SAMPLE_DEPTH, &sample, userdata); 341 342 // Temperature (1/100 °C). 343 sample.temperature = temperature / 100.0; 344 if (callback) callback (DC_SAMPLE_TEMPERATURE, &sample, userdata); 345 346 // Gas mix 347 if (o2 != o2_previous) { 348 // Find the gasmix in the list. 349 unsigned int idx = 0; 350 while (idx < ngasmixes) { 351 if (o2 == oxygen[idx]) 352 break; 353 idx++; 354 } 355 356 // Add it to list if not found. 357 if (idx >= ngasmixes) { 358 if (idx >= NGASMIXES) { 359 ERROR (abstract->context, "Maximum number of gas mixes reached."); 360 return DC_STATUS_DATAFORMAT; 361 } 362 oxygen[idx] = o2; 363 ngasmixes = idx + 1; 364 } 365 366 sample.gasmix = idx; 367 if (callback) callback(DC_SAMPLE_GASMIX, &sample, userdata); 368 o2_previous = o2; 369 } 370 371 // NDL / Deco 372 if (decodepth) { 373 sample.deco.type = DC_DECO_DECOSTOP; 374 sample.deco.time = decotime; 375 sample.deco.depth = decodepth / 100.0; 376 } else { 377 sample.deco.type = DC_DECO_NDL; 378 sample.deco.time = ndl_tts; 379 sample.deco.depth = 0; 380 } 381 sample.deco.tts = 0; 382 if (callback) callback (DC_SAMPLE_DECO, &sample, userdata); 383 384 // CNS 385 sample.cns = cns / 100.0; 386 if (callback) callback (DC_SAMPLE_CNS, &sample, userdata); 387 388 // Tank pressure 389 if (pressure && parser->model == TABLET) { 390 sample.pressure.tank = 0; 391 sample.pressure.value = pressure; 392 if (callback) callback (DC_SAMPLE_PRESSURE, &sample, userdata); 393 } 394 395 // Deco model 396 if (gf_low == 0 && gf_high == 0) { 397 gf_low = gf_lo; 398 gf_high = gf_hi; 399 } 400 401 offset += SZ_SAMPLE; 402 } 403 404 // Cache the data for later use. 405 for (unsigned int i = 0; i < ngasmixes; ++i) { 406 parser->oxygen[i] = oxygen[i]; 407 } 408 parser->ngasmixes = ngasmixes; 409 parser->gf_low = gf_low; 410 parser->gf_high = gf_high; 411 parser->cached = 1; 412 413 return DC_STATUS_SUCCESS; 414 }