xref: /aosp_15_r20/system/nfc/src/nfa/ce/nfa_ce_main.cc (revision 7eba2f3b06c51ae21384f6a4f14577b668a869b3)
1 /******************************************************************************
2  *
3  *  Copyright (C) 2011-2014 Broadcom Corporation
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 /******************************************************************************
20  *
21  *  This is the main implementation file for the NFA_CE
22  *
23  ******************************************************************************/
24 #include <android-base/logging.h>
25 #include <android-base/stringprintf.h>
26 #include <string>
27 
28 #include "nfa_ce_api.h"
29 #include "nfa_ce_int.h"
30 
31 using android::base::StringPrintf;
32 
33 /* NFA_CE control block */
34 tNFA_CE_CB nfa_ce_cb;
35 
36 /*****************************************************************************
37 ** Constants and types
38 *****************************************************************************/
39 #define NFA_CE_DEFAULT_ISODEP_DISC_MASK \
40   (NFA_DM_DISC_MASK_LA_ISO_DEP | NFA_DM_DISC_MASK_LB_ISO_DEP)
41 static void nfa_ce_proc_nfcc_power_mode(uint8_t nfcc_power_mode);
42 
43 static const tNFA_SYS_REG nfa_ce_sys_reg = {
44     nullptr, nfa_ce_hdl_event, nfa_ce_sys_disable, nfa_ce_proc_nfcc_power_mode};
45 
46 /* NFA_CE actions */
47 const tNFA_CE_ACTION nfa_ce_action_tbl[] = {
48     nfa_ce_api_cfg_local_tag,   /* NFA_CE_API_CFG_LOCAL_TAG_EVT */
49     nfa_ce_api_reg_listen,      /* NFA_CE_API_REG_LISTEN_EVT    */
50     nfa_ce_api_dereg_listen,    /* NFA_CE_API_DEREG_LISTEN_EVT  */
51     nfa_ce_api_cfg_isodep_tech, /* NFA_CE_API_CFG_ISODEP_TECH_EVT*/
52     nfa_ce_activate_ntf,        /* NFA_CE_ACTIVATE_NTF_EVT      */
53     nfa_ce_deactivate_ntf,      /* NFA_CE_DEACTIVATE_NTF_EVT    */
54 };
55 #define NFA_CE_ACTION_TBL_SIZE \
56   (sizeof(nfa_ce_action_tbl) / sizeof(tNFA_CE_ACTION))
57 
58 /*****************************************************************************
59 ** Local function prototypes
60 *****************************************************************************/
61 static std::string nfa_ce_evt_2_str(uint16_t event);
62 
63 /*******************************************************************************
64 **
65 ** Function         nfa_ce_init
66 **
67 ** Description      Initialize NFA CE
68 **
69 ** Returns          None
70 **
71 *******************************************************************************/
nfa_ce_init(void)72 void nfa_ce_init(void) {
73   LOG(VERBOSE) << __func__;
74 
75   /* initialize control block */
76   memset(&nfa_ce_cb, 0, sizeof(tNFA_CE_CB));
77 
78   /* Generate a random NFCID for Type-3 NDEF emulation (Type-3 tag NFCID2 must
79    * start with 02:FE) */
80   nfa_ce_t3t_generate_rand_nfcid(
81       nfa_ce_cb.listen_info[NFA_CE_LISTEN_INFO_IDX_NDEF].t3t_nfcid2);
82   nfa_ce_cb.listen_info[NFA_CE_LISTEN_INFO_IDX_NDEF].rf_disc_handle =
83       NFA_HANDLE_INVALID;
84   nfa_ce_cb.isodep_disc_mask = NFA_CE_DEFAULT_ISODEP_DISC_MASK;
85   nfa_ce_cb.idx_wild_card = NFA_CE_LISTEN_INFO_IDX_INVALID;
86 
87   /* register message handler on NFA SYS */
88   nfa_sys_register(NFA_ID_CE, &nfa_ce_sys_reg);
89 }
90 
91 /*******************************************************************************
92 **
93 ** Function         nfa_ce_sys_disable
94 **
95 ** Description      Clean up ce sub-system
96 **
97 **
98 ** Returns          void
99 **
100 *******************************************************************************/
nfa_ce_sys_disable(void)101 void nfa_ce_sys_disable(void) {
102   tNFA_CE_LISTEN_INFO* p_info;
103   uint8_t xx;
104 
105   NFC_SetStaticRfCback(nullptr);
106 
107   /* Free scratch buf if any */
108   nfa_ce_free_scratch_buf();
109 
110   /* Delete discovery handles */
111   for (xx = 0, p_info = nfa_ce_cb.listen_info; xx < NFA_CE_LISTEN_INFO_MAX;
112        xx++, p_info++) {
113     if ((p_info->flags & NFA_CE_LISTEN_INFO_IN_USE) &&
114         (p_info->rf_disc_handle != NFA_HANDLE_INVALID)) {
115       nfa_dm_delete_rf_discover(p_info->rf_disc_handle);
116       p_info->rf_disc_handle = NFA_HANDLE_INVALID;
117     }
118   }
119 
120   nfa_sys_deregister(NFA_ID_CE);
121 }
122 
123 /*******************************************************************************
124 **
125 ** Function         nfa_ce_proc_nfcc_power_mode
126 **
127 ** Description      Processing NFCC power mode changes
128 **
129 ** Returns          None
130 **
131 *******************************************************************************/
nfa_ce_proc_nfcc_power_mode(uint8_t nfcc_power_mode)132 static void nfa_ce_proc_nfcc_power_mode(uint8_t nfcc_power_mode) {
133   tNFA_CE_CB* p_cb = &nfa_ce_cb;
134   uint8_t listen_info_idx;
135 
136   LOG(VERBOSE) << StringPrintf("nfcc_power_mode=%d", nfcc_power_mode);
137 
138   /* if NFCC power mode is change to full power */
139   if (nfcc_power_mode == NFA_DM_PWR_MODE_FULL) {
140     nfa_ce_restart_listen_check();
141   } else {
142     for (listen_info_idx = 0; listen_info_idx < NFA_CE_LISTEN_INFO_IDX_INVALID;
143          listen_info_idx++) {
144       /* add RF discovery to DM only if it is not added yet */
145       if ((p_cb->listen_info[listen_info_idx].flags &
146            NFA_CE_LISTEN_INFO_IN_USE) &&
147           (p_cb->listen_info[listen_info_idx].rf_disc_handle !=
148            NFA_HANDLE_INVALID)) {
149         nfa_dm_delete_rf_discover(
150             p_cb->listen_info[listen_info_idx].rf_disc_handle);
151         p_cb->listen_info[listen_info_idx].rf_disc_handle = NFA_HANDLE_INVALID;
152       }
153     }
154   }
155 
156   nfa_sys_cback_notify_nfcc_power_mode_proc_complete(NFA_ID_CE);
157 }
158 
159 /*******************************************************************************
160 **
161 ** Function         nfa_ce_hdl_event
162 **
163 ** Description      nfa rw main event handling function.
164 **
165 ** Returns          bool
166 **
167 *******************************************************************************/
nfa_ce_hdl_event(NFC_HDR * p_msg)168 bool nfa_ce_hdl_event(NFC_HDR* p_msg) {
169   uint16_t act_idx;
170   bool freebuf = true;
171 
172   LOG(VERBOSE) << StringPrintf(
173       "nfa_ce_handle_event event: %s (0x%02x), flags: %08x",
174       nfa_ce_evt_2_str(p_msg->event).c_str(), p_msg->event, nfa_ce_cb.flags);
175 
176   /* Get NFA_RW sub-event */
177   act_idx = (p_msg->event & 0x00FF);
178   if (act_idx < NFA_CE_ACTION_TBL_SIZE) {
179     freebuf = (*nfa_ce_action_tbl[act_idx])((tNFA_CE_MSG*)p_msg);
180   }
181 
182   /* if vendor specific event handler is registered */
183   if (nfa_ce_cb.p_vs_evt_hdlr) {
184     (*nfa_ce_cb.p_vs_evt_hdlr)(p_msg);
185   }
186 
187   return freebuf;
188 }
189 
190 /*******************************************************************************
191 **
192 ** Function         nfa_ce_evt_2_str
193 **
194 ** Description      convert nfc evt to string
195 **
196 *******************************************************************************/
nfa_ce_evt_2_str(uint16_t event)197 static std::string nfa_ce_evt_2_str(uint16_t event) {
198   switch (event) {
199     case NFA_CE_API_CFG_LOCAL_TAG_EVT:
200       return "NFA_CE_API_CFG_LOCAL_TAG_EVT";
201     case NFA_CE_API_REG_LISTEN_EVT:
202       return "NFA_CE_API_REG_LISTEN_EVT";
203     case NFA_CE_API_DEREG_LISTEN_EVT:
204       return "NFA_CE_API_DEREG_LISTEN_EVT";
205     case NFA_CE_API_CFG_ISODEP_TECH_EVT:
206       return "NFA_CE_API_CFG_ISODEP_TECH_EVT";
207     case NFA_CE_ACTIVATE_NTF_EVT:
208       return "NFA_CE_ACTIVATE_NTF_EVT";
209     case NFA_CE_DEACTIVATE_NTF_EVT:
210       return "NFA_CE_DEACTIVATE_NTF_EVT";
211     default:
212       return "Unknown";
213   }
214 }
215