Home | History | Annotate | Download | only in hal
      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  *  Functions for handling NFC HAL NCI Transport events
     23  *
     24  ******************************************************************************/
     25 #include <string.h>
     26 #include "nfc_hal_int.h"
     27 #include "nfc_hal_post_reset.h"
     28 #include "userial.h"
     29 #include "upio.h"
     30 
     31 /****************************************************************************
     32 ** Definitions
     33 ****************************************************************************/
     34 
     35 /* Default NFC HAL NCI port configuration  */
     36 NFC_HAL_TRANS_CFG_QUALIFIER tNFC_HAL_TRANS_CFG nfc_hal_trans_cfg =
     37 {
     38     NFC_HAL_SHARED_TRANSPORT_ENABLED,   /* bSharedTransport */
     39     USERIAL_BAUD_115200,                /* Baud rate */
     40     USERIAL_FC_HW                       /* Flow control */
     41 };
     42 
     43 /* Control block for NFC HAL NCI transport */
     44 #if NFC_DYNAMIC_MEMORY == FALSE
     45 tNFC_HAL_CB nfc_hal_cb;
     46 #endif
     47 
     48 extern tNFC_HAL_CFG *p_nfc_hal_cfg;
     49 /****************************************************************************
     50 ** Internal function prototypes
     51 ****************************************************************************/
     52 static void nfc_hal_main_userial_cback (tUSERIAL_PORT port, tUSERIAL_EVT evt, tUSERIAL_EVT_DATA *p_data);
     53 static void nfc_hal_main_handle_terminate (void);
     54 static void nfc_hal_main_timeout_cback (void *p_tle);
     55 
     56 #if (NFC_HAL_DEBUG == TRUE)
     57 const char * const nfc_hal_init_state_str[] =
     58 {
     59     "IDLE",             /* Initialization is done                */
     60     "W4_XTAL_SET",      /* Waiting for crystal setting rsp       */
     61     "POST_XTAL_SET",    /* Waiting for reset ntf after xtal set  */
     62     "W4_NFCC_ENABLE",   /* Waiting for reset ntf atter REG_PU up */
     63     "W4_BUILD_INFO",    /* Waiting for build info rsp            */
     64     "W4_PATCH_INFO",    /* Waiting for patch info rsp            */
     65     "W4_APP_COMPL",     /* Waiting for complete from application */
     66     "W4_POST_INIT",     /* Waiting for complete of post init     */
     67     "W4_CONTROL",       /* Waiting for control release           */
     68     "W4_PREDISC",       /* Waiting for complete of prediscover   */
     69     "CLOSING"           /* Shutting down                         */
     70 };
     71 #endif
     72 
     73 /*******************************************************************************
     74 **
     75 ** Function         nfc_hal_main_init
     76 **
     77 ** Description      This function initializes control block for NFC HAL
     78 **
     79 ** Returns          nothing
     80 **
     81 *******************************************************************************/
     82 void nfc_hal_main_init (void)
     83 {
     84     /* Clear control block */
     85     memset (&nfc_hal_cb, 0, sizeof (tNFC_HAL_CB));
     86 
     87     nfc_hal_cb.ncit_cb.nci_ctrl_size   = NFC_HAL_NCI_INIT_CTRL_PAYLOAD_SIZE;
     88     nfc_hal_cb.trace_level             = NFC_HAL_INITIAL_TRACE_LEVEL;
     89     nfc_hal_cb.timer.p_cback           = nfc_hal_main_timeout_cback;
     90 }
     91 
     92 /*******************************************************************************
     93 **
     94 ** Function         nfc_hal_main_open_transport
     95 **
     96 ** Description      Open transport and prepare for new incoming message;
     97 **
     98 ** Returns          nothing
     99 **
    100 *******************************************************************************/
    101 static void nfc_hal_main_open_transport (void)
    102 {
    103     tUSERIAL_OPEN_CFG open_cfg;
    104 
    105     /* Initialize control block */
    106     nfc_hal_cb.ncit_cb.rcv_state = NFC_HAL_RCV_IDLE_ST; /* to process packet type */
    107 
    108     if (nfc_hal_cb.ncit_cb.p_rcv_msg)
    109     {
    110         GKI_freebuf (nfc_hal_cb.ncit_cb.p_rcv_msg);
    111         nfc_hal_cb.ncit_cb.p_rcv_msg = NULL;
    112     }
    113 
    114     /* open transport */
    115     open_cfg.fmt    = (USERIAL_DATABITS_8 | USERIAL_PARITY_NONE | USERIAL_STOPBITS_1);
    116     open_cfg.baud   = nfc_hal_trans_cfg.userial_baud;
    117     open_cfg.fc     = nfc_hal_trans_cfg.userial_fc;
    118     open_cfg.buf    = USERIAL_BUF_BYTE;
    119 
    120     USERIAL_Open (USERIAL_NFC_PORT, &open_cfg, nfc_hal_main_userial_cback);
    121 
    122     {
    123         /* Wait for NFCC to enable - Core reset notification */
    124         NFC_HAL_SET_INIT_STATE (NFC_HAL_INIT_STATE_W4_NFCC_ENABLE);
    125 
    126         /* NFCC Enable timeout */
    127         nfc_hal_main_start_quick_timer (&nfc_hal_cb.timer, NFC_HAL_TTYPE_NFCC_ENABLE,
    128                                         ((p_nfc_hal_cfg->nfc_hal_nfcc_enable_timeout)*QUICK_TIMER_TICKS_PER_SEC)/1000);
    129     }
    130 }
    131 
    132 /*******************************************************************************
    133 **
    134 ** Function         nfa_hal_pre_discover_done_cback
    135 **
    136 ** Description      Pre-discovery CFG is sent.
    137 **
    138 ** Returns          nothing
    139 **
    140 *******************************************************************************/
    141 void nfa_hal_pre_discover_done_cback (tNFC_HAL_NCI_EVT event, UINT16 data_len, UINT8 *p_data)
    142 {
    143     NFC_HAL_SET_INIT_STATE(NFC_HAL_INIT_STATE_IDLE);
    144     nfc_hal_main_stop_quick_timer (&nfc_hal_cb.ncit_cb.nci_wait_rsp_timer);
    145     nfc_hal_cb.p_stack_cback (HAL_NFC_PRE_DISCOVER_CPLT_EVT, HAL_NFC_STATUS_OK);
    146 }
    147 
    148 /*******************************************************************************
    149 **
    150 ** Function         nfa_hal_send_pre_discover_cfg
    151 **
    152 ** Description      sending Pre-discovery CFG
    153 **
    154 ** Returns          nothing
    155 **
    156 *******************************************************************************/
    157 void nfa_hal_send_pre_discover_cfg (void)
    158 {
    159     if (nfc_hal_dm_set_config (p_nfc_hal_pre_discover_cfg [0],
    160                                &p_nfc_hal_pre_discover_cfg[1],
    161                                 nfa_hal_pre_discover_done_cback) != HAL_NFC_STATUS_OK)
    162     {
    163         nfa_hal_pre_discover_done_cback(0, 0, NULL);
    164     }
    165 }
    166 
    167 /*******************************************************************************
    168 **
    169 ** Function         nfc_hal_main_send_error
    170 **
    171 ** Description      send an Error event to NFC stack
    172 **
    173 ** Returns          nothing
    174 **
    175 *******************************************************************************/
    176 void nfc_hal_main_send_error (tHAL_NFC_STATUS status)
    177 {
    178     /* Notify stack */
    179     nfc_hal_cb.p_stack_cback(HAL_NFC_ERROR_EVT, status);
    180 }
    181 
    182 /*******************************************************************************
    183 **
    184 ** Function         nfc_hal_main_userial_cback
    185 **
    186 ** Description      USERIAL callback for NCI transport
    187 **
    188 ** Returns          nothing
    189 **
    190 *******************************************************************************/
    191 static void nfc_hal_main_userial_cback (tUSERIAL_PORT port, tUSERIAL_EVT evt, tUSERIAL_EVT_DATA *p_data)
    192 {
    193     if (evt == USERIAL_RX_READY_EVT)
    194     {
    195         /* Notify transport task of serial port event */
    196         GKI_send_event (NFC_HAL_TASK, NFC_HAL_TASK_EVT_DATA_RDY);
    197     }
    198     else if (evt == USERIAL_TX_DONE_EVT)
    199     {
    200         /* Serial driver has finshed sending data from USERIAL_Write */
    201         /* Currently, no action is needed for this event */
    202     }
    203     else if (evt == USERIAL_ERR_EVT)
    204     {
    205         HAL_TRACE_ERROR0 ("nfc_hal_main_userial_cback: USERIAL_ERR_EVT. Notifying NFC_TASK of transport error");
    206         if (nfc_hal_cb.ncit_cb.nci_wait_rsp != NFC_HAL_WAIT_RSP_NONE)
    207         {
    208             nfc_hal_main_stop_quick_timer (&nfc_hal_cb.ncit_cb.nci_wait_rsp_timer);
    209             nfc_hal_nci_cmd_timeout_cback ((void *)&nfc_hal_cb.ncit_cb.nci_wait_rsp_timer);
    210         }
    211         else
    212         {
    213             nfc_hal_main_send_error (HAL_NFC_STATUS_ERR_TRANSPORT);
    214         }
    215     }
    216     else if (evt == USERIAL_WAKEUP_EVT)
    217     {
    218         HAL_TRACE_DEBUG1 ("nfc_hal_main_userial_cback: USERIAL_WAKEUP_EVT: %d", p_data->sigs);
    219     }
    220     else
    221     {
    222         HAL_TRACE_DEBUG1 ("nfc_hal_main_userial_cback: unhandled userial evt: %i", evt);
    223     }
    224 }
    225 
    226 /*******************************************************************************
    227 **
    228 ** Function         nfc_hal_main_pre_init_done
    229 **
    230 ** Description      notify complete of pre-initialization
    231 **
    232 ** Returns          nothing
    233 **
    234 *******************************************************************************/
    235 void nfc_hal_main_pre_init_done (tHAL_NFC_STATUS status)
    236 {
    237     HAL_TRACE_DEBUG1 ("nfc_hal_main_pre_init_done () status = %d", status);
    238 
    239     if (status != HAL_NFC_STATUS_OK)
    240     {
    241         nfc_hal_main_handle_terminate ();
    242 
    243         /* Close uart */
    244         USERIAL_Close (USERIAL_NFC_PORT);
    245     }
    246 
    247     /* Notify NFC Task the status of initialization */
    248     nfc_hal_cb.p_stack_cback (HAL_NFC_OPEN_CPLT_EVT, status);
    249 }
    250 
    251 /*******************************************************************************
    252 **
    253 ** Function         nfc_hal_main_timeout_cback
    254 **
    255 ** Description      callback function for timeout
    256 **
    257 ** Returns          void
    258 **
    259 *******************************************************************************/
    260 static void nfc_hal_main_timeout_cback (void *p_tle)
    261 {
    262     TIMER_LIST_ENT  *p_tlent = (TIMER_LIST_ENT *) p_tle;
    263 
    264     HAL_TRACE_DEBUG0 ("nfc_hal_main_timeout_cback ()");
    265 
    266     switch (p_tlent->event)
    267     {
    268     case NFC_HAL_TTYPE_POWER_CYCLE:
    269         nfc_hal_main_open_transport ();
    270         break;
    271 
    272     case NFC_HAL_TTYPE_NFCC_ENABLE:
    273         /* NFCC should have enabled now, notify transport openned */
    274         nfc_hal_dm_pre_init_nfcc ();
    275         break;
    276 
    277     default:
    278         HAL_TRACE_DEBUG1 ("nfc_hal_main_timeout_cback: unhandled timer event (0x%04x)", p_tlent->event);
    279         break;
    280     }
    281 }
    282 
    283 /*******************************************************************************
    284 **
    285 ** Function         nfc_hal_main_handle_terminate
    286 **
    287 ** Description      Handle NFI transport shutdown
    288 **
    289 ** Returns          nothing
    290 **
    291 *******************************************************************************/
    292 static void nfc_hal_main_handle_terminate (void)
    293 {
    294     NFC_HDR *p_msg;
    295 
    296     /* dequeue and free buffer */
    297     if (nfc_hal_cb.ncit_cb.p_pend_cmd != NULL)
    298     {
    299         GKI_freebuf (nfc_hal_cb.ncit_cb.p_pend_cmd);
    300         nfc_hal_cb.ncit_cb.p_pend_cmd = NULL;
    301     }
    302 
    303     /* Free unsent nfc rx buffer */
    304     if (nfc_hal_cb.ncit_cb.p_rcv_msg)
    305     {
    306         GKI_freebuf (nfc_hal_cb.ncit_cb.p_rcv_msg);
    307         nfc_hal_cb.ncit_cb.p_rcv_msg  = NULL;
    308     }
    309 
    310     /* Free buffer for pending fragmented response/notification */
    311     if (nfc_hal_cb.ncit_cb.p_frag_msg)
    312     {
    313         GKI_freebuf (nfc_hal_cb.ncit_cb.p_frag_msg);
    314         nfc_hal_cb.ncit_cb.p_frag_msg = NULL;
    315     }
    316 
    317     /* Free buffers in the tx mbox */
    318     while ((p_msg = (NFC_HDR *) GKI_read_mbox (NFC_HAL_TASK_MBOX)) != NULL)
    319     {
    320         GKI_freebuf (p_msg);
    321     }
    322 
    323     /* notify closing transport */
    324     nfc_hal_dm_shutting_down_nfcc ();
    325 }
    326 
    327 /*******************************************************************************
    328 **
    329 ** Function         nfc_hal_main_start_quick_timer
    330 **
    331 ** Description      Start a timer for the specified amount of time.
    332 **                  NOTE: The timeout resolution depends on including modules.
    333 **                  QUICK_TIMER_TICKS_PER_SEC should be used to convert from
    334 **                  time to ticks.
    335 **
    336 **
    337 ** Returns          void
    338 **
    339 *******************************************************************************/
    340 void nfc_hal_main_start_quick_timer (TIMER_LIST_ENT *p_tle, UINT16 type, UINT32 timeout)
    341 {
    342     NFC_HDR *p_msg;
    343 
    344     /* if timer list is currently empty, start periodic GKI timer */
    345     if (nfc_hal_cb.quick_timer_queue.p_first == NULL)
    346     {
    347         /* if timer starts on other than NCIT task (script wrapper) */
    348         if(GKI_get_taskid () != NFC_HAL_TASK)
    349         {
    350             /* post event to start timer in NCIT task */
    351             if ((p_msg = (NFC_HDR *) GKI_getbuf (NFC_HDR_SIZE)) != NULL)
    352             {
    353                 p_msg->event = NFC_HAL_EVT_TO_START_QUICK_TIMER;
    354                 GKI_send_msg (NFC_HAL_TASK, NFC_HAL_TASK_MBOX, p_msg);
    355             }
    356         }
    357         else
    358         {
    359             GKI_start_timer (NFC_HAL_QUICK_TIMER_ID, ((GKI_SECS_TO_TICKS (1) / QUICK_TIMER_TICKS_PER_SEC)), TRUE);
    360         }
    361     }
    362 
    363     GKI_remove_from_timer_list (&nfc_hal_cb.quick_timer_queue, p_tle);
    364 
    365     p_tle->event = type;
    366     p_tle->ticks = timeout; /* Save the number of ticks for the timer */
    367 
    368     GKI_add_to_timer_list (&nfc_hal_cb.quick_timer_queue, p_tle);
    369 }
    370 
    371 /*******************************************************************************
    372 **
    373 ** Function         nfc_hal_main_stop_quick_timer
    374 **
    375 ** Description      Stop a timer.
    376 **
    377 ** Returns          void
    378 **
    379 *******************************************************************************/
    380 void nfc_hal_main_stop_quick_timer (TIMER_LIST_ENT *p_tle)
    381 {
    382     GKI_remove_from_timer_list (&nfc_hal_cb.quick_timer_queue, p_tle);
    383 
    384     /* if timer list is empty stop periodic GKI timer */
    385     if (nfc_hal_cb.quick_timer_queue.p_first == NULL)
    386     {
    387         GKI_stop_timer (NFC_HAL_QUICK_TIMER_ID);
    388     }
    389 }
    390 
    391 /*******************************************************************************
    392 **
    393 ** Function         nfc_hal_main_process_quick_timer_evt
    394 **
    395 ** Description      Process quick timer event
    396 **
    397 ** Returns          void
    398 **
    399 *******************************************************************************/
    400 static void nfc_hal_main_process_quick_timer_evt (void)
    401 {
    402     TIMER_LIST_ENT  *p_tle;
    403 
    404     GKI_update_timer_list (&nfc_hal_cb.quick_timer_queue, 1);
    405 
    406     while ((nfc_hal_cb.quick_timer_queue.p_first) && (!nfc_hal_cb.quick_timer_queue.p_first->ticks))
    407     {
    408         p_tle = nfc_hal_cb.quick_timer_queue.p_first;
    409         GKI_remove_from_timer_list (&nfc_hal_cb.quick_timer_queue, p_tle);
    410 
    411         if (p_tle->p_cback)
    412         {
    413             (*p_tle->p_cback) (p_tle);
    414         }
    415     }
    416 
    417     /* if timer list is empty stop periodic GKI timer */
    418     if (nfc_hal_cb.quick_timer_queue.p_first == NULL)
    419     {
    420         GKI_stop_timer (NFC_HAL_QUICK_TIMER_ID);
    421     }
    422 }
    423 
    424 /*******************************************************************************
    425 **
    426 ** Function         nfc_hal_send_nci_msg_to_nfc_task
    427 **
    428 ** Description      This function is called to send nci message to nfc task
    429 **
    430 ** Returns          void
    431 **
    432 *******************************************************************************/
    433 void nfc_hal_send_nci_msg_to_nfc_task (NFC_HDR * p_msg)
    434 {
    435 #ifdef NFC_HAL_SHARED_GKI
    436     /* Using shared NFC/HAL GKI resources - send message buffer directly to NFC_TASK for processing */
    437     p_msg->event = BT_EVT_TO_NFC_NCI;
    438     GKI_send_msg (NFC_TASK, NFC_MBOX_ID, p_msg);
    439 #else
    440     /* Send NCI message to the stack */
    441     nfc_hal_cb.p_data_cback (p_msg->len, (UINT8 *) ((p_msg + 1)
    442                                  + p_msg->offset));
    443     GKI_freebuf(p_msg);
    444 #endif
    445 }
    446 
    447 /*******************************************************************************
    448 **
    449 ** Function         nfc_hal_send_credit_ntf_for_cid
    450 **
    451 ** Description      This function is called to send credit ntf
    452 **                  for the specified connection id to nfc task
    453 **
    454 ** Returns          void
    455 **
    456 *******************************************************************************/
    457 static void nfc_hal_send_credit_ntf_for_cid (UINT8 cid)
    458 {
    459     NFC_HDR  *p_msg;
    460     UINT8    *p, *ps;
    461 
    462     /* Start of new message. Allocate a buffer for message */
    463     if ((p_msg = (NFC_HDR *) GKI_getpoolbuf (NFC_HAL_NCI_POOL_ID)) != NULL)
    464     {
    465         /* Initialize NFC_HDR */
    466         p_msg->len    = NCI_DATA_HDR_SIZE + 0x03;
    467         p_msg->event  = 0;
    468         p_msg->offset = 0;
    469         p_msg->layer_specific = 0;
    470 
    471         p = (UINT8 *) (p_msg + 1) + p_msg->offset;
    472         ps = p;
    473         NCI_MSG_BLD_HDR0(p, NCI_MT_NTF, NCI_GID_CORE);
    474         NCI_MSG_BLD_HDR1(p, NCI_MSG_CORE_CONN_CREDITS);
    475         UINT8_TO_STREAM (p, 0x03);
    476 
    477         /* Number of credit entries */
    478         *p++ = 0x01;
    479         /* Connection id of the credit ntf */
    480         *p++ = cid;
    481         /* Number of credits */
    482         *p = 0x01;
    483 #ifdef DISP_NCI
    484         DISP_NCI (ps, (UINT16) p_msg->len, TRUE);
    485 #endif
    486         nfc_hal_send_nci_msg_to_nfc_task (p_msg);
    487     }
    488     else
    489     {
    490         HAL_TRACE_ERROR0 ("Unable to allocate buffer for Sending credit ntf to stack");
    491     }
    492 }
    493 
    494 /*******************************************************************************
    495 **
    496 ** Function         nfc_hal_main_send_message
    497 **
    498 ** Description      This function is calledto send an NCI message.
    499 **
    500 ** Returns          void
    501 **
    502 *******************************************************************************/
    503 static void nfc_hal_main_send_message (NFC_HDR *p_msg)
    504 {
    505     UINT8   *ps, *pp, cid, pbf;
    506     UINT16  len = p_msg->len;
    507     UINT16  data_len;
    508 #ifdef DISP_NCI
    509     UINT8   delta;
    510 #endif
    511 
    512     HAL_TRACE_DEBUG1 ("nfc_hal_main_send_message() ls:0x%x", p_msg->layer_specific);
    513     if (  (p_msg->layer_specific == NFC_HAL_WAIT_RSP_CMD)
    514         ||(p_msg->layer_specific == NFC_HAL_WAIT_RSP_VSC)  )
    515     {
    516         nfc_hal_nci_send_cmd (p_msg);
    517     }
    518     else
    519     {
    520         /* NFC task has fragmented the data packet to the appropriate size
    521          * and data credit is available; just send it */
    522 
    523         /* add NCI packet type in front of message */
    524         nfc_hal_nci_add_nfc_pkt_type (p_msg);
    525 
    526         /* send this packet to transport */
    527         ps = (UINT8 *) (p_msg + 1) + p_msg->offset;
    528         pp = ps + 1;
    529 #ifdef DISP_NCI
    530         delta = p_msg->len - len;
    531         DISP_NCI (ps + delta, (UINT16) (p_msg->len - delta), FALSE);
    532 #endif
    533         if (nfc_hal_cb.hci_cb.hcp_conn_id)
    534         {
    535             NCI_DATA_PRS_HDR(pp, pbf, cid, data_len);
    536             if (cid == nfc_hal_cb.hci_cb.hcp_conn_id)
    537             {
    538                 if (nfc_hal_hci_handle_hcp_pkt_to_hc (pp))
    539                 {
    540                     HAL_TRACE_DEBUG0 ("nfc_hal_main_send_message() - Drop rsp to Fake cmd, Fake credit ntf");
    541                     GKI_freebuf (p_msg);
    542                     nfc_hal_send_credit_ntf_for_cid (cid);
    543                     return;
    544                 }
    545             }
    546 
    547         }
    548         /* check low power mode state */
    549         if (nfc_hal_dm_power_mode_execute (NFC_HAL_LP_TX_DATA_EVT))
    550         {
    551             USERIAL_Write (USERIAL_NFC_PORT, ps, p_msg->len);
    552         }
    553         else
    554         {
    555             HAL_TRACE_ERROR0 ("nfc_hal_main_send_message(): drop data in low power mode");
    556         }
    557         GKI_freebuf (p_msg);
    558     }
    559 }
    560 
    561 /*******************************************************************************
    562 **
    563 ** Function         nfc_hal_main_task
    564 **
    565 ** Description      NFC HAL NCI transport event processing task
    566 **
    567 ** Returns          0
    568 **
    569 *******************************************************************************/
    570 UINT32 nfc_hal_main_task (UINT32 param)
    571 {
    572     UINT16   event;
    573     UINT8    byte;
    574     UINT8    num_interfaces;
    575     UINT8    *p;
    576     NFC_HDR  *p_msg;
    577     BOOLEAN  free_msg;
    578 
    579     HAL_TRACE_DEBUG0 ("NFC_HAL_TASK started");
    580 
    581     /* Main loop */
    582     while (TRUE)
    583     {
    584         event = GKI_wait (0xFFFF, 0);
    585 
    586         /* Handle NFC_HAL_TASK_EVT_INITIALIZE (for initializing NCI transport) */
    587         if (event & NFC_HAL_TASK_EVT_INITIALIZE)
    588         {
    589             HAL_TRACE_DEBUG0 ("NFC_HAL_TASK got NFC_HAL_TASK_EVT_INITIALIZE signal. Opening NFC transport...");
    590 
    591             nfc_hal_main_open_transport ();
    592         }
    593 
    594         /* Check for terminate event */
    595         if (event & NFC_HAL_TASK_EVT_TERMINATE)
    596         {
    597             HAL_TRACE_DEBUG0 ("NFC_HAL_TASK got NFC_HAL_TASK_EVT_TERMINATE");
    598             nfc_hal_main_handle_terminate ();
    599 
    600             /* Close uart */
    601             USERIAL_Close (USERIAL_NFC_PORT);
    602 
    603             if (nfc_hal_cb.p_stack_cback)
    604             {
    605                 nfc_hal_cb.p_stack_cback (HAL_NFC_CLOSE_CPLT_EVT, HAL_NFC_STATUS_OK);
    606                 nfc_hal_cb.p_stack_cback = NULL;
    607             }
    608             continue;
    609         }
    610 
    611         /* Check for power cycle event */
    612         if (event & NFC_HAL_TASK_EVT_POWER_CYCLE)
    613         {
    614             HAL_TRACE_DEBUG0 ("NFC_HAL_TASK got NFC_HAL_TASK_EVT_POWER_CYCLE");
    615             nfc_hal_main_handle_terminate ();
    616 
    617             /* Close uart */
    618             USERIAL_Close (USERIAL_NFC_PORT);
    619 
    620             /* power cycle timeout */
    621             nfc_hal_main_start_quick_timer (&nfc_hal_cb.timer, NFC_HAL_TTYPE_POWER_CYCLE,
    622                                             (NFC_HAL_POWER_CYCLE_DELAY*QUICK_TIMER_TICKS_PER_SEC)/1000);
    623             continue;
    624         }
    625 
    626         /* NCI message ready to be sent to NFCC */
    627         if (event & NFC_HAL_TASK_EVT_MBOX)
    628         {
    629             while ((p_msg = (NFC_HDR *) GKI_read_mbox (NFC_HAL_TASK_MBOX)) != NULL)
    630             {
    631                 free_msg = TRUE;
    632                 switch (p_msg->event & NFC_EVT_MASK)
    633                 {
    634                 case NFC_HAL_EVT_TO_NFC_NCI:
    635                     nfc_hal_main_send_message (p_msg);
    636                     /* do not free buffer. NCI VS code may keep it for processing later */
    637                     free_msg = FALSE;
    638                     break;
    639 
    640                 case NFC_HAL_EVT_POST_CORE_RESET:
    641                     NFC_HAL_SET_INIT_STATE (NFC_HAL_INIT_STATE_W4_POST_INIT_DONE);
    642 
    643                     /* set NCI Control packet size from CORE_INIT_RSP */
    644                     p = (UINT8 *) (p_msg + 1) + p_msg->offset + NCI_MSG_HDR_SIZE;
    645                     p += 5;
    646                     STREAM_TO_UINT8 (num_interfaces, p);
    647                     p += (num_interfaces + 3);
    648                     nfc_hal_cb.ncit_cb.nci_ctrl_size = *p;
    649 
    650                     /* start post initialization */
    651                     nfc_hal_cb.dev_cb.next_dm_config = NFC_HAL_DM_CONFIG_LPTD;
    652                     nfc_hal_cb.dev_cb.next_startup_vsc = 1;
    653 
    654                     nfc_hal_dm_config_nfcc ();
    655                     break;
    656 
    657                 case NFC_HAL_EVT_TO_START_QUICK_TIMER:
    658                     GKI_start_timer (NFC_HAL_QUICK_TIMER_ID, ((GKI_SECS_TO_TICKS (1) / QUICK_TIMER_TICKS_PER_SEC)), TRUE);
    659                     break;
    660 
    661                 case NFC_HAL_EVT_HCI:
    662                     nfc_hal_hci_evt_hdlr ((tNFC_HAL_HCI_EVENT_DATA *) p_msg);
    663                     break;
    664 
    665                 case NFC_HAL_EVT_PRE_DISCOVER:
    666                     NFC_HAL_SET_INIT_STATE(NFC_HAL_INIT_STATE_W4_PREDISCOVER_DONE);
    667                     nfa_hal_send_pre_discover_cfg ();
    668                     break;
    669 
    670                 case NFC_HAL_EVT_CONTROL_GRANTED:
    671                     nfc_hal_dm_send_pend_cmd ();
    672                     break;
    673 
    674                 default:
    675                     break;
    676                 }
    677 
    678                 if (free_msg)
    679                     GKI_freebuf (p_msg);
    680             }
    681         }
    682 
    683         /* Data waiting to be read from serial port */
    684         if (event & NFC_HAL_TASK_EVT_DATA_RDY)
    685         {
    686             while (TRUE)
    687             {
    688                 /* Read one byte to see if there is anything waiting to be read */
    689                 if (USERIAL_Read (USERIAL_NFC_PORT, &byte, 1) == 0)
    690                 {
    691                     break;
    692                 }
    693 
    694                 if (nfc_hal_nci_receive_msg (byte))
    695                 {
    696                     /* complete of receiving NCI message */
    697                     nfc_hal_nci_assemble_nci_msg ();
    698                     if (nfc_hal_cb.ncit_cb.p_rcv_msg)
    699                     {
    700                         if (nfc_hal_nci_preproc_rx_nci_msg (nfc_hal_cb.ncit_cb.p_rcv_msg))
    701                         {
    702                             /* Send NCI message to the stack */
    703                             nfc_hal_send_nci_msg_to_nfc_task (nfc_hal_cb.ncit_cb.p_rcv_msg);
    704                         }
    705                         else
    706                         {
    707                             if (nfc_hal_cb.ncit_cb.p_rcv_msg)
    708                                 GKI_freebuf(nfc_hal_cb.ncit_cb.p_rcv_msg);
    709                         }
    710                         nfc_hal_cb.ncit_cb.p_rcv_msg = NULL;
    711                     }
    712                 }
    713             } /* while (TRUE) */
    714         }
    715 
    716         /* Process quick timer tick */
    717         if (event & NFC_HAL_QUICK_TIMER_EVT_MASK)
    718         {
    719             nfc_hal_main_process_quick_timer_evt ();
    720         }
    721     }
    722 
    723     HAL_TRACE_DEBUG0 ("nfc_hal_main_task terminated");
    724 
    725     GKI_exit_task (GKI_get_taskid ());
    726     return 0;
    727 }
    728 
    729 /*******************************************************************************
    730 **
    731 ** Function         HAL_NfcSetTraceLevel
    732 **
    733 ** Description      This function sets the trace level for HAL.  If called with
    734 **                  a value of 0xFF, it simply returns the current trace level.
    735 **
    736 ** Returns          The new or current trace level
    737 **
    738 *******************************************************************************/
    739 UINT8 HAL_NfcSetTraceLevel (UINT8 new_level)
    740 {
    741     if (new_level != 0xFF)
    742         nfc_hal_cb.trace_level = new_level;
    743 
    744     return (nfc_hal_cb.trace_level);
    745 }
    746