Changeset d01b7bf in subsurface


Ignore:
Timestamp:
Jun 27, 2017, 1:58:15 PM (4 weeks ago)
Author:
Linus Torvalds <torvalds@…>
Branches:
master
Children:
5265956
Parents:
e79bede
git-author:
Linus Torvalds <torvalds@…> (06/27/17 11:59:11)
git-committer:
Linus Torvalds <torvalds@…> (06/27/17 13:58:15)
Message:

Switch over to SSRF_CUSTOM_IO v2

I hate changing the IO interfaces this often, but when I converted the
custom serial interface to the more generic custom IO interface, I
intentionally left the legacy serial operations alone, because I didn't
want to change something I didn't care about.

But it turns out that leaving them with the old calling convention
caused extra problems when converting the bluetooth serial code to have
the BLE GATT packet fall-back, which requires mixing two kinds of
operations.

Also, the packet_open() routine was passed a copy of the 'dc_context_t',
which makes it possible to update the 'dc_custom_io_t' field on the fly
at open time. That makes a lot of chaining operations much simpler,
since now you can chain the 'custom_io_t' at open time and then
libdivecomputer will automatically call the new routines instead of the
old ones.

That dc_context_t availability gets rid of all the

if (device && device->ops)

return device->ops->serial_xyz(..);

hackery inside the rfcomm routines - now we can just at open time do a simple

dc_context_set_custom_io(context, &ble_serial_ops);

to switch things over to the BLE version of the serial code instead.

Finally, SSRF_CUSTOM_IO v2 added an opaque "dc_user_device_t" pointer
argument to the custom_io descriptor, which gets filled in as the
custom_io is registered with the download context. Note that unlike
most opaque pointers, this one is opaque to *libdivecomputer*, and the
type is supposed to be supplied by the user.

We define the "dc_user_device_t" as our old "struct device_data_t",
making it "struct user_device_t" instead. That means that the IO
routines now get passed the device info showing what device they are
supposed to download for.

That, in turn, means that now our BLE GATT open code can take the device
type it opens for into account if it wants to. And it will want to,
since the rules for Shearwater are different from the rules for Suunto,
for example.

NOTE! Because of the interface change with libdivecomputer, this will
need a flag-day again where libdivecomputer and subsurface are updated
together. It may not be the last time, either.

Signed-off-by: Linus Torvalds <torvalds@…>

