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
/*
 * libdivecomputer
 *
 * Copyright (C) 2019 Linus Torvalds
 * Copyright (C) 2022 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 "deepblu_cosmiq.h"
#include "context-private.h"
#include "device-private.h"
#include "platform.h"
#include "checksum.h"
#include "array.h"

// Maximum data in a packet. It's actually much
// less than this, since BLE packets are small and
// with the 7 bytes of headers and final newline
// and the HEX encoding, the actual maximum is
// just something like 6 bytes.
//
// But in theory the data could be done over
// multiple packets. That doesn't seem to be
// the case in anything I've seen so far.
//
// Pick something small and easy to use for
// stack buffers.
#define MAX_DATA 20

#define SZ_HEADER 36

#define FP_OFFSET   6
#define FP_SIZE     6

#define CMD_SET_DATETIME      0x20

#define CMD_DIVE_COUNT        0x40
#define CMD_DIVE_HEADER       0x41
#define CMD_DIVE_HEADER_DATA  0x42
#define CMD_DIVE_PROFILE      0x43
#define CMD_DIVE_PROFILE_DATA 0x44

#define CMD_SYSTEM_FW         0x58
#define CMD_SYSTEM_MAC        0x5A

#define NSTEPS    1000
#define STEP(i,n) (NSTEPS * (i) / (n))

typedef struct deepblu_cosmiq_device_t {
	dc_device_t base;
	dc_iostream_t *iostream;
	unsigned char fingerprint[FP_SIZE];
} deepblu_cosmiq_device_t;

static dc_status_t deepblu_cosmiq_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size);
static dc_status_t deepblu_cosmiq_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata);
static dc_status_t deepblu_cosmiq_device_timesync(dc_device_t *abstract, const dc_datetime_t *datetime);

static const dc_device_vtable_t deepblu_cosmiq_device_vtable = {
	sizeof(deepblu_cosmiq_device_t),
	DC_FAMILY_DEEPBLU_COSMIQ,
	deepblu_cosmiq_device_set_fingerprint, /* set_fingerprint */
	NULL, /* read */
	NULL, /* write */
	NULL, /* dump */
	deepblu_cosmiq_device_foreach, /* foreach */
	deepblu_cosmiq_device_timesync, /* timesync */
	NULL, /* close */
};

//
// Send a cmd packet.
//
// The format of the cmd on the "wire" is:
//  - byte '#'
//  - HEX char of cmd
//  - HEX char two's complement modular sum of packet data (including cmd/size)
//  - HEX char size of data as encoded in HEX
//  - n * HEX char data
//  - byte '\n'
// so you end up having 8 bytes of header/trailer overhead, and two bytes
// for every byte of data sent due to the HEX encoding.
//
static dc_status_t
deepblu_cosmiq_send (deepblu_cosmiq_device_t *device, const unsigned char cmd, const unsigned char data[], size_t size)
{
	dc_status_t status = DC_STATUS_SUCCESS;
	dc_device_t *abstract = (dc_device_t *) device;

	if (size > MAX_DATA)
		return DC_STATUS_INVALIDARGS;

	if (device_is_cancelled (abstract))
		return DC_STATUS_CANCELLED;

	// Build the packet.
	unsigned char csum = ~checksum_add_uint8 (data, size, cmd + 2 * size) + 1;
	unsigned char raw[3 + MAX_DATA] = {
		cmd,
		csum,
		2 * size
	};
	if (size) {
		memcpy (raw + 3, data, size);
	}
	unsigned char packet[1 + 2 * (3 + MAX_DATA) + 1] = {0};
	packet[0] = '#';
	array_convert_bin2hex (raw, 3 + size, packet + 1, 2 * (3 + size));
	packet[1 + 2 * (3 + size)] = '\n';

	HEXDUMP (device->base.context, DC_LOGLEVEL_DEBUG, "cmd", raw, 3 + size);

	// Send the command.
	status = dc_iostream_write (device->iostream, packet, 2 + 2 * (3 + size), NULL);
	if (status != DC_STATUS_SUCCESS) {
		ERROR (device->base.context, "Failed to send the command.");
		return status;
	}

	return status;
}

