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