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/strings/stringprintf.h>
28 #include <bluetooth/log.h>
29 
30 #include <chrono>
31 #include <cstdint>
32 #include <string>
33 
34 #include "internal_include/bt_target.h"
35 #include "macros.h"
36 #include "osi/include/alarm.h"
37 #include "stack/include/bt_hdr.h"
38 #include "stack/include/hci_error_code.h"
39 #include "types/bluetooth/uuid.h"
40 #include "types/hci_role.h"
41 #include "types/raw_address.h"
42 
43 /*****************************************************************************
44  *  Constants and data types
45  ****************************************************************************/
46 
47 /* vendor specific event handler function type */
48 typedef bool(tBTA_SYS_VS_EVT_HDLR)(uint16_t evt, void* p);
49 
50 /* event handler function type */
51 typedef bool(tBTA_SYS_EVT_HDLR)(const BT_HDR_RIGID* p_msg);
52 static_assert(
53     sizeof(BT_HDR) == sizeof(BT_HDR_RIGID),
54     "Rigid replacement should be same size struct with flexible member");
55 
56 /* disable function type */
57 typedef void(tBTA_SYS_DISABLE)(void);
58 
59 template <typename T, typename U>
Specialize(U * u)60 inline const T* Specialize(U* u) {
61   return const_cast<const T*>(reinterpret_cast<T*>(u));
62 }
63 
64 #ifndef BTA_DM_NUM_JV_ID
65 #define BTA_DM_NUM_JV_ID 2
66 #endif
67 
68 typedef enum : uint8_t {
69   /* SW sub-systems */
70   BTA_ID_SYS = 0,    /* system manager */
71                      /* BLUETOOTH PART - from = 0, to BTA_ID_BLUETOOTH_MAX */
72   BTA_ID_DM_SEC = 3, /* device manager security */
73   BTA_ID_DG = 4,     /* data gateway */
74   BTA_ID_AG = 5,     /* audio gateway */
75   BTA_ID_OPC = 6,    /* object push client */
76   BTA_ID_OPS = 7,    /* object push server */
77   BTA_ID_FTS = 8,    /* file transfer server */
78   BTA_ID_CT = 9,     /* cordless telephony terminal */
79   BTA_ID_FTC = 10,   /* file transfer client */
80   BTA_ID_SS = 11,    /* synchronization server */
81   BTA_ID_PR = 12,    /* Printer client */
82   BTA_ID_BIC = 13,   /* Basic Imaging Client */
83   BTA_ID_PAN = 14,   /* Personal Area Networking */
84   BTA_ID_BIS = 15,   /* Basic Imaging Server */
85   BTA_ID_ACC = 16,   /* Advanced Camera Client */
86   BTA_ID_SC = 17,    /* SIM Card Access server */
87   BTA_ID_AV = 18,    /* Advanced audio/video */
88   BTA_ID_AVK = 19,   /* Audio/video sink */
89   BTA_ID_HD = 20,    /* HID Device */
90   BTA_ID_CG = 21,    /* Cordless Gateway */
91   BTA_ID_BP = 22,    /* Basic Printing Client */
92   BTA_ID_HH = 23,    /* Human Interface Device Host */
93   BTA_ID_PBS = 24,   /* Phone Book Access Server */
94   BTA_ID_PBC = 25,   /* Phone Book Access Client */
95   BTA_ID_JV = 26,    /* Java */
96   BTA_ID_HS = 27,    /* Headset */
97   BTA_ID_MSE = 28,   /* Message Server Equipment */
98   BTA_ID_MCE = 29,   /* Message Client Equipment */
99   BTA_ID_HL = 30,    /* Health Device Profile*/
100   BTA_ID_GATTC = 31, /* GATT Client */
101   BTA_ID_GATTS = 32, /* GATT Client */
102   BTA_ID_SDP = 33,   /* SDP Client */
103   BTA_ID_BLUETOOTH_MAX = 34, /* last BT profile */
104 
105   BTA_ID_MAX = (44 + BTA_DM_NUM_JV_ID),
106 } tBTA_SYS_ID;
107 
BtaIdSysText(const tBTA_SYS_ID & sys_id)108 inline std::string BtaIdSysText(const tBTA_SYS_ID& sys_id) {
109   switch (sys_id) {
110     CASE_RETURN_TEXT(BTA_ID_SYS);
111     CASE_RETURN_TEXT(BTA_ID_DM_SEC);
112     CASE_RETURN_TEXT(BTA_ID_DG);
113     CASE_RETURN_TEXT(BTA_ID_AG);
114     CASE_RETURN_TEXT(BTA_ID_OPC);
115     CASE_RETURN_TEXT(BTA_ID_OPS);
116     CASE_RETURN_TEXT(BTA_ID_FTS);
117     CASE_RETURN_TEXT(BTA_ID_CT);
118     CASE_RETURN_TEXT(BTA_ID_FTC);
119     CASE_RETURN_TEXT(BTA_ID_SS);
120     CASE_RETURN_TEXT(BTA_ID_PR);
121     CASE_RETURN_TEXT(BTA_ID_BIC);
122     CASE_RETURN_TEXT(BTA_ID_PAN);
123     CASE_RETURN_TEXT(BTA_ID_BIS);
124     CASE_RETURN_TEXT(BTA_ID_ACC);
125     CASE_RETURN_TEXT(BTA_ID_SC);
126     CASE_RETURN_TEXT(BTA_ID_AV);
127     CASE_RETURN_TEXT(BTA_ID_AVK);
128     CASE_RETURN_TEXT(BTA_ID_HD);
129     CASE_RETURN_TEXT(BTA_ID_CG);
130     CASE_RETURN_TEXT(BTA_ID_BP);
131     CASE_RETURN_TEXT(BTA_ID_HH);
132     CASE_RETURN_TEXT(BTA_ID_PBS);
133     CASE_RETURN_TEXT(BTA_ID_PBC);
134     CASE_RETURN_TEXT(BTA_ID_JV);
135     CASE_RETURN_TEXT(BTA_ID_HS);
136     CASE_RETURN_TEXT(BTA_ID_MSE);
137     CASE_RETURN_TEXT(BTA_ID_MCE);
138     CASE_RETURN_TEXT(BTA_ID_HL);
139     CASE_RETURN_TEXT(BTA_ID_GATTC);
140     CASE_RETURN_TEXT(BTA_ID_GATTS);
141     CASE_RETURN_TEXT(BTA_ID_SDP);
142     CASE_RETURN_TEXT(BTA_ID_BLUETOOTH_MAX);
143     default:
144       return base::StringPrintf("Unknown[%hhu]", sys_id);
145   }
146 }
147 
148 typedef enum : uint8_t {
149   BTA_SYS_CONN_OPEN = 0x00,
150   BTA_SYS_CONN_CLOSE = 0x01,
151   BTA_SYS_APP_OPEN = 0x02,
152   BTA_SYS_APP_CLOSE = 0x03,
153   BTA_SYS_SCO_OPEN = 0x04,
154   BTA_SYS_SCO_CLOSE = 0x05,
155   BTA_SYS_CONN_IDLE = 0x06,
156   BTA_SYS_CONN_BUSY = 0x07,
157   BTA_SYS_ROLE_CHANGE = 0x14, /* role change */
158 } tBTA_SYS_CONN_STATUS;
159 
bta_sys_conn_status_text(tBTA_SYS_CONN_STATUS status)160 inline std::string bta_sys_conn_status_text(tBTA_SYS_CONN_STATUS status) {
161   switch (status) {
162     case BTA_SYS_CONN_OPEN:
163       return std::string("BTA_SYS_CONN_OPEN");
164     case BTA_SYS_CONN_CLOSE:
165       return std::string("BTA_SYS_CONN_CLOSE");
166     case BTA_SYS_APP_OPEN:
167       return std::string("BTA_SYS_APP_OPEN");
168     case BTA_SYS_APP_CLOSE:
169       return std::string("BTA_SYS_APP_CLOSE");
170     case BTA_SYS_SCO_OPEN:
171       return std::string("BTA_SYS_SCO_OPEN");
172     case BTA_SYS_SCO_CLOSE:
173       return std::string("BTA_SYS_SCO_CLOSE");
174     case BTA_SYS_CONN_IDLE:
175       return std::string("BTA_SYS_CONN_IDLE");
176     case BTA_SYS_CONN_BUSY:
177       return std::string("BTA_SYS_CONN_BUSY");
178     case BTA_SYS_ROLE_CHANGE:
179       return std::string("BTA_SYS_ROLE_CHANGE");
180     default:
181       return std::string("UNKNOWN");
182   }
183 }
184 
185 /* conn callback for power mode manager */
186 typedef void(tBTA_SYS_CONN_CBACK)(tBTA_SYS_CONN_STATUS status,
187                                   const tBTA_SYS_ID id, uint8_t app_id,
188                                   const RawAddress& peer_addr);
189 /* conn callback for sco change */
190 typedef void(tBTA_SYS_CONN_SCO_CBACK)(tBTA_SYS_CONN_STATUS status,
191                                       uint8_t num_sco_links, uint8_t app_id,
192                                       const RawAddress& peer_addr);
193 /* callback for role switch */
194 typedef void(tBTA_SYS_ROLE_SWITCH_CBACK)(tBTA_SYS_CONN_STATUS status,
195                                          tHCI_ROLE new_role,
196                                          tHCI_STATUS hci_status,
197                                          const RawAddress& peer_addr);
198 /* callback for sniff subrating updates */
199 typedef void(tBTA_SYS_SSR_CFG_CBACK)(uint8_t id, uint8_t app_id,
200                                      uint16_t latency, uint16_t tout);
201 
202 typedef void(tBTA_SYS_SNIFF_CBACK)(uint8_t id, uint8_t app_id,
203                                    const RawAddress& peer_addr);
204 
205 typedef struct {
206   bluetooth::Uuid custom_uuid;
207   uint32_t handle;
208 } tBTA_CUSTOM_UUID;
209 
210 /* eir callback for adding/removeing UUID */
211 typedef void(tBTA_SYS_EIR_CBACK)(uint16_t uuid16, bool adding);
212 typedef void(tBTA_SYS_CUST_EIR_CBACK)(const tBTA_CUSTOM_UUID& curr,
213                                       bool adding);
214 
215 /* registration structure */
216 typedef struct {
217   tBTA_SYS_EVT_HDLR* evt_hdlr;
218   tBTA_SYS_DISABLE* disable;
219 } tBTA_SYS_REG;
220 
221 /*****************************************************************************
222  *  Macros
223  ****************************************************************************/
224 /* Calculate start of event enumeration; id is top 8 bits of event */
225 #define BTA_SYS_EVT_START(id) ((id) << 8)
226 
227 /*****************************************************************************
228  *  Function declarations
229  ****************************************************************************/
230 void bta_sys_init(void);
231 void bta_sys_register(uint8_t id, const tBTA_SYS_REG* p_reg);
232 void bta_sys_deregister(uint8_t id);
233 bool bta_sys_is_register(uint8_t id);
234 void bta_sys_sendmsg(void* p_msg);
235 void bta_sys_sendmsg_delayed(void* p_msg, std::chrono::microseconds delay);
236 void bta_sys_start_timer(alarm_t* alarm, uint64_t interval_ms, uint16_t event,
237                          uint16_t layer_specific);
238 void bta_sys_disable();
239 
240 void bta_sys_rm_register(tBTA_SYS_CONN_CBACK* p_cback);
241 void bta_sys_pm_register(tBTA_SYS_CONN_CBACK* p_cback);
242 void bta_sys_sniff_register(tBTA_SYS_SNIFF_CBACK* p_cback);
243 void bta_sys_sco_register(tBTA_SYS_CONN_SCO_CBACK* p_cback);
244 
245 void bta_sys_conn_open(tBTA_SYS_ID id, uint8_t app_id,
246                        const RawAddress& peer_addr);
247 void bta_sys_conn_close(tBTA_SYS_ID id, uint8_t app_id,
248                         const RawAddress& peer_addr);
249 void bta_sys_app_open(tBTA_SYS_ID id, uint8_t app_id,
250                       const RawAddress& peer_addr);
251 void bta_sys_app_close(tBTA_SYS_ID id, uint8_t app_id,
252                        const RawAddress& peer_addr);
253 void bta_sys_sco_open(tBTA_SYS_ID id, uint8_t app_id,
254                       const RawAddress& peer_addr);
255 void bta_sys_sco_close(tBTA_SYS_ID id, uint8_t app_id,
256                        const RawAddress& peer_addr);
257 void bta_sys_sco_use(tBTA_SYS_ID id, uint8_t app_id,
258                      const RawAddress& peer_addr);
259 void bta_sys_sco_unuse(tBTA_SYS_ID id, uint8_t app_id,
260                        const RawAddress& peer_addr);
261 void bta_sys_idle(tBTA_SYS_ID id, uint8_t app_id, const RawAddress& peer_addr);
262 void bta_sys_busy(tBTA_SYS_ID id, uint8_t app_id, const RawAddress& peer_addr);
263 void bta_sys_reset_sniff(uint8_t id, uint8_t app_id,
264                          const RawAddress& peer_addr);
265 
266 void bta_sys_role_chg_register(tBTA_SYS_ROLE_SWITCH_CBACK* p_cback);
267 void bta_sys_notify_role_chg(const RawAddress& peer_addr, tHCI_ROLE new_role,
268                              tHCI_STATUS hci_status);
269 void bta_sys_collision_register(tBTA_SYS_ID bta_id,
270                                 tBTA_SYS_CONN_CBACK* p_cback);
271 void bta_sys_notify_collision(const RawAddress& peer_addr);
272 
273 void bta_sys_eir_register(tBTA_SYS_EIR_CBACK* p_cback);
274 void bta_sys_eir_unregister();
275 void bta_sys_add_uuid(uint16_t uuid16);
276 void bta_sys_remove_uuid(uint16_t uuid16);
277 void bta_sys_cust_eir_register(tBTA_SYS_CUST_EIR_CBACK* p_cback);
278 void bta_sys_add_cust_uuid(const tBTA_CUSTOM_UUID& curr);
279 void bta_sys_remove_cust_uuid(const tBTA_CUSTOM_UUID& curr);
280 
281 namespace fmt {
282 template <>
283 struct formatter<tBTA_SYS_ID> : enum_formatter<tBTA_SYS_ID> {};
284 template <>
285 struct formatter<tBTA_SYS_CONN_STATUS> : enum_formatter<tBTA_SYS_CONN_STATUS> {
286 };
287 }  // namespace fmt
288 
289 #endif /* BTA_SYS_H */
290