reefnet_sensusultra.c (20927B)
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 "reefnet_sensusultra.h" 27 #include "context-private.h" 28 #include "device-private.h" 29 #include "checksum.h" 30 #include "array.h" 31 32 #define ISINSTANCE(device) dc_device_isinstance((device), &reefnet_sensusultra_device_vtable) 33 34 #define SZ_PACKET 512 35 #define SZ_MEMORY 2080768 36 #define SZ_USER 16384 37 #define SZ_HANDSHAKE 24 38 #define SZ_SENSE 6 39 40 #define MAXRETRIES 2 41 #define PROMPT 0xA5 42 #define ACCEPT PROMPT 43 #define REJECT 0x00 44 45 typedef struct reefnet_sensusultra_device_t { 46 dc_device_t base; 47 dc_iostream_t *iostream; 48 unsigned char handshake[SZ_HANDSHAKE]; 49 unsigned int timestamp; 50 unsigned int devtime; 51 dc_ticks_t systime; 52 } reefnet_sensusultra_device_t; 53 54 static dc_status_t reefnet_sensusultra_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size); 55 static dc_status_t reefnet_sensusultra_device_dump (dc_device_t *abstract, dc_buffer_t *buffer); 56 static dc_status_t reefnet_sensusultra_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata); 57 58 static const dc_device_vtable_t reefnet_sensusultra_device_vtable = { 59 sizeof(reefnet_sensusultra_device_t), 60 DC_FAMILY_REEFNET_SENSUSULTRA, 61 reefnet_sensusultra_device_set_fingerprint, /* set_fingerprint */ 62 NULL, /* read */ 63 NULL, /* write */ 64 reefnet_sensusultra_device_dump, /* dump */ 65 reefnet_sensusultra_device_foreach, /* foreach */ 66 NULL, /* timesync */ 67 NULL /* close */ 68 }; 69 70 71 dc_status_t 72 reefnet_sensusultra_device_open (dc_device_t **out, dc_context_t *context, dc_iostream_t *iostream) 73 { 74 dc_status_t status = DC_STATUS_SUCCESS; 75 reefnet_sensusultra_device_t *device = NULL; 76 77 if (out == NULL) 78 return DC_STATUS_INVALIDARGS; 79 80 // Allocate memory. 81 device = (reefnet_sensusultra_device_t *) dc_device_allocate (context, &reefnet_sensusultra_device_vtable); 82 if (device == NULL) { 83 ERROR (context, "Failed to allocate memory."); 84 return DC_STATUS_NOMEMORY; 85 } 86 87 // Set the default values. 88 device->iostream = iostream; 89 device->timestamp = 0; 90 device->systime = (dc_ticks_t) -1; 91 device->devtime = 0; 92 memset (device->handshake, 0, sizeof (device->handshake)); 93 94 // Set the serial communication protocol (115200 8N1). 95 status = dc_iostream_configure (device->iostream, 115200, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE); 96 if (status != DC_STATUS_SUCCESS) { 97 ERROR (context, "Failed to set the terminal attributes."); 98 goto error_free; 99 } 100 101 // Set the timeout for receiving data (3000ms). 102 status = dc_iostream_set_timeout (device->iostream, 3000); 103 if (status != DC_STATUS_SUCCESS) { 104 ERROR (context, "Failed to set the timeout."); 105 goto error_free; 106 } 107 108 // Make sure everything is in a sane state. 109 dc_iostream_purge (device->iostream, DC_DIRECTION_ALL); 110 111 *out = (dc_device_t*) device; 112 113 return DC_STATUS_SUCCESS; 114 115 error_free: 116 dc_device_deallocate ((dc_device_t *) device); 117 return status; 118 } 119 120 121 dc_status_t 122 reefnet_sensusultra_device_get_handshake (dc_device_t *abstract, unsigned char data[], unsigned int size) 123 { 124 reefnet_sensusultra_device_t *device = (reefnet_sensusultra_device_t*) abstract; 125 126 if (!ISINSTANCE (abstract)) 127 return DC_STATUS_INVALIDARGS; 128 129 if (size < SZ_HANDSHAKE) { 130 ERROR (abstract->context, "Insufficient buffer space available."); 131 return DC_STATUS_INVALIDARGS; 132 } 133 134 memcpy (data, device->handshake, SZ_HANDSHAKE); 135 136 return DC_STATUS_SUCCESS; 137 } 138 139 140 static dc_status_t 141 reefnet_sensusultra_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size) 142 { 143 reefnet_sensusultra_device_t *device = (reefnet_sensusultra_device_t*) abstract; 144 145 if (size && size != 4) 146 return DC_STATUS_INVALIDARGS; 147 148 if (size) 149 device->timestamp = array_uint32_le (data); 150 else 151 device->timestamp = 0; 152 153 return DC_STATUS_SUCCESS; 154 } 155 156 157 static dc_status_t 158 reefnet_sensusultra_send_uchar (reefnet_sensusultra_device_t *device, unsigned char value) 159 { 160 dc_status_t status = DC_STATUS_SUCCESS; 161 dc_device_t *abstract = (dc_device_t *) device; 162 163 // Wait for the prompt byte. 164 unsigned char prompt = 0; 165 status = dc_iostream_read (device->iostream, &prompt, 1, NULL); 166 if (status != DC_STATUS_SUCCESS) { 167 ERROR (abstract->context, "Failed to receive the prompt byte"); 168 return status; 169 } 170 171 // Verify the prompt byte. 172 if (prompt != PROMPT) { 173 ERROR (abstract->context, "Unexpected answer data."); 174 return DC_STATUS_PROTOCOL; 175 } 176 177 // Send the value to the device. 178 status = dc_iostream_write (device->iostream, &value, 1, NULL); 179 if (status != DC_STATUS_SUCCESS) { 180 ERROR (abstract->context, "Failed to send the value."); 181 return status; 182 } 183 184 return DC_STATUS_SUCCESS; 185 } 186 187 188 static dc_status_t 189 reefnet_sensusultra_send_ushort (reefnet_sensusultra_device_t *device, unsigned short value) 190 { 191 // Send the least-significant byte. 192 unsigned char lsb = value & 0xFF; 193 dc_status_t rc = reefnet_sensusultra_send_uchar (device, lsb); 194 if (rc != DC_STATUS_SUCCESS) 195 return rc; 196 197 // Send the most-significant byte. 198 unsigned char msb = (value >> 8) & 0xFF; 199 rc = reefnet_sensusultra_send_uchar (device, msb); 200 if (rc != DC_STATUS_SUCCESS) 201 return rc; 202 203 return DC_STATUS_SUCCESS; 204 } 205 206 207 static dc_status_t 208 reefnet_sensusultra_packet (reefnet_sensusultra_device_t *device, unsigned char *data, unsigned int size, unsigned int header) 209 { 210 dc_status_t status = DC_STATUS_SUCCESS; 211 dc_device_t *abstract = (dc_device_t *) device; 212 213 assert (size >= header + 2); 214 215 if (device_is_cancelled (abstract)) 216 return DC_STATUS_CANCELLED; 217 218 // Receive the data packet. 219 status = dc_iostream_read (device->iostream, data, size, NULL); 220 if (status != DC_STATUS_SUCCESS) { 221 ERROR (abstract->context, "Failed to receive the packet."); 222 return status; 223 } 224 225 // Verify the checksum of the packet. 226 unsigned short crc = array_uint16_le (data + size - 2); 227 unsigned short ccrc = checksum_crc16_ccitt (data + header, size - header - 2, 0xffff, 0x0000); 228 if (crc != ccrc) { 229 ERROR (abstract->context, "Unexpected answer checksum."); 230 return DC_STATUS_PROTOCOL; 231 } 232 233 return DC_STATUS_SUCCESS; 234 } 235 236 237 static dc_status_t 238 reefnet_sensusultra_handshake (reefnet_sensusultra_device_t *device, unsigned short value) 239 { 240 // Wake-up the device. 241 unsigned char handshake[SZ_HANDSHAKE + 2] = {0}; 242 dc_status_t rc = reefnet_sensusultra_packet (device, handshake, sizeof (handshake), 0); 243 if (rc != DC_STATUS_SUCCESS) 244 return rc; 245 246 HEXDUMP (device->base.context, DC_LOGLEVEL_DEBUG, "Handshake", handshake, sizeof(handshake) - 2); 247 248 // Store the clock calibration values. 249 device->systime = dc_datetime_now (); 250 device->devtime = array_uint32_le (handshake + 4); 251 252 // Store the handshake packet. 253 memcpy (device->handshake, handshake, SZ_HANDSHAKE); 254 255 // Emit a clock event. 256 dc_event_clock_t clock; 257 clock.systime = device->systime; 258 clock.devtime = device->devtime; 259 device_event_emit (&device->base, DC_EVENT_CLOCK, &clock); 260 261 // Emit a device info event. 262 dc_event_devinfo_t devinfo; 263 devinfo.model = handshake[1]; 264 devinfo.firmware = handshake[0]; 265 devinfo.serial = array_uint16_le (handshake + 2); 266 device_event_emit (&device->base, DC_EVENT_DEVINFO, &devinfo); 267 268 // Emit a vendor event. 269 dc_event_vendor_t vendor; 270 vendor.data = device->handshake; 271 vendor.size = sizeof (device->handshake); 272 device_event_emit (&device->base, DC_EVENT_VENDOR, &vendor); 273 274 // Send the instruction code to the device. 275 rc = reefnet_sensusultra_send_ushort (device, value); 276 if (rc != DC_STATUS_SUCCESS) 277 return rc; 278 279 return DC_STATUS_SUCCESS; 280 } 281 282 283 static dc_status_t 284 reefnet_sensusultra_page (reefnet_sensusultra_device_t *device, unsigned char *data, unsigned int size, unsigned int pagenum) 285 { 286 dc_device_t *abstract = (dc_device_t *) device; 287 288 assert (size >= SZ_PACKET + 4); 289 290 unsigned int nretries = 0; 291 dc_status_t rc = DC_STATUS_SUCCESS; 292 while ((rc = reefnet_sensusultra_packet (device, data, size, 2)) != DC_STATUS_SUCCESS) { 293 // Automatically discard a corrupted packet, 294 // and request a new one. 295 if (rc != DC_STATUS_PROTOCOL) 296 return rc; 297 298 // Abort if the maximum number of retries is reached. 299 if (nretries++ >= MAXRETRIES) 300 return rc; 301 302 // Reject the packet. 303 rc = reefnet_sensusultra_send_uchar (device, REJECT); 304 if (rc != DC_STATUS_SUCCESS) 305 return rc; 306 } 307 308 // Verify the page number. 309 unsigned int page = array_uint16_le (data); 310 if (page != pagenum) { 311 ERROR (abstract->context, "Unexpected page number."); 312 return DC_STATUS_PROTOCOL; 313 } 314 315 return DC_STATUS_SUCCESS; 316 } 317 318 319 static dc_status_t 320 reefnet_sensusultra_send (reefnet_sensusultra_device_t *device, unsigned short command) 321 { 322 // Flush the input and output buffers. 323 dc_iostream_purge (device->iostream, DC_DIRECTION_ALL); 324 325 // Wake-up the device and send the instruction code. 326 unsigned int nretries = 0; 327 dc_status_t rc = DC_STATUS_SUCCESS; 328 while ((rc = reefnet_sensusultra_handshake (device, command)) != DC_STATUS_SUCCESS) { 329 // Automatically discard a corrupted handshake packet, 330 // and wait for the next one. 331 if (rc != DC_STATUS_PROTOCOL && rc != DC_STATUS_TIMEOUT) 332 return rc; 333 334 // Abort if the maximum number of retries is reached. 335 if (nretries++ >= MAXRETRIES) 336 return rc; 337 338 // According to the developers guide, a 250 ms delay is suggested to 339 // guarantee that the prompt byte sent after the handshake packet is 340 // not accidentally buffered by the host and (mis)interpreted as part 341 // of the next packet. 342 343 dc_iostream_sleep (device->iostream, 250); 344 dc_iostream_purge (device->iostream, DC_DIRECTION_ALL); 345 } 346 347 return DC_STATUS_SUCCESS; 348 } 349 350 351 static dc_status_t 352 reefnet_sensusultra_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) 353 { 354 reefnet_sensusultra_device_t *device = (reefnet_sensusultra_device_t*) abstract; 355 356 // Pre-allocate the required amount of memory. 357 if (!dc_buffer_reserve (buffer, SZ_MEMORY)) { 358 ERROR (abstract->context, "Insufficient buffer space available."); 359 return DC_STATUS_NOMEMORY; 360 } 361 362 // Enable progress notifications. 363 dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER; 364 progress.maximum = SZ_MEMORY; 365 device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); 366 367 // Wake-up the device and send the instruction code. 368 dc_status_t rc = reefnet_sensusultra_send (device, 0xB421); 369 if (rc != DC_STATUS_SUCCESS) 370 return rc; 371 372 unsigned int nbytes = 0; 373 unsigned int npages = 0; 374 while (nbytes < SZ_MEMORY) { 375 // Receive the packet. 376 unsigned char packet[SZ_PACKET + 4] = {0}; 377 rc = reefnet_sensusultra_page (device, packet, sizeof (packet), npages); 378 if (rc != DC_STATUS_SUCCESS) 379 return rc; 380 381 // Update and emit a progress event. 382 progress.current += SZ_PACKET; 383 device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); 384 385 // Prepend the packet to the buffer. 386 if (!dc_buffer_prepend (buffer, packet + 2, SZ_PACKET)) { 387 ERROR (abstract->context, "Insufficient buffer space available."); 388 return DC_STATUS_NOMEMORY; 389 } 390 391 // Accept the packet. 392 rc = reefnet_sensusultra_send_uchar (device, ACCEPT); 393 if (rc != DC_STATUS_SUCCESS) 394 return rc; 395 396 nbytes += SZ_PACKET; 397 npages++; 398 } 399 400 return DC_STATUS_SUCCESS; 401 } 402 403 404 dc_status_t 405 reefnet_sensusultra_device_read_user (dc_device_t *abstract, unsigned char *data, unsigned int size) 406 { 407 reefnet_sensusultra_device_t *device = (reefnet_sensusultra_device_t*) abstract; 408 409 if (!ISINSTANCE (abstract)) 410 return DC_STATUS_INVALIDARGS; 411 412 if (size < SZ_USER) { 413 ERROR (abstract->context, "Insufficient buffer space available."); 414 return DC_STATUS_INVALIDARGS; 415 } 416 417 // Wake-up the device and send the instruction code. 418 dc_status_t rc = reefnet_sensusultra_send (device, 0xB420); 419 if (rc != DC_STATUS_SUCCESS) 420 return rc; 421 422 unsigned int nbytes = 0; 423 unsigned int npages = 0; 424 while (nbytes < SZ_USER) { 425 // Receive the packet. 426 unsigned char packet[SZ_PACKET + 4] = {0}; 427 rc = reefnet_sensusultra_page (device, packet, sizeof (packet), npages); 428 if (rc != DC_STATUS_SUCCESS) 429 return rc; 430 431 // Append the packet to the buffer. 432 memcpy (data + nbytes, packet + 2, SZ_PACKET); 433 434 // Accept the packet. 435 rc = reefnet_sensusultra_send_uchar (device, ACCEPT); 436 if (rc != DC_STATUS_SUCCESS) 437 return rc; 438 439 nbytes += SZ_PACKET; 440 npages++; 441 } 442 443 return DC_STATUS_SUCCESS; 444 } 445 446 447 dc_status_t 448 reefnet_sensusultra_device_write_user (dc_device_t *abstract, const unsigned char *data, unsigned int size) 449 { 450 reefnet_sensusultra_device_t *device = (reefnet_sensusultra_device_t*) abstract; 451 452 if (!ISINSTANCE (abstract)) 453 return DC_STATUS_INVALIDARGS; 454 455 if (size < SZ_USER) { 456 ERROR (abstract->context, "Insufficient buffer space available."); 457 return DC_STATUS_INVALIDARGS; 458 } 459 460 // Enable progress notifications. 461 dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER; 462 progress.maximum = SZ_USER + 2; 463 device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); 464 465 // Wake-up the device and send the instruction code. 466 dc_status_t rc = reefnet_sensusultra_send (device, 0xB430); 467 if (rc != DC_STATUS_SUCCESS) 468 return rc; 469 470 // Send the data to the device. 471 for (unsigned int i = 0; i < SZ_USER; ++i) { 472 rc = reefnet_sensusultra_send_uchar (device, data[i]); 473 if (rc != DC_STATUS_SUCCESS) 474 return rc; 475 476 // Update and emit a progress event. 477 progress.current += 1; 478 device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); 479 } 480 481 // Send the checksum to the device. 482 unsigned short crc = checksum_crc16_ccitt (data, SZ_USER, 0xffff, 0x0000); 483 rc = reefnet_sensusultra_send_ushort (device, crc); 484 if (rc != DC_STATUS_SUCCESS) 485 return rc; 486 487 // Update and emit a progress event. 488 progress.current += 2; 489 device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); 490 491 return DC_STATUS_SUCCESS; 492 } 493 494 495 dc_status_t 496 reefnet_sensusultra_device_write_parameter (dc_device_t *abstract, reefnet_sensusultra_parameter_t parameter, unsigned int value) 497 { 498 reefnet_sensusultra_device_t *device = (reefnet_sensusultra_device_t*) abstract; 499 500 if (!ISINSTANCE (abstract)) 501 return DC_STATUS_INVALIDARGS; 502 503 // Set the instruction code and validate the new value. 504 unsigned short code = 0; 505 switch (parameter) { 506 case REEFNET_SENSUSULTRA_PARAMETER_INTERVAL: 507 code = 0xB410; 508 if (value < 1 || value > 65535) 509 return DC_STATUS_INVALIDARGS; 510 break; 511 case REEFNET_SENSUSULTRA_PARAMETER_THRESHOLD: 512 code = 0xB411; 513 if (value < 1 || value > 65535) 514 return DC_STATUS_INVALIDARGS; 515 break; 516 case REEFNET_SENSUSULTRA_PARAMETER_ENDCOUNT: 517 code = 0xB412; 518 if (value < 1 || value > 65535) 519 return DC_STATUS_INVALIDARGS; 520 break; 521 case REEFNET_SENSUSULTRA_PARAMETER_AVERAGING: 522 code = 0xB413; 523 if (value != 1 && value != 2 && value != 4) 524 return DC_STATUS_INVALIDARGS; 525 break; 526 default: 527 return DC_STATUS_INVALIDARGS; 528 } 529 530 // Wake-up the device and send the instruction code. 531 dc_status_t rc = reefnet_sensusultra_send (device, code); 532 if (rc != DC_STATUS_SUCCESS) 533 return rc; 534 535 // Send the new value to the device. 536 rc = reefnet_sensusultra_send_ushort (device, value); 537 if (rc != DC_STATUS_SUCCESS) 538 return rc; 539 540 return DC_STATUS_SUCCESS; 541 } 542 543 544 dc_status_t 545 reefnet_sensusultra_device_sense (dc_device_t *abstract, unsigned char *data, unsigned int size) 546 { 547 reefnet_sensusultra_device_t *device = (reefnet_sensusultra_device_t*) abstract; 548 549 if (!ISINSTANCE (abstract)) 550 return DC_STATUS_INVALIDARGS; 551 552 if (size < SZ_SENSE) { 553 ERROR (abstract->context, "Insufficient buffer space available."); 554 return DC_STATUS_INVALIDARGS; 555 } 556 557 // Wake-up the device and send the instruction code. 558 dc_status_t rc = reefnet_sensusultra_send (device, 0xB440); 559 if (rc != DC_STATUS_SUCCESS) 560 return rc; 561 562 // Receive the packet. 563 unsigned char package[SZ_SENSE + 2] = {0}; 564 rc = reefnet_sensusultra_packet (device, package, sizeof (package), 0); 565 if (rc != DC_STATUS_SUCCESS) 566 return rc; 567 568 memcpy (data, package, SZ_SENSE); 569 570 return DC_STATUS_SUCCESS; 571 } 572 573 574 static dc_status_t 575 reefnet_sensusultra_parse (reefnet_sensusultra_device_t *device, 576 const unsigned char data[], unsigned int *premaining, unsigned int *pprevious, 577 int *aborted, dc_dive_callback_t callback, void *userdata) 578 { 579 const unsigned char header[4] = {0x00, 0x00, 0x00, 0x00}; 580 const unsigned char footer[4] = {0xFF, 0xFF, 0xFF, 0xFF}; 581 582 // Initialize the data stream pointers. 583 const unsigned char *current = data + *premaining; 584 const unsigned char *previous = data + *pprevious; 585 586 // Search the data stream for header markers. 587 while ((current = array_search_backward (data, current - data, header, sizeof (header))) != NULL) { 588 // Move the pointer to the begin of the header. 589 current -= sizeof (header); 590 591 // If there is a sequence of more than 4 zero bytes present, the header 592 // marker is located at the start of this sequence, not the end. 593 while (current > data && current[-1] == 0x00) 594 current--; 595 596 // Once a header marker is found, start searching 597 // for the corresponding footer marker. The search is 598 // now limited to the start of the previous dive. 599 if (previous - current >= 16) { 600 previous = array_search_forward (current + 16, previous - current - 16, footer, sizeof (footer)); 601 } else { 602 previous = NULL; 603 } 604 605 // Skip dives without a footer marker. 606 if (previous) { 607 // Move the pointer to the end of the footer. 608 previous += sizeof (footer); 609 610 // Automatically abort when a dive is older than the provided timestamp. 611 unsigned int timestamp = array_uint32_le (current + 4); 612 if (device && timestamp <= device->timestamp) { 613 if (aborted) 614 *aborted = 1; 615 return DC_STATUS_SUCCESS; 616 } 617 618 if (callback && !callback (current, previous - current, current + 4, 4, userdata)) { 619 if (aborted) 620 *aborted = 1; 621 return DC_STATUS_SUCCESS; 622 } 623 } 624 625 // Prepare for the next iteration. 626 previous = current; 627 628 // Return the current state. 629 *premaining = *pprevious = current - data; 630 } 631 632 // Return the current state. 633 *premaining = sizeof (header) - 1; 634 if (*premaining > *pprevious) 635 *premaining = *pprevious; 636 637 if (aborted) 638 *aborted = 0; 639 640 return DC_STATUS_SUCCESS; 641 } 642 643 644 static dc_status_t 645 reefnet_sensusultra_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata) 646 { 647 reefnet_sensusultra_device_t *device = (reefnet_sensusultra_device_t*) abstract; 648 649 dc_buffer_t *buffer = dc_buffer_new (SZ_MEMORY); 650 if (buffer == NULL) { 651 ERROR (abstract->context, "Failed to allocate memory."); 652 return DC_STATUS_NOMEMORY; 653 } 654 655 // Enable progress notifications. 656 dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER; 657 progress.maximum = SZ_MEMORY; 658 device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); 659 660 // Wake-up the device and send the instruction code. 661 dc_status_t rc = reefnet_sensusultra_send (device, 0xB421); 662 if (rc != DC_STATUS_SUCCESS) { 663 dc_buffer_free (buffer); 664 return rc; 665 } 666 667 // Initialize the state for the incremental parser. 668 unsigned int remaining = 0; 669 unsigned int previous = 0; 670 671 unsigned int nbytes = 0; 672 unsigned int npages = 0; 673 while (nbytes < SZ_MEMORY) { 674 // Receive the packet. 675 unsigned char packet[SZ_PACKET + 4] = {0}; 676 rc = reefnet_sensusultra_page (device, packet, sizeof (packet), npages); 677 if (rc != DC_STATUS_SUCCESS) { 678 dc_buffer_free (buffer); 679 return rc; 680 } 681 682 // Update and emit a progress event. 683 progress.current += SZ_PACKET; 684 device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); 685 686 // Abort the transfer if the page contains no useful data. 687 if (array_isequal (packet + 2, SZ_PACKET, 0xFF) && nbytes != 0) 688 break; 689 690 // Prepend the packet to the buffer. 691 if (!dc_buffer_prepend (buffer, packet + 2, SZ_PACKET)) { 692 dc_buffer_free (buffer); 693 ERROR (abstract->context, "Insufficient buffer space available."); 694 return DC_STATUS_NOMEMORY; 695 } 696 697 // Update the parser state. 698 remaining += SZ_PACKET; 699 previous += SZ_PACKET; 700 701 // Parse the page data. 702 int aborted = 0; 703 rc = reefnet_sensusultra_parse (device, dc_buffer_get_data (buffer), 704 &remaining, &previous, &aborted, callback, userdata); 705 if (rc != DC_STATUS_SUCCESS) { 706 dc_buffer_free (buffer); 707 return rc; 708 } 709 if (aborted) 710 break; 711 712 // Accept the packet. 713 rc = reefnet_sensusultra_send_uchar (device, ACCEPT); 714 if (rc != DC_STATUS_SUCCESS) { 715 dc_buffer_free (buffer); 716 return rc; 717 } 718 719 nbytes += SZ_PACKET; 720 npages++; 721 } 722 723 dc_buffer_free (buffer); 724 725 return DC_STATUS_SUCCESS; 726 }