• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /*******************************************************************************
152  *  Type definitions for callback functions
153  ******************************************************************************/
154 
155 typedef void(tBTIF_CBACK)(uint16_t event, char* p_param);
156 typedef void(tBTIF_COPY_CBACK)(uint16_t event, char* p_dest, char* p_src);
157 
158 /*******************************************************************************
159  *  Type definitions and return values
160  ******************************************************************************/
161 
162 /* this type handles all btif context switches between BTU and HAL */
163 typedef struct {
164   BT_HDR hdr;
165   tBTIF_CBACK* p_cb; /* context switch callback */
166 
167   /* parameters passed to callback */
168   uint16_t event;                          /* message event id */
169   char __attribute__((aligned)) p_param[]; /* parameter area needs to be last */
170 } tBTIF_CONTEXT_SWITCH_CBACK;
171 
172 /*******************************************************************************
173  *  Functions
174  ******************************************************************************/
175 
176 extern bt_status_t do_in_jni_thread(const base::Closure& task);
177 extern bt_status_t do_in_jni_thread(const tracked_objects::Location& from_here,
178                                     const base::Closure& task);
179 /**
180  * This template wraps callback into callback that will be executed on jni
181  * thread
182  */
183 template <typename R, typename... Args>
jni_thread_wrapper(const tracked_objects::Location & from_here,base::Callback<R (Args...)> cb)184 base::Callback<R(Args...)> jni_thread_wrapper(
185     const tracked_objects::Location& from_here, base::Callback<R(Args...)> cb) {
186   return base::Bind(
187       [](const tracked_objects::Location& from_here,
188          base::Callback<R(Args...)> cb, Args... args) {
189         do_in_jni_thread(from_here,
190                          base::Bind(cb, std::forward<Args>(args)...));
191       },
192       from_here, std::move(cb));
193 }
194 
195 tBTA_SERVICE_MASK btif_get_enabled_services_mask(void);
196 bt_status_t btif_enable_service(tBTA_SERVICE_ID service_id);
197 bt_status_t btif_disable_service(tBTA_SERVICE_ID service_id);
198 int btif_is_enabled(void);
199 
200 /**
201  * BTIF_Events
202  */
203 void btif_enable_bluetooth_evt(tBTA_STATUS status);
204 void btif_disable_bluetooth_evt(void);
205 void btif_adapter_properties_evt(bt_status_t status, uint32_t num_props,
206                                  bt_property_t* p_props);
207 void btif_remote_properties_evt(bt_status_t status, RawAddress* remote_addr,
208                                 uint32_t num_props, bt_property_t* p_props);
209 
210 void bte_load_did_conf(const char* p_path);
211 void bte_main_boot_entry(void);
212 void bte_main_enable(void);
213 void bte_main_disable(void);
214 void bte_main_cleanup(void);
215 void bte_main_postload_cfg(void);
216 
217 bt_status_t btif_transfer_context(tBTIF_CBACK* p_cback, uint16_t event,
218                                   char* p_params, int param_len,
219                                   tBTIF_COPY_CBACK* p_copy_cback);
220 
221 void btif_init_ok(UNUSED_ATTR uint16_t event, UNUSED_ATTR char* p_param);
222 
223 #endif /* BTIF_COMMON_H */
224