yy1717
2020-08-07 e43f00fbe051dc8f9dfa5a19143c38613b64ecad
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#include <jni.h>
#include <string>
#include <pthread.h>
#include <unistd.h>
#include <cstdlib>
#include "common/serial_port.h"
#include "jni_log.h"
#include "common/net.h"
#include "common/apptimer.h"
#include "rtk_platform/parse_net.h"
#include "native-lib.h"
#include "test_common/Geometry.h"
#include "rtk_platform/platform.h"
#include "rtk_module/rtk.h"
#include "mcu/mcu_if.h"
#include "driver_test.h"
#include "master/comm_if.h"
#include "rtk_module/virtual_rtk.h"
 
#define DEBUG(fmt, args...)     LOGD("<native-lib> <%s>: " fmt, __func__, ##args)
 
static JavaVM *sg_jvm = NULL;
static jobject sg_obj = NULL;
 
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.16.100";
const int VIRTUAL_RTK_PORT = 9001;
 
static pthread_mutex_t tts_mutex = PTHREAD_MUTEX_INITIALIZER;
 
static int ttsSeq = 1;
 
static void SendBootIndicate(union sigval sig);
 
int DESEncrypt(const uint8_t *key, int key_length,
        const uint8_t *plaintext, int plaintext_length,
        uint8_t **ciphertext)
{
    JNIEnv *env;
    bool ready_in_java_env = false;
    int ciphertext_length = 0;
    *ciphertext = NULL;
 
    LOGD("JNI_DESEncrypt");
 
    if (sg_jvm->GetEnv((void **)&env, JNI_VERSION_1_6) != JNI_OK) {
        // Attach主线程
        if (sg_jvm->AttachCurrentThread(&env, NULL) != JNI_OK) {
            LOGE("%s: AttachCurrentThread() failed", __FUNCTION__);
            return 0;
        }
    } else {
        ready_in_java_env = true;
    }
 
    jclass cls = env->GetObjectClass(sg_obj);
    jmethodID fun = env->GetMethodID(cls, "javaDESEncrypt", "([B[B)[B");
 
    jbyteArray jni_key = env->NewByteArray(key_length);
    jbyteArray jni_plaintext = env->NewByteArray(plaintext_length);
 
    env->SetByteArrayRegion(jni_key, 0, key_length, (jbyte *) key);
    env->SetByteArrayRegion(jni_plaintext, 0, plaintext_length, (jbyte *) plaintext);
 
    jbyteArray jni_ciphertext = (jbyteArray) env->CallObjectMethod(sg_obj, fun, jni_plaintext, jni_key);
    if (jni_ciphertext != NULL) {
        ciphertext_length = env->GetArrayLength(jni_ciphertext);
        uint8_t *buffer = (uint8_t *)malloc(ciphertext_length);
        env->GetByteArrayRegion(jni_ciphertext, 0, ciphertext_length, (jbyte *) buffer);
        *ciphertext = buffer;
    }
 
    env->DeleteLocalRef(jni_key);
    env->DeleteLocalRef(jni_plaintext);
    env->DeleteLocalRef(cls);
 
    if (!ready_in_java_env) {
        //Detach主线程
        if (sg_jvm->DetachCurrentThread() != JNI_OK) {
            LOGE("%s: DetachCurrentThread() failed", __FUNCTION__);
        }
    }
    return ciphertext_length;
}
 
void TextOsd(int type, const char *text)
{
/*    JNIEnv *env;
    bool ready_in_java_env = false;
 
    if (sg_jvm->GetEnv((void **)&env, JNI_VERSION_1_6) != JNI_OK) {
        // Attach主线程
        if (sg_jvm->AttachCurrentThread(&env, NULL) != JNI_OK) {
            LOGE("%s: AttachCurrentThread() failed", __FUNCTION__);
            return;
        }
    } else {
        ready_in_java_env = true;
    }
 
    jclass cls = env->GetObjectClass(sg_obj);
    jmethodID fun = env->GetMethodID(cls, "TextOsd", "(ILjava/lang/String;)V");
 
    std::string cstext = text;
    env->CallVoidMethod(sg_obj, fun, type, env->NewStringUTF(cstext.c_str()));
    env->DeleteLocalRef(cls);
 
    if (!ready_in_java_env) {
        //Detach主线程
        if (sg_jvm->DetachCurrentThread() != JNI_OK) {
            LOGE("%s: DetachCurrentThread() failed", __FUNCTION__);
        }
    }*/
}
 
