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