Home | History | Annotate | Download | only in hal
      1 /******************************************************************************
      2  *
      3  *  Copyright (C) 2010-2014 Broadcom Corporation
      4  *
      5  *  Licensed under the Apache License, Version 2.0 (the "License");
      6  *  you may not use this file except in compliance with the License.
      7  *  You may obtain a copy of the License at:
      8  *
      9  *  http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  *
     17  ******************************************************************************/
     18 
     19 
     20 /******************************************************************************
     21  *
     22  *  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         nfc_hal_main_close
    135 **
    136 ** Description      Check and shutdown NFCC
    137 **
    138 ** Returns          None
    139 **
    140 *******************************************************************************/
    141 void nfc_hal_main_close (void)
    142 {
    143     tHAL_NFC_CBACK *p_stack_cback_temp;
    144 
    145     if (  (nfc_hal_cb.dev_cb.initializing_state != NFC_HAL_INIT_STATE_W4_NFCC_TURN_OFF)
    146         && (nfc_hal_cb.hal_flags & NFC_HAL_FLAGS_NEED_DISABLE_VSC) )
    147     {
    148         nfc_hal_cb.dev_cb.initializing_state = NFC_HAL_INIT_STATE_W4_NFCC_TURN_OFF;
    149         nfc_hal_dm_set_power_level_zero ();
    150     }
    151     else
    152     {
    153         nfc_hal_main_handle_terminate ();
    154 
    155         /* Close uart */
    156         USERIAL_Close (USERIAL_NFC_PORT);
    157 
    158         if (nfc_hal_cb.p_stack_cback)
    159         {
    160             p_stack_cback_temp          = nfc_hal_cb.p_stack_cback;
    161             nfc_hal_cb.p_stack_cback    = NULL;
    162             p_stack_cback_temp (HAL_NFC_CLOSE_CPLT_EVT, HAL_NFC_STATUS_OK);
    163         }
    164     }
    165 }
    166 
    167 /*******************************************************************************
    168 **
    169 ** Function         nfa_hal_pre_discover_done_cback
    170 **
    171 ** Description      Pre-discovery CFG is sent.
    172 **
    173 ** Returns          nothing
    174 **
    175 *******************************************************************************/
    176 void nfa_hal_pre_discover_done_cback (tNFC_HAL_NCI_EVT event, UINT16 data_len, UINT8 *p_data)
    177 {
    178     NFC_HAL_SET_INIT_STATE(NFC_HAL_INIT_STATE_IDLE);
    179     nfc_hal_main_stop_quick_timer (&nfc_hal_cb.ncit_cb.nci_wait_rsp_timer);
    180     nfc_hal_cb.p_stack_cback (HAL_NFC_PRE_DISCOVER_CPLT_EVT, HAL_NFC_STATUS_OK);
    181 }
    182 
    183 /*******************************************************************************
    184 **
    185 ** Function         nfa_hal_send_pre_discover_cfg
    186 **
    187 ** Description      sending Pre-discovery CFG
    188 **
    189 ** Returns          nothing
    190 **
    191 *******************************************************************************/
    192 void nfa_hal_send_pre_discover_cfg (void)
    193 {
    194     if (nfc_hal_dm_set_config (p_nfc_hal_pre_discover_cfg [0],
    195                                &p_nfc_hal_pre_discover_cfg[1],
    196                                 nfa_hal_pre_discover_done_cback) != HAL_NFC_STATUS_OK)
    197     {
    198         nfa_hal_pre_discover_done_cback(0, 0, NULL);
    199     }
    200 }
    201 
    202 /*******************************************************************************
    203 **
    204 ** Function         nfc_hal_main_send_error
    205 **
    206 ** Description      send an Error event to NFC stack
    207 **
    208 ** Returns          nothing
    209 **
    210 *******************************************************************************/
    211 void nfc_hal_main_send_error (tHAL_NFC_STATUS status)
    212 {
    213     /* Notify stack */
    214     nfc_hal_cb.p_stack_cback(HAL_NFC_ERROR_EVT, status);
    215 }
    216 
    217 /*******************************************************************************
    218 **
    219 ** Function         nfc_hal_main_userial_cback
    220 **
    221 ** Description      USERIAL callback for NCI transport
    222 **
    223 ** Returns          nothing
    224 **
    225 *******************************************************************************/
    226 static void nfc_hal_main_userial_cback (tUSERIAL_PORT port, tUSERIAL_EVT evt, tUSERIAL_EVT_DATA *p_data)
    227 {
    228     if (evt == USERIAL_RX_READY_EVT)
    229     {
    230         /* Notify transport task of serial port event */
    231         GKI_send_event (NFC_HAL_TASK, NFC_HAL_TASK_EVT_DATA_RDY);
    232     }
    233     else if (evt == USERIAL_TX_DONE_EVT)
    234     {
    235         /* Serial driver has finshed sending data from USERIAL_Write */
    236         /* Currently, no action is needed for this event */
    237     }
    238     else if (evt == USERIAL_ERR_EVT)
    239     {
    240         HAL_TRACE_ERROR0 ("nfc_hal_main_userial_cback: USERIAL_ERR_EVT. Notifying NFC_TASK of transport error");
    241         if (nfc_hal_cb.ncit_cb.nci_wait_rsp != NFC_HAL_WAIT_RSP_NONE)
    242         {
    243             nfc_hal_main_stop_quick_timer (&nfc_hal_cb.ncit_cb.nci_wait_rsp_timer);
    244             nfc_hal_nci_cmd_timeout_cback ((void *)&nfc_hal_cb.ncit_cb.nci_wait_rsp_timer);
    245         }
    246         else
    247         {
    248             nfc_hal_main_send_error (HAL_NFC_STATUS_ERR_TRANSPORT);
    249         }
    250     }
    251     else if (evt == USERIAL_WAKEUP_EVT)
    252     {
    253         HAL_TRACE_DEBUG1 ("nfc_hal_main_userial_cback: USERIAL_WAKEUP_EVT: %d", p_data->sigs);
    254     }
    255     else
    256     {
    257         HAL_TRACE_DEBUG1 ("nfc_hal_main_userial_cback: unhandled userial evt: %i", evt);
    258     }
    259 }
    260 
    261 /*******************************************************************************
    262 **
    263 ** Function         nfc_hal_main_exit_op_done
    264 **
    265 ** Description      handle completion of HAL exit operation
    266 **
    267 ** Returns          nothing
    268 **
    269 *******************************************************************************/
    270 void nfc_hal_main_exit_op_done (tNFC_HAL_NCI_EVT event, UINT16 data_len, UINT8 *p_data)
    271 {
    272     nfc_hal_main_close ();
    273 }
    274 
    275 /*******************************************************************************
    276 **
    277 ** Function         nfc_hal_main_pre_init_done
    278 **
    279 ** Description      notify complete of pre-initialization
    280 **
    281 ** Returns          nothing
    282 **
    283 *******************************************************************************/
    284 void nfc_hal_main_pre_init_done (tHAL_NFC_STATUS status)
    285 {
    286     HAL_TRACE_DEBUG1 ("nfc_hal_main_pre_init_done () status = %d", status);
    287 
    288     if (status != HAL_NFC_STATUS_OK)
    289     {
    290         nfc_hal_main_handle_terminate ();
    291 
    292         /* Close uart */
    293         USERIAL_Close (USERIAL_NFC_PORT);
    294     }
    295 
    296     /* Notify NFC Task the status of initialization */
    297     nfc_hal_cb.p_stack_cback (HAL_NFC_OPEN_CPLT_EVT, status);
    298 }
    299 
    300 /*******************************************************************************
    301 **
    302 ** Function         nfc_hal_main_timeout_cback
    303 **
    304 ** Description      callback function for timeout
    305 **
    306 ** Returns          void
    307 **
    308 *******************************************************************************/
    309 static void nfc_hal_main_timeout_cback (void *p_tle)
    310 {
    311     TIMER_LIST_ENT  *p_tlent = (TIMER_LIST_ENT *) p_tle;
    312 
    313     HAL_TRACE_DEBUG0 ("nfc_hal_main_timeout_cback ()");
    314 
    315     switch (p_tlent->event)
    316     {
    317     case NFC_HAL_TTYPE_POWER_CYCLE:
    318         nfc_hal_main_open_transport ();
    319         break;
    320 
    321     case NFC_HAL_TTYPE_NFCC_ENABLE:
    322         /* NFCC should have enabled now, notify transport openned */
    323         nfc_hal_dm_pre_init_nfcc ();
    324         break;
    325 
    326     default:
    327         HAL_TRACE_DEBUG1 ("nfc_hal_main_timeout_cback: unhandled timer event (0x%04x)", p_tlent->event);
    328         break;
    329     }
    330 }
    331 
    332 /*******************************************************************************
    333 **
    334 ** Function         nfc_hal_main_handle_terminate
    335 **
    336 ** Description      Handle NFI transport shutdown
    337 **
    338 ** Returns          nothing
    339 **
    340 *******************************************************************************/
    341 static void nfc_hal_main_handle_terminate (void)
    342 {
    343     NFC_HDR *p_msg;
    344 
    345     /* dequeue and free buffer */
    346     if (nfc_hal_cb.ncit_cb.p_pend_cmd != NULL)
    347     {
    348         GKI_freebuf (nfc_hal_cb.ncit_cb.p_pend_cmd);
    349         nfc_hal_cb.ncit_cb.p_pend_cmd = NULL;
    350     }
    351 
    352     /* Free unsent nfc rx buffer */
    353     if (nfc_hal_cb.ncit_cb.p_rcv_msg)
    354     {
    355         GKI_freebuf (nfc_hal_cb.ncit_cb.p_rcv_msg);
    356         nfc_hal_cb.ncit_cb.p_rcv_msg  = NULL;
    357     }
    358 
    359     /* Free buffer for pending fragmented response/notification */
    360     if (nfc_hal_cb.ncit_cb.p_frag_msg)
    361     {
    362         GKI_freebuf (nfc_hal_cb.ncit_cb.p_frag_msg);
    363         nfc_hal_cb.ncit_cb.p_frag_msg = NULL;
    364     }
    365 
    366     /* Free buffers in the tx mbox */
    367     while ((p_msg = (NFC_HDR *) GKI_read_mbox (NFC_HAL_TASK_MBOX)) != NULL)
    368     {
    369         GKI_freebuf (p_msg);
    370     }
    371 
    372     /* notify closing transport */
    373     nfc_hal_dm_shutting_down_nfcc ();
    374 }
    375 
    376 /*******************************************************************************
    377 **
    378 ** Function         nfc_hal_main_start_quick_timer
    379 **
    380 ** Description      Start a timer for the specified amount of time.
    381 **                  NOTE: The timeout resolution depends on including modules.
    382 **                  QUICK_TIMER_TICKS_PER_SEC should be used to convert from
    383 **                  time to ticks.
    384 **
    385 **
    386 ** Returns          void
    387 **
    388 *******************************************************************************/
    389 void nfc_hal_main_start_quick_timer (TIMER_LIST_ENT *p_tle, UINT16 type, UINT32 timeout)
    390 {
    391     NFC_HDR *p_msg;
    392 
    393     /* if timer list is currently empty, start periodic GKI timer */
    394     if (nfc_hal_cb.quick_timer_queue.p_first == NULL)
    395     {
    396         /* if timer starts on other than NCIT task (script wrapper) */
    397         if(GKI_get_taskid () != NFC_HAL_TASK)
    398         {
    399             /* post event to start timer in NCIT task */
    400             if ((p_msg = (NFC_HDR *) GKI_getbuf (NFC_HDR_SIZE)) != NULL)
    401             {
    402                 p_msg->event = NFC_HAL_EVT_TO_START_QUICK_TIMER;
    403                 GKI_send_msg (NFC_HAL_TASK, NFC_HAL_TASK_MBOX, p_msg);
    404             }
    405         }
    406         else
    407         {
    408             GKI_start_timer (NFC_HAL_QUICK_TIMER_ID, ((GKI_SECS_TO_TICKS (1) / QUICK_TIMER_TICKS_PER_SEC)), TRUE);
    409         }
    410     }
    411 
    412     GKI_remove_from_timer_list (&nfc_hal_cb.quick_timer_queue, p_tle);
    413 
    414     p_tle->event = type;
    415     p_tle->ticks = timeout; /* Save the number of ticks for the timer */
    416 
    417     GKI_add_to_timer_list (&nfc_hal_cb.quick_timer_queue, p_tle);
    418 }
    419 
    420 /*******************************************************************************
    421 **
    422 ** Function         nfc_hal_main_stop_quick_timer
    423 **
    424 ** Description      Stop a timer.
    425 **
    426 ** Returns          void
    427 **
    428 *******************************************************************************/
    429 void nfc_hal_main_stop_quick_timer (TIMER_LIST_ENT *p_tle)
    430 {
    431     GKI_remove_from_timer_list (&nfc_hal_cb.quick_timer_queue, p_tle);
    432 
    433     /* if timer list is empty stop periodic GKI timer */
    434     if (nfc_hal_cb.quick_timer_queue.p_first == NULL)
    435     {
    436         GKI_stop_timer (NFC_HAL_QUICK_TIMER_ID);
    437     }
    438 }
    439 
    440 /*******************************************************************************
    441 **
    442 ** Function         nfc_hal_main_process_quick_timer_evt
    443 **
    444 ** Description      Process quick timer event
    445 **
    446 ** Returns          void
    447 **
    448 *******************************************************************************/
    449 static void nfc_hal_main_process_quick_timer_evt (void)
    450 {
    451     TIMER_LIST_ENT  *p_tle;
    452 
    453     GKI_update_timer_list (&nfc_hal_cb.quick_timer_queue, 1);
    454 
    455     while ((nfc_hal_cb.quick_timer_queue.p_first) && (!nfc_hal_cb.quick_timer_queue.p_first->ticks))
    456     {
    457         p_tle = nfc_hal_cb.quick_timer_queue.p_first;
    458         GKI_remove_from_timer_list (&nfc_hal_cb.quick_timer_queue, p_tle);
    459 
    460         if (p_tle->p_cback)
    461         {
    462             (*p_tle->p_cback) (p_tle);
    463         }
    464     }
    465 
    466     /* if timer list is empty stop periodic GKI timer */
    467     if (nfc_hal_cb.quick_timer_queue.p_first == NULL)
    468     {
    469         GKI_stop_timer (NFC_HAL_QUICK_TIMER_ID);
    470     }
    471 }
    472 
    473 /*******************************************************************************
    474 **
    475 ** Function         nfc_hal_send_nci_msg_to_nfc_task
    476 **
    477 ** Description      This function is called to send nci message to nfc task
    478 **
    479 ** Returns          void
    480 **
    481 *******************************************************************************/
    482 void nfc_hal_send_nci_msg_to_nfc_task (NFC_HDR * p_msg)
    483 {
    484 #ifdef NFC_HAL_SHARED_GKI
    485     /* Using shared NFC/HAL GKI resources - send message buffer directly to NFC_TASK for processing */
    486     p_msg->event = BT_EVT_TO_NFC_NCI;
    487     GKI_send_msg (NFC_TASK, NFC_MBOX_ID, p_msg);
    488 #else
    489     /* Send NCI message to the stack */
    490     nfc_hal_cb.p_data_cback (p_msg->len, (UINT8 *) ((p_msg + 1)
    491                                  + p_msg->offset));
    492     GKI_freebuf(p_msg);
    493 #endif
    494 }
    495 
    496 /*******************************************************************************
    497 **
    498 ** Function         nfc_hal_send_credit_ntf_for_cid
    499 **
    500 ** Description      This function is called to send credit ntf
    501 **                  for the specified connection id to nfc task
    502 **
    503 ** Returns          void
    504 **
    505 *******************************************************************************/
    506 static void nfc_hal_send_credit_ntf_for_cid (UINT8 cid)
    507 {
    508     NFC_HDR  *p_msg;
    509     UINT8    *p, *ps;
    510 
    511     /* Start of new message. Allocate a buffer for message */
    512     if ((p_msg = (NFC_HDR *) GKI_getpoolbuf (NFC_HAL_NCI_POOL_ID)) != NULL)
    513     {
    514         /* Initialize NFC_HDR */
    515         p_msg->len    = NCI_DATA_HDR_SIZE + 0x03;
    516         p_msg->event  = 0;
    517         p_msg->offset = 0;
    518         p_msg->layer_specific = 0;
    519 
    520         p = (UINT8 *) (p_msg + 1) + p_msg->offset;
    521         ps = p;
    522         NCI_MSG_BLD_HDR0(p, NCI_MT_NTF, NCI_GID_CORE);
    523         NCI_MSG_BLD_HDR1(p, NCI_MSG_CORE_CONN_CREDITS);
    524         UINT8_TO_STREAM (p, 0x03);
    525 
    526         /* Number of credit entries */
    527         *p++ = 0x01;
    528         /* Connection id of the credit ntf */
    529         *p++ = cid;
    530         /* Number of credits */
    531         *p = 0x01;
    532 #ifdef DISP_NCI
    533         DISP_NCI (ps, (UINT16) p_msg->len, TRUE);
    534 #endif
    535         nfc_hal_send_nci_msg_to_nfc_task (p_msg);
    536     }
    537     else
    538     {
    539         HAL_TRACE_ERROR0 ("Unable to allocate buffer for Sending credit ntf to stack");
    540     }
    541 }
    542 
    543 /*******************************************************************************
    544 **
    545 ** Function         nfc_hal_main_send_message
    546 **
    547 ** Description      This function is calledto send an NCI message.
    548 **
    549 ** Returns          void
    550 **
    551 *******************************************************************************/
    552 static void nfc_hal_main_send_message (NFC_HDR *p_msg)
    553 {
    554 #if (defined(NFC_HAL_HCI_INCLUDED) && (NFC_HAL_HCI_INCLUDED == TRUE))
    555     UINT8   cid, pbf;
    556     UINT16  data_len;
    557 #endif
    558     UINT8   *ps, *pp;
    559     UINT16  len = p_msg->len;
    560 #ifdef DISP_NCI
    561     UINT8   delta;
    562 #endif
    563 
    564     HAL_TRACE_DEBUG1 ("nfc_hal_main_send_message() ls:0x%x", p_msg->layer_specific);
    565     if (  (p_msg->layer_specific == NFC_HAL_WAIT_RSP_CMD)
    566         ||(p_msg->layer_specific == NFC_HAL_WAIT_RSP_VSC)  )
    567     {
    568         nfc_hal_nci_send_cmd (p_msg);
    569     }
    570     else
    571     {
    572         /* NFC task has fragmented the data packet to the appropriate size
    573          * and data credit is available; just send it */
    574 
    575         /* add NCI packet type in front of message */
    576         nfc_hal_nci_add_nfc_pkt_type (p_msg);
    577 
    578         /* send this packet to transport */
    579         ps = (UINT8 *) (p_msg + 1) + p_msg->offset;
    580         pp = ps + 1;
    581 #ifdef DISP_NCI
    582         delta = p_msg->len - len;
    583         DISP_NCI (ps + delta, (UINT16) (p_msg->len - delta), FALSE);
    584 #endif
    585 
    586 #if (defined(NFC_HAL_HCI_INCLUDED) && (NFC_HAL_HCI_INCLUDED == TRUE))
    587         if (nfc_hal_cb.hci_cb.hcp_conn_id)
    588         {
    589             NCI_DATA_PRS_HDR(pp, pbf, cid, data_len);
    590             if (cid == nfc_hal_cb.hci_cb.hcp_conn_id)
    591             {
    592                 if (nfc_hal_hci_handle_hcp_pkt_to_hc (pp))
    593                 {
    594                     HAL_TRACE_DEBUG0 ("nfc_hal_main_send_message() - Drop rsp to Fake cmd, Fake credit ntf");
    595                     GKI_freebuf (p_msg);
    596                     nfc_hal_send_credit_ntf_for_cid (cid);
    597                     return;
    598                 }
    599             }
    600 
    601         }
    602 #endif
    603 
    604         /* check low power mode state */
    605         if (nfc_hal_dm_power_mode_execute (NFC_HAL_LP_TX_DATA_EVT))
    606         {
    607             USERIAL_Write (USERIAL_NFC_PORT, ps, p_msg->len);
    608         }
    609         else
    610         {
    611             HAL_TRACE_ERROR0 ("nfc_hal_main_send_message(): drop data in low power mode");
    612         }
    613         GKI_freebuf (p_msg);
    614     }
    615 }
    616 
    617 /*******************************************************************************
    618 **
    619 ** Function         nfc_hal_main_task
    620 **
    621 ** Description      NFC HAL NCI transport event processing task
    622 **
    623 ** Returns          0
    624 **
    625 *******************************************************************************/
    626 UINT32 nfc_hal_main_task (UINT32 param)
    627 {
    628     UINT16   event;
    629     UINT8    byte;
    630     UINT8    num_interfaces;
    631     UINT8    *p;
    632     NFC_HDR  *p_msg;
    633     BOOLEAN  free_msg;
    634 
    635     HAL_TRACE_DEBUG0 ("NFC_HAL_TASK started");
    636 
    637     /* Main loop */
    638     while (TRUE)
    639     {
    640         event = GKI_wait (0xFFFF, 0);
    641 
    642         /* Handle NFC_HAL_TASK_EVT_INITIALIZE (for initializing NCI transport) */
    643         if (event & NFC_HAL_TASK_EVT_INITIALIZE)
    644         {
    645             HAL_TRACE_DEBUG0 ("NFC_HAL_TASK got NFC_HAL_TASK_EVT_INITIALIZE signal. Opening NFC transport...");
    646 
    647             nfc_hal_main_open_transport ();
    648         }
    649 
    650         /* Check for terminate event */
    651         if (event & NFC_HAL_TASK_EVT_TERMINATE)
    652         {
    653             HAL_TRACE_DEBUG0 ("NFC_HAL_TASK got NFC_HAL_TASK_EVT_TERMINATE");
    654 
    655             nfc_hal_main_close ();
    656 
    657             continue;
    658         }
    659 
    660         /* Check for power cycle event */
    661         if (event & NFC_HAL_TASK_EVT_POWER_CYCLE)
    662         {
    663             HAL_TRACE_DEBUG0 ("NFC_HAL_TASK got NFC_HAL_TASK_EVT_POWER_CYCLE");
    664             nfc_hal_main_handle_terminate ();
    665 
    666             /* Close uart */
    667             USERIAL_Close (USERIAL_NFC_PORT);
    668 
    669             /* power cycle timeout */
    670             nfc_hal_main_start_quick_timer (&nfc_hal_cb.timer, NFC_HAL_TTYPE_POWER_CYCLE,
    671                                             (NFC_HAL_POWER_CYCLE_DELAY*QUICK_TIMER_TICKS_PER_SEC)/1000);
    672             continue;
    673         }
    674 
    675         /* NCI message ready to be sent to NFCC */
    676         if (event & NFC_HAL_TASK_EVT_MBOX)
    677         {
    678             while ((p_msg = (NFC_HDR *) GKI_read_mbox (NFC_HAL_TASK_MBOX)) != NULL)
    679             {
    680                 free_msg = TRUE;
    681                 switch (p_msg->event & NFC_EVT_MASK)
    682                 {
    683                 case NFC_HAL_EVT_TO_NFC_NCI:
    684                     nfc_hal_main_send_message (p_msg);
    685                     /* do not free buffer. NCI VS code may keep it for processing later */
    686                     free_msg = FALSE;
    687                     break;
    688 
    689                 case NFC_HAL_EVT_POST_CORE_RESET:
    690                     NFC_HAL_SET_INIT_STATE (NFC_HAL_INIT_STATE_W4_POST_INIT_DONE);
    691 
    692                     /* set NCI Control packet size from CORE_INIT_RSP */
    693                     p = (UINT8 *) (p_msg + 1) + p_msg->offset + NCI_MSG_HDR_SIZE;
    694                     p += 5;
    695                     STREAM_TO_UINT8 (num_interfaces, p);
    696                     p += (num_interfaces + 3);
    697                     nfc_hal_cb.ncit_cb.nci_ctrl_size = *p;
    698 
    699                     /* start post initialization */
    700                     nfc_hal_cb.dev_cb.next_dm_config = NFC_HAL_DM_CONFIG_LPTD;
    701                     nfc_hal_cb.dev_cb.next_startup_vsc = 1;
    702 
    703                     nfc_hal_dm_config_nfcc ();
    704                     break;
    705 
    706                 case NFC_HAL_EVT_TO_START_QUICK_TIMER:
    707                     GKI_start_timer (NFC_HAL_QUICK_TIMER_ID, ((GKI_SECS_TO_TICKS (1) / QUICK_TIMER_TICKS_PER_SEC)), TRUE);
    708                     break;
    709 
    710                 case NFC_HAL_EVT_HCI:
    711                     nfc_hal_hci_evt_hdlr ((tNFC_HAL_HCI_EVENT_DATA *) p_msg);
    712                     break;
    713 
    714                 case NFC_HAL_EVT_PRE_DISCOVER:
    715                     NFC_HAL_SET_INIT_STATE(NFC_HAL_INIT_STATE_W4_PREDISCOVER_DONE);
    716                     nfa_hal_send_pre_discover_cfg ();
    717                     break;
    718 
    719                 case NFC_HAL_EVT_CONTROL_GRANTED:
    720                     nfc_hal_dm_send_pend_cmd ();
    721                     break;
    722 
    723                 default:
    724                     break;
    725                 }
    726 
    727                 if (free_msg)
    728                     GKI_freebuf (p_msg);
    729             }
    730         }
    731 
    732         /* Data waiting to be read from serial port */
    733         if (event & NFC_HAL_TASK_EVT_DATA_RDY)
    734         {
    735             while (TRUE)
    736             {
    737                 /* Read one byte to see if there is anything waiting to be read */
    738                 if (USERIAL_Read (USERIAL_NFC_PORT, &byte, 1) == 0)
    739                 {
    740                     break;
    741                 }
    742 
    743                 if (nfc_hal_nci_receive_msg (byte))
    744                 {
    745                     /* complete of receiving NCI message */
    746                     nfc_hal_nci_assemble_nci_msg ();
    747                     if (nfc_hal_cb.ncit_cb.p_rcv_msg)
    748                     {
    749                         if (nfc_hal_nci_preproc_rx_nci_msg (nfc_hal_cb.ncit_cb.p_rcv_msg))
    750                         {
    751                             /* Send NCI message to the stack */
    752                             nfc_hal_send_nci_msg_to_nfc_task (nfc_hal_cb.ncit_cb.p_rcv_msg);
    753                         }
    754                         else
    755                         {
    756                             if (nfc_hal_cb.ncit_cb.p_rcv_msg)
    757                                 GKI_freebuf(nfc_hal_cb.ncit_cb.p_rcv_msg);
    758                         }
    759                         nfc_hal_cb.ncit_cb.p_rcv_msg = NULL;
    760                     }
    761                 }
    762             } /* while (TRUE) */
    763         }
    764 
    765         /* Process quick timer tick */
    766         if (event & NFC_HAL_QUICK_TIMER_EVT_MASK)
    767         {
    768             nfc_hal_main_process_quick_timer_evt ();
    769         }
    770     }
    771 
    772     HAL_TRACE_DEBUG0 ("nfc_hal_main_task terminated");
    773 
    774     GKI_exit_task (GKI_get_taskid ());
    775     return 0;
    776 }
    777 
    778 /*******************************************************************************
    779 **
    780 ** Function         HAL_NfcSetTraceLevel
    781 **
    782 ** Description      This function sets the trace level for HAL.  If called with
    783 **                  a value of 0xFF, it simply returns the current trace level.
    784 **
    785 ** Returns          The new or current trace level
    786 **
    787 *******************************************************************************/
    788 UINT8 HAL_NfcSetTraceLevel (UINT8 new_level)
    789 {
    790     if (new_level != 0xFF)
    791         nfc_hal_cb.trace_level = new_level;
    792 
    793     return (nfc_hal_cb.trace_level);
    794 }
    795