blob: edbbec88b64b21ee6abf593ab665e7b6826c7cd4 [file] [log] [blame]
Matthew Xie676cb1b2012-03-22 17:32:29 -07001/*
Zhihai Xuede67c22012-10-23 17:01:01 -07002 * 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.
Matthew Xie676cb1b2012-03-22 17:32:29 -070015 */
16
17#define LOG_TAG "BluetoothHealthServiceJni"
18
19#define LOG_NDEBUG 0
20
21#define CHECK_CALLBACK_ENV \
22 if (!checkCallbackThread()) { \
Matthew Xiec55a9832012-04-07 03:44:13 -070023 ALOGE("Callback: '%s' is not called on the correct thread", __FUNCTION__);\
Matthew Xie676cb1b2012-03-22 17:32:29 -070024 return; \
25 }
26
27#include "com_android_bluetooth.h"
28#include "hardware/bt_hl.h"
29#include "utils/Log.h"
30#include "android_runtime/AndroidRuntime.h"
31
32#include <string.h>
33
34namespace android {
35
36static jmethodID method_onAppRegistrationState;
37static jmethodID method_onChannelStateChanged;
38
39static const bthl_interface_t *sBluetoothHdpInterface = NULL;
40static jobject mCallbacksObj = NULL;
41static JNIEnv *sCallbackEnv = NULL;
42
43static bool checkCallbackThread() {
44 sCallbackEnv = getCallbackEnv();
45
46 JNIEnv* env = AndroidRuntime::getJNIEnv();
47 if (sCallbackEnv != env || sCallbackEnv == NULL) {
Matthew Xiec55a9832012-04-07 03:44:13 -070048 ALOGE("Callback env check fail: env: %p, callback: %p", env, sCallbackEnv);
Matthew Xie676cb1b2012-03-22 17:32:29 -070049 return false;
50 }
51 return true;
52}
53
54// Define callback functions
55static void app_registration_state_callback(int app_id, bthl_app_reg_state_t state) {
56 CHECK_CALLBACK_ENV
57 sCallbackEnv->CallVoidMethod(mCallbacksObj, method_onAppRegistrationState, app_id,
58 (jint) state);
59 checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__);
60}
61
62static void channel_state_callback(int app_id, bt_bdaddr_t *bd_addr, int mdep_cfg_index,
63 int channel_id, bthl_channel_state_t state, int fd) {
64 jbyteArray addr;
65 jobject fileDescriptor = NULL;
66
67 CHECK_CALLBACK_ENV
68 addr = sCallbackEnv->NewByteArray(sizeof(bt_bdaddr_t));
69 if (!addr) {
Matthew Xiec55a9832012-04-07 03:44:13 -070070 ALOGE("Fail to new jbyteArray bd addr for channel state");
Matthew Xie676cb1b2012-03-22 17:32:29 -070071 checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__);
72 return;
73 }
74
75 // TODO(BT) check if fd is only valid for BTHH_CONN_STATE_CONNECTED state
76 if (state == BTHL_CONN_STATE_CONNECTED) {
77 fileDescriptor = jniCreateFileDescriptor(sCallbackEnv, fd);
78 if (!fileDescriptor) {
Matthew Xiec55a9832012-04-07 03:44:13 -070079 ALOGE("Failed to convert file descriptor, fd: %d", fd);
Matthew Xie676cb1b2012-03-22 17:32:29 -070080 sCallbackEnv->DeleteLocalRef(addr);
81 return;
82 }
83 }
84
85 sCallbackEnv->SetByteArrayRegion(addr, 0, sizeof(bt_bdaddr_t), (jbyte*) bd_addr);
86 sCallbackEnv->CallVoidMethod(mCallbacksObj, method_onChannelStateChanged, app_id, addr,
87 mdep_cfg_index, channel_id, (jint) state, fileDescriptor);
88 checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__);
89 sCallbackEnv->DeleteLocalRef(addr);
90}
91
92static bthl_callbacks_t sBluetoothHdpCallbacks = {
93 sizeof(sBluetoothHdpCallbacks),
94 app_registration_state_callback,
95 channel_state_callback
96};
97
98// Define native functions
99
100static void classInitNative(JNIEnv* env, jclass clazz) {
Matthew Xie676cb1b2012-03-22 17:32:29 -0700101 method_onAppRegistrationState = env->GetMethodID(clazz, "onAppRegistrationState", "(II)V");
102 method_onChannelStateChanged = env->GetMethodID(clazz, "onChannelStateChanged",
Aurimas Liutikasd4a64dd2016-02-22 16:39:38 -0800103 "(I[BIIILjava/io/FileDescriptor;)V");
Matthew Xiec55a9832012-04-07 03:44:13 -0700104 ALOGI("%s: succeeds", __FUNCTION__);
Matthew Xie676cb1b2012-03-22 17:32:29 -0700105}
106
fredc6654f5c2012-04-12 00:18:52 -0700107static void initializeNative(JNIEnv *env, jobject object) {
108 const bt_interface_t* btInf;
109 bt_status_t status;
110
111 if ( (btInf = getBluetoothInterface()) == NULL) {
Matthew Xiee469f162012-06-05 23:57:59 -0700112 ALOGE("Bluetooth module is not loaded");
fredc6654f5c2012-04-12 00:18:52 -0700113 return;
114 }
115
116 if (sBluetoothHdpInterface !=NULL) {
Matthew Xiee469f162012-06-05 23:57:59 -0700117 ALOGW("Cleaning up Bluetooth Health Interface before initializing...");
fredc6654f5c2012-04-12 00:18:52 -0700118 sBluetoothHdpInterface->cleanup();
119 sBluetoothHdpInterface = NULL;
120 }
121
122 if (mCallbacksObj != NULL) {
Matthew Xiee469f162012-06-05 23:57:59 -0700123 ALOGW("Cleaning up Bluetooth Health callback object");
fredc6654f5c2012-04-12 00:18:52 -0700124 env->DeleteGlobalRef(mCallbacksObj);
125 mCallbacksObj = NULL;
126 }
127
128 if ( (sBluetoothHdpInterface = (bthl_interface_t *)
129 btInf->get_profile_interface(BT_PROFILE_HEALTH_ID)) == NULL) {
Matthew Xiee469f162012-06-05 23:57:59 -0700130 ALOGE("Failed to get Bluetooth Health Interface");
fredc6654f5c2012-04-12 00:18:52 -0700131 return;
132 }
133
134 if ( (status = sBluetoothHdpInterface->init(&sBluetoothHdpCallbacks)) != BT_STATUS_SUCCESS) {
Matthew Xiee469f162012-06-05 23:57:59 -0700135 ALOGE("Failed to initialize Bluetooth HDP, status: %d", status);
fredc6654f5c2012-04-12 00:18:52 -0700136 sBluetoothHdpInterface = NULL;
137 return;
138 }
139
Matthew Xie676cb1b2012-03-22 17:32:29 -0700140 mCallbacksObj = env->NewGlobalRef(object);
141}
142
fredc6654f5c2012-04-12 00:18:52 -0700143static void cleanupNative(JNIEnv *env, jobject object) {
144 const bt_interface_t* btInf;
fredc6654f5c2012-04-12 00:18:52 -0700145
146 if ( (btInf = getBluetoothInterface()) == NULL) {
Matthew Xiee469f162012-06-05 23:57:59 -0700147 ALOGE("Bluetooth module is not loaded");
fredc6654f5c2012-04-12 00:18:52 -0700148 return;
149 }
150
151 if (sBluetoothHdpInterface !=NULL) {
Matthew Xiee469f162012-06-05 23:57:59 -0700152 ALOGW("Cleaning up Bluetooth Health Interface...");
fredc6654f5c2012-04-12 00:18:52 -0700153 sBluetoothHdpInterface->cleanup();
154 sBluetoothHdpInterface = NULL;
155 }
156
157 if (mCallbacksObj != NULL) {
Matthew Xiee469f162012-06-05 23:57:59 -0700158 ALOGW("Cleaning up Bluetooth Health object");
fredc6654f5c2012-04-12 00:18:52 -0700159 env->DeleteGlobalRef(mCallbacksObj);
160 mCallbacksObj = NULL;
161 }
162}
163
Matthew Xie676cb1b2012-03-22 17:32:29 -0700164static jint registerHealthAppNative(JNIEnv *env, jobject object, jint data_type,
165 jint role, jstring name, jint channel_type) {
166 bt_status_t status;
167 bthl_mdep_cfg_t mdep_cfg;
168 bthl_reg_param_t reg_param;
169 int app_id;
170
Arnav Guptaceec8272016-02-10 05:57:49 +0530171 if (!sBluetoothHdpInterface) {
172 ALOGE("Failed to register health app. No Bluetooth Health Interface available");
173 return -1;
174 }
Matthew Xie676cb1b2012-03-22 17:32:29 -0700175
176 mdep_cfg.mdep_role = (bthl_mdep_role_t) role;
177 mdep_cfg.data_type = data_type;
178 mdep_cfg.channel_type = (bthl_channel_type_t) channel_type;
179 // TODO(BT) pass all the followings in from java instead of reuse name
180 mdep_cfg.mdep_description = env->GetStringUTFChars(name, NULL);
181 reg_param.application_name = env->GetStringUTFChars(name, NULL);
182 reg_param.provider_name = NULL;
183 reg_param.srv_name = NULL;
184 reg_param.srv_desp = NULL;
185 reg_param.number_of_mdeps = 1;
186 reg_param.mdep_cfg = &mdep_cfg;
187
188 if ( (status = sBluetoothHdpInterface->register_application(&reg_param, &app_id)) !=
189 BT_STATUS_SUCCESS) {
Matthew Xiec55a9832012-04-07 03:44:13 -0700190 ALOGE("Failed register health app, status: %d", status);
Matthew Xie676cb1b2012-03-22 17:32:29 -0700191 return -1;
192 }
193
194 env->ReleaseStringUTFChars(name, mdep_cfg.mdep_description);
195 env->ReleaseStringUTFChars(name, reg_param.application_name);
196 return app_id;
197}
198
199static jboolean unregisterHealthAppNative(JNIEnv *env, jobject object, int app_id) {
200 bt_status_t status;
201 if (!sBluetoothHdpInterface) return JNI_FALSE;
202
203 if ((status = sBluetoothHdpInterface->unregister_application(app_id)) != BT_STATUS_SUCCESS) {
Matthew Xiec55a9832012-04-07 03:44:13 -0700204 ALOGE("Failed to unregister app %d, status: %d", app_id, status);
Matthew Xie676cb1b2012-03-22 17:32:29 -0700205 }
206 return (status == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE;
207}
208
209static jint connectChannelNative(JNIEnv *env, jobject object,
210 jbyteArray address, jint app_id) {
211 bt_status_t status;
212 jbyte *addr;
213 jint chan_id;
214 if (!sBluetoothHdpInterface) return -1;
215
216 addr = env->GetByteArrayElements(address, NULL);
217 if (!addr) {
Matthew Xiec55a9832012-04-07 03:44:13 -0700218 ALOGE("Bluetooth device address null");
Matthew Xie676cb1b2012-03-22 17:32:29 -0700219 return -1;
220 }
221
222 if ( (status = sBluetoothHdpInterface->connect_channel(app_id, (bt_bdaddr_t *) addr,
223 0, &chan_id)) !=
224 BT_STATUS_SUCCESS) {
Matthew Xiec55a9832012-04-07 03:44:13 -0700225 ALOGE("Failed HDP channel connection, status: %d", status);
Matthew Xie676cb1b2012-03-22 17:32:29 -0700226 chan_id = -1;
227 }
228 env->ReleaseByteArrayElements(address, addr, 0);
229
230 return chan_id;
231}
232
233static jboolean disconnectChannelNative(JNIEnv *env, jobject object, jint channel_id) {
234 bt_status_t status;
235 if (!sBluetoothHdpInterface) return JNI_FALSE;
236
237 if ( (status = sBluetoothHdpInterface->destroy_channel(channel_id)) !=
238 BT_STATUS_SUCCESS) {
Matthew Xiec55a9832012-04-07 03:44:13 -0700239 ALOGE("Failed disconnect health channel, status: %d", status);
Matthew Xie676cb1b2012-03-22 17:32:29 -0700240 return JNI_FALSE;
241 }
242 return JNI_TRUE;
243}
244
245static JNINativeMethod sMethods[] = {
246 {"classInitNative", "()V", (void *) classInitNative},
fredc6654f5c2012-04-12 00:18:52 -0700247 {"initializeNative", "()V", (void *) initializeNative},
248 {"cleanupNative", "()V", (void *) cleanupNative},
Matthew Xie676cb1b2012-03-22 17:32:29 -0700249 {"registerHealthAppNative", "(IILjava/lang/String;I)I", (void *) registerHealthAppNative},
250 {"unregisterHealthAppNative", "(I)Z", (void *) unregisterHealthAppNative},
251 {"connectChannelNative", "([BI)I", (void *) connectChannelNative},
252 {"disconnectChannelNative", "(I)Z", (void *) disconnectChannelNative},
Matthew Xie676cb1b2012-03-22 17:32:29 -0700253};
254
255int register_com_android_bluetooth_hdp(JNIEnv* env)
256{
257 return jniRegisterNativeMethods(env, "com/android/bluetooth/hdp/HealthService",
258 sMethods, NELEM(sMethods));
259}
260
261}