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  *  Routes connection status callbacks from various sub systems to DM
22  *
23  ******************************************************************************/
24 
25 #include <stddef.h>
26 
27 #include "bt_common.h"
28 #include "bta_api.h"
29 #include "bta_sys.h"
30 #include "bta_sys_int.h"
31 #include "osi/include/osi.h"
32 #include "utl.h"
33 
34 /*******************************************************************************
35  *
36  * Function         bta_sys_rm_register
37  *
38  * Description      Called by BTA DM to register role management callbacks
39  *
40  *
41  * Returns          void
42  *
43  ******************************************************************************/
bta_sys_rm_register(tBTA_SYS_CONN_CBACK * p_cback)44 void bta_sys_rm_register(tBTA_SYS_CONN_CBACK* p_cback) {
45   bta_sys_cb.prm_cb = p_cback;
46 }
47 
48 /*******************************************************************************
49  *
50  * Function         bta_sys_policy_register
51  *
52  * Description      Called by BTA DM to register link policy change callbacks
53  *
54  *
55  * Returns          void
56  *
57  ******************************************************************************/
bta_sys_policy_register(tBTA_SYS_CONN_CBACK * p_cback)58 void bta_sys_policy_register(tBTA_SYS_CONN_CBACK* p_cback) {
59   bta_sys_cb.p_policy_cb = p_cback;
60 }
61 
62 /*******************************************************************************
63  *
64  * Function         bta_sys_role_chg_register
65  *
66  * Description      Called by BTA AV to register role change callbacks
67  *
68  *
69  * Returns          void
70  *
71  ******************************************************************************/
bta_sys_role_chg_register(tBTA_SYS_CONN_CBACK * p_cback)72 void bta_sys_role_chg_register(tBTA_SYS_CONN_CBACK* p_cback) {
73   bta_sys_cb.p_role_cb = p_cback;
74 }
75 /*******************************************************************************
76  *
77  * Function         bta_sys_ssr_cfg_register
78  *
79  * Description      Called by BTA DM to register SSR configuration callback
80  *
81  *
82  * Returns          void
83  *
84  ******************************************************************************/
85 #if (BTM_SSR_INCLUDED == TRUE)
bta_sys_ssr_cfg_register(tBTA_SYS_SSR_CFG_CBACK * p_cback)86 void bta_sys_ssr_cfg_register(tBTA_SYS_SSR_CFG_CBACK* p_cback) {
87   bta_sys_cb.p_ssr_cb = p_cback;
88 }
89 #endif
90 /*******************************************************************************
91  *
92  * Function         bta_sys_role_chg_register
93  *
94  * Description      Called by BTA AV to register role change callbacks
95  *
96  *
97  * Returns          void
98  *
99  ******************************************************************************/
bta_sys_notify_role_chg(const RawAddress & peer_addr,uint8_t new_role,uint8_t hci_status)100 void bta_sys_notify_role_chg(const RawAddress& peer_addr, uint8_t new_role,
101                              uint8_t hci_status) {
102   APPL_TRACE_DEBUG("%s: peer %s new_role:%d hci_status:0x%x", __func__,
103                    peer_addr.ToString().c_str(), new_role, hci_status);
104   if (bta_sys_cb.p_role_cb) {
105     bta_sys_cb.p_role_cb(BTA_SYS_ROLE_CHANGE, new_role, hci_status, peer_addr);
106   }
107 }
108 
109 /*******************************************************************************
110  *
111  * Function         bta_sys_collision_register
112  *
113  * Description      Called by any BTA module to register for collision event.
114  *
115  *
116  * Returns          void
117  *
118  ******************************************************************************/
bta_sys_collision_register(uint8_t bta_id,tBTA_SYS_CONN_CBACK * p_cback)119 void bta_sys_collision_register(uint8_t bta_id, tBTA_SYS_CONN_CBACK* p_cback) {
120   uint8_t index;
121 
122   for (index = 0; index < MAX_COLLISION_REG; index++) {
123     if ((bta_sys_cb.colli_reg.id[index] == bta_id) ||
124         (bta_sys_cb.colli_reg.id[index] == 0)) {
125       bta_sys_cb.colli_reg.id[index] = bta_id;
126       bta_sys_cb.colli_reg.p_coll_cback[index] = p_cback;
127       return;
128     }
129   }
130 }
131 
132 /*******************************************************************************
133  *
134  * Function         bta_sys_notify_collision
135  *
136  * Description      Called by BTA DM to notify collision event.
137  *
138  *
139  * Returns          void
140  *
141  ******************************************************************************/
bta_sys_notify_collision(const RawAddress & peer_addr)142 void bta_sys_notify_collision(const RawAddress& peer_addr) {
143   uint8_t index;
144 
145   for (index = 0; index < MAX_COLLISION_REG; index++) {
146     if ((bta_sys_cb.colli_reg.id[index] != 0) &&
147         (bta_sys_cb.colli_reg.p_coll_cback[index] != NULL)) {
148       bta_sys_cb.colli_reg.p_coll_cback[index](0, BTA_ID_SYS, 0, peer_addr);
149     }
150   }
151 }
152 
153 /*******************************************************************************
154  *
155  * Function         bta_sys_sco_register
156  *
157  * Description      Called by BTA AV to register sco connection change callbacks
158  *
159  *
160  * Returns          void
161  *
162  ******************************************************************************/
bta_sys_sco_register(tBTA_SYS_CONN_CBACK * p_cback)163 void bta_sys_sco_register(tBTA_SYS_CONN_CBACK* p_cback) {
164   bta_sys_cb.p_sco_cb = p_cback;
165 }
166 
167 /*******************************************************************************
168  *
169  * Function         bta_sys_pm_register
170  *
171  * Description      Called by BTA DM to register power management callbacks
172  *
173  *
174  * Returns          void
175  *
176  ******************************************************************************/
bta_sys_pm_register(tBTA_SYS_CONN_CBACK * p_cback)177 void bta_sys_pm_register(tBTA_SYS_CONN_CBACK* p_cback) {
178   bta_sys_cb.ppm_cb = p_cback;
179 }
180 
181 /*******************************************************************************
182  *
183  * Function         bta_sys_conn_open
184  *
185  * Description      Called by BTA subsystems when a connection is made to
186  *                  the service
187  *
188  *
189  * Returns          void
190  *
191  ******************************************************************************/
bta_sys_conn_open(uint8_t id,uint8_t app_id,const RawAddress & peer_addr)192 void bta_sys_conn_open(uint8_t id, uint8_t app_id,
193                        const RawAddress& peer_addr) {
194   if (bta_sys_cb.prm_cb) {
195     bta_sys_cb.prm_cb(BTA_SYS_CONN_OPEN, id, app_id, peer_addr);
196   }
197 
198   if (bta_sys_cb.ppm_cb) {
199     bta_sys_cb.ppm_cb(BTA_SYS_CONN_OPEN, id, app_id, peer_addr);
200   }
201 }
202 
203 /*******************************************************************************
204  *
205  * Function         bta_sys_conn_close
206  *
207  * Description      Called by BTA subsystems when a connection to the service
208  *                  is closed
209  *
210  *
211  * Returns          void
212  *
213  ******************************************************************************/
bta_sys_conn_close(uint8_t id,uint8_t app_id,const RawAddress & peer_addr)214 void bta_sys_conn_close(uint8_t id, uint8_t app_id,
215                         const RawAddress& peer_addr) {
216   if (bta_sys_cb.prm_cb) {
217     bta_sys_cb.prm_cb(BTA_SYS_CONN_CLOSE, id, app_id, peer_addr);
218   }
219 
220   if (bta_sys_cb.ppm_cb) {
221     bta_sys_cb.ppm_cb(BTA_SYS_CONN_CLOSE, id, app_id, peer_addr);
222   }
223 }
224 
225 /*******************************************************************************
226  *
227  * Function         bta_sys_app_open
228  *
229  * Description      Called by BTA subsystems when application initiates
230  *                  connection to a peer device
231  *
232  *
233  * Returns          void
234  *
235  ******************************************************************************/
bta_sys_app_open(uint8_t id,uint8_t app_id,const RawAddress & peer_addr)236 void bta_sys_app_open(uint8_t id, uint8_t app_id, const RawAddress& peer_addr) {
237   if (bta_sys_cb.ppm_cb) {
238     bta_sys_cb.ppm_cb(BTA_SYS_APP_OPEN, id, app_id, peer_addr);
239   }
240 }
241 
242 /*******************************************************************************
243  *
244  * Function         bta_sys_app_close
245  *
246  * Description      Called by BTA subsystems when application initiates close
247  *                  of connection to peer device
248  *
249  * Returns          void
250  *
251  ******************************************************************************/
bta_sys_app_close(uint8_t id,uint8_t app_id,const RawAddress & peer_addr)252 void bta_sys_app_close(uint8_t id, uint8_t app_id,
253                        const RawAddress& peer_addr) {
254   if (bta_sys_cb.ppm_cb) {
255     bta_sys_cb.ppm_cb(BTA_SYS_APP_CLOSE, id, app_id, peer_addr);
256   }
257 }
258 
259 /*******************************************************************************
260  *
261  * Function         bta_sys_sco_open
262  *
263  * Description      Called by BTA subsystems when sco connection for that
264  *                  service is open
265  *
266  * Returns          void
267  *
268  ******************************************************************************/
bta_sys_sco_open(uint8_t id,uint8_t app_id,const RawAddress & peer_addr)269 void bta_sys_sco_open(uint8_t id, uint8_t app_id, const RawAddress& peer_addr) {
270   /* AG triggers p_sco_cb by bta_sys_sco_use. */
271   if ((id != BTA_ID_AG) && (bta_sys_cb.p_sco_cb)) {
272     /* without querying BTM_GetNumScoLinks() */
273     bta_sys_cb.p_sco_cb(BTA_SYS_SCO_OPEN, 1, app_id, peer_addr);
274   }
275 
276   if (bta_sys_cb.ppm_cb) {
277     bta_sys_cb.ppm_cb(BTA_SYS_SCO_OPEN, id, app_id, peer_addr);
278   }
279 }
280 
281 /*******************************************************************************
282  *
283  * Function         bta_sys_sco_close
284  *
285  * Description      Called by BTA subsystems when sco connection for that
286  *                  service is closed
287  *
288  * Returns          void
289  *
290  ******************************************************************************/
bta_sys_sco_close(uint8_t id,uint8_t app_id,const RawAddress & peer_addr)291 void bta_sys_sco_close(uint8_t id, uint8_t app_id,
292                        const RawAddress& peer_addr) {
293   uint8_t num_sco_links;
294 
295   if ((id != BTA_ID_AG) && (bta_sys_cb.p_sco_cb)) {
296     num_sco_links = BTM_GetNumScoLinks();
297     bta_sys_cb.p_sco_cb(BTA_SYS_SCO_CLOSE, num_sco_links, app_id, peer_addr);
298   }
299 
300   if (bta_sys_cb.ppm_cb) {
301     bta_sys_cb.ppm_cb(BTA_SYS_SCO_CLOSE, id, app_id, peer_addr);
302   }
303 }
304 
305 /*******************************************************************************
306  *
307  * Function         bta_sys_sco_use
308  *
309  * Description      Called by BTA subsystems when that service needs to use sco.
310  *
311  *
312  * Returns          void
313  *
314  ******************************************************************************/
bta_sys_sco_use(UNUSED_ATTR uint8_t id,uint8_t app_id,const RawAddress & peer_addr)315 void bta_sys_sco_use(UNUSED_ATTR uint8_t id, uint8_t app_id,
316                      const RawAddress& peer_addr) {
317   /* AV streaming need to be suspended before SCO is connected. */
318   if (bta_sys_cb.p_sco_cb) {
319     /* without querying BTM_GetNumScoLinks() */
320     bta_sys_cb.p_sco_cb(BTA_SYS_SCO_OPEN, 1, app_id, peer_addr);
321   }
322 }
323 
324 /*******************************************************************************
325  *
326  * Function         bta_sys_sco_unuse
327  *
328  * Description      Called by BTA subsystems when sco connection for that
329  *                  service is no longer needed.
330  *
331  * Returns          void
332  *
333  ******************************************************************************/
bta_sys_sco_unuse(uint8_t id,uint8_t app_id,const RawAddress & peer_addr)334 void bta_sys_sco_unuse(uint8_t id, uint8_t app_id,
335                        const RawAddress& peer_addr) {
336   if ((bta_sys_cb.p_sco_cb)) {
337     uint8_t num_sco_links = BTM_GetNumScoLinks();
338     bta_sys_cb.p_sco_cb(BTA_SYS_SCO_CLOSE, num_sco_links, app_id, peer_addr);
339   }
340 }
341 /*******************************************************************************
342  *
343  * Function         bta_sys_chg_ssr_config
344  *
345  * Description      Called by BTA subsystems to indicate that the given app SSR
346  *                  setting needs to be changed.
347  *
348  * Returns          void
349  *
350  ******************************************************************************/
351 #if (BTM_SSR_INCLUDED == TRUE)
bta_sys_chg_ssr_config(uint8_t id,uint8_t app_id,uint16_t max_latency,uint16_t min_tout)352 void bta_sys_chg_ssr_config(uint8_t id, uint8_t app_id, uint16_t max_latency,
353                             uint16_t min_tout) {
354   if (bta_sys_cb.p_ssr_cb) {
355     bta_sys_cb.p_ssr_cb(id, app_id, max_latency, min_tout);
356   }
357 }
358 #endif
359 /*******************************************************************************
360  *
361  * Function         bta_sys_set_policy
362  *
363  * Description      Called by BTA subsystems to indicate that the given link
364  *                  policy to peer device should be set
365  *
366  * Returns          void
367  *
368  ******************************************************************************/
bta_sys_set_policy(uint8_t id,uint8_t policy,const RawAddress & peer_addr)369 void bta_sys_set_policy(uint8_t id, uint8_t policy,
370                         const RawAddress& peer_addr) {
371   APPL_TRACE_DEBUG("%s: peer %s id:%d policy:0x%x", __func__,
372                    peer_addr.ToString().c_str(), id, policy);
373   if (bta_sys_cb.p_policy_cb) {
374     bta_sys_cb.p_policy_cb(BTA_SYS_PLCY_SET, id, policy, peer_addr);
375   }
376 }
377 
378 /*******************************************************************************
379  *
380  * Function         bta_sys_clear_policy
381  *
382  * Description      Called by BTA subsystems to indicate that the given link
383  *                  policy to peer device should be clear
384  *
385  * Returns          void
386  *
387  ******************************************************************************/
bta_sys_clear_policy(uint8_t id,uint8_t policy,const RawAddress & peer_addr)388 void bta_sys_clear_policy(uint8_t id, uint8_t policy,
389                           const RawAddress& peer_addr) {
390   APPL_TRACE_DEBUG("%s: peer %s id:%d policy:0x%x", __func__,
391                    peer_addr.ToString().c_str(), id, policy);
392   if (bta_sys_cb.p_policy_cb) {
393     bta_sys_cb.p_policy_cb(BTA_SYS_PLCY_CLR, id, policy, peer_addr);
394   }
395 }
396 
397 /*******************************************************************************
398  *
399  * Function         bta_sys_set_default_policy
400  *
401  * Description      Called by BTA subsystems to indicate that the given default
402  *                  link policy should be set
403  *
404  * Returns          void
405  *
406  ******************************************************************************/
bta_sys_set_default_policy(uint8_t id,uint8_t policy)407 void bta_sys_set_default_policy(uint8_t id, uint8_t policy) {
408   APPL_TRACE_DEBUG("%s: id:%d policy:0x%x", __func__, id, policy);
409   if (bta_sys_cb.p_policy_cb) {
410     bta_sys_cb.p_policy_cb(BTA_SYS_PLCY_DEF_SET, id, policy,
411                            RawAddress::kEmpty);
412   }
413 }
414 
415 /*******************************************************************************
416  *
417  * Function         bta_sys_clear_default_policy
418  *
419  * Description      Called by BTA subsystems to indicate that the given default
420  *                  link policy should be clear
421  *
422  * Returns          void
423  *
424  ******************************************************************************/
bta_sys_clear_default_policy(uint8_t id,uint8_t policy)425 void bta_sys_clear_default_policy(uint8_t id, uint8_t policy) {
426   APPL_TRACE_DEBUG("%s: id:%d policy:0x%x", __func__, id, policy);
427   if (bta_sys_cb.p_policy_cb) {
428     bta_sys_cb.p_policy_cb(BTA_SYS_PLCY_DEF_CLR, id, policy,
429                            RawAddress::kEmpty);
430   }
431 }
432 
433 /*******************************************************************************
434  *
435  * Function         bta_sys_idle
436  *
437  * Description      Called by BTA subsystems to indicate that the connection to
438  *                  peer device is idle
439  *
440  * Returns          void
441  *
442  ******************************************************************************/
bta_sys_idle(uint8_t id,uint8_t app_id,const RawAddress & peer_addr)443 void bta_sys_idle(uint8_t id, uint8_t app_id, const RawAddress& peer_addr) {
444   if (bta_sys_cb.prm_cb) {
445     bta_sys_cb.prm_cb(BTA_SYS_CONN_IDLE, id, app_id, peer_addr);
446   }
447 
448   if (bta_sys_cb.ppm_cb) {
449     bta_sys_cb.ppm_cb(BTA_SYS_CONN_IDLE, id, app_id, peer_addr);
450   }
451 }
452 
453 /*******************************************************************************
454  *
455  * Function         bta_sys_busy
456  *
457  * Description      Called by BTA subsystems to indicate that the connection to
458  *                  peer device is busy
459  *
460  * Returns          void
461  *
462  ******************************************************************************/
bta_sys_busy(uint8_t id,uint8_t app_id,const RawAddress & peer_addr)463 void bta_sys_busy(uint8_t id, uint8_t app_id, const RawAddress& peer_addr) {
464   if (bta_sys_cb.prm_cb) {
465     bta_sys_cb.prm_cb(BTA_SYS_CONN_BUSY, id, app_id, peer_addr);
466   }
467 
468   if (bta_sys_cb.ppm_cb) {
469     bta_sys_cb.ppm_cb(BTA_SYS_CONN_BUSY, id, app_id, peer_addr);
470   }
471 }
472 
473 #if (BTA_EIR_CANNED_UUID_LIST != TRUE)
474 /*******************************************************************************
475  *
476  * Function         bta_sys_eir_register
477  *
478  * Description      Called by BTA DM to register EIR utility function that can
479  *                  be used by the other BTA modules to add/remove UUID.
480  *
481  * Returns          void
482  *
483  ******************************************************************************/
bta_sys_eir_register(tBTA_SYS_EIR_CBACK * p_cback)484 void bta_sys_eir_register(tBTA_SYS_EIR_CBACK* p_cback) {
485   bta_sys_cb.eir_cb = p_cback;
486 }
487 
488 /*******************************************************************************
489  *
490  * Function         bta_sys_add_uuid
491  *
492  * Description      Called by BTA subsystems to indicate to DM that new service
493  *                  class UUID is added.
494  *
495  * Returns          void
496  *
497  ******************************************************************************/
bta_sys_add_uuid(uint16_t uuid16)498 void bta_sys_add_uuid(uint16_t uuid16) {
499   if (bta_sys_cb.eir_cb) {
500     bta_sys_cb.eir_cb(uuid16, true);
501   }
502 }
503 
504 /*******************************************************************************
505  *
506  * Function         bta_sys_remove_uuid
507  *
508  * Description      Called by BTA subsystems to indicate to DM that the service
509  *                  class UUID is removed.
510  *
511  * Returns          void
512  *
513  ******************************************************************************/
bta_sys_remove_uuid(uint16_t uuid16)514 void bta_sys_remove_uuid(uint16_t uuid16) {
515   if (bta_sys_cb.eir_cb) {
516     bta_sys_cb.eir_cb(uuid16, false);
517   }
518 }
519 #endif
520