subrascasse

Sub Rascasse: an iPhone dive log for Subsurface cloud storage, with a pure-Swift git client (GPL-2.0)
git clone https://git.schurger.org/subrascasse.git
Log | Files | Refs | README | LICENSE

deepblu_cosmiq_parser.c (6454B)


      1 /*
      2  * libdivecomputer
      3  *
      4  * Copyright (C) 2019 Linus Torvalds
      5  * Copyright (C) 2022 Jef Driesen
      6  *
      7  * This library is free software; you can redistribute it and/or
      8  * modify it under the terms of the GNU Lesser General Public
      9  * License as published by the Free Software Foundation; either
     10  * version 2.1 of the License, or (at your option) any later version.
     11  *
     12  * This library is distributed in the hope that it will be useful,
     13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     15  * Lesser General Public License for more details.
     16  *
     17  * You should have received a copy of the GNU Lesser General Public
     18  * License along with this library; if not, write to the Free Software
     19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
     20  * MA 02110-1301 USA
     21  */
     22 
     23 #include <stdlib.h>
     24 
     25 #include <libdivecomputer/units.h>
     26 
     27 #include "deepblu_cosmiq.h"
     28 #include "context-private.h"
     29 #include "parser-private.h"
     30 #include "array.h"
     31 
     32 #define SCUBA    2
     33 #define GAUGE    3
     34 #define FREEDIVE 4
     35 
     36 #define SZ_HEADER 36
     37 #define SZ_SAMPLE 4
     38 
     39 typedef struct deepblu_cosmiq_parser_t {
     40 	dc_parser_t base;
     41 	double hydrostatic;
     42 } deepblu_cosmiq_parser_t;
     43 
     44 static dc_status_t deepblu_cosmiq_parser_set_density (dc_parser_t *abstract, double density);
     45 static dc_status_t deepblu_cosmiq_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime);
     46 static dc_status_t deepblu_cosmiq_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value);
     47 static dc_status_t deepblu_cosmiq_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata);
     48 
     49 static const dc_parser_vtable_t deepblu_cosmiq_parser_vtable = {
     50 	sizeof(deepblu_cosmiq_parser_t),
     51 	DC_FAMILY_DEEPBLU_COSMIQ,
     52 	NULL, /* set_clock */
     53 	NULL, /* set_atmospheric */
     54 	deepblu_cosmiq_parser_set_density, /* set_density */
     55 	deepblu_cosmiq_parser_get_datetime, /* datetime */
     56 	deepblu_cosmiq_parser_get_field, /* fields */
     57 	deepblu_cosmiq_parser_samples_foreach, /* samples_foreach */
     58 	NULL /* destroy */
     59 };
     60 
     61 dc_status_t
     62 deepblu_cosmiq_parser_create (dc_parser_t **out, dc_context_t *context, const unsigned char data[], size_t size)
     63 {
     64 	deepblu_cosmiq_parser_t *parser = NULL;
     65 
     66 	if (out == NULL)
     67 		return DC_STATUS_INVALIDARGS;
     68 
     69 	// Allocate memory.
     70 	parser = (deepblu_cosmiq_parser_t *) dc_parser_allocate (context, &deepblu_cosmiq_parser_vtable, data, size);
     71 	if (parser == NULL) {
     72 		ERROR (context, "Failed to allocate memory.");
     73 		return DC_STATUS_NOMEMORY;
     74 	}
     75 
     76 	// Set the default values.
     77 	parser->hydrostatic = DEF_DENSITY_SALT * GRAVITY;
     78 
     79 	*out = (dc_parser_t *) parser;
     80 
     81 	return DC_STATUS_SUCCESS;
     82 }
     83 
     84 static dc_status_t
     85 deepblu_cosmiq_parser_set_density (dc_parser_t *abstract, double density)
     86 {
     87 	deepblu_cosmiq_parser_t *parser = (deepblu_cosmiq_parser_t *) abstract;
     88 
     89 	parser->hydrostatic = density * GRAVITY;
     90 
     91 	return DC_STATUS_SUCCESS;
     92 }
     93 
     94 static dc_status_t
     95 deepblu_cosmiq_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime)
     96 {
     97 	const unsigned char *data = abstract->data;
     98 	unsigned int size = abstract->size;
     99 
    100 	if (size < SZ_HEADER)
    101 		return DC_STATUS_DATAFORMAT;
    102 
    103 	if (datetime) {
    104 		datetime->year = array_uint16_le(data + 6);
    105 		datetime->day = data[8];
    106 		datetime->month = data[9];
    107 		datetime->minute = data[10];
    108 		datetime->hour = data[11];
    109 		datetime->second = 0;
    110 		datetime->timezone = DC_TIMEZONE_NONE;
    111 	}
    112 
    113 	return DC_STATUS_SUCCESS;
    114 }
    115 
    116 static dc_status_t
    117 deepblu_cosmiq_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value)
    118 {
    119 	deepblu_cosmiq_parser_t *parser = (deepblu_cosmiq_parser_t *) abstract;
    120 	const unsigned char *data = abstract->data;
    121 	unsigned int size = abstract->size;
    122 
    123 	dc_gasmix_t *gasmix = (dc_gasmix_t *) value;
    124 
    125 	if (size < SZ_HEADER)
    126 		return DC_STATUS_DATAFORMAT;
    127 
    128 	unsigned int mode = data[2];
    129 	unsigned int atmospheric = array_uint16_le (data + 4) & 0x1FFF;
    130 
    131 	if (value) {
    132 		switch (type) {
    133 		case DC_FIELD_DIVETIME:
    134 			if (mode == SCUBA || mode == GAUGE)
    135 				*((unsigned int *) value) = array_uint16_le(data + 12) * 60;
    136 			else
    137 				*((unsigned int *) value) = array_uint16_le(data + 12);
    138 			break;
    139 		case DC_FIELD_MAXDEPTH:
    140 			*((double *) value) = (signed int) (array_uint16_le (data + 22) - atmospheric) * (BAR / 1000.0) / parser->hydrostatic;
    141 			break;
    142 		case DC_FIELD_GASMIX_COUNT:
    143 			*((unsigned int *) value) = mode == SCUBA;
    144 			break;
    145 		case DC_FIELD_GASMIX:
    146 			gasmix->usage = DC_USAGE_NONE;
    147 			gasmix->oxygen = data[3] / 100.0;
    148 			gasmix->helium = 0.0;
    149 			gasmix->nitrogen = 1.0 - gasmix->oxygen - gasmix->helium;
    150 			break;
    151 		case DC_FIELD_DIVEMODE:
    152 			switch (mode) {
    153 			case SCUBA:
    154 				*((dc_divemode_t *) value) = DC_DIVEMODE_OC;
    155 				break;
    156 			case GAUGE:
    157 				*((dc_divemode_t *) value) = DC_DIVEMODE_GAUGE;
    158 				break;
    159 			case FREEDIVE:
    160 				*((dc_divemode_t *) value) = DC_DIVEMODE_FREEDIVE;
    161 				break;
    162 			default:
    163 				ERROR (abstract->context, "Unknown activity type '%02x'", mode);
    164 				return DC_STATUS_DATAFORMAT;
    165 			}
    166 			break;
    167 		case DC_FIELD_ATMOSPHERIC:
    168 			*((double *) value) = atmospheric / 1000.0;
    169 			break;
    170 		default:
    171 			return DC_STATUS_UNSUPPORTED;
    172 		}
    173 	}
    174 
    175 	return DC_STATUS_SUCCESS;
    176 }
    177 
    178 static dc_status_t
    179 deepblu_cosmiq_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata)
    180 {
    181 	deepblu_cosmiq_parser_t *parser = (deepblu_cosmiq_parser_t *) abstract;
    182 	const unsigned char *data = abstract->data;
    183 	unsigned int size = abstract->size;
    184 
    185 	if (size < SZ_HEADER)
    186 		return DC_STATUS_DATAFORMAT;
    187 
    188 	unsigned int interval = data[26];
    189 	unsigned int atmospheric = array_uint16_le (data + 4) & 0x1FFF;
    190 
    191 	unsigned int time = 0;
    192 	unsigned int offset = SZ_HEADER;
    193 	while (offset + SZ_SAMPLE <= size) {
    194 		// Ignore empty samples.
    195 		if (array_isequal (data + offset, SZ_SAMPLE, 0xFF)) {
    196 			offset += SZ_SAMPLE;
    197 			continue;
    198 		}
    199 
    200 		dc_sample_value_t sample = {0};
    201 		unsigned int temperature = array_uint16_le(data + offset + 0);
    202 		unsigned int depth = array_uint16_le(data + offset + 2);
    203 		offset += SZ_SAMPLE;
    204 
    205 		time += interval;
    206 		sample.time = time * 1000;
    207 		if (callback) callback (DC_SAMPLE_TIME, &sample, userdata);
    208 
    209 		sample.depth = (signed int) (depth - atmospheric) * (BAR / 1000.0) / parser->hydrostatic;
    210 		if (callback) callback (DC_SAMPLE_DEPTH, &sample, userdata);
    211 
    212 		sample.temperature = temperature / 10.0;
    213 		if (callback) callback (DC_SAMPLE_TEMPERATURE, &sample, userdata);
    214 	}
    215 
    216 	return DC_STATUS_SUCCESS;
    217 }