1 /******************************************************************************
2 *
3 * Copyright (C) 2010-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 the implementation for Type 3 tag in Reader/Writer
22 * mode.
23 *
24 ******************************************************************************/
25 #include <string.h>
26
27 #include <android-base/stringprintf.h>
28 #include <base/logging.h>
29 #include <log/log.h>
30
31 #include "nfc_target.h"
32
33 #include "bt_types.h"
34 #include "nci_hmsgs.h"
35 #include "nfc_api.h"
36 #include "nfc_int.h"
37 #include "rw_api.h"
38 #include "rw_int.h"
39
40 using android::base::StringPrintf;
41
42 extern bool nfc_debug_enabled;
43
44 /* Definitions for constructing t3t command messages */
45 #define RW_T3T_FL_PADDING 0x01 /* Padding needed for last NDEF block */
46 /* Maximum number of NDEF blocks updates that can fit into one command (when all
47 * block-numbers are < 256) */
48 #define RW_T3T_MAX_NDEF_BLOCKS_PER_UPDATE_1_BYTE_FORMAT (13)
49 /* Maximum number of NDEF blocks updates that can fit into one command (when all
50 * block-numbers are >= 256) */
51 #define RW_T3T_MAX_NDEF_BLOCKS_PER_UPDATE_2_BYTE_FORMAT (12)
52
53 /* Definitions for SENSF_RES */
54 /* Offset of RD in SENSF_RES from NCI_POLL NTF (includes 1 byte SENSF_RES
55 * length) */
56 #define RW_T3T_SENSF_RES_RD_OFFSET 17
57 #define RW_T3T_SENSF_RES_RD_LEN 2 /* Size of RD in SENSF_RES */
58
59 /* Timeout definitions for commands */
60 #define RW_T3T_POLL_CMD_TIMEOUT_TICKS \
61 ((RW_T3T_TOUT_RESP * 2 * QUICK_TIMER_TICKS_PER_SEC) / 1000)
62 #define RW_T3T_DEFAULT_CMD_TIMEOUT_TICKS \
63 ((RW_T3T_TOUT_RESP * QUICK_TIMER_TICKS_PER_SEC) / 1000)
64 #define RW_T3T_RAW_FRAME_CMD_TIMEOUT_TICKS \
65 (RW_T3T_DEFAULT_CMD_TIMEOUT_TICKS * 4)
66 #define RW_T3T_MIN_TIMEOUT_TICKS 10
67
68 /* Macro to extract major version from NDEF version byte */
69 #define T3T_GET_MAJOR_VERSION(ver) ((ver) >> 4)
70
71 /* Enumeration of API commands */
72 enum {
73 RW_T3T_CMD_DETECT_NDEF,
74 RW_T3T_CMD_CHECK_NDEF,
75 RW_T3T_CMD_UPDATE_NDEF,
76 RW_T3T_CMD_CHECK,
77 RW_T3T_CMD_UPDATE,
78 RW_T3T_CMD_SEND_RAW_FRAME,
79 RW_T3T_CMD_GET_SYSTEM_CODES,
80 RW_T3T_CMD_FORMAT,
81 RW_T3T_CMD_SET_READ_ONLY_SOFT,
82 RW_T3T_CMD_SET_READ_ONLY_HARD,
83
84 RW_T3T_CMD_MAX
85 };
86
87 /* RW_CBACK events corresponding to API comands */
88 const uint8_t rw_t3t_api_res_evt[RW_T3T_CMD_MAX] = {
89 RW_T3T_NDEF_DETECT_EVT, /* RW_T3T_CMD_DETECT_NDEF */
90 RW_T3T_CHECK_CPLT_EVT, /* RW_T3T_CMD_CHECK_NDEF */
91 RW_T3T_UPDATE_CPLT_EVT, /* RW_T3T_CMD_UPDATE_NDEF */
92 RW_T3T_CHECK_CPLT_EVT, /* RW_T3T_CMD_CHECK */
93 RW_T3T_UPDATE_CPLT_EVT, /* RW_T3T_CMD_UPDATE */
94 RW_T3T_RAW_FRAME_EVT, /* RW_T3T_CMD_SEND_RAW_FRAME */
95 RW_T3T_GET_SYSTEM_CODES_EVT, /* RW_T3T_CMD_GET_SYSTEM_CODES */
96 RW_T3T_FORMAT_CPLT_EVT, /* RW_T3T_CMD_FORMAT */
97 RW_T3T_SET_READ_ONLY_CPLT_EVT /* RW_T3T_CMD_SET_READ_ONLY */
98 };
99
100 /* States */
101 enum {
102 RW_T3T_STATE_NOT_ACTIVATED,
103 RW_T3T_STATE_IDLE,
104 RW_T3T_STATE_COMMAND_PENDING
105 };
106
107 /* Sub-states */
108 enum {
109 /* Sub states for formatting Felica-Lite */
110 RW_T3T_FMT_SST_POLL_FELICA_LITE, /* Waiting for POLL Felica-Lite response (for
111 formatting) */
112 RW_T3T_FMT_SST_CHECK_MC_BLK, /* Waiting for Felica-Lite MC (MemoryControl)
113 block-read to complete */
114 RW_T3T_FMT_SST_UPDATE_MC_BLK, /* Waiting for Felica-Lite MC (MemoryControl)
115 block-write to complete */
116 RW_T3T_FMT_SST_UPDATE_NDEF_ATTRIB, /* Waiting for NDEF attribute block-write
117 to complete */
118
119 /* Sub states for setting Felica-Lite read only */
120 RW_T3T_SRO_SST_POLL_FELICA_LITE, /* Waiting for POLL Felica-Lite response (for
121 setting read only) */
122 RW_T3T_SRO_SST_UPDATE_NDEF_ATTRIB, /* Waiting for NDEF attribute block-write
123 to complete */
124 RW_T3T_SRO_SST_CHECK_MC_BLK, /* Waiting for Felica-Lite MC (MemoryControl)
125 block-read to complete */
126 RW_T3T_SRO_SST_UPDATE_MC_BLK /* Waiting for Felica-Lite MC (MemoryControl)
127 block-write to complete */
128 };
129
130 static std::string rw_t3t_cmd_str(uint8_t cmd_id);
131 static std::string rw_t3t_state_str(uint8_t state_id);
132
133 /* Local static functions */
134 static void rw_t3t_update_ndef_flag(uint8_t* p_flag);
135 static tNFC_STATUS rw_t3t_unselect();
136 static NFC_HDR* rw_t3t_get_cmd_buf(void);
137 static tNFC_STATUS rw_t3t_send_to_lower(NFC_HDR* p_msg);
138 static void rw_t3t_handle_get_system_codes_cplt(void);
139 static void rw_t3t_handle_get_sc_poll_rsp(tRW_T3T_CB* p_cb, uint8_t nci_status,
140 uint8_t num_responses,
141 uint8_t sensf_res_buf_size,
142 uint8_t* p_sensf_res_buf);
143 static void rw_t3t_handle_ndef_detect_poll_rsp(tRW_T3T_CB* p_cb,
144 uint8_t nci_status,
145 uint8_t num_responses);
146 static void rw_t3t_handle_fmt_poll_rsp(tRW_T3T_CB* p_cb, uint8_t nci_status,
147 uint8_t num_responses);
148 static void rw_t3t_handle_sro_poll_rsp(tRW_T3T_CB* p_cb, uint8_t nci_status,
149 uint8_t num_responses);
150
151 /* Default NDEF attribute information block (used when formatting Felica-Lite
152 * tags) */
153 /* NBr (max block reads per cmd)*/
154 #define RW_T3T_DEFAULT_FELICALITE_NBR 4
155 /* NBw (max block write per cmd)*/
156 #define RW_T3T_DEFAULT_FELICALITE_NBW 1
157 #define RW_T3T_DEFAULT_FELICALITE_NMAXB (T3T_FELICALITE_NMAXB)
158 #define RW_T3T_DEFAULT_FELICALITE_ATTRIB_INFO_CHECKSUM \
159 ((T3T_MSG_NDEF_VERSION + RW_T3T_DEFAULT_FELICALITE_NBR + \
160 RW_T3T_DEFAULT_FELICALITE_NBW + (RW_T3T_DEFAULT_FELICALITE_NMAXB >> 8) + \
161 (RW_T3T_DEFAULT_FELICALITE_NMAXB & 0xFF) + T3T_MSG_NDEF_WRITEF_OFF + \
162 T3T_MSG_NDEF_RWFLAG_RW) & \
163 0xFFFF)
164
165 const uint8_t rw_t3t_default_attrib_info[T3T_MSG_BLOCKSIZE] = {
166 T3T_MSG_NDEF_VERSION, /* Ver */
167 RW_T3T_DEFAULT_FELICALITE_NBR, /* NBr (max block reads per cmd)*/
168 RW_T3T_DEFAULT_FELICALITE_NBW, /* NBw (max block write per cmd)*/
169 (RW_T3T_DEFAULT_FELICALITE_NMAXB >> 8), /* Nmaxb (max size in blocks) */
170 (RW_T3T_DEFAULT_FELICALITE_NMAXB & 0xFF), /* Nmaxb (max size in blocks) */
171 0, 0, 0, 0, /* Unused */
172 T3T_MSG_NDEF_WRITEF_OFF, /* WriteF */
173 T3T_MSG_NDEF_RWFLAG_RW, /* RW Flag */
174 0, 0, 0, /* Ln (current size in bytes) */
175
176 (RW_T3T_DEFAULT_FELICALITE_ATTRIB_INFO_CHECKSUM >>
177 8), /* checksum (high-byte) */
178 (RW_T3T_DEFAULT_FELICALITE_ATTRIB_INFO_CHECKSUM &
179 0xFF) /* checksum (low-byte) */
180
181 };
182
183 /* This is (T/t3t * 4^E) , E is the index of the array. The unit is .0001 ms */
184 static const uint32_t rw_t3t_mrti_base[] = {302, 1208, 4832, 19328};
185
186 /*******************************************************************************
187 **
188 ** Function rw_t3t_check_timeout
189 **
190 ** Description The timeout value is a + b * number_blocks)
191 **
192 ** Returns timeout value in ticks
193 **
194 *******************************************************************************/
rw_t3t_check_timeout(uint16_t num_blocks)195 static uint32_t rw_t3t_check_timeout(uint16_t num_blocks) {
196 tRW_T3T_CB* p_cb = &rw_cb.tcb.t3t;
197 uint32_t timeout;
198 uint32_t extra;
199
200 timeout = (p_cb->check_tout_a + num_blocks * p_cb->check_tout_b) *
201 QUICK_TIMER_TICKS_PER_SEC / 1000000;
202 /* allow some extra time for driver */
203 extra = (timeout / 10) + RW_T3T_MIN_TIMEOUT_TICKS;
204 timeout += extra;
205
206 return timeout;
207 }
208
209 /*******************************************************************************
210 **
211 ** Function rw_t3t_update_timeout
212 **
213 ** Description The timeout value is a + b * number_blocks)
214 **
215 ** Returns timeout value in ticks
216 **
217 *******************************************************************************/
rw_t3t_update_timeout(uint16_t num_blocks)218 static uint32_t rw_t3t_update_timeout(uint16_t num_blocks) {
219 tRW_T3T_CB* p_cb = &rw_cb.tcb.t3t;
220 uint32_t timeout;
221 uint32_t extra;
222
223 timeout = (p_cb->update_tout_a + num_blocks * p_cb->update_tout_b) *
224 QUICK_TIMER_TICKS_PER_SEC / 1000000;
225 /* allow some extra time for driver */
226 extra = (timeout / 10) + RW_T3T_MIN_TIMEOUT_TICKS;
227 timeout += extra;
228
229 return timeout;
230 }
231 /*******************************************************************************
232 **
233 ** Function rw_t3t_process_error
234 **
235 ** Description Process error (timeout or CRC error)
236 **
237 ** Returns none
238 **
239 *******************************************************************************/
rw_t3t_process_error(tNFC_STATUS status)240 void rw_t3t_process_error(tNFC_STATUS status) {
241 tRW_T3T_CB* p_cb = &rw_cb.tcb.t3t;
242 uint8_t evt;
243 tRW_DATA evt_data;
244 NFC_HDR* p_cmd_buf;
245
246 if (p_cb->rw_state == RW_T3T_STATE_COMMAND_PENDING) {
247 if (p_cb->cur_cmd == RW_T3T_CMD_GET_SYSTEM_CODES) {
248 /* For GetSystemCode: tag did not respond to requested POLL */
249 rw_t3t_handle_get_system_codes_cplt();
250 return;
251 }
252 /* Retry sending command if retry-count < max */
253 else if (rw_cb.cur_retry < RW_MAX_RETRIES) {
254 /* retry sending the command */
255 rw_cb.cur_retry++;
256
257 DLOG_IF(INFO, nfc_debug_enabled)
258 << StringPrintf("T3T retransmission attempt %i of %i",
259 rw_cb.cur_retry, RW_MAX_RETRIES);
260
261 /* allocate a new buffer for message */
262 p_cmd_buf = rw_t3t_get_cmd_buf();
263 if (p_cmd_buf != nullptr) {
264 memcpy(p_cmd_buf, p_cb->p_cur_cmd_buf, sizeof(NFC_HDR) +
265 p_cb->p_cur_cmd_buf->offset +
266 p_cb->p_cur_cmd_buf->len);
267
268 if (rw_t3t_send_to_lower(p_cmd_buf) == NFC_STATUS_OK) {
269 /* Start timer for waiting for response */
270 nfc_start_quick_timer(&p_cb->timer, NFC_TTYPE_RW_T3T_RESPONSE,
271 p_cb->cur_tout);
272 return;
273 } else {
274 /* failure - could not send buffer */
275 GKI_freebuf(p_cmd_buf);
276 }
277 }
278 } else {
279 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
280 "T3T maximum retransmission attempts reached (%i)", RW_MAX_RETRIES);
281 }
282
283 #if (RW_STATS_INCLUDED == TRUE)
284 /* update failure count */
285 rw_main_update_fail_stats();
286 #endif /* RW_STATS_INCLUDED */
287
288 p_cb->rw_state = RW_T3T_STATE_IDLE;
289
290 /* Notify app of result (if there was a pending command) */
291 if (p_cb->cur_cmd < RW_T3T_CMD_MAX) {
292 /* If doing presence check, use status=NFC_STATUS_FAILED, otherwise
293 * NFC_STATUS_TIMEOUT */
294 evt_data.status = status;
295 evt = rw_t3t_api_res_evt[p_cb->cur_cmd];
296
297 /* Set additional flags for RW_T3T_NDEF_DETECT_EVT */
298 if (evt == RW_T3T_NDEF_DETECT_EVT) {
299 evt_data.ndef.flags = RW_NDEF_FL_UNKNOWN;
300 rw_t3t_update_ndef_flag(&evt_data.ndef.flags);
301 }
302
303 (*(rw_cb.p_cback))(evt, &evt_data);
304 }
305 } else {
306 evt_data.status = status;
307 (*(rw_cb.p_cback))(RW_T3T_INTF_ERROR_EVT, &evt_data);
308 }
309 }
310
311 /*******************************************************************************
312 **
313 ** Function rw_t3t_start_poll_timer
314 **
315 ** Description Start the timer for T3T POLL Command
316 **
317 ** Returns none
318 **
319 *******************************************************************************/
rw_t3t_start_poll_timer(tRW_T3T_CB * p_cb)320 void rw_t3t_start_poll_timer(tRW_T3T_CB* p_cb) {
321 nfc_start_quick_timer(&p_cb->poll_timer, NFC_TTYPE_RW_T3T_RESPONSE,
322 RW_T3T_POLL_CMD_TIMEOUT_TICKS);
323 }
324
325 /*******************************************************************************
326 **
327 ** Function rw_t3t_handle_nci_poll_ntf
328 **
329 ** Description Handle NCI_T3T_POLLING_NTF
330 **
331 ** Returns none
332 **
333 *******************************************************************************/
rw_t3t_handle_nci_poll_ntf(uint8_t nci_status,uint8_t num_responses,uint8_t sensf_res_buf_size,uint8_t * p_sensf_res_buf)334 void rw_t3t_handle_nci_poll_ntf(uint8_t nci_status, uint8_t num_responses,
335 uint8_t sensf_res_buf_size,
336 uint8_t* p_sensf_res_buf) {
337 tRW_DATA evt_data;
338 tRW_T3T_CB* p_cb = &rw_cb.tcb.t3t;
339
340 /* stop timer for poll response */
341 nfc_stop_quick_timer(&p_cb->poll_timer);
342
343 /* Stop t3t timer (if started) */
344 if (p_cb->flags & RW_T3T_FL_W4_PRESENCE_CHECK_POLL_RSP) {
345 p_cb->flags &= ~RW_T3T_FL_W4_PRESENCE_CHECK_POLL_RSP;
346 evt_data.status = nci_status;
347 p_cb->rw_state = RW_T3T_STATE_IDLE;
348 (*(rw_cb.p_cback))(RW_T3T_PRESENCE_CHECK_EVT, &evt_data);
349 } else if (p_cb->flags & RW_T3T_FL_W4_GET_SC_POLL_RSP) {
350 /* Handle POLL ntf in response to get system codes */
351 p_cb->flags &= ~RW_T3T_FL_W4_GET_SC_POLL_RSP;
352 rw_t3t_handle_get_sc_poll_rsp(p_cb, nci_status, num_responses,
353 sensf_res_buf_size, p_sensf_res_buf);
354 } else if (p_cb->flags & RW_T3T_FL_W4_FMT_FELICA_LITE_POLL_RSP) {
355 /* Handle POLL ntf in response to get system codes */
356 p_cb->flags &= ~RW_T3T_FL_W4_FMT_FELICA_LITE_POLL_RSP;
357 rw_t3t_handle_fmt_poll_rsp(p_cb, nci_status, num_responses);
358 } else if (p_cb->flags & RW_T3T_FL_W4_SRO_FELICA_LITE_POLL_RSP) {
359 /* Handle POLL ntf in response to get system codes */
360 p_cb->flags &= ~RW_T3T_FL_W4_SRO_FELICA_LITE_POLL_RSP;
361 rw_t3t_handle_sro_poll_rsp(p_cb, nci_status, num_responses);
362 } else if (p_cb->flags & RW_T3T_FL_W4_NDEF_DETECT_POLL_RSP) {
363 /* Handle POLL ntf in response to ndef detection */
364 p_cb->flags &= ~RW_T3T_FL_W4_NDEF_DETECT_POLL_RSP;
365 rw_t3t_handle_ndef_detect_poll_rsp(p_cb, nci_status, num_responses);
366 } else {
367 /* Handle POLL ntf in response to RW_T3tPoll */
368 evt_data.t3t_poll.status = nci_status;
369 if (evt_data.t3t_poll.status == NCI_STATUS_OK) {
370 evt_data.t3t_poll.rc = p_cb->cur_poll_rc;
371 evt_data.t3t_poll.response_num = num_responses;
372 evt_data.t3t_poll.response_bufsize = sensf_res_buf_size;
373 evt_data.t3t_poll.response_buf = p_sensf_res_buf;
374 }
375
376 p_cb->rw_state = RW_T3T_STATE_IDLE;
377 (*(rw_cb.p_cback))(RW_T3T_POLL_EVT, &evt_data);
378 }
379 }
380
381 /*******************************************************************************
382 **
383 ** Function rw_t3t_handle_get_system_codes_cplt
384 **
385 ** Description Notify upper layer of system codes
386 **
387 ** Returns none
388 **
389 *******************************************************************************/
rw_t3t_handle_get_system_codes_cplt(void)390 void rw_t3t_handle_get_system_codes_cplt(void) {
391 tRW_T3T_CB* p_cb = &rw_cb.tcb.t3t;
392 tRW_DATA evt_data;
393 uint8_t i;
394
395 evt_data.t3t_sc.status = NFC_STATUS_OK;
396 evt_data.t3t_sc.num_system_codes = p_cb->num_system_codes;
397 evt_data.t3t_sc.p_system_codes = p_cb->system_codes;
398
399 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
400 "number of systems: %i", evt_data.t3t_sc.num_system_codes);
401 for (i = 0; i < evt_data.t3t_sc.num_system_codes; i++) {
402 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
403 "system %i: %04X", i, evt_data.t3t_sc.p_system_codes[i]);
404 }
405
406 p_cb->rw_state = RW_T3T_STATE_IDLE;
407 (*(rw_cb.p_cback))(RW_T3T_GET_SYSTEM_CODES_EVT, &evt_data);
408 }
409
410 /*******************************************************************************
411 **
412 ** Function rw_t3t_format_cplt
413 **
414 ** Description Notify upper layer of format complete
415 **
416 ** Returns none
417 **
418 *******************************************************************************/
rw_t3t_format_cplt(tNFC_STATUS status)419 void rw_t3t_format_cplt(tNFC_STATUS status) {
420 tRW_T3T_CB* p_cb = &rw_cb.tcb.t3t;
421 tRW_DATA evt_data;
422
423 p_cb->rw_state = RW_T3T_STATE_IDLE;
424
425 /* Update ndef info */
426 p_cb->ndef_attrib.status = status;
427 if (status == NFC_STATUS_OK) {
428 p_cb->ndef_attrib.version = T3T_MSG_NDEF_VERSION;
429 p_cb->ndef_attrib.nbr = RW_T3T_DEFAULT_FELICALITE_NBR;
430 p_cb->ndef_attrib.nbw = RW_T3T_DEFAULT_FELICALITE_NBW;
431 p_cb->ndef_attrib.nmaxb = RW_T3T_DEFAULT_FELICALITE_NMAXB;
432 p_cb->ndef_attrib.writef = T3T_MSG_NDEF_WRITEF_OFF;
433 p_cb->ndef_attrib.rwflag = T3T_MSG_NDEF_RWFLAG_RW;
434 p_cb->ndef_attrib.ln = 0;
435 }
436
437 /* Notify upper layer of format complete */
438 evt_data.status = status;
439 (*(rw_cb.p_cback))(RW_T3T_FORMAT_CPLT_EVT, &evt_data);
440 }
441
442 /*******************************************************************************
443 **
444 ** Function rw_t3t_set_readonly_cplt
445 **
446 ** Description Notify upper layer of set read only complete
447 **
448 ** Returns none
449 **
450 *******************************************************************************/
rw_t3t_set_readonly_cplt(tNFC_STATUS status)451 void rw_t3t_set_readonly_cplt(tNFC_STATUS status) {
452 tRW_T3T_CB* p_cb = &rw_cb.tcb.t3t;
453 tRW_DATA evt_data;
454
455 p_cb->rw_state = RW_T3T_STATE_IDLE;
456
457 /* Notify upper layer of format complete */
458 evt_data.status = status;
459 (*(rw_cb.p_cback))(RW_T3T_SET_READ_ONLY_CPLT_EVT, &evt_data);
460 }
461
462 /*******************************************************************************
463 **
464 ** Function rw_t3t_process_timeout
465 **
466 ** Description Process timeout
467 **
468 ** Returns none
469 **
470 *******************************************************************************/
rw_t3t_process_timeout(TIMER_LIST_ENT * p_tle)471 void rw_t3t_process_timeout(TIMER_LIST_ENT* p_tle) {
472 tRW_T3T_CB* p_cb = &rw_cb.tcb.t3t;
473 tRW_DATA evt_data;
474
475 /* Check which timer timed out */
476 if (p_tle == &p_cb->timer) {
477 /* UPDATE/CHECK response timeout */
478 LOG(ERROR) << StringPrintf("T3T timeout. state=%s cur_cmd=0x%02X (%s)",
479 rw_t3t_state_str(rw_cb.tcb.t3t.rw_state).c_str(),
480 rw_cb.tcb.t3t.cur_cmd,
481 rw_t3t_cmd_str(rw_cb.tcb.t3t.cur_cmd).c_str());
482
483 rw_t3t_process_error(NFC_STATUS_TIMEOUT);
484 } else {
485 LOG(ERROR) << StringPrintf("T3T POLL timeout.");
486
487 /* POLL response timeout */
488 if (p_cb->flags & RW_T3T_FL_W4_PRESENCE_CHECK_POLL_RSP) {
489 /* POLL timeout for presence check */
490 p_cb->flags &= ~RW_T3T_FL_W4_PRESENCE_CHECK_POLL_RSP;
491 evt_data.status = NFC_STATUS_FAILED;
492 p_cb->rw_state = RW_T3T_STATE_IDLE;
493 (*(rw_cb.p_cback))(RW_T3T_PRESENCE_CHECK_EVT, &evt_data);
494 } else if (p_cb->flags & RW_T3T_FL_W4_GET_SC_POLL_RSP) {
495 /* POLL timeout for getting system codes */
496 p_cb->flags &= ~RW_T3T_FL_W4_GET_SC_POLL_RSP;
497 rw_t3t_handle_get_system_codes_cplt();
498 } else if (p_cb->flags & RW_T3T_FL_W4_FMT_FELICA_LITE_POLL_RSP) {
499 /* POLL timeout for formatting Felica Lite */
500 p_cb->flags &= ~RW_T3T_FL_W4_FMT_FELICA_LITE_POLL_RSP;
501 LOG(ERROR) << StringPrintf("Felica-Lite tag not detected");
502 rw_t3t_format_cplt(NFC_STATUS_FAILED);
503 } else if (p_cb->flags & RW_T3T_FL_W4_SRO_FELICA_LITE_POLL_RSP) {
504 /* POLL timeout for configuring Felica Lite read only */
505 p_cb->flags &= ~RW_T3T_FL_W4_SRO_FELICA_LITE_POLL_RSP;
506 LOG(ERROR) << StringPrintf("Felica-Lite tag not detected");
507 rw_t3t_set_readonly_cplt(NFC_STATUS_FAILED);
508 } else if (p_cb->flags & RW_T3T_FL_W4_NDEF_DETECT_POLL_RSP) {
509 /* POLL timeout for ndef detection */
510 p_cb->flags &= ~RW_T3T_FL_W4_NDEF_DETECT_POLL_RSP;
511 rw_t3t_handle_ndef_detect_poll_rsp(p_cb, NFC_STATUS_TIMEOUT, 0);
512 } else {
513 /* Timeout waiting for response for RW_T3tPoll */
514 evt_data.t3t_poll.status = NFC_STATUS_FAILED;
515 p_cb->rw_state = RW_T3T_STATE_IDLE;
516 (*(rw_cb.p_cback))(RW_T3T_POLL_EVT, &evt_data);
517 }
518 }
519 }
520
521 /*******************************************************************************
522 **
523 ** Function rw_t3t_process_frame_error
524 **
525 ** Description Process frame crc error
526 **
527 ** Returns none
528 **
529 *******************************************************************************/
rw_t3t_process_frame_error(void)530 void rw_t3t_process_frame_error(void) {
531 LOG(ERROR) << StringPrintf("T3T frame error. state=%s cur_cmd=0x%02X (%s)",
532 rw_t3t_state_str(rw_cb.tcb.t3t.rw_state).c_str(),
533 rw_cb.tcb.t3t.cur_cmd,
534 rw_t3t_cmd_str(rw_cb.tcb.t3t.cur_cmd).c_str());
535
536 #if (RW_STATS_INCLUDED == TRUE)
537 /* Update stats */
538 rw_main_update_crc_error_stats();
539 #endif /* RW_STATS_INCLUDED */
540
541 /* Process the error */
542 rw_t3t_process_error(NFC_STATUS_MSG_CORRUPTED);
543 }
544
545 /*******************************************************************************
546 **
547 ** Function rw_t3t_send_to_lower
548 **
549 ** Description Send command to lower layer
550 **
551 ** Returns status of the send
552 **
553 *******************************************************************************/
rw_t3t_send_to_lower(NFC_HDR * p_msg)554 tNFC_STATUS rw_t3t_send_to_lower(NFC_HDR* p_msg) {
555 uint8_t* p;
556
557 #if (RW_STATS_INCLUDED == TRUE)
558 bool is_retry;
559 /* Update stats */
560 rw_main_update_tx_stats(p_msg->len, ((rw_cb.cur_retry == 0) ? false : true));
561 #endif /* RW_STATS_INCLUDED */
562
563 /* Set NFC-F SoD field (payload len + 1) */
564 p_msg->offset -= 1; /* Point to SoD field */
565 p = (uint8_t*)(p_msg + 1) + p_msg->offset;
566 UINT8_TO_STREAM(p, (p_msg->len + 1));
567 p_msg->len += 1; /* Increment len to include SoD */
568
569 return (NFC_SendData(NFC_RF_CONN_ID, p_msg));
570 }
571
572 /*****************************************************************************
573 **
574 ** Function rw_t3t_get_cmd_buf
575 **
576 ** Description Get a buffer for sending T3T messages
577 **
578 ** Returns NFC_HDR *
579 **
580 *****************************************************************************/
rw_t3t_get_cmd_buf(void)581 NFC_HDR* rw_t3t_get_cmd_buf(void) {
582 NFC_HDR* p_cmd_buf;
583
584 p_cmd_buf = (NFC_HDR*)GKI_getpoolbuf(NFC_RW_POOL_ID);
585 if (p_cmd_buf != nullptr) {
586 /* Reserve offset for NCI_DATA_HDR and NFC-F Sod (LEN) field */
587 p_cmd_buf->offset = NCI_MSG_OFFSET_SIZE + NCI_DATA_HDR_SIZE + 1;
588 p_cmd_buf->len = 0;
589 }
590
591 return (p_cmd_buf);
592 }
593
594 /*****************************************************************************
595 **
596 ** Function rw_t3t_send_cmd
597 **
598 ** Description Send command to tag, and start timer for response
599 **
600 ** Returns tNFC_STATUS
601 **
602 *****************************************************************************/
rw_t3t_send_cmd(tRW_T3T_CB * p_cb,uint8_t rw_t3t_cmd,NFC_HDR * p_cmd_buf,uint32_t timeout_ticks)603 tNFC_STATUS rw_t3t_send_cmd(tRW_T3T_CB* p_cb, uint8_t rw_t3t_cmd,
604 NFC_HDR* p_cmd_buf, uint32_t timeout_ticks) {
605 tNFC_STATUS retval;
606
607 /* Indicate first attempt to send command, back up cmd buffer in case needed
608 * for retransmission */
609 rw_cb.cur_retry = 0;
610 memcpy(p_cb->p_cur_cmd_buf, p_cmd_buf,
611 sizeof(NFC_HDR) + p_cmd_buf->offset + p_cmd_buf->len);
612
613 p_cb->cur_cmd = rw_t3t_cmd;
614 p_cb->cur_tout = timeout_ticks;
615 p_cb->rw_state = RW_T3T_STATE_COMMAND_PENDING;
616
617 retval = rw_t3t_send_to_lower(p_cmd_buf);
618 if (retval == NFC_STATUS_OK) {
619 /* Start timer for waiting for response */
620 nfc_start_quick_timer(&p_cb->timer, NFC_TTYPE_RW_T3T_RESPONSE,
621 timeout_ticks);
622 } else {
623 /* Error sending */
624 p_cb->rw_state = RW_T3T_STATE_IDLE;
625 }
626
627 DLOG_IF(INFO, nfc_debug_enabled)
628 << StringPrintf("cur_tout: %d, timeout_ticks: %d ret:%d", p_cb->cur_tout,
629 timeout_ticks, retval);
630 return (retval);
631 }
632
633 /*****************************************************************************
634 **
635 ** Function rw_t3t_send_update_ndef_attribute_cmd
636 **
637 ** Description Send UPDATE command for Attribute Information
638 **
639 ** Returns tNFC_STATUS
640 **
641 *****************************************************************************/
rw_t3t_send_update_ndef_attribute_cmd(tRW_T3T_CB * p_cb,bool write_in_progress)642 tNFC_STATUS rw_t3t_send_update_ndef_attribute_cmd(tRW_T3T_CB* p_cb,
643 bool write_in_progress) {
644 tNFC_STATUS retval = NFC_STATUS_OK;
645 NFC_HDR* p_cmd_buf;
646 uint8_t *p_cmd_start, *p;
647 uint16_t checksum, i;
648 uint8_t write_f;
649 uint32_t ln;
650 uint8_t* p_ndef_attr_info_start;
651
652 p_cmd_buf = rw_t3t_get_cmd_buf();
653 if (p_cmd_buf != nullptr) {
654 /* Construct T3T message */
655 p = p_cmd_start = (uint8_t*)(p_cmd_buf + 1) + p_cmd_buf->offset;
656
657 /* Add UPDATE opcode to message */
658 UINT8_TO_STREAM(p, T3T_MSG_OPC_UPDATE_CMD);
659
660 /* Add IDm to message */
661 ARRAY_TO_STREAM(p, p_cb->peer_nfcid2, NCI_NFCID2_LEN);
662
663 /* Add Service code list */
664 UINT8_TO_STREAM(p, 1); /* Number of services (only 1 service: NDEF) */
665 UINT16_TO_STREAM(
666 p, T3T_MSG_NDEF_SC_RW); /* Service code (little-endian format) */
667
668 /* Add number of blocks in this UPDATE command */
669 UINT8_TO_STREAM(p, 1); /* Number of blocks to write in this command */
670
671 /* Block List element: the NDEF attribute information block (block 0) */
672 UINT8_TO_STREAM(p, T3T_MSG_MASK_TWO_BYTE_BLOCK_DESC_FORMAT);
673 UINT8_TO_STREAM(p, 0);
674
675 /* Add payload (Attribute information block) */
676 p_ndef_attr_info_start =
677 p; /* Save start of a NDEF attribute info block for checksum */
678 UINT8_TO_STREAM(p, T3T_MSG_NDEF_VERSION);
679 UINT8_TO_STREAM(p, p_cb->ndef_attrib.nbr);
680 UINT8_TO_STREAM(p, p_cb->ndef_attrib.nbw);
681 UINT16_TO_BE_STREAM(p, p_cb->ndef_attrib.nmaxb);
682 UINT32_TO_STREAM(p, 0);
683
684 /* If starting NDEF write: set WriteF=ON, and ln=current ndef length */
685 if (write_in_progress) {
686 write_f = T3T_MSG_NDEF_WRITEF_ON;
687 ln = p_cb->ndef_attrib.ln;
688 }
689 /* If finishing NDEF write: set WriteF=OFF, and ln=new ndef len */
690 else {
691 write_f = T3T_MSG_NDEF_WRITEF_OFF;
692 ln = p_cb->ndef_msg_len;
693 }
694 UINT8_TO_STREAM(p, write_f);
695 UINT8_TO_STREAM(p, p_cb->ndef_attrib.rwflag);
696 UINT8_TO_STREAM(p, (ln >> 16) & 0xFF); /* High byte (of 3) of Ln */
697 UINT8_TO_STREAM(p, (ln >> 8) & 0xFF); /* Middle byte (of 3) of Ln */
698 UINT8_TO_STREAM(p, (ln)&0xFF); /* Low byte (of 3) of Ln */
699
700 /* Calculate and append Checksum */
701 checksum = 0;
702 for (i = 0; i < T3T_MSG_NDEF_ATTR_INFO_SIZE; i++) {
703 checksum += p_ndef_attr_info_start[i];
704 }
705 UINT16_TO_BE_STREAM(p, checksum);
706
707 /* Calculate length of message */
708 p_cmd_buf->len = (uint16_t)(p - p_cmd_start);
709
710 /* Send the T3T message */
711 retval = rw_t3t_send_cmd(p_cb, RW_T3T_CMD_UPDATE_NDEF, p_cmd_buf,
712 rw_t3t_update_timeout(1));
713 } else {
714 retval = NFC_STATUS_NO_BUFFERS;
715 }
716
717 return (retval);
718 }
719
720 /*****************************************************************************
721 **
722 ** Function rw_t3t_send_next_ndef_update_cmd
723 **
724 ** Description Send next segment of NDEF message to update
725 **
726 ** Returns tNFC_STATUS
727 **
728 *****************************************************************************/
rw_t3t_send_next_ndef_update_cmd(tRW_T3T_CB * p_cb)729 tNFC_STATUS rw_t3t_send_next_ndef_update_cmd(tRW_T3T_CB* p_cb) {
730 tNFC_STATUS retval = NFC_STATUS_OK;
731 uint16_t block_id;
732 uint16_t first_block_to_write;
733 uint16_t ndef_blocks_to_write, ndef_blocks_remaining;
734 uint32_t ndef_bytes_remaining, ndef_padding = 0;
735 uint8_t flags = 0;
736 uint8_t* p_cur_ndef_src_offset;
737 NFC_HDR* p_cmd_buf;
738 uint8_t *p_cmd_start, *p;
739 uint8_t blocks_per_update;
740 uint32_t timeout;
741
742 p_cmd_buf = rw_t3t_get_cmd_buf();
743 if (p_cmd_buf != nullptr) {
744 /* Construct T3T message */
745 p = p_cmd_start = (uint8_t*)(p_cmd_buf + 1) + p_cmd_buf->offset;
746
747 /* Calculate number of ndef bytes remaining to write */
748 ndef_bytes_remaining = p_cb->ndef_msg_len - p_cb->ndef_msg_bytes_sent;
749
750 /* Calculate number of blocks remaining to write */
751 ndef_blocks_remaining =
752 (uint16_t)((ndef_bytes_remaining + 15) >>
753 4); /* ndef blocks remaining (rounded upward) */
754
755 /* Calculate first NDEF block ID for this UPDATE command */
756 first_block_to_write = (uint16_t)((p_cb->ndef_msg_bytes_sent >> 4) + 1);
757
758 /* Calculate max number of blocks per write. */
759 if ((first_block_to_write +
760 RW_T3T_MAX_NDEF_BLOCKS_PER_UPDATE_1_BYTE_FORMAT) < 0x100) {
761 /* All block-numbers are < 0x100 (i.e. can be specified using one-byte
762 * format) */
763 blocks_per_update = RW_T3T_MAX_NDEF_BLOCKS_PER_UPDATE_1_BYTE_FORMAT;
764 } else {
765 /* Block-numbers are >= 0x100 (i.e. need to be specified using two-byte
766 * format) */
767 blocks_per_update = RW_T3T_MAX_NDEF_BLOCKS_PER_UPDATE_2_BYTE_FORMAT;
768 }
769
770 /* Check if blocks_per_update is bigger than what peer allows */
771 if (blocks_per_update > p_cb->ndef_attrib.nbw)
772 blocks_per_update = p_cb->ndef_attrib.nbw;
773
774 /* Check if remaining blocks can fit into one UPDATE command */
775 if (ndef_blocks_remaining <= blocks_per_update) {
776 /* remaining blocks can fit into one UPDATE command */
777 ndef_blocks_to_write = ndef_blocks_remaining;
778 } else {
779 /* Remaining blocks cannot fit into one UPDATE command */
780 ndef_blocks_to_write = blocks_per_update;
781 }
782
783 /* Write to command header for UPDATE */
784
785 /* Add UPDATE opcode to message */
786 UINT8_TO_STREAM(p, T3T_MSG_OPC_UPDATE_CMD);
787
788 /* Add IDm to message */
789 ARRAY_TO_STREAM(p, p_cb->peer_nfcid2, NCI_NFCID2_LEN);
790
791 /* Add Service code list */
792 UINT8_TO_STREAM(p, 1); /* Number of services (only 1 service: NDEF) */
793 UINT16_TO_STREAM(
794 p, T3T_MSG_NDEF_SC_RW); /* Service code (little-endian format) */
795
796 /* Add number of blocks in this UPDATE command */
797 UINT8_TO_STREAM(
798 p,
799 ndef_blocks_to_write); /* Number of blocks to write in this command */
800 timeout = rw_t3t_update_timeout(ndef_blocks_to_write);
801
802 for (block_id = first_block_to_write;
803 block_id < (first_block_to_write + ndef_blocks_to_write); block_id++) {
804 if (block_id < 256) {
805 /* Block IDs 0-255 can be specified in '2-byte' format: byte0=0,
806 * byte1=blocknumber */
807 UINT8_TO_STREAM(
808 p, T3T_MSG_MASK_TWO_BYTE_BLOCK_DESC_FORMAT); /* byte0: len=1;
809 access-mode=0;
810 service code list
811 order=0 */
812 UINT8_TO_STREAM(p, block_id); /* byte1: block number */
813 } else {
814 /* Block IDs 256+ must be specified in '3-byte' format: byte0=80h,
815 * followed by blocknumber */
816 UINT8_TO_STREAM(
817 p,
818 0x00); /* byte0: len=0; access-mode=0; service code list order=0 */
819 UINT16_TO_STREAM(
820 p, block_id); /* byte1-2: block number in little-endian format */
821 }
822 }
823
824 /* Add NDEF payload */
825
826 /* If this sending last block of NDEF, check if padding is needed to make
827 * payload a multiple of 16 bytes */
828 if (ndef_blocks_to_write == ndef_blocks_remaining) {
829 ndef_padding = (16 - (ndef_bytes_remaining & 0x0F)) & 0x0F;
830 if (ndef_padding) {
831 flags |= RW_T3T_FL_PADDING;
832 ndef_blocks_to_write--; /* handle the last block separately if it needs
833 padding */
834 }
835 }
836
837 /* Add NDEF payload to the message */
838 p_cur_ndef_src_offset = &p_cb->ndef_msg[p_cb->ndef_msg_bytes_sent];
839
840 ARRAY_TO_STREAM(p, p_cur_ndef_src_offset, (ndef_blocks_to_write * 16));
841 p_cb->ndef_msg_bytes_sent += ((uint32_t)ndef_blocks_to_write * 16);
842
843 if (flags & RW_T3T_FL_PADDING) {
844 /* Add last of the NDEF message */
845 p_cur_ndef_src_offset = &p_cb->ndef_msg[p_cb->ndef_msg_bytes_sent];
846 ARRAY_TO_STREAM(p, p_cur_ndef_src_offset, (int)(16 - ndef_padding));
847 p_cb->ndef_msg_bytes_sent += (16 - ndef_padding);
848
849 /* Add padding */
850 memset(p, 0, ndef_padding);
851 p += ndef_padding;
852 }
853
854 /* Calculate length of message */
855 p_cmd_buf->len = (uint16_t)(p - p_cmd_start);
856
857 /* Send the T3T message */
858 retval = rw_t3t_send_cmd(p_cb, RW_T3T_CMD_UPDATE_NDEF, p_cmd_buf, timeout);
859 } else {
860 retval = NFC_STATUS_NO_BUFFERS;
861 }
862
863 return (retval);
864 }
865
866 /*****************************************************************************
867 **
868 ** Function rw_t3t_send_next_ndef_check_cmd
869 **
870 ** Description Send command for reading next segment of NDEF message
871 **
872 ** Returns tNFC_STATUS
873 **
874 *****************************************************************************/
rw_t3t_send_next_ndef_check_cmd(tRW_T3T_CB * p_cb)875 tNFC_STATUS rw_t3t_send_next_ndef_check_cmd(tRW_T3T_CB* p_cb) {
876 tNFC_STATUS retval = NFC_STATUS_OK;
877 uint16_t block_id;
878 uint16_t ndef_blocks_remaining, first_block_to_read, cur_blocks_to_read;
879 uint32_t ndef_bytes_remaining;
880 NFC_HDR* p_cmd_buf;
881 uint8_t *p_cmd_start, *p;
882
883 p_cmd_buf = rw_t3t_get_cmd_buf();
884 if (p_cmd_buf != nullptr) {
885 /* Construct T3T message */
886 p = p_cmd_start = (uint8_t*)(p_cmd_buf + 1) + p_cmd_buf->offset;
887
888 /* Calculate number of ndef bytes remaining to read */
889 ndef_bytes_remaining = p_cb->ndef_attrib.ln - p_cb->ndef_rx_offset;
890
891 /* Calculate number of blocks remaining to read */
892 ndef_blocks_remaining =
893 (uint16_t)((ndef_bytes_remaining + 15) >>
894 4); /* ndef blocks remaining (rounded upward) */
895
896 /* Calculate first NDEF block ID */
897 first_block_to_read = (uint16_t)((p_cb->ndef_rx_offset >> 4) + 1);
898
899 /* Check if remaining blocks can fit into one CHECK command */
900 if (ndef_blocks_remaining <= p_cb->ndef_attrib.nbr) {
901 /* remaining blocks can fit into one CHECK command */
902 cur_blocks_to_read = ndef_blocks_remaining;
903 p_cb->ndef_rx_readlen = ndef_bytes_remaining;
904 p_cb->flags |= RW_T3T_FL_IS_FINAL_NDEF_SEGMENT;
905 } else {
906 /* Remaining blocks cannot fit into one CHECK command */
907 cur_blocks_to_read =
908 p_cb->ndef_attrib
909 .nbr; /* Read maximum number of blocks allowed by the peer */
910 p_cb->ndef_rx_readlen = ((uint32_t)p_cb->ndef_attrib.nbr * 16);
911 }
912
913 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
914 "bytes_remaining: %i, cur_blocks_to_read: %i, is_final: %i",
915 ndef_bytes_remaining, cur_blocks_to_read,
916 (p_cb->flags & RW_T3T_FL_IS_FINAL_NDEF_SEGMENT));
917
918 /* Add CHECK opcode to message */
919 UINT8_TO_STREAM(p, T3T_MSG_OPC_CHECK_CMD);
920
921 /* Add IDm to message */
922 ARRAY_TO_STREAM(p, p_cb->peer_nfcid2, NCI_NFCID2_LEN);
923
924 /* Add Service code list */
925 UINT8_TO_STREAM(p, 1); /* Number of services (only 1 service: NDEF) */
926
927 /* Service code (little-endian format) . If NDEF is read-only, then use
928 * T3T_MSG_NDEF_SC_RO, otherwise use T3T_MSG_NDEF_SC_RW */
929 if (p_cb->ndef_attrib.rwflag == T3T_MSG_NDEF_RWFLAG_RO) {
930 UINT16_TO_STREAM(p, T3T_MSG_NDEF_SC_RO);
931 } else {
932 UINT16_TO_STREAM(p, T3T_MSG_NDEF_SC_RW);
933 }
934
935 /* Add number of blocks in this CHECK command */
936 UINT8_TO_STREAM(
937 p, cur_blocks_to_read); /* Number of blocks to check in this command */
938
939 for (block_id = first_block_to_read;
940 block_id < (first_block_to_read + cur_blocks_to_read); block_id++) {
941 if (block_id < 256) {
942 /* Block IDs 0-255 can be specified in '2-byte' format: byte0=0,
943 * byte1=blocknumber */
944 UINT8_TO_STREAM(
945 p, T3T_MSG_MASK_TWO_BYTE_BLOCK_DESC_FORMAT); /* byte1: len=0;
946 access-mode=0;
947 service code list
948 order=0 */
949 UINT8_TO_STREAM(p, block_id); /* byte1: block number */
950 } else {
951 /* Block IDs 256+ must be specified in '3-byte' format: byte0=80h,
952 * followed by blocknumber */
953 UINT8_TO_STREAM(
954 p,
955 0x00); /* byte0: len=1; access-mode=0; service code list order=0 */
956 UINT16_TO_STREAM(
957 p, block_id); /* byte1-2: block number in little-endian format */
958 }
959 }
960
961 /* Calculate length of message */
962 p_cmd_buf->len = (uint16_t)(p - p_cmd_start);
963
964 /* Send the T3T message */
965 retval = rw_t3t_send_cmd(p_cb, RW_T3T_CMD_CHECK_NDEF, p_cmd_buf,
966 rw_t3t_check_timeout(cur_blocks_to_read));
967 } else {
968 retval = NFC_STATUS_NO_BUFFERS;
969 }
970
971 return (retval);
972 }
973
974 /*****************************************************************************
975 **
976 ** Function rw_t3t_message_set_block_list
977 **
978 ** Description Add block list to T3T message
979 **
980 ** Returns Number of bytes added to message
981 **
982 *****************************************************************************/
rw_t3t_message_set_block_list(tRW_T3T_CB * p_cb,uint8_t ** p,uint8_t num_blocks,tT3T_BLOCK_DESC * p_t3t_blocks)983 void rw_t3t_message_set_block_list(tRW_T3T_CB* p_cb, uint8_t** p,
984 uint8_t num_blocks,
985 tT3T_BLOCK_DESC* p_t3t_blocks) {
986 uint16_t i, cur_service_code;
987 uint8_t service_code_idx, num_services = 0;
988 uint8_t* p_msg_num_services;
989 uint16_t service_list[T3T_MSG_SERVICE_LIST_MAX];
990
991 /* Add CHECK or UPDATE opcode to message */
992 UINT8_TO_STREAM(
993 (*p), ((p_cb->cur_cmd == RW_T3T_CMD_CHECK) ? T3T_MSG_OPC_CHECK_CMD
994 : T3T_MSG_OPC_UPDATE_CMD));
995
996 /* Add IDm to message */
997 ARRAY_TO_STREAM((*p), p_cb->peer_nfcid2, NCI_NFCID2_LEN);
998
999 /* Skip over Number of Services field */
1000 p_msg_num_services = (*p); /* pointer to Number of Services offset */
1001 (*p)++;
1002
1003 /* Count number of different services are specified in the list, and add
1004 * services to Service Code list */
1005 for (i = 0; i < num_blocks; i++) {
1006 cur_service_code = p_t3t_blocks[i].service_code;
1007
1008 /* Check if current service_code is already in the service_list */
1009 for (service_code_idx = 0; service_code_idx < num_services;
1010 service_code_idx++) {
1011 if (service_list[service_code_idx] == cur_service_code) break;
1012 }
1013
1014 if (service_code_idx == num_services) {
1015 /* Service not in the list yet. Add it. */
1016 service_list[service_code_idx] = cur_service_code;
1017 num_services++;
1018
1019 /* Add service code to T3T message */
1020 UINT16_TO_STREAM((*p), cur_service_code);
1021 }
1022 }
1023
1024 /* Add 'Number of Sservices' to the message */
1025 *p_msg_num_services = num_services;
1026
1027 /* Add 'number of blocks' to the message */
1028 UINT8_TO_STREAM((*p), num_blocks);
1029
1030 /* Add block descriptors */
1031 for (i = 0; i < num_blocks; i++) {
1032 cur_service_code = p_t3t_blocks[i].service_code;
1033
1034 /* Check if current service_code is already in the service_list */
1035 for (service_code_idx = 0; service_code_idx < num_services;
1036 service_code_idx++) {
1037 if (service_list[service_code_idx] == cur_service_code) break;
1038 }
1039
1040 /* Add decriptor to T3T message */
1041 if (p_t3t_blocks[i].block_number > 0xFF) {
1042 UINT8_TO_STREAM((*p), service_code_idx);
1043 UINT16_TO_STREAM((*p), p_t3t_blocks[i].block_number);
1044 } else {
1045 service_code_idx |= T3T_MSG_MASK_TWO_BYTE_BLOCK_DESC_FORMAT;
1046 UINT8_TO_STREAM((*p), service_code_idx);
1047 UINT8_TO_STREAM((*p), p_t3t_blocks[i].block_number);
1048 }
1049 }
1050 }
1051
1052 /*****************************************************************************
1053 **
1054 ** Function rw_t3t_send_check_cmd
1055 **
1056 ** Description Send CHECK command
1057 **
1058 ** Returns tNFC_STATUS
1059 **
1060 *****************************************************************************/
rw_t3t_send_check_cmd(tRW_T3T_CB * p_cb,uint8_t num_blocks,tT3T_BLOCK_DESC * p_t3t_blocks)1061 tNFC_STATUS rw_t3t_send_check_cmd(tRW_T3T_CB* p_cb, uint8_t num_blocks,
1062 tT3T_BLOCK_DESC* p_t3t_blocks) {
1063 NFC_HDR* p_cmd_buf;
1064 uint8_t *p, *p_cmd_start;
1065 tNFC_STATUS retval = NFC_STATUS_OK;
1066
1067 p_cb->cur_cmd = RW_T3T_CMD_CHECK;
1068 p_cmd_buf = rw_t3t_get_cmd_buf();
1069 if (p_cmd_buf != nullptr) {
1070 /* Construct T3T message */
1071 p = p_cmd_start = (uint8_t*)(p_cmd_buf + 1) + p_cmd_buf->offset;
1072 rw_t3t_message_set_block_list(p_cb, &p, num_blocks, p_t3t_blocks);
1073
1074 /* Calculate length of message */
1075 p_cmd_buf->len = (uint16_t)(p - p_cmd_start);
1076
1077 /* Send the T3T message */
1078 retval = rw_t3t_send_cmd(p_cb, RW_T3T_CMD_CHECK, p_cmd_buf,
1079 rw_t3t_check_timeout(num_blocks));
1080 } else {
1081 retval = NFC_STATUS_NO_BUFFERS;
1082 }
1083
1084 return (retval);
1085 }
1086
1087 /*****************************************************************************
1088 **
1089 ** Function rw_t3t_send_update_cmd
1090 **
1091 ** Description Send UPDATE command
1092 **
1093 ** Returns tNFC_STATUS
1094 **
1095 *****************************************************************************/
rw_t3t_send_update_cmd(tRW_T3T_CB * p_cb,uint8_t num_blocks,tT3T_BLOCK_DESC * p_t3t_blocks,uint8_t * p_data)1096 tNFC_STATUS rw_t3t_send_update_cmd(tRW_T3T_CB* p_cb, uint8_t num_blocks,
1097 tT3T_BLOCK_DESC* p_t3t_blocks,
1098 uint8_t* p_data) {
1099 NFC_HDR* p_cmd_buf;
1100 uint8_t *p, *p_cmd_start;
1101 tNFC_STATUS retval = NFC_STATUS_OK;
1102
1103 p_cb->cur_cmd = RW_T3T_CMD_UPDATE;
1104 p_cmd_buf = rw_t3t_get_cmd_buf();
1105 if (p_cmd_buf != nullptr) {
1106 /* Construct T3T message */
1107 p = p_cmd_start = (uint8_t*)(p_cmd_buf + 1) + p_cmd_buf->offset;
1108 rw_t3t_message_set_block_list(p_cb, &p, num_blocks, p_t3t_blocks);
1109
1110 /* Add data blocks to the message */
1111 ARRAY_TO_STREAM(p, p_data, num_blocks * 16);
1112
1113 /* Calculate length of message */
1114 p_cmd_buf->len = (uint16_t)(p - p_cmd_start);
1115
1116 /* Send the T3T message */
1117 retval = rw_t3t_send_cmd(p_cb, RW_T3T_CMD_UPDATE, p_cmd_buf,
1118 rw_t3t_update_timeout(num_blocks));
1119 } else {
1120 retval = NFC_STATUS_NO_BUFFERS;
1121 }
1122
1123 return (retval);
1124 }
1125
1126 /*****************************************************************************
1127 **
1128 ** Function rw_t3t_check_mc_block
1129 **
1130 ** Description Send command to check Memory Configuration Block
1131 **
1132 ** Returns tNFC_STATUS
1133 **
1134 *****************************************************************************/
rw_t3t_check_mc_block(tRW_T3T_CB * p_cb)1135 tNFC_STATUS rw_t3t_check_mc_block(tRW_T3T_CB* p_cb) {
1136 NFC_HDR* p_cmd_buf;
1137 uint8_t *p, *p_cmd_start;
1138
1139 /* Read Memory Configuration block */
1140 p_cmd_buf = rw_t3t_get_cmd_buf();
1141 if (p_cmd_buf != nullptr) {
1142 /* Construct T3T message */
1143 p = p_cmd_start = (uint8_t*)(p_cmd_buf + 1) + p_cmd_buf->offset;
1144
1145 /* Add CHECK opcode to message */
1146 UINT8_TO_STREAM(p, T3T_MSG_OPC_CHECK_CMD);
1147
1148 /* Add IDm to message */
1149 ARRAY_TO_STREAM(p, p_cb->peer_nfcid2, NCI_NFCID2_LEN);
1150
1151 /* Add Service code list */
1152 UINT8_TO_STREAM(p, 1); /* Number of services (only 1 service: NDEF) */
1153 UINT16_TO_STREAM(
1154 p, T3T_MSG_NDEF_SC_RO); /* Service code (little-endian format) */
1155
1156 /* Number of blocks */
1157 UINT8_TO_STREAM(p, 1); /* Number of blocks (only 1 block: Memory
1158 Configuration Information ) */
1159
1160 /* Block List element: the Memory Configuration block (block 0x88) */
1161 UINT8_TO_STREAM(p, T3T_MSG_MASK_TWO_BYTE_BLOCK_DESC_FORMAT);
1162 UINT8_TO_STREAM(p, T3T_MSG_FELICALITE_BLOCK_ID_MC);
1163
1164 /* Calculate length of message */
1165 p_cmd_buf->len = (uint16_t)(p - p_cmd_start);
1166
1167 /* Send the T3T message */
1168 return rw_t3t_send_cmd(p_cb, p_cb->cur_cmd, p_cmd_buf,
1169 rw_t3t_check_timeout(1));
1170 } else {
1171 LOG(ERROR) << StringPrintf("Unable to allocate buffer to read MC block");
1172 return (NFC_STATUS_NO_BUFFERS);
1173 }
1174 }
1175
1176 /*****************************************************************************
1177 **
1178 ** Function rw_t3t_send_raw_frame
1179 **
1180 ** Description Send raw frame
1181 **
1182 ** Returns tNFC_STATUS
1183 **
1184 *****************************************************************************/
rw_t3t_send_raw_frame(tRW_T3T_CB * p_cb,uint16_t len,uint8_t * p_data)1185 tNFC_STATUS rw_t3t_send_raw_frame(tRW_T3T_CB* p_cb, uint16_t len,
1186 uint8_t* p_data) {
1187 NFC_HDR* p_cmd_buf;
1188 uint8_t* p;
1189 tNFC_STATUS retval = NFC_STATUS_OK;
1190
1191 p_cmd_buf = rw_t3t_get_cmd_buf();
1192 if (p_cmd_buf != nullptr) {
1193 /* Construct T3T message */
1194 p = (uint8_t*)(p_cmd_buf + 1) + p_cmd_buf->offset;
1195
1196 /* Add data blocks to the message */
1197 ARRAY_TO_STREAM(p, p_data, len);
1198
1199 /* Calculate length of message */
1200 p_cmd_buf->len = len;
1201
1202 /* Send the T3T message */
1203 retval = rw_t3t_send_cmd(p_cb, RW_T3T_CMD_SEND_RAW_FRAME, p_cmd_buf,
1204 RW_T3T_RAW_FRAME_CMD_TIMEOUT_TICKS);
1205 } else {
1206 retval = NFC_STATUS_NO_BUFFERS;
1207 }
1208
1209 return (retval);
1210 }
1211
1212 /*****************************************************************************
1213 ** TAG RESPONSE HANDLERS
1214 *****************************************************************************/
1215
1216 /*****************************************************************************
1217 **
1218 ** Function rw_t3t_act_handle_ndef_detect_rsp
1219 **
1220 ** Description Handle response to NDEF detection
1221 **
1222 ** Returns Nothing
1223 **
1224 *****************************************************************************/
rw_t3t_act_handle_ndef_detect_rsp(tRW_T3T_CB * p_cb,NFC_HDR * p_msg_rsp)1225 void rw_t3t_act_handle_ndef_detect_rsp(tRW_T3T_CB* p_cb, NFC_HDR* p_msg_rsp) {
1226 uint8_t* p;
1227 uint32_t temp;
1228 uint8_t i;
1229 uint16_t checksum_calc, checksum_rx;
1230 tRW_DETECT_NDEF_DATA evt_data;
1231 uint8_t* p_t3t_rsp = (uint8_t*)(p_msg_rsp + 1) + p_msg_rsp->offset;
1232
1233 evt_data.status = NFC_STATUS_FAILED;
1234 evt_data.flags = RW_NDEF_FL_UNKNOWN;
1235
1236 /* Check if response code is CHECK resp (for reading NDEF attribute block) */
1237 if (p_t3t_rsp[T3T_MSG_RSP_OFFSET_RSPCODE] != T3T_MSG_OPC_CHECK_RSP) {
1238 LOG(ERROR) << StringPrintf(
1239 "Response error: expecting rsp_code %02X, but got %02X",
1240 T3T_MSG_OPC_CHECK_RSP, p_t3t_rsp[T3T_MSG_RSP_OFFSET_RSPCODE]);
1241 evt_data.status = NFC_STATUS_FAILED;
1242 }
1243 /* Validate status code and NFCID2 response from tag */
1244 else if ((p_t3t_rsp[T3T_MSG_RSP_OFFSET_STATUS1] !=
1245 T3T_MSG_RSP_STATUS_OK) /* verify response status code */
1246 || (memcmp(p_cb->peer_nfcid2, &p_t3t_rsp[T3T_MSG_RSP_OFFSET_IDM],
1247 NCI_NFCID2_LEN) != 0)) /* verify response IDm */
1248 {
1249 evt_data.status = NFC_STATUS_FAILED;
1250 } else if (p_msg_rsp->len <
1251 (T3T_MSG_RSP_OFFSET_CHECK_DATA + T3T_MSG_BLOCKSIZE)) {
1252 evt_data.status = NFC_STATUS_FAILED;
1253 android_errorWriteLog(0x534e4554, "120428041");
1254 } else {
1255 /* Get checksum from received ndef attribute msg */
1256 p = &p_t3t_rsp[T3T_MSG_RSP_OFFSET_CHECK_DATA + T3T_MSG_NDEF_ATTR_INFO_SIZE];
1257 BE_STREAM_TO_UINT16(checksum_rx, p);
1258
1259 /* Calculate checksum - move check for checsum to beginning */
1260 checksum_calc = 0;
1261 p = &p_t3t_rsp[T3T_MSG_RSP_OFFSET_CHECK_DATA];
1262 for (i = 0; i < T3T_MSG_NDEF_ATTR_INFO_SIZE; i++) {
1263 checksum_calc += p[i];
1264 }
1265
1266 /* Validate checksum */
1267 if (checksum_calc != checksum_rx) {
1268 p_cb->ndef_attrib.status =
1269 NFC_STATUS_FAILED; /* only ok or failed passed to the app. can be
1270 boolean*/
1271
1272 LOG(ERROR) << StringPrintf("RW_T3tDetectNDEF checksum failed");
1273 } else {
1274 p_cb->ndef_attrib.status = NFC_STATUS_OK;
1275
1276 /* Validate version number */
1277 STREAM_TO_UINT8(p_cb->ndef_attrib.version, p);
1278
1279 if (T3T_GET_MAJOR_VERSION(T3T_MSG_NDEF_VERSION) <
1280 T3T_GET_MAJOR_VERSION(p_cb->ndef_attrib.version)) {
1281 /* Remote tag's MajorVer is newer than our's. Reject NDEF as per T3TOP
1282 * RQ_T3T_NDA_024 */
1283 LOG(ERROR) << StringPrintf(
1284 "RW_T3tDetectNDEF: incompatible NDEF version. Local=0x%02x, "
1285 "Remote=0x%02x",
1286 T3T_MSG_NDEF_VERSION, p_cb->ndef_attrib.version);
1287 p_cb->ndef_attrib.status = NFC_STATUS_FAILED;
1288 evt_data.status = NFC_STATUS_BAD_RESP;
1289 } else {
1290 /* Remote tag's MajorVer is equal or older than our's. NDEF is
1291 * compatible with our version. */
1292
1293 /* Update NDEF info */
1294 STREAM_TO_UINT8(
1295 p_cb->ndef_attrib.nbr,
1296 p); /* NBr: number of blocks that can be read using one Check
1297 command */
1298 STREAM_TO_UINT8(p_cb->ndef_attrib.nbw,
1299 p); /* Nbw: number of blocks that can be written using
1300 one Update command */
1301 BE_STREAM_TO_UINT16(
1302 p_cb->ndef_attrib.nmaxb,
1303 p); /* Nmaxb: maximum number of blocks available for NDEF data */
1304 BE_STREAM_TO_UINT32(temp, p);
1305 STREAM_TO_UINT8(p_cb->ndef_attrib.writef,
1306 p); /* WriteFlag: 00h if writing data finished; 0Fh if
1307 writing data in progress */
1308 STREAM_TO_UINT8(
1309 p_cb->ndef_attrib.rwflag,
1310 p); /* RWFlag: 00h NDEF is read-only; 01h if read/write available */
1311
1312 /* Get length (3-byte, big-endian) */
1313 STREAM_TO_UINT8(temp, p); /* Ln: high-byte */
1314 BE_STREAM_TO_UINT16(p_cb->ndef_attrib.ln, p); /* Ln: lo-word */
1315 p_cb->ndef_attrib.ln += (temp << 16);
1316
1317 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
1318 "Detected NDEF Ver: 0x%02x", p_cb->ndef_attrib.version);
1319 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
1320 "Detected NDEF Attributes: Nbr=%i, Nbw=%i, Nmaxb=%i, WriteF=%i, "
1321 "RWFlag=%i, Ln=%i",
1322 p_cb->ndef_attrib.nbr, p_cb->ndef_attrib.nbw,
1323 p_cb->ndef_attrib.nmaxb, p_cb->ndef_attrib.writef,
1324 p_cb->ndef_attrib.rwflag, p_cb->ndef_attrib.ln);
1325
1326 /* Set data for RW_T3T_NDEF_DETECT_EVT */
1327 evt_data.status = p_cb->ndef_attrib.status;
1328 evt_data.cur_size = p_cb->ndef_attrib.ln;
1329 evt_data.max_size = (uint32_t)p_cb->ndef_attrib.nmaxb * 16;
1330 evt_data.protocol = NFC_PROTOCOL_T3T;
1331 evt_data.flags = (RW_NDEF_FL_SUPPORTED | RW_NDEF_FL_FORMATED);
1332 if (p_cb->ndef_attrib.rwflag == T3T_MSG_NDEF_RWFLAG_RO)
1333 evt_data.flags |= RW_NDEF_FL_READ_ONLY;
1334 }
1335 }
1336 }
1337
1338 DLOG_IF(INFO, nfc_debug_enabled)
1339 << StringPrintf("RW_T3tDetectNDEF response: %i", evt_data.status);
1340
1341 p_cb->rw_state = RW_T3T_STATE_IDLE;
1342 rw_t3t_update_ndef_flag(&evt_data.flags);
1343 /* Notify app of NDEF detection result */
1344 tRW_DATA rw_data;
1345 rw_data.ndef = evt_data;
1346 (*(rw_cb.p_cback))(RW_T3T_NDEF_DETECT_EVT, &rw_data);
1347
1348 GKI_freebuf(p_msg_rsp);
1349 }
1350
1351 /*****************************************************************************
1352 **
1353 ** Function rw_t3t_act_handle_check_rsp
1354 **
1355 ** Description Handle response to CHECK command
1356 **
1357 ** Returns Nothing
1358 **
1359 *****************************************************************************/
rw_t3t_act_handle_check_rsp(tRW_T3T_CB * p_cb,NFC_HDR * p_msg_rsp)1360 void rw_t3t_act_handle_check_rsp(tRW_T3T_CB* p_cb, NFC_HDR* p_msg_rsp) {
1361 uint8_t* p_t3t_rsp = (uint8_t*)(p_msg_rsp + 1) + p_msg_rsp->offset;
1362 tRW_READ_DATA evt_data;
1363 tNFC_STATUS nfc_status = NFC_STATUS_OK;
1364
1365 /* Validate response from tag */
1366 if ((p_t3t_rsp[T3T_MSG_RSP_OFFSET_STATUS1] !=
1367 T3T_MSG_RSP_STATUS_OK) /* verify response status code */
1368 || (memcmp(p_cb->peer_nfcid2, &p_t3t_rsp[T3T_MSG_RSP_OFFSET_IDM],
1369 NCI_NFCID2_LEN) != 0)) /* verify response IDm */
1370 {
1371 nfc_status = NFC_STATUS_FAILED;
1372 GKI_freebuf(p_msg_rsp);
1373 } else if (p_t3t_rsp[T3T_MSG_RSP_OFFSET_RSPCODE] != T3T_MSG_OPC_CHECK_RSP) {
1374 LOG(ERROR) << StringPrintf(
1375 "Response error: expecting rsp_code %02X, but got %02X",
1376 T3T_MSG_OPC_CHECK_RSP, p_t3t_rsp[T3T_MSG_RSP_OFFSET_RSPCODE]);
1377 nfc_status = NFC_STATUS_FAILED;
1378 GKI_freebuf(p_msg_rsp);
1379 } else if (p_msg_rsp->len >= T3T_MSG_RSP_OFFSET_CHECK_DATA) {
1380 /* Copy incoming data into buffer */
1381 p_msg_rsp->offset +=
1382 T3T_MSG_RSP_OFFSET_CHECK_DATA; /* Skip over t3t header */
1383 p_msg_rsp->len -= T3T_MSG_RSP_OFFSET_CHECK_DATA;
1384 evt_data.status = NFC_STATUS_OK;
1385 evt_data.p_data = p_msg_rsp;
1386 tRW_DATA rw_data;
1387 rw_data.data = evt_data;
1388 (*(rw_cb.p_cback))(RW_T3T_CHECK_EVT, &rw_data);
1389 } else {
1390 android_errorWriteLog(0x534e4554, "120503926");
1391 nfc_status = NFC_STATUS_FAILED;
1392 GKI_freebuf(p_msg_rsp);
1393 }
1394
1395 p_cb->rw_state = RW_T3T_STATE_IDLE;
1396
1397 tRW_DATA rw_data;
1398 rw_data.status = nfc_status;
1399 (*(rw_cb.p_cback))(RW_T3T_CHECK_CPLT_EVT, &rw_data);
1400 }
1401
1402 /*****************************************************************************
1403 **
1404 ** Function rw_t3t_act_handle_update_rsp
1405 **
1406 ** Description Handle response to UPDATE command
1407 **
1408 ** Returns Nothing
1409 **
1410 *****************************************************************************/
rw_t3t_act_handle_update_rsp(tRW_T3T_CB * p_cb,NFC_HDR * p_msg_rsp)1411 void rw_t3t_act_handle_update_rsp(tRW_T3T_CB* p_cb, NFC_HDR* p_msg_rsp) {
1412 uint8_t* p_t3t_rsp = (uint8_t*)(p_msg_rsp + 1) + p_msg_rsp->offset;
1413 tRW_READ_DATA evt_data;
1414
1415 /* Validate response from tag */
1416 if ((p_t3t_rsp[T3T_MSG_RSP_OFFSET_STATUS1] !=
1417 T3T_MSG_RSP_STATUS_OK) /* verify response status code */
1418 || (memcmp(p_cb->peer_nfcid2, &p_t3t_rsp[T3T_MSG_RSP_OFFSET_IDM],
1419 NCI_NFCID2_LEN) != 0)) /* verify response IDm */
1420 {
1421 evt_data.status = NFC_STATUS_FAILED;
1422 } else if (p_t3t_rsp[T3T_MSG_RSP_OFFSET_RSPCODE] != T3T_MSG_OPC_UPDATE_RSP) {
1423 LOG(ERROR) << StringPrintf(
1424 "Response error: expecting rsp_code %02X, but got %02X",
1425 T3T_MSG_OPC_UPDATE_RSP, p_t3t_rsp[T3T_MSG_RSP_OFFSET_RSPCODE]);
1426 evt_data.status = NFC_STATUS_FAILED;
1427 } else {
1428 /* Copy incoming data into buffer */
1429 evt_data.status = NFC_STATUS_OK;
1430 }
1431
1432 p_cb->rw_state = RW_T3T_STATE_IDLE;
1433
1434 tRW_DATA rw_data;
1435 rw_data.data = evt_data;
1436 (*(rw_cb.p_cback))(RW_T3T_UPDATE_CPLT_EVT, &rw_data);
1437
1438 GKI_freebuf(p_msg_rsp);
1439 }
1440
1441 /*****************************************************************************
1442 **
1443 ** Function rw_t3t_act_handle_raw_senddata_rsp
1444 **
1445 ** Description Handle response to NDEF detection
1446 **
1447 ** Returns Nothing
1448 **
1449 *****************************************************************************/
rw_t3t_act_handle_raw_senddata_rsp(tRW_T3T_CB * p_cb,tNFC_DATA_CEVT * p_data)1450 void rw_t3t_act_handle_raw_senddata_rsp(tRW_T3T_CB* p_cb,
1451 tNFC_DATA_CEVT* p_data) {
1452 tRW_READ_DATA evt_data;
1453 NFC_HDR* p_pkt = p_data->p_data;
1454
1455 DLOG_IF(INFO, nfc_debug_enabled)
1456 << StringPrintf("RW T3T Raw Frame: Len [0x%X] Status [%s]", p_pkt->len,
1457 NFC_GetStatusName(p_data->status).c_str());
1458
1459 /* Copy incoming data into buffer */
1460 evt_data.status = p_data->status;
1461 evt_data.p_data = p_pkt;
1462
1463 p_cb->rw_state = RW_T3T_STATE_IDLE;
1464
1465 tRW_DATA rw_data;
1466 rw_data.data = evt_data;
1467 (*(rw_cb.p_cback))(RW_T3T_RAW_FRAME_EVT, &rw_data);
1468 }
1469
1470 /*****************************************************************************
1471 **
1472 ** Function rw_t3t_act_handle_check_ndef_rsp
1473 **
1474 ** Description Handle response to NDEF read segment
1475 **
1476 ** Returns Nothing
1477 **
1478 *****************************************************************************/
rw_t3t_act_handle_check_ndef_rsp(tRW_T3T_CB * p_cb,NFC_HDR * p_msg_rsp)1479 void rw_t3t_act_handle_check_ndef_rsp(tRW_T3T_CB* p_cb, NFC_HDR* p_msg_rsp) {
1480 bool check_complete = true;
1481 tNFC_STATUS nfc_status = NFC_STATUS_OK;
1482 uint8_t* p_t3t_rsp = (uint8_t*)(p_msg_rsp + 1) + p_msg_rsp->offset;
1483 uint8_t rsp_num_bytes_rx;
1484
1485 if (p_msg_rsp->len < T3T_MSG_RSP_OFFSET_CHECK_DATA) {
1486 LOG(ERROR) << StringPrintf("%s invalid len", __func__);
1487 nfc_status = NFC_STATUS_FAILED;
1488 GKI_freebuf(p_msg_rsp);
1489 android_errorWriteLog(0x534e4554, "120428637");
1490 /* Validate response from tag */
1491 } else if ((p_t3t_rsp[T3T_MSG_RSP_OFFSET_STATUS1] !=
1492 T3T_MSG_RSP_STATUS_OK) /* verify response status code */
1493 || (memcmp(p_cb->peer_nfcid2, &p_t3t_rsp[T3T_MSG_RSP_OFFSET_IDM],
1494 NCI_NFCID2_LEN) != 0) /* verify response IDm */
1495 || (p_t3t_rsp[T3T_MSG_RSP_OFFSET_NUMBLOCKS] !=
1496 ((p_cb->ndef_rx_readlen + 15) >>
1497 4))) /* verify length of response */
1498 {
1499 LOG(ERROR) << StringPrintf(
1500 "Response error: bad status, nfcid2, or invalid len: %i %i",
1501 p_t3t_rsp[T3T_MSG_RSP_OFFSET_NUMBLOCKS],
1502 ((p_cb->ndef_rx_readlen + 15) >> 4));
1503 nfc_status = NFC_STATUS_FAILED;
1504 GKI_freebuf(p_msg_rsp);
1505 } else if (p_t3t_rsp[T3T_MSG_RSP_OFFSET_RSPCODE] != T3T_MSG_OPC_CHECK_RSP) {
1506 LOG(ERROR) << StringPrintf(
1507 "Response error: expecting rsp_code %02X, but got %02X",
1508 T3T_MSG_OPC_CHECK_RSP, p_t3t_rsp[T3T_MSG_RSP_OFFSET_RSPCODE]);
1509 nfc_status = NFC_STATUS_FAILED;
1510 GKI_freebuf(p_msg_rsp);
1511 } else if (p_msg_rsp->len >= T3T_MSG_RSP_OFFSET_CHECK_DATA &&
1512 p_t3t_rsp[T3T_MSG_RSP_OFFSET_NUMBLOCKS] > 0) {
1513 /* Notify app of NDEF segment received */
1514 /* Number of bytes received, according to header */
1515 rsp_num_bytes_rx = p_t3t_rsp[T3T_MSG_RSP_OFFSET_NUMBLOCKS] * 16;
1516 p_cb->ndef_rx_offset += p_cb->ndef_rx_readlen;
1517 p_msg_rsp->offset +=
1518 T3T_MSG_RSP_OFFSET_CHECK_DATA; /* Skip over t3t header (point to block
1519 data) */
1520 p_msg_rsp->len -= T3T_MSG_RSP_OFFSET_CHECK_DATA;
1521
1522 /* Verify that the bytes received is really the amount indicated in the
1523 * check-response header */
1524 if (rsp_num_bytes_rx > p_msg_rsp->len) {
1525 LOG(ERROR) << StringPrintf(
1526 "Response error: CHECK rsp header indicates %i bytes, but only "
1527 "received %i bytes",
1528 rsp_num_bytes_rx, p_msg_rsp->len);
1529 nfc_status = NFC_STATUS_FAILED;
1530 GKI_freebuf(p_msg_rsp);
1531 } else {
1532 /* If this is the the final block, then set len to reflect only valid
1533 * bytes (do not include padding to 16-byte boundary) */
1534 if ((p_cb->flags & RW_T3T_FL_IS_FINAL_NDEF_SEGMENT) &&
1535 (p_cb->ndef_attrib.ln & 0x000F)) {
1536 rsp_num_bytes_rx -= (16 - (p_cb->ndef_attrib.ln & 0x000F));
1537 }
1538
1539 p_msg_rsp->len = rsp_num_bytes_rx;
1540 tRW_DATA rw_data;
1541 rw_data.data.status = NFC_STATUS_OK;
1542 rw_data.data.p_data = p_msg_rsp;
1543 (*(rw_cb.p_cback))(RW_T3T_CHECK_EVT, &rw_data);
1544
1545 /* Send CHECK cmd for next NDEF segment, if needed */
1546 if (!(p_cb->flags & RW_T3T_FL_IS_FINAL_NDEF_SEGMENT)) {
1547 nfc_status = rw_t3t_send_next_ndef_check_cmd(p_cb);
1548 if (nfc_status == NFC_STATUS_OK) {
1549 /* Still getting more segments. Don't send RW_T3T_CHECK_CPLT_EVT yet
1550 */
1551 check_complete = false;
1552 }
1553 }
1554 }
1555 } else {
1556 android_errorWriteLog(0x534e4554, "120502559");
1557 GKI_freebuf(p_msg_rsp);
1558 nfc_status = NFC_STATUS_FAILED;
1559 LOG(ERROR) << StringPrintf("Underflow in p_msg_rsp->len!");
1560 }
1561
1562 /* Notify app of RW_T3T_CHECK_CPLT_EVT if entire NDEF has been read, or if
1563 * failure */
1564 if (check_complete) {
1565 p_cb->rw_state = RW_T3T_STATE_IDLE;
1566 tRW_DATA evt_data;
1567 evt_data.status = nfc_status;
1568 (*(rw_cb.p_cback))(RW_T3T_CHECK_CPLT_EVT, &evt_data);
1569 }
1570 }
1571
1572 /*****************************************************************************
1573 **
1574 ** Function rw_t3t_act_handle_update_ndef_rsp
1575 **
1576 ** Description Handle response to NDEF write segment
1577 **
1578 ** Returns Nothing
1579 **
1580 *****************************************************************************/
rw_t3t_act_handle_update_ndef_rsp(tRW_T3T_CB * p_cb,NFC_HDR * p_msg_rsp)1581 void rw_t3t_act_handle_update_ndef_rsp(tRW_T3T_CB* p_cb, NFC_HDR* p_msg_rsp) {
1582 bool update_complete = true;
1583 tNFC_STATUS nfc_status = NFC_STATUS_OK;
1584 uint8_t* p_t3t_rsp = (uint8_t*)(p_msg_rsp + 1) + p_msg_rsp->offset;
1585
1586 /* Check nfcid2 and status of response */
1587 if ((p_t3t_rsp[T3T_MSG_RSP_OFFSET_STATUS1] !=
1588 T3T_MSG_RSP_STATUS_OK) /* verify response status code */
1589 || (memcmp(p_cb->peer_nfcid2, &p_t3t_rsp[T3T_MSG_RSP_OFFSET_IDM],
1590 NCI_NFCID2_LEN) != 0)) /* verify response IDm */
1591 {
1592 nfc_status = NFC_STATUS_FAILED;
1593 }
1594 /* Validate response opcode */
1595 else if (p_t3t_rsp[T3T_MSG_RSP_OFFSET_RSPCODE] != T3T_MSG_OPC_UPDATE_RSP) {
1596 LOG(ERROR) << StringPrintf(
1597 "Response error: expecting rsp_code %02X, but got %02X",
1598 T3T_MSG_OPC_UPDATE_RSP, p_t3t_rsp[T3T_MSG_RSP_OFFSET_RSPCODE]);
1599 nfc_status = NFC_STATUS_FAILED;
1600 }
1601 /* If this is response to final UPDATE, then update NDEF local size */
1602 else if (p_cb->flags & RW_T3T_FL_IS_FINAL_NDEF_SEGMENT) {
1603 /* If successful, update current NDEF size */
1604 p_cb->ndef_attrib.ln = p_cb->ndef_msg_len;
1605 }
1606 /* If any more NDEF bytes to update, then send next UPDATE command */
1607 else if (p_cb->ndef_msg_bytes_sent < p_cb->ndef_msg_len) {
1608 /* Send UPDATE command for next segment of NDEF */
1609 nfc_status = rw_t3t_send_next_ndef_update_cmd(p_cb);
1610 if (nfc_status == NFC_STATUS_OK) {
1611 /* Wait for update response */
1612 update_complete = false;
1613 }
1614 }
1615 /* Otherwise, no more NDEF bytes. Send final UPDATE for Attribute Information
1616 block */
1617 else {
1618 p_cb->flags |= RW_T3T_FL_IS_FINAL_NDEF_SEGMENT;
1619 nfc_status = rw_t3t_send_update_ndef_attribute_cmd(p_cb, false);
1620 if (nfc_status == NFC_STATUS_OK) {
1621 /* Wait for update response */
1622 update_complete = false;
1623 }
1624 }
1625
1626 /* If update is completed, then notify app */
1627 if (update_complete) {
1628 p_cb->rw_state = RW_T3T_STATE_IDLE;
1629 tRW_DATA evt_data;
1630 evt_data.status = nfc_status;
1631 (*(rw_cb.p_cback))(RW_T3T_UPDATE_CPLT_EVT, &evt_data);
1632 }
1633
1634 GKI_freebuf(p_msg_rsp);
1635
1636 return;
1637 }
1638
1639 /*****************************************************************************
1640 **
1641 ** Function rw_t3t_handle_get_sc_poll_rsp
1642 **
1643 ** Description Handle POLL response for getting system codes
1644 **
1645 ** Returns Nothing
1646 **
1647 *****************************************************************************/
rw_t3t_handle_get_sc_poll_rsp(tRW_T3T_CB * p_cb,uint8_t nci_status,uint8_t num_responses,uint8_t sensf_res_buf_size,uint8_t * p_sensf_res_buf)1648 static void rw_t3t_handle_get_sc_poll_rsp(tRW_T3T_CB* p_cb, uint8_t nci_status,
1649 uint8_t num_responses,
1650 uint8_t sensf_res_buf_size,
1651 uint8_t* p_sensf_res_buf) {
1652 uint8_t* p;
1653 uint16_t sc;
1654
1655 /* Get the system code from the response */
1656 if ((nci_status == NCI_STATUS_OK) && (num_responses > 0) &&
1657 (sensf_res_buf_size >=
1658 (RW_T3T_SENSF_RES_RD_OFFSET + RW_T3T_SENSF_RES_RD_LEN))) {
1659 p = &p_sensf_res_buf[RW_T3T_SENSF_RES_RD_OFFSET];
1660 BE_STREAM_TO_UINT16(sc, p);
1661
1662 DLOG_IF(INFO, nfc_debug_enabled)
1663 << StringPrintf("FeliCa detected (RD, system code %04X)", sc);
1664 if (p_cb->num_system_codes < T3T_MAX_SYSTEM_CODES) {
1665 p_cb->system_codes[p_cb->num_system_codes++] = sc;
1666 } else {
1667 LOG(ERROR) << StringPrintf("Exceed T3T_MAX_SYSTEM_CODES!");
1668 android_errorWriteLog(0x534e4554, "120499324");
1669 }
1670 }
1671
1672 rw_t3t_handle_get_system_codes_cplt();
1673 }
1674
1675 /*****************************************************************************
1676 **
1677 ** Function rw_t3t_handle_ndef_detect_poll_rsp
1678 **
1679 ** Description Handle POLL response for getting system codes
1680 **
1681 ** Returns Nothing
1682 **
1683 *****************************************************************************/
rw_t3t_handle_ndef_detect_poll_rsp(tRW_T3T_CB * p_cb,uint8_t nci_status,uint8_t num_responses)1684 static void rw_t3t_handle_ndef_detect_poll_rsp(tRW_T3T_CB* p_cb,
1685 uint8_t nci_status,
1686 uint8_t num_responses) {
1687 NFC_HDR* p_cmd_buf;
1688 uint8_t *p, *p_cmd_start;
1689 tRW_DATA evt_data;
1690
1691 /* Validate response for NDEF poll */
1692 if ((nci_status == NCI_STATUS_OK) && (num_responses > 0)) {
1693 /* Tag responded for NDEF poll */
1694
1695 /* Read NDEF attribute block */
1696 p_cmd_buf = rw_t3t_get_cmd_buf();
1697 if (p_cmd_buf != nullptr) {
1698 /* Construct T3T message */
1699 p = p_cmd_start = (uint8_t*)(p_cmd_buf + 1) + p_cmd_buf->offset;
1700
1701 /* Add CHECK opcode to message */
1702 UINT8_TO_STREAM(p, T3T_MSG_OPC_CHECK_CMD);
1703
1704 /* Add IDm to message */
1705 ARRAY_TO_STREAM(p, p_cb->peer_nfcid2, NCI_NFCID2_LEN);
1706
1707 /* Add Service code list */
1708 UINT8_TO_STREAM(p, 1); /* Number of services (only 1 service: NDEF) */
1709 UINT16_TO_STREAM(
1710 p, T3T_MSG_NDEF_SC_RO); /* Service code (little-endian format) */
1711
1712 /* Number of blocks */
1713 UINT8_TO_STREAM(
1714 p,
1715 1); /* Number of blocks (only 1 block: NDEF Attribute Information ) */
1716
1717 /* Block List element: the NDEF attribute information block (block 0) */
1718 UINT8_TO_STREAM(p, T3T_MSG_MASK_TWO_BYTE_BLOCK_DESC_FORMAT);
1719 UINT8_TO_STREAM(p, 0);
1720
1721 /* Calculate length of message */
1722 p_cmd_buf->len = (uint16_t)(p - p_cmd_start);
1723
1724 /* Send the T3T message */
1725 evt_data.status = rw_t3t_send_cmd(p_cb, RW_T3T_CMD_DETECT_NDEF, p_cmd_buf,
1726 rw_t3t_check_timeout(1));
1727 if (evt_data.status == NFC_STATUS_OK) {
1728 /* CHECK command sent. Wait for response */
1729 return;
1730 }
1731 }
1732 nci_status = NFC_STATUS_FAILED;
1733 }
1734
1735 /* NDEF detection failed */
1736 p_cb->rw_state = RW_T3T_STATE_IDLE;
1737 evt_data.ndef.status = nci_status;
1738 evt_data.ndef.flags = RW_NDEF_FL_UNKNOWN;
1739 rw_t3t_update_ndef_flag(&evt_data.ndef.flags);
1740 (*(rw_cb.p_cback))(RW_T3T_NDEF_DETECT_EVT, &evt_data);
1741 }
1742
1743 /*****************************************************************************
1744 **
1745 ** Function rw_t3t_update_block
1746 **
1747 ** Description Send UPDATE command for single block
1748 ** (for formatting/configuring read only)
1749 **
1750 ** Returns tNFC_STATUS
1751 **
1752 *****************************************************************************/
rw_t3t_update_block(tRW_T3T_CB * p_cb,uint8_t block_id,uint8_t * p_block_data)1753 tNFC_STATUS rw_t3t_update_block(tRW_T3T_CB* p_cb, uint8_t block_id,
1754 uint8_t* p_block_data) {
1755 uint8_t *p_dst, *p_cmd_start;
1756 NFC_HDR* p_cmd_buf;
1757 tNFC_STATUS status;
1758
1759 p_cmd_buf = rw_t3t_get_cmd_buf();
1760 if (p_cmd_buf != nullptr) {
1761 p_dst = p_cmd_start = (uint8_t*)(p_cmd_buf + 1) + p_cmd_buf->offset;
1762
1763 /* Add UPDATE opcode to message */
1764 UINT8_TO_STREAM(p_dst, T3T_MSG_OPC_UPDATE_CMD);
1765
1766 /* Add IDm to message */
1767 ARRAY_TO_STREAM(p_dst, p_cb->peer_nfcid2, NCI_NFCID2_LEN);
1768
1769 /* Add Service code list */
1770 UINT8_TO_STREAM(p_dst, 1); /* Number of services (only 1 service: NDEF) */
1771 UINT16_TO_STREAM(
1772 p_dst, T3T_MSG_NDEF_SC_RW); /* Service code (little-endian format) */
1773
1774 /* Number of blocks */
1775 UINT8_TO_STREAM(p_dst, 1);
1776
1777 /* Add Block list element for MC */
1778 UINT8_TO_STREAM(p_dst, T3T_MSG_MASK_TWO_BYTE_BLOCK_DESC_FORMAT);
1779 UINT8_TO_STREAM(p_dst, block_id);
1780
1781 /* Copy MC data to UPDATE message */
1782 ARRAY_TO_STREAM(p_dst, p_block_data, T3T_MSG_BLOCKSIZE);
1783
1784 /* Calculate length of message */
1785 p_cmd_buf->len = (uint16_t)(p_dst - p_cmd_start);
1786
1787 /* Send the T3T message */
1788 status = rw_t3t_send_cmd(p_cb, p_cb->cur_cmd, p_cmd_buf,
1789 rw_t3t_update_timeout(1));
1790 } else {
1791 /* Unable to send UPDATE command */
1792 status = NFC_STATUS_NO_BUFFERS;
1793 }
1794
1795 return (status);
1796 }
1797
1798 /*****************************************************************************
1799 **
1800 ** Function rw_t3t_handle_fmt_poll_rsp
1801 **
1802 ** Description Handle POLL response for formatting felica-lite
1803 **
1804 ** Returns Nothing
1805 **
1806 *****************************************************************************/
rw_t3t_handle_fmt_poll_rsp(tRW_T3T_CB * p_cb,uint8_t nci_status,uint8_t num_responses)1807 static void rw_t3t_handle_fmt_poll_rsp(tRW_T3T_CB* p_cb, uint8_t nci_status,
1808 uint8_t num_responses) {
1809 tRW_DATA evt_data;
1810
1811 evt_data.status = NFC_STATUS_OK;
1812
1813 /* Validate response for poll response */
1814 if ((nci_status == NCI_STATUS_OK) && (num_responses > 0)) {
1815 /* Tag responded for Felica-Lite poll */
1816 /* Get MemoryControl block */
1817 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
1818 "Felica-Lite tag detected...getting Memory Control block.");
1819
1820 p_cb->rw_substate = RW_T3T_FMT_SST_CHECK_MC_BLK;
1821
1822 /* Send command to check Memory Configuration block */
1823 evt_data.status = rw_t3t_check_mc_block(p_cb);
1824 } else {
1825 LOG(ERROR) << StringPrintf("Felica-Lite tag not detected");
1826 evt_data.status = NFC_STATUS_FAILED;
1827 }
1828
1829 /* If error, notify upper layer */
1830 if (evt_data.status != NFC_STATUS_OK) {
1831 rw_t3t_format_cplt(evt_data.status);
1832 }
1833 }
1834
1835 /*****************************************************************************
1836 **
1837 ** Function rw_t3t_act_handle_fmt_rsp
1838 **
1839 ** Description Handle response for formatting codes
1840 **
1841 ** Returns Nothing
1842 **
1843 *****************************************************************************/
rw_t3t_act_handle_fmt_rsp(tRW_T3T_CB * p_cb,NFC_HDR * p_msg_rsp)1844 void rw_t3t_act_handle_fmt_rsp(tRW_T3T_CB* p_cb, NFC_HDR* p_msg_rsp) {
1845 uint8_t* p_t3t_rsp = (uint8_t*)(p_msg_rsp + 1) + p_msg_rsp->offset;
1846 uint8_t* p_mc;
1847 tRW_DATA evt_data;
1848
1849 evt_data.status = NFC_STATUS_OK;
1850
1851 /* Check tags's response for reading MemoryControl block */
1852 if (p_cb->rw_substate == RW_T3T_FMT_SST_CHECK_MC_BLK) {
1853 /* Validate response opcode */
1854 if (p_t3t_rsp[T3T_MSG_RSP_OFFSET_RSPCODE] != T3T_MSG_OPC_CHECK_RSP) {
1855 LOG(ERROR) << StringPrintf(
1856 "Response error: expecting rsp_code %02X, but got %02X",
1857 T3T_MSG_OPC_CHECK_RSP, p_t3t_rsp[T3T_MSG_RSP_OFFSET_RSPCODE]);
1858 evt_data.status = NFC_STATUS_FAILED;
1859 }
1860 /* Validate status code and NFCID2 response from tag */
1861 else if ((p_t3t_rsp[T3T_MSG_RSP_OFFSET_STATUS1] !=
1862 T3T_MSG_RSP_STATUS_OK) /* verify response status code */
1863 || (memcmp(p_cb->peer_nfcid2, &p_t3t_rsp[T3T_MSG_RSP_OFFSET_IDM],
1864 NCI_NFCID2_LEN) != 0)) /* verify response IDm */
1865 {
1866 evt_data.status = NFC_STATUS_FAILED;
1867 } else if (p_msg_rsp->len <
1868 (T3T_MSG_RSP_OFFSET_CHECK_DATA + T3T_MSG_BLOCKSIZE)) {
1869 evt_data.status = NFC_STATUS_FAILED;
1870 android_errorWriteLog(0x534e4554, "120506143");
1871 } else {
1872 /* Check if memory configuration (MC) block to see if SYS_OP=1 (NDEF
1873 * enabled) */
1874 p_mc = &p_t3t_rsp[T3T_MSG_RSP_OFFSET_CHECK_DATA]; /* Point to MC data of
1875 CHECK response */
1876
1877 if (p_mc[T3T_MSG_FELICALITE_MC_OFFSET_SYS_OP] != 0x01) {
1878 /* Tag is not currently enabled for NDEF. Indicate that we need to
1879 * update the MC block */
1880
1881 /* Set SYS_OP field to 0x01 (enable NDEF) */
1882 p_mc[T3T_MSG_FELICALITE_MC_OFFSET_SYS_OP] = 0x01;
1883
1884 /* Set RF_PRM field to 0x07 (procedure of issuance) */
1885 p_mc[T3T_MSG_FELICALITE_MC_OFFSET_RF_PRM] = 0x07;
1886
1887 /* Construct and send UPDATE message to write MC block */
1888 p_cb->rw_substate = RW_T3T_FMT_SST_UPDATE_MC_BLK;
1889 evt_data.status =
1890 rw_t3t_update_block(p_cb, T3T_MSG_FELICALITE_BLOCK_ID_MC, p_mc);
1891 } else {
1892 /* SYS_OP=1: ndef already enabled. Just need to update attribute
1893 * information block */
1894 p_cb->rw_substate = RW_T3T_FMT_SST_UPDATE_NDEF_ATTRIB;
1895 evt_data.status =
1896 rw_t3t_update_block(p_cb, 0, (uint8_t*)rw_t3t_default_attrib_info);
1897 }
1898 }
1899
1900 /* If error, notify upper layer */
1901 if (evt_data.status != NFC_STATUS_OK) {
1902 rw_t3t_format_cplt(evt_data.status);
1903 }
1904 } else if (p_cb->rw_substate == RW_T3T_FMT_SST_UPDATE_MC_BLK) {
1905 /* Validate response opcode */
1906 if ((p_t3t_rsp[T3T_MSG_RSP_OFFSET_RSPCODE] != T3T_MSG_OPC_UPDATE_RSP) ||
1907 (p_t3t_rsp[T3T_MSG_RSP_OFFSET_STATUS1] != T3T_MSG_RSP_STATUS_OK))
1908
1909 {
1910 LOG(ERROR) << StringPrintf("Response error: rsp_code=%02X, status=%02X",
1911 p_t3t_rsp[T3T_MSG_RSP_OFFSET_RSPCODE],
1912 p_t3t_rsp[T3T_MSG_RSP_OFFSET_STATUS1]);
1913 evt_data.status = NFC_STATUS_FAILED;
1914 } else {
1915 /* SYS_OP=1: ndef already enabled. Just need to update attribute
1916 * information block */
1917 p_cb->rw_substate = RW_T3T_FMT_SST_UPDATE_NDEF_ATTRIB;
1918 evt_data.status =
1919 rw_t3t_update_block(p_cb, 0, (uint8_t*)rw_t3t_default_attrib_info);
1920 }
1921
1922 /* If error, notify upper layer */
1923 if (evt_data.status != NFC_STATUS_OK) {
1924 rw_t3t_format_cplt(evt_data.status);
1925 }
1926 } else if (p_cb->rw_substate == RW_T3T_FMT_SST_UPDATE_NDEF_ATTRIB) {
1927 /* Validate response opcode */
1928 if ((p_t3t_rsp[T3T_MSG_RSP_OFFSET_RSPCODE] != T3T_MSG_OPC_UPDATE_RSP) ||
1929 (p_t3t_rsp[T3T_MSG_RSP_OFFSET_STATUS1] != T3T_MSG_RSP_STATUS_OK))
1930
1931 {
1932 LOG(ERROR) << StringPrintf("Response error: rsp_code=%02X, status=%02X",
1933 p_t3t_rsp[T3T_MSG_RSP_OFFSET_RSPCODE],
1934 p_t3t_rsp[T3T_MSG_RSP_OFFSET_STATUS1]);
1935 evt_data.status = NFC_STATUS_FAILED;
1936 }
1937
1938 rw_t3t_format_cplt(evt_data.status);
1939 }
1940
1941 GKI_freebuf(p_msg_rsp);
1942 }
1943
1944 /*****************************************************************************
1945 **
1946 ** Function rw_t3t_handle_sro_poll_rsp
1947 **
1948 ** Description Handle POLL response for configuring felica-lite read only
1949 **
1950 ** Returns Nothing
1951 **
1952 *****************************************************************************/
rw_t3t_handle_sro_poll_rsp(tRW_T3T_CB * p_cb,uint8_t nci_status,uint8_t num_responses)1953 static void rw_t3t_handle_sro_poll_rsp(tRW_T3T_CB* p_cb, uint8_t nci_status,
1954 uint8_t num_responses) {
1955 tRW_DATA evt_data;
1956 uint8_t rw_t3t_ndef_attrib_info[T3T_MSG_BLOCKSIZE];
1957 uint8_t* p;
1958 uint8_t tempU8;
1959 uint16_t checksum, i;
1960 uint32_t tempU32 = 0;
1961
1962 evt_data.status = NFC_STATUS_OK;
1963
1964 /* Validate response for poll response */
1965 if ((nci_status == NCI_STATUS_OK) && (num_responses > 0)) {
1966 /* Tag responded for Felica-Lite poll */
1967 if (p_cb->ndef_attrib.rwflag != T3T_MSG_NDEF_RWFLAG_RO) {
1968 /* First update attribute information block */
1969 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
1970 "Felica-Lite tag detected...update NDef attribution block.");
1971
1972 p_cb->rw_substate = RW_T3T_SRO_SST_UPDATE_NDEF_ATTRIB;
1973
1974 p = rw_t3t_ndef_attrib_info;
1975
1976 UINT8_TO_STREAM(p, p_cb->ndef_attrib.version);
1977
1978 /* Update NDEF info */
1979 UINT8_TO_STREAM(
1980 p, p_cb->ndef_attrib.nbr); /* NBr: number of blocks that can be read
1981 using one Check command */
1982 UINT8_TO_STREAM(p, p_cb->ndef_attrib.nbw); /* Nbw: number of blocks that
1983 can be written using one
1984 Update command */
1985 UINT16_TO_BE_STREAM(
1986 p, p_cb->ndef_attrib.nmaxb); /* Nmaxb: maximum number of blocks
1987 available for NDEF data */
1988 UINT32_TO_BE_STREAM(p, tempU32);
1989 UINT8_TO_STREAM(p,
1990 p_cb->ndef_attrib.writef); /* WriteFlag: 00h if writing
1991 data finished; 0Fh if
1992 writing data in progress */
1993 UINT8_TO_STREAM(p, 0x00); /* RWFlag: 00h NDEF is read-only */
1994
1995 tempU8 = (uint8_t)(p_cb->ndef_attrib.ln >> 16);
1996 /* Get length (3-byte, big-endian) */
1997 UINT8_TO_STREAM(p, tempU8); /* Ln: high-byte */
1998 UINT16_TO_BE_STREAM(p, p_cb->ndef_attrib.ln); /* Ln: lo-word */
1999
2000 /* Calculate and append Checksum */
2001 checksum = 0;
2002 for (i = 0; i < T3T_MSG_NDEF_ATTR_INFO_SIZE; i++) {
2003 checksum += rw_t3t_ndef_attrib_info[i];
2004 }
2005 UINT16_TO_BE_STREAM(p, checksum);
2006
2007 evt_data.status =
2008 rw_t3t_update_block(p_cb, 0, (uint8_t*)rw_t3t_ndef_attrib_info);
2009 } else if (p_cb->cur_cmd == RW_T3T_CMD_SET_READ_ONLY_HARD) {
2010 /* NDEF is already read only, Read and update MemoryControl block */
2011 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
2012 "Felica-Lite tag detected...getting Memory Control block.");
2013 p_cb->rw_substate = RW_T3T_SRO_SST_CHECK_MC_BLK;
2014
2015 /* Send command to check Memory Configuration block */
2016 evt_data.status = rw_t3t_check_mc_block(p_cb);
2017 }
2018 } else {
2019 LOG(ERROR) << StringPrintf("Felica-Lite tag not detected");
2020 evt_data.status = NFC_STATUS_FAILED;
2021 }
2022
2023 /* If error, notify upper layer */
2024 if (evt_data.status != NFC_STATUS_OK) {
2025 rw_t3t_set_readonly_cplt(evt_data.status);
2026 }
2027 }
2028
2029 /*****************************************************************************
2030 **
2031 ** Function rw_t3t_act_handle_sro_rsp
2032 **
2033 ** Description Handle response for setting read only codes
2034 **
2035 ** Returns Nothing
2036 **
2037 *****************************************************************************/
rw_t3t_act_handle_sro_rsp(tRW_T3T_CB * p_cb,NFC_HDR * p_msg_rsp)2038 void rw_t3t_act_handle_sro_rsp(tRW_T3T_CB* p_cb, NFC_HDR* p_msg_rsp) {
2039 uint8_t* p_t3t_rsp = (uint8_t*)(p_msg_rsp + 1) + p_msg_rsp->offset;
2040 uint8_t* p_mc;
2041 tRW_DATA evt_data;
2042
2043 evt_data.status = NFC_STATUS_OK;
2044
2045 if (p_cb->rw_substate == RW_T3T_SRO_SST_UPDATE_NDEF_ATTRIB) {
2046 /* Validate response opcode */
2047 if ((p_t3t_rsp[T3T_MSG_RSP_OFFSET_RSPCODE] != T3T_MSG_OPC_UPDATE_RSP) ||
2048 (p_t3t_rsp[T3T_MSG_RSP_OFFSET_STATUS1] != T3T_MSG_RSP_STATUS_OK))
2049
2050 {
2051 LOG(ERROR) << StringPrintf("Response error: rsp_code=%02X, status=%02X",
2052 p_t3t_rsp[T3T_MSG_RSP_OFFSET_RSPCODE],
2053 p_t3t_rsp[T3T_MSG_RSP_OFFSET_STATUS1]);
2054 evt_data.status = NFC_STATUS_FAILED;
2055 } else {
2056 p_cb->ndef_attrib.rwflag = T3T_MSG_NDEF_RWFLAG_RO;
2057 if (p_cb->cur_cmd == RW_T3T_CMD_SET_READ_ONLY_HARD) {
2058 p_cb->rw_substate = RW_T3T_SRO_SST_CHECK_MC_BLK;
2059
2060 /* Send command to check Memory Configuration block */
2061 evt_data.status = rw_t3t_check_mc_block(p_cb);
2062 } else {
2063 rw_t3t_set_readonly_cplt(evt_data.status);
2064 }
2065 }
2066 } else if (p_cb->rw_substate == RW_T3T_SRO_SST_CHECK_MC_BLK) {
2067 /* Check tags's response for reading MemoryControl block, Validate response
2068 * opcode */
2069 if (p_t3t_rsp[T3T_MSG_RSP_OFFSET_RSPCODE] != T3T_MSG_OPC_CHECK_RSP) {
2070 LOG(ERROR) << StringPrintf(
2071 "Response error: expecting rsp_code %02X, but got %02X",
2072 T3T_MSG_OPC_CHECK_RSP, p_t3t_rsp[T3T_MSG_RSP_OFFSET_RSPCODE]);
2073 evt_data.status = NFC_STATUS_FAILED;
2074 }
2075 /* Validate status code and NFCID2 response from tag */
2076 else if ((p_t3t_rsp[T3T_MSG_RSP_OFFSET_STATUS1] !=
2077 T3T_MSG_RSP_STATUS_OK) /* verify response status code */
2078 || (memcmp(p_cb->peer_nfcid2, &p_t3t_rsp[T3T_MSG_RSP_OFFSET_IDM],
2079 NCI_NFCID2_LEN) != 0)) /* verify response IDm */
2080 {
2081 evt_data.status = NFC_STATUS_FAILED;
2082 } else if (p_msg_rsp->len <
2083 (T3T_MSG_RSP_OFFSET_CHECK_DATA + T3T_MSG_BLOCKSIZE)) {
2084 evt_data.status = NFC_STATUS_FAILED;
2085 android_errorWriteLog(0x534e4554, "120506143");
2086 } else {
2087 /* Check if memory configuration (MC) block to see if SYS_OP=1 (NDEF
2088 * enabled) */
2089 p_mc = &p_t3t_rsp[T3T_MSG_RSP_OFFSET_CHECK_DATA]; /* Point to MC data of
2090 CHECK response */
2091
2092 evt_data.status = NFC_STATUS_FAILED;
2093 if (p_mc[T3T_MSG_FELICALITE_MC_OFFSET_SYS_OP] == 0x01) {
2094 /* Set MC_SP field with MC[0] = 0x00 & MC[1] = 0xC0 (Hardlock) to change
2095 * access permission from RW to RO */
2096 p_mc[T3T_MSG_FELICALITE_MC_OFFSET_MC_SP] = 0x00;
2097 /* Not changing the access permission of Subtraction Register and
2098 * MC[0:1] */
2099 p_mc[T3T_MSG_FELICALITE_MC_OFFSET_MC_SP + 1] = 0xC0;
2100
2101 /* Set RF_PRM field to 0x07 (procedure of issuance) */
2102 p_mc[T3T_MSG_FELICALITE_MC_OFFSET_RF_PRM] = 0x07;
2103
2104 /* Construct and send UPDATE message to write MC block */
2105 p_cb->rw_substate = RW_T3T_SRO_SST_UPDATE_MC_BLK;
2106 evt_data.status =
2107 rw_t3t_update_block(p_cb, T3T_MSG_FELICALITE_BLOCK_ID_MC, p_mc);
2108 }
2109 }
2110 } else if (p_cb->rw_substate == RW_T3T_SRO_SST_UPDATE_MC_BLK) {
2111 /* Validate response opcode */
2112 if ((p_t3t_rsp[T3T_MSG_RSP_OFFSET_RSPCODE] != T3T_MSG_OPC_UPDATE_RSP) ||
2113 (p_t3t_rsp[T3T_MSG_RSP_OFFSET_STATUS1] != T3T_MSG_RSP_STATUS_OK))
2114
2115 {
2116 LOG(ERROR) << StringPrintf("Response error: rsp_code=%02X, status=%02X",
2117 p_t3t_rsp[T3T_MSG_RSP_OFFSET_RSPCODE],
2118 p_t3t_rsp[T3T_MSG_RSP_OFFSET_STATUS1]);
2119 evt_data.status = NFC_STATUS_FAILED;
2120 } else {
2121 rw_t3t_set_readonly_cplt(evt_data.status);
2122 }
2123 }
2124
2125 /* If error, notify upper layer */
2126 if (evt_data.status != NFC_STATUS_OK) {
2127 rw_t3t_set_readonly_cplt(evt_data.status);
2128 }
2129
2130 GKI_freebuf(p_msg_rsp);
2131 }
2132
2133 /*******************************************************************************
2134 **
2135 ** Function rw_t3t_data_cback
2136 **
2137 ** Description This callback function receives the data from NFCC.
2138 **
2139 ** Returns none
2140 **
2141 *******************************************************************************/
rw_t3t_data_cback(uint8_t conn_id,tNFC_DATA_CEVT * p_data)2142 void rw_t3t_data_cback(__attribute__((unused)) uint8_t conn_id,
2143 tNFC_DATA_CEVT* p_data) {
2144 tRW_T3T_CB* p_cb = &rw_cb.tcb.t3t;
2145 NFC_HDR* p_msg = p_data->p_data;
2146 bool free_msg = false; /* if TRUE, free msg buffer before returning */
2147 uint8_t *p, sod;
2148
2149 /* Stop rsponse timer */
2150 nfc_stop_quick_timer(&p_cb->timer);
2151
2152 #if (RW_STATS_INCLUDED == TRUE)
2153 /* Update rx stats */
2154 rw_main_update_rx_stats(p_msg->len);
2155 #endif /* RW_STATS_INCLUDED */
2156
2157 /* Check if we are expecting a response */
2158 if (p_cb->rw_state != RW_T3T_STATE_COMMAND_PENDING) {
2159 /*
2160 ** This must be raw frame response
2161 ** send raw frame to app with SoD
2162 */
2163 rw_t3t_act_handle_raw_senddata_rsp(p_cb, p_data);
2164 }
2165 /* Sanity check: verify msg len is big enough to contain t3t header */
2166 else if (p_msg->len < T3T_MSG_RSP_COMMON_HDR_LEN) {
2167 LOG(ERROR) << StringPrintf(
2168 "T3T: invalid Type3 Tag Message (invalid len: %i)", p_msg->len);
2169 free_msg = true;
2170 rw_t3t_process_frame_error();
2171 } else {
2172 /* Check for RF frame error */
2173 p = (uint8_t*)(p_msg + 1) + p_msg->offset;
2174 sod = p[0];
2175
2176 if (p_msg->len < sod || p[sod] != NCI_STATUS_OK) {
2177 LOG(ERROR) << "T3T: rf frame error";
2178 GKI_freebuf(p_msg);
2179 rw_t3t_process_frame_error();
2180 return;
2181 }
2182
2183 /* Skip over SoD */
2184 p_msg->offset++;
2185 p_msg->len--;
2186
2187 /* Get response code */
2188 switch (p_cb->cur_cmd) {
2189 case RW_T3T_CMD_DETECT_NDEF:
2190 rw_t3t_act_handle_ndef_detect_rsp(p_cb, p_msg);
2191 break;
2192
2193 case RW_T3T_CMD_CHECK_NDEF:
2194 rw_t3t_act_handle_check_ndef_rsp(p_cb, p_msg);
2195 break;
2196
2197 case RW_T3T_CMD_UPDATE_NDEF:
2198 rw_t3t_act_handle_update_ndef_rsp(p_cb, p_msg);
2199 break;
2200
2201 case RW_T3T_CMD_CHECK:
2202 rw_t3t_act_handle_check_rsp(p_cb, p_msg);
2203 break;
2204
2205 case RW_T3T_CMD_UPDATE:
2206 rw_t3t_act_handle_update_rsp(p_cb, p_msg);
2207 break;
2208
2209 case RW_T3T_CMD_SEND_RAW_FRAME:
2210 rw_t3t_act_handle_raw_senddata_rsp(p_cb, p_data);
2211 break;
2212
2213 case RW_T3T_CMD_FORMAT:
2214 rw_t3t_act_handle_fmt_rsp(p_cb, p_msg);
2215 break;
2216
2217 case RW_T3T_CMD_SET_READ_ONLY_SOFT:
2218 case RW_T3T_CMD_SET_READ_ONLY_HARD:
2219 rw_t3t_act_handle_sro_rsp(p_cb, p_msg);
2220 break;
2221
2222 default:
2223 GKI_freebuf(p_msg);
2224 break;
2225 }
2226 }
2227
2228 if (free_msg) {
2229 GKI_freebuf(p_msg);
2230 }
2231 }
2232
2233 /*******************************************************************************
2234 **
2235 ** Function rw_t3t_conn_cback
2236 **
2237 ** Description This callback function receives the events/data from NFCC.
2238 **
2239 ** Returns none
2240 **
2241 *******************************************************************************/
rw_t3t_conn_cback(uint8_t conn_id,tNFC_CONN_EVT event,tNFC_CONN * p_data)2242 void rw_t3t_conn_cback(uint8_t conn_id, tNFC_CONN_EVT event,
2243 tNFC_CONN* p_data) {
2244 tRW_T3T_CB* p_cb = &rw_cb.tcb.t3t;
2245 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
2246 "rw_t3t_conn_cback: conn_id=%i, evt=0x%02x", conn_id, event);
2247
2248 /* Only handle NFC_RF_CONN_ID conn_id */
2249 if (conn_id != NFC_RF_CONN_ID) {
2250 return;
2251 }
2252
2253 switch (event) {
2254 case NFC_DEACTIVATE_CEVT:
2255 rw_t3t_unselect();
2256 break;
2257
2258 case NFC_DATA_CEVT: /* check for status in tNFC_CONN */
2259 if ((p_data != nullptr) && ((p_data->data.status == NFC_STATUS_OK) ||
2260 (p_data->data.status == NFC_STATUS_CONTINUE))) {
2261 rw_t3t_data_cback(conn_id, &(p_data->data));
2262 break;
2263 } else if (p_data->data.p_data != nullptr) {
2264 /* Free the response buffer in case of error response */
2265 GKI_freebuf((NFC_HDR*)(p_data->data.p_data));
2266 p_data->data.p_data = nullptr;
2267 }
2268 /* Data event with error status...fall through to NFC_ERROR_CEVT case */
2269 FALLTHROUGH_INTENDED;
2270
2271 case NFC_ERROR_CEVT:
2272 nfc_stop_quick_timer(&p_cb->timer);
2273
2274 #if (RW_STATS_INCLUDED == TRUE)
2275 rw_main_update_trans_error_stats();
2276 #endif /* RW_STATS_INCLUDED */
2277
2278 if (event == NFC_ERROR_CEVT)
2279 rw_t3t_process_error(NFC_STATUS_TIMEOUT);
2280 else if (p_data)
2281 rw_t3t_process_error(p_data->status);
2282 break;
2283
2284 default:
2285 break;
2286 }
2287 }
2288
2289 /*******************************************************************************
2290 **
2291 ** Function rw_t3t_mrti_to_a_b
2292 **
2293 ** Description Converts the given MRTI (Maximum Response Time Information)
2294 ** to the base to calculate timeout value.
2295 ** (The timeout value is a + b * number_blocks)
2296 **
2297 ** Returns NFC_STATUS_OK
2298 **
2299 *******************************************************************************/
rw_t3t_mrti_to_a_b(uint8_t mrti,uint32_t * p_a,uint32_t * p_b)2300 static void rw_t3t_mrti_to_a_b(uint8_t mrti, uint32_t* p_a, uint32_t* p_b) {
2301 uint8_t a, b, e;
2302
2303 a = (mrti & 0x7) + 1; /* A is bit 0 ~ bit 2 */
2304 mrti >>= 3;
2305 b = (mrti & 0x7) + 1; /* B is bit 3 ~ bit 5 */
2306 mrti >>= 3;
2307 e = mrti & 0x3; /* E is bit 6 ~ bit 7 */
2308 *p_a = rw_t3t_mrti_base[e] * a; /* (A+1) * base (i.e T/t3t * 4^E) */
2309 *p_b = rw_t3t_mrti_base[e] * b; /* (B+1) * base (i.e T/t3t * 4^E) */
2310 }
2311
2312 /*******************************************************************************
2313 **
2314 ** Function rw_t3t_select
2315 **
2316 ** Description Called by NFC manager when a Type3 tag has been activated
2317 **
2318 ** Returns NFC_STATUS_OK
2319 **
2320 *******************************************************************************/
rw_t3t_select(uint8_t peer_nfcid2[NCI_RF_F_UID_LEN],uint8_t mrti_check,uint8_t mrti_update)2321 tNFC_STATUS rw_t3t_select(uint8_t peer_nfcid2[NCI_RF_F_UID_LEN],
2322 uint8_t mrti_check, uint8_t mrti_update) {
2323 tRW_T3T_CB* p_cb = &rw_cb.tcb.t3t;
2324
2325 DLOG_IF(INFO, nfc_debug_enabled) << __func__;
2326
2327 memcpy(p_cb->peer_nfcid2, peer_nfcid2,
2328 NCI_NFCID2_LEN); /* Store tag's NFCID2 */
2329 p_cb->ndef_attrib.status =
2330 NFC_STATUS_NOT_INITIALIZED; /* Indicate that NDEF detection has not been
2331 performed yet */
2332 p_cb->rw_state = RW_T3T_STATE_IDLE;
2333 p_cb->flags = 0;
2334 rw_t3t_mrti_to_a_b(mrti_check, &p_cb->check_tout_a, &p_cb->check_tout_b);
2335 rw_t3t_mrti_to_a_b(mrti_update, &p_cb->update_tout_a, &p_cb->update_tout_b);
2336
2337 /* Alloc cmd buf for retransmissions */
2338 if (p_cb->p_cur_cmd_buf == nullptr) {
2339 p_cb->p_cur_cmd_buf = (NFC_HDR*)GKI_getpoolbuf(NFC_RW_POOL_ID);
2340 if (p_cb->p_cur_cmd_buf == nullptr) {
2341 LOG(ERROR) << StringPrintf(
2342 "rw_t3t_select: unable to allocate buffer for retransmission");
2343 p_cb->rw_state = RW_T3T_STATE_NOT_ACTIVATED;
2344 return (NFC_STATUS_FAILED);
2345 }
2346 }
2347
2348 NFC_SetStaticRfCback(rw_t3t_conn_cback);
2349
2350 return NFC_STATUS_OK;
2351 }
2352
2353 /*******************************************************************************
2354 **
2355 ** Function rw_t3t_unselect
2356 **
2357 ** Description Called by NFC manager when a Type3 tag has been de-activated
2358 **
2359 ** Returns NFC_STATUS_OK
2360 **
2361 *******************************************************************************/
rw_t3t_unselect()2362 static tNFC_STATUS rw_t3t_unselect() {
2363 tRW_T3T_CB* p_cb = &rw_cb.tcb.t3t;
2364
2365 #if (RW_STATS_INCLUDED == TRUE)
2366 /* Display stats */
2367 rw_main_log_stats();
2368 #endif /* RW_STATS_INCLUDED */
2369
2370 /* Stop t3t timer (if started) */
2371 nfc_stop_quick_timer(&p_cb->timer);
2372
2373 /* Free cmd buf for retransmissions */
2374 if (p_cb->p_cur_cmd_buf) {
2375 GKI_freebuf(p_cb->p_cur_cmd_buf);
2376 p_cb->p_cur_cmd_buf = nullptr;
2377 }
2378
2379 p_cb->rw_state = RW_T3T_STATE_NOT_ACTIVATED;
2380 NFC_SetStaticRfCback(nullptr);
2381
2382 return NFC_STATUS_OK;
2383 }
2384
2385 /*******************************************************************************
2386 **
2387 ** Function rw_t3t_update_ndef_flag
2388 **
2389 ** Description set additional NDEF Flags for felica lite tag
2390 **
2391 ** Returns updated NDEF Flag value
2392 **
2393 *******************************************************************************/
rw_t3t_update_ndef_flag(uint8_t * p_flag)2394 static void rw_t3t_update_ndef_flag(uint8_t* p_flag) {
2395 tRW_T3T_CB* p_cb = &rw_cb.tcb.t3t;
2396 uint8_t xx;
2397
2398 for (xx = 0; xx < p_cb->num_system_codes; xx++) {
2399 if (p_cb->system_codes[xx] == T3T_SYSTEM_CODE_FELICA_LITE) {
2400 *p_flag &= ~RW_NDEF_FL_UNKNOWN;
2401 *p_flag |= (RW_NDEF_FL_SUPPORTED | RW_NDEF_FL_FORMATABLE);
2402 break;
2403 }
2404 }
2405 }
2406
2407 /*******************************************************************************
2408 **
2409 ** Function rw_t3t_cmd_str
2410 **
2411 ** Description Converts cmd_id to command string for logging
2412 **
2413 ** Returns command string
2414 **
2415 *******************************************************************************/
rw_t3t_cmd_str(uint8_t cmd_id)2416 static std::string rw_t3t_cmd_str(uint8_t cmd_id) {
2417 switch (cmd_id) {
2418 case RW_T3T_CMD_DETECT_NDEF:
2419 return "RW_T3T_CMD_DETECT_NDEF";
2420 case RW_T3T_CMD_CHECK_NDEF:
2421 return "RW_T3T_CMD_CHECK_NDEF";
2422 case RW_T3T_CMD_UPDATE_NDEF:
2423 return "RW_T3T_CMD_UPDATE_NDEF";
2424 case RW_T3T_CMD_CHECK:
2425 return "RW_T3T_CMD_CHECK";
2426 case RW_T3T_CMD_UPDATE:
2427 return "RW_T3T_CMD_UPDATE";
2428 case RW_T3T_CMD_SEND_RAW_FRAME:
2429 return "RW_T3T_CMD_SEND_RAW_FRAME";
2430 case RW_T3T_CMD_GET_SYSTEM_CODES:
2431 return "RW_T3T_CMD_GET_SYSTEM_CODES";
2432 default:
2433 return "Unknown";
2434 }
2435 }
2436
2437 /*******************************************************************************
2438 **
2439 ** Function rw_t3t_state_str
2440 **
2441 ** Description Converts state_id to command string for logging
2442 **
2443 ** Returns command string
2444 **
2445 *******************************************************************************/
rw_t3t_state_str(uint8_t state_id)2446 static std::string rw_t3t_state_str(uint8_t state_id) {
2447 switch (state_id) {
2448 case RW_T3T_STATE_NOT_ACTIVATED:
2449 return "RW_T3T_STATE_NOT_ACTIVATED";
2450 case RW_T3T_STATE_IDLE:
2451 return "RW_T3T_STATE_IDLE";
2452 case RW_T3T_STATE_COMMAND_PENDING:
2453 return "RW_T3T_STATE_COMMAND_PENDING";
2454 default:
2455 return "Unknown";
2456 }
2457 }
2458
2459 /*****************************************************************************
2460 ** Type3 Tag API Functions
2461 *****************************************************************************/
2462
2463 /*****************************************************************************
2464 **
2465 ** Function RW_T3tDetectNDef
2466 **
2467 ** Description
2468 ** This function is used to perform NDEF detection on a Type 3 tag, and
2469 ** retrieve the tag's NDEF attribute information (block 0).
2470 **
2471 ** Before using this API, the application must call RW_SelectTagType to
2472 ** indicate that a Type 3 tag has been activated, and to provide the
2473 ** tag's Manufacture ID (IDm) .
2474 **
2475 ** Returns
2476 ** NFC_STATUS_OK: ndef detection procedure started
2477 ** NFC_STATUS_NO_BUFFERS: unable to allocate a buffer for this operation
2478 ** NFC_STATUS_FAILED: other error
2479 **
2480 *****************************************************************************/
RW_T3tDetectNDef(void)2481 tNFC_STATUS RW_T3tDetectNDef(void) {
2482 tRW_T3T_CB* p_cb = &rw_cb.tcb.t3t;
2483 tNFC_STATUS retval = NFC_STATUS_OK;
2484
2485 DLOG_IF(INFO, nfc_debug_enabled) << __func__;
2486
2487 /* Check if we are in valid state to handle this API */
2488 if (p_cb->rw_state != RW_T3T_STATE_IDLE) {
2489 LOG(ERROR) << StringPrintf("Error: invalid state to handle API (0x%x)",
2490 p_cb->rw_state);
2491 return (NFC_STATUS_FAILED);
2492 }
2493
2494 retval = (tNFC_STATUS)nci_snd_t3t_polling(T3T_SYSTEM_CODE_NDEF, 0, 0);
2495 if (retval == NCI_STATUS_OK) {
2496 p_cb->cur_cmd = RW_T3T_CMD_DETECT_NDEF;
2497 p_cb->cur_tout = RW_T3T_DEFAULT_CMD_TIMEOUT_TICKS;
2498 p_cb->cur_poll_rc = 0;
2499 p_cb->rw_state = RW_T3T_STATE_COMMAND_PENDING;
2500 p_cb->flags |= RW_T3T_FL_W4_NDEF_DETECT_POLL_RSP;
2501
2502 /* start timer for waiting for responses */
2503 rw_t3t_start_poll_timer(p_cb);
2504 }
2505
2506 return (retval);
2507 }
2508
2509 /*****************************************************************************
2510 **
2511 ** Function RW_T3tCheckNDef
2512 **
2513 ** Description
2514 ** Retrieve NDEF contents from a Type3 tag.
2515 **
2516 ** The RW_T3T_CHECK_EVT event is used to notify the application for each
2517 ** segment of NDEF data received. The RW_T3T_CHECK_CPLT_EVT event is used
2518 ** to notify the application all segments have been received.
2519 **
2520 ** Before using this API, the RW_T3tDetectNDef function must be called to
2521 ** verify that the tag contains NDEF data, and to retrieve the NDEF
2522 ** attributes.
2523 **
2524 ** Internally, this command will be separated into multiple Tag 3 Check
2525 ** commands (if necessary) - depending on the tag's Nbr (max number of
2526 ** blocks per read) attribute.
2527 **
2528 ** Returns
2529 ** NFC_STATUS_OK: check command started
2530 ** NFC_STATUS_NO_BUFFERS: unable to allocate a buffer for this operation
2531 ** NFC_STATUS_FAILED: other error
2532 **
2533 *****************************************************************************/
RW_T3tCheckNDef(void)2534 tNFC_STATUS RW_T3tCheckNDef(void) {
2535 tNFC_STATUS retval = NFC_STATUS_OK;
2536 tRW_T3T_CB* p_cb = &rw_cb.tcb.t3t;
2537
2538 DLOG_IF(INFO, nfc_debug_enabled) << __func__;
2539
2540 /* Check if we are in valid state to handle this API */
2541 if (p_cb->rw_state != RW_T3T_STATE_IDLE) {
2542 LOG(ERROR) << StringPrintf("Error: invalid state to handle API (0x%x)",
2543 p_cb->rw_state);
2544 return (NFC_STATUS_FAILED);
2545 } else if (p_cb->ndef_attrib.status !=
2546 NFC_STATUS_OK) /* NDEF detection not performed yet? */
2547 {
2548 LOG(ERROR) << StringPrintf("Error: NDEF detection not performed yet");
2549 return (NFC_STATUS_NOT_INITIALIZED);
2550 } else if (p_cb->ndef_attrib.ln == 0) {
2551 LOG(ERROR) << StringPrintf("Type 3 tag contains empty NDEF message");
2552 return (NFC_STATUS_FAILED);
2553 }
2554
2555 /* Check number of blocks needed for this update */
2556 p_cb->flags &= ~RW_T3T_FL_IS_FINAL_NDEF_SEGMENT;
2557 p_cb->ndef_rx_offset = 0;
2558 retval = rw_t3t_send_next_ndef_check_cmd(p_cb);
2559
2560 return (retval);
2561 }
2562
2563 /*****************************************************************************
2564 **
2565 ** Function RW_T3tUpdateNDef
2566 **
2567 ** Description
2568 ** Write NDEF contents to a Type3 tag.
2569 **
2570 ** The RW_T3T_UPDATE_CPLT_EVT callback event will be used to notify the
2571 ** application of the response.
2572 **
2573 ** Before using this API, the RW_T3tDetectNDef function must be called to
2574 ** verify that the tag contains NDEF data, and to retrieve the NDEF
2575 ** attributes.
2576 **
2577 ** Internally, this command will be separated into multiple Tag 3 Update
2578 ** commands (if necessary) - depending on the tag's Nbw (max number of
2579 ** blocks per write) attribute.
2580 **
2581 ** Returns
2582 ** NFC_STATUS_OK: check command started
2583 ** NFC_STATUS_NO_BUFFERS: unable to allocate a buffer for this operation
2584 ** NFC_STATUS_REFUSED: tag is read-only
2585 ** NFC_STATUS_BUFFER_FULL: len exceeds tag's maximum size
2586 ** NFC_STATUS_FAILED: other error
2587 **
2588 *****************************************************************************/
RW_T3tUpdateNDef(uint32_t len,uint8_t * p_data)2589 tNFC_STATUS RW_T3tUpdateNDef(uint32_t len, uint8_t* p_data) {
2590 tNFC_STATUS retval = NFC_STATUS_OK;
2591 tRW_T3T_CB* p_cb = &rw_cb.tcb.t3t;
2592
2593 DLOG_IF(INFO, nfc_debug_enabled)
2594 << StringPrintf("RW_T3tUpdateNDef (len=%i)", len);
2595
2596 /* Check if we are in valid state to handle this API */
2597 if (p_cb->rw_state != RW_T3T_STATE_IDLE) {
2598 LOG(ERROR) << StringPrintf("Error: invalid state to handle API (0x%x)",
2599 p_cb->rw_state);
2600 return (NFC_STATUS_FAILED);
2601 } else if (p_cb->ndef_attrib.status !=
2602 NFC_STATUS_OK) /* NDEF detection not performed yet? */
2603 {
2604 LOG(ERROR) << StringPrintf("Error: NDEF detection not performed yet");
2605 return (NFC_STATUS_NOT_INITIALIZED);
2606 } else if (len > (((uint32_t)p_cb->ndef_attrib.nmaxb) *
2607 16)) /* Len exceed's tag's NDEF memory? */
2608 {
2609 return (NFC_STATUS_BUFFER_FULL);
2610 } else if (p_cb->ndef_attrib.rwflag ==
2611 T3T_MSG_NDEF_RWFLAG_RO) /* Tag's NDEF memory is read-only? */
2612 {
2613 return (NFC_STATUS_REFUSED);
2614 }
2615
2616 /* Check number of blocks needed for this update */
2617 p_cb->flags &= ~RW_T3T_FL_IS_FINAL_NDEF_SEGMENT;
2618 p_cb->ndef_msg_bytes_sent = 0;
2619 p_cb->ndef_msg_len = len;
2620 p_cb->ndef_msg = p_data;
2621
2622 /* Send initial UPDATE command for NDEF Attribute Info */
2623 retval = rw_t3t_send_update_ndef_attribute_cmd(p_cb, true);
2624
2625 return (retval);
2626 }
2627
2628 /*****************************************************************************
2629 **
2630 ** Function RW_T3tCheck
2631 **
2632 ** Description
2633 ** Read (non-NDEF) contents from a Type3 tag.
2634 **
2635 ** The RW_READ_EVT event is used to notify the application for each
2636 ** segment of NDEF data received. The RW_READ_CPLT_EVT event is used to
2637 ** notify the application all segments have been received.
2638 **
2639 ** Before using this API, the application must call RW_SelectTagType to
2640 ** indicate that a Type 3 tag has been activated, and to provide the
2641 ** tag's Manufacture ID (IDm) .
2642 **
2643 ** Returns
2644 ** NFC_STATUS_OK: check command started
2645 ** NFC_STATUS_NO_BUFFERS: unable to allocate a buffer for this operation
2646 ** NFC_STATUS_FAILED: other error
2647 **
2648 *****************************************************************************/
RW_T3tCheck(uint8_t num_blocks,tT3T_BLOCK_DESC * t3t_blocks)2649 tNFC_STATUS RW_T3tCheck(uint8_t num_blocks, tT3T_BLOCK_DESC* t3t_blocks) {
2650 tNFC_STATUS retval = NFC_STATUS_OK;
2651 tRW_T3T_CB* p_cb = &rw_cb.tcb.t3t;
2652
2653 DLOG_IF(INFO, nfc_debug_enabled)
2654 << StringPrintf("RW_T3tCheck (num_blocks = %i)", num_blocks);
2655
2656 /* Check if we are in valid state to handle this API */
2657 if (p_cb->rw_state != RW_T3T_STATE_IDLE) {
2658 LOG(ERROR) << StringPrintf("Error: invalid state to handle API (0x%x)",
2659 p_cb->rw_state);
2660 return (NFC_STATUS_FAILED);
2661 }
2662
2663 /* Send the CHECK command */
2664 retval = rw_t3t_send_check_cmd(p_cb, num_blocks, t3t_blocks);
2665
2666 return (retval);
2667 }
2668
2669 /*****************************************************************************
2670 **
2671 ** Function RW_T3tUpdate
2672 **
2673 ** Description
2674 ** Write (non-NDEF) contents to a Type3 tag.
2675 **
2676 ** The RW_WRITE_CPLT_EVT event is used to notify the application all
2677 ** segments have been received.
2678 **
2679 ** Before using this API, the application must call RW_SelectTagType to
2680 ** indicate that a Type 3 tag has been activated, and to provide the tag's
2681 ** Manufacture ID (IDm) .
2682 **
2683 ** Returns
2684 ** NFC_STATUS_OK: check command started
2685 ** NFC_STATUS_NO_BUFFERS: unable to allocate a buffer for this operation
2686 ** NFC_STATUS_FAILED: other error
2687 **
2688 *****************************************************************************/
RW_T3tUpdate(uint8_t num_blocks,tT3T_BLOCK_DESC * t3t_blocks,uint8_t * p_data)2689 tNFC_STATUS RW_T3tUpdate(uint8_t num_blocks, tT3T_BLOCK_DESC* t3t_blocks,
2690 uint8_t* p_data) {
2691 tNFC_STATUS retval = NFC_STATUS_OK;
2692 tRW_T3T_CB* p_cb = &rw_cb.tcb.t3t;
2693
2694 DLOG_IF(INFO, nfc_debug_enabled)
2695 << StringPrintf("RW_T3tUpdate (num_blocks = %i)", num_blocks);
2696
2697 /* Check if we are in valid state to handle this API */
2698 if (p_cb->rw_state != RW_T3T_STATE_IDLE) {
2699 LOG(ERROR) << StringPrintf("Error: invalid state to handle API (0x%x)",
2700 p_cb->rw_state);
2701 return (NFC_STATUS_FAILED);
2702 }
2703
2704 /* Send the UPDATE command */
2705 retval = rw_t3t_send_update_cmd(p_cb, num_blocks, t3t_blocks, p_data);
2706
2707 return (retval);
2708 }
2709
2710 /*****************************************************************************
2711 **
2712 ** Function RW_T3tPresenceCheck
2713 **
2714 ** Description
2715 ** Check if the tag is still in the field.
2716 **
2717 ** The RW_T3T_PRESENCE_CHECK_EVT w/ status is used to indicate presence
2718 ** or non-presence.
2719 **
2720 ** Returns
2721 ** NFC_STATUS_OK, if raw data frame sent
2722 ** NFC_STATUS_NO_BUFFERS: unable to allocate a buffer for this operation
2723 ** NFC_STATUS_FAILED: other error
2724 **
2725 *****************************************************************************/
RW_T3tPresenceCheck(void)2726 tNFC_STATUS RW_T3tPresenceCheck(void) {
2727 tNFC_STATUS retval = NFC_STATUS_OK;
2728 tRW_DATA evt_data;
2729 tRW_CB* p_rw_cb = &rw_cb;
2730
2731 DLOG_IF(INFO, nfc_debug_enabled) << __func__;
2732
2733 /* If RW_SelectTagType was not called (no conn_callback) return failure */
2734 if (!(p_rw_cb->p_cback)) {
2735 retval = NFC_STATUS_FAILED;
2736 }
2737 /* If we are not activated, then RW_T3T_PRESENCE_CHECK_EVT status=FAIL */
2738 else if (p_rw_cb->tcb.t3t.rw_state == RW_T3T_STATE_NOT_ACTIVATED) {
2739 evt_data.status = NFC_STATUS_FAILED;
2740 (*p_rw_cb->p_cback)(RW_T3T_PRESENCE_CHECK_EVT, &evt_data);
2741 }
2742 /* If command is pending */
2743 else if (p_rw_cb->tcb.t3t.rw_state == RW_T3T_STATE_COMMAND_PENDING) {
2744 /* If already performing presence check, return error */
2745 if (p_rw_cb->tcb.t3t.flags & RW_T3T_FL_W4_PRESENCE_CHECK_POLL_RSP) {
2746 DLOG_IF(INFO, nfc_debug_enabled)
2747 << StringPrintf("RW_T3tPresenceCheck already in progress");
2748 retval = NFC_STATUS_FAILED;
2749 }
2750 /* If busy with any other command, assume that the tag is present */
2751 else {
2752 evt_data.status = NFC_STATUS_OK;
2753 (*p_rw_cb->p_cback)(RW_T3T_PRESENCE_CHECK_EVT, &evt_data);
2754 }
2755 } else {
2756 /* IDLE state: send POLL command */
2757 retval = (tNFC_STATUS)nci_snd_t3t_polling(0xFFFF, T3T_POLL_RC_SC, 0);
2758 if (retval == NCI_STATUS_OK) {
2759 p_rw_cb->tcb.t3t.flags |= RW_T3T_FL_W4_PRESENCE_CHECK_POLL_RSP;
2760 p_rw_cb->tcb.t3t.rw_state = RW_T3T_STATE_COMMAND_PENDING;
2761 p_rw_cb->tcb.t3t.cur_poll_rc = 0;
2762
2763 /* start timer for waiting for responses */
2764 rw_t3t_start_poll_timer(&p_rw_cb->tcb.t3t);
2765 } else {
2766 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
2767 "RW_T3tPresenceCheck error sending NCI_RF_T3T_POLLING cmd (status = "
2768 "0x%0x)",
2769 retval);
2770 }
2771 }
2772
2773 return (retval);
2774 }
2775
2776 /*****************************************************************************
2777 **
2778 ** Function RW_T3tPoll
2779 **
2780 ** Description
2781 ** Send POLL command
2782 **
2783 ** Returns
2784 ** NFC_STATUS_OK, if raw data frame sent
2785 ** NFC_STATUS_NO_BUFFERS: unable to allocate a buffer for this operation
2786 ** NFC_STATUS_FAILED: other error
2787 **
2788 *****************************************************************************/
RW_T3tPoll(uint16_t system_code,tT3T_POLL_RC rc,uint8_t tsn)2789 tNFC_STATUS RW_T3tPoll(uint16_t system_code, tT3T_POLL_RC rc, uint8_t tsn) {
2790 tNFC_STATUS retval = NFC_STATUS_OK;
2791 tRW_T3T_CB* p_cb = &rw_cb.tcb.t3t;
2792
2793 DLOG_IF(INFO, nfc_debug_enabled) << __func__;
2794
2795 /* Check if we are in valid state to handle this API */
2796 if (p_cb->rw_state != RW_T3T_STATE_IDLE) {
2797 LOG(ERROR) << StringPrintf("Error: invalid state to handle API (0x%x)",
2798 p_cb->rw_state);
2799 return (NFC_STATUS_FAILED);
2800 }
2801
2802 retval = (tNFC_STATUS)nci_snd_t3t_polling(system_code, (uint8_t)rc, tsn);
2803 if (retval == NCI_STATUS_OK) {
2804 /* start timer for waiting for responses */
2805 p_cb->cur_poll_rc = rc;
2806 p_cb->rw_state = RW_T3T_STATE_COMMAND_PENDING;
2807 rw_t3t_start_poll_timer(p_cb);
2808 }
2809
2810 return (retval);
2811 }
2812
2813 /*****************************************************************************
2814 **
2815 ** Function RW_T3tSendRawFrame
2816 **
2817 ** Description
2818 ** This function is called to send a raw data frame to the peer device.
2819 ** When type 3 tag receives response from peer, the callback function
2820 ** will be called with a RW_T3T_RAW_FRAME_EVT [Table 6].
2821 **
2822 ** Before using this API, the application must call RW_SelectTagType to
2823 ** indicate that a Type 3 tag has been activated.
2824 **
2825 ** The raw frame should be a properly formatted Type 3 tag message.
2826 **
2827 ** Returns
2828 ** NFC_STATUS_OK, if raw data frame sent
2829 ** NFC_STATUS_NO_BUFFERS: unable to allocate a buffer for this operation
2830 ** NFC_STATUS_FAILED: other error
2831 **
2832 *****************************************************************************/
RW_T3tSendRawFrame(uint16_t len,uint8_t * p_data)2833 tNFC_STATUS RW_T3tSendRawFrame(uint16_t len, uint8_t* p_data) {
2834 tNFC_STATUS retval = NFC_STATUS_OK;
2835 tRW_T3T_CB* p_cb = &rw_cb.tcb.t3t;
2836
2837 DLOG_IF(INFO, nfc_debug_enabled)
2838 << StringPrintf("RW_T3tSendRawFrame (len = %i)", len);
2839
2840 /* Check if we are in valid state to handle this API */
2841 if (p_cb->rw_state != RW_T3T_STATE_IDLE) {
2842 LOG(ERROR) << StringPrintf("Error: invalid state to handle API (0x%x)",
2843 p_cb->rw_state);
2844 return (NFC_STATUS_FAILED);
2845 }
2846
2847 /* Send the UPDATE command */
2848 retval = rw_t3t_send_raw_frame(p_cb, len, p_data);
2849
2850 return (retval);
2851 }
2852
2853 /*****************************************************************************
2854 **
2855 ** Function RW_T3tGetSystemCodes
2856 **
2857 ** Description
2858 ** Get systems codes supported by the activated tag:
2859 ** Poll for wildcard (FFFF, RC=1):
2860 **
2861 ** Before using this API, the application must call RW_SelectTagType to
2862 ** indicate that a Type 3 tag has been activated.
2863 **
2864 ** Returns
2865 ** NFC_STATUS_OK, if raw data frame sent
2866 ** NFC_STATUS_NO_BUFFERS: unable to allocate a buffer for this operation
2867 ** NFC_STATUS_FAILED: other error
2868 **
2869 *****************************************************************************/
RW_T3tGetSystemCodes(void)2870 tNFC_STATUS RW_T3tGetSystemCodes(void) {
2871 tNFC_STATUS retval = NFC_STATUS_OK;
2872 tRW_T3T_CB* p_cb = &rw_cb.tcb.t3t;
2873
2874 DLOG_IF(INFO, nfc_debug_enabled) << __func__;
2875
2876 /* Check if we are in valid state to handle this API */
2877 if (p_cb->rw_state != RW_T3T_STATE_IDLE) {
2878 LOG(ERROR) << StringPrintf("Error: invalid state to handle API (0x%x)",
2879 p_cb->rw_state);
2880 return (NFC_STATUS_FAILED);
2881 } else {
2882 retval = (tNFC_STATUS)nci_snd_t3t_polling(0xFFFF, T3T_POLL_RC_SC, 0);
2883 if (retval == NCI_STATUS_OK) {
2884 p_cb->cur_cmd = RW_T3T_CMD_GET_SYSTEM_CODES;
2885 p_cb->cur_tout = RW_T3T_DEFAULT_CMD_TIMEOUT_TICKS;
2886 p_cb->cur_poll_rc = T3T_POLL_RC_SC;
2887 p_cb->rw_state = RW_T3T_STATE_COMMAND_PENDING;
2888 p_cb->flags |= RW_T3T_FL_W4_GET_SC_POLL_RSP;
2889 p_cb->num_system_codes = 0;
2890
2891 /* start timer for waiting for responses */
2892 rw_t3t_start_poll_timer(p_cb);
2893 }
2894 }
2895
2896 return (retval);
2897 }
2898
2899 /*****************************************************************************
2900 **
2901 ** Function RW_T3tFormatNDef
2902 **
2903 ** Description
2904 ** Format a type-3 tag for NDEF.
2905 **
2906 ** Only Felica-Lite tags are supported by this API. The
2907 ** RW_T3T_FORMAT_CPLT_EVT is used to notify the status of the operation.
2908 **
2909 ** Returns
2910 ** NFC_STATUS_OK: ndef detection procedure started
2911 ** NFC_STATUS_NO_BUFFERS: unable to allocate a buffer for this operation
2912 ** NFC_STATUS_FAILED: other error
2913 **
2914 *****************************************************************************/
RW_T3tFormatNDef(void)2915 tNFC_STATUS RW_T3tFormatNDef(void) {
2916 tNFC_STATUS retval = NFC_STATUS_OK;
2917 tRW_T3T_CB* p_cb = &rw_cb.tcb.t3t;
2918
2919 DLOG_IF(INFO, nfc_debug_enabled) << __func__;
2920
2921 /* Check if we are in valid state to handle this API */
2922 if (p_cb->rw_state != RW_T3T_STATE_IDLE) {
2923 LOG(ERROR) << StringPrintf("Error: invalid state to handle API (0x%x)",
2924 p_cb->rw_state);
2925 return (NFC_STATUS_FAILED);
2926 } else {
2927 /* Poll tag, to see if Felica-Lite system is supported */
2928 retval = (tNFC_STATUS)nci_snd_t3t_polling(T3T_SYSTEM_CODE_FELICA_LITE,
2929 T3T_POLL_RC_SC, 0);
2930 if (retval == NCI_STATUS_OK) {
2931 p_cb->cur_cmd = RW_T3T_CMD_FORMAT;
2932 p_cb->cur_tout = RW_T3T_DEFAULT_CMD_TIMEOUT_TICKS;
2933 p_cb->cur_poll_rc = T3T_POLL_RC_SC;
2934 p_cb->rw_state = RW_T3T_STATE_COMMAND_PENDING;
2935 p_cb->rw_substate = RW_T3T_FMT_SST_POLL_FELICA_LITE;
2936 p_cb->flags |= RW_T3T_FL_W4_FMT_FELICA_LITE_POLL_RSP;
2937
2938 /* start timer for waiting for responses */
2939 rw_t3t_start_poll_timer(p_cb);
2940 }
2941 }
2942
2943 return (retval);
2944 }
2945
2946 /*****************************************************************************
2947 **
2948 ** Function RW_T3tSetReadOnly
2949 **
2950 ** Description This function performs NDEF read-only procedure
2951 ** Note: Only Felica-Lite tags are supported by this API.
2952 ** RW_T3tDetectNDef() must be called before using this
2953 **
2954 ** The RW_T3T_SET_READ_ONLY_CPLT_EVT event will be returned.
2955 **
2956 ** Returns NFC_STATUS_OK if success
2957 ** NFC_STATUS_FAILED if T3T is busy or other error
2958 **
2959 *****************************************************************************/
RW_T3tSetReadOnly(bool b_hard_lock)2960 tNFC_STATUS RW_T3tSetReadOnly(bool b_hard_lock) {
2961 tNFC_STATUS retval = NFC_STATUS_OK;
2962 tRW_T3T_CB* p_cb = &rw_cb.tcb.t3t;
2963 tRW_DATA evt_data;
2964
2965 DLOG_IF(INFO, nfc_debug_enabled)
2966 << StringPrintf("b_hard_lock=%d", b_hard_lock);
2967
2968 /* Check if we are in valid state to handle this API */
2969 if (p_cb->rw_state != RW_T3T_STATE_IDLE) {
2970 LOG(ERROR) << StringPrintf("Error: invalid state to handle API (0x%x)",
2971 p_cb->rw_state);
2972 return (NFC_STATUS_FAILED);
2973 }
2974
2975 if (p_cb->ndef_attrib.status !=
2976 NFC_STATUS_OK) /* NDEF detection not performed yet? */
2977 {
2978 LOG(ERROR) << StringPrintf("Error: NDEF detection not performed yet");
2979 return (NFC_STATUS_NOT_INITIALIZED);
2980 }
2981
2982 if ((!b_hard_lock) &&
2983 (p_cb->ndef_attrib.rwflag ==
2984 T3T_MSG_NDEF_RWFLAG_RO)) /* Tag's NDEF memory is read-only already */
2985 {
2986 evt_data.status = NFC_STATUS_OK;
2987 (*(rw_cb.p_cback))(RW_T3T_SET_READ_ONLY_CPLT_EVT, &evt_data);
2988 return (retval);
2989 } else {
2990 /* Poll tag, to see if Felica-Lite system is supported */
2991 retval = (tNFC_STATUS)nci_snd_t3t_polling(T3T_SYSTEM_CODE_FELICA_LITE,
2992 T3T_POLL_RC_SC, 0);
2993 if (retval == NCI_STATUS_OK) {
2994 if (b_hard_lock)
2995 p_cb->cur_cmd = RW_T3T_CMD_SET_READ_ONLY_HARD;
2996 else
2997 p_cb->cur_cmd = RW_T3T_CMD_SET_READ_ONLY_SOFT;
2998 p_cb->cur_tout = RW_T3T_DEFAULT_CMD_TIMEOUT_TICKS;
2999 p_cb->cur_poll_rc = T3T_POLL_RC_SC;
3000 p_cb->rw_state = RW_T3T_STATE_COMMAND_PENDING;
3001 p_cb->rw_substate = RW_T3T_SRO_SST_POLL_FELICA_LITE;
3002 p_cb->flags |= RW_T3T_FL_W4_SRO_FELICA_LITE_POLL_RSP;
3003
3004 /* start timer for waiting for responses */
3005 rw_t3t_start_poll_timer(p_cb);
3006 }
3007 }
3008 return (retval);
3009 }
3010