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