diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 5376a90..8d3e5ce 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -11,10 +11,10 @@ dependencies: sdk: flutter mtrust_sec_kit: path: ../ - mtrust_urp_types: ^6.0.0 - mtrust_urp_ble_strategy: ^9.1.0-12 - mtrust_urp_virtual_strategy: ^9.1.0-12 - mtrust_urp_ui: ^9.1.0-12 + mtrust_urp_types: ^6.2.1 + mtrust_urp_ble_strategy: ^9.1.0-13 + mtrust_urp_virtual_strategy: ^9.1.0-13 + mtrust_urp_ui: ^9.1.0-13 liquid_flutter: ^22.0.2 dev_dependencies: diff --git a/lib/src/sec_reader.dart b/lib/src/sec_reader.dart index f3e10d2..3b07f49 100644 --- a/lib/src/sec_reader.dart +++ b/lib/src/sec_reader.dart @@ -97,236 +97,29 @@ class SECReader extends CmdWrapper { return SECReader(connectionStrategy: connectionStrategy); } - Future _addCommandToQueue({ - UrpCoreCommand? coreCommand, - UrpSecDeviceCommand? deviceCommand, - }) async { + @override + Future addCoreCmdToQueue( + UrpCoreCommand coreCommand, + ) async { return connectionStrategy.addQueue( UrpSecCommandWrapper( coreCommand: coreCommand, - deviceCommand: deviceCommand, ).writeToBuffer(), target, origin, ); } - /// Pings the device. - @override - Future ping() async { - final cmd = UrpCoreCommand( - command: UrpCommand.urpPing, - ); - await _addCommandToQueue(coreCommand: cmd); - } - - /// Returns the device info. Throws an error if failed. - @override - Future info() async { - final cmd = UrpCoreCommand( - command: UrpCommand.urpGetInfo, - ); - final res = await _addCommandToQueue(coreCommand: cmd); - - if (!res.hasPayload()) { - throw SecReaderException(message: 'Failed to get info'); - } - return UrpDeviceInfo.fromBuffer(res.payload); - } - - /// Returns the power state of the device. Throws an error if failed. - @override - Future getPower() async { - final cmd = UrpCoreCommand( - command: UrpCommand.urpGetPower, - ); - final res = await _addCommandToQueue(coreCommand: cmd); - - if (!res.hasPayload()) { - throw SecReaderException(message: 'Failed to get power state'); - } - return UrpPowerState.fromBuffer(res.payload); - } - - /// Sets the name of the device. - @override - Future setName(String? name) async { - final cmd = UrpCoreCommand( - command: UrpCommand.urpSetName, - setNameParameters: UrpSetNameParameters(name: name), - ); - await _addCommandToQueue(coreCommand: cmd); - } - - /// Returns the devic name. Throws an error if failed. - @override - Future getName() async { - final cmd = UrpCoreCommand( - command: UrpCommand.urpGetName, - ); - final res = await _addCommandToQueue(coreCommand: cmd); - - if (!res.hasPayload()) { - throw SecReaderException(message: 'Failed to get name'); - } - return UrpDeviceName.fromBuffer(res.payload); - } - - /// Pair the device. - @override - Future pair() async { - final cmd = UrpCoreCommand( - command: UrpCommand.urpPair, - ); - await _addCommandToQueue(coreCommand: cmd); - } - - /// Unpair the device. - @override - Future unpair() async { - final cmd = UrpCoreCommand( - command: UrpCommand.urpUnpair, - ); - await _addCommandToQueue(coreCommand: cmd); - } - - /// Starts the DFU mode of the device. - @override - Future startDFU() async { - final cmd = UrpCoreCommand( - command: UrpCommand.urpStartDfu, - ); - await _addCommandToQueue(coreCommand: cmd); - } - - /// Stops the DFU mode of the device. - @override - Future stopDFU() async { - final cmd = UrpCoreCommand( - command: UrpCommand.urpStopDfu, - ); - await _addCommandToQueue(coreCommand: cmd); - } - - /// Puts the device to sleep mode. This will disconnect the device. - @override - Future sleep() async { - final cmd = UrpCoreCommand( - command: UrpCommand.urpSleep, - ); - await _addCommandToQueue(coreCommand: cmd); - } - - /// Turns the device off. This will disconnect the device. - @override - Future off() async { - final cmd = UrpCoreCommand( - command: UrpCommand.urpOff, - ); - await _addCommandToQueue(coreCommand: cmd); - } - - /// Reboots the device. This will disconnect the device. - @override - Future reboot() async { - final cmd = UrpCoreCommand( - command: UrpCommand.urpReboot, - ); - await _addCommandToQueue(coreCommand: cmd); - } - - /// Prevents the device from going to sleep mode. - @override - Future stayAwake() async { - final cmd = UrpCoreCommand( - command: UrpCommand.urpStayAwake, - ); - await _addCommandToQueue(coreCommand: cmd); - } - - /// Returns the public key of the device. Throws an error if failed. - @override - Future getPublicKey() async { - final cmd = UrpCoreCommand( - command: UrpCommand.urpGetPublicKey, - ); - final res = await _addCommandToQueue(coreCommand: cmd); - - if (!res.hasPayload()) { - throw SecReaderException(message: 'Failed to get public key'); - } - return UrpPublicKey.fromBuffer(res.payload); - } - - /// Return the device id. Throws an error if failed. - @override - Future getDeviceId() async { - final cmd = UrpCoreCommand( - command: UrpCommand.urpGetDeviceId, - ); - final res = await _addCommandToQueue(coreCommand: cmd); - - if (!res.hasPayload()) { - throw SecReaderException(message: 'Failed to get public key'); - } - return UrpDeviceId.fromBuffer(res.payload); - } - - /// Identify reader. Triggers the LED to identify the device. - @override - Future identify() async { - final cmd = UrpCoreCommand( - command: UrpCommand.urpIdentify, - ); - await _addCommandToQueue(coreCommand: cmd); - } - - /// Connect AP. Throws an error if failed. - @override - Future connectAP(String ssid, String apk) async { - final cmd = UrpCoreCommand( - command: UrpCommand.urpConnectAp, - apParameters: UrpApParamters(ssid: ssid, password: apk), - ); - final res = await _addCommandToQueue(coreCommand: cmd); - - if (!res.hasPayload()) { - throw SecReaderException(message: 'Failed to connect to AP'); - } - return UrpWifiState.fromBuffer(res.payload); - } - - /// Disconnect AP. - @override - Future disconnectAP() async { - final cmd = UrpCoreCommand( - command: UrpCommand.urpDisconnectAp, - ); - await _addCommandToQueue(coreCommand: cmd); - } - - /// Start AP. Throws an error if failed. - @override - Future startAP(String ssid, String apk) async { - final cmd = UrpCoreCommand( - command: UrpCommand.urpStartAp, - apParameters: UrpApParamters(ssid: ssid, password: apk), - ); - final res = await _addCommandToQueue(coreCommand: cmd); - - if (!res.hasPayload()) { - throw SecReaderException(message: 'Failed to start AP'); - } - return UrpWifiState.fromBuffer(res.payload); - } - - /// Stop AP. - @override - Future stopAP() async { - final cmd = UrpCoreCommand( - command: UrpCommand.urpStopAp, + Future _addDeviceCmdToQueue({ + UrpSecDeviceCommand? deviceCommand, + }) async { + return connectionStrategy.addQueue( + UrpSecCommandWrapper( + deviceCommand: deviceCommand, + ).writeToBuffer(), + target, + origin, ); - await _addCommandToQueue(coreCommand: cmd); } /// Prepares (primes) a measurement for the given [payload]. @@ -336,11 +129,12 @@ class SECReader extends CmdWrapper { primeParameters: UrpSecPrimeParameters(payload: payload), ); try { - final res = await _addCommandToQueue(deviceCommand: cmd); + final res = await _addDeviceCmdToQueue(deviceCommand: cmd); return UrpSecPrimeResponse.fromBuffer(res.payload); } catch (e) { if (e is DeviceError) { - if (e.errorCode != 4) { + if (e.errorCode.value != 4) { + // urpLeaseError rethrow; } final publicKey = await getPublicKey(); @@ -372,7 +166,7 @@ class SECReader extends CmdWrapper { amount: _requestTokenAmount, ), ); - final res = await _addCommandToQueue( + final res = await _addDeviceCmdToQueue( deviceCommand: cmd, ); @@ -391,7 +185,7 @@ class SECReader extends CmdWrapper { command: UrpSecCommand.urpSecSetToken, secureToken: token, ); - await _addCommandToQueue(deviceCommand: cmd); + await _addDeviceCmdToQueue(deviceCommand: cmd); } /// Request the currently installed token from the device @@ -399,7 +193,7 @@ class SECReader extends CmdWrapper { final cmd = UrpSecDeviceCommand( command: UrpSecCommand.urpSecGetToken, ); - final res = await _addCommandToQueue( + final res = await _addDeviceCmdToQueue( deviceCommand: cmd, ); @@ -417,7 +211,7 @@ class SECReader extends CmdWrapper { final cmd = UrpSecDeviceCommand( command: UrpSecCommand.urpSecUnprime, ); - await _addCommandToQueue(deviceCommand: cmd); + await _addDeviceCmdToQueue(deviceCommand: cmd); } /// Measures until detection. Returns the result if successful. @@ -426,22 +220,9 @@ class SECReader extends CmdWrapper { final cmd = UrpSecDeviceCommand( command: UrpSecCommand.urpSecStartMeasurement, ); - final res = await _addCommandToQueue(deviceCommand: cmd); + final res = await _addDeviceCmdToQueue(deviceCommand: cmd); - if (!res.hasPayload()) { - throw SecReaderException( - message: 'Failed to measure!', - type: SecReaderExceptionType.measurementFailed, - ); - } - try { - return UrpSecSecureMeasurement.fromBuffer(res.payload); - } catch (e) { - throw SecReaderException( - message: 'Incompatible device firmware version. Please update!', - type: SecReaderExceptionType.incompatibleFirmware, - ); - } + return UrpSecSecureMeasurement.fromBuffer(res.payload); } /// Stop measurement @@ -449,7 +230,7 @@ class SECReader extends CmdWrapper { final cmd = UrpSecDeviceCommand( command: UrpSecCommand.urpSecStopMeasurement, ); - await _addCommandToQueue(deviceCommand: cmd); + await _addDeviceCmdToQueue(deviceCommand: cmd); } /// Get model info. Returns the result if successful. @@ -458,7 +239,7 @@ class SECReader extends CmdWrapper { final cmd = UrpSecDeviceCommand( command: UrpSecCommand.urpSecGetModelInfo, ); - final res = await _addCommandToQueue(deviceCommand: cmd); + final res = await _addDeviceCmdToQueue(deviceCommand: cmd); if (!res.hasPayload()) { throw Exception('Failed to get model info'); diff --git a/pubspec.yaml b/pubspec.yaml index d1e3d5c..3eda5ba 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -13,14 +13,14 @@ dependencies: sdk: flutter intl: ^0.20.2 liquid_flutter: ^22.0.4 - mtrust_urp_core: ^9.1.0-12 - mtrust_urp_types: ^6.2.0 - mtrust_urp_ui: ^9.1.0-12 + mtrust_urp_core: ^9.1.0-13 + mtrust_urp_types: ^6.2.1 + mtrust_urp_ui: ^9.1.0-13 dev_dependencies: flutter_test: sdk: flutter golden_toolkit: ^0.15.0 - mtrust_urp_virtual_strategy: ^9.1.0-12 + mtrust_urp_virtual_strategy: ^9.1.0-13 very_good_analysis: ^5.1.0 flutter: uses-material-design: true