mclean_extreme.c (17869B)
1 /* 2 * libdivecomputer 3 * 4 * Copyright (C) 2020 Jef Driesen, David Carron 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 25 #include "mclean_extreme.h" 26 #include "context-private.h" 27 #include "device-private.h" 28 #include "array.h" 29 #include "packet.h" 30 31 #define ISINSTANCE(device) dc_device_isinstance((device), &mclean_extreme_device_vtable) 32 33 #define MAXRETRIES 14 34 35 #define STX 0x7E 36 37 #define CMD_SERIALNUMBER 0x91 38 #define CMD_COMPUTER 0xA0 39 #define CMD_SET_COMPUTER 0xA1 40 #define CMD_DIVE 0xA3 41 #define CMD_CLOSE 0xAA 42 #define CMD_SET_TIME 0xAC 43 #define CMD_FIRMWARE 0xAD 44 45 #define SZ_PACKET 512 46 #define SZ_FINGERPRINT 4 47 #define SZ_CFG 0x002D 48 #define SZ_COMPUTER (SZ_CFG + 0x6A) 49 #define SZ_HEADER (SZ_CFG + 0x31) 50 #define SZ_SAMPLE 0x0004 51 52 #define EPOCH 946684800 // 2000-01-01 00:00:00 UTC 53 54 #define NSTEPS 1000 55 #define STEP(i,n) (NSTEPS * (i) / (n)) 56 57 typedef struct mclean_extreme_device_t { 58 dc_device_t base; 59 dc_iostream_t *iostream; 60 unsigned char fingerprint[SZ_FINGERPRINT]; 61 } mclean_extreme_device_t; 62 63 static dc_status_t mclean_extreme_device_set_fingerprint(dc_device_t *abstract, const unsigned char data[], unsigned int size); 64 static dc_status_t mclean_extreme_device_timesync(dc_device_t *abstract, const dc_datetime_t *datetime); 65 static dc_status_t mclean_extreme_device_foreach(dc_device_t *abstract, dc_dive_callback_t callback, void *userdata); 66 static dc_status_t mclean_extreme_device_close(dc_device_t *abstract); 67 68 static const dc_device_vtable_t mclean_extreme_device_vtable = { 69 sizeof(mclean_extreme_device_t), 70 DC_FAMILY_MCLEAN_EXTREME, 71 mclean_extreme_device_set_fingerprint, /* set_fingerprint */ 72 NULL, /* read */ 73 NULL, /* write */ 74 NULL, /* dump */ 75 mclean_extreme_device_foreach, /* foreach */ 76 mclean_extreme_device_timesync, /* timesync */ 77 mclean_extreme_device_close, /* close */ 78 }; 79 80 static unsigned int 81 hashcode (const unsigned char data[], size_t size) 82 { 83 unsigned int result = 0; 84 85 for (size_t i = 0; i < size; ++i) { 86 result *= 31; 87 result += data[i]; 88 } 89 90 return result; 91 } 92 93 static unsigned short 94 checksum_crc(const unsigned char data[], unsigned int size, unsigned short init) 95 { 96 unsigned short crc = init; 97 for (unsigned int i = 0; i < size; ++i) { 98 crc ^= data[i] << 8; 99 if (crc & 0x8000) { 100 crc <<= 1; 101 crc ^= 0x1021; 102 } else { 103 crc <<= 1; 104 } 105 } 106 107 return crc; 108 } 109 110 static dc_status_t 111 mclean_extreme_send(mclean_extreme_device_t *device, unsigned char cmd, const unsigned char data[], size_t size) 112 { 113 dc_status_t status = DC_STATUS_SUCCESS; 114 dc_device_t *abstract = (dc_device_t *)device; 115 unsigned short crc = 0; 116 117 if (device_is_cancelled(abstract)) 118 return DC_STATUS_CANCELLED; 119 120 if (size > SZ_PACKET) 121 return DC_STATUS_INVALIDARGS; 122 123 // Setup the data packet 124 unsigned char packet[SZ_PACKET + 11] = { 125 STX, 126 0x00, 127 (size >> 0) & 0xFF, 128 (size >> 8) & 0xFF, 129 (size >> 16) & 0xFF, 130 (size >> 24) & 0xFF, 131 cmd, 132 }; 133 if (size) { 134 memcpy(packet + 7, data, size); 135 } 136 crc = checksum_crc(packet + 1, size + 6, 0); 137 packet[size + 7] = (crc >> 8) & 0xFF; 138 packet[size + 8] = (crc) & 0xFF; 139 packet[size + 9] = 0x00; 140 packet[size + 10] = 0x00; 141 142 // Give the dive computer some extra time. 143 dc_iostream_sleep(device->iostream, 300); 144 145 // Send the data packet. 146 status = dc_iostream_write(device->iostream, packet, size + 11, NULL); 147 if (status != DC_STATUS_SUCCESS) { 148 ERROR(abstract->context, "Failed to send the command."); 149 return status; 150 } 151 152 return DC_STATUS_SUCCESS; 153 } 154 155 static dc_status_t 156 mclean_extreme_receive(mclean_extreme_device_t *device, unsigned char rsp, unsigned char data[], size_t size, size_t *actual) 157 { 158 dc_status_t status = DC_STATUS_SUCCESS; 159 dc_device_t *abstract = (dc_device_t *)device; 160 unsigned char header[7]; 161 unsigned int nretries = 0; 162 163 // Read the packet start byte. 164 // Unfortunately it takes a relative long time, about 6-8 seconds, 165 // before the STX byte arrives. Hence the standard timeout of one 166 // second is not sufficient, and we need to retry a few times on 167 // timeout. The advantage over using a single read operation with a 168 // large timeout is that we can give the user a chance to cancel the 169 // operation. 170 while (1) { 171 status = dc_iostream_read(device->iostream, header + 0, 1, NULL); 172 if (status != DC_STATUS_SUCCESS) { 173 if (status != DC_STATUS_TIMEOUT) { 174 ERROR(abstract->context, "Failed to receive the packet start byte."); 175 return status; 176 } 177 178 // Abort if the maximum number of retries is reached. 179 if (nretries++ >= MAXRETRIES) 180 return status; 181 182 // Cancel if requested by the user. 183 if (device_is_cancelled(abstract)) 184 return DC_STATUS_CANCELLED; 185 186 // Try again. 187 continue; 188 } 189 190 if (header[0] == STX) 191 break; 192 193 // Reset the retry counter. 194 nretries = 0; 195 } 196 197 // Read the packet header. 198 status = dc_iostream_read(device->iostream, header + 1, sizeof(header) - 1, NULL); 199 if (status != DC_STATUS_SUCCESS) { 200 ERROR(abstract->context, "Failed to receive the packet header."); 201 return status; 202 } 203 204 // Verify the type byte. 205 unsigned int type = header[1]; 206 if (type != 0x00) { 207 ERROR(abstract->context, "Unexpected type byte (%02x).", type); 208 return DC_STATUS_PROTOCOL; 209 } 210 211 // Verify the length. 212 unsigned int length = array_uint32_le(header + 2); 213 if (length > size) { 214 ERROR(abstract->context, "Unexpected packet length (%u).", length); 215 return DC_STATUS_PROTOCOL; 216 } 217 218 // Get the command type. 219 unsigned int cmd = header[6]; 220 if (cmd != rsp) { 221 ERROR(abstract->context, "Unexpected command byte (%02x).", cmd); 222 return DC_STATUS_PROTOCOL; 223 } 224 225 size_t nbytes = 0; 226 while (nbytes < length) { 227 // Set the maximum packet size. 228 size_t len = 1000; 229 230 // Limit the packet size to the total size. 231 if (nbytes + len > length) 232 len = length - nbytes; 233 234 // Read the packet payload. 235 status = dc_iostream_read(device->iostream, data + nbytes, len, NULL); 236 if (status != DC_STATUS_SUCCESS) { 237 ERROR(abstract->context, "Failed to receive the packet payload."); 238 return status; 239 } 240 241 nbytes += len; 242 } 243 244 // Read the packet checksum. 245 unsigned char checksum[4]; 246 status = dc_iostream_read(device->iostream, checksum, sizeof(checksum), NULL); 247 if (status != DC_STATUS_SUCCESS) { 248 ERROR(abstract->context, "Failed to receive the packet checksum."); 249 return status; 250 } 251 252 // Verify the checksum. 253 unsigned short crc = array_uint16_be(checksum); 254 unsigned short ccrc = 0; 255 ccrc = checksum_crc(header + 1, sizeof(header) - 1, ccrc); 256 ccrc = checksum_crc(data, length, ccrc); 257 if (crc != ccrc || checksum[2] != 0x00 || checksum[3] != 0) { 258 ERROR(abstract->context, "Unexpected packet checksum."); 259 return DC_STATUS_PROTOCOL; 260 } 261 262 if (actual == NULL) { 263 // Verify the actual length. 264 if (length != size) { 265 ERROR (abstract->context, "Unexpected packet length (%u).", length); 266 return DC_STATUS_PROTOCOL; 267 } 268 } else { 269 // Return the actual length. 270 *actual = length; 271 } 272 273 return DC_STATUS_SUCCESS; 274 } 275 276 static dc_status_t 277 mclean_extreme_transfer(mclean_extreme_device_t *device, unsigned char cmd, const unsigned char data[], size_t size, unsigned char answer[], size_t asize, size_t *actual) 278 { 279 dc_status_t status = DC_STATUS_SUCCESS; 280 281 // Send the command 282 status = mclean_extreme_send(device, cmd, data, size); 283 if (status != DC_STATUS_SUCCESS) { 284 return status; 285 } 286 287 // Receive the answer 288 if (asize) { 289 status = mclean_extreme_receive(device, cmd, answer, asize, actual); 290 if (status != DC_STATUS_SUCCESS) { 291 return status; 292 } 293 } 294 295 return DC_STATUS_SUCCESS; 296 } 297 298 static dc_status_t 299 mclean_extreme_readdive (dc_device_t *abstract, dc_event_progress_t *progress, unsigned int idx, dc_buffer_t *buffer) 300 { 301 dc_status_t status = DC_STATUS_SUCCESS; 302 mclean_extreme_device_t *device = (mclean_extreme_device_t *) abstract; 303 304 // Erase the buffer. 305 dc_buffer_clear (buffer); 306 307 // Encode the logbook ID. 308 unsigned char id[] = { 309 (idx ) & 0xFF, 310 (idx >> 8) & 0xFF, 311 }; 312 313 // Update and emit a progress event. 314 unsigned int initial = 0; 315 if (progress) { 316 initial = progress->current; 317 device_event_emit (abstract, DC_EVENT_PROGRESS, progress); 318 } 319 320 // Request the dive. 321 status = mclean_extreme_send (device, CMD_DIVE, id, sizeof(id)); 322 if (status != DC_STATUS_SUCCESS) { 323 ERROR (abstract->context, "Failed to send the dive command."); 324 return status; 325 } 326 327 // Read the dive header. 328 unsigned char header[SZ_HEADER]; 329 status = mclean_extreme_receive (device, CMD_DIVE, header, sizeof(header), NULL); 330 if (status != DC_STATUS_SUCCESS) { 331 ERROR (abstract->context, "Failed to receive the dive header."); 332 return status; 333 } 334 335 // Verify the format version. 336 unsigned int format = header[0x0000]; 337 if (format != 0) { 338 ERROR(abstract->context, "Unrecognised dive format."); 339 return DC_STATUS_DATAFORMAT; 340 } 341 342 // Get the number of samples. 343 unsigned int nsamples = array_uint16_le (header + 0x005C); 344 345 // Calculate the total size. 346 unsigned int size = sizeof(header) + nsamples * SZ_SAMPLE; 347 348 // Update and emit a progress event. 349 if (progress) { 350 progress->current = initial + STEP(sizeof(header), size); 351 device_event_emit (abstract, DC_EVENT_PROGRESS, progress); 352 } 353 354 // Allocate memory for the dive. 355 if (!dc_buffer_resize (buffer, size)) { 356 ERROR (abstract->context, "Insufficient buffer space available."); 357 return DC_STATUS_NOMEMORY; 358 } 359 360 // Cache the pointer. 361 unsigned char *data = dc_buffer_get_data(buffer); 362 363 // Append the header. 364 memcpy (data, header, sizeof(header)); 365 366 unsigned int nbytes = sizeof(header); 367 while (nbytes < size) { 368 // Get the maximum packet size. 369 size_t len = size - nbytes; 370 371 // Read the dive samples. 372 status = mclean_extreme_receive (device, CMD_DIVE, data + nbytes, len, &len); 373 if (status != DC_STATUS_SUCCESS) { 374 ERROR (abstract->context, "Failed to receive the dive samples."); 375 return status; 376 } 377 378 nbytes += len; 379 380 // Update and emit a progress event. 381 if (progress) { 382 progress->current = initial + STEP(nbytes, size); 383 device_event_emit (abstract, DC_EVENT_PROGRESS, progress); 384 } 385 } 386 387 return DC_STATUS_SUCCESS; 388 } 389 390 dc_status_t 391 mclean_extreme_device_open(dc_device_t **out, dc_context_t *context, dc_iostream_t *iostream) 392 { 393 dc_status_t status = DC_STATUS_SUCCESS; 394 mclean_extreme_device_t *device = NULL; 395 dc_transport_t transport = dc_iostream_get_transport (iostream); 396 397 if (out == NULL) 398 return DC_STATUS_INVALIDARGS; 399 400 // Allocate memory. 401 device = (mclean_extreme_device_t *)dc_device_allocate(context, &mclean_extreme_device_vtable); 402 if (device == NULL) { 403 ERROR(context, "Failed to allocate memory."); 404 return DC_STATUS_NOMEMORY; 405 } 406 407 // Set the default values. 408 memset(device->fingerprint, 0, sizeof(device->fingerprint)); 409 410 // Create the packet stream. 411 if (transport == DC_TRANSPORT_BLE) { 412 status = dc_packet_open (&device->iostream, context, iostream, 244, 244); 413 if (status != DC_STATUS_SUCCESS) { 414 ERROR (context, "Failed to create the packet stream."); 415 goto error_free; 416 } 417 } else { 418 device->iostream = iostream; 419 } 420 421 // Set the serial communication protocol (115200 8N1). 422 status = dc_iostream_configure(device->iostream, 115200, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE); 423 if (status != DC_STATUS_SUCCESS) { 424 ERROR(context, "Failed to set the terminal attributes."); 425 goto error_free_iostream; 426 } 427 428 // Set the timeout for receiving data (1000ms). 429 status = dc_iostream_set_timeout(device->iostream, 1000); 430 if (status != DC_STATUS_SUCCESS) { 431 ERROR(context, "Failed to set the timeout."); 432 goto error_free_iostream; 433 } 434 435 // Make sure everything is in a sane state. 436 dc_iostream_sleep (device->iostream, 100); 437 dc_iostream_purge (device->iostream, DC_DIRECTION_ALL); 438 439 *out = (dc_device_t *)device; 440 441 return DC_STATUS_SUCCESS; 442 443 error_free_iostream: 444 if (transport == DC_TRANSPORT_BLE) { 445 dc_iostream_close (device->iostream); 446 } 447 error_free: 448 dc_device_deallocate((dc_device_t *)device); 449 return status; 450 } 451 452 static dc_status_t 453 mclean_extreme_device_close(dc_device_t *abstract) 454 { 455 dc_status_t status = DC_STATUS_SUCCESS; 456 mclean_extreme_device_t *device = (mclean_extreme_device_t *)abstract; 457 dc_status_t rc = DC_STATUS_SUCCESS; 458 459 rc = mclean_extreme_send(device, CMD_CLOSE, NULL, 0); 460 if (rc != DC_STATUS_SUCCESS) { 461 ERROR(abstract->context, "Failed to send the exit command."); 462 dc_status_set_error(&status, rc); 463 } 464 465 // Close the packet stream. 466 if (dc_iostream_get_transport (device->iostream) == DC_TRANSPORT_BLE) { 467 rc = dc_iostream_close (device->iostream); 468 if (rc != DC_STATUS_SUCCESS) { 469 ERROR (abstract->context, "Failed to close the packet stream."); 470 dc_status_set_error(&status, rc); 471 } 472 } 473 474 return status; 475 } 476 477 static dc_status_t 478 mclean_extreme_device_set_fingerprint(dc_device_t *abstract, const unsigned char data[], unsigned int size) 479 { 480 mclean_extreme_device_t *device = (mclean_extreme_device_t *)abstract; 481 482 if (size && size != sizeof(device->fingerprint)) { 483 return DC_STATUS_INVALIDARGS; 484 } 485 486 if (size) { 487 memcpy(device->fingerprint, data, sizeof(device->fingerprint)); 488 } else { 489 memset(device->fingerprint, 0, sizeof(device->fingerprint)); 490 } 491 492 return DC_STATUS_SUCCESS; 493 } 494 495 static dc_status_t 496 mclean_extreme_device_timesync(dc_device_t *abstract, const dc_datetime_t *datetime) 497 { 498 mclean_extreme_device_t *device = (mclean_extreme_device_t *)abstract; 499 500 // Get the UTC timestamp. 501 dc_ticks_t ticks = dc_datetime_mktime(datetime); 502 if (ticks == -1 || ticks < EPOCH || ticks - EPOCH > 0xFFFFFFFF) { 503 ERROR (abstract->context, "Invalid date/time value specified."); 504 return DC_STATUS_INVALIDARGS; 505 } 506 507 // Adjust the epoch. 508 unsigned int timestamp = ticks - EPOCH; 509 510 // Send the command. 511 const unsigned char cmd[] = { 512 (timestamp ) & 0xFF, 513 (timestamp >> 8) & 0xFF, 514 (timestamp >> 16) & 0xFF, 515 (timestamp >> 24) & 0xFF 516 }; 517 dc_status_t status = mclean_extreme_send(device, CMD_SET_TIME, cmd, sizeof(cmd)); 518 if (status != DC_STATUS_SUCCESS) { 519 ERROR(abstract->context, "Failed to send the set time command."); 520 return status; 521 } 522 523 return status; 524 } 525 526 static dc_status_t 527 mclean_extreme_device_foreach(dc_device_t *abstract, dc_dive_callback_t callback, void *userdata) 528 { 529 dc_status_t status = DC_STATUS_SUCCESS; 530 mclean_extreme_device_t *device = (mclean_extreme_device_t *)abstract; 531 532 // Enable progress notifications. 533 dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER; 534 device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); 535 536 // Read the firmware version. 537 unsigned char firmware[4] = {0}; 538 status = mclean_extreme_transfer(device, CMD_FIRMWARE, NULL, 0, firmware, sizeof(firmware), NULL); 539 if (status != DC_STATUS_SUCCESS) { 540 ERROR(abstract->context, "Failed to read the firmware version."); 541 return status; 542 } 543 544 HEXDUMP (abstract->context, DC_LOGLEVEL_DEBUG, "Firmware", firmware, sizeof(firmware)); 545 546 // Read the serial number. 547 size_t serial_len = 0; 548 unsigned char serial[SZ_PACKET] = {0}; 549 status = mclean_extreme_transfer(device, CMD_SERIALNUMBER, NULL, 0, serial, sizeof(serial), &serial_len); 550 if (status != DC_STATUS_SUCCESS) { 551 ERROR(abstract->context, "Failed to read serial number."); 552 return status; 553 } 554 555 HEXDUMP (abstract->context, DC_LOGLEVEL_DEBUG, "Serial", serial, serial_len); 556 557 // Emit a device info event. 558 dc_event_devinfo_t devinfo; 559 devinfo.model = 0; 560 devinfo.firmware = array_uint32_le (firmware); 561 devinfo.serial = hashcode (serial, serial_len); 562 device_event_emit (abstract, DC_EVENT_DEVINFO, &devinfo); 563 564 // Read the computer configuration. 565 unsigned char computer[SZ_COMPUTER]; 566 status = mclean_extreme_transfer(device, CMD_COMPUTER, NULL, 0, computer, sizeof(computer), NULL); 567 if (status != DC_STATUS_SUCCESS) { 568 ERROR(abstract->context, "Failed to read the computer configuration."); 569 return status; 570 } 571 572 HEXDUMP (abstract->context, DC_LOGLEVEL_DEBUG, "Config", computer, sizeof(computer)); 573 574 // Verify the format version. 575 unsigned int format = computer[0x0000]; 576 if (format != 0) { 577 ERROR(abstract->context, "Unsupported device format."); 578 return DC_STATUS_DATAFORMAT; 579 } 580 581 // Get the number of dives. 582 unsigned int ndives = array_uint16_le(computer + 0x0019); 583 584 // Update and emit a progress event. 585 progress.current = 1 * NSTEPS; 586 progress.maximum = (ndives + 1) * NSTEPS; 587 device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); 588 589 // Allocate a memory buffer for a single dive. 590 dc_buffer_t *buffer = dc_buffer_new(0); 591 if (buffer == NULL) { 592 status = DC_STATUS_NOMEMORY; 593 goto error_exit; 594 } 595 596 for (unsigned int i = 0; i < ndives; ++i) { 597 // Download in reverse order (newest first). 598 unsigned int idx = ndives - 1 - i; 599 600 // Read the dive. 601 status = mclean_extreme_readdive (abstract, &progress, idx, buffer); 602 if (status != DC_STATUS_SUCCESS) { 603 goto error_buffer_free; 604 } 605 606 // Cache the pointer. 607 unsigned char *data = dc_buffer_get_data(buffer); 608 unsigned int size = dc_buffer_get_size(buffer); 609 610 if (memcmp(data + SZ_CFG, device->fingerprint, sizeof(device->fingerprint)) == 0) 611 break; 612 613 if (callback && !callback (data, size, data + SZ_CFG, sizeof(device->fingerprint), userdata)) { 614 break; 615 } 616 } 617 618 error_buffer_free: 619 dc_buffer_free (buffer); 620 error_exit: 621 return status; 622 }