summaryrefslogtreecommitdiffstats
path: root/bta/dm/bta_dm_pm.c
blob: 8a993de22bd1ccd8008af98933d9f1c2fc3d67f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
/******************************************************************************
 *
 *  Copyright (C) 2003-2012 Broadcom Corporation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at:
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 ******************************************************************************/

/******************************************************************************
 *
 *  This file contains the action functions for device manager state
 *  machine.
 *
 ******************************************************************************/

#include "gki.h"
#include "bd.h"
#include "bta_sys.h"
#include "bta_api.h"
#include "bta_dm_int.h"
#include "btm_api.h"

#include <string.h>


static void bta_dm_pm_cback(tBTA_SYS_CONN_STATUS status, UINT8 id, UINT8 app_id, BD_ADDR peer_addr);
static void bta_dm_pm_set_mode(BD_ADDR peer_addr, BOOLEAN timed_out );
static void bta_dm_pm_timer_cback(void *p_tle);
static void bta_dm_pm_btm_cback(BD_ADDR bd_addr, tBTM_PM_STATUS status, UINT16 value, UINT8 hci_status);
static BOOLEAN bta_dm_pm_park(BD_ADDR peer_addr);
static BOOLEAN bta_dm_pm_sniff(tBTA_DM_PEER_DEVICE *p_peer_dev, UINT8 index);
static BOOLEAN bta_dm_pm_is_sco_active ();
static void bta_dm_pm_hid_check(BOOLEAN bScoActive);
static void bta_dm_pm_set_sniff_policy(tBTA_DM_PEER_DEVICE *p_dev, BOOLEAN bDisable);
#if (BTM_SSR_INCLUDED == TRUE)
static void bta_dm_pm_ssr(BD_ADDR peer_addr);
static void bta_dm_ssr_cfg_cback(UINT8 id, UINT8 app_id, UINT16 max_lat, UINT16 min_rmt_to);
#endif

tBTA_DM_CONNECTED_SRVCS bta_dm_conn_srvcs;


/*******************************************************************************
**
** Function         bta_dm_init_pm
**
** Description      Initialises the BT low power manager
**
**
** Returns          void
**
*******************************************************************************/
void bta_dm_init_pm(void)
{

    memset(&bta_dm_conn_srvcs, 0x00, sizeof(bta_dm_conn_srvcs));

    /* if there are no power manger entries, so not register */
    if(p_bta_dm_pm_cfg[0].app_id != 0)
    {
        bta_sys_pm_register((tBTA_SYS_CONN_CBACK*)bta_dm_pm_cback);

#if (BTM_SSR_INCLUDED == TRUE)
        bta_sys_ssr_cfg_register((tBTA_SYS_SSR_CFG_CBACK*)bta_dm_ssr_cfg_cback);
#endif
        BTM_PmRegister((BTM_PM_REG_SET | BTM_PM_REG_NOTIF), &bta_dm_cb.pm_id,
                       bta_dm_pm_btm_cback);
    }


}


/*******************************************************************************
**
** Function         bta_dm_disable_pm
**
** Description      Disable PM
**
**
** Returns          void
**
*******************************************************************************/
void bta_dm_disable_pm(void)
{
    UINT8 i;

    bta_sys_pm_register(NULL);
    BTM_PmRegister( BTM_PM_DEREG, &bta_dm_cb.pm_id, NULL);

    /* Need to stop all active timers. */
    for(i=0; i<BTA_DM_NUM_PM_TIMER; i++)
    {
        if(bta_dm_cb.pm_timer[i].in_use)
        {
            APPL_TRACE_DEBUG1("stop dm_pm_timer:%d", i);
            bta_sys_stop_timer(&bta_dm_cb.pm_timer[i].timer);
            bta_dm_cb.pm_timer[i].in_use = FALSE;
        }
    }
}

/*******************************************************************************
**
** Function         bta_dm_pm_stop_timer
**
** Description      stop a PM timer
**
**
** Returns          void
**
*******************************************************************************/
static void bta_dm_pm_stop_timer(BD_ADDR peer_addr)
{
    UINT8 i;

    for(i=0; i<BTA_DM_NUM_PM_TIMER; i++)
    {

        if(bta_dm_cb.pm_timer[i].in_use && !bdcmp(bta_dm_cb.pm_timer[i].peer_bdaddr, peer_addr))
        {
            APPL_TRACE_DEBUG1("stop dm_pm_timer:%d", i);
            bta_sys_stop_timer(&bta_dm_cb.pm_timer[i].timer);
            bta_dm_cb.pm_timer[i].in_use = FALSE;
            break;
        }

    }
}