//
// Receive one 'line' of data
//
// The deepblu BLE protocol is ASCII line based and packetized.
// Normally one packet is one line, but it looks like the Nordic
// Semi BLE chip will sometimes send packets early (some internal
// serial buffer timeout?) with incompete data.
//
// So read packets until you get newline.
static dc_status_t
deepblu_cosmiq_recv_line (deepblu_cosmiq_device_t *device, unsigned char data[], size_t size, size_t *actual)
{
	dc_status_t status = DC_STATUS_SUCCESS;
	size_t nbytes = 0;

	while (1) {
		unsigned char buffer[20] = {0};
		size_t transferred = 0;

		status = dc_iostream_read (device->iostream, buffer, sizeof(buffer), &transferred);
		if (status != DC_STATUS_SUCCESS) {
			ERROR (device->base.context, "Failed to receive the reply packet.");
			return status;
		}

		if (transferred < 1) {
			ERROR (device->base.context, "Empty reply packet received.");
			return DC_STATUS_PROTOCOL;
		}

		// Append the payload data to the output buffer. If the output
		// buffer is too small, the error is not reported immediately
		// but delayed until all packets have been received.
		if (nbytes < size) {
			size_t n = transferred;
			if (nbytes + n > size) {
				n = size - nbytes;
			}
			memcpy(data + nbytes, buffer, n);
		}
		nbytes += transferred;

		// Last packet?
		if (buffer[transferred - 1] == '\n')
			break;
	}

	// Verify the expected number of bytes.
	if (nbytes > size) {
		ERROR (device->base.context, "Unexpected number of bytes received (" DC_PRINTF_SIZE " " DC_PRINTF_SIZE ").", nbytes, size);
		return DC_STATUS_PROTOCOL;
	}

	if (actual)
		*actual = nbytes;

	return DC_STATUS_SUCCESS;
}

//
// Receive a reply packet
//
// The reply packet has the same format as the cmd packet we
// send, except the first byte is '$' instead of '#'.
static dc_status_t
deepblu_cosmiq_recv (deepblu_cosmiq_device_t *device, const unsigned char cmd, unsigned char data[], size_t size, size_t *actual)
{
	dc_status_t status = DC_STATUS_SUCCESS;
	unsigned char packet[1 + 2 * (3 + MAX_DATA) + 1] = {0};

	size_t transferred = 0;
	status = deepblu_cosmiq_recv_line (device, packet, sizeof(packet), &transferred);
	if (status != DC_STATUS_SUCCESS)
		return status;

	if (transferred < 8 || (transferred % 2) != 0) {
		ERROR (device->base.context, "Unexpected packet length (" DC_PRINTF_SIZE ").", transferred);
		return DC_STATUS_PROTOCOL;
	}

	if (packet[0] != '$' || packet[transferred - 1] != '\n') {
		ERROR (device->base.context, "Unexpected packet start/end byte.");
		return DC_STATUS_PROTOCOL;
	}

	size_t length = transferred - 2;

	unsigned char raw[3 + MAX_DATA] = {0};
	if (array_convert_hex2bin (packet + 1, length, raw, length / 2) != 0) {
		ERROR (device->base.context, "Unexpected packet data.");
		return DC_STATUS_PROTOCOL;
	}

	length /= 2;

	HEXDUMP (device->base.context, DC_LOGLEVEL_DEBUG, "rcv", raw, length);

	unsigned char rsp = raw[0];
	if (rsp != cmd) {
		ERROR (device->base.context, "Unexpected packet command byte (%02x)", rsp);
		return DC_STATUS_PROTOCOL;
	}

	unsigned int n = raw[2];
	if ((n % 2) != 0 || n != transferred - 8) {
		ERROR (device->base.context, "Unexpected packet length (%u)", n);
		return DC_STATUS_PROTOCOL;
	}

	unsigned char csum = checksum_add_uint8 (raw, length, 0);
	if (csum != 0) {
		ERROR (device->base.context, "Unexpected packet checksum (%02x).", csum);
		return DC_STATUS_PROTOCOL;
	}

	length -= 3;

	if (length > size) {
		ERROR (device->base.context, "Unexpected number of bytes received (" DC_PRINTF_SIZE " " DC_PRINTF_SIZE ").", length, size);
		return DC_STATUS_PROTOCOL;
	}

	memcpy (data, raw + 3, length);

	if (actual)
		*actual = length;

	return DC_STATUS_SUCCESS;
}

