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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
/*
 * libdivecomputer
 *
 * Copyright (C) 2008 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> // memcmp, memcpy
#include <stdlib.h> // malloc, free
#include <assert.h> // assert

#include "reefnet_sensusultra.h"
#include "context-private.h"
#include "device-private.h"
#include "checksum.h"
#include "array.h"

#define ISINSTANCE(device) dc_device_isinstance((device), &reefnet_sensusultra_device_vtable)

#define SZ_PACKET    512
#define SZ_MEMORY    2080768
#define SZ_USER      16384
#define SZ_HANDSHAKE 24
#define SZ_SENSE     6

#define MAXRETRIES 2
#define PROMPT 0xA5
#define ACCEPT PROMPT
#define REJECT 0x00

typedef struct reefnet_sensusultra_device_t {
	dc_device_t base;
	dc_iostream_t *iostream;
	unsigned char handshake[SZ_HANDSHAKE];
	unsigned int timestamp;
	unsigned int devtime;
	dc_ticks_t systime;
} reefnet_sensusultra_device_t;

static dc_status_t reefnet_sensusultra_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size);
static dc_status_t reefnet_sensusultra_device_dump (dc_device_t *abstract, dc_buffer_t *buffer);
static dc_status_t reefnet_sensusultra_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata);

static const dc_device_vtable_t reefnet_sensusultra_device_vtable = {
	sizeof(reefnet_sensusultra_device_t),
	DC_FAMILY_REEFNET_SENSUSULTRA,
	reefnet_sensusultra_device_set_fingerprint, /* set_fingerprint */
	NULL, /* read */
	NULL, /* write */
	reefnet_sensusultra_device_dump, /* dump */
	reefnet_sensusultra_device_foreach, /* foreach */
	NULL, /* timesync */
	NULL /* close */
};


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

	if (out == NULL)
		return DC_STATUS_INVALIDARGS;

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

	// Set the default values.
	device->iostream = iostream;
	device->timestamp = 0;
	device->systime = (dc_ticks_t) -1;
	device->devtime = 0;
	memset (device->handshake, 0, sizeof (device->handshake));

	// 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;
	}

	// 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;
	}

	// Make sure everything is in a sane state.
	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;
}


dc_status_t
reefnet_sensusultra_device_get_handshake (dc_device_t *abstract, unsigned char data[], unsigned int size)
{
	reefnet_sensusultra_device_t *device = (reefnet_sensusultra_device_t*) abstract;

	if (!ISINSTANCE (abstract))
		return DC_STATUS_INVALIDARGS;

	if (size < SZ_HANDSHAKE) {
		ERROR (abstract->context, "Insufficient buffer space available.");
		return DC_STATUS_INVALIDARGS;
	}

	memcpy (data, device->handshake, SZ_HANDSHAKE);

	return DC_STATUS_SUCCESS;
}


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

	if (size && size != 4)
		return DC_STATUS_INVALIDARGS;

	if (size)
		device->timestamp = array_uint32_le (data);
	else
		device->timestamp = 0;

	return DC_STATUS_SUCCESS;
}


static dc_status_t
reefnet_sensusultra_send_uchar (reefnet_sensusultra_device_t *device, unsigned char value)
{
	dc_status_t status = DC_STATUS_SUCCESS;
	dc_device_t *abstract = (dc_device_t *) device;

	// Wait for the prompt byte.
	unsigned char prompt = 0;
	status = dc_iostream_read (device->iostream, &prompt, 1, NULL);
	if (status != DC_STATUS_SUCCESS) {
		ERROR (abstract->context, "Failed to receive the prompt byte");
		return status;
	}

	// Verify the prompt byte.
	if (prompt != PROMPT) {
		ERROR (abstract->context, "Unexpected answer data.");
		return DC_STATUS_PROTOCOL;
	}

	// Send the value to the device.
	status = dc_iostream_write (device->iostream, &value, 1, NULL);
	if (status != DC_STATUS_SUCCESS) {
		ERROR (abstract->context, "Failed to send the value.");
		return status;
	}

	return DC_STATUS_SUCCESS;
}


