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

suunto_solution_parser.c (6279B)


      1 /*
      2  * libdivecomputer
      3  *
      4  * Copyright (C) 2008 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 "suunto_solution.h"
     27 #include "context-private.h"
     28 #include "parser-private.h"
     29 
     30 #define ISINSTANCE(parser) dc_parser_isinstance((parser), &suunto_solution_parser_vtable)
     31 
     32 typedef struct suunto_solution_parser_t suunto_solution_parser_t;
     33 
     34 struct suunto_solution_parser_t {
     35 	dc_parser_t base;
     36 	// Cached fields.
     37 	unsigned int cached;
     38 	unsigned int divetime;
     39 	unsigned int maxdepth;
     40 };
     41 
     42 static dc_status_t suunto_solution_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value);
     43 static dc_status_t suunto_solution_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata);
     44 
     45 static const dc_parser_vtable_t suunto_solution_parser_vtable = {
     46 	sizeof(suunto_solution_parser_t),
     47 	DC_FAMILY_SUUNTO_SOLUTION,
     48 	NULL, /* set_clock */
     49 	NULL, /* set_atmospheric */
     50 	NULL, /* set_density */
     51 	NULL, /* datetime */
     52 	suunto_solution_parser_get_field, /* fields */
     53 	suunto_solution_parser_samples_foreach, /* samples_foreach */
     54 	NULL /* destroy */
     55 };
     56 
     57 
     58 dc_status_t
     59 suunto_solution_parser_create (dc_parser_t **out, dc_context_t *context, const unsigned char data[], size_t size)
     60 {
     61 	suunto_solution_parser_t *parser = NULL;
     62 
     63 	if (out == NULL)
     64 		return DC_STATUS_INVALIDARGS;
     65 
     66 	// Allocate memory.
     67 	parser = (suunto_solution_parser_t *) dc_parser_allocate (context, &suunto_solution_parser_vtable, data, size);
     68 	if (parser == NULL) {
     69 		ERROR (context, "Failed to allocate memory.");
     70 		return DC_STATUS_NOMEMORY;
     71 	}
     72 
     73 	// Set the default values.
     74 	parser->cached = 0;
     75 	parser->divetime = 0;
     76 	parser->maxdepth = 0;
     77 
     78 	*out = (dc_parser_t*) parser;
     79 
     80 	return DC_STATUS_SUCCESS;
     81 }
     82 
     83 
     84 static dc_status_t
     85 suunto_solution_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value)
     86 {
     87 	suunto_solution_parser_t *parser = (suunto_solution_parser_t *) abstract;
     88 
     89 	const unsigned char *data = abstract->data;
     90 	unsigned int size = abstract->size;
     91 
     92 	if (size < 4)
     93 		return DC_STATUS_DATAFORMAT;
     94 
     95 	if (!parser->cached) {
     96 		unsigned int nsamples = 0;
     97 		unsigned int depth = 0, maxdepth = 0;
     98 		unsigned int offset = 3;
     99 		while (offset < size && data[offset] != 0x80) {
    100 			unsigned char raw = data[offset++];
    101 			if (raw < 0x7e || raw > 0x82) {
    102 				depth += (signed char) raw;
    103 				if (raw == 0x7D || raw == 0x83) {
    104 					if (offset + 1 > size)
    105 						return DC_STATUS_DATAFORMAT;
    106 					depth += (signed char) data[offset++];
    107 				}
    108 				if (depth > maxdepth)
    109 					maxdepth = depth;
    110 				nsamples++;
    111 			}
    112 		}
    113 
    114 		// Store the offset to the end marker.
    115 		unsigned int marker = offset;
    116 		if (marker + 1 >= size || data[marker] != 0x80)
    117 			return DC_STATUS_DATAFORMAT;
    118 
    119 		parser->cached = 1;
    120 		parser->divetime = (nsamples * 3 + data[marker + 1]) * 60;
    121 		parser->maxdepth = maxdepth;
    122 	}
    123 
    124 	dc_gasmix_t *gasmix = (dc_gasmix_t *) value;
    125 
    126 	if (value) {
    127 		switch (type) {
    128 		case DC_FIELD_DIVETIME:
    129 			*((unsigned int *) value) = parser->divetime;
    130 			break;
    131 		case DC_FIELD_MAXDEPTH:
    132 			*((double *) value) = parser->maxdepth * FEET;
    133 			break;
    134 		case DC_FIELD_GASMIX_COUNT:
    135 			*((unsigned int *) value) = 1;
    136 			break;
    137 		case DC_FIELD_GASMIX:
    138 			gasmix->usage = DC_USAGE_NONE;
    139 			gasmix->helium = 0.0;
    140 			gasmix->oxygen = 0.21;
    141 			gasmix->nitrogen = 1.0 - gasmix->oxygen - gasmix->helium;
    142 			break;
    143 		default:
    144 			return DC_STATUS_UNSUPPORTED;
    145 		}
    146 	}
    147 
    148 	return DC_STATUS_SUCCESS;
    149 }
    150 
    151 static dc_status_t
    152 suunto_solution_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata)
    153 {
    154 	const unsigned char *data = abstract->data;
    155 	unsigned int size = abstract->size;
    156 
    157 	if (size < 4)
    158 		return DC_STATUS_DATAFORMAT;
    159 
    160 	unsigned int time = 0, depth = 0;
    161 	unsigned int gasmix_previous = 0xFFFFFFFF;
    162 	unsigned int gasmix = 0;
    163 
    164 	unsigned int offset = 3;
    165 	while (offset < size &&	data[offset] != 0x80) {
    166 		dc_sample_value_t sample = {0};
    167 		unsigned char value = data[offset++];
    168 		if (value < 0x7e || value > 0x82) {
    169 			// Time (minutes).
    170 			time += 3 * 60;
    171 			sample.time = time * 1000;
    172 			if (callback) callback (DC_SAMPLE_TIME, &sample, userdata);
    173 
    174 			// Depth (ft).
    175 			depth += (signed char) value;
    176 			if (value == 0x7D || value == 0x83) {
    177 				// A value of 0x7D (125) or 0x83 (-125) indicates a descent
    178 				// or ascent greater than 124 feet. The remaining part of
    179 				// the total delta value is stored in the next byte.
    180 				if (offset + 1 > size)
    181 					return DC_STATUS_DATAFORMAT;
    182 				depth += (signed char) data[offset++];
    183 			}
    184 			sample.depth = depth * FEET;
    185 			if (callback) callback (DC_SAMPLE_DEPTH, &sample, userdata);
    186 
    187 			// Gas change.
    188 			if (gasmix != gasmix_previous) {
    189 				sample.gasmix = gasmix;
    190 				if (callback) callback (DC_SAMPLE_GASMIX, &sample, userdata);
    191 				gasmix_previous = gasmix;
    192 			}
    193 		} else {
    194 			// Event.
    195 			sample.event.type = SAMPLE_EVENT_NONE;
    196 			sample.event.time = 0;
    197 			sample.event.flags = 0;
    198 			sample.event.value = 0;
    199 			switch (value) {
    200 			case 0x7e: // Deco, ASC
    201 				sample.event.type = SAMPLE_EVENT_DECOSTOP;
    202 				break;
    203 			case 0x7f: // Ceiling, ERR
    204 				sample.event.type = SAMPLE_EVENT_CEILING;
    205 				break;
    206 			case 0x81: // Slow
    207 				sample.event.type = SAMPLE_EVENT_ASCENT;
    208 				break;
    209 			default: // Unknown
    210 				WARNING (abstract->context, "Unknown event");
    211 				break;
    212 			}
    213 
    214 			if (sample.event.type != SAMPLE_EVENT_NONE) {
    215 				if (callback) callback (DC_SAMPLE_EVENT, &sample, userdata);
    216 			}
    217 		}
    218 	}
    219 
    220 	if (data[offset] != 0x80)
    221 		return DC_STATUS_DATAFORMAT;
    222 
    223 	return DC_STATUS_SUCCESS;
    224 }