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

diverite_nitekq.c (12917B)


      1 /*
      2  * libdivecomputer
      3  *
      4  * Copyright (C) 2013 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 "diverite_nitekq.h"
     27 #include "context-private.h"
     28 #include "device-private.h"
     29 #include "checksum.h"
     30 #include "array.h"
     31 
     32 #define ISINSTANCE(device) dc_device_isinstance((device), &diverite_nitekq_device_vtable)
     33 
     34 #define KEEPALIVE  0x3E // '<'
     35 #define BLOCK      0x42 // 'B'
     36 #define DISCONNECT 0x44 // 'D'
     37 #define HANDSHAKE  0x48 // 'H'
     38 #define RESET      0x52 // 'R'
     39 #define UPLOAD     0x55 // 'U'
     40 
     41 #define SZ_PACKET 256
     42 #define SZ_MEMORY (128 * SZ_PACKET)
     43 #define SZ_LOGBOOK 6
     44 
     45 #define LOGBOOK          0x0320
     46 #define ADDRESS          0x0384
     47 #define EOP              0x03E6
     48 #define RB_PROFILE_BEGIN 0x03E8
     49 #define RB_PROFILE_END   SZ_MEMORY
     50 
     51 typedef struct diverite_nitekq_device_t {
     52 	dc_device_t base;
     53 	dc_iostream_t *iostream;
     54 	unsigned char version[32];
     55 	unsigned char fingerprint[SZ_LOGBOOK];
     56 } diverite_nitekq_device_t;
     57 
     58 static dc_status_t diverite_nitekq_device_set_fingerprint (dc_device_t *device, const unsigned char data[], unsigned int size);
     59 static dc_status_t diverite_nitekq_device_dump (dc_device_t *abstract, dc_buffer_t *buffer);
     60 static dc_status_t diverite_nitekq_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata);
     61 static dc_status_t diverite_nitekq_device_close (dc_device_t *abstract);
     62 
     63 static const dc_device_vtable_t diverite_nitekq_device_vtable = {
     64 	sizeof(diverite_nitekq_device_t),
     65 	DC_FAMILY_DIVERITE_NITEKQ,
     66 	diverite_nitekq_device_set_fingerprint, /* set_fingerprint */
     67 	NULL, /* read */
     68 	NULL, /* write */
     69 	diverite_nitekq_device_dump, /* dump */
     70 	diverite_nitekq_device_foreach, /* foreach */
     71 	NULL, /* timesync */
     72 	diverite_nitekq_device_close /* close */
     73 };
     74 
     75 static dc_status_t
     76 diverite_nitekq_extract_dives (dc_device_t *device, const unsigned char data[], unsigned int size, dc_dive_callback_t callback, void *userdata);
     77 
     78 static dc_status_t
     79 diverite_nitekq_send (diverite_nitekq_device_t *device, unsigned char cmd)
     80 {
     81 	dc_status_t status = DC_STATUS_SUCCESS;
     82 	dc_device_t *abstract = (dc_device_t *) device;
     83 
     84 	if (device_is_cancelled (abstract))
     85 		return DC_STATUS_CANCELLED;
     86 
     87 	// Send the command.
     88 	unsigned char command[] = {cmd};
     89 	status = dc_iostream_write (device->iostream, command, sizeof (command), NULL);
     90 	if (status != DC_STATUS_SUCCESS) {
     91 		ERROR (abstract->context, "Failed to send the command.");
     92 		return status;
     93 	}
     94 
     95 	return DC_STATUS_SUCCESS;
     96 }
     97 
     98 
     99 static dc_status_t
    100 diverite_nitekq_receive (diverite_nitekq_device_t *device, unsigned char data[], unsigned int size)
    101 {
    102 	dc_status_t status = DC_STATUS_SUCCESS;
    103 	dc_device_t *abstract = (dc_device_t *) device;
    104 
    105 	// Read the answer.
    106 	status = dc_iostream_read (device->iostream, data, size, NULL);
    107 	if (status != DC_STATUS_SUCCESS) {
    108 		ERROR (abstract->context, "Failed to receive the answer.");
    109 		return status;
    110 	}
    111 
    112 	// Read the checksum.
    113 	unsigned char checksum[2] = {0};
    114 	status = dc_iostream_read (device->iostream, checksum, sizeof (checksum), NULL);
    115 	if (status != DC_STATUS_SUCCESS) {
    116 		ERROR (abstract->context, "Failed to receive the checksum.");
    117 		return status;
    118 	}
    119 
    120 	return DC_STATUS_SUCCESS;
    121 }
    122 
    123 
    124 static dc_status_t
    125 diverite_nitekq_handshake (diverite_nitekq_device_t *device)
    126 {
    127 	dc_status_t status = DC_STATUS_SUCCESS;
    128 	dc_device_t *abstract = (dc_device_t *) device;
    129 
    130 	// Send the command.
    131 	unsigned char command[] = {HANDSHAKE};
    132 	status = dc_iostream_write (device->iostream, command, sizeof (command), NULL);
    133 	if (status != DC_STATUS_SUCCESS) {
    134 		ERROR (abstract->context, "Failed to send the command.");
    135 		return status;
    136 	}
    137 
    138 	// Read the answer.
    139 	status = dc_iostream_read (device->iostream, device->version, sizeof (device->version), NULL);
    140 	if (status != DC_STATUS_SUCCESS) {
    141 		ERROR (abstract->context, "Failed to receive the answer.");
    142 		return status;
    143 	}
    144 
    145 	return DC_STATUS_SUCCESS;
    146 }
    147 
    148 
    149 dc_status_t
    150 diverite_nitekq_device_open (dc_device_t **out, dc_context_t *context, dc_iostream_t *iostream)
    151 {
    152 	dc_status_t status = DC_STATUS_SUCCESS;
    153 	diverite_nitekq_device_t *device = NULL;
    154 
    155 	if (out == NULL)
    156 		return DC_STATUS_INVALIDARGS;
    157 
    158 	// Allocate memory.
    159 	device = (diverite_nitekq_device_t *) dc_device_allocate (context, &diverite_nitekq_device_vtable);
    160 	if (device == NULL) {
    161 		ERROR (context, "Failed to allocate memory.");
    162 		return DC_STATUS_NOMEMORY;
    163 	}
    164 
    165 	// Set the default values.
    166 	device->iostream = iostream;
    167 	memset (device->fingerprint, 0, sizeof (device->fingerprint));
    168 
    169 	// Set the serial communication protocol (9600 8N1).
    170 	status = dc_iostream_configure (device->iostream, 9600, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
    171 	if (status != DC_STATUS_SUCCESS) {
    172 		ERROR (context, "Failed to set the terminal attributes.");
    173 		goto error_free;
    174 	}
    175 
    176 	// Set the timeout for receiving data (1000ms).
    177 	status = dc_iostream_set_timeout (device->iostream, 1000);
    178 	if (status != DC_STATUS_SUCCESS) {
    179 		ERROR (context, "Failed to set the timeout.");
    180 		goto error_free;
    181 	}
    182 
    183 	// Make sure everything is in a sane state.
    184 	dc_iostream_sleep (device->iostream, 100);
    185 	dc_iostream_purge (device->iostream, DC_DIRECTION_ALL);
    186 
    187 	// Perform the handshaking.
    188 	status = diverite_nitekq_handshake (device);
    189 	if (status != DC_STATUS_SUCCESS) {
    190 		ERROR (context, "Failed to handshake.");
    191 		goto error_free;
    192 	}
    193 
    194 	HEXDUMP (context, DC_LOGLEVEL_DEBUG, "Version", device->version, sizeof (device->version));
    195 
    196 	*out = (dc_device_t*) device;
    197 
    198 	return DC_STATUS_SUCCESS;
    199 
    200 error_free:
    201 	dc_device_deallocate ((dc_device_t *) device);
    202 	return status;
    203 }
    204 
    205 
    206 static dc_status_t
    207 diverite_nitekq_device_close (dc_device_t *abstract)
    208 {
    209 	dc_status_t status = DC_STATUS_SUCCESS;
    210 	diverite_nitekq_device_t *device = (diverite_nitekq_device_t*) abstract;
    211 	dc_status_t rc = DC_STATUS_SUCCESS;
    212 
    213 	// Disconnect.
    214 	rc = diverite_nitekq_send (device, DISCONNECT);
    215 	if (rc != DC_STATUS_SUCCESS) {
    216 		dc_status_set_error(&status, rc);
    217 	}
    218 
    219 	return status;
    220 }
    221 
    222 
    223 static dc_status_t
    224 diverite_nitekq_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size)
    225 {
    226 	diverite_nitekq_device_t *device = (diverite_nitekq_device_t*) abstract;
    227 
    228 	if (size && size != sizeof (device->fingerprint))
    229 		return DC_STATUS_INVALIDARGS;
    230 
    231 	if (size)
    232 		memcpy (device->fingerprint, data, sizeof (device->fingerprint));
    233 	else
    234 		memset (device->fingerprint, 0, sizeof (device->fingerprint));
    235 
    236 	return DC_STATUS_SUCCESS;
    237 }
    238 
    239 
    240 static dc_status_t
    241 diverite_nitekq_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
    242 {
    243 	diverite_nitekq_device_t *device = (diverite_nitekq_device_t*) abstract;
    244 	dc_status_t rc = DC_STATUS_SUCCESS;
    245 	unsigned char packet[256] = {0};
    246 
    247 	// Pre-allocate the required amount of memory.
    248 	if (!dc_buffer_reserve (buffer, SZ_PACKET + SZ_MEMORY)) {
    249 		ERROR (abstract->context, "Insufficient buffer space available.");
    250 		return DC_STATUS_NOMEMORY;
    251 	}
    252 
    253 	// Enable progress notifications.
    254 	dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER;
    255 	progress.maximum = SZ_PACKET + SZ_MEMORY;
    256 	device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);
    257 
    258 	// Emit a vendor event.
    259 	dc_event_vendor_t vendor;
    260 	vendor.data = device->version;
    261 	vendor.size = sizeof (device->version);
    262 	device_event_emit (abstract, DC_EVENT_VENDOR, &vendor);
    263 
    264 	// Emit a device info event.
    265 	dc_event_devinfo_t devinfo;
    266 	devinfo.model = 0;
    267 	devinfo.firmware = 0;
    268 	devinfo.serial = array_uint32_be (device->version + 0x0A);
    269 	device_event_emit (abstract, DC_EVENT_DEVINFO, &devinfo);
    270 
    271 	// Send the upload request. It's not clear whether this request is
    272 	// actually needed, but let's send it anyway.
    273 	rc = diverite_nitekq_send (device, UPLOAD);
    274 	if (rc != DC_STATUS_SUCCESS) {
    275 		return rc;
    276 	}
    277 
    278 	// Receive the response packet. It's currently not used (or needed)
    279 	// for anything, but we prepend it to the main data anyway, in case
    280 	// we ever need it in the future.
    281 	rc = diverite_nitekq_receive (device, packet, sizeof (packet));
    282 	if (rc != DC_STATUS_SUCCESS) {
    283 		return rc;
    284 	}
    285 
    286 	dc_buffer_append (buffer, packet, sizeof (packet));
    287 
    288 	// Update and emit a progress event.
    289 	progress.current += SZ_PACKET;
    290 	device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);
    291 
    292 	// Send the request to initiate downloading memory blocks.
    293 	rc = diverite_nitekq_send (device, RESET);
    294 	if (rc != DC_STATUS_SUCCESS) {
    295 		return rc;
    296 	}
    297 
    298 	for (unsigned int i = 0; i < 128; ++i) {
    299 		// Request the next memory block.
    300 		rc = diverite_nitekq_send (device, BLOCK);
    301 		if (rc != DC_STATUS_SUCCESS) {
    302 			return rc;
    303 		}
    304 
    305 		// Receive the memory block.
    306 		rc = diverite_nitekq_receive (device, packet, sizeof (packet));
    307 		if (rc != DC_STATUS_SUCCESS) {
    308 			return rc;
    309 		}
    310 
    311 		dc_buffer_append (buffer, packet, sizeof (packet));
    312 
    313 		// Update and emit a progress event.
    314 		progress.current += SZ_PACKET;
    315 		device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);
    316 	}
    317 
    318 	return DC_STATUS_SUCCESS;
    319 }
    320 
    321 
    322 static dc_status_t
    323 diverite_nitekq_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata)
    324 {
    325 	dc_buffer_t *buffer = dc_buffer_new (0);
    326 	if (buffer == NULL)
    327 		return DC_STATUS_NOMEMORY;
    328 
    329 	dc_status_t rc = diverite_nitekq_device_dump (abstract, buffer);
    330 	if (rc != DC_STATUS_SUCCESS) {
    331 		dc_buffer_free (buffer);
    332 		return rc;
    333 	}
    334 
    335 	rc = diverite_nitekq_extract_dives (abstract,
    336 		dc_buffer_get_data (buffer), dc_buffer_get_size (buffer), callback, userdata);
    337 
    338 	dc_buffer_free (buffer);
    339 
    340 	return rc;
    341 }
    342 
    343 
    344 static dc_status_t
    345 diverite_nitekq_extract_dives (dc_device_t *abstract, const unsigned char data[], unsigned int size, dc_dive_callback_t callback, void *userdata)
    346 {
    347 	diverite_nitekq_device_t *device = (diverite_nitekq_device_t *) abstract;
    348 	dc_context_t *context = (abstract ? abstract->context : NULL);
    349 
    350 	if (abstract && !ISINSTANCE (abstract))
    351 		return DC_STATUS_INVALIDARGS;
    352 
    353 	if (size < SZ_PACKET + SZ_MEMORY)
    354 		return DC_STATUS_DATAFORMAT;
    355 
    356 	// Skip the first packet. We don't need it for anything. It also
    357 	// makes the logic easier because all offsets in the data are
    358 	// relative to the real start of the memory (e.g. excluding this
    359 	// artificial first block).
    360 	data += SZ_PACKET;
    361 
    362 	// Allocate memory.
    363 	unsigned char *buffer = (unsigned char *) malloc (SZ_LOGBOOK + RB_PROFILE_END - RB_PROFILE_BEGIN);
    364 	if (buffer == NULL) {
    365 		ERROR (context, "Failed to allocate memory.");
    366 		return DC_STATUS_NOMEMORY;
    367 	}
    368 
    369 	// Get the end of profile pointer.
    370 	unsigned int eop = array_uint16_be(data + EOP);
    371 	if (eop < RB_PROFILE_BEGIN || eop >= RB_PROFILE_END) {
    372 		ERROR (context, "Invalid ringbuffer pointer detected (0x%04x).", eop);
    373 		free (buffer);
    374 		return DC_STATUS_DATAFORMAT;
    375 	}
    376 
    377 	// When a new dive is added, the device moves all existing logbook
    378 	// and address entries towards the end, such that the most recent
    379 	// one is always the first one. This is not the case for the profile
    380 	// data, which is added at the end.
    381 	unsigned int previous = eop;
    382 	for (unsigned int i = 0; i < 10; ++i) {
    383 		// Get the pointer to the logbook entry.
    384 		const unsigned char *p = data + LOGBOOK + i * SZ_LOGBOOK;
    385 
    386 		// Abort if an empty logbook is found.
    387 		if (array_isequal (p, SZ_LOGBOOK, 0x00))
    388 			break;
    389 
    390 		// Get the address of the profile data.
    391 		unsigned int address = array_uint16_be(data + ADDRESS + i * 2);
    392 		if (address < RB_PROFILE_BEGIN || address >= RB_PROFILE_END) {
    393 			ERROR (context, "Invalid ringbuffer pointer detected (0x%04x).", address);
    394 			free (buffer);
    395 			return DC_STATUS_DATAFORMAT;
    396 		}
    397 
    398 		// Check the fingerprint data.
    399 		if (device && memcmp (p, device->fingerprint, sizeof (device->fingerprint)) == 0)
    400 			break;
    401 
    402 		// Copy the logbook entry.
    403 		memcpy (buffer, p, SZ_LOGBOOK);
    404 
    405 		// Copy the profile data.
    406 		unsigned int length = 0;
    407 		if (previous > address) {
    408 			length = previous - address;
    409 			memcpy (buffer + SZ_LOGBOOK, data + address, length);
    410 		} else {
    411 			unsigned int len_a = RB_PROFILE_END - address;
    412 			unsigned int len_b = previous - RB_PROFILE_BEGIN;
    413 			length = len_a + len_b;
    414 			memcpy (buffer + SZ_LOGBOOK, data + address, len_a);
    415 			memcpy (buffer + SZ_LOGBOOK + len_a, data + RB_PROFILE_BEGIN, len_b);
    416 		}
    417 
    418 		if (callback && !callback (buffer, length + SZ_LOGBOOK, buffer, SZ_LOGBOOK, userdata)) {
    419 			break;
    420 		}
    421 
    422 		previous = address;
    423 	}
    424 
    425 	free (buffer);
    426 
    427 	return DC_STATUS_SUCCESS;
    428 }