static dc_status_t
reefnet_sensusultra_send_ushort (reefnet_sensusultra_device_t *device, unsigned short value)
{
	// Send the least-significant byte.
	unsigned char lsb = value & 0xFF;
	dc_status_t rc = reefnet_sensusultra_send_uchar (device, lsb);
	if (rc != DC_STATUS_SUCCESS)
		return rc;

	// Send the most-significant byte.
	unsigned char msb = (value >> 8) & 0xFF;
	rc = reefnet_sensusultra_send_uchar (device, msb);
	if (rc != DC_STATUS_SUCCESS)
		return rc;

	return DC_STATUS_SUCCESS;
}


static dc_status_t
reefnet_sensusultra_packet (reefnet_sensusultra_device_t *device, unsigned char *data, unsigned int size, unsigned int header)
{
	dc_status_t status = DC_STATUS_SUCCESS;
	dc_device_t *abstract = (dc_device_t *) device;

	assert (size >= header + 2);

	if (device_is_cancelled (abstract))
		return DC_STATUS_CANCELLED;

	// Receive the data packet.
	status = dc_iostream_read (device->iostream, data, size, NULL);
	if (status != DC_STATUS_SUCCESS) {
		ERROR (abstract->context, "Failed to receive the packet.");
		return status;
	}

	// Verify the checksum of the packet.
	unsigned short crc = array_uint16_le (data + size - 2);
	unsigned short ccrc = checksum_crc16_ccitt (data + header, size - header - 2, 0xffff, 0x0000);
	if (crc != ccrc) {
		ERROR (abstract->context, "Unexpected answer checksum.");
		return DC_STATUS_PROTOCOL;
	}

	return DC_STATUS_SUCCESS;
}


static dc_status_t
reefnet_sensusultra_handshake (reefnet_sensusultra_device_t *device, unsigned short value)
{
	// Wake-up the device.
	unsigned char handshake[SZ_HANDSHAKE + 2] = {0};
	dc_status_t rc = reefnet_sensusultra_packet (device, handshake, sizeof (handshake), 0);
	if (rc != DC_STATUS_SUCCESS)
		return rc;

	// Store the clock calibration values.
	device->systime = dc_datetime_now ();
	device->devtime = array_uint32_le (handshake + 4);

	// Store the handshake packet.
	memcpy (device->handshake, handshake, SZ_HANDSHAKE);

	// Emit a clock event.
	dc_event_clock_t clock;
	clock.systime = device->systime;
	clock.devtime = device->devtime;
	device_event_emit (&device->base, DC_EVENT_CLOCK, &clock);

	// Emit a device info event.
	dc_event_devinfo_t devinfo;
	devinfo.model = handshake[1];
	devinfo.firmware = handshake[0];
	devinfo.serial = array_uint16_le (handshake + 2);
	device_event_emit (&device->base, DC_EVENT_DEVINFO, &devinfo);

	// Emit a vendor event.
	dc_event_vendor_t vendor;
	vendor.data = device->handshake;
	vendor.size = sizeof (device->handshake);
	device_event_emit (&device->base, DC_EVENT_VENDOR, &vendor);

	// Send the instruction code to the device.
	rc = reefnet_sensusultra_send_ushort (device, value);
	if (rc != DC_STATUS_SUCCESS)
		return rc;

	return DC_STATUS_SUCCESS;
}


static dc_status_t
reefnet_sensusultra_page (reefnet_sensusultra_device_t *device, unsigned char *data, unsigned int size, unsigned int pagenum)
{
	dc_device_t *abstract = (dc_device_t *) device;

	assert (size >= SZ_PACKET + 4);

	unsigned int nretries = 0;
	dc_status_t rc = DC_STATUS_SUCCESS;
	while ((rc = reefnet_sensusultra_packet (device, data, size, 2)) != DC_STATUS_SUCCESS) {
		// Automatically discard a corrupted packet,
		// and request a new one.
		if (rc != DC_STATUS_PROTOCOL)
			return rc;

		// Abort if the maximum number of retries is reached.
		if (nretries++ >= MAXRETRIES)
			return rc;

		// Reject the packet.
		rc = reefnet_sensusultra_send_uchar (device, REJECT);
		if (rc != DC_STATUS_SUCCESS)
			return rc;
	}

	// Verify the page number.
	unsigned int page = array_uint16_le (data);
	if (page != pagenum) {
		ERROR (abstract->context, "Unexpected page number.");
		return DC_STATUS_PROTOCOL;
	}

	return DC_STATUS_SUCCESS;
}


