libdivecomputer-sys 0.1.0

Unsafe bindings for libdivecomputer
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
 * libdivecomputer
 *
 * Copyright (C) 2016 Jef Driesen
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301 USA
 */

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include <libdivecomputer/units.h>

#include "output-private.h"
#include "utils.h"

static dc_status_t dctool_xml_output_write (dctool_output_t *output, dc_parser_t *parser, const unsigned char data[], unsigned int size, const unsigned char fingerprint[], unsigned int fsize);
static dc_status_t dctool_xml_output_free (dctool_output_t *output);

typedef struct dctool_xml_output_t {
	dctool_output_t base;
	FILE *ostream;
	dctool_units_t units;
} dctool_xml_output_t;

static const dctool_output_vtable_t xml_vtable = {
	sizeof(dctool_xml_output_t), /* size */
	dctool_xml_output_write, /* write */
	dctool_xml_output_free, /* free */
};

typedef struct sample_data_t {
	FILE *ostream;
	dctool_units_t units;
	unsigned int nsamples;
} sample_data_t;

static double
convert_depth (double value, dctool_units_t units)
{
	if (units == DCTOOL_UNITS_IMPERIAL) {
		return value / FEET;
	} else {
		return value;
	}
}

static double
convert_temperature (double value, dctool_units_t units)
{
	if (units == DCTOOL_UNITS_IMPERIAL) {
		return value * (9.0 / 5.0) + 32.0;
	} else {
		return value;
	}
}

static double
convert_pressure (double value, dctool_units_t units)
{
	if (units == DCTOOL_UNITS_IMPERIAL) {
		return value * BAR / PSI;
	} else {
		return value;
	}
}

static double
convert_volume (double value, dctool_units_t units)
{
	if (units == DCTOOL_UNITS_IMPERIAL) {
		return value / 1000.0 / CUFT;
	} else {
		return value;
	}
}

static void
sample_cb (dc_sample_type_t type, dc_sample_value_t value, void *userdata)
{
	static const char *events[] = {
		"none", "deco", "rbt", "ascent", "ceiling", "workload", "transmitter",
		"violation", "bookmark", "surface", "safety stop", "gaschange",
		"safety stop (voluntary)", "safety stop (mandatory)", "deepstop",
		"ceiling (safety stop)", "floor", "divetime", "maxdepth",
		"OLF", "PO2", "airtime", "rgbm", "heading", "tissue level warning",
		"gaschange2"};
	static const char *decostop[] = {
		"ndl", "safety", "deco", "deep"};

	sample_data_t *sampledata = (sample_data_t *) userdata;

	switch (type) {
	case DC_SAMPLE_TIME:
		if (sampledata->nsamples++)
			fprintf (sampledata->ostream, "</sample>\n");
		fprintf (sampledata->ostream, "<sample>\n");
		fprintf (sampledata->ostream, "   <time>%02u:%02u</time>\n", value.time / 60, value.time % 60);
		break;
	case DC_SAMPLE_DEPTH:
		fprintf (sampledata->ostream, "   <depth>%.2f</depth>\n",
			convert_depth(value.depth, sampledata->units));
		break;
	case DC_SAMPLE_PRESSURE:
		fprintf (sampledata->ostream, "   <pressure tank=\"%u\">%.2f</pressure>\n",
			value.pressure.tank,
			convert_pressure(value.pressure.value, sampledata->units));
		break;
	case DC_SAMPLE_TEMPERATURE:
		fprintf (sampledata->ostream, "   <temperature>%.2f</temperature>\n",
			convert_temperature(value.temperature, sampledata->units));
		break;
	case DC_SAMPLE_EVENT:
		if (value.event.type != SAMPLE_EVENT_GASCHANGE && value.event.type != SAMPLE_EVENT_GASCHANGE2) {
			fprintf (sampledata->ostream, "   <event type=\"%u\" time=\"%u\" flags=\"%u\" value=\"%u\">%s</event>\n",
				value.event.type, value.event.time, value.event.flags, value.event.value, events[value.event.type]);
		}
		break;
	case DC_SAMPLE_RBT:
		fprintf (sampledata->ostream, "   <rbt>%u</rbt>\n", value.rbt);
		break;
	case DC_SAMPLE_HEARTBEAT:
		fprintf (sampledata->ostream, "   <heartbeat>%u</heartbeat>\n", value.heartbeat);
		break;
	case DC_SAMPLE_BEARING:
		fprintf (sampledata->ostream, "   <bearing>%u</bearing>\n", value.bearing);
		break;
	case DC_SAMPLE_VENDOR:
		fprintf (sampledata->ostream, "   <vendor type=\"%u\" size=\"%u\">", value.vendor.type, value.vendor.size);
		for (unsigned int i = 0; i < value.vendor.size; ++i)
			fprintf (sampledata->ostream, "%02X", ((const unsigned char *) value.vendor.data)[i]);
		fprintf (sampledata->ostream, "</vendor>\n");
		break;
	case DC_SAMPLE_SETPOINT:
		fprintf (sampledata->ostream, "   <setpoint>%.2f</setpoint>\n", value.setpoint);
		break;
	case DC_SAMPLE_PPO2:
		fprintf (sampledata->ostream, "   <ppo2>%.2f</ppo2>\n", value.ppo2);
		break;
	case DC_SAMPLE_CNS:
		fprintf (sampledata->ostream, "   <cns>%.1f</cns>\n", value.cns * 100.0);
		break;
	case DC_SAMPLE_DECO:
		fprintf (sampledata->ostream, "   <deco time=\"%u\" depth=\"%.2f\">%s</deco>\n",
			value.deco.time,
			convert_depth(value.deco.depth, sampledata->units),
			decostop[value.deco.type]);
		break;
	case DC_SAMPLE_GASMIX:
		fprintf (sampledata->ostream, "   <gasmix>%u</gasmix>\n", value.gasmix);
		break;
	default:
		break;
	}
}

