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_atom2.c (37113B)


      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 
     25 #include <libdivecomputer/ble.h>
     26 
     27 #include "oceanic_atom2.h"
     28 #include "oceanic_common.h"
     29 #include "context-private.h"
     30 #include "device-private.h"
     31 #include "array.h"
     32 #include "ringbuffer.h"
     33 #include "checksum.h"
     34 #include "platform.h"
     35 
     36 #define ISINSTANCE(device) dc_device_isinstance((device), &oceanic_atom2_device_vtable.base)
     37 
     38 #define MAXPACKET  256
     39 #define MAXRETRIES 2
     40 #define MAXDELAY   16
     41 #define INVALID    0xFFFFFFFF
     42 
     43 #define CMD_INIT      0xA8
     44 #define CMD_VERSION   0x84
     45 #define CMD_HANDSHAKE 0xE5
     46 #define CMD_READ1     0xB1
     47 #define CMD_READ8     0xB4
     48 #define CMD_READ16    0xB8
     49 #define CMD_READ16HI  0xF6
     50 #define CMD_WRITE     0xB2
     51 #define CMD_KEEPALIVE 0x91
     52 #define CMD_QUIT      0x6A
     53 
     54 #define ACK 0x5A
     55 #define NAK 0xA5
     56 
     57 #define REPEAT 50
     58 
     59 typedef struct oceanic_atom2_device_t {
     60 	oceanic_common_device_t base;
     61 	dc_iostream_t *iostream;
     62 	unsigned int handshake_repeat;
     63 	unsigned int handshake_counter;
     64 	unsigned int sequence;
     65 	unsigned int delay;
     66 	unsigned int extra;
     67 	unsigned int bigpage;
     68 	unsigned char cache[256];
     69 	unsigned int cached_page;
     70 	unsigned int cached_highmem;
     71 } oceanic_atom2_device_t;
     72 
     73 static dc_status_t oceanic_atom2_device_read (dc_device_t *abstract, unsigned int address, unsigned char data[], unsigned int size);
     74 static dc_status_t oceanic_atom2_device_write (dc_device_t *abstract, unsigned int address, const unsigned char data[], unsigned int size);
     75 static dc_status_t oceanic_atom2_device_close (dc_device_t *abstract);
     76 
     77 static const oceanic_common_device_vtable_t oceanic_atom2_device_vtable = {
     78 	{
     79 		sizeof(oceanic_atom2_device_t),
     80 		DC_FAMILY_OCEANIC_ATOM2,
     81 		oceanic_common_device_set_fingerprint, /* set_fingerprint */
     82 		oceanic_atom2_device_read, /* read */
     83 		oceanic_atom2_device_write, /* write */
     84 		oceanic_common_device_dump, /* dump */
     85 		oceanic_common_device_foreach, /* foreach */
     86 		NULL, /* timesync */
     87 		oceanic_atom2_device_close /* close */
     88 	},
     89 	oceanic_common_device_devinfo,
     90 	oceanic_common_device_pointers,
     91 	oceanic_common_device_logbook,
     92 	oceanic_common_device_profile,
     93 };
     94 
     95 static const oceanic_common_layout_t aeris_f10_layout = {
     96 	0x10000, /* memsize */
     97 	0, /* highmem */
     98 	0x0000, /* cf_devinfo */
     99 	0x0040, /* cf_pointers */
    100 	0x0100, /* rb_logbook_begin */
    101 	0x0D80, /* rb_logbook_end */
    102 	32, /* rb_logbook_entry_size */
    103 	1, /* rb_logbook_direction */
    104 	0x0D80, /* rb_profile_begin */
    105 	0x10000, /* rb_profile_end */
    106 	0, /* pt_mode_global */
    107 	3, /* pt_mode_logbook */
    108 	0, /* pt_mode_serial */
    109 };
    110 
    111 static const oceanic_common_layout_t aeris_f11_layout = {
    112 	0x20000, /* memsize */
    113 	0, /* highmem */
    114 	0x0000, /* cf_devinfo */
    115 	0x0040, /* cf_pointers */
    116 	0x0100, /* rb_logbook_begin */
    117 	0x0D80, /* rb_logbook_end */
    118 	32, /* rb_logbook_entry_size */
    119 	1, /* rb_logbook_direction */
    120 	0x0D80, /* rb_profile_begin */
    121 	0x20000, /* rb_profile_end */
    122 	0, /* pt_mode_global */
    123 	2, /* pt_mode_logbook */
    124 	0, /* pt_mode_serial */
    125 };
    126 
    127 static const oceanic_common_layout_t oceanic_default_layout = {
    128 	0x10000, /* memsize */
    129 	0, /* highmem */
    130 	0x0000, /* cf_devinfo */
    131 	0x0040, /* cf_pointers */
    132 	0x0240, /* rb_logbook_begin */
    133 	0x0A40, /* rb_logbook_end */
    134 	8, /* rb_logbook_entry_size */
    135 	1, /* rb_logbook_direction */
    136 	0x0A40, /* rb_profile_begin */
    137 	0x10000, /* rb_profile_end */
    138 	0, /* pt_mode_global */
    139 	0, /* pt_mode_logbook */
    140 	0, /* pt_mode_serial */
    141 };
    142 
    143 static const oceanic_common_layout_t oceanic_atom1_layout = {
    144 	0x8000, /* memsize */
    145 	0, /* highmem */
    146 	0x0000, /* cf_devinfo */
    147 	0x0040, /* cf_pointers */
    148 	0x0240, /* rb_logbook_begin */
    149 	0x0440, /* rb_logbook_end */
    150 	8, /* rb_logbook_entry_size */
    151 	1, /* rb_logbook_direction */
    152 	0x0440, /* rb_profile_begin */
    153 	0x8000, /* rb_profile_end */
    154 	0, /* pt_mode_global */
    155 	0, /* pt_mode_logbook */
    156 	0, /* pt_mode_serial */
    157 };
    158 
    159 static const oceanic_common_layout_t oceanic_atom2a_layout = {
    160 	0xFFF0, /* memsize */
    161 	0, /* highmem */
    162 	0x0000, /* cf_devinfo */
    163 	0x0040, /* cf_pointers */
    164 	0x0240, /* rb_logbook_begin */
    165 	0x0A40, /* rb_logbook_end */
    166 	8, /* rb_logbook_entry_size */
    167 	1, /* rb_logbook_direction */
    168 	0x0A40, /* rb_profile_begin */
    169 	0xFE00, /* rb_profile_end */
    170 	0, /* pt_mode_global */
    171 	0, /* pt_mode_logbook */
    172 	0, /* pt_mode_serial */
    173 };
    174 
    175 static const oceanic_common_layout_t oceanic_atom2b_layout = {
    176 	0x10000, /* memsize */
    177 	0, /* highmem */
    178 	0x0000, /* cf_devinfo */
    179 	0x0040, /* cf_pointers */
    180 	0x0240, /* rb_logbook_begin */
    181 	0x0A40, /* rb_logbook_end */
    182 	8, /* rb_logbook_entry_size */
    183 	1, /* rb_logbook_direction */
    184 	0x0A40, /* rb_profile_begin */
    185 	0xFE00, /* rb_profile_end */
    186 	0, /* pt_mode_global */
    187 	0, /* pt_mode_logbook */
    188 	0, /* pt_mode_serial */
    189 };
    190 
    191 static const oceanic_common_layout_t oceanic_atom2c_layout = {
    192 	0xFFF0, /* memsize */
    193 	0, /* highmem */
    194 	0x0000, /* cf_devinfo */
    195 	0x0040, /* cf_pointers */
    196 	0x0240, /* rb_logbook_begin */
    197 	0x0A40, /* rb_logbook_end */
    198 	8, /* rb_logbook_entry_size */
    199 	1, /* rb_logbook_direction */
    200 	0x0A40, /* rb_profile_begin */
    201 	0xFFF0, /* rb_profile_end */
    202 	0, /* pt_mode_global */
    203 	0, /* pt_mode_logbook */
    204 	0, /* pt_mode_serial */
    205 };
    206 
    207 static const oceanic_common_layout_t sherwood_wisdom_layout = {
    208 	0xFFF0, /* memsize */
    209 	0, /* highmem */
    210 	0x0000, /* cf_devinfo */
    211 	0x0040, /* cf_pointers */
    212 	0x03D0, /* rb_logbook_begin */
    213 	0x0A40, /* rb_logbook_end */
    214 	8, /* rb_logbook_entry_size */
    215 	1, /* rb_logbook_direction */
    216 	0x0A40, /* rb_profile_begin */
    217 	0xFE00, /* rb_profile_end */
    218 	0, /* pt_mode_global */
    219 	0, /* pt_mode_logbook */
    220 	0, /* pt_mode_serial */
    221 };
    222 
    223 static const oceanic_common_layout_t oceanic_proplus3_layout = {
    224 	0x10000, /* memsize */
    225 	0, /* highmem */
    226 	0x0000, /* cf_devinfo */
    227 	0x0040, /* cf_pointers */
    228 	0x03E0, /* rb_logbook_begin */
    229 	0x0A40, /* rb_logbook_end */
    230 	8, /* rb_logbook_entry_size */
    231 	1, /* rb_logbook_direction */
    232 	0x0A40, /* rb_profile_begin */
    233 	0xFE00, /* rb_profile_end */
    234 	0, /* pt_mode_global */
    235 	0, /* pt_mode_logbook */
    236 	0, /* pt_mode_serial */
    237 };
    238 
    239 static const oceanic_common_layout_t tusa_zenair_layout = {
    240 	0xFFF0, /* memsize */
    241 	0, /* highmem */
    242 	0x0000, /* cf_devinfo */
    243 	0x0040, /* cf_pointers */
    244 	0x0240, /* rb_logbook_begin */
    245 	0x0A40, /* rb_logbook_end */
    246 	8, /* rb_logbook_entry_size */
    247 	1, /* rb_logbook_direction */
    248 	0x0A40, /* rb_profile_begin */
    249 	0xFE00, /* rb_profile_end */
    250 	0, /* pt_mode_global */
    251 	1, /* pt_mode_logbook */
    252 	0, /* pt_mode_serial */
    253 };
    254 
    255 static const oceanic_common_layout_t oceanic_oc1_layout = {
    256 	0x20000, /* memsize */
    257 	0, /* highmem */
    258 	0x0000, /* cf_devinfo */
    259 	0x0040, /* cf_pointers */
    260 	0x0240, /* rb_logbook_begin */
    261 	0x0A40, /* rb_logbook_end */
    262 	8, /* rb_logbook_entry_size */
    263 	1, /* rb_logbook_direction */
    264 	0x0A40, /* rb_profile_begin */
    265 	0x1FE00, /* rb_profile_end */
    266 	0, /* pt_mode_global */
    267 	1, /* pt_mode_logbook */
    268 	0, /* pt_mode_serial */
    269 };
    270 
    271 static const oceanic_common_layout_t oceanic_oci_layout = {
    272 	0x20000, /* memsize */
    273 	0, /* highmem */
    274 	0x0000, /* cf_devinfo */
    275 	0x0040, /* cf_pointers */
    276 	0x10C0, /* rb_logbook_begin */
    277 	0x1400, /* rb_logbook_end */
    278 	8, /* rb_logbook_entry_size */
    279 	1, /* rb_logbook_direction */
    280 	0x1400, /* rb_profile_begin */
    281 	0x1FE00, /* rb_profile_end */
    282 	0, /* pt_mode_global */
    283 	1, /* pt_mode_logbook */
    284 	0, /* pt_mode_serial */
    285 };
    286 
    287 static const oceanic_common_layout_t oceanic_atom3_layout = {
    288 	0x20000, /* memsize */
    289 	0, /* highmem */
    290 	0x0000, /* cf_devinfo */
    291 	0x0040, /* cf_pointers */
    292 	0x0400, /* rb_logbook_begin */
    293 	0x0A40, /* rb_logbook_end */
    294 	8, /* rb_logbook_entry_size */
    295 	1, /* rb_logbook_direction */
    296 	0x0A40, /* rb_profile_begin */
    297 	0x1FE00, /* rb_profile_end */
    298 	0, /* pt_mode_global */
    299 	1, /* pt_mode_logbook */
    300 	0, /* pt_mode_serial */
    301 };
    302 
    303 static const oceanic_common_layout_t oceanic_vt4_layout = {
    304 	0x20000, /* memsize */
    305 	0, /* highmem */
    306 	0x0000, /* cf_devinfo */
    307 	0x0040, /* cf_pointers */
    308 	0x0420, /* rb_logbook_begin */
    309 	0x0A40, /* rb_logbook_end */
    310 	8, /* rb_logbook_entry_size */
    311 	1, /* rb_logbook_direction */
    312 	0x0A40, /* rb_profile_begin */
    313 	0x1FE00, /* rb_profile_end */
    314 	0, /* pt_mode_global */
    315 	1, /* pt_mode_logbook */
    316 	0, /* pt_mode_serial */
    317 };
    318 
    319 static const oceanic_common_layout_t hollis_tx1_layout = {
    320 	0x40000, /* memsize */
    321 	0, /* highmem */
    322 	0x0000, /* cf_devinfo */
    323 	0x0040, /* cf_pointers */
    324 	0x0780, /* rb_logbook_begin */
    325 	0x1000, /* rb_logbook_end */
    326 	8, /* rb_logbook_entry_size */
    327 	1, /* rb_logbook_direction */
    328 	0x1000, /* rb_profile_begin */
    329 	0x40000, /* rb_profile_end */
    330 	0, /* pt_mode_global */
    331 	1, /* pt_mode_logbook */
    332 	0, /* pt_mode_serial */
    333 };
    334 
    335 static const oceanic_common_layout_t oceanic_veo1_layout = {
    336 	0x0400, /* memsize */
    337 	0, /* highmem */
    338 	0x0000, /* cf_devinfo */
    339 	0x0040, /* cf_pointers */
    340 	0x0400, /* rb_logbook_begin */
    341 	0x0400, /* rb_logbook_end */
    342 	8, /* rb_logbook_entry_size */
    343 	1, /* rb_logbook_direction */
    344 	0x0400, /* rb_profile_begin */
    345 	0x0400, /* rb_profile_end */
    346 	0, /* pt_mode_global */
    347 	0, /* pt_mode_logbook */
    348 	0, /* pt_mode_serial */
    349 };
    350 
    351 static const oceanic_common_layout_t oceanic_reactpro_layout = {
    352 	0xFFF0, /* memsize */
    353 	0, /* highmem */
    354 	0x0000, /* cf_devinfo */
    355 	0x0040, /* cf_pointers */
    356 	0x0400, /* rb_logbook_begin */
    357 	0x0600, /* rb_logbook_end */
    358 	8, /* rb_logbook_entry_size */
    359 	1, /* rb_logbook_direction */
    360 	0x0600, /* rb_profile_begin */
    361 	0xFE00, /* rb_profile_end */
    362 	1, /* pt_mode_global */
    363 	1, /* pt_mode_logbook */
    364 	0, /* pt_mode_serial */
    365 };
    366 
    367 static const oceanic_common_layout_t oceanic_proplusx_layout = {
    368 	0x440000, /* memsize */
    369 	0x40000, /* highmem */
    370 	0x0000, /* cf_devinfo */
    371 	0x0040, /* cf_pointers */
    372 	0x1000, /* rb_logbook_begin */
    373 	0x10000, /* rb_logbook_end */
    374 	16, /* rb_logbook_entry_size */
    375 	1, /* rb_logbook_direction */
    376 	0x40000, /* rb_profile_begin */
    377 	0x440000, /* rb_profile_end */
    378 	0, /* pt_mode_global */
    379 	1, /* pt_mode_logbook */
    380 	0, /* pt_mode_serial */
    381 };
    382 
    383 static const oceanic_common_layout_t aqualung_i770r_layout = {
    384 	0x640000, /* memsize */
    385 	0x40000, /* highmem */
    386 	0x0000, /* cf_devinfo */
    387 	0x0040, /* cf_pointers */
    388 	0x2000, /* rb_logbook_begin */
    389 	0x10000, /* rb_logbook_end */
    390 	16, /* rb_logbook_entry_size */
    391 	1, /* rb_logbook_direction */
    392 	0x40000, /* rb_profile_begin */
    393 	0x640000, /* rb_profile_end */
    394 	0, /* pt_mode_global */
    395 	1, /* pt_mode_logbook */
    396 	0, /* pt_mode_serial */
    397 };
    398 
    399 static const oceanic_common_layout_t aeris_a300cs_layout = {
    400 	0x40000, /* memsize */
    401 	0, /* highmem */
    402 	0x0000, /* cf_devinfo */
    403 	0x0040, /* cf_pointers */
    404 	0x0900, /* rb_logbook_begin */
    405 	0x1000, /* rb_logbook_end */
    406 	16, /* rb_logbook_entry_size */
    407 	1, /* rb_logbook_direction */
    408 	0x1000, /* rb_profile_begin */
    409 	0x3FE00, /* rb_profile_end */
    410 	0, /* pt_mode_global */
    411 	1, /* pt_mode_logbook */
    412 	0, /* pt_mode_serial */
    413 };
    414 
    415 static const oceanic_common_layout_t aqualung_i450t_layout = {
    416 	0x40000, /* memsize */
    417 	0, /* highmem */
    418 	0x0000, /* cf_devinfo */
    419 	0x0040, /* cf_pointers */
    420 	0x10C0, /* rb_logbook_begin */
    421 	0x1400, /* rb_logbook_end */
    422 	16, /* rb_logbook_entry_size */
    423 	1, /* rb_logbook_direction */
    424 	0x1400, /* rb_profile_begin */
    425 	0x3FE00, /* rb_profile_end */
    426 	0, /* pt_mode_global */
    427 	1, /* pt_mode_logbook */
    428 	0, /* pt_mode_serial */
    429 };
    430 
    431 static const oceanic_common_version_t versions[] = {
    432 	{"OCEVEO10 \0\0   8K", 0,      VEO10,         &oceanic_veo1_layout},
    433 	{"AERIS XR1 NX R\0\0", 0,      XR1NX,         &oceanic_veo1_layout},
    434 
    435 	{"ATOM rev\0\0  256K", 0,      ATOM1,         &oceanic_atom1_layout},
    436 
    437 	{"MANTA  R\0\0  512K", 0x3242, MANTA,         &oceanic_atom2a_layout},
    438 	{"MANTA  R\0\0  512K", 0,      MANTA,         &oceanic_atom2c_layout},
    439 	{"2M ATOM r\0\0 512K", 0x3349, ATOM2,         &oceanic_atom2a_layout},
    440 	{"2M ATOM r\0\0 512K", 0,      ATOM2,         &oceanic_atom2c_layout},
    441 	{"OCE GEO R\0\0 512K", 0x3242, GEO,           &oceanic_atom2a_layout},
    442 	{"OCE GEO R\0\0 512K", 0,      GEO,           &oceanic_atom2c_layout},
    443 
    444 	{"INSIGHT2 \0\0 512K", 0,      INSIGHT2,      &oceanic_atom2a_layout},
    445 	{"OCEVEO30 \0\0 512K", 0,      VEO30,         &oceanic_atom2a_layout},
    446 	{"ATMOSAI R\0\0 512K", 0,      ATMOSAI2,      &oceanic_atom2a_layout},
    447 	{"PROPLUS2 \0\0 512K", 0,      PROPLUS21,     &oceanic_atom2a_layout},
    448 	{"OCEGEO20 \0\0 512K", 0,      GEO20,         &oceanic_atom2a_layout},
    449 	{"AQUAI200 \0\0 512K", 0,      I200,          &oceanic_atom2a_layout},
    450 	{"AQUA200C \0\0 512K", 0,      I200C,         &oceanic_atom2a_layout},
    451 
    452 	{"ELEMENT2 \0\0 512K", 0,      ELEMENT2,      &oceanic_atom2b_layout},
    453 	{"OCEVEO20 \0\0 512K", 0,      VEO20,         &oceanic_atom2b_layout},
    454 	{"TUSAZEN \0\0  512K", 0,      ZEN,           &oceanic_atom2b_layout},
    455 	{"AQUAI300 \0\0 512K", 0,      I300,          &oceanic_atom2b_layout},
    456 	{"HOLLDG03 \0\0 512K", 0,      DG03,          &oceanic_atom2b_layout},
    457 	{"AQUAI100 \0\0 512K", 0,      I100,          &oceanic_atom2b_layout},
    458 	{"AQUA300C \0\0 512K", 0,      I300C,         &oceanic_atom2b_layout},
    459 	{"OCEGEO40 \0\0 512K", 0,      GEO40,         &oceanic_atom2b_layout},
    460 	{"VEOSMART \0\0 512K", 0,      VEO40,         &oceanic_atom2b_layout},
    461 
    462 	{"2M EPIC r\0\0 512K", 0,      EPICA,         &oceanic_atom2c_layout},
    463 	{"EPIC1  R\0\0  512K", 0,      EPICB,         &oceanic_atom2c_layout},
    464 	{"AERIA300 \0\0 512K", 0,      A300,          &oceanic_atom2c_layout},
    465 
    466 	{"OCE VT3 R\0\0 512K", 0,      VT3,           &oceanic_default_layout},
    467 	{"ELITET3 R\0\0 512K", 0,      T3A,           &oceanic_default_layout},
    468 	{"ELITET31 \0\0 512K", 0,      T3B,           &oceanic_default_layout},
    469 	{"DATAMASK \0\0 512K", 0,      DATAMASK,      &oceanic_default_layout},
    470 	{"COMPMASK \0\0 512K", 0,      COMPUMASK,     &oceanic_default_layout},
    471 
    472 	{"WISDOM R\0\0  512K", 0x3342, WISDOM3,       &sherwood_wisdom_layout},
    473 	{"WISDOM R\0\0  512K", 0,      WISDOM2,       &sherwood_wisdom_layout},
    474 
    475 	{"PROPLUS3 \0\0 512K", 0,      PROPLUS3,      &oceanic_proplus3_layout},
    476 	{"PROPLUS4 \0\0 512K", 0,      PROPLUS4,      &oceanic_proplus3_layout},
    477 
    478 	{"TUZENAIR \0\0 512K", 0,      ZENAIR,        &tusa_zenair_layout},
    479 	{"AMPHOSSW \0\0 512K", 0,      AMPHOS,        &tusa_zenair_layout},
    480 	{"AMPHOAIR \0\0 512K", 0,      AMPHOSAIR,     &tusa_zenair_layout},
    481 	{"VOYAGE2G \0\0 512K", 0,      VOYAGER2G,     &tusa_zenair_layout},
    482 	{"TUSTALIS \0\0 512K", 0,      TALIS,         &tusa_zenair_layout},
    483 	{"AMPHOS20 \0\0 512K", 0,      AMPHOS2,       &tusa_zenair_layout},
    484 	{"AMPAIR20 \0\0 512K", 0,      AMPHOSAIR2,    &tusa_zenair_layout},
    485 
    486 	{"REACPRO2 \0\0 512K", 0,      REACTPROWHITE, &oceanic_reactpro_layout},
    487 
    488 	{"FREEWAER \0\0 512K", 0,      F10A,          &aeris_f10_layout},
    489 	{"OCEANF10 \0\0 512K", 0,      F10B,          &aeris_f10_layout},
    490 	{"MUNDIAL R\0\0 512K", 0x3300, MUNDIAL3,      &aeris_f10_layout},
    491 	{"MUNDIAL R\0\0 512K", 0,      MUNDIAL2,      &aeris_f10_layout},
    492 
    493 	{"AERISF11 \0\0 1024", 0,      F11A,          &aeris_f11_layout},
    494 	{"OCEANF11 \0\0 1024", 0,      F11B,          &aeris_f11_layout},
    495 
    496 	{"OCWATCH R\0\0 1024", 0,      OC1A,          &oceanic_oc1_layout},
    497 	{"OC1WATCH \0\0 1024", 0,      OC1B,          &oceanic_oc1_layout},
    498 	{"OCSWATCH \0\0 1024", 0,      OCS,           &oceanic_oc1_layout},
    499 	{"AQUAI550 \0\0 1024", 0,      I550,          &oceanic_oc1_layout},
    500 	{"AQUA550C \0\0 1024", 0,      I550C,         &oceanic_oc1_layout},
    501 	{"WISDOM04 \0\0 1024", 0,      WISDOM4,       &oceanic_oc1_layout},
    502 	{"AQUA470C \0\0 1024", 0,      I470TC,        &oceanic_oc1_layout},
    503 	{"AQUA200C \0\0 1024", 0,      I200CV2,       &oceanic_oc1_layout},
    504 	{"GEOAIR   \0\0 1024", 0,      GEOAIR,        &oceanic_oc1_layout},
    505 
    506 	{"OCEANOCI \0\0 1024", 0,      OCI,           &oceanic_oci_layout},
    507 
    508 	{"OCEATOM3 \0\0 1024", 0,      ATOM3,         &oceanic_atom3_layout},
    509 	{"ATOM31  \0\0  1024", 0,      ATOM31,        &oceanic_atom3_layout},
    510 
    511 	{"OCEANVT4 \0\0 1024", 0,      VT4,           &oceanic_vt4_layout},
    512 	{"OCEAVT41 \0\0 1024", 0,      VT41,          &oceanic_vt4_layout},
    513 	{"AERISAIR \0\0 1024", 0,      A300AI,        &oceanic_vt4_layout},
    514 	{"SWVISION \0\0 1024", 0,      VISION,        &oceanic_vt4_layout},
    515 	{"XPSUBAIR \0\0 1024", 0,      XPAIR,         &oceanic_vt4_layout},
    516 	{"AQUAI100 \0\0 1024", 0,      I100V2,        &oceanic_vt4_layout},
    517 
    518 	{"HOLLDG04 \0\0 2048", 0,      TX1,           &hollis_tx1_layout},
    519 
    520 	{"AER300CS \0\0 2048", 0,      A300CS,        &aeris_a300cs_layout},
    521 	{"OCEANVTX \0\0 2048", 0,      VTX,           &aeris_a300cs_layout},
    522 	{"AQUAI750 \0\0 2048", 0,      I750TC,        &aeris_a300cs_layout},
    523 	{"SWDRAGON \0\0 2048", 0,      SAGE,          &aeris_a300cs_layout},
    524 	{"SWBEACON \0\0 2048", 0,      BEACON,        &aeris_a300cs_layout},
    525 
    526 	{"AQUAI450 \0\0 2048", 0,      I450T,         &aqualung_i450t_layout},
    527 
    528 	{"OCEANOCX \0\0 \0\0\0\0", 0,  PROPLUSX,      &oceanic_proplusx_layout},
    529 
    530 	{"AQUA770R \0\0 \0\0\0\0", 0,  I770R,         &aqualung_i770r_layout},
    531 };
    532 
    533 /*
    534  * The BLE GATT packet size is up to 20 bytes and the format is:
    535  *
    536  * byte 0: <0xCD>
    537  *         Seems to always have this value. Don't ask what it means
    538  * byte 1: <d 1 c s s s s s>
    539  *          d=0 means "command", d=1 means "reply from dive computer"
    540  *          1 is always set, afaik
    541  *          c=0 means "last packet" in sequence, c=1 means "more packets coming"
    542  *          sssss is a 5-bit sequence number for packets
    543  * byte 2: <cmd seq>
    544  *          starts at 0 for the connection, incremented for each command
    545  * byte 3: <length of data>
    546  *          1-16 bytes of data per packet.
    547  * byte 4..n: <data>
    548  */
    549 static dc_status_t
    550 oceanic_atom2_ble_write (oceanic_atom2_device_t *device, const unsigned char data[], unsigned int size)
    551 {
    552 	dc_status_t rc = DC_STATUS_SUCCESS;
    553 	unsigned char buf[20];
    554 	unsigned char cmd_seq = device->sequence;
    555 	unsigned char pkt_seq = 0;
    556 
    557 	unsigned int nbytes = 0;
    558 	while (nbytes < size) {
    559 		unsigned char status = 0x40;
    560 		unsigned int length = size - nbytes;
    561 		if (length > sizeof(buf) - 4) {
    562 			length = sizeof(buf) - 4;
    563 			status |= 0x20;
    564 		}
    565 		buf[0] = 0xcd;
    566 		buf[1] = status | (pkt_seq & 0x1F);
    567 		buf[2] = cmd_seq;
    568 		buf[3] = length;
    569 		memcpy (buf + 4, data, length);
    570 
    571 		rc = dc_iostream_write (device->iostream, buf, 4 + length, NULL);
    572 		if (rc != DC_STATUS_SUCCESS)
    573 			return rc;
    574 
    575 		nbytes += length;
    576 		pkt_seq++;
    577 	}
    578 
    579 	return DC_STATUS_SUCCESS;
    580 }
    581 
    582 static dc_status_t
    583 oceanic_atom2_ble_read (oceanic_atom2_device_t *device, unsigned char data[], unsigned int size, unsigned int *actual)
    584 {
    585 	dc_status_t rc = DC_STATUS_SUCCESS;
    586 	dc_device_t *abstract = (dc_device_t *) device;
    587 	unsigned char buf[20];
    588 	unsigned char cmd_seq = device->sequence;
    589 	unsigned char pkt_seq = 0;
    590 
    591 	unsigned int nbytes = 0;
    592 	while (1) {
    593 		size_t transferred = 0;
    594 		rc = dc_iostream_read (device->iostream, buf, sizeof(buf), &transferred);
    595 		if (rc != DC_STATUS_SUCCESS)
    596 			return rc;
    597 
    598 		if (transferred < 4) {
    599 			ERROR (abstract->context, "Invalid packet size (" DC_PRINTF_SIZE ").", transferred);
    600 			return DC_STATUS_PROTOCOL;
    601 		}
    602 
    603 		// Verify the start byte.
    604 		if (buf[0] != 0xcd) {
    605 			ERROR (abstract->context, "Unexpected packet start byte (%02x).", buf[0]);
    606 			return DC_STATUS_PROTOCOL;
    607 		}
    608 
    609 		// Verify the status byte.
    610 		unsigned char status = buf[1];
    611 		unsigned char expect = 0xc0 | (pkt_seq & 0x1F) | (status & 0x20);
    612 		if (status != expect) {
    613 			ERROR (abstract->context, "Unexpected packet status byte (%02x %02x).", status, expect);
    614 			return DC_STATUS_PROTOCOL;
    615 		}
    616 
    617 		// Verify the sequence byte.
    618 		if (buf[2] != cmd_seq) {
    619 			ERROR (abstract->context, "Unexpected packet sequence byte (%02x %02x).", buf[2], cmd_seq);
    620 			return DC_STATUS_PROTOCOL;
    621 		}
    622 
    623 		// Verify the length byte.
    624 		unsigned int length = buf[3];
    625 		if (length + 4 > transferred) {
    626 			ERROR (abstract->context, "Invalid packet length (%u).", length);
    627 			return DC_STATUS_PROTOCOL;
    628 		}
    629 
    630 		// Append the payload data to the output buffer. If the output
    631 		// buffer is too small, the error is not reported immediately
    632 		// but delayed until all packets have been received.
    633 		if (nbytes < size) {
    634 			unsigned int n = length;
    635 			if (nbytes + n > size) {
    636 				n = size - nbytes;
    637 			}
    638 			memcpy (data + nbytes, buf + 4, n);
    639 		}
    640 		nbytes += length;
    641 		pkt_seq++;
    642 
    643 		// Last packet?
    644 		if ((status & 0x20) == 0)
    645 			break;
    646 	}
    647 
    648 	// Verify the expected number of bytes.
    649 	if (nbytes > size) {
    650 		ERROR (abstract->context, "Unexpected number of bytes received (%u %u).", nbytes, size);
    651 		return DC_STATUS_PROTOCOL;
    652 	}
    653 
    654 	if (actual) {
    655 		*actual = nbytes;
    656 	}
    657 
    658 	return DC_STATUS_SUCCESS;
    659 }
    660 
    661 static dc_status_t
    662 oceanic_atom2_packet (oceanic_atom2_device_t *device, const unsigned char command[], unsigned int csize, unsigned char ack, unsigned char answer[], unsigned int asize, unsigned int crc_size)
    663 {
    664 	dc_status_t status = DC_STATUS_SUCCESS;
    665 	dc_device_t *abstract = (dc_device_t *) device;
    666 	dc_transport_t transport = dc_iostream_get_transport (device->iostream);
    667 
    668 	if (asize > MAXPACKET) {
    669 		return DC_STATUS_INVALIDARGS;
    670 	}
    671 
    672 	if (crc_size > 2 || (crc_size != 0 && asize == 0)) {
    673 		return DC_STATUS_INVALIDARGS;
    674 	}
    675 
    676 	if (device_is_cancelled (abstract))
    677 		return DC_STATUS_CANCELLED;
    678 
    679 	if (device->delay) {
    680 		dc_iostream_sleep (device->iostream, device->delay);
    681 	}
    682 
    683 	// Send the command to the dive computer.
    684 	if (transport == DC_TRANSPORT_BLE) {
    685 		status = oceanic_atom2_ble_write (device, command, csize);
    686 	} else {
    687 		status = dc_iostream_write (device->iostream, command, csize, NULL);
    688 	}
    689 	if (status != DC_STATUS_SUCCESS) {
    690 		ERROR (abstract->context, "Failed to send the command.");
    691 		return status;
    692 	}
    693 
    694 	// Receive the answer of the dive computer.
    695 	unsigned char packet[1 + MAXPACKET + 2];
    696 	unsigned int nbytes = 1 + asize + crc_size;
    697 	if (transport == DC_TRANSPORT_BLE) {
    698 		// Accept excess bytes for some models.
    699 		if (asize && device->extra) {
    700 			nbytes = 1 + MAXPACKET + crc_size;
    701 		}
    702 		status = oceanic_atom2_ble_read (device, packet, nbytes, &nbytes);
    703 	} else {
    704 		status = dc_iostream_read (device->iostream, packet, 1 + asize + crc_size, NULL);
    705 	}
    706 	if (status != DC_STATUS_SUCCESS) {
    707 		ERROR (abstract->context, "Failed to receive the answer.");
    708 		return status;
    709 	}
    710 
    711 	// Verify the number of bytes.
    712 	if (nbytes < 1) {
    713 		ERROR (abstract->context, "Invalid packet size (%u).", nbytes);
    714 		return DC_STATUS_PROTOCOL;
    715 	}
    716 
    717 	// Verify the ACK byte of the answer.
    718 	if (packet[0] != ack) {
    719 		ERROR (abstract->context, "Unexpected answer start byte(s).");
    720 		if (packet[0] == (unsigned char) ~ack) {
    721 			return DC_STATUS_UNSUPPORTED;
    722 		} else {
    723 			return DC_STATUS_PROTOCOL;
    724 		}
    725 	}
    726 
    727 	// Verify the number of bytes.
    728 	if (nbytes < 1 + asize + crc_size) {
    729 		ERROR (abstract->context, "Unexpected number of bytes received (%u %u).", nbytes, 1 + asize + crc_size);
    730 		return DC_STATUS_PROTOCOL;
    731 	}
    732 
    733 	nbytes -= 1 + crc_size;
    734 
    735 	if (asize) {
    736 		// Verify the checksum of the answer.
    737 		unsigned short crc, ccrc;
    738 		if (crc_size == 2) {
    739 			crc = array_uint16_le (packet + 1 + nbytes);
    740 			ccrc = checksum_add_uint16 (packet + 1, nbytes, 0x0000);
    741 		} else {
    742 			crc = packet[1 + nbytes];
    743 			ccrc = checksum_add_uint8 (packet + 1, nbytes, 0x00);
    744 		}
    745 		if (crc != ccrc) {
    746 			ERROR (abstract->context, "Unexpected answer checksum.");
    747 			return DC_STATUS_PROTOCOL;
    748 		}
    749 
    750 		memcpy (answer, packet + 1, asize);
    751 	}
    752 
    753 	if (nbytes > asize) {
    754 		WARNING (abstract->context, "Ignored %u excess byte(s).", nbytes - asize);
    755 	}
    756 
    757 	device->sequence++;
    758 
    759 	return DC_STATUS_SUCCESS;
    760 }
    761 
    762 
    763 static dc_status_t
    764 oceanic_atom2_transfer (oceanic_atom2_device_t *device, const unsigned char command[], unsigned int csize, unsigned char ack, unsigned char answer[], unsigned int asize, unsigned int crc_size)
    765 {
    766 	// Send the command to the device. If the device responds with an
    767 	// ACK byte, the command was received successfully and the answer
    768 	// (if any) follows after the ACK byte. If the device responds with
    769 	// a NAK byte, we try to resend the command a number of times before
    770 	// returning an error.
    771 
    772 	unsigned int nretries = 0;
    773 	dc_status_t rc = DC_STATUS_SUCCESS;
    774 	while ((rc = oceanic_atom2_packet (device, command, csize, ack, answer, asize, crc_size)) != DC_STATUS_SUCCESS) {
    775 		if (rc != DC_STATUS_TIMEOUT && rc != DC_STATUS_PROTOCOL)
    776 			return rc;
    777 
    778 		// Abort if the maximum number of retries is reached.
    779 		if (nretries++ >= MAXRETRIES)
    780 			return rc;
    781 
    782 		// Increase the inter packet delay.
    783 		if (device->delay < MAXDELAY)
    784 			device->delay++;
    785 
    786 		// Delay the next attempt.
    787 		dc_iostream_sleep (device->iostream, 100);
    788 		dc_iostream_purge (device->iostream, DC_DIRECTION_INPUT);
    789 	}
    790 
    791 	return DC_STATUS_SUCCESS;
    792 }
    793 
    794 /*
    795  * The BLE communication sends a handshake packet that seems
    796  * to be a passphrase based on the BLE name of the device
    797  * (more specifically the serial number encoded in the name).
    798  *
    799  * The packet format is:
    800  *    0xe5
    801  *    < 8 bytes of passphrase >
    802  *    one-byte checksum of the passphrase.
    803  */
    804 static dc_status_t
    805 oceanic_atom2_ble_handshake(oceanic_atom2_device_t *device)
    806 {
    807 	dc_status_t rc = DC_STATUS_SUCCESS;
    808 	dc_device_t *abstract = (dc_device_t *) device;
    809 
    810 	// Retrieve the bluetooth device name.
    811 	// The format of the name is something like 'FQ001124', where the
    812 	// two first letters are the ASCII representation of the model
    813 	// number (e.g. 'FQ' or 0x4651 for the i770R), and the six digits
    814 	// are the serial number.
    815 	char name[8 + 1] = {0};
    816 	rc = dc_iostream_ioctl (device->iostream, DC_IOCTL_BLE_GET_NAME, name, sizeof(name));
    817 	if (rc != DC_STATUS_SUCCESS) {
    818 		if (rc == DC_STATUS_UNSUPPORTED) {
    819 			// Allow skipping the handshake if no name. But the download
    820 			// will likely fail.
    821 			WARNING (abstract->context, "Bluetooth device name unavailable.");
    822 			return DC_STATUS_SUCCESS;
    823 		} else {
    824 			return rc;
    825 		}
    826 	}
    827 
    828 	// Force a null terminated string.
    829 	name[sizeof(name) - 1] = 0;
    830 
    831 	// Check the minimum length.
    832 	if (strlen (name) < 8) {
    833 		ERROR (abstract->context, "Bluetooth device name too short.");
    834 		return DC_STATUS_IO;
    835 	}
    836 
    837 	// Turn ASCII numbers into just raw byte values.
    838 	unsigned char handshake[10] = {CMD_HANDSHAKE};
    839 	for (unsigned int i = 0; i < 6; i++) {
    840 		handshake[i + 1] = name[i + 2] - '0';
    841 	}
    842 
    843 	// Add simple checksum.
    844 	handshake[9] = checksum_add_uint8 (handshake + 1, 8, 0x00);
    845 
    846 	// Send the command to the dive computer.
    847 	rc = oceanic_atom2_transfer (device, handshake, sizeof(handshake), ACK, NULL, 0, 0);
    848 	if (rc != DC_STATUS_SUCCESS) {
    849 		if (rc == DC_STATUS_UNSUPPORTED) {
    850 			WARNING (abstract->context, "Bluetooth handshake not supported.");
    851 			return DC_STATUS_SUCCESS;
    852 		} else {
    853 			return rc;
    854 		}
    855 	}
    856 
    857 	return DC_STATUS_SUCCESS;
    858 }
    859 
    860 dc_status_t
    861 oceanic_atom2_device_open (dc_device_t **out, dc_context_t *context, dc_iostream_t *iostream, unsigned int model)
    862 {
    863 	dc_status_t status = DC_STATUS_SUCCESS;
    864 	oceanic_atom2_device_t *device = NULL;
    865 
    866 	if (out == NULL)
    867 		return DC_STATUS_INVALIDARGS;
    868 
    869 	// Allocate memory.
    870 	device = (oceanic_atom2_device_t *) dc_device_allocate (context, &oceanic_atom2_device_vtable.base);
    871 	if (device == NULL) {
    872 		ERROR (context, "Failed to allocate memory.");
    873 		return DC_STATUS_NOMEMORY;
    874 	}
    875 
    876 	// Initialize the base class.
    877 	oceanic_common_device_init (&device->base);
    878 
    879 	// Set the default values.
    880 	device->iostream = iostream;
    881 	device->delay = 0;
    882 	device->extra = model == PROPLUSX || model == I770R;
    883 	device->sequence = 0;
    884 	device->bigpage = 1; // no big pages
    885 	device->cached_page = INVALID;
    886 	device->cached_highmem = INVALID;
    887 	memset(device->cache, 0, sizeof(device->cache));
    888 
    889 	// Get the correct baudrate.
    890 	unsigned int baudrate = 38400;
    891 	if (model == VTX || model == I750TC || model == PROPLUSX || model == I770R) {
    892 		baudrate = 115200;
    893 	}
    894 
    895 	// Set the serial communication protocol (38400 8N1).
    896 	status = dc_iostream_configure (device->iostream, baudrate, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
    897 	if (status != DC_STATUS_SUCCESS) {
    898 		ERROR (context, "Failed to set the terminal attributes.");
    899 		goto error_free;
    900 	}
    901 
    902 	// Set the timeout for receiving data (1000 ms).
    903 	status = dc_iostream_set_timeout (device->iostream, 1000);
    904 	if (status != DC_STATUS_SUCCESS) {
    905 		ERROR (context, "Failed to set the timeout.");
    906 		goto error_free;
    907 	}
    908 
    909 	// Set the DTR line.
    910 	status = dc_iostream_set_dtr (device->iostream, 1);
    911 	if (status != DC_STATUS_SUCCESS) {
    912 		ERROR (context, "Failed to set the DTR line.");
    913 		goto error_free;
    914 	}
    915 
    916 	// Clear the RTS line to reset the PIC inside the data cable as it
    917 	// may not have have been previously cleared. This ensures that the
    918 	// PIC will always start in a known state once RTS is set. Starting
    919 	// in a known default state is very important as the PIC won't
    920 	// respond to init commands unless it is in a default state.
    921 	status = dc_iostream_set_rts (device->iostream, 0);
    922 	if (status != DC_STATUS_SUCCESS) {
    923 		ERROR (context, "Failed to clear the RTS line.");
    924 		goto error_free;
    925 	}
    926 
    927 	// Hold RTS clear for a bit to allow PIC to reset.
    928 	dc_iostream_sleep (device->iostream, 100);
    929 
    930 	// Set the RTS line.
    931 	status = dc_iostream_set_rts (device->iostream, 1);
    932 	if (status != DC_STATUS_SUCCESS) {
    933 		ERROR (context, "Failed to set the RTS line.");
    934 		goto error_free;
    935 	}
    936 
    937 	// Give the interface 100 ms to settle and draw power up.
    938 	dc_iostream_sleep (device->iostream, 100);
    939 
    940 	// Make sure everything is in a sane state.
    941 	dc_iostream_purge (device->iostream, DC_DIRECTION_ALL);
    942 
    943 	// Switch the device from surface mode into download mode. Before sending
    944 	// this command, the device needs to be in PC mode (automatically activated
    945 	// by connecting the device), or already in download mode.
    946 	status = oceanic_atom2_device_version ((dc_device_t *) device, device->base.version, sizeof (device->base.version));
    947 	if (status != DC_STATUS_SUCCESS) {
    948 		goto error_free;
    949 	}
    950 
    951 	HEXDUMP (context, DC_LOGLEVEL_DEBUG, "Version", device->base.version, sizeof (device->base.version));
    952 
    953 	if (dc_iostream_get_transport (device->iostream) == DC_TRANSPORT_BLE) {
    954 		status = oceanic_atom2_ble_handshake(device);
    955 		if (status != DC_STATUS_SUCCESS) {
    956 			goto error_free;
    957 		}
    958 	}
    959 
    960 	// Detect the memory layout.
    961 	const oceanic_common_version_t *version = OCEANIC_COMMON_MATCH(device->base.version, versions, &device->base.firmware);
    962 	if (version == NULL) {
    963 		WARNING (context, "Unsupported device detected (%s)!", device->base.version);
    964 		if (memcmp(device->base.version + 12, "256K", 4) == 0) {
    965 			device->base.layout = &oceanic_atom1_layout;
    966 		} else if (memcmp(device->base.version + 12, "512K", 4) == 0) {
    967 			device->base.layout = &oceanic_default_layout;
    968 		} else if (memcmp(device->base.version + 12, "1024", 4) == 0) {
    969 			device->base.layout = &oceanic_oc1_layout;
    970 		} else if (memcmp(device->base.version + 12, "2048", 4) == 0) {
    971 			device->base.layout = &hollis_tx1_layout;
    972 		} else {
    973 			device->base.layout = &oceanic_default_layout;
    974 		}
    975 		device->base.model = 0;
    976 	} else {
    977 		device->base.layout = version->layout;
    978 		device->base.model = version->model;
    979 	}
    980 
    981 	// Set the big page support.
    982 	if (device->base.layout == &aeris_f11_layout ||
    983 		device->base.layout == &oceanic_proplus3_layout) {
    984 		device->bigpage = 8;
    985 	} else if (device->base.layout == &oceanic_proplusx_layout ||
    986 		device->base.layout == &aqualung_i770r_layout ||
    987 		device->base.layout == &aeris_a300cs_layout) {
    988 		device->bigpage = 16;
    989 	}
    990 
    991 	// Repeat the handshaking every few packets.
    992 	device->handshake_repeat = dc_iostream_get_transport (device->iostream) == DC_TRANSPORT_BLE &&
    993 		device->base.model == PROPLUS4;
    994 	device->handshake_counter = 0;
    995 
    996 	*out = (dc_device_t*) device;
    997 
    998 	return DC_STATUS_SUCCESS;
    999 
   1000 error_free:
   1001 	dc_device_deallocate ((dc_device_t *) device);
   1002 	return status;
   1003 }
   1004 
   1005 
   1006 static dc_status_t
   1007 oceanic_atom2_device_close (dc_device_t *abstract)
   1008 {
   1009 	dc_status_t status = DC_STATUS_SUCCESS;
   1010 	oceanic_atom2_device_t *device = (oceanic_atom2_device_t*) abstract;
   1011 	dc_status_t rc = DC_STATUS_SUCCESS;
   1012 
   1013 	// Send the quit command.
   1014 	unsigned char command[4] = {CMD_QUIT, 0x05, 0xA5};
   1015 	rc = oceanic_atom2_transfer (device, command, sizeof (command), NAK, NULL, 0, 0);
   1016 	if (rc != DC_STATUS_SUCCESS) {
   1017 		dc_status_set_error(&status, rc);
   1018 	}
   1019 
   1020 	return status;
   1021 }
   1022 
   1023 
   1024 dc_status_t
   1025 oceanic_atom2_device_keepalive (dc_device_t *abstract)
   1026 {
   1027 	oceanic_atom2_device_t *device = (oceanic_atom2_device_t*) abstract;
   1028 
   1029 	if (!ISINSTANCE (abstract))
   1030 		return DC_STATUS_INVALIDARGS;
   1031 
   1032 	// Send the command to the dive computer.
   1033 	unsigned char command[] = {CMD_KEEPALIVE, 0x05, 0xA5};
   1034 	dc_status_t rc = oceanic_atom2_transfer (device, command, sizeof (command), ACK, NULL, 0, 0);
   1035 	if (rc != DC_STATUS_SUCCESS)
   1036 		return rc;
   1037 
   1038 	return DC_STATUS_SUCCESS;
   1039 }
   1040 
   1041 
   1042 dc_status_t
   1043 oceanic_atom2_device_version (dc_device_t *abstract, unsigned char data[], unsigned int size)
   1044 {
   1045 	oceanic_atom2_device_t *device = (oceanic_atom2_device_t*) abstract;
   1046 
   1047 	if (!ISINSTANCE (abstract))
   1048 		return DC_STATUS_INVALIDARGS;
   1049 
   1050 	if (size < PAGESIZE)
   1051 		return DC_STATUS_INVALIDARGS;
   1052 
   1053 	unsigned char command[] = {CMD_VERSION};
   1054 	dc_status_t rc = oceanic_atom2_transfer (device, command, sizeof (command), ACK, data, PAGESIZE, 1);
   1055 	if (rc != DC_STATUS_SUCCESS)
   1056 		return rc;
   1057 
   1058 	return DC_STATUS_SUCCESS;
   1059 }
   1060 
   1061 
   1062 static dc_status_t
   1063 oceanic_atom2_device_read (dc_device_t *abstract, unsigned int address, unsigned char data[], unsigned int size)
   1064 {
   1065 	oceanic_atom2_device_t *device = (oceanic_atom2_device_t*) abstract;
   1066 	const oceanic_common_layout_t *layout = device->base.layout;
   1067 
   1068 	if ((address % PAGESIZE != 0) ||
   1069 		(size    % PAGESIZE != 0))
   1070 		return DC_STATUS_INVALIDARGS;
   1071 
   1072 	// Pick the correct read command and number of checksum bytes.
   1073 	unsigned char read_cmd = 0x00;
   1074 	unsigned int crc_size = 0;
   1075 	switch (device->bigpage) {
   1076 	case 1:
   1077 		read_cmd = CMD_READ1;
   1078 		crc_size = 1;
   1079 		break;
   1080 	case 8:
   1081 		read_cmd = CMD_READ8;
   1082 		crc_size = device->base.model == PROPLUS4 ? 2 : 1;
   1083 		break;
   1084 	case 16:
   1085 		read_cmd = CMD_READ16;
   1086 		crc_size = 2;
   1087 		break;
   1088 	default:
   1089 		return DC_STATUS_INVALIDARGS;
   1090 	}
   1091 
   1092 	// Pick the best pagesize to use.
   1093 	unsigned int pagesize = device->bigpage * PAGESIZE;
   1094 
   1095 	// High memory state.
   1096 	unsigned int highmem = 0;
   1097 
   1098 	unsigned int nbytes = 0;
   1099 	while (nbytes < size) {
   1100 		// Switch to the correct read command when entering the high memory area.
   1101 		if (layout->highmem && address >= layout->highmem && !highmem) {
   1102 			highmem = layout->highmem;
   1103 			read_cmd = CMD_READ16HI;
   1104 			crc_size = 2;
   1105 			pagesize = 16 * PAGESIZE;
   1106 		}
   1107 
   1108 		// Calculate the page number after mapping the virtual high memory
   1109 		// addresses back to their physical address.
   1110 		unsigned int page = (address - highmem) / pagesize;
   1111 
   1112 		if (page != device->cached_page || highmem != device->cached_highmem) {
   1113 			if (device->handshake_repeat && ++device->handshake_counter % REPEAT == 0) {
   1114 				unsigned char version[PAGESIZE] = {0};
   1115 				oceanic_atom2_device_version (abstract, version, sizeof (version));
   1116 				oceanic_atom2_ble_handshake (device);
   1117 			}
   1118 
   1119 			// Read the package.
   1120 			unsigned int number = highmem ? page : page * device->bigpage; // This is always PAGESIZE, even in big page mode.
   1121 			unsigned char command[] = {read_cmd,
   1122 					(number >> 8) & 0xFF, // high
   1123 					(number     ) & 0xFF, // low
   1124 				};
   1125 			dc_status_t rc = oceanic_atom2_transfer (device, command, sizeof (command), ACK, device->cache, pagesize, crc_size);
   1126 			if (rc != DC_STATUS_SUCCESS)
   1127 				return rc;
   1128 
   1129 			// Cache the page.
   1130 			device->cached_page = page;
   1131 			device->cached_highmem = highmem;
   1132 		}
   1133 
   1134 		unsigned int offset = address % pagesize;
   1135 		unsigned int length = pagesize - offset;
   1136 		if (nbytes + length > size)
   1137 			length = size - nbytes;
   1138 
   1139 		memcpy (data, device->cache + offset, length);
   1140 
   1141 		nbytes += length;
   1142 		address += length;
   1143 		data += length;
   1144 	}
   1145 
   1146 	return DC_STATUS_SUCCESS;
   1147 }
   1148 
   1149 
   1150 static dc_status_t
   1151 oceanic_atom2_device_write (dc_device_t *abstract, unsigned int address, const unsigned char data[], unsigned int size)
   1152 {
   1153 	oceanic_atom2_device_t *device = (oceanic_atom2_device_t*) abstract;
   1154 
   1155 	if ((address % PAGESIZE != 0) ||
   1156 		(size    % PAGESIZE != 0))
   1157 		return DC_STATUS_INVALIDARGS;
   1158 
   1159 	// Invalidate the cache.
   1160 	device->cached_page = INVALID;
   1161 	device->cached_highmem = INVALID;
   1162 
   1163 	unsigned int nbytes = 0;
   1164 	while (nbytes < size) {
   1165 		// Prepare to write the package.
   1166 		unsigned int number = address / PAGESIZE;
   1167 		unsigned char prepare[] = {CMD_WRITE,
   1168 				(number >> 8) & 0xFF, // high
   1169 				(number     ) & 0xFF, // low
   1170 			};
   1171 		dc_status_t rc = oceanic_atom2_transfer (device, prepare, sizeof (prepare), ACK, NULL, 0, 0);
   1172 		if (rc != DC_STATUS_SUCCESS)
   1173 			return rc;
   1174 
   1175 		// Write the package.
   1176 		unsigned char command[PAGESIZE + 1] = {0};
   1177 		memcpy (command, data, PAGESIZE);
   1178 		command[PAGESIZE] = checksum_add_uint8 (command, PAGESIZE, 0x00);
   1179 		rc = oceanic_atom2_transfer (device, command, sizeof (command), ACK, NULL, 0, 0);
   1180 		if (rc != DC_STATUS_SUCCESS)
   1181 			return rc;
   1182 
   1183 		nbytes += PAGESIZE;
   1184 		address += PAGESIZE;
   1185 		data += PAGESIZE;
   1186 	}
   1187 
   1188 	return DC_STATUS_SUCCESS;
   1189 }