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