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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
/*
 * libdivecomputer
 *
 * Copyright (C) 2023 Jan Matoušek, 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 <string.h>
#include <stdlib.h>
#include <stdio.h>

#include "divesoft_freedom.h"
#include "context-private.h"
#include "device-private.h"
#include "platform.h"
#include "checksum.h"
#include "array.h"
#include "hdlc.h"

#define MAXDATA 256

#define HEADER_SIGNATURE_V1 0x45766944 // "DivE"
#define HEADER_SIGNATURE_V2 0x45566944 // "DiVE"

#define HEADER_SIZE_V1 32
#define HEADER_SIZE_V2 64

#define RECORD_SIZE 16
#define FINGERPRINT_SIZE 20

#define INVALID     0xFFFFFFFF
#define COMPRESSION 1
#define DIRECTION   1
#define NRECORDS    100

#define DEVICE_CCR_CU      1  // Liberty HW rev. 1.X
#define DEVICE_FREEDOM     2  // Freedom HW rev. 2.X
#define DEVICE_FREEDOM3    5  // Freedom HW rev. 3.X
#define DEVICE_CCR_CU15    10 // Liberty HW rev. 2.X, Bluetooth enabled
#define DEVICE_FREEDOM4    19 // Freedom HW rev. 4.X, Bluetooth enabled

typedef enum message_t {
	MSG_ECHO = 0,
	MSG_RESULT = 1,
	MSG_CONNECT = 2,
	MSG_CONNECTED = 3,
	MSG_VERSION = 4,
	MSG_VERSION_RSP = 5,
	MSG_DIVE_DATA = 64,
	MSG_DIVE_DATA_RSP = 65,
	MSG_DIVE_LIST = 66,
	MSG_DIVE_LIST_V1 = 67,
	MSG_DIVE_LIST_V2 = 71,
} message_t;

typedef struct divesoft_freedom_device_t {
	dc_device_t base;
	dc_iostream_t *iostream;
	unsigned char fingerprint[FINGERPRINT_SIZE];
	unsigned int seqnum;
} divesoft_freedom_device_t;

static dc_status_t divesoft_freedom_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size);
static dc_status_t divesoft_freedom_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata);
static dc_status_t divesoft_freedom_device_close (dc_device_t *device);

static const dc_device_vtable_t divesoft_freedom_device_vtable = {
	sizeof(divesoft_freedom_device_t),
	DC_FAMILY_DIVESOFT_FREEDOM,
	divesoft_freedom_device_set_fingerprint, /* set_fingerprint */
	NULL, /* read */
	NULL, /* write */
	NULL, /* dump */
	divesoft_freedom_device_foreach, /* foreach */
	NULL, /* timesync */
	divesoft_freedom_device_close, /* close */
};

static dc_status_t
divesoft_freedom_send (divesoft_freedom_device_t *device, message_t message, const unsigned char data[], size_t size)
{
	dc_status_t status = DC_STATUS_SUCCESS;
	dc_device_t *abstract = (dc_device_t *) device;

	size_t nbytes = 0, count = 0;
	while (1) {
		size_t len = size - nbytes;
		if (len > MAXDATA)
			len = MAXDATA;

		unsigned int islast = nbytes + len == size;

		unsigned char packet[6 + MAXDATA + 2] = {0};
		packet[0] = ((count & 0x0F) << 4) | (device->seqnum & 0x0F);
		packet[1] = 0x80 | (islast << 6);
		array_uint16_le_set (packet + 2, message);
		array_uint16_le_set (packet + 4, len);
		if (len) {
			memcpy (packet + 6, data + nbytes, len);
		}
		unsigned short crc = checksum_crc16r_ccitt (packet, len + 6, 0xFFFF, 0xFFFF);
		array_uint16_le_set (packet + 6 + len, crc);

		HEXDUMP (abstract->context, DC_LOGLEVEL_DEBUG, "cmd", packet, 6 + len + 2);

		status = dc_iostream_write (device->iostream, packet, 6 + len + 2, NULL);
		if (status != DC_STATUS_SUCCESS) {
			ERROR (abstract->context, "Failed to send the packet.");
			return status;
		}

		nbytes += len;
		count++;

		if (islast)
			break;
	}

	return status;
}

