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

mares_darwin.c (10259B)


      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 <stdlib.h>
     23 #include <string.h>
     24 #include <assert.h>
     25 
     26 #include <libdivecomputer/units.h>
     27 
     28 #include "mares_darwin.h"
     29 #include "mares_common.h"
     30 #include "context-private.h"
     31 #include "device-private.h"
     32 #include "array.h"
     33 
     34 #define ISINSTANCE(device) dc_device_isinstance((device), &mares_darwin_device_vtable)
     35 
     36 #define DARWIN    0
     37 #define DARWINAIR 1
     38 
     39 typedef struct mares_darwin_layout_t {
     40 	// Memory size.
     41 	unsigned int memsize;
     42 	// Logbook ringbuffer.
     43 	unsigned int rb_logbook_offset;
     44 	unsigned int rb_logbook_size;
     45 	unsigned int rb_logbook_count;
     46 	// Profile ringbuffer
     47 	unsigned int rb_profile_begin;
     48 	unsigned int rb_profile_end;
     49 	// Sample size
     50 	unsigned int samplesize;
     51 } mares_darwin_layout_t;
     52 
     53 typedef struct mares_darwin_device_t {
     54 	mares_common_device_t base;
     55 	const mares_darwin_layout_t *layout;
     56 	unsigned int model;
     57 	unsigned char fingerprint[6];
     58 } mares_darwin_device_t;
     59 
     60 static dc_status_t mares_darwin_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size);
     61 static dc_status_t mares_darwin_device_dump (dc_device_t *abstract, dc_buffer_t *buffer);
     62 static dc_status_t mares_darwin_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata);
     63 
     64 static const dc_device_vtable_t mares_darwin_device_vtable = {
     65 	sizeof(mares_darwin_device_t),
     66 	DC_FAMILY_MARES_DARWIN,
     67 	mares_darwin_device_set_fingerprint, /* set_fingerprint */
     68 	mares_common_device_read, /* read */
     69 	NULL, /* write */
     70 	mares_darwin_device_dump, /* dump */
     71 	mares_darwin_device_foreach, /* foreach */
     72 	NULL, /* timesync */
     73 	NULL /* close */
     74 };
     75 
     76 static const mares_darwin_layout_t mares_darwin_layout = {
     77 	0x4000, /* memsize */
     78 	0x0100, /* rb_logbook_offset */
     79 	52,     /* rb_logbook_size */
     80 	50,     /* rb_logbook_count */
     81 	0x0B30, /* rb_profile_begin */
     82 	0x4000, /* rb_profile_end */
     83 	2       /* samplesize */
     84 };
     85 
     86 static const mares_darwin_layout_t mares_darwinair_layout = {
     87 	0x4000, /* memsize */
     88 	0x0100, /* rb_logbook_offset */
     89 	60,     /* rb_logbook_size */
     90 	50,     /* rb_logbook_count */
     91 	0x0CC0, /* rb_profile_begin */
     92 	0x3FFF, /* rb_profile_end */
     93 	3       /* samplesize */
     94 };
     95 
     96 static dc_status_t
     97 mares_darwin_extract_dives (dc_device_t *device, const unsigned char data[], unsigned int size, dc_dive_callback_t callback, void *userdata);
     98 
     99 dc_status_t
    100 mares_darwin_device_open (dc_device_t **out, dc_context_t *context, dc_iostream_t *iostream, unsigned int model)
    101 {
    102 	dc_status_t status = DC_STATUS_SUCCESS;
    103 	mares_darwin_device_t *device = NULL;
    104 
    105 	if (out == NULL)
    106 		return DC_STATUS_INVALIDARGS;
    107 
    108 	// Allocate memory.
    109 	device = (mares_darwin_device_t *) dc_device_allocate (context, &mares_darwin_device_vtable);
    110 	if (device == NULL) {
    111 		ERROR (context, "Failed to allocate memory.");
    112 		return DC_STATUS_NOMEMORY;
    113 	}
    114 
    115 	// Initialize the base class.
    116 	mares_common_device_init (&device->base, iostream);
    117 
    118 	// Set the default values.
    119 	memset (device->fingerprint, 0, sizeof (device->fingerprint));
    120 	device->model = model;
    121 	if (model == DARWINAIR)
    122 		device->layout = &mares_darwinair_layout;
    123 	else
    124 		device->layout = &mares_darwin_layout;
    125 
    126 	// Set the serial communication protocol (9600 8N1).
    127 	status = dc_iostream_configure (device->base.iostream, 9600, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
    128 	if (status != DC_STATUS_SUCCESS) {
    129 		ERROR (context, "Failed to set the terminal attributes.");
    130 		goto error_free;
    131 	}
    132 
    133 	// Set the timeout for receiving data (1000 ms).
    134 	status = dc_iostream_set_timeout (device->base.iostream, 1000);
    135 	if (status != DC_STATUS_SUCCESS) {
    136 		ERROR (context, "Failed to set the timeout.");
    137 		goto error_free;
    138 	}
    139 
    140 	// Set the DTR line.
    141 	status = dc_iostream_set_dtr (device->base.iostream, 1);
    142 	if (status != DC_STATUS_SUCCESS) {
    143 		ERROR (context, "Failed to set the DTR line.");
    144 		goto error_free;
    145 	}
    146 
    147 	// Set the RTS line.
    148 	status = dc_iostream_set_rts (device->base.iostream, 1);
    149 	if (status != DC_STATUS_SUCCESS) {
    150 		ERROR (context, "Failed to set the RTS line.");
    151 		goto error_free;
    152 	}
    153 
    154 	// Make sure everything is in a sane state.
    155 	dc_iostream_sleep (device->base.iostream, 100);
    156 	dc_iostream_purge (device->base.iostream, DC_DIRECTION_ALL);
    157 
    158 	// Override the base class values.
    159 	device->base.echo = 1;
    160 	device->base.delay = 50;
    161 
    162 	*out = (dc_device_t *) device;
    163 
    164 	return DC_STATUS_SUCCESS;
    165 
    166 error_free:
    167 	dc_device_deallocate ((dc_device_t *) device);
    168 	return status;
    169 }
    170 
    171 
    172 static dc_status_t
    173 mares_darwin_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size)
    174 {
    175 	mares_darwin_device_t *device = (mares_darwin_device_t *) abstract;
    176 
    177 	if (size && size != sizeof (device->fingerprint))
    178 		return DC_STATUS_INVALIDARGS;
    179 
    180 	if (size)
    181 		memcpy (device->fingerprint, data, sizeof (device->fingerprint));
    182 	else
    183 		memset (device->fingerprint, 0, sizeof (device->fingerprint));
    184 
    185 	return DC_STATUS_SUCCESS;
    186 }
    187 
    188 
    189 static dc_status_t
    190 mares_darwin_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
    191 {
    192 	dc_status_t status = DC_STATUS_SUCCESS;
    193 	mares_darwin_device_t *device = (mares_darwin_device_t *) abstract;
    194 
    195 	assert (device->layout != NULL);
    196 
    197 	// Allocate the required amount of memory.
    198 	if (!dc_buffer_resize (buffer, device->layout->memsize)) {
    199 		ERROR (abstract->context, "Insufficient buffer space available.");
    200 		return DC_STATUS_NOMEMORY;
    201 	}
    202 
    203 	// Download the memory dump.
    204 	status = device_dump_read (abstract, 0, dc_buffer_get_data (buffer),
    205 		dc_buffer_get_size (buffer), PACKETSIZE);
    206 	if (status != DC_STATUS_SUCCESS) {
    207 		return status;
    208 	}
    209 
    210 	// Emit a device info event.
    211 	unsigned char *data = dc_buffer_get_data (buffer);
    212 	dc_event_devinfo_t devinfo;
    213 	devinfo.model = device->model;
    214 	devinfo.firmware = 0;
    215 	devinfo.serial = array_uint16_be (data + 8);
    216 	device_event_emit (abstract, DC_EVENT_DEVINFO, &devinfo);
    217 
    218 	return status;
    219 }
    220 
    221 
    222 static dc_status_t
    223 mares_darwin_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata)
    224 {
    225 	mares_darwin_device_t *device = (mares_darwin_device_t *) abstract;
    226 
    227 	assert (device->layout != NULL);
    228 
    229 	dc_buffer_t *buffer = dc_buffer_new (device->layout->memsize);
    230 	if (buffer == NULL)
    231 		return DC_STATUS_NOMEMORY;
    232 
    233 	dc_status_t rc = mares_darwin_device_dump (abstract, buffer);
    234 	if (rc != DC_STATUS_SUCCESS) {
    235 		dc_buffer_free (buffer);
    236 		return rc;
    237 	}
    238 
    239 	rc = mares_darwin_extract_dives (abstract, dc_buffer_get_data (buffer),
    240 		dc_buffer_get_size (buffer), callback, userdata);
    241 
    242 	dc_buffer_free (buffer);
    243 
    244 	return rc;
    245 }
    246 
    247 
    248 static dc_status_t
    249 mares_darwin_extract_dives (dc_device_t *abstract, const unsigned char data[], unsigned int size, dc_dive_callback_t callback, void *userdata)
    250 {
    251 	mares_darwin_device_t *device = (mares_darwin_device_t *) abstract;
    252 
    253 	if (!ISINSTANCE (abstract))
    254 		return DC_STATUS_INVALIDARGS;
    255 
    256 	assert (device->layout != NULL);
    257 
    258 	const mares_darwin_layout_t *layout = device->layout;
    259 
    260 	// Get the profile pointer.
    261 	unsigned int eop = array_uint16_be (data + 0x8A);
    262 	if (eop < layout->rb_profile_begin || eop >= layout->rb_profile_end) {
    263 		ERROR (abstract->context, "Invalid ringbuffer pointer detected (0x%04x).", eop);
    264 		return DC_STATUS_DATAFORMAT;
    265 	}
    266 
    267 	// Get the logbook index.
    268 	unsigned int last = data[0x8C];
    269 	if (last >= layout->rb_logbook_count) {
    270 		ERROR (abstract->context, "Invalid ringbuffer pointer detected (0x%02x).", last);
    271 		return DC_STATUS_DATAFORMAT;
    272 	}
    273 
    274 	// Allocate memory for the largest possible dive.
    275 	unsigned char *buffer = (unsigned char *) malloc (layout->rb_logbook_size + layout->rb_profile_end - layout->rb_profile_begin);
    276 	if (buffer == NULL) {
    277 		ERROR (abstract->context, "Failed to allocate memory.");
    278 		return DC_STATUS_NOMEMORY;
    279 	}
    280 
    281 	// The logbook ringbuffer can store a fixed amount of entries, but there
    282 	// is no guarantee that the profile ringbuffer will contain a profile for
    283 	// each entry. The number of remaining bytes (which is initialized to the
    284 	// largest possible value) is used to detect the last valid profile.
    285 	unsigned int remaining = layout->rb_profile_end - layout->rb_profile_begin;
    286 
    287 	unsigned int current = eop;
    288 	for (unsigned int i = 0; i < layout->rb_logbook_count; ++i) {
    289 		// Get the offset to the current logbook entry in the ringbuffer.
    290 		unsigned int idx = (layout->rb_logbook_count + last - i) % layout->rb_logbook_count;
    291 		unsigned int offset = layout->rb_logbook_offset + idx * layout->rb_logbook_size;
    292 
    293 		// Get the length of the current dive.
    294 		unsigned int nsamples = array_uint16_be (data + offset + 6);
    295 		unsigned int length = nsamples * layout->samplesize;
    296 		if (nsamples == 0xFFFF || length > remaining)
    297 			break;
    298 
    299 		// Copy the logbook entry.
    300 		memcpy (buffer, data + offset, layout->rb_logbook_size);
    301 
    302 		// Copy the profile data.
    303 		if (current < layout->rb_profile_begin + length) {
    304 			unsigned int a = current - layout->rb_profile_begin;
    305 			unsigned int b = length - a;
    306 			memcpy (buffer + layout->rb_logbook_size, data + layout->rb_profile_end - b, b);
    307 			memcpy (buffer + layout->rb_logbook_size + b, data + layout->rb_profile_begin, a);
    308 			current = layout->rb_profile_end - b;
    309 		} else {
    310 			memcpy (buffer + layout->rb_logbook_size, data + current - length, length);
    311 			current -= length;
    312 		}
    313 
    314 		if (memcmp (buffer, device->fingerprint, sizeof (device->fingerprint)) == 0) {
    315 			free (buffer);
    316 			return DC_STATUS_SUCCESS;
    317 		}
    318 
    319 		if (callback && !callback (buffer, layout->rb_logbook_size + length, buffer, 6, userdata)) {
    320 			free (buffer);
    321 			return DC_STATUS_SUCCESS;
    322 		}
    323 
    324 		remaining -= length;
    325 	}
    326 
    327 	free (buffer);
    328 
    329 	return DC_STATUS_SUCCESS;
    330 }