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