static dc_status_t
divesoft_freedom_recv (divesoft_freedom_device_t *device, dc_event_progress_t *progress, message_t *message, dc_buffer_t *buffer)
{
	dc_status_t status = DC_STATUS_SUCCESS;
	dc_device_t *abstract = (dc_device_t *) device;
	unsigned int msg = INVALID;

	unsigned int count = 0;
	while (1) {
		size_t len = 0;
		unsigned char packet[6 + MAXDATA + 2] = {0};
		status = dc_iostream_read (device->iostream, packet, sizeof(packet), &len);
		if (status != DC_STATUS_SUCCESS) {
			ERROR (abstract->context, "Failed to receive the packet.");
			return status;
		}

		HEXDUMP (abstract->context, DC_LOGLEVEL_DEBUG, "rcv", packet, len);

		if (len < 8) {
			ERROR (abstract->context, "Unexpected packet length (" DC_PRINTF_SIZE ").", len);
			return DC_STATUS_PROTOCOL;
		}

		unsigned int seqnum = packet[0];
		unsigned int flags = packet[1];
		unsigned int type = array_uint16_le (packet + 2);
		unsigned int length = array_uint16_le (packet + 4);

		unsigned int expected = ((count & 0x0F) << 4) | (device->seqnum & 0x0F);
		if (seqnum != expected) {
			ERROR (abstract->context, "Unexpected packet sequence number (%u %u).", seqnum, expected);
			return DC_STATUS_PROTOCOL;
		}

		if ((flags & ~0x40) != 0) {
			ERROR (abstract->context, "Unexpected packet flags (%u).", flags);
			return DC_STATUS_PROTOCOL;
		}

		if (length != len - 8) {
			ERROR (abstract->context, "Unexpected packet length (%u " DC_PRINTF_SIZE ").", length, len - 8);
			return DC_STATUS_PROTOCOL;
		}

		if (msg == INVALID) {
			msg = type;
		} else if (msg != type) {
			ERROR (abstract->context, "Unexpected packet type (%u).", msg);
			return DC_STATUS_PROTOCOL;
		}

		unsigned short crc = array_uint16_le (packet + len - 2);
		unsigned short ccrc = checksum_crc16r_ccitt (packet, len - 2, 0xFFFF, 0xFFFF);
		if (crc != ccrc) {
			ERROR (abstract->context, "Unexpected packet checksum (%04x %04x).", crc, ccrc);
			return DC_STATUS_PROTOCOL;
		}

		// Update and emit a progress event.
		if (progress) {
			progress->current += len - 8;
			// Limit the progress to the maximum size. This could happen if the
			// dive computer sends more data than requested for some reason.
			if (progress->current > progress->maximum) {
				WARNING (abstract->context, "Progress exceeds the maximum size.");
				progress->current = progress->maximum;
			}
			device_event_emit (abstract, DC_EVENT_PROGRESS, progress);
		}

		if (!dc_buffer_append (buffer, packet + 6, len - 8)) {
			ERROR (abstract->context, "Insufficient buffer space available.");
			return DC_STATUS_NOMEMORY;
		}

		count++;

		if (flags & 0x40)
			break;
	}

	if (message)
		*message = msg;

	return status;
}

static dc_status_t
divesoft_freedom_transfer (divesoft_freedom_device_t *device, dc_event_progress_t *progress, message_t cmd, const unsigned char data[], size_t size, message_t *msg, dc_buffer_t *buffer)
{
	dc_status_t status = DC_STATUS_SUCCESS;
	dc_device_t *abstract = (dc_device_t *) device;

	if (device_is_cancelled (abstract))
		return DC_STATUS_CANCELLED;

	device->seqnum++;

	status = divesoft_freedom_send (device, cmd, data, size);
	if (status != DC_STATUS_SUCCESS) {
		ERROR (abstract->context, "Failed to send the command.");
		return status;
	}

	status = divesoft_freedom_recv (device, progress, msg, buffer);
	if(status != DC_STATUS_SUCCESS) {
		ERROR (abstract->context, "Failed to receive response.");
		return status;
	}

	return status;
}

