1 /******************************************************************************
2  *
3  *  Copyright (c) 2014 The Android Open Source Project
4  *  Copyright (C) 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/tracked_objects.h>
27 #include <hardware/bluetooth.h>
28 
29 #include "bt_types.h"
30 #include "bta_api.h"
31 #include "osi/include/log.h"
32 #include "osi/include/osi.h"
33 
34 /*******************************************************************************
35  *  Constants & Macros
36  ******************************************************************************/
37 
38 #define ASSERTC(cond, msg, val)                                              \
39   do {                                                                       \
40     if (!(cond)) {                                                           \
41       LOG_ERROR(LOG_TAG, "### ASSERT : %s %s line %d %s (%d) ###", __FILE__, \
42                 __func__, __LINE__, (msg), (val));                           \
43     }                                                                        \
44   } while (0)
45 
46 /* Calculate start of event enumeration; id is top 8 bits of event */
47 #define BTIF_SIG_START(id) ((id) << 8)
48 
49 /* For upstream the MSB bit is always SET */
50 #define BTIF_SIG_CB_BIT (0x8000)
51 #define BTIF_SIG_CB_START(id) (((id) << 8) | BTIF_SIG_CB_BIT)
52 
53 /*
54  * A memcpy(3) wrapper when copying memory that might not be aligned.
55  *
56  * On certain architectures, if the memcpy(3) arguments appear to be
57  * pointing to aligned memory (e.g., struct pointers), the compiler might
58  * generate optimized memcpy(3) code. However, if the original memory was not
59  * aligned (e.g., because of incorrect "char *" to struct pointer casting),
60  * the result code might trigger SIGBUS crash.
61  *
62  * As a short-term solution, we use the help of the maybe_non_aligned_memcpy()
63  * macro to identify and fix such cases. In the future, we should fix the
64  * problematic "char *" to struct pointer casting, and this macro itself should
65  * be removed.
66  */
67 #define maybe_non_aligned_memcpy(_a, _b, _c) \
68   memcpy((void*)(_a), (void*)(_b), (_c))
69 
70 /* BTIF sub-systems */
71 #define BTIF_CORE 0
72 #define BTIF_DM 1
73 #define BTIF_HFP 2
74 #define BTIF_AV 3
75 #define BTIF_PAN 4
76 #define BTIF_HF_CLIENT 5
77 
78 extern bt_callbacks_t* bt_hal_cbacks;
79 
80 #define HAL_CBACK(P_CB, P_CBACK, ...)                \
81   do {                                               \
82     if ((P_CB) && (P_CB)->P_CBACK) {                 \
83       BTIF_TRACE_API("HAL %s->%s", #P_CB, #P_CBACK); \
84       (P_CB)->P_CBACK(__VA_ARGS__);                  \
85     } else {                                         \
86       ASSERTC(0, "Callback is NULL", 0);             \
87     }                                                \
88   } while (0)
89 
90 /**
91  * BTIF events for requests that require context switch to btif task
92  * on downstreams path
93  */
94 enum {
95   BTIF_CORE_API_START = BTIF_SIG_START(BTIF_CORE),
96   BTIF_CORE_STORAGE_NO_ACTION,
97   BTIF_CORE_STORAGE_ADAPTER_WRITE,
98   BTIF_CORE_STORAGE_ADAPTER_READ,
99   BTIF_CORE_STORAGE_ADAPTER_READ_ALL,
100   BTIF_CORE_STORAGE_REMOTE_WRITE,
101   BTIF_CORE_STORAGE_REMOTE_READ,
102   BTIF_CORE_STORAGE_REMOTE_READ_ALL,
103   BTIF_CORE_STORAGE_READ_ALL,
104   BTIF_CORE_STORAGE_NOTIFY_STATUS,
105   /* add here */
106 
107   BTIF_DM_API_START = BTIF_SIG_START(BTIF_DM),
108   BTIF_DM_ENABLE_SERVICE,
109   BTIF_DM_DISABLE_SERVICE,
110   /* add here */
111 
112   BTIF_HFP_API_START = BTIF_SIG_START(BTIF_HFP),
113   /* add here */
114 
115   BTIF_AV_API_START = BTIF_SIG_START(BTIF_AV),
116   /* add here */
117 };
118 
119 /**
120  * BTIF events for callbacks that require context switch to btif task
121  * on upstream path - Typically these would be non-BTA events
122  * that are generated by the BTIF layer.
123  */
124 enum {
125   BTIF_CORE_CB_START = BTIF_SIG_CB_START(BTIF_CORE),
126   /* add here */
127 
128   BTIF_DM_CB_START = BTIF_SIG_CB_START(BTIF_DM),
129   BTIF_DM_CB_DISCOVERY_STARTED, /* Discovery has started */
130   BTIF_DM_CB_CREATE_BOND,       /* Create bond */
131   BTIF_DM_CB_REMOVE_BOND,       /*Remove bond */
132   BTIF_DM_CB_HID_REMOTE_NAME,   /* Remote name callback for HID device */
133   BTIF_DM_CB_BOND_STATE_BONDING,
134   BTIF_DM_CB_LE_TX_TEST,  /* BLE Tx Test command complete callback */
135   BTIF_DM_CB_LE_RX_TEST,  /* BLE Rx Test command complete callback */
136   BTIF_DM_CB_LE_TEST_END, /* BLE Test mode end callback */
137 
138   BTIF_HFP_CB_START = BTIF_SIG_CB_START(BTIF_HFP),
139   BTIF_HFP_CB_AUDIO_CONNECTING, /* HF AUDIO connect has been sent to BTA
140                                    successfully */
141 
142   BTIF_PAN_CB_START = BTIF_SIG_CB_START(BTIF_PAN),
143   BTIF_PAN_CB_DISCONNECTING, /* PAN Disconnect has been sent to BTA successfully
144                                 */
145 
146   BTIF_HF_CLIENT_CLIENT_CB_START = BTIF_SIG_CB_START(BTIF_HF_CLIENT),
147   BTIF_HF_CLIENT_CB_AUDIO_CONNECTING, /* AUDIO connect has been sent to BTA
148                                          successfully */
149 };
150 
151 /* Macro definitions for BD ADDR persistence */
152 
153 /**
154  * PROPERTY_BT_BDADDR_PATH
155  * The property key stores the storage location of Bluetooth Device Address
156  */
157 #ifndef PROPERTY_BT_BDADDR_PATH
158 #define PROPERTY_BT_BDADDR_PATH "ro.bt.bdaddr_path"
159 #endif
160 
161 /**
162  * PERSIST_BDADDR_PROPERTY
163  * If there is no valid bdaddr available from PROPERTY_BT_BDADDR_PATH,
164  * generating a random BDADDR and keeping it in the PERSIST_BDADDR_DROP.
165  */
166 #ifndef PERSIST_BDADDR_PROPERTY
167 #define PERSIST_BDADDR_PROPERTY "persist.service.bdroid.bdaddr"
168 #endif
169 
170 /**
171  * FACTORY_BT_BDADDR_PROPERTY
172  * If there is no valid bdaddr available from PROPERTY_BT_BDADDR_PATH
173  * and there is no available persistent bdaddr available from
174  * PERSIST_BDADDR_PROPERTY use a factory set address
175  */
176 #ifndef FACTORY_BT_ADDR_PROPERTY
177 #define FACTORY_BT_ADDR_PROPERTY "ro.boot.btmacaddr"
178 #endif
179 
180 #define FACTORY_BT_BDADDR_STORAGE_LEN 17
181 
182 /*******************************************************************************
183  *  Type definitions for callback functions
184  ******************************************************************************/
185 
186 typedef void(tBTIF_CBACK)(uint16_t event, char* p_param);
187 typedef void(tBTIF_COPY_CBACK)(uint16_t event, char* p_dest, char* p_src);
188 
189 /*******************************************************************************
190  *  Type definitions and return values
191  ******************************************************************************/
192 
193 /* this type handles all btif context switches between BTU and HAL */
194 typedef struct {
195   BT_HDR hdr;
196   tBTIF_CBACK* p_cb; /* context switch callback */
197 
198   /* parameters passed to callback */
199   uint16_t event;                          /* message event id */
200   char __attribute__((aligned)) p_param[]; /* parameter area needs to be last */
201 } tBTIF_CONTEXT_SWITCH_CBACK;
202 
203 /*******************************************************************************
204  *  Functions
205  ******************************************************************************/
206 
207 extern bt_status_t do_in_jni_thread(const base::Closure& task);
208 extern bt_status_t do_in_jni_thread(const tracked_objects::Location& from_here,
209                                     const base::Closure& task);
210 /**
211  * This template wraps callback into callback that will be executed on jni
212  * thread
213  */
214 template <typename R, typename... Args>
jni_thread_wrapper(const tracked_objects::Location & from_here,base::Callback<R (Args...)> cb)215 base::Callback<R(Args...)> jni_thread_wrapper(
216     const tracked_objects::Location& from_here, base::Callback<R(Args...)> cb) {
217   return base::Bind(
218       [](const tracked_objects::Location& from_here,
219          base::Callback<R(Args...)> cb, Args... args) {
220         do_in_jni_thread(from_here,
221                          base::Bind(cb, std::forward<Args>(args)...));
222       },
223       from_here, std::move(cb));
224 }
225 
226 tBTA_SERVICE_MASK btif_get_enabled_services_mask(void);
227 bt_status_t btif_enable_service(tBTA_SERVICE_ID service_id);
228 bt_status_t btif_disable_service(tBTA_SERVICE_ID service_id);
229 int btif_is_enabled(void);
230 
231 /**
232  * BTIF_Events
233  */
234 void btif_enable_bluetooth_evt(tBTA_STATUS status);
235 void btif_disable_bluetooth_evt(void);
236 void btif_adapter_properties_evt(bt_status_t status, uint32_t num_props,
237                                  bt_property_t* p_props);
238 void btif_remote_properties_evt(bt_status_t status, bt_bdaddr_t* remote_addr,
239                                 uint32_t num_props, bt_property_t* p_props);
240 
241 void bte_load_did_conf(const char* p_path);
242 void bte_main_boot_entry(void);
243 void bte_main_enable(void);
244 void bte_main_disable(void);
245 void bte_main_cleanup(void);
246 void bte_main_postload_cfg(void);
247 
248 bt_status_t btif_transfer_context(tBTIF_CBACK* p_cback, uint16_t event,
249                                   char* p_params, int param_len,
250                                   tBTIF_COPY_CBACK* p_copy_cback);
251 
252 void btif_init_ok(UNUSED_ATTR uint16_t event, UNUSED_ATTR char* p_param);
253 
254 #endif /* BTIF_COMMON_H */
255