1 /******************************************************************************
2 *
3 * Copyright (C) 2010-2014 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 /******************************************************************************
21 *
22 * This is the main implementation file for the NFA device manager.
23 *
24 ******************************************************************************/
25
26 #include <string.h>
27 #include "nfa_api.h"
28 #include "nfa_sys.h"
29 #include "nfa_dm_int.h"
30 #include "nfa_sys_int.h"
31
32
33 /*****************************************************************************
34 ** Constants and types
35 *****************************************************************************/
36 static const tNFA_SYS_REG nfa_dm_sys_reg =
37 {
38 nfa_dm_sys_enable,
39 nfa_dm_evt_hdlr,
40 nfa_dm_sys_disable,
41 nfa_dm_proc_nfcc_power_mode
42 };
43
44
45 tNFA_DM_CB nfa_dm_cb = {FALSE};
46
47
48 #define NFA_DM_NUM_ACTIONS (NFA_DM_MAX_EVT & 0x00ff)
49
50 /* type for action functions */
51 typedef BOOLEAN (*tNFA_DM_ACTION) (tNFA_DM_MSG *p_data);
52
53 /* action function list */
54 const tNFA_DM_ACTION nfa_dm_action[] =
55 {
56 /* device manager local device API events */
57 nfa_dm_enable, /* NFA_DM_API_ENABLE_EVT */
58 nfa_dm_disable, /* NFA_DM_API_DISABLE_EVT */
59 nfa_dm_set_config, /* NFA_DM_API_SET_CONFIG_EVT */
60 nfa_dm_get_config, /* NFA_DM_API_GET_CONFIG_EVT */
61 nfa_dm_act_request_excl_rf_ctrl, /* NFA_DM_API_REQUEST_EXCL_RF_CTRL_EVT */
62 nfa_dm_act_release_excl_rf_ctrl, /* NFA_DM_API_RELEASE_EXCL_RF_CTRL_EVT */
63 nfa_dm_act_enable_polling, /* NFA_DM_API_ENABLE_POLLING_EVT */
64 nfa_dm_act_disable_polling, /* NFA_DM_API_DISABLE_POLLING_EVT */
65 nfa_dm_act_enable_listening, /* NFA_DM_API_ENABLE_LISTENING_EVT */
66 nfa_dm_act_disable_listening, /* NFA_DM_API_DISABLE_LISTENING_EVT */
67 nfa_dm_act_pause_p2p, /* NFA_DM_API_PAUSE_P2P_EVT */
68 nfa_dm_act_resume_p2p, /* NFA_DM_API_RESUME_P2P_EVT */
69 nfa_dm_act_send_raw_frame, /* NFA_DM_API_RAW_FRAME_EVT */
70 nfa_dm_set_p2p_listen_tech, /* NFA_DM_API_SET_P2P_LISTEN_TECH_EVT */
71 nfa_dm_act_start_rf_discovery, /* NFA_DM_API_START_RF_DISCOVERY_EVT */
72 nfa_dm_act_stop_rf_discovery, /* NFA_DM_API_STOP_RF_DISCOVERY_EVT */
73 nfa_dm_act_set_rf_disc_duration, /* NFA_DM_API_SET_RF_DISC_DURATION_EVT */
74 nfa_dm_act_select, /* NFA_DM_API_SELECT_EVT */
75 nfa_dm_act_update_rf_params, /* NFA_DM_API_UPDATE_RF_PARAMS_EVT */
76 nfa_dm_act_deactivate, /* NFA_DM_API_DEACTIVATE_EVT */
77 nfa_dm_act_power_off_sleep, /* NFA_DM_API_POWER_OFF_SLEEP_EVT */
78 nfa_dm_ndef_reg_hdlr, /* NFA_DM_API_REG_NDEF_HDLR_EVT */
79 nfa_dm_ndef_dereg_hdlr, /* NFA_DM_API_DEREG_NDEF_HDLR_EVT */
80 nfa_dm_act_reg_vsc, /* NFA_DM_API_REG_VSC_EVT */
81 nfa_dm_act_send_vsc, /* NFA_DM_API_SEND_VSC_EVT */
82 nfa_dm_act_disable_timeout /* NFA_DM_TIMEOUT_DISABLE_EVT */
83 };
84
85 /*****************************************************************************
86 ** Local function prototypes
87 *****************************************************************************/
88 #if (BT_TRACE_VERBOSE == TRUE)
89 static char *nfa_dm_evt_2_str (UINT16 event);
90 #endif
91 /*******************************************************************************
92 **
93 ** Function nfa_dm_init
94 **
95 ** Description Initialises the NFC device manager
96 **
97 ** Returns void
98 **
99 *******************************************************************************/
nfa_dm_init(void)100 void nfa_dm_init (void)
101 {
102 NFA_TRACE_DEBUG0 ("nfa_dm_init ()");
103 memset (&nfa_dm_cb, 0, sizeof (tNFA_DM_CB));
104 nfa_dm_cb.poll_disc_handle = NFA_HANDLE_INVALID;
105 nfa_dm_cb.disc_cb.disc_duration = NFA_DM_DISC_DURATION_POLL;
106 nfa_dm_cb.nfcc_pwr_mode = NFA_DM_PWR_MODE_FULL;
107
108 /* register message handler on NFA SYS */
109 nfa_sys_register (NFA_ID_DM, &nfa_dm_sys_reg);
110 }
111
112 /*******************************************************************************
113 **
114 ** Function nfa_dm_evt_hdlr
115 **
116 ** Description Event handling function for DM
117 **
118 **
119 ** Returns void
120 **
121 *******************************************************************************/
nfa_dm_evt_hdlr(BT_HDR * p_msg)122 BOOLEAN nfa_dm_evt_hdlr (BT_HDR *p_msg)
123 {
124 BOOLEAN freebuf = TRUE;
125 UINT16 event = p_msg->event & 0x00ff;
126
127 #if (BT_TRACE_VERBOSE == TRUE)
128 NFA_TRACE_EVENT2 ("nfa_dm_evt_hdlr event: %s (0x%02x)", nfa_dm_evt_2_str (event), event);
129 #else
130 NFA_TRACE_EVENT1 ("nfa_dm_evt_hdlr event: 0x%x", event);
131 #endif
132
133 /* execute action functions */
134 if (event < NFA_DM_NUM_ACTIONS)
135 {
136 freebuf = (*nfa_dm_action[event]) ((tNFA_DM_MSG*) p_msg);
137 }
138 return freebuf;
139 }
140
141 /*******************************************************************************
142 **
143 ** Function nfa_dm_sys_disable
144 **
145 ** Description This function is called after all subsystems have been disabled.
146 **
147 ** Returns void
148 **
149 *******************************************************************************/
nfa_dm_sys_disable(void)150 void nfa_dm_sys_disable (void)
151 {
152 /* Disable the DM sub-system */
153 /* If discovery state is not IDLE or DEACTIVATED and graceful disable, */
154 /* then we need to deactivate link or stop discovery */
155
156 if (nfa_sys_is_graceful_disable ())
157 {
158 if ( (nfa_dm_cb.disc_cb.disc_state == NFA_DM_RFST_IDLE)
159 &&((nfa_dm_cb.disc_cb.disc_flags & NFA_DM_DISC_FLAGS_DISABLING) == 0) )
160 {
161 /* discovery is not started */
162 nfa_dm_disable_complete ();
163 }
164 else
165 {
166 /* probably waiting to be disabled */
167 NFA_TRACE_WARNING2 ("DM disc_state state = %d disc_flags:0x%x", nfa_dm_cb.disc_cb.disc_state, nfa_dm_cb.disc_cb.disc_flags);
168 }
169
170 }
171 else
172 {
173 nfa_dm_disable_complete ();
174 }
175 }
176
177 /*******************************************************************************
178 **
179 ** Function nfa_dm_is_protocol_supported
180 **
181 ** Description Check if protocol is supported by RW module
182 **
183 ** Returns TRUE if protocol is supported by NFA
184 **
185 *******************************************************************************/
nfa_dm_is_protocol_supported(tNFC_PROTOCOL protocol,UINT8 sel_res)186 BOOLEAN nfa_dm_is_protocol_supported (tNFC_PROTOCOL protocol, UINT8 sel_res)
187 {
188 return ( (protocol == NFC_PROTOCOL_T1T)
189 ||((protocol == NFC_PROTOCOL_T2T) && (sel_res == NFC_SEL_RES_NFC_FORUM_T2T))
190 ||(protocol == NFC_PROTOCOL_T3T)
191 ||(protocol == NFC_PROTOCOL_ISO_DEP)
192 ||(protocol == NFC_PROTOCOL_NFC_DEP)
193 ||(protocol == NFC_PROTOCOL_15693) );
194 }
195 /*******************************************************************************
196 **
197 ** Function nfa_dm_is_active
198 **
199 ** Description check if all modules of NFA is done with enable process and
200 ** NFA is not restoring NFCC.
201 **
202 ** Returns TRUE, if NFA_DM_ENABLE_EVT is reported and it is not restoring NFCC
203 **
204 *******************************************************************************/
nfa_dm_is_active(void)205 BOOLEAN nfa_dm_is_active (void)
206 {
207 NFA_TRACE_DEBUG1 ("nfa_dm_is_active () flags:0x%x", nfa_dm_cb.flags);
208 if ( (nfa_dm_cb.flags & NFA_DM_FLAGS_DM_IS_ACTIVE)
209 &&((nfa_dm_cb.flags & (NFA_DM_FLAGS_ENABLE_EVT_PEND | NFA_DM_FLAGS_NFCC_IS_RESTORING | NFA_DM_FLAGS_POWER_OFF_SLEEP)) == 0) )
210 {
211 return TRUE;
212 }
213 else
214 return FALSE;
215 }
216 /*******************************************************************************
217 **
218 ** Function nfa_dm_check_set_config
219 **
220 ** Description Update config parameters only if it's different from NFCC
221 **
222 **
223 ** Returns tNFA_STATUS
224 **
225 *******************************************************************************/
nfa_dm_check_set_config(UINT8 tlv_list_len,UINT8 * p_tlv_list,BOOLEAN app_init)226 tNFA_STATUS nfa_dm_check_set_config (UINT8 tlv_list_len, UINT8 *p_tlv_list, BOOLEAN app_init)
227 {
228 UINT8 type, len, *p_value, *p_stored, max_len;
229 UINT8 xx = 0, updated_len = 0, *p_cur_len;
230 BOOLEAN update;
231 tNFC_STATUS nfc_status;
232 UINT32 cur_bit;
233
234 NFA_TRACE_DEBUG0 ("nfa_dm_check_set_config ()");
235
236 /* We only allow 32 pending SET_CONFIGs */
237 if (nfa_dm_cb.setcfg_pending_num >= NFA_DM_SETCONFIG_PENDING_MAX)
238 {
239 NFA_TRACE_ERROR0 ("nfa_dm_check_set_config () error: pending number of SET_CONFIG exceeded");
240 return NFA_STATUS_FAILED;
241 }
242
243 while (tlv_list_len - xx >= 2) /* at least type and len */
244 {
245 update = FALSE;
246 type = *(p_tlv_list + xx);
247 len = *(p_tlv_list + xx + 1);
248 p_value = p_tlv_list + xx + 2;
249 p_cur_len = NULL;
250
251 switch (type)
252 {
253 /*
254 ** Poll F Configuration
255 */
256 case NFC_PMID_PF_RC:
257 p_stored = nfa_dm_cb.params.pf_rc;
258 max_len = NCI_PARAM_LEN_PF_RC;
259 break;
260 case NFC_PMID_TOTAL_DURATION:
261 p_stored = nfa_dm_cb.params.total_duration;
262 max_len = NCI_PARAM_LEN_TOTAL_DURATION;
263 break;
264
265 /*
266 ** Listen A Configuration
267 */
268 case NFC_PMID_LA_BIT_FRAME_SDD:
269 p_stored = nfa_dm_cb.params.la_bit_frame_sdd;
270 max_len = NCI_PARAM_LEN_LA_BIT_FRAME_SDD;
271 p_cur_len = &nfa_dm_cb.params.la_bit_frame_sdd_len;
272 break;
273 case NFC_PMID_LA_PLATFORM_CONFIG:
274 p_stored = nfa_dm_cb.params.la_platform_config;
275 max_len = NCI_PARAM_LEN_LA_PLATFORM_CONFIG;
276 p_cur_len = &nfa_dm_cb.params.la_platform_config_len;
277 break;
278 case NFC_PMID_LA_SEL_INFO:
279 p_stored = nfa_dm_cb.params.la_sel_info;
280 max_len = NCI_PARAM_LEN_LA_SEL_INFO;
281 p_cur_len = &nfa_dm_cb.params.la_sel_info_len;
282 break;
283 case NFC_PMID_LA_NFCID1:
284 p_stored = nfa_dm_cb.params.la_nfcid1;
285 max_len = NCI_NFCID1_MAX_LEN;
286 p_cur_len = &nfa_dm_cb.params.la_nfcid1_len;
287 break;
288 case NFC_PMID_LA_HIST_BY:
289 p_stored = nfa_dm_cb.params.la_hist_by;
290 max_len = NCI_MAX_HIS_BYTES_LEN;
291 p_cur_len = &nfa_dm_cb.params.la_hist_by_len;
292 break;
293
294 /*
295 ** Listen B Configuration
296 */
297 case NFC_PMID_LB_SENSB_INFO:
298 p_stored = nfa_dm_cb.params.lb_sensb_info;
299 max_len = NCI_PARAM_LEN_LB_SENSB_INFO;
300 p_cur_len = &nfa_dm_cb.params.lb_sensb_info_len;
301 break;
302 case NFC_PMID_LB_NFCID0:
303 p_stored = nfa_dm_cb.params.lb_nfcid0;
304 max_len = NCI_PARAM_LEN_LB_NFCID0;
305 p_cur_len = &nfa_dm_cb.params.lb_nfcid0_len;
306 break;
307 case NFC_PMID_LB_APPDATA:
308 p_stored = nfa_dm_cb.params.lb_appdata;
309 max_len = NCI_PARAM_LEN_LB_APPDATA;
310 p_cur_len = &nfa_dm_cb.params.lb_appdata_len;
311 break;
312 case NFC_PMID_LB_ADC_FO:
313 p_stored = nfa_dm_cb.params.lb_adc_fo;
314 max_len = NCI_PARAM_LEN_LB_ADC_FO;
315 p_cur_len = &nfa_dm_cb.params.lb_adc_fo_len;
316 break;
317 case NFC_PMID_LB_H_INFO:
318 p_stored = nfa_dm_cb.params.lb_h_info;
319 max_len = NCI_MAX_ATTRIB_LEN;
320 p_cur_len = &nfa_dm_cb.params.lb_h_info_len;
321 break;
322
323 /*
324 ** Listen F Configuration
325 */
326 case NFC_PMID_LF_PROTOCOL:
327 p_stored = nfa_dm_cb.params.lf_protocol;
328 max_len = NCI_PARAM_LEN_LF_PROTOCOL;
329 p_cur_len = &nfa_dm_cb.params.lf_protocol_len;
330 break;
331 case NFC_PMID_LF_T3T_FLAGS2:
332 p_stored = nfa_dm_cb.params.lf_t3t_flags2;
333 max_len = NCI_PARAM_LEN_LF_T3T_FLAGS2;
334 p_cur_len = &nfa_dm_cb.params.lf_t3t_flags2_len;
335 break;
336 case NFC_PMID_LF_T3T_PMM:
337 p_stored = nfa_dm_cb.params.lf_t3t_pmm;
338 max_len = NCI_PARAM_LEN_LF_T3T_PMM;
339 break;
340
341 /*
342 ** ISO-DEP and NFC-DEP Configuration
343 */
344 case NFC_PMID_FWI:
345 p_stored = nfa_dm_cb.params.fwi;
346 max_len = NCI_PARAM_LEN_FWI;
347 break;
348 case NFC_PMID_WT:
349 p_stored = nfa_dm_cb.params.wt;
350 max_len = NCI_PARAM_LEN_WT;
351 break;
352 case NFC_PMID_ATR_REQ_GEN_BYTES:
353 p_stored = nfa_dm_cb.params.atr_req_gen_bytes;
354 max_len = NCI_MAX_GEN_BYTES_LEN;
355 p_cur_len = &nfa_dm_cb.params.atr_req_gen_bytes_len;
356 break;
357 case NFC_PMID_ATR_RES_GEN_BYTES:
358 p_stored = nfa_dm_cb.params.atr_res_gen_bytes;
359 max_len = NCI_MAX_GEN_BYTES_LEN;
360 p_cur_len = &nfa_dm_cb.params.atr_res_gen_bytes_len;
361 break;
362 default:
363 /*
364 ** Listen F Configuration
365 */
366 if ((type >= NFC_PMID_LF_T3T_ID1) && (type < NFC_PMID_LF_T3T_ID1 + NFA_CE_LISTEN_INFO_MAX))
367 {
368 p_stored = nfa_dm_cb.params.lf_t3t_id[type - NFC_PMID_LF_T3T_ID1];
369 max_len = NCI_PARAM_LEN_LF_T3T_ID;
370 }
371 else
372 {
373 /* we don't stored this config items */
374 update = TRUE;
375 p_stored = NULL;
376 }
377 break;
378 }
379
380 if ((p_stored)&&(len <= max_len))
381 {
382 if (p_cur_len)
383 {
384 if (*p_cur_len != len)
385 {
386 *p_cur_len = len;
387 update = TRUE;
388 }
389 else if (memcmp (p_value, p_stored, len))
390 {
391 update = TRUE;
392 }
393 }
394 else if (len == max_len) /* fixed length */
395 {
396 if (memcmp (p_value, p_stored, len))
397 {
398 update = TRUE;
399 }
400 }
401 }
402
403 if (update)
404 {
405 /* we don't store this type */
406 if (p_stored)
407 {
408 memcpy (p_stored, p_value, len);
409 }
410
411 /* If need to change TLV in the original list. (Do not modify list if app_init) */
412 if ((updated_len != xx) && (!app_init))
413 {
414 memcpy (p_tlv_list + updated_len, p_tlv_list + xx, (len + 2));
415 }
416 updated_len += (len + 2);
417 }
418 xx += len + 2; /* move to next TLV */
419 }
420
421 /* If any TVLs to update, or if the SetConfig was initiated by the application, then send the SET_CONFIG command */
422 if (updated_len || app_init)
423 {
424 if ((nfc_status = NFC_SetConfig (updated_len, p_tlv_list)) == NFC_STATUS_OK)
425 {
426 /* Keep track of whether we will need to notify NFA_DM_SET_CONFIG_EVT on NFC_SET_CONFIG_REVT */
427
428 /* Get the next available bit offset for this setconfig (based on how many SetConfigs are outstanding) */
429 cur_bit = (UINT32) (1 << nfa_dm_cb.setcfg_pending_num);
430
431 /* If setconfig is due to NFA_SetConfig: then set the bit (NFA_DM_SET_CONFIG_EVT needed on NFC_SET_CONFIG_REVT) */
432 if (app_init)
433 {
434 nfa_dm_cb.setcfg_pending_mask |= cur_bit;
435 }
436 /* Otherwise setconfig is internal: clear the bit (NFA_DM_SET_CONFIG_EVT not needed on NFC_SET_CONFIG_REVT) */
437 else
438 {
439 nfa_dm_cb.setcfg_pending_mask &= ~cur_bit;
440 }
441
442 /* Increment setcfg_pending counter */
443 nfa_dm_cb.setcfg_pending_num++;
444 }
445 return (nfc_status);
446
447 }
448 else
449 {
450 return NFA_STATUS_OK;
451 }
452 }
453
454 #if (BT_TRACE_VERBOSE == TRUE)
455 /*******************************************************************************
456 **
457 ** Function nfa_dm_nfc_revt_2_str
458 **
459 ** Description convert nfc revt to string
460 **
461 *******************************************************************************/
nfa_dm_evt_2_str(UINT16 event)462 static char *nfa_dm_evt_2_str (UINT16 event)
463 {
464 switch (NFA_SYS_EVT_START (NFA_ID_DM) | event)
465 {
466 case NFA_DM_API_ENABLE_EVT:
467 return "NFA_DM_API_ENABLE_EVT";
468
469 case NFA_DM_API_DISABLE_EVT:
470 return "NFA_DM_API_DISABLE_EVT";
471
472 case NFA_DM_API_SET_CONFIG_EVT:
473 return "NFA_DM_API_SET_CONFIG_EVT";
474
475 case NFA_DM_API_GET_CONFIG_EVT:
476 return "NFA_DM_API_GET_CONFIG_EVT";
477
478 case NFA_DM_API_REQUEST_EXCL_RF_CTRL_EVT:
479 return "NFA_DM_API_REQUEST_EXCL_RF_CTRL_EVT";
480
481 case NFA_DM_API_RELEASE_EXCL_RF_CTRL_EVT:
482 return "NFA_DM_API_RELEASE_EXCL_RF_CTRL_EVT";
483
484 case NFA_DM_API_ENABLE_POLLING_EVT:
485 return "NFA_DM_API_ENABLE_POLLING_EVT";
486
487 case NFA_DM_API_DISABLE_POLLING_EVT:
488 return "NFA_DM_API_DISABLE_POLLING_EVT";
489
490 case NFA_DM_API_ENABLE_LISTENING_EVT:
491 return "NFA_DM_API_ENABLE_LISTENING_EVT";
492
493 case NFA_DM_API_DISABLE_LISTENING_EVT:
494 return "NFA_DM_API_DISABLE_LISTENING_EVT";
495
496 case NFA_DM_API_PAUSE_P2P_EVT:
497 return "NFA_DM_API_PAUSE_P2P_EVT";
498
499 case NFA_DM_API_RESUME_P2P_EVT:
500 return "NFA_DM_API_RESUME_P2P_EVT";
501
502 case NFA_DM_API_RAW_FRAME_EVT:
503 return "NFA_DM_API_RAW_FRAME_EVT";
504
505 case NFA_DM_API_SET_P2P_LISTEN_TECH_EVT:
506 return "NFA_DM_API_SET_P2P_LISTEN_TECH_EVT";
507
508 case NFA_DM_API_START_RF_DISCOVERY_EVT:
509 return "NFA_DM_API_START_RF_DISCOVERY_EVT";
510
511 case NFA_DM_API_STOP_RF_DISCOVERY_EVT:
512 return "NFA_DM_API_STOP_RF_DISCOVERY_EVT";
513
514 case NFA_DM_API_SET_RF_DISC_DURATION_EVT:
515 return "NFA_DM_API_SET_RF_DISC_DURATION_EVT";
516
517 case NFA_DM_API_SELECT_EVT:
518 return "NFA_DM_API_SELECT_EVT";
519
520 case NFA_DM_API_UPDATE_RF_PARAMS_EVT:
521 return "NFA_DM_API_UPDATE_RF_PARAMS_EVT";
522
523 case NFA_DM_API_DEACTIVATE_EVT:
524 return "NFA_DM_API_DEACTIVATE_EVT";
525
526 case NFA_DM_API_POWER_OFF_SLEEP_EVT:
527 return "NFA_DM_API_POWER_OFF_SLEEP_EVT";
528
529 case NFA_DM_API_REG_NDEF_HDLR_EVT:
530 return "NFA_DM_API_REG_NDEF_HDLR_EVT";
531
532 case NFA_DM_API_DEREG_NDEF_HDLR_EVT:
533 return "NFA_DM_API_DEREG_NDEF_HDLR_EVT";
534
535 case NFA_DM_TIMEOUT_DISABLE_EVT:
536 return "NFA_DM_TIMEOUT_DISABLE_EVT";
537
538 }
539
540 return "Unknown or Vendor Specific";
541 }
542 #endif /* BT_TRACE_VERBOSE */
543