The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
Hemant Gupta | 1025687 | 2013-08-19 18:33:01 +0530 | [diff] [blame] | 3 | * Copyright (c) 2014 The Android Open Source Project |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 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 | |
Vinit Deshpande | f54df6b | 2015-04-15 13:02:58 -0700 | [diff] [blame] | 23 | #include <stdlib.h> |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 24 | |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 25 | #include <hardware/bluetooth.h> |
| 26 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 27 | #include "bt_types.h" |
| 28 | #include "bta_api.h" |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 29 | #include "osi/include/log.h" |
| 30 | #include "osi/include/osi.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 31 | |
| 32 | /******************************************************************************* |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 33 | * Constants & Macros |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 34 | ******************************************************************************/ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 35 | |
Myles Watson | 40cde56 | 2016-10-21 09:39:13 -0700 | [diff] [blame] | 36 | #define ASSERTC(cond, msg, val) \ |
| 37 | do { \ |
| 38 | if (!(cond)) { \ |
| 39 | LOG_ERROR(LOG_TAG, "### ASSERT : %s %s line %d %s (%d) ###", __FILE__, \ |
| 40 | __func__, __LINE__, (msg), (val)); \ |
| 41 | } \ |
| 42 | } while (0) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 43 | |
| 44 | /* Calculate start of event enumeration; id is top 8 bits of event */ |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 45 | #define BTIF_SIG_START(id) ((id) << 8) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 46 | |
| 47 | /* For upstream the MSB bit is always SET */ |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 48 | #define BTIF_SIG_CB_BIT (0x8000) |
| 49 | #define BTIF_SIG_CB_START(id) (((id) << 8) | BTIF_SIG_CB_BIT) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 50 | |
Pavlin Radoslavov | 3f06e14 | 2015-11-09 18:39:03 -0800 | [diff] [blame] | 51 | /* |
| 52 | * A memcpy(3) wrapper when copying memory that might not be aligned. |
| 53 | * |
| 54 | * On certain architectures, if the memcpy(3) arguments appear to be |
| 55 | * pointing to aligned memory (e.g., struct pointers), the compiler might |
| 56 | * generate optimized memcpy(3) code. However, if the original memory was not |
| 57 | * aligned (e.g., because of incorrect "char *" to struct pointer casting), |
| 58 | * the result code might trigger SIGBUS crash. |
| 59 | * |
| 60 | * As a short-term solution, we use the help of the maybe_non_aligned_memcpy() |
| 61 | * macro to identify and fix such cases. In the future, we should fix the |
| 62 | * problematic "char *" to struct pointer casting, and this macro itself should |
| 63 | * be removed. |
| 64 | */ |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 65 | #define maybe_non_aligned_memcpy(_a, _b, _c) memcpy((void*)(_a), (_b), (_c)) |
Pavlin Radoslavov | 3f06e14 | 2015-11-09 18:39:03 -0800 | [diff] [blame] | 66 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 67 | /* BTIF sub-systems */ |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 68 | #define BTIF_CORE 0 |
| 69 | #define BTIF_DM 1 |
| 70 | #define BTIF_HFP 2 |
| 71 | #define BTIF_AV 3 |
| 72 | #define BTIF_PAN 4 |
| 73 | #define BTIF_HF_CLIENT 5 |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 74 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 75 | extern bt_callbacks_t* bt_hal_cbacks; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 76 | |
Myles Watson | 40cde56 | 2016-10-21 09:39:13 -0700 | [diff] [blame] | 77 | #define HAL_CBACK(P_CB, P_CBACK, ...) \ |
| 78 | do { \ |
| 79 | if ((P_CB) && (P_CB)->P_CBACK) { \ |
| 80 | BTIF_TRACE_API("HAL %s->%s", #P_CB, #P_CBACK); \ |
| 81 | (P_CB)->P_CBACK(__VA_ARGS__); \ |
| 82 | } else { \ |
| 83 | ASSERTC(0, "Callback is NULL", 0); \ |
| 84 | } \ |
| 85 | } while (0) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 86 | |
| 87 | /** |
| 88 | * BTIF events for requests that require context switch to btif task |
| 89 | * on downstreams path |
| 90 | */ |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 91 | enum { |
| 92 | BTIF_CORE_API_START = BTIF_SIG_START(BTIF_CORE), |
| 93 | BTIF_CORE_STORAGE_NO_ACTION, |
| 94 | BTIF_CORE_STORAGE_ADAPTER_WRITE, |
| 95 | BTIF_CORE_STORAGE_ADAPTER_READ, |
| 96 | BTIF_CORE_STORAGE_ADAPTER_READ_ALL, |
| 97 | BTIF_CORE_STORAGE_REMOTE_WRITE, |
| 98 | BTIF_CORE_STORAGE_REMOTE_READ, |
| 99 | BTIF_CORE_STORAGE_REMOTE_READ_ALL, |
| 100 | BTIF_CORE_STORAGE_READ_ALL, |
| 101 | BTIF_CORE_STORAGE_NOTIFY_STATUS, |
| 102 | /* add here */ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 103 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 104 | BTIF_DM_API_START = BTIF_SIG_START(BTIF_DM), |
| 105 | BTIF_DM_ENABLE_SERVICE, |
| 106 | BTIF_DM_DISABLE_SERVICE, |
| 107 | /* add here */ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 108 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 109 | BTIF_HFP_API_START = BTIF_SIG_START(BTIF_HFP), |
| 110 | /* add here */ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 111 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 112 | BTIF_AV_API_START = BTIF_SIG_START(BTIF_AV), |
| 113 | /* add here */ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | /** |
| 117 | * BTIF events for callbacks that require context switch to btif task |
| 118 | * on upstream path - Typically these would be non-BTA events |
| 119 | * that are generated by the BTIF layer. |
| 120 | */ |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 121 | enum { |
| 122 | BTIF_CORE_CB_START = BTIF_SIG_CB_START(BTIF_CORE), |
| 123 | /* add here */ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 124 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 125 | BTIF_DM_CB_START = BTIF_SIG_CB_START(BTIF_DM), |
| 126 | BTIF_DM_CB_DISCOVERY_STARTED, /* Discovery has started */ |
| 127 | BTIF_DM_CB_CREATE_BOND, /* Create bond */ |
| 128 | BTIF_DM_CB_REMOVE_BOND, /*Remove bond */ |
| 129 | BTIF_DM_CB_HID_REMOTE_NAME, /* Remote name callback for HID device */ |
| 130 | BTIF_DM_CB_BOND_STATE_BONDING, |
| 131 | BTIF_DM_CB_LE_TX_TEST, /* BLE Tx Test command complete callback */ |
| 132 | BTIF_DM_CB_LE_RX_TEST, /* BLE Rx Test command complete callback */ |
| 133 | BTIF_DM_CB_LE_TEST_END, /* BLE Test mode end callback */ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 134 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 135 | BTIF_HFP_CB_START = BTIF_SIG_CB_START(BTIF_HFP), |
| 136 | BTIF_HFP_CB_AUDIO_CONNECTING, /* HF AUDIO connect has been sent to BTA |
| 137 | successfully */ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 138 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 139 | BTIF_PAN_CB_START = BTIF_SIG_CB_START(BTIF_PAN), |
| 140 | BTIF_PAN_CB_DISCONNECTING, /* PAN Disconnect has been sent to BTA successfully |
| 141 | */ |
Hemant Gupta | 1025687 | 2013-08-19 18:33:01 +0530 | [diff] [blame] | 142 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 143 | BTIF_HF_CLIENT_CLIENT_CB_START = BTIF_SIG_CB_START(BTIF_HF_CLIENT), |
| 144 | BTIF_HF_CLIENT_CB_AUDIO_CONNECTING, /* AUDIO connect has been sent to BTA |
| 145 | successfully */ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 146 | }; |
| 147 | |
| 148 | /* Macro definitions for BD ADDR persistence */ |
| 149 | |
| 150 | /** |
| 151 | * PROPERTY_BT_BDADDR_PATH |
| 152 | * The property key stores the storage location of Bluetooth Device Address |
| 153 | */ |
| 154 | #ifndef PROPERTY_BT_BDADDR_PATH |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 155 | #define PROPERTY_BT_BDADDR_PATH "ro.bt.bdaddr_path" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 156 | #endif |
| 157 | |
| 158 | /** |
| 159 | * PERSIST_BDADDR_PROPERTY |
| 160 | * If there is no valid bdaddr available from PROPERTY_BT_BDADDR_PATH, |
| 161 | * generating a random BDADDR and keeping it in the PERSIST_BDADDR_DROP. |
| 162 | */ |
| 163 | #ifndef PERSIST_BDADDR_PROPERTY |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 164 | #define PERSIST_BDADDR_PROPERTY "persist.service.bdroid.bdaddr" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 165 | #endif |
| 166 | |
Ajay Panicker | 28f294b | 2015-08-03 16:29:31 -0700 | [diff] [blame] | 167 | /** |
| 168 | * FACTORY_BT_BDADDR_PROPERTY |
| 169 | * If there is no valid bdaddr available from PROPERTY_BT_BDADDR_PATH |
| 170 | * and there is no available persistent bdaddr available from |
| 171 | * PERSIST_BDADDR_PROPERTY use a factory set address |
| 172 | */ |
| 173 | #ifndef FACTORY_BT_ADDR_PROPERTY |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 174 | #define FACTORY_BT_ADDR_PROPERTY "ro.boot.btmacaddr" |
Ajay Panicker | 28f294b | 2015-08-03 16:29:31 -0700 | [diff] [blame] | 175 | #endif |
| 176 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 177 | #define FACTORY_BT_BDADDR_STORAGE_LEN 17 |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 178 | |
| 179 | /******************************************************************************* |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 180 | * Type definitions for callback functions |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 181 | ******************************************************************************/ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 182 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 183 | typedef void(tBTIF_CBACK)(uint16_t event, char* p_param); |
| 184 | typedef void(tBTIF_COPY_CBACK)(uint16_t event, char* p_dest, char* p_src); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 185 | |
| 186 | /******************************************************************************* |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 187 | * Type definitions and return values |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 188 | ******************************************************************************/ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 189 | |
| 190 | /* this type handles all btif context switches between BTU and HAL */ |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 191 | typedef struct { |
| 192 | BT_HDR hdr; |
| 193 | tBTIF_CBACK* p_cb; /* context switch callback */ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 194 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 195 | /* parameters passed to callback */ |
| 196 | uint16_t event; /* message event id */ |
| 197 | char __attribute__((aligned)) p_param[]; /* parameter area needs to be last */ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 198 | } tBTIF_CONTEXT_SWITCH_CBACK; |
| 199 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 200 | /******************************************************************************* |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 201 | * Functions |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 202 | ******************************************************************************/ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 203 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 204 | tBTA_SERVICE_MASK btif_get_enabled_services_mask(void); |
| 205 | bt_status_t btif_enable_service(tBTA_SERVICE_ID service_id); |
| 206 | bt_status_t btif_disable_service(tBTA_SERVICE_ID service_id); |
| 207 | int btif_is_enabled(void); |
| 208 | |
| 209 | /** |
| 210 | * BTIF_Events |
| 211 | */ |
Zach Johnson | 39110ec | 2014-10-06 13:15:00 -0700 | [diff] [blame] | 212 | void btif_enable_bluetooth_evt(tBTA_STATUS status); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 213 | void btif_disable_bluetooth_evt(void); |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 214 | void btif_adapter_properties_evt(bt_status_t status, uint32_t num_props, |
| 215 | bt_property_t* p_props); |
| 216 | void btif_remote_properties_evt(bt_status_t status, bt_bdaddr_t* remote_addr, |
| 217 | uint32_t num_props, bt_property_t* p_props); |
Jakub Pawlowski | 22c590f | 2016-06-15 17:15:42 -0700 | [diff] [blame] | 218 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 219 | void bte_load_did_conf(const char* p_path); |
Jakub Pawlowski | 22c590f | 2016-06-15 17:15:42 -0700 | [diff] [blame] | 220 | void bte_main_boot_entry(void); |
Pavlin Radoslavov | b2a292b | 2016-10-14 19:34:48 -0700 | [diff] [blame] | 221 | void bte_main_enable(void); |
Jakub Pawlowski | 22c590f | 2016-06-15 17:15:42 -0700 | [diff] [blame] | 222 | void bte_main_disable(void); |
| 223 | void bte_main_cleanup(void); |
Marie Janssen | b7f64bc | 2016-06-22 12:52:19 -0700 | [diff] [blame] | 224 | #if (HCILP_INCLUDED == TRUE) |
| 225 | void bte_main_enable_lpm(bool enable); |
Jakub Pawlowski | 22c590f | 2016-06-15 17:15:42 -0700 | [diff] [blame] | 226 | #endif |
| 227 | void bte_main_postload_cfg(void); |
| 228 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 229 | bt_status_t btif_transfer_context(tBTIF_CBACK* p_cback, uint16_t event, |
| 230 | char* p_params, int param_len, |
| 231 | tBTIF_COPY_CBACK* p_copy_cback); |
Jakub Pawlowski | 22c590f | 2016-06-15 17:15:42 -0700 | [diff] [blame] | 232 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 233 | void btif_init_ok(UNUSED_ATTR uint16_t event, UNUSED_ATTR char* p_param); |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 234 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 235 | #endif /* BTIF_COMMON_H */ |