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

suunto_common2.c (13277B)


      1 /*
      2  * libdivecomputer
      3  *
      4  * Copyright (C) 2009 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 <stdlib.h> // malloc
     23 #include <string.h> // memcmp, memcpy
     24 #include <assert.h> // assert
     25 
     26 #include "context-private.h"
     27 #include "suunto_common2.h"
     28 #include "ringbuffer.h"
     29 #include "rbstream.h"
     30 #include "checksum.h"
     31 #include "array.h"
     32 
     33 #define MAXRETRIES 2
     34 
     35 #define SZ_VERSION    0x04
     36 #define SZ_PACKET     0x78
     37 #define SZ_MINIMUM    8
     38 
     39 #define RB_PROFILE_DISTANCE(l,a,b,m)  ringbuffer_distance (a, b, m, l->rb_profile_begin, l->rb_profile_end)
     40 
     41 #define VTABLE(abstract)	((const suunto_common2_device_vtable_t *) abstract->vtable)
     42 
     43 void
     44 suunto_common2_device_init (suunto_common2_device_t *device)
     45 {
     46 	assert (device != NULL);
     47 
     48 	// Set the default values.
     49 	device->layout = NULL;
     50 	memset (device->version, 0, sizeof (device->version));
     51 	memset (device->fingerprint, 0, sizeof (device->fingerprint));
     52 }
     53 
     54 
     55 static dc_status_t
     56 suunto_common2_transfer (dc_device_t *abstract, const unsigned char command[], unsigned int csize, unsigned char answer[], unsigned int asize, unsigned int size)
     57 {
     58 	assert (asize >= size + 4);
     59 
     60 	if (VTABLE (abstract)->packet == NULL)
     61 		return DC_STATUS_UNSUPPORTED;
     62 
     63 	// Occasionally, the dive computer does not respond to a command.
     64 	// In that case we retry the command a number of times before
     65 	// returning an error. Usually the dive computer will respond
     66 	// again during one of the retries.
     67 
     68 	unsigned int nretries = 0;
     69 	dc_status_t rc = DC_STATUS_SUCCESS;
     70 	while ((rc = VTABLE (abstract)->packet (abstract, command, csize, answer, asize, size)) != DC_STATUS_SUCCESS) {
     71 		// Automatically discard a corrupted packet,
     72 		// and request a new one.
     73 		if (rc != DC_STATUS_TIMEOUT && rc != DC_STATUS_PROTOCOL)
     74 			return rc;
     75 
     76 		// Abort if the maximum number of retries is reached.
     77 		if (nretries++ >= MAXRETRIES)
     78 			return rc;
     79 	}
     80 
     81 	return rc;
     82 }
     83 
     84 
     85 dc_status_t
     86 suunto_common2_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size)
     87 {
     88 	suunto_common2_device_t *device = (suunto_common2_device_t*) abstract;
     89 
     90 	if (size && size != sizeof (device->fingerprint))
     91 		return DC_STATUS_INVALIDARGS;
     92 
     93 	if (size)
     94 		memcpy (device->fingerprint, data, sizeof (device->fingerprint));
     95 	else
     96 		memset (device->fingerprint, 0, sizeof (device->fingerprint));
     97 
     98 	return DC_STATUS_SUCCESS;
     99 }
    100 
    101 
    102 dc_status_t
    103 suunto_common2_device_version (dc_device_t *abstract, unsigned char data[], unsigned int size)
    104 {
    105 	if (size < SZ_VERSION) {
    106 		ERROR (abstract->context, "Insufficient buffer space available.");
    107 		return DC_STATUS_INVALIDARGS;
    108 	}
    109 
    110 	unsigned char answer[SZ_VERSION + 4] = {0};
    111 	unsigned char command[4] = {0x0F, 0x00, 0x00, 0x0F};
    112 	dc_status_t rc = suunto_common2_transfer (abstract, command, sizeof (command), answer, sizeof (answer), 4);
    113 	if (rc != DC_STATUS_SUCCESS)
    114 		return rc;
    115 
    116 	memcpy (data, answer + 3, SZ_VERSION);
    117 
    118 	return DC_STATUS_SUCCESS;
    119 }
    120 
    121 
    122 dc_status_t
    123 suunto_common2_device_reset_maxdepth (dc_device_t *abstract)
    124 {
    125 	unsigned char answer[4] = {0};
    126 	unsigned char command[4] = {0x20, 0x00, 0x00, 0x20};
    127 	dc_status_t rc = suunto_common2_transfer (abstract, command, sizeof (command), answer, sizeof (answer), 0);
    128 	if (rc != DC_STATUS_SUCCESS)
    129 		return rc;
    130 
    131 	return DC_STATUS_SUCCESS;
    132 }
    133 
    134 
    135 dc_status_t
    136 suunto_common2_device_read (dc_device_t *abstract, unsigned int address, unsigned char data[], unsigned int size)
    137 {
    138 	unsigned int nbytes = 0;
    139 	while (nbytes < size) {
    140 		// Calculate the package size.
    141 		unsigned int len = size - nbytes;
    142 		if (len > SZ_PACKET)
    143 			len = SZ_PACKET;
    144 
    145 		// Read the package.
    146 		unsigned char answer[SZ_PACKET + 7] = {0};
    147 		unsigned char command[7] = {0x05, 0x00, 0x03,
    148 				(address >> 8) & 0xFF, // high
    149 				(address     ) & 0xFF, // low
    150 				len, // count
    151 				0};  // CRC
    152 		command[6] = checksum_xor_uint8 (command, 6, 0x00);
    153 		dc_status_t rc = suunto_common2_transfer (abstract, command, sizeof (command), answer, len + 7, len);
    154 		if (rc != DC_STATUS_SUCCESS)
    155 			return rc;
    156 
    157 		memcpy (data, answer + 6, len);
    158 
    159 		nbytes += len;
    160 		address += len;
    161 		data += len;
    162 	}
    163 
    164 	return DC_STATUS_SUCCESS;
    165 }
    166 
    167 
    168 dc_status_t
    169 suunto_common2_device_write (dc_device_t *abstract, unsigned int address, const unsigned char data[], unsigned int size)
    170 {
    171 	unsigned int nbytes = 0;
    172 	while (nbytes < size) {
    173 		// Calculate the package size.
    174 		unsigned int len = size - nbytes;
    175 		if (len > SZ_PACKET)
    176 			len = SZ_PACKET;
    177 
    178 		// Write the package.
    179 		unsigned char answer[7] = {0};
    180 		unsigned char command[SZ_PACKET + 7] = {0x06, 0x00, len + 3,
    181 				(address >> 8) & 0xFF, // high
    182 				(address     ) & 0xFF, // low
    183 				len, // count
    184 				0};  // data + CRC
    185 		memcpy (command + 6, data, len);
    186 		command[len + 6] = checksum_xor_uint8 (command, len + 6, 0x00);
    187 		dc_status_t rc = suunto_common2_transfer (abstract, command, len + 7, answer, sizeof (answer), 0);
    188 		if (rc != DC_STATUS_SUCCESS)
    189 			return rc;
    190 
    191 		nbytes += len;
    192 		address += len;
    193 		data += len;
    194 	}
    195 
    196 	return DC_STATUS_SUCCESS;
    197 }
    198 
    199 
    200 dc_status_t
    201 suunto_common2_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
    202 {
    203 	suunto_common2_device_t *device = (suunto_common2_device_t *) abstract;
    204 
    205 	assert (device != NULL);
    206 	assert (device->layout != NULL);
    207 
    208 	// Erase the current contents of the buffer and
    209 	// allocate the required amount of memory.
    210 	if (!dc_buffer_clear (buffer) || !dc_buffer_resize (buffer, device->layout->memsize)) {
    211 		ERROR (abstract->context, "Insufficient buffer space available.");
    212 		return DC_STATUS_NOMEMORY;
    213 	}
    214 
    215 	// Emit a vendor event.
    216 	dc_event_vendor_t vendor;
    217 	vendor.data = device->version;
    218 	vendor.size = sizeof (device->version);
    219 	device_event_emit (abstract, DC_EVENT_VENDOR, &vendor);
    220 
    221 	return device_dump_read (abstract, 0, dc_buffer_get_data (buffer),
    222 		dc_buffer_get_size (buffer), SZ_PACKET);
    223 }
    224 
    225 
    226 dc_status_t
    227 suunto_common2_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata)
    228 {
    229 	suunto_common2_device_t *device = (suunto_common2_device_t*) abstract;
    230 
    231 	assert (device != NULL);
    232 	assert (device->layout != NULL);
    233 
    234 	const suunto_common2_layout_t *layout = device->layout;
    235 
    236 	// Error status for delayed errors.
    237 	dc_status_t status = DC_STATUS_SUCCESS;
    238 
    239 	// Enable progress notifications.
    240 	dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER;
    241 	progress.maximum = layout->rb_profile_end - layout->rb_profile_begin +
    242 		8 + (SZ_MINIMUM > 4 ? SZ_MINIMUM : 4);
    243 	device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);
    244 
    245 	// Emit a vendor event.
    246 	dc_event_vendor_t vendor;
    247 	vendor.data = device->version;
    248 	vendor.size = sizeof (device->version);
    249 	device_event_emit (abstract, DC_EVENT_VENDOR, &vendor);
    250 
    251 	// Read the serial number.
    252 	unsigned char serial[SZ_MINIMUM > 4 ? SZ_MINIMUM : 4] = {0};
    253 	dc_status_t rc = suunto_common2_device_read (abstract, layout->serial, serial, sizeof (serial));
    254 	if (rc != DC_STATUS_SUCCESS) {
    255 		ERROR (abstract->context, "Failed to read the memory header.");
    256 		return rc;
    257 	}
    258 
    259 	// Update and emit a progress event.
    260 	progress.current += sizeof (serial);
    261 	device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);
    262 
    263 	// Emit a device info event.
    264 	dc_event_devinfo_t devinfo;
    265 	devinfo.model = device->version[0];
    266 	devinfo.firmware = array_uint24_be (device->version + 1);
    267 	devinfo.serial = array_convert_bin2dec (serial, 4);
    268 	device_event_emit (abstract, DC_EVENT_DEVINFO, &devinfo);
    269 
    270 	// Read the header bytes.
    271 	unsigned char header[8] = {0};
    272 	rc = suunto_common2_device_read (abstract, 0x0190, header, sizeof (header));
    273 	if (rc != DC_STATUS_SUCCESS) {
    274 		ERROR (abstract->context, "Failed to read the memory header.");
    275 		return rc;
    276 	}
    277 
    278 	// Obtain the pointers from the header.
    279 	unsigned int last  = array_uint16_le (header + 0);
    280 	unsigned int count = array_uint16_le (header + 2);
    281 	unsigned int end   = array_uint16_le (header + 4);
    282 	unsigned int begin = array_uint16_le (header + 6);
    283 	if (last < layout->rb_profile_begin ||
    284 		last >= layout->rb_profile_end ||
    285 		end < layout->rb_profile_begin ||
    286 		end >= layout->rb_profile_end)
    287 	{
    288 		ERROR (abstract->context, "Invalid ringbuffer pointer detected (0x%04x 0x%04x 0x%04x %u).", begin, last, end, count);
    289 		return DC_STATUS_DATAFORMAT;
    290 	}
    291 
    292 	// Calculate the total amount of bytes.
    293 	unsigned int remaining = 0;
    294 	if (begin < layout->rb_profile_begin || begin >= layout->rb_profile_end) {
    295 		// Fall back to downloading the entire ringbuffer as workaround
    296 		// for an invalid begin pointer!
    297 		ERROR (abstract->context, "Invalid ringbuffer pointer detected (0x%04x 0x%04x 0x%04x %u).", begin, last, end, count);
    298 		remaining = layout->rb_profile_end - layout->rb_profile_begin;
    299 	} else {
    300 		remaining = RB_PROFILE_DISTANCE (layout, begin, end, count ? DC_RINGBUFFER_FULL : DC_RINGBUFFER_EMPTY);
    301 	}
    302 
    303 	// Update and emit a progress event.
    304 	progress.maximum -= (layout->rb_profile_end - layout->rb_profile_begin) - remaining;
    305 	progress.current += sizeof (header);
    306 	device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);
    307 
    308 	// Create the ringbuffer stream.
    309 	dc_rbstream_t *rbstream = NULL;
    310 	rc = dc_rbstream_new (&rbstream, abstract, 1, SZ_PACKET, layout->rb_profile_begin, layout->rb_profile_end, end, DC_RBSTREAM_BACKWARD);
    311 	if (rc != DC_STATUS_SUCCESS) {
    312 		ERROR (abstract->context, "Failed to create the ringbuffer stream.");
    313 		return rc;
    314 	}
    315 
    316 	// Memory buffer to store all the dives.
    317 	unsigned char *data = (unsigned char *) malloc (layout->rb_profile_end - layout->rb_profile_begin);
    318 	if (data == NULL) {
    319 		ERROR (abstract->context, "Failed to allocate memory.");
    320 		dc_rbstream_free (rbstream);
    321 		return DC_STATUS_NOMEMORY;
    322 	}
    323 
    324 	// The ring buffer is traversed backwards to retrieve the most recent
    325 	// dives first. This allows us to download only the new dives.
    326 	unsigned int current = last;
    327 	unsigned int previous = end;
    328 	unsigned int offset = remaining;
    329 	while (offset) {
    330 		// Calculate the size of the current dive.
    331 		unsigned int size = RB_PROFILE_DISTANCE (layout, current, previous, DC_RINGBUFFER_FULL);
    332 
    333 		if (size < 4 || size > offset) {
    334 			ERROR (abstract->context, "Unexpected profile size (%u %u).", size, offset);
    335 			dc_rbstream_free (rbstream);
    336 			free (data);
    337 			return DC_STATUS_DATAFORMAT;
    338 		}
    339 
    340 		// Move to the begin of the current dive.
    341 		offset -= size;
    342 
    343 		// Read the dive.
    344 		rc = dc_rbstream_read (rbstream, &progress, data + offset, size);
    345 		if (rc != DC_STATUS_SUCCESS) {
    346 			ERROR (abstract->context, "Failed to read the dive.");
    347 			dc_rbstream_free (rbstream);
    348 			free (data);
    349 			return rc;
    350 		}
    351 
    352 		unsigned char *p = data + offset;
    353 		unsigned int prev = array_uint16_le (p + 0);
    354 		unsigned int next = array_uint16_le (p + 2);
    355 		if (prev < layout->rb_profile_begin ||
    356 			prev >= layout->rb_profile_end ||
    357 			next < layout->rb_profile_begin ||
    358 			next >= layout->rb_profile_end)
    359 		{
    360 			ERROR (abstract->context, "Invalid ringbuffer pointer detected (0x%04x 0x%04x).", prev, next);
    361 			dc_rbstream_free (rbstream);
    362 			free (data);
    363 			return DC_STATUS_DATAFORMAT;
    364 		}
    365 		if (next != previous && next != current) {
    366 			ERROR (abstract->context, "Profiles are not continuous (0x%04x 0x%04x 0x%04x).", current, next, previous);
    367 			dc_rbstream_free (rbstream);
    368 			free (data);
    369 			return DC_STATUS_DATAFORMAT;
    370 		}
    371 
    372 		if (next != current) {
    373 			unsigned int fp_offset = layout->fingerprint + 4;
    374 			if (memcmp (p + fp_offset, device->fingerprint, sizeof (device->fingerprint)) == 0) {
    375 				dc_rbstream_free (rbstream);
    376 				free (data);
    377 				return DC_STATUS_SUCCESS;
    378 			}
    379 
    380 			if (callback && !callback (p + 4, size - 4, p + fp_offset, sizeof (device->fingerprint), userdata)) {
    381 				dc_rbstream_free (rbstream);
    382 				free (data);
    383 				return DC_STATUS_SUCCESS;
    384 			}
    385 		} else {
    386 			ERROR (abstract->context, "Skipping incomplete dive (0x%04x 0x%04x 0x%04x).", current, next, previous);
    387 			status = DC_STATUS_DATAFORMAT;
    388 		}
    389 
    390 		// Next dive.
    391 		previous = current;
    392 		current = prev;
    393 	}
    394 
    395 	dc_rbstream_free (rbstream);
    396 	free (data);
    397 
    398 	return status;
    399 }
    400 
    401 dc_status_t
    402 suunto_common2_device_timesync (dc_device_t *abstract, const dc_datetime_t *datetime)
    403 {
    404 	dc_status_t status = DC_STATUS_SUCCESS;
    405 
    406 	unsigned char answer[5] = {0};
    407 	unsigned char command[11] = {0x10, 0x00, 0x07,
    408 			(datetime->year >> 8) & 0xFF,
    409 			(datetime->year     ) & 0xFF,
    410 			datetime->month,
    411 			datetime->day,
    412 			datetime->hour,
    413 			datetime->minute,
    414 			datetime->second,
    415 			0};  // CRC
    416 	command[10] = checksum_xor_uint8 (command, 10, 0x00);
    417 
    418 	status = suunto_common2_transfer (abstract, command, sizeof (command), answer, sizeof (answer), 1);
    419 	if (status != DC_STATUS_SUCCESS)
    420 		return status;
    421 
    422 	if (answer[3] != 1) {
    423 		ERROR (abstract->context, "Unexpected response code (%u).", answer[3]);
    424 		return DC_STATUS_PROTOCOL;
    425 	}
    426 
    427 	return status;
    428 }