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

oceans_s1.c (19754B)


      1 /*
      2  * libdivecomputer
      3  *
      4  * Copyright (C) 2020 Linus Torvalds
      5  * Copyright (C) 2022 Jef Driesen
      6  *
      7  * This library is free software; you can redistribute it and/or
      8  * modify it under the terms of the GNU Lesser General Public
      9  * License as published by the Free Software Foundation; either
     10  * version 2.1 of the License, or (at your option) any later version.
     11  *
     12  * This library is distributed in the hope that it will be useful,
     13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     15  * Lesser General Public License for more details.
     16  *
     17  * You should have received a copy of the GNU Lesser General Public
     18  * License along with this library; if not, write to the Free Software
     19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
     20  * MA 02110-1301 USA
     21  */
     22 
     23 #include <string.h> // memcmp, memcpy
     24 #include <stdlib.h> // malloc, free
     25 #include <stdarg.h>
     26 #include <stdio.h>
     27 
     28 #include "oceans_s1.h"
     29 #include "oceans_s1_common.h"
     30 #include "context-private.h"
     31 #include "device-private.h"
     32 #include "platform.h"
     33 #include "checksum.h"
     34 #include "array.h"
     35 
     36 #define SOH 0x01
     37 #define EOT 0x04
     38 #define ACK 0x06
     39 #define NAK 0x15
     40 #define CAN 0x18
     41 #define CRC 0x43
     42 
     43 #define SZ_PACKET 256
     44 #define SZ_XMODEM 512
     45 
     46 #define SZ_FINGERPRINT 8
     47 
     48 typedef struct oceans_s1_dive_t {
     49 	struct oceans_s1_dive_t *next;
     50 	dc_ticks_t timestamp;
     51 	unsigned int number;
     52 } oceans_s1_dive_t;
     53 
     54 typedef struct oceans_s1_device_t {
     55 	dc_device_t base;
     56 	dc_iostream_t *iostream;
     57 	dc_ticks_t timestamp;
     58 } oceans_s1_device_t;
     59 
     60 static dc_status_t oceans_s1_device_set_fingerprint(dc_device_t *abstract, const unsigned char data[], unsigned int size);
     61 static dc_status_t oceans_s1_device_foreach(dc_device_t *abstract, dc_dive_callback_t callback, void *userdata);
     62 static dc_status_t oceans_s1_device_timesync(dc_device_t *abstract, const dc_datetime_t *datetime);
     63 
     64 static const dc_device_vtable_t oceans_s1_device_vtable = {
     65 	sizeof(oceans_s1_device_t),
     66 	DC_FAMILY_OCEANS_S1,
     67 	oceans_s1_device_set_fingerprint, /* set_fingerprint */
     68 	NULL, /* read */
     69 	NULL, /* write */
     70 	NULL, /* dump */
     71 	oceans_s1_device_foreach, /* foreach */
     72 	oceans_s1_device_timesync, /* timesync */
     73 	NULL, /* close */
     74 };
     75 
     76 /*
     77  * Oceans S1 initial sequence (all ASCII text with newlines):
     78  *
     79  *    Cmd               Reply
     80  *
     81  *    utc\n             utc>ok 1592912375\r\n
     82  *    battery\n         battery>ok 59%\r\n
     83  *    version\n         version>ok 1.1 42a7e564\r\n
     84  *    utc 1592912364\n  utc>ok\r\n
     85  *    units 1\n         units>ok\r\n
     86  *    dllist\n          dllist>xmr\r\n
     87  *
     88  * At this point, the dive computer switches to the XMODEM protocol and
     89  * the sequence is no longer single packets with a full line with newline
     90  * termination.
     91  *
     92  * The actual payload remains ASCII text (note the single space indentation):
     93  *
     94  *    divelog v1,10s/sample
     95  *     dive 1,0,21,1591372057
     96  *     continue 612,10
     97  *     enddive 3131,496
     98  *     dive 2,0,21,1591372925
     99  *     enddive 1535,277
    100  *     dive 3,0,32,1591463368
    101  *     enddive 1711,4515
    102  *     dive 4,0,32,1591961688
    103  *     continue 300,45
    104  *     continue 391,238
    105  *     continue 420,126
    106  *     continue 236,17
    107  *     enddive 1087,2636
    108  *    endlog
    109  *
    110  * Because the XMODEM protocol uses fixed size packets (512 bytes), the last
    111  * packet is padded with newline characters.
    112  *
    113  * Then it goes back to line-mode:
    114  *
    115  *    dlget 4 5\n       dlget>xmr\r\n
    116  *
    117  * and the data is again transferred using the XMODEM protocol. The payload is
    118  * also ASCII text (note the space indentation again):
    119  *
    120  *    divelog v1,10s/sample
    121  *     dive 4,0,32,1591961688
    122  *      365,13,1
    123  *      382,13,51456
    124  *      367,13,16640
    125  *      381,13,49408
    126  *      375,13,24576
    127  *      355,13,16384
    128  *      346,13,16384
    129  *      326,14,16384
    130  *      355,14,16384
    131  *      394,14,24576
    132  *      397,14,16384
    133  *      434,14,49152
    134  *      479,14,49152
    135  *      488,14,16384
    136  *      556,14,57344
    137  *      616,14,49152
    138  *      655,14,49152
    139  *      738,14,49152
    140  *      800,14,57344
    141  *      800,14,49408
    142  *      834,14,16640
    143  *      871,14,24832
    144  *      860,14,16640
    145  *      860,14,16640
    146  *      815,14,24832
    147  *      738,14,16640
    148  *      707,14,16640
    149  *      653,14,24832
    150  *      647,13,16640
    151  *      670,13,16640
    152  *      653,13,24832
    153  *      ...
    154  *     continue 236,17
    155  *      227,13,57600
    156  *      238,14,16640
    157  *      267,14,24832
    158  *      283,14,16384
    159  *      272,14,16384
    160  *      303,14,24576
    161  *      320,14,16384
    162  *      318,14,16384
    163  *      318,14,16384
    164  *      335,14,24576
    165  *      332,14,16384
    166  *      386,14,16384
    167  *      417,14,24576
    168  *      244,14,16640
    169  *      71,14,16640
    170  *     enddive 1087,2636
    171  *    endlog
    172  *
    173  * Where the samples seem to be
    174  *  - depth in cm
    175  *  - temperature in °C
    176  *  - events
    177  *
    178  * Repeat with 'dlget 3 4', 'dlget 2 3', 'dlget 1 2'.
    179  *
    180  * Done.
    181  */
    182 
    183 /*
    184  * Add a dive to the dive list, sorted with newest dive first
    185  *
    186  * I'm not sure if the dive list is always presented sorted by the
    187  * Oceans S1, but it arrives in the reverse order of what we want
    188  * (we want newest first, it lists them oldest first). So we need
    189  * to switch the order, and we might as well make sure it's sorted
    190  * while doing that.
    191  */
    192 static void
    193 oceans_s1_list_add (oceans_s1_dive_t **head, oceans_s1_dive_t *dive)
    194 {
    195 	if (head == NULL)
    196 		return;
    197 
    198 	oceans_s1_dive_t *current = *head, *previous = NULL;
    199 	while (current) {
    200 		if (dive->number >= current->number)
    201 			break;
    202 		previous = current;
    203 		current = current->next;
    204 	}
    205 
    206 	if (previous) {
    207 		dive->next = previous->next;
    208 		previous->next = dive;
    209 	} else {
    210 		dive->next = *head;
    211 		*head = dive;
    212 	}
    213 }
    214 
    215 static void
    216 oceans_s1_list_free (oceans_s1_dive_t *head)
    217 {
    218 	oceans_s1_dive_t *current = head;
    219 	while (current) {
    220 		oceans_s1_dive_t *next = current->next;
    221 		free (current);
    222 		current = next;
    223 	}
    224 }
    225 
    226 /*
    227  * The main data is transferred using the XMODEM-CRC protocol.
    228  *
    229  * This variant of the XMODEM protocol uses a sequence of 517 byte packets,
    230  * where each packet has a three byte header, 512 bytes of payload data and a
    231  * two byte CRC checksum. The header is a 'SOH' byte, followed by the block
    232  * number (starting at 1), and the inverse block number (255-block).
    233  *
    234  * We're supposed to start the sequence with a 'CRC' byte, and reply to each
    235  * packet with a 'ACK' byte. When there is no more data, the device will
    236  * send us a 'EOT' packet, which we'll ack with a final 'ACK' byte.
    237  *
    238  * So we get a sequence of:
    239  *
    240  *  01 01 fe <512 bytes> xx xx
    241  *  01 02 fd <512 bytes> xx xx
    242  *  01 03 fc <512 bytes> xx xx
    243  *  01 04 fb <512 bytes> xx xx
    244  *  01 05 fa <512 bytes> xx xx
    245  *  01 06 f9 <512 bytes> xx xx
    246  *  01 07 f8 <512 bytes> xx xx
    247  *  04
    248  *
    249  * And we should reply with an 'ACK' byte for each of those entries.
    250  *
    251  * NOTE! The above is not in single BLE packets, although the
    252  * sequence blocks always start at a packet boundary.
    253  *
    254  * NOTE! The Oceans Android app uses GATT "Write Commands" (0x53), and not
    255  * GATT "Write Requests" (0x12) for sending the XMODEM single byte commands,
    256  * but this difference does not seem to matter.
    257  */
    258 static dc_status_t
    259 oceans_s1_xmodem_packet (oceans_s1_device_t *device, unsigned char seq, unsigned char data[], size_t size)
    260 {
    261 	dc_status_t status = DC_STATUS_SUCCESS;
    262 	unsigned char packet[3 + SZ_XMODEM + 2] = {0};
    263 	size_t nbytes = 0;
    264 
    265 	if (size < SZ_XMODEM)
    266 		return DC_STATUS_INVALIDARGS;
    267 
    268 	status = dc_iostream_read (device->iostream, packet, sizeof(packet), &nbytes);
    269 	if (status != DC_STATUS_SUCCESS) {
    270 		ERROR (device->base.context, "Failed to receive the packet.");
    271 		return status;
    272 	}
    273 
    274 	if (nbytes < 1) {
    275 		ERROR (device->base.context, "Unexpected packet length (" DC_PRINTF_SIZE ").", nbytes);
    276 		return DC_STATUS_PROTOCOL;
    277 	}
    278 
    279 	if (packet[0] == EOT) {
    280 		return DC_STATUS_DONE;
    281 	}
    282 
    283 	if (nbytes < 3) {
    284 		ERROR (device->base.context, "Unexpected packet length (" DC_PRINTF_SIZE ").", nbytes);
    285 		return DC_STATUS_PROTOCOL;
    286 	}
    287 
    288 	if (packet[0] != SOH || packet[1] != seq || packet[1] + packet[2] != 0xFF) {
    289 		ERROR (device->base.context, "Unexpected packet header.");
    290 		return DC_STATUS_PROTOCOL;
    291 	}
    292 
    293 	while (nbytes < sizeof(packet)) {
    294 		size_t received = 0;
    295 		status = dc_iostream_read (device->iostream, packet + nbytes, sizeof(packet) - nbytes, &received);
    296 		if (status != DC_STATUS_SUCCESS) {
    297 			ERROR (device->base.context, "Failed to receive the packet.");
    298 			return status;
    299 		}
    300 
    301 		nbytes += received;
    302 	}
    303 
    304 	unsigned short crc = array_uint16_be (packet + nbytes - 2);
    305 	unsigned short ccrc = checksum_crc16_ccitt (packet + 3, nbytes - 5, 0x0000, 0x0000);
    306 	if (crc != ccrc) {
    307 		ERROR (device->base.context, "Unexpected answer checksum (%04x %04x).", crc, ccrc);
    308 		return DC_STATUS_PROTOCOL;
    309 	}
    310 
    311 	memcpy (data, packet + 3, SZ_XMODEM);
    312 
    313 	return DC_STATUS_SUCCESS;
    314 }
    315 
    316 static dc_status_t
    317 oceans_s1_xmodem_recv (oceans_s1_device_t *device, dc_buffer_t *buffer)
    318 {
    319 	dc_status_t status = DC_STATUS_SUCCESS;
    320 	const unsigned char crc = CRC;
    321 	const unsigned char ack = ACK;
    322 
    323 	dc_buffer_clear (buffer);
    324 
    325 	// Request XMODEM-CRC mode.
    326 	status = dc_iostream_write (device->iostream, &crc, 1, NULL);
    327 	if (status != DC_STATUS_SUCCESS)
    328 		return status;
    329 
    330 	unsigned char seq = 1;
    331 	while (1) {
    332 		// Receive the XMODEM data packet.
    333 		unsigned char packet[SZ_XMODEM] = {0};
    334 		status = oceans_s1_xmodem_packet (device, seq, packet, sizeof(packet));
    335 		if (status != DC_STATUS_SUCCESS) {
    336 			if (status == DC_STATUS_DONE)
    337 				break;
    338 			return status;
    339 		}
    340 
    341 		dc_buffer_append (buffer, packet, sizeof(packet));
    342 
    343 		// Ack the data packet.
    344 		status = dc_iostream_write (device->iostream, &ack, 1, NULL);
    345 		if (status != DC_STATUS_SUCCESS)
    346 			return status;
    347 
    348 		seq++;
    349 	}
    350 
    351 	// Ack the EOT packet.
    352 	status = dc_iostream_write (device->iostream, &ack, 1, NULL);
    353 	if (status != DC_STATUS_SUCCESS) {
    354 		return status;
    355 	}
    356 
    357 	// Find trailing newline(s).
    358 	size_t size = dc_buffer_get_size (buffer);
    359 	unsigned char *data = dc_buffer_get_data (buffer);
    360 	while (size > 1 && (data[size - 2] == '\r' || data[size - 2] == '\n'))
    361 		size--;
    362 
    363 	// Remove trailing newline(s).
    364 	dc_buffer_slice (buffer, 0, size);
    365 
    366 	return DC_STATUS_SUCCESS;
    367 }
    368 
    369 static dc_status_t DC_ATTR_FORMAT_PRINTF(6, 7)
    370 oceans_s1_transfer (oceans_s1_device_t *device, dc_buffer_t *buffer, char data[], size_t size, const char *cmd, const char *params, ...)
    371 {
    372 	dc_status_t status = DC_STATUS_SUCCESS;
    373 	char buf[SZ_PACKET + 1] = {0};
    374 	size_t buflen = 0;
    375 
    376 	if (device_is_cancelled (&device->base))
    377 		return DC_STATUS_CANCELLED;
    378 
    379 	size_t cmdlen = strlen (cmd);
    380 	if (buflen + cmdlen > sizeof(buf) - 1) {
    381 		ERROR (device->base.context, "Not enough space for the command string.");
    382 		return DC_STATUS_NOMEMORY;
    383 	}
    384 
    385 	// Copy the command string.
    386 	memcpy (buf, cmd, cmdlen);
    387 	buflen += cmdlen;
    388 
    389 	// Null terminate the buffer.
    390 	buf[buflen] = 0;
    391 
    392 	if (params) {
    393 		if (buflen + 1 > sizeof(buf) - 1) {
    394 			ERROR (device->base.context, "Not enough space for the separator.");
    395 			return DC_STATUS_NOMEMORY;
    396 		}
    397 
    398 		// Append a space.
    399 		buf[buflen++] = ' ';
    400 
    401 		// Null terminate the buffer.
    402 		buf[buflen] = 0;
    403 
    404 		// Append the arguments.
    405 		va_list ap;
    406 		va_start (ap, params);
    407 		int n = dc_platform_vsnprintf (buf + buflen, sizeof(buf) - buflen, params, ap);
    408 		va_end (ap);
    409 		if (n < 0) {
    410 			ERROR (device->base.context, "Not enough space for the arguments.");
    411 			return DC_STATUS_NOMEMORY;
    412 		}
    413 
    414 		buflen += n;
    415 	}
    416 
    417 	DEBUG(device->base.context, "cmd: %s", buf);
    418 
    419 	if (buflen + 1 > sizeof(buf) - 1) {
    420 		ERROR (device->base.context, "Not enough space for the newline.");
    421 		return DC_STATUS_NOMEMORY;
    422 	}
    423 
    424 	// Append a newline.
    425 	buf[buflen++] = '\n';
    426 
    427 	// Null terminate the buffer.
    428 	buf[buflen] = 0;
    429 
    430 	// Send the command.
    431 	status = dc_iostream_write (device->iostream, buf, buflen, NULL);
    432 	if (status != DC_STATUS_SUCCESS) {
    433 		ERROR (device->base.context, "Failed to send the command.");
    434 		return status;
    435 	}
    436 
    437 	// Receive the response.
    438 	size_t nbytes = 0;
    439 	status = dc_iostream_read (device->iostream, buf, sizeof(buf) - 1, &nbytes);
    440 	if (status != DC_STATUS_SUCCESS) {
    441 		ERROR (device->base.context, "Failed to receive the response.");
    442 		return status;
    443 	}
    444 
    445 	// Remove trailing newline(s).
    446 	while (nbytes && (buf[nbytes - 1] == '\r' || buf[nbytes - 1] == '\n'))
    447 		nbytes--;
    448 
    449 	// Null terminate the buffer.
    450 	buf[nbytes] = 0;
    451 
    452 	DEBUG (device->base.context, "rcv: %s", buf);
    453 
    454 	// Verify the response.
    455 	if (strncmp (buf, cmd, cmdlen) != 0) {
    456 		ERROR (device->base.context, "Received unexpected packet data ('%s').", buf);
    457 		return DC_STATUS_PROTOCOL;
    458 	}
    459 
    460 	// Check the type of response.
    461 	// If the response indicates "ok", the payload data is send inline in
    462 	// the remainder of the response packet. If the response indicates "xmr",
    463 	// the payload data is send separately using the XMODEM protocol.
    464 	if (strncmp (buf + cmdlen, ">ok", 3) == 0) {
    465 		// Ignore leading whitespace.
    466 		const char *line = buf + cmdlen + 3;
    467 		while (*line == ' ')
    468 			line++;
    469 
    470 		// Copy the payload data.
    471 		size_t len = nbytes - (line - buf);
    472 		if (size) {
    473 			if (len + 1 > size) {
    474 				ERROR (device->base.context, "Unexpected packet length (" DC_PRINTF_SIZE ").", len);
    475 				return DC_STATUS_PROTOCOL;
    476 			}
    477 			memcpy (data, line, len + 1);
    478 		} else {
    479 			if (len != 0) {
    480 				ERROR (device->base.context, "Unexpected packet length (" DC_PRINTF_SIZE ").", len);
    481 				return DC_STATUS_PROTOCOL;
    482 			}
    483 		}
    484 	} else if (strncmp (buf + cmdlen, ">xmr", 4) == 0) {
    485 		if (nbytes > cmdlen + 4) {
    486 			WARNING (device->base.context, "Packet contains extra data ('%s').", buf + cmdlen + 4);
    487 		}
    488 		return oceans_s1_xmodem_recv (device, buffer);
    489 	} else {
    490 		ERROR (device->base.context, "Received unexpected packet data ('%s').", buf);
    491 		return DC_STATUS_PROTOCOL;
    492 	}
    493 
    494 	return status;
    495 }
    496 
    497 dc_status_t
    498 oceans_s1_device_open(dc_device_t **out, dc_context_t *context, dc_iostream_t *iostream)
    499 {
    500 	dc_status_t status = DC_STATUS_SUCCESS;
    501 	oceans_s1_device_t *device = NULL;
    502 
    503 	if (out == NULL)
    504 		return DC_STATUS_INVALIDARGS;
    505 
    506 	// Allocate memory.
    507 	device = (oceans_s1_device_t *) dc_device_allocate (context, &oceans_s1_device_vtable);
    508 	if (device == NULL) {
    509 		ERROR(context, "Failed to allocate memory.");
    510 		return DC_STATUS_NOMEMORY;
    511 	}
    512 
    513 	// Set the default values.
    514 	device->iostream = iostream;
    515 	device->timestamp = 0;
    516 
    517 	// Set the timeout for receiving data (4000 ms).
    518 	status = dc_iostream_set_timeout (device->iostream, 4000);
    519 	if (status != DC_STATUS_SUCCESS) {
    520 		ERROR (context, "Failed to set the timeout.");
    521 		goto error_free;
    522 	}
    523 
    524 	dc_iostream_purge (device->iostream, DC_DIRECTION_ALL);
    525 
    526 	*out = (dc_device_t *) device;
    527 
    528 	return DC_STATUS_SUCCESS;
    529 
    530 error_free:
    531 	dc_device_deallocate ((dc_device_t *) device);
    532 	return status;
    533 }
    534 
    535 static dc_status_t
    536 oceans_s1_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size)
    537 {
    538 	oceans_s1_device_t *device = (oceans_s1_device_t *) abstract;
    539 
    540 	if (size && size != SZ_FINGERPRINT)
    541 		return DC_STATUS_INVALIDARGS;
    542 
    543 	if (size)
    544 		device->timestamp = array_uint64_be (data);
    545 	else
    546 		device->timestamp = 0;
    547 
    548 	return DC_STATUS_SUCCESS;
    549 }
    550 
    551 static dc_status_t
    552 oceans_s1_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata)
    553 {
    554 	dc_status_t status = DC_STATUS_SUCCESS;
    555 	oceans_s1_device_t *device = (oceans_s1_device_t *) abstract;
    556 
    557 	dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER;
    558 	device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);
    559 
    560 	char version[SZ_PACKET] = {0};
    561 	status = oceans_s1_transfer (device, NULL, version, sizeof(version), "version", NULL);
    562 	if (status != DC_STATUS_SUCCESS) {
    563 		ERROR (abstract->context, "Failed to read the version.");
    564 		return status;
    565 	}
    566 
    567 	unsigned int major = 0, minor = 0, unknown = 0;
    568 	if (sscanf (version, "%u.%u %x", &major, &minor, &unknown) != 3) {
    569 		ERROR (abstract->context, "Failed to parse the version response.");
    570 		return DC_STATUS_PROTOCOL;
    571 	}
    572 
    573 	// Emit a device info event.
    574 	dc_event_devinfo_t devinfo;
    575 	devinfo.model = 0;
    576 	devinfo.firmware = major << 16 | minor;
    577 	devinfo.serial = 0;
    578 	device_event_emit (abstract, DC_EVENT_DEVINFO, &devinfo);
    579 
    580 	dc_buffer_t *buffer = dc_buffer_new (4096);
    581 	if (buffer == NULL) {
    582 		ERROR (abstract->context, "Failed to allocate memory.");
    583 		status = DC_STATUS_NOMEMORY;
    584 		goto error_exit;
    585 	}
    586 
    587 	status = oceans_s1_transfer (device, buffer, NULL, 0, "dllist", NULL);
    588 	if (status != DC_STATUS_SUCCESS) {
    589 		ERROR (abstract->context, "Failed to download the dive list.");
    590 		goto error_free_buffer;
    591 	}
    592 
    593 	const unsigned char *data = dc_buffer_get_data (buffer);
    594 	size_t size = dc_buffer_get_size (buffer);
    595 
    596 	oceans_s1_dive_t *logbook = NULL, *dive = NULL;
    597 	unsigned int ndives = 0;
    598 
    599 	char *ptr = NULL;
    600 	size_t len = 0;
    601 	int n = 0;
    602 	while ((n = oceans_s1_getline (&ptr, &len, &data, &size)) != -1) {
    603 		// Ignore empty lines.
    604 		if (n == 0)
    605 			continue;
    606 
    607 		// Ignore leading whitespace.
    608 		const char *line = ptr;
    609 		while (*line == ' ')
    610 			line++;
    611 
    612 		if (strncmp (line, "divelog", 7) == 0 ||
    613 			strncmp (line, "endlog", 6) == 0 ||
    614 			strncmp (line, "continue", 8) == 0) {
    615 			// Nothing to do.
    616 		} else if (strncmp (line, "dive", 4) == 0) {
    617 			if (dive != NULL) {
    618 				ERROR (abstract->context, "Skipping dive without 'enddive' line.");
    619 				free (dive);
    620 				dive = NULL;
    621 			}
    622 
    623 			unsigned int number = 0, divemode = 0, o2 = 0;
    624 			dc_ticks_t timestamp = 0;
    625 			if (sscanf (line, "dive %u,%u,%u," DC_FORMAT_INT64, &number, &divemode, &o2, &timestamp) != 4) {
    626 				ERROR (abstract->context, "Failed to parse the line '%s'.", line);
    627 				status = DC_STATUS_DATAFORMAT;
    628 				goto error_free_list;
    629 			}
    630 
    631 			dive = (oceans_s1_dive_t *) malloc (sizeof (oceans_s1_dive_t));
    632 			if (dive == NULL) {
    633 				ERROR (abstract->context, "Failed to allocate memory.");
    634 				status = DC_STATUS_NOMEMORY;
    635 				goto error_free_list;
    636 			}
    637 
    638 			dive->next = NULL;
    639 			dive->timestamp = timestamp;
    640 			dive->number = number;
    641 		} else if (strncmp (line, "enddive", 7) == 0) {
    642 			if (dive) {
    643 				if (dive->timestamp > device->timestamp) {
    644 					oceans_s1_list_add (&logbook, dive);
    645 					ndives++;
    646 				} else {
    647 					free (dive);
    648 				}
    649 				dive = NULL;
    650 			} else {
    651 				WARNING (abstract->context, "Unexpected line '%s'.", line);
    652 			}
    653 		} else {
    654 			WARNING (abstract->context, "Unexpected line '%s'.", line);
    655 		}
    656 	}
    657 
    658 	if (dive != NULL) {
    659 		WARNING (abstract->context, "Skipping dive without 'enddive' line.");
    660 		free (dive);
    661 		dive = NULL;
    662 	}
    663 
    664 	progress.current = 1;
    665 	progress.maximum = 1 + ndives;
    666 	device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);
    667 
    668 	for (dive = logbook; dive; dive = dive->next) {
    669 		status = oceans_s1_transfer (device, buffer, NULL, 0, "dlget", "%u %u", dive->number, dive->number + 1);
    670 		if (status != DC_STATUS_SUCCESS) {
    671 			ERROR (abstract->context, "Failed to download the dive.");
    672 			goto error_free_list;
    673 		}
    674 
    675 		progress.current++;
    676 		device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);
    677 
    678 		unsigned char fingerprint[SZ_FINGERPRINT] = {0};
    679 		array_uint64_be_set (fingerprint, dive->timestamp);
    680 
    681 		if (callback && !callback (dc_buffer_get_data (buffer), dc_buffer_get_size (buffer), fingerprint, sizeof(fingerprint), userdata))
    682 			break;
    683 	}
    684 
    685 error_free_list:
    686 	oceans_s1_list_free (logbook);
    687 	free (ptr);
    688 error_free_buffer:
    689 	dc_buffer_free (buffer);
    690 error_exit:
    691 	return status;
    692 }
    693 
    694 static dc_status_t
    695 oceans_s1_device_timesync (dc_device_t *abstract, const dc_datetime_t *datetime)
    696 {
    697 	dc_status_t status = DC_STATUS_SUCCESS;
    698 	oceans_s1_device_t *device = (oceans_s1_device_t *) abstract;
    699 
    700 	// Ignore the timezone offset.
    701 	dc_datetime_t dt = *datetime;
    702 	dt.timezone = DC_TIMEZONE_NONE;
    703 
    704 	dc_ticks_t timestamp = dc_datetime_mktime (&dt);
    705 	if (timestamp < 0) {
    706 		ERROR (abstract->context, "Invalid date/time value specified.");
    707 		return DC_STATUS_INVALIDARGS;
    708 	}
    709 
    710 	status = oceans_s1_transfer (device, NULL, NULL, 0, "utc", DC_FORMAT_INT64, timestamp);
    711 	if (status != DC_STATUS_SUCCESS) {
    712 		ERROR (abstract->context, "Failed to set the date/time.");
    713 		return status;
    714 	}
    715 
    716 	return status;
    717 }