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