nfa_ee_act.c revision ca0bff46ca93ed18142e20f03db5174399e1de75
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_lmrt_size
782**
783** Description      Reports the remaining size in the Listen Mode Routing Table
784**
785** Returns          void
786**
787*******************************************************************************/
788void nfa_ee_api_lmrt_size(tNFA_EE_MSG *p_data)
789{
790    tNFA_EE_CBACK_DATA  evt_data = {0};
791    UINT16 total_size = NFC_GetLmrtSize();
792
793    evt_data.size       = total_size - nfa_ee_total_lmrt_size();
794    NFA_TRACE_DEBUG2 ("nfa_ee_api_lmrt_size total size:%d remaining size:%d", total_size, evt_data.size);
795
796    nfa_ee_report_event (NULL, NFA_EE_REMAINING_SIZE_EVT, &evt_data);
797}
798
799/*******************************************************************************
800**
801** Function         nfa_ee_api_update_now
802**
803** Description      Initiates connection creation process to the given NFCEE
804**
805** Returns          void
806**
807*******************************************************************************/
808void nfa_ee_api_update_now(tNFA_EE_MSG *p_data)
809{
810    nfa_sys_stop_timer(&nfa_ee_cb.timer);
811    nfa_ee_cb.ee_cfged  |= NFA_EE_CFGED_UPDATE_NOW;
812    nfa_ee_rout_timeout(p_data);
813}
814
815/*******************************************************************************
816**
817** Function         nfa_ee_api_connect
818**
819** Description      Initiates connection creation process to the given NFCEE
820**
821** Returns          void
822**
823*******************************************************************************/
824void nfa_ee_api_connect(tNFA_EE_MSG *p_data)
825{
826    tNFA_EE_ECB  *p_cb = p_data->connect.p_cb;
827    int xx;
828    tNFA_EE_CBACK_DATA  evt_data = {0};
829
830    evt_data.connect.status       = NFA_STATUS_FAILED;
831    if (p_cb->conn_st == NFA_EE_CONN_ST_NONE)
832    {
833        for (xx = 0; xx < p_cb->num_interface; xx++)
834        {
835            if (p_data->connect.ee_interface == p_cb->ee_interface[xx])
836            {
837                p_cb->p_ee_cback        = p_data->connect.p_cback;
838                p_cb->conn_st           = NFA_EE_CONN_ST_WAIT;
839                p_cb->use_interface     = p_data->connect.ee_interface;
840                evt_data.connect.status = NFC_ConnCreate(NCI_DEST_TYPE_NFCEE, p_data->connect.nfcee_id,
841                    p_data->connect.ee_interface, nfa_ee_conn_cback);
842                /* report the NFA_EE_CONNECT_EVT status on the response from NFCC */
843                break;
844            }
845        }
846    }
847
848    if (evt_data.connect.status != NCI_STATUS_OK)
849    {
850        evt_data.connect.ee_handle    = (tNFA_HANDLE)p_data->connect.nfcee_id | NFA_HANDLE_GROUP_EE;
851        evt_data.connect.status       = NFA_STATUS_INVALID_PARAM;
852        evt_data.connect.ee_interface = p_data->connect.ee_interface;
853        nfa_ee_report_event (p_data->connect.p_cback, NFA_EE_CONNECT_EVT, &evt_data);
854    }
855}
856
857/*******************************************************************************
858**
859** Function         nfa_ee_api_send_data
860**
861** Description      Send the given data packet to the given NFCEE
862**
863** Returns          void
864**
865*******************************************************************************/
866void nfa_ee_api_send_data(tNFA_EE_MSG *p_data)
867{
868    tNFA_EE_ECB  *p_cb = p_data->send_data.p_cb;
869    BT_HDR *p_pkt;
870    UINT16 size = NCI_MSG_OFFSET_SIZE + NCI_DATA_HDR_SIZE + p_data->send_data.data_len + BT_HDR_SIZE;
871    UINT8  *p;
872    tNFA_STATUS status = NFA_STATUS_FAILED;
873
874    if (p_cb->conn_st == NFA_EE_CONN_ST_CONN)
875    {
876        p_pkt = (BT_HDR *)GKI_getbuf(size);
877        if (p_pkt)
878        {
879            p_pkt->offset   = NCI_MSG_OFFSET_SIZE + NCI_DATA_HDR_SIZE;
880            p_pkt->len      = p_data->send_data.data_len;
881            p               = (UINT8 *)(p_pkt+1) + p_pkt->offset;
882            memcpy(p, p_data->send_data.p_data, p_pkt->len);
883            NFC_SendData (p_cb->conn_id, p_pkt);
884        }
885        else
886        {
887            nfa_ee_report_event( p_cb->p_ee_cback, NFA_EE_NO_MEM_ERR_EVT, (tNFA_EE_CBACK_DATA *)&status);
888        }
889    }
890    else
891    {
892        nfa_ee_report_event( p_cb->p_ee_cback, NFA_EE_NO_CB_ERR_EVT, (tNFA_EE_CBACK_DATA *)&status);
893    }
894}
895
896/*******************************************************************************
897**
898** Function         nfa_ee_api_disconnect
899**
900** Description      Initiates closing of the connection to the given NFCEE
901**
902** Returns          void
903**
904*******************************************************************************/
905void nfa_ee_api_disconnect(tNFA_EE_MSG *p_data)
906{
907    tNFA_EE_ECB  *p_cb = p_data->disconnect.p_cb;
908    tNFA_EE_CBACK_DATA  evt_data = {0};
909
910    if (p_cb->conn_st == NFA_EE_CONN_ST_CONN)
911    {
912        p_cb->conn_st = NFA_EE_CONN_ST_DISC;
913        NFC_ConnClose(p_cb->conn_id);
914    }
915    evt_data.handle = (tNFA_HANDLE)p_cb->nfcee_id | NFA_HANDLE_GROUP_EE;
916    nfa_ee_report_event(p_cb->p_ee_cback, NFA_EE_DISCONNECT_EVT, &evt_data);
917}
918
919/*******************************************************************************
920**
921** Function         nfa_ee_report_disc_done
922**
923** Description      Process the callback for NFCEE discovery response
924**
925** Returns          void
926**
927*******************************************************************************/
928void nfa_ee_report_disc_done(BOOLEAN notify_enable_done)
929{
930    tNFA_EE_CBACK           *p_cback;
931    tNFA_EE_CBACK_DATA      evt_data = {0};
932
933    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);
934    if (nfa_ee_cb.num_ee_expecting == 0)
935    {
936        if (notify_enable_done)
937        {
938            if (nfa_ee_cb.em_state == NFA_EE_EM_STATE_INIT_DONE)
939            {
940                nfa_sys_cback_notify_enable_complete (NFA_ID_EE);
941                if (nfa_ee_cb.p_enable_cback)
942                    (*nfa_ee_cb.p_enable_cback)(NFA_EE_DISC_STS_ON);
943            }
944            else if ((nfa_ee_cb.em_state == NFA_EE_EM_STATE_RESTORING) && (nfa_ee_cb.ee_flags & NFA_EE_FLAG_NOTIFY_HCI) )
945            {
946                nfa_ee_cb.ee_flags   &= ~NFA_EE_FLAG_NOTIFY_HCI;
947                if (nfa_ee_cb.p_enable_cback)
948                    (*nfa_ee_cb.p_enable_cback)(NFA_EE_DISC_STS_ON);
949            }
950        }
951
952
953        if (nfa_ee_cb.p_ee_disc_cback)
954        {
955            /* notify API callback */
956            p_cback                         = nfa_ee_cb.p_ee_disc_cback;
957            nfa_ee_cb.p_ee_disc_cback       = NULL;
958            evt_data.status                         = NFA_STATUS_OK;
959            evt_data.ee_discover.num_ee             = NFA_EE_MAX_EE_SUPPORTED;
960            NFA_EeGetInfo(&evt_data.ee_discover.num_ee, evt_data.ee_discover.ee_info);
961            nfa_ee_report_event (p_cback, NFA_EE_DISCOVER_EVT, &evt_data);
962        }
963    }
964}
965
966/*******************************************************************************
967**
968** Function         nfa_ee_restore_ntf_done
969**
970** Description      check if any ee_status still has NFA_EE_STATUS_PENDING bit
971**
972** Returns          TRUE, if all NFA_EE_STATUS_PENDING bits are removed
973**
974*******************************************************************************/
975BOOLEAN nfa_ee_restore_ntf_done(void)
976{
977    tNFA_EE_ECB     *p_cb;
978    BOOLEAN         is_done = TRUE;
979    int             xx;
980
981    p_cb = nfa_ee_cb.ecb;
982    for (xx = 0; xx < nfa_ee_cb.cur_ee; xx++, p_cb++)
983    {
984        if ((p_cb->nfcee_id != NFA_EE_INVALID) && (p_cb->ee_old_status & NFA_EE_STATUS_RESTORING))
985        {
986            is_done = FALSE;
987            break;
988        }
989    }
990    return is_done;
991}
992
993/*******************************************************************************
994**
995** Function         nfa_ee_remove_pending
996**
997** Description      check if any ee_status still has NFA_EE_STATUS_RESTORING bit
998**
999** Returns          TRUE, if all NFA_EE_STATUS_RESTORING bits are removed
1000**
1001*******************************************************************************/
1002static void nfa_ee_remove_pending(void)
1003{
1004    tNFA_EE_ECB     *p_cb;
1005    tNFA_EE_ECB     *p_cb_n, *p_cb_end;
1006    int             xx, num_removed = 0;
1007    int             first_removed = NFA_EE_MAX_EE_SUPPORTED;
1008
1009    p_cb = nfa_ee_cb.ecb;
1010    for (xx = 0; xx < nfa_ee_cb.cur_ee; xx++, p_cb++)
1011    {
1012        if ((p_cb->nfcee_id != NFA_EE_INVALID) && (p_cb->ee_status & NFA_EE_STATUS_RESTORING))
1013        {
1014            p_cb->nfcee_id  = NFA_EE_INVALID;
1015            num_removed ++;
1016            if (first_removed == NFA_EE_MAX_EE_SUPPORTED)
1017                first_removed   = xx;
1018        }
1019    }
1020
1021    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);
1022    if (num_removed && (first_removed != (nfa_ee_cb.cur_ee - num_removed)))
1023    {
1024        /* if the removes ECB entried are not at the end, move the entries up */
1025        p_cb_end = &nfa_ee_cb.ecb[nfa_ee_cb.cur_ee - 1];
1026        p_cb = &nfa_ee_cb.ecb[first_removed];
1027        for (p_cb_n = p_cb + 1; p_cb_n <= p_cb_end;)
1028        {
1029            while ((p_cb_n->nfcee_id == NFA_EE_INVALID) && (p_cb_n <= p_cb_end))
1030            {
1031                p_cb_n++;
1032            }
1033
1034            if (p_cb_n <= p_cb_end)
1035            {
1036                memcpy(p_cb, p_cb_n, sizeof(tNFA_EE_ECB));
1037                p_cb_n->nfcee_id = NFA_EE_INVALID;
1038            }
1039            p_cb++;
1040            p_cb_n++;
1041        }
1042    }
1043    nfa_ee_cb.cur_ee -= (UINT8)num_removed;
1044}
1045
1046
1047/*******************************************************************************
1048**
1049** Function         nfa_ee_nci_disc_rsp
1050**
1051** Description      Process the callback for NFCEE discovery response
1052**
1053** Returns          void
1054**
1055*******************************************************************************/
1056void nfa_ee_nci_disc_rsp(tNFA_EE_MSG *p_data)
1057{
1058    tNFC_NFCEE_DISCOVER_REVT    *p_evt = p_data->disc_rsp.p_data;
1059    tNFA_EE_ECB              *p_cb;
1060    UINT8   xx;
1061    UINT8   num_nfcee = p_evt->num_nfcee;
1062    BOOLEAN notify_enable_done = FALSE;
1063
1064    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);
1065    switch (nfa_ee_cb.em_state)
1066    {
1067    case NFA_EE_EM_STATE_INIT:
1068        nfa_ee_cb.cur_ee            = 0;
1069        nfa_ee_cb.num_ee_expecting  = 0;
1070        if (num_nfcee == 0)
1071        {
1072            nfa_ee_cb.em_state = NFA_EE_EM_STATE_INIT_DONE;
1073            notify_enable_done = TRUE;
1074            if (p_evt->status != NFC_STATUS_OK)
1075            {
1076                nfa_sys_stop_timer(&nfa_ee_cb.discv_timer);
1077            }
1078        }
1079        break;
1080
1081    case NFA_EE_EM_STATE_INIT_DONE:
1082        if (num_nfcee)
1083        {
1084            /* if this is initiated by api function,
1085             * check if the number of NFCEE expected is more than what's currently in CB */
1086            if (num_nfcee > NFA_EE_MAX_EE_SUPPORTED)
1087                num_nfcee = NFA_EE_MAX_EE_SUPPORTED;
1088            if (nfa_ee_cb.cur_ee < num_nfcee)
1089            {
1090                p_cb = &nfa_ee_cb.ecb[nfa_ee_cb.cur_ee];
1091                for (xx = nfa_ee_cb.cur_ee; xx < num_nfcee; xx++, p_cb++)
1092                {
1093                    /* mark the new entries as a new one */
1094                    p_cb->nfcee_id = NFA_EE_INVALID;
1095                }
1096            }
1097            nfa_ee_cb.cur_ee = num_nfcee;
1098        }
1099        break;
1100
1101    case NFA_EE_EM_STATE_RESTORING:
1102        if (num_nfcee == 0)
1103        {
1104            nfa_ee_cb.em_state = NFA_EE_EM_STATE_INIT_DONE;
1105            nfa_ee_remove_pending();
1106            nfa_ee_check_restore_complete();
1107            if (p_evt->status != NFC_STATUS_OK)
1108            {
1109                nfa_sys_stop_timer(&nfa_ee_cb.discv_timer);
1110            }
1111        }
1112        break;
1113    }
1114
1115    if (p_evt->status == NFC_STATUS_OK)
1116    {
1117        nfa_ee_cb.num_ee_expecting = p_evt->num_nfcee;
1118        if (nfa_ee_cb.num_ee_expecting > NFA_EE_MAX_EE_SUPPORTED)
1119        {
1120            NFA_TRACE_ERROR2 ("NFA-EE num_ee_expecting:%d > max:%d", nfa_ee_cb.num_ee_expecting, NFA_EE_MAX_EE_SUPPORTED);
1121        }
1122    }
1123    nfa_ee_report_disc_done(notify_enable_done);
1124    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);
1125}
1126
1127/*******************************************************************************
1128**
1129** Function         nfa_ee_nci_disc_ntf
1130**
1131** Description      Process the callback for NFCEE discovery notification
1132**
1133** Returns          void
1134**
1135*******************************************************************************/
1136void nfa_ee_nci_disc_ntf(tNFA_EE_MSG *p_data)
1137{
1138    tNFC_NFCEE_INFO_REVT    *p_ee = p_data->disc_ntf.p_data;
1139    tNFA_EE_ECB             *p_cb = NULL;
1140    BOOLEAN                 notify_enable_done = FALSE;
1141    BOOLEAN                 notify_new_ee = FALSE;
1142    tNFA_EE_CBACK_DATA      evt_data = {0};
1143    tNFA_EE_INFO            *p_info;
1144    tNFA_EE_EM_STATE        new_em_state = NFA_EE_EM_STATE_MAX;
1145
1146    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);
1147    if (nfa_ee_cb.num_ee_expecting)
1148    {
1149        nfa_ee_cb.num_ee_expecting--;
1150        if ((nfa_ee_cb.num_ee_expecting == 0) && (nfa_ee_cb.p_ee_disc_cback != NULL))
1151        {
1152            /* Discovery triggered by API function */
1153            NFC_NfceeDiscover(FALSE);
1154        }
1155    }
1156    switch (nfa_ee_cb.em_state)
1157    {
1158    case NFA_EE_EM_STATE_INIT:
1159        if (nfa_ee_cb.cur_ee < NFA_EE_MAX_EE_SUPPORTED)
1160        {
1161            /* the cb can collect up to NFA_EE_MAX_EE_SUPPORTED ee_info */
1162            p_cb = &nfa_ee_cb.ecb[nfa_ee_cb.cur_ee++];
1163        }
1164
1165        if (nfa_ee_cb.num_ee_expecting == 0)
1166        {
1167            /* notify init_done callback */
1168            nfa_ee_cb.em_state = NFA_EE_EM_STATE_INIT_DONE;
1169            notify_enable_done = TRUE;
1170        }
1171        break;
1172
1173    case NFA_EE_EM_STATE_INIT_DONE:
1174        p_cb = nfa_ee_find_ecb (p_ee->nfcee_id);
1175        if (p_cb == NULL)
1176        {
1177            /* the NFCEE ID is not in the last NFCEE discovery
1178             * maybe it's a new one */
1179            p_cb = nfa_ee_find_ecb (NFA_EE_INVALID);
1180            if (p_cb)
1181            {
1182                nfa_ee_cb.cur_ee++;
1183                notify_new_ee = TRUE;
1184            }
1185        }
1186        else if (p_cb->ecb_flags & NFA_EE_ECB_FLAGS_ORDER)
1187        {
1188            nfa_ee_cb.cur_ee++;
1189            notify_new_ee = TRUE;
1190        }
1191        else
1192        {
1193            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);
1194        }
1195        break;
1196
1197    case NFA_EE_EM_STATE_RESTORING:
1198        p_cb = nfa_ee_find_ecb (p_ee->nfcee_id);
1199        if (p_cb == NULL)
1200        {
1201            /* the NFCEE ID is not in the last NFCEE discovery
1202             * maybe it's a new one */
1203            p_cb = nfa_ee_find_ecb (NFA_EE_INVALID);
1204            if (p_cb)
1205            {
1206                nfa_ee_cb.cur_ee++;
1207                notify_new_ee = TRUE;
1208            }
1209        }
1210        if (nfa_ee_cb.num_ee_expecting == 0)
1211        {
1212            /* notify init_done callback */
1213            notify_enable_done = TRUE;
1214            if (nfa_ee_restore_ntf_done())
1215            {
1216                new_em_state       = NFA_EE_EM_STATE_INIT_DONE;
1217            }
1218        }
1219        break;
1220    }
1221    NFA_TRACE_DEBUG1 ("nfa_ee_nci_disc_ntf cur_ee:%d", nfa_ee_cb.cur_ee);
1222
1223    if (p_cb)
1224    {
1225        p_cb->nfcee_id      = p_ee->nfcee_id;
1226        p_cb->ee_status     = p_ee->ee_status;
1227        p_cb->num_interface = p_ee->num_interface;
1228        memcpy(p_cb->ee_interface, p_ee->ee_interface, p_ee->num_interface);
1229        p_cb->num_tlvs      = p_ee->num_tlvs;
1230        memcpy(p_cb->ee_tlv, p_ee->ee_tlv, p_ee->num_tlvs * sizeof(tNFA_EE_TLV));
1231
1232        if (nfa_ee_cb.em_state == NFA_EE_EM_STATE_RESTORING)
1233        {
1234            /* NCI spec says: An NFCEE_DISCOVER_NTF that contains a Protocol type of "HCI Access"
1235             * SHALL NOT contain any other additional Protocol
1236             * i.e. check only first supported NFCEE interface is HCI access */
1237            /* NFA_HCI module handles restoring configurations for HCI access */
1238            if (p_cb->ee_interface[0] != NFC_NFCEE_INTERFACE_HCI_ACCESS)
1239            {
1240                if ((nfa_ee_cb.ee_flags & NFA_EE_FLAG_WAIT_HCI) == 0)
1241                {
1242                    nfa_ee_restore_one_ecb (p_cb);
1243                }
1244                /* else wait for NFA-HCI module to restore the HCI network information before enabling the NFCEE */
1245            }
1246        }
1247
1248        if ((nfa_ee_cb.p_ee_disc_cback == NULL) && (notify_new_ee == TRUE))
1249        {
1250            if (nfa_dm_is_active() && (p_cb->ee_status != NFA_EE_STATUS_REMOVED))
1251            {
1252                /* report this NFA_EE_NEW_EE_EVT only after NFA_DM_ENABLE_EVT is reported */
1253                p_info                  = &evt_data.new_ee;
1254                p_info->ee_handle       = NFA_HANDLE_GROUP_EE | (tNFA_HANDLE)p_cb->nfcee_id;
1255                p_info->ee_status       = p_cb->ee_status;
1256                p_info->num_interface   = p_cb->num_interface;
1257                p_info->num_tlvs        = p_cb->num_tlvs;
1258                memcpy(p_info->ee_interface, p_cb->ee_interface, p_cb->num_interface);
1259                memcpy(p_info->ee_tlv, p_cb->ee_tlv, p_cb->num_tlvs * sizeof(tNFA_EE_TLV));
1260                nfa_ee_report_event (NULL, NFA_EE_NEW_EE_EVT, &evt_data);
1261            }
1262        }
1263        else
1264            nfa_ee_report_disc_done(notify_enable_done);
1265
1266        if (p_cb->ecb_flags & NFA_EE_ECB_FLAGS_ORDER)
1267        {
1268            NFA_TRACE_DEBUG0 ("NFA_EE_ECB_FLAGS_ORDER");
1269            p_cb->ecb_flags &= ~NFA_EE_ECB_FLAGS_ORDER;
1270            nfa_ee_report_discover_req_evt();
1271        }
1272
1273    }
1274
1275    if (new_em_state != NFA_EE_EM_STATE_MAX)
1276    {
1277        nfa_ee_cb.em_state = new_em_state;
1278        nfa_ee_check_restore_complete();
1279    }
1280
1281    if ((nfa_ee_cb.cur_ee == nfa_ee_max_ee_cfg) && (nfa_ee_cb.em_state == NFA_EE_EM_STATE_INIT_DONE) )
1282    {
1283        if (nfa_ee_cb.discv_timer.in_use)
1284        {
1285            nfa_sys_stop_timer (&nfa_ee_cb.discv_timer);
1286            p_data->hdr.event = NFA_EE_DISCV_TIMEOUT_EVT;
1287            nfa_ee_evt_hdlr((BT_HDR *)p_data);
1288        }
1289    }
1290}
1291
1292/*******************************************************************************
1293**
1294** Function         nfa_ee_check_restore_complete
1295**
1296** Description      Check if restore the NFA-EE related configuration to the
1297**                  state prior to low power mode is complete.
1298**                  If complete, notify sys.
1299**
1300** Returns          void
1301**
1302*******************************************************************************/
1303void nfa_ee_check_restore_complete(void)
1304{
1305    UINT32  xx;
1306    tNFA_EE_ECB     *p_cb;
1307    BOOLEAN         proc_complete = TRUE;
1308
1309    p_cb = nfa_ee_cb.ecb;
1310    for (xx = 0; xx < nfa_ee_cb.cur_ee; xx++, p_cb++)
1311    {
1312        if (p_cb->ecb_flags & NFA_EE_ECB_FLAGS_RESTORE)
1313        {
1314            /* NFA_HCI module handles restoring configurations for HCI access.
1315             * ignore the restoring status for HCI Access */
1316            if (p_cb->ee_interface[0] != NFC_NFCEE_INTERFACE_HCI_ACCESS)
1317            {
1318                proc_complete = FALSE;
1319                break;
1320            }
1321        }
1322    }
1323
1324    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);
1325    if (proc_complete)
1326    {
1327        /* update routing table when NFA_EE_ROUT_TIMEOUT_EVT is received */
1328        if (nfa_ee_cb.ee_cfg_sts & NFA_EE_STS_PREV_ROUTING)
1329            nfa_ee_api_update_now(NULL);
1330
1331        nfa_ee_cb.em_state = NFA_EE_EM_STATE_INIT_DONE;
1332        nfa_sys_cback_notify_nfcc_power_mode_proc_complete (NFA_ID_EE);
1333    }
1334}
1335
1336/*******************************************************************************
1337**
1338** Function         nfa_ee_build_discover_req_evt
1339**
1340** Description      Build NFA_EE_DISCOVER_REQ_EVT for all active NFCEE
1341**
1342** Returns          void
1343**
1344*******************************************************************************/
1345static void nfa_ee_build_discover_req_evt (tNFA_EE_DISCOVER_REQ *p_evt_data)
1346{
1347    tNFA_EE_ECB           *p_cb;
1348    tNFA_EE_DISCOVER_INFO *p_info;
1349    UINT8                 xx;
1350
1351    if (!p_evt_data)
1352        return;
1353
1354    p_evt_data->num_ee = 0;
1355    p_cb               = nfa_ee_cb.ecb;
1356    p_info             = p_evt_data->ee_disc_info;
1357
1358    for (xx = 0; xx < nfa_ee_cb.cur_ee; xx++, p_cb++)
1359    {
1360        if (  (p_cb->ee_status & NFA_EE_STATUS_INT_MASK)
1361            ||(p_cb->ee_status != NFA_EE_STATUS_ACTIVE)
1362            ||((p_cb->ecb_flags & NFA_EE_ECB_FLAGS_DISC_REQ) == 0)  )
1363        {
1364            continue;
1365        }
1366        p_info->ee_handle       = (tNFA_HANDLE)p_cb->nfcee_id | NFA_HANDLE_GROUP_EE;
1367        p_info->la_protocol     = p_cb->la_protocol;
1368        p_info->lb_protocol     = p_cb->lb_protocol;
1369        p_info->lf_protocol     = p_cb->lf_protocol;
1370        p_info->lbp_protocol    = p_cb->lbp_protocol;
1371        p_evt_data->num_ee++;
1372        p_info++;
1373
1374        NFA_TRACE_DEBUG6 ("[%d] ee_handle:0x%x, listen protocol A:%d, B:%d, F:%d, BP:%d",
1375                          p_evt_data->num_ee, p_cb->nfcee_id,
1376                          p_cb->la_protocol, p_cb->lb_protocol, p_cb->lf_protocol, p_cb->lbp_protocol);
1377    }
1378
1379    p_evt_data->status     = NFA_STATUS_OK;
1380}
1381
1382/*******************************************************************************
1383**
1384** Function         nfa_ee_report_discover_req_evt
1385**
1386** Description      Report NFA_EE_DISCOVER_REQ_EVT for all active NFCEE
1387**
1388** Returns          void
1389**
1390*******************************************************************************/
1391static void nfa_ee_report_discover_req_evt(void)
1392{
1393    tNFA_EE_DISCOVER_REQ    evt_data;
1394
1395    if (nfa_ee_cb.p_enable_cback)
1396        (*nfa_ee_cb.p_enable_cback) (NFA_EE_DISC_STS_REQ);
1397
1398
1399    /* if this is restoring NFCC */
1400    if (!nfa_dm_is_active ())
1401    {
1402        NFA_TRACE_DEBUG0 ("nfa_ee_report_discover_req_evt DM is not active");
1403        return;
1404    }
1405
1406    nfa_ee_build_discover_req_evt (&evt_data);
1407    nfa_ee_report_event(NULL, NFA_EE_DISCOVER_REQ_EVT, (tNFA_EE_CBACK_DATA *)&evt_data);
1408}
1409
1410/*******************************************************************************
1411**
1412** Function         nfa_ee_nci_mode_set_rsp
1413**
1414** Description      Process the result for NFCEE ModeSet response
1415**
1416** Returns          void
1417**
1418*******************************************************************************/
1419void nfa_ee_nci_mode_set_rsp(tNFA_EE_MSG *p_data)
1420{
1421    tNFA_EE_ECB *p_cb;
1422    tNFA_EE_MODE_SET    mode_set;
1423    tNFC_NFCEE_MODE_SET_REVT    *p_rsp = p_data->mode_set_rsp.p_data;
1424
1425    NFA_TRACE_DEBUG2 ("nfa_ee_nci_mode_set_rsp() handle:0x%02x mode:%d", p_rsp->nfcee_id, p_rsp->mode);
1426    p_cb = nfa_ee_find_ecb (p_rsp->nfcee_id);
1427    if (p_cb == NULL)
1428    {
1429        NFA_TRACE_ERROR1 ("nfa_ee_nci_mode_set_rsp() Can not find cb for handle:0x%02x", p_rsp->nfcee_id);
1430        return;
1431    }
1432
1433    /* update routing table and vs on mode change */
1434    nfa_ee_start_timer();
1435
1436    if (p_rsp->status == NFA_STATUS_OK)
1437    {
1438
1439        if (p_rsp->mode == NFA_EE_MD_ACTIVATE)
1440        {
1441            p_cb->ee_status = NFC_NFCEE_STATUS_ACTIVE;
1442        }
1443        else
1444        {
1445            if (p_cb->tech_switch_on | p_cb->tech_switch_off | p_cb->tech_battery_off |
1446                p_cb->proto_switch_on| p_cb->proto_switch_off| p_cb->proto_battery_off |
1447                p_cb->aid_entries)
1448            {
1449                /* this NFCEE still has configuration when deactivated. clear the configuration */
1450                nfa_ee_cb.ee_cfged  &= ~nfa_ee_ecb_to_mask(p_cb);
1451                nfa_ee_cb.ee_cfg_sts|= NFA_EE_STS_CHANGED_ROUTING;
1452                NFA_TRACE_DEBUG0("deactivating/still configured. Force update");
1453            }
1454            p_cb->tech_switch_on    = p_cb->tech_switch_off = p_cb->tech_battery_off    = 0;
1455            p_cb->proto_switch_on   = p_cb->proto_switch_off= p_cb->proto_battery_off   = 0;
1456            p_cb->aid_entries       = 0;
1457            p_cb->ee_status = NFC_NFCEE_STATUS_INACTIVE;
1458        }
1459    }
1460    NFA_TRACE_DEBUG4 ("status:%d ecb_flags  :0x%02x ee_cfged:0x%02x ee_status:%d",
1461        p_rsp->status, p_cb->ecb_flags  , nfa_ee_cb.ee_cfged, p_cb->ee_status);
1462    if (p_cb->ecb_flags   & NFA_EE_ECB_FLAGS_RESTORE)
1463    {
1464        if (p_cb->conn_st == NFA_EE_CONN_ST_CONN)
1465        {
1466            /* NFA_HCI module handles restoring configurations for HCI access */
1467            if (p_cb->ee_interface[0] != NFC_NFCEE_INTERFACE_HCI_ACCESS)
1468            {
1469                NFC_ConnCreate(NCI_DEST_TYPE_NFCEE, p_cb->nfcee_id,  p_cb->use_interface, nfa_ee_conn_cback);
1470            }
1471        }
1472        else
1473        {
1474            p_cb->ecb_flags   &= ~NFA_EE_ECB_FLAGS_RESTORE;
1475            nfa_ee_check_restore_complete();
1476        }
1477    }
1478    else
1479    {
1480        mode_set.status     = p_rsp->status;
1481        mode_set.ee_handle  = (tNFA_HANDLE)p_rsp->nfcee_id | NFA_HANDLE_GROUP_EE;
1482        mode_set.ee_status  = p_cb->ee_status;
1483
1484        nfa_ee_report_event(p_cb->p_ee_cback, NFA_EE_MODE_SET_EVT, (tNFA_EE_CBACK_DATA *)&mode_set);
1485
1486        /* WAR for BLTH02787041 - Special for 2079xB4/B5, where we also need to report
1487           the discover request event when EE mode is changed from Inactive to Active. */
1488        if ((p_cb->ee_status == NFC_NFCEE_STATUS_INACTIVE)
1489            || (p_cb->ee_status == NFC_NFCEE_STATUS_ACTIVE))
1490        {
1491            /* Report NFA_EE_DISCOVER_REQ_EVT for all active NFCEE */
1492            nfa_ee_report_discover_req_evt();
1493        }
1494    }
1495}
1496
1497/*******************************************************************************
1498**
1499** Function         nfa_ee_nci_conn
1500**
1501** Description      process the connection callback events
1502**
1503** Returns          void
1504**
1505*******************************************************************************/
1506void nfa_ee_nci_conn(tNFA_EE_MSG *p_data)
1507{
1508    tNFA_EE_ECB      *p_cb;
1509    tNFA_EE_NCI_CONN    *p_cbk   = &p_data->conn;
1510    tNFC_CONN           *p_conn  = p_data->conn.p_data;
1511    BT_HDR              *p_pkt   = NULL;
1512    tNFA_EE_CBACK_DATA  evt_data = {0};
1513    tNFA_EE_EVT         event    = NFA_EE_INVALID;
1514    tNFA_EE_CBACK       *p_cback = NULL;
1515
1516    if (p_cbk->event == NFC_CONN_CREATE_CEVT)
1517    {
1518        p_cb = nfa_ee_find_ecb (p_cbk->p_data->conn_create.id);
1519    }
1520    else
1521    {
1522        p_cb = nfa_ee_find_ecb_by_conn_id (p_cbk->conn_id);
1523        if (p_cbk->event == NFC_DATA_CEVT)
1524            p_pkt = p_conn->data.p_data;
1525    }
1526
1527    if (p_cb)
1528    {
1529        p_cback         = p_cb->p_ee_cback;
1530        evt_data.handle = (tNFA_HANDLE)p_cb->nfcee_id | NFA_HANDLE_GROUP_EE;
1531        switch (p_cbk->event)
1532        {
1533        case NFC_CONN_CREATE_CEVT:
1534            if (p_conn->conn_create.status == NFC_STATUS_OK)
1535            {
1536                p_cb->conn_id = p_cbk->conn_id;
1537                p_cb->conn_st = NFA_EE_CONN_ST_CONN;
1538            }
1539            else
1540            {
1541                p_cb->conn_st = NFA_EE_CONN_ST_NONE;
1542            }
1543            if (p_cb->ecb_flags & NFA_EE_ECB_FLAGS_RESTORE)
1544            {
1545                p_cb->ecb_flags &= ~NFA_EE_ECB_FLAGS_RESTORE;
1546                nfa_ee_check_restore_complete();
1547            }
1548            else
1549            {
1550                evt_data.connect.status       = p_conn->conn_create.status;
1551                evt_data.connect.ee_interface = p_cb->use_interface;
1552                event = NFA_EE_CONNECT_EVT;
1553            }
1554            break;
1555
1556        case NFC_CONN_CLOSE_CEVT:
1557            if (p_cb->conn_st != NFA_EE_CONN_ST_DISC)
1558                event = NFA_EE_DISCONNECT_EVT;
1559            p_cb->conn_st    = NFA_EE_CONN_ST_NONE;
1560            p_cb->p_ee_cback = NULL;
1561            p_cb->conn_id    = 0;
1562            if (nfa_ee_cb.em_state == NFA_EE_EM_STATE_DISABLING)
1563            {
1564                if (nfa_ee_cb.ee_flags & NFA_EE_FLAG_WAIT_DISCONN)
1565                {
1566                    if (nfa_ee_cb.num_ee_expecting)
1567                    {
1568                        nfa_ee_cb.num_ee_expecting--;
1569                    }
1570                }
1571                if (nfa_ee_cb.num_ee_expecting == 0)
1572                {
1573                    nfa_ee_cb.ee_flags &= ~NFA_EE_FLAG_WAIT_DISCONN;
1574                    nfa_ee_check_disable();
1575                }
1576            }
1577            break;
1578
1579        case NFC_DATA_CEVT:
1580            if (p_cb->conn_st == NFA_EE_CONN_ST_CONN)
1581            {
1582                /* report data event only in connected state */
1583                if (p_cb->p_ee_cback && p_pkt)
1584                {
1585                    evt_data.data.len   = p_pkt->len;
1586                    evt_data.data.p_buf = (UINT8 *)(p_pkt+1) + p_pkt->offset;
1587                    event               = NFA_EE_DATA_EVT;
1588                    p_pkt               = NULL; /* so this function does not free this GKI buffer */
1589                }
1590            }
1591            break;
1592        }
1593
1594        if ((event != NFA_EE_INVALID) && (p_cback))
1595            (*p_cback)(event, &evt_data);
1596    }
1597    if (p_pkt)
1598        GKI_freebuf (p_pkt);
1599}
1600
1601
1602/*******************************************************************************
1603**
1604** Function         nfa_ee_nci_action_ntf
1605**
1606** Description      process the NFCEE action callback event
1607**
1608** Returns          void
1609**
1610*******************************************************************************/
1611void nfa_ee_nci_action_ntf(tNFA_EE_MSG *p_data)
1612{
1613    tNFC_EE_ACTION_REVT *p_cbk = p_data->act.p_data;
1614    tNFA_EE_ACTION      evt_data;
1615
1616    evt_data.ee_handle  = (tNFA_HANDLE)p_cbk->nfcee_id | NFA_HANDLE_GROUP_EE;
1617    evt_data.trigger    = p_cbk->act_data.trigger;
1618    memcpy (&(evt_data.param), &(p_cbk->act_data.param), sizeof (tNFA_EE_ACTION_PARAM));
1619    nfa_ee_report_event(NULL, NFA_EE_ACTION_EVT, (tNFA_EE_CBACK_DATA *)&evt_data);
1620}
1621
1622/*******************************************************************************
1623**
1624** Function         nfa_ee_nci_disc_req_ntf
1625**
1626** Description      process the NFCEE discover request callback event
1627**
1628** Returns          void
1629**
1630*******************************************************************************/
1631void nfa_ee_nci_disc_req_ntf(tNFA_EE_MSG *p_data)
1632{
1633    tNFC_EE_DISCOVER_REQ_REVT   *p_cbk = p_data->disc_req.p_data;
1634    tNFA_HANDLE         ee_handle;
1635    tNFA_EE_ECB         *p_cb = NULL;
1636    UINT8               report_ntf = 0;
1637    UINT8 xx;
1638
1639    NFA_TRACE_DEBUG2 ("nfa_ee_nci_disc_req_ntf () num_info: %d cur_ee:%d", p_cbk->num_info, nfa_ee_cb.cur_ee );
1640
1641    for (xx = 0; xx < p_cbk->num_info; xx++)
1642    {
1643        ee_handle = NFA_HANDLE_GROUP_EE|p_cbk->info[xx].nfcee_id;
1644
1645        p_cb = nfa_ee_find_ecb (p_cbk->info[xx].nfcee_id);
1646        if (!p_cb)
1647        {
1648            NFA_TRACE_DEBUG1 ("Cannot find cb for NFCEE: 0x%x", p_cbk->info[xx].nfcee_id);
1649            p_cb = nfa_ee_find_ecb (NFA_EE_INVALID);
1650            if (p_cb)
1651            {
1652                p_cb->nfcee_id   = p_cbk->info[xx].nfcee_id;
1653                p_cb->ecb_flags |= NFA_EE_ECB_FLAGS_ORDER;
1654            }
1655            else
1656            {
1657                NFA_TRACE_ERROR1 ("Cannot allocate cb for NFCEE: 0x%x", p_cbk->info[xx].nfcee_id);
1658                continue;
1659            }
1660        }
1661        else
1662        {
1663            report_ntf  |= nfa_ee_ecb_to_mask (p_cb);
1664        }
1665
1666        p_cb->ecb_flags |= NFA_EE_ECB_FLAGS_DISC_REQ;
1667        if (p_cbk->info[xx].op == NFC_EE_DISC_OP_ADD)
1668        {
1669            if (p_cbk->info[xx].tech_n_mode == NFC_DISCOVERY_TYPE_LISTEN_A)
1670            {
1671                p_cb->la_protocol = p_cbk->info[xx].protocol;
1672            }
1673            else if (p_cbk->info[xx].tech_n_mode == NFC_DISCOVERY_TYPE_LISTEN_B)
1674            {
1675                p_cb->lb_protocol = p_cbk->info[xx].protocol;
1676            }
1677            else if (p_cbk->info[xx].tech_n_mode == NFC_DISCOVERY_TYPE_LISTEN_F)
1678            {
1679                p_cb->lf_protocol = p_cbk->info[xx].protocol;
1680            }
1681            else if (p_cbk->info[xx].tech_n_mode == NFC_DISCOVERY_TYPE_LISTEN_B_PRIME)
1682            {
1683                p_cb->lbp_protocol = p_cbk->info[xx].protocol;
1684            }
1685            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",
1686                p_cb->nfcee_id, p_cb->ee_status, p_cb->ecb_flags,
1687                p_cb->la_protocol, p_cb->lb_protocol, p_cb->lf_protocol);
1688        }
1689        else
1690        {
1691            if (p_cbk->info[xx].tech_n_mode == NFC_DISCOVERY_TYPE_LISTEN_A)
1692            {
1693                p_cb->la_protocol = 0;
1694            }
1695            else if (p_cbk->info[xx].tech_n_mode == NFC_DISCOVERY_TYPE_LISTEN_B)
1696            {
1697                p_cb->lb_protocol = 0;
1698            }
1699            else if (p_cbk->info[xx].tech_n_mode == NFC_DISCOVERY_TYPE_LISTEN_F)
1700            {
1701                p_cb->lf_protocol = 0;
1702            }
1703            else if (p_cbk->info[xx].tech_n_mode == NFC_DISCOVERY_TYPE_LISTEN_B_PRIME)
1704            {
1705                p_cb->lbp_protocol = 0;
1706            }
1707        }
1708    }
1709
1710
1711    /* Report NFA_EE_DISCOVER_REQ_EVT for all active NFCEE */
1712    if (report_ntf)
1713        nfa_ee_report_discover_req_evt();
1714
1715}
1716
1717/*******************************************************************************
1718**
1719** Function         nfa_ee_is_active
1720**
1721** Description      Check if the given NFCEE is active
1722**
1723** Returns          TRUE if the given NFCEE is active
1724**
1725*******************************************************************************/
1726BOOLEAN nfa_ee_is_active (tNFA_HANDLE nfcee_id)
1727{
1728    BOOLEAN is_active = FALSE;
1729    int     xx;
1730    tNFA_EE_ECB  *p_cb = nfa_ee_cb.ecb;
1731
1732    if ((NFA_HANDLE_GROUP_MASK & nfcee_id) == NFA_HANDLE_GROUP_EE)
1733        nfcee_id    &= NFA_HANDLE_MASK;
1734
1735    /* compose output */
1736    for (xx = 0; xx < nfa_ee_cb.cur_ee; xx++, p_cb++)
1737    {
1738        if ((tNFA_HANDLE)p_cb->nfcee_id == nfcee_id)
1739        {
1740            if (p_cb->ee_status == NFA_EE_STATUS_ACTIVE)
1741            {
1742                is_active = TRUE;
1743            }
1744            break;
1745        }
1746    }
1747    return is_active;
1748}
1749
1750/*******************************************************************************
1751**
1752** Function         nfa_ee_get_tech_route
1753**
1754** Description      Given a power state, find the technology routing destination.
1755**                  The result is filled in the given p_handles
1756**                  in the order of A, B, F, Bprime
1757**
1758** Returns          None
1759**
1760*******************************************************************************/
1761void nfa_ee_get_tech_route (UINT8 power_state, UINT8 *p_handles)
1762{
1763    int     xx, yy;
1764    tNFA_EE_ECB *p_cb;
1765    UINT8   tech_mask_list[NFA_EE_MAX_TECH_ROUTE] =
1766    {
1767        NFA_TECHNOLOGY_MASK_A,
1768        NFA_TECHNOLOGY_MASK_B,
1769        NFA_TECHNOLOGY_MASK_F,
1770        NFA_TECHNOLOGY_MASK_B_PRIME
1771    };
1772
1773    NFA_TRACE_DEBUG1("nfa_ee_get_tech_route(): %d", power_state);
1774
1775    for (xx = 0; xx < NFA_EE_MAX_TECH_ROUTE; xx++)
1776    {
1777        p_handles[xx] = NFC_DH_ID;
1778        p_cb = &nfa_ee_cb.ecb[nfa_ee_cb.cur_ee - 1];
1779        for (yy = 0; yy < nfa_ee_cb.cur_ee; yy++, p_cb--)
1780        {
1781            if (p_cb->ee_status == NFC_NFCEE_STATUS_ACTIVE)
1782            {
1783                switch (power_state)
1784                {
1785                case NFA_EE_PWR_STATE_ON:
1786                    if (p_cb->tech_switch_on & tech_mask_list[xx])
1787                        p_handles[xx] = p_cb->nfcee_id;
1788                    break;
1789                case NFA_EE_PWR_STATE_SWITCH_OFF:
1790                    if (p_cb->tech_switch_off & tech_mask_list[xx])
1791                        p_handles[xx] = p_cb->nfcee_id;
1792                    break;
1793                case NFA_EE_PWR_STATE_BATT_OFF:
1794                    if (p_cb->tech_battery_off & tech_mask_list[xx])
1795                        p_handles[xx] = p_cb->nfcee_id;
1796                    break;
1797                }
1798            }
1799        }
1800    }
1801    NFA_TRACE_DEBUG4("0x%x, 0x%x, 0x%x, 0x%x", p_handles[0], p_handles[1], p_handles[2], p_handles[3]);
1802}
1803
1804/*******************************************************************************
1805**
1806** Function         nfa_ee_check_set_routing
1807**
1808** Description      If the new size exceeds the capacity of next block,
1809**                  send the routing command now and reset the related parameters
1810**
1811** Returns          void
1812**
1813*******************************************************************************/
1814void nfa_ee_check_set_routing(UINT16 new_size, int *p_max_len, UINT8 *p, int *p_cur_offset)
1815{
1816    UINT8   max_tlv = (UINT8)((*p_max_len > NFA_EE_ROUT_MAX_TLV_SIZE)?NFA_EE_ROUT_MAX_TLV_SIZE:*p_max_len);
1817    tNFA_STATUS status = NFA_STATUS_OK;
1818
1819    if (new_size + *p_cur_offset > max_tlv)
1820    {
1821        NFC_SetRouting(TRUE, *p, *p_cur_offset, p + 1);
1822        /* after the routing command is sent, re-use the same buffer to send the next routing command.
1823         * reset the related parameters */
1824        if (*p_max_len > *p_cur_offset)
1825            *p_max_len     -= *p_cur_offset;/* the max is reduced */
1826        else
1827            *p_max_len      = 0;
1828        *p_cur_offset   = 0;                /* nothing is in queue any more */
1829        *p              = 0;                /* num_tlv=0 */
1830    }
1831}
1832
1833/*******************************************************************************
1834**
1835** Function         nfa_ee_route_add_one_ecb
1836**
1837** Description      Add the routing entries for one NFCEE/DH
1838**
1839** Returns          NFA_STATUS_OK, if ok to continue
1840**
1841*******************************************************************************/
1842tNFA_STATUS nfa_ee_route_add_one_ecb(tNFA_EE_ECB *p_cb, int *p_max_len, BOOLEAN more, UINT8 *ps, int *p_cur_offset)
1843{
1844    UINT8   *p, *pa;
1845    UINT16  tlv_size;
1846    UINT8   num_tlv, len;
1847    int     xx;
1848    int     start_offset;
1849    UINT8   power_cfg = 0;
1850    UINT8   *pp = ps + *p_cur_offset;
1851    UINT8   entry_size;
1852    UINT8   max_tlv;
1853    UINT8   *p_start;
1854    UINT8   new_size;
1855    tNFA_STATUS status = NFA_STATUS_OK;
1856
1857    nfa_ee_check_set_routing (p_cb->size_mask, p_max_len, ps, p_cur_offset);
1858    max_tlv = (UINT8)((*p_max_len > NFA_EE_ROUT_MAX_TLV_SIZE)?NFA_EE_ROUT_MAX_TLV_SIZE:*p_max_len);
1859    /* use the first byte of the buffer (ps) to keep the num_tlv */
1860    num_tlv  = *ps;
1861    NFA_TRACE_DEBUG5 ("nfa_ee_route_add_one_ecb max_len:%d, max_tlv:%d, cur_offset:%d, more:%d, num_tlv:%d",
1862        *p_max_len, max_tlv, *p_cur_offset, more, num_tlv);
1863    pp       = ps + 1 + *p_cur_offset;
1864    p        = pp;
1865    tlv_size = (UINT8)*p_cur_offset;
1866    /* add the Technology based routing */
1867    for (xx = 0; xx < NFA_EE_NUM_TECH; xx++)
1868    {
1869        power_cfg = 0;
1870        if (p_cb->tech_switch_on & nfa_ee_tech_mask_list[xx])
1871            power_cfg |= NCI_ROUTE_PWR_STATE_ON;
1872        if (p_cb->tech_switch_off & nfa_ee_tech_mask_list[xx])
1873            power_cfg |= NCI_ROUTE_PWR_STATE_SWITCH_OFF;
1874        if (p_cb->tech_battery_off & nfa_ee_tech_mask_list[xx])
1875            power_cfg |= NCI_ROUTE_PWR_STATE_BATT_OFF;
1876        if (power_cfg)
1877        {
1878            *pp++   = NFC_ROUTE_TAG_TECH;
1879            *pp++   = 3;
1880            *pp++   = p_cb->nfcee_id;
1881            *pp++   = power_cfg;
1882            *pp++   = nfa_ee_tech_list[xx];
1883            num_tlv++;
1884            if (power_cfg != NCI_ROUTE_PWR_STATE_ON)
1885                nfa_ee_cb.ee_cfged  |= NFA_EE_CFGED_OFF_ROUTING;
1886        }
1887    }
1888
1889    /* add the Protocol based routing */
1890    for (xx = 0; xx < NFA_EE_NUM_PROTO; xx++)
1891    {
1892        power_cfg = 0;
1893        if (p_cb->proto_switch_on & nfa_ee_proto_mask_list[xx])
1894            power_cfg |= NCI_ROUTE_PWR_STATE_ON;
1895        if (p_cb->proto_switch_off & nfa_ee_proto_mask_list[xx])
1896            power_cfg |= NCI_ROUTE_PWR_STATE_SWITCH_OFF;
1897        if (p_cb->proto_battery_off & nfa_ee_proto_mask_list[xx])
1898            power_cfg |= NCI_ROUTE_PWR_STATE_BATT_OFF;
1899        if (power_cfg)
1900        {
1901            *pp++   = NFC_ROUTE_TAG_PROTO;
1902            *pp++   = 3;
1903            *pp++   = p_cb->nfcee_id;
1904            *pp++   = power_cfg;
1905            *pp++   = nfa_ee_proto_list[xx];
1906            num_tlv++;
1907            if (power_cfg != NCI_ROUTE_PWR_STATE_ON)
1908                nfa_ee_cb.ee_cfged  |= NFA_EE_CFGED_OFF_ROUTING;
1909        }
1910    }
1911
1912    /* update the num_tlv and current offset */
1913    entry_size       = (UINT8)(pp - p);
1914    *p_cur_offset   += entry_size;
1915    *ps              = num_tlv;
1916    /* add the AID routing */
1917    if (p_cb->aid_entries)
1918    {
1919        start_offset = 0;
1920        for (xx = 0; xx < p_cb->aid_entries; xx++)
1921        {
1922            p_start     = pp; /* rememebr the beginning of this AID routing entry, just in case we need to put it in next command */
1923            /* add one AID entry */
1924            if (p_cb->aid_rt_info[xx] & NFA_EE_AE_ROUTE)
1925            {
1926                num_tlv++;
1927                pa      = &p_cb->aid_cfg[start_offset];
1928                pa ++; /* EMV tag */
1929                len     = *pa++; /* aid_len */
1930                *pp++   = NFC_ROUTE_TAG_AID;
1931                *pp++   = len + 2;
1932                *pp++   = p_cb->nfcee_id;
1933                *pp++   = p_cb->aid_pwr_cfg[xx];
1934                /* copy the AID */
1935                memcpy(pp, pa, len);
1936                pp     += len;
1937            }
1938            start_offset += p_cb->aid_len[xx];
1939            new_size        = (UINT8)(pp - p_start);
1940            nfa_ee_check_set_routing(new_size, p_max_len, ps, p_cur_offset);
1941            if (*ps == 0)
1942            {
1943                /* just sent routing command, update local */
1944                *ps      = 1;
1945                num_tlv  = *ps;
1946                *p_cur_offset = new_size;
1947                pp       = ps + 1;
1948                p        = pp;
1949                tlv_size = (UINT8)*p_cur_offset;
1950                max_tlv  = (UINT8)((*p_max_len > NFA_EE_ROUT_MAX_TLV_SIZE)?NFA_EE_ROUT_MAX_TLV_SIZE:*p_max_len);
1951                memcpy (p, p_start, new_size);
1952                pp      += new_size;
1953            }
1954            else
1955            {
1956                /* add the new entry */
1957                *ps              = num_tlv;
1958                *p_cur_offset   += new_size;
1959            }
1960        }
1961    }
1962
1963    tlv_size   = nfa_ee_total_lmrt_size();
1964    if (tlv_size)
1965    {
1966        nfa_ee_cb.ee_cfged |= nfa_ee_ecb_to_mask(p_cb);
1967    }
1968    if (p_cb->ecb_flags   & NFA_EE_ECB_FLAGS_ROUTING)
1969    {
1970        nfa_ee_cb.ee_cfg_sts   |= NFA_EE_STS_CHANGED_ROUTING;
1971    }
1972    NFA_TRACE_DEBUG2 ("ee_cfg_sts:0x%02x lmrt_size:%d", nfa_ee_cb.ee_cfg_sts, tlv_size);
1973
1974    if (more == FALSE)
1975    {
1976        /* last entry. update routing table now */
1977        if (nfa_ee_cb.ee_cfg_sts & NFA_EE_STS_CHANGED_ROUTING)
1978        {
1979            if (tlv_size)
1980            {
1981                nfa_ee_cb.ee_cfg_sts       |= NFA_EE_STS_PREV_ROUTING;
1982            }
1983            else
1984            {
1985                nfa_ee_cb.ee_cfg_sts       &= ~NFA_EE_STS_PREV_ROUTING;
1986            }
1987            NFA_TRACE_DEBUG2 ("nfa_ee_route_add_one_ecb: set routing num_tlv:%d tlv_size:%d", num_tlv, tlv_size);
1988            NFC_SetRouting(more, num_tlv, (UINT8)(*p_cur_offset), ps + 1);
1989        }
1990        else if (nfa_ee_cb.ee_cfg_sts & NFA_EE_STS_PREV_ROUTING)
1991        {
1992            if (tlv_size == 0)
1993            {
1994                nfa_ee_cb.ee_cfg_sts       &= ~NFA_EE_STS_PREV_ROUTING;
1995                /* indicated routing is configured to NFCC */
1996                nfa_ee_cb.ee_cfg_sts       |= NFA_EE_STS_CHANGED_ROUTING;
1997                NFC_SetRouting(more, 0, 0, ps + 1);
1998            }
1999        }
2000    }
2001
2002    return status;
2003}
2004
2005
2006/*******************************************************************************
2007**
2008** Function         nfa_ee_need_recfg
2009**
2010** Description      Check if any API function to configure the routing table or
2011**                  VS is called since last update
2012**
2013**                  The algorithm for the NFCEE configuration handling is as follows:
2014**
2015**                  Each NFCEE_ID/DH has its own control block - tNFA_EE_ECB
2016**                  Each control block uses ecb_flags to keep track if an API
2017**                  that changes routing/VS is invoked.
2018**                  This ecb_flags is cleared at the end of nfa_ee_update_rout().
2019**
2020**                  nfa_ee_cb.ee_cfged is the bitmask of the control blocks with
2021**                  routing/VS configuration and NFA_EE_CFGED_UPDATE_NOW.
2022**                  nfa_ee_cb.ee_cfged is cleared and re-calculated at the end of
2023**                  nfa_ee_update_rout().
2024**
2025**                  nfa_ee_cb.ee_cfg_sts is used to check is any status is changed
2026**                  and the associated command is issued to NFCC.
2027**                  nfa_ee_cb.ee_cfg_sts is AND with NFA_EE_STS_PREV at the end of
2028**                  nfa_ee_update_rout() to clear the NFA_EE_STS_CHANGED bits
2029**                  (except NFA_EE_STS_CHANGED_CANNED_VS is cleared in nfa_ee_vs_cback)
2030**
2031** Returns          TRUE if any configuration is changed
2032**
2033*******************************************************************************/
2034static BOOLEAN nfa_ee_need_recfg(void)
2035{
2036    BOOLEAN needed = FALSE;
2037    UINT32  xx;
2038    tNFA_EE_ECB  *p_cb;
2039    UINT8   mask;
2040
2041    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);
2042    /* if no routing/vs is configured, do not need to send the info to NFCC */
2043    if (nfa_ee_cb.ee_cfged || nfa_ee_cb.ee_cfg_sts)
2044    {
2045        if (nfa_ee_cb.ee_cfged & NFA_EE_CFGED_UPDATE_NOW)
2046        {
2047            needed = TRUE;
2048        }
2049        else if (nfa_ee_cb.ee_cfg_sts & NFA_EE_STS_CHANGED)
2050        {
2051            needed = TRUE;
2052        }
2053        else
2054        {
2055            p_cb = &nfa_ee_cb.ecb[NFA_EE_CB_4_DH];
2056            mask = 1 << NFA_EE_CB_4_DH;
2057            for (xx = 0; xx <= nfa_ee_cb.cur_ee; xx++)
2058            {
2059                NFA_TRACE_DEBUG3("%d: ecb_flags  : 0x%02x, mask: 0x%02x", xx, p_cb->ecb_flags  , mask);
2060                if ((p_cb->ecb_flags  ) && (nfa_ee_cb.ee_cfged & mask))
2061                {
2062                    needed = TRUE;
2063                    break;
2064                }
2065                p_cb = &nfa_ee_cb.ecb[xx];
2066                mask = 1 << xx;
2067            }
2068        }
2069    }
2070
2071    return needed;
2072}
2073
2074/*******************************************************************************
2075**
2076** Function         nfa_ee_rout_timeout
2077**
2078** Description      Anytime VS or routing entries are changed,
2079**                  a 1 second timer is started. This function is called when
2080**                  the timer expires or NFA_EeUpdateNow() is called.
2081**
2082** Returns          void
2083**
2084*******************************************************************************/
2085void nfa_ee_rout_timeout(tNFA_EE_MSG *p_data)
2086{
2087    NFA_TRACE_DEBUG0("nfa_ee_rout_timeout()");
2088    if (nfa_ee_need_recfg())
2089    {
2090        /* discovery is not started */
2091        nfa_ee_update_rout();
2092    }
2093}
2094
2095/*******************************************************************************
2096**
2097** Function         nfa_ee_discv_timeout
2098**
2099** Description
2100**
2101**
2102**
2103** Returns          void
2104**
2105*******************************************************************************/
2106void nfa_ee_discv_timeout(tNFA_EE_MSG *p_data)
2107{
2108    NFC_NfceeDiscover(FALSE);
2109    if (nfa_ee_cb.p_enable_cback)
2110        (*nfa_ee_cb.p_enable_cback)(NFA_EE_DISC_STS_OFF);
2111}
2112
2113/*******************************************************************************
2114**
2115** Function         nfa_ee_lmrt_to_nfcc
2116**
2117** Description      This function would set the listen mode routing table
2118**                  to NFCC.
2119**
2120** Returns          void
2121**
2122*******************************************************************************/
2123void nfa_ee_lmrt_to_nfcc(tNFA_EE_MSG *p_data)
2124{
2125    int xx;
2126    tNFA_EE_ECB          *p_cb;
2127    UINT8   *p = NULL;
2128    BOOLEAN more = TRUE;
2129    UINT8   last_active = NFA_EE_INVALID;
2130    int     max_len, len;
2131    tNFA_STATUS status = NFA_STATUS_FAILED;
2132    int     cur_offset;
2133    UINT8   max_tlv;
2134
2135    /* update routing table: DH and the activated NFCEEs */
2136    p = (UINT8 *)GKI_getbuf(NFA_EE_ROUT_BUF_SIZE);
2137    if (p == NULL)
2138    {
2139        NFA_TRACE_ERROR0 ("nfa_ee_lmrt_to_nfcc() no buffer to send routing info.");
2140        nfa_ee_report_event( NULL, NFA_EE_NO_MEM_ERR_EVT, (tNFA_EE_CBACK_DATA *)&status);
2141        return;
2142    }
2143
2144    /* find the last active NFCEE. */
2145    p_cb = &nfa_ee_cb.ecb[nfa_ee_cb.cur_ee - 1];
2146    for (xx = 0; xx < nfa_ee_cb.cur_ee; xx++, p_cb--)
2147    {
2148        if (p_cb->ee_status == NFC_NFCEE_STATUS_ACTIVE)
2149        {
2150            if (last_active == NFA_EE_INVALID)
2151            {
2152                last_active = p_cb->nfcee_id;
2153                NFA_TRACE_DEBUG1 ("last_active: 0x%x", last_active);
2154            }
2155        }
2156    }
2157    if (last_active == NFA_EE_INVALID)
2158    {
2159        more = FALSE;
2160    }
2161
2162    /* add the routing for DH first */
2163    status  = NFA_STATUS_OK;
2164    max_len = NFC_GetLmrtSize();
2165    max_tlv = (UINT8)((max_len > NFA_EE_ROUT_MAX_TLV_SIZE)?NFA_EE_ROUT_MAX_TLV_SIZE:max_len);
2166    cur_offset  = 0;
2167    /* use the first byte of the buffer (p) to keep the num_tlv */
2168    *p          = 0;
2169    status = nfa_ee_route_add_one_ecb(&nfa_ee_cb.ecb[NFA_EE_CB_4_DH], &max_len, more, p, &cur_offset);
2170
2171    /* add only what is supported by NFCC. report overflow */
2172    if (status == NFA_STATUS_OK)
2173    {
2174        /* add the routing for NFCEEs */
2175        p_cb = &nfa_ee_cb.ecb[0];
2176        for (xx = 0; (xx < nfa_ee_cb.cur_ee) && more; xx++, p_cb++)
2177        {
2178            len = 0;
2179            if (p_cb->ee_status == NFC_NFCEE_STATUS_ACTIVE)
2180            {
2181                NFA_TRACE_DEBUG2 ("nfcee_id:0x%x, last_active: 0x%x", p_cb->nfcee_id, last_active);
2182                if (last_active == p_cb->nfcee_id)
2183                    more = FALSE;
2184                status = nfa_ee_route_add_one_ecb(p_cb, &max_len, more, p, &cur_offset);
2185                if (status != NFA_STATUS_OK)
2186                {
2187                    more    = FALSE;
2188                }
2189            }
2190        }
2191    }
2192    if (status != NFA_STATUS_OK)
2193    {
2194        nfa_ee_report_event( NULL, NFA_EE_ROUT_ERR_EVT, (tNFA_EE_CBACK_DATA *)&status);
2195    }
2196    GKI_freebuf(p);
2197}
2198
2199/*******************************************************************************
2200**
2201** Function         nfa_ee_update_rout
2202**
2203** Description      This function would set the VS and listen mode routing table
2204**                  to NFCC.
2205**
2206** Returns          void
2207**
2208*******************************************************************************/
2209void nfa_ee_update_rout(void)
2210{
2211    int xx;
2212    tNFA_EE_ECB          *p_cb;
2213    UINT8   mask;
2214    BT_HDR  msg;
2215
2216    NFA_TRACE_DEBUG1 ("nfa_ee_update_rout ee_cfg_sts:0x%02x", nfa_ee_cb.ee_cfg_sts);
2217
2218    /* use action function to send routing and VS configuration to NFCC */
2219    msg.event = NFA_EE_CFG_TO_NFCC_EVT;
2220    nfa_ee_evt_hdlr (&msg);
2221
2222    /* all configuration is updated to NFCC, clear the status mask */
2223    nfa_ee_cb.ee_cfg_sts   &= NFA_EE_STS_PREV;
2224    nfa_ee_cb.ee_cfged  = 0;
2225    p_cb                = &nfa_ee_cb.ecb[0];
2226    for (xx = 0; xx < NFA_EE_NUM_ECBS; xx++, p_cb++)
2227    {
2228        p_cb->ecb_flags     = 0;
2229        mask                = (1 << xx);
2230        if (p_cb->tech_switch_on | p_cb->tech_switch_off | p_cb->tech_battery_off |
2231            p_cb->proto_switch_on| p_cb->proto_switch_off| p_cb->proto_battery_off |
2232            p_cb->aid_entries)
2233        {
2234            /* this entry has routing configuration. mark it configured */
2235            nfa_ee_cb.ee_cfged  |= mask;
2236        }
2237    }
2238    NFA_TRACE_DEBUG2 ("ee_cfg_sts:0x%02x ee_cfged:0x%02x", nfa_ee_cb.ee_cfg_sts, nfa_ee_cb.ee_cfged);
2239}
2240
2241
2242