static dc_status_t
deepblu_cosmiq_transfer (deepblu_cosmiq_device_t *device, const unsigned char cmd,
	const unsigned char input[], size_t isize,
	unsigned char output[], size_t osize)
{
	dc_status_t status = DC_STATUS_SUCCESS;

	status = deepblu_cosmiq_send (device, cmd, input, isize);
	if (status != DC_STATUS_SUCCESS)
		return status;

	size_t transferred = 0;
	status = deepblu_cosmiq_recv (device, cmd, output, osize, &transferred);
	if (status != DC_STATUS_SUCCESS)
		return status;

	if (transferred != osize) {
		ERROR (device->base.context, "Unexpected number of bytes received (" DC_PRINTF_SIZE " " DC_PRINTF_SIZE ").", osize, transferred);
		return DC_STATUS_PROTOCOL;
	}

	return DC_STATUS_SUCCESS;
}

static dc_status_t
deepblu_cosmiq_recv_bulk (deepblu_cosmiq_device_t *device, dc_event_progress_t *progress, const unsigned char cmd, unsigned char data[], size_t size)
{
	dc_status_t status = DC_STATUS_SUCCESS;
	dc_device_t *abstract = (dc_device_t *) device;

	const unsigned int initial = progress ? progress->current : 0;

	size_t nbytes = 0;
	while (nbytes < size) {
		size_t transferred = 0;
		status = deepblu_cosmiq_recv (device, cmd, data + nbytes, size - nbytes, &transferred);
		if (status != DC_STATUS_SUCCESS)
			return status;

		nbytes += transferred;

		// Update and emit a progress event.
		if (progress) {
			progress->current = initial + STEP(nbytes, size);
			device_event_emit (abstract, DC_EVENT_PROGRESS, progress);
		}
	}

	return DC_STATUS_SUCCESS;
}

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

	if (out == NULL)
		return DC_STATUS_INVALIDARGS;

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

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

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

	dc_iostream_purge (device->iostream, DC_DIRECTION_ALL);

	*out = (dc_device_t *) device;

	return DC_STATUS_SUCCESS;

error_free:
	dc_device_deallocate ((dc_device_t *) device);
	return status;
}

