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) {
58     p->p_cb(p->event, p->p_param);
59   }
60   osi_free(p);
61 }
62 
63 /*******************************************************************************
64  *
65  * Function         btif_transfer_context
66  *
67  * Description      This function switches context to btif task
68  *
69  *                  p_cback   : callback used to process message in btif context
70  *                  event     : event id of message
71  *                  p_params  : parameter area passed to callback (copied)
72  *                  param_len : length of parameter area
73  *                  p_copy_cback : If set this function will be invoked for deep
74  *                                 copy
75  *
76  * Returns          void
77  *
78  ******************************************************************************/
79 
btif_transfer_context(tBTIF_CBACK * p_cback,uint16_t event,char * p_params,int param_len,tBTIF_COPY_CBACK * p_copy_cback)80 bt_status_t btif_transfer_context(tBTIF_CBACK* p_cback, uint16_t event, char* p_params,
81                                   int param_len, tBTIF_COPY_CBACK* p_copy_cback) {
82   tBTIF_CONTEXT_SWITCH_CBACK* p_msg =
83           (tBTIF_CONTEXT_SWITCH_CBACK*)osi_malloc(sizeof(tBTIF_CONTEXT_SWITCH_CBACK) + param_len);
84 
85   log::verbose("btif_transfer_context event {}, len {}", event, param_len);
86 
87   /* allocate and send message that will be executed in btif context */
88   p_msg->hdr.event = BT_EVT_CONTEXT_SWITCH_EVT; /* internal event */
89   p_msg->p_cb = p_cback;
90 
91   p_msg->event = event; /* callback event */
92 
93   /* check if caller has provided a copy callback to do the deep copy */
94   if (p_copy_cback) {
95     p_copy_cback(event, p_msg->p_param, p_params);
96   } else if (p_params) {
97     memcpy(p_msg->p_param, p_params, param_len); /* callback parameter data */
98   }
99 
100   return do_in_jni_thread(base::BindOnce(&bt_jni_msg_ready, p_msg));
101 }
102 
103 /**
104  * This function posts a task into the btif message loop, that executes it in
105  * the JNI message loop.
106  **/
do_in_jni_thread(base::OnceClosure task)107 bt_status_t do_in_jni_thread(base::OnceClosure task) {
108   if (!jni_thread.DoInThread(FROM_HERE, std::move(task))) {
109     log::error("Post task to task runner failed!");
110     return BT_STATUS_JNI_THREAD_ATTACH_ERROR;
111   }
112   return BT_STATUS_SUCCESS;
113 }
114 
is_on_jni_thread()115 bool is_on_jni_thread() { return jni_thread.GetThreadId() == PlatformThread::CurrentId(); }
116 
do_post_on_bt_jni(BtJniClosure closure)117 static void do_post_on_bt_jni(BtJniClosure closure) { closure(); }
118 
post_on_bt_jni(BtJniClosure closure)119 void post_on_bt_jni(BtJniClosure closure) {
120   log::assert_that(do_in_jni_thread(base::BindOnce(do_post_on_bt_jni, std::move(closure))) ==
121                            BT_STATUS_SUCCESS,
122                    "assert failed: do_in_jni_thread("
123                    "base::BindOnce(do_post_on_bt_jni, std::move(closure))) == "
124                    "BT_STATUS_SUCCESS");
125 }
126 
get_jni()127 bluetooth::common::PostableContext* get_jni() { return jni_thread.Postable(); }
128