1 /******************************************************************************
2 *
3 * Copyright (C) 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 file contains the action functions for device manager state
22 * machine.
23 *
24 ******************************************************************************/
25
26 #include "gki.h"
27 #include "bd.h"
28 #include "bta_sys.h"
29 #include "bta_api.h"
30 #include "bta_dm_int.h"
31 #include "btm_api.h"
32
33 #include <string.h>
34
35
36 static void bta_dm_pm_cback(tBTA_SYS_CONN_STATUS status, UINT8 id, UINT8 app_id, BD_ADDR peer_addr);
37 static void bta_dm_pm_set_mode(BD_ADDR peer_addr, BOOLEAN timed_out );
38 static void bta_dm_pm_timer_cback(void *p_tle);
39 static void bta_dm_pm_btm_cback(BD_ADDR bd_addr, tBTM_PM_STATUS status, UINT16 value, UINT8 hci_status);
40 static BOOLEAN bta_dm_pm_park(BD_ADDR peer_addr);
41 static BOOLEAN bta_dm_pm_sniff(tBTA_DM_PEER_DEVICE *p_peer_dev, UINT8 index);
42 static BOOLEAN bta_dm_pm_is_sco_active ();
43 static void bta_dm_pm_hid_check(BOOLEAN bScoActive);
44 static void bta_dm_pm_set_sniff_policy(tBTA_DM_PEER_DEVICE *p_dev, BOOLEAN bDisable);
45
46 #if (BTM_SSR_INCLUDED == TRUE)
47 #if (defined BTA_HH_INCLUDED && BTA_HH_INCLUDED == TRUE)
48 #include "../hh/bta_hh_int.h"
49 /* BTA_DM_PM_SSR1 will be dedicated for HH SSR setting entry, no other profile can use it */
50 #define BTA_DM_PM_SSR_HH BTA_DM_PM_SSR1
51 #endif
52 static void bta_dm_pm_ssr(BD_ADDR peer_addr);
53 #endif
54
55 tBTA_DM_CONNECTED_SRVCS bta_dm_conn_srvcs;
56
57
58 /*******************************************************************************
59 **
60 ** Function bta_dm_init_pm
61 **
62 ** Description Initialises the BT low power manager
63 **
64 **
65 ** Returns void
66 **
67 *******************************************************************************/
bta_dm_init_pm(void)68 void bta_dm_init_pm(void)
69 {
70
71 memset(&bta_dm_conn_srvcs, 0x00, sizeof(bta_dm_conn_srvcs));
72
73 /* if there are no power manger entries, so not register */
74 if(p_bta_dm_pm_cfg[0].app_id != 0)
75 {
76 bta_sys_pm_register((tBTA_SYS_CONN_CBACK*)bta_dm_pm_cback);
77
78 BTM_PmRegister((BTM_PM_REG_SET | BTM_PM_REG_NOTIF), &bta_dm_cb.pm_id,
79 bta_dm_pm_btm_cback);
80 }
81
82
83 }
84
85
86 /*******************************************************************************
87 **
88 ** Function bta_dm_disable_pm
89 **
90 ** Description Disable PM
91 **
92 **
93 ** Returns void
94 **
95 *******************************************************************************/
bta_dm_disable_pm(void)96 void bta_dm_disable_pm(void)
97 {
98 UINT8 i;
99
100 bta_sys_pm_register(NULL);
101 BTM_PmRegister( BTM_PM_DEREG, &bta_dm_cb.pm_id, NULL);
102
103 /* Need to stop all active timers. */
104 for(i=0; i<BTA_DM_NUM_PM_TIMER; i++)
105 {
106 if(bta_dm_cb.pm_timer[i].in_use)
107 {
108 APPL_TRACE_DEBUG("stop dm_pm_timer:%d", i);
109 bta_sys_stop_timer(&bta_dm_cb.pm_timer[i].timer);
110 bta_dm_cb.pm_timer[i].in_use = FALSE;
111 }
112 }
113 }
114
115 /*******************************************************************************
116 **
117 ** Function bta_dm_pm_stop_timer
118 **
119 ** Description stop a PM timer
120 **
121 **
122 ** Returns void
123 **
124 *******************************************************************************/
bta_dm_pm_stop_timer(BD_ADDR peer_addr)125 static void bta_dm_pm_stop_timer(BD_ADDR peer_addr)
126 {
127 UINT8 i;
128
129 for(i=0; i<BTA_DM_NUM_PM_TIMER; i++)
130 {
131
132 if(bta_dm_cb.pm_timer[i].in_use && !bdcmp(bta_dm_cb.pm_timer[i].peer_bdaddr, peer_addr))
133 {
134 APPL_TRACE_DEBUG("stop dm_pm_timer:%d", i);
135 bta_sys_stop_timer(&bta_dm_cb.pm_timer[i].timer);
136 bta_dm_cb.pm_timer[i].in_use = FALSE;
137 break;
138 }
139
140 }
141 }
142
143 /*******************************************************************************
144 **
145 ** Function bta_dm_pm_cback
146 **
147 ** Description Conn change callback from sys for low power management
148 **
149 **
150 ** Returns void
151 **
152 *******************************************************************************/
bta_dm_pm_cback(tBTA_SYS_CONN_STATUS status,UINT8 id,UINT8 app_id,BD_ADDR peer_addr)153 static void bta_dm_pm_cback(tBTA_SYS_CONN_STATUS status, UINT8 id, UINT8 app_id, BD_ADDR peer_addr)
154 {
155
156 UINT8 i,j;
157 UINT16 policy_setting;
158 tBTM_STATUS btm_status;
159 tBTM_VERSION_INFO vers;
160 #if (BTM_SSR_INCLUDED == TRUE)
161 int index = BTA_DM_PM_SSR0;
162 #endif
163 tBTA_DM_PEER_DEVICE *p_dev;
164
165 APPL_TRACE_DEBUG("bta_dm_pm_cback: st(%d), id(%d), app(%d)", status, id, app_id);
166
167 btm_status = BTM_ReadLocalVersion (&vers);
168 p_dev = bta_dm_find_peer_device(peer_addr);
169
170 /* Disable/Enable sniff policy on the SCO link if sco Up/Down. Will be removed in 2.2*/
171 if ((p_dev) &&
172 ((status == BTA_SYS_SCO_OPEN) || (status == BTA_SYS_SCO_CLOSE)) )
173 {
174 if ((btm_status == BTM_SUCCESS) &&
175 (vers.manufacturer == LMP_COMPID_BROADCOM) &&
176 (vers.hci_version < HCI_PROTO_VERSION_2_0))
177 {
178 bta_dm_pm_set_sniff_policy(p_dev, (status == BTA_SYS_SCO_OPEN));
179 }
180 }
181
182 /* find if there is an power mode entry for the service */
183 for(i=1; i<=p_bta_dm_pm_cfg[0].app_id; i++)
184 {
185
186 if((p_bta_dm_pm_cfg[i].id == id)
187 && ((p_bta_dm_pm_cfg[i].app_id == BTA_ALL_APP_ID ) || (p_bta_dm_pm_cfg[i].app_id == app_id )))
188 break;
189
190 }
191
192 /* if no entries are there for the app_id and subystem in p_bta_dm_pm_spec*/
193 if(i> p_bta_dm_pm_cfg[0].app_id)
194 return;
195
196 bta_dm_pm_stop_timer(peer_addr);
197 /*p_dev = bta_dm_find_peer_device(peer_addr);*/
198
199 #if (BTM_SSR_INCLUDED == TRUE)
200 /* set SSR parameters on SYS CONN OPEN */
201 if((BTA_SYS_CONN_OPEN == status) && p_dev && (p_dev->info & BTA_DM_DI_USE_SSR))
202 {
203 index = p_bta_dm_pm_spec[p_bta_dm_pm_cfg[i].spec_idx].ssr;
204 }
205 #endif
206
207 /* if no action for the event */
208 if(p_bta_dm_pm_spec[p_bta_dm_pm_cfg[i].spec_idx].actn_tbl[status][0].power_mode == BTA_DM_PM_NO_ACTION)
209 {
210 #if (BTM_SSR_INCLUDED == TRUE)
211 if(BTA_DM_PM_SSR0 == index) /* and do not need to set SSR, return. */
212 #endif
213 return;
214 }
215
216 for(j=0; j<bta_dm_conn_srvcs.count ; j++)
217 {
218 /* check if an entry already present */
219 if((bta_dm_conn_srvcs.conn_srvc[j].id == id)
220 && (bta_dm_conn_srvcs.conn_srvc[j].app_id == app_id )
221 && !bdcmp(bta_dm_conn_srvcs.conn_srvc[j].peer_bdaddr, peer_addr))
222 break;
223
224 }
225
226 /* if subsystem has no more preference on the power mode remove
227 the cb */
228 if(p_bta_dm_pm_spec[p_bta_dm_pm_cfg[i].spec_idx].actn_tbl[status][0].power_mode == BTA_DM_PM_NO_PREF)
229 {
230
231 if(j != bta_dm_conn_srvcs.count)
232 {
233 bta_dm_conn_srvcs.count--;
234
235 for(; j<bta_dm_conn_srvcs.count ; j++)
236 {
237
238 memcpy(&bta_dm_conn_srvcs.conn_srvc[j], &bta_dm_conn_srvcs.conn_srvc[j+1], sizeof(bta_dm_conn_srvcs.conn_srvc[j]));
239
240 }
241 }
242 else
243 {
244 APPL_TRACE_WARNING("bta_dm_act no entry for connected service cbs");
245 return;
246 }
247 }
248 else if(j == bta_dm_conn_srvcs.count )
249 {
250 /* check if we have more connected service that cbs */
251 if(bta_dm_conn_srvcs.count == BTA_DM_NUM_CONN_SRVS)
252 {
253 APPL_TRACE_WARNING("bta_dm_act no more connected service cbs");
254 return;
255 }
256
257 /* fill in a new cb */
258 bta_dm_conn_srvcs.conn_srvc[j].id = id;
259 bta_dm_conn_srvcs.conn_srvc[j].app_id = app_id;
260 bdcpy(bta_dm_conn_srvcs.conn_srvc[j].peer_bdaddr, peer_addr);
261
262 APPL_TRACE_WARNING("new conn_srvc id:%d, app_id:%d", id, app_id);
263
264 bta_dm_conn_srvcs.count++;
265 bta_dm_conn_srvcs.conn_srvc[j].state = status;
266 }
267 else
268 {
269 /* no service is added or removed. only updating status. */
270 bta_dm_conn_srvcs.conn_srvc[j].state = status;
271 }
272
273 if(p_dev)
274 {
275 p_dev->pm_mode_attempted = 0;
276 p_dev->pm_mode_failed = 0;
277 }
278
279 #if (BTM_SSR_INCLUDED == TRUE)
280 if(p_bta_dm_ssr_spec[index].max_lat
281 #if (defined BTA_HH_INCLUDED && BTA_HH_INCLUDED == TRUE)
282 || index == BTA_DM_PM_SSR_HH
283 #endif
284 )
285 {
286 bta_dm_pm_ssr(peer_addr);
287 }
288 #endif
289
290 bta_dm_pm_set_mode(peer_addr, FALSE);
291
292 /* perform the HID link workaround if needed
293 ** 1. If SCO up/down event is received OR
294 ** 2. If HID connection open is received and SCO is already active.
295 ** This will handle the case where HID connects when SCO already active
296 */
297 if ( (btm_status == BTM_SUCCESS) &&
298 ( ((status == BTA_SYS_SCO_OPEN) || (status == BTA_SYS_SCO_CLOSE)) ||
299 ((status == BTA_SYS_CONN_OPEN) && (id == BTA_ID_HH) && bta_dm_pm_is_sco_active()) ) )
300 {
301 BOOLEAN bScoActive;
302 if (status == BTA_SYS_CONN_OPEN)
303 bScoActive = TRUE;
304 else
305 bScoActive = (status == BTA_SYS_SCO_OPEN);
306
307 bta_dm_pm_hid_check(bScoActive);
308 }
309
310 }
311
312
313 /*******************************************************************************
314 **
315 ** Function bta_dm_pm_set_mode
316 **
317 ** Description Set the power mode for the device
318 **
319 **
320 ** Returns void
321 **
322 *******************************************************************************/
bta_dm_pm_set_mode(BD_ADDR peer_addr,BOOLEAN timed_out)323 static void bta_dm_pm_set_mode(BD_ADDR peer_addr, BOOLEAN timed_out )
324 {
325
326 tBTA_DM_PM_ACTTION pm_action = BTA_DM_PM_NO_ACTION;
327 UINT16 timeout = 0;
328 UINT8 i,j;
329 tBTA_DM_PM_ACTTION failed_pm = 0;
330 tBTA_DM_PEER_DEVICE *p_peer_device = NULL;
331 tBTA_DM_PM_ACTTION allowed_modes = 0;
332 tBTA_DM_PM_ACTTION pref_modes = 0;
333 tBTA_DM_PM_CFG *p_pm_cfg;
334 tBTA_DM_PM_SPEC *p_pm_spec;
335 tBTA_DM_PM_ACTN *p_act0, *p_act1;
336 tBTA_DM_SRVCS *p_srvcs;
337
338
339 if(!bta_dm_cb.device_list.count)
340 return;
341
342 /* see if any attempt to put device in low power mode failed */
343 p_peer_device = bta_dm_find_peer_device(peer_addr);
344 /* if no peer device found return */
345 if (p_peer_device == NULL)
346 return;
347
348 failed_pm = p_peer_device->pm_mode_failed;
349
350 for(i=0; i<bta_dm_conn_srvcs.count ; i++)
351 {
352
353 p_srvcs = &bta_dm_conn_srvcs.conn_srvc[i];
354 if(!bdcmp(p_srvcs->peer_bdaddr, peer_addr))
355 {
356
357 /* p_bta_dm_pm_cfg[0].app_id is the number of entries */
358 for(j=1; j<=p_bta_dm_pm_cfg[0].app_id; j++)
359 {
360 if((p_bta_dm_pm_cfg[j].id == p_srvcs->id)
361 && ((p_bta_dm_pm_cfg[j].app_id == BTA_ALL_APP_ID ) ||
362 (p_bta_dm_pm_cfg[j].app_id == p_srvcs->app_id)))
363 break;
364 }
365
366 p_pm_cfg = &p_bta_dm_pm_cfg[j];
367 p_pm_spec = &p_bta_dm_pm_spec[p_pm_cfg->spec_idx];
368 p_act0 = &p_pm_spec->actn_tbl[p_srvcs->state][0];
369 p_act1 = &p_pm_spec->actn_tbl[p_srvcs->state][1];
370
371 APPL_TRACE_DEBUG("bta_dm_pm_set_mode: srvcsid: %d, state: %d, j: %d", p_srvcs->id, p_srvcs->state, j);
372 allowed_modes |= p_pm_spec->allow_mask;
373
374 /* PM actions are in the order of strictness */
375
376 /* first check if the first preference is ok */
377 if(!(failed_pm & p_act0->power_mode))
378 {
379 pref_modes |= p_act0->power_mode;
380
381 if(p_act0->power_mode > pm_action)
382 {
383 pm_action = p_act0->power_mode;
384 timeout = p_act0->timeout;
385
386 }
387 }
388 /* if first preference has already failed, try second preference */
389 else if(!(failed_pm & p_act1->power_mode))
390 {
391 pref_modes |= p_act1->power_mode;
392
393 if(p_act1->power_mode > pm_action)
394 {
395 pm_action = p_act1->power_mode;
396 timeout = p_act1->timeout;
397
398 }
399 }
400 }
401 }
402
403 if(pm_action & (BTA_DM_PM_PARK | BTA_DM_PM_SNIFF))
404 {
405
406 /* some service don't like the mode */
407 if(!(allowed_modes & pm_action))
408 {
409
410 /* select the other mode if its allowed and preferred, otherwise 0 which is BTA_DM_PM_NO_ACTION */
411 pm_action = (allowed_modes & (BTA_DM_PM_PARK | BTA_DM_PM_SNIFF) & pref_modes);
412
413 /* no timeout needed if no action is required */
414 if(pm_action == BTA_DM_PM_NO_ACTION)
415 {
416 timeout = 0;
417 }
418
419 }
420
421
422 }
423
424 if(!timed_out && timeout)
425 {
426
427 for(i=0; i<BTA_DM_NUM_PM_TIMER; i++)
428 {
429
430 if(!bta_dm_cb.pm_timer[i].in_use)
431 {
432 bta_dm_cb.pm_timer[i].in_use = TRUE;
433 bdcpy(bta_dm_cb.pm_timer[i].peer_bdaddr, peer_addr);
434 bta_dm_cb.pm_timer[i].timer.p_cback = bta_dm_pm_timer_cback;
435 bta_sys_start_timer(&bta_dm_cb.pm_timer[i].timer, 0, timeout);
436 APPL_TRACE_DEBUG("start dm_pm_timer:%d, %d", i, timeout);
437 return;
438
439 }
440
441 }
442
443 /* no more timers */
444 if(i==BTA_DM_NUM_PM_TIMER)
445 {
446 APPL_TRACE_WARNING("bta_dm_act dm_pm_timer no more");
447 return;
448 }
449 }
450
451 if(pm_action == BTA_DM_PM_NO_ACTION)
452 {
453
454
455 }
456 else if(pm_action == BTA_DM_PM_PARK)
457 {
458 p_peer_device->pm_mode_attempted = BTA_DM_PM_PARK;
459 bta_dm_pm_park(peer_addr);
460
461 }
462 else if(pm_action & BTA_DM_PM_SNIFF)
463 {
464 /* dont initiate SNIFF, if link_policy has it disabled */
465 if (p_peer_device->link_policy & HCI_ENABLE_SNIFF_MODE)
466 {
467 p_peer_device->pm_mode_attempted = BTA_DM_PM_SNIFF;
468 bta_dm_pm_sniff(p_peer_device, (UINT8)(pm_action & 0x0F) );
469 }
470 else
471 {
472 APPL_TRACE_DEBUG("bta_dm_pm_set_mode: Link policy disallows SNIFF, ignore request");
473 }
474 }
475 else if(pm_action == BTA_DM_PM_ACTIVE)
476 {
477
478 bta_dm_pm_active(peer_addr);
479
480 }
481
482
483 }
484
485
486 /*******************************************************************************
487 **
488 ** Function bta_ag_pm_park
489 **
490 ** Description Switch to park mode.
491 **
492 **
493 ** Returns TRUE if park attempted, FALSE otherwise.
494 **
495 *******************************************************************************/
bta_dm_pm_park(BD_ADDR peer_addr)496 static BOOLEAN bta_dm_pm_park(BD_ADDR peer_addr)
497 {
498
499 tBTM_PM_MODE mode = BTM_PM_STS_ACTIVE;
500
501 /* if not in park mode, switch to park */
502 BTM_ReadPowerMode(peer_addr, &mode);
503
504 if(mode != BTM_PM_MD_PARK)
505 {
506 BTM_SetPowerMode (bta_dm_cb.pm_id, peer_addr, &p_bta_dm_pm_md[BTA_DM_PM_PARK_IDX]);
507 }
508 return TRUE;
509
510 }
511
512
513 /*******************************************************************************
514 **
515 ** Function bta_ag_pm_sniff
516 **
517 ** Description Switch to sniff mode.
518 **
519 **
520 ** Returns TRUE if sniff attempted, FALSE otherwise.
521 **
522 *******************************************************************************/
bta_dm_pm_sniff(tBTA_DM_PEER_DEVICE * p_peer_dev,UINT8 index)523 static BOOLEAN bta_dm_pm_sniff(tBTA_DM_PEER_DEVICE *p_peer_dev, UINT8 index)
524 {
525 tBTM_PM_MODE mode = BTM_PM_STS_ACTIVE;
526 tBTM_PM_PWR_MD pwr_md;
527 tBTM_STATUS status;
528
529 BTM_ReadPowerMode(p_peer_dev->peer_bdaddr, &mode);
530
531 #if (BTM_SSR_INCLUDED == TRUE)
532 APPL_TRACE_DEBUG("bta_dm_pm_sniff cur:%d, idx:%d, info:x%x", mode, index, p_peer_dev->info);
533 if (mode != BTM_PM_MD_SNIFF ||
534 (HCI_SNIFF_SUB_RATE_SUPPORTED(BTM_ReadLocalFeatures ()) &&
535 HCI_SNIFF_SUB_RATE_SUPPORTED(BTM_ReadRemoteFeatures (p_peer_dev->peer_bdaddr)) &&
536 !(p_peer_dev->info & BTA_DM_DI_USE_SSR)))
537 #else
538 APPL_TRACE_DEBUG("bta_dm_pm_sniff cur:%d, idx:%d", mode, index);
539 if(mode != BTM_PM_MD_SNIFF)
540 #endif
541 {
542 /* if the current mode is not sniff, issue the sniff command.
543 * If sniff, but SSR is not used in this link, still issue the command */
544 memcpy(&pwr_md, &p_bta_dm_pm_md[index], sizeof (tBTM_PM_PWR_MD));
545 if (p_peer_dev->info & BTA_DM_DI_INT_SNIFF)
546 {
547 pwr_md.mode |= BTM_PM_MD_FORCE;
548 }
549 status = BTM_SetPowerMode (bta_dm_cb.pm_id, p_peer_dev->peer_bdaddr, &pwr_md);
550 if (status == BTM_CMD_STORED|| status == BTM_CMD_STARTED)
551 {
552 p_peer_dev->info &= ~(BTA_DM_DI_INT_SNIFF|BTA_DM_DI_ACP_SNIFF);
553 p_peer_dev->info |= BTA_DM_DI_SET_SNIFF;
554 }
555 else if (status == BTM_SUCCESS)
556 {
557 APPL_TRACE_DEBUG("bta_dm_pm_sniff BTM_SetPowerMode() returns BTM_SUCCESS");
558 p_peer_dev->info &= ~(BTA_DM_DI_INT_SNIFF|BTA_DM_DI_ACP_SNIFF|BTA_DM_DI_SET_SNIFF);
559 }
560 else /* error */
561 {
562 APPL_TRACE_ERROR("bta_dm_pm_sniff BTM_SetPowerMode() returns ERROR status=%d", status);
563 p_peer_dev->info &= ~(BTA_DM_DI_INT_SNIFF|BTA_DM_DI_ACP_SNIFF|BTA_DM_DI_SET_SNIFF);
564 }
565 }
566 /* else already in sniff and is using SSR, do nothing */
567
568 return TRUE;
569
570 }
571 /*******************************************************************************
572 **
573 ** Function bta_dm_pm_ssr
574 **
575 ** Description checks and sends SSR parameters
576 **
577 ** Returns void
578 **
579 *******************************************************************************/
580 #if (BTM_SSR_INCLUDED == TRUE)
bta_dm_pm_ssr(BD_ADDR peer_addr)581 static void bta_dm_pm_ssr(BD_ADDR peer_addr)
582 {
583 tBTA_DM_SSR_SPEC *p_spec, *p_spec_cur;
584 UINT8 i,j;
585 int ssr = BTA_DM_PM_SSR0;
586
587 /* go through the connected services */
588 for(i=0; i<bta_dm_conn_srvcs.count ; i++)
589 {
590 if(!bdcmp(bta_dm_conn_srvcs.conn_srvc[i].peer_bdaddr, peer_addr))
591 {
592 /* p_bta_dm_pm_cfg[0].app_id is the number of entries */
593 for(j=1; j<=p_bta_dm_pm_cfg[0].app_id; j++)
594 {
595 /* find the associated p_bta_dm_pm_cfg */
596 if((p_bta_dm_pm_cfg[j].id == bta_dm_conn_srvcs.conn_srvc[i].id)
597 && ((p_bta_dm_pm_cfg[j].app_id == BTA_ALL_APP_ID )
598 || (p_bta_dm_pm_cfg[j].app_id == bta_dm_conn_srvcs.conn_srvc[i].app_id)))
599 {
600 APPL_TRACE_WARNING("bta_dm_pm_ssr conn_srvc id:%d, app_id:%d",
601 bta_dm_conn_srvcs.conn_srvc[i].id, bta_dm_conn_srvcs.conn_srvc[i].app_id);
602 break;
603 }
604 }
605
606 /* find the ssr index with the smallest max latency. */
607 p_spec_cur = &p_bta_dm_ssr_spec[p_bta_dm_pm_spec[p_bta_dm_pm_cfg[j].spec_idx].ssr];
608 p_spec = &p_bta_dm_ssr_spec[ssr];
609
610 #if (defined BTA_HH_INCLUDED && BTA_HH_INCLUDED == TRUE)
611 /* HH has the per connection SSR preference, already read the SSR params from BTA HH */
612 if (p_bta_dm_pm_spec[p_bta_dm_pm_cfg[j].spec_idx].ssr == BTA_DM_PM_SSR_HH)
613 {
614 if (bta_hh_read_ssr_param(peer_addr, &p_spec_cur->max_lat, &p_spec_cur->min_rmt_to) == BTA_HH_ERR)
615 continue;
616 }
617 #endif
618 if (p_spec_cur->max_lat < p_spec->max_lat ||
619 (ssr == BTA_DM_PM_SSR0 && p_bta_dm_pm_spec[p_bta_dm_pm_cfg[j].spec_idx].ssr != BTA_DM_PM_SSR0))
620 {
621 ssr = p_bta_dm_pm_spec[p_bta_dm_pm_cfg[j].spec_idx].ssr;
622 }
623
624 }
625 }
626
627 p_spec = &p_bta_dm_ssr_spec[ssr];
628 APPL_TRACE_WARNING("bta_dm_pm_ssr:%d, lat:%d", ssr, p_spec->max_lat);
629 if(p_spec->max_lat)
630 {
631 /* set the SSR parameters. */
632 BTM_SetSsrParams (peer_addr, p_spec->max_lat,
633 p_spec->min_rmt_to, p_spec->min_loc_to);
634 }
635 }
636 #endif
637 /*******************************************************************************
638 **
639 ** Function bta_dm_pm_active
640 **
641 ** Description Brings connection to active mode
642 **
643 **
644 ** Returns void
645 **
646 *******************************************************************************/
bta_dm_pm_active(BD_ADDR peer_addr)647 void bta_dm_pm_active(BD_ADDR peer_addr)
648 {
649 tBTM_PM_PWR_MD pm;
650
651 memset( (void*)&pm, 0, sizeof(pm));
652
653 /* switch to active mode */
654 pm.mode = BTM_PM_MD_ACTIVE;
655 BTM_SetPowerMode (bta_dm_cb.pm_id, peer_addr, &pm);
656
657
658 }
659
660
661 /*******************************************************************************
662 **
663 ** Function bta_dm_pm_btm_cback
664 **
665 ** Description BTM power manager callback.
666 **
667 **
668 ** Returns void
669 **
670 *******************************************************************************/
bta_dm_pm_btm_cback(BD_ADDR bd_addr,tBTM_PM_STATUS status,UINT16 value,UINT8 hci_status)671 static void bta_dm_pm_btm_cback(BD_ADDR bd_addr, tBTM_PM_STATUS status, UINT16 value, UINT8 hci_status)
672 {
673 tBTA_DM_PM_BTM_STATUS *p_buf;
674
675 if ((p_buf = (tBTA_DM_PM_BTM_STATUS *) GKI_getbuf(sizeof(tBTA_DM_PM_BTM_STATUS))) != NULL)
676 {
677 p_buf->hdr.event = BTA_DM_PM_BTM_STATUS_EVT;
678 p_buf->status = status;
679 p_buf->value = value;
680 p_buf->hci_status = hci_status;
681 bdcpy(p_buf->bd_addr, bd_addr);
682 bta_sys_sendmsg(p_buf);
683 }
684 }
685
686 /*******************************************************************************
687 **
688 ** Function bta_dm_pm_timer_cback
689 **
690 ** Description Power management timer callback.
691 **
692 **
693 ** Returns void
694 **
695 *******************************************************************************/
bta_dm_pm_timer_cback(void * p_tle)696 static void bta_dm_pm_timer_cback(void *p_tle)
697 {
698 tBTA_DM_PM_TIMER *p_buf;
699 UINT8 i;
700
701 APPL_TRACE_WARNING("dm_pm_timer expires");
702
703 for(i=0; i<BTA_DM_NUM_PM_TIMER; i++)
704 {
705
706 if(bta_dm_cb.pm_timer[i].in_use)
707 {
708
709 if(&bta_dm_cb.pm_timer[i].timer == (TIMER_LIST_ENT*) p_tle)
710 {
711 APPL_TRACE_WARNING("dm_pm_timer expires %d", i);
712 bta_dm_cb.pm_timer[i].in_use = FALSE;
713 break;
714 }
715
716 }
717
718 }
719
720
721 /* no more timers */
722 if(i==BTA_DM_NUM_PM_TIMER)
723 {
724 return;
725 }
726
727 if ((p_buf = (tBTA_DM_PM_TIMER *) GKI_getbuf(sizeof(tBTA_DM_PM_TIMER))) != NULL)
728 {
729 p_buf->hdr.event = BTA_DM_PM_TIMER_EVT;
730 bdcpy(p_buf->bd_addr, bta_dm_cb.pm_timer[i].peer_bdaddr);
731 bta_sys_sendmsg(p_buf);
732 }
733 }
734
735 /*******************************************************************************
736 **
737 ** Function bta_dm_pm_btm_status
738 **
739 ** Description Process pm status event from btm
740 **
741 **
742 ** Returns void
743 **
744 *******************************************************************************/
bta_dm_pm_btm_status(tBTA_DM_MSG * p_data)745 void bta_dm_pm_btm_status(tBTA_DM_MSG *p_data)
746 {
747
748 tBTA_DM_PEER_DEVICE *p_dev;
749 tBTA_DM_DEV_INFO info;
750
751 APPL_TRACE_DEBUG("bta_dm_pm_btm_status:%d", p_data->pm_status.status);
752 p_dev = bta_dm_find_peer_device(p_data->pm_status.bd_addr);
753 if(NULL == p_dev)
754 return;
755
756 info = p_dev->info;
757 /* check new mode */
758 switch (p_data->pm_status.status)
759 {
760 case BTM_PM_STS_ACTIVE:
761 /* if our sniff or park attempt failed
762 we should not try it again*/
763 if (p_data->pm_status.hci_status != 0)
764 {
765 APPL_TRACE_ERROR("bta_dm_pm_btm_status hci_status=%d", p_data->pm_status.hci_status);
766 p_dev->info &= ~(BTA_DM_DI_INT_SNIFF|BTA_DM_DI_ACP_SNIFF|BTA_DM_DI_SET_SNIFF);
767
768 if(p_dev->pm_mode_attempted &(BTA_DM_PM_PARK | BTA_DM_PM_SNIFF))
769 {
770 p_dev->pm_mode_failed
771 |= ((BTA_DM_PM_PARK | BTA_DM_PM_SNIFF) & p_dev->pm_mode_attempted);
772 bta_dm_pm_stop_timer(p_data->pm_status.bd_addr);
773 bta_dm_pm_set_mode(p_data->pm_status.bd_addr, FALSE);
774 }
775 }
776 else
777 {
778 #if (BTM_SSR_INCLUDED == TRUE)
779 if(p_dev->prev_low)
780 {
781 /* need to send the SSR paramaters to controller again */
782 bta_dm_pm_ssr(p_dev->peer_bdaddr);
783 }
784 p_dev->prev_low = BTM_PM_STS_ACTIVE;
785 #endif
786 bta_dm_pm_stop_timer(p_data->pm_status.bd_addr);
787 bta_dm_pm_set_mode(p_data->pm_status.bd_addr, FALSE);
788 }
789 break;
790
791 #if (BTM_SSR_INCLUDED == TRUE)
792 case BTM_PM_STS_PARK:
793 case BTM_PM_STS_HOLD:
794 /* save the previous low power mode - for SSR.
795 * SSR parameters are sent to controller on "conn open".
796 * the numbers stay good until park/hold/detach */
797 if(p_dev->info & BTA_DM_DI_USE_SSR)
798 p_dev->prev_low = p_data->pm_status.status;
799 break;
800
801 case BTM_PM_STS_SSR:
802 if(p_data->pm_status.value)
803 p_dev->info |= BTA_DM_DI_USE_SSR;
804 else
805 p_dev->info &= ~BTA_DM_DI_USE_SSR;
806 break;
807 #endif
808 case BTM_PM_STS_SNIFF:
809 if (p_data->pm_status.hci_status == 0)
810 {
811 /* Stop PM timer now if already active for
812 * particular device since link is already
813 * put in sniff mode by remote device, and
814 * PM timer sole purpose is to put the link
815 * in sniff mode from host side.
816 */
817 bta_dm_pm_stop_timer(p_data->pm_status.bd_addr);
818 }
819 p_dev->info &= ~(BTA_DM_DI_SET_SNIFF|BTA_DM_DI_INT_SNIFF|BTA_DM_DI_ACP_SNIFF);
820 if (info & BTA_DM_DI_SET_SNIFF)
821 p_dev->info |= BTA_DM_DI_INT_SNIFF;
822 else
823 p_dev->info |= BTA_DM_DI_ACP_SNIFF;
824 break;
825
826 case BTM_PM_STS_ERROR:
827 p_dev->info &= ~BTA_DM_DI_SET_SNIFF;
828 break;
829
830 default:
831 break;
832 }
833
834
835
836 }
837
838 /*******************************************************************************
839 **
840 ** Function bta_dm_pm_timer
841 **
842 ** Description Process pm timer event from btm
843 **
844 **
845 ** Returns void
846 **
847 *******************************************************************************/
bta_dm_pm_timer(tBTA_DM_MSG * p_data)848 void bta_dm_pm_timer(tBTA_DM_MSG *p_data)
849 {
850
851 APPL_TRACE_WARNING("proc dm_pm_timer expires");
852 bta_dm_pm_set_mode(p_data->pm_status.bd_addr, TRUE);
853
854
855 }
856
857 /*******************************************************************************
858 **
859 ** Function bta_dm_find_peer_device
860 **
861 ** Description Given an address, find the associated control block.
862 **
863 ** Returns tBTA_DM_PEER_DEVICE
864 **
865 *******************************************************************************/
bta_dm_find_peer_device(BD_ADDR peer_addr)866 tBTA_DM_PEER_DEVICE * bta_dm_find_peer_device(BD_ADDR peer_addr)
867 {
868 tBTA_DM_PEER_DEVICE *p_dev = NULL;
869 int i;
870
871 for(i=0; i<bta_dm_cb.device_list.count; i++)
872 {
873 if(!bdcmp( bta_dm_cb.device_list.peer_device[i].peer_bdaddr, peer_addr))
874 {
875 p_dev = &bta_dm_cb.device_list.peer_device[i];
876 break;
877 }
878
879 }
880 return p_dev;
881 }
882
883 /*******************************************************************************
884 **
885 ** Function bta_dm_is_sco_active
886 **
887 ** Description Loop through connected services for HFP+State=SCO
888 **
889 ** Returns BOOLEAN. TRUE if SCO active, else FALSE
890 **
891 *******************************************************************************/
bta_dm_pm_is_sco_active()892 static BOOLEAN bta_dm_pm_is_sco_active ()
893 {
894 int j;
895 BOOLEAN bScoActive = FALSE;
896
897 for(j=0; j<bta_dm_conn_srvcs.count ; j++)
898 {
899 /* check if an entry already present */
900 if ( (bta_dm_conn_srvcs.conn_srvc[j].id == BTA_ID_AG ) && (bta_dm_conn_srvcs.conn_srvc[j].state == BTA_SYS_SCO_OPEN) )
901 {
902 bScoActive = TRUE;
903 break;
904 }
905 }
906
907 APPL_TRACE_DEBUG("bta_dm_is_sco_active: SCO active: %d", bScoActive);
908 return bScoActive;
909 }
910
911
912 /*******************************************************************************
913 **
914 ** Function bta_dm_pm_hid_check
915 **
916 ** Description Disables/Enables sniff in link policy based on SCO Up/Down
917 **
918 ** Returns None
919 **
920 *******************************************************************************/
921
bta_dm_pm_hid_check(BOOLEAN bScoActive)922 static void bta_dm_pm_hid_check(BOOLEAN bScoActive)
923 {
924 int j;
925
926 /* if HID is active, disable the link policy */
927 for(j=0; j<bta_dm_conn_srvcs.count ; j++)
928 {
929 /* check if an entry already present */
930 if(bta_dm_conn_srvcs.conn_srvc[j].id == BTA_ID_HH )
931 {
932 APPL_TRACE_DEBUG ("SCO status change(Active: %d), modify HID link policy. state: %d",
933 bScoActive, bta_dm_conn_srvcs.conn_srvc[j].state);
934 bta_dm_pm_set_sniff_policy( bta_dm_find_peer_device(bta_dm_conn_srvcs.conn_srvc[j].peer_bdaddr), bScoActive);
935
936 /* if we had disabled link policy, seems like the hid device stop retrying SNIFF after a few tries. force sniff if needed */
937 if (!bScoActive)
938 bta_dm_pm_set_mode(bta_dm_conn_srvcs.conn_srvc[j].peer_bdaddr, FALSE);
939 }
940 }
941
942 }
943
944 /*******************************************************************************
945 **
946 ** Function bta_dm_pm_set_sniff_policy
947 **
948 ** Description Disables/Enables sniff in link policy for the give device
949 **
950 ** Returns None
951 **
952 *******************************************************************************/
bta_dm_pm_set_sniff_policy(tBTA_DM_PEER_DEVICE * p_dev,BOOLEAN bDisable)953 static void bta_dm_pm_set_sniff_policy(tBTA_DM_PEER_DEVICE *p_dev, BOOLEAN bDisable)
954 {
955 UINT16 policy_setting;
956
957 if (!p_dev)
958 return;
959
960 if (bDisable)
961 {
962 policy_setting = bta_dm_cb.cur_policy &
963 (HCI_ENABLE_MASTER_SLAVE_SWITCH |
964 HCI_ENABLE_HOLD_MODE |
965 HCI_ENABLE_PARK_MODE);
966
967 }
968 else
969 {
970 /* allow sniff after sco is closed */
971 policy_setting= bta_dm_cb.cur_policy;
972 }
973
974 /* if disabling SNIFF, make sure link is Active */
975 if (bDisable)
976 bta_dm_pm_active(p_dev->peer_bdaddr);
977
978 /* update device record and set link policy */
979 p_dev->link_policy = policy_setting;
980 BTM_SetLinkPolicy(p_dev->peer_bdaddr, &policy_setting);
981
982 }
983
984 #if ((defined BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
985 /*******************************************************************************
986 **
987 ** Function bta_dm_pm_obtain_controller_state
988 **
989 ** Description This function obtains the consolidated controller power state
990 **
991 ** Parameters:
992 **
993 *******************************************************************************/
bta_dm_pm_obtain_controller_state(void)994 tBTA_DM_CONTRL_STATE bta_dm_pm_obtain_controller_state(void)
995 {
996 /* Did not use counts as it is not sure, how accurate the count values are in
997 ** bta_dm_cb.device_list.count > 0 || bta_dm_cb.device_list.le_count > 0 */
998
999 tBTA_DM_CONTRL_STATE cur_state = BTA_DM_CONTRL_UNKNOWN;
1000 cur_state = BTM_PM_ReadControllerState();
1001
1002 APPL_TRACE_DEBUG("bta_dm_pm_obtain_controller_state: %d", cur_state);
1003 return cur_state;
1004 }
1005 #endif
1006