suunto_eonsteel.c (23216B)
1 /* 2 * libdivecomputer 3 * 4 * Copyright (C) 2014 Linus Torvalds 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 <stdio.h> 23 #include <stdlib.h> 24 #include <string.h> 25 26 #include "suunto_eonsteel.h" 27 #include "context-private.h" 28 #include "device-private.h" 29 #include "array.h" 30 #include "platform.h" 31 #include "checksum.h" 32 #include "hdlc.h" 33 34 #define EONSTEEL 0 35 #define EONCORE 1 36 37 typedef struct suunto_eonsteel_device_t { 38 dc_device_t base; 39 dc_iostream_t *iostream; 40 unsigned int model; 41 unsigned int magic; 42 unsigned short seq; 43 unsigned char version[0x30]; 44 unsigned char fingerprint[4]; 45 } suunto_eonsteel_device_t; 46 47 // The EON Steel implements a small filesystem 48 #define DIRTYPE_FILE 0x0001 49 #define DIRTYPE_DIR 0x0002 50 51 struct directory_entry { 52 struct directory_entry *next; 53 int type; 54 int namelen; 55 char name[1]; 56 }; 57 58 // EON Steel command numbers and other magic field values 59 #define CMD_INIT 0x0000 60 #define INIT_MAGIC 0x0001 61 #define INIT_SEQ 0 62 63 #define CMD_READ_STRING 0x0411 64 65 #define CMD_FILE_OPEN 0x0010 66 #define CMD_FILE_READ 0x0110 67 #define CMD_FILE_STAT 0x0710 68 #define CMD_FILE_CLOSE 0x0510 69 70 #define CMD_DIR_OPEN 0x0810 71 #define CMD_DIR_READDIR 0x0910 72 #define CMD_DIR_CLOSE 0x0a10 73 74 #define CMD_SET_TIME 0x0003 75 #define CMD_GET_TIME 0x0103 76 #define CMD_SET_DATE 0x0203 77 #define CMD_GET_DATE 0x0303 78 79 #define PACKET_SIZE 64 80 #define HEADER_SIZE 12 81 #define MAXDATA_SIZE 2048 82 #define CRC_SIZE 4 83 84 static dc_status_t suunto_eonsteel_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size); 85 static dc_status_t suunto_eonsteel_device_foreach(dc_device_t *abstract, dc_dive_callback_t callback, void *userdata); 86 static dc_status_t suunto_eonsteel_device_timesync(dc_device_t *abstract, const dc_datetime_t *datetime); 87 static dc_status_t suunto_eonsteel_device_close (dc_device_t *abstract); 88 89 static const dc_device_vtable_t suunto_eonsteel_device_vtable = { 90 sizeof(suunto_eonsteel_device_t), 91 DC_FAMILY_SUUNTO_EONSTEEL, 92 suunto_eonsteel_device_set_fingerprint, /* set_fingerprint */ 93 NULL, /* read */ 94 NULL, /* write */ 95 NULL, /* dump */ 96 suunto_eonsteel_device_foreach, /* foreach */ 97 suunto_eonsteel_device_timesync, /* timesync */ 98 suunto_eonsteel_device_close /* close */ 99 }; 100 101 static const char dive_directory[] = "0:/dives"; 102 103 static void file_list_free (struct directory_entry *de) 104 { 105 while (de) { 106 struct directory_entry *next = de->next; 107 free (de); 108 de = next; 109 } 110 } 111 112 static struct directory_entry *alloc_dirent(int type, int len, const char *name) 113 { 114 struct directory_entry *res; 115 116 res = (struct directory_entry *) malloc(offsetof(struct directory_entry, name) + len + 1); 117 if (res) { 118 res->next = NULL; 119 res->type = type; 120 res->namelen = len; 121 memcpy(res->name, name, len); 122 res->name[len] = 0; 123 } 124 return res; 125 } 126 127 /* 128 * Get a single 64-byte packet from the dive computer. This handles packet 129 * logging and any obvious packet-level errors, and returns the payload of 130 * packet. 131 * 132 * The two first bytes of the packet are packet-level metadata: the report 133 * type (always 0x3f), and then the size of the valid data in the packet. 134 * 135 * The maximum payload is 62 bytes. 136 */ 137 static dc_status_t 138 suunto_eonsteel_receive_usb(suunto_eonsteel_device_t *device, unsigned char data[], unsigned int size, unsigned int *actual) 139 { 140 dc_status_t rc = DC_STATUS_SUCCESS; 141 unsigned char buf[PACKET_SIZE]; 142 size_t transferred = 0; 143 unsigned int len = 0; 144 145 rc = dc_iostream_read(device->iostream, buf, sizeof(buf), &transferred); 146 if (rc != DC_STATUS_SUCCESS) { 147 ERROR(device->base.context, "Failed to receive the packet."); 148 return rc; 149 } 150 151 if (transferred < 2) { 152 ERROR(device->base.context, "Invalid packet length (" DC_PRINTF_SIZE ").", transferred); 153 return DC_STATUS_PROTOCOL; 154 } 155 156 if (buf[0] != 0x3f) { 157 ERROR(device->base.context, "Invalid report type (%02x).", buf[0]); 158 return DC_STATUS_PROTOCOL; 159 } 160 161 len = buf[1]; 162 if (len + 2 > transferred) { 163 ERROR(device->base.context, "Invalid payload length (%u).", len); 164 return DC_STATUS_PROTOCOL; 165 } 166 if (len > size) { 167 ERROR(device->base.context, "Insufficient buffer space available."); 168 return DC_STATUS_PROTOCOL; 169 } 170 171 HEXDUMP (device->base.context, DC_LOGLEVEL_DEBUG, "rcv", buf + 2, len); 172 173 memcpy(data, buf + 2, len); 174 175 if (actual) 176 *actual = len; 177 178 return DC_STATUS_SUCCESS; 179 } 180 181 static dc_status_t 182 suunto_eonsteel_receive_ble(suunto_eonsteel_device_t *device, unsigned char data[], unsigned int size, unsigned int *actual) 183 { 184 dc_status_t rc = DC_STATUS_SUCCESS; 185 unsigned char buffer[HEADER_SIZE + MAXDATA_SIZE + CRC_SIZE]; 186 size_t transferred = 0; 187 188 rc = dc_iostream_read(device->iostream, buffer, sizeof(buffer), &transferred); 189 if (rc != DC_STATUS_SUCCESS) { 190 ERROR(device->base.context, "Failed to receive the packet."); 191 return rc; 192 } 193 194 if (transferred < CRC_SIZE) { 195 ERROR(device->base.context, "Invalid packet length (" DC_PRINTF_SIZE ").", transferred); 196 return DC_STATUS_PROTOCOL; 197 } 198 199 unsigned int nbytes = transferred - CRC_SIZE; 200 201 unsigned int crc = array_uint32_le(buffer + nbytes); 202 unsigned int ccrc = checksum_crc32r(buffer, nbytes); 203 if (crc != ccrc) { 204 ERROR(device->base.context, "Invalid checksum (expected %08x, received %08x).", ccrc, crc); 205 return DC_STATUS_PROTOCOL; 206 } 207 208 if (nbytes > size) { 209 ERROR(device->base.context, "Insufficient buffer space available."); 210 return DC_STATUS_PROTOCOL; 211 } 212 213 memcpy(data, buffer, nbytes); 214 215 HEXDUMP (device->base.context, DC_LOGLEVEL_DEBUG, "rcv", buffer, nbytes); 216 217 if (actual) 218 *actual = nbytes; 219 220 return DC_STATUS_SUCCESS; 221 } 222 223 static dc_status_t 224 suunto_eonsteel_send(suunto_eonsteel_device_t *device, 225 unsigned short cmd, 226 const unsigned char data[], 227 unsigned int size) 228 { 229 dc_status_t rc = DC_STATUS_SUCCESS; 230 unsigned char buf[PACKET_SIZE + CRC_SIZE]; 231 232 // Two-byte packet header, followed by 12 bytes of extended header 233 if (size + 2 + HEADER_SIZE + CRC_SIZE > sizeof(buf)) { 234 ERROR(device->base.context, "Insufficient buffer space available."); 235 return DC_STATUS_PROTOCOL; 236 } 237 238 memset(buf, 0, sizeof(buf)); 239 240 buf[0] = 0x3f; 241 buf[1] = size + HEADER_SIZE; 242 243 // 2-byte LE command word 244 array_uint16_le_set(buf + 2, cmd); 245 246 // 4-byte LE magic value (starts at 1) 247 array_uint32_le_set(buf + 4, device->magic); 248 249 // 2-byte LE sequence number; 250 array_uint16_le_set(buf + 8, device->seq); 251 252 // 4-byte LE length 253 array_uint32_le_set(buf + 10, size); 254 255 // .. followed by actual data 256 if (size) { 257 memcpy(buf + 14, data, size); 258 } 259 260 // 4 byte LE checksum 261 unsigned int crc = checksum_crc32r(buf + 2, size + HEADER_SIZE); 262 array_uint32_le_set(buf + 14 + size, crc); 263 264 if (dc_iostream_get_transport(device->iostream) == DC_TRANSPORT_BLE) { 265 rc = dc_iostream_write(device->iostream, buf + 2, size + HEADER_SIZE + CRC_SIZE, NULL); 266 } else { 267 rc = dc_iostream_write(device->iostream, buf, sizeof(buf) - CRC_SIZE, NULL); 268 } 269 if (rc != DC_STATUS_SUCCESS) { 270 ERROR(device->base.context, "Failed to send the command."); 271 return rc; 272 } 273 274 HEXDUMP (device->base.context, DC_LOGLEVEL_DEBUG, "cmd", buf + 2, size + HEADER_SIZE); 275 276 return DC_STATUS_SUCCESS; 277 } 278 279 /* 280 * Send a command, receive a reply 281 * 282 * This carefully checks the data fields in the reply for a match 283 * against the command, and then only returns the actual reply 284 * data itself. 285 * 286 * Also note that receive() function itself will have removed the 287 * per-packet handshake bytes, so unlike the send() function, this 288 * functon does not see the two initial 0x3f 0x?? bytes, and thus the 289 * offsets for the cmd/magic/seq/len are off by two compared to the 290 * send() side. The offsets are the same in the actual raw packet. 291 */ 292 static dc_status_t 293 suunto_eonsteel_transfer(suunto_eonsteel_device_t *device, 294 unsigned short cmd, 295 const unsigned char data[], unsigned int size, 296 unsigned char answer[], unsigned int asize, 297 unsigned int *actual) 298 { 299 dc_status_t rc = DC_STATUS_SUCCESS; 300 unsigned char header[HEADER_SIZE + MAXDATA_SIZE]; 301 unsigned int len = 0; 302 303 // Send the command. 304 rc = suunto_eonsteel_send(device, cmd, data, size); 305 if (rc != DC_STATUS_SUCCESS) 306 return rc; 307 308 if (dc_iostream_get_transport(device->iostream) == DC_TRANSPORT_BLE) { 309 // Receive the entire data packet. 310 rc = suunto_eonsteel_receive_ble(device, header, sizeof(header), &len); 311 } else { 312 // Receive the header and the first part of the data. 313 rc = suunto_eonsteel_receive_usb(device, header, sizeof(header), &len); 314 } 315 if (rc != DC_STATUS_SUCCESS) 316 return rc; 317 318 // Verify the header length. 319 if (len < HEADER_SIZE) { 320 ERROR(device->base.context, "Invalid packet length (%u).", len); 321 return DC_STATUS_PROTOCOL; 322 } 323 324 // Unpack the 12 byte header. 325 unsigned int reply = array_uint16_le(header); 326 unsigned int magic = array_uint32_le(header + 2); 327 unsigned int seq = array_uint16_le(header + 6); 328 unsigned int length = array_uint32_le(header + 8); 329 330 if (cmd != CMD_INIT) { 331 // Verify the command reply. 332 if (reply != cmd) { 333 ERROR(device->base.context, "Unexpected command reply (received %04x, expected %04x).", reply, cmd); 334 return DC_STATUS_PROTOCOL; 335 } 336 337 // Verify the magic value. 338 if (magic != device->magic + 5) { 339 ERROR(device->base.context, "Unexpected magic value (received %08x, expected %08x).", magic, device->magic + 5); 340 return DC_STATUS_PROTOCOL; 341 } 342 } 343 344 // Verify the sequence number. 345 if (seq != device->seq) { 346 ERROR(device->base.context, "Unexpected sequence number (received %04x, expected %04x).", seq, device->seq); 347 return DC_STATUS_PROTOCOL; 348 } 349 350 // Verify the length. 351 if (length > asize) { 352 ERROR(device->base.context, "Insufficient buffer space available."); 353 return DC_STATUS_PROTOCOL; 354 } 355 356 // Verify the initial payload length. 357 unsigned int nbytes = len - HEADER_SIZE; 358 if (nbytes > length) { 359 ERROR(device->base.context, "Unexpected number of bytes (received %u, expected %u).", nbytes, length); 360 return DC_STATUS_PROTOCOL; 361 } 362 363 // Copy the payload data. 364 memcpy(answer, header + HEADER_SIZE, nbytes); 365 366 // Receive the remainder of the data. 367 if (dc_iostream_get_transport(device->iostream) != DC_TRANSPORT_BLE) { 368 while (nbytes < length) { 369 rc = suunto_eonsteel_receive_usb(device, answer + nbytes, length - nbytes, &len); 370 if (rc != DC_STATUS_SUCCESS) 371 return rc; 372 373 nbytes += len; 374 375 if (len < PACKET_SIZE - 2) 376 break; 377 } 378 } 379 380 // Verify the total payload length. 381 if (nbytes != length) { 382 ERROR(device->base.context, "Unexpected number of bytes (received %u, expected %u).", nbytes, length); 383 return DC_STATUS_PROTOCOL; 384 } 385 386 // Remember the magic number. 387 if (cmd == CMD_INIT) { 388 device->magic = (magic & 0xffff0000) | 0x0005; 389 } 390 391 // Increment the sequence number. 392 device->seq++; 393 394 if (actual) 395 *actual = nbytes; 396 397 return DC_STATUS_SUCCESS; 398 } 399 400 static dc_status_t 401 read_file(suunto_eonsteel_device_t *eon, const char *filename, dc_buffer_t *buf) 402 { 403 dc_status_t rc = DC_STATUS_SUCCESS; 404 unsigned char result[2560]; 405 unsigned char cmdbuf[64]; 406 unsigned int size, offset, len; 407 unsigned int n = 0; 408 409 memset(cmdbuf, 0, sizeof(cmdbuf)); 410 len = strlen(filename) + 1; 411 if (len + 4 > sizeof(cmdbuf)) { 412 ERROR(eon->base.context, "too long filename: %s", filename); 413 return DC_STATUS_PROTOCOL; 414 } 415 memcpy(cmdbuf+4, filename, len); 416 rc = suunto_eonsteel_transfer(eon, CMD_FILE_OPEN, 417 cmdbuf, len + 4, result, sizeof(result), &n); 418 if (rc != DC_STATUS_SUCCESS) { 419 ERROR(eon->base.context, "unable to look up %s", filename); 420 return rc; 421 } 422 HEXDUMP (eon->base.context, DC_LOGLEVEL_DEBUG, "lookup", result, n); 423 424 rc = suunto_eonsteel_transfer(eon, CMD_FILE_STAT, 425 NULL, 0, result, sizeof(result), &n); 426 if (rc != DC_STATUS_SUCCESS) { 427 ERROR(eon->base.context, "unable to stat %s", filename); 428 return rc; 429 } 430 HEXDUMP (eon->base.context, DC_LOGLEVEL_DEBUG, "stat", result, n); 431 432 size = array_uint32_le(result+4); 433 offset = 0; 434 435 while (size > 0) { 436 unsigned int ask, got, at; 437 438 ask = size; 439 if (ask > 1024) 440 ask = 1024; 441 array_uint32_le_set(cmdbuf + 0, 1234); // Not file offset, after all 442 array_uint32_le_set(cmdbuf + 4, ask); // Size of read 443 rc = suunto_eonsteel_transfer(eon, CMD_FILE_READ, 444 cmdbuf, 8, result, sizeof(result), &n); 445 if (rc != DC_STATUS_SUCCESS) { 446 ERROR(eon->base.context, "unable to read %s", filename); 447 return rc; 448 } 449 if (n < 8) { 450 ERROR(eon->base.context, "got short read reply for %s", filename); 451 return DC_STATUS_PROTOCOL; 452 } 453 454 // Not file offset, just stays unmodified. 455 at = array_uint32_le(result); 456 if (at != 1234) { 457 ERROR(eon->base.context, "read of %s returned different offset than asked for (%d vs %d)", filename, at, offset); 458 return DC_STATUS_PROTOCOL; 459 } 460 461 // Number of bytes actually read 462 got = array_uint32_le(result+4); 463 if (!got) 464 break; 465 if (n < 8 + got) { 466 ERROR(eon->base.context, "odd read size reply for offset %d of file %s", offset, filename); 467 return DC_STATUS_PROTOCOL; 468 } 469 470 if (got > size) 471 got = size; 472 if (!dc_buffer_append (buf, result + 8, got)) { 473 ERROR (eon->base.context, "Insufficient buffer space available."); 474 return DC_STATUS_NOMEMORY; 475 } 476 offset += got; 477 size -= got; 478 } 479 480 rc = suunto_eonsteel_transfer(eon, CMD_FILE_CLOSE, 481 NULL, 0, result, sizeof(result), &n); 482 if (rc != DC_STATUS_SUCCESS) { 483 ERROR(eon->base.context, "cmd CMD_FILE_CLOSE failed"); 484 return rc; 485 } 486 HEXDUMP(eon->base.context, DC_LOGLEVEL_DEBUG, "close", result, n); 487 488 return DC_STATUS_SUCCESS; 489 } 490 491 /* 492 * Insert a directory entry in the sorted list, most recent entry 493 * first. 494 * 495 * The directory entry names are the timestamps as hex, so ordering 496 * in alphabetical order ends up also ordering in date order! 497 */ 498 static struct directory_entry *insert_dirent(struct directory_entry *entry, struct directory_entry *list) 499 { 500 struct directory_entry **pos = &list, *next; 501 502 while ((next = *pos) != NULL) { 503 /* Is this bigger (more recent) than the next entry? We're good! */ 504 if (strcmp(entry->name, next->name) > 0) 505 break; 506 pos = &next->next; 507 } 508 entry->next = next; 509 *pos = entry; 510 511 return list; 512 } 513 514 /* 515 * NOTE! This will create the list of dirent's in reverse order, 516 * with the last dirent first. That's intentional: for dives, 517 * we will want to look up the last dive first. 518 */ 519 static struct directory_entry *parse_dirent(suunto_eonsteel_device_t *eon, int nr, const unsigned char *p, unsigned int len, struct directory_entry *list) 520 { 521 while (len > 8) { 522 unsigned int type = array_uint32_le(p); 523 unsigned int namelen = array_uint32_le(p+4); 524 const unsigned char *name = p+8; 525 struct directory_entry *entry; 526 527 if (namelen + 8 + 1 > len || name[namelen] != 0) { 528 ERROR(eon->base.context, "corrupt dirent entry"); 529 break; 530 } 531 HEXDUMP(eon->base.context, DC_LOGLEVEL_DEBUG, "dir entry", p, 8); 532 533 p += 8 + namelen + 1; 534 len -= 8 + namelen + 1; 535 entry = alloc_dirent(type, namelen, (const char *) name); 536 if (!entry) { 537 ERROR(eon->base.context, "out of memory"); 538 break; 539 } 540 list = insert_dirent(entry, list); 541 } 542 return list; 543 } 544 545 static dc_status_t 546 get_file_list(suunto_eonsteel_device_t *eon, struct directory_entry **res) 547 { 548 dc_status_t rc = DC_STATUS_SUCCESS; 549 struct directory_entry *de = NULL; 550 unsigned char cmd[64]; 551 unsigned char result[2048]; 552 unsigned int n = 0; 553 unsigned int cmdlen; 554 555 array_uint32_le_set(cmd, 0); 556 memcpy(cmd + 4, dive_directory, sizeof(dive_directory)); 557 cmdlen = 4 + sizeof(dive_directory); 558 rc = suunto_eonsteel_transfer(eon, CMD_DIR_OPEN, 559 cmd, cmdlen, result, sizeof(result), &n); 560 if (rc != DC_STATUS_SUCCESS) { 561 ERROR(eon->base.context, "cmd DIR_LOOKUP failed"); 562 return rc; 563 } 564 HEXDUMP(eon->base.context, DC_LOGLEVEL_DEBUG, "DIR_LOOKUP", result, n); 565 566 for (;;) { 567 unsigned int nr, last; 568 569 rc = suunto_eonsteel_transfer(eon, CMD_DIR_READDIR, 570 NULL, 0, result, sizeof(result), &n); 571 if (rc != DC_STATUS_SUCCESS) { 572 ERROR(eon->base.context, "readdir failed"); 573 file_list_free(de); 574 return rc; 575 } 576 if (n < 8) { 577 ERROR(eon->base.context, "short readdir result"); 578 file_list_free(de); 579 return DC_STATUS_PROTOCOL; 580 } 581 nr = array_uint32_le(result); 582 last = array_uint32_le(result+4); 583 HEXDUMP(eon->base.context, DC_LOGLEVEL_DEBUG, "dir packet", result, 8); 584 585 de = parse_dirent(eon, nr, result+8, n-8, de); 586 if (last) 587 break; 588 } 589 590 rc = suunto_eonsteel_transfer(eon, CMD_DIR_CLOSE, 591 NULL, 0, result, sizeof(result), NULL); 592 if (rc != DC_STATUS_SUCCESS) { 593 ERROR(eon->base.context, "dir close failed"); 594 file_list_free(de); 595 return rc; 596 } 597 598 *res = de; 599 600 return DC_STATUS_SUCCESS; 601 } 602 603 static int 604 count_file_list(struct directory_entry *list) 605 { 606 int count = 0; 607 608 while (list) { 609 count++; 610 list = list->next; 611 } 612 613 return count; 614 } 615 616 dc_status_t 617 suunto_eonsteel_device_open(dc_device_t **out, dc_context_t *context, dc_iostream_t *iostream, unsigned int model) 618 { 619 dc_status_t status = DC_STATUS_SUCCESS; 620 suunto_eonsteel_device_t *eon = NULL; 621 dc_transport_t transport = dc_iostream_get_transport (iostream); 622 623 if (out == NULL) 624 return DC_STATUS_INVALIDARGS; 625 626 eon = (suunto_eonsteel_device_t *) dc_device_allocate(context, &suunto_eonsteel_device_vtable); 627 if (!eon) 628 return DC_STATUS_NOMEMORY; 629 630 // Set up the magic handshake fields 631 eon->model = model; 632 eon->magic = INIT_MAGIC; 633 eon->seq = INIT_SEQ; 634 memset (eon->version, 0, sizeof (eon->version)); 635 memset (eon->fingerprint, 0, sizeof (eon->fingerprint)); 636 637 if (transport == DC_TRANSPORT_BLE) { 638 status = dc_hdlc_open (&eon->iostream, context, iostream, 20, 20); 639 if (status != DC_STATUS_SUCCESS) { 640 ERROR (context, "Failed to create the HDLC stream."); 641 goto error_free; 642 } 643 } else { 644 eon->iostream = iostream; 645 } 646 647 status = dc_iostream_set_timeout(eon->iostream, 5000); 648 if (status != DC_STATUS_SUCCESS) { 649 ERROR (context, "Failed to set the timeout."); 650 goto error_free_iostream; 651 } 652 653 const unsigned char init[] = {0x02, 0x00, 0x2a, 0x00}; 654 status = suunto_eonsteel_transfer(eon, CMD_INIT, 655 init, sizeof(init), eon->version, sizeof(eon->version), NULL); 656 if (status != DC_STATUS_SUCCESS) { 657 ERROR(context, "unable to initialize device"); 658 goto error_free_iostream; 659 } 660 661 *out = (dc_device_t *) eon; 662 663 return DC_STATUS_SUCCESS; 664 665 error_free_iostream: 666 if (transport == DC_TRANSPORT_BLE) { 667 dc_iostream_close (eon->iostream); 668 } 669 error_free: 670 dc_device_deallocate ((dc_device_t *) eon); 671 return status; 672 } 673 674 static dc_status_t 675 suunto_eonsteel_device_close (dc_device_t *abstract) 676 { 677 suunto_eonsteel_device_t *device = (suunto_eonsteel_device_t *) abstract; 678 679 if (dc_iostream_get_transport (device->iostream) == DC_TRANSPORT_BLE) { 680 return dc_iostream_close (device->iostream); 681 } 682 683 return DC_STATUS_SUCCESS; 684 } 685 686 static dc_status_t 687 suunto_eonsteel_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size) 688 { 689 suunto_eonsteel_device_t *device = (suunto_eonsteel_device_t *) abstract; 690 691 if (size && size != sizeof (device->fingerprint)) 692 return DC_STATUS_INVALIDARGS; 693 694 if (size) 695 memcpy (device->fingerprint, data, sizeof (device->fingerprint)); 696 else 697 memset (device->fingerprint, 0, sizeof (device->fingerprint)); 698 699 return DC_STATUS_SUCCESS; 700 } 701 702 static dc_status_t 703 suunto_eonsteel_device_foreach(dc_device_t *abstract, dc_dive_callback_t callback, void *userdata) 704 { 705 dc_status_t status = DC_STATUS_SUCCESS; 706 dc_status_t rc = DC_STATUS_SUCCESS; 707 int skip = 0; 708 struct directory_entry *de; 709 suunto_eonsteel_device_t *eon = (suunto_eonsteel_device_t *) abstract; 710 dc_buffer_t *file; 711 char pathname[64]; 712 unsigned int time; 713 dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER; 714 715 // Emit a device info event. 716 dc_event_devinfo_t devinfo; 717 devinfo.model = eon->model; 718 devinfo.firmware = array_uint32_be (eon->version + 0x20); 719 devinfo.serial = array_convert_str2num(eon->version + 0x10, 16); 720 device_event_emit (abstract, DC_EVENT_DEVINFO, &devinfo); 721 722 rc = get_file_list(eon, &de); 723 if (rc != DC_STATUS_SUCCESS) 724 return rc; 725 726 if (de == NULL) { 727 return DC_STATUS_SUCCESS; 728 } 729 730 file = dc_buffer_new (16384); 731 if (file == NULL) { 732 ERROR (abstract->context, "Insufficient buffer space available."); 733 file_list_free(de); 734 return DC_STATUS_NOMEMORY; 735 } 736 737 progress.maximum = count_file_list(de); 738 progress.current = 0; 739 device_event_emit(abstract, DC_EVENT_PROGRESS, &progress); 740 741 while (de) { 742 int len; 743 struct directory_entry *next = de->next; 744 unsigned char buf[4]; 745 const unsigned char *data = NULL; 746 unsigned int size = 0; 747 748 if (device_is_cancelled(abstract)) { 749 dc_status_set_error(&status, DC_STATUS_CANCELLED); 750 skip = 1; 751 } 752 753 switch (de->type) { 754 case DIRTYPE_DIR: 755 /* Ignore subdirectories in the dive directory */ 756 break; 757 case DIRTYPE_FILE: 758 if (skip) 759 break; 760 761 if (sscanf(de->name, "%x.LOG", &time) != 1) { 762 dc_status_set_error(&status, DC_STATUS_PROTOCOL); 763 break; 764 } 765 766 array_uint32_le_set(buf, time); 767 768 if (memcmp (buf, eon->fingerprint, sizeof (eon->fingerprint)) == 0) { 769 skip = 1; 770 break; 771 } 772 773 len = dc_platform_snprintf(pathname, sizeof(pathname), "%s/%s", dive_directory, de->name); 774 if (len < 0 || (unsigned int) len >= sizeof(pathname)) { 775 dc_status_set_error(&status, DC_STATUS_PROTOCOL); 776 break; 777 } 778 779 // Reset the membuffer, put the 4-byte length at the head. 780 dc_buffer_clear(file); 781 dc_buffer_append(file, buf, 4); 782 783 // Then read the filename into the rest of the buffer 784 rc = read_file(eon, pathname, file); 785 if (rc != DC_STATUS_SUCCESS) { 786 dc_status_set_error(&status, rc); 787 break; 788 } 789 790 data = dc_buffer_get_data(file); 791 size = dc_buffer_get_size(file); 792 793 if (callback && !callback(data, size, data, sizeof(eon->fingerprint), userdata)) 794 skip = 1; 795 } 796 progress.current++; 797 device_event_emit(abstract, DC_EVENT_PROGRESS, &progress); 798 799 free(de); 800 de = next; 801 } 802 dc_buffer_free(file); 803 804 return status; 805 } 806 807 static dc_status_t suunto_eonsteel_device_timesync(dc_device_t *abstract, const dc_datetime_t *datetime) 808 { 809 suunto_eonsteel_device_t *eon = (suunto_eonsteel_device_t *) abstract; 810 dc_status_t rc = DC_STATUS_SUCCESS; 811 unsigned char result[64], cmd[8]; 812 unsigned int year, month, day; 813 unsigned int hour, min, msec; 814 815 year = datetime->year; 816 month = datetime->month; 817 day = datetime->day; 818 hour = datetime->hour; 819 min = datetime->minute; 820 msec = datetime->second * 1000; 821 822 cmd[0] = year & 0xFF; 823 cmd[1] = year >> 8; 824 cmd[2] = month; 825 cmd[3] = day; 826 cmd[4] = hour; 827 cmd[5] = min; 828 cmd[6] = msec & 0xFF; 829 cmd[7] = msec >> 8; 830 831 rc = suunto_eonsteel_transfer(eon, CMD_SET_TIME, cmd, sizeof(cmd), result, sizeof(result), NULL); 832 if (rc != DC_STATUS_SUCCESS) { 833 return rc; 834 } 835 836 rc = suunto_eonsteel_transfer(eon, CMD_SET_DATE, cmd, sizeof(cmd), result, sizeof(result), NULL); 837 if (rc != DC_STATUS_SUCCESS) { 838 return rc; 839 } 840 841 return DC_STATUS_SUCCESS; 842 }