static dc_status_t
reefnet_sensusultra_send (reefnet_sensusultra_device_t *device, unsigned short command)
{
	// Flush the input and output buffers.
	dc_iostream_purge (device->iostream, DC_DIRECTION_ALL);

	// Wake-up the device and send the instruction code.
	unsigned int nretries = 0;
	dc_status_t rc = DC_STATUS_SUCCESS;
	while ((rc = reefnet_sensusultra_handshake (device, command)) != DC_STATUS_SUCCESS) {
		// Automatically discard a corrupted handshake packet,
		// and wait for the next one.
		if (rc != DC_STATUS_PROTOCOL && rc != DC_STATUS_TIMEOUT)
			return rc;

		// Abort if the maximum number of retries is reached.
		if (nretries++ >= MAXRETRIES)
			return rc;

		// According to the developers guide, a 250 ms delay is suggested to
		// guarantee that the prompt byte sent after the handshake packet is
		// not accidentally buffered by the host and (mis)interpreted as part
		// of the next packet.

		dc_iostream_sleep (device->iostream, 250);
		dc_iostream_purge (device->iostream, DC_DIRECTION_ALL);
	}

	return DC_STATUS_SUCCESS;
}


static dc_status_t
reefnet_sensusultra_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
{
	reefnet_sensusultra_device_t *device = (reefnet_sensusultra_device_t*) abstract;

	// Pre-allocate the required amount of memory.
	if (!dc_buffer_reserve (buffer, SZ_MEMORY)) {
		ERROR (abstract->context, "Insufficient buffer space available.");
		return DC_STATUS_NOMEMORY;
	}

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

	// Wake-up the device and send the instruction code.
	dc_status_t rc = reefnet_sensusultra_send (device, 0xB421);
	if (rc != DC_STATUS_SUCCESS)
		return rc;

	unsigned int nbytes = 0;
	unsigned int npages = 0;
	while (nbytes < SZ_MEMORY) {
		// Receive the packet.
		unsigned char packet[SZ_PACKET + 4] = {0};
		rc = reefnet_sensusultra_page (device, packet, sizeof (packet), npages);
		if (rc != DC_STATUS_SUCCESS)
			return rc;

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

		// Prepend the packet to the buffer.
		if (!dc_buffer_prepend (buffer, packet + 2, SZ_PACKET)) {
			ERROR (abstract->context, "Insufficient buffer space available.");
			return DC_STATUS_NOMEMORY;
		}

		// Accept the packet.
		rc = reefnet_sensusultra_send_uchar (device, ACCEPT);
		if (rc != DC_STATUS_SUCCESS)
			return rc;

		nbytes += SZ_PACKET;
		npages++;
	}

	return DC_STATUS_SUCCESS;
}


dc_status_t
reefnet_sensusultra_device_read_user (dc_device_t *abstract, unsigned char *data, unsigned int size)
{
	reefnet_sensusultra_device_t *device = (reefnet_sensusultra_device_t*) abstract;

	if (!ISINSTANCE (abstract))
		return DC_STATUS_INVALIDARGS;

	if (size < SZ_USER) {
		ERROR (abstract->context, "Insufficient buffer space available.");
		return DC_STATUS_INVALIDARGS;
	}

	// Wake-up the device and send the instruction code.
	dc_status_t rc = reefnet_sensusultra_send (device, 0xB420);
	if (rc != DC_STATUS_SUCCESS)
		return rc;

	unsigned int nbytes = 0;
	unsigned int npages = 0;
	while (nbytes < SZ_USER) {
		// Receive the packet.
		unsigned char packet[SZ_PACKET + 4] = {0};
		rc = reefnet_sensusultra_page (device, packet, sizeof (packet), npages);
		if (rc != DC_STATUS_SUCCESS)
			return rc;

		// Append the packet to the buffer.
		memcpy (data + nbytes, packet + 2, SZ_PACKET);

		// Accept the packet.
		rc = reefnet_sensusultra_send_uchar (device, ACCEPT);
		if (rc != DC_STATUS_SUCCESS)
			return rc;

		nbytes += SZ_PACKET;
		npages++;
	}

	return DC_STATUS_SUCCESS;
}


