• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <string>
25 
26 #include <android-base/stringprintf.h>
27 #include <base/logging.h>
28 
29 #include "nfa_ce_api.h"
30 #include "nfa_ce_int.h"
31 
32 using android::base::StringPrintf;
33 
34 extern bool nfc_debug_enabled;
35 
36 /* NFA_CE control block */
37 tNFA_CE_CB nfa_ce_cb;
38 
39 /*****************************************************************************
40 ** Constants and types
41 *****************************************************************************/
42 #define NFA_CE_DEFAULT_ISODEP_DISC_MASK \
43   (NFA_DM_DISC_MASK_LA_ISO_DEP | NFA_DM_DISC_MASK_LB_ISO_DEP)
44 static void nfa_ce_proc_nfcc_power_mode(uint8_t nfcc_power_mode);
45 
46 static const tNFA_SYS_REG nfa_ce_sys_reg = {
47     nullptr, nfa_ce_hdl_event, nfa_ce_sys_disable, nfa_ce_proc_nfcc_power_mode};
48 
49 /* NFA_CE actions */
50 const tNFA_CE_ACTION nfa_ce_action_tbl[] = {
51     nfa_ce_api_cfg_local_tag,   /* NFA_CE_API_CFG_LOCAL_TAG_EVT */
52     nfa_ce_api_reg_listen,      /* NFA_CE_API_REG_LISTEN_EVT    */
53     nfa_ce_api_dereg_listen,    /* NFA_CE_API_DEREG_LISTEN_EVT  */
54     nfa_ce_api_cfg_isodep_tech, /* NFA_CE_API_CFG_ISODEP_TECH_EVT*/
55     nfa_ce_activate_ntf,        /* NFA_CE_ACTIVATE_NTF_EVT      */
56     nfa_ce_deactivate_ntf,      /* NFA_CE_DEACTIVATE_NTF_EVT    */
57 };
58 #define NFA_CE_ACTION_TBL_SIZE \
59   (sizeof(nfa_ce_action_tbl) / sizeof(tNFA_CE_ACTION))
60 
61 /*****************************************************************************
62 ** Local function prototypes
63 *****************************************************************************/
64 static std::string nfa_ce_evt_2_str(uint16_t event);
65 
66 /*******************************************************************************
67 **
68 ** Function         nfa_ce_init
69 **
70 ** Description      Initialize NFA CE
71 **
72 ** Returns          None
73 **
74 *******************************************************************************/
nfa_ce_init(void)75 void nfa_ce_init(void) {
76   DLOG_IF(INFO, nfc_debug_enabled) << __func__;
77 
78   /* initialize control block */
79   memset(&nfa_ce_cb, 0, sizeof(tNFA_CE_CB));
80 
81   /* Generate a random NFCID for Type-3 NDEF emulation (Type-3 tag NFCID2 must
82    * start with 02:FE) */
83   nfa_ce_t3t_generate_rand_nfcid(
84       nfa_ce_cb.listen_info[NFA_CE_LISTEN_INFO_IDX_NDEF].t3t_nfcid2);
85   nfa_ce_cb.listen_info[NFA_CE_LISTEN_INFO_IDX_NDEF].rf_disc_handle =
86       NFA_HANDLE_INVALID;
87   nfa_ce_cb.isodep_disc_mask = NFA_CE_DEFAULT_ISODEP_DISC_MASK;
88   nfa_ce_cb.idx_wild_card = NFA_CE_LISTEN_INFO_IDX_INVALID;
89 
90   /* register message handler on NFA SYS */
91   nfa_sys_register(NFA_ID_CE, &nfa_ce_sys_reg);
92 }
93 
94 /*******************************************************************************
95 **
96 ** Function         nfa_ce_sys_disable
97 **
98 ** Description      Clean up ce sub-system
99 **
100 **
101 ** Returns          void
102 **
103 *******************************************************************************/
nfa_ce_sys_disable(void)104 void nfa_ce_sys_disable(void) {
105   tNFA_CE_LISTEN_INFO* p_info;
106   uint8_t xx;
107 
108   NFC_SetStaticRfCback(nullptr);
109 
110   /* Free scratch buf if any */
111   nfa_ce_free_scratch_buf();
112 
113   /* Delete discovery handles */
114   for (xx = 0, p_info = nfa_ce_cb.listen_info; xx < NFA_CE_LISTEN_INFO_MAX;
115        xx++, p_info++) {
116     if ((p_info->flags & NFA_CE_LISTEN_INFO_IN_USE) &&
117         (p_info->rf_disc_handle != NFA_HANDLE_INVALID)) {
118       nfa_dm_delete_rf_discover(p_info->rf_disc_handle);
119       p_info->rf_disc_handle = NFA_HANDLE_INVALID;
120     }
121   }
122 
123   nfa_sys_deregister(NFA_ID_CE);
124 }
125 
126 /*******************************************************************************
127 **
128 ** Function         nfa_ce_proc_nfcc_power_mode
129 **
130 ** Description      Processing NFCC power mode changes
131 **
132 ** Returns          None
133 **
134 *******************************************************************************/
nfa_ce_proc_nfcc_power_mode(uint8_t nfcc_power_mode)135 static void nfa_ce_proc_nfcc_power_mode(uint8_t nfcc_power_mode) {
136   tNFA_CE_CB* p_cb = &nfa_ce_cb;
137   uint8_t listen_info_idx;
138 
139   DLOG_IF(INFO, nfc_debug_enabled)
140       << StringPrintf("nfcc_power_mode=%d", nfcc_power_mode);
141 
142   /* if NFCC power mode is change to full power */
143   if (nfcc_power_mode == NFA_DM_PWR_MODE_FULL) {
144     nfa_ce_restart_listen_check();
145   } else {
146     for (listen_info_idx = 0; listen_info_idx < NFA_CE_LISTEN_INFO_IDX_INVALID;
147          listen_info_idx++) {
148       /* add RF discovery to DM only if it is not added yet */
149       if ((p_cb->listen_info[listen_info_idx].flags &
150            NFA_CE_LISTEN_INFO_IN_USE) &&
151           (p_cb->listen_info[listen_info_idx].rf_disc_handle !=
152            NFA_HANDLE_INVALID)) {
153         nfa_dm_delete_rf_discover(
154             p_cb->listen_info[listen_info_idx].rf_disc_handle);
155         p_cb->listen_info[listen_info_idx].rf_disc_handle = NFA_HANDLE_INVALID;
156       }
157     }
158   }
159 
160   nfa_sys_cback_notify_nfcc_power_mode_proc_complete(NFA_ID_CE);
161 }
162 
163 /*******************************************************************************
164 **
165 ** Function         nfa_ce_hdl_event
166 **
167 ** Description      nfa rw main event handling function.
168 **
169 ** Returns          bool
170 **
171 *******************************************************************************/
nfa_ce_hdl_event(NFC_HDR * p_msg)172 bool nfa_ce_hdl_event(NFC_HDR* p_msg) {
173   uint16_t act_idx;
174   bool freebuf = true;
175 
176   DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
177       "nfa_ce_handle_event event: %s (0x%02x), flags: %08x",
178       nfa_ce_evt_2_str(p_msg->event).c_str(), p_msg->event, nfa_ce_cb.flags);
179 
180   /* Get NFA_RW sub-event */
181   act_idx = (p_msg->event & 0x00FF);
182   if (act_idx < NFA_CE_ACTION_TBL_SIZE) {
183     freebuf = (*nfa_ce_action_tbl[act_idx])((tNFA_CE_MSG*)p_msg);
184   }
185 
186   /* if vendor specific event handler is registered */
187   if (nfa_ce_cb.p_vs_evt_hdlr) {
188     (*nfa_ce_cb.p_vs_evt_hdlr)(p_msg);
189   }
190 
191   return freebuf;
192 }
193 
194 /*******************************************************************************
195 **
196 ** Function         nfa_ce_evt_2_str
197 **
198 ** Description      convert nfc evt to string
199 **
200 *******************************************************************************/
nfa_ce_evt_2_str(uint16_t event)201 static std::string nfa_ce_evt_2_str(uint16_t event) {
202   switch (event) {
203     case NFA_CE_API_CFG_LOCAL_TAG_EVT:
204       return "NFA_CE_API_CFG_LOCAL_TAG_EVT";
205     case NFA_CE_API_REG_LISTEN_EVT:
206       return "NFA_CE_API_REG_LISTEN_EVT";
207     case NFA_CE_API_DEREG_LISTEN_EVT:
208       return "NFA_CE_API_DEREG_LISTEN_EVT";
209     case NFA_CE_API_CFG_ISODEP_TECH_EVT:
210       return "NFA_CE_API_CFG_ISODEP_TECH_EVT";
211     case NFA_CE_ACTIVATE_NTF_EVT:
212       return "NFA_CE_ACTIVATE_NTF_EVT";
213     case NFA_CE_DEACTIVATE_NTF_EVT:
214       return "NFA_CE_DEACTIVATE_NTF_EVT";
215     default:
216       return "Unknown";
217   }
218 }
219