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_NativeNfcTag(e) == -1) return JNI_ERR;
54   if (RoutingManager::getInstance().registerJniFunctions(e) == -1)
55     return JNI_ERR;
56   if (NativeWlcManager::getInstance().registerJniFunctions(e) == -1)
57     return JNI_ERR;
58   LOG(DEBUG) << StringPrintf("%s: exit", __func__);
59   return JNI_VERSION_1_6;
60 }
61 
62 namespace android {
63 
64 /*******************************************************************************
65 **
66 ** Function:        nfc_jni_cache_object
67 **
68 ** Description:
69 **
70 ** Returns:         Status code.
71 **
72 *******************************************************************************/
nfc_jni_cache_object(JNIEnv * e,const char * className,jobject * cachedObj)73 int nfc_jni_cache_object(JNIEnv* e, const char* className, jobject* cachedObj) {
74   ScopedLocalRef<jclass> cls(e, e->FindClass(className));
75   if (cls.get() == NULL) {
76     LOG(ERROR) << StringPrintf("%s: find class error", __func__);
77     return -1;
78   }
79 
80   jmethodID ctor = e->GetMethodID(cls.get(), "<init>", "()V");
81   ScopedLocalRef<jobject> obj(e, e->NewObject(cls.get(), ctor));
82   if (obj.get() == NULL) {
83     LOG(ERROR) << StringPrintf("%s: create object error", __func__);
84     return -1;
85   }
86 
87   *cachedObj = e->NewGlobalRef(obj.get());
88   if (*cachedObj == NULL) {
89     LOG(ERROR) << StringPrintf("%s: global ref error", __func__);
90     return -1;
91   }
92   return 0;
93 }
94 
95 /*******************************************************************************
96 **
97 ** Function:        nfc_jni_get_nfc_socket_handle
98 **
99 ** Description:     Get the value of "mHandle" member variable.
100 **                  e: JVM environment.
101 **                  o: Java object.
102 **
103 ** Returns:         Value of mHandle.
104 **
105 *******************************************************************************/
nfc_jni_get_nfc_socket_handle(JNIEnv * e,jobject o)106 int nfc_jni_get_nfc_socket_handle(JNIEnv* e, jobject o) {
107   ScopedLocalRef<jclass> c(e, e->GetObjectClass(o));
108   jfieldID f = e->GetFieldID(c.get(), "mHandle", "I");
109   return e->GetIntField(o, f);
110 }
111 
112 /*******************************************************************************
113 **
114 ** Function:        nfc_jni_get_nat
115 **
116 ** Description:     Get the value of "mNative" member variable.
117 **                  e: JVM environment.
118 **                  o: Java object.
119 **
120 ** Returns:         Pointer to the value of mNative.
121 **
122 *******************************************************************************/
nfc_jni_get_nat(JNIEnv * e,jobject o)123 struct nfc_jni_native_data* nfc_jni_get_nat(JNIEnv* e, jobject o) {
124   ScopedLocalRef<jclass> c(e, e->GetObjectClass(o));
125   jfieldID f = e->GetFieldID(c.get(), "mNative", "J");
126   /* Retrieve native structure address */
127   return (struct nfc_jni_native_data*)e->GetLongField(o, f);
128 }
129 
130 /*******************************************************************************
131 **
132 ** Function         nfc_jni_cache_object_local
133 **
134 ** Description      Allocates a java object and calls it's constructor
135 **
136 ** Returns          -1 on failure, 0 on success
137 **
138 *******************************************************************************/
nfc_jni_cache_object_local(JNIEnv * e,const char * className,jobject * cachedObj)139 int nfc_jni_cache_object_local(JNIEnv* e, const char* className,
140                                jobject* cachedObj) {
141   ScopedLocalRef<jclass> cls(e, e->FindClass(className));
142   if (cls.get() == NULL) {
143     LOG(ERROR) << StringPrintf("%s: find class error", __func__);
144     return -1;
145   }
146 
147   jmethodID ctor = e->GetMethodID(cls.get(), "<init>", "()V");
148   jobject obj = e->NewObject(cls.get(), ctor);
149   if (obj == NULL) {
150     LOG(ERROR) << StringPrintf("%s: create object error", __func__);
151     return -1;
152   }
153 
154   *cachedObj = obj;
155   if (*cachedObj == NULL) {
156     LOG(ERROR) << StringPrintf("%s: global ref error", __func__);
157     return -1;
158   }
159   return 0;
160 }
161 
162 }  // namespace android
163