/*******************************************************************************
**
** Function         bta_dm_pm_cback
**
** Description      Conn change callback from sys for low power management
**
**
** Returns          void
**
*******************************************************************************/
static void bta_dm_pm_cback(tBTA_SYS_CONN_STATUS status, UINT8 id, UINT8 app_id, BD_ADDR peer_addr)
{

    UINT8 i,j;
    UINT16 policy_setting;
    tBTM_STATUS btm_status;
    tBTM_VERSION_INFO vers;
#if (BTM_SSR_INCLUDED == TRUE)
    int               index = BTA_DM_PM_SSR0;
#endif
    tBTA_DM_PEER_DEVICE *p_dev;

    APPL_TRACE_DEBUG3("bta_dm_pm_cback: st(%d), id(%d), app(%d)", status, id, app_id);

    btm_status = BTM_ReadLocalVersion (&vers);
    p_dev = bta_dm_find_peer_device(peer_addr);

	/* Disable/Enable sniff policy on the SCO link if sco Up/Down. Will be removed in 2.2*/
    if ((btm_status == BTM_SUCCESS) &&
        (vers.manufacturer == LMP_COMPID_BROADCOM) &&
        (vers.hci_version < HCI_PROTO_VERSION_2_0) &&
        ((status == BTA_SYS_SCO_OPEN) || (status == BTA_SYS_SCO_CLOSE)) )
        {
        bta_dm_pm_set_sniff_policy(p_dev, (status == BTA_SYS_SCO_OPEN));
    }

    /* find if there is an power mode entry for the service */
    for(i=1; i<=p_bta_dm_pm_cfg[0].app_id; i++)
    {

        if((p_bta_dm_pm_cfg[i].id == id)
            && ((p_bta_dm_pm_cfg[i].app_id == BTA_ALL_APP_ID ) || (p_bta_dm_pm_cfg[i].app_id == app_id )))
            break;

    }

    /* if no entries are there for the app_id and subystem in p_bta_dm_pm_spec*/
    if(i> p_bta_dm_pm_cfg[0].app_id)
        return;

    bta_dm_pm_stop_timer(peer_addr);
    /*p_dev = bta_dm_find_peer_device(peer_addr);*/

#if (BTM_SSR_INCLUDED == TRUE)
    /* set SSR parameters on SYS CONN OPEN */
    if((BTA_SYS_CONN_OPEN == status) && p_dev && (p_dev->info & BTA_DM_DI_USE_SSR))
    {
        index = p_bta_dm_pm_spec[p_bta_dm_pm_cfg[i].spec_idx].ssr;
    }
#endif

    /* if no action for the event */
    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)
    {
#if (BTM_SSR_INCLUDED == TRUE)
        if(BTA_DM_PM_SSR0 == index) /* and do not need to set SSR, return. */
#endif
        return;
    }

    for(j=0; j<bta_dm_conn_srvcs.count ; j++)
    {
        /* check if an entry already present */
        if((bta_dm_conn_srvcs.conn_srvc[j].id == id)
            && (bta_dm_conn_srvcs.conn_srvc[j].app_id == app_id )
            && !bdcmp(bta_dm_conn_srvcs.conn_srvc[j].peer_bdaddr, peer_addr))
            break;

    }

        /* if subsystem has no more preference on the power mode remove
       the cb */
    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)
    {

        if(j != bta_dm_conn_srvcs.count)
        {
            bta_dm_conn_srvcs.count--;

            for(; j<bta_dm_conn_srvcs.count ; j++)
            {

                memcpy(&bta_dm_conn_srvcs.conn_srvc[j], &bta_dm_conn_srvcs.conn_srvc[j+1], sizeof(bta_dm_conn_srvcs.conn_srvc[j]));

            }
        }
        else
        {
            APPL_TRACE_WARNING0("bta_dm_act no entry for connected service cbs");
            return;
        }
    }
    else if(j == bta_dm_conn_srvcs.count )
    {
        /* check if we have more connected service that cbs */
        if(bta_dm_conn_srvcs.count == BTA_DM_NUM_CONN_SRVS)
        {
            APPL_TRACE_WARNING0("bta_dm_act no more connected service cbs");
            return;
        }

        /* fill in a new cb */
        bta_dm_conn_srvcs.conn_srvc[j].id = id;
        bta_dm_conn_srvcs.conn_srvc[j].app_id = app_id;
        bdcpy(bta_dm_conn_srvcs.conn_srvc[j].peer_bdaddr, peer_addr);

        APPL_TRACE_WARNING2("new conn_srvc id:%d, app_id:%d", id, app_id);

        bta_dm_conn_srvcs.count++;
        bta_dm_conn_srvcs.conn_srvc[j].state = status;
    }
    else
    {
        /* no service is added or removed. only updating status. */
        bta_dm_conn_srvcs.conn_srvc[j].state = status;
    }

    if(p_dev)
    {
        p_dev->pm_mode_attempted = 0;
        p_dev->pm_mode_failed = 0;
    }

