deepblu_cosmiq.c (16267B)
1 /* 2 * libdivecomputer 3 * 4 * Copyright (C) 2019 Linus Torvalds 5 * Copyright (C) 2022 Jef Driesen 6 * 7 * This library is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2.1 of the License, or (at your option) any later version. 11 * 12 * This library is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public 18 * License along with this library; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 20 * MA 02110-1301 USA 21 */ 22 23 #include <string.h> 24 #include <stdlib.h> 25 26 #include "deepblu_cosmiq.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 // Maximum data in a packet. It's actually much 34 // less than this, since BLE packets are small and 35 // with the 7 bytes of headers and final newline 36 // and the HEX encoding, the actual maximum is 37 // just something like 6 bytes. 38 // 39 // But in theory the data could be done over 40 // multiple packets. That doesn't seem to be 41 // the case in anything I've seen so far. 42 // 43 // Pick something small and easy to use for 44 // stack buffers. 45 #define MAX_DATA 20 46 47 #define SZ_HEADER 36 48 49 #define FP_OFFSET 6 50 #define FP_SIZE 6 51 52 #define CMD_SET_DATETIME 0x20 53 54 #define CMD_DIVE_COUNT 0x40 55 #define CMD_DIVE_HEADER 0x41 56 #define CMD_DIVE_HEADER_DATA 0x42 57 #define CMD_DIVE_PROFILE 0x43 58 #define CMD_DIVE_PROFILE_DATA 0x44 59 60 #define CMD_SYSTEM_FW 0x58 61 #define CMD_SYSTEM_MAC 0x5A 62 63 #define NSTEPS 1000 64 #define STEP(i,n) (NSTEPS * (i) / (n)) 65 66 typedef struct deepblu_cosmiq_device_t { 67 dc_device_t base; 68 dc_iostream_t *iostream; 69 unsigned char fingerprint[FP_SIZE]; 70 } deepblu_cosmiq_device_t; 71 72 static dc_status_t deepblu_cosmiq_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size); 73 static dc_status_t deepblu_cosmiq_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata); 74 static dc_status_t deepblu_cosmiq_device_timesync(dc_device_t *abstract, const dc_datetime_t *datetime); 75 76 static const dc_device_vtable_t deepblu_cosmiq_device_vtable = { 77 sizeof(deepblu_cosmiq_device_t), 78 DC_FAMILY_DEEPBLU_COSMIQ, 79 deepblu_cosmiq_device_set_fingerprint, /* set_fingerprint */ 80 NULL, /* read */ 81 NULL, /* write */ 82 NULL, /* dump */ 83 deepblu_cosmiq_device_foreach, /* foreach */ 84 deepblu_cosmiq_device_timesync, /* timesync */ 85 NULL, /* close */ 86 }; 87 88 // 89 // Send a cmd packet. 90 // 91 // The format of the cmd on the "wire" is: 92 // - byte '#' 93 // - HEX char of cmd 94 // - HEX char two's complement modular sum of packet data (including cmd/size) 95 // - HEX char size of data as encoded in HEX 96 // - n * HEX char data 97 // - byte '\n' 98 // so you end up having 8 bytes of header/trailer overhead, and two bytes 99 // for every byte of data sent due to the HEX encoding. 100 // 101 static dc_status_t 102 deepblu_cosmiq_send (deepblu_cosmiq_device_t *device, const unsigned char cmd, const unsigned char data[], size_t size) 103 { 104 dc_status_t status = DC_STATUS_SUCCESS; 105 dc_device_t *abstract = (dc_device_t *) device; 106 107 if (size > MAX_DATA) 108 return DC_STATUS_INVALIDARGS; 109 110 if (device_is_cancelled (abstract)) 111 return DC_STATUS_CANCELLED; 112 113 // Build the packet. 114 unsigned char csum = ~checksum_add_uint8 (data, size, cmd + 2 * size) + 1; 115 unsigned char raw[3 + MAX_DATA] = { 116 cmd, 117 csum, 118 2 * size 119 }; 120 if (size) { 121 memcpy (raw + 3, data, size); 122 } 123 unsigned char packet[1 + 2 * (3 + MAX_DATA) + 1] = {0}; 124 packet[0] = '#'; 125 array_convert_bin2hex (raw, 3 + size, packet + 1, 2 * (3 + size)); 126 packet[1 + 2 * (3 + size)] = '\n'; 127 128 HEXDUMP (device->base.context, DC_LOGLEVEL_DEBUG, "cmd", raw, 3 + size); 129 130 // Send the command. 131 status = dc_iostream_write (device->iostream, packet, 2 + 2 * (3 + size), NULL); 132 if (status != DC_STATUS_SUCCESS) { 133 ERROR (device->base.context, "Failed to send the command."); 134 return status; 135 } 136 137 return status; 138 } 139 140 // 141 // Receive one 'line' of data 142 // 143 // The deepblu BLE protocol is ASCII line based and packetized. 144 // Normally one packet is one line, but it looks like the Nordic 145 // Semi BLE chip will sometimes send packets early (some internal 146 // serial buffer timeout?) with incompete data. 147 // 148 // So read packets until you get newline. 149 static dc_status_t 150 deepblu_cosmiq_recv_line (deepblu_cosmiq_device_t *device, unsigned char data[], size_t size, size_t *actual) 151 { 152 dc_status_t status = DC_STATUS_SUCCESS; 153 size_t nbytes = 0; 154 155 while (1) { 156 unsigned char buffer[20] = {0}; 157 size_t transferred = 0; 158 159 status = dc_iostream_read (device->iostream, buffer, sizeof(buffer), &transferred); 160 if (status != DC_STATUS_SUCCESS) { 161 ERROR (device->base.context, "Failed to receive the reply packet."); 162 return status; 163 } 164 165 if (transferred < 1) { 166 ERROR (device->base.context, "Empty reply packet received."); 167 return DC_STATUS_PROTOCOL; 168 } 169 170 // Append the payload data to the output buffer. If the output 171 // buffer is too small, the error is not reported immediately 172 // but delayed until all packets have been received. 173 if (nbytes < size) { 174 size_t n = transferred; 175 if (nbytes + n > size) { 176 n = size - nbytes; 177 } 178 memcpy(data + nbytes, buffer, n); 179 } 180 nbytes += transferred; 181 182 // Last packet? 183 if (buffer[transferred - 1] == '\n') 184 break; 185 } 186 187 // Verify the expected number of bytes. 188 if (nbytes > size) { 189 ERROR (device->base.context, "Unexpected number of bytes received (" DC_PRINTF_SIZE " " DC_PRINTF_SIZE ").", nbytes, size); 190 return DC_STATUS_PROTOCOL; 191 } 192 193 if (actual) 194 *actual = nbytes; 195 196 return DC_STATUS_SUCCESS; 197 } 198 199 // 200 // Receive a reply packet 201 // 202 // The reply packet has the same format as the cmd packet we 203 // send, except the first byte is '$' instead of '#'. 204 static dc_status_t 205 deepblu_cosmiq_recv (deepblu_cosmiq_device_t *device, const unsigned char cmd, unsigned char data[], size_t size, size_t *actual) 206 { 207 dc_status_t status = DC_STATUS_SUCCESS; 208 unsigned char packet[1 + 2 * (3 + MAX_DATA) + 1] = {0}; 209 210 size_t transferred = 0; 211 status = deepblu_cosmiq_recv_line (device, packet, sizeof(packet), &transferred); 212 if (status != DC_STATUS_SUCCESS) 213 return status; 214 215 if (transferred < 8 || (transferred % 2) != 0) { 216 ERROR (device->base.context, "Unexpected packet length (" DC_PRINTF_SIZE ").", transferred); 217 return DC_STATUS_PROTOCOL; 218 } 219 220 if (packet[0] != '$' || packet[transferred - 1] != '\n') { 221 ERROR (device->base.context, "Unexpected packet start/end byte."); 222 return DC_STATUS_PROTOCOL; 223 } 224 225 size_t length = transferred - 2; 226 227 unsigned char raw[3 + MAX_DATA] = {0}; 228 if (array_convert_hex2bin (packet + 1, length, raw, length / 2) != 0) { 229 ERROR (device->base.context, "Unexpected packet data."); 230 return DC_STATUS_PROTOCOL; 231 } 232 233 length /= 2; 234 235 HEXDUMP (device->base.context, DC_LOGLEVEL_DEBUG, "rcv", raw, length); 236 237 unsigned char rsp = raw[0]; 238 if (rsp != cmd) { 239 ERROR (device->base.context, "Unexpected packet command byte (%02x)", rsp); 240 return DC_STATUS_PROTOCOL; 241 } 242 243 unsigned int n = raw[2]; 244 if ((n % 2) != 0 || n != transferred - 8) { 245 ERROR (device->base.context, "Unexpected packet length (%u)", n); 246 return DC_STATUS_PROTOCOL; 247 } 248 249 unsigned char csum = checksum_add_uint8 (raw, length, 0); 250 if (csum != 0) { 251 ERROR (device->base.context, "Unexpected packet checksum (%02x).", csum); 252 return DC_STATUS_PROTOCOL; 253 } 254 255 length -= 3; 256 257 if (length > size) { 258 ERROR (device->base.context, "Unexpected number of bytes received (" DC_PRINTF_SIZE " " DC_PRINTF_SIZE ").", length, size); 259 return DC_STATUS_PROTOCOL; 260 } 261 262 memcpy (data, raw + 3, length); 263 264 if (actual) 265 *actual = length; 266 267 return DC_STATUS_SUCCESS; 268 } 269 270 static dc_status_t 271 deepblu_cosmiq_transfer (deepblu_cosmiq_device_t *device, const unsigned char cmd, 272 const unsigned char input[], size_t isize, 273 unsigned char output[], size_t osize) 274 { 275 dc_status_t status = DC_STATUS_SUCCESS; 276 277 status = deepblu_cosmiq_send (device, cmd, input, isize); 278 if (status != DC_STATUS_SUCCESS) 279 return status; 280 281 size_t transferred = 0; 282 status = deepblu_cosmiq_recv (device, cmd, output, osize, &transferred); 283 if (status != DC_STATUS_SUCCESS) 284 return status; 285 286 if (transferred != osize) { 287 ERROR (device->base.context, "Unexpected number of bytes received (" DC_PRINTF_SIZE " " DC_PRINTF_SIZE ").", osize, transferred); 288 return DC_STATUS_PROTOCOL; 289 } 290 291 return DC_STATUS_SUCCESS; 292 } 293 294 static dc_status_t 295 deepblu_cosmiq_recv_bulk (deepblu_cosmiq_device_t *device, dc_event_progress_t *progress, const unsigned char cmd, unsigned char data[], size_t size) 296 { 297 dc_status_t status = DC_STATUS_SUCCESS; 298 dc_device_t *abstract = (dc_device_t *) device; 299 300 const unsigned int initial = progress ? progress->current : 0; 301 302 size_t nbytes = 0; 303 while (nbytes < size) { 304 size_t transferred = 0; 305 status = deepblu_cosmiq_recv (device, cmd, data + nbytes, size - nbytes, &transferred); 306 if (status != DC_STATUS_SUCCESS) 307 return status; 308 309 nbytes += transferred; 310 311 // Update and emit a progress event. 312 if (progress) { 313 progress->current = initial + STEP(nbytes, size); 314 device_event_emit (abstract, DC_EVENT_PROGRESS, progress); 315 } 316 } 317 318 return DC_STATUS_SUCCESS; 319 } 320 321 dc_status_t 322 deepblu_cosmiq_device_open (dc_device_t **out, dc_context_t *context, dc_iostream_t *iostream) 323 { 324 dc_status_t status = DC_STATUS_SUCCESS; 325 deepblu_cosmiq_device_t *device = NULL; 326 327 if (out == NULL) 328 return DC_STATUS_INVALIDARGS; 329 330 // Allocate memory. 331 device = (deepblu_cosmiq_device_t *) dc_device_allocate (context, &deepblu_cosmiq_device_vtable); 332 if (device == NULL) { 333 ERROR (context, "Failed to allocate memory."); 334 return DC_STATUS_NOMEMORY; 335 } 336 337 // Set the default values. 338 device->iostream = iostream; 339 memset (device->fingerprint, 0, sizeof(device->fingerprint)); 340 341 // Set the timeout for receiving data (1000ms). 342 status = dc_iostream_set_timeout (device->iostream, 1000); 343 if (status != DC_STATUS_SUCCESS) { 344 ERROR (context, "Failed to set the timeout."); 345 goto error_free; 346 } 347 348 dc_iostream_purge (device->iostream, DC_DIRECTION_ALL); 349 350 *out = (dc_device_t *) device; 351 352 return DC_STATUS_SUCCESS; 353 354 error_free: 355 dc_device_deallocate ((dc_device_t *) device); 356 return status; 357 } 358 359 static dc_status_t 360 deepblu_cosmiq_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size) 361 { 362 deepblu_cosmiq_device_t *device = (deepblu_cosmiq_device_t *)abstract; 363 364 if (size && size != sizeof (device->fingerprint)) 365 return DC_STATUS_INVALIDARGS; 366 367 if (size) 368 memcpy (device->fingerprint, data, sizeof (device->fingerprint)); 369 else 370 memset (device->fingerprint, 0, sizeof (device->fingerprint)); 371 372 return DC_STATUS_SUCCESS; 373 } 374 375 static dc_status_t 376 deepblu_cosmiq_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata) 377 { 378 dc_status_t status = DC_STATUS_SUCCESS; 379 deepblu_cosmiq_device_t *device = (deepblu_cosmiq_device_t *) abstract; 380 const unsigned char zero = 0; 381 382 // Enable progress notifications. 383 dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER; 384 device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); 385 386 // Read the firmware version. 387 unsigned char fw[1] = {0}; 388 status = deepblu_cosmiq_transfer (device, CMD_SYSTEM_FW, &zero, 1, fw, sizeof(fw)); 389 if (status != DC_STATUS_SUCCESS) { 390 ERROR (abstract->context, "Failed to read the firmware version."); 391 goto error_exit; 392 } 393 394 HEXDUMP (abstract->context, DC_LOGLEVEL_DEBUG, "Firmware", fw, sizeof(fw)); 395 396 // Read the MAC address. 397 unsigned char mac[6] = {0}; 398 status = deepblu_cosmiq_transfer (device, CMD_SYSTEM_MAC, &zero, 1, mac, sizeof(mac)); 399 if (status != DC_STATUS_SUCCESS) { 400 ERROR (abstract->context, "Failed to read the MAC address."); 401 goto error_exit; 402 } 403 404 HEXDUMP (abstract->context, DC_LOGLEVEL_DEBUG, "Serial", mac, sizeof(mac)); 405 406 // Emit a device info event. 407 dc_event_devinfo_t devinfo; 408 devinfo.model = 0; 409 devinfo.firmware = fw[0] & 0x3F; 410 devinfo.serial = array_uint32_le (mac); 411 device_event_emit (abstract, DC_EVENT_DEVINFO, &devinfo); 412 413 unsigned char ndives = 0; 414 status = deepblu_cosmiq_transfer (device, CMD_DIVE_COUNT, &zero, 1, &ndives, 1); 415 if (status != DC_STATUS_SUCCESS) { 416 ERROR (abstract->context, "Failed to read the number of dives."); 417 goto error_exit; 418 } 419 420 // Update and emit a progress event. 421 progress.current = (ndives == 0 ? 1 : 0) * NSTEPS; 422 progress.maximum = (ndives + 1) * NSTEPS; 423 device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); 424 425 if (ndives == 0) { 426 status = DC_STATUS_SUCCESS; 427 goto error_exit; 428 } 429 430 unsigned char *headers = malloc (ndives * SZ_HEADER); 431 if (headers == NULL) { 432 ERROR (abstract->context, "Failed to allocate memory."); 433 status = DC_STATUS_NOMEMORY; 434 goto error_exit; 435 } 436 437 unsigned int count = 0; 438 for (unsigned int i = 0; i < ndives; ++i) { 439 unsigned char number = i + 1; 440 441 unsigned char len = 0; 442 status = deepblu_cosmiq_transfer (device, CMD_DIVE_HEADER, &number, 1, &len, 1); 443 if (status != DC_STATUS_SUCCESS) { 444 ERROR (abstract->context, "Failed to read the dive header."); 445 goto error_free_headers; 446 } 447 448 if (len != SZ_HEADER) { 449 status = DC_STATUS_PROTOCOL; 450 goto error_free_headers; 451 } 452 453 status = deepblu_cosmiq_recv_bulk (device, NULL, CMD_DIVE_HEADER_DATA, headers + i * SZ_HEADER, SZ_HEADER); 454 if (status != DC_STATUS_SUCCESS) { 455 ERROR (abstract->context, "Failed to read the dive header."); 456 goto error_free_headers; 457 } 458 459 // Update and emit a progress event. 460 progress.current = STEP(i + 1, ndives); 461 device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); 462 463 if (memcmp (headers + i * SZ_HEADER + FP_OFFSET, device->fingerprint, sizeof (device->fingerprint)) == 0) 464 break; 465 466 count++; 467 } 468 469 // Update and emit a progress event. 470 progress.current = 1 * NSTEPS; 471 progress.maximum = (count + 1) * NSTEPS; 472 device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); 473 474 // Allocate memory for the dive. 475 dc_buffer_t *dive = dc_buffer_new (4096); 476 if (dive == NULL) { 477 ERROR (abstract->context, "Failed to allocate memory."); 478 status = DC_STATUS_NOMEMORY; 479 goto error_free_headers; 480 } 481 482 for (unsigned int i = 0; i < count; ++i) { 483 unsigned char number = i + 1; 484 485 unsigned char rsp_len[2] = {0}; 486 status = deepblu_cosmiq_transfer (device, CMD_DIVE_PROFILE, &number, 1, rsp_len, sizeof(rsp_len)); 487 if (status != DC_STATUS_SUCCESS) { 488 ERROR (abstract->context, "Failed to read the dive profile."); 489 goto error_free_dive; 490 } 491 492 unsigned int len = array_uint16_be (rsp_len); 493 494 // Erase the buffer. 495 dc_buffer_clear (dive); 496 497 // Allocate memory for the dive. 498 if (!dc_buffer_resize (dive, len + SZ_HEADER)) { 499 ERROR (abstract->context, "Failed to allocate memory."); 500 status = DC_STATUS_NOMEMORY; 501 goto error_free_dive; 502 } 503 504 // Cache the pointer. 505 unsigned char *data = dc_buffer_get_data (dive); 506 507 memcpy (data, headers + i * SZ_HEADER, SZ_HEADER); 508 509 status = deepblu_cosmiq_recv_bulk (device, &progress, CMD_DIVE_PROFILE_DATA, data + SZ_HEADER, len); 510 if (status != DC_STATUS_SUCCESS) { 511 ERROR (abstract->context, "Failed to read the dive profile."); 512 goto error_free_dive; 513 } 514 515 if (callback && !callback (data, len + SZ_HEADER, data + FP_OFFSET, FP_SIZE, userdata)) 516 break; 517 } 518 519 error_free_dive: 520 dc_buffer_free (dive); 521 error_free_headers: 522 free (headers); 523 error_exit: 524 return status; 525 } 526 527 static dc_status_t 528 deepblu_cosmiq_device_timesync (dc_device_t *abstract, const dc_datetime_t *datetime) 529 { 530 dc_status_t status = DC_STATUS_SUCCESS; 531 deepblu_cosmiq_device_t *device = (deepblu_cosmiq_device_t *) abstract; 532 533 if (datetime->year < 2000) { 534 ERROR (abstract->context, "Invalid date/time value specified."); 535 return DC_STATUS_INVALIDARGS; 536 } 537 538 const unsigned char cmd[6] = { 539 dec2bcd(datetime->year - 2000), 540 dec2bcd(datetime->month), 541 dec2bcd(datetime->day), 542 dec2bcd(datetime->hour), 543 dec2bcd(datetime->minute), 544 dec2bcd(datetime->second)}; 545 unsigned char rsp[1] = {0}; 546 status = deepblu_cosmiq_transfer (device, CMD_SET_DATETIME, 547 cmd, sizeof(cmd), rsp, sizeof(rsp)); 548 if (status != DC_STATUS_SUCCESS) 549 return status; 550 551 return DC_STATUS_SUCCESS; 552 }