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