static dc_status_t
divesoft_freedom_download (divesoft_freedom_device_t *device, message_t cmd, const unsigned char cdata[], size_t csize, unsigned char rdata[], size_t rsize)
{
	dc_status_t status = DC_STATUS_SUCCESS;
	dc_device_t *abstract = (dc_device_t *) device;

	dc_buffer_t *buffer = dc_buffer_new (rsize);
	if (buffer == NULL) {
		ERROR (abstract->context, "Failed to allocate memory.");
		status = DC_STATUS_NOMEMORY;
		goto error_exit;
	}

	message_t msg = MSG_ECHO;
	status = divesoft_freedom_transfer (device, NULL, cmd, cdata, csize, &msg, buffer);
	if (status != DC_STATUS_SUCCESS) {
		ERROR (abstract->context, "Failed to transfer the packet.");
		goto error_free;
	}

	if (msg != cmd + 1) {
		ERROR (abstract->context, "Unexpected response message (%u).", msg);
		status = DC_STATUS_PROTOCOL;
		goto error_free;
	}

	size_t length = dc_buffer_get_size (buffer);
	if (length != rsize) {
		ERROR (abstract->context, "Unexpected response length (" DC_PRINTF_SIZE " " DC_PRINTF_SIZE ").", length, rsize);
		status =  DC_STATUS_PROTOCOL;
		goto error_free;
	}

	if (rsize) {
		memcpy (rdata, dc_buffer_get_data (buffer), rsize);
	}

error_free:
	dc_buffer_free (buffer);
error_exit:
	return status;
}

dc_status_t
divesoft_freedom_device_open (dc_device_t **out, dc_context_t *context, dc_iostream_t *iostream)
{
	dc_status_t status = DC_STATUS_SUCCESS;
	divesoft_freedom_device_t *device = NULL;

	if (out == NULL)
		return DC_STATUS_INVALIDARGS;

	// Allocate memory.
	device = (divesoft_freedom_device_t *) dc_device_allocate (context, &divesoft_freedom_device_vtable);
	if (device == NULL) {
		ERROR (context, "Failed to allocate memory.");
		return DC_STATUS_NOMEMORY;
	}

	// Set the default values.
	device->iostream = NULL;
	memset(device->fingerprint, 0, sizeof(device->fingerprint));
	device->seqnum = 0;

	// Setup the HDLC communication.
	status = dc_hdlc_open (&device->iostream, context, iostream, 244, 244);
	if (status != DC_STATUS_SUCCESS) {
		ERROR (context, "Failed to create the HDLC stream.");
		goto error_free;
	}

	// Set the serial communication protocol (115200 8N1).
	status = dc_iostream_configure (device->iostream, 115200, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
	if (status != DC_STATUS_SUCCESS) {
		ERROR (context, "Failed to set the terminal attributes.");
		goto error_free_hdlc;
	}

	// Set the timeout for receiving data (3000ms).
	status = dc_iostream_set_timeout (device->iostream, 3000);
	if (status != DC_STATUS_SUCCESS) {
		ERROR (context, "Failed to set the timeout.");
		goto error_free_hdlc;
	}

	// Initiate the connection with the dive computer.
	const char client[] = "libdivecomputer";
	unsigned char cmd_connect[2 + sizeof(client) - 1] = {0};
	array_uint16_le_set (cmd_connect, COMPRESSION);
	memcpy (cmd_connect + 2, client, sizeof(client) - 1);
	unsigned char rsp_connect[36] = {0};
	status = divesoft_freedom_download (device, MSG_CONNECT, cmd_connect, sizeof(cmd_connect), rsp_connect, sizeof(rsp_connect));
	if (status != DC_STATUS_SUCCESS) {
		ERROR (context, "Failed to connect to the device.");
		goto error_free_hdlc;
	}

	DEBUG (context, "Connection: compression=%u, protocol=%u.%u, serial=%.16s",
		array_uint16_le (rsp_connect),
		rsp_connect[2], rsp_connect[3],
		rsp_connect + 4);

	*out = (dc_device_t *) device;

	return DC_STATUS_SUCCESS;

error_free_hdlc:
	dc_iostream_close (device->iostream);
error_free:
	dc_device_deallocate ((dc_device_t *) device);
	return status;
}

static dc_status_t
divesoft_freedom_device_close (dc_device_t *abstract)
{
	divesoft_freedom_device_t *device = (divesoft_freedom_device_t *) abstract;

	return dc_iostream_close (device->iostream);
}

static dc_status_t
divesoft_freedom_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size)
{
	divesoft_freedom_device_t *device = (divesoft_freedom_device_t *) abstract;

	if (size && size != sizeof (device->fingerprint))
		return DC_STATUS_INVALIDARGS;

	if (size)
		memcpy (device->fingerprint, data, sizeof (device->fingerprint));
	else
		memset (device->fingerprint, 0, sizeof (device->fingerprint));

	return DC_STATUS_SUCCESS;
}

