diff options
Diffstat (limited to 'src/bluetooth/qbluetoothdevicediscoveryagent_osx.mm')
-rw-r--r-- | src/bluetooth/qbluetoothdevicediscoveryagent_osx.mm | 131 |
1 files changed, 39 insertions, 92 deletions
diff --git a/src/bluetooth/qbluetoothdevicediscoveryagent_osx.mm b/src/bluetooth/qbluetoothdevicediscoveryagent_osx.mm index 6ccdd0ff..2c4a7acd 100644 --- a/src/bluetooth/qbluetoothdevicediscoveryagent_osx.mm +++ b/src/bluetooth/qbluetoothdevicediscoveryagent_osx.mm @@ -50,10 +50,13 @@ #include "qbluetoothuuid.h" #include <QtCore/qloggingcategory.h> +#include <QtCore/qglobal.h> #include <QtCore/qstring.h> #include <QtCore/qdebug.h> #include <QtCore/qlist.h> +#include <Foundation/Foundation.h> +// Only after Foundation.h: #include "osx/corebluetoothwrapper_p.h" QT_BEGIN_NAMESPACE @@ -137,36 +140,32 @@ QBluetoothDeviceDiscoveryAgentPrivate::QBluetoothDeviceDiscoveryAgentPrivate(con lastError(QBluetoothDeviceDiscoveryAgent::NoError), inquiryType(QBluetoothDeviceDiscoveryAgent::GeneralUnlimitedInquiry) { - Q_ASSERT_X(q != Q_NULLPTR, "QBluetoothDeviceDiscoveryAgentPrivate()", - "invalid q_ptr (null)"); + Q_ASSERT_X(q != Q_NULLPTR, Q_FUNC_INFO, "invalid q_ptr (null)"); HostController controller([[IOBluetoothHostController defaultController] retain]); if (!controller || [controller powerState] != kBluetoothHCIPowerStateON) { - qCCritical(QT_BT_OSX) << "QBluetoothDeviceDiscoveryAgentPrivate() " - "no default host controller or adapter is off"; + qCCritical(QT_BT_OSX) << Q_FUNC_INFO << "no default host " + "controller or adapter is off"; return; } DeviceInquiry newInquiry([[DeviceInquiryObjC alloc]initWithDelegate:this]); if (!newInquiry) { // Obj-C's way of "reporting errors": - qCCritical(QT_BT_OSX) << "QBluetoothDeviceDiscoveryAgentPrivate() " - "failed to initialize an inquiry"; + qCCritical(QT_BT_OSX) << Q_FUNC_INFO << "failed to " + "initialize an inquiry"; return; } // OSXBTLEDeviceInquiry can be constructed even if LE is not supported - // at this stage it's only a memory allocation of the object itself, - // if it fails - we have some memory-related problems. + // if it fails - we have some memory-related problem. LEDeviceInquiry newInquiryLE([[LEDeviceInquiryObjC alloc] initWithDelegate:this]); if (!newInquiryLE) { - qCWarning(QT_BT_OSX) << "QBluetoothDeviceDiscoveryAgentPrivate() " - "failed to initialize a LE inquiry"; + qCWarning(QT_BT_OSX) << Q_FUNC_INFO << "failed to " + "initialize a LE inquiry"; return; } - qCDebug(QT_BT_OSX) << "host controller is in 'on' state, " - "discovery agent created successfully"; - hostController.reset(controller.take()); inquiry.reset(newInquiry.take()); inquiryLE.reset(newInquiryLE.take()); @@ -182,41 +181,28 @@ bool QBluetoothDeviceDiscoveryAgentPrivate::isValid() const // can fail to initialize some important data-members // (and the error is probably not even related to Bluetooth at all) // - say, allocation error - this is what meant here by valid/invalid. - - const bool valid = hostController && [hostController powerState] == kBluetoothHCIPowerStateON && inquiry; - qCDebug(QT_BT_OSX) << "private agent is valid state? "<<valid; - - if (hostController && [hostController powerState] != kBluetoothHCIPowerStateON) - qCWarning(QT_BT_OSX) << "adapter is powered off (was on)"; - return hostController && [hostController powerState] == kBluetoothHCIPowerStateON && inquiry; } bool QBluetoothDeviceDiscoveryAgentPrivate::isActive() const { - if (startPending) { - qCDebug(QT_BT_OSX) << "start is pending, isActive == true"; + if (startPending) return true; - } - if (stopPending) { - qCDebug(QT_BT_OSX) << "stop is pending, isActive == false"; - return false; - } - qCDebug(QT_BT_OSX)<<"isActive? "<< (agentState != NonActive); + if (stopPending) + return false; return agentState != NonActive; } void QBluetoothDeviceDiscoveryAgentPrivate::start() { - Q_ASSERT_X(isValid(), "start()", "called on invalid device discovery agent"); - Q_ASSERT_X(!isActive(), "start()", "called on active device discovery agent"); + Q_ASSERT_X(isValid(), Q_FUNC_INFO, "called on invalid device discovery agent"); + Q_ASSERT_X(!isActive(), Q_FUNC_INFO, "called on active device discovery agent"); Q_ASSERT_X(lastError != QBluetoothDeviceDiscoveryAgent::InvalidBluetoothAdapterError, - "start()", "called with an invalid Bluetooth adapter"); + Q_FUNC_INFO, "called with an invalid Bluetooth adapter"); if (stopPending) { - qCDebug(QT_BT_OSX) << "START: stop is pending, set start pending and return"; startPending = true; return; } @@ -228,44 +214,37 @@ void QBluetoothDeviceDiscoveryAgentPrivate::start() const IOReturn res = [inquiry start]; if (res != kIOReturnSuccess) { - qCDebug(QT_BT_OSX) << "START: private agent, failed to start"; setError(res, QObject::tr("device discovery agent: failed to start")); agentState = NonActive; emit q_ptr->error(lastError); - } else { - qCDebug(QT_BT_OSX) << "START: device inquiry started ..."; } } void QBluetoothDeviceDiscoveryAgentPrivate::startLE() { - Q_ASSERT_X(isValid(), "startLE()", "called on invalid device discovery agent"); + Q_ASSERT_X(isValid(), Q_FUNC_INFO, "called on invalid device discovery agent"); Q_ASSERT_X(lastError != QBluetoothDeviceDiscoveryAgent::InvalidBluetoothAdapterError, - "startLE()", "called with an invalid Bluetooth adapter"); + Q_FUNC_INFO, "called with an invalid Bluetooth adapter"); agentState = LEScan; if (![inquiryLE start]) { // We can be here only if we have some kind of resource allocation error, so we // do not emit finished, we emit error. - qCDebug(QT_BT_OSX) << "STARTLE: failed to start LE scan ..."; - setError(QBluetoothDeviceDiscoveryAgent::UnknownError, QObject::tr("device discovery agent, LE mode: " "resource allocation error")); agentState = NonActive; emit q_ptr->error(lastError); - } else { - qCDebug(QT_BT_OSX) << "STARTLE: scan started."; } } void QBluetoothDeviceDiscoveryAgentPrivate::stop() { - Q_ASSERT_X(isValid(), "stop()", "called on invalid device discovery agent"); - Q_ASSERT_X(isActive(), "stop()", "called whithout active inquiry"); + Q_ASSERT_X(isValid(), Q_FUNC_INFO, "called on invalid device discovery agent"); + Q_ASSERT_X(isActive(), Q_FUNC_INFO, "called whithout active inquiry"); Q_ASSERT_X(lastError != QBluetoothDeviceDiscoveryAgent::InvalidBluetoothAdapterError, - "stop()", "called with invalid bluetooth adapter"); + Q_FUNC_INFO, "called with invalid bluetooth adapter"); const bool prevStart = startPending; startPending = false; @@ -276,19 +255,15 @@ void QBluetoothDeviceDiscoveryAgentPrivate::stop() if (agentState == ClassicScan) { const IOReturn res = [inquiry stop]; if (res != kIOReturnSuccess) { - qCWarning(QT_BT_OSX) << "QBluetoothDeviceDiscoveryAgentPrivate::stop(), " - "failed to stop"; + qCWarning(QT_BT_OSX) << Q_FUNC_INFO << "failed to stop"; startPending = prevStart; stopPending = false; setError(res, QObject::tr("device discovery agent: failed to stop")); emit q_ptr->error(lastError); - } else { - qCDebug(QT_BT_OSX) << "stop success on a classic device inquiry"; } } else { // Can be asynchronous (depending on a status update of CBCentralManager). // The call itself is always 'success'. - qCDebug(QT_BT_OSX) << "trying to stop LE scan ..."; [inquiryLE stop]; } } @@ -297,19 +272,16 @@ void QBluetoothDeviceDiscoveryAgentPrivate::inquiryFinished(IOBluetoothDeviceInq { Q_UNUSED(inq) - Q_ASSERT_X(isValid(), "inquiryFinished", "invalid device discovery agent"); //We can never be here. - Q_ASSERT_X(q_ptr, "inquiryFinished", "invalid q_ptr (null)"); + Q_ASSERT_X(isValid(), Q_FUNC_INFO, "invalid device discovery agent"); //We can never be here. // The subsequent start(LE) function (if any) // will (re)set the correct state. agentState = NonActive; if (stopPending && !startPending) { - qCDebug(QT_BT_OSX) << "inquiryFinished, stop pending, no pending start, emit canceled"; stopPending = false; emit q_ptr->canceled(); } else if (startPending) { - qCDebug(QT_BT_OSX) << "inquiryFinished, NO stop pending, pending start, re-starting"; startPending = false; stopPending = false; start(); @@ -318,7 +290,6 @@ void QBluetoothDeviceDiscoveryAgentPrivate::inquiryFinished(IOBluetoothDeviceInq // finished in a normal way (not cancelled). // startLE() will take care of old devices // not supporting Bluetooth 4.0. - qCDebug(QT_BT_OSX)<<"CLASSIC inquiryFinished, NO stop pending, starting LE"; startLE(); } } @@ -327,16 +298,13 @@ void QBluetoothDeviceDiscoveryAgentPrivate::error(IOBluetoothDeviceInquiry *inq, { Q_UNUSED(inq) - Q_ASSERT_X(isValid(), "error", "invalid device discovery agent"); - - qCDebug(QT_BT_OSX)<<"ERROR: got a native error code: "<<int(error); + Q_ASSERT_X(isValid(), Q_FUNC_INFO, "invalid device discovery agent"); startPending = false; stopPending = false; setError(error); - Q_ASSERT_X(q_ptr, "error", "invalid q_ptr (null)"); emit q_ptr->error(lastError); } @@ -344,10 +312,9 @@ void QBluetoothDeviceDiscoveryAgentPrivate::deviceFound(IOBluetoothDeviceInquiry { Q_UNUSED(inq) - Q_ASSERT_X(isValid(), "deviceFound()", - "invalid device discovery agent"); - Q_ASSERT_X(device, "deviceFound()", "invalid IOBluetoothDevice (nil)"); - Q_ASSERT_X(agentState == ClassicScan, "deviceFound", + Q_ASSERT_X(isValid(), Q_FUNC_INFO, "invalid device discovery agent"); + Q_ASSERT_X(device, Q_FUNC_INFO, "invalid IOBluetoothDevice (nil)"); + Q_ASSERT_X(agentState == ClassicScan, Q_FUNC_INFO, "invalid agent state (expected classic scan)"); QT_BT_MAC_AUTORELEASEPOOL; @@ -355,8 +322,7 @@ void QBluetoothDeviceDiscoveryAgentPrivate::deviceFound(IOBluetoothDeviceInquiry // Let's collect some info about this device: const QBluetoothAddress deviceAddress(OSXBluetooth::qt_address([device getAddress])); if (deviceAddress.isNull()) { - qCWarning(QT_BT_OSX) << "QBluetoothDeviceDiscoveryAgentPrivate::deviceFound(), " - "invalid Bluetooth address"; + qCWarning(QT_BT_OSX) << Q_FUNC_INFO << "invalid Bluetooth address"; return; } @@ -413,7 +379,8 @@ void QBluetoothDeviceDiscoveryAgentPrivate::setError(QBluetoothDeviceDiscoveryAg } } - qCDebug(QT_BT_OSX) << "error set: "<<errorString; + if (lastError != QBluetoothDeviceDiscoveryAgent::NoError) + qCDebug(QT_BT_OSX) << "error set: "<<errorString; } void QBluetoothDeviceDiscoveryAgentPrivate::LEdeviceInquiryError(QBluetoothDeviceDiscoveryAgent::Error error) @@ -421,9 +388,7 @@ void QBluetoothDeviceDiscoveryAgentPrivate::LEdeviceInquiryError(QBluetoothDevic // At the moment the only error reported can be 'powered off' error, it happens // after the LE scan started (so we have LE support and this is a real PoweredOffError). Q_ASSERT_X(error == QBluetoothDeviceDiscoveryAgent::PoweredOffError, - "LEdeviceInquiryError", "unexpected error code"); - - qCDebug(QT_BT_OSX) << "LEDeviceInquiryError: powered off"; + Q_FUNC_INFO, "unexpected error code"); agentState = NonActive; setError(error); @@ -438,8 +403,6 @@ void QBluetoothDeviceDiscoveryAgentPrivate::LEnotSupported() // the user can call stop (setting a pending stop). // So the same rule apply: - qCDebug(QT_BT_OSX) << "LE not supported."; - LEdeviceInquiryFinished(); } @@ -447,8 +410,8 @@ void QBluetoothDeviceDiscoveryAgentPrivate::LEdeviceFound(CBPeripheral *peripher NSDictionary *advertisementData, NSNumber *RSSI) { - Q_ASSERT_X(peripheral, "LEdeviceFound()", "invalid peripheral (nil)"); - Q_ASSERT_X(agentState == LEScan, "LEdeviceFound", + Q_ASSERT_X(peripheral, Q_FUNC_INFO, "invalid peripheral (nil)"); + Q_ASSERT_X(agentState == LEScan, Q_FUNC_INFO, "invalid agent state, expected LE scan"); Q_UNUSED(advertisementData) @@ -473,16 +436,13 @@ void QBluetoothDeviceDiscoveryAgentPrivate::LEdeviceInquiryFinished() agentState = NonActive; if (stopPending && !startPending) { - qCDebug(QT_BT_OSX) << "LE scan finished, stop pending, NO start pending, emit canceled"; stopPending = false; emit q_ptr->canceled(); } else if (startPending) { - qCDebug(QT_BT_OSX) << "LE scan finished, start pending, NO stop pending, re-start"; startPending = false; stopPending = false; start(); //Start from a classic scan again. } else { - qCDebug(QT_BT_OSX) << "LE scan finished, emit finished"; emit q_ptr->finished(); } } @@ -557,44 +517,31 @@ void QBluetoothDeviceDiscoveryAgent::start() { if (d_ptr->lastError != InvalidBluetoothAdapterError) { if (d_ptr->isValid()) { - qCDebug(QT_BT_OSX) << "DDA::start?"; - if (!isActive()) { - qCDebug(QT_BT_OSX) << "DDA::start!"; + if (!isActive()) d_ptr->start(); - } } else { // We previously failed to initialize d_ptr correctly: // either some memory allocation problem or // no BT adapter found. - qCDebug(QT_BT_OSX) << "start failed, invalid d_ptr"; d_ptr->setError(InvalidBluetoothAdapterError); emit error(InvalidBluetoothAdapterError); } - } else - qCDebug(QT_BT_OSX) << "start failed, invalid adapter"; + } } void QBluetoothDeviceDiscoveryAgent::stop() { if (d_ptr->isValid()) { - qCDebug(QT_BT_OSX) << "DDA::stop, is valid"; - if (isActive() && d_ptr->lastError != InvalidBluetoothAdapterError) { - qCDebug(QT_BT_OSX) << "DDA::stop, is active and no error..."; + if (isActive() && d_ptr->lastError != InvalidBluetoothAdapterError) d_ptr->stop(); - } - } else { - qCDebug(QT_BT_OSX) << "DDA::stop, d_ptr is not in valid state, can not stop"; } } bool QBluetoothDeviceDiscoveryAgent::isActive() const { - qCDebug(QT_BT_OSX) << "DDA::isActive"; - if (d_ptr->isValid()) { + if (d_ptr->isValid()) return d_ptr->isActive(); - } else { - qCDebug(QT_BT_OSX) << "DDA::isActive, d_ptr is invalid"; - } + return false; } |