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