Skip to content

Commit

Permalink
Merge pull request #58 from tw-mosip/release-0.3.x
Browse files Browse the repository at this point in the history
[Release 19-04-2023] Stability improvements & Windows build fix
  • Loading branch information
krishnakumar4a4 authored Apr 19, 2023
2 parents b16c5ea + 7a6ecdc commit cf22b78
Show file tree
Hide file tree
Showing 111 changed files with 335 additions and 7,885 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ interface ICentralListener {
fun onSubscriptionFailure(charUUID: UUID, err: Int)
fun onNotificationReceived(charUUID: UUID, value: ByteArray?)
fun onClosed()
fun onException(exception: Throwable)
fun onException(exception: Exception)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ const val MTU_HEADER_SIZE = 3
const val REQUEST_MTU_TIME_OUT = -1

class Controller(val context: Context) {
private lateinit var scanner: Scanner
private val logTag = getLogTag(javaClass.simpleName)

private var gattClient: GattClient? = null
private var scanner: Scanner? = null
private lateinit var messageSender: IMessageSender
private var requestedMTUValue = -1
private lateinit var mtuValues : Array<Int>
Expand All @@ -34,7 +34,7 @@ class Controller(val context: Context) {

fun scan(scanStartMessage: ScanStartMessage) {
scanner = Scanner(context)
scanner.start(
scanner?.start(
scanStartMessage.serviceUUID,
scanStartMessage.advPayload,
this::onDeviceFound,
Expand All @@ -43,7 +43,7 @@ class Controller(val context: Context) {
}

fun stopScan() {
scanner.stopScan()
scanner?.stopScan()
}

@SuppressLint("MissingPermission")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import android.util.Log
import io.mosip.tuvali.ble.central.impl.Controller
import io.mosip.tuvali.ble.central.ICentralListener
import io.mosip.tuvali.ble.central.state.message.*
import io.mosip.tuvali.openid4vpble.exception.exception.StateHandlerException
import io.mosip.tuvali.openid4vpble.exception.StateHandlerException
import io.mosip.tuvali.transfer.Util.Companion.getLogTag

class StateHandler(
Expand Down Expand Up @@ -255,9 +255,9 @@ class StateHandler(
override fun dispatchMessage(msg: Message) {
try {
super.dispatchMessage(msg)
} catch (e: Throwable) {
} catch (e: Exception) {
listener.onException(StateHandlerException("Exception in Central State Handler", e))
Log.d(logTag, "dispatchMessage " + e.message)
Log.e(logTag, "dispatchMessage " + e.message)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ interface IPeripheralListener {
fun onSendDataNotified(uuid: UUID, isSent: Boolean)
fun onClosed()
fun onMTUChanged(mtu: Int)
fun onException(exception: Throwable)
fun onException(exception: Exception)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import android.util.Log
import io.mosip.tuvali.ble.peripheral.state.IMessageSender
import io.mosip.tuvali.ble.peripheral.state.message.*
import io.mosip.tuvali.transfer.Util.Companion.getLogTag
import java.lang.Exception

const val MTU_HEADER_SIZE = 3

class Controller(val context: Context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import android.util.Log
import com.facebook.common.util.Hex
import io.mosip.tuvali.ble.peripheral.IPeripheralListener
import io.mosip.tuvali.ble.peripheral.impl.Controller
import io.mosip.tuvali.openid4vpble.exception.exception.StateHandlerException
import io.mosip.tuvali.openid4vpble.exception.StateHandlerException
import io.mosip.tuvali.ble.peripheral.state.message.*
import io.mosip.tuvali.transfer.Util.Companion.getLogTag

Expand Down Expand Up @@ -156,9 +156,9 @@ class StateHandler(
override fun dispatchMessage(msg: Message) {
try {
super.dispatchMessage(msg)
} catch (e: Throwable) {
} catch (e: Exception) {
peripheralListener.onException(StateHandlerException("Exception in Peripheral State Handler", e))
Log.d(logTag, "dispatchMessage " + e.message)
Log.e(logTag, "dispatchMessage " + e.message)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.mosip.tuvali.common.safeExecute

import io.mosip.tuvali.openid4vpble.exception.OpenIdBLEExceptionHandler

class TryExecuteSync(private val bleExceptionHandler: OpenIdBLEExceptionHandler) {
private val mutex = Object()

fun <T> run(fn: () -> T): T? {
var returnValue: T? = null;

synchronized(mutex) {
try {
returnValue = fn()
} catch (e: Exception) {
bleExceptionHandler.handleException(Exception("Unknown Exception", e))
}
}

return returnValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,20 @@ package io.mosip.tuvali.openid4vpble
import android.util.Log
import com.facebook.react.bridge.*
import com.facebook.react.modules.core.DeviceEventManagerModule.RCTDeviceEventEmitter
import io.mosip.tuvali.verifier.Verifier
import io.mosip.tuvali.wallet.Wallet
import io.mosip.tuvali.common.safeExecute.TryExecuteSync
import io.mosip.tuvali.openid4vpble.exception.OpenIdBLEExceptionHandler
import org.json.JSONObject
import io.mosip.tuvali.transfer.Util.Companion.getLogTag
import io.mosip.tuvali.verifier.Verifier
import io.mosip.tuvali.wallet.Wallet

class Openid4vpBleModule(private val reactContext: ReactApplicationContext) :
ReactContextBaseJavaModule(reactContext) {
private val logTag = getLogTag(javaClass.simpleName)
private var verifier: Verifier? = null
private var wallet: Wallet? = null
private val mutex = Object()
private var walletExceptionHandler = OpenIdBLEExceptionHandler(this::emitNearbyErrorEvent, this::stopBLE)

init {
Thread.setDefaultUncaughtExceptionHandler { _, exception ->
walletExceptionHandler.handleException(exception)
}
}
private var bleExceptionHandler = OpenIdBLEExceptionHandler(this::emitNearbyErrorEvent, this::stopBLE)
private val tryExecuteSync = TryExecuteSync(bleExceptionHandler);

//Inji contract requires double quotes around the states.
enum class VCResponseStates(val value: String) {
Expand Down Expand Up @@ -49,28 +44,22 @@ class Openid4vpBleModule(private val reactContext: ReactApplicationContext) :
@ReactMethod(isBlockingSynchronousMethod = true)
fun getConnectionParameters(): String {
Log.d(logTag, "getConnectionParameters new verifier object at ${System.nanoTime()}")
synchronized(mutex) {

return tryExecuteSync.run {
if (verifier == null) {
Log.d(logTag, "synchronized getConnectionParameters new verifier object at ${System.nanoTime()}")
verifier = Verifier(reactContext, this::emitNearbyMessage, this::emitNearbyEvent, this::onException)
verifier = Verifier(reactContext, this::emitNearbyMessage, this::emitNearbyEvent, bleExceptionHandler::handleException)
verifier?.generateKeyPair()
}

val payload = verifier?.getAdvIdentifier("OVPMOSIP")
Log.d(
logTag,
"synchronized getConnectionParameters called with adv identifier $payload at ${System.nanoTime()} and verifier hashcode: ${verifier.hashCode()}"
)
return "{\"cid\":\"ilB8l\",\"pk\":\"${payload}\"}"
}
}

private fun onException(exception: Throwable){
if(exception.cause != null){
Log.e(logTag, "Exception: ${exception.message}");
walletExceptionHandler.handleException(exception.cause!!)
} else {
walletExceptionHandler.handleException(exception)
}
return@run "{\"cid\":\"ilB8l\",\"pk\":\"${payload}\"}"
}.orEmpty()
}

@ReactMethod(isBlockingSynchronousMethod = true)
Expand All @@ -81,10 +70,11 @@ class Openid4vpBleModule(private val reactContext: ReactApplicationContext) :
@ReactMethod(isBlockingSynchronousMethod = true)
fun setConnectionParameters(params: String) {
Log.d(logTag, "setConnectionParameters at ${System.nanoTime()}")
synchronized(mutex) {

tryExecuteSync.run {
if (wallet == null) {
Log.d(logTag, "synchronized setConnectionParameters new wallet object at ${System.nanoTime()}")
wallet = Wallet(reactContext, this::emitNearbyMessage, this::emitNearbyEvent, this::onException)
wallet = Wallet(reactContext, this::emitNearbyMessage, this::emitNearbyEvent, bleExceptionHandler::handleException)
}
val paramsObj = JSONObject(params)
val firstPartOfPk = paramsObj.getString("pk")
Expand All @@ -99,8 +89,9 @@ class Openid4vpBleModule(private val reactContext: ReactApplicationContext) :
@ReactMethod
fun createConnection(mode: String, callback: Callback) {
Log.d(logTag, "createConnection: received request with mode $mode at ${System.nanoTime()}")
synchronized(mutex) {
Log.d(logTag, "synchronized createConnection: received request with mode $mode at ${System.nanoTime()}")

tryExecuteSync.run {
Log.d(logTag, "synchronized createConnection: received request with mode $mode at ${System.nanoTime()}")
when (mode) {
"advertiser" -> {
verifier?.startAdvertisement("OVPMOSIP", callback)
Expand All @@ -119,15 +110,15 @@ class Openid4vpBleModule(private val reactContext: ReactApplicationContext) :
fun destroyConnection(callback: Callback) {
//TODO: Make sure callback can be called only once[Can be done once wallet and verifier split into different modules]
Log.d(logTag, "destroyConnection called at ${System.nanoTime()}")

stopBLE(callback)
tryExecuteSync.run {
stopBLE(callback)
}
}

private fun stopBLE(callback: Callback) {
synchronized(mutex) {
if (wallet == null && verifier == null) {
if (wallet == null && verifier == null) {
callback()
} else {
} else {
if (wallet != null) {
Log.d(logTag, "synchronized destroyConnection called for wallet at ${System.nanoTime()}")
stopWallet { callback() }
Expand All @@ -137,7 +128,6 @@ class Openid4vpBleModule(private val reactContext: ReactApplicationContext) :
stopVerifier { callback() }
}
}
}
}

private fun stopVerifier(onDestroy: Callback) {
Expand Down Expand Up @@ -165,21 +155,27 @@ class Openid4vpBleModule(private val reactContext: ReactApplicationContext) :
@ReactMethod
fun send(message: String, callback: Callback) {
Log.d(logTag, "send: message $message at ${System.nanoTime()}")
val messageSplits = message.split("\n", limit = 2)
when (messageSplits[0]) {
NearbyEvents.EXCHANGE_RECEIVER_INFO.value -> {
callback()
}
NearbyEvents.EXCHANGE_SENDER_INFO.value -> {
callback()
wallet?.writeToIdentifyRequest()
}
NearbyEvents.SEND_VC.value -> {
callback()
wallet?.sendData(messageSplits[1])
}
NearbyEvents.SEND_VC_RESPONSE.value -> {
verifier?.notifyVerificationStatus(messageSplits[1] == VCResponseStates.ACCEPTED.value)

tryExecuteSync.run {
val messageSplits = message.split("\n", limit = 2)
when (messageSplits[0]) {
NearbyEvents.EXCHANGE_RECEIVER_INFO.value -> {
callback()
}
NearbyEvents.EXCHANGE_SENDER_INFO.value -> {
callback()
wallet?.writeToIdentifyRequest()
}
NearbyEvents.SEND_VC.value -> {
callback()
wallet?.sendData(messageSplits[1])
}
NearbyEvents.SEND_VC_RESPONSE.value -> {
verifier?.notifyVerificationStatus(messageSplits[1] == VCResponseStates.ACCEPTED.value)
}
else -> {
Log.d(logTag, "send: received send with unrecognized event")
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package io.mosip.tuvali.openid4vpble.exception

import android.util.Log
import com.facebook.react.bridge.Callback
import com.facebook.react.bridge.CallbackImpl
import io.mosip.tuvali.transfer.Util
import io.mosip.tuvali.verifier.exception.VerifierException
import io.mosip.tuvali.verifier.exception.VerifierExceptionHandler
Expand Down Expand Up @@ -35,7 +34,11 @@ class OpenIdBLEExceptionHandler(private val sendError: (String) -> Unit, private
}
}

stopBle {}
try{
stopBle {}
} catch (e: Exception) {
Log.d(logTag,"Failed to stop BLE connection while handling exception: $e")
}
}

private fun handleUnknownException(e: Throwable) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package io.mosip.tuvali.openid4vpble.exception

class StateHandlerException(message: String, cause: Throwable): Exception(message, cause) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package io.mosip.tuvali.openid4vpble.exception

class TransferHandlerException(message: String, cause: Throwable): Exception(message, cause) {
}

This file was deleted.

This file was deleted.

7 changes: 4 additions & 3 deletions android/src/main/java/io/mosip/tuvali/verifier/Verifier.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import io.mosip.tuvali.transfer.TransferReportRequest
import io.mosip.tuvali.transfer.Util
import io.mosip.tuvali.transfer.Util.Companion.getLogTag
import io.mosip.tuvali.verifier.exception.UnsupportedMTUSizeException
import io.mosip.tuvali.verifier.exception.VerifierException
import io.mosip.tuvali.verifier.transfer.ITransferListener
import io.mosip.tuvali.verifier.transfer.TransferHandler
import io.mosip.tuvali.verifier.transfer.message.InitTransferMessage
Expand All @@ -31,7 +32,7 @@ class Verifier(
context: Context,
private val messageResponseListener: (String, String) -> Unit,
private val eventResponseListener: (String) -> Unit,
private val onBLEException: (Throwable) -> Unit
private val handleException: (Exception) -> Unit
) :
IPeripheralListener, ITransferListener {
private var secretsTranslator: SecretsTranslator? = null;
Expand Down Expand Up @@ -200,8 +201,8 @@ class Verifier(
}
}

override fun onException(e: Throwable) {
onBLEException(e)
override fun onException(exception: Exception) {
handleException(VerifierException("Exception in Verifier", exception))
}

override fun onClosed() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.mosip.tuvali.verifier.exception

class ReadFromRemoteException: Throwable() {
class ReadFromRemoteException: Exception() {
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package io.mosip.tuvali.verifier.exception

open class VerifierException(message: String): Throwable(message)
open class VerifierException(message: String, cause: Throwable? = null): Exception(message, cause)
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package io.mosip.tuvali.verifier.exception

import android.util.Log
import io.mosip.tuvali.openid4vpble.exception.StateHandlerException
import io.mosip.tuvali.openid4vpble.exception.TransferHandlerException
import io.mosip.tuvali.transfer.Util

class VerifierExceptionHandler(val sendError: (String) -> Unit) {
private val logTag = Util.getLogTag(javaClass.simpleName)

fun handleException(e: VerifierException){
Log.e(logTag, "Verifier Exception: $e")
when (e.cause?.javaClass?.simpleName) {
StateHandlerException::class.simpleName -> Log.e(logTag, "Verifier State Handler Exception: ${e.cause}")
TransferHandlerException::class.simpleName -> Log.e(logTag, "Verifier Transfer Handler Exception: ${e.cause}")
else -> Log.e(logTag, "Verifier Exception: $e")
}
sendError(e.message ?: "Something went wrong in Verifier: ${e.cause}")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ interface ITransferListener {
fun sendDataOverNotification(charUUID: UUID, data: ByteArray)
fun onResponseReceived(data: ByteArray)
fun onResponseReceivedFailed(errorMsg: String)
fun onException(exception: Throwable )
fun onException(exception: Exception )
}
Loading

0 comments on commit cf22b78

Please sign in to comment.