blob: 0fbcfb018f378bbbf1ed169341b7f95cad83093c [file] [log] [blame]
The Android Open Source Project5738f832012-12-12 16:00:35 -08001/******************************************************************************
2 *
Hemant Gupta10256872013-08-19 18:33:01 +05303 * Copyright (c) 2014 The Android Open Source Project
The Android Open Source Project5738f832012-12-12 16:00:35 -08004 * 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 Deshpandef54df6b2015-04-15 13:02:58 -070023#include <stdlib.h>
Marie Janssendb554582015-06-26 14:53:46 -070024
Chris Manton0eefef02014-09-08 15:01:39 -070025#include <hardware/bluetooth.h>
26
The Android Open Source Project5738f832012-12-12 16:00:35 -080027#include "bt_types.h"
28#include "bta_api.h"
Myles Watson6bd442f2016-10-19 09:50:22 -070029#include "osi/include/log.h"
30#include "osi/include/osi.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080031
32/*******************************************************************************
Myles Watson6bd442f2016-10-19 09:50:22 -070033 * Constants & Macros
Myles Watsonee96a3c2016-11-23 14:49:54 -080034 ******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -080035
Myles Watson40cde562016-10-21 09:39:13 -070036#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 Project5738f832012-12-12 16:00:35 -080043
44/* Calculate start of event enumeration; id is top 8 bits of event */
Myles Watson6bd442f2016-10-19 09:50:22 -070045#define BTIF_SIG_START(id) ((id) << 8)
The Android Open Source Project5738f832012-12-12 16:00:35 -080046
47/* For upstream the MSB bit is always SET */
Myles Watson6bd442f2016-10-19 09:50:22 -070048#define BTIF_SIG_CB_BIT (0x8000)
49#define BTIF_SIG_CB_START(id) (((id) << 8) | BTIF_SIG_CB_BIT)
The Android Open Source Project5738f832012-12-12 16:00:35 -080050
Pavlin Radoslavov3f06e142015-11-09 18:39:03 -080051/*
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 Watson6bd442f2016-10-19 09:50:22 -070065#define maybe_non_aligned_memcpy(_a, _b, _c) memcpy((void*)(_a), (_b), (_c))
Pavlin Radoslavov3f06e142015-11-09 18:39:03 -080066
The Android Open Source Project5738f832012-12-12 16:00:35 -080067/* BTIF sub-systems */
Myles Watson6bd442f2016-10-19 09:50:22 -070068#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 Project5738f832012-12-12 16:00:35 -080074
Myles Watson6bd442f2016-10-19 09:50:22 -070075extern bt_callbacks_t* bt_hal_cbacks;
The Android Open Source Project5738f832012-12-12 16:00:35 -080076
Myles Watson40cde562016-10-21 09:39:13 -070077#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 Project5738f832012-12-12 16:00:35 -080086
87/**
88 * BTIF events for requests that require context switch to btif task
89 * on downstreams path
90 */
Myles Watson6bd442f2016-10-19 09:50:22 -070091enum {
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 Project5738f832012-12-12 16:00:35 -0800103
Myles Watson6bd442f2016-10-19 09:50:22 -0700104 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 Project5738f832012-12-12 16:00:35 -0800108
Myles Watson6bd442f2016-10-19 09:50:22 -0700109 BTIF_HFP_API_START = BTIF_SIG_START(BTIF_HFP),
110 /* add here */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800111
Myles Watson6bd442f2016-10-19 09:50:22 -0700112 BTIF_AV_API_START = BTIF_SIG_START(BTIF_AV),
113 /* add here */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800114};
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 Watson6bd442f2016-10-19 09:50:22 -0700121enum {
122 BTIF_CORE_CB_START = BTIF_SIG_CB_START(BTIF_CORE),
123 /* add here */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800124
Myles Watson6bd442f2016-10-19 09:50:22 -0700125 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 Project5738f832012-12-12 16:00:35 -0800134
Myles Watson6bd442f2016-10-19 09:50:22 -0700135 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 Project5738f832012-12-12 16:00:35 -0800138
Myles Watson6bd442f2016-10-19 09:50:22 -0700139 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 Gupta10256872013-08-19 18:33:01 +0530142
Myles Watson6bd442f2016-10-19 09:50:22 -0700143 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 Project5738f832012-12-12 16:00:35 -0800146};
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 Watson6bd442f2016-10-19 09:50:22 -0700155#define PROPERTY_BT_BDADDR_PATH "ro.bt.bdaddr_path"
The Android Open Source Project5738f832012-12-12 16:00:35 -0800156#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 Watson6bd442f2016-10-19 09:50:22 -0700164#define PERSIST_BDADDR_PROPERTY "persist.service.bdroid.bdaddr"
The Android Open Source Project5738f832012-12-12 16:00:35 -0800165#endif
166
Ajay Panicker28f294b2015-08-03 16:29:31 -0700167/**
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 Watson6bd442f2016-10-19 09:50:22 -0700174#define FACTORY_BT_ADDR_PROPERTY "ro.boot.btmacaddr"
Ajay Panicker28f294b2015-08-03 16:29:31 -0700175#endif
176
Myles Watson6bd442f2016-10-19 09:50:22 -0700177#define FACTORY_BT_BDADDR_STORAGE_LEN 17
The Android Open Source Project5738f832012-12-12 16:00:35 -0800178
179/*******************************************************************************
Myles Watson6bd442f2016-10-19 09:50:22 -0700180 * Type definitions for callback functions
Myles Watsonee96a3c2016-11-23 14:49:54 -0800181 ******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -0800182
Myles Watson6bd442f2016-10-19 09:50:22 -0700183typedef void(tBTIF_CBACK)(uint16_t event, char* p_param);
184typedef void(tBTIF_COPY_CBACK)(uint16_t event, char* p_dest, char* p_src);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800185
186/*******************************************************************************
Myles Watson6bd442f2016-10-19 09:50:22 -0700187 * Type definitions and return values
Myles Watsonee96a3c2016-11-23 14:49:54 -0800188 ******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -0800189
190/* this type handles all btif context switches between BTU and HAL */
Myles Watson6bd442f2016-10-19 09:50:22 -0700191typedef struct {
192 BT_HDR hdr;
193 tBTIF_CBACK* p_cb; /* context switch callback */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800194
Myles Watson6bd442f2016-10-19 09:50:22 -0700195 /* 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 Project5738f832012-12-12 16:00:35 -0800198} tBTIF_CONTEXT_SWITCH_CBACK;
199
The Android Open Source Project5738f832012-12-12 16:00:35 -0800200/*******************************************************************************
Myles Watson6bd442f2016-10-19 09:50:22 -0700201 * Functions
Myles Watsonee96a3c2016-11-23 14:49:54 -0800202 ******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -0800203
The Android Open Source Project5738f832012-12-12 16:00:35 -0800204tBTA_SERVICE_MASK btif_get_enabled_services_mask(void);
205bt_status_t btif_enable_service(tBTA_SERVICE_ID service_id);
206bt_status_t btif_disable_service(tBTA_SERVICE_ID service_id);
207int btif_is_enabled(void);
208
209/**
210 * BTIF_Events
211 */
Zach Johnson39110ec2014-10-06 13:15:00 -0700212void btif_enable_bluetooth_evt(tBTA_STATUS status);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800213void btif_disable_bluetooth_evt(void);
Myles Watson6bd442f2016-10-19 09:50:22 -0700214void btif_adapter_properties_evt(bt_status_t status, uint32_t num_props,
215 bt_property_t* p_props);
216void btif_remote_properties_evt(bt_status_t status, bt_bdaddr_t* remote_addr,
217 uint32_t num_props, bt_property_t* p_props);
Jakub Pawlowski22c590f2016-06-15 17:15:42 -0700218
Myles Watson6bd442f2016-10-19 09:50:22 -0700219void bte_load_did_conf(const char* p_path);
Jakub Pawlowski22c590f2016-06-15 17:15:42 -0700220void bte_main_boot_entry(void);
Pavlin Radoslavovb2a292b2016-10-14 19:34:48 -0700221void bte_main_enable(void);
Jakub Pawlowski22c590f2016-06-15 17:15:42 -0700222void bte_main_disable(void);
223void bte_main_cleanup(void);
Marie Janssenb7f64bc2016-06-22 12:52:19 -0700224#if (HCILP_INCLUDED == TRUE)
225void bte_main_enable_lpm(bool enable);
Jakub Pawlowski22c590f2016-06-15 17:15:42 -0700226#endif
227void bte_main_postload_cfg(void);
228
Myles Watson6bd442f2016-10-19 09:50:22 -0700229bt_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 Pawlowski22c590f2016-06-15 17:15:42 -0700232
Myles Watson6bd442f2016-10-19 09:50:22 -0700233void btif_init_ok(UNUSED_ATTR uint16_t event, UNUSED_ATTR char* p_param);
Chris Manton0eefef02014-09-08 15:01:39 -0700234
The Android Open Source Project5738f832012-12-12 16:00:35 -0800235#endif /* BTIF_COMMON_H */