void DrawScreen(const Polygon *map, const Polygon *car)
{
    JNIEnv *env;
    bool ready_in_java_env = false;
 
    if (sg_jvm->GetEnv((void **)&env, JNI_VERSION_1_6) != JNI_OK) {
        // Attach主线程
        if (sg_jvm->AttachCurrentThread(&env, NULL) != JNI_OK) {
            LOGE("%s: AttachCurrentThread() failed", __FUNCTION__);
            return;
        }
    } else {
        ready_in_java_env = true;
    }
 
    jclass cls = env->GetObjectClass(sg_obj);
    jmethodID fun = env->GetMethodID(cls, "DrawMap", "([[D[[D)V");
 
    jclass doubleArrCls = env->FindClass("[D");
    jobjectArray retmap = env->NewObjectArray(map->num, doubleArrCls, NULL);
    jobjectArray retcar = env->NewObjectArray(car->num, doubleArrCls, NULL);
 
    for (int i = 0; i < map->num; ++i) {
        jdoubleArray doubleArr = env->NewDoubleArray(2);
        jdouble tmp[2] = {map->point[i].X, 0 - map->point[i].Y};
 
        env->SetDoubleArrayRegion(doubleArr, 0, 2, tmp);
        env->SetObjectArrayElement(retmap, i, doubleArr);
        env->DeleteLocalRef(doubleArr);
    }
 
    for (int i = 0; i < car->num; ++i) {
        jdoubleArray doubleArr = env->NewDoubleArray(2);
        jdouble tmp[2] = {car->point[i].X, 0 - car->point[i].Y};
 
        env->SetDoubleArrayRegion(doubleArr, 0, 2, tmp);
        env->SetObjectArrayElement(retcar, i, doubleArr);
        env->DeleteLocalRef(doubleArr);
    }
 
    env->CallVoidMethod(sg_obj, fun, retmap, retcar);
    env->DeleteLocalRef(retmap);
    env->DeleteLocalRef(retcar);
    env->DeleteLocalRef(cls);
 
    if (!ready_in_java_env) {
        //Detach主线程
        if (sg_jvm->DetachCurrentThread() != JNI_OK) {
            LOGE("%s: DetachCurrentThread() failed", __FUNCTION__);
        }
    }
}
 
void SendMsgToMainProc(int cmd, const char *value)
{
    JNIEnv *env;
    bool ready_in_java_env = false;
 
    if (sg_jvm->GetEnv((void **)&env, JNI_VERSION_1_6) != JNI_OK) {
        // Attach主线程
        if (sg_jvm->AttachCurrentThread(&env, NULL) != JNI_OK) {
            LOGE("%s: AttachCurrentThread() failed", __FUNCTION__);
            return;
        }
    } else {
        ready_in_java_env = true;
    }
 
    jclass cls = env->GetObjectClass(sg_obj);
    jmethodID fun = env->GetMethodID(cls, "SendMsgToMainProc", "(ILjava/lang/String;)V");
 
    env->CallVoidMethod(sg_obj, fun, cmd, value != NULL ? env->NewStringUTF(value) : NULL);
 
    env->DeleteLocalRef(cls);
 
    if (!ready_in_java_env) {
        //Detach主线程
        if (sg_jvm->DetachCurrentThread() != JNI_OK) {
            LOGE("%s: DetachCurrentThread() failed", __FUNCTION__);
        }
    }
}
 
static int GetTtsSeq(void)
{
    int seq = 0;
 
    pthread_mutex_lock(&tts_mutex);
    seq = ttsSeq++;
    pthread_mutex_unlock(&tts_mutex);
 
    return seq;
}
 
