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_nemo.c (8950B)


      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 <string.h> // memcpy, memcmp
     23 #include <stdlib.h> // malloc, free
     24 
     25 #include "mares_nemo.h"
     26 #include "mares_common.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), &mares_nemo_device_vtable)
     33 
     34 #ifdef PACKETSIZE
     35 #undef PACKETSIZE /* Override the common value. */
     36 #endif
     37 
     38 #define MEMORYSIZE 0x4000
     39 #define PACKETSIZE 0x20
     40 
     41 #define NEMO        0
     42 #define NEMOEXCEL   17
     43 #define NEMOAPNEIST 18
     44 
     45 typedef struct mares_nemo_device_t {
     46 	dc_device_t base;
     47 	dc_iostream_t *iostream;
     48 	unsigned char fingerprint[5];
     49 } mares_nemo_device_t;
     50 
     51 static dc_status_t mares_nemo_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size);
     52 static dc_status_t mares_nemo_device_dump (dc_device_t *abstract, dc_buffer_t *buffer);
     53 static dc_status_t mares_nemo_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata);
     54 
     55 static const dc_device_vtable_t mares_nemo_device_vtable = {
     56 	sizeof(mares_nemo_device_t),
     57 	DC_FAMILY_MARES_NEMO,
     58 	mares_nemo_device_set_fingerprint, /* set_fingerprint */
     59 	NULL, /* read */
     60 	NULL, /* write */
     61 	mares_nemo_device_dump, /* dump */
     62 	mares_nemo_device_foreach, /* foreach */
     63 	NULL, /* timesync */
     64 	NULL /* close */
     65 };
     66 
     67 static const mares_common_layout_t mares_nemo_layout = {
     68 	MEMORYSIZE, /* memsize */
     69 	0x0070, /* rb_profile_begin */
     70 	0x3400, /* rb_profile_end */
     71 	0x3400, /* rb_freedives_begin */
     72 	0x4000  /* rb_freedives_end */
     73 };
     74 
     75 static const mares_common_layout_t mares_nemo_apneist_layout = {
     76 	MEMORYSIZE, /* memsize */
     77 	0x0070, /* rb_profile_begin */
     78 	0x0800, /* rb_profile_end */
     79 	0x0800, /* rb_freedives_begin */
     80 	0x4000  /* rb_freedives_end */
     81 };
     82 
     83 
     84 dc_status_t
     85 mares_nemo_device_open (dc_device_t **out, dc_context_t *context, dc_iostream_t *iostream)
     86 {
     87 	dc_status_t status = DC_STATUS_SUCCESS;
     88 	mares_nemo_device_t *device = NULL;
     89 
     90 	if (out == NULL)
     91 		return DC_STATUS_INVALIDARGS;
     92 
     93 	// Allocate memory.
     94 	device = (mares_nemo_device_t *) dc_device_allocate (context, &mares_nemo_device_vtable);
     95 	if (device == NULL) {
     96 		ERROR (context, "Failed to allocate memory.");
     97 		return DC_STATUS_NOMEMORY;
     98 	}
     99 
    100 	// Set the default values.
    101 	device->iostream = iostream;
    102 	memset (device->fingerprint, 0, sizeof (device->fingerprint));
    103 
    104 	// Set the serial communication protocol (9600 8N1).
    105 	status = dc_iostream_configure (device->iostream, 9600, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
    106 	if (status != DC_STATUS_SUCCESS) {
    107 		ERROR (context, "Failed to set the terminal attributes.");
    108 		goto error_free;
    109 	}
    110 
    111 	// Set the timeout for receiving data (1000 ms).
    112 	status = dc_iostream_set_timeout (device->iostream, 1000);
    113 	if (status != DC_STATUS_SUCCESS) {
    114 		ERROR (context, "Failed to set the timeout.");
    115 		goto error_free;
    116 	}
    117 
    118 	// Set the DTR line.
    119 	status = dc_iostream_set_dtr (device->iostream, 1);
    120 	if (status != DC_STATUS_SUCCESS) {
    121 		ERROR (context, "Failed to set the DTR line.");
    122 		goto error_free;
    123 	}
    124 
    125 	// Set the RTS line.
    126 	status = dc_iostream_set_rts (device->iostream, 1);
    127 	if (status != DC_STATUS_SUCCESS) {
    128 		ERROR (context, "Failed to set the RTS line.");
    129 		goto error_free;
    130 	}
    131 
    132 	// Make sure everything is in a sane state.
    133 	dc_iostream_purge (device->iostream, DC_DIRECTION_ALL);
    134 
    135 	*out = (dc_device_t*) device;
    136 
    137 	return DC_STATUS_SUCCESS;
    138 
    139 error_free:
    140 	dc_device_deallocate ((dc_device_t *) device);
    141 	return status;
    142 }
    143 
    144 
    145 static dc_status_t
    146 mares_nemo_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size)
    147 {
    148 	mares_nemo_device_t *device = (mares_nemo_device_t *) abstract;
    149 
    150 	if (size && size != sizeof (device->fingerprint))
    151 		return DC_STATUS_INVALIDARGS;
    152 
    153 	if (size)
    154 		memcpy (device->fingerprint, data, sizeof (device->fingerprint));
    155 	else
    156 		memset (device->fingerprint, 0, sizeof (device->fingerprint));
    157 
    158 	return DC_STATUS_SUCCESS;
    159 }
    160 
    161 
    162 static dc_status_t
    163 mares_nemo_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
    164 {
    165 	dc_status_t status = DC_STATUS_SUCCESS;
    166 	mares_nemo_device_t *device = (mares_nemo_device_t *) abstract;
    167 
    168 	// Pre-allocate the required amount of memory.
    169 	if (!dc_buffer_reserve (buffer, MEMORYSIZE)) {
    170 		ERROR (abstract->context, "Insufficient buffer space available.");
    171 		return DC_STATUS_NOMEMORY;
    172 	}
    173 
    174 	// Enable progress notifications.
    175 	dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER;
    176 	progress.maximum = MEMORYSIZE + 20;
    177 	device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);
    178 
    179 	// Wait until some data arrives.
    180 	while (dc_iostream_poll (device->iostream, 100) == DC_STATUS_TIMEOUT) {
    181 		if (device_is_cancelled (abstract))
    182 			return DC_STATUS_CANCELLED;
    183 
    184 		device_event_emit (abstract, DC_EVENT_WAITING, NULL);
    185 	}
    186 
    187 	// Receive the header of the package.
    188 	unsigned char header = 0x00;
    189 	for (unsigned int i = 0; i < 20;) {
    190 		status = dc_iostream_read (device->iostream, &header, 1, NULL);
    191 		if (status != DC_STATUS_SUCCESS) {
    192 			ERROR (abstract->context, "Failed to receive the header.");
    193 			return status;
    194 		}
    195 		if (header == 0xEE) {
    196 			i++; // Continue.
    197 		} else {
    198 			i = 0; // Reset.
    199 		}
    200 	}
    201 
    202 	// Update and emit a progress event.
    203 	progress.current += 20;
    204 	device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);
    205 
    206 	unsigned int nbytes = 0;
    207 	while (nbytes < MEMORYSIZE) {
    208 		// Read the packet.
    209 		unsigned char packet[(PACKETSIZE + 1) * 2] = {0};
    210 		status = dc_iostream_read (device->iostream, packet, sizeof (packet), NULL);
    211 		if (status != DC_STATUS_SUCCESS) {
    212 			ERROR (abstract->context, "Failed to receive the answer.");
    213 			return status;
    214 		}
    215 
    216 		// Verify the checksums of the packet.
    217 		unsigned char crc1 = packet[PACKETSIZE];
    218 		unsigned char crc2 = packet[PACKETSIZE * 2 + 1];
    219 		unsigned char ccrc1 = checksum_add_uint8 (packet, PACKETSIZE, 0x00);
    220 		unsigned char ccrc2 = checksum_add_uint8 (packet + PACKETSIZE + 1, PACKETSIZE, 0x00);
    221 		if (crc1 == ccrc1 && crc2 == ccrc2) {
    222 			// Both packets have a correct checksum.
    223 			if (memcmp (packet, packet + PACKETSIZE + 1, PACKETSIZE) != 0) {
    224 				ERROR (abstract->context, "Both packets are not equal.");
    225 				return DC_STATUS_PROTOCOL;
    226 			}
    227 			dc_buffer_append (buffer, packet, PACKETSIZE);
    228 		} else if (crc1 == ccrc1) {
    229 			// Only the first packet has a correct checksum.
    230 			WARNING (abstract->context, "Only the first packet has a correct checksum.");
    231 			dc_buffer_append (buffer, packet, PACKETSIZE);
    232 		} else if (crc2 == ccrc2) {
    233 			// Only the second packet has a correct checksum.
    234 			WARNING (abstract->context, "Only the second packet has a correct checksum.");
    235 			dc_buffer_append (buffer, packet + PACKETSIZE + 1, PACKETSIZE);
    236 		} else {
    237 			ERROR (abstract->context, "Unexpected answer checksum.");
    238 			return DC_STATUS_PROTOCOL;
    239 		}
    240 
    241 		// Update and emit a progress event.
    242 		progress.current += PACKETSIZE;
    243 		device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);
    244 
    245 		nbytes += PACKETSIZE;
    246 	}
    247 
    248 	// Emit a device info event.
    249 	unsigned char *data = dc_buffer_get_data (buffer);
    250 	dc_event_devinfo_t devinfo;
    251 	devinfo.model = data[1];
    252 	devinfo.firmware = 0;
    253 	devinfo.serial = array_uint16_be (data + 8);
    254 	device_event_emit (abstract, DC_EVENT_DEVINFO, &devinfo);
    255 
    256 	return DC_STATUS_SUCCESS;
    257 }
    258 
    259 
    260 static dc_status_t
    261 mares_nemo_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata)
    262 {
    263 	mares_nemo_device_t *device = (mares_nemo_device_t *) abstract;
    264 
    265 	dc_buffer_t *buffer = dc_buffer_new (MEMORYSIZE);
    266 	if (buffer == NULL)
    267 		return DC_STATUS_NOMEMORY;
    268 
    269 	dc_status_t rc = mares_nemo_device_dump (abstract, buffer);
    270 	if (rc != DC_STATUS_SUCCESS) {
    271 		dc_buffer_free (buffer);
    272 		return rc;
    273 	}
    274 
    275 	unsigned char *data = dc_buffer_get_data (buffer);
    276 
    277 	const mares_common_layout_t *layout = NULL;
    278 	switch (data[1]) {
    279 	case NEMO:
    280 	case NEMOEXCEL:
    281 		layout = &mares_nemo_layout;
    282 		break;
    283 	case NEMOAPNEIST:
    284 		layout = &mares_nemo_apneist_layout;
    285 		break;
    286 	default: // Unknown, try nemo
    287 		WARNING (abstract->context, "Unsupported model %02x detected!", data[1]);
    288 		layout = &mares_nemo_layout;
    289 		break;
    290 	}
    291 
    292 	rc = mares_common_extract_dives (abstract->context, layout, device->fingerprint, data, callback, userdata);
    293 
    294 	dc_buffer_free (buffer);
    295 
    296 	return rc;
    297 }