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

suunto_d9.c (9208B)


      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> // memcmp, memcpy
     23 #include <stdlib.h> // malloc, free
     24 #include <assert.h> // assert
     25 
     26 #include "suunto_d9.h"
     27 #include "suunto_common2.h"
     28 #include "context-private.h"
     29 #include "checksum.h"
     30 #include "array.h"
     31 
     32 #define ISINSTANCE(device) dc_device_isinstance((device), (const dc_device_vtable_t *) &suunto_d9_device_vtable)
     33 
     34 #define D4i         0x19
     35 #define D6i         0x1A
     36 #define D9tx        0x1B
     37 #define DX          0x1C
     38 #define VYPERNOVO   0x1D
     39 #define ZOOPNOVO_A  0x1E
     40 #define ZOOPNOVO_B  0x1F
     41 #define D4F         0x20
     42 
     43 typedef struct suunto_d9_device_t {
     44 	suunto_common2_device_t base;
     45 	dc_iostream_t *iostream;
     46 } suunto_d9_device_t;
     47 
     48 static dc_status_t suunto_d9_device_packet (dc_device_t *abstract, const unsigned char command[], unsigned int csize, unsigned char answer[], unsigned int asize, unsigned int size);
     49 
     50 static const suunto_common2_device_vtable_t suunto_d9_device_vtable = {
     51 	{
     52 		sizeof(suunto_d9_device_t),
     53 		DC_FAMILY_SUUNTO_D9,
     54 		suunto_common2_device_set_fingerprint, /* set_fingerprint */
     55 		suunto_common2_device_read, /* read */
     56 		suunto_common2_device_write, /* write */
     57 		suunto_common2_device_dump, /* dump */
     58 		suunto_common2_device_foreach, /* foreach */
     59 		suunto_common2_device_timesync, /* timesync */
     60 		NULL /* close */
     61 	},
     62 	suunto_d9_device_packet
     63 };
     64 
     65 static const suunto_common2_layout_t suunto_d9_layout = {
     66 	0x8000, /* memsize */
     67 	0x0011, /* fingerprint */
     68 	0x0023, /* serial */
     69 	0x019A, /* rb_profile_begin */
     70 	0x7FFE /* rb_profile_end */
     71 };
     72 
     73 static const suunto_common2_layout_t suunto_d9tx_layout = {
     74 	0x10000, /* memsize */
     75 	0x0013, /* fingerprint */
     76 	0x0024, /* serial */
     77 	0x019A, /* rb_profile_begin */
     78 	0xEBF0 /* rb_profile_end */
     79 };
     80 
     81 static const suunto_common2_layout_t suunto_dx_layout = {
     82 	0x10000, /* memsize */
     83 	0x0017, /* fingerprint */
     84 	0x0024, /* serial */
     85 	0x019A, /* rb_profile_begin */
     86 	0xEBF0 /* rb_profile_end */
     87 };
     88 
     89 
     90 static dc_status_t
     91 suunto_d9_device_autodetect (suunto_d9_device_t *device, unsigned int model)
     92 {
     93 	dc_status_t status = DC_STATUS_SUCCESS;
     94 	dc_device_t *abstract = (dc_device_t *) device;
     95 
     96 	// The list with possible baudrates.
     97 	const int baudrates[] = {9600, 115200};
     98 
     99 	// Use the model number as a hint to speedup the detection.
    100 	unsigned int hint = 0;
    101 	if (model == D4i || model == D6i || model == D9tx ||
    102 		model == DX || model == VYPERNOVO || model == ZOOPNOVO_A || model == ZOOPNOVO_B ||
    103 		model == D4F)
    104 		hint = 1;
    105 
    106 	for (unsigned int i = 0; i < C_ARRAY_SIZE(baudrates); ++i) {
    107 		// Use the baudrate array as circular array, starting from the hint.
    108 		unsigned int idx = (hint + i) % C_ARRAY_SIZE(baudrates);
    109 
    110 		// Adjust the baudrate.
    111 		status = dc_iostream_configure (device->iostream, baudrates[idx], 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
    112 		if (status != DC_STATUS_SUCCESS) {
    113 			ERROR (abstract->context, "Failed to set the terminal attributes.");
    114 			return status;
    115 		}
    116 
    117 		// Try reading the version info.
    118 		status = suunto_common2_device_version ((dc_device_t *) device, device->base.version, sizeof (device->base.version));
    119 		if (status == DC_STATUS_SUCCESS)
    120 			break;
    121 	}
    122 
    123 	return status;
    124 }
    125 
    126 
    127 dc_status_t
    128 suunto_d9_device_open (dc_device_t **out, dc_context_t *context, dc_iostream_t *iostream, unsigned int model)
    129 {
    130 	dc_status_t status = DC_STATUS_SUCCESS;
    131 	suunto_d9_device_t *device = NULL;
    132 
    133 	if (out == NULL)
    134 		return DC_STATUS_INVALIDARGS;
    135 
    136 	// Allocate memory.
    137 	device = (suunto_d9_device_t *) dc_device_allocate (context, &suunto_d9_device_vtable.base);
    138 	if (device == NULL) {
    139 		ERROR (context, "Failed to allocate memory.");
    140 		return DC_STATUS_NOMEMORY;
    141 	}
    142 
    143 	// Initialize the base class.
    144 	suunto_common2_device_init (&device->base);
    145 
    146 	// Set the default values.
    147 	device->iostream = iostream;
    148 
    149 	// Set the serial communication protocol (9600 8N1).
    150 	status = dc_iostream_configure (device->iostream, 9600, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
    151 	if (status != DC_STATUS_SUCCESS) {
    152 		ERROR (context, "Failed to set the terminal attributes.");
    153 		goto error_free;
    154 	}
    155 
    156 	// Set the timeout for receiving data (3000 ms).
    157 	status = dc_iostream_set_timeout (device->iostream, 3000);
    158 	if (status != DC_STATUS_SUCCESS) {
    159 		ERROR (context, "Failed to set the timeout.");
    160 		goto error_free;
    161 	}
    162 
    163 	// Set the DTR line (power supply for the interface).
    164 	status = dc_iostream_set_dtr (device->iostream, 1);
    165 	if (status != DC_STATUS_SUCCESS) {
    166 		ERROR (context, "Failed to set the DTR line.");
    167 		goto error_free;
    168 	}
    169 
    170 	// Give the interface 100 ms to settle and draw power up.
    171 	dc_iostream_sleep (device->iostream, 100);
    172 
    173 	// Make sure everything is in a sane state.
    174 	dc_iostream_purge (device->iostream, DC_DIRECTION_ALL);
    175 
    176 	// Try to autodetect the protocol variant.
    177 	status = suunto_d9_device_autodetect (device, model);
    178 	if (status != DC_STATUS_SUCCESS) {
    179 		ERROR (context, "Failed to identify the protocol variant.");
    180 		goto error_free;
    181 	}
    182 
    183 	HEXDUMP (context, DC_LOGLEVEL_DEBUG, "Version", device->base.version, sizeof (device->base.version));
    184 
    185 	// Override the base class values.
    186 	model = device->base.version[0];
    187 	if (model == D4i || model == D6i || model == D9tx ||
    188 		model == VYPERNOVO || model == ZOOPNOVO_A || model == ZOOPNOVO_B ||
    189 		model == D4F)
    190 		device->base.layout = &suunto_d9tx_layout;
    191 	else if (model == DX)
    192 		device->base.layout = &suunto_dx_layout;
    193 	else
    194 		device->base.layout = &suunto_d9_layout;
    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 suunto_d9_device_packet (dc_device_t *abstract, const unsigned char command[], unsigned int csize, unsigned char answer[], unsigned int asize, unsigned int size)
    208 {
    209 	dc_status_t status = DC_STATUS_SUCCESS;
    210 	suunto_d9_device_t *device = (suunto_d9_device_t *) abstract;
    211 
    212 	if (device_is_cancelled (abstract))
    213 		return DC_STATUS_CANCELLED;
    214 
    215 	// Clear RTS to send the command.
    216 	status = dc_iostream_set_rts (device->iostream, 0);
    217 	if (status != DC_STATUS_SUCCESS) {
    218 		ERROR (abstract->context, "Failed to clear RTS.");
    219 		return status;
    220 	}
    221 
    222 	// Send the command to the dive computer.
    223 	status = dc_iostream_write (device->iostream, command, csize, NULL);
    224 	if (status != DC_STATUS_SUCCESS) {
    225 		ERROR (abstract->context, "Failed to send the command.");
    226 		return status;
    227 	}
    228 
    229 	// Receive the echo.
    230 	unsigned char echo[128] = {0};
    231 	assert (sizeof (echo) >= csize);
    232 	status = dc_iostream_read (device->iostream, echo, csize, NULL);
    233 	if (status != DC_STATUS_SUCCESS) {
    234 		ERROR (abstract->context, "Failed to receive the echo.");
    235 		return status;
    236 	}
    237 
    238 	// Verify the echo.
    239 	if (memcmp (command, echo, csize) != 0) {
    240 		ERROR (abstract->context, "Unexpected echo.");
    241 		return DC_STATUS_PROTOCOL;
    242 	}
    243 
    244 	// Set RTS to receive the reply.
    245 	status = dc_iostream_set_rts (device->iostream, 1);
    246 	if (status != DC_STATUS_SUCCESS) {
    247 		ERROR (abstract->context, "Failed to set RTS.");
    248 		return status;
    249 	}
    250 
    251 	// Receive the answer of the dive computer.
    252 	status = dc_iostream_read (device->iostream, answer, asize, NULL);
    253 	if (status != DC_STATUS_SUCCESS) {
    254 		ERROR (abstract->context, "Failed to receive the answer.");
    255 		return status;
    256 	}
    257 
    258 	// Verify the header of the package.
    259 	if (answer[0] != command[0]) {
    260 		ERROR (abstract->context, "Unexpected answer header.");
    261 		return DC_STATUS_PROTOCOL;
    262 	}
    263 
    264 	// Verify the size of the package.
    265 	unsigned int len = array_uint16_be (answer + 1);
    266 	if (len + 4 != asize) {
    267 		ERROR (abstract->context, "Unexpected answer size.");
    268 		return DC_STATUS_PROTOCOL;
    269 	}
    270 
    271 	// Verify the parameters of the package.
    272 	if (memcmp (command + 3, answer + 3, asize - size - 4) != 0) {
    273 		ERROR (abstract->context, "Unexpected answer parameters.");
    274 		return DC_STATUS_PROTOCOL;
    275 	}
    276 
    277 	// Verify the checksum of the package.
    278 	unsigned char crc = answer[asize - 1];
    279 	unsigned char ccrc = checksum_xor_uint8 (answer, asize - 1, 0x00);
    280 	if (crc != ccrc) {
    281 		ERROR (abstract->context, "Unexpected answer checksum.");
    282 		return DC_STATUS_PROTOCOL;
    283 	}
    284 
    285 	return DC_STATUS_SUCCESS;
    286 }
    287 
    288 
    289 dc_status_t
    290 suunto_d9_device_version (dc_device_t *abstract, unsigned char data[], unsigned int size)
    291 {
    292 	if (!ISINSTANCE (abstract))
    293 		return DC_STATUS_INVALIDARGS;
    294 
    295 	return suunto_common2_device_version (abstract, data, size);
    296 }
    297 
    298 
    299 dc_status_t
    300 suunto_d9_device_reset_maxdepth (dc_device_t *abstract)
    301 {
    302 	if (!ISINSTANCE (abstract))
    303 		return DC_STATUS_INVALIDARGS;
    304 
    305 	return suunto_common2_device_reset_maxdepth (abstract);
    306 }