nfa_ee_act.c revision a96095b67de952a978390f2bfe5987e1c62d456b
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 *
22 *  This file contains the action functions for NFA-EE
23 *
24 ******************************************************************************/
25#include <string.h>
26#include "nfa_sys.h"
27#include "nfa_api.h"
28#include "nfa_dm_int.h"
29#include "nfa_sys_int.h"
30#include "nfc_api.h"
31#include "nfa_ee_int.h"
32
33
34/* the de-bounce timer:
35 * The NFA-EE API functions are called to set the routing and VS configuration.
36 * When this timer expires, the configuration is sent to NFCC all at once.
37 * This is the timeout value for the de-bounce timer. */
38#ifndef NFA_EE_ROUT_TIMEOUT_VAL
39#define NFA_EE_ROUT_TIMEOUT_VAL         1000
40#endif
41
42#define NFA_EE_ROUT_BUF_SIZE            540
43#define NFA_EE_ROUT_ONE_TECH_CFG_LEN    4
44#define NFA_EE_ROUT_ONE_PROTO_CFG_LEN   4
45#define NFA_EE_ROUT_MAX_TLV_SIZE        0xFD
46
47
48/* the following 2 tables convert the technology mask in API and control block to the command for NFCC */
49#define NFA_EE_NUM_TECH     3
50const UINT8 nfa_ee_tech_mask_list[NFA_EE_NUM_TECH] =
51{
52    NFA_TECHNOLOGY_MASK_A,
53    NFA_TECHNOLOGY_MASK_B,
54    NFA_TECHNOLOGY_MASK_F
55};
56
57const UINT8 nfa_ee_tech_list[NFA_EE_NUM_TECH] =
58{
59    NFC_RF_TECHNOLOGY_A,
60    NFC_RF_TECHNOLOGY_B,
61    NFC_RF_TECHNOLOGY_F
62};
63
64/* the following 2 tables convert the protocol mask in API and control block to the command for NFCC */
65#define NFA_EE_NUM_PROTO     5
66const UINT8 nfa_ee_proto_mask_list[NFA_EE_NUM_PROTO] =
67{
68    NFA_PROTOCOL_MASK_T1T,
69    NFA_PROTOCOL_MASK_T2T,
70    NFA_PROTOCOL_MASK_T3T,
71    NFA_PROTOCOL_MASK_ISO_DEP,
72    NFA_PROTOCOL_MASK_NFC_DEP
73};
74
75const UINT8 nfa_ee_proto_list[NFA_EE_NUM_PROTO] =
76{
77    NFC_PROTOCOL_T1T,
78    NFC_PROTOCOL_T2T,
79    NFC_PROTOCOL_T3T,
80    NFC_PROTOCOL_ISO_DEP,
81    NFC_PROTOCOL_NFC_DEP
82};
83
84static void nfa_ee_report_discover_req_evt(void);
85static void nfa_ee_build_discover_req_evt (tNFA_EE_DISCOVER_REQ *p_evt_data);
86/*******************************************************************************
87**
88** Function         nfa_ee_trace_aid
89**
90** Description      trace AID
91**
92** Returns          void
93**
94*******************************************************************************/
95static void nfa_ee_trace_aid (char *p_str, UINT8 id,  UINT8 aid_len, UINT8 *p)
96{
97    int     len = aid_len;
98    int     xx, yy = 0;
99    char    buff[100];
100
101    buff[0] = 0;
102    if (aid_len > NFA_MAX_AID_LEN)
103    {
104        NFA_TRACE_ERROR2 ("aid_len: %d exceeds max(%d)", aid_len, NFA_MAX_AID_LEN);
105        len = NFA_MAX_AID_LEN;
106    }
107    for (xx = 0; xx < len; xx++)
108    {
109        yy += sprintf (&buff[yy], "%02x ", *p);
110        p++;
111    }
112    NFA_TRACE_DEBUG4 ("%s id:0x%x len=%d aid:%s", p_str, id, aid_len, buff);
113
114}
115
116/*******************************************************************************
117**
118** Function         nfa_ee_update_route_size
119**
120** Description      Update the size required for technology and protocol routing
121**                  of the given NFCEE ID.
122**
123** Returns          void
124**
125*******************************************************************************/
126static void nfa_ee_update_route_size(tNFA_EE_ECB *p_cb)
127{
128    int     xx;
129    UINT8   power_cfg = 0;
130
131    p_cb->size_mask = 0;
132    /* add the Technology based routing */
133    for (xx = 0; xx < NFA_EE_NUM_TECH; xx++)
134    {
135        power_cfg = 0;
136        if (p_cb->tech_switch_on & nfa_ee_tech_mask_list[xx])
137            power_cfg |= NCI_ROUTE_PWR_STATE_ON;
138        if (p_cb->tech_switch_off & nfa_ee_tech_mask_list[xx])
139            power_cfg |= NCI_ROUTE_PWR_STATE_SWITCH_OFF;
140        if (p_cb->tech_battery_off & nfa_ee_tech_mask_list[xx])
141            power_cfg |= NCI_ROUTE_PWR_STATE_BATT_OFF;
142        if (power_cfg)
143        {
144            /* 5 = 1 (tag) + 1 (len) + 1(nfcee_id) + 1(power cfg) + 1 (techonogy) */
145            p_cb->size_mask += 5;
146        }
147    }
148
149    /* add the Protocol based routing */
150    for (xx = 0; xx < NFA_EE_NUM_PROTO; xx++)
151    {
152        power_cfg = 0;
153        if (p_cb->proto_switch_on & nfa_ee_proto_mask_list[xx])
154            power_cfg |= NCI_ROUTE_PWR_STATE_ON;
155        if (p_cb->proto_switch_off & nfa_ee_proto_mask_list[xx])
156            power_cfg |= NCI_ROUTE_PWR_STATE_SWITCH_OFF;
157        if (p_cb->proto_battery_off & nfa_ee_proto_mask_list[xx])
158            power_cfg |= NCI_ROUTE_PWR_STATE_BATT_OFF;
159        if (power_cfg)
160        {
161            /* 5 = 1 (tag) + 1 (len) + 1(nfcee_id) + 1(power cfg) + 1 (protocol) */
162            p_cb->size_mask += 5;
163        }
164    }
165    NFA_TRACE_DEBUG2 ("nfa_ee_update_route_size nfcee_id:0x%x size_mask:%d", p_cb->nfcee_id, p_cb->size_mask);
166}
167
168/*******************************************************************************
169**
170** Function         nfa_ee_update_route_aid_size
171**
172** Description      Update the size required for AID routing
173**                  of the given NFCEE ID.
174**
175** Returns          void
176**
177*******************************************************************************/
178static void nfa_ee_update_route_aid_size(tNFA_EE_ECB *p_cb)
179{
180    UINT8   *pa, len;
181    int     start_offset;
182    int     xx;
183
184    p_cb->size_aid  = 0;
185    if (p_cb->aid_entries)
186    {
187        start_offset = 0;
188        for (xx = 0; xx < p_cb->aid_entries; xx++)
189        {
190            /* add one AID entry */
191            if (p_cb->aid_rt_info[xx] & NFA_EE_AE_ROUTE)
192            {
193                pa      = &p_cb->aid_cfg[start_offset];
194                pa ++; /* EMV tag */
195                len     = *pa++; /* aid_len */
196                /* 4 = 1 (tag) + 1 (len) + 1(nfcee_id) + 1(power cfg) */
197                p_cb->size_aid  += 4;
198                p_cb->size_aid  += len;
199            }
200            start_offset += p_cb->aid_len[xx];
201        }
202    }
203    NFA_TRACE_DEBUG2 ("nfa_ee_update_route_aid_size nfcee_id:0x%x size_aid:%d", p_cb->nfcee_id, p_cb->size_aid);
204}
205
206/*******************************************************************************
207**
208** Function         nfa_ee_total_lmrt_size
209**
210** Description      the total listen mode routing table size
211**
212** Returns          UINT16
213**
214*******************************************************************************/
215static UINT16 nfa_ee_total_lmrt_size(void)
216{
217    int xx;
218    UINT16 lmrt_size = 0;
219    tNFA_EE_ECB          *p_cb;
220
221    p_cb = &nfa_ee_cb.ecb[NFA_EE_CB_4_DH];
222    lmrt_size += p_cb->size_mask;
223    lmrt_size += p_cb->size_aid;
224    p_cb = &nfa_ee_cb.ecb[nfa_ee_cb.cur_ee - 1];
225    for (xx = 0; xx < nfa_ee_cb.cur_ee; xx++, p_cb--)
226    {
227        if (p_cb->ee_status == NFC_NFCEE_STATUS_ACTIVE)
228        {
229            lmrt_size += p_cb->size_mask;
230            lmrt_size += p_cb->size_aid;
231        }
232    }
233    NFA_TRACE_DEBUG1 ("nfa_ee_total_lmrt_size size:%d", lmrt_size);
234    return lmrt_size;
235}
236
237/*******************************************************************************
238**
239** Function         nfa_ee_conn_cback
240**
241** Description      process connection callback event from stack
242**
243** Returns          void
244**
245*******************************************************************************/
246static void nfa_ee_conn_cback (UINT8 conn_id, tNFC_CONN_EVT event, tNFC_CONN *p_data)
247{
248    BT_HDR             *p_msg;
249    tNFA_EE_NCI_CONN    cbk;
250
251    NFA_TRACE_DEBUG2("nfa_ee_conn_cback: conn_id: %d, event=0x%02x", conn_id, event);
252
253    cbk.hdr.event   = NFA_EE_NCI_CONN_EVT;
254    if (event == NFC_DATA_CEVT)
255    {
256        /* Treat data event specially to avoid potential memory leak */
257        cbk.hdr.event   = NFA_EE_NCI_DATA_EVT;
258    }
259    cbk.conn_id     = conn_id;
260    cbk.event       = event;
261    cbk.p_data      = p_data;
262    p_msg           = (BT_HDR *)&cbk;
263
264    nfa_ee_evt_hdlr (p_msg);
265}
266
267
268/*******************************************************************************
269**
270** Function         nfa_ee_find_total_aid_len
271**
272** Description      Find the total len in aid_cfg from start_entry to the last
273**
274** Returns          void
275**
276*******************************************************************************/
277int nfa_ee_find_total_aid_len(tNFA_EE_ECB *p_cb, int start_entry)
278{
279    int len = 0, xx;
280
281    if (p_cb->aid_entries > start_entry)
282    {
283        for (xx = start_entry; xx < p_cb->aid_entries; xx++)
284        {
285            len += p_cb->aid_len[xx];
286        }
287    }
288    return len;
289}
290
291
292
293
294/*******************************************************************************
295**
296** Function         nfa_ee_find_aid_offset
297**
298** Description      Given the AID, find the associated tNFA_EE_ECB and the
299**                  offset in aid_cfg[]. *p_entry is the index.
300**
301** Returns          void
302**
303*******************************************************************************/
304tNFA_EE_ECB * nfa_ee_find_aid_offset(UINT8 aid_len, UINT8 *p_aid, int *p_offset, int *p_entry)
305{
306    int  xx, yy, aid_len_offset, offset;
307    tNFA_EE_ECB *p_ret = NULL, *p_ecb;
308
309    p_ecb = &nfa_ee_cb.ecb[NFA_EE_CB_4_DH];
310    aid_len_offset = 1; /* skip the tag */
311    for (yy = 0; yy < nfa_ee_cb.cur_ee; yy++, p_ecb++)
312    {
313        if (p_ecb->aid_entries)
314        {
315            offset = 0;
316            for (xx = 0; xx < p_ecb->aid_entries; xx++)
317            {
318                if (  (p_ecb->aid_cfg[offset + aid_len_offset] == aid_len)
319                    &&(memcmp(&p_ecb->aid_cfg[offset + aid_len_offset + 1], p_aid, aid_len) == 0)  )
320                {
321                    p_ret = p_ecb;
322                    if (p_offset)
323                        *p_offset = offset;
324                    if (p_entry)
325                        *p_entry  = xx;
326                    break;
327                }
328                offset += p_ecb->aid_len[xx];
329            }
330
331            if (p_ret)
332            {
333                /* found the entry already */
334                break;
335            }
336        }
337        p_ecb = &nfa_ee_cb.ecb[yy];
338    }
339
340    return p_ret;
341}
342
343/*******************************************************************************
344**
345** Function         nfa_ee_report_event
346**
347** Description      report the given event to the callback
348**
349** Returns          void
350**
351*******************************************************************************/
352void nfa_ee_report_event(tNFA_EE_CBACK *p_cback, tNFA_EE_EVT event, tNFA_EE_CBACK_DATA *p_data)
353{
354    int xx;
355
356    /* use the given callback, if not NULL */
357    if (p_cback)
358    {
359        (*p_cback)(event, p_data);
360        return;
361    }
362    /* if the given is NULL, report to all registered ones */
363    for (xx = 0; xx < NFA_EE_MAX_CBACKS; xx++)
364    {
365        if (nfa_ee_cb.p_ee_cback[xx] != NULL)
366        {
367            (*nfa_ee_cb.p_ee_cback[xx])(event, p_data);
368        }
369    }
370}
371/*******************************************************************************
372**
373** Function         nfa_ee_start_timer
374**
375** Description      start the de-bounce timer
376**
377** Returns          void
378**
379*******************************************************************************/
380void nfa_ee_start_timer(void)
381{
382    nfa_sys_start_timer(&nfa_ee_cb.timer, NFA_EE_ROUT_TIMEOUT_EVT, NFA_EE_ROUT_TIMEOUT_VAL);
383}
384
385/*******************************************************************************
386**
387** Function         nfa_ee_api_discover
388**
389** Description      process discover command from user
390**
391** Returns          void
392**
393*******************************************************************************/
394void nfa_ee_api_discover(tNFA_EE_MSG *p_data)
395{
396    tNFA_EE_CBACK *p_cback = p_data->ee_discover.p_cback;
397    tNFA_EE_CBACK_DATA  evt_data = {0};
398
399    NFA_TRACE_DEBUG1 ("nfa_ee_api_discover() in_use:%d", nfa_ee_cb.discv_timer.in_use);
400    if (nfa_ee_cb.discv_timer.in_use)
401    {
402        nfa_sys_stop_timer(&nfa_ee_cb.discv_timer);
403        NFC_NfceeDiscover(FALSE);
404    }
405    if (nfa_ee_cb.p_ee_disc_cback == NULL && NFC_NfceeDiscover(TRUE) == NFC_STATUS_OK)
406    {
407        nfa_ee_cb.p_ee_disc_cback   = p_cback;
408    }
409    else
410    {
411        evt_data.status = NFA_STATUS_FAILED;
412        nfa_ee_report_event (p_cback, NFA_EE_DISCOVER_EVT, &evt_data);
413    }
414}
415
416/*******************************************************************************
417**
418** Function         nfa_ee_api_register
419**
420** Description      process register command from user
421**
422** Returns          void
423**
424*******************************************************************************/
425void nfa_ee_api_register(tNFA_EE_MSG *p_data)
426{
427    int xx;
428    tNFA_EE_CBACK *p_cback = p_data->ee_register.p_cback;
429    tNFA_EE_CBACK_DATA  evt_data = {0};
430    BOOLEAN found = FALSE;
431
432    evt_data.ee_register = NFA_STATUS_FAILED;
433    /* loop through all entries to see if there's a matching callback */
434    for (xx = 0; xx < NFA_EE_MAX_CBACKS; xx++)
435    {
436        if (nfa_ee_cb.p_ee_cback[xx] == p_cback)
437        {
438            evt_data.ee_register        = NFA_STATUS_OK;
439            found                       = TRUE;
440            break;
441        }
442    }
443
444    /* If no matching callback, allocated an entry */
445    if (!found)
446    {
447        for (xx = 0; xx < NFA_EE_MAX_CBACKS; xx++)
448        {
449            if (nfa_ee_cb.p_ee_cback[xx] == NULL)
450            {
451                nfa_ee_cb.p_ee_cback[xx]    = p_cback;
452                evt_data.ee_register        = NFA_STATUS_OK;
453                break;
454            }
455        }
456    }
457    /* This callback is verified (not NULL) in NFA_EeRegister() */
458    (*p_cback)(NFA_EE_REGISTER_EVT, &evt_data);
459
460    /* report NFCEE Discovery Request collected during booting up */
461    nfa_ee_build_discover_req_evt (&evt_data.discover_req);
462    (*p_cback)(NFA_EE_DISCOVER_REQ_EVT, &evt_data);
463}
464
465/*******************************************************************************
466**
467** Function         nfa_ee_api_deregister
468**
469** Description      process de-register command from user
470**
471** Returns          void
472**
473*******************************************************************************/
474void nfa_ee_api_deregister(tNFA_EE_MSG *p_data)
475{
476    tNFA_EE_CBACK *p_cback = NULL;
477    int                 index  = p_data->deregister.index;
478    tNFA_EE_CBACK_DATA  evt_data = {0};
479
480    NFA_TRACE_DEBUG0 ("nfa_ee_api_deregister");
481    p_cback = nfa_ee_cb.p_ee_cback[index];
482    nfa_ee_cb.p_ee_cback[index] = NULL;
483    if (p_cback)
484        (*p_cback)(NFA_EE_DEREGISTER_EVT, &evt_data);
485}
486
487
488/*******************************************************************************
489**
490** Function         nfa_ee_api_mode_set
491**
492** Description      process mode set command from user
493**
494** Returns          void
495**
496*******************************************************************************/
497void nfa_ee_api_mode_set(tNFA_EE_MSG *p_data)
498{
499    tNFA_EE_ECB *p_cb= p_data->cfg_hdr.p_cb;
500
501    NFA_TRACE_DEBUG2 ("nfa_ee_api_mode_set() handle:0x%02x mode:%d", p_cb->nfcee_id, p_data->mode_set.mode);
502    NFC_NfceeModeSet (p_cb->nfcee_id, p_data->mode_set.mode);
503    /* set the NFA_EE_STATUS_PENDING bit to indicate the status is not exactly active */
504    if (p_data->mode_set.mode == NFC_MODE_ACTIVATE)
505        p_cb->ee_status = NFA_EE_STATUS_PENDING | NFA_EE_STATUS_ACTIVE;
506    else
507    {
508        p_cb->ee_status = NFA_EE_STATUS_INACTIVE;
509        /* DH should release the NCI connection before deactivate the NFCEE */
510        if (p_cb->conn_st == NFA_EE_CONN_ST_CONN)
511        {
512            p_cb->conn_st = NFA_EE_CONN_ST_DISC;
513            NFC_ConnClose(p_cb->conn_id);
514        }
515    }
516    /* report the NFA_EE_MODE_SET_EVT status on the response from NFCC */
517}
518
519
520
521/*******************************************************************************
522**
523** Function         nfa_ee_api_set_tech_cfg
524**
525** Description      process set technology routing configuration from user
526**                  start a 1 second timer. When the timer expires,
527**                  the configuration collected in control block is sent to NFCC
528**
529** Returns          void
530**
531*******************************************************************************/
532void nfa_ee_api_set_tech_cfg(tNFA_EE_MSG *p_data)
533{
534    tNFA_EE_ECB *p_cb = p_data->cfg_hdr.p_cb;
535    tNFA_EE_CBACK_DATA  evt_data = {0};
536    tNFA_TECHNOLOGY_MASK    old_tech_switch_on   = p_cb->tech_switch_on;
537    tNFA_TECHNOLOGY_MASK    old_tech_switch_off  = p_cb->tech_switch_off;
538    tNFA_TECHNOLOGY_MASK    old_tech_battery_off = p_cb->tech_battery_off;
539    UINT8                   old_size_mask        = p_cb->size_mask;
540
541    p_cb->tech_switch_on   = p_data->set_tech.technologies_switch_on;
542    p_cb->tech_switch_off  = p_data->set_tech.technologies_switch_off;
543    p_cb->tech_battery_off = p_data->set_tech.technologies_battery_off;
544    nfa_ee_update_route_size(p_cb);
545    if (nfa_ee_total_lmrt_size() > NFC_GetLmrtSize())
546    {
547        NFA_TRACE_ERROR0 ("nfa_ee_api_set_tech_cfg Exceed LMRT size");
548        evt_data.status        = NFA_STATUS_BUFFER_FULL;
549        p_cb->tech_switch_on   = old_tech_switch_on;
550        p_cb->tech_switch_off  = old_tech_switch_off;
551        p_cb->tech_battery_off = old_tech_battery_off;
552        p_cb->size_mask        = old_size_mask;
553    }
554    else
555    {
556        p_cb->ecb_flags       |= NFA_EE_ECB_FLAGS_TECH;
557        if (p_cb->tech_switch_on | p_cb->tech_switch_off | p_cb->tech_battery_off)
558        {
559            /* if any technology in any power mode is configured, mark this entry as configured */
560            nfa_ee_cb.ee_cfged    |= nfa_ee_ecb_to_mask(p_cb);
561        }
562        nfa_ee_start_timer();
563    }
564    nfa_ee_report_event (p_cb->p_ee_cback, NFA_EE_SET_TECH_CFG_EVT, &evt_data);
565}
566
567/*******************************************************************************
568**
569** Function         nfa_ee_api_set_proto_cfg
570**
571** Description      process set protocol routing configuration from user
572**                  start a 1 second timer. When the timer expires,
573**                  the configuration collected in control block is sent to NFCC
574**
575** Returns          void
576**
577*******************************************************************************/
578void nfa_ee_api_set_proto_cfg(tNFA_EE_MSG *p_data)
579{
580    tNFA_EE_ECB *p_cb = p_data->cfg_hdr.p_cb;
581    tNFA_EE_CBACK_DATA  evt_data = {0};
582    tNFA_PROTOCOL_MASK    old_proto_switch_on   = p_cb->proto_switch_on;
583    tNFA_PROTOCOL_MASK    old_proto_switch_off  = p_cb->proto_switch_off;
584    tNFA_PROTOCOL_MASK    old_proto_battery_off = p_cb->proto_battery_off;
585    UINT8                   old_size_mask        = p_cb->size_mask;
586
587    p_cb->proto_switch_on       = p_data->set_proto.protocols_switch_on;
588    p_cb->proto_switch_off      = p_data->set_proto.protocols_switch_off;
589    p_cb->proto_battery_off     = p_data->set_proto.protocols_battery_off;
590    nfa_ee_update_route_size(p_cb);
591    if (nfa_ee_total_lmrt_size() > NFC_GetLmrtSize())
592    {
593        NFA_TRACE_ERROR0 ("nfa_ee_api_set_proto_cfg Exceed LMRT size");
594        evt_data.status         = NFA_STATUS_BUFFER_FULL;
595        p_cb->proto_switch_on   = old_proto_switch_on;
596        p_cb->proto_switch_off  = old_proto_switch_off;
597        p_cb->proto_battery_off = old_proto_battery_off;
598        p_cb->size_mask         = old_size_mask;
599    }
600    else
601    {
602        p_cb->ecb_flags            |= NFA_EE_ECB_FLAGS_PROTO;
603        if (p_cb->proto_switch_on | p_cb->proto_switch_off | p_cb->proto_battery_off)
604        {
605            /* if any protocol in any power mode is configured, mark this entry as configured */
606            nfa_ee_cb.ee_cfged         |= nfa_ee_ecb_to_mask(p_cb);
607        }
608        nfa_ee_start_timer();
609    }
610    nfa_ee_report_event (p_cb->p_ee_cback, NFA_EE_SET_PROTO_CFG_EVT, &evt_data);
611}
612
613/*******************************************************************************
614**
615** Function         nfa_ee_api_add_aid
616**
617** Description      process add an AID routing configuration from user
618**                  start a 1 second timer. When the timer expires,
619**                  the configuration collected in control block is sent to NFCC
620**
621** Returns          void
622**
623*******************************************************************************/
624void nfa_ee_api_add_aid(tNFA_EE_MSG *p_data)
625{
626    tNFA_EE_API_ADD_AID *p_add = &p_data->add_aid;
627    tNFA_EE_ECB *p_cb = p_data->cfg_hdr.p_cb;
628    tNFA_EE_ECB *p_chk_cb;
629    UINT8   *p, *p_start;
630    int     len, len_needed;
631    tNFA_EE_CBACK_DATA  evt_data = {0};
632    int offset = 0, entry = 0;
633    UINT16  new_size;
634
635    nfa_ee_trace_aid ("nfa_ee_api_add_aid", p_cb->nfcee_id, p_add->aid_len, p_add->p_aid);
636    p_chk_cb = nfa_ee_find_aid_offset(p_add->aid_len, p_add->p_aid, &offset, &entry);
637    if (p_chk_cb)
638    {
639        NFA_TRACE_DEBUG0 ("nfa_ee_api_add_aid The AID entry is already in the database");
640        if (p_chk_cb == p_cb)
641        {
642            p_cb->aid_rt_info[entry]    |= NFA_EE_AE_ROUTE;
643            new_size = nfa_ee_total_lmrt_size();
644            if (new_size > NFC_GetLmrtSize())
645            {
646                NFA_TRACE_ERROR1 ("Exceed LMRT size:%d (add ROUTE)", new_size);
647                evt_data.status             = NFA_STATUS_BUFFER_FULL;
648                p_cb->aid_rt_info[entry]    &= ~NFA_EE_AE_ROUTE;
649            }
650            else
651            {
652                p_cb->aid_pwr_cfg[entry]     = p_add->power_state;
653            }
654        }
655        else
656        {
657            NFA_TRACE_ERROR1 ("The AID entry is already in the database for different NFCEE ID:0x%02x", p_chk_cb->nfcee_id);
658            evt_data.status = NFA_STATUS_SEMANTIC_ERROR;
659        }
660    }
661    else
662    {
663        /* Find the total length so far */
664        len = nfa_ee_find_total_aid_len(p_cb, 0);
665
666        /* make sure the control block has enough room to hold this entry */
667        len_needed  = p_add->aid_len + 2; /* tag/len */
668
669        if ((len_needed + len) > NFA_EE_MAX_AID_CFG_LEN)
670        {
671            NFA_TRACE_ERROR3 ("Exceed capacity: (len_needed:%d + len:%d) > NFA_EE_MAX_AID_CFG_LEN:%d", len_needed, len, NFA_EE_MAX_AID_CFG_LEN);
672            evt_data.status = NFA_STATUS_BUFFER_FULL;
673        }
674        else if (p_cb->aid_entries < NFA_EE_MAX_AID_ENTRIES)
675        {
676            new_size = nfa_ee_total_lmrt_size() + 4 + p_add->aid_len; /* 4 = 1 (tag) + 1 (len) + 1(nfcee_id) + 1(power cfg) */
677            if (new_size > NFC_GetLmrtSize())
678            {
679                NFA_TRACE_ERROR1 ("Exceed LMRT size:%d", new_size);
680                evt_data.status        = NFA_STATUS_BUFFER_FULL;
681            }
682            else
683            {
684                /* add AID */
685                p_cb->aid_pwr_cfg[p_cb->aid_entries]    = p_add->power_state;
686                p_cb->aid_rt_info[p_cb->aid_entries]    = NFA_EE_AE_ROUTE;
687                p       = p_cb->aid_cfg + len;
688                p_start = p;
689                *p++    = NFA_EE_AID_CFG_TAG_NAME;
690                *p++    = p_add->aid_len;
691                memcpy(p, p_add->p_aid, p_add->aid_len);
692                p      += p_add->aid_len;
693
694                p_cb->aid_len[p_cb->aid_entries++]     = (UINT8)(p - p_start);
695            }
696        }
697        else
698        {
699            NFA_TRACE_ERROR1 ("Exceed NFA_EE_MAX_AID_ENTRIES:%d", NFA_EE_MAX_AID_ENTRIES);
700            evt_data.status = NFA_STATUS_BUFFER_FULL;
701        }
702    }
703
704    if (evt_data.status == NFA_STATUS_OK)
705    {
706        /* mark AID changed */
707        p_cb->ecb_flags                       |= NFA_EE_ECB_FLAGS_AID;
708        nfa_ee_cb.ee_cfged                    |= nfa_ee_ecb_to_mask(p_cb);
709        nfa_ee_update_route_aid_size(p_cb);
710        nfa_ee_start_timer();
711    }
712    NFA_TRACE_DEBUG2 ("status:%d ee_cfged:0x%02x ",evt_data.status, nfa_ee_cb.ee_cfged);
713    /* report the status of this operation */
714    nfa_ee_report_event (p_cb->p_ee_cback, NFA_EE_ADD_AID_EVT, &evt_data);
715}
716
717/*******************************************************************************
718**
719** Function         nfa_ee_api_remove_aid
720**
721** Description      process remove an AID routing configuration from user
722**                  start a 1 second timer. When the timer expires,
723**                  the configuration collected in control block is sent to NFCC
724**
725** Returns          void
726**
727*******************************************************************************/
728void nfa_ee_api_remove_aid(tNFA_EE_MSG *p_data)
729{
730    tNFA_EE_ECB  *p_cb;
731    tNFA_EE_CBACK_DATA  evt_data = {0};
732    int offset = 0, entry = 0, len;
733    int rest_len;
734    tNFA_EE_CBACK *p_cback = NULL;
735
736    nfa_ee_trace_aid ("nfa_ee_api_remove_aid", 0, p_data->rm_aid.aid_len, p_data->rm_aid.p_aid);
737    p_cb = nfa_ee_find_aid_offset(p_data->rm_aid.aid_len, p_data->rm_aid.p_aid, &offset, &entry);
738    if (p_cb && p_cb->aid_entries)
739    {
740        NFA_TRACE_DEBUG2 ("aid_rt_info[%d]: 0x%02x", entry, p_cb->aid_rt_info[entry]);
741        /* mark routing and VS changed */
742        if (p_cb->aid_rt_info[entry] & NFA_EE_AE_ROUTE)
743            p_cb->ecb_flags         |= NFA_EE_ECB_FLAGS_AID;
744
745        if (p_cb->aid_rt_info[entry] & NFA_EE_AE_VS)
746            p_cb->ecb_flags         |= NFA_EE_ECB_FLAGS_VS;
747
748        /* remove the aid */
749        if ((entry+1) < p_cb->aid_entries)
750        {
751            /* not the last entry, move the aid entries in control block */
752            /* Find the total len from the next entry to the last one */
753            rest_len = nfa_ee_find_total_aid_len(p_cb, entry + 1);
754
755            len = p_cb->aid_len[entry];
756            NFA_TRACE_DEBUG2 ("nfa_ee_api_remove_aid len:%d, rest_len:%d", len, rest_len);
757            GKI_shiftup (&p_cb->aid_cfg[offset], &p_cb->aid_cfg[offset+ len], rest_len);
758            rest_len = p_cb->aid_entries - entry;
759            GKI_shiftup (&p_cb->aid_len[entry], &p_cb->aid_len[entry + 1], rest_len);
760            GKI_shiftup (&p_cb->aid_pwr_cfg[entry], &p_cb->aid_pwr_cfg[entry + 1], rest_len);
761            GKI_shiftup (&p_cb->aid_rt_info[entry], &p_cb->aid_rt_info[entry + 1], rest_len);
762        }
763        /* else the last entry, just reduce the aid_entries by 1 */
764        p_cb->aid_entries--;
765        nfa_ee_cb.ee_cfged      |= nfa_ee_ecb_to_mask(p_cb);
766        nfa_ee_update_route_aid_size(p_cb);
767        nfa_ee_start_timer();
768        /* report NFA_EE_REMOVE_AID_EVT to the callback associated the NFCEE */
769        p_cback = p_cb->p_ee_cback;
770    }
771    else
772    {
773        NFA_TRACE_ERROR0 ("nfa_ee_api_remove_aid The AID entry is not in the database");
774        evt_data.status = NFA_STATUS_INVALID_PARAM;
775    }
776    nfa_ee_report_event (p_cback, NFA_EE_REMOVE_AID_EVT, &evt_data);
777}
778
779/*******************************************************************************
780**
781** Function         nfa_ee_api_update_now
782**
783** Description      Initiates connection creation process to the given NFCEE
784**
785** Returns          void
786**
787*******************************************************************************/
788void nfa_ee_api_update_now(tNFA_EE_MSG *p_data)
789{
790    nfa_sys_stop_timer(&nfa_ee_cb.timer);
791    nfa_ee_cb.ee_cfged  |= NFA_EE_CFGED_UPDATE_NOW;
792    nfa_ee_rout_timeout(p_data);
793}
794
795/*******************************************************************************
796**
797** Function         nfa_ee_api_connect
798**
799** Description      Initiates connection creation process to the given NFCEE
800**
801** Returns          void
802**
803*******************************************************************************/
804void nfa_ee_api_connect(tNFA_EE_MSG *p_data)
805{
806    tNFA_EE_ECB  *p_cb = p_data->connect.p_cb;
807    int xx;
808    tNFA_EE_CBACK_DATA  evt_data = {0};
809
810    evt_data.connect.status       = NFA_STATUS_FAILED;
811    if (p_cb->conn_st == NFA_EE_CONN_ST_NONE)
812    {
813        for (xx = 0; xx < p_cb->num_interface; xx++)
814        {
815            if (p_data->connect.ee_interface == p_cb->ee_interface[xx])
816            {
817                p_cb->p_ee_cback        = p_data->connect.p_cback;
818                p_cb->conn_st           = NFA_EE_CONN_ST_WAIT;
819                p_cb->use_interface     = p_data->connect.ee_interface;
820                evt_data.connect.status = NFC_ConnCreate(NCI_DEST_TYPE_NFCEE, p_data->connect.nfcee_id,
821                    p_data->connect.ee_interface, nfa_ee_conn_cback);
822                /* report the NFA_EE_CONNECT_EVT status on the response from NFCC */
823                break;
824            }
825        }
826    }
827
828    if (evt_data.connect.status != NCI_STATUS_OK)
829    {
830        evt_data.connect.ee_handle    = (tNFA_HANDLE)p_data->connect.nfcee_id | NFA_HANDLE_GROUP_EE;
831        evt_data.connect.status       = NFA_STATUS_INVALID_PARAM;
832        evt_data.connect.ee_interface = p_data->connect.ee_interface;
833        nfa_ee_report_event (p_data->connect.p_cback, NFA_EE_CONNECT_EVT, &evt_data);
834    }
835}
836
837/*******************************************************************************
838**
839** Function         nfa_ee_api_send_data
840**
841** Description      Send the given data packet to the given NFCEE
842**
843** Returns          void
844**
845*******************************************************************************/
846void nfa_ee_api_send_data(tNFA_EE_MSG *p_data)
847{
848    tNFA_EE_ECB  *p_cb = p_data->send_data.p_cb;
849    BT_HDR *p_pkt;
850    UINT16 size = NCI_MSG_OFFSET_SIZE + NCI_DATA_HDR_SIZE + p_data->send_data.data_len + BT_HDR_SIZE;
851    UINT8  *p;
852    tNFA_STATUS status = NFA_STATUS_FAILED;
853
854    if (p_cb->conn_st == NFA_EE_CONN_ST_CONN)
855    {
856        p_pkt = (BT_HDR *)GKI_getbuf(size);
857        if (p_pkt)
858        {
859            p_pkt->offset   = NCI_MSG_OFFSET_SIZE + NCI_DATA_HDR_SIZE;
860            p_pkt->len      = p_data->send_data.data_len;
861            p               = (UINT8 *)(p_pkt+1) + p_pkt->offset;
862            memcpy(p, p_data->send_data.p_data, p_pkt->len);
863            NFC_SendData (p_cb->conn_id, p_pkt);
864        }
865        else
866        {
867            nfa_ee_report_event( p_cb->p_ee_cback, NFA_EE_NO_MEM_ERR_EVT, (tNFA_EE_CBACK_DATA *)&status);
868        }
869    }
870    else
871    {
872        nfa_ee_report_event( p_cb->p_ee_cback, NFA_EE_NO_CB_ERR_EVT, (tNFA_EE_CBACK_DATA *)&status);
873    }
874}
875
876/*******************************************************************************
877**
878** Function         nfa_ee_api_disconnect
879**
880** Description      Initiates closing of the connection to the given NFCEE
881**
882** Returns          void
883**
884*******************************************************************************/
885void nfa_ee_api_disconnect(tNFA_EE_MSG *p_data)
886{
887    tNFA_EE_ECB  *p_cb = p_data->disconnect.p_cb;
888    tNFA_EE_CBACK_DATA  evt_data = {0};
889
890    if (p_cb->conn_st == NFA_EE_CONN_ST_CONN)
891    {
892        p_cb->conn_st = NFA_EE_CONN_ST_DISC;
893        NFC_ConnClose(p_cb->conn_id);
894    }
895    evt_data.handle = (tNFA_HANDLE)p_cb->nfcee_id | NFA_HANDLE_GROUP_EE;
896    nfa_ee_report_event(p_cb->p_ee_cback, NFA_EE_DISCONNECT_EVT, &evt_data);
897}
898
899/*******************************************************************************
900**
901** Function         nfa_ee_report_disc_done
902**
903** Description      Process the callback for NFCEE discovery response
904**
905** Returns          void
906**
907*******************************************************************************/
908void nfa_ee_report_disc_done(BOOLEAN notify_enable_done)
909{
910    tNFA_EE_CBACK           *p_cback;
911    tNFA_EE_CBACK_DATA      evt_data = {0};
912
913    NFA_TRACE_DEBUG3("nfa_ee_report_disc_done() em_state:%d num_ee_expecting:%d notify_enable_done:%d", nfa_ee_cb.em_state, nfa_ee_cb.num_ee_expecting, notify_enable_done);
914    if (nfa_ee_cb.num_ee_expecting == 0)
915    {
916        if (notify_enable_done)
917        {
918            if (nfa_ee_cb.em_state == NFA_EE_EM_STATE_INIT_DONE)
919            {
920                nfa_sys_cback_notify_enable_complete (NFA_ID_EE);
921                if (nfa_ee_cb.p_enable_cback)
922                    (*nfa_ee_cb.p_enable_cback)(NFA_EE_DISC_STS_ON);
923            }
924            else if ((nfa_ee_cb.em_state == NFA_EE_EM_STATE_RESTORING) && (nfa_ee_cb.ee_flags & NFA_EE_FLAG_NOTIFY_HCI) )
925            {
926                nfa_ee_cb.ee_flags   &= ~NFA_EE_FLAG_NOTIFY_HCI;
927                if (nfa_ee_cb.p_enable_cback)
928                    (*nfa_ee_cb.p_enable_cback)(NFA_EE_DISC_STS_ON);
929            }
930        }
931
932
933        if (nfa_ee_cb.p_ee_disc_cback)
934        {
935            /* notify API callback */
936            p_cback                         = nfa_ee_cb.p_ee_disc_cback;
937            nfa_ee_cb.p_ee_disc_cback       = NULL;
938            evt_data.status                         = NFA_STATUS_OK;
939            evt_data.ee_discover.num_ee             = NFA_EE_MAX_EE_SUPPORTED;
940            NFA_EeGetInfo(&evt_data.ee_discover.num_ee, evt_data.ee_discover.ee_info);
941            nfa_ee_report_event (p_cback, NFA_EE_DISCOVER_EVT, &evt_data);
942        }
943    }
944}
945
946/*******************************************************************************
947**
948** Function         nfa_ee_restore_ntf_done
949**
950** Description      check if any ee_status still has NFA_EE_STATUS_PENDING bit
951**
952** Returns          TRUE, if all NFA_EE_STATUS_PENDING bits are removed
953**
954*******************************************************************************/
955BOOLEAN nfa_ee_restore_ntf_done(void)
956{
957    tNFA_EE_ECB     *p_cb;
958    BOOLEAN         is_done = TRUE;
959    int             xx;
960
961    p_cb = nfa_ee_cb.ecb;
962    for (xx = 0; xx < nfa_ee_cb.cur_ee; xx++, p_cb++)
963    {
964        if ((p_cb->nfcee_id != NFA_EE_INVALID) && (p_cb->ee_old_status & NFA_EE_STATUS_RESTORING))
965        {
966            is_done = FALSE;
967            break;
968        }
969    }
970    return is_done;
971}
972
973/*******************************************************************************
974**
975** Function         nfa_ee_remove_pending
976**
977** Description      check if any ee_status still has NFA_EE_STATUS_RESTORING bit
978**
979** Returns          TRUE, if all NFA_EE_STATUS_RESTORING bits are removed
980**
981*******************************************************************************/
982static void nfa_ee_remove_pending(void)
983{
984    tNFA_EE_ECB     *p_cb;
985    tNFA_EE_ECB     *p_cb_n, *p_cb_end;
986    int             xx, num_removed = 0;
987    int             first_removed = NFA_EE_MAX_EE_SUPPORTED;
988
989    p_cb = nfa_ee_cb.ecb;
990    for (xx = 0; xx < nfa_ee_cb.cur_ee; xx++, p_cb++)
991    {
992        if ((p_cb->nfcee_id != NFA_EE_INVALID) && (p_cb->ee_status & NFA_EE_STATUS_RESTORING))
993        {
994            p_cb->nfcee_id  = NFA_EE_INVALID;
995            num_removed ++;
996            if (first_removed == NFA_EE_MAX_EE_SUPPORTED)
997                first_removed   = xx;
998        }
999    }
1000
1001    NFA_TRACE_DEBUG3("nfa_ee_remove_pending() cur_ee:%d, num_removed:%d first_removed:%d", nfa_ee_cb.cur_ee, num_removed, first_removed);
1002    if (num_removed && (first_removed != (nfa_ee_cb.cur_ee - num_removed)))
1003    {
1004        /* if the removes ECB entried are not at the end, move the entries up */
1005        p_cb_end = &nfa_ee_cb.ecb[nfa_ee_cb.cur_ee - 1];
1006        p_cb = &nfa_ee_cb.ecb[first_removed];
1007        for (p_cb_n = p_cb + 1; p_cb_n <= p_cb_end;)
1008        {
1009            while ((p_cb_n->nfcee_id == NFA_EE_INVALID) && (p_cb_n <= p_cb_end))
1010            {
1011                p_cb_n++;
1012            }
1013
1014            if (p_cb_n <= p_cb_end)
1015            {
1016                memcpy(p_cb, p_cb_n, sizeof(tNFA_EE_ECB));
1017                p_cb_n->nfcee_id = NFA_EE_INVALID;
1018            }
1019            p_cb++;
1020            p_cb_n++;
1021        }
1022    }
1023    nfa_ee_cb.cur_ee -= (UINT8)num_removed;
1024}
1025
1026
1027/*******************************************************************************
1028**
1029** Function         nfa_ee_nci_disc_rsp
1030**
1031** Description      Process the callback for NFCEE discovery response
1032**
1033** Returns          void
1034**
1035*******************************************************************************/
1036void nfa_ee_nci_disc_rsp(tNFA_EE_MSG *p_data)
1037{
1038    tNFC_NFCEE_DISCOVER_REVT    *p_evt = p_data->disc_rsp.p_data;
1039    tNFA_EE_ECB              *p_cb;
1040    UINT8   xx;
1041    UINT8   num_nfcee = p_evt->num_nfcee;
1042    BOOLEAN notify_enable_done = FALSE;
1043
1044    NFA_TRACE_DEBUG3("nfa_ee_nci_disc_rsp() em_state:%d cur_ee:%d, num_nfcee:%d", nfa_ee_cb.em_state, nfa_ee_cb.cur_ee, num_nfcee);
1045    switch (nfa_ee_cb.em_state)
1046    {
1047    case NFA_EE_EM_STATE_INIT:
1048        nfa_ee_cb.cur_ee            = 0;
1049        nfa_ee_cb.num_ee_expecting  = 0;
1050        if (num_nfcee == 0)
1051        {
1052            nfa_ee_cb.em_state = NFA_EE_EM_STATE_INIT_DONE;
1053            notify_enable_done = TRUE;
1054            if (p_evt->status != NFC_STATUS_OK)
1055            {
1056                nfa_sys_stop_timer(&nfa_ee_cb.discv_timer);
1057            }
1058        }
1059        break;
1060
1061    case NFA_EE_EM_STATE_INIT_DONE:
1062        if (num_nfcee)
1063        {
1064            /* if this is initiated by api function,
1065             * check if the number of NFCEE expected is more than what's currently in CB */
1066            if (num_nfcee > NFA_EE_MAX_EE_SUPPORTED)
1067                num_nfcee = NFA_EE_MAX_EE_SUPPORTED;
1068            if (nfa_ee_cb.cur_ee < num_nfcee)
1069            {
1070                p_cb = &nfa_ee_cb.ecb[nfa_ee_cb.cur_ee];
1071                for (xx = nfa_ee_cb.cur_ee; xx < num_nfcee; xx++, p_cb++)
1072                {
1073                    /* mark the new entries as a new one */
1074                    p_cb->nfcee_id = NFA_EE_INVALID;
1075                }
1076            }
1077            nfa_ee_cb.cur_ee = num_nfcee;
1078        }
1079        break;
1080
1081    case NFA_EE_EM_STATE_RESTORING:
1082        if (num_nfcee == 0)
1083        {
1084            nfa_ee_cb.em_state = NFA_EE_EM_STATE_INIT_DONE;
1085            nfa_ee_remove_pending();
1086            nfa_ee_check_restore_complete();
1087            if (p_evt->status != NFC_STATUS_OK)
1088            {
1089                nfa_sys_stop_timer(&nfa_ee_cb.discv_timer);
1090            }
1091        }
1092        break;
1093    }
1094
1095    if (p_evt->status == NFC_STATUS_OK)
1096    {
1097        nfa_ee_cb.num_ee_expecting = p_evt->num_nfcee;
1098        if (nfa_ee_cb.num_ee_expecting > NFA_EE_MAX_EE_SUPPORTED)
1099        {
1100            NFA_TRACE_ERROR2 ("NFA-EE num_ee_expecting:%d > max:%d", nfa_ee_cb.num_ee_expecting, NFA_EE_MAX_EE_SUPPORTED);
1101        }
1102    }
1103    nfa_ee_report_disc_done(notify_enable_done);
1104    NFA_TRACE_DEBUG3("nfa_ee_nci_disc_rsp() em_state:%d cur_ee:%d num_ee_expecting:%d", nfa_ee_cb.em_state, nfa_ee_cb.cur_ee, nfa_ee_cb.num_ee_expecting);
1105}
1106
1107/*******************************************************************************
1108**
1109** Function         nfa_ee_nci_disc_ntf
1110**
1111** Description      Process the callback for NFCEE discovery notification
1112**
1113** Returns          void
1114**
1115*******************************************************************************/
1116void nfa_ee_nci_disc_ntf(tNFA_EE_MSG *p_data)
1117{
1118    tNFC_NFCEE_INFO_REVT    *p_ee = p_data->disc_ntf.p_data;
1119    tNFA_EE_ECB             *p_cb = NULL;
1120    BOOLEAN                 notify_enable_done = FALSE;
1121    BOOLEAN                 notify_new_ee = FALSE;
1122    tNFA_EE_CBACK_DATA      evt_data = {0};
1123    tNFA_EE_INFO            *p_info;
1124    tNFA_EE_EM_STATE        new_em_state = NFA_EE_EM_STATE_MAX;
1125
1126    NFA_TRACE_DEBUG4("nfa_ee_nci_disc_ntf() em_state:%d ee_flags:0x%x cur_ee:%d num_ee_expecting:%d", nfa_ee_cb.em_state, nfa_ee_cb.ee_flags, nfa_ee_cb.cur_ee, nfa_ee_cb.num_ee_expecting);
1127    if (nfa_ee_cb.num_ee_expecting)
1128    {
1129        nfa_ee_cb.num_ee_expecting--;
1130        if ((nfa_ee_cb.num_ee_expecting == 0) && (nfa_ee_cb.p_ee_disc_cback != NULL))
1131        {
1132            /* Discovery triggered by API function */
1133            NFC_NfceeDiscover(FALSE);
1134        }
1135    }
1136    switch (nfa_ee_cb.em_state)
1137    {
1138    case NFA_EE_EM_STATE_INIT:
1139        if (nfa_ee_cb.cur_ee < NFA_EE_MAX_EE_SUPPORTED)
1140        {
1141            /* the cb can collect up to NFA_EE_MAX_EE_SUPPORTED ee_info */
1142            p_cb = &nfa_ee_cb.ecb[nfa_ee_cb.cur_ee++];
1143        }
1144
1145        if (nfa_ee_cb.num_ee_expecting == 0)
1146        {
1147            /* notify init_done callback */
1148            nfa_ee_cb.em_state = NFA_EE_EM_STATE_INIT_DONE;
1149            notify_enable_done = TRUE;
1150        }
1151        break;
1152
1153    case NFA_EE_EM_STATE_INIT_DONE:
1154        p_cb = nfa_ee_find_ecb (p_ee->nfcee_id);
1155        if (p_cb == NULL)
1156        {
1157            /* the NFCEE ID is not in the last NFCEE discovery
1158             * maybe it's a new one */
1159            p_cb = nfa_ee_find_ecb (NFA_EE_INVALID);
1160            if (p_cb)
1161            {
1162                nfa_ee_cb.cur_ee++;
1163                notify_new_ee = TRUE;
1164            }
1165        }
1166        else if (p_cb->ecb_flags & NFA_EE_ECB_FLAGS_ORDER)
1167        {
1168            nfa_ee_cb.cur_ee++;
1169            notify_new_ee = TRUE;
1170        }
1171        else
1172        {
1173            NFA_TRACE_DEBUG3 ("cur_ee:%d ecb_flags=0x%02x  ee_status=0x%x", nfa_ee_cb.cur_ee, p_cb->ecb_flags, p_cb->ee_status);
1174        }
1175        break;
1176
1177    case NFA_EE_EM_STATE_RESTORING:
1178        p_cb = nfa_ee_find_ecb (p_ee->nfcee_id);
1179        if (p_cb == NULL)
1180        {
1181            /* the NFCEE ID is not in the last NFCEE discovery
1182             * maybe it's a new one */
1183            p_cb = nfa_ee_find_ecb (NFA_EE_INVALID);
1184            if (p_cb)
1185            {
1186                nfa_ee_cb.cur_ee++;
1187                notify_new_ee = TRUE;
1188            }
1189        }
1190        if (nfa_ee_cb.num_ee_expecting == 0)
1191        {
1192            /* notify init_done callback */
1193            notify_enable_done = TRUE;
1194            if (nfa_ee_restore_ntf_done())
1195            {
1196                new_em_state       = NFA_EE_EM_STATE_INIT_DONE;
1197            }
1198        }
1199        break;
1200    }
1201    NFA_TRACE_DEBUG1 ("nfa_ee_nci_disc_ntf cur_ee:%d", nfa_ee_cb.cur_ee);
1202
1203    if (p_cb)
1204    {
1205        p_cb->nfcee_id      = p_ee->nfcee_id;
1206        p_cb->ee_status     = p_ee->ee_status;
1207        p_cb->num_interface = p_ee->num_interface;
1208        memcpy(p_cb->ee_interface, p_ee->ee_interface, p_ee->num_interface);
1209        p_cb->num_tlvs      = p_ee->num_tlvs;
1210        memcpy(p_cb->ee_tlv, p_ee->ee_tlv, p_ee->num_tlvs * sizeof(tNFA_EE_TLV));
1211
1212        if (nfa_ee_cb.em_state == NFA_EE_EM_STATE_RESTORING)
1213        {
1214            /* NCI spec says: An NFCEE_DISCOVER_NTF that contains a Protocol type of "HCI Access"
1215             * SHALL NOT contain any other additional Protocol
1216             * i.e. check only first supported NFCEE interface is HCI access */
1217            /* NFA_HCI module handles restoring configurations for HCI access */
1218            if (p_cb->ee_interface[0] != NFC_NFCEE_INTERFACE_HCI_ACCESS)
1219            {
1220                if ((nfa_ee_cb.ee_flags & NFA_EE_FLAG_WAIT_HCI) == 0)
1221                {
1222                    nfa_ee_restore_one_ecb (p_cb);
1223                }
1224                /* else wait for NFA-HCI module to restore the HCI network information before enabling the NFCEE */
1225            }
1226        }
1227
1228        if ((nfa_ee_cb.p_ee_disc_cback == NULL) && (notify_new_ee == TRUE))
1229        {
1230            if (nfa_dm_is_active() && (p_cb->ee_status != NFA_EE_STATUS_REMOVED))
1231            {
1232                /* report this NFA_EE_NEW_EE_EVT only after NFA_DM_ENABLE_EVT is reported */
1233                p_info                  = &evt_data.new_ee;
1234                p_info->ee_handle       = NFA_HANDLE_GROUP_EE | (tNFA_HANDLE)p_cb->nfcee_id;
1235                p_info->ee_status       = p_cb->ee_status;
1236                p_info->num_interface   = p_cb->num_interface;
1237                p_info->num_tlvs        = p_cb->num_tlvs;
1238                memcpy(p_info->ee_interface, p_cb->ee_interface, p_cb->num_interface);
1239                memcpy(p_info->ee_tlv, p_cb->ee_tlv, p_cb->num_tlvs * sizeof(tNFA_EE_TLV));
1240                nfa_ee_report_event (NULL, NFA_EE_NEW_EE_EVT, &evt_data);
1241            }
1242        }
1243        else
1244            nfa_ee_report_disc_done(notify_enable_done);
1245
1246        if (p_cb->ecb_flags & NFA_EE_ECB_FLAGS_ORDER)
1247        {
1248            NFA_TRACE_DEBUG0 ("NFA_EE_ECB_FLAGS_ORDER");
1249            p_cb->ecb_flags &= ~NFA_EE_ECB_FLAGS_ORDER;
1250            nfa_ee_report_discover_req_evt();
1251        }
1252
1253    }
1254
1255    if (new_em_state != NFA_EE_EM_STATE_MAX)
1256    {
1257        nfa_ee_cb.em_state = new_em_state;
1258        nfa_ee_check_restore_complete();
1259    }
1260
1261    if ((nfa_ee_cb.cur_ee == nfa_ee_max_ee_cfg) && (nfa_ee_cb.em_state == NFA_EE_EM_STATE_INIT_DONE) )
1262    {
1263        if (nfa_ee_cb.discv_timer.in_use)
1264        {
1265            nfa_sys_stop_timer (&nfa_ee_cb.discv_timer);
1266            p_data->hdr.event = NFA_EE_DISCV_TIMEOUT_EVT;
1267            nfa_ee_evt_hdlr((BT_HDR *)p_data);
1268        }
1269    }
1270}
1271
1272/*******************************************************************************
1273**
1274** Function         nfa_ee_check_restore_complete
1275**
1276** Description      Check if restore the NFA-EE related configuration to the
1277**                  state prior to low power mode is complete.
1278**                  If complete, notify sys.
1279**
1280** Returns          void
1281**
1282*******************************************************************************/
1283void nfa_ee_check_restore_complete(void)
1284{
1285    UINT32  xx;
1286    tNFA_EE_ECB     *p_cb;
1287    BOOLEAN         proc_complete = TRUE;
1288
1289    p_cb = nfa_ee_cb.ecb;
1290    for (xx = 0; xx < nfa_ee_cb.cur_ee; xx++, p_cb++)
1291    {
1292        if (p_cb->ecb_flags & NFA_EE_ECB_FLAGS_RESTORE)
1293        {
1294            /* NFA_HCI module handles restoring configurations for HCI access.
1295             * ignore the restoring status for HCI Access */
1296            if (p_cb->ee_interface[0] != NFC_NFCEE_INTERFACE_HCI_ACCESS)
1297            {
1298                proc_complete = FALSE;
1299                break;
1300            }
1301        }
1302    }
1303
1304    NFA_TRACE_DEBUG2 ("nfa_ee_check_restore_complete nfa_ee_cb.ee_cfg_sts:0x%02x proc_complete:%d", nfa_ee_cb.ee_cfg_sts, proc_complete);
1305    if (proc_complete)
1306    {
1307        /* update routing table when NFA_EE_ROUT_TIMEOUT_EVT is received */
1308        if (nfa_ee_cb.ee_cfg_sts & NFA_EE_STS_PREV_ROUTING)
1309            nfa_ee_api_update_now(NULL);
1310
1311        nfa_ee_cb.em_state = NFA_EE_EM_STATE_INIT_DONE;
1312        nfa_sys_cback_notify_nfcc_power_mode_proc_complete (NFA_ID_EE);
1313    }
1314}
1315
1316/*******************************************************************************
1317**
1318** Function         nfa_ee_build_discover_req_evt
1319**
1320** Description      Build NFA_EE_DISCOVER_REQ_EVT for all active NFCEE
1321**
1322** Returns          void
1323**
1324*******************************************************************************/
1325static void nfa_ee_build_discover_req_evt (tNFA_EE_DISCOVER_REQ *p_evt_data)
1326{
1327    tNFA_EE_ECB           *p_cb;
1328    tNFA_EE_DISCOVER_INFO *p_info;
1329    UINT8                 xx;
1330
1331    if (!p_evt_data)
1332        return;
1333
1334    p_evt_data->num_ee = 0;
1335    p_cb               = nfa_ee_cb.ecb;
1336    p_info             = p_evt_data->ee_disc_info;
1337
1338    for (xx = 0; xx < nfa_ee_cb.cur_ee; xx++, p_cb++)
1339    {
1340        if (  (p_cb->ee_status & NFA_EE_STATUS_INT_MASK)
1341            ||(p_cb->ee_status != NFA_EE_STATUS_ACTIVE)
1342            ||((p_cb->ecb_flags & NFA_EE_ECB_FLAGS_DISC_REQ) == 0)  )
1343        {
1344            continue;
1345        }
1346        p_info->ee_handle       = (tNFA_HANDLE)p_cb->nfcee_id | NFA_HANDLE_GROUP_EE;
1347        p_info->la_protocol     = p_cb->la_protocol;
1348        p_info->lb_protocol     = p_cb->lb_protocol;
1349        p_info->lf_protocol     = p_cb->lf_protocol;
1350        p_info->lbp_protocol    = p_cb->lbp_protocol;
1351        p_evt_data->num_ee++;
1352        p_info++;
1353
1354        NFA_TRACE_DEBUG6 ("[%d] ee_handle:0x%x, listen protocol A:%d, B:%d, F:%d, BP:%d",
1355                          p_evt_data->num_ee, p_cb->nfcee_id,
1356                          p_cb->la_protocol, p_cb->lb_protocol, p_cb->lf_protocol, p_cb->lbp_protocol);
1357    }
1358
1359    p_evt_data->status     = NFA_STATUS_OK;
1360}
1361
1362/*******************************************************************************
1363**
1364** Function         nfa_ee_report_discover_req_evt
1365**
1366** Description      Report NFA_EE_DISCOVER_REQ_EVT for all active NFCEE
1367**
1368** Returns          void
1369**
1370*******************************************************************************/
1371static void nfa_ee_report_discover_req_evt(void)
1372{
1373    tNFA_EE_DISCOVER_REQ    evt_data;
1374
1375    if (nfa_ee_cb.p_enable_cback)
1376        (*nfa_ee_cb.p_enable_cback) (NFA_EE_DISC_STS_REQ);
1377
1378
1379    /* if this is restoring NFCC */
1380    if (!nfa_dm_is_active ())
1381    {
1382        NFA_TRACE_DEBUG0 ("nfa_ee_report_discover_req_evt DM is not active");
1383        return;
1384    }
1385
1386    nfa_ee_build_discover_req_evt (&evt_data);
1387    nfa_ee_report_event(NULL, NFA_EE_DISCOVER_REQ_EVT, (tNFA_EE_CBACK_DATA *)&evt_data);
1388}
1389
1390/*******************************************************************************
1391**
1392** Function         nfa_ee_nci_mode_set_rsp
1393**
1394** Description      Process the result for NFCEE ModeSet response
1395**
1396** Returns          void
1397**
1398*******************************************************************************/
1399void nfa_ee_nci_mode_set_rsp(tNFA_EE_MSG *p_data)
1400{
1401    tNFA_EE_ECB *p_cb;
1402    tNFA_EE_MODE_SET    mode_set;
1403    tNFC_NFCEE_MODE_SET_REVT    *p_rsp = p_data->mode_set_rsp.p_data;
1404
1405    NFA_TRACE_DEBUG2 ("nfa_ee_nci_mode_set_rsp() handle:0x%02x mode:%d", p_rsp->nfcee_id, p_rsp->mode);
1406    p_cb = nfa_ee_find_ecb (p_rsp->nfcee_id);
1407    if (p_cb == NULL)
1408    {
1409        NFA_TRACE_ERROR1 ("nfa_ee_nci_mode_set_rsp() Can not find cb for handle:0x%02x", p_rsp->nfcee_id);
1410        return;
1411    }
1412
1413    /* update routing table and vs on mode change */
1414    nfa_ee_start_timer();
1415
1416    if (p_rsp->status == NFA_STATUS_OK)
1417    {
1418
1419        if (p_rsp->mode == NFA_EE_MD_ACTIVATE)
1420        {
1421            p_cb->ee_status = NFC_NFCEE_STATUS_ACTIVE;
1422        }
1423        else
1424        {
1425            if (p_cb->tech_switch_on | p_cb->tech_switch_off | p_cb->tech_battery_off |
1426                p_cb->proto_switch_on| p_cb->proto_switch_off| p_cb->proto_battery_off |
1427                p_cb->aid_entries)
1428            {
1429                /* this NFCEE still has configuration when deactivated. clear the configuration */
1430                nfa_ee_cb.ee_cfged  &= ~nfa_ee_ecb_to_mask(p_cb);
1431                nfa_ee_cb.ee_cfg_sts|= NFA_EE_STS_CHANGED_ROUTING;
1432                NFA_TRACE_DEBUG0("deactivating/still configured. Force update");
1433            }
1434            p_cb->tech_switch_on    = p_cb->tech_switch_off = p_cb->tech_battery_off    = 0;
1435            p_cb->proto_switch_on   = p_cb->proto_switch_off= p_cb->proto_battery_off   = 0;
1436            p_cb->aid_entries       = 0;
1437            p_cb->ee_status = NFC_NFCEE_STATUS_INACTIVE;
1438        }
1439    }
1440    NFA_TRACE_DEBUG4 ("status:%d ecb_flags  :0x%02x ee_cfged:0x%02x ee_status:%d",
1441        p_rsp->status, p_cb->ecb_flags  , nfa_ee_cb.ee_cfged, p_cb->ee_status);
1442    if (p_cb->ecb_flags   & NFA_EE_ECB_FLAGS_RESTORE)
1443    {
1444        if (p_cb->conn_st == NFA_EE_CONN_ST_CONN)
1445        {
1446            /* NFA_HCI module handles restoring configurations for HCI access */
1447            if (p_cb->ee_interface[0] != NFC_NFCEE_INTERFACE_HCI_ACCESS)
1448            {
1449                NFC_ConnCreate(NCI_DEST_TYPE_NFCEE, p_cb->nfcee_id,  p_cb->use_interface, nfa_ee_conn_cback);
1450            }
1451        }
1452        else
1453        {
1454            p_cb->ecb_flags   &= ~NFA_EE_ECB_FLAGS_RESTORE;
1455            nfa_ee_check_restore_complete();
1456        }
1457    }
1458    else
1459    {
1460        mode_set.status     = p_rsp->status;
1461        mode_set.ee_handle  = (tNFA_HANDLE)p_rsp->nfcee_id | NFA_HANDLE_GROUP_EE;
1462        mode_set.ee_status  = p_cb->ee_status;
1463
1464        nfa_ee_report_event(p_cb->p_ee_cback, NFA_EE_MODE_SET_EVT, (tNFA_EE_CBACK_DATA *)&mode_set);
1465
1466        /* WAR for BLTH02787041 - Special for 2079xB4/B5, where we also need to report
1467           the discover request event when EE mode is changed from Inactive to Active. */
1468        if ((p_cb->ee_status == NFC_NFCEE_STATUS_INACTIVE)
1469            || (p_cb->ee_status == NFC_NFCEE_STATUS_ACTIVE))
1470        {
1471            /* Report NFA_EE_DISCOVER_REQ_EVT for all active NFCEE */
1472            nfa_ee_report_discover_req_evt();
1473        }
1474    }
1475}
1476
1477/*******************************************************************************
1478**
1479** Function         nfa_ee_nci_conn
1480**
1481** Description      process the connection callback events
1482**
1483** Returns          void
1484**
1485*******************************************************************************/
1486void nfa_ee_nci_conn(tNFA_EE_MSG *p_data)
1487{
1488    tNFA_EE_ECB      *p_cb;
1489    tNFA_EE_NCI_CONN    *p_cbk   = &p_data->conn;
1490    tNFC_CONN           *p_conn  = p_data->conn.p_data;
1491    BT_HDR              *p_pkt   = NULL;
1492    tNFA_EE_CBACK_DATA  evt_data = {0};
1493    tNFA_EE_EVT         event    = NFA_EE_INVALID;
1494    tNFA_EE_CBACK       *p_cback = NULL;
1495
1496    if (p_cbk->event == NFC_CONN_CREATE_CEVT)
1497    {
1498        p_cb = nfa_ee_find_ecb (p_cbk->p_data->conn_create.id);
1499    }
1500    else
1501    {
1502        p_cb = nfa_ee_find_ecb_by_conn_id (p_cbk->conn_id);
1503        if (p_cbk->event == NFC_DATA_CEVT)
1504            p_pkt = p_conn->data.p_data;
1505    }
1506
1507    if (p_cb)
1508    {
1509        p_cback         = p_cb->p_ee_cback;
1510        evt_data.handle = (tNFA_HANDLE)p_cb->nfcee_id | NFA_HANDLE_GROUP_EE;
1511        switch (p_cbk->event)
1512        {
1513        case NFC_CONN_CREATE_CEVT:
1514            if (p_conn->conn_create.status == NFC_STATUS_OK)
1515            {
1516                p_cb->conn_id = p_cbk->conn_id;
1517                p_cb->conn_st = NFA_EE_CONN_ST_CONN;
1518            }
1519            else
1520            {
1521                p_cb->conn_st = NFA_EE_CONN_ST_NONE;
1522            }
1523            if (p_cb->ecb_flags & NFA_EE_ECB_FLAGS_RESTORE)
1524            {
1525                p_cb->ecb_flags &= ~NFA_EE_ECB_FLAGS_RESTORE;
1526                nfa_ee_check_restore_complete();
1527            }
1528            else
1529            {
1530                evt_data.connect.status       = p_conn->conn_create.status;
1531                evt_data.connect.ee_interface = p_cb->use_interface;
1532                event = NFA_EE_CONNECT_EVT;
1533            }
1534            break;
1535
1536        case NFC_CONN_CLOSE_CEVT:
1537            if (p_cb->conn_st != NFA_EE_CONN_ST_DISC)
1538                event = NFA_EE_DISCONNECT_EVT;
1539            p_cb->conn_st    = NFA_EE_CONN_ST_NONE;
1540            p_cb->p_ee_cback = NULL;
1541            p_cb->conn_id    = 0;
1542            if (nfa_ee_cb.em_state == NFA_EE_EM_STATE_DISABLING)
1543            {
1544                if (nfa_ee_cb.ee_flags & NFA_EE_FLAG_WAIT_DISCONN)
1545                {
1546                    if (nfa_ee_cb.num_ee_expecting)
1547                    {
1548                        nfa_ee_cb.num_ee_expecting--;
1549                    }
1550                }
1551                if (nfa_ee_cb.num_ee_expecting == 0)
1552                {
1553                    nfa_ee_cb.ee_flags &= ~NFA_EE_FLAG_WAIT_DISCONN;
1554                    nfa_ee_check_disable();
1555                }
1556            }
1557            break;
1558
1559        case NFC_DATA_CEVT:
1560            if (p_cb->conn_st == NFA_EE_CONN_ST_CONN)
1561            {
1562                /* report data event only in connected state */
1563                if (p_cb->p_ee_cback && p_pkt)
1564                {
1565                    evt_data.data.len   = p_pkt->len;
1566                    evt_data.data.p_buf = (UINT8 *)(p_pkt+1) + p_pkt->offset;
1567                    event               = NFA_EE_DATA_EVT;
1568                    p_pkt               = NULL; /* so this function does not free this GKI buffer */
1569                }
1570            }
1571            break;
1572        }
1573
1574        if ((event != NFA_EE_INVALID) && (p_cback))
1575            (*p_cback)(event, &evt_data);
1576    }
1577    if (p_pkt)
1578        GKI_freebuf (p_pkt);
1579}
1580
1581
1582/*******************************************************************************
1583**
1584** Function         nfa_ee_nci_action_ntf
1585**
1586** Description      process the NFCEE action callback event
1587**
1588** Returns          void
1589**
1590*******************************************************************************/
1591void nfa_ee_nci_action_ntf(tNFA_EE_MSG *p_data)
1592{
1593    tNFC_EE_ACTION_REVT *p_cbk = p_data->act.p_data;
1594    tNFA_EE_ACTION      evt_data;
1595
1596    evt_data.ee_handle  = (tNFA_HANDLE)p_cbk->nfcee_id | NFA_HANDLE_GROUP_EE;
1597    evt_data.trigger    = p_cbk->act_data.trigger;
1598    memcpy (&(evt_data.param), &(p_cbk->act_data.param), sizeof (tNFA_EE_ACTION_PARAM));
1599    nfa_ee_report_event(NULL, NFA_EE_ACTION_EVT, (tNFA_EE_CBACK_DATA *)&evt_data);
1600}
1601
1602/*******************************************************************************
1603**
1604** Function         nfa_ee_nci_disc_req_ntf
1605**
1606** Description      process the NFCEE discover request callback event
1607**
1608** Returns          void
1609**
1610*******************************************************************************/
1611void nfa_ee_nci_disc_req_ntf(tNFA_EE_MSG *p_data)
1612{
1613    tNFC_EE_DISCOVER_REQ_REVT   *p_cbk = p_data->disc_req.p_data;
1614    tNFA_HANDLE         ee_handle;
1615    tNFA_EE_ECB         *p_cb = NULL;
1616    UINT8               report_ntf = 0;
1617    UINT8 xx;
1618
1619    NFA_TRACE_DEBUG2 ("nfa_ee_nci_disc_req_ntf () num_info: %d cur_ee:%d", p_cbk->num_info, nfa_ee_cb.cur_ee );
1620
1621    for (xx = 0; xx < p_cbk->num_info; xx++)
1622    {
1623        ee_handle = NFA_HANDLE_GROUP_EE|p_cbk->info[xx].nfcee_id;
1624
1625        p_cb = nfa_ee_find_ecb (p_cbk->info[xx].nfcee_id);
1626        if (!p_cb)
1627        {
1628            NFA_TRACE_DEBUG1 ("Cannot find cb for NFCEE: 0x%x", p_cbk->info[xx].nfcee_id);
1629            p_cb = nfa_ee_find_ecb (NFA_EE_INVALID);
1630            if (p_cb)
1631            {
1632                p_cb->nfcee_id   = p_cbk->info[xx].nfcee_id;
1633                p_cb->ecb_flags |= NFA_EE_ECB_FLAGS_ORDER;
1634            }
1635            else
1636            {
1637                NFA_TRACE_ERROR1 ("Cannot allocate cb for NFCEE: 0x%x", p_cbk->info[xx].nfcee_id);
1638                continue;
1639            }
1640        }
1641        else
1642        {
1643            report_ntf  |= nfa_ee_ecb_to_mask (p_cb);
1644        }
1645
1646        p_cb->ecb_flags |= NFA_EE_ECB_FLAGS_DISC_REQ;
1647        if (p_cbk->info[xx].op == NFC_EE_DISC_OP_ADD)
1648        {
1649            if (p_cbk->info[xx].tech_n_mode == NFC_DISCOVERY_TYPE_LISTEN_A)
1650            {
1651                p_cb->la_protocol = p_cbk->info[xx].protocol;
1652            }
1653            else if (p_cbk->info[xx].tech_n_mode == NFC_DISCOVERY_TYPE_LISTEN_B)
1654            {
1655                p_cb->lb_protocol = p_cbk->info[xx].protocol;
1656            }
1657            else if (p_cbk->info[xx].tech_n_mode == NFC_DISCOVERY_TYPE_LISTEN_F)
1658            {
1659                p_cb->lf_protocol = p_cbk->info[xx].protocol;
1660            }
1661            else if (p_cbk->info[xx].tech_n_mode == NFC_DISCOVERY_TYPE_LISTEN_B_PRIME)
1662            {
1663                p_cb->lbp_protocol = p_cbk->info[xx].protocol;
1664            }
1665            NFA_TRACE_DEBUG6 ("nfcee_id=0x%x ee_status=0x%x ecb_flags=0x%x la_protocol=0x%x la_protocol=0x%x la_protocol=0x%x",
1666                p_cb->nfcee_id, p_cb->ee_status, p_cb->ecb_flags,
1667                p_cb->la_protocol, p_cb->lb_protocol, p_cb->lf_protocol);
1668        }
1669        else
1670        {
1671            if (p_cbk->info[xx].tech_n_mode == NFC_DISCOVERY_TYPE_LISTEN_A)
1672            {
1673                p_cb->la_protocol = 0;
1674            }
1675            else if (p_cbk->info[xx].tech_n_mode == NFC_DISCOVERY_TYPE_LISTEN_B)
1676            {
1677                p_cb->lb_protocol = 0;
1678            }
1679            else if (p_cbk->info[xx].tech_n_mode == NFC_DISCOVERY_TYPE_LISTEN_F)
1680            {
1681                p_cb->lf_protocol = 0;
1682            }
1683            else if (p_cbk->info[xx].tech_n_mode == NFC_DISCOVERY_TYPE_LISTEN_B_PRIME)
1684            {
1685                p_cb->lbp_protocol = 0;
1686            }
1687        }
1688    }
1689
1690
1691    /* Report NFA_EE_DISCOVER_REQ_EVT for all active NFCEE */
1692    if (report_ntf)
1693        nfa_ee_report_discover_req_evt();
1694
1695}
1696
1697/*******************************************************************************
1698**
1699** Function         nfa_ee_is_active
1700**
1701** Description      Check if the given NFCEE is active
1702**
1703** Returns          TRUE if the given NFCEE is active
1704**
1705*******************************************************************************/
1706BOOLEAN nfa_ee_is_active (tNFA_HANDLE nfcee_id)
1707{
1708    BOOLEAN is_active = FALSE;
1709    int     xx;
1710    tNFA_EE_ECB  *p_cb = nfa_ee_cb.ecb;
1711
1712    if ((NFA_HANDLE_GROUP_MASK & nfcee_id) == NFA_HANDLE_GROUP_EE)
1713        nfcee_id    &= NFA_HANDLE_MASK;
1714
1715    /* compose output */
1716    for (xx = 0; xx < nfa_ee_cb.cur_ee; xx++, p_cb++)
1717    {
1718        if ((tNFA_HANDLE)p_cb->nfcee_id == nfcee_id)
1719        {
1720            if (p_cb->ee_status == NFA_EE_STATUS_ACTIVE)
1721            {
1722                is_active = TRUE;
1723            }
1724            break;
1725        }
1726    }
1727    return is_active;
1728}
1729
1730/*******************************************************************************
1731**
1732** Function         nfa_ee_get_tech_route
1733**
1734** Description      Given a power state, find the technology routing destination.
1735**                  The result is filled in the given p_handles
1736**                  in the order of A, B, F, Bprime
1737**
1738** Returns          None
1739**
1740*******************************************************************************/
1741void nfa_ee_get_tech_route (UINT8 power_state, UINT8 *p_handles)
1742{
1743    int     xx, yy;
1744    tNFA_EE_ECB *p_cb;
1745    UINT8   tech_mask_list[NFA_EE_MAX_TECH_ROUTE] =
1746    {
1747        NFA_TECHNOLOGY_MASK_A,
1748        NFA_TECHNOLOGY_MASK_B,
1749        NFA_TECHNOLOGY_MASK_F,
1750        NFA_TECHNOLOGY_MASK_B_PRIME
1751    };
1752
1753    NFA_TRACE_DEBUG1("nfa_ee_get_tech_route(): %d", power_state);
1754
1755    for (xx = 0; xx < NFA_EE_MAX_TECH_ROUTE; xx++)
1756    {
1757        p_handles[xx] = NFC_DH_ID;
1758        p_cb = &nfa_ee_cb.ecb[nfa_ee_cb.cur_ee - 1];
1759        for (yy = 0; yy < nfa_ee_cb.cur_ee; yy++, p_cb--)
1760        {
1761            if (p_cb->ee_status == NFC_NFCEE_STATUS_ACTIVE)
1762            {
1763                switch (power_state)
1764                {
1765                case NFA_EE_PWR_STATE_ON:
1766                    if (p_cb->tech_switch_on & tech_mask_list[xx])
1767                        p_handles[xx] = p_cb->nfcee_id;
1768                    break;
1769                case NFA_EE_PWR_STATE_SWITCH_OFF:
1770                    if (p_cb->tech_switch_off & tech_mask_list[xx])
1771                        p_handles[xx] = p_cb->nfcee_id;
1772                    break;
1773                case NFA_EE_PWR_STATE_BATT_OFF:
1774                    if (p_cb->tech_battery_off & tech_mask_list[xx])
1775                        p_handles[xx] = p_cb->nfcee_id;
1776                    break;
1777                }
1778            }
1779        }
1780    }
1781    NFA_TRACE_DEBUG4("0x%x, 0x%x, 0x%x, 0x%x", p_handles[0], p_handles[1], p_handles[2], p_handles[3]);
1782}
1783
1784/*******************************************************************************
1785**
1786** Function         nfa_ee_check_set_routing
1787**
1788** Description      If the new size exceeds the capacity of next block,
1789**                  send the routing command now and reset the related parameters
1790**
1791** Returns          void
1792**
1793*******************************************************************************/
1794void nfa_ee_check_set_routing(UINT16 new_size, int *p_max_len, UINT8 *p, int *p_cur_offset)
1795{
1796    UINT8   max_tlv = (UINT8)((*p_max_len > NFA_EE_ROUT_MAX_TLV_SIZE)?NFA_EE_ROUT_MAX_TLV_SIZE:*p_max_len);
1797    tNFA_STATUS status = NFA_STATUS_OK;
1798
1799    if (new_size + *p_cur_offset > max_tlv)
1800    {
1801        NFC_SetRouting(TRUE, *p, *p_cur_offset, p + 1);
1802        /* after the routing command is sent, re-use the same buffer to send the next routing command.
1803         * reset the related parameters */
1804        if (*p_max_len > *p_cur_offset)
1805            *p_max_len     -= *p_cur_offset;/* the max is reduced */
1806        else
1807            *p_max_len      = 0;
1808        *p_cur_offset   = 0;                /* nothing is in queue any more */
1809        *p              = 0;                /* num_tlv=0 */
1810    }
1811}
1812
1813/*******************************************************************************
1814**
1815** Function         nfa_ee_route_add_one_ecb
1816**
1817** Description      Add the routing entries for one NFCEE/DH
1818**
1819** Returns          NFA_STATUS_OK, if ok to continue
1820**
1821*******************************************************************************/
1822tNFA_STATUS nfa_ee_route_add_one_ecb(tNFA_EE_ECB *p_cb, int *p_max_len, BOOLEAN more, UINT8 *ps, int *p_cur_offset)
1823{
1824    UINT8   *p, *pa;
1825    UINT16  tlv_size;
1826    UINT8   num_tlv, len;
1827    int     xx;
1828    int     start_offset;
1829    UINT8   power_cfg = 0;
1830    UINT8   *pp = ps + *p_cur_offset;
1831    UINT8   entry_size;
1832    UINT8   max_tlv;
1833    UINT8   *p_start;
1834    UINT8   new_size;
1835    tNFA_STATUS status = NFA_STATUS_OK;
1836
1837    nfa_ee_check_set_routing (p_cb->size_mask, p_max_len, ps, p_cur_offset);
1838    max_tlv = (UINT8)((*p_max_len > NFA_EE_ROUT_MAX_TLV_SIZE)?NFA_EE_ROUT_MAX_TLV_SIZE:*p_max_len);
1839    /* use the first byte of the buffer (ps) to keep the num_tlv */
1840    num_tlv  = *ps;
1841    NFA_TRACE_DEBUG5 ("nfa_ee_route_add_one_ecb max_len:%d, max_tlv:%d, cur_offset:%d, more:%d, num_tlv:%d",
1842        *p_max_len, max_tlv, *p_cur_offset, more, num_tlv);
1843    pp       = ps + 1 + *p_cur_offset;
1844    p        = pp;
1845    tlv_size = (UINT8)*p_cur_offset;
1846    /* add the Technology based routing */
1847    for (xx = 0; xx < NFA_EE_NUM_TECH; xx++)
1848    {
1849        power_cfg = 0;
1850        if (p_cb->tech_switch_on & nfa_ee_tech_mask_list[xx])
1851            power_cfg |= NCI_ROUTE_PWR_STATE_ON;
1852        if (p_cb->tech_switch_off & nfa_ee_tech_mask_list[xx])
1853            power_cfg |= NCI_ROUTE_PWR_STATE_SWITCH_OFF;
1854        if (p_cb->tech_battery_off & nfa_ee_tech_mask_list[xx])
1855            power_cfg |= NCI_ROUTE_PWR_STATE_BATT_OFF;
1856        if (power_cfg)
1857        {
1858            *pp++   = NFC_ROUTE_TAG_TECH;
1859            *pp++   = 3;
1860            *pp++   = p_cb->nfcee_id;
1861            *pp++   = power_cfg;
1862            *pp++   = nfa_ee_tech_list[xx];
1863            num_tlv++;
1864            if (power_cfg != NCI_ROUTE_PWR_STATE_ON)
1865                nfa_ee_cb.ee_cfged  |= NFA_EE_CFGED_OFF_ROUTING;
1866        }
1867    }
1868
1869    /* add the Protocol based routing */
1870    for (xx = 0; xx < NFA_EE_NUM_PROTO; xx++)
1871    {
1872        power_cfg = 0;
1873        if (p_cb->proto_switch_on & nfa_ee_proto_mask_list[xx])
1874            power_cfg |= NCI_ROUTE_PWR_STATE_ON;
1875        if (p_cb->proto_switch_off & nfa_ee_proto_mask_list[xx])
1876            power_cfg |= NCI_ROUTE_PWR_STATE_SWITCH_OFF;
1877        if (p_cb->proto_battery_off & nfa_ee_proto_mask_list[xx])
1878            power_cfg |= NCI_ROUTE_PWR_STATE_BATT_OFF;
1879        if (power_cfg)
1880        {
1881            *pp++   = NFC_ROUTE_TAG_PROTO;
1882            *pp++   = 3;
1883            *pp++   = p_cb->nfcee_id;
1884            *pp++   = power_cfg;
1885            *pp++   = nfa_ee_proto_list[xx];
1886            num_tlv++;
1887            if (power_cfg != NCI_ROUTE_PWR_STATE_ON)
1888                nfa_ee_cb.ee_cfged  |= NFA_EE_CFGED_OFF_ROUTING;
1889        }
1890    }
1891
1892    /* update the num_tlv and current offset */
1893    entry_size       = (UINT8)(pp - p);
1894    *p_cur_offset   += entry_size;
1895    *ps              = num_tlv;
1896    /* add the AID routing */
1897    if (p_cb->aid_entries)
1898    {
1899        start_offset = 0;
1900        for (xx = 0; xx < p_cb->aid_entries; xx++)
1901        {
1902            p_start     = pp; /* rememebr the beginning of this AID routing entry, just in case we need to put it in next command */
1903            /* add one AID entry */
1904            if (p_cb->aid_rt_info[xx] & NFA_EE_AE_ROUTE)
1905            {
1906                num_tlv++;
1907                pa      = &p_cb->aid_cfg[start_offset];
1908                pa ++; /* EMV tag */
1909                len     = *pa++; /* aid_len */
1910                *pp++   = NFC_ROUTE_TAG_AID;
1911                *pp++   = len + 2;
1912                *pp++   = p_cb->nfcee_id;
1913                *pp++   = p_cb->aid_pwr_cfg[xx];
1914                /* copy the AID */
1915                memcpy(pp, pa, len);
1916                pp     += len;
1917            }
1918            start_offset += p_cb->aid_len[xx];
1919            new_size        = (UINT8)(pp - p_start);
1920            nfa_ee_check_set_routing(new_size, p_max_len, ps, p_cur_offset);
1921            if (*ps == 0)
1922            {
1923                /* just sent routing command, update local */
1924                *ps      = 1;
1925                num_tlv  = *ps;
1926                *p_cur_offset = new_size;
1927                pp       = ps + 1;
1928                p        = pp;
1929                tlv_size = (UINT8)*p_cur_offset;
1930                max_tlv  = (UINT8)((*p_max_len > NFA_EE_ROUT_MAX_TLV_SIZE)?NFA_EE_ROUT_MAX_TLV_SIZE:*p_max_len);
1931                memcpy (p, p_start, new_size);
1932                pp      += new_size;
1933            }
1934            else
1935            {
1936                /* add the new entry */
1937                *ps              = num_tlv;
1938                *p_cur_offset   += new_size;
1939            }
1940        }
1941    }
1942
1943    tlv_size   = nfa_ee_total_lmrt_size();
1944    if (tlv_size)
1945    {
1946        nfa_ee_cb.ee_cfged |= nfa_ee_ecb_to_mask(p_cb);
1947    }
1948    if (p_cb->ecb_flags   & NFA_EE_ECB_FLAGS_ROUTING)
1949    {
1950        nfa_ee_cb.ee_cfg_sts   |= NFA_EE_STS_CHANGED_ROUTING;
1951    }
1952    NFA_TRACE_DEBUG2 ("ee_cfg_sts:0x%02x lmrt_size:%d", nfa_ee_cb.ee_cfg_sts, tlv_size);
1953
1954    if (more == FALSE)
1955    {
1956        /* last entry. update routing table now */
1957        if (nfa_ee_cb.ee_cfg_sts & NFA_EE_STS_CHANGED_ROUTING)
1958        {
1959            if (tlv_size)
1960            {
1961                nfa_ee_cb.ee_cfg_sts       |= NFA_EE_STS_PREV_ROUTING;
1962            }
1963            else
1964            {
1965                nfa_ee_cb.ee_cfg_sts       &= ~NFA_EE_STS_PREV_ROUTING;
1966            }
1967            NFA_TRACE_DEBUG2 ("nfa_ee_route_add_one_ecb: set routing num_tlv:%d tlv_size:%d", num_tlv, tlv_size);
1968            NFC_SetRouting(more, num_tlv, (UINT8)(*p_cur_offset), ps + 1);
1969        }
1970        else if (nfa_ee_cb.ee_cfg_sts & NFA_EE_STS_PREV_ROUTING)
1971        {
1972            if (tlv_size == 0)
1973            {
1974                nfa_ee_cb.ee_cfg_sts       &= ~NFA_EE_STS_PREV_ROUTING;
1975                /* indicated routing is configured to NFCC */
1976                nfa_ee_cb.ee_cfg_sts       |= NFA_EE_STS_CHANGED_ROUTING;
1977                NFC_SetRouting(more, 0, 0, ps + 1);
1978            }
1979        }
1980    }
1981
1982    return status;
1983}
1984
1985
1986/*******************************************************************************
1987**
1988** Function         nfa_ee_need_recfg
1989**
1990** Description      Check if any API function to configure the routing table or
1991**                  VS is called since last update
1992**
1993**                  The algorithm for the NFCEE configuration handling is as follows:
1994**
1995**                  Each NFCEE_ID/DH has its own control block - tNFA_EE_ECB
1996**                  Each control block uses ecb_flags to keep track if an API
1997**                  that changes routing/VS is invoked.
1998**                  This ecb_flags is cleared at the end of nfa_ee_update_rout().
1999**
2000**                  nfa_ee_cb.ee_cfged is the bitmask of the control blocks with
2001**                  routing/VS configuration and NFA_EE_CFGED_UPDATE_NOW.
2002**                  nfa_ee_cb.ee_cfged is cleared and re-calculated at the end of
2003**                  nfa_ee_update_rout().
2004**
2005**                  nfa_ee_cb.ee_cfg_sts is used to check is any status is changed
2006**                  and the associated command is issued to NFCC.
2007**                  nfa_ee_cb.ee_cfg_sts is AND with NFA_EE_STS_PREV at the end of
2008**                  nfa_ee_update_rout() to clear the NFA_EE_STS_CHANGED bits
2009**                  (except NFA_EE_STS_CHANGED_CANNED_VS is cleared in nfa_ee_vs_cback)
2010**
2011** Returns          TRUE if any configuration is changed
2012**
2013*******************************************************************************/
2014static BOOLEAN nfa_ee_need_recfg(void)
2015{
2016    BOOLEAN needed = FALSE;
2017    UINT32  xx;
2018    tNFA_EE_ECB  *p_cb;
2019    UINT8   mask;
2020
2021    NFA_TRACE_DEBUG2("nfa_ee_need_recfg() ee_cfged: 0x%02x ee_cfg_sts: 0x%02x", nfa_ee_cb.ee_cfged, nfa_ee_cb.ee_cfg_sts);
2022    /* if no routing/vs is configured, do not need to send the info to NFCC */
2023    if (nfa_ee_cb.ee_cfged || nfa_ee_cb.ee_cfg_sts)
2024    {
2025        if (nfa_ee_cb.ee_cfged & NFA_EE_CFGED_UPDATE_NOW)
2026        {
2027            needed = TRUE;
2028        }
2029        else if (nfa_ee_cb.ee_cfg_sts & NFA_EE_STS_CHANGED)
2030        {
2031            needed = TRUE;
2032        }
2033        else
2034        {
2035            p_cb = &nfa_ee_cb.ecb[NFA_EE_CB_4_DH];
2036            mask = 1 << NFA_EE_CB_4_DH;
2037            for (xx = 0; xx <= nfa_ee_cb.cur_ee; xx++)
2038            {
2039                NFA_TRACE_DEBUG3("%d: ecb_flags  : 0x%02x, mask: 0x%02x", xx, p_cb->ecb_flags  , mask);
2040                if ((p_cb->ecb_flags  ) && (nfa_ee_cb.ee_cfged & mask))
2041                {
2042                    needed = TRUE;
2043                    break;
2044                }
2045                p_cb = &nfa_ee_cb.ecb[xx];
2046                mask = 1 << xx;
2047            }
2048        }
2049    }
2050
2051    return needed;
2052}
2053
2054/*******************************************************************************
2055**
2056** Function         nfa_ee_rout_timeout
2057**
2058** Description      Anytime VS or routing entries are changed,
2059**                  a 1 second timer is started. This function is called when
2060**                  the timer expires or NFA_EeUpdateNow() is called.
2061**
2062** Returns          void
2063**
2064*******************************************************************************/
2065void nfa_ee_rout_timeout(tNFA_EE_MSG *p_data)
2066{
2067    NFA_TRACE_DEBUG0("nfa_ee_rout_timeout()");
2068    if (nfa_ee_need_recfg())
2069    {
2070        /* discovery is not started */
2071        nfa_ee_update_rout();
2072    }
2073}
2074
2075/*******************************************************************************
2076**
2077** Function         nfa_ee_discv_timeout
2078**
2079** Description
2080**
2081**
2082**
2083** Returns          void
2084**
2085*******************************************************************************/
2086void nfa_ee_discv_timeout(tNFA_EE_MSG *p_data)
2087{
2088    NFC_NfceeDiscover(FALSE);
2089    if (nfa_ee_cb.p_enable_cback)
2090        (*nfa_ee_cb.p_enable_cback)(NFA_EE_DISC_STS_OFF);
2091}
2092
2093/*******************************************************************************
2094**
2095** Function         nfa_ee_lmrt_to_nfcc
2096**
2097** Description      This function would set the listen mode routing table
2098**                  to NFCC.
2099**
2100** Returns          void
2101**
2102*******************************************************************************/
2103void nfa_ee_lmrt_to_nfcc(tNFA_EE_MSG *p_data)
2104{
2105    int xx;
2106    tNFA_EE_ECB          *p_cb;
2107    UINT8   *p = NULL;
2108    BOOLEAN more = TRUE;
2109    UINT8   last_active = NFA_EE_INVALID;
2110    int     max_len, len;
2111    tNFA_STATUS status = NFA_STATUS_FAILED;
2112    int     cur_offset;
2113    UINT8   max_tlv;
2114
2115    /* update routing table: DH and the activated NFCEEs */
2116    p = (UINT8 *)GKI_getbuf(NFA_EE_ROUT_BUF_SIZE);
2117    if (p == NULL)
2118    {
2119        NFA_TRACE_ERROR0 ("nfa_ee_lmrt_to_nfcc() no buffer to send routing info.");
2120        nfa_ee_report_event( NULL, NFA_EE_NO_MEM_ERR_EVT, (tNFA_EE_CBACK_DATA *)&status);
2121        return;
2122    }
2123
2124    /* find the last active NFCEE. */
2125    p_cb = &nfa_ee_cb.ecb[nfa_ee_cb.cur_ee - 1];
2126    for (xx = 0; xx < nfa_ee_cb.cur_ee; xx++, p_cb--)
2127    {
2128        if (p_cb->ee_status == NFC_NFCEE_STATUS_ACTIVE)
2129        {
2130            if (last_active == NFA_EE_INVALID)
2131            {
2132                last_active = p_cb->nfcee_id;
2133                NFA_TRACE_DEBUG1 ("last_active: 0x%x", last_active);
2134            }
2135        }
2136    }
2137    if (last_active == NFA_EE_INVALID)
2138    {
2139        more = FALSE;
2140    }
2141
2142    /* add the routing for DH first */
2143    status  = NFA_STATUS_OK;
2144    max_len = NFC_GetLmrtSize();
2145    max_tlv = (UINT8)((max_len > NFA_EE_ROUT_MAX_TLV_SIZE)?NFA_EE_ROUT_MAX_TLV_SIZE:max_len);
2146    cur_offset  = 0;
2147    /* use the first byte of the buffer (p) to keep the num_tlv */
2148    *p          = 0;
2149    status = nfa_ee_route_add_one_ecb(&nfa_ee_cb.ecb[NFA_EE_CB_4_DH], &max_len, more, p, &cur_offset);
2150
2151    /* add only what is supported by NFCC. report overflow */
2152    if (status == NFA_STATUS_OK)
2153    {
2154        /* add the routing for NFCEEs */
2155        p_cb = &nfa_ee_cb.ecb[0];
2156        for (xx = 0; (xx < nfa_ee_cb.cur_ee) && more; xx++, p_cb++)
2157        {
2158            len = 0;
2159            if (p_cb->ee_status == NFC_NFCEE_STATUS_ACTIVE)
2160            {
2161                NFA_TRACE_DEBUG2 ("nfcee_id:0x%x, last_active: 0x%x", p_cb->nfcee_id, last_active);
2162                if (last_active == p_cb->nfcee_id)
2163                    more = FALSE;
2164                status = nfa_ee_route_add_one_ecb(p_cb, &max_len, more, p, &cur_offset);
2165                if (status != NFA_STATUS_OK)
2166                {
2167                    more    = FALSE;
2168                }
2169            }
2170        }
2171    }
2172    if (status != NFA_STATUS_OK)
2173    {
2174        nfa_ee_report_event( NULL, NFA_EE_ROUT_ERR_EVT, (tNFA_EE_CBACK_DATA *)&status);
2175    }
2176    GKI_freebuf(p);
2177}
2178
2179/*******************************************************************************
2180**
2181** Function         nfa_ee_update_rout
2182**
2183** Description      This function would set the VS and listen mode routing table
2184**                  to NFCC.
2185**
2186** Returns          void
2187**
2188*******************************************************************************/
2189void nfa_ee_update_rout(void)
2190{
2191    int xx;
2192    tNFA_EE_ECB          *p_cb;
2193    UINT8   mask;
2194    BT_HDR  msg;
2195
2196    NFA_TRACE_DEBUG1 ("nfa_ee_update_rout ee_cfg_sts:0x%02x", nfa_ee_cb.ee_cfg_sts);
2197
2198    /* use action function to send routing and VS configuration to NFCC */
2199    msg.event = NFA_EE_CFG_TO_NFCC_EVT;
2200    nfa_ee_evt_hdlr (&msg);
2201
2202    /* all configuration is updated to NFCC, clear the status mask */
2203    nfa_ee_cb.ee_cfg_sts   &= NFA_EE_STS_PREV;
2204    nfa_ee_cb.ee_cfged  = 0;
2205    p_cb                = &nfa_ee_cb.ecb[0];
2206    for (xx = 0; xx < NFA_EE_NUM_ECBS; xx++, p_cb++)
2207    {
2208        p_cb->ecb_flags     = 0;
2209        mask                = (1 << xx);
2210        if (p_cb->tech_switch_on | p_cb->tech_switch_off | p_cb->tech_battery_off |
2211            p_cb->proto_switch_on| p_cb->proto_switch_off| p_cb->proto_battery_off |
2212            p_cb->aid_entries)
2213        {
2214            /* this entry has routing configuration. mark it configured */
2215            nfa_ee_cb.ee_cfged  |= mask;
2216        }
2217    }
2218    NFA_TRACE_DEBUG2 ("ee_cfg_sts:0x%02x ee_cfged:0x%02x", nfa_ee_cb.ee_cfg_sts, nfa_ee_cb.ee_cfged);
2219}
2220
2221
2222