dc_status_t
reefnet_sensusultra_device_write_user (dc_device_t *abstract, const unsigned char *data, unsigned int size)
{
	reefnet_sensusultra_device_t *device = (reefnet_sensusultra_device_t*) abstract;

	if (!ISINSTANCE (abstract))
		return DC_STATUS_INVALIDARGS;

	if (size < SZ_USER) {
		ERROR (abstract->context, "Insufficient buffer space available.");
		return DC_STATUS_INVALIDARGS;
	}

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

	// Wake-up the device and send the instruction code.
	dc_status_t rc = reefnet_sensusultra_send (device, 0xB430);
	if (rc != DC_STATUS_SUCCESS)
		return rc;

	// Send the data to the device.
	for (unsigned int i = 0; i < SZ_USER; ++i) {
		rc = reefnet_sensusultra_send_uchar (device, data[i]);
		if (rc != DC_STATUS_SUCCESS)
			return rc;

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

	// Send the checksum to the device.
	unsigned short crc = checksum_crc16_ccitt (data, SZ_USER, 0xffff, 0x0000);
	rc = reefnet_sensusultra_send_ushort (device, crc);
	if (rc != DC_STATUS_SUCCESS)
		return rc;

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

	return DC_STATUS_SUCCESS;
}


dc_status_t
reefnet_sensusultra_device_write_parameter (dc_device_t *abstract, reefnet_sensusultra_parameter_t parameter, unsigned int value)
{
	reefnet_sensusultra_device_t *device = (reefnet_sensusultra_device_t*) abstract;

	if (!ISINSTANCE (abstract))
		return DC_STATUS_INVALIDARGS;

	// Set the instruction code and validate the new value.
	unsigned short code = 0;
	switch (parameter) {
	case REEFNET_SENSUSULTRA_PARAMETER_INTERVAL:
		code = 0xB410;
		if (value < 1 || value > 65535)
			return DC_STATUS_INVALIDARGS;
		break;
	case REEFNET_SENSUSULTRA_PARAMETER_THRESHOLD:
		code = 0xB411;
		if (value < 1 || value > 65535)
			return DC_STATUS_INVALIDARGS;
		break;
	case REEFNET_SENSUSULTRA_PARAMETER_ENDCOUNT:
		code = 0xB412;
		if (value < 1 || value > 65535)
			return DC_STATUS_INVALIDARGS;
		break;
	case REEFNET_SENSUSULTRA_PARAMETER_AVERAGING:
		code = 0xB413;
		if (value != 1 && value != 2 && value != 4)
			return DC_STATUS_INVALIDARGS;
		break;
	default:
		return DC_STATUS_INVALIDARGS;
	}

	// Wake-up the device and send the instruction code.
	dc_status_t rc = reefnet_sensusultra_send (device, code);
	if (rc != DC_STATUS_SUCCESS)
		return rc;

	// Send the new value to the device.
	rc = reefnet_sensusultra_send_ushort (device, value);
	if (rc != DC_STATUS_SUCCESS)
		return rc;

	return DC_STATUS_SUCCESS;
}


dc_status_t
reefnet_sensusultra_device_sense (dc_device_t *abstract, unsigned char *data, unsigned int size)
{
	reefnet_sensusultra_device_t *device = (reefnet_sensusultra_device_t*) abstract;

	if (!ISINSTANCE (abstract))
		return DC_STATUS_INVALIDARGS;

	if (size < SZ_SENSE) {
		ERROR (abstract->context, "Insufficient buffer space available.");
		return DC_STATUS_INVALIDARGS;
	}

	// Wake-up the device and send the instruction code.
	dc_status_t rc = reefnet_sensusultra_send (device, 0xB440);
	if (rc != DC_STATUS_SUCCESS)
		return rc;

	// Receive the packet.
	unsigned char package[SZ_SENSE + 2] = {0};
	rc = reefnet_sensusultra_packet (device, package, sizeof (package), 0);
	if (rc != DC_STATUS_SUCCESS)
		return rc;

	memcpy (data, package, SZ_SENSE);

	return DC_STATUS_SUCCESS;
}


static dc_status_t
reefnet_sensusultra_parse (reefnet_sensusultra_device_t *device,
	const unsigned char data[], unsigned int *premaining, unsigned int *pprevious,
	int *aborted, dc_dive_callback_t callback, void *userdata)
{
	const unsigned char header[4] = {0x00, 0x00, 0x00, 0x00};
	const unsigned char footer[4] = {0xFF, 0xFF, 0xFF, 0xFF};

	// Initialize the data stream pointers.
	const unsigned char *current  = data + *premaining;
	const unsigned char *previous = data + *pprevious;

	// Search the data stream for header markers.
	while ((current = array_search_backward (data, current - data, header, sizeof (header))) != NULL) {
		// Move the pointer to the begin of the header.
		current -= sizeof (header);

		// If there is a sequence of more than 4 zero bytes present, the header
		// marker is located at the start of this sequence, not the end.
		while (current > data && current[-1] == 0x00)
			current--;

		// Once a header marker is found, start searching
		// for the corresponding footer marker. The search is
		// now limited to the start of the previous dive.
		if (previous - current >= 16) {
			previous = array_search_forward (current + 16, previous - current - 16, footer, sizeof (footer));
		} else {
			previous = NULL;
		}

		// Skip dives without a footer marker.
		if (previous) {
			// Move the pointer to the end of the footer.
			previous += sizeof (footer);

			// Automatically abort when a dive is older than the provided timestamp.
			unsigned int timestamp = array_uint32_le (current + 4);
			if (device && timestamp <= device->timestamp) {
				if (aborted)
					*aborted = 1;
				return DC_STATUS_SUCCESS;
			}

			if (callback && !callback (current, previous - current, current + 4, 4, userdata)) {
				if (aborted)
					*aborted = 1;
				return DC_STATUS_SUCCESS;
			}
		}

		// Prepare for the next iteration.
		previous = current;

		// Return the current state.
		*premaining = *pprevious = current - data;
	}

	// Return the current state.
	*premaining = sizeof (header) - 1;
	if (*premaining > *pprevious)
		*premaining = *pprevious;

	if (aborted)
		*aborted = 0;

	return DC_STATUS_SUCCESS;
}


static dc_status_t
reefnet_sensusultra_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata)
{
	reefnet_sensusultra_device_t *device = (reefnet_sensusultra_device_t*) abstract;

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

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

	// Wake-up the device and send the instruction code.
	dc_status_t rc = reefnet_sensusultra_send (device, 0xB421);
	if (rc != DC_STATUS_SUCCESS) {
		dc_buffer_free (buffer);
		return rc;
	}

	// Initialize the state for the incremental parser.
	unsigned int remaining = 0;
	unsigned int previous = 0;

	unsigned int nbytes = 0;
	unsigned int npages = 0;
	while (nbytes < SZ_MEMORY) {
		// Receive the packet.
		unsigned char packet[SZ_PACKET + 4] = {0};
		rc = reefnet_sensusultra_page (device, packet, sizeof (packet), npages);
		if (rc != DC_STATUS_SUCCESS) {
			dc_buffer_free (buffer);
			return rc;
		}

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

		// Abort the transfer if the page contains no useful data.
		if (array_isequal (packet + 2, SZ_PACKET, 0xFF) && nbytes != 0)
			break;

		// Prepend the packet to the buffer.
		if (!dc_buffer_prepend (buffer, packet + 2, SZ_PACKET)) {
			dc_buffer_free (buffer);
			ERROR (abstract->context, "Insufficient buffer space available.");
			return DC_STATUS_NOMEMORY;
		}

		// Update the parser state.
		remaining += SZ_PACKET;
		previous += SZ_PACKET;

		// Parse the page data.
		int aborted = 0;
		rc = reefnet_sensusultra_parse (device, dc_buffer_get_data (buffer),
			&remaining, &previous, &aborted, callback, userdata);
		if (rc != DC_STATUS_SUCCESS) {
			dc_buffer_free (buffer);
			return rc;
		}
		if (aborted)
			break;

		// Accept the packet.
		rc = reefnet_sensusultra_send_uchar (device, ACCEPT);
		if (rc != DC_STATUS_SUCCESS) {
			dc_buffer_free (buffer);
			return rc;
		}

		nbytes += SZ_PACKET;
		npages++;
	}

	dc_buffer_free (buffer);

	return DC_STATUS_SUCCESS;
}