1 /******************************************************************************
3 * Copyright (C) 2003-2012 Broadcom Corporation
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ******************************************************************************/
19 /******************************************************************************
21 * This file contains the action functions for device manager state
24 ******************************************************************************/
32 #include "bta_dm_int.h"
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, tBTA_DM_PM_ACTION pm_mode,
38 tBTA_DM_PM_REQ pm_req);
39 static void bta_dm_pm_timer_cback(void *p_tle);
40 static void bta_dm_pm_btm_cback(BD_ADDR bd_addr, tBTM_PM_STATUS status, UINT16 value, UINT8 hci_status);
41 static BOOLEAN bta_dm_pm_park(BD_ADDR peer_addr);
42 static BOOLEAN bta_dm_pm_sniff(tBTA_DM_PEER_DEVICE *p_peer_dev, UINT8 index);
43 static BOOLEAN bta_dm_pm_is_sco_active ();
44 static void bta_dm_pm_hid_check(BOOLEAN bScoActive);
45 static void bta_dm_pm_set_sniff_policy(tBTA_DM_PEER_DEVICE *p_dev, BOOLEAN bDisable);
46 static void bta_dm_pm_stop_timer_by_index(tBTA_PM_TIMER *p_timer,
49 #if (BTM_SSR_INCLUDED == TRUE)
50 #if (defined BTA_HH_INCLUDED && BTA_HH_INCLUDED == TRUE)
51 #include "../hh/bta_hh_int.h"
52 /* BTA_DM_PM_SSR1 will be dedicated for HH SSR setting entry, no other profile can use it */
53 #define BTA_DM_PM_SSR_HH BTA_DM_PM_SSR1
55 static void bta_dm_pm_ssr(BD_ADDR peer_addr);
58 tBTA_DM_CONNECTED_SRVCS bta_dm_conn_srvcs;
61 /*******************************************************************************
63 ** Function bta_dm_init_pm
65 ** Description Initializes the BT low power manager
70 *******************************************************************************/
71 void bta_dm_init_pm(void)
73 memset(&bta_dm_conn_srvcs, 0x00, sizeof(bta_dm_conn_srvcs));
75 /* if there are no power manger entries, so not register */
76 if(p_bta_dm_pm_cfg[0].app_id != 0)
78 bta_sys_pm_register((tBTA_SYS_CONN_CBACK*)bta_dm_pm_cback);
80 BTM_PmRegister((BTM_PM_REG_SET | BTM_PM_REG_NOTIF), &bta_dm_cb.pm_id,
84 /* Need to initialize all PM timer service IDs */
85 for (int i = 0; i < BTA_DM_NUM_PM_TIMER; i++)
87 for (int j = 0; j < BTA_DM_PM_MODE_TIMER_MAX; j++)
88 bta_dm_cb.pm_timer[i].srvc_id[j] = BTA_ID_MAX;
93 /*******************************************************************************
95 ** Function bta_dm_disable_pm
97 ** Description Disable PM
102 *******************************************************************************/
103 void bta_dm_disable_pm(void)
105 BTM_PmRegister( BTM_PM_DEREG, &bta_dm_cb.pm_id, NULL);
108 * Deregister the PM callback from the system handling to prevent
109 * re-enabling the PM timers after this call if the callback is invoked.
111 bta_sys_pm_register((tBTA_SYS_CONN_CBACK*)NULL);
113 /* Need to stop all active timers. */
114 for (int i = 0; i < BTA_DM_NUM_PM_TIMER; i++)
116 for (int j = 0; j < BTA_DM_PM_MODE_TIMER_MAX; j++)
118 bta_dm_pm_stop_timer_by_index(&bta_dm_cb.pm_timer[i], j);
119 bta_dm_cb.pm_timer[i].pm_action[j] = BTA_DM_PM_NO_ACTION;
124 /*******************************************************************************
126 ** Function bta_dm_get_av_count
128 ** Description Get the number of connected AV
131 ** Returns number of av connections
133 *******************************************************************************/
134 UINT8 bta_dm_get_av_count(void)
137 for (int i = 0; i < bta_dm_conn_srvcs.count; i++)
139 if (bta_dm_conn_srvcs.conn_srvc[i].id == BTA_ID_AV)
145 /*******************************************************************************
147 ** Function bta_dm_pm_stop_timer
149 ** Description stop a PM timer
154 *******************************************************************************/
155 static void bta_dm_pm_stop_timer(BD_ADDR peer_addr)
157 APPL_TRACE_DEBUG("%s: ", __func__);
159 for(int i=0; i<BTA_DM_NUM_PM_TIMER; i++)
161 if (bta_dm_cb.pm_timer[i].in_use && !bdcmp(bta_dm_cb.pm_timer[i].peer_bdaddr, peer_addr))
163 for (int j = 0; j < BTA_DM_PM_MODE_TIMER_MAX; j++)
165 bta_dm_pm_stop_timer_by_index(&bta_dm_cb.pm_timer[i], j);
167 * TODO: For now, stopping the timer does not reset
169 * The reason is because some of the internal logic that
170 * (re)assigns the pm_action[] values is taking into account
171 * the older value; e.g., see the pm_action[] assignment in
172 * function bta_dm_pm_start_timer().
173 * Such subtlety in the execution logic is error prone, and
174 * should be eliminiated in the future.
182 /*******************************************************************************
184 ** Function bta_pm_action_to_timer_idx
186 ** Description convert power mode into timer index for each connected device
189 ** Returns index of the power mode delay timer
191 *******************************************************************************/
192 static UINT8 bta_pm_action_to_timer_idx(UINT8 pm_action)
194 if (pm_action == BTA_DM_PM_SUSPEND)
195 return BTA_DM_PM_SUSPEND_TIMER_IDX;
196 else if (pm_action == BTA_DM_PM_PARK)
197 return BTA_DM_PM_PARK_TIMER_IDX;
198 else if ((pm_action & BTA_DM_PM_SNIFF) == BTA_DM_PM_SNIFF)
199 return BTA_DM_PM_SNIFF_TIMER_IDX;
201 /* Active, no preference, no action and retry */
202 return BTA_DM_PM_MODE_TIMER_MAX;
205 /*******************************************************************************
207 ** Function bta_dm_pm_stop_timer_by_mode
209 ** Description stop a PM timer
214 *******************************************************************************/
215 static void bta_dm_pm_stop_timer_by_mode(BD_ADDR peer_addr, UINT8 power_mode)
217 const UINT8 timer_idx = bta_pm_action_to_timer_idx(power_mode);
218 if (timer_idx == BTA_DM_PM_MODE_TIMER_MAX)
221 for (int i = 0; i < BTA_DM_NUM_PM_TIMER; i++)
223 if (bta_dm_cb.pm_timer[i].in_use && !bdcmp(bta_dm_cb.pm_timer[i].peer_bdaddr, peer_addr))
225 if (bta_dm_cb.pm_timer[i].srvc_id[timer_idx] != BTA_ID_MAX)
227 bta_dm_pm_stop_timer_by_index(&bta_dm_cb.pm_timer[i], timer_idx);
229 * TODO: Intentionally setting pm_action[timer_idx].
230 * This assignment should be eliminated in the future - see the
231 * pm_action[] related comment inside function
232 * bta_dm_pm_stop_timer().
234 bta_dm_cb.pm_timer[i].pm_action[timer_idx] = power_mode;
241 /*******************************************************************************
243 ** Function bta_dm_pm_stop_timer_by_srvc_id
245 ** Description stop all timer started by the service ID.
248 ** Returns index of the power mode delay timer
250 *******************************************************************************/
251 static void bta_dm_pm_stop_timer_by_srvc_id(BD_ADDR peer_addr, UINT8 srvc_id)
253 for (int i = 0; i < BTA_DM_NUM_PM_TIMER; i++)
255 if (bta_dm_cb.pm_timer[i].in_use && !bdcmp(bta_dm_cb.pm_timer[i].peer_bdaddr, peer_addr))
257 for (int j = 0; j < BTA_DM_PM_MODE_TIMER_MAX; j++)
259 if (bta_dm_cb.pm_timer[i].srvc_id[j] == srvc_id)
261 bta_dm_pm_stop_timer_by_index(&bta_dm_cb.pm_timer[i], j);
262 bta_dm_cb.pm_timer[i].pm_action[j] = BTA_DM_PM_NO_ACTION;
270 /*******************************************************************************
272 ** Function bta_dm_pm_start_timer
274 ** Description start a PM timer
279 *******************************************************************************/
280 static void bta_dm_pm_start_timer(tBTA_PM_TIMER *p_timer, UINT8 timer_idx,
281 INT32 timeout, UINT8 srvc_id, UINT8 pm_action)
283 p_timer->in_use = TRUE;
284 p_timer->timer[timer_idx].p_cback = bta_dm_pm_timer_cback;
286 if (p_timer->srvc_id[timer_idx] == BTA_ID_MAX)
289 if (p_timer->pm_action[timer_idx] < pm_action)
290 p_timer->pm_action[timer_idx] = pm_action;
292 p_timer->srvc_id[timer_idx] = srvc_id;
294 bta_sys_start_timer(&p_timer->timer[timer_idx], 0, timeout);
297 /*******************************************************************************
299 ** Function bta_dm_pm_stop_timer_by_index
301 ** Description stop a PM timer
306 *******************************************************************************/
307 static void bta_dm_pm_stop_timer_by_index(tBTA_PM_TIMER *p_timer,
310 if ((p_timer == NULL) || (timer_idx >= BTA_DM_PM_MODE_TIMER_MAX))
313 if (p_timer->srvc_id[timer_idx] == BTA_ID_MAX)
314 return; /* The timer was not scheduled */
316 assert(p_timer->in_use && (p_timer->active > 0));
318 bta_sys_stop_timer(&p_timer->timer[timer_idx]);
319 p_timer->srvc_id[timer_idx] = BTA_ID_MAX;
320 /* NOTE: pm_action[timer_idx] intentionally not reset */
323 if (p_timer->active == 0)
324 p_timer->in_use = FALSE;
327 UINT32 bta_dm_pm_get_remaining_ticks (TIMER_LIST_ENT *p_target_tle)
329 return bta_sys_get_remaining_ticks(p_target_tle);
332 /*******************************************************************************
334 ** Function bta_dm_pm_cback
336 ** Description Conn change callback from sys for low power management
341 *******************************************************************************/
342 static void bta_dm_pm_cback(tBTA_SYS_CONN_STATUS status, UINT8 id, UINT8 app_id, BD_ADDR peer_addr)
346 UINT16 policy_setting;
348 tBTA_DM_PEER_DEVICE *p_dev;
350 #if (BTM_SSR_INCLUDED == TRUE)
351 int index = BTA_DM_PM_SSR0;
354 APPL_TRACE_DEBUG("bta_dm_pm_cback: st(%d), id(%d), app(%d)", status, id, app_id);
356 p_dev = bta_dm_find_peer_device(peer_addr);
358 /* find if there is an power mode entry for the service */
359 for(i=1; i<=p_bta_dm_pm_cfg[0].app_id; i++)
362 if((p_bta_dm_pm_cfg[i].id == id)
363 && ((p_bta_dm_pm_cfg[i].app_id == BTA_ALL_APP_ID ) || (p_bta_dm_pm_cfg[i].app_id == app_id )))
368 /* if no entries are there for the app_id and subsystem in p_bta_dm_pm_spec*/
369 if(i> p_bta_dm_pm_cfg[0].app_id)
372 bta_dm_pm_stop_timer_by_srvc_id(peer_addr, id);
373 /*p_dev = bta_dm_find_peer_device(peer_addr);*/
375 #if (BTM_SSR_INCLUDED == TRUE)
376 /* set SSR parameters on SYS CONN OPEN */
377 if((BTA_SYS_CONN_OPEN == status) && p_dev && (p_dev->info & BTA_DM_DI_USE_SSR))
379 index = p_bta_dm_pm_spec[p_bta_dm_pm_cfg[i].spec_idx].ssr;
383 /* if no action for the event */
384 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)
386 #if (BTM_SSR_INCLUDED == TRUE)
387 if(BTA_DM_PM_SSR0 == index) /* and do not need to set SSR, return. */
392 for(j=0; j<bta_dm_conn_srvcs.count ; j++)
394 /* check if an entry already present */
395 if((bta_dm_conn_srvcs.conn_srvc[j].id == id)
396 && (bta_dm_conn_srvcs.conn_srvc[j].app_id == app_id )
397 && !bdcmp(bta_dm_conn_srvcs.conn_srvc[j].peer_bdaddr, peer_addr))
399 bta_dm_conn_srvcs.conn_srvc[j].new_request = TRUE;
405 /* if subsystem has no more preference on the power mode remove
407 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)
409 if(j != bta_dm_conn_srvcs.count)
411 bta_dm_conn_srvcs.count--;
413 for(; j<bta_dm_conn_srvcs.count ; j++)
416 memcpy(&bta_dm_conn_srvcs.conn_srvc[j], &bta_dm_conn_srvcs.conn_srvc[j+1], sizeof(bta_dm_conn_srvcs.conn_srvc[j]));
422 APPL_TRACE_WARNING("bta_dm_act no entry for connected service cbs");
426 else if(j == bta_dm_conn_srvcs.count )
428 /* check if we have more connected service that cbs */
429 if(bta_dm_conn_srvcs.count == BTA_DM_NUM_CONN_SRVS)
431 APPL_TRACE_WARNING("bta_dm_act no more connected service cbs");
435 /* fill in a new cb */
436 bta_dm_conn_srvcs.conn_srvc[j].id = id;
437 bta_dm_conn_srvcs.conn_srvc[j].app_id = app_id;
438 bta_dm_conn_srvcs.conn_srvc[j].new_request = TRUE;
439 bdcpy(bta_dm_conn_srvcs.conn_srvc[j].peer_bdaddr, peer_addr);
441 APPL_TRACE_WARNING("new conn_srvc id:%d, app_id:%d", id, app_id);
443 bta_dm_conn_srvcs.count++;
444 bta_dm_conn_srvcs.conn_srvc[j].state = status;
448 /* no service is added or removed. only updating status. */
449 bta_dm_conn_srvcs.conn_srvc[j].state = status;
453 bta_dm_pm_stop_timer(peer_addr);
457 p_dev->pm_mode_attempted = 0;
458 p_dev->pm_mode_failed = 0;
461 #if (BTM_SSR_INCLUDED == TRUE)
462 if(p_bta_dm_ssr_spec[index].max_lat
463 #if (defined BTA_HH_INCLUDED && BTA_HH_INCLUDED == TRUE)
464 || index == BTA_DM_PM_SSR_HH
468 bta_dm_pm_ssr(peer_addr);
472 if( ((NULL != (p = BTM_ReadLocalFeatures ())) && HCI_SNIFF_SUB_RATE_SUPPORTED(p)) &&
473 ((NULL != (p = BTM_ReadRemoteFeatures (peer_addr))) && HCI_SNIFF_SUB_RATE_SUPPORTED(p)) &&
474 (index == BTA_DM_PM_SSR0))
476 if (status == BTA_SYS_SCO_OPEN)
478 APPL_TRACE_DEBUG("%s: SCO inactive, reset SSR to zero", __func__);
479 BTM_SetSsrParams (peer_addr, 0,0,0 );
481 else if (status == BTA_SYS_SCO_CLOSE)
483 APPL_TRACE_DEBUG("%s: SCO active, back to old SSR", __func__);
484 bta_dm_pm_ssr(peer_addr);
490 bta_dm_pm_set_mode(peer_addr, BTA_DM_PM_NO_ACTION, BTA_DM_PM_NEW_REQ);
492 /* perform the HID link workaround if needed
493 ** 1. If SCO up/down event is received OR
494 ** 2. If HID connection open is received and SCO is already active.
495 ** This will handle the case where HID connects when SCO already active
497 if ( BTM_IsDeviceUp() &&
498 ( ((status == BTA_SYS_SCO_OPEN) || (status == BTA_SYS_SCO_CLOSE)) ||
499 ((status == BTA_SYS_CONN_OPEN) && (id == BTA_ID_HH) && bta_dm_pm_is_sco_active()) ) )
502 if (status == BTA_SYS_CONN_OPEN)
505 bScoActive = (status == BTA_SYS_SCO_OPEN);
507 bta_dm_pm_hid_check(bScoActive);
513 /*******************************************************************************
515 ** Function bta_dm_pm_set_mode
517 ** Description Set the power mode for the device
522 *******************************************************************************/
524 static void bta_dm_pm_set_mode(BD_ADDR peer_addr, tBTA_DM_PM_ACTION pm_request,
525 tBTA_DM_PM_REQ pm_req )
528 tBTA_DM_PM_ACTION pm_action = BTA_DM_PM_NO_ACTION;
531 tBTA_DM_PM_ACTION failed_pm = 0;
532 tBTA_DM_PEER_DEVICE *p_peer_device = NULL;
533 tBTA_DM_PM_ACTION allowed_modes = 0;
534 tBTA_DM_PM_ACTION pref_modes = 0;
535 tBTA_DM_PM_CFG *p_pm_cfg;
536 tBTA_DM_PM_SPEC *p_pm_spec;
537 tBTA_DM_PM_ACTN *p_act0, *p_act1;
538 tBTA_DM_SRVCS *p_srvcs = NULL;
539 BOOLEAN timer_started = FALSE;
540 UINT8 timer_idx, available_timer = BTA_DM_PM_MODE_TIMER_MAX;
541 UINT32 remaining_ticks = 0;
543 if(!bta_dm_cb.device_list.count)
546 /* see if any attempt to put device in low power mode failed */
547 p_peer_device = bta_dm_find_peer_device(peer_addr);
548 /* if no peer device found return */
549 if (p_peer_device == NULL)
552 failed_pm = p_peer_device->pm_mode_failed;
554 for(i=0; i<bta_dm_conn_srvcs.count ; i++)
557 p_srvcs = &bta_dm_conn_srvcs.conn_srvc[i];
558 if(!bdcmp(p_srvcs->peer_bdaddr, peer_addr))
561 /* p_bta_dm_pm_cfg[0].app_id is the number of entries */
562 for(j=1; j<=p_bta_dm_pm_cfg[0].app_id; j++)
564 if((p_bta_dm_pm_cfg[j].id == p_srvcs->id)
565 && ((p_bta_dm_pm_cfg[j].app_id == BTA_ALL_APP_ID ) ||
566 (p_bta_dm_pm_cfg[j].app_id == p_srvcs->app_id)))
570 p_pm_cfg = &p_bta_dm_pm_cfg[j];
571 p_pm_spec = &p_bta_dm_pm_spec[p_pm_cfg->spec_idx];
572 p_act0 = &p_pm_spec->actn_tbl[p_srvcs->state][0];
573 p_act1 = &p_pm_spec->actn_tbl[p_srvcs->state][1];
575 APPL_TRACE_DEBUG("bta_dm_pm_set_mode: srvcsid: %d, state: %d, j: %d", p_srvcs->id, p_srvcs->state, j);
576 allowed_modes |= p_pm_spec->allow_mask;
578 /* PM actions are in the order of strictness */
580 /* first check if the first preference is ok */
581 if(!(failed_pm & p_act0->power_mode))
583 pref_modes |= p_act0->power_mode;
585 if(p_act0->power_mode >= pm_action)
587 pm_action = p_act0->power_mode;
589 if (pm_req != BTA_DM_PM_NEW_REQ || p_srvcs->new_request)
591 p_srvcs->new_request = FALSE;
592 timeout = p_act0->timeout;
596 /* if first preference has already failed, try second preference */
597 else if(!(failed_pm & p_act1->power_mode))
599 pref_modes |= p_act1->power_mode;
601 if(p_act1->power_mode > pm_action)
603 pm_action = p_act1->power_mode;
604 timeout = p_act1->timeout;
610 if(pm_action & (BTA_DM_PM_PARK | BTA_DM_PM_SNIFF))
612 /* some service don't like the mode */
613 if(!(allowed_modes & pm_action))
616 /* select the other mode if its allowed and preferred, otherwise 0 which is BTA_DM_PM_NO_ACTION */
617 pm_action = (allowed_modes & (BTA_DM_PM_PARK | BTA_DM_PM_SNIFF) & pref_modes);
619 /* no timeout needed if no action is required */
620 if(pm_action == BTA_DM_PM_NO_ACTION)
627 /* if need to start a timer */
628 if((pm_req != BTA_DM_PM_EXECUTE) && timeout)
630 for(i=0; i<BTA_DM_NUM_PM_TIMER; i++)
632 if(bta_dm_cb.pm_timer[i].in_use && bdcmp(bta_dm_cb.pm_timer[i].peer_bdaddr, peer_addr) == 0)
634 if ((timer_idx = bta_pm_action_to_timer_idx(pm_action)) != BTA_DM_PM_MODE_TIMER_MAX)
636 remaining_ticks = bta_dm_pm_get_remaining_ticks(&bta_dm_cb.pm_timer[i].timer[timer_idx]);
637 if (remaining_ticks < timeout)
639 /* Cancel and restart the timer */
641 * TODO: The value of pm_action[timer_idx] is
642 * conditionally updated between the two function
643 * calls below when the timer is restarted.
644 * This logic is error-prone and should be eliminated
647 bta_dm_pm_stop_timer_by_index(&bta_dm_cb.pm_timer[i],
649 bta_dm_pm_start_timer(&bta_dm_cb.pm_timer[i], timer_idx, timeout, p_srvcs->id, pm_action);
651 timer_started = TRUE;
655 else if (!bta_dm_cb.pm_timer[i].in_use)
657 APPL_TRACE_DEBUG("%s dm_pm_timer:%d, %d", __func__, i, timeout);
658 if (available_timer == BTA_DM_PM_MODE_TIMER_MAX)
662 /* new power mode for a new active connection */
665 if( available_timer != BTA_DM_PM_MODE_TIMER_MAX)
667 bdcpy(bta_dm_cb.pm_timer[available_timer].peer_bdaddr, peer_addr);
668 if ((timer_idx = bta_pm_action_to_timer_idx(pm_action)) != BTA_DM_PM_MODE_TIMER_MAX)
670 bta_dm_pm_start_timer(&bta_dm_cb.pm_timer[available_timer], timer_idx, timeout, p_srvcs->id, pm_action);
671 timer_started = TRUE;
677 APPL_TRACE_WARNING("bta_dm_act dm_pm_timer no more");
682 /* if pending power mode timer expires, and currecnt link is in a
683 lower power mode than current profile requirement, igonre it */
684 if (pm_req == BTA_DM_PM_EXECUTE && pm_request < pm_action)
686 APPL_TRACE_ERROR("Ignore the power mode request: %d", pm_request)
689 if(pm_action == BTA_DM_PM_PARK)
691 p_peer_device->pm_mode_attempted = BTA_DM_PM_PARK;
692 bta_dm_pm_park(peer_addr);
694 else if(pm_action & BTA_DM_PM_SNIFF)
696 /* dont initiate SNIFF, if link_policy has it disabled */
697 if (p_peer_device->link_policy & HCI_ENABLE_SNIFF_MODE)
699 p_peer_device->pm_mode_attempted = BTA_DM_PM_SNIFF;
700 bta_dm_pm_sniff(p_peer_device, (UINT8)(pm_action & 0x0F) );
704 APPL_TRACE_DEBUG("bta_dm_pm_set_mode: Link policy disallows SNIFF, ignore request");
707 else if(pm_action == BTA_DM_PM_ACTIVE)
709 bta_dm_pm_active(peer_addr);
712 /*******************************************************************************
714 ** Function bta_ag_pm_park
716 ** Description Switch to park mode.
719 ** Returns TRUE if park attempted, FALSE otherwise.
721 *******************************************************************************/
722 static BOOLEAN bta_dm_pm_park(BD_ADDR peer_addr)
725 tBTM_PM_MODE mode = BTM_PM_STS_ACTIVE;
727 /* if not in park mode, switch to park */
728 BTM_ReadPowerMode(peer_addr, &mode);
730 if(mode != BTM_PM_MD_PARK)
732 BTM_SetPowerMode (bta_dm_cb.pm_id, peer_addr, &p_bta_dm_pm_md[BTA_DM_PM_PARK_IDX]);
738 /*******************************************************************************
740 ** Function bta_ag_pm_sniff
742 ** Description Switch to sniff mode.
745 ** Returns TRUE if sniff attempted, FALSE otherwise.
747 *******************************************************************************/
748 static BOOLEAN bta_dm_pm_sniff(tBTA_DM_PEER_DEVICE *p_peer_dev, UINT8 index)
750 tBTM_PM_MODE mode = BTM_PM_STS_ACTIVE;
751 tBTM_PM_PWR_MD pwr_md;
753 #if (BTM_SSR_INCLUDED == TRUE)
754 UINT8 *p_rem_feat = NULL;
757 BTM_ReadPowerMode(p_peer_dev->peer_bdaddr, &mode);
758 p_rem_feat = BTM_ReadRemoteFeatures (p_peer_dev->peer_bdaddr);
759 #if (BTM_SSR_INCLUDED == TRUE)
760 APPL_TRACE_DEBUG("bta_dm_pm_sniff cur:%d, idx:%d, info:x%x", mode, index, p_peer_dev->info);
761 if (mode != BTM_PM_MD_SNIFF ||
762 (HCI_SNIFF_SUB_RATE_SUPPORTED(BTM_ReadLocalFeatures ()) && p_rem_feat &&
763 HCI_SNIFF_SUB_RATE_SUPPORTED(p_rem_feat) &&
764 !(p_peer_dev->info & BTA_DM_DI_USE_SSR)))
766 APPL_TRACE_DEBUG("bta_dm_pm_sniff cur:%d, idx:%d", mode, index);
767 if(mode != BTM_PM_MD_SNIFF)
770 #if (BTM_SSR_INCLUDED == TRUE)
771 /* Dont initiate Sniff if controller has alreay accepted
772 * remote sniff params. This avoid sniff loop issue with
773 * some agrresive headsets who use sniff latencies more than
774 * DUT supported range of Sniff intervals.*/
775 if ((mode == BTM_PM_MD_SNIFF) && (p_peer_dev->info & BTA_DM_DI_ACP_SNIFF))
777 APPL_TRACE_DEBUG("%s: already in remote initiate sniff", __func__);
781 /* if the current mode is not sniff, issue the sniff command.
782 * If sniff, but SSR is not used in this link, still issue the command */
783 memcpy(&pwr_md, &p_bta_dm_pm_md[index], sizeof (tBTM_PM_PWR_MD));
784 if (p_peer_dev->info & BTA_DM_DI_INT_SNIFF)
786 pwr_md.mode |= BTM_PM_MD_FORCE;
788 status = BTM_SetPowerMode (bta_dm_cb.pm_id, p_peer_dev->peer_bdaddr, &pwr_md);
789 if (status == BTM_CMD_STORED|| status == BTM_CMD_STARTED)
791 p_peer_dev->info &= ~(BTA_DM_DI_INT_SNIFF|BTA_DM_DI_ACP_SNIFF);
792 p_peer_dev->info |= BTA_DM_DI_SET_SNIFF;
794 else if (status == BTM_SUCCESS)
796 APPL_TRACE_DEBUG("bta_dm_pm_sniff BTM_SetPowerMode() returns BTM_SUCCESS");
797 p_peer_dev->info &= ~(BTA_DM_DI_INT_SNIFF|BTA_DM_DI_ACP_SNIFF|BTA_DM_DI_SET_SNIFF);
801 APPL_TRACE_ERROR("bta_dm_pm_sniff BTM_SetPowerMode() returns ERROR status=%d", status);
802 p_peer_dev->info &= ~(BTA_DM_DI_INT_SNIFF|BTA_DM_DI_ACP_SNIFF|BTA_DM_DI_SET_SNIFF);
808 /*******************************************************************************
810 ** Function bta_dm_pm_ssr
812 ** Description checks and sends SSR parameters
816 *******************************************************************************/
817 #if (BTM_SSR_INCLUDED == TRUE)
818 static void bta_dm_pm_ssr(BD_ADDR peer_addr)
820 tBTA_DM_SSR_SPEC *p_spec, *p_spec_cur;
822 int ssr = BTA_DM_PM_SSR0;
824 /* go through the connected services */
825 for(i=0; i<bta_dm_conn_srvcs.count ; i++)
827 if(!bdcmp(bta_dm_conn_srvcs.conn_srvc[i].peer_bdaddr, peer_addr))
829 /* p_bta_dm_pm_cfg[0].app_id is the number of entries */
830 for(j=1; j<=p_bta_dm_pm_cfg[0].app_id; j++)
832 /* find the associated p_bta_dm_pm_cfg */
833 if((p_bta_dm_pm_cfg[j].id == bta_dm_conn_srvcs.conn_srvc[i].id)
834 && ((p_bta_dm_pm_cfg[j].app_id == BTA_ALL_APP_ID )
835 || (p_bta_dm_pm_cfg[j].app_id == bta_dm_conn_srvcs.conn_srvc[i].app_id)))
837 APPL_TRACE_WARNING("bta_dm_pm_ssr conn_srvc id:%d, app_id:%d",
838 bta_dm_conn_srvcs.conn_srvc[i].id, bta_dm_conn_srvcs.conn_srvc[i].app_id);
843 /* find the ssr index with the smallest max latency. */
844 p_spec_cur = &p_bta_dm_ssr_spec[p_bta_dm_pm_spec[p_bta_dm_pm_cfg[j].spec_idx].ssr];
845 p_spec = &p_bta_dm_ssr_spec[ssr];
847 #if (defined BTA_HH_INCLUDED && BTA_HH_INCLUDED == TRUE)
848 /* HH has the per connection SSR preference, already read the SSR params from BTA HH */
849 if (p_bta_dm_pm_spec[p_bta_dm_pm_cfg[j].spec_idx].ssr == BTA_DM_PM_SSR_HH)
851 if (bta_hh_read_ssr_param(peer_addr, &p_spec_cur->max_lat, &p_spec_cur->min_rmt_to) == BTA_HH_ERR)
855 if (p_spec_cur->max_lat < p_spec->max_lat ||
856 (ssr == BTA_DM_PM_SSR0 && p_bta_dm_pm_spec[p_bta_dm_pm_cfg[j].spec_idx].ssr != BTA_DM_PM_SSR0))
858 ssr = p_bta_dm_pm_spec[p_bta_dm_pm_cfg[j].spec_idx].ssr;
864 p_spec = &p_bta_dm_ssr_spec[ssr];
865 APPL_TRACE_WARNING("bta_dm_pm_ssr:%d, lat:%d", ssr, p_spec->max_lat);
868 /* set the SSR parameters. */
869 BTM_SetSsrParams (peer_addr, p_spec->max_lat,
870 p_spec->min_rmt_to, p_spec->min_loc_to);
874 /*******************************************************************************
876 ** Function bta_dm_pm_active
878 ** Description Brings connection to active mode
883 *******************************************************************************/
884 void bta_dm_pm_active(BD_ADDR peer_addr)
888 memset( (void*)&pm, 0, sizeof(pm));
890 /* switch to active mode */
891 pm.mode = BTM_PM_MD_ACTIVE;
892 BTM_SetPowerMode (bta_dm_cb.pm_id, peer_addr, &pm);
898 /*******************************************************************************
900 ** Function bta_dm_pm_btm_cback
902 ** Description BTM power manager callback.
907 *******************************************************************************/
908 static void bta_dm_pm_btm_cback(BD_ADDR bd_addr, tBTM_PM_STATUS status, UINT16 value, UINT8 hci_status)
910 tBTA_DM_PM_BTM_STATUS *p_buf;
912 if ((p_buf = (tBTA_DM_PM_BTM_STATUS *) GKI_getbuf(sizeof(tBTA_DM_PM_BTM_STATUS))) != NULL)
914 p_buf->hdr.event = BTA_DM_PM_BTM_STATUS_EVT;
915 p_buf->status = status;
916 p_buf->value = value;
917 p_buf->hci_status = hci_status;
918 bdcpy(p_buf->bd_addr, bd_addr);
919 bta_sys_sendmsg(p_buf);
923 /*******************************************************************************
925 ** Function bta_dm_pm_timer_cback
927 ** Description Power management timer callback.
932 *******************************************************************************/
933 static void bta_dm_pm_timer_cback(void *p_tle)
937 for (i=0; i<BTA_DM_NUM_PM_TIMER; i++)
939 APPL_TRACE_DEBUG("dm_pm_timer[%d] in use? %d", i, bta_dm_cb.pm_timer[i].in_use);
940 if (bta_dm_cb.pm_timer[i].in_use)
942 for (j = 0; j < BTA_DM_PM_MODE_TIMER_MAX; j++)
944 if(&bta_dm_cb.pm_timer[i].timer[j] == (TIMER_LIST_ENT*) p_tle)
946 bta_dm_cb.pm_timer[i].active --;
947 bta_dm_cb.pm_timer[i].srvc_id[j] = BTA_ID_MAX;
948 APPL_TRACE_DEBUG("dm_pm_timer[%d] expires, timer_idx=%d", i, j);
952 if (bta_dm_cb.pm_timer[i].active == 0)
953 bta_dm_cb.pm_timer[i].in_use = FALSE;
954 if (j < BTA_DM_PM_MODE_TIMER_MAX)
960 if (i==BTA_DM_NUM_PM_TIMER)
963 tBTA_DM_PM_TIMER *p_buf = (tBTA_DM_PM_TIMER *) GKI_getbuf(sizeof(tBTA_DM_PM_TIMER));
966 p_buf->hdr.event = BTA_DM_PM_TIMER_EVT;
967 p_buf->pm_request = bta_dm_cb.pm_timer[i].pm_action[j];
968 bdcpy(p_buf->bd_addr, bta_dm_cb.pm_timer[i].peer_bdaddr);
969 bta_sys_sendmsg(p_buf);
973 /*******************************************************************************
975 ** Function bta_dm_pm_btm_status
977 ** Description Process pm status event from btm
982 *******************************************************************************/
983 void bta_dm_pm_btm_status(tBTA_DM_MSG *p_data)
985 APPL_TRACE_DEBUG("%s status: %d", __func__, p_data->pm_status.status);
987 tBTA_DM_PEER_DEVICE *p_dev = bta_dm_find_peer_device(p_data->pm_status.bd_addr);
991 tBTA_DM_DEV_INFO info = p_dev->info;
993 switch (p_data->pm_status.status)
995 case BTM_PM_STS_ACTIVE:
996 /* if our sniff or park attempt failed
997 we should not try it again*/
998 if (p_data->pm_status.hci_status != 0)
1000 APPL_TRACE_ERROR("%s hci_status=%d", __func__, p_data->pm_status.hci_status);
1001 p_dev->info &= ~(BTA_DM_DI_INT_SNIFF|BTA_DM_DI_ACP_SNIFF|BTA_DM_DI_SET_SNIFF);
1003 if(p_dev->pm_mode_attempted &(BTA_DM_PM_PARK | BTA_DM_PM_SNIFF))
1005 p_dev->pm_mode_failed
1006 |= ((BTA_DM_PM_PARK | BTA_DM_PM_SNIFF) & p_dev->pm_mode_attempted);
1007 bta_dm_pm_stop_timer_by_mode(p_data->pm_status.bd_addr, p_dev->pm_mode_attempted);
1008 bta_dm_pm_set_mode(p_data->pm_status.bd_addr, BTA_DM_PM_NO_ACTION, BTA_DM_PM_RESTART);
1013 #if (BTM_SSR_INCLUDED == TRUE)
1016 /* need to send the SSR paramaters to controller again */
1017 bta_dm_pm_ssr(p_dev->peer_bdaddr);
1019 p_dev->prev_low = BTM_PM_STS_ACTIVE;
1021 /* link to active mode, need to restart the timer for next low power mode if needed */
1022 bta_dm_pm_stop_timer(p_data->pm_status.bd_addr);
1023 bta_dm_pm_set_mode(p_data->pm_status.bd_addr, BTA_DM_PM_NO_ACTION, BTA_DM_PM_RESTART);
1027 #if (BTM_SSR_INCLUDED == TRUE)
1028 case BTM_PM_STS_PARK:
1029 case BTM_PM_STS_HOLD:
1030 /* save the previous low power mode - for SSR.
1031 * SSR parameters are sent to controller on "conn open".
1032 * the numbers stay good until park/hold/detach */
1033 if(p_dev->info & BTA_DM_DI_USE_SSR)
1034 p_dev->prev_low = p_data->pm_status.status;
1037 case BTM_PM_STS_SSR:
1038 if(p_data->pm_status.value)
1039 p_dev->info |= BTA_DM_DI_USE_SSR;
1041 p_dev->info &= ~BTA_DM_DI_USE_SSR;
1044 case BTM_PM_STS_SNIFF:
1045 if (p_data->pm_status.hci_status == 0)
1047 /* Stop PM timer now if already active for
1048 * particular device since link is already
1049 * put in sniff mode by remote device, and
1050 * PM timer sole purpose is to put the link
1051 * in sniff mode from host side.
1053 bta_dm_pm_stop_timer(p_data->pm_status.bd_addr);
1057 p_dev->info &= ~(BTA_DM_DI_SET_SNIFF | BTA_DM_DI_INT_SNIFF | BTA_DM_DI_ACP_SNIFF);
1058 if (info & BTA_DM_DI_SET_SNIFF)
1059 p_dev->info |= BTA_DM_DI_INT_SNIFF;
1061 p_dev->info |= BTA_DM_DI_ACP_SNIFF;
1065 case BTM_PM_STS_ERROR:
1066 p_dev->info &= ~BTA_DM_DI_SET_SNIFF;
1077 /*******************************************************************************
1079 ** Function bta_dm_pm_timer
1081 ** Description Process pm timer event from btm
1086 *******************************************************************************/
1087 void bta_dm_pm_timer(tBTA_DM_MSG *p_data)
1089 APPL_TRACE_EVENT("%s", __func__);
1090 bta_dm_pm_set_mode(p_data->pm_timer.bd_addr, p_data->pm_timer.pm_request, BTA_DM_PM_EXECUTE);
1093 /*******************************************************************************
1095 ** Function bta_dm_find_peer_device
1097 ** Description Given an address, find the associated control block.
1099 ** Returns tBTA_DM_PEER_DEVICE
1101 *******************************************************************************/
1102 tBTA_DM_PEER_DEVICE * bta_dm_find_peer_device(BD_ADDR peer_addr)
1104 tBTA_DM_PEER_DEVICE *p_dev = NULL;
1106 for (int i=0; i<bta_dm_cb.device_list.count; i++)
1108 if(!bdcmp( bta_dm_cb.device_list.peer_device[i].peer_bdaddr, peer_addr))
1110 p_dev = &bta_dm_cb.device_list.peer_device[i];
1118 /*******************************************************************************
1120 ** Function bta_dm_is_sco_active
1122 ** Description Loop through connected services for HFP+State=SCO
1124 ** Returns BOOLEAN. TRUE if SCO active, else FALSE
1126 *******************************************************************************/
1127 static BOOLEAN bta_dm_pm_is_sco_active ()
1130 BOOLEAN bScoActive = FALSE;
1132 for(j=0; j<bta_dm_conn_srvcs.count ; j++)
1134 /* check if an entry already present */
1135 if ( (bta_dm_conn_srvcs.conn_srvc[j].id == BTA_ID_AG ) && (bta_dm_conn_srvcs.conn_srvc[j].state == BTA_SYS_SCO_OPEN) )
1142 APPL_TRACE_DEBUG("bta_dm_is_sco_active: SCO active: %d", bScoActive);
1147 /*******************************************************************************
1149 ** Function bta_dm_pm_hid_check
1151 ** Description Disables/Enables sniff in link policy based on SCO Up/Down
1155 *******************************************************************************/
1157 static void bta_dm_pm_hid_check(BOOLEAN bScoActive)
1161 /* if HID is active, disable the link policy */
1162 for(j=0; j<bta_dm_conn_srvcs.count ; j++)
1164 /* check if an entry already present */
1165 if(bta_dm_conn_srvcs.conn_srvc[j].id == BTA_ID_HH )
1167 APPL_TRACE_DEBUG ("SCO status change(Active: %d), modify HID link policy. state: %d",
1168 bScoActive, bta_dm_conn_srvcs.conn_srvc[j].state);
1169 bta_dm_pm_set_sniff_policy( bta_dm_find_peer_device(bta_dm_conn_srvcs.conn_srvc[j].peer_bdaddr), bScoActive);
1171 /* if we had disabled link policy, seems like the hid device stop retrying SNIFF after a few tries. force sniff if needed */
1173 bta_dm_pm_set_mode(bta_dm_conn_srvcs.conn_srvc[j].peer_bdaddr, BTA_DM_PM_NO_ACTION,
1180 /*******************************************************************************
1182 ** Function bta_dm_pm_set_sniff_policy
1184 ** Description Disables/Enables sniff in link policy for the give device
1188 *******************************************************************************/
1189 static void bta_dm_pm_set_sniff_policy(tBTA_DM_PEER_DEVICE *p_dev, BOOLEAN bDisable)
1191 UINT16 policy_setting;
1198 policy_setting = bta_dm_cb.cur_policy &
1199 (HCI_ENABLE_MASTER_SLAVE_SWITCH |
1200 HCI_ENABLE_HOLD_MODE |
1201 HCI_ENABLE_PARK_MODE);
1206 /* allow sniff after sco is closed */
1207 policy_setting= bta_dm_cb.cur_policy;
1210 /* if disabling SNIFF, make sure link is Active */
1212 bta_dm_pm_active(p_dev->peer_bdaddr);
1214 /* update device record and set link policy */
1215 p_dev->link_policy = policy_setting;
1216 BTM_SetLinkPolicy(p_dev->peer_bdaddr, &policy_setting);
1220 #if ((defined BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1221 /*******************************************************************************
1223 ** Function bta_dm_pm_obtain_controller_state
1225 ** Description This function obtains the consolidated controller power state
1229 *******************************************************************************/
1230 tBTA_DM_CONTRL_STATE bta_dm_pm_obtain_controller_state(void)
1232 /* Did not use counts as it is not sure, how accurate the count values are in
1233 ** bta_dm_cb.device_list.count > 0 || bta_dm_cb.device_list.le_count > 0 */
1235 tBTA_DM_CONTRL_STATE cur_state = BTA_DM_CONTRL_UNKNOWN;
1236 cur_state = BTM_PM_ReadControllerState();
1238 APPL_TRACE_DEBUG("bta_dm_pm_obtain_controller_state: %d", cur_state);