suunto_vyper2.c (9021B)
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 25 #include "suunto_vyper2.h" 26 #include "suunto_common2.h" 27 #include "context-private.h" 28 #include "checksum.h" 29 #include "array.h" 30 #include "timer.h" 31 32 #define ISINSTANCE(device) dc_device_isinstance((device), (const dc_device_vtable_t *) &suunto_vyper2_device_vtable) 33 34 #define HELO2 0x15 35 36 typedef struct suunto_vyper2_device_t { 37 suunto_common2_device_t base; 38 dc_iostream_t *iostream; 39 dc_timer_t *timer; 40 } suunto_vyper2_device_t; 41 42 static dc_status_t suunto_vyper2_device_packet (dc_device_t *abstract, const unsigned char command[], unsigned int csize, unsigned char answer[], unsigned int asize, unsigned int size); 43 static dc_status_t suunto_vyper2_device_close (dc_device_t *abstract); 44 45 static const suunto_common2_device_vtable_t suunto_vyper2_device_vtable = { 46 { 47 sizeof(suunto_vyper2_device_t), 48 DC_FAMILY_SUUNTO_VYPER2, 49 suunto_common2_device_set_fingerprint, /* set_fingerprint */ 50 suunto_common2_device_read, /* read */ 51 suunto_common2_device_write, /* write */ 52 suunto_common2_device_dump, /* dump */ 53 suunto_common2_device_foreach, /* foreach */ 54 suunto_common2_device_timesync, /* timesync */ 55 suunto_vyper2_device_close /* close */ 56 }, 57 suunto_vyper2_device_packet 58 }; 59 60 static const suunto_common2_layout_t suunto_vyper2_layout = { 61 0x8000, /* memsize */ 62 0x0011, /* fingerprint */ 63 0x0023, /* serial */ 64 0x019A, /* rb_profile_begin */ 65 0x7FFE /* rb_profile_end */ 66 }; 67 68 static const suunto_common2_layout_t suunto_helo2_layout = { 69 0x8000, /* memsize */ 70 0x0017, /* fingerprint */ 71 0x0023, /* serial */ 72 0x019A, /* rb_profile_begin */ 73 0x7FFE /* rb_profile_end */ 74 }; 75 76 77 dc_status_t 78 suunto_vyper2_device_open (dc_device_t **out, dc_context_t *context, dc_iostream_t *iostream) 79 { 80 dc_status_t status = DC_STATUS_SUCCESS; 81 suunto_vyper2_device_t *device = NULL; 82 83 if (out == NULL) 84 return DC_STATUS_INVALIDARGS; 85 86 // Allocate memory. 87 device = (suunto_vyper2_device_t *) dc_device_allocate (context, &suunto_vyper2_device_vtable.base); 88 if (device == NULL) { 89 ERROR (context, "Failed to allocate memory."); 90 return DC_STATUS_NOMEMORY; 91 } 92 93 // Initialize the base class. 94 suunto_common2_device_init (&device->base); 95 96 // Set the default values. 97 device->iostream = iostream; 98 99 // Create a high resolution timer. 100 status = dc_timer_new (&device->timer); 101 if (status != DC_STATUS_SUCCESS) { 102 ERROR (context, "Failed to create a high resolution timer."); 103 goto error_free; 104 } 105 106 // Set the serial communication protocol (9600 8N1). 107 status = dc_iostream_configure (device->iostream, 9600, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE); 108 if (status != DC_STATUS_SUCCESS) { 109 ERROR (context, "Failed to set the terminal attributes."); 110 goto error_timer_free; 111 } 112 113 // Set the timeout for receiving data (3000 ms). 114 status = dc_iostream_set_timeout (device->iostream, 3000); 115 if (status != DC_STATUS_SUCCESS) { 116 ERROR (context, "Failed to set the timeout."); 117 goto error_timer_free; 118 } 119 120 // Set the DTR line (power supply for the interface). 121 status = dc_iostream_set_dtr (device->iostream, 1); 122 if (status != DC_STATUS_SUCCESS) { 123 ERROR (context, "Failed to set the DTR line."); 124 goto error_timer_free; 125 } 126 127 // Give the interface 100 ms to settle and draw power up. 128 dc_iostream_sleep (device->iostream, 100); 129 130 // Make sure everything is in a sane state. 131 status = dc_iostream_purge (device->iostream, DC_DIRECTION_ALL); 132 if (status != DC_STATUS_SUCCESS) { 133 ERROR (context, "Failed to reset IO state."); 134 goto error_timer_free; 135 } 136 137 // Read the version info. 138 status = suunto_common2_device_version ((dc_device_t *) device, device->base.version, sizeof (device->base.version)); 139 if (status != DC_STATUS_SUCCESS) { 140 ERROR (context, "Failed to read the version info."); 141 goto error_timer_free; 142 } 143 144 HEXDUMP (context, DC_LOGLEVEL_DEBUG, "Version", device->base.version, sizeof (device->base.version)); 145 146 // Override the base class values. 147 unsigned int model = device->base.version[0]; 148 if (model == HELO2) 149 device->base.layout = &suunto_helo2_layout; 150 else 151 device->base.layout = &suunto_vyper2_layout; 152 153 *out = (dc_device_t*) device; 154 155 return DC_STATUS_SUCCESS; 156 157 error_timer_free: 158 dc_timer_free (device->timer); 159 error_free: 160 dc_device_deallocate ((dc_device_t *) device); 161 return status; 162 } 163 164 165 static dc_status_t 166 suunto_vyper2_device_close (dc_device_t *abstract) 167 { 168 suunto_vyper2_device_t *device = (suunto_vyper2_device_t*) abstract; 169 170 dc_timer_free (device->timer); 171 172 return DC_STATUS_SUCCESS; 173 } 174 175 176 static dc_status_t 177 suunto_vyper2_device_packet (dc_device_t *abstract, const unsigned char command[], unsigned int csize, unsigned char answer[], unsigned int asize, unsigned int size) 178 { 179 dc_status_t status = DC_STATUS_SUCCESS; 180 suunto_vyper2_device_t *device = (suunto_vyper2_device_t *) abstract; 181 182 if (device_is_cancelled (abstract)) 183 return DC_STATUS_CANCELLED; 184 185 dc_iostream_sleep (device->iostream, 600); 186 187 // Set RTS to send the command. 188 status = dc_iostream_set_rts (device->iostream, 1); 189 if (status != DC_STATUS_SUCCESS) { 190 ERROR (abstract->context, "Failed to set the RTS line."); 191 return status; 192 } 193 194 // Get the current timestamp. 195 dc_usecs_t begin = 0; 196 status = dc_timer_now (device->timer, &begin); 197 if (status != DC_STATUS_SUCCESS) { 198 return status; 199 } 200 201 // Send the command to the dive computer. 202 status = dc_iostream_write (device->iostream, command, csize, NULL); 203 if (status != DC_STATUS_SUCCESS) { 204 ERROR (abstract->context, "Failed to send the command."); 205 return status; 206 } 207 208 // Get the current timestamp. 209 dc_usecs_t end = 0; 210 status = dc_timer_now (device->timer, &end); 211 if (status != DC_STATUS_SUCCESS) { 212 return status; 213 } 214 215 // Calculate the elapsed time. 216 dc_usecs_t elapsed = end - begin; 217 218 // Calculate the expected duration. A 2 millisecond fudge factor is added 219 // because it improves the success rate significantly. 220 unsigned int baudrate = 9600; 221 unsigned int nbits = 1 + 8 /* databits */ + 1 /* stopbits */ + 0 /* parity */; 222 dc_usecs_t expected = (dc_usecs_t) csize * 1000000 * nbits / baudrate + 2000; 223 224 // Wait for the remaining time. 225 if (elapsed < expected) { 226 dc_usecs_t remaining = expected - elapsed; 227 228 // The remaining time is rounded up to the nearest millisecond 229 // because the sleep function doesn't support a higher 230 // resolution on all platforms. The higher resolution is 231 // pointless anyway, since we already added a fudge factor 232 // above. 233 dc_iostream_sleep (device->iostream, (remaining + 999) / 1000); 234 } 235 236 // Clear RTS to receive the reply. 237 status = dc_iostream_set_rts (device->iostream, 0); 238 if (status != DC_STATUS_SUCCESS) { 239 ERROR (abstract->context, "Failed to set the RTS line."); 240 return status; 241 } 242 243 // Receive the answer of the dive computer. 244 status = dc_iostream_read (device->iostream, answer, asize, NULL); 245 if (status != DC_STATUS_SUCCESS) { 246 ERROR (abstract->context, "Failed to receive the answer."); 247 return status; 248 } 249 250 // Verify the header of the package. 251 if (answer[0] != command[0]) { 252 ERROR (abstract->context, "Unexpected answer header."); 253 return DC_STATUS_PROTOCOL; 254 } 255 256 // Verify the size of the package. 257 unsigned int len = array_uint16_be (answer + 1); 258 if (len + 4 != asize) { 259 ERROR (abstract->context, "Unexpected answer size."); 260 return DC_STATUS_PROTOCOL; 261 } 262 263 // Verify the parameters of the package. 264 if (memcmp (command + 3, answer + 3, asize - size - 4) != 0) { 265 ERROR (abstract->context, "Unexpected answer parameters."); 266 return DC_STATUS_PROTOCOL; 267 } 268 269 // Verify the checksum of the package. 270 unsigned char crc = answer[asize - 1]; 271 unsigned char ccrc = checksum_xor_uint8 (answer, asize - 1, 0x00); 272 if (crc != ccrc) { 273 ERROR (abstract->context, "Unexpected answer checksum."); 274 return DC_STATUS_PROTOCOL; 275 } 276 277 return DC_STATUS_SUCCESS; 278 } 279 280 281 dc_status_t 282 suunto_vyper2_device_version (dc_device_t *abstract, unsigned char data[], unsigned int size) 283 { 284 if (!ISINSTANCE (abstract)) 285 return DC_STATUS_INVALIDARGS; 286 287 return suunto_common2_device_version (abstract, data, size); 288 } 289 290 291 dc_status_t 292 suunto_vyper2_device_reset_maxdepth (dc_device_t *abstract) 293 { 294 if (!ISINSTANCE (abstract)) 295 return DC_STATUS_INVALIDARGS; 296 297 return suunto_common2_device_reset_maxdepth (abstract); 298 }