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