1 /******************************************************************************
2  *
3  *  Copyright (C) 2023 The Android Open Source Project.
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_WLC
22  *
23  ******************************************************************************/
24 #include <android-base/logging.h>
25 #include <android-base/stringprintf.h>
26 #include <string.h>
27 
28 #include "nfa_wlc_int.h"
29 
30 using android::base::StringPrintf;
31 
32 /* NFA_WLC control block */
33 tNFA_WLC_CB nfa_wlc_cb;
34 
35 bool nfa_wlc_handle_event(NFC_HDR* p_msg);
36 void nfa_wlc_sys_disable(void);
37 
38 /*****************************************************************************
39 ** Constants and types
40 *****************************************************************************/
41 static const tNFA_SYS_REG nfa_wlc_sys_reg = {nullptr, nfa_wlc_handle_event,
42                                              nfa_wlc_sys_disable, nullptr};
43 
44 /* NFA_WLC actions */
45 const tNFA_WLC_ACTION nfa_wlc_action_tbl[] = {
46     nfa_wlc_enable,             /* NFA_WLC_API_ENABLE_EVT            */
47     nfa_wlc_start,              /* NFA_WLC_API_START_EVT              */
48     nfa_wlc_non_auto_start_wpt, /* NFA_WLC_API_NON_AUTO_START_WPT_EVT */
49 };
50 
51 #define NFA_WLC_ACTION_TBL_SIZE \
52   (sizeof(nfa_wlc_action_tbl) / sizeof(tNFA_WLC_ACTION))
53 
54 /*****************************************************************************
55 ** Local function prototypes
56 *****************************************************************************/
57 static std::string nfa_wlc_evt_2_str(uint16_t event);
58 
59 /*******************************************************************************
60 **
61 ** Function         nfa_wlc_init
62 **
63 ** Description      Initialize NFA WLC
64 **
65 ** Returns          none
66 **
67 *******************************************************************************/
nfa_wlc_init(void)68 void nfa_wlc_init(void) {
69   LOG(VERBOSE) << __func__;
70 
71   /* initialize control block */
72   memset(&nfa_wlc_cb, 0, sizeof(tNFA_WLC_CB));
73 
74   /* register message handler on NFA SYS */
75   nfa_sys_register(NFA_ID_WLC, &nfa_wlc_sys_reg);
76 }
77 
78 /*******************************************************************************
79 **
80 ** Function         nfa_wlc_sys_disable
81 **
82 ** Description      Clean up rw sub-system
83 **
84 **
85 ** Returns          none
86 **
87 *******************************************************************************/
nfa_wlc_sys_disable(void)88 void nfa_wlc_sys_disable(void) {
89   LOG(VERBOSE) << __func__;
90 
91   nfa_sys_deregister(NFA_ID_WLC);
92 }
93 
94 /*******************************************************************************
95 **
96 ** Function         nfa_wlc_event_notify
97 **
98 ** Description      Called by nfa_dm to handle WLC dedicated events
99 **
100 ** Returns          none
101 **
102 *******************************************************************************/
nfa_wlc_event_notify(tNFA_WLC_EVT event,tNFA_WLC_EVT_DATA * p_data)103 void nfa_wlc_event_notify(tNFA_WLC_EVT event, tNFA_WLC_EVT_DATA* p_data) {
104   LOG(VERBOSE) << __func__;
105 
106   if (nfa_wlc_cb.p_wlc_cback) {
107     (*nfa_wlc_cb.p_wlc_cback)(event, p_data);
108   } else {
109     LOG(VERBOSE) << StringPrintf("%s; callback pointer null", __func__);
110   }
111 }
112 
113 /*******************************************************************************
114 **
115 ** Function         nfa_wlc_handle_event
116 **
117 ** Description      nfa wlc main event handling function.
118 **
119 ** Returns          TRUE if caller should free p_msg buffer
120 **
121 *******************************************************************************/
nfa_wlc_handle_event(NFC_HDR * p_msg)122 bool nfa_wlc_handle_event(NFC_HDR* p_msg) {
123   uint16_t act_idx;
124 
125   LOG(VERBOSE) << StringPrintf("%s; event: %s (0x%02x), flags: %08x", __func__,
126                              nfa_wlc_evt_2_str(p_msg->event).c_str(),
127                              p_msg->event, nfa_wlc_cb.flags);
128 
129   /* Get NFA_WLC sub-event */
130   act_idx = (p_msg->event & 0x00FF);
131   if (act_idx < (NFA_WLC_ACTION_TBL_SIZE)) {
132     return (*nfa_wlc_action_tbl[act_idx])((tNFA_WLC_MSG*)p_msg);
133   } else {
134     LOG(ERROR) << StringPrintf("%s; unhandled event 0x%02X", __func__,
135                                p_msg->event);
136     return true;
137   }
138 }
139 
140 /*******************************************************************************
141 **
142 ** Function         nfa_wlc_evt_2_str
143 **
144 ** Description      convert nfa_wlc evt to string
145 **
146 *******************************************************************************/
nfa_wlc_evt_2_str(uint16_t event)147 static std::string nfa_wlc_evt_2_str(uint16_t event) {
148   switch (event) {
149     case NFA_WLC_API_ENABLE_EVT:
150       return "NFA_WLC_API_ENABLE_EVT";
151     case NFA_WLC_API_START_EVT:
152       return "NFA_WLC_API_START_EVT";
153     case NFA_WLC_API_NON_AUTO_START_WPT_EVT:
154       return "NFA_WLC_API_NON_AUTO_START_WPT_EVT";
155     default:
156       return "Unknown";
157   }
158 }
159