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 <alexander.blasche@qt.io>
This commit is contained in:
Timur Pocheptsov 2018-12-13 15:34:23 +01:00
parent 4a6999d8f8
commit 9f5c8e5253
5 changed files with 70 additions and 29 deletions

View File

@ -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);

View File

@ -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

View File

@ -44,7 +44,23 @@
#include <algorithm>
@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<QT_MANGLE_NAMESPACE(GCDTimerDelegate)> timeoutHandler;
bool cancelled;
}
- (instancetype)initWithDelegate:(id<QT_MANGLE_NAMESPACE(GCDTimerDelegate)>)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

View File

@ -58,25 +58,39 @@
#include <Foundation/Foundation.h>
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<QT_MANGLE_NAMESPACE(GCDTimerDelegate)> timeoutHandler;
bool cancelled;
}
@interface QT_MANGLE_NAMESPACE(OSXBTGCDTimer) : NSObject
- (instancetype)initWithDelegate:(id<QT_MANGLE_NAMESPACE(GCDTimerDelegate)>)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<GCDTimerObjC>;
}
} // namespace OSXBluetooth
QT_END_NAMESPACE

View File

@ -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];