Zhihai Xu | c1c259c | 2013-03-14 11:51:06 -0700 | [diff] [blame] | 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 "BluetoothAvrcpServiceJni" |
| 18 | |
| 19 | #define LOG_NDEBUG 0 |
| 20 | |
| 21 | #include "com_android_bluetooth.h" |
| 22 | #include "hardware/bt_rc.h" |
| 23 | #include "utils/Log.h" |
| 24 | #include "android_runtime/AndroidRuntime.h" |
| 25 | |
| 26 | #include <string.h> |
| 27 | |
| 28 | namespace android { |
John Du | 1767590 | 2013-07-18 15:48:16 -0700 | [diff] [blame] | 29 | static jmethodID method_getRcFeatures; |
Zhihai Xu | c1c259c | 2013-03-14 11:51:06 -0700 | [diff] [blame] | 30 | static jmethodID method_getPlayStatus; |
broodplank | 0c58930 | 2013-12-29 05:41:41 +0100 | [diff] [blame] | 31 | static jmethodID method_onListPlayerAttributeRequest; |
Zhihai Xu | c1c259c | 2013-03-14 11:51:06 -0700 | [diff] [blame] | 32 | static jmethodID method_getElementAttr; |
| 33 | static jmethodID method_registerNotification; |
John Du | 1767590 | 2013-07-18 15:48:16 -0700 | [diff] [blame] | 34 | static jmethodID method_volumeChangeCallback; |
John Du | ace834f | 2013-06-27 18:39:42 -0700 | [diff] [blame] | 35 | static jmethodID method_handlePassthroughCmd; |
broodplank | 0c58930 | 2013-12-29 05:41:41 +0100 | [diff] [blame] | 36 | static jmethodID method_getFolderItems; |
| 37 | static jmethodID method_setAddressedPlayer; |
| 38 | static jmethodID method_onListPlayerAttributeValues; |
| 39 | static jmethodID method_onGetPlayerAttributeValues; |
| 40 | static jmethodID method_setPlayerAppSetting; |
| 41 | static jmethodID method_getplayerattribute_text; |
| 42 | static jmethodID method_getplayervalue_text; |
Zhihai Xu | c1c259c | 2013-03-14 11:51:06 -0700 | [diff] [blame] | 43 | |
| 44 | static const btrc_interface_t *sBluetoothAvrcpInterface = NULL; |
| 45 | static jobject mCallbacksObj = NULL; |
| 46 | static JNIEnv *sCallbackEnv = NULL; |
| 47 | |
| 48 | static bool checkCallbackThread() { |
| 49 | // Always fetch the latest callbackEnv from AdapterService. |
| 50 | // Caching this could cause this sCallbackEnv to go out-of-sync |
| 51 | // with the AdapterService's ENV if an ASSOCIATE/DISASSOCIATE event |
| 52 | // is received |
| 53 | sCallbackEnv = getCallbackEnv(); |
| 54 | |
| 55 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 56 | if (sCallbackEnv != env || sCallbackEnv == NULL) return false; |
| 57 | return true; |
| 58 | } |
| 59 | |
John Du | 1767590 | 2013-07-18 15:48:16 -0700 | [diff] [blame] | 60 | static void btavrcp_remote_features_callback(bt_bdaddr_t* bd_addr, btrc_remote_features_t features) { |
| 61 | ALOGI("%s", __FUNCTION__); |
| 62 | jbyteArray addr; |
| 63 | |
| 64 | if (!checkCallbackThread()) { |
| 65 | ALOGE("Callback: '%s' is not called on the correct thread", __FUNCTION__); |
| 66 | return; |
| 67 | } |
| 68 | addr = sCallbackEnv->NewByteArray(sizeof(bt_bdaddr_t)); |
| 69 | if (!addr) { |
| 70 | ALOGE("Fail to new jbyteArray bd addr for connection state"); |
| 71 | checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__); |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | sCallbackEnv->SetByteArrayRegion(addr, 0, sizeof(bt_bdaddr_t), (jbyte*) bd_addr); |
| 76 | sCallbackEnv->CallVoidMethod(mCallbacksObj, method_getRcFeatures, addr, (jint)features); |
| 77 | checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__); |
| 78 | } |
| 79 | |
Zhihai Xu | c1c259c | 2013-03-14 11:51:06 -0700 | [diff] [blame] | 80 | static void btavrcp_get_play_status_callback() { |
| 81 | ALOGI("%s", __FUNCTION__); |
| 82 | |
| 83 | if (!checkCallbackThread()) { |
| 84 | ALOGE("Callback: '%s' is not called on the correct thread", __FUNCTION__); |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | sCallbackEnv->CallVoidMethod(mCallbacksObj, method_getPlayStatus); |
| 89 | checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__); |
| 90 | } |
| 91 | |
broodplank | 0c58930 | 2013-12-29 05:41:41 +0100 | [diff] [blame] | 92 | static void btavrcp_get_player_seeting_value_callback(btrc_player_attr_t player_att) { |
| 93 | ALOGI("%s", __FUNCTION__); |
| 94 | if (!checkCallbackThread()) { |
| 95 | ALOGE("Callback: '%s' is not called on the correct thread", __FUNCTION__); |
| 96 | return; |
| 97 | } |
| 98 | sCallbackEnv->CallVoidMethod(mCallbacksObj ,method_onListPlayerAttributeValues , (jbyte)player_att ); |
| 99 | checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__); |
| 100 | } |
| 101 | |
| 102 | static void btavrcp_get_player_attribute_id_callback() { |
| 103 | ALOGI("%s", __FUNCTION__); |
| 104 | if (!checkCallbackThread()) { |
| 105 | ALOGE("Callback: '%s' is not called on the correct thread", __FUNCTION__); |
| 106 | return; |
| 107 | } |
| 108 | sCallbackEnv->CallVoidMethod(mCallbacksObj,method_onListPlayerAttributeRequest ); |
| 109 | checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__); |
| 110 | } |
| 111 | |
| 112 | static void btavrcp_getcurrent_player_app_setting_values( uint8_t num_attr, |
| 113 | btrc_player_attr_t *p_attrs) { |
| 114 | jintArray attrs; |
| 115 | ALOGI("%s", __FUNCTION__); |
| 116 | if (!checkCallbackThread()) { |
| 117 | ALOGE("Callback: '%s' is not called on the correct thread", __FUNCTION__); |
| 118 | return; |
| 119 | } |
| 120 | attrs = (jintArray)sCallbackEnv->NewIntArray(num_attr); |
| 121 | if (!attrs) { |
| 122 | ALOGE("Fail to new jintArray for attrs"); |
| 123 | checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__); |
| 124 | return; |
| 125 | } |
| 126 | sCallbackEnv->SetIntArrayRegion(attrs, 0, num_attr, (jint *)p_attrs); |
| 127 | sCallbackEnv->CallVoidMethod(mCallbacksObj,method_onGetPlayerAttributeValues, |
| 128 | (jbyte)num_attr,attrs ); |
| 129 | checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__); |
| 130 | sCallbackEnv->DeleteLocalRef(attrs); |
| 131 | } |
| 132 | |
| 133 | static void btavrcp_set_playerapp_setting_value_callback(btrc_player_settings_t *attr) |
| 134 | { |
| 135 | jbyteArray attrs_ids; |
| 136 | jbyteArray attrs_value; |
| 137 | ALOGI("%s", __FUNCTION__); |
| 138 | if (!checkCallbackThread()) { |
| 139 | ALOGE("Callback: '%s' is not called on the correct thread", __FUNCTION__); |
| 140 | return; |
| 141 | } |
| 142 | attrs_ids = (jbyteArray)sCallbackEnv->NewByteArray(attr->num_attr); |
| 143 | if (!attrs_ids) { |
| 144 | ALOGE("Fail to new jintArray for attrs"); |
| 145 | checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__); |
| 146 | return; |
| 147 | } |
| 148 | sCallbackEnv->SetByteArrayRegion(attrs_ids, 0, attr->num_attr, (jbyte *)attr->attr_ids); |
| 149 | attrs_value = (jbyteArray)sCallbackEnv->NewByteArray(attr->num_attr); |
| 150 | if (!attrs_value) { |
| 151 | ALOGE("Fail to new jintArray for attrs"); |
| 152 | checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__); |
| 153 | return; |
| 154 | } |
| 155 | sCallbackEnv->SetByteArrayRegion(attrs_value, 0, attr->num_attr, (jbyte *)attr->attr_values); |
| 156 | sCallbackEnv->CallVoidMethod(mCallbacksObj, method_setPlayerAppSetting, (jbyte)attr->num_attr, |
| 157 | attrs_ids ,attrs_value ); |
| 158 | sCallbackEnv->DeleteLocalRef(attrs_ids); |
| 159 | sCallbackEnv->DeleteLocalRef(attrs_value); |
| 160 | } |
| 161 | |
| 162 | static void btavrcp_getPlayer_app_attribute_text(uint8_t num , btrc_player_attr_t *att) |
| 163 | { |
| 164 | jbyteArray attrs; |
| 165 | ALOGI("%s", __FUNCTION__); |
| 166 | if (!checkCallbackThread()) { |
| 167 | ALOGE("Callback: '%s' is not called on the correct thread", __FUNCTION__); |
| 168 | return; |
| 169 | } |
| 170 | attrs = (jbyteArray)sCallbackEnv->NewByteArray(num); |
| 171 | if (!attrs) { |
| 172 | ALOGE("Fail to new jintArray for attrs"); |
| 173 | checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__); |
| 174 | return; |
| 175 | } |
| 176 | sCallbackEnv->SetByteArrayRegion(attrs, 0, num, (jbyte *)att); |
| 177 | sCallbackEnv->CallVoidMethod(mCallbacksObj, method_getplayerattribute_text,(jbyte) num ,attrs ); |
| 178 | sCallbackEnv->DeleteLocalRef(attrs); |
| 179 | } |
| 180 | |
| 181 | static void btavrcp_getPlayer_app_value_text(uint8_t attr_id , uint8_t num_val , uint8_t *value) |
| 182 | { |
| 183 | jbyteArray Attr_Value ; |
| 184 | ALOGI("%s", __FUNCTION__); |
| 185 | if (!checkCallbackThread()) { |
| 186 | ALOGE("Callback: '%s' is not called on the correct thread", __FUNCTION__); |
| 187 | return; |
| 188 | } |
| 189 | Attr_Value = (jbyteArray)sCallbackEnv->NewByteArray(num_val); |
| 190 | if (!Attr_Value) { |
| 191 | ALOGE("Fail to new jintArray for attrs"); |
| 192 | checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__); |
| 193 | return; |
| 194 | } |
| 195 | sCallbackEnv->SetByteArrayRegion(Attr_Value, 0, num_val, (jbyte *)value); |
| 196 | sCallbackEnv->CallVoidMethod(mCallbacksObj, method_getplayervalue_text,(jbyte) attr_id, |
| 197 | (jbyte) num_val , Attr_Value ); |
| 198 | sCallbackEnv->DeleteLocalRef(Attr_Value); |
| 199 | } |
| 200 | |
Zhihai Xu | c1c259c | 2013-03-14 11:51:06 -0700 | [diff] [blame] | 201 | static void btavrcp_get_element_attr_callback(uint8_t num_attr, btrc_media_attr_t *p_attrs) { |
| 202 | jintArray attrs; |
| 203 | |
| 204 | ALOGI("%s", __FUNCTION__); |
| 205 | |
| 206 | if (!checkCallbackThread()) { |
| 207 | ALOGE("Callback: '%s' is not called on the correct thread", __FUNCTION__); |
| 208 | return; |
| 209 | } |
| 210 | attrs = (jintArray)sCallbackEnv->NewIntArray(num_attr); |
| 211 | if (!attrs) { |
| 212 | ALOGE("Fail to new jintArray for attrs"); |
| 213 | checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__); |
| 214 | return; |
| 215 | } |
| 216 | sCallbackEnv->SetIntArrayRegion(attrs, 0, num_attr, (jint *)p_attrs); |
| 217 | sCallbackEnv->CallVoidMethod(mCallbacksObj, method_getElementAttr, (jbyte)num_attr, attrs); |
| 218 | checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__); |
| 219 | sCallbackEnv->DeleteLocalRef(attrs); |
| 220 | } |
| 221 | |
| 222 | static void btavrcp_register_notification_callback(btrc_event_id_t event_id, uint32_t param) { |
| 223 | ALOGI("%s", __FUNCTION__); |
| 224 | |
| 225 | if (!checkCallbackThread()) { |
| 226 | ALOGE("Callback: '%s' is not called on the correct thread", __FUNCTION__); |
| 227 | return; |
| 228 | } |
| 229 | |
| 230 | sCallbackEnv->CallVoidMethod(mCallbacksObj, method_registerNotification, |
| 231 | (jint)event_id, (jint)param); |
| 232 | checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__); |
| 233 | } |
| 234 | |
John Du | 1767590 | 2013-07-18 15:48:16 -0700 | [diff] [blame] | 235 | static void btavrcp_volume_change_callback(uint8_t volume, uint8_t ctype) { |
| 236 | ALOGI("%s", __FUNCTION__); |
| 237 | |
| 238 | if (!checkCallbackThread()) { |
| 239 | ALOGE("Callback: '%s' is not called on the correct thread", __FUNCTION__); |
| 240 | return; |
| 241 | } |
| 242 | |
| 243 | sCallbackEnv->CallVoidMethod(mCallbacksObj, method_volumeChangeCallback, (jint)volume, |
| 244 | (jint)ctype); |
| 245 | checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__); |
| 246 | } |
| 247 | |
broodplank | 0c58930 | 2013-12-29 05:41:41 +0100 | [diff] [blame] | 248 | |
| 249 | static void btavrcp_get_folder_items_callback(btrc_browse_folderitem_t scope , btrc_getfolderitem_t *param) { |
| 250 | jint start = param->start_item; |
| 251 | jint end = param->end_item; |
| 252 | jint size = param->size; |
| 253 | |
| 254 | ALOGI("%s", __FUNCTION__); |
| 255 | ALOGI("scope: %d", scope); |
| 256 | ALOGI("start entry: %d", start); |
| 257 | ALOGI("end entry: %d", end); |
| 258 | ALOGI("size: %d", size); |
| 259 | |
| 260 | if (!checkCallbackThread()) { |
| 261 | ALOGE("Callback: '%s' is not called on the correct thread", __FUNCTION__); |
| 262 | return; |
| 263 | } |
| 264 | |
| 265 | sCallbackEnv->CallVoidMethod(mCallbacksObj, method_getFolderItems, (jbyte)scope, |
| 266 | start, end, size); |
| 267 | checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__); |
| 268 | } |
John Du | ace834f | 2013-06-27 18:39:42 -0700 | [diff] [blame] | 269 | static void btavrcp_passthrough_command_callback(int id, int pressed) { |
| 270 | ALOGI("%s", __FUNCTION__); |
| 271 | |
| 272 | if (!checkCallbackThread()) { |
| 273 | ALOGE("Callback: '%s' is not called on the correct thread", __FUNCTION__); |
| 274 | return; |
| 275 | } |
| 276 | |
| 277 | sCallbackEnv->CallVoidMethod(mCallbacksObj, method_handlePassthroughCmd, (jint)id, |
| 278 | (jint)pressed); |
| 279 | checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__); |
| 280 | } |
| 281 | |
broodplank | 0c58930 | 2013-12-29 05:41:41 +0100 | [diff] [blame] | 282 | static void btavrcp_set_addressed_player_callback(uint32_t player_id) { |
| 283 | ALOGI("%s", __FUNCTION__); |
| 284 | ALOGI("player id: %d", player_id); |
| 285 | |
| 286 | if (!checkCallbackThread()) { |
| 287 | ALOGE("Callback: '%s' is not called on the correct thread", __FUNCTION__); |
| 288 | return; |
| 289 | } |
| 290 | |
| 291 | sCallbackEnv->CallVoidMethod(mCallbacksObj, method_setAddressedPlayer, (jint)player_id); |
| 292 | |
| 293 | checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__); |
| 294 | } |
Zhihai Xu | c1c259c | 2013-03-14 11:51:06 -0700 | [diff] [blame] | 295 | static btrc_callbacks_t sBluetoothAvrcpCallbacks = { |
| 296 | sizeof(sBluetoothAvrcpCallbacks), |
John Du | 1767590 | 2013-07-18 15:48:16 -0700 | [diff] [blame] | 297 | btavrcp_remote_features_callback, |
Zhihai Xu | c1c259c | 2013-03-14 11:51:06 -0700 | [diff] [blame] | 298 | btavrcp_get_play_status_callback, |
broodplank | 0c58930 | 2013-12-29 05:41:41 +0100 | [diff] [blame] | 299 | btavrcp_get_player_attribute_id_callback, |
| 300 | btavrcp_get_player_seeting_value_callback, |
| 301 | btavrcp_getcurrent_player_app_setting_values, |
| 302 | btavrcp_getPlayer_app_attribute_text, |
| 303 | btavrcp_getPlayer_app_value_text, |
| 304 | btavrcp_set_playerapp_setting_value_callback, |
Zhihai Xu | c1c259c | 2013-03-14 11:51:06 -0700 | [diff] [blame] | 305 | btavrcp_get_element_attr_callback, |
John Du | ace834f | 2013-06-27 18:39:42 -0700 | [diff] [blame] | 306 | btavrcp_register_notification_callback, |
John Du | 1767590 | 2013-07-18 15:48:16 -0700 | [diff] [blame] | 307 | btavrcp_volume_change_callback, |
broodplank | 0c58930 | 2013-12-29 05:41:41 +0100 | [diff] [blame] | 308 | btavrcp_passthrough_command_callback, |
| 309 | btavrcp_get_folder_items_callback, |
| 310 | btavrcp_set_addressed_player_callback |
Zhihai Xu | c1c259c | 2013-03-14 11:51:06 -0700 | [diff] [blame] | 311 | }; |
| 312 | |
| 313 | static void classInitNative(JNIEnv* env, jclass clazz) { |
John Du | 1767590 | 2013-07-18 15:48:16 -0700 | [diff] [blame] | 314 | method_getRcFeatures = |
| 315 | env->GetMethodID(clazz, "getRcFeatures", "([BI)V"); |
Zhihai Xu | c1c259c | 2013-03-14 11:51:06 -0700 | [diff] [blame] | 316 | method_getPlayStatus = |
| 317 | env->GetMethodID(clazz, "getPlayStatus", "()V"); |
broodplank | 0c58930 | 2013-12-29 05:41:41 +0100 | [diff] [blame] | 318 | method_onListPlayerAttributeRequest = |
| 319 | env->GetMethodID(clazz , "onListPlayerAttributeRequest" , "()V"); |
| 320 | method_onListPlayerAttributeValues = |
| 321 | env->GetMethodID(clazz , "onListPlayerAttributeValues" , "(B)V"); |
Zhihai Xu | c1c259c | 2013-03-14 11:51:06 -0700 | [diff] [blame] | 322 | method_getElementAttr = |
| 323 | env->GetMethodID(clazz, "getElementAttr", "(B[I)V"); |
broodplank | 0c58930 | 2013-12-29 05:41:41 +0100 | [diff] [blame] | 324 | method_setPlayerAppSetting = |
| 325 | env->GetMethodID(clazz, "setPlayerAppSetting","(B[B[B)V"); |
| 326 | method_getplayerattribute_text = |
| 327 | env->GetMethodID(clazz, "getplayerattribute_text" , "(B[B)V"); |
| 328 | method_getplayervalue_text = |
| 329 | env->GetMethodID(clazz, "getplayervalue_text" , "(BB[B)V"); |
Zhihai Xu | c1c259c | 2013-03-14 11:51:06 -0700 | [diff] [blame] | 330 | method_registerNotification = |
| 331 | env->GetMethodID(clazz, "registerNotification", "(II)V"); |
broodplank | 0c58930 | 2013-12-29 05:41:41 +0100 | [diff] [blame] | 332 | method_onGetPlayerAttributeValues = |
| 333 | env->GetMethodID(clazz, "onGetPlayerAttributeValues", "(B[I)V"); |
John Du | 1767590 | 2013-07-18 15:48:16 -0700 | [diff] [blame] | 334 | method_volumeChangeCallback = |
| 335 | env->GetMethodID(clazz, "volumeChangeCallback", "(II)V"); |
| 336 | |
John Du | ace834f | 2013-06-27 18:39:42 -0700 | [diff] [blame] | 337 | method_handlePassthroughCmd = |
| 338 | env->GetMethodID(clazz, "handlePassthroughCmd", "(II)V"); |
| 339 | |
broodplank | 0c58930 | 2013-12-29 05:41:41 +0100 | [diff] [blame] | 340 | //setAddressedPlayer: attributes to pass: Player ID |
| 341 | method_setAddressedPlayer = |
| 342 | env->GetMethodID(clazz, "setAddressedPlayer", "(I)V"); |
| 343 | |
| 344 | //getFolderItems: attributes to pass: Scope, Start, End, Attr Cnt |
| 345 | method_getFolderItems = |
| 346 | env->GetMethodID(clazz, "getFolderItems", "(BIII)V"); |
| 347 | |
Zhihai Xu | c1c259c | 2013-03-14 11:51:06 -0700 | [diff] [blame] | 348 | ALOGI("%s: succeeds", __FUNCTION__); |
| 349 | } |
| 350 | |
| 351 | static void initNative(JNIEnv *env, jobject object) { |
| 352 | const bt_interface_t* btInf; |
| 353 | bt_status_t status; |
| 354 | |
| 355 | if ( (btInf = getBluetoothInterface()) == NULL) { |
| 356 | ALOGE("Bluetooth module is not loaded"); |
| 357 | return; |
| 358 | } |
| 359 | |
| 360 | if (sBluetoothAvrcpInterface !=NULL) { |
| 361 | ALOGW("Cleaning up Avrcp Interface before initializing..."); |
| 362 | sBluetoothAvrcpInterface->cleanup(); |
| 363 | sBluetoothAvrcpInterface = NULL; |
| 364 | } |
| 365 | |
| 366 | if (mCallbacksObj != NULL) { |
| 367 | ALOGW("Cleaning up Avrcp callback object"); |
| 368 | env->DeleteGlobalRef(mCallbacksObj); |
| 369 | mCallbacksObj = NULL; |
| 370 | } |
| 371 | |
| 372 | if ( (sBluetoothAvrcpInterface = (btrc_interface_t *) |
| 373 | btInf->get_profile_interface(BT_PROFILE_AV_RC_ID)) == NULL) { |
| 374 | ALOGE("Failed to get Bluetooth Avrcp Interface"); |
| 375 | return; |
| 376 | } |
| 377 | |
| 378 | if ( (status = sBluetoothAvrcpInterface->init(&sBluetoothAvrcpCallbacks)) != |
| 379 | BT_STATUS_SUCCESS) { |
| 380 | ALOGE("Failed to initialize Bluetooth Avrcp, status: %d", status); |
| 381 | sBluetoothAvrcpInterface = NULL; |
| 382 | return; |
| 383 | } |
| 384 | |
| 385 | mCallbacksObj = env->NewGlobalRef(object); |
| 386 | } |
| 387 | |
| 388 | static void cleanupNative(JNIEnv *env, jobject object) { |
| 389 | const bt_interface_t* btInf; |
| 390 | |
| 391 | if ( (btInf = getBluetoothInterface()) == NULL) { |
| 392 | ALOGE("Bluetooth module is not loaded"); |
| 393 | return; |
| 394 | } |
| 395 | |
| 396 | if (sBluetoothAvrcpInterface !=NULL) { |
| 397 | sBluetoothAvrcpInterface->cleanup(); |
| 398 | sBluetoothAvrcpInterface = NULL; |
| 399 | } |
| 400 | |
| 401 | if (mCallbacksObj != NULL) { |
| 402 | env->DeleteGlobalRef(mCallbacksObj); |
| 403 | mCallbacksObj = NULL; |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | static jboolean getPlayStatusRspNative(JNIEnv *env, jobject object, jint playStatus, |
| 408 | jint songLen, jint songPos) { |
| 409 | bt_status_t status; |
| 410 | |
| 411 | ALOGI("%s: sBluetoothAvrcpInterface: %p", __FUNCTION__, sBluetoothAvrcpInterface); |
| 412 | if (!sBluetoothAvrcpInterface) return JNI_FALSE; |
| 413 | |
| 414 | if ((status = sBluetoothAvrcpInterface->get_play_status_rsp((btrc_play_status_t)playStatus, |
| 415 | songLen, songPos)) != BT_STATUS_SUCCESS) { |
| 416 | ALOGE("Failed get_play_status_rsp, status: %d", status); |
| 417 | } |
| 418 | |
| 419 | return (status == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE; |
| 420 | } |
| 421 | |
broodplank | 0c58930 | 2013-12-29 05:41:41 +0100 | [diff] [blame] | 422 | |
| 423 | static jboolean getListPlayerappAttrRspNative(JNIEnv *env ,jobject object , jbyte numAttr, |
| 424 | jbyteArray attrIds ) { |
| 425 | bt_status_t status; |
| 426 | btrc_player_attr_t *pAttrs = NULL; |
| 427 | int i; |
| 428 | jbyte *attr; |
| 429 | |
| 430 | if (!sBluetoothAvrcpInterface) return JNI_FALSE; |
| 431 | if( numAttr > BTRC_MAX_APP_ATTR_SIZE) { |
| 432 | ALOGE("get_element_attr_rsp: number of attributes exceed maximum"); |
| 433 | return JNI_FALSE; |
| 434 | } |
| 435 | ALOGI("getListPlayerappAttrRspNative"); |
| 436 | pAttrs = new btrc_player_attr_t[numAttr]; |
| 437 | if (!pAttrs) { |
| 438 | ALOGE("getListPlayerappAttrRspNative: not have enough memeory"); |
| 439 | return JNI_FALSE; |
| 440 | } |
| 441 | attr = env->GetByteArrayElements(attrIds, NULL); |
| 442 | if( !attr) { |
| 443 | delete[] pAttrs; |
| 444 | jniThrowIOException(env, EINVAL); |
| 445 | return JNI_FALSE ; |
| 446 | } |
| 447 | for (i = 0; i < numAttr; ++i) { |
| 448 | pAttrs[i] = (btrc_player_attr_t)attr[i]; |
| 449 | } |
| 450 | if (i < numAttr) { |
| 451 | delete[] pAttrs; |
| 452 | env->ReleaseByteArrayElements(attrIds, attr, 0); |
| 453 | return JNI_FALSE; |
| 454 | } |
| 455 | //Call Stack Method |
| 456 | if ((status = sBluetoothAvrcpInterface->list_player_app_attr_rsp(numAttr, pAttrs)) != |
| 457 | BT_STATUS_SUCCESS) { |
| 458 | ALOGE("Failed getelementattrrsp, status: %d", status); |
| 459 | } |
| 460 | delete[] pAttrs; |
| 461 | env->ReleaseByteArrayElements(attrIds, attr, 0); |
| 462 | return (status == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE; |
| 463 | } |
| 464 | |
| 465 | |
| 466 | static jboolean getPlayerAppValueRspNative(JNIEnv *env ,jobject object , jbyte numvalue, |
| 467 | jbyteArray value) |
| 468 | { |
| 469 | bt_status_t status; |
| 470 | uint8_t *pAttrs = NULL; |
| 471 | int i; |
| 472 | jbyte *attr; |
| 473 | |
| 474 | if( numvalue > BTRC_MAX_APP_ATTR_SIZE) { |
| 475 | ALOGE("get_element_attr_rsp: number of attributes exceed maximum"); |
| 476 | return JNI_FALSE; |
| 477 | } |
| 478 | pAttrs = new uint8_t[numvalue]; |
| 479 | if (!pAttrs) { |
| 480 | ALOGE("getPlayerAppValueRspNative: not have enough memeory"); |
| 481 | return JNI_FALSE; |
| 482 | } |
| 483 | attr = env->GetByteArrayElements(value, NULL); |
| 484 | if (!attr) { |
| 485 | jniThrowIOException(env, EINVAL); |
| 486 | return JNI_FALSE; |
| 487 | } |
| 488 | for (i = 0; i < numvalue; ++i) { |
| 489 | pAttrs[i] = (uint8_t)attr[i]; |
| 490 | } |
| 491 | if (i < numvalue) { |
| 492 | delete[] pAttrs; |
| 493 | env->ReleaseByteArrayElements(value, attr, 0); |
| 494 | return JNI_FALSE; |
| 495 | } |
| 496 | if ((status = sBluetoothAvrcpInterface->list_player_app_value_rsp(numvalue, pAttrs)) != |
| 497 | BT_STATUS_SUCCESS) { |
| 498 | ALOGE("Failed get_element_attr_rsp, status: %d", status); |
| 499 | } |
| 500 | delete[] pAttrs; |
| 501 | env->ReleaseByteArrayElements(value, attr, 0); |
| 502 | return (status == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE; |
| 503 | } |
| 504 | |
| 505 | static jboolean SendCurrentPlayerValueRspNative(JNIEnv *env, jobject object , |
| 506 | jbyte numattr ,jbyteArray value) { |
| 507 | btrc_player_settings_t *pAttrs = NULL ; |
| 508 | bt_status_t status; |
| 509 | int i; |
| 510 | jbyte *attr; |
| 511 | |
| 512 | if( numattr > BTRC_MAX_APP_ATTR_SIZE || numattr == 0) { |
| 513 | ALOGE("SendCurrentPlayerValueRspNative: number of attributes exceed maximum"); |
| 514 | return JNI_FALSE; |
| 515 | } |
| 516 | pAttrs = new btrc_player_settings_t; |
| 517 | if (!pAttrs) { |
| 518 | ALOGE("SendCurrentPlayerValueRspNative: not have enough memeory"); |
| 519 | return JNI_FALSE; |
| 520 | } |
| 521 | attr = env->GetByteArrayElements(value, NULL); |
| 522 | if (!attr) { |
| 523 | delete[] pAttrs; |
| 524 | jniThrowIOException(env, EINVAL); |
| 525 | return JNI_FALSE; |
| 526 | } |
| 527 | pAttrs->num_attr = numattr/2 ; |
| 528 | for(i =0 ; i < numattr; i+=2) |
| 529 | { |
| 530 | pAttrs->attr_ids[i/2] = attr[i]; |
| 531 | pAttrs->attr_values[i/2] = attr[i+1]; |
| 532 | } |
| 533 | if ((status = sBluetoothAvrcpInterface->get_player_app_value_rsp(pAttrs)) != |
| 534 | BT_STATUS_SUCCESS) { |
| 535 | ALOGE("Failed get_element_attr_rsp, status: %d", status); |
| 536 | } |
| 537 | delete[] pAttrs; |
| 538 | env->ReleaseByteArrayElements(value, attr, 0); |
| 539 | return (status == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE; |
| 540 | } |
| 541 | |
| 542 | |
| 543 | //JNI Method called to Respond to PDU 0x14 |
| 544 | static jboolean SendSetPlayerAppRspNative(JNIEnv *env, jobject object) |
| 545 | { |
| 546 | bt_status_t status; |
| 547 | btrc_status_t player_rsp = BTRC_STS_NO_ERROR; |
| 548 | if ((status = sBluetoothAvrcpInterface->set_player_app_value_rsp(player_rsp)) != |
| 549 | BT_STATUS_SUCCESS) { |
| 550 | ALOGE("Failed get_element_attr_rsp, status: %d", status); |
| 551 | } |
| 552 | return (status == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE; |
| 553 | } |
| 554 | |
| 555 | |
| 556 | //JNI Method Called to Respond to PDU 0x15 |
| 557 | static jboolean sendSettingsTextRspNative(JNIEnv *env, jobject object, jint num_attr, |
| 558 | jbyteArray attr,jint length , jobjectArray textArray ) { |
| 559 | btrc_player_setting_text_t *pAttrs = NULL; |
| 560 | bt_status_t status; |
| 561 | int i; |
| 562 | jstring text ; |
| 563 | const char* textStr; |
| 564 | jbyte *arr ; |
| 565 | //ALOGE("sendSettingsTextRspNative"); |
| 566 | if (!sBluetoothAvrcpInterface) return JNI_FALSE; |
| 567 | if (num_attr > BTRC_MAX_ELEM_ATTR_SIZE) { |
| 568 | ALOGE("get_element_attr_rsp: number of attributes exceed maximum"); |
| 569 | return JNI_FALSE; |
| 570 | } |
| 571 | pAttrs = new btrc_player_setting_text_t[num_attr]; |
| 572 | arr = env->GetByteArrayElements(attr, NULL); |
| 573 | if (!arr) { |
| 574 | delete[] pAttrs; |
| 575 | jniThrowIOException(env, EINVAL); |
| 576 | return JNI_FALSE; |
| 577 | } |
| 578 | for (i = 0; i < num_attr ; ++i) { |
| 579 | text = (jstring) env->GetObjectArrayElement(textArray, i); |
| 580 | textStr = env->GetStringUTFChars(text, NULL); |
| 581 | if (!textStr) { |
| 582 | ALOGE("get_element_attr_rsp: GetStringUTFChars return NULL"); |
| 583 | env->DeleteLocalRef(text); |
| 584 | break; |
| 585 | } |
| 586 | pAttrs[i].id = arr[i]; |
| 587 | if (strlen(textStr) >= BTRC_MAX_ATTR_STR_LEN) { |
| 588 | ALOGE("sendSettingsTextRspNative: string length exceed maximum"); |
| 589 | strncpy((char *)pAttrs[i].text, textStr, BTRC_MAX_ATTR_STR_LEN-1); |
| 590 | pAttrs[i].text[BTRC_MAX_ATTR_STR_LEN-1] = 0; |
| 591 | } else { |
| 592 | strcpy((char *)pAttrs[i].text, textStr); |
| 593 | } |
| 594 | //Check out if release need to be done in for loop |
| 595 | env->ReleaseStringUTFChars(text, textStr); |
| 596 | env->DeleteLocalRef(text); |
| 597 | } |
| 598 | //Call Stack Methos to Respond PDU 0x16 |
| 599 | if ((status = sBluetoothAvrcpInterface->get_player_app_attr_text_rsp(num_attr, pAttrs)) |
| 600 | != BT_STATUS_SUCCESS) { |
| 601 | ALOGE("Failed get_element_attr_rsp, status: %d", status); |
| 602 | } |
| 603 | delete[] pAttrs; |
| 604 | env->ReleaseByteArrayElements(attr, arr, 0); |
| 605 | return (status == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE; |
| 606 | } |
| 607 | |
| 608 | //JNI Method Called to respond to PDU 0x16 |
| 609 | static jboolean sendValueTextRspNative(JNIEnv *env, jobject object, jint num_attr, |
| 610 | jbyteArray attr, jint length , jobjectArray textArray ) { |
| 611 | btrc_player_setting_text_t *pAttrs = NULL; |
| 612 | bt_status_t status; |
| 613 | int i; |
| 614 | jstring text ; |
| 615 | const char* textStr; |
| 616 | jbyte *arr ; |
| 617 | |
| 618 | //ALOGE("sendValueTextRspNative"); |
| 619 | if (!sBluetoothAvrcpInterface) return JNI_FALSE; |
| 620 | if (num_attr > BTRC_MAX_ELEM_ATTR_SIZE) { |
| 621 | ALOGE("sendValueTextRspNative: number of attributes exceed maximum"); |
| 622 | return JNI_FALSE; |
| 623 | } |
| 624 | pAttrs = new btrc_player_setting_text_t[num_attr]; |
| 625 | arr = env->GetByteArrayElements(attr, NULL); |
| 626 | if (!arr) { |
| 627 | delete[] pAttrs; |
| 628 | jniThrowIOException(env, EINVAL); |
| 629 | return JNI_FALSE; |
| 630 | } |
| 631 | for (i = 0; i < num_attr ; ++i) { |
| 632 | text = (jstring) env->GetObjectArrayElement(textArray, i); |
| 633 | textStr = env->GetStringUTFChars(text, NULL); |
| 634 | if (!textStr) { |
| 635 | ALOGE("sendValueTextRspNative: GetStringUTFChars return NULL"); |
| 636 | env->DeleteLocalRef(text); |
| 637 | break; |
| 638 | } |
| 639 | pAttrs[i].id = arr[i]; |
| 640 | if (strlen(textStr) >= BTRC_MAX_ATTR_STR_LEN) { |
| 641 | ALOGE("sendValueTextRspNative: string length exceed maximum"); |
| 642 | strncpy((char *)pAttrs[i].text, textStr, BTRC_MAX_ATTR_STR_LEN-1); |
| 643 | pAttrs[i].text[BTRC_MAX_ATTR_STR_LEN-1] = 0; |
| 644 | } else { |
| 645 | strcpy((char *)pAttrs[i].text, textStr); |
| 646 | } |
| 647 | env->ReleaseStringUTFChars(text, textStr); |
| 648 | env->DeleteLocalRef(text); |
| 649 | } |
| 650 | //Call Stack Method to Respond to PDU 0x16 |
| 651 | if ((status = sBluetoothAvrcpInterface->get_player_app_value_text_rsp(num_attr, pAttrs)) |
| 652 | != BT_STATUS_SUCCESS) { |
| 653 | ALOGE("Failed get_element_attr_rsp, status: %d", status); |
| 654 | } |
| 655 | delete[] pAttrs; |
| 656 | env->ReleaseByteArrayElements(attr, arr, 0); |
| 657 | return (status == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE; |
| 658 | } |
| 659 | |
Zhihai Xu | c1c259c | 2013-03-14 11:51:06 -0700 | [diff] [blame] | 660 | static jboolean getElementAttrRspNative(JNIEnv *env, jobject object, jbyte numAttr, |
| 661 | jintArray attrIds, jobjectArray textArray) { |
| 662 | jint *attr; |
| 663 | bt_status_t status; |
| 664 | jstring text; |
| 665 | int i; |
| 666 | btrc_element_attr_val_t *pAttrs = NULL; |
| 667 | const char* textStr; |
| 668 | |
| 669 | if (!sBluetoothAvrcpInterface) return JNI_FALSE; |
| 670 | |
| 671 | if (numAttr > BTRC_MAX_ELEM_ATTR_SIZE) { |
| 672 | ALOGE("get_element_attr_rsp: number of attributes exceed maximum"); |
| 673 | return JNI_FALSE; |
| 674 | } |
| 675 | |
| 676 | pAttrs = new btrc_element_attr_val_t[numAttr]; |
| 677 | if (!pAttrs) { |
| 678 | ALOGE("get_element_attr_rsp: not have enough memeory"); |
| 679 | return JNI_FALSE; |
| 680 | } |
| 681 | |
| 682 | attr = env->GetIntArrayElements(attrIds, NULL); |
| 683 | if (!attr) { |
| 684 | delete[] pAttrs; |
| 685 | jniThrowIOException(env, EINVAL); |
| 686 | return JNI_FALSE; |
| 687 | } |
| 688 | |
| 689 | for (i = 0; i < numAttr; ++i) { |
| 690 | text = (jstring) env->GetObjectArrayElement(textArray, i); |
| 691 | textStr = env->GetStringUTFChars(text, NULL); |
| 692 | if (!textStr) { |
| 693 | ALOGE("get_element_attr_rsp: GetStringUTFChars return NULL"); |
| 694 | env->DeleteLocalRef(text); |
| 695 | break; |
| 696 | } |
Zhihai Xu | 8852134 | 2013-03-25 17:45:56 -0700 | [diff] [blame] | 697 | |
| 698 | pAttrs[i].attr_id = attr[i]; |
Zhihai Xu | c1c259c | 2013-03-14 11:51:06 -0700 | [diff] [blame] | 699 | if (strlen(textStr) >= BTRC_MAX_ATTR_STR_LEN) { |
| 700 | ALOGE("get_element_attr_rsp: string length exceed maximum"); |
Zhihai Xu | 8852134 | 2013-03-25 17:45:56 -0700 | [diff] [blame] | 701 | strncpy((char *)pAttrs[i].text, textStr, BTRC_MAX_ATTR_STR_LEN-1); |
| 702 | pAttrs[i].text[BTRC_MAX_ATTR_STR_LEN-1] = 0; |
| 703 | } else { |
| 704 | strcpy((char *)pAttrs[i].text, textStr); |
Zhihai Xu | c1c259c | 2013-03-14 11:51:06 -0700 | [diff] [blame] | 705 | } |
Zhihai Xu | c1c259c | 2013-03-14 11:51:06 -0700 | [diff] [blame] | 706 | env->ReleaseStringUTFChars(text, textStr); |
| 707 | env->DeleteLocalRef(text); |
| 708 | } |
| 709 | |
| 710 | if (i < numAttr) { |
| 711 | delete[] pAttrs; |
| 712 | env->ReleaseIntArrayElements(attrIds, attr, 0); |
| 713 | return JNI_FALSE; |
| 714 | } |
| 715 | |
| 716 | if ((status = sBluetoothAvrcpInterface->get_element_attr_rsp(numAttr, pAttrs)) != |
| 717 | BT_STATUS_SUCCESS) { |
| 718 | ALOGE("Failed get_element_attr_rsp, status: %d", status); |
| 719 | } |
| 720 | |
| 721 | delete[] pAttrs; |
| 722 | env->ReleaseIntArrayElements(attrIds, attr, 0); |
| 723 | return (status == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE; |
| 724 | } |
| 725 | |
broodplank | 0c58930 | 2013-12-29 05:41:41 +0100 | [diff] [blame] | 726 | static jboolean registerNotificationPlayerAppRspNative(JNIEnv *env, jobject object ,jint type, |
| 727 | jbyte numattr ,jbyteArray value) { |
| 728 | bt_status_t status; |
| 729 | int i; |
| 730 | jbyte *attr; |
| 731 | btrc_register_notification_t *param= NULL; |
| 732 | |
| 733 | if( numattr > BTRC_MAX_APP_ATTR_SIZE || numattr == 0) { |
| 734 | ALOGE("registerNotificationPlayerAppRspNative: number of attributes exceed maximum"); |
| 735 | return JNI_FALSE; |
| 736 | } |
| 737 | param = new btrc_register_notification_t; |
| 738 | |
| 739 | if (!param) { |
| 740 | ALOGE("registerNotificationPlayerAppRspNative: not have enough memeory"); |
| 741 | return JNI_FALSE; |
| 742 | } |
| 743 | attr = env->GetByteArrayElements(value, NULL); |
| 744 | if (!attr) { |
| 745 | delete[] param; |
| 746 | jniThrowIOException(env, EINVAL); |
| 747 | return JNI_FALSE; |
| 748 | } |
| 749 | param->player_setting.num_attr = numattr/2; |
| 750 | for(i =0 ; i < numattr; i+=2) |
| 751 | { |
| 752 | param->player_setting.attr_ids[i/2] = attr[i]; |
| 753 | param->player_setting.attr_values[i/2] = attr[i+1]; |
| 754 | } |
| 755 | //Call Stack Method |
| 756 | if ((status = sBluetoothAvrcpInterface->register_notification_rsp(BTRC_EVT_APP_SETTINGS_CHANGED, |
| 757 | (btrc_notification_type_t)type,param)) != |
| 758 | BT_STATUS_SUCCESS) { |
| 759 | ALOGE("Failed get_element_attr_rsp, status: %d", status); |
| 760 | } |
| 761 | delete[] param; |
| 762 | env->ReleaseByteArrayElements(value, attr, 0); |
| 763 | return (status == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE; |
| 764 | } |
Zhihai Xu | c1c259c | 2013-03-14 11:51:06 -0700 | [diff] [blame] | 765 | static jboolean registerNotificationRspPlayStatusNative(JNIEnv *env, jobject object, |
| 766 | jint type, jint playStatus) { |
| 767 | bt_status_t status; |
| 768 | btrc_register_notification_t param; |
| 769 | |
| 770 | ALOGI("%s: sBluetoothAvrcpInterface: %p", __FUNCTION__, sBluetoothAvrcpInterface); |
| 771 | if (!sBluetoothAvrcpInterface) return JNI_FALSE; |
| 772 | |
| 773 | param.play_status = (btrc_play_status_t)playStatus; |
| 774 | if ((status = sBluetoothAvrcpInterface->register_notification_rsp(BTRC_EVT_PLAY_STATUS_CHANGED, |
| 775 | (btrc_notification_type_t)type, ¶m)) != BT_STATUS_SUCCESS) { |
| 776 | ALOGE("Failed register_notification_rsp play status, status: %d", status); |
| 777 | } |
| 778 | |
| 779 | return (status == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE; |
| 780 | } |
| 781 | |
| 782 | static jboolean registerNotificationRspTrackChangeNative(JNIEnv *env, jobject object, |
| 783 | jint type, jbyteArray track) { |
| 784 | bt_status_t status; |
| 785 | btrc_register_notification_t param; |
| 786 | jbyte *trk; |
| 787 | int i; |
| 788 | |
| 789 | ALOGI("%s: sBluetoothAvrcpInterface: %p", __FUNCTION__, sBluetoothAvrcpInterface); |
| 790 | if (!sBluetoothAvrcpInterface) return JNI_FALSE; |
| 791 | |
| 792 | trk = env->GetByteArrayElements(track, NULL); |
| 793 | if (!trk) { |
| 794 | jniThrowIOException(env, EINVAL); |
| 795 | return JNI_FALSE; |
| 796 | } |
| 797 | |
| 798 | for (i = 0; i < BTRC_UID_SIZE; ++i) { |
| 799 | param.track[i] = trk[i]; |
| 800 | } |
| 801 | |
| 802 | if ((status = sBluetoothAvrcpInterface->register_notification_rsp(BTRC_EVT_TRACK_CHANGE, |
| 803 | (btrc_notification_type_t)type, ¶m)) != BT_STATUS_SUCCESS) { |
| 804 | ALOGE("Failed register_notification_rsp track change, status: %d", status); |
| 805 | } |
| 806 | |
| 807 | env->ReleaseByteArrayElements(track, trk, 0); |
| 808 | return (status == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE; |
| 809 | } |
| 810 | |
Zhihai Xu | aa1ffd5 | 2013-04-03 19:03:57 -0700 | [diff] [blame] | 811 | static jboolean registerNotificationRspPlayPosNative(JNIEnv *env, jobject object, |
| 812 | jint type, jint playPos) { |
| 813 | bt_status_t status; |
| 814 | btrc_register_notification_t param; |
| 815 | |
| 816 | ALOGI("%s: sBluetoothAvrcpInterface: %p", __FUNCTION__, sBluetoothAvrcpInterface); |
| 817 | if (!sBluetoothAvrcpInterface) return JNI_FALSE; |
| 818 | |
| 819 | param.song_pos = (uint32_t)playPos; |
| 820 | if ((status = sBluetoothAvrcpInterface->register_notification_rsp(BTRC_EVT_PLAY_POS_CHANGED, |
| 821 | (btrc_notification_type_t)type, ¶m)) != BT_STATUS_SUCCESS) { |
| 822 | ALOGE("Failed register_notification_rsp play position, status: %d", status); |
| 823 | } |
| 824 | |
| 825 | return (status == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE; |
| 826 | } |
| 827 | |
John Du | 1767590 | 2013-07-18 15:48:16 -0700 | [diff] [blame] | 828 | static jboolean setVolumeNative(JNIEnv *env, jobject object, jint volume) { |
| 829 | bt_status_t status; |
| 830 | |
| 831 | //TODO: delete test code |
| 832 | ALOGI("%s: jint: %d, uint8_t: %u", __FUNCTION__, volume, (uint8_t) volume); |
| 833 | |
| 834 | ALOGI("%s: sBluetoothAvrcpInterface: %p", __FUNCTION__, sBluetoothAvrcpInterface); |
| 835 | if (!sBluetoothAvrcpInterface) return JNI_FALSE; |
| 836 | |
| 837 | if ((status = sBluetoothAvrcpInterface->set_volume((uint8_t)volume)) != BT_STATUS_SUCCESS) { |
| 838 | ALOGE("Failed set_volume, status: %d", status); |
| 839 | } |
| 840 | |
| 841 | return (status == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE; |
| 842 | } |
| 843 | |
broodplank | 0c58930 | 2013-12-29 05:41:41 +0100 | [diff] [blame] | 844 | static jboolean registerNotificationRspAddressedPlayerChangedNative (JNIEnv *env, |
| 845 | jobject object, jint type, jint playerId) { |
| 846 | bt_status_t status; |
| 847 | btrc_register_notification_t param; |
| 848 | |
| 849 | ALOGI("%s: sBluetoothAvrcpInterface: %p", __FUNCTION__, sBluetoothAvrcpInterface); |
| 850 | ALOGI("playerId: %d", playerId); |
| 851 | if (!sBluetoothAvrcpInterface) return JNI_FALSE; |
| 852 | |
| 853 | param.player_id = (uint16_t)playerId; |
| 854 | if ((status = sBluetoothAvrcpInterface->register_notification_rsp(BTRC_EVT_ADDRESSED_PLAYER_CHANGED, |
| 855 | (btrc_notification_type_t)type, ¶m)) != BT_STATUS_SUCCESS) { |
| 856 | ALOGE("Failed registerNotificationRspAddressedPlayerChangedNative, status: %d", status); |
| 857 | } |
| 858 | |
| 859 | return (status == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE; |
| 860 | |
| 861 | } |
| 862 | |
| 863 | static jboolean registerNotificationRspAvailablePlayersChangedNative (JNIEnv *env, |
| 864 | jobject object, jint type) { |
| 865 | bt_status_t status; |
| 866 | btrc_register_notification_t param; |
| 867 | |
| 868 | ALOGI("%s: sBluetoothAvrcpInterface: %p", __FUNCTION__, sBluetoothAvrcpInterface); |
| 869 | if (!sBluetoothAvrcpInterface) return JNI_FALSE; |
| 870 | if ((status = sBluetoothAvrcpInterface->register_notification_rsp(BTRC_EVT_AVAILABLE_PLAYERS_CHANGED, |
| 871 | (btrc_notification_type_t)type, ¶m)) != BT_STATUS_SUCCESS) { |
| 872 | ALOGE("Failed registerNotificationRspAvailablePlayersChangedNative, status: %d", status); |
| 873 | } |
| 874 | |
| 875 | return (status == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE; |
| 876 | } |
| 877 | |
| 878 | // FolderItems are populated as byte stream from the apps |
| 879 | static jboolean getFolderItemsRspNative(JNIEnv *env, jobject object, jbyte statusCode, jint uidCounter, |
| 880 | jint itemCount, jbooleanArray folderItems, jintArray folderItemLengths) { |
| 881 | bt_status_t status; |
| 882 | uint8_t *folderElements; |
| 883 | int32_t *folderElementLengths; |
| 884 | int32_t count = 0; |
| 885 | int32_t countElementLength = 0; |
| 886 | int32_t countTotalBytes = 0; |
| 887 | int32_t countTemp = 0; |
| 888 | int32_t checkLength = 0; |
| 889 | btrc_folder_list_entries_t param; |
| 890 | |
| 891 | ALOGI("%s: sBluetoothAvrcpInterface: %p", __FUNCTION__, sBluetoothAvrcpInterface); |
| 892 | if (!sBluetoothAvrcpInterface) return JNI_FALSE; |
| 893 | |
| 894 | folderElements = env->GetBooleanArrayElements(folderItems, NULL); |
| 895 | if (!folderElements) { |
| 896 | jniThrowIOException(env, EINVAL); |
| 897 | return JNI_FALSE; |
| 898 | } |
| 899 | |
| 900 | folderElementLengths = env->GetIntArrayElements(folderItemLengths, NULL); |
| 901 | if (!folderElementLengths) { |
| 902 | jniThrowIOException(env, EINVAL); |
| 903 | return JNI_FALSE; |
| 904 | } |
| 905 | |
| 906 | param.status = statusCode; |
| 907 | param.uid_counter = uidCounter; |
| 908 | param.item_count = itemCount; |
| 909 | ALOGI("status: %d, item count: %d", param.status, param.item_count); |
| 910 | param.p_item_list = new btrc_folder_list_item_t[itemCount]; |
| 911 | ALOGI("Intermediate List entries:"); |
| 912 | for (; count < itemCount; count++) { |
| 913 | param.p_item_list[count].item_type = folderElements[countTotalBytes]; countTotalBytes++; |
| 914 | param.p_item_list[count].player.player_id = (uint16_t)(folderElements[countTotalBytes] & 0x00ff); countTotalBytes++; |
| 915 | param.p_item_list[count].player.player_id += (uint16_t)((folderElements[countTotalBytes] << 8) & 0xff00); countTotalBytes++; |
| 916 | param.p_item_list[count].player.major_type = folderElements[countTotalBytes]; countTotalBytes++; |
| 917 | param.p_item_list[count].player.sub_type = (uint32_t)(folderElements[countTotalBytes] & 0x000000ff); countTotalBytes++; |
| 918 | param.p_item_list[count].player.sub_type += (uint32_t)((folderElements[countTotalBytes] << 8) & 0x0000ff00); countTotalBytes++; |
| 919 | param.p_item_list[count].player.sub_type += (uint32_t)((folderElements[countTotalBytes] << 16) & 0x00ff0000); countTotalBytes++; |
| 920 | param.p_item_list[count].player.sub_type += (uint32_t)((folderElements[countTotalBytes] << 24) & 0xff000000); countTotalBytes++; |
| 921 | param.p_item_list[count].player.play_status = folderElements[countTotalBytes]; countTotalBytes++; |
| 922 | for (countTemp = 0; countTemp < 16; countTemp ++) { |
| 923 | param.p_item_list[count].player.features[countTemp] = folderElements[countTotalBytes]; countTotalBytes++; |
| 924 | } |
| 925 | param.p_item_list[count].player.name.charset_id = (uint16_t)(folderElements[countTotalBytes] & 0x00ff); countTotalBytes++; |
| 926 | param.p_item_list[count].player.name.charset_id += (uint16_t)((folderElements[countTotalBytes] << 8) & 0xff00); countTotalBytes++; |
| 927 | param.p_item_list[count].player.name.str_len = (uint16_t)(folderElements[countTotalBytes] & 0x00ff); countTotalBytes++; |
| 928 | param.p_item_list[count].player.name.str_len += (uint16_t)((folderElements[countTotalBytes] << 8) & 0xff00); countTotalBytes++; |
| 929 | param.p_item_list[count].player.name.p_str = new uint8_t[param.p_item_list[count].player.name.str_len]; |
| 930 | for (countTemp = 0; countTemp < param.p_item_list[count].player.name.str_len; countTemp ++) { |
| 931 | param.p_item_list[count].player.name.p_str[countTemp] = folderElements[countTotalBytes]; countTotalBytes++; |
| 932 | } |
| 933 | /*To check if byte feeding went well*/ |
| 934 | checkLength += folderElementLengths[count]; |
| 935 | if (checkLength != countTotalBytes) { |
| 936 | ALOGE("Error Populating Intermediate Folder Entry"); |
| 937 | ALOGE("checkLength = %u countTotalBytes = %u", checkLength, countTotalBytes); |
| 938 | } |
| 939 | ALOGI("entry: %u", count); |
| 940 | ALOGI("item type: %u", param.p_item_list[count].item_type); |
| 941 | ALOGI("player id: %u", param.p_item_list[count].player.player_id); |
| 942 | ALOGI("major type: %u", param.p_item_list[count].player.major_type); |
| 943 | ALOGI("sub type: %u", param.p_item_list[count].player.sub_type); |
| 944 | ALOGI("play status: %u", param.p_item_list[count].player.play_status); |
| 945 | ALOGI("features: "); |
| 946 | for (countTemp = 0; countTemp < 16; countTemp ++) |
| 947 | ALOGI("%u", param.p_item_list[count].player.features[countTemp]); |
| 948 | ALOGI("charset id: %u", param.p_item_list[count].player.name.charset_id); |
| 949 | ALOGI("name len: %u", param.p_item_list[count].player.name.str_len); |
| 950 | ALOGI("name: "); |
| 951 | for (countTemp = 0; countTemp < param.p_item_list[count].player.name.str_len; countTemp ++) { |
| 952 | ALOGI("%u", param.p_item_list[count].player.name.p_str[countTemp]); |
| 953 | } |
| 954 | } |
| 955 | |
| 956 | if ((status = sBluetoothAvrcpInterface->get_folder_items_rsp(¶m)) != BT_STATUS_SUCCESS) { |
| 957 | ALOGE("Failed getFolderItemsRspNative, status: %u", status); |
| 958 | } |
| 959 | |
| 960 | env->ReleaseBooleanArrayElements(folderItems, folderElements, 0); |
| 961 | env->ReleaseIntArrayElements(folderItemLengths, folderElementLengths, 0); |
| 962 | |
| 963 | return (status == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE; |
| 964 | } |
| 965 | |
| 966 | static jboolean setAdressedPlayerRspNative(JNIEnv *env, jobject object, jbyte statusCode) { |
| 967 | bt_status_t status; |
| 968 | |
| 969 | ALOGI("%s: sBluetoothAvrcpInterface: %p", __FUNCTION__, sBluetoothAvrcpInterface); |
| 970 | if (!sBluetoothAvrcpInterface) return JNI_FALSE; |
| 971 | |
| 972 | if ((status = sBluetoothAvrcpInterface->set_addressed_player_rsp((btrc_status_t)statusCode)) != BT_STATUS_SUCCESS) { |
| 973 | ALOGE("Failed setAdressedPlayerRspNative, status: %d", status); |
| 974 | } |
| 975 | |
| 976 | return (status == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE; |
| 977 | } |
Zhihai Xu | c1c259c | 2013-03-14 11:51:06 -0700 | [diff] [blame] | 978 | static JNINativeMethod sMethods[] = { |
| 979 | {"classInitNative", "()V", (void *) classInitNative}, |
| 980 | {"initNative", "()V", (void *) initNative}, |
| 981 | {"cleanupNative", "()V", (void *) cleanupNative}, |
| 982 | {"getPlayStatusRspNative", "(III)Z", (void *) getPlayStatusRspNative}, |
| 983 | {"getElementAttrRspNative", "(B[I[Ljava/lang/String;)Z", (void *) getElementAttrRspNative}, |
broodplank | 0c58930 | 2013-12-29 05:41:41 +0100 | [diff] [blame] | 984 | {"getListPlayerappAttrRspNative", "(B[B)Z", (void *)getListPlayerappAttrRspNative}, |
| 985 | {"getPlayerAppValueRspNative", "(B[B)Z", (void *)getPlayerAppValueRspNative}, |
Zhihai Xu | c1c259c | 2013-03-14 11:51:06 -0700 | [diff] [blame] | 986 | {"registerNotificationRspPlayStatusNative", "(II)Z", |
| 987 | (void *) registerNotificationRspPlayStatusNative}, |
broodplank | 0c58930 | 2013-12-29 05:41:41 +0100 | [diff] [blame] | 988 | {"SendCurrentPlayerValueRspNative", "(B[B)Z", |
| 989 | (void *)SendCurrentPlayerValueRspNative}, |
| 990 | {"registerNotificationPlayerAppRspNative", "(IB[B)Z", |
| 991 | (void *)registerNotificationPlayerAppRspNative}, |
Zhihai Xu | c1c259c | 2013-03-14 11:51:06 -0700 | [diff] [blame] | 992 | {"registerNotificationRspTrackChangeNative", "(I[B)Z", |
| 993 | (void *) registerNotificationRspTrackChangeNative}, |
broodplank | 0c58930 | 2013-12-29 05:41:41 +0100 | [diff] [blame] | 994 | {"SendSetPlayerAppRspNative", "()Z", |
| 995 | (void *) SendSetPlayerAppRspNative}, |
| 996 | {"sendSettingsTextRspNative" , "(I[BI[Ljava/lang/String;)Z", |
| 997 | (void *)sendSettingsTextRspNative}, |
Zhihai Xu | aa1ffd5 | 2013-04-03 19:03:57 -0700 | [diff] [blame] | 998 | {"registerNotificationRspPlayPosNative", "(II)Z", |
| 999 | (void *) registerNotificationRspPlayPosNative}, |
John Du | 1767590 | 2013-07-18 15:48:16 -0700 | [diff] [blame] | 1000 | {"setVolumeNative", "(I)Z", |
broodplank | 0c58930 | 2013-12-29 05:41:41 +0100 | [diff] [blame] | 1001 | (void *) setVolumeNative}, |
| 1002 | {"setAdressedPlayerRspNative", "(B)Z", |
| 1003 | (void *) setAdressedPlayerRspNative}, |
| 1004 | {"getFolderItemsRspNative", "(BII[B[I)Z", |
| 1005 | (void *) getFolderItemsRspNative}, |
| 1006 | {"registerNotificationRspAddressedPlayerChangedNative", "(II)Z", |
| 1007 | (void *) registerNotificationRspAddressedPlayerChangedNative}, |
| 1008 | {"registerNotificationRspAvailablePlayersChangedNative", "(I)Z", |
| 1009 | (void *) registerNotificationRspAvailablePlayersChangedNative}, |
Zhihai Xu | c1c259c | 2013-03-14 11:51:06 -0700 | [diff] [blame] | 1010 | }; |
| 1011 | |
| 1012 | int register_com_android_bluetooth_avrcp(JNIEnv* env) |
| 1013 | { |
| 1014 | return jniRegisterNativeMethods(env, "com/android/bluetooth/a2dp/Avrcp", |
| 1015 | sMethods, NELEM(sMethods)); |
| 1016 | } |
| 1017 | |
| 1018 | } |