diff options
-rw-r--r-- | core/qt-ble.cpp | 38 | ||||
-rw-r--r-- | core/qt-ble.h | 11 | ||||
-rw-r--r-- | core/qtserialbluetooth.cpp | 35 | ||||
m--------- | libdivecomputer | 0 |
4 files changed, 74 insertions, 10 deletions
diff --git a/core/qt-ble.cpp b/core/qt-ble.cpp index 88d7c8a4a..1b157b8ef 100644 --- a/core/qt-ble.cpp +++ b/core/qt-ble.cpp @@ -14,6 +14,7 @@ #include <QLoggingCategory> #include <libdivecomputer/version.h> +#include <libdivecomputer/ble.h> #include "libdivecomputer.h" #include "core/qt-ble.h" @@ -198,10 +199,8 @@ dc_status_t BLEObject::write(const void *data, size_t size, size_t *actual) return DC_STATUS_IO; } -dc_status_t BLEObject::read(void *data, size_t size, size_t *actual) +dc_status_t BLEObject::poll(int timeout) { - if (actual) - *actual = 0; if (receivedPackets.isEmpty()) { QList<QLowEnergyCharacteristic> list = preferredService()->characteristics(); if (list.isEmpty()) @@ -215,6 +214,21 @@ dc_status_t BLEObject::read(void *data, size_t size, size_t *actual) return DC_STATUS_TIMEOUT; } + return DC_STATUS_SUCCESS; +} + +dc_status_t BLEObject::read(void *data, size_t size, size_t *actual) +{ + dc_status_t rc; + + if (actual) + *actual = 0; + + // Wait for a packet + rc = poll(timeout); + if (rc != DC_STATUS_SUCCESS) + return rc; + QByteArray packet = receivedPackets.takeFirst(); // Did we get more than asked for? @@ -549,10 +563,24 @@ dc_status_t qt_ble_write(void *io, const void* data, size_t size, size_t *actual return ble->write(data, size, actual); } -const char *qt_ble_get_name(void *io) +dc_status_t qt_ble_poll(void *io, int timeout) { BLEObject *ble = (BLEObject *) io; - return ble->get_name(); + + return ble->poll(timeout); } +dc_status_t qt_ble_ioctl(void *io, unsigned int request, void *data, size_t size) +{ + BLEObject *ble = (BLEObject *) io; + + switch (request) { + case DC_IOCTL_BLE_GET_NAME: + return ble->get_name((char *) data, size); + default: + return DC_STATUS_UNSUPPORTED; + } +} + + } /* extern "C" */ diff --git a/core/qt-ble.h b/core/qt-ble.h index 2b011163f..4da51a981 100644 --- a/core/qt-ble.h +++ b/core/qt-ble.h @@ -23,7 +23,13 @@ public: inline void set_timeout(int value) { timeout = value; } dc_status_t write(const void* data, size_t size, size_t *actual); dc_status_t read(void* data, size_t size, size_t *actual); - inline const char *get_name() { return device->btname; } + inline dc_status_t get_name(char *res, size_t size) + { + if (!device->btname) return DC_STATUS_UNSUPPORTED; + strncpy(res, device->btname, size); + return DC_STATUS_SUCCESS; + } + dc_status_t poll(int timeout); inline QLowEnergyService *preferredService() { return preferred; } inline int descriptorWritten() { return desc_written; } @@ -61,10 +67,11 @@ private: extern "C" { dc_status_t qt_ble_open(void **io, dc_context_t *context, const char *devaddr, dc_user_device_t *user_device); dc_status_t qt_ble_set_timeout(void *io, int timeout); +dc_status_t qt_ble_poll(void *io, int timeout); dc_status_t qt_ble_read(void *io, void* data, size_t size, size_t *actual); dc_status_t qt_ble_write(void *io, const void* data, size_t size, size_t *actual); +dc_status_t qt_ble_ioctl(void *io, unsigned int request, void *data, size_t size); dc_status_t qt_ble_close(void *io); -const char *qt_ble_get_name(void *io); } #endif diff --git a/core/qtserialbluetooth.cpp b/core/qtserialbluetooth.cpp index ae4069f33..abb69284d 100644 --- a/core/qtserialbluetooth.cpp +++ b/core/qtserialbluetooth.cpp @@ -12,6 +12,7 @@ #include <libdivecomputer/version.h> #include <libdivecomputer/context.h> #include <libdivecomputer/custom.h> +#include <libdivecomputer/serial.h> #if defined(Q_OS_WIN) #include <winsock2.h> @@ -302,6 +303,33 @@ static dc_status_t qt_serial_write(void *io, const void* data, size_t size, size return DC_STATUS_SUCCESS; } +static dc_status_t qt_serial_poll(void *io, int timeout) +{ + qt_serial_t *device = (qt_serial_t*) io; + + if (!device) + return DC_STATUS_INVALIDARGS; + +#if defined(Q_OS_WIN) + // FIXME FIXME FIXME!! But how ? + // I have no idea about windows socket programming - Linus + // We'll just pretend it's always readable, and hope for the best + // Why is the windows side not using QBluetoothSocket? + return DC_STATUS_SUCCESS; +#else + if (!device->socket) + return DC_STATUS_INVALIDARGS; + if (device->socket->waitForReadyRead(timeout)) + return DC_STATUS_SUCCESS; + return DC_STATUS_TIMEOUT; +#endif +} + +static dc_status_t qt_serial_ioctl(void *io, unsigned int request, void *data, size_t size) +{ + return DC_STATUS_UNSUPPORTED; +} + static dc_status_t qt_serial_purge(void *io, dc_direction_t) { qt_serial_t *device = (qt_serial_t*) io; @@ -385,20 +413,20 @@ ble_packet_open(dc_iostream_t **iostream, dc_context_t *context, const char* dev static const dc_custom_cbs_t callbacks = { qt_ble_set_timeout, /* set_timeout */ - NULL, /* set_latency */ NULL, /* set_break */ NULL, /* set_dtr */ NULL, /* set_rts */ NULL, /* get_lines */ NULL, /* get_received */ NULL, /* configure */ + qt_ble_poll, /* poll */ qt_ble_read, /* read */ qt_ble_write, /* write */ + qt_ble_ioctl, /* ioctl */ NULL, /* flush */ NULL, /* purge */ qt_custom_sleep, /* sleep */ qt_ble_close, /* close */ - qt_ble_get_name, /* get_name */ }; rc = qt_ble_open(&io, context, devaddr, (dc_user_device_t *) userdata); @@ -419,15 +447,16 @@ rfcomm_stream_open(dc_iostream_t **iostream, dc_context_t *context, const char* static const dc_custom_cbs_t callbacks = { qt_serial_set_timeout, /* set_timeout */ - NULL, /* set_latency */ NULL, /* set_break */ NULL, /* set_dtr */ NULL, /* set_rts */ NULL, /* get_lines */ qt_serial_get_available, /* get_received */ NULL, /* configure */ + qt_serial_poll, /* poll */ qt_serial_read, /* read */ qt_serial_write, /* write */ + qt_serial_ioctl, /* ioctl */ NULL, /* flush */ qt_serial_purge, /* purge */ qt_custom_sleep, /* sleep */ diff --git a/libdivecomputer b/libdivecomputer -Subproject 4eb34b1466e7dff3ee2c0dfbeeef3642c2166d8 +Subproject f2f775b9aaec76abf9a534b9385dfd2d5fe5058 |