dctool_output_t *
dctool_xml_output_new (const char *filename, dctool_units_t units)
{
	dctool_xml_output_t *output = NULL;

	if (filename == NULL)
		goto error_exit;

	// Allocate memory.
	output = (dctool_xml_output_t *) dctool_output_allocate (&xml_vtable);
	if (output == NULL) {
		goto error_exit;
	}

	// Open the output file.
	output->ostream = fopen (filename, "w");
	if (output->ostream == NULL) {
		goto error_free;
	}

	output->units = units;

	fprintf (output->ostream, "<device>\n");

	return (dctool_output_t *) output;

error_free:
	dctool_output_deallocate ((dctool_output_t *) output);
error_exit:
	return NULL;
}

static dc_status_t
dctool_xml_output_write (dctool_output_t *abstract, dc_parser_t *parser, const unsigned char data[], unsigned int size, const unsigned char fingerprint[], unsigned int fsize)
{
	dctool_xml_output_t *output = (dctool_xml_output_t *) abstract;
	dc_status_t status = DC_STATUS_SUCCESS;

	// Initialize the sample data.
	sample_data_t sampledata = {0};
	sampledata.nsamples = 0;
	sampledata.ostream = output->ostream;
	sampledata.units = output->units;

	fprintf (output->ostream, "<dive>\n<number>%u</number>\n<size>%u</size>\n", abstract->number, size);

	if (fingerprint) {
		fprintf (output->ostream, "<fingerprint>");
		for (unsigned int i = 0; i < fsize; ++i)
			fprintf (output->ostream, "%02X", fingerprint[i]);
		fprintf (output->ostream, "</fingerprint>\n");
	}

	// Parse the datetime.
	message ("Parsing the datetime.\n");
	dc_datetime_t dt = {0};
	status = dc_parser_get_datetime (parser, &dt);
	if (status != DC_STATUS_SUCCESS && status != DC_STATUS_UNSUPPORTED) {
		ERROR ("Error parsing the datetime.");
		goto cleanup;
	}

	if (dt.timezone == DC_TIMEZONE_NONE) {
		fprintf (output->ostream, "<datetime>%04i-%02i-%02i %02i:%02i:%02i</datetime>\n",
			dt.year, dt.month, dt.day,
			dt.hour, dt.minute, dt.second);
	} else {
		fprintf (output->ostream, "<datetime>%04i-%02i-%02i %02i:%02i:%02i %+03i:%02i</datetime>\n",
			dt.year, dt.month, dt.day,
			dt.hour, dt.minute, dt.second,
			dt.timezone / 3600, (abs(dt.timezone) % 3600) / 60);
	}

	// Parse the divetime.
	message ("Parsing the divetime.\n");
	unsigned int divetime = 0;
	status = dc_parser_get_field (parser, DC_FIELD_DIVETIME, 0, &divetime);
	if (status != DC_STATUS_SUCCESS && status != DC_STATUS_UNSUPPORTED) {
		ERROR ("Error parsing the divetime.");
		goto cleanup;
	}

	fprintf (output->ostream, "<divetime>%02u:%02u</divetime>\n",
		divetime / 60, divetime % 60);

	// Parse the maxdepth.
	message ("Parsing the maxdepth.\n");
	double maxdepth = 0.0;
	status = dc_parser_get_field (parser, DC_FIELD_MAXDEPTH, 0, &maxdepth);
	if (status != DC_STATUS_SUCCESS && status != DC_STATUS_UNSUPPORTED) {
		ERROR ("Error parsing the maxdepth.");
		goto cleanup;
	}

	fprintf (output->ostream, "<maxdepth>%.2f</maxdepth>\n",
		convert_depth(maxdepth, output->units));

	// Parse the avgdepth.
	message ("Parsing the avgdepth.\n");
	double avgdepth = 0.0;
	status = dc_parser_get_field (parser, DC_FIELD_AVGDEPTH, 0, &avgdepth);
	if (status != DC_STATUS_SUCCESS && status != DC_STATUS_UNSUPPORTED) {
		ERROR ("Error parsing the avgdepth.");
		goto cleanup;
	}

	if (status != DC_STATUS_UNSUPPORTED) {
		fprintf (output->ostream, "<avgdepth>%.2f</avgdepth>\n",
			convert_depth(avgdepth, output->units));
	}

	// Parse the temperature.
	message ("Parsing the temperature.\n");
	for (unsigned int i = 0; i < 3; ++i) {
		dc_field_type_t fields[] = {DC_FIELD_TEMPERATURE_SURFACE,
			DC_FIELD_TEMPERATURE_MINIMUM,
			DC_FIELD_TEMPERATURE_MAXIMUM};
		const char *names[] = {"surface", "minimum", "maximum"};

		double temperature = 0.0;
		status = dc_parser_get_field (parser, fields[i], 0, &temperature);
		if (status != DC_STATUS_SUCCESS && status != DC_STATUS_UNSUPPORTED) {
			ERROR ("Error parsing the temperature.");
			goto cleanup;
		}

		if (status != DC_STATUS_UNSUPPORTED) {
			fprintf (output->ostream, "<temperature type=\"%s\">%.1f</temperature>\n",
				names[i],
				convert_temperature(temperature, output->units));
		}
	}

	// Parse the gas mixes.
	message ("Parsing the gas mixes.\n");
	unsigned int ngases = 0;
	status = dc_parser_get_field (parser, DC_FIELD_GASMIX_COUNT, 0, &ngases);
	if (status != DC_STATUS_SUCCESS && status != DC_STATUS_UNSUPPORTED) {
		ERROR ("Error parsing the gas mix count.");
		goto cleanup;
	}

	for (unsigned int i = 0; i < ngases; ++i) {
		dc_gasmix_t gasmix = {0};
		status = dc_parser_get_field (parser, DC_FIELD_GASMIX, i, &gasmix);
		if (status != DC_STATUS_SUCCESS && status != DC_STATUS_UNSUPPORTED) {
			ERROR ("Error parsing the gas mix.");
			goto cleanup;
		}

		fprintf (output->ostream,
			"<gasmix>\n"
			"   <he>%.1f</he>\n"
			"   <o2>%.1f</o2>\n"
			"   <n2>%.1f</n2>\n"
			"</gasmix>\n",
			gasmix.helium * 100.0,
			gasmix.oxygen * 100.0,
			gasmix.nitrogen * 100.0);
	}

	// Parse the tanks.
	message ("Parsing the tanks.\n");
	unsigned int ntanks = 0;
	status = dc_parser_get_field (parser, DC_FIELD_TANK_COUNT, 0, &ntanks);
	if (status != DC_STATUS_SUCCESS && status != DC_STATUS_UNSUPPORTED) {
		ERROR ("Error parsing the tank count.");
		goto cleanup;
	}

	for (unsigned int i = 0; i < ntanks; ++i) {
		const char *names[] = {"none", "metric", "imperial"};

		dc_tank_t tank = {0};
		status = dc_parser_get_field (parser, DC_FIELD_TANK, i, &tank);
		if (status != DC_STATUS_SUCCESS && status != DC_STATUS_UNSUPPORTED) {
			ERROR ("Error parsing the tank.");
			goto cleanup;
		}

		fprintf (output->ostream, "<tank>\n");
		if (tank.gasmix != DC_GASMIX_UNKNOWN) {
			fprintf (output->ostream,
				"   <gasmix>%u</gasmix>\n",
				tank.gasmix);
		}
		if (tank.type != DC_TANKVOLUME_NONE) {
			fprintf (output->ostream,
				"   <type>%s</type>\n"
				"   <volume>%.1f</volume>\n"
				"   <workpressure>%.2f</workpressure>\n",
				names[tank.type],
				convert_volume(tank.volume, output->units),
				convert_pressure(tank.workpressure, output->units));
		}
		fprintf (output->ostream,
			"   <beginpressure>%.2f</beginpressure>\n"
			"   <endpressure>%.2f</endpressure>\n"
			"</tank>\n",
			convert_pressure(tank.beginpressure, output->units),
			convert_pressure(tank.endpressure, output->units));
	}

	// Parse the dive mode.
	message ("Parsing the dive mode.\n");
	dc_divemode_t divemode = DC_DIVEMODE_OC;
	status = dc_parser_get_field (parser, DC_FIELD_DIVEMODE, 0, &divemode);
	if (status != DC_STATUS_SUCCESS && status != DC_STATUS_UNSUPPORTED) {
		ERROR ("Error parsing the dive mode.");
		goto cleanup;
	}

	if (status != DC_STATUS_UNSUPPORTED) {
		const char *names[] = {"freedive", "gauge", "oc", "ccr", "scr"};
		fprintf (output->ostream, "<divemode>%s</divemode>\n",
			names[divemode]);
	}

	// Parse the deco model.
	message ("Parsing the deco model.\n");
	dc_decomodel_t decomodel = {DC_DECOMODEL_NONE};
	status = dc_parser_get_field (parser, DC_FIELD_DECOMODEL, 0, &decomodel);
	if (status != DC_STATUS_SUCCESS && status != DC_STATUS_UNSUPPORTED) {
		ERROR ("Error parsing the deco model.");
		goto cleanup;
	}

	if (status != DC_STATUS_UNSUPPORTED) {
		const char *names[] = {"none", "buhlmann", "vpm", "rgbm", "dciem"};
		fprintf (output->ostream, "<decomodel>%s</decomodel>\n",
			names[decomodel.type]);
		if (decomodel.type == DC_DECOMODEL_BUHLMANN &&
			(decomodel.params.gf.low != 0 || decomodel.params.gf.high != 0)) {
			fprintf (output->ostream, "<gf>%u/%u</gf>\n",
				decomodel.params.gf.low, decomodel.params.gf.high);
		}
		if (decomodel.conservatism) {
			fprintf (output->ostream, "<conservatism>%d</conservatism>\n",
				decomodel.conservatism);
		}
	}

	// Parse the salinity.
	message ("Parsing the salinity.\n");
	dc_salinity_t salinity = {DC_WATER_FRESH, 0.0};
	status = dc_parser_get_field (parser, DC_FIELD_SALINITY, 0, &salinity);
	if (status != DC_STATUS_SUCCESS && status != DC_STATUS_UNSUPPORTED) {
		ERROR ("Error parsing the salinity.");
		goto cleanup;
	}

	if (status != DC_STATUS_UNSUPPORTED) {
		fprintf (output->ostream, "<salinity type=\"%u\">%.1f</salinity>\n",
			salinity.type, salinity.density);
	}

	// Parse the atmospheric pressure.
	message ("Parsing the atmospheric pressure.\n");
	double atmospheric = 0.0;
	status = dc_parser_get_field (parser, DC_FIELD_ATMOSPHERIC, 0, &atmospheric);
	if (status != DC_STATUS_SUCCESS && status != DC_STATUS_UNSUPPORTED) {
		ERROR ("Error parsing the atmospheric pressure.");
		goto cleanup;
	}

	if (status != DC_STATUS_UNSUPPORTED) {
		fprintf (output->ostream, "<atmospheric>%.5f</atmospheric>\n",
			convert_pressure(atmospheric, output->units));
	}

	// Parse the sample data.
	message ("Parsing the sample data.\n");
	status = dc_parser_samples_foreach (parser, sample_cb, &sampledata);
	if (status != DC_STATUS_SUCCESS) {
		ERROR ("Error parsing the sample data.");
		goto cleanup;
	}

cleanup:

	if (sampledata.nsamples)
		fprintf (output->ostream, "</sample>\n");
	fprintf (output->ostream, "</dive>\n");

	return status;
}

static dc_status_t
dctool_xml_output_free (dctool_output_t *abstract)
{
	dctool_xml_output_t *output = (dctool_xml_output_t *) abstract;

	fprintf (output->ostream, "</device>\n");

	fclose (output->ostream);

	return DC_STATUS_SUCCESS;
}