Location:
core
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • core/configuredivecomputer.cpp

    radd253c rd01b7bf  
    636636        if (data->bluetooth_mode) {
    637637#if defined(BT_SUPPORT) && defined(SSRF_CUSTOM_IO)
    638                 rc = dc_context_set_custom_io(data->context, get_qt_serial_ops());
     638                rc = dc_context_set_custom_io(data->context, get_qt_serial_ops(), data);
    639639#endif
    640640#ifdef SERIAL_FTDI
    641641        } else if (!strcmp(data->devname, "ftdi")) {
    642                 rc = dc_context_set_custom_io(data->context, &serial_ftdi_ops);
     642                rc = dc_context_set_custom_io(data->context, &serial_ftdi_ops, data);
    643643#endif
    644644        }
  • core/libdivecomputer.c

    r63a4a75 rd01b7bf  
    606606#endif
    607607
    608 static dc_status_t libdc_header_parser(dc_parser_t *parser, struct device_data_t *devdata, struct dive *dive)
     608static dc_status_t libdc_header_parser(dc_parser_t *parser, dc_user_device_t *devdata, struct dive *dive)
    609609{
    610610        dc_status_t rc = 0;
     
    10921092        if (data->bluetooth_mode) {
    10931093#if defined(BT_SUPPORT) && defined(SSRF_CUSTOM_IO)
    1094                 rc = dc_context_set_custom_io(data->context, get_qt_serial_ops());
     1094                rc = dc_context_set_custom_io(data->context, get_qt_serial_ops(), data);
    10951095#endif
    10961096#ifdef SERIAL_FTDI
    10971097        } else if (!strcmp(data->devname, "ftdi")) {
    1098                 rc = dc_context_set_custom_io(data->context, &serial_ftdi_ops);
     1098                rc = dc_context_set_custom_io(data->context, &serial_ftdi_ops, data);
    10991099                INFO(0, "setting up ftdi ops");
    11001100#else
  • core/libdivecomputer.h

    radd253c rd01b7bf  
    2424/* don't forget to include the UI toolkit specific display-XXX.h first
    2525   to get the definition of progressbar_t */
    26 typedef struct device_data_t
     26typedef struct dc_user_device_t
    2727{
    2828        dc_descriptor_t *descriptor;
  • core/qtserialbluetooth.cpp

    r4f3a9cdb rd01b7bf  
    99
    1010#include <libdivecomputer/version.h>
     11#include <libdivecomputer/context.h>
    1112
    1213#if defined(SSRF_CUSTOM_IO)
     
    3839extern "C" {
    3940typedef struct qt_serial_t {
    40         dc_custom_io_t *ops;
    4141        /*
    4242         * RFCOMM socket used for Bluetooth Serial communication.
     
    5252#ifdef BLE_SUPPORT
    5353
    54 static dc_status_t ble_serial_open(void **userdata, const char* devaddr);
    55 static dc_status_t ble_serial_close(void **userdata);
    56 static dc_status_t ble_serial_read(void **userdata, void* data, size_t size, size_t *actual);
    57 static dc_status_t ble_serial_write(void **userdata, const void* data, size_t size, size_t *actual);
    58 static dc_status_t ble_serial_purge(void **userdata, dc_direction_t queue);
    59 static dc_status_t ble_serial_get_available(void **userdata, size_t *available);
     54static dc_status_t ble_serial_open(dc_custom_io_t *io, dc_context_t *, const char* devaddr);
     55static dc_status_t ble_serial_close(dc_custom_io_t *io);
     56static dc_status_t ble_serial_read(dc_custom_io_t *io, void* data, size_t size, size_t *actual);
     57static dc_status_t ble_serial_write(dc_custom_io_t *io, const void* data, size_t size, size_t *actual);
     58static dc_status_t ble_serial_purge(dc_custom_io_t *io, dc_direction_t queue);
     59static dc_status_t ble_serial_get_available(dc_custom_io_t *io, size_t *available);
     60static dc_status_t ble_serial_set_timeout(dc_custom_io_t *io, long timeout);
    6061
    6162static dc_custom_io_t ble_serial_ops = {
    6263        .userdata = NULL,
     64        .user_device = NULL,
    6365
    6466        .serial_open = ble_serial_open,
     
    6870        .serial_purge = ble_serial_purge,
    6971        .serial_get_available = ble_serial_get_available,
    70         .serial_set_timeout = NULL,     // the regular qt_set_timeout is fine
     72        .serial_set_timeout = ble_serial_set_timeout,
    7173// These doesn't make sense over bluetooth
    7274// NULL means NOP
     
    8486};
    8587
    86 static struct qt_serial_t serial_over_ble = {
    87         .ops = &ble_serial_ops,
    88 };
    89 
    90 
    91 static dc_status_t ble_serial_open(void **userdata, const char* devaddr)
    92 {
    93         *userdata = &serial_over_ble;
    94         return qt_ble_open(&ble_serial_ops, NULL, devaddr);
     88
     89static dc_status_t ble_serial_open(dc_custom_io_t *io, dc_context_t *context, const char* devaddr)
     90{
     91        dc_context_set_custom_io(context, &ble_serial_ops, io->user_device);
     92        return qt_ble_open(&ble_serial_ops, context, devaddr);
    9593}
    9694
     
    118116}
    119117
    120 static dc_status_t ble_serial_close(void **userdata)
     118static dc_status_t ble_serial_close(dc_custom_io_t *io)
    121119{
    122120        ble_serial_flush_write();
    123         *userdata = NULL;
     121        io->userdata = NULL;
    124122        return qt_ble_close(&ble_serial_ops);
    125123}
    126124
    127 static dc_status_t ble_serial_read(void **userdata, void* data, size_t size, size_t *actual)
     125static dc_status_t ble_serial_read(dc_custom_io_t *io, void* data, size_t size, size_t *actual)
    128126{
    129127        int len;
     
    154152}
    155153
    156 static dc_status_t ble_serial_write(void **userdata, const void* data, size_t size, size_t *actual)
     154static dc_status_t ble_serial_write(dc_custom_io_t *io, const void* data, size_t size, size_t *actual)
    157155{
    158156        dc_status_t rc = DC_STATUS_SUCCESS;
     
    182180}
    183181
    184 static dc_status_t ble_serial_purge(void **userdata, dc_direction_t queue)
     182static dc_status_t ble_serial_purge(dc_custom_io_t *io, dc_direction_t queue)
    185183{
    186184        /* Do we care? */
     
    188186}
    189187
    190 static dc_status_t ble_serial_get_available(void **userdata, size_t *available)
     188static dc_status_t ble_serial_get_available(dc_custom_io_t *io, size_t *available)
    191189{
    192190        *available = buffer.in_bytes - buffer.in_pos;
     
    194192}
    195193
    196 #endif
    197 
    198 
    199 
    200 static dc_status_t qt_serial_open(void **userdata, const char* devaddr)
     194static dc_status_t ble_serial_set_timeout(dc_custom_io_t *io, long timeout)
     195{
     196        /* Do we care? */
     197        return DC_STATUS_SUCCESS;
     198}
     199
     200#endif
     201
     202
     203
     204static dc_status_t qt_serial_open(dc_custom_io_t *io, dc_context_t *context, const char* devaddr)
    201205{
    202206#ifdef BLE_SUPPORT
    203207        if (!strncmp(devaddr, "LE:", 3))
    204                 return ble_serial_open(userdata, devaddr);
     208                return ble_serial_open(io, context, devaddr);
    205209#endif
    206210
     
    210214                return DC_STATUS_NOMEMORY;
    211215        }
    212 
    213         serial_port->ops = NULL;
    214216
    215217        // Default to blocking reads.
     
    349351        }
    350352#endif
    351         *userdata = serial_port;
    352 
    353         return DC_STATUS_SUCCESS;
    354 }
    355 
    356 static dc_status_t qt_serial_close(void **userdata)
    357 {
    358         qt_serial_t *device = (qt_serial_t*) *userdata;
     353        io->userdata = serial_port;
     354
     355        return DC_STATUS_SUCCESS;
     356}
     357
     358static dc_status_t qt_serial_close(dc_custom_io_t *io)
     359{
     360        qt_serial_t *device = (qt_serial_t*) io->userdata;
    359361
    360362        if (device == NULL)
    361363                return DC_STATUS_SUCCESS;
    362 
    363         if (device && device->ops)
    364                 return device->ops->serial_close(userdata);
    365364
    366365#if defined(Q_OS_WIN)
     
    380379#endif
    381380
    382         *userdata = NULL;
    383 
    384         return DC_STATUS_SUCCESS;
    385 }
    386 
    387 static dc_status_t qt_serial_read(void **userdata, void* data, size_t size, size_t *actual)
    388 {
    389         qt_serial_t *device = (qt_serial_t*) *userdata;
    390 
    391         if (device && device->ops)
    392                 return device->ops->serial_read(userdata, data, size, actual);
     381        io->userdata = NULL;
     382
     383        return DC_STATUS_SUCCESS;
     384}
     385
     386static dc_status_t qt_serial_read(dc_custom_io_t *io, void* data, size_t size, size_t *actual)
     387{
     388        qt_serial_t *device = (qt_serial_t*) io->userdata;
    393389
    394390#if defined(Q_OS_WIN)
     
    449445}
    450446
    451 static dc_status_t qt_serial_write(void **userdata, const void* data, size_t size, size_t *actual)
    452 {
    453         qt_serial_t *device = (qt_serial_t*) *userdata;
    454 
    455         if (device && device->ops)
    456                 return device->ops->serial_write(userdata, data, size, actual);
     447static dc_status_t qt_serial_write(dc_custom_io_t *io, const void* data, size_t size, size_t *actual)
     448{
     449        qt_serial_t *device = (qt_serial_t*) io->userdata;
    457450
    458451#if defined(Q_OS_WIN)
     
    501494}
    502495
    503 static dc_status_t qt_serial_purge(void **userdata, dc_direction_t queue)
    504 {
    505         qt_serial_t *device = (qt_serial_t*) *userdata;
    506 
    507         if (device && device->ops)
    508                 return device->ops->serial_purge(userdata, queue);
     496static dc_status_t qt_serial_purge(dc_custom_io_t *io, dc_direction_t queue)
     497{
     498        qt_serial_t *device = (qt_serial_t*) io->userdata;
    509499
    510500        (void)queue;
     
    520510}
    521511
    522 static dc_status_t qt_serial_get_available(void **userdata, size_t *available)
    523 {
    524         qt_serial_t *device = (qt_serial_t*) *userdata;
    525 
    526         if (device && device->ops)
    527                 return device->ops->serial_get_available(userdata, available);
     512static dc_status_t qt_serial_get_available(dc_custom_io_t *io, size_t *available)
     513{
     514        qt_serial_t *device = (qt_serial_t*) io->userdata;
    528515
    529516#if defined(Q_OS_WIN)
     
    564551}
    565552
    566 static dc_status_t qt_serial_set_timeout(void **userdata, long timeout)
    567 {
    568         qt_serial_t *device = (qt_serial_t*) *userdata;
     553static dc_status_t qt_serial_set_timeout(dc_custom_io_t *io, long timeout)
     554{
     555        qt_serial_t *device = (qt_serial_t*) io->userdata;
    569556
    570557        if (device == NULL)
     
    578565dc_custom_io_t qt_serial_ops = {
    579566        .userdata = NULL,
     567        .user_device = NULL,
    580568        .serial_open = qt_serial_open,
    581569        .serial_close = qt_serial_close,
  • core/serial_ftdi.c

    radd253c rd01b7bf  
    7272} ftdi_serial_t;
    7373
    74 static dc_status_t serial_ftdi_get_received (void **userdata, size_t *value)
    75 {
    76         ftdi_serial_t *device = (ftdi_serial_t*) *userdata;
     74static dc_status_t serial_ftdi_get_received (dc_custom_io_t *io, size_t *value)
     75{
     76        ftdi_serial_t *device = (ftdi_serial_t*) io->userdata;
    7777
    7878        if (device == NULL)
     
    146146// Open the serial port.
    147147// Initialise ftdi_context and use it to open the device
    148 static dc_status_t serial_ftdi_open (void **userdata, const char* name)
     148static dc_status_t serial_ftdi_open (dc_custom_io_t *io, dc_context_t *context, const char* name)
    149149{
    150150        INFO(0, "serial_ftdi_open called");
     
    204204        device->ftdi_ctx = ftdi_ctx;
    205205
    206         *userdata = device;
     206        io->userdata = device;
    207207
    208208        return DC_STATUS_SUCCESS;
     
    212212// Close the serial port.
    213213//
    214 static dc_status_t serial_ftdi_close (void **userdata)
    215 {
    216         ftdi_serial_t *device = (ftdi_serial_t*) *userdata;
     214static dc_status_t serial_ftdi_close (dc_custom_io_t *io)
     215{
     216        ftdi_serial_t *device = (ftdi_serial_t*) io->userdata;
    217217
    218218        if (device == NULL)
     
    234234        free (device);
    235235
    236         *userdata = NULL;
     236        io->userdata = NULL;
    237237
    238238        return DC_STATUS_SUCCESS;
     
    242242// Configure the serial port (baudrate, databits, parity, stopbits and flowcontrol).
    243243//
    244 static dc_status_t serial_ftdi_configure (void **userdata, unsigned int baudrate, unsigned int databits, dc_parity_t parity, dc_stopbits_t stopbits, dc_flowcontrol_t flowcontrol)
    245 {
    246         ftdi_serial_t *device = (ftdi_serial_t*) *userdata;
     244static dc_status_t serial_ftdi_configure (dc_custom_io_t *io, unsigned int baudrate, unsigned int databits, dc_parity_t parity, dc_stopbits_t stopbits, dc_flowcontrol_t flowcontrol)
     245{
     246        ftdi_serial_t *device = (ftdi_serial_t*) io->userdata;
    247247
    248248        if (device == NULL)
     
    342342// Configure the serial port (timeouts).
    343343//
    344 static dc_status_t serial_ftdi_set_timeout (void **userdata, long timeout)
    345 {
    346         ftdi_serial_t *device = (ftdi_serial_t*) *userdata;
     344static dc_status_t serial_ftdi_set_timeout (dc_custom_io_t *io, long timeout)
     345{
     346        ftdi_serial_t *device = (ftdi_serial_t*) io->userdata;
    347347
    348348        if (device == NULL)
     
    356356}
    357357
    358 static dc_status_t serial_ftdi_set_halfduplex (void **userdata, unsigned int value)
    359 {
    360         ftdi_serial_t *device = (ftdi_serial_t*) *userdata;
     358static dc_status_t serial_ftdi_set_halfduplex (dc_custom_io_t *io, unsigned int value)
     359{
     360        ftdi_serial_t *device = (ftdi_serial_t*) io->userdata;
    361361
    362362        if (device == NULL)
     
    371371}
    372372
    373 static dc_status_t serial_ftdi_read (void **userdata, void *data, size_t size, size_t *actual)
    374 {
    375         ftdi_serial_t *device = (ftdi_serial_t*) *userdata;
     373static dc_status_t serial_ftdi_read (dc_custom_io_t *io, void *data, size_t size, size_t *actual)
     374{
     375        ftdi_serial_t *device = (ftdi_serial_t*) io->userdata;
    376376
    377377        if (device == NULL)
     
    422422}
    423423
    424 static dc_status_t serial_ftdi_write (void **userdata, const void *data, size_t size, size_t *actual)
    425 {
    426         ftdi_serial_t *device = (ftdi_serial_t*) *userdata;
     424static dc_status_t serial_ftdi_write (dc_custom_io_t *io, const void *data, size_t size, size_t *actual)
     425{
     426        ftdi_serial_t *device = (ftdi_serial_t*) io->userdata;
    427427
    428428        if (device == NULL)
     
    489489}
    490490
    491 static dc_status_t serial_ftdi_flush (void **userdata, dc_direction_t queue)
    492 {
    493         ftdi_serial_t *device = (ftdi_serial_t*) *userdata;
     491static dc_status_t serial_ftdi_flush (dc_custom_io_t *io, dc_direction_t queue)
     492{
     493        ftdi_serial_t *device = (ftdi_serial_t*) io->userdata;
    494494
    495495        if (device == NULL)
     
    526526}
    527527
    528 static dc_status_t serial_ftdi_send_break (void **userdata)
    529 {
    530         ftdi_serial_t *device = (ftdi_serial_t*) *userdata;
     528static dc_status_t serial_ftdi_send_break (dc_custom_io_t *io)
     529{
     530        ftdi_serial_t *device = (ftdi_serial_t*) io->userdata;
    531531
    532532        if (device == NULL)
     
    543543}
    544544
    545 static dc_status_t serial_ftdi_set_break (void **userdata, int level)
    546 {
    547         ftdi_serial_t *device = (ftdi_serial_t*) *userdata;
     545static dc_status_t serial_ftdi_set_break (dc_custom_io_t *io, int level)
     546{
     547        ftdi_serial_t *device = (ftdi_serial_t*) io->userdata;
    548548
    549549        if (device == NULL)
     
    557557}
    558558
    559 static dc_status_t serial_ftdi_set_dtr (void **userdata, int level)
    560 {
    561         ftdi_serial_t *device = (ftdi_serial_t*) *userdata;
     559static dc_status_t serial_ftdi_set_dtr (dc_custom_io_t *io, int level)
     560{
     561        ftdi_serial_t *device = (ftdi_serial_t*) io->userdata;
    562562
    563563        if (device == NULL)
     
    574574}
    575575
    576 static dc_status_t serial_ftdi_set_rts (void **userdata, int level)
    577 {
    578         ftdi_serial_t *device = (ftdi_serial_t*) *userdata;
     576static dc_status_t serial_ftdi_set_rts (dc_custom_io_t *io, int level)
     577{
     578        ftdi_serial_t *device = (ftdi_serial_t*) io->userdata;
    579579
    580580        if (device == NULL)
  • core/uemis-downloader.c

    rdec47e1 rd01b7bf  
    11211121}
    11221122
    1123 static bool get_matching_dive(int idx, char *newmax, int *uemis_mem_status, struct device_data_t *data, const char *mountpath, const char deviceidnr)
     1123static bool get_matching_dive(int idx, char *newmax, int *uemis_mem_status, dc_user_device_t *data, const char *mountpath, const char deviceidnr)
    11241124{
    11251125        struct dive *dive = data->download_table->dives[idx];
Note: See TracChangeset for help on using the changeset viewer.