• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *  Copyright (C) 2000-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 functions that manages ACL link modes.
22  *  This includes operations such as active, hold,
23  *  park and sniff modes.
24  *
25  *  This module contains both internal and external (API)
26  *  functions. External (API) functions are distinguishable
27  *  by their names beginning with uppercase BTM.
28  *
29  *****************************************************************************/
30 
31 #define LOG_TAG "bt_btm_pm"
32 
33 #include <stdlib.h>
34 #include <string.h>
35 #include <stdio.h>
36 #include <stddef.h>
37 
38 #include "bt_types.h"
39 #include "gki.h"
40 #include "hcimsgs.h"
41 #include "btu.h"
42 #include "btm_api.h"
43 #include "btm_int.h"
44 #include "l2c_int.h"
45 #include "hcidefs.h"
46 #include "bt_utils.h"
47 #include "osi/include/log.h"
48 
49 /*****************************************************************************/
50 /*      to handle different modes                                            */
51 /*****************************************************************************/
52 #define BTM_PM_STORED_MASK      0x80 /* set this mask if the command is stored */
53 #define BTM_PM_NUM_SET_MODES    3 /* only hold, sniff & park */
54 
55 /* Usage:  (ptr_features[ offset ] & mask )?TRUE:FALSE */
56 /* offset to supported feature */
57 const UINT8 btm_pm_mode_off[BTM_PM_NUM_SET_MODES] = {0,    0,    1};
58 /* mask to supported feature */
59 const UINT8 btm_pm_mode_msk[BTM_PM_NUM_SET_MODES] = {0x40, 0x80, 0x01};
60 
61 #define BTM_PM_GET_MD1      1
62 #define BTM_PM_GET_MD2      2
63 #define BTM_PM_GET_COMP     3
64 
65 const UINT8 btm_pm_md_comp_matrix[BTM_PM_NUM_SET_MODES*BTM_PM_NUM_SET_MODES] =
66 {
67     BTM_PM_GET_COMP,
68     BTM_PM_GET_MD2,
69     BTM_PM_GET_MD2,
70 
71     BTM_PM_GET_MD1,
72     BTM_PM_GET_COMP,
73     BTM_PM_GET_MD1,
74 
75     BTM_PM_GET_MD1,
76     BTM_PM_GET_MD2,
77     BTM_PM_GET_COMP
78 };
79 
80 /* function prototype */
81 static int btm_pm_find_acl_ind(BD_ADDR remote_bda);
82 static tBTM_STATUS btm_pm_snd_md_req( UINT8 pm_id, int link_ind, tBTM_PM_PWR_MD *p_mode );
83 static const char *mode_to_string(tBTM_PM_MODE mode);
84 
85 /*
86 #ifdef BTM_PM_DEBUG
87 #undef BTM_PM_DEBUG
88 #define BTM_PM_DEBUG    TRUE
89 #endif
90 */
91 
92 #if BTM_PM_DEBUG == TRUE
93 const char * btm_pm_state_str[] =
94 {
95     "pm_active_state",
96     "pm_hold_state",
97     "pm_sniff_state",
98     "pm_park_state",
99     "pm_pend_state"
100 };
101 
102 const char * btm_pm_event_str[] =
103 {
104     "pm_set_mode_event",
105     "pm_hci_sts_event",
106     "pm_mod_chg_event",
107     "pm_update_event"
108 };
109 
110 const char * btm_pm_action_str[] =
111 {
112     "pm_set_mode_action",
113     "pm_update_db_action",
114     "pm_mod_chg_action",
115     "pm_hci_sts_action",
116     "pm_update_action"
117 };
118 #endif  // BTM_PM_DEBUG
119 
120 /*****************************************************************************/
121 /*                     P U B L I C  F U N C T I O N S                        */
122 /*****************************************************************************/
123 /*******************************************************************************
124 **
125 ** Function         BTM_PmRegister
126 **
127 ** Description      register or deregister with power manager
128 **
129 ** Returns          BTM_SUCCESS if successful,
130 **                  BTM_NO_RESOURCES if no room to hold registration
131 **                  BTM_ILLEGAL_VALUE
132 **
133 *******************************************************************************/
BTM_PmRegister(UINT8 mask,UINT8 * p_pm_id,tBTM_PM_STATUS_CBACK * p_cb)134 tBTM_STATUS BTM_PmRegister (UINT8 mask, UINT8 *p_pm_id, tBTM_PM_STATUS_CBACK *p_cb)
135 {
136     int xx;
137 
138     /* de-register */
139     if(mask & BTM_PM_DEREG)
140     {
141         if(*p_pm_id >= BTM_MAX_PM_RECORDS)
142             return BTM_ILLEGAL_VALUE;
143         btm_cb.pm_reg_db[*p_pm_id].mask = BTM_PM_REC_NOT_USED;
144         return BTM_SUCCESS;
145     }
146 
147     for(xx=0; xx<BTM_MAX_PM_RECORDS; xx++)
148     {
149         /* find an unused entry */
150         if(btm_cb.pm_reg_db[xx].mask == BTM_PM_REC_NOT_USED)
151         {
152             /* if register for notification, should provide callback routine */
153             if(mask & BTM_PM_REG_NOTIF)
154             {
155                 if(p_cb == NULL)
156                     return BTM_ILLEGAL_VALUE;
157                 btm_cb.pm_reg_db[xx].cback = p_cb;
158             }
159             btm_cb.pm_reg_db[xx].mask = mask;
160             *p_pm_id = xx;
161             return BTM_SUCCESS;
162         }
163     }
164 
165     return BTM_NO_RESOURCES;
166 }
167 
168 /*******************************************************************************
169 **
170 ** Function         BTM_SetPowerMode
171 **
172 ** Description      store the mode in control block or
173 **                  alter ACL connection behavior.
174 **
175 ** Returns          BTM_SUCCESS if successful,
176 **                  BTM_UNKNOWN_ADDR if bd addr is not active or bad
177 **
178 *******************************************************************************/
BTM_SetPowerMode(UINT8 pm_id,BD_ADDR remote_bda,tBTM_PM_PWR_MD * p_mode)179 tBTM_STATUS BTM_SetPowerMode (UINT8 pm_id, BD_ADDR remote_bda, tBTM_PM_PWR_MD *p_mode)
180 {
181     UINT8               *p_features;
182     int               ind, acl_ind;
183     tBTM_PM_MCB *p_cb = NULL;   /* per ACL link */
184     tBTM_PM_MODE        mode;
185     int                 temp_pm_id;
186 
187 
188     if(pm_id >= BTM_MAX_PM_RECORDS)
189         pm_id = BTM_PM_SET_ONLY_ID;
190 
191     if(p_mode == NULL)
192         return BTM_ILLEGAL_VALUE;
193 
194     BTM_TRACE_API( "BTM_SetPowerMode: pm_id %d BDA: %08x mode:0x%x", pm_id,
195                    (remote_bda[2]<<24)+(remote_bda[3]<<16)+(remote_bda[4]<<8)+remote_bda[5], p_mode->mode);
196 
197     /* take out the force bit */
198     mode = p_mode->mode & ~BTM_PM_MD_FORCE;
199 
200     acl_ind = btm_pm_find_acl_ind(remote_bda);
201     if(acl_ind == MAX_L2CAP_LINKS)
202         return (BTM_UNKNOWN_ADDR);
203 
204     p_cb = &(btm_cb.pm_mode_db[acl_ind]);
205 
206     if(mode != BTM_PM_MD_ACTIVE)
207     {
208         /* check if the requested mode is supported */
209         ind = mode - BTM_PM_MD_HOLD; /* make it base 0 */
210         p_features = BTM_ReadLocalFeatures();
211         if( !(p_features[ btm_pm_mode_off[ind] ] & btm_pm_mode_msk[ind] ) )
212             return BTM_MODE_UNSUPPORTED;
213     }
214 
215     if(mode == p_cb->state) /* the requested mode is current mode */
216     {
217         /* already in the requested mode and the current interval has less latency than the max */
218         if( (mode == BTM_PM_MD_ACTIVE) ||
219             ((p_mode->mode & BTM_PM_MD_FORCE) && (p_mode->max >= p_cb->interval) && (p_mode->min <= p_cb->interval)) ||
220             ((p_mode->mode & BTM_PM_MD_FORCE)==0 && (p_mode->max >= p_cb->interval)) )
221         {
222             BTM_TRACE_DEBUG( "BTM_SetPowerMode: mode:0x%x interval %d max:%d, min:%d", p_mode->mode, p_cb->interval, p_mode->max, p_mode->min);
223             return BTM_SUCCESS;
224         }
225     }
226 
227     temp_pm_id = pm_id;
228     if(pm_id == BTM_PM_SET_ONLY_ID)
229         temp_pm_id = BTM_MAX_PM_RECORDS;
230 
231     /* update mode database */
232     if( ((pm_id != BTM_PM_SET_ONLY_ID) &&
233          (btm_cb.pm_reg_db[pm_id].mask & BTM_PM_REG_SET))
234        || ((pm_id == BTM_PM_SET_ONLY_ID) && (btm_cb.pm_pend_link != MAX_L2CAP_LINKS)) )
235     {
236 #if BTM_PM_DEBUG == TRUE
237     BTM_TRACE_DEBUG( "BTM_SetPowerMode: Saving cmd acl_ind %d temp_pm_id %d", acl_ind,temp_pm_id);
238 #endif  // BTM_PM_DEBUG
239         /* Make sure mask is set to BTM_PM_REG_SET */
240         btm_cb.pm_reg_db[temp_pm_id].mask |= BTM_PM_REG_SET;
241         *(&p_cb->req_mode[temp_pm_id]) = *((tBTM_PM_PWR_MD *)p_mode);
242         p_cb->chg_ind = TRUE;
243     }
244 
245 #if BTM_PM_DEBUG == TRUE
246     BTM_TRACE_DEBUG( "btm_pm state:0x%x, pm_pend_link: %d", p_cb->state, btm_cb.pm_pend_link);
247 #endif  // BTM_PM_DEBUG
248     /* if mode == hold or pending, return */
249     if( (p_cb->state == BTM_PM_STS_HOLD) ||
250         (p_cb->state ==  BTM_PM_STS_PENDING) ||
251         (btm_cb.pm_pend_link != MAX_L2CAP_LINKS) ) /* command pending */
252     {
253         if(acl_ind != btm_cb.pm_pend_link)
254         {
255             /* set the stored mask */
256             p_cb->state |= BTM_PM_STORED_MASK;
257             BTM_TRACE_DEBUG( "btm_pm state stored:%d",acl_ind);
258         }
259         return BTM_CMD_STORED;
260     }
261 
262 
263 
264     return btm_pm_snd_md_req(pm_id, acl_ind, p_mode);
265 }
266 
267 /*******************************************************************************
268 **
269 ** Function         BTM_ReadPowerMode
270 **
271 ** Description      This returns the current mode for a specific
272 **                  ACL connection.
273 **
274 ** Input Param      remote_bda - device address of desired ACL connection
275 **
276 ** Output Param     p_mode - address where the current mode is copied into.
277 **                          BTM_ACL_MODE_NORMAL
278 **                          BTM_ACL_MODE_HOLD
279 **                          BTM_ACL_MODE_SNIFF
280 **                          BTM_ACL_MODE_PARK
281 **                          (valid only if return code is BTM_SUCCESS)
282 **
283 ** Returns          BTM_SUCCESS if successful,
284 **                  BTM_UNKNOWN_ADDR if bd addr is not active or bad
285 **
286 *******************************************************************************/
BTM_ReadPowerMode(BD_ADDR remote_bda,tBTM_PM_MODE * p_mode)287 tBTM_STATUS BTM_ReadPowerMode (BD_ADDR remote_bda, tBTM_PM_MODE *p_mode)
288 {
289     int acl_ind;
290 
291     if( (acl_ind = btm_pm_find_acl_ind(remote_bda)) == MAX_L2CAP_LINKS)
292         return (BTM_UNKNOWN_ADDR);
293 
294     *p_mode = btm_cb.pm_mode_db[acl_ind].state;
295     return BTM_SUCCESS;
296 }
297 
298 /*******************************************************************************
299 **
300 ** Function         BTM_SetSsrParams
301 **
302 ** Description      This sends the given SSR parameters for the given ACL
303 **                  connection if it is in ACTIVE mode.
304 **
305 ** Input Param      remote_bda - device address of desired ACL connection
306 **                  max_lat    - maximum latency (in 0.625ms)(0-0xFFFE)
307 **                  min_rmt_to - minimum remote timeout
308 **                  min_loc_to - minimum local timeout
309 **
310 **
311 ** Returns          BTM_SUCCESS if the HCI command is issued successful,
312 **                  BTM_UNKNOWN_ADDR if bd addr is not active or bad
313 **                  BTM_CMD_STORED if the command is stored
314 **
315 *******************************************************************************/
BTM_SetSsrParams(BD_ADDR remote_bda,UINT16 max_lat,UINT16 min_rmt_to,UINT16 min_loc_to)316 tBTM_STATUS BTM_SetSsrParams (BD_ADDR remote_bda, UINT16 max_lat,
317                               UINT16 min_rmt_to, UINT16 min_loc_to)
318 {
319 #if (BTM_SSR_INCLUDED == TRUE)
320     int acl_ind;
321     tBTM_PM_MCB *p_cb;
322 
323     if( (acl_ind = btm_pm_find_acl_ind(remote_bda)) == MAX_L2CAP_LINKS)
324         return (BTM_UNKNOWN_ADDR);
325 
326     if(BTM_PM_STS_ACTIVE == btm_cb.pm_mode_db[acl_ind].state ||
327         BTM_PM_STS_SNIFF == btm_cb.pm_mode_db[acl_ind].state)
328     {
329         if (btsnd_hcic_sniff_sub_rate(btm_cb.acl_db[acl_ind].hci_handle, max_lat,
330                                       min_rmt_to, min_loc_to))
331             return BTM_SUCCESS;
332         else
333             return BTM_NO_RESOURCES;
334     }
335     p_cb = &btm_cb.pm_mode_db[acl_ind];
336     p_cb->max_lat       = max_lat;
337     p_cb->min_rmt_to    = min_rmt_to;
338     p_cb->min_loc_to    = min_loc_to;
339     return BTM_CMD_STORED;
340 #else
341     return BTM_ILLEGAL_ACTION;
342 #endif  // BTM_SSR_INCLUDED
343 }
344 
345 /*******************************************************************************
346 **
347 ** Function         btm_pm_reset
348 **
349 ** Description      as a part of the BTM reset process.
350 **
351 ** Returns          void
352 **
353 *******************************************************************************/
btm_pm_reset(void)354 void btm_pm_reset(void)
355 {
356     int xx;
357     tBTM_PM_STATUS_CBACK *cb = NULL;
358 
359     /* clear the pending request for application */
360     if( (btm_cb.pm_pend_id != BTM_PM_SET_ONLY_ID) &&
361         (btm_cb.pm_reg_db[btm_cb.pm_pend_id].mask & BTM_PM_REG_NOTIF) )
362     {
363         cb = btm_cb.pm_reg_db[btm_cb.pm_pend_id].cback;
364     }
365 
366 
367     /* clear the register record */
368     for(xx=0; xx<BTM_MAX_PM_RECORDS; xx++)
369     {
370         btm_cb.pm_reg_db[xx].mask = BTM_PM_REC_NOT_USED;
371     }
372 
373     if(cb != NULL && btm_cb.pm_pend_link < MAX_L2CAP_LINKS)
374         (*cb)(btm_cb.acl_db[btm_cb.pm_pend_link].remote_addr, BTM_PM_STS_ERROR, BTM_DEV_RESET, 0);
375 
376     /* no command pending */
377     btm_cb.pm_pend_link = MAX_L2CAP_LINKS;
378 }
379 
380 /*******************************************************************************
381 **
382 ** Function         btm_pm_sm_alloc
383 **
384 ** Description      This function initializes the control block of an ACL link.
385 **                  It is called when an ACL connection is created.
386 **
387 ** Returns          void
388 **
389 *******************************************************************************/
btm_pm_sm_alloc(UINT8 ind)390 void btm_pm_sm_alloc(UINT8 ind)
391 {
392     tBTM_PM_MCB *p_db = &btm_cb.pm_mode_db[ind];   /* per ACL link */
393     memset (p_db, 0, sizeof(tBTM_PM_MCB));
394     p_db->state = BTM_PM_ST_ACTIVE;
395 #if BTM_PM_DEBUG == TRUE
396     BTM_TRACE_DEBUG( "btm_pm_sm_alloc ind:%d st:%d", ind, p_db->state);
397 #endif  // BTM_PM_DEBUG
398 }
399 
400 /*******************************************************************************
401 **
402 ** Function         btm_pm_find_acl_ind
403 **
404 ** Description      This function initializes the control block of an ACL link.
405 **                  It is called when an ACL connection is created.
406 **
407 ** Returns          void
408 **
409 *******************************************************************************/
btm_pm_find_acl_ind(BD_ADDR remote_bda)410 static int btm_pm_find_acl_ind(BD_ADDR remote_bda)
411 {
412     tACL_CONN   *p = &btm_cb.acl_db[0];
413     UINT8 xx;
414 
415     for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p++)
416     {
417         if ((p->in_use) && (!memcmp (p->remote_addr, remote_bda, BD_ADDR_LEN))
418 #if (BLE_INCLUDED == TRUE)
419             && p->transport == BT_TRANSPORT_BR_EDR
420 #endif  // BLE_INCLUDED
421             )
422         {
423 #if BTM_PM_DEBUG == TRUE
424             BTM_TRACE_DEBUG( "btm_pm_find_acl_ind ind:%d, st:%d", xx, btm_cb.pm_mode_db[xx].state);
425 #endif  // BTM_PM_DEBUG
426             break;
427         }
428     }
429     return xx;
430 }
431 
432 /*******************************************************************************
433 **
434 ** Function     btm_pm_compare_modes
435 ** Description  get the "more active" mode of the 2
436 ** Returns      void
437 **
438 *******************************************************************************/
btm_pm_compare_modes(tBTM_PM_PWR_MD * p_md1,tBTM_PM_PWR_MD * p_md2,tBTM_PM_PWR_MD * p_res)439 static tBTM_PM_PWR_MD * btm_pm_compare_modes(tBTM_PM_PWR_MD *p_md1, tBTM_PM_PWR_MD *p_md2, tBTM_PM_PWR_MD *p_res)
440 {
441     UINT8 res;
442 
443     if(p_md1 == NULL)
444     {
445         *p_res = *p_md2;
446         p_res->mode &= ~BTM_PM_MD_FORCE;
447 
448         return p_md2;
449     }
450 
451     if(p_md2->mode == BTM_PM_MD_ACTIVE || p_md1->mode == BTM_PM_MD_ACTIVE)
452     {
453         return NULL;
454     }
455 
456     /* check if force bit is involved */
457     if(p_md1->mode & BTM_PM_MD_FORCE)
458     {
459         *p_res = *p_md1;
460         p_res->mode &= ~BTM_PM_MD_FORCE;
461         return p_res;
462     }
463 
464     if(p_md2->mode & BTM_PM_MD_FORCE)
465     {
466         *p_res = *p_md2;
467         p_res->mode &= ~BTM_PM_MD_FORCE;
468         return p_res;
469     }
470 
471     res = (p_md1->mode - 1) * BTM_PM_NUM_SET_MODES + (p_md2->mode - 1);
472     res = btm_pm_md_comp_matrix[res];
473     switch(res)
474     {
475     case BTM_PM_GET_MD1:
476         *p_res = *p_md1;
477         return p_md1;
478 
479     case BTM_PM_GET_MD2:
480         *p_res = *p_md2;
481         return p_md2;
482 
483     case BTM_PM_GET_COMP:
484         p_res->mode = p_md1->mode;
485         /* min of the two */
486         p_res->max  = (p_md1->max < p_md2->max)? (p_md1->max) : (p_md2->max);
487         /* max of the two */
488         p_res->min  = (p_md1->min > p_md2->min)? (p_md1->min) : (p_md2->min);
489 
490         /* the intersection is NULL */
491         if( p_res->max < p_res->min)
492             return NULL;
493 
494         if(p_res->mode == BTM_PM_MD_SNIFF)
495         {
496             /* max of the two */
497             p_res->attempt  = (p_md1->attempt > p_md2->attempt)? (p_md1->attempt) : (p_md2->attempt);
498             p_res->timeout  = (p_md1->timeout > p_md2->timeout)? (p_md1->timeout) : (p_md2->timeout);
499         }
500         return p_res;
501     }
502     return NULL;
503 }
504 
505 /*******************************************************************************
506 **
507 ** Function     btm_pm_get_set_mode
508 ** Description  get the resulting mode from the registered parties, then compare it
509 **              with the requested mode, if the command is from an unregistered party.
510 ** Returns      void
511 **
512 *******************************************************************************/
btm_pm_get_set_mode(UINT8 pm_id,tBTM_PM_MCB * p_cb,tBTM_PM_PWR_MD * p_mode,tBTM_PM_PWR_MD * p_res)513 static tBTM_PM_MODE btm_pm_get_set_mode(UINT8 pm_id, tBTM_PM_MCB *p_cb, tBTM_PM_PWR_MD *p_mode, tBTM_PM_PWR_MD *p_res)
514 {
515     int   xx, loop_max;
516     tBTM_PM_PWR_MD *p_md = NULL;
517 
518     if(p_mode != NULL && p_mode->mode & BTM_PM_MD_FORCE)
519     {
520         *p_res = *p_mode;
521         p_res->mode &= ~BTM_PM_MD_FORCE;
522         return p_res->mode;
523     }
524 
525     if(!p_mode)
526         loop_max = BTM_MAX_PM_RECORDS+1;
527     else
528         loop_max = BTM_MAX_PM_RECORDS;
529 
530     for( xx=0; xx<loop_max; xx++)
531     {
532         /* g through all the registered "set" parties */
533         if(btm_cb.pm_reg_db[xx].mask & BTM_PM_REG_SET)
534         {
535             if(p_cb->req_mode[xx].mode == BTM_PM_MD_ACTIVE)
536             {
537                 /* if at least one registered (SET) party says ACTIVE, stay active */
538                 return BTM_PM_MD_ACTIVE;
539             }
540             else
541             {
542                 /* if registered parties give conflicting information, stay active */
543                 if( (btm_pm_compare_modes(p_md, &p_cb->req_mode[xx], p_res)) == NULL)
544                     return BTM_PM_MD_ACTIVE;
545                 p_md = p_res;
546             }
547         }
548     }
549 
550     /* if the resulting mode is NULL(nobody registers SET), use the requested mode */
551     if(p_md == NULL)
552     {
553         if(p_mode)
554             *p_res = *((tBTM_PM_PWR_MD *)p_mode);
555         else /* p_mode is NULL when btm_pm_snd_md_req is called from btm_pm_proc_mode_change */
556             return BTM_PM_MD_ACTIVE;
557     }
558     else
559     {
560         /* if the command is from unregistered party,
561            compare the resulting mode from registered party*/
562         if( (pm_id == BTM_PM_SET_ONLY_ID) &&
563             ((btm_pm_compare_modes(p_mode, p_md, p_res)) == NULL) )
564             return BTM_PM_MD_ACTIVE;
565     }
566 
567     return p_res->mode;
568 }
569 
570 /*******************************************************************************
571 **
572 ** Function     btm_pm_snd_md_req
573 ** Description  get the resulting mode and send the resuest to host controller
574 ** Returns      tBTM_STATUS
575 **, BOOLEAN *p_chg_ind
576 *******************************************************************************/
btm_pm_snd_md_req(UINT8 pm_id,int link_ind,tBTM_PM_PWR_MD * p_mode)577 static tBTM_STATUS btm_pm_snd_md_req(UINT8 pm_id, int link_ind, tBTM_PM_PWR_MD *p_mode)
578 {
579     tBTM_PM_PWR_MD  md_res;
580     tBTM_PM_MODE    mode;
581     tBTM_PM_MCB *p_cb = &btm_cb.pm_mode_db[link_ind];
582     BOOLEAN      chg_ind = FALSE;
583 
584     mode = btm_pm_get_set_mode(pm_id, p_cb, p_mode, &md_res);
585     md_res.mode = mode;
586 
587 #if BTM_PM_DEBUG == TRUE
588     BTM_TRACE_DEBUG( "btm_pm_snd_md_req link_ind:%d, mode: %d",
589         link_ind, mode);
590 #endif  // BTM_PM_DEBUG
591 
592     if( p_cb->state == mode)
593     {
594         /* already in the resulting mode */
595         if( (mode == BTM_PM_MD_ACTIVE) ||
596             ((md_res.max >= p_cb->interval) && (md_res.min <= p_cb->interval)) )
597             return BTM_CMD_STORED;
598         /* Otherwise, needs to wake, then sleep */
599         chg_ind = TRUE;
600     }
601     p_cb->chg_ind = chg_ind;
602 
603      /* cannot go directly from current mode to resulting mode. */
604     if( mode != BTM_PM_MD_ACTIVE && p_cb->state != BTM_PM_MD_ACTIVE)
605         p_cb->chg_ind = TRUE; /* needs to wake, then sleep */
606 
607     if(p_cb->chg_ind == TRUE) /* needs to wake first */
608         md_res.mode = BTM_PM_MD_ACTIVE;
609 #if (BTM_SSR_INCLUDED == TRUE)
610     else if(BTM_PM_MD_SNIFF == md_res.mode && p_cb->max_lat)
611     {
612         btsnd_hcic_sniff_sub_rate(btm_cb.acl_db[link_ind].hci_handle, p_cb->max_lat,
613                                   p_cb->min_rmt_to, p_cb->min_loc_to);
614         p_cb->max_lat = 0;
615     }
616 #endif  // BTM_SSR_INCLUDED
617     /* Default is failure */
618     btm_cb.pm_pend_link = MAX_L2CAP_LINKS;
619 
620     /* send the appropriate HCI command */
621     btm_cb.pm_pend_id   = pm_id;
622 
623 #if BTM_PM_DEBUG == TRUE
624     BTM_TRACE_DEBUG("btm_pm_snd_md_req state:0x%x, link_ind: %d", p_cb->state, link_ind);
625 #endif  // BTM_PM_DEBUG
626 
627     LOG_DEBUG("%s switching from %s to %s.", __func__, mode_to_string(p_cb->state), mode_to_string(md_res.mode));
628     switch(md_res.mode)
629     {
630     case BTM_PM_MD_ACTIVE:
631         switch(p_cb->state)
632         {
633         case BTM_PM_MD_SNIFF:
634             if (btsnd_hcic_exit_sniff_mode(btm_cb.acl_db[link_ind].hci_handle))
635             {
636                 btm_cb.pm_pend_link = link_ind;
637             }
638             break;
639         case BTM_PM_MD_PARK:
640             if (btsnd_hcic_exit_park_mode(btm_cb.acl_db[link_ind].hci_handle))
641             {
642                 btm_cb.pm_pend_link = link_ind;
643             }
644             break;
645         default:
646             /* Failure btm_cb.pm_pend_link = MAX_L2CAP_LINKS */
647             break;
648         }
649         break;
650 
651     case BTM_PM_MD_HOLD:
652         if (btsnd_hcic_hold_mode (btm_cb.acl_db[link_ind].hci_handle,
653                                   md_res.max, md_res.min))
654         {
655             btm_cb.pm_pend_link = link_ind;
656         }
657         break;
658 
659     case BTM_PM_MD_SNIFF:
660         if (btsnd_hcic_sniff_mode (btm_cb.acl_db[link_ind].hci_handle,
661                                    md_res.max, md_res.min, md_res.attempt,
662                                    md_res.timeout))
663         {
664             btm_cb.pm_pend_link = link_ind;
665         }
666         break;
667 
668     case BTM_PM_MD_PARK:
669         if (btsnd_hcic_park_mode (btm_cb.acl_db[link_ind].hci_handle,
670                                   md_res.max, md_res.min))
671         {
672             btm_cb.pm_pend_link = link_ind;
673         }
674         break;
675     default:
676         /* Failure btm_cb.pm_pend_link = MAX_L2CAP_LINKS */
677         break;
678     }
679 
680     if(btm_cb.pm_pend_link == MAX_L2CAP_LINKS)
681     {
682         /* the command was not sent */
683 #if BTM_PM_DEBUG == TRUE
684         BTM_TRACE_DEBUG( "pm_pend_link: %d",btm_cb.pm_pend_link);
685 #endif  // BTM_PM_DEBUG
686         return (BTM_NO_RESOURCES);
687     }
688 
689     return BTM_CMD_STARTED;
690 }
691 
692 /*******************************************************************************
693 **
694 ** Function         btm_pm_check_stored
695 **
696 ** Description      This function is called when an HCI command status event occurs
697 **                  to check if there's any PM command issued while waiting for
698 **                  HCI command status.
699 **
700 ** Returns          none.
701 **
702 *******************************************************************************/
btm_pm_check_stored(void)703 static void btm_pm_check_stored(void)
704 {
705     int     xx;
706     for(xx=0; xx<MAX_L2CAP_LINKS; xx++)
707     {
708         if(btm_cb.pm_mode_db[xx].state & BTM_PM_STORED_MASK)
709         {
710             btm_cb.pm_mode_db[xx].state &= ~BTM_PM_STORED_MASK;
711             BTM_TRACE_DEBUG( "btm_pm_check_stored :%d", xx);
712             btm_pm_snd_md_req(BTM_PM_SET_ONLY_ID, xx, NULL);
713             break;
714         }
715     }
716 }
717 
718 
719 /*******************************************************************************
720 **
721 ** Function         btm_pm_proc_cmd_status
722 **
723 ** Description      This function is called when an HCI command status event occurs
724 **                  for power manager related commands.
725 **
726 ** Input Parms      status - status of the event (HCI_SUCCESS if no errors)
727 **
728 ** Returns          none.
729 **
730 *******************************************************************************/
btm_pm_proc_cmd_status(UINT8 status)731 void btm_pm_proc_cmd_status(UINT8 status)
732 {
733     tBTM_PM_MCB     *p_cb;
734     tBTM_PM_STATUS  pm_status;
735 
736     if(btm_cb.pm_pend_link >= MAX_L2CAP_LINKS)
737         return;
738 
739     p_cb = &btm_cb.pm_mode_db[btm_cb.pm_pend_link];
740 
741     if(status == HCI_SUCCESS)
742     {
743         p_cb->state = BTM_PM_ST_PENDING;
744         pm_status = BTM_PM_STS_PENDING;
745 #if BTM_PM_DEBUG == TRUE
746         BTM_TRACE_DEBUG( "btm_pm_proc_cmd_status new state:0x%x", p_cb->state);
747 #endif // BTM_PM_DEBUG
748     }
749     else /* the command was not successfull. Stay in the same state */
750     {
751         pm_status = BTM_PM_STS_ERROR;
752     }
753 
754     /* notify the caller is appropriate */
755     if( (btm_cb.pm_pend_id != BTM_PM_SET_ONLY_ID) &&
756         (btm_cb.pm_reg_db[btm_cb.pm_pend_id].mask & BTM_PM_REG_NOTIF) )
757     {
758         (*btm_cb.pm_reg_db[btm_cb.pm_pend_id].cback)(btm_cb.acl_db[btm_cb.pm_pend_link].remote_addr, pm_status, 0, status);
759     }
760 
761     /* no pending cmd now */
762 #if BTM_PM_DEBUG == TRUE
763     BTM_TRACE_DEBUG( "btm_pm_proc_cmd_status state:0x%x, pm_pend_link: %d(new: %d)",
764         p_cb->state, btm_cb.pm_pend_link, MAX_L2CAP_LINKS);
765 #endif  // BTM_PM_DEBUG
766     btm_cb.pm_pend_link = MAX_L2CAP_LINKS;
767 
768     btm_pm_check_stored();
769 }
770 
771 /*******************************************************************************
772 **
773 ** Function         btm_process_mode_change
774 **
775 ** Description      This function is called when an HCI mode change event occurs.
776 **
777 ** Input Parms      hci_status - status of the event (HCI_SUCCESS if no errors)
778 **                  hci_handle - connection handle associated with the change
779 **                  mode - HCI_MODE_ACTIVE, HCI_MODE_HOLD, HCI_MODE_SNIFF, or HCI_MODE_PARK
780 **                  interval - number of baseband slots (meaning depends on mode)
781 **
782 ** Returns          none.
783 **
784 *******************************************************************************/
btm_pm_proc_mode_change(UINT8 hci_status,UINT16 hci_handle,UINT8 mode,UINT16 interval)785 void btm_pm_proc_mode_change (UINT8 hci_status, UINT16 hci_handle, UINT8 mode, UINT16 interval)
786 {
787     tACL_CONN   *p;
788     tBTM_PM_MCB *p_cb = NULL;
789     int xx, yy, zz;
790     tBTM_PM_STATE  old_state;
791     tL2C_LCB        *p_lcb;
792 
793     /* get the index to acl_db */
794     if ((xx = btm_handle_to_acl_index(hci_handle)) >= MAX_L2CAP_LINKS)
795         return;
796 
797     p = &btm_cb.acl_db[xx];
798 
799     /* update control block */
800     p_cb = &(btm_cb.pm_mode_db[xx]);
801     old_state       = p_cb->state;
802     p_cb->state     = mode;
803     p_cb->interval  = interval;
804 
805     LOG_DEBUG("%s switched from %s to %s.", __func__, mode_to_string(old_state), mode_to_string(p_cb->state));
806 
807     if ((p_lcb = l2cu_find_lcb_by_bd_addr(p->remote_addr, BT_TRANSPORT_BR_EDR)) != NULL)
808     {
809         if ((p_cb->state == BTM_PM_ST_ACTIVE) || (p_cb->state == BTM_PM_ST_SNIFF))
810         {
811             /* There might be any pending packets due to SNIFF or PENDING state */
812             /* Trigger L2C to start transmission of the pending packets. */
813             BTM_TRACE_DEBUG("btm mode change to active; check l2c_link for outgoing packets");
814             l2c_link_check_send_pkts(p_lcb, NULL, NULL);
815         }
816     }
817 
818     /* notify registered parties */
819     for(yy=0; yy<=BTM_MAX_PM_RECORDS; yy++)
820     {
821         /* set req_mode  HOLD mode->ACTIVE */
822         if( (mode == BTM_PM_MD_ACTIVE) && (p_cb->req_mode[yy].mode == BTM_PM_MD_HOLD) )
823             p_cb->req_mode[yy].mode = BTM_PM_MD_ACTIVE;
824     }
825 
826     /* new request has been made. - post a message to BTU task */
827     if(old_state & BTM_PM_STORED_MASK)
828     {
829 #if BTM_PM_DEBUG == TRUE
830         BTM_TRACE_DEBUG( "btm_pm_proc_mode_change: Sending stored req:%d", xx);
831 #endif  // BTM_PM_DEBUG
832         btm_pm_snd_md_req(BTM_PM_SET_ONLY_ID, xx, NULL);
833     }
834     else
835     {
836         for(zz=0; zz<MAX_L2CAP_LINKS; zz++)
837         {
838             if(btm_cb.pm_mode_db[zz].chg_ind == TRUE)
839             {
840 #if BTM_PM_DEBUG == TRUE
841                 BTM_TRACE_DEBUG( "btm_pm_proc_mode_change: Sending PM req :%d", zz);
842 #endif   // BTM_PM_DEBUG
843                 btm_pm_snd_md_req(BTM_PM_SET_ONLY_ID, zz, NULL);
844                 break;
845             }
846         }
847     }
848 
849 
850     /* notify registered parties */
851     for(yy=0; yy<BTM_MAX_PM_RECORDS; yy++)
852     {
853         if(btm_cb.pm_reg_db[yy].mask & BTM_PM_REG_NOTIF)
854         {
855             (*btm_cb.pm_reg_db[yy].cback)( p->remote_addr, mode, interval, hci_status);
856         }
857     }
858 
859     /* If mode change was because of an active role switch or change link key */
860     btm_cont_rswitch(p, btm_find_dev(p->remote_addr), hci_status);
861 }
862 
863 /*******************************************************************************
864 **
865 ** Function         btm_pm_proc_ssr_evt
866 **
867 ** Description      This function is called when an HCI sniff subrating event occurs.
868 **
869 ** Returns          none.
870 **
871 *******************************************************************************/
872 #if (BTM_SSR_INCLUDED == TRUE)
btm_pm_proc_ssr_evt(UINT8 * p,UINT16 evt_len)873 void btm_pm_proc_ssr_evt (UINT8 *p, UINT16 evt_len)
874 {
875     UINT8       status;
876     UINT16      handle;
877     UINT16      max_rx_lat;
878     int         xx, yy;
879     tBTM_PM_MCB *p_cb;
880     tACL_CONN   *p_acl=NULL;
881     UINT16      use_ssr = TRUE;
882     UNUSED(evt_len);
883 
884     STREAM_TO_UINT8 (status, p);
885 
886     STREAM_TO_UINT16 (handle, p);
887     /* get the index to acl_db */
888     if ((xx = btm_handle_to_acl_index(handle)) >= MAX_L2CAP_LINKS)
889         return;
890 
891     p += 2;
892     STREAM_TO_UINT16 (max_rx_lat, p);
893     p_cb = &(btm_cb.pm_mode_db[xx]);
894 
895     p_acl = &btm_cb.acl_db[xx];
896     if(p_cb->interval == max_rx_lat)
897     {
898         /* using legacy sniff */
899         use_ssr = FALSE;
900     }
901 
902     /* notify registered parties */
903     for(yy=0; yy<BTM_MAX_PM_RECORDS; yy++)
904     {
905         if(btm_cb.pm_reg_db[yy].mask & BTM_PM_REG_NOTIF)
906         {
907             if( p_acl)
908             {
909                 (*btm_cb.pm_reg_db[yy].cback)( p_acl->remote_addr, BTM_PM_STS_SSR, use_ssr, status);
910             }
911         }
912     }
913 }
914 #endif  // BTM_SSR_INCLUDED
915 
916 /*******************************************************************************
917 **
918 ** Function         btm_pm_device_in_active_or_sniff_mode
919 **
920 ** Description      This function is called to check if in active or sniff mode
921 **
922 ** Returns          TRUE, if in active or sniff mode
923 **
924 *******************************************************************************/
btm_pm_device_in_active_or_sniff_mode(void)925 BOOLEAN btm_pm_device_in_active_or_sniff_mode(void)
926 {
927     /* The active state is the highest state-includes connected device and sniff mode*/
928 
929     /* Covers active and sniff modes */
930     if (BTM_GetNumAclLinks() > 0)
931     {
932         BTM_TRACE_DEBUG("%s - ACL links: %d", __func__, BTM_GetNumAclLinks());
933         return TRUE;
934     }
935 
936 #if ((defined BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
937     /* Check BLE states */
938     if (btm_ble_get_conn_st() != BLE_CONN_IDLE)
939     {
940         BTM_TRACE_DEBUG("%s - BLE state: %x", __func__, btm_ble_get_conn_st());
941         return TRUE;
942     }
943 #endif
944 
945     return FALSE;
946 }
947 
948 /*******************************************************************************
949 **
950 ** Function         btm_pm_device_in_scan_state
951 **
952 ** Description      This function is called to check if in paging, inquiry or connecting mode
953 **
954 ** Returns          TRUE, if in paging, inquiry or connecting mode
955 **
956 *******************************************************************************/
btm_pm_device_in_scan_state(void)957 BOOLEAN btm_pm_device_in_scan_state(void)
958 {
959     /* Scan state-paging, inquiry, and trying to connect */
960 
961     /* Check for paging */
962     if (btm_cb.is_paging || GKI_queue_length(&btm_cb.page_queue) > 0 ||
963        BTM_BL_PAGING_STARTED == btm_cb.busy_level)
964     {
965        BTM_TRACE_DEBUG("btm_pm_device_in_scan_state- paging");
966        return TRUE;
967     }
968 
969     /* Check for inquiry */
970     if ((btm_cb.btm_inq_vars.inq_active & (BTM_BR_INQ_ACTIVE_MASK | BTM_BLE_INQ_ACTIVE_MASK)) != 0)
971     {
972         BTM_TRACE_DEBUG("btm_pm_device_in_scan_state- Inq active");
973         return TRUE;
974     }
975 
976     return FALSE;
977 }
978 
979 /*******************************************************************************
980 **
981 ** Function         BTM_PM_ReadControllerState
982 **
983 ** Description      This function is called to obtain the controller state
984 **
985 ** Returns          Controller State-BTM_CONTRL_ACTIVE, BTM_CONTRL_SCAN, and BTM_CONTRL_IDLE
986 **
987 *******************************************************************************/
BTM_PM_ReadControllerState(void)988 tBTM_CONTRL_STATE BTM_PM_ReadControllerState(void)
989 {
990     if (TRUE == btm_pm_device_in_active_or_sniff_mode())
991        return BTM_CONTRL_ACTIVE;
992     else
993     if (TRUE == btm_pm_device_in_scan_state())
994        return BTM_CONTRL_SCAN;
995     else
996        return BTM_CONTRL_IDLE;
997 }
998 
mode_to_string(tBTM_PM_MODE mode)999 static const char *mode_to_string(tBTM_PM_MODE mode) {
1000   switch (mode) {
1001     case BTM_PM_MD_ACTIVE: return "ACTIVE";
1002     case BTM_PM_MD_SNIFF:  return "SNIFF";
1003     case BTM_PM_MD_PARK:   return "PARK";
1004     case BTM_PM_MD_HOLD:   return "HOLD";
1005     default:               return "UNKNOWN";
1006   }
1007 }
1008