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

oceans_s1_parser.c (8764B)


      1 /*
      2  * libdivecomputer
      3  *
      4  * Copyright (C) 2020 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 <string.h>
     24 #include <stdlib.h>
     25 #include <stdio.h>
     26 
     27 #include "oceans_s1.h"
     28 #include "oceans_s1_common.h"
     29 #include "context-private.h"
     30 #include "parser-private.h"
     31 #include "platform.h"
     32 #include "array.h"
     33 
     34 #define SCUBA 0
     35 #define APNEA 1
     36 
     37 #define EVENT_DIVE_STARTED  0x0001
     38 #define EVENT_DIVE_ENDED    0x0002
     39 #define EVENT_DIVE_RESUMED  0x0004
     40 #define EVENT_PING_SENT     0x0008
     41 #define EVENT_PING_RECEIVED 0x0010
     42 #define EVENT_DECO_STOP     0x0020
     43 #define EVENT_SAFETY_STOP   0x0040
     44 #define EVENT_BATTERY_LOW   0x0080
     45 #define EVENT_BACKLIGHT_ON  0x0100
     46 
     47 typedef struct oceans_s1_parser_t oceans_s1_parser_t;
     48 
     49 struct oceans_s1_parser_t {
     50 	dc_parser_t base;
     51 	// Cached fields.
     52 	dc_ticks_t timestamp;
     53 	unsigned int cached;
     54 	unsigned int number;
     55 	unsigned int divemode;
     56 	unsigned int oxygen;
     57 	unsigned int maxdepth;
     58 	unsigned int divetime;
     59 };
     60 
     61 static dc_status_t oceans_s1_parser_get_datetime(dc_parser_t *abstract, dc_datetime_t *datetime);
     62 static dc_status_t oceans_s1_parser_get_field(dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value);
     63 static dc_status_t oceans_s1_parser_samples_foreach(dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata);
     64 
     65 static const dc_parser_vtable_t oceans_s1_parser_vtable = {
     66 	sizeof(oceans_s1_parser_t),
     67 	DC_FAMILY_OCEANS_S1,
     68 	NULL, /* set_clock */
     69 	NULL, /* set_atmospheric */
     70 	NULL, /* set_density */
     71 	oceans_s1_parser_get_datetime, /* datetime */
     72 	oceans_s1_parser_get_field, /* fields */
     73 	oceans_s1_parser_samples_foreach, /* samples_foreach */
     74 	NULL /* destroy */
     75 };
     76 
     77 dc_status_t
     78 oceans_s1_parser_create (dc_parser_t **out, dc_context_t *context, const unsigned char data[], size_t size)
     79 {
     80 	oceans_s1_parser_t *parser = NULL;
     81 
     82 	if (out == NULL)
     83 		return DC_STATUS_INVALIDARGS;
     84 
     85 	// Allocate memory.
     86 	parser = (oceans_s1_parser_t *) dc_parser_allocate (context, &oceans_s1_parser_vtable, data, size);
     87 	if (parser == NULL) {
     88 		ERROR (context, "Failed to allocate memory.");
     89 		return DC_STATUS_NOMEMORY;
     90 	}
     91 
     92 	// Set the default values.
     93 	parser->cached = 0;
     94 	parser->timestamp = 0;
     95 	parser->number = 0;
     96 	parser->divemode = 0;
     97 	parser->oxygen = 0;
     98 	parser->maxdepth = 0;
     99 	parser->divetime = 0;
    100 
    101 	*out = (dc_parser_t *) parser;
    102 
    103 	return DC_STATUS_SUCCESS;
    104 }
    105 
    106 static dc_status_t
    107 oceans_s1_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime)
    108 {
    109 	oceans_s1_parser_t *parser = (oceans_s1_parser_t *) abstract;
    110 
    111 	if (!parser->cached) {
    112 		dc_status_t status = oceans_s1_parser_samples_foreach (abstract, NULL, NULL);
    113 		if (status != DC_STATUS_SUCCESS)
    114 			return status;
    115 	}
    116 
    117 	if (!dc_datetime_gmtime (datetime, parser->timestamp))
    118 		return DC_STATUS_DATAFORMAT;
    119 
    120 	datetime->timezone = DC_TIMEZONE_NONE;
    121 
    122 	return DC_STATUS_SUCCESS;
    123 }
    124 
    125 static dc_status_t
    126 oceans_s1_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value)
    127 {
    128 	oceans_s1_parser_t *parser = (oceans_s1_parser_t *) abstract;
    129 
    130 	if (!parser->cached) {
    131 		dc_status_t status = oceans_s1_parser_samples_foreach (abstract, NULL, NULL);
    132 		if (status != DC_STATUS_SUCCESS)
    133 			return status;
    134 	}
    135 
    136 	dc_gasmix_t *gasmix = (dc_gasmix_t *) value;
    137 
    138 	if (value) {
    139 		switch (type) {
    140 		case DC_FIELD_DIVETIME:
    141 			*((unsigned int *) value) = parser->divetime;
    142 			break;
    143 		case DC_FIELD_MAXDEPTH:
    144 			*((double *) value) = parser->maxdepth / 100.0;
    145 			break;
    146 		case DC_FIELD_GASMIX_COUNT:
    147 			*((unsigned int *) value) = parser->divemode == SCUBA;
    148 			break;
    149 		case DC_FIELD_GASMIX:
    150 			gasmix->usage = DC_USAGE_NONE;
    151 			gasmix->helium = 0.0;
    152 			gasmix->oxygen = parser->oxygen / 100.0;
    153 			gasmix->nitrogen = 1.0 - gasmix->oxygen - gasmix->helium;
    154 			break;
    155 		case DC_FIELD_DIVEMODE:
    156 			switch (parser->divemode) {
    157 			case SCUBA:
    158 				*((dc_divemode_t *) value) = DC_DIVEMODE_OC;
    159 				break;
    160 			case APNEA:
    161 				*((dc_divemode_t *) value) = DC_DIVEMODE_FREEDIVE;
    162 				break;
    163 			default:
    164 				return DC_STATUS_DATAFORMAT;
    165 			}
    166 			break;
    167 		default:
    168 			return DC_STATUS_UNSUPPORTED;
    169 		}
    170 	}
    171 
    172 	return DC_STATUS_SUCCESS;
    173 }
    174 
    175 static dc_status_t
    176 oceans_s1_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata)
    177 {
    178 	dc_status_t status = DC_STATUS_SUCCESS;
    179 	oceans_s1_parser_t *parser = (oceans_s1_parser_t *) abstract;
    180 	const unsigned char *data = abstract->data;
    181 	size_t size = abstract->size;
    182 
    183 	dc_ticks_t timestamp = 0;
    184 	unsigned int number = 0, divemode = 0, oxygen = 0;
    185 	unsigned int maxdepth = 0, divetime = 0;
    186 	unsigned int interval = 10;
    187 	unsigned int time = 0;
    188 
    189 	char *ptr = NULL;
    190 	size_t len = 0;
    191 	int n = 0;
    192 	while ((n = oceans_s1_getline (&ptr, &len, &data, &size)) != -1) {
    193 		dc_sample_value_t sample = {0};
    194 
    195 		// Ignore empty lines.
    196 		if (n == 0)
    197 			continue;
    198 
    199 		// Ignore leading whitespace.
    200 		const char *line = ptr;
    201 		while (*line == ' ')
    202 			line++;
    203 
    204 		if (strncmp (line, "divelog", 7) == 0) {
    205 			if (sscanf (line, "divelog v1,%us/sample", &interval) != 1) {
    206 				ERROR (parser->base.context, "Failed to parse the line '%s'.", line);
    207 				status = DC_STATUS_DATAFORMAT;
    208 				goto error_free;
    209 			}
    210 			if (interval == 0) {
    211 				ERROR (parser->base.context, "Invalid sample interval (%u).", interval);
    212 				status = DC_STATUS_DATAFORMAT;
    213 				goto error_free;
    214 			}
    215 		} else if (strncmp (line, "dive", 4) == 0) {
    216 			if (sscanf (line, "dive %u,%u,%u," DC_FORMAT_INT64, &number, &divemode, &oxygen, &timestamp) != 4) {
    217 				ERROR (parser->base.context, "Failed to parse the line '%s'.", line);
    218 				status = DC_STATUS_DATAFORMAT;
    219 				goto error_free;
    220 			}
    221 		} else if (strncmp (line, "continue", 8) == 0) {
    222 			unsigned int depth = 0, seconds = 0;
    223 			if (sscanf (line, "continue %u,%u", &depth, &seconds) != 2) {
    224 				ERROR (parser->base.context, "Failed to parse the line '%s'.", line);
    225 				status = DC_STATUS_DATAFORMAT;
    226 				goto error_free;
    227 			}
    228 
    229 			// Create surface samples for the surface time,
    230 			// and then a depth sample at the stated depth.
    231 			unsigned int nsamples = seconds / interval;
    232 			for (unsigned int i = 0; i < nsamples; ++i) {
    233 				time += interval;
    234 				sample.time = time * 1000;
    235 				if (callback) callback (DC_SAMPLE_TIME, &sample, userdata);
    236 
    237 				sample.depth = 0;
    238 				if (callback) callback (DC_SAMPLE_DEPTH, &sample, userdata);
    239 			}
    240 
    241 			time += interval;
    242 			sample.time = time * 1000;
    243 			if (callback) callback (DC_SAMPLE_TIME, &sample, userdata);
    244 
    245 			sample.depth = depth / 100.0;
    246 			if (callback) callback (DC_SAMPLE_DEPTH, &sample, userdata);
    247 		} else if (strncmp(line, "enddive", 7) == 0) {
    248 			if (sscanf(line, "enddive %u,%u", &maxdepth, &divetime) != 2) {
    249 				ERROR (parser->base.context, "Failed to parse the line '%s'.", line);
    250 				status = DC_STATUS_DATAFORMAT;
    251 				goto error_free;
    252 			}
    253 		} else if (strncmp (line, "endlog", 6) == 0) {
    254 			// Nothing to do.
    255 		} else {
    256 			unsigned int depth = 0, events = 0;
    257 			int temperature = 0;
    258 			if (sscanf (line, "%u,%d,%u", &depth, &temperature, &events) != 3) {
    259 				ERROR (parser->base.context, "Failed to parse the line '%s'.", line);
    260 				status = DC_STATUS_DATAFORMAT;
    261 				goto error_free;
    262 			}
    263 
    264 			time += interval;
    265 			sample.time = time * 1000;
    266 			if (callback) callback (DC_SAMPLE_TIME, &sample, userdata);
    267 
    268 			sample.depth = depth / 100.0;
    269 			if (callback) callback (DC_SAMPLE_DEPTH, &sample, userdata);
    270 
    271 			sample.temperature = temperature;
    272 			if (callback) callback (DC_SAMPLE_TEMPERATURE, &sample, userdata);
    273 
    274 			if (events & EVENT_DECO_STOP) {
    275 				sample.deco.type = DC_DECO_DECOSTOP;
    276 			} else if (events & EVENT_SAFETY_STOP) {
    277 				sample.deco.type = DC_DECO_SAFETYSTOP;
    278 			} else {
    279 				sample.deco.type = DC_DECO_NDL;
    280 			}
    281 			sample.deco.depth = 0.0;
    282 			sample.deco.time = 0;
    283 			sample.deco.tts = 0;
    284 			if (callback) callback (DC_SAMPLE_DECO, &sample, userdata);
    285 		}
    286 	}
    287 
    288 	// Cache the data for later use.
    289 	parser->timestamp = timestamp;
    290 	parser->number = number;
    291 	parser->divemode = divemode;
    292 	parser->oxygen = oxygen;
    293 	parser->maxdepth = maxdepth;
    294 	parser->divetime = divetime;
    295 	parser->cached = 1;
    296 
    297 error_free:
    298 	free (ptr);
    299 	return status;
    300 }