1 /*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "NfcJniUtil.h"
18
19 #include <android-base/logging.h>
20 #include <android-base/stringprintf.h>
21 #include <errno.h>
22 #include <log/log.h>
23 #include <nativehelper/JNIHelp.h>
24 #include <nativehelper/ScopedLocalRef.h>
25
26 #include "NativeWlcManager.h"
27 #include "RoutingManager.h"
28
29 using android::base::StringPrintf;
30
31 /*******************************************************************************
32 **
33 ** Function: JNI_OnLoad
34 **
35 ** Description: Register all JNI functions with Java Virtual Machine.
36 ** jvm: Java Virtual Machine.
37 ** reserved: Not used.
38 **
39 ** Returns: JNI version.
40 **
41 *******************************************************************************/
JNI_OnLoad(JavaVM * jvm,void *)42 jint JNI_OnLoad(JavaVM* jvm, void*) {
43 LOG(DEBUG) << StringPrintf("%s: enter", __func__);
44 JNIEnv* e = NULL;
45
46 LOG(INFO) << StringPrintf("NFC Service: loading nci JNI");
47
48 // Check JNI version
49 if (jvm->GetEnv((void**)&e, JNI_VERSION_1_6)) return JNI_ERR;
50
51 if (android::register_com_android_nfc_NativeNfcManager(e) == -1)
52 return JNI_ERR;
53 if (android::register_com_android_nfc_NativeT4tNfcee(e) == -1) return JNI_ERR;
54 if (android::register_com_android_nfc_NativeNfcTag(e) == -1) return JNI_ERR;
55 if (RoutingManager::getInstance().registerJniFunctions(e) == -1)
56 return JNI_ERR;
57 if (NativeWlcManager::getInstance().registerJniFunctions(e) == -1)
58 return JNI_ERR;
59 LOG(DEBUG) << StringPrintf("%s: exit", __func__);
60 return JNI_VERSION_1_6;
61 }
62
63 namespace android {
64
65 /*******************************************************************************
66 **
67 ** Function: nfc_jni_cache_object
68 **
69 ** Description:
70 **
71 ** Returns: Status code.
72 **
73 *******************************************************************************/
nfc_jni_cache_object(JNIEnv * e,const char * className,jobject * cachedObj)74 int nfc_jni_cache_object(JNIEnv* e, const char* className, jobject* cachedObj) {
75 ScopedLocalRef<jclass> cls(e, e->FindClass(className));
76 if (cls.get() == NULL) {
77 LOG(ERROR) << StringPrintf("%s: find class error", __func__);
78 return -1;
79 }
80
81 jmethodID ctor = e->GetMethodID(cls.get(), "<init>", "()V");
82 ScopedLocalRef<jobject> obj(e, e->NewObject(cls.get(), ctor));
83 if (obj.get() == NULL) {
84 LOG(ERROR) << StringPrintf("%s: create object error", __func__);
85 return -1;
86 }
87
88 *cachedObj = e->NewGlobalRef(obj.get());
89 if (*cachedObj == NULL) {
90 LOG(ERROR) << StringPrintf("%s: global ref error", __func__);
91 return -1;
92 }
93 return 0;
94 }
95
96 /*******************************************************************************
97 **
98 ** Function: nfc_jni_get_nfc_socket_handle
99 **
100 ** Description: Get the value of "mHandle" member variable.
101 ** e: JVM environment.
102 ** o: Java object.
103 **
104 ** Returns: Value of mHandle.
105 **
106 *******************************************************************************/
nfc_jni_get_nfc_socket_handle(JNIEnv * e,jobject o)107 int nfc_jni_get_nfc_socket_handle(JNIEnv* e, jobject o) {
108 ScopedLocalRef<jclass> c(e, e->GetObjectClass(o));
109 jfieldID f = e->GetFieldID(c.get(), "mHandle", "I");
110 return e->GetIntField(o, f);
111 }
112
113 /*******************************************************************************
114 **
115 ** Function: nfc_jni_get_nat
116 **
117 ** Description: Get the value of "mNative" member variable.
118 ** e: JVM environment.
119 ** o: Java object.
120 **
121 ** Returns: Pointer to the value of mNative.
122 **
123 *******************************************************************************/
nfc_jni_get_nat(JNIEnv * e,jobject o)124 struct nfc_jni_native_data* nfc_jni_get_nat(JNIEnv* e, jobject o) {
125 ScopedLocalRef<jclass> c(e, e->GetObjectClass(o));
126 jfieldID f = e->GetFieldID(c.get(), "mNative", "J");
127 /* Retrieve native structure address */
128 return (struct nfc_jni_native_data*)e->GetLongField(o, f);
129 }
130
131 /*******************************************************************************
132 **
133 ** Function nfc_jni_cache_object_local
134 **
135 ** Description Allocates a java object and calls it's constructor
136 **
137 ** Returns -1 on failure, 0 on success
138 **
139 *******************************************************************************/
nfc_jni_cache_object_local(JNIEnv * e,const char * className,jobject * cachedObj)140 int nfc_jni_cache_object_local(JNIEnv* e, const char* className,
141 jobject* cachedObj) {
142 ScopedLocalRef<jclass> cls(e, e->FindClass(className));
143 if (cls.get() == NULL) {
144 LOG(ERROR) << StringPrintf("%s: find class error", __func__);
145 return -1;
146 }
147
148 jmethodID ctor = e->GetMethodID(cls.get(), "<init>", "()V");
149 jobject obj = e->NewObject(cls.get(), ctor);
150 if (obj == NULL) {
151 LOG(ERROR) << StringPrintf("%s: create object error", __func__);
152 return -1;
153 }
154
155 *cachedObj = obj;
156 if (*cachedObj == NULL) {
157 LOG(ERROR) << StringPrintf("%s: global ref error", __func__);
158 return -1;
159 }
160 return 0;
161 }
162
163 } // namespace android
164