btm_pm.c 32 KB

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