2017-06-25 05:00:52 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2017-06-13 02:47:50 +00:00
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <QtBluetooth/QBluetoothAddress>
|
|
|
|
#include <QLowEnergyController>
|
2017-07-03 17:24:39 +00:00
|
|
|
#include <QLowEnergyService>
|
2017-06-27 13:58:36 +00:00
|
|
|
#include <QCoreApplication>
|
|
|
|
#include <QElapsedTimer>
|
2017-06-13 02:47:50 +00:00
|
|
|
#include <QEventLoop>
|
2017-06-27 13:58:36 +00:00
|
|
|
#include <QThread>
|
2017-06-13 02:47:50 +00:00
|
|
|
#include <QTimer>
|
|
|
|
#include <QDebug>
|
2017-07-06 14:21:04 +00:00
|
|
|
#include <QLoggingCategory>
|
2017-06-13 02:47:50 +00:00
|
|
|
|
|
|
|
#include <libdivecomputer/version.h>
|
|
|
|
|
|
|
|
#include "libdivecomputer.h"
|
|
|
|
#include "core/qt-ble.h"
|
2017-09-17 03:21:46 +00:00
|
|
|
#include "core/btdiscovery.h"
|
2017-06-13 02:47:50 +00:00
|
|
|
|
|
|
|
#if defined(SSRF_CUSTOM_IO)
|
|
|
|
|
|
|
|
#include <libdivecomputer/custom_io.h>
|
|
|
|
|
2017-07-04 00:46:22 +00:00
|
|
|
#define BLE_TIMEOUT 12000 // 12 seconds seems like a very long time to wait
|
2017-07-06 14:21:04 +00:00
|
|
|
#define DEBUG_THRESHOLD 20
|
|
|
|
static int debugCounter;
|
2017-07-04 00:46:22 +00:00
|
|
|
|
2017-07-04 14:40:33 +00:00
|
|
|
#define IS_HW(_d) same_string((_d)->vendor, "Heinrichs Weikamp")
|
|
|
|
#define IS_SHEARWATER(_d) same_string((_d)->vendor, "Shearwater")
|
2017-06-13 02:47:50 +00:00
|
|
|
|
2017-07-05 16:37:21 +00:00
|
|
|
#define MAXIMAL_HW_CREDIT 255
|
|
|
|
#define MINIMAL_HW_CREDIT 32
|
|
|
|
|
2017-07-04 14:40:33 +00:00
|
|
|
extern "C" {
|
2017-07-03 17:24:39 +00:00
|
|
|
|
2017-06-27 13:58:36 +00:00
|
|
|
void waitFor(int ms) {
|
|
|
|
Q_ASSERT(QCoreApplication::instance());
|
|
|
|
Q_ASSERT(QThread::currentThread());
|
|
|
|
|
|
|
|
QElapsedTimer timer;
|
|
|
|
timer.start();
|
|
|
|
|
|
|
|
do {
|
|
|
|
QCoreApplication::processEvents(QEventLoop::AllEvents, ms);
|
|
|
|
QCoreApplication::sendPostedEvents(nullptr, QEvent::DeferredDelete);
|
|
|
|
QThread::msleep(10);
|
|
|
|
} while (timer.elapsed() < ms);
|
|
|
|
}
|
|
|
|
|
2017-06-13 02:47:50 +00:00
|
|
|
void BLEObject::serviceStateChanged(QLowEnergyService::ServiceState s)
|
|
|
|
{
|
2017-06-28 03:53:11 +00:00
|
|
|
Q_UNUSED(s)
|
|
|
|
|
2017-06-13 02:47:50 +00:00
|
|
|
QList<QLowEnergyCharacteristic> list;
|
|
|
|
|
2017-06-27 12:56:30 +00:00
|
|
|
auto service = qobject_cast<QLowEnergyService*>(sender());
|
|
|
|
if (service)
|
|
|
|
list = service->characteristics();
|
2017-06-13 02:47:50 +00:00
|
|
|
|
|
|
|
Q_FOREACH(QLowEnergyCharacteristic c, list) {
|
2017-06-25 05:32:47 +00:00
|
|
|
qDebug() << " " << c.uuid().toString();
|
2017-06-13 02:47:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BLEObject::characteristcStateChanged(const QLowEnergyCharacteristic &c, const QByteArray &value)
|
|
|
|
{
|
2017-07-04 14:40:33 +00:00
|
|
|
if (IS_HW(device)) {
|
2017-07-03 19:21:02 +00:00
|
|
|
if (c.uuid() == hwAllCharacteristics[HW_OSTC_BLE_DATA_TX]) {
|
2017-07-05 16:37:21 +00:00
|
|
|
hw_credit--;
|
2017-07-03 19:21:02 +00:00
|
|
|
receivedPackets.append(value);
|
2017-07-05 16:37:21 +00:00
|
|
|
if (hw_credit == MINIMAL_HW_CREDIT)
|
2017-07-11 15:11:49 +00:00
|
|
|
setHwCredit(MAXIMAL_HW_CREDIT - MINIMAL_HW_CREDIT);
|
2017-07-03 19:21:02 +00:00
|
|
|
} else {
|
|
|
|
qDebug() << "ignore packet from" << c.uuid() << value.toHex();
|
|
|
|
}
|
|
|
|
} else {
|
2017-07-04 14:40:33 +00:00
|
|
|
receivedPackets.append(value);
|
2017-07-03 19:21:02 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-28 03:53:11 +00:00
|
|
|
|
2017-07-03 19:21:02 +00:00
|
|
|
void BLEObject::characteristicWritten(const QLowEnergyCharacteristic &c, const QByteArray &value)
|
|
|
|
{
|
2017-07-04 14:40:33 +00:00
|
|
|
if (IS_HW(device)) {
|
2017-07-03 19:21:02 +00:00
|
|
|
if (c.uuid() == hwAllCharacteristics[HW_OSTC_BLE_CREDITS_RX]) {
|
2017-07-05 16:37:21 +00:00
|
|
|
bool ok;
|
|
|
|
hw_credit += value.toHex().toInt(&ok, 16);
|
2017-07-03 19:21:02 +00:00
|
|
|
isCharacteristicWritten = true;
|
|
|
|
}
|
|
|
|
} else {
|
2017-07-06 14:21:04 +00:00
|
|
|
if (debugCounter < DEBUG_THRESHOLD)
|
|
|
|
qDebug() << "BLEObject::characteristicWritten";
|
2017-07-03 19:21:02 +00:00
|
|
|
}
|
2017-06-13 02:47:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BLEObject::writeCompleted(const QLowEnergyDescriptor &d, const QByteArray &value)
|
|
|
|
{
|
2017-07-11 15:11:49 +00:00
|
|
|
Q_UNUSED(value)
|
|
|
|
Q_UNUSED(d)
|
|
|
|
qDebug() << "BLE write completed";
|
2017-06-13 02:47:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BLEObject::addService(const QBluetoothUuid &newService)
|
|
|
|
{
|
2017-06-27 11:50:19 +00:00
|
|
|
qDebug() << "Found service" << newService;
|
|
|
|
bool isStandardUuid = false;
|
|
|
|
newService.toUInt16(&isStandardUuid);
|
2017-07-04 14:40:33 +00:00
|
|
|
if (IS_HW(device)) {
|
2017-07-03 18:27:57 +00:00
|
|
|
/* The HW BT/BLE piece or hardware uses, what we
|
|
|
|
* call here, "a Standard UUID. It is standard because the Telit/Stollmann
|
|
|
|
* manufacturer applied for an own UUID for its product, and this was granted
|
|
|
|
* by the Bluetooth SIG.
|
|
|
|
*/
|
|
|
|
if (newService != QUuid("{0000fefb-0000-1000-8000-00805f9b34fb}"))
|
|
|
|
return; // skip all services except the right one
|
2017-07-06 08:37:21 +00:00
|
|
|
} else if (isStandardUuid) {
|
2017-06-27 11:50:19 +00:00
|
|
|
qDebug () << " .. ignoring standard service";
|
2017-06-13 02:47:50 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-06-27 11:50:19 +00:00
|
|
|
|
2017-06-27 12:56:30 +00:00
|
|
|
auto service = controller->createServiceObject(newService, this);
|
2017-06-25 05:32:47 +00:00
|
|
|
qDebug() << " .. created service object" << service;
|
2017-06-13 02:47:50 +00:00
|
|
|
if (service) {
|
2017-06-27 12:56:30 +00:00
|
|
|
services.append(service);
|
2017-06-13 02:47:50 +00:00
|
|
|
connect(service, &QLowEnergyService::stateChanged, this, &BLEObject::serviceStateChanged);
|
|
|
|
connect(service, &QLowEnergyService::characteristicChanged, this, &BLEObject::characteristcStateChanged);
|
2017-07-03 19:21:02 +00:00
|
|
|
connect(service, &QLowEnergyService::characteristicWritten, this, &BLEObject::characteristicWritten);
|
2017-06-13 02:47:50 +00:00
|
|
|
connect(service, &QLowEnergyService::descriptorWritten, this, &BLEObject::writeCompleted);
|
|
|
|
service->discoverDetails();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-27 22:14:27 +00:00
|
|
|
BLEObject::BLEObject(QLowEnergyController *c, dc_user_device_t *d)
|
2017-06-13 02:47:50 +00:00
|
|
|
{
|
|
|
|
controller = c;
|
2017-06-27 22:14:27 +00:00
|
|
|
device = d;
|
2017-07-06 14:21:04 +00:00
|
|
|
debugCounter = 0;
|
2017-06-13 02:47:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BLEObject::~BLEObject()
|
|
|
|
{
|
2017-06-25 05:32:47 +00:00
|
|
|
qDebug() << "Deleting BLE object";
|
2017-11-01 14:25:24 +00:00
|
|
|
|
|
|
|
foreach (QLowEnergyService *service, services)
|
|
|
|
delete service;
|
|
|
|
|
|
|
|
delete controller;
|
2017-06-13 02:47:50 +00:00
|
|
|
}
|
|
|
|
|
2017-06-27 22:14:27 +00:00
|
|
|
dc_status_t BLEObject::write(const void *data, size_t size, size_t *actual)
|
2017-06-13 02:47:50 +00:00
|
|
|
{
|
2017-06-28 03:53:11 +00:00
|
|
|
Q_UNUSED(actual) // that seems like it might cause problems
|
|
|
|
|
BLE: read until no more data in coming in
The current BLE read reads just one 20 bype packet. That packet size is set
in ble_serial_ops, so, without being able to test on anything other than
a OSTC3, I assume that this holds for other BLE DCs too. So, I think is
is weird that those interfaces work with the current read() of just one
packet at the time.
As we need a blocking read (at least for the OSTC parser), just read all
data that is available on the input. And when we think we are done, give
the QtEventloop control to see if there is more, and process that incoming
data as well. All this basically implements a blocking read.
CAVEAT 1: This might break the reading from the currently working BLE devices.
CAVEAT 2: With this, I still cannot read the OSTC3 completely. For
developers familiar with the HW transfer protocol: it just stops while
reading the first full dive (header + profile) command 0x66, despite
correctly reading about 5Kb of data before. For some
reason, I do not believe that this is related to this commit.
CAVEAT 3: All above tested on Linux Desktop with bluez stack, and
confirmed NOT to work on Android 7.1.2, build with Qt 5.9.0, And
yes, I know 5.9.1 recommended.
Signed-off-by: Jan Mulder <jlmulder@xs4all.nl>
2017-07-04 07:03:30 +00:00
|
|
|
if (!receivedPackets.isEmpty()) {
|
|
|
|
qDebug() << ".. write HIT with still incoming packets in queue";
|
|
|
|
}
|
|
|
|
|
2017-06-27 12:56:30 +00:00
|
|
|
QList<QLowEnergyCharacteristic> list = preferredService()->characteristics();
|
2017-09-20 23:19:25 +00:00
|
|
|
|
|
|
|
if (list.isEmpty())
|
|
|
|
return DC_STATUS_IO;
|
|
|
|
|
2017-06-13 02:47:50 +00:00
|
|
|
QByteArray bytes((const char *)data, (int) size);
|
|
|
|
|
2017-09-20 23:19:25 +00:00
|
|
|
const QLowEnergyCharacteristic &c = list.constFirst();
|
|
|
|
QLowEnergyService::WriteMode mode;
|
2017-06-13 02:47:50 +00:00
|
|
|
|
2017-09-20 23:19:25 +00:00
|
|
|
mode = (c.properties() & QLowEnergyCharacteristic::WriteNoResponse) ?
|
2017-06-13 02:47:50 +00:00
|
|
|
QLowEnergyService::WriteWithoutResponse :
|
|
|
|
QLowEnergyService::WriteWithResponse;
|
|
|
|
|
2017-09-20 23:19:25 +00:00
|
|
|
if (IS_SHEARWATER(device))
|
|
|
|
bytes.prepend("\1\0", 2);
|
2017-06-13 02:47:50 +00:00
|
|
|
|
2017-09-20 23:19:25 +00:00
|
|
|
preferredService()->writeCharacteristic(c, bytes, mode);
|
|
|
|
return DC_STATUS_SUCCESS;
|
2017-06-13 02:47:50 +00:00
|
|
|
}
|
|
|
|
|
2017-06-27 22:14:27 +00:00
|
|
|
dc_status_t BLEObject::read(void *data, size_t size, size_t *actual)
|
2017-06-13 02:47:50 +00:00
|
|
|
{
|
2017-07-12 19:23:02 +00:00
|
|
|
if (actual)
|
|
|
|
*actual = 0;
|
2017-06-13 02:47:50 +00:00
|
|
|
if (receivedPackets.isEmpty()) {
|
2017-06-27 12:56:30 +00:00
|
|
|
QList<QLowEnergyCharacteristic> list = preferredService()->characteristics();
|
2017-06-13 02:47:50 +00:00
|
|
|
if (list.isEmpty())
|
|
|
|
return DC_STATUS_IO;
|
|
|
|
|
2017-07-04 00:46:22 +00:00
|
|
|
int msec = BLE_TIMEOUT;
|
2017-07-03 18:56:44 +00:00
|
|
|
while (msec > 0 && receivedPackets.isEmpty()) {
|
|
|
|
waitFor(100);
|
|
|
|
msec -= 100;
|
|
|
|
};
|
2017-06-13 02:47:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Still no packet?
|
|
|
|
if (receivedPackets.isEmpty())
|
|
|
|
return DC_STATUS_IO;
|
|
|
|
|
2017-07-11 11:29:41 +00:00
|
|
|
QByteArray packet = receivedPackets.takeFirst();
|
BLE: read until no more data in coming in
The current BLE read reads just one 20 bype packet. That packet size is set
in ble_serial_ops, so, without being able to test on anything other than
a OSTC3, I assume that this holds for other BLE DCs too. So, I think is
is weird that those interfaces work with the current read() of just one
packet at the time.
As we need a blocking read (at least for the OSTC parser), just read all
data that is available on the input. And when we think we are done, give
the QtEventloop control to see if there is more, and process that incoming
data as well. All this basically implements a blocking read.
CAVEAT 1: This might break the reading from the currently working BLE devices.
CAVEAT 2: With this, I still cannot read the OSTC3 completely. For
developers familiar with the HW transfer protocol: it just stops while
reading the first full dive (header + profile) command 0x66, despite
correctly reading about 5Kb of data before. For some
reason, I do not believe that this is related to this commit.
CAVEAT 3: All above tested on Linux Desktop with bluez stack, and
confirmed NOT to work on Android 7.1.2, build with Qt 5.9.0, And
yes, I know 5.9.1 recommended.
Signed-off-by: Jan Mulder <jlmulder@xs4all.nl>
2017-07-04 07:03:30 +00:00
|
|
|
|
2017-07-11 11:29:41 +00:00
|
|
|
if (IS_SHEARWATER(device))
|
|
|
|
packet.remove(0,2);
|
2017-06-27 22:14:27 +00:00
|
|
|
|
2017-08-26 21:53:59 +00:00
|
|
|
if ((size_t)packet.size() > size)
|
2017-07-11 11:29:41 +00:00
|
|
|
return DC_STATUS_NOMEMORY;
|
2017-06-27 22:14:27 +00:00
|
|
|
|
2017-07-11 11:29:41 +00:00
|
|
|
memcpy((char *)data, packet.data(), packet.size());
|
2017-07-12 19:23:02 +00:00
|
|
|
if (actual)
|
|
|
|
*actual += packet.size();
|
BLE: read until no more data in coming in
The current BLE read reads just one 20 bype packet. That packet size is set
in ble_serial_ops, so, without being able to test on anything other than
a OSTC3, I assume that this holds for other BLE DCs too. So, I think is
is weird that those interfaces work with the current read() of just one
packet at the time.
As we need a blocking read (at least for the OSTC parser), just read all
data that is available on the input. And when we think we are done, give
the QtEventloop control to see if there is more, and process that incoming
data as well. All this basically implements a blocking read.
CAVEAT 1: This might break the reading from the currently working BLE devices.
CAVEAT 2: With this, I still cannot read the OSTC3 completely. For
developers familiar with the HW transfer protocol: it just stops while
reading the first full dive (header + profile) command 0x66, despite
correctly reading about 5Kb of data before. For some
reason, I do not believe that this is related to this commit.
CAVEAT 3: All above tested on Linux Desktop with bluez stack, and
confirmed NOT to work on Android 7.1.2, build with Qt 5.9.0, And
yes, I know 5.9.1 recommended.
Signed-off-by: Jan Mulder <jlmulder@xs4all.nl>
2017-07-04 07:03:30 +00:00
|
|
|
|
2017-06-13 02:47:50 +00:00
|
|
|
return DC_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2017-07-05 16:37:21 +00:00
|
|
|
dc_status_t BLEObject::setHwCredit(unsigned int c)
|
|
|
|
{
|
|
|
|
/* The Terminal I/O client transmits initial UART credits to the server (see 6.5).
|
|
|
|
*
|
|
|
|
* Notice that we have to write to the characteristic here, and not to its
|
|
|
|
* descriptor as for the enabeling of notifications or indications.
|
|
|
|
*
|
|
|
|
* Futher notice that this function has the implicit effect of processing the
|
|
|
|
* event loop (due to waiting for the confirmation of the credit request).
|
|
|
|
* So, as characteristcStateChanged will be triggered, while receiving
|
|
|
|
* data from the OSTC, these are processed too.
|
|
|
|
*/
|
|
|
|
|
|
|
|
QList<QLowEnergyCharacteristic> list = preferredService()->characteristics();
|
|
|
|
isCharacteristicWritten = false;
|
|
|
|
preferredService()->writeCharacteristic(list[HW_OSTC_BLE_CREDITS_RX],
|
|
|
|
QByteArray(1, c),
|
|
|
|
QLowEnergyService::WriteWithResponse);
|
|
|
|
|
|
|
|
/* And wait for the answer*/
|
|
|
|
int msec = BLE_TIMEOUT;
|
|
|
|
while (msec > 0 && !isCharacteristicWritten) {
|
|
|
|
waitFor(100);
|
|
|
|
msec -= 100;
|
|
|
|
};
|
|
|
|
if (!isCharacteristicWritten)
|
|
|
|
return DC_STATUS_TIMEOUT;
|
|
|
|
return DC_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2017-07-04 14:40:33 +00:00
|
|
|
dc_status_t BLEObject::setupHwTerminalIo(QList<QLowEnergyCharacteristic> allC)
|
2017-07-03 17:24:39 +00:00
|
|
|
{ /* This initalizes the Terminal I/O client as described in
|
|
|
|
* http://www.telit.com/fileadmin/user_upload/products/Downloads/sr-rf/BlueMod/TIO_Implementation_Guide_r04.pdf
|
|
|
|
* Referenced section numbers below are from that document.
|
|
|
|
*
|
|
|
|
* This is for all HW computers, that use referenced BT/BLE hardware module from Telit
|
|
|
|
* (formerly Stollmann). The 16 bit UUID 0xFEFB (or a derived 128 bit UUID starting with
|
|
|
|
* 0x0000FEFB is a clear indication that the OSTC is equipped with this BT/BLE hardware.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (allC.length() != 4) {
|
|
|
|
qDebug() << "This should not happen. HW/OSTC BT/BLE device without 4 Characteristics";
|
|
|
|
return DC_STATUS_IO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The Terminal I/O client subscribes to indications of the UART credits TX
|
|
|
|
* characteristic (see 6.4).
|
|
|
|
*
|
|
|
|
* Notice that indications are subscribed to by writing 0x0200 to its descriptor. This
|
|
|
|
* can be understood by looking for Client Characteristic Configuration, Assigned
|
|
|
|
* Number: 0x2902. Enabling/Disabeling is setting the proper bit, and they
|
|
|
|
* differ for indications and notifications.
|
|
|
|
*/
|
|
|
|
QLowEnergyDescriptor d = allC[HW_OSTC_BLE_CREDITS_TX].descriptors().first();
|
|
|
|
preferredService()->writeDescriptor(d, QByteArray::fromHex("0200"));
|
|
|
|
|
|
|
|
/* The Terminal I/O client subscribes to notifications of the UART data TX
|
|
|
|
* characteristic (see 6.2).
|
|
|
|
*/
|
|
|
|
d = allC[HW_OSTC_BLE_DATA_TX].descriptors().first();
|
|
|
|
preferredService()->writeDescriptor(d, QByteArray::fromHex("0100"));
|
|
|
|
|
2017-07-05 16:37:21 +00:00
|
|
|
/* The Terminal I/O client transmits initial UART credits to the server (see 6.5). */
|
|
|
|
return setHwCredit(MAXIMAL_HW_CREDIT);
|
2017-07-03 17:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-06-13 02:47:50 +00:00
|
|
|
dc_status_t qt_ble_open(dc_custom_io_t *io, dc_context_t *context, const char *devaddr)
|
|
|
|
{
|
2017-06-28 03:53:11 +00:00
|
|
|
Q_UNUSED(context)
|
2017-07-06 14:21:04 +00:00
|
|
|
debugCounter = 0;
|
|
|
|
QLoggingCategory::setFilterRules(QStringLiteral("qt.bluetooth* = true"));
|
|
|
|
|
2017-06-27 01:17:06 +00:00
|
|
|
/*
|
|
|
|
* LE-only devices get the "LE:" prepended by the scanning
|
|
|
|
* code, so that the rfcomm code can see they only do LE.
|
|
|
|
*
|
|
|
|
* We just skip that prefix (and it doesn't always exist,
|
|
|
|
* since the device may support both legacy BT and LE).
|
|
|
|
*/
|
|
|
|
if (!strncmp(devaddr, "LE:", 3))
|
|
|
|
devaddr += 3;
|
|
|
|
|
2017-06-13 02:47:50 +00:00
|
|
|
// HACK ALERT! Qt 5.9 needs this for proper Bluez operation
|
|
|
|
qputenv("QT_DEFAULT_CENTRAL_SERVICES", "1");
|
|
|
|
|
2017-09-17 23:31:07 +00:00
|
|
|
#if defined(Q_OS_MACOS) || defined(Q_OS_IOS)
|
2017-09-17 03:21:46 +00:00
|
|
|
QBluetoothDeviceInfo remoteDevice = getBtDeviceInfo(devaddr);
|
|
|
|
QLowEnergyController *controller = QLowEnergyController::createCentral(remoteDevice);
|
2017-09-17 23:31:07 +00:00
|
|
|
#else
|
|
|
|
// this is deprecated but given that we don't use Qt to scan for
|
|
|
|
// devices on Android, we don't have QBluetoothDeviceInfo for the
|
|
|
|
// paired devices and therefore cannot use the newer interfaces
|
|
|
|
// that are preferred starting with Qt 5.7
|
|
|
|
QBluetoothAddress remoteDeviceAddress(devaddr);
|
|
|
|
QLowEnergyController *controller = new QLowEnergyController(remoteDeviceAddress);
|
|
|
|
#endif
|
2017-06-25 05:32:47 +00:00
|
|
|
qDebug() << "qt_ble_open(" << devaddr << ")";
|
2017-06-13 02:47:50 +00:00
|
|
|
|
2017-07-04 14:40:33 +00:00
|
|
|
if (IS_SHEARWATER(io->user_device))
|
2017-06-27 22:14:27 +00:00
|
|
|
controller->setRemoteAddressType(QLowEnergyController::RandomAddress);
|
|
|
|
|
2017-06-27 13:58:36 +00:00
|
|
|
// Try to connect to the device
|
|
|
|
controller->connectToDevice();
|
2017-06-13 02:47:50 +00:00
|
|
|
|
|
|
|
// Create a timer. If the connection doesn't succeed after five seconds or no error occurs then stop the opening step
|
2017-07-04 00:46:22 +00:00
|
|
|
int msec = BLE_TIMEOUT;
|
2017-06-27 13:58:36 +00:00
|
|
|
while (msec > 0 && controller->state() == QLowEnergyController::ConnectingState) {
|
|
|
|
waitFor(100);
|
|
|
|
msec -= 100;
|
|
|
|
};
|
2017-06-13 02:47:50 +00:00
|
|
|
|
|
|
|
switch (controller->state()) {
|
|
|
|
case QLowEnergyController::ConnectedState:
|
2017-06-25 05:32:47 +00:00
|
|
|
qDebug() << "connected to the controller for device" << devaddr;
|
2017-06-13 02:47:50 +00:00
|
|
|
break;
|
2017-11-13 22:41:11 +00:00
|
|
|
case QLowEnergyController::ConnectingState:
|
|
|
|
qDebug() << "timeout while trying to connect to the controller " << devaddr;
|
|
|
|
report_error("Timeout while trying to connect to %s", devaddr);
|
|
|
|
delete controller;
|
|
|
|
return DC_STATUS_IO;
|
2017-06-13 02:47:50 +00:00
|
|
|
default:
|
2017-06-25 05:32:47 +00:00
|
|
|
qDebug() << "failed to connect to the controller " << devaddr << "with error" << controller->errorString();
|
2017-06-13 02:47:50 +00:00
|
|
|
report_error("Failed to connect to %s: '%s'", devaddr, controller->errorString().toUtf8().data());
|
|
|
|
delete controller;
|
|
|
|
return DC_STATUS_IO;
|
|
|
|
}
|
|
|
|
|
2017-11-01 14:25:24 +00:00
|
|
|
// We need to discover services etc here!
|
|
|
|
// Note that ble takes ownership of controller and henceforth deleting ble will
|
|
|
|
// take care of deleting controller.
|
2017-06-27 22:14:27 +00:00
|
|
|
BLEObject *ble = new BLEObject(controller, io->user_device);
|
2017-06-13 02:47:50 +00:00
|
|
|
ble->connect(controller, SIGNAL(serviceDiscovered(QBluetoothUuid)), SLOT(addService(QBluetoothUuid)));
|
|
|
|
|
2017-06-25 05:32:47 +00:00
|
|
|
qDebug() << " .. discovering services";
|
2017-06-13 02:47:50 +00:00
|
|
|
|
|
|
|
controller->discoverServices();
|
2017-06-27 13:58:36 +00:00
|
|
|
|
2017-07-04 00:46:22 +00:00
|
|
|
msec = BLE_TIMEOUT;
|
2017-06-27 13:58:36 +00:00
|
|
|
while (msec > 0 && controller->state() == QLowEnergyController::DiscoveringState) {
|
|
|
|
waitFor(100);
|
|
|
|
msec -= 100;
|
|
|
|
};
|
2017-06-13 02:47:50 +00:00
|
|
|
|
2017-06-25 05:32:47 +00:00
|
|
|
qDebug() << " .. done discovering services";
|
2017-06-27 13:58:36 +00:00
|
|
|
if (ble->preferredService() == nullptr) {
|
|
|
|
qDebug() << "failed to find suitable service on" << devaddr;
|
|
|
|
report_error("Failed to find suitable service on '%s'", devaddr);
|
2017-11-01 14:25:24 +00:00
|
|
|
delete ble;
|
2017-06-27 13:58:36 +00:00
|
|
|
return DC_STATUS_IO;
|
|
|
|
}
|
2017-06-13 02:47:50 +00:00
|
|
|
|
2017-06-25 05:32:47 +00:00
|
|
|
qDebug() << " .. discovering details";
|
2017-07-04 00:46:22 +00:00
|
|
|
msec = BLE_TIMEOUT;
|
2017-06-27 13:58:36 +00:00
|
|
|
while (msec > 0 && ble->preferredService()->state() == QLowEnergyService::DiscoveringServices) {
|
|
|
|
waitFor(100);
|
|
|
|
msec -= 100;
|
|
|
|
};
|
2017-06-13 02:47:50 +00:00
|
|
|
|
2017-06-27 13:58:36 +00:00
|
|
|
if (ble->preferredService()->state() != QLowEnergyService::ServiceDiscovered) {
|
2017-06-27 12:56:30 +00:00
|
|
|
qDebug() << "failed to find suitable service on" << devaddr;
|
|
|
|
report_error("Failed to find suitable service on '%s'", devaddr);
|
2017-11-01 14:25:24 +00:00
|
|
|
delete ble;
|
2017-06-27 12:56:30 +00:00
|
|
|
return DC_STATUS_IO;
|
|
|
|
}
|
|
|
|
|
2017-06-13 02:47:50 +00:00
|
|
|
|
2017-06-25 05:32:47 +00:00
|
|
|
qDebug() << " .. enabling notifications";
|
2017-06-13 02:47:50 +00:00
|
|
|
|
|
|
|
/* Enable notifications */
|
2017-06-27 12:56:30 +00:00
|
|
|
QList<QLowEnergyCharacteristic> list = ble->preferredService()->characteristics();
|
2017-06-13 02:47:50 +00:00
|
|
|
|
|
|
|
if (!list.isEmpty()) {
|
|
|
|
const QLowEnergyCharacteristic &c = list.constLast();
|
|
|
|
|
2017-07-04 14:40:33 +00:00
|
|
|
if (IS_HW(io->user_device)) {
|
|
|
|
dc_status_t r = ble->setupHwTerminalIo(list);
|
2017-11-01 14:25:24 +00:00
|
|
|
if (r != DC_STATUS_SUCCESS) {
|
|
|
|
delete ble;
|
2017-07-04 14:40:33 +00:00
|
|
|
return r;
|
2017-11-01 14:25:24 +00:00
|
|
|
}
|
2017-07-03 17:24:39 +00:00
|
|
|
} else {
|
|
|
|
QList<QLowEnergyDescriptor> l = c.descriptors();
|
2017-06-13 02:47:50 +00:00
|
|
|
|
2017-07-03 17:24:39 +00:00
|
|
|
qDebug() << "Descriptor list with" << l.length() << "elements";
|
2017-06-13 02:47:50 +00:00
|
|
|
|
2017-07-03 17:24:39 +00:00
|
|
|
QLowEnergyDescriptor d;
|
|
|
|
foreach(d, l)
|
|
|
|
qDebug() << "Descriptor:" << d.name() << "uuid:" << d.uuid().toString();
|
2017-06-25 05:32:47 +00:00
|
|
|
|
2017-07-03 17:24:39 +00:00
|
|
|
if (!l.isEmpty()) {
|
2017-09-17 16:48:52 +00:00
|
|
|
bool foundCCC = false;
|
|
|
|
foreach (d, l) {
|
|
|
|
if (d.type() == QBluetoothUuid::ClientCharacteristicConfiguration) {
|
|
|
|
// pick the correct characteristic
|
|
|
|
foundCCC = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!foundCCC)
|
|
|
|
// if we didn't find a ClientCharacteristicConfiguration, try the first one
|
|
|
|
d = l.first();
|
|
|
|
|
|
|
|
qDebug() << "now writing \"0x0100\" to the descriptor" << d.uuid().toString();
|
2017-06-13 02:47:50 +00:00
|
|
|
|
2017-07-03 17:24:39 +00:00
|
|
|
ble->preferredService()->writeDescriptor(d, QByteArray::fromHex("0100"));
|
|
|
|
}
|
2017-06-13 02:47:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill in info
|
|
|
|
io->userdata = (void *)ble;
|
|
|
|
return DC_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
dc_status_t qt_ble_close(dc_custom_io_t *io)
|
|
|
|
{
|
|
|
|
BLEObject *ble = (BLEObject *) io->userdata;
|
|
|
|
|
|
|
|
io->userdata = NULL;
|
|
|
|
delete ble;
|
|
|
|
|
|
|
|
return DC_STATUS_SUCCESS;
|
|
|
|
}
|
2017-07-06 14:21:04 +00:00
|
|
|
static void checkThreshold()
|
|
|
|
{
|
|
|
|
if (++debugCounter == DEBUG_THRESHOLD) {
|
|
|
|
QLoggingCategory::setFilterRules(QStringLiteral("qt.bluetooth* = false"));
|
|
|
|
qDebug() << "turning off further BT debug output";
|
|
|
|
}
|
|
|
|
}
|
2017-06-13 02:47:50 +00:00
|
|
|
|
|
|
|
dc_status_t qt_ble_read(dc_custom_io_t *io, void* data, size_t size, size_t *actual)
|
|
|
|
{
|
2017-07-06 14:21:04 +00:00
|
|
|
checkThreshold();
|
2017-06-13 02:47:50 +00:00
|
|
|
BLEObject *ble = (BLEObject *) io->userdata;
|
|
|
|
return ble->read(data, size, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
dc_status_t qt_ble_write(dc_custom_io_t *io, const void* data, size_t size, size_t *actual)
|
|
|
|
{
|
2017-07-06 14:21:04 +00:00
|
|
|
checkThreshold();
|
2017-06-13 02:47:50 +00:00
|
|
|
BLEObject *ble = (BLEObject *) io->userdata;
|
|
|
|
return ble->write(data, size, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
} /* extern "C" */
|
|
|
|
#endif
|