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 *  NFA interface to NFCEE - API functions
23 *
24 ******************************************************************************/
25#include <string.h>
26#include "nfa_ee_api.h"
27#include "nfa_ee_int.h"
28#include "nfa_sys_int.h"
29#include "nfa_dm_int.h"
30
31/*****************************************************************************
32**  APIs
33*****************************************************************************/
34/*******************************************************************************
35**
36** Function         NFA_EeDiscover
37**
38** Description      This function retrieves the NFCEE information from NFCC.
39**                  The NFCEE information is reported in NFA_EE_DISCOVER_EVT.
40**
41**                  This function may be called when a system supports removable
42**                  NFCEEs,
43**
44** Returns          NFA_STATUS_OK if information is retrieved successfully
45**                  NFA_STATUS_FAILED If wrong state (retry later)
46**                  NFA_STATUS_INVALID_PARAM If bad parameter
47**
48*******************************************************************************/
49tNFA_STATUS NFA_EeDiscover(tNFA_EE_CBACK *p_cback)
50{
51    tNFA_EE_API_DISCOVER *p_msg;
52    tNFA_STATUS status = NFA_STATUS_FAILED;
53
54    NFA_TRACE_API0 ("NFA_EeDiscover()");
55
56    if (nfa_ee_cb.em_state != NFA_EE_EM_STATE_INIT_DONE)
57    {
58        NFA_TRACE_ERROR1 ("NFA_EeDiscover bad em state: %d", nfa_ee_cb.em_state);
59        status = NFA_STATUS_FAILED;
60    }
61    else if ((nfa_ee_cb.p_ee_disc_cback != NULL) || (p_cback == NULL))
62    {
63        NFA_TRACE_ERROR0 ("NFA_EeDiscover() in progress or NULL callback function");
64        status = NFA_STATUS_INVALID_PARAM;
65    }
66    else if ((p_msg = (tNFA_EE_API_DISCOVER *) GKI_getbuf (sizeof(tNFA_EE_API_DISCOVER))) != NULL)
67    {
68        p_msg->hdr.event    = NFA_EE_API_DISCOVER_EVT;
69        p_msg->p_cback      = p_cback;
70
71        nfa_sys_sendmsg (p_msg);
72
73        status = NFA_STATUS_OK;
74    }
75
76    return status;
77}
78
79/*******************************************************************************
80**
81** Function         NFA_EeGetInfo
82**
83** Description      This function retrieves the NFCEE information from NFA.
84**                  The actual number of NFCEE is returned in p_num_nfcee
85**                  and NFCEE information is returned in p_info
86**
87** Returns          NFA_STATUS_OK if information is retrieved successfully
88**                  NFA_STATUS_FAILED If wrong state (retry later)
89**                  NFA_STATUS_INVALID_PARAM If bad parameter
90**
91*******************************************************************************/
92tNFA_STATUS NFA_EeGetInfo(UINT8        *p_num_nfcee,
93                          tNFA_EE_INFO *p_info)
94{
95    int   xx, ret = nfa_ee_cb.cur_ee;
96    tNFA_EE_ECB  *p_cb = nfa_ee_cb.ecb;
97    UINT8   max_ret;
98    UINT8   num_ret  = 0;
99
100    NFA_TRACE_DEBUG2 ("NFA_EeGetInfo em_state:%d cur_ee:%d", nfa_ee_cb.em_state, nfa_ee_cb.cur_ee);
101    /* validate parameters */
102    if (p_info == NULL || p_num_nfcee == NULL)
103    {
104        NFA_TRACE_ERROR0 ("NFA_EeGetInfo bad parameter");
105        return (NFA_STATUS_INVALID_PARAM);
106    }
107    max_ret         = *p_num_nfcee;
108    *p_num_nfcee = 0;
109    if (nfa_ee_cb.em_state == NFA_EE_EM_STATE_INIT)
110    {
111        NFA_TRACE_ERROR1 ("NFA_EeGetInfo bad em state: %d", nfa_ee_cb.em_state);
112        return (NFA_STATUS_FAILED);
113    }
114
115    /* compose output */
116    for (xx = 0; (xx < ret) && (num_ret < max_ret); xx++, p_cb++)
117    {
118        NFA_TRACE_DEBUG4 ("xx:%d max_ret:%d, num_ret:%d ee_status:0x%x", xx, max_ret, num_ret, p_cb->ee_status);
119        if ((p_cb->ee_status & NFA_EE_STATUS_INT_MASK) || (p_cb->ee_status == NFA_EE_STATUS_REMOVED))
120        {
121            continue;
122        }
123        p_info->ee_handle       = NFA_HANDLE_GROUP_EE | (tNFA_HANDLE)p_cb->nfcee_id;
124        p_info->ee_status       = p_cb->ee_status;
125        p_info->num_interface   = p_cb->num_interface;
126        p_info->num_tlvs        = p_cb->num_tlvs;
127        memcpy(p_info->ee_interface, p_cb->ee_interface, p_cb->num_interface);
128        memcpy(p_info->ee_tlv, p_cb->ee_tlv, p_cb->num_tlvs * sizeof(tNFA_EE_TLV));
129        p_info++;
130        num_ret++;
131    }
132    NFA_TRACE_DEBUG1 ("num_ret:%d", num_ret);
133    *p_num_nfcee = num_ret;
134    return (NFA_STATUS_OK);
135}
136
137/*******************************************************************************
138**
139** Function         NFA_EeRegister
140**
141** Description      This function registers a callback function to receive the
142**                  events from NFA-EE module.
143**
144** Returns          NFA_STATUS_OK if successfully initiated
145**                  NFA_STATUS_FAILED otherwise
146**                  NFA_STATUS_INVALID_PARAM If bad parameter
147**
148*******************************************************************************/
149tNFA_STATUS NFA_EeRegister(tNFA_EE_CBACK *p_cback)
150{
151    tNFA_EE_API_REGISTER *p_msg;
152    tNFA_STATUS status = NFA_STATUS_FAILED;
153
154    NFA_TRACE_API0 ("NFA_EeRegister()");
155
156    if (p_cback == NULL)
157    {
158        NFA_TRACE_ERROR0 ("NFA_EeRegister(): with NULL callback function");
159        status = NFA_STATUS_INVALID_PARAM;
160    }
161    else if ((p_msg = (tNFA_EE_API_REGISTER *) GKI_getbuf (sizeof(tNFA_EE_API_REGISTER))) != NULL)
162    {
163        p_msg->hdr.event    = NFA_EE_API_REGISTER_EVT;
164        p_msg->p_cback      = p_cback;
165
166        nfa_sys_sendmsg (p_msg);
167
168        status = NFA_STATUS_OK;
169    }
170
171    return status;
172}
173
174/*******************************************************************************
175**
176** Function         NFA_EeDeregister
177**
178** Description      This function de-registers the callback function
179**
180** Returns          NFA_STATUS_OK if successfully initiated
181**                  NFA_STATUS_FAILED otherwise
182**                  NFA_STATUS_INVALID_PARAM If bad parameter
183**
184*******************************************************************************/
185tNFA_STATUS NFA_EeDeregister(tNFA_EE_CBACK *p_cback)
186{
187    tNFA_EE_API_DEREGISTER *p_msg;
188    tNFA_STATUS status = NFA_STATUS_INVALID_PARAM;
189    int         index  = NFA_EE_MAX_CBACKS;
190    int         xx;
191
192    for (xx = 0; xx < NFA_EE_MAX_CBACKS; xx++)
193    {
194        if (nfa_ee_cb.p_ee_cback[xx] == p_cback)
195        {
196            index   = xx;
197            status  = NFA_STATUS_FAILED;
198            break;
199        }
200    }
201
202    NFA_TRACE_API2 ("NFA_EeDeregister() %d, status:%d", index, status);
203    if ((status != NFA_STATUS_INVALID_PARAM) &&
204        (p_msg = (tNFA_EE_API_DEREGISTER *) GKI_getbuf (sizeof(tNFA_EE_API_DEREGISTER))) != NULL)
205    {
206        p_msg->hdr.event = NFA_EE_API_DEREGISTER_EVT;
207        p_msg->index     = index;
208
209        nfa_sys_sendmsg (p_msg);
210
211        status = NFA_STATUS_OK;
212    }
213
214    return status;
215}
216
217/*******************************************************************************
218**
219** Function         NFA_EeModeSet
220**
221** Description      This function is called to activate (mode = NFA_EE_MD_ACTIVATE)
222**                  or deactivate (mode = NFA_EE_MD_DEACTIVATE) the NFCEE
223**                  identified by the given ee_handle. The result of this
224**                  operation is reported with the NFA_EE_MODE_SET_EVT.
225**
226** Returns          NFA_STATUS_OK if successfully initiated
227**                  NFA_STATUS_FAILED otherwise
228**                  NFA_STATUS_INVALID_PARAM If bad parameter
229**
230*******************************************************************************/
231tNFA_STATUS NFA_EeModeSet(tNFA_HANDLE    ee_handle,
232                          tNFA_EE_MD     mode)
233{
234    tNFA_EE_API_MODE_SET *p_msg;
235    tNFA_STATUS status = NFA_STATUS_FAILED;
236    tNFA_EE_ECB *p_cb, *p_found = NULL;
237    UINT32  xx;
238    UINT8   nfcee_id = (ee_handle & 0xFF);
239
240    p_cb = nfa_ee_cb.ecb;
241    for (xx = 0; xx < nfa_ee_cb.cur_ee; xx++, p_cb++)
242    {
243        if (nfcee_id == p_cb->nfcee_id)
244        {
245            p_found = p_cb;
246            break;
247        }
248    }
249    NFA_TRACE_API2 ("NFA_EeModeSet(): handle:<0x%x>, mode:0x%02X", ee_handle, mode);
250
251    if (p_found == NULL)
252    {
253        NFA_TRACE_ERROR1 ("NFA_EeModeSet() invalid NFCEE:0x%04x", ee_handle);
254        status = NFA_STATUS_INVALID_PARAM;
255    }
256    else if ((p_msg = (tNFA_EE_API_MODE_SET *) GKI_getbuf (sizeof(tNFA_EE_API_MODE_SET))) != NULL)
257    {
258        p_msg->hdr.event        = NFA_EE_API_MODE_SET_EVT;
259        p_msg->nfcee_id         = nfcee_id;
260        p_msg->mode             = mode;
261        p_msg->p_cb             = p_found;
262
263        nfa_sys_sendmsg (p_msg);
264
265        status = NFA_STATUS_OK;
266    }
267
268
269    return status;
270}
271
272
273/*******************************************************************************
274**
275** Function         NFA_EeSetDefaultTechRouting
276**
277** Description      This function is called to add, change or remove the
278**                  default routing based on RF technology in the listen mode
279**                  routing table for the given ee_handle. The status of this
280**                  operation is reported as the NFA_EE_SET_TECH_CFG_EVT.
281**
282** Note:            If RF discovery is started, NFA_StopRfDiscovery()/NFA_RF_DISCOVERY_STOPPED_EVT
283**                  should happen before calling this function
284**
285** Note:            NFA_EeUpdateNow() should be called after last NFA-EE function
286**                  to change the listen mode routing is called.
287**
288** Returns          NFA_STATUS_OK if successfully initiated
289**                  NFA_STATUS_FAILED otherwise
290**                  NFA_STATUS_INVALID_PARAM If bad parameter
291**
292*******************************************************************************/
293tNFA_STATUS NFA_EeSetDefaultTechRouting(tNFA_HANDLE          ee_handle,
294                                        tNFA_TECHNOLOGY_MASK technologies_switch_on,
295                                        tNFA_TECHNOLOGY_MASK technologies_switch_off,
296                                        tNFA_TECHNOLOGY_MASK technologies_battery_off)
297{
298    tNFA_EE_API_SET_TECH_CFG *p_msg;
299    tNFA_STATUS status = NFA_STATUS_FAILED;
300    UINT8       nfcee_id = (UINT8)(ee_handle & 0xFF);
301    tNFA_EE_ECB *p_cb;
302
303    NFA_TRACE_API4 ("NFA_EeSetDefaultTechRouting(): handle:<0x%x>technology_mask:<0x%x>/<0x%x>/<0x%x>",
304        ee_handle, technologies_switch_on, technologies_switch_off, technologies_battery_off);
305    p_cb = nfa_ee_find_ecb (nfcee_id);
306
307    if (p_cb == NULL)
308    {
309        NFA_TRACE_ERROR0 ("Bad ee_handle");
310        status = NFA_STATUS_INVALID_PARAM;
311    }
312    else if ((p_msg = (tNFA_EE_API_SET_TECH_CFG *) GKI_getbuf (sizeof(tNFA_EE_API_SET_TECH_CFG))) != NULL)
313    {
314        p_msg->hdr.event                = NFA_EE_API_SET_TECH_CFG_EVT;
315        p_msg->nfcee_id                 = nfcee_id;
316        p_msg->p_cb                     = p_cb;
317        p_msg->technologies_switch_on   = technologies_switch_on;
318        p_msg->technologies_switch_off  = technologies_switch_off;
319        p_msg->technologies_battery_off = technologies_battery_off;
320
321        nfa_sys_sendmsg (p_msg);
322
323        status = NFA_STATUS_OK;
324    }
325
326    return status;
327}
328
329/*******************************************************************************
330**
331** Function         NFA_EeSetDefaultProtoRouting
332**
333** Description      This function is called to add, change or remove the
334**                  default routing based on Protocol in the listen mode routing
335**                  table for the given ee_handle. The status of this
336**                  operation is reported as the NFA_EE_SET_PROTO_CFG_EVT.
337**
338** Note:            If RF discovery is started, NFA_StopRfDiscovery()/NFA_RF_DISCOVERY_STOPPED_EVT
339**                  should happen before calling this function
340**
341** Note:            NFA_EeUpdateNow() should be called after last NFA-EE function
342**                  to change the listen mode routing is called.
343**
344** Returns          NFA_STATUS_OK if successfully initiated
345**                  NFA_STATUS_FAILED otherwise
346**                  NFA_STATUS_INVALID_PARAM If bad parameter
347**
348*******************************************************************************/
349tNFA_STATUS NFA_EeSetDefaultProtoRouting(tNFA_HANDLE         ee_handle,
350                                         tNFA_PROTOCOL_MASK  protocols_switch_on,
351                                         tNFA_PROTOCOL_MASK  protocols_switch_off,
352                                         tNFA_PROTOCOL_MASK  protocols_battery_off)
353{
354    tNFA_EE_API_SET_PROTO_CFG *p_msg;
355    tNFA_STATUS status = NFA_STATUS_FAILED;
356    UINT8       nfcee_id = (UINT8)(ee_handle & 0xFF);
357    tNFA_EE_ECB *p_cb;
358
359    NFA_TRACE_API4 ("NFA_EeSetDefaultProtoRouting(): handle:<0x%x>protocol_mask:<0x%x>/<0x%x>/<0x%x>",
360        ee_handle, protocols_switch_on, protocols_switch_off, protocols_battery_off);
361    p_cb = nfa_ee_find_ecb (nfcee_id);
362
363    if (p_cb == NULL)
364    {
365        NFA_TRACE_ERROR0 ("Bad ee_handle");
366        status = NFA_STATUS_INVALID_PARAM;
367    }
368    else if ((p_msg = (tNFA_EE_API_SET_PROTO_CFG *) GKI_getbuf (sizeof(tNFA_EE_API_SET_PROTO_CFG))) != NULL)
369    {
370        p_msg->hdr.event                = NFA_EE_API_SET_PROTO_CFG_EVT;
371        p_msg->nfcee_id                 = nfcee_id;
372        p_msg->p_cb                     = p_cb;
373        p_msg->protocols_switch_on      = protocols_switch_on;
374        p_msg->protocols_switch_off     = protocols_switch_off;
375        p_msg->protocols_battery_off    = protocols_battery_off;
376
377        nfa_sys_sendmsg (p_msg);
378
379        status = NFA_STATUS_OK;
380    }
381
382    return status;
383}
384
385/*******************************************************************************
386**
387** Function         NFA_EeAddAidRouting
388**
389** Description      This function is called to add an AID entry in the
390**                  listen mode routing table in NFCC. The status of this
391**                  operation is reported as the NFA_EE_ADD_AID_EVT.
392**
393** Note:            If RF discovery is started, NFA_StopRfDiscovery()/NFA_RF_DISCOVERY_STOPPED_EVT
394**                  should happen before calling this function
395**
396** Note:            NFA_EeUpdateNow() should be called after last NFA-EE function
397**                  to change the listen mode routing is called.
398**
399** Returns          NFA_STATUS_OK if successfully initiated
400**                  NFA_STATUS_FAILED otherwise
401**                  NFA_STATUS_INVALID_PARAM If bad parameter
402**
403*******************************************************************************/
404tNFA_STATUS NFA_EeAddAidRouting(tNFA_HANDLE          ee_handle,
405                                UINT8                aid_len,
406                                UINT8               *p_aid,
407                                tNFA_EE_PWR_STATE    power_state)
408{
409    tNFA_EE_API_ADD_AID *p_msg;
410    tNFA_STATUS status = NFA_STATUS_FAILED;
411    UINT16 size = sizeof(tNFA_EE_API_ADD_AID) + aid_len;
412    UINT8       nfcee_id = (UINT8)(ee_handle & 0xFF);
413    tNFA_EE_ECB *p_cb;
414
415    NFA_TRACE_API1 ("NFA_EeAddAidRouting(): handle:<0x%x>", ee_handle);
416    p_cb = nfa_ee_find_ecb (nfcee_id);
417
418    /* validate parameters - make sure the AID is in valid length range */
419    if ((p_cb == NULL) || (aid_len == 0) || (p_aid == NULL) || (aid_len < NFA_MIN_AID_LEN) || (aid_len > NFA_MAX_AID_LEN))
420    {
421        NFA_TRACE_ERROR1 ("Bad ee_handle or AID (len=%d)", aid_len);
422        status = NFA_STATUS_INVALID_PARAM;
423    }
424    else if ((p_msg = (tNFA_EE_API_ADD_AID *) GKI_getbuf (size)) != NULL)
425    {
426        NFA_TRACE_DEBUG2 ("aid:<%02x%02x>", p_aid[0], p_aid[1]);
427        p_msg->hdr.event        = NFA_EE_API_ADD_AID_EVT;
428        p_msg->nfcee_id         = nfcee_id;
429        p_msg->p_cb             = p_cb;
430        p_msg->aid_len          = aid_len;
431        p_msg->power_state      = power_state;
432        p_msg->p_aid            = (UINT8 *)(p_msg + 1);
433        memcpy(p_msg->p_aid, p_aid, aid_len);
434
435        nfa_sys_sendmsg (p_msg);
436
437        status = NFA_STATUS_OK;
438    }
439
440    return status;
441}
442
443
444/*******************************************************************************
445**
446** Function         NFA_EeRemoveAidRouting
447**
448** Description      This function is called to remove the given AID entry from the
449**                  listen mode routing table. If the entry configures VS,
450**                  it is also removed. The status of this operation is reported
451**                  as the NFA_EE_REMOVE_AID_EVT.
452**
453** Note:            If RF discovery is started, NFA_StopRfDiscovery()/NFA_RF_DISCOVERY_STOPPED_EVT
454**                  should happen before calling this function
455**
456** Note:            NFA_EeUpdateNow() should be called after last NFA-EE function
457**                  to change the listen mode routing is called.
458**
459** Returns          NFA_STATUS_OK if successfully initiated
460**                  NFA_STATUS_FAILED otherwise
461**                  NFA_STATUS_INVALID_PARAM If bad parameter
462**
463*******************************************************************************/
464tNFA_STATUS NFA_EeRemoveAidRouting(UINT8     aid_len,
465                                   UINT8    *p_aid)
466{
467    tNFA_EE_API_REMOVE_AID *p_msg;
468    tNFA_STATUS status = NFA_STATUS_FAILED;
469    UINT16 size = sizeof(tNFA_EE_API_REMOVE_AID) + aid_len;
470
471    NFA_TRACE_API0 ("NFA_EeRemoveAidRouting()");
472    if ((aid_len == 0) || (p_aid == NULL) || (aid_len > NFA_MAX_AID_LEN))
473    {
474        NFA_TRACE_ERROR0 ("Bad AID");
475        status = NFA_STATUS_INVALID_PARAM;
476    }
477    else if ((p_msg = (tNFA_EE_API_REMOVE_AID *) GKI_getbuf (size)) != NULL)
478    {
479        p_msg->hdr.event        = NFA_EE_API_REMOVE_AID_EVT;
480        p_msg->aid_len          = aid_len;
481        p_msg->p_aid            = (UINT8 *)(p_msg + 1);
482        memcpy(p_msg->p_aid, p_aid, aid_len);
483
484        nfa_sys_sendmsg (p_msg);
485
486        status = NFA_STATUS_OK;
487    }
488
489    return status;
490}
491
492/*******************************************************************************
493**
494** Function         NFA_EeGetLmrtRemainingSize
495**
496** Description      This function is called to get remaining size of the
497**                  Listen Mode Routing Table.
498**                  The remaining size is reported in NFA_EE_REMAINING_SIZE_EVT
499**
500** Returns          NFA_STATUS_OK if successfully initiated
501**                  NFA_STATUS_FAILED otherwise
502**
503*******************************************************************************/
504tNFA_STATUS NFA_EeGetLmrtRemainingSize (void)
505{
506    tNFA_EE_API_LMRT_SIZE *p_msg;
507    tNFA_STATUS status = NFA_STATUS_FAILED;
508
509    NFA_TRACE_API0 ("NFA_EeGetLmrtRemainingSize()");
510    if ((p_msg = (tNFA_EE_API_LMRT_SIZE *) GKI_getbuf (sizeof(tNFA_EE_API_LMRT_SIZE))) != NULL)
511    {
512        p_msg->event    = NFA_EE_API_LMRT_SIZE_EVT;
513        nfa_sys_sendmsg (p_msg);
514        status = NFA_STATUS_OK;
515    }
516
517    return status;
518}
519
520/******************************************************************************
521**
522** Function         NFA_EeUpdateNow
523**
524** Description      This function is called to send the current listen mode
525**                  routing table and VS configuration to the NFCC (without waiting
526**                  for NFA_EE_ROUT_TIMEOUT_VAL).
527**
528**                  The status of this operation is
529**                  reported with the NFA_EE_UPDATED_EVT.
530**
531** Returns          NFA_STATUS_OK if successfully initiated
532**                  NFA_STATUS_SEMANTIC_ERROR is update is currently in progress
533**                  NFA_STATUS_FAILED otherwise
534**
535*******************************************************************************/
536tNFA_STATUS NFA_EeUpdateNow(void)
537{
538    BT_HDR *p_msg;
539    tNFA_STATUS status = NFA_STATUS_FAILED;
540
541    NFA_TRACE_API0 ("NFA_EeUpdateNow()");
542    if (nfa_ee_cb.ee_wait_evt & NFA_EE_WAIT_UPDATE_ALL)
543    {
544        NFA_TRACE_ERROR0 ("update in progress");
545        status = NFA_STATUS_SEMANTIC_ERROR;
546    }
547    else if ((p_msg = (BT_HDR *) GKI_getbuf (BT_HDR_SIZE)) != NULL)
548    {
549        p_msg->event    = NFA_EE_API_UPDATE_NOW_EVT;
550
551        nfa_sys_sendmsg (p_msg);
552
553        status = NFA_STATUS_OK;
554    }
555
556    return status;
557}
558
559
560/*******************************************************************************
561**
562** Function         NFA_EeConnect
563**
564** Description      Open connection to an NFCEE attached to the NFCC
565**
566**                  The status of this operation is
567**                  reported with the NFA_EE_CONNECT_EVT.
568**
569** Returns          NFA_STATUS_OK if successfully initiated
570**                  NFA_STATUS_FAILED otherwise
571**                  NFA_STATUS_INVALID_PARAM If bad parameter
572**
573*******************************************************************************/
574tNFA_STATUS NFA_EeConnect(tNFA_HANDLE    ee_handle,
575                          UINT8          ee_interface,
576                          tNFA_EE_CBACK *p_cback)
577{
578    tNFA_EE_API_CONNECT *p_msg;
579    tNFA_STATUS status = NFA_STATUS_FAILED;
580    UINT8       nfcee_id = (UINT8)(ee_handle & 0xFF);
581    tNFA_EE_ECB *p_cb;
582
583    NFA_TRACE_API2 ("NFA_EeConnect(): handle:<0x%x> ee_interface:0x%x", ee_handle, ee_interface);
584    p_cb = nfa_ee_find_ecb (nfcee_id);
585
586    if ((p_cb == NULL) || (p_cback == NULL))
587    {
588        NFA_TRACE_ERROR0 ("Bad ee_handle or NULL callback function");
589        status = NFA_STATUS_INVALID_PARAM;
590    }
591    else if ((p_msg = (tNFA_EE_API_CONNECT *) GKI_getbuf (sizeof(tNFA_EE_API_CONNECT))) != NULL)
592    {
593        p_msg->hdr.event        = NFA_EE_API_CONNECT_EVT;
594        p_msg->nfcee_id         = nfcee_id;
595        p_msg->p_cb             = p_cb;
596        p_msg->ee_interface     = ee_interface;
597        p_msg->p_cback          = p_cback;
598
599        nfa_sys_sendmsg (p_msg);
600
601        status = NFA_STATUS_OK;
602    }
603
604    return status;
605}
606
607/*******************************************************************************
608**
609** Function         NFA_EeSendData
610**
611** Description      Send data to the given NFCEE.
612**                  This function shall be called after NFA_EE_CONNECT_EVT is reported
613**                  and before NFA_EeDisconnect is called on the given ee_handle.
614**
615** Returns          NFA_STATUS_OK if successfully initiated
616**                  NFA_STATUS_FAILED otherwise
617**                  NFA_STATUS_INVALID_PARAM If bad parameter
618**
619*******************************************************************************/
620tNFA_STATUS NFA_EeSendData (tNFA_HANDLE  ee_handle,
621                            UINT16       data_len,
622                            UINT8       *p_data)
623{
624    tNFA_EE_API_SEND_DATA *p_msg;
625    tNFA_STATUS status = NFA_STATUS_FAILED;
626    UINT8       nfcee_id = (UINT8)(ee_handle & 0xFF);
627    tNFA_EE_ECB *p_cb;
628
629    NFA_TRACE_API1 ("NFA_EeSendData(): handle:<0x%x>", ee_handle);
630
631    p_cb = nfa_ee_find_ecb (nfcee_id);
632
633    if ((p_cb == NULL) || (p_cb->conn_st != NFA_EE_CONN_ST_CONN) || (p_data == NULL))
634    {
635        NFA_TRACE_ERROR0 ("Bad ee_handle or NULL data");
636        status = NFA_STATUS_INVALID_PARAM;
637    }
638    else if ((p_msg = (tNFA_EE_API_SEND_DATA *) GKI_getbuf ((UINT16)(sizeof(tNFA_EE_API_SEND_DATA) + data_len))) != NULL)
639    {
640        p_msg->hdr.event        = NFA_EE_API_SEND_DATA_EVT;
641        p_msg->nfcee_id         = nfcee_id;
642        p_msg->p_cb             = p_cb;
643        p_msg->data_len         = data_len;
644        p_msg->p_data           = (UINT8 *)(p_msg + 1);
645        memcpy(p_msg->p_data, p_data, data_len);
646
647        nfa_sys_sendmsg (p_msg);
648
649        status = NFA_STATUS_OK;
650    }
651
652    return status;
653}
654
655/*******************************************************************************
656**
657** Function         NFA_EeDisconnect
658**
659** Description      Disconnect (if a connection is currently open) from an
660**                  NFCEE interface. The result of this operation is reported
661**                  with the NFA_EE_DISCONNECT_EVT.
662**
663** Returns          NFA_STATUS_OK if successfully initiated
664**                  NFA_STATUS_FAILED otherwise
665**                  NFA_STATUS_INVALID_PARAM If bad parameter
666**
667*******************************************************************************/
668tNFA_STATUS NFA_EeDisconnect(tNFA_HANDLE ee_handle)
669{
670    tNFA_EE_API_DISCONNECT *p_msg;
671    tNFA_STATUS status = NFA_STATUS_FAILED;
672    UINT8       nfcee_id = (UINT8)(ee_handle & 0xFF);
673    tNFA_EE_ECB *p_cb;
674
675    NFA_TRACE_API1 ("NFA_EeDisconnect(): handle:<0x%x>", ee_handle);
676    p_cb = nfa_ee_find_ecb (nfcee_id);
677
678    if ((p_cb == NULL) || (p_cb->conn_st != NFA_EE_CONN_ST_CONN))
679    {
680        NFA_TRACE_ERROR0 ("NFA_EeDisconnect() Bad ee_handle");
681        status = NFA_STATUS_INVALID_PARAM;
682    }
683    else if ((p_msg = (tNFA_EE_API_DISCONNECT *) GKI_getbuf (sizeof(tNFA_EE_API_DISCONNECT))) != NULL)
684    {
685        p_msg->hdr.event        = NFA_EE_API_DISCONNECT_EVT;
686        p_msg->nfcee_id         = nfcee_id;
687        p_msg->p_cb             = p_cb;
688
689        nfa_sys_sendmsg (p_msg);
690
691        status = NFA_STATUS_OK;
692    }
693
694    return status;
695}
696