1 /******************************************************************************
2  *
3  *  Copyright (c) 2014 The Android Open Source Project
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 
23 #include <stdlib.h>
24 
25 #include <hardware/bluetooth.h>
26 
27 #include "osi/include/osi.h"
28 #include "osi/include/log.h"
29 #include "bt_types.h"
30 #include "bta_api.h"
31 
32 /*******************************************************************************
33 **  Constants & Macros
34 ********************************************************************************/
35 
36 #define ASSERTC(cond, msg, val) if (!(cond)) { LOG_ERROR(LOG_TAG, \
37     "### ASSERT : %s line %d %s (%d) ###", __FILE__, __LINE__, msg, val);}
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 
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 
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
68 #define BTIF_HF_CLIENT      5
69 
70 extern bt_callbacks_t *bt_hal_cbacks;
71 
72 #define HAL_CBACK(P_CB, P_CBACK, ...)\
73     if (P_CB && P_CB->P_CBACK) {            \
74         BTIF_TRACE_API("HAL %s->%s", #P_CB, #P_CBACK); \
75         P_CB->P_CBACK(__VA_ARGS__);         \
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,
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 */
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 */
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 */
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 
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 
170 #define FACTORY_BT_BDADDR_STORAGE_LEN   17
171 
172 
173 /*******************************************************************************
174 **  Type definitions for callback functions
175 ********************************************************************************/
176 
177 typedef void (tBTIF_CBACK) (UINT16 event, char *p_param);
178 typedef void (tBTIF_COPY_CBACK) (UINT16 event, char *p_dest, char *p_src);
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 */
192     UINT16               event;   /* message event id */
193     char __attribute__ ((aligned)) p_param[]; /* parameter area needs to be last */
194 } tBTIF_CONTEXT_SWITCH_CBACK;
195 
196 
197 /*******************************************************************************
198 **  Functions
199 ********************************************************************************/
200 
201 bt_status_t btif_transfer_context (tBTIF_CBACK *p_cback, UINT16 event, char* p_params,
202                                     int param_len, tBTIF_COPY_CBACK *p_copy_cback);
203 tBTA_SERVICE_MASK btif_get_enabled_services_mask(void);
204 bt_status_t btif_enable_service(tBTA_SERVICE_ID service_id);
205 bt_status_t btif_disable_service(tBTA_SERVICE_ID service_id);
206 int btif_is_enabled(void);
207 
208 /**
209  * BTIF_Events
210  */
211 void btif_enable_bluetooth_evt(tBTA_STATUS status);
212 void btif_disable_bluetooth_evt(void);
213 void btif_adapter_properties_evt(bt_status_t status, uint32_t num_props, bt_property_t *p_props);
214 void btif_remote_properties_evt(bt_status_t status, bt_bdaddr_t *remote_addr,
215                                    uint32_t num_props, bt_property_t *p_props);
216 
217 void btif_init_ok(UNUSED_ATTR uint16_t event, UNUSED_ATTR char *p_param);
218 
219 #endif /* BTIF_COMMON_H */
220