Compare commits

..

1 commit

Author SHA1 Message Date
bd4ab0fdf0
refactor: lpac-jni bridge 2025-03-11 04:07:38 +08:00
4 changed files with 112 additions and 41 deletions

View file

@ -14,21 +14,30 @@ static jmethodID method_http_transmit;
static jfieldID field_resp_rcode;
static jfieldID field_resp_data;
#define APDU_INTERFACE_CLASS PACKAGE_NAME "/ApduInterface"
#define HTTP_INTERFACE_CLASS PACKAGE_NAME "/HttpInterface"
#define HTTP_RESPONSE_CLASS HTTP_INTERFACE_CLASS "$HttpResponse"
void interface_wrapper_init(JNIEnv *env) {
jclass apdu_class = (*env)->FindClass(env, "net/typeblog/lpac_jni/ApduInterface");
jclass apdu_class = (*env)->FindClass(env, APDU_INTERFACE_CLASS);
method_apdu_connect = (*env)->GetMethodID(env, apdu_class, "connect", "()V");
method_apdu_disconnect = (*env)->GetMethodID(env, apdu_class, "disconnect", "()V");
method_apdu_logical_channel_open = (*env)->GetMethodID(env, apdu_class, "logicalChannelOpen",
"([B)I");
method_apdu_logical_channel_close = (*env)->GetMethodID(env, apdu_class, "logicalChannelClose",
"(I)V");
method_apdu_logical_channel_open = (*env)->GetMethodID(env, apdu_class, "logicalChannelOpen", "([B)I");
method_apdu_logical_channel_close = (*env)->GetMethodID(env, apdu_class, "logicalChannelClose", "(I)V");
method_apdu_transmit = (*env)->GetMethodID(env, apdu_class, "transmit", "(I[B)[B");
jclass http_class = (*env)->FindClass(env, "net/typeblog/lpac_jni/HttpInterface");
method_http_transmit = (*env)->GetMethodID(env, http_class, "transmit",
"(Ljava/lang/String;[BLjava/util/List;)Lnet/typeblog/lpac_jni/HttpInterface$HttpResponse;");
jclass http_class = (*env)->FindClass(env, HTTP_INTERFACE_CLASS);
method_http_transmit = (*env)->GetMethodID(
env, http_class, "transmit",
"("
"Ljava/lang/String;" // url
"[B" // byte array
"Ljava/util/List;" // headers
")"
"L" HTTP_RESPONSE_CLASS ";"
);
jclass resp_class = (*env)->FindClass(env, "net/typeblog/lpac_jni/HttpInterface$HttpResponse");
jclass resp_class = (*env)->FindClass(env, HTTP_RESPONSE_CLASS);
field_resp_rcode = (*env)->GetFieldID(env, resp_class, "rcode", "I");
field_resp_data = (*env)->GetFieldID(env, resp_class, "data", "[B");
}
@ -36,7 +45,7 @@ void interface_wrapper_init(JNIEnv *env) {
static int apdu_interface_connect(struct euicc_ctx *ctx) {
LPAC_JNI_SETUP_ENV;
(*env)->CallVoidMethod(env, LPAC_JNI_CTX(ctx)->apdu_interface, method_apdu_connect);
LPAC_JNI_EXCEPTION_RETURN;
LPAC_JNI_EXCEPTION_RETURN
return 0;
}
@ -52,7 +61,7 @@ apdu_interface_logical_channel_open(struct euicc_ctx *ctx, const uint8_t *aid, u
(*env)->SetByteArrayRegion(env, jbarr, 0, aid_len, (const jbyte *) aid);
jint ret = (*env)->CallIntMethod(env, LPAC_JNI_CTX(ctx)->apdu_interface,
method_apdu_logical_channel_open, jbarr);
LPAC_JNI_EXCEPTION_RETURN;
LPAC_JNI_EXCEPTION_RETURN
LPAC_JNI_CTX(ctx)->logical_channel_id = ret;
return ret;
}
@ -71,13 +80,13 @@ apdu_interface_transmit(struct euicc_ctx *ctx, uint8_t **rx, uint32_t *rx_len, c
uint32_t tx_len) {
const int logic_channel = LPAC_JNI_CTX(ctx)->logical_channel_id;
LPAC_JNI_SETUP_ENV;
jbyteArray txArr = (*env)->NewByteArray(env, tx_len);
(*env)->SetByteArrayRegion(env, txArr, 0, tx_len, (const jbyte *) tx);
jbyteArray txArr = (*env)->NewByteArray(env, (jsize) tx_len);
(*env)->SetByteArrayRegion(env, txArr, 0, (jsize) tx_len, (const jbyte *) tx);
jbyteArray ret = (jbyteArray) (*env)->CallObjectMethod(
env, LPAC_JNI_CTX(ctx)->apdu_interface,
method_apdu_transmit, logic_channel, txArr
);
LPAC_JNI_EXCEPTION_RETURN;
LPAC_JNI_EXCEPTION_RETURN
*rx_len = (*env)->GetArrayLength(env, ret);
*rx = calloc(*rx_len, sizeof(uint8_t));
(*env)->GetByteArrayRegion(env, ret, 0, (jsize) *rx_len, (jbyte *) *rx);
@ -99,7 +108,7 @@ http_interface_transmit(struct euicc_ctx *ctx, const char *url, uint32_t *rcode,
jobject ret = (*env)->CallObjectMethod(env, LPAC_JNI_CTX(ctx)->http_interface,
method_http_transmit, jurl, txArr, header_list);
LPAC_JNI_EXCEPTION_RETURN;
LPAC_JNI_EXCEPTION_RETURN
*rcode = (*env)->GetIntField(env, ret, field_resp_rcode);
jbyteArray rxArr = (jbyteArray) (*env)->GetObjectField(env, ret, field_resp_data);
*rx_len = (*env)->GetArrayLength(env, rxArr);

View file

@ -174,18 +174,25 @@ Java_net_typeblog_lpac_1jni_LpacJni_downloadProfile(
}
JNIEXPORT void JNICALL
Java_net_typeblog_lpac_1jni_LpacJni_cancelSessions(JNIEnv *env, jobject thiz, jlong handle) {
Java_net_typeblog_lpac_1jni_LpacJni_cancelSessions(
__attribute__((unused)) JNIEnv *env,
__attribute__((unused)) jobject thiz,
jlong handle
) {
struct euicc_ctx *ctx = (struct euicc_ctx *) handle;
es9p_cancel_session(ctx);
es10b_cancel_session(ctx, ES10B_CANCEL_SESSION_REASON_UNDEFINED);
euicc_http_cleanup(ctx);
}
#define QUOTE(S) #S
#define ERRCODE_ENUM_TO_STRING(VARIANT) case VARIANT: return toJString(env, QUOTE(VARIANT))
#define ERRCODE_ENUM_TO_STRING(VARIANT) case VARIANT: return toJString(env, #VARIANT)
JNIEXPORT jstring JNICALL
Java_net_typeblog_lpac_1jni_LpacJni_downloadErrCodeToString(JNIEnv *env, jobject thiz, jint code) {
Java_net_typeblog_lpac_1jni_LpacJni_downloadErrCodeToString(
JNIEnv *env,
__attribute__((unused)) jobject thiz,
jint code
) {
switch (code) {
ERRCODE_ENUM_TO_STRING(ES10B_ERROR_REASON_INCORRECT_INPUT_VALUES);
ERRCODE_ENUM_TO_STRING(ES10B_ERROR_REASON_INVALID_SIGNATURE);

View file

@ -16,7 +16,7 @@ JavaVM *jvm = NULL;
#define LOCAL_PROFILE_INFO_CLASS "net/typeblog/lpac_jni/LocalProfileInfo"
jint JNI_OnLoad(JavaVM *vm, void *reserved) {
jint JNI_OnLoad(JavaVM *vm, __attribute__((unused)) void *reserved) {
jvm = vm;
LPAC_JNI_SETUP_ENV;
@ -30,7 +30,7 @@ jint JNI_OnLoad(JavaVM *vm, void *reserved) {
JNIEXPORT jlong JNICALL
Java_net_typeblog_lpac_1jni_LpacJni_createContext(
JNIEnv *env,
jobject thiz,
__attribute__((unused)) jobject thiz,
jbyteArray isdr_aid,
jobject apdu_interface,
jobject http_interface
@ -61,7 +61,11 @@ Java_net_typeblog_lpac_1jni_LpacJni_createContext(
}
JNIEXPORT void JNICALL
Java_net_typeblog_lpac_1jni_LpacJni_destroyContext(JNIEnv *env, jobject thiz, jlong handle) {
Java_net_typeblog_lpac_1jni_LpacJni_destroyContext(
JNIEnv *env,
__attribute__((unused)) jobject thiz,
jlong handle
) {
struct euicc_ctx *ctx = (struct euicc_ctx *) handle;
struct lpac_jni_ctx *jni_ctx = LPAC_JNI_CTX(ctx);
@ -73,26 +77,42 @@ Java_net_typeblog_lpac_1jni_LpacJni_destroyContext(JNIEnv *env, jobject thiz, jl
}
JNIEXPORT jint JNICALL
Java_net_typeblog_lpac_1jni_LpacJni_euiccInit(JNIEnv *env, jobject thiz, jlong handle) {
Java_net_typeblog_lpac_1jni_LpacJni_euiccInit(
__attribute__((unused)) JNIEnv *env,
__attribute__((unused)) jobject thiz,
jlong handle
) {
struct euicc_ctx *ctx = (struct euicc_ctx *) handle;
return euicc_init(ctx);
}
JNIEXPORT void JNICALL
Java_net_typeblog_lpac_1jni_LpacJni_euiccFini(JNIEnv *env, jobject thiz, jlong handle) {
Java_net_typeblog_lpac_1jni_LpacJni_euiccFini(
__attribute__((unused)) JNIEnv *env,
__attribute__((unused)) jobject thiz,
jlong handle
) {
struct euicc_ctx *ctx = (struct euicc_ctx *) handle;
euicc_fini(ctx);
}
JNIEXPORT void JNICALL
Java_net_typeblog_lpac_1jni_LpacJni_euiccSetMss(JNIEnv *env, jobject thiz, jlong handle,
jbyte mss) {
Java_net_typeblog_lpac_1jni_LpacJni_euiccSetMss(
__attribute__((unused)) JNIEnv *env,
__attribute__((unused)) jobject thiz,
jlong handle,
jbyte mss
) {
struct euicc_ctx *ctx = (struct euicc_ctx *) handle;
ctx->es10x_mss = (uint8_t) mss;
}
JNIEXPORT jstring JNICALL
Java_net_typeblog_lpac_1jni_LpacJni_es10cGetEid(JNIEnv *env, jobject thiz, jlong handle) {
Java_net_typeblog_lpac_1jni_LpacJni_es10cGetEid(
__attribute__((unused)) JNIEnv *env,
__attribute__((unused)) jobject thiz,
jlong handle
) {
struct euicc_ctx *ctx = (struct euicc_ctx *) handle;
char *buf = NULL;
@ -155,8 +175,13 @@ Java_net_typeblog_lpac_1jni_LpacJni_es10cGetProfilesInfo(
}
JNIEXPORT jint JNICALL
Java_net_typeblog_lpac_1jni_LpacJni_es10cEnableProfile(JNIEnv *env, jobject thiz, jlong handle,
jstring iccid, jboolean refresh) {
Java_net_typeblog_lpac_1jni_LpacJni_es10cEnableProfile(
JNIEnv *env,
__attribute__((unused)) jobject thiz,
jlong handle,
jstring iccid,
jboolean refresh
) {
struct euicc_ctx *ctx = (struct euicc_ctx *) handle;
const char *_iccid = NULL;
int ret;
@ -168,8 +193,13 @@ Java_net_typeblog_lpac_1jni_LpacJni_es10cEnableProfile(JNIEnv *env, jobject thiz
}
JNIEXPORT jint JNICALL
Java_net_typeblog_lpac_1jni_LpacJni_es10cDisableProfile(JNIEnv *env, jobject thiz, jlong handle,
jstring iccid, jboolean refresh) {
Java_net_typeblog_lpac_1jni_LpacJni_es10cDisableProfile(
JNIEnv *env,
__attribute__((unused)) jobject thiz,
jlong handle,
jstring iccid,
jboolean refresh
) {
struct euicc_ctx *ctx = (struct euicc_ctx *) handle;
const char *_iccid = NULL;
int ret;
@ -181,8 +211,13 @@ Java_net_typeblog_lpac_1jni_LpacJni_es10cDisableProfile(JNIEnv *env, jobject thi
}
JNIEXPORT jint JNICALL
Java_net_typeblog_lpac_1jni_LpacJni_es10cSetNickname(JNIEnv *env, jobject thiz, jlong handle,
jstring iccid, jbyteArray nick) {
Java_net_typeblog_lpac_1jni_LpacJni_es10cSetNickname(
JNIEnv *env,
__attribute__((unused)) jobject thiz,
jlong handle,
jstring iccid,
jbyteArray nick
) {
struct euicc_ctx *ctx = (struct euicc_ctx *) handle;
const char *_iccid = NULL;
jbyte *_nick = NULL;
@ -197,8 +232,12 @@ Java_net_typeblog_lpac_1jni_LpacJni_es10cSetNickname(JNIEnv *env, jobject thiz,
}
JNIEXPORT jint JNICALL
Java_net_typeblog_lpac_1jni_LpacJni_es10cDeleteProfile(JNIEnv *env, jobject thiz, jlong handle,
jstring iccid) {
Java_net_typeblog_lpac_1jni_LpacJni_es10cDeleteProfile(
JNIEnv *env,
__attribute__((unused)) jobject thiz,
jlong handle,
jstring iccid
) {
struct euicc_ctx *ctx = (struct euicc_ctx *) handle;
const char *_iccid = NULL;
int ret;
@ -210,7 +249,11 @@ Java_net_typeblog_lpac_1jni_LpacJni_es10cDeleteProfile(JNIEnv *env, jobject thiz
}
JNIEXPORT jobject JNICALL
Java_net_typeblog_lpac_1jni_LpacJni_es10cexGetEuiccInfo2(JNIEnv *env, jobject thiz, jlong handle) {
Java_net_typeblog_lpac_1jni_LpacJni_es10cexGetEuiccInfo2(
JNIEnv *env,
__attribute__((unused)) jobject thiz,
jlong handle
) {
struct euicc_ctx *ctx = (struct euicc_ctx *) handle;
struct es10c_ex_euiccinfo2 *info = malloc(sizeof(struct es10c_ex_euiccinfo2));
jobject ret = NULL;
@ -253,7 +296,11 @@ Java_net_typeblog_lpac_1jni_LpacJni_es10cexGetEuiccInfo2(JNIEnv *env, jobject th
}
JNIEXPORT jint JNICALL
Java_net_typeblog_lpac_1jni_LpacJni_es10cEuiccMemoryReset(JNIEnv *env, jobject thiz, jlong handle) {
Java_net_typeblog_lpac_1jni_LpacJni_es10cEuiccMemoryReset(
__attribute__((unused)) JNIEnv *env,
__attribute__((unused)) jobject thiz,
jlong handle
) {
struct euicc_ctx *ctx = (struct euicc_ctx *) handle;
return es10c_euicc_memory_reset(ctx);
}

View file

@ -52,8 +52,12 @@ Java_net_typeblog_lpac_1jni_LpacJni_es10bListNotification(
}
JNIEXPORT jint JNICALL
Java_net_typeblog_lpac_1jni_LpacJni_handleNotification(JNIEnv *env, jobject thiz, jlong handle,
jlong seq_number) {
Java_net_typeblog_lpac_1jni_LpacJni_handleNotification(
__attribute__((unused)) JNIEnv *env,
__attribute__((unused)) jobject thiz,
jlong handle,
jlong seq_number
) {
struct euicc_ctx *ctx = (struct euicc_ctx *) handle;
struct es10b_pending_notification notification;
int res;
@ -76,8 +80,12 @@ Java_net_typeblog_lpac_1jni_LpacJni_handleNotification(JNIEnv *env, jobject thiz
}
JNIEXPORT jint JNICALL
Java_net_typeblog_lpac_1jni_LpacJni_es10bDeleteNotification(JNIEnv *env, jobject thiz, jlong handle,
jlong seq_number) {
Java_net_typeblog_lpac_1jni_LpacJni_es10bDeleteNotification(
__attribute__((unused)) JNIEnv *env,
__attribute__((unused)) jobject thiz,
jlong handle,
jlong seq_number
) {
struct euicc_ctx *ctx = (struct euicc_ctx *) handle;
return es10b_remove_notification_from_list(ctx, (unsigned long) seq_number);
}