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