subsurface/core/qt-ble.h
Linus Torvalds 30fb7bf35c qt-ble: set up infrastructure for better preferred service choice
We used to just pick the first non-standard service we found (with a
special case for the Heinrichs Weikamp dive computers that have an
actual registered standard service).

We then waited for that service to finish discovery, and started using
it.

This changes the logic to wait for _all_ services to finish discovery,
and then after that we pick the one we like best.  Right now the rule
for picking a preferred service is the same one we had before, but the
difference is that we now have the full discovery data, so we *could* do
something better.

Plus this makes our debug messages a lot more legible, when we don't
have the mix of overlapping service discovery with the actual IO we do
to the preferred service.

NOTE! This doesn't much matter for most of the dive computers that we
currently support BLE for.  They don't tend to have a lot of odd
services.

But at least both the Mares BlueLink and the Garmin Descent both have
multiple services and it's not obvious which one to use, and this will
make it not only easier to debug those, it will make it easier to pick
the right preferred service descriptor to use.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2018-09-23 17:29:05 -07:00

63 lines
2.1 KiB
C++

// SPDX-License-Identifier: GPL-2.0
#ifndef QT_BLE_H
#define QT_BLE_H
#include <stddef.h>
#include "core/libdivecomputer.h"
#include <QVector>
#include <QLowEnergyController>
#include <QEventLoop>
#define HW_OSTC_BLE_DATA_RX 0
#define HW_OSTC_BLE_DATA_TX 1
#define HW_OSTC_BLE_CREDITS_RX 2
#define HW_OSTC_BLE_CREDITS_TX 3
class BLEObject : public QObject
{
Q_OBJECT
public:
BLEObject(QLowEnergyController *c, dc_user_device_t *);
~BLEObject();
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 QLowEnergyService *preferredService() { return preferred; }
dc_status_t select_preferred_service(void);
public slots:
void addService(const QBluetoothUuid &newService);
void serviceStateChanged(QLowEnergyService::ServiceState s);
void characteristcStateChanged(const QLowEnergyCharacteristic &c, const QByteArray &value);
void characteristicWritten(const QLowEnergyCharacteristic &c, const QByteArray &value);
void writeCompleted(const QLowEnergyDescriptor &d, const QByteArray &value);
dc_status_t setupHwTerminalIo(QList<QLowEnergyCharacteristic>);
dc_status_t setHwCredit(unsigned int c);
private:
QVector<QLowEnergyService *> services;
QLowEnergyController *controller = nullptr;
QLowEnergyService *preferred = nullptr;
QList<QByteArray> receivedPackets;
bool isCharacteristicWritten;
dc_user_device_t *device;
unsigned int hw_credit = 0;
QList<QUuid> hwAllCharacteristics = {
"{00000001-0000-1000-8000-008025000000}", // HW_OSTC_BLE_DATA_RX
"{00000002-0000-1000-8000-008025000000}", // HW_OSTC_BLE_DATA_TX
"{00000003-0000-1000-8000-008025000000}", // HW_OSTC_BLE_CREDITS_RX
"{00000004-0000-1000-8000-008025000000}" // HW_OSTC_BLE_CREDITS_TX
};
};
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_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_close(void *io);
}
#endif