• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <base/bind.h>
24 #include <base/location.h>
25 #include <hardware/bluetooth.h>
26 #include <stdlib.h>
27 
28 #include <functional>
29 
30 #include "abstract_message_loop.h"
31 #include "bta/include/bta_api.h"
32 #include "osi/include/log.h"
33 #include "osi/include/osi.h"
34 #include "stack/include/bt_hdr.h"
35 #include "types/raw_address.h"
36 
37 /*******************************************************************************
38  *  Constants & Macros
39  ******************************************************************************/
40 
41 #define ASSERTC(cond, msg, val)                                               \
42   do {                                                                        \
43     if (!(cond)) {                                                            \
44       LOG_ERROR("### ASSERT : %s %s line %d %s (%d) ###", __FILE__, __func__, \
45                 __LINE__, (msg), (val));                                      \
46     }                                                                         \
47   } while (0)
48 
49 /* Calculate start of event enumeration; id is top 8 bits of event */
50 #define BTIF_SIG_START(id) ((id) << 8)
51 
52 /* For upstream the MSB bit is always SET */
53 #define BTIF_SIG_CB_BIT (0x8000)
54 #define BTIF_SIG_CB_START(id) (((id) << 8) | BTIF_SIG_CB_BIT)
55 
56 /*
57  * A memcpy(3) wrapper when copying memory that might not be aligned.
58  *
59  * On certain architectures, if the memcpy(3) arguments appear to be
60  * pointing to aligned memory (e.g., struct pointers), the compiler might
61  * generate optimized memcpy(3) code. However, if the original memory was not
62  * aligned (e.g., because of incorrect "char *" to struct pointer casting),
63  * the result code might trigger SIGBUS crash.
64  *
65  * As a short-term solution, we use the help of the maybe_non_aligned_memcpy()
66  * macro to identify and fix such cases. In the future, we should fix the
67  * problematic "char *" to struct pointer casting, and this macro itself should
68  * be removed.
69  */
70 #define maybe_non_aligned_memcpy(_a, _b, _c) \
71   memcpy((void*)(_a), (void*)(_b), (_c))
72 
73 /* BTIF sub-systems */
74 #define BTIF_CORE 0
75 #define BTIF_DM 1
76 #define BTIF_HFP 2
77 #define BTIF_AV 3
78 #define BTIF_PAN 4
79 #define BTIF_HF_CLIENT 5
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_DM_API_START = BTIF_SIG_START(BTIF_DM),
97   BTIF_DM_ENABLE_SERVICE,
98   BTIF_DM_DISABLE_SERVICE,
99   /* add here */
100 
101   BTIF_HFP_API_START = BTIF_SIG_START(BTIF_HFP),
102   /* add here */
103 
104   BTIF_AV_API_START = BTIF_SIG_START(BTIF_AV),
105   /* add here */
106 };
107 
108 /**
109  * BTIF events for callbacks that require context switch to btif task
110  * on upstream path - Typically these would be non-BTA events
111  * that are generated by the BTIF layer.
112  */
113 enum {
114   BTIF_CORE_CB_START = BTIF_SIG_CB_START(BTIF_CORE),
115   /* add here */
116 
117   BTIF_HFP_CB_START = BTIF_SIG_CB_START(BTIF_HFP),
118   BTIF_HFP_CB_AUDIO_CONNECTING, /* HF AUDIO connect has been sent to BTA
119                                    successfully */
120 
121   BTIF_PAN_CB_START = BTIF_SIG_CB_START(BTIF_PAN),
122   BTIF_PAN_CB_DISCONNECTING, /* PAN Disconnect has been sent to BTA successfully
123                                 */
124 
125   BTIF_HF_CLIENT_CLIENT_CB_START = BTIF_SIG_CB_START(BTIF_HF_CLIENT),
126   BTIF_HF_CLIENT_CB_AUDIO_CONNECTING, /* AUDIO connect has been sent to BTA
127                                          successfully */
128 };
129 
130 /*******************************************************************************
131  *  Type definitions for callback functions
132  ******************************************************************************/
133 
134 typedef void(tBTIF_CBACK)(uint16_t event, char* p_param);
135 typedef void(tBTIF_COPY_CBACK)(uint16_t event, char* p_dest, char* p_src);
136 
137 /*******************************************************************************
138  *  Type definitions and return values
139  ******************************************************************************/
140 
141 /* this type handles all btif context switches between BTU and HAL */
142 typedef struct {
143   BT_HDR_RIGID hdr;
144   tBTIF_CBACK* p_cb; /* context switch callback */
145 
146   /* parameters passed to callback */
147   uint16_t event;                          /* message event id */
148   char __attribute__((aligned)) p_param[]; /* parameter area needs to be last */
149 } tBTIF_CONTEXT_SWITCH_CBACK;
150 
151 /*******************************************************************************
152  *  Functions
153  ******************************************************************************/
154 
155 extern bt_status_t do_in_jni_thread(base::OnceClosure task);
156 extern bt_status_t do_in_jni_thread(const base::Location& from_here,
157                                     base::OnceClosure task);
158 extern bool is_on_jni_thread();
159 extern btbase::AbstractMessageLoop* get_jni_message_loop();
160 
161 using BtJniClosure = std::function<void()>;
162 void post_on_bt_jni(BtJniClosure closure);
163 
164 /**
165  * This template wraps callback into callback that will be executed on jni
166  * thread
167  */
168 template <typename R, typename... Args>
jni_thread_wrapper(const base::Location & from_here,base::Callback<R (Args...)> cb)169 base::Callback<R(Args...)> jni_thread_wrapper(const base::Location& from_here,
170                                               base::Callback<R(Args...)> cb) {
171   return base::Bind(
172       [](const base::Location& from_here, base::Callback<R(Args...)> cb,
173          Args... args) {
174         do_in_jni_thread(from_here,
175                          base::Bind(cb, std::forward<Args>(args)...));
176       },
177       from_here, std::move(cb));
178 }
179 
180 tBTA_SERVICE_MASK btif_get_enabled_services_mask(void);
181 void btif_enable_service(tBTA_SERVICE_ID service_id);
182 void btif_disable_service(tBTA_SERVICE_ID service_id);
183 int btif_is_enabled(void);
184 
185 /**
186  * BTIF_Events
187  */
188 void btif_enable_bluetooth_evt();
189 void btif_adapter_properties_evt(bt_status_t status, uint32_t num_props,
190                                  bt_property_t* p_props);
191 void btif_remote_properties_evt(bt_status_t status, RawAddress* remote_addr,
192                                 uint32_t num_props, bt_property_t* p_props);
193 
194 void bte_load_did_conf(const char* p_path);
195 void bte_main_init(void);
196 
197 bt_status_t btif_transfer_context(tBTIF_CBACK* p_cback, uint16_t event,
198                                   char* p_params, int param_len,
199                                   tBTIF_COPY_CBACK* p_copy_cback);
200 
201 void btif_init_ok();
202 
203 void invoke_adapter_state_changed_cb(bt_state_t state);
204 void invoke_adapter_properties_cb(bt_status_t status, int num_properties,
205                                   bt_property_t* properties);
206 void invoke_remote_device_properties_cb(bt_status_t status, RawAddress bd_addr,
207                                         int num_properties,
208                                         bt_property_t* properties);
209 void invoke_device_found_cb(int num_properties, bt_property_t* properties);
210 void invoke_discovery_state_changed_cb(bt_discovery_state_t state);
211 void invoke_pin_request_cb(RawAddress bd_addr, bt_bdname_t bd_name,
212                            uint32_t cod, bool min_16_digit);
213 void invoke_ssp_request_cb(RawAddress bd_addr, bt_bdname_t bd_name,
214                            uint32_t cod, bt_ssp_variant_t pairing_variant,
215                            uint32_t pass_key);
216 void invoke_oob_data_request_cb(tBT_TRANSPORT t, bool valid, Octet16 c,
217                                 Octet16 r, RawAddress raw_address,
218                                 uint8_t address_type);
219 void invoke_bond_state_changed_cb(bt_status_t status, RawAddress bd_addr,
220                                   bt_bond_state_t state, int fail_reason);
221 void invoke_address_consolidate_cb(RawAddress main_bd_addr,
222                                    RawAddress secondary_bd_addr);
223 void invoke_le_address_associate_cb(RawAddress main_bd_addr,
224                                     RawAddress secondary_bd_addr);
225 void invoke_acl_state_changed_cb(bt_status_t status, RawAddress bd_addr,
226                                  bt_acl_state_t state, int transport_link_type,
227                                  bt_hci_error_code_t hci_reason);
228 void invoke_thread_evt_cb(bt_cb_thread_evt event);
229 void invoke_le_test_mode_cb(bt_status_t status, uint16_t count);
230 void invoke_energy_info_cb(bt_activity_energy_info energy_info,
231                            bt_uid_traffic_t* uid_data);
232 void invoke_link_quality_report_cb(
233     uint64_t timestamp, int report_id, int rssi, int snr,
234     int retransmission_count, int packets_not_receive_count,
235     int negative_acknowledgement_count);
236 
237 void invoke_switch_buffer_size_cb(bool is_low_latency_buffer_size);
238 void invoke_switch_codec_cb(bool is_low_latency_buffer_size);
239 
240 #endif /* BTIF_COMMON_H */
241