#if (BTM_SSR_INCLUDED == TRUE)
    if(p_bta_dm_ssr_spec[index].max_lat)
    {
        bta_dm_pm_ssr(peer_addr);
    }
#endif

    bta_dm_pm_set_mode(peer_addr, FALSE);

    /* perform the HID link workaround if needed
    ** 1. If SCO up/down event is received OR
    ** 2. If HID connection open is received and SCO is already active.
    **     This will handle the case where HID connects when SCO already active
    */
    if ( (btm_status == BTM_SUCCESS) &&
         ( ((status == BTA_SYS_SCO_OPEN) || (status == BTA_SYS_SCO_CLOSE)) ||
           ((status == BTA_SYS_CONN_OPEN) && (id == BTA_ID_HH) && bta_dm_pm_is_sco_active()) ) )
    {
        BOOLEAN bScoActive;
        if (status == BTA_SYS_CONN_OPEN)
            bScoActive = TRUE;
        else
            bScoActive = (status == BTA_SYS_SCO_OPEN);

        bta_dm_pm_hid_check(bScoActive);
    }

}


/*******************************************************************************
**
** Function         bta_dm_pm_set_mode
**
** Description      Set the power mode for the device
**
**
** Returns          void
**
*******************************************************************************/
static void bta_dm_pm_set_mode(BD_ADDR peer_addr, BOOLEAN timed_out )
{

    tBTA_DM_PM_ACTTION  pm_action = BTA_DM_PM_NO_ACTION;
    UINT16              timeout = 0;
    UINT8               i,j;
    tBTA_DM_PM_ACTTION  failed_pm = 0;
    tBTA_DM_PEER_DEVICE *p_peer_device = NULL;
    tBTA_DM_PM_ACTTION   allowed_modes = 0;
    tBTA_DM_PM_ACTTION   pref_modes = 0;
    tBTA_DM_PM_CFG      *p_pm_cfg;
    tBTA_DM_PM_SPEC     *p_pm_spec;
    tBTA_DM_PM_ACTN     *p_act0, *p_act1;
    tBTA_DM_SRVCS       *p_srvcs;


    if(!bta_dm_cb.device_list.count)
        return;

    /* see if any attempt to put device in low power mode failed */
    p_peer_device = bta_dm_find_peer_device(peer_addr);
    /* if no peer device found return */
    if (p_peer_device == NULL)
        return;

    failed_pm = p_peer_device->pm_mode_failed;

    for(i=0; i<bta_dm_conn_srvcs.count ; i++)
    {

        p_srvcs = &bta_dm_conn_srvcs.conn_srvc[i];
        if(!bdcmp(p_srvcs->peer_bdaddr, peer_addr))
        {

            /* p_bta_dm_pm_cfg[0].app_id is the number of entries */
            for(j=1; j<=p_bta_dm_pm_cfg[0].app_id; j++)
            {
                if((p_bta_dm_pm_cfg[j].id == p_srvcs->id)
                    && ((p_bta_dm_pm_cfg[j].app_id == BTA_ALL_APP_ID ) ||
                    (p_bta_dm_pm_cfg[j].app_id == p_srvcs->app_id)))
                    break;
            }

            p_pm_cfg = &p_bta_dm_pm_cfg[j];
            p_pm_spec = &p_bta_dm_pm_spec[p_pm_cfg->spec_idx];
            p_act0 = &p_pm_spec->actn_tbl[p_srvcs->state][0];
            p_act1 = &p_pm_spec->actn_tbl[p_srvcs->state][1];

            APPL_TRACE_DEBUG3("bta_dm_pm_set_mode: srvcsid: %d, state: %d, j: %d", p_srvcs->id, p_srvcs->state, j);
            allowed_modes |= p_pm_spec->allow_mask;

            /* PM actions are in the order of strictness */

            /* first check if the first preference is ok */
            if(!(failed_pm & p_act0->power_mode))
            {
                pref_modes |= p_act0->power_mode;

                if(p_act0->power_mode > pm_action)
                {
                    pm_action = p_act0->power_mode;
                    timeout =  p_act0->timeout;

                }
            }
            /* if first preference has already failed, try second preference */
            else if(!(failed_pm & p_act1->power_mode))
            {
                pref_modes |= p_act1->power_mode;

                if(p_act1->power_mode > pm_action)
                {
                    pm_action = p_act1->power_mode;
                    timeout =  p_act1->timeout;

                }
            }
        }
    }

    if(pm_action & (BTA_DM_PM_PARK | BTA_DM_PM_SNIFF))
    {

        /* some service don't like the mode */
        if(!(allowed_modes & pm_action))
        {

            /* select the other mode if its allowed and preferred, otherwise 0 which is BTA_DM_PM_NO_ACTION */
            pm_action =  (allowed_modes & (BTA_DM_PM_PARK | BTA_DM_PM_SNIFF) & pref_modes);

            /* no timeout needed if no action is required */
            if(pm_action == BTA_DM_PM_NO_ACTION)
            {
                timeout = 0;
            }

        }


    }

    if(!timed_out && timeout)
    {

        for(i=0; i<BTA_DM_NUM_PM_TIMER; i++)
        {

            if(!bta_dm_cb.pm_timer[i].in_use)
            {
                bta_dm_cb.pm_timer[i].in_use = TRUE;
                bdcpy(bta_dm_cb.pm_timer[i].peer_bdaddr, peer_addr);
                bta_dm_cb.pm_timer[i].timer.p_cback = bta_dm_pm_timer_cback;
                bta_sys_start_timer(&bta_dm_cb.pm_timer[i].timer, 0, timeout);
                APPL_TRACE_DEBUG2("start dm_pm_timer:%d, %d", i, timeout);
                return;

            }

        }

        /* no more timers */
        if(i==BTA_DM_NUM_PM_TIMER)
        {
            APPL_TRACE_WARNING0("bta_dm_act dm_pm_timer no more");
            return;
        }
    }

    if(pm_action == BTA_DM_PM_NO_ACTION)
    {


    }
    else if(pm_action == BTA_DM_PM_PARK)
    {
        p_peer_device->pm_mode_attempted = BTA_DM_PM_PARK;
        bta_dm_pm_park(peer_addr);

    }
    else if(pm_action & BTA_DM_PM_SNIFF)
    {
        /* dont initiate SNIFF, if link_policy has it disabled */
        if (p_peer_device->link_policy & HCI_ENABLE_SNIFF_MODE)
        {
	        p_peer_device->pm_mode_attempted = BTA_DM_PM_SNIFF;
    	    bta_dm_pm_sniff(p_peer_device, (UINT8)(pm_action & 0x0F) );
        }
        else
        {
            APPL_TRACE_DEBUG0("bta_dm_pm_set_mode: Link policy disallows SNIFF, ignore request");
        }
    }
    else if(pm_action == BTA_DM_PM_ACTIVE)
    {

        bta_dm_pm_active(peer_addr);

    }


}


