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

atomics_cobalt.c (11189B)


      1 /*
      2  * libdivecomputer
      3  *
      4  * Copyright (C) 2011 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> // memcmp, memcpy
     23 #include <stdlib.h> // malloc, free
     24 
     25 #include <libdivecomputer/usb.h>
     26 
     27 #include "atomics_cobalt.h"
     28 #include "context-private.h"
     29 #include "device-private.h"
     30 #include "checksum.h"
     31 #include "array.h"
     32 
     33 #define ISINSTANCE(device) dc_device_isinstance((device), &atomics_cobalt_device_vtable)
     34 
     35 #define COBALT1 0
     36 #define COBALT2 2
     37 
     38 #define VID 0x0471
     39 #define PID 0x0888
     40 #define TIMEOUT 2000
     41 
     42 #define FP_OFFSET 20
     43 
     44 #define SZ_HEADER 228
     45 
     46 #define SZ_MEMORY1 (29 * 64 * 1024) // Cobalt 1
     47 #define SZ_MEMORY2 (41 * 64 * 1024) // Cobalt 2
     48 #define SZ_VERSION 14
     49 
     50 typedef struct atomics_cobalt_device_t {
     51 	dc_device_t base;
     52 	dc_iostream_t *iostream;
     53 	unsigned int simulation;
     54 	unsigned char fingerprint[6];
     55 	unsigned char version[SZ_VERSION];
     56 } atomics_cobalt_device_t;
     57 
     58 static dc_status_t atomics_cobalt_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size);
     59 static dc_status_t atomics_cobalt_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata);
     60 
     61 static const dc_device_vtable_t atomics_cobalt_device_vtable = {
     62 	sizeof(atomics_cobalt_device_t),
     63 	DC_FAMILY_ATOMICS_COBALT,
     64 	atomics_cobalt_device_set_fingerprint, /* set_fingerprint */
     65 	NULL, /* read */
     66 	NULL, /* write */
     67 	NULL, /* dump */
     68 	atomics_cobalt_device_foreach, /* foreach */
     69 	NULL, /* timesync */
     70 	NULL /* close */
     71 };
     72 
     73 
     74 dc_status_t
     75 atomics_cobalt_device_open (dc_device_t **out, dc_context_t *context, dc_iostream_t *iostream)
     76 {
     77 	dc_status_t status = DC_STATUS_SUCCESS;
     78 	atomics_cobalt_device_t *device = NULL;
     79 
     80 	if (out == NULL)
     81 		return DC_STATUS_INVALIDARGS;
     82 
     83 	// Allocate memory.
     84 	device = (atomics_cobalt_device_t *) dc_device_allocate (context, &atomics_cobalt_device_vtable);
     85 	if (device == NULL) {
     86 		ERROR (context, "Failed to allocate memory.");
     87 		return DC_STATUS_NOMEMORY;
     88 	}
     89 
     90 	// Set the default values.
     91 	device->iostream = iostream;
     92 	device->simulation = 0;
     93 	memset (device->fingerprint, 0, sizeof (device->fingerprint));
     94 
     95 	// Set the timeout for receiving data (2000 ms).
     96 	status = dc_iostream_set_timeout (device->iostream, TIMEOUT);
     97 	if (status != DC_STATUS_SUCCESS) {
     98 		ERROR (context, "Failed to set the timeout.");
     99 		goto error_free;
    100 	}
    101 
    102 	status = atomics_cobalt_device_version ((dc_device_t *) device, device->version, sizeof (device->version));
    103 	if (status != DC_STATUS_SUCCESS) {
    104 		ERROR (context, "Failed to identify the dive computer.");
    105 		goto error_free;
    106 	}
    107 
    108 	HEXDUMP (context, DC_LOGLEVEL_DEBUG, "Version", device->version, sizeof (device->version));
    109 
    110 	*out = (dc_device_t*) device;
    111 
    112 	return DC_STATUS_SUCCESS;
    113 
    114 error_free:
    115 	dc_device_deallocate ((dc_device_t *) device);
    116 	return status;
    117 }
    118 
    119 
    120 static dc_status_t
    121 atomics_cobalt_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size)
    122 {
    123 	atomics_cobalt_device_t *device = (atomics_cobalt_device_t *) abstract;
    124 
    125 	if (size && size != sizeof (device->fingerprint))
    126 		return DC_STATUS_INVALIDARGS;
    127 
    128 	if (size)
    129 		memcpy (device->fingerprint, data, sizeof (device->fingerprint));
    130 	else
    131 		memset (device->fingerprint, 0, sizeof (device->fingerprint));
    132 
    133 	return DC_STATUS_SUCCESS;
    134 }
    135 
    136 
    137 dc_status_t
    138 atomics_cobalt_device_set_simulation (dc_device_t *abstract, unsigned int simulation)
    139 {
    140 	atomics_cobalt_device_t *device = (atomics_cobalt_device_t *) abstract;
    141 
    142 	if (!ISINSTANCE (abstract))
    143 		return DC_STATUS_INVALIDARGS;
    144 
    145 	device->simulation = simulation;
    146 
    147 	return DC_STATUS_SUCCESS;
    148 }
    149 
    150 
    151 dc_status_t
    152 atomics_cobalt_device_version (dc_device_t *abstract, unsigned char data[], unsigned int size)
    153 {
    154 	dc_status_t status = DC_STATUS_SUCCESS;
    155 	atomics_cobalt_device_t *device = (atomics_cobalt_device_t *) abstract;
    156 
    157 	if (!ISINSTANCE (abstract))
    158 		return DC_STATUS_INVALIDARGS;
    159 
    160 	if (size < SZ_VERSION)
    161 		return DC_STATUS_INVALIDARGS;
    162 
    163 	// Send the command to the dive computer.
    164 	unsigned char bRequest = 0x01;
    165 	dc_usb_control_t control = {
    166 		(unsigned char) DC_USB_REQUEST_VENDOR | DC_USB_RECIPIENT_DEVICE | DC_USB_ENDPOINT_OUT, /* bmRequestType */
    167 		bRequest, /* bRequest */
    168 		0, /* wValue */
    169 		0, /* wIndex */
    170 		0, /* wLength */
    171 	};
    172 
    173 	status = dc_iostream_ioctl (device->iostream, DC_IOCTL_USB_CONTROL_WRITE, &control, sizeof(control));
    174 	if (status != DC_STATUS_SUCCESS) {
    175 		ERROR (abstract->context, "Failed to send the command.");
    176 		return status;
    177 	}
    178 
    179 	// Receive the answer from the dive computer.
    180 	size_t length = 0;
    181 	unsigned char packet[SZ_VERSION + 2] = {0};
    182 	status = dc_iostream_read (device->iostream, packet, sizeof(packet), &length);
    183 	if (status != DC_STATUS_SUCCESS || length != sizeof (packet)) {
    184 		ERROR (abstract->context, "Failed to receive the answer.");
    185 		return status;
    186 	}
    187 
    188 	// Verify the checksum of the packet.
    189 	unsigned short crc = array_uint16_le (packet + SZ_VERSION);
    190 	unsigned short ccrc = checksum_add_uint16 (packet, SZ_VERSION, 0x0);
    191 	if (crc != ccrc) {
    192 		ERROR (abstract->context, "Unexpected answer checksum.");
    193 		return DC_STATUS_PROTOCOL;
    194 	}
    195 
    196 	memcpy (data, packet, SZ_VERSION);
    197 
    198 	return DC_STATUS_SUCCESS;
    199 }
    200 
    201 
    202 static dc_status_t
    203 atomics_cobalt_read_dive (dc_device_t *abstract, dc_buffer_t *buffer, int init, dc_event_progress_t *progress)
    204 {
    205 	dc_status_t status = DC_STATUS_SUCCESS;
    206 	atomics_cobalt_device_t *device = (atomics_cobalt_device_t *) abstract;
    207 
    208 	if (device_is_cancelled (abstract))
    209 		return DC_STATUS_CANCELLED;
    210 
    211 	// Erase the current contents of the buffer.
    212 	if (!dc_buffer_clear (buffer)) {
    213 		ERROR (abstract->context, "Insufficient buffer space available.");
    214 		return DC_STATUS_NOMEMORY;
    215 	}
    216 
    217 	// Adjust the maximum value to take into account the two byte checksum and
    218 	// the 8 byte serial number. Those extra bytes are not stored inside the
    219 	// dive header and are added dynamically during the data transfer. Since we
    220 	// don't know the total number of dives in advance, we can't calculate the
    221 	// total number of extra bytes and adjust the maximum on the fly.
    222 	if (progress) {
    223 		progress->maximum += 2 + 8;
    224 	}
    225 
    226 	// Send the command to the dive computer.
    227 	unsigned char bRequest = 0;
    228 	if (device->simulation)
    229 		bRequest = init ? 0x02 : 0x03;
    230 	else
    231 		bRequest = init ? 0x09 : 0x0A;
    232 
    233 	dc_usb_control_t control = {
    234 		(unsigned char) DC_USB_REQUEST_VENDOR | DC_USB_RECIPIENT_DEVICE | DC_USB_ENDPOINT_OUT, /* bmRequestType */
    235 		bRequest, /* bRequest */
    236 		0, /* wValue */
    237 		0, /* wIndex */
    238 		0, /* wLength */
    239 	};
    240 
    241 	status = dc_iostream_ioctl (device->iostream, DC_IOCTL_USB_CONTROL_WRITE, &control, sizeof(control));
    242 	if (status != DC_STATUS_SUCCESS) {
    243 		ERROR (abstract->context, "Failed to send the command.");
    244 		return status;
    245 	}
    246 
    247 	unsigned int nbytes = 0;
    248 	while (1) {
    249 		// Receive the answer from the dive computer.
    250 		size_t length = 0;
    251 		unsigned char packet[8 * 1024] = {0};
    252 		status = dc_iostream_read (device->iostream, packet, sizeof(packet), &length);
    253 		if (status != DC_STATUS_SUCCESS && status != DC_STATUS_TIMEOUT) {
    254 			ERROR (abstract->context, "Failed to receive the answer.");
    255 			return status;
    256 		}
    257 
    258 		// Update and emit a progress event.
    259 		if (progress) {
    260 			progress->current += length;
    261 			device_event_emit (abstract, DC_EVENT_PROGRESS, progress);
    262 		}
    263 
    264 		// Append the packet to the output buffer.
    265 		dc_buffer_append (buffer, packet, length);
    266 		nbytes += length;
    267 
    268 		// If we received fewer bytes than requested, the transfer is finished.
    269 		if (length < sizeof (packet))
    270 			break;
    271 	}
    272 
    273 	// Check for a buffer error.
    274 	if (dc_buffer_get_size (buffer) != nbytes) {
    275 		ERROR (abstract->context, "Insufficient buffer space available.");
    276 		return DC_STATUS_NOMEMORY;
    277 	}
    278 
    279 	// Check for the minimum length.
    280 	if (nbytes < 2) {
    281 		ERROR (abstract->context, "Data packet is too short.");
    282 		return DC_STATUS_PROTOCOL;
    283 	}
    284 
    285 	// When only two 0xFF bytes are received, there are no more dives.
    286 	unsigned char *data = dc_buffer_get_data (buffer);
    287 	if (nbytes == 2 && data[0] == 0xFF && data[1] == 0xFF) {
    288 		dc_buffer_clear (buffer);
    289 		return DC_STATUS_SUCCESS;
    290 	}
    291 
    292 	// Verify the checksum of the packet.
    293 	unsigned short crc = array_uint16_le (data + nbytes - 2);
    294 	unsigned short ccrc = checksum_add_uint16 (data, nbytes - 2, 0x0);
    295 	if (crc != ccrc) {
    296 		ERROR (abstract->context, "Unexpected answer checksum.");
    297 		return DC_STATUS_PROTOCOL;
    298 	}
    299 
    300 	// Remove the checksum bytes.
    301 	dc_buffer_slice (buffer, 0, nbytes - 2);
    302 
    303 	return DC_STATUS_SUCCESS;
    304 }
    305 
    306 
    307 static dc_status_t
    308 atomics_cobalt_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata)
    309 {
    310 	atomics_cobalt_device_t *device = (atomics_cobalt_device_t *) abstract;
    311 
    312 	// Get the model number.
    313 	unsigned int model = array_uint16_le (device->version + 12);
    314 
    315 	// Enable progress notifications.
    316 	dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER;
    317 	progress.maximum = (model == COBALT2 ? SZ_MEMORY2 : SZ_MEMORY1) + 2;
    318 	device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);
    319 
    320 	// Emit a vendor event.
    321 	dc_event_vendor_t vendor;
    322 	vendor.data = device->version;
    323 	vendor.size = sizeof (device->version);
    324 	device_event_emit (abstract, DC_EVENT_VENDOR, &vendor);
    325 
    326 	// Emit a device info event.
    327 	dc_event_devinfo_t devinfo;
    328 	devinfo.model = array_uint16_le (device->version + 12);
    329 	devinfo.firmware = (array_uint16_le (device->version + 8) << 16)
    330 		+ array_uint16_le (device->version + 10);
    331 	devinfo.serial = 0;
    332 	for (unsigned int i = 0; i < 8; ++i) {
    333 		devinfo.serial *= 10;
    334 		devinfo.serial += device->version[i] - '0';
    335 	}
    336 	device_event_emit (abstract, DC_EVENT_DEVINFO, &devinfo);
    337 
    338 	// Allocate a memory buffer.
    339 	dc_buffer_t *buffer = dc_buffer_new (0);
    340 	if (buffer == NULL)
    341 		return DC_STATUS_NOMEMORY;
    342 
    343 	unsigned int ndives = 0;
    344 	dc_status_t rc = DC_STATUS_SUCCESS;
    345 	while ((rc = atomics_cobalt_read_dive (abstract, buffer, (ndives == 0), &progress)) == DC_STATUS_SUCCESS) {
    346 		unsigned char *data = dc_buffer_get_data (buffer);
    347 		unsigned int size = dc_buffer_get_size (buffer);
    348 
    349 		if (size == 0) {
    350 			dc_buffer_free (buffer);
    351 			return DC_STATUS_SUCCESS;
    352 		}
    353 
    354 		if (size < SZ_HEADER) {
    355 			ERROR (abstract->context, "Dive header is too small (%u).", size);
    356 			dc_buffer_free (buffer);
    357 			return DC_STATUS_DATAFORMAT;
    358 		}
    359 
    360 		if (memcmp (data + FP_OFFSET, device->fingerprint, sizeof (device->fingerprint)) == 0) {
    361 			dc_buffer_free (buffer);
    362 			return DC_STATUS_SUCCESS;
    363 		}
    364 
    365 		if (callback && !callback (data, size, data + FP_OFFSET, sizeof (device->fingerprint), userdata)) {
    366 			dc_buffer_free (buffer);
    367 			return DC_STATUS_SUCCESS;
    368 		}
    369 
    370 		ndives++;
    371 	}
    372 
    373 	dc_buffer_free (buffer);
    374 
    375 	return rc;
    376 }