1 /******************************************************************************
2 *
3 * Copyright (C) 2003-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 #include "bt_target.h"
20
21 #if SMP_INCLUDED == TRUE
22
23 #include <string.h>
24 #include "smp_int.h"
25
26
27 const char * const smp_state_name [] =
28 {
29 "SMP_ST_IDLE",
30 "SMP_ST_WAIT_APP_RSP",
31 "SMP_ST_SEC_REQ_PENDING",
32 "SMP_ST_PAIR_REQ_RSP",
33 "SMP_ST_WAIT_CONFIRM",
34 "SMP_ST_CONFIRM",
35 "SMP_ST_RAND",
36 "SMP_ST_ENC_PENDING",
37 "SMP_ST_BOND_PENDING",
38 "SMP_ST_RELEASE_DELAY",
39 "SMP_ST_MAX"
40 };
41 const char * const smp_event_name [] =
42 {
43 "PAIRING_REQ_EVT",
44 "PAIRING_RSP_EVT",
45 "CONFIRM_EVT",
46 "RAND_EVT",
47 "PAIRING_FAILED_EVT",
48 "ENC_INFO_EVT",
49 "MASTER_ID_EVT",
50 "ID_INFO_EVT",
51 "ID_ADDR_EVT",
52 "SIGN_INFO_EVT",
53 "SECURITY_REQ_EVT",
54 "KEY_READY_EVT",
55 "ENCRYPTED_EVT",
56 "L2CAP_CONN_EVT",
57 "L2CAP_DISCONN_EVT",
58 "API_IO_RSP_EVT",
59 "API_SEC_GRANT_EVT",
60 "TK_REQ_EVT",
61 "AUTH_CMPL_EVT",
62 "ENC_REQ_EVT",
63 "BOND_REQ_EVT",
64 "DISCARD_SEC_REQ_EVT",
65 "RELEASE_DELAY_EVT",
66 "RELEASE_DELAY_TOUT_EVT",
67 "MAX_EVT"
68 };
69 const char * smp_get_event_name(tSMP_EVENT event);
70 const char * smp_get_state_name(tSMP_STATE state);
71
72 #define SMP_SM_IGNORE 0
73 #define SMP_NUM_ACTIONS 2
74 #define SMP_SME_NEXT_STATE 2
75 #define SMP_SM_NUM_COLS 3
76 typedef const UINT8 (*tSMP_SM_TBL)[SMP_SM_NUM_COLS];
77
78 enum
79 {
80 SMP_PROC_SEC_REQ,
81 SMP_SEND_PAIR_REQ,
82 SMP_SEND_PAIR_RSP,
83 SMP_SEND_CONFIRM,
84 SMP_SEND_PAIR_FAIL,
85 SMP_SEND_INIT,
86 SMP_SEND_SECU_INFO,
87 SMP_SEND_ID_INFO,
88 SMP_SEND_LTK_REPLY,
89 SMP_PROC_PAIR_CMD,
90 SMP_PROC_PAIR_FAIL,
91 SMP_PROC_CONFIRM,
92 SMP_PROC_INIT,
93 SMP_PROC_ENC_INFO,
94 SMP_PROC_MASTER_ID,
95 SMP_PROC_ID_INFO,
96 SMP_PROC_ID_ADDR,
97 SMP_PROC_SRK_INFO,
98 SMP_PROC_SEC_GRANT,
99 SMP_PROC_SL_KEY,
100 SMP_PROC_COMPARE,
101 SMP_PROC_IO_RSP,
102 SMP_GENERATE_COMPARE,
103 SMP_GENERATE_CONFIRM,
104 SMP_GENERATE_STK,
105 SMP_KEY_DISTRIBUTE,
106 SMP_START_ENC,
107 SMP_PAIRING_CMPL,
108 SMP_DECIDE_ASSO_MODEL,
109 SMP_SEND_APP_CBACK,
110 SMP_CHECK_AUTH_REQ,
111 SMP_PAIR_TERMINATE,
112 SMP_ENC_CMPL,
113 SMP_PROC_DISCARD,
114 SMP_PROC_REL_DELAY,
115 SMP_PROC_REL_DELAY_TOUT,
116 SMP_SM_NO_ACTION
117 };
118
119 static const tSMP_ACT smp_sm_action[] =
120 {
121 smp_proc_sec_req,
122 smp_send_pair_req,
123 smp_send_pair_rsp,
124 smp_send_confirm,
125 smp_send_pair_fail,
126 smp_send_init,
127 smp_send_enc_info,
128 smp_send_id_info,
129 smp_send_ltk_reply,
130 smp_proc_pair_cmd,
131 smp_proc_pair_fail,
132 smp_proc_confirm,
133 smp_proc_init,
134 smp_proc_enc_info,
135 smp_proc_master_id,
136 smp_proc_id_info,
137 smp_proc_id_addr,
138 smp_proc_srk_info,
139 smp_proc_sec_grant,
140 smp_proc_sl_key,
141 smp_proc_compare,
142 smp_proc_io_rsp,
143 smp_generate_compare,
144 smp_generate_confirm,
145 smp_generate_stk,
146 smp_key_distribution,
147 smp_start_enc,
148 smp_pairing_cmpl,
149 smp_decide_asso_model,
150 smp_send_app_cback,
151 smp_check_auth_req,
152 smp_pair_terminate,
153 smp_enc_cmpl,
154 smp_proc_discard,
155 smp_proc_release_delay,
156 smp_proc_release_delay_tout,
157 };
158 /************ SMP Master FSM State/Event Indirection Table **************/
159 static const UINT8 smp_ma_entry_map[][SMP_ST_MAX] =
160 {
161 /* state name: Idle WaitApp SecReq Pair Wait Confirm Init Enc Bond Rel
162 Rsp Pend ReqRsp Cfm Pend Pend Delay */
163 /* PAIR_REQ */{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
164 /* PAIR_RSP */{ 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
165 /* CONFIRM */{ 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 },
166 /* INIT */{ 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 },
167 /* PAIR_FAIL */{ 0, 0x81, 0, 0x81, 0x81,0x81, 0x81,0, 0, 0 },
168 /* ENC_INFO */{ 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 },
169 /* MASTER_ID */{ 0, 0, 0, 0, 0, 0, 0, 0, 4, 0 },
170 /* ID_INFO */{ 0, 0, 0, 0, 0, 0, 0, 0, 2, 0 },
171 /* ID_ADDR */{ 0, 0, 0, 0, 0, 0, 0, 0, 5, 0 },
172 /* SIGN_INFO */{ 0, 0, 0, 0, 0, 0, 0, 0, 3, 0 },
173 /* SEC_REQ */{ 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
174 /* KEY_READY */{ 0, 3, 0, 3, 1, 0, 2, 1, 6, 0 },
175 /* ENC_CMPL */{ 0, 0, 0, 0, 0, 0, 0, 2, 0, 0 },
176 /* L2C_CONN */{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
177 /* L2C_DISC */{ 0x83, 0x83, 0, 0x83, 0x83,0x83, 0x83,0x83, 0x83, 2 },
178 /* IO_RSP */{ 0, 2, 0, 0, 0, 0, 0, 0, 0, 0 },
179 /* SEC_GRANT */{ 0, 1, 0, 0, 0, 0, 0, 0, 0, 0 },
180 /* TK_REQ */{ 0, 0, 0, 2, 0, 0, 0, 0, 0, 0 },
181 /* AUTH_CMPL */{ 0, 0x82, 0, 0x82, 0x82,0x82, 0x82,0x82, 0x82, 0 },
182 /* ENC_REQ */{ 0, 4, 0, 0, 0, 0, 3, 0, 0, 0 },
183 /* BOND_REQ */{ 0, 0, 0, 0, 0, 0, 0, 3, 0, 0 },
184 /* DISCARD_SEC_REQ */{ 0, 5, 0, 0, 0, 0, 0, 3, 0, 0 },
185 /* RELEASE_DELAY */{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
186 /* RELEASE_DELAY_TOUT */{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 2 },
187 };
188
189 static const UINT8 smp_all_table[][SMP_SM_NUM_COLS] = {
190 /* Event Action Next State */
191 /* PAIR_FAIL */ {SMP_PROC_PAIR_FAIL, SMP_PROC_REL_DELAY_TOUT, SMP_ST_IDLE},
192 /* AUTH_CMPL */ {SMP_SEND_PAIR_FAIL, SMP_PAIRING_CMPL, SMP_ST_RELEASE_DELAY},
193 /* L2C_DISC */ {SMP_PAIR_TERMINATE, SMP_SM_NO_ACTION, SMP_ST_IDLE}
194 };
195
196 static const UINT8 smp_ma_idle_table[][SMP_SM_NUM_COLS] = {
197 /* Event Action Next State */
198 /* L2C_CONN */ {SMP_SEND_APP_CBACK, SMP_SM_NO_ACTION, SMP_ST_WAIT_APP_RSP},
199 /* SEC_REQ */ {SMP_PROC_SEC_REQ, SMP_SEND_APP_CBACK, SMP_ST_WAIT_APP_RSP}
200 };
201
202 static const UINT8 smp_ma_wait_app_rsp_table[][SMP_SM_NUM_COLS] = {
203 /* Event Action Next State */
204 /* SEC_GRANT */ { SMP_PROC_SEC_GRANT, SMP_SEND_APP_CBACK, SMP_ST_WAIT_APP_RSP},
205 /* IO_RSP */ { SMP_SEND_PAIR_REQ, SMP_SM_NO_ACTION, SMP_ST_PAIR_REQ_RSP},
206 /* KEY_READY */ { SMP_GENERATE_CONFIRM, SMP_SM_NO_ACTION, SMP_ST_WAIT_CONFIRM},/* TK ready */
207 /* ENC_REQ */ { SMP_START_ENC, SMP_SM_NO_ACTION, SMP_ST_ENC_PENDING},/* start enc mode setup */
208 /* DISCARD_SEC_REQ */ { SMP_PROC_DISCARD, SMP_SM_NO_ACTION, SMP_ST_IDLE}
209 };
210
211 static const UINT8 smp_ma_pair_req_rsp_table [][SMP_SM_NUM_COLS] = {
212 /* Event Action Next State */
213 /* PAIR_RSP */ { SMP_PROC_PAIR_CMD, SMP_DECIDE_ASSO_MODEL, SMP_ST_PAIR_REQ_RSP},
214 /* TK_REQ */ { SMP_SEND_APP_CBACK, SMP_SM_NO_ACTION, SMP_ST_WAIT_APP_RSP},
215 /* KEY_READY */{ SMP_GENERATE_CONFIRM, SMP_SM_NO_ACTION, SMP_ST_WAIT_CONFIRM} /* TK ready */
216 };
217
218 static const UINT8 smp_ma_wait_confirm_table[][SMP_SM_NUM_COLS] = {
219 /* Event Action Next State */
220 /* KEY_READY*/ {SMP_SEND_CONFIRM, SMP_SM_NO_ACTION, SMP_ST_CONFIRM}/* CONFIRM ready */
221 };
222
223 static const UINT8 smp_ma_confirm_table [][SMP_SM_NUM_COLS] = {
224 /* Event Action Next State */
225 /* CONFIRM */ { SMP_PROC_CONFIRM, SMP_SEND_INIT, SMP_ST_RAND}
226 };
227
228 static const UINT8 smp_ma_init_table [][SMP_SM_NUM_COLS] = {
229 /* Event Action Next State */
230 /* INIT */ { SMP_PROC_INIT, SMP_GENERATE_COMPARE, SMP_ST_RAND},
231 /* KEY_READY*/ { SMP_PROC_COMPARE, SMP_SM_NO_ACTION, SMP_ST_RAND}, /* Compare ready */
232 /* ENC_REQ */ { SMP_GENERATE_STK, SMP_SM_NO_ACTION, SMP_ST_ENC_PENDING}
233 };
234 static const UINT8 smp_ma_enc_pending_table[][SMP_SM_NUM_COLS] = {
235 /* Event Action Next State */
236 /* KEY_READY */ { SMP_START_ENC, SMP_SM_NO_ACTION, SMP_ST_ENC_PENDING}, /* STK ready */
237 /* ENCRYPTED */ { SMP_CHECK_AUTH_REQ, SMP_SM_NO_ACTION, SMP_ST_ENC_PENDING},
238 /* BOND_REQ */ { SMP_KEY_DISTRIBUTE, SMP_SM_NO_ACTION, SMP_ST_BOND_PENDING}
239 };
240 static const UINT8 smp_ma_bond_pending_table[][SMP_SM_NUM_COLS] = {
241 /* Event Action Next State */
242 /* ENC_INFO */ { SMP_PROC_ENC_INFO, SMP_SM_NO_ACTION, SMP_ST_BOND_PENDING},
243 /* ID_INFO */ { SMP_PROC_ID_INFO, SMP_SM_NO_ACTION, SMP_ST_BOND_PENDING},
244 /* SIGN_INFO*/ { SMP_PROC_SRK_INFO, SMP_SM_NO_ACTION, SMP_ST_BOND_PENDING},
245 /* MASTER_ID*/ { SMP_PROC_MASTER_ID, SMP_SM_NO_ACTION, SMP_ST_BOND_PENDING},
246 /* ID_ADDR */ { SMP_PROC_ID_ADDR, SMP_SM_NO_ACTION, SMP_ST_BOND_PENDING},
247 /* KEY_READY */ {SMP_SEND_SECU_INFO, SMP_SM_NO_ACTION, SMP_ST_BOND_PENDING} /* LTK ready */
248 };
249
250 static const UINT8 smp_ma_rel_delay_table[][SMP_SM_NUM_COLS] = {
251 /* Event Action Next State */
252 /* RELEASE_DELAY*/ {SMP_PROC_REL_DELAY, SMP_SM_NO_ACTION, SMP_ST_RELEASE_DELAY},
253 /* RELEASE_DELAY_TOUT*/ {SMP_PROC_REL_DELAY_TOUT, SMP_SM_NO_ACTION, SMP_ST_IDLE}
254 };
255
256
257 /************ SMP Slave FSM State/Event Indirection Table **************/
258 static const UINT8 smp_sl_entry_map[][SMP_ST_MAX] =
259 {
260 /* state name: Idle Wait SecReq Pair Wait Confirm Init Enc Bond Rel
261 AppRsp Pend ReqRsp Cfm Pend Pend Delay */
262 /* PAIR_REQ */ { 2, 0, 1, 0, 0, 0, 0, 0, 0, 0 },
263 /* PAIR_RSP */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
264 /* CONFIRM */ { 0, 4, 0, 1, 1, 0, 0, 0, 0, 0 },
265 /* INIT */ { 0, 0, 0, 0, 0, 1, 2, 0, 0, 0 },
266 /* PAIR_FAIL*/ { 0, 0x81, 0x81, 0x81, 0x81,0x81, 0x81,0x81, 0, 0 },
267 /* ENC_INFO */ { 0, 0, 0, 0, 0, 0, 0, 0, 3, 0 },
268 /* MASTER_ID*/ { 0, 0, 0, 0, 0, 0, 0, 0, 5, 0 },
269 /* ID_INFO */ { 0, 0, 0, 0, 0, 0, 0, 0, 4, 0 },
270 /* ID_ADDR */ { 0, 0, 0, 0, 0, 0, 0, 0, 6, 0 },
271 /* SIGN_INFO*/ { 0, 0, 0, 0, 0, 0, 0, 0, 2, 0 },
272 /* SEC_REQ */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
273
274 /* KEY_READY*/ { 0, 3, 0, 3, 2, 2, 1, 2, 1, 0 },
275 /* ENC_CMPL */ { 0, 0, 2, 0, 0, 0, 0, 3, 0, 0 },
276 /* L2C_CONN */ { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
277 /* L2C_DISC */ { 0, 0x83, 0x83, 0x83, 0x83,0x83, 0x83,0x83, 0x83, 2 },
278 /* IO_RSP */ { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0 },
279 /* SEC_GRANT*/ { 0, 2, 0, 0, 0, 0, 0, 0, 0, 0 },
280
281 /* TK_REQ */ { 0, 0, 0, 2, 0, 0, 0, 0, 0, 0 },
282 /* AUTH_CMPL*/ { 0, 0x82, 0x82, 0x82, 0x82,0x82, 0x82,0x82, 0x82, 0 },
283 /* ENC_REQ */ { 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },
284 /* BOND_REQ */ { 0, 0, 0, 0, 0, 0, 0, 4, 0, 0 },
285 /* DISCARD_SEC_REQ */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
286 /* RELEASE_DELAY */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
287 /* RELEASE_DELAY_TOUT */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 2 },
288 };
289
290 static const UINT8 smp_sl_idle_table[][SMP_SM_NUM_COLS] = {
291 /* Event Action Next State */
292 /* L2C_CONN */ {SMP_SEND_APP_CBACK, SMP_SM_NO_ACTION, SMP_ST_WAIT_APP_RSP},
293 /* PAIR_REQ */ {SMP_PROC_PAIR_CMD, SMP_SEND_APP_CBACK, SMP_ST_WAIT_APP_RSP}
294 };
295
296 static const UINT8 smp_sl_wait_app_rsp_table [][SMP_SM_NUM_COLS] = {
297 /* Event Action Next State */
298 /* IO_RSP */ {SMP_PROC_IO_RSP, SMP_SM_NO_ACTION, SMP_ST_PAIR_REQ_RSP},
299 /* SEC_GRANT */ {SMP_PROC_SEC_GRANT, SMP_SEND_APP_CBACK, SMP_ST_WAIT_APP_RSP},
300 /* KEY_READY */ {SMP_PROC_SL_KEY, SMP_SM_NO_ACTION, SMP_ST_WAIT_APP_RSP},/* TK ready */
301 /* CONFIRM */ {SMP_PROC_CONFIRM, SMP_SM_NO_ACTION, SMP_ST_CONFIRM}
302 };
303
304 static const UINT8 smp_sl_sec_request_table[][SMP_SM_NUM_COLS] = {
305 /* Event Action Next State */
306 /* PAIR_REQ */{SMP_PROC_PAIR_CMD, SMP_SEND_PAIR_RSP, SMP_ST_PAIR_REQ_RSP},
307 /* ENCRYPTED*/{SMP_ENC_CMPL, SMP_SM_NO_ACTION, SMP_ST_PAIR_REQ_RSP},
308 };
309
310 static const UINT8 smp_sl_pair_req_rsp_table[][SMP_SM_NUM_COLS] = {
311 /* Event Action Next State */
312 /* CONFIRM */ {SMP_PROC_CONFIRM, SMP_SM_NO_ACTION, SMP_ST_CONFIRM},
313 /* TK_REQ */ {SMP_SEND_APP_CBACK, SMP_SM_NO_ACTION, SMP_ST_WAIT_APP_RSP},
314 /* KEY_READY */{SMP_PROC_SL_KEY, SMP_SM_NO_ACTION, SMP_ST_PAIR_REQ_RSP} /* TK/Confirm ready */
315
316 };
317
318 static const UINT8 smp_sl_wait_confirm_table[][SMP_SM_NUM_COLS] = {
319 /* Event Action Next State */
320 /* CONFIRM */ {SMP_PROC_CONFIRM, SMP_SEND_CONFIRM, SMP_ST_CONFIRM},
321 /* KEY_READY*/ {SMP_PROC_SL_KEY, SMP_SM_NO_ACTION, SMP_ST_WAIT_CONFIRM}
322 };
323 static const UINT8 smp_sl_confirm_table [][SMP_SM_NUM_COLS] = {
324 /* Event Action Next State */
325 /* INIT_EVT */{ SMP_PROC_INIT, SMP_GENERATE_COMPARE, SMP_ST_RAND},
326 /* KEY_READY*/ {SMP_PROC_SL_KEY, SMP_SM_NO_ACTION, SMP_ST_CONFIRM} /* TK/Confirm ready */
327 };
328 static const UINT8 smp_sl_init_table [][SMP_SM_NUM_COLS] = {
329 /* Event Action Next State */
330 /* KEY_READY */ {SMP_PROC_COMPARE, SMP_SM_NO_ACTION, SMP_ST_RAND}, /* compare match */
331 /* INIT_EVT */ {SMP_SEND_INIT, SMP_SM_NO_ACTION, SMP_ST_ENC_PENDING}
332 };
333 static const UINT8 smp_sl_enc_pending_table[][SMP_SM_NUM_COLS] = {
334 /* Event Action Next State */
335 /* ENC_REQ */ {SMP_GENERATE_STK, SMP_SM_NO_ACTION, SMP_ST_ENC_PENDING},
336 /* KEY_READY */ {SMP_SEND_LTK_REPLY, SMP_SM_NO_ACTION, SMP_ST_ENC_PENDING},/* STK ready */
337 /* ENCRYPTED */ {SMP_CHECK_AUTH_REQ, SMP_SM_NO_ACTION, SMP_ST_ENC_PENDING},
338 /* BOND_REQ */ {SMP_KEY_DISTRIBUTE, SMP_SM_NO_ACTION, SMP_ST_BOND_PENDING}
339 };
340 static const UINT8 smp_sl_bond_pending_table[][SMP_SM_NUM_COLS] = {
341 /* Event Action Next State */
342 /* KEY_READY */ {SMP_SEND_SECU_INFO, SMP_SM_NO_ACTION, SMP_ST_BOND_PENDING}, /* LTK ready */
343 /* SIGN_INFO */ {SMP_PROC_SRK_INFO, SMP_SM_NO_ACTION, SMP_ST_BOND_PENDING}, /* rev SRK */
344 /* ENC_INFO */ { SMP_PROC_ENC_INFO, SMP_SM_NO_ACTION, SMP_ST_BOND_PENDING},
345 /* ID_INFO */ { SMP_PROC_ID_INFO, SMP_SM_NO_ACTION, SMP_ST_BOND_PENDING},
346 /* MASTER_ID*/ { SMP_PROC_MASTER_ID, SMP_SM_NO_ACTION, SMP_ST_BOND_PENDING},
347 /* ID_ADDR */ { SMP_PROC_ID_ADDR, SMP_SM_NO_ACTION, SMP_ST_BOND_PENDING}
348
349 };
350
351 static const UINT8 smp_sl_rel_delay_table[][SMP_SM_NUM_COLS] = {
352 /* Event Action Next State */
353 /* RELEASE_DELAY*/ {SMP_PROC_REL_DELAY, SMP_SM_NO_ACTION, SMP_ST_RELEASE_DELAY},
354 /* RELEASE_DELAY_TOUT*/ {SMP_PROC_REL_DELAY_TOUT, SMP_SM_NO_ACTION, SMP_ST_IDLE}
355 };
356
357 static const tSMP_SM_TBL smp_state_table[][2] = {
358 {smp_ma_idle_table, smp_sl_idle_table}, /* SMP_ST_IDLE*/
359 {smp_ma_wait_app_rsp_table, smp_sl_wait_app_rsp_table}, /* SMP_ST_WAIT_APP_RSP */
360 {NULL, smp_sl_sec_request_table}, /* SMP_ST_SEC_REQ_PENDING */
361 {smp_ma_pair_req_rsp_table, smp_sl_pair_req_rsp_table}, /* SMP_ST_PAIR_REQ_RSP */
362 {smp_ma_wait_confirm_table, smp_sl_wait_confirm_table}, /* SMP_ST_WAIT_CONFIRM */
363 {smp_ma_confirm_table, smp_sl_confirm_table}, /* SMP_ST_CONFIRM */
364 {smp_ma_init_table, smp_sl_init_table}, /* SMP_ST_RAND */
365 {smp_ma_enc_pending_table, smp_sl_enc_pending_table}, /* SMP_ST_ENC_PENDING */
366 {smp_ma_bond_pending_table, smp_sl_bond_pending_table}, /* SMP_ST_BOND_PENDING */
367 {smp_ma_rel_delay_table, smp_sl_rel_delay_table} /* SMP_ST_RELEASE_DELAY */
368 };
369
370 typedef const UINT8 (*tSMP_ENTRY_TBL)[SMP_ST_MAX];
371 static const tSMP_ENTRY_TBL smp_entry_table[] ={
372 smp_ma_entry_map,
373 smp_sl_entry_map
374 };
375
376 #if SMP_DYNAMIC_MEMORY == FALSE
377 tSMP_CB smp_cb;
378 #endif
379 #define SMP_ALL_TBL_MASK 0x80
380
381
382 /*******************************************************************************
383 ** Function smp_set_state
384 ** Returns None
385 *******************************************************************************/
smp_set_state(tSMP_STATE state)386 void smp_set_state(tSMP_STATE state)
387 {
388 if (state < SMP_ST_MAX)
389 {
390 SMP_TRACE_DEBUG4( "State change: %s(%d) ==> %s(%d)",
391 smp_get_state_name(smp_cb.state), smp_cb.state,
392 smp_get_state_name(state), state );
393 smp_cb.state = state;
394 }
395 else
396 {
397 SMP_TRACE_DEBUG1("smp_set_state invalid state =%d", state );
398 }
399 }
400
401 /*******************************************************************************
402 ** Function smp_get_state
403 ** Returns The smp state
404 *******************************************************************************/
smp_get_state(void)405 tSMP_STATE smp_get_state(void)
406 {
407 return smp_cb.state;
408 }
409
410
411 /*******************************************************************************
412 **
413 ** Function smp_sm_event
414 **
415 ** Description Handle events to the state machine. It looks up the entry
416 ** in the smp_entry_table array.
417 ** If it is a valid entry, it gets the state table.Set the next state,
418 ** if not NULL state.Execute the action function according to the
419 ** state table. If the state returned by action function is not NULL
420 ** state, adjust the new state to the returned state.If (api_evt != MAX),
421 ** call callback function.
422 **
423 ** Returns void.
424 **
425 *******************************************************************************/
smp_sm_event(tSMP_CB * p_cb,tSMP_EVENT event,void * p_data)426 void smp_sm_event(tSMP_CB *p_cb, tSMP_EVENT event, void *p_data)
427 {
428 UINT8 curr_state = p_cb->state;
429 tSMP_SM_TBL state_table;
430 UINT8 action, entry, i;
431 tSMP_ENTRY_TBL entry_table = smp_entry_table[p_cb->role];
432
433 SMP_TRACE_EVENT0("main smp_sm_event");
434 if (curr_state >= SMP_ST_MAX)
435 {
436 SMP_TRACE_DEBUG1( "Invalid state: %d", curr_state) ;
437 return;
438 }
439
440 SMP_TRACE_DEBUG5( "SMP Role: %s State: [%s (%d)], Event: [%s (%d)]",\
441 (p_cb->role == 0x01) ?"Slave" : "Master", smp_get_state_name( p_cb->state),
442 p_cb->state, smp_get_event_name(event), event) ;
443
444 /* look up the state table for the current state */
445 /* lookup entry /w event & curr_state */
446 /* If entry is ignore, return.
447 * Otherwise, get state table (according to curr_state or all_state) */
448 if ( (entry = entry_table[event - 1][curr_state]) != SMP_SM_IGNORE )
449 {
450 if (entry & SMP_ALL_TBL_MASK)
451 {
452 entry &= ~SMP_ALL_TBL_MASK;
453 state_table = smp_all_table;
454 }
455 else
456 state_table = smp_state_table[curr_state][p_cb->role];
457 }
458 else
459 {
460 SMP_TRACE_DEBUG4( "Ignore event [%s (%d)] in state [%s (%d)]",
461 smp_get_event_name(event), event, smp_get_state_name(curr_state), curr_state);
462 return;
463 }
464
465 /* Get possible next state from state table. */
466
467 smp_set_state(state_table[entry-1][SMP_SME_NEXT_STATE]);
468
469 /* If action is not ignore, clear param, exec action and get next state.
470 * The action function may set the Param for cback.
471 * Depending on param, call cback or free buffer. */
472 /* execute action */
473 /* execute action functions */
474 for (i = 0; i < SMP_NUM_ACTIONS; i++)
475 {
476 if ((action = state_table[entry-1][i]) != SMP_SM_NO_ACTION)
477 {
478 (*smp_sm_action[action])(p_cb, (tSMP_INT_DATA *)p_data);
479 }
480 else
481 {
482 break;
483 }
484 }
485 SMP_TRACE_DEBUG1( "result state = %s", smp_get_state_name( p_cb->state ) ) ;
486 }
487
488
489 /*******************************************************************************
490 ** Function smp_get_state_name
491 ** Returns The smp state name.
492 *******************************************************************************/
smp_get_state_name(tSMP_STATE state)493 const char * smp_get_state_name(tSMP_STATE state)
494 {
495 const char * p_str = smp_state_name[SMP_ST_MAX];
496
497 if (state < SMP_ST_MAX)
498 {
499 p_str = smp_state_name[state];
500 }
501 return p_str;
502 }
503 /*******************************************************************************
504 ** Function smp_get_event_name
505 ** Returns The smp event name.
506 *******************************************************************************/
smp_get_event_name(tSMP_EVENT event)507 const char * smp_get_event_name(tSMP_EVENT event)
508 {
509 const char * p_str = smp_event_name[SMP_MAX_EVT - 1];
510
511 if (event < SMP_MAX_EVT)
512 {
513 p_str = smp_event_name[event- 1];
514 }
515 return p_str;
516 }
517 #endif
518
519