Compare commits

..

1 commit

Author SHA1 Message Date
98fe4c353b
refactor: lpac-jni bridge 2025-03-11 03:57:24 +08:00
4 changed files with 41 additions and 112 deletions

View file

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

View file

@ -174,25 +174,18 @@ Java_net_typeblog_lpac_1jni_LpacJni_downloadProfile(
} }
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
Java_net_typeblog_lpac_1jni_LpacJni_cancelSessions( Java_net_typeblog_lpac_1jni_LpacJni_cancelSessions(JNIEnv *env, jobject thiz, jlong handle) {
__attribute__((unused)) JNIEnv *env,
__attribute__((unused)) jobject thiz,
jlong handle
) {
struct euicc_ctx *ctx = (struct euicc_ctx *) handle; struct euicc_ctx *ctx = (struct euicc_ctx *) handle;
es9p_cancel_session(ctx); es9p_cancel_session(ctx);
es10b_cancel_session(ctx, ES10B_CANCEL_SESSION_REASON_UNDEFINED); es10b_cancel_session(ctx, ES10B_CANCEL_SESSION_REASON_UNDEFINED);
euicc_http_cleanup(ctx); euicc_http_cleanup(ctx);
} }
#define ERRCODE_ENUM_TO_STRING(VARIANT) case VARIANT: return toJString(env, #VARIANT) #define QUOTE(S) #S
#define ERRCODE_ENUM_TO_STRING(VARIANT) case VARIANT: return toJString(env, QUOTE(VARIANT))
JNIEXPORT jstring JNICALL JNIEXPORT jstring JNICALL
Java_net_typeblog_lpac_1jni_LpacJni_downloadErrCodeToString( Java_net_typeblog_lpac_1jni_LpacJni_downloadErrCodeToString(JNIEnv *env, jobject thiz, jint code) {
JNIEnv *env,
__attribute__((unused)) jobject thiz,
jint code
) {
switch (code) { switch (code) {
ERRCODE_ENUM_TO_STRING(ES10B_ERROR_REASON_INCORRECT_INPUT_VALUES); ERRCODE_ENUM_TO_STRING(ES10B_ERROR_REASON_INCORRECT_INPUT_VALUES);
ERRCODE_ENUM_TO_STRING(ES10B_ERROR_REASON_INVALID_SIGNATURE); 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" #define LOCAL_PROFILE_INFO_CLASS "net/typeblog/lpac_jni/LocalProfileInfo"
jint JNI_OnLoad(JavaVM *vm, __attribute__((unused)) void *reserved) { jint JNI_OnLoad(JavaVM *vm, void *reserved) {
jvm = vm; jvm = vm;
LPAC_JNI_SETUP_ENV; LPAC_JNI_SETUP_ENV;
@ -30,7 +30,7 @@ jint JNI_OnLoad(JavaVM *vm, __attribute__((unused)) void *reserved) {
JNIEXPORT jlong JNICALL JNIEXPORT jlong JNICALL
Java_net_typeblog_lpac_1jni_LpacJni_createContext( Java_net_typeblog_lpac_1jni_LpacJni_createContext(
JNIEnv *env, JNIEnv *env,
__attribute__((unused)) jobject thiz, jobject thiz,
jbyteArray isdr_aid, jbyteArray isdr_aid,
jobject apdu_interface, jobject apdu_interface,
jobject http_interface jobject http_interface
@ -61,11 +61,7 @@ Java_net_typeblog_lpac_1jni_LpacJni_createContext(
} }
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
Java_net_typeblog_lpac_1jni_LpacJni_destroyContext( Java_net_typeblog_lpac_1jni_LpacJni_destroyContext(JNIEnv *env, jobject thiz, jlong handle) {
JNIEnv *env,
__attribute__((unused)) jobject thiz,
jlong handle
) {
struct euicc_ctx *ctx = (struct euicc_ctx *) handle; struct euicc_ctx *ctx = (struct euicc_ctx *) handle;
struct lpac_jni_ctx *jni_ctx = LPAC_JNI_CTX(ctx); struct lpac_jni_ctx *jni_ctx = LPAC_JNI_CTX(ctx);
@ -77,42 +73,26 @@ Java_net_typeblog_lpac_1jni_LpacJni_destroyContext(
} }
JNIEXPORT jint JNICALL JNIEXPORT jint JNICALL
Java_net_typeblog_lpac_1jni_LpacJni_euiccInit( Java_net_typeblog_lpac_1jni_LpacJni_euiccInit(JNIEnv *env, jobject thiz, jlong handle) {
__attribute__((unused)) JNIEnv *env,
__attribute__((unused)) jobject thiz,
jlong handle
) {
struct euicc_ctx *ctx = (struct euicc_ctx *) handle; struct euicc_ctx *ctx = (struct euicc_ctx *) handle;
return euicc_init(ctx); return euicc_init(ctx);
} }
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
Java_net_typeblog_lpac_1jni_LpacJni_euiccFini( Java_net_typeblog_lpac_1jni_LpacJni_euiccFini(JNIEnv *env, jobject thiz, jlong handle) {
__attribute__((unused)) JNIEnv *env,
__attribute__((unused)) jobject thiz,
jlong handle
) {
struct euicc_ctx *ctx = (struct euicc_ctx *) handle; struct euicc_ctx *ctx = (struct euicc_ctx *) handle;
euicc_fini(ctx); euicc_fini(ctx);
} }
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
Java_net_typeblog_lpac_1jni_LpacJni_euiccSetMss( Java_net_typeblog_lpac_1jni_LpacJni_euiccSetMss(JNIEnv *env, jobject thiz, jlong handle,
__attribute__((unused)) JNIEnv *env, jbyte mss) {
__attribute__((unused)) jobject thiz,
jlong handle,
jbyte mss
) {
struct euicc_ctx *ctx = (struct euicc_ctx *) handle; struct euicc_ctx *ctx = (struct euicc_ctx *) handle;
ctx->es10x_mss = (uint8_t) mss; ctx->es10x_mss = (uint8_t) mss;
} }
JNIEXPORT jstring JNICALL JNIEXPORT jstring JNICALL
Java_net_typeblog_lpac_1jni_LpacJni_es10cGetEid( Java_net_typeblog_lpac_1jni_LpacJni_es10cGetEid(JNIEnv *env, jobject thiz, jlong handle) {
__attribute__((unused)) JNIEnv *env,
__attribute__((unused)) jobject thiz,
jlong handle
) {
struct euicc_ctx *ctx = (struct euicc_ctx *) handle; struct euicc_ctx *ctx = (struct euicc_ctx *) handle;
char *buf = NULL; char *buf = NULL;
@ -175,13 +155,8 @@ Java_net_typeblog_lpac_1jni_LpacJni_es10cGetProfilesInfo(
} }
JNIEXPORT jint JNICALL JNIEXPORT jint JNICALL
Java_net_typeblog_lpac_1jni_LpacJni_es10cEnableProfile( Java_net_typeblog_lpac_1jni_LpacJni_es10cEnableProfile(JNIEnv *env, jobject thiz, jlong handle,
JNIEnv *env, jstring iccid, jboolean refresh) {
__attribute__((unused)) jobject thiz,
jlong handle,
jstring iccid,
jboolean refresh
) {
struct euicc_ctx *ctx = (struct euicc_ctx *) handle; struct euicc_ctx *ctx = (struct euicc_ctx *) handle;
const char *_iccid = NULL; const char *_iccid = NULL;
int ret; int ret;
@ -193,13 +168,8 @@ Java_net_typeblog_lpac_1jni_LpacJni_es10cEnableProfile(
} }
JNIEXPORT jint JNICALL JNIEXPORT jint JNICALL
Java_net_typeblog_lpac_1jni_LpacJni_es10cDisableProfile( Java_net_typeblog_lpac_1jni_LpacJni_es10cDisableProfile(JNIEnv *env, jobject thiz, jlong handle,
JNIEnv *env, jstring iccid, jboolean refresh) {
__attribute__((unused)) jobject thiz,
jlong handle,
jstring iccid,
jboolean refresh
) {
struct euicc_ctx *ctx = (struct euicc_ctx *) handle; struct euicc_ctx *ctx = (struct euicc_ctx *) handle;
const char *_iccid = NULL; const char *_iccid = NULL;
int ret; int ret;
@ -211,13 +181,8 @@ Java_net_typeblog_lpac_1jni_LpacJni_es10cDisableProfile(
} }
JNIEXPORT jint JNICALL JNIEXPORT jint JNICALL
Java_net_typeblog_lpac_1jni_LpacJni_es10cSetNickname( Java_net_typeblog_lpac_1jni_LpacJni_es10cSetNickname(JNIEnv *env, jobject thiz, jlong handle,
JNIEnv *env, jstring iccid, jbyteArray nick) {
__attribute__((unused)) jobject thiz,
jlong handle,
jstring iccid,
jbyteArray nick
) {
struct euicc_ctx *ctx = (struct euicc_ctx *) handle; struct euicc_ctx *ctx = (struct euicc_ctx *) handle;
const char *_iccid = NULL; const char *_iccid = NULL;
jbyte *_nick = NULL; jbyte *_nick = NULL;
@ -232,12 +197,8 @@ Java_net_typeblog_lpac_1jni_LpacJni_es10cSetNickname(
} }
JNIEXPORT jint JNICALL JNIEXPORT jint JNICALL
Java_net_typeblog_lpac_1jni_LpacJni_es10cDeleteProfile( Java_net_typeblog_lpac_1jni_LpacJni_es10cDeleteProfile(JNIEnv *env, jobject thiz, jlong handle,
JNIEnv *env, jstring iccid) {
__attribute__((unused)) jobject thiz,
jlong handle,
jstring iccid
) {
struct euicc_ctx *ctx = (struct euicc_ctx *) handle; struct euicc_ctx *ctx = (struct euicc_ctx *) handle;
const char *_iccid = NULL; const char *_iccid = NULL;
int ret; int ret;
@ -249,11 +210,7 @@ Java_net_typeblog_lpac_1jni_LpacJni_es10cDeleteProfile(
} }
JNIEXPORT jobject JNICALL JNIEXPORT jobject JNICALL
Java_net_typeblog_lpac_1jni_LpacJni_es10cexGetEuiccInfo2( Java_net_typeblog_lpac_1jni_LpacJni_es10cexGetEuiccInfo2(JNIEnv *env, jobject thiz, jlong handle) {
JNIEnv *env,
__attribute__((unused)) jobject thiz,
jlong handle
) {
struct euicc_ctx *ctx = (struct euicc_ctx *) handle; struct euicc_ctx *ctx = (struct euicc_ctx *) handle;
struct es10c_ex_euiccinfo2 *info = malloc(sizeof(struct es10c_ex_euiccinfo2)); struct es10c_ex_euiccinfo2 *info = malloc(sizeof(struct es10c_ex_euiccinfo2));
jobject ret = NULL; jobject ret = NULL;
@ -296,11 +253,7 @@ Java_net_typeblog_lpac_1jni_LpacJni_es10cexGetEuiccInfo2(
} }
JNIEXPORT jint JNICALL JNIEXPORT jint JNICALL
Java_net_typeblog_lpac_1jni_LpacJni_es10cEuiccMemoryReset( Java_net_typeblog_lpac_1jni_LpacJni_es10cEuiccMemoryReset(JNIEnv *env, jobject thiz, jlong handle) {
__attribute__((unused)) JNIEnv *env,
__attribute__((unused)) jobject thiz,
jlong handle
) {
struct euicc_ctx *ctx = (struct euicc_ctx *) handle; struct euicc_ctx *ctx = (struct euicc_ctx *) handle;
return es10c_euicc_memory_reset(ctx); return es10c_euicc_memory_reset(ctx);
} }

View file

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