From 9f5c8e5253a742d148f713dcbb137ba26ffa4089 Mon Sep 17 00:00:00 2001 From: Timur Pocheptsov Date: Thu, 13 Dec 2018 15:34:23 +0100 Subject: GCD timer - refactor and fix warnings 1. We move the logic 'object we are watching after and the query's type' into the GCD timer, since we'll need several timers in osxbtcentralmanager (and after all it's not a timer really, it's 'a timeout watchdog' more like. 2. Move i-vars into the implementation to suppress compiler warnings. Task-number: QTBUG-72487 Change-Id: I090e4cc2e0e747211aae8ec91c4e0ff4a53f570b Reviewed-by: Alex Blasche --- src/bluetooth/osx/osxbtcentralmanager.mm | 4 ++- src/bluetooth/osx/osxbtcentralmanager_p.h | 12 --------- src/bluetooth/osx/osxbtgcdtimer.mm | 41 ++++++++++++++++++++++++++++--- src/bluetooth/osx/osxbtgcdtimer_p.h | 38 +++++++++++++++++++--------- src/bluetooth/osx/osxbtledeviceinquiry.mm | 4 ++- 5 files changed, 70 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/src/bluetooth/osx/osxbtcentralmanager.mm b/src/bluetooth/osx/osxbtcentralmanager.mm index 9254bd98..75d9f5c0 100644 --- a/src/bluetooth/osx/osxbtcentralmanager.mm +++ b/src/bluetooth/osx/osxbtcentralmanager.mm @@ -197,8 +197,10 @@ QT_END_NAMESPACE timeoutType = OSXBluetooth::OperationTimeout::none; } -- (void)timeout +- (void)timeout:(id)sender { + Q_UNUSED(sender) + using namespace OSXBluetooth; Q_ASSERT(objectUnderWatch); diff --git a/src/bluetooth/osx/osxbtcentralmanager_p.h b/src/bluetooth/osx/osxbtcentralmanager_p.h index e172d874..a1d2db66 100644 --- a/src/bluetooth/osx/osxbtcentralmanager_p.h +++ b/src/bluetooth/osx/osxbtcentralmanager_p.h @@ -87,18 +87,6 @@ enum CentralManagerState CentralManagerDisconnecting }; -enum class OperationTimeout -{ - none, - serviceDiscovery, - includedServicesDiscovery, - characteristicsDiscovery, - characteristicRead, - descriptorsDiscovery, - descriptorRead, - characteristicWrite -}; - // In Qt we work with handles and UUIDs. Core Bluetooth // has NSArrays (and nested NSArrays inside servces/characteristics). // To simplify a navigation, I need a simple way to map from a handle diff --git a/src/bluetooth/osx/osxbtgcdtimer.mm b/src/bluetooth/osx/osxbtgcdtimer.mm index 095f8680..9663ca93 100644 --- a/src/bluetooth/osx/osxbtgcdtimer.mm +++ b/src/bluetooth/osx/osxbtgcdtimer.mm @@ -44,7 +44,23 @@ #include -@implementation QT_MANGLE_NAMESPACE(OSXBTGCDTimer) +QT_USE_NAMESPACE +using namespace OSXBluetooth; + +@implementation QT_MANGLE_NAMESPACE(OSXBTGCDTimer) { +@private + qint64 timeoutMS; + qint64 timeoutStepMS; + + // Optional: + id objectUnderWatch; + OperationTimeout timeoutType; + + QElapsedTimer timer; + id timeoutHandler; + + bool cancelled; +} - (instancetype)initWithDelegate:(id)delegate { @@ -52,11 +68,19 @@ timeoutHandler = delegate; timeoutMS = 0; timeoutStepMS = 0; + objectUnderWatch = nil; + timeoutType = OperationTimeout::none; cancelled = false; } return self; } +- (void)watchAfter:(id)object withTimeoutType:(OperationTimeout)type +{ + objectUnderWatch = object; + timeoutType = type; +} + - (void)startWithTimeout:(qint64)ms step:(qint64)stepMS { Q_ASSERT(!timeoutMS && !timeoutStepMS); @@ -86,9 +110,8 @@ const qint64 elapsed = timer.elapsed(); if (elapsed >= timeoutMS) { - [timeoutHandler timeout]; + [timeoutHandler timeout:self]; } else { - using namespace QT_PREPEND_NAMESPACE(OSXBluetooth); // Re-schedule: dispatch_queue_t leQueue(qt_LE_queue()); Q_ASSERT(leQueue); @@ -106,6 +129,18 @@ { cancelled = true; timeoutHandler = nil; + objectUnderWatch = nil; + timeoutType = OperationTimeout::none; +} + +- (id)objectUnderWatch +{ + return objectUnderWatch; +} + +- (OperationTimeout)timeoutType +{ + return timeoutType; } @end diff --git a/src/bluetooth/osx/osxbtgcdtimer_p.h b/src/bluetooth/osx/osxbtgcdtimer_p.h index 007a004b..153e9033 100644 --- a/src/bluetooth/osx/osxbtgcdtimer_p.h +++ b/src/bluetooth/osx/osxbtgcdtimer_p.h @@ -58,25 +58,39 @@ #include +QT_BEGIN_NAMESPACE + +namespace OSXBluetooth { + +enum class OperationTimeout +{ + none, + serviceDiscovery, + includedServicesDiscovery, + characteristicsDiscovery, + characteristicRead, + descriptorsDiscovery, + descriptorRead, + characteristicWrite +}; + +} // namespace OSXBluetooth + +QT_END_NAMESPACE + @protocol QT_MANGLE_NAMESPACE(GCDTimerDelegate) @required -- (void)timeout; +- (void)timeout:(id)sender; @end -@interface QT_MANGLE_NAMESPACE(OSXBTGCDTimer) : NSObject { -@private - qint64 timeoutMS; - qint64 timeoutStepMS; - QT_PREPEND_NAMESPACE(QElapsedTimer) timer; - id timeoutHandler; - bool cancelled; -} - +@interface QT_MANGLE_NAMESPACE(OSXBTGCDTimer) : NSObject - (instancetype)initWithDelegate:(id)delegate; +- (void)watchAfter:(id)object withTimeoutType:(QT_PREPEND_NAMESPACE(OSXBluetooth)::OperationTimeout)type; - (void)startWithTimeout:(qint64)ms step:(qint64)stepMS; - (void)handleTimeout; - (void)cancelTimer; - +- (id)objectUnderWatch; +- (QT_PREPEND_NAMESPACE(OSXBluetooth)::OperationTimeout)timeoutType; @end QT_BEGIN_NAMESPACE @@ -86,7 +100,7 @@ namespace OSXBluetooth { using GCDTimerObjC = QT_MANGLE_NAMESPACE(OSXBTGCDTimer); using GCDTimer = ObjCScopedPointer; -} +} // namespace OSXBluetooth QT_END_NAMESPACE diff --git a/src/bluetooth/osx/osxbtledeviceinquiry.mm b/src/bluetooth/osx/osxbtledeviceinquiry.mm index e61968ed..c82cb31d 100644 --- a/src/bluetooth/osx/osxbtledeviceinquiry.mm +++ b/src/bluetooth/osx/osxbtledeviceinquiry.mm @@ -151,8 +151,10 @@ QT_USE_NAMESPACE [super dealloc]; } -- (void)timeout +- (void)timeout:(id)sender { + Q_UNUSED(sender) + if (internalState == InquiryActive) { [manager stopScan]; [manager setDelegate:nil]; -- cgit v1.2.3