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 *******************************************************************************/ 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 *******************************************************************************/ 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 *******************************************************************************/ 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 *******************************************************************************/ 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 *******************************************************************************/ 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 *******************************************************************************/ 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 case NFC_PMID_TOTAL_DURATION: 254 p_stored = nfa_dm_cb.params.total_duration; 255 max_len = NCI_PARAM_LEN_TOTAL_DURATION; 256 break; 257 258 /* 259 ** Listen A Configuration 260 */ 261 case NFC_PMID_LA_BIT_FRAME_SDD: 262 p_stored = nfa_dm_cb.params.la_bit_frame_sdd; 263 max_len = NCI_PARAM_LEN_LA_BIT_FRAME_SDD; 264 p_cur_len = &nfa_dm_cb.params.la_bit_frame_sdd_len; 265 break; 266 case NFC_PMID_LA_PLATFORM_CONFIG: 267 p_stored = nfa_dm_cb.params.la_platform_config; 268 max_len = NCI_PARAM_LEN_LA_PLATFORM_CONFIG; 269 p_cur_len = &nfa_dm_cb.params.la_platform_config_len; 270 break; 271 case NFC_PMID_LA_SEL_INFO: 272 p_stored = nfa_dm_cb.params.la_sel_info; 273 max_len = NCI_PARAM_LEN_LA_SEL_INFO; 274 p_cur_len = &nfa_dm_cb.params.la_sel_info_len; 275 break; 276 case NFC_PMID_LA_NFCID1: 277 p_stored = nfa_dm_cb.params.la_nfcid1; 278 max_len = NCI_NFCID1_MAX_LEN; 279 p_cur_len = &nfa_dm_cb.params.la_nfcid1_len; 280 break; 281 case NFC_PMID_LA_HIST_BY: 282 p_stored = nfa_dm_cb.params.la_hist_by; 283 max_len = NCI_MAX_HIS_BYTES_LEN; 284 p_cur_len = &nfa_dm_cb.params.la_hist_by_len; 285 break; 286 287 /* 288 ** Listen B Configuration 289 */ 290 case NFC_PMID_LB_SENSB_INFO: 291 p_stored = nfa_dm_cb.params.lb_sensb_info; 292 max_len = NCI_PARAM_LEN_LB_SENSB_INFO; 293 p_cur_len = &nfa_dm_cb.params.lb_sensb_info_len; 294 break; 295 case NFC_PMID_LB_NFCID0: 296 p_stored = nfa_dm_cb.params.lb_nfcid0; 297 max_len = NCI_PARAM_LEN_LB_NFCID0; 298 p_cur_len = &nfa_dm_cb.params.lb_nfcid0_len; 299 break; 300 case NFC_PMID_LB_APPDATA: 301 p_stored = nfa_dm_cb.params.lb_appdata; 302 max_len = NCI_PARAM_LEN_LB_APPDATA; 303 p_cur_len = &nfa_dm_cb.params.lb_appdata_len; 304 break; 305 case NFC_PMID_LB_ADC_FO: 306 p_stored = nfa_dm_cb.params.lb_adc_fo; 307 max_len = NCI_PARAM_LEN_LB_ADC_FO; 308 p_cur_len = &nfa_dm_cb.params.lb_adc_fo_len; 309 break; 310 case NFC_PMID_LB_H_INFO: 311 p_stored = nfa_dm_cb.params.lb_h_info; 312 max_len = NCI_MAX_ATTRIB_LEN; 313 p_cur_len = &nfa_dm_cb.params.lb_h_info_len; 314 break; 315 316 /* 317 ** Listen F Configuration 318 */ 319 case NFC_PMID_LF_PROTOCOL: 320 p_stored = nfa_dm_cb.params.lf_protocol; 321 max_len = NCI_PARAM_LEN_LF_PROTOCOL; 322 p_cur_len = &nfa_dm_cb.params.lf_protocol_len; 323 break; 324 case NFC_PMID_LF_T3T_FLAGS2: 325 p_stored = nfa_dm_cb.params.lf_t3t_flags2; 326 max_len = NCI_PARAM_LEN_LF_T3T_FLAGS2; 327 p_cur_len = &nfa_dm_cb.params.lf_t3t_flags2_len; 328 break; 329 case NFC_PMID_LF_T3T_PMM: 330 p_stored = nfa_dm_cb.params.lf_t3t_pmm; 331 max_len = NCI_PARAM_LEN_LF_T3T_PMM; 332 break; 333 334 /* 335 ** ISO-DEP and NFC-DEP Configuration 336 */ 337 case NFC_PMID_FWI: 338 p_stored = nfa_dm_cb.params.fwi; 339 max_len = NCI_PARAM_LEN_FWI; 340 break; 341 case NFC_PMID_WT: 342 p_stored = nfa_dm_cb.params.wt; 343 max_len = NCI_PARAM_LEN_WT; 344 break; 345 case NFC_PMID_ATR_REQ_GEN_BYTES: 346 p_stored = nfa_dm_cb.params.atr_req_gen_bytes; 347 max_len = NCI_MAX_GEN_BYTES_LEN; 348 p_cur_len = &nfa_dm_cb.params.atr_req_gen_bytes_len; 349 break; 350 case NFC_PMID_ATR_RES_GEN_BYTES: 351 p_stored = nfa_dm_cb.params.atr_res_gen_bytes; 352 max_len = NCI_MAX_GEN_BYTES_LEN; 353 p_cur_len = &nfa_dm_cb.params.atr_res_gen_bytes_len; 354 break; 355 default: 356 /* 357 ** Listen F Configuration 358 */ 359 if ((type >= NFC_PMID_LF_T3T_ID1) && (type < NFC_PMID_LF_T3T_ID1 + NFA_CE_LISTEN_INFO_MAX)) 360 { 361 p_stored = nfa_dm_cb.params.lf_t3t_id[type - NFC_PMID_LF_T3T_ID1]; 362 max_len = NCI_PARAM_LEN_LF_T3T_ID; 363 } 364 else 365 { 366 /* we don't stored this config items */ 367 update = TRUE; 368 p_stored = NULL; 369 } 370 break; 371 } 372 373 if ((p_stored)&&(len <= max_len)) 374 { 375 if (p_cur_len) 376 { 377 if (*p_cur_len != len) 378 { 379 *p_cur_len = len; 380 update = TRUE; 381 } 382 else if (memcmp (p_value, p_stored, len)) 383 { 384 update = TRUE; 385 } 386 } 387 else if (len == max_len) /* fixed length */ 388 { 389 if (memcmp (p_value, p_stored, len)) 390 { 391 update = TRUE; 392 } 393 } 394 } 395 396 if (update) 397 { 398 /* we don't store this type */ 399 if (p_stored) 400 { 401 memcpy (p_stored, p_value, len); 402 } 403 404 /* If need to change TLV in the original list. (Do not modify list if app_init) */ 405 if ((updated_len != xx) && (!app_init)) 406 { 407 memcpy (p_tlv_list + updated_len, p_tlv_list + xx, (len + 2)); 408 } 409 updated_len += (len + 2); 410 } 411 xx += len + 2; /* move to next TLV */ 412 } 413 414 /* If any TVLs to update, or if the SetConfig was initiated by the application, then send the SET_CONFIG command */ 415 if (updated_len || app_init) 416 { 417 if ((nfc_status = NFC_SetConfig (updated_len, p_tlv_list)) == NFC_STATUS_OK) 418 { 419 /* Keep track of whether we will need to notify NFA_DM_SET_CONFIG_EVT on NFC_SET_CONFIG_REVT */ 420 421 /* Get the next available bit offset for this setconfig (based on how many SetConfigs are outstanding) */ 422 cur_bit = (UINT32) (1 << nfa_dm_cb.setcfg_pending_num); 423 424 /* If setconfig is due to NFA_SetConfig: then set the bit (NFA_DM_SET_CONFIG_EVT needed on NFC_SET_CONFIG_REVT) */ 425 if (app_init) 426 { 427 nfa_dm_cb.setcfg_pending_mask |= cur_bit; 428 } 429 /* Otherwise setconfig is internal: clear the bit (NFA_DM_SET_CONFIG_EVT not needed on NFC_SET_CONFIG_REVT) */ 430 else 431 { 432 nfa_dm_cb.setcfg_pending_mask &= ~cur_bit; 433 } 434 435 /* Increment setcfg_pending counter */ 436 nfa_dm_cb.setcfg_pending_num++; 437 } 438 return (nfc_status); 439 440 } 441 else 442 { 443 return NFA_STATUS_OK; 444 } 445 } 446 447 #if (BT_TRACE_VERBOSE == TRUE) 448 /******************************************************************************* 449 ** 450 ** Function nfa_dm_nfc_revt_2_str 451 ** 452 ** Description convert nfc revt to string 453 ** 454 *******************************************************************************/ 455 static char *nfa_dm_evt_2_str (UINT16 event) 456 { 457 switch (NFA_SYS_EVT_START (NFA_ID_DM) | event) 458 { 459 case NFA_DM_API_ENABLE_EVT: 460 return "NFA_DM_API_ENABLE_EVT"; 461 462 case NFA_DM_API_DISABLE_EVT: 463 return "NFA_DM_API_DISABLE_EVT"; 464 465 case NFA_DM_API_SET_CONFIG_EVT: 466 return "NFA_DM_API_SET_CONFIG_EVT"; 467 468 case NFA_DM_API_GET_CONFIG_EVT: 469 return "NFA_DM_API_GET_CONFIG_EVT"; 470 471 case NFA_DM_API_REQUEST_EXCL_RF_CTRL_EVT: 472 return "NFA_DM_API_REQUEST_EXCL_RF_CTRL_EVT"; 473 474 case NFA_DM_API_RELEASE_EXCL_RF_CTRL_EVT: 475 return "NFA_DM_API_RELEASE_EXCL_RF_CTRL_EVT"; 476 477 case NFA_DM_API_ENABLE_POLLING_EVT: 478 return "NFA_DM_API_ENABLE_POLLING_EVT"; 479 480 case NFA_DM_API_DISABLE_POLLING_EVT: 481 return "NFA_DM_API_DISABLE_POLLING_EVT"; 482 483 case NFA_DM_API_ENABLE_LISTENING_EVT: 484 return "NFA_DM_API_ENABLE_LISTENING_EVT"; 485 486 case NFA_DM_API_DISABLE_LISTENING_EVT: 487 return "NFA_DM_API_DISABLE_LISTENING_EVT"; 488 489 case NFA_DM_API_PAUSE_P2P_EVT: 490 return "NFA_DM_API_PAUSE_P2P_EVT"; 491 492 case NFA_DM_API_RESUME_P2P_EVT: 493 return "NFA_DM_API_RESUME_P2P_EVT"; 494 495 case NFA_DM_API_RAW_FRAME_EVT: 496 return "NFA_DM_API_RAW_FRAME_EVT"; 497 498 case NFA_DM_API_SET_P2P_LISTEN_TECH_EVT: 499 return "NFA_DM_API_SET_P2P_LISTEN_TECH_EVT"; 500 501 case NFA_DM_API_START_RF_DISCOVERY_EVT: 502 return "NFA_DM_API_START_RF_DISCOVERY_EVT"; 503 504 case NFA_DM_API_STOP_RF_DISCOVERY_EVT: 505 return "NFA_DM_API_STOP_RF_DISCOVERY_EVT"; 506 507 case NFA_DM_API_SET_RF_DISC_DURATION_EVT: 508 return "NFA_DM_API_SET_RF_DISC_DURATION_EVT"; 509 510 case NFA_DM_API_SELECT_EVT: 511 return "NFA_DM_API_SELECT_EVT"; 512 513 case NFA_DM_API_UPDATE_RF_PARAMS_EVT: 514 return "NFA_DM_API_UPDATE_RF_PARAMS_EVT"; 515 516 case NFA_DM_API_DEACTIVATE_EVT: 517 return "NFA_DM_API_DEACTIVATE_EVT"; 518 519 case NFA_DM_API_POWER_OFF_SLEEP_EVT: 520 return "NFA_DM_API_POWER_OFF_SLEEP_EVT"; 521 522 case NFA_DM_API_REG_NDEF_HDLR_EVT: 523 return "NFA_DM_API_REG_NDEF_HDLR_EVT"; 524 525 case NFA_DM_API_DEREG_NDEF_HDLR_EVT: 526 return "NFA_DM_API_DEREG_NDEF_HDLR_EVT"; 527 528 case NFA_DM_TIMEOUT_DISABLE_EVT: 529 return "NFA_DM_TIMEOUT_DISABLE_EVT"; 530 531 } 532 533 return "Unknown or Vendor Specific"; 534 } 535 #endif /* BT_TRACE_VERBOSE */ 536