Home | History | Annotate | Download | only in nfc
      1 /******************************************************************************
      2  *
      3  *  Copyright (C) 2010-2013 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 functions that interface with the NFC NCI transport.
     23  *  On the receive side, it routes events to the appropriate handler
     24  *  (callback). On the transmit side, it manages the command transmission.
     25  *
     26  ******************************************************************************/
     27 #include <string.h>
     28 #include "gki.h"
     29 #include "nfc_target.h"
     30 #include "bt_types.h"
     31 #include "hcidefs.h"
     32 
     33 #if (NFC_INCLUDED == TRUE)
     34 #include "nfc_hal_api.h"
     35 #include "nfc_api.h"
     36 #include "nfc_int.h"
     37 #include "nci_hmsgs.h"
     38 #include "rw_int.h"
     39 #include "ce_int.h"
     40 
     41 
     42 #if (NFC_RW_ONLY == FALSE)
     43 #include "ce_api.h"
     44 #include "ce_int.h"
     45 #include "llcp_int.h"
     46 
     47 /* NFC mandates support for at least one logical connection;
     48  * Update max_conn to the NFCC capability on InitRsp */
     49 #define NFC_SET_MAX_CONN_DEFAULT()    {nfc_cb.max_conn = 1;}
     50 
     51 #else /* NFC_RW_ONLY */
     52 #define ce_init()
     53 #define llcp_init()
     54 
     55 #define NFC_SET_MAX_CONN_DEFAULT()
     56 
     57 #endif /* NFC_RW_ONLY */
     58 /****************************************************************************
     59 ** Declarations
     60 ****************************************************************************/
     61 #if NFC_DYNAMIC_MEMORY == FALSE
     62 tNFC_CB nfc_cb;
     63 #endif
     64 
     65 #if (NFC_RW_ONLY == FALSE)
     66 #define NFC_NUM_INTERFACE_MAP   2
     67 #else
     68 #define NFC_NUM_INTERFACE_MAP   1
     69 #endif
     70 
     71 static const tNCI_DISCOVER_MAPS nfc_interface_mapping[NFC_NUM_INTERFACE_MAP] =
     72 {
     73     /* Protocols that use Frame Interface do not need to be included in the interface mapping */
     74     {
     75         NCI_PROTOCOL_ISO_DEP,
     76         NCI_INTERFACE_MODE_POLL_N_LISTEN,
     77         NCI_INTERFACE_ISO_DEP
     78     }
     79 #if (NFC_RW_ONLY == FALSE)
     80     ,
     81     /* this can not be set here due to 2079xB0 NFCC issues */
     82     {
     83         NCI_PROTOCOL_NFC_DEP,
     84         NCI_INTERFACE_MODE_POLL_N_LISTEN,
     85         NCI_INTERFACE_NFC_DEP
     86     }
     87 #endif
     88 };
     89 
     90 
     91 #if (BT_TRACE_VERBOSE == TRUE)
     92 /*******************************************************************************
     93 **
     94 ** Function         nfc_state_name
     95 **
     96 ** Description      This function returns the state name.
     97 **
     98 ** NOTE             conditionally compiled to save memory.
     99 **
    100 ** Returns          pointer to the name
    101 **
    102 *******************************************************************************/
    103 static char *nfc_state_name (UINT8 state)
    104 {
    105     switch (state)
    106     {
    107     case NFC_STATE_NONE:
    108         return ("NONE");
    109     case NFC_STATE_W4_HAL_OPEN:
    110         return ("W4_HAL_OPEN");
    111     case NFC_STATE_CORE_INIT:
    112         return ("CORE_INIT");
    113     case NFC_STATE_W4_POST_INIT_CPLT:
    114         return ("W4_POST_INIT_CPLT");
    115     case NFC_STATE_IDLE:
    116         return ("IDLE");
    117     case NFC_STATE_OPEN:
    118         return ("OPEN");
    119     case NFC_STATE_CLOSING:
    120         return ("CLOSING");
    121     case NFC_STATE_W4_HAL_CLOSE:
    122         return ("W4_HAL_CLOSE");
    123     case NFC_STATE_NFCC_POWER_OFF_SLEEP:
    124         return ("NFCC_POWER_OFF_SLEEP");
    125     default:
    126         return ("???? UNKNOWN STATE");
    127     }
    128 }
    129 
    130 /*******************************************************************************
    131 **
    132 ** Function         nfc_hal_event_name
    133 **
    134 ** Description      This function returns the HAL event name.
    135 **
    136 ** NOTE             conditionally compiled to save memory.
    137 **
    138 ** Returns          pointer to the name
    139 **
    140 *******************************************************************************/
    141 static char *nfc_hal_event_name (UINT8 event)
    142 {
    143     switch (event)
    144     {
    145     case HAL_NFC_OPEN_CPLT_EVT:
    146         return ("HAL_NFC_OPEN_CPLT_EVT");
    147 
    148     case HAL_NFC_CLOSE_CPLT_EVT:
    149         return ("HAL_NFC_CLOSE_CPLT_EVT");
    150 
    151     case HAL_NFC_POST_INIT_CPLT_EVT:
    152         return ("HAL_NFC_POST_INIT_CPLT_EVT");
    153 
    154     case HAL_NFC_PRE_DISCOVER_CPLT_EVT:
    155         return ("HAL_NFC_PRE_DISCOVER_CPLT_EVT");
    156 
    157     case HAL_NFC_REQUEST_CONTROL_EVT:
    158         return ("HAL_NFC_REQUEST_CONTROL_EVT");
    159 
    160     case HAL_NFC_RELEASE_CONTROL_EVT:
    161         return ("HAL_NFC_RELEASE_CONTROL_EVT");
    162 
    163     case HAL_NFC_ERROR_EVT:
    164         return ("HAL_NFC_ERROR_EVT");
    165 
    166     default:
    167         return ("???? UNKNOWN EVENT");
    168     }
    169 }
    170 #endif /* BT_TRACE_VERBOSE == TRUE */
    171 
    172 /*******************************************************************************
    173 **
    174 ** Function         nfc_main_notify_enable_status
    175 **
    176 ** Description      Notify status of Enable/PowerOffSleep/PowerCycle
    177 **
    178 *******************************************************************************/
    179 static void nfc_main_notify_enable_status (tNFC_STATUS nfc_status)
    180 {
    181     tNFC_RESPONSE   evt_data;
    182 
    183     evt_data.status = nfc_status;
    184 
    185     if (nfc_cb.p_resp_cback)
    186     {
    187         /* if getting out of PowerOffSleep mode or restarting NFCC */
    188         if (nfc_cb.flags & (NFC_FL_RESTARTING|NFC_FL_POWER_CYCLE_NFCC))
    189         {
    190             nfc_cb.flags &= ~(NFC_FL_RESTARTING|NFC_FL_POWER_CYCLE_NFCC);
    191             if (nfc_status != NFC_STATUS_OK)
    192             {
    193                 nfc_cb.flags |= NFC_FL_POWER_OFF_SLEEP;
    194             }
    195             (*nfc_cb.p_resp_cback) (NFC_NFCC_RESTART_REVT, &evt_data);
    196         }
    197         else
    198         {
    199             (*nfc_cb.p_resp_cback) (NFC_ENABLE_REVT, &evt_data);
    200         }
    201     }
    202 }
    203 
    204 
    205 
    206 /*******************************************************************************
    207 **
    208 ** Function         nfc_enabled
    209 **
    210 ** Description      NFCC enabled, proceed with stack start up.
    211 **
    212 ** Returns          void
    213 **
    214 *******************************************************************************/
    215 void nfc_enabled (tNFC_STATUS nfc_status, BT_HDR *p_init_rsp_msg)
    216 {
    217     tNFC_RESPONSE evt_data;
    218     tNFC_CONN_CB  *p_cb = &nfc_cb.conn_cb[NFC_RF_CONN_ID];
    219     UINT8   *p;
    220     UINT8   num_interfaces = 0, xx;
    221     int     yy = 0;
    222 
    223     memset (&evt_data, 0, sizeof (tNFC_RESPONSE));
    224 
    225     if (nfc_status == NCI_STATUS_OK)
    226     {
    227         nfc_set_state (NFC_STATE_IDLE);
    228 
    229         p = (UINT8 *) (p_init_rsp_msg + 1) + p_init_rsp_msg->offset + NCI_MSG_HDR_SIZE + 1;
    230         /* we currently only support NCI of the same version.
    231         * We may need to change this, when we support multiple version of NFCC */
    232         evt_data.enable.nci_version = NCI_VERSION;
    233         STREAM_TO_UINT32 (evt_data.enable.nci_features, p);
    234         STREAM_TO_UINT8 (num_interfaces, p);
    235 
    236         evt_data.enable.nci_interfaces = 0;
    237         for (xx = 0; xx < num_interfaces; xx++)
    238         {
    239             if ((*p) <= NCI_INTERFACE_MAX)
    240                 evt_data.enable.nci_interfaces |= (1 << (*p));
    241             else if (((*p) > NCI_INTERFACE_FIRST_VS) && (yy < NFC_NFCC_MAX_NUM_VS_INTERFACE))
    242             {
    243                 /* save the VS RF interface in control block, if there's still room */
    244                 nfc_cb.vs_interface[yy++] = *p;
    245             }
    246             p++;
    247         }
    248         nfc_cb.nci_interfaces    = evt_data.enable.nci_interfaces;
    249         memcpy (evt_data.enable.vs_interface, nfc_cb.vs_interface, NFC_NFCC_MAX_NUM_VS_INTERFACE);
    250         evt_data.enable.max_conn = *p++;
    251         STREAM_TO_UINT16 (evt_data.enable.max_ce_table, p);
    252 #if (NFC_RW_ONLY == FALSE)
    253         nfc_cb.max_ce_table          = evt_data.enable.max_ce_table;
    254         nfc_cb.nci_features          = evt_data.enable.nci_features;
    255         nfc_cb.max_conn              = evt_data.enable.max_conn;
    256 #endif
    257         nfc_cb.nci_ctrl_size         = *p++; /* Max Control Packet Payload Length */
    258         p_cb->init_credits           = p_cb->num_buff = 0;
    259         STREAM_TO_UINT16 (evt_data.enable.max_param_size, p);
    260         nfc_set_conn_id (p_cb, NFC_RF_CONN_ID);
    261         evt_data.enable.manufacture_id   = *p++;
    262         STREAM_TO_ARRAY (evt_data.enable.nfcc_info, p, NFC_NFCC_INFO_LEN);
    263         NFC_DiscoveryMap (nfc_cb.num_disc_maps, (tNCI_DISCOVER_MAPS *) nfc_cb.p_disc_maps, NULL);
    264     }
    265     /* else not successful. the buffers will be freed in nfc_free_conn_cb () */
    266     else
    267     {
    268         if (nfc_cb.flags & NFC_FL_RESTARTING)
    269         {
    270             nfc_set_state (NFC_STATE_NFCC_POWER_OFF_SLEEP);
    271         }
    272         else
    273         {
    274             nfc_free_conn_cb (p_cb);
    275 
    276             /* if NFCC didn't respond to CORE_RESET or CORE_INIT */
    277             if (nfc_cb.nfc_state == NFC_STATE_CORE_INIT)
    278             {
    279                 /* report status after closing HAL */
    280                 nfc_cb.p_hal->close ();
    281                 return;
    282             }
    283             else
    284                 nfc_set_state (NFC_STATE_NONE);
    285         }
    286     }
    287 
    288     nfc_main_notify_enable_status (nfc_status);
    289 }
    290 
    291 /*******************************************************************************
    292 **
    293 ** Function         nfc_set_state
    294 **
    295 ** Description      Set the state of NFC stack
    296 **
    297 ** Returns          void
    298 **
    299 *******************************************************************************/
    300 void nfc_set_state (tNFC_STATE nfc_state)
    301 {
    302 #if (BT_TRACE_VERBOSE == TRUE)
    303     NFC_TRACE_DEBUG4 ("nfc_set_state %d (%s)->%d (%s)", nfc_cb.nfc_state, nfc_state_name (nfc_cb.nfc_state), nfc_state, nfc_state_name (nfc_state));
    304 #else
    305     NFC_TRACE_DEBUG2 ("nfc_set_state %d->%d", nfc_cb.nfc_state, nfc_state);
    306 #endif
    307     nfc_cb.nfc_state = nfc_state;
    308 }
    309 
    310 /*******************************************************************************
    311 **
    312 ** Function         nfc_gen_cleanup
    313 **
    314 ** Description      Clean up for both going into low power mode and disabling NFC
    315 **
    316 *******************************************************************************/
    317 void nfc_gen_cleanup (void)
    318 {
    319     nfc_cb.flags  &= ~NFC_FL_DEACTIVATING;
    320 
    321     /* the HAL pre-discover is still active - clear the pending flag/free the buffer */
    322     if (nfc_cb.flags & NFC_FL_DISCOVER_PENDING)
    323     {
    324         nfc_cb.flags &= ~NFC_FL_DISCOVER_PENDING;
    325         GKI_freebuf (nfc_cb.p_disc_pending);
    326         nfc_cb.p_disc_pending = NULL;
    327     }
    328 
    329     nfc_cb.flags &= ~(NFC_FL_CONTROL_REQUESTED|NFC_FL_CONTROL_GRANTED);
    330 
    331     nfc_stop_timer (&nfc_cb.deactivate_timer);
    332 
    333     /* Reset the connection control blocks */
    334     nfc_reset_all_conn_cbs ();
    335 
    336     if (nfc_cb.p_nci_init_rsp)
    337     {
    338         GKI_freebuf (nfc_cb.p_nci_init_rsp);
    339         nfc_cb.p_nci_init_rsp = NULL;
    340     }
    341 
    342     /* clear any pending CMD/RSP */
    343     nfc_main_flush_cmd_queue ();
    344 }
    345 
    346 /*******************************************************************************
    347 **
    348 ** Function         nfc_main_handle_hal_evt
    349 **
    350 ** Description      Handle BT_EVT_TO_NFC_MSGS
    351 **
    352 *******************************************************************************/
    353 void nfc_main_handle_hal_evt (tNFC_HAL_EVT_MSG *p_msg)
    354 {
    355     UINT8  *ps;
    356 
    357     NFC_TRACE_DEBUG1 ("nfc_main_handle_hal_evt(): HAL event=0x%x", p_msg->hal_evt);
    358 
    359     switch (p_msg->hal_evt)
    360     {
    361     case HAL_NFC_OPEN_CPLT_EVT: /* only for failure case */
    362         nfc_enabled (NFC_STATUS_FAILED, NULL);
    363         break;
    364 
    365     case HAL_NFC_CLOSE_CPLT_EVT:
    366         if (nfc_cb.p_resp_cback)
    367         {
    368             if (nfc_cb.nfc_state == NFC_STATE_W4_HAL_CLOSE)
    369             {
    370                 if (nfc_cb.flags & NFC_FL_POWER_OFF_SLEEP)
    371                 {
    372                     nfc_cb.flags &= ~NFC_FL_POWER_OFF_SLEEP;
    373                     nfc_set_state (NFC_STATE_NFCC_POWER_OFF_SLEEP);
    374                     (*nfc_cb.p_resp_cback) (NFC_NFCC_POWER_OFF_REVT, NULL);
    375                 }
    376                 else
    377                 {
    378                     nfc_set_state (NFC_STATE_NONE);
    379                     (*nfc_cb.p_resp_cback) (NFC_DISABLE_REVT, NULL);
    380                     nfc_cb.p_resp_cback = NULL;
    381                 }
    382             }
    383             else
    384             {
    385                 /* found error during initialization */
    386                 nfc_set_state (NFC_STATE_NONE);
    387                 nfc_main_notify_enable_status (NFC_STATUS_FAILED);
    388             }
    389         }
    390         break;
    391 
    392     case HAL_NFC_POST_INIT_CPLT_EVT:
    393         if (nfc_cb.p_nci_init_rsp)
    394         {
    395             /*
    396             ** if NFC_Disable() is called before receiving HAL_NFC_POST_INIT_CPLT_EVT,
    397             ** then wait for HAL_NFC_CLOSE_CPLT_EVT.
    398             */
    399             if (nfc_cb.nfc_state == NFC_STATE_W4_POST_INIT_CPLT)
    400             {
    401                 if (p_msg->status == HAL_NFC_STATUS_OK)
    402                 {
    403                     nfc_enabled (NCI_STATUS_OK, nfc_cb.p_nci_init_rsp);
    404                 }
    405                 else /* if post initailization failed */
    406                 {
    407                     nfc_enabled (NCI_STATUS_FAILED, NULL);
    408                 }
    409             }
    410 
    411             GKI_freebuf (nfc_cb.p_nci_init_rsp);
    412             nfc_cb.p_nci_init_rsp = NULL;
    413         }
    414         break;
    415 
    416     case HAL_NFC_PRE_DISCOVER_CPLT_EVT:
    417         /* restore the command window, no matter if the discover command is still pending */
    418         nfc_cb.nci_cmd_window = NCI_MAX_CMD_WINDOW;
    419         nfc_cb.flags         &= ~NFC_FL_CONTROL_GRANTED;
    420         if (nfc_cb.flags & NFC_FL_DISCOVER_PENDING)
    421         {
    422             /* issue the discovery command now, if it is still pending */
    423             nfc_cb.flags &= ~NFC_FL_DISCOVER_PENDING;
    424             ps            = (UINT8 *)nfc_cb.p_disc_pending;
    425             nci_snd_discover_cmd (*ps, (tNFC_DISCOVER_PARAMS *)(ps + 1));
    426             GKI_freebuf (nfc_cb.p_disc_pending);
    427             nfc_cb.p_disc_pending = NULL;
    428         }
    429         else
    430         {
    431             /* check if there's other pending commands */
    432             nfc_ncif_check_cmd_queue (NULL);
    433         }
    434 
    435         if (p_msg->status == HAL_NFC_STATUS_ERR_CMD_TIMEOUT)
    436             nfc_ncif_event_status (NFC_NFCC_TIMEOUT_REVT, NFC_STATUS_HW_TIMEOUT);
    437         break;
    438 
    439     case HAL_NFC_REQUEST_CONTROL_EVT:
    440         nfc_cb.flags    |= NFC_FL_CONTROL_REQUESTED;
    441         nfc_ncif_check_cmd_queue (NULL);
    442         break;
    443 
    444     case HAL_NFC_RELEASE_CONTROL_EVT:
    445         if (nfc_cb.flags & NFC_FL_CONTROL_GRANTED)
    446         {
    447             nfc_cb.flags &= ~NFC_FL_CONTROL_GRANTED;
    448             nfc_cb.nci_cmd_window = NCI_MAX_CMD_WINDOW;
    449             nfc_ncif_check_cmd_queue (NULL);
    450 
    451             if (p_msg->status == HAL_NFC_STATUS_ERR_CMD_TIMEOUT)
    452                 nfc_ncif_event_status (NFC_NFCC_TIMEOUT_REVT, NFC_STATUS_HW_TIMEOUT);
    453         }
    454         break;
    455 
    456     case HAL_NFC_ERROR_EVT:
    457         switch (p_msg->status)
    458         {
    459         case HAL_NFC_STATUS_ERR_TRANSPORT:
    460             /* Notify app of transport error */
    461             if (nfc_cb.p_resp_cback)
    462             {
    463                 (*nfc_cb.p_resp_cback) (NFC_NFCC_TRANSPORT_ERR_REVT, NULL);
    464 
    465                 /* if enabling NFC, notify upper layer of failure after closing HAL */
    466                 if (nfc_cb.nfc_state < NFC_STATE_IDLE)
    467                 {
    468                     nfc_enabled (NFC_STATUS_FAILED, NULL);
    469                 }
    470             }
    471             break;
    472 
    473         case HAL_NFC_STATUS_ERR_CMD_TIMEOUT:
    474             nfc_ncif_event_status (NFC_NFCC_TIMEOUT_REVT, NFC_STATUS_HW_TIMEOUT);
    475 
    476             /* if enabling NFC, notify upper layer of failure after closing HAL */
    477             if (nfc_cb.nfc_state < NFC_STATE_IDLE)
    478             {
    479                 nfc_enabled (NFC_STATUS_FAILED, NULL);
    480                 return;
    481             }
    482             break;
    483 
    484         default:
    485             break;
    486         }
    487         break;
    488 
    489     default:
    490         NFC_TRACE_ERROR1 ("nfc_main_handle_hal_evt (): unhandled event (0x%x).", p_msg->hal_evt);
    491         break;
    492     }
    493 }
    494 
    495 
    496 /*******************************************************************************
    497 **
    498 ** Function         nfc_main_flush_cmd_queue
    499 **
    500 ** Description      This function is called when setting power off sleep state.
    501 **
    502 ** Returns          void
    503 **
    504 *******************************************************************************/
    505 void nfc_main_flush_cmd_queue (void)
    506 {
    507     BT_HDR *p_msg;
    508 
    509     NFC_TRACE_DEBUG0 ("nfc_main_flush_cmd_queue ()");
    510 
    511     /* initialize command window */
    512     nfc_cb.nci_cmd_window = NCI_MAX_CMD_WINDOW;
    513 
    514     /* Stop command-pending timer */
    515     nfc_stop_timer(&nfc_cb.nci_wait_rsp_timer);
    516 
    517     /* dequeue and free buffer */
    518     while ((p_msg = (BT_HDR *)GKI_dequeue (&nfc_cb.nci_cmd_xmit_q)) != NULL)
    519     {
    520         GKI_freebuf (p_msg);
    521     }
    522 }
    523 
    524 /*******************************************************************************
    525 **
    526 ** Function         nfc_main_post_hal_evt
    527 **
    528 ** Description      This function posts HAL event to NFC_TASK
    529 **
    530 ** Returns          void
    531 **
    532 *******************************************************************************/
    533 void nfc_main_post_hal_evt (UINT8 hal_evt, tHAL_NFC_STATUS status)
    534 {
    535     tNFC_HAL_EVT_MSG *p_msg;
    536 
    537     if ((p_msg = (tNFC_HAL_EVT_MSG *) GKI_getbuf (sizeof(tNFC_HAL_EVT_MSG))) != NULL)
    538     {
    539         /* Initialize BT_HDR */
    540         p_msg->hdr.len    = 0;
    541         p_msg->hdr.event  = BT_EVT_TO_NFC_MSGS;
    542         p_msg->hdr.offset = 0;
    543         p_msg->hdr.layer_specific = 0;
    544         p_msg->hal_evt = hal_evt;
    545         p_msg->status  = status;
    546         GKI_send_msg (NFC_TASK, NFC_MBOX_ID, p_msg);
    547     }
    548     else
    549     {
    550         NFC_TRACE_ERROR0 ("nfc_main_post_hal_evt (): No buffer");
    551     }
    552 }
    553 
    554 
    555 /*******************************************************************************
    556 **
    557 ** Function         nfc_main_hal_cback
    558 **
    559 ** Description      HAL event handler
    560 **
    561 ** Returns          void
    562 **
    563 *******************************************************************************/
    564 static void nfc_main_hal_cback(UINT8 event, tHAL_NFC_STATUS status)
    565 {
    566 #if (BT_TRACE_VERBOSE == TRUE)
    567     NFC_TRACE_DEBUG3 ("nfc_main_hal_cback event: %s(0x%x), status=%d",
    568                        nfc_hal_event_name (event), event, status);
    569 #else
    570     NFC_TRACE_DEBUG2 ("nfc_main_hal_cback event: 0x%x, status=%d", event, status);
    571 #endif
    572 
    573     switch (event)
    574     {
    575     case HAL_NFC_OPEN_CPLT_EVT:
    576         /*
    577         ** if NFC_Disable() is called before receiving HAL_NFC_OPEN_CPLT_EVT,
    578         ** then wait for HAL_NFC_CLOSE_CPLT_EVT.
    579         */
    580         if (nfc_cb.nfc_state == NFC_STATE_W4_HAL_OPEN)
    581         {
    582             if (status == HAL_NFC_STATUS_OK)
    583             {
    584                 /* Notify NFC_TASK that NCI tranport is initialized */
    585                 GKI_send_event (NFC_TASK, NFC_TASK_EVT_TRANSPORT_READY);
    586             }
    587             else
    588             {
    589                 nfc_main_post_hal_evt (event, status);
    590             }
    591         }
    592         break;
    593 
    594     case HAL_NFC_CLOSE_CPLT_EVT:
    595     case HAL_NFC_POST_INIT_CPLT_EVT:
    596     case HAL_NFC_PRE_DISCOVER_CPLT_EVT:
    597     case HAL_NFC_REQUEST_CONTROL_EVT:
    598     case HAL_NFC_RELEASE_CONTROL_EVT:
    599     case HAL_NFC_ERROR_EVT:
    600         nfc_main_post_hal_evt (event, status);
    601         break;
    602 
    603     default:
    604         NFC_TRACE_DEBUG1 ("nfc_main_hal_cback unhandled event %x", event);
    605         break;
    606     }
    607 }
    608 
    609 /*******************************************************************************
    610 **
    611 ** Function         nfc_main_hal_data_cback
    612 **
    613 ** Description      HAL data event handler
    614 **
    615 ** Returns          void
    616 **
    617 *******************************************************************************/
    618 static void nfc_main_hal_data_cback(UINT16 data_len, UINT8   *p_data)
    619 {
    620     BT_HDR *p_msg;
    621 
    622     /* ignore all data while shutting down NFCC */
    623     if (nfc_cb.nfc_state == NFC_STATE_W4_HAL_CLOSE)
    624     {
    625         return;
    626     }
    627 
    628     if (p_data)
    629     {
    630         if ((p_msg = (BT_HDR *) GKI_getpoolbuf (NFC_NCI_POOL_ID)) != NULL)
    631         {
    632             /* Initialize BT_HDR */
    633             p_msg->len    = data_len;
    634             p_msg->event  = BT_EVT_TO_NFC_NCI;
    635             p_msg->offset = NFC_RECEIVE_MSGS_OFFSET;
    636 
    637             /* no need to check length, it always less than pool size */
    638             memcpy ((UINT8 *)(p_msg + 1) + p_msg->offset, p_data, p_msg->len);
    639 
    640             GKI_send_msg (NFC_TASK, NFC_MBOX_ID, p_msg);
    641         }
    642         else
    643         {
    644             NFC_TRACE_ERROR0 ("nfc_main_hal_data_cback (): No buffer");
    645         }
    646     }
    647 }
    648 
    649 /*******************************************************************************
    650 **
    651 ** Function         NFC_Enable
    652 **
    653 ** Description      This function enables NFC. Prior to calling NFC_Enable:
    654 **                  - the NFCC must be powered up, and ready to receive commands.
    655 **                  - GKI must be enabled
    656 **                  - NFC_TASK must be started
    657 **                  - NCIT_TASK must be started (if using dedicated NCI transport)
    658 **
    659 **                  This function opens the NCI transport (if applicable),
    660 **                  resets the NFC controller, and initializes the NFC subsystems.
    661 **
    662 **                  When the NFC startup procedure is completed, an
    663 **                  NFC_ENABLE_REVT is returned to the application using the
    664 **                  tNFC_RESPONSE_CBACK.
    665 **
    666 ** Returns          tNFC_STATUS
    667 **
    668 *******************************************************************************/
    669 tNFC_STATUS NFC_Enable (tNFC_RESPONSE_CBACK *p_cback)
    670 {
    671     NFC_TRACE_API0 ("NFC_Enable ()");
    672 
    673     /* Validate callback */
    674     if (!p_cback)
    675     {
    676         return (NFC_STATUS_INVALID_PARAM);
    677     }
    678     nfc_cb.p_resp_cback = p_cback;
    679 
    680     /* Open HAL transport. */
    681     nfc_set_state (NFC_STATE_W4_HAL_OPEN);
    682     nfc_cb.p_hal->open (nfc_main_hal_cback, nfc_main_hal_data_cback);
    683 
    684     return (NFC_STATUS_OK);
    685 }
    686 
    687 /*******************************************************************************
    688 **
    689 ** Function         NFC_Disable
    690 **
    691 ** Description      This function performs clean up routines for shutting down
    692 **                  NFC and closes the NCI transport (if using dedicated NCI
    693 **                  transport).
    694 **
    695 **                  When the NFC shutdown procedure is completed, an
    696 **                  NFC_DISABLED_REVT is returned to the application using the
    697 **                  tNFC_RESPONSE_CBACK.
    698 **
    699 ** Returns          nothing
    700 **
    701 *******************************************************************************/
    702 void NFC_Disable (void)
    703 {
    704     NFC_TRACE_API1 ("NFC_Disable (): nfc_state = %d", nfc_cb.nfc_state);
    705 
    706     if ((nfc_cb.nfc_state == NFC_STATE_NONE) || (nfc_cb.nfc_state == NFC_STATE_NFCC_POWER_OFF_SLEEP))
    707     {
    708         nfc_set_state (NFC_STATE_NONE);
    709         if (nfc_cb.p_resp_cback)
    710         {
    711             (*nfc_cb.p_resp_cback) (NFC_DISABLE_REVT, NULL);
    712             nfc_cb.p_resp_cback = NULL;
    713         }
    714         return;
    715     }
    716 
    717     /* Close transport and clean up */
    718     nfc_task_shutdown_nfcc ();
    719 }
    720 
    721 /*******************************************************************************
    722 **
    723 ** Function         NFC_Init
    724 **
    725 ** Description      This function initializes control block for NFC
    726 **
    727 ** Returns          nothing
    728 **
    729 *******************************************************************************/
    730 void NFC_Init (tHAL_NFC_ENTRY *p_hal_entry_tbl)
    731 {
    732     int xx;
    733 
    734     /* Clear nfc control block */
    735     memset (&nfc_cb, 0, sizeof (tNFC_CB));
    736 
    737     /* Reset the nfc control block */
    738     for (xx = 0; xx < NCI_MAX_CONN_CBS; xx++)
    739     {
    740         nfc_cb.conn_cb[xx].conn_id = NFC_ILLEGAL_CONN_ID;
    741     }
    742 
    743     /* NCI init */
    744     nfc_cb.p_hal            = p_hal_entry_tbl;
    745     nfc_cb.nfc_state        = NFC_STATE_NONE;
    746     nfc_cb.nci_cmd_window   = NCI_MAX_CMD_WINDOW;
    747     nfc_cb.nci_wait_rsp_tout= NFC_CMD_CMPL_TIMEOUT;
    748     nfc_cb.p_disc_maps      = nfc_interface_mapping;
    749     nfc_cb.num_disc_maps    = NFC_NUM_INTERFACE_MAP;
    750     nfc_cb.trace_level      = NFC_INITIAL_TRACE_LEVEL;
    751     nfc_cb.nci_ctrl_size    = NCI_CTRL_INIT_SIZE;
    752 
    753     rw_init ();
    754     ce_init ();
    755     llcp_init ();
    756     NFC_SET_MAX_CONN_DEFAULT ();
    757 }
    758 
    759 /*******************************************************************************
    760 **
    761 ** Function         NFC_GetLmrtSize
    762 **
    763 ** Description      Called by application wto query the Listen Mode Routing
    764 **                  Table size supported by NFCC
    765 **
    766 ** Returns          Listen Mode Routing Table size
    767 **
    768 *******************************************************************************/
    769 UINT16 NFC_GetLmrtSize (void)
    770 {
    771     UINT16 size = 0;
    772 #if (NFC_RW_ONLY == FALSE)
    773     size = nfc_cb.max_ce_table;
    774 #endif
    775     return size;
    776 }
    777 
    778 
    779 /*******************************************************************************
    780 **
    781 ** Function         NFC_SetConfig
    782 **
    783 ** Description      This function is called to send the configuration parameter
    784 **                  TLV to NFCC. The response from NFCC is reported by
    785 **                  tNFC_RESPONSE_CBACK as NFC_SET_CONFIG_REVT.
    786 **
    787 ** Parameters       tlv_size - the length of p_param_tlvs.
    788 **                  p_param_tlvs - the parameter ID/Len/Value list
    789 **
    790 ** Returns          tNFC_STATUS
    791 **
    792 *******************************************************************************/
    793 tNFC_STATUS NFC_SetConfig (UINT8     tlv_size,
    794                            UINT8    *p_param_tlvs)
    795 {
    796     return nci_snd_core_set_config (p_param_tlvs, tlv_size);
    797 }
    798 
    799 /*******************************************************************************
    800 **
    801 ** Function         NFC_GetConfig
    802 **
    803 ** Description      This function is called to retrieve the parameter TLV from NFCC.
    804 **                  The response from NFCC is reported by tNFC_RESPONSE_CBACK
    805 **                  as NFC_GET_CONFIG_REVT.
    806 **
    807 ** Parameters       num_ids - the number of parameter IDs
    808 **                  p_param_ids - the parameter ID list.
    809 **
    810 ** Returns          tNFC_STATUS
    811 **
    812 *******************************************************************************/
    813 tNFC_STATUS NFC_GetConfig (UINT8     num_ids,
    814                            UINT8    *p_param_ids)
    815 {
    816     return nci_snd_core_get_config (p_param_ids, num_ids);
    817 }
    818 
    819 /*******************************************************************************
    820 **
    821 ** Function         NFC_DiscoveryMap
    822 **
    823 ** Description      This function is called to set the discovery interface mapping.
    824 **                  The response from NFCC is reported by tNFC_DISCOVER_CBACK as.
    825 **                  NFC_MAP_DEVT.
    826 **
    827 ** Parameters       num - the number of items in p_params.
    828 **                  p_maps - the discovery interface mappings
    829 **                  p_cback - the discovery callback function
    830 **
    831 ** Returns          tNFC_STATUS
    832 **
    833 *******************************************************************************/
    834 tNFC_STATUS NFC_DiscoveryMap (UINT8 num, tNFC_DISCOVER_MAPS *p_maps,
    835                                         tNFC_DISCOVER_CBACK *p_cback)
    836 {
    837     UINT8   num_disc_maps = num;
    838     UINT8   xx, yy, num_intf, intf_mask;
    839     tNFC_DISCOVER_MAPS  max_maps[NFC_NFCC_MAX_NUM_VS_INTERFACE + NCI_INTERFACE_MAX];
    840     BOOLEAN is_supported;
    841 
    842     nfc_cb.p_discv_cback = p_cback;
    843     num_intf             = 0;
    844     NFC_TRACE_DEBUG1 ("nci_interfaces supported by NFCC: 0x%x", nfc_cb.nci_interfaces);
    845     for (xx = 0; xx < num_disc_maps; xx++)
    846     {
    847         is_supported = FALSE;
    848         if (p_maps[xx].intf_type > NCI_INTERFACE_MAX)
    849         {
    850             for (yy = 0; yy < NFC_NFCC_MAX_NUM_VS_INTERFACE; yy++)
    851             {
    852                 if (nfc_cb.vs_interface[yy] == p_maps[xx].intf_type)
    853                     is_supported    = TRUE;
    854             }
    855             NFC_TRACE_DEBUG3 ("[%d]: vs intf_type:0x%x is_supported:%d", xx, p_maps[xx].intf_type, is_supported);
    856         }
    857         else
    858         {
    859             intf_mask = (1 << (p_maps[xx].intf_type));
    860             if (intf_mask & nfc_cb.nci_interfaces)
    861             {
    862                 is_supported    = TRUE;
    863             }
    864             NFC_TRACE_DEBUG4 ("[%d]: intf_type:%d intf_mask: 0x%x is_supported:%d", xx, p_maps[xx].intf_type, intf_mask, is_supported);
    865         }
    866         if (is_supported)
    867             memcpy (&max_maps[num_intf++], &p_maps[xx], sizeof (tNFC_DISCOVER_MAPS));
    868         else
    869         {
    870             NFC_TRACE_WARNING1 ("NFC_DiscoveryMap interface=0x%x is not supported by NFCC", p_maps[xx].intf_type);
    871         }
    872     }
    873 
    874     return nci_snd_discover_map_cmd (num_intf, (tNCI_DISCOVER_MAPS *) max_maps);
    875 }
    876 
    877 /*******************************************************************************
    878 **
    879 ** Function         NFC_DiscoveryStart
    880 **
    881 ** Description      This function is called to start Polling and/or Listening.
    882 **                  The response from NFCC is reported by tNFC_DISCOVER_CBACK as.
    883 **                  NFC_START_DEVT. The notification from NFCC is reported by
    884 **                  tNFC_DISCOVER_CBACK as NFC_RESULT_DEVT.
    885 **
    886 ** Parameters       num_params - the number of items in p_params.
    887 **                  p_params - the discovery parameters
    888 **                  p_cback - the discovery callback function
    889 **
    890 ** Returns          tNFC_STATUS
    891 **
    892 *******************************************************************************/
    893 tNFC_STATUS NFC_DiscoveryStart (UINT8                 num_params,
    894                                 tNFC_DISCOVER_PARAMS *p_params,
    895                                 tNFC_DISCOVER_CBACK  *p_cback)
    896 {
    897     UINT8   *p;
    898     int     params_size;
    899     tNFC_STATUS status = NFC_STATUS_NO_BUFFERS;
    900 
    901     NFC_TRACE_API0 ("NFC_DiscoveryStart");
    902     if (nfc_cb.p_disc_pending)
    903     {
    904         NFC_TRACE_ERROR0 ("There's pending NFC_DiscoveryStart");
    905         status = NFC_STATUS_BUSY;
    906     }
    907     else
    908     {
    909         nfc_cb.p_discv_cback = p_cback;
    910         nfc_cb.flags        |= NFC_FL_DISCOVER_PENDING;
    911         nfc_cb.flags        |= NFC_FL_CONTROL_REQUESTED;
    912         params_size          = sizeof (tNFC_DISCOVER_PARAMS) * num_params;
    913         nfc_cb.p_disc_pending = GKI_getbuf ((UINT16)(BT_HDR_SIZE + 1 + params_size));
    914         if (nfc_cb.p_disc_pending)
    915         {
    916             p       = (UINT8 *)nfc_cb.p_disc_pending;
    917             *p++    = num_params;
    918             memcpy (p, p_params, params_size);
    919             status = NFC_STATUS_CMD_STARTED;
    920             nfc_ncif_check_cmd_queue (NULL);
    921         }
    922     }
    923 
    924     NFC_TRACE_API1 ("NFC_DiscoveryStart status: 0x%x", status);
    925     return status;
    926 }
    927 
    928 /*******************************************************************************
    929 **
    930 ** Function         NFC_DiscoverySelect
    931 **
    932 ** Description      If tNFC_DISCOVER_CBACK reports status=NFC_MULTIPLE_PROT,
    933 **                  the application needs to use this function to select the
    934 **                  the logical endpoint to continue. The response from NFCC is
    935 **                  reported by tNFC_DISCOVER_CBACK as NFC_SELECT_DEVT.
    936 **
    937 ** Parameters       rf_disc_id - The ID identifies the remote device.
    938 **                  protocol - the logical endpoint on the remote devide
    939 **                  rf_interface - the RF interface to communicate with NFCC
    940 **
    941 ** Returns          tNFC_STATUS
    942 **
    943 *******************************************************************************/
    944 tNFC_STATUS NFC_DiscoverySelect (UINT8    rf_disc_id,
    945                                  UINT8    protocol,
    946                                  UINT8    rf_interface)
    947 {
    948     return nci_snd_discover_select_cmd (rf_disc_id, protocol, rf_interface);
    949 }
    950 
    951 /*******************************************************************************
    952 **
    953 ** Function         NFC_ConnCreate
    954 **
    955 ** Description      This function is called to create a logical connection with
    956 **                  NFCC for data exchange.
    957 **
    958 ** Parameters       dest_type - the destination type
    959 **                  id   - the NFCEE ID or RF Discovery ID .
    960 **                  protocol   - the protocol.
    961 **                  p_cback - the connection callback function
    962 **
    963 ** Returns          tNFC_STATUS
    964 **
    965 *******************************************************************************/
    966 tNFC_STATUS NFC_ConnCreate (UINT8            dest_type,
    967                             UINT8            id,
    968                             UINT8            protocol,
    969                             tNFC_CONN_CBACK *p_cback)
    970 {
    971     tNFC_STATUS     status = NFC_STATUS_FAILED;
    972     tNFC_CONN_CB    *p_cb;
    973     UINT8           num_tlv=0, tlv_size=0;
    974     UINT8           param_tlvs[4], *pp;
    975 
    976     p_cb = nfc_alloc_conn_cb (p_cback);
    977     if (p_cb)
    978     {
    979         p_cb->id = id;
    980         pp = param_tlvs;
    981         if (dest_type == NCI_DEST_TYPE_NFCEE)
    982         {
    983             num_tlv = 1;
    984             UINT8_TO_STREAM (pp, NCI_CON_CREATE_TAG_NFCEE_VAL);
    985             UINT8_TO_STREAM (pp, 2);
    986             UINT8_TO_STREAM (pp, id);
    987             UINT8_TO_STREAM (pp, protocol);
    988             tlv_size = 4;
    989         }
    990         else if (dest_type == NCI_DEST_TYPE_REMOTE)
    991         {
    992             num_tlv = 1;
    993             UINT8_TO_STREAM (pp, NCI_CON_CREATE_TAG_RF_DISC_ID);
    994             UINT8_TO_STREAM (pp, 1);
    995             UINT8_TO_STREAM (pp, id);
    996             tlv_size = 3;
    997         }
    998         else if (dest_type == NCI_DEST_TYPE_NFCC)
    999         {
   1000             p_cb->id = NFC_TEST_ID;
   1001         }
   1002         /* Add handling of NCI_DEST_TYPE_REMOTE when more RF interface definitions are added */
   1003         p_cb->act_protocol = protocol;
   1004         p_cb->p_cback = p_cback;
   1005         status = nci_snd_core_conn_create (dest_type, num_tlv, tlv_size, param_tlvs);
   1006         if (status == NFC_STATUS_FAILED)
   1007             nfc_free_conn_cb (p_cb);
   1008     }
   1009     return status;
   1010 }
   1011 
   1012 /*******************************************************************************
   1013 **
   1014 ** Function         NFC_ConnClose
   1015 **
   1016 ** Description      This function is called to close a logical connection with
   1017 **                  NFCC.
   1018 **
   1019 ** Parameters       conn_id - the connection id.
   1020 **
   1021 ** Returns          tNFC_STATUS
   1022 **
   1023 *******************************************************************************/
   1024 tNFC_STATUS NFC_ConnClose (UINT8 conn_id)
   1025 {
   1026     tNFC_CONN_CB *p_cb = nfc_find_conn_cb_by_conn_id (conn_id);
   1027     tNFC_STATUS     status = NFC_STATUS_FAILED;
   1028 
   1029     if (p_cb)
   1030     {
   1031         status = nci_snd_core_conn_close (conn_id);
   1032     }
   1033     return status;
   1034 }
   1035 
   1036 /*******************************************************************************
   1037 **
   1038 ** Function         NFC_SetStaticRfCback
   1039 **
   1040 ** Description      This function is called to update the data callback function
   1041 **                  to receive the data for the given connection id.
   1042 **
   1043 ** Parameters       p_cback - the connection callback function
   1044 **
   1045 ** Returns          Nothing
   1046 **
   1047 *******************************************************************************/
   1048 void NFC_SetStaticRfCback (tNFC_CONN_CBACK    *p_cback)
   1049 {
   1050     tNFC_CONN_CB * p_cb = &nfc_cb.conn_cb[NFC_RF_CONN_ID];
   1051 
   1052     p_cb->p_cback = p_cback;
   1053     /* just in case DH has received NCI data before the data callback is set
   1054      * check if there's any data event to report on this connection id */
   1055     nfc_data_event (p_cb);
   1056 }
   1057 
   1058 /*******************************************************************************
   1059 **
   1060 ** Function         NFC_SendData
   1061 **
   1062 ** Description      This function is called to send the given data packet
   1063 **                  to the connection identified by the given connection id.
   1064 **
   1065 ** Parameters       conn_id - the connection id.
   1066 **                  p_data - the data packet.
   1067 **                  p_data->offset must be >= NCI_MSG_OFFSET_SIZE + NCI_DATA_HDR_SIZE
   1068 **                  The data payload starts at ((UINT8 *) (p_data + 1) + p_data->offset)
   1069 **
   1070 ** Returns          tNFC_STATUS
   1071 **
   1072 *******************************************************************************/
   1073 tNFC_STATUS NFC_SendData (UINT8       conn_id,
   1074                           BT_HDR     *p_data)
   1075 {
   1076     tNFC_STATUS     status = NFC_STATUS_FAILED;
   1077     tNFC_CONN_CB *p_cb = nfc_find_conn_cb_by_conn_id (conn_id);
   1078 
   1079     if (p_cb && p_data && p_data->offset >= NCI_MSG_OFFSET_SIZE + NCI_DATA_HDR_SIZE)
   1080     {
   1081         status = nfc_ncif_send_data (p_cb, p_data);
   1082     }
   1083 
   1084     if (status != NFC_STATUS_OK)
   1085         GKI_freebuf (p_data);
   1086 
   1087     return status;
   1088 }
   1089 
   1090 /*******************************************************************************
   1091 **
   1092 ** Function         NFC_FlushData
   1093 **
   1094 ** Description      This function is called to discard the tx data queue of
   1095 **                  the given connection id.
   1096 **
   1097 ** Parameters       conn_id - the connection id.
   1098 **
   1099 ** Returns          tNFC_STATUS
   1100 **
   1101 *******************************************************************************/
   1102 tNFC_STATUS NFC_FlushData (UINT8       conn_id)
   1103 {
   1104     tNFC_STATUS     status = NFC_STATUS_FAILED;
   1105     tNFC_CONN_CB    *p_cb = nfc_find_conn_cb_by_conn_id (conn_id);
   1106     void            *p_buf;
   1107 
   1108     if (p_cb)
   1109     {
   1110         status  = NFC_STATUS_OK;
   1111         while ((p_buf = GKI_dequeue (&p_cb->tx_q)) != NULL)
   1112             GKI_freebuf (p_buf);
   1113     }
   1114 
   1115     return status;
   1116 }
   1117 
   1118 /*******************************************************************************
   1119 **
   1120 ** Function         NFC_Deactivate
   1121 **
   1122 ** Description      This function is called to stop the discovery process or
   1123 **                  put the listen device in sleep mode or terminate the NFC link.
   1124 **
   1125 **                  The response from NFCC is reported by tNFC_DISCOVER_CBACK
   1126 **                  as NFC_DEACTIVATE_DEVT.
   1127 **
   1128 ** Parameters       deactivate_type - NFC_DEACTIVATE_TYPE_IDLE, to IDLE mode.
   1129 **                                    NFC_DEACTIVATE_TYPE_SLEEP to SLEEP mode.
   1130 **                                    NFC_DEACTIVATE_TYPE_SLEEP_AF to SLEEP_AF mode.
   1131 **
   1132 ** Returns          tNFC_STATUS
   1133 **
   1134 *******************************************************************************/
   1135 tNFC_STATUS NFC_Deactivate (tNFC_DEACT_TYPE deactivate_type)
   1136 {
   1137     tNFC_CONN_CB * p_cb = &nfc_cb.conn_cb[NFC_RF_CONN_ID];
   1138     tNFC_STATUS  status = NFC_STATUS_OK;
   1139 
   1140 #if (BT_TRACE_VERBOSE == TRUE)
   1141     NFC_TRACE_API3 ("NFC_Deactivate %d (%s) deactivate_type:%d", nfc_cb.nfc_state, nfc_state_name (nfc_cb.nfc_state), deactivate_type);
   1142 #else
   1143     NFC_TRACE_API2 ("NFC_Deactivate %d deactivate_type:%d", nfc_cb.nfc_state, deactivate_type);
   1144 #endif
   1145 
   1146     if (nfc_cb.flags & NFC_FL_DISCOVER_PENDING)
   1147     {
   1148         /* the HAL pre-discover is still active - clear the pending flag */
   1149         nfc_cb.flags &= ~NFC_FL_DISCOVER_PENDING;
   1150         GKI_freebuf (nfc_cb.p_disc_pending);
   1151         nfc_cb.p_disc_pending = NULL;
   1152         return NFC_STATUS_OK;
   1153     }
   1154 
   1155     if (nfc_cb.nfc_state == NFC_STATE_OPEN)
   1156     {
   1157         nfc_set_state (NFC_STATE_CLOSING);
   1158         NFC_TRACE_DEBUG3 ( "act_protocol %d credits:%d/%d", p_cb->act_protocol, p_cb->init_credits, p_cb->num_buff);
   1159         if ((p_cb->act_protocol == NCI_PROTOCOL_NFC_DEP) &&
   1160             (p_cb->init_credits != p_cb->num_buff))
   1161         {
   1162             nfc_cb.flags           |= NFC_FL_DEACTIVATING;
   1163             nfc_cb.deactivate_timer.param = (TIMER_PARAM_TYPE) deactivate_type;
   1164             nfc_start_timer (&nfc_cb.deactivate_timer , (UINT16) (NFC_TTYPE_WAIT_2_DEACTIVATE), NFC_DEACTIVATE_TIMEOUT);
   1165             return status;
   1166         }
   1167     }
   1168 
   1169     status = nci_snd_deactivate_cmd (deactivate_type);
   1170     return status;
   1171 }
   1172 
   1173 /*******************************************************************************
   1174 **
   1175 ** Function         NFC_UpdateRFCommParams
   1176 **
   1177 ** Description      This function is called to update RF Communication parameters
   1178 **                  once the Frame RF Interface has been activated.
   1179 **
   1180 **                  The response from NFCC is reported by tNFC_RESPONSE_CBACK
   1181 **                  as NFC_RF_COMM_PARAMS_UPDATE_REVT.
   1182 **
   1183 ** Returns          tNFC_STATUS
   1184 **
   1185 *******************************************************************************/
   1186 tNFC_STATUS NFC_UpdateRFCommParams (tNFC_RF_COMM_PARAMS *p_params)
   1187 {
   1188     UINT8 tlvs[12];
   1189     UINT8 *p = tlvs;
   1190     UINT8 data_exch_config;
   1191 
   1192     /* RF Technology and Mode */
   1193     if (p_params->include_rf_tech_mode)
   1194     {
   1195         UINT8_TO_STREAM (p, NCI_RF_PARAM_ID_TECH_N_MODE);
   1196         UINT8_TO_STREAM (p, 1);
   1197         UINT8_TO_STREAM (p, p_params->rf_tech_n_mode);
   1198     }
   1199 
   1200     /* Transmit Bit Rate */
   1201     if (p_params->include_tx_bit_rate)
   1202     {
   1203         UINT8_TO_STREAM (p, NCI_RF_PARAM_ID_TX_BIT_RATE);
   1204         UINT8_TO_STREAM (p, 1);
   1205         UINT8_TO_STREAM (p, p_params->tx_bit_rate);
   1206     }
   1207 
   1208     /* Receive Bit Rate */
   1209     if (p_params->include_tx_bit_rate)
   1210     {
   1211         UINT8_TO_STREAM (p, NCI_RF_PARAM_ID_RX_BIT_RATE);
   1212         UINT8_TO_STREAM (p, 1);
   1213         UINT8_TO_STREAM (p, p_params->rx_bit_rate);
   1214     }
   1215 
   1216     /* NFC-B Data Exchange Configuration */
   1217     if (p_params->include_nfc_b_config)
   1218     {
   1219         UINT8_TO_STREAM (p, NCI_RF_PARAM_ID_B_DATA_EX_PARAM);
   1220         UINT8_TO_STREAM (p, 1);
   1221 
   1222         data_exch_config =  (p_params->min_tr0 & 0x03) << 6;          /* b7b6 : Mininum TR0 */
   1223         data_exch_config |= (p_params->min_tr1 & 0x03) << 4;          /* b5b4 : Mininum TR1 */
   1224         data_exch_config |= (p_params->suppression_eos & 0x01) << 3;  /* b3 :   Suppression of EoS */
   1225         data_exch_config |= (p_params->suppression_sos & 0x01) << 2;  /* b2 :   Suppression of SoS */
   1226         data_exch_config |= (p_params->min_tr2 & 0x03);               /* b1b0 : Mininum TR2 */
   1227 
   1228         UINT8_TO_STREAM (p, data_exch_config);
   1229     }
   1230 
   1231     return nci_snd_parameter_update_cmd (tlvs, (UINT8) (p - tlvs));
   1232 }
   1233 
   1234 /*******************************************************************************
   1235 **
   1236 ** Function         NFC_SetPowerOffSleep
   1237 **
   1238 ** Description      This function closes/opens transport and turns off/on NFCC.
   1239 **
   1240 ** Returns          tNFC_STATUS
   1241 **
   1242 *******************************************************************************/
   1243 tNFC_STATUS NFC_SetPowerOffSleep (BOOLEAN enable)
   1244 {
   1245     NFC_TRACE_API1 ("NFC_SetPowerOffSleep () enable = %d", enable);
   1246 
   1247     if (  (enable == FALSE)
   1248         &&(nfc_cb.nfc_state == NFC_STATE_NFCC_POWER_OFF_SLEEP)  )
   1249     {
   1250         nfc_cb.flags |= NFC_FL_RESTARTING;
   1251 
   1252         /* open transport */
   1253         nfc_set_state (NFC_STATE_W4_HAL_OPEN);
   1254         nfc_cb.p_hal->open (nfc_main_hal_cback, nfc_main_hal_data_cback);
   1255 
   1256         return NFC_STATUS_OK;
   1257     }
   1258     else if (  (enable == TRUE)
   1259              &&(nfc_cb.nfc_state == NFC_STATE_IDLE)  )
   1260     {
   1261         /* close transport to turn off NFCC and clean up */
   1262         nfc_cb.flags |= NFC_FL_POWER_OFF_SLEEP;
   1263         nfc_task_shutdown_nfcc ();
   1264 
   1265         return NFC_STATUS_OK;
   1266     }
   1267 
   1268     NFC_TRACE_ERROR1 ("NFC_SetPowerOffSleep () invalid state = %d", nfc_cb.nfc_state);
   1269     return NFC_STATUS_FAILED;
   1270 }
   1271 
   1272 /*******************************************************************************
   1273 **
   1274 ** Function         NFC_PowerCycleNFCC
   1275 **
   1276 ** Description      This function turns off and then on NFCC.
   1277 **
   1278 ** Returns          tNFC_STATUS
   1279 **
   1280 *******************************************************************************/
   1281 tNFC_STATUS NFC_PowerCycleNFCC (void)
   1282 {
   1283     NFC_TRACE_API0 ("NFC_PowerCycleNFCC ()");
   1284 
   1285     if (nfc_cb.nfc_state == NFC_STATE_IDLE)
   1286     {
   1287         /* power cycle NFCC */
   1288         nfc_cb.flags |= NFC_FL_POWER_CYCLE_NFCC;
   1289         nfc_task_shutdown_nfcc ();
   1290 
   1291         return NFC_STATUS_OK;
   1292     }
   1293 
   1294     NFC_TRACE_ERROR1 ("NFC_PowerCycleNFCC () invalid state = %d", nfc_cb.nfc_state);
   1295     return NFC_STATUS_FAILED;
   1296 }
   1297 
   1298 
   1299 /*******************************************************************************
   1300 **
   1301 ** Function         NFC_SetTraceLevel
   1302 **
   1303 ** Description      This function sets the trace level for NFC.  If called with
   1304 **                  a value of 0xFF, it simply returns the current trace level.
   1305 **
   1306 ** Returns          The new or current trace level
   1307 **
   1308 *******************************************************************************/
   1309 UINT8 NFC_SetTraceLevel (UINT8 new_level)
   1310 {
   1311     NFC_TRACE_API1 ("NFC_SetTraceLevel () new_level = %d", new_level);
   1312 
   1313     if (new_level != 0xFF)
   1314         nfc_cb.trace_level = new_level;
   1315 
   1316     return (nfc_cb.trace_level);
   1317 }
   1318 
   1319 
   1320 #endif /* NFC_INCLUDED == TRUE */
   1321