/*******************************************************************************
**
** Function         bta_ag_pm_park
**
** Description      Switch to park mode.
**
**
** Returns          TRUE if park attempted, FALSE otherwise.
**
*******************************************************************************/
static BOOLEAN bta_dm_pm_park(BD_ADDR peer_addr)
{

    tBTM_PM_MODE    mode = BTM_PM_STS_ACTIVE;

    /* if not in park mode, switch to park */
    BTM_ReadPowerMode(peer_addr, &mode);

    if(mode != BTM_PM_MD_PARK)
    {
        BTM_SetPowerMode (bta_dm_cb.pm_id, peer_addr, &p_bta_dm_pm_md[BTA_DM_PM_PARK_IDX]);
    }
    return TRUE;

}


/*******************************************************************************
**
** Function         bta_ag_pm_sniff
**
** Description      Switch to sniff mode.
**
**
** Returns          TRUE if sniff attempted, FALSE otherwise.
**
*******************************************************************************/
static BOOLEAN bta_dm_pm_sniff(tBTA_DM_PEER_DEVICE *p_peer_dev, UINT8 index)
{
    tBTM_PM_MODE    mode = BTM_PM_STS_ACTIVE;
    tBTM_PM_PWR_MD  pwr_md;
    tBTM_STATUS     status;

    BTM_ReadPowerMode(p_peer_dev->peer_bdaddr, &mode);

#if (BTM_SSR_INCLUDED == TRUE)
    APPL_TRACE_DEBUG3("bta_dm_pm_sniff cur:%d, idx:%d, info:x%x", mode, index, p_peer_dev->info);
    if (mode != BTM_PM_MD_SNIFF ||
        (HCI_SNIFF_SUB_RATE_SUPPORTED(BTM_ReadLocalFeatures ()) &&
         HCI_SNIFF_SUB_RATE_SUPPORTED(BTM_ReadRemoteFeatures (p_peer_dev->peer_bdaddr)) &&
         !(p_peer_dev->info & BTA_DM_DI_USE_SSR)))
#else
    APPL_TRACE_DEBUG2("bta_dm_pm_sniff cur:%d, idx:%d", mode, index);
    if(mode != BTM_PM_MD_SNIFF)
#endif
    {
        /* if the current mode is not sniff, issue the sniff command.
         * If sniff, but SSR is not used in this link, still issue the command */
        memcpy(&pwr_md, &p_bta_dm_pm_md[index], sizeof (tBTM_PM_PWR_MD));
        if (p_peer_dev->info & BTA_DM_DI_INT_SNIFF)
        {
            pwr_md.mode |= BTM_PM_MD_FORCE;
        }
        status = BTM_SetPowerMode (bta_dm_cb.pm_id, p_peer_dev->peer_bdaddr, &pwr_md);
        if (status == BTM_CMD_STORED|| status == BTM_CMD_STARTED)
        {
            p_peer_dev->info &= ~(BTA_DM_DI_INT_SNIFF|BTA_DM_DI_ACP_SNIFF);
            p_peer_dev->info |= BTA_DM_DI_SET_SNIFF;
        }
        else if (status == BTM_SUCCESS)
        {
            APPL_TRACE_DEBUG0("bta_dm_pm_sniff BTM_SetPowerMode() returns BTM_SUCCESS");
            p_peer_dev->info &= ~(BTA_DM_DI_INT_SNIFF|BTA_DM_DI_ACP_SNIFF|BTA_DM_DI_SET_SNIFF);
        }
        else /* error */
        {
            APPL_TRACE_ERROR1("bta_dm_pm_sniff BTM_SetPowerMode() returns ERROR status=%d", status);
            p_peer_dev->info &= ~(BTA_DM_DI_INT_SNIFF|BTA_DM_DI_ACP_SNIFF|BTA_DM_DI_SET_SNIFF);
        }
    }
    /* else already in sniff and is using SSR, do nothing */

    return TRUE;

}

