suunto_vyper.c (18013B)
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_vyper.h" 27 #include "suunto_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), &suunto_vyper_device_vtable) 34 35 #define MIN(a,b) (((a) < (b)) ? (a) : (b)) 36 #define MAX(a,b) (((a) > (b)) ? (a) : (b)) 37 38 #define SZ_MEMORY 0x2000 39 #define SZ_PACKET 32 40 41 #define HDR_DEVINFO_VYPER 0x24 42 #define HDR_DEVINFO_SPYDER 0x16 43 #define HDR_DEVINFO_BEGIN (HDR_DEVINFO_SPYDER) 44 #define HDR_DEVINFO_END (HDR_DEVINFO_VYPER + 6) 45 46 typedef struct suunto_vyper_device_t { 47 suunto_common_device_t base; 48 dc_iostream_t *iostream; 49 } suunto_vyper_device_t; 50 51 static dc_status_t suunto_vyper_device_read (dc_device_t *abstract, unsigned int address, unsigned char data[], unsigned int size); 52 static dc_status_t suunto_vyper_device_write (dc_device_t *abstract, unsigned int address, const unsigned char data[], unsigned int size); 53 static dc_status_t suunto_vyper_device_dump (dc_device_t *abstract, dc_buffer_t *buffer); 54 static dc_status_t suunto_vyper_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata); 55 56 static const dc_device_vtable_t suunto_vyper_device_vtable = { 57 sizeof(suunto_vyper_device_t), 58 DC_FAMILY_SUUNTO_VYPER, 59 suunto_common_device_set_fingerprint, /* set_fingerprint */ 60 suunto_vyper_device_read, /* read */ 61 suunto_vyper_device_write, /* write */ 62 suunto_vyper_device_dump, /* dump */ 63 suunto_vyper_device_foreach, /* foreach */ 64 NULL, /* timesync */ 65 NULL /* close */ 66 }; 67 68 static const suunto_common_layout_t suunto_vyper_layout = { 69 0x51, /* eop */ 70 0x71, /* rb_profile_begin */ 71 SZ_MEMORY, /* rb_profile_end */ 72 9, /* fp_offset */ 73 5 /* peek */ 74 }; 75 76 static const suunto_common_layout_t suunto_spyder_layout = { 77 0x1C, /* eop */ 78 0x4C, /* rb_profile_begin */ 79 SZ_MEMORY, /* rb_profile_end */ 80 6, /* fp_offset */ 81 3 /* peek */ 82 }; 83 84 85 dc_status_t 86 suunto_vyper_device_open (dc_device_t **out, dc_context_t *context, dc_iostream_t *iostream) 87 { 88 dc_status_t status = DC_STATUS_SUCCESS; 89 suunto_vyper_device_t *device = NULL; 90 91 if (out == NULL) 92 return DC_STATUS_INVALIDARGS; 93 94 // Allocate memory. 95 device = (suunto_vyper_device_t *) dc_device_allocate (context, &suunto_vyper_device_vtable); 96 if (device == NULL) { 97 ERROR (context, "Failed to allocate memory."); 98 return DC_STATUS_NOMEMORY; 99 } 100 101 // Initialize the base class. 102 suunto_common_device_init (&device->base); 103 104 // Set the default values. 105 device->iostream = iostream; 106 107 // Set the serial communication protocol (2400 8O1). 108 status = dc_iostream_configure (device->iostream, 2400, 8, DC_PARITY_ODD, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE); 109 if (status != DC_STATUS_SUCCESS) { 110 ERROR (context, "Failed to set the terminal attributes."); 111 goto error_free; 112 } 113 114 // Set the timeout for receiving data (1000 ms). 115 status = dc_iostream_set_timeout (device->iostream, 1000); 116 if (status != DC_STATUS_SUCCESS) { 117 ERROR (context, "Failed to set the timeout."); 118 goto error_free; 119 } 120 121 // Set the DTR line (power supply for the interface). 122 status = dc_iostream_set_dtr (device->iostream, 1); 123 if (status != DC_STATUS_SUCCESS) { 124 ERROR (context, "Failed to set the DTR line."); 125 goto error_free; 126 } 127 128 // Give the interface 100 ms to settle and draw power up. 129 dc_iostream_sleep (device->iostream, 100); 130 131 // Make sure everything is in a sane state. 132 dc_iostream_purge (device->iostream, DC_DIRECTION_ALL); 133 134 *out = (dc_device_t*) device; 135 136 return DC_STATUS_SUCCESS; 137 138 error_free: 139 dc_device_deallocate ((dc_device_t *) device); 140 return status; 141 } 142 143 144 static dc_status_t 145 suunto_vyper_send (suunto_vyper_device_t *device, const unsigned char command[], unsigned int csize) 146 { 147 dc_status_t status = DC_STATUS_SUCCESS; 148 dc_device_t *abstract = (dc_device_t *) device; 149 150 dc_iostream_sleep (device->iostream, 500); 151 152 // Set RTS to send the command. 153 status = dc_iostream_set_rts (device->iostream, 1); 154 if (status != DC_STATUS_SUCCESS) { 155 ERROR (abstract->context, "Failed to set RTS."); 156 return status; 157 } 158 159 // Send the command to the dive computer. 160 status = dc_iostream_write (device->iostream, command, csize, NULL); 161 if (status != DC_STATUS_SUCCESS) { 162 ERROR (abstract->context, "Failed to send the command."); 163 return status; 164 } 165 166 // If the interface sends an echo back (which is the case for many clone 167 // interfaces), this echo should be removed from the input queue before 168 // attempting to read the real reply from the dive computer. Otherwise, 169 // the data transfer will fail. Timing is also critical here! We have to 170 // wait at least until the echo appears (40ms), but not until the reply 171 // from the dive computer appears (600ms). 172 // The original suunto interface does not have this problem, because it 173 // does not send an echo and the RTS switching makes it impossible to 174 // receive the reply before RTS is cleared. We have to wait some time 175 // before clearing RTS (around 30ms). But if we wait too long (> 500ms), 176 // the reply disappears again. 177 dc_iostream_sleep (device->iostream, 200); 178 dc_iostream_purge (device->iostream, DC_DIRECTION_INPUT); 179 180 // Clear RTS to receive the reply. 181 status = dc_iostream_set_rts (device->iostream, 0); 182 if (status != DC_STATUS_SUCCESS) { 183 ERROR (abstract->context, "Failed to clear RTS."); 184 return status; 185 } 186 187 return DC_STATUS_SUCCESS; 188 } 189 190 191 static dc_status_t 192 suunto_vyper_transfer (suunto_vyper_device_t *device, const unsigned char command[], unsigned int csize, unsigned char answer[], unsigned int asize, unsigned int size) 193 { 194 dc_status_t status = DC_STATUS_SUCCESS; 195 dc_device_t *abstract = (dc_device_t *) device; 196 197 assert (asize >= size + 2); 198 199 if (device_is_cancelled (abstract)) 200 return DC_STATUS_CANCELLED; 201 202 // Send the command to the dive computer. 203 dc_status_t rc = suunto_vyper_send (device, command, csize); 204 if (rc != DC_STATUS_SUCCESS) { 205 ERROR (abstract->context, "Failed to send the command."); 206 return rc; 207 } 208 209 // Receive the answer of the dive computer. 210 status = dc_iostream_read (device->iostream, answer, asize, NULL); 211 if (status != DC_STATUS_SUCCESS) { 212 ERROR (abstract->context, "Failed to receive the answer."); 213 return status; 214 } 215 216 // Verify the header of the package. 217 if (memcmp (command, answer, asize - size - 1) != 0) { 218 ERROR (abstract->context, "Unexpected answer start byte(s)."); 219 return DC_STATUS_PROTOCOL; 220 } 221 222 // Verify the checksum of the package. 223 unsigned char crc = answer[asize - 1]; 224 unsigned char ccrc = checksum_xor_uint8 (answer, asize - 1, 0x00); 225 if (crc != ccrc) { 226 ERROR (abstract->context, "Unexpected answer checksum."); 227 return DC_STATUS_PROTOCOL; 228 } 229 230 return DC_STATUS_SUCCESS; 231 } 232 233 234 static dc_status_t 235 suunto_vyper_device_read (dc_device_t *abstract, unsigned int address, unsigned char data[], unsigned int size) 236 { 237 suunto_vyper_device_t *device = (suunto_vyper_device_t*) abstract; 238 239 unsigned int nbytes = 0; 240 while (nbytes < size) { 241 // Calculate the package size. 242 unsigned int len = MIN (size - nbytes, SZ_PACKET); 243 244 // Read the package. 245 unsigned char answer[SZ_PACKET + 5] = {0}; 246 unsigned char command[5] = {0x05, 247 (address >> 8) & 0xFF, // high 248 (address ) & 0xFF, // low 249 len, // count 250 0}; // CRC 251 command[4] = checksum_xor_uint8 (command, 4, 0x00); 252 dc_status_t rc = suunto_vyper_transfer (device, command, sizeof (command), answer, len + 5, len); 253 if (rc != DC_STATUS_SUCCESS) 254 return rc; 255 256 memcpy (data, answer + 4, len); 257 258 nbytes += len; 259 address += len; 260 data += len; 261 } 262 263 return DC_STATUS_SUCCESS; 264 } 265 266 267 static dc_status_t 268 suunto_vyper_device_write (dc_device_t *abstract, unsigned int address, const unsigned char data[], unsigned int size) 269 { 270 suunto_vyper_device_t *device = (suunto_vyper_device_t*) abstract; 271 272 unsigned int nbytes = 0; 273 while (nbytes < size) { 274 // Calculate the package size. 275 unsigned int len = MIN (size - nbytes, SZ_PACKET); 276 277 // Prepare to write the package. 278 unsigned char panswer[3] = {0}; 279 unsigned char pcommand[3] = {0x07, 0xA5, 0xA2}; 280 dc_status_t rc = suunto_vyper_transfer (device, pcommand, sizeof (pcommand), panswer, sizeof (panswer), 0); 281 if (rc != DC_STATUS_SUCCESS) 282 return rc; 283 284 // Write the package. 285 unsigned char wanswer[5] = {0}; 286 unsigned char wcommand[SZ_PACKET + 5] = {0x06, 287 (address >> 8) & 0xFF, // high 288 (address ) & 0xFF, // low 289 len, // count 290 0}; // data + CRC 291 memcpy (wcommand + 4, data, len); 292 wcommand[len + 4] = checksum_xor_uint8 (wcommand, len + 4, 0x00); 293 rc = suunto_vyper_transfer (device, wcommand, len + 5, wanswer, sizeof (wanswer), 0); 294 if (rc != DC_STATUS_SUCCESS) 295 return rc; 296 297 nbytes += len; 298 address += len; 299 data += len; 300 } 301 302 return DC_STATUS_SUCCESS; 303 } 304 305 306 static dc_status_t 307 suunto_vyper_read_dive (dc_device_t *abstract, dc_buffer_t *buffer, int init, dc_event_progress_t *progress) 308 { 309 dc_status_t status = DC_STATUS_SUCCESS; 310 suunto_vyper_device_t *device = (suunto_vyper_device_t*) abstract; 311 312 if (device_is_cancelled (abstract)) 313 return DC_STATUS_CANCELLED; 314 315 // Erase the current contents of the buffer. 316 if (!dc_buffer_clear (buffer)) { 317 ERROR (abstract->context, "Insufficient buffer space available."); 318 return DC_STATUS_NOMEMORY; 319 } 320 321 // Send the command to the dive computer. 322 unsigned char command[3] = {init ? 0x08 : 0x09, 0xA5, 0x00}; 323 command[2] = checksum_xor_uint8 (command, 2, 0x00); 324 dc_status_t rc = suunto_vyper_send (device, command, 3); 325 if (rc != DC_STATUS_SUCCESS) { 326 ERROR (abstract->context, "Failed to send the command."); 327 return rc; 328 } 329 330 unsigned int nbytes = 0; 331 for (unsigned int npackages = 0;; ++npackages) { 332 // Receive the header of the package. 333 size_t n = 0; 334 unsigned char answer[SZ_PACKET + 3] = {0}; 335 status = dc_iostream_read (device->iostream, answer, 2, &n); 336 if (status != DC_STATUS_SUCCESS) { 337 // If no data is received because a timeout occured, we assume 338 // the last package was already received and the transmission 339 // can be finished. Unfortunately this is not 100% reliable, 340 // because there is always a small chance that more data will 341 // arrive later (especially with a short timeout). But it works 342 // good enough in practice. 343 // Only for the very first package, we can be sure there was 344 // an error, because the DC always sends at least one package. 345 if (n == 0 && npackages != 0) 346 break; 347 ERROR (abstract->context, "Failed to receive the answer."); 348 return status; 349 } 350 351 // Verify the header of the package. 352 if (answer[0] != command[0] || 353 answer[1] > SZ_PACKET) { 354 ERROR (abstract->context, "Unexpected answer start byte(s)."); 355 return DC_STATUS_PROTOCOL; 356 } 357 358 // Receive the remaining part of the package. 359 unsigned char len = answer[1]; 360 status = dc_iostream_read (device->iostream, answer + 2, len + 1, NULL); 361 if (status != DC_STATUS_SUCCESS) { 362 ERROR (abstract->context, "Failed to receive the answer."); 363 return status; 364 } 365 366 // Verify the checksum of the package. 367 unsigned char crc = answer[len + 2]; 368 unsigned char ccrc = checksum_xor_uint8 (answer, len + 2, 0x00); 369 if (crc != ccrc) { 370 ERROR (abstract->context, "Unexpected answer checksum."); 371 return DC_STATUS_PROTOCOL; 372 } 373 374 // The DC sends a null package (a package with length zero) when it 375 // has reached the end of its internal ring buffer. From this point on, 376 // the current dive has been overwritten with newer data. Therefore, 377 // we discard the current (incomplete) dive and end the transmission. 378 if (len == 0) { 379 dc_buffer_clear (buffer); 380 return DC_STATUS_SUCCESS; 381 } 382 383 // Update and emit a progress event. 384 if (progress) { 385 progress->current += len; 386 if (progress->current > progress->maximum) 387 progress->current = progress->maximum; 388 device_event_emit (abstract, DC_EVENT_PROGRESS, progress); 389 } 390 391 // Append the package to the output buffer. 392 // Reporting of buffer errors is delayed until the entire 393 // transfer is finished. This approach leaves no data behind in 394 // the serial receive buffer, and if this packet is part of the 395 // last incomplete dive, no error has to be reported at all. 396 dc_buffer_append (buffer, answer + 2, len); 397 398 nbytes += len; 399 400 // If a package is smaller than $SZ_PACKET bytes, 401 // we assume it's the last packet and the transmission can be 402 // finished early. However, this approach does not work if the 403 // last packet is exactly $SZ_PACKET bytes long! 404 #if 0 405 if (len != SZ_PACKET) 406 break; 407 #endif 408 } 409 410 // Check for a buffer error. 411 if (dc_buffer_get_size (buffer) != nbytes) { 412 ERROR (abstract->context, "Insufficient buffer space available."); 413 return DC_STATUS_NOMEMORY; 414 } 415 416 // The DC traverses its internal ring buffer backwards. The most recent 417 // dive is send first (which allows you to download only the new dives), 418 // but also the contents of each dive is reversed. Therefore, we reverse 419 // the bytes again before returning them to the application. 420 array_reverse_bytes (dc_buffer_get_data (buffer), dc_buffer_get_size (buffer)); 421 422 return DC_STATUS_SUCCESS; 423 } 424 425 426 static dc_status_t 427 suunto_vyper_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) 428 { 429 dc_status_t status = DC_STATUS_SUCCESS; 430 431 // Allocate the required amount of memory. 432 if (!dc_buffer_resize (buffer, SZ_MEMORY)) { 433 ERROR (abstract->context, "Insufficient buffer space available."); 434 return DC_STATUS_NOMEMORY; 435 } 436 437 // Download the memory dump. 438 status = device_dump_read (abstract, 0, dc_buffer_get_data (buffer), 439 dc_buffer_get_size (buffer), SZ_PACKET); 440 if (status != DC_STATUS_SUCCESS) { 441 return status; 442 } 443 444 // Identify the connected device as a Vyper or a Spyder, by inspecting 445 // the Vyper model code. For a Spyder, this value will contain the 446 // sample interval (20, 30 or 60s) instead of the model code. 447 unsigned char *data = dc_buffer_get_data (buffer); 448 unsigned int hoffset = HDR_DEVINFO_VYPER; 449 if (data[hoffset] == 20 || data[hoffset] == 30 || data[hoffset] == 60) { 450 hoffset = HDR_DEVINFO_SPYDER; 451 } 452 453 // Emit a device info event. 454 dc_event_devinfo_t devinfo; 455 devinfo.model = data[hoffset + 0]; 456 devinfo.firmware = data[hoffset + 1]; 457 devinfo.serial = 0; 458 devinfo.serial = array_convert_bin2dec (data + hoffset + 2, 4); 459 device_event_emit (abstract, DC_EVENT_DEVINFO, &devinfo); 460 461 return status; 462 } 463 464 465 static dc_status_t 466 suunto_vyper_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata) 467 { 468 suunto_common_device_t *device = (suunto_common_device_t*) abstract; 469 470 // Enable progress notifications. 471 dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER; 472 progress.maximum = SZ_MEMORY; 473 device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); 474 475 // Read the device info. The Vyper and the Spyder store this data 476 // in a different location. To minimize the number of (slow) reads, 477 // we read a larger block of memory that always contains the data 478 // for both devices. 479 unsigned char header[HDR_DEVINFO_END - HDR_DEVINFO_BEGIN] = {0}; 480 dc_status_t rc = suunto_vyper_device_read (abstract, HDR_DEVINFO_BEGIN, header, sizeof (header)); 481 if (rc != DC_STATUS_SUCCESS) 482 return rc; 483 484 // Identify the connected device as a Vyper or a Spyder, by inspecting 485 // the Vyper model code. For a Spyder, this value will contain the 486 // sample interval (20, 30 or 60s) instead of the model code. 487 unsigned int hoffset = HDR_DEVINFO_VYPER - HDR_DEVINFO_BEGIN; 488 const suunto_common_layout_t *layout = &suunto_vyper_layout; 489 if (header[hoffset] == 20 || header[hoffset] == 30 || header[hoffset] == 60) { 490 hoffset = HDR_DEVINFO_SPYDER - HDR_DEVINFO_BEGIN; 491 layout = &suunto_spyder_layout; 492 } 493 494 // Update and emit a progress event. 495 progress.maximum = sizeof (header) + 496 (layout->rb_profile_end - layout->rb_profile_begin); 497 progress.current += sizeof (header); 498 device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); 499 500 // Emit a device info event. 501 dc_event_devinfo_t devinfo; 502 devinfo.model = header[hoffset + 0]; 503 devinfo.firmware = header[hoffset + 1]; 504 devinfo.serial = array_convert_bin2dec (header + hoffset + 2, 4); 505 device_event_emit (abstract, DC_EVENT_DEVINFO, &devinfo); 506 507 // Allocate a memory buffer. 508 dc_buffer_t *buffer = dc_buffer_new (layout->rb_profile_end - layout->rb_profile_begin); 509 if (buffer == NULL) 510 return DC_STATUS_NOMEMORY; 511 512 unsigned int ndives = 0; 513 unsigned int remaining = layout->rb_profile_end - layout->rb_profile_begin; 514 while ((rc = suunto_vyper_read_dive (abstract, buffer, (ndives == 0), &progress)) == DC_STATUS_SUCCESS) { 515 unsigned char *data = dc_buffer_get_data (buffer); 516 unsigned int size = dc_buffer_get_size (buffer); 517 518 if (size > remaining) { 519 ERROR (abstract->context, "Unexpected number of bytes received."); 520 dc_buffer_free (buffer); 521 return DC_STATUS_DATAFORMAT; 522 } 523 524 if (size == 0) { 525 dc_buffer_free (buffer); 526 return DC_STATUS_SUCCESS; 527 } 528 529 if (memcmp (data + layout->fp_offset, device->fingerprint, sizeof (device->fingerprint)) == 0) { 530 dc_buffer_free (buffer); 531 return DC_STATUS_SUCCESS; 532 } 533 534 if (callback && !callback (data, size, data + layout->fp_offset, sizeof (device->fingerprint), userdata)) { 535 dc_buffer_free (buffer); 536 return DC_STATUS_SUCCESS; 537 } 538 539 remaining -= size; 540 ndives++; 541 } 542 543 dc_buffer_free (buffer); 544 545 return rc; 546 }