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

mares_darwin_parser.c (7948B)


      1 /*
      2  * libdivecomputer
      3  *
      4  * Copyright (C) 2011 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 #include <string.h>
     24 
     25 #include <libdivecomputer/units.h>
     26 
     27 #include "mares_darwin.h"
     28 #include "context-private.h"
     29 #include "parser-private.h"
     30 #include "array.h"
     31 
     32 #define ISINSTANCE(parser) dc_parser_isinstance((parser), &mares_darwin_parser_vtable)
     33 
     34 #define DARWIN    0
     35 #define DARWINAIR 1
     36 
     37 #define AIR    0
     38 #define GAUGE  1
     39 #define NITROX 2
     40 
     41 typedef struct mares_darwin_parser_t mares_darwin_parser_t;
     42 
     43 struct mares_darwin_parser_t {
     44 	dc_parser_t base;
     45 	unsigned int model;
     46 	unsigned int headersize;
     47 	unsigned int samplesize;
     48 };
     49 
     50 static dc_status_t mares_darwin_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime);
     51 static dc_status_t mares_darwin_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value);
     52 static dc_status_t mares_darwin_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata);
     53 
     54 static const dc_parser_vtable_t mares_darwin_parser_vtable = {
     55 	sizeof(mares_darwin_parser_t),
     56 	DC_FAMILY_MARES_DARWIN,
     57 	NULL, /* set_clock */
     58 	NULL, /* set_atmospheric */
     59 	NULL, /* set_density */
     60 	mares_darwin_parser_get_datetime, /* datetime */
     61 	mares_darwin_parser_get_field, /* fields */
     62 	mares_darwin_parser_samples_foreach, /* samples_foreach */
     63 	NULL /* destroy */
     64 };
     65 
     66 
     67 dc_status_t
     68 mares_darwin_parser_create (dc_parser_t **out, dc_context_t *context, const unsigned char data[], size_t size, unsigned int model)
     69 {
     70 	mares_darwin_parser_t *parser = NULL;
     71 
     72 	if (out == NULL)
     73 		return DC_STATUS_INVALIDARGS;
     74 
     75 	// Allocate memory.
     76 	parser = (mares_darwin_parser_t *) dc_parser_allocate (context, &mares_darwin_parser_vtable, data, size);
     77 	if (parser == NULL) {
     78 		ERROR (context, "Failed to allocate memory.");
     79 		return DC_STATUS_NOMEMORY;
     80 	}
     81 
     82 	parser->model = model;
     83 
     84 	if (model == DARWINAIR) {
     85 		parser->headersize = 60;
     86 		parser->samplesize = 3;
     87 	} else {
     88 		parser->headersize = 52;
     89 		parser->samplesize = 2;
     90 	}
     91 
     92 	*out = (dc_parser_t *) parser;
     93 
     94 	return DC_STATUS_SUCCESS;
     95 }
     96 
     97 
     98 static dc_status_t
     99 mares_darwin_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime)
    100 {
    101 	mares_darwin_parser_t *parser = (mares_darwin_parser_t *) abstract;
    102 
    103 	if (abstract->size < parser->headersize)
    104 		return DC_STATUS_DATAFORMAT;
    105 
    106 	const unsigned char *p = abstract->data;
    107 
    108 	if (datetime) {
    109 		datetime->year   = array_uint16_be (p);
    110 		datetime->month  = p[2];
    111 		datetime->day    = p[3];
    112 		datetime->hour   = p[4];
    113 		datetime->minute = p[5];
    114 		datetime->second = 0;
    115 		datetime->timezone = DC_TIMEZONE_NONE;
    116 	}
    117 
    118 	return DC_STATUS_SUCCESS;
    119 }
    120 
    121 
    122 static dc_status_t
    123 mares_darwin_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value)
    124 {
    125 	mares_darwin_parser_t *parser = (mares_darwin_parser_t *) abstract;
    126 
    127 	if (abstract->size < parser->headersize)
    128 		return DC_STATUS_DATAFORMAT;
    129 
    130 	const unsigned char *p = abstract->data;
    131 
    132 	dc_gasmix_t *gasmix = (dc_gasmix_t *) value;
    133 	dc_tank_t *tank = (dc_tank_t *) value;
    134 
    135 	unsigned int mode = p[0x0C] & 0x03;
    136 
    137 	if (value) {
    138 		switch (type) {
    139 		case DC_FIELD_DIVETIME:
    140 			*((unsigned int *) value) = array_uint16_be (p + 0x06) * 20;
    141 			break;
    142 		case DC_FIELD_MAXDEPTH:
    143 			*((double *) value) = array_uint16_be (p + 0x08) / 10.0;
    144 			break;
    145 		case DC_FIELD_GASMIX_COUNT:
    146 			if (mode == GAUGE) {
    147 				*((unsigned int *) value) = 0;
    148 			} else {
    149 				*((unsigned int *) value) = 1;
    150 			}
    151 			break;
    152 		case DC_FIELD_GASMIX:
    153 			gasmix->usage = DC_USAGE_NONE;
    154 			gasmix->helium = 0.0;
    155 			if (mode == NITROX) {
    156 				gasmix->oxygen = p[0x0E] / 100.0;
    157 			} else {
    158 				gasmix->oxygen = 0.21;
    159 			}
    160 			gasmix->nitrogen = 1.0 - gasmix->oxygen - gasmix->helium;
    161 			break;
    162 		case DC_FIELD_TEMPERATURE_MINIMUM:
    163 			*((double *) value) = (signed char) p[0x0A];
    164 			break;
    165 		case DC_FIELD_TANK_COUNT:
    166 			if (parser->model == DARWINAIR) {
    167 				*((unsigned int *) value) = 1;
    168 			} else {
    169 				*((unsigned int *) value) = 0;
    170 			}
    171 			break;
    172 		case DC_FIELD_TANK:
    173 			if (parser->model == DARWINAIR) {
    174 				tank->type = DC_TANKVOLUME_METRIC;
    175 				tank->volume = p[0x13] / 10.0;
    176 				tank->workpressure = 0.0;
    177 				tank->gasmix = 0;
    178 				tank->beginpressure = array_uint16_be (p + 0x17);
    179 				tank->endpressure = array_uint16_be (p + 0x19);
    180 				tank->usage = DC_TANK_USAGE_NONE;
    181 			} else {
    182 				return DC_STATUS_UNSUPPORTED;
    183 			}
    184 			break;
    185 		case DC_FIELD_DIVEMODE:
    186 			switch (mode) {
    187 			case AIR:
    188 			case NITROX:
    189 				*((dc_divemode_t *) value) = DC_DIVEMODE_OC;
    190 				break;
    191 			case GAUGE:
    192 				*((dc_divemode_t *) value) = DC_DIVEMODE_GAUGE;
    193 				break;
    194 			default:
    195 				return DC_STATUS_DATAFORMAT;
    196 			}
    197 			break;
    198 		default:
    199 			return DC_STATUS_UNSUPPORTED;
    200 		}
    201 	}
    202 
    203 	return DC_STATUS_SUCCESS;
    204 }
    205 
    206 
    207 static dc_status_t
    208 mares_darwin_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata)
    209 {
    210 	mares_darwin_parser_t *parser = (mares_darwin_parser_t *) abstract;
    211 
    212 	if (abstract->size < parser->headersize)
    213 		return DC_STATUS_DATAFORMAT;
    214 
    215 	unsigned int time = 0;
    216 
    217 	unsigned int mode = abstract->data[0x0C] & 0x03;
    218 	unsigned int pressure = array_uint16_be (abstract->data + 0x17);
    219 
    220 	unsigned int gasmix_previous = 0xFFFFFFFF;
    221 	unsigned int gasmix = gasmix_previous;
    222 	if (mode != GAUGE) {
    223 		gasmix = 0;
    224 	}
    225 
    226 	unsigned int offset = parser->headersize;
    227 	while (offset + parser->samplesize <= abstract->size) {
    228 			dc_sample_value_t sample = {0};
    229 
    230 			unsigned int value = array_uint16_le (abstract->data + offset);
    231 			unsigned int depth = value & 0x07FF;
    232 			unsigned int ascent = (value & 0xE000) >> 13;
    233 			unsigned int violation = (value & 0x1000) >> 12;
    234 			unsigned int deco = (value & 0x0800) >> 11;
    235 
    236 			// Surface Time (seconds).
    237 			time += 20;
    238 			sample.time = time * 1000;
    239 			if (callback) callback (DC_SAMPLE_TIME, &sample, userdata);
    240 
    241 			// Depth (1/10 m).
    242 			sample.depth = depth / 10.0;
    243 			if (callback) callback (DC_SAMPLE_DEPTH, &sample, userdata);
    244 
    245 			// Gas change.
    246 			if (gasmix != gasmix_previous) {
    247 				sample.gasmix = gasmix;
    248 				if (callback) callback (DC_SAMPLE_GASMIX, &sample, userdata);
    249 				gasmix_previous = gasmix;
    250 			}
    251 
    252 			// Ascent rate
    253 			if (ascent) {
    254 				sample.event.type = SAMPLE_EVENT_ASCENT;
    255 				sample.event.time = 0;
    256 				sample.event.flags = 0;
    257 				sample.event.value = ascent;
    258 				if (callback) callback (DC_SAMPLE_EVENT, &sample, userdata);
    259 			}
    260 
    261 			// Deco violation
    262 			if (violation) {
    263 				sample.event.type = SAMPLE_EVENT_CEILING;
    264 				sample.event.time = 0;
    265 				sample.event.flags = 0;
    266 				sample.event.value = 0;
    267 				if (callback) callback (DC_SAMPLE_EVENT, &sample, userdata);
    268 			}
    269 
    270 			// Deco stop
    271 			if (deco) {
    272 				sample.deco.type = DC_DECO_DECOSTOP;
    273 			} else {
    274 				sample.deco.type = DC_DECO_NDL;
    275 			}
    276 			sample.deco.time = 0;
    277 			sample.deco.depth = 0.0;
    278 			sample.deco.tts = 0;
    279 			if (callback) callback (DC_SAMPLE_DECO, &sample, userdata);
    280 
    281 			if (parser->samplesize == 3) {
    282 				unsigned int type = (time / 20 + 2) % 3;
    283 				if (type == 0) {
    284 					// Tank Pressure (bar)
    285 					pressure -= abstract->data[offset + 2];
    286 					sample.pressure.tank = 0;
    287 					sample.pressure.value = pressure;
    288 					if (callback) callback (DC_SAMPLE_PRESSURE, &sample, userdata);
    289 				}
    290 			}
    291 
    292 			offset += parser->samplesize;
    293 	}
    294 
    295 	return DC_STATUS_SUCCESS;
    296 }