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