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

uwatec_aladin.c (11792B)


      1 /*
      2  * libdivecomputer
      3  *
      4  * Copyright (C) 2008 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, free
     23 #include <memory.h> // memcpy
     24 
     25 #include "uwatec_aladin.h"
     26 #include "context-private.h"
     27 #include "device-private.h"
     28 #include "ringbuffer.h"
     29 #include "checksum.h"
     30 #include "array.h"
     31 
     32 #define ISINSTANCE(device) dc_device_isinstance((device), &uwatec_aladin_device_vtable)
     33 
     34 #define SZ_MEMORY 2048
     35 
     36 #define RB_PROFILE_BEGIN			0x000
     37 #define RB_PROFILE_END				0x600
     38 #define RB_PROFILE_NEXT(a)			ringbuffer_increment (a, 1, RB_PROFILE_BEGIN, RB_PROFILE_END)
     39 #define RB_PROFILE_DISTANCE(a,b)	ringbuffer_distance (a, b, DC_RINGBUFFER_EMPTY, RB_PROFILE_BEGIN, RB_PROFILE_END)
     40 
     41 #define HEADER 4
     42 
     43 typedef struct uwatec_aladin_device_t {
     44 	dc_device_t base;
     45 	dc_iostream_t *iostream;
     46 	unsigned int timestamp;
     47 	unsigned int devtime;
     48 	dc_ticks_t systime;
     49 } uwatec_aladin_device_t ;
     50 
     51 static dc_status_t uwatec_aladin_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size);
     52 static dc_status_t uwatec_aladin_device_dump (dc_device_t *abstract, dc_buffer_t *buffer);
     53 static dc_status_t uwatec_aladin_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata);
     54 
     55 static const dc_device_vtable_t uwatec_aladin_device_vtable = {
     56 	sizeof(uwatec_aladin_device_t),
     57 	DC_FAMILY_UWATEC_ALADIN,
     58 	uwatec_aladin_device_set_fingerprint, /* set_fingerprint */
     59 	NULL, /* read */
     60 	NULL, /* write */
     61 	uwatec_aladin_device_dump, /* dump */
     62 	uwatec_aladin_device_foreach, /* foreach */
     63 	NULL, /* timesync */
     64 	NULL /* close */
     65 };
     66 
     67 static dc_status_t
     68 uwatec_aladin_extract_dives (dc_device_t *device, const unsigned char data[], unsigned int size, dc_dive_callback_t callback, void *userdata);
     69 
     70 dc_status_t
     71 uwatec_aladin_device_open (dc_device_t **out, dc_context_t *context, dc_iostream_t *iostream)
     72 {
     73 	dc_status_t status = DC_STATUS_SUCCESS;
     74 	uwatec_aladin_device_t *device = NULL;
     75 
     76 	if (out == NULL)
     77 		return DC_STATUS_INVALIDARGS;
     78 
     79 	// Allocate memory.
     80 	device = (uwatec_aladin_device_t *) dc_device_allocate (context, &uwatec_aladin_device_vtable);
     81 	if (device == NULL) {
     82 		ERROR (context, "Failed to allocate memory.");
     83 		return DC_STATUS_NOMEMORY;
     84 	}
     85 
     86 	// Set the default values.
     87 	device->iostream = iostream;
     88 	device->timestamp = 0;
     89 	device->systime = (dc_ticks_t) -1;
     90 	device->devtime = 0;
     91 
     92 	// Set the serial communication protocol (19200 8N1).
     93 	status = dc_iostream_configure (device->iostream, 19200, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
     94 	if (status != DC_STATUS_SUCCESS) {
     95 		ERROR (context, "Failed to set the terminal attributes.");
     96 		goto error_free;
     97 	}
     98 
     99 	// Set the timeout for receiving data (3000ms).
    100 	status = dc_iostream_set_timeout (device->iostream, 3000);
    101 	if (status != DC_STATUS_SUCCESS) {
    102 		ERROR (context, "Failed to set the timeout.");
    103 		goto error_free;
    104 	}
    105 
    106 	// Set the DTR line.
    107 	status = dc_iostream_set_dtr (device->iostream, 1);
    108 	if (status != DC_STATUS_SUCCESS) {
    109 		ERROR (context, "Failed to set the DTR line.");
    110 		goto error_free;
    111 	}
    112 
    113 	// Clear the RTS line.
    114 	status = dc_iostream_set_rts (device->iostream, 0);
    115 	if (status != DC_STATUS_SUCCESS) {
    116 		ERROR (context, "Failed to clear the RTS line.");
    117 		goto error_free;
    118 	}
    119 
    120 	*out = (dc_device_t*) device;
    121 
    122 	return DC_STATUS_SUCCESS;
    123 
    124 error_free:
    125 	dc_device_deallocate ((dc_device_t *) device);
    126 	return status;
    127 }
    128 
    129 
    130 static dc_status_t
    131 uwatec_aladin_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size)
    132 {
    133 	uwatec_aladin_device_t *device = (uwatec_aladin_device_t*) abstract;
    134 
    135 	if (size && size != 4)
    136 		return DC_STATUS_INVALIDARGS;
    137 
    138 	if (size)
    139 		device->timestamp = array_uint32_le (data);
    140 	else
    141 		device->timestamp = 0;
    142 
    143 	return DC_STATUS_SUCCESS;
    144 }
    145 
    146 
    147 static dc_status_t
    148 uwatec_aladin_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
    149 {
    150 	dc_status_t status = DC_STATUS_SUCCESS;
    151 	uwatec_aladin_device_t *device = (uwatec_aladin_device_t*) abstract;
    152 
    153 	// Pre-allocate the required amount of memory.
    154 	if (!dc_buffer_reserve (buffer, SZ_MEMORY)) {
    155 		ERROR (abstract->context, "Insufficient buffer space available.");
    156 		return DC_STATUS_NOMEMORY;
    157 	}
    158 
    159 	// Enable progress notifications.
    160 	dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER;
    161 	progress.maximum = SZ_MEMORY + 2;
    162 	device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);
    163 
    164 	unsigned char answer[SZ_MEMORY + 2] = {0};
    165 
    166 	// Receive the header of the package.
    167 	unsigned int i = 0;
    168 	while (i < 4) {
    169 		if (device_is_cancelled (abstract))
    170 			return DC_STATUS_CANCELLED;
    171 
    172 		status = dc_iostream_read (device->iostream, answer + i, 1, NULL);
    173 		if (status != DC_STATUS_SUCCESS) {
    174 			ERROR (abstract->context, "Failed to receive the answer.");
    175 			if (status != DC_STATUS_TIMEOUT)
    176 				return status;
    177 		}
    178 
    179 		const unsigned char expected = i < 3 ? 0x55 : 0x00;
    180 		if (status != DC_STATUS_SUCCESS || answer[i] != expected) {
    181 			device_event_emit (abstract, DC_EVENT_WAITING, NULL);
    182 			i = 0;
    183 		} else {
    184 			i++;
    185 		}
    186 	}
    187 
    188 	// Fetch the current system time.
    189 	dc_ticks_t now = dc_datetime_now ();
    190 
    191 	// Update and emit a progress event.
    192 	progress.current += 4;
    193 	device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);
    194 
    195 	// Receive the remaining part of the package.
    196 	status = dc_iostream_read (device->iostream, answer + 4, sizeof (answer) - 4, NULL);
    197 	if (status != DC_STATUS_SUCCESS) {
    198 		ERROR (abstract->context, "Unexpected EOF in answer.");
    199 		return status;
    200 	}
    201 
    202 	// Update and emit a progress event.
    203 	progress.current += sizeof (answer) - 4;
    204 	device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);
    205 
    206 	// Reverse the bit order.
    207 	array_reverse_bits (answer, sizeof (answer));
    208 
    209 	// Verify the checksum of the package.
    210 	unsigned short crc = array_uint16_le (answer + SZ_MEMORY);
    211 	unsigned short ccrc = checksum_add_uint16 (answer, SZ_MEMORY, 0x0000);
    212 	if (ccrc != crc) {
    213 		ERROR (abstract->context, "Unexpected answer checksum.");
    214 		return DC_STATUS_PROTOCOL;
    215 	}
    216 
    217 	// Store the clock calibration values.
    218 	device->systime = now;
    219 	device->devtime = array_uint32_be (answer + HEADER + 0x7f8);
    220 
    221 	// Emit a clock event.
    222 	dc_event_clock_t clock;
    223 	clock.systime = device->systime;
    224 	clock.devtime = device->devtime;
    225 	device_event_emit (abstract, DC_EVENT_CLOCK, &clock);
    226 
    227 	dc_buffer_append (buffer, answer, SZ_MEMORY);
    228 
    229 	return DC_STATUS_SUCCESS;
    230 }
    231 
    232 
    233 static dc_status_t
    234 uwatec_aladin_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata)
    235 {
    236 	dc_buffer_t *buffer = dc_buffer_new (SZ_MEMORY);
    237 	if (buffer == NULL)
    238 		return DC_STATUS_NOMEMORY;
    239 
    240 	dc_status_t rc = uwatec_aladin_device_dump (abstract, buffer);
    241 	if (rc != DC_STATUS_SUCCESS) {
    242 		dc_buffer_free (buffer);
    243 		return rc;
    244 	}
    245 
    246 	// Emit a device info event.
    247 	unsigned char *data = dc_buffer_get_data (buffer);
    248 	dc_event_devinfo_t devinfo;
    249 	devinfo.model = data[HEADER + 0x7bc];
    250 	devinfo.firmware = 0;
    251 	devinfo.serial = array_uint24_be (data + HEADER + 0x7ed);
    252 	device_event_emit (abstract, DC_EVENT_DEVINFO, &devinfo);
    253 
    254 	rc = uwatec_aladin_extract_dives (abstract,
    255 		dc_buffer_get_data (buffer), dc_buffer_get_size (buffer), callback, userdata);
    256 
    257 	dc_buffer_free (buffer);
    258 
    259 	return rc;
    260 }
    261 
    262 
    263 static dc_status_t
    264 uwatec_aladin_extract_dives (dc_device_t *abstract, const unsigned char* data, unsigned int size, dc_dive_callback_t callback, void *userdata)
    265 {
    266 	uwatec_aladin_device_t *device = (uwatec_aladin_device_t*) abstract;
    267 
    268 	if (abstract && !ISINSTANCE (abstract))
    269 		return DC_STATUS_INVALIDARGS;
    270 
    271 	if (size < SZ_MEMORY)
    272 		return DC_STATUS_DATAFORMAT;
    273 
    274 	// The logbook ring buffer can store up to 37 dives. But
    275 	// if the total number of dives is less, not all logbook
    276 	// entries contain valid data.
    277 	unsigned int ndives = array_uint16_be (data + HEADER + 0x7f2);
    278 	if (ndives > 37)
    279 		ndives = 37;
    280 
    281 	// Get the index to the newest logbook entry. This value is
    282 	// normally in the range from 1 to 37 and is converted to
    283 	// a zero based index, taking care not to underflow.
    284 	unsigned int eol = (data[HEADER + 0x7f4] + 37 - 1) % 37;
    285 
    286 	// Get the end of the profile ring buffer. This value points
    287 	// to the last byte of the last profile and is incremented
    288 	// one byte to point immediately after the last profile.
    289 	unsigned int eop = RB_PROFILE_NEXT (data[HEADER + 0x7f6] +
    290 		(((data[HEADER + 0x7f7] & 0x0F) >> 1) << 8));
    291 
    292 	// Start scanning the profile ringbuffer.
    293 	int profiles = 1;
    294 
    295 	// Both ring buffers are traversed backwards to retrieve the most recent
    296 	// dives first. This allows you to download only the new dives and avoids
    297 	// having to rely on the number of profiles in the ring buffer (which
    298 	// is buggy according to the documentation). During the traversal, the
    299 	// previous pointer does always point to the end of the dive data and
    300 	// we move the current pointer backwards until a start marker is found.
    301 	unsigned int previous = eop;
    302 	unsigned int current = eop;
    303 	for (unsigned int i = 0; i < ndives; ++i) {
    304 		// Memory buffer to store one dive.
    305 		unsigned char buffer[18 + RB_PROFILE_END - RB_PROFILE_BEGIN] = {0};
    306 
    307 		// Get the offset to the current logbook entry.
    308 		unsigned int offset = ((eol + 37 - i) % 37) * 12 + RB_PROFILE_END;
    309 
    310 		// Copy the serial number, type and logbook data
    311 		// to the buffer and set the profile length to zero.
    312 		memcpy (buffer + 0, data + HEADER + 0x07ed, 3);
    313 		memcpy (buffer + 3, data + HEADER + 0x07bc, 1);
    314 		memcpy (buffer + 4, data + HEADER + offset, 12);
    315 		memset (buffer + 16, 0, 2);
    316 
    317 		// Convert the timestamp from the Aladin (big endian)
    318 		// to the Memomouse format (little endian).
    319 		array_reverse_bytes (buffer + 11, 4);
    320 
    321 		unsigned int len = 0;
    322 		if (profiles) {
    323 			// Search the profile ringbuffer for a start marker.
    324 			do {
    325 				if (current == RB_PROFILE_BEGIN)
    326 					current = RB_PROFILE_END;
    327 				current--;
    328 
    329 				if (data[HEADER + current] == 0xFF) {
    330 					len = RB_PROFILE_DISTANCE (current, previous);
    331 					previous = current;
    332 					break;
    333 				}
    334 			} while (current != eop);
    335 
    336 			if (len >= 1) {
    337 				// Skip the start marker.
    338 				len--;
    339 				unsigned int begin = RB_PROFILE_NEXT (current);
    340 				// Set the profile length.
    341 				buffer[16] = (len     ) & 0xFF;
    342 				buffer[17] = (len >> 8) & 0xFF;
    343 				// Copy the profile data.
    344 				if (begin + len > RB_PROFILE_END) {
    345 					unsigned int a = RB_PROFILE_END - begin;
    346 					unsigned int b = (begin + len) - RB_PROFILE_END;
    347 					memcpy (buffer + 18 + 0, data + HEADER + begin, a);
    348 					memcpy (buffer + 18 + a, data + HEADER,         b);
    349 				} else {
    350 					memcpy (buffer + 18, data + HEADER + begin, len);
    351 				}
    352 			}
    353 
    354 			// Since the size of the profile ringbuffer is limited,
    355 			// not all logbook entries will have profile data. Thus,
    356 			// once the end of the profile ringbuffer is reached,
    357 			// there is no need to keep scanning the ringbuffer.
    358 			if (current == eop)
    359 				profiles = 0;
    360 		}
    361 
    362 		// Automatically abort when a dive is older than the provided timestamp.
    363 		unsigned int timestamp = array_uint32_le (buffer + 11);
    364 		if (device && timestamp <= device->timestamp)
    365 			return DC_STATUS_SUCCESS;
    366 
    367 		if (callback && !callback (buffer, len + 18, buffer + 11, 4, userdata))
    368 			return DC_STATUS_SUCCESS;
    369 	}
    370 
    371 	return DC_STATUS_SUCCESS;
    372 }