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