btm_pm.c 32 KB

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