• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *  Copyright (C) 2003-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_RW
22  *
23  ******************************************************************************/
24 #include <string.h>
25 
26 #include <android-base/stringprintf.h>
27 #include <base/logging.h>
28 
29 #include "nfa_dm_int.h"
30 #include "nfa_rw_api.h"
31 #include "nfa_rw_int.h"
32 
33 using android::base::StringPrintf;
34 
35 extern bool nfc_debug_enabled;
36 
37 /* NFA_RW control block */
38 tNFA_RW_CB nfa_rw_cb;
39 
40 /*****************************************************************************
41 ** Constants and types
42 *****************************************************************************/
43 static const tNFA_SYS_REG nfa_rw_sys_reg = {nullptr, nfa_rw_handle_event,
44                                             nfa_rw_sys_disable, nullptr};
45 
46 /* NFA_RW actions */
47 const tNFA_RW_ACTION nfa_rw_action_tbl[] = {
48     nfa_rw_handle_op_req,         /* NFA_RW_OP_REQUEST_EVT            */
49     nfa_rw_activate_ntf,          /* NFA_RW_ACTIVATE_NTF_EVT          */
50     nfa_rw_deactivate_ntf,        /* NFA_RW_DEACTIVATE_NTF_EVT        */
51     nfa_rw_presence_check_tick,   /* NFA_RW_PRESENCE_CHECK_TICK_EVT   */
52     nfa_rw_presence_check_timeout /* NFA_RW_PRESENCE_CHECK_TIMEOUT_EVT*/
53 };
54 
55 /*****************************************************************************
56 ** Local function prototypes
57 *****************************************************************************/
58 static std::string nfa_rw_evt_2_str(uint16_t event);
59 
60 /*******************************************************************************
61 **
62 ** Function         nfa_rw_init
63 **
64 ** Description      Initialize NFA RW
65 **
66 ** Returns          None
67 **
68 *******************************************************************************/
nfa_rw_init(void)69 void nfa_rw_init(void) {
70   DLOG_IF(INFO, nfc_debug_enabled) << __func__;
71 
72   /* initialize control block */
73   memset(&nfa_rw_cb, 0, sizeof(tNFA_RW_CB));
74 
75   /* register message handler on NFA SYS */
76   nfa_sys_register(NFA_ID_RW, &nfa_rw_sys_reg);
77 }
78 
79 /*******************************************************************************
80 **
81 ** Function         nfa_rw_sys_disable
82 **
83 ** Description      Clean up rw sub-system
84 **
85 **
86 ** Returns          void
87 **
88 *******************************************************************************/
nfa_rw_sys_disable(void)89 void nfa_rw_sys_disable(void) {
90   /* Return to idle */
91   NFC_SetStaticRfCback(nullptr);
92 
93   /* Stop presence check timer (if started) */
94   nfa_rw_stop_presence_check_timer();
95 
96   /* Free scratch buffer if any */
97   nfa_rw_free_ndef_rx_buf();
98 
99   /* Free pending command if any */
100   if (nfa_rw_cb.p_pending_msg) {
101     GKI_freebuf(nfa_rw_cb.p_pending_msg);
102     nfa_rw_cb.p_pending_msg = nullptr;
103   }
104 
105   nfa_sys_deregister(NFA_ID_RW);
106 }
107 
108 /*******************************************************************************
109 **
110 ** Function         nfa_rw_proc_disc_evt
111 **
112 ** Description      Called by nfa_dm to handle ACTIVATED/DEACTIVATED  events
113 **
114 ** Returns          void
115 **
116 *******************************************************************************/
nfa_rw_proc_disc_evt(tNFA_DM_RF_DISC_EVT event,tNFC_DISCOVER * p_data,bool excl_rf_not_active)117 void nfa_rw_proc_disc_evt(tNFA_DM_RF_DISC_EVT event, tNFC_DISCOVER* p_data,
118                           bool excl_rf_not_active) {
119   tNFA_RW_MSG msg;
120 
121   switch (event) {
122     case NFA_DM_RF_DISC_ACTIVATED_EVT:
123       msg.hdr.event = NFA_RW_ACTIVATE_NTF_EVT;
124       msg.activate_ntf.p_activate_params = &p_data->activate;
125       msg.activate_ntf.excl_rf_not_active = excl_rf_not_active;
126 
127       nfa_rw_handle_event((NFC_HDR*)&msg);
128       break;
129 
130     case NFA_DM_RF_DISC_DEACTIVATED_EVT:
131       msg.hdr.event = NFA_RW_DEACTIVATE_NTF_EVT;
132 
133       nfa_rw_handle_event((NFC_HDR*)&msg);
134       break;
135 
136     default:
137       break;
138   }
139 }
140 
141 /*******************************************************************************
142 **
143 ** Function         nfa_rw_send_raw_frame
144 **
145 ** Description      Called by nfa_dm to send raw frame
146 **
147 ** Returns          tNFA_STATUS
148 **
149 *******************************************************************************/
nfa_rw_send_raw_frame(NFC_HDR * p_data)150 tNFA_STATUS nfa_rw_send_raw_frame(NFC_HDR* p_data) {
151   tNFA_RW_MSG* p_msg;
152 
153   p_msg = (tNFA_RW_MSG*)GKI_getbuf((uint16_t)sizeof(tNFA_RW_MSG));
154   if (p_msg != nullptr) {
155     p_msg->hdr.event = NFA_RW_OP_REQUEST_EVT;
156     p_msg->op_req.op = NFA_RW_OP_SEND_RAW_FRAME;
157 
158     p_msg->op_req.params.send_raw_frame.p_data = p_data;
159 
160     if (nfa_rw_handle_event((NFC_HDR*)p_msg)) GKI_freebuf(p_msg);
161 
162     return (NFA_STATUS_OK);
163   }
164   return NFA_STATUS_FAILED;
165 }
166 
167 /*******************************************************************************
168 **
169 ** Function         nfa_rw_handle_event
170 **
171 ** Description      nfa rw main event handling function.
172 **
173 ** Returns          TRUE if caller should free p_msg buffer
174 **
175 *******************************************************************************/
nfa_rw_handle_event(NFC_HDR * p_msg)176 bool nfa_rw_handle_event(NFC_HDR* p_msg) {
177   uint16_t act_idx;
178 
179   DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
180       "nfa_rw_handle_event event: %s (0x%02x), flags: %08x",
181       nfa_rw_evt_2_str(p_msg->event).c_str(), p_msg->event, nfa_rw_cb.flags);
182 
183   /* Get NFA_RW sub-event */
184   act_idx = (p_msg->event & 0x00FF);
185   if (act_idx < (NFA_RW_MAX_EVT & 0xFF)) {
186     return (*nfa_rw_action_tbl[act_idx])((tNFA_RW_MSG*)p_msg);
187   } else {
188     LOG(ERROR) << StringPrintf("nfa_rw_handle_event: unhandled event 0x%02X",
189                                p_msg->event);
190     return true;
191   }
192 }
193 
194 /*******************************************************************************
195 **
196 ** Function         nfa_rw_evt_2_str
197 **
198 ** Description      convert nfa_rw evt to string
199 **
200 *******************************************************************************/
nfa_rw_evt_2_str(uint16_t event)201 static std::string nfa_rw_evt_2_str(uint16_t event) {
202   switch (event) {
203     case NFA_RW_OP_REQUEST_EVT:
204       return "NFA_RW_OP_REQUEST_EVT";
205     case NFA_RW_ACTIVATE_NTF_EVT:
206       return "NFA_RW_ACTIVATE_NTF_EVT";
207     case NFA_RW_DEACTIVATE_NTF_EVT:
208       return "NFA_RW_DEACTIVATE_NTF_EVT";
209     case NFA_RW_PRESENCE_CHECK_TICK_EVT:
210       return "NFA_RW_PRESENCE_CHECK_TICK_EVT";
211     case NFA_RW_PRESENCE_CHECK_TIMEOUT_EVT:
212       return "NFA_RW_PRESENCE_CHECK_TIMEOUT_EVT";
213     default:
214       return "Unknown";
215   }
216 }
217