• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define LOG_TAG "BluetoothA2dpServiceJni"
18 
19 #define LOG_NDEBUG 0
20 
21 #include "com_android_bluetooth.h"
22 #include "hardware/bt_av.h"
23 #include "utils/Log.h"
24 #include "android_runtime/AndroidRuntime.h"
25 
26 #include <string.h>
27 
28 namespace android {
29 static jmethodID method_onConnectionStateChanged;
30 static jmethodID method_onAudioStateChanged;
31 
32 static const btav_interface_t *sBluetoothA2dpInterface = NULL;
33 static jobject mCallbacksObj = NULL;
34 static JNIEnv *sCallbackEnv = NULL;
35 
checkCallbackThread()36 static bool checkCallbackThread() {
37     // Always fetch the latest callbackEnv from AdapterService.
38     // Caching this could cause this sCallbackEnv to go out-of-sync
39     // with the AdapterService's ENV if an ASSOCIATE/DISASSOCIATE event
40     // is received
41     //if (sCallbackEnv == NULL) {
42     sCallbackEnv = getCallbackEnv();
43     //}
44 
45     JNIEnv* env = AndroidRuntime::getJNIEnv();
46     if (sCallbackEnv != env || sCallbackEnv == NULL) return false;
47     return true;
48 }
49 
bta2dp_connection_state_callback(btav_connection_state_t state,bt_bdaddr_t * bd_addr)50 static void bta2dp_connection_state_callback(btav_connection_state_t state, bt_bdaddr_t* bd_addr) {
51     jbyteArray addr;
52 
53     ALOGI("%s", __FUNCTION__);
54 
55     if (!checkCallbackThread()) {                                       \
56         ALOGE("Callback: '%s' is not called on the correct thread", __FUNCTION__); \
57         return;                                                         \
58     }
59     addr = sCallbackEnv->NewByteArray(sizeof(bt_bdaddr_t));
60     if (!addr) {
61         ALOGE("Fail to new jbyteArray bd addr for connection state");
62         checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__);
63         return;
64     }
65 
66     sCallbackEnv->SetByteArrayRegion(addr, 0, sizeof(bt_bdaddr_t), (jbyte*) bd_addr);
67     sCallbackEnv->CallVoidMethod(mCallbacksObj, method_onConnectionStateChanged, (jint) state,
68                                  addr);
69     checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__);
70     sCallbackEnv->DeleteLocalRef(addr);
71 }
72 
bta2dp_audio_state_callback(btav_audio_state_t state,bt_bdaddr_t * bd_addr)73 static void bta2dp_audio_state_callback(btav_audio_state_t state, bt_bdaddr_t* bd_addr) {
74     jbyteArray addr;
75 
76     ALOGI("%s", __FUNCTION__);
77 
78     if (!checkCallbackThread()) {                                       \
79         ALOGE("Callback: '%s' is not called on the correct thread", __FUNCTION__); \
80         return;                                                         \
81     }
82     addr = sCallbackEnv->NewByteArray(sizeof(bt_bdaddr_t));
83     if (!addr) {
84         ALOGE("Fail to new jbyteArray bd addr for connection state");
85         checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__);
86         return;
87     }
88 
89     sCallbackEnv->SetByteArrayRegion(addr, 0, sizeof(bt_bdaddr_t), (jbyte*) bd_addr);
90     sCallbackEnv->CallVoidMethod(mCallbacksObj, method_onAudioStateChanged, (jint) state,
91                                  addr);
92     checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__);
93     sCallbackEnv->DeleteLocalRef(addr);
94 }
95 
96 static btav_callbacks_t sBluetoothA2dpCallbacks = {
97     sizeof(sBluetoothA2dpCallbacks),
98     bta2dp_connection_state_callback,
99     bta2dp_audio_state_callback,
100     NULL, /* audio_config_cb */
101 };
102 
classInitNative(JNIEnv * env,jclass clazz)103 static void classInitNative(JNIEnv* env, jclass clazz) {
104     method_onConnectionStateChanged =
105         env->GetMethodID(clazz, "onConnectionStateChanged", "(I[B)V");
106 
107     method_onAudioStateChanged =
108         env->GetMethodID(clazz, "onAudioStateChanged", "(I[B)V");
109 
110     ALOGI("%s: succeeds", __FUNCTION__);
111 }
112 
initNative(JNIEnv * env,jobject object)113 static void initNative(JNIEnv *env, jobject object) {
114     const bt_interface_t* btInf;
115     bt_status_t status;
116 
117     if ( (btInf = getBluetoothInterface()) == NULL) {
118         ALOGE("Bluetooth module is not loaded");
119         return;
120     }
121 
122     if (sBluetoothA2dpInterface !=NULL) {
123          ALOGW("Cleaning up A2DP Interface before initializing...");
124          sBluetoothA2dpInterface->cleanup();
125          sBluetoothA2dpInterface = NULL;
126     }
127 
128     if (mCallbacksObj != NULL) {
129          ALOGW("Cleaning up A2DP callback object");
130          env->DeleteGlobalRef(mCallbacksObj);
131          mCallbacksObj = NULL;
132     }
133 
134     if ( (sBluetoothA2dpInterface = (btav_interface_t *)
135           btInf->get_profile_interface(BT_PROFILE_ADVANCED_AUDIO_ID)) == NULL) {
136         ALOGE("Failed to get Bluetooth A2DP Interface");
137         return;
138     }
139 
140     if ( (status = sBluetoothA2dpInterface->init(&sBluetoothA2dpCallbacks)) != BT_STATUS_SUCCESS) {
141         ALOGE("Failed to initialize Bluetooth A2DP, status: %d", status);
142         sBluetoothA2dpInterface = NULL;
143         return;
144     }
145 
146     mCallbacksObj = env->NewGlobalRef(object);
147 }
148 
cleanupNative(JNIEnv * env,jobject object)149 static void cleanupNative(JNIEnv *env, jobject object) {
150     const bt_interface_t* btInf;
151 
152     if ( (btInf = getBluetoothInterface()) == NULL) {
153         ALOGE("Bluetooth module is not loaded");
154         return;
155     }
156 
157     if (sBluetoothA2dpInterface !=NULL) {
158         sBluetoothA2dpInterface->cleanup();
159         sBluetoothA2dpInterface = NULL;
160     }
161 
162     if (mCallbacksObj != NULL) {
163         env->DeleteGlobalRef(mCallbacksObj);
164         mCallbacksObj = NULL;
165     }
166 }
167 
connectA2dpNative(JNIEnv * env,jobject object,jbyteArray address)168 static jboolean connectA2dpNative(JNIEnv *env, jobject object, jbyteArray address) {
169     jbyte *addr;
170     bt_bdaddr_t * btAddr;
171     bt_status_t status;
172 
173     ALOGI("%s: sBluetoothA2dpInterface: %p", __FUNCTION__, sBluetoothA2dpInterface);
174     if (!sBluetoothA2dpInterface) return JNI_FALSE;
175 
176     addr = env->GetByteArrayElements(address, NULL);
177     btAddr = (bt_bdaddr_t *) addr;
178     if (!addr) {
179         jniThrowIOException(env, EINVAL);
180         return JNI_FALSE;
181     }
182 
183     if ((status = sBluetoothA2dpInterface->connect((bt_bdaddr_t *)addr)) != BT_STATUS_SUCCESS) {
184         ALOGE("Failed HF connection, status: %d", status);
185     }
186     env->ReleaseByteArrayElements(address, addr, 0);
187     return (status == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE;
188 }
189 
disconnectA2dpNative(JNIEnv * env,jobject object,jbyteArray address)190 static jboolean disconnectA2dpNative(JNIEnv *env, jobject object, jbyteArray address) {
191     jbyte *addr;
192     bt_status_t status;
193 
194     if (!sBluetoothA2dpInterface) return JNI_FALSE;
195 
196     addr = env->GetByteArrayElements(address, NULL);
197     if (!addr) {
198         jniThrowIOException(env, EINVAL);
199         return JNI_FALSE;
200     }
201 
202     if ( (status = sBluetoothA2dpInterface->disconnect((bt_bdaddr_t *)addr)) != BT_STATUS_SUCCESS) {
203         ALOGE("Failed HF disconnection, status: %d", status);
204     }
205     env->ReleaseByteArrayElements(address, addr, 0);
206     return (status == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE;
207 }
208 
209 static JNINativeMethod sMethods[] = {
210     {"classInitNative", "()V", (void *) classInitNative},
211     {"initNative", "()V", (void *) initNative},
212     {"cleanupNative", "()V", (void *) cleanupNative},
213     {"connectA2dpNative", "([B)Z", (void *) connectA2dpNative},
214     {"disconnectA2dpNative", "([B)Z", (void *) disconnectA2dpNative},
215 };
216 
register_com_android_bluetooth_a2dp(JNIEnv * env)217 int register_com_android_bluetooth_a2dp(JNIEnv* env)
218 {
219     return jniRegisterNativeMethods(env, "com/android/bluetooth/a2dp/A2dpStateMachine",
220                                     sMethods, NELEM(sMethods));
221 }
222 
223 }
224