Compare commits

..

3 commits

View file

@ -11,6 +11,8 @@ import net.typeblog.lpac_jni.LocalProfileInfo
import net.typeblog.lpac_jni.LocalProfileNotification
import net.typeblog.lpac_jni.ProfileDownloadCallback
import net.typeblog.lpac_jni.Version
import java.util.concurrent.locks.ReentrantLock
import kotlin.concurrent.withLock
class LocalProfileAssistantImpl(
isdrAid: ByteArray,
@ -74,6 +76,10 @@ class LocalProfileAssistantImpl(
}
}
// Controls concurrency of every single method in this class, since
// the C-side is explicitly NOT thread-safe
private val lock = ReentrantLock()
private val apduInterface = ApduInterfaceWrapper(rawApduInterface)
private val httpInterface = HttpInterfaceWrapper(rawHttpInterface)
@ -105,15 +111,15 @@ class LocalProfileAssistantImpl(
}
override val profiles: List<LocalProfileInfo>
@Synchronized
get() {
get() = lock.withLock {
val head = LpacJni.es10cGetProfilesInfo(contextHandle)
var curr = head
val ret = mutableListOf<LocalProfileInfo>()
while (curr != 0L) {
val state = LocalProfileInfo.State.fromString(LpacJni.profileGetStateString(curr))
val clazz = LocalProfileInfo.Clazz.fromString(LpacJni.profileGetClassString(curr))
ret.add(LocalProfileInfo(
ret.add(
LocalProfileInfo(
LpacJni.profileGetIccid(curr),
state,
LpacJni.profileGetName(curr),
@ -121,7 +127,8 @@ class LocalProfileAssistantImpl(
LpacJni.profileGetServiceProvider(curr),
LpacJni.profileGetIsdpAid(curr),
clazz
))
)
)
curr = LpacJni.profilesNext(curr)
}
@ -130,35 +137,43 @@ class LocalProfileAssistantImpl(
}
override val notifications: List<LocalProfileNotification>
@Synchronized
get() {
get() = lock.withLock {
val head = LpacJni.es10bListNotification(contextHandle)
var curr = head
try {
val ret = mutableListOf<LocalProfileNotification>()
while (curr != 0L) {
ret.add(LocalProfileNotification(
ret.add(
LocalProfileNotification(
LpacJni.notificationGetSeq(curr),
LocalProfileNotification.Operation.fromString(LpacJni.notificationGetOperationString(curr)),
LocalProfileNotification.Operation.fromString(
LpacJni.notificationGetOperationString(
curr
)
),
LpacJni.notificationGetAddress(curr),
LpacJni.notificationGetIccid(curr),
))
)
)
curr = LpacJni.notificationsNext(curr)
}
LpacJni.notificationsFree(head)
return ret.sortedBy { it.seqNumber }.reversed()
} finally {
LpacJni.notificationsFree(head)
}
}
override val eID: String
@Synchronized
get() = LpacJni.es10cGetEid(contextHandle)!!
get() = lock.withLock { LpacJni.es10cGetEid(contextHandle)!! }
override val euiccInfo2: EuiccInfo2?
@Synchronized
get() {
get() = lock.withLock {
val cInfo = LpacJni.es10cexGetEuiccInfo2(contextHandle)
if (cInfo == 0L) return null
val ret = EuiccInfo2(
try {
return EuiccInfo2(
Version(LpacJni.euiccInfo2GetSGP22Version(cInfo)),
Version(LpacJni.euiccInfo2GetProfileVersion(cInfo)),
Version(LpacJni.euiccInfo2GetEuiccFirmwareVersion(cInfo)),
@ -182,27 +197,27 @@ class LocalProfileAssistantImpl(
}
},
)
} finally {
LpacJni.euiccInfo2Free(cInfo)
return ret
}
}
@Synchronized
override fun enableProfile(iccid: String, refresh: Boolean): Boolean =
override fun enableProfile(iccid: String, refresh: Boolean): Boolean = lock.withLock {
LpacJni.es10cEnableProfile(contextHandle, iccid, refresh) == 0
}
@Synchronized
override fun disableProfile(iccid: String, refresh: Boolean): Boolean =
override fun disableProfile(iccid: String, refresh: Boolean): Boolean = lock.withLock {
LpacJni.es10cDisableProfile(contextHandle, iccid, refresh) == 0
}
@Synchronized
override fun deleteProfile(iccid: String): Boolean =
override fun deleteProfile(iccid: String): Boolean = lock.withLock {
LpacJni.es10cDeleteProfile(contextHandle, iccid) == 0
}
@Synchronized
override fun downloadProfile(smdp: String, matchingId: String?, imei: String?,
confirmationCode: String?, callback: ProfileDownloadCallback) {
override fun downloadProfile(
smdp: String, matchingId: String?, imei: String?,
confirmationCode: String?, callback: ProfileDownloadCallback
) = lock.withLock {
val res = LpacJni.downloadProfile(
contextHandle,
smdp,
@ -229,18 +244,17 @@ class LocalProfileAssistantImpl(
}
}
@Synchronized
override fun deleteNotification(seqNumber: Long): Boolean =
override fun deleteNotification(seqNumber: Long): Boolean = lock.withLock {
LpacJni.es10bDeleteNotification(contextHandle, seqNumber) == 0
}
@Synchronized
override fun handleNotification(seqNumber: Long): Boolean =
override fun handleNotification(seqNumber: Long): Boolean = lock.withLock {
LpacJni.handleNotification(contextHandle, seqNumber).also {
Log.d(TAG, "handleNotification $seqNumber = $it")
} == 0
}
@Synchronized
override fun setNickname(iccid: String, nickname: String) {
override fun setNickname(iccid: String, nickname: String) = lock.withLock {
val encoded = try {
Charsets.UTF_8.encode(nickname).array()
} catch (e: CharacterCodingException) {
@ -259,11 +273,12 @@ class LocalProfileAssistantImpl(
}
override fun euiccMemoryReset() {
lock.withLock {
LpacJni.es10cEuiccMemoryReset(contextHandle)
}
}
@Synchronized
override fun close() {
override fun close() = lock.withLock {
if (!finalized) {
LpacJni.euiccFini(contextHandle)
LpacJni.destroyContext(contextHandle)