btm_pm.c 31 KB

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