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

oceanic_vtpro.c (23192B)


      1 /*
      2  * libdivecomputer
      3  *
      4  * Copyright (C) 2008 Jef Driesen
      5  *
      6  * This library is free software; you can redistribute it and/or
      7  * modify it under the terms of the GNU Lesser General Public
      8  * License as published by the Free Software Foundation; either
      9  * version 2.1 of the License, or (at your option) any later version.
     10  *
     11  * This library is distributed in the hope that it will be useful,
     12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     14  * Lesser General Public License for more details.
     15  *
     16  * You should have received a copy of the GNU Lesser General Public
     17  * License along with this library; if not, write to the Free Software
     18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
     19  * MA 02110-1301 USA
     20  */
     21 
     22 #include <string.h> // memcpy
     23 #include <stdlib.h> // malloc, free
     24 #include <assert.h>
     25 
     26 #include "oceanic_vtpro.h"
     27 #include "oceanic_common.h"
     28 #include "context-private.h"
     29 #include "device-private.h"
     30 #include "ringbuffer.h"
     31 #include "checksum.h"
     32 #include "array.h"
     33 
     34 #define ISINSTANCE(device) dc_device_isinstance((device), &oceanic_vtpro_device_vtable.base)
     35 
     36 #define MAXRETRIES 2
     37 #define MULTIPAGE  4
     38 
     39 #define ACK 0x5A
     40 #define NAK 0xA5
     41 #define END 0x51
     42 
     43 typedef enum oceanic_vtpro_protocol_t {
     44 	MOD,
     45 	INTR,
     46 } oceanic_vtpro_protocol_t;
     47 
     48 typedef struct oceanic_vtpro_device_t {
     49 	oceanic_common_device_t base;
     50 	dc_iostream_t *iostream;
     51 	oceanic_vtpro_protocol_t protocol;
     52 } oceanic_vtpro_device_t;
     53 
     54 static dc_status_t oceanic_vtpro_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);
     55 static dc_status_t oceanic_vtpro_device_logbook (dc_device_t *abstract, dc_event_progress_t *progress, dc_buffer_t *logbook, unsigned int begin, unsigned int end);
     56 static dc_status_t oceanic_vtpro_device_read (dc_device_t *abstract, unsigned int address, unsigned char data[], unsigned int size);
     57 static dc_status_t oceanic_vtpro_device_close (dc_device_t *abstract);
     58 
     59 static const oceanic_common_device_vtable_t oceanic_vtpro_device_vtable = {
     60 	{
     61 		sizeof(oceanic_vtpro_device_t),
     62 		DC_FAMILY_OCEANIC_VTPRO,
     63 		oceanic_common_device_set_fingerprint, /* set_fingerprint */
     64 		oceanic_vtpro_device_read, /* read */
     65 		NULL, /* write */
     66 		oceanic_common_device_dump, /* dump */
     67 		oceanic_common_device_foreach, /* foreach */
     68 		NULL, /* timesync */
     69 		oceanic_vtpro_device_close /* close */
     70 	},
     71 	oceanic_common_device_devinfo,
     72 	oceanic_vtpro_device_pointers,
     73 	oceanic_vtpro_device_logbook,
     74 	oceanic_common_device_profile,
     75 };
     76 
     77 static const oceanic_common_layout_t oceanic_vtpro_layout = {
     78 	0x8000, /* memsize */
     79 	0, /* highmem */
     80 	0x0000, /* cf_devinfo */
     81 	0x0040, /* cf_pointers */
     82 	0x0240, /* rb_logbook_begin */
     83 	0x0440, /* rb_logbook_end */
     84 	8, /* rb_logbook_entry_size */
     85 	1, /* rb_logbook_direction */
     86 	0x0440, /* rb_profile_begin */
     87 	0x8000, /* rb_profile_end */
     88 	0, /* pt_mode_global */
     89 	0, /* pt_mode_logbook */
     90 	0, /* pt_mode_serial */
     91 };
     92 
     93 static const oceanic_common_layout_t oceanic_wisdom_layout = {
     94 	0x8000, /* memsize */
     95 	0, /* highmem */
     96 	0x0000, /* cf_devinfo */
     97 	0x0040, /* cf_pointers */
     98 	0x03D0, /* rb_logbook_begin */
     99 	0x05D0, /* rb_logbook_end */
    100 	8, /* rb_logbook_entry_size */
    101 	1, /* rb_logbook_direction */
    102 	0x05D0, /* rb_profile_begin */
    103 	0x8000, /* rb_profile_end */
    104 	0, /* pt_mode_global */
    105 	0, /* pt_mode_logbook */
    106 	0, /* pt_mode_serial */
    107 };
    108 
    109 static const oceanic_common_layout_t aeris_500ai_layout = {
    110 	0x20000, /* memsize */
    111 	0, /* highmem */
    112 	0x0000, /* cf_devinfo */
    113 	0x0110, /* cf_pointers */
    114 	0x0200, /* rb_logbook_begin */
    115 	0x0200, /* rb_logbook_end */
    116 	8, /* rb_logbook_entry_size */
    117 	1, /* rb_logbook_direction */
    118 	0x00200, /* rb_profile_begin */
    119 	0x20000, /* rb_profile_end */
    120 	0, /* pt_mode_global */
    121 	1, /* pt_mode_logbook */
    122 	2, /* pt_mode_serial */
    123 };
    124 
    125 static const oceanic_common_version_t versions[] = {
    126 	{"VERSAPRO \0\0 256K", 0, VERSAPRO,      &oceanic_vtpro_layout},
    127 	{"ATMOSTWO \0\0 256K", 0, ATMOS2,        &oceanic_vtpro_layout},
    128 	{"PROPLUS2 \0\0 256K", 0, PROPLUS2,      &oceanic_vtpro_layout},
    129 	{"ATMOSAIR \0\0 256K", 0, ATMOSAI,       &oceanic_vtpro_layout},
    130 	{"VTPRO  r\0\0  256K", 0, VTPRO,         &oceanic_vtpro_layout},
    131 	{"ELITE  r\0\0  256K", 0, ELITE,         &oceanic_vtpro_layout},
    132 
    133 	{"WISDOM r\0\0  256K", 0, WISDOM,        &oceanic_wisdom_layout},
    134 };
    135 
    136 static dc_status_t
    137 oceanic_vtpro_send (oceanic_vtpro_device_t *device, const unsigned char command[], unsigned int csize)
    138 {
    139 	dc_status_t status = DC_STATUS_SUCCESS;
    140 	dc_device_t *abstract = (dc_device_t *) device;
    141 
    142 	if (device_is_cancelled (abstract))
    143 		return DC_STATUS_CANCELLED;
    144 
    145 	// Send the command to the dive computer.
    146 	status = dc_iostream_write (device->iostream, command, csize, NULL);
    147 	if (status != DC_STATUS_SUCCESS) {
    148 		ERROR (abstract->context, "Failed to send the command.");
    149 		return status;
    150 	}
    151 
    152 	// Receive the response (ACK/NAK) of the dive computer.
    153 	unsigned char response = NAK;
    154 	status = dc_iostream_read (device->iostream, &response, 1, NULL);
    155 	if (status != DC_STATUS_SUCCESS) {
    156 		ERROR (abstract->context, "Failed to receive the answer.");
    157 		return status;
    158 	}
    159 
    160 	// Verify the response of the dive computer.
    161 	if (response != ACK) {
    162 		ERROR (abstract->context, "Unexpected answer start byte(s).");
    163 		return DC_STATUS_PROTOCOL;
    164 	}
    165 
    166 	return DC_STATUS_SUCCESS;
    167 }
    168 
    169 
    170 static dc_status_t
    171 oceanic_vtpro_transfer (oceanic_vtpro_device_t *device, const unsigned char command[], unsigned int csize, unsigned char answer[], unsigned int asize)
    172 {
    173 	dc_status_t status = DC_STATUS_SUCCESS;
    174 	dc_device_t *abstract = (dc_device_t *) device;
    175 
    176 	// Send the command to the device. If the device responds with an
    177 	// ACK byte, the command was received successfully and the answer
    178 	// (if any) follows after the ACK byte. If the device responds with
    179 	// a NAK byte, we try to resend the command a number of times before
    180 	// returning an error.
    181 
    182 	unsigned int nretries = 0;
    183 	dc_status_t rc = DC_STATUS_SUCCESS;
    184 	while ((rc = oceanic_vtpro_send (device, command, csize)) != DC_STATUS_SUCCESS) {
    185 		if (rc != DC_STATUS_TIMEOUT && rc != DC_STATUS_PROTOCOL)
    186 			return rc;
    187 
    188 		// Abort if the maximum number of retries is reached.
    189 		if (nretries++ >= MAXRETRIES)
    190 			return rc;
    191 	}
    192 
    193 	if (asize) {
    194 		// Receive the answer of the dive computer.
    195 		status = dc_iostream_read (device->iostream, answer, asize, NULL);
    196 		if (status != DC_STATUS_SUCCESS) {
    197 			ERROR (abstract->context, "Failed to receive the answer.");
    198 			return status;
    199 		}
    200 	}
    201 
    202 	return DC_STATUS_SUCCESS;
    203 }
    204 
    205 
    206 static dc_status_t
    207 oceanic_vtpro_init (oceanic_vtpro_device_t *device)
    208 {
    209 	dc_status_t status = DC_STATUS_SUCCESS;
    210 	dc_device_t *abstract = (dc_device_t *) device;
    211 
    212 	// Send the command to the dive computer.
    213 	unsigned char command[2][2] = {
    214 		{0xAA, 0x00},
    215 		{0x20, 0x00}};
    216 	status = dc_iostream_write (device->iostream, command[device->protocol], sizeof (command[device->protocol]), NULL);
    217 	if (status != DC_STATUS_SUCCESS) {
    218 		ERROR (abstract->context, "Failed to send the command.");
    219 		return status;
    220 	}
    221 
    222 	// Receive the answer of the dive computer.
    223 	unsigned char answer[13] = {0};
    224 	status = dc_iostream_read (device->iostream, answer, sizeof (answer), NULL);
    225 	if (status != DC_STATUS_SUCCESS) {
    226 		ERROR (abstract->context, "Failed to receive the answer.");
    227 		return status;
    228 	}
    229 
    230 	// Verify the answer.
    231 	const unsigned char response[2][13] = {
    232 		{0x4D, 0x4F, 0x44, 0x2D, 0x2D, 0x4F, 0x4B, 0x5F, 0x56, 0x32, 0x2E, 0x30, 0x30},
    233 		{0x49, 0x4E, 0x54, 0x52, 0x2D, 0x4F, 0x4B, 0x5F, 0x56, 0x31, 0x2E, 0x31, 0x31}};
    234 	if (memcmp (answer, response[device->protocol], sizeof (response[device->protocol])) != 0) {
    235 		ERROR (abstract->context, "Unexpected answer byte(s).");
    236 		return DC_STATUS_PROTOCOL;
    237 	}
    238 
    239 	return DC_STATUS_SUCCESS;
    240 }
    241 
    242 
    243 static dc_status_t
    244 oceanic_vtpro_quit (oceanic_vtpro_device_t *device)
    245 {
    246 	dc_device_t *abstract = (dc_device_t *) device;
    247 
    248 	// Send the command to the dive computer.
    249 	unsigned char answer[1] = {0};
    250 	unsigned char command[4] = {0x6A, 0x05, 0xA5, 0x00};
    251 	dc_status_t rc = oceanic_vtpro_transfer (device, command, sizeof (command), answer, sizeof (answer));
    252 	if (rc != DC_STATUS_SUCCESS)
    253 		return rc;
    254 
    255 	// Verify the last byte of the answer.
    256 	if (answer[0] != END) {
    257 		ERROR (abstract->context, "Unexpected answer byte(s).");
    258 		return DC_STATUS_PROTOCOL;
    259 	}
    260 
    261 	return DC_STATUS_SUCCESS;
    262 }
    263 
    264 
    265 static dc_status_t
    266 oceanic_vtpro_calibrate (oceanic_vtpro_device_t *device)
    267 {
    268 	dc_device_t *abstract = (dc_device_t *) device;
    269 
    270 	// Send the command to the dive computer.
    271 	// The timeout is temporary increased, because the
    272 	// device needs approximately 6 seconds to respond.
    273 	unsigned char answer[2] = {0};
    274 	unsigned char command[2] = {0x18, 0x00};
    275 	dc_status_t rc = dc_iostream_set_timeout (device->iostream, 9000);
    276 	if (rc != DC_STATUS_SUCCESS)
    277 		return rc;
    278 	rc = oceanic_vtpro_transfer (device, command, sizeof (command), answer, sizeof (answer));
    279 	if (rc != DC_STATUS_SUCCESS)
    280 		return rc;
    281 	rc = dc_iostream_set_timeout (device->iostream, 3000);
    282 	if (rc != DC_STATUS_SUCCESS)
    283 		return rc;
    284 
    285 	// Verify the last byte of the answer.
    286 	if (answer[1] != 0x00) {
    287 		ERROR (abstract->context, "Unexpected answer byte(s).");
    288 		return DC_STATUS_PROTOCOL;
    289 	}
    290 
    291 	return DC_STATUS_SUCCESS;
    292 }
    293 
    294 static dc_status_t
    295 oceanic_aeris500ai_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)
    296 {
    297 	dc_status_t status = DC_STATUS_SUCCESS;
    298 	oceanic_vtpro_device_t *device = (oceanic_vtpro_device_t *) abstract;
    299 
    300 	assert (device != NULL);
    301 	assert (device->base.layout != NULL);
    302 	assert (rb_logbook_begin != NULL && rb_logbook_end != NULL);
    303 	assert (rb_profile_begin != NULL && rb_profile_end != NULL);
    304 
    305 	const oceanic_common_layout_t *layout = device->base.layout;
    306 
    307 	// Read the pointer data.
    308 	unsigned char pointers[PAGESIZE] = {0};
    309 	status = oceanic_vtpro_device_read (abstract, layout->cf_pointers, pointers, sizeof (pointers));
    310 	if (status != DC_STATUS_SUCCESS) {
    311 		ERROR (abstract->context, "Failed to read the memory page.");
    312 		return status;
    313 	}
    314 
    315 	// Update and emit a progress event.
    316 	if (progress) {
    317 		progress->current += PAGESIZE;
    318 		progress->maximum += PAGESIZE;
    319 		device_event_emit (abstract, DC_EVENT_PROGRESS, progress);
    320 	}
    321 
    322 	// Get the pointers.
    323 	unsigned int rb_logbook_first = pointers[0x02];
    324 	unsigned int rb_logbook_last  = pointers[0x03];
    325 	unsigned int rb_profile_first = array_uint16_le (pointers + 4) * PAGESIZE;
    326 	unsigned int rb_profile_last  = array_uint16_le (pointers + 6) * PAGESIZE;
    327 
    328 	*rb_logbook_begin = rb_logbook_first;
    329 	*rb_logbook_end   = rb_logbook_last;
    330 	*rb_profile_begin = rb_profile_first;
    331 	*rb_profile_end   = rb_profile_last;
    332 
    333 	return status;
    334 }
    335 
    336 static dc_status_t
    337 oceanic_aeris500ai_device_logbook (dc_device_t *abstract, dc_event_progress_t *progress, dc_buffer_t *logbook, unsigned int begin, unsigned int end)
    338 {
    339 	dc_status_t rc = DC_STATUS_SUCCESS;
    340 	oceanic_vtpro_device_t *device = (oceanic_vtpro_device_t *) abstract;
    341 
    342 	assert (device != NULL);
    343 	assert (device->base.layout != NULL);
    344 	assert (device->base.layout->rb_logbook_entry_size == PAGESIZE / 2);
    345 	assert (device->base.layout->rb_logbook_begin == device->base.layout->rb_logbook_end);
    346 	assert (progress != NULL);
    347 
    348 	// Erase the buffer.
    349 	if (!dc_buffer_clear (logbook))
    350 		return DC_STATUS_NOMEMORY;
    351 
    352 	// Get the number of dives.
    353 	unsigned int ndives = end - begin + 1;
    354 
    355 	// Update and emit a progress event.
    356 	progress->maximum += ndives * PAGESIZE / 2;
    357 	device_event_emit (abstract, DC_EVENT_PROGRESS, progress);
    358 
    359 	// Allocate memory for the logbook entries.
    360 	if (!dc_buffer_reserve (logbook, ndives * PAGESIZE / 2))
    361 		return DC_STATUS_NOMEMORY;
    362 
    363 	// Send the logbook index command.
    364 	unsigned char command[] = {0x52,
    365 			begin & 0xFF,
    366 			end   & 0xFF,
    367 			0x00};
    368 	rc = oceanic_vtpro_transfer (device, command, sizeof (command), NULL, 0);
    369 	if (rc != DC_STATUS_SUCCESS) {
    370 		ERROR (abstract->context, "Failed to send the logbook index command.");
    371 		return rc;
    372 	}
    373 
    374 	// Read the logbook index.
    375 	for (unsigned int i = 0; i < ndives; ++i) {
    376 		// Receive the answer of the dive computer.
    377 		unsigned char answer[PAGESIZE / 2 + 1] = {0};
    378 		rc = dc_iostream_read (device->iostream, answer, sizeof(answer), NULL);
    379 		if (rc != DC_STATUS_SUCCESS) {
    380 			ERROR (abstract->context, "Failed to receive the answer.");
    381 			return rc;
    382 		}
    383 
    384 		// Verify the checksum of the answer.
    385 		unsigned char crc = answer[PAGESIZE / 2];
    386 		unsigned char ccrc = checksum_add_uint4 (answer, PAGESIZE / 2, 0x00);
    387 		if (crc != ccrc) {
    388 			ERROR (abstract->context, "Unexpected answer checksum.");
    389 			return DC_STATUS_PROTOCOL;
    390 		}
    391 
    392 		// Update and emit a progress event.
    393 		progress->current += PAGESIZE / 2;
    394 		device_event_emit (abstract, DC_EVENT_PROGRESS, progress);
    395 
    396 		// Ignore uninitialized entries.
    397 		if (array_isequal (answer, PAGESIZE / 2, 0xFF)) {
    398 			WARNING (abstract->context, "Uninitialized logbook entries detected!");
    399 			continue;
    400 		}
    401 
    402 		// Compare the fingerprint to identify previously downloaded entries.
    403 		if (memcmp (answer, device->base.fingerprint, PAGESIZE / 2) == 0) {
    404 			dc_buffer_clear (logbook);
    405 		} else {
    406 			dc_buffer_append (logbook, answer, PAGESIZE / 2);
    407 		}
    408 	}
    409 
    410 	return DC_STATUS_SUCCESS;
    411 }
    412 
    413 static dc_status_t
    414 oceanic_vtpro_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)
    415 {
    416 	oceanic_vtpro_device_t *device = (oceanic_vtpro_device_t *) abstract;
    417 
    418 	if (device->base.model == AERIS500AI) {
    419 		return oceanic_aeris500ai_device_pointers (abstract, progress, rb_logbook_begin, rb_logbook_end, rb_profile_begin, rb_profile_end);
    420 	} else {
    421 		return oceanic_common_device_pointers (abstract, progress, rb_logbook_begin, rb_logbook_end, rb_profile_begin, rb_profile_end);
    422 	}
    423 }
    424 
    425 static dc_status_t
    426 oceanic_vtpro_device_logbook (dc_device_t *abstract, dc_event_progress_t *progress, dc_buffer_t *logbook, unsigned int begin, unsigned int end)
    427 {
    428 	oceanic_vtpro_device_t *device = (oceanic_vtpro_device_t *) abstract;
    429 
    430 	if (device->base.model == AERIS500AI) {
    431 		return oceanic_aeris500ai_device_logbook (abstract, progress, logbook, begin, end);
    432 	} else {
    433 		return oceanic_common_device_logbook (abstract, progress, logbook, begin, end);
    434 	}
    435 }
    436 
    437 dc_status_t
    438 oceanic_vtpro_device_open (dc_device_t **out, dc_context_t *context, dc_iostream_t *iostream, unsigned int model)
    439 {
    440 	dc_status_t status = DC_STATUS_SUCCESS;
    441 	oceanic_vtpro_device_t *device = NULL;
    442 
    443 	if (out == NULL)
    444 		return DC_STATUS_INVALIDARGS;
    445 
    446 	// Allocate memory.
    447 	device = (oceanic_vtpro_device_t *) dc_device_allocate (context, &oceanic_vtpro_device_vtable.base);
    448 	if (device == NULL) {
    449 		ERROR (context, "Failed to allocate memory.");
    450 		return DC_STATUS_NOMEMORY;
    451 	}
    452 
    453 	// Initialize the base class.
    454 	oceanic_common_device_init (&device->base);
    455 
    456 	// Override the base class values.
    457 	device->base.multipage = MULTIPAGE;
    458 
    459 	// Set the default values.
    460 	device->iostream = iostream;
    461 	if (model == AERIS500AI) {
    462 		device->protocol = INTR;
    463 	} else {
    464 		device->protocol = MOD;
    465 	}
    466 
    467 	// Set the serial communication protocol (9600 8N1).
    468 	status = dc_iostream_configure (device->iostream, 9600, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
    469 	if (status != DC_STATUS_SUCCESS) {
    470 		ERROR (context, "Failed to set the terminal attributes.");
    471 		goto error_free;
    472 	}
    473 
    474 	// Set the timeout for receiving data (3000 ms).
    475 	status = dc_iostream_set_timeout (device->iostream, 3000);
    476 	if (status != DC_STATUS_SUCCESS) {
    477 		ERROR (context, "Failed to set the timeout.");
    478 		goto error_free;
    479 	}
    480 
    481 	// Set the DTR line.
    482 	status = dc_iostream_set_dtr (device->iostream, 1);
    483 	if (status != DC_STATUS_SUCCESS) {
    484 		ERROR (context, "Failed to set the DTR line.");
    485 		goto error_free;
    486 	}
    487 
    488 	// Clear the RTS line to reset the PIC inside the data cable as it
    489 	// may not have have been previously cleared. This ensures that the
    490 	// PIC will always start in a known state once RTS is set. Starting
    491 	// in a known default state is very important as the PIC won't
    492 	// respond to init commands unless it is in a default state.
    493 	status = dc_iostream_set_rts (device->iostream, 0);
    494 	if (status != DC_STATUS_SUCCESS) {
    495 		ERROR (context, "Failed to clear the RTS line.");
    496 		goto error_free;
    497 	}
    498 
    499 	// Hold RTS clear for a bit to allow PIC to reset.
    500 	dc_iostream_sleep (device->iostream, 100);
    501 
    502 	// Set the RTS line.
    503 	status = dc_iostream_set_rts (device->iostream, 1);
    504 	if (status != DC_STATUS_SUCCESS) {
    505 		ERROR (context, "Failed to set the RTS line.");
    506 		goto error_free;
    507 	}
    508 
    509 	// Give the interface 100 ms to settle and draw power up.
    510 	dc_iostream_sleep (device->iostream, device->protocol == MOD ? 100 : 1000);
    511 
    512 	// Make sure everything is in a sane state.
    513 	dc_iostream_purge (device->iostream, DC_DIRECTION_ALL);
    514 
    515 	// Initialize the data cable (MOD mode).
    516 	status = oceanic_vtpro_init (device);
    517 	if (status != DC_STATUS_SUCCESS) {
    518 		goto error_free;
    519 	}
    520 
    521 	// Switch the device from surface mode into download mode. Before sending
    522 	// this command, the device needs to be in PC mode (manually activated by
    523 	// the user), or already in download mode.
    524 	status = oceanic_vtpro_device_version ((dc_device_t *) device, device->base.version, sizeof (device->base.version));
    525 	if (status != DC_STATUS_SUCCESS) {
    526 		goto error_free;
    527 	}
    528 
    529 	HEXDUMP (context, DC_LOGLEVEL_DEBUG, "Version", device->base.version, sizeof (device->base.version));
    530 
    531 	// Calibrate the device. Although calibration is optional, it's highly
    532 	// recommended because it reduces the transfer time considerably, even
    533 	// when processing the command itself is quite slow.
    534 	status = oceanic_vtpro_calibrate (device);
    535 	if (status != DC_STATUS_SUCCESS) {
    536 		goto error_free;
    537 	}
    538 
    539 	// Detect the memory layout.
    540 	if (model == AERIS500AI) {
    541 		device->base.layout = &aeris_500ai_layout;
    542 		device->base.model = AERIS500AI;
    543 	} else {
    544 		const oceanic_common_version_t * version = OCEANIC_COMMON_MATCH(device->base.version, versions, &device->base.firmware);
    545 		if (version == NULL) {
    546 			WARNING (context, "Unsupported device detected!");
    547 			device->base.layout = &oceanic_vtpro_layout;
    548 			device->base.model = 0;
    549 		} else {
    550 			device->base.layout = version->layout;
    551 			device->base.model = version->model;
    552 		}
    553 	}
    554 
    555 	*out = (dc_device_t*) device;
    556 
    557 	return DC_STATUS_SUCCESS;
    558 
    559 error_free:
    560 	dc_device_deallocate ((dc_device_t *) device);
    561 	return status;
    562 }
    563 
    564 
    565 static dc_status_t
    566 oceanic_vtpro_device_close (dc_device_t *abstract)
    567 {
    568 	dc_status_t status = DC_STATUS_SUCCESS;
    569 	oceanic_vtpro_device_t *device = (oceanic_vtpro_device_t*) abstract;
    570 	dc_status_t rc = DC_STATUS_SUCCESS;
    571 
    572 	// Switch the device back to surface mode.
    573 	rc = oceanic_vtpro_quit (device);
    574 	if (rc != DC_STATUS_SUCCESS) {
    575 		dc_status_set_error(&status, rc);
    576 	}
    577 
    578 	return status;
    579 }
    580 
    581 
    582 dc_status_t
    583 oceanic_vtpro_device_keepalive (dc_device_t *abstract)
    584 {
    585 	oceanic_vtpro_device_t *device = (oceanic_vtpro_device_t*) abstract;
    586 
    587 	if (!ISINSTANCE (abstract))
    588 		return DC_STATUS_INVALIDARGS;
    589 
    590 	// Send the command to the dive computer.
    591 	unsigned char answer[1] = {0};
    592 	unsigned char command[4] = {0x6A, 0x08, 0x00, 0x00};
    593 	dc_status_t rc = oceanic_vtpro_transfer (device, command, sizeof (command), answer, sizeof (answer));
    594 	if (rc != DC_STATUS_SUCCESS)
    595 		return rc;
    596 
    597 	// Verify the last byte of the answer.
    598 	if (answer[0] != END) {
    599 		ERROR (abstract->context, "Unexpected answer byte(s).");
    600 		return DC_STATUS_PROTOCOL;
    601 	}
    602 
    603 	return DC_STATUS_SUCCESS;
    604 }
    605 
    606 
    607 dc_status_t
    608 oceanic_vtpro_device_version (dc_device_t *abstract, unsigned char data[], unsigned int size)
    609 {
    610 	oceanic_vtpro_device_t *device = (oceanic_vtpro_device_t*) abstract;
    611 
    612 	if (!ISINSTANCE (abstract))
    613 		return DC_STATUS_INVALIDARGS;
    614 
    615 	if (size < PAGESIZE)
    616 		return DC_STATUS_INVALIDARGS;
    617 
    618 	// Switch the device into download mode. The response is ignored here,
    619 	// since it is identical (except for the missing trailing byte) to the
    620 	// response of the first part of the other command in this function.
    621 
    622 	unsigned char cmd[2] = {0x88, 0x00};
    623 	unsigned char ans[PAGESIZE / 2 + 1] = {0};
    624 	dc_status_t rc = oceanic_vtpro_transfer (device, cmd, sizeof (cmd), ans, sizeof (ans));
    625 	if (rc != DC_STATUS_SUCCESS)
    626 		return rc;
    627 
    628 	// Verify the checksum of the answer.
    629 	unsigned char crc = ans[PAGESIZE / 2];
    630 	unsigned char ccrc = checksum_add_uint4 (ans, PAGESIZE / 2, 0x00);
    631 	if (crc != ccrc) {
    632 		ERROR (abstract->context, "Unexpected answer checksum.");
    633 		return DC_STATUS_PROTOCOL;
    634 	}
    635 
    636 	if (device->protocol == MOD) {
    637 		// Obtain the device identification string. This string is
    638 		// split over two packets, but we join both parts again.
    639 		for (unsigned int i = 0; i < 2; ++i) {
    640 			unsigned char command[4] = {0x72, 0x03, i * 0x10, 0x00};
    641 			unsigned char answer[PAGESIZE / 2 + 2] = {0};
    642 			rc = oceanic_vtpro_transfer (device, command, sizeof (command), answer, sizeof (answer));
    643 			if (rc != DC_STATUS_SUCCESS)
    644 				return rc;
    645 
    646 			// Verify the checksum of the answer.
    647 			crc = answer[PAGESIZE / 2];
    648 			ccrc = checksum_add_uint4 (answer, PAGESIZE / 2, 0x00);
    649 			if (crc != ccrc) {
    650 				ERROR (abstract->context, "Unexpected answer checksum.");
    651 				return DC_STATUS_PROTOCOL;
    652 			}
    653 
    654 			// Verify the last byte of the answer.
    655 			if (answer[PAGESIZE / 2 + 1] != END) {
    656 				ERROR (abstract->context, "Unexpected answer byte.");
    657 				return DC_STATUS_PROTOCOL;
    658 			}
    659 
    660 			// Append the answer to the output buffer.
    661 			memcpy (data + i * PAGESIZE / 2, answer, PAGESIZE / 2);
    662 		}
    663 	} else {
    664 		// Return an empty device identification string.
    665 		memset (data, 0x00, PAGESIZE);
    666 	}
    667 
    668 	return DC_STATUS_SUCCESS;
    669 }
    670 
    671 
    672 static dc_status_t
    673 oceanic_vtpro_device_read (dc_device_t *abstract, unsigned int address, unsigned char data[], unsigned int size)
    674 {
    675 	oceanic_vtpro_device_t *device = (oceanic_vtpro_device_t*) abstract;
    676 
    677 	if ((address % PAGESIZE != 0) ||
    678 		(size    % PAGESIZE != 0))
    679 		return DC_STATUS_INVALIDARGS;
    680 
    681 	unsigned int nbytes = 0;
    682 	while (nbytes < size) {
    683 		// Calculate the number of packages.
    684 		unsigned int npackets = (size - nbytes) / PAGESIZE;
    685 		if (npackets > MULTIPAGE)
    686 			npackets = MULTIPAGE;
    687 
    688 		// Read the package.
    689 		unsigned int first =  address / PAGESIZE;
    690 		unsigned int last  = first + npackets - 1;
    691 		unsigned char answer[(PAGESIZE + 1) * MULTIPAGE] = {0};
    692 		unsigned char command[6] = {0x34,
    693 				(first >> 8) & 0xFF, // high
    694 				(first     ) & 0xFF, // low
    695 				(last >> 8) & 0xFF, // high
    696 				(last     ) & 0xFF, // low
    697 				0x00};
    698 		dc_status_t rc = oceanic_vtpro_transfer (device, command, sizeof (command), answer, (PAGESIZE + 1) * npackets);
    699 		if (rc != DC_STATUS_SUCCESS)
    700 			return rc;
    701 
    702 		unsigned int offset = 0;
    703 		for (unsigned int i = 0; i < npackets; ++i) {
    704 			// Verify the checksum of the answer.
    705 			unsigned char crc = answer[offset + PAGESIZE];
    706 			unsigned char ccrc = checksum_add_uint8 (answer + offset, PAGESIZE, 0x00);
    707 			if (crc != ccrc) {
    708 				ERROR (abstract->context, "Unexpected answer checksum.");
    709 				return DC_STATUS_PROTOCOL;
    710 			}
    711 
    712 			memcpy (data, answer + offset, PAGESIZE);
    713 
    714 			offset += PAGESIZE + 1;
    715 			nbytes += PAGESIZE;
    716 			address += PAGESIZE;
    717 			data += PAGESIZE;
    718 		}
    719 	}
    720 
    721 	return DC_STATUS_SUCCESS;
    722 }