1 /******************************************************************************
2 *
3 * Copyright (C) 2016 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 #include "base/pending_task.h"
20 #include "base/time/time.h"
21 #include "bta_closure_int.h"
22 #include "bta_sys.h"
23
24 using base::PendingTask;
25 using base::TaskQueue;
26 using base::TimeTicks;
27
28 namespace {
29
30 enum {
31 /* these events are handled by the state machine */
32 BTA_CLOSURE_EXECUTE_EVT = BTA_SYS_EVT_START(BTA_ID_CLOSURE),
33 };
34
35 struct tBTA_CLOSURE_EXECUTE {
36 BT_HDR hdr;
37 PendingTask pending_task;
38 };
39
40 static const tBTA_SYS_REG bta_closure_hw_reg = {bta_closure_execute, NULL};
41 tBTA_SYS_SENDMSG bta_closure_sys_sendmsg = NULL;
42
43 } // namespace
44
45 /* Accept bta_sys_register, and bta_sys_sendmsg. Those parameters can be used to
46 * override system methods for tests.
47 */
bta_closure_init(tBTA_SYS_REGISTER registerer,tBTA_SYS_SENDMSG sender)48 void bta_closure_init(tBTA_SYS_REGISTER registerer, tBTA_SYS_SENDMSG sender) {
49 /* register closure message handler */
50 registerer(BTA_ID_CLOSURE, &bta_closure_hw_reg);
51 bta_closure_sys_sendmsg = sender;
52 }
53
bta_closure_execute(BT_HDR * p_raw_msg)54 bool bta_closure_execute(BT_HDR* p_raw_msg) {
55 if (p_raw_msg->event != BTA_CLOSURE_EXECUTE_EVT) {
56 APPL_TRACE_ERROR("%s: don't know how to execute event type %d", __func__,
57 p_raw_msg->event);
58 return false;
59 }
60
61 tBTA_CLOSURE_EXECUTE* p_msg = ((tBTA_CLOSURE_EXECUTE*)p_raw_msg);
62
63 APPL_TRACE_API("%s: executing closure %s", __func__,
64 p_msg->pending_task.posted_from.ToString().c_str());
65 p_msg->pending_task.task.Run();
66
67 p_msg->pending_task.~PendingTask();
68 return true;
69 }
70
71 /*
72 * This function posts a closure for execution on the btu_bta_msg_queue. Please
73 * see documentation at
74 * https://www.chromium.org/developers/coding-style/important-abstractions-and-data-structures
75 * for how to handle dynamic memory ownership/smart pointers with base::Owned(),
76 * base::Passed(), base::ConstRef() and others.
77 */
do_in_bta_thread(const tracked_objects::Location & from_here,const base::Closure & task)78 void do_in_bta_thread(const tracked_objects::Location& from_here,
79 const base::Closure& task) {
80 APPL_TRACE_API("%s: posting %s", __func__, from_here.ToString().c_str());
81 tBTA_CLOSURE_EXECUTE* p_msg =
82 (tBTA_CLOSURE_EXECUTE*)osi_malloc(sizeof(tBTA_CLOSURE_EXECUTE));
83
84 new (&p_msg->pending_task) PendingTask(from_here, task, TimeTicks(), true);
85 p_msg->hdr.event = BTA_CLOSURE_EXECUTE_EVT;
86 bta_closure_sys_sendmsg(p_msg);
87 }
88