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 #include "NfcJniUtil.h"
17
18 #include <cutils/log.h>
19 #include <errno.h>
20 #include <JNIHelp.h>
21 #include <ScopedLocalRef.h>
22 #include "RoutingManager.h"
23
24
25 /*******************************************************************************
26 **
27 ** Function: JNI_OnLoad
28 **
29 ** Description: Register all JNI functions with Java Virtual Machine.
30 ** jvm: Java Virtual Machine.
31 ** reserved: Not used.
32 **
33 ** Returns: JNI version.
34 **
35 *******************************************************************************/
JNI_OnLoad(JavaVM * jvm,void *)36 jint JNI_OnLoad (JavaVM* jvm, void*)
37 {
38 ALOGD ("%s: enter", __FUNCTION__);
39 JNIEnv *e = NULL;
40
41 ALOGI("NFC Service: loading nci JNI");
42
43 // Check JNI version
44 if (jvm->GetEnv ((void **) &e, JNI_VERSION_1_6))
45 return JNI_ERR;
46
47 if (android::register_com_android_nfc_NativeNfcManager (e) == -1)
48 return JNI_ERR;
49 if (android::register_com_android_nfc_NativeLlcpServiceSocket (e) == -1)
50 return JNI_ERR;
51 if (android::register_com_android_nfc_NativeLlcpSocket (e) == -1)
52 return JNI_ERR;
53 if (android::register_com_android_nfc_NativeNfcTag (e) == -1)
54 return JNI_ERR;
55 if (android::register_com_android_nfc_NativeLlcpConnectionlessSocket (e) == -1)
56 return JNI_ERR;
57 if (android::register_com_android_nfc_NativeP2pDevice (e) == -1)
58 return JNI_ERR;
59 if (RoutingManager::getInstance().registerJniFunctions (e) == -1)
60 return JNI_ERR;
61 ALOGD ("%s: exit", __FUNCTION__);
62 return JNI_VERSION_1_6;
63 }
64
65
66 namespace android
67 {
68
69
70 /*******************************************************************************
71 **
72 ** Function: nfc_jni_cache_object
73 **
74 ** Description:
75 **
76 ** Returns: Status code.
77 **
78 *******************************************************************************/
nfc_jni_cache_object(JNIEnv * e,const char * className,jobject * cachedObj)79 int nfc_jni_cache_object (JNIEnv *e, const char *className, jobject *cachedObj)
80 {
81 ScopedLocalRef<jclass> cls(e, e->FindClass(className));
82 if (cls.get() == NULL)
83 {
84 ALOGE("%s: find class error", __FUNCTION__);
85 return -1;
86 }
87
88 jmethodID ctor = e->GetMethodID(cls.get(), "<init>", "()V");
89 ScopedLocalRef<jobject> obj(e, e->NewObject(cls.get(), ctor));
90 if (obj.get() == NULL)
91 {
92 ALOGE("%s: create object error", __FUNCTION__);
93 return -1;
94 }
95
96 *cachedObj = e->NewGlobalRef(obj.get());
97 if (*cachedObj == NULL)
98 {
99 ALOGE("%s: global ref error", __FUNCTION__);
100 return -1;
101 }
102 return 0;
103 }
104
105
106 /*******************************************************************************
107 **
108 ** Function: nfc_jni_get_nfc_socket_handle
109 **
110 ** Description: Get the value of "mHandle" member variable.
111 ** e: JVM environment.
112 ** o: Java object.
113 **
114 ** Returns: Value of mHandle.
115 **
116 *******************************************************************************/
nfc_jni_get_nfc_socket_handle(JNIEnv * e,jobject o)117 int nfc_jni_get_nfc_socket_handle (JNIEnv *e, jobject o)
118 {
119 ScopedLocalRef<jclass> c(e, e->GetObjectClass(o));
120 jfieldID f = e->GetFieldID(c.get(), "mHandle", "I");
121 return e->GetIntField(o, f);
122 }
123
124
125 /*******************************************************************************
126 **
127 ** Function: nfc_jni_get_nat
128 **
129 ** Description: Get the value of "mNative" member variable.
130 ** e: JVM environment.
131 ** o: Java object.
132 **
133 ** Returns: Pointer to the value of mNative.
134 **
135 *******************************************************************************/
nfc_jni_get_nat(JNIEnv * e,jobject o)136 struct nfc_jni_native_data* nfc_jni_get_nat(JNIEnv *e, jobject o)
137 {
138 ScopedLocalRef<jclass> c(e, e->GetObjectClass(o));
139 jfieldID f = e->GetFieldID(c.get(), "mNative", "J");
140 /* Retrieve native structure address */
141 return (struct nfc_jni_native_data*) e->GetLongField(o, f);
142 }
143
144
145 /*******************************************************************************
146 **
147 ** Function nfc_jni_cache_object_local
148 **
149 ** Description Allocates a java object and calls it's constructor
150 **
151 ** Returns -1 on failure, 0 on success
152 **
153 *******************************************************************************/
nfc_jni_cache_object_local(JNIEnv * e,const char * className,jobject * cachedObj)154 int nfc_jni_cache_object_local (JNIEnv *e, const char *className, jobject *cachedObj)
155 {
156 ScopedLocalRef<jclass> cls(e, e->FindClass(className));
157 if(cls.get() == NULL)
158 {
159 ALOGE ("%s: find class error", __FUNCTION__);
160 return -1;
161 }
162
163 jmethodID ctor = e->GetMethodID(cls.get(), "<init>", "()V");
164 jobject obj = e->NewObject(cls.get(), ctor);
165 if (obj == NULL)
166 {
167 ALOGE ("%s: create object error", __FUNCTION__);
168 return -1;
169 }
170
171 *cachedObj = obj;
172 if (*cachedObj == NULL)
173 {
174 ALOGE ("%s: global ref error", __FUNCTION__);
175 return -1;
176 }
177 return 0;
178 }
179
180
181 } // namespace android
182