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 is the main implementation file for the NFA system manager.
22  *
23  ******************************************************************************/
24 #include <android-base/logging.h>
25 #include <android-base/stringprintf.h>
26 #include <string.h>
27 
28 #include "nfa_api.h"
29 #include "nfa_dm_int.h"
30 #include "nfa_sys_int.h"
31 
32 using android::base::StringPrintf;
33 
34 /* protocol timer update period, in milliseconds */
35 #ifndef NFA_SYS_TIMER_PERIOD
36 #define NFA_SYS_TIMER_PERIOD 10
37 #endif
38 
39 /* system manager control block definition */
40 tNFA_SYS_CB nfa_sys_cb = {};
41 /* nfa_sys control block. statically initialize 'flags' field to 0 */
42 
43 /*******************************************************************************
44 **
45 ** Function         nfa_sys_init
46 **
47 ** Description      NFA initialization; called from task initialization.
48 **
49 **
50 ** Returns          void
51 **
52 *******************************************************************************/
nfa_sys_init(void)53 void nfa_sys_init(void) {
54   memset(&nfa_sys_cb, 0, sizeof(tNFA_SYS_CB));
55   nfa_sys_cb.flags |= NFA_SYS_FL_INITIALIZED;
56   nfa_sys_ptim_init(&nfa_sys_cb.ptim_cb, NFA_SYS_TIMER_PERIOD,
57                     p_nfa_sys_cfg->timer);
58 }
59 
60 /*******************************************************************************
61 **
62 ** Function         nfa_sys_event
63 **
64 ** Description      BTA event handler; called from task event handler.
65 **
66 **
67 ** Returns          void
68 **
69 *******************************************************************************/
nfa_sys_event(NFC_HDR * p_msg)70 void nfa_sys_event(NFC_HDR* p_msg) {
71   uint8_t id;
72   bool freebuf = true;
73 
74   LOG(VERBOSE) << StringPrintf("NFA got event 0x%04X", p_msg->event);
75 
76   /* get subsystem id from event */
77   id = (uint8_t)(p_msg->event >> 8);
78 
79   /* verify id and call subsystem event handler */
80   if ((id < NFA_ID_MAX) && (nfa_sys_cb.is_reg[id])) {
81     freebuf = (*nfa_sys_cb.reg[id]->evt_hdlr)(p_msg);
82   } else {
83     LOG(WARNING) << StringPrintf("NFA got unregistered event id %d", id);
84   }
85 
86   if (freebuf) {
87     GKI_freebuf(p_msg);
88   }
89 }
90 
91 /*******************************************************************************
92 **
93 ** Function         nfa_sys_timer_update
94 **
95 ** Description      Update the BTA timer list and handle expired timers.
96 **
97 ** Returns          void
98 **
99 *******************************************************************************/
nfa_sys_timer_update(void)100 void nfa_sys_timer_update(void) {
101   if (!nfa_sys_cb.timers_disabled) {
102     nfa_sys_ptim_timer_update(&nfa_sys_cb.ptim_cb);
103   }
104 }
105 
106 /*******************************************************************************
107 **
108 ** Function         nfa_sys_register
109 **
110 ** Description      Called by other BTA subsystems to register their event
111 **                  handler.
112 **
113 **
114 ** Returns          void
115 **
116 *******************************************************************************/
nfa_sys_register(uint8_t id,const tNFA_SYS_REG * p_reg)117 void nfa_sys_register(uint8_t id, const tNFA_SYS_REG* p_reg) {
118   nfa_sys_cb.reg[id] = (tNFA_SYS_REG*)p_reg;
119   nfa_sys_cb.is_reg[id] = true;
120 
121   if ((id != NFA_ID_DM) && (id != NFA_ID_SYS))
122     nfa_sys_cb.enable_cplt_mask |= (0x0001 << id);
123 
124   if (id != NFA_ID_SYS) {
125     if (p_reg->proc_nfcc_pwr_mode)
126       nfa_sys_cb.proc_nfcc_pwr_mode_cplt_mask |= (0x0001 << id);
127   }
128 
129   LOG(VERBOSE) << StringPrintf("id=%i, enable_cplt_mask=0x%x", id,
130                              nfa_sys_cb.enable_cplt_mask);
131 }
132 
133 /*******************************************************************************
134 **
135 ** Function         nfa_sys_check_disabled
136 **
137 ** Description      If all subsystems above DM have been disabled, then
138 **                  disable DM. Called during NFA shutdown
139 **
140 ** Returns          void
141 **
142 *******************************************************************************/
nfa_sys_check_disabled(void)143 void nfa_sys_check_disabled(void) {
144   uint8_t id;
145   uint8_t done = true;
146 
147   /* Check if all subsystems above DM have been disabled. */
148   for (id = (NFA_ID_DM + 1); id < NFA_ID_MAX; id++) {
149     if (nfa_sys_cb.is_reg[id]) {
150       /* as long as one subsystem is not done */
151       done = false;
152       break;
153     }
154   }
155 
156   /* All subsystems disabled. disable DM */
157   if ((done) && (nfa_sys_cb.is_reg[NFA_ID_DM])) {
158     (*nfa_sys_cb.reg[NFA_ID_DM]->disable)();
159   }
160 }
161 
162 /*******************************************************************************
163 **
164 ** Function         nfa_sys_deregister
165 **
166 ** Description      Called by other BTA subsystems to de-register
167 **                  handler.
168 **
169 **
170 ** Returns          void
171 **
172 *******************************************************************************/
nfa_sys_deregister(uint8_t id)173 void nfa_sys_deregister(uint8_t id) {
174   LOG(VERBOSE) << StringPrintf("nfa_sys: deregistering subsystem %i", id);
175 
176   nfa_sys_cb.is_reg[id] = false;
177 
178   /* If not deregistering DM, then check if any other subsystems above DM are
179    * still  */
180   /* registered. */
181   if (id != NFA_ID_DM) {
182     /* If all subsystems above NFA_DM have been disabled, then okay to disable
183      * DM */
184     nfa_sys_check_disabled();
185   } else {
186     /* DM (the final sub-system) is deregistering. Clear pending timer events in
187      * nfa_sys. */
188     nfa_sys_ptim_init(&nfa_sys_cb.ptim_cb, NFA_SYS_TIMER_PERIOD,
189                       p_nfa_sys_cfg->timer);
190   }
191 }
192 
193 /*******************************************************************************
194 **
195 ** Function         nfa_sys_is_register
196 **
197 ** Description      Called by other BTA subsystems to get registeration
198 **                  status.
199 **
200 **
201 ** Returns          void
202 **
203 *******************************************************************************/
nfa_sys_is_register(uint8_t id)204 bool nfa_sys_is_register(uint8_t id) { return nfa_sys_cb.is_reg[id]; }
205 
206 /*******************************************************************************
207 **
208 ** Function         nfa_sys_is_graceful_disable
209 **
210 ** Description      Called by other BTA subsystems to get disable
211 **                  parameter.
212 **
213 **
214 ** Returns          void
215 **
216 *******************************************************************************/
nfa_sys_is_graceful_disable(void)217 bool nfa_sys_is_graceful_disable(void) { return nfa_sys_cb.graceful_disable; }
218 
219 /*******************************************************************************
220 **
221 ** Function         nfa_sys_enable_subsystems
222 **
223 ** Description      Call on NFA Start up
224 **
225 ** Returns          void
226 **
227 *******************************************************************************/
nfa_sys_enable_subsystems(void)228 void nfa_sys_enable_subsystems(void) {
229   uint8_t id;
230 
231   LOG(VERBOSE) << StringPrintf("nfa_sys: enabling subsystems");
232 
233   /* Enable all subsystems except SYS */
234   for (id = NFA_ID_DM; id < NFA_ID_MAX; id++) {
235     if (nfa_sys_cb.is_reg[id]) {
236       if (nfa_sys_cb.reg[id]->enable != nullptr) {
237         /* Subsytem has a Disable funciton. Call it now */
238         (*nfa_sys_cb.reg[id]->enable)();
239       } else {
240         /* Subsytem does not have a Enable function. Report Enable on behalf of
241          * subsystem */
242         nfa_sys_cback_notify_enable_complete(id);
243       }
244     }
245   }
246 }
247 
248 /*******************************************************************************
249 **
250 ** Function         nfa_sys_disable_subsystems
251 **
252 ** Description      Call on NFA shutdown. Disable all subsystems above NFA_DM
253 **
254 ** Returns          void
255 **
256 *******************************************************************************/
nfa_sys_disable_subsystems(bool graceful)257 void nfa_sys_disable_subsystems(bool graceful) {
258   uint8_t id;
259   bool done = true;
260 
261   LOG(VERBOSE) << StringPrintf("nfa_sys: disabling subsystems:%d", graceful);
262   nfa_sys_cb.graceful_disable = graceful;
263 
264   /* Disable all subsystems above NFA_DM. (NFA_DM and NFA_SYS will be disabled
265    * last) */
266   for (id = (NFA_ID_DM + 1); id < NFA_ID_MAX; id++) {
267     if (nfa_sys_cb.is_reg[id]) {
268       done = false;
269       if (nfa_sys_cb.reg[id]->disable != nullptr) {
270         /* Subsytem has a Disable funciton. Call it now */
271         (*nfa_sys_cb.reg[id]->disable)();
272       } else {
273         /* Subsytem does not have a Disable function. Deregister on behalf of
274          * subsystem */
275         nfa_sys_deregister(id);
276       }
277     }
278   }
279 
280   /* If All subsystems disabled. disable DM */
281   if ((done) && (nfa_sys_cb.is_reg[NFA_ID_DM])) {
282     (*nfa_sys_cb.reg[NFA_ID_DM]->disable)();
283   }
284 }
285 
286 /*******************************************************************************
287 **
288 ** Function         nfa_sys_notify_nfcc_power_mode
289 **
290 ** Description      Call to notify NFCC power mode to NFA sub-modules
291 **
292 ** Returns          void
293 **
294 *******************************************************************************/
nfa_sys_notify_nfcc_power_mode(uint8_t nfcc_power_mode)295 void nfa_sys_notify_nfcc_power_mode(uint8_t nfcc_power_mode) {
296   uint8_t id;
297 
298   LOG(VERBOSE) << StringPrintf(
299       "nfa_sys: notify NFCC power mode(%d) to subsystems", nfcc_power_mode);
300 
301   /* Notify NFCC power state to all subsystems except NFA_SYS */
302   for (id = NFA_ID_DM; id < NFA_ID_MAX; id++) {
303     if ((nfa_sys_cb.is_reg[id]) && (nfa_sys_cb.reg[id]->proc_nfcc_pwr_mode)) {
304       /* Subsytem has a funciton for processing NFCC power mode. Call it now */
305       (*nfa_sys_cb.reg[id]->proc_nfcc_pwr_mode)(nfcc_power_mode);
306     }
307   }
308 }
309 
310 /*******************************************************************************
311 **
312 ** Function         nfa_sys_sendmsg
313 **
314 ** Description      Send a GKI message to BTA.  This function is designed to
315 **                  optimize sending of messages to BTA.  It is called by BTA
316 **                  API functions and call-in functions.
317 **
318 **
319 ** Returns          void
320 **
321 *******************************************************************************/
nfa_sys_sendmsg(void * p_msg)322 void nfa_sys_sendmsg(void* p_msg) {
323   GKI_send_msg(NFC_TASK, p_nfa_sys_cfg->mbox, p_msg);
324 }
325 
326 /*******************************************************************************
327 **
328 ** Function         nfa_sys_start_timer
329 **
330 ** Description      Start a protocol timer for the specified amount
331 **                  of time in milliseconds.
332 **
333 ** Returns          void
334 **
335 *******************************************************************************/
nfa_sys_start_timer(TIMER_LIST_ENT * p_tle,uint16_t type,int32_t timeout)336 void nfa_sys_start_timer(TIMER_LIST_ENT* p_tle, uint16_t type,
337                          int32_t timeout) {
338   nfa_sys_ptim_start_timer(&nfa_sys_cb.ptim_cb, p_tle, type, timeout);
339 }
340 
341 /*******************************************************************************
342 **
343 ** Function         nfa_sys_stop_timer
344 **
345 ** Description      Stop a BTA timer.
346 **
347 ** Returns          void
348 **
349 *******************************************************************************/
nfa_sys_stop_timer(TIMER_LIST_ENT * p_tle)350 void nfa_sys_stop_timer(TIMER_LIST_ENT* p_tle) {
351   nfa_sys_ptim_stop_timer(&nfa_sys_cb.ptim_cb, p_tle);
352 }
353 
354 /*******************************************************************************
355 **
356 ** Function         nfa_sys_disable_timers
357 **
358 ** Description      Disable sys timer event handling
359 **
360 ** Returns          void
361 **
362 *******************************************************************************/
nfa_sys_disable_timers(void)363 void nfa_sys_disable_timers(void) { nfa_sys_cb.timers_disabled = true; }
364