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