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