int PlayTTS(const char *string)
{
    DEBUG("PlayTTS: %s", string);
 
    JNIEnv *env;
    bool ready_in_java_env = false;
 
    if (sg_jvm->GetEnv((void **)&env, JNI_VERSION_1_6) != JNI_OK) {
        // Attach主线程
        if (sg_jvm->AttachCurrentThread(&env, NULL) != JNI_OK) {
            LOGE("%s: AttachCurrentThread() failed", __FUNCTION__);
            return -1;
        }
    } else {
        ready_in_java_env = true;
    }
 
    jclass cls = env->GetObjectClass(sg_obj);
    jmethodID fun = env->GetMethodID(cls, "TextSpeak", "(Ljava/lang/String;I)V");
 
    int id = GetTtsSeq();
 
    env->CallVoidMethod(sg_obj, fun, env->NewStringUTF(string), id);
 
    env->DeleteLocalRef(cls);
 
    if (!ready_in_java_env) {
        //Detach主线程
        if (sg_jvm->DetachCurrentThread() != JNI_OK) {
            LOGE("%s: DetachCurrentThread() failed", __FUNCTION__);
        }
    }
 
    return id;
}
 
extern "C"
JNIEXPORT void JNICALL
Java_com_anyun_exam_lib_RemoteService_startNative(JNIEnv *env, jobject thiz) {
    // TODO: implement startNative()
    // 保存全局JVM以便在子线程中使用
    DEBUG("启动Native");
    env->GetJavaVM(&sg_jvm);
    // 不能直接赋值(g_obj = ojb)
    sg_obj = env->NewGlobalRef(thiz);
 
    srand(time(NULL));
 
    AppTimer_Init();
    ConfigMCU();
    DriverTestInit();
    ConfigRTKModule();
    MA_Init();
    InitPlatform(phone, RTK_PLATFORM_IP, RTK_PLATFORM_PORT);
    AppTimer_add(SendBootIndicate, D_SEC(1));
 
    InitVirtualDevice(VIRTUAL_RTK_IP, VIRTUAL_RTK_PORT);
 
    pthread_mutex_init(&tts_mutex, NULL);
}
 
static void SendBootIndicate(union sigval sig) {
    static int n = 0;
    AppTimer_delete(SendBootIndicate);
    MA_NdkStart();
 
    n++;
 
    if (n < 3) {
        AppTimer_add(SendBootIndicate, D_SEC(1));
    }
}
 
extern "C"
JNIEXPORT void JNICALL
Java_com_anyun_exam_lib_RemoteService_MainProcMsgEntry(JNIEnv *env, jobject thiz, jint cmd,
                                                       jstring value) {
    // TODO: implement MainProcMsgEntry()
    if (value != NULL) {
        const char *str = env->GetStringUTFChars(value, 0);
        MA_MainProcMsgEntry(cmd, str);
        env->ReleaseStringUTFChars(value, str);
    } else {
        MA_MainProcMsgEntry(cmd, NULL);
    }
}
 
extern "C"
JNIEXPORT void JNICALL
Java_com_anyun_exam_lib_RemoteService_MainProcBinMsgEntry(JNIEnv *env, jobject thiz, jint cmd,
                                                          jbyteArray data, jint length) {
    // TODO: implement MainProcBinMsgEntry()
    jbyte *c_dat = env->GetByteArrayElements(data, NULL);
    int len = env->GetArrayLength(data);
 
    MA_MainProcBinMsgEntry(cmd, (uint8_t *)c_dat, len);
 
    env->ReleaseByteArrayElements(data, c_dat, NULL);
}
 
extern "C"
JNIEXPORT void JNICALL
Java_com_anyun_exam_lib_RemoteService_TextSpeakEnd(JNIEnv *env, jobject thiz, jint id) {
    // TODO: implement TextSpeakEnd()
    PlatformStatusChanged(PLAY_TTS_DONE_EVT, (uint8_t *)&id, sizeof(id));
}