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

divesoft_freedom.c (18307B)


      1 /*
      2  * libdivecomputer
      3  *
      4  * Copyright (C) 2023 Jan MatouĊĦek, Jef Driesen
      5  *
      6  * This library is free software; you can redistribute it and/or
      7  * modify it under the terms of the GNU Lesser General Public
      8  * License as published by the Free Software Foundation; either
      9  * version 2.1 of the License, or (at your option) any later version.
     10  *
     11  * This library is distributed in the hope that it will be useful,
     12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     14  * Lesser General Public License for more details.
     15  *
     16  * You should have received a copy of the GNU Lesser General Public
     17  * License along with this library; if not, write to the Free Software
     18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
     19  * MA 02110-1301 USA
     20  */
     21 
     22 #include <string.h>
     23 #include <stdlib.h>
     24 #include <stdio.h>
     25 
     26 #include "divesoft_freedom.h"
     27 #include "context-private.h"
     28 #include "device-private.h"
     29 #include "platform.h"
     30 #include "checksum.h"
     31 #include "array.h"
     32 #include "hdlc.h"
     33 
     34 #define MAXDATA 256
     35 
     36 #define HEADER_SIGNATURE_V1 0x45766944 // "DivE"
     37 #define HEADER_SIGNATURE_V2 0x45566944 // "DiVE"
     38 
     39 #define HEADER_SIZE_V1 32
     40 #define HEADER_SIZE_V2 64
     41 
     42 #define RECORD_SIZE 16
     43 #define FINGERPRINT_SIZE 20
     44 
     45 #define INVALID     0xFFFFFFFF
     46 #define COMPRESSION 1
     47 #define DIRECTION   1
     48 #define NRECORDS    100
     49 
     50 #define DEVICE_CCR_CU      1  // Liberty HW rev. 1.X
     51 #define DEVICE_FREEDOM     2  // Freedom HW rev. 2.X
     52 #define DEVICE_FREEDOM3    5  // Freedom HW rev. 3.X
     53 #define DEVICE_CCR_CU15    10 // Liberty HW rev. 2.X, Bluetooth enabled
     54 #define DEVICE_FREEDOM4    19 // Freedom HW rev. 4.X, Bluetooth enabled
     55 
     56 typedef enum message_t {
     57 	MSG_ECHO = 0,
     58 	MSG_RESULT = 1,
     59 	MSG_CONNECT = 2,
     60 	MSG_CONNECTED = 3,
     61 	MSG_VERSION = 4,
     62 	MSG_VERSION_RSP = 5,
     63 	MSG_DIVE_DATA = 64,
     64 	MSG_DIVE_DATA_RSP = 65,
     65 	MSG_DIVE_LIST = 66,
     66 	MSG_DIVE_LIST_V1 = 67,
     67 	MSG_DIVE_LIST_V2 = 71,
     68 } message_t;
     69 
     70 typedef struct divesoft_freedom_device_t {
     71 	dc_device_t base;
     72 	dc_iostream_t *iostream;
     73 	unsigned char fingerprint[FINGERPRINT_SIZE];
     74 	unsigned int seqnum;
     75 } divesoft_freedom_device_t;
     76 
     77 static dc_status_t divesoft_freedom_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size);
     78 static dc_status_t divesoft_freedom_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata);
     79 static dc_status_t divesoft_freedom_device_close (dc_device_t *device);
     80 
     81 static const dc_device_vtable_t divesoft_freedom_device_vtable = {
     82 	sizeof(divesoft_freedom_device_t),
     83 	DC_FAMILY_DIVESOFT_FREEDOM,
     84 	divesoft_freedom_device_set_fingerprint, /* set_fingerprint */
     85 	NULL, /* read */
     86 	NULL, /* write */
     87 	NULL, /* dump */
     88 	divesoft_freedom_device_foreach, /* foreach */
     89 	NULL, /* timesync */
     90 	divesoft_freedom_device_close, /* close */
     91 };
     92 
     93 static dc_status_t
     94 divesoft_freedom_send (divesoft_freedom_device_t *device, message_t message, const unsigned char data[], size_t size)
     95 {
     96 	dc_status_t status = DC_STATUS_SUCCESS;
     97 	dc_device_t *abstract = (dc_device_t *) device;
     98 
     99 	size_t nbytes = 0, count = 0;
    100 	while (1) {
    101 		size_t len = size - nbytes;
    102 		if (len > MAXDATA)
    103 			len = MAXDATA;
    104 
    105 		unsigned int islast = nbytes + len == size;
    106 
    107 		unsigned char packet[6 + MAXDATA + 2] = {0};
    108 		packet[0] = ((count & 0x0F) << 4) | (device->seqnum & 0x0F);
    109 		packet[1] = 0x80 | (islast << 6);
    110 		array_uint16_le_set (packet + 2, message);
    111 		array_uint16_le_set (packet + 4, len);
    112 		if (len) {
    113 			memcpy (packet + 6, data + nbytes, len);
    114 		}
    115 		unsigned short crc = checksum_crc16r_ccitt (packet, len + 6, 0xFFFF, 0xFFFF);
    116 		array_uint16_le_set (packet + 6 + len, crc);
    117 
    118 		HEXDUMP (abstract->context, DC_LOGLEVEL_DEBUG, "cmd", packet, 6 + len + 2);
    119 
    120 		status = dc_iostream_write (device->iostream, packet, 6 + len + 2, NULL);
    121 		if (status != DC_STATUS_SUCCESS) {
    122 			ERROR (abstract->context, "Failed to send the packet.");
    123 			return status;
    124 		}
    125 
    126 		nbytes += len;
    127 		count++;
    128 
    129 		if (islast)
    130 			break;
    131 	}
    132 
    133 	return status;
    134 }
    135 
    136 static dc_status_t
    137 divesoft_freedom_recv (divesoft_freedom_device_t *device, dc_event_progress_t *progress, message_t *message, dc_buffer_t *buffer)
    138 {
    139 	dc_status_t status = DC_STATUS_SUCCESS;
    140 	dc_device_t *abstract = (dc_device_t *) device;
    141 	unsigned int msg = INVALID;
    142 
    143 	unsigned int count = 0;
    144 	while (1) {
    145 		size_t len = 0;
    146 		unsigned char packet[6 + MAXDATA + 2] = {0};
    147 		status = dc_iostream_read (device->iostream, packet, sizeof(packet), &len);
    148 		if (status != DC_STATUS_SUCCESS) {
    149 			ERROR (abstract->context, "Failed to receive the packet.");
    150 			return status;
    151 		}
    152 
    153 		HEXDUMP (abstract->context, DC_LOGLEVEL_DEBUG, "rcv", packet, len);
    154 
    155 		if (len < 8) {
    156 			ERROR (abstract->context, "Unexpected packet length (" DC_PRINTF_SIZE ").", len);
    157 			return DC_STATUS_PROTOCOL;
    158 		}
    159 
    160 		unsigned int seqnum = packet[0];
    161 		unsigned int flags = packet[1];
    162 		unsigned int type = array_uint16_le (packet + 2);
    163 		unsigned int length = array_uint16_le (packet + 4);
    164 
    165 		unsigned int expected = ((count & 0x0F) << 4) | (device->seqnum & 0x0F);
    166 		if (seqnum != expected) {
    167 			ERROR (abstract->context, "Unexpected packet sequence number (%u %u).", seqnum, expected);
    168 			return DC_STATUS_PROTOCOL;
    169 		}
    170 
    171 		if ((flags & ~0x40) != 0) {
    172 			ERROR (abstract->context, "Unexpected packet flags (%u).", flags);
    173 			return DC_STATUS_PROTOCOL;
    174 		}
    175 
    176 		if (length != len - 8) {
    177 			ERROR (abstract->context, "Unexpected packet length (%u " DC_PRINTF_SIZE ").", length, len - 8);
    178 			return DC_STATUS_PROTOCOL;
    179 		}
    180 
    181 		if (msg == INVALID) {
    182 			msg = type;
    183 		} else if (msg != type) {
    184 			ERROR (abstract->context, "Unexpected packet type (%u).", msg);
    185 			return DC_STATUS_PROTOCOL;
    186 		}
    187 
    188 		unsigned short crc = array_uint16_le (packet + len - 2);
    189 		unsigned short ccrc = checksum_crc16r_ccitt (packet, len - 2, 0xFFFF, 0xFFFF);
    190 		if (crc != ccrc) {
    191 			ERROR (abstract->context, "Unexpected packet checksum (%04x %04x).", crc, ccrc);
    192 			return DC_STATUS_PROTOCOL;
    193 		}
    194 
    195 		// Update and emit a progress event.
    196 		if (progress) {
    197 			progress->current += len - 8;
    198 			// Limit the progress to the maximum size. This could happen if the
    199 			// dive computer sends more data than requested for some reason.
    200 			if (progress->current > progress->maximum) {
    201 				WARNING (abstract->context, "Progress exceeds the maximum size.");
    202 				progress->current = progress->maximum;
    203 			}
    204 			device_event_emit (abstract, DC_EVENT_PROGRESS, progress);
    205 		}
    206 
    207 		if (!dc_buffer_append (buffer, packet + 6, len - 8)) {
    208 			ERROR (abstract->context, "Insufficient buffer space available.");
    209 			return DC_STATUS_NOMEMORY;
    210 		}
    211 
    212 		count++;
    213 
    214 		if (flags & 0x40)
    215 			break;
    216 	}
    217 
    218 	if (message)
    219 		*message = msg;
    220 
    221 	return status;
    222 }
    223 
    224 static dc_status_t
    225 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)
    226 {
    227 	dc_status_t status = DC_STATUS_SUCCESS;
    228 	dc_device_t *abstract = (dc_device_t *) device;
    229 
    230 	if (device_is_cancelled (abstract))
    231 		return DC_STATUS_CANCELLED;
    232 
    233 	device->seqnum++;
    234 
    235 	status = divesoft_freedom_send (device, cmd, data, size);
    236 	if (status != DC_STATUS_SUCCESS) {
    237 		ERROR (abstract->context, "Failed to send the command.");
    238 		return status;
    239 	}
    240 
    241 	status = divesoft_freedom_recv (device, progress, msg, buffer);
    242 	if(status != DC_STATUS_SUCCESS) {
    243 		ERROR (abstract->context, "Failed to receive response.");
    244 		return status;
    245 	}
    246 
    247 	return status;
    248 }
    249 
    250 static dc_status_t
    251 divesoft_freedom_download (divesoft_freedom_device_t *device, message_t cmd, const unsigned char cdata[], size_t csize, unsigned char rdata[], size_t rsize)
    252 {
    253 	dc_status_t status = DC_STATUS_SUCCESS;
    254 	dc_device_t *abstract = (dc_device_t *) device;
    255 
    256 	dc_buffer_t *buffer = dc_buffer_new (rsize);
    257 	if (buffer == NULL) {
    258 		ERROR (abstract->context, "Failed to allocate memory.");
    259 		status = DC_STATUS_NOMEMORY;
    260 		goto error_exit;
    261 	}
    262 
    263 	message_t msg = MSG_ECHO;
    264 	status = divesoft_freedom_transfer (device, NULL, cmd, cdata, csize, &msg, buffer);
    265 	if (status != DC_STATUS_SUCCESS) {
    266 		ERROR (abstract->context, "Failed to transfer the packet.");
    267 		goto error_free;
    268 	}
    269 
    270 	if (msg != cmd + 1) {
    271 		ERROR (abstract->context, "Unexpected response message (%u).", msg);
    272 		status = DC_STATUS_PROTOCOL;
    273 		goto error_free;
    274 	}
    275 
    276 	size_t length = dc_buffer_get_size (buffer);
    277 	if (length != rsize) {
    278 		ERROR (abstract->context, "Unexpected response length (" DC_PRINTF_SIZE " " DC_PRINTF_SIZE ").", length, rsize);
    279 		status =  DC_STATUS_PROTOCOL;
    280 		goto error_free;
    281 	}
    282 
    283 	if (rsize) {
    284 		memcpy (rdata, dc_buffer_get_data (buffer), rsize);
    285 	}
    286 
    287 error_free:
    288 	dc_buffer_free (buffer);
    289 error_exit:
    290 	return status;
    291 }
    292 
    293 dc_status_t
    294 divesoft_freedom_device_open (dc_device_t **out, dc_context_t *context, dc_iostream_t *iostream)
    295 {
    296 	dc_status_t status = DC_STATUS_SUCCESS;
    297 	divesoft_freedom_device_t *device = NULL;
    298 
    299 	if (out == NULL)
    300 		return DC_STATUS_INVALIDARGS;
    301 
    302 	// Allocate memory.
    303 	device = (divesoft_freedom_device_t *) dc_device_allocate (context, &divesoft_freedom_device_vtable);
    304 	if (device == NULL) {
    305 		ERROR (context, "Failed to allocate memory.");
    306 		return DC_STATUS_NOMEMORY;
    307 	}
    308 
    309 	// Set the default values.
    310 	device->iostream = NULL;
    311 	memset(device->fingerprint, 0, sizeof(device->fingerprint));
    312 	device->seqnum = 0;
    313 
    314 	// Setup the HDLC communication.
    315 	status = dc_hdlc_open (&device->iostream, context, iostream, 244, 244);
    316 	if (status != DC_STATUS_SUCCESS) {
    317 		ERROR (context, "Failed to create the HDLC stream.");
    318 		goto error_free;
    319 	}
    320 
    321 	// Set the serial communication protocol (115200 8N1).
    322 	status = dc_iostream_configure (device->iostream, 115200, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
    323 	if (status != DC_STATUS_SUCCESS) {
    324 		ERROR (context, "Failed to set the terminal attributes.");
    325 		goto error_free_hdlc;
    326 	}
    327 
    328 	// Set the timeout for receiving data (3000ms).
    329 	status = dc_iostream_set_timeout (device->iostream, 3000);
    330 	if (status != DC_STATUS_SUCCESS) {
    331 		ERROR (context, "Failed to set the timeout.");
    332 		goto error_free_hdlc;
    333 	}
    334 
    335 	// Initiate the connection with the dive computer.
    336 	const char client[] = "libdivecomputer";
    337 	unsigned char cmd_connect[2 + sizeof(client) - 1] = {0};
    338 	array_uint16_le_set (cmd_connect, COMPRESSION);
    339 	memcpy (cmd_connect + 2, client, sizeof(client) - 1);
    340 	unsigned char rsp_connect[36] = {0};
    341 	status = divesoft_freedom_download (device, MSG_CONNECT, cmd_connect, sizeof(cmd_connect), rsp_connect, sizeof(rsp_connect));
    342 	if (status != DC_STATUS_SUCCESS) {
    343 		ERROR (context, "Failed to connect to the device.");
    344 		goto error_free_hdlc;
    345 	}
    346 
    347 	HEXDUMP (context, DC_LOGLEVEL_DEBUG, "Connection", rsp_connect, sizeof(rsp_connect));
    348 
    349 	DEBUG (context, "Connection: compression=%u, protocol=%u.%u, serial=%.16s",
    350 		array_uint16_le (rsp_connect),
    351 		rsp_connect[2], rsp_connect[3],
    352 		rsp_connect + 4);
    353 
    354 	*out = (dc_device_t *) device;
    355 
    356 	return DC_STATUS_SUCCESS;
    357 
    358 error_free_hdlc:
    359 	dc_iostream_close (device->iostream);
    360 error_free:
    361 	dc_device_deallocate ((dc_device_t *) device);
    362 	return status;
    363 }
    364 
    365 static dc_status_t
    366 divesoft_freedom_device_close (dc_device_t *abstract)
    367 {
    368 	divesoft_freedom_device_t *device = (divesoft_freedom_device_t *) abstract;
    369 
    370 	return dc_iostream_close (device->iostream);
    371 }
    372 
    373 static dc_status_t
    374 divesoft_freedom_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size)
    375 {
    376 	divesoft_freedom_device_t *device = (divesoft_freedom_device_t *) abstract;
    377 
    378 	if (size && size != sizeof (device->fingerprint))
    379 		return DC_STATUS_INVALIDARGS;
    380 
    381 	if (size)
    382 		memcpy (device->fingerprint, data, sizeof (device->fingerprint));
    383 	else
    384 		memset (device->fingerprint, 0, sizeof (device->fingerprint));
    385 
    386 	return DC_STATUS_SUCCESS;
    387 }
    388 
    389 static dc_status_t
    390 divesoft_freedom_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata)
    391 {
    392 	dc_status_t status = DC_STATUS_SUCCESS;
    393 	divesoft_freedom_device_t *device = (divesoft_freedom_device_t *) abstract;
    394 
    395 	// Enable progress notifications.
    396 	dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER;
    397 	device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);
    398 
    399 	// Read the device information.
    400 	unsigned char rsp_version[26] = {0};
    401 	status = divesoft_freedom_download (device, MSG_VERSION, NULL, 0, rsp_version, sizeof(rsp_version));
    402 	if (status != DC_STATUS_SUCCESS) {
    403 		ERROR (abstract->context, "Failed to read the device information.");
    404 		goto error_exit;
    405 	}
    406 
    407 	HEXDUMP (abstract->context, DC_LOGLEVEL_DEBUG, "Version", rsp_version, sizeof(rsp_version));
    408 
    409 	DEBUG (abstract->context, "Device: model=%u, hw=%u.%u, sw=%u.%u.%u.%u serial=%.16s",
    410 		rsp_version[0],
    411 		rsp_version[1], rsp_version[2],
    412 		rsp_version[3], rsp_version[4], rsp_version[5],
    413 		array_uint32_le (rsp_version + 6),
    414 		rsp_version + 10);
    415 
    416 	// Emit a device info event.
    417 	dc_event_devinfo_t devinfo;
    418 	devinfo.model = rsp_version[0];
    419 	devinfo.firmware = array_uint24_be (rsp_version + 3);
    420 	devinfo.serial = array_convert_str2num (rsp_version + 10 + 5, 11);
    421 	device_event_emit(abstract, DC_EVENT_DEVINFO, &devinfo);
    422 
    423 	// Allocate memory for the dive list.
    424 	dc_buffer_t *divelist = dc_buffer_new (0);
    425 	if (divelist == NULL) {
    426 		status = DC_STATUS_NOMEMORY;
    427 		goto error_exit;
    428 	}
    429 
    430 	// Allocate memory for the download buffer.
    431 	dc_buffer_t *buffer = dc_buffer_new (NRECORDS * (4 + FINGERPRINT_SIZE + HEADER_SIZE_V2));
    432 	if (buffer == NULL) {
    433 		status = DC_STATUS_NOMEMORY;
    434 		goto error_free_divelist;
    435 	}
    436 
    437 	// Record version and size.
    438 	unsigned int version = 0;
    439 	unsigned int headersize = 0;
    440 	unsigned int recordsize = 0;
    441 
    442 	// Download the dive list.
    443 	unsigned int total = 0;
    444 	unsigned int maxsize = 0;
    445 	unsigned int current = INVALID;
    446 	while (1) {
    447 		// Clear the buffer.
    448 		dc_buffer_clear (buffer);
    449 
    450 		// Prepare the command.
    451 		unsigned char cmd_list[6] = {0};
    452 		array_uint32_le_set (cmd_list, current);
    453 		cmd_list[4] = DIRECTION;
    454 		cmd_list[5] = NRECORDS;
    455 
    456 		// Download the dive list records.
    457 		message_t msg_list = MSG_ECHO;
    458 		status = divesoft_freedom_transfer (device, &progress, MSG_DIVE_LIST, cmd_list, sizeof(cmd_list), &msg_list, buffer);
    459 		if (status != DC_STATUS_SUCCESS) {
    460 			ERROR (abstract->context, "Failed to download the dive list.");
    461 			goto error_free_buffer;
    462 		}
    463 
    464 		// Check the response message type.
    465 		if (msg_list != MSG_DIVE_LIST_V1 && msg_list != MSG_DIVE_LIST_V2) {
    466 			ERROR (abstract->context, "Unexpected response message (%u).", msg_list);
    467 			status = DC_STATUS_PROTOCOL;
    468 			goto error_free_buffer;
    469 		}
    470 
    471 		// Store/check the version.
    472 		if (version == 0) {
    473 			version = msg_list;
    474 			headersize = version == MSG_DIVE_LIST_V1 ?
    475 				HEADER_SIZE_V1 : HEADER_SIZE_V2;
    476 			recordsize = 4 + FINGERPRINT_SIZE + headersize;
    477 		} else if (version != msg_list) {
    478 			ERROR (abstract->context, "Unexpected response message (%u).", msg_list);
    479 			status = DC_STATUS_PROTOCOL;
    480 			goto error_free_buffer;
    481 		}
    482 
    483 		const unsigned char *data = dc_buffer_get_data (buffer);
    484 		size_t size = dc_buffer_get_size (buffer);
    485 
    486 		// Process the records.
    487 		size_t offset = 0, count = 0;
    488 		while (offset + recordsize <= size) {
    489 			// Get the record data.
    490 			unsigned int handle = array_uint32_le (data + offset);
    491 			const unsigned char *fingerprint = data + offset + 4;
    492 			const unsigned char *header = data + offset + 4 + FINGERPRINT_SIZE;
    493 
    494 			// Check the fingerprint data.
    495 			if (memcmp (device->fingerprint, fingerprint, sizeof(device->fingerprint)) == 0) {
    496 				break;
    497 			}
    498 
    499 			// Get the length of the dive.
    500 			unsigned int nrecords = version == MSG_DIVE_LIST_V1 ?
    501 				array_uint32_le (header + 16) & 0x3FFFF :
    502 				array_uint32_le (header + 20);
    503 			unsigned int length = headersize + nrecords * RECORD_SIZE;
    504 
    505 			// Calculate the total and maximum size.
    506 			if (length > maxsize)
    507 				maxsize = length;
    508 			total += length;
    509 
    510 			// Set the handle for the next request.
    511 			current = handle;
    512 
    513 			offset += recordsize;
    514 			count++;
    515 		}
    516 
    517 		// Append the records to the dive list buffer.
    518 		if (!dc_buffer_append (divelist, data, count * recordsize)) {
    519 			ERROR (abstract->context, "Insufficient buffer space available.");
    520 			status = DC_STATUS_NOMEMORY;
    521 			goto error_free_buffer;
    522 		}
    523 
    524 		// Stop downloading if there are no more records.
    525 		if (count < NRECORDS)
    526 			break;
    527 	}
    528 
    529 	// Update and emit a progress event.
    530 	progress.maximum = progress.current + total;
    531 	device_event_emit(abstract, DC_EVENT_PROGRESS, &progress);
    532 
    533 	// Reserve memory for the largest dive.
    534 	dc_buffer_reserve (buffer, maxsize);
    535 
    536 	const unsigned char *data = dc_buffer_get_data (divelist);
    537 	size_t size = dc_buffer_get_size (divelist);
    538 
    539 	size_t offset = 0;
    540 	while (offset + recordsize <= size) {
    541 		// Get the record data.
    542 		unsigned int handle = array_uint32_le (data + offset);
    543 		const unsigned char *fingerprint = data + offset + 4;
    544 		const unsigned char *header = data + offset + 4 + FINGERPRINT_SIZE;
    545 
    546 		// Get the length of the dive.
    547 		unsigned int nrecords = version == MSG_DIVE_LIST_V1 ?
    548 			array_uint32_le (header + 16) & 0x3FFFF :
    549 			array_uint32_le (header + 20);
    550 		unsigned int length = headersize + nrecords * RECORD_SIZE;
    551 
    552 		// Clear the buffer.
    553 		dc_buffer_clear (buffer);
    554 
    555 		// Prepare the command.
    556 		unsigned char cmd_dive[12] = {0};
    557 		array_uint32_le_set (cmd_dive + 0, handle);
    558 		array_uint32_le_set (cmd_dive + 4, 0);
    559 		array_uint32_le_set (cmd_dive + 8, length);
    560 
    561 		// Download the dive.
    562 		message_t msg_dive = MSG_ECHO;
    563 		status = divesoft_freedom_transfer (device, &progress, MSG_DIVE_DATA, cmd_dive, sizeof(cmd_dive), &msg_dive, buffer);
    564 		if (status != DC_STATUS_SUCCESS) {
    565 			ERROR (abstract->context, "Failed to download the dive.");
    566 			goto error_free_buffer;
    567 		}
    568 
    569 		// Check the response message type.
    570 		if (msg_dive != MSG_DIVE_DATA_RSP) {
    571 			ERROR (abstract->context, "Unexpected response message (%u).", msg_dive);
    572 			status = DC_STATUS_PROTOCOL;
    573 			goto error_free_buffer;
    574 		}
    575 
    576 		// Verify both dive headers are identical.
    577 		if (dc_buffer_get_size (buffer) < headersize ||
    578 			memcmp (header, dc_buffer_get_data (buffer), headersize) != 0) {
    579 			ERROR (abstract->context, "Unexpected profile header.");
    580 			status = DC_STATUS_PROTOCOL;
    581 			goto error_free_buffer;
    582 		}
    583 
    584 		if (callback && !callback (dc_buffer_get_data(buffer), dc_buffer_get_size(buffer), fingerprint, sizeof (device->fingerprint), userdata)) {
    585 			break;
    586 		}
    587 
    588 		offset += recordsize;
    589 	}
    590 
    591 error_free_buffer:
    592 	dc_buffer_free (buffer);
    593 error_free_divelist:
    594 	dc_buffer_free (divelist);
    595 error_exit:
    596 	return status;
    597 }