Skip to content

Commit

Permalink
Lint
Browse files Browse the repository at this point in the history
  • Loading branch information
sbihel committed Sep 4, 2024
1 parent bf82da2 commit 4b9ef9b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 24 deletions.
16 changes: 13 additions & 3 deletions Sources/MobileSdk/MDocReader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,22 @@ public class MDocReader {
var bleManager: MDocReaderBLEPeripheral!
var callback: BLEReaderSessionStateDelegate

public init?(callback: BLEReaderSessionStateDelegate, uri: String, requestedItems: [String: [String: Bool]], trustAnchorRegistry: [String]?) {
public init?(
callback: BLEReaderSessionStateDelegate,
uri: String,
requestedItems: [String: [String: Bool]],
trustAnchorRegistry: [String]?
) {
self.callback = callback
do {
let sessionData = try SpruceIDMobileSdkRs.establishSession(uri: uri, requestedItems: requestedItems, trustAnchorRegistry: trustAnchorRegistry)
let sessionData = try SpruceIDMobileSdkRs.establishSession(uri: uri,
requestedItems: requestedItems,
trustAnchorRegistry: trustAnchorRegistry)
self.sessionManager = sessionData.state
self.bleManager = MDocReaderBLEPeripheral(callback: self, serviceUuid: CBUUID(string: sessionData.uuid), request: sessionData.request, bleIdent: sessionData.bleIdent)
self.bleManager = MDocReaderBLEPeripheral(callback: self,
serviceUuid: CBUUID(string: sessionData.uuid),
request: sessionData.request,
bleIdent: sessionData.bleIdent)
} catch {
print("\(error)")
return nil
Expand Down
42 changes: 21 additions & 21 deletions Sources/MobileSdk/MDocReaderBLEPeripheral.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,17 @@ class MDocReaderBLEPeripheral: NSObject {
self.requestData = request
self.incomingMessageBuffer = Data()
super.init()
self.peripheralManager = CBPeripheralManager(delegate: self, queue: nil, options: [CBPeripheralManagerOptionShowPowerAlertKey: true])
self.peripheralManager = CBPeripheralManager(delegate: self,
queue: nil,
options: [CBPeripheralManagerOptionShowPowerAlertKey: true])
}

func setupService() {
let service = CBMutableService(type: self.serviceUuid, primary: true)
// CBUUIDClientCharacteristicConfigurationString only returns "2902"
// let clientDescriptor = CBMutableDescriptor(type: CBUUID(string: "00002902-0000-1000-8000-00805f9b34fb"), value: Data([0x00, 0x00])) as CBDescriptor
// wallet-sdk-kt isn't using write without response...
self.stateCharacteristic = CBMutableCharacteristic(type: readerStateCharacteristicId,
properties: [.notify, .writeWithoutResponse, .write],
value: nil,
permissions: [.writeable])
// for some reason this seems to drop all other descriptors
// self.stateCharacteristic!.descriptors = [clientDescriptor] + (self.stateCharacteristic!.descriptors ?? [] )
// self.stateCharacteristic!.descriptors?.insert(clientDescriptor, at: 0)
// wallet-sdk-kt isn't using write without response...
self.readCharacteristic = CBMutableCharacteristic(type: readerClient2ServerCharacteristicId,
properties: [.writeWithoutResponse, .write],
value: nil,
Expand All @@ -52,13 +47,10 @@ class MDocReaderBLEPeripheral: NSObject {
properties: [.notify],
value: nil,
permissions: [.readable, .writeable])
// self.writeCharacteristic!.descriptors = [clientDescriptor] + (self.writeCharacteristic!.descriptors ?? [] )
// self.writeCharacteristic!.descriptors?.insert(clientDescriptor, at: 0)
self.identCharacteristic = CBMutableCharacteristic(type: readerIdentCharacteristicId,
properties: [.read],
value: bleIdent,
permissions: [.readable])
// wallet-sdk-kt is failing if this is present
// self.l2capCharacteristic = CBMutableCharacteristic(type: readerL2CAPCharacteristicId,
// properties: [.read],
// value: nil,
Expand All @@ -67,7 +59,7 @@ class MDocReaderBLEPeripheral: NSObject {
stateCharacteristic! as CBCharacteristic,
readCharacteristic! as CBCharacteristic,
writeCharacteristic! as CBCharacteristic,
identCharacteristic! as CBCharacteristic,
identCharacteristic! as CBCharacteristic
// l2capCharacteristic! as CBCharacteristic
]
peripheralManager.add(service)
Expand Down Expand Up @@ -180,7 +172,11 @@ extension MDocReaderBLEPeripheral: CBPeripheralManagerDelegate {
self.drainWritingQueue()
}

func peripheralManager(_ peripheral: CBPeripheralManager, central: CBCentral, didSubscribeTo characteristic: CBCharacteristic) {
func peripheralManager(
_ peripheral: CBPeripheralManager,
central: CBCentral,
didSubscribeTo characteristic: CBCharacteristic
) {
print("Subscribed to \(characteristic.uuid)")
self.callback.callback(message: .connected)
self.peripheralManager?.stopAdvertising()
Expand All @@ -194,28 +190,30 @@ extension MDocReaderBLEPeripheral: CBPeripheralManagerDelegate {
return
}
}

func peripheralManager(_ peripheral: CBPeripheralManager, didReceiveRead request: CBATTRequest) {
print("Received read request for \(request.characteristic.uuid)")

// Since there is no callback for MTU on iOS we will grab it here.
maximumCharacteristicSize = min(request.central.maximumUpdateValueLength, 512)
if (request.characteristic.uuid == readerIdentCharacteristicId) {

if request.characteristic.uuid == readerIdentCharacteristicId {
peripheralManager.respond(to: request, withResult: .success)
} else if (request.characteristic.uuid == readerL2CAPCharacteristicId) {
} else if request.characteristic.uuid == readerL2CAPCharacteristicId {
// peripheralManager.publishL2CAPChannel(withEncryption: true)
// peripheralManager.respond(to: request, withResult: .success)
} else {
self.callback.callback(message: .error(.server("Read on unexpected characteristic with UUID \(request.characteristic.uuid)")))
self.callback.callback(message:
.error(.server("Read on unexpected characteristic with UUID \(request.characteristic.uuid)"))
)
}
}

func peripheralManager(_ peripheral: CBPeripheralManager, didReceiveWrite requests: [CBATTRequest]) {
for request in requests {
// Since there is no callback for MTU on iOS we will grab it here.
maximumCharacteristicSize = min(request.central.maximumUpdateValueLength, 512)

do {
print("Processing request")
try processData(central: request.central, characteristic: request.characteristic, value: request.value)
Expand All @@ -225,7 +223,9 @@ extension MDocReaderBLEPeripheral: CBPeripheralManagerDelegate {
}
} catch {
self.callback.callback(message: .error(.server("\(error)")))
self.peripheralManager?.updateValue(Data([0x02]), for: self.stateCharacteristic!, onSubscribedCentrals: nil)
self.peripheralManager?.updateValue(Data([0x02]),
for: self.stateCharacteristic!,
onSubscribedCentrals: nil)
}
}
}
Expand Down

0 comments on commit 4b9ef9b

Please sign in to comment.