Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
267 changes: 24 additions & 243 deletions lib/src/sec_reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,236 +97,29 @@ class SECReader extends CmdWrapper {
return SECReader(connectionStrategy: connectionStrategy);
}

Future<UrpResponse> _addCommandToQueue({
UrpCoreCommand? coreCommand,
UrpSecDeviceCommand? deviceCommand,
}) async {
@override
Future<UrpResponse> addCoreCmdToQueue(
UrpCoreCommand coreCommand,
) async {
return connectionStrategy.addQueue(
UrpSecCommandWrapper(
coreCommand: coreCommand,
deviceCommand: deviceCommand,
).writeToBuffer(),
target,
origin,
);
}

/// Pings the device.
@override
Future<void> ping() async {
final cmd = UrpCoreCommand(
command: UrpCommand.urpPing,
);
await _addCommandToQueue(coreCommand: cmd);
}

/// Returns the device info. Throws an error if failed.
@override
Future<UrpDeviceInfo> 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<UrpPowerState> 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<void> 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<UrpDeviceName> 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<void> pair() async {
final cmd = UrpCoreCommand(
command: UrpCommand.urpPair,
);
await _addCommandToQueue(coreCommand: cmd);
}

/// Unpair the device.
@override
Future<void> unpair() async {
final cmd = UrpCoreCommand(
command: UrpCommand.urpUnpair,
);
await _addCommandToQueue(coreCommand: cmd);
}

/// Starts the DFU mode of the device.
@override
Future<void> startDFU() async {
final cmd = UrpCoreCommand(
command: UrpCommand.urpStartDfu,
);
await _addCommandToQueue(coreCommand: cmd);
}

/// Stops the DFU mode of the device.
@override
Future<void> 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<void> sleep() async {
final cmd = UrpCoreCommand(
command: UrpCommand.urpSleep,
);
await _addCommandToQueue(coreCommand: cmd);
}

/// Turns the device off. This will disconnect the device.
@override
Future<void> off() async {
final cmd = UrpCoreCommand(
command: UrpCommand.urpOff,
);
await _addCommandToQueue(coreCommand: cmd);
}

/// Reboots the device. This will disconnect the device.
@override
Future<void> reboot() async {
final cmd = UrpCoreCommand(
command: UrpCommand.urpReboot,
);
await _addCommandToQueue(coreCommand: cmd);
}

/// Prevents the device from going to sleep mode.
@override
Future<void> 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<UrpPublicKey> 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<UrpDeviceId> 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<void> identify() async {
final cmd = UrpCoreCommand(
command: UrpCommand.urpIdentify,
);
await _addCommandToQueue(coreCommand: cmd);
}

/// Connect AP. Throws an error if failed.
@override
Future<UrpWifiState> 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<void> disconnectAP() async {
final cmd = UrpCoreCommand(
command: UrpCommand.urpDisconnectAp,
);
await _addCommandToQueue(coreCommand: cmd);
}

/// Start AP. Throws an error if failed.
@override
Future<UrpWifiState> 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<void> stopAP() async {
final cmd = UrpCoreCommand(
command: UrpCommand.urpStopAp,
Future<UrpResponse> _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].
Expand All @@ -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();
Expand Down Expand Up @@ -372,7 +166,7 @@ class SECReader extends CmdWrapper {
amount: _requestTokenAmount,
),
);
final res = await _addCommandToQueue(
final res = await _addDeviceCmdToQueue(
deviceCommand: cmd,
);

Expand All @@ -391,15 +185,15 @@ 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
Future<UrpSecureToken> getCurrentToken() async {
final cmd = UrpSecDeviceCommand(
command: UrpSecCommand.urpSecGetToken,
);
final res = await _addCommandToQueue(
final res = await _addDeviceCmdToQueue(
deviceCommand: cmd,
);

Expand All @@ -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.
Expand All @@ -426,30 +220,17 @@ 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
Future<void> stopMeasurement() async {
final cmd = UrpSecDeviceCommand(
command: UrpSecCommand.urpSecStopMeasurement,
);
await _addCommandToQueue(deviceCommand: cmd);
await _addDeviceCmdToQueue(deviceCommand: cmd);
}

/// Get model info. Returns the result if successful.
Expand All @@ -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');
Expand Down
8 changes: 4 additions & 4 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down