1 /*
2 * Copyright (C) 2012 The Android Open Source Project
3 * Copyright 2018-2020 NXP.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 #include "UwbJniUtil.h"
19
20 #include <nativehelper/JNIHelp.h>
21 #include <nativehelper/ScopedLocalRef.h>
22
23 #include "JniLog.h"
24 #include "UwbJniInternal.h"
25
26 /*******************************************************************************
27 **
28 ** Function: JNI_OnLoad
29 **
30 ** Description: Register all JNI functions with Java Virtual Machine.
31 ** jvm: Java Virtual Machine.
32 ** reserved: Not used.
33 **
34 ** Returns: JNI version.
35 **
36 *******************************************************************************/
JNI_OnLoad(JavaVM * jvm,void *)37 jint JNI_OnLoad(JavaVM *jvm, void *) {
38 JNI_TRACE_I("%s: enter", __func__);
39 JNIEnv *env = NULL;
40
41 JNI_TRACE_I("UWB Service: loading uci JNI");
42
43 // Check JNI version
44 if (jvm->GetEnv((void **)&env, JNI_VERSION_1_6))
45 return JNI_ERR;
46
47 if (android::register_com_android_uwb_dhimpl_UwbNativeManager(env) == -1)
48 return JNI_ERR;
49 /*if (android::register_com_android_uwb_dhimpl_UwbRfTestNativeManager(env) ==
50 -1)
51 return JNI_ERR;*/
52
53 JNI_TRACE_I("%s: exit", __func__);
54 return JNI_VERSION_1_6;
55 }
56
57 /*******************************************************************************
58 **
59 ** Function: uwb_jni_cache_jclass
60 **
61 ** Description: This API invoked during JNI initialization to register
62 ** Required class and corresponding Global refference will be
63 ** used during sending Ranging ntf to upper layer.
64 **
65 ** Returns: Status code.
66 **
67 *******************************************************************************/
uwb_jni_cache_jclass(JNIEnv * env,const char * className,jclass * cachedJclass)68 int uwb_jni_cache_jclass(JNIEnv *env, const char *className,
69 jclass *cachedJclass) {
70 jclass cls = env->FindClass(className);
71 if (cls == NULL) {
72 JNI_TRACE_E("%s: find class error", __func__);
73 return -1;
74 }
75
76 *cachedJclass = static_cast<jclass>(env->NewGlobalRef(cls));
77 if (*cachedJclass == NULL) {
78 JNI_TRACE_E("%s: global ref error", __func__);
79 return -1;
80 }
81 return 0;
82 }