30
30
31
31
import CoreBluetooth
32
32
33
+ open class CBMAttribute : NSObject {
34
+
35
+ /// The Bluetooth UUID of the attribute.
36
+ var uuid : CBMUUID {
37
+ fatalError ( )
38
+ }
39
+ }
40
+
33
41
open class CBMService : CBMAttribute {
34
42
internal let identifier : UUID
35
43
private let _uuid : CBMUUID
36
44
37
45
internal var _includedServices : [ CBMService ] ?
38
46
internal var _characteristics : [ CBMCharacteristic ] ?
39
47
48
+ #if swift(>=5.5)
49
+ /// A back-pointer to the peripheral this service belongs to.
50
+ open internal( set) weak var peripheral : CBMPeripheral ?
51
+ #else
40
52
/// A back-pointer to the peripheral this service belongs to.
41
53
open internal( set) unowned var peripheral : CBMPeripheral
54
+ #endif
42
55
43
56
/// The type of the service (primary or secondary).
44
57
open fileprivate( set) var isPrimary : Bool
45
58
46
- /// The Bluetooth UUID of the attribute.
47
59
open override var uuid : CBMUUID {
48
60
return _uuid
49
61
}
@@ -106,30 +118,19 @@ internal class CBMServiceNative: CBMService {
106
118
}
107
119
108
120
open class CBMServiceMock : CBMService {
109
-
110
- open override var includedServices : [ CBMService ] ? {
111
- set { _includedServices = newValue }
112
- get { return _includedServices }
113
- }
114
-
115
- open override var characteristics : [ CBMCharacteristic ] ? {
116
- set { _characteristics = newValue }
117
- get { return _characteristics }
118
- }
119
121
120
122
/// Returns a service, initialized with a service type and UUID.
121
123
/// - Parameters:
122
124
/// - uuid: The Bluetooth UUID of the service.
123
125
/// - isPrimary: The type of the service (primary or secondary).
126
+ /// - includedServices: Optional list of included services.
124
127
/// - characteristics: Optional list of characteristics.
125
128
public init ( type uuid: CBMUUID , primary isPrimary: Bool ,
129
+ includedService: CBMServiceMock ... ,
126
130
characteristics: CBMCharacteristicMock ... ) {
127
131
super. init ( type: uuid, primary: isPrimary)
128
- self . characteristics = characteristics
129
- }
130
-
131
- open func contains( _ characteristic: CBMCharacteristicMock ) -> Bool {
132
- return _characteristics? . contains ( characteristic) ?? false
132
+ self . _includedServices = includedService
133
+ self . _characteristics = characteristics
133
134
}
134
135
135
136
open override func isEqual( _ object: Any ? ) -> Bool {
@@ -146,13 +147,23 @@ open class CBMCharacteristic: CBMAttribute {
146
147
147
148
internal var _descriptors : [ CBMDescriptor ] ?
148
149
149
- /// The Bluetooth UUID of the attribute.
150
150
open override var uuid : CBMUUID {
151
151
return _uuid
152
152
}
153
153
154
+ #if swift(>=5.5)
155
+ /// A back-pointer to the service this characteristic belongs to.
156
+ open internal( set) weak var service : CBMService ?
157
+ #else
154
158
/// A back-pointer to the service this characteristic belongs to.
155
- open internal( set) var service : CBMService
159
+ open internal( set) unowned var service : CBMService
160
+ #endif
161
+
162
+ /// Casts the sometimes weak, sometimes unowned service to
163
+ /// always optional object.
164
+ internal var optionalService : CBMService ? {
165
+ return service
166
+ }
156
167
157
168
/// The properties of the characteristic.
158
169
public let properties : CBMCharacteristicProperties
@@ -234,10 +245,6 @@ open class CBMCharacteristicMock: CBMCharacteristic {
234
245
self . descriptors = descriptors
235
246
}
236
247
237
- open func contains( _ descriptor: CBMDescriptor ) -> Bool {
238
- return _descriptors? . contains ( descriptor) ?? false
239
- }
240
-
241
248
open override func isEqual( _ object: Any ? ) -> Bool {
242
249
if let other = object as? CBMCharacteristicMock {
243
250
return identifier == other. identifier
@@ -250,13 +257,23 @@ open class CBMDescriptor: CBMAttribute {
250
257
internal let identifier : UUID
251
258
private let _uuid : CBMUUID
252
259
253
- /// The Bluetooth UUID of the attribute.
254
260
open override var uuid : CBMUUID {
255
261
return _uuid
256
262
}
257
263
264
+ #if swift(>=5.5)
265
+ /// A back-pointer to the characteristic this descriptor belongs to.
266
+ open internal( set) weak var characteristic : CBMCharacteristic ?
267
+ #else
258
268
/// A back-pointer to the characteristic this descriptor belongs to.
259
- open internal( set) var characteristic : CBMCharacteristic
269
+ open internal( set) unowned var characteristic : CBMCharacteristic
270
+ #endif
271
+
272
+ /// Casts the sometimes weak, sometimes unowned characteristic to
273
+ /// always optional object.
274
+ internal var optionalCharacteristic : CBMCharacteristic ? {
275
+ return characteristic
276
+ }
260
277
261
278
/// The value of the descriptor.
262
279
open internal( set) var value : Any ?
@@ -321,6 +338,42 @@ open class CBMClientCharacteristicConfigurationDescriptorMock: CBMDescriptorMock
321
338
322
339
public typealias CBMCCCDescriptorMock = CBMClientCharacteristicConfigurationDescriptorMock
323
340
341
+ // MARK: - Utilities
342
+
343
+ internal extension Array where Element == CBMServiceMock {
344
+
345
+ func find( mockOf service: CBMService ) -> CBMServiceMock ? {
346
+ return first { $0. identifier == service. identifier }
347
+ }
348
+
349
+ func find( mockOf characteristic: CBMCharacteristic ) -> CBMCharacteristicMock ? {
350
+ guard let service = characteristic. optionalService,
351
+ let mockService = find ( mockOf: service) ,
352
+ let mockCharacteristic = mockService. characteristics? . first ( where: {
353
+ $0. identifier == characteristic. identifier
354
+ } ) else {
355
+ return nil
356
+ }
357
+ return mockCharacteristic as? CBMCharacteristicMock
358
+ }
359
+
360
+ func find( mockOf descriptor: CBMDescriptor ) -> CBMDescriptorMock ? {
361
+ guard let characteristic = descriptor. optionalCharacteristic,
362
+ let service = characteristic. optionalService,
363
+ let mockService = find ( mockOf: service) ,
364
+ let mockCharacteristic = mockService. characteristics? . first ( where: {
365
+ $0. identifier == characteristic. identifier
366
+ } ) ,
367
+ let mockDescriptor = mockCharacteristic. descriptors? . first ( where: {
368
+ $0. identifier == descriptor. identifier
369
+ } ) else {
370
+ return nil
371
+ }
372
+ return mockDescriptor as? CBMDescriptorMock
373
+ }
374
+
375
+ }
376
+
324
377
// MARK: - Mocking uninitialized objects
325
378
326
379
fileprivate let uninitializedPeriperheral = CBMPeripheralUninitialized ( )
0 commit comments