bta_dm_pm.c revision ead3cde4bac0c3e32cd31f149093f004eef8ceeb
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
36static void bta_dm_pm_cback(tBTA_SYS_CONN_STATUS status, UINT8 id, UINT8 app_id, BD_ADDR peer_addr);
37static void bta_dm_pm_set_mode(BD_ADDR peer_addr, BOOLEAN timed_out );
38static void bta_dm_pm_timer_cback(void *p_tle);
39static void bta_dm_pm_btm_cback(BD_ADDR bd_addr, tBTM_PM_STATUS status, UINT16 value, UINT8 hci_status);
40static BOOLEAN bta_dm_pm_park(BD_ADDR peer_addr);
41static BOOLEAN bta_dm_pm_sniff(tBTA_DM_PEER_DEVICE *p_peer_dev, UINT8 index);
42static BOOLEAN bta_dm_pm_is_sco_active ();
43static void bta_dm_pm_hid_check(BOOLEAN bScoActive);
44static 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
52static void bta_dm_pm_ssr(BD_ADDR peer_addr);
53#endif
54
55tBTA_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*******************************************************************************/
68void 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*******************************************************************************/
96void 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_DEBUG1("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*******************************************************************************/
125static 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_DEBUG1("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*******************************************************************************/
153static 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_DEBUG3("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_WARNING0("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_WARNING0("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_WARNING2("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*******************************************************************************/
323static 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_DEBUG3("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_DEBUG2("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_WARNING0("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_DEBUG0("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*******************************************************************************/
496static 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*******************************************************************************/
523static 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_DEBUG3("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_DEBUG2("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_DEBUG0("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_ERROR1("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)
581static 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_WARNING2("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_WARNING2("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*******************************************************************************/
647void 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*******************************************************************************/
671static 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*******************************************************************************/
696static void bta_dm_pm_timer_cback(void *p_tle)
697{
698    tBTA_DM_PM_TIMER  *p_buf;
699    UINT8 i;
700
701    APPL_TRACE_WARNING0("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_WARNING1("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*******************************************************************************/
745void 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_DEBUG1("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_ERROR1("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            p_dev->info &= ~(BTA_DM_DI_SET_SNIFF|BTA_DM_DI_INT_SNIFF|BTA_DM_DI_ACP_SNIFF);
810            if (info & BTA_DM_DI_SET_SNIFF)
811                p_dev->info |= BTA_DM_DI_INT_SNIFF;
812            else
813                p_dev->info |= BTA_DM_DI_ACP_SNIFF;
814            break;
815
816        case BTM_PM_STS_ERROR:
817            p_dev->info &= ~BTA_DM_DI_SET_SNIFF;
818            break;
819
820        default:
821            break;
822    }
823
824
825
826}
827
828/*******************************************************************************
829**
830** Function         bta_dm_pm_timer
831**
832** Description      Process pm timer event from btm
833**
834**
835** Returns          void
836**
837*******************************************************************************/
838void bta_dm_pm_timer(tBTA_DM_MSG *p_data)
839{
840
841    APPL_TRACE_WARNING0("proc dm_pm_timer expires");
842    bta_dm_pm_set_mode(p_data->pm_status.bd_addr, TRUE);
843
844
845}
846
847/*******************************************************************************
848**
849** Function         bta_dm_find_peer_device
850**
851** Description      Given an address, find the associated control block.
852**
853** Returns          tBTA_DM_PEER_DEVICE
854**
855*******************************************************************************/
856tBTA_DM_PEER_DEVICE * bta_dm_find_peer_device(BD_ADDR peer_addr)
857{
858    tBTA_DM_PEER_DEVICE *p_dev = NULL;
859    int i;
860
861    for(i=0; i<bta_dm_cb.device_list.count; i++)
862    {
863        if(!bdcmp( bta_dm_cb.device_list.peer_device[i].peer_bdaddr, peer_addr))
864        {
865            p_dev = &bta_dm_cb.device_list.peer_device[i];
866            break;
867        }
868
869    }
870    return p_dev;
871}
872
873/*******************************************************************************
874**
875** Function         bta_dm_is_sco_active
876**
877** Description      Loop through connected services for HFP+State=SCO
878**
879** Returns          BOOLEAN. TRUE if SCO active, else FALSE
880**
881*******************************************************************************/
882static BOOLEAN bta_dm_pm_is_sco_active ()
883{
884    int j;
885    BOOLEAN bScoActive = FALSE;
886
887    for(j=0; j<bta_dm_conn_srvcs.count ; j++)
888    {
889        /* check if an entry already present */
890        if ( (bta_dm_conn_srvcs.conn_srvc[j].id == BTA_ID_AG )  && (bta_dm_conn_srvcs.conn_srvc[j].state == BTA_SYS_SCO_OPEN) )
891        {
892            bScoActive = TRUE;
893            break;
894        }
895    }
896
897    APPL_TRACE_DEBUG1("bta_dm_is_sco_active: SCO active: %d", bScoActive);
898    return bScoActive;
899}
900
901
902/*******************************************************************************
903**
904** Function         bta_dm_pm_hid_check
905**
906** Description      Disables/Enables sniff in link policy based on SCO Up/Down
907**
908** Returns          None
909**
910*******************************************************************************/
911
912static void bta_dm_pm_hid_check(BOOLEAN bScoActive)
913{
914    int j;
915
916    /* if HID is active, disable the link policy */
917    for(j=0; j<bta_dm_conn_srvcs.count ; j++)
918    {
919        /* check if an entry already present */
920        if(bta_dm_conn_srvcs.conn_srvc[j].id == BTA_ID_HH )
921        {
922            APPL_TRACE_DEBUG2 ("SCO status change(Active: %d), modify HID link policy. state: %d",
923                bScoActive, bta_dm_conn_srvcs.conn_srvc[j].state);
924            bta_dm_pm_set_sniff_policy( bta_dm_find_peer_device(bta_dm_conn_srvcs.conn_srvc[j].peer_bdaddr), bScoActive);
925
926            /* if we had disabled link policy, seems like the hid device stop retrying SNIFF after a few tries. force sniff if needed */
927            if (!bScoActive)
928                bta_dm_pm_set_mode(bta_dm_conn_srvcs.conn_srvc[j].peer_bdaddr, FALSE);
929        }
930    }
931
932}
933
934/*******************************************************************************
935**
936** Function         bta_dm_pm_set_sniff_policy
937**
938** Description      Disables/Enables sniff in link policy for the give device
939**
940** Returns          None
941**
942*******************************************************************************/
943static void bta_dm_pm_set_sniff_policy(tBTA_DM_PEER_DEVICE *p_dev, BOOLEAN bDisable)
944{
945    UINT16 policy_setting;
946
947    if (!p_dev)
948        return;
949
950    if (bDisable)
951    {
952        policy_setting = bta_dm_cb.cur_policy &
953            (HCI_ENABLE_MASTER_SLAVE_SWITCH |
954             HCI_ENABLE_HOLD_MODE  |
955             HCI_ENABLE_PARK_MODE);
956
957    }
958    else
959    {
960        /*  allow sniff after sco is closed */
961         policy_setting= bta_dm_cb.cur_policy;
962    }
963
964    /* if disabling SNIFF, make sure link is Active */
965    if (bDisable)
966        bta_dm_pm_active(p_dev->peer_bdaddr);
967
968    /* update device record and set link policy */
969    p_dev->link_policy = policy_setting;
970    BTM_SetLinkPolicy(p_dev->peer_bdaddr, &policy_setting);
971
972}
973