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