reefnet_sensuspro.c (11948B)
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 25 #include "reefnet_sensuspro.h" 26 #include "context-private.h" 27 #include "device-private.h" 28 #include "checksum.h" 29 #include "array.h" 30 31 #define ISINSTANCE(device) dc_device_isinstance((device), &reefnet_sensuspro_device_vtable) 32 33 #define SZ_MEMORY 56320 34 #define SZ_HANDSHAKE 10 35 36 typedef struct reefnet_sensuspro_device_t { 37 dc_device_t base; 38 dc_iostream_t *iostream; 39 unsigned char handshake[SZ_HANDSHAKE]; 40 unsigned int timestamp; 41 unsigned int devtime; 42 dc_ticks_t systime; 43 } reefnet_sensuspro_device_t; 44 45 static dc_status_t reefnet_sensuspro_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size); 46 static dc_status_t reefnet_sensuspro_device_dump (dc_device_t *abstract, dc_buffer_t *buffer); 47 static dc_status_t reefnet_sensuspro_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata); 48 49 static const dc_device_vtable_t reefnet_sensuspro_device_vtable = { 50 sizeof(reefnet_sensuspro_device_t), 51 DC_FAMILY_REEFNET_SENSUSPRO, 52 reefnet_sensuspro_device_set_fingerprint, /* set_fingerprint */ 53 NULL, /* read */ 54 NULL, /* write */ 55 reefnet_sensuspro_device_dump, /* dump */ 56 reefnet_sensuspro_device_foreach, /* foreach */ 57 NULL, /* timesync */ 58 NULL /* close */ 59 }; 60 61 static dc_status_t 62 reefnet_sensuspro_extract_dives (dc_device_t *device, const unsigned char data[], unsigned int size, dc_dive_callback_t callback, void *userdata); 63 64 dc_status_t 65 reefnet_sensuspro_device_open (dc_device_t **out, dc_context_t *context, dc_iostream_t *iostream) 66 { 67 dc_status_t status = DC_STATUS_SUCCESS; 68 reefnet_sensuspro_device_t *device = NULL; 69 70 if (out == NULL) 71 return DC_STATUS_INVALIDARGS; 72 73 // Allocate memory. 74 device = (reefnet_sensuspro_device_t *) dc_device_allocate (context, &reefnet_sensuspro_device_vtable); 75 if (device == NULL) { 76 ERROR (context, "Failed to allocate memory."); 77 return DC_STATUS_NOMEMORY; 78 } 79 80 // Set the default values. 81 device->iostream = iostream; 82 device->timestamp = 0; 83 device->systime = (dc_ticks_t) -1; 84 device->devtime = 0; 85 memset (device->handshake, 0, sizeof (device->handshake)); 86 87 // Set the serial communication protocol (19200 8N1). 88 status = dc_iostream_configure (device->iostream, 19200, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE); 89 if (status != DC_STATUS_SUCCESS) { 90 ERROR (context, "Failed to set the terminal attributes."); 91 goto error_free; 92 } 93 94 // Set the timeout for receiving data (3000ms). 95 status = dc_iostream_set_timeout (device->iostream, 3000); 96 if (status != DC_STATUS_SUCCESS) { 97 ERROR (context, "Failed to set the timeout."); 98 goto error_free; 99 } 100 101 // Make sure everything is in a sane state. 102 dc_iostream_purge (device->iostream, DC_DIRECTION_ALL); 103 104 *out = (dc_device_t*) device; 105 106 return DC_STATUS_SUCCESS; 107 108 error_free: 109 dc_device_deallocate ((dc_device_t *) device); 110 return status; 111 } 112 113 114 dc_status_t 115 reefnet_sensuspro_device_get_handshake (dc_device_t *abstract, unsigned char data[], unsigned int size) 116 { 117 reefnet_sensuspro_device_t *device = (reefnet_sensuspro_device_t*) abstract; 118 119 if (!ISINSTANCE (abstract)) 120 return DC_STATUS_INVALIDARGS; 121 122 if (size < SZ_HANDSHAKE) { 123 ERROR (abstract->context, "Insufficient buffer space available."); 124 return DC_STATUS_INVALIDARGS; 125 } 126 127 memcpy (data, device->handshake, SZ_HANDSHAKE); 128 129 return DC_STATUS_SUCCESS; 130 } 131 132 133 static dc_status_t 134 reefnet_sensuspro_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size) 135 { 136 reefnet_sensuspro_device_t *device = (reefnet_sensuspro_device_t*) abstract; 137 138 if (size && size != 4) 139 return DC_STATUS_INVALIDARGS; 140 141 if (size) 142 device->timestamp = array_uint32_le (data); 143 else 144 device->timestamp = 0; 145 146 return DC_STATUS_SUCCESS; 147 } 148 149 150 static dc_status_t 151 reefnet_sensuspro_handshake (reefnet_sensuspro_device_t *device) 152 { 153 dc_status_t status = DC_STATUS_SUCCESS; 154 dc_device_t *abstract = (dc_device_t *) device; 155 156 // Assert a break condition. 157 status = dc_iostream_set_break (device->iostream, 1); 158 if (status != DC_STATUS_SUCCESS) { 159 ERROR (abstract->context, "Failed to set break."); 160 return status; 161 } 162 163 // Receive the handshake from the dive computer. 164 unsigned char handshake[SZ_HANDSHAKE + 2] = {0}; 165 status = dc_iostream_read (device->iostream, handshake, sizeof (handshake), NULL); 166 if (status != DC_STATUS_SUCCESS) { 167 ERROR (abstract->context, "Failed to receive the handshake."); 168 return status; 169 } 170 171 // Clear the break condition again. 172 status = dc_iostream_set_break (device->iostream, 0); 173 if (status != DC_STATUS_SUCCESS) { 174 ERROR (abstract->context, "Failed to clear break."); 175 return status; 176 } 177 178 // Verify the checksum of the handshake packet. 179 unsigned short crc = array_uint16_le (handshake + SZ_HANDSHAKE); 180 unsigned short ccrc = checksum_crc16_ccitt (handshake, SZ_HANDSHAKE, 0xffff, 0x0000); 181 if (crc != ccrc) { 182 ERROR (abstract->context, "Unexpected answer checksum."); 183 return DC_STATUS_PROTOCOL; 184 } 185 186 HEXDUMP (abstract->context, DC_LOGLEVEL_DEBUG, "Handshake", handshake, SZ_HANDSHAKE); 187 188 // Store the clock calibration values. 189 device->systime = dc_datetime_now (); 190 device->devtime = array_uint32_le (handshake + 6); 191 192 // Store the handshake packet. 193 memcpy (device->handshake, handshake, SZ_HANDSHAKE); 194 195 // Emit a clock event. 196 dc_event_clock_t clock; 197 clock.systime = device->systime; 198 clock.devtime = device->devtime; 199 device_event_emit (&device->base, DC_EVENT_CLOCK, &clock); 200 201 // Emit a device info event. 202 dc_event_devinfo_t devinfo; 203 devinfo.model = handshake[0]; 204 devinfo.firmware = handshake[1]; 205 devinfo.serial = array_uint16_le (handshake + 4); 206 device_event_emit (&device->base, DC_EVENT_DEVINFO, &devinfo); 207 208 // Emit a vendor event. 209 dc_event_vendor_t vendor; 210 vendor.data = device->handshake; 211 vendor.size = sizeof (device->handshake); 212 device_event_emit (abstract, DC_EVENT_VENDOR, &vendor); 213 214 dc_iostream_sleep (device->iostream, 10); 215 216 return DC_STATUS_SUCCESS; 217 } 218 219 220 static dc_status_t 221 reefnet_sensuspro_send (reefnet_sensuspro_device_t *device, unsigned char command) 222 { 223 dc_status_t status = DC_STATUS_SUCCESS; 224 dc_device_t *abstract = (dc_device_t *) device; 225 226 // Wake-up the device. 227 dc_status_t rc = reefnet_sensuspro_handshake (device); 228 if (rc != DC_STATUS_SUCCESS) 229 return rc; 230 231 // Send the instruction code to the device. 232 status = dc_iostream_write (device->iostream, &command, 1, NULL); 233 if (status != DC_STATUS_SUCCESS) { 234 ERROR (abstract->context, "Failed to send the command."); 235 return status; 236 } 237 238 return DC_STATUS_SUCCESS; 239 } 240 241 242 static dc_status_t 243 reefnet_sensuspro_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) 244 { 245 dc_status_t status = DC_STATUS_SUCCESS; 246 reefnet_sensuspro_device_t *device = (reefnet_sensuspro_device_t*) abstract; 247 248 // Pre-allocate the required amount of memory. 249 if (!dc_buffer_reserve (buffer, SZ_MEMORY)) { 250 ERROR (abstract->context, "Insufficient buffer space available."); 251 return DC_STATUS_NOMEMORY; 252 } 253 254 // Enable progress notifications. 255 dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER; 256 progress.maximum = SZ_MEMORY + 2; 257 device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); 258 259 // Wake-up the device and send the instruction code. 260 dc_status_t rc = reefnet_sensuspro_send (device, 0xB4); 261 if (rc != DC_STATUS_SUCCESS) 262 return rc; 263 264 unsigned int nbytes = 0; 265 unsigned char answer[SZ_MEMORY + 2] = {0}; 266 while (nbytes < sizeof (answer)) { 267 unsigned int len = sizeof (answer) - nbytes; 268 if (len > 256) 269 len = 256; 270 271 status = dc_iostream_read (device->iostream, answer + nbytes, len, NULL); 272 if (status != DC_STATUS_SUCCESS) { 273 ERROR (abstract->context, "Failed to receive the answer."); 274 return status; 275 } 276 277 // Update and emit a progress event. 278 progress.current += len; 279 device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); 280 281 nbytes += len; 282 } 283 284 unsigned short crc = array_uint16_le (answer + SZ_MEMORY); 285 unsigned short ccrc = checksum_crc16_ccitt (answer, SZ_MEMORY, 0xffff, 0x0000); 286 if (crc != ccrc) { 287 ERROR (abstract->context, "Unexpected answer checksum."); 288 return DC_STATUS_PROTOCOL; 289 } 290 291 dc_buffer_append (buffer, answer, SZ_MEMORY); 292 293 return DC_STATUS_SUCCESS; 294 } 295 296 297 static dc_status_t 298 reefnet_sensuspro_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata) 299 { 300 dc_buffer_t *buffer = dc_buffer_new (SZ_MEMORY); 301 if (buffer == NULL) 302 return DC_STATUS_NOMEMORY; 303 304 dc_status_t rc = reefnet_sensuspro_device_dump (abstract, buffer); 305 if (rc != DC_STATUS_SUCCESS) { 306 dc_buffer_free (buffer); 307 return rc; 308 } 309 310 rc = reefnet_sensuspro_extract_dives (abstract, 311 dc_buffer_get_data (buffer), dc_buffer_get_size (buffer), callback, userdata); 312 313 dc_buffer_free (buffer); 314 315 return rc; 316 } 317 318 319 dc_status_t 320 reefnet_sensuspro_device_write_interval (dc_device_t *abstract, unsigned char interval) 321 { 322 dc_status_t status = DC_STATUS_SUCCESS; 323 reefnet_sensuspro_device_t *device = (reefnet_sensuspro_device_t*) abstract; 324 325 if (!ISINSTANCE (abstract)) 326 return DC_STATUS_INVALIDARGS; 327 328 if (interval < 1 || interval > 127) 329 return DC_STATUS_INVALIDARGS; 330 331 // Wake-up the device and send the instruction code. 332 dc_status_t rc = reefnet_sensuspro_send (device, 0xB5); 333 if (rc != DC_STATUS_SUCCESS) 334 return rc; 335 336 dc_iostream_sleep (device->iostream, 10); 337 338 status = dc_iostream_write (device->iostream, &interval, 1, NULL); 339 if (status != DC_STATUS_SUCCESS) { 340 ERROR (abstract->context, "Failed to send the data packet."); 341 return status; 342 } 343 344 return DC_STATUS_SUCCESS; 345 } 346 347 348 static dc_status_t 349 reefnet_sensuspro_extract_dives (dc_device_t *abstract, const unsigned char data[], unsigned int size, dc_dive_callback_t callback, void *userdata) 350 { 351 reefnet_sensuspro_device_t *device = (reefnet_sensuspro_device_t*) abstract; 352 353 if (abstract && !ISINSTANCE (abstract)) 354 return DC_STATUS_INVALIDARGS; 355 356 const unsigned char header[4] = {0x00, 0x00, 0x00, 0x00}; 357 const unsigned char footer[2] = {0xFF, 0xFF}; 358 359 // Search the entire data stream for start markers. 360 unsigned int previous = size; 361 unsigned int current = (size >= 4 ? size - 4 : 0); 362 while (current > 0) { 363 current--; 364 if (memcmp (data + current, header, sizeof (header)) == 0) { 365 // Once a start marker is found, start searching 366 // for the corresponding stop marker. The search is 367 // now limited to the start of the previous dive. 368 int found = 0; 369 unsigned int offset = current + 10; // Skip non-sample data. 370 while (offset + 2 <= previous) { 371 if (memcmp (data + offset, footer, sizeof (footer)) == 0) { 372 found = 1; 373 break; 374 } else { 375 offset++; 376 } 377 } 378 379 // Report an error if no stop marker was found. 380 if (!found) 381 return DC_STATUS_DATAFORMAT; 382 383 // Automatically abort when a dive is older than the provided timestamp. 384 unsigned int timestamp = array_uint32_le (data + current + 6); 385 if (device && timestamp <= device->timestamp) 386 return DC_STATUS_SUCCESS; 387 388 if (callback && !callback (data + current, offset + 2 - current, data + current + 6, 4, userdata)) 389 return DC_STATUS_SUCCESS; 390 391 // Prepare for the next dive. 392 previous = current; 393 current = (current >= 4 ? current - 4 : 0); 394 } 395 } 396 397 return DC_STATUS_SUCCESS; 398 }