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  */
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#if (NFC_HAL_DEBUG == TRUE)
57const 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*******************************************************************************/
82void 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*******************************************************************************/
101static 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*******************************************************************************/
141void 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*******************************************************************************/
157void 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*******************************************************************************/
176void 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*******************************************************************************/
191static 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*******************************************************************************/
235void 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*******************************************************************************/
260static 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*******************************************************************************/
292static 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*******************************************************************************/
340void 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*******************************************************************************/
380void 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*******************************************************************************/
400static 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*******************************************************************************/
433void 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*******************************************************************************/
457static 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*******************************************************************************/
503static void nfc_hal_main_send_message (NFC_HDR *p_msg)
504{
505#if (defined(NFC_HAL_HCI_INCLUDED) && (NFC_HAL_HCI_INCLUDED == TRUE))
506    UINT8   cid, pbf;
507    UINT16  data_len;
508#endif
509    UINT8   *ps, *pp;
510    UINT16  len = p_msg->len;
511#ifdef DISP_NCI
512    UINT8   delta;
513#endif
514
515    HAL_TRACE_DEBUG1 ("nfc_hal_main_send_message() ls:0x%x", p_msg->layer_specific);
516    if (  (p_msg->layer_specific == NFC_HAL_WAIT_RSP_CMD)
517        ||(p_msg->layer_specific == NFC_HAL_WAIT_RSP_VSC)  )
518    {
519        nfc_hal_nci_send_cmd (p_msg);
520    }
521    else
522    {
523        /* NFC task has fragmented the data packet to the appropriate size
524         * and data credit is available; just send it */
525
526        /* add NCI packet type in front of message */
527        nfc_hal_nci_add_nfc_pkt_type (p_msg);
528
529        /* send this packet to transport */
530        ps = (UINT8 *) (p_msg + 1) + p_msg->offset;
531        pp = ps + 1;
532#ifdef DISP_NCI
533        delta = p_msg->len - len;
534        DISP_NCI (ps + delta, (UINT16) (p_msg->len - delta), FALSE);
535#endif
536
537#if (defined(NFC_HAL_HCI_INCLUDED) && (NFC_HAL_HCI_INCLUDED == TRUE))
538        if (nfc_hal_cb.hci_cb.hcp_conn_id)
539        {
540            NCI_DATA_PRS_HDR(pp, pbf, cid, data_len);
541            if (cid == nfc_hal_cb.hci_cb.hcp_conn_id)
542            {
543                if (nfc_hal_hci_handle_hcp_pkt_to_hc (pp))
544                {
545                    HAL_TRACE_DEBUG0 ("nfc_hal_main_send_message() - Drop rsp to Fake cmd, Fake credit ntf");
546                    GKI_freebuf (p_msg);
547                    nfc_hal_send_credit_ntf_for_cid (cid);
548                    return;
549                }
550            }
551
552        }
553#endif
554
555        /* check low power mode state */
556        if (nfc_hal_dm_power_mode_execute (NFC_HAL_LP_TX_DATA_EVT))
557        {
558            USERIAL_Write (USERIAL_NFC_PORT, ps, p_msg->len);
559        }
560        else
561        {
562            HAL_TRACE_ERROR0 ("nfc_hal_main_send_message(): drop data in low power mode");
563        }
564        GKI_freebuf (p_msg);
565    }
566}
567
568/*******************************************************************************
569**
570** Function         nfc_hal_main_task
571**
572** Description      NFC HAL NCI transport event processing task
573**
574** Returns          0
575**
576*******************************************************************************/
577UINT32 nfc_hal_main_task (UINT32 param)
578{
579    UINT16   event;
580    UINT8    byte;
581    UINT8    num_interfaces;
582    UINT8    *p;
583    NFC_HDR  *p_msg;
584    BOOLEAN  free_msg;
585
586    HAL_TRACE_DEBUG0 ("NFC_HAL_TASK started");
587
588    /* Main loop */
589    while (TRUE)
590    {
591        event = GKI_wait (0xFFFF, 0);
592
593        /* Handle NFC_HAL_TASK_EVT_INITIALIZE (for initializing NCI transport) */
594        if (event & NFC_HAL_TASK_EVT_INITIALIZE)
595        {
596            HAL_TRACE_DEBUG0 ("NFC_HAL_TASK got NFC_HAL_TASK_EVT_INITIALIZE signal. Opening NFC transport...");
597
598            nfc_hal_main_open_transport ();
599        }
600
601        /* Check for terminate event */
602        if (event & NFC_HAL_TASK_EVT_TERMINATE)
603        {
604            HAL_TRACE_DEBUG0 ("NFC_HAL_TASK got NFC_HAL_TASK_EVT_TERMINATE");
605            nfc_hal_main_handle_terminate ();
606
607            /* Close uart */
608            USERIAL_Close (USERIAL_NFC_PORT);
609
610            if (nfc_hal_cb.p_stack_cback)
611            {
612                nfc_hal_cb.p_stack_cback (HAL_NFC_CLOSE_CPLT_EVT, HAL_NFC_STATUS_OK);
613                nfc_hal_cb.p_stack_cback = NULL;
614            }
615            continue;
616        }
617
618        /* Check for power cycle event */
619        if (event & NFC_HAL_TASK_EVT_POWER_CYCLE)
620        {
621            HAL_TRACE_DEBUG0 ("NFC_HAL_TASK got NFC_HAL_TASK_EVT_POWER_CYCLE");
622            nfc_hal_main_handle_terminate ();
623
624            /* Close uart */
625            USERIAL_Close (USERIAL_NFC_PORT);
626
627            /* power cycle timeout */
628            nfc_hal_main_start_quick_timer (&nfc_hal_cb.timer, NFC_HAL_TTYPE_POWER_CYCLE,
629                                            (NFC_HAL_POWER_CYCLE_DELAY*QUICK_TIMER_TICKS_PER_SEC)/1000);
630            continue;
631        }
632
633        /* NCI message ready to be sent to NFCC */
634        if (event & NFC_HAL_TASK_EVT_MBOX)
635        {
636            while ((p_msg = (NFC_HDR *) GKI_read_mbox (NFC_HAL_TASK_MBOX)) != NULL)
637            {
638                free_msg = TRUE;
639                switch (p_msg->event & NFC_EVT_MASK)
640                {
641                case NFC_HAL_EVT_TO_NFC_NCI:
642                    nfc_hal_main_send_message (p_msg);
643                    /* do not free buffer. NCI VS code may keep it for processing later */
644                    free_msg = FALSE;
645                    break;
646
647                case NFC_HAL_EVT_POST_CORE_RESET:
648                    NFC_HAL_SET_INIT_STATE (NFC_HAL_INIT_STATE_W4_POST_INIT_DONE);
649
650                    /* set NCI Control packet size from CORE_INIT_RSP */
651                    p = (UINT8 *) (p_msg + 1) + p_msg->offset + NCI_MSG_HDR_SIZE;
652                    p += 5;
653                    STREAM_TO_UINT8 (num_interfaces, p);
654                    p += (num_interfaces + 3);
655                    nfc_hal_cb.ncit_cb.nci_ctrl_size = *p;
656
657                    /* start post initialization */
658                    nfc_hal_cb.dev_cb.next_dm_config = NFC_HAL_DM_CONFIG_LPTD;
659                    nfc_hal_cb.dev_cb.next_startup_vsc = 1;
660
661                    nfc_hal_dm_config_nfcc ();
662                    break;
663
664                case NFC_HAL_EVT_TO_START_QUICK_TIMER:
665                    GKI_start_timer (NFC_HAL_QUICK_TIMER_ID, ((GKI_SECS_TO_TICKS (1) / QUICK_TIMER_TICKS_PER_SEC)), TRUE);
666                    break;
667
668                case NFC_HAL_EVT_HCI:
669                    nfc_hal_hci_evt_hdlr ((tNFC_HAL_HCI_EVENT_DATA *) p_msg);
670                    break;
671
672                case NFC_HAL_EVT_PRE_DISCOVER:
673                    NFC_HAL_SET_INIT_STATE(NFC_HAL_INIT_STATE_W4_PREDISCOVER_DONE);
674                    nfa_hal_send_pre_discover_cfg ();
675                    break;
676
677                case NFC_HAL_EVT_CONTROL_GRANTED:
678                    nfc_hal_dm_send_pend_cmd ();
679                    break;
680
681                default:
682                    break;
683                }
684
685                if (free_msg)
686                    GKI_freebuf (p_msg);
687            }
688        }
689
690        /* Data waiting to be read from serial port */
691        if (event & NFC_HAL_TASK_EVT_DATA_RDY)
692        {
693            while (TRUE)
694            {
695                /* Read one byte to see if there is anything waiting to be read */
696                if (USERIAL_Read (USERIAL_NFC_PORT, &byte, 1) == 0)
697                {
698                    break;
699                }
700
701                if (nfc_hal_nci_receive_msg (byte))
702                {
703                    /* complete of receiving NCI message */
704                    nfc_hal_nci_assemble_nci_msg ();
705                    if (nfc_hal_cb.ncit_cb.p_rcv_msg)
706                    {
707                        if (nfc_hal_nci_preproc_rx_nci_msg (nfc_hal_cb.ncit_cb.p_rcv_msg))
708                        {
709                            /* Send NCI message to the stack */
710                            nfc_hal_send_nci_msg_to_nfc_task (nfc_hal_cb.ncit_cb.p_rcv_msg);
711                        }
712                        else
713                        {
714                            if (nfc_hal_cb.ncit_cb.p_rcv_msg)
715                                GKI_freebuf(nfc_hal_cb.ncit_cb.p_rcv_msg);
716                        }
717                        nfc_hal_cb.ncit_cb.p_rcv_msg = NULL;
718                    }
719                }
720            } /* while (TRUE) */
721        }
722
723        /* Process quick timer tick */
724        if (event & NFC_HAL_QUICK_TIMER_EVT_MASK)
725        {
726            nfc_hal_main_process_quick_timer_evt ();
727        }
728    }
729
730    HAL_TRACE_DEBUG0 ("nfc_hal_main_task terminated");
731
732    GKI_exit_task (GKI_get_taskid ());
733    return 0;
734}
735
736/*******************************************************************************
737**
738** Function         HAL_NfcSetTraceLevel
739**
740** Description      This function sets the trace level for HAL.  If called with
741**                  a value of 0xFF, it simply returns the current trace level.
742**
743** Returns          The new or current trace level
744**
745*******************************************************************************/
746UINT8 HAL_NfcSetTraceLevel (UINT8 new_level)
747{
748    if (new_level != 0xFF)
749        nfc_hal_cb.trace_level = new_level;
750
751    return (nfc_hal_cb.trace_level);
752}
753