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 file contains the action functions for device manager state
22  *  machine.
23  *
24  ******************************************************************************/
25 #include <string.h>
26 
27 #include <android-base/stringprintf.h>
28 #include <base/logging.h>
29 
30 #include "nci_hmsgs.h"
31 #include "nfa_api.h"
32 #include "nfa_ce_int.h"
33 #include "nfa_p2p_int.h"
34 #include "nfa_rw_api.h"
35 #include "nfa_rw_int.h"
36 
37 #if (NFC_NFCEE_INCLUDED == TRUE)
38 #include "nfa_ee_int.h"
39 
40 #endif
41 
42 #if (NFA_SNEP_INCLUDED == TRUE)
43 #include "nfa_snep_int.h"
44 #endif
45 
46 using android::base::StringPrintf;
47 
48 extern bool nfc_debug_enabled;
49 
50 /* This is the timeout value to guarantee disable is performed within reasonable
51  * amount of time */
52 #ifndef NFA_DM_DISABLE_TIMEOUT_VAL
53 #define NFA_DM_DISABLE_TIMEOUT_VAL 1000
54 #endif
55 
56 static void nfa_dm_set_init_nci_params(void);
57 static tNFA_STATUS nfa_dm_start_polling(void);
58 static bool nfa_dm_deactivate_polling(void);
59 static void nfa_dm_excl_disc_cback(tNFA_DM_RF_DISC_EVT event,
60                                    tNFC_DISCOVER* p_data);
61 static void nfa_dm_poll_disc_cback(tNFA_DM_RF_DISC_EVT event,
62                                    tNFC_DISCOVER* p_data);
63 
64 /*******************************************************************************
65 **
66 ** Function         nfa_dm_module_init_cback
67 **
68 ** Description      Processing initialization complete event from sub-modules
69 **
70 ** Returns          None
71 **
72 *******************************************************************************/
nfa_dm_module_init_cback(void)73 static void nfa_dm_module_init_cback(void) {
74   tNFA_DM_CBACK_DATA dm_cback_data;
75 
76   nfa_dm_cb.flags &= ~NFA_DM_FLAGS_ENABLE_EVT_PEND;
77 
78   /* All subsystem are initialized */
79   dm_cback_data.status = NFA_STATUS_OK;
80   (*nfa_dm_cb.p_dm_cback)(NFA_DM_ENABLE_EVT, &dm_cback_data);
81 }
82 
83 /*******************************************************************************
84 **
85 ** Function         nfa_dm_nfcc_power_mode_proc_complete_cback
86 **
87 ** Description      Processing complete of processing NFCC power state change
88 **                  from all sub-modules
89 **
90 ** Returns          None
91 **
92 *******************************************************************************/
nfa_dm_nfcc_power_mode_proc_complete_cback(void)93 static void nfa_dm_nfcc_power_mode_proc_complete_cback(void) {
94   tNFA_DM_PWR_MODE_CHANGE power_mode_change;
95 
96   DLOG_IF(INFO, nfc_debug_enabled)
97       << StringPrintf("nfcc_pwr_mode = 0x%x", nfa_dm_cb.nfcc_pwr_mode);
98 
99   /* if NFCC power state is change to full power */
100   if (nfa_dm_cb.nfcc_pwr_mode != NFA_DM_PWR_MODE_OFF_SLEEP) {
101     nfa_dm_cb.flags &= ~NFA_DM_FLAGS_NFCC_IS_RESTORING;
102 
103     /* reconfigure BRCM NFCC */
104     nfa_dm_disc_sm_execute(NFA_DM_RF_DISCOVER_CMD, nullptr);
105   }
106 
107   nfa_dm_cb.flags &= ~NFA_DM_FLAGS_SETTING_PWR_MODE;
108 
109   power_mode_change.status = NFA_STATUS_OK;
110   power_mode_change.power_mode = nfa_dm_cb.nfcc_pwr_mode;
111   tNFA_DM_CBACK_DATA nfa_dm_cback_data;
112   nfa_dm_cback_data.power_mode = power_mode_change;
113   (*nfa_dm_cb.p_dm_cback)(NFA_DM_PWR_MODE_CHANGE_EVT, &nfa_dm_cback_data);
114 }
115 /*******************************************************************************
116 **
117 ** Function         nfa_dm_sys_enable
118 **
119 ** Description      This function on enable
120 **
121 ** Returns          void
122 **
123 *******************************************************************************/
nfa_dm_sys_enable(void)124 void nfa_dm_sys_enable(void) { nfa_dm_set_init_nci_params(); }
125 
126 /*******************************************************************************
127 **
128 ** Function         nfa_dm_set_init_nci_params
129 **
130 ** Description      Set initial NCI configuration parameters
131 **
132 ** Returns          void
133 **
134 *******************************************************************************/
nfa_dm_set_init_nci_params(void)135 static void nfa_dm_set_init_nci_params(void) {
136   uint8_t xx;
137 
138   /* set NCI default value if other than zero */
139 
140   if (NFC_GetNCIVersion() == NCI_VERSION_2_0) {
141     /* Default Values: For each identifier
142      * Octet 0-1   = OxFF
143      * Octet 2     = Ox02
144      * Octet 3     = 0xFE
145      * Octet 4-9   = 0x00
146      * Octet 10-17 = 0xFF*/
147     for (xx = 0; xx < NFA_CE_LISTEN_INFO_MAX; xx++) {
148       nfa_dm_cb.params.lf_t3t_id[xx][0] = 0xFF;
149       nfa_dm_cb.params.lf_t3t_id[xx][1] = 0xFF;
150       nfa_dm_cb.params.lf_t3t_id[xx][2] = 0x02;
151       nfa_dm_cb.params.lf_t3t_id[xx][3] = 0xFE;
152     }
153 
154     /* LF_T3T_PMM value is added to LF_T3T_IDENTIFIERS_X in NCI2.0. */
155     for (xx = 0; xx < NFA_CE_LISTEN_INFO_MAX; xx++) {
156       for (uint8_t yy = 10; yy < NCI_PARAM_LEN_LF_T3T_ID(NFC_GetNCIVersion());
157            yy++)
158         nfa_dm_cb.params.lf_t3t_id[xx][yy] = 0xFF;
159     }
160   } else {
161     /* LF_T3T_IDENTIFIERS_1/2/.../16 */
162     for (xx = 0; xx < NFA_CE_LISTEN_INFO_MAX; xx++) {
163       nfa_dm_cb.params.lf_t3t_id[xx][0] = 0xFF;
164       nfa_dm_cb.params.lf_t3t_id[xx][1] = 0xFF;
165       nfa_dm_cb.params.lf_t3t_id[xx][2] = 0x02;
166       nfa_dm_cb.params.lf_t3t_id[xx][3] = 0xFE;
167     }
168 
169     /* LF_T3T_PMM */
170     for (xx = 0; xx < NCI_PARAM_LEN_LF_T3T_PMM; xx++) {
171       nfa_dm_cb.params.lf_t3t_pmm[xx] = 0xFF;
172     }
173   }
174 
175   /* LF_T3T_FLAGS:
176   ** DH needs to set this configuration, even if default value (not listening)
177   ** is used, to let NFCC know of intention (not listening) of DH.
178   */
179 
180   /* FWI */
181   nfa_dm_cb.params.fwi[0] = 0x04;
182 
183   /* WT */
184   nfa_dm_cb.params.wt[0] = 14;
185 
186   /* Set CE default configuration */
187   if (p_nfa_dm_ce_cfg[0] && NFC_GetNCIVersion() != NCI_VERSION_2_0) {
188     nfa_dm_check_set_config(p_nfa_dm_ce_cfg[0], &p_nfa_dm_ce_cfg[1], false);
189   }
190 
191   /* Set optional general default configuration */
192   if (p_nfa_dm_gen_cfg && p_nfa_dm_gen_cfg[0]) {
193     nfa_dm_check_set_config(p_nfa_dm_gen_cfg[0], &p_nfa_dm_gen_cfg[1], false);
194   }
195 
196   if (p_nfa_dm_interface_mapping && nfa_dm_num_dm_interface_mapping) {
197     NFC_DiscoveryMap(nfa_dm_num_dm_interface_mapping,
198                      p_nfa_dm_interface_mapping, nullptr);
199   }
200 }
201 
202 /*******************************************************************************
203 **
204 ** Function         nfa_dm_proc_nfcc_power_mode
205 **
206 ** Description      Processing NFCC power mode changes
207 **
208 ** Returns          None
209 **
210 *******************************************************************************/
nfa_dm_proc_nfcc_power_mode(uint8_t nfcc_power_mode)211 void nfa_dm_proc_nfcc_power_mode(uint8_t nfcc_power_mode) {
212   DLOG_IF(INFO, nfc_debug_enabled)
213       << StringPrintf("nfcc_power_mode=%d", nfcc_power_mode);
214 
215   /* if NFCC power mode is change to full power */
216   if (nfcc_power_mode == NFA_DM_PWR_MODE_FULL) {
217     memset(&nfa_dm_cb.params, 0x00, sizeof(tNFA_DM_PARAMS));
218     DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
219         "setcfg_pending_mask=0x%x, setcfg_pending_num=%d",
220         nfa_dm_cb.setcfg_pending_mask, nfa_dm_cb.setcfg_pending_num);
221     nfa_dm_cb.setcfg_pending_mask = 0;
222     nfa_dm_cb.setcfg_pending_num = 0;
223 
224     nfa_dm_set_init_nci_params();
225     nfa_dm_cb.flags &= ~NFA_DM_FLAGS_POWER_OFF_SLEEP;
226   } else if (nfcc_power_mode == NFA_DM_PWR_MODE_OFF_SLEEP) {
227     nfa_dm_cb.flags |= NFA_DM_FLAGS_POWER_OFF_SLEEP;
228   }
229 
230   nfa_sys_cback_notify_nfcc_power_mode_proc_complete(NFA_ID_DM);
231 }
232 
233 /*******************************************************************************
234 **
235 ** Function         nfa_dm_disable_event
236 **
237 ** Description      report disable event
238 **
239 ** Returns          void
240 **
241 *******************************************************************************/
nfa_dm_disable_event(void)242 static void nfa_dm_disable_event(void) {
243   /* Deregister DM from sys */
244   nfa_sys_deregister(NFA_ID_DM);
245 
246   /* Notify app */
247   nfa_dm_cb.flags &=
248       ~(NFA_DM_FLAGS_DM_IS_ACTIVE | NFA_DM_FLAGS_DM_DISABLING_NFC |
249         NFA_DM_FLAGS_ENABLE_EVT_PEND);
250   (*nfa_dm_cb.p_dm_cback)(NFA_DM_DISABLE_EVT, nullptr);
251 }
252 
253 /*******************************************************************************
254 **
255 ** Function         nfa_dm_nfc_response_cback
256 **
257 ** Description      Call DM event hanlder with NFC response callback data
258 **
259 ** Returns          void
260 **
261 *******************************************************************************/
nfa_dm_nfc_response_cback(tNFC_RESPONSE_EVT event,tNFC_RESPONSE * p_data)262 static void nfa_dm_nfc_response_cback(tNFC_RESPONSE_EVT event,
263                                       tNFC_RESPONSE* p_data) {
264   tNFA_DM_CBACK_DATA dm_cback_data;
265   tNFA_CONN_EVT_DATA conn_evt;
266   uint8_t dm_cback_evt;
267   uint8_t max_ee = 0;
268 
269   DLOG_IF(INFO, nfc_debug_enabled)
270       << StringPrintf("%s(0x%x)", nfa_dm_nfc_revt_2_str(event).c_str(), event);
271 
272   switch (event) {
273     case NFC_ENABLE_REVT: /* 0  Enable event */
274 
275       /* NFC stack enabled. Enable nfa sub-systems */
276       if (p_data->enable.status == NFC_STATUS_OK) {
277         if (nfa_ee_max_ee_cfg != 0) {
278           if (nfa_dm_cb.get_max_ee) {
279             max_ee = nfa_dm_cb.get_max_ee();
280             if (max_ee) {
281               nfa_ee_max_ee_cfg = max_ee;
282             }
283           }
284         }
285         /* Initialize NFA subsystems */
286         nfa_sys_enable_subsystems();
287       } else if (nfa_dm_cb.flags & NFA_DM_FLAGS_ENABLE_EVT_PEND) {
288         /* Notify app */
289         nfa_dm_cb.flags &=
290             ~(NFA_DM_FLAGS_ENABLE_EVT_PEND | NFA_DM_FLAGS_DM_IS_ACTIVE);
291         dm_cback_data.status = p_data->enable.status;
292         (*nfa_dm_cb.p_dm_cback)(NFA_DM_ENABLE_EVT, &dm_cback_data);
293       }
294       break;
295 
296     case NFC_DISABLE_REVT: /* 1  Disable event */
297       nfa_dm_disable_event();
298       break;
299 
300     case NFC_SET_CONFIG_REVT: /* 2  Set Config Response */
301       /* If this setconfig was due to NFA_SetConfig, then notify the app */
302       /* lsb=whether last NCI_SET_CONFIG was due to NFA_SetConfig */
303       if (nfa_dm_cb.setcfg_pending_mask & 1) {
304         dm_cback_data.set_config.status = p_data->set_config.status;
305         dm_cback_data.set_config.num_param_id = p_data->set_config.num_param_id;
306         memcpy(dm_cback_data.set_config.param_ids, p_data->set_config.param_ids,
307                p_data->set_config.num_param_id);
308         (*nfa_dm_cb.p_dm_cback)(NFA_DM_SET_CONFIG_EVT, &dm_cback_data);
309       }
310 
311       /* Update the pending mask */
312       if (nfa_dm_cb.setcfg_pending_num > 0) {
313         nfa_dm_cb.setcfg_pending_mask >>= 1;
314         nfa_dm_cb.setcfg_pending_num--;
315       } else {
316         /* This should not occur (means we got a SET_CONFIG_NTF that's
317          * unaccounted for */
318         LOG(ERROR) << StringPrintf(
319             "NFA received unexpected NFC_SET_CONFIG_REVT");
320       }
321       break;
322 
323     case NFC_GET_CONFIG_REVT: /* 3  Get Config Response */
324       if (p_data->get_config.status == NFC_STATUS_OK) {
325         tNFA_GET_CONFIG* p_nfa_get_confg = &dm_cback_data.get_config;
326         p_nfa_get_confg->status = NFA_STATUS_OK;
327         p_nfa_get_confg->tlv_size = p_data->get_config.tlv_size;
328         p_nfa_get_confg->param_tlvs = p_data->get_config.p_param_tlvs;
329         (*nfa_dm_cb.p_dm_cback)(NFA_DM_GET_CONFIG_EVT, &dm_cback_data);
330         return;
331       }
332 
333       /* Return result of getconfig to the app */
334       dm_cback_data.get_config.status = NFA_STATUS_FAILED;
335       (*nfa_dm_cb.p_dm_cback)(NFA_DM_GET_CONFIG_EVT, &dm_cback_data);
336       break;
337 
338 #if (NFC_NFCEE_INCLUDED == TRUE)
339     case NFC_NFCEE_DISCOVER_REVT: /* NFCEE Discover response */
340     case NFC_NFCEE_INFO_REVT:     /* NFCEE Discover Notification */
341     case NFC_EE_ACTION_REVT:      /* EE Action notification */
342     case NFC_NFCEE_MODE_SET_REVT: /* NFCEE Mode Set response */
343     case NFC_NFCEE_STATUS_REVT:   /* NFCEE Status notification*/
344     case NFC_SET_ROUTING_REVT:    /* Configure Routing response */
345       nfa_ee_proc_evt(event, p_data);
346       break;
347 
348     case NFC_EE_DISCOVER_REQ_REVT: /* EE Discover Req notification */
349       if (nfa_dm_is_active() &&
350           (nfa_dm_cb.disc_cb.disc_state == NFA_DM_RFST_DISCOVERY)) {
351         nfa_dm_rf_deactivate(NFA_DEACTIVATE_TYPE_IDLE);
352       }
353       nfa_ee_proc_evt(event, p_data);
354       break;
355 
356     case NFC_GET_ROUTING_REVT: /* Retrieve Routing response */
357       break;
358 #endif
359 
360     case NFC_SET_POWER_SUB_STATE_REVT:
361       dm_cback_data.power_sub_state.status = p_data->status;
362       dm_cback_data.power_sub_state.power_state = nfa_dm_cb.power_state;
363       (*nfa_dm_cb.p_dm_cback)(NFA_DM_SET_POWER_SUB_STATE_EVT, &dm_cback_data);
364       break;
365 
366     case NFC_RF_FIELD_REVT: /* RF Field information            */
367       dm_cback_data.rf_field.status = NFA_STATUS_OK;
368       dm_cback_data.rf_field.rf_field_status = p_data->rf_field.rf_field;
369       (*nfa_dm_cb.p_dm_cback)(NFA_DM_RF_FIELD_EVT, &dm_cback_data);
370       break;
371 
372     case NFC_GEN_ERROR_REVT: /* generic error command or notification */
373       break;
374 
375     case NFC_NFCC_RESTART_REVT: /* NFCC has been re-initialized */
376 
377       if (p_data->status == NFC_STATUS_OK) {
378         nfa_dm_cb.nfcc_pwr_mode = NFA_DM_PWR_MODE_FULL;
379         nfa_dm_cb.flags |= NFA_DM_FLAGS_NFCC_IS_RESTORING;
380 
381         /* NFCC will start from IDLE when turned on again */
382         nfa_dm_cb.disc_cb.disc_flags &= ~NFA_DM_DISC_FLAGS_W4_RSP;
383         nfa_dm_cb.disc_cb.disc_flags &= ~NFA_DM_DISC_FLAGS_W4_NTF;
384         nfa_dm_cb.disc_cb.disc_state = NFA_DM_RFST_IDLE;
385       } else {
386         nfa_dm_cb.nfcc_pwr_mode = NFA_DM_PWR_MODE_OFF_SLEEP;
387       }
388       /* Notify NFA submodules change of NFCC power mode */
389       nfa_sys_cback_reg_nfcc_power_mode_proc_complete(
390           nfa_dm_nfcc_power_mode_proc_complete_cback);
391       nfa_sys_notify_nfcc_power_mode(nfa_dm_cb.nfcc_pwr_mode);
392       break;
393 
394     case NFC_NFCC_TIMEOUT_REVT:
395     case NFC_NFCC_TRANSPORT_ERR_REVT:
396       DLOG_IF(INFO, nfc_debug_enabled)
397           << StringPrintf("flags:0x%08x", nfa_dm_cb.flags);
398       dm_cback_evt = (event == NFC_NFCC_TIMEOUT_REVT)
399                          ? NFA_DM_NFCC_TIMEOUT_EVT
400                          : NFA_DM_NFCC_TRANSPORT_ERR_EVT;
401       (*nfa_dm_cb.p_dm_cback)(dm_cback_evt, nullptr);
402       break;
403 
404     case NFC_NFCC_POWER_OFF_REVT:
405       nfa_dm_cb.nfcc_pwr_mode = NFA_DM_PWR_MODE_OFF_SLEEP;
406 
407       /* Notify NFA submodules change of NFCC power mode */
408       nfa_sys_cback_reg_nfcc_power_mode_proc_complete(
409           nfa_dm_nfcc_power_mode_proc_complete_cback);
410       nfa_sys_notify_nfcc_power_mode(NFA_DM_PWR_MODE_OFF_SLEEP);
411       break;
412 
413     case NFC_RF_COMM_PARAMS_UPDATE_REVT:
414       conn_evt.status = p_data->status;
415       nfa_dm_conn_cback_event_notify(NFA_UPDATE_RF_PARAM_RESULT_EVT, &conn_evt);
416       break;
417 
418     default:
419       break;
420   }
421 }
422 
423 /*******************************************************************************
424 **
425 ** Function         nfa_dm_enable
426 **
427 ** Description      Initialises the NFC device manager
428 **
429 ** Returns          TRUE (message buffer to be freed by caller)
430 **
431 *******************************************************************************/
nfa_dm_enable(tNFA_DM_MSG * p_data)432 bool nfa_dm_enable(tNFA_DM_MSG* p_data) {
433   tNFA_DM_CBACK_DATA dm_cback_data;
434   DLOG_IF(INFO, nfc_debug_enabled) << __func__;
435 
436   /* Check if NFA is already enabled */
437   if (!(nfa_dm_cb.flags & NFA_DM_FLAGS_DM_IS_ACTIVE)) {
438     /* Initialize BRCM control block, it musb be called before setting any flags
439      */
440     nfa_dm_cb.flags |=
441         (NFA_DM_FLAGS_DM_IS_ACTIVE | NFA_DM_FLAGS_ENABLE_EVT_PEND);
442     nfa_sys_cback_reg_enable_complete(nfa_dm_module_init_cback);
443 
444     /* Store Enable parameters */
445     nfa_dm_cb.p_dm_cback = p_data->enable.p_dm_cback;
446     nfa_dm_cb.p_conn_cback = p_data->enable.p_conn_cback;
447 
448     /* Enable NFC stack */
449     NFC_Enable(nfa_dm_nfc_response_cback);
450   } else {
451     LOG(ERROR) << StringPrintf("nfa_dm_enable: ERROR ALREADY ENABLED.");
452     dm_cback_data.status = NFA_STATUS_ALREADY_STARTED;
453     (*(p_data->enable.p_dm_cback))(NFA_DM_ENABLE_EVT, &dm_cback_data);
454   }
455 
456   return true;
457 }
458 
459 /*******************************************************************************
460 **
461 ** Function         nfa_dm_disable
462 **
463 ** Description      Disables the NFC device manager
464 **
465 ** Returns          TRUE (message buffer to be freed by caller)
466 **
467 *******************************************************************************/
nfa_dm_disable(tNFA_DM_MSG * p_data)468 bool nfa_dm_disable(tNFA_DM_MSG* p_data) {
469   DLOG_IF(INFO, nfc_debug_enabled)
470       << StringPrintf("graceful:%d", p_data->disable.graceful);
471 
472   if (p_data->disable.graceful) {
473     /* if RF discovery is enabled */
474     if (nfa_dm_cb.disc_cb.disc_flags & NFA_DM_DISC_FLAGS_ENABLED) {
475       nfa_dm_cb.disc_cb.disc_flags &= ~NFA_DM_DISC_FLAGS_ENABLED;
476 
477       if (nfa_dm_cb.disc_cb.disc_state == NFA_DM_RFST_IDLE) {
478         /* if waiting RSP in idle state */
479         if (nfa_dm_cb.disc_cb.disc_flags & NFA_DM_DISC_FLAGS_W4_RSP) {
480           nfa_dm_cb.disc_cb.disc_flags |= NFA_DM_DISC_FLAGS_DISABLING;
481         }
482       } else {
483         nfa_dm_cb.disc_cb.disc_flags |= NFA_DM_DISC_FLAGS_DISABLING;
484         tNFA_DM_RF_DISC_DATA nfa_dm_rf_disc_data;
485         nfa_dm_rf_disc_data.deactivate_type = NFA_DEACTIVATE_TYPE_IDLE;
486 
487         nfa_dm_disc_sm_execute(NFA_DM_RF_DEACTIVATE_CMD, &nfa_dm_rf_disc_data);
488         if ((nfa_dm_cb.disc_cb.disc_flags &
489              (NFA_DM_DISC_FLAGS_W4_RSP | NFA_DM_DISC_FLAGS_W4_NTF)) == 0) {
490           /* not waiting to deactivate, clear the flag now */
491           nfa_dm_cb.disc_cb.disc_flags &= ~NFA_DM_DISC_FLAGS_DISABLING;
492         }
493       }
494     }
495     /* Start timeout for graceful shutdown. If timer expires, then force an
496      * ungraceful shutdown */
497     nfa_sys_start_timer(&nfa_dm_cb.tle, NFA_DM_TIMEOUT_DISABLE_EVT,
498                         NFA_DM_DISABLE_TIMEOUT_VAL);
499   }
500 
501   /* Disable all subsystems other than DM (DM will be disabled after all  */
502   /* the other subsystem have been disabled)                              */
503   nfa_sys_disable_subsystems(p_data->disable.graceful);
504   return true;
505 }
506 
507 /*******************************************************************************
508 **
509 ** Function         nfa_dm_disable_complete
510 **
511 ** Description      Called when all NFA subsytems are disabled.
512 **
513 **                  NFC core stack can now be disabled.
514 **
515 ** Returns          void
516 **
517 *******************************************************************************/
nfa_dm_disable_complete(void)518 void nfa_dm_disable_complete(void) {
519   DLOG_IF(INFO, nfc_debug_enabled) << __func__;
520 
521   if ((nfa_dm_cb.flags & NFA_DM_FLAGS_DM_DISABLING_NFC) == 0) {
522     DLOG_IF(INFO, nfc_debug_enabled)
523         << StringPrintf("proceeding with nfc core shutdown.");
524 
525     nfa_dm_cb.flags |= NFA_DM_FLAGS_DM_DISABLING_NFC;
526 
527     nfa_sys_stop_timer(&nfa_dm_cb.tle);
528 
529     /* Free all buffers for NDEF handlers */
530     nfa_dm_ndef_dereg_all();
531 
532     /* Disable nfc core stack */
533     NFC_Disable();
534   }
535 }
536 
537 /*******************************************************************************
538 **
539 ** Function         nfa_dm_set_config
540 **
541 ** Description      Process set config command
542 **
543 ** Returns          TRUE (message buffer to be freed by caller)
544 **
545 *******************************************************************************/
nfa_dm_set_config(tNFA_DM_MSG * p_data)546 bool nfa_dm_set_config(tNFA_DM_MSG* p_data) {
547   tNFC_STATUS status;
548   uint8_t buff[255];
549   uint8_t* p = buff;
550 
551   tNFA_DM_CBACK_DATA dm_cback_data;
552 
553   if (p_data->setconfig.length + 2 > 255) {
554     /* Total length of TLV must be less than 256 (1 byte) */
555     status = NFC_STATUS_FAILED;
556   } else {
557     UINT8_TO_STREAM(p, p_data->setconfig.param_id);
558     UINT8_TO_STREAM(p, p_data->setconfig.length);
559     ARRAY_TO_STREAM(p, p_data->setconfig.p_data, p_data->setconfig.length)
560     status = nfa_dm_check_set_config((uint8_t)(p_data->setconfig.length + 2),
561                                      buff, true);
562   }
563 
564   if (status != NFC_STATUS_OK) {
565     dm_cback_data.set_config.status = NFA_STATUS_INVALID_PARAM;
566     (*nfa_dm_cb.p_dm_cback)(NFA_DM_SET_CONFIG_EVT, &dm_cback_data);
567   }
568 
569   return true;
570 }
571 
572 /*******************************************************************************
573 **
574 ** Function         nfa_dm_get_config
575 **
576 ** Description      Process get config command
577 **
578 ** Returns          TRUE (message buffer to be freed by caller)
579 **
580 *******************************************************************************/
nfa_dm_get_config(tNFA_DM_MSG * p_data)581 bool nfa_dm_get_config(tNFA_DM_MSG* p_data) {
582   NFC_GetConfig(p_data->getconfig.num_ids, p_data->getconfig.p_pmids);
583 
584   return true;
585 }
586 /*******************************************************************************
587 **
588 ** Function         nfa_dm_set_power_sub_state
589 **
590 ** Description      Process the power sub state command
591 **
592 ** Returns          TRUE (message buffer to be freed by caller)
593 **
594 *******************************************************************************/
nfa_dm_set_power_sub_state(tNFA_DM_MSG * p_data)595 bool nfa_dm_set_power_sub_state(tNFA_DM_MSG* p_data) {
596   tNFC_STATUS status;
597   tNFA_DM_CBACK_DATA dm_cback_data;
598 
599   DLOG_IF(INFO, nfc_debug_enabled) << __func__;
600 
601   nfa_dm_cb.power_state = p_data->set_power_state.screen_state;
602   if (nfa_dm_cb.disc_cb.disc_state == NFA_DM_RFST_LISTEN_ACTIVE) {
603     DLOG_IF(INFO, nfc_debug_enabled)
604         << StringPrintf("NFA_DM_RFST_LISTEN_ACTIVE");
605     /* NFCC will give semantic error for power sub state command in Rf listen
606      * active state */
607     nfa_dm_cb.pending_power_state = nfa_dm_cb.power_state;
608     status = NFC_STATUS_SEMANTIC_ERROR;
609   } else {
610     status = NFC_SetPowerSubState(p_data->set_power_state.screen_state);
611   }
612 
613   if (status != NFC_STATUS_OK) {
614     dm_cback_data.power_sub_state.status = NFC_STATUS_FAILED;
615     dm_cback_data.power_sub_state.power_state = nfa_dm_cb.power_state;
616     (*nfa_dm_cb.p_dm_cback)(NFA_DM_SET_POWER_SUB_STATE_EVT, &dm_cback_data);
617   }
618   return (true);
619 }
620 /*******************************************************************************
621 **
622 ** Function         nfa_dm_conn_cback_event_notify
623 **
624 ** Description      Notify application of CONN_CBACK event, using appropriate
625 **                  callback
626 **
627 ** Returns          nothing
628 **
629 *******************************************************************************/
nfa_dm_conn_cback_event_notify(uint8_t event,tNFA_CONN_EVT_DATA * p_data)630 void nfa_dm_conn_cback_event_notify(uint8_t event, tNFA_CONN_EVT_DATA* p_data) {
631   if (nfa_dm_cb.flags & NFA_DM_FLAGS_EXCL_RF_ACTIVE) {
632     /* Use exclusive RF mode callback */
633     if (nfa_dm_cb.p_excl_conn_cback)
634       (*nfa_dm_cb.p_excl_conn_cback)(event, p_data);
635   } else {
636     (*nfa_dm_cb.p_conn_cback)(event, p_data);
637   }
638 }
639 
640 /*******************************************************************************
641 **
642 ** Function         nfa_dm_rel_excl_rf_control_and_notify
643 **
644 ** Description      Stop exclusive RF control and notify app of
645 **                  NFA_EXCLUSIVE_RF_CONTROL_STOPPED_EVT
646 **
647 ** Returns          void
648 **
649 *******************************************************************************/
nfa_dm_rel_excl_rf_control_and_notify(void)650 void nfa_dm_rel_excl_rf_control_and_notify(void) {
651   tNFA_CONN_EVT_DATA conn_evt;
652 
653   DLOG_IF(INFO, nfc_debug_enabled) << __func__;
654 
655   /* Exclusive RF control stopped. Notify app */
656   nfa_dm_cb.flags &= ~NFA_DM_FLAGS_EXCL_RF_ACTIVE;
657 
658   /* Stop exclusive RF discovery for exclusive RF mode */
659   nfa_dm_stop_excl_discovery();
660 
661   /* Notify app that exclusive RF control has stopped */
662   conn_evt.status = NFA_STATUS_OK;
663   (*nfa_dm_cb.p_excl_conn_cback)(NFA_EXCLUSIVE_RF_CONTROL_STOPPED_EVT,
664                                  &conn_evt);
665   nfa_dm_cb.p_excl_conn_cback = nullptr;
666   nfa_dm_cb.p_excl_ndef_cback = nullptr;
667 }
668 
669 /*******************************************************************************
670 **
671 ** Function         nfa_dm_act_request_excl_rf_ctrl
672 **
673 ** Description      Request exclusive RF control
674 **
675 ** Returns          TRUE (message buffer to be freed by caller)
676 **
677 *******************************************************************************/
nfa_dm_act_request_excl_rf_ctrl(tNFA_DM_MSG * p_data)678 bool nfa_dm_act_request_excl_rf_ctrl(tNFA_DM_MSG* p_data) {
679   tNFA_CONN_EVT_DATA conn_evt;
680 
681   DLOG_IF(INFO, nfc_debug_enabled) << __func__;
682 
683   if (!nfa_dm_cb.p_excl_conn_cback) {
684     if (nfa_dm_cb.disc_cb.disc_state != NFA_DM_RFST_IDLE) {
685       conn_evt.status = NFA_STATUS_FAILED;
686       (*p_data->req_excl_rf_ctrl.p_conn_cback)(
687           NFA_EXCLUSIVE_RF_CONTROL_STARTED_EVT, &conn_evt);
688       return true;
689     }
690 
691     /* Store callbacks */
692     nfa_dm_cb.p_excl_conn_cback = p_data->req_excl_rf_ctrl.p_conn_cback;
693     nfa_dm_cb.p_excl_ndef_cback = p_data->req_excl_rf_ctrl.p_ndef_cback;
694 
695     nfa_dm_cb.flags |= NFA_DM_FLAGS_EXCL_RF_ACTIVE;
696 
697     /* start exclusive RF discovery */
698     nfa_dm_start_excl_discovery(p_data->req_excl_rf_ctrl.poll_mask,
699                                 &p_data->req_excl_rf_ctrl.listen_cfg,
700                                 nfa_dm_excl_disc_cback);
701   } else {
702     LOG(ERROR) << StringPrintf("Exclusive rf control already requested");
703 
704     conn_evt.status = NFA_STATUS_FAILED;
705     (*p_data->req_excl_rf_ctrl.p_conn_cback)(
706         NFA_EXCLUSIVE_RF_CONTROL_STARTED_EVT, &conn_evt);
707   }
708 
709   return true;
710 }
711 
712 /*******************************************************************************
713 **
714 ** Function         nfa_dm_act_release_excl_rf_ctrl
715 **
716 ** Description      Release exclusive RF control
717 **
718 ** Returns          TRUE (message buffer to be freed by caller)
719 **
720 *******************************************************************************/
nfa_dm_act_release_excl_rf_ctrl(tNFA_DM_MSG * p_data)721 bool nfa_dm_act_release_excl_rf_ctrl(__attribute__((unused))
722                                      tNFA_DM_MSG* p_data) {
723   DLOG_IF(INFO, nfc_debug_enabled) << __func__;
724 
725   /* nfa_dm_rel_excl_rf_control_and_notify() is called when discovery state goes
726    * IDLE */
727   nfa_dm_cb.disc_cb.disc_flags |= NFA_DM_DISC_FLAGS_STOPPING;
728 
729   /* if discover command has been sent in IDLE state and waiting for response
730   ** then just wait for responose. Otherwise initiate deactivating.
731   */
732   if (!((nfa_dm_cb.disc_cb.disc_state == NFA_DM_RFST_IDLE) &&
733         (nfa_dm_cb.disc_cb.disc_flags & NFA_DM_DISC_FLAGS_W4_RSP))) {
734     nfa_dm_rf_deactivate(NFA_DEACTIVATE_TYPE_IDLE);
735   }
736 
737   if (nfa_dm_cb.disc_cb.kovio_tle.in_use)
738     nfa_sys_stop_timer(&nfa_dm_cb.disc_cb.kovio_tle);
739 
740   return true;
741 }
742 
743 /*******************************************************************************
744 **
745 ** Function         nfa_dm_act_deactivate
746 **
747 ** Description      Process deactivate command
748 **
749 ** Returns          TRUE (message buffer to be freed by caller)
750 **
751 *******************************************************************************/
nfa_dm_act_deactivate(tNFA_DM_MSG * p_data)752 bool nfa_dm_act_deactivate(tNFA_DM_MSG* p_data) {
753   tNFA_CONN_EVT_DATA conn_evt;
754   tNFA_DEACTIVATE_TYPE deact_type;
755 
756   DLOG_IF(INFO, nfc_debug_enabled) << __func__;
757 
758   /* Always allow deactivate to IDLE */
759   /* Do not allow deactivate to SLEEP for T1T,NFCDEP, ISO15693 */
760   if (p_data->deactivate.sleep_mode == false ||
761       (nfa_dm_cb.disc_cb.activated_protocol != NFA_PROTOCOL_T1T &&
762        (nfa_dm_cb.disc_cb.activated_protocol != NFA_PROTOCOL_NFC_DEP ||
763         appl_dta_mode_flag) &&
764        nfa_dm_cb.disc_cb.activated_protocol != NFA_PROTOCOL_T5T &&
765        nfa_dm_cb.disc_cb.activated_protocol != NFC_PROTOCOL_KOVIO)) {
766     deact_type = NFA_DEACTIVATE_TYPE_DISCOVERY;
767     if (p_data->deactivate.sleep_mode) {
768       if (nfa_dm_cb.disc_cb.disc_state == NFA_DM_RFST_W4_HOST_SELECT) {
769         /* Deactivate to sleep mode not allowed in this state. */
770         deact_type = NFA_DEACTIVATE_TYPE_IDLE;
771       } else if (appl_dta_mode_flag == true &&
772                  (nfa_dm_cb.disc_cb.disc_state != NFA_DM_RFST_LISTEN_SLEEP ||
773                   nfa_dm_cb.disc_cb.disc_state == NFA_DM_RFST_POLL_ACTIVE)) {
774         deact_type = NFA_DEACTIVATE_TYPE_SLEEP;
775       } else if (nfa_dm_cb.disc_cb.disc_state != NFA_DM_RFST_LISTEN_SLEEP) {
776         deact_type = NFA_DEACTIVATE_TYPE_SLEEP;
777       }
778     }
779     if (nfa_dm_cb.disc_cb.disc_state == NFA_DM_RFST_W4_ALL_DISCOVERIES) {
780       /* Only deactivate to IDLE is allowed in this state. */
781       deact_type = NFA_DEACTIVATE_TYPE_IDLE;
782     }
783 
784     if ((nfa_dm_cb.disc_cb.activated_protocol == NFA_PROTOCOL_NFC_DEP) &&
785         ((nfa_dm_cb.flags & NFA_DM_FLAGS_EXCL_RF_ACTIVE) == 0x00) &&
786         appl_dta_mode_flag != true) {
787       /* Exclusive RF control doesn't use NFA P2P */
788       /* NFA P2P will deactivate NFC link after deactivating LLCP link */
789       if (!(nfa_dm_cb.flags & NFA_DM_FLAGS_P2P_PAUSED)) {
790         nfa_p2p_deactivate_llcp();
791       } else {
792         nfa_dm_rf_deactivate(deact_type);
793       }
794       return true;
795     } else {
796       if (nfa_dm_rf_deactivate(deact_type) == NFA_STATUS_OK) {
797         if (nfa_dm_cb.disc_cb.kovio_tle.in_use)
798           nfa_sys_stop_timer(&nfa_dm_cb.disc_cb.kovio_tle);
799         nfa_rw_stop_presence_check_timer();
800         return true;
801       }
802     }
803   }
804 
805   LOG(ERROR) << StringPrintf("invalid protocol, mode or state");
806 
807   /* Notify error to application */
808   conn_evt.status = NFA_STATUS_FAILED;
809   nfa_dm_conn_cback_event_notify(NFA_DEACTIVATE_FAIL_EVT, &conn_evt);
810 
811   return true;
812 }
813 
814 /*******************************************************************************
815 **
816 ** Function         nfa_dm_act_power_off_sleep
817 **
818 ** Description      Process power off sleep mode request
819 **
820 ** Returns          TRUE (message buffer to be freed by caller)
821 **
822 *******************************************************************************/
nfa_dm_act_power_off_sleep(tNFA_DM_MSG * p_data)823 bool nfa_dm_act_power_off_sleep(tNFA_DM_MSG* p_data) {
824   DLOG_IF(INFO, nfc_debug_enabled) << __func__;
825 
826   NFC_SetPowerOffSleep((bool)(p_data->hdr.layer_specific));
827 
828   return true;
829 }
830 
831 /*******************************************************************************
832 **
833 ** Function         nfa_dm_act_reg_vsc
834 **
835 ** Description      Process registers VSC callback
836 **
837 ** Returns          TRUE (message buffer to be freed by caller)
838 **
839 *******************************************************************************/
nfa_dm_act_reg_vsc(tNFA_DM_MSG * p_data)840 bool nfa_dm_act_reg_vsc(tNFA_DM_MSG* p_data) {
841   if (NFC_RegVSCback(p_data->reg_vsc.is_register, p_data->reg_vsc.p_cback) !=
842       NFC_STATUS_OK) {
843     LOG(ERROR) << StringPrintf("NFC_RegVSCback failed");
844   }
845   return true;
846 }
847 
848 /*******************************************************************************
849 **
850 ** Function         nfa_dm_act_send_vsc
851 **
852 ** Description      Send the NCI Vendor Specific command to the NCI command
853 **                  queue
854 **
855 ** Returns          FALSE (message buffer is NOT freed by caller)
856 **
857 *******************************************************************************/
nfa_dm_act_send_vsc(tNFA_DM_MSG * p_data)858 bool nfa_dm_act_send_vsc(tNFA_DM_MSG* p_data) {
859   NFC_HDR* p_cmd = (NFC_HDR*)p_data;
860 
861   p_cmd->offset = sizeof(tNFA_DM_API_SEND_VSC) - NFC_HDR_SIZE;
862   p_cmd->len = p_data->send_vsc.cmd_params_len;
863   NFC_SendVsCommand(p_data->send_vsc.oid, p_cmd, p_data->send_vsc.p_cback);
864 
865   /* Most dm action functions return TRUE, so nfa-sys frees the GKI buffer
866    * carrying the message, This action function re-use the GKI buffer to
867    * send the VSC, so the GKI buffer can not be freed by nfa-sys */
868 
869   return false;
870 }
871 
872 /*******************************************************************************
873 **
874 ** Function         nfa_dm_act_send_raw_vs
875 **
876 ** Description      Send the raw vs command to the NCI command queue
877 **
878 ** Returns          FALSE (message buffer is NOT freed by caller)
879 **
880 *******************************************************************************/
nfa_dm_act_send_raw_vs(tNFA_DM_MSG * p_data)881 bool nfa_dm_act_send_raw_vs(tNFA_DM_MSG* p_data) {
882   NFC_HDR* p_cmd = (NFC_HDR*)p_data;
883 
884   p_cmd->offset = sizeof(tNFA_DM_API_SEND_VSC) - NFC_HDR_SIZE;
885   p_cmd->len = p_data->send_vsc.cmd_params_len;
886   NFC_SendRawVsCommand(p_cmd, p_data->send_vsc.p_cback);
887 
888   /* Most dm action functions return TRUE, so nfa-sys frees the GKI buffer
889    * carrying the message,
890    * This action function re-use the GKI buffer to send the VSC, so the GKI
891    * buffer can not be freed by nfa-sys */
892   return false;
893 }
894 
895 /*******************************************************************************
896 **
897 ** Function         nfa_dm_start_polling
898 **
899 ** Description      Start polling
900 **
901 ** Returns          tNFA_STATUS
902 **
903 *******************************************************************************/
nfa_dm_start_polling(void)904 tNFA_STATUS nfa_dm_start_polling(void) {
905   tNFA_STATUS status;
906   tNFA_TECHNOLOGY_MASK poll_tech_mask;
907   tNFA_DM_DISC_TECH_PROTO_MASK poll_disc_mask = 0;
908 
909   DLOG_IF(INFO, nfc_debug_enabled) << __func__;
910 
911   poll_tech_mask = nfa_dm_cb.poll_mask;
912 
913   /* start RF discovery with discovery callback */
914   if (nfa_dm_cb.poll_disc_handle == NFA_HANDLE_INVALID) {
915     if (poll_tech_mask & NFA_TECHNOLOGY_MASK_A) {
916       poll_disc_mask |= NFA_DM_DISC_MASK_PA_T1T;
917       poll_disc_mask |= NFA_DM_DISC_MASK_PA_T2T;
918       poll_disc_mask |= NFA_DM_DISC_MASK_PA_ISO_DEP;
919       poll_disc_mask |= NFA_DM_DISC_MASK_PA_NFC_DEP;
920       poll_disc_mask |= NFA_DM_DISC_MASK_P_LEGACY;
921       poll_disc_mask |= NFA_DM_DISC_MASK_PA_MIFARE;
922     }
923     if (NFC_GetNCIVersion() == NCI_VERSION_2_0) {
924       if (poll_tech_mask & NFA_TECHNOLOGY_MASK_ACTIVE) {
925         poll_disc_mask |= NFA_DM_DISC_MASK_PACM_NFC_DEP;
926       }
927     } else {
928       if (poll_tech_mask & NFA_TECHNOLOGY_MASK_A_ACTIVE) {
929         poll_disc_mask |= NFA_DM_DISC_MASK_PAA_NFC_DEP;
930       }
931       if (poll_tech_mask & NFA_TECHNOLOGY_MASK_F_ACTIVE) {
932         poll_disc_mask |= NFA_DM_DISC_MASK_PFA_NFC_DEP;
933       }
934     }
935     if (poll_tech_mask & NFA_TECHNOLOGY_MASK_B) {
936       poll_disc_mask |= NFA_DM_DISC_MASK_PB_ISO_DEP;
937     }
938     if (poll_tech_mask & NFA_TECHNOLOGY_MASK_F) {
939       poll_disc_mask |= NFA_DM_DISC_MASK_PF_T3T;
940       poll_disc_mask |= NFA_DM_DISC_MASK_PF_NFC_DEP;
941     }
942     if (poll_tech_mask & NFA_TECHNOLOGY_MASK_V) {
943       poll_disc_mask |= NFA_DM_DISC_MASK_P_T5T;
944     }
945     if (poll_tech_mask & NFA_TECHNOLOGY_MASK_B_PRIME) {
946       poll_disc_mask |= NFA_DM_DISC_MASK_P_B_PRIME;
947     }
948     if (poll_tech_mask & NFA_TECHNOLOGY_MASK_KOVIO) {
949       poll_disc_mask |= NFA_DM_DISC_MASK_P_KOVIO;
950     }
951 
952     nfa_dm_cb.poll_disc_handle = nfa_dm_add_rf_discover(
953         poll_disc_mask, NFA_DM_DISC_HOST_ID_DH, nfa_dm_poll_disc_cback);
954 
955     if (nfa_dm_cb.poll_disc_handle != NFA_HANDLE_INVALID)
956       status = NFA_STATUS_OK;
957     else
958       status = NFA_STATUS_FAILED;
959   } else {
960     status = NFA_STATUS_OK;
961   }
962 
963   return (status);
964 }
965 
966 /*******************************************************************************
967 **
968 ** Function         nfa_dm_act_enable_polling
969 **
970 ** Description      Process enable polling command
971 **
972 ** Returns          TRUE (message buffer to be freed by caller)
973 **
974 *******************************************************************************/
nfa_dm_act_enable_polling(tNFA_DM_MSG * p_data)975 bool nfa_dm_act_enable_polling(tNFA_DM_MSG* p_data) {
976   tNFA_CONN_EVT_DATA evt_data;
977 
978   DLOG_IF(INFO, nfc_debug_enabled) << __func__;
979 
980   if ((!(nfa_dm_cb.flags & NFA_DM_FLAGS_POLLING_ENABLED)) &&
981       (!(nfa_dm_cb.flags & NFA_DM_FLAGS_EXCL_RF_ACTIVE))) {
982     nfa_dm_cb.poll_mask = p_data->enable_poll.poll_mask;
983 
984     if (nfa_dm_start_polling() == NFA_STATUS_OK) {
985       nfa_dm_cb.flags |= NFA_DM_FLAGS_POLLING_ENABLED;
986 
987       evt_data.status = NFA_STATUS_OK;
988       nfa_dm_conn_cback_event_notify(NFA_POLL_ENABLED_EVT, &evt_data);
989       return true;
990     }
991   } else {
992     LOG(ERROR) << StringPrintf("already started");
993   }
994 
995   /* send NFA_POLL_ENABLED_EVT with NFA_STATUS_FAILED */
996   evt_data.status = NFA_STATUS_FAILED;
997   nfa_dm_conn_cback_event_notify(NFA_POLL_ENABLED_EVT, &evt_data);
998 
999   return true;
1000 }
1001 
1002 /*******************************************************************************
1003 **
1004 ** Function         nfa_dm_deactivate_polling
1005 **
1006 ** Description      Deactivate any polling state
1007 **
1008 ** Returns          TRUE if need to wait for deactivation
1009 **
1010 *******************************************************************************/
nfa_dm_deactivate_polling(void)1011 static bool nfa_dm_deactivate_polling(void) {
1012   DLOG_IF(INFO, nfc_debug_enabled) << __func__;
1013 
1014   if ((nfa_dm_cb.disc_cb.disc_state == NFA_DM_RFST_W4_ALL_DISCOVERIES) ||
1015       (nfa_dm_cb.disc_cb.disc_state == NFA_DM_RFST_W4_HOST_SELECT)) {
1016     nfa_dm_rf_deactivate(NFA_DEACTIVATE_TYPE_IDLE);
1017     return false;
1018   } else if (nfa_dm_cb.disc_cb.disc_state == NFA_DM_RFST_POLL_ACTIVE) {
1019     if (nfa_dm_cb.disc_cb.activated_protocol == NFC_PROTOCOL_NFC_DEP) {
1020       /* NFA P2P will deactivate NFC link after deactivating LLCP link */
1021       nfa_p2p_deactivate_llcp();
1022     } else {
1023       nfa_dm_rf_deactivate(NFA_DEACTIVATE_TYPE_IDLE);
1024     }
1025     return true;
1026   } else {
1027     return false;
1028   }
1029 }
1030 
1031 /*******************************************************************************
1032 **
1033 ** Function         nfa_dm_act_disable_polling
1034 **
1035 ** Description      Process disable polling command
1036 **
1037 ** Returns          TRUE (message buffer to be freed by caller)
1038 **
1039 *******************************************************************************/
nfa_dm_act_disable_polling(tNFA_DM_MSG * p_data)1040 bool nfa_dm_act_disable_polling(__attribute__((unused)) tNFA_DM_MSG* p_data) {
1041   tNFA_CONN_EVT_DATA evt_data;
1042 
1043   DLOG_IF(INFO, nfc_debug_enabled) << __func__;
1044 
1045   if (nfa_dm_cb.poll_disc_handle != NFA_HANDLE_INVALID) {
1046     nfa_dm_cb.flags &= ~NFA_DM_FLAGS_POLLING_ENABLED;
1047 
1048     if (nfa_dm_deactivate_polling() == false) {
1049       nfa_dm_delete_rf_discover(nfa_dm_cb.poll_disc_handle);
1050       nfa_dm_cb.poll_disc_handle = NFA_HANDLE_INVALID;
1051 
1052       evt_data.status = NFA_STATUS_OK;
1053       nfa_dm_conn_cback_event_notify(NFA_POLL_DISABLED_EVT, &evt_data);
1054     } else {
1055       nfa_dm_cb.flags |= NFA_DM_FLAGS_SEND_POLL_STOP_EVT;
1056     }
1057   } else {
1058     evt_data.status = NFA_STATUS_FAILED;
1059     nfa_dm_conn_cback_event_notify(NFA_POLL_DISABLED_EVT, &evt_data);
1060   }
1061 
1062   return true;
1063 }
1064 
1065 /*******************************************************************************
1066 **
1067 ** Function         nfa_dm_act_enable_listening
1068 **
1069 ** Description      Process enable listening command
1070 **
1071 ** Returns          TRUE (message buffer to be freed by caller)
1072 **
1073 *******************************************************************************/
nfa_dm_act_enable_listening(tNFA_DM_MSG * p_data)1074 bool nfa_dm_act_enable_listening(__attribute__((unused)) tNFA_DM_MSG* p_data) {
1075   tNFA_CONN_EVT_DATA evt_data;
1076 
1077   DLOG_IF(INFO, nfc_debug_enabled) << __func__;
1078 
1079   nfa_dm_cb.flags &= ~NFA_DM_FLAGS_LISTEN_DISABLED;
1080   evt_data.status = NFA_STATUS_OK;
1081   nfa_dm_conn_cback_event_notify(NFA_LISTEN_ENABLED_EVT, &evt_data);
1082 
1083   return true;
1084 }
1085 
1086 /*******************************************************************************
1087 **
1088 ** Function         nfa_dm_act_disable_listening
1089 **
1090 ** Description      Process disable listening command
1091 **
1092 ** Returns          TRUE (message buffer to be freed by caller)
1093 **
1094 *******************************************************************************/
nfa_dm_act_disable_listening(tNFA_DM_MSG * p_data)1095 bool nfa_dm_act_disable_listening(__attribute__((unused)) tNFA_DM_MSG* p_data) {
1096   tNFA_CONN_EVT_DATA evt_data;
1097 
1098   DLOG_IF(INFO, nfc_debug_enabled) << __func__;
1099 
1100   nfa_dm_cb.flags |= NFA_DM_FLAGS_LISTEN_DISABLED;
1101   evt_data.status = NFA_STATUS_OK;
1102   nfa_dm_conn_cback_event_notify(NFA_LISTEN_DISABLED_EVT, &evt_data);
1103 
1104   return true;
1105 }
1106 
1107 /*******************************************************************************
1108 **
1109 ** Function         nfa_dm_act_pause_p2p
1110 **
1111 ** Description      Process Pause P2P command
1112 **
1113 ** Returns          TRUE (message buffer to be freed by caller)
1114 **
1115 *******************************************************************************/
nfa_dm_act_pause_p2p(tNFA_DM_MSG * p_data)1116 bool nfa_dm_act_pause_p2p(__attribute__((unused)) tNFA_DM_MSG* p_data) {
1117   tNFA_CONN_EVT_DATA evt_data;
1118 
1119   DLOG_IF(INFO, nfc_debug_enabled) << __func__;
1120 
1121   nfa_dm_cb.flags |= NFA_DM_FLAGS_P2P_PAUSED;
1122   evt_data.status = NFA_STATUS_OK;
1123   nfa_dm_conn_cback_event_notify(NFA_P2P_PAUSED_EVT, &evt_data);
1124 
1125   return true;
1126 }
1127 
1128 /*******************************************************************************
1129 **
1130 ** Function         nfa_dm_act_resume_p2p
1131 **
1132 ** Description      Process resume P2P command
1133 **
1134 ** Returns          TRUE (message buffer to be freed by caller)
1135 **
1136 *******************************************************************************/
nfa_dm_act_resume_p2p(tNFA_DM_MSG * p_data)1137 bool nfa_dm_act_resume_p2p(__attribute__((unused)) tNFA_DM_MSG* p_data) {
1138   tNFA_CONN_EVT_DATA evt_data;
1139 
1140   DLOG_IF(INFO, nfc_debug_enabled) << __func__;
1141 
1142   nfa_dm_cb.flags &= ~NFA_DM_FLAGS_P2P_PAUSED;
1143   evt_data.status = NFA_STATUS_OK;
1144   nfa_dm_conn_cback_event_notify(NFA_P2P_RESUMED_EVT, &evt_data);
1145 
1146   return true;
1147 }
1148 
1149 /*******************************************************************************
1150 **
1151 ** Function         nfa_dm_act_send_raw_frame
1152 **
1153 ** Description      Send an raw frame on RF link
1154 **
1155 ** Returns          TRUE (message buffer to be freed by caller)
1156 **
1157 *******************************************************************************/
nfa_dm_act_send_raw_frame(tNFA_DM_MSG * p_data)1158 bool nfa_dm_act_send_raw_frame(tNFA_DM_MSG* p_data) {
1159   tNFC_STATUS status = NFC_STATUS_FAILED;
1160 
1161   DLOG_IF(INFO, nfc_debug_enabled) << __func__;
1162 
1163   /* If NFC link is activated */
1164   if ((nfa_dm_cb.disc_cb.disc_state == NFA_DM_RFST_POLL_ACTIVE) ||
1165       (nfa_dm_cb.disc_cb.disc_state == NFA_DM_RFST_LISTEN_ACTIVE)) {
1166     nfa_dm_cb.flags |= NFA_DM_FLAGS_RAW_FRAME;
1167     NFC_SetReassemblyFlag(false);
1168     /* If not in exclusive mode, and not activated for LISTEN, then forward raw
1169      * data to NFA_RW to send */
1170     if (!(nfa_dm_cb.flags & NFA_DM_FLAGS_EXCL_RF_ACTIVE) &&
1171         !(nfa_dm_cb.disc_cb.disc_state == NFA_DM_RFST_LISTEN_ACTIVE) &&
1172         ((nfa_dm_cb.disc_cb.activated_protocol == NFA_PROTOCOL_T1T) ||
1173          (nfa_dm_cb.disc_cb.activated_protocol == NFA_PROTOCOL_T2T) ||
1174          (nfa_dm_cb.disc_cb.activated_protocol == NFA_PROTOCOL_T3T) ||
1175          (nfa_dm_cb.disc_cb.activated_protocol == NFA_PROTOCOL_ISO_DEP) ||
1176          (nfa_dm_cb.disc_cb.activated_protocol == NFA_PROTOCOL_T5T))) {
1177       /* if RW is checking presence then it will put into pending queue */
1178       status = nfa_rw_send_raw_frame((NFC_HDR*)p_data);
1179     } else {
1180       status = NFC_SendData(NFC_RF_CONN_ID, (NFC_HDR*)p_data);
1181       if (status != NFC_STATUS_OK) {
1182         NFC_SetReassemblyFlag(true);
1183       }
1184       /* Already freed or NCI layer will free buffer */
1185       return false;
1186     }
1187   }
1188 
1189   if (status == NFC_STATUS_FAILED) {
1190     NFC_SetReassemblyFlag(true);
1191     /* free the buffer */
1192     return true;
1193   } else {
1194     /* NCI layer will free buffer */
1195     return false;
1196   }
1197 }
1198 
1199 /*******************************************************************************
1200 **
1201 ** Function         nfa_dm_set_p2p_listen_tech
1202 **
1203 ** Description      Notify change of P2P listen technologies to NFA P2P
1204 **
1205 ** Returns          TRUE (message buffer to be freed by caller)
1206 **
1207 *******************************************************************************/
nfa_dm_set_p2p_listen_tech(tNFA_DM_MSG * p_data)1208 bool nfa_dm_set_p2p_listen_tech(tNFA_DM_MSG* p_data) {
1209   DLOG_IF(INFO, nfc_debug_enabled)
1210       << StringPrintf("tech_mask = %d", p_data->set_p2p_listen_tech.tech_mask);
1211 
1212   nfa_p2p_update_listen_tech(p_data->set_p2p_listen_tech.tech_mask);
1213   nfa_dm_conn_cback_event_notify(NFA_SET_P2P_LISTEN_TECH_EVT, nullptr);
1214 
1215   return true;
1216 }
1217 
1218 /*******************************************************************************
1219 **
1220 ** Function         nfa_dm_act_start_rf_discovery
1221 **
1222 ** Description      Process start RF discovery command
1223 **
1224 ** Returns          TRUE (message buffer to be freed by caller)
1225 **
1226 *******************************************************************************/
nfa_dm_act_start_rf_discovery(tNFA_DM_MSG * p_data)1227 bool nfa_dm_act_start_rf_discovery(__attribute__((unused))
1228                                    tNFA_DM_MSG* p_data) {
1229   tNFA_CONN_EVT_DATA evt_data;
1230 
1231   DLOG_IF(INFO, nfc_debug_enabled) << __func__;
1232 
1233   if (nfa_dm_cb.disc_cb.disc_flags & NFA_DM_DISC_FLAGS_ENABLED) {
1234     evt_data.status = NFA_STATUS_OK;
1235     nfa_dm_conn_cback_event_notify(NFA_RF_DISCOVERY_STARTED_EVT, &evt_data);
1236   } else if (nfa_dm_cb.disc_cb.disc_state != NFA_DM_RFST_IDLE) {
1237     evt_data.status = NFA_STATUS_SEMANTIC_ERROR;
1238     nfa_dm_conn_cback_event_notify(NFA_RF_DISCOVERY_STARTED_EVT, &evt_data);
1239   } else {
1240     nfa_dm_cb.disc_cb.disc_flags |=
1241         (NFA_DM_DISC_FLAGS_ENABLED | NFA_DM_DISC_FLAGS_NOTIFY);
1242     nfa_dm_start_rf_discover();
1243   }
1244 
1245   return true;
1246 }
1247 
1248 /*******************************************************************************
1249 **
1250 ** Function         nfa_dm_act_stop_rf_discovery
1251 **
1252 ** Description      Process stop RF discovery command
1253 **
1254 ** Returns          TRUE (message buffer to be freed by caller)
1255 **
1256 *******************************************************************************/
nfa_dm_act_stop_rf_discovery(tNFA_DM_MSG * p_data)1257 bool nfa_dm_act_stop_rf_discovery(__attribute__((unused)) tNFA_DM_MSG* p_data) {
1258   tNFA_CONN_EVT_DATA evt_data;
1259 
1260   DLOG_IF(INFO, nfc_debug_enabled) << __func__;
1261 
1262   if (!(nfa_dm_cb.disc_cb.disc_flags & NFA_DM_DISC_FLAGS_ENABLED) ||
1263       (nfa_dm_cb.disc_cb.disc_state == NFA_DM_RFST_IDLE)) {
1264     nfa_dm_cb.disc_cb.disc_flags &= ~NFA_DM_DISC_FLAGS_ENABLED;
1265 
1266     /* if discover command has been sent in IDLE state and waiting for response
1267      */
1268     if (nfa_dm_cb.disc_cb.disc_flags & NFA_DM_DISC_FLAGS_W4_RSP) {
1269       nfa_dm_cb.disc_cb.disc_flags |= NFA_DM_DISC_FLAGS_STOPPING;
1270     } else {
1271       evt_data.status = NFA_STATUS_OK;
1272       nfa_dm_conn_cback_event_notify(NFA_RF_DISCOVERY_STOPPED_EVT, &evt_data);
1273     }
1274   } else {
1275     nfa_dm_cb.disc_cb.disc_flags &= ~NFA_DM_DISC_FLAGS_ENABLED;
1276     nfa_dm_cb.disc_cb.disc_flags |= NFA_DM_DISC_FLAGS_STOPPING;
1277 
1278     if (nfa_dm_rf_deactivate(NFA_DEACTIVATE_TYPE_IDLE) == NFA_STATUS_OK) {
1279       if (nfa_dm_cb.disc_cb.kovio_tle.in_use)
1280         nfa_sys_stop_timer(&nfa_dm_cb.disc_cb.kovio_tle);
1281       nfa_rw_stop_presence_check_timer();
1282     }
1283   }
1284   return true;
1285 }
1286 
1287 /*******************************************************************************
1288 **
1289 ** Function         nfa_dm_act_set_rf_disc_duration
1290 **
1291 ** Description      Set duration for RF discovery
1292 **
1293 ** Returns          TRUE (message buffer to be freed by caller)
1294 **
1295 *******************************************************************************/
nfa_dm_act_set_rf_disc_duration(tNFA_DM_MSG * p_data)1296 bool nfa_dm_act_set_rf_disc_duration(tNFA_DM_MSG* p_data) {
1297   nfa_dm_cb.disc_cb.disc_duration = p_data->disc_duration.rf_disc_dur_ms;
1298   return true;
1299 }
1300 
1301 /*******************************************************************************
1302 **
1303 ** Function         nfa_dm_act_get_rf_disc_duration
1304 **
1305 ** Description      Get duration for RF discovery
1306 **
1307 ** Returns          uint16_t
1308 **
1309 *******************************************************************************/
nfa_dm_act_get_rf_disc_duration()1310 uint16_t nfa_dm_act_get_rf_disc_duration() {
1311   return (nfa_dm_cb.disc_cb.disc_duration);
1312 }
1313 /*******************************************************************************
1314 **
1315 ** Function         nfa_dm_act_select
1316 **
1317 ** Description      Process RF select command
1318 **
1319 ** Returns          TRUE (message buffer to be freed by caller)
1320 **
1321 *******************************************************************************/
nfa_dm_act_select(tNFA_DM_MSG * p_data)1322 bool nfa_dm_act_select(tNFA_DM_MSG* p_data) {
1323   DLOG_IF(INFO, nfc_debug_enabled) << __func__;
1324 
1325   nfa_dm_rf_discover_select(p_data->select.rf_disc_id, p_data->select.protocol,
1326                             p_data->select.rf_interface);
1327   return true;
1328 }
1329 
1330 /*******************************************************************************
1331 **
1332 ** Function         nfa_dm_act_update_rf_params
1333 **
1334 ** Description      Process update RF communication parameters command
1335 **
1336 ** Returns          TRUE (message buffer to be freed by caller)
1337 **
1338 *******************************************************************************/
nfa_dm_act_update_rf_params(tNFA_DM_MSG * p_data)1339 bool nfa_dm_act_update_rf_params(tNFA_DM_MSG* p_data) {
1340   tNFA_CONN_EVT_DATA conn_evt;
1341 
1342   DLOG_IF(INFO, nfc_debug_enabled) << __func__;
1343 
1344   if (NFC_UpdateRFCommParams(&p_data->update_rf_params.params) !=
1345       NFC_STATUS_OK) {
1346     conn_evt.status = NFA_STATUS_FAILED;
1347     nfa_dm_conn_cback_event_notify(NFA_UPDATE_RF_PARAM_RESULT_EVT, &conn_evt);
1348   }
1349 
1350   return true;
1351 }
1352 
1353 /*******************************************************************************
1354 **
1355 ** Function         nfa_dm_act_disable_timeout
1356 **
1357 ** Description      timeout on disable process. Shutdown immediately
1358 **
1359 ** Returns          TRUE (message buffer to be freed by caller)
1360 **
1361 *******************************************************************************/
nfa_dm_act_disable_timeout(tNFA_DM_MSG * p_data)1362 bool nfa_dm_act_disable_timeout(__attribute__((unused)) tNFA_DM_MSG* p_data) {
1363   tNFA_DM_MSG nfa_dm_msg;
1364   nfa_dm_msg.disable.graceful = false;
1365   nfa_dm_disable(&nfa_dm_msg);
1366   return true;
1367 }
1368 
1369 /*******************************************************************************
1370 **
1371 ** Function         nfa_dm_act_conn_cback_notify
1372 **
1373 ** Description      Notify app of reader/writer/ndef events
1374 **
1375 ** Returns          nothing
1376 **
1377 *******************************************************************************/
nfa_dm_act_conn_cback_notify(uint8_t event,tNFA_CONN_EVT_DATA * p_data)1378 void nfa_dm_act_conn_cback_notify(uint8_t event, tNFA_CONN_EVT_DATA* p_data) {
1379   DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("event:0x%X", event);
1380 
1381   /* Notify event using appropriate CONN_CBACK */
1382   nfa_dm_conn_cback_event_notify(event, p_data);
1383 
1384   /* If not in exclusive RF mode, then read NDEF message from tag (if automatic
1385    * reading is enabled) */
1386   if (!(nfa_dm_cb.flags & NFA_DM_FLAGS_EXCL_RF_ACTIVE)) {
1387     if ((event == NFA_NDEF_DETECT_EVT) &&
1388         (nfa_dm_cb.flags & NFA_DM_FLAGS_AUTO_READING_NDEF)) {
1389       /* read NDEF message from tag */
1390       if (p_data->ndef_detect.status == NFA_STATUS_OK) {
1391         NFA_RwReadNDef();
1392       } else if (p_data->ndef_detect.status == NFA_STATUS_FAILED) {
1393         nfa_dm_cb.flags &= ~NFA_DM_FLAGS_AUTO_READING_NDEF;
1394       }
1395       /* ignore NFA_STATUS_BUSY */
1396     } else if ((event == NFA_READ_CPLT_EVT) &&
1397                (nfa_dm_cb.flags & NFA_DM_FLAGS_AUTO_READING_NDEF)) {
1398       /* reading NDEF message is done */
1399       nfa_dm_cb.flags &= ~NFA_DM_FLAGS_AUTO_READING_NDEF;
1400     }
1401   }
1402 }
1403 
1404 /*******************************************************************************
1405 **
1406 ** Function         nfa_dm_act_data_cback
1407 **
1408 ** Description      Processing data from RF link
1409 **
1410 ** Returns          None
1411 **
1412 *******************************************************************************/
nfa_dm_act_data_cback(uint8_t conn_id,tNFC_CONN_EVT event,tNFC_CONN * p_data)1413 static void nfa_dm_act_data_cback(__attribute__((unused)) uint8_t conn_id,
1414                                   tNFC_CONN_EVT event, tNFC_CONN* p_data) {
1415   NFC_HDR* p_msg;
1416   tNFA_CONN_EVT_DATA evt_data;
1417 
1418   DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("event = 0x%X", event);
1419 
1420   if (event == NFC_DATA_CEVT) {
1421     p_msg = (NFC_HDR*)p_data->data.p_data;
1422 
1423     if (p_msg) {
1424       evt_data.data.status = p_data->data.status;
1425       evt_data.data.p_data = (uint8_t*)(p_msg + 1) + p_msg->offset;
1426       evt_data.data.len = p_msg->len;
1427 
1428       nfa_dm_conn_cback_event_notify(NFA_DATA_EVT, &evt_data);
1429 
1430       GKI_freebuf(p_msg);
1431     } else {
1432       LOG(ERROR) << StringPrintf(
1433           "received NFC_DATA_CEVT with NULL data "
1434           "pointer");
1435     }
1436   } else if (event == NFC_DEACTIVATE_CEVT) {
1437     NFC_SetStaticRfCback(nullptr);
1438   }
1439 }
1440 
1441 /*******************************************************************************
1442 **
1443 ** Function         nfa_dm_excl_disc_cback
1444 **
1445 ** Description      Processing event from discovery callback
1446 **
1447 ** Returns          None
1448 **
1449 *******************************************************************************/
nfa_dm_excl_disc_cback(tNFA_DM_RF_DISC_EVT event,tNFC_DISCOVER * p_data)1450 static void nfa_dm_excl_disc_cback(tNFA_DM_RF_DISC_EVT event,
1451                                    tNFC_DISCOVER* p_data) {
1452   tNFA_CONN_EVT_DATA evt_data;
1453 
1454   DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("event:0x%02X", event);
1455 
1456   switch (event) {
1457     case NFA_DM_RF_DISC_START_EVT:
1458       evt_data.status = NFA_STATUS_OK;
1459       nfa_dm_conn_cback_event_notify(NFA_EXCLUSIVE_RF_CONTROL_STARTED_EVT,
1460                                      &evt_data);
1461       break;
1462 
1463     case NFA_DM_RF_DISC_ACTIVATED_EVT:
1464       if (nfa_dm_cb.disc_cb.activated_tech_mode == NFC_DISCOVERY_TYPE_POLL_A) {
1465         /* store SEL_RES response */
1466         nfa_dm_cb.disc_cb.activated_sel_res =
1467             p_data->activate.rf_tech_param.param.pa.sel_rsp;
1468       }
1469 
1470       if (nfa_dm_cb.disc_cb.disc_state == NFA_DM_RFST_LISTEN_ACTIVE) {
1471         /* Set data callback to receive raw frame */
1472         NFC_SetStaticRfCback(nfa_dm_act_data_cback);
1473 
1474         memset(&(evt_data.activated.params), 0x00, sizeof(tNFA_TAG_PARAMS));
1475         memcpy(&(evt_data.activated.activate_ntf), &(p_data->activate),
1476                sizeof(tNFC_ACTIVATE_DEVT));
1477 
1478         nfa_dm_conn_cback_event_notify(NFA_ACTIVATED_EVT, &evt_data);
1479       } else {
1480         /* holding activation notification until sub-module is ready */
1481         nfa_dm_cb.p_activate_ntf =
1482             (uint8_t*)GKI_getbuf(sizeof(tNFC_ACTIVATE_DEVT));
1483 
1484         if (nfa_dm_cb.p_activate_ntf) {
1485           memcpy(nfa_dm_cb.p_activate_ntf, &(p_data->activate),
1486                  sizeof(tNFC_ACTIVATE_DEVT));
1487 
1488           if ((nfa_dm_cb.disc_cb.activated_protocol == NFC_PROTOCOL_T1T) ||
1489               (nfa_dm_cb.disc_cb.activated_protocol == NFC_PROTOCOL_T2T) ||
1490               (nfa_dm_cb.disc_cb.activated_protocol == NFC_PROTOCOL_T3T) ||
1491               (nfa_dm_cb.disc_cb.activated_protocol == NFC_PROTOCOL_ISO_DEP) ||
1492               (nfa_dm_cb.disc_cb.activated_protocol == NFA_PROTOCOL_T5T) ||
1493               (nfa_dm_cb.disc_cb.activated_protocol == NFC_PROTOCOL_KOVIO) ||
1494               (nfa_dm_cb.disc_cb.activated_protocol == NFC_PROTOCOL_MIFARE)) {
1495             /* Notify NFA tag sub-system */
1496             nfa_rw_proc_disc_evt(NFA_DM_RF_DISC_ACTIVATED_EVT, p_data, false);
1497           } else /* if NFC-DEP, ISO-DEP with frame interface or others */
1498           {
1499             /* Set data callback to receive raw frame */
1500             NFC_SetStaticRfCback(nfa_dm_act_data_cback);
1501             nfa_dm_notify_activation_status(NFA_STATUS_OK, nullptr);
1502           }
1503         } else {
1504           /* deactivate and restart RF discovery */
1505           nfa_dm_rf_deactivate(NFA_DEACTIVATE_TYPE_DISCOVERY);
1506         }
1507       }
1508       break;
1509 
1510     case NFA_DM_RF_DISC_DEACTIVATED_EVT:
1511 
1512       /* if deactivated to idle or discovery */
1513       if ((p_data->deactivate.type == NFC_DEACTIVATE_TYPE_IDLE) ||
1514           (p_data->deactivate.type == NFC_DEACTIVATE_TYPE_DISCOVERY)) {
1515         /* clear stored NFCID/UID/KOVIO bar code */
1516         nfa_dm_cb.activated_nfcid_len = 0;
1517       }
1518 
1519       if (nfa_dm_cb.disc_cb.activated_protocol != NFC_PROTOCOL_NFC_DEP) {
1520         /* Notify NFA RW sub-systems */
1521         nfa_rw_proc_disc_evt(NFA_DM_RF_DISC_DEACTIVATED_EVT, nullptr, false);
1522       }
1523 
1524       /* if deactivated as sleep mode */
1525       if ((p_data->deactivate.type == NFC_DEACTIVATE_TYPE_SLEEP) ||
1526           (p_data->deactivate.type == NFC_DEACTIVATE_TYPE_SLEEP_AF)) {
1527         evt_data.deactivated.type = NFA_DEACTIVATE_TYPE_SLEEP;
1528       } else {
1529         evt_data.deactivated.type = NFA_DEACTIVATE_TYPE_IDLE;
1530       }
1531 
1532       /* notify deactivation to upper layer */
1533       nfa_dm_conn_cback_event_notify(NFA_DEACTIVATED_EVT, &evt_data);
1534 
1535       /* clean up SEL_RES response */
1536       nfa_dm_cb.disc_cb.activated_sel_res = 0;
1537       break;
1538 
1539     default:
1540       LOG(ERROR) << StringPrintf("Unexpected event");
1541       break;
1542   }
1543 }
1544 
1545 /*******************************************************************************
1546 **
1547 ** Function         nfa_dm_poll_disc_cback
1548 **
1549 ** Description      Processing event from discovery callback
1550 **
1551 ** Returns          None
1552 **
1553 *******************************************************************************/
nfa_dm_poll_disc_cback(tNFA_DM_RF_DISC_EVT event,tNFC_DISCOVER * p_data)1554 static void nfa_dm_poll_disc_cback(tNFA_DM_RF_DISC_EVT event,
1555                                    tNFC_DISCOVER* p_data) {
1556   tNFA_CONN_EVT_DATA evt_data;
1557 
1558   DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("event:0x%02X", event);
1559 
1560   switch (event) {
1561     case NFA_DM_RF_DISC_START_EVT:
1562       break;
1563 
1564     case NFA_DM_RF_DISC_ACTIVATED_EVT:
1565 
1566       if (nfa_dm_cb.disc_cb.activated_tech_mode == NFC_DISCOVERY_TYPE_POLL_A) {
1567         /* store SEL_RES response */
1568         nfa_dm_cb.disc_cb.activated_sel_res =
1569             p_data->activate.rf_tech_param.param.pa.sel_rsp;
1570       }
1571 
1572       /* holding activation notification until sub-module is ready */
1573       nfa_dm_cb.p_activate_ntf =
1574           (uint8_t*)GKI_getbuf(sizeof(tNFC_ACTIVATE_DEVT));
1575 
1576       if (nfa_dm_cb.p_activate_ntf) {
1577         memcpy(nfa_dm_cb.p_activate_ntf, &(p_data->activate),
1578                sizeof(tNFC_ACTIVATE_DEVT));
1579         if ((nfa_dm_cb.disc_cb.activated_protocol == NFC_PROTOCOL_NFC_DEP) &&
1580             (nfa_dm_cb.disc_cb.activated_rf_interface ==
1581              NFC_INTERFACE_NFC_DEP)) {
1582           /* For P2P mode(Default DTA mode) open Raw channel to bypass LLCP
1583            * layer. For LLCP DTA mode activate LLCP */
1584           if ((appl_dta_mode_flag == 1) &&
1585               ((nfa_dm_cb.eDtaMode & 0x0F) == NFA_DTA_DEFAULT_MODE)) {
1586             /* Open raw channel in case of p2p for DTA testing */
1587             NFC_SetStaticRfCback(nfa_dm_act_data_cback);
1588             nfa_dm_notify_activation_status(NFA_STATUS_OK, nullptr);
1589           } else {
1590             if (!(nfa_dm_cb.flags & NFA_DM_FLAGS_P2P_PAUSED)) {
1591               /* activate LLCP */
1592               nfa_p2p_activate_llcp(p_data);
1593               if (nfa_dm_cb.p_activate_ntf) {
1594                 GKI_freebuf(nfa_dm_cb.p_activate_ntf);
1595                 nfa_dm_cb.p_activate_ntf = nullptr;
1596               }
1597             } else {
1598               DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("P2P is paused");
1599               nfa_dm_notify_activation_status(NFA_STATUS_OK, nullptr);
1600             }
1601           }
1602         } else if ((nfa_dm_cb.disc_cb.activated_protocol == NFC_PROTOCOL_T1T) ||
1603                    (nfa_dm_cb.disc_cb.activated_protocol == NFC_PROTOCOL_T2T) ||
1604                    (nfa_dm_cb.disc_cb.activated_protocol == NFC_PROTOCOL_T3T) ||
1605                    (nfa_dm_cb.disc_cb.activated_protocol ==
1606                     NFC_PROTOCOL_ISO_DEP) ||
1607                    (nfa_dm_cb.disc_cb.activated_protocol == NFC_PROTOCOL_T5T) ||
1608                    (nfa_dm_cb.disc_cb.activated_protocol ==
1609                     NFC_PROTOCOL_KOVIO) ||
1610                    (nfa_dm_cb.disc_cb.activated_protocol ==
1611                     NFC_PROTOCOL_MIFARE)) {
1612           /* Notify NFA tag sub-system */
1613           nfa_rw_proc_disc_evt(NFA_DM_RF_DISC_ACTIVATED_EVT, p_data, true);
1614         } else /* if NFC-DEP/ISO-DEP with frame interface */
1615         {
1616           /* Set data callback to receive raw frame */
1617           NFC_SetStaticRfCback(nfa_dm_act_data_cback);
1618           nfa_dm_notify_activation_status(NFA_STATUS_OK, nullptr);
1619         }
1620       } else {
1621         /* deactivate and restart RF discovery */
1622         nfa_dm_rf_deactivate(NFA_DEACTIVATE_TYPE_DISCOVERY);
1623       }
1624       break;
1625 
1626     case NFA_DM_RF_DISC_DEACTIVATED_EVT:
1627 
1628       /* if deactivated to idle or discovery */
1629       if ((p_data->deactivate.type == NFC_DEACTIVATE_TYPE_IDLE) ||
1630           (p_data->deactivate.type == NFC_DEACTIVATE_TYPE_DISCOVERY)) {
1631         /* clear stored NFCID/UID/KOVIO bar code */
1632         nfa_dm_cb.activated_nfcid_len = 0;
1633       }
1634 
1635       if ((nfa_dm_cb.disc_cb.activated_protocol == NFC_PROTOCOL_NFC_DEP) &&
1636           (nfa_dm_cb.disc_cb.activated_rf_interface == NFC_INTERFACE_NFC_DEP)) {
1637         /*
1638         ** If LLCP link is not deactivated yet,
1639         ** LLCP will receive deactivation ntf through data callback.
1640         ** NFA P2P will receive callback event from LLCP.
1641         */
1642       } else {
1643         /* Notify NFA RW sub-systems */
1644         nfa_rw_proc_disc_evt(NFA_DM_RF_DISC_DEACTIVATED_EVT, nullptr, true);
1645       }
1646 
1647       /* if NFA sent NFA_ACTIVATED_EVT earlier */
1648       if (nfa_dm_cb.flags & NFA_DM_FLAGS_SEND_DEACTIVATED_EVT) {
1649         nfa_dm_cb.flags &= ~NFA_DM_FLAGS_SEND_DEACTIVATED_EVT;
1650 
1651         /* if deactivated as sleep mode */
1652         if ((p_data->deactivate.type == NFC_DEACTIVATE_TYPE_SLEEP) ||
1653             (p_data->deactivate.type == NFC_DEACTIVATE_TYPE_SLEEP_AF)) {
1654           evt_data.deactivated.type = NFA_DEACTIVATE_TYPE_SLEEP;
1655         } else {
1656           evt_data.deactivated.type = NFA_DEACTIVATE_TYPE_IDLE;
1657         }
1658         /* notify deactivation to application */
1659         nfa_dm_conn_cback_event_notify(NFA_DEACTIVATED_EVT, &evt_data);
1660       }
1661 
1662       /* clean up SEL_RES response */
1663       nfa_dm_cb.disc_cb.activated_sel_res = 0;
1664 
1665       if (!(nfa_dm_cb.flags & NFA_DM_FLAGS_POLLING_ENABLED)) {
1666         /* deregister discovery callback from NFA DM Discovery */
1667         nfa_dm_delete_rf_discover(nfa_dm_cb.poll_disc_handle);
1668         nfa_dm_cb.poll_disc_handle = NFA_HANDLE_INVALID;
1669 
1670         /* this is for disable polling */
1671         if (nfa_dm_cb.flags & NFA_DM_FLAGS_SEND_POLL_STOP_EVT) {
1672           nfa_dm_cb.flags &= ~NFA_DM_FLAGS_SEND_POLL_STOP_EVT;
1673 
1674           evt_data.status = NFA_STATUS_OK;
1675           nfa_dm_conn_cback_event_notify(NFA_POLL_DISABLED_EVT, &evt_data);
1676         }
1677       }
1678       break;
1679   }
1680 }
1681 
1682 /*******************************************************************************
1683 ** Function         nfa_dm_poll_disc_cback_dta_wrapper
1684 **
1685 ** Description      Accessing the nfa_dm_poll_disc_cback for DTA wrapper
1686 **
1687 ** Returns          None
1688 **
1689 *******************************************************************************/
nfa_dm_poll_disc_cback_dta_wrapper(tNFA_DM_RF_DISC_EVT event,tNFC_DISCOVER * p_data)1690 void nfa_dm_poll_disc_cback_dta_wrapper(tNFA_DM_RF_DISC_EVT event,
1691                                         tNFC_DISCOVER* p_data) {
1692   nfa_dm_poll_disc_cback(event, p_data);
1693 }
1694 
1695 /*******************************************************************************
1696 **
1697 ** Function         nfa_dm_notify_activation_status
1698 **
1699 ** Description      Processing activation status from sub-modules
1700 **
1701 ** Returns          None
1702 **
1703 *******************************************************************************/
nfa_dm_notify_activation_status(tNFA_STATUS status,tNFA_TAG_PARAMS * p_params)1704 void nfa_dm_notify_activation_status(tNFA_STATUS status,
1705                                      tNFA_TAG_PARAMS* p_params) {
1706   tNFA_CONN_EVT_DATA evt_data;
1707   tNFC_RF_TECH_PARAMS* p_tech_params;
1708   uint8_t *p_nfcid = nullptr, nfcid_len;
1709 
1710   DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("status:0x%X", status);
1711 
1712   if (!nfa_dm_cb.p_activate_ntf) {
1713     /* this is for NFA P2P listen */
1714     return;
1715   }
1716 
1717   if (status == NFA_STATUS_OK) {
1718     /* notify NFC link activation */
1719     memcpy(&(evt_data.activated.activate_ntf), nfa_dm_cb.p_activate_ntf,
1720            sizeof(tNFC_ACTIVATE_DEVT));
1721 
1722     p_tech_params = &evt_data.activated.activate_ntf.rf_tech_param;
1723 
1724     memset(&(evt_data.activated.params), 0x00, sizeof(tNFA_TAG_PARAMS));
1725     if (p_params) {
1726       memcpy(&(evt_data.activated.params), p_params, sizeof(tNFA_TAG_PARAMS));
1727     }
1728 
1729     /* get length of NFCID and location */
1730     if (p_tech_params->mode == NFC_DISCOVERY_TYPE_POLL_A) {
1731       if ((p_tech_params->param.pa.nfcid1_len == 0) && (p_params != nullptr)) {
1732         nfcid_len = sizeof(p_params->t1t.uid);
1733         p_nfcid = p_params->t1t.uid;
1734         evt_data.activated.activate_ntf.rf_tech_param.param.pa.nfcid1_len =
1735             nfcid_len;
1736         memcpy(evt_data.activated.activate_ntf.rf_tech_param.param.pa.nfcid1,
1737                p_nfcid, nfcid_len);
1738       } else {
1739         nfcid_len = p_tech_params->param.pa.nfcid1_len;
1740         p_nfcid = p_tech_params->param.pa.nfcid1;
1741       }
1742     } else if (p_tech_params->mode == NFC_DISCOVERY_TYPE_POLL_B) {
1743       nfcid_len = NFC_NFCID0_MAX_LEN;
1744       p_nfcid = p_tech_params->param.pb.nfcid0;
1745     } else if (p_tech_params->mode == NFC_DISCOVERY_TYPE_POLL_F) {
1746       nfcid_len = NFC_NFCID2_LEN;
1747       p_nfcid = p_tech_params->param.pf.nfcid2;
1748     } else if (p_tech_params->mode == NFC_DISCOVERY_TYPE_POLL_V) {
1749       nfcid_len = NFC_ISO15693_UID_LEN;
1750       p_nfcid = p_tech_params->param.pi93.uid;
1751     } else if (p_tech_params->mode == NFC_DISCOVERY_TYPE_POLL_KOVIO) {
1752       nfcid_len = p_tech_params->param.pk.uid_len;
1753       p_nfcid = p_tech_params->param.pk.uid;
1754     } else {
1755       nfcid_len = 0;
1756     }
1757 
1758     /*
1759     ** If not in exlusive RF mode, and
1760     **      P2P activation, then push default NDEF message through SNEP
1761     **      TAG activation, then read NDEF message
1762     */
1763     if (nfa_dm_cb.disc_cb.activated_protocol == NFC_PROTOCOL_NFC_DEP) {
1764       /*
1765       ** Default NDEF message will be put to NFC Forum defualt SNEP server
1766       ** after receiving NFA_LLCP_ACTIVATED_EVT.
1767       */
1768     } else if (!(nfa_dm_cb.flags & NFA_DM_FLAGS_EXCL_RF_ACTIVE)) {
1769       /*
1770       ** if the same tag is activated then do not perform auto NDEF
1771       ** detection. Application may put a tag into sleep mode and
1772       ** reactivate the same tag.
1773       */
1774 
1775       if ((p_tech_params->mode != nfa_dm_cb.activated_tech_mode) ||
1776           (nfcid_len != nfa_dm_cb.activated_nfcid_len) ||
1777           (memcmp(p_nfcid, nfa_dm_cb.activated_nfcid, nfcid_len))) {
1778         if ((nfa_dm_cb.disc_cb.activated_protocol == NFC_PROTOCOL_T1T) ||
1779             (nfa_dm_cb.disc_cb.activated_protocol == NFC_PROTOCOL_T2T) ||
1780             (nfa_dm_cb.disc_cb.activated_protocol == NFC_PROTOCOL_T3T) ||
1781             ((nfa_dm_cb.disc_cb.activated_protocol == NFC_PROTOCOL_ISO_DEP) &&
1782              (nfa_dm_cb.disc_cb.activated_rf_interface ==
1783               NFC_INTERFACE_ISO_DEP)) ||
1784             (nfa_dm_cb.disc_cb.activated_protocol == NFA_PROTOCOL_T5T)) {
1785           if (p_nfa_dm_cfg->auto_detect_ndef) {
1786             if (p_nfa_dm_cfg->auto_read_ndef) {
1787               nfa_dm_cb.flags |= NFA_DM_FLAGS_AUTO_READING_NDEF;
1788             }
1789             NFA_RwDetectNDef();
1790           } else if (p_nfa_dm_cfg->auto_read_ndef) {
1791             NFA_RwReadNDef();
1792           }
1793         }
1794       }
1795     }
1796 
1797     /* store activated tag information */
1798     nfa_dm_cb.activated_tech_mode = p_tech_params->mode;
1799     nfa_dm_cb.activated_nfcid_len = nfcid_len;
1800     if (nfcid_len) memcpy(nfa_dm_cb.activated_nfcid, p_nfcid, nfcid_len);
1801 
1802     nfa_dm_cb.flags |= NFA_DM_FLAGS_SEND_DEACTIVATED_EVT;
1803     if (!(nfa_dm_cb.disc_cb.disc_flags & NFA_DM_DISC_FLAGS_CHECKING))
1804       nfa_dm_conn_cback_event_notify(NFA_ACTIVATED_EVT, &evt_data);
1805   } else {
1806     /* if NFC_DEP, NFA P2P will deactivate */
1807     if (nfa_dm_cb.disc_cb.activated_protocol != NFC_PROTOCOL_NFC_DEP) {
1808       nfa_dm_rf_deactivate(NFA_DEACTIVATE_TYPE_DISCOVERY);
1809     }
1810   }
1811 
1812   GKI_freebuf(nfa_dm_cb.p_activate_ntf);
1813   nfa_dm_cb.p_activate_ntf = nullptr;
1814 }
1815 
1816 /*******************************************************************************
1817 **
1818 ** Function         nfa_dm_nfc_revt_2_str
1819 **
1820 ** Description      convert nfc revt to string
1821 **
1822 *******************************************************************************/
nfa_dm_nfc_revt_2_str(tNFC_RESPONSE_EVT event)1823 std::string nfa_dm_nfc_revt_2_str(tNFC_RESPONSE_EVT event) {
1824   switch (event) {
1825     case NFC_ENABLE_REVT:
1826       return "NFC_ENABLE_REVT";
1827     case NFC_DISABLE_REVT:
1828       return "NFC_DISABLE_REVT";
1829     case NFC_SET_CONFIG_REVT:
1830       return "NFC_SET_CONFIG_REVT";
1831     case NFC_GET_CONFIG_REVT:
1832       return "NFC_GET_CONFIG_REVT";
1833     case NFC_NFCEE_DISCOVER_REVT:
1834       return "NFC_NFCEE_DISCOVER_REVT";
1835     case NFC_NFCEE_INFO_REVT:
1836       return "NFC_NFCEE_INFO_REVT";
1837     case NFC_NFCEE_MODE_SET_REVT:
1838       return "NFC_NFCEE_MODE_SET_REVT";
1839     case NFC_RF_FIELD_REVT:
1840       return "NFC_RF_FIELD_REVT";
1841     case NFC_EE_ACTION_REVT:
1842       return "NFC_EE_ACTION_REVT";
1843     case NFC_EE_DISCOVER_REQ_REVT:
1844       return "NFC_EE_DISCOVER_REQ_REVT";
1845     case NFC_SET_ROUTING_REVT:
1846       return "NFC_SET_ROUTING_REVT";
1847     case NFC_GET_ROUTING_REVT:
1848       return "NFC_GET_ROUTING_REVT";
1849     case NFC_GEN_ERROR_REVT:
1850       return "NFC_GEN_ERROR_REVT";
1851     case NFC_NFCC_RESTART_REVT:
1852       return "NFC_NFCC_RESTART_REVT";
1853     case NFC_NFCC_TIMEOUT_REVT:
1854       return "NFC_NFCC_TIMEOUT_REVT";
1855     case NFC_NFCC_TRANSPORT_ERR_REVT:
1856       return "NFC_NFCC_TRANSPORT_ERR_REVT";
1857     case NFC_NFCC_POWER_OFF_REVT:
1858       return "NFC_NFCC_POWER_OFF_REVT";
1859     case NFC_NFCEE_STATUS_REVT:
1860       return "NFC_NFCEE_STATUS_REVT";
1861     default:
1862       return "unknown revt";
1863   }
1864 }
1865