1 /******************************************************************************
2  *
3  *  Copyright 2003-2012 Broadcom Corporation
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 /******************************************************************************
20  *
21  *  This is the public interface file for the BTA system manager.
22  *
23  ******************************************************************************/
24 #ifndef BTA_SYS_H
25 #define BTA_SYS_H
26 
27 #include <base/time/time.h>
28 #include <cstdint>
29 
30 #include "bt_target.h"  // Must be first to define build configuration
31 
32 #include "osi/include/alarm.h"
33 #include "stack/include/hci_error_code.h"
34 #include "types/hci_role.h"
35 
36 /*****************************************************************************
37  *  Constants and data types
38  ****************************************************************************/
39 
40 /* vendor specific event handler function type */
41 typedef bool(tBTA_SYS_VS_EVT_HDLR)(uint16_t evt, void* p);
42 
43 /* event handler function type */
44 typedef bool(tBTA_SYS_EVT_HDLR)(BT_HDR_RIGID* p_msg);
45 static_assert(
46     sizeof(BT_HDR) == sizeof(BT_HDR_RIGID),
47     "Rigid replacement should be same size struct with flexible member");
48 
49 /* disable function type */
50 typedef void(tBTA_SYS_DISABLE)(void);
51 
52 template <typename T, typename U>
Specialize(U * u)53 inline const T* Specialize(U* u) {
54   return const_cast<const T*>(reinterpret_cast<T*>(u));
55 }
56 
57 #ifndef BTA_DM_NUM_JV_ID
58 #define BTA_DM_NUM_JV_ID 2
59 #endif
60 
61 /* SW sub-systems */
62 #define BTA_ID_SYS 0 /* system manager */
63 /* BLUETOOTH PART - from 0 to BTA_ID_BLUETOOTH_MAX */
64 #define BTA_ID_DM_SEARCH 2      /* device manager search */
65 #define BTA_ID_DM_SEC 3         /* device manager security */
66 #define BTA_ID_DG 4             /* data gateway */
67 #define BTA_ID_AG 5             /* audio gateway */
68 #define BTA_ID_OPC 6            /* object push client */
69 #define BTA_ID_OPS 7            /* object push server */
70 #define BTA_ID_FTS 8            /* file transfer server */
71 #define BTA_ID_CT 9             /* cordless telephony terminal */
72 #define BTA_ID_FTC 10           /* file transfer client */
73 #define BTA_ID_SS 11            /* synchronization server */
74 #define BTA_ID_PR 12            /* Printer client */
75 #define BTA_ID_BIC 13           /* Basic Imaging Client */
76 #define BTA_ID_PAN 14           /* Personal Area Networking */
77 #define BTA_ID_BIS 15           /* Basic Imaging Server */
78 #define BTA_ID_ACC 16           /* Advanced Camera Client */
79 #define BTA_ID_SC 17            /* SIM Card Access server */
80 #define BTA_ID_AV 18            /* Advanced audio/video */
81 #define BTA_ID_HD 20            /* HID Device */
82 #define BTA_ID_CG 21            /* Cordless Gateway */
83 #define BTA_ID_BP 22            /* Basic Printing Client */
84 #define BTA_ID_HH 23            /* Human Interface Device Host */
85 #define BTA_ID_PBS 24           /* Phone Book Access Server */
86 #define BTA_ID_PBC 25           /* Phone Book Access Client */
87 #define BTA_ID_JV 26            /* Java */
88 #define BTA_ID_HS 27            /* Headset */
89 #define BTA_ID_MSE 28           /* Message Server Equipment */
90 #define BTA_ID_MCE 29           /* Message Client Equipment */
91 #define BTA_ID_HL 30            /* Health Device Profile*/
92 #define BTA_ID_GATTC 31         /* GATT Client */
93 #define BTA_ID_GATTS 32         /* GATT Client */
94 #define BTA_ID_SDP 33           /* SDP Client */
95 #define BTA_ID_BLUETOOTH_MAX 34 /* last BT profile */
96 
97 #define BTA_ID_MAX (44 + BTA_DM_NUM_JV_ID)
98 
99 typedef uint8_t tBTA_SYS_ID;
100 
BtaIdSysText(tBTA_SYS_ID sys_id)101 inline std::string BtaIdSysText(tBTA_SYS_ID sys_id) {
102   switch (sys_id) {
103     case BTA_ID_DM_SEARCH:  // 2
104       return std::string("Scanner");
105     case BTA_ID_AG:  // 5
106       return std::string("Audio gateway");
107     case BTA_ID_PAN:  // 14
108       return std::string("PAN Personal area network");
109     case BTA_ID_AV:  // 18
110       return std::string("Advanced audio/video");
111     case BTA_ID_HD:  // 20
112       return std::string("HID Human interface device");
113     case BTA_ID_HH:  // 23
114       return std::string("HID Human interface host");
115     case BTA_ID_GATTC:  // 31
116       return std::string("GATT client");
117     case BTA_ID_GATTS:  // 32
118       return std::string("GATT server");
119     default:
120       return std::string("Unknown");
121   }
122 }
123 
124 typedef enum : uint8_t {
125   BTA_SYS_CONN_OPEN = 0x00,
126   BTA_SYS_CONN_CLOSE = 0x01,
127   BTA_SYS_APP_OPEN = 0x02,
128   BTA_SYS_APP_CLOSE = 0x03,
129   BTA_SYS_SCO_OPEN = 0x04,
130   BTA_SYS_SCO_CLOSE = 0x05,
131   BTA_SYS_CONN_IDLE = 0x06,
132   BTA_SYS_CONN_BUSY = 0x07,
133   BTA_SYS_ROLE_CHANGE = 0x14, /* role change */
134 } tBTA_SYS_CONN_STATUS;
135 
bta_sys_conn_status_text(tBTA_SYS_CONN_STATUS status)136 inline std::string bta_sys_conn_status_text(tBTA_SYS_CONN_STATUS status) {
137   switch (status) {
138     case BTA_SYS_CONN_OPEN:
139       return std::string("BTA_SYS_CONN_OPEN");
140     case BTA_SYS_CONN_CLOSE:
141       return std::string("BTA_SYS_CONN_CLOSE");
142     case BTA_SYS_APP_OPEN:
143       return std::string("BTA_SYS_APP_OPEN");
144     case BTA_SYS_APP_CLOSE:
145       return std::string("BTA_SYS_APP_CLOSE");
146     case BTA_SYS_SCO_OPEN:
147       return std::string("BTA_SYS_SCO_OPEN");
148     case BTA_SYS_SCO_CLOSE:
149       return std::string("BTA_SYS_SCO_CLOSE");
150     case BTA_SYS_CONN_IDLE:
151       return std::string("BTA_SYS_CONN_IDLE");
152     case BTA_SYS_CONN_BUSY:
153       return std::string("BTA_SYS_CONN_BUSY");
154     case BTA_SYS_ROLE_CHANGE:
155       return std::string("BTA_SYS_ROLE_CHANGE");
156     default:
157       return std::string("UNKNOWN");
158   }
159 }
160 
161 /* conn callback for role / low power manager*/
162 typedef void(tBTA_SYS_CONN_CBACK)(tBTA_SYS_CONN_STATUS status, uint8_t id,
163                                   uint8_t app_id, const RawAddress& peer_addr);
164 
165 /* conn callback for role / low power manager*/
166 typedef void(tBTA_SYS_SSR_CFG_CBACK)(uint8_t id, uint8_t app_id,
167                                      uint16_t latency, uint16_t tout);
168 
169 typedef struct {
170   bluetooth::Uuid custom_uuid;
171   uint32_t handle;
172 } tBTA_CUSTOM_UUID;
173 
174 #if (BTA_EIR_CANNED_UUID_LIST != TRUE)
175 /* eir callback for adding/removeing UUID */
176 typedef void(tBTA_SYS_EIR_CBACK)(uint16_t uuid16, bool adding);
177 typedef void(tBTA_SYS_CUST_EIR_CBACK)(const tBTA_CUSTOM_UUID &curr, bool adding);
178 #endif
179 
180 /* registration structure */
181 typedef struct {
182   tBTA_SYS_EVT_HDLR* evt_hdlr;
183   tBTA_SYS_DISABLE* disable;
184 } tBTA_SYS_REG;
185 
186 /*****************************************************************************
187  *  Global data
188  ****************************************************************************/
189 
190 /* trace level */
191 extern uint8_t appl_trace_level;
192 
193 /*****************************************************************************
194  *  Macros
195  ****************************************************************************/
196 /* Calculate start of event enumeration; id is top 8 bits of event */
197 #define BTA_SYS_EVT_START(id) ((id) << 8)
198 
199 /*****************************************************************************
200  *  Function declarations
201  ****************************************************************************/
202 void bta_set_forward_hw_failures(bool value);
203 void BTA_sys_signal_hw_error();
204 
205 extern void bta_sys_init(void);
206 extern void bta_sys_register(uint8_t id, const tBTA_SYS_REG* p_reg);
207 extern void bta_sys_deregister(uint8_t id);
208 extern bool bta_sys_is_register(uint8_t id);
209 extern void bta_sys_sendmsg(void* p_msg);
210 extern void bta_sys_sendmsg_delayed(void* p_msg, const base::TimeDelta& delay);
211 extern void bta_sys_start_timer(alarm_t* alarm, uint64_t interval_ms,
212                                 uint16_t event, uint16_t layer_specific);
213 extern void bta_sys_disable();
214 
215 extern void bta_sys_rm_register(tBTA_SYS_CONN_CBACK* p_cback);
216 extern void bta_sys_pm_register(tBTA_SYS_CONN_CBACK* p_cback);
217 
218 extern void bta_sys_sco_register(tBTA_SYS_CONN_CBACK* p_cback);
219 
220 extern void bta_sys_conn_open(uint8_t id, uint8_t app_id,
221                               const RawAddress& peer_addr);
222 extern void bta_sys_conn_close(uint8_t id, uint8_t app_id,
223                                const RawAddress& peer_addr);
224 extern void bta_sys_app_open(uint8_t id, uint8_t app_id,
225                              const RawAddress& peer_addr);
226 extern void bta_sys_app_close(uint8_t id, uint8_t app_id,
227                               const RawAddress& peer_addr);
228 extern void bta_sys_sco_open(uint8_t id, uint8_t app_id,
229                              const RawAddress& peer_addr);
230 extern void bta_sys_sco_close(uint8_t id, uint8_t app_id,
231                               const RawAddress& peer_addr);
232 extern void bta_sys_sco_use(uint8_t id, uint8_t app_id,
233                             const RawAddress& peer_addr);
234 extern void bta_sys_sco_unuse(uint8_t id, uint8_t app_id,
235                               const RawAddress& peer_addr);
236 extern void bta_sys_idle(uint8_t id, uint8_t app_id,
237                          const RawAddress& peer_addr);
238 extern void bta_sys_busy(uint8_t id, uint8_t app_id,
239                          const RawAddress& peer_addr);
240 
241 extern void bta_sys_ssr_cfg_register(tBTA_SYS_SSR_CFG_CBACK* p_cback);
242 extern void bta_sys_chg_ssr_config(uint8_t id, uint8_t app_id,
243                                    uint16_t max_latency, uint16_t min_tout);
244 
245 extern void bta_sys_role_chg_register(tBTA_SYS_CONN_CBACK* p_cback);
246 extern void bta_sys_notify_role_chg(const RawAddress& peer_addr,
247                                     tHCI_ROLE new_role, tHCI_STATUS hci_status);
248 extern void bta_sys_collision_register(uint8_t bta_id,
249                                        tBTA_SYS_CONN_CBACK* p_cback);
250 extern void bta_sys_notify_collision(const RawAddress& peer_addr);
251 
252 #if (BTA_EIR_CANNED_UUID_LIST != TRUE)
253 extern void bta_sys_eir_register(tBTA_SYS_EIR_CBACK* p_cback);
254 extern void bta_sys_add_uuid(uint16_t uuid16);
255 extern void bta_sys_remove_uuid(uint16_t uuid16);
256 extern void bta_sys_cust_eir_register(tBTA_SYS_CUST_EIR_CBACK* p_cback);
257 extern void bta_sys_add_cust_uuid(const tBTA_CUSTOM_UUID& curr);
258 extern void bta_sys_remove_cust_uuid(const tBTA_CUSTOM_UUID& curr);
259 #else
260 #define bta_sys_eir_register(ut)
261 #define bta_sys_add_uuid(ut)
262 #define bta_sys_remove_uuid(ut)
263 #define bta_sys_cust_eir_register(ut)
264 #define bta_sys_add_cust_uuid(ut)
265 #define bta_sys_remove_cust_uuid(ut)
266 #endif
267 
268 #endif /* BTA_SYS_H */
269