btm_pm.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  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) ) { /* command pending */
  218. if (p_acl_cb->hci_handle != btm_cb.pm_pend_link_hdl) {
  219. /* set the stored mask */
  220. p_cb->state |= BTM_PM_STORED_MASK;
  221. BTM_TRACE_DEBUG( "btm_pm state stored:%d", p_acl_cb->hci_handle);
  222. }
  223. return BTM_CMD_STORED;
  224. }
  225. return btm_pm_snd_md_req(pm_id, p_acl_cb->hci_handle, p_mode);
  226. }
  227. /*******************************************************************************
  228. **
  229. ** Function BTM_ReadPowerMode
  230. **
  231. ** Description This returns the current mode for a specific
  232. ** ACL connection.
  233. **
  234. ** Input Param remote_bda - device address of desired ACL connection
  235. **
  236. ** Output Param p_mode - address where the current mode is copied into.
  237. ** BTM_ACL_MODE_NORMAL
  238. ** BTM_ACL_MODE_HOLD
  239. ** BTM_ACL_MODE_SNIFF
  240. ** BTM_ACL_MODE_PARK
  241. ** (valid only if return code is BTM_SUCCESS)
  242. **
  243. ** Returns BTM_SUCCESS if successful,
  244. ** BTM_UNKNOWN_ADDR if bd addr is not active or bad
  245. **
  246. *******************************************************************************/
  247. tBTM_STATUS BTM_ReadPowerMode (BD_ADDR remote_bda, tBTM_PM_MODE *p_mode)
  248. {
  249. tACL_CONN *p_acl_cb = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
  250. if (!p_acl_cb) {
  251. return (BTM_UNKNOWN_ADDR);
  252. }
  253. *p_mode = p_acl_cb->p_pm_mode_db->state;
  254. return BTM_SUCCESS;
  255. }
  256. /*******************************************************************************
  257. **
  258. ** Function BTM_SetSsrParams
  259. **
  260. ** Description This sends the given SSR parameters for the given ACL
  261. ** connection if it is in ACTIVE mode.
  262. **
  263. ** Input Param remote_bda - device address of desired ACL connection
  264. ** max_lat - maximum latency (in 0.625ms)(0-0xFFFE)
  265. ** min_rmt_to - minimum remote timeout
  266. ** min_loc_to - minimum local timeout
  267. **
  268. **
  269. ** Returns BTM_SUCCESS if the HCI command is issued successful,
  270. ** BTM_UNKNOWN_ADDR if bd addr is not active or bad
  271. ** BTM_CMD_STORED if the command is stored
  272. **
  273. *******************************************************************************/
  274. tBTM_STATUS BTM_SetSsrParams (BD_ADDR remote_bda, UINT16 max_lat,
  275. UINT16 min_rmt_to, UINT16 min_loc_to)
  276. {
  277. #if (BTM_SSR_INCLUDED == TRUE)
  278. int acl_ind;
  279. tBTM_PM_MCB *p_cb;
  280. tACL_CONN *p_acl_cb = NULL;
  281. if ( (acl_ind = btm_pm_find_acl_ind(remote_bda)) == MAX_L2CAP_LINKS) {
  282. return (BTM_UNKNOWN_ADDR);
  283. }
  284. p_acl_cb = btm_bda_to_acl(remote_bda);
  285. if (!p_acl_cb) {
  286. return (BTM_UNKNOWN_ADDR);
  287. }
  288. p_cb = p_acl_cb->p_pm_mode_db;
  289. if (BTM_PM_STS_ACTIVE == p_cb->state ||
  290. BTM_PM_STS_SNIFF == p_cb->state) {
  291. if (btsnd_hcic_sniff_sub_rate(p_acl_cb->hci_handle, max_lat,
  292. min_rmt_to, min_loc_to)) {
  293. return BTM_SUCCESS;
  294. } else {
  295. return BTM_NO_RESOURCES;
  296. }
  297. }
  298. p_cb->max_lat = max_lat;
  299. p_cb->min_rmt_to = min_rmt_to;
  300. p_cb->min_loc_to = min_loc_to;
  301. return BTM_CMD_STORED;
  302. #else
  303. return BTM_ILLEGAL_ACTION;
  304. #endif // BTM_SSR_INCLUDED
  305. }
  306. /*******************************************************************************
  307. **
  308. ** Function btm_pm_reset
  309. **
  310. ** Description as a part of the BTM reset process.
  311. **
  312. ** Returns void
  313. **
  314. *******************************************************************************/
  315. void btm_pm_reset(void)
  316. {
  317. int xx;
  318. tBTM_PM_STATUS_CBACK *cb = NULL;
  319. /* clear the pending request for application */
  320. if ( (btm_cb.pm_pend_id != BTM_PM_SET_ONLY_ID) &&
  321. (btm_cb.pm_reg_db[btm_cb.pm_pend_id].mask & BTM_PM_REG_NOTIF) ) {
  322. cb = btm_cb.pm_reg_db[btm_cb.pm_pend_id].cback;
  323. }
  324. /* clear the register record */
  325. for (xx = 0; xx < BTM_MAX_PM_RECORDS; xx++) {
  326. btm_cb.pm_reg_db[xx].mask = BTM_PM_REC_NOT_USED;
  327. }
  328. if (cb != NULL && btm_cb.pm_pend_link_hdl != BTM_INVALID_HANDLE) {
  329. (*cb)((btm_handle_to_acl(btm_cb.pm_pend_link_hdl))->remote_addr, BTM_PM_STS_ERROR, BTM_DEV_RESET, 0);
  330. }
  331. /* no command pending */
  332. btm_cb.pm_pend_link_hdl = BTM_INVALID_HANDLE;
  333. }
  334. /*******************************************************************************
  335. **
  336. ** Function btm_pm_sm_alloc
  337. **
  338. ** Description This function initializes the control block of an ACL link.
  339. ** It is called when an ACL connection is created.
  340. **
  341. ** Returns void
  342. **
  343. *******************************************************************************/
  344. tBTM_PM_MCB *btm_pm_sm_alloc(void)
  345. {
  346. tBTM_PM_MCB *p_db = (tBTM_PM_MCB *) osi_malloc(sizeof(tBTM_PM_MCB)); /* per ACL link */
  347. if (p_db) {
  348. memset (p_db, 0, sizeof(tBTM_PM_MCB));
  349. p_db->state = BTM_PM_ST_ACTIVE;
  350. if (list_length(btm_cb.p_pm_mode_db_list) >= MAX_L2CAP_LINKS) {
  351. osi_free(p_db);
  352. p_db = NULL;
  353. }
  354. if (!list_append(btm_cb.p_pm_mode_db_list, p_db)) {
  355. osi_free(p_db);
  356. p_db = NULL;
  357. }
  358. }
  359. return p_db;
  360. }
  361. /*******************************************************************************
  362. **
  363. ** Function btm_pm_find_acl_ind
  364. **
  365. ** Description This function initializes the control block of an ACL link.
  366. ** It is called when an ACL connection is created.
  367. **
  368. ** Returns void
  369. **
  370. *******************************************************************************/
  371. /*******************************************************************************
  372. **
  373. ** Function btm_pm_compare_modes
  374. ** Description get the "more active" mode of the 2
  375. ** Returns void
  376. **
  377. *******************************************************************************/
  378. 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)
  379. {
  380. UINT8 res;
  381. if (p_md1 == NULL) {
  382. *p_res = *p_md2;
  383. p_res->mode &= ~BTM_PM_MD_FORCE;
  384. return p_md2;
  385. }
  386. if (p_md2->mode == BTM_PM_MD_ACTIVE || p_md1->mode == BTM_PM_MD_ACTIVE) {
  387. return NULL;
  388. }
  389. /* check if force bit is involved */
  390. if (p_md1->mode & BTM_PM_MD_FORCE) {
  391. *p_res = *p_md1;
  392. p_res->mode &= ~BTM_PM_MD_FORCE;
  393. return p_res;
  394. }
  395. if (p_md2->mode & BTM_PM_MD_FORCE) {
  396. *p_res = *p_md2;
  397. p_res->mode &= ~BTM_PM_MD_FORCE;
  398. return p_res;
  399. }
  400. res = (p_md1->mode - 1) * BTM_PM_NUM_SET_MODES + (p_md2->mode - 1);
  401. res = btm_pm_md_comp_matrix[res];
  402. switch (res) {
  403. case BTM_PM_GET_MD1:
  404. *p_res = *p_md1;
  405. return p_md1;
  406. case BTM_PM_GET_MD2:
  407. *p_res = *p_md2;
  408. return p_md2;
  409. case BTM_PM_GET_COMP:
  410. p_res->mode = p_md1->mode;
  411. /* min of the two */
  412. p_res->max = (p_md1->max < p_md2->max) ? (p_md1->max) : (p_md2->max);
  413. /* max of the two */
  414. p_res->min = (p_md1->min > p_md2->min) ? (p_md1->min) : (p_md2->min);
  415. /* the intersection is NULL */
  416. if ( p_res->max < p_res->min) {
  417. return NULL;
  418. }
  419. if (p_res->mode == BTM_PM_MD_SNIFF) {
  420. /* max of the two */
  421. p_res->attempt = (p_md1->attempt > p_md2->attempt) ? (p_md1->attempt) : (p_md2->attempt);
  422. p_res->timeout = (p_md1->timeout > p_md2->timeout) ? (p_md1->timeout) : (p_md2->timeout);
  423. }
  424. return p_res;
  425. }
  426. return NULL;
  427. }
  428. /*******************************************************************************
  429. **
  430. ** Function btm_pm_get_set_mode
  431. ** Description get the resulting mode from the registered parties, then compare it
  432. ** with the requested mode, if the command is from an unregistered party.
  433. ** Returns void
  434. **
  435. *******************************************************************************/
  436. 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)
  437. {
  438. int xx, loop_max;
  439. tBTM_PM_PWR_MD *p_md = NULL;
  440. if (p_mode != NULL && p_mode->mode & BTM_PM_MD_FORCE) {
  441. *p_res = *p_mode;
  442. p_res->mode &= ~BTM_PM_MD_FORCE;
  443. return p_res->mode;
  444. }
  445. if (!p_mode) {
  446. loop_max = BTM_MAX_PM_RECORDS + 1;
  447. } else {
  448. loop_max = BTM_MAX_PM_RECORDS;
  449. }
  450. for ( xx = 0; xx < loop_max; xx++) {
  451. /* g through all the registered "set" parties */
  452. if (btm_cb.pm_reg_db[xx].mask & BTM_PM_REG_SET) {
  453. if (p_cb->req_mode[xx].mode == BTM_PM_MD_ACTIVE) {
  454. /* if at least one registered (SET) party says ACTIVE, stay active */
  455. return BTM_PM_MD_ACTIVE;
  456. } else {
  457. /* if registered parties give conflicting information, stay active */
  458. if ( (btm_pm_compare_modes(p_md, &p_cb->req_mode[xx], p_res)) == NULL) {
  459. return BTM_PM_MD_ACTIVE;
  460. }
  461. p_md = p_res;
  462. }
  463. }
  464. }
  465. /* if the resulting mode is NULL(nobody registers SET), use the requested mode */
  466. if (p_md == NULL) {
  467. if (p_mode) {
  468. *p_res = *((tBTM_PM_PWR_MD *)p_mode);
  469. } else { /* p_mode is NULL when btm_pm_snd_md_req is called from btm_pm_proc_mode_change */
  470. return BTM_PM_MD_ACTIVE;
  471. }
  472. } else {
  473. /* if the command is from unregistered party,
  474. compare the resulting mode from registered party*/
  475. if ( (pm_id == BTM_PM_SET_ONLY_ID) &&
  476. ((btm_pm_compare_modes(p_mode, p_md, p_res)) == NULL) ) {
  477. return BTM_PM_MD_ACTIVE;
  478. }
  479. }
  480. return p_res->mode;
  481. }
  482. /*******************************************************************************
  483. **
  484. ** Function btm_pm_snd_md_req
  485. ** Description get the resulting mode and send the resuest to host controller
  486. ** Returns tBTM_STATUS
  487. **, BOOLEAN *p_chg_ind
  488. *******************************************************************************/
  489. static tBTM_STATUS btm_pm_snd_md_req(UINT8 pm_id, UINT16 link_hdl, tBTM_PM_PWR_MD *p_mode)
  490. {
  491. tBTM_PM_PWR_MD md_res;
  492. tBTM_PM_MODE mode;
  493. tACL_CONN *p_acl_cb = btm_handle_to_acl(link_hdl);
  494. tBTM_PM_MCB *p_cb = p_acl_cb->p_pm_mode_db;
  495. BOOLEAN chg_ind = FALSE;
  496. mode = btm_pm_get_set_mode(pm_id, p_cb, p_mode, &md_res);
  497. md_res.mode = mode;
  498. #if BTM_PM_DEBUG == TRUE
  499. BTM_TRACE_DEBUG( "btm_pm_snd_md_req link_hdl:%d, mode: %d",
  500. link_hdl, mode);
  501. #endif // BTM_PM_DEBUG
  502. if ( p_cb->state == mode) {
  503. /* already in the resulting mode */
  504. if ( (mode == BTM_PM_MD_ACTIVE) ||
  505. ((md_res.max >= p_cb->interval) && (md_res.min <= p_cb->interval)) ) {
  506. return BTM_CMD_STORED;
  507. }
  508. /* Otherwise, needs to wake, then sleep */
  509. chg_ind = TRUE;
  510. }
  511. p_cb->chg_ind = chg_ind;
  512. /* cannot go directly from current mode to resulting mode. */
  513. if ( mode != BTM_PM_MD_ACTIVE && p_cb->state != BTM_PM_MD_ACTIVE) {
  514. p_cb->chg_ind = TRUE; /* needs to wake, then sleep */
  515. }
  516. if (p_cb->chg_ind == TRUE) { /* needs to wake first */
  517. md_res.mode = BTM_PM_MD_ACTIVE;
  518. }
  519. #if (BTM_SSR_INCLUDED == TRUE)
  520. else if (BTM_PM_MD_SNIFF == md_res.mode && p_cb->max_lat) {
  521. btsnd_hcic_sniff_sub_rate(link_hdl, p_cb->max_lat,
  522. p_cb->min_rmt_to, p_cb->min_loc_to);
  523. p_cb->max_lat = 0;
  524. }
  525. #endif // BTM_SSR_INCLUDED
  526. /* Default is failure */
  527. btm_cb.pm_pend_link_hdl = BTM_INVALID_HANDLE;
  528. /* send the appropriate HCI command */
  529. btm_cb.pm_pend_id = pm_id;
  530. #if BTM_PM_DEBUG == TRUE
  531. BTM_TRACE_DEBUG("btm_pm_snd_md_req state:0x%x, link_hdl: %d", p_cb->state, link_hdl);
  532. #endif // BTM_PM_DEBUG
  533. BTM_TRACE_DEBUG("%s switching from %s to %s.", __func__, mode_to_string(p_cb->state), mode_to_string(md_res.mode));
  534. switch (md_res.mode) {
  535. case BTM_PM_MD_ACTIVE:
  536. switch (p_cb->state) {
  537. case BTM_PM_MD_SNIFF:
  538. if (btsnd_hcic_exit_sniff_mode(link_hdl)) {
  539. btm_cb.pm_pend_link_hdl = link_hdl;
  540. }
  541. break;
  542. case BTM_PM_MD_PARK:
  543. if (btsnd_hcic_exit_park_mode(link_hdl)) {
  544. btm_cb.pm_pend_link_hdl = link_hdl;
  545. }
  546. break;
  547. default:
  548. /* Failure btm_cb.pm_pend_link = MAX_L2CAP_LINKS */
  549. break;
  550. }
  551. break;
  552. case BTM_PM_MD_HOLD:
  553. if (btsnd_hcic_hold_mode (link_hdl,
  554. md_res.max, md_res.min)) {
  555. btm_cb.pm_pend_link_hdl = link_hdl;
  556. }
  557. break;
  558. case BTM_PM_MD_SNIFF:
  559. if (btsnd_hcic_sniff_mode (link_hdl,
  560. md_res.max, md_res.min, md_res.attempt,
  561. md_res.timeout)) {
  562. btm_cb.pm_pend_link_hdl = link_hdl;
  563. }
  564. break;
  565. case BTM_PM_MD_PARK:
  566. if (btsnd_hcic_park_mode (link_hdl,
  567. md_res.max, md_res.min)) {
  568. btm_cb.pm_pend_link_hdl = link_hdl;
  569. }
  570. break;
  571. default:
  572. /* Failure btm_cb.pm_pend_link = MAX_L2CAP_LINKS */
  573. break;
  574. }
  575. if (btm_cb.pm_pend_link_hdl == BTM_INVALID_HANDLE) {
  576. /* the command was not sent */
  577. #if BTM_PM_DEBUG == TRUE
  578. BTM_TRACE_DEBUG( "pm_pend_link_hdl: %d", btm_cb.pm_pend_link_hdl);
  579. #endif // BTM_PM_DEBUG
  580. return (BTM_NO_RESOURCES);
  581. }
  582. return BTM_CMD_STARTED;
  583. }
  584. /*******************************************************************************
  585. **
  586. ** Function btm_pm_check_stored
  587. **
  588. ** Description This function is called when an HCI command status event occurs
  589. ** to check if there's any PM command issued while waiting for
  590. ** HCI command status.
  591. **
  592. ** Returns none.
  593. **
  594. *******************************************************************************/
  595. static void btm_pm_check_stored(void)
  596. {
  597. tACL_CONN *p_acl_cb = NULL;
  598. list_node_t *p_node = NULL;
  599. for (p_node = list_begin(btm_cb.p_acl_db_list); p_node; p_node = list_next(p_node)) {
  600. p_acl_cb = list_node(p_node);
  601. if (p_acl_cb->p_pm_mode_db->state & BTM_PM_STORED_MASK) {
  602. p_acl_cb->p_pm_mode_db->state &= ~BTM_PM_STORED_MASK;
  603. BTM_TRACE_DEBUG( "btm_pm_check_stored :%d", p_acl_cb->hci_handle);
  604. btm_pm_snd_md_req(BTM_PM_SET_ONLY_ID, p_acl_cb->hci_handle, NULL);
  605. break;
  606. }
  607. }
  608. }
  609. /*******************************************************************************
  610. **
  611. ** Function btm_pm_proc_cmd_status
  612. **
  613. ** Description This function is called when an HCI command status event occurs
  614. ** for power manager related commands.
  615. **
  616. ** Input Parms status - status of the event (HCI_SUCCESS if no errors)
  617. **
  618. ** Returns none.
  619. **
  620. *******************************************************************************/
  621. void btm_pm_proc_cmd_status(UINT8 status)
  622. {
  623. tBTM_PM_MCB *p_cb;
  624. tBTM_PM_STATUS pm_status;
  625. tACL_CONN *p_acl_cb;
  626. if (btm_cb.pm_pend_link_hdl == BTM_INVALID_HANDLE) {
  627. return;
  628. }
  629. p_acl_cb = btm_handle_to_acl(btm_cb.pm_pend_link_hdl);
  630. if (p_acl_cb == NULL) {
  631. return;
  632. }
  633. p_cb = p_acl_cb->p_pm_mode_db;
  634. if (status == HCI_SUCCESS) {
  635. p_cb->state = BTM_PM_ST_PENDING;
  636. pm_status = BTM_PM_STS_PENDING;
  637. #if BTM_PM_DEBUG == TRUE
  638. BTM_TRACE_DEBUG( "btm_pm_proc_cmd_status new state:0x%x", p_cb->state);
  639. #endif // BTM_PM_DEBUG
  640. } else { /* the command was not successfull. Stay in the same state */
  641. pm_status = BTM_PM_STS_ERROR;
  642. }
  643. /* notify the caller is appropriate */
  644. if ( (btm_cb.pm_pend_id != BTM_PM_SET_ONLY_ID) &&
  645. (btm_cb.pm_reg_db[btm_cb.pm_pend_id].mask & BTM_PM_REG_NOTIF) ) {
  646. (*btm_cb.pm_reg_db[btm_cb.pm_pend_id].cback)(p_acl_cb->remote_addr, pm_status, 0, status);
  647. }
  648. /* no pending cmd now */
  649. #if BTM_PM_DEBUG == TRUE
  650. BTM_TRACE_DEBUG( "btm_pm_proc_cmd_status state:0x%x, pm_pend_link: %d(new: %d)",
  651. p_cb->state, btm_cb.pm_pend_link_hdl, MAX_L2CAP_LINKS);
  652. #endif // BTM_PM_DEBUG
  653. btm_cb.pm_pend_link_hdl = BTM_INVALID_HANDLE;
  654. btm_pm_check_stored();
  655. }
  656. /*******************************************************************************
  657. **
  658. ** Function btm_process_mode_change
  659. **
  660. ** Description This function is called when an HCI mode change event occurs.
  661. **
  662. ** Input Parms hci_status - status of the event (HCI_SUCCESS if no errors)
  663. ** hci_handle - connection handle associated with the change
  664. ** mode - HCI_MODE_ACTIVE, HCI_MODE_HOLD, HCI_MODE_SNIFF, or HCI_MODE_PARK
  665. ** interval - number of baseband slots (meaning depends on mode)
  666. **
  667. ** Returns none.
  668. **
  669. *******************************************************************************/
  670. void btm_pm_proc_mode_change (UINT8 hci_status, UINT16 hci_handle, UINT8 mode, UINT16 interval)
  671. {
  672. tACL_CONN *p;
  673. tBTM_PM_MCB *p_cb = NULL;
  674. int yy;
  675. tBTM_PM_STATE old_state;
  676. tL2C_LCB *p_lcb;
  677. /* get the index to acl_db */
  678. p = btm_handle_to_acl(hci_handle);
  679. if (!p) {
  680. return;
  681. }
  682. /* update control block */
  683. p_cb = p->p_pm_mode_db;
  684. old_state = p_cb->state;
  685. p_cb->state = mode;
  686. p_cb->interval = interval;
  687. BTM_TRACE_DEBUG("%s switched from %s to %s.", __func__, mode_to_string(old_state), mode_to_string(p_cb->state));
  688. if ((p_lcb = l2cu_find_lcb_by_bd_addr(p->remote_addr, BT_TRANSPORT_BR_EDR)) != NULL) {
  689. if ((p_cb->state == BTM_PM_ST_ACTIVE) || (p_cb->state == BTM_PM_ST_SNIFF)) {
  690. /* There might be any pending packets due to SNIFF or PENDING state */
  691. /* Trigger L2C to start transmission of the pending packets. */
  692. BTM_TRACE_DEBUG("btm mode change to active; check l2c_link for outgoing packets");
  693. l2c_link_check_send_pkts(p_lcb, NULL, NULL);
  694. }
  695. }
  696. /* notify registered parties */
  697. for (yy = 0; yy <= BTM_MAX_PM_RECORDS; yy++) {
  698. /* set req_mode HOLD mode->ACTIVE */
  699. if ( (mode == BTM_PM_MD_ACTIVE) && (p_cb->req_mode[yy].mode == BTM_PM_MD_HOLD) ) {
  700. p_cb->req_mode[yy].mode = BTM_PM_MD_ACTIVE;
  701. }
  702. }
  703. /* new request has been made. - post a message to BTU task */
  704. if (old_state & BTM_PM_STORED_MASK) {
  705. #if BTM_PM_DEBUG == TRUE
  706. BTM_TRACE_DEBUG( "btm_pm_proc_mode_change: Sending stored req:%d", xx);
  707. #endif // BTM_PM_DEBUG
  708. btm_pm_snd_md_req(BTM_PM_SET_ONLY_ID, hci_handle, NULL);
  709. } else {
  710. list_node_t *p_node = NULL;
  711. for (p_node =(list_begin(btm_cb.p_pm_mode_db_list)); p_node; p_node = (list_next(p_node))) {
  712. p_cb = (tBTM_PM_MCB *)list_node(p_node);
  713. if (p_cb->chg_ind == TRUE) {
  714. #if BTM_PM_DEBUG == TRUE
  715. BTM_TRACE_DEBUG( "btm_pm_proc_mode_change: Sending PM req :%d", zz);
  716. #endif // BTM_PM_DEBUG
  717. btm_pm_snd_md_req(BTM_PM_SET_ONLY_ID, hci_handle, NULL);
  718. break;
  719. }
  720. }
  721. }
  722. /* notify registered parties */
  723. for (yy = 0; yy < BTM_MAX_PM_RECORDS; yy++) {
  724. if (btm_cb.pm_reg_db[yy].mask & BTM_PM_REG_NOTIF) {
  725. (*btm_cb.pm_reg_db[yy].cback)( p->remote_addr, mode, interval, hci_status);
  726. }
  727. }
  728. /* If mode change was because of an active role switch or change link key */
  729. btm_cont_rswitch(p, btm_find_dev(p->remote_addr), hci_status);
  730. }
  731. /*******************************************************************************
  732. **
  733. ** Function btm_pm_proc_ssr_evt
  734. **
  735. ** Description This function is called when an HCI sniff subrating event occurs.
  736. **
  737. ** Returns none.
  738. **
  739. *******************************************************************************/
  740. #if (BTM_SSR_INCLUDED == TRUE)
  741. void btm_pm_proc_ssr_evt (UINT8 *p, UINT16 evt_len)
  742. {
  743. UINT8 status;
  744. UINT16 handle;
  745. UINT16 max_rx_lat;
  746. int xx, yy;
  747. tBTM_PM_MCB *p_cb;
  748. tACL_CONN *p_acl = NULL;
  749. UINT16 use_ssr = TRUE;
  750. UNUSED(evt_len);
  751. STREAM_TO_UINT8 (status, p);
  752. STREAM_TO_UINT16 (handle, p);
  753. /* get the index to acl_db */
  754. p += 2;
  755. STREAM_TO_UINT16 (max_rx_lat, p);
  756. p_acl = btm_handle_to_acl(handle);
  757. if (!p_acl) {
  758. return;
  759. }
  760. p_cb = p_acl->p_pm_mode_db;
  761. if (p_cb->interval == max_rx_lat) {
  762. /* using legacy sniff */
  763. use_ssr = FALSE;
  764. }
  765. /* notify registered parties */
  766. for (yy = 0; yy < BTM_MAX_PM_RECORDS; yy++) {
  767. if (btm_cb.pm_reg_db[yy].mask & BTM_PM_REG_NOTIF) {
  768. if ( p_acl) {
  769. (*btm_cb.pm_reg_db[yy].cback)( p_acl->remote_addr, BTM_PM_STS_SSR, use_ssr, status);
  770. }
  771. }
  772. }
  773. }
  774. #endif // BTM_SSR_INCLUDED
  775. /*******************************************************************************
  776. **
  777. ** Function btm_pm_device_in_active_or_sniff_mode
  778. **
  779. ** Description This function is called to check if in active or sniff mode
  780. **
  781. ** Returns TRUE, if in active or sniff mode
  782. **
  783. *******************************************************************************/
  784. BOOLEAN btm_pm_device_in_active_or_sniff_mode(void)
  785. {
  786. /* The active state is the highest state-includes connected device and sniff mode*/
  787. /* Covers active and sniff modes */
  788. if (BTM_GetNumAclLinks() > 0) {
  789. BTM_TRACE_DEBUG("%s - ACL links: %d", __func__, BTM_GetNumAclLinks());
  790. return TRUE;
  791. }
  792. #if ((defined BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
  793. /* Check BLE states */
  794. if (btm_ble_get_conn_st() != BLE_CONN_IDLE) {
  795. BTM_TRACE_DEBUG("%s - BLE state: %x", __func__, btm_ble_get_conn_st());
  796. return TRUE;
  797. }
  798. #endif
  799. return FALSE;
  800. }
  801. /*******************************************************************************
  802. **
  803. ** Function btm_pm_device_in_scan_state
  804. **
  805. ** Description This function is called to check if in paging, inquiry or connecting mode
  806. **
  807. ** Returns TRUE, if in paging, inquiry or connecting mode
  808. **
  809. *******************************************************************************/
  810. BOOLEAN btm_pm_device_in_scan_state(void)
  811. {
  812. /* Scan state-paging, inquiry, and trying to connect */
  813. /* Check for paging */
  814. if (btm_cb.is_paging || (!fixed_queue_is_empty(btm_cb.page_queue)) ||
  815. BTM_BL_PAGING_STARTED == btm_cb.busy_level) {
  816. BTM_TRACE_DEBUG("btm_pm_device_in_scan_state- paging");
  817. return TRUE;
  818. }
  819. /* Check for inquiry */
  820. if ((btm_cb.btm_inq_vars.inq_active & (BTM_BR_INQ_ACTIVE_MASK | BTM_BLE_INQ_ACTIVE_MASK)) != 0) {
  821. BTM_TRACE_DEBUG("btm_pm_device_in_scan_state- Inq active");
  822. return TRUE;
  823. }
  824. return FALSE;
  825. }
  826. /*******************************************************************************
  827. **
  828. ** Function BTM_PM_ReadControllerState
  829. **
  830. ** Description This function is called to obtain the controller state
  831. **
  832. ** Returns Controller State-BTM_CONTRL_ACTIVE, BTM_CONTRL_SCAN, and BTM_CONTRL_IDLE
  833. **
  834. *******************************************************************************/
  835. tBTM_CONTRL_STATE BTM_PM_ReadControllerState(void)
  836. {
  837. if (TRUE == btm_pm_device_in_active_or_sniff_mode()) {
  838. return BTM_CONTRL_ACTIVE;
  839. } else if (TRUE == btm_pm_device_in_scan_state()) {
  840. return BTM_CONTRL_SCAN;
  841. } else {
  842. return BTM_CONTRL_IDLE;
  843. }
  844. }
  845. #if (!CONFIG_BT_STACK_NO_LOG)
  846. static const char *mode_to_string(tBTM_PM_MODE mode)
  847. {
  848. switch (mode) {
  849. case BTM_PM_MD_ACTIVE: return "ACTIVE";
  850. case BTM_PM_MD_SNIFF: return "SNIFF";
  851. case BTM_PM_MD_PARK: return "PARK";
  852. case BTM_PM_MD_HOLD: return "HOLD";
  853. default: return "UNKNOWN";
  854. }
  855. }
  856. #endif