uwatec_memomouse.c (16259B)
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 "uwatec_memomouse.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), &uwatec_memomouse_device_vtable) 33 34 #define PACKETSIZE 126 35 36 #define ACK 0x60 37 #define NAK 0xA8 38 39 typedef struct uwatec_memomouse_device_t { 40 dc_device_t base; 41 dc_iostream_t *iostream; 42 unsigned int timestamp; 43 unsigned int devtime; 44 dc_ticks_t systime; 45 } uwatec_memomouse_device_t; 46 47 static dc_status_t uwatec_memomouse_device_set_fingerprint (dc_device_t *device, const unsigned char data[], unsigned int size); 48 static dc_status_t uwatec_memomouse_device_dump (dc_device_t *abstract, dc_buffer_t *buffer); 49 static dc_status_t uwatec_memomouse_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata); 50 51 static const dc_device_vtable_t uwatec_memomouse_device_vtable = { 52 sizeof(uwatec_memomouse_device_t), 53 DC_FAMILY_UWATEC_MEMOMOUSE, 54 uwatec_memomouse_device_set_fingerprint, /* set_fingerprint */ 55 NULL, /* read */ 56 NULL, /* write */ 57 uwatec_memomouse_device_dump, /* dump */ 58 uwatec_memomouse_device_foreach, /* foreach */ 59 NULL, /* timesync */ 60 NULL /* close */ 61 }; 62 63 static dc_status_t 64 uwatec_memomouse_extract_dives (dc_device_t *device, const unsigned char data[], unsigned int size, dc_dive_callback_t callback, void *userdata); 65 66 dc_status_t 67 uwatec_memomouse_device_open (dc_device_t **out, dc_context_t *context, dc_iostream_t *iostream) 68 { 69 dc_status_t status = DC_STATUS_SUCCESS; 70 uwatec_memomouse_device_t *device = NULL; 71 72 if (out == NULL) 73 return DC_STATUS_INVALIDARGS; 74 75 // Allocate memory. 76 device = (uwatec_memomouse_device_t *) dc_device_allocate (context, &uwatec_memomouse_device_vtable); 77 if (device == NULL) { 78 ERROR (context, "Failed to allocate memory."); 79 return DC_STATUS_NOMEMORY; 80 } 81 82 // Set the default values. 83 device->iostream = iostream; 84 device->timestamp = 0; 85 device->systime = (dc_ticks_t) -1; 86 device->devtime = 0; 87 88 // Set the serial communication protocol (9600 8N1). 89 status = dc_iostream_configure (device->iostream, 9600, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE); 90 if (status != DC_STATUS_SUCCESS) { 91 ERROR (context, "Failed to set the terminal attributes."); 92 goto error_free; 93 } 94 95 // Set the timeout for receiving data (1000 ms). 96 status = dc_iostream_set_timeout (device->iostream, 1000); 97 if (status != DC_STATUS_SUCCESS) { 98 ERROR (context, "Failed to set the timeout."); 99 goto error_free; 100 } 101 102 // Clear the DTR line. 103 status = dc_iostream_set_dtr (device->iostream, 0); 104 if (status != DC_STATUS_SUCCESS) { 105 ERROR (context, "Failed to clear the DTR line."); 106 goto error_free; 107 } 108 109 // Clear the RTS line. 110 status = dc_iostream_set_rts (device->iostream, 0); 111 if (status != DC_STATUS_SUCCESS) { 112 ERROR (context, "Failed to clear the RTS line."); 113 goto error_free; 114 } 115 116 // Make sure everything is in a sane state. 117 dc_iostream_purge (device->iostream, DC_DIRECTION_ALL); 118 119 *out = (dc_device_t*) device; 120 121 return DC_STATUS_SUCCESS; 122 123 error_free: 124 dc_device_deallocate ((dc_device_t *) device); 125 return status; 126 } 127 128 129 static dc_status_t 130 uwatec_memomouse_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size) 131 { 132 uwatec_memomouse_device_t *device = (uwatec_memomouse_device_t*) abstract; 133 134 if (size && size != 4) 135 return DC_STATUS_INVALIDARGS; 136 137 if (size) 138 device->timestamp = array_uint32_le (data); 139 else 140 device->timestamp = 0; 141 142 return DC_STATUS_SUCCESS; 143 } 144 145 146 static dc_status_t 147 uwatec_memomouse_read_packet (uwatec_memomouse_device_t *device, unsigned char data[], unsigned int size, unsigned int *result) 148 { 149 dc_status_t status = DC_STATUS_SUCCESS; 150 dc_device_t *abstract = (dc_device_t *) device; 151 152 assert (result != NULL); 153 154 // Receive the header of the package. 155 status = dc_iostream_read (device->iostream, data, 1, NULL); 156 if (status != DC_STATUS_SUCCESS) { 157 ERROR (abstract->context, "Failed to receive the answer."); 158 return status; 159 } 160 161 // Reverse the bits. 162 array_reverse_bits (data, 1); 163 164 // Verify the header of the package. 165 unsigned int len = data[0]; 166 if (len + 2 > size) { 167 ERROR (abstract->context, "Unexpected answer start byte(s)."); 168 return DC_STATUS_PROTOCOL; 169 } 170 171 // Receive the remaining part of the package. 172 status = dc_iostream_read (device->iostream, data + 1, len + 1, NULL); 173 if (status != DC_STATUS_SUCCESS) { 174 ERROR (abstract->context, "Failed to receive the answer."); 175 return status; 176 } 177 178 // Reverse the bits. 179 array_reverse_bits (data + 1, len + 1); 180 181 // Verify the checksum of the package. 182 unsigned char crc = data[len + 1]; 183 unsigned char ccrc = checksum_xor_uint8 (data, len + 1, 0x00); 184 if (crc != ccrc) { 185 ERROR (abstract->context, "Unexpected answer checksum."); 186 return DC_STATUS_PROTOCOL; 187 } 188 189 *result = len; 190 191 return DC_STATUS_SUCCESS; 192 } 193 194 195 static dc_status_t 196 uwatec_memomouse_read_packet_outer (uwatec_memomouse_device_t *device, unsigned char data[], unsigned int size, unsigned int *result) 197 { 198 dc_status_t status = DC_STATUS_SUCCESS; 199 dc_device_t *abstract = (dc_device_t *) device; 200 201 dc_status_t rc = DC_STATUS_SUCCESS; 202 while ((rc = uwatec_memomouse_read_packet (device, data, size, result)) != DC_STATUS_SUCCESS) { 203 // Automatically discard a corrupted packet, 204 // and request a new one. 205 if (rc != DC_STATUS_PROTOCOL) 206 return rc; 207 208 // Flush the input buffer. 209 dc_iostream_purge (device->iostream, DC_DIRECTION_INPUT); 210 211 // Reject the packet. 212 unsigned char value = NAK; 213 status = dc_iostream_write (device->iostream, &value, 1, NULL); 214 if (status != DC_STATUS_SUCCESS) { 215 ERROR (abstract->context, "Failed to reject the packet."); 216 return status; 217 } 218 } 219 220 return DC_STATUS_SUCCESS; 221 } 222 223 224 static dc_status_t 225 uwatec_memomouse_read_packet_inner (uwatec_memomouse_device_t *device, dc_buffer_t *buffer, dc_event_progress_t *progress) 226 { 227 dc_status_t status = DC_STATUS_SUCCESS; 228 dc_device_t *abstract = (dc_device_t *) device; 229 230 // Erase the current contents of the buffer. 231 if (!dc_buffer_clear (buffer)) { 232 ERROR (abstract->context, "Insufficient buffer space available."); 233 return DC_STATUS_NOMEMORY; 234 } 235 236 unsigned int nbytes = 0; 237 unsigned int total = PACKETSIZE; 238 while (nbytes < total) { 239 // Calculate the packet size. 240 unsigned int length = total - nbytes; 241 if (length > PACKETSIZE) 242 length = PACKETSIZE; 243 244 // Read the packet. 245 unsigned char packet[PACKETSIZE + 2] = {0}; 246 dc_status_t rc = uwatec_memomouse_read_packet_outer (device, packet, length + 2, &length); 247 if (rc != DC_STATUS_SUCCESS) 248 return rc; 249 250 // Accept the packet. 251 unsigned char value = ACK; 252 status = dc_iostream_write (device->iostream, &value, 1, NULL); 253 if (status != DC_STATUS_SUCCESS) { 254 ERROR (abstract->context, "Failed to accept the packet."); 255 return status; 256 } 257 258 if (nbytes == 0) { 259 // The first packet should contain at least 260 // the total size of the inner packet. 261 if (length < 2) { 262 ERROR (abstract->context, "Data packet is too short."); 263 return DC_STATUS_PROTOCOL; 264 } 265 266 // Calculate the total size of the inner packet. 267 total = array_uint16_le (packet + 1) + 3; 268 269 // Pre-allocate the required amount of memory. 270 if (!dc_buffer_reserve (buffer, total)) { 271 ERROR (abstract->context, "Insufficient buffer space available."); 272 return DC_STATUS_NOMEMORY; 273 } 274 } 275 276 // Update and emit a progress event. 277 if (progress) { 278 progress->maximum = total; 279 progress->current += length; 280 device_event_emit (&device->base, DC_EVENT_PROGRESS, progress); 281 } 282 283 // Append the packet to the buffer. 284 dc_buffer_append (buffer, packet + 1, length); 285 286 nbytes += length; 287 } 288 289 // Obtain the pointer to the buffer contents. 290 unsigned char *data = dc_buffer_get_data (buffer); 291 292 // Verify the checksum. 293 unsigned char crc = data[total - 1]; 294 unsigned char ccrc = checksum_xor_uint8 (data, total - 1, 0x00); 295 if (crc != ccrc) { 296 ERROR (abstract->context, "Unexpected answer checksum."); 297 return DC_STATUS_PROTOCOL; 298 } 299 300 // Discard the header and checksum bytes. 301 dc_buffer_slice (buffer, 2, total - 3); 302 303 return DC_STATUS_SUCCESS; 304 } 305 306 307 static dc_status_t 308 uwatec_memomouse_dump_internal (uwatec_memomouse_device_t *device, dc_buffer_t *buffer) 309 { 310 dc_status_t status = DC_STATUS_SUCCESS; 311 dc_device_t *abstract = (dc_device_t *) device; 312 313 // Enable progress notifications. 314 dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER; 315 device_event_emit (&device->base, DC_EVENT_PROGRESS, &progress); 316 317 // Waiting for greeting message. 318 while (dc_iostream_poll (device->iostream, 300) == DC_STATUS_TIMEOUT) { 319 if (device_is_cancelled (abstract)) 320 return DC_STATUS_CANCELLED; 321 322 // Flush the input buffer. 323 dc_iostream_purge (device->iostream, DC_DIRECTION_INPUT); 324 325 // Reject the packet. 326 unsigned char value = NAK; 327 status = dc_iostream_write (device->iostream, &value, 1, NULL); 328 if (status != DC_STATUS_SUCCESS) { 329 ERROR (abstract->context, "Failed to reject the packet."); 330 return status; 331 } 332 } 333 334 // Read the ID string. 335 dc_status_t rc = uwatec_memomouse_read_packet_inner (device, buffer, NULL); 336 if (rc != DC_STATUS_SUCCESS) 337 return rc; 338 339 // Prepare the command. 340 unsigned char command [9] = { 341 0x07, // Outer packet size. 342 0x05, 0x00, // Inner packet size. 343 0x55, // Command byte. 344 (device->timestamp ) & 0xFF, 345 (device->timestamp >> 8) & 0xFF, 346 (device->timestamp >> 16) & 0xFF, 347 (device->timestamp >> 24) & 0xFF, 348 0x00}; // Outer packet checksum. 349 command[8] = checksum_xor_uint8 (command, 8, 0x00); 350 array_reverse_bits (command, sizeof (command)); 351 352 // Wait a small amount of time before sending the command. 353 // Without this delay, the transfer will fail most of the time. 354 dc_iostream_sleep (device->iostream, 50); 355 356 // Keep send the command to the device, 357 // until the ACK answer is received. 358 unsigned char answer = NAK; 359 while (answer == NAK) { 360 // Flush the input buffer. 361 dc_iostream_purge (device->iostream, DC_DIRECTION_INPUT); 362 363 // Send the command to the device. 364 status = dc_iostream_write (device->iostream, command, sizeof (command), NULL); 365 if (status != DC_STATUS_SUCCESS) { 366 ERROR (abstract->context, "Failed to send the command."); 367 return status; 368 } 369 370 // Wait for the answer (ACK). 371 status = dc_iostream_read (device->iostream, &answer, 1, NULL); 372 if (status != DC_STATUS_SUCCESS) { 373 ERROR (abstract->context, "Failed to receive the answer."); 374 return status; 375 } 376 } 377 378 // Verify the answer. 379 if (answer != ACK) { 380 ERROR (abstract->context, "Unexpected answer start byte(s)."); 381 return DC_STATUS_PROTOCOL; 382 } 383 384 // Wait for the data packet. 385 while (dc_iostream_poll (device->iostream, 100) == DC_STATUS_TIMEOUT) { 386 if (device_is_cancelled (abstract)) 387 return DC_STATUS_CANCELLED; 388 389 device_event_emit (&device->base, DC_EVENT_WAITING, NULL); 390 } 391 392 // Fetch the current system time. 393 dc_ticks_t now = dc_datetime_now (); 394 395 // Read the data packet. 396 rc = uwatec_memomouse_read_packet_inner (device, buffer, &progress); 397 if (rc != DC_STATUS_SUCCESS) 398 return rc; 399 400 // Store the clock calibration values. 401 device->systime = now; 402 device->devtime = array_uint32_le (dc_buffer_get_data (buffer) + 1); 403 404 // Emit a clock event. 405 dc_event_clock_t clock; 406 clock.systime = device->systime; 407 clock.devtime = device->devtime; 408 device_event_emit ((dc_device_t *) device, DC_EVENT_CLOCK, &clock); 409 410 return DC_STATUS_SUCCESS; 411 } 412 413 414 static dc_status_t 415 uwatec_memomouse_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) 416 { 417 dc_status_t status = DC_STATUS_SUCCESS; 418 uwatec_memomouse_device_t *device = (uwatec_memomouse_device_t*) abstract; 419 dc_status_t rc = DC_STATUS_SUCCESS; 420 421 // Give the interface some time to notice the DTR 422 // line change from a previous transfer (if any). 423 dc_iostream_sleep (device->iostream, 500); 424 425 // Set the DTR line. 426 rc = dc_iostream_set_dtr (device->iostream, 1); 427 if (rc != DC_STATUS_SUCCESS) { 428 ERROR (abstract->context, "Failed to set the RTS line."); 429 return rc; 430 } 431 432 // Start the transfer. 433 status = uwatec_memomouse_dump_internal (device, buffer); 434 435 // Clear the DTR line again. 436 rc = dc_iostream_set_dtr (device->iostream, 0); 437 if (rc != DC_STATUS_SUCCESS) { 438 ERROR (abstract->context, "Failed to set the RTS line."); 439 return rc; 440 } 441 442 return status; 443 } 444 445 446 static dc_status_t 447 uwatec_memomouse_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata) 448 { 449 dc_buffer_t *buffer = dc_buffer_new (0); 450 if (buffer == NULL) 451 return DC_STATUS_NOMEMORY; 452 453 dc_status_t rc = uwatec_memomouse_device_dump (abstract, buffer); 454 if (rc != DC_STATUS_SUCCESS) { 455 dc_buffer_free (buffer); 456 return rc; 457 } 458 459 rc = uwatec_memomouse_extract_dives (abstract, 460 dc_buffer_get_data (buffer), dc_buffer_get_size (buffer), callback, userdata); 461 462 dc_buffer_free (buffer); 463 464 return rc; 465 } 466 467 468 static dc_status_t 469 uwatec_memomouse_extract_dives (dc_device_t *abstract, const unsigned char data[], unsigned int size, dc_dive_callback_t callback, void *userdata) 470 { 471 if (abstract && !ISINSTANCE (abstract)) 472 return DC_STATUS_INVALIDARGS; 473 474 // Parse the data stream to find the total number of dives. 475 unsigned int ndives = 0; 476 unsigned int previous = 0; 477 unsigned int current = 5; 478 while (current + 18 <= size) { 479 // Memomouse sends all the data twice. The first time, it sends 480 // the data starting from the oldest dive towards the newest dive. 481 // Next, it send the same data in reverse order (newest to oldest). 482 // We abort the parsing once we detect the first duplicate dive. 483 // The second data stream contains always exactly 37 dives, and not 484 // all dives have profile data, so it's probably data from the 485 // connected Uwatec Aladin (converted to the memomouse format). 486 if (previous && memcmp (data + previous, data + current, 18) == 0) 487 break; 488 489 // Get the length of the profile data. 490 unsigned int len = array_uint16_le (data + current + 16); 491 492 // Check for a buffer overflow. 493 if (current + len + 18 > size) 494 return DC_STATUS_DATAFORMAT; 495 496 // A memomouse can store data from several dive computers, but only 497 // the data of the connected dive computer can be transferred. 498 // Therefore, the device info will be the same for all dives, and 499 // only needs to be reported once. 500 if (abstract && ndives == 0) { 501 // Emit a device info event. 502 dc_event_devinfo_t devinfo; 503 devinfo.model = data[current + 3]; 504 devinfo.firmware = 0; 505 devinfo.serial = array_uint24_be (data + current); 506 device_event_emit (abstract, DC_EVENT_DEVINFO, &devinfo); 507 } 508 509 // Move to the next dive. 510 previous = current; 511 current += len + 18; 512 ndives++; 513 } 514 515 // Parse the data stream again to return each dive in reverse order 516 // (newest dive first). This is less efficient, since the data stream 517 // needs to be scanned multiple times, but it makes the behaviour 518 // consistent with the equivalent function for the Uwatec Aladin. 519 for (unsigned int i = 0; i < ndives; ++i) { 520 // Skip the older dives. 521 unsigned int offset = 5; 522 unsigned int skip = ndives - i - 1; 523 while (skip) { 524 // Get the length of the profile data. 525 unsigned int len = array_uint16_le (data + offset + 16); 526 // Move to the next dive. 527 offset += len + 18; 528 skip--; 529 } 530 531 // Get the length of the profile data. 532 unsigned int length = array_uint16_le (data + offset + 16); 533 534 if (callback && !callback (data + offset, length + 18, data + offset + 11, 4, userdata)) 535 return DC_STATUS_SUCCESS; 536 } 537 538 return DC_STATUS_SUCCESS; 539 }