From 6b6b4fea87b7b9063299e4b15adf307f71319cf1 Mon Sep 17 00:00:00 2001 From: Timur Pocheptsov Date: Thu, 2 Jun 2016 12:30:54 +0200 Subject: QtBluetooth: remove outdate code (OS X/iOS) Since we now have OS X 10.9 and iOS 7.0 as the minimal required (SDK and deployment target) - get rid of outdated code-branches in _SDK_EQUAL_OR_ABOVE etc. Change-Id: I5f546e8bb746f6b94f26a40460b3caa0af6a531c Reviewed-by: Alex Blasche --- src/bluetooth/osx/osxbtcentralmanager.mm | 95 ++++++------------------------ src/bluetooth/osx/osxbtdevicepair.mm | 13 +--- src/bluetooth/osx/osxbtledeviceinquiry.mm | 77 +++--------------------- src/bluetooth/qbluetoothserviceinfo_osx.mm | 92 ++--------------------------- src/bluetooth/qlowenergycontroller_osx.mm | 58 +++++++----------- 5 files changed, 53 insertions(+), 282 deletions(-) (limited to 'src') diff --git a/src/bluetooth/osx/osxbtcentralmanager.mm b/src/bluetooth/osx/osxbtcentralmanager.mm index 3b0e098d..b4c946af 100644 --- a/src/bluetooth/osx/osxbtcentralmanager.mm +++ b/src/bluetooth/osx/osxbtcentralmanager.mm @@ -43,7 +43,6 @@ #include "osxbtnotifier_p.h" #include -#include #include #include @@ -209,56 +208,31 @@ QT_END_NAMESPACE return; } -#if QT_MAC_PLATFORM_SDK_EQUAL_OR_ABOVE(__MAC_10_9, __IPHONE_7_0) - if (QSysInfo::MacintoshVersion >= qt_OS_limit(QSysInfo::MV_10_9, QSysInfo::MV_IOS_7_0)) { - const quint128 qtUuidData(deviceUuid.toUInt128()); - // STATIC_ASSERT on sizes would be handy! - uuid_t uuidData = {}; - std::copy(qtUuidData.data, qtUuidData.data + 16, uuidData); - const ObjCScopedPointer nsUuid([[NSUUID alloc] initWithUUIDBytes:uuidData]); - if (!nsUuid) { - qCWarning(QT_BT_OSX) << Q_FUNC_INFO << "failed to allocate NSUUID identifier"; - if (notifier) - emit notifier->CBManagerError(QLowEnergyController::ConnectionError); - return; - } - - [uuids addObject:nsUuid]; - // With the latest CoreBluetooth, we can synchronously retrive peripherals: - QT_BT_MAC_AUTORELEASEPOOL; - NSArray *const peripherals = [manager retrievePeripheralsWithIdentifiers:uuids]; - if (!peripherals || peripherals.count != 1) { - qCWarning(QT_BT_OSX) << Q_FUNC_INFO << "failed to retrive a peripheral"; - if (notifier) - emit notifier->CBManagerError(QLowEnergyController::UnknownRemoteDeviceError); - return; - } - peripheral = [static_cast([peripherals objectAtIndex:0]) retain]; - [self connectToPeripheral]; + const quint128 qtUuidData(deviceUuid.toUInt128()); + uuid_t uuidData = {}; + std::copy(qtUuidData.data, qtUuidData.data + 16, uuidData); + const ObjCScopedPointer nsUuid([[NSUUID alloc] initWithUUIDBytes:uuidData]); + if (!nsUuid) { + qCWarning(QT_BT_OSX) << Q_FUNC_INFO << "failed to allocate NSUUID identifier"; + if (notifier) + emit notifier->CBManagerError(QLowEnergyController::ConnectionError); return; } -#endif - // Either SDK or the target is below 10.9/7.0 - if (![manager respondsToSelector:@selector(retrievePeripherals:)]) { + + [uuids addObject:nsUuid]; + // With the latest CoreBluetooth, we can synchronously retrive peripherals: + QT_BT_MAC_AUTORELEASEPOOL; + NSArray *const peripherals = [manager retrievePeripheralsWithIdentifiers:uuids]; + if (!peripherals || peripherals.count != 1) { qCWarning(QT_BT_OSX) << Q_FUNC_INFO << "failed to retrive a peripheral"; if (notifier) emit notifier->CBManagerError(QLowEnergyController::UnknownRemoteDeviceError); return; } - OSXBluetooth::CFStrongReference cfUuid(OSXBluetooth::cf_uuid(deviceUuid)); - if (!cfUuid) { - qCWarning(QT_BT_OSX) << Q_FUNC_INFO << "failed to create CFUUID object"; - if (notifier) - emit notifier->CBManagerError(QLowEnergyController::ConnectionError); - return; - } - // With ARC this cast will be illegal: - [uuids addObject:(id)cfUuid.data()]; - // Unfortunately, with old Core Bluetooth this call is asynchronous ... - managerState = OSXBluetooth::CentralManagerConnecting; - [manager performSelector:@selector(retrievePeripherals:) withObject:uuids.data()]; + peripheral = [static_cast([peripherals objectAtIndex:0]) retain]; + [self connectToPeripheral]; } - (void)connectToPeripheral @@ -285,18 +259,7 @@ QT_END_NAMESPACE if (!peripheral) return false; -#if QT_MAC_PLATFORM_SDK_EQUAL_OR_ABOVE(__MAC_10_9, __IPHONE_7_0) - using OSXBluetooth::qt_OS_limit; - - if (QSysInfo::MacintoshVersion >= qt_OS_limit(QSysInfo::MV_10_9, QSysInfo::MV_IOS_7_0)) - return peripheral.state == CBPeripheralStateConnected; -#endif - // Either SDK or the target is below 10.9/7.0 ... - if (![peripheral respondsToSelector:@selector(isConnected)]) - return false; - - // Ugly cast to deal with id being a pointer ... - return reinterpret_cast([peripheral performSelector:@selector(isConnected)]); + return peripheral.state == CBPeripheralStateConnected; } - (void)disconnectFromDevice @@ -1132,30 +1095,6 @@ QT_END_NAMESPACE } } -- (void)centralManager:(CBCentralManager *)central didRetrievePeripherals:(NSArray *)peripherals -{ - Q_UNUSED(central) - - // This method is required for iOS before 7.0 and OS X below 10.9. - Q_ASSERT_X(manager, Q_FUNC_INFO, "invalid central manager (nil)"); - - if (managerState != OSXBluetooth::CentralManagerConnecting) { - // Canceled by calling -disconnectFromDevice method. - return; - } - - managerState = OSXBluetooth::CentralManagerIdle; - - if (!peripherals || peripherals.count != 1) { - qCDebug(QT_BT_OSX) << Q_FUNC_INFO <<"unexpected number of peripherals (!= 1)"; - if (notifier) - emit notifier->CBManagerError(QLowEnergyController::UnknownRemoteDeviceError); - } else { - peripheral = [static_cast([peripherals objectAtIndex:0]) retain]; - [self connectToPeripheral]; - } -} - - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)aPeripheral { Q_UNUSED(central) diff --git a/src/bluetooth/osx/osxbtdevicepair.mm b/src/bluetooth/osx/osxbtdevicepair.mm index 737a312f..c9458255 100644 --- a/src/bluetooth/osx/osxbtdevicepair.mm +++ b/src/bluetooth/osx/osxbtdevicepair.mm @@ -41,7 +41,6 @@ #include "osxbtutility_p.h" #include -#include #include QT_BEGIN_NAMESPACE @@ -91,18 +90,8 @@ using namespace QT_NAMESPACE; - (void)dealloc { -#if QT_OSX_PLATFORM_SDK_EQUAL_OR_ABOVE(__MAC_10_9) - // Stop also sets a delegate to nil (Apple's docs). - // 10.9 only. - if (QSysInfo::MacintoshVersion >= QSysInfo::MV_10_9) - [m_pairing stop]; - else - [m_pairing setDelegate:nil]; -#else - [m_pairing setDelegate:nil]; -#endif + [m_pairing stop]; [m_pairing release]; - [super dealloc]; } diff --git a/src/bluetooth/osx/osxbtledeviceinquiry.mm b/src/bluetooth/osx/osxbtledeviceinquiry.mm index 88370fa6..3539ccf3 100644 --- a/src/bluetooth/osx/osxbtledeviceinquiry.mm +++ b/src/bluetooth/osx/osxbtledeviceinquiry.mm @@ -43,7 +43,6 @@ #include "osxbtutility_p.h" #include -#include #include #include "corebluetoothwrapper_p.h" @@ -52,8 +51,6 @@ QT_BEGIN_NAMESPACE namespace OSXBluetooth { -#if QT_MAC_PLATFORM_SDK_EQUAL_OR_ABOVE(__MAC_10_9, __IPHONE_6_0) - QBluetoothUuid qt_uuid(NSUUID *nsUuid) { if (!nsUuid) @@ -66,38 +63,6 @@ QBluetoothUuid qt_uuid(NSUUID *nsUuid) return QBluetoothUuid(qtUuidData); } -#endif - -QBluetoothUuid qt_uuid(CFUUIDRef uuid) -{ - if (!uuid) - return QBluetoothUuid(); - - const CFUUIDBytes data = CFUUIDGetUUIDBytes(uuid); - quint128 qtUuidData = {{data.byte0, data.byte1, data.byte2, data.byte3, - data.byte4, data.byte5, data.byte6, data.byte7, - data.byte8, data.byte9, data.byte10, data.byte11, - data.byte12, data.byte13, data.byte14, data.byte15}}; - - return QBluetoothUuid(qtUuidData); -} - -typedef ObjCStrongReference StringStrongReference; - -StringStrongReference uuid_as_nsstring(CFUUIDRef uuid) -{ - // We use the UUDI's string representation as a key in a dictionary. - if (!uuid) - return StringStrongReference(); - - CFStringRef cfStr = CFUUIDCreateString(kCFAllocatorDefault, uuid); - if (!cfStr) - return StringStrongReference(); - - // Imporant: with ARC this will require a different cast/ownership! - return StringStrongReference((NSString *)cfStr, false); -} - } @@ -301,45 +266,21 @@ using namespace QT_NAMESPACE; QBluetoothUuid deviceUuid; -#if QT_MAC_PLATFORM_SDK_EQUAL_OR_ABOVE(__MAC_10_9, __IPHONE_7_0) - if (QSysInfo::MacintoshVersion >= qt_OS_limit(QSysInfo::MV_10_9, QSysInfo::MV_IOS_7_0)) { - if (!peripheral.identifier) { - qCWarning(QT_BT_OSX) << Q_FUNC_INFO << "peripheral without NSUUID"; - return; - } - if ([uuids containsObject:peripheral.identifier]) { - // We already know this peripheral ... - return; - } - [uuids addObject:peripheral.identifier]; - deviceUuid = OSXBluetooth::qt_uuid(peripheral.identifier); + if (!peripheral.identifier) { + qCWarning(QT_BT_OSX) << Q_FUNC_INFO << "peripheral without NSUUID"; + return; } -#endif - // Either SDK or the target is below 10.9/7.0: - // The property UUID was finally removed in iOS 9, we have - // to avoid compilation errors ... - if (deviceUuid.isNull()) { - CFUUIDRef cfUUID = Q_NULLPTR; - if ([peripheral respondsToSelector:@selector(UUID)]) { - // This will require a bridged cast if we switch to ARC ... - cfUUID = reinterpret_cast([peripheral performSelector:@selector(UUID)]); - } - - if (!cfUUID) { - qCWarning(QT_BT_OSX) << Q_FUNC_INFO << "peripheral without CFUUID"; - return; - } - - StringStrongReference key(uuid_as_nsstring(cfUUID)); - if ([uuids containsObject:key.data()]) - return; // We've seen this peripheral before ... - [uuids addObject:key.data()]; - deviceUuid = OSXBluetooth::qt_uuid(cfUUID); + if ([uuids containsObject:peripheral.identifier]) { + // We already know this peripheral ... + return; } + [uuids addObject:peripheral.identifier]; + deviceUuid = OSXBluetooth::qt_uuid(peripheral.identifier); + if (deviceUuid.isNull()) { qCWarning(QT_BT_OSX) << Q_FUNC_INFO << "no way to address peripheral, QBluetoothUuid is null"; return; diff --git a/src/bluetooth/qbluetoothserviceinfo_osx.mm b/src/bluetooth/qbluetoothserviceinfo_osx.mm index f2cba6cf..c51eb1c6 100644 --- a/src/bluetooth/qbluetoothserviceinfo_osx.mm +++ b/src/bluetooth/qbluetoothserviceinfo_osx.mm @@ -45,7 +45,6 @@ #include #include -#include #include #include #include @@ -57,44 +56,6 @@ QT_BEGIN_NAMESPACE -namespace { - -// This is not in osxbtutility_p, since it's not required -// in general and just fixes the problem with SDK < 10.9, -// where we have to care about about IOBluetoothSDPServiceRecordRef. -class ServiceRecordRefGuard -{ -public: - ServiceRecordRefGuard() - : recordRef(Q_NULLPTR) - { - } - explicit ServiceRecordRefGuard(IOBluetoothSDPServiceRecordRef r) - : recordRef(r) - { - } - ~ServiceRecordRefGuard() - { - if (recordRef) // Requires non-NULL pointers. - CFRelease(recordRef); - } - - void reset(IOBluetoothSDPServiceRecordRef r) - { - if (recordRef) - CFRelease(recordRef); - // Take the ownership: - recordRef = r; - } - -private: - IOBluetoothSDPServiceRecordRef recordRef; - - Q_DISABLE_COPY(ServiceRecordRefGuard) -}; - -} - class QBluetoothServiceInfoPrivate { public: @@ -149,44 +110,19 @@ bool QBluetoothServiceInfoPrivate::registerService(const QBluetoothAddress &loca return false; } - ServiceRecordRefGuard refGuard; SDPRecord newRecord; -#if QT_OSX_PLATFORM_SDK_EQUAL_OR_ABOVE(__MAC_10_9) - if (QSysInfo::MacintoshVersion >= QSysInfo::MV_10_9) { - newRecord.reset([[IOBluetoothSDPServiceRecord - publishedServiceRecordWithDictionary:serviceDict] retain]); - } else { -#else - { -#endif - IOBluetoothSDPServiceRecordRef recordRef = Q_NULLPTR; - // With ARC this will require a different cast? - const IOReturn status = IOBluetoothAddServiceDict((CFDictionaryRef)serviceDict.data(), &recordRef); - if (status != kIOReturnSuccess) { - qCWarning(QT_BT_OSX) << Q_FUNC_INFO << "failed to register a service record"; - return false; - } - - refGuard.reset(recordRef); - newRecord.reset([[IOBluetoothSDPServiceRecord withSDPServiceRecordRef:recordRef] retain]); - // It's weird, but ... it's not possible to release a record ref yet. - } + newRecord.reset([[IOBluetoothSDPServiceRecord + publishedServiceRecordWithDictionary:serviceDict] retain]); if (!newRecord) { qCWarning(QT_BT_OSX) << Q_FUNC_INFO << "failed to register a service record"; - // In case of SDK < 10.9 it's not possible to remove a service record ... - // no way to obtain record handle yet. return false; } BluetoothSDPServiceRecordHandle newRecordHandle = 0; if ([newRecord getServiceRecordHandle:&newRecordHandle] != kIOReturnSuccess) { qCWarning(QT_BT_OSX) << Q_FUNC_INFO << "failed to register a service record"; -#if QT_OSX_PLATFORM_SDK_EQUAL_OR_ABOVE(__MAC_10_9) - if (QSysInfo::MacintoshVersion >= QSysInfo::MV_10_9) - [newRecord removeServiceRecord]; -#endif - // With SDK < 10.9 there is no way to unregister at this point ... + [newRecord removeServiceRecord]; return false; } @@ -212,16 +148,7 @@ bool QBluetoothServiceInfoPrivate::registerService(const QBluetoothAddress &loca } if (!configured) { -#if QT_OSX_PLATFORM_SDK_EQUAL_OR_ABOVE(__MAC_10_9) - if (QSysInfo::MacintoshVersion >= QSysInfo::MV_10_9) { - [newRecord removeServiceRecord]; - } else { -#else - {// Just to balance braces ... -#endif - IOBluetoothRemoveServiceWithRecordHandle(newRecordHandle); - } - + [newRecord removeServiceRecord]; qCWarning(QT_BT_OSX) << Q_FUNC_INFO << "failed to register a service record"; return false; } @@ -248,16 +175,7 @@ bool QBluetoothServiceInfoPrivate::unregisterService() Q_ASSERT_X(serviceRecord, Q_FUNC_INFO, "service registered, but serviceRecord is nil"); -#if QT_OSX_PLATFORM_SDK_EQUAL_OR_ABOVE(__MAC_10_9) - if (QSysInfo::MacintoshVersion >= QSysInfo::MV_10_9) { - [serviceRecord removeServiceRecord]; - } else { -#else - { -#endif - // Assert on newRecordHandle? Is 0 a valid/invalid handle? - IOBluetoothRemoveServiceWithRecordHandle(serviceRecordHandle); - } + [serviceRecord removeServiceRecord]; serviceRecord.reset(nil); diff --git a/src/bluetooth/qlowenergycontroller_osx.mm b/src/bluetooth/qlowenergycontroller_osx.mm index 97c154d7..6ab257ea 100644 --- a/src/bluetooth/qlowenergycontroller_osx.mm +++ b/src/bluetooth/qlowenergycontroller_osx.mm @@ -54,7 +54,6 @@ #include #include #include -#include #include #include #include @@ -105,18 +104,13 @@ ServicePrivate qt_createLEService(QLowEnergyControllerPrivateOSX *controller, CB // TODO: isPrimary is ... always 'NO' - to be investigated. /* - #if QT_MAC_PLATFORM_SDK_EQUAL_OR_ABOVE(__MAC_10_9, __IPHONE_6_0) using OSXBluetooth::qt_OS_limit; - if (QSysInfo::MacintoshVersion >= qt_OS_limit(QSysInfo::MV_10_9, QSysInfo::MV_IOS_6_0)) { - if (!cbService.isPrimary) { - // Our guess included/not was probably wrong. - newService->type &= ~QLowEnergyService::PrimaryService; - newService->type |= QLowEnergyService::IncludedService; - } + if (!cbService.isPrimary) { + // Our guess included/not was probably wrong. + newService->type &= ~QLowEnergyService::PrimaryService; + newService->type |= QLowEnergyService::IncludedService; } - #endif */ - // No such property before 10_9/6_0. return newService; } @@ -163,21 +157,17 @@ QLowEnergyControllerPrivateOSX::QLowEnergyControllerPrivateOSX(QLowEnergyControl QScopedPointer notifier(new LECBManagerNotifier); if (role == QLowEnergyController::PeripheralRole) { #ifndef Q_OS_TVOS - if (QSysInfo::MacintoshVersion >= qt_OS_limit(QSysInfo::MV_10_9, QSysInfo::MV_IOS_6_0)) { - peripheralManager.reset([[ObjCPeripheralManager alloc] initWith:notifier.data()]); - if (!peripheralManager) { - qCWarning(QT_BT_OSX) << Q_FUNC_INFO - << "failed to initialize peripheral manager"; - return; - } - } else { -#else - { -#endif + peripheralManager.reset([[ObjCPeripheralManager alloc] initWith:notifier.data()]); + if (!peripheralManager) { qCWarning(QT_BT_OSX) << Q_FUNC_INFO - << "peripheral role is not supported on your platform"; + << "failed to initialize peripheral manager"; return; } +#else + qCWarning(QT_BT_OSX) << Q_FUNC_INFO + << "peripheral role is not supported on your platform"; + return; +#endif } else { centralManager.reset([[ObjCCentralManager alloc] initWith:notifier.data()]); if (!centralManager) { @@ -205,12 +195,10 @@ QLowEnergyControllerPrivateOSX::~QLowEnergyControllerPrivateOSX() }); } else { #ifndef Q_OS_TVOS - if (QSysInfo::MacintoshVersion >= qt_OS_limit(QSysInfo::MV_10_9, QSysInfo::MV_IOS_6_0)) { - const auto manager = peripheralManager.data(); - dispatch_sync(leQueue, ^{ - [manager detach]; - }); - } + const auto manager = peripheralManager.data(); + dispatch_sync(leQueue, ^{ + [manager detach]; + }); #endif } } @@ -731,18 +719,14 @@ void QLowEnergyControllerPrivateOSX::writeCharacteristic(QSharedPointer= qt_OS_limit(QSysInfo::MV_10_9, QSysInfo::MV_IOS_6_0)) { - const auto manager = peripheralManager.data(); - dispatch_async(leQueue, ^{ - [manager write:newValueCopy charHandle:charHandle]; - }); - } else { + const auto manager = peripheralManager.data(); + dispatch_async(leQueue, ^{ + [manager write:newValueCopy charHandle:charHandle]; + }); #else - { -#endif qCWarning(QT_BT_OSX) << Q_FUNC_INFO << "peripheral role is not supported on your platform"; - } +#endif } } -- cgit v1.2.3