static dc_status_t
divesoft_freedom_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata)
{
	dc_status_t status = DC_STATUS_SUCCESS;
	divesoft_freedom_device_t *device = (divesoft_freedom_device_t *) abstract;

	// Enable progress notifications.
	dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER;
	device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);

	// Read the device information.
	unsigned char rsp_version[26] = {0};
	status = divesoft_freedom_download (device, MSG_VERSION, NULL, 0, rsp_version, sizeof(rsp_version));
	if (status != DC_STATUS_SUCCESS) {
		ERROR (abstract->context, "Failed to read the device information.");
		goto error_exit;
	}

	DEBUG (abstract->context, "Device: model=%u, hw=%u.%u, sw=%u.%u.%u.%u serial=%.16s",
		rsp_version[0],
		rsp_version[1], rsp_version[2],
		rsp_version[3], rsp_version[4], rsp_version[5],
		array_uint32_le (rsp_version + 6),
		rsp_version + 10);

	// Emit a device info event.
	dc_event_devinfo_t devinfo;
	devinfo.model = rsp_version[0];
	devinfo.firmware = array_uint24_be (rsp_version + 3);
	devinfo.serial = array_convert_str2num (rsp_version + 10 + 5, 11);
	device_event_emit(abstract, DC_EVENT_DEVINFO, &devinfo);

	// Allocate memory for the dive list.
	dc_buffer_t *divelist = dc_buffer_new (0);
	if (divelist == NULL) {
		status = DC_STATUS_NOMEMORY;
		goto error_exit;
	}

	// Allocate memory for the download buffer.
	dc_buffer_t *buffer = dc_buffer_new (NRECORDS * (4 + FINGERPRINT_SIZE + HEADER_SIZE_V2));
	if (buffer == NULL) {
		status = DC_STATUS_NOMEMORY;
		goto error_free_divelist;
	}

	// Record version and size.
	unsigned int version = 0;
	unsigned int headersize = 0;
	unsigned int recordsize = 0;

	// Download the dive list.
	unsigned int ndives = 0;
	unsigned int total = 0;
	unsigned int maxsize = 0;
	unsigned int current = INVALID;
	while (1) {
		// Clear the buffer.
		dc_buffer_clear (buffer);

		// Prepare the command.
		unsigned char cmd_list[6] = {0};
		array_uint32_le_set (cmd_list, current);
		cmd_list[4] = DIRECTION;
		cmd_list[5] = NRECORDS;

		// Download the dive list records.
		message_t msg_list = MSG_ECHO;
		status = divesoft_freedom_transfer (device, &progress, MSG_DIVE_LIST, cmd_list, sizeof(cmd_list), &msg_list, buffer);
		if (status != DC_STATUS_SUCCESS) {
			ERROR (abstract->context, "Failed to download the dive list.");
			goto error_free_buffer;
		}

		// Check the response message type.
		if (msg_list != MSG_DIVE_LIST_V1 && msg_list != MSG_DIVE_LIST_V2) {
			ERROR (abstract->context, "Unexpected response message (%u).", msg_list);
			status = DC_STATUS_PROTOCOL;
			goto error_free_buffer;
		}

		// Store/check the version.
		if (version == 0) {
			version = msg_list;
			headersize = version == MSG_DIVE_LIST_V1 ?
				HEADER_SIZE_V1 : HEADER_SIZE_V2;
			recordsize = 4 + FINGERPRINT_SIZE + headersize;
		} else if (version != msg_list) {
			ERROR (abstract->context, "Unexpected response message (%u).", msg_list);
			status = DC_STATUS_PROTOCOL;
			goto error_free_buffer;
		}

		const unsigned char *data = dc_buffer_get_data (buffer);
		size_t size = dc_buffer_get_size (buffer);

		// Process the records.
		size_t offset = 0, count = 0;
		while (offset + recordsize <= size) {
			// Get the record data.
			unsigned int handle = array_uint32_le (data + offset);
			const unsigned char *fingerprint = data + offset + 4;
			const unsigned char *header = data + offset + 4 + FINGERPRINT_SIZE;

			// Check the fingerprint data.
			if (memcmp (device->fingerprint, fingerprint, sizeof(device->fingerprint)) == 0) {
				break;
			}

			// Get the length of the dive.
			unsigned int nrecords = version == MSG_DIVE_LIST_V1 ?
				array_uint32_le (header + 16) & 0x3FFFF :
				array_uint32_le (header + 20);
			unsigned int length = headersize + nrecords * RECORD_SIZE;

			// Calculate the total and maximum size.
			if (length > maxsize)
				maxsize = length;
			total += length;

			// Set the handle for the next request.
			current = handle;

			offset += recordsize;
			count++;
			ndives++;
		}

		// Append the records to the dive list buffer.
		if (!dc_buffer_append (divelist, data, count * recordsize)) {
			ERROR (abstract->context, "Insufficient buffer space available.");
			status = DC_STATUS_NOMEMORY;
			goto error_free_buffer;
		}

		// Stop downloading if there are no more records.
		if (count < NRECORDS)
			break;
	}

	// Update and emit a progress event.
	progress.maximum = progress.current + total;
	device_event_emit(abstract, DC_EVENT_PROGRESS, &progress);

	// Reserve memory for the largest dive.
	dc_buffer_reserve (buffer, maxsize);

	const unsigned char *data = dc_buffer_get_data (divelist);
	size_t size = dc_buffer_get_size (divelist);

	size_t offset = 0;
	while (offset + recordsize <= size) {
		// Get the record data.
		unsigned int handle = array_uint32_le (data + offset);
		const unsigned char *fingerprint = data + offset + 4;
		const unsigned char *header = data + offset + 4 + FINGERPRINT_SIZE;

		// Get the length of the dive.
		unsigned int nrecords = version == MSG_DIVE_LIST_V1 ?
			array_uint32_le (header + 16) & 0x3FFFF :
			array_uint32_le (header + 20);
		unsigned int length = headersize + nrecords * RECORD_SIZE;

		// Clear the buffer.
		dc_buffer_clear (buffer);

		// Prepare the command.
		unsigned char cmd_dive[12] = {0};
		array_uint32_le_set (cmd_dive + 0, handle);
		array_uint32_le_set (cmd_dive + 4, 0);
		array_uint32_le_set (cmd_dive + 8, length);

		// Download the dive.
		message_t msg_dive = MSG_ECHO;
		status = divesoft_freedom_transfer (device, &progress, MSG_DIVE_DATA, cmd_dive, sizeof(cmd_dive), &msg_dive, buffer);
		if (status != DC_STATUS_SUCCESS) {
			ERROR (abstract->context, "Failed to download the dive.");
			goto error_free_buffer;
		}

		// Check the response message type.
		if (msg_dive != MSG_DIVE_DATA_RSP) {
			ERROR (abstract->context, "Unexpected response message (%u).", msg_dive);
			status = DC_STATUS_PROTOCOL;
			goto error_free_buffer;
		}

		// Verify both dive headers are identical.
		if (dc_buffer_get_size (buffer) < headersize ||
			memcmp (header, dc_buffer_get_data (buffer), headersize) != 0) {
			ERROR (abstract->context, "Unexpected profile header.");
			status = DC_STATUS_PROTOCOL;
			goto error_free_buffer;
		}

		if (callback && !callback (dc_buffer_get_data(buffer), dc_buffer_get_size(buffer), fingerprint, sizeof (device->fingerprint), userdata)) {
			break;
		}

		offset += recordsize;
	}

error_free_buffer:
	dc_buffer_free (buffer);
error_free_divelist:
	dc_buffer_free (divelist);
error_exit:
	return status;
}