Loading android/app/jni/com_android_bluetooth.h +5 −7 Original line number Diff line number Diff line Loading @@ -18,18 +18,17 @@ #ifndef COM_ANDROID_BLUETOOTH_H #define COM_ANDROID_BLUETOOTH_H #include <nativehelper/JNIHelp.h> #include "android_runtime/AndroidRuntime.h" #include "android_runtime/Log.h" #include "hardware/bluetooth.h" #include "hardware/hardware.h" #include "jni.h" #include "jni_logging.h" #include "nativehelper/ScopedLocalRef.h" #include "utils/Log.h" namespace android { JNIEnv* getCallbackEnv(); bool isCallbackThread(); class CallbackEnv { public: Loading @@ -46,9 +45,8 @@ public: } bool valid() const { JNIEnv *env = AndroidRuntime::getJNIEnv(); if (!mCallbackEnv || (mCallbackEnv != env)) { ALOGE("%s: Callback env fail: env: %p, callback: %p", mName, env, mCallbackEnv); if (!mCallbackEnv || !isCallbackThread()) { ALOGE("%s: Callback env fail", mName); return false; } return true; Loading android/app/jni/com_android_bluetooth_btservice_AdapterService.cpp +21 −9 Original line number Diff line number Diff line Loading @@ -15,8 +15,6 @@ */ #define LOG_TAG "BluetoothServiceJni" #include "android_runtime/AndroidRuntime.h" #include "android_runtime/Log.h" #include "bluetooth_socket_manager.h" #include "com_android_bluetooth.h" #include "hardware/bt_sock.h" Loading @@ -40,6 +38,8 @@ #include <hardware/bluetooth.h> #include <mutex> #include <pthread.h> using android::bluetooth::BluetoothSocketManagerBinderServer; namespace android { Loading Loading @@ -70,7 +70,10 @@ static struct { static const bt_interface_t* sBluetoothInterface = NULL; static const btsock_interface_t* sBluetoothSocketInterface = NULL; static JavaVM* vm = NULL; static JNIEnv* callbackEnv = NULL; static pthread_t sCallbackThread; static bool sHaveCallbackThread; static jobject sJniAdapterServiceObj; static jobject sJniCallbacksObj; Loading @@ -85,6 +88,10 @@ const bt_interface_t* getBluetoothInterface() { return sBluetoothInterface; } JNIEnv* getCallbackEnv() { return callbackEnv; } bool isCallbackThread() { return sHaveCallbackThread && pthread_equal(sCallbackThread, pthread_self()); } static void adapter_state_change_callback(bt_state_t status) { CallbackEnv sCallbackEnv(__func__); if (!sCallbackEnv.valid()) return; Loading Loading @@ -389,7 +396,6 @@ static void ssp_request_callback(RawAddress* bd_addr, bt_bdname_t* bdname, } static void callback_thread_event(bt_cb_thread_evt event) { JavaVM* vm = AndroidRuntime::getJavaVM(); if (event == ASSOCIATE_JVM) { JavaVMAttachArgs args; char name[] = "BT Service Callback Thread"; Loading @@ -397,13 +403,16 @@ static void callback_thread_event(bt_cb_thread_evt event) { args.name = name; args.group = NULL; vm->AttachCurrentThread(&callbackEnv, &args); sHaveCallbackThread = true; sCallbackThread = pthread_self(); ALOGV("Callback thread attached: %p", callbackEnv); } else if (event == DISASSOCIATE_JVM) { if (callbackEnv != AndroidRuntime::getJNIEnv()) { if (!isCallbackThread()) { ALOGE("Callback: '%s' is not called on the correct thread", __func__); return; } vm->DetachCurrentThread(); sHaveCallbackThread = false; } } Loading Loading @@ -463,8 +472,7 @@ static void* sAlarmCallbackData; class JNIThreadAttacher { public: JNIThreadAttacher() : vm_(nullptr), env_(nullptr) { vm_ = AndroidRuntime::getJavaVM(); JNIThreadAttacher(JavaVM* vm) : vm_(vm), env_(nullptr) { status_ = vm_->GetEnv((void**)&env_, JNI_VERSION_1_6); if (status_ != JNI_OK && status_ != JNI_EDETACHED) { Loading Loading @@ -510,7 +518,7 @@ class JNIThreadAttacher { static bool set_wake_alarm_callout(uint64_t delay_millis, bool should_wake, alarm_cb cb, void* data) { JNIThreadAttacher attacher; JNIThreadAttacher attacher(vm); JNIEnv* env = attacher.getEnv(); if (env == nullptr) { Loading @@ -534,7 +542,7 @@ static bool set_wake_alarm_callout(uint64_t delay_millis, bool should_wake, } static int acquire_wake_lock_callout(const char* lock_name) { JNIThreadAttacher attacher; JNIThreadAttacher attacher(vm); JNIEnv* env = attacher.getEnv(); if (env == nullptr) { Loading @@ -559,7 +567,7 @@ static int acquire_wake_lock_callout(const char* lock_name) { } static int release_wake_lock_callout(const char* lock_name) { JNIThreadAttacher attacher; JNIThreadAttacher attacher(vm); JNIEnv* env = attacher.getEnv(); if (env == nullptr) { Loading Loading @@ -678,6 +686,10 @@ static void classInitNative(JNIEnv* env, jclass clazz) { method_energyInfo = env->GetMethodID( clazz, "energyInfoCallback", "(IIJJJJ[Landroid/bluetooth/UidTraffic;)V"); if (env->GetJavaVM(&vm) != JNI_OK) { ALOGE("Could not get JavaVM"); } if (hal_util_load_bt_library((bt_interface_t const**)&sBluetoothInterface)) { ALOGE("No Bluetooth Library found"); } Loading android/app/jni/jni_logging.h 0 → 100644 +31 −0 Original line number Diff line number Diff line /* * Copyright 2019 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include <nativehelper/JNIHelp.h> /* Logging macros imported from android_runtime/Log.h * * Logs an exception. If the exception is omitted or NULL, logs the current * exception from the JNI environment, if any. */ #define LOG_EX(env, priority, tag, ...) \ jniLogException(env, ANDROID_##priority, tag, ##__VA_ARGS__) #define LOGV_EX(env, ...) LOG_EX(env, LOG_VERBOSE, LOG_TAG, ##__VA_ARGS__) #define LOGD_EX(env, ...) LOG_EX(env, LOG_DEBUG, LOG_TAG, ##__VA_ARGS__) #define LOGI_EX(env, ...) LOG_EX(env, LOG_INFO, LOG_TAG, ##__VA_ARGS__) #define LOGW_EX(env, ...) LOG_EX(env, LOG_WARN, LOG_TAG, ##__VA_ARGS__) #define LOGE_EX(env, ...) LOG_EX(env, LOG_ERROR, LOG_TAG, ##__VA_ARGS__) Loading
android/app/jni/com_android_bluetooth.h +5 −7 Original line number Diff line number Diff line Loading @@ -18,18 +18,17 @@ #ifndef COM_ANDROID_BLUETOOTH_H #define COM_ANDROID_BLUETOOTH_H #include <nativehelper/JNIHelp.h> #include "android_runtime/AndroidRuntime.h" #include "android_runtime/Log.h" #include "hardware/bluetooth.h" #include "hardware/hardware.h" #include "jni.h" #include "jni_logging.h" #include "nativehelper/ScopedLocalRef.h" #include "utils/Log.h" namespace android { JNIEnv* getCallbackEnv(); bool isCallbackThread(); class CallbackEnv { public: Loading @@ -46,9 +45,8 @@ public: } bool valid() const { JNIEnv *env = AndroidRuntime::getJNIEnv(); if (!mCallbackEnv || (mCallbackEnv != env)) { ALOGE("%s: Callback env fail: env: %p, callback: %p", mName, env, mCallbackEnv); if (!mCallbackEnv || !isCallbackThread()) { ALOGE("%s: Callback env fail", mName); return false; } return true; Loading
android/app/jni/com_android_bluetooth_btservice_AdapterService.cpp +21 −9 Original line number Diff line number Diff line Loading @@ -15,8 +15,6 @@ */ #define LOG_TAG "BluetoothServiceJni" #include "android_runtime/AndroidRuntime.h" #include "android_runtime/Log.h" #include "bluetooth_socket_manager.h" #include "com_android_bluetooth.h" #include "hardware/bt_sock.h" Loading @@ -40,6 +38,8 @@ #include <hardware/bluetooth.h> #include <mutex> #include <pthread.h> using android::bluetooth::BluetoothSocketManagerBinderServer; namespace android { Loading Loading @@ -70,7 +70,10 @@ static struct { static const bt_interface_t* sBluetoothInterface = NULL; static const btsock_interface_t* sBluetoothSocketInterface = NULL; static JavaVM* vm = NULL; static JNIEnv* callbackEnv = NULL; static pthread_t sCallbackThread; static bool sHaveCallbackThread; static jobject sJniAdapterServiceObj; static jobject sJniCallbacksObj; Loading @@ -85,6 +88,10 @@ const bt_interface_t* getBluetoothInterface() { return sBluetoothInterface; } JNIEnv* getCallbackEnv() { return callbackEnv; } bool isCallbackThread() { return sHaveCallbackThread && pthread_equal(sCallbackThread, pthread_self()); } static void adapter_state_change_callback(bt_state_t status) { CallbackEnv sCallbackEnv(__func__); if (!sCallbackEnv.valid()) return; Loading Loading @@ -389,7 +396,6 @@ static void ssp_request_callback(RawAddress* bd_addr, bt_bdname_t* bdname, } static void callback_thread_event(bt_cb_thread_evt event) { JavaVM* vm = AndroidRuntime::getJavaVM(); if (event == ASSOCIATE_JVM) { JavaVMAttachArgs args; char name[] = "BT Service Callback Thread"; Loading @@ -397,13 +403,16 @@ static void callback_thread_event(bt_cb_thread_evt event) { args.name = name; args.group = NULL; vm->AttachCurrentThread(&callbackEnv, &args); sHaveCallbackThread = true; sCallbackThread = pthread_self(); ALOGV("Callback thread attached: %p", callbackEnv); } else if (event == DISASSOCIATE_JVM) { if (callbackEnv != AndroidRuntime::getJNIEnv()) { if (!isCallbackThread()) { ALOGE("Callback: '%s' is not called on the correct thread", __func__); return; } vm->DetachCurrentThread(); sHaveCallbackThread = false; } } Loading Loading @@ -463,8 +472,7 @@ static void* sAlarmCallbackData; class JNIThreadAttacher { public: JNIThreadAttacher() : vm_(nullptr), env_(nullptr) { vm_ = AndroidRuntime::getJavaVM(); JNIThreadAttacher(JavaVM* vm) : vm_(vm), env_(nullptr) { status_ = vm_->GetEnv((void**)&env_, JNI_VERSION_1_6); if (status_ != JNI_OK && status_ != JNI_EDETACHED) { Loading Loading @@ -510,7 +518,7 @@ class JNIThreadAttacher { static bool set_wake_alarm_callout(uint64_t delay_millis, bool should_wake, alarm_cb cb, void* data) { JNIThreadAttacher attacher; JNIThreadAttacher attacher(vm); JNIEnv* env = attacher.getEnv(); if (env == nullptr) { Loading @@ -534,7 +542,7 @@ static bool set_wake_alarm_callout(uint64_t delay_millis, bool should_wake, } static int acquire_wake_lock_callout(const char* lock_name) { JNIThreadAttacher attacher; JNIThreadAttacher attacher(vm); JNIEnv* env = attacher.getEnv(); if (env == nullptr) { Loading @@ -559,7 +567,7 @@ static int acquire_wake_lock_callout(const char* lock_name) { } static int release_wake_lock_callout(const char* lock_name) { JNIThreadAttacher attacher; JNIThreadAttacher attacher(vm); JNIEnv* env = attacher.getEnv(); if (env == nullptr) { Loading Loading @@ -678,6 +686,10 @@ static void classInitNative(JNIEnv* env, jclass clazz) { method_energyInfo = env->GetMethodID( clazz, "energyInfoCallback", "(IIJJJJ[Landroid/bluetooth/UidTraffic;)V"); if (env->GetJavaVM(&vm) != JNI_OK) { ALOGE("Could not get JavaVM"); } if (hal_util_load_bt_library((bt_interface_t const**)&sBluetoothInterface)) { ALOGE("No Bluetooth Library found"); } Loading
android/app/jni/jni_logging.h 0 → 100644 +31 −0 Original line number Diff line number Diff line /* * Copyright 2019 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include <nativehelper/JNIHelp.h> /* Logging macros imported from android_runtime/Log.h * * Logs an exception. If the exception is omitted or NULL, logs the current * exception from the JNI environment, if any. */ #define LOG_EX(env, priority, tag, ...) \ jniLogException(env, ANDROID_##priority, tag, ##__VA_ARGS__) #define LOGV_EX(env, ...) LOG_EX(env, LOG_VERBOSE, LOG_TAG, ##__VA_ARGS__) #define LOGD_EX(env, ...) LOG_EX(env, LOG_DEBUG, LOG_TAG, ##__VA_ARGS__) #define LOGI_EX(env, ...) LOG_EX(env, LOG_INFO, LOG_TAG, ##__VA_ARGS__) #define LOGW_EX(env, ...) LOG_EX(env, LOG_WARN, LOG_TAG, ##__VA_ARGS__) #define LOGE_EX(env, ...) LOG_EX(env, LOG_ERROR, LOG_TAG, ##__VA_ARGS__)