subrascasse

Sub Rascasse: an iPhone dive log for Subsurface cloud storage, with a pure-Swift git client (GPL-2.0)
git clone https://git.schurger.org/subrascasse.git
Log | Files | Refs | README | LICENSE

pelagic_i330r.c (20627B)


      1 /*
      2  * libdivecomputer
      3  *
      4  * Copyright (C) 2023 Janice McLaughlin
      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
     23 #include <stdlib.h> // malloc, free
     24 #include <assert.h>
     25 
     26 #include <libdivecomputer/ble.h>
     27 
     28 #include "pelagic_i330r.h"
     29 #include "oceanic_common.h"
     30 
     31 #include "context-private.h"
     32 #include "device-private.h"
     33 #include "ringbuffer.h"
     34 #include "rbstream.h"
     35 #include "checksum.h"
     36 #include "array.h"
     37 
     38 #define UNDEFINED 0
     39 
     40 #define STARTBYTE  0xCD
     41 
     42 #define FLAG_NONE    0x00
     43 #define FLAG_REQUEST 0x40
     44 #define FLAG_DATA    0x80
     45 #define FLAG_LAST    0xC0
     46 
     47 #define CMD_ACCESS_REQUEST       0xFA
     48 #define CMD_ACCESS_CODE          0xFB
     49 #define CMD_AUTHENTICATION       0x97
     50 #define CMD_WAKEUP_RDONLY        0x21
     51 #define CMD_WAKEUP_RDWR          0x22
     52 #define CMD_READ_HW_CAL          0x27
     53 #define CMD_READ_A2D             0x25
     54 #define CMD_READ_DEVICE_REC      0x31
     55 #define CMD_READ_GEN_SET         0x29
     56 #define CMD_READ_EXFLASHMAP      0x2F
     57 #define CMD_READ_FLASH           0x0D
     58 
     59 #define RSP_READY 1
     60 #define RSP_DONE  2
     61 #define RSP_ACCESSCODE_INVALID 13
     62 
     63 #define MAXPACKET 255
     64 
     65 #define MAXPASSCODE 6
     66 
     67 #define MAXRETRIES 3
     68 
     69 typedef struct pelagic_i330r_device_t {
     70 	oceanic_common_device_t base;
     71 	dc_iostream_t *iostream;
     72 	unsigned char accesscode[16];
     73 	unsigned char id[16];
     74 	unsigned char hwcal[256];
     75 	unsigned char flashmap[256];
     76 	unsigned int model;
     77 } pelagic_i330r_device_t;
     78 
     79 static dc_status_t pelagic_i330r_device_read (dc_device_t *abstract, unsigned int address, unsigned char data[], unsigned int size);
     80 static dc_status_t pelagic_i330r_device_devinfo (dc_device_t *abstract, dc_event_progress_t *progress);
     81 static dc_status_t pelagic_i330r_device_pointers (dc_device_t *abstract, dc_event_progress_t *progress, unsigned int *rb_logbook_begin, unsigned int *rb_logbook_end, unsigned int *rb_profile_begin, unsigned int *rb_profile_end);
     82 
     83 static const oceanic_common_device_vtable_t pelagic_i330r_device_vtable = {
     84 	{
     85 		sizeof(pelagic_i330r_device_t),
     86 		DC_FAMILY_PELAGIC_I330R,
     87 		oceanic_common_device_set_fingerprint, /* set_fingerprint */
     88 		pelagic_i330r_device_read, /* read */
     89 		NULL, /* write */
     90 		oceanic_common_device_dump, /* dump */
     91 		oceanic_common_device_foreach, /* foreach */
     92 		NULL, /* timesync */
     93 		NULL /* close */
     94 	},
     95 	pelagic_i330r_device_devinfo,
     96 	pelagic_i330r_device_pointers,
     97 	oceanic_common_device_logbook,
     98 	oceanic_common_device_profile,
     99 };
    100 
    101 static const oceanic_common_layout_t pelagic_i330r = {
    102 	0x00400000, /* memsize */
    103 	0, /* highmem */
    104 	UNDEFINED, /* cf_devinfo */
    105 	UNDEFINED, /* cf_pointers */
    106 	0x00102000, /* rb_logbook_begin */
    107 	0x00106000, /* rb_logbook_end */
    108 	64, /* rb_logbook_entry_size */
    109 	0, /* rb_logbook_direction */
    110 	0x0010A000, /* rb_profile_begin */
    111 	0x00400000, /* rb_profile_end */
    112 	1, /* pt_mode_global */
    113 	4, /* pt_mode_logbook */
    114 	UNDEFINED, /* pt_mode_serial */
    115 };
    116 
    117 static const oceanic_common_layout_t pelagic_dsx = {
    118 	0x02000000, /* memsize */
    119 	0, /* highmem */
    120 	UNDEFINED, /* cf_devinfo */
    121 	UNDEFINED, /* cf_pointers */
    122 	0x00800000, /* rb_logbook_begin */
    123 	0x00880000, /* rb_logbook_end */
    124 	512, /* rb_logbook_entry_size */
    125 	1, /* rb_logbook_direction */
    126 	0x01000000, /* rb_profile_begin */
    127 	0x02000000, /* rb_profile_end */
    128 	1, /* pt_mode_global */
    129 	4, /* pt_mode_logbook */
    130 	UNDEFINED /* pt_mode_serial */
    131 };
    132 
    133 static unsigned char
    134 checksum (const unsigned char data[], unsigned int size)
    135 {
    136 	unsigned int csum = 0;
    137 	for (unsigned int i = 0; i < size; i++) {
    138 		unsigned int a = csum ^ data[i];
    139 		unsigned int b = (a >> 7) ^ ((a >> 4) ^ a);
    140 		csum = ((b << 4) & 0xFF) ^ ((b << 1) & 0xFF);
    141 	}
    142 	return csum & 0xFF;
    143 }
    144 
    145 static dc_status_t
    146 pelagic_i330r_send (pelagic_i330r_device_t *device, unsigned char cmd, unsigned char flag, const unsigned char data[], unsigned int size)
    147 {
    148 	dc_status_t status = DC_STATUS_SUCCESS;
    149 	dc_device_t *abstract = (dc_device_t *) device;
    150 
    151 	if (size > MAXPACKET) {
    152 		ERROR (abstract->context, "Packet payload is too large (%u).", size);
    153 		return DC_STATUS_INVALIDARGS;
    154 	}
    155 
    156 	unsigned char packet[MAXPACKET + 5] = {
    157 		STARTBYTE,
    158 		flag,
    159 		cmd,
    160 		0,
    161 		size
    162 	};
    163 	if (size) {
    164 		memcpy(packet + 5, data, size);
    165 	}
    166 	packet[3] = checksum (packet, size + 5);
    167 
    168 	// Send the data packet.
    169 	status = dc_iostream_write (device->iostream, packet, size + 5, NULL);
    170 	if (status != DC_STATUS_SUCCESS) {
    171 		ERROR (abstract->context, "Failed to send the command.");
    172 		return status;
    173 	}
    174 
    175 	return DC_STATUS_SUCCESS;
    176 }
    177 
    178 static dc_status_t
    179 pelagic_i330r_recv (pelagic_i330r_device_t *device, unsigned char cmd, unsigned char data[], unsigned int size, unsigned int *errorcode)
    180 {
    181 	dc_status_t status = DC_STATUS_SUCCESS;
    182 	dc_device_t *abstract = (dc_device_t *) device;
    183 	unsigned char packet[MAXPACKET + 5] = {0};
    184 	unsigned int errcode = 0;
    185 
    186 	unsigned int nbytes = 0;
    187 	while (1) {
    188 		// Read the data packet.
    189 		size_t transferred = 0;
    190 		status = dc_iostream_read (device->iostream, packet, sizeof(packet), &transferred);
    191 		if (status != DC_STATUS_SUCCESS) {
    192 			ERROR (abstract->context, "Failed to receive the data packet.");
    193 			return status;
    194 		}
    195 
    196 		// Verify the minimum packet size.
    197 		if (transferred < 5) {
    198 			ERROR (abstract->context, "Invalid packet length (" DC_PRINTF_SIZE ").", transferred);
    199 			return DC_STATUS_PROTOCOL;
    200 		}
    201 
    202 		// Verify the start byte.
    203 		if (packet[0] != STARTBYTE) {
    204 			ERROR (abstract->context, "Unexpected packet start byte (%02x).", packet[0]);
    205 			return DC_STATUS_PROTOCOL;
    206 		}
    207 
    208 		// Verify the command byte.
    209 		if (packet[2] != cmd) {
    210 			ERROR (abstract->context, "Unexpected packet command byte (%02x).", packet[2]);
    211 			return DC_STATUS_PROTOCOL;
    212 		}
    213 
    214 		// Verify the length byte.
    215 		unsigned int length = packet[4];
    216 		if (length + 5 > transferred) {
    217 			ERROR (abstract->context, "Invalid packet length (%u).", length);
    218 			return DC_STATUS_PROTOCOL;
    219 		}
    220 
    221 		// Verify the checksum.
    222 		unsigned char crc = packet[3]; packet[3] = 0;
    223 		unsigned char ccrc = checksum (packet, length + 5);
    224 		if (crc != ccrc) {
    225 			ERROR (abstract->context, "Unexpected packet checksum (%02x %02x).", crc, ccrc);
    226 			return DC_STATUS_PROTOCOL;
    227 		}
    228 
    229 		// Check the flag byte for the last packet.
    230 		unsigned char flag = packet[1];
    231 		if ((flag & FLAG_LAST) == FLAG_LAST) {
    232 			// The last packet (typically 2 bytes) does not get appended!
    233 			if (length) {
    234 				errcode = packet[5];
    235 			}
    236 			break;
    237 		}
    238 
    239 		// Append the payload data to the output buffer. If the output
    240 		// buffer is too small, the error is not reported immediately
    241 		// but delayed until all packets have been received.
    242 		if (nbytes < size) {
    243 			unsigned int n = length;
    244 			if (nbytes + n > size) {
    245 				n = size - nbytes;
    246 			}
    247 			memcpy (data + nbytes, packet + 5, n);
    248 		}
    249 		nbytes += length;
    250 	}
    251 
    252 	// Verify the expected number of bytes.
    253 	if (nbytes != size) {
    254 		ERROR (abstract->context, "Unexpected number of bytes received (%u %u).", nbytes, size);
    255 		return DC_STATUS_PROTOCOL;
    256 	}
    257 
    258 	if (errorcode) {
    259 		*errorcode = errcode;
    260 	}
    261 
    262 	return DC_STATUS_SUCCESS;
    263 }
    264 
    265 static dc_status_t
    266 pelagic_i330r_transfer (pelagic_i330r_device_t *device, unsigned char cmd, unsigned char flag, const unsigned char data[], unsigned int size, unsigned char answer[], unsigned int asize, unsigned int response)
    267 {
    268 	dc_status_t status = DC_STATUS_SUCCESS;
    269 	dc_device_t *abstract = (dc_device_t *) device;
    270 	unsigned int errorcode = 0;
    271 
    272 	status = pelagic_i330r_send (device, cmd, flag, data, size);
    273 	if (status != DC_STATUS_SUCCESS)
    274 		return status;
    275 
    276 	status = pelagic_i330r_recv (device, cmd, answer, asize, &errorcode);
    277 	if (status != DC_STATUS_SUCCESS)
    278 		return status;
    279 
    280 	if (errorcode != response) {
    281 		ERROR (abstract->context, "Unexpected response code (%u)", errorcode);
    282 		if (errorcode == RSP_ACCESSCODE_INVALID)
    283 			return DC_STATUS_NOACCESS;
    284 		return DC_STATUS_PROTOCOL;
    285 	}
    286 
    287 	return status;
    288 }
    289 
    290 static dc_status_t
    291 pelagic_i330r_init_accesscode (pelagic_i330r_device_t *device)
    292 {
    293 	dc_status_t status = DC_STATUS_SUCCESS;
    294 
    295 	const unsigned char zero[9] = {0};
    296 	status = pelagic_i330r_transfer (device, CMD_ACCESS_REQUEST, FLAG_REQUEST, zero, sizeof(zero), NULL, 0, RSP_READY);
    297 	if (status != DC_STATUS_SUCCESS)
    298 		return status;
    299 
    300 	status = pelagic_i330r_transfer (device, CMD_ACCESS_REQUEST, FLAG_DATA, device->accesscode, sizeof(device->accesscode), NULL, 0, RSP_DONE);
    301 	if (status != DC_STATUS_SUCCESS)
    302 		return status;
    303 
    304 	return status;
    305 }
    306 
    307 static dc_status_t
    308 pelagic_i330r_init_passcode (pelagic_i330r_device_t *device, const char *pincode)
    309 {
    310 	dc_status_t status = DC_STATUS_SUCCESS;
    311 	dc_device_t *abstract = (dc_device_t *) device;
    312 	unsigned char passcode[MAXPASSCODE] = {0};
    313 
    314 	// Check the maximum length.
    315 	size_t len = pincode ? strlen (pincode) : 0;
    316 	if (len > sizeof(passcode)) {
    317 		ERROR (abstract->context, "Invalid pincode length (" DC_PRINTF_SIZE ").", len);
    318 		return DC_STATUS_INVALIDARGS;
    319 	}
    320 
    321 	// Convert to binary number.
    322 	unsigned int offset = sizeof(passcode) - len;
    323 	for (unsigned int i = 0; i < len; i++) {
    324 		unsigned char c = pincode[i];
    325 		if (c < '0' || c > '9') {
    326 			ERROR (abstract->context, "Invalid pincode character (%c).", c);
    327 			return DC_STATUS_INVALIDARGS;
    328 		}
    329 		passcode[offset + i] = c - '0';
    330 	}
    331 
    332 	const unsigned char zero[9] = {0};
    333 	status = pelagic_i330r_transfer (device, CMD_ACCESS_CODE, FLAG_REQUEST, zero, sizeof(zero), NULL, 0, RSP_READY);
    334 	if (status != DC_STATUS_SUCCESS)
    335 		return status;
    336 
    337 	status = pelagic_i330r_transfer (device, CMD_ACCESS_CODE, FLAG_DATA, passcode, sizeof(passcode), device->accesscode, sizeof(device->accesscode), RSP_DONE);
    338 	if (status != DC_STATUS_SUCCESS)
    339 		return status;
    340 
    341 	HEXDUMP (abstract->context, DC_LOGLEVEL_DEBUG, "Access code", device->accesscode, sizeof(device->accesscode));
    342 
    343 	return status;
    344 }
    345 
    346 static dc_status_t
    347 pelagic_i330r_init_handshake (pelagic_i330r_device_t *device, unsigned int readwrite)
    348 {
    349 	dc_status_t status = DC_STATUS_SUCCESS;
    350 	dc_device_t *abstract = (dc_device_t *) device;
    351 
    352 	const unsigned char cmd = readwrite ? CMD_WAKEUP_RDWR : CMD_WAKEUP_RDONLY;
    353 
    354 	const unsigned char args[9] = {0, 0, 0, 0, 0x0C, 0, 0, 0, 0};
    355 	status = pelagic_i330r_transfer (device, cmd, FLAG_REQUEST, args, sizeof(args), device->id, sizeof(device->id), RSP_DONE);
    356 	if (status != DC_STATUS_SUCCESS)
    357 		return status;
    358 
    359 	HEXDUMP (abstract->context, DC_LOGLEVEL_DEBUG, "ID", device->id, sizeof(device->id));
    360 
    361 	device->model = array_uint16_be (device->id + 12);
    362 
    363 	return status;
    364 }
    365 
    366 static dc_status_t
    367 pelagic_i330r_init_auth (pelagic_i330r_device_t *device)
    368 {
    369 	dc_status_t status = DC_STATUS_SUCCESS;
    370 
    371 	const unsigned char args[2][9] = {
    372 		{0xFF, 0xFF, 0xFF, 0xFF}, // DSX
    373 		{0x37, 0x30, 0x31, 0x55}, // I330R
    374 	};
    375 	unsigned int args_idx = device->model == DSX ? 0 : 1;
    376 	status = pelagic_i330r_transfer (device, CMD_AUTHENTICATION, FLAG_REQUEST, args[args_idx], sizeof(args[args_idx]), NULL, 0, RSP_READY);
    377 	if (status != DC_STATUS_SUCCESS)
    378 		return status;
    379 
    380 	return status;
    381 }
    382 
    383 static dc_status_t
    384 pelagic_i330r_init (pelagic_i330r_device_t *device)
    385 {
    386 	dc_status_t status = DC_STATUS_SUCCESS;
    387 	dc_device_t *abstract = (dc_device_t *) device;
    388 
    389 	// Get the bluetooth access code.
    390 	status = dc_iostream_ioctl (device->iostream, DC_IOCTL_BLE_GET_ACCESSCODE, device->accesscode, sizeof(device->accesscode));
    391 	if (status != DC_STATUS_SUCCESS && status != DC_STATUS_UNSUPPORTED) {
    392 		ERROR (abstract->context, "Failed to get the access code.");
    393 		return status;
    394 	}
    395 
    396 	unsigned int nretries = 0;
    397 	while (1) {
    398 		// Try to request access.
    399 		status = pelagic_i330r_init_accesscode (device);
    400 		if (status != DC_STATUS_SUCCESS && status != DC_STATUS_NOACCESS) {
    401 			ERROR (abstract->context, "Failed to request access.");
    402 			return status;
    403 		}
    404 
    405 		if (array_isequal (device->accesscode, sizeof(device->accesscode), 0)) {
    406 			WARNING (abstract->context, "Access denied, no access code.");
    407 		} else {
    408 			if (status == DC_STATUS_SUCCESS)
    409 				break;
    410 			WARNING (abstract->context, "Access denied, invalid access code.");
    411 		}
    412 
    413 		// Abort if the maximum number of retries is reached.
    414 		if (nretries++ >= MAXRETRIES) {
    415 			ERROR (abstract->context, "Maximum number of retries reached.");
    416 			return DC_STATUS_NOACCESS;
    417 		}
    418 
    419 		// Get the bluetooth PIN code.
    420 		char pincode[6 + 1] = {0};
    421 		status = dc_iostream_ioctl (device->iostream, DC_IOCTL_BLE_GET_PINCODE, pincode, sizeof(pincode));
    422 		if (status != DC_STATUS_SUCCESS) {
    423 			ERROR (abstract->context, "Failed to get the PIN code.");
    424 			return status;
    425 		}
    426 
    427 		// Force a null terminated string.
    428 		pincode[sizeof(pincode) - 1] = 0;
    429 
    430 		// Request the access code.
    431 		status = pelagic_i330r_init_passcode (device, pincode);
    432 		if (status != DC_STATUS_SUCCESS) {
    433 			ERROR (abstract->context, "Failed to request the access code.");
    434 			return status;
    435 		}
    436 
    437 		// Store the bluetooth access code.
    438 		status = dc_iostream_ioctl (device->iostream, DC_IOCTL_BLE_SET_ACCESSCODE, device->accesscode, sizeof(device->accesscode));
    439 		if (status != DC_STATUS_SUCCESS && status != DC_STATUS_UNSUPPORTED) {
    440 			ERROR (abstract->context, "Failed to store the access code.");
    441 			return status;
    442 		}
    443 	}
    444 
    445 	// Send the wakeup command.
    446 	status = pelagic_i330r_init_handshake (device, 1);
    447 	if (status != DC_STATUS_SUCCESS) {
    448 		ERROR (abstract->context, "Failed to send the wakeup command.");
    449 		return status;
    450 	}
    451 
    452 	// Send the authentication code.
    453 	status = pelagic_i330r_init_auth (device);
    454 	if (status != DC_STATUS_SUCCESS) {
    455 		ERROR (abstract->context, "Failed to send the authentication code.");
    456 		return status;
    457 	}
    458 
    459 	return status;
    460 }
    461 
    462 static dc_status_t
    463 pelagic_i330r_download (pelagic_i330r_device_t *device, unsigned char cmd, const unsigned char data[], unsigned int size, unsigned char answer[], unsigned int asize)
    464 {
    465 	dc_status_t status = DC_STATUS_SUCCESS;
    466 	dc_device_t *abstract = (dc_device_t *) device;
    467 
    468 	status = pelagic_i330r_transfer (device, cmd, FLAG_REQUEST, data, size, answer, asize, RSP_DONE);
    469 	if (status != DC_STATUS_SUCCESS)
    470 		return status;
    471 
    472 	// Verify the checksum
    473 	unsigned short crc = array_uint16_be (answer + asize - 2);
    474 	unsigned short ccrc = checksum_crc16_ccitt (answer, asize - 2, 0xffff, 0x0000);
    475 	if (crc != ccrc) {
    476 		ERROR (abstract->context, "Unexpected data checksum (%04x %04x).", crc, ccrc);
    477 		return DC_STATUS_PROTOCOL;
    478 	}
    479 
    480 	return status;
    481 }
    482 
    483 dc_status_t
    484 pelagic_i330r_device_open (dc_device_t **out, dc_context_t *context, dc_iostream_t *iostream, unsigned int model)
    485 {
    486 	dc_status_t status = DC_STATUS_SUCCESS;
    487 	pelagic_i330r_device_t *device = NULL;
    488 
    489 	if (out == NULL)
    490 		return DC_STATUS_INVALIDARGS;
    491 
    492 	// Allocate memory.
    493 	device = (pelagic_i330r_device_t *) dc_device_allocate (context, &pelagic_i330r_device_vtable.base);
    494 	if (device == NULL) {
    495 		ERROR (context, "Failed to allocate memory.");
    496 		return DC_STATUS_NOMEMORY;
    497 	}
    498 
    499 	// Initialize the base class.
    500 	oceanic_common_device_init (&device->base);
    501 
    502 	// Override the base class values.
    503 	device->base.multipage = 256;
    504 
    505 	// Set the default values.
    506 	device->iostream = iostream;
    507 	memset (device->accesscode, 0, sizeof(device->accesscode));
    508 	memset (device->id, 0, sizeof(device->id));
    509 	memset (device->hwcal, 0, sizeof(device->hwcal));
    510 	memset (device->flashmap, 0, sizeof(device->flashmap));
    511 	device->model = 0;
    512 
    513 	// Set the timeout for receiving data (3000 ms).
    514 	status = dc_iostream_set_timeout (device->iostream, 3000);
    515 	if (status != DC_STATUS_SUCCESS) {
    516 		ERROR (context, "Failed to set the timeout.");
    517 		goto error_free;
    518 	}
    519 
    520 	// Perform the bluetooth authentication.
    521 	status = pelagic_i330r_init (device);
    522 	if (status != DC_STATUS_SUCCESS) {
    523 		ERROR (context, "Failed to perform the bluetooth authentication.");
    524 		goto error_free;
    525 	}
    526 
    527 	// Download the calibration data.
    528 	const unsigned char args[9] = {0, 0, 0, 0, 0, 0x01, 0, 0, 0};
    529 	status = pelagic_i330r_download (device, CMD_READ_HW_CAL, args, sizeof(args), device->hwcal, sizeof(device->hwcal));
    530 	if (status != DC_STATUS_SUCCESS) {
    531 		ERROR (context, "Failed to download the calibration data.");
    532 		goto error_free;
    533 	}
    534 
    535 	HEXDUMP (context, DC_LOGLEVEL_DEBUG, "Hwcal", device->hwcal, sizeof(device->hwcal));
    536 
    537 	// Download the flash map.
    538 	const unsigned char zero[9] = {0};
    539 	status = pelagic_i330r_download (device, CMD_READ_EXFLASHMAP, zero, sizeof(zero), device->flashmap, sizeof(device->flashmap));
    540 	if (status != DC_STATUS_SUCCESS) {
    541 		ERROR (context, "Failed to download the flash map.");
    542 		goto error_free;
    543 	}
    544 
    545 	HEXDUMP (context, DC_LOGLEVEL_DEBUG, "Flashmap", device->flashmap, sizeof(device->flashmap));
    546 
    547 	// Detect the memory layout.
    548 	if (device->model == DSX) {
    549 		device->base.layout = &pelagic_dsx;
    550 	} else {
    551 		device->base.layout = &pelagic_i330r;
    552 	}
    553 
    554 	*out = (dc_device_t *) device;
    555 
    556 	return DC_STATUS_SUCCESS;
    557 
    558 error_free:
    559 	dc_device_deallocate ((dc_device_t *) device);
    560 	return status;
    561 }
    562 
    563 static dc_status_t
    564 pelagic_i330r_device_read (dc_device_t *abstract, unsigned int address, unsigned char data[], unsigned int size)
    565 {
    566 	dc_status_t status = DC_STATUS_SUCCESS;
    567 	pelagic_i330r_device_t *device = (pelagic_i330r_device_t*) abstract;
    568 
    569 	unsigned char command[9] = {0};
    570 	array_uint32_le_set(command + 0, address);
    571 	array_uint32_le_set(command + 4, size);
    572 
    573 	status = pelagic_i330r_transfer (device, CMD_READ_FLASH, FLAG_NONE, command, sizeof(command), data, size, RSP_DONE);
    574 	if (status != DC_STATUS_SUCCESS) {
    575 		return status;
    576 	}
    577 
    578 	return status;
    579 }
    580 
    581 static dc_status_t
    582 pelagic_i330r_device_devinfo (dc_device_t *abstract, dc_event_progress_t *progress)
    583 {
    584 	pelagic_i330r_device_t *device = (pelagic_i330r_device_t *) abstract;
    585 
    586 	assert (device != NULL);
    587 
    588 	// Emit a device info event.
    589 	dc_event_devinfo_t devinfo;
    590 	devinfo.model = device->model;
    591 	devinfo.firmware = 0;
    592 	devinfo.serial =
    593 		bcd2dec (device->hwcal[12]) +
    594 		bcd2dec (device->hwcal[13]) * 100 +
    595 		bcd2dec (device->hwcal[14]) * 10000;
    596 	device_event_emit (abstract, DC_EVENT_DEVINFO, &devinfo);
    597 
    598 	return DC_STATUS_SUCCESS;
    599 }
    600 
    601 static dc_status_t
    602 pelagic_i330r_device_pointers (dc_device_t *abstract, dc_event_progress_t *progress, unsigned int *rb_logbook_begin, unsigned int *rb_logbook_end, unsigned int *rb_profile_begin, unsigned int *rb_profile_end)
    603 {
    604 	pelagic_i330r_device_t *device = (pelagic_i330r_device_t *) abstract;
    605 
    606 	assert (device != NULL);
    607 	assert (device->base.layout != NULL);
    608 	assert (rb_logbook_begin != NULL && rb_logbook_end != NULL);
    609 	assert (rb_profile_begin != NULL && rb_profile_end != NULL);
    610 
    611 	const oceanic_common_layout_t *layout = device->base.layout;
    612 
    613 	// Get the logbook pointers.
    614 	unsigned int rb_logbook_min   = array_uint32_le (device->flashmap + 0x50);
    615 	unsigned int rb_logbook_max   = array_uint32_le (device->flashmap + 0x54);
    616 	unsigned int rb_logbook_first = array_uint32_le (device->flashmap + 0x58);
    617 	unsigned int rb_logbook_last  = array_uint32_le (device->flashmap + 0x5C);
    618 	if (rb_logbook_min != 0 && rb_logbook_max != 0) {
    619 		rb_logbook_max += 1;
    620 	}
    621 
    622 	// Get the profile pointers.
    623 	unsigned int rb_profile_min   = array_uint32_le (device->flashmap + 0x70);
    624 	unsigned int rb_profile_max   = array_uint32_le (device->flashmap + 0x74);
    625 	unsigned int rb_profile_first = array_uint32_le (device->flashmap + 0x78);
    626 	unsigned int rb_profile_last  = array_uint32_le (device->flashmap + 0x7C);
    627 	if (rb_profile_min != 0 && rb_profile_max != 0) {
    628 		rb_profile_max += 1;
    629 	}
    630 
    631 	// Check the logbook ringbuffer area.
    632 	if (rb_logbook_min != layout->rb_logbook_begin ||
    633 		rb_logbook_max != layout->rb_logbook_end) {
    634 		ERROR (abstract->context, "Unexpected logbook ringbuffer area (%08x %08x)",
    635 			rb_logbook_min, rb_logbook_max);
    636 		return DC_STATUS_DATAFORMAT;
    637 	}
    638 
    639 	// Check the profile ringbuffer area.
    640 	if (rb_profile_min != layout->rb_profile_begin ||
    641 		rb_profile_max != layout->rb_profile_end) {
    642 		ERROR (abstract->context, "Unexpected profile ringbuffer area (%08x %08x)",
    643 			rb_profile_min, rb_profile_max);
    644 		return DC_STATUS_DATAFORMAT;
    645 	}
    646 
    647 	// Get the begin/end pointers.
    648 	if (device->model == DSX) {
    649 		*rb_logbook_begin = rb_logbook_first;
    650 		*rb_logbook_end   = rb_logbook_last;
    651 	} else {
    652 		*rb_logbook_begin = rb_logbook_min;
    653 		*rb_logbook_end   = rb_logbook_last + 1;
    654 	}
    655 	*rb_profile_begin = rb_profile_first;
    656 	*rb_profile_end   = rb_profile_last;
    657 
    658 	return DC_STATUS_SUCCESS;
    659 }