/*******************************************************************************
**
** Function         bta_dm_pm_ssr
**
** Description      checks and sends SSR parameters
**
** Returns          void
**
*******************************************************************************/
#if (BTM_SSR_INCLUDED == TRUE)
static void bta_dm_pm_ssr(BD_ADDR peer_addr)
{
    tBTA_DM_SSR_SPEC *p_spec, *p_spec_cur;
    UINT8   i,j;
    int     ssr = BTA_DM_PM_SSR0;

    /* go through the connected services */
    for(i=0; i<bta_dm_conn_srvcs.count ; i++)
    {
        if(!bdcmp(bta_dm_conn_srvcs.conn_srvc[i].peer_bdaddr, peer_addr))
        {
            /* p_bta_dm_pm_cfg[0].app_id is the number of entries */
            for(j=1; j<=p_bta_dm_pm_cfg[0].app_id; j++)
            {
                /* find the associated p_bta_dm_pm_cfg */
                if((p_bta_dm_pm_cfg[j].id == bta_dm_conn_srvcs.conn_srvc[i].id)
                    && ((p_bta_dm_pm_cfg[j].app_id == BTA_ALL_APP_ID )
                    || (p_bta_dm_pm_cfg[j].app_id == bta_dm_conn_srvcs.conn_srvc[i].app_id)))
                {
                    APPL_TRACE_WARNING2("bta_dm_pm_ssr conn_srvc id:%d, app_id:%d",
                        bta_dm_conn_srvcs.conn_srvc[i].id, bta_dm_conn_srvcs.conn_srvc[i].app_id);
                    break;
                }
            }

            /* find the ssr index with the smallest max latency. */
            p_spec_cur = &p_bta_dm_ssr_spec[p_bta_dm_pm_spec[p_bta_dm_pm_cfg[j].spec_idx].ssr];
            p_spec = &p_bta_dm_ssr_spec[ssr];

            if (p_spec_cur->max_lat < p_spec->max_lat ||
                (ssr == BTA_DM_PM_SSR0 && p_bta_dm_pm_spec[p_bta_dm_pm_cfg[j].spec_idx].ssr != BTA_DM_PM_SSR0))
            {
                ssr = p_bta_dm_pm_spec[p_bta_dm_pm_cfg[j].spec_idx].ssr;
            }

        }
    }

    p_spec = &p_bta_dm_ssr_spec[ssr];
    APPL_TRACE_WARNING2("bta_dm_pm_ssr:%d, lat:%d", ssr, p_spec->max_lat);
    if(p_spec->max_lat)
    {
        /* set the SSR parameters. */
        BTM_SetSsrParams (peer_addr, p_spec->max_lat,
            p_spec->min_rmt_to, p_spec->min_loc_to);
    }
}
/*******************************************************************************
**
** Function         bta_dm_ssr_cfg_cback
**
** Description      Conn change callback from sys for low power management
**
**
** Returns          void
**
*******************************************************************************/
static void bta_dm_ssr_cfg_cback(UINT8 id, UINT8 app_id,
                                 UINT16 max_lat, UINT16 min_rmt_to)
{
    tBTA_DM_SSR_SPEC *p_spec;
    UINT8   i, index;
    /* find if there is an power mode entry for the service */
    for(i=1; i<=p_bta_dm_pm_cfg[0].app_id; i++)
    {

        if((p_bta_dm_pm_cfg[i].id == id)
            && ((p_bta_dm_pm_cfg[i].app_id == BTA_ALL_APP_ID ) || (p_bta_dm_pm_cfg[i].app_id == app_id )))
            break;

    }
    /* if no entries are there for the app_id and subystem in p_bta_dm_pm_spec*/
    if(i> p_bta_dm_pm_cfg[0].app_id)
        return;

    index = p_bta_dm_pm_spec[p_bta_dm_pm_cfg[i].spec_idx].ssr;

    APPL_TRACE_DEBUG2("SSR parameter changed to: max_latency: %d min_tout: %d", max_lat, min_rmt_to);

    p_spec = &p_bta_dm_ssr_spec[index];
    p_spec->max_lat = max_lat;
    p_spec->min_rmt_to = min_rmt_to;
}


