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