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