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