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 1 tag in Reader/Writer
22  *  mode.
23  *
24  ******************************************************************************/
25 #include <string>
26 
27 #include <android-base/stringprintf.h>
28 #include <base/logging.h>
29 
30 #include "nfc_target.h"
31 
32 #include "gki.h"
33 #include "nci_hmsgs.h"
34 #include "nfc_api.h"
35 #include "nfc_int.h"
36 #include "rw_api.h"
37 #include "rw_int.h"
38 
39 using android::base::StringPrintf;
40 
41 extern bool nfc_debug_enabled;
42 extern unsigned char appl_dta_mode_flag;
43 
44 /* Local Functions */
45 static tRW_EVENT rw_t1t_handle_rid_rsp(NFC_HDR* p_pkt);
46 static void rw_t1t_data_cback(uint8_t conn_id, tNFC_CONN_EVT event,
47                               tNFC_CONN* p_data);
48 static void rw_t1t_process_frame_error(void);
49 static void rw_t1t_process_error(void);
50 static void rw_t1t_handle_presence_check_rsp(tNFC_STATUS status);
51 static std::string rw_t1t_get_state_name(uint8_t state);
52 
53 /*******************************************************************************
54 **
55 ** Function         rw_t1t_data_cback
56 **
57 ** Description      This callback function handles data from NFCC.
58 **
59 ** Returns          none
60 **
61 *******************************************************************************/
rw_t1t_data_cback(uint8_t conn_id,tNFC_CONN_EVT event,tNFC_CONN * p_data)62 static void rw_t1t_data_cback(__attribute__((unused)) uint8_t conn_id,
63                               __attribute__((unused)) tNFC_CONN_EVT event,
64                               tNFC_CONN* p_data) {
65   tRW_T1T_CB* p_t1t = &rw_cb.tcb.t1t;
66   tRW_EVENT rw_event = RW_RAW_FRAME_EVT;
67   bool b_notify = true;
68   tRW_DATA evt_data;
69   NFC_HDR* p_pkt;
70   uint8_t* p;
71   tT1T_CMD_RSP_INFO* p_cmd_rsp_info =
72       (tT1T_CMD_RSP_INFO*)rw_cb.tcb.t1t.p_cmd_rsp_info;
73   uint8_t begin_state = p_t1t->state;
74 
75   p_pkt = (NFC_HDR*)(p_data->data.p_data);
76   if (p_pkt == NULL) return;
77   /* Assume the data is just the response byte sequence */
78   p = (uint8_t*)(p_pkt + 1) + p_pkt->offset;
79 
80   DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
81       "state:%s (%d)", rw_t1t_get_state_name(p_t1t->state).c_str(),
82       p_t1t->state);
83 
84   evt_data.status = NFC_STATUS_OK;
85 
86   if ((p_t1t->state == RW_T1T_STATE_IDLE) || (!p_cmd_rsp_info)) {
87     /* If previous command was retransmitted and if response is pending to
88      * previous command retransmission,
89      * check if lenght and ADD/ADD8/ADDS field matches the expected value of
90      * previous
91      * retransmited command response. However, ignore ADD field if the command
92      * was RALL/RID
93      */
94     if ((p_t1t->prev_cmd_rsp_info.pend_retx_rsp) &&
95         (p_t1t->prev_cmd_rsp_info.rsp_len == p_pkt->len) &&
96         ((p_t1t->prev_cmd_rsp_info.op_code == T1T_CMD_RID) ||
97          (p_t1t->prev_cmd_rsp_info.op_code == T1T_CMD_RALL) ||
98          (p_t1t->prev_cmd_rsp_info.addr == *p))) {
99       /* Response to previous command retransmission */
100       LOG(ERROR) << StringPrintf(
101           "T1T Response to previous command in Idle state. command=0x%02x, "
102           "Remaining max retx rsp:0x%02x ",
103           p_t1t->prev_cmd_rsp_info.op_code,
104           p_t1t->prev_cmd_rsp_info.pend_retx_rsp - 1);
105       p_t1t->prev_cmd_rsp_info.pend_retx_rsp--;
106       GKI_freebuf(p_pkt);
107     } else {
108       /* Raw frame event */
109       evt_data.data.p_data = p_pkt;
110       (*rw_cb.p_cback)(RW_T1T_RAW_FRAME_EVT, &evt_data);
111     }
112     return;
113   }
114 
115 #if (RW_STATS_INCLUDED == TRUE)
116   /* Update rx stats */
117   rw_main_update_rx_stats(p_pkt->len);
118 #endif /* RW_STATS_INCLUDED */
119 
120   if ((p_pkt->len != p_cmd_rsp_info->rsp_len) ||
121       ((p_cmd_rsp_info->opcode != T1T_CMD_RALL) &&
122        (p_cmd_rsp_info->opcode != T1T_CMD_RID) && (*p != p_t1t->addr)))
123 
124   {
125     /* If previous command was retransmitted and if response is pending to
126      * previous command retransmission,
127      * then check if lenght and ADD/ADD8/ADDS field matches the expected value
128      * of previous
129      * retransmited command response. However, ignore ADD field if the command
130      * was RALL/RID
131      */
132     if ((p_t1t->prev_cmd_rsp_info.pend_retx_rsp) &&
133         (p_t1t->prev_cmd_rsp_info.rsp_len == p_pkt->len) &&
134         ((p_t1t->prev_cmd_rsp_info.op_code == T1T_CMD_RID) ||
135          (p_t1t->prev_cmd_rsp_info.op_code == T1T_CMD_RALL) ||
136          (p_t1t->prev_cmd_rsp_info.addr == *p))) {
137       LOG(ERROR) << StringPrintf(
138           "T1T Response to previous command. command=0x%02x, Remaining max "
139           "retx rsp:0x%02x",
140           p_t1t->prev_cmd_rsp_info.op_code,
141           p_t1t->prev_cmd_rsp_info.pend_retx_rsp - 1);
142       p_t1t->prev_cmd_rsp_info.pend_retx_rsp--;
143     } else {
144       /* Stop timer as some response to current command is received */
145       nfc_stop_quick_timer(&p_t1t->timer);
146 /* Retrasmit the last sent command if retry-count < max retry */
147       LOG(ERROR) << StringPrintf(
148           "T1T Frame error. state=%s command (opcode) = 0x%02x",
149           rw_t1t_get_state_name(p_t1t->state).c_str(), p_cmd_rsp_info->opcode);
150       rw_t1t_process_frame_error();
151     }
152     GKI_freebuf(p_pkt);
153     return;
154   }
155 
156   /* Stop timer as response to current command is received */
157   nfc_stop_quick_timer(&p_t1t->timer);
158 
159   DLOG_IF(INFO, nfc_debug_enabled)
160       << StringPrintf("RW RECV [%s]:0x%x RSP", t1t_info_to_str(p_cmd_rsp_info),
161                       p_cmd_rsp_info->opcode);
162 
163   /* If we did not receive response to all retransmitted previous command,
164    * dont expect that as response have come for the current command itself.
165    */
166   if (p_t1t->prev_cmd_rsp_info.pend_retx_rsp)
167     memset(&(p_t1t->prev_cmd_rsp_info), 0, sizeof(tRW_T1T_PREV_CMD_RSP_INFO));
168 
169   if (rw_cb.cur_retry) {
170     /* If the current command was retransmitted to get this response, we might
171        get
172        response later to all or some of the retrasnmission of the current
173        command
174      */
175     p_t1t->prev_cmd_rsp_info.addr = ((p_cmd_rsp_info->opcode != T1T_CMD_RALL) &&
176                                      (p_cmd_rsp_info->opcode != T1T_CMD_RID))
177                                         ? p_t1t->addr
178                                         : 0;
179     p_t1t->prev_cmd_rsp_info.rsp_len = p_cmd_rsp_info->rsp_len;
180     p_t1t->prev_cmd_rsp_info.op_code = p_cmd_rsp_info->opcode;
181     p_t1t->prev_cmd_rsp_info.pend_retx_rsp = (uint8_t)rw_cb.cur_retry;
182   }
183 
184   rw_cb.cur_retry = 0;
185 
186   if (p_cmd_rsp_info->opcode == T1T_CMD_RID) {
187     rw_event = rw_t1t_handle_rid_rsp(p_pkt);
188   } else {
189     rw_event =
190         rw_t1t_handle_rsp(p_cmd_rsp_info, &b_notify, p, &evt_data.status);
191   }
192 
193   if (b_notify) {
194     if ((p_t1t->state != RW_T1T_STATE_READ) &&
195         (p_t1t->state != RW_T1T_STATE_WRITE)) {
196       GKI_freebuf(p_pkt);
197       evt_data.data.p_data = NULL;
198     } else {
199       evt_data.data.p_data = p_pkt;
200     }
201     rw_t1t_handle_op_complete();
202     (*rw_cb.p_cback)(rw_event, &evt_data);
203   } else
204     GKI_freebuf(p_pkt);
205 
206   if (begin_state != p_t1t->state) {
207     DLOG_IF(INFO, nfc_debug_enabled)
208         << StringPrintf("RW T1T state changed:<%s> -> <%s>",
209                         rw_t1t_get_state_name(begin_state).c_str(),
210                         rw_t1t_get_state_name(p_t1t->state).c_str());
211   }
212 }
213 
214 /*******************************************************************************
215 **
216 ** Function         rw_t1t_conn_cback
217 **
218 ** Description      This callback function receives the events/data from NFCC.
219 **
220 ** Returns          none
221 **
222 *******************************************************************************/
rw_t1t_conn_cback(uint8_t conn_id,tNFC_CONN_EVT event,tNFC_CONN * p_data)223 void rw_t1t_conn_cback(uint8_t conn_id, tNFC_CONN_EVT event,
224                        tNFC_CONN* p_data) {
225   tRW_T1T_CB* p_t1t = &rw_cb.tcb.t1t;
226 
227   DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
228       "rw_t1t_conn_cback: conn_id=%i, evt=0x%x", conn_id, event);
229   /* Only handle static conn_id */
230   if (conn_id != NFC_RF_CONN_ID) {
231     LOG(WARNING) << StringPrintf(
232         "rw_t1t_conn_cback - Not static connection id: =%i", conn_id);
233     return;
234   }
235 
236   switch (event) {
237     case NFC_CONN_CREATE_CEVT:
238     case NFC_CONN_CLOSE_CEVT:
239       break;
240 
241     case NFC_DEACTIVATE_CEVT:
242 #if (RW_STATS_INCLUDED == TRUE)
243       /* Display stats */
244       rw_main_log_stats();
245 #endif /* RW_STATS_INCLUDED */
246 
247       /* Stop t1t timer (if started) */
248       nfc_stop_quick_timer(&p_t1t->timer);
249 
250       /* Free cmd buf for retransmissions */
251       if (p_t1t->p_cur_cmd_buf) {
252         GKI_freebuf(p_t1t->p_cur_cmd_buf);
253         p_t1t->p_cur_cmd_buf = NULL;
254       }
255 
256       p_t1t->state = RW_T1T_STATE_NOT_ACTIVATED;
257       NFC_SetStaticRfCback(NULL);
258       break;
259 
260     case NFC_DATA_CEVT:
261       if (p_data != NULL) {
262         if (p_data->data.status == NFC_STATUS_OK) {
263           rw_t1t_data_cback(conn_id, event, p_data);
264           break;
265         } else if (p_data->data.p_data != NULL) {
266           /* Free the response buffer in case of error response */
267           GKI_freebuf((NFC_HDR*)(p_data->data.p_data));
268           p_data->data.p_data = NULL;
269         }
270       }
271     /* Data event with error status...fall through to NFC_ERROR_CEVT case */
272 
273     case NFC_ERROR_CEVT:
274       if ((p_t1t->state == RW_T1T_STATE_NOT_ACTIVATED) ||
275           (p_t1t->state == RW_T1T_STATE_IDLE)) {
276 #if (RW_STATS_INCLUDED == TRUE)
277         rw_main_update_trans_error_stats();
278 #endif /* RW_STATS_INCLUDED */
279 
280         tRW_READ_DATA evt_data;
281         if (event == NFC_ERROR_CEVT)
282           evt_data.status = (tNFC_STATUS)(*(uint8_t*)p_data);
283         else if (p_data)
284           evt_data.status = p_data->status;
285         else
286           evt_data.status = NFC_STATUS_FAILED;
287 
288         evt_data.p_data = NULL;
289         tRW_DATA rw_data;
290         rw_data.data = evt_data;
291         (*rw_cb.p_cback)(RW_T1T_INTF_ERROR_EVT, &rw_data);
292         break;
293       }
294       nfc_stop_quick_timer(&p_t1t->timer);
295 
296 #if (RW_STATS_INCLUDED == TRUE)
297       rw_main_update_trans_error_stats();
298 #endif /* RW_STATS_INCLUDED */
299 
300       if (p_t1t->state == RW_T1T_STATE_CHECK_PRESENCE) {
301         rw_t1t_handle_presence_check_rsp(NFC_STATUS_FAILED);
302       } else {
303         rw_t1t_process_error();
304       }
305       break;
306 
307     default:
308       break;
309   }
310 }
311 
312 /*******************************************************************************
313 **
314 ** Function         rw_t1t_send_static_cmd
315 **
316 ** Description      This function composes a Type 1 Tag command for static
317 **                  memory and send through NCI to NFCC.
318 **
319 ** Returns          NFC_STATUS_OK if the command is successfuly sent to NCI
320 **                  otherwise, error status
321 **
322 *******************************************************************************/
rw_t1t_send_static_cmd(uint8_t opcode,uint8_t add,uint8_t dat)323 tNFC_STATUS rw_t1t_send_static_cmd(uint8_t opcode, uint8_t add, uint8_t dat) {
324   tNFC_STATUS status = NFC_STATUS_FAILED;
325   tRW_T1T_CB* p_t1t = &rw_cb.tcb.t1t;
326   const tT1T_CMD_RSP_INFO* p_cmd_rsp_info = t1t_cmd_to_rsp_info(opcode);
327   NFC_HDR* p_data;
328   uint8_t* p;
329 
330   if (p_cmd_rsp_info) {
331     /* a valid opcode for RW */
332     p_data = (NFC_HDR*)GKI_getpoolbuf(NFC_RW_POOL_ID);
333     if (p_data) {
334       p_t1t->p_cmd_rsp_info = (tT1T_CMD_RSP_INFO*)p_cmd_rsp_info;
335       p_t1t->addr = add;
336       p_data->offset = NCI_MSG_OFFSET_SIZE + NCI_DATA_HDR_SIZE;
337       p = (uint8_t*)(p_data + 1) + p_data->offset;
338       UINT8_TO_BE_STREAM(p, opcode);
339       UINT8_TO_BE_STREAM(p, add);
340       UINT8_TO_BE_STREAM(p, dat);
341 
342       ARRAY_TO_STREAM(p, p_t1t->mem, T1T_CMD_UID_LEN);
343       p_data->len = p_cmd_rsp_info->cmd_len;
344 
345       /* Indicate first attempt to send command, back up cmd buffer in case
346        * needed for retransmission */
347       rw_cb.cur_retry = 0;
348       memcpy(p_t1t->p_cur_cmd_buf, p_data,
349              sizeof(NFC_HDR) + p_data->offset + p_data->len);
350 
351 #if (RW_STATS_INCLUDED == TRUE)
352       /* Update stats */
353       rw_main_update_tx_stats(p_data->len, false);
354 #endif /* RW_STATS_INCLUDED */
355 
356       DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
357           "RW SENT [%s]:0x%x CMD", t1t_info_to_str(p_cmd_rsp_info),
358           p_cmd_rsp_info->opcode);
359       status = NFC_SendData(NFC_RF_CONN_ID, p_data);
360       if (status == NFC_STATUS_OK) {
361         nfc_start_quick_timer(
362             &p_t1t->timer, NFC_TTYPE_RW_T1T_RESPONSE,
363             (RW_T1T_TOUT_RESP * QUICK_TIMER_TICKS_PER_SEC) / 1000);
364       }
365     } else {
366       status = NFC_STATUS_NO_BUFFERS;
367     }
368   }
369   return status;
370 }
371 
372 /*******************************************************************************
373 **
374 ** Function         rw_t1t_send_dyn_cmd
375 **
376 ** Description      This function composes a Type 1 Tag command for dynamic
377 **                  memory and send through NCI to NFCC.
378 **
379 ** Returns          NFC_STATUS_OK if the command is successfuly sent to NCI
380 **                  otherwise, error status
381 **
382 *******************************************************************************/
rw_t1t_send_dyn_cmd(uint8_t opcode,uint8_t add,uint8_t * p_dat)383 tNFC_STATUS rw_t1t_send_dyn_cmd(uint8_t opcode, uint8_t add, uint8_t* p_dat) {
384   tNFC_STATUS status = NFC_STATUS_FAILED;
385   tRW_T1T_CB* p_t1t = &rw_cb.tcb.t1t;
386   const tT1T_CMD_RSP_INFO* p_cmd_rsp_info = t1t_cmd_to_rsp_info(opcode);
387   NFC_HDR* p_data;
388   uint8_t* p;
389 
390   if (p_cmd_rsp_info) {
391     /* a valid opcode for RW */
392     p_data = (NFC_HDR*)GKI_getpoolbuf(NFC_RW_POOL_ID);
393     if (p_data) {
394       p_t1t->p_cmd_rsp_info = (tT1T_CMD_RSP_INFO*)p_cmd_rsp_info;
395       p_t1t->addr = add;
396       p_data->offset = NCI_MSG_OFFSET_SIZE + NCI_DATA_HDR_SIZE;
397       p = (uint8_t*)(p_data + 1) + p_data->offset;
398       UINT8_TO_BE_STREAM(p, opcode);
399       UINT8_TO_BE_STREAM(p, add);
400 
401       if (p_dat) {
402         ARRAY_TO_STREAM(p, p_dat, 8);
403       } else {
404         memset(p, 0, 8);
405         p += 8;
406       }
407       ARRAY_TO_STREAM(p, p_t1t->mem, T1T_CMD_UID_LEN);
408       p_data->len = p_cmd_rsp_info->cmd_len;
409 
410       /* Indicate first attempt to send command, back up cmd buffer in case
411        * needed for retransmission */
412       rw_cb.cur_retry = 0;
413       memcpy(p_t1t->p_cur_cmd_buf, p_data,
414              sizeof(NFC_HDR) + p_data->offset + p_data->len);
415 
416 #if (RW_STATS_INCLUDED == TRUE)
417       /* Update stats */
418       rw_main_update_tx_stats(p_data->len, false);
419 #endif /* RW_STATS_INCLUDED */
420 
421       DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
422           "RW SENT [%s]:0x%x CMD", t1t_info_to_str(p_cmd_rsp_info),
423           p_cmd_rsp_info->opcode);
424 
425       status = NFC_SendData(NFC_RF_CONN_ID, p_data);
426       if (status == NFC_STATUS_OK) {
427         nfc_start_quick_timer(
428             &p_t1t->timer, NFC_TTYPE_RW_T1T_RESPONSE,
429             (RW_T1T_TOUT_RESP * QUICK_TIMER_TICKS_PER_SEC) / 1000);
430       }
431     } else {
432       status = NFC_STATUS_NO_BUFFERS;
433     }
434   }
435   return status;
436 }
437 
438 /*****************************************************************************
439 **
440 ** Function         rw_t1t_handle_rid_rsp
441 **
442 ** Description      Handles response to RID: Collects HR, UID, notify up the
443 **                  stack
444 **
445 ** Returns          event to notify application
446 **
447 *****************************************************************************/
rw_t1t_handle_rid_rsp(NFC_HDR * p_pkt)448 static tRW_EVENT rw_t1t_handle_rid_rsp(NFC_HDR* p_pkt) {
449   tRW_T1T_CB* p_t1t = &rw_cb.tcb.t1t;
450   tRW_DATA evt_data;
451   uint8_t* p_rid_rsp;
452 
453   evt_data.status = NFC_STATUS_OK;
454   evt_data.data.p_data = p_pkt;
455 
456   /* Assume the data is just the response byte sequence */
457   p_rid_rsp = (uint8_t*)(p_pkt + 1) + p_pkt->offset;
458 
459   /* Response indicates tag is present */
460   if (p_t1t->state == RW_T1T_STATE_CHECK_PRESENCE) {
461     /* If checking for the presence of the tag then just notify */
462     return RW_T1T_PRESENCE_CHECK_EVT;
463   }
464 
465   /* Extract HR and UID from response */
466   STREAM_TO_ARRAY(p_t1t->hr, p_rid_rsp, T1T_HR_LEN);
467 
468   DLOG_IF(INFO, nfc_debug_enabled)
469       << StringPrintf("hr0:0x%x, hr1:0x%x", p_t1t->hr[0], p_t1t->hr[1]);
470   DLOG_IF(INFO, nfc_debug_enabled)
471       << StringPrintf("UID0-3=%02x%02x%02x%02x", p_rid_rsp[0], p_rid_rsp[1],
472                       p_rid_rsp[2], p_rid_rsp[3]);
473 
474   /* Fetch UID0-3 from RID response message */
475   STREAM_TO_ARRAY(p_t1t->mem, p_rid_rsp, T1T_CMD_UID_LEN);
476 
477   /* Notify RID response Event */
478   return RW_T1T_RID_EVT;
479 }
480 
481 /*******************************************************************************
482 **
483 ** Function         rw_t1t_select
484 **
485 ** Description      This function will set the callback function to
486 **                  receive data from lower layers and also send rid command
487 **
488 ** Returns          none
489 **
490 *******************************************************************************/
rw_t1t_select(uint8_t hr[T1T_HR_LEN],uint8_t uid[T1T_CMD_UID_LEN])491 tNFC_STATUS rw_t1t_select(uint8_t hr[T1T_HR_LEN],
492                           uint8_t uid[T1T_CMD_UID_LEN]) {
493   tNFC_STATUS status = NFC_STATUS_FAILED;
494   tRW_T1T_CB* p_t1t = &rw_cb.tcb.t1t;
495 
496   p_t1t->state = RW_T1T_STATE_NOT_ACTIVATED;
497 
498   /* Alloc cmd buf for retransmissions */
499   if (p_t1t->p_cur_cmd_buf == NULL) {
500     p_t1t->p_cur_cmd_buf = (NFC_HDR*)GKI_getpoolbuf(NFC_RW_POOL_ID);
501     if (p_t1t->p_cur_cmd_buf == NULL) {
502       LOG(ERROR) << StringPrintf(
503           "rw_t1t_select: unable to allocate buffer for retransmission");
504       return status;
505     }
506   }
507 
508   memcpy(p_t1t->hr, hr, T1T_HR_LEN);
509   memcpy(p_t1t->mem, uid, T1T_CMD_UID_LEN);
510 
511   NFC_SetStaticRfCback(rw_t1t_conn_cback);
512 
513   p_t1t->state = RW_T1T_STATE_IDLE;
514 
515   return NFC_STATUS_OK;
516 }
517 
518 /*******************************************************************************
519 **
520 ** Function         rw_t1t_process_timeout
521 **
522 ** Description      process timeout event
523 **
524 ** Returns          none
525 **
526 *******************************************************************************/
rw_t1t_process_timeout(TIMER_LIST_ENT * p_tle)527 void rw_t1t_process_timeout(__attribute__((unused)) TIMER_LIST_ENT* p_tle) {
528   tRW_T1T_CB* p_t1t = &rw_cb.tcb.t1t;
529 
530   LOG(ERROR) << StringPrintf("T1T timeout. state=%s command (opcode)=0x%02x ",
531                              rw_t1t_get_state_name(p_t1t->state).c_str(),
532                              (rw_cb.tcb.t1t.p_cmd_rsp_info)->opcode);
533 
534   if (p_t1t->state == RW_T1T_STATE_CHECK_PRESENCE) {
535     /* Tag has moved from range */
536     rw_t1t_handle_presence_check_rsp(NFC_STATUS_FAILED);
537   } else if (p_t1t->state != RW_T1T_STATE_IDLE) {
538     rw_t1t_process_error();
539   }
540 }
541 
542 /*******************************************************************************
543 **
544 ** Function         rw_t1t_process_frame_error
545 **
546 ** Description      Process frame crc error
547 **
548 ** Returns          none
549 **
550 *******************************************************************************/
rw_t1t_process_frame_error(void)551 static void rw_t1t_process_frame_error(void) {
552 #if (RW_STATS_INCLUDED == TRUE)
553   /* Update stats */
554   rw_main_update_crc_error_stats();
555 #endif /* RW_STATS_INCLUDED */
556 
557   /* Process the error */
558   rw_t1t_process_error();
559 }
560 
561 /*******************************************************************************
562 **
563 ** Function         rw_t1t_process_error
564 **
565 ** Description      process timeout event
566 **
567 ** Returns          none
568 **
569 *******************************************************************************/
rw_t1t_process_error(void)570 static void rw_t1t_process_error(void) {
571   tRW_EVENT rw_event;
572   NFC_HDR* p_cmd_buf;
573   tRW_T1T_CB* p_t1t = &rw_cb.tcb.t1t;
574   tT1T_CMD_RSP_INFO* p_cmd_rsp_info =
575       (tT1T_CMD_RSP_INFO*)rw_cb.tcb.t1t.p_cmd_rsp_info;
576 
577   DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("State: %u", p_t1t->state);
578 
579   /* Retry sending command if retry-count < max */
580   if (rw_cb.cur_retry < RW_MAX_RETRIES) {
581     /* retry sending the command */
582     rw_cb.cur_retry++;
583 
584     DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
585         "T1T retransmission attempt %i of %i", rw_cb.cur_retry, RW_MAX_RETRIES);
586 
587     /* allocate a new buffer for message */
588     p_cmd_buf = (NFC_HDR*)GKI_getpoolbuf(NFC_RW_POOL_ID);
589     if (p_cmd_buf != NULL) {
590       memcpy(p_cmd_buf, p_t1t->p_cur_cmd_buf, sizeof(NFC_HDR) +
591                                                   p_t1t->p_cur_cmd_buf->offset +
592                                                   p_t1t->p_cur_cmd_buf->len);
593 
594 #if (RW_STATS_INCLUDED == TRUE)
595       /* Update stats */
596       rw_main_update_tx_stats(p_cmd_buf->len, true);
597 #endif /* RW_STATS_INCLUDED */
598 
599       if (NFC_SendData(NFC_RF_CONN_ID, p_cmd_buf) == NFC_STATUS_OK) {
600         /* Start timer for waiting for response */
601         nfc_start_quick_timer(
602             &p_t1t->timer, NFC_TTYPE_RW_T1T_RESPONSE,
603             (RW_T1T_TOUT_RESP * QUICK_TIMER_TICKS_PER_SEC) / 1000);
604 
605         return;
606       }
607     }
608   } else {
609     /* we might get response later to all or some of the retrasnmission
610      * of the current command, update previous command response information */
611     DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
612         "T1T maximum retransmission attempts reached (%i)", RW_MAX_RETRIES);
613     p_t1t->prev_cmd_rsp_info.addr = ((p_cmd_rsp_info->opcode != T1T_CMD_RALL) &&
614                                      (p_cmd_rsp_info->opcode != T1T_CMD_RID))
615                                         ? p_t1t->addr
616                                         : 0;
617     p_t1t->prev_cmd_rsp_info.rsp_len = p_cmd_rsp_info->rsp_len;
618     p_t1t->prev_cmd_rsp_info.op_code = p_cmd_rsp_info->opcode;
619     p_t1t->prev_cmd_rsp_info.pend_retx_rsp = RW_MAX_RETRIES;
620   }
621 
622 #if (RW_STATS_INCLUDED == TRUE)
623   /* update failure count */
624   rw_main_update_fail_stats();
625 #endif /* RW_STATS_INCLUDED */
626 
627   rw_event = rw_t1t_info_to_event(p_cmd_rsp_info);
628   if (p_t1t->state != RW_T1T_STATE_NOT_ACTIVATED) rw_t1t_handle_op_complete();
629 
630   if (rw_event == RW_T2T_NDEF_DETECT_EVT) {
631     tRW_DETECT_NDEF_DATA ndef_data;
632     ndef_data.status = NFC_STATUS_TIMEOUT;
633     ndef_data.protocol = NFC_PROTOCOL_T1T;
634     ndef_data.flags = RW_NDEF_FL_UNKNOWN;
635     ndef_data.max_size = 0;
636     ndef_data.cur_size = 0;
637     tRW_DATA rw_data;
638     rw_data.ndef = ndef_data;
639     (*rw_cb.p_cback)(rw_event, &rw_data);
640   } else {
641     tRW_READ_DATA evt_data;
642     evt_data.status = NFC_STATUS_TIMEOUT;
643     evt_data.p_data = NULL;
644     tRW_DATA rw_data;
645     rw_data.data = evt_data;
646     (*rw_cb.p_cback)(rw_event, &rw_data);
647   }
648 }
649 
650 /*****************************************************************************
651 **
652 ** Function         rw_t1t_handle_presence_check_rsp
653 **
654 ** Description      Handle response to presence check
655 **
656 ** Returns          Nothing
657 **
658 *****************************************************************************/
rw_t1t_handle_presence_check_rsp(tNFC_STATUS status)659 void rw_t1t_handle_presence_check_rsp(tNFC_STATUS status) {
660   tRW_DATA rw_data;
661 
662   /* Notify, Tag is present or not */
663   rw_data.data.status = status;
664   rw_t1t_handle_op_complete();
665 
666   (*(rw_cb.p_cback))(RW_T1T_PRESENCE_CHECK_EVT, &rw_data);
667 }
668 
669 /*****************************************************************************
670 **
671 ** Function         rw_t1t_handle_op_complete
672 **
673 ** Description      Reset to IDLE state
674 **
675 ** Returns          Nothing
676 **
677 *****************************************************************************/
rw_t1t_handle_op_complete(void)678 void rw_t1t_handle_op_complete(void) {
679   tRW_T1T_CB* p_t1t = &rw_cb.tcb.t1t;
680 
681   p_t1t->state = RW_T1T_STATE_IDLE;
682 #if (RW_NDEF_INCLUDED == TRUE)
683   if (appl_dta_mode_flag == 0 && (p_t1t->state != RW_T1T_STATE_READ_NDEF)) {
684     p_t1t->b_update = false;
685     p_t1t->b_rseg = false;
686   }
687   p_t1t->substate = RW_T1T_SUBSTATE_NONE;
688 #endif
689   return;
690 }
691 
692 /*****************************************************************************
693 **
694 ** Function         RW_T1tPresenceCheck
695 **
696 ** Description
697 **      Check if the tag is still in the field.
698 **
699 **      The RW_T1T_PRESENCE_CHECK_EVT w/ status is used to indicate presence
700 **      or non-presence.
701 **
702 ** Returns
703 **      NFC_STATUS_OK, if raw data frame sent
704 **      NFC_STATUS_NO_BUFFERS: unable to allocate a buffer for this operation
705 **      NFC_STATUS_FAILED: other error
706 **
707 *****************************************************************************/
RW_T1tPresenceCheck(void)708 tNFC_STATUS RW_T1tPresenceCheck(void) {
709   tNFC_STATUS retval = NFC_STATUS_OK;
710   tRW_DATA evt_data;
711   tRW_CB* p_rw_cb = &rw_cb;
712 
713   DLOG_IF(INFO, nfc_debug_enabled) << __func__;
714 
715   /* If RW_SelectTagType was not called (no conn_callback) return failure */
716   if (!p_rw_cb->p_cback) {
717     retval = NFC_STATUS_FAILED;
718   }
719   /* If we are not activated, then RW_T1T_PRESENCE_CHECK_EVT status=FAIL */
720   else if (p_rw_cb->tcb.t1t.state == RW_T1T_STATE_NOT_ACTIVATED) {
721     evt_data.status = NFC_STATUS_FAILED;
722     (*p_rw_cb->p_cback)(RW_T1T_PRESENCE_CHECK_EVT, &evt_data);
723   }
724   /* If command is pending, assume tag is still present */
725   else if (p_rw_cb->tcb.t1t.state != RW_T1T_STATE_IDLE) {
726     evt_data.status = NFC_STATUS_OK;
727     (*p_rw_cb->p_cback)(RW_T1T_PRESENCE_CHECK_EVT, &evt_data);
728   } else {
729     /* IDLE state: send a RID command to the tag to see if it responds */
730     retval = rw_t1t_send_static_cmd(T1T_CMD_RID, 0, 0);
731     if (retval == NFC_STATUS_OK) {
732       p_rw_cb->tcb.t1t.state = RW_T1T_STATE_CHECK_PRESENCE;
733     }
734   }
735 
736   return (retval);
737 }
738 
739 /*****************************************************************************
740 **
741 ** Function         RW_T1tRid
742 **
743 ** Description
744 **      This function sends a RID command for Reader/Writer mode.
745 **
746 ** Returns
747 **      NFC_STATUS_OK, if raw data frame sent
748 **      NFC_STATUS_NO_BUFFERS: unable to allocate a buffer for this operation
749 **      NFC_STATUS_FAILED: other error
750 **
751 *****************************************************************************/
RW_T1tRid(void)752 tNFC_STATUS RW_T1tRid(void) {
753   tRW_T1T_CB* p_t1t = &rw_cb.tcb.t1t;
754   tNFC_STATUS status = NFC_STATUS_FAILED;
755 
756   DLOG_IF(INFO, nfc_debug_enabled) << __func__;
757 
758   if (p_t1t->state != RW_T1T_STATE_IDLE) {
759     LOG(WARNING) << StringPrintf("RW_T1tRid - Busy - State: %u", p_t1t->state);
760     return (NFC_STATUS_BUSY);
761   }
762 
763   /* send a RID command */
764   status = rw_t1t_send_static_cmd(T1T_CMD_RID, 0, 0);
765   if (status == NFC_STATUS_OK) {
766     p_t1t->state = RW_T1T_STATE_READ;
767   }
768 
769   return (status);
770 }
771 
772 /*******************************************************************************
773 **
774 ** Function         RW_T1tReadAll
775 **
776 ** Description      This function sends a RALL command for Reader/Writer mode.
777 **
778 ** Returns          tNFC_STATUS
779 **
780 *******************************************************************************/
RW_T1tReadAll(void)781 tNFC_STATUS RW_T1tReadAll(void) {
782   tRW_T1T_CB* p_t1t = &rw_cb.tcb.t1t;
783   tNFC_STATUS status = NFC_STATUS_FAILED;
784 
785   DLOG_IF(INFO, nfc_debug_enabled) << __func__;
786 
787   if (p_t1t->state != RW_T1T_STATE_IDLE) {
788     LOG(WARNING) << StringPrintf("RW_T1tReadAll - Busy - State: %u",
789                                  p_t1t->state);
790     return (NFC_STATUS_BUSY);
791   }
792 
793   /* send RALL command */
794   status = rw_t1t_send_static_cmd(T1T_CMD_RALL, 0, 0);
795   if (status == NFC_STATUS_OK) {
796     p_t1t->state = RW_T1T_STATE_READ;
797   }
798 
799   return status;
800 }
801 
802 /*******************************************************************************
803 **
804 ** Function         RW_T1tRead
805 **
806 ** Description      This function sends a READ command for Reader/Writer mode.
807 **
808 ** Returns          tNFC_STATUS
809 **
810 *******************************************************************************/
RW_T1tRead(uint8_t block,uint8_t byte)811 tNFC_STATUS RW_T1tRead(uint8_t block, uint8_t byte) {
812   tNFC_STATUS status = NFC_STATUS_FAILED;
813   tRW_T1T_CB* p_t1t = &rw_cb.tcb.t1t;
814   uint8_t addr;
815 
816   if (p_t1t->state != RW_T1T_STATE_IDLE) {
817     LOG(WARNING) << StringPrintf("RW_T1tRead - Busy - State: %u", p_t1t->state);
818     return (NFC_STATUS_BUSY);
819   }
820 
821   /* send READ command */
822   RW_T1T_BLD_ADD((addr), (block), (byte));
823   status = rw_t1t_send_static_cmd(T1T_CMD_READ, addr, 0);
824   if (status == NFC_STATUS_OK) {
825     p_t1t->state = RW_T1T_STATE_READ;
826   }
827   return status;
828 }
829 
830 /*******************************************************************************
831 **
832 ** Function         RW_T1tWriteErase
833 **
834 ** Description      This function sends a WRITE-E command for Reader/Writer
835 **                  mode.
836 **
837 ** Returns          tNFC_STATUS
838 **
839 *******************************************************************************/
RW_T1tWriteErase(uint8_t block,uint8_t byte,uint8_t new_byte)840 tNFC_STATUS RW_T1tWriteErase(uint8_t block, uint8_t byte, uint8_t new_byte) {
841   tNFC_STATUS status = NFC_STATUS_FAILED;
842   tRW_T1T_CB* p_t1t = &rw_cb.tcb.t1t;
843   uint8_t addr;
844 
845   if (p_t1t->state != RW_T1T_STATE_IDLE) {
846     LOG(WARNING) << StringPrintf("RW_T1tWriteErase - Busy - State: %u",
847                                  p_t1t->state);
848     return (NFC_STATUS_BUSY);
849   }
850   if ((p_t1t->tag_attribute == RW_T1_TAG_ATTRB_READ_ONLY) &&
851       (block != T1T_CC_BLOCK) && (byte != T1T_CC_RWA_OFFSET)) {
852     LOG(ERROR) << StringPrintf("RW_T1tWriteErase - Tag is in Read only state");
853     return (NFC_STATUS_REFUSED);
854   }
855   if ((block >= T1T_STATIC_BLOCKS) || (byte >= T1T_BLOCK_SIZE)) {
856     LOG(ERROR) << StringPrintf("RW_T1tWriteErase - Invalid Block/byte: %u / %u",
857                                block, byte);
858     return (NFC_STATUS_REFUSED);
859   }
860   if ((block == T1T_UID_BLOCK) || (block == T1T_RES_BLOCK)) {
861     LOG(WARNING) << StringPrintf(
862         "RW_T1tWriteErase - Cannot write to Locked block: %u", block);
863     return (NFC_STATUS_REFUSED);
864   }
865   /* send WRITE-E command */
866   RW_T1T_BLD_ADD((addr), (block), (byte));
867   status = rw_t1t_send_static_cmd(T1T_CMD_WRITE_E, addr, new_byte);
868   if (status == NFC_STATUS_OK) {
869     p_t1t->state = RW_T1T_STATE_WRITE;
870     if (block < T1T_BLOCKS_PER_SEGMENT) {
871       p_t1t->b_update = false;
872       p_t1t->b_rseg = false;
873     }
874   }
875   return status;
876 }
877 
878 /*******************************************************************************
879 **
880 ** Function         RW_T1tWriteNoErase
881 **
882 ** Description      This function sends a WRITE-NE command for Reader/Writer
883 **                  mode.
884 **
885 ** Returns          tNFC_STATUS
886 **
887 *******************************************************************************/
RW_T1tWriteNoErase(uint8_t block,uint8_t byte,uint8_t new_byte)888 tNFC_STATUS RW_T1tWriteNoErase(uint8_t block, uint8_t byte, uint8_t new_byte) {
889   tNFC_STATUS status = NFC_STATUS_FAILED;
890   tRW_T1T_CB* p_t1t = &rw_cb.tcb.t1t;
891   uint8_t addr;
892 
893   if (p_t1t->state != RW_T1T_STATE_IDLE) {
894     LOG(WARNING) << StringPrintf("RW_T1tWriteNoErase - Busy - State: %u",
895                                  p_t1t->state);
896     return (NFC_STATUS_BUSY);
897   }
898   if ((p_t1t->tag_attribute == RW_T1_TAG_ATTRB_READ_ONLY) &&
899       (block != T1T_CC_BLOCK) && (byte != T1T_CC_RWA_OFFSET)) {
900     LOG(ERROR) << StringPrintf("RW_T1tWriteErase - Tag is in Read only state");
901     return (NFC_STATUS_REFUSED);
902   }
903   if ((block >= T1T_STATIC_BLOCKS) || (byte >= T1T_BLOCK_SIZE)) {
904     LOG(ERROR) << StringPrintf("RW_T1tWriteErase - Invalid Block/byte: %u / %u",
905                                block, byte);
906     return (NFC_STATUS_REFUSED);
907   }
908   if ((block == T1T_UID_BLOCK) || (block == T1T_RES_BLOCK)) {
909     LOG(WARNING) << StringPrintf(
910         "RW_T1tWriteNoErase - Cannot write to Locked block: %u", block);
911     return (NFC_STATUS_REFUSED);
912   }
913   /* send WRITE-NE command */
914   RW_T1T_BLD_ADD((addr), (block), (byte));
915   status = rw_t1t_send_static_cmd(T1T_CMD_WRITE_NE, addr, new_byte);
916   if (status == NFC_STATUS_OK) {
917     p_t1t->state = RW_T1T_STATE_WRITE;
918     if (block < T1T_BLOCKS_PER_SEGMENT) {
919       p_t1t->b_update = false;
920       p_t1t->b_rseg = false;
921     }
922   }
923   return status;
924 }
925 
926 /*******************************************************************************
927 **
928 ** Function         RW_T1tReadSeg
929 **
930 ** Description      This function sends a RSEG command for Reader/Writer mode.
931 **
932 ** Returns          tNFC_STATUS
933 **
934 *******************************************************************************/
RW_T1tReadSeg(uint8_t segment)935 tNFC_STATUS RW_T1tReadSeg(uint8_t segment) {
936   tNFC_STATUS status = NFC_STATUS_FAILED;
937   tRW_T1T_CB* p_t1t = &rw_cb.tcb.t1t;
938   uint8_t adds;
939 
940   if (p_t1t->state != RW_T1T_STATE_IDLE) {
941     LOG(WARNING) << StringPrintf("RW_T1tReadSeg - Busy - State: %u",
942                                  p_t1t->state);
943     return (NFC_STATUS_BUSY);
944   }
945   if (segment >= T1T_MAX_SEGMENTS) {
946     LOG(ERROR) << StringPrintf("RW_T1tReadSeg - Invalid Segment: %u", segment);
947     return (NFC_STATUS_REFUSED);
948   }
949   if (rw_cb.tcb.t1t.hr[0] != T1T_STATIC_HR0) {
950     /* send RSEG command */
951     RW_T1T_BLD_ADDS((adds), (segment));
952     status = rw_t1t_send_dyn_cmd(T1T_CMD_RSEG, adds, NULL);
953     if (status == NFC_STATUS_OK) {
954       p_t1t->state = RW_T1T_STATE_READ;
955     }
956   }
957   return status;
958 }
959 
960 /*******************************************************************************
961 **
962 ** Function         RW_T1tRead8
963 **
964 ** Description      This function sends a READ8 command for Reader/Writer mode.
965 **
966 ** Returns          tNFC_STATUS
967 **
968 *******************************************************************************/
RW_T1tRead8(uint8_t block)969 tNFC_STATUS RW_T1tRead8(uint8_t block) {
970   tNFC_STATUS status = NFC_STATUS_FAILED;
971   tRW_T1T_CB* p_t1t = &rw_cb.tcb.t1t;
972 
973   if (p_t1t->state != RW_T1T_STATE_IDLE) {
974     LOG(WARNING) << StringPrintf("RW_T1tRead8 - Busy - State: %u",
975                                  p_t1t->state);
976     return (NFC_STATUS_BUSY);
977   }
978 
979   if (rw_cb.tcb.t1t.hr[0] != T1T_STATIC_HR0 ||
980       rw_cb.tcb.t1t.hr[1] >= RW_T1T_HR1_MIN) {
981     /* send READ8 command */
982     status = rw_t1t_send_dyn_cmd(T1T_CMD_READ8, block, NULL);
983     if (status == NFC_STATUS_OK) {
984       p_t1t->state = RW_T1T_STATE_READ;
985     }
986   }
987   return status;
988 }
989 
990 /*******************************************************************************
991 **
992 ** Function         RW_T1tWriteErase8
993 **
994 ** Description      This function sends a WRITE-E8 command for Reader/Writer
995 **                  mode.
996 **
997 ** Returns          tNFC_STATUS
998 **
999 *******************************************************************************/
RW_T1tWriteErase8(uint8_t block,uint8_t * p_new_dat)1000 tNFC_STATUS RW_T1tWriteErase8(uint8_t block, uint8_t* p_new_dat) {
1001   tRW_T1T_CB* p_t1t = &rw_cb.tcb.t1t;
1002   tNFC_STATUS status = NFC_STATUS_FAILED;
1003 
1004   if (p_t1t->state != RW_T1T_STATE_IDLE) {
1005     LOG(WARNING) << StringPrintf("RW_T1tWriteErase8 - Busy - State: %u",
1006                                  p_t1t->state);
1007     return (NFC_STATUS_BUSY);
1008   }
1009 
1010   if ((p_t1t->tag_attribute == RW_T1_TAG_ATTRB_READ_ONLY) &&
1011       (block != T1T_CC_BLOCK)) {
1012     LOG(ERROR) << StringPrintf("RW_T1tWriteErase8 - Tag is in Read only state");
1013     return (NFC_STATUS_REFUSED);
1014   }
1015 
1016   if ((block == T1T_UID_BLOCK) || (block == T1T_RES_BLOCK)) {
1017     LOG(WARNING) << StringPrintf(
1018         "RW_T1tWriteErase8 - Cannot write to Locked block: %u", block);
1019     return (NFC_STATUS_REFUSED);
1020   }
1021 
1022   if (rw_cb.tcb.t1t.hr[0] != T1T_STATIC_HR0 ||
1023       rw_cb.tcb.t1t.hr[1] >= RW_T1T_HR1_MIN) {
1024     /* send WRITE-E8 command */
1025     status = rw_t1t_send_dyn_cmd(T1T_CMD_WRITE_E8, block, p_new_dat);
1026     if (status == NFC_STATUS_OK) {
1027       p_t1t->state = RW_T1T_STATE_WRITE;
1028       if (block < T1T_BLOCKS_PER_SEGMENT) {
1029         p_t1t->b_update = false;
1030         p_t1t->b_rseg = false;
1031       }
1032     }
1033   }
1034   return status;
1035 }
1036 
1037 /*******************************************************************************
1038 **
1039 ** Function         RW_T1tWriteNoErase8
1040 **
1041 ** Description      This function sends a WRITE-NE8 command for Reader/Writer
1042 **                  mode.
1043 **
1044 ** Returns          tNFC_STATUS
1045 **
1046 *******************************************************************************/
RW_T1tWriteNoErase8(uint8_t block,uint8_t * p_new_dat)1047 tNFC_STATUS RW_T1tWriteNoErase8(uint8_t block, uint8_t* p_new_dat) {
1048   tNFC_STATUS status = NFC_STATUS_FAILED;
1049   tRW_T1T_CB* p_t1t = &rw_cb.tcb.t1t;
1050 
1051   if (p_t1t->state != RW_T1T_STATE_IDLE) {
1052     LOG(WARNING) << StringPrintf("RW_T1tWriteNoErase8 - Busy - State: %u",
1053                                  p_t1t->state);
1054     return (NFC_STATUS_BUSY);
1055   }
1056 
1057   if ((p_t1t->tag_attribute == RW_T1_TAG_ATTRB_READ_ONLY) &&
1058       (block != T1T_CC_BLOCK)) {
1059     LOG(ERROR) << StringPrintf(
1060         "RW_T1tWriteNoErase8 - Tag is in Read only state");
1061     return (NFC_STATUS_REFUSED);
1062   }
1063 
1064   if ((block == T1T_UID_BLOCK) || (block == T1T_RES_BLOCK)) {
1065     LOG(WARNING) << StringPrintf(
1066         "RW_T1tWriteNoErase8 - Cannot write to Locked block: %u", block);
1067     return (NFC_STATUS_REFUSED);
1068   }
1069 
1070   if (rw_cb.tcb.t1t.hr[0] != T1T_STATIC_HR0 ||
1071       rw_cb.tcb.t1t.hr[1] >= RW_T1T_HR1_MIN) {
1072     /* send WRITE-NE command */
1073     status = rw_t1t_send_dyn_cmd(T1T_CMD_WRITE_NE8, block, p_new_dat);
1074     if (status == NFC_STATUS_OK) {
1075       p_t1t->state = RW_T1T_STATE_WRITE;
1076       if (block < T1T_BLOCKS_PER_SEGMENT) {
1077         p_t1t->b_update = false;
1078         p_t1t->b_rseg = false;
1079       }
1080     }
1081   }
1082   return status;
1083 }
1084 
1085 /*******************************************************************************
1086 **
1087 ** Function         rw_t1t_get_state_name
1088 **
1089 ** Description      This function returns the state name.
1090 **
1091 ** NOTE             conditionally compiled to save memory.
1092 **
1093 ** Returns          pointer to the name
1094 **
1095 *******************************************************************************/
rw_t1t_get_state_name(uint8_t state)1096 static std::string rw_t1t_get_state_name(uint8_t state) {
1097   switch (state) {
1098     case RW_T1T_STATE_IDLE:
1099       return "IDLE";
1100     case RW_T1T_STATE_NOT_ACTIVATED:
1101       return "NOT_ACTIVATED";
1102     case RW_T1T_STATE_READ:
1103       return "APP_READ";
1104     case RW_T1T_STATE_WRITE:
1105       return "APP_WRITE";
1106     case RW_T1T_STATE_TLV_DETECT:
1107       return "TLV_DETECTION";
1108     case RW_T1T_STATE_READ_NDEF:
1109       return "READING_NDEF";
1110     case RW_T1T_STATE_WRITE_NDEF:
1111       return "WRITING_NDEF";
1112     case RW_T1T_STATE_SET_TAG_RO:
1113       return "SET_TAG_RO";
1114     case RW_T1T_STATE_CHECK_PRESENCE:
1115       return "CHECK_PRESENCE";
1116     case RW_T1T_STATE_FORMAT_TAG:
1117       return "FORMAT_TAG";
1118     default:
1119       return "???? UNKNOWN STATE";
1120   }
1121 }
1122