Browse Source

refactor: lpac-jni: Use C macros to generate struct-exposing functions

Peter Cai 1 year ago
parent
commit
394cad2eac

+ 1 - 1
libs/lpac-jni/src/main/java/net/typeblog/lpac_jni/LpacJni.kt

@@ -35,7 +35,7 @@ internal object LpacJni {
 
     // C <-> Java struct / linked list handling
     // Notifications
-    external fun notificationNext(curr: Long): Long
+    external fun notificationsNext(curr: Long): Long
     external fun notificationGetSeq(curr: Long): Long
     external fun notificationGetOperationString(curr: Long): String
     external fun notificationGetAddress(curr: Long): String

+ 1 - 1
libs/lpac-jni/src/main/java/net/typeblog/lpac_jni/impl/LocalProfileAssistantImpl.kt

@@ -56,7 +56,7 @@ class LocalProfileAssistantImpl(
                     LpacJni.notificationGetAddress(curr),
                     LpacJni.notificationGetIccid(curr),
                 ))
-                curr = LpacJni.notificationNext(curr)
+                curr = LpacJni.notificationsNext(curr)
             }
             LpacJni.notificationsFree(head)
             return ret.sortedBy { it.seqNumber }.reversed()

+ 28 - 1
libs/lpac-jni/src/main/jni/lpac-jni/lpac-jni.h

@@ -50,4 +50,31 @@ struct lpac_jni_ctx {
 extern JavaVM *jvm;
 extern jclass string_class;
 
-jstring toJString(JNIEnv *env, const char *pat);
+jstring toJString(JNIEnv *env, const char *pat);
+
+#define LPAC_JNI_STRUCT_GETTER_LINKED_LIST_NEXT(st, st_jname) \
+        JNIEXPORT jlong JNICALL Java_net_typeblog_lpac_1jni_LpacJni_##st_jname##Next(JNIEnv *env, jobject thiz, jlong raw) { \
+            st *p = (st *) raw;                       \
+            if (p == NULL) return 0;                  \
+            return (jlong) p->next;                   \
+        }
+
+#define LPAC_JNI_STRUCT_LINKED_LIST_FREE(st, st_jname, free_func) \
+        JNIEXPORT void JNICALL Java_net_typeblog_lpac_1jni_LpacJni_##st_jname##Free(JNIEnv *env, jobject thiz, jlong raw) { \
+            st *p = (st *) raw;                       \
+            if (p == NULL) return;                    \
+            free_func(p);                             \
+        }
+
+#define LPAC_JNI_STRUCT_GETTER_LONG(st, name, jname) \
+        JNIEXPORT jlong JNICALL Java_net_typeblog_lpac_1jni_LpacJni_notificationGet##jname(JNIEnv *env, jobject thiz, jlong raw) { \
+            st *p = (st *) raw;                       \
+            if (p == NULL) return 0;                  \
+            return (jlong) p->name;                   \
+        }
+
+#define LPAC_JNI_STRUCT_GETTER_STRING(st, name, jname) \
+        JNIEXPORT jstring JNICALL Java_net_typeblog_lpac_1jni_LpacJni_notificationGet##jname(JNIEnv *env, jobject thiz, jlong raw) { \
+            st *p = (st *) raw;                       \
+            return toJString(env, p->name);           \
+        }

+ 5 - 33
libs/lpac-jni/src/main/jni/lpac-jni/lpac-notifications.c

@@ -46,21 +46,6 @@ Java_net_typeblog_lpac_1jni_LpacJni_es10bDeleteNotification(JNIEnv *env, jobject
     return es10b_remove_notification_from_list(ctx, (unsigned long) seq_number);
 }
 
-JNIEXPORT jlong JNICALL
-Java_net_typeblog_lpac_1jni_LpacJni_notificationNext(JNIEnv *env, jobject thiz, jlong curr) {
-    struct es10b_notification_metadata_list *info = (struct es10b_notification_metadata_list *) curr;
-    if (info == NULL) {
-        return 0;
-    }
-    return (jlong) info->next;
-}
-
-JNIEXPORT jlong JNICALL
-Java_net_typeblog_lpac_1jni_LpacJni_notificationGetSeq(JNIEnv *env, jobject thiz, jlong curr) {
-    struct es10b_notification_metadata_list *info = (struct es10b_notification_metadata_list *) curr;
-    return info->seqNumber;
-}
-
 JNIEXPORT jstring JNICALL
 Java_net_typeblog_lpac_1jni_LpacJni_notificationGetOperationString(JNIEnv *env, jobject thiz,
                                                                    jlong curr) {
@@ -87,21 +72,8 @@ Java_net_typeblog_lpac_1jni_LpacJni_notificationGetOperationString(JNIEnv *env,
     return toJString(env, profileManagementOperationStr);
 }
 
-JNIEXPORT jstring JNICALL
-Java_net_typeblog_lpac_1jni_LpacJni_notificationGetAddress(JNIEnv *env, jobject thiz, jlong curr) {
-    struct es10b_notification_metadata_list *info = (struct es10b_notification_metadata_list *) curr;
-    return toJString(env, info->notificationAddress);
-}
-
-JNIEXPORT jstring JNICALL
-Java_net_typeblog_lpac_1jni_LpacJni_notificationGetIccid(JNIEnv *env, jobject thiz, jlong curr) {
-    struct es10b_notification_metadata_list *info = (struct es10b_notification_metadata_list *) curr;
-    return toJString(env, info->iccid);
-}
-
-JNIEXPORT void JNICALL
-Java_net_typeblog_lpac_1jni_LpacJni_notificationsFree(JNIEnv *env, jobject thiz, jlong head) {
-    struct es10b_notification_metadata_list *info = (struct es10b_notification_metadata_list *) head;
-    if (info == NULL) return;
-    es10b_notification_metadata_list_free_all(info);
-}
+LPAC_JNI_STRUCT_GETTER_LINKED_LIST_NEXT(struct es10b_notification_metadata_list, notifications)
+LPAC_JNI_STRUCT_LINKED_LIST_FREE(struct es10b_notification_metadata_list, notifications, es10b_notification_metadata_list_free_all)
+LPAC_JNI_STRUCT_GETTER_LONG(struct es10b_notification_metadata_list, seqNumber, Seq)
+LPAC_JNI_STRUCT_GETTER_STRING(struct es10b_notification_metadata_list, notificationAddress, Address)
+LPAC_JNI_STRUCT_GETTER_STRING(struct es10b_notification_metadata_list, iccid, Iccid)