shearwater_petrel.c (12749B)
1 /* 2 * libdivecomputer 3 * 4 * Copyright (C) 2013 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 "shearwater_petrel.h" 26 #include "shearwater_common.h" 27 #include "context-private.h" 28 #include "device-private.h" 29 #include "platform.h" 30 #include "array.h" 31 32 #define ISINSTANCE(device) dc_device_isinstance((device), &shearwater_petrel_device_vtable) 33 34 #define MANIFEST_ADDR 0xE0000000 35 #define MANIFEST_SIZE 0x600 36 37 #define DIVE_SIZE 0xFFFFFF 38 39 #define RECORD_SIZE 0x20 40 #define RECORD_COUNT (MANIFEST_SIZE / RECORD_SIZE) 41 42 typedef struct shearwater_petrel_device_t { 43 shearwater_common_device_t base; 44 unsigned char fingerprint[4]; 45 } shearwater_petrel_device_t; 46 47 48 static dc_status_t shearwater_petrel_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size); 49 static dc_status_t shearwater_petrel_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata); 50 static dc_status_t shearwater_petrel_device_timesync (dc_device_t *abstract, const dc_datetime_t *datetime); 51 static dc_status_t shearwater_petrel_device_close (dc_device_t *abstract); 52 53 static const dc_device_vtable_t shearwater_petrel_device_vtable = { 54 sizeof(shearwater_petrel_device_t), 55 DC_FAMILY_SHEARWATER_PETREL, 56 shearwater_petrel_device_set_fingerprint, /* set_fingerprint */ 57 NULL, /* read */ 58 NULL, /* write */ 59 NULL, /* dump */ 60 shearwater_petrel_device_foreach, /* foreach */ 61 shearwater_petrel_device_timesync, 62 shearwater_petrel_device_close /* close */ 63 }; 64 65 66 static unsigned int 67 str2num (unsigned char data[], unsigned int size, unsigned int offset) 68 { 69 unsigned int value = 0; 70 for (unsigned int i = offset; i < size; ++i) { 71 if (data[i] < '0' || data[i] > '9') 72 break; 73 value *= 10; 74 value += data[i] - '0'; 75 } 76 77 return value; 78 } 79 80 81 dc_status_t 82 shearwater_petrel_device_open (dc_device_t **out, dc_context_t *context, dc_iostream_t *iostream, unsigned int model) 83 { 84 dc_status_t status = DC_STATUS_SUCCESS; 85 shearwater_petrel_device_t *device = NULL; 86 87 if (out == NULL) 88 return DC_STATUS_INVALIDARGS; 89 90 // Allocate memory. 91 device = (shearwater_petrel_device_t *) dc_device_allocate (context, &shearwater_petrel_device_vtable); 92 if (device == NULL) { 93 ERROR (context, "Failed to allocate memory."); 94 return DC_STATUS_NOMEMORY; 95 } 96 97 // Set the default values. 98 memset (device->fingerprint, 0, sizeof (device->fingerprint)); 99 100 // Setup the device. 101 status = shearwater_common_setup (&device->base, context, iostream, model); 102 if (status != DC_STATUS_SUCCESS) { 103 goto error_free; 104 } 105 106 *out = (dc_device_t *) device; 107 108 return DC_STATUS_SUCCESS; 109 110 error_free: 111 dc_device_deallocate ((dc_device_t *) device); 112 return status; 113 } 114 115 116 static dc_status_t 117 shearwater_petrel_device_close (dc_device_t *abstract) 118 { 119 dc_status_t status = DC_STATUS_SUCCESS; 120 shearwater_common_device_t *device = (shearwater_common_device_t *) abstract; 121 dc_status_t rc = DC_STATUS_SUCCESS; 122 123 // Shutdown the device. 124 unsigned char request[] = {0x2E, 0x90, 0x20, 0x00}; 125 rc = shearwater_common_transfer (device, request, sizeof (request), NULL, 0, NULL); 126 if (rc != DC_STATUS_SUCCESS) { 127 dc_status_set_error(&status, rc); 128 } 129 130 return status; 131 } 132 133 134 static dc_status_t 135 shearwater_petrel_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size) 136 { 137 shearwater_petrel_device_t *device = (shearwater_petrel_device_t *) abstract; 138 139 if (size && size != sizeof (device->fingerprint)) 140 return DC_STATUS_INVALIDARGS; 141 142 if (size) 143 memcpy (device->fingerprint, data, sizeof (device->fingerprint)); 144 else 145 memset (device->fingerprint, 0, sizeof (device->fingerprint)); 146 147 return DC_STATUS_SUCCESS; 148 } 149 150 151 static dc_status_t 152 shearwater_petrel_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata) 153 { 154 shearwater_petrel_device_t *device = (shearwater_petrel_device_t *) abstract; 155 dc_status_t rc = DC_STATUS_SUCCESS; 156 157 // Enable progress notifications. 158 unsigned int current = 0, maximum = 0; 159 dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER; 160 device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); 161 162 // Read the serial number. 163 unsigned char rsp_serial[8] = {0}; 164 rc = shearwater_common_rdbi (&device->base, ID_SERIAL, rsp_serial, sizeof(rsp_serial), NULL); 165 if (rc != DC_STATUS_SUCCESS) { 166 ERROR (abstract->context, "Failed to read the serial number."); 167 return rc; 168 } 169 170 HEXDUMP(abstract->context, DC_LOGLEVEL_DEBUG, "Serial", rsp_serial, sizeof(rsp_serial)); 171 172 // Convert to a number. 173 unsigned char serial[4] = {0}; 174 if (array_convert_hex2bin (rsp_serial, sizeof(rsp_serial), serial, sizeof (serial)) != 0 ) { 175 ERROR (abstract->context, "Failed to convert the serial number."); 176 return DC_STATUS_DATAFORMAT; 177 } 178 179 // Read the firmware version. 180 unsigned char rsp_firmware[12] = {0}; 181 unsigned int rsp_firmware_length = 0; 182 rc = shearwater_common_rdbi (&device->base, ID_FIRMWARE, rsp_firmware, sizeof(rsp_firmware), &rsp_firmware_length); 183 if (rc != DC_STATUS_SUCCESS) { 184 ERROR (abstract->context, "Failed to read the firmware version."); 185 return rc; 186 } 187 188 HEXDUMP(abstract->context, DC_LOGLEVEL_DEBUG, "Firmware", rsp_firmware, rsp_firmware_length); 189 190 // Convert to a number. 191 unsigned int firmware = str2num (rsp_firmware, rsp_firmware_length, 1); 192 193 // Read the model number (ID_MODEL, RDBI 0x8060) — Product-Version byte, 194 // authoritative for the coarse product family (aligns with upstream libdc). 195 unsigned char rsp_model = 0; 196 rc = shearwater_common_rdbi (&device->base, ID_MODEL, &rsp_model, sizeof(rsp_model), NULL); 197 if (rc != DC_STATUS_SUCCESS) { 198 ERROR (abstract->context, "Failed to read the model number."); 199 return rc; 200 } 201 202 // Attempt to read the hardware type (ID_HARDWARE, RDBI 0x8050) for 203 // consumer-side sub-model differentiation (e.g. Petrel 1 vs Petrel 2). 204 // Best-effort: FWID can change across firmware updates and stored logs 205 // do not carry it. Failure is non-fatal; hw_id remains 0. 206 unsigned char rsp_hardware[2] = {0}; 207 unsigned int fwid = 0; 208 dc_status_t hw_rc = shearwater_common_rdbi (&device->base, ID_HARDWARE, rsp_hardware, sizeof(rsp_hardware), NULL); 209 if (hw_rc == DC_STATUS_SUCCESS) { 210 fwid = array_uint16_be (rsp_hardware); 211 DEBUG (abstract->context, "Hardware type (FWID): 0x%04x", fwid); 212 } 213 214 // Emit a device info event. 215 dc_event_devinfo_t devinfo; 216 devinfo.model = rsp_model; 217 devinfo.firmware = firmware; 218 devinfo.serial = array_uint32_be (serial); 219 device_set_hw_id (abstract, fwid); 220 device_event_emit (abstract, DC_EVENT_DEVINFO, &devinfo); 221 222 // Read the logbook type 223 unsigned char rsp_logupload[9] = {0}; 224 rc = shearwater_common_rdbi (&device->base, ID_LOGUPLOAD, rsp_logupload, sizeof(rsp_logupload), NULL); 225 if (rc != DC_STATUS_SUCCESS) { 226 ERROR (abstract->context, "Failed to read the logbook type."); 227 return rc; 228 } 229 230 unsigned int base_addr = array_uint32_be (rsp_logupload + 1); 231 switch (base_addr) { 232 case 0xDD000000: // Predator - we shouldn't get here, we could give up or we can try 0xC0000000 233 case 0xC0000000: // Predator-Like Format (what we used to call the Petrel format) 234 case 0x90000000: // some firmware versions supported an earlier version of PNF without final record 235 // use the Predator-Like Format instead 236 base_addr = 0xC0000000; 237 break; 238 case 0x80000000: // new Petrel Native Format with final record 239 // that's the correct address 240 break; 241 default: // unknown format 242 ERROR (abstract->context, "Unknown logbook format %08x", base_addr); 243 return DC_STATUS_DATAFORMAT; 244 } 245 246 // Allocate memory buffers for the manifests. 247 dc_buffer_t *buffer = dc_buffer_new (MANIFEST_SIZE); 248 dc_buffer_t *manifests = dc_buffer_new (MANIFEST_SIZE); 249 if (buffer == NULL || manifests == NULL) { 250 ERROR (abstract->context, "Insufficient buffer space available."); 251 dc_buffer_free (buffer); 252 dc_buffer_free (manifests); 253 return DC_STATUS_NOMEMORY; 254 } 255 256 // Read the manifest pages 257 while (1) { 258 // Update the progress state. 259 // Assume the worst case scenario of a full manifest, and adjust the 260 // value with the actual number of dives after the manifest has been 261 // processed. 262 maximum += 1 + RECORD_COUNT; 263 264 // Download a manifest. 265 progress.current = NSTEPS * current; 266 progress.maximum = NSTEPS * maximum; 267 rc = shearwater_common_download (&device->base, buffer, MANIFEST_ADDR, MANIFEST_SIZE, 0, &progress); 268 if (rc != DC_STATUS_SUCCESS) { 269 ERROR (abstract->context, "Failed to download the manifest."); 270 dc_buffer_free (buffer); 271 dc_buffer_free (manifests); 272 return rc; 273 } 274 275 HEXDUMP(abstract->context, DC_LOGLEVEL_DEBUG, "Manifest", dc_buffer_get_data (buffer), dc_buffer_get_size (buffer)); 276 277 // Cache the buffer pointer and size. 278 unsigned char *data = dc_buffer_get_data (buffer); 279 unsigned int size = dc_buffer_get_size (buffer); 280 281 // Process the records in the manifest. 282 unsigned int count = 0, deleted = 0; 283 unsigned int offset = 0; 284 while (offset < size) { 285 // Check for a valid dive header. 286 unsigned int header = array_uint16_be (data + offset); 287 if (header == 0x5A23) { 288 // this is a deleted dive; keep looking 289 offset += RECORD_SIZE; 290 deleted++; 291 continue; 292 } 293 if (header != 0xA5C4) 294 break; 295 296 // Check the fingerprint data. 297 if (memcmp (data + offset + 4, device->fingerprint, sizeof (device->fingerprint)) == 0) 298 break; 299 300 offset += RECORD_SIZE; 301 count++; 302 } 303 304 // Update the progress state. 305 current += 1; 306 maximum -= RECORD_COUNT - count - deleted; 307 308 // Append the manifest records to the main buffer. 309 if (!dc_buffer_append (manifests, data, count * RECORD_SIZE)) { 310 ERROR (abstract->context, "Insufficient buffer space available."); 311 dc_buffer_free (buffer); 312 dc_buffer_free (manifests); 313 return DC_STATUS_NOMEMORY; 314 } 315 316 // Stop downloading manifest if there are no more records. 317 if (count + deleted != RECORD_COUNT) 318 break; 319 } 320 321 // Update and emit a progress event. 322 progress.current = NSTEPS * current; 323 progress.maximum = NSTEPS * maximum; 324 device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); 325 326 // Cache the buffer pointer and size. 327 unsigned char *data = dc_buffer_get_data (manifests); 328 unsigned int size = dc_buffer_get_size (manifests); 329 330 unsigned int offset = 0; 331 while (offset < size) { 332 // skip deleted dives 333 if (array_uint16_be(data + offset) == 0x5A23) { 334 offset += RECORD_SIZE; 335 continue; 336 } 337 // Get the address of the dive. 338 unsigned int address = array_uint32_be (data + offset + 20); 339 340 // Download the dive. 341 progress.current = NSTEPS * current; 342 progress.maximum = NSTEPS * maximum; 343 rc = shearwater_common_download (&device->base, buffer, base_addr + address, DIVE_SIZE, 1, &progress); 344 if (rc != DC_STATUS_SUCCESS) { 345 ERROR (abstract->context, "Failed to download the dive."); 346 dc_buffer_free (buffer); 347 dc_buffer_free (manifests); 348 return rc; 349 } 350 351 // Update the progress state. 352 current += 1; 353 354 unsigned char *buf = dc_buffer_get_data (buffer); 355 unsigned int len = dc_buffer_get_size (buffer); 356 if (callback && !callback (buf, len, buf + 12, sizeof (device->fingerprint), userdata)) 357 break; 358 359 offset += RECORD_SIZE; 360 } 361 362 // Update and emit a progress event. 363 progress.current = NSTEPS * current; 364 progress.maximum = NSTEPS * maximum; 365 device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); 366 367 dc_buffer_free (manifests); 368 dc_buffer_free (buffer); 369 370 return rc; 371 } 372 373 static dc_status_t 374 shearwater_petrel_device_timesync (dc_device_t *abstract, const dc_datetime_t *datetime) 375 { 376 shearwater_common_device_t *device = (shearwater_common_device_t *) abstract; 377 378 // Read ID_MODEL (RDBI 0x8060) directly to determine time-sync variant. 379 unsigned char rsp_model = 0; 380 dc_status_t status = shearwater_common_rdbi (device, ID_MODEL, &rsp_model, sizeof(rsp_model), NULL); 381 if (status != DC_STATUS_SUCCESS) { 382 ERROR (abstract->context, "Failed to read the model number."); 383 return status; 384 } 385 386 if (rsp_model == TERIC) { 387 return shearwater_common_timesync_utc (device, datetime); 388 } else { 389 return shearwater_common_timesync_local (device, datetime); 390 } 391 }