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

shearwater_predator.c (11905B)


      1 /*
      2  * libdivecomputer
      3  *
      4  * Copyright (C) 2012 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 "shearwater_predator.h"
     26 #include "shearwater_common.h"
     27 #include "context-private.h"
     28 #include "device-private.h"
     29 #include "array.h"
     30 
     31 #define ISINSTANCE(device) dc_device_isinstance((device), &shearwater_predator_device_vtable)
     32 
     33 #define SZ_BLOCK   0x80
     34 #define SZ_MEMORY  0x20080
     35 
     36 #define RB_PROFILE_BEGIN 0
     37 #define RB_PROFILE_END   0x1F600
     38 
     39 typedef struct shearwater_predator_device_t {
     40 	shearwater_common_device_t base;
     41 	unsigned char fingerprint[4];
     42 } shearwater_predator_device_t;
     43 
     44 static dc_status_t shearwater_predator_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size);
     45 static dc_status_t shearwater_predator_device_dump (dc_device_t *abstract, dc_buffer_t *buffer);
     46 static dc_status_t shearwater_predator_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata);
     47 
     48 static const dc_device_vtable_t shearwater_predator_device_vtable = {
     49 	sizeof(shearwater_predator_device_t),
     50 	DC_FAMILY_SHEARWATER_PREDATOR,
     51 	shearwater_predator_device_set_fingerprint, /* set_fingerprint */
     52 	NULL, /* read */
     53 	NULL, /* write */
     54 	shearwater_predator_device_dump, /* dump */
     55 	shearwater_predator_device_foreach, /* foreach */
     56 	NULL, /* timesync */
     57 	NULL /* close */
     58 };
     59 
     60 static dc_status_t
     61 shearwater_predator_extract_dives (dc_device_t *device, const unsigned char data[], unsigned int size, dc_dive_callback_t callback, void *userdata);
     62 
     63 dc_status_t
     64 shearwater_predator_device_open (dc_device_t **out, dc_context_t *context, dc_iostream_t *iostream)
     65 {
     66 	dc_status_t status = DC_STATUS_SUCCESS;
     67 	shearwater_predator_device_t *device = NULL;
     68 
     69 	if (out == NULL)
     70 		return DC_STATUS_INVALIDARGS;
     71 
     72 	// Allocate memory.
     73 	device = (shearwater_predator_device_t *) dc_device_allocate (context, &shearwater_predator_device_vtable);
     74 	if (device == NULL) {
     75 		ERROR (context, "Failed to allocate memory.");
     76 		return DC_STATUS_NOMEMORY;
     77 	}
     78 
     79 	// Set the default values.
     80 	memset (device->fingerprint, 0, sizeof (device->fingerprint));
     81 
     82 	// Setup the device.
     83 	status = shearwater_common_setup (&device->base, context, iostream, PREDATOR);
     84 	if (status != DC_STATUS_SUCCESS) {
     85 		goto error_free;
     86 	}
     87 
     88 	*out = (dc_device_t *) device;
     89 
     90 	return DC_STATUS_SUCCESS;
     91 
     92 error_free:
     93 	dc_device_deallocate ((dc_device_t *) device);
     94 	return status;
     95 }
     96 
     97 
     98 static dc_status_t
     99 shearwater_predator_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size)
    100 {
    101 	shearwater_predator_device_t *device = (shearwater_predator_device_t *) abstract;
    102 
    103 	if (size && size != sizeof (device->fingerprint))
    104 		return DC_STATUS_INVALIDARGS;
    105 
    106 	if (size)
    107 		memcpy (device->fingerprint, data, sizeof (device->fingerprint));
    108 	else
    109 		memset (device->fingerprint, 0, sizeof (device->fingerprint));
    110 
    111 	return DC_STATUS_SUCCESS;
    112 }
    113 
    114 
    115 static dc_status_t
    116 shearwater_predator_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
    117 {
    118 	dc_status_t status = DC_STATUS_SUCCESS;
    119 	shearwater_common_device_t *device = (shearwater_common_device_t *) abstract;
    120 
    121 	// Pre-allocate the required amount of memory.
    122 	if (!dc_buffer_reserve (buffer, SZ_MEMORY)) {
    123 		ERROR (abstract->context, "Insufficient buffer space available.");
    124 		return DC_STATUS_NOMEMORY;
    125 	}
    126 
    127 	// Enable progress notifications.
    128 	dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER;
    129 	progress.current = 0;
    130 	progress.maximum = NSTEPS;
    131 
    132 	// Download the memory dump.
    133 	status = shearwater_common_download (device, buffer, 0xDD000000, SZ_MEMORY, 0, &progress);
    134 	if (status != DC_STATUS_SUCCESS) {
    135 		return status;
    136 	}
    137 
    138 	// Emit a device info event.
    139 	unsigned char *data = dc_buffer_get_data (buffer);
    140 	dc_event_devinfo_t devinfo;
    141 	devinfo.model = data[0x2000D];
    142 	devinfo.firmware = bcd2dec (data[0x2000A]);
    143 	devinfo.serial = array_uint32_be (data + 0x20002);
    144 	device_event_emit (abstract, DC_EVENT_DEVINFO, &devinfo);
    145 
    146 	return status;
    147 }
    148 
    149 
    150 static dc_status_t
    151 shearwater_predator_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata)
    152 {
    153 	dc_buffer_t *buffer = dc_buffer_new (SZ_MEMORY);
    154 	if (buffer == NULL)
    155 		return DC_STATUS_NOMEMORY;
    156 
    157 	dc_status_t rc = shearwater_predator_device_dump (abstract, buffer);
    158 	if (rc != DC_STATUS_SUCCESS) {
    159 		dc_buffer_free (buffer);
    160 		return rc;
    161 	}
    162 
    163 	rc = shearwater_predator_extract_dives (abstract, dc_buffer_get_data (buffer),
    164 		dc_buffer_get_size (buffer), callback, userdata);
    165 
    166 	dc_buffer_free (buffer);
    167 
    168 	return rc;
    169 }
    170 
    171 
    172 static dc_status_t
    173 shearwater_predator_extract_predator (dc_device_t *abstract, const unsigned char data[], unsigned int size, dc_dive_callback_t callback, void *userdata)
    174 {
    175 	shearwater_predator_device_t *device = (shearwater_predator_device_t*) abstract;
    176 	dc_context_t *context = (abstract ? abstract->context : NULL);
    177 
    178 	// Locate the most recent dive.
    179 	// The device maintains an internal counter which is incremented for every
    180 	// dive, and the current value at the time of the dive is stored in the
    181 	// dive header. Thus the most recent dive will have the highest value.
    182 	unsigned int maximum = 0;
    183 	unsigned int eop = RB_PROFILE_END;
    184 
    185 	// Search the ringbuffer backwards to locate matching header and
    186 	// footer markers. Because the ringbuffer search algorithm starts at
    187 	// some arbitrary position, which does not necessary corresponds
    188 	// with a boundary between two dives, the begin position is adjusted
    189 	// as soon as the first dive has been found. Without this step,
    190 	// dives crossing the ringbuffer wrap point won't be detected when
    191 	// searching backwards from the ringbuffer end offset.
    192 	unsigned int footer = 0;
    193 	unsigned int have_footer = 0;
    194 	unsigned int begin = RB_PROFILE_BEGIN;
    195 	unsigned int offset = RB_PROFILE_END;
    196 	while (offset != begin) {
    197 		// Handle the ringbuffer wrap point.
    198 		if (offset == RB_PROFILE_BEGIN)
    199 			offset = RB_PROFILE_END;
    200 
    201 		// Move to the start of the block.
    202 		offset -= SZ_BLOCK;
    203 
    204 		if (array_isequal (data + offset, SZ_BLOCK, 0xFF)) {
    205 			// Ignore empty blocks explicitly, because otherwise they are
    206 			// incorrectly recognized as header markers.
    207 		} else if (data[offset + 0] == 0xFF && data[offset + 1] == 0xFF && have_footer) {
    208 			// If the first header marker is found, the begin offset is moved
    209 			// after the corresponding footer marker. This is necessary to be
    210 			// able to detect dives that cross the ringbuffer wrap point.
    211 			if (begin == RB_PROFILE_BEGIN)
    212 				begin = footer + SZ_BLOCK;
    213 
    214 			// Get the internal dive number.
    215 			unsigned int current = array_uint16_be (data + offset + 2);
    216 			if (current > maximum) {
    217 				maximum = current;
    218 				eop = footer + SZ_BLOCK;
    219 			}
    220 
    221 			// The dive number in the header and footer should be identical.
    222 			if (current != array_uint16_be (data + footer + 2)) {
    223 				ERROR (context, "Unexpected dive number.");
    224 				return DC_STATUS_DATAFORMAT;
    225 			}
    226 
    227 			// Reset the footer marker.
    228 			have_footer = 0;
    229 		} else if (data[offset + 0] == 0xFF && data[offset + 1] == 0xFE) {
    230 			// Remember the footer marker.
    231 			footer = offset;
    232 			have_footer = 1;
    233 		}
    234 	}
    235 
    236 	// Allocate memory for the profiles.
    237 	unsigned char *buffer = (unsigned char *) malloc (RB_PROFILE_END - RB_PROFILE_BEGIN + SZ_BLOCK);
    238 	if (buffer == NULL) {
    239 		return DC_STATUS_NOMEMORY;
    240 	}
    241 
    242 	// Linearize the ringbuffer.
    243 	memcpy (buffer + 0, data + eop, RB_PROFILE_END - eop);
    244 	memcpy (buffer + RB_PROFILE_END - eop, data + RB_PROFILE_BEGIN, eop - RB_PROFILE_BEGIN);
    245 
    246 	// Find the dives again in the linear buffer.
    247 	footer = 0;
    248 	have_footer = 0;
    249 	offset = RB_PROFILE_END;
    250 	while (offset != RB_PROFILE_BEGIN) {
    251 		// Move to the start of the block.
    252 		offset -= SZ_BLOCK;
    253 
    254 		if (array_isequal (buffer + offset, SZ_BLOCK, 0xFF)) {
    255 			break;
    256 		} else if (buffer[offset + 0] == 0xFF && buffer[offset + 1] == 0xFF && have_footer) {
    257 			// Append the final block.
    258 			unsigned int length = footer + SZ_BLOCK - offset;
    259 			memcpy (buffer + offset + length, data + SZ_MEMORY - SZ_BLOCK, SZ_BLOCK);
    260 
    261 			// Check the fingerprint data.
    262 			if (device && memcmp (buffer + offset + 12, device->fingerprint, sizeof (device->fingerprint)) == 0)
    263 				break;
    264 
    265 			if (callback && !callback (buffer + offset, length + SZ_BLOCK, buffer + offset + 12, sizeof (device->fingerprint), userdata))
    266 				break;
    267 
    268 			have_footer = 0;
    269 		} else if (buffer[offset + 0] == 0xFF && buffer[offset + 1] == 0xFE) {
    270 			footer = offset;
    271 			have_footer = 1;
    272 		}
    273 	}
    274 
    275 	free (buffer);
    276 
    277 	return DC_STATUS_SUCCESS;
    278 }
    279 
    280 
    281 static dc_status_t
    282 shearwater_predator_extract_petrel (dc_device_t *abstract, const unsigned char data[], unsigned int size, dc_dive_callback_t callback, void *userdata)
    283 {
    284 	shearwater_predator_device_t *device = (shearwater_predator_device_t*) abstract;
    285 	dc_context_t *context = (abstract ? abstract->context : NULL);
    286 
    287 	// Allocate memory for the profiles.
    288 	unsigned char *buffer = (unsigned char *) malloc (RB_PROFILE_END - RB_PROFILE_BEGIN + SZ_BLOCK);
    289 	if (buffer == NULL) {
    290 		return DC_STATUS_NOMEMORY;
    291 	}
    292 
    293 	// Search the ringbuffer to locate matching header and footer
    294 	// markers. Because the Petrel does reorder the internal ringbuffer
    295 	// before sending the data, the most recent dive is always the first
    296 	// one. Therefore, there is no need to search for it, as we have to
    297 	// do for the Predator.
    298 	unsigned int header = 0;
    299 	unsigned int have_header = 0;
    300 	unsigned int offset = RB_PROFILE_BEGIN;
    301 	while (offset != RB_PROFILE_END) {
    302 		if (array_isequal (data + offset, SZ_BLOCK, 0xFF)) {
    303 			// Ignore empty blocks explicitly, because otherwise they are
    304 			// incorrectly recognized as header markers.
    305 			break;
    306 		} else if (data[offset + 0] == 0xFF && data[offset + 1] == 0xFF) {
    307 			// Remember the header marker.
    308 			header = offset;
    309 			have_header = 1;
    310 		} else if (data[offset + 0] == 0xFF && data[offset + 1] == 0xFE && have_header) {
    311 			// The dive number in the header and footer should be identical.
    312 			if (memcmp (data + header + 2, data + offset + 2, 2) != 0) {
    313 				ERROR (context, "Unexpected dive number.");
    314 				free (buffer);
    315 				return DC_STATUS_DATAFORMAT;
    316 			}
    317 
    318 			// Append the final block.
    319 			unsigned int length = offset + SZ_BLOCK - header;
    320 			memcpy (buffer, data + header, length);
    321 			memcpy (buffer + length, data + SZ_MEMORY - SZ_BLOCK, SZ_BLOCK);
    322 
    323 			// Check the fingerprint data.
    324 			if (device && memcmp (buffer + 12, device->fingerprint, sizeof (device->fingerprint)) == 0)
    325 				break;
    326 
    327 			if (callback && !callback (buffer, length + SZ_BLOCK, buffer + 12, sizeof (device->fingerprint), userdata))
    328 				break;
    329 
    330 			// Reset the header marker.
    331 			have_header = 0;
    332 		}
    333 
    334 		offset += SZ_BLOCK;
    335 	}
    336 
    337 	free (buffer);
    338 
    339 	return DC_STATUS_SUCCESS;
    340 }
    341 
    342 
    343 static dc_status_t
    344 shearwater_predator_extract_dives (dc_device_t *abstract, const unsigned char data[], unsigned int size, dc_dive_callback_t callback, void *userdata)
    345 {
    346 	if (abstract && !ISINSTANCE (abstract))
    347 		return DC_STATUS_INVALIDARGS;
    348 
    349 	if (size < SZ_MEMORY)
    350 		return DC_STATUS_DATAFORMAT;
    351 
    352 	unsigned int model = data[0x2000D];
    353 
    354 	if (model == PETREL) {
    355 		return shearwater_predator_extract_petrel (abstract, data, size, callback, userdata);
    356 	} else {
    357 		return shearwater_predator_extract_predator (abstract, data, size, callback, userdata);
    358 	}
    359 }