-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathNBCoreBluetoothAPIMisuseGuard.m
169 lines (150 loc) · 6.42 KB
/
NBCoreBluetoothAPIMisuseGuard.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//
// NBCoreBluetoothAPIMisuseGuard.m
//
// Created by Nick Brook on 03/05/2016.
// Copyright © 2016 Nick Brook. All rights reserved.
//
#import <CoreBluetooth/CoreBluetooth.h>
#import <objc/runtime.h>
#import <objc/message.h>
#if DEBUG
static BOOL isCentralPoweredOn(CBCentralManager * c) {
#if TARGET_OS_IPHONE
if(@available(iOS 10.0, *)) {
return c.state == CBManagerStatePoweredOn;
} else {
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_10_0
return c.state == CBCentralManagerStatePoweredOn;
#else
return NO;
#endif
}
#else
return c.state == CBCentralManagerStatePoweredOn;
#endif
}
static BOOL isPeripheralPoweredOn(CBPeripheralManager * c) {
#if TARGET_OS_IPHONE
if(@available(iOS 10.0, *)) {
return c.state == CBManagerStatePoweredOn;
} else {
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_10_0
return c.state == CBPeripheralManagerStatePoweredOn;
#else
return NO;
#endif
}
#else
return c.state == CBPeripheralManagerStatePoweredOn;
#endif
}
static void swizzleForwarding(Class class, NSString * swizzledPrefix, void (^before)(NSInvocation * invocation), void (^after)(NSInvocation * invocation)) {
SEL selector = @selector(forwardInvocation:);
Method method = class_getInstanceMethod(class, selector);
void(^block)(id self, NSInvocation *) = ^void(id self, NSInvocation * invocation) {
NSString *origSelectorName = NSStringFromSelector(invocation.selector);
NSString *altSelectorName = [NSString stringWithFormat:@"%@%@", swizzledPrefix, origSelectorName];
SEL altSelector = NSSelectorFromString(altSelectorName);
if([self respondsToSelector:altSelector]) {
if(before) {
before(invocation);
}
invocation.selector = altSelector;
[invocation invoke];
if(after) {
after(invocation);
}
}
};
IMP newImp = imp_implementationWithBlock(block);
class_addMethod(class, selector, newImp, method_getTypeEncoding(method));
}
static void removeAllFromList(Class class, NSString *toSwizzlePrefix, SEL selectors[], unsigned int numSelectors) {
SEL selectorWithNoImplementation = sel_registerName("methodWhichMustNotExist::::");
IMP forwarderIMP = class_getMethodImplementation(class, selectorWithNoImplementation);
for (int i = 0; i < numSelectors; i++) {
Method originalMethod = class_getInstanceMethod(class, selectors[i]);
IMP originalIMP = method_getImplementation(originalMethod);
const char *types = method_getTypeEncoding(originalMethod);
NSString *aliasSelectorName = [NSString stringWithFormat:@"%@%@", toSwizzlePrefix, NSStringFromSelector(selectors[i])];
class_replaceMethod(class, selectors[i], forwarderIMP, types);
class_addMethod(class, NSSelectorFromString(aliasSelectorName), originalIMP, types);
}
}
static void surroundMethods(Class class, SEL *selectors, unsigned int numSelectors, void (^before)(NSInvocation * invocation), void (^after)(NSInvocation * invocation)) {
NSString *prefix = @"nb_";
swizzleForwarding(class, prefix, before, after);
removeAllFromList(class, prefix, selectors, numSelectors);
}
@implementation CBCentralManager (APIMisuseGuard)
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
SEL selectors[] = {
@selector(connectPeripheral:options:),
@selector(retrieveConnectedPeripheralsWithServices:),
@selector(cancelPeripheralConnection:),
@selector(scanForPeripheralsWithServices:options:),
@selector(stopScan)
};
surroundMethods(self.class, selectors, sizeof(selectors) / sizeof(SEL), ^(NSInvocation *invocation) {
CBCentralManager * s = invocation.target;
if(!isCentralPoweredOn(s)) {
NSString *stack = [[NSThread callStackSymbols] componentsJoinedByString:@"\n"];
NSAssert(NO, @"CBCentralManager was not in powered on state when %@ was called. State was %ld\n\nStacktrace:\n%@", NSStringFromSelector(invocation.selector), (long)s.state, stack);
}
}, nil);
});
}
@end
@implementation CBPeripheral (APIMisuseGuard)
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
SEL selectors[] = {
@selector(discoverServices:),
@selector(discoverIncludedServices:forService:),
@selector(discoverCharacteristics:forService:),
@selector(discoverDescriptorsForCharacteristic:),
@selector(readValueForCharacteristic:),
@selector(readValueForDescriptor:),
@selector(writeValue:forCharacteristic:type:),
@selector(writeValue:forDescriptor:),
@selector(setNotifyValue:forCharacteristic:),
@selector(readRSSI)
};
surroundMethods(self.class, selectors, sizeof(selectors) / sizeof(SEL), ^(NSInvocation *invocation) {
CBPeripheral * s = invocation.target;
if(s.state != CBPeripheralStateConnected) {
NSString *stack = [[NSThread callStackSymbols] componentsJoinedByString:@"\n"];
NSAssert(NO, @"CBPeripheral was not in connected state when %@ was called. State was %ld\n\nStacktrace:\n%@", NSStringFromSelector(invocation.selector), (long)s.state, stack);
}
}, nil);
});
}
@end
@implementation CBPeripheralManager (APIMisuseGuard)
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
SEL selectors[] = {
@selector(addService:),
@selector(removeService:),
@selector(removeAllServices),
@selector(startAdvertising:),
@selector(stopAdvertising),
@selector(updateValue:forCharacteristic:onSubscribedCentrals:),
@selector(respondToRequest:withResult:),
@selector(setDesiredConnectionLatency:forCentral:)
};
surroundMethods(self.class, selectors, sizeof(selectors) / sizeof(SEL), ^(NSInvocation *invocation) {
CBPeripheralManager * s = invocation.target;
if(!isPeripheralPoweredOn(s)) {
NSString *stack = [[NSThread callStackSymbols] componentsJoinedByString:@"\n"];
NSAssert(NO, @"CBPeripheralManager was not in powered on state when %@ was called. State was %ld\n\nStacktrace:\n%@", NSStringFromSelector(invocation.selector), (long)s.state, stack);
}
}, nil);
});
}
@end
#endif