1 /*
2 * Copyright 2023 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 "btif/include/btif_jni_task.h"
18
19 #include <base/functional/bind.h>
20 #include <base/location.h>
21 #include <base/threading/platform_thread.h>
22 #include <bluetooth/log.h>
23
24 #include <cstdint>
25 #include <utility>
26
27 #include "common/message_loop_thread.h"
28 #include "common/postable_context.h"
29 #include "include/hardware/bluetooth.h"
30 #include "osi/include/allocator.h"
31
32 /* BTIF Events */
33 #define BT_EVT_BTIF 0xA000
34 #define BT_EVT_CONTEXT_SWITCH_EVT (0x0001 | BT_EVT_BTIF)
35
36 using base::PlatformThread;
37 using namespace bluetooth;
38
39 static bluetooth::common::MessageLoopThread jni_thread("bt_jni_thread");
40
jni_thread_startup()41 void jni_thread_startup() { jni_thread.StartUp(); }
42
jni_thread_shutdown()43 void jni_thread_shutdown() { jni_thread.ShutDown(); }
44
45 /*******************************************************************************
46 *
47 * Function btif_task
48 *
49 * Description BTIF task handler managing all messages being passed
50 * Bluetooth HAL and BTA.
51 *
52 * Returns void
53 *
54 ******************************************************************************/
bt_jni_msg_ready(void * context)55 static void bt_jni_msg_ready(void* context) {
56 tBTIF_CONTEXT_SWITCH_CBACK* p = (tBTIF_CONTEXT_SWITCH_CBACK*)context;
57 if (p->p_cb) p->p_cb(p->event, p->p_param);
58 osi_free(p);
59 }
60
61 /*******************************************************************************
62 *
63 * Function btif_transfer_context
64 *
65 * Description This function switches context to btif task
66 *
67 * p_cback : callback used to process message in btif context
68 * event : event id of message
69 * p_params : parameter area passed to callback (copied)
70 * param_len : length of parameter area
71 * p_copy_cback : If set this function will be invoked for deep
72 * copy
73 *
74 * Returns void
75 *
76 ******************************************************************************/
77
btif_transfer_context(tBTIF_CBACK * p_cback,uint16_t event,char * p_params,int param_len,tBTIF_COPY_CBACK * p_copy_cback)78 bt_status_t btif_transfer_context(tBTIF_CBACK* p_cback, uint16_t event,
79 char* p_params, int param_len,
80 tBTIF_COPY_CBACK* p_copy_cback) {
81 tBTIF_CONTEXT_SWITCH_CBACK* p_msg = (tBTIF_CONTEXT_SWITCH_CBACK*)osi_malloc(
82 sizeof(tBTIF_CONTEXT_SWITCH_CBACK) + param_len);
83
84 log::verbose("btif_transfer_context event {}, len {}", event, param_len);
85
86 /* allocate and send message that will be executed in btif context */
87 p_msg->hdr.event = BT_EVT_CONTEXT_SWITCH_EVT; /* internal event */
88 p_msg->p_cb = p_cback;
89
90 p_msg->event = event; /* callback event */
91
92 /* check if caller has provided a copy callback to do the deep copy */
93 if (p_copy_cback) {
94 p_copy_cback(event, p_msg->p_param, p_params);
95 } else if (p_params) {
96 memcpy(p_msg->p_param, p_params, param_len); /* callback parameter data */
97 }
98
99 return do_in_jni_thread(base::BindOnce(&bt_jni_msg_ready, p_msg));
100 }
101
102 /**
103 * This function posts a task into the btif message loop, that executes it in
104 * the JNI message loop.
105 **/
do_in_jni_thread(base::OnceClosure task)106 bt_status_t do_in_jni_thread(base::OnceClosure task) {
107 if (!jni_thread.DoInThread(FROM_HERE, std::move(task))) {
108 log::error("Post task to task runner failed!");
109 return BT_STATUS_JNI_THREAD_ATTACH_ERROR;
110 }
111 return BT_STATUS_SUCCESS;
112 }
113
is_on_jni_thread()114 bool is_on_jni_thread() {
115 return jni_thread.GetThreadId() == PlatformThread::CurrentId();
116 }
117
do_post_on_bt_jni(BtJniClosure closure)118 static void do_post_on_bt_jni(BtJniClosure closure) { closure(); }
119
post_on_bt_jni(BtJniClosure closure)120 void post_on_bt_jni(BtJniClosure closure) {
121 log::assert_that(
122 do_in_jni_thread(base::BindOnce(do_post_on_bt_jni, std::move(closure))) ==
123 BT_STATUS_SUCCESS,
124 "assert failed: do_in_jni_thread("
125 "base::BindOnce(do_post_on_bt_jni, std::move(closure))) == "
126 "BT_STATUS_SUCCESS");
127 }
128
get_jni()129 bluetooth::common::PostableContext* get_jni() { return jni_thread.Postable(); }
130