-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathPeripheralDelegate.kt
346 lines (301 loc) · 12 KB
/
PeripheralDelegate.kt
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package com.juul.kable
import com.juul.kable.PeripheralDelegate.DidUpdateValueForCharacteristic.Closed
import com.juul.kable.PeripheralDelegate.Response.DidDiscoverServices
import com.juul.kable.PeripheralDelegate.Response.DidReadRssi
import com.juul.kable.PeripheralDelegate.Response.DidUpdateNotificationStateForCharacteristic
import com.juul.kable.PeripheralDelegate.Response.DidUpdateValueForDescriptor
import com.juul.kable.PeripheralDelegate.Response.DidWriteValueForCharacteristic
import com.juul.kable.PeripheralDelegate.Response.IsReadyToSendWriteWithoutResponse
import com.juul.kable.logs.LogMessage
import com.juul.kable.logs.Logger
import com.juul.kable.logs.Logging
import com.juul.kable.logs.detail
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.Channel.Factory.BUFFERED
import kotlinx.coroutines.channels.ReceiveChannel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import platform.CoreBluetooth.CBCharacteristic
import platform.CoreBluetooth.CBDescriptor
import platform.CoreBluetooth.CBL2CAPChannel
import platform.CoreBluetooth.CBPeripheral
import platform.CoreBluetooth.CBPeripheralDelegateProtocol
import platform.CoreBluetooth.CBService
import platform.Foundation.NSData
import platform.Foundation.NSError
import platform.Foundation.NSNumber
import platform.Foundation.NSUUID
import platform.darwin.NSObject
import kotlin.native.concurrent.freeze
// https://developer.apple.com/documentation/corebluetooth/cbperipheraldelegate
internal class PeripheralDelegate(
logging: Logging,
identifier: String
) : NSObject(), CBPeripheralDelegateProtocol {
sealed class Response {
abstract val peripheralIdentifier: NSUUID
abstract val error: NSError?
data class DidDiscoverServices(
override val peripheralIdentifier: NSUUID,
override val error: NSError?,
) : Response()
data class DidDiscoverCharacteristicsForService(
override val peripheralIdentifier: NSUUID,
val service: CBService,
override val error: NSError?,
) : Response()
data class DidWriteValueForCharacteristic(
override val peripheralIdentifier: NSUUID,
val characteristic: CBCharacteristic,
override val error: NSError?,
) : Response()
data class DidUpdateValueForDescriptor(
override val peripheralIdentifier: NSUUID,
val descriptor: CBDescriptor,
override val error: NSError?,
) : Response()
data class DidUpdateNotificationStateForCharacteristic(
override val peripheralIdentifier: NSUUID,
val characteristic: CBCharacteristic,
override val error: NSError?,
) : Response()
data class IsReadyToSendWriteWithoutResponse(
override val peripheralIdentifier: NSUUID,
override val error: NSError?,
) : Response()
data class DidReadRssi(
override val peripheralIdentifier: NSUUID,
val rssi: NSNumber,
override val error: NSError?,
) : Response()
}
private val _response = Channel<Response>(BUFFERED)
val response: ReceiveChannel<Response> = _response
sealed class DidUpdateValueForCharacteristic {
data class Data(
val cbCharacteristic: CBCharacteristic,
val data: NSData,
) : DidUpdateValueForCharacteristic()
data class Error(
val cbCharacteristic: CBCharacteristic,
val error: NSError,
) : DidUpdateValueForCharacteristic()
/** Signal to downstream that [PeripheralDelegate] has been [closed][close]. */
object Closed : DidUpdateValueForCharacteristic()
}
private val _characteristicChanges = MutableSharedFlow<DidUpdateValueForCharacteristic>(extraBufferCapacity = 64)
val characteristicChanges = _characteristicChanges.asSharedFlow()
private val logger = Logger(logging, tag = "Kable/Delegate", identifier = identifier)
/* Discovering Services */
override fun peripheral(
peripheral: CBPeripheral,
didDiscoverServices: NSError?,
) {
logger.debug {
message = "${peripheral.identifier} didDiscoverServices"
detail(didDiscoverServices)
}
_response.sendBlocking(DidDiscoverServices(peripheral.identifier, didDiscoverServices))
}
// https://kotlinlang.org/docs/reference/native/objc_interop.html#subclassing-swiftobjective-c-classes-and-protocols-from-kotlin
@Suppress("CONFLICTING_OVERLOADS")
override fun peripheral(
peripheral: CBPeripheral,
didDiscoverIncludedServicesForService: CBService,
error: NSError?,
) {
logger.debug(error) {
message = "${peripheral.identifier} didDiscoverIncludedServicesForService"
detail(didDiscoverIncludedServicesForService)
}
// todo
}
/* Discovering Characteristics and their Descriptors */
// https://kotlinlang.org/docs/reference/native/objc_interop.html#subclassing-swiftobjective-c-classes-and-protocols-from-kotlin
@Suppress("CONFLICTING_OVERLOADS")
override fun peripheral(
peripheral: CBPeripheral,
didDiscoverCharacteristicsForService: CBService,
error: NSError?
) {
logger.debug(error) {
message = "${peripheral.identifier} didDiscoverCharacteristicsForService"
detail(didDiscoverCharacteristicsForService)
}
_response.sendBlocking(
Response.DidDiscoverCharacteristicsForService(
peripheral.identifier,
didDiscoverCharacteristicsForService,
null
)
)
}
// https://kotlinlang.org/docs/reference/native/objc_interop.html#subclassing-swiftobjective-c-classes-and-protocols-from-kotlin
@Suppress("CONFLICTING_OVERLOADS")
override fun peripheral(
peripheral: CBPeripheral,
didDiscoverDescriptorsForCharacteristic: CBCharacteristic,
error: NSError?,
) {
logger.debug(error) {
message = "${peripheral.identifier} didDiscoverDescriptorsForCharacteristic"
detail(didDiscoverDescriptorsForCharacteristic)
}
// todo
}
/* Retrieving Characteristic and Descriptor Values */
// https://kotlinlang.org/docs/reference/native/objc_interop.html#subclassing-swiftobjective-c-classes-and-protocols-from-kotlin
@Suppress("CONFLICTING_OVERLOADS")
override fun peripheral(
peripheral: CBPeripheral,
didUpdateValueForCharacteristic: CBCharacteristic,
error: NSError?,
) {
val cbCharacteristic = didUpdateValueForCharacteristic.freeze()
logger.debug(error) {
message = "${peripheral.identifier} didUpdateValueForCharacteristic"
detail(didUpdateValueForCharacteristic)
detail(cbCharacteristic.value)
}
val change = if (error == null) {
// Assumption: `value == null` and `error == null` are mutually exclusive.
// i.e. When `error == null` then `CBCharacteristic`'s `value` is non-null.
val data = cbCharacteristic.value!!
DidUpdateValueForCharacteristic.Data(cbCharacteristic, data)
} else {
DidUpdateValueForCharacteristic.Error(cbCharacteristic, error)
}
_characteristicChanges.emitBlocking(change)
}
// https://kotlinlang.org/docs/reference/native/objc_interop.html#subclassing-swiftobjective-c-classes-and-protocols-from-kotlin
@Suppress("CONFLICTING_OVERLOADS")
override fun peripheral(
peripheral: CBPeripheral,
didUpdateValueForDescriptor: CBDescriptor,
error: NSError?,
) {
logger.debug(error) {
message = "${peripheral.identifier} didUpdateValueForDescriptor"
detail(didUpdateValueForDescriptor)
}
_response.sendBlocking(
DidUpdateValueForDescriptor(peripheral.identifier, didUpdateValueForDescriptor, error)
)
}
/* Writing Characteristic and Descriptor Values */
// https://kotlinlang.org/docs/reference/native/objc_interop.html#subclassing-swiftobjective-c-classes-and-protocols-from-kotlin
@Suppress("CONFLICTING_OVERLOADS")
override fun peripheral(
peripheral: CBPeripheral,
didWriteValueForCharacteristic: CBCharacteristic,
error: NSError?
) {
logger.debug(error) {
message = "${peripheral.identifier} didWriteValueForCharacteristic"
detail(didWriteValueForCharacteristic)
}
_response.sendBlocking(
DidWriteValueForCharacteristic(
peripheral.identifier,
didWriteValueForCharacteristic,
error
)
)
}
// https://kotlinlang.org/docs/reference/native/objc_interop.html#subclassing-swiftobjective-c-classes-and-protocols-from-kotlin
@Suppress("CONFLICTING_OVERLOADS")
override fun peripheral(
peripheral: CBPeripheral,
didWriteValueForDescriptor: CBDescriptor,
error: NSError?,
) {
logger.debug(error) {
message = "${peripheral.identifier} didWriteValueForDescriptor"
detail(didWriteValueForDescriptor)
}
// todo
}
override fun peripheralIsReadyToSendWriteWithoutResponse(
peripheral: CBPeripheral
) {
logger.debug {
message = "${peripheral.identifier} peripheralIsReadyToSendWriteWithoutResponse"
}
_response.sendBlocking(
IsReadyToSendWriteWithoutResponse(peripheral.identifier, error = null)
)
}
/* Managing Notifications for a Characteristic’s Value */
// https://kotlinlang.org/docs/reference/native/objc_interop.html#subclassing-swiftobjective-c-classes-and-protocols-from-kotlin
@Suppress("CONFLICTING_OVERLOADS")
override fun peripheral(
peripheral: CBPeripheral,
didUpdateNotificationStateForCharacteristic: CBCharacteristic,
error: NSError?
) {
logger.debug(error) {
message = "${peripheral.identifier} didUpdateNotificationStateForCharacteristic"
detail(didUpdateNotificationStateForCharacteristic)
}
_response.sendBlocking(
DidUpdateNotificationStateForCharacteristic(
peripheral.identifier,
didUpdateNotificationStateForCharacteristic,
error
)
)
}
/* Retrieving a Peripheral’s RSSI Data */
override fun peripheral(
peripheral: CBPeripheral,
didReadRSSI: NSNumber,
error: NSError?,
) {
logger.debug(error) {
message = "${peripheral.identifier} didReadRSSI"
detail("rssi", didReadRSSI.stringValue)
}
_response.sendBlocking(DidReadRssi(peripheral.identifier, didReadRSSI, error))
}
/* Monitoring Changes to a Peripheral’s Name or Services */
override fun peripheralDidUpdateName(peripheral: CBPeripheral) {
logger.debug {
message = "${peripheral.identifier} peripheralDidUpdateName"
}
// todo
}
override fun peripheral(
peripheral: CBPeripheral,
didModifyServices: List<*>,
) {
logger.debug {
message = "${peripheral.identifier} didModifyServices"
}
// todo
}
/* Monitoring L2CAP Channels */
override fun peripheral(
peripheral: CBPeripheral,
didOpenL2CAPChannel: CBL2CAPChannel?,
error: NSError?,
) {
logger.debug(error) {
message = "${peripheral.identifier} didOpenL2CAPChannel"
}
// todo
}
fun close() {
_response.close(ConnectionLostException())
_characteristicChanges.emitBlocking(Closed)
}
}
private inline fun Logger.debug(error: NSError?, crossinline init: LogMessage.() -> Unit) {
if (error == null) {
debug(init = init)
} else {
warn {
init()
detail(error)
}
}
}