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/functional/bind.h>
24 #include <base/location.h>
25 #include <bluetooth/log.h>
26 #include <hardware/bluetooth.h>
27 #include <stdlib.h>
28 
29 #include <functional>
30 
31 #include "abstract_message_loop.h"
32 #include "bta/include/bta_api.h"
33 #include "os/log.h"
34 #include "osi/include/osi.h"
35 #include "stack/include/bt_hdr.h"
36 #include "types/raw_address.h"
37 
38 /*******************************************************************************
39  *  Constants & Macros
40  ******************************************************************************/
41 
42 #define ASSERTC(cond, msg, val)                                        \
43   do {                                                                 \
44     if (!(cond)) {                                                     \
45       bluetooth::log::error("### ASSERT : {} ({}) ###", (msg), (val)); \
46     }                                                                  \
47   } while (0)
48 
49 /*
50  * A memcpy(3) wrapper when copying memory that might not be aligned.
51  *
52  * On certain architectures, if the memcpy(3) arguments appear to be
53  * pointing to aligned memory (e.g., struct pointers), the compiler might
54  * generate optimized memcpy(3) code. However, if the original memory was not
55  * aligned (e.g., because of incorrect "char *" to struct pointer casting),
56  * the result code might trigger SIGBUS crash.
57  *
58  * As a short-term solution, we use the help of the maybe_non_aligned_memcpy()
59  * macro to identify and fix such cases. In the future, we should fix the
60  * problematic "char *" to struct pointer casting, and this macro itself should
61  * be removed.
62  */
63 #define maybe_non_aligned_memcpy(_a, _b, _c) \
64   memcpy((void*)(_a), (void*)(_b), (_c))
65 
66 #define HAL_CBACK(P_CB, P_CBACK, ...)                         \
67   do {                                                        \
68     if ((P_CB) && (P_CB)->P_CBACK) {                          \
69       bluetooth::log::verbose("HAL {}->{}", #P_CB, #P_CBACK); \
70       (P_CB)->P_CBACK(__VA_ARGS__);                           \
71     } else {                                                  \
72       ASSERTC(0, "Callback is NULL", 0);                      \
73     }                                                         \
74   } while (0)
75 
76 /*******************************************************************************
77  *  Type definitions for callback functions
78  ******************************************************************************/
79 
80 typedef void(tBTIF_CBACK)(uint16_t event, char* p_param);
81 typedef void(tBTIF_COPY_CBACK)(uint16_t event, char* p_dest, const char* p_src);
82 
83 /*******************************************************************************
84  *  Type definitions and return values
85  ******************************************************************************/
86 
87 /* this type handles all btif context switches between BTU and HAL */
88 typedef struct {
89   BT_HDR_RIGID hdr;
90   tBTIF_CBACK* p_cb; /* context switch callback */
91 
92   /* parameters passed to callback */
93   uint16_t event;                          /* message event id */
94   char __attribute__((aligned)) p_param[]; /* parameter area needs to be last */
95 } tBTIF_CONTEXT_SWITCH_CBACK;
96 
97 /*******************************************************************************
98  *  Functions
99  ******************************************************************************/
100 
101 bt_status_t do_in_jni_thread(base::OnceClosure task);
102 bool is_on_jni_thread();
103 
104 using BtJniClosure = std::function<void()>;
105 void post_on_bt_jni(BtJniClosure closure);
106 
107 /**
108  * This template wraps callback into callback that will be executed on jni
109  * thread
110  */
111 template <typename R, typename... Args>
jni_thread_wrapper(base::Callback<R (Args...)> cb)112 base::Callback<R(Args...)> jni_thread_wrapper(base::Callback<R(Args...)> cb) {
113   return base::Bind(
114       [](base::Callback<R(Args...)> cb, Args... args) {
115         do_in_jni_thread(base::BindOnce(cb, std::forward<Args>(args)...));
116       },
117       std::move(cb));
118 }
119 
120 tBTA_SERVICE_MASK btif_get_enabled_services_mask(void);
121 void btif_enable_service(tBTA_SERVICE_ID service_id);
122 void btif_disable_service(tBTA_SERVICE_ID service_id);
123 int btif_is_enabled(void);
124 
125 /**
126  * BTIF_Events
127  */
128 void btif_enable_bluetooth_evt();
129 void btif_adapter_properties_evt(bt_status_t status, uint32_t num_props,
130                                  bt_property_t* p_props);
131 void btif_remote_properties_evt(bt_status_t status, RawAddress* remote_addr,
132                                 uint32_t num_props, bt_property_t* p_props);
133 
134 void bte_load_did_conf(const char* p_path);
135 
136 bt_status_t btif_transfer_context(tBTIF_CBACK* p_cback, uint16_t event,
137                                   char* p_params, int param_len,
138                                   tBTIF_COPY_CBACK* p_copy_cback);
139 
140 void btif_init_ok();
141 
142 void invoke_adapter_state_changed_cb(bt_state_t state);
143 void invoke_adapter_properties_cb(bt_status_t status, int num_properties,
144                                   bt_property_t* properties);
145 void invoke_remote_device_properties_cb(bt_status_t status, RawAddress bd_addr,
146                                         int num_properties,
147                                         bt_property_t* properties);
148 void invoke_device_found_cb(int num_properties, bt_property_t* properties);
149 void invoke_discovery_state_changed_cb(bt_discovery_state_t state);
150 void invoke_pin_request_cb(RawAddress bd_addr, bt_bdname_t bd_name,
151                            uint32_t cod, bool min_16_digit);
152 void invoke_ssp_request_cb(RawAddress bd_addr, bt_ssp_variant_t pairing_variant,
153                            uint32_t pass_key);
154 void invoke_oob_data_request_cb(tBT_TRANSPORT t, bool valid, Octet16 c,
155                                 Octet16 r, RawAddress raw_address,
156                                 uint8_t address_type);
157 void invoke_bond_state_changed_cb(bt_status_t status, RawAddress bd_addr,
158                                   bt_bond_state_t state, int fail_reason);
159 void invoke_address_consolidate_cb(RawAddress main_bd_addr,
160                                    RawAddress secondary_bd_addr);
161 void invoke_le_address_associate_cb(RawAddress main_bd_addr,
162                                     RawAddress secondary_bd_addr);
163 void invoke_acl_state_changed_cb(bt_status_t status, RawAddress bd_addr,
164                                  bt_acl_state_t state, int transport_link_type,
165                                  bt_hci_error_code_t hci_reason,
166                                  bt_conn_direction_t direction,
167                                  uint16_t acl_handle);
168 void invoke_thread_evt_cb(bt_cb_thread_evt event);
169 void invoke_le_test_mode_cb(bt_status_t status, uint16_t count);
170 void invoke_energy_info_cb(bt_activity_energy_info energy_info,
171                            bt_uid_traffic_t* uid_data);
172 void invoke_link_quality_report_cb(
173     uint64_t timestamp, int report_id, int rssi, int snr,
174     int retransmission_count, int packets_not_receive_count,
175     int negative_acknowledgement_count);
176 
177 void invoke_switch_buffer_size_cb(bool is_low_latency_buffer_size);
178 void invoke_switch_codec_cb(bool is_low_latency_buffer_size);
179 void invoke_key_missing_cb(RawAddress bd_addr);
180 #endif /* BTIF_COMMON_H */
181