static dc_status_t
deepblu_cosmiq_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size)
{
	deepblu_cosmiq_device_t *device = (deepblu_cosmiq_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
deepblu_cosmiq_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata)
{
	dc_status_t status = DC_STATUS_SUCCESS;
	deepblu_cosmiq_device_t *device = (deepblu_cosmiq_device_t *) abstract;
	const unsigned char zero = 0;

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

	// Read the firmware version.
	unsigned char fw[1] = {0};
	status = deepblu_cosmiq_transfer (device, CMD_SYSTEM_FW, &zero, 1, fw, sizeof(fw));
	if (status != DC_STATUS_SUCCESS) {
		ERROR (abstract->context, "Failed to read the firmware version.");
		goto error_exit;
	}

	// Read the MAC address.
	unsigned char mac[6] = {0};
	status = deepblu_cosmiq_transfer (device, CMD_SYSTEM_MAC, &zero, 1, mac, sizeof(mac));
	if (status != DC_STATUS_SUCCESS) {
		ERROR (abstract->context, "Failed to read the MAC address.");
		goto error_exit;
	}

	// Emit a device info event.
	dc_event_devinfo_t devinfo;
	devinfo.model = 0;
	devinfo.firmware = fw[0] & 0x3F;
	devinfo.serial = array_uint32_le (mac);
	device_event_emit (abstract, DC_EVENT_DEVINFO, &devinfo);

	unsigned char ndives = 0;
	status = deepblu_cosmiq_transfer (device, CMD_DIVE_COUNT, &zero, 1, &ndives, 1);
	if (status != DC_STATUS_SUCCESS) {
		ERROR (abstract->context, "Failed to read the number of dives.");
		goto error_exit;
	}

	// Update and emit a progress event.
	progress.current = (ndives == 0 ? 1 : 0) * NSTEPS;
	progress.maximum = (ndives + 1) * NSTEPS;
	device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);

	if (ndives == 0) {
		status = DC_STATUS_SUCCESS;
		goto error_exit;
	}

	unsigned char *headers = malloc (ndives * SZ_HEADER);
	if (headers == NULL) {
		ERROR (abstract->context, "Failed to allocate memory.");
		status = DC_STATUS_NOMEMORY;
		goto error_exit;
	}

	unsigned int count = 0;
	for (unsigned int i = 0; i < ndives; ++i) {
		unsigned char number = i + 1;

		unsigned char len = 0;
		status = deepblu_cosmiq_transfer (device, CMD_DIVE_HEADER, &number, 1, &len, 1);
		if (status != DC_STATUS_SUCCESS) {
			ERROR (abstract->context, "Failed to read the dive header.");
			goto error_free_headers;
		}

		if (len != SZ_HEADER) {
			status = DC_STATUS_PROTOCOL;
			goto error_free_headers;
		}

		status = deepblu_cosmiq_recv_bulk (device, NULL, CMD_DIVE_HEADER_DATA, headers + i * SZ_HEADER, SZ_HEADER);
		if (status != DC_STATUS_SUCCESS) {
			ERROR (abstract->context, "Failed to read the dive header.");
			goto error_free_headers;
		}

		// Update and emit a progress event.
		progress.current = STEP(i + 1, ndives);
		device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);

		if (memcmp (headers + i * SZ_HEADER + FP_OFFSET, device->fingerprint, sizeof (device->fingerprint)) == 0)
			break;

		count++;
	}

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

	// Allocate memory for the dive.
	dc_buffer_t *dive = dc_buffer_new (4096);
	if (dive == NULL) {
		ERROR (abstract->context, "Failed to allocate memory.");
		status = DC_STATUS_NOMEMORY;
		goto error_free_headers;
	}

	for (unsigned int i = 0; i < count; ++i) {
		unsigned char number = i + 1;

		unsigned char rsp_len[2] = {0};
		status = deepblu_cosmiq_transfer (device, CMD_DIVE_PROFILE, &number, 1, rsp_len, sizeof(rsp_len));
		if (status != DC_STATUS_SUCCESS) {
			ERROR (abstract->context, "Failed to read the dive profile.");
			goto error_free_dive;
		}

		unsigned int len = array_uint16_be (rsp_len);

		// Erase the buffer.
		dc_buffer_clear (dive);

		// Allocate memory for the dive.
		if (!dc_buffer_resize (dive, len + SZ_HEADER)) {
			ERROR (abstract->context, "Failed to allocate memory.");
			status = DC_STATUS_NOMEMORY;
			goto error_free_dive;
		}

		// Cache the pointer.
		unsigned char *data = dc_buffer_get_data (dive);

		memcpy (data, headers + i * SZ_HEADER, SZ_HEADER);

		status = deepblu_cosmiq_recv_bulk (device, &progress, CMD_DIVE_PROFILE_DATA, data + SZ_HEADER, len);
		if (status != DC_STATUS_SUCCESS) {
			ERROR (abstract->context, "Failed to read the dive profile.");
			goto error_free_dive;
		}

		if (callback && !callback (data, len + SZ_HEADER, data + FP_OFFSET, FP_SIZE, userdata))
			break;
	}

error_free_dive:
	dc_buffer_free (dive);
error_free_headers:
	free (headers);
error_exit:
	return status;
}

static dc_status_t
deepblu_cosmiq_device_timesync (dc_device_t *abstract, const dc_datetime_t *datetime)
{
	dc_status_t status = DC_STATUS_SUCCESS;
	deepblu_cosmiq_device_t *device = (deepblu_cosmiq_device_t *) abstract;

	if (datetime->year < 2000) {
		ERROR (abstract->context, "Invalid date/time value specified.");
		return DC_STATUS_INVALIDARGS;
	}

	const unsigned char cmd[6] = {
		dec2bcd(datetime->year - 2000),
		dec2bcd(datetime->month),
		dec2bcd(datetime->day),
		dec2bcd(datetime->hour),
		dec2bcd(datetime->minute),
		dec2bcd(datetime->second)};
	unsigned char rsp[1] = {0};
	status = deepblu_cosmiq_transfer (device, CMD_SET_DATETIME,
		cmd, sizeof(cmd), rsp, sizeof(rsp));
	if (status != DC_STATUS_SUCCESS)
		return status;

	return DC_STATUS_SUCCESS;
}