nfc_ncif.c revision eb190654c5fbaea2f396bb5523f57062f291879a
1/*****************************************************************************
2**
3**  Name:          nfc_ncif.c
4**
5**  Description:   This file contains functions that interface with the NFC NCI
6**                 transport. On the receive side, it routes events to
7**                 the appropriate handler (callback). On the
8**                 transmit side, it manages the command transmission.
9**
10**
11**  Copyright (c) 1999-2012, Broadcom Corp., All Rights Reserved.
12**  Broadcom Bluetooth Core. Proprietary and confidential.
13**
14******************************************************************************/
15#include <string.h>
16#include "nfc_target.h"
17
18#if NFC_INCLUDED == TRUE
19#include "nfc_hal_api.h"
20#include "nfc_api.h"
21#include "nci_defs.h"
22#include "nci_hmsgs.h"
23#include "nfc_int.h"
24#include "rw_api.h"
25#include "rw_int.h"
26#include "hcidefs.h"
27#include "nfc_hal_api.h"
28
29#if (NFC_RW_ONLY == FALSE)
30static const UINT8 nfc_mpl_code_to_size[] =
31{64, 128, 192, 254};
32
33#endif /* NFC_RW_ONLY */
34
35
36#define NFC_PB_ATTRIB_REQ_FIXED_BYTES   1
37#define NFC_LB_ATTRIB_REQ_FIXED_BYTES   8
38
39
40/*******************************************************************************
41**
42** Function         nfc_ncif_update_window
43**
44** Description      Update tx cmd window to indicate that NFCC can received
45**                  is_rsp=TRUE if receiving valid NCI rsp from NFCC
46**                  is_rsp=FALSE if timeout waiting for NCI rsp from NFCC
47**
48** Returns          void
49**
50*********************************************************************************/
51void nfc_ncif_update_window(BOOLEAN is_rsp)
52{
53    /* Sanity check - see if we were expecting a update_window */
54    if (nfc_cb.nci_cmd_window == NCI_MAX_CMD_WINDOW)
55    {
56        NFC_TRACE_ERROR0("nfc_ncif_update_window: Unexpected call");
57        return;
58    }
59
60    /* Stop command-pending timer */
61    nfc_stop_timer(&nfc_cb.nci_wait_rsp_timer);
62
63    if (is_rsp)
64    {
65        nfc_cb.p_vsc_cback      = NULL;
66        nfc_cb.nci_cmd_window++;
67    }
68    else
69    {
70        nfc_ncif_event_status(NFC_NFCC_TIMEOUT_REVT, NFC_STATUS_HW_TIMEOUT);
71        /* if enabling NFC, notify upper layer of failure */
72        if (nfc_cb.flags & NFC_FL_ENABLE_PENDING)
73        {
74            nfc_enabled(NFC_STATUS_FAILED, NULL);
75            return;
76        }
77    }
78
79    /* Check if there were any commands waiting to be sent */
80    nfc_ncif_check_cmd_queue (NULL);
81}
82
83/*******************************************************************************
84**
85** Function         nfc_ncif_cmd_timeout
86**
87** Description      Handle a command timeout
88**
89** Returns          void
90**
91*******************************************************************************/
92void nfc_ncif_cmd_timeout (void)
93{
94    NFC_TRACE_ERROR0("nfc_ncif_cmd_timeout");
95
96    /* report an error */
97    nfc_ncif_event_status(NFC_GEN_ERROR_REVT, NFC_STATUS_HW_TIMEOUT);
98    /* Send next command in the xmit_q */
99    nfc_ncif_update_window(FALSE);
100
101}
102
103/*******************************************************************************
104**
105** Function         nfc_wait_2_deactivate_timeout
106**
107** Description      Handle a command timeout
108**
109** Returns          void
110**
111*******************************************************************************/
112void nfc_wait_2_deactivate_timeout (void)
113{
114    NFC_TRACE_ERROR0 ("nfc_wait_2_deactivate_timeout");
115    nfc_cb.flags  &= ~NFC_FL_DEACTIVATING;
116    nci_snd_deactivate_cmd ((UINT8) ((TIMER_PARAM_TYPE) nfc_cb.deactivate_timer.param));
117}
118
119
120/*******************************************************************************
121**
122** Function         nfc_ncif_send_data
123**
124** Description      This function is called to add the NCI data header
125**                  and send it to NCIT task for sending it to transport
126**                  as credits are available.
127**
128** Returns          void
129**
130*******************************************************************************/
131UINT8 nfc_ncif_send_data (tNFC_CONN_CB *p_cb, BT_HDR *p_data)
132{
133    UINT8 *pp;
134    UINT8 *ps;
135    UINT8   ulen = NCI_MAX_PAYLOAD_SIZE;
136    BT_HDR *p;
137    UINT8   pbf = 1;
138    UINT8   buffer_size = p_cb->buff_size;
139    UINT8   hdr0 = p_cb->conn_id;
140    BOOLEAN fragmented = FALSE;
141
142    NFC_TRACE_DEBUG3 ("nfc_ncif_send_data :%d, num_buff:%d qc:%d", p_cb->conn_id, p_cb->num_buff, p_cb->tx_q.count);
143    if (p_cb->id == NFC_RF_CONN_ID)
144    {
145        if (nfc_cb.nfc_state != NFC_STATE_OPEN)
146        {
147            if (nfc_cb.nfc_state == NFC_STATE_CLOSING)
148            {
149                if ((p_data == NULL) && /* called because credit from NFCC */
150                    (nfc_cb.flags  & NFC_FL_DEACTIVATING))
151                {
152                    if (p_cb->init_credits == p_cb->num_buff)
153                    {
154                        /* all the credits are back */
155                        nfc_cb.flags  &= ~NFC_FL_DEACTIVATING;
156                        NFC_TRACE_DEBUG2 ("deactivating NFC-DEP init_credits:%d, num_buff:%d", p_cb->init_credits, p_cb->num_buff);
157                        nfc_stop_timer(&nfc_cb.deactivate_timer);
158                        nci_snd_deactivate_cmd ((UINT8)((TIMER_PARAM_TYPE)nfc_cb.deactivate_timer.param));
159                    }
160                }
161            }
162            return NCI_STATUS_FAILED;
163        }
164    }
165
166    if (p_data)
167    {
168        /* always enqueue the data to the tx queue */
169        GKI_enqueue (&p_cb->tx_q, p_data);
170    }
171
172    /* try to send the first data packet in the tx queue  */
173    p_data = (BT_HDR *)GKI_getfirst (&p_cb->tx_q);
174
175    /* post data fragment to NCIT task as credits are available */
176    while (p_data && (p_data->len > 0) && (p_cb->num_buff > 0))
177    {
178        if (p_data->len <= buffer_size)
179        {
180            pbf         = 0;   /* last fragment */
181            ulen        = (UINT8)(p_data->len);
182            fragmented  = FALSE;
183        }
184        else
185        {
186            fragmented  = TRUE;
187            ulen        = buffer_size;
188        }
189
190        if (!fragmented)
191        {
192            /* if data packet is not fragmented, use the original buffer */
193            p         = p_data;
194            p_data    = (BT_HDR *)GKI_dequeue (&p_cb->tx_q);
195        }
196        else
197        {
198            /* the data packet is too big and need to be fragmented
199             * prepare a new GKI buffer
200             * (even the last fragment to avoid issues) */
201            if ((p = NCI_GET_CMD_BUF(ulen)) == NULL)
202                return (NCI_STATUS_BUFFER_FULL);
203            p->len    = ulen;
204            p->offset = NCI_MSG_OFFSET_SIZE + NCI_DATA_HDR_SIZE + 1;
205            pp        = (UINT8 *)(p + 1) + p->offset;
206            ps        = (UINT8 *)(p_data + 1) + p_data->offset;
207            memcpy (pp, ps, ulen);
208            /* adjust the BT_HDR on the old fragment */
209            p_data->len     -= ulen;
210            p_data->offset  += ulen;
211        }
212
213        p->event             = BT_EVT_TO_NFC_NCI;
214        p->layer_specific    = pbf;
215        p->len              += NCI_DATA_HDR_SIZE;
216        p->offset           -= NCI_DATA_HDR_SIZE;
217        pp = (UINT8 *)(p + 1) + p->offset;
218        /* build NCI Data packet header */
219        NCI_DATA_PBLD_HDR(pp, pbf, hdr0, ulen);
220
221        if (p_cb->num_buff != NFC_CONN_NO_FC)
222            p_cb->num_buff--;
223
224        /* send to HAL */
225        nfc_cb.p_hal->write(p->len, (UINT8 *)(p+1) + p->offset);
226        GKI_freebuf(p);
227
228        if (!fragmented)
229        {
230            /* check if there are more data to send */
231            p_data = (BT_HDR *)GKI_getfirst (&p_cb->tx_q);
232        }
233    }
234
235    return (NCI_STATUS_OK);
236}
237
238/*******************************************************************************
239**
240** Function         nfc_ncif_check_cmd_queue
241**
242** Description      Send NCI command to the transport
243**
244** Returns          void
245**
246*******************************************************************************/
247void nfc_ncif_check_cmd_queue (BT_HDR *p_buf)
248{
249    UINT8   *ps;
250    /* If there are commands waiting in the xmit queue, or if the controller cannot accept any more commands, */
251    /* then enqueue this command */
252    if (p_buf)
253    {
254        if ((nfc_cb.nci_cmd_xmit_q.count) || (nfc_cb.nci_cmd_window == 0))
255        {
256            GKI_enqueue (&nfc_cb.nci_cmd_xmit_q, p_buf);
257            p_buf = NULL;
258        }
259    }
260
261    /* If controller can accept another command, then send the next command */
262    if (nfc_cb.nci_cmd_window > 0)
263    {
264        /* If no command was provided, or if older commands were in the queue, then get cmd from the queue */
265        if (!p_buf)
266            p_buf = (BT_HDR *)GKI_dequeue (&nfc_cb.nci_cmd_xmit_q);
267
268        if (p_buf)
269        {
270            /* save the message header to double check the response */
271            ps   = (UINT8 *)(p_buf + 1) + p_buf->offset;
272            memcpy(nfc_cb.last_hdr, ps, NFC_SAVED_HDR_SIZE);
273            memcpy(nfc_cb.last_cmd, ps + NCI_MSG_HDR_SIZE, NFC_SAVED_CMD_SIZE);
274            if (p_buf->layer_specific == NFC_WAIT_RSP_VSC)
275            {
276                /* save the callback for NCI VSCs)  */
277                nfc_cb.p_vsc_cback = (void *)((tNFC_NCI_VS_MSG *)p_buf)->p_cback;
278            }
279
280            /* send to HAL */
281            nfc_cb.p_hal->write(p_buf->len, (UINT8 *)(p_buf+1) + p_buf->offset);
282            GKI_freebuf(p_buf);
283
284            /* Indicate command is pending */
285            nfc_cb.nci_cmd_window--;
286
287            /* start NFC command-timeout timer */
288            nfc_start_timer (&nfc_cb.nci_wait_rsp_timer, (UINT16)(NFC_TTYPE_NCI_WAIT_RSP), nfc_cb.nci_wait_rsp_tout);
289        }
290    }
291
292    if (nfc_cb.nci_cmd_window == NCI_MAX_CMD_WINDOW)
293    {
294        /* the command queue must be empty now */
295        if (nfc_cb.flags & NFC_FL_CONTROL_REQUESTED)
296        {
297            /* HAL requested control or stack needs to handle pre-discover */
298            nfc_cb.flags &= ~NFC_FL_CONTROL_REQUESTED;
299            if (nfc_cb.flags & NFC_FL_DISCOVER_PENDING)
300            {
301                if (nfc_cb.p_hal->prediscover ())
302                {
303                    /* HAL has the command window now */
304                    nfc_cb.flags         |= NFC_FL_CONTROL_GRANTED;
305                    nfc_cb.nci_cmd_window = 0;
306                }
307                else
308                {
309                    /* HAL does not need to send command,
310                     * - restore the command window and issue the discovery command now */
311                    nfc_cb.flags         &= ~NFC_FL_DISCOVER_PENDING;
312                    ps                    = (UINT8 *)nfc_cb.p_disc_pending;
313                    nci_snd_discover_cmd (*ps, (tNFC_DISCOVER_PARAMS *)(ps + 1));
314                    GKI_freebuf (nfc_cb.p_disc_pending);
315                    nfc_cb.p_disc_pending = NULL;
316                }
317            }
318            else
319            {
320                /* grant the control to HAL */
321                nfc_cb.flags         |= NFC_FL_CONTROL_GRANTED;
322                nfc_cb.nci_cmd_window = 0;
323                nfc_cb.p_hal->control_granted ();
324            }
325        }
326    }
327}
328
329
330/*******************************************************************************
331**
332** Function         nfc_ncif_send_cmd
333**
334** Description      Send NCI command to the NCIT task
335**
336** Returns          void
337**
338*******************************************************************************/
339void nfc_ncif_send_cmd (BT_HDR *p_buf)
340{
341    /* post the p_buf to NCIT task */
342    p_buf->event            = BT_EVT_TO_NFC_NCI;
343    p_buf->layer_specific   = 0;
344    nfc_ncif_check_cmd_queue (p_buf);
345}
346
347
348/*******************************************************************************
349**
350** Function         nfc_ncif_process_event
351**
352** Description      This function is called to process the data/response/notification
353**                  from NFCC
354**
355** Returns          TRUE if need to free buffer
356**
357*******************************************************************************/
358BOOLEAN nfc_ncif_process_event (BT_HDR *p_msg)
359{
360    UINT8   mt, pbf, gid, *p, *pp;
361    BOOLEAN free = TRUE;
362    UINT8   oid;
363    UINT8   *p_old, old_gid, old_oid, old_mt;
364
365    p = (UINT8 *) (p_msg + 1) + p_msg->offset;
366
367    pp = p;
368    NCI_MSG_PRS_HDR0 (pp, mt, pbf, gid);
369
370    switch (mt)
371    {
372    case NCI_MT_DATA:
373        NFC_TRACE_DEBUG0 ("NFC received data");
374        nfc_ncif_proc_data (p_msg);
375        free = FALSE;
376        break;
377
378    case NCI_MT_RSP:
379        NFC_TRACE_DEBUG1 ("NFC received rsp gid:%d", gid);
380        oid = ((*pp) & NCI_OID_MASK);
381        p_old   = nfc_cb.last_hdr;
382        NCI_MSG_PRS_HDR0(p_old, old_mt, pbf, old_gid);
383        old_oid = ((*p_old) & NCI_OID_MASK);
384        /* make sure this is the RSP we are waiting for before updating the command window */
385        if ((old_gid != gid) || (old_oid != oid))
386        {
387            NFC_TRACE_ERROR2 ("nfc_ncif_process_event unexpected rsp: gid:0x%x, oid:0x%x", gid, oid);
388            return TRUE;
389        }
390
391        switch (gid)
392        {
393        case NCI_GID_CORE:      /* 0000b NCI Core group */
394            free = nci_proc_core_rsp (p_msg);
395            break;
396        case NCI_GID_RF_MANAGE:   /* 0001b NCI Discovery group */
397            nci_proc_rf_management_rsp (p_msg);
398            break;
399#if (NFC_NFCEE_INCLUDED == TRUE)
400#if (NFC_RW_ONLY == FALSE)
401        case NCI_GID_EE_MANAGE:  /* 0x02 0010b NFCEE Discovery group */
402            nci_proc_ee_management_rsp (p_msg);
403            break;
404#endif
405#endif
406        case NCI_GID_PROP:      /* 1111b Proprietary */
407                nci_proc_prop_rsp (p_msg);
408            break;
409        default:
410            NFC_TRACE_ERROR1 ("NFC: Unknown gid:%d", gid);
411            break;
412        }
413
414        nfc_ncif_update_window(TRUE);
415        break;
416
417    case NCI_MT_NTF:
418        NFC_TRACE_DEBUG1 ("NFC received ntf gid:%d", gid);
419        switch (gid)
420        {
421        case NCI_GID_CORE:      /* 0000b NCI Core group */
422            nci_proc_core_ntf (p_msg);
423            break;
424        case NCI_GID_RF_MANAGE:   /* 0001b NCI Discovery group */
425            nci_proc_rf_management_ntf (p_msg);
426            break;
427#if (NFC_NFCEE_INCLUDED == TRUE)
428#if (NFC_RW_ONLY == FALSE)
429        case NCI_GID_EE_MANAGE:  /* 0x02 0010b NFCEE Discovery group */
430            nci_proc_ee_management_ntf (p_msg);
431            break;
432#endif
433#endif
434        case NCI_GID_PROP:      /* 1111b Proprietary */
435                nci_proc_prop_ntf (p_msg);
436            break;
437        default:
438            NFC_TRACE_ERROR1 ("NFC: Unknown gid:%d", gid);
439            break;
440        }
441        break;
442
443    default:
444        NFC_TRACE_DEBUG2 ("NFC received unknown mt:0x%x, gid:%d", mt, gid);
445    }
446
447    return (free);
448}
449
450/*******************************************************************************
451**
452** Function         nfc_ncif_rf_management_status
453**
454** Description      This function is called to report an event
455**
456** Returns          void
457**
458*******************************************************************************/
459void nfc_ncif_rf_management_status (tNFC_DISCOVER_EVT event, UINT8 status)
460{
461    tNFC_DISCOVER   evt_data;
462    if (nfc_cb.p_discv_cback)
463    {
464        evt_data.status = (tNFC_STATUS) status;
465        (*nfc_cb.p_discv_cback) (event, &evt_data);
466    }
467}
468
469/*******************************************************************************
470**
471** Function         nfc_ncif_set_config_status
472**
473** Description      This function is called to report NFC_SET_CONFIG_REVT
474**
475** Returns          void
476**
477*******************************************************************************/
478void nfc_ncif_set_config_status (UINT8 *p, UINT8 len)
479{
480    tNFC_RESPONSE   evt_data;
481    if (nfc_cb.p_resp_cback)
482    {
483        evt_data.set_config.status          = (tNFC_STATUS) *p++;
484        evt_data.set_config.num_param_id    = NFC_STATUS_OK;
485        if (evt_data.set_config.status != NFC_STATUS_OK)
486        {
487            evt_data.set_config.num_param_id    = *p++;
488            STREAM_TO_ARRAY (evt_data.set_config.param_ids, p, evt_data.set_config.num_param_id);
489        }
490
491        (*nfc_cb.p_resp_cback) (NFC_SET_CONFIG_REVT, &evt_data);
492    }
493}
494
495/*******************************************************************************
496**
497** Function         nfc_ncif_event_status
498**
499** Description      This function is called to report an event
500**
501** Returns          void
502**
503*******************************************************************************/
504void nfc_ncif_event_status (tNFC_RESPONSE_EVT event, UINT8 status)
505{
506    tNFC_RESPONSE   evt_data;
507    if (nfc_cb.p_resp_cback)
508    {
509        evt_data.status = (tNFC_STATUS) status;
510        (*nfc_cb.p_resp_cback) (event, &evt_data);
511    }
512}
513
514/*******************************************************************************
515**
516** Function         nfc_ncif_error_status
517**
518** Description      This function is called to report an error event to data cback
519**
520** Returns          void
521**
522*******************************************************************************/
523void nfc_ncif_error_status (UINT8 conn_id, UINT8 status)
524{
525    tNFC_CONN_CB * p_cb;
526    p_cb = nfc_find_conn_cb_by_conn_id (conn_id);
527    if (p_cb && p_cb->p_cback)
528    {
529        (*p_cb->p_cback) (conn_id, NFC_ERROR_CEVT, (tNFC_CONN *) &status);
530    }
531}
532
533/*******************************************************************************
534**
535** Function         nfc_ncif_proc_rf_field_ntf
536**
537** Description      This function is called to process RF field notification
538**
539** Returns          void
540**
541*******************************************************************************/
542#if (NFC_RW_ONLY == FALSE)
543void nfc_ncif_proc_rf_field_ntf (UINT8 rf_status)
544{
545    tNFC_RESPONSE   evt_data;
546    if (nfc_cb.p_resp_cback)
547    {
548        evt_data.status            = (tNFC_STATUS) NFC_STATUS_OK;
549        evt_data.rf_field.rf_field = rf_status;
550        (*nfc_cb.p_resp_cback) (NFC_RF_FIELD_REVT, &evt_data);
551    }
552}
553#endif
554
555/*******************************************************************************
556**
557** Function         nfc_ncif_proc_credits
558**
559** Description      This function is called to process data credits
560**
561** Returns          void
562**
563*******************************************************************************/
564void nfc_ncif_proc_credits(UINT8 *p, UINT16 plen)
565{
566    UINT8   num, xx;
567    tNFC_CONN_CB * p_cb;
568
569    num = *p++;
570    for (xx = 0; xx < num; xx++)
571    {
572        p_cb = nfc_find_conn_cb_by_conn_id(*p++);
573        if (p_cb && p_cb->num_buff != NFC_CONN_NO_FC)
574        {
575            p_cb->num_buff += (*p);
576#if (BT_USE_TRACES == TRUE)
577            if (p_cb->num_buff > p_cb->init_credits)
578            {
579                if (nfc_cb.nfc_state == NFC_STATE_OPEN)
580                {
581                    /* if this happens in activated state, it's very likely that our NFCC has issues */
582                    /* However, credit may be returned after deactivation */
583                    NFC_TRACE_ERROR2( "num_buff:0x%x, init_credits:0x%x", p_cb->num_buff, p_cb->init_credits);
584                }
585                p_cb->num_buff = p_cb->init_credits;
586            }
587#endif
588            /* check if there's nay data in tx q to be sent */
589            nfc_ncif_send_data (p_cb, NULL);
590        }
591        p++;
592    }
593}
594/*******************************************************************************
595**
596** Function         nfc_ncif_decode_rf_params
597**
598** Description      This function is called to process the detected technology
599**                  and mode and the associated parameters for DISCOVER_NTF and
600**                  ACTIVATE_NTF
601**
602** Returns          void
603**
604*******************************************************************************/
605UINT8 * nfc_ncif_decode_rf_params (tNFC_RF_TECH_PARAMS *p_param, UINT8 *p)
606{
607    tNFC_RF_PA_PARAMS   *p_pa;
608    UINT8               len, *p_start, u8;
609    tNFC_RF_PB_PARAMS   *p_pb;
610    tNFC_RF_LF_PARAMS   *p_lf;
611    tNFC_RF_PF_PARAMS   *p_pf;
612    tNFC_RF_PISO15693_PARAMS *p_i93;
613
614    len             = *p++;
615    p_start         = p;
616    switch (p_param->mode)
617    {
618    case NCI_DISCOVERY_TYPE_POLL_A:
619    case NCI_DISCOVERY_TYPE_POLL_A_ACTIVE:
620        p_pa        = &p_param->param.pa;
621        /*
622SENS_RES Response   2 bytes Defined in [DIGPROT] Available after Technology Detection
623NFCID1 length   1 byte  Length of NFCID1 Available after Collision Resolution
624NFCID1  4, 7, or 10 bytes   Defined in [DIGPROT]Available after Collision Resolution
625SEL_RES Response    1 byte  Defined in [DIGPROT]Available after Collision Resolution
626        */
627        STREAM_TO_ARRAY (p_pa->sens_res, p, 2);
628        p_pa->nfcid1_len     = *p++;
629        if (p_pa->nfcid1_len > NCI_NFCID1_MAX_LEN)
630            p_pa->nfcid1_len = NCI_NFCID1_MAX_LEN;
631        STREAM_TO_ARRAY (p_pa->nfcid1, p, p_pa->nfcid1_len);
632        u8                   = *p++;
633        if (u8)
634            p_pa->sel_rsp    = *p++;
635        break;
636
637    case NCI_DISCOVERY_TYPE_POLL_B:
638        /*
639SENSB_RES Response length (n)   1 byte  Length of SENSB_RES Response (Byte 2 - Byte 12 or 13)Available after Technology Detection
640SENSB_RES Response Byte 2 - Byte 12 or 13   11 or 12 bytes  Defined in [DIGPROT] Available after Technology Detection
641        */
642        p_pb                = &p_param->param.pb;
643        p_pb->sensb_res_len = *p++;
644        if (p_pb->sensb_res_len > NCI_MAX_SENSB_RES_LEN)
645            p_pb->sensb_res_len = NCI_MAX_SENSB_RES_LEN;
646        STREAM_TO_ARRAY (p_pb->sensb_res, p, p_pb->sensb_res_len);
647        memcpy (p_pb->nfcid0, p_pb->sensb_res, NFC_NFCID0_MAX_LEN);
648        break;
649
650    case NCI_DISCOVERY_TYPE_POLL_F:
651    case NCI_DISCOVERY_TYPE_POLL_F_ACTIVE:
652        /*
653Bit Rate    1 byte  1   212 kbps/2   424 kbps/0 and 3 to 255  RFU
654SENSF_RES Response length.(n) 1 byte  Length of SENSF_RES (Byte 2 - Byte 17 or 19).Available after Technology Detection
655SENSF_RES Response Byte 2 - Byte 17 or 19  n bytes Defined in [DIGPROT] Available after Technology Detection
656        */
657        p_pf                = &p_param->param.pf;
658        p_pf->bit_rate      = *p++;
659        p_pf->sensf_res_len = *p++;
660        if (p_pf->sensf_res_len > NCI_MAX_SENSF_RES_LEN)
661            p_pf->sensf_res_len = NCI_MAX_SENSF_RES_LEN;
662        STREAM_TO_ARRAY (p_pf->sensf_res, p, p_pf->sensf_res_len);
663        memcpy (p_pf->nfcid2, p_pf->sensf_res, NCI_NFCID2_LEN);
664        p_pf->mrti_check    = p_pf->sensf_res[NCI_MRTI_CHECK_INDEX];
665        p_pf->mrti_update   = p_pf->sensf_res[NCI_MRTI_UPDATE_INDEX];
666        break;
667
668    case NCI_DISCOVERY_TYPE_LISTEN_F:
669    case NCI_DISCOVERY_TYPE_LISTEN_F_ACTIVE:
670        p_lf                = &p_param->param.lf;
671        u8                  = *p++;
672        if (u8)
673        {
674            STREAM_TO_ARRAY (p_lf->nfcid2, p, NCI_NFCID2_LEN);
675        }
676        break;
677
678    case NCI_DISCOVERY_TYPE_POLL_ISO15693:
679        p_i93               = &p_param->param.pi93;
680        p_i93->flag         = *p++;
681        p_i93->dsfid        = *p++;
682        STREAM_TO_ARRAY (p_i93->uid, p, NFC_ISO15693_UID_LEN);
683        break;
684
685    case NCI_DISCOVERY_TYPE_POLL_KOVIO:
686        p_param->param.pk.uid_len = *p++;
687        STREAM_TO_ARRAY (p_param->param.pk.uid, p, NFC_KOVIO_MAX_LEN);
688        break;
689    }
690
691    return (p_start + len);
692}
693
694/*******************************************************************************
695**
696** Function         nfc_ncif_proc_discover_ntf
697**
698** Description      This function is called to process discover notification
699**
700** Returns          void
701**
702*******************************************************************************/
703void nfc_ncif_proc_discover_ntf (UINT8 *p, UINT16 plen)
704{
705    tNFC_DISCOVER   evt_data;
706
707    if (nfc_cb.p_discv_cback)
708    {
709        p                              += NCI_MSG_HDR_SIZE;
710        evt_data.status                 = NCI_STATUS_OK;
711        evt_data.result.rf_disc_id      = *p++;
712        evt_data.result.protocol        = *p++;
713
714        /* fill in tNFC_RESULT_DEVT */
715        evt_data.result.rf_tech_param.mode  = *p++;
716        p = nfc_ncif_decode_rf_params (&evt_data.result.rf_tech_param, p);
717
718        evt_data.result.more            = *p++;
719        (*nfc_cb.p_discv_cback) (NFC_RESULT_DEVT, &evt_data);
720    }
721}
722
723/*******************************************************************************
724**
725** Function         nfc_ncif_proc_activate
726**
727** Description      This function is called to process de-activate
728**                  response and notification
729**
730** Returns          void
731**
732*******************************************************************************/
733void nfc_ncif_proc_activate (UINT8 *p, UINT8 len)
734{
735    tNFC_DISCOVER   evt_data;
736    tNFC_INTF_PARAMS        *p_intf = &evt_data.activate.intf_param;
737    tNFC_INTF_PA_ISO_DEP    *p_pa_iso;
738    tNFC_INTF_LB_ISO_DEP    *p_lb_iso;
739    tNFC_INTF_PB_ISO_DEP    *p_pb_iso;
740#if (NFC_RW_ONLY == FALSE)
741    tNFC_INTF_PA_NFC_DEP    *p_pa_nfc;
742    int                     mpl_idx = 0;
743    UINT8                   gb_idx = 0, mpl;
744#endif
745    UINT8                   t0;
746    tNCI_DISCOVERY_TYPE     mode;
747    tNFC_CONN_CB * p_cb = &nfc_cb.conn_cb[NFC_RF_CONN_ID];
748    UINT8                   *pp, len_act;
749    UINT8                   buff_size, num_buff;
750
751    nfc_set_state (NFC_STATE_OPEN);
752
753    memset (p_intf, 0, sizeof (tNFC_INTF_PARAMS));
754    evt_data.activate.rf_disc_id    = *p++;
755    p_intf->type                    = *p++;
756    evt_data.activate.protocol      = *p++;
757
758    if (evt_data.activate.protocol == NCI_PROTOCOL_18092_ACTIVE)
759        evt_data.activate.protocol = NCI_PROTOCOL_NFC_DEP;
760
761    evt_data.activate.rf_tech_param.mode    = *p++;
762    buff_size                               = *p++;
763    num_buff                                = *p++;
764    if (num_buff > NCI_MAX_RF_DATA_CREDITS)
765        num_buff = NCI_MAX_RF_DATA_CREDITS;
766    /* fill in tNFC_activate_DEVT */
767    p = nfc_ncif_decode_rf_params (&evt_data.activate.rf_tech_param, p);
768
769    evt_data.activate.rf_tech_param.mode    = *p++;
770    evt_data.activate.tx_bitrate            = *p++;
771    evt_data.activate.rx_bitrate            = *p++;
772    mode         = evt_data.activate.rf_tech_param.mode;
773    len_act      = *p++;
774    NFC_TRACE_DEBUG3 ("nfc_ncif_proc_activate:%d %d, mode:0x%02x", len, len_act, mode);
775    /* just in case the interface reports activation parameters not defined in the NCI spec */
776    p_intf->intf_param.frame.param_len      = len_act;
777    if (p_intf->intf_param.frame.param_len > NFC_MAX_RAW_PARAMS)
778        p_intf->intf_param.frame.param_len = NFC_MAX_RAW_PARAMS;
779    pp = p;
780    STREAM_TO_ARRAY (p_intf->intf_param.frame.param, pp, p_intf->intf_param.frame.param_len);
781    if (evt_data.activate.intf_param.type == NCI_INTERFACE_ISO_DEP)
782    {
783        /* Make max payload of NCI aligned to max payload of ISO-DEP for better performance */
784        if (buff_size > NCI_ISO_DEP_MAX_INFO)
785            buff_size = NCI_ISO_DEP_MAX_INFO;
786
787        switch (mode)
788        {
789        case NCI_DISCOVERY_TYPE_POLL_A:
790            p_pa_iso                  = &p_intf->intf_param.pa_iso;
791            p_pa_iso->ats_res_len     = *p++;
792
793            if (p_pa_iso->ats_res_len == 0)
794                break;
795
796            if (p_pa_iso->ats_res_len > NFC_MAX_ATS_LEN)
797                p_pa_iso->ats_res_len = NFC_MAX_ATS_LEN;
798            STREAM_TO_ARRAY (p_pa_iso->ats_res, p, p_pa_iso->ats_res_len);
799            pp = &p_pa_iso->ats_res[NCI_ATS_T0_INDEX];
800            t0 = p_pa_iso->ats_res[NCI_ATS_T0_INDEX];
801            pp++;       /* T0 */
802            if (t0 & NCI_ATS_TA_MASK)
803                pp++;   /* TA */
804            if (t0 & NCI_ATS_TB_MASK)
805            {
806                /* FWI (Frame Waiting time Integer) & SPGI (Start-up Frame Guard time Integer) */
807                p_pa_iso->fwi       = (((*pp) >> 4) & 0x0F);
808                p_pa_iso->sfgi      = ((*pp) & 0x0F);
809                pp++;   /* TB */
810            }
811            if (t0 & NCI_ATS_TC_MASK)
812            {
813                p_pa_iso->nad_used  = ((*pp) & 0x01);
814                pp++;   /* TC */
815            }
816            p_pa_iso->his_byte_len  = (UINT8) (p_pa_iso->ats_res_len - (pp - p_pa_iso->ats_res));
817            memcpy (p_pa_iso->his_byte,  pp, p_pa_iso->his_byte_len);
818            break;
819
820        case NCI_DISCOVERY_TYPE_LISTEN_A:
821            p_intf->intf_param.la_iso.rats = *p++;
822            break;
823
824        case NCI_DISCOVERY_TYPE_POLL_B:
825            /* ATTRIB RSP
826            Byte 1   Byte 2 ~ 2+n-1
827            MBLI/DID Higher layer - Response
828            */
829            p_pb_iso                     = &p_intf->intf_param.pb_iso;
830            p_pb_iso->attrib_res_len     = *p++;
831
832            if (p_pb_iso->attrib_res_len == 0)
833                break;
834
835            if (p_pb_iso->attrib_res_len > NFC_MAX_ATTRIB_LEN)
836                p_pb_iso->attrib_res_len = NFC_MAX_ATTRIB_LEN;
837            STREAM_TO_ARRAY (p_pb_iso->attrib_res, p, p_pb_iso->attrib_res_len);
838            p_pb_iso->mbli = (p_pb_iso->attrib_res[0]) >> 4;
839            if (p_pb_iso->attrib_res_len > NFC_PB_ATTRIB_REQ_FIXED_BYTES)
840            {
841                p_pb_iso->hi_info_len    = p_pb_iso->attrib_res_len - NFC_PB_ATTRIB_REQ_FIXED_BYTES;
842                if (p_pb_iso->hi_info_len > NFC_MAX_GEN_BYTES_LEN)
843                    p_pb_iso->hi_info_len = NFC_MAX_GEN_BYTES_LEN;
844                memcpy (p_pb_iso->hi_info, &p_pb_iso->attrib_res[NFC_PB_ATTRIB_REQ_FIXED_BYTES], p_pb_iso->hi_info_len);
845            }
846            break;
847
848        case NCI_DISCOVERY_TYPE_LISTEN_B:
849            /* ATTRIB CMD
850            Byte 2~5 Byte 6  Byte 7  Byte 8  Byte 9  Byte 10 ~ 10+k-1
851            NFCID0   Param 1 Param 2 Param 3 Param 4 Higher layer - INF
852            */
853            p_lb_iso                     = &p_intf->intf_param.lb_iso;
854            p_lb_iso->attrib_req_len     = *p++;
855
856            if (p_lb_iso->attrib_req_len == 0)
857                break;
858
859            if (p_lb_iso->attrib_req_len > NFC_MAX_ATTRIB_LEN)
860                p_lb_iso->attrib_req_len = NFC_MAX_ATTRIB_LEN;
861            STREAM_TO_ARRAY (p_lb_iso->attrib_req, p, p_lb_iso->attrib_req_len);
862            memcpy (p_lb_iso->nfcid0, p_lb_iso->attrib_req, NFC_NFCID0_MAX_LEN);
863            if (p_lb_iso->attrib_req_len > NFC_LB_ATTRIB_REQ_FIXED_BYTES)
864            {
865                p_lb_iso->hi_info_len    = p_lb_iso->attrib_req_len - NFC_LB_ATTRIB_REQ_FIXED_BYTES;
866                if (p_lb_iso->hi_info_len > NFC_MAX_GEN_BYTES_LEN)
867                    p_lb_iso->hi_info_len = NFC_MAX_GEN_BYTES_LEN;
868                memcpy (p_lb_iso->hi_info, &p_lb_iso->attrib_req[NFC_LB_ATTRIB_REQ_FIXED_BYTES], p_lb_iso->hi_info_len);
869            }
870            break;
871        }
872
873    }
874#if (NFC_RW_ONLY == FALSE)
875    else if (evt_data.activate.intf_param.type == NCI_INTERFACE_NFC_DEP)
876    {
877        /* Make max payload of NCI aligned to max payload of NFC-DEP for better performance */
878        if (buff_size > NCI_NFC_DEP_MAX_DATA)
879            buff_size = NCI_NFC_DEP_MAX_DATA;
880
881        p_pa_nfc                  = &p_intf->intf_param.pa_nfc;
882        p_pa_nfc->atr_res_len     = *p++;
883
884        if (p_pa_nfc->atr_res_len > 0)
885        {
886            if (p_pa_nfc->atr_res_len > NFC_MAX_ATS_LEN)
887                p_pa_nfc->atr_res_len = NFC_MAX_ATS_LEN;
888            STREAM_TO_ARRAY (p_pa_nfc->atr_res, p, p_pa_nfc->atr_res_len);
889            if (  (mode == NCI_DISCOVERY_TYPE_POLL_A)
890                ||(mode == NCI_DISCOVERY_TYPE_POLL_F)
891                ||(mode == NCI_DISCOVERY_TYPE_POLL_A_ACTIVE)
892                ||(mode == NCI_DISCOVERY_TYPE_POLL_F_ACTIVE)  )
893            {
894                /* ATR_RES
895                Byte 3~12 Byte 13 Byte 14 Byte 15 Byte 16 Byte 17 Byte 18~18+n
896                NFCID3T   DIDT    BST     BRT     TO      PPT     [GT0 ... GTn] */
897                mpl_idx                 = 14;
898                gb_idx                  = NCI_P_GEN_BYTE_INDEX;
899                p_pa_nfc->waiting_time  = p_pa_nfc->atr_res[NCI_L_NFC_DEP_TO_INDEX] & 0x0F;
900            }
901            else if (  (mode == NCI_DISCOVERY_TYPE_LISTEN_A)
902                     ||(mode == NCI_DISCOVERY_TYPE_LISTEN_F)
903                     ||(mode == NCI_DISCOVERY_TYPE_LISTEN_A_ACTIVE)
904                     ||(mode == NCI_DISCOVERY_TYPE_LISTEN_F_ACTIVE)  )
905            {
906                /* ATR_REQ
907                Byte 3~12 Byte 13 Byte 14 Byte 15 Byte 16 Byte 17~17+n
908                NFCID3I   DIDI    BSI     BRI     PPI     [GI0 ... GIn] */
909                mpl_idx = 13;
910                gb_idx  = NCI_L_GEN_BYTE_INDEX;
911            }
912
913            mpl                         = ((p_pa_nfc->atr_res[mpl_idx]) >> 4) & 0x03;
914            p_pa_nfc->max_payload_size  = nfc_mpl_code_to_size[mpl];
915            if (p_pa_nfc->atr_res_len > gb_idx)
916            {
917                p_pa_nfc->gen_bytes_len = p_pa_nfc->atr_res_len - gb_idx;
918                if (p_pa_nfc->gen_bytes_len > NFC_MAX_GEN_BYTES_LEN)
919                    p_pa_nfc->gen_bytes_len = NFC_MAX_GEN_BYTES_LEN;
920                memcpy (p_pa_nfc->gen_bytes, &p_pa_nfc->atr_res[gb_idx], p_pa_nfc->gen_bytes_len);
921            }
922        }
923    }
924#endif
925    p_cb->act_protocol  = evt_data.activate.protocol;
926    p_cb->buff_size     = buff_size;
927    p_cb->num_buff      = num_buff;
928    p_cb->init_credits  = num_buff;
929
930    if (nfc_cb.p_discv_cback)
931    {
932        (*nfc_cb.p_discv_cback) (NFC_ACTIVATE_DEVT, &evt_data);
933    }
934}
935
936/*******************************************************************************
937**
938** Function         nfc_ncif_proc_deactivate
939**
940** Description      This function is called to process de-activate
941**                  response and notification
942**
943** Returns          void
944**
945*******************************************************************************/
946void nfc_ncif_proc_deactivate (UINT8 status, UINT8 deact_type, BOOLEAN is_ntf)
947{
948    tNFC_DISCOVER   evt_data;
949    tNFC_DEACTIVATE_DEVT    *p_deact;
950    tNFC_CONN_CB * p_cb = &nfc_cb.conn_cb[NFC_RF_CONN_ID];
951    void    *p_data;
952
953    nfc_set_state (NFC_STATE_IDLE);
954    p_deact             = &evt_data.deactivate;
955    p_deact->status     = status;
956    p_deact->type       = deact_type;
957    p_deact->is_ntf     = is_ntf;
958
959    while ((p_data = GKI_dequeue (&p_cb->rx_q)) != NULL)
960    {
961        GKI_freebuf (p_data);
962    }
963
964    while ((p_data = GKI_dequeue (&p_cb->tx_q)) != NULL)
965    {
966        GKI_freebuf (p_data);
967    }
968
969    if (p_cb->p_cback)
970        (*p_cb->p_cback) (NFC_RF_CONN_ID, NFC_DEACTIVATE_CEVT, (tNFC_CONN *) p_deact);
971
972    if (nfc_cb.p_discv_cback)
973    {
974        (*nfc_cb.p_discv_cback) (NFC_DEACTIVATE_DEVT, &evt_data);
975    }
976}
977/*******************************************************************************
978**
979** Function         nfc_ncif_proc_ee_action
980**
981** Description      This function is called to process NFCEE ACTION NTF
982**
983** Returns          void
984**
985*******************************************************************************/
986#if ((NFC_NFCEE_INCLUDED == TRUE) && (NFC_RW_ONLY == FALSE))
987void nfc_ncif_proc_ee_action (UINT8 *p, UINT16 plen)
988{
989    tNFC_EE_ACTION_REVT evt_data;
990    tNFC_RESPONSE_CBACK *p_cback = nfc_cb.p_resp_cback;
991    UINT8   data_len, ulen, tag, *p_data;
992    UINT8   max_len;
993
994    if (p_cback)
995    {
996        memset (&evt_data.act_data, 0, sizeof (tNFC_ACTION_DATA));
997        evt_data.status             = NFC_STATUS_OK;
998        evt_data.nfcee_id           = *p++;
999        evt_data.act_data.trigger   = *p++;
1000        data_len                    = *p++;
1001        if (plen >= 3)
1002            plen -= 3;
1003        if (data_len > plen)
1004            data_len = (UINT8) plen;
1005
1006        switch (evt_data.act_data.trigger)
1007        {
1008        case NCI_EE_TRIG_7816_SELECT:
1009            if (data_len > NFC_MAX_AID_LEN)
1010                data_len = NFC_MAX_AID_LEN;
1011            evt_data.act_data.param.aid.len_aid = data_len;
1012            STREAM_TO_ARRAY (evt_data.act_data.param.aid.aid, p, data_len);
1013            break;
1014        case NCI_EE_TRIG_RF_PROTOCOL:
1015            evt_data.act_data.param.protocol    = *p++;
1016            break;
1017        case NCI_EE_TRIG_RF_TECHNOLOGY:
1018            evt_data.act_data.param.technology  = *p++;
1019            break;
1020        case NCI_EE_TRIG_APP_INIT:
1021            while (data_len > NFC_TL_SIZE)
1022            {
1023                data_len    -= NFC_TL_SIZE;
1024                tag         = *p++;
1025                ulen        = *p++;
1026                if (ulen > data_len)
1027                    ulen = data_len;
1028                p_data      = NULL;
1029                max_len     = ulen;
1030                switch (tag)
1031                {
1032                case NCI_EE_ACT_TAG_AID:    /* AID                 */
1033                    if (max_len > NFC_MAX_AID_LEN)
1034                        max_len = NFC_MAX_AID_LEN;
1035                    evt_data.act_data.param.app_init.len_aid = max_len;
1036                    p_data = evt_data.act_data.param.app_init.aid;
1037                    break;
1038                case NCI_EE_ACT_TAG_DATA:   /* hex data for app    */
1039                    if (max_len > NFC_MAX_APP_DATA_LEN)
1040                        max_len = NFC_MAX_APP_DATA_LEN;
1041                    evt_data.act_data.param.app_init.len_data   = max_len;
1042                    p_data                                      = evt_data.act_data.param.app_init.data;
1043                    break;
1044                }
1045                if (p_data)
1046                {
1047                    STREAM_TO_ARRAY (p_data, p, max_len);
1048                }
1049                data_len -= ulen;
1050            }
1051            break;
1052        }
1053        (*p_cback) (NFC_EE_ACTION_REVT, (tNFC_RESPONSE *) &evt_data);
1054    }
1055}
1056
1057/*******************************************************************************
1058**
1059** Function         nfc_ncif_proc_ee_discover_req
1060**
1061** Description      This function is called to process NFCEE DISCOVER REQ NTF
1062**
1063** Returns          void
1064**
1065*******************************************************************************/
1066void nfc_ncif_proc_ee_discover_req (UINT8 *p, UINT16 plen)
1067{
1068    tNFC_RESPONSE_CBACK *p_cback = nfc_cb.p_resp_cback;
1069    tNFC_EE_DISCOVER_REQ_REVT   ee_disc_req;
1070    tNFC_EE_DISCOVER_INFO       *p_info;
1071    UINT8                       u8;
1072
1073    NFC_TRACE_DEBUG2 ("nfc_ncif_proc_ee_discover_req %d len:%d", *p, plen);
1074    if (p_cback)
1075    {
1076        u8  = *p;
1077        ee_disc_req.status      = NFC_STATUS_OK;
1078        ee_disc_req.num_info    = *p++;
1079        p_info                  = ee_disc_req.info;
1080        if (plen)
1081            plen--;
1082        while ((u8 > 0) && (plen >= NFC_EE_DISCOVER_ENTRY_LEN))
1083        {
1084            p_info->op  = *p++;                  /* T */
1085            if (*p != NFC_EE_DISCOVER_INFO_LEN)/* L */
1086            {
1087                NFC_TRACE_DEBUG1 ("bad entry len:%d", *p );
1088                return;
1089            }
1090            p++;
1091            /* V */
1092            p_info->nfcee_id    = *p++;
1093            p_info->tech_n_mode = *p++;
1094            p_info->protocol    = *p++;
1095            u8--;
1096            plen    -=NFC_EE_DISCOVER_ENTRY_LEN;
1097            p_info++;
1098        }
1099        (*p_cback) (NFC_EE_DISCOVER_REQ_REVT, (tNFC_RESPONSE *) &ee_disc_req);
1100    }
1101
1102}
1103
1104/*******************************************************************************
1105**
1106** Function         nfc_ncif_proc_get_routing
1107**
1108** Description      This function is called to process get routing notification
1109**
1110** Returns          void
1111**
1112*******************************************************************************/
1113void nfc_ncif_proc_get_routing (UINT8 *p, UINT8 len)
1114{
1115    tNFC_GET_ROUTING_REVT evt_data;
1116    UINT8       more, num_entries, xx, yy, *pn, tl;
1117    tNFC_STATUS status = NFC_STATUS_CONTINUE;
1118
1119    if (nfc_cb.p_resp_cback)
1120    {
1121        more        = *p++;
1122        num_entries = *p++;
1123        for (xx = 0; xx < num_entries; xx++)
1124        {
1125            if ((more == FALSE) && (xx == (num_entries - 1)))
1126                status = NFC_STATUS_OK;
1127            evt_data.status         = (tNFC_STATUS) status;
1128            evt_data.nfcee_id       = *p++;
1129            evt_data.num_tlvs       = *p++;
1130            evt_data.tlv_size       = 0;
1131            pn                      = evt_data.param_tlvs;
1132            for (yy = 0; yy < evt_data.num_tlvs; yy++)
1133            {
1134                tl                  = *(p+1);
1135                tl                 += NFC_TL_SIZE;
1136                STREAM_TO_ARRAY (pn, p, tl);
1137                evt_data.tlv_size  += tl;
1138                pn                 += tl;
1139            }
1140            (*nfc_cb.p_resp_cback) (NFC_GET_ROUTING_REVT, (tNFC_RESPONSE *) &evt_data);
1141        }
1142    }
1143}
1144#endif
1145
1146/*******************************************************************************
1147**
1148** Function         nfc_ncif_proc_conn_create_rsp
1149**
1150** Description      This function is called to process connection create
1151**                  response
1152**
1153** Returns          void
1154**
1155*******************************************************************************/
1156void nfc_ncif_proc_conn_create_rsp (UINT8 *p, UINT16 plen, UINT8 dest_type)
1157{
1158    tNFC_CONN_CB * p_cb;
1159    tNFC_STATUS    status;
1160    tNFC_CONN_CBACK *p_cback;
1161    tNFC_CONN   evt_data;
1162    UINT8           conn_id;
1163
1164    /* find the pending connection control block */
1165    p_cb                = nfc_find_conn_cb_by_conn_id (NFC_PEND_CONN_ID);
1166    if (p_cb)
1167    {
1168        p                                  += NCI_MSG_HDR_SIZE;
1169        status                              = *p++;
1170        p_cb->buff_size                     = *p++;
1171        p_cb->num_buff = p_cb->init_credits = *p++;
1172        conn_id                             = *p++;
1173        evt_data.conn_create.status         = status;
1174        evt_data.conn_create.dest_type      = dest_type;
1175        evt_data.conn_create.id             = p_cb->id;
1176        evt_data.conn_create.buff_size      = p_cb->buff_size;
1177        evt_data.conn_create.num_buffs      = p_cb->num_buff;
1178        p_cback = p_cb->p_cback;
1179        if (status == NCI_STATUS_OK)
1180        {
1181            nfc_set_conn_id (p_cb, conn_id);
1182        }
1183        else
1184        {
1185            nfc_free_conn_cb (p_cb);
1186        }
1187
1188
1189        if (p_cback)
1190            (*p_cback) (conn_id, NFC_CONN_CREATE_CEVT, &evt_data);
1191    }
1192}
1193
1194/*******************************************************************************
1195**
1196** Function         nfc_ncif_report_conn_close_evt
1197**
1198** Description      This function is called to report connection close event
1199**
1200** Returns          void
1201**
1202*******************************************************************************/
1203void nfc_ncif_report_conn_close_evt (UINT8 conn_id, tNFC_STATUS status)
1204{
1205    tNFC_CONN       evt_data;
1206    tNFC_CONN_CBACK *p_cback;
1207    tNFC_CONN_CB    *p_cb;
1208
1209    p_cb = nfc_find_conn_cb_by_conn_id (conn_id);
1210    if (p_cb)
1211    {
1212        p_cback         = p_cb->p_cback;
1213        nfc_free_conn_cb (p_cb);
1214        evt_data.status = status;
1215        if (p_cback)
1216            (*p_cback) (conn_id, NFC_CONN_CLOSE_CEVT, &evt_data);
1217    }
1218}
1219
1220/*******************************************************************************
1221**
1222** Function         nfc_ncif_proc_reset_rsp
1223**
1224** Description      This function is called to process reset response/notification
1225**
1226** Returns          void
1227**
1228*******************************************************************************/
1229void nfc_ncif_proc_reset_rsp (UINT8 *p, BOOLEAN is_ntf)
1230{
1231    UINT8 status = *p++;
1232
1233    if (is_ntf)
1234    {
1235        NFC_TRACE_ERROR1 ("reset notification!!:0x%x ", status);
1236        /* clean up, if the state is OPEN
1237         * FW does not report reset ntf right now */
1238        if (nfc_cb.nfc_state == NFC_STATE_OPEN)
1239        {
1240            /*if any conn_cb is connected, close it.
1241              if any pending outgoing packets are dropped.*/
1242            nfc_reset_all_conn_cbs ();
1243        }
1244        status = NCI_STATUS_OK;
1245    }
1246
1247    if (nfc_cb.nfc_state == NFC_STATE_RESTARTING)
1248    {
1249        nfc_reset_all_conn_cbs ();
1250    }
1251
1252    if (status == NCI_STATUS_OK)
1253    {
1254        if ((*p) != NCI_VERSION)
1255        {
1256            NFC_TRACE_ERROR2 ("NCI version mismatch!!:0x%02x != 0x%02x ", NCI_VERSION, *p);
1257            if ((*p) < NCI_VERSION_0_F)
1258            {
1259                NFC_TRACE_ERROR0 ("NFCC version is too old");
1260                nfc_enabled (NCI_STATUS_FAILED, NULL);
1261                return;
1262            }
1263        }
1264
1265        nci_snd_core_init ();
1266    }
1267    else
1268    {
1269        NFC_TRACE_ERROR0 ("Failed to reset NFCC");
1270        nfc_enabled (status, NULL);
1271    }
1272}
1273
1274/*******************************************************************************
1275**
1276** Function         nfc_ncif_proc_init_rsp
1277**
1278** Description      This function is called to process init response
1279**
1280** Returns          void
1281**
1282*******************************************************************************/
1283void nfc_ncif_proc_init_rsp (BT_HDR *p_msg)
1284{
1285    UINT8 *p, status;
1286    tNFC_CONN_CB * p_cb = &nfc_cb.conn_cb[NFC_RF_CONN_ID];
1287
1288    p        = (UINT8 *) (p_msg + 1) + p_msg->offset;
1289
1290    /* handle init params in nfc_enabled */
1291    status   = *(p + NCI_MSG_HDR_SIZE);
1292    if (status == NCI_STATUS_OK)
1293    {
1294        p_cb->id            = NFC_RF_CONN_ID;
1295        p_cb->act_protocol  = NCI_PROTOCOL_UNKNOWN;
1296
1297        nfc_cb.p_hal->core_initialized(p);
1298        nfc_cb.p_nci_init_rsp = p_msg;
1299    }
1300    else
1301    {
1302        nfc_enabled (status, NULL);
1303        GKI_freebuf (p_msg);
1304    }
1305}
1306
1307/*******************************************************************************
1308**
1309** Function         nfc_ncif_proc_get_config_rsp
1310**
1311** Description      This function is called to process get config response
1312**
1313** Returns          void
1314**
1315*******************************************************************************/
1316void nfc_ncif_proc_get_config_rsp (BT_HDR *p_evt)
1317{
1318    UINT8   *p;
1319    tNFC_RESPONSE_CBACK *p_cback = nfc_cb.p_resp_cback;
1320    tNFC_RESPONSE  evt_data;
1321
1322    p_evt->offset += NCI_MSG_HDR_SIZE;
1323    p_evt->len    -= NCI_MSG_HDR_SIZE;
1324    if (p_cback)
1325    {
1326        p                                = (UINT8 *) (p_evt + 1) + p_evt->offset;
1327        evt_data.get_config.status       = *p++;
1328        evt_data.get_config.tlv_size     = p_evt->len;
1329        evt_data.get_config.p_param_tlvs = p;
1330        (*p_cback) (NFC_GET_CONFIG_REVT, &evt_data);
1331    }
1332}
1333
1334/*******************************************************************************
1335**
1336** Function         nfc_ncif_proc_t3t_polling_ntf
1337**
1338** Description      Handle NCI_MSG_RF_T3T_POLLING NTF
1339**
1340** Returns          void
1341**
1342*******************************************************************************/
1343void nfc_ncif_proc_t3t_polling_ntf (UINT8 *p, UINT16 plen)
1344{
1345    UINT8 status;
1346    UINT8 num_responses;
1347
1348    /* Pass result to RW_T3T for processing */
1349    STREAM_TO_UINT8 (status, p);
1350    STREAM_TO_UINT8 (num_responses, p);
1351    plen-=NFC_TL_SIZE;
1352    rw_t3t_handle_nci_poll_ntf (status, num_responses, (UINT8) plen, p);
1353}
1354
1355/*******************************************************************************
1356**
1357** Function         nfc_data_event
1358**
1359** Description      Report Data event on the given connection control block
1360**
1361** Returns          void
1362**
1363*******************************************************************************/
1364void nfc_data_event (tNFC_CONN_CB * p_cb)
1365{
1366    BT_HDR      *p_evt;
1367    tNFC_DATA_CEVT data_cevt;
1368    UINT8       *p;
1369
1370    if (p_cb->p_cback)
1371    {
1372        while ((p_evt = (BT_HDR *)GKI_getfirst (&p_cb->rx_q)) != NULL)
1373        {
1374            if (p_evt->layer_specific & NFC_RAS_FRAGMENTED)
1375            {
1376                break;
1377            }
1378            p_evt = (BT_HDR *) GKI_dequeue (&p_cb->rx_q);
1379            /* report data event */
1380            p_evt->offset   += NCI_MSG_HDR_SIZE;
1381            p_evt->len      -= NCI_MSG_HDR_SIZE;
1382            if (p_evt->layer_specific)
1383                data_cevt.status = NFC_STATUS_BAD_LENGTH;
1384            else
1385                data_cevt.status = NFC_STATUS_OK;
1386            data_cevt.p_data = p_evt;
1387            /* adjust payload, if needed */
1388            if (p_cb->conn_id == NFC_RF_CONN_ID)
1389            {
1390                /* if NCI_PROTOCOL_T1T/NCI_PROTOCOL_T2T/NCI_PROTOCOL_T3T, the status byte needs to be removed
1391                 */
1392                if ((p_cb->act_protocol >= NCI_PROTOCOL_T1T) && (p_cb->act_protocol <= NCI_PROTOCOL_T3T))
1393                {
1394                    p_evt->len--;
1395                    p                = (UINT8 *) (p_evt + 1);
1396                    data_cevt.status = *(p + p_evt->offset + p_evt->len);
1397                }
1398            }
1399            (*p_cb->p_cback) (p_cb->conn_id, NFC_DATA_CEVT, (tNFC_CONN *) &data_cevt);
1400            p_evt = NULL;
1401        }
1402    }
1403}
1404
1405/*******************************************************************************
1406**
1407** Function         nfc_ncif_proc_data
1408**
1409** Description      Find the connection control block associated with the data
1410**                  packet. Assemble the data packet, if needed.
1411**                  Report the Data event.
1412**
1413** Returns          void
1414**
1415*******************************************************************************/
1416void nfc_ncif_proc_data (BT_HDR *p_msg)
1417{
1418    UINT8   *pp, cid;
1419    tNFC_CONN_CB * p_cb;
1420    UINT8   pbf;
1421    BT_HDR  *p_last;
1422    UINT8   *ps, *pd;
1423    UINT16  size;
1424    BT_HDR  *p_max = NULL;
1425    UINT16  len;
1426    UINT16  error_mask = 0;
1427
1428    pp   = (UINT8 *) (p_msg+1) + p_msg->offset;
1429    NFC_TRACE_DEBUG3 ("nfc_ncif_proc_data 0x%02x%02x%02x", pp[0], pp[1], pp[2]);
1430    NCI_DATA_PRS_HDR (pp, pbf, cid, len);
1431    p_cb = nfc_find_conn_cb_by_conn_id (cid);
1432    if (p_cb && (p_msg->len >= NCI_DATA_HDR_SIZE))
1433    {
1434        NFC_TRACE_DEBUG1 ("nfc_ncif_proc_data len:%d", len);
1435        if (len > 0)
1436        {
1437            p_msg->layer_specific       = 0;
1438            if (pbf)
1439                p_msg->layer_specific   = NFC_RAS_FRAGMENTED;
1440            p_last = (BT_HDR *)GKI_getlast (&p_cb->rx_q);
1441            if (p_last && (p_last->layer_specific & NFC_RAS_FRAGMENTED))
1442            {
1443                /* last data buffer is not last fragment, append this new packet to the last */
1444                size = GKI_get_buf_size(p_last);
1445                if (size < (BT_HDR_SIZE + p_last->len + p_last->offset + len))
1446                {
1447                    /* the current size of p_last is not big enough to hold the new fragment, p_msg */
1448                    if (size != GKI_MAX_BUF_SIZE)
1449                    {
1450                        /* try the biggest GKI pool */
1451                        p_max = (BT_HDR *)GKI_getpoolbuf (GKI_MAX_BUF_SIZE_POOL_ID);
1452                        if (p_max)
1453                        {
1454                            /* copy the content of last buffer to the new buffer */
1455                            memcpy(p_max, p_last, BT_HDR_SIZE);
1456                            pd  = (UINT8 *)(p_max + 1) + p_max->offset;
1457                            ps  = (UINT8 *)(p_last + 1) + p_last->offset;
1458                            memcpy(pd, ps, p_last->len);
1459
1460                            /* place the new buffer in the queue instead */
1461                            GKI_remove_from_queue (&p_cb->rx_q, p_last);
1462                            GKI_freebuf (p_last);
1463                            GKI_enqueue (&p_cb->rx_q, p_max);
1464                            p_last  = p_max;
1465                        }
1466                    }
1467                    if (p_max == NULL)
1468                    {
1469                        p_last->layer_specific  |= NFC_RAS_TOO_BIG;
1470                        NFC_TRACE_ERROR1 ("nci_reassemble_msg buffer overrun(%d)!!", len);
1471                    }
1472                }
1473
1474                ps   = (UINT8 *)(p_msg + 1) + p_msg->offset + NCI_MSG_HDR_SIZE;
1475                len  = p_msg->len - NCI_MSG_HDR_SIZE;
1476                if ((p_last->layer_specific & NFC_RAS_TOO_BIG) == 0)
1477                {
1478                    pd   = (UINT8 *)(p_last + 1) + p_last->offset + p_last->len;
1479                    memcpy(pd, ps, len);
1480                    p_last->len  += len;
1481                    /* do not need to update pbf and len in NCI header.
1482                     * They are stripped off at NFC_DATA_CEVT and len may exceed 255 */
1483                    NFC_TRACE_DEBUG1 ("nfc_ncif_proc_data len:%d", p_last->len);
1484                }
1485
1486                error_mask              = (p_last->layer_specific & NFC_RAS_TOO_BIG);
1487                p_last->layer_specific  = (p_msg->layer_specific | error_mask);
1488                GKI_freebuf (p_msg);
1489#ifdef DISP_NCI
1490                if ((p_last->layer_specific & NFC_RAS_FRAGMENTED) == 0)
1491                {
1492                    /* this packet was reassembled. display the complete packet */
1493                    DISP_NCI ((UINT8 *)(p_last + 1) + p_last->offset, p_last->len, TRUE);
1494                }
1495#endif
1496            }
1497            else
1498            {
1499                /* enqueue the new buffer to the rx queue */
1500                GKI_enqueue (&p_cb->rx_q, p_msg);
1501            }
1502            nfc_data_event (p_cb);
1503            return;
1504        }
1505        /* else an empty data packet*/
1506    }
1507    GKI_freebuf (p_msg);
1508}
1509
1510#endif /* NFC_INCLUDED == TRUE*/
1511