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