oceanic_veo250.c (14316B)
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> // memcpy 23 #include <stdlib.h> // malloc, free 24 25 #include "oceanic_veo250.h" 26 #include "oceanic_common.h" 27 #include "context-private.h" 28 #include "device-private.h" 29 #include "ringbuffer.h" 30 #include "checksum.h" 31 32 #define ISINSTANCE(device) dc_device_isinstance((device), &oceanic_veo250_device_vtable.base) 33 34 #define MAXRETRIES 2 35 #define MULTIPAGE 4 36 37 #define ACK 0x5A 38 #define NAK 0xA5 39 40 typedef struct oceanic_veo250_device_t { 41 oceanic_common_device_t base; 42 dc_iostream_t *iostream; 43 unsigned int last; 44 } oceanic_veo250_device_t; 45 46 static dc_status_t oceanic_veo250_device_read (dc_device_t *abstract, unsigned int address, unsigned char data[], unsigned int size); 47 static dc_status_t oceanic_veo250_device_close (dc_device_t *abstract); 48 49 static const oceanic_common_device_vtable_t oceanic_veo250_device_vtable = { 50 { 51 sizeof(oceanic_veo250_device_t), 52 DC_FAMILY_OCEANIC_VEO250, 53 oceanic_common_device_set_fingerprint, /* set_fingerprint */ 54 oceanic_veo250_device_read, /* read */ 55 NULL, /* write */ 56 oceanic_common_device_dump, /* dump */ 57 oceanic_common_device_foreach, /* foreach */ 58 NULL, /* timesync */ 59 oceanic_veo250_device_close /* close */ 60 }, 61 oceanic_common_device_devinfo, 62 oceanic_common_device_pointers, 63 oceanic_common_device_logbook, 64 oceanic_common_device_profile, 65 }; 66 67 static const oceanic_common_layout_t oceanic_veo250_layout = { 68 0x8000, /* memsize */ 69 0, /* highmem */ 70 0x0000, /* cf_devinfo */ 71 0x0040, /* cf_pointers */ 72 0x0400, /* rb_logbook_begin */ 73 0x0600, /* rb_logbook_end */ 74 8, /* rb_logbook_entry_size */ 75 1, /* rb_logbook_direction */ 76 0x0600, /* rb_profile_begin */ 77 0x8000, /* rb_profile_end */ 78 1, /* pt_mode_global */ 79 1, /* pt_mode_logbook */ 80 1, /* pt_mode_serial */ 81 }; 82 83 static const oceanic_common_version_t versions[] = { 84 {"GENREACT \0\0 256K", 0, REACTPRO, &oceanic_veo250_layout}, 85 {"VEO 200 R\0\0 256K", 0, VEO200, &oceanic_veo250_layout}, 86 {"VEO 250 R\0\0 256K", 0, VEO250, &oceanic_veo250_layout}, 87 {"SEEMANN R\0\0 256K", 0, XP5, &oceanic_veo250_layout}, 88 {"VEO 180 R\0\0 256K", 0, VEO180, &oceanic_veo250_layout}, 89 {"AERISXR2 \0\0 256K", 0, XR2, &oceanic_veo250_layout}, 90 {"INSIGHT R\0\0 256K", 0, INSIGHT, &oceanic_veo250_layout}, 91 {"HO DGO2 R\0\0 256K", 0, DG02, &oceanic_veo250_layout}, 92 }; 93 94 static dc_status_t 95 oceanic_veo250_send (oceanic_veo250_device_t *device, const unsigned char command[], unsigned int csize) 96 { 97 dc_status_t status = DC_STATUS_SUCCESS; 98 dc_device_t *abstract = (dc_device_t *) device; 99 100 if (device_is_cancelled (abstract)) 101 return DC_STATUS_CANCELLED; 102 103 // Discard garbage bytes. 104 dc_iostream_purge (device->iostream, DC_DIRECTION_INPUT); 105 106 // Send the command to the dive computer. 107 status = dc_iostream_write (device->iostream, command, csize, NULL); 108 if (status != DC_STATUS_SUCCESS) { 109 ERROR (abstract->context, "Failed to send the command."); 110 return status; 111 } 112 113 // Receive the response (ACK/NAK) of the dive computer. 114 unsigned char response = NAK; 115 status = dc_iostream_read (device->iostream, &response, 1, NULL); 116 if (status != DC_STATUS_SUCCESS) { 117 ERROR (abstract->context, "Failed to receive the answer."); 118 return status; 119 } 120 121 // Verify the response of the dive computer. 122 if (response != ACK) { 123 ERROR (abstract->context, "Unexpected answer start byte(s)."); 124 return DC_STATUS_PROTOCOL; 125 } 126 127 return DC_STATUS_SUCCESS; 128 } 129 130 131 static dc_status_t 132 oceanic_veo250_transfer (oceanic_veo250_device_t *device, const unsigned char command[], unsigned int csize, unsigned char answer[], unsigned int asize) 133 { 134 dc_status_t status = DC_STATUS_SUCCESS; 135 dc_device_t *abstract = (dc_device_t *) device; 136 137 // Send the command to the device. If the device responds with an 138 // ACK byte, the command was received successfully and the answer 139 // (if any) follows after the ACK byte. If the device responds with 140 // a NAK byte, we try to resend the command a number of times before 141 // returning an error. 142 143 unsigned int nretries = 0; 144 dc_status_t rc = DC_STATUS_SUCCESS; 145 while ((rc = oceanic_veo250_send (device, command, csize)) != DC_STATUS_SUCCESS) { 146 if (rc != DC_STATUS_TIMEOUT && rc != DC_STATUS_PROTOCOL) 147 return rc; 148 149 // Abort if the maximum number of retries is reached. 150 if (nretries++ >= MAXRETRIES) 151 return rc; 152 153 // Delay the next attempt. 154 dc_iostream_sleep (device->iostream, 100); 155 } 156 157 // Receive the answer of the dive computer. 158 status = dc_iostream_read (device->iostream, answer, asize, NULL); 159 if (status != DC_STATUS_SUCCESS) { 160 ERROR (abstract->context, "Failed to receive the answer."); 161 return status; 162 } 163 164 // Verify the last byte of the answer. 165 if (answer[asize - 1] != NAK) { 166 ERROR (abstract->context, "Unexpected answer byte."); 167 return DC_STATUS_PROTOCOL; 168 } 169 170 return DC_STATUS_SUCCESS; 171 } 172 173 174 static dc_status_t 175 oceanic_veo250_init (oceanic_veo250_device_t *device) 176 { 177 dc_status_t status = DC_STATUS_SUCCESS; 178 dc_device_t *abstract = (dc_device_t *) device; 179 180 // Send the command to the dive computer. 181 unsigned char command[2] = {0x55, 0x00}; 182 status = dc_iostream_write (device->iostream, command, sizeof (command), NULL); 183 if (status != DC_STATUS_SUCCESS) { 184 ERROR (abstract->context, "Failed to send the command."); 185 return status; 186 } 187 188 // Receive the answer of the dive computer. 189 size_t n = 0; 190 unsigned char answer[13] = {0}; 191 status = dc_iostream_read (device->iostream, answer, sizeof (answer), &n); 192 if (status != DC_STATUS_SUCCESS) { 193 ERROR (abstract->context, "Failed to receive the answer."); 194 if (n == 0) 195 return DC_STATUS_SUCCESS; 196 return status; 197 } 198 199 // Verify the answer. 200 const unsigned char response[13] = { 201 0x50, 0x50, 0x53, 0x2D, 0x2D, 0x4F, 0x4B, 202 0x5F, 0x56, 0x32, 0x2E, 0x30, 0x30}; 203 if (memcmp (answer, response, sizeof (response)) != 0) { 204 ERROR (abstract->context, "Unexpected answer byte(s)."); 205 return DC_STATUS_PROTOCOL; 206 } 207 208 return DC_STATUS_SUCCESS; 209 } 210 211 212 static dc_status_t 213 oceanic_veo250_quit (oceanic_veo250_device_t *device) 214 { 215 dc_status_t status = DC_STATUS_SUCCESS; 216 dc_device_t *abstract = (dc_device_t *) device; 217 218 // Send the command to the dive computer. 219 unsigned char command[2] = {0x98, 0x00}; 220 status = dc_iostream_write (device->iostream, command, sizeof (command), NULL); 221 if (status != DC_STATUS_SUCCESS) { 222 ERROR (abstract->context, "Failed to send the command."); 223 return status; 224 } 225 226 return DC_STATUS_SUCCESS; 227 } 228 229 230 dc_status_t 231 oceanic_veo250_device_open (dc_device_t **out, dc_context_t *context, dc_iostream_t *iostream) 232 { 233 dc_status_t status = DC_STATUS_SUCCESS; 234 oceanic_veo250_device_t *device = NULL; 235 236 if (out == NULL) 237 return DC_STATUS_INVALIDARGS; 238 239 // Allocate memory. 240 device = (oceanic_veo250_device_t *) dc_device_allocate (context, &oceanic_veo250_device_vtable.base); 241 if (device == NULL) { 242 ERROR (context, "Failed to allocate memory."); 243 return DC_STATUS_NOMEMORY; 244 } 245 246 // Initialize the base class. 247 oceanic_common_device_init (&device->base); 248 249 // Override the base class values. 250 device->base.multipage = MULTIPAGE; 251 252 // Set the default values. 253 device->iostream = iostream; 254 device->last = 0; 255 256 // Set the serial communication protocol (9600 8N1). 257 status = dc_iostream_configure (device->iostream, 9600, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE); 258 if (status != DC_STATUS_SUCCESS) { 259 ERROR (context, "Failed to set the terminal attributes."); 260 goto error_free; 261 } 262 263 // Set the timeout for receiving data (3000 ms). 264 status = dc_iostream_set_timeout (device->iostream, 3000); 265 if (status != DC_STATUS_SUCCESS) { 266 ERROR (context, "Failed to set the timeout."); 267 goto error_free; 268 } 269 270 // Set the DTR line. 271 status = dc_iostream_set_dtr (device->iostream, 1); 272 if (status != DC_STATUS_SUCCESS) { 273 ERROR (context, "Failed to set the DTR line."); 274 goto error_free; 275 } 276 277 // Clear the RTS line to reset the PIC inside the data cable as it 278 // may not have have been previously cleared. This ensures that the 279 // PIC will always start in a known state once RTS is set. Starting 280 // in a known default state is very important as the PIC won't 281 // respond to init commands unless it is in a default state. 282 status = dc_iostream_set_rts (device->iostream, 0); 283 if (status != DC_STATUS_SUCCESS) { 284 ERROR (context, "Failed to clear the RTS line."); 285 goto error_free; 286 } 287 288 // Hold RTS clear for a bit to allow PIC to reset. 289 dc_iostream_sleep (device->iostream, 100); 290 291 // Set the RTS line. 292 status = dc_iostream_set_rts (device->iostream, 1); 293 if (status != DC_STATUS_SUCCESS) { 294 ERROR (context, "Failed to set the RTS line."); 295 goto error_free; 296 } 297 298 // Give the interface 100 ms to settle and draw power up. 299 dc_iostream_sleep (device->iostream, 100); 300 301 // Make sure everything is in a sane state. 302 dc_iostream_purge (device->iostream, DC_DIRECTION_ALL); 303 304 // Initialize the data cable (PPS mode). 305 status = oceanic_veo250_init (device); 306 if (status != DC_STATUS_SUCCESS) { 307 goto error_free; 308 } 309 310 // Delay the sending of the version command. 311 dc_iostream_sleep (device->iostream, 100); 312 313 // Switch the device from surface mode into download mode. Before sending 314 // this command, the device needs to be in PC mode (manually activated by 315 // the user), or already in download mode. 316 status = oceanic_veo250_device_version ((dc_device_t *) device, device->base.version, sizeof (device->base.version)); 317 if (status != DC_STATUS_SUCCESS) { 318 goto error_free; 319 } 320 321 HEXDUMP (context, DC_LOGLEVEL_DEBUG, "Version", device->base.version, sizeof (device->base.version)); 322 323 // Detect the memory layout. 324 const oceanic_common_version_t *version = OCEANIC_COMMON_MATCH(device->base.version, versions, &device->base.firmware); 325 if (version == NULL) { 326 WARNING (context, "Unsupported device detected!"); 327 device->base.layout = &oceanic_veo250_layout; 328 device->base.model = 0; 329 } else { 330 device->base.layout = version->layout; 331 device->base.model = version->model; 332 } 333 334 *out = (dc_device_t*) device; 335 336 return DC_STATUS_SUCCESS; 337 338 error_free: 339 dc_device_deallocate ((dc_device_t *) device); 340 return status; 341 } 342 343 344 static dc_status_t 345 oceanic_veo250_device_close (dc_device_t *abstract) 346 { 347 dc_status_t status = DC_STATUS_SUCCESS; 348 oceanic_veo250_device_t *device = (oceanic_veo250_device_t*) abstract; 349 dc_status_t rc = DC_STATUS_SUCCESS; 350 351 // Switch the device back to surface mode. 352 rc = oceanic_veo250_quit (device); 353 if (rc != DC_STATUS_SUCCESS) { 354 dc_status_set_error(&status, rc); 355 } 356 357 return status; 358 } 359 360 361 dc_status_t 362 oceanic_veo250_device_keepalive (dc_device_t *abstract) 363 { 364 oceanic_veo250_device_t *device = (oceanic_veo250_device_t*) abstract; 365 366 if (!ISINSTANCE (abstract)) 367 return DC_STATUS_INVALIDARGS; 368 369 unsigned char answer[2] = {0}; 370 unsigned char command[4] = {0x91, 371 (device->last ) & 0xFF, // low 372 (device->last >> 8) & 0xFF, // high 373 0x00}; 374 dc_status_t rc = oceanic_veo250_transfer (device, command, sizeof (command), answer, sizeof (answer)); 375 if (rc != DC_STATUS_SUCCESS) 376 return rc; 377 378 // Verify the answer. 379 if (answer[0] != NAK) { 380 ERROR (abstract->context, "Unexpected answer byte(s)."); 381 return DC_STATUS_PROTOCOL; 382 } 383 384 return DC_STATUS_SUCCESS; 385 } 386 387 388 dc_status_t 389 oceanic_veo250_device_version (dc_device_t *abstract, unsigned char data[], unsigned int size) 390 { 391 oceanic_veo250_device_t *device = (oceanic_veo250_device_t*) abstract; 392 393 if (!ISINSTANCE (abstract)) 394 return DC_STATUS_INVALIDARGS; 395 396 if (size < PAGESIZE) 397 return DC_STATUS_INVALIDARGS; 398 399 unsigned char answer[PAGESIZE + 2] = {0}; 400 unsigned char command[2] = {0x90, 0x00}; 401 dc_status_t rc = oceanic_veo250_transfer (device, command, sizeof (command), answer, sizeof (answer)); 402 if (rc != DC_STATUS_SUCCESS) 403 return rc; 404 405 // Verify the checksum of the answer. 406 unsigned char crc = answer[PAGESIZE]; 407 unsigned char ccrc = checksum_add_uint8 (answer, PAGESIZE, 0x00); 408 if (crc != ccrc) { 409 ERROR (abstract->context, "Unexpected answer checksum."); 410 return DC_STATUS_PROTOCOL; 411 } 412 413 memcpy (data, answer, PAGESIZE); 414 415 return DC_STATUS_SUCCESS; 416 } 417 418 419 static dc_status_t 420 oceanic_veo250_device_read (dc_device_t *abstract, unsigned int address, unsigned char data[], unsigned int size) 421 { 422 oceanic_veo250_device_t *device = (oceanic_veo250_device_t*) abstract; 423 424 if ((address % PAGESIZE != 0) || 425 (size % PAGESIZE != 0)) 426 return DC_STATUS_INVALIDARGS; 427 428 unsigned int nbytes = 0; 429 while (nbytes < size) { 430 // Calculate the number of packages. 431 unsigned int npackets = (size - nbytes) / PAGESIZE; 432 if (npackets > MULTIPAGE) 433 npackets = MULTIPAGE; 434 435 // Read the package. 436 unsigned int first = address / PAGESIZE; 437 unsigned int last = first + npackets - 1; 438 unsigned char answer[(PAGESIZE + 1) * MULTIPAGE + 1] = {0}; 439 unsigned char command[6] = {0x20, 440 (first ) & 0xFF, // low 441 (first >> 8) & 0xFF, // high 442 (last ) & 0xFF, // low 443 (last >> 8) & 0xFF, // high 444 0}; 445 dc_status_t rc = oceanic_veo250_transfer (device, command, sizeof (command), answer, (PAGESIZE + 1) * npackets + 1); 446 if (rc != DC_STATUS_SUCCESS) 447 return rc; 448 449 device->last = last; 450 451 unsigned int offset = 0; 452 for (unsigned int i = 0; i < npackets; ++i) { 453 // Verify the checksum of the answer. 454 unsigned char crc = answer[offset + PAGESIZE]; 455 unsigned char ccrc = checksum_add_uint8 (answer + offset, PAGESIZE, 0x00); 456 if (crc != ccrc) { 457 ERROR (abstract->context, "Unexpected answer checksum."); 458 return DC_STATUS_PROTOCOL; 459 } 460 461 memcpy (data, answer + offset, PAGESIZE); 462 463 offset += PAGESIZE + 1; 464 nbytes += PAGESIZE; 465 address += PAGESIZE; 466 data += PAGESIZE; 467 } 468 } 469 470 return DC_STATUS_SUCCESS; 471 }