zeagle_n2ition3.c (12091B)
1 /* 2 * libdivecomputer 3 * 4 * Copyright (C) 2010 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> // memcpy, memcmp 23 #include <stdlib.h> // malloc, free 24 #include <assert.h> // assert 25 26 #include "zeagle_n2ition3.h" 27 #include "context-private.h" 28 #include "device-private.h" 29 #include "checksum.h" 30 #include "array.h" 31 #include "ringbuffer.h" 32 #include "rbstream.h" 33 34 #define ISINSTANCE(device) dc_device_isinstance((device), &zeagle_n2ition3_device_vtable) 35 36 #define SZ_MEMORY 0x8000 37 #define SZ_PACKET 64 38 39 #define RB_PROFILE_BEGIN 0x3FA0 40 #define RB_PROFILE_END 0x7EC0 41 42 #define RB_LOGBOOK_OFFSET 0x7EC0 43 #define RB_LOGBOOK_BEGIN 0 44 #define RB_LOGBOOK_END 60 45 46 typedef struct zeagle_n2ition3_device_t { 47 dc_device_t base; 48 dc_iostream_t *iostream; 49 unsigned char fingerprint[16]; 50 } zeagle_n2ition3_device_t; 51 52 static dc_status_t zeagle_n2ition3_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size); 53 static dc_status_t zeagle_n2ition3_device_read (dc_device_t *abstract, unsigned int address, unsigned char data[], unsigned int size); 54 static dc_status_t zeagle_n2ition3_device_dump (dc_device_t *abstract, dc_buffer_t *buffer); 55 static dc_status_t zeagle_n2ition3_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata); 56 57 static const dc_device_vtable_t zeagle_n2ition3_device_vtable = { 58 sizeof(zeagle_n2ition3_device_t), 59 DC_FAMILY_ZEAGLE_N2ITION3, 60 zeagle_n2ition3_device_set_fingerprint, /* set_fingerprint */ 61 zeagle_n2ition3_device_read, /* read */ 62 NULL, /* write */ 63 zeagle_n2ition3_device_dump, /* dump */ 64 zeagle_n2ition3_device_foreach, /* foreach */ 65 NULL, /* timesync */ 66 NULL /* close */ 67 }; 68 69 70 static dc_status_t 71 zeagle_n2ition3_packet (zeagle_n2ition3_device_t *device, const unsigned char command[], unsigned int csize, unsigned char answer[], unsigned int asize) 72 { 73 dc_status_t status = DC_STATUS_SUCCESS; 74 dc_device_t *abstract = (dc_device_t *) device; 75 76 assert (asize >= csize + 5); 77 78 if (device_is_cancelled (abstract)) 79 return DC_STATUS_CANCELLED; 80 81 // Send the command to the device. 82 status = dc_iostream_write (device->iostream, command, csize, NULL); 83 if (status != DC_STATUS_SUCCESS) { 84 ERROR (abstract->context, "Failed to send the command."); 85 return status; 86 } 87 88 // Receive the answer of the device. 89 status = dc_iostream_read (device->iostream, answer, asize, NULL); 90 if (status != DC_STATUS_SUCCESS) { 91 ERROR (abstract->context, "Failed to receive the answer."); 92 return status; 93 } 94 95 // Verify the echo. 96 if (memcmp (answer, command, csize) != 0) { 97 ERROR (abstract->context, "Unexpected echo."); 98 return DC_STATUS_PROTOCOL; 99 } 100 101 // Verify the header and trailer of the packet. 102 if (answer[csize] != 0x02 && answer[asize - 1] != 0x03) { 103 ERROR (abstract->context, "Unexpected answer header/trailer byte."); 104 return DC_STATUS_PROTOCOL; 105 } 106 107 // Verify the size of the packet. 108 if (array_uint16_le (answer + csize + 1) + csize + 5 != asize) { 109 ERROR (abstract->context, "Unexpected answer size."); 110 return DC_STATUS_PROTOCOL; 111 } 112 113 // Verify the checksum of the packet. 114 unsigned char crc = answer[asize - 2]; 115 unsigned char ccrc = ~checksum_add_uint8 (answer + csize + 3, asize - csize - 5, 0x00) + 1; 116 if (crc != ccrc) { 117 ERROR (abstract->context, "Unexpected answer checksum."); 118 return DC_STATUS_PROTOCOL; 119 } 120 121 return DC_STATUS_SUCCESS; 122 } 123 124 static dc_status_t 125 zeagle_n2ition3_init (zeagle_n2ition3_device_t *device) 126 { 127 unsigned char answer[6 + 13] = {0}; 128 unsigned char command[6] = {0x02, 0x01, 0x00, 0x41, 0xBF, 0x03}; 129 130 return zeagle_n2ition3_packet (device, command, sizeof (command), answer, sizeof (answer)); 131 } 132 133 dc_status_t 134 zeagle_n2ition3_device_open (dc_device_t **out, dc_context_t *context, dc_iostream_t *iostream) 135 { 136 dc_status_t status = DC_STATUS_SUCCESS; 137 zeagle_n2ition3_device_t *device = NULL; 138 139 if (out == NULL) 140 return DC_STATUS_INVALIDARGS; 141 142 // Allocate memory. 143 device = (zeagle_n2ition3_device_t *) dc_device_allocate (context, &zeagle_n2ition3_device_vtable); 144 if (device == NULL) { 145 ERROR (context, "Failed to allocate memory."); 146 return DC_STATUS_NOMEMORY; 147 } 148 149 // Set the default values. 150 device->iostream = iostream; 151 memset (device->fingerprint, 0, sizeof (device->fingerprint)); 152 153 // Set the serial communication protocol (4800 8N1). 154 status = dc_iostream_configure (device->iostream, 4800, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE); 155 if (status != DC_STATUS_SUCCESS) { 156 ERROR (context, "Failed to set the terminal attributes."); 157 goto error_free; 158 } 159 160 // Set the timeout for receiving data (1000 ms). 161 status = dc_iostream_set_timeout (device->iostream, 1000); 162 if (status != DC_STATUS_SUCCESS) { 163 ERROR (context, "Failed to set the timeout."); 164 goto error_free; 165 } 166 167 // Make sure everything is in a sane state. 168 dc_iostream_purge (device->iostream, DC_DIRECTION_ALL); 169 170 // Send the init commands. 171 zeagle_n2ition3_init (device); 172 173 *out = (dc_device_t *) device; 174 175 return DC_STATUS_SUCCESS; 176 177 error_free: 178 dc_device_deallocate ((dc_device_t *) device); 179 return status; 180 } 181 182 183 static dc_status_t 184 zeagle_n2ition3_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size) 185 { 186 zeagle_n2ition3_device_t *device = (zeagle_n2ition3_device_t *) abstract; 187 188 if (size && size != sizeof (device->fingerprint)) 189 return DC_STATUS_INVALIDARGS; 190 191 if (size) 192 memcpy (device->fingerprint, data, sizeof (device->fingerprint)); 193 else 194 memset (device->fingerprint, 0, sizeof (device->fingerprint)); 195 196 return DC_STATUS_SUCCESS; 197 } 198 199 200 static dc_status_t 201 zeagle_n2ition3_device_read (dc_device_t *abstract, unsigned int address, unsigned char data[], unsigned int size) 202 { 203 zeagle_n2ition3_device_t *device = (zeagle_n2ition3_device_t*) abstract; 204 205 unsigned int nbytes = 0; 206 while (nbytes < size) { 207 // Calculate the package size. 208 unsigned int len = size - nbytes; 209 if (len > SZ_PACKET) 210 len = SZ_PACKET; 211 212 // Read the package. 213 unsigned char answer[13 + SZ_PACKET + 6] = {0}; 214 unsigned char command[13] = {0x02, 0x08, 0x00, 0x4D, 215 (address ) & 0xFF, // low 216 (address >> 8) & 0xFF, // high 217 len, // count 218 0x00, 0x00, 0x00, 0x00, 0x00, 0x03}; 219 command[11] = ~checksum_add_uint8 (command + 3, 8, 0x00) + 1; 220 dc_status_t rc = zeagle_n2ition3_packet (device, command, sizeof (command), answer, 13 + len + 6); 221 if (rc != DC_STATUS_SUCCESS) 222 return rc; 223 224 memcpy (data, answer + 17, len); 225 226 nbytes += len; 227 address += len; 228 data += len; 229 } 230 231 return DC_STATUS_SUCCESS; 232 } 233 234 235 static dc_status_t 236 zeagle_n2ition3_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) 237 { 238 // Allocate the required amount of memory. 239 if (!dc_buffer_resize (buffer, SZ_MEMORY)) { 240 ERROR (abstract->context, "Insufficient buffer space available."); 241 return DC_STATUS_NOMEMORY; 242 } 243 244 return device_dump_read (abstract, 0, dc_buffer_get_data (buffer), 245 dc_buffer_get_size (buffer), SZ_PACKET); 246 } 247 248 249 static dc_status_t 250 zeagle_n2ition3_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata) 251 { 252 zeagle_n2ition3_device_t *device = (zeagle_n2ition3_device_t *) abstract; 253 254 // Enable progress notifications. 255 dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER; 256 progress.maximum = (RB_LOGBOOK_END - RB_LOGBOOK_BEGIN) * 2 + 8 + 257 (RB_PROFILE_END - RB_PROFILE_BEGIN); 258 device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); 259 260 // Read the configuration data. 261 unsigned char config[(RB_LOGBOOK_END - RB_LOGBOOK_BEGIN) * 2 + 8] = {0}; 262 dc_status_t rc = zeagle_n2ition3_device_read (abstract, RB_LOGBOOK_OFFSET, config, sizeof (config)); 263 if (rc != DC_STATUS_SUCCESS) { 264 ERROR (abstract->context, "Failed to read the configuration data."); 265 return rc; 266 } 267 268 // Get the logbook pointers. 269 unsigned int last = config[0x7C]; 270 unsigned int first = config[0x7D]; 271 if (first < RB_LOGBOOK_BEGIN || first >= RB_LOGBOOK_END || 272 last < RB_LOGBOOK_BEGIN || last >= RB_LOGBOOK_END) { 273 if (last == 0xFF) 274 return DC_STATUS_SUCCESS; 275 ERROR (abstract->context, "Invalid ringbuffer pointer detected (0x%02x 0x%02x).", first, last); 276 return DC_STATUS_DATAFORMAT; 277 } 278 279 // Get the number of logbook items. 280 unsigned int count = ringbuffer_distance (first, last, DC_RINGBUFFER_EMPTY, RB_LOGBOOK_BEGIN, RB_LOGBOOK_END) + 1; 281 282 // Get the profile pointer. 283 unsigned int eop = array_uint16_le (config + 0x7E); 284 if (eop < RB_PROFILE_BEGIN || eop >= RB_PROFILE_END) { 285 ERROR (abstract->context, "Invalid ringbuffer pointer detected (0x%04x).", eop); 286 return DC_STATUS_DATAFORMAT; 287 } 288 289 // The logbook ringbuffer can store at most 60 dives, even if the profile 290 // data could store more (e.g. many small dives). But it's also possible 291 // that the profile ringbuffer is filled faster than the logbook ringbuffer 292 // (e.g. many large dives). We detect this by checking the total length. 293 unsigned int total = 0; 294 unsigned int idx = last; 295 unsigned int previous = eop; 296 for (unsigned int i = 0; i < count; ++i) { 297 // Get the pointer to the profile data. 298 unsigned int current = array_uint16_le (config + 2 * idx); 299 if (current < RB_PROFILE_BEGIN || current >= RB_PROFILE_END) { 300 ERROR (abstract->context, "Invalid ringbuffer pointer detected (0x%04x).", current); 301 return DC_STATUS_DATAFORMAT; 302 } 303 304 // Get the profile length. 305 unsigned int length = ringbuffer_distance (current, previous, DC_RINGBUFFER_FULL, RB_PROFILE_BEGIN, RB_PROFILE_END); 306 307 // Check for a ringbuffer overflow. 308 if (total + length > RB_PROFILE_END - RB_PROFILE_BEGIN) { 309 count = i; 310 break; 311 } 312 313 total += length; 314 315 previous = current; 316 317 if (idx == RB_LOGBOOK_BEGIN) 318 idx = RB_LOGBOOK_END; 319 idx--; 320 } 321 322 // Update and emit a progress event. 323 progress.current += sizeof (config); 324 progress.maximum = (RB_LOGBOOK_END - RB_LOGBOOK_BEGIN) * 2 + 8 + total; 325 device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); 326 327 // Create the ringbuffer stream. 328 dc_rbstream_t *rbstream = NULL; 329 rc = dc_rbstream_new (&rbstream, abstract, 1, SZ_PACKET, RB_PROFILE_BEGIN, RB_PROFILE_END, eop, DC_RBSTREAM_BACKWARD); 330 if (rc != DC_STATUS_SUCCESS) { 331 ERROR (abstract->context, "Failed to create the ringbuffer stream."); 332 return rc; 333 } 334 335 // Memory buffer for the profile data. 336 unsigned char buffer[RB_PROFILE_END - RB_PROFILE_BEGIN] = {0}; 337 338 unsigned int offset = RB_PROFILE_END - RB_PROFILE_BEGIN; 339 340 idx = last; 341 previous = eop; 342 for (unsigned int i = 0; i < count; ++i) { 343 // Get the pointer to the profile data. 344 unsigned int current = array_uint16_le (config + 2 * idx); 345 346 // Get the profile length. 347 unsigned int length = ringbuffer_distance (current, previous, DC_RINGBUFFER_FULL, RB_PROFILE_BEGIN, RB_PROFILE_END); 348 349 // Move to the begin of the current dive. 350 offset -= length; 351 352 // Read the dive. 353 rc = dc_rbstream_read (rbstream, &progress, buffer + offset, length); 354 if (rc != DC_STATUS_SUCCESS) { 355 ERROR (abstract->context, "Failed to read the dive."); 356 dc_rbstream_free (rbstream); 357 return rc; 358 } 359 360 unsigned char *p = buffer + offset; 361 362 if (memcmp (p, device->fingerprint, sizeof (device->fingerprint)) == 0) { 363 dc_rbstream_free (rbstream); 364 return DC_STATUS_SUCCESS; 365 } 366 367 if (callback && !callback (p, length, p, sizeof (device->fingerprint), userdata)) { 368 dc_rbstream_free (rbstream); 369 return DC_STATUS_SUCCESS; 370 } 371 372 previous = current; 373 374 if (idx == RB_LOGBOOK_BEGIN) 375 idx = RB_LOGBOOK_END; 376 idx--; 377 } 378 379 dc_rbstream_free (rbstream); 380 381 return DC_STATUS_SUCCESS; 382 }