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 #include <hardware/bluetooth.h>
25 
26 #include "bt_types.h"
27 #include "bta_api.h"
28 #include "osi.h"
29 
30 #ifndef LOG_TAG
31 #error "LOG_TAG not defined, please add in .c file prior to including bt_common.h"
32 #endif
33 
34 #include "osi/include/log.h"
35 
36 /*******************************************************************************
37 **  Constants & Macros
38 ********************************************************************************/
39 
40 #define ASSERTC(cond, msg, val) if (!(cond)) { LOG_ERROR( \
41     "### ASSERT : %s line %d %s (%d) ###", __FILE__, __LINE__, msg, val);}
42 
43 /* Calculate start of event enumeration; id is top 8 bits of event */
44 #define BTIF_SIG_START(id)       ((id) << 8)
45 
46 /* For upstream the MSB bit is always SET */
47 #define BTIF_SIG_CB_BIT   (0x8000)
48 #define BTIF_SIG_CB_START(id)    (((id) << 8) | BTIF_SIG_CB_BIT)
49 
50 /* BTIF sub-systems */
51 #define BTIF_CORE           0
52 #define BTIF_DM             1
53 #define BTIF_HFP            2
54 #define BTIF_AV             3
55 #define BTIF_PAN            4
56 #define BTIF_HF_CLIENT      5
57 
58 extern bt_callbacks_t *bt_hal_cbacks;
59 
60 #define HAL_CBACK(P_CB, P_CBACK, ...)\
61     if (P_CB && P_CB->P_CBACK) {            \
62         BTIF_TRACE_API("HAL %s->%s", #P_CB, #P_CBACK); \
63         P_CB->P_CBACK(__VA_ARGS__);         \
64     }                                       \
65     else {                                  \
66         ASSERTC(0, "Callback is NULL", 0);  \
67     }
68 
69 /**
70  * BTIF events for requests that require context switch to btif task
71  * on downstreams path
72  */
73 enum
74 {
75     BTIF_CORE_API_START = BTIF_SIG_START(BTIF_CORE),
76     BTIF_CORE_STORAGE_NO_ACTION,
77     BTIF_CORE_STORAGE_ADAPTER_WRITE,
78     BTIF_CORE_STORAGE_ADAPTER_READ,
79     BTIF_CORE_STORAGE_ADAPTER_READ_ALL,
80     BTIF_CORE_STORAGE_REMOTE_WRITE,
81     BTIF_CORE_STORAGE_REMOTE_READ,
82     BTIF_CORE_STORAGE_REMOTE_READ_ALL,
83     BTIF_CORE_STORAGE_READ_ALL,
84     BTIF_CORE_STORAGE_NOTIFY_STATUS,
85     /* add here */
86 
87     BTIF_DM_API_START = BTIF_SIG_START(BTIF_DM),
88     BTIF_DM_ENABLE_SERVICE,
89     BTIF_DM_DISABLE_SERVICE,
90     /* add here */
91 
92     BTIF_HFP_API_START = BTIF_SIG_START(BTIF_HFP),
93     /* add here */
94 
95     BTIF_AV_API_START = BTIF_SIG_START(BTIF_AV),
96     /* add here */
97 };
98 
99 /**
100  * BTIF events for callbacks that require context switch to btif task
101  * on upstream path - Typically these would be non-BTA events
102  * that are generated by the BTIF layer.
103  */
104 enum
105 {
106     BTIF_CORE_CB_START = BTIF_SIG_CB_START(BTIF_CORE),
107     /* add here */
108 
109     BTIF_DM_CB_START = BTIF_SIG_CB_START(BTIF_DM),
110     BTIF_DM_CB_DISCOVERY_STARTED, /* Discovery has started */
111     BTIF_DM_CB_CREATE_BOND,   /* Create bond */
112     BTIF_DM_CB_REMOVE_BOND,   /*Remove bond */
113     BTIF_DM_CB_HID_REMOTE_NAME,   /* Remote name callback for HID device */
114     BTIF_DM_CB_BOND_STATE_BONDING,
115     BTIF_DM_CB_LE_TX_TEST,    /* BLE Tx Test command complete callback */
116     BTIF_DM_CB_LE_RX_TEST,    /* BLE Rx Test command complete callback */
117     BTIF_DM_CB_LE_TEST_END,   /* BLE Test mode end callback */
118 
119     BTIF_HFP_CB_START  = BTIF_SIG_CB_START(BTIF_HFP),
120     BTIF_HFP_CB_AUDIO_CONNECTING, /* HF AUDIO connect has been sent to BTA successfully */
121 
122     BTIF_PAN_CB_START = BTIF_SIG_CB_START(BTIF_PAN),
123     BTIF_PAN_CB_DISCONNECTING, /* PAN Disconnect has been sent to BTA successfully */
124 
125     BTIF_HF_CLIENT_CLIENT_CB_START  = BTIF_SIG_CB_START(BTIF_HF_CLIENT),
126     BTIF_HF_CLIENT_CB_AUDIO_CONNECTING, /* AUDIO connect has been sent to BTA successfully */
127 };
128 
129 /* Macro definitions for BD ADDR persistence */
130 
131 /**
132  * PROPERTY_BT_BDADDR_PATH
133  * The property key stores the storage location of Bluetooth Device Address
134  */
135 #ifndef PROPERTY_BT_BDADDR_PATH
136 #define PROPERTY_BT_BDADDR_PATH         "ro.bt.bdaddr_path"
137 #endif
138 
139 /**
140  * PERSIST_BDADDR_PROPERTY
141  * If there is no valid bdaddr available from PROPERTY_BT_BDADDR_PATH,
142  * generating a random BDADDR and keeping it in the PERSIST_BDADDR_DROP.
143  */
144 #ifndef PERSIST_BDADDR_PROPERTY
145 #define PERSIST_BDADDR_PROPERTY         "persist.service.bdroid.bdaddr"
146 #endif
147 
148 #define FACTORY_BT_BDADDR_STORAGE_LEN   17
149 
150 
151 /*******************************************************************************
152 **  Type definitions for callback functions
153 ********************************************************************************/
154 
155 typedef void (tBTIF_CBACK) (UINT16 event, char *p_param);
156 typedef void (tBTIF_COPY_CBACK) (UINT16 event, char *p_dest, char *p_src);
157 
158 
159 /*******************************************************************************
160 **  Type definitions and return values
161 ********************************************************************************/
162 
163 /* this type handles all btif context switches between BTU and HAL */
164 typedef struct
165 {
166     BT_HDR               hdr;
167     tBTIF_CBACK*         p_cb;    /* context switch callback */
168 
169     /* parameters passed to callback */
170     UINT16               event;   /* message event id */
171     char                 p_param[0]; /* parameter area needs to be last */
172 } tBTIF_CONTEXT_SWITCH_CBACK;
173 
174 
175 /*******************************************************************************
176 **  Functions
177 ********************************************************************************/
178 
179 bt_status_t btif_transfer_context (tBTIF_CBACK *p_cback, UINT16 event, char* p_params,
180                                     int param_len, tBTIF_COPY_CBACK *p_copy_cback);
181 tBTA_SERVICE_MASK btif_get_enabled_services_mask(void);
182 bt_status_t btif_enable_service(tBTA_SERVICE_ID service_id);
183 bt_status_t btif_disable_service(tBTA_SERVICE_ID service_id);
184 int btif_is_enabled(void);
185 
186 /**
187  * BTIF_Events
188  */
189 void btif_enable_bluetooth_evt(tBTA_STATUS status);
190 void btif_disable_bluetooth_evt(void);
191 void btif_adapter_properties_evt(bt_status_t status, uint32_t num_props, bt_property_t *p_props);
192 void btif_remote_properties_evt(bt_status_t status, bt_bdaddr_t *remote_addr,
193                                    uint32_t num_props, bt_property_t *p_props);
194 
195 void btif_init_ok(UNUSED_ATTR uint16_t event, UNUSED_ATTR char *p_param);
196 void btif_init_fail(UNUSED_ATTR uint16_t event, UNUSED_ATTR char *p_param);
197 
198 #endif /* BTIF_COMMON_H */
199