smp_br_main.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 2014-2015 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. #include "common/bt_target.h"
  19. #include <string.h>
  20. #include "smp_int.h"
  21. #if ( CLASSIC_BT_INCLUDED== TRUE && SMP_INCLUDED == TRUE)
  22. const char *const smp_br_state_name [SMP_BR_STATE_MAX + 1] = {
  23. "SMP_BR_STATE_IDLE",
  24. "SMP_BR_STATE_WAIT_APP_RSP",
  25. "SMP_BR_STATE_PAIR_REQ_RSP",
  26. "SMP_BR_STATE_BOND_PENDING",
  27. "SMP_BR_STATE_OUT_OF_RANGE"
  28. };
  29. const char *const smp_br_event_name [SMP_BR_MAX_EVT] = {
  30. "BR_PAIRING_REQ_EVT",
  31. "BR_PAIRING_RSP_EVT",
  32. "BR_CONFIRM_EVT",
  33. "BR_RAND_EVT",
  34. "BR_PAIRING_FAILED_EVT",
  35. "BR_ENCRPTION_INFO_EVT",
  36. "BR_MASTER_ID_EVT",
  37. "BR_ID_INFO_EVT",
  38. "BR_ID_ADDR_EVT",
  39. "BR_SIGN_INFO_EVT",
  40. "BR_SECURITY_REQ_EVT",
  41. "BR_PAIR_PUBLIC_KEY_EVT",
  42. "BR_PAIR_DHKEY_CHCK_EVT",
  43. "BR_PAIR_KEYPR_NOTIF_EVT",
  44. "BR_KEY_READY_EVT",
  45. "BR_ENCRYPTED_EVT",
  46. "BR_L2CAP_CONN_EVT",
  47. "BR_L2CAP_DISCONN_EVT",
  48. "BR_KEYS_RSP_EVT",
  49. "BR_API_SEC_GRANT_EVT",
  50. "BR_TK_REQ_EVT",
  51. "BR_AUTH_CMPL_EVT",
  52. "BR_ENC_REQ_EVT",
  53. "BR_BOND_REQ_EVT",
  54. "BR_DISCARD_SEC_REQ_EVT",
  55. "BR_OUT_OF_RANGE_EVT"
  56. };
  57. const char *smp_get_br_event_name(tSMP_BR_EVENT event);
  58. const char *smp_get_br_state_name(tSMP_BR_STATE state);
  59. #define SMP_BR_SM_IGNORE 0
  60. #define SMP_BR_NUM_ACTIONS 2
  61. #define SMP_BR_SME_NEXT_STATE 2
  62. #define SMP_BR_SM_NUM_COLS 3
  63. typedef const UINT8 (*tSMP_BR_SM_TBL)[SMP_BR_SM_NUM_COLS];
  64. enum {
  65. SMP_SEND_PAIR_REQ,
  66. SMP_BR_SEND_PAIR_RSP,
  67. SMP_SEND_PAIR_FAIL,
  68. SMP_SEND_ID_INFO,
  69. SMP_BR_PROC_PAIR_CMD,
  70. SMP_PROC_PAIR_FAIL,
  71. SMP_PROC_ID_INFO,
  72. SMP_PROC_ID_ADDR,
  73. SMP_PROC_SRK_INFO,
  74. SMP_BR_PROC_SEC_GRANT,
  75. SMP_BR_PROC_SL_KEYS_RSP,
  76. SMP_BR_KEY_DISTRIBUTION,
  77. SMP_BR_PAIRING_COMPLETE,
  78. SMP_SEND_APP_CBACK,
  79. SMP_BR_CHECK_AUTH_REQ,
  80. SMP_PAIR_TERMINATE,
  81. SMP_IDLE_TERMINATE,
  82. SMP_BR_SM_NO_ACTION
  83. };
  84. static const tSMP_ACT smp_br_sm_action[] = {
  85. smp_send_pair_req,
  86. smp_br_send_pair_response,
  87. smp_send_pair_fail,
  88. smp_send_id_info,
  89. smp_br_process_pairing_command,
  90. smp_proc_pair_fail,
  91. smp_proc_id_info,
  92. smp_proc_id_addr,
  93. smp_proc_srk_info,
  94. smp_br_process_security_grant,
  95. smp_br_process_slave_keys_response,
  96. smp_br_select_next_key,
  97. smp_br_pairing_complete,
  98. smp_send_app_cback,
  99. smp_br_check_authorization_request,
  100. smp_pair_terminate,
  101. smp_idle_terminate
  102. };
  103. static const UINT8 smp_br_all_table[][SMP_BR_SM_NUM_COLS] = {
  104. /* Event Action Next State */
  105. /* BR_PAIRING_FAILED */ {SMP_PROC_PAIR_FAIL, SMP_BR_PAIRING_COMPLETE, SMP_BR_STATE_IDLE},
  106. /* BR_AUTH_CMPL */ {SMP_SEND_PAIR_FAIL, SMP_BR_PAIRING_COMPLETE, SMP_BR_STATE_IDLE},
  107. /* BR_L2CAP_DISCONN */ {SMP_PAIR_TERMINATE, SMP_BR_SM_NO_ACTION, SMP_BR_STATE_IDLE}
  108. };
  109. /************ SMP Master FSM State/Event Indirection Table **************/
  110. static const UINT8 smp_br_master_entry_map[][SMP_BR_STATE_MAX] = {
  111. /* br_state name: Idle WaitApp Pair Bond
  112. Rsp ReqRsp Pend */
  113. /* BR_PAIRING_REQ */ { 0, 0, 0, 0 },
  114. /* BR_PAIRING_RSP */ { 0, 0, 1, 0 },
  115. /* BR_CONFIRM */ { 0, 0, 0, 0 },
  116. /* BR_RAND */ { 0, 0, 0, 0 },
  117. /* BR_PAIRING_FAILED */ { 0, 0x81, 0x81, 0 },
  118. /* BR_ENCRPTION_INFO */ { 0, 0, 0, 0 },
  119. /* BR_MASTER_ID */ { 0, 0, 0, 0 },
  120. /* BR_ID_INFO */ { 0, 0, 0, 1 },
  121. /* BR_ID_ADDR */ { 0, 0, 0, 2 },
  122. /* BR_SIGN_INFO */ { 0, 0, 0, 3 },
  123. /* BR_SECURITY_REQ */ { 0, 0, 0, 0 },
  124. /* BR_PAIR_PUBLIC_KEY_EVT */ { 0, 0, 0, 0 },
  125. /* BR_PAIR_DHKEY_CHCK_EVT */ { 0, 0, 0, 0 },
  126. /* BR_PAIR_KEYPR_NOTIF_EVT */ { 0, 0, 0, 0 },
  127. /* BR_KEY_READY */ { 0, 0, 0, 0 },
  128. /* BR_ENCRYPTED */ { 0, 0, 0, 0 },
  129. /* BR_L2CAP_CONN */ { 1, 0, 0, 0 },
  130. /* BR_L2CAP_DISCONN */ { 2, 0x83, 0x83, 0x83 },
  131. /* BR_KEYS_RSP */ { 0, 1, 0, 0 },
  132. /* BR_API_SEC_GRANT */ { 0, 0, 0, 0 },
  133. /* BR_TK_REQ */ { 0, 0, 0, 0 },
  134. /* BR_AUTH_CMPL */ { 0, 0x82, 0x82, 0x82 },
  135. /* BR_ENC_REQ */ { 0, 0, 0, 0 },
  136. /* BR_BOND_REQ */ { 0, 0, 2, 0 },
  137. /* BR_DISCARD_SEC_REQ */ { 0, 0, 0, 0 }
  138. };
  139. static const UINT8 smp_br_master_idle_table[][SMP_BR_SM_NUM_COLS] = {
  140. /* Event Action Next State */
  141. /* BR_L2CAP_CONN */ {SMP_SEND_APP_CBACK, SMP_BR_SM_NO_ACTION, SMP_BR_STATE_WAIT_APP_RSP},
  142. /* BR_L2CAP_DISCONN */ {SMP_IDLE_TERMINATE, SMP_BR_SM_NO_ACTION, SMP_BR_STATE_IDLE}
  143. };
  144. static const UINT8 smp_br_master_wait_appln_response_table[][SMP_BR_SM_NUM_COLS] = {
  145. /* Event Action Next State */
  146. /* BR_KEYS_RSP */{SMP_SEND_PAIR_REQ, SMP_BR_SM_NO_ACTION, SMP_BR_STATE_PAIR_REQ_RSP}
  147. };
  148. static const UINT8 smp_br_master_pair_request_response_table [][SMP_BR_SM_NUM_COLS] = {
  149. /* Event Action Next State */
  150. /* BR_PAIRING_RSP */ {SMP_BR_PROC_PAIR_CMD, SMP_BR_CHECK_AUTH_REQ, SMP_BR_STATE_PAIR_REQ_RSP},
  151. /* BR_BOND_REQ */ {SMP_BR_SM_NO_ACTION, SMP_BR_SM_NO_ACTION, SMP_BR_STATE_BOND_PENDING}
  152. };
  153. static const UINT8 smp_br_master_bond_pending_table[][SMP_BR_SM_NUM_COLS] = {
  154. /* Event Action Next State */
  155. /* BR_ID_INFO */{SMP_PROC_ID_INFO, SMP_BR_SM_NO_ACTION, SMP_BR_STATE_BOND_PENDING},
  156. /* BR_ID_ADDR */{SMP_PROC_ID_ADDR, SMP_BR_SM_NO_ACTION, SMP_BR_STATE_BOND_PENDING},
  157. /* BR_SIGN_INFO */{SMP_PROC_SRK_INFO, SMP_BR_SM_NO_ACTION, SMP_BR_STATE_BOND_PENDING}
  158. };
  159. static const UINT8 smp_br_slave_entry_map[][SMP_BR_STATE_MAX] = {
  160. /* br_state name: Idle WaitApp Pair Bond
  161. Rsp ReqRsp Pend */
  162. /* BR_PAIRING_REQ */ { 1, 0, 0, 0 },
  163. /* BR_PAIRING_RSP */ { 0, 0, 0, 0 },
  164. /* BR_CONFIRM */ { 0, 0, 0, 0 },
  165. /* BR_RAND */ { 0, 0, 0, 0 },
  166. /* BR_PAIRING_FAILED */ { 0, 0x81, 0x81, 0x81 },
  167. /* BR_ENCRPTION_INFO */ { 0, 0, 0, 0 },
  168. /* BR_MASTER_ID */ { 0, 0, 0, 0 },
  169. /* BR_ID_INFO */ { 0, 0, 0, 1 },
  170. /* BR_ID_ADDR */ { 0, 0, 0, 2 },
  171. /* BR_SIGN_INFO */ { 0, 0, 0, 3 },
  172. /* BR_SECURITY_REQ */ { 0, 0, 0, 0 },
  173. /* BR_PAIR_PUBLIC_KEY_EVT */ { 0, 0, 0, 0 },
  174. /* BR_PAIR_DHKEY_CHCK_EVT */ { 0, 0, 0, 0 },
  175. /* BR_PAIR_KEYPR_NOTIF_EVT */ { 0, 0, 0, 0 },
  176. /* BR_KEY_READY */ { 0, 0, 0, 0 },
  177. /* BR_ENCRYPTED */ { 0, 0, 0, 0 },
  178. /* BR_L2CAP_CONN */ { 0, 0, 0, 0 },
  179. /* BR_L2CAP_DISCONN */ { 0, 0x83, 0x83, 0x83 },
  180. /* BR_KEYS_RSP */ { 0, 2, 0, 0 },
  181. /* BR_API_SEC_GRANT */ { 0, 1, 0, 0 },
  182. /* BR_TK_REQ */ { 0, 0, 0, 0 },
  183. /* BR_AUTH_CMPL */ { 0, 0x82, 0x82, 0x82 },
  184. /* BR_ENC_REQ */ { 0, 0, 0, 0 },
  185. /* BR_BOND_REQ */ { 0, 3, 0, 0 },
  186. /* BR_DISCARD_SEC_REQ */ { 0, 0, 0, 0 }
  187. };
  188. static const UINT8 smp_br_slave_idle_table[][SMP_BR_SM_NUM_COLS] = {
  189. /* Event Action Next State */
  190. /* BR_PAIRING_REQ */ {SMP_BR_PROC_PAIR_CMD, SMP_SEND_APP_CBACK, SMP_BR_STATE_WAIT_APP_RSP}
  191. };
  192. static const UINT8 smp_br_slave_wait_appln_response_table [][SMP_BR_SM_NUM_COLS] = {
  193. /* Event Action Next State */
  194. /* BR_API_SEC_GRANT */ {SMP_BR_PROC_SEC_GRANT, SMP_SEND_APP_CBACK, SMP_BR_STATE_WAIT_APP_RSP},
  195. /* BR_KEYS_RSP */{SMP_BR_PROC_SL_KEYS_RSP, SMP_BR_CHECK_AUTH_REQ, SMP_BR_STATE_WAIT_APP_RSP},
  196. /* BR_BOND_REQ */ {SMP_BR_KEY_DISTRIBUTION, SMP_BR_SM_NO_ACTION, SMP_BR_STATE_BOND_PENDING}
  197. };
  198. static const UINT8 smp_br_slave_bond_pending_table[][SMP_BR_SM_NUM_COLS] = {
  199. /* Event Action Next State */
  200. /* BR_ID_INFO */ {SMP_PROC_ID_INFO, SMP_BR_SM_NO_ACTION, SMP_BR_STATE_BOND_PENDING},
  201. /* BR_ID_ADDR */ {SMP_PROC_ID_ADDR, SMP_BR_SM_NO_ACTION, SMP_BR_STATE_BOND_PENDING},
  202. /* BR_SIGN_INFO */ {SMP_PROC_SRK_INFO, SMP_BR_SM_NO_ACTION, SMP_BR_STATE_BOND_PENDING}
  203. };
  204. static const tSMP_BR_SM_TBL smp_br_state_table[][2] = {
  205. /* SMP_BR_STATE_IDLE */
  206. {smp_br_master_idle_table, smp_br_slave_idle_table},
  207. /* SMP_BR_STATE_WAIT_APP_RSP */
  208. {smp_br_master_wait_appln_response_table, smp_br_slave_wait_appln_response_table},
  209. /* SMP_BR_STATE_PAIR_REQ_RSP */
  210. {smp_br_master_pair_request_response_table, NULL},
  211. /* SMP_BR_STATE_BOND_PENDING */
  212. {smp_br_master_bond_pending_table, smp_br_slave_bond_pending_table},
  213. };
  214. typedef const UINT8 (*tSMP_BR_ENTRY_TBL)[SMP_BR_STATE_MAX];
  215. static const tSMP_BR_ENTRY_TBL smp_br_entry_table[] = {
  216. smp_br_master_entry_map,
  217. smp_br_slave_entry_map
  218. };
  219. #define SMP_BR_ALL_TABLE_MASK 0x80
  220. /*******************************************************************************
  221. ** Function smp_set_br_state
  222. ** Returns None
  223. *******************************************************************************/
  224. void smp_set_br_state(tSMP_BR_STATE br_state)
  225. {
  226. if (br_state < SMP_BR_STATE_MAX) {
  227. SMP_TRACE_DEBUG( "BR_State change: %s(%d) ==> %s(%d)",
  228. smp_get_br_state_name(smp_cb.br_state), smp_cb.br_state,
  229. smp_get_br_state_name(br_state), br_state );
  230. smp_cb.br_state = br_state;
  231. } else {
  232. SMP_TRACE_DEBUG("%s invalid br_state =%d", __FUNCTION__, br_state );
  233. }
  234. }
  235. /*******************************************************************************
  236. ** Function smp_get_br_state
  237. ** Returns The smp_br state
  238. *******************************************************************************/
  239. tSMP_BR_STATE smp_get_br_state(void)
  240. {
  241. return smp_cb.br_state;
  242. }
  243. /*******************************************************************************
  244. ** Function smp_get_br_state_name
  245. ** Returns The smp_br state name.
  246. *******************************************************************************/
  247. const char *smp_get_br_state_name(tSMP_BR_STATE br_state)
  248. {
  249. const char *p_str = smp_br_state_name[SMP_BR_STATE_MAX];
  250. if (br_state < SMP_BR_STATE_MAX) {
  251. p_str = smp_br_state_name[br_state];
  252. }
  253. return p_str;
  254. }
  255. /*******************************************************************************
  256. ** Function smp_get_br_event_name
  257. ** Returns The smp_br event name.
  258. *******************************************************************************/
  259. const char *smp_get_br_event_name(tSMP_BR_EVENT event)
  260. {
  261. const char *p_str = smp_br_event_name[SMP_BR_MAX_EVT - 1];
  262. if (event < SMP_BR_MAX_EVT) {
  263. p_str = smp_br_event_name[event - 1];
  264. }
  265. return p_str;
  266. }
  267. /*******************************************************************************
  268. **
  269. ** Function smp_br_state_machine_event
  270. **
  271. ** Description Handle events to the state machine. It looks up the entry
  272. ** in the smp_br_entry_table array.
  273. ** If it is a valid entry, it gets the state table.Set the next state,
  274. ** if not NULL state. Execute the action function according to the
  275. ** state table. If the state returned by action function is not NULL
  276. ** state, adjust the new state to the returned state.
  277. **
  278. ** Returns void.
  279. **
  280. *******************************************************************************/
  281. void smp_br_state_machine_event(tSMP_CB *p_cb, tSMP_BR_EVENT event, void *p_data)
  282. {
  283. tSMP_BR_STATE curr_state = p_cb->br_state;
  284. tSMP_BR_SM_TBL state_table;
  285. UINT8 action, entry;
  286. tSMP_BR_ENTRY_TBL entry_table = smp_br_entry_table[p_cb->role];
  287. SMP_TRACE_EVENT("main %s", __func__);
  288. if (curr_state >= SMP_BR_STATE_MAX) {
  289. SMP_TRACE_DEBUG( "Invalid br_state: %d", curr_state) ;
  290. return;
  291. }
  292. SMP_TRACE_DEBUG( "SMP Role: %s State: [%s (%d)], Event: [%s (%d)]",
  293. (p_cb->role == HCI_ROLE_SLAVE) ? "Slave" : "Master",
  294. smp_get_br_state_name( p_cb->br_state),
  295. p_cb->br_state, smp_get_br_event_name(event), event) ;
  296. /* look up the state table for the current state */
  297. /* lookup entry / w event & curr_state */
  298. /* If entry is ignore, return.
  299. * Otherwise, get state table (according to curr_state or all_state) */
  300. if ((event <= SMP_BR_MAX_EVT) && ( (entry = entry_table[event - 1][curr_state])
  301. != SMP_BR_SM_IGNORE )) {
  302. if (entry & SMP_BR_ALL_TABLE_MASK) {
  303. entry &= ~SMP_BR_ALL_TABLE_MASK;
  304. state_table = smp_br_all_table;
  305. } else {
  306. state_table = smp_br_state_table[curr_state][p_cb->role];
  307. }
  308. } else {
  309. SMP_TRACE_DEBUG( "Ignore event [%s (%d)] in state [%s (%d)]",
  310. smp_get_br_event_name(event), event,
  311. smp_get_br_state_name(curr_state), curr_state);
  312. return;
  313. }
  314. /* Get possible next state from state table. */
  315. smp_set_br_state(state_table[entry - 1][SMP_BR_SME_NEXT_STATE]);
  316. /* If action is not ignore, clear param, exec action and get next state.
  317. * The action function may set the Param for cback.
  318. * Depending on param, call cback or free buffer. */
  319. /* execute action functions */
  320. for (UINT8 i = 0; i < SMP_BR_NUM_ACTIONS; i++) {
  321. if ((action = state_table[entry - 1][i]) != SMP_BR_SM_NO_ACTION) {
  322. (*smp_br_sm_action[action])(p_cb, (tSMP_INT_DATA *)p_data);
  323. } else {
  324. break;
  325. }
  326. }
  327. SMP_TRACE_DEBUG( "result state = %s", smp_get_br_state_name( p_cb->br_state ) ) ;
  328. }
  329. #endif