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 *
22 *  This file contains the implementation for Type 2 tag in Reader/Writer
23 *  mode.
24 *
25 ******************************************************************************/
26#include <string.h>
27#include "nfc_target.h"
28#include "bt_types.h"
29
30#if (NFC_INCLUDED == TRUE)
31#include "nfc_api.h"
32#include "nci_hmsgs.h"
33#include "rw_api.h"
34#include "rw_int.h"
35#include "nfc_int.h"
36#include "gki.h"
37
38/* Static local functions */
39static void rw_t2t_proc_data (UINT8 conn_id, tNFC_DATA_CEVT *p_data);
40static tNFC_STATUS rw_t2t_send_cmd (UINT8 opcode, UINT8 *p_dat);
41static void rw_t2t_process_error (void);
42static void rw_t2t_process_frame_error (void);
43static void rw_t2t_handle_presence_check_rsp (tNFC_STATUS status);
44static void rw_t2t_resume_op (void);
45
46#if (BT_TRACE_VERBOSE == TRUE)
47static char *rw_t2t_get_state_name (UINT8 state);
48static char *rw_t2t_get_substate_name (UINT8 substate);
49#endif
50
51/*******************************************************************************
52**
53** Function         rw_t2t_proc_data
54**
55** Description      This function handles data evt received from NFC Controller.
56**
57** Returns          none
58**
59*******************************************************************************/
60static void rw_t2t_proc_data (UINT8 conn_id, tNFC_DATA_CEVT *p_data)
61{
62    tRW_EVENT               rw_event    = RW_RAW_FRAME_EVT;
63    tRW_T2T_CB              *p_t2t      = &rw_cb.tcb.t2t;
64    BT_HDR                  *p_pkt      = p_data->p_data;
65    BOOLEAN                 b_notify    = TRUE;
66    BOOLEAN                 b_release   = TRUE;
67    UINT8                   *p;
68    tRW_READ_DATA           evt_data = {0};
69    tT2T_CMD_RSP_INFO       *p_cmd_rsp_info = (tT2T_CMD_RSP_INFO *) rw_cb.tcb.t2t.p_cmd_rsp_info;
70    tRW_DETECT_NDEF_DATA    ndef_data;
71#if (BT_TRACE_VERBOSE == TRUE)
72    UINT8                   begin_state     = p_t2t->state;
73#endif
74
75    if (  (p_t2t->state == RW_T2T_STATE_IDLE)
76        ||(p_cmd_rsp_info == NULL)  )
77    {
78
79#if (BT_TRACE_VERBOSE == TRUE)
80        RW_TRACE_DEBUG2 ("RW T2T Raw Frame: Len [0x%X] Status [%s]", p_pkt->len, NFC_GetStatusName (p_data->status));
81#else
82        RW_TRACE_DEBUG2 ("RW T2T Raw Frame: Len [0x%X] Status [0x%X]", p_pkt->len, p_data->status);
83#endif
84        evt_data.status = p_data->status;
85        evt_data.p_data = p_pkt;
86        (*rw_cb.p_cback) (RW_T2T_RAW_FRAME_EVT, (tRW_DATA *)&evt_data);
87        return;
88    }
89#if (defined (RW_STATS_INCLUDED) && (RW_STATS_INCLUDED == TRUE))
90    /* Update rx stats */
91    rw_main_update_rx_stats (p_pkt->len);
92#endif
93    /* Stop timer as response is received */
94    nfc_stop_quick_timer (&p_t2t->t2_timer);
95
96    RW_TRACE_EVENT2 ("RW RECV [%s]:0x%x RSP", t2t_info_to_str (p_cmd_rsp_info), p_cmd_rsp_info->opcode);
97
98    if (  (  (p_pkt->len != p_cmd_rsp_info->rsp_len)
99           &&(p_pkt->len != p_cmd_rsp_info->nack_rsp_len)
100           &&(p_t2t->substate != RW_T2T_SUBSTATE_WAIT_SELECT_SECTOR)  )
101        ||(p_t2t->state == RW_T2T_STATE_HALT)  )
102    {
103#if (BT_TRACE_VERBOSE == TRUE)
104        RW_TRACE_ERROR1 ("T2T Frame error. state=%s ", rw_t2t_get_state_name (p_t2t->state));
105#else
106        RW_TRACE_ERROR1 ("T2T Frame error. state=0x%02X command=0x%02X ", p_t2t->state);
107#endif
108        if (p_t2t->state != RW_T2T_STATE_HALT)
109        {
110            /* Retrasmit the last sent command if retry-count < max retry */
111            rw_t2t_process_frame_error ();
112            p_t2t->check_tag_halt = FALSE;
113        }
114        GKI_freebuf (p_pkt);
115        return;
116    }
117    rw_cb.cur_retry = 0;
118
119    /* Assume the data is just the response byte sequence */
120    p = (UINT8 *) (p_pkt + 1) + p_pkt->offset;
121
122
123    RW_TRACE_EVENT4 ("rw_t2t_proc_data State: %u  conn_id: %u  len: %u  data[0]: 0x%02x",
124                      p_t2t->state, conn_id, p_pkt->len, *p);
125
126    evt_data.p_data     = NULL;
127
128    if (p_t2t->substate == RW_T2T_SUBSTATE_WAIT_SELECT_SECTOR_SUPPORT)
129    {
130        /* The select process happens in two steps */
131        if ((*p & 0x0f) == T2T_RSP_ACK)
132        {
133            if (rw_t2t_sector_change (p_t2t->select_sector) == NFC_STATUS_OK)
134                b_notify = FALSE;
135            else
136                evt_data.status = NFC_STATUS_FAILED;
137        }
138        else
139        {
140            RW_TRACE_EVENT1 ("rw_t2t_proc_data - Received NACK response(0x%x) to SEC-SELCT CMD", (*p & 0x0f));
141            evt_data.status = NFC_STATUS_REJECTED;
142        }
143    }
144    else if (p_t2t->substate == RW_T2T_SUBSTATE_WAIT_SELECT_SECTOR)
145    {
146        evt_data.status = NFC_STATUS_FAILED;
147    }
148    else if (  (p_pkt->len != p_cmd_rsp_info->rsp_len)
149             ||((p_cmd_rsp_info->opcode == T2T_CMD_WRITE) && ((*p & 0x0f) != T2T_RSP_ACK))  )
150    {
151        /* Received NACK response */
152        evt_data.p_data = p_pkt;
153        if (p_t2t->state == RW_T2T_STATE_READ)
154            b_release = FALSE;
155
156        RW_TRACE_EVENT1 ("rw_t2t_proc_data - Received NACK response(0x%x)", (*p & 0x0f));
157
158        if (!p_t2t->check_tag_halt)
159        {
160            /* Just received first NACK. Retry just one time to find if tag went in to HALT State */
161            b_notify =  FALSE;
162            rw_t2t_process_error ();
163            /* Assume Tag is in HALT State, untill we get response to retry command */
164            p_t2t->check_tag_halt = TRUE;
165        }
166        else
167        {
168            p_t2t->check_tag_halt = FALSE;
169            /* Got consecutive NACK so tag not really halt after first NACK, but current operation failed */
170            evt_data.status = NFC_STATUS_FAILED;
171        }
172    }
173    else
174    {
175        /* If the response length indicates positive response or cannot be known from length then assume success */
176        evt_data.status  = NFC_STATUS_OK;
177        p_t2t->check_tag_halt = FALSE;
178
179        /* The response data depends on what the current operation was */
180        switch (p_t2t->state)
181        {
182        case RW_T2T_STATE_CHECK_PRESENCE:
183            b_notify = FALSE;
184            rw_t2t_handle_presence_check_rsp (NFC_STATUS_OK);
185            break;
186
187        case RW_T2T_STATE_READ:
188            evt_data.p_data = p_pkt;
189            b_release = FALSE;
190            if (p_t2t->block_read == 0)
191            {
192                p_t2t->b_read_hdr = TRUE;
193                memcpy (p_t2t->tag_hdr,  p, T2T_READ_DATA_LEN);
194            }
195            break;
196
197        case RW_T2T_STATE_WRITE:
198            /* Write operation completed successfully */
199            break;
200
201        default:
202            /* NDEF/other Tlv Operation/Format-Tag/Config Tag as Read only */
203            b_notify = FALSE;
204            rw_t2t_handle_rsp (p);
205            break;
206        }
207    }
208
209    if (b_notify)
210    {
211        rw_event = rw_t2t_info_to_event (p_cmd_rsp_info);
212
213        if (rw_event == RW_T2T_NDEF_DETECT_EVT)
214        {
215            ndef_data.status    = evt_data.status;
216            ndef_data.protocol  = NFC_PROTOCOL_T2T;
217            ndef_data.flags     = RW_NDEF_FL_UNKNOWN;
218            if (p_t2t->substate == RW_T2T_SUBSTATE_WAIT_READ_LOCKS)
219                ndef_data.flags = RW_NDEF_FL_FORMATED;
220            ndef_data.max_size  = 0;
221            ndef_data.cur_size  = 0;
222            /* Move back to idle state */
223            rw_t2t_handle_op_complete ();
224            (*rw_cb.p_cback) (rw_event, (tRW_DATA *) &ndef_data);
225        }
226        else
227        {
228            /* Move back to idle state */
229            rw_t2t_handle_op_complete ();
230            (*rw_cb.p_cback) (rw_event, (tRW_DATA *) &evt_data);
231        }
232    }
233
234    if (b_release)
235        GKI_freebuf (p_pkt);
236
237#if (BT_TRACE_VERBOSE == TRUE)
238    if (begin_state != p_t2t->state)
239    {
240        RW_TRACE_DEBUG2 ("RW T2T state changed:<%s> -> <%s>",
241                          rw_t2t_get_state_name (begin_state),
242                          rw_t2t_get_state_name (p_t2t->state));
243    }
244#endif
245}
246
247/*******************************************************************************
248**
249** Function         rw_t2t_conn_cback
250**
251** Description      This callback function receives events/data from NFCC.
252**
253** Returns          none
254**
255*******************************************************************************/
256void rw_t2t_conn_cback (UINT8 conn_id, tNFC_CONN_EVT event, tNFC_CONN *p_data)
257{
258    tRW_T2T_CB  *p_t2t  = &rw_cb.tcb.t2t;
259    tRW_READ_DATA       evt_data;
260
261    RW_TRACE_DEBUG2 ("rw_t2t_conn_cback: conn_id=%i, evt=%i", conn_id, event);
262    /* Only handle static conn_id */
263    if (conn_id != NFC_RF_CONN_ID)
264    {
265        return;
266    }
267
268    switch (event)
269    {
270    case NFC_CONN_CREATE_CEVT:
271    case NFC_CONN_CLOSE_CEVT:
272        break;
273
274    case NFC_DEACTIVATE_CEVT:
275#if (defined (RW_STATS_INCLUDED) && (RW_STATS_INCLUDED == TRUE))
276        /* Display stats */
277        rw_main_log_stats ();
278#endif
279        /* Stop t2t timer (if started) */
280        nfc_stop_quick_timer (&p_t2t->t2_timer);
281
282        /* Free cmd buf for retransmissions */
283        if (p_t2t->p_cur_cmd_buf)
284        {
285            GKI_freebuf (p_t2t->p_cur_cmd_buf);
286            p_t2t->p_cur_cmd_buf = NULL;
287        }
288        /* Free cmd buf used to hold command before sector change */
289        if (p_t2t->p_sec_cmd_buf)
290        {
291            GKI_freebuf (p_t2t->p_sec_cmd_buf);
292            p_t2t->p_sec_cmd_buf = NULL;
293        }
294
295        p_t2t->state = RW_T2T_STATE_NOT_ACTIVATED;
296        NFC_SetStaticRfCback (NULL);
297        break;
298
299    case NFC_DATA_CEVT:
300        if (  (p_data != NULL)
301            &&(  (p_data->data.status == NFC_STATUS_OK)
302               ||(p_data->data.status == NFC_STATUS_CONTINUE)  )  )
303        {
304            rw_t2t_proc_data (conn_id, &(p_data->data));
305            break;
306        }
307        /* Data event with error status...fall through to NFC_ERROR_CEVT case */
308
309    case NFC_ERROR_CEVT:
310        if (  (p_t2t->state == RW_T2T_STATE_NOT_ACTIVATED)
311            ||(p_t2t->state == RW_T2T_STATE_IDLE)
312            ||(p_t2t->state == RW_T2T_STATE_HALT)  )
313        {
314#if (defined (RW_STATS_INCLUDED) && (RW_STATS_INCLUDED == TRUE))
315            rw_main_update_trans_error_stats ();
316#endif  /* RW_STATS_INCLUDED */
317            if (event == NFC_ERROR_CEVT)
318                evt_data.status = (tNFC_STATUS) (*(UINT8*) p_data);
319            else if (p_data)
320                evt_data.status = p_data->status;
321            else
322                evt_data.status = NFC_STATUS_FAILED;
323
324            evt_data.p_data = NULL;
325            (*rw_cb.p_cback) (RW_T2T_INTF_ERROR_EVT, (tRW_DATA *) &evt_data);
326            break;
327        }
328        nfc_stop_quick_timer (&p_t2t->t2_timer);
329#if (defined (RW_STATS_INCLUDED) && (RW_STATS_INCLUDED == TRUE))
330        rw_main_update_trans_error_stats ();
331#endif
332        if (p_t2t->state == RW_T2T_STATE_CHECK_PRESENCE)
333        {
334            if (p_t2t->check_tag_halt)
335            {
336                p_t2t->state = RW_T2T_STATE_HALT;
337                rw_t2t_handle_presence_check_rsp (NFC_STATUS_REJECTED);
338            }
339            else
340            {
341                /* Move back to idle state */
342                rw_t2t_handle_presence_check_rsp (NFC_STATUS_FAILED);
343            }
344        }
345        else
346        {
347            rw_t2t_process_error ();
348        }
349        break;
350
351    default:
352        break;
353
354    }
355}
356
357/*******************************************************************************
358**
359** Function         rw_t2t_send_cmd
360**
361** Description      This function composes a Type 2 Tag command and send it via
362**                  NCI to NFCC.
363**
364** Returns          NFC_STATUS_OK if the command is successfuly sent to NCI
365**                  otherwise, error status
366**
367*******************************************************************************/
368tNFC_STATUS rw_t2t_send_cmd (UINT8 opcode, UINT8 *p_dat)
369{
370    tNFC_STATUS             status  = NFC_STATUS_FAILED;
371    tRW_T2T_CB              *p_t2t  = &rw_cb.tcb.t2t;
372    const tT2T_CMD_RSP_INFO *p_cmd_rsp_info = t2t_cmd_to_rsp_info (opcode);
373    BT_HDR                  *p_data;
374    UINT8                   *p;
375
376    if (p_cmd_rsp_info)
377    {
378        /* a valid opcode for RW */
379        p_data = (BT_HDR *) GKI_getpoolbuf (NFC_RW_POOL_ID);
380        if (p_data)
381        {
382            p_t2t->p_cmd_rsp_info   = (tT2T_CMD_RSP_INFO *) p_cmd_rsp_info;
383            p_data->offset  = NCI_MSG_OFFSET_SIZE + NCI_DATA_HDR_SIZE;
384            p               = (UINT8 *) (p_data + 1) + p_data->offset;
385
386            UINT8_TO_STREAM (p, opcode);
387
388            if (p_dat)
389            {
390                ARRAY_TO_STREAM (p, p_dat, (p_cmd_rsp_info->cmd_len - 1));
391            }
392
393            p_data->len     = p_cmd_rsp_info->cmd_len;
394
395            /* Indicate first attempt to send command, back up cmd buffer in case needed for retransmission */
396            rw_cb.cur_retry = 0;
397            memcpy (p_t2t->p_cur_cmd_buf, p_data, sizeof (BT_HDR) + p_data->offset + p_data->len);
398
399#if (defined (RW_STATS_INCLUDED) && (RW_STATS_INCLUDED == TRUE))
400            /* Update stats */
401            rw_main_update_tx_stats (p_data->len, FALSE);
402#endif
403            RW_TRACE_EVENT2 ("RW SENT [%s]:0x%x CMD", t2t_info_to_str (p_cmd_rsp_info), p_cmd_rsp_info->opcode);
404
405            if ((status = NFC_SendData (NFC_RF_CONN_ID, p_data)) == NFC_STATUS_OK)
406            {
407                nfc_start_quick_timer (&p_t2t->t2_timer, NFC_TTYPE_RW_T2T_RESPONSE,
408                       (RW_T2T_TOUT_RESP*QUICK_TIMER_TICKS_PER_SEC) / 1000);
409            }
410            else
411            {
412#if (BT_TRACE_VERBOSE == TRUE)
413                RW_TRACE_ERROR2 ("T2T NFC Send data failed. state=%s substate=%s ", rw_t2t_get_state_name (p_t2t->state), rw_t2t_get_substate_name (p_t2t->substate));
414#else
415                RW_TRACE_ERROR2 ("T2T NFC Send data failed. state=0x%02X substate=0x%02X ", p_t2t->state, p_t2t->substate);
416#endif
417            }
418        }
419        else
420        {
421            status = NFC_STATUS_NO_BUFFERS;
422        }
423    }
424    return status;
425}
426
427/*******************************************************************************
428**
429** Function         rw_t2t_process_timeout
430**
431** Description      handles timeout event
432**
433** Returns          none
434**
435*******************************************************************************/
436void rw_t2t_process_timeout (TIMER_LIST_ENT *p_tle)
437{
438    tRW_READ_DATA       evt_data;
439    tRW_T2T_CB          *p_t2t          = &rw_cb.tcb.t2t;
440
441    if (p_t2t->state == RW_T2T_STATE_CHECK_PRESENCE)
442    {
443        if (p_t2t->check_tag_halt)
444        {
445            p_t2t->state = RW_T2T_STATE_HALT;
446            rw_t2t_handle_presence_check_rsp (NFC_STATUS_REJECTED);
447        }
448        else
449        {
450            /* Move back to idle state */
451            rw_t2t_handle_presence_check_rsp (NFC_STATUS_FAILED);
452        }
453        return;
454    }
455
456    if (p_t2t->substate == RW_T2T_SUBSTATE_WAIT_SELECT_SECTOR)
457    {
458        p_t2t->sector   = p_t2t->select_sector;
459        /* Here timeout is an acknowledgment for successfull sector change */
460        if (p_t2t->state == RW_T2T_STATE_SELECT_SECTOR)
461        {
462            /* Notify that select sector op is successfull */
463            rw_t2t_handle_op_complete ();
464            evt_data.status = NFC_STATUS_OK;
465            evt_data.p_data = NULL;
466            (*rw_cb.p_cback) (RW_T2T_SELECT_CPLT_EVT, (tRW_DATA *) &evt_data);
467        }
468        else
469        {
470            /* Resume operation from where we stopped before sector change */
471            rw_t2t_resume_op ();
472        }
473    }
474    else if (p_t2t->state != RW_T2T_STATE_IDLE)
475    {
476#if (BT_TRACE_VERBOSE == TRUE)
477        RW_TRACE_ERROR1 ("T2T timeout. state=%s ", rw_t2t_get_state_name (p_t2t->state));
478#else
479        RW_TRACE_ERROR1 ("T2T timeout. state=0x%02X ", p_t2t->state);
480#endif
481        /* Handle timeout error as no response to the command sent */
482        rw_t2t_process_error ();
483    }
484}
485
486/*******************************************************************************
487**
488** Function         rw_t2t_process_frame_error
489**
490** Description      handles frame crc error
491**
492** Returns          none
493**
494*******************************************************************************/
495static void rw_t2t_process_frame_error (void)
496{
497#if (defined (RW_STATS_INCLUDED) && (RW_STATS_INCLUDED == TRUE))
498    /* Update stats */
499    rw_main_update_crc_error_stats ();
500#endif
501    /* Process the error */
502    rw_t2t_process_error ();
503}
504
505/*******************************************************************************
506**
507** Function         rw_t2t_process_error
508**
509** Description      Process error including Timeout, Frame error. This function
510**                  will retry atleast till RW_MAX_RETRIES before give up and
511**                  sending negative notification to upper layer
512**
513** Returns          none
514**
515*******************************************************************************/
516static void rw_t2t_process_error (void)
517{
518    tRW_READ_DATA           evt_data;
519    tRW_EVENT               rw_event;
520    BT_HDR                  *p_cmd_buf;
521    tRW_T2T_CB              *p_t2t          = &rw_cb.tcb.t2t;
522    tT2T_CMD_RSP_INFO       *p_cmd_rsp_info = (tT2T_CMD_RSP_INFO *) rw_cb.tcb.t2t.p_cmd_rsp_info;
523    tRW_DETECT_NDEF_DATA    ndef_data;
524
525    RW_TRACE_DEBUG1 ("rw_t2t_process_error () State: %u", p_t2t->state);
526
527    /* Retry sending command if retry-count < max */
528    if (  (!p_t2t->check_tag_halt)
529        &&(rw_cb.cur_retry < RW_MAX_RETRIES)  )
530    {
531        /* retry sending the command */
532        rw_cb.cur_retry++;
533
534        RW_TRACE_DEBUG2 ("T2T retransmission attempt %i of %i", rw_cb.cur_retry, RW_MAX_RETRIES);
535
536        /* allocate a new buffer for message */
537        if ((p_cmd_buf = (BT_HDR *) GKI_getpoolbuf (NFC_RW_POOL_ID)) != NULL)
538        {
539            memcpy (p_cmd_buf, p_t2t->p_cur_cmd_buf, sizeof (BT_HDR) + p_t2t->p_cur_cmd_buf->offset + p_t2t->p_cur_cmd_buf->len);
540#if (defined (RW_STATS_INCLUDED) && (RW_STATS_INCLUDED == TRUE))
541            /* Update stats */
542            rw_main_update_tx_stats (p_cmd_buf->len, TRUE);
543#endif
544            if (NFC_SendData (NFC_RF_CONN_ID, p_cmd_buf) == NFC_STATUS_OK)
545            {
546                /* Start timer for waiting for response */
547                nfc_start_quick_timer (&p_t2t->t2_timer, NFC_TTYPE_RW_T2T_RESPONSE,
548                                       (RW_T2T_TOUT_RESP * QUICK_TIMER_TICKS_PER_SEC) / 1000);
549
550                return;
551            }
552        }
553    }
554    else
555    {
556        if (p_t2t->check_tag_halt)
557        {
558            RW_TRACE_DEBUG0 ("T2T Went to HALT State!");
559        }
560        else
561        {
562            RW_TRACE_DEBUG1 ("T2T maximum retransmission attempts reached (%i)", RW_MAX_RETRIES);
563        }
564    }
565    rw_event = rw_t2t_info_to_event (p_cmd_rsp_info);
566#if (defined (RW_STATS_INCLUDED) && (RW_STATS_INCLUDED == TRUE))
567    /* update failure count */
568    rw_main_update_fail_stats ();
569#endif
570    if (p_t2t->check_tag_halt)
571    {
572        evt_data.status = NFC_STATUS_REJECTED;
573        p_t2t->state    = RW_T2T_STATE_HALT;
574    }
575    else
576    {
577        evt_data.status = NFC_STATUS_TIMEOUT;
578    }
579
580    if (rw_event == RW_T2T_NDEF_DETECT_EVT)
581    {
582        ndef_data.status    = evt_data.status;
583        ndef_data.protocol  = NFC_PROTOCOL_T2T;
584        ndef_data.flags     = RW_NDEF_FL_UNKNOWN;
585        if (p_t2t->substate == RW_T2T_SUBSTATE_WAIT_READ_LOCKS)
586            ndef_data.flags = RW_NDEF_FL_FORMATED;
587        ndef_data.max_size  = 0;
588        ndef_data.cur_size  = 0;
589        /* If not Halt move to idle state */
590        rw_t2t_handle_op_complete ();
591
592        (*rw_cb.p_cback) (rw_event, (tRW_DATA *) &ndef_data);
593    }
594    else
595    {
596        evt_data.p_data = NULL;
597        /* If activated and not Halt move to idle state */
598        if (p_t2t->state != RW_T2T_STATE_NOT_ACTIVATED)
599            rw_t2t_handle_op_complete ();
600
601        p_t2t->substate = RW_T2T_SUBSTATE_NONE;
602        (*rw_cb.p_cback) (rw_event, (tRW_DATA *) &evt_data);
603    }
604}
605
606/*****************************************************************************
607**
608** Function         rw_t2t_handle_presence_check_rsp
609**
610** Description      Handle response to presence check
611**
612** Returns          Nothing
613**
614*****************************************************************************/
615void rw_t2t_handle_presence_check_rsp (tNFC_STATUS status)
616{
617    tRW_READ_DATA   evt_data;
618
619    /* Notify, Tag is present or not */
620    evt_data.status = status;
621    rw_t2t_handle_op_complete ();
622
623    (*rw_cb.p_cback) (RW_T2T_PRESENCE_CHECK_EVT, (tRW_DATA *) &evt_data);
624}
625
626/*******************************************************************************
627**
628** Function         rw_t2t_resume_op
629**
630** Description      This function will continue operation after moving to new
631**                  sector
632**
633** Returns          tNFC_STATUS
634**
635*******************************************************************************/
636static void rw_t2t_resume_op (void)
637{
638    tRW_T2T_CB          *p_t2t = &rw_cb.tcb.t2t;
639    tRW_READ_DATA       evt_data;
640    BT_HDR              *p_cmd_buf;
641    tRW_EVENT           event;
642    const tT2T_CMD_RSP_INFO   *p_cmd_rsp_info = (tT2T_CMD_RSP_INFO *) rw_cb.tcb.t2t.p_cmd_rsp_info;
643    UINT8               *p;
644
645    /* Move back to the substate where we were before changing sector */
646    p_t2t->substate = p_t2t->prev_substate;
647
648    p              = (UINT8 *) (p_t2t->p_sec_cmd_buf + 1) + p_t2t->p_sec_cmd_buf->offset;
649    p_cmd_rsp_info = t2t_cmd_to_rsp_info ((UINT8) *p);
650    p_t2t->p_cmd_rsp_info   = (tT2T_CMD_RSP_INFO *) p_cmd_rsp_info;
651
652    /* allocate a new buffer for message */
653    if ((p_cmd_buf = (BT_HDR *) GKI_getpoolbuf (NFC_RW_POOL_ID)) != NULL)
654    {
655        memcpy (p_cmd_buf, p_t2t->p_sec_cmd_buf, sizeof (BT_HDR) + p_t2t->p_sec_cmd_buf->offset + p_t2t->p_sec_cmd_buf->len);
656        memcpy (p_t2t->p_cur_cmd_buf, p_t2t->p_sec_cmd_buf, sizeof (BT_HDR) + p_t2t->p_sec_cmd_buf->offset + p_t2t->p_sec_cmd_buf->len);
657
658#if (defined (RW_STATS_INCLUDED) && (RW_STATS_INCLUDED == TRUE))
659        /* Update stats */
660         rw_main_update_tx_stats (p_cmd_buf->len, TRUE);
661#endif
662        if (NFC_SendData (NFC_RF_CONN_ID, p_cmd_buf) == NFC_STATUS_OK)
663        {
664            /* Start timer for waiting for response */
665            nfc_start_quick_timer (&p_t2t->t2_timer, NFC_TTYPE_RW_T2T_RESPONSE,
666                                   (RW_T2T_TOUT_RESP * QUICK_TIMER_TICKS_PER_SEC) / 1000);
667        }
668        else
669        {
670            /* failure - could not send buffer */
671            evt_data.p_data = NULL;
672            evt_data.status = NFC_STATUS_FAILED;
673            event = rw_t2t_info_to_event (p_cmd_rsp_info);
674            rw_t2t_handle_op_complete ();
675            (*rw_cb.p_cback) (event, (tRW_DATA *) &evt_data);
676        }
677    }
678}
679
680/*******************************************************************************
681**
682** Function         rw_t2t_sector_change
683**
684** Description      This function issues Type 2 Tag SECTOR-SELECT command
685**                  packet 1.
686**
687** Returns          tNFC_STATUS
688**
689*******************************************************************************/
690tNFC_STATUS rw_t2t_sector_change (UINT8 sector)
691{
692    tNFC_STATUS status;
693    BT_HDR      *p_data;
694    UINT8       *p;
695    tRW_T2T_CB  *p_t2t = &rw_cb.tcb.t2t;
696
697    if ((p_data = (BT_HDR *) GKI_getpoolbuf (NFC_RW_POOL_ID)) == NULL)
698    {
699        RW_TRACE_ERROR0 ("rw_t2t_sector_change - No buffer");
700         return (NFC_STATUS_NO_BUFFERS);
701    }
702
703    p_data->offset = NCI_MSG_OFFSET_SIZE + NCI_DATA_HDR_SIZE;
704    p = (UINT8 *) (p_data + 1) + p_data->offset;
705
706    UINT8_TO_BE_STREAM (p, sector);
707    UINT8_TO_BE_STREAM (p, 0x00);
708    UINT8_TO_BE_STREAM (p, 0x00);
709    UINT8_TO_BE_STREAM (p, 0x00);
710
711    p_data->len = 4;
712
713    if ((status = NFC_SendData (NFC_RF_CONN_ID , p_data)) == NFC_STATUS_OK)
714    {
715        /* Passive rsp command and suppose not to get response to this command */
716        p_t2t->p_cmd_rsp_info = NULL;
717        p_t2t->substate       = RW_T2T_SUBSTATE_WAIT_SELECT_SECTOR;
718
719        RW_TRACE_EVENT0 ("rw_t2t_sector_change Sent Second Command");
720        nfc_start_quick_timer (&p_t2t->t2_timer, NFC_TTYPE_RW_T2T_RESPONSE,
721                               (RW_T2T_SEC_SEL_TOUT_RESP * QUICK_TIMER_TICKS_PER_SEC) / 1000);
722    }
723    else
724    {
725        RW_TRACE_ERROR1 ("rw_t2t_sector_change Send failed at rw_t2t_send_cmd, error: %u", status);
726    }
727
728    return status;
729}
730
731/*******************************************************************************
732**
733** Function         rw_t2t_read
734**
735** Description      This function issues Type 2 Tag READ command for the
736**                  specified block. If the specified block is in different
737**                  sector then it first sends command to move to new sector
738**                  and after the tag moves to new sector it issues the read
739**                  command for the block.
740**
741** Returns          tNFC_STATUS
742**
743*******************************************************************************/
744tNFC_STATUS rw_t2t_read (UINT16 block)
745{
746    tNFC_STATUS status;
747    UINT8       *p;
748    tRW_T2T_CB  *p_t2t = &rw_cb.tcb.t2t;
749    UINT8       sector_byte2[1];
750    UINT8       read_cmd[1];
751
752
753    read_cmd[0] = block % T2T_BLOCKS_PER_SECTOR;
754    if (p_t2t->sector != block/T2T_BLOCKS_PER_SECTOR)
755    {
756        sector_byte2[0] = 0xFF;
757        /* First Move to new sector before sending Read command */
758        if ((status = rw_t2t_send_cmd (T2T_CMD_SEC_SEL,sector_byte2)) == NFC_STATUS_OK)
759        {
760            /* Prepare command that needs to be sent after sector change op is completed */
761            p_t2t->select_sector         = (UINT8) (block/T2T_BLOCKS_PER_SECTOR);
762            p_t2t->p_sec_cmd_buf->offset = NCI_MSG_OFFSET_SIZE + NCI_DATA_HDR_SIZE;
763
764            p = (UINT8 *) (p_t2t->p_sec_cmd_buf + 1) + p_t2t->p_sec_cmd_buf->offset;
765            UINT8_TO_BE_STREAM (p, T2T_CMD_READ);
766            UINT8_TO_BE_STREAM (p, read_cmd[0]);
767            p_t2t->p_sec_cmd_buf->len = 2;
768            p_t2t->block_read = block;
769
770            /* Backup the current substate to move back to this substate after changing sector */
771            p_t2t->prev_substate = p_t2t->substate;
772            p_t2t->substate      = RW_T2T_SUBSTATE_WAIT_SELECT_SECTOR_SUPPORT;
773            return NFC_STATUS_OK;
774        }
775        return NFC_STATUS_FAILED;
776    }
777
778    /* Send Read command as sector change is not needed */
779    if ((status = rw_t2t_send_cmd (T2T_CMD_READ, (UINT8 *) read_cmd)) == NFC_STATUS_OK)
780    {
781        p_t2t->block_read = block;
782        RW_TRACE_EVENT1 ("rw_t2t_read Sent Command for Block: %u", block);
783    }
784
785    return status;
786}
787
788/*******************************************************************************
789**
790** Function         rw_t2t_write
791**
792** Description      This function issues Type 2 Tag WRITE command for the
793**                  specified block.  If the specified block is in different
794**                  sector then it first sends command to move to new sector
795**                  and after the tag moves to new sector it issues the write
796**                  command for the block.
797**
798** Returns          tNFC_STATUS
799**
800*******************************************************************************/
801tNFC_STATUS rw_t2t_write (UINT16 block, UINT8 *p_write_data)
802{
803    tNFC_STATUS status;
804    UINT8       *p;
805    tRW_T2T_CB  *p_t2t = &rw_cb.tcb.t2t;
806    UINT8       write_cmd[T2T_WRITE_DATA_LEN + 1];
807    UINT8       sector_byte2[1];
808
809    p_t2t->block_written = block;
810    write_cmd[0] = (UINT8) (block%T2T_BLOCKS_PER_SECTOR);
811    memcpy (&write_cmd[1], p_write_data, T2T_WRITE_DATA_LEN);
812
813    if (p_t2t->sector != block/T2T_BLOCKS_PER_SECTOR)
814    {
815        sector_byte2[0] = 0xFF;
816        /* First Move to new sector before sending Write command */
817        if ((status = rw_t2t_send_cmd (T2T_CMD_SEC_SEL, sector_byte2)) == NFC_STATUS_OK)
818        {
819            /* Prepare command that needs to be sent after sector change op is completed */
820            p_t2t->select_sector         = (UINT8) (block/T2T_BLOCKS_PER_SECTOR);
821            p_t2t->p_sec_cmd_buf->offset = NCI_MSG_OFFSET_SIZE + NCI_DATA_HDR_SIZE;
822            p = (UINT8 *) (p_t2t->p_sec_cmd_buf + 1) + p_t2t->p_sec_cmd_buf->offset;
823            UINT8_TO_BE_STREAM (p, T2T_CMD_WRITE);
824            memcpy (p, write_cmd, T2T_WRITE_DATA_LEN + 1);
825            p_t2t->p_sec_cmd_buf->len   = 2 + T2T_WRITE_DATA_LEN;
826            p_t2t->block_written  = block;
827
828            /* Backup the current substate to move back to this substate after changing sector */
829            p_t2t->prev_substate        = p_t2t->substate;
830            p_t2t->substate             = RW_T2T_SUBSTATE_WAIT_SELECT_SECTOR_SUPPORT;
831            return NFC_STATUS_OK;
832        }
833        return NFC_STATUS_FAILED;
834    }
835
836    /* Send Write command as sector change is not needed */
837    if ((status = rw_t2t_send_cmd (T2T_CMD_WRITE, write_cmd)) == NFC_STATUS_OK)
838    {
839        RW_TRACE_EVENT1 ("rw_t2t_write Sent Command for Block: %u", block);
840    }
841
842    return status;
843}
844
845/*******************************************************************************
846**
847** Function         rw_t2t_select
848**
849** Description      This function selects type 2 tag.
850**
851** Returns          Tag selection status
852**
853*******************************************************************************/
854tNFC_STATUS rw_t2t_select (void)
855{
856    tRW_T2T_CB    *p_t2t = &rw_cb.tcb.t2t;
857
858    p_t2t->state       = RW_T2T_STATE_IDLE;
859    p_t2t->ndef_status = T2T_NDEF_NOT_DETECTED;
860
861
862    /* Alloc cmd buf for retransmissions */
863    if (p_t2t->p_cur_cmd_buf ==  NULL)
864    {
865        if ((p_t2t->p_cur_cmd_buf = (BT_HDR *) GKI_getpoolbuf (NFC_RW_POOL_ID)) == NULL)
866        {
867            RW_TRACE_ERROR0 ("rw_t2t_select: unable to allocate buffer for retransmission");
868            return (NFC_STATUS_FAILED);
869        }
870    }
871    /* Alloc cmd buf for holding a command untill sector changes */
872    if (p_t2t->p_sec_cmd_buf ==  NULL)
873    {
874        if ((p_t2t->p_sec_cmd_buf = (BT_HDR *) GKI_getpoolbuf (NFC_RW_POOL_ID)) == NULL)
875        {
876            RW_TRACE_ERROR0 ("rw_t2t_select: unable to allocate buffer used during sector change");
877            return (NFC_STATUS_FAILED);
878        }
879    }
880
881    NFC_SetStaticRfCback (rw_t2t_conn_cback);
882    rw_t2t_handle_op_complete ();
883    p_t2t->check_tag_halt = FALSE;
884
885    return NFC_STATUS_OK;
886}
887
888/*****************************************************************************
889**
890** Function         rw_t2t_handle_op_complete
891**
892** Description      Reset to IDLE state
893**
894** Returns          Nothing
895**
896*****************************************************************************/
897void rw_t2t_handle_op_complete (void)
898{
899    tRW_T2T_CB      *p_t2t  = &rw_cb.tcb.t2t;
900
901    if (  (p_t2t->state == RW_T2T_STATE_READ_NDEF)
902        ||(p_t2t->state == RW_T2T_STATE_WRITE_NDEF)  )
903    {
904        p_t2t->b_read_data = FALSE;
905    }
906
907    if (p_t2t->state != RW_T2T_STATE_HALT)
908        p_t2t->state    = RW_T2T_STATE_IDLE;
909    p_t2t->substate = RW_T2T_SUBSTATE_NONE;
910    return;
911}
912
913/*****************************************************************************
914**
915** Function         RW_T2tPresenceCheck
916**
917** Description
918**      Check if the tag is still in the field.
919**
920**      The RW_T2T_PRESENCE_CHECK_EVT w/ status is used to indicate presence
921**      or non-presence.
922**
923** Returns
924**      NFC_STATUS_OK, if raw data frame sent
925**      NFC_STATUS_NO_BUFFERS: unable to allocate a buffer for this operation
926**      NFC_STATUS_FAILED: other error
927**
928*****************************************************************************/
929tNFC_STATUS RW_T2tPresenceCheck (void)
930{
931    tNFC_STATUS retval = NFC_STATUS_OK;
932    tRW_DATA evt_data;
933    tRW_CB *p_rw_cb = &rw_cb;
934    UINT8 sector_blk = 0;           /* block 0 of current sector */
935
936    RW_TRACE_API0 ("RW_T2tPresenceCheck");
937
938    /* If RW_SelectTagType was not called (no conn_callback) return failure */
939    if (!p_rw_cb->p_cback)
940    {
941        retval = NFC_STATUS_FAILED;
942    }
943    /* If we are not activated, then RW_T2T_PRESENCE_CHECK_EVT status=FAIL */
944    else if (p_rw_cb->tcb.t2t.state == RW_T2T_STATE_NOT_ACTIVATED)
945    {
946        evt_data.status = NFC_STATUS_FAILED;
947        (*p_rw_cb->p_cback) (RW_T2T_PRESENCE_CHECK_EVT, &evt_data);
948    }
949    /* If command is pending, assume tag is still present */
950    else if (p_rw_cb->tcb.t2t.state != RW_T2T_STATE_IDLE)
951    {
952        evt_data.status = NFC_STATUS_OK;
953        (*p_rw_cb->p_cback) (RW_T2T_PRESENCE_CHECK_EVT, &evt_data);
954    }
955    else
956    {
957        /* IDLE state: send a READ command to block 0 of the current sector */
958        if((retval = rw_t2t_send_cmd (T2T_CMD_READ, &sector_blk))== NFC_STATUS_OK)
959        {
960            p_rw_cb->tcb.t2t.state = RW_T2T_STATE_CHECK_PRESENCE;
961        }
962    }
963
964    return (retval);
965}
966
967/*******************************************************************************
968**
969** Function         RW_T2tRead
970**
971** Description      This function issues the Type 2 Tag READ command. When the
972**                  operation is complete the callback function will be called
973**                  with a RW_T2T_READ_EVT.
974**
975** Returns          tNFC_STATUS
976**
977*******************************************************************************/
978tNFC_STATUS RW_T2tRead (UINT16 block)
979{
980    tRW_T2T_CB  *p_t2t = &rw_cb.tcb.t2t;
981    tNFC_STATUS status;
982
983    if (p_t2t->state != RW_T2T_STATE_IDLE)
984    {
985        RW_TRACE_ERROR1 ("Error: Type 2 tag not activated or Busy - State: %u", p_t2t->state);
986        return (NFC_STATUS_FAILED);
987    }
988
989    if ((status = rw_t2t_read (block)) == NFC_STATUS_OK)
990    {
991        p_t2t->state    = RW_T2T_STATE_READ;
992        RW_TRACE_EVENT0 ("RW_T2tRead Sent Read command");
993    }
994
995    return status;
996
997}
998
999/*******************************************************************************
1000**
1001** Function         RW_T2tWrite
1002**
1003** Description      This function issues the Type 2 Tag WRITE command. When the
1004**                  operation is complete the callback function will be called
1005**                  with a RW_T2T_WRITE_EVT.
1006**
1007**                  p_new_bytes points to the array of 4 bytes to be written
1008**
1009** Returns          tNFC_STATUS
1010**
1011*******************************************************************************/
1012tNFC_STATUS RW_T2tWrite (UINT16 block, UINT8 *p_write_data)
1013{
1014    tRW_T2T_CB  *p_t2t = &rw_cb.tcb.t2t;
1015    tNFC_STATUS status;
1016
1017    if (p_t2t->state != RW_T2T_STATE_IDLE)
1018    {
1019        RW_TRACE_ERROR1 ("Error: Type 2 tag not activated or Busy - State: %u", p_t2t->state);
1020        return (NFC_STATUS_FAILED);
1021    }
1022
1023    if ((status = rw_t2t_write (block, p_write_data)) == NFC_STATUS_OK)
1024    {
1025        p_t2t->state    = RW_T2T_STATE_WRITE;
1026        if (block < T2T_FIRST_DATA_BLOCK)
1027            p_t2t->b_read_hdr = FALSE;
1028        else if (block < (T2T_FIRST_DATA_BLOCK + T2T_READ_BLOCKS))
1029            p_t2t->b_read_data = FALSE;
1030        RW_TRACE_EVENT0 ("RW_T2tWrite Sent Write command");
1031    }
1032
1033    return status;
1034}
1035
1036/*******************************************************************************
1037**
1038** Function         RW_T2tSectorSelect
1039**
1040** Description      This function issues the Type 2 Tag SECTOR-SELECT command
1041**                  packet 1. If a NACK is received as the response, the callback
1042**                  function will be called with a RW_T2T_SECTOR_SELECT_EVT. If
1043**                  an ACK is received as the response, the command packet 2 with
1044**                  the given sector number is sent to the peer device. When the
1045**                  response for packet 2 is received, the callback function will
1046**                  be called with a RW_T2T_SECTOR_SELECT_EVT.
1047**
1048**                  A sector is 256 contiguous blocks (1024 bytes).
1049**
1050** Returns          tNFC_STATUS
1051**
1052*******************************************************************************/
1053tNFC_STATUS RW_T2tSectorSelect (UINT8 sector)
1054{
1055    tNFC_STATUS status;
1056    tRW_T2T_CB  *p_t2t       = &rw_cb.tcb.t2t;
1057    UINT8       sector_byte2[1];
1058
1059    if (p_t2t->state != RW_T2T_STATE_IDLE)
1060    {
1061        RW_TRACE_ERROR1 ("Error: Type 2 tag not activated or Busy - State: %u", p_t2t->state);
1062        return (NFC_STATUS_FAILED);
1063    }
1064
1065    if (sector >= T2T_MAX_SECTOR)
1066    {
1067        RW_TRACE_ERROR2 ("RW_T2tSectorSelect - Invalid sector: %u, T2 Max supported sector value: %u", sector, T2T_MAX_SECTOR - 1);
1068        return (NFC_STATUS_FAILED);
1069    }
1070
1071    sector_byte2[0] = 0xFF;
1072
1073    if ((status = rw_t2t_send_cmd (T2T_CMD_SEC_SEL, sector_byte2)) == NFC_STATUS_OK)
1074    {
1075        p_t2t->state         = RW_T2T_STATE_SELECT_SECTOR;
1076        p_t2t->select_sector = sector;
1077        p_t2t->substate      = RW_T2T_SUBSTATE_WAIT_SELECT_SECTOR_SUPPORT;
1078
1079        RW_TRACE_EVENT0 ("RW_T2tSectorSelect Sent Sector select first command");
1080    }
1081
1082    return status;
1083}
1084
1085#if (BT_TRACE_VERBOSE == TRUE)
1086/*******************************************************************************
1087**
1088** Function         rw_t2t_get_state_name
1089**
1090** Description      This function returns the state name.
1091**
1092** NOTE             conditionally compiled to save memory.
1093**
1094** Returns          pointer to the name
1095**
1096*******************************************************************************/
1097static char *rw_t2t_get_state_name (UINT8 state)
1098{
1099    switch (state)
1100    {
1101    case RW_T2T_STATE_NOT_ACTIVATED:
1102        return ("NOT_ACTIVATED");
1103    case RW_T2T_STATE_IDLE:
1104        return ("IDLE");
1105    case RW_T2T_STATE_READ:
1106        return ("APP_READ");
1107    case RW_T2T_STATE_WRITE:
1108        return ("APP_WRITE");
1109    case RW_T2T_STATE_SELECT_SECTOR:
1110        return ("SECTOR_SELECT");
1111    case RW_T2T_STATE_DETECT_TLV:
1112        return ("TLV_DETECT");
1113    case RW_T2T_STATE_READ_NDEF:
1114        return ("READ_NDEF");
1115    case RW_T2T_STATE_WRITE_NDEF:
1116        return ("WRITE_NDEF");
1117    case RW_T2T_STATE_SET_TAG_RO:
1118        return ("SET_TAG_RO");
1119    case RW_T2T_STATE_CHECK_PRESENCE:
1120        return ("CHECK_PRESENCE");
1121    default:
1122        return ("???? UNKNOWN STATE");
1123    }
1124}
1125
1126/*******************************************************************************
1127**
1128** Function         rw_t2t_get_substate_name
1129**
1130** Description      This function returns the substate name.
1131**
1132** NOTE             conditionally compiled to save memory.
1133**
1134** Returns          pointer to the name
1135**
1136*******************************************************************************/
1137static char *rw_t2t_get_substate_name (UINT8 substate)
1138{
1139    switch (substate)
1140    {
1141    case RW_T2T_SUBSTATE_NONE:
1142        return ("RW_T2T_SUBSTATE_NONE");
1143    case RW_T2T_SUBSTATE_WAIT_SELECT_SECTOR_SUPPORT:
1144        return ("RW_T2T_SUBSTATE_WAIT_SELECT_SECTOR_SUPPORT");
1145    case RW_T2T_SUBSTATE_WAIT_SELECT_SECTOR:
1146        return ("RW_T2T_SUBSTATE_WAIT_SELECT_SECTOR");
1147    case RW_T2T_SUBSTATE_WAIT_READ_CC:
1148        return ("RW_T2T_SUBSTATE_WAIT_READ_CC");
1149    case RW_T2T_SUBSTATE_WAIT_TLV_DETECT:
1150        return ("RW_T2T_SUBSTATE_WAIT_TLV_DETECT");
1151    case RW_T2T_SUBSTATE_WAIT_FIND_LEN_FIELD_LEN:
1152        return ("RW_T2T_SUBSTATE_WAIT_FIND_LEN_FIELD_LEN");
1153    case RW_T2T_SUBSTATE_WAIT_READ_TLV_LEN0:
1154        return ("RW_T2T_SUBSTATE_WAIT_READ_TLV_LEN0");
1155    case RW_T2T_SUBSTATE_WAIT_READ_TLV_LEN1:
1156        return ("RW_T2T_SUBSTATE_WAIT_READ_TLV_LEN1");
1157    case RW_T2T_SUBSTATE_WAIT_READ_TLV_VALUE:
1158        return ("RW_T2T_SUBSTATE_WAIT_READ_TLV_VALUE");
1159    case RW_T2T_SUBSTATE_WAIT_READ_LOCKS:
1160        return ("RW_T2T_SUBSTATE_WAIT_READ_LOCKS");
1161    case RW_T2T_SUBSTATE_WAIT_READ_NDEF_FIRST_BLOCK:
1162        return ("RW_T2T_SUBSTATE_WAIT_READ_NDEF_FIRST_BLOCK");
1163    case RW_T2T_SUBSTATE_WAIT_READ_NDEF_LAST_BLOCK:
1164        return ("RW_T2T_SUBSTATE_WAIT_READ_NDEF_LAST_BLOCK");
1165    case RW_T2T_SUBSTATE_WAIT_READ_TERM_TLV_BLOCK:
1166        return ("RW_T2T_SUBSTATE_WAIT_READ_TERM_TLV_BLOCK");
1167    case RW_T2T_SUBSTATE_WAIT_READ_NDEF_NEXT_BLOCK:
1168        return ("RW_T2T_SUBSTATE_WAIT_READ_NDEF_NEXT_BLOCK");
1169    case RW_T2T_SUBSTATE_WAIT_WRITE_NDEF_NEXT_BLOCK:
1170        return ("RW_T2T_SUBSTATE_WAIT_WRITE_NDEF_NEXT_BLOCK");
1171    case RW_T2T_SUBSTATE_WAIT_WRITE_NDEF_LAST_BLOCK:
1172        return ("RW_T2T_SUBSTATE_WAIT_WRITE_NDEF_LAST_BLOCK");
1173    case RW_T2T_SUBSTATE_WAIT_READ_NDEF_LEN_BLOCK:
1174        return ("RW_T2T_SUBSTATE_WAIT_READ_NDEF_LEN_BLOCK");
1175    case RW_T2T_SUBSTATE_WAIT_WRITE_NDEF_LEN_BLOCK:
1176        return ("RW_T2T_SUBSTATE_WAIT_WRITE_NDEF_LEN_BLOCK");
1177    case RW_T2T_SUBSTATE_WAIT_WRITE_NDEF_LEN_NEXT_BLOCK:
1178        return ("RW_T2T_SUBSTATE_WAIT_WRITE_NDEF_LEN_NEXT_BLOCK");
1179    case RW_T2T_SUBSTATE_WAIT_WRITE_TERM_TLV_CMPLT:
1180        return ("RW_T2T_SUBSTATE_WAIT_WRITE_TERM_TLV_CMPLT");
1181    default:
1182        return ("???? UNKNOWN SUBSTATE");
1183    }
1184}
1185
1186#endif /* (BT_TRACE_VERBOSE == TRUE) */
1187
1188#endif /* NFC_INCLUDED == TRUE*/
1189