From f7a18ec4494b9c5c9ef3fd440bbf68ffc6425e18 Mon Sep 17 00:00:00 2001
From: yy1717 <fctom1215@outlook.com>
Date: 星期四, 08 十二月 2022 15:40:56 +0800
Subject: [PATCH] 智慧驾培首次提交

---
 lib/src/main/cpp/native-lib.cpp |  125 +++++++++++++++++++++++++++--------------
 1 files changed, 83 insertions(+), 42 deletions(-)

diff --git a/lib/src/main/cpp/native-lib.cpp b/lib/src/main/cpp/native-lib.cpp
index 4d2fcdb..c3e60c6 100644
--- a/lib/src/main/cpp/native-lib.cpp
+++ b/lib/src/main/cpp/native-lib.cpp
@@ -1,6 +1,6 @@
 #include <jni.h>
 #include <string>
-#include <pthread.h>
+#include <mutex>
 #include <unistd.h>
 #include <cstdlib>
 #include <map>
@@ -18,22 +18,26 @@
 #include "master/comm_if.h"
 #include "rtk_module/virtual_rtk.h"
 #include "defs.h"
+#include "mcu/ada.h"
+#include "mcu/ahp.h"
 
 #define DEBUG(fmt, args...)     LOGD("<native-lib> <%s>: " fmt, __func__, ##args)
 
+#define LIBENC_ARRAY_ELEMS(a)  (sizeof(a) / sizeof(a[0]))
+
 static JavaVM *sg_jvm = NULL;
 static jobject sg_obj = NULL;
+static JNIEnv *jenv;
+
 
 const char *RTK_PLATFORM_IP = "47.93.80.84";
 const int RTK_PLATFORM_PORT = 12125;
 const uint8_t phone[] = {0x20,0x19,0x10,0x15,0x00,0x00,0x00,0x01};
 
-const char *VIRTUAL_RTK_IP = "192.168.43.76";
+const char *VIRTUAL_RTK_IP = "192.168.16.212";
 const int VIRTUAL_RTK_PORT = 9002;
 
-static pthread_mutex_t tts_mutex = PTHREAD_MUTEX_INITIALIZER;
-
-static int ttsSeq = 1;
+static std::mutex tts_mutex;
 
 static std::map<int, void (*)(int)> TTSCallBack;
 
@@ -206,12 +210,10 @@
 
 static int GetTtsSeq(void)
 {
-    int seq = 0;
+    static int seq = 0;
 
-    pthread_mutex_lock(&tts_mutex);
-    seq = ttsSeq++;
-    pthread_mutex_unlock(&tts_mutex);
-
+    lock_guard<std::mutex> lock(tts_mutex);
+    seq++;
     return seq;
 }
 
@@ -414,35 +416,30 @@
     }
 }
 
