2017-06-06 02:41:57 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
|
|
|
|
#include "btdiscovery.h"
|
|
|
|
#include "downloadfromdcthread.h"
|
2017-06-10 12:22:28 +00:00
|
|
|
#include "core/libdivecomputer.h"
|
2017-07-18 21:49:42 +00:00
|
|
|
#include <QTimer>
|
2017-06-06 02:41:57 +00:00
|
|
|
#include <QDebug>
|
|
|
|
|
2017-06-10 12:22:28 +00:00
|
|
|
extern QMap<QString, dc_descriptor_t *> descriptorLookup;
|
|
|
|
|
2017-06-06 02:41:57 +00:00
|
|
|
BTDiscovery *BTDiscovery::m_instance = NULL;
|
|
|
|
|
2017-07-17 04:45:21 +00:00
|
|
|
ConnectionListModel::ConnectionListModel(QObject *parent) :
|
|
|
|
QAbstractListModel(parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QHash <int, QByteArray> ConnectionListModel::roleNames() const
|
|
|
|
{
|
|
|
|
QHash<int, QByteArray> roles;
|
|
|
|
roles[AddressRole] = "address";
|
|
|
|
return roles;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant ConnectionListModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
if (index.row() < 0 || index.row() >= m_addresses.count())
|
|
|
|
return QVariant();
|
|
|
|
if (role != AddressRole)
|
|
|
|
return QVariant();
|
|
|
|
return m_addresses[index.row()];
|
|
|
|
}
|
|
|
|
|
|
|
|
QString ConnectionListModel::address(int idx) const
|
|
|
|
{
|
|
|
|
if (idx < 0 || idx >> m_addresses.count())
|
|
|
|
return QString();
|
|
|
|
return m_addresses[idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
int ConnectionListModel::rowCount(const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
Q_UNUSED(parent)
|
|
|
|
return m_addresses.count();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConnectionListModel::addAddress(const QString address)
|
|
|
|
{
|
|
|
|
beginInsertRows(QModelIndex(), rowCount(), rowCount());
|
|
|
|
m_addresses.append(address);
|
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
2017-06-10 12:22:28 +00:00
|
|
|
static dc_descriptor_t *getDeviceType(QString btName)
|
|
|
|
// central function to convert a BT name to a Subsurface known vendor/model pair
|
|
|
|
{
|
|
|
|
QString vendor, product;
|
|
|
|
|
|
|
|
if (btName.startsWith("OSTC")) {
|
|
|
|
vendor = "Heinrichs Weikamp";
|
|
|
|
if (btName.mid(4,2) == "3#") product = "OSTC 3";
|
|
|
|
else if (btName.mid(4,2) == "3+") product = "OSTC 3+";
|
|
|
|
else if (btName.mid(4,2) == "s#") product = "OSTC Sport";
|
2017-07-13 18:48:52 +00:00
|
|
|
else if (btName.mid(4,2) == "s ") product = "OSTC Sport";
|
2017-06-10 12:22:28 +00:00
|
|
|
else if (btName.mid(4,2) == "4-") product = "OSTC 4";
|
|
|
|
else if (btName.mid(4,2) == "2-") product = "OSTC 2N";
|
|
|
|
// all OSTCs are HW_FAMILY_OSTC_3, so when we do not know,
|
|
|
|
// just try this
|
|
|
|
else product = "OSTC 3"; // all OSTCs are HW_FAMILY_OSTC_3
|
|
|
|
}
|
|
|
|
|
2017-07-17 14:42:22 +00:00
|
|
|
if (btName.startsWith("Petrel") || btName.startsWith("Perdix") || btName.startsWith("Predator")) {
|
2017-06-12 18:29:19 +00:00
|
|
|
vendor = "Shearwater";
|
|
|
|
if (btName.startsWith("Petrel")) product = "Petrel"; // or petrel 2?
|
|
|
|
if (btName.startsWith("Perdix")) product = "Perdix";
|
2017-07-17 14:42:22 +00:00
|
|
|
if (btName.startsWith("Predator")) product = "Predator";
|
2017-06-12 18:29:19 +00:00
|
|
|
}
|
|
|
|
|
2017-06-24 04:29:34 +00:00
|
|
|
if (btName.startsWith("EON Steel")) {
|
|
|
|
vendor = "Suunto";
|
|
|
|
product = "EON Steel";
|
|
|
|
}
|
|
|
|
|
2017-07-03 01:31:40 +00:00
|
|
|
if (btName.startsWith("G2")) {
|
|
|
|
vendor = "Scubapro";
|
|
|
|
product = "G2";
|
|
|
|
}
|
|
|
|
|
2017-06-10 12:22:28 +00:00
|
|
|
if (!vendor.isEmpty() && !product.isEmpty())
|
|
|
|
return(descriptorLookup[vendor + product]);
|
|
|
|
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
2017-06-06 02:41:57 +00:00
|
|
|
BTDiscovery::BTDiscovery(QObject *parent)
|
|
|
|
{
|
|
|
|
Q_UNUSED(parent)
|
|
|
|
if (m_instance) {
|
|
|
|
qDebug() << "trying to create an additional BTDiscovery object";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_instance = this;
|
|
|
|
#if defined(BT_SUPPORT)
|
2017-07-18 21:49:14 +00:00
|
|
|
#if !defined(Q_OS_IOS)
|
2017-06-06 02:41:57 +00:00
|
|
|
if (localBtDevice.isValid() &&
|
|
|
|
localBtDevice.hostMode() == QBluetoothLocalDevice::HostConnectable) {
|
|
|
|
btPairedDevices.clear();
|
|
|
|
qDebug() << "localDevice " + localBtDevice.name() + " is valid, starting discovery";
|
2017-07-12 11:13:25 +00:00
|
|
|
m_btValid = true;
|
2017-07-18 21:49:14 +00:00
|
|
|
#else
|
|
|
|
m_btValid = false;
|
|
|
|
#endif
|
|
|
|
#if defined(Q_OS_IOS) || (defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID))
|
2017-06-06 02:41:57 +00:00
|
|
|
discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
|
|
|
|
connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &BTDiscovery::btDeviceDiscovered);
|
2017-07-18 21:49:14 +00:00
|
|
|
qDebug() << "starting BLE discovery";
|
2017-06-06 02:41:57 +00:00
|
|
|
discoveryAgent->start();
|
|
|
|
#endif
|
|
|
|
#if defined(Q_OS_ANDROID) && defined(BT_SUPPORT)
|
|
|
|
getBluetoothDevices();
|
Mobile: wrap up fixes for BT download on Android
Major functional change in this commit is the addition of found static BT devices
to the internal administration (on Android), in a way that is equivalent to
mobile-on-desktop. So, in both cases, the list of devices in the app are
as in the list of devices on the host OS (Linux or Android). To minimize code
duplication, the btDeviceDiscovered slot is split in two parts, the part to
act as slot for the Qt BT discovery agent (Linux, so mobile-on-desktop), and
the part only needed for Android.
Remaining to be fixed: the correct handling of the QML UI selection of
vendor/product. The first default dive computer is correctly detected,
all paired devices from the virtual vendow can be selected, but clicking
through vendors results in non logical selections. It is obvious why
this is, but a fix is not straigforward at this point.
Signed-off-by: Jan Mulder <jlmulder@xs4all.nl>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2017-06-12 08:38:24 +00:00
|
|
|
// and add the paired devices to the internal data
|
|
|
|
// So behaviour is same on Linux/Bluez stack and
|
|
|
|
// Android/Java stack with respect to discovery
|
|
|
|
for (int i = 0; i < btPairedDevices.length(); i++) {
|
|
|
|
btDeviceDiscoveredMain(btPairedDevices[i]);
|
|
|
|
|
|
|
|
}
|
2017-06-06 02:41:57 +00:00
|
|
|
#endif
|
|
|
|
for (int i = 0; i < btPairedDevices.length(); i++) {
|
2017-06-30 02:47:51 +00:00
|
|
|
qDebug() << "Paired =" << btPairedDevices[i].name << btPairedDevices[i].address;
|
2017-06-06 02:41:57 +00:00
|
|
|
}
|
2017-07-18 21:49:42 +00:00
|
|
|
#if defined(Q_OS_IOS) || (defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID))
|
|
|
|
QTimer timer;
|
|
|
|
timer.setSingleShot(true);
|
|
|
|
connect(&timer, &QTimer::timeout, discoveryAgent, &QBluetoothDeviceDiscoveryAgent::stop);
|
|
|
|
timer.start(3000);
|
2017-06-06 02:41:57 +00:00
|
|
|
#endif
|
2017-07-18 21:49:14 +00:00
|
|
|
#if !defined(Q_OS_IOS)
|
2017-06-06 02:41:57 +00:00
|
|
|
} else {
|
|
|
|
qDebug() << "localBtDevice isn't valid";
|
2017-07-12 11:13:25 +00:00
|
|
|
m_btValid = false;
|
2017-06-06 02:41:57 +00:00
|
|
|
}
|
|
|
|
#endif
|
2017-07-18 21:49:14 +00:00
|
|
|
#endif
|
2017-06-06 02:41:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BTDiscovery::~BTDiscovery()
|
|
|
|
{
|
|
|
|
m_instance = NULL;
|
|
|
|
#if defined(BT_SUPPORT)
|
|
|
|
free(discoveryAgent);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
BTDiscovery *BTDiscovery::instance()
|
|
|
|
{
|
|
|
|
if (!m_instance)
|
|
|
|
m_instance = new BTDiscovery();
|
|
|
|
return m_instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(BT_SUPPORT)
|
2017-06-23 00:53:53 +00:00
|
|
|
#if defined(SSRF_CUSTOM_IO)
|
2017-06-06 02:41:57 +00:00
|
|
|
extern void addBtUuid(QBluetoothUuid uuid);
|
2017-06-23 00:53:53 +00:00
|
|
|
#endif
|
2017-06-06 02:41:57 +00:00
|
|
|
extern QHash<QString, QStringList> productList;
|
|
|
|
extern QStringList vendorList;
|
|
|
|
|
2017-06-30 02:43:00 +00:00
|
|
|
QString markBLEAddress(const QBluetoothDeviceInfo *device)
|
|
|
|
{
|
|
|
|
QBluetoothDeviceInfo::CoreConfigurations flags;
|
|
|
|
QString prefix = "";
|
|
|
|
|
|
|
|
flags = device->coreConfigurations();
|
|
|
|
if (flags == QBluetoothDeviceInfo::LowEnergyCoreConfiguration)
|
|
|
|
prefix = "LE:";
|
2017-07-19 13:15:30 +00:00
|
|
|
#if defined(Q_OS_IOS)
|
|
|
|
return prefix + device->deviceUuid().toString();
|
|
|
|
#else
|
2017-06-30 02:43:00 +00:00
|
|
|
return prefix + device->address().toString();
|
2017-07-19 13:15:30 +00:00
|
|
|
#endif
|
2017-06-30 02:43:00 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 02:41:57 +00:00
|
|
|
void BTDiscovery::btDeviceDiscovered(const QBluetoothDeviceInfo &device)
|
|
|
|
{
|
2017-06-23 00:53:53 +00:00
|
|
|
#if defined(SSRF_CUSTOM_IO)
|
2017-06-06 02:41:57 +00:00
|
|
|
btPairedDevice this_d;
|
2017-06-30 02:47:51 +00:00
|
|
|
this_d.address = markBLEAddress(&device);
|
2017-06-06 02:41:57 +00:00
|
|
|
this_d.name = device.name();
|
|
|
|
btPairedDevices.append(this_d);
|
|
|
|
|
|
|
|
QList<QBluetoothUuid> serviceUuids = device.serviceUuids();
|
|
|
|
foreach (QBluetoothUuid id, serviceUuids) {
|
|
|
|
addBtUuid(id);
|
|
|
|
qDebug() << id.toByteArray();
|
|
|
|
}
|
Mobile: wrap up fixes for BT download on Android
Major functional change in this commit is the addition of found static BT devices
to the internal administration (on Android), in a way that is equivalent to
mobile-on-desktop. So, in both cases, the list of devices in the app are
as in the list of devices on the host OS (Linux or Android). To minimize code
duplication, the btDeviceDiscovered slot is split in two parts, the part to
act as slot for the Qt BT discovery agent (Linux, so mobile-on-desktop), and
the part only needed for Android.
Remaining to be fixed: the correct handling of the QML UI selection of
vendor/product. The first default dive computer is correctly detected,
all paired devices from the virtual vendow can be selected, but clicking
through vendors results in non logical selections. It is obvious why
this is, but a fix is not straigforward at this point.
Signed-off-by: Jan Mulder <jlmulder@xs4all.nl>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2017-06-12 08:38:24 +00:00
|
|
|
|
|
|
|
btDeviceDiscoveredMain(this_d);
|
2017-06-23 00:53:53 +00:00
|
|
|
#endif
|
Mobile: wrap up fixes for BT download on Android
Major functional change in this commit is the addition of found static BT devices
to the internal administration (on Android), in a way that is equivalent to
mobile-on-desktop. So, in both cases, the list of devices in the app are
as in the list of devices on the host OS (Linux or Android). To minimize code
duplication, the btDeviceDiscovered slot is split in two parts, the part to
act as slot for the Qt BT discovery agent (Linux, so mobile-on-desktop), and
the part only needed for Android.
Remaining to be fixed: the correct handling of the QML UI selection of
vendor/product. The first default dive computer is correctly detected,
all paired devices from the virtual vendow can be selected, but clicking
through vendors results in non logical selections. It is obvious why
this is, but a fix is not straigforward at this point.
Signed-off-by: Jan Mulder <jlmulder@xs4all.nl>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2017-06-12 08:38:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BTDiscovery::btDeviceDiscoveredMain(const btPairedDevice &device)
|
|
|
|
{
|
|
|
|
btVendorProduct btVP;
|
|
|
|
|
|
|
|
QString newDevice;
|
|
|
|
dc_descriptor_t *newDC = getDeviceType(device.name);
|
|
|
|
if (newDC)
|
|
|
|
newDevice = dc_descriptor_get_product(newDC);
|
2017-07-17 04:49:11 +00:00
|
|
|
else
|
Mobile: wrap up fixes for BT download on Android
Major functional change in this commit is the addition of found static BT devices
to the internal administration (on Android), in a way that is equivalent to
mobile-on-desktop. So, in both cases, the list of devices in the app are
as in the list of devices on the host OS (Linux or Android). To minimize code
duplication, the btDeviceDiscovered slot is split in two parts, the part to
act as slot for the Qt BT discovery agent (Linux, so mobile-on-desktop), and
the part only needed for Android.
Remaining to be fixed: the correct handling of the QML UI selection of
vendor/product. The first default dive computer is correctly detected,
all paired devices from the virtual vendow can be selected, but clicking
through vendors results in non logical selections. It is obvious why
this is, but a fix is not straigforward at this point.
Signed-off-by: Jan Mulder <jlmulder@xs4all.nl>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2017-06-12 08:38:24 +00:00
|
|
|
newDevice = device.name;
|
|
|
|
|
|
|
|
qDebug() << "Found new device:" << newDevice << device.address;
|
2017-07-17 19:40:30 +00:00
|
|
|
if (newDC) {
|
|
|
|
QString vendor = dc_descriptor_get_vendor(newDC);
|
|
|
|
qDebug() << "this could be a " + vendor + " " + newDevice;
|
|
|
|
btVP.btpdi = device;
|
|
|
|
btVP.dcDescriptor = newDC;
|
|
|
|
btVP.vendorIdx = vendorList.indexOf(vendor);
|
|
|
|
btVP.productIdx = productList[vendor].indexOf(newDevice);
|
|
|
|
btDCs << btVP;
|
|
|
|
connectionListModel.addAddress(device.address + " (" + newDevice + ")");
|
|
|
|
return;
|
|
|
|
}
|
2017-07-17 04:49:11 +00:00
|
|
|
connectionListModel.addAddress(device.address);
|
2017-07-04 00:25:51 +00:00
|
|
|
qDebug() << "Not recognized as dive computer";
|
2017-06-06 02:41:57 +00:00
|
|
|
}
|
|
|
|
|
Mobile: wrap up fixes for BT download on Android
Major functional change in this commit is the addition of found static BT devices
to the internal administration (on Android), in a way that is equivalent to
mobile-on-desktop. So, in both cases, the list of devices in the app are
as in the list of devices on the host OS (Linux or Android). To minimize code
duplication, the btDeviceDiscovered slot is split in two parts, the part to
act as slot for the Qt BT discovery agent (Linux, so mobile-on-desktop), and
the part only needed for Android.
Remaining to be fixed: the correct handling of the QML UI selection of
vendor/product. The first default dive computer is correctly detected,
all paired devices from the virtual vendow can be selected, but clicking
through vendors results in non logical selections. It is obvious why
this is, but a fix is not straigforward at this point.
Signed-off-by: Jan Mulder <jlmulder@xs4all.nl>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2017-06-12 08:38:24 +00:00
|
|
|
QList<BTDiscovery::btVendorProduct> BTDiscovery::getBtDcs()
|
2017-06-06 02:41:57 +00:00
|
|
|
{
|
|
|
|
return btDCs;
|
|
|
|
}
|
|
|
|
|
2017-07-12 11:13:25 +00:00
|
|
|
bool BTDiscovery::btAvailable() const
|
|
|
|
{
|
|
|
|
return m_btValid;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-06-06 02:41:57 +00:00
|
|
|
// Android: As Qt is not able to pull the pairing data from a device, i
|
|
|
|
// a lengthy discovery process is needed to see what devices are paired. On
|
|
|
|
// https://forum.qt.io/topic/46075/solved-bluetooth-list-paired-devices
|
|
|
|
// user s.frings74 does, however, present a solution to this using JNI.
|
|
|
|
// Currently, this code is taken "as is".
|
|
|
|
|
|
|
|
#if defined(Q_OS_ANDROID)
|
|
|
|
void BTDiscovery::getBluetoothDevices()
|
|
|
|
{
|
|
|
|
struct BTDiscovery::btPairedDevice result;
|
|
|
|
// Query via Android Java API.
|
|
|
|
|
|
|
|
// returns a BluetoothAdapter
|
|
|
|
QAndroidJniObject adapter=QAndroidJniObject::callStaticObjectMethod("android/bluetooth/BluetoothAdapter","getDefaultAdapter","()Landroid/bluetooth/BluetoothAdapter;");
|
|
|
|
if (checkException("BluetoothAdapter.getDefaultAdapter()", &adapter)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// returns a Set<BluetoothDevice>
|
|
|
|
QAndroidJniObject pairedDevicesSet=adapter.callObjectMethod("getBondedDevices","()Ljava/util/Set;");
|
|
|
|
if (checkException("BluetoothAdapter.getBondedDevices()", &pairedDevicesSet)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
jint size=pairedDevicesSet.callMethod<jint>("size");
|
|
|
|
checkException("Set<BluetoothDevice>.size()", &pairedDevicesSet);
|
|
|
|
if (size > 0) {
|
|
|
|
// returns an Iterator<BluetoothDevice>
|
|
|
|
QAndroidJniObject iterator=pairedDevicesSet.callObjectMethod("iterator","()Ljava/util/Iterator;");
|
|
|
|
if (checkException("Set<BluetoothDevice>.iterator()", &iterator)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
// returns a BluetoothDevice
|
|
|
|
QAndroidJniObject dev=iterator.callObjectMethod("next","()Ljava/lang/Object;");
|
|
|
|
if (checkException("Iterator<BluetoothDevice>.next()", &dev)) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-06-30 05:53:07 +00:00
|
|
|
jint btType = dev.callMethod<jint>("getType", "()I");
|
2017-06-30 02:47:51 +00:00
|
|
|
result.address = dev.callObjectMethod("getAddress","()Ljava/lang/String;").toString();
|
2017-06-30 05:53:07 +00:00
|
|
|
if (btType == 2) // DEVICE_TYPE_LE
|
|
|
|
result.address = QString("LE:%1").arg(result.address);
|
2017-06-06 02:41:57 +00:00
|
|
|
result.name = dev.callObjectMethod("getName", "()Ljava/lang/String;").toString();
|
2017-06-30 05:53:07 +00:00
|
|
|
qDebug() << "paired Device type" << btType << "with address" << result.address;
|
2017-06-06 02:41:57 +00:00
|
|
|
btPairedDevices.append(result);
|
2017-07-03 09:16:18 +00:00
|
|
|
if (btType == 3) { // DEVICE_TYPE_DUAL
|
|
|
|
result.address = QString("LE:%1").arg(result.address);
|
|
|
|
qDebug() << "paired Device type" << btType << "with address" << result.address;
|
|
|
|
btPairedDevices.append(result);
|
|
|
|
}
|
2017-06-06 02:41:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BTDiscovery::checkException(const char* method, const QAndroidJniObject *obj)
|
|
|
|
{
|
|
|
|
static QAndroidJniEnvironment env;
|
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
if (env->ExceptionCheck()) {
|
|
|
|
qCritical("Exception in %s", method);
|
|
|
|
env->ExceptionDescribe();
|
|
|
|
env->ExceptionClear();
|
|
|
|
result=true;
|
|
|
|
}
|
|
|
|
if (!(obj == NULL || obj->isValid())) {
|
|
|
|
qCritical("Invalid object returned by %s", method);
|
|
|
|
result=true;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#endif // Q_OS_ANDROID
|
2017-06-10 12:22:28 +00:00
|
|
|
|
2017-06-06 02:41:57 +00:00
|
|
|
#endif // BT_SUPPORT
|