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

deepsix_excursion.c (17019B)


      1 /*
      2  * libdivecomputer
      3  *
      4  * Copyright (C) 2021 Ryan Gardner, 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 "deepsix_excursion.h"
     27 #include "context-private.h"
     28 #include "device-private.h"
     29 #include "platform.h"
     30 #include "checksum.h"
     31 #include "array.h"
     32 
     33 #define MAXPACKET 255
     34 
     35 #define HEADERSIZE_MIN 128
     36 #define HEADERSIZE_V0  156
     37 
     38 #define NSTEPS    1000
     39 #define STEP(i,n) (NSTEPS * (i) / (n))
     40 
     41 #define FP_SIZE   6
     42 #define FP_OFFSET 12
     43 
     44 #define DIR_WRITE          0x00
     45 #define DIR_READ           0x01
     46 
     47 #define GRP_INFO           0xA0
     48 #define CMD_INFO_HARDWARE  0x01
     49 #define CMD_INFO_SOFTWARE  0x02
     50 #define CMD_INFO_SERIAL    0x03
     51 #define CMD_INFO_LASTDIVE  0x04
     52 
     53 #define GRP_SETTINGS       0xB0
     54 #define CMD_SETTINGS_DATE  0x01
     55 #define CMD_SETTINGS_TIME  0x03
     56 #define CMD_SETTINGS_STORE 0x27
     57 #define CMD_SETTINGS_LOAD  0x28
     58 
     59 #define GRP_DIVE                 0xC0
     60 #define CMD_DIVE_HEADER          0x02
     61 #define CMD_DIVE_PROFILE         0x03
     62 #define CMD_DIVE_COUNT           0x05
     63 #define CMD_DIVE_INDEX_LAST      0x06
     64 #define CMD_DIVE_INDEX_FIRST     0x07
     65 #define CMD_DIVE_INDEX_PREVIOUS  0x08
     66 #define CMD_DIVE_INDEX_NEXT      0x09
     67 
     68 typedef struct deepsix_excursion_device_t {
     69 	dc_device_t base;
     70 	dc_iostream_t *iostream;
     71 	unsigned char fingerprint[FP_SIZE];
     72 } deepsix_excursion_device_t;
     73 
     74 static dc_status_t deepsix_excursion_device_set_fingerprint (dc_device_t *abstract, const unsigned char *data, unsigned int size);
     75 static dc_status_t deepsix_excursion_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata);
     76 static dc_status_t deepsix_excursion_device_timesync(dc_device_t *abstract, const dc_datetime_t *datetime);
     77 
     78 static const dc_device_vtable_t deepsix_excursion_device_vtable = {
     79 	sizeof(deepsix_excursion_device_t),
     80 	DC_FAMILY_DEEPSIX_EXCURSION,
     81 	deepsix_excursion_device_set_fingerprint, /* set_fingerprint */
     82 	NULL, /* read */
     83 	NULL, /* write */
     84 	NULL, /* dump */
     85 	deepsix_excursion_device_foreach, /* foreach */
     86 	deepsix_excursion_device_timesync, /* timesync */
     87 	NULL, /* close */
     88 };
     89 
     90 static dc_status_t
     91 deepsix_excursion_send (deepsix_excursion_device_t *device, unsigned char grp, unsigned char cmd, unsigned char dir, const unsigned char data[], unsigned int size)
     92 {
     93 	dc_status_t status = DC_STATUS_SUCCESS;
     94 	dc_device_t *abstract = (dc_device_t *) device;
     95 	unsigned char packet[4 + MAXPACKET + 1];
     96 
     97 	if (device_is_cancelled (abstract))
     98 		return DC_STATUS_CANCELLED;
     99 
    100 	if (size > MAXPACKET)
    101 		return DC_STATUS_INVALIDARGS;
    102 
    103 	// Setup the data packet
    104 	packet[0] = grp;
    105 	packet[1] = cmd;
    106 	packet[2] = dir;
    107 	packet[3] = size;
    108 	if (size) {
    109 		memcpy(packet + 4, data, size);
    110 	}
    111 	packet[size + 4] = checksum_add_uint8 (packet, size + 4, 0) ^ 0xFF;
    112 
    113 	// Send the data packet.
    114 	status = dc_iostream_write (device->iostream, packet, 4 + size + 1, NULL);
    115 	if (status != DC_STATUS_SUCCESS) {
    116 		ERROR (abstract->context, "Failed to send the command.");
    117 		return status;
    118 	}
    119 
    120 	return status;
    121 }
    122 
    123 static dc_status_t
    124 deepsix_excursion_recv (deepsix_excursion_device_t *device, unsigned char grp, unsigned char cmd, unsigned char dir, unsigned char data[], unsigned int size, unsigned int *actual)
    125 {
    126 	dc_status_t status = DC_STATUS_SUCCESS;
    127 	dc_device_t *abstract = (dc_device_t *) device;
    128 	unsigned char packet[4 + MAXPACKET + 1];
    129 	size_t transferred = 0;
    130 
    131 	// Read the packet header, payload and checksum.
    132 	status = dc_iostream_read (device->iostream, packet, sizeof(packet), &transferred);
    133 	if (status != DC_STATUS_SUCCESS) {
    134 		ERROR (abstract->context, "Failed to receive the packet.");
    135 		return status;
    136 	}
    137 
    138 	if (transferred < 4) {
    139 		ERROR (abstract->context, "Packet header too short ("DC_PRINTF_SIZE").", transferred);
    140 		return DC_STATUS_PROTOCOL;
    141 	}
    142 
    143 	// Verify the packet header.
    144 	if (packet[0] != grp || packet[1] != cmd || packet[2] != dir) {
    145 		ERROR (device->base.context, "Unexpected packet header.");
    146 		return DC_STATUS_PROTOCOL;
    147 	}
    148 
    149 	unsigned int len = packet[3];
    150 	if (len > MAXPACKET) {
    151 		ERROR (abstract->context, "Packet header length too large (%u).", len);
    152 		return DC_STATUS_PROTOCOL;
    153 	}
    154 
    155 	if (transferred < 4 + len + 1) {
    156 		ERROR (abstract->context, "Packet data too short ("DC_PRINTF_SIZE").", transferred);
    157 		return DC_STATUS_PROTOCOL;
    158 	}
    159 
    160 	// Verify the checksum.
    161 	unsigned char csum = checksum_add_uint8 (packet, len + 4, 0) ^ 0xFF;
    162 	if (packet[len + 4] != csum) {
    163 		ERROR (abstract->context, "Unexpected packet checksum (%02x)", csum);
    164 		return DC_STATUS_PROTOCOL;
    165 	}
    166 
    167 	if (len > size) {
    168 		ERROR (abstract->context, "Unexpected packet length (%u).", len);
    169 		return DC_STATUS_PROTOCOL;
    170 	}
    171 
    172 	if (len) {
    173 		memcpy(data, packet + 4, len);
    174 	}
    175 
    176 	if (actual)
    177 		*actual = len;
    178 
    179 	return status;
    180 }
    181 
    182 static dc_status_t
    183 deepsix_excursion_transfer (deepsix_excursion_device_t *device, unsigned char grp, unsigned char cmd, unsigned char dir, const unsigned char command[], unsigned int csize, unsigned char answer[], unsigned int asize, unsigned int *actual)
    184 {
    185 	dc_status_t status = DC_STATUS_SUCCESS;
    186 
    187 	status = deepsix_excursion_send (device, grp, cmd, dir, command, csize);
    188 	if (status != DC_STATUS_SUCCESS)
    189 		return status;
    190 
    191 	status = deepsix_excursion_recv (device, grp + 1, cmd, dir, answer, asize, actual);
    192 	if (status != DC_STATUS_SUCCESS)
    193 		return status;
    194 
    195 	return status;
    196 }
    197 
    198 dc_status_t
    199 deepsix_excursion_device_open (dc_device_t **out, dc_context_t *context, dc_iostream_t *iostream)
    200 {
    201 	dc_status_t status = DC_STATUS_SUCCESS;
    202 	deepsix_excursion_device_t *device = NULL;
    203 
    204 	if (out == NULL)
    205 		return DC_STATUS_INVALIDARGS;
    206 
    207 	// Allocate memory.
    208 	device = (deepsix_excursion_device_t *) dc_device_allocate (context, &deepsix_excursion_device_vtable);
    209 	if (device == NULL) {
    210 		ERROR (context, "Failed to allocate memory.");
    211 		return DC_STATUS_NOMEMORY;
    212 	}
    213 
    214 	// Set the default values.
    215 	device->iostream = iostream;
    216 	memset(device->fingerprint, 0, sizeof(device->fingerprint));
    217 
    218 	// Set the serial communication protocol (115200 8N1).
    219 	status = dc_iostream_configure (device->iostream, 115200, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
    220 	if (status != DC_STATUS_SUCCESS) {
    221 		ERROR (context, "Failed to set the terminal attributes.");
    222 		goto error_free;
    223 	}
    224 
    225 	// Set the timeout for receiving data (3000ms).
    226 	status = dc_iostream_set_timeout (device->iostream, 3000);
    227 	if (status != DC_STATUS_SUCCESS) {
    228 		ERROR (context, "Failed to set the timeout.");
    229 		goto error_free;
    230 	}
    231 
    232 	// Make sure everything is in a sane state.
    233 	dc_iostream_sleep (device->iostream, 300);
    234 	dc_iostream_purge (device->iostream, DC_DIRECTION_ALL);
    235 
    236 	*out = (dc_device_t *) device;
    237 
    238 	return DC_STATUS_SUCCESS;
    239 
    240 error_free:
    241 	dc_device_deallocate ((dc_device_t *) device);
    242 	return status;
    243 }
    244 
    245 static dc_status_t
    246 deepsix_excursion_device_set_fingerprint (dc_device_t *abstract, const unsigned char *data, unsigned int size)
    247 {
    248 	deepsix_excursion_device_t *device = (deepsix_excursion_device_t *)abstract;
    249 
    250 	if (size && size != sizeof (device->fingerprint))
    251 		return DC_STATUS_INVALIDARGS;
    252 
    253 	if (size)
    254 		memcpy (device->fingerprint, data, sizeof (device->fingerprint));
    255 	else
    256 		memset (device->fingerprint, 0, sizeof (device->fingerprint));
    257 
    258 	return DC_STATUS_SUCCESS;
    259 }
    260 
    261 static dc_status_t
    262 deepsix_excursion_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata)
    263 {
    264 	dc_status_t status = DC_STATUS_SUCCESS;
    265 	deepsix_excursion_device_t *device = (deepsix_excursion_device_t *) abstract;
    266 
    267 	// Enable progress notifications.
    268 	dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER;
    269 	device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);
    270 
    271 	// Load the settings into memory.
    272 	status = deepsix_excursion_transfer (device, GRP_SETTINGS, CMD_SETTINGS_LOAD, DIR_WRITE, NULL, 0, NULL, 0, NULL);
    273 	if (status != DC_STATUS_SUCCESS) {
    274 		ERROR (abstract->context, "Failed to load the settings.");
    275 		return status;
    276 	}
    277 
    278 	// Read the hardware version.
    279 	unsigned char rsp_hardware[6] = {0};
    280 	status = deepsix_excursion_transfer (device, GRP_INFO, CMD_INFO_HARDWARE, DIR_READ, NULL, 0, rsp_hardware, sizeof(rsp_hardware), NULL);
    281 	if (status != DC_STATUS_SUCCESS) {
    282 		ERROR (abstract->context, "Failed to read the hardware version.");
    283 		return status;
    284 	}
    285 
    286 	HEXDUMP (abstract->context, DC_LOGLEVEL_DEBUG, "Hardware", rsp_hardware, sizeof(rsp_hardware));
    287 
    288 	// Read the software version.
    289 	unsigned char rsp_software[6] = {0};
    290 	status = deepsix_excursion_transfer (device, GRP_INFO, CMD_INFO_SOFTWARE, DIR_READ, NULL, 0, rsp_software, sizeof(rsp_software), NULL);
    291 	if (status != DC_STATUS_SUCCESS) {
    292 		ERROR (abstract->context, "Failed to read the software version.");
    293 		return status;
    294 	}
    295 
    296 	HEXDUMP (abstract->context, DC_LOGLEVEL_DEBUG, "Software", rsp_software, sizeof(rsp_software));
    297 
    298 	// Read the serial number
    299 	unsigned char rsp_serial[12] = {0};
    300 	status = deepsix_excursion_transfer (device, GRP_INFO, CMD_INFO_SERIAL, DIR_READ, NULL, 0, rsp_serial, sizeof(rsp_serial), NULL);
    301 	if (status != DC_STATUS_SUCCESS) {
    302 		ERROR (abstract->context, "Failed to read the serial number.");
    303 		return status;
    304 	}
    305 
    306 	HEXDUMP (abstract->context, DC_LOGLEVEL_DEBUG, "Serial", rsp_serial, sizeof(rsp_serial));
    307 
    308 	// Emit a device info event.
    309 	dc_event_devinfo_t devinfo;
    310 	devinfo.model = 0;
    311 	devinfo.firmware = array_uint16_be (rsp_software + 4);
    312 	devinfo.serial = array_convert_str2num (rsp_serial + 3, sizeof(rsp_serial) - 3);
    313 	device_event_emit (abstract, DC_EVENT_DEVINFO, &devinfo);
    314 
    315 	// Firmware version 6+ uses the new commands.
    316 	unsigned int fw6 = memcmp(rsp_software, "D01", 3) == 0 && rsp_software[4] >= '6';
    317 
    318 	unsigned int ndives = 0, last = 0;
    319 	if (fw6) {
    320 		// Read the number of dives.
    321 		unsigned char rsp_ndives[2] = {0};
    322 		status = deepsix_excursion_transfer (device, GRP_DIVE, CMD_DIVE_COUNT, DIR_READ, NULL, 0, rsp_ndives, sizeof(rsp_ndives), NULL);
    323 		if (status != DC_STATUS_SUCCESS) {
    324 			ERROR (abstract->context, "Failed to read the number of dives");
    325 			return status;
    326 		}
    327 
    328 		// Read the index of the last dive.
    329 		unsigned char rsp_last[4] = {0};
    330 		status = deepsix_excursion_transfer (device, GRP_DIVE, CMD_DIVE_INDEX_LAST, DIR_READ, NULL, 0, rsp_last, sizeof(rsp_last), NULL);
    331 		if (status != DC_STATUS_SUCCESS) {
    332 			ERROR (abstract->context, "Failed to read the last dive index.");
    333 			return status;
    334 		}
    335 
    336 		ndives = array_uint16_le (rsp_ndives);
    337 		last = array_uint16_le (rsp_last);
    338 	} else {
    339 		// Read the index of the last dive.
    340 		const unsigned char cmd_last[2] = {0};
    341 		unsigned char rsp_last[2] = {0};
    342 		status = deepsix_excursion_transfer (device, GRP_INFO, CMD_INFO_LASTDIVE, DIR_READ, cmd_last, sizeof(cmd_last), rsp_last, sizeof(rsp_last), NULL);
    343 		if (status != DC_STATUS_SUCCESS) {
    344 			ERROR (abstract->context, "Failed to read the last dive index.");
    345 			return status;
    346 		}
    347 
    348 		ndives = last = array_uint16_le (rsp_last);
    349 	}
    350 
    351 	// Update and emit a progress event.
    352 	progress.current = 1 * NSTEPS;
    353 	progress.maximum = (ndives + 1) * NSTEPS;
    354 	device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);
    355 
    356 	dc_buffer_t *buffer = dc_buffer_new(0);
    357 	if (buffer == NULL) {
    358 		ERROR (abstract->context, "Insufficient buffer space available.");
    359 		return DC_STATUS_NOMEMORY;
    360 	}
    361 
    362 	unsigned int number = last;
    363 	for (unsigned int i = 0; i < ndives; ++i) {
    364 		if (fw6) {
    365 			if (i > 0) {
    366 				const unsigned char cmd_previous[] = {
    367 					(number      ) & 0xFF,
    368 					(number >>  8) & 0xFF,
    369 					(number >> 16) & 0xFF,
    370 					(number >> 24) & 0xFF};
    371 				unsigned char rsp_previous[4] = {0};
    372 				status = deepsix_excursion_transfer (device, GRP_DIVE, CMD_DIVE_INDEX_PREVIOUS, DIR_READ,
    373 					cmd_previous, sizeof(cmd_previous), rsp_previous, sizeof(rsp_previous), NULL);
    374 				if (status != DC_STATUS_SUCCESS) {
    375 					ERROR (abstract->context, "Failed to read the previous dive index");
    376 					return status;
    377 				}
    378 				number = array_uint32_le (rsp_previous);
    379 			}
    380 		} else {
    381 			number = ndives - i;
    382 		}
    383 
    384 		unsigned int headersize = 0;
    385 		const unsigned char cmd_header[] = {
    386 			(number     ) & 0xFF,
    387 			(number >> 8) & 0xFF};
    388 		unsigned char rsp_header[MAXPACKET] = {0};
    389 		status = deepsix_excursion_transfer (device, GRP_DIVE, CMD_DIVE_HEADER, DIR_READ,
    390 			cmd_header, sizeof(cmd_header), rsp_header, sizeof(rsp_header), &headersize);
    391 		if (status != DC_STATUS_SUCCESS) {
    392 			ERROR (abstract->context, "Failed to read the dive header.");
    393 			goto error_free;
    394 		}
    395 
    396 		if (headersize < HEADERSIZE_MIN || headersize != (fw6 ? rsp_header[2] : HEADERSIZE_V0)) {
    397 			ERROR (abstract->context, "Unexpected size of the dive header (%u).", headersize);
    398 			goto error_free;
    399 		}
    400 
    401 		if (memcmp(rsp_header + FP_OFFSET, device->fingerprint, sizeof(device->fingerprint)) == 0)
    402 			break;
    403 
    404 		unsigned int length = array_uint32_le (rsp_header + 8);
    405 
    406 		// Update and emit a progress event.
    407 		progress.current = (i + 1) * NSTEPS + STEP(headersize, headersize + length);
    408 		device_event_emit(abstract, DC_EVENT_PROGRESS, &progress);
    409 
    410 		dc_buffer_clear(buffer);
    411 		dc_buffer_reserve(buffer, headersize + length);
    412 
    413 		if (!dc_buffer_append(buffer, rsp_header, headersize)) {
    414 			ERROR (abstract->context, "Insufficient buffer space available.");
    415 			status = DC_STATUS_NOMEMORY;
    416 			goto error_free;
    417 		}
    418 
    419 		unsigned offset = 0;
    420 		while (offset < length) {
    421 			unsigned int len = 0;
    422 			const unsigned char cmd_profile[] = {
    423 				(number      ) & 0xFF,
    424 				(number >>  8) & 0xFF,
    425 				(offset      ) & 0xFF,
    426 				(offset >>  8) & 0xFF,
    427 				(offset >> 16) & 0xFF,
    428 				(offset >> 24) & 0xFF};
    429 			unsigned char rsp_profile[MAXPACKET] = {0};
    430 			status = deepsix_excursion_transfer (device, GRP_DIVE, CMD_DIVE_PROFILE, DIR_READ,
    431 				cmd_profile, sizeof(cmd_profile), rsp_profile, sizeof(rsp_profile), &len);
    432 			if (status != DC_STATUS_SUCCESS) {
    433 				ERROR (abstract->context, "Failed to read the dive profile.");
    434 				goto error_free;
    435 			}
    436 
    437 			// Remove padding from the last packet.
    438 			unsigned int n = len;
    439 			if (offset + n > length) {
    440 				n = length - offset;
    441 			}
    442 
    443 			// Update and emit a progress event.
    444 			progress.current = (i + 1) * NSTEPS + STEP(headersize + offset + n, headersize + length);
    445 			device_event_emit(abstract, DC_EVENT_PROGRESS, &progress);
    446 
    447 			if (!dc_buffer_append(buffer, rsp_profile, n)) {
    448 				ERROR (abstract->context, "Insufficient buffer space available.");
    449 				status = DC_STATUS_NOMEMORY;
    450 				goto error_free;
    451 			}
    452 
    453 			offset += n;
    454 		}
    455 
    456 		unsigned char *data = dc_buffer_get_data(buffer);
    457 		unsigned int   size = dc_buffer_get_size(buffer);
    458 		if (callback && !callback (data, size, data + FP_OFFSET, sizeof(device->fingerprint), userdata)) {
    459 			break;
    460 		}
    461 	}
    462 
    463 error_free:
    464 	dc_buffer_free(buffer);
    465 	return status;
    466 }
    467 
    468 static dc_status_t
    469 deepsix_excursion_device_timesync (dc_device_t *abstract, const dc_datetime_t *datetime)
    470 {
    471 	dc_status_t status = DC_STATUS_SUCCESS;
    472 	deepsix_excursion_device_t *device = (deepsix_excursion_device_t *) abstract;
    473 
    474 	if (datetime->year < 2000) {
    475 		ERROR (abstract->context, "Invalid date/time value specified.");
    476 		return DC_STATUS_INVALIDARGS;
    477 	}
    478 
    479 	const unsigned char cmd_date[] = {
    480 		datetime->year - 2000,
    481 		datetime->month,
    482 		datetime->day};
    483 
    484 	const unsigned char cmd_time[] = {
    485 		datetime->hour,
    486 		datetime->minute,
    487 		datetime->second};
    488 
    489 	const unsigned char cmd_store[] = {0x00};
    490 
    491 	unsigned char rsp_date[sizeof(cmd_date)] = {0};
    492 	status = deepsix_excursion_transfer (device, GRP_SETTINGS, CMD_SETTINGS_DATE, DIR_WRITE, cmd_date, sizeof(cmd_date), rsp_date, sizeof(rsp_date), NULL);
    493 	if (status != DC_STATUS_SUCCESS) {
    494 		ERROR (abstract->context, "Failed to set the date.");
    495 		return status;
    496 	}
    497 
    498 	if (memcmp(rsp_date, cmd_date, sizeof(cmd_date)) != 0) {
    499 		ERROR (abstract->context, "Failed to verify the date.");
    500 		return DC_STATUS_PROTOCOL;
    501 	}
    502 
    503 	unsigned char rsp_time[sizeof(cmd_time)] = {0};
    504 	status = deepsix_excursion_transfer (device, GRP_SETTINGS, CMD_SETTINGS_TIME, DIR_WRITE, cmd_time, sizeof(cmd_time), rsp_time, sizeof(rsp_time), NULL);
    505 	if (status != DC_STATUS_SUCCESS) {
    506 		ERROR (abstract->context, "Failed to set the time.");
    507 		return status;
    508 	}
    509 
    510 	if (memcmp(rsp_time, cmd_time, sizeof(cmd_time)) != 0) {
    511 		ERROR (abstract->context, "Failed to verify the time.");
    512 		return DC_STATUS_PROTOCOL;
    513 	}
    514 
    515 	status = deepsix_excursion_transfer (device, GRP_SETTINGS, CMD_SETTINGS_STORE, DIR_WRITE, cmd_store, sizeof(cmd_store), NULL, 0, NULL);
    516 	if (status != DC_STATUS_SUCCESS) {
    517 		ERROR (abstract->context, "Failed to store the settings.");
    518 		return status;
    519 	}
    520 
    521 	return status;
    522 }