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

cressi_edy.c (17512B)


      1 /*
      2  * libdivecomputer
      3  *
      4  * Copyright (C) 2009 Jef Driesen
      5  *
      6  * This library is free software; you can redistribute it and/or
      7  * modify it under the terms of the GNU Lesser General Public
      8  * License as published by the Free Software Foundation; either
      9  * version 2.1 of the License, or (at your option) any later version.
     10  *
     11  * This library is distributed in the hope that it will be useful,
     12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     14  * Lesser General Public License for more details.
     15  *
     16  * You should have received a copy of the GNU Lesser General Public
     17  * License along with this library; if not, write to the Free Software
     18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
     19  * MA 02110-1301 USA
     20  */
     21 
     22 #include <string.h> // memcpy, memcmp
     23 #include <stdlib.h> // malloc, free
     24 #include <assert.h> // assert
     25 
     26 #include "cressi_edy.h"
     27 #include "context-private.h"
     28 #include "device-private.h"
     29 #include "checksum.h"
     30 #include "array.h"
     31 #include "ringbuffer.h"
     32 #include "rbstream.h"
     33 
     34 #define ISINSTANCE(device) dc_device_isinstance((device), &cressi_edy_device_vtable)
     35 
     36 #define MAXRETRIES        4
     37 
     38 #define SZ_PAGE           32
     39 
     40 #define SZ_HEADER 32
     41 
     42 #define ARCHIMEDE 0x01
     43 #define IQ700 0x05
     44 #define EDY   0x08
     45 
     46 typedef struct cressi_edy_layout_t {
     47 	unsigned int memsize;
     48 	unsigned int rb_profile_begin;
     49 	unsigned int rb_profile_end;
     50 	unsigned int rb_logbook_offset;
     51 	unsigned int rb_logbook_size;
     52 	unsigned int rb_logbook_begin;
     53 	unsigned int rb_logbook_end;
     54 	unsigned int config;
     55 } cressi_edy_layout_t;
     56 
     57 typedef struct cressi_edy_device_t {
     58 	dc_device_t base;
     59 	dc_iostream_t *iostream;
     60 	const cressi_edy_layout_t *layout;
     61 	unsigned char fingerprint[SZ_PAGE / 2];
     62 	unsigned int model;
     63 	unsigned int packetsize;
     64 } cressi_edy_device_t;
     65 
     66 static dc_status_t cressi_edy_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size);
     67 static dc_status_t cressi_edy_device_read (dc_device_t *abstract, unsigned int address, unsigned char data[], unsigned int size);
     68 static dc_status_t cressi_edy_device_dump (dc_device_t *abstract, dc_buffer_t *buffer);
     69 static dc_status_t cressi_edy_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata);
     70 static dc_status_t cressi_edy_device_close (dc_device_t *abstract);
     71 
     72 static const dc_device_vtable_t cressi_edy_device_vtable = {
     73 	sizeof(cressi_edy_device_t),
     74 	DC_FAMILY_CRESSI_EDY,
     75 	cressi_edy_device_set_fingerprint, /* set_fingerprint */
     76 	cressi_edy_device_read, /* read */
     77 	NULL, /* write */
     78 	cressi_edy_device_dump, /* dump */
     79 	cressi_edy_device_foreach, /* foreach */
     80 	NULL, /* timesync */
     81 	cressi_edy_device_close /* close */
     82 };
     83 
     84 static const cressi_edy_layout_t cressi_edy_layout = {
     85 	0x8000, /* memsize */
     86 	0x3FE0, /* rb_profile_begin */
     87 	0x7F80, /* rb_profile_end */
     88 	0x7F80, /* rb_logbook_offset */
     89 	2,  /* rb_logbook_size */
     90 	0,  /* rb_logbook_begin */
     91 	60, /* rb_logbook_end */
     92 	0x7C, /* config */
     93 };
     94 
     95 static const cressi_edy_layout_t tusa_iq700_layout = {
     96 	0x2000, /* memsize */
     97 	0x0000, /* rb_profile_begin */
     98 	0x1F60, /* rb_profile_end */
     99 	0x1F80, /* rb_logbook_offset */
    100 	1,  /* rb_logbook_size */
    101 	0,  /* rb_logbook_begin */
    102 	60, /* rb_logbook_end */
    103 	0x3C, /* config */
    104 };
    105 
    106 static dc_status_t
    107 cressi_edy_packet (cressi_edy_device_t *device, const unsigned char command[], unsigned int csize, unsigned char answer[], unsigned int asize, int trailer)
    108 {
    109 	dc_status_t status = DC_STATUS_SUCCESS;
    110 	dc_device_t *abstract = (dc_device_t *) device;
    111 
    112 	if (device_is_cancelled (abstract))
    113 		return DC_STATUS_CANCELLED;
    114 
    115 	for (unsigned int i = 0; i < csize; ++i) {
    116 		// Send the command to the device.
    117 		status = dc_iostream_write (device->iostream, command + i, 1, NULL);
    118 		if (status != DC_STATUS_SUCCESS) {
    119 			ERROR (abstract->context, "Failed to send the command.");
    120 			return status;
    121 		}
    122 
    123 		// Receive the echo.
    124 		unsigned char echo = 0;
    125 		status = dc_iostream_read (device->iostream, &echo, 1, NULL);
    126 		if (status != DC_STATUS_SUCCESS) {
    127 			ERROR (abstract->context, "Failed to receive the echo.");
    128 			return status;
    129 		}
    130 
    131 		// Verify the echo.
    132 		if (command[i] != echo) {
    133 			ERROR (abstract->context, "Unexpected echo.");
    134 			return DC_STATUS_PROTOCOL;
    135 		}
    136 	}
    137 
    138 	if (asize) {
    139 		// Receive the answer of the device.
    140 		status = dc_iostream_read (device->iostream, answer, asize, NULL);
    141 		if (status != DC_STATUS_SUCCESS) {
    142 			ERROR (abstract->context, "Failed to receive the answer.");
    143 			return status;
    144 		}
    145 	}
    146 
    147 	if (trailer) {
    148 		// Receive the trailer byte.
    149 		unsigned char end = 0;
    150 		status = dc_iostream_read (device->iostream, &end, 1, NULL);
    151 		if (status != DC_STATUS_SUCCESS) {
    152 			ERROR (abstract->context, "Failed to receive the answer.");
    153 			return status;
    154 		}
    155 
    156 		// Verify the trailer of the packet.
    157 		if (end != 0x45) {
    158 			ERROR (abstract->context, "Unexpected answer trailer byte.");
    159 			return DC_STATUS_PROTOCOL;
    160 		}
    161 	}
    162 
    163 	return DC_STATUS_SUCCESS;
    164 }
    165 
    166 static dc_status_t
    167 cressi_edy_transfer (cressi_edy_device_t *device, const unsigned char command[], unsigned int csize, unsigned char answer[], unsigned int asize, int trailer)
    168 {
    169 	unsigned int nretries = 0;
    170 	dc_status_t rc = DC_STATUS_SUCCESS;
    171 	while ((rc = cressi_edy_packet (device, command, csize, answer, asize, trailer)) != DC_STATUS_SUCCESS) {
    172 		if (rc != DC_STATUS_TIMEOUT && rc != DC_STATUS_PROTOCOL)
    173 			return rc;
    174 
    175 		// Abort if the maximum number of retries is reached.
    176 		if (nretries++ >= MAXRETRIES)
    177 			return rc;
    178 
    179 		// Delay the next attempt.
    180 		dc_iostream_sleep (device->iostream, 300);
    181 		dc_iostream_purge (device->iostream, DC_DIRECTION_INPUT);
    182 	}
    183 
    184 	return DC_STATUS_SUCCESS;
    185 }
    186 
    187 static dc_status_t
    188 cressi_edy_init1 (cressi_edy_device_t *device)
    189 {
    190 	unsigned char command[3] = {0x41, 0x42, 0x43};
    191 	unsigned char answer[3] = {0};
    192 
    193 	return cressi_edy_transfer (device, command, sizeof (command), answer, sizeof (answer), 0);
    194 }
    195 
    196 
    197 static dc_status_t
    198 cressi_edy_init2 (cressi_edy_device_t *device)
    199 {
    200 	unsigned char command[1] = {0x44};
    201 	unsigned char answer[1] = {0};
    202 
    203 	dc_status_t rc = cressi_edy_transfer (device, command, sizeof (command), answer, sizeof (answer), 0);
    204 	if (rc != DC_STATUS_SUCCESS)
    205 		return rc;
    206 
    207 	HEXDUMP (device->base.context, DC_LOGLEVEL_DEBUG, "Model", answer, sizeof (answer));
    208 
    209 	device->model = answer[0];
    210 
    211 	return DC_STATUS_SUCCESS;
    212 }
    213 
    214 
    215 static dc_status_t
    216 cressi_edy_init3 (cressi_edy_device_t *device)
    217 {
    218 	unsigned char command[1] = {0x0C};
    219 
    220 	return cressi_edy_transfer (device, command, sizeof (command), NULL, 0, 1);
    221 }
    222 
    223 
    224 static dc_status_t
    225 cressi_edy_quit (cressi_edy_device_t *device)
    226 {
    227 	unsigned char command[1] = {0x46};
    228 
    229 	return cressi_edy_transfer (device, command, sizeof (command), NULL, 0, 0);
    230 }
    231 
    232 
    233 dc_status_t
    234 cressi_edy_device_open (dc_device_t **out, dc_context_t *context, dc_iostream_t *iostream)
    235 {
    236 	dc_status_t status = DC_STATUS_SUCCESS;
    237 	cressi_edy_device_t *device = NULL;
    238 
    239 	if (out == NULL)
    240 		return DC_STATUS_INVALIDARGS;
    241 
    242 	// Allocate memory.
    243 	device = (cressi_edy_device_t *) dc_device_allocate (context, &cressi_edy_device_vtable);
    244 	if (device == NULL) {
    245 		ERROR (context, "Failed to allocate memory.");
    246 		return DC_STATUS_NOMEMORY;
    247 	}
    248 
    249 	// Set the default values.
    250 	device->iostream = iostream;
    251 	device->layout = NULL;
    252 	device->model = 0;
    253 	device->packetsize = 0;
    254 	memset (device->fingerprint, 0, sizeof (device->fingerprint));
    255 
    256 	// Set the serial communication protocol (1200 8N1).
    257 	status = dc_iostream_configure (device->iostream, 1200, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
    258 	if (status != DC_STATUS_SUCCESS) {
    259 		ERROR (context, "Failed to set the terminal attributes.");
    260 		goto error_free;
    261 	}
    262 
    263 	// Set the timeout for receiving data (1000 ms).
    264 	status = dc_iostream_set_timeout (device->iostream, 1000);
    265 	if (status != DC_STATUS_SUCCESS) {
    266 		ERROR (context, "Failed to set the timeout.");
    267 		goto error_free;
    268 	}
    269 
    270 	// Set the DTR line.
    271 	status = dc_iostream_set_dtr (device->iostream, 1);
    272 	if (status != DC_STATUS_SUCCESS) {
    273 		ERROR (context, "Failed to set the DTR line.");
    274 		goto error_free;
    275 	}
    276 
    277 	// Clear the RTS line.
    278 	status = dc_iostream_set_rts (device->iostream, 0);
    279 	if (status != DC_STATUS_SUCCESS) {
    280 		ERROR (context, "Failed to clear the RTS line.");
    281 		goto error_free;
    282 	}
    283 
    284 	// Make sure everything is in a sane state.
    285 	dc_iostream_sleep(device->iostream, 300);
    286 	dc_iostream_purge(device->iostream, DC_DIRECTION_ALL);
    287 
    288 	// Send the init commands.
    289 	cressi_edy_init1 (device);
    290 	cressi_edy_init2 (device);
    291 	if (device->model != ARCHIMEDE) {
    292 		cressi_edy_init3 (device);
    293 	}
    294 
    295 	device->packetsize = device->model == ARCHIMEDE ?
    296 		SZ_PAGE : SZ_PAGE * 4;
    297 
    298 	if (device->model == IQ700 || device->model == ARCHIMEDE) {
    299 		device->layout = &tusa_iq700_layout;
    300 	} else {
    301 		device->layout = &cressi_edy_layout;
    302 	}
    303 
    304 	if (device->model != ARCHIMEDE) {
    305 		// Set the serial communication protocol (4800 8N1).
    306 		status = dc_iostream_configure (device->iostream, 4800, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
    307 		if (status != DC_STATUS_SUCCESS) {
    308 			ERROR (context, "Failed to set the terminal attributes.");
    309 			goto error_free;
    310 		}
    311 	}
    312 
    313 	// Make sure everything is in a sane state.
    314 	dc_iostream_sleep(device->iostream, 300);
    315 	dc_iostream_purge(device->iostream, DC_DIRECTION_ALL);
    316 
    317 	*out = (dc_device_t*) device;
    318 
    319 	return DC_STATUS_SUCCESS;
    320 
    321 error_free:
    322 	dc_device_deallocate ((dc_device_t *) device);
    323 	return status;
    324 }
    325 
    326 
    327 static dc_status_t
    328 cressi_edy_device_close (dc_device_t *abstract)
    329 {
    330 	dc_status_t status = DC_STATUS_SUCCESS;
    331 	cressi_edy_device_t *device = (cressi_edy_device_t*) abstract;
    332 	dc_status_t rc = DC_STATUS_SUCCESS;
    333 
    334 	// Send the quit command.
    335 	rc = cressi_edy_quit (device);
    336 	if (rc != DC_STATUS_SUCCESS) {
    337 		dc_status_set_error(&status, rc);
    338 	}
    339 
    340 	return status;
    341 }
    342 
    343 
    344 static dc_status_t
    345 cressi_edy_device_read (dc_device_t *abstract, unsigned int address, unsigned char data[], unsigned int size)
    346 {
    347 	dc_status_t status = DC_STATUS_SUCCESS;
    348 	cressi_edy_device_t *device = (cressi_edy_device_t*) abstract;
    349 
    350 	if ((address % SZ_PAGE != 0) ||
    351 		(size    % device->packetsize != 0))
    352 		return DC_STATUS_INVALIDARGS;
    353 
    354 	unsigned int nbytes = 0;
    355 	while (nbytes < size) {
    356 		// Read the package.
    357 		unsigned int number = address / SZ_PAGE;
    358 		unsigned char command[3] = {0};
    359 		if (device->model == ARCHIMEDE) {
    360 			command[0] = 0x45;
    361 			command[1] = 0x52;
    362 			command[2] = number & 0xFF;
    363 		} else {
    364 			command[0] = 0x52;
    365 			command[1] = (number >> 8) & 0xFF; // high;
    366 			command[2] = (number     ) & 0xFF; // low
    367 		}
    368 		status = cressi_edy_transfer (device, command, sizeof (command), data + nbytes, device->packetsize, 1);
    369 		if (status != DC_STATUS_SUCCESS)
    370 			return status;
    371 
    372 		nbytes += device->packetsize;
    373 		address += device->packetsize;
    374 	}
    375 
    376 	if (device->model == ARCHIMEDE) {
    377 		array_reverse_nibbles (data, size);
    378 	}
    379 
    380 	return status;
    381 }
    382 
    383 
    384 static dc_status_t
    385 cressi_edy_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size)
    386 {
    387 	cressi_edy_device_t *device = (cressi_edy_device_t *) abstract;
    388 
    389 	if (size && size != sizeof (device->fingerprint))
    390 		return DC_STATUS_INVALIDARGS;
    391 
    392 	if (size)
    393 		memcpy (device->fingerprint, data, sizeof (device->fingerprint));
    394 	else
    395 		memset (device->fingerprint, 0, sizeof (device->fingerprint));
    396 
    397 	return DC_STATUS_SUCCESS;
    398 }
    399 
    400 
    401 static dc_status_t
    402 cressi_edy_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
    403 {
    404 	cressi_edy_device_t *device = (cressi_edy_device_t *) abstract;
    405 
    406 	// Allocate the required amount of memory.
    407 	if (!dc_buffer_resize (buffer, device->layout->memsize)) {
    408 		ERROR (abstract->context, "Insufficient buffer space available.");
    409 		return DC_STATUS_NOMEMORY;
    410 	}
    411 
    412 	// Emit a device info event.
    413 	dc_event_devinfo_t devinfo;
    414 	devinfo.model = device->model;
    415 	devinfo.firmware = 0;
    416 	devinfo.serial = 0;
    417 	device_event_emit (abstract, DC_EVENT_DEVINFO, &devinfo);
    418 
    419 	return device_dump_read (abstract, 0, dc_buffer_get_data (buffer),
    420 		dc_buffer_get_size (buffer), device->packetsize);
    421 }
    422 
    423 
    424 static dc_status_t
    425 cressi_edy_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata)
    426 {
    427 	cressi_edy_device_t *device = (cressi_edy_device_t *) abstract;
    428 	const cressi_edy_layout_t *layout = device->layout;
    429 
    430 	// Enable progress notifications.
    431 	dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER;
    432 	progress.maximum = 4 * SZ_PAGE +
    433 		(layout->rb_profile_end - layout->rb_profile_begin);
    434 	device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);
    435 
    436 	// Emit a device info event.
    437 	dc_event_devinfo_t devinfo;
    438 	devinfo.model = device->model;
    439 	devinfo.firmware = 0;
    440 	devinfo.serial = 0;
    441 	device_event_emit (abstract, DC_EVENT_DEVINFO, &devinfo);
    442 
    443 	// Read the logbook data.
    444 	unsigned char logbook[4 * SZ_PAGE] = {0};
    445 	dc_status_t rc = cressi_edy_device_read (abstract, layout->rb_logbook_offset, logbook, sizeof (logbook));
    446 	if (rc != DC_STATUS_SUCCESS) {
    447 		ERROR (abstract->context, "Failed to read the logbook data.");
    448 		return rc;
    449 	}
    450 
    451 	// Get the logbook pointers.
    452 	unsigned int last  = logbook[layout->config + 0];
    453 	unsigned int first = logbook[layout->config + 1];
    454 	if (first < layout->rb_logbook_begin || first >= layout->rb_logbook_end ||
    455 		last < layout->rb_logbook_begin || last >= layout->rb_logbook_end) {
    456 		if (last == 0xFF)
    457 			return DC_STATUS_SUCCESS;
    458 		ERROR (abstract->context, "Invalid ringbuffer pointer detected (0x%02x 0x%02x).", first, last);
    459 		return DC_STATUS_DATAFORMAT;
    460 	}
    461 
    462 	// Get the number of logbook items.
    463 	unsigned int count = ringbuffer_distance (first, last, DC_RINGBUFFER_EMPTY, layout->rb_logbook_begin, layout->rb_logbook_end) + 1;
    464 
    465 	// Get the profile pointer.
    466 	unsigned int eop = array_uint_le (logbook + layout->config + 2, layout->rb_logbook_size) * SZ_PAGE + layout->rb_profile_begin;
    467 	if (eop < layout->rb_profile_begin || eop >= layout->rb_profile_end) {
    468 		ERROR (abstract->context, "Invalid ringbuffer pointer detected (0x%04x).", eop);
    469 		return DC_STATUS_DATAFORMAT;
    470 	}
    471 
    472 	// The logbook ringbuffer can store at most 60 dives, even if the profile
    473 	// data could store more (e.g. many small dives). But it's also possible
    474 	// that the profile ringbuffer is filled faster than the logbook ringbuffer
    475 	// (e.g. many large dives). We detect this by checking the total length.
    476 	unsigned int total = 0;
    477 	unsigned int idx = last;
    478 	unsigned int previous = eop;
    479 	for (unsigned int i = 0; i < count; ++i) {
    480 		// Get the pointer to the profile data.
    481 		unsigned int current = array_uint_le (logbook + idx * layout->rb_logbook_size, layout->rb_logbook_size) * SZ_PAGE + layout->rb_profile_begin;
    482 		if (current < layout->rb_profile_begin || current >= layout->rb_profile_end) {
    483 			ERROR (abstract->context, "Invalid ringbuffer pointer detected (0x%04x).", current);
    484 			return DC_STATUS_DATAFORMAT;
    485 		}
    486 
    487 		// Get the profile length.
    488 		unsigned int length = ringbuffer_distance (current, previous, DC_RINGBUFFER_FULL, layout->rb_profile_begin, layout->rb_profile_end);
    489 
    490 		// Check for a ringbuffer overflow.
    491 		if (total + length > layout->rb_profile_end - layout->rb_profile_begin) {
    492 			count = i;
    493 			break;
    494 		}
    495 
    496 		total += length;
    497 
    498 		previous = current;
    499 
    500 		if (idx == layout->rb_logbook_begin)
    501 			idx = layout->rb_logbook_end;
    502 		idx--;
    503 	}
    504 
    505 	// Update and emit a progress event.
    506 	progress.current += 4 * SZ_PAGE;
    507 	progress.maximum = 4 * SZ_PAGE + total;
    508 	device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);
    509 
    510 	// Create the ringbuffer stream.
    511 	dc_rbstream_t *rbstream = NULL;
    512 	rc = dc_rbstream_new (&rbstream, abstract, SZ_PAGE, device->packetsize, layout->rb_profile_begin, layout->rb_profile_end, eop, DC_RBSTREAM_BACKWARD);
    513 	if (rc != DC_STATUS_SUCCESS) {
    514 		ERROR (abstract->context, "Failed to create the ringbuffer stream.");
    515 		return rc;
    516 	}
    517 
    518 	// Memory buffer for the profile data.
    519 	unsigned char *buffer = (unsigned char *) malloc (total);
    520 	if (buffer == NULL) {
    521 		ERROR (abstract->context, "Failed to allocate memory.");
    522 		dc_rbstream_free (rbstream);
    523 		return DC_STATUS_NOMEMORY;
    524 	}
    525 
    526 	unsigned int offset = total;
    527 
    528 	idx = last;
    529 	previous = eop;
    530 	for (unsigned int i = 0; i < count; ++i) {
    531 		// Get the pointer to the profile data.
    532 		unsigned int current = array_uint_le (logbook + idx * layout->rb_logbook_size, layout->rb_logbook_size) * SZ_PAGE + layout->rb_profile_begin;
    533 		if (current < layout->rb_profile_begin || current >= layout->rb_profile_end) {
    534 			ERROR (abstract->context, "Invalid ringbuffer pointer detected (0x%04x).", current);
    535 			dc_rbstream_free (rbstream);
    536 			free(buffer);
    537 			return DC_STATUS_DATAFORMAT;
    538 		}
    539 
    540 		// Get the profile length.
    541 		unsigned int length = ringbuffer_distance (current, previous, DC_RINGBUFFER_FULL, layout->rb_profile_begin, layout->rb_profile_end);
    542 
    543 		// Move to the begin of the current dive.
    544 		offset -= length;
    545 
    546 		// Read the dive.
    547 		rc = dc_rbstream_read (rbstream, &progress, buffer + offset, length);
    548 		if (rc != DC_STATUS_SUCCESS) {
    549 			ERROR (abstract->context, "Failed to read the dive.");
    550 			dc_rbstream_free (rbstream);
    551 			free (buffer);
    552 			return rc;
    553 		}
    554 
    555 		if (length < SZ_HEADER) {
    556 			ERROR (abstract->context, "Dive header is too small (%u).", length);
    557 			dc_rbstream_free (rbstream);
    558 			free (buffer);
    559 			return DC_STATUS_DATAFORMAT;
    560 		}
    561 
    562 		unsigned char *p = buffer + offset;
    563 
    564 		if (memcmp (p, device->fingerprint, sizeof (device->fingerprint)) == 0)
    565 			break;
    566 
    567 		if (callback && !callback (p, length, p, sizeof (device->fingerprint), userdata))
    568 			break;
    569 
    570 		previous = current;
    571 
    572 		if (idx == layout->rb_logbook_begin)
    573 			idx = layout->rb_logbook_end;
    574 		idx--;
    575 	}
    576 
    577 	dc_rbstream_free (rbstream);
    578 	free(buffer);
    579 
    580 	return DC_STATUS_SUCCESS;
    581 }