-extern "C"
-JNIEXPORT void JNICALL
-Java_com_anyun_exam_lib_RemoteService_startNative(JNIEnv *env, jobject thiz, jboolean ayDevice) {
+static void startNative(JNIEnv *env, jobject thiz, jboolean ayDevice) {
     // TODO: implement startNative()
     // 淇濆瓨鍏ㄥ眬JVM浠ヤ究鍦ㄥ瓙绾跨▼涓娇鐢�
     DEBUG("鍚姩Native");
-    env->GetJavaVM(&sg_jvm);
     // 涓嶈兘鐩存帴璧嬪��(g_obj = ojb)
     sg_obj = env->NewGlobalRef(thiz);
 
     srand(time(NULL));
 
-    AppTimer_Init();
-    ConfigMCU(ayDevice);
+    AppTimer_init();
+    ///////////////ConfigMCU(ayDevice);
+    InitAda();
+    InitAhp();
     DriverTestInit();
-    ConfigRTKModule(ayDevice);
+
     MA_Init();
     InitPlatform(ayDevice, phone, RTK_PLATFORM_IP, RTK_PLATFORM_PORT);
 
     InitVirtualDevice(VIRTUAL_RTK_IP, VIRTUAL_RTK_PORT);
 
-    pthread_mutex_init(&tts_mutex, NULL);
-
-    MA_NdkStart();
+//    MA_NdkStart();
 }
 
-extern "C"
-JNIEXPORT void JNICALL
-Java_com_anyun_exam_lib_RemoteService_MainProcMsgEntry(JNIEnv *env, jobject thiz, jint cmd,
+static void MainProcMsgEntry(JNIEnv *env, jobject thiz, jint cmd,
                                                        jstring value) {
     // TODO: implement MainProcMsgEntry()
     union {
@@ -474,9 +471,7 @@
     }
 }
 
-extern "C"
-JNIEXPORT void JNICALL
-Java_com_anyun_exam_lib_RemoteService_MainProcBinMsgEntry(JNIEnv *env, jobject thiz, jint cmd,
+void MainProcBinMsgEntry(JNIEnv *env, jobject thiz, jint cmd,
                                                           jbyteArray data, jint length) {
     // TODO: implement MainProcBinMsgEntry()
     jbyte *c_dat = env->GetByteArrayElements(data, NULL);
@@ -487,11 +482,8 @@
     env->ReleaseByteArrayElements(data, c_dat, NULL);
 }
 
-extern "C"
-JNIEXPORT void JNICALL
-Java_com_anyun_exam_lib_RemoteService_TextSpeakEnd(JNIEnv *env, jobject thiz, jint id) {
+void TextSpeakEnd(JNIEnv *env, jobject thiz, jint id) {
     // TODO: implement TextSpeakEnd()
-
     auto it = TTSCallBack.find(id);
 
     if (it != TTSCallBack.end()) {
@@ -506,9 +498,7 @@
     }
 }
 
-extern "C"
-JNIEXPORT void JNICALL
-Java_com_anyun_exam_lib_RemoteService_UpgradeMcu(JNIEnv *env, jobject thiz, jstring vercode,
+void UpgradeMcu(JNIEnv *env, jobject thiz, jstring vercode,
                                                  jbyteArray rom) {
     // TODO: implement UpgradeMcu()
     if (vercode != NULL && rom != NULL) {
@@ -523,9 +513,7 @@
     }
 }
 
-extern "C"
-JNIEXPORT void JNICALL
-Java_com_anyun_exam_lib_RemoteService_BluetoothDataComeIn(JNIEnv *env, jobject thiz,
+void BluetoothDataComeIn(JNIEnv *env, jobject thiz,
                                                           jbyteArray data, jint length) {
     // TODO: implement BluetoothDataComeIn()
     jbyte *c_dat = env->GetByteArrayElements(data, NULL);
@@ -535,9 +523,8 @@
     env->ReleaseByteArrayElements(data, c_dat, NULL);
 }
 
-extern "C"
-JNIEXPORT void JNICALL
-Java_com_anyun_exam_lib_RemoteService_BluetoothStatusChange(JNIEnv *env, jobject thiz,
+
+void BluetoothStatusChange(JNIEnv *env, jobject thiz,
                                                             jint status) {
     // TODO: implement BluetoothStatusChange()
     uint8_t  sta = status;
@@ -545,9 +532,7 @@
     PlatformStatusChanged(BLUETOOTH_STATUS_EVT, &sta, 1);
 }
 
-extern "C"
-JNIEXPORT void JNICALL
-Java_com_anyun_exam_lib_RemoteService_BluetoothConnected(JNIEnv *env, jobject thiz, jstring name,
+void BluetoothConnected(JNIEnv *env, jobject thiz, jstring name,
                                                          jstring addr) {
     // TODO: implement BluetoothConnected()
     if (name != NULL && addr != NULL) {
@@ -576,3 +561,59 @@
         PlatformStatusChanged(BLUETOOTH_STATUS_EVT, &sta, 1);
     }
 }
+
+static JNINativeMethod methods[] = {
+        {"startNative", "(Z)V", reinterpret_cast<void *>(startNative)},
+        {"MainProcMsgEntry", "(ILjava/lang/String;)V", reinterpret_cast<void *>(MainProcMsgEntry)},
+        {"MainProcBinMsgEntry", "(I[BI)V", reinterpret_cast<void *>(MainProcBinMsgEntry)},
+        {"UpgradeMcu", "(Ljava/lang/String;[B)V", reinterpret_cast<void *>(UpgradeMcu)},
+        {"TextSpeakEnd", "(I)V", reinterpret_cast<void *>(TextSpeakEnd)},
+        {"BluetoothConnected", "(Ljava/lang/String;Ljava/lang/String;)V", reinterpret_cast<void *>(BluetoothConnected)},
+        {"BluetoothStatusChange", "(I)V", reinterpret_cast<void *>(BluetoothStatusChange)},
+        {"BluetoothDataComeIn", "([BI)V", reinterpret_cast<void *>(BluetoothDataComeIn)}
+};
+
+jint JNI_OnLoad(JavaVM *vm, void *reserved)
+{
+    sg_jvm = vm;
+
+    if (sg_jvm->GetEnv(reinterpret_cast<void **> (&jenv), JNI_VERSION_1_6) != JNI_OK) {
+        DEBUG("Env not got");
+        return JNI_ERR;
+    }
+
+    jclass clz = jenv->FindClass("com/anyun/exam/lib/RemoteService");
+    if (clz == NULL) {
+        DEBUG("鐩爣绫绘湭鎵惧埌");
+        return JNI_ERR;
+    }
+
+    if (jenv->RegisterNatives(clz, methods, LIBENC_ARRAY_ELEMS(methods))) {
+        DEBUG("methods not registered");
+        return JNI_ERR;
+    }
+
+    DEBUG("JNI_OnLoad");
+
+    return JNI_VERSION_1_6;
+}
+
+void JNI_OnUnload(JavaVM* vm, void* reserved)
+{
+    JNIEnv* env;
+
+    if (vm->GetEnv(reinterpret_cast<void **> (&env), JNI_VERSION_1_6) != JNI_OK) {
+        DEBUG("Env not got");
+        return;
+    }
+
+    jclass clz = env->FindClass("com/anyun/exam/lib/RemoteService");
+    if (clz == NULL) {
+        DEBUG("鐩爣绫绘湭鎵惧埌");
+        return;
+    }
+
+    env->UnregisterNatives(clz);
+
+    DEBUG("JNI_OnUnload");
+}

--
Gitblit v1.8.0