dc_status_t device_dump_read (dc_device_t *device, unsigned char data[], unsigned int size, unsigned int blocksize) { if (device == NULL) return DC_STATUS_UNSUPPORTED; if (device->vtable->read == NULL) return DC_STATUS_UNSUPPORTED; // Enable progress notifications. dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER; progress.maximum = size; device_event_emit (device, DC_EVENT_PROGRESS, &progress); unsigned int nbytes = 0; while (nbytes < size) { // Calculate the packet size. unsigned int len = size - nbytes; if (len > blocksize) len = blocksize; // Read the packet. dc_status_t rc = device->vtable->read (device, nbytes, data + nbytes, len); if (rc != DC_STATUS_SUCCESS) return rc; // Update and emit a progress event. progress.current += len; device_event_emit (device, DC_EVENT_PROGRESS, &progress); nbytes += len; } return DC_STATUS_SUCCESS; }
static device_status_t suunto_eon_device_dump (device_t *abstract, dc_buffer_t *buffer) { suunto_eon_device_t *device = (suunto_eon_device_t*) abstract; if (! device_is_suunto_eon (abstract)) return DEVICE_STATUS_TYPE_MISMATCH; // Erase the current contents of the buffer and // pre-allocate the required amount of memory. if (!dc_buffer_clear (buffer) || !dc_buffer_reserve (buffer, SUUNTO_EON_MEMORY_SIZE)) { WARNING ("Insufficient buffer space available."); return DEVICE_STATUS_MEMORY; } // Enable progress notifications. device_progress_t progress = DEVICE_PROGRESS_INITIALIZER; progress.maximum = SUUNTO_EON_MEMORY_SIZE + 1; device_event_emit (abstract, DEVICE_EVENT_PROGRESS, &progress); // Send the command. unsigned char command[1] = {'P'}; int rc = serial_write (device->port, command, sizeof (command)); if (rc != sizeof (command)) { WARNING ("Failed to send the command."); return EXITCODE (rc); } // Receive the answer. unsigned char answer[SUUNTO_EON_MEMORY_SIZE + 1] = {0}; rc = serial_read (device->port, answer, sizeof (answer)); if (rc != sizeof (answer)) { WARNING ("Failed to receive the answer."); return EXITCODE (rc); } // Update and emit a progress event. progress.current += sizeof (answer); device_event_emit (abstract, DEVICE_EVENT_PROGRESS, &progress); // Verify the checksum of the package. unsigned char crc = answer[sizeof (answer) - 1]; unsigned char ccrc = checksum_add_uint8 (answer, sizeof (answer) - 1, 0x00); if (crc != ccrc) { WARNING ("Unexpected answer CRC."); return DEVICE_STATUS_PROTOCOL; } dc_buffer_append (buffer, answer, SUUNTO_EON_MEMORY_SIZE); return DEVICE_STATUS_SUCCESS; }
static device_status_t suunto_eon_device_foreach (device_t *abstract, dive_callback_t callback, void *userdata) { dc_buffer_t *buffer = dc_buffer_new (SUUNTO_EON_MEMORY_SIZE); if (buffer == NULL) return DEVICE_STATUS_MEMORY; device_status_t rc = suunto_eon_device_dump (abstract, buffer); if (rc != DEVICE_STATUS_SUCCESS) { dc_buffer_free (buffer); return rc; } // Emit a device info event. unsigned char *data = dc_buffer_get_data (buffer); device_devinfo_t devinfo; devinfo.model = 0; devinfo.firmware = 0; devinfo.serial = array_uint24_be (data + 244); device_event_emit (abstract, DEVICE_EVENT_DEVINFO, &devinfo); rc = suunto_eon_extract_dives (abstract, dc_buffer_get_data (buffer), dc_buffer_get_size (buffer), callback, userdata); dc_buffer_free (buffer); return rc; }
static dc_status_t shearwater_predator_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata) { dc_buffer_t *buffer = dc_buffer_new (SZ_MEMORY); if (buffer == NULL) return DC_STATUS_NOMEMORY; dc_status_t rc = shearwater_predator_device_dump (abstract, buffer); if (rc != DC_STATUS_SUCCESS) { dc_buffer_free (buffer); return rc; } // Emit a device info event. unsigned char *data = dc_buffer_get_data (buffer); dc_event_devinfo_t devinfo; devinfo.model = data[0x2000D]; devinfo.firmware = bcd2dec (data[0x2000A]); devinfo.serial = array_uint32_be (data + 0x20002); device_event_emit (abstract, DC_EVENT_DEVINFO, &devinfo); rc = shearwater_predator_extract_dives (abstract, data, SZ_MEMORY, callback, userdata); dc_buffer_free (buffer); return rc; }
static dc_status_t hw_ostc_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata) { dc_buffer_t *buffer = dc_buffer_new (0); if (buffer == NULL) return DC_STATUS_NOMEMORY; dc_status_t rc = hw_ostc_device_dump (abstract, buffer); if (rc != DC_STATUS_SUCCESS) { dc_buffer_free (buffer); return rc; } // Emit a device info event. unsigned char *data = dc_buffer_get_data (buffer); dc_event_devinfo_t devinfo; devinfo.firmware = array_uint16_be (data + 264); devinfo.serial = array_uint16_le (data + 6); if (devinfo.serial > 7000) devinfo.model = 3; // OSTC 2C else if (devinfo.serial > 2048) devinfo.model = 2; // OSTC 2N else if (devinfo.serial > 300) devinfo.model = 1; // OSTC Mk2 else devinfo.model = 0; // OSTC device_event_emit (abstract, DC_EVENT_DEVINFO, &devinfo); rc = hw_ostc_extract_dives (abstract, dc_buffer_get_data (buffer), dc_buffer_get_size (buffer), callback, userdata); dc_buffer_free (buffer); return rc; }
static dc_status_t hw_ostc3_transfer (hw_ostc3_device_t *device, dc_event_progress_t *progress, unsigned char cmd, const unsigned char input[], unsigned int isize, unsigned char output[], unsigned int osize) { dc_device_t *abstract = (dc_device_t *) device; if (device_is_cancelled (abstract)) return DC_STATUS_CANCELLED; // Send the command. unsigned char command[1] = {cmd}; int n = serial_write (device->port, command, sizeof (command)); if (n != sizeof (command)) { ERROR (abstract->context, "Failed to send the command."); return EXITCODE (n); } // Read the echo. unsigned char echo[1] = {0}; n = serial_read (device->port, echo, sizeof (echo)); if (n != sizeof (echo)) { ERROR (abstract->context, "Failed to receive the echo."); return EXITCODE (n); } // Verify the echo. if (memcmp (echo, command, sizeof (command)) != 0) { ERROR (abstract->context, "Unexpected echo."); return DC_STATUS_PROTOCOL; } if (input) { // Send the input data packet. n = serial_write (device->port, input, isize); if (n != isize) { ERROR (abstract->context, "Failed to send the data packet."); return EXITCODE (n); } } if (output) { unsigned int nbytes = 0; while (nbytes < osize) { // Set the minimum packet size. unsigned int len = 1024; // Increase the packet size if more data is immediately available. int available = serial_get_received (device->port); if (available > len) len = available; // Limit the packet size to the total size. if (nbytes + len > osize) len = osize - nbytes; // Read the packet. n = serial_read (device->port, output + nbytes, len); if (n != len) { ERROR (abstract->context, "Failed to receive the answer."); return EXITCODE (n); } // Update and emit a progress event. if (progress) { progress->current += len; device_event_emit ((dc_device_t *) device, DC_EVENT_PROGRESS, progress); } nbytes += len; } } if (cmd != EXIT) { // Read the ready byte. unsigned char ready[1] = {0}; n = serial_read (device->port, ready, sizeof (ready)); if (n != sizeof (ready)) { ERROR (abstract->context, "Failed to receive the ready byte."); return EXITCODE (n); } // Verify the ready byte. if (ready[0] != READY) { ERROR (abstract->context, "Unexpected ready byte."); return DC_STATUS_PROTOCOL; } } return DC_STATUS_SUCCESS; }
dc_status_t hw_ostc_device_screenshot (dc_device_t *abstract, dc_buffer_t *buffer, hw_ostc_format_t format) { hw_ostc_device_t *device = (hw_ostc_device_t *) abstract; if (!ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; // Erase the current contents of the buffer. if (!dc_buffer_clear (buffer)) { ERROR (abstract->context, "Insufficient buffer space available."); return DC_STATUS_NOMEMORY; } // Bytes per pixel (RGB formats only). unsigned int bpp = 0; if (format == HW_OSTC_FORMAT_RAW) { // The RAW format has a variable size, depending on the actual image // content. Usually the total size is around 4K, which is used as an // initial guess and expanded when necessary. if (!dc_buffer_reserve (buffer, 4096)) { ERROR (abstract->context, "Insufficient buffer space available."); return DC_STATUS_NOMEMORY; } } else { // The RGB formats have a fixed size, depending only on the dimensions // and the number of bytes per pixel. The required amount of memory is // allocated immediately. bpp = (format == HW_OSTC_FORMAT_RGB16) ? 2 : 3; if (!dc_buffer_resize (buffer, WIDTH * HEIGHT * bpp)) { ERROR (abstract->context, "Insufficient buffer space available."); return DC_STATUS_NOMEMORY; } } // Enable progress notifications. dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER; progress.maximum = WIDTH * HEIGHT; device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); // Send the command. dc_status_t rc = hw_ostc_send (device, 'l', 1); if (rc != DC_STATUS_SUCCESS) return rc; // Cache the pointer to the image data (RGB formats only). unsigned char *image = dc_buffer_get_data (buffer); // The OSTC sends the image data in a column by column layout, which is // converted on the fly to a more convenient row by row layout as used // in the majority of image formats. This conversions requires knowledge // of the pixel coordinates. unsigned int x = 0, y = 0; unsigned int npixels = 0; while (npixels < WIDTH * HEIGHT) { unsigned char raw[3] = {0}; int n = serial_read (device->port, raw, 1); if (n != 1) { ERROR (abstract->context, "Failed to receive the packet."); return EXITCODE (n); } unsigned int nbytes = n; unsigned int count = raw[0]; if ((count & 0x80) == 0x00) { // Black pixel. raw[1] = raw[2] = BLACK; count &= 0x7F; } else if ((count & 0xC0) == 0xC0) { // White pixel. raw[1] = raw[2] = WHITE; count &= 0x3F; } else { // Color pixel. n = serial_read (device->port, raw + 1, 2); if (n != 2) { ERROR (abstract->context, "Failed to receive the packet."); return EXITCODE (n); } nbytes += n; count &= 0x3F; } count++; // Check for buffer overflows. if (npixels + count > WIDTH * HEIGHT) { ERROR (abstract->context, "Unexpected number of pixels received."); return DC_STATUS_DATAFORMAT; } if (format == HW_OSTC_FORMAT_RAW) { // Append the raw data to the output buffer. dc_buffer_append (buffer, raw, nbytes); } else { // Store the decompressed data in the output buffer. for (unsigned int i = 0; i < count; ++i) { // Calculate the offset to the current pixel (row layout) unsigned int offset = (y * WIDTH + x) * bpp; if (format == HW_OSTC_FORMAT_RGB16) { image[offset + 0] = raw[1]; image[offset + 1] = raw[2]; } else { unsigned int value = (raw[1] << 8) + raw[2]; unsigned char r = (value & 0xF800) >> 11; unsigned char g = (value & 0x07E0) >> 5; unsigned char b = (value & 0x001F); image[offset + 0] = 255 * r / 31; image[offset + 1] = 255 * g / 63; image[offset + 2] = 255 * b / 31; } // Move to the next pixel coordinate (column layout). y++; if (y == HEIGHT) { y = 0; x++; } } } // Update and emit a progress event. progress.current += count; device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); npixels += count; } return DC_STATUS_SUCCESS; }
static dc_status_t reefnet_sensus_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) { reefnet_sensus_device_t *device = (reefnet_sensus_device_t*) abstract; // Erase the current contents of the buffer and // pre-allocate the required amount of memory. if (!dc_buffer_clear (buffer) || !dc_buffer_reserve (buffer, SZ_MEMORY)) { ERROR (abstract->context, "Insufficient buffer space available."); return DC_STATUS_NOMEMORY; } // Enable progress notifications. dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER; progress.maximum = 4 + SZ_MEMORY + 2 + 3; device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); // Wake-up the device. dc_status_t rc = reefnet_sensus_handshake (device); if (rc != DC_STATUS_SUCCESS) return rc; // Send the command to the device. unsigned char command = 0x40; int n = serial_write (device->port, &command, 1); if (n != 1) { ERROR (abstract->context, "Failed to send the command."); return EXITCODE (n); } // The device leaves the waiting state. device->waiting = 0; // Receive the answer from the device. unsigned int nbytes = 0; unsigned char answer[4 + SZ_MEMORY + 2 + 3] = {0}; while (nbytes < sizeof (answer)) { unsigned int len = sizeof (answer) - nbytes; if (len > 128) len = 128; n = serial_read (device->port, answer + nbytes, len); if (n != len) { ERROR (abstract->context, "Failed to receive the answer."); return EXITCODE (n); } // Update and emit a progress event. progress.current += len; device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); nbytes += len; } // Verify the headers of the package. if (memcmp (answer, "DATA", 4) != 0 || memcmp (answer + sizeof (answer) - 3, "END", 3) != 0) { ERROR (abstract->context, "Unexpected answer start or end byte(s)."); return DC_STATUS_PROTOCOL; } // Verify the checksum of the package. unsigned short crc = array_uint16_le (answer + 4 + SZ_MEMORY); unsigned short ccrc = checksum_add_uint16 (answer + 4, SZ_MEMORY, 0x00); if (crc != ccrc) { ERROR (abstract->context, "Unexpected answer checksum."); return DC_STATUS_PROTOCOL; } dc_buffer_append (buffer, answer + 4, SZ_MEMORY); return DC_STATUS_SUCCESS; }
static dc_status_t reefnet_sensus_handshake (reefnet_sensus_device_t *device) { dc_device_t *abstract = (dc_device_t *) device; // Send the command to the device. unsigned char command = 0x0A; int n = serial_write (device->port, &command, 1); if (n != 1) { ERROR (abstract->context, "Failed to send the command."); return EXITCODE (n); } // Receive the answer from the device. unsigned char handshake[SZ_HANDSHAKE + 2] = {0}; n = serial_read (device->port, handshake, sizeof (handshake)); if (n != sizeof (handshake)) { ERROR (abstract->context, "Failed to receive the handshake."); return EXITCODE (n); } // Verify the header of the packet. if (handshake[0] != 'O' || handshake[1] != 'K') { ERROR (abstract->context, "Unexpected answer header."); return DC_STATUS_PROTOCOL; } // The device is now waiting for a data request. device->waiting = 1; // Store the clock calibration values. device->systime = dc_datetime_now (); device->devtime = array_uint32_le (handshake + 8); // Store the handshake packet. memcpy (device->handshake, handshake + 2, SZ_HANDSHAKE); // Emit a clock event. dc_event_clock_t clock; clock.systime = device->systime; clock.devtime = device->devtime; device_event_emit (&device->base, DC_EVENT_CLOCK, &clock); // Emit a device info event. dc_event_devinfo_t devinfo; devinfo.model = handshake[2] - '0'; devinfo.firmware = handshake[3] - '0'; devinfo.serial = array_uint16_le (handshake + 6); device_event_emit (&device->base, DC_EVENT_DEVINFO, &devinfo); // Emit a vendor event. dc_event_vendor_t vendor; vendor.data = device->handshake; vendor.size = sizeof (device->handshake); device_event_emit (abstract, DC_EVENT_VENDOR, &vendor); // Wait at least 10 ms to ensures the data line is // clear before transmission from the host begins. serial_sleep (device->port, 10); return DC_STATUS_SUCCESS; }
static dc_status_t atomics_cobalt_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata) { atomics_cobalt_device_t *device = (atomics_cobalt_device_t *) abstract; // Enable progress notifications. dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER; progress.maximum = SZ_MEMORY + 2; device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); // Emit a vendor event. dc_event_vendor_t vendor; vendor.data = device->version; vendor.size = sizeof (device->version); device_event_emit (abstract, DC_EVENT_VENDOR, &vendor); // Emit a device info event. dc_event_devinfo_t devinfo; devinfo.model = array_uint16_le (device->version + 12); devinfo.firmware = (array_uint16_le (device->version + 8) << 16) + array_uint16_le (device->version + 10); devinfo.serial = 0; for (unsigned int i = 0; i < 8; ++i) { devinfo.serial *= 10; devinfo.serial += device->version[i] - '0'; } device_event_emit (abstract, DC_EVENT_DEVINFO, &devinfo); // Allocate a memory buffer. dc_buffer_t *buffer = dc_buffer_new (0); if (buffer == NULL) return DC_STATUS_NOMEMORY; unsigned int ndives = 0; dc_status_t rc = DC_STATUS_SUCCESS; while ((rc = atomics_cobalt_read_dive (abstract, buffer, (ndives == 0), &progress)) == DC_STATUS_SUCCESS) { unsigned char *data = dc_buffer_get_data (buffer); unsigned int size = dc_buffer_get_size (buffer); if (size == 0) { dc_buffer_free (buffer); return DC_STATUS_SUCCESS; } if (memcmp (data + FP_OFFSET, device->fingerprint, sizeof (device->fingerprint)) == 0) { dc_buffer_free (buffer); return DC_STATUS_SUCCESS; } if (callback && !callback (data, size, data + FP_OFFSET, sizeof (device->fingerprint), userdata)) { dc_buffer_free (buffer); return DC_STATUS_SUCCESS; } // Adjust the maximum value to take into account the two checksum bytes // for the next dive. Since we don't know the total number of dives in // advance, we can't calculate the total number of checksum bytes and // adjust the maximum on the fly. progress.maximum += 2; ndives++; } dc_buffer_free (buffer); return rc; }
static dc_status_t atomics_cobalt_read_dive (dc_device_t *abstract, dc_buffer_t *buffer, int init, dc_event_progress_t *progress) { #ifdef HAVE_LIBUSB atomics_cobalt_device_t *device = (atomics_cobalt_device_t *) abstract; if (device_is_cancelled (abstract)) return DC_STATUS_CANCELLED; // Erase the current contents of the buffer. if (!dc_buffer_clear (buffer)) { ERROR (abstract->context, "Insufficient buffer space available."); return DC_STATUS_NOMEMORY; } // Send the command to the dive computer. uint8_t bRequest = 0; if (device->simulation) bRequest = init ? 0x02 : 0x03; else bRequest = init ? 0x09 : 0x0A; int rc = libusb_control_transfer (device->handle, LIBUSB_RECIPIENT_DEVICE | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT, bRequest, 0, 0, NULL, 0, TIMEOUT); if (rc != LIBUSB_SUCCESS) { ERROR (abstract->context, "Failed to send the command."); return EXITCODE(rc); } HEXDUMP (abstract->context, DC_LOGLEVEL_INFO, "Write", &bRequest, 1); unsigned int nbytes = 0; while (1) { // Receive the answer from the dive computer. int length = 0; unsigned char packet[8 * 1024] = {0}; rc = libusb_bulk_transfer (device->handle, 0x82, packet, sizeof (packet), &length, TIMEOUT); if (rc != LIBUSB_SUCCESS) { ERROR (abstract->context, "Failed to receive the answer."); return EXITCODE(rc); } HEXDUMP (abstract->context, DC_LOGLEVEL_INFO, "Read", packet, length); // Update and emit a progress event. if (progress) { progress->current += length; device_event_emit (abstract, DC_EVENT_PROGRESS, progress); } // Append the packet to the output buffer. dc_buffer_append (buffer, packet, length); nbytes += length; // If we received fewer bytes than requested, the transfer is finished. if (length < sizeof (packet)) break; } // Check for a buffer error. if (dc_buffer_get_size (buffer) != nbytes) { ERROR (abstract->context, "Insufficient buffer space available."); return DC_STATUS_NOMEMORY; } // Check for the minimum length. if (nbytes < 2) { ERROR (abstract->context, "Data packet is too short."); return DC_STATUS_PROTOCOL; } // When only two 0xFF bytes are received, there are no more dives. unsigned char *data = dc_buffer_get_data (buffer); if (nbytes == 2 && data[0] == 0xFF && data[1] == 0xFF) { dc_buffer_clear (buffer); return DC_STATUS_SUCCESS; } // Verify the checksum of the packet. unsigned short crc = array_uint16_le (data + nbytes - 2); unsigned short ccrc = checksum_add_uint16 (data, nbytes - 2, 0x0); if (crc != ccrc) { ERROR (abstract->context, "Unexpected answer checksum."); return DC_STATUS_PROTOCOL; } // Remove the checksum bytes. dc_buffer_slice (buffer, 0, nbytes - 2); return DC_STATUS_SUCCESS; #else return DC_STATUS_UNSUPPORTED; #endif }
device_status_t suunto_common2_device_foreach (device_t *abstract, dive_callback_t callback, void *userdata) { suunto_common2_device_t *device = (suunto_common2_device_t*) abstract; // Enable progress notifications. device_progress_t progress = DEVICE_PROGRESS_INITIALIZER; progress.maximum = RB_PROFILE_END - RB_PROFILE_BEGIN + 8 + SZ_VERSION + (SZ_MINIMUM > 4 ? SZ_MINIMUM : 4); device_event_emit (abstract, DEVICE_EVENT_PROGRESS, &progress); // Read the version info. unsigned char version[SZ_VERSION] = {0}; device_status_t rc = suunto_common2_device_version (abstract, version, sizeof (version)); if (rc != DEVICE_STATUS_SUCCESS) { WARNING ("Cannot read memory header."); return rc; } // Update and emit a progress event. progress.current += sizeof (version); device_event_emit (abstract, DEVICE_EVENT_PROGRESS, &progress); // Read the serial number. unsigned char serial[SZ_MINIMUM > 4 ? SZ_MINIMUM : 4] = {0}; rc = suunto_common2_device_read (abstract, 0x0023, serial, sizeof (serial)); if (rc != DEVICE_STATUS_SUCCESS) { WARNING ("Cannot read memory header."); return rc; } // Update and emit a progress event. progress.current += sizeof (serial); device_event_emit (abstract, DEVICE_EVENT_PROGRESS, &progress); // Emit a device info event. device_devinfo_t devinfo; devinfo.model = version[0]; devinfo.firmware = array_uint24_be (version + 1); devinfo.serial = array_uint32_be (serial); device_event_emit (abstract, DEVICE_EVENT_DEVINFO, &devinfo); // Read the header bytes. unsigned char header[8] = {0}; rc = suunto_common2_device_read (abstract, 0x0190, header, sizeof (header)); if (rc != DEVICE_STATUS_SUCCESS) { WARNING ("Cannot read memory header."); return rc; } // Obtain the pointers from the header. unsigned int last = array_uint16_le (header + 0); unsigned int count = array_uint16_le (header + 2); unsigned int end = array_uint16_le (header + 4); unsigned int begin = array_uint16_le (header + 6); // Memory buffer to store all the dives. unsigned char data[SZ_MINIMUM + RB_PROFILE_END - RB_PROFILE_BEGIN] = {0}; // Calculate the total amount of bytes. unsigned int remaining = RB_PROFILE_DISTANCE (begin, end, count != 0); // Update and emit a progress event. progress.maximum -= (RB_PROFILE_END - RB_PROFILE_BEGIN) - remaining; progress.current += sizeof (header); device_event_emit (abstract, DEVICE_EVENT_PROGRESS, &progress); // To reduce the number of read operations, we always try to read // packages with the largest possible size. As a consequence, the // last package of a dive can contain data from more than one dive. // Therefore, the remaining data of this package (and its size) // needs to be preserved for the next dive. unsigned int available = 0; // The ring buffer is traversed backwards to retrieve the most recent // dives first. This allows us to download only the new dives. unsigned int current = last; unsigned int previous = end; unsigned int address = previous; unsigned int offset = remaining + SZ_MINIMUM; while (remaining) { // Calculate the size of the current dive. unsigned int size = RB_PROFILE_DISTANCE (current, previous, 1); if (size < 4 || size > remaining) { WARNING ("Unexpected profile size."); return DEVICE_STATUS_ERROR; } unsigned int nbytes = available; while (nbytes < size) { // Handle the ringbuffer wrap point. if (address == RB_PROFILE_BEGIN) address = RB_PROFILE_END; // Calculate the package size. Try with the largest possible // size first, and adjust when the end of the ringbuffer or // the end of the profile data is reached. unsigned int len = SZ_PACKET; if (RB_PROFILE_BEGIN + len > address) len = address - RB_PROFILE_BEGIN; // End of ringbuffer. if (nbytes + len > remaining) len = remaining - nbytes; // End of profile. /*if (nbytes + len > size) len = size - nbytes;*/ // End of dive (for testing only). // Move to the begin of the current package. offset -= len; address -= len; // Always read at least the minimum amount of bytes, because // reading fewer bytes is unreliable. The memory buffer is // large enough to prevent buffer overflows, and the extra // bytes are automatically ignored (due to reading backwards). unsigned int extra = 0; if (len < SZ_MINIMUM) extra = SZ_MINIMUM - len; // Read the package. rc = suunto_common2_device_read (abstract, address - extra, data + offset - extra, len + extra); if (rc != DEVICE_STATUS_SUCCESS) { WARNING ("Cannot read memory."); return rc; } // Update and emit a progress event. progress.current += len; device_event_emit (abstract, DEVICE_EVENT_PROGRESS, &progress); // Next package. nbytes += len; } // The last package of the current dive contains the previous and // next pointers (in a continuous memory area). It can also contain // a number of bytes from the next dive. remaining -= size; available = nbytes - size; unsigned char *p = data + offset + available; unsigned int prev = array_uint16_le (p + 0); unsigned int next = array_uint16_le (p + 2); if (next != previous) { WARNING ("Profiles are not continuous."); return DEVICE_STATUS_ERROR; } // Next dive. previous = current; current = prev; unsigned int fp_offset = FP_OFFSET; if (devinfo.model == 0x15) fp_offset += 6; // HelO2 if (memcmp (p + fp_offset, device->fingerprint, sizeof (device->fingerprint)) == 0) return DEVICE_STATUS_SUCCESS; if (callback && !callback (p + 4, size - 4, p + fp_offset, sizeof (device->fingerprint), userdata)) return DEVICE_STATUS_SUCCESS; } return DEVICE_STATUS_SUCCESS; }
static dc_status_t hw_ostc3_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata) { hw_ostc3_device_t *device = (hw_ostc3_device_t *) abstract; // Enable progress notifications. dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER; progress.maximum = (RB_LOGBOOK_SIZE * RB_LOGBOOK_COUNT) + SZ_MEMORY; device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); // Download the version data. unsigned char id[SZ_VERSION] = {0}; dc_status_t rc = hw_ostc3_device_version (abstract, id, sizeof (id)); if (rc != DC_STATUS_SUCCESS) { ERROR (abstract->context, "Failed to read the version."); return rc; } // Emit a device info event. dc_event_devinfo_t devinfo; devinfo.model = 0; devinfo.firmware = array_uint16_be (id + 2); devinfo.serial = array_uint16_le (id + 0); device_event_emit (abstract, DC_EVENT_DEVINFO, &devinfo); // Allocate memory. unsigned char *header = (unsigned char *) malloc (RB_LOGBOOK_SIZE * RB_LOGBOOK_COUNT); if (header == NULL) { ERROR (abstract->context, "Failed to allocate memory."); return DC_STATUS_NOMEMORY; } // Download the logbook headers. rc = hw_ostc3_transfer (device, &progress, HEADER, NULL, 0, header, RB_LOGBOOK_SIZE * RB_LOGBOOK_COUNT); if (rc != DC_STATUS_SUCCESS) { ERROR (abstract->context, "Failed to read the header."); free (header); return rc; } // Locate the most recent dive. // The device maintains an internal counter which is incremented for every // dive, and the current value at the time of the dive is stored in the // dive header. Thus the most recent dive will have the highest value. unsigned int count = 0; unsigned int latest = 0; unsigned int maximum = 0; for (unsigned int i = 0; i < RB_LOGBOOK_COUNT; ++i) { unsigned int offset = i * RB_LOGBOOK_SIZE; // Ignore uninitialized header entries. if (array_isequal (header + offset, RB_LOGBOOK_SIZE, 0xFF)) continue; // Get the internal dive number. unsigned int current = array_uint16_le (header + offset + 80); if (current > maximum) { maximum = current; latest = i; } count++; } // Calculate the total and maximum size. unsigned int ndives = 0; unsigned int size = 0; unsigned int maxsize = 0; for (unsigned int i = 0; i < count; ++i) { unsigned int idx = (latest + RB_LOGBOOK_COUNT - i) % RB_LOGBOOK_COUNT; unsigned int offset = idx * RB_LOGBOOK_SIZE; // Uninitialized header entries should no longer be present at this // stage, unless the dives are interleaved with empty entries. But // that's something we don't support at all. if (array_isequal (header + offset, RB_LOGBOOK_SIZE, 0xFF)) { WARNING (abstract->context, "Unexpected empty header found."); break; } // Get the firmware version. unsigned int firmware = array_uint16_be (header + offset + 0x30); // Calculate the profile length. unsigned int length = RB_LOGBOOK_SIZE + array_uint24_le (header + offset + 9) - 6; if (firmware >= 93) length += 3; // Check the fingerprint data. if (memcmp (header + offset + 12, device->fingerprint, sizeof (device->fingerprint)) == 0) break; if (length > maxsize) maxsize = length; size += length; ndives++; } // Update and emit a progress event. progress.maximum = (RB_LOGBOOK_SIZE * RB_LOGBOOK_COUNT) + size; device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); // Finish immediately if there are no dives available. if (ndives == 0) { free (header); return DC_STATUS_SUCCESS; } // Allocate enough memory for the largest dive. unsigned char *profile = (unsigned char *) malloc (maxsize); if (profile == NULL) { ERROR (abstract->context, "Failed to allocate memory."); free (header); return DC_STATUS_NOMEMORY; } // Download the dives. for (unsigned int i = 0; i < ndives; ++i) { unsigned int idx = (latest + RB_LOGBOOK_COUNT - i) % RB_LOGBOOK_COUNT; unsigned int offset = idx * RB_LOGBOOK_SIZE; // Get the firmware version. unsigned int firmware = array_uint16_be (header + offset + 0x30); // Calculate the profile length. unsigned int length = RB_LOGBOOK_SIZE + array_uint24_le (header + offset + 9) - 6; if (firmware >= 93) length += 3; // Download the dive. unsigned char number[1] = {idx}; rc = hw_ostc3_transfer (device, &progress, DIVE, number, sizeof (number), profile, length); if (rc != DC_STATUS_SUCCESS) { ERROR (abstract->context, "Failed to read the dive."); free (profile); free (header); return rc; } // Verify the header in the logbook and profile are identical. if (memcmp (profile, header + offset, RB_LOGBOOK_SIZE) != 0) { ERROR (abstract->context, "Unexpected profile header."); free (profile); free (header); return rc; } if (callback && !callback (profile, length, profile + 12, sizeof (device->fingerprint), userdata)) break; } free (profile); free (header); return DC_STATUS_SUCCESS; }
/* * Think twice before modifying the code for updating the ostc firmware! * It has been carefully developed and tested with assistance from * Heinrichs-Weikamp, using a special development unit. If you start * experimenting with a normal unit and accidentally screw up, you might * brick the device permanently and turn it into an expensive * paperweight. You have been warned! */ dc_status_t hw_ostc_device_fwupdate (dc_device_t *abstract, const char *filename) { dc_status_t rc = DC_STATUS_SUCCESS; hw_ostc_device_t *device = (hw_ostc_device_t *) abstract; dc_context_t *context = (abstract ? abstract->context : NULL); if (!ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; // Allocate memory for the firmware data. hw_ostc_firmware_t *firmware = (hw_ostc_firmware_t *) malloc (sizeof (hw_ostc_firmware_t)); if (firmware == NULL) { ERROR (context, "Failed to allocate memory."); return DC_STATUS_NOMEMORY; } // Read the hex file. rc = hw_ostc_firmware_readfile (firmware, context, filename); if (rc != DC_STATUS_SUCCESS) { ERROR (context, "Failed to read the firmware file."); free (firmware); return rc; } // Temporary set a relative short timeout. The command to setup the // bootloader needs to be send repeatedly, until the response packet is // received. Thus the time between each two attempts is directly controlled // by the timeout value. serial_set_timeout (device->port, 300); // Setup the bootloader. const unsigned int baudrates[] = {19200, 115200}; for (unsigned int i = 0; i < C_ARRAY_SIZE(baudrates); ++i) { // Adjust the baudrate. if (serial_configure (device->port, baudrates[i], 8, SERIAL_PARITY_NONE, 1, SERIAL_FLOWCONTROL_NONE) == -1) { ERROR (abstract->context, "Failed to set the terminal attributes."); free (firmware); return DC_STATUS_IO; } // Try to setup the bootloader. unsigned int maxretries = (i == 0 ? 1 : MAXRETRIES); rc = hw_ostc_firmware_setup (device, maxretries); if (rc == DC_STATUS_SUCCESS) break; } if (rc != DC_STATUS_SUCCESS) { ERROR (abstract->context, "Failed to setup the bootloader."); free (firmware); return rc; } // Increase the timeout again. serial_set_timeout (device->port, 1000); // Enable progress notifications. dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER; progress.maximum = C_ARRAY_SIZE(firmware->bitmap); device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); for (unsigned int i = 0; i < C_ARRAY_SIZE(firmware->bitmap); ++i) { // Skip empty blocks. if (firmware->bitmap[i] == 0) continue; // Create the packet. unsigned int address = i * SZ_BLOCK; unsigned char packet[4 + SZ_BLOCK + 1] = { (address >> 16) & 0xFF, (address >> 8) & 0xFF, (address ) & 0xFF, SZ_BLOCK }; memcpy (packet + 4, firmware->data + address, SZ_BLOCK); packet[sizeof (packet) - 1] = ~checksum_add_uint8 (packet, 4 + SZ_BLOCK, 0x00) + 1; // Send the packet. rc = hw_ostc_firmware_write (device, packet, sizeof (packet)); if (rc != DC_STATUS_SUCCESS) { ERROR (abstract->context, "Failed to send the packet."); free (firmware); return rc; } // Update and emit a progress event. progress.current = i + 1; device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); } free (firmware); return DC_STATUS_SUCCESS; }
dc_status_t dc_rbstream_read (dc_rbstream_t *rbstream, dc_event_progress_t *progress, unsigned char data[], unsigned int size) { dc_status_t rc = DC_STATUS_SUCCESS; if (rbstream == NULL) return DC_STATUS_INVALIDARGS; unsigned int address = rbstream->address; unsigned int available = rbstream->available; unsigned int skip = rbstream->skip; unsigned int nbytes = 0; unsigned int offset = size; while (nbytes < size) { if (available == 0) { // Handle the ringbuffer wrap point. if (address == rbstream->begin) address = rbstream->end; // Calculate the packet size. unsigned int len = rbstream->packetsize; if (rbstream->begin + len > address) len = address - rbstream->begin; // Move to the begin of the current packet. address -= len; // Read the packet into the cache. rc = dc_device_read (rbstream->device, address, rbstream->cache, rbstream->packetsize); if (rc != DC_STATUS_SUCCESS) return rc; available = len - skip; skip = 0; } unsigned int length = available; if (nbytes + length > size) length = size - nbytes; offset -= length; available -= length; memcpy (data + offset, rbstream->cache + available, length); // Update and emit a progress event. if (progress) { progress->current += length; device_event_emit (rbstream->device, DC_EVENT_PROGRESS, progress); } nbytes += length; } rbstream->address = address; rbstream->available = available; rbstream->skip = skip; return rc; }
static dc_status_t cressi_leonardo_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) { cressi_leonardo_device_t *device = (cressi_leonardo_device_t *) abstract; // Erase the current contents of the buffer and // pre-allocate the required amount of memory. if (!dc_buffer_clear (buffer) || !dc_buffer_resize (buffer, SZ_MEMORY)) { ERROR (abstract->context, "Insufficient buffer space available."); return DC_STATUS_NOMEMORY; } // Enable progress notifications. dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER; progress.maximum = SZ_MEMORY; device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); // Send the command header to the dive computer. const unsigned char command[] = {0x7B, 0x31, 0x32, 0x33, 0x44, 0x42, 0x41, 0x7d}; int n = serial_write (device->port, command, sizeof (command)); if (n != sizeof (command)) { ERROR (abstract->context, "Failed to send the command."); return EXITCODE (n); } // Receive the header packet. unsigned char header[7] = {0}; n = serial_read (device->port, header, sizeof (header)); if (n != sizeof (header)) { ERROR (abstract->context, "Failed to receive the answer."); return EXITCODE (n); } // Verify the header packet. const unsigned char expected[] = {0x7B, 0x21, 0x44, 0x35, 0x42, 0x33, 0x7d}; if (memcmp (header, expected, sizeof (expected)) != 0) { ERROR (abstract->context, "Unexpected answer byte."); return DC_STATUS_PROTOCOL; } unsigned char *data = dc_buffer_get_data (buffer); unsigned int nbytes = 0; while (nbytes < SZ_MEMORY) { // Set the minimum packet size. unsigned int len = 1024; // Increase the packet size if more data is immediately available. int available = serial_get_received (device->port); if (available > len) len = available; // Limit the packet size to the total size. if (nbytes + len > SZ_MEMORY) len = SZ_MEMORY - nbytes; // Read the packet. n = serial_read (device->port, data + nbytes, len); if (n != len) { ERROR (abstract->context, "Failed to receive the answer."); return EXITCODE (n); } // Update and emit a progress event. progress.current += len; device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); nbytes += len; } // Receive the trailer packet. unsigned char trailer[4] = {0}; n = serial_read (device->port, trailer, sizeof (trailer)); if (n != sizeof (trailer)) { ERROR (abstract->context, "Failed to receive the answer."); return EXITCODE (n); } // Convert to a binary checksum. unsigned char checksum[2] = {0}; array_convert_hex2bin (trailer, sizeof (trailer), checksum, sizeof (checksum)); // Verify the checksum. unsigned int csum1 = array_uint16_be (checksum); unsigned int csum2 = checksum_crc_ccitt_uint16 (data, SZ_MEMORY); if (csum1 != csum2) { ERROR (abstract->context, "Unexpected answer bytes."); return DC_STATUS_PROTOCOL; } return DC_STATUS_SUCCESS; }
dc_status_t shearwater_common_download (shearwater_common_device_t *device, dc_buffer_t *buffer, unsigned int address, unsigned int size, unsigned int compression) { dc_device_t *abstract = (dc_device_t *) device; dc_status_t rc = DC_STATUS_SUCCESS; unsigned int n = 0; unsigned char req_init[] = { 0x35, (compression ? 0x10 : 0x00), 0x34, (address >> 24) & 0xFF, (address >> 16) & 0xFF, (address >> 8) & 0xFF, (address ) & 0xFF, (size >> 16) & 0xFF, (size >> 8) & 0xFF, (size ) & 0xFF}; unsigned char req_block[] = {0x36, 0x00}; unsigned char req_quit[] = {0x37}; unsigned char response[SZ_PACKET]; // Erase the current contents of the buffer. if (!dc_buffer_clear (buffer)) { ERROR (abstract->context, "Insufficient buffer space available."); return DC_STATUS_NOMEMORY; } // Enable progress notifications. dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER; progress.maximum = 3 + size + 1; device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); // Transfer the init request. rc = shearwater_common_transfer (device, req_init, sizeof (req_init), response, 3, &n); if (rc != DC_STATUS_SUCCESS) { return rc; } // Verify the init response. if (n != 3 || response[0] != 0x75 || response[1] != 0x10 || response[2] > SZ_PACKET) { ERROR (abstract->context, "Unexpected response packet."); return DC_STATUS_PROTOCOL; } // Update and emit a progress event. progress.current += 3; device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); unsigned int done = 0; unsigned char block = 1; unsigned int nbytes = 0; while (nbytes < size && !done) { // Transfer the block request. req_block[1] = block; rc = shearwater_common_transfer (device, req_block, sizeof (req_block), response, sizeof (response), &n); if (rc != DC_STATUS_SUCCESS) { return rc; } // Verify the block header. if (n < 2 || response[0] != 0x76 || response[1] != block) { ERROR (abstract->context, "Unexpected response packet."); return DC_STATUS_PROTOCOL; } // Verify the block length. unsigned int length = n - 2; if (nbytes + length > size) { ERROR (abstract->context, "Unexpected packet size."); return DC_STATUS_PROTOCOL; } // Update and emit a progress event. progress.current += length; device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); if (compression) { if (shearwater_common_decompress_lre (response + 2, length, buffer, &done) != 0) { ERROR (abstract->context, "Decompression error (LRE phase)."); return DC_STATUS_PROTOCOL; } } else { if (!dc_buffer_append (buffer, response + 2, length)) { ERROR (abstract->context, "Insufficient buffer space available."); return DC_STATUS_PROTOCOL; } } nbytes += length; block++; } if (compression) { if (shearwater_common_decompress_xor (dc_buffer_get_data (buffer), dc_buffer_get_size (buffer)) != 0) { ERROR (abstract->context, "Decompression error (XOR phase)."); return DC_STATUS_PROTOCOL; } } // Transfer the quit request. rc = shearwater_common_transfer (device, req_quit, sizeof (req_quit), response, 2, &n); if (rc != DC_STATUS_SUCCESS) { return rc; } // Verify the quit response. if (n != 2 || response[0] != 0x77 || response[1] != 0x00) { ERROR (abstract->context, "Unexpected response packet."); return DC_STATUS_PROTOCOL; } // Update and emit a progress event. progress.current += 1; device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); return DC_STATUS_SUCCESS; }
static dc_status_t diverite_nitekq_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) { diverite_nitekq_device_t *device = (diverite_nitekq_device_t*) abstract; dc_status_t rc = DC_STATUS_SUCCESS; unsigned char packet[256] = {0}; // Erase the current contents of the buffer. if (!dc_buffer_clear (buffer) || !dc_buffer_reserve (buffer, SZ_PACKET + SZ_MEMORY)) { ERROR (abstract->context, "Insufficient buffer space available."); return DC_STATUS_NOMEMORY; } // Enable progress notifications. dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER; progress.maximum = SZ_PACKET + SZ_MEMORY; device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); // Emit a vendor event. dc_event_vendor_t vendor; vendor.data = device->version; vendor.size = sizeof (device->version); device_event_emit (abstract, DC_EVENT_VENDOR, &vendor); // Emit a device info event. dc_event_devinfo_t devinfo; devinfo.model = 0; devinfo.firmware = 0; devinfo.serial = array_uint32_be (device->version + 0x0A); device_event_emit (abstract, DC_EVENT_DEVINFO, &devinfo); // Send the upload request. It's not clear whether this request is // actually needed, but let's send it anyway. rc = diverite_nitekq_send (device, UPLOAD); if (rc != DC_STATUS_SUCCESS) { return rc; } // Receive the response packet. It's currently not used (or needed) // for anything, but we prepend it to the main data anyway, in case // we ever need it in the future. rc = diverite_nitekq_receive (device, packet, sizeof (packet)); if (rc != DC_STATUS_SUCCESS) { return rc; } dc_buffer_append (buffer, packet, sizeof (packet)); // Update and emit a progress event. progress.current += SZ_PACKET; device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); // Send the request to initiate downloading memory blocks. rc = diverite_nitekq_send (device, RESET); if (rc != DC_STATUS_SUCCESS) { return rc; } for (unsigned int i = 0; i < 128; ++i) { // Request the next memory block. rc = diverite_nitekq_send (device, BLOCK); if (rc != DC_STATUS_SUCCESS) { return rc; } // Receive the memory block. rc = diverite_nitekq_receive (device, packet, sizeof (packet)); if (rc != DC_STATUS_SUCCESS) { return rc; } dc_buffer_append (buffer, packet, sizeof (packet)); // Update and emit a progress event. progress.current += SZ_PACKET; device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); } return DC_STATUS_SUCCESS; }
static dc_status_t shearwater_petrel_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata) { shearwater_petrel_device_t *device = (shearwater_petrel_device_t *) abstract; dc_status_t rc = DC_STATUS_SUCCESS; // Allocate memory buffers for the manifests. dc_buffer_t *buffer = dc_buffer_new (MANIFEST_SIZE); dc_buffer_t *manifests = dc_buffer_new (MANIFEST_SIZE); if (buffer == NULL || manifests == NULL) { ERROR (abstract->context, "Insufficient buffer space available."); dc_buffer_free (buffer); dc_buffer_free (manifests); return DC_STATUS_NOMEMORY; } // Enable progress notifications. unsigned int current = 0, maximum = 0; dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER; device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); // Read the serial number. rc = shearwater_common_identifier (&device->base, buffer, ID_SERIAL); if (rc != DC_STATUS_SUCCESS) { ERROR (abstract->context, "Failed to read the serial number."); dc_buffer_free (buffer); dc_buffer_free (manifests); return rc; } // Convert to a number. unsigned char serial[4] = {0}; if (array_convert_hex2bin (dc_buffer_get_data (buffer), dc_buffer_get_size (buffer), serial, sizeof (serial)) != 0 ) { ERROR (abstract->context, "Failed to convert the serial number."); dc_buffer_free (buffer); dc_buffer_free (manifests); return DC_STATUS_DATAFORMAT; } // Read the firmware version. rc = shearwater_common_identifier (&device->base, buffer, ID_FIRMWARE); if (rc != DC_STATUS_SUCCESS) { ERROR (abstract->context, "Failed to read the firmware version."); dc_buffer_free (buffer); dc_buffer_free (manifests); return rc; } // Convert to a number. unsigned int firmware = str2num (dc_buffer_get_data (buffer), dc_buffer_get_size (buffer), 1); // Read the hardware type. rc = shearwater_common_identifier (&device->base, buffer, ID_HARDWARE); if (rc != DC_STATUS_SUCCESS) { ERROR (abstract->context, "Failed to read the hardware type."); dc_buffer_free (buffer); dc_buffer_free (manifests); return rc; } // Convert and map to the model number. unsigned int hardware = array_uint_be (dc_buffer_get_data (buffer), dc_buffer_get_size (buffer)); unsigned int model = 0; switch (hardware) { case 0x0808: // Petrel 2 case 0x0909: // Petrel 1 case 0x0B0B: // Petrel 1 (newer hardware) model = PETREL; break; case 0x0A0A: // Nerd 1 case 0x0E0D: // Nerd 2 model = NERD; break; case 0x0707: model = PERDIX; break; case 0x0C0D: model = PERDIXAI; break; default: WARNING (abstract->context, "Unknown hardware type %04x.", hardware); } // Emit a device info event. dc_event_devinfo_t devinfo; devinfo.model = model; devinfo.firmware = firmware; devinfo.serial = array_uint32_be (serial); device_event_emit (abstract, DC_EVENT_DEVINFO, &devinfo); while (1) { // Update the progress state. // Assume the worst case scenario of a full manifest, and adjust the // value with the actual number of dives after the manifest has been // processed. maximum += 1 + RECORD_COUNT; // Download a manifest. progress.current = NSTEPS * current; progress.maximum = NSTEPS * maximum; rc = shearwater_common_download (&device->base, buffer, MANIFEST_ADDR, MANIFEST_SIZE, 0, &progress); if (rc != DC_STATUS_SUCCESS) { ERROR (abstract->context, "Failed to download the manifest."); dc_buffer_free (buffer); dc_buffer_free (manifests); return rc; } // Cache the buffer pointer and size. unsigned char *data = dc_buffer_get_data (buffer); unsigned int size = dc_buffer_get_size (buffer); // Process the records in the manifest. unsigned int count = 0; unsigned int offset = 0; while (offset < size) { // Check for a valid dive header. unsigned int header = array_uint16_be (data + offset); if (header != 0xA5C4) break; // Check the fingerprint data. if (memcmp (data + offset + 4, device->fingerprint, sizeof (device->fingerprint)) == 0) break; offset += RECORD_SIZE; count++; } // Update the progress state. current += 1; maximum -= RECORD_COUNT - count; // Append the manifest records to the main buffer. if (!dc_buffer_append (manifests, data, count * RECORD_SIZE)) { ERROR (abstract->context, "Insufficient buffer space available."); dc_buffer_free (buffer); dc_buffer_free (manifests); return DC_STATUS_NOMEMORY; } // Stop downloading manifest if there are no more records. if (count != RECORD_COUNT) break; } // Update and emit a progress event. progress.current = NSTEPS * current; progress.maximum = NSTEPS * maximum; device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); // Cache the buffer pointer and size. unsigned char *data = dc_buffer_get_data (manifests); unsigned int size = dc_buffer_get_size (manifests); unsigned int offset = 0; while (offset < size) { // Get the address of the dive. unsigned int address = array_uint32_be (data + offset + 20); // Download the dive. progress.current = NSTEPS * current; progress.maximum = NSTEPS * maximum; rc = shearwater_common_download (&device->base, buffer, DIVE_ADDR + address, DIVE_SIZE, 1, &progress); if (rc != DC_STATUS_SUCCESS) { ERROR (abstract->context, "Failed to download the dive."); dc_buffer_free (buffer); dc_buffer_free (manifests); return rc; } // Update the progress state. current += 1; unsigned char *buf = dc_buffer_get_data (buffer); unsigned int len = dc_buffer_get_size (buffer); if (callback && !callback (buf, len, buf + 12, sizeof (device->fingerprint), userdata)) break; offset += RECORD_SIZE; } // Update and emit a progress event. progress.current = NSTEPS * current; progress.maximum = NSTEPS * maximum; device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); dc_buffer_free (manifests); dc_buffer_free (buffer); return rc; }
static dc_status_t uwatec_meridian_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) { uwatec_meridian_device_t *device = (uwatec_meridian_device_t*) abstract; dc_status_t rc = DC_STATUS_SUCCESS; // Erase the current contents of the buffer. if (!dc_buffer_clear (buffer)) { ERROR (abstract->context, "Insufficient buffer space available."); return DC_STATUS_NOMEMORY; } // Enable progress notifications. dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER; device_event_emit (&device->base, DC_EVENT_PROGRESS, &progress); // Command template. unsigned char command[9] = {0x00, (device->timestamp ) & 0xFF, (device->timestamp >> 8 ) & 0xFF, (device->timestamp >> 16) & 0xFF, (device->timestamp >> 24) & 0xFF, 0x10, 0x27, 0, 0}; // Read the model number. command[0] = 0x10; unsigned char model[1] = {0}; rc = uwatec_meridian_transfer (device, command, 1, model, sizeof (model)); if (rc != DC_STATUS_SUCCESS) return rc; // Read the serial number. command[0] = 0x14; unsigned char serial[4] = {0}; rc = uwatec_meridian_transfer (device, command, 1, serial, sizeof (serial)); if (rc != DC_STATUS_SUCCESS) return rc; // Read the device clock. command[0] = 0x1A; unsigned char devtime[4] = {0}; rc = uwatec_meridian_transfer (device, command, 1, devtime, sizeof (devtime)); if (rc != DC_STATUS_SUCCESS) return rc; // Store the clock calibration values. device->systime = dc_datetime_now (); device->devtime = array_uint32_le (devtime); // Update and emit a progress event. progress.current += 9; device_event_emit (&device->base, DC_EVENT_PROGRESS, &progress); // Emit a clock event. dc_event_clock_t clock; clock.systime = device->systime; clock.devtime = device->devtime; device_event_emit (&device->base, DC_EVENT_CLOCK, &clock); // Emit a device info event. dc_event_devinfo_t devinfo; devinfo.model = model[0]; devinfo.firmware = 0; devinfo.serial = array_uint32_le (serial); device_event_emit (&device->base, DC_EVENT_DEVINFO, &devinfo); // Data Length. command[0] = 0xC6; unsigned char answer[4] = {0}; rc = uwatec_meridian_transfer (device, command, sizeof (command), answer, sizeof (answer)); if (rc != DC_STATUS_SUCCESS) return rc; unsigned int length = array_uint32_le (answer); // Update and emit a progress event. progress.maximum = 4 + 9 + (length ? length + 4 : 0); progress.current += 4; device_event_emit (&device->base, DC_EVENT_PROGRESS, &progress); if (length == 0) return DC_STATUS_SUCCESS; // Allocate the required amount of memory. if (!dc_buffer_resize (buffer, length)) { ERROR (abstract->context, "Insufficient buffer space available."); return DC_STATUS_NOMEMORY; } unsigned char *data = dc_buffer_get_data (buffer); // Data. command[0] = 0xC4; rc = uwatec_meridian_transfer (device, command, sizeof (command), answer, sizeof (answer)); if (rc != DC_STATUS_SUCCESS) return rc; unsigned int total = array_uint32_le (answer); // Update and emit a progress event. progress.current += 4; device_event_emit (&device->base, DC_EVENT_PROGRESS, &progress); if (total != length + 4) { ERROR (abstract->context, "Received an unexpected size."); return DC_STATUS_PROTOCOL; } unsigned int nbytes = 0; while (nbytes < length) { // Read the header. unsigned char header[5]; int n = serial_read (device->port, header, sizeof (header)); if (n != sizeof (header)) { ERROR (abstract->context, "Failed to receive the header."); return EXITCODE (n); } // Get the packet size. unsigned int packetsize = array_uint32_le (header); if (packetsize < 1 || nbytes + packetsize - 1 > length) { WARNING (abstract->context, "Unexpected header."); return DC_STATUS_PROTOCOL; } // Read the packet data. n = serial_read (device->port, data + nbytes, packetsize - 1); if (n != packetsize - 1) { ERROR (abstract->context, "Failed to receive the packet."); return EXITCODE (n); } // Read the checksum. unsigned char csum = 0x00; n = serial_read (device->port, &csum, sizeof (csum)); if (n != sizeof (csum)) { ERROR (abstract->context, "Failed to receive the checksum."); return EXITCODE (n); } // Verify the checksum. unsigned char ccsum = 0x00; ccsum = checksum_xor_uint8 (header, sizeof (header), ccsum); ccsum = checksum_xor_uint8 (data + nbytes, packetsize - 1, ccsum); if (csum != ccsum) { ERROR (abstract->context, "Unexpected answer checksum."); return DC_STATUS_PROTOCOL; } // Update and emit a progress event. progress.current += packetsize - 1; device_event_emit (&device->base, DC_EVENT_PROGRESS, &progress); nbytes += packetsize - 1; } return DC_STATUS_SUCCESS; }
static dc_status_t hw_ostc_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) { hw_ostc_device_t *device = (hw_ostc_device_t*) abstract; // Erase the current contents of the buffer. if (!dc_buffer_clear (buffer)) { ERROR (abstract->context, "Insufficient buffer space available."); return DC_STATUS_NOMEMORY; } // Enable progress notifications. dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER; progress.maximum = SZ_HEADER + SZ_FW_NEW; device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); // Send the command. unsigned char command[1] = {'a'}; int rc = serial_write (device->port, command, sizeof (command)); if (rc != sizeof (command)) { ERROR (abstract->context, "Failed to send the command."); return EXITCODE (rc); } // Read the header. unsigned char header[SZ_HEADER] = {0}; int n = serial_read (device->port, header, sizeof (header)); if (n != sizeof (header)) { ERROR (abstract->context, "Failed to receive the header."); return EXITCODE (n); } // Verify the header. unsigned char preamble[] = {0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0x55}; if (memcmp (header, preamble, sizeof (preamble)) != 0) { ERROR (abstract->context, "Unexpected answer header."); return DC_STATUS_DATAFORMAT; } // Get the firmware version. unsigned int firmware = array_uint16_be (header + 264); // Get the amount of profile data. unsigned int size = sizeof (header); if (firmware > FW_190) size += SZ_FW_NEW; else size += SZ_FW_190; // Update and emit a progress event. progress.current = sizeof (header); progress.maximum = size; device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); // Allocate the required amount of memory. if (!dc_buffer_resize (buffer, size)) { ERROR (abstract->context, "Insufficient buffer space available."); return DC_STATUS_NOMEMORY; } unsigned char *data = dc_buffer_get_data (buffer); // Copy the header to the output buffer. memcpy (data, header, sizeof (header)); unsigned int nbytes = sizeof (header); while (nbytes < size) { // Set the minimum packet size. unsigned int len = 1024; // Increase the packet size if more data is immediately available. int available = serial_get_received (device->port); if (available > len) len = available; // Limit the packet size to the total size. if (nbytes + len > size) len = size - nbytes; // Read the packet. int n = serial_read (device->port, data + nbytes, len); if (n != len) { ERROR (abstract->context, "Failed to receive the answer."); return EXITCODE (n); } // Update and emit a progress event. progress.current += len; device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); nbytes += len; } return DC_STATUS_SUCCESS; }