1 /******************************************************************************
2 *
3 * Copyright (c) 2014 The Android Open Source Project
4 * Copyright 2009-2012 Broadcom Corporation
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 ******************************************************************************/
19
20 #ifndef BTIF_COMMON_H
21 #define BTIF_COMMON_H
22
23 #include <stdlib.h>
24
25 #include <base/bind.h>
26 #include <base/location.h>
27 #include <base/message_loop/message_loop.h>
28 #include <hardware/bluetooth.h>
29
30 #include "bt_types.h"
31 #include "bta/include/bta_api.h"
32 #include "osi/include/log.h"
33 #include "osi/include/osi.h"
34
35 /*******************************************************************************
36 * Constants & Macros
37 ******************************************************************************/
38
39 #define ASSERTC(cond, msg, val) \
40 do { \
41 if (!(cond)) { \
42 LOG_ERROR("### ASSERT : %s %s line %d %s (%d) ###", __FILE__, __func__, \
43 __LINE__, (msg), (val)); \
44 } \
45 } while (0)
46
47 /* Calculate start of event enumeration; id is top 8 bits of event */
48 #define BTIF_SIG_START(id) ((id) << 8)
49
50 /* For upstream the MSB bit is always SET */
51 #define BTIF_SIG_CB_BIT (0x8000)
52 #define BTIF_SIG_CB_START(id) (((id) << 8) | BTIF_SIG_CB_BIT)
53
54 /*
55 * A memcpy(3) wrapper when copying memory that might not be aligned.
56 *
57 * On certain architectures, if the memcpy(3) arguments appear to be
58 * pointing to aligned memory (e.g., struct pointers), the compiler might
59 * generate optimized memcpy(3) code. However, if the original memory was not
60 * aligned (e.g., because of incorrect "char *" to struct pointer casting),
61 * the result code might trigger SIGBUS crash.
62 *
63 * As a short-term solution, we use the help of the maybe_non_aligned_memcpy()
64 * macro to identify and fix such cases. In the future, we should fix the
65 * problematic "char *" to struct pointer casting, and this macro itself should
66 * be removed.
67 */
68 #define maybe_non_aligned_memcpy(_a, _b, _c) \
69 memcpy((void*)(_a), (void*)(_b), (_c))
70
71 /* BTIF sub-systems */
72 #define BTIF_CORE 0
73 #define BTIF_DM 1
74 #define BTIF_HFP 2
75 #define BTIF_AV 3
76 #define BTIF_PAN 4
77 #define BTIF_HF_CLIENT 5
78
79 extern bt_callbacks_t* bt_hal_cbacks;
80
81 #define HAL_CBACK(P_CB, P_CBACK, ...) \
82 do { \
83 if ((P_CB) && (P_CB)->P_CBACK) { \
84 BTIF_TRACE_API("%s: HAL %s->%s", __func__, #P_CB, #P_CBACK); \
85 (P_CB)->P_CBACK(__VA_ARGS__); \
86 } else { \
87 ASSERTC(0, "Callback is NULL", 0); \
88 } \
89 } while (0)
90
91 /**
92 * BTIF events for requests that require context switch to btif task
93 * on downstreams path
94 */
95 enum {
96 BTIF_CORE_API_START = BTIF_SIG_START(BTIF_CORE),
97 BTIF_CORE_STORAGE_NO_ACTION,
98 BTIF_CORE_STORAGE_ADAPTER_WRITE,
99 BTIF_CORE_STORAGE_ADAPTER_READ,
100 BTIF_CORE_STORAGE_ADAPTER_READ_ALL,
101 BTIF_CORE_STORAGE_REMOTE_WRITE,
102 BTIF_CORE_STORAGE_REMOTE_READ,
103 BTIF_CORE_STORAGE_REMOTE_READ_ALL,
104 BTIF_CORE_STORAGE_READ_ALL,
105 BTIF_CORE_STORAGE_NOTIFY_STATUS,
106 /* add here */
107
108 BTIF_DM_API_START = BTIF_SIG_START(BTIF_DM),
109 BTIF_DM_ENABLE_SERVICE,
110 BTIF_DM_DISABLE_SERVICE,
111 /* add here */
112
113 BTIF_HFP_API_START = BTIF_SIG_START(BTIF_HFP),
114 /* add here */
115
116 BTIF_AV_API_START = BTIF_SIG_START(BTIF_AV),
117 /* add here */
118 };
119
120 /**
121 * BTIF events for callbacks that require context switch to btif task
122 * on upstream path - Typically these would be non-BTA events
123 * that are generated by the BTIF layer.
124 */
125 enum {
126 BTIF_CORE_CB_START = BTIF_SIG_CB_START(BTIF_CORE),
127 /* add here */
128
129 BTIF_DM_CB_START = BTIF_SIG_CB_START(BTIF_DM),
130 BTIF_DM_CB_DISCOVERY_STARTED, /* Discovery has started */
131 BTIF_DM_CB_CREATE_BOND, /* Create bond */
132 BTIF_DM_CB_REMOVE_BOND, /*Remove bond */
133 BTIF_DM_CB_HID_REMOTE_NAME, /* Remote name callback for HID device */
134 BTIF_DM_CB_BOND_STATE_BONDING,
135 BTIF_DM_CB_LE_TX_TEST, /* BLE Tx Test command complete callback */
136 BTIF_DM_CB_LE_RX_TEST, /* BLE Rx Test command complete callback */
137 BTIF_DM_CB_LE_TEST_END, /* BLE Test mode end callback */
138
139 BTIF_HFP_CB_START = BTIF_SIG_CB_START(BTIF_HFP),
140 BTIF_HFP_CB_AUDIO_CONNECTING, /* HF AUDIO connect has been sent to BTA
141 successfully */
142
143 BTIF_PAN_CB_START = BTIF_SIG_CB_START(BTIF_PAN),
144 BTIF_PAN_CB_DISCONNECTING, /* PAN Disconnect has been sent to BTA successfully
145 */
146
147 BTIF_HF_CLIENT_CLIENT_CB_START = BTIF_SIG_CB_START(BTIF_HF_CLIENT),
148 BTIF_HF_CLIENT_CB_AUDIO_CONNECTING, /* AUDIO connect has been sent to BTA
149 successfully */
150 };
151
152 /*******************************************************************************
153 * Type definitions for callback functions
154 ******************************************************************************/
155
156 typedef void(tBTIF_CBACK)(uint16_t event, char* p_param);
157 typedef void(tBTIF_COPY_CBACK)(uint16_t event, char* p_dest, char* p_src);
158
159 /*******************************************************************************
160 * Type definitions and return values
161 ******************************************************************************/
162
163 /* this type handles all btif context switches between BTU and HAL */
164 typedef struct {
165 BT_HDR hdr;
166 tBTIF_CBACK* p_cb; /* context switch callback */
167
168 /* parameters passed to callback */
169 uint16_t event; /* message event id */
170 char __attribute__((aligned)) p_param[]; /* parameter area needs to be last */
171 } tBTIF_CONTEXT_SWITCH_CBACK;
172
173 /*******************************************************************************
174 * Functions
175 ******************************************************************************/
176
177 extern bt_status_t do_in_jni_thread(base::OnceClosure task);
178 extern bt_status_t do_in_jni_thread(const base::Location& from_here,
179 base::OnceClosure task);
180 extern bool is_on_jni_thread();
181 extern base::MessageLoop* get_jni_message_loop();
182 /**
183 * This template wraps callback into callback that will be executed on jni
184 * thread
185 */
186 template <typename R, typename... Args>
jni_thread_wrapper(const base::Location & from_here,base::Callback<R (Args...)> cb)187 base::Callback<R(Args...)> jni_thread_wrapper(const base::Location& from_here,
188 base::Callback<R(Args...)> cb) {
189 return base::Bind(
190 [](const base::Location& from_here, base::Callback<R(Args...)> cb,
191 Args... args) {
192 do_in_jni_thread(from_here,
193 base::Bind(cb, std::forward<Args>(args)...));
194 },
195 from_here, std::move(cb));
196 }
197
198 tBTA_SERVICE_MASK btif_get_enabled_services_mask(void);
199 bt_status_t btif_enable_service(tBTA_SERVICE_ID service_id);
200 bt_status_t btif_disable_service(tBTA_SERVICE_ID service_id);
201 int btif_is_enabled(void);
202
203 /**
204 * BTIF_Events
205 */
206 void btif_enable_bluetooth_evt(tBTA_STATUS status);
207 void btif_disable_bluetooth_evt(void);
208 void btif_adapter_properties_evt(bt_status_t status, uint32_t num_props,
209 bt_property_t* p_props);
210 void btif_remote_properties_evt(bt_status_t status, RawAddress* remote_addr,
211 uint32_t num_props, bt_property_t* p_props);
212
213 void bte_load_did_conf(const char* p_path);
214 void bte_main_boot_entry(void);
215 void bte_main_enable(void);
216 void bte_main_disable(void);
217 void bte_main_cleanup(void);
218 void bte_main_postload_cfg(void);
219
220 bt_status_t btif_transfer_context(tBTIF_CBACK* p_cback, uint16_t event,
221 char* p_params, int param_len,
222 tBTIF_COPY_CBACK* p_copy_cback);
223
224 void btif_init_ok(UNUSED_ATTR uint16_t event, UNUSED_ATTR char* p_param);
225
226 #endif /* BTIF_COMMON_H */
227