#endif
/*******************************************************************************
**
** Function         bta_dm_pm_active
**
** Description      Brings connection to active mode
**
**
** Returns          void
**
*******************************************************************************/
void bta_dm_pm_active(BD_ADDR peer_addr)
{
    tBTM_PM_PWR_MD  pm;

    memset( (void*)&pm, 0, sizeof(pm));

    /* switch to active mode */
    pm.mode = BTM_PM_MD_ACTIVE;
    BTM_SetPowerMode (bta_dm_cb.pm_id, peer_addr, &pm);


}


/*******************************************************************************
**
** Function         bta_dm_pm_btm_cback
**
** Description      BTM power manager callback.
**
**
** Returns          void
**
*******************************************************************************/
static void bta_dm_pm_btm_cback(BD_ADDR bd_addr, tBTM_PM_STATUS status, UINT16 value, UINT8 hci_status)
{
   tBTA_DM_PM_BTM_STATUS  *p_buf;

   if ((p_buf = (tBTA_DM_PM_BTM_STATUS *) GKI_getbuf(sizeof(tBTA_DM_PM_BTM_STATUS))) != NULL)
    {
        p_buf->hdr.event = BTA_DM_PM_BTM_STATUS_EVT;
        p_buf->status = status;
        p_buf->value = value;
        p_buf->hci_status = hci_status;
        bdcpy(p_buf->bd_addr, bd_addr);
        bta_sys_sendmsg(p_buf);
    }
}

