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 EE.
22  *
23  ******************************************************************************/
24 #include <android-base/logging.h>
25 #include <android-base/stringprintf.h>
26 
27 #include <string>
28 
29 #include "nfa_dm_int.h"
30 #include "nfa_ee_int.h"
31 #include "nfc_config.h"
32 
33 using android::base::StringPrintf;
34 
35 /*****************************************************************************
36 **  Global Variables
37 *****************************************************************************/
38 
39 /* system manager control block definition */
40 tNFA_EE_CB nfa_ee_cb;
41 
42 /*****************************************************************************
43 **  Constants
44 *****************************************************************************/
45 static const tNFA_SYS_REG nfa_ee_sys_reg = {nfa_ee_sys_enable, nfa_ee_evt_hdlr,
46                                             nfa_ee_sys_disable,
47                                             nfa_ee_proc_nfcc_power_mode};
48 
49 #define NFA_EE_NUM_ACTIONS (NFA_EE_MAX_EVT & 0x00ff)
50 
51 const tNFA_EE_SM_ACT nfa_ee_actions[] = {
52     /* NFA-EE action function/ internal events */
53     nfa_ee_api_discover,          /* NFA_EE_API_DISCOVER_EVT      */
54     nfa_ee_api_register,          /* NFA_EE_API_REGISTER_EVT      */
55     nfa_ee_api_deregister,        /* NFA_EE_API_DEREGISTER_EVT    */
56     nfa_ee_api_mode_set,          /* NFA_EE_API_MODE_SET_EVT      */
57     nfa_ee_api_set_tech_cfg,      /* NFA_EE_API_SET_TECH_CFG_EVT  */
58     nfa_ee_api_clear_tech_cfg,    /*NFA_EE_API_CLEAR_TECH_CFG_EVT */
59     nfa_ee_api_set_proto_cfg,     /* NFA_EE_API_SET_PROTO_CFG_EVT */
60     nfa_ee_api_clear_proto_cfg,   /*NFA_EE_API_CLEAR_PROTO_CFG_EVT*/
61     nfa_ee_api_add_aid,           /* NFA_EE_API_ADD_AID_EVT       */
62     nfa_ee_api_remove_aid,        /* NFA_EE_API_REMOVE_AID_EVT    */
63     nfa_ee_api_add_sys_code,      /* NFA_EE_API_ADD_SYSCODE_EVT   */
64     nfa_ee_api_remove_sys_code,   /* NFA_EE_API_REMOVE_SYSCODE_EVT*/
65     nfa_ee_api_lmrt_size,         /* NFA_EE_API_LMRT_SIZE_EVT     */
66     nfa_ee_api_update_now,        /* NFA_EE_API_UPDATE_NOW_EVT    */
67     nfa_ee_api_connect,           /* NFA_EE_API_CONNECT_EVT       */
68     nfa_ee_api_send_data,         /* NFA_EE_API_SEND_DATA_EVT     */
69     nfa_ee_api_disconnect,        /* NFA_EE_API_DISCONNECT_EVT    */
70     nfa_ee_api_pwr_and_link_ctrl, /* NFA_EE_API_PWR_AND_LINK_CTRL_EVT */
71     nfa_ee_nci_disc_rsp,          /* NFA_EE_NCI_DISC_RSP_EVT      */
72     nfa_ee_nci_disc_ntf,          /* NFA_EE_NCI_DISC_NTF_EVT      */
73     nfa_ee_nci_mode_set_rsp,      /* NFA_EE_NCI_MODE_SET_RSP_EVT  */
74     nfa_ee_nci_conn,              /* NFA_EE_NCI_CONN_EVT          */
75     nfa_ee_nci_conn,              /* NFA_EE_NCI_DATA_EVT          */
76     nfa_ee_nci_action_ntf,        /* NFA_EE_NCI_ACTION_NTF_EVT    */
77     nfa_ee_nci_disc_req_ntf,      /* NFA_EE_NCI_DISC_REQ_NTF_EVT  */
78     nfa_ee_nci_wait_rsp,          /* NFA_EE_NCI_WAIT_RSP_EVT      */
79     nfa_ee_rout_timeout,          /* NFA_EE_ROUT_TIMEOUT_EVT      */
80     nfa_ee_discv_timeout,         /* NFA_EE_DISCV_TIMEOUT_EVT     */
81     nfa_ee_lmrt_to_nfcc,          /* NFA_EE_CFG_TO_NFCC_EVT       */
82     nfa_ee_nci_nfcee_status_ntf,  /*NFA_EE_NCI_NFCEE_STATUS_NTF_EVT*/
83     nfa_ee_pwr_and_link_ctrl_rsp  /* NFA_EE_PWR_CONTROL_EVT */
84 };
85 
86 /*******************************************************************************
87 **
88 ** Function         nfa_ee_init
89 **
90 ** Description      Initialize NFA EE control block
91 **                  register to NFA SYS
92 **
93 ** Returns          None
94 **
95 *******************************************************************************/
nfa_ee_init(void)96 void nfa_ee_init(void) {
97   int xx;
98 
99   LOG(VERBOSE) << __func__;
100 
101   /* initialize control block */
102   memset(&nfa_ee_cb, 0, sizeof(tNFA_EE_CB));
103   for (xx = 0; xx < NFA_EE_MAX_EE_SUPPORTED; xx++) {
104     nfa_ee_cb.ecb[xx].nfcee_id = NFA_EE_INVALID;
105     nfa_ee_cb.ecb[xx].ee_status = NFC_NFCEE_STATUS_INACTIVE;
106   }
107 
108   nfa_ee_cb.ecb[NFA_EE_CB_4_DH].ee_status = NFC_NFCEE_STATUS_ACTIVE;
109   nfa_ee_cb.ecb[NFA_EE_CB_4_DH].nfcee_id = NFC_DH_ID;
110 
111   /* register message handler on NFA SYS */
112   nfa_sys_register(NFA_ID_EE, &nfa_ee_sys_reg);
113 }
114 
115 /*******************************************************************************
116 **
117 ** Function         nfa_ee_sys_enable
118 **
119 ** Description      Enable NFA EE
120 **
121 ** Returns          None
122 **
123 *******************************************************************************/
nfa_ee_sys_enable(void)124 void nfa_ee_sys_enable(void) {
125   LOG(VERBOSE) << StringPrintf("%s", __func__);
126 
127   nfa_ee_cb.route_block_control = 0x00;
128 
129   if (NfcConfig::hasKey(NAME_NFA_AID_BLOCK_ROUTE)) {
130     unsigned retlen = NfcConfig::getUnsigned(NAME_NFA_AID_BLOCK_ROUTE);
131     if ((retlen == 0x01) && (NFC_GetNCIVersion() >= NCI_VERSION_2_0)) {
132       nfa_ee_cb.route_block_control = NCI_ROUTE_QUAL_BLOCK_ROUTE;
133       LOG(VERBOSE) << StringPrintf("nfa_ee_cb.route_block_control=0x%x",
134                                  nfa_ee_cb.route_block_control);
135     }
136   }
137 
138   if (nfa_ee_max_ee_cfg) {
139     /* collect NFCEE information */
140     NFC_NfceeDiscover(true);
141     nfa_sys_start_timer(&nfa_ee_cb.discv_timer, NFA_EE_DISCV_TIMEOUT_EVT,
142                         NFA_EE_DISCV_TIMEOUT_VAL);
143   } else {
144     nfa_ee_cb.em_state = NFA_EE_EM_STATE_INIT_DONE;
145     nfa_sys_cback_notify_enable_complete(NFA_ID_EE);
146   }
147 }
148 
149 /*******************************************************************************
150 **
151 ** Function         nfa_ee_restore_one_ecb
152 **
153 ** Description      activate the NFCEE and restore the routing when
154 **                  changing power state from low power mode to full power mode
155 **
156 ** Returns          None
157 **
158 *******************************************************************************/
nfa_ee_restore_one_ecb(tNFA_EE_ECB * p_cb)159 void nfa_ee_restore_one_ecb(tNFA_EE_ECB* p_cb) {
160   uint8_t mask;
161   tNFC_NFCEE_MODE_SET_REVT rsp;
162   tNFA_EE_NCI_MODE_SET ee_msg;
163 
164   LOG(VERBOSE) << StringPrintf(
165       "nfcee_id:0x%x, ecb_flags:0x%x ee_status:0x%x "
166       "ee_old_status: 0x%x",
167       p_cb->nfcee_id, p_cb->ecb_flags, p_cb->ee_status, p_cb->ee_old_status);
168   if ((p_cb->nfcee_id != NFA_EE_INVALID) &&
169       (p_cb->ee_status & NFA_EE_STATUS_RESTORING) == 0 &&
170       (p_cb->ee_old_status & NFA_EE_STATUS_RESTORING) != 0) {
171     p_cb->ee_old_status &= ~NFA_EE_STATUS_RESTORING;
172     mask = nfa_ee_ecb_to_mask(p_cb);
173     if (p_cb->ee_status != p_cb->ee_old_status) {
174       p_cb->ecb_flags |= NFA_EE_ECB_FLAGS_RESTORE;
175       if (p_cb->ee_old_status == NFC_NFCEE_STATUS_ACTIVE) {
176         NFC_NfceeModeSet(p_cb->nfcee_id, NFC_MODE_ACTIVATE);
177 
178         if (nfa_ee_cb.ee_cfged & mask) {
179           /* if any routing is configured on this NFCEE. need to mark this NFCEE
180            * as changed
181            * to cause the configuration to be sent to NFCC again */
182           p_cb->ecb_flags |= NFA_EE_ECB_FLAGS_ROUTING;
183           p_cb->ecb_flags |= NFA_EE_ECB_FLAGS_VS;
184         }
185       } else {
186         NFC_NfceeModeSet(p_cb->nfcee_id, NFC_MODE_DEACTIVATE);
187       }
188     } else if (p_cb->ee_status == NFC_NFCEE_STATUS_ACTIVE) {
189       /* the initial NFCEE status after start up is the same as the current
190        * status and it's active:
191        * process the same as the host gets activate rsp */
192       p_cb->ecb_flags |= NFA_EE_ECB_FLAGS_RESTORE;
193       if (nfa_ee_cb.ee_cfged & mask) {
194         /* if any routing is configured on this NFCEE. need to mark this NFCEE
195          * as changed
196          * to cause the configuration to be sent to NFCC again */
197         p_cb->ecb_flags |= NFA_EE_ECB_FLAGS_ROUTING;
198         p_cb->ecb_flags |= NFA_EE_ECB_FLAGS_VS;
199       }
200       rsp.mode = NFA_EE_MD_ACTIVATE;
201       rsp.nfcee_id = p_cb->nfcee_id;
202       rsp.status = NFA_STATUS_OK;
203       ee_msg.p_data = &rsp;
204       tNFA_EE_MSG nfa_ee_msg;
205       nfa_ee_msg.mode_set_rsp = ee_msg;
206       nfa_ee_nci_mode_set_rsp(&nfa_ee_msg);
207     }
208   }
209 }
210 
211 /*******************************************************************************
212 **
213 ** Function         nfa_ee_proc_nfcc_power_mode
214 **
215 ** Description      Restore NFA EE sub-module
216 **
217 ** Returns          None
218 **
219 *******************************************************************************/
nfa_ee_proc_nfcc_power_mode(uint8_t nfcc_power_mode)220 void nfa_ee_proc_nfcc_power_mode(uint8_t nfcc_power_mode) {
221   uint32_t xx;
222   tNFA_EE_ECB* p_cb;
223   bool proc_complete = true;
224 
225   LOG(VERBOSE) << StringPrintf("nfcc_power_mode=%d", nfcc_power_mode);
226   /* if NFCC power state is change to full power */
227   if (nfcc_power_mode == NFA_DM_PWR_MODE_FULL) {
228     if (nfa_ee_max_ee_cfg) {
229       p_cb = nfa_ee_cb.ecb;
230       for (xx = 0; xx < NFA_EE_MAX_EE_SUPPORTED; xx++, p_cb++) {
231         p_cb->ee_old_status = 0;
232         if (xx >= nfa_ee_cb.cur_ee) p_cb->nfcee_id = NFA_EE_INVALID;
233 
234         if ((p_cb->nfcee_id != NFA_EE_INVALID) &&
235             (p_cb->ee_interface[0] != NFC_NFCEE_INTERFACE_HCI_ACCESS) &&
236             (p_cb->ee_status != NFA_EE_STATUS_REMOVED)) {
237           proc_complete = false;
238           /* NFA_EE_STATUS_RESTORING bit makes sure the ee_status restore to
239            * ee_old_status
240            * NFA_EE_STATUS_RESTORING bit is cleared in ee_status at
241            * NFCEE_DISCOVER NTF.
242            * NFA_EE_STATUS_RESTORING bit is cleared in ee_old_status at
243            * restoring the activate/inactive status after NFCEE_DISCOVER NTF */
244           p_cb->ee_status |= NFA_EE_STATUS_RESTORING;
245           p_cb->ee_old_status = p_cb->ee_status;
246           /* NFA_EE_FLAGS_RESTORE bit makes sure the routing/nci logical
247            * connection is restore to prior to entering low power mode */
248           p_cb->ecb_flags |= NFA_EE_ECB_FLAGS_RESTORE;
249         }
250       }
251       nfa_ee_cb.em_state = NFA_EE_EM_STATE_RESTORING;
252       nfa_ee_cb.num_ee_expecting = 0;
253       if (nfa_sys_is_register(NFA_ID_HCI)) {
254         nfa_ee_cb.ee_flags |= NFA_EE_FLAG_WAIT_HCI;
255         nfa_ee_cb.ee_flags |= NFA_EE_FLAG_NOTIFY_HCI;
256       }
257       NFC_NfceeDiscover(true);
258       nfa_sys_start_timer(&nfa_ee_cb.discv_timer, NFA_EE_DISCV_TIMEOUT_EVT,
259                           NFA_EE_DISCV_TIMEOUT_VAL);
260     }
261   } else {
262     nfa_sys_stop_timer(&nfa_ee_cb.timer);
263     nfa_sys_stop_timer(&nfa_ee_cb.discv_timer);
264     nfa_ee_cb.num_ee_expecting = 0;
265   }
266 
267   if (proc_complete)
268     nfa_sys_cback_notify_nfcc_power_mode_proc_complete(NFA_ID_EE);
269 }
270 
271 /*******************************************************************************
272 **
273 ** Function         nfa_ee_proc_hci_info_cback
274 **
275 ** Description      HCI initialization complete from power off sleep mode
276 **
277 ** Returns          None
278 **
279 *******************************************************************************/
nfa_ee_proc_hci_info_cback(void)280 void nfa_ee_proc_hci_info_cback(void) {
281   uint32_t xx;
282   tNFA_EE_ECB* p_cb;
283   tNFA_EE_MSG data;
284 
285   LOG(VERBOSE) << __func__;
286   /* if NFCC power state is change to full power */
287   nfa_ee_cb.ee_flags &= ~NFA_EE_FLAG_WAIT_HCI;
288 
289   p_cb = nfa_ee_cb.ecb;
290   for (xx = 0; xx < NFA_EE_MAX_EE_SUPPORTED; xx++, p_cb++) {
291     /* NCI spec says: An NFCEE_DISCOVER_NTF that contains a Protocol type of
292      * "HCI Access"
293      * SHALL NOT contain any other additional Protocol
294      * i.e. check only first supported NFCEE interface is HCI access */
295     /* NFA_HCI module handles restoring configurations for HCI access */
296     if (p_cb->ee_interface[0] != NFC_NFCEE_INTERFACE_HCI_ACCESS) {
297       nfa_ee_restore_one_ecb(p_cb);
298     }
299   }
300 
301   if (nfa_ee_restore_ntf_done()) {
302     nfa_ee_check_restore_complete();
303     if (nfa_ee_cb.em_state == NFA_EE_EM_STATE_INIT_DONE) {
304       if (nfa_ee_cb.discv_timer.in_use) {
305         nfa_sys_stop_timer(&nfa_ee_cb.discv_timer);
306         data.hdr.event = NFA_EE_DISCV_TIMEOUT_EVT;
307         nfa_ee_evt_hdlr(&data.hdr);
308       }
309     }
310   }
311 }
312 
313 /*******************************************************************************
314 **
315 ** Function         nfa_ee_proc_evt
316 **
317 ** Description      Process NFCEE related events from NFC stack
318 **
319 **
320 ** Returns          None
321 **
322 *******************************************************************************/
nfa_ee_proc_evt(tNFC_RESPONSE_EVT event,void * p_data)323 void nfa_ee_proc_evt(tNFC_RESPONSE_EVT event, void* p_data) {
324   tNFA_EE_INT_EVT int_event = 0;
325   tNFA_EE_NCI_WAIT_RSP cbk = tNFA_EE_NCI_WAIT_RSP();
326 
327   switch (event) {
328     case NFC_NFCEE_DISCOVER_REVT: /* 4  NFCEE Discover response */
329       int_event = NFA_EE_NCI_DISC_RSP_EVT;
330       break;
331 
332     case NFC_NFCEE_INFO_REVT: /* 5  NFCEE Discover Notification */
333       int_event = NFA_EE_NCI_DISC_NTF_EVT;
334       break;
335 
336     case NFC_NFCEE_MODE_SET_REVT: /* 6  NFCEE Mode Set response */
337       int_event = NFA_EE_NCI_MODE_SET_RSP_EVT;
338       break;
339 
340     case NFC_EE_ACTION_REVT:
341       int_event = NFA_EE_NCI_ACTION_NTF_EVT;
342       break;
343 
344     case NFC_EE_DISCOVER_REQ_REVT: /* 10 EE Discover Req notification */
345       int_event = NFA_EE_NCI_DISC_REQ_NTF_EVT;
346       break;
347 
348     case NFC_SET_ROUTING_REVT:
349       int_event = NFA_EE_NCI_WAIT_RSP_EVT;
350       cbk.opcode = NCI_MSG_RF_SET_ROUTING;
351       break;
352 
353     case NFC_NFCEE_STATUS_REVT:
354       int_event = NFA_EE_NCI_NFCEE_STATUS_NTF_EVT;
355       break;
356 
357     case NFC_NFCEE_PL_CONTROL_REVT:
358       int_event = NFA_EE_PWR_CONTROL_EVT;
359       break;
360   }
361 
362   LOG(VERBOSE) << StringPrintf("nfa_ee_proc_evt: event=0x%02x int_event:0x%x",
363                              event, int_event);
364   if (int_event) {
365     cbk.hdr.event = int_event;
366     cbk.p_data = p_data;
367     tNFA_EE_MSG nfa_ee_msg;
368     nfa_ee_msg.wait_rsp = cbk;
369 
370     nfa_ee_evt_hdlr(&nfa_ee_msg.hdr);
371   }
372 }
373 
374 /*******************************************************************************
375 **
376 ** Function         nfa_ee_ecb_to_mask
377 **
378 ** Description      Given a ecb, return the bit mask to be used in
379 **                  nfa_ee_cb.ee_cfged
380 **
381 ** Returns          the bitmask for the given ecb.
382 **
383 *******************************************************************************/
nfa_ee_ecb_to_mask(tNFA_EE_ECB * p_cb)384 uint8_t nfa_ee_ecb_to_mask(tNFA_EE_ECB* p_cb) {
385   uint8_t mask;
386   uint8_t index;
387 
388   index = (uint8_t)(p_cb - nfa_ee_cb.ecb);
389   mask = 1 << index;
390 
391   return mask;
392 }
393 
394 /*******************************************************************************
395 **
396 ** Function         nfa_ee_find_ecb
397 **
398 ** Description      Return the ecb associated with the given nfcee_id
399 **
400 ** Returns          tNFA_EE_ECB
401 **
402 *******************************************************************************/
nfa_ee_find_ecb(uint8_t nfcee_id)403 tNFA_EE_ECB* nfa_ee_find_ecb(uint8_t nfcee_id) {
404   uint32_t xx;
405   tNFA_EE_ECB *p_ret = nullptr, *p_cb;
406   LOG(VERBOSE) << __func__;
407 
408   if (nfcee_id == NFC_DH_ID) {
409     p_ret = &nfa_ee_cb.ecb[NFA_EE_CB_4_DH];
410   } else {
411     p_cb = nfa_ee_cb.ecb;
412     for (xx = 0; xx < NFA_EE_MAX_EE_SUPPORTED; xx++, p_cb++) {
413       if (nfcee_id == p_cb->nfcee_id) {
414         p_ret = p_cb;
415         break;
416       }
417     }
418   }
419 
420   return p_ret;
421 }
422 
423 /*******************************************************************************
424 **
425 ** Function         nfa_ee_find_ecb_by_conn_id
426 **
427 ** Description      Return the ecb associated with the given connection id
428 **
429 ** Returns          tNFA_EE_ECB
430 **
431 *******************************************************************************/
nfa_ee_find_ecb_by_conn_id(uint8_t conn_id)432 tNFA_EE_ECB* nfa_ee_find_ecb_by_conn_id(uint8_t conn_id) {
433   uint32_t xx;
434   tNFA_EE_ECB *p_ret = nullptr, *p_cb;
435   LOG(VERBOSE) << __func__;
436 
437   p_cb = nfa_ee_cb.ecb;
438   for (xx = 0; xx < nfa_ee_cb.cur_ee; xx++, p_cb++) {
439     if (conn_id == p_cb->conn_id) {
440       p_ret = p_cb;
441       break;
442     }
443   }
444 
445   return p_ret;
446 }
447 
448 /*******************************************************************************
449 **
450 ** Function         nfa_ee_sys_disable
451 **
452 ** Description      Deregister NFA EE from NFA SYS/DM
453 **
454 **
455 ** Returns          None
456 **
457 *******************************************************************************/
nfa_ee_sys_disable(void)458 void nfa_ee_sys_disable(void) {
459   uint32_t xx;
460   tNFA_EE_ECB* p_cb;
461   tNFA_EE_MSG msg;
462 
463   LOG(VERBOSE) << __func__;
464 
465   nfa_ee_cb.em_state = NFA_EE_EM_STATE_DISABLED;
466   /* report NFA_EE_DEREGISTER_EVT to all registered to EE */
467   for (xx = 0; xx < NFA_EE_MAX_CBACKS; xx++) {
468     if (nfa_ee_cb.p_ee_cback[xx]) {
469       msg.deregister.index = xx;
470       nfa_ee_api_deregister(&msg);
471     }
472   }
473 
474   nfa_ee_cb.num_ee_expecting = 0;
475   p_cb = nfa_ee_cb.ecb;
476   for (xx = 0; xx < nfa_ee_cb.cur_ee; xx++, p_cb++) {
477     if (p_cb->conn_st == NFA_EE_CONN_ST_CONN) {
478       if (nfa_sys_is_graceful_disable()) {
479         /* Disconnect NCI connection on graceful shutdown */
480         msg.disconnect.p_cb = p_cb;
481         nfa_ee_api_disconnect(&msg);
482         nfa_ee_cb.num_ee_expecting++;
483       } else {
484         /* fake NFA_EE_DISCONNECT_EVT on ungraceful shutdown */
485         msg.conn.conn_id = p_cb->conn_id;
486         msg.conn.event = NFC_CONN_CLOSE_CEVT;
487         nfa_ee_nci_conn(&msg);
488       }
489     }
490   }
491 
492   if (nfa_ee_cb.num_ee_expecting) {
493     nfa_ee_cb.ee_flags |= NFA_EE_FLAG_WAIT_DISCONN;
494     nfa_ee_cb.em_state = NFA_EE_EM_STATE_DISABLING;
495   }
496 
497   nfa_sys_stop_timer(&nfa_ee_cb.timer);
498   nfa_sys_stop_timer(&nfa_ee_cb.discv_timer);
499 
500   /* If Application initiated NFCEE discovery, fake/report the event */
501   nfa_ee_report_disc_done(false);
502 
503   /* deregister message handler on NFA SYS */
504   if (nfa_ee_cb.em_state == NFA_EE_EM_STATE_DISABLED)
505     nfa_sys_deregister(NFA_ID_EE);
506 }
507 
508 /*******************************************************************************
509 **
510 ** Function         nfa_ee_check_disable
511 **
512 ** Description      Check if it is safe to move to disabled state
513 **
514 ** Returns          None
515 **
516 *******************************************************************************/
nfa_ee_check_disable(void)517 void nfa_ee_check_disable(void) {
518   if (!(nfa_ee_cb.ee_flags & NFA_EE_FLAG_WAIT_DISCONN)) {
519     nfa_ee_cb.em_state = NFA_EE_EM_STATE_DISABLED;
520     nfa_sys_deregister(NFA_ID_EE);
521   }
522 }
523 /*******************************************************************************
524 **
525 ** Function         nfa_ee_reg_cback_enable_done
526 **
527 ** Description      Allow a module to register to EE to be notified when NFA-EE
528 **                  finishes enable process
529 **
530 ** Returns          None
531 **
532 *******************************************************************************/
nfa_ee_reg_cback_enable_done(tNFA_EE_ENABLE_DONE_CBACK * p_cback)533 void nfa_ee_reg_cback_enable_done(tNFA_EE_ENABLE_DONE_CBACK* p_cback) {
534   nfa_ee_cb.p_enable_cback = p_cback;
535 }
536 
537 /*******************************************************************************
538 **
539 ** Function         nfa_ee_sm_st_2_str
540 **
541 ** Description      convert nfa-ee state to string
542 **
543 *******************************************************************************/
nfa_ee_sm_st_2_str(uint8_t state)544 static std::string nfa_ee_sm_st_2_str(uint8_t state) {
545   switch (state) {
546     case NFA_EE_EM_STATE_INIT:
547       return "INIT";
548     case NFA_EE_EM_STATE_INIT_DONE:
549       return "INIT_DONE";
550     case NFA_EE_EM_STATE_RESTORING:
551       return "RESTORING";
552     case NFA_EE_EM_STATE_DISABLING:
553       return "DISABLING";
554     case NFA_EE_EM_STATE_DISABLED:
555       return "DISABLED";
556     default:
557       return "Unknown";
558   }
559 }
560 
561 /*******************************************************************************
562 **
563 ** Function         nfa_ee_sm_evt_2_str
564 **
565 ** Description      convert nfa-ee evt to string
566 **
567 *******************************************************************************/
nfa_ee_sm_evt_2_str(uint16_t event)568 static std::string nfa_ee_sm_evt_2_str(uint16_t event) {
569   switch (event) {
570     case NFA_EE_API_DISCOVER_EVT:
571       return "API_DISCOVER";
572     case NFA_EE_API_REGISTER_EVT:
573       return "API_REGISTER";
574     case NFA_EE_API_DEREGISTER_EVT:
575       return "API_DEREGISTER";
576     case NFA_EE_API_MODE_SET_EVT:
577       return "API_MODE_SET";
578     case NFA_EE_API_SET_TECH_CFG_EVT:
579       return "API_SET_TECH_CFG";
580     case NFA_EE_API_CLEAR_TECH_CFG_EVT:
581       return "API_CLEAR_TECH_CFG";
582     case NFA_EE_API_SET_PROTO_CFG_EVT:
583       return "API_SET_PROTO_CFG";
584     case NFA_EE_API_CLEAR_PROTO_CFG_EVT:
585       return "API_CLEAR_PROTO_CFG";
586     case NFA_EE_API_ADD_AID_EVT:
587       return "API_ADD_AID";
588     case NFA_EE_API_REMOVE_AID_EVT:
589       return "API_REMOVE_AID";
590     case NFA_EE_API_ADD_SYSCODE_EVT:
591       return "NFA_EE_API_ADD_SYSCODE_EVT";
592     case NFA_EE_API_REMOVE_SYSCODE_EVT:
593       return "NFA_EE_API_REMOVE_SYSCODE_EVT";
594     case NFA_EE_API_LMRT_SIZE_EVT:
595       return "API_LMRT_SIZE";
596     case NFA_EE_API_UPDATE_NOW_EVT:
597       return "API_UPDATE_NOW";
598     case NFA_EE_API_CONNECT_EVT:
599       return "API_CONNECT";
600     case NFA_EE_API_SEND_DATA_EVT:
601       return "API_SEND_DATA";
602     case NFA_EE_API_DISCONNECT_EVT:
603       return "API_DISCONNECT";
604     case NFA_EE_API_PWR_AND_LINK_CTRL_EVT:
605       return "NFA_EE_API_PWR_AND_LINK_CTRL_EVT";
606     case NFA_EE_NCI_DISC_RSP_EVT:
607       return "NCI_DISC_RSP";
608     case NFA_EE_NCI_DISC_NTF_EVT:
609       return "NCI_DISC_NTF";
610     case NFA_EE_NCI_MODE_SET_RSP_EVT:
611       return "NCI_MODE_SET";
612     case NFA_EE_NCI_CONN_EVT:
613       return "NCI_CONN";
614     case NFA_EE_NCI_DATA_EVT:
615       return "NCI_DATA";
616     case NFA_EE_NCI_ACTION_NTF_EVT:
617       return "NCI_ACTION";
618     case NFA_EE_NCI_DISC_REQ_NTF_EVT:
619       return "NCI_DISC_REQ";
620     case NFA_EE_NCI_WAIT_RSP_EVT:
621       return "NCI_WAIT_RSP";
622     case NFA_EE_ROUT_TIMEOUT_EVT:
623       return "ROUT_TIMEOUT";
624     case NFA_EE_DISCV_TIMEOUT_EVT:
625       return "NFA_EE_DISCV_TIMEOUT_EVT";
626     case NFA_EE_CFG_TO_NFCC_EVT:
627       return "CFG_TO_NFCC";
628     case NFA_EE_PWR_CONTROL_EVT:
629       return "NFA_EE_PWR_CONTROL_EVT";
630     default:
631       return "Unknown";
632   }
633 }
634 
635 /*******************************************************************************
636 **
637 ** Function         nfa_ee_evt_hdlr
638 **
639 ** Description      Processing event for NFA EE
640 **
641 **
642 ** Returns          TRUE if p_msg needs to be deallocated
643 **
644 *******************************************************************************/
nfa_ee_evt_hdlr(NFC_HDR * p_msg)645 bool nfa_ee_evt_hdlr(NFC_HDR* p_msg) {
646   bool act = false;
647 
648   LOG(VERBOSE) << StringPrintf(
649       "Event %s(0x%02x), State: %s(%d)",
650       nfa_ee_sm_evt_2_str(p_msg->event).c_str(), p_msg->event,
651       nfa_ee_sm_st_2_str(nfa_ee_cb.em_state).c_str(), nfa_ee_cb.em_state);
652 
653   switch (nfa_ee_cb.em_state) {
654     case NFA_EE_EM_STATE_INIT_DONE:
655     case NFA_EE_EM_STATE_RESTORING:
656       act = true;
657       break;
658     case NFA_EE_EM_STATE_INIT:
659       if ((p_msg->event == NFA_EE_NCI_DISC_NTF_EVT) ||
660           (p_msg->event == NFA_EE_NCI_DISC_RSP_EVT))
661         act = true;
662       break;
663     case NFA_EE_EM_STATE_DISABLING:
664       if (p_msg->event == NFA_EE_NCI_CONN_EVT) act = true;
665       break;
666   }
667 
668   tNFA_EE_MSG* p_evt_data = (tNFA_EE_MSG*)p_msg;
669   if (act) {
670     uint16_t event = p_msg->event & 0x00ff;
671     if (event < NFA_EE_NUM_ACTIONS) {
672       (*nfa_ee_actions[event])(p_evt_data);
673     }
674   } else {
675     /* If the event is not handled, free the data packet. */
676     /* FIXME: Is it really always tNFA_EE_NCI_CONN? */
677     if (p_msg->event == NFA_EE_NCI_DATA_EVT)
678       GKI_freebuf(p_evt_data->conn.p_data);
679   }
680 
681   return true;
682 }
683