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