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 #include "main/shim/entry.h"
32 #define LOG_TAG "bt_btm_pm"
33
34 #include <bluetooth/log.h>
35
36 #include <cstdint>
37 #include <unordered_map>
38
39 #include "device/include/interop.h"
40 #include "hci/controller_interface.h"
41 #include "internal_include/bt_target.h"
42 #include "main/shim/dumpsys.h"
43 #include "main/shim/entry.h"
44 #include "osi/include/stack_power_telemetry.h"
45 #include "stack/btm/btm_int_types.h"
46 #include "stack/include/acl_api.h"
47 #include "stack/include/acl_hci_link_interface.h"
48 #include "stack/include/bt_types.h"
49 #include "stack/include/btm_log_history.h"
50 #include "stack/include/btm_status.h"
51 #include "stack/include/l2cap_hci_link_interface.h"
52 #include "stack/include/sco_hci_link_interface.h"
53 #include "types/raw_address.h"
54
55 using namespace bluetooth;
56
57 extern tBTM_CB btm_cb;
58
59 namespace {
60 uint16_t pm_pend_link = 0;
61
62 std::unordered_map<uint16_t /* handle */, tBTM_PM_MCB> pm_mode_db;
63
btm_pm_get_power_manager_from_address(const RawAddress & bda)64 tBTM_PM_MCB* btm_pm_get_power_manager_from_address(const RawAddress& bda) {
65 for (auto& entry : pm_mode_db) {
66 if (entry.second.bda_ == bda) {
67 return &entry.second;
68 }
69 }
70 return nullptr;
71 }
72
73 tBTM_PM_RCB pm_reg_db; /* per application/module */
74
75 uint8_t pm_pend_id = 0; /* the id pf the module, which has a pending PM cmd */
76
77 constexpr char kBtmLogTag[] = "ACL";
78 } // namespace
79
80 /*****************************************************************************/
81 /* to handle different modes */
82 /*****************************************************************************/
83 #define BTM_PM_NUM_SET_MODES 3 /* only hold, sniff & park */
84
85 #define BTM_PM_GET_MD1 1
86 #define BTM_PM_GET_MD2 2
87 #define BTM_PM_GET_COMP 3
88
89 const uint8_t btm_pm_md_comp_matrix[BTM_PM_NUM_SET_MODES * BTM_PM_NUM_SET_MODES] = {
90 BTM_PM_GET_COMP, BTM_PM_GET_MD2, BTM_PM_GET_MD2,
91
92 BTM_PM_GET_MD1, BTM_PM_GET_COMP, BTM_PM_GET_MD1,
93
94 BTM_PM_GET_MD1, BTM_PM_GET_MD2, BTM_PM_GET_COMP};
95
send_sniff_subrating(uint16_t handle,const RawAddress & addr,uint16_t max_lat,uint16_t min_rmt_to,uint16_t min_loc_to)96 static void send_sniff_subrating(uint16_t handle, const RawAddress& addr, uint16_t max_lat,
97 uint16_t min_rmt_to, uint16_t min_loc_to) {
98 uint16_t new_max_lat = 0;
99 if (interop_match_addr_get_max_lat(INTEROP_UPDATE_HID_SSR_MAX_LAT, &addr, &new_max_lat)) {
100 max_lat = new_max_lat;
101 }
102
103 btsnd_hcic_sniff_sub_rate(handle, max_lat, min_rmt_to, min_loc_to);
104 BTM_LogHistory(kBtmLogTag, addr, "Sniff subrating",
105 std::format("max_latency:{:.2f} peer_timeout:{:.2f} local_timeout:{:.2f}",
106 ticks_to_seconds(max_lat), ticks_to_seconds(min_rmt_to),
107 ticks_to_seconds(min_loc_to)));
108 }
109
110 static tBTM_STATUS btm_pm_snd_md_req(uint16_t handle, uint8_t pm_id, int link_ind,
111 const tBTM_PM_PWR_MD* p_mode);
112
113 /*****************************************************************************/
114 /* P U B L I C F U N C T I O N S */
115 /*****************************************************************************/
116 /*******************************************************************************
117 *
118 * Function BTM_PmRegister
119 *
120 * Description register or deregister with power manager
121 *
122 * Returns tBTM_STATUS::BTM_SUCCESS if successful,
123 * tBTM_STATUS::BTM_NO_RESOURCES if no room to hold registration
124 * tBTM_STATUS::BTM_ILLEGAL_VALUE
125 *
126 ******************************************************************************/
BTM_PmRegister(uint8_t mask,uint8_t * p_pm_id,tBTM_PM_STATUS_CBACK * p_cb)127 tBTM_STATUS BTM_PmRegister(uint8_t mask, uint8_t* p_pm_id, tBTM_PM_STATUS_CBACK* p_cb) {
128 /* de-register */
129 if (mask & BTM_PM_DEREG) {
130 if (*p_pm_id >= BTM_MAX_PM_RECORDS) {
131 return tBTM_STATUS::BTM_ILLEGAL_VALUE;
132 }
133 pm_reg_db.mask = BTM_PM_REC_NOT_USED;
134 return tBTM_STATUS::BTM_SUCCESS;
135 }
136
137 if (pm_reg_db.mask == BTM_PM_REC_NOT_USED) {
138 /* if register for notification, should provide callback routine */
139 if (p_cb == NULL) {
140 return tBTM_STATUS::BTM_ILLEGAL_VALUE;
141 }
142 pm_reg_db.cback = p_cb;
143 pm_reg_db.mask = mask;
144 *p_pm_id = 0;
145 return tBTM_STATUS::BTM_SUCCESS;
146 }
147
148 return tBTM_STATUS::BTM_NO_RESOURCES;
149 }
150
BTM_PM_OnConnected(uint16_t handle,const RawAddress & remote_bda)151 void BTM_PM_OnConnected(uint16_t handle, const RawAddress& remote_bda) {
152 if (pm_mode_db.find(handle) != pm_mode_db.end()) {
153 log::error("Overwriting power mode db entry handle:{} peer:{}", handle, remote_bda);
154 }
155 pm_mode_db[handle] = {};
156 pm_mode_db[handle].Init(remote_bda, handle);
157 }
158
BTM_PM_OnDisconnected(uint16_t handle)159 void BTM_PM_OnDisconnected(uint16_t handle) {
160 if (pm_mode_db.find(handle) == pm_mode_db.end()) {
161 log::error("Erasing unknown power mode db entry handle:{}", handle);
162 }
163 pm_mode_db.erase(handle);
164 if (handle == pm_pend_link) {
165 pm_pend_link = 0;
166 }
167 }
168
169 /*******************************************************************************
170 *
171 * Function BTM_SetPowerMode
172 *
173 * Description store the mode in control block or
174 * alter ACL connection behavior.
175 *
176 * Returns tBTM_STATUS::BTM_SUCCESS if successful,
177 * tBTM_STATUS::BTM_UNKNOWN_ADDR if bd addr is not active or bad
178 *
179 ******************************************************************************/
BTM_SetPowerMode(uint8_t pm_id,const RawAddress & remote_bda,const tBTM_PM_PWR_MD * p_mode)180 tBTM_STATUS BTM_SetPowerMode(uint8_t pm_id, const RawAddress& remote_bda,
181 const tBTM_PM_PWR_MD* p_mode) {
182 if (pm_id >= BTM_MAX_PM_RECORDS) {
183 pm_id = BTM_PM_SET_ONLY_ID;
184 }
185
186 if (!p_mode) {
187 log::error("pm_id: {}, p_mode is null for {}", unsigned(pm_id), remote_bda);
188 return tBTM_STATUS::BTM_ILLEGAL_VALUE;
189 }
190
191 // per ACL link
192 auto* p_cb = btm_pm_get_power_manager_from_address(remote_bda);
193 if (p_cb == nullptr) {
194 log::warn("Unable to find power manager for peer: {}", remote_bda);
195 return tBTM_STATUS::BTM_UNKNOWN_ADDR;
196 }
197 uint16_t handle = p_cb->handle_;
198
199 tBTM_PM_MODE mode = p_mode->mode;
200 if (!is_legal_power_mode(mode)) {
201 log::error("Unable to set illegal power mode value:0x{:02x}", mode);
202 return tBTM_STATUS::BTM_ILLEGAL_VALUE;
203 }
204
205 if (p_mode->mode & BTM_PM_MD_FORCE) {
206 log::info("Attempting to force into this power mode");
207 /* take out the force bit */
208 mode &= (~BTM_PM_MD_FORCE);
209 }
210
211 if (mode != BTM_PM_MD_ACTIVE) {
212 auto controller = bluetooth::shim::GetController();
213 if ((mode == BTM_PM_MD_HOLD && !controller->SupportsHoldMode()) ||
214 (mode == BTM_PM_MD_SNIFF && !controller->SupportsSniffMode()) ||
215 (mode == BTM_PM_MD_PARK && !controller->SupportsParkMode()) ||
216 interop_match_addr(INTEROP_DISABLE_SNIFF, &remote_bda)) {
217 log::error("pm_id {} mode {} is not supported for {}", pm_id, mode, remote_bda);
218 return tBTM_STATUS::BTM_MODE_UNSUPPORTED;
219 }
220 }
221
222 if (mode == p_cb->state) {
223 /* already in the requested mode and the current interval has less latency
224 * than the max */
225 if ((mode == BTM_PM_MD_ACTIVE) ||
226 ((p_mode->mode & BTM_PM_MD_FORCE) && (p_mode->max >= p_cb->interval) &&
227 (p_mode->min <= p_cb->interval)) ||
228 ((p_mode->mode & BTM_PM_MD_FORCE) == 0 && (p_mode->max >= p_cb->interval))) {
229 log::debug(
230 "Device is already in requested mode {}, interval: {}, max: {}, min: "
231 "{}",
232 p_mode->mode, p_cb->interval, p_mode->max, p_mode->min);
233 return tBTM_STATUS::BTM_SUCCESS;
234 }
235 }
236
237 /* update mode database */
238 if (((pm_id != BTM_PM_SET_ONLY_ID) && (pm_reg_db.mask & BTM_PM_REG_SET)) ||
239 ((pm_id == BTM_PM_SET_ONLY_ID) && (pm_pend_link != 0))) {
240 /* Make sure mask is set to BTM_PM_REG_SET */
241 pm_reg_db.mask |= BTM_PM_REG_SET;
242 *(&p_cb->req_mode) = *p_mode;
243 p_cb->chg_ind = true;
244 }
245
246 /* if mode == hold or pending, return */
247 if ((p_cb->state == BTM_PM_STS_HOLD) || (p_cb->state == BTM_PM_STS_PENDING) ||
248 (pm_pend_link != 0)) {
249 log::info(
250 "Current power mode is hold or pending status or pending links "
251 "state:{}[{}] pm_pending_link:{}",
252 power_mode_state_text(p_cb->state), p_cb->state, pm_pend_link);
253 /* command pending */
254 if (handle != pm_pend_link) {
255 p_cb->state |= BTM_PM_STORED_MASK;
256 log::info("Setting stored bitmask for peer:{}", remote_bda);
257 }
258 return tBTM_STATUS::BTM_CMD_STORED;
259 }
260
261 log::info("Setting power mode for peer:{} current_mode:{}[{}] new_mode:{}[{}]", remote_bda,
262 power_mode_state_text(p_cb->state), p_cb->state, power_mode_text(p_mode->mode),
263 p_mode->mode);
264
265 return btm_pm_snd_md_req(p_cb->handle_, pm_id, p_cb->handle_, p_mode);
266 }
267
BTM_SetLinkPolicyActiveMode(const RawAddress & remote_bda)268 bool BTM_SetLinkPolicyActiveMode(const RawAddress& remote_bda) {
269 tBTM_PM_PWR_MD settings;
270 memset((void*)&settings, 0, sizeof(settings));
271 settings.mode = BTM_PM_MD_ACTIVE;
272
273 switch (BTM_SetPowerMode(BTM_PM_SET_ONLY_ID, remote_bda, &settings)) {
274 case tBTM_STATUS::BTM_CMD_STORED:
275 case tBTM_STATUS::BTM_SUCCESS:
276 return true;
277 default:
278 return false;
279 }
280 }
281
BTM_ReadPowerMode(const RawAddress & remote_bda,tBTM_PM_MODE * p_mode)282 bool BTM_ReadPowerMode(const RawAddress& remote_bda, tBTM_PM_MODE* p_mode) {
283 if (p_mode == nullptr) {
284 log::error("power mode is nullptr");
285 return false;
286 }
287 tBTM_PM_MCB* p_mcb = btm_pm_get_power_manager_from_address(remote_bda);
288 if (p_mcb == nullptr) {
289 log::warn("Unknown device:{}", remote_bda);
290 return false;
291 }
292 *p_mode = static_cast<tBTM_PM_MODE>(p_mcb->state);
293 return true;
294 }
295
296 /*******************************************************************************
297 *
298 * Function BTM_SetSsrParams
299 *
300 * Description This sends the given SSR parameters for the given ACL
301 * connection if it is in ACTIVE mode.
302 *
303 * Input Param remote_bda - device address of desired ACL connection
304 * max_lat - maximum latency (in 0.625ms)(0-0xFFFE)
305 * min_rmt_to - minimum remote timeout
306 * min_loc_to - minimum local timeout
307 *
308 *
309 * Returns tBTM_STATUS::BTM_SUCCESS if the HCI command is issued successful,
310 * tBTM_STATUS::BTM_UNKNOWN_ADDR if bd addr is not active or bad
311 * tBTM_STATUS::BTM_CMD_STORED if the command is stored
312 *
313 ******************************************************************************/
BTM_SetSsrParams(const RawAddress & remote_bda,uint16_t max_lat,uint16_t min_rmt_to,uint16_t min_loc_to)314 tBTM_STATUS BTM_SetSsrParams(const RawAddress& remote_bda, uint16_t max_lat, uint16_t min_rmt_to,
315 uint16_t min_loc_to) {
316 tBTM_PM_MCB* p_cb = btm_pm_get_power_manager_from_address(remote_bda);
317 if (p_cb == nullptr) {
318 log::warn("Unable to find power manager for peer:{}", remote_bda);
319 return tBTM_STATUS::BTM_UNKNOWN_ADDR;
320 }
321
322 if (!bluetooth::shim::GetController()->SupportsSniffSubrating()) {
323 log::info("No controller support for sniff subrating");
324 return tBTM_STATUS::BTM_SUCCESS;
325 }
326
327 if (p_cb->state == BTM_PM_ST_ACTIVE || p_cb->state == BTM_PM_ST_SNIFF) {
328 log::info(
329 "Set sniff subrating state:{}[{}] max_latency:0x{:04x} "
330 "min_remote_timeout:0x{:04x} min_local_timeout:0x{:04x}",
331 power_mode_state_text(p_cb->state), p_cb->state, max_lat, min_rmt_to, min_loc_to);
332 send_sniff_subrating(p_cb->handle_, remote_bda, max_lat, min_rmt_to, min_loc_to);
333 return tBTM_STATUS::BTM_SUCCESS;
334 }
335 log::info("pm_mode_db state: {}", p_cb->state);
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 tBTM_STATUS::BTM_CMD_STORED;
340 }
341
342 /*******************************************************************************
343 *
344 * Function btm_pm_reset
345 *
346 * Description as a part of the BTM reset process.
347 *
348 * Returns void
349 *
350 ******************************************************************************/
btm_pm_reset(void)351 void btm_pm_reset(void) {
352 tBTM_PM_STATUS_CBACK* cb = NULL;
353
354 /* clear the pending request for application */
355 if ((pm_pend_id != BTM_PM_SET_ONLY_ID) && (pm_reg_db.mask & BTM_PM_REG_SET)) {
356 cb = pm_reg_db.cback;
357 }
358
359 pm_reg_db.mask = BTM_PM_REC_NOT_USED;
360
361 if (cb != NULL && pm_pend_link != 0) {
362 const RawAddress raw_address = pm_mode_db[pm_pend_link].bda_;
363 (*cb)(raw_address, BTM_PM_STS_ERROR, static_cast<uint16_t>(tBTM_STATUS::BTM_DEV_RESET),
364 HCI_SUCCESS);
365 }
366 /* no command pending */
367 pm_pend_link = 0;
368 pm_mode_db.clear();
369 pm_pend_id = 0;
370 memset(&pm_reg_db, 0, sizeof(pm_reg_db));
371 log::info("reset pm");
372 }
373
374 /*******************************************************************************
375 *
376 * Function btm_pm_compare_modes
377 * Description get the "more active" mode of the 2
378 * Returns void
379 *
380 ******************************************************************************/
btm_pm_compare_modes(const tBTM_PM_PWR_MD * p_md1,const tBTM_PM_PWR_MD * p_md2,tBTM_PM_PWR_MD * p_res)381 static tBTM_PM_PWR_MD* btm_pm_compare_modes(const tBTM_PM_PWR_MD* p_md1,
382 const tBTM_PM_PWR_MD* p_md2, tBTM_PM_PWR_MD* p_res) {
383 uint8_t res;
384
385 if (p_md1 == NULL) {
386 *p_res = *p_md2;
387 p_res->mode &= ~BTM_PM_MD_FORCE;
388
389 return p_res;
390 }
391
392 if (p_md2->mode == BTM_PM_MD_ACTIVE || p_md1->mode == BTM_PM_MD_ACTIVE) {
393 return NULL;
394 }
395
396 /* check if force bit is involved */
397 if (p_md1->mode & BTM_PM_MD_FORCE) {
398 *p_res = *p_md1;
399 p_res->mode &= ~BTM_PM_MD_FORCE;
400 return p_res;
401 }
402
403 if (p_md2->mode & BTM_PM_MD_FORCE) {
404 *p_res = *p_md2;
405 p_res->mode &= ~BTM_PM_MD_FORCE;
406 return p_res;
407 }
408
409 res = (p_md1->mode - 1) * BTM_PM_NUM_SET_MODES + (p_md2->mode - 1);
410 res = btm_pm_md_comp_matrix[res];
411 switch (res) {
412 case BTM_PM_GET_MD1:
413 *p_res = *p_md1;
414 return p_res;
415
416 case BTM_PM_GET_MD2:
417 *p_res = *p_md2;
418 return p_res;
419
420 case BTM_PM_GET_COMP:
421 p_res->mode = p_md1->mode;
422 /* min of the two */
423 p_res->max = (p_md1->max < p_md2->max) ? (p_md1->max) : (p_md2->max);
424 /* max of the two */
425 p_res->min = (p_md1->min > p_md2->min) ? (p_md1->min) : (p_md2->min);
426
427 /* the intersection is NULL */
428 if (p_res->max < p_res->min) {
429 return NULL;
430 }
431
432 if (p_res->mode == BTM_PM_MD_SNIFF) {
433 /* max of the two */
434 p_res->attempt = (p_md1->attempt > p_md2->attempt) ? (p_md1->attempt) : (p_md2->attempt);
435 p_res->timeout = (p_md1->timeout > p_md2->timeout) ? (p_md1->timeout) : (p_md2->timeout);
436 }
437 return p_res;
438 }
439 return NULL;
440 }
441
442 /*******************************************************************************
443 *
444 * Function btm_pm_get_set_mode
445 * Description get the resulting mode from the registered parties, then compare
446 * it with the requested mode, if the command is from an
447 * unregistered party.
448 *
449 * Returns void
450 *
451 ******************************************************************************/
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)452 static tBTM_PM_MODE btm_pm_get_set_mode(uint8_t pm_id, tBTM_PM_MCB* p_cb,
453 const tBTM_PM_PWR_MD* p_mode, tBTM_PM_PWR_MD* p_res) {
454 tBTM_PM_PWR_MD* p_md = NULL;
455
456 if (p_mode != NULL && p_mode->mode & BTM_PM_MD_FORCE) {
457 *p_res = *p_mode;
458 p_res->mode &= ~BTM_PM_MD_FORCE;
459 return p_res->mode;
460 }
461
462 /* g through all the registered "set" parties */
463 if (pm_reg_db.mask & BTM_PM_REG_SET) {
464 if (p_cb->req_mode.mode == BTM_PM_MD_ACTIVE) {
465 /* if at least one registered (SET) party says ACTIVE, stay active */
466 return BTM_PM_MD_ACTIVE;
467 } else {
468 /* if registered parties give conflicting information, stay active */
469 if ((btm_pm_compare_modes(p_md, &p_cb->req_mode, p_res)) == NULL) {
470 return BTM_PM_MD_ACTIVE;
471 }
472 p_md = p_res;
473 }
474 }
475
476 /* if the resulting mode is NULL(nobody registers SET), use the requested mode
477 */
478 if (p_md == NULL) {
479 if (p_mode) {
480 *p_res = *((tBTM_PM_PWR_MD*)p_mode);
481 } else { /* p_mode is NULL when btm_pm_snd_md_req is called from
482 btm_pm_proc_mode_change */
483 return BTM_PM_MD_ACTIVE;
484 }
485 } else {
486 /* if the command is from unregistered party,
487 compare the resulting mode from registered party*/
488 if ((pm_id == BTM_PM_SET_ONLY_ID) && ((btm_pm_compare_modes(p_mode, p_md, p_res)) == NULL)) {
489 return BTM_PM_MD_ACTIVE;
490 }
491 }
492
493 return p_res->mode;
494 }
495
496 /*******************************************************************************
497 *
498 * Function btm_pm_snd_md_req
499 * Description get the resulting mode and send the resuest to host controller
500 * Returns tBTM_STATUS
501 *, bool *p_chg_ind
502 ******************************************************************************/
btm_pm_snd_md_req(uint16_t handle,uint8_t pm_id,int link_ind,const tBTM_PM_PWR_MD * p_mode)503 static tBTM_STATUS btm_pm_snd_md_req(uint16_t handle, uint8_t pm_id, int link_ind,
504 const tBTM_PM_PWR_MD* p_mode) {
505 log::assert_that(pm_mode_db.count(handle) != 0, "Unable to find active acl for handle {}",
506 handle);
507 tBTM_PM_PWR_MD md_res;
508 tBTM_PM_MODE mode;
509 tBTM_PM_MCB* p_cb = &pm_mode_db[handle];
510 bool chg_ind = false;
511
512 mode = btm_pm_get_set_mode(pm_id, p_cb, p_mode, &md_res);
513 md_res.mode = mode;
514
515 log::verbose("Found controller in mode:{}", power_mode_text(mode));
516
517 if (p_cb->state == mode) {
518 log::info("Link already in requested mode pm_id:{} link_ind:{} mode:{}[{}]", pm_id, link_ind,
519 power_mode_text(mode), mode);
520
521 /* already in the resulting mode */
522 if ((mode == BTM_PM_MD_ACTIVE) ||
523 ((md_res.max >= p_cb->interval) && (md_res.min <= p_cb->interval))) {
524 log::debug("Storing command");
525 return tBTM_STATUS::BTM_CMD_STORED;
526 }
527 log::debug("Need to wake then sleep");
528 chg_ind = true;
529 }
530 p_cb->chg_ind = chg_ind;
531
532 /* cannot go directly from current mode to resulting mode. */
533 if (mode != BTM_PM_MD_ACTIVE && p_cb->state != BTM_PM_MD_ACTIVE) {
534 log::debug("Power mode change delay required");
535 p_cb->chg_ind = true; /* needs to wake, then sleep */
536 }
537
538 if (p_cb->chg_ind) {
539 log::debug("Need to wake first");
540 md_res.mode = BTM_PM_MD_ACTIVE;
541 } else if (BTM_PM_MD_SNIFF == md_res.mode && p_cb->max_lat) {
542 if (bluetooth::shim::GetController()->SupportsSniffSubrating()) {
543 log::debug("Sending sniff subrating to controller");
544 send_sniff_subrating(handle, p_cb->bda_, p_cb->max_lat, p_cb->min_rmt_to, p_cb->min_loc_to);
545 }
546 p_cb->max_lat = 0;
547 }
548 /* Default is failure */
549 pm_pend_link = 0;
550
551 /* send the appropriate HCI command */
552 pm_pend_id = pm_id;
553
554 log::info("Switching from {}[0x{:02x}] to {}[0x{:02x}]", power_mode_state_text(p_cb->state),
555 p_cb->state, power_mode_state_text(md_res.mode), md_res.mode);
556 BTM_LogHistory(kBtmLogTag, p_cb->bda_, "Power mode change",
557 std::format("{}[0x{:02x}] ==> {}[0x{:02x}]", power_mode_state_text(p_cb->state),
558 p_cb->state, power_mode_state_text(md_res.mode), md_res.mode));
559
560 switch (md_res.mode) {
561 case BTM_PM_MD_ACTIVE:
562 switch (p_cb->state) {
563 case BTM_PM_MD_SNIFF:
564 btsnd_hcic_exit_sniff_mode(handle);
565 pm_pend_link = handle;
566 break;
567 case BTM_PM_MD_PARK:
568 btsnd_hcic_exit_park_mode(handle);
569 pm_pend_link = handle;
570 break;
571 default:
572 /* Failure pm_pend_link = MAX_L2CAP_LINKS */
573 break;
574 }
575 break;
576
577 case BTM_PM_MD_HOLD:
578 btsnd_hcic_hold_mode(handle, md_res.max, md_res.min);
579 pm_pend_link = handle;
580 break;
581
582 case BTM_PM_MD_SNIFF:
583 btsnd_hcic_sniff_mode(handle, md_res.max, md_res.min, md_res.attempt, md_res.timeout);
584 pm_pend_link = handle;
585 break;
586
587 case BTM_PM_MD_PARK:
588 btsnd_hcic_park_mode(handle, md_res.max, md_res.min);
589 pm_pend_link = handle;
590 break;
591 default:
592 /* Failure pm_pend_link = MAX_L2CAP_LINKS */
593 break;
594 }
595
596 if (pm_pend_link == 0) {
597 /* the command was not sent */
598 log::error("pm_pending_link maxed out");
599 return tBTM_STATUS::BTM_NO_RESOURCES;
600 }
601
602 return tBTM_STATUS::BTM_CMD_STARTED;
603 }
604
btm_pm_continue_pending_mode_changes()605 static void btm_pm_continue_pending_mode_changes() {
606 for (auto& entry : pm_mode_db) {
607 if (entry.second.state & BTM_PM_STORED_MASK) {
608 entry.second.state &= ~BTM_PM_STORED_MASK;
609 log::info("Found another link requiring power mode change:{}", entry.second.bda_);
610 btm_pm_snd_md_req(entry.second.handle_, BTM_PM_SET_ONLY_ID, entry.second.handle_, NULL);
611 return;
612 }
613 }
614 }
615
616 /*******************************************************************************
617 *
618 * Function btm_pm_proc_cmd_status
619 *
620 * Description This function is called when an HCI command status event
621 * occurs for power manager related commands.
622 *
623 * Input Parms status - status of the event (HCI_SUCCESS if no errors)
624 *
625 * Returns none.
626 *
627 ******************************************************************************/
btm_pm_proc_cmd_status(tHCI_STATUS status)628 void btm_pm_proc_cmd_status(tHCI_STATUS status) {
629 if (pm_pend_link == 0) {
630 log::error(
631 "There are no links pending power mode changes; try to find other "
632 "pending changes");
633 btm_pm_continue_pending_mode_changes();
634 return;
635 }
636 if (pm_mode_db.count(pm_pend_link) == 0) {
637 log::error(
638 "Got PM change status for disconnected link {}; forgot to clean up "
639 "pm_pend_link?",
640 pm_pend_link);
641 btm_pm_continue_pending_mode_changes();
642 return;
643 }
644
645 tBTM_PM_MCB* p_cb = &pm_mode_db[pm_pend_link];
646
647 // if the command was not successful. Stay in the same state
648 tBTM_PM_STATUS pm_status = BTM_PM_STS_ERROR;
649
650 if (status == HCI_SUCCESS) {
651 p_cb->state = BTM_PM_ST_PENDING;
652 pm_status = BTM_PM_STS_PENDING;
653 }
654
655 /* notify the caller is appropriate */
656 if ((pm_pend_id != BTM_PM_SET_ONLY_ID) && (pm_reg_db.mask & BTM_PM_REG_SET)) {
657 const RawAddress bd_addr = pm_mode_db[pm_pend_link].bda_;
658 log::verbose("Notifying callback that link power mode is complete peer:{}", bd_addr);
659 (*pm_reg_db.cback)(bd_addr, pm_status, 0, status);
660 }
661
662 log::verbose("Clearing pending power mode link state:{}", power_mode_state_text(p_cb->state));
663 pm_pend_link = 0;
664
665 btm_pm_continue_pending_mode_changes();
666 }
667
668 /*******************************************************************************
669 *
670 * Function btm_process_mode_change
671 *
672 * Description This function is called when an HCI mode change event
673 * occurs.
674 *
675 * Input Parms hci_status - status of the event (HCI_SUCCESS if no errors)
676 * hci_handle - connection handle associated with the change
677 * mode - HCI_MODE_ACTIVE, HCI_MODE_HOLD, HCI_MODE_SNIFF, or
678 * HCI_MODE_PARK
679 * interval - number of baseband slots (meaning depends on
680 * mode)
681 *
682 * Returns none.
683 *
684 ******************************************************************************/
btm_pm_proc_mode_change(tHCI_STATUS hci_status,uint16_t hci_handle,tHCI_MODE hci_mode,uint16_t interval)685 void btm_pm_proc_mode_change(tHCI_STATUS hci_status, uint16_t hci_handle, tHCI_MODE hci_mode,
686 uint16_t interval) {
687 tBTM_PM_STATUS mode = static_cast<tBTM_PM_STATUS>(hci_mode);
688
689 /* update control block */
690 if (pm_mode_db.count(hci_handle) == 0) {
691 log::warn("Unable to find active acl for handle {}", hci_handle);
692 return;
693 }
694 tBTM_PM_MCB* p_cb = &pm_mode_db[hci_handle];
695
696 const tBTM_PM_STATE old_state = p_cb->state;
697 p_cb->state = mode;
698 p_cb->interval = interval;
699
700 log::info("Power mode switched from {}[{}] to {}[{}]", power_mode_state_text(old_state),
701 old_state, power_mode_state_text(p_cb->state), p_cb->state);
702
703 if ((p_cb->state == BTM_PM_ST_ACTIVE) || (p_cb->state == BTM_PM_ST_SNIFF)) {
704 l2c_OnHciModeChangeSendPendingPackets(p_cb->bda_);
705 }
706
707 (mode != BTM_PM_ST_ACTIVE)
708 ? power_telemetry::GetInstance().LogSniffStarted(hci_handle, p_cb->bda_)
709 : power_telemetry::GetInstance().LogSniffStopped(hci_handle, p_cb->bda_);
710
711 /* set req_mode HOLD mode->ACTIVE */
712 if ((mode == BTM_PM_MD_ACTIVE) && (p_cb->req_mode.mode == BTM_PM_MD_HOLD)) {
713 p_cb->req_mode.mode = BTM_PM_MD_ACTIVE;
714 }
715
716 /* new request has been made. - post a message to BTU task */
717 if (old_state & BTM_PM_STORED_MASK) {
718 btm_pm_snd_md_req(hci_handle, BTM_PM_SET_ONLY_ID, hci_handle, NULL);
719 } else {
720 for (auto& entry : pm_mode_db) {
721 if (entry.second.chg_ind) {
722 btm_pm_snd_md_req(entry.second.handle_, BTM_PM_SET_ONLY_ID, entry.second.handle_, NULL);
723 break;
724 }
725 }
726 }
727
728 /* notify registered parties */
729 if (pm_reg_db.mask & BTM_PM_REG_SET) {
730 (*pm_reg_db.cback)(p_cb->bda_, mode, interval, hci_status);
731 }
732 /*check if sco disconnect is waiting for the mode change */
733 btm_sco_disc_chk_pend_for_modechange(hci_handle);
734
735 /* If mode change was because of an active role switch or change link key */
736 btm_cont_rswitch_from_handle(hci_handle);
737 }
738
739 /*******************************************************************************
740 *
741 * Function btm_pm_proc_ssr_evt
742 *
743 * Description This function is called when an HCI sniff subrating event
744 * occurs.
745 *
746 * Returns none.
747 *
748 ******************************************************************************/
process_ssr_event(tHCI_STATUS status,uint16_t handle,uint16_t,uint16_t max_rx_lat)749 static void process_ssr_event(tHCI_STATUS status, uint16_t handle, uint16_t /* max_tx_lat */,
750 uint16_t max_rx_lat) {
751 if (pm_mode_db.count(handle) == 0) {
752 log::warn("Received sniff subrating event with no active ACL");
753 return;
754 }
755 tBTM_PM_MCB* p_cb = &pm_mode_db[handle];
756 auto bd_addr = p_cb->bda_;
757
758 bool use_ssr = true;
759 if (p_cb->interval == max_rx_lat) {
760 log::verbose("Sniff subrating unsupported so dropping to legacy sniff mode");
761 use_ssr = false;
762 } else {
763 log::verbose("Sniff subrating enabled");
764 }
765
766 int cnt = 0;
767 if (pm_reg_db.mask & BTM_PM_REG_SET) {
768 (*pm_reg_db.cback)(bd_addr, BTM_PM_STS_SSR, (use_ssr) ? 1 : 0, status);
769 cnt++;
770 }
771 log::debug(
772 "Notified sniff subrating registered clients cnt:{} peer:{} use_ssr:{} "
773 "status:{}",
774 cnt, bd_addr, use_ssr, hci_error_code_text(status));
775 }
776
btm_pm_on_sniff_subrating(tHCI_STATUS status,uint16_t handle,uint16_t maximum_transmit_latency,uint16_t maximum_receive_latency,uint16_t,uint16_t)777 void btm_pm_on_sniff_subrating(tHCI_STATUS status, uint16_t handle,
778 uint16_t maximum_transmit_latency, uint16_t maximum_receive_latency,
779 uint16_t /* minimum_remote_timeout */,
780 uint16_t /* minimum_local_timeout */) {
781 process_ssr_event(status, handle, maximum_transmit_latency, maximum_receive_latency);
782 }
783
btm_pm_proc_ssr_evt(uint8_t * p,uint16_t)784 void btm_pm_proc_ssr_evt(uint8_t* p, uint16_t /* evt_len */) {
785 uint8_t status;
786 uint16_t handle;
787 uint16_t max_tx_lat;
788 uint16_t max_rx_lat;
789
790 STREAM_TO_UINT8(status, p);
791 STREAM_TO_UINT16(handle, p);
792 STREAM_TO_UINT16(max_tx_lat, p);
793 STREAM_TO_UINT16(max_rx_lat, p);
794
795 process_ssr_event(static_cast<tHCI_STATUS>(status), handle, max_tx_lat, max_rx_lat);
796 }
797
798 /*******************************************************************************
799 *
800 * Function btm_pm_device_in_active_or_sniff_mode
801 *
802 * Description This function is called to check if in active or sniff mode
803 *
804 * Returns true, if in active or sniff mode
805 *
806 ******************************************************************************/
btm_pm_device_in_active_or_sniff_mode(void)807 static bool btm_pm_device_in_active_or_sniff_mode(void) {
808 /* The active state is the highest state-includes connected device and sniff
809 * mode*/
810
811 /* Covers active and sniff modes */
812 if (!pm_mode_db.empty()) {
813 return true;
814 }
815
816 /* Check BLE states */
817 if (!btm_cb.ble_ctr_cb.is_connection_state_idle()) {
818 log::verbose("- BLE state is not idle");
819 return true;
820 }
821
822 return false;
823 }
824
825 /*******************************************************************************
826 *
827 * Function BTM_PM_DeviceInScanState
828 *
829 * Description This function is called to check if in inquiry
830 *
831 * Returns true, if in inquiry
832 *
833 ******************************************************************************/
BTM_PM_DeviceInScanState(void)834 bool BTM_PM_DeviceInScanState(void) {
835 /* Check for inquiry */
836 if ((btm_cb.btm_inq_vars.inq_active & (BTM_GENERAL_INQUIRY | BTM_BLE_GENERAL_INQUIRY)) != 0) {
837 log::verbose("BTM_PM_DeviceInScanState- Inq active");
838 return true;
839 }
840
841 return false;
842 }
843
844 /*******************************************************************************
845 *
846 * Function BTM_PM_ReadControllerState
847 *
848 * Description This function is called to obtain the controller state
849 *
850 * Returns Controller State-BTM_CONTRL_ACTIVE, BTM_CONTRL_SCAN, and
851 * BTM_CONTRL_IDLE
852 *
853 ******************************************************************************/
BTM_PM_ReadControllerState(void)854 tBTM_CONTRL_STATE BTM_PM_ReadControllerState(void) {
855 if (btm_pm_device_in_active_or_sniff_mode()) {
856 return BTM_CONTRL_ACTIVE;
857 } else if (BTM_PM_DeviceInScanState()) {
858 return BTM_CONTRL_SCAN;
859 } else {
860 return BTM_CONTRL_IDLE;
861 }
862 }
863
864 /*******************************************************************************
865 *
866 * Function BTM_PM_ReadSniffLinkCount
867 *
868 * Description Return the number of BT connection in sniff mode
869 *
870 * Returns Number of BT connection in sniff mode
871 *
872 ******************************************************************************/
BTM_PM_ReadSniffLinkCount(void)873 uint8_t BTM_PM_ReadSniffLinkCount(void) {
874 uint8_t count = 0;
875 for (auto& entry : pm_mode_db) {
876 if (entry.second.state == HCI_MODE_SNIFF) {
877 ++count;
878 }
879 }
880 return count;
881 }
882
883 /*******************************************************************************
884 *
885 * Function BTM_PM_ReadBleLinkCount
886 *
887 * Description Return the number of BLE connection
888 *
889 * Returns Number of BLE connection
890 *
891 ******************************************************************************/
BTM_PM_ReadBleLinkCount(void)892 uint8_t BTM_PM_ReadBleLinkCount(void) {
893 return btm_cb.ble_ctr_cb.link_count[HCI_ROLE_CENTRAL] +
894 btm_cb.ble_ctr_cb.link_count[HCI_ROLE_PERIPHERAL];
895 }
896
897 /*******************************************************************************
898 *
899 * Function BTM_PM_ReadBleScanDutyCycle
900 *
901 * Description Returns BLE scan duty cycle which is (window * 100) /
902 *interval
903 *
904 * Returns BLE scan duty cycle
905 *
906 ******************************************************************************/
BTM_PM_ReadBleScanDutyCycle(void)907 uint32_t BTM_PM_ReadBleScanDutyCycle(void) {
908 if (!btm_cb.ble_ctr_cb.is_ble_scan_active()) {
909 return 0;
910 }
911 uint32_t duty_cycle_1m = 0;
912 uint32_t duty_cycle_coded = 0;
913 if (btm_cb.ble_ctr_cb.inq_var.is_1m_phy_configured()) {
914 uint32_t scan_window = btm_cb.ble_ctr_cb.inq_var.scan_window_1m;
915 uint32_t scan_interval = btm_cb.ble_ctr_cb.inq_var.scan_interval_1m;
916 log::debug("LE 1m scan_window:{} scan interval:{}", scan_window, scan_interval);
917 duty_cycle_1m = (scan_window * 100) / scan_interval;
918 }
919 if (btm_cb.ble_ctr_cb.inq_var.is_coded_phy_configured()) {
920 uint32_t scan_window = btm_cb.ble_ctr_cb.inq_var.scan_window_coded;
921 uint32_t scan_interval = btm_cb.ble_ctr_cb.inq_var.scan_interval_coded;
922 log::debug("LE coded scan_window:{} scan interval:{}", scan_window, scan_interval);
923 duty_cycle_coded = (scan_window * 100) / scan_interval;
924 }
925 return std::max(duty_cycle_1m, duty_cycle_coded);
926 }
927
btm_pm_on_mode_change(tHCI_STATUS status,uint16_t handle,tHCI_MODE current_mode,uint16_t interval)928 void btm_pm_on_mode_change(tHCI_STATUS status, uint16_t handle, tHCI_MODE current_mode,
929 uint16_t interval) {
930 btm_sco_chk_pend_unpark(status, handle);
931 btm_pm_proc_mode_change(status, handle, current_mode, interval);
932 }
933