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 | NFC_FL_HAL_REQUESTED);
    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_cb.flags    |= NFC_FL_HAL_REQUESTED;
    442         nfc_ncif_check_cmd_queue (NULL);
    443         break;
    444 
    445     case HAL_NFC_RELEASE_CONTROL_EVT:
    446         if (nfc_cb.flags & NFC_FL_CONTROL_GRANTED)
    447         {
    448             nfc_cb.flags &= ~NFC_FL_CONTROL_GRANTED;
    449             nfc_cb.nci_cmd_window = NCI_MAX_CMD_WINDOW;
    450             nfc_ncif_check_cmd_queue (NULL);
    451 
    452             if (p_msg->status == HAL_NFC_STATUS_ERR_CMD_TIMEOUT)
    453                 nfc_ncif_event_status (NFC_NFCC_TIMEOUT_REVT, NFC_STATUS_HW_TIMEOUT);
    454         }
    455         break;
    456 
    457     case HAL_NFC_ERROR_EVT:
    458         switch (p_msg->status)
    459         {
    460         case HAL_NFC_STATUS_ERR_TRANSPORT:
    461             /* Notify app of transport error */
    462             if (nfc_cb.p_resp_cback)
    463             {
    464                 (*nfc_cb.p_resp_cback) (NFC_NFCC_TRANSPORT_ERR_REVT, NULL);
    465 
    466                 /* if enabling NFC, notify upper layer of failure after closing HAL */
    467                 if (nfc_cb.nfc_state < NFC_STATE_IDLE)
    468                 {
    469                     nfc_enabled (NFC_STATUS_FAILED, NULL);
    470                 }
    471             }
    472             break;
    473 
    474         case HAL_NFC_STATUS_ERR_CMD_TIMEOUT:
    475             nfc_ncif_event_status (NFC_NFCC_TIMEOUT_REVT, NFC_STATUS_HW_TIMEOUT);
    476 
    477             /* if enabling NFC, notify upper layer of failure after closing HAL */
    478             if (nfc_cb.nfc_state < NFC_STATE_IDLE)
    479             {
    480                 nfc_enabled (NFC_STATUS_FAILED, NULL);
    481                 return;
    482             }
    483             break;
    484 
    485         default:
    486             break;
    487         }
    488         break;
    489 
    490     default:
    491         NFC_TRACE_ERROR1 ("nfc_main_handle_hal_evt (): unhandled event (0x%x).", p_msg->hal_evt);
    492         break;
    493     }
    494 }
    495 
    496 
    497 /*******************************************************************************
    498 **
    499 ** Function         nfc_main_flush_cmd_queue
    500 **
    501 ** Description      This function is called when setting power off sleep state.
    502 **
    503 ** Returns          void
    504 **
    505 *******************************************************************************/
    506 void nfc_main_flush_cmd_queue (void)
    507 {
    508     BT_HDR *p_msg;
    509 
    510     NFC_TRACE_DEBUG0 ("nfc_main_flush_cmd_queue ()");
    511 
    512     /* initialize command window */
    513     nfc_cb.nci_cmd_window = NCI_MAX_CMD_WINDOW;
    514 
    515     /* Stop command-pending timer */
    516     nfc_stop_timer(&nfc_cb.nci_wait_rsp_timer);
    517 
    518     /* dequeue and free buffer */
    519     while ((p_msg = (BT_HDR *)GKI_dequeue (&nfc_cb.nci_cmd_xmit_q)) != NULL)
    520     {
    521         GKI_freebuf (p_msg);
    522     }
    523 }
    524 
    525 /*******************************************************************************
    526 **
    527 ** Function         nfc_main_post_hal_evt
    528 **
    529 ** Description      This function posts HAL event to NFC_TASK
    530 **
    531 ** Returns          void
    532 **
    533 *******************************************************************************/
    534 void nfc_main_post_hal_evt (UINT8 hal_evt, tHAL_NFC_STATUS status)
    535 {
    536     tNFC_HAL_EVT_MSG *p_msg;
    537 
    538     if ((p_msg = (tNFC_HAL_EVT_MSG *) GKI_getbuf (sizeof(tNFC_HAL_EVT_MSG))) != NULL)
    539     {
    540         /* Initialize BT_HDR */
    541         p_msg->hdr.len    = 0;
    542         p_msg->hdr.event  = BT_EVT_TO_NFC_MSGS;
    543         p_msg->hdr.offset = 0;
    544         p_msg->hdr.layer_specific = 0;
    545         p_msg->hal_evt = hal_evt;
    546         p_msg->status  = status;
    547         GKI_send_msg (NFC_TASK, NFC_MBOX_ID, p_msg);
    548     }
    549     else
    550     {
    551         NFC_TRACE_ERROR0 ("nfc_main_post_hal_evt (): No buffer");
    552     }
    553 }
    554 
    555 
    556 /*******************************************************************************
    557 **
    558 ** Function         nfc_main_hal_cback
    559 **
    560 ** Description      HAL event handler
    561 **
    562 ** Returns          void
    563 **
    564 *******************************************************************************/
    565 static void nfc_main_hal_cback(UINT8 event, tHAL_NFC_STATUS status)
    566 {
    567 #if (BT_TRACE_VERBOSE == TRUE)
    568     NFC_TRACE_DEBUG3 ("nfc_main_hal_cback event: %s(0x%x), status=%d",
    569                        nfc_hal_event_name (event), event, status);
    570 #else
    571     NFC_TRACE_DEBUG2 ("nfc_main_hal_cback event: 0x%x, status=%d", event, status);
    572 #endif
    573 
    574     switch (event)
    575     {
    576     case HAL_NFC_OPEN_CPLT_EVT:
    577         /*
    578         ** if NFC_Disable() is called before receiving HAL_NFC_OPEN_CPLT_EVT,
    579         ** then wait for HAL_NFC_CLOSE_CPLT_EVT.
    580         */
    581         if (nfc_cb.nfc_state == NFC_STATE_W4_HAL_OPEN)
    582         {
    583             if (status == HAL_NFC_STATUS_OK)
    584             {
    585                 /* Notify NFC_TASK that NCI tranport is initialized */
    586                 GKI_send_event (NFC_TASK, NFC_TASK_EVT_TRANSPORT_READY);
    587             }
    588             else
    589             {
    590                 nfc_main_post_hal_evt (event, status);
    591             }
    592         }
    593         break;
    594 
    595     case HAL_NFC_CLOSE_CPLT_EVT:
    596     case HAL_NFC_POST_INIT_CPLT_EVT:
    597     case HAL_NFC_PRE_DISCOVER_CPLT_EVT:
    598     case HAL_NFC_REQUEST_CONTROL_EVT:
    599     case HAL_NFC_RELEASE_CONTROL_EVT:
    600     case HAL_NFC_ERROR_EVT:
    601         nfc_main_post_hal_evt (event, status);
    602         break;
    603 
    604     default:
    605         NFC_TRACE_DEBUG1 ("nfc_main_hal_cback unhandled event %x", event);
    606         break;
    607     }
    608 }
    609 
    610 /*******************************************************************************
    611 **
    612 ** Function         nfc_main_hal_data_cback
    613 **
    614 ** Description      HAL data event handler
    615 **
    616 ** Returns          void
    617 **
    618 *******************************************************************************/
    619 static void nfc_main_hal_data_cback(UINT16 data_len, UINT8   *p_data)
    620 {
    621     BT_HDR *p_msg;
    622 
    623     /* ignore all data while shutting down NFCC */
    624     if (nfc_cb.nfc_state == NFC_STATE_W4_HAL_CLOSE)
    625     {
    626         return;
    627     }
    628 
    629     if (p_data)
    630     {
    631         if ((p_msg = (BT_HDR *) GKI_getpoolbuf (NFC_NCI_POOL_ID)) != NULL)
    632         {
    633             /* Initialize BT_HDR */
    634             p_msg->len    = data_len;
    635             p_msg->event  = BT_EVT_TO_NFC_NCI;
    636             p_msg->offset = NFC_RECEIVE_MSGS_OFFSET;
    637 
    638             /* no need to check length, it always less than pool size */
    639             memcpy ((UINT8 *)(p_msg + 1) + p_msg->offset, p_data, p_msg->len);
    640 
    641             GKI_send_msg (NFC_TASK, NFC_MBOX_ID, p_msg);
    642         }
    643         else
    644         {
    645             NFC_TRACE_ERROR0 ("nfc_main_hal_data_cback (): No buffer");
    646         }
    647     }
    648 }
    649 
    650 /*******************************************************************************
    651 **
    652 ** Function         NFC_Enable
    653 **
    654 ** Description      This function enables NFC. Prior to calling NFC_Enable:
    655 **                  - the NFCC must be powered up, and ready to receive commands.
    656 **                  - GKI must be enabled
    657 **                  - NFC_TASK must be started
    658 **                  - NCIT_TASK must be started (if using dedicated NCI transport)
    659 **
    660 **                  This function opens the NCI transport (if applicable),
    661 **                  resets the NFC controller, and initializes the NFC subsystems.
    662 **
    663 **                  When the NFC startup procedure is completed, an
    664 **                  NFC_ENABLE_REVT is returned to the application using the
    665 **                  tNFC_RESPONSE_CBACK.
    666 **
    667 ** Returns          tNFC_STATUS
    668 **
    669 *******************************************************************************/
    670 tNFC_STATUS NFC_Enable (tNFC_RESPONSE_CBACK *p_cback)
    671 {
    672     NFC_TRACE_API0 ("NFC_Enable ()");
    673 
    674     /* Validate callback */
    675     if (!p_cback)
    676     {
    677         return (NFC_STATUS_INVALID_PARAM);
    678     }
    679     nfc_cb.p_resp_cback = p_cback;
    680 
    681     /* Open HAL transport. */
    682     nfc_set_state (NFC_STATE_W4_HAL_OPEN);
    683     nfc_cb.p_hal->open (nfc_main_hal_cback, nfc_main_hal_data_cback);
    684 
    685     return (NFC_STATUS_OK);
    686 }
    687 
    688 /*******************************************************************************
    689 **
    690 ** Function         NFC_Disable
    691 **
    692 ** Description      This function performs clean up routines for shutting down
    693 **                  NFC and closes the NCI transport (if using dedicated NCI
    694 **                  transport).
    695 **
    696 **                  When the NFC shutdown procedure is completed, an
    697 **                  NFC_DISABLED_REVT is returned to the application using the
    698 **                  tNFC_RESPONSE_CBACK.
    699 **
    700 ** Returns          nothing
    701 **
    702 *******************************************************************************/
    703 void NFC_Disable (void)
    704 {
    705     NFC_TRACE_API1 ("NFC_Disable (): nfc_state = %d", nfc_cb.nfc_state);
    706 
    707     if ((nfc_cb.nfc_state == NFC_STATE_NONE) || (nfc_cb.nfc_state == NFC_STATE_NFCC_POWER_OFF_SLEEP))
    708     {
    709         nfc_set_state (NFC_STATE_NONE);
    710         if (nfc_cb.p_resp_cback)
    711         {
    712             (*nfc_cb.p_resp_cback) (NFC_DISABLE_REVT, NULL);
    713             nfc_cb.p_resp_cback = NULL;
    714         }
    715         return;
    716     }
    717 
    718     /* Close transport and clean up */
    719     nfc_task_shutdown_nfcc ();
    720 }
    721 
    722 /*******************************************************************************
    723 **
    724 ** Function         NFC_Init
    725 **
    726 ** Description      This function initializes control block for NFC
    727 **
    728 ** Returns          nothing
    729 **
    730 *******************************************************************************/
    731 void NFC_Init (tHAL_NFC_ENTRY *p_hal_entry_tbl)
    732 {
    733     int xx;
    734 
    735     /* Clear nfc control block */
    736     memset (&nfc_cb, 0, sizeof (tNFC_CB));
    737 
    738     /* Reset the nfc control block */
    739     for (xx = 0; xx < NCI_MAX_CONN_CBS; xx++)
    740     {
    741         nfc_cb.conn_cb[xx].conn_id = NFC_ILLEGAL_CONN_ID;
    742     }
    743 
    744     /* NCI init */
    745     nfc_cb.p_hal            = p_hal_entry_tbl;
    746     nfc_cb.nfc_state        = NFC_STATE_NONE;
    747     nfc_cb.nci_cmd_window   = NCI_MAX_CMD_WINDOW;
    748     nfc_cb.nci_wait_rsp_tout= NFC_CMD_CMPL_TIMEOUT;
    749     nfc_cb.p_disc_maps      = nfc_interface_mapping;
    750     nfc_cb.num_disc_maps    = NFC_NUM_INTERFACE_MAP;
    751     nfc_cb.trace_level      = NFC_INITIAL_TRACE_LEVEL;
    752     nfc_cb.nci_ctrl_size    = NCI_CTRL_INIT_SIZE;
    753 
    754     rw_init ();
    755     ce_init ();
    756     llcp_init ();
    757     NFC_SET_MAX_CONN_DEFAULT ();
    758 }
    759 
    760 /*******************************************************************************
    761 **
    762 ** Function         NFC_GetLmrtSize
    763 **
    764 ** Description      Called by application wto query the Listen Mode Routing
    765 **                  Table size supported by NFCC
    766 **
    767 ** Returns          Listen Mode Routing Table size
    768 **
    769 *******************************************************************************/
    770 UINT16 NFC_GetLmrtSize (void)
    771 {
    772     UINT16 size = 0;
    773 #if (NFC_RW_ONLY == FALSE)
    774     size = nfc_cb.max_ce_table;
    775 #endif
    776     return size;
    777 }
    778 
    779 
    780 /*******************************************************************************
    781 **
    782 ** Function         NFC_SetConfig
    783 **
    784 ** Description      This function is called to send the configuration parameter
    785 **                  TLV to NFCC. The response from NFCC is reported by
    786 **                  tNFC_RESPONSE_CBACK as NFC_SET_CONFIG_REVT.
    787 **
    788 ** Parameters       tlv_size - the length of p_param_tlvs.
    789 **                  p_param_tlvs - the parameter ID/Len/Value list
    790 **
    791 ** Returns          tNFC_STATUS
    792 **
    793 *******************************************************************************/
    794 tNFC_STATUS NFC_SetConfig (UINT8     tlv_size,
    795                            UINT8    *p_param_tlvs)
    796 {
    797     return nci_snd_core_set_config (p_param_tlvs, tlv_size);
    798 }
    799 
    800 /*******************************************************************************
    801 **
    802 ** Function         NFC_GetConfig
    803 **
    804 ** Description      This function is called to retrieve the parameter TLV from NFCC.
    805 **                  The response from NFCC is reported by tNFC_RESPONSE_CBACK
    806 **                  as NFC_GET_CONFIG_REVT.
    807 **
    808 ** Parameters       num_ids - the number of parameter IDs
    809 **                  p_param_ids - the parameter ID list.
    810 **
    811 ** Returns          tNFC_STATUS
    812 **
    813 *******************************************************************************/
    814 tNFC_STATUS NFC_GetConfig (UINT8     num_ids,
    815                            UINT8    *p_param_ids)
    816 {
    817     return nci_snd_core_get_config (p_param_ids, num_ids);
    818 }
    819 
    820 /*******************************************************************************
    821 **
    822 ** Function         NFC_DiscoveryMap
    823 **
    824 ** Description      This function is called to set the discovery interface mapping.
    825 **                  The response from NFCC is reported by tNFC_DISCOVER_CBACK as.
    826 **                  NFC_MAP_DEVT.
    827 **
    828 ** Parameters       num - the number of items in p_params.
    829 **                  p_maps - the discovery interface mappings
    830 **                  p_cback - the discovery callback function
    831 **
    832 ** Returns          tNFC_STATUS
    833 **
    834 *******************************************************************************/
    835 tNFC_STATUS NFC_DiscoveryMap (UINT8 num, tNFC_DISCOVER_MAPS *p_maps,
    836                                         tNFC_DISCOVER_CBACK *p_cback)
    837 {
    838     UINT8   num_disc_maps = num;
    839     UINT8   xx, yy, num_intf, intf_mask;
    840     tNFC_DISCOVER_MAPS  max_maps[NFC_NFCC_MAX_NUM_VS_INTERFACE + NCI_INTERFACE_MAX];
    841     BOOLEAN is_supported;
    842 
    843     nfc_cb.p_discv_cback = p_cback;
    844     num_intf             = 0;
    845     NFC_TRACE_DEBUG1 ("nci_interfaces supported by NFCC: 0x%x", nfc_cb.nci_interfaces);
    846     for (xx = 0; xx < num_disc_maps; xx++)
    847     {
    848         is_supported = FALSE;
    849         if (p_maps[xx].intf_type > NCI_INTERFACE_MAX)
    850         {
    851             for (yy = 0; yy < NFC_NFCC_MAX_NUM_VS_INTERFACE; yy++)
    852             {
    853                 if (nfc_cb.vs_interface[yy] == p_maps[xx].intf_type)
    854                     is_supported    = TRUE;
    855             }
    856             NFC_TRACE_DEBUG3 ("[%d]: vs intf_type:0x%x is_supported:%d", xx, p_maps[xx].intf_type, is_supported);
    857         }
    858         else
    859         {
    860             intf_mask = (1 << (p_maps[xx].intf_type));
    861             if (intf_mask & nfc_cb.nci_interfaces)
    862             {
    863                 is_supported    = TRUE;
    864             }
    865             NFC_TRACE_DEBUG4 ("[%d]: intf_type:%d intf_mask: 0x%x is_supported:%d", xx, p_maps[xx].intf_type, intf_mask, is_supported);
    866         }
    867         if (is_supported)
    868             memcpy (&max_maps[num_intf++], &p_maps[xx], sizeof (tNFC_DISCOVER_MAPS));
    869         else
    870         {
    871             NFC_TRACE_WARNING1 ("NFC_DiscoveryMap interface=0x%x is not supported by NFCC", p_maps[xx].intf_type);
    872         }
    873     }
    874 
    875     return nci_snd_discover_map_cmd (num_intf, (tNCI_DISCOVER_MAPS *) max_maps);
    876 }
    877 
    878 /*******************************************************************************
    879 **
    880 ** Function         NFC_DiscoveryStart
    881 **
    882 ** Description      This function is called to start Polling and/or Listening.
    883 **                  The response from NFCC is reported by tNFC_DISCOVER_CBACK as.
    884 **                  NFC_START_DEVT. The notification from NFCC is reported by
    885 **                  tNFC_DISCOVER_CBACK as NFC_RESULT_DEVT.
    886 **
    887 ** Parameters       num_params - the number of items in p_params.
    888 **                  p_params - the discovery parameters
    889 **                  p_cback - the discovery callback function
    890 **
    891 ** Returns          tNFC_STATUS
    892 **
    893 *******************************************************************************/
    894 tNFC_STATUS NFC_DiscoveryStart (UINT8                 num_params,
    895                                 tNFC_DISCOVER_PARAMS *p_params,
    896                                 tNFC_DISCOVER_CBACK  *p_cback)
    897 {
    898     UINT8   *p;
    899     int     params_size;
    900     tNFC_STATUS status = NFC_STATUS_NO_BUFFERS;
    901 
    902     NFC_TRACE_API0 ("NFC_DiscoveryStart");
    903     if (nfc_cb.p_disc_pending)
    904     {
    905         NFC_TRACE_ERROR0 ("There's pending NFC_DiscoveryStart");
    906         status = NFC_STATUS_BUSY;
    907     }
    908     else
    909     {
    910         nfc_cb.p_discv_cback = p_cback;
    911         nfc_cb.flags        |= NFC_FL_DISCOVER_PENDING;
    912         nfc_cb.flags        |= NFC_FL_CONTROL_REQUESTED;
    913         params_size          = sizeof (tNFC_DISCOVER_PARAMS) * num_params;
    914         nfc_cb.p_disc_pending = GKI_getbuf ((UINT16)(BT_HDR_SIZE + 1 + params_size));
    915         if (nfc_cb.p_disc_pending)
    916         {
    917             p       = (UINT8 *)nfc_cb.p_disc_pending;
    918             *p++    = num_params;
    919             memcpy (p, p_params, params_size);
    920             status = NFC_STATUS_CMD_STARTED;
    921             nfc_ncif_check_cmd_queue (NULL);
    922         }
    923     }
    924 
    925     NFC_TRACE_API1 ("NFC_DiscoveryStart status: 0x%x", status);
    926     return status;
    927 }
    928 
    929 /*******************************************************************************
    930 **
    931 ** Function         NFC_DiscoverySelect
    932 **
    933 ** Description      If tNFC_DISCOVER_CBACK reports status=NFC_MULTIPLE_PROT,
    934 **                  the application needs to use this function to select the
    935 **                  the logical endpoint to continue. The response from NFCC is
    936 **                  reported by tNFC_DISCOVER_CBACK as NFC_SELECT_DEVT.
    937 **
    938 ** Parameters       rf_disc_id - The ID identifies the remote device.
    939 **                  protocol - the logical endpoint on the remote devide
    940 **                  rf_interface - the RF interface to communicate with NFCC
    941 **
    942 ** Returns          tNFC_STATUS
    943 **
    944 *******************************************************************************/
    945 tNFC_STATUS NFC_DiscoverySelect (UINT8    rf_disc_id,
    946                                  UINT8    protocol,
    947                                  UINT8    rf_interface)
    948 {
    949     return nci_snd_discover_select_cmd (rf_disc_id, protocol, rf_interface);
    950 }
    951 
    952 /*******************************************************************************
    953 **
    954 ** Function         NFC_ConnCreate
    955 **
    956 ** Description      This function is called to create a logical connection with
    957 **                  NFCC for data exchange.
    958 **
    959 ** Parameters       dest_type - the destination type
    960 **                  id   - the NFCEE ID or RF Discovery ID .
    961 **                  protocol   - the protocol.
    962 **                  p_cback - the connection callback function
    963 **
    964 ** Returns          tNFC_STATUS
    965 **
    966 *******************************************************************************/
    967 tNFC_STATUS NFC_ConnCreate (UINT8            dest_type,
    968                             UINT8            id,
    969                             UINT8            protocol,
    970                             tNFC_CONN_CBACK *p_cback)
    971 {
    972     tNFC_STATUS     status = NFC_STATUS_FAILED;
    973     tNFC_CONN_CB    *p_cb;
    974     UINT8           num_tlv=0, tlv_size=0;
    975     UINT8           param_tlvs[4], *pp;
    976 
    977     p_cb = nfc_alloc_conn_cb (p_cback);
    978     if (p_cb)
    979     {
    980         p_cb->id = id;
    981         pp = param_tlvs;
    982         if (dest_type == NCI_DEST_TYPE_NFCEE)
    983         {
    984             num_tlv = 1;
    985             UINT8_TO_STREAM (pp, NCI_CON_CREATE_TAG_NFCEE_VAL);
    986             UINT8_TO_STREAM (pp, 2);
    987             UINT8_TO_STREAM (pp, id);
    988             UINT8_TO_STREAM (pp, protocol);
    989             tlv_size = 4;
    990         }
    991         else if (dest_type == NCI_DEST_TYPE_REMOTE)
    992         {
    993             num_tlv = 1;
    994             UINT8_TO_STREAM (pp, NCI_CON_CREATE_TAG_RF_DISC_ID);
    995             UINT8_TO_STREAM (pp, 1);
    996             UINT8_TO_STREAM (pp, id);
    997             tlv_size = 3;
    998         }
    999         else if (dest_type == NCI_DEST_TYPE_NFCC)
   1000         {
   1001             p_cb->id = NFC_TEST_ID;
   1002         }
   1003         /* Add handling of NCI_DEST_TYPE_REMOTE when more RF interface definitions are added */
   1004         p_cb->act_protocol = protocol;
   1005         p_cb->p_cback = p_cback;
   1006         status = nci_snd_core_conn_create (dest_type, num_tlv, tlv_size, param_tlvs);
   1007         if (status == NFC_STATUS_FAILED)
   1008             nfc_free_conn_cb (p_cb);
   1009     }
   1010     return status;
   1011 }
   1012 
   1013 /*******************************************************************************
   1014 **
   1015 ** Function         NFC_ConnClose
   1016 **
   1017 ** Description      This function is called to close a logical connection with
   1018 **                  NFCC.
   1019 **
   1020 ** Parameters       conn_id - the connection id.
   1021 **
   1022 ** Returns          tNFC_STATUS
   1023 **
   1024 *******************************************************************************/
   1025 tNFC_STATUS NFC_ConnClose (UINT8 conn_id)
   1026 {
   1027     tNFC_CONN_CB *p_cb = nfc_find_conn_cb_by_conn_id (conn_id);
   1028     tNFC_STATUS     status = NFC_STATUS_FAILED;
   1029 
   1030     if (p_cb)
   1031     {
   1032         status = nci_snd_core_conn_close (conn_id);
   1033     }
   1034     return status;
   1035 }
   1036 
   1037 /*******************************************************************************
   1038 **
   1039 ** Function         NFC_SetStaticRfCback
   1040 **
   1041 ** Description      This function is called to update the data callback function
   1042 **                  to receive the data for the given connection id.
   1043 **
   1044 ** Parameters       p_cback - the connection callback function
   1045 **
   1046 ** Returns          Nothing
   1047 **
   1048 *******************************************************************************/
   1049 void NFC_SetStaticRfCback (tNFC_CONN_CBACK    *p_cback)
   1050 {
   1051     tNFC_CONN_CB * p_cb = &nfc_cb.conn_cb[NFC_RF_CONN_ID];
   1052 
   1053     p_cb->p_cback = p_cback;
   1054     /* just in case DH has received NCI data before the data callback is set
   1055      * check if there's any data event to report on this connection id */
   1056     nfc_data_event (p_cb);
   1057 }
   1058 
   1059 /*******************************************************************************
   1060 **
   1061 ** Function         NFC_SendData
   1062 **
   1063 ** Description      This function is called to send the given data packet
   1064 **                  to the connection identified by the given connection id.
   1065 **
   1066 ** Parameters       conn_id - the connection id.
   1067 **                  p_data - the data packet.
   1068 **                  p_data->offset must be >= NCI_MSG_OFFSET_SIZE + NCI_DATA_HDR_SIZE
   1069 **                  The data payload starts at ((UINT8 *) (p_data + 1) + p_data->offset)
   1070 **
   1071 ** Returns          tNFC_STATUS
   1072 **
   1073 *******************************************************************************/
   1074 tNFC_STATUS NFC_SendData (UINT8       conn_id,
   1075                           BT_HDR     *p_data)
   1076 {
   1077     tNFC_STATUS     status = NFC_STATUS_FAILED;
   1078     tNFC_CONN_CB *p_cb = nfc_find_conn_cb_by_conn_id (conn_id);
   1079 
   1080     if (p_cb && p_data && p_data->offset >= NCI_MSG_OFFSET_SIZE + NCI_DATA_HDR_SIZE)
   1081     {
   1082         status = nfc_ncif_send_data (p_cb, p_data);
   1083     }
   1084 
   1085     if (status != NFC_STATUS_OK)
   1086         GKI_freebuf (p_data);
   1087 
   1088     return status;
   1089 }
   1090 
   1091 /*******************************************************************************
   1092 **
   1093 ** Function         NFC_FlushData
   1094 **
   1095 ** Description      This function is called to discard the tx data queue of
   1096 **                  the given connection id.
   1097 **
   1098 ** Parameters       conn_id - the connection id.
   1099 **
   1100 ** Returns          tNFC_STATUS
   1101 **
   1102 *******************************************************************************/
   1103 tNFC_STATUS NFC_FlushData (UINT8       conn_id)
   1104 {
   1105     tNFC_STATUS     status = NFC_STATUS_FAILED;
   1106     tNFC_CONN_CB    *p_cb = nfc_find_conn_cb_by_conn_id (conn_id);
   1107     void            *p_buf;
   1108 
   1109     if (p_cb)
   1110     {
   1111         status  = NFC_STATUS_OK;
   1112         while ((p_buf = GKI_dequeue (&p_cb->tx_q)) != NULL)
   1113             GKI_freebuf (p_buf);
   1114     }
   1115 
   1116     return status;
   1117 }
   1118 
   1119 /*******************************************************************************
   1120 **
   1121 ** Function         NFC_Deactivate
   1122 **
   1123 ** Description      This function is called to stop the discovery process or
   1124 **                  put the listen device in sleep mode or terminate the NFC link.
   1125 **
   1126 **                  The response from NFCC is reported by tNFC_DISCOVER_CBACK
   1127 **                  as NFC_DEACTIVATE_DEVT.
   1128 **
   1129 ** Parameters       deactivate_type - NFC_DEACTIVATE_TYPE_IDLE, to IDLE mode.
   1130 **                                    NFC_DEACTIVATE_TYPE_SLEEP to SLEEP mode.
   1131 **                                    NFC_DEACTIVATE_TYPE_SLEEP_AF to SLEEP_AF mode.
   1132 **
   1133 ** Returns          tNFC_STATUS
   1134 **
   1135 *******************************************************************************/
   1136 tNFC_STATUS NFC_Deactivate (tNFC_DEACT_TYPE deactivate_type)
   1137 {
   1138     tNFC_CONN_CB * p_cb = &nfc_cb.conn_cb[NFC_RF_CONN_ID];
   1139     tNFC_STATUS  status = NFC_STATUS_OK;
   1140 
   1141 #if (BT_TRACE_VERBOSE == TRUE)
   1142     NFC_TRACE_API3 ("NFC_Deactivate %d (%s) deactivate_type:%d", nfc_cb.nfc_state, nfc_state_name (nfc_cb.nfc_state), deactivate_type);
   1143 #else
   1144     NFC_TRACE_API2 ("NFC_Deactivate %d deactivate_type:%d", nfc_cb.nfc_state, deactivate_type);
   1145 #endif
   1146 
   1147     if (nfc_cb.flags & NFC_FL_DISCOVER_PENDING)
   1148     {
   1149         /* the HAL pre-discover is still active - clear the pending flag */
   1150         nfc_cb.flags &= ~NFC_FL_DISCOVER_PENDING;
   1151         if (!(nfc_cb.flags & NFC_FL_HAL_REQUESTED))
   1152         {
   1153             /* if HAL did not request for control, clear this bit now */
   1154             nfc_cb.flags &= ~NFC_FL_CONTROL_REQUESTED;
   1155         }
   1156         GKI_freebuf (nfc_cb.p_disc_pending);
   1157         nfc_cb.p_disc_pending = NULL;
   1158         return NFC_STATUS_OK;
   1159     }
   1160 
   1161     if (nfc_cb.nfc_state == NFC_STATE_OPEN)
   1162     {
   1163         nfc_set_state (NFC_STATE_CLOSING);
   1164         NFC_TRACE_DEBUG3 ( "act_protocol %d credits:%d/%d", p_cb->act_protocol, p_cb->init_credits, p_cb->num_buff);
   1165         if ((p_cb->act_protocol == NCI_PROTOCOL_NFC_DEP) &&
   1166             (p_cb->init_credits != p_cb->num_buff))
   1167         {
   1168             nfc_cb.flags           |= NFC_FL_DEACTIVATING;
   1169             nfc_cb.deactivate_timer.param = (TIMER_PARAM_TYPE) deactivate_type;
   1170             nfc_start_timer (&nfc_cb.deactivate_timer , (UINT16) (NFC_TTYPE_WAIT_2_DEACTIVATE), NFC_DEACTIVATE_TIMEOUT);
   1171             return status;
   1172         }
   1173     }
   1174 
   1175     status = nci_snd_deactivate_cmd (deactivate_type);
   1176     return status;
   1177 }
   1178 
   1179 /*******************************************************************************
   1180 **
   1181 ** Function         NFC_UpdateRFCommParams
   1182 **
   1183 ** Description      This function is called to update RF Communication parameters
   1184 **                  once the Frame RF Interface has been activated.
   1185 **
   1186 **                  The response from NFCC is reported by tNFC_RESPONSE_CBACK
   1187 **                  as NFC_RF_COMM_PARAMS_UPDATE_REVT.
   1188 **
   1189 ** Returns          tNFC_STATUS
   1190 **
   1191 *******************************************************************************/
   1192 tNFC_STATUS NFC_UpdateRFCommParams (tNFC_RF_COMM_PARAMS *p_params)
   1193 {
   1194     UINT8 tlvs[12];
   1195     UINT8 *p = tlvs;
   1196     UINT8 data_exch_config;
   1197 
   1198     /* RF Technology and Mode */
   1199     if (p_params->include_rf_tech_mode)
   1200     {
   1201         UINT8_TO_STREAM (p, NCI_RF_PARAM_ID_TECH_N_MODE);
   1202         UINT8_TO_STREAM (p, 1);
   1203         UINT8_TO_STREAM (p, p_params->rf_tech_n_mode);
   1204     }
   1205 
   1206     /* Transmit Bit Rate */
   1207     if (p_params->include_tx_bit_rate)
   1208     {
   1209         UINT8_TO_STREAM (p, NCI_RF_PARAM_ID_TX_BIT_RATE);
   1210         UINT8_TO_STREAM (p, 1);
   1211         UINT8_TO_STREAM (p, p_params->tx_bit_rate);
   1212     }
   1213 
   1214     /* Receive Bit Rate */
   1215     if (p_params->include_tx_bit_rate)
   1216     {
   1217         UINT8_TO_STREAM (p, NCI_RF_PARAM_ID_RX_BIT_RATE);
   1218         UINT8_TO_STREAM (p, 1);
   1219         UINT8_TO_STREAM (p, p_params->rx_bit_rate);
   1220     }
   1221 
   1222     /* NFC-B Data Exchange Configuration */
   1223     if (p_params->include_nfc_b_config)
   1224     {
   1225         UINT8_TO_STREAM (p, NCI_RF_PARAM_ID_B_DATA_EX_PARAM);
   1226         UINT8_TO_STREAM (p, 1);
   1227 
   1228         data_exch_config =  (p_params->min_tr0 & 0x03) << 6;          /* b7b6 : Mininum TR0 */
   1229         data_exch_config |= (p_params->min_tr1 & 0x03) << 4;          /* b5b4 : Mininum TR1 */
   1230         data_exch_config |= (p_params->suppression_eos & 0x01) << 3;  /* b3 :   Suppression of EoS */
   1231         data_exch_config |= (p_params->suppression_sos & 0x01) << 2;  /* b2 :   Suppression of SoS */
   1232         data_exch_config |= (p_params->min_tr2 & 0x03);               /* b1b0 : Mininum TR2 */
   1233 
   1234         UINT8_TO_STREAM (p, data_exch_config);
   1235     }
   1236 
   1237     return nci_snd_parameter_update_cmd (tlvs, (UINT8) (p - tlvs));
   1238 }
   1239 
   1240 /*******************************************************************************
   1241 **
   1242 ** Function         NFC_SetPowerOffSleep
   1243 **
   1244 ** Description      This function closes/opens transport and turns off/on NFCC.
   1245 **
   1246 ** Returns          tNFC_STATUS
   1247 **
   1248 *******************************************************************************/
   1249 tNFC_STATUS NFC_SetPowerOffSleep (BOOLEAN enable)
   1250 {
   1251     NFC_TRACE_API1 ("NFC_SetPowerOffSleep () enable = %d", enable);
   1252 
   1253     if (  (enable == FALSE)
   1254         &&(nfc_cb.nfc_state == NFC_STATE_NFCC_POWER_OFF_SLEEP)  )
   1255     {
   1256         nfc_cb.flags |= NFC_FL_RESTARTING;
   1257 
   1258         /* open transport */
   1259         nfc_set_state (NFC_STATE_W4_HAL_OPEN);
   1260         nfc_cb.p_hal->open (nfc_main_hal_cback, nfc_main_hal_data_cback);
   1261 
   1262         return NFC_STATUS_OK;
   1263     }
   1264     else if (  (enable == TRUE)
   1265              &&(nfc_cb.nfc_state == NFC_STATE_IDLE)  )
   1266     {
   1267         /* close transport to turn off NFCC and clean up */
   1268         nfc_cb.flags |= NFC_FL_POWER_OFF_SLEEP;
   1269         nfc_task_shutdown_nfcc ();
   1270 
   1271         return NFC_STATUS_OK;
   1272     }
   1273 
   1274     NFC_TRACE_ERROR1 ("NFC_SetPowerOffSleep () invalid state = %d", nfc_cb.nfc_state);
   1275     return NFC_STATUS_FAILED;
   1276 }
   1277 
   1278 /*******************************************************************************
   1279 **
   1280 ** Function         NFC_PowerCycleNFCC
   1281 **
   1282 ** Description      This function turns off and then on NFCC.
   1283 **
   1284 ** Returns          tNFC_STATUS
   1285 **
   1286 *******************************************************************************/
   1287 tNFC_STATUS NFC_PowerCycleNFCC (void)
   1288 {
   1289     NFC_TRACE_API0 ("NFC_PowerCycleNFCC ()");
   1290 
   1291     if (nfc_cb.nfc_state == NFC_STATE_IDLE)
   1292     {
   1293         /* power cycle NFCC */
   1294         nfc_cb.flags |= NFC_FL_POWER_CYCLE_NFCC;
   1295         nfc_task_shutdown_nfcc ();
   1296 
   1297         return NFC_STATUS_OK;
   1298     }
   1299 
   1300     NFC_TRACE_ERROR1 ("NFC_PowerCycleNFCC () invalid state = %d", nfc_cb.nfc_state);
   1301     return NFC_STATUS_FAILED;
   1302 }
   1303 
   1304 
   1305 /*******************************************************************************
   1306 **
   1307 ** Function         NFC_SetTraceLevel
   1308 **
   1309 ** Description      This function sets the trace level for NFC.  If called with
   1310 **                  a value of 0xFF, it simply returns the current trace level.
   1311 **
   1312 ** Returns          The new or current trace level
   1313 **
   1314 *******************************************************************************/
   1315 UINT8 NFC_SetTraceLevel (UINT8 new_level)
   1316 {
   1317     NFC_TRACE_API1 ("NFC_SetTraceLevel () new_level = %d", new_level);
   1318 
   1319     if (new_level != 0xFF)
   1320         nfc_cb.trace_level = new_level;
   1321 
   1322     return (nfc_cb.trace_level);
   1323 }
   1324 
   1325 
   1326 #endif /* NFC_INCLUDED == TRUE */
   1327