From 5142d7409f9dc9a102ae9b5898b43b57621c660c Mon Sep 17 00:00:00 2001 From: Jan Mulder Date: Sat, 10 Jun 2017 14:22:28 +0200 Subject: Mobile: add BT name to vendor/product capability This adds a central function to convert a BT name to a vendor/product pair known to Subsurface. This allows interfacing from a paired BT dive computer, without actively selecting its type, but by selecting it from the list of paired BT devices. So, after this, downloading from multiple (paired) DCs is also possible. And not the niced piece of code ... Signed-off-by: Jan Mulder Signed-off-by: Dirk Hohndel --- core/btdiscovery.cpp | 41 ++++++++++++++++++++++++++---- core/btdiscovery.h | 4 +++ core/downloadfromdcthread.cpp | 59 +++++++++++++++++++++++++++++++++++++++++-- core/downloadfromdcthread.h | 4 +++ 4 files changed, 101 insertions(+), 7 deletions(-) (limited to 'core') diff --git a/core/btdiscovery.cpp b/core/btdiscovery.cpp index 3159c3d30..5a93174a6 100644 --- a/core/btdiscovery.cpp +++ b/core/btdiscovery.cpp @@ -2,10 +2,36 @@ #include "btdiscovery.h" #include "downloadfromdcthread.h" +#include "core/libdivecomputer.h" #include +extern QMap descriptorLookup; + BTDiscovery *BTDiscovery::m_instance = NULL; +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"; + 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 + } + + if (!vendor.isEmpty() && !product.isEmpty()) + return(descriptorLookup[vendor + product]); + + return(NULL); +} + BTDiscovery::BTDiscovery(QObject *parent) { Q_UNUSED(parent) @@ -68,11 +94,13 @@ void BTDiscovery::btDeviceDiscovered(const QBluetoothDeviceInfo &device) btPairedDevices.append(this_d); struct btVendorProduct btVP; - QString newDevice = device.name(); + QString newDevice; + dc_descriptor_t *newDC = getDeviceType(device.name()); + if (newDC) + newDevice = dc_descriptor_get_product(newDC); + else + newDevice = device.name(); - // all the HW OSTC BT computers show up as "OSTC" + some other text, depending on model - if (newDevice.startsWith("OSTC")) - newDevice = "OSTC 3"; QList serviceUuids = device.serviceUuids(); foreach (QBluetoothUuid id, serviceUuids) { addBtUuid(id); @@ -80,11 +108,12 @@ void BTDiscovery::btDeviceDiscovered(const QBluetoothDeviceInfo &device) } qDebug() << "Found new device:" << newDevice << device.address(); QString vendor; - foreach (vendor, productList.keys()) { + if (newDC) foreach (vendor, productList.keys()) { if (productList[vendor].contains(newDevice)) { qDebug() << "this could be a " + vendor + " " + (newDevice == "OSTC 3" ? "OSTC family" : newDevice); btVP.btdi = device; + btVP.dcDescriptor = newDC; btVP.vendorIdx = vendorList.indexOf(vendor); btVP.productIdx = productList[vendor].indexOf(newDevice); qDebug() << "adding new btDCs entry (detected DC)" << newDevice << btVP.vendorIdx << btVP.productIdx << btVP.btdi.address();; @@ -95,6 +124,7 @@ void BTDiscovery::btDeviceDiscovered(const QBluetoothDeviceInfo &device) productList[QObject::tr("Paired Bluetooth Devices")].append(this_d.name + " (" + this_d.address.toString() + ")"); btVP.btdi = device; + btVP.dcDescriptor = newDC; btVP.vendorIdx = vendorList.indexOf(QObject::tr("Paired Bluetooth Devices")); btVP.productIdx = productList[QObject::tr("Paired Bluetooth Devices")].indexOf(this_d.name); qDebug() << "adding new btDCs entry (all paired)" << newDevice << btVP.vendorIdx << btVP.productIdx << btVP.btdi.address(); @@ -174,4 +204,5 @@ bool BTDiscovery::checkException(const char* method, const QAndroidJniObject *ob return result; } #endif // Q_OS_ANDROID + #endif // BT_SUPPORT diff --git a/core/btdiscovery.h b/core/btdiscovery.h index 1951b6672..301c4195d 100644 --- a/core/btdiscovery.h +++ b/core/btdiscovery.h @@ -9,13 +9,17 @@ #include #include #include +#include "core/libdivecomputer.h" struct btVendorProduct { QBluetoothDeviceInfo btdi; + dc_descriptor_t *dcDescriptor; int vendorIdx; int productIdx; }; +static dc_descriptor_t *getDeviceType(QString btName); + #endif #if defined(Q_OS_ANDROID) #include diff --git a/core/downloadfromdcthread.cpp b/core/downloadfromdcthread.cpp index 64933b5bc..a98ab17b2 100644 --- a/core/downloadfromdcthread.cpp +++ b/core/downloadfromdcthread.cpp @@ -249,7 +249,7 @@ int DCDeviceData::getDetectedVendorIndex(const QString ¤tText) QList btAllDevices = BTDiscovery::instance()->getBtAllDevices(); // Pick the vendor of the first confirmed find of a DC (if any), but - // only return a true vendow, and not our virtual one + // only return a true vendor, and not our virtual one if (!btDCs.isEmpty() && currentText != QObject::tr("Paired Bluetooth Devices")) { qDebug() << "getDetectedVendorIndex" << currentText << btDCs.first().vendorIdx; return btDCs.first().vendorIdx; @@ -311,7 +311,62 @@ QString DCDeviceData::getDetectedDeviceAddress(const QString ¤tVendorText, qDebug() << "getDetectedDeviceAddress" << btAddr; return btAddr; } - +#endif return QString(); +} + +QString DCDeviceData::getDeviceDescriptorVendor(const QString ¤tVendorText, + const QString ¤tProductText) +{ +#if defined(BT_SUPPORT) + QList btDCs = BTDiscovery::instance()->getBtDcs(); + QList btAllDevices = BTDiscovery::instance()->getBtAllDevices(); + + // Pull the vendor from the first found dive computer that is been + // detected as a possible real dive computer (and not some other paired + // BT device + if (currentVendorText != QObject::tr("Paired Bluetooth Devices") && !btDCs.isEmpty()) { + QString dcVendor = dc_descriptor_get_vendor(btDCs.first().dcDescriptor); + qDebug() << "getDeviceDescriptorVendor" << dcVendor; + return dcVendor; + } + + // if the above fails, pull vendor from the selected paired device + // unsure being a dive computer + if (currentVendorText == QObject::tr("Paired Bluetooth Devices")) { + int i = productList[currentVendorText].indexOf(currentProductText); + QString dcVendor = dc_descriptor_get_vendor(btAllDevices[i].dcDescriptor); + qDebug() << "getDeviceDescriptorVendor" << dcVendor; + return dcVendor; + } +#endif + return NULL; +} + +QString DCDeviceData::getDeviceDescriptorProduct(const QString ¤tVendorText, + const QString ¤tProductText) +{ +#if defined(BT_SUPPORT) + QList btDCs = BTDiscovery::instance()->getBtDcs(); + QList btAllDevices = BTDiscovery::instance()->getBtAllDevices(); + + // Pull the product from the first found dive computer that is been + // detected as a possible real dive computer (and not some other paired + // BT device + if (currentVendorText != QObject::tr("Paired Bluetooth Devices") && !btDCs.isEmpty()) { + QString dcProduct = dc_descriptor_get_product(btDCs.first().dcDescriptor); + qDebug() << "getDeviceDescriptorProduct" << dcProduct; + return dcProduct; + } + + // if the above fails, pull product from the selected paired device + // unsure being a dive computer + if (currentVendorText == QObject::tr("Paired Bluetooth Devices")) { + int i = productList[currentVendorText].indexOf(currentProductText); + QString dcProduct = dc_descriptor_get_product(btAllDevices[i].dcDescriptor); + qDebug() << "getDeviceDescriptorProduct" << dcProduct; + return dcProduct; + } #endif + return NULL; } diff --git a/core/downloadfromdcthread.h b/core/downloadfromdcthread.h index f606a7a74..912c33258 100644 --- a/core/downloadfromdcthread.h +++ b/core/downloadfromdcthread.h @@ -50,6 +50,10 @@ public: const QString ¤tProductText); Q_INVOKABLE QString getDetectedDeviceAddress(const QString ¤tVendorText, const QString ¤tProductText); + Q_INVOKABLE QString getDeviceDescriptorVendor(const QString ¤tVendorText, + const QString ¤tProductText); + Q_INVOKABLE QString getDeviceDescriptorProduct(const QString ¤tVendorText, + const QString ¤tProductText); public slots: void setVendor(const QString& vendor); -- cgit v1.2.3-70-g09d2