uwatec_smart.c (21674B)
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 <stdlib.h> // malloc, free 23 #include <string.h> // strncmp, strstr 24 25 #include "uwatec_smart.h" 26 #include "context-private.h" 27 #include "device-private.h" 28 #include "checksum.h" 29 #include "platform.h" 30 #include "array.h" 31 32 #define ISINSTANCE(device) dc_device_isinstance((device), &uwatec_smart_device_vtable) 33 34 #define DATASIZE_RX 255 35 #define DATASIZE_TX 254 36 #define MAX_PACKETSIZE 256 37 #define PACKETSIZE_USBHID_RX 64 38 #define PACKETSIZE_USBHID_TX 32 39 40 #define CMD_MODEL 0x10 41 #define CMD_HARDWARE 0x11 42 #define CMD_SOFTWARE 0x13 43 #define CMD_SERIAL 0x14 44 #define CMD_DEVTIME 0x1A 45 #define CMD_HANDSHAKE1 0x1B 46 #define CMD_HANDSHAKE2 0x1C 47 #define CMD_DATA 0xC4 48 #define CMD_SIZE 0xC6 49 50 #define OK 0x01 51 #define ACK 0x11 52 #define NAK 0x66 53 54 typedef struct uwatec_smart_device_t uwatec_smart_device_t; 55 56 typedef dc_status_t (*uwatec_smart_receive_t) (uwatec_smart_device_t *device, dc_event_progress_t *progress, unsigned char cmd, unsigned char data[], size_t size); 57 typedef dc_status_t (*uwatec_smart_send_t) (uwatec_smart_device_t *device, unsigned char cmd, const unsigned char data[], size_t size); 58 59 struct uwatec_smart_device_t { 60 dc_device_t base; 61 dc_iostream_t *iostream; 62 uwatec_smart_send_t send; 63 uwatec_smart_receive_t receive; 64 unsigned int timestamp; 65 unsigned int devtime; 66 dc_ticks_t systime; 67 }; 68 69 static dc_status_t uwatec_smart_device_set_fingerprint (dc_device_t *device, const unsigned char data[], unsigned int size); 70 static dc_status_t uwatec_smart_device_dump (dc_device_t *abstract, dc_buffer_t *buffer); 71 static dc_status_t uwatec_smart_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata); 72 73 static const dc_device_vtable_t uwatec_smart_device_vtable = { 74 sizeof(uwatec_smart_device_t), 75 DC_FAMILY_UWATEC_SMART, 76 uwatec_smart_device_set_fingerprint, /* set_fingerprint */ 77 NULL, /* read */ 78 NULL, /* write */ 79 uwatec_smart_device_dump, /* dump */ 80 uwatec_smart_device_foreach, /* foreach */ 81 NULL, /* timesync */ 82 NULL /* close */ 83 }; 84 85 static dc_status_t 86 uwatec_smart_extract_dives (dc_device_t *device, const unsigned char data[], unsigned int size, dc_dive_callback_t callback, void *userdata); 87 88 static dc_status_t 89 uwatec_smart_irda_send (uwatec_smart_device_t *device, unsigned char cmd, const unsigned char data[], size_t size) 90 { 91 dc_status_t rc = DC_STATUS_SUCCESS; 92 dc_device_t *abstract = (dc_device_t *) device; 93 94 if (size > DATASIZE_TX) { 95 ERROR (abstract->context, "Command too large (" DC_PRINTF_SIZE ").", size); 96 return DC_STATUS_PROTOCOL; 97 } 98 99 // Build the packet. 100 unsigned char packet[1 + DATASIZE_TX] = { 101 cmd}; 102 if (size) { 103 memcpy (packet + 1, data, size); 104 } 105 106 // Send the packet. 107 rc = dc_iostream_write (device->iostream, packet, size + 1, NULL); 108 if (rc != DC_STATUS_SUCCESS) { 109 ERROR (abstract->context, "Failed to send the data packet."); 110 return rc; 111 } 112 113 return DC_STATUS_SUCCESS; 114 } 115 116 static dc_status_t 117 uwatec_smart_irda_receive (uwatec_smart_device_t *device, dc_event_progress_t *progress, unsigned char cmd, unsigned char data[], size_t size) 118 { 119 dc_status_t rc = DC_STATUS_SUCCESS; 120 dc_device_t *abstract = (dc_device_t *) device; 121 122 size_t nbytes = 0; 123 while (nbytes < size) { 124 // Set the minimum packet size. 125 size_t len = 32; 126 127 // Increase the packet size if more data is immediately available. 128 size_t available = 0; 129 rc = dc_iostream_get_available (device->iostream, &available); 130 if (rc == DC_STATUS_SUCCESS && available > len) 131 len = available; 132 133 // Limit the packet size to the total size. 134 if (nbytes + len > size) 135 len = size - nbytes; 136 137 rc = dc_iostream_read (device->iostream, data + nbytes, len, NULL); 138 if (rc != DC_STATUS_SUCCESS) { 139 ERROR (abstract->context, "Failed to receive the data packet."); 140 return rc; 141 } 142 143 // Update and emit a progress event. 144 if (progress) { 145 progress->current += len; 146 device_event_emit (abstract, DC_EVENT_PROGRESS, progress); 147 } 148 149 nbytes += len; 150 } 151 152 return DC_STATUS_SUCCESS; 153 } 154 155 static dc_status_t 156 uwatec_smart_serial_send (uwatec_smart_device_t *device, unsigned char cmd, const unsigned char data[], size_t size) 157 { 158 dc_status_t status = DC_STATUS_SUCCESS; 159 dc_device_t *abstract = (dc_device_t *) device; 160 161 if (size > DATASIZE_TX) { 162 ERROR (abstract->context, "Command too large (" DC_PRINTF_SIZE ").", size); 163 return DC_STATUS_PROTOCOL; 164 } 165 166 // Build the packet. 167 unsigned char packet[12 + DATASIZE_TX + 1] = { 168 0xFF, 0xFF, 0xFF, 169 0xA6, 0x59, 0xBD, 0xC2, 170 size + 1, 171 0x00, 0x00, 0x00, 172 cmd}; 173 if (size) { 174 memcpy (packet + 12, data, size); 175 } 176 packet[12 + size] = checksum_xor_uint8 (packet + 7, size + 5, 0x00); 177 178 // Send the packet. 179 status = dc_iostream_write (device->iostream, packet, size + 13, NULL); 180 if (status != DC_STATUS_SUCCESS) { 181 ERROR (abstract->context, "Failed to send the command."); 182 return status; 183 } 184 185 // Read the echo and the ACK byte. 186 unsigned char echo[sizeof(packet) + 1]; 187 status = dc_iostream_read (device->iostream, echo, size + 13 + 1, NULL); 188 if (status != DC_STATUS_SUCCESS) { 189 ERROR (abstract->context, "Failed to receive the echo."); 190 return status; 191 } 192 193 // Verify the echo. 194 if (memcmp (echo, packet, size + 13) != 0) { 195 WARNING (abstract->context, "Unexpected echo."); 196 return DC_STATUS_PROTOCOL; 197 } 198 199 // Verify the ACK byte. 200 unsigned char ack = echo[size + 13]; 201 if (ack != ACK) { 202 WARNING (abstract->context, "Unexpected ACK byte (%02x).", ack); 203 return DC_STATUS_PROTOCOL; 204 } 205 206 return DC_STATUS_SUCCESS; 207 } 208 209 static dc_status_t 210 uwatec_smart_serial_receive (uwatec_smart_device_t *device, dc_event_progress_t *progress, unsigned char cmd, unsigned char data[], size_t size) 211 { 212 dc_status_t status = DC_STATUS_SUCCESS; 213 dc_device_t *abstract = (dc_device_t *) device; 214 215 size_t nbytes = 0; 216 while (nbytes < size) { 217 // Read the header. 218 unsigned char header[5]; 219 status = dc_iostream_read (device->iostream, header, sizeof (header), NULL); 220 if (status != DC_STATUS_SUCCESS) { 221 ERROR (abstract->context, "Failed to receive the header."); 222 return status; 223 } 224 225 // Get the packet size. 226 unsigned int len = array_uint32_le (header); 227 if (len < 1 || nbytes + len - 1 > size) { 228 WARNING (abstract->context, "Unexpected header size (%u).", len); 229 return DC_STATUS_PROTOCOL; 230 } 231 232 // Verify the command byte. 233 unsigned char rsp = header[4]; 234 if (rsp != cmd) { 235 ERROR (abstract->context, "Unexpected header command byte (%02x).", rsp); 236 return DC_STATUS_PROTOCOL; 237 } 238 239 // Read the packet data. 240 status = dc_iostream_read (device->iostream, data + nbytes, len - 1, NULL); 241 if (status != DC_STATUS_SUCCESS) { 242 ERROR (abstract->context, "Failed to receive the packet."); 243 return status; 244 } 245 246 // Read the checksum. 247 unsigned char csum = 0x00; 248 status = dc_iostream_read (device->iostream, &csum, sizeof (csum), NULL); 249 if (status != DC_STATUS_SUCCESS) { 250 ERROR (abstract->context, "Failed to receive the checksum."); 251 return status; 252 } 253 254 // Verify the checksum. 255 unsigned char ccsum = 0x00; 256 ccsum = checksum_xor_uint8 (header, sizeof (header), ccsum); 257 ccsum = checksum_xor_uint8 (data + nbytes, len - 1, ccsum); 258 if (csum != ccsum) { 259 ERROR (abstract->context, "Unexpected answer checksum."); 260 return DC_STATUS_PROTOCOL; 261 } 262 263 // Update and emit a progress event. 264 if (progress) { 265 progress->current += len - 1; 266 device_event_emit (abstract, DC_EVENT_PROGRESS, progress); 267 } 268 269 nbytes += len - 1; 270 } 271 272 return DC_STATUS_SUCCESS; 273 } 274 275 static dc_status_t 276 uwatec_smart_usbhid_send (uwatec_smart_device_t *device, unsigned char cmd, const unsigned char data[], size_t size) 277 { 278 dc_status_t rc = DC_STATUS_SUCCESS; 279 dc_device_t *abstract = (dc_device_t *) device; 280 dc_transport_t transport = dc_iostream_get_transport(device->iostream); 281 unsigned char buf[DATASIZE_TX + 3]; 282 283 size_t packetsize = transport == DC_TRANSPORT_USBHID ? 284 PACKETSIZE_USBHID_TX + 1 : sizeof(buf); 285 286 if (size > DATASIZE_TX || size + 3 > packetsize) { 287 ERROR (abstract->context, "Command too large (" DC_PRINTF_SIZE ").", size); 288 return DC_STATUS_INVALIDARGS; 289 } 290 291 buf[0] = 0; 292 buf[1] = size + 1; 293 buf[2] = cmd; 294 if (size) { 295 memcpy(buf + 3, data, size); 296 } 297 memset(buf + 3 + size, 0, sizeof(buf) - (size + 3)); 298 299 HEXDUMP (abstract->context, DC_LOGLEVEL_DEBUG, "cmd", buf + 2, size + 1); 300 301 if (dc_iostream_get_transport(device->iostream) == DC_TRANSPORT_BLE) { 302 rc = dc_iostream_write(device->iostream, buf + 1, size + 2, NULL); 303 } else { 304 rc = dc_iostream_write(device->iostream, buf, packetsize, NULL); 305 } 306 if (rc != DC_STATUS_SUCCESS) { 307 ERROR (abstract->context, "Failed to send the command."); 308 return rc; 309 } 310 311 return DC_STATUS_SUCCESS; 312 } 313 314 static dc_status_t 315 uwatec_smart_usbhid_receive (uwatec_smart_device_t *device, dc_event_progress_t *progress, unsigned char cmd, unsigned char data[], size_t size) 316 { 317 dc_status_t rc = DC_STATUS_SUCCESS; 318 dc_device_t *abstract = (dc_device_t *) device; 319 dc_transport_t transport = dc_iostream_get_transport(device->iostream); 320 unsigned char buf[DATASIZE_RX + 1]; 321 322 size_t packetsize = transport == DC_TRANSPORT_USBHID ? 323 PACKETSIZE_USBHID_RX : sizeof(buf); 324 325 size_t nbytes = 0; 326 while (nbytes < size) { 327 size_t transferred = 0; 328 rc = dc_iostream_read (device->iostream, buf, packetsize, &transferred); 329 if (rc != DC_STATUS_SUCCESS) { 330 ERROR (abstract->context, "Failed to receive the packet."); 331 return rc; 332 } 333 334 if (transferred < 1) { 335 ERROR (abstract->context, "Invalid packet length (" DC_PRINTF_SIZE ").", transferred); 336 return DC_STATUS_PROTOCOL; 337 } 338 339 /* 340 * Something changed in the G2 firmware between versions 1.2 and 1.4. 341 * 342 * The first byte of a packet always used to be the length of the 343 * packet data. That's still true for simple single-packet replies, 344 * but multi-packet replies seem to have some other data in it, at 345 * least for BLE. 346 * 347 * The new pattern *seems* to be: 348 * 349 * - simple one-packet reply: the byte remains the size of the reply 350 * 351 * - otherwise, it's an endlessly repeating sequence of 352 * 353 * 0xf7 247 354 * 0x14 20 355 * 0x27 39 356 * 0x3a 58 357 * 0x4d 77 358 * 0x60 96 359 * 0x73 115 360 * 0x86 134 361 * 0x99 153 362 * 0xac 172 363 * 0xbf 191 364 * 0xd2 210 365 * 0xe5 229 366 * 0xf7 247 367 * .. repeats .. 368 * 369 * which is basically "increase by 19" except for that last one (229->247 370 * is an increase by 18). 371 * 372 * The number 19 is the real payload size for BLE GATT (20 bytes minus the 373 * one-byte magic size-that-isn't-size-any-more-byte). 374 * 375 * It may be just an oddly implemented sequence number. Whatever. 376 */ 377 unsigned int len = transferred - 1; 378 if (transport == DC_TRANSPORT_USBHID) { 379 if (len > buf[0]) 380 len = buf[0]; 381 } 382 383 HEXDUMP (abstract->context, DC_LOGLEVEL_DEBUG, "rcv", buf + 1, len); 384 385 if (len > size) { 386 ERROR (abstract->context, "Insufficient buffer space available."); 387 return DC_STATUS_PROTOCOL; 388 } 389 390 // Update and emit a progress event. 391 if (progress) { 392 progress->current += len; 393 device_event_emit (abstract, DC_EVENT_PROGRESS, progress); 394 } 395 396 memcpy(data + nbytes, buf + 1, len); 397 nbytes += len; 398 } 399 400 return DC_STATUS_SUCCESS; 401 } 402 403 404 static dc_status_t 405 uwatec_smart_transfer (uwatec_smart_device_t *device, unsigned char cmd, const unsigned char command[], unsigned int csize, unsigned char answer[], unsigned int asize) 406 { 407 dc_status_t status = DC_STATUS_SUCCESS; 408 dc_device_t *abstract = (dc_device_t *) device; 409 410 status = device->send (device, cmd, command, csize); 411 if (status != DC_STATUS_SUCCESS) { 412 ERROR (abstract->context, "Failed to send the command."); 413 return status; 414 } 415 416 status = device->receive (device, NULL, cmd, answer, asize); 417 if (status != DC_STATUS_SUCCESS) { 418 ERROR (abstract->context, "Failed to receive the answer."); 419 return status; 420 } 421 422 return DC_STATUS_SUCCESS; 423 } 424 425 426 static dc_status_t 427 uwatec_smart_handshake (uwatec_smart_device_t *device) 428 { 429 dc_device_t *abstract = (dc_device_t *) device; 430 const unsigned char params[] = {0x10, 0x27, 0, 0}; 431 unsigned char answer[1] = {0}; 432 433 // Skip the handshake for BLE communication. 434 if (dc_iostream_get_transport (device->iostream) == DC_TRANSPORT_BLE) 435 return DC_STATUS_SUCCESS; 436 437 // Handshake (stage 1). 438 dc_status_t rc = uwatec_smart_transfer (device, CMD_HANDSHAKE1, NULL, 0, answer, sizeof(answer)); 439 if (rc != DC_STATUS_SUCCESS) 440 return rc; 441 442 // Verify the answer. 443 if (answer[0] != OK) { 444 ERROR (abstract->context, "Unexpected answer byte(s)."); 445 return DC_STATUS_PROTOCOL; 446 } 447 448 // Handshake (stage 2). 449 rc = uwatec_smart_transfer (device, CMD_HANDSHAKE2, params, sizeof(params), answer, sizeof(answer)); 450 if (rc != DC_STATUS_SUCCESS) 451 return rc; 452 453 // Verify the answer. 454 if (answer[0] != OK) { 455 ERROR (abstract->context, "Unexpected answer byte(s)."); 456 return DC_STATUS_PROTOCOL; 457 } 458 459 return DC_STATUS_SUCCESS; 460 } 461 462 463 dc_status_t 464 uwatec_smart_device_open (dc_device_t **out, dc_context_t *context, dc_iostream_t *iostream) 465 { 466 dc_status_t status = DC_STATUS_SUCCESS; 467 uwatec_smart_device_t *device = NULL; 468 469 if (out == NULL) 470 return DC_STATUS_INVALIDARGS; 471 472 // Allocate memory. 473 device = (uwatec_smart_device_t *) dc_device_allocate (context, &uwatec_smart_device_vtable); 474 if (device == NULL) { 475 ERROR (context, "Failed to allocate memory."); 476 return DC_STATUS_NOMEMORY; 477 } 478 479 // Set the default values. 480 device->iostream = iostream; 481 device->timestamp = 0; 482 device->systime = (dc_ticks_t) -1; 483 device->devtime = 0; 484 485 // Set the serial communication protocol (57600 8N1). 486 status = dc_iostream_configure (device->iostream, 57600, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE); 487 if (status != DC_STATUS_SUCCESS) { 488 ERROR (context, "Failed to set the terminal attributes."); 489 goto error_free; 490 } 491 492 // Set the timeout for receiving data (5000ms). 493 status = dc_iostream_set_timeout (device->iostream, 5000); 494 if (status != DC_STATUS_SUCCESS) { 495 ERROR (context, "Failed to set the timeout."); 496 goto error_free; 497 } 498 499 // Make sure everything is in a sane state. 500 dc_iostream_purge (device->iostream, DC_DIRECTION_ALL); 501 502 // Select the correct send/receive function. 503 dc_transport_t transport = dc_iostream_get_transport(iostream); 504 switch (transport) { 505 case DC_TRANSPORT_IRDA: 506 device->send = uwatec_smart_irda_send; 507 device->receive = uwatec_smart_irda_receive; 508 break; 509 case DC_TRANSPORT_SERIAL: 510 device->send = uwatec_smart_serial_send; 511 device->receive = uwatec_smart_serial_receive; 512 break; 513 case DC_TRANSPORT_USBHID: 514 case DC_TRANSPORT_BLE: 515 device->send = uwatec_smart_usbhid_send; 516 device->receive = uwatec_smart_usbhid_receive; 517 break; 518 default: 519 ERROR (context, "Unsupported transport type (%u).", transport); 520 status = DC_STATUS_UNSUPPORTED; 521 goto error_free; 522 } 523 524 // Perform the handshaking. 525 status = uwatec_smart_handshake (device); 526 if (status != DC_STATUS_SUCCESS) { 527 ERROR (context, "Failed to handshake with the device."); 528 goto error_free; 529 } 530 531 *out = (dc_device_t*) device; 532 533 return DC_STATUS_SUCCESS; 534 535 error_free: 536 dc_device_deallocate ((dc_device_t *) device); 537 return status; 538 } 539 540 541 static dc_status_t 542 uwatec_smart_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size) 543 { 544 uwatec_smart_device_t *device = (uwatec_smart_device_t*) abstract; 545 546 if (size && size != 4) 547 return DC_STATUS_INVALIDARGS; 548 549 if (size) 550 device->timestamp = array_uint32_le (data); 551 else 552 device->timestamp = 0; 553 554 return DC_STATUS_SUCCESS; 555 } 556 557 558 static dc_status_t 559 uwatec_smart_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) 560 { 561 uwatec_smart_device_t *device = (uwatec_smart_device_t*) abstract; 562 dc_status_t rc = DC_STATUS_SUCCESS; 563 564 // Enable progress notifications. 565 dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER; 566 device_event_emit (&device->base, DC_EVENT_PROGRESS, &progress); 567 568 // Read the model number. 569 unsigned char model[1] = {0}; 570 rc = uwatec_smart_transfer (device, CMD_MODEL, NULL, 0, model, sizeof (model)); 571 if (rc != DC_STATUS_SUCCESS) 572 return rc; 573 574 HEXDUMP (abstract->context, DC_LOGLEVEL_DEBUG, "Model", model, sizeof (model)); 575 576 // Read the hardware version. 577 unsigned char hardware[1] = {0}; 578 rc = uwatec_smart_transfer (device, CMD_HARDWARE, NULL, 0, hardware, sizeof (hardware)); 579 if (rc != DC_STATUS_SUCCESS) 580 return rc; 581 582 HEXDUMP (abstract->context, DC_LOGLEVEL_DEBUG, "Hardware", hardware, sizeof (hardware)); 583 584 // Read the software version. 585 unsigned char software[1] = {0}; 586 rc = uwatec_smart_transfer (device, CMD_SOFTWARE, NULL, 0, software, sizeof (software)); 587 if (rc != DC_STATUS_SUCCESS) 588 return rc; 589 590 HEXDUMP (abstract->context, DC_LOGLEVEL_DEBUG, "Software", software, sizeof (software)); 591 592 // Read the serial number. 593 unsigned char serial[4] = {0}; 594 rc = uwatec_smart_transfer (device, CMD_SERIAL, NULL, 0, serial, sizeof (serial)); 595 if (rc != DC_STATUS_SUCCESS) 596 return rc; 597 598 HEXDUMP (abstract->context, DC_LOGLEVEL_DEBUG, "Serial", serial, sizeof (serial)); 599 600 // Read the device clock. 601 unsigned char devtime[4] = {0}; 602 rc = uwatec_smart_transfer (device, CMD_DEVTIME, NULL, 0, devtime, sizeof (devtime)); 603 if (rc != DC_STATUS_SUCCESS) 604 return rc; 605 606 HEXDUMP (abstract->context, DC_LOGLEVEL_DEBUG, "Clock", devtime, sizeof (devtime)); 607 608 // Store the clock calibration values. 609 device->systime = dc_datetime_now (); 610 device->devtime = array_uint32_le (devtime); 611 612 // Update and emit a progress event. 613 progress.current += 11; 614 device_event_emit (&device->base, DC_EVENT_PROGRESS, &progress); 615 616 // Emit a clock event. 617 dc_event_clock_t clock; 618 clock.systime = device->systime; 619 clock.devtime = device->devtime; 620 device_event_emit (&device->base, DC_EVENT_CLOCK, &clock); 621 622 // Emit a device info event. 623 dc_event_devinfo_t devinfo; 624 devinfo.model = model[0]; 625 devinfo.firmware = bcd2dec (software[0]); 626 devinfo.serial = array_uint32_le (serial); 627 device_event_emit (&device->base, DC_EVENT_DEVINFO, &devinfo); 628 629 // Command parameters. 630 const unsigned char params[] = { 631 (device->timestamp ) & 0xFF, 632 (device->timestamp >> 8 ) & 0xFF, 633 (device->timestamp >> 16) & 0xFF, 634 (device->timestamp >> 24) & 0xFF, 635 0x10, 636 0x27, 637 0, 638 0}; 639 640 // Data Length. 641 unsigned char answer[4] = {0}; 642 rc = uwatec_smart_transfer (device, CMD_SIZE, params, sizeof (params), answer, sizeof (answer)); 643 if (rc != DC_STATUS_SUCCESS) 644 return rc; 645 646 unsigned int length = array_uint32_le (answer); 647 648 // Update and emit a progress event. 649 progress.maximum = 4 + 11 + (length ? length + 4 : 0); 650 progress.current += 4; 651 device_event_emit (&device->base, DC_EVENT_PROGRESS, &progress); 652 653 if (length == 0) 654 return DC_STATUS_SUCCESS; 655 656 // Allocate the required amount of memory. 657 if (!dc_buffer_resize (buffer, length)) { 658 ERROR (abstract->context, "Insufficient buffer space available."); 659 return DC_STATUS_NOMEMORY; 660 } 661 662 unsigned char *data = dc_buffer_get_data (buffer); 663 664 // Data. 665 rc = uwatec_smart_transfer (device, CMD_DATA, params, sizeof (params), answer, sizeof (answer)); 666 if (rc != DC_STATUS_SUCCESS) 667 return rc; 668 669 // Update and emit a progress event. 670 progress.current += 4; 671 device_event_emit (&device->base, DC_EVENT_PROGRESS, &progress); 672 673 unsigned int total = array_uint32_le (answer); 674 if (total != length + 4) { 675 ERROR (abstract->context, "Received an unexpected size."); 676 return DC_STATUS_PROTOCOL; 677 } 678 679 rc = device->receive (device, &progress, CMD_DATA, data, length); 680 if (rc != DC_STATUS_SUCCESS) { 681 ERROR (abstract->context, "Failed to receive the answer."); 682 return rc; 683 } 684 685 return DC_STATUS_SUCCESS; 686 } 687 688 689 static dc_status_t 690 uwatec_smart_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata) 691 { 692 dc_buffer_t *buffer = dc_buffer_new (0); 693 if (buffer == NULL) 694 return DC_STATUS_NOMEMORY; 695 696 dc_status_t rc = uwatec_smart_device_dump (abstract, buffer); 697 if (rc != DC_STATUS_SUCCESS) { 698 dc_buffer_free (buffer); 699 return rc; 700 } 701 702 rc = uwatec_smart_extract_dives (abstract, 703 dc_buffer_get_data (buffer), dc_buffer_get_size (buffer), callback, userdata); 704 705 dc_buffer_free (buffer); 706 707 return rc; 708 } 709 710 711 static dc_status_t 712 uwatec_smart_extract_dives (dc_device_t *abstract, const unsigned char data[], unsigned int size, dc_dive_callback_t callback, void *userdata) 713 { 714 if (abstract && !ISINSTANCE (abstract)) 715 return DC_STATUS_INVALIDARGS; 716 717 const unsigned char header[4] = {0xa5, 0xa5, 0x5a, 0x5a}; 718 719 // Search the data stream for start markers. 720 unsigned int previous = size; 721 unsigned int current = (size >= 4 ? size - 4 : 0); 722 while (current > 0) { 723 current--; 724 if (memcmp (data + current, header, sizeof (header)) == 0) { 725 // Get the length of the profile data. 726 unsigned int len = array_uint32_le (data + current + 4); 727 728 // Check for a buffer overflow. 729 if (current + len > previous) 730 return DC_STATUS_DATAFORMAT; 731 732 if (callback && !callback (data + current, len, data + current + 8, 4, userdata)) 733 return DC_STATUS_SUCCESS; 734 735 // Prepare for the next dive. 736 previous = current; 737 current = (current >= 4 ? current - 4 : 0); 738 } 739 } 740 741 return DC_STATUS_SUCCESS; 742 }