/*******************************************************************************
**
** Function         bta_dm_pm_timer_cback
**
** Description      Power management timer callback.
**
**
** Returns          void
**
*******************************************************************************/
static void bta_dm_pm_timer_cback(void *p_tle)
{
    tBTA_DM_PM_TIMER  *p_buf;
    UINT8 i;

    APPL_TRACE_WARNING0("dm_pm_timer expires");

    for(i=0; i<BTA_DM_NUM_PM_TIMER; i++)
    {

        if(bta_dm_cb.pm_timer[i].in_use)
        {

            if(&bta_dm_cb.pm_timer[i].timer == (TIMER_LIST_ENT*) p_tle)
            {
                APPL_TRACE_WARNING1("dm_pm_timer expires %d", i);
                bta_dm_cb.pm_timer[i].in_use = FALSE;
                break;
            }

        }

    }


    /* no more timers */
    if(i==BTA_DM_NUM_PM_TIMER)
    {
        return;
    }

    if ((p_buf = (tBTA_DM_PM_TIMER *) GKI_getbuf(sizeof(tBTA_DM_PM_TIMER))) != NULL)
    {
        p_buf->hdr.event = BTA_DM_PM_TIMER_EVT;
        bdcpy(p_buf->bd_addr, bta_dm_cb.pm_timer[i].peer_bdaddr);
        bta_sys_sendmsg(p_buf);
    }
}

/*******************************************************************************
**
** Function         bta_dm_pm_btm_status
**
** Description      Process pm status event from btm
**
**
** Returns          void
**
*******************************************************************************/
void bta_dm_pm_btm_status(tBTA_DM_MSG *p_data)
{

    tBTA_DM_PEER_DEVICE *p_dev;
    tBTA_DM_DEV_INFO    info;

    APPL_TRACE_DEBUG1("bta_dm_pm_btm_status:%d", p_data->pm_status.status);
    p_dev = bta_dm_find_peer_device(p_data->pm_status.bd_addr);
    if(NULL == p_dev)
        return;

    info = p_dev->info;
    /* check new mode */
    switch (p_data->pm_status.status)
    {
        case BTM_PM_STS_ACTIVE:
            /* if our sniff or park attempt failed
            we should not try it again*/
            if (p_data->pm_status.hci_status != 0)
            {
                APPL_TRACE_ERROR1("bta_dm_pm_btm_status  hci_status=%d", p_data->pm_status.hci_status);
                p_dev->info &= ~(BTA_DM_DI_INT_SNIFF|BTA_DM_DI_ACP_SNIFF|BTA_DM_DI_SET_SNIFF);

                if(p_dev->pm_mode_attempted &(BTA_DM_PM_PARK | BTA_DM_PM_SNIFF))
                {
                    p_dev->pm_mode_failed
                        |= ((BTA_DM_PM_PARK | BTA_DM_PM_SNIFF) & p_dev->pm_mode_attempted);
                    bta_dm_pm_stop_timer(p_data->pm_status.bd_addr);
                    bta_dm_pm_set_mode(p_data->pm_status.bd_addr, FALSE);
                }
            }
            else
            {
#if (BTM_SSR_INCLUDED == TRUE)
                if(p_dev->prev_low)
                {
                    /* need to send the SSR paramaters to controller again */
                    bta_dm_pm_ssr(p_dev->peer_bdaddr);
                }
                p_dev->prev_low = BTM_PM_STS_ACTIVE;
#endif
                bta_dm_pm_stop_timer(p_data->pm_status.bd_addr);
                bta_dm_pm_set_mode(p_data->pm_status.bd_addr, FALSE);
            }
            break;

#if (BTM_SSR_INCLUDED == TRUE)
        case BTM_PM_STS_PARK:
        case BTM_PM_STS_HOLD:
            /* save the previous low power mode - for SSR.
             * SSR parameters are sent to controller on "conn open".
             * the numbers stay good until park/hold/detach */
            if(p_dev->info & BTA_DM_DI_USE_SSR)
                p_dev->prev_low = p_data->pm_status.status;
            break;

        case BTM_PM_STS_SSR:
            if(p_data->pm_status.value)
                p_dev->info |= BTA_DM_DI_USE_SSR;
            else
                p_dev->info &= ~BTA_DM_DI_USE_SSR;
            break;
#endif
        case BTM_PM_STS_SNIFF:
            p_dev->info &= ~(BTA_DM_DI_SET_SNIFF|BTA_DM_DI_INT_SNIFF|BTA_DM_DI_ACP_SNIFF);
            if (info & BTA_DM_DI_SET_SNIFF)
                p_dev->info |= BTA_DM_DI_INT_SNIFF;
            else
                p_dev->info |= BTA_DM_DI_ACP_SNIFF;
            break;

        case BTM_PM_STS_ERROR:
            p_dev->info &= ~BTA_DM_DI_SET_SNIFF;
            break;

        default:
            break;
    }



}

/*******************************************************************************
**
** Function         bta_dm_pm_timer
**
** Description      Process pm timer event from btm
**
**
** Returns          void
**
*******************************************************************************/
void bta_dm_pm_timer(tBTA_DM_MSG *p_data)
{

    APPL_TRACE_WARNING0("proc dm_pm_timer expires");
    bta_dm_pm_set_mode(p_data->pm_status.bd_addr, TRUE);


}

/*******************************************************************************
**
** Function         bta_dm_find_peer_device
**
** Description      Given an address, find the associated control block.
**
** Returns          tBTA_DM_PEER_DEVICE
**
*******************************************************************************/
tBTA_DM_PEER_DEVICE * bta_dm_find_peer_device(BD_ADDR peer_addr)
{
    tBTA_DM_PEER_DEVICE *p_dev = NULL;
    int i;

    for(i=0; i<bta_dm_cb.device_list.count; i++)
    {
        if(!bdcmp( bta_dm_cb.device_list.peer_device[i].peer_bdaddr, peer_addr))
        {
            p_dev = &bta_dm_cb.device_list.peer_device[i];
            break;
        }

    }
    return p_dev;
}

/*******************************************************************************
**
** Function         bta_dm_is_sco_active
**
** Description      Loop through connected services for HFP+State=SCO
**
** Returns          BOOLEAN. TRUE if SCO active, else FALSE
**
*******************************************************************************/
static BOOLEAN bta_dm_pm_is_sco_active ()
{
    int j;
    BOOLEAN bScoActive = FALSE;

    for(j=0; j<bta_dm_conn_srvcs.count ; j++)
    {
        /* check if an entry already present */
        if ( (bta_dm_conn_srvcs.conn_srvc[j].id == BTA_ID_AG )  && (bta_dm_conn_srvcs.conn_srvc[j].state == BTA_SYS_SCO_OPEN) )
        {
            bScoActive = TRUE;
            break;
        }
    }

    APPL_TRACE_DEBUG1("bta_dm_is_sco_active: SCO active: %d", bScoActive);
    return bScoActive;
}


/*******************************************************************************
**
** Function         bta_dm_pm_hid_check
**
** Description      Disables/Enables sniff in link policy based on SCO Up/Down
**
** Returns          None
**
*******************************************************************************/

static void bta_dm_pm_hid_check(BOOLEAN bScoActive)
{
    int j;

    /* if HID is active, disable the link policy */
    for(j=0; j<bta_dm_conn_srvcs.count ; j++)
    {
        /* check if an entry already present */
        if(bta_dm_conn_srvcs.conn_srvc[j].id == BTA_ID_HH )
        {
            APPL_TRACE_DEBUG2 ("SCO status change(Active: %d), modify HID link policy. state: %d",
                bScoActive, bta_dm_conn_srvcs.conn_srvc[j].state);
            bta_dm_pm_set_sniff_policy( bta_dm_find_peer_device(bta_dm_conn_srvcs.conn_srvc[j].peer_bdaddr), bScoActive);

            /* if we had disabled link policy, seems like the hid device stop retrying SNIFF after a few tries. force sniff if needed */
            if (!bScoActive)
                bta_dm_pm_set_mode(bta_dm_conn_srvcs.conn_srvc[j].peer_bdaddr, FALSE);
        }
    }

}

/*******************************************************************************
**
** Function         bta_dm_pm_set_sniff_policy
**
** Description      Disables/Enables sniff in link policy for the give device
**
** Returns          None
**
*******************************************************************************/
static void bta_dm_pm_set_sniff_policy(tBTA_DM_PEER_DEVICE *p_dev, BOOLEAN bDisable)
{
    UINT16 policy_setting;

    if (!p_dev)
        return;

    if (bDisable)
    {
        policy_setting = bta_dm_cb.cur_policy &
            (HCI_ENABLE_MASTER_SLAVE_SWITCH |
             HCI_ENABLE_HOLD_MODE  |
             HCI_ENABLE_PARK_MODE);

    }
    else
    {
        /*  allow sniff after sco is closed */
         policy_setting= bta_dm_cb.cur_policy;
    }

    /* if disabling SNIFF, make sure link is Active */
    if (bDisable)
        bta_dm_pm_active(p_dev->peer_bdaddr);

    /* update device record and set link policy */
    p_dev->link_policy = policy_setting;
    BTM_SetLinkPolicy(p_dev->peer_bdaddr, &policy_setting);

}