-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
GATTServer.swift
935 lines (600 loc) · 32.2 KB
/
GATTServer.swift
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
//
// GATTServer.swift
// Bluetooth
//
// Created by Alsey Coleman Miller on 2/29/16.
// Copyright © 2016 PureSwift. All rights reserved.
//
import Foundation
public extension GATT {
public typealias Server = GATTServer
}
public final class GATTServer {
// MARK: - Properties
public var log: ((String) -> ())?
public var writePending: (() -> ())? {
get { return connection.writePending }
set { connection.writePending = newValue }
}
public private(set) var maximumTransmissionUnit: ATTMaximumTransmissionUnit {
get { return connection.maximumTransmissionUnit }
set { connection.maximumTransmissionUnit = newValue }
}
public let preferredMaximumTransmissionUnit: ATTMaximumTransmissionUnit
public let maximumPreparedWrites: Int
public var database = GATTDatabase()
public var willRead: ((_ uuid: BluetoothUUID, _ handle: UInt16, _ value: Data, _ offset: Int) -> ATT.Error?)?
public var willWrite: ((_ uuid: BluetoothUUID, _ handle: UInt16, _ value: Data, _ newValue: Data) -> ATT.Error?)?
public var didWrite: ((_ uuid: BluetoothUUID, _ handle: UInt16, _ value: Data) -> Void)?
// Don't modify
internal let connection: ATTConnection
private var preparedWrites = [PreparedWrite]()
// MARK: - Initialization
public init(socket: L2CAPSocketProtocol,
maximumTransmissionUnit: ATT.MaximumTransmissionUnit = .default,
maximumPreparedWrites: Int = 50) {
// set initial MTU and register handlers
self.maximumPreparedWrites = maximumPreparedWrites
self.preferredMaximumTransmissionUnit = maximumTransmissionUnit
self.connection = ATTConnection(socket: socket)
self.registerATTHandlers()
}
// MARK: - Methods
/// Performs the actual IO for sending data.
public func read() throws -> Bool {
return try connection.read()
}
/// Performs the actual IO for recieving data.
public func write() throws -> Bool {
return try connection.write()
}
/// Update the value of a characteristic attribute.
public func writeValue(_ value: Data, forCharacteristic handle: UInt16) {
database.write(value, forAttribute: handle)
didWriteAttribute(handle, isLocalWrite: true)
}
/// Update the value of a characteristic attribute.
public func writeValue(_ value: Data, forCharacteristic uuid: BluetoothUUID) {
guard let attribute = database.first(where: { $0.uuid == uuid })
else { fatalError("Invalid uuid \(uuid)") }
writeValue(value, forCharacteristic: attribute.handle)
}
// MARK: - Private Methods
@inline(__always)
private func registerATTHandlers() {
// Exchange MTU
connection.register { [weak self] in self?.exchangeMTU($0) }
// Read By Group Type
connection.register { [weak self] in self?.readByGroupType($0) }
// Read By Type
connection.register { [weak self] in self?.readByType($0) }
// Find Information
connection.register { [weak self] in self?.findInformation($0) }
// Find By Type Value
connection.register { [weak self] in self?.findByTypeValue($0) }
// Write Request
connection.register { [weak self] in self?.writeRequest($0) }
// Write Command
connection.register { [weak self] in self?.writeCommand($0) }
// Read Request
connection.register { [weak self] in self?.readRequest($0) }
// Read Blob Request
connection.register { [weak self] in self?.readBlobRequest($0) }
// Read Multiple Request
connection.register { [weak self] in self?.readMultipleRequest($0) }
// Prepare Write Request
connection.register { [weak self] in self?.prepareWriteRequest($0) }
// Execute Write Request
connection.register { [weak self] in self?.executeWriteRequest($0) }
}
@inline(__always)
private func errorResponse(_ opcode: ATT.Opcode, _ error: ATT.Error, _ handle: UInt16 = 0) {
log?("Error \(error) - \(opcode) (\(handle))")
guard let _ = connection.send(error: error, opcode: opcode, handle: handle)
else { fatalError("Could not add error PDU to queue: \(opcode) \(error) \(handle)") }
}
@inline(__always)
private func fatalErrorResponse(_ message: String, _ opcode: ATT.Opcode, _ handle: UInt16 = 0, line: UInt = #line) -> Never {
errorResponse(opcode, .unlikelyError, handle)
do { let _ = try connection.write() }
catch { log?("Could not send .unlikelyError to client. (\(error))") }
fatalError(message, line: line)
}
/// Respond to a client-initiated PDU message.
@inline(__always)
private func respond <T: ATTProtocolDataUnit> (_ response: T) {
log?("Response: \(response)")
guard let _ = connection.send(response)
else { fatalError("Could not add PDU to queue: \(response)") }
}
/// Send a server-initiated PDU message.
@inline(__always)
private func send (_ indication: ATTHandleValueIndication, response: @escaping (ATTResponse<ATTHandleValueConfirmation>) -> ()) {
log?("Indication: \(indication)")
let callback: (AnyATTResponse) -> () = { response(ATTResponse<ATTHandleValueConfirmation>($0)) }
guard let _ = connection.send(indication, response: (callback, ATTHandleValueConfirmation.self))
else { fatalError("Could not add PDU to queue: \(indication)") }
}
/// Send a server-initiated PDU message.
@inline(__always)
private func send (_ notification: ATTHandleValueNotification) {
log?("Notification: \(notification)")
guard let _ = connection.send(notification)
else { fatalError("Could not add PDU to queue: \(notification)") }
}
private func checkPermissions(_ permissions: BitMaskOptionSet<ATT.AttributePermission>,
_ attribute: GATTDatabase.Attribute) -> ATT.Error? {
guard attribute.permissions != permissions else { return nil }
// check permissions
if permissions.contains(.read) && !attribute.permissions.contains(.read) {
return .readNotPermitted
}
if permissions.contains(.write) && !attribute.permissions.contains(.write) {
return .writeNotPermitted
}
// check security
let security = connection.socket.securityLevel
if attribute.permissions.contains(.readAuthentication)
|| attribute.permissions.contains(.writeAuthentication)
&& security < .high {
return .insufficientAuthentication
}
if attribute.permissions.contains(.readEncrypt)
|| attribute.permissions.contains(.writeEncrypt)
&& security < .medium {
return .insufficientEncryption
}
return nil
}
/// Handler for Write Request and Command
private func handleWriteRequest(opcode: ATT.Opcode, handle: UInt16, value: Data, shouldRespond: Bool) {
/// Conditionally respond
@inline(__always)
func doResponse( _ block: @autoclosure() -> ()) {
if shouldRespond { block() }
}
log?("Write \(shouldRespond ? "Request" : "Command") (\(handle)) \(value)")
// no attributes, impossible to write
guard database.attributes.isEmpty == false
else { doResponse(errorResponse(opcode, .invalidHandle, handle)); return }
// validate handle
guard database.contains(handle: handle)
else { errorResponse(opcode, .invalidHandle, handle); return }
// get attribute
let attribute = database[handle: handle]
// validate permissions
if let error = checkPermissions([.write, .writeAuthentication, .writeEncrypt], attribute) {
doResponse(errorResponse(opcode, error, handle))
return
}
// validate application errors with write callback
if let error = willWrite?(attribute.uuid, handle, attribute.value, value) {
doResponse(errorResponse(opcode, error, handle))
return
}
database.write(value, forAttribute: handle)
doResponse(respond(ATTWriteResponse()))
didWriteAttribute(handle)
}
private func didWriteAttribute(_ attributeHandle: UInt16, isLocalWrite: Bool = false) {
let (group, attribute) = database.attributeGroup(for: attributeHandle)
assert(attribute.handle == attributeHandle)
guard let service = group.service,
let characteristic = service.characteristics.first(where: { $0.uuid == attribute.uuid })
else { return }
// inform delegate
if isLocalWrite == false {
didWrite?(attribute.uuid, attribute.handle, attribute.value)
}
// Client configuration
if let clientConfigurationDescriptor = characteristic.descriptors.first(where: { $0.uuid == .clientCharacteristicConfiguration }) {
guard let descriptor = GATTClientCharacteristicConfiguration(data: clientConfigurationDescriptor.value)
else { return }
// notify
if descriptor.configuration.contains(.notify) {
let notification = ATTHandleValueNotification(attribute: attribute, maximumTransmissionUnit: connection.maximumTransmissionUnit)
send(notification)
}
// indicate
if descriptor.configuration.contains(.indicate) {
let indication = ATTHandleValueIndication(attribute: attribute, maximumTransmissionUnit: connection.maximumTransmissionUnit)
send(indication) { [unowned self] (confirmation) in
self.log?("Confirmation: \(confirmation)")
}
}
}
}
private func handleReadRequest(opcode: ATT.Opcode,
handle: UInt16,
offset: UInt16 = 0,
isBlob: Bool = false) -> Data? {
// no attributes
guard database.attributes.isEmpty == false
else { errorResponse(opcode, .invalidHandle, handle); return nil }
// validate handle
guard database.contains(handle: handle)
else { errorResponse(opcode, .invalidHandle, handle); return nil }
// get attribute
let attribute = database[handle: handle]
// validate permissions
if let error = checkPermissions([.read, .readAuthentication, .readEncrypt], attribute) {
errorResponse(opcode, error, handle)
return nil
}
// Verify attribute value size for blob reading
//
// If the Characteristic Value is not longer than (ATT_MTU – 1) an Error Response with
// the Error Code set to Attribute Not Long shall be received on the first Read Blob Request.
guard isBlob == false || attribute.value.count > (Int(connection.maximumTransmissionUnit.rawValue) - 1)
else { errorResponse(opcode, .attributeNotLong, handle); return nil }
// check boundary
guard offset <= UInt16(attribute.value.count)
else { errorResponse(opcode, .invalidOffset, handle); return nil }
var value: Data
// Guard against invalid access if offset equals to value length
if offset == UInt16(attribute.value.count) {
value = Data()
} else if offset > 0 {
value = Data(attribute.value.suffix(from: Int(offset)))
} else {
value = attribute.value
}
// adjust value for MTU
value = Data(value.prefix(Int(connection.maximumTransmissionUnit.rawValue) - 1))
// validate application errors with read callback
if let error = willRead?(attribute.uuid, handle, value, Int(offset)) {
errorResponse(opcode, error, handle)
return nil
}
return value
}
// MARK: Callbacks
private func exchangeMTU(_ pdu: ATTMaximumTransmissionUnitRequest) {
let serverMTU = preferredMaximumTransmissionUnit.rawValue
let finalMTU = ATTMaximumTransmissionUnit(server: serverMTU, client: pdu.clientMTU)
// Respond with the server MTU (not final MTU)
connection.send(ATTMaximumTransmissionUnitResponse(serverMTU: serverMTU))
// Set MTU
maximumTransmissionUnit = finalMTU
log?("MTU Exchange (\(pdu.clientMTU) -> \(serverMTU))")
}
private func readByGroupType(_ pdu: ATTReadByGroupTypeRequest) {
typealias AttributeData = ATTReadByGroupTypeResponse.AttributeData
log?("Read by Group Type (\(pdu.startHandle) - \(pdu.endHandle))")
// validate handles
guard pdu.startHandle != 0 && pdu.endHandle != 0
else { errorResponse(type(of: pdu).attributeOpcode, .invalidHandle); return }
guard pdu.startHandle <= pdu.endHandle
else { errorResponse(type(of: pdu).attributeOpcode, .invalidHandle, pdu.startHandle); return }
// GATT defines that only the Primary Service and Secondary Service group types
// can be used for the "Read By Group Type" request. Return an error if any other group type is given.
guard pdu.type == .primaryService
|| pdu.type == .secondaryService
else { errorResponse(type(of: pdu).attributeOpcode, .unsupportedGroupType, pdu.startHandle); return }
let attributeData = database.readByGroupType(handle: (pdu.startHandle, pdu.endHandle), type: pdu.type)
guard let firstAttribute = attributeData.first
else { errorResponse(type(of: pdu).attributeOpcode, .attributeNotFound, pdu.startHandle); return }
let mtu = Int(connection.maximumTransmissionUnit.rawValue)
let valueLength = firstAttribute.value.count
let response: ATTReadByGroupTypeResponse
// truncate for MTU if first handle is too large
if ATTReadByGroupTypeResponse([firstAttribute]).dataLength > mtu {
let maxLength = min(min(mtu - 6, 251), valueLength)
let truncatedAttribute = AttributeData(attributeHandle: firstAttribute.attributeHandle,
endGroupHandle: firstAttribute.endGroupHandle,
value: Data(firstAttribute.value.prefix(maxLength)))
response = ATTReadByGroupTypeResponse([truncatedAttribute])
} else {
var count = 1
// respond with results that are the same length
if attributeData.count > 1 {
for (index, attribute) in attributeData.suffix(from: 1).enumerated() {
let newCount = index + 1
guard attribute.value.count == valueLength,
ATTReadByGroupTypeResponse.dataLength(for: attributeData.prefix(newCount)) <= mtu
else { break }
count = newCount
}
}
let limitedAttributes = Array(attributeData.prefix(count))
response = ATTReadByGroupTypeResponse(limitedAttributes)
}
assert(response.dataLength <= mtu,
"Response \(response.dataLength) bytes > MTU (\(mtu))")
respond(response)
}
private func readByType(_ pdu: ATTReadByTypeRequest) {
typealias AttributeData = ATTReadByTypeResponse.AttributeData
log?("Read by Type (\(pdu.attributeType)) (\(pdu.startHandle) - \(pdu.endHandle))")
guard pdu.startHandle != 0 && pdu.endHandle != 0
else { errorResponse(type(of: pdu).attributeOpcode, .invalidHandle); return }
guard pdu.startHandle <= pdu.endHandle
else { errorResponse(type(of: pdu).attributeOpcode, .invalidHandle, pdu.startHandle); return }
let attributeData = database
.readByType(handle: (pdu.startHandle, pdu.endHandle), type: pdu.attributeType)
.map { AttributeData(handle: $0.handle, value: $0.value) }
guard let firstAttribute = attributeData.first
else { errorResponse(type(of: pdu).attributeOpcode, .attributeNotFound, pdu.startHandle); return }
let mtu = Int(connection.maximumTransmissionUnit.rawValue)
let valueLength = firstAttribute.value.count
let response: ATTReadByTypeResponse
// truncate data for MTU if first handle is too large
if ATTReadByTypeResponse([firstAttribute]).dataLength > mtu {
let maxLength = min(min(mtu - 4, 253), firstAttribute.value.count)
let truncatedAttribute = AttributeData(handle: firstAttribute.handle,
value: Data(firstAttribute.value.prefix(maxLength)))
response = ATTReadByTypeResponse([truncatedAttribute])
} else {
var count = 1
// respond with results that are the same length
if attributeData.count > 1 {
for (index, attribute) in attributeData.suffix(from: 1).enumerated() {
let newCount = index + 1
guard attribute.value.count == valueLength,
ATTReadByTypeResponse.dataLength(for: attributeData.prefix(newCount)) <= mtu
else { break }
count = newCount
}
}
let limitedAttributes = Array(attributeData.prefix(count))
response = ATTReadByTypeResponse(limitedAttributes)
}
assert(response.dataLength <= mtu,
"Response \(response.dataLength) bytes > MTU (\(mtu))")
respond(response)
}
private func findInformation(_ pdu: ATTFindInformationRequest) {
typealias AttributeData = ATTFindInformationResponse.AttributeData
typealias Format = ATTFindInformationResponse.Format
let opcode = type(of: pdu).attributeOpcode
log?("Find Information (\(pdu.startHandle) - \(pdu.endHandle))")
guard pdu.startHandle != 0 && pdu.endHandle != 0
else { errorResponse(opcode, .invalidHandle); return }
guard pdu.startHandle <= pdu.endHandle
else { errorResponse(opcode, .invalidHandle, pdu.startHandle); return }
let attributes = database.findInformation(handle: (pdu.startHandle, pdu.endHandle))
guard attributes.isEmpty == false
else { errorResponse(opcode, .attributeNotFound, pdu.startHandle); return }
guard let format = Format(uuid: attributes[0].uuid)
else { errorResponse(opcode, .unlikelyError, pdu.startHandle); return }
var bit16Pairs = [ATTFindInformationResponse.Attribute16Bit]()
var bit128Pairs = [ATTFindInformationResponse.Attribute128Bit]()
for (index, attribute) in attributes.enumerated() {
// truncate if bigger than MTU
let encodedLength = 2 + ((index + 1) * format.length)
guard encodedLength <= Int(connection.maximumTransmissionUnit.rawValue)
else { break }
var mismatchedType = false
// encode attribute
switch (attribute.uuid, format) {
case let (.bit16(type), .bit16):
bit16Pairs.append(ATTFindInformationResponse.Attribute16Bit(handle: attribute.handle, uuid: type))
case let (.bit128(type), .bit128):
bit128Pairs.append(ATTFindInformationResponse.Attribute128Bit(handle: attribute.handle, uuid: type))
default:
mismatchedType = true // mismatching types
}
// stop enumerating
guard mismatchedType == false
else { break }
}
let attributeData: AttributeData
switch format {
case .bit16: attributeData = .bit16(bit16Pairs)
case .bit128: attributeData = .bit128(bit128Pairs)
}
let response = ATTFindInformationResponse(attributeData: attributeData)
respond(response)
}
private func findByTypeValue(_ pdu: ATTFindByTypeRequest) {
typealias Handle = ATTFindByTypeResponse.HandlesInformation
log?("Find By Type Value (\(pdu.startHandle) - \(pdu.endHandle)) (\(pdu.attributeType))")
guard pdu.startHandle != 0 && pdu.endHandle != 0
else { errorResponse(type(of: pdu).attributeOpcode, .invalidHandle); return }
guard pdu.startHandle <= pdu.endHandle
else { errorResponse(type(of: pdu).attributeOpcode, .invalidHandle, pdu.startHandle); return }
let handles = database.findByTypeValue(handle: (pdu.startHandle, pdu.endHandle),
type: pdu.attributeType,
value: pdu.attributeValue)
guard handles.isEmpty == false
else { errorResponse(type(of: pdu).attributeOpcode, .attributeNotFound, pdu.startHandle); return }
let response = ATTFindByTypeResponse(handles)
respond(response)
}
private func writeRequest(_ pdu: ATTWriteRequest) {
let opcode = type(of: pdu).attributeOpcode
handleWriteRequest(opcode: opcode, handle: pdu.handle, value: pdu.value, shouldRespond: true)
}
private func writeCommand(_ pdu: ATTWriteCommand) {
let opcode = type(of: pdu).attributeOpcode
handleWriteRequest(opcode: opcode, handle: pdu.handle, value: pdu.value, shouldRespond: false)
}
private func readRequest(_ pdu: ATTReadRequest) {
let opcode = type(of: pdu).attributeOpcode
log?("Read (\(pdu.handle))")
if let value = handleReadRequest(opcode: opcode, handle: pdu.handle) {
respond(ATTReadResponse(attributeValue: value))
}
}
private func readBlobRequest(_ pdu: ATTReadBlobRequest) {
let opcode = type(of: pdu).attributeOpcode
log?("Read Blob (\(pdu.handle))")
if let value = handleReadRequest(opcode: opcode, handle: pdu.handle, offset: pdu.offset, isBlob: true) {
respond(ATTReadBlobResponse(partAttributeValue: value))
}
}
private func readMultipleRequest(_ pdu: ATTReadMultipleRequest) {
let opcode = type(of: pdu).attributeOpcode
log?("Read Multiple Request \(pdu.handles)")
// no attributes, impossible to read
guard database.attributes.isEmpty == false
else { errorResponse(opcode, .invalidHandle, pdu.handles[0]); return }
var values = Data()
for handle in pdu.handles {
// validate handle
guard database.contains(handle: handle)
else { errorResponse(opcode, .invalidHandle, handle); return }
// get attribute
let attribute = database[handle: handle]
// validate application errors with read callback
if let error = willRead?(attribute.uuid, handle, attribute.value, 0) {
errorResponse(opcode, error, handle)
return
}
values += attribute.value
}
let response = ATTReadMultipleResponse(values: values)
respond(response)
}
private func prepareWriteRequest(_ pdu: ATTPrepareWriteRequest) {
let opcode = type(of: pdu).attributeOpcode
log?("Prepare Write Request (\(pdu.handle))")
// no attributes, impossible to write
guard database.attributes.isEmpty == false
else { errorResponse(opcode, .invalidHandle, pdu.handle); return }
// validate handle
guard database.contains(handle: pdu.handle)
else { errorResponse(opcode, .invalidHandle, pdu.handle); return }
// validate that the prepared writes queue is not full
guard preparedWrites.count <= maximumPreparedWrites
else { errorResponse(opcode, .prepareQueueFull); return }
// get attribute
let attribute = database[handle: pdu.handle]
// validate permissions
if let error = checkPermissions([.write, .writeAuthentication, .writeEncrypt], attribute) {
errorResponse(opcode, error, pdu.handle)
return
}
// The Attribute Value validation is done when an Execute Write Request is received.
// Hence, any Invalid Offset or Invalid Attribute Value Length errors are generated
// when an Execute Write Request is received.
// add queued write
let preparedWrite = PreparedWrite(handle: pdu.handle, value: pdu.partValue, offset: pdu.offset)
preparedWrites.append(preparedWrite)
let response = ATTPrepareWriteResponse(handle: pdu.handle, offset: pdu.offset, partValue: pdu.partValue)
respond(response)
}
private func executeWriteRequest(_ pdu: ATTExecuteWriteRequest) {
let opcode = type(of: pdu).attributeOpcode
log?("Execute Write Request (\(pdu))")
let preparedWrites = self.preparedWrites
self.preparedWrites = []
var newValues = [UInt16: Data]()
switch pdu {
case .cancel:
break // queue always cleared
case .write:
// validate
for write in preparedWrites {
let previousValue = newValues[write.handle] ?? Data()
let newValue = previousValue + write.value
// validate offset?
newValues[write.handle] = newValue
}
// validate new values
for (handle, newValue) in newValues {
let attribute = database[handle: handle]
// validate application errors with write callback
if let error = willWrite?(attribute.uuid, handle, attribute.value, newValue) {
errorResponse(opcode, error, handle)
return
}
}
// write new values
for (handle, newValue) in newValues {
database.write(newValue, forAttribute: handle)
}
}
respond(ATTExecuteWriteResponse())
for handle in newValues.keys {
didWriteAttribute(handle)
}
}
}
// MARK: - Supporting Types
private extension GATTServer {
struct PreparedWrite {
let handle: UInt16
let value: Data
let offset: UInt16
}
}
// MARK: - GATTDatabase Extensions
internal struct HandleRange {
public let start: UInt16
public let end: UInt16
public init(start: UInt16, end: UInt16) {
assert(start <= end)
self.start = start
self.end = end
}
}
internal extension HandleRange {
init(group: GATTDatabase.AttributeGroup) {
self.init(start: group.startHandle, end: group.endHandle)
}
}
internal extension HandleRange {
func isSubset(_ other: HandleRange) -> Bool {
return self.start >= other.start
&& self.start <= other.end
&& self.end >= other.start
&& self.end <= other.end
}
func contains(_ element: UInt16) -> Bool {
return start <= element
&& element <= end
}
}
internal extension GATTDatabase {
/// Find the enclosing Service attribute group for the specified handle
func attributeGroup(for handle: UInt16) -> (group: AttributeGroup, attribute: Attribute) {
for group in attributeGroups {
for attribute in group.attributes {
guard attribute.handle != handle
else { return (group, attribute) }
}
}
fatalError("Invalid handle \(handle)")
}
/// Used for Service discovery.
func readByGroupType(handle: (start: UInt16, end: UInt16), type: BluetoothUUID) -> [ATTReadByGroupTypeResponse.AttributeData] {
typealias AttributeData = ATTReadByGroupTypeResponse.AttributeData
var data = [AttributeData]()
data.reserveCapacity(attributeGroups.count)
let handleRange = HandleRange(start: handle.start, end: handle.end)
for group in attributeGroups {
let groupRange = HandleRange(group: group)
guard group.serviceAttribute.uuid == type,
groupRange.isSubset(handleRange)
else { continue }
data.append(AttributeData(attributeHandle: group.startHandle,
endGroupHandle: group.endHandle,
value: group.serviceAttribute.value))
}
return data
}
func readByType(handle: (start: UInt16, end: UInt16), type: BluetoothUUID) -> [Attribute] {
let range = HandleRange(start: handle.start, end: handle.end)
return attributes.filter { range.contains($0.handle) && $0.uuid == type }
}
func findInformation(handle: (start: UInt16, end: UInt16)) -> [Attribute] {
let range = HandleRange(start: handle.start, end: handle.end)
return attributes.filter { range.contains($0.handle) }
}
func findByTypeValue(handle: (start: UInt16, end: UInt16), type: UInt16, value: Data) -> [ATTFindByTypeResponse.HandlesInformation] {
typealias HandleInformation = ATTFindByTypeResponse.HandlesInformation
let range = HandleRange(start: handle.start, end: handle.end)
var results = [HandleInformation]()
for group in attributeGroups {
for attribute in group.attributes {
let match = range.contains(attribute.handle)
&& attribute.uuid == .bit16(type)
&& attribute.value == value
guard match else { continue }
results.append(HandleInformation(foundAttribute: group.startHandle, groupEnd: group.endHandle))
}
}
return results
}
}