blob: 6d6607d15bbe3956b6d264a03f847a3ca8675c01 [file] [log] [blame]
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001/******************************************************************************
2 *
Evan Chue9629ba2014-01-31 11:18:47 -05003 * Copyright (C) 1999-2014 Broadcom Corporation
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08004 *
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 ******************************************************************************/
Andre Eisenbach8a4edf62017-11-20 14:51:11 -080026#include <android-base/stringprintf.h>
27#include <base/logging.h>
Ruchi Kandoia6610b52018-10-17 16:14:08 -070028#include <log/log.h>
Ruchi Kandoif67b3342017-05-17 11:50:47 -070029#include <metricslogger/metrics_logger.h>
Andre Eisenbach8a4edf62017-11-20 14:51:11 -080030
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -080031#include "nfc_target.h"
32
Ruchi Kandoic0cd1ab2017-04-17 17:23:56 -070033#include "include/debug_nfcsnoop.h"
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -080034#include "nci_defs.h"
35#include "nci_hmsgs.h"
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -080036#include "nfc_api.h"
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -080037#include "nfc_int.h"
38#include "rw_api.h"
39#include "rw_int.h"
Andre Eisenbach8a4edf62017-11-20 14:51:11 -080040
41using android::base::StringPrintf;
42
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -080043#if (NFC_RW_ONLY == FALSE)
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -080044static const uint8_t nfc_mpl_code_to_size[] = {64, 128, 192, 254};
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -080045
46#endif /* NFC_RW_ONLY */
Love Khannad7852c92017-06-02 19:55:05 +053047#if (APPL_DTA_MODE == TRUE)
48// Global Structure varibale for FW Version
49static tNFC_FW_VERSION nfc_fw_version;
50#endif
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -080051#define NFC_PB_ATTRIB_REQ_FIXED_BYTES 1
52#define NFC_LB_ATTRIB_REQ_FIXED_BYTES 8
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -080053
Love Khannad7852c92017-06-02 19:55:05 +053054extern unsigned char appl_dta_mode_flag;
Andre Eisenbach8a4edf62017-11-20 14:51:11 -080055extern bool nfc_debug_enabled;
56
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -080057/*******************************************************************************
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**
Ruchi Kandoi552f2b72017-01-28 16:22:55 -080065*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -080066void 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) {
Ruchi Kandoi6767aec2017-09-26 09:46:26 -070070 LOG(ERROR) << StringPrintf("nfc_ncif_update_window: Unexpected call");
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -080071 }
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -080072 return;
73 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -080074
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -080075 /* Stop command-pending timer */
76 nfc_stop_timer(&nfc_cb.nci_wait_rsp_timer);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -080077
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -080078 nfc_cb.p_vsc_cback = NULL;
79 nfc_cb.nci_cmd_window++;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -080080
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -080081 /* Check if there were any commands waiting to be sent */
82 nfc_ncif_check_cmd_queue(NULL);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -080083}
84
85/*******************************************************************************
86**
87** Function nfc_ncif_cmd_timeout
88**
89** Description Handle a command timeout
90**
91** Returns void
92**
93*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -080094void nfc_ncif_cmd_timeout(void) {
Ruchi Kandoi6767aec2017-09-26 09:46:26 -070095 LOG(ERROR) << StringPrintf("nfc_ncif_cmd_timeout");
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -080096
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -080097 /* 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);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800100
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800101 /* 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 }
Martijn Coenen1c9ec362014-06-04 12:51:49 -0700105
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800106 /* XXX maco since this failure is unrecoverable, abort the process */
107 abort();
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800108}
109
110/*******************************************************************************
111**
112** Function nfc_wait_2_deactivate_timeout
113**
114** Description Handle a command timeout
115**
116** Returns void
117**
118*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800119void nfc_wait_2_deactivate_timeout(void) {
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700120 LOG(ERROR) << StringPrintf("nfc_wait_2_deactivate_timeout");
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800121 nfc_cb.flags &= ~NFC_FL_DEACTIVATING;
122 nci_snd_deactivate_cmd((uint8_t)nfc_cb.deactivate_timer.param);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800123}
124
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800125/*******************************************************************************
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*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800136uint8_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;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800145
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700146 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);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800149 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;
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700157 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800158 "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 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800163 }
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800164 }
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;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800186 }
187
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800188 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) */
Ruchi Kandoi0c515ae2017-01-30 17:48:41 -0800196 p = NCI_GET_CMD_BUF(ulen);
197 if (p == NULL) return (NCI_STATUS_BUFFER_FULL);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800198 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;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800208 }
209
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800210 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);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800217
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800218 if (p_cb->num_buff != NFC_CONN_NO_FC) p_cb->num_buff--;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800219
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800220 /* send to HAL */
221 HAL_WRITE(p);
Ruchi Kandoic0cd1ab2017-04-17 17:23:56 -0700222 nfcsnoop_capture(p, false);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800223
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800224 if (!fragmented) {
225 /* check if there are more data to send */
226 p_data = (NFC_HDR*)GKI_getfirst(&p_cb->tx_q);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800227 }
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800228 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800229
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800230 return (NCI_STATUS_OK);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800231}
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*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800242void 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;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800251 }
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800252 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800253
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800254 /* 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);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800259
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800260 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;
Love Khannad7852c92017-06-02 19:55:05 +0530268 } 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;
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800272 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800273
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800274 /* Indicate command is pending */
275 nfc_cb.nci_cmd_window--;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800276
Love Khannad7852c92017-06-02 19:55:05 +0530277 /* send to HAL */
278 HAL_WRITE(p_buf);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800279 /* 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);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800283 }
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800284 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800285
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800286 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;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800304 }
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800305 } 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 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800312 }
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800313 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800314}
315
Love Khannad7852c92017-06-02 19:55:05 +0530316#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*******************************************************************************/
326tNFC_FW_VERSION nfc_ncif_getFWVersion() { return nfc_fw_version; }
327#endif
328
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800329/*******************************************************************************
330**
331** Function nfc_ncif_send_cmd
332**
333** Description Send NCI command to the NCIT task
334**
335** Returns void
336**
337*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800338void 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;
Ruchi Kandoic0cd1ab2017-04-17 17:23:56 -0700342 nfcsnoop_capture(p_buf, false);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800343 nfc_ncif_check_cmd_queue(p_buf);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800344}
345
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800346/*******************************************************************************
347**
348** Function nfc_ncif_process_event
349**
Ruchi Kandoi552f2b72017-01-28 16:22:55 -0800350** Description This function is called to process the
351** data/response/notification from NFCC
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800352**
353** Returns TRUE if need to free buffer
354**
355*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800356bool 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;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800361
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800362 p = (uint8_t*)(p_msg + 1) + p_msg->offset;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800363
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800364 pp = p;
365 NCI_MSG_PRS_HDR0(pp, mt, pbf, gid);
Love Khannad7852c92017-06-02 19:55:05 +0530366 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 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800373
Ruchi Kandoic0cd1ab2017-04-17 17:23:56 -0700374 nfcsnoop_capture(p_msg, true);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800375 switch (mt) {
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800376 case NCI_MT_DATA:
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700377 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("NFC received data");
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800378 nfc_ncif_proc_data(p_msg);
379 free = false;
380 break;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800381
382 case NCI_MT_RSP:
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700383 DLOG_IF(INFO, nfc_debug_enabled)
384 << StringPrintf("NFC received rsp gid:%d", gid);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800385 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)) {
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700392 LOG(ERROR) << StringPrintf(
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800393 "nfc_ncif_process_event unexpected rsp: gid:0x%x, oid:0x%x", gid,
394 oid);
395 return true;
396 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800397
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800398 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;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800405#if (NFC_NFCEE_INCLUDED == TRUE)
406#if (NFC_RW_ONLY == FALSE)
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800407 case NCI_GID_EE_MANAGE: /* 0x02 0010b NFCEE Discovery group */
408 nci_proc_ee_management_rsp(p_msg);
409 break;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800410#endif
411#endif
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800412 case NCI_GID_PROP: /* 1111b Proprietary */
413 nci_proc_prop_rsp(p_msg);
414 break;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800415 default:
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700416 LOG(ERROR) << StringPrintf("NFC: Unknown gid:%d", gid);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800417 break;
418 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800419
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800420 nfc_ncif_update_window();
421 break;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800422
423 case NCI_MT_NTF:
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700424 DLOG_IF(INFO, nfc_debug_enabled)
425 << StringPrintf("NFC received ntf gid:%d", gid);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800426 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;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800433#if (NFC_NFCEE_INCLUDED == TRUE)
434#if (NFC_RW_ONLY == FALSE)
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800435 case NCI_GID_EE_MANAGE: /* 0x02 0010b NFCEE Discovery group */
436 nci_proc_ee_management_ntf(p_msg);
437 break;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800438#endif
439#endif
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800440 case NCI_GID_PROP: /* 1111b Proprietary */
441 nci_proc_prop_ntf(p_msg);
442 break;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800443 default:
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700444 LOG(ERROR) << StringPrintf("NFC: Unknown gid:%d", gid);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800445 break;
446 }
447 break;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800448
449 default:
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700450 DLOG_IF(INFO, nfc_debug_enabled)
451 << StringPrintf("NFC received unknown mt:0x%x, gid:%d", mt, gid);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800452 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800453
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800454 return (free);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800455}
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*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800466void 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 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800472}
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*******************************************************************************/
George Chang8d07fd22018-10-24 20:50:02 +0800483void nfc_ncif_set_config_status(uint8_t* p, uint8_t len) {
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800484 tNFC_RESPONSE evt_data;
485 if (nfc_cb.p_resp_cback) {
George Chang8d07fd22018-10-24 20:50:02 +0800486 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;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800492 }
George Chang8d07fd22018-10-24 20:50:02 +0800493 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 }
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800510 (*nfc_cb.p_resp_cback)(NFC_SET_CONFIG_REVT, &evt_data);
511 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800512}
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*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800523void nfc_ncif_event_status(tNFC_RESPONSE_EVT event, uint8_t status) {
524 tNFC_RESPONSE evt_data;
Ruchi Kandoif67b3342017-05-17 11:50:47 -0700525 if (event == NFC_NFCC_TIMEOUT_REVT && status == NFC_STATUS_HW_TIMEOUT)
526 android::metricslogger::LogCounter("nfc_hw_timeout_error", 1);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800527 if (nfc_cb.p_resp_cback) {
528 evt_data.status = (tNFC_STATUS)status;
529 (*nfc_cb.p_resp_cback)(event, &evt_data);
530 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800531}
532
533/*******************************************************************************
534**
535** Function nfc_ncif_error_status
536**
Ruchi Kandoi552f2b72017-01-28 16:22:55 -0800537** Description This function is called to report an error event to data
538** cback
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800539**
540** Returns void
541**
542*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800543void nfc_ncif_error_status(uint8_t conn_id, uint8_t status) {
Myles Watson1361d522017-09-26 13:39:54 -0700544 tNFC_CONN_CB* p_cb = nfc_find_conn_cb_by_conn_id(conn_id);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800545 if (p_cb && p_cb->p_cback) {
Myles Watson1361d522017-09-26 13:39:54 -0700546 tNFC_CONN nfc_conn;
547 nfc_conn.status = status;
548 (*p_cb->p_cback)(conn_id, NFC_ERROR_CEVT, &nfc_conn);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800549 }
Ruchi Kandoif67b3342017-05-17 11:50:47 -0700550 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);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800566}
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)
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800578void 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 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800585}
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*******************************************************************************/
Ruchi Kandoi9ba6d242017-10-13 16:07:01 -0700597void nfc_ncif_proc_credits(uint8_t* p, __attribute__((unused)) uint16_t plen) {
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800598 uint8_t num, xx;
599 tNFC_CONN_CB* p_cb;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800600
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800601 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);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800606#if (BT_USE_TRACES == TRUE)
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800607 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 */
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700612 LOG(ERROR) << StringPrintf("num_buff:0x%x, init_credits:0x%x",
613 p_cb->num_buff, p_cb->init_credits);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800614 }
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800615 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);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800620 }
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800621 p++;
622 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800623}
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*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800635uint8_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;
Love Khanna78eae6d2017-04-04 16:23:47 +0530642 tNFC_RF_ACM_P_PARAMS* acm_p;
643 uint8_t mpl_idx = 0;
644 uint8_t gb_idx = 0, mpl;
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800645 len = *p++;
646 p_start = p;
647 memset(&p_param->param, 0, sizeof(tNFC_RF_TECH_PARAMU));
Jizhou Liaoacf1ec02015-06-30 10:25:41 -0700648
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800649 if (NCI_DISCOVERY_TYPE_POLL_A == p_param->mode ||
Love Khanna78eae6d2017-04-04 16:23:47 +0530650 (NCI_DISCOVERY_TYPE_POLL_A_ACTIVE == p_param->mode &&
651 NFC_GetNCIVersion() != NCI_VERSION_2_0)) {
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800652 p_pa = &p_param->param.pa;
653 /*
654SENS_RES Response 2 bytes Defined in [DIGPROT] Available after Technology
655Detection
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800656NFCID1 length 1 byte Length of NFCID1 Available after Collision Resolution
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800657NFCID1 4, 7, or 10 bytes Defined in [DIGPROT]Available after Collision
658Resolution
659SEL_RES Response 1 byte Defined in [DIGPROT]Available after Collision
660Resolution
661HRx Length 1 Octets Length of HRx Parameters collected from the response to
662the T1T RID command.
663HRx 0 or 2 Octets If present, the first byte SHALL contain HR0 and the second
664byte 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) */
Jizhou Liaoacf1ec02015-06-30 10:25:41 -0700677 {
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800678 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 }
Jizhou Liaoacf1ec02015-06-30 10:25:41 -0700683 }
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800684 } else if (NCI_DISCOVERY_TYPE_POLL_B == p_param->mode) {
685 /*
686SENSB_RES Response length (n) 1 byte Length of SENSB_RES Response (Byte 2 -
687Byte 12 or 13)Available after Technology Detection
688SENSB_RES Response Byte 2 - Byte 12 or 13 11 or 12 bytes Defined in [DIGPROT]
689Available 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 ||
Love Khanna78eae6d2017-04-04 16:23:47 +0530698 (NCI_DISCOVERY_TYPE_POLL_F_ACTIVE == p_param->mode &&
699 NFC_GetNCIVersion() != NCI_VERSION_2_0)) {
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800700 /*
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800701Bit Rate 1 byte 1 212 kbps/2 424 kbps/0 and 3 to 255 RFU
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800702SENSF_RES Response length.(n) 1 byte Length of SENSF_RES (Byte 2 - Byte 17 or
70319).Available after Technology Detection
704SENSF_RES Response Byte 2 - Byte 17 or 19 n bytes Defined in [DIGPROT]
705Available 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 ||
Love Khanna78eae6d2017-04-04 16:23:47 +0530717 (NCI_DISCOVERY_TYPE_LISTEN_F_ACTIVE == p_param->mode &&
718 NFC_GetNCIVersion() != NCI_VERSION_2_0)) {
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800719 p_lf = &p_param->param.lf;
720 u8 = *p++;
721 if (u8) {
722 STREAM_TO_ARRAY(p_lf->nfcid2, p, NCI_NFCID2_LEN);
Jizhou Liaoacf1ec02015-06-30 10:25:41 -0700723 }
Love Khanna57a3dfa2017-03-28 20:03:38 +0530724 } else if (NCI_DISCOVERY_TYPE_POLL_V == p_param->mode) {
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800725 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) {
Jizhou Liaoafe16e22017-09-05 12:41:28 -0700730 p_param->param.pk.uid_len = len;
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800731 if (p_param->param.pk.uid_len > NFC_KOVIO_MAX_LEN) {
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700732 LOG(ERROR) << StringPrintf("Kovio UID len:0x%x exceeds max(0x%x)",
733 p_param->param.pk.uid_len, NFC_KOVIO_MAX_LEN);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800734 p_param->param.pk.uid_len = NFC_KOVIO_MAX_LEN;
Jizhou Liaoacf1ec02015-06-30 10:25:41 -0700735 }
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800736 STREAM_TO_ARRAY(p_param->param.pk.uid, p, p_param->param.pk.uid_len);
Love Khanna78eae6d2017-04-04 16:23:47 +0530737 } 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 }
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800780 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800781
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800782 return (p_start + len);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800783}
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*******************************************************************************/
Ruchi Kandoi9ba6d242017-10-13 16:07:01 -0700794void nfc_ncif_proc_discover_ntf(uint8_t* p,
795 __attribute__((unused)) uint16_t plen) {
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800796 tNFC_DISCOVER evt_data;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800797
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800798 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++;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800803
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800804 /* 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);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800807
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800808 evt_data.result.more = *p++;
809 (*nfc_cb.p_discv_cback)(NFC_RESULT_DEVT, &evt_data);
810 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800811}
812
813/*******************************************************************************
814**
Love Khannad09d4bf2017-04-06 14:31:48 +0530815** 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*******************************************************************************/
823void 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**
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800829** 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*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800837void 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;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800843#if (NFC_RW_ONLY == FALSE)
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800844 tNFC_INTF_PA_NFC_DEP* p_pa_nfc;
845 int mpl_idx = 0;
846 uint8_t gb_idx = 0, mpl;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800847#endif
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800848 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;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800854
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800855 nfc_set_state(NFC_STATE_OPEN);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800856
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800857 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++;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800861
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800862 if (evt_data.activate.protocol == NCI_PROTOCOL_18092_ACTIVE)
863 evt_data.activate.protocol = NCI_PROTOCOL_NFC_DEP;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800864
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800865 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);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800870
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800871 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++;
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700876 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
877 "nfc_ncif_proc_activate:%d %d, mode:0x%02x", len, len_act, mode);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800878 /* 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;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800890
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800891 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++;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800895
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800896 if (p_pa_iso->ats_res_len == 0) break;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800897
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800898 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 */
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800911 }
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800912 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;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800922
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800923 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;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800976 }
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800977
978 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800979#if (NFC_RW_ONLY == FALSE)
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800980 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;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800984
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800985 p_pa_nfc = &p_intf->intf_param.pa_nfc;
986 p_pa_nfc->atr_res_len = *p++;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800987
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800988 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) ||
Love Khanna78eae6d2017-04-04 16:23:47 +0530994 ((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)) {
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800999 /* 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) ||
Love Khanna78eae6d2017-04-04 16:23:47 +05301008 (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)) {
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001013 /* 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 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001019
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001020 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 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001029 }
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001030 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001031#endif
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001032 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++;
Martijn Coenen5c65c3a2013-03-27 13:23:36 -07001039 }
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001040 }
Martijn Coenen5c65c3a2013-03-27 13:23:36 -07001041
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001042 p_cb->act_protocol = evt_data.activate.protocol;
Love Khanna67a3e862017-06-02 18:45:37 +05301043 p_cb->act_interface = evt_data.activate.intf_param.type;
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001044 p_cb->buff_size = buff_size;
1045 p_cb->num_buff = num_buff;
1046 p_cb->init_credits = num_buff;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001047
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001048 if (nfc_cb.p_discv_cback) {
1049 (*nfc_cb.p_discv_cback)(NFC_ACTIVATE_DEVT, &evt_data);
1050 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001051}
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*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001063void nfc_ncif_proc_deactivate(uint8_t status, uint8_t deact_type, bool is_ntf) {
1064 tNFC_DISCOVER evt_data;
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001065 tNFC_CONN_CB* p_cb = &nfc_cb.conn_cb[NFC_RF_CONN_ID];
1066 void* p_data;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001067
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001068 nfc_set_state(NFC_STATE_IDLE);
Myles Watson1361d522017-09-26 13:39:54 -07001069 evt_data.deactivate.status = status;
1070 evt_data.deactivate.type = deact_type;
1071 evt_data.deactivate.is_ntf = is_ntf;
Love Khanna13c47602017-04-04 17:36:20 +05301072 if (NFC_GetNCIVersion() == NCI_VERSION_2_0) {
Myles Watson1361d522017-09-26 13:39:54 -07001073 evt_data.deactivate.reason = nfc_cb.deact_reason;
Love Khanna13c47602017-04-04 17:36:20 +05301074 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001075
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001076 while ((p_data = GKI_dequeue(&p_cb->rx_q)) != NULL) {
1077 GKI_freebuf(p_data);
1078 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001079
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001080 while ((p_data = GKI_dequeue(&p_cb->tx_q)) != NULL) {
1081 GKI_freebuf(p_data);
1082 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001083
Myles Watson1361d522017-09-26 13:39:54 -07001084 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 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001089
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001090 if (nfc_cb.p_discv_cback) {
1091 (*nfc_cb.p_discv_cback)(NFC_DEACTIVATE_DEVT, &evt_data);
1092 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001093}
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*******************************************************************************/
Ruchi Kandoi303fec12016-12-14 13:22:38 -08001103#if (NFC_NFCEE_INCLUDED == TRUE && NFC_RW_ONLY == FALSE)
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001104void 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;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001109
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001110 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;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001118
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001119 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;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001156 }
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001157 break;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001158 }
Myles Watson1361d522017-09-26 13:39:54 -07001159 tNFC_RESPONSE nfc_response;
1160 nfc_response.ee_action = evt_data;
1161 (*p_cback)(NFC_EE_ACTION_REVT, &nfc_response);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001162 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001163}
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*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001174void 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;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001179
Ruchi Kandoi6767aec2017-09-26 09:46:26 -07001180 DLOG_IF(INFO, nfc_debug_enabled)
1181 << StringPrintf("nfc_ncif_proc_ee_discover_req %d len:%d", *p, plen);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001182 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 {
Ruchi Kandoi6767aec2017-09-26 09:46:26 -07001192 DLOG_IF(INFO, nfc_debug_enabled)
1193 << StringPrintf("bad entry len:%d", *p);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001194 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++;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001204 }
Myles Watson1361d522017-09-26 13:39:54 -07001205 tNFC_RESPONSE nfc_response;
1206 nfc_response.ee_discover_req = ee_disc_req;
1207 (*p_cback)(NFC_EE_DISCOVER_REQ_REVT, &nfc_response);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001208 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001209}
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*******************************************************************************/
Ruchi Kandoi9ba6d242017-10-13 16:07:01 -07001220void nfc_ncif_proc_get_routing(uint8_t* p,
1221 __attribute__((unused)) uint8_t len) {
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001222 tNFC_GET_ROUTING_REVT evt_data;
1223 uint8_t more, num_entries, xx, yy, *pn, tl;
1224 tNFC_STATUS status = NFC_STATUS_CONTINUE;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001225
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001226 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;
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001239 evt_data.tlv_size += tl;
Ruchi Kandoia6610b52018-10-17 16:14:08 -07001240 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);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001246 pn += tl;
1247 }
Myles Watson1361d522017-09-26 13:39:54 -07001248 tNFC_RESPONSE nfc_response;
1249 nfc_response.get_routing = evt_data;
1250 (*nfc_cb.p_resp_cback)(NFC_GET_ROUTING_REVT, &nfc_response);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001251 }
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001252 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001253}
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*******************************************************************************/
Ruchi Kandoi9ba6d242017-10-13 16:07:01 -07001266void nfc_ncif_proc_conn_create_rsp(uint8_t* p,
1267 __attribute__((unused)) uint16_t plen,
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001268 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;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001274
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001275 /* 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);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001293 }
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001294
1295 if (p_cback) (*p_cback)(conn_id, NFC_CONN_CREATE_CEVT, &evt_data);
1296 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001297}
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*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001308void 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;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001312
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001313 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 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001320}
1321
1322/*******************************************************************************
1323**
1324** Function nfc_ncif_proc_reset_rsp
1325**
Ruchi Kandoi552f2b72017-01-28 16:22:55 -08001326** Description This function is called to process reset
1327** response/notification
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001328**
1329** Returns void
1330**
1331*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001332void nfc_ncif_proc_reset_rsp(uint8_t* p, bool is_ntf) {
Ruchi Kandoi3f64b002017-03-27 16:52:28 -07001333 uint8_t* p_len = p - 1;
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001334 uint8_t status = *p++;
Ruchi Kandoi3f64b002017-03-27 16:52:28 -07001335 uint8_t wait_for_ntf = FALSE;
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001336 if (is_ntf) {
Ruchi Kandoi6767aec2017-09-26 09:46:26 -07001337 LOG(ERROR) << StringPrintf("reset notification!!:0x%x ", status);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001338 /* clean up, if the state is OPEN
1339 * FW does not report reset ntf right now */
Ruchi Kandoi3f64b002017-03-27 16:52:28 -07001340 if (status == NCI2_0_RESET_TRIGGER_TYPE_CORE_RESET_CMD_RECEIVED ||
1341 status == NCI2_0_RESET_TRIGGER_TYPE_POWERED_ON) {
Ruchi Kandoi6767aec2017-09-26 09:46:26 -07001342 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);
Ruchi Kandoi3f64b002017-03-27 16:52:28 -07001345 nfc_stop_timer(&nfc_cb.nci_wait_rsp_timer);
1346 p++;
1347 STREAM_TO_UINT8(nfc_cb.nci_version, p);
Ruchi Kandoi6767aec2017-09-26 09:46:26 -07001348 DLOG_IF(INFO, nfc_debug_enabled)
1349 << StringPrintf(" CORE_RESET_NTF nci_version%x", nfc_cb.nci_version);
Ruchi Kandoi3f64b002017-03-27 16:52:28 -07001350 status = NCI_STATUS_OK;
1351 } else {
1352 /* CORE_RESET_NTF received error case , trigger recovery*/
Ruchi Kandoi6767aec2017-09-26 09:46:26 -07001353 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);
Ruchi Kandoi3f64b002017-03-27 16:52:28 -07001356 nfc_ncif_cmd_timeout();
1357 status = NCI_STATUS_FAILED;
1358 }
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001359 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();
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001363 }
Ruchi Kandoi3f64b002017-03-27 16:52:28 -07001364 } else {
Ruchi Kandoi6767aec2017-09-26 09:46:26 -07001365 DLOG_IF(INFO, nfc_debug_enabled)
1366 << StringPrintf("CORE_RESET_RSP len :0x%x ", *p_len);
Ruchi Kandoi3f64b002017-03-27 16:52:28 -07001367 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 }
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001372 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001373
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001374 if (nfc_cb.flags & (NFC_FL_RESTARTING | NFC_FL_POWER_CYCLE_NFCC)) {
1375 nfc_reset_all_conn_cbs();
1376 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001377
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001378 if (status == NCI_STATUS_OK) {
Ruchi Kandoi3f64b002017-03-27 16:52:28 -07001379 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 }
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001391 } else {
Ruchi Kandoi6767aec2017-09-26 09:46:26 -07001392 LOG(ERROR) << StringPrintf("Failed to reset NFCC");
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001393 nfc_enabled(status, NULL);
1394 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001395}
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*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001406void 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];
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001409
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001410 p = (uint8_t*)(p_msg + 1) + p_msg->offset;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001411
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001412 /* handle init params in nfc_enabled */
1413 status = *(p + NCI_MSG_HDR_SIZE);
1414 if (status == NCI_STATUS_OK) {
Ruchi Kandoi3f64b002017-03-27 16:52:28 -07001415 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;
Love Khanna1d062772018-04-10 21:10:02 +05301419 // 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);
Ruchi Kandoi3f64b002017-03-27 16:52:28 -07001423 p_cb->act_protocol = NCI_PROTOCOL_UNKNOWN;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001424
Ruchi Kandoi3f64b002017-03-27 16:52:28 -07001425 nfc_set_state(NFC_STATE_W4_POST_INIT_CPLT);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001426
Ruchi Kandoi3f64b002017-03-27 16:52:28 -07001427 nfc_cb.p_nci_init_rsp = p_msg;
Ruchi Kandoif54a5912017-11-27 13:17:43 -08001428 nfc_cb.p_hal->core_initialized(p_msg->len, p);
Ruchi Kandoi3f64b002017-03-27 16:52:28 -07001429 }
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001430 } else {
Ruchi Kandoi3f64b002017-03-27 16:52:28 -07001431 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 }
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001438 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001439}
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*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001450void 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;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001454
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001455 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 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001464}
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*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001475void nfc_ncif_proc_t3t_polling_ntf(uint8_t* p, uint16_t plen) {
1476 uint8_t status;
1477 uint8_t num_responses;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001478
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001479 /* 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);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001484}
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*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001495void nfc_data_event(tNFC_CONN_CB* p_cb) {
1496 NFC_HDR* p_evt;
1497 tNFC_DATA_CEVT data_cevt;
1498 uint8_t* p;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001499
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001500 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 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001512 }
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001513 }
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);
Love Khanna67a3e862017-06-02 18:45:37 +05301537 data_cevt.status = *(p + p_evt->offset + p_evt->len);
Love Khannafebfb872017-03-07 16:37:42 +05301538 if ((NFC_GetNCIVersion() == NCI_VERSION_2_0) &&
1539 (p_cb->act_protocol == NCI_PROTOCOL_T2T) &&
Love Khanna67a3e862017-06-02 18:45:37 +05301540 (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))) {
Ruchi Kandoi6767aec2017-09-26 09:46:26 -07001544 DLOG_IF(INFO, nfc_debug_enabled)
1545 << StringPrintf("%s: T2T tag data xchange", __func__);
Love Khanna67a3e862017-06-02 18:45:37 +05301546 data_cevt.status = NFC_STATUS_OK;
1547 }
Love Khannafebfb872017-03-07 16:37:42 +05301548 }
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001549 }
Love Khanna57a3dfa2017-03-28 20:03:38 +05301550 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 }
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001556 }
Myles Watson1361d522017-09-26 13:39:54 -07001557 tNFC_CONN nfc_conn;
1558 nfc_conn.data = data_cevt;
1559 (*p_cb->p_cback)(p_cb->conn_id, NFC_DATA_CEVT, &nfc_conn);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001560 p_evt = NULL;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001561 }
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001562 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001563}
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*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001576void 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;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001585
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001586 pp = (uint8_t*)(p_msg + 1) + p_msg->offset;
Ruchi Kandoi6767aec2017-09-26 09:46:26 -07001587 DLOG_IF(INFO, nfc_debug_enabled)
1588 << StringPrintf("nfc_ncif_proc_data 0x%02x%02x%02x", pp[0], pp[1], pp[2]);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001589 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)) {
Ruchi Kandoi6767aec2017-09-26 09:46:26 -07001592 DLOG_IF(INFO, nfc_debug_enabled)
1593 << StringPrintf("nfc_ncif_proc_data len:%d", len);
Evan Chua24be4f2013-11-13 15:30:16 -05001594
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001595 p_msg->layer_specific = 0;
Love Khanna8f776632018-04-13 22:02:23 +05301596 if (pbf) {
1597 NFC_SetReassemblyFlag(true);
1598 p_msg->layer_specific = NFC_RAS_FRAGMENTED;
1599 }
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001600 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);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001617
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001618 /* 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 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001632
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001633 ps = (uint8_t*)(p_msg + 1) + p_msg->offset + NCI_MSG_HDR_SIZE;
1634 len = p_msg->len - NCI_MSG_HDR_SIZE;
Evan Chua24be4f2013-11-13 15:30:16 -05001635
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001636 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 */
Ruchi Kandoi6767aec2017-09-26 09:46:26 -07001642 DLOG_IF(INFO, nfc_debug_enabled)
1643 << StringPrintf("nfc_ncif_proc_data len:%d", p_last->len);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001644 p_last->layer_specific = p_msg->layer_specific;
1645 GKI_freebuf(p_msg);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001646#ifdef DISP_NCI
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001647 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 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001651#endif
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001652 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);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001670 }
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -08001671 return;
1672 }
1673 GKI_freebuf(p_msg);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001674}
Love Khannad7852c92017-06-02 19:55:05 +05301675
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*******************************************************************************/
1686bool nfc_ncif_proc_proprietary_rsp(uint8_t mt, uint8_t gid, uint8_t oid) {
1687 bool stat = FALSE;
Ruchi Kandoi6767aec2017-09-26 09:46:26 -07001688 DLOG_IF(INFO, nfc_debug_enabled)
1689 << StringPrintf("%s: mt=%u, gid=%u, oid=%u", __func__, mt, gid, oid);
Love Khannad7852c92017-06-02 19:55:05 +05301690
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 }
Ruchi Kandoi6767aec2017-09-26 09:46:26 -07001721 DLOG_IF(INFO, nfc_debug_enabled)
1722 << StringPrintf("%s: exit status=%u", __func__, stat);
Love Khannad7852c92017-06-02 19:55:05 +05301723 return stat;
1724}
Love Khanna81e4f812017-06-02 17:50:19 +05301725
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*******************************************************************************/
1734void nfc_mode_set_ntf_timeout() {
Ruchi Kandoi6767aec2017-09-26 09:46:26 -07001735 LOG(ERROR) << StringPrintf("%s", __func__);
Myles Watson1361d522017-09-26 13:39:54 -07001736 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;
Love Khanna81e4f812017-06-02 17:50:19 +05301740
1741 tNFC_RESPONSE_CBACK* p_cback = nfc_cb.p_resp_cback;
1742 tNFC_RESPONSE_EVT event = NFC_NFCEE_MODE_SET_REVT;
Myles Watson1361d522017-09-26 13:39:54 -07001743 if (p_cback) (*p_cback)(event, &nfc_response);
Love Khanna81e4f812017-06-02 17:50:19 +05301744}