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