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 <hardware/bluetooth.h>
28
29 #include "abstract_message_loop.h"
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 #define HAL_CBACK(P_CB, P_CBACK, ...) \
80 do { \
81 if ((P_CB) && (P_CB)->P_CBACK) { \
82 BTIF_TRACE_API("%s: HAL %s->%s", __func__, #P_CB, #P_CBACK); \
83 (P_CB)->P_CBACK(__VA_ARGS__); \
84 } else { \
85 ASSERTC(0, "Callback is NULL", 0); \
86 } \
87 } while (0)
88
89 /**
90 * BTIF events for requests that require context switch to btif task
91 * on downstreams path
92 */
93 enum {
94 BTIF_DM_API_START = BTIF_SIG_START(BTIF_DM),
95 BTIF_DM_ENABLE_SERVICE,
96 BTIF_DM_DISABLE_SERVICE,
97 /* add here */
98
99 BTIF_HFP_API_START = BTIF_SIG_START(BTIF_HFP),
100 /* add here */
101
102 BTIF_AV_API_START = BTIF_SIG_START(BTIF_AV),
103 /* add here */
104 };
105
106 /**
107 * BTIF events for callbacks that require context switch to btif task
108 * on upstream path - Typically these would be non-BTA events
109 * that are generated by the BTIF layer.
110 */
111 enum {
112 BTIF_CORE_CB_START = BTIF_SIG_CB_START(BTIF_CORE),
113 /* add here */
114
115 BTIF_HFP_CB_START = BTIF_SIG_CB_START(BTIF_HFP),
116 BTIF_HFP_CB_AUDIO_CONNECTING, /* HF AUDIO connect has been sent to BTA
117 successfully */
118
119 BTIF_PAN_CB_START = BTIF_SIG_CB_START(BTIF_PAN),
120 BTIF_PAN_CB_DISCONNECTING, /* PAN Disconnect has been sent to BTA successfully
121 */
122
123 BTIF_HF_CLIENT_CLIENT_CB_START = BTIF_SIG_CB_START(BTIF_HF_CLIENT),
124 BTIF_HF_CLIENT_CB_AUDIO_CONNECTING, /* AUDIO connect has been sent to BTA
125 successfully */
126 };
127
128 /*******************************************************************************
129 * Type definitions for callback functions
130 ******************************************************************************/
131
132 typedef void(tBTIF_CBACK)(uint16_t event, char* p_param);
133 typedef void(tBTIF_COPY_CBACK)(uint16_t event, char* p_dest, char* p_src);
134
135 /*******************************************************************************
136 * Type definitions and return values
137 ******************************************************************************/
138
139 /* this type handles all btif context switches between BTU and HAL */
140 typedef struct {
141 BT_HDR_RIGID hdr;
142 tBTIF_CBACK* p_cb; /* context switch callback */
143
144 /* parameters passed to callback */
145 uint16_t event; /* message event id */
146 char __attribute__((aligned)) p_param[]; /* parameter area needs to be last */
147 } tBTIF_CONTEXT_SWITCH_CBACK;
148
149 /*******************************************************************************
150 * Functions
151 ******************************************************************************/
152
153 extern bt_status_t do_in_jni_thread(base::OnceClosure task);
154 extern bt_status_t do_in_jni_thread(const base::Location& from_here,
155 base::OnceClosure task);
156 extern bool is_on_jni_thread();
157 extern btbase::AbstractMessageLoop* get_jni_message_loop();
158 /**
159 * This template wraps callback into callback that will be executed on jni
160 * thread
161 */
162 template <typename R, typename... Args>
jni_thread_wrapper(const base::Location & from_here,base::Callback<R (Args...)> cb)163 base::Callback<R(Args...)> jni_thread_wrapper(const base::Location& from_here,
164 base::Callback<R(Args...)> cb) {
165 return base::Bind(
166 [](const base::Location& from_here, base::Callback<R(Args...)> cb,
167 Args... args) {
168 do_in_jni_thread(from_here,
169 base::Bind(cb, std::forward<Args>(args)...));
170 },
171 from_here, std::move(cb));
172 }
173
174 tBTA_SERVICE_MASK btif_get_enabled_services_mask(void);
175 void btif_enable_service(tBTA_SERVICE_ID service_id);
176 void btif_disable_service(tBTA_SERVICE_ID service_id);
177 int btif_is_enabled(void);
178
179 /**
180 * BTIF_Events
181 */
182 void btif_enable_bluetooth_evt();
183 void btif_adapter_properties_evt(bt_status_t status, uint32_t num_props,
184 bt_property_t* p_props);
185 void btif_remote_properties_evt(bt_status_t status, RawAddress* remote_addr,
186 uint32_t num_props, bt_property_t* p_props);
187
188 void bte_load_did_conf(const char* p_path);
189 void bte_main_init(void);
190
191 bt_status_t btif_transfer_context(tBTIF_CBACK* p_cback, uint16_t event,
192 char* p_params, int param_len,
193 tBTIF_COPY_CBACK* p_copy_cback);
194
195 void btif_init_ok();
196
197 void invoke_adapter_state_changed_cb(bt_state_t state);
198 void invoke_adapter_properties_cb(bt_status_t status, int num_properties,
199 bt_property_t* properties);
200 void invoke_remote_device_properties_cb(bt_status_t status, RawAddress bd_addr,
201 int num_properties,
202 bt_property_t* properties);
203 void invoke_device_found_cb(int num_properties, bt_property_t* properties);
204 void invoke_discovery_state_changed_cb(bt_discovery_state_t state);
205 void invoke_pin_request_cb(RawAddress bd_addr, bt_bdname_t bd_name,
206 uint32_t cod, bool min_16_digit);
207 void invoke_ssp_request_cb(RawAddress bd_addr, bt_bdname_t bd_name,
208 uint32_t cod, bt_ssp_variant_t pairing_variant,
209 uint32_t pass_key);
210 void invoke_oob_data_request_cb(tBT_TRANSPORT t, bool valid, Octet16 c,
211 Octet16 r, RawAddress raw_address,
212 uint8_t address_type);
213 void invoke_bond_state_changed_cb(bt_status_t status, RawAddress bd_addr,
214 bt_bond_state_t state);
215 void invoke_acl_state_changed_cb(bt_status_t status, RawAddress bd_addr,
216 bt_acl_state_t state, bt_hci_error_code_t hci_reason);
217 void invoke_thread_evt_cb(bt_cb_thread_evt event);
218 void invoke_le_test_mode_cb(bt_status_t status, uint16_t count);
219 void invoke_energy_info_cb(bt_activity_energy_info energy_info,
220 bt_uid_traffic_t* uid_data);
221 void invoke_link_quality_report_cb(
222 uint64_t timestamp, int report_id, int rssi, int snr,
223 int retransmission_count, int packets_not_receive_count,
224 int negative_acknowledgement_count);
225
226 #endif /* BTIF_COMMON_H */
227