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