halcyon_symbios.c (18198B)
1 /* 2 * libdivecomputer 3 * 4 * Copyright (C) 2023 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> 23 #include <stdlib.h> 24 #include <stdio.h> 25 26 #include "halcyon_symbios.h" 27 #include "context-private.h" 28 #include "device-private.h" 29 #include "platform.h" 30 #include "checksum.h" 31 #include "array.h" 32 33 #define CMD_GET_STATUS 0x01 34 #define CMD_GET_SETTINGS 0x02 35 #define CMD_SET_SETTINGS 0x03 36 #define CMD_LOGBOOK_REQUEST 0x04 37 #define CMD_DIVELOG_REQUEST 0x05 38 #define CMD_SET_TIME 0x07 39 #define CMD_LOGBOOK_BLOCK 0x08 40 #define CMD_DIVELOG_BLOCK 0x09 41 42 #define CMD_RESPONSE 0x80 43 44 #define ERR_BASE 0x80000000 45 #define ERR_CRC 0 46 #define ERR_BOUNDARY 1 47 #define ERR_CMD_LENGTH 2 48 #define ERR_CMD_UNKNOWN 3 49 #define ERR_TIMEOUT 4 50 #define ERR_FILE 5 51 #define ERR_UNKNOWN 6 52 53 #define ACK 0x06 54 #define NAK 0x15 55 56 #define MAXRETRIES 3 57 58 #define MAXPACKET 256 59 60 #define SZ_BLOCK 200 61 #define SZ_LOGBOOK 32 62 63 #define FP_OFFSET 20 64 #define FP_SIZE 4 65 66 #define NSTEPS 1000 67 #define STEP(i,n) (NSTEPS * (i) / (n)) 68 69 typedef struct halcyon_symbios_device_t { 70 dc_device_t base; 71 dc_iostream_t *iostream; 72 unsigned char fingerprint[FP_SIZE]; 73 } halcyon_symbios_device_t; 74 75 static dc_status_t halcyon_symbios_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size); 76 static dc_status_t halcyon_symbios_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata); 77 static dc_status_t halcyon_symbios_device_timesync (dc_device_t *abstract, const dc_datetime_t *datetime); 78 79 static const dc_device_vtable_t halcyon_symbios_device_vtable = { 80 sizeof(halcyon_symbios_device_t), 81 DC_FAMILY_HALCYON_SYMBIOS, 82 halcyon_symbios_device_set_fingerprint, /* set_fingerprint */ 83 NULL, /* read */ 84 NULL, /* write */ 85 NULL, /* dump */ 86 halcyon_symbios_device_foreach, /* foreach */ 87 halcyon_symbios_device_timesync, /* timesync */ 88 NULL, /* close */ 89 }; 90 91 static dc_status_t 92 halcyon_symbios_send (halcyon_symbios_device_t *device, unsigned char cmd, const unsigned char data[], unsigned int size) 93 { 94 dc_status_t status = DC_STATUS_SUCCESS; 95 dc_device_t *abstract = (dc_device_t *) device; 96 unsigned char packet[1 + MAXPACKET + 1] = {0}; 97 unsigned int length = 1; 98 99 if (device_is_cancelled (abstract)) 100 return DC_STATUS_CANCELLED; 101 102 if (size > MAXPACKET) 103 return DC_STATUS_INVALIDARGS; 104 105 // Setup the data packet 106 packet[0] = cmd; 107 if (size) { 108 memcpy (packet + 1, data, size); 109 packet[1 + size] = checksum_crc8 (data, size, 0x00, 0x00); 110 length += size + 1; 111 } 112 113 // Send the data packet. 114 status = dc_iostream_write (device->iostream, packet, length, NULL); 115 if (status != DC_STATUS_SUCCESS) { 116 ERROR (abstract->context, "Failed to send the command."); 117 return status; 118 } 119 120 return DC_STATUS_SUCCESS; 121 } 122 123 static dc_status_t 124 halcyon_symbios_recv (halcyon_symbios_device_t *device, unsigned char cmd, unsigned char data[], unsigned int size, unsigned int *actual, unsigned int *errorcode) 125 { 126 dc_status_t status = DC_STATUS_SUCCESS; 127 dc_device_t *abstract = (dc_device_t *) device; 128 unsigned char packet[2 + MAXPACKET + 1] = {0}; 129 unsigned int length = 0; 130 unsigned int errcode = 0; 131 132 // Receive the answer. 133 size_t len = 0; 134 status = dc_iostream_read (device->iostream, packet, sizeof(packet), &len); 135 if (status != DC_STATUS_SUCCESS) { 136 ERROR (abstract->context, "Failed to receive the packet."); 137 goto error_exit; 138 } 139 140 // Verify the minimum length of the packet. 141 if (len < 3) { 142 ERROR (abstract->context, "Unexpected packet length (" DC_PRINTF_SIZE ").", len); 143 status = DC_STATUS_PROTOCOL; 144 goto error_exit; 145 } 146 147 // Verify the checksum. 148 unsigned char crc = packet[len - 1]; 149 unsigned char ccrc = checksum_crc8 (packet + 1, len - 2, 0x00, 0x00); 150 if (crc != ccrc) { 151 ERROR (abstract->context, "Unexpected packet checksum (%02x %02x).", crc, ccrc); 152 status = DC_STATUS_PROTOCOL; 153 goto error_exit; 154 } 155 156 // Verify the command byte. 157 unsigned int rsp = packet[0]; 158 unsigned int expected = cmd | CMD_RESPONSE; 159 if (rsp != expected) { 160 ERROR (abstract->context, "Unexpected command byte (%02x).", rsp); 161 status = DC_STATUS_PROTOCOL; 162 goto error_exit; 163 } 164 165 // Verify the ACK/NAK byte. 166 unsigned int type = packet[1]; 167 if (type != ACK && type != NAK) { 168 ERROR (abstract->context, "Unexpected ACK/NAK byte (%02x).", type); 169 status = DC_STATUS_PROTOCOL; 170 goto error_exit; 171 } 172 173 // Get the error code from a NAK packet. 174 if (type == NAK) { 175 // Verify the length of the NAK packet. 176 if (len != 4) { 177 ERROR (abstract->context, "Unexpected NAK packet length (" DC_PRINTF_SIZE ").", len); 178 status = DC_STATUS_PROTOCOL; 179 goto error_exit; 180 } 181 182 // Set the ERR_BASE bit to indicate an error code is available. 183 errcode = packet[2] | ERR_BASE; 184 185 ERROR (abstract->context, "Received NAK packet with error code %u.", errcode & ~ERR_BASE); 186 status = DC_STATUS_PROTOCOL; 187 goto error_exit; 188 } 189 190 // Verify the maximum length of the packet. 191 if (len - 3 > size) { 192 ERROR (abstract->context, "Unexpected packet length (" DC_PRINTF_SIZE ").", len); 193 status = DC_STATUS_PROTOCOL; 194 goto error_exit; 195 } 196 197 if (len - 3) { 198 memcpy (data, packet + 2, len - 3); 199 } 200 length = len - 3; 201 202 error_exit: 203 if (actual) { 204 *actual = length; 205 } 206 if (errorcode) { 207 *errorcode = errcode; 208 } 209 return status; 210 } 211 212 static dc_status_t 213 halcyon_symbios_transfer (halcyon_symbios_device_t *device, unsigned char cmd, const unsigned char data[], unsigned int size, unsigned char answer[], unsigned int asize, unsigned int *actual, unsigned int *errorcode) 214 { 215 dc_status_t status = DC_STATUS_SUCCESS; 216 dc_device_t *abstract = (dc_device_t *) device; 217 unsigned int errcode = 0; 218 219 // Send the command. 220 status = halcyon_symbios_send (device, cmd, data, size); 221 if (status != DC_STATUS_SUCCESS) { 222 ERROR (abstract->context, "Failed to send the command."); 223 goto error_exit; 224 } 225 226 // Receive the answer. 227 unsigned int length = 0; 228 status = halcyon_symbios_recv (device, cmd, answer, asize, &length, &errcode); 229 if (status != DC_STATUS_SUCCESS) { 230 ERROR (abstract->context, "Failed to receive the answer."); 231 goto error_exit; 232 } 233 234 if (actual == NULL) { 235 // Verify the length of the packet. 236 if (length != asize) { 237 ERROR (abstract->context, "Unexpected packet length (%u).", length); 238 status = DC_STATUS_PROTOCOL; 239 goto error_exit; 240 } 241 } else { 242 // Return the actual length. 243 *actual = length; 244 } 245 246 error_exit: 247 if (errorcode) { 248 *errorcode = errcode; 249 } 250 return status; 251 } 252 253 static dc_status_t 254 halcyon_symbios_download (halcyon_symbios_device_t *device, dc_event_progress_t *progress, unsigned char request, const unsigned char data[], unsigned int size, unsigned char block, dc_buffer_t *buffer, unsigned int *errorcode) 255 { 256 dc_status_t status = DC_STATUS_SUCCESS; 257 dc_device_t *abstract = (dc_device_t *) device; 258 unsigned int errcode = 0; 259 260 // Clear the buffer. 261 dc_buffer_clear (buffer); 262 263 // Request the data. 264 unsigned char response[4] = {0}; 265 status = halcyon_symbios_transfer (device, request, data, size, response, sizeof(response), NULL, &errcode); 266 if (status != DC_STATUS_SUCCESS) { 267 ERROR (abstract->context, "Failed to request the data."); 268 goto error_exit; 269 } 270 271 // Get the total length. 272 unsigned int length = array_uint32_le (response); 273 274 // Resize the buffer. 275 if (!dc_buffer_reserve (buffer, length)) { 276 ERROR (abstract->context, "Failed to allocate memory."); 277 status = DC_STATUS_NOMEMORY; 278 goto error_exit; 279 } 280 281 // Send the request for the first data block. 282 status = halcyon_symbios_send (device, block, NULL, 0); 283 if (status != DC_STATUS_SUCCESS) { 284 ERROR (abstract->context, "Failed to send the command."); 285 goto error_exit; 286 } 287 288 const unsigned int initial = progress ? progress->current : 0; 289 290 unsigned int counter = 1; 291 unsigned int nbytes = 0; 292 while (1) { 293 // Receive the data block. 294 unsigned int len = 0; 295 unsigned char payload[2 + SZ_BLOCK] = {0}; 296 unsigned int nretries = 0; 297 while ((status = halcyon_symbios_recv (device, block, payload, sizeof(payload), &len, NULL)) != DC_STATUS_SUCCESS) { 298 // Abort if the error is fatal. 299 if (status != DC_STATUS_PROTOCOL && status != DC_STATUS_TIMEOUT) { 300 ERROR (abstract->context, "Failed to receive the answer."); 301 goto error_exit; 302 } 303 304 // Abort if the maximum number of retries is reached. 305 if (nretries++ >= MAXRETRIES) { 306 ERROR (abstract->context, "Reached the maximum number of retries."); 307 goto error_exit; 308 } 309 310 // If the error was a timeout, the packet may have arrived 311 // late and be sitting in the receive queue. Flush it before 312 // requesting a re-transmission to avoid reading the stale 313 // copy instead of the retransmitted block. 314 if (status == DC_STATUS_TIMEOUT) { 315 // AI-generated (Claude) 316 // Poll processes transport events while waiting for late data. 317 status = dc_iostream_poll (device->iostream, 300); 318 if (status != DC_STATUS_SUCCESS && status != DC_STATUS_TIMEOUT) { 319 ERROR (abstract->context, "Failed to wait for late data."); 320 goto error_exit; 321 } 322 323 status = dc_iostream_purge (device->iostream, DC_DIRECTION_INPUT); 324 if (status != DC_STATUS_SUCCESS) { 325 ERROR (abstract->context, "Failed to purge late data."); 326 goto error_exit; 327 } 328 } 329 330 // Send a NAK to request a re-transmission. 331 status = halcyon_symbios_send (device, NAK, NULL, 0); 332 if (status != DC_STATUS_SUCCESS) { 333 ERROR (abstract->context, "Failed to send the NAK."); 334 goto error_exit; 335 } 336 } 337 338 // Verify the minimum block length. 339 if (len < 2) { 340 ERROR (abstract->context, "Unexpected block length (%u).", len); 341 status = DC_STATUS_PROTOCOL; 342 goto error_exit; 343 } 344 345 // Verify the sequence number. 346 unsigned int id = array_uint16_le (payload); 347 unsigned int seqnum = id & 0x7FFF; 348 if (seqnum != counter) { 349 ERROR (abstract->context, "Unexpected block sequence number (%04x %04x).", seqnum, counter); 350 status = DC_STATUS_PROTOCOL; 351 goto error_exit; 352 } 353 354 // Append the payload data to the output buffer. 355 if (!dc_buffer_append (buffer, payload + 2, len - 2)) { 356 ERROR (abstract->context, "Failed to allocate memory."); 357 status = DC_STATUS_NOMEMORY; 358 goto error_exit; 359 } 360 361 nbytes += len - 2; 362 counter += 1; 363 counter &= 0x7FFF; 364 365 // Update and emit a progress event. 366 if (progress) { 367 // Limit the progress events to the announced length. 368 unsigned int n = nbytes > length ? length : nbytes; 369 progress->current = initial + STEP(n, length); 370 device_event_emit (abstract, DC_EVENT_PROGRESS, progress); 371 } 372 373 // Send an ACK to request the next block or finalize the download. 374 status = halcyon_symbios_send (device, ACK, NULL, 0); 375 if (status != DC_STATUS_SUCCESS) { 376 ERROR (abstract->context, "Failed to send the ACK."); 377 goto error_exit; 378 } 379 380 // Check for the last packet. 381 if (id & 0x8000) 382 break; 383 } 384 385 // Verify the length of the data. 386 if (nbytes != length) { 387 ERROR (abstract->context, "Unexpected data length (%u %u).", nbytes, length); 388 status = DC_STATUS_PROTOCOL; 389 goto error_exit; 390 } 391 392 error_exit: 393 if (errorcode) { 394 *errorcode = errcode; 395 } 396 return status; 397 } 398 399 dc_status_t 400 halcyon_symbios_device_open (dc_device_t **out, dc_context_t *context, dc_iostream_t *iostream) 401 { 402 dc_status_t status = DC_STATUS_SUCCESS; 403 halcyon_symbios_device_t *device = NULL; 404 405 if (out == NULL) 406 return DC_STATUS_INVALIDARGS; 407 408 // Allocate memory. 409 device = (halcyon_symbios_device_t *) dc_device_allocate (context, &halcyon_symbios_device_vtable); 410 if (device == NULL) { 411 ERROR (context, "Failed to allocate memory."); 412 return DC_STATUS_NOMEMORY; 413 } 414 415 // Set the default values. 416 device->iostream = iostream; 417 memset(device->fingerprint, 0, sizeof(device->fingerprint)); 418 419 // Set the timeout for receiving data (5000ms). 420 status = dc_iostream_set_timeout (device->iostream, 5000); 421 if (status != DC_STATUS_SUCCESS) { 422 ERROR (context, "Failed to set the timeout."); 423 goto error_free; 424 } 425 426 // Make sure everything is in a sane state. 427 dc_iostream_purge (device->iostream, DC_DIRECTION_ALL); 428 429 *out = (dc_device_t *) device; 430 431 return DC_STATUS_SUCCESS; 432 433 error_free: 434 dc_device_deallocate ((dc_device_t *) device); 435 return status; 436 } 437 438 static dc_status_t 439 halcyon_symbios_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size) 440 { 441 halcyon_symbios_device_t *device = (halcyon_symbios_device_t *) abstract; 442 443 if (size && size != sizeof (device->fingerprint)) 444 return DC_STATUS_INVALIDARGS; 445 446 if (size) 447 memcpy (device->fingerprint, data, sizeof (device->fingerprint)); 448 else 449 memset (device->fingerprint, 0, sizeof (device->fingerprint)); 450 451 return DC_STATUS_SUCCESS; 452 } 453 454 static dc_status_t 455 halcyon_symbios_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata) 456 { 457 dc_status_t status = DC_STATUS_SUCCESS; 458 halcyon_symbios_device_t *device = (halcyon_symbios_device_t *) abstract; 459 unsigned int errcode = 0; 460 461 // Enable progress notifications. 462 dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER; 463 device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); 464 465 // Read the device status. 466 unsigned char info[36] = {0}; 467 unsigned int info_size = 0; 468 status = halcyon_symbios_transfer (device, CMD_GET_STATUS, NULL, 0, info, sizeof(info), &info_size, NULL); 469 if (status != DC_STATUS_SUCCESS) { 470 ERROR (abstract->context, "Failed to read the device status."); 471 goto error_exit; 472 } 473 474 // Verify the length of the packet. 475 if (info_size < 20) { 476 ERROR (abstract->context, "Unexpected packet length (%u).", info_size); 477 status = DC_STATUS_PROTOCOL; 478 goto error_exit; 479 } 480 481 HEXDUMP (abstract->context, DC_LOGLEVEL_DEBUG, "Version", info, info_size); 482 483 // Emit a vendor event. 484 dc_event_vendor_t vendor; 485 vendor.data = info; 486 vendor.size = info_size; 487 device_event_emit (abstract, DC_EVENT_VENDOR, &vendor); 488 489 // Emit a device info event. 490 dc_event_devinfo_t devinfo; 491 devinfo.model = info[5]; 492 devinfo.firmware = array_uint24_be (info + 16); 493 devinfo.serial = array_uint32_le (info); 494 device_event_emit (abstract, DC_EVENT_DEVINFO, &devinfo); 495 496 DEBUG (abstract->context, "Device: model=%u, serial=%u, firmware=%u.%u.%u, hw=%u, bt=%u.%u, battery=%u, pressure=%u, errorbits=%u", 497 info[5], 498 array_uint32_le (info), 499 info[16], info[17], info[18], 500 info[4], info[6], info[7], 501 array_uint16_le (info + 8), 502 array_uint16_le (info + 10), 503 array_uint32_le (info + 12)); 504 505 dc_buffer_t *logbook = dc_buffer_new (0); 506 dc_buffer_t *dive = dc_buffer_new (0); 507 if (logbook == NULL || dive == NULL) { 508 ERROR (abstract->context, "Failed to allocate memory."); 509 status = DC_STATUS_NOMEMORY; 510 goto error_free; 511 } 512 513 // Download the logbook. 514 status = halcyon_symbios_download (device, &progress, CMD_LOGBOOK_REQUEST, NULL, 0, CMD_LOGBOOK_BLOCK, logbook, &errcode); 515 if (status != DC_STATUS_SUCCESS) { 516 if (errcode == (ERR_FILE | ERR_BASE)) { 517 WARNING (abstract->context, "Logbook not available!"); 518 519 // Update and emit a progress event. 520 progress.current = NSTEPS; 521 progress.maximum = NSTEPS; 522 device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); 523 524 status = DC_STATUS_SUCCESS; 525 goto error_free; 526 } 527 ERROR (abstract->context, "Failed to download the logbook."); 528 goto error_free; 529 } 530 531 const unsigned char *data = dc_buffer_get_data (logbook); 532 size_t size = dc_buffer_get_size (logbook); 533 534 HEXDUMP (abstract->context, DC_LOGLEVEL_DEBUG, "Logbook", data, size); 535 536 // Get the number of dives. 537 unsigned int ndives = 0; 538 unsigned int offset = size; 539 while (offset >= SZ_LOGBOOK) { 540 offset -= SZ_LOGBOOK; 541 542 // Compare the fingerprint to identify previously downloaded entries. 543 if (memcmp (data + offset + FP_OFFSET, device->fingerprint, sizeof(device->fingerprint)) == 0) { 544 break; 545 } 546 547 ndives++; 548 } 549 550 // Update and emit a progress event. 551 progress.current = 1 * NSTEPS; 552 progress.maximum = (ndives + 1) * NSTEPS; 553 device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); 554 555 offset = size; 556 for (unsigned int i = 0; i < ndives; ++i) { 557 offset -= SZ_LOGBOOK; 558 559 // Clear the buffer. 560 dc_buffer_clear (dive); 561 562 // Download the dive. 563 status = halcyon_symbios_download (device, &progress, CMD_DIVELOG_REQUEST, data + offset + 16, 2, CMD_DIVELOG_BLOCK, dive, &errcode); 564 if (status != DC_STATUS_SUCCESS) { 565 if (errcode == (ERR_FILE | ERR_BASE)) { 566 WARNING (abstract->context, "Dive #%u not available!", 567 array_uint16_le (data + offset + 16)); 568 569 // Update and emit a progress event. 570 progress.current = (i + 2) * NSTEPS; 571 device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); 572 573 status = DC_STATUS_SUCCESS; 574 continue; 575 } 576 ERROR (abstract->context, "Failed to download the dive."); 577 goto error_free; 578 } 579 580 if (callback && !callback (dc_buffer_get_data (dive), dc_buffer_get_size (dive), data + offset + FP_OFFSET, FP_SIZE, userdata)) { 581 break; 582 } 583 } 584 585 error_free: 586 dc_buffer_free (dive); 587 dc_buffer_free (logbook); 588 error_exit: 589 return status; 590 } 591 592 static dc_status_t 593 halcyon_symbios_device_timesync (dc_device_t *abstract, const dc_datetime_t *datetime) 594 { 595 dc_status_t status = DC_STATUS_SUCCESS; 596 halcyon_symbios_device_t *device = (halcyon_symbios_device_t *) abstract; 597 598 unsigned char request[] = { 599 datetime->year - 2000, 600 datetime->month, 601 datetime->day, 602 datetime->hour, 603 datetime->minute, 604 datetime->second, 605 }; 606 status = halcyon_symbios_transfer (device, CMD_SET_TIME, request, sizeof(request), NULL, 0, NULL, NULL); 607 if (status != DC_STATUS_SUCCESS) { 608 ERROR (abstract->context, "Failed to set the time."); 609 goto error_exit; 610 } 611 612 error_exit: 613 return status; 614 }