• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <errno.h>
18 
19 
20 /*******************************************************************************
21 **
22 ** Function:        JNI_OnLoad
23 **
24 ** Description:     Register all JNI functions with Java Virtual Machine.
25 **                  jvm: Java Virtual Machine.
26 **                  reserved: Not used.
27 **
28 ** Returns:         JNI version.
29 **
30 *******************************************************************************/
JNI_OnLoad(JavaVM * jvm,void * reserved)31 jint JNI_OnLoad (JavaVM *jvm, void *reserved)
32 {
33     ALOGD ("%s: enter", __FUNCTION__);
34     JNIEnv *e = NULL;
35 
36     // Check JNI version
37     if (jvm->GetEnv ((void **) &e, JNI_VERSION_1_6))
38         return JNI_ERR;
39 
40     if (android::register_com_android_nfc_NativeNfcManager (e) == -1)
41         return JNI_ERR;
42     if (android::register_com_android_nfc_NativeLlcpServiceSocket (e) == -1)
43         return JNI_ERR;
44     if (android::register_com_android_nfc_NativeLlcpSocket (e) == -1)
45         return JNI_ERR;
46     if (android::register_com_android_nfc_NativeNfcTag (e) == -1)
47         return JNI_ERR;
48     if (android::register_com_android_nfc_NativeLlcpConnectionlessSocket (e) == -1)
49         return JNI_ERR;
50     if (android::register_com_android_nfc_NativeP2pDevice (e) == -1)
51         return JNI_ERR;
52     if (android::register_com_android_nfc_NativeNfcSecureElement (e) == -1)
53         return JNI_ERR;
54     ALOGD ("%s: exit", __FUNCTION__);
55     return JNI_VERSION_1_6;
56 }
57 
58 
59 namespace android
60 {
61 
62 
63 /*******************************************************************************
64 **
65 ** Function:        nfc_jni_cache_object
66 **
67 ** Description:
68 **
69 ** Returns:         Status code.
70 **
71 *******************************************************************************/
nfc_jni_cache_object(JNIEnv * e,const char * className,jobject * cachedObj)72 int nfc_jni_cache_object (JNIEnv *e, const char *className, jobject *cachedObj)
73 {
74     jclass cls = NULL;
75     jobject obj = NULL;
76     jmethodID ctor = 0;
77 
78     cls = e->FindClass (className);
79     if(cls == NULL)
80     {
81         ALOGE ("%s: find class error", __FUNCTION__);
82         return -1;
83     }
84 
85     ctor = e->GetMethodID (cls, "<init>", "()V");
86     obj = e->NewObject (cls, ctor);
87     if (obj == NULL)
88     {
89        ALOGE ("%s: create object error", __FUNCTION__);
90        return -1;
91     }
92 
93     *cachedObj = e->NewGlobalRef (obj);
94     if (*cachedObj == NULL)
95     {
96         e->DeleteLocalRef (obj);
97         ALOGE ("%s: global ref error", __FUNCTION__);
98         return -1;
99     }
100     e->DeleteLocalRef (obj);
101     return 0;
102 }
103 
104 
105 /*******************************************************************************
106 **
107 ** Function:        nfc_jni_get_nfc_socket_handle
108 **
109 ** Description:     Get the value of "mHandle" member variable.
110 **                  e: JVM environment.
111 **                  o: Java object.
112 **
113 ** Returns:         Value of mHandle.
114 **
115 *******************************************************************************/
nfc_jni_get_nfc_socket_handle(JNIEnv * e,jobject o)116 int nfc_jni_get_nfc_socket_handle (JNIEnv *e, jobject o)
117 {
118     jclass c = NULL;
119     jfieldID f = 0;
120 
121     c = e->GetObjectClass (o);
122     f = e->GetFieldID (c, "mHandle", "I");
123     return e->GetIntField (o, f);
124 }
125 
126 
127 /*******************************************************************************
128 **
129 ** Function:        nfc_jni_get_nat
130 **
131 ** Description:     Get the value of "mNative" member variable.
132 **                  e: JVM environment.
133 **                  o: Java object.
134 **
135 ** Returns:         Pointer to the value of mNative.
136 **
137 *******************************************************************************/
nfc_jni_get_nat(JNIEnv * e,jobject o)138 struct nfc_jni_native_data* nfc_jni_get_nat(JNIEnv *e, jobject o)
139 {
140    jclass c = NULL;
141    jfieldID f = 0;
142 
143    /* Retrieve native structure address */
144    c = e->GetObjectClass(o);
145    f = e->GetFieldID(c, "mNative", "I");
146    return (struct nfc_jni_native_data*)e->GetIntField(o, f);
147 }
148 
149 
150 } // namespace android
151