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

citizen_aqualand.c (6664B)


      1 /*
      2  * libdivecomputer
      3  *
      4  * Copyright (C) 2014 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 "citizen_aqualand.h"
     26 #include "context-private.h"
     27 #include "device-private.h"
     28 #include "checksum.h"
     29 #include "ringbuffer.h"
     30 #include "array.h"
     31 
     32 #define ISINSTANCE(device) dc_device_isinstance((device), &citizen_aqualand_device_vtable)
     33 
     34 #define SZ_HEADER 32
     35 
     36 typedef struct citizen_aqualand_device_t {
     37 	dc_device_t base;
     38 	dc_iostream_t *iostream;
     39 	unsigned char fingerprint[8];
     40 } citizen_aqualand_device_t;
     41 
     42 static dc_status_t citizen_aqualand_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size);
     43 static dc_status_t citizen_aqualand_device_dump (dc_device_t *abstract, dc_buffer_t *buffer);
     44 static dc_status_t citizen_aqualand_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata);
     45 
     46 static const dc_device_vtable_t citizen_aqualand_device_vtable = {
     47 	sizeof(citizen_aqualand_device_t),
     48 	DC_FAMILY_CITIZEN_AQUALAND,
     49 	citizen_aqualand_device_set_fingerprint, /* set_fingerprint */
     50 	NULL, /* read */
     51 	NULL, /* write */
     52 	citizen_aqualand_device_dump, /* dump */
     53 	citizen_aqualand_device_foreach, /* foreach */
     54 	NULL, /* timesync */
     55 	NULL /* close */
     56 };
     57 
     58 
     59 dc_status_t
     60 citizen_aqualand_device_open (dc_device_t **out, dc_context_t *context, dc_iostream_t *iostream)
     61 {
     62 	dc_status_t status = DC_STATUS_SUCCESS;
     63 	citizen_aqualand_device_t *device = NULL;
     64 
     65 	if (out == NULL)
     66 		return DC_STATUS_INVALIDARGS;
     67 
     68 	// Allocate memory.
     69 	device = (citizen_aqualand_device_t *) dc_device_allocate (context, &citizen_aqualand_device_vtable);
     70 	if (device == NULL) {
     71 		ERROR (context, "Failed to allocate memory.");
     72 		return DC_STATUS_NOMEMORY;
     73 	}
     74 
     75 	// Set the default values.
     76 	device->iostream = iostream;
     77 	memset (device->fingerprint, 0, sizeof (device->fingerprint));
     78 
     79 	// Set the serial communication protocol (4800 8N1).
     80 	status = dc_iostream_configure (device->iostream, 4800, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
     81 	if (status != DC_STATUS_SUCCESS) {
     82 		ERROR (context, "Failed to set the terminal attributes.");
     83 		goto error_free;
     84 	}
     85 
     86 	// Set the timeout for receiving data (1000ms).
     87 	status = dc_iostream_set_timeout (device->iostream, 1000);
     88 	if (status != DC_STATUS_SUCCESS) {
     89 		ERROR (context, "Failed to set the timeout.");
     90 		goto error_free;
     91 	}
     92 
     93 	// Make sure everything is in a sane state.
     94 	dc_iostream_sleep (device->iostream, 300);
     95 	dc_iostream_purge (device->iostream, DC_DIRECTION_ALL);
     96 
     97 	*out = (dc_device_t *) device;
     98 
     99 	return DC_STATUS_SUCCESS;
    100 
    101 error_free:
    102 	dc_device_deallocate ((dc_device_t *) device);
    103 	return status;
    104 }
    105 
    106 
    107 static dc_status_t
    108 citizen_aqualand_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size)
    109 {
    110 	citizen_aqualand_device_t *device = (citizen_aqualand_device_t *) abstract;
    111 
    112 	if (size && size != sizeof (device->fingerprint))
    113 		return DC_STATUS_INVALIDARGS;
    114 
    115 	if (size)
    116 		memcpy (device->fingerprint, data, sizeof (device->fingerprint));
    117 	else
    118 		memset (device->fingerprint, 0, sizeof (device->fingerprint));
    119 
    120 	return DC_STATUS_SUCCESS;
    121 }
    122 
    123 static dc_status_t
    124 citizen_aqualand_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
    125 {
    126 	dc_status_t status = DC_STATUS_SUCCESS;
    127 	citizen_aqualand_device_t *device = (citizen_aqualand_device_t *) abstract;
    128 
    129 	status = dc_iostream_set_dtr (device->iostream, 1);
    130 	if (status != DC_STATUS_SUCCESS) {
    131 		ERROR (abstract->context, "Failed to set the DTR line.");
    132 		return status;
    133 	}
    134 
    135 	// Send the init byte.
    136 	const unsigned char init[] = {0x7F};
    137 	status = dc_iostream_write (device->iostream, init, sizeof (init), NULL);
    138 	if (status != DC_STATUS_SUCCESS) {
    139 		ERROR (abstract->context, "Failed to send the command.");
    140 		return status;
    141 	}
    142 
    143 	dc_iostream_sleep(device->iostream, 1200);
    144 
    145 	// Send the command.
    146 	const unsigned char command[] = {0xFF};
    147 	status = dc_iostream_write (device->iostream, command, sizeof (command), NULL);
    148 	if (status != DC_STATUS_SUCCESS) {
    149 		ERROR (abstract->context, "Failed to send the command.");
    150 		return status;
    151 	}
    152 
    153 	while (1) {
    154 		// Receive the response packet.
    155 		unsigned char answer[32] = {0};
    156 		status = dc_iostream_read (device->iostream, answer, sizeof (answer), NULL);
    157 		if (status != DC_STATUS_SUCCESS) {
    158 			ERROR (abstract->context, "Failed to receive the answer.");
    159 			return status;
    160 		}
    161 
    162 		if (!dc_buffer_append(buffer, answer, sizeof (answer))) {
    163 			ERROR (abstract->context, "Insufficient buffer space available.");
    164 			return status;
    165 		}
    166 
    167 		// Send the command.
    168 		status = dc_iostream_write (device->iostream, command, sizeof (command), NULL);
    169 		if (status != DC_STATUS_SUCCESS) {
    170 			ERROR (abstract->context, "Failed to send the command.");
    171 			return status;
    172 		}
    173 
    174 		if (answer[sizeof(answer) - 1] == 0xFF)
    175 			break;
    176 	}
    177 
    178 	status = dc_iostream_set_dtr (device->iostream, 0);
    179 	if (status != DC_STATUS_SUCCESS) {
    180 		ERROR (abstract->context, "Failed to clear the DTR line.");
    181 		return status;
    182 	}
    183 
    184 	return DC_STATUS_SUCCESS;
    185 }
    186 
    187 static dc_status_t
    188 citizen_aqualand_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata)
    189 {
    190 	citizen_aqualand_device_t *device = (citizen_aqualand_device_t *) abstract;
    191 
    192 	dc_buffer_t *buffer = dc_buffer_new (0);
    193 	if (buffer == NULL)
    194 		return DC_STATUS_NOMEMORY;
    195 
    196 	dc_status_t rc = citizen_aqualand_device_dump (abstract, buffer);
    197 	if (rc != DC_STATUS_SUCCESS) {
    198 		dc_buffer_free (buffer);
    199 		return rc;
    200 	}
    201 
    202 	unsigned char *data = dc_buffer_get_data (buffer);
    203 	unsigned int   size = dc_buffer_get_size (buffer);
    204 
    205 	if (size < SZ_HEADER) {
    206 		ERROR (abstract->context, "Dive header is too small (%u).", size);
    207 		dc_buffer_free (buffer);
    208 		return DC_STATUS_DATAFORMAT;
    209 	}
    210 
    211 	if (callback && memcmp (data + 0x05, device->fingerprint, sizeof (device->fingerprint)) != 0) {
    212 		callback (data, size, data + 0x05, sizeof (device->fingerprint), userdata);
    213 	}
    214 
    215 	dc_buffer_free (buffer);
    216 
    217 	return rc;
    218 }