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_puck.c (7134B)


      1 /*
      2  * libdivecomputer
      3  *
      4  * Copyright (C) 2009 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 #include <assert.h> // assert
     25 
     26 #include "mares_puck.h"
     27 #include "mares_common.h"
     28 #include "context-private.h"
     29 #include "device-private.h"
     30 #include "checksum.h"
     31 #include "array.h"
     32 
     33 #define ISINSTANCE(device) dc_device_isinstance((device), &mares_puck_device_vtable)
     34 
     35 #define NEMOWIDE    1
     36 #define NEMOAIR     4
     37 #define PUCK        7
     38 #define PUCKAIR     19
     39 
     40 typedef struct mares_puck_device_t {
     41 	mares_common_device_t base;
     42 	const mares_common_layout_t *layout;
     43 	unsigned char fingerprint[5];
     44 } mares_puck_device_t;
     45 
     46 static dc_status_t mares_puck_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size);
     47 static dc_status_t mares_puck_device_dump (dc_device_t *abstract, dc_buffer_t *buffer);
     48 static dc_status_t mares_puck_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata);
     49 
     50 static const dc_device_vtable_t mares_puck_device_vtable = {
     51 	sizeof(mares_puck_device_t),
     52 	DC_FAMILY_MARES_PUCK,
     53 	mares_puck_device_set_fingerprint, /* set_fingerprint */
     54 	mares_common_device_read, /* read */
     55 	NULL, /* write */
     56 	mares_puck_device_dump, /* dump */
     57 	mares_puck_device_foreach, /* foreach */
     58 	NULL, /* timesync */
     59 	NULL /* close */
     60 };
     61 
     62 static const mares_common_layout_t mares_puck_layout = {
     63 	0x4000, /* memsize */
     64 	0x0070, /* rb_profile_begin */
     65 	0x4000, /* rb_profile_end */
     66 	0x4000, /* rb_freedives_begin */
     67 	0x4000  /* rb_freedives_end */
     68 };
     69 
     70 static const mares_common_layout_t mares_nemoair_layout = {
     71 	0x8000, /* memsize */
     72 	0x0070, /* rb_profile_begin */
     73 	0x8000, /* rb_profile_end */
     74 	0x8000, /* rb_freedives_begin */
     75 	0x8000  /* rb_freedives_end */
     76 };
     77 
     78 static const mares_common_layout_t mares_nemowide_layout = {
     79 	0x4000, /* memsize */
     80 	0x0070, /* rb_profile_begin */
     81 	0x3400, /* rb_profile_end */
     82 	0x3400, /* rb_freedives_begin */
     83 	0x4000  /* rb_freedives_end */
     84 };
     85 
     86 
     87 dc_status_t
     88 mares_puck_device_open (dc_device_t **out, dc_context_t *context, dc_iostream_t *iostream)
     89 {
     90 	dc_status_t status = DC_STATUS_SUCCESS;
     91 	mares_puck_device_t *device = NULL;
     92 
     93 	if (out == NULL)
     94 		return DC_STATUS_INVALIDARGS;
     95 
     96 	// Allocate memory.
     97 	device = (mares_puck_device_t *) dc_device_allocate (context, &mares_puck_device_vtable);
     98 	if (device == NULL) {
     99 		ERROR (context, "Failed to allocate memory.");
    100 		return DC_STATUS_NOMEMORY;
    101 	}
    102 
    103 	// Initialize the base class.
    104 	mares_common_device_init (&device->base, iostream);
    105 
    106 	// Set the default values.
    107 	device->layout = NULL;
    108 	memset (device->fingerprint, 0, sizeof (device->fingerprint));
    109 
    110 	// Set the serial communication protocol (38400 8N1).
    111 	status = dc_iostream_configure (device->base.iostream, 38400, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
    112 	if (status != DC_STATUS_SUCCESS) {
    113 		ERROR (context, "Failed to set the terminal attributes.");
    114 		goto error_free;
    115 	}
    116 
    117 	// Set the timeout for receiving data (1000 ms).
    118 	status = dc_iostream_set_timeout (device->base.iostream, 1000);
    119 	if (status != DC_STATUS_SUCCESS) {
    120 		ERROR (context, "Failed to set the timeout.");
    121 		goto error_free;
    122 	}
    123 
    124 	// Clear the DTR line.
    125 	status = dc_iostream_set_dtr (device->base.iostream, 0);
    126 	if (status != DC_STATUS_SUCCESS) {
    127 		ERROR (context, "Failed to clear the DTR line.");
    128 		goto error_free;
    129 	}
    130 
    131 	// Clear the RTS line.
    132 	status = dc_iostream_set_rts (device->base.iostream, 0);
    133 	if (status != DC_STATUS_SUCCESS) {
    134 		ERROR (context, "Failed to clear the RTS line.");
    135 		goto error_free;
    136 	}
    137 
    138 	// Make sure everything is in a sane state.
    139 	dc_iostream_purge (device->base.iostream, DC_DIRECTION_ALL);
    140 
    141 	// Identify the model number.
    142 	unsigned char header[PACKETSIZE] = {0};
    143 	status = mares_common_device_read ((dc_device_t *) device, 0, header, sizeof (header));
    144 	if (status != DC_STATUS_SUCCESS) {
    145 		goto error_free;
    146 	}
    147 
    148 	// Override the base class values.
    149 	switch (header[1]) {
    150 	case NEMOWIDE:
    151 		device->layout = &mares_nemowide_layout;
    152 		break;
    153 	case NEMOAIR:
    154 	case PUCKAIR:
    155 		device->layout = &mares_nemoair_layout;
    156 		break;
    157 	case PUCK:
    158 		device->layout = &mares_puck_layout;
    159 		break;
    160 	default: // Unknown, try puck
    161 		device->layout = &mares_puck_layout;
    162 		break;
    163 	}
    164 
    165 	*out = (dc_device_t*) device;
    166 
    167 	return DC_STATUS_SUCCESS;
    168 
    169 error_free:
    170 	dc_device_deallocate ((dc_device_t *) device);
    171 	return status;
    172 }
    173 
    174 
    175 static dc_status_t
    176 mares_puck_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size)
    177 {
    178 	mares_puck_device_t *device = (mares_puck_device_t *) abstract;
    179 
    180 	if (size && size != sizeof (device->fingerprint))
    181 		return DC_STATUS_INVALIDARGS;
    182 
    183 	if (size)
    184 		memcpy (device->fingerprint, data, sizeof (device->fingerprint));
    185 	else
    186 		memset (device->fingerprint, 0, sizeof (device->fingerprint));
    187 
    188 	return DC_STATUS_SUCCESS;
    189 }
    190 
    191 
    192 static dc_status_t
    193 mares_puck_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
    194 {
    195 	dc_status_t status = DC_STATUS_SUCCESS;
    196 	mares_puck_device_t *device = (mares_puck_device_t *) abstract;
    197 
    198 	assert (device->layout != NULL);
    199 
    200 	// Allocate the required amount of memory.
    201 	if (!dc_buffer_resize (buffer, device->layout->memsize)) {
    202 		ERROR (abstract->context, "Insufficient buffer space available.");
    203 		return DC_STATUS_NOMEMORY;
    204 	}
    205 
    206 	// Download the memory dump.
    207 	status = device_dump_read (abstract, 0, dc_buffer_get_data (buffer),
    208 		dc_buffer_get_size (buffer), PACKETSIZE);
    209 	if (status != DC_STATUS_SUCCESS) {
    210 		return status;
    211 	}
    212 
    213 	// Emit a device info event.
    214 	unsigned char *data = dc_buffer_get_data (buffer);
    215 	dc_event_devinfo_t devinfo;
    216 	devinfo.model = data[1];
    217 	devinfo.firmware = 0;
    218 	devinfo.serial = array_uint16_be (data + 8);
    219 	device_event_emit (abstract, DC_EVENT_DEVINFO, &devinfo);
    220 
    221 	return status;
    222 }
    223 
    224 
    225 static dc_status_t
    226 mares_puck_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata)
    227 {
    228 	mares_puck_device_t *device = (mares_puck_device_t *) abstract;
    229 
    230 	assert (device->layout != NULL);
    231 
    232 	dc_buffer_t *buffer = dc_buffer_new (device->layout->memsize);
    233 	if (buffer == NULL)
    234 		return DC_STATUS_NOMEMORY;
    235 
    236 	dc_status_t rc = mares_puck_device_dump (abstract, buffer);
    237 	if (rc != DC_STATUS_SUCCESS) {
    238 		dc_buffer_free (buffer);
    239 		return rc;
    240 	}
    241 
    242 	rc = mares_common_extract_dives (abstract->context, device->layout, device->fingerprint,
    243 		dc_buffer_get_data (buffer), callback, userdata);
    244 
    245 	dc_buffer_free (buffer);
    246 
    247 	return rc;
    248 }