summaryrefslogtreecommitdiff
path: root/halimpl/pn54x/tml/phTmlNfc.c
blob: e50b49e00307e957b831abe183ddcf8ad18f226a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
/*
 * Copyright (C) 2010-2014 NXP Semiconductors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * TML Implementation.
 */

#include <phTmlNfc.h>
#include <phOsalNfc_Timer.h>
#include <phNxpLog.h>
#include <phDal4Nfc_messageQueueLib.h>
#include <phTmlNfc_i2c.h>
#include <phNxpNciHal_utils.h>

/*
 * Duration of Timer to wait after sending an Nci packet
 */
#define PHTMLNFC_MAXTIME_RETRANSMIT (200U)
#define MAX_WRITE_RETRY_COUNT 0x03
/* Retry Count = Standby Recovery time of NFCC / Retransmission time + 1 */
static uint8_t bCurrentRetryCount = (2000 / PHTMLNFC_MAXTIME_RETRANSMIT) + 1;


/* Value to reset variables of TML  */
#define PH_TMLNFC_RESET_VALUE               (0x00)

/* Indicates a Initial or offset value */
#define PH_TMLNFC_VALUE_ONE                 (0x01)

/* Initialize Context structure pointer used to access context structure */
phTmlNfc_Context_t *gpphTmlNfc_Context = NULL;
phTmlNfc_i2cfragmentation_t fragmentation_enabled = I2C_FRAGMENATATION_DISABLED;
/* Local Function prototypes */
static NFCSTATUS phTmlNfc_StartThread(void);
static void phTmlNfc_CleanUp(void);
static void phTmlNfc_ReadDeferredCb(void *pParams);
static void phTmlNfc_WriteDeferredCb(void *pParams);
static void phTmlNfc_TmlThread(void *pParam);
static void phTmlNfc_TmlWriterThread(void *pParam);
static void phTmlNfc_ReTxTimerCb(uint32_t dwTimerId, void *pContext);
static NFCSTATUS phTmlNfc_InitiateTimer(void);


/* Function definitions */

/*******************************************************************************
**
** Function         phTmlNfc_Init
**
** Description      Provides initialization of TML layer and hardware interface
**                  Configures given hardware interface and sends handle to the caller
**
** Parameters       pConfig     - TML configuration details as provided by the upper layer
**
** Returns          NFC status:
**                  NFCSTATUS_SUCCESS            - initialization successful
**                  NFCSTATUS_INVALID_PARAMETER  - at least one parameter is invalid
**                  NFCSTATUS_FAILED             - initialization failed
**                                                 (for example, unable to open hardware interface)
**                  NFCSTATUS_INVALID_DEVICE     - device has not been opened or has been disconnected
**
*******************************************************************************/
NFCSTATUS phTmlNfc_Init(pphTmlNfc_Config_t pConfig)
{
    NFCSTATUS wInitStatus = NFCSTATUS_SUCCESS;

    /* Check if TML layer is already Initialized */
    if (NULL != gpphTmlNfc_Context)
    {
        /* TML initialization is already completed */
        wInitStatus = PHNFCSTVAL(CID_NFC_TML, NFCSTATUS_ALREADY_INITIALISED);
    }
    /* Validate Input parameters */
    else if ((NULL == pConfig)  ||
            (PH_TMLNFC_RESET_VALUE == pConfig->dwGetMsgThreadId))
    {
        /*Parameters passed to TML init are wrong */
        wInitStatus = PHNFCSTVAL(CID_NFC_TML, NFCSTATUS_INVALID_PARAMETER);
    }
    else
    {
        /* Allocate memory for TML context */
        gpphTmlNfc_Context = malloc(sizeof(phTmlNfc_Context_t));

        if (NULL == gpphTmlNfc_Context)
        {
            wInitStatus = PHNFCSTVAL(CID_NFC_TML, NFCSTATUS_FAILED);
        }
        else
        {
            /* Initialise all the internal TML variables */
            memset(gpphTmlNfc_Context, PH_TMLNFC_RESET_VALUE, sizeof(phTmlNfc_Context_t));
            /* Make sure that the thread runs once it is created */
            gpphTmlNfc_Context->bThreadDone = 1;

            /* Open the device file to which data is read/written */
            wInitStatus = phTmlNfc_i2c_open_and_configure(pConfig, &(gpphTmlNfc_Context->pDevHandle));

            if (NFCSTATUS_SUCCESS != wInitStatus)
            {
                wInitStatus = PHNFCSTVAL(CID_NFC_TML, NFCSTATUS_INVALID_DEVICE);
                gpphTmlNfc_Context->pDevHandle = NULL;
            }
            else
            {
                gpphTmlNfc_Context->tReadInfo.bEnable = 0;
                gpphTmlNfc_Context->tWriteInfo.bEnable = 0;
                gpphTmlNfc_Context->tReadInfo.bThreadBusy = FALSE;
                gpphTmlNfc_Context->tWriteInfo.bThreadBusy = FALSE;

                if(0 != sem_init(&gpphTmlNfc_Context->rxSemaphore, 0, 0))
                {
                    wInitStatus = NFCSTATUS_FAILED;
                }
                else if(0 != sem_init(&gpphTmlNfc_Context->txSemaphore, 0, 0))
                {
                    wInitStatus = NFCSTATUS_FAILED;
                }
                else if(0 != sem_init(&gpphTmlNfc_Context->postMsgSemaphore, 0, 0))
                {
                    wInitStatus = NFCSTATUS_FAILED;
                }
                else
                {
                    sem_post(&gpphTmlNfc_Context->postMsgSemaphore);
                    /* Start TML thread (to handle write and read operations) */
                    if (NFCSTATUS_SUCCESS != phTmlNfc_StartThread())
                    {
                        wInitStatus = PHNFCSTVAL(CID_NFC_TML, NFCSTATUS_FAILED);
                    }
                    else
                    {
                        /* Create Timer used for Retransmission of NCI packets */
                        gpphTmlNfc_Context->dwTimerId = phOsalNfc_Timer_Create();
                        if (PH_OSALNFC_TIMER_ID_INVALID != gpphTmlNfc_Context->dwTimerId)
                        {
                            /* Store the Thread Identifier to which Message is to be posted */
                            gpphTmlNfc_Context->dwCallbackThreadId = pConfig->dwGetMsgThreadId;
                            /* Enable retransmission of Nci packet & set retry count to default */
                            gpphTmlNfc_Context->eConfig = phTmlNfc_e_DisableRetrans;
                            /** Retry Count = Standby Recovery time of NFCC / Retransmission time + 1 */
                            gpphTmlNfc_Context->bRetryCount = (2000 / PHTMLNFC_MAXTIME_RETRANSMIT) + 1;
                            gpphTmlNfc_Context->bWriteCbInvoked = FALSE;
                        }
                        else
                        {
                            wInitStatus = PHNFCSTVAL(CID_NFC_TML, NFCSTATUS_FAILED);
                        }
                    }
                }
            }
        }
    }
    /* Clean up all the TML resources if any error */
    if (NFCSTATUS_SUCCESS != wInitStatus)
    {
        /* Clear all handles and memory locations initialized during init */
        phTmlNfc_CleanUp();
    }

    return wInitStatus;
}

/*******************************************************************************
**
** Function         phTmlNfc_ConfigNciPktReTx
**
** Description      Provides Enable/Disable Retransmission of NCI packets
**                  Needed in case of Timeout between Transmission and Reception of NCI packets
**                  Retransmission can be enabled only if standby mode is enabled
**
** Parameters       eConfig     - values from phTmlNfc_ConfigRetrans_t
**                  bRetryCount - Number of times Nci packets shall be retransmitted (default = 3)
**
** Returns          None
**
*******************************************************************************/
void phTmlNfc_ConfigNciPktReTx(phTmlNfc_ConfigRetrans_t eConfiguration, uint8_t bRetryCounter)
{
    /* Enable/Disable Retransmission */

    gpphTmlNfc_Context->eConfig = eConfiguration;
    if (phTmlNfc_e_EnableRetrans == eConfiguration)
    {
        /* Check whether Retry counter passed is valid */
        if (0 != bRetryCounter)
        {
            gpphTmlNfc_Context->bRetryCount = bRetryCounter;
        }
        /* Set retry counter to its default value */
        else
        {
            /** Retry Count = Standby Recovery time of NFCC / Retransmission time + 1 */
            gpphTmlNfc_Context->bRetryCount = (2000 / PHTMLNFC_MAXTIME_RETRANSMIT) + 1;
        }
    }

    return;
}

/*******************************************************************************
**
** Function         phTmlNfc_StartThread
**
** Description      Initializes comport, reader and writer threads
**
** Parameters       None
**
** Returns          NFC status:
**                  NFCSTATUS_SUCCESS    - threads initialized successfully
**                  NFCSTATUS_FAILED     - initialization failed due to system error
**
*******************************************************************************/
static NFCSTATUS phTmlNfc_StartThread(void)
{
    NFCSTATUS wStartStatus = NFCSTATUS_SUCCESS;
    void *h_threadsEvent = 0x00;
    uint32_t dwEvent;
    int pthread_create_status = 0;

    /* Create Reader and Writer threads */
    pthread_create_status = pthread_create(&gpphTmlNfc_Context->readerThread,NULL,(void *)&phTmlNfc_TmlThread,
                                  (void *)h_threadsEvent);
    if(0 != pthread_create_status)
    {
        wStartStatus = NFCSTATUS_FAILED;
    }
    else
    {
        /*Start Writer Thread*/
        pthread_create_status = pthread_create(&gpphTmlNfc_Context->writerThread,NULL,(void *)&phTmlNfc_TmlWriterThread,
                                   (void *)h_threadsEvent);
        if(0 != pthread_create_status)
        {
            wStartStatus = NFCSTATUS_FAILED;
        }
    }

    return wStartStatus;
}

/*******************************************************************************
**
** Function         phTmlNfc_ReTxTimerCb
**
** Description      This is the timer callback function after timer expiration.
**
** Parameters       dwThreadId  - id of the thread posting message
**                  pContext    - context provided by upper layer
**
** Returns          None
**
*******************************************************************************/
static void phTmlNfc_ReTxTimerCb(uint32_t dwTimerId, void *pContext)
{
    if ((gpphTmlNfc_Context->dwTimerId == dwTimerId) &&
            (NULL == pContext))
    {
        /* If Retry Count has reached its limit,Retransmit Nci
           packet */
        if (0 == bCurrentRetryCount)
        {
            /* Since the count has reached its limit,return from timer callback
               Upper layer Timeout would have happened */
        }
        else
        {
            bCurrentRetryCount--;
            gpphTmlNfc_Context->tWriteInfo.bThreadBusy = TRUE;
            gpphTmlNfc_Context->tWriteInfo.bEnable = 1;
        }
        sem_post(&gpphTmlNfc_Context->txSemaphore);
    }

    return;
}

/*******************************************************************************
**
** Function         phTmlNfc_InitiateTimer
**
** Description      Start a timer for Tx and Rx thread.
**
** Parameters       void
**
** Returns          NFC status
**
*******************************************************************************/
static NFCSTATUS phTmlNfc_InitiateTimer(void)
{
    NFCSTATUS wStatus = NFCSTATUS_SUCCESS;

    /* Start Timer once Nci packet is sent */
    wStatus = phOsalNfc_Timer_Start(gpphTmlNfc_Context->dwTimerId,
            (uint32_t) PHTMLNFC_MAXTIME_RETRANSMIT,
            phTmlNfc_ReTxTimerCb, NULL);

    return wStatus;
}

/*******************************************************************************
**
** Function         phTmlNfc_TmlThread
**
** Description      Read the data from the lower layer driver
**
** Parameters       pParam  - parameters for Writer thread function
**
** Returns          None
**
*******************************************************************************/
static void phTmlNfc_TmlThread(void *pParam)
{
    NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
    int32_t dwNoBytesWrRd = PH_TMLNFC_RESET_VALUE;
    uint8_t temp[260];
    /* Transaction info buffer to be passed to Callback Thread */
    static phTmlNfc_TransactInfo_t tTransactionInfo;
    /* Structure containing Tml callback function and parameters to be invoked
       by the callback thread */
    static phLibNfc_DeferredCall_t tDeferredInfo;
    /* Initialize Message structure to post message onto Callback Thread */
    static phLibNfc_Message_t tMsg;
    UNUSED(pParam);
    NXPLOG_TML_D("PN54X - Tml Reader Thread Started................\n");

    /* Writer thread loop shall be running till shutdown is invoked */
    while (gpphTmlNfc_Context->bThreadDone)
    {
        /* If Tml write is requested */
        /* Set the variable to success initially */
        wStatus = NFCSTATUS_SUCCESS;
        sem_wait(&gpphTmlNfc_Context->rxSemaphore);

        /* If Tml read is requested */
        if (1 == gpphTmlNfc_Context->tReadInfo.bEnable)
        {
            NXPLOG_TML_D("PN54X - Read requested.....\n");
            /* Set the variable to success initially */
            wStatus = NFCSTATUS_SUCCESS;

            /* Variable to fetch the actual number of bytes read */
            dwNoBytesWrRd = PH_TMLNFC_RESET_VALUE;

            /* Read the data from the file onto the buffer */
            if (NULL != gpphTmlNfc_Context->pDevHandle)
            {
                NXPLOG_TML_D("PN54X - Invoking I2C Read.....\n");
                dwNoBytesWrRd = phTmlNfc_i2c_read(gpphTmlNfc_Context->pDevHandle, temp, 260);

                if (-1 == dwNoBytesWrRd)
                {
                    NXPLOG_TML_E("PN54X - Error in I2C Read.....\n");
                    sem_post(&gpphTmlNfc_Context->rxSemaphore);
                }
                else if (dwNoBytesWrRd > 260)
                {
                    NXPLOG_TML_E ("Numer of bytes read exceeds the limit 260.....\n");
                    sem_post (&gpphTmlNfc_Context->rxSemaphore);
                }
                else
                {
                    memcpy(gpphTmlNfc_Context->tReadInfo.pBuffer, temp, dwNoBytesWrRd);

                    NXPLOG_TML_D("PN54X - I2C Read successful.....\n");
                    /* This has to be reset only after a successful read */
                    gpphTmlNfc_Context->tReadInfo.bEnable = 0;
                    if ((phTmlNfc_e_EnableRetrans == gpphTmlNfc_Context->eConfig) &&
                            (0x00 != (gpphTmlNfc_Context->tReadInfo.pBuffer[0] & 0xE0)))
                    {

                        NXPLOG_TML_D("PN54X - Retransmission timer stopped.....\n");
                        /* Stop Timer to prevent Retransmission */
                        uint32_t timerStatus = phOsalNfc_Timer_Stop(gpphTmlNfc_Context->dwTimerId);
                        if (NFCSTATUS_SUCCESS != timerStatus)
                        {
                            NXPLOG_TML_E("PN54X - timer stopped returned failure.....\n");
                        }
                        else
                        {
                            gpphTmlNfc_Context->bWriteCbInvoked = FALSE;
                        }
                    }
                    /* Update the actual number of bytes read including header */
                    gpphTmlNfc_Context->tReadInfo.wLength = (uint16_t) (dwNoBytesWrRd);
                    phNxpNciHal_print_packet("RECV", gpphTmlNfc_Context->tReadInfo.pBuffer,
                            gpphTmlNfc_Context->tReadInfo.wLength);

                    dwNoBytesWrRd = PH_TMLNFC_RESET_VALUE;

                    /* Fill the Transaction info structure to be passed to Callback Function */
                    tTransactionInfo.wStatus = wStatus;
                    tTransactionInfo.pBuff = gpphTmlNfc_Context->tReadInfo.pBuffer;
                    /* Actual number of bytes read is filled in the structure */
                    tTransactionInfo.wLength = gpphTmlNfc_Context->tReadInfo.wLength;

                    /* Read operation completed successfully. Post a Message onto Callback Thread*/
                    /* Prepare the message to be posted on User thread */
                    tDeferredInfo.pCallback = &phTmlNfc_ReadDeferredCb;
                    tDeferredInfo.pParameter = &tTransactionInfo;
                    tMsg.eMsgType = PH_LIBNFC_DEFERREDCALL_MSG;
                    tMsg.pMsgData = &tDeferredInfo;
                    tMsg.Size = sizeof(tDeferredInfo);
                    NXPLOG_TML_D("PN54X - Posting read message.....\n");
                    phTmlNfc_DeferredCall(gpphTmlNfc_Context->dwCallbackThreadId, &tMsg);

                }
            }
            else
            {
                NXPLOG_TML_D ("PN54X -gpphTmlNfc_Context->pDevHandle is NULL");
            }
        }
        else
        {
            NXPLOG_TML_D("PN54X - read request NOT enabled");
            usleep(10*1000);
        }
    }/* End of While loop */

    return;
}

/*******************************************************************************
**
** Function         phTmlNfc_TmlWriterThread
**
** Description      Writes the requested data onto the lower layer driver
**
** Parameters       pParam  - context provided by upper layer
**
** Returns          None
**
*******************************************************************************/
static void phTmlNfc_TmlWriterThread(void *pParam)
{
    NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
    int32_t dwNoBytesWrRd = PH_TMLNFC_RESET_VALUE;
    /* Transaction info buffer to be passed to Callback Thread */
    static phTmlNfc_TransactInfo_t tTransactionInfo;
    /* Structure containing Tml callback function and parameters to be invoked
       by the callback thread */
    static phLibNfc_DeferredCall_t tDeferredInfo;
    /* Initialize Message structure to post message onto Callback Thread */
    static phLibNfc_Message_t tMsg;
    /* In case of I2C Write Retry */
    static uint16_t retry_cnt;
    UNUSED(pParam);
    NXPLOG_TML_D("PN54X - Tml Writer Thread Started................\n");

    /* Writer thread loop shall be running till shutdown is invoked */
    while (gpphTmlNfc_Context->bThreadDone)
    {
        NXPLOG_TML_D("PN54X - Tml Writer Thread Running................\n");
        sem_wait(&gpphTmlNfc_Context->txSemaphore);
        /* If Tml write is requested */
        if (1 == gpphTmlNfc_Context->tWriteInfo.bEnable)
        {
            NXPLOG_TML_D("PN54X - Write requested.....\n");
            /* Set the variable to success initially */
            wStatus = NFCSTATUS_SUCCESS;
            if (NULL != gpphTmlNfc_Context->pDevHandle)
            {
            retry:
                gpphTmlNfc_Context->tWriteInfo.bEnable = 0;
                /* Variable to fetch the actual number of bytes written */
                dwNoBytesWrRd = PH_TMLNFC_RESET_VALUE;
                /* Write the data in the buffer onto the file */
                NXPLOG_TML_D("PN54X - Invoking I2C Write.....\n");
                dwNoBytesWrRd = phTmlNfc_i2c_write(gpphTmlNfc_Context->pDevHandle,
                        gpphTmlNfc_Context->tWriteInfo.pBuffer,
                        gpphTmlNfc_Context->tWriteInfo.wLength
                        );

                /* Try I2C Write Five Times, if it fails : Raju */
                if (-1 == dwNoBytesWrRd)
                {
                    if (getDownloadFlag() == TRUE)
                    {
                        if (retry_cnt++ < MAX_WRITE_RETRY_COUNT)
                        {
                            NXPLOG_NCIHAL_E(
                                    "PN54X - Error in I2C Write  - Retry 0x%x", retry_cnt);
                            goto retry;
                        }
                    }
                    NXPLOG_TML_E("PN54X - Error in I2C Write.....\n");
                    wStatus = PHNFCSTVAL(CID_NFC_TML, NFCSTATUS_FAILED);
                }
                else
                {
                    phNxpNciHal_print_packet("SEND", gpphTmlNfc_Context->tWriteInfo.pBuffer,
                            gpphTmlNfc_Context->tWriteInfo.wLength);
                }
                retry_cnt = 0;
                if (NFCSTATUS_SUCCESS == wStatus)
                {
                    NXPLOG_TML_D("PN54X - I2C Write successful.....\n");
                    dwNoBytesWrRd = PH_TMLNFC_VALUE_ONE;
                }
                /* Fill the Transaction info structure to be passed to Callback Function */
                tTransactionInfo.wStatus = wStatus;
                tTransactionInfo.pBuff = gpphTmlNfc_Context->tWriteInfo.pBuffer;
                /* Actual number of bytes written is filled in the structure */
                tTransactionInfo.wLength = (uint16_t) dwNoBytesWrRd;

                /* Prepare the message to be posted on the User thread */
                tDeferredInfo.pCallback = &phTmlNfc_WriteDeferredCb;
                tDeferredInfo.pParameter = &tTransactionInfo;
                /* Write operation completed successfully. Post a Message onto Callback Thread*/
                tMsg.eMsgType = PH_LIBNFC_DEFERREDCALL_MSG;
                tMsg.pMsgData = &tDeferredInfo;
                tMsg.Size = sizeof(tDeferredInfo);

                /* Check whether Retransmission needs to be started,
                 * If yes, Post message only if
                 * case 1. Message is not posted &&
                 * case 11. Write status is success ||
                 * case 12. Last retry of write is also failure
                 */
                if ((phTmlNfc_e_EnableRetrans == gpphTmlNfc_Context->eConfig) &&
                        (0x00 != (gpphTmlNfc_Context->tWriteInfo.pBuffer[0] & 0xE0)))
                {
                    if (FALSE == gpphTmlNfc_Context->bWriteCbInvoked)
                    {
                        if ((NFCSTATUS_SUCCESS == wStatus) ||
                                (bCurrentRetryCount == 0))
                        {
                                NXPLOG_TML_D("PN54X - Posting Write message.....\n");
                                phTmlNfc_DeferredCall(gpphTmlNfc_Context->dwCallbackThreadId,
                                        &tMsg);
                                gpphTmlNfc_Context->bWriteCbInvoked = TRUE;
                        }
                    }
                }
                else
                {
                    NXPLOG_TML_D("PN54X - Posting Fresh Write message.....\n");
                    phTmlNfc_DeferredCall(gpphTmlNfc_Context->dwCallbackThreadId, &tMsg);
                }
            }
            else
            {
                NXPLOG_TML_D ("PN54X - gpphTmlNfc_Context->pDevHandle is NULL");
            }

            /* If Data packet is sent, then NO retransmission */
            if ((phTmlNfc_e_EnableRetrans == gpphTmlNfc_Context->eConfig) &&
                    (0x00 != (gpphTmlNfc_Context->tWriteInfo.pBuffer[0] & 0xE0)))
            {
                NXPLOG_TML_D("PN54X - Starting timer for Retransmission case");
                wStatus = phTmlNfc_InitiateTimer();
                if (NFCSTATUS_SUCCESS != wStatus)
                {
                    /* Reset Variables used for Retransmission */
                    NXPLOG_TML_D("PN54X - Retransmission timer initiate failed");
                    gpphTmlNfc_Context->tWriteInfo.bEnable = 0;
                    bCurrentRetryCount = 0;
                }
            }
        }
        else
        {
            NXPLOG_TML_D("PN54X - Write request NOT enabled");
            usleep(10000);
        }

    }/* End of While loop */

    return;
}

/*******************************************************************************
**
** Function         phTmlNfc_CleanUp
**
** Description      Clears all handles opened during TML initialization
**
** Parameters       None
**
** Returns          None
**
*******************************************************************************/
static void phTmlNfc_CleanUp(void)
{
    NFCSTATUS wRetval = NFCSTATUS_SUCCESS;

    if (NULL != gpphTmlNfc_Context->pDevHandle)
    {
        (void) phTmlNfc_i2c_reset(gpphTmlNfc_Context->pDevHandle, 0);
        gpphTmlNfc_Context->bThreadDone = 0;
    }
    sem_destroy(&gpphTmlNfc_Context->rxSemaphore);
    sem_destroy(&gpphTmlNfc_Context->txSemaphore);
    sem_destroy(&gpphTmlNfc_Context->postMsgSemaphore);
    phTmlNfc_i2c_close(gpphTmlNfc_Context->pDevHandle);
    gpphTmlNfc_Context->pDevHandle = NULL;
    /* Clear memory allocated for storing Context variables */
    free((void *) gpphTmlNfc_Context);
    /* Set the pointer to NULL to indicate De-Initialization */
    gpphTmlNfc_Context = NULL;

    return;
}

/*******************************************************************************
**
** Function         phTmlNfc_Shutdown
**
** Description      Uninitializes TML layer and hardware interface
**
** Parameters       None
**
** Returns          NFC status:
**                  NFCSTATUS_SUCCESS            - TML configuration released successfully
**                  NFCSTATUS_INVALID_PARAMETER  - at least one parameter is invalid
**                  NFCSTATUS_FAILED             - un-initialization failed (example: unable to close interface)
**
*******************************************************************************/
NFCSTATUS phTmlNfc_Shutdown(void)
{
    NFCSTATUS wShutdownStatus = NFCSTATUS_SUCCESS;

    /* Check whether TML is Initialized */
    if (NULL != gpphTmlNfc_Context)
    {
        /* Reset thread variable to terminate the thread */
        gpphTmlNfc_Context->bThreadDone = 0;
        usleep(1000);
        /* Clear All the resources allocated during initialization */
        sem_post(&gpphTmlNfc_Context->rxSemaphore);
        usleep(1000);
        sem_post(&gpphTmlNfc_Context->txSemaphore);
        usleep(1000);
        sem_post(&gpphTmlNfc_Context->postMsgSemaphore);
        usleep(1000);
        sem_post(&gpphTmlNfc_Context->postMsgSemaphore);
        usleep(1000);
        if (0 != pthread_join(gpphTmlNfc_Context->readerThread, (void**)NULL))
        {
            NXPLOG_TML_E ("Fail to kill reader thread!");
        }
        if (0 != pthread_join(gpphTmlNfc_Context->writerThread, (void**)NULL))
        {
            NXPLOG_TML_E ("Fail to kill writer thread!");
        }
        NXPLOG_TML_D ("bThreadDone == 0");

        phTmlNfc_CleanUp();
    }
    else
    {
        wShutdownStatus = PHNFCSTVAL(CID_NFC_TML, NFCSTATUS_NOT_INITIALISED);
    }

    return wShutdownStatus;
}

/*******************************************************************************
**
** Function         phTmlNfc_Write
**
** Description      Asynchronously writes given data block to hardware interface/driver
**                  Enables writer thread if there are no write requests pending
**                  Returns successfully once writer thread completes write operation
**                  Notifies upper layer using callback mechanism
**                  NOTE:
**                  * it is important to post a message with id PH_TMLNFC_WRITE_MESSAGE
**                    to IntegrationThread after data has been written to PN54X
**                  * if CRC needs to be computed, then input buffer should be capable to store
**                    two more bytes apart from length of packet
**
** Parameters       pBuffer              - data to be sent
**                  wLength              - length of data buffer
**                  pTmlWriteComplete    - pointer to the function to be invoked upon completion
**                  pContext             - context provided by upper layer
**
** Returns          NFC status:
**                  NFCSTATUS_PENDING             - command is yet to be processed
**                  NFCSTATUS_INVALID_PARAMETER   - at least one parameter is invalid
**                  NFCSTATUS_BUSY                - write request is already in progress
**
*******************************************************************************/
NFCSTATUS phTmlNfc_Write(uint8_t *pBuffer, uint16_t wLength, pphTmlNfc_TransactCompletionCb_t pTmlWriteComplete, void *pContext)
{
    NFCSTATUS wWriteStatus;

    /* Check whether TML is Initialized */

    if (NULL != gpphTmlNfc_Context)
    {
        if ((NULL != gpphTmlNfc_Context->pDevHandle) && (NULL != pBuffer) &&
                (PH_TMLNFC_RESET_VALUE != wLength) && (NULL != pTmlWriteComplete))
        {
            if (!gpphTmlNfc_Context->tWriteInfo.bThreadBusy)
            {
                /* Setting the flag marks beginning of a Write Operation */
                gpphTmlNfc_Context->tWriteInfo.bThreadBusy = TRUE;
                /* Copy the buffer, length and Callback function,
                   This shall be utilized while invoking the Callback function in thread */
                gpphTmlNfc_Context->tWriteInfo.pBuffer = pBuffer;
                gpphTmlNfc_Context->tWriteInfo.wLength = wLength;
                gpphTmlNfc_Context->tWriteInfo.pThread_Callback = pTmlWriteComplete;
                gpphTmlNfc_Context->tWriteInfo.pContext = pContext;

                wWriteStatus = NFCSTATUS_PENDING;
                //FIXME: If retry is going on. Stop the retry thread/timer
                if (phTmlNfc_e_EnableRetrans == gpphTmlNfc_Context->eConfig)
                {
                    /* Set retry count to default value */
                    //FIXME: If the timer expired there, and meanwhile we have created
                    // a new request. The expired timer will think that retry is still
                    // ongoing.
                    bCurrentRetryCount = gpphTmlNfc_Context->bRetryCount;
                    gpphTmlNfc_Context->bWriteCbInvoked = FALSE;
                }
                /* Set event to invoke Writer Thread */
                gpphTmlNfc_Context->tWriteInfo.bEnable = 1;
                sem_post(&gpphTmlNfc_Context->txSemaphore);
            }
            else
            {
                wWriteStatus = PHNFCSTVAL(CID_NFC_TML, NFCSTATUS_BUSY);
            }
        }
        else
        {
            wWriteStatus = PHNFCSTVAL(CID_NFC_TML, NFCSTATUS_INVALID_PARAMETER);
        }
    }
    else
    {
        wWriteStatus = PHNFCSTVAL(CID_NFC_TML, NFCSTATUS_NOT_INITIALISED);
    }

    return wWriteStatus;
}

/*******************************************************************************
**
** Function         phTmlNfc_Read
**
** Description      Asynchronously reads data from the driver
**                  Number of bytes to be read and buffer are passed by upper layer
**                  Enables reader thread if there are no read requests pending
**                  Returns successfully once read operation is completed
**                  Notifies upper layer using callback mechanism
**
** Parameters       pBuffer              - location to send read data to the upper layer via callback
**                  wLength              - length of read data buffer passed by upper layer
**                  pTmlReadComplete     - pointer to the function to be invoked upon completion of read operation
**                  pContext             - context provided by upper layer
**
** Returns          NFC status:
**                  NFCSTATUS_PENDING             - command is yet to be processed
**                  NFCSTATUS_INVALID_PARAMETER   - at least one parameter is invalid
**                  NFCSTATUS_BUSY                - read request is already in progress
**
*******************************************************************************/
NFCSTATUS phTmlNfc_Read(uint8_t *pBuffer, uint16_t wLength, pphTmlNfc_TransactCompletionCb_t pTmlReadComplete, void *pContext)
{
    NFCSTATUS wReadStatus;

    /* Check whether TML is Initialized */
    if (NULL != gpphTmlNfc_Context)
    {
        if ((gpphTmlNfc_Context->pDevHandle != NULL) && (NULL != pBuffer) &&
                (PH_TMLNFC_RESET_VALUE != wLength) && (NULL != pTmlReadComplete))
        {
            if (!gpphTmlNfc_Context->tReadInfo.bThreadBusy)
            {
                /* Setting the flag marks beginning of a Read Operation */
                gpphTmlNfc_Context->tReadInfo.bThreadBusy = TRUE;
                /* Copy the buffer, length and Callback function,
                   This shall be utilized while invoking the Callback function in thread */
                gpphTmlNfc_Context->tReadInfo.pBuffer = pBuffer;
                gpphTmlNfc_Context->tReadInfo.wLength = wLength;
                gpphTmlNfc_Context->tReadInfo.pThread_Callback = pTmlReadComplete;
                gpphTmlNfc_Context->tReadInfo.pContext = pContext;
                wReadStatus = NFCSTATUS_PENDING;

                /* Set event to invoke Reader Thread */
                gpphTmlNfc_Context->tReadInfo.bEnable = 1;
                sem_post(&gpphTmlNfc_Context->rxSemaphore);
            }
            else
            {
                wReadStatus = PHNFCSTVAL(CID_NFC_TML, NFCSTATUS_BUSY);
            }
        }
        else
        {
            wReadStatus = PHNFCSTVAL(CID_NFC_TML, NFCSTATUS_INVALID_PARAMETER);
        }
    }
    else
    {
        wReadStatus = PHNFCSTVAL(CID_NFC_TML, NFCSTATUS_NOT_INITIALISED);
    }

    return wReadStatus;
}

/*******************************************************************************
**
** Function         phTmlNfc_ReadAbort
**
** Description      Aborts pending read request (if any)
**
** Parameters       None
**
** Returns          NFC status:
**                  NFCSTATUS_SUCCESS                    - ongoing read operation aborted
**                  NFCSTATUS_INVALID_PARAMETER          - at least one parameter is invalid
**                  NFCSTATUS_NOT_INITIALIZED            - TML layer is not initialized
**                  NFCSTATUS_BOARD_COMMUNICATION_ERROR  - unable to cancel read operation
**
*******************************************************************************/
NFCSTATUS phTmlNfc_ReadAbort(void)
{
    NFCSTATUS wStatus = NFCSTATUS_INVALID_PARAMETER;
    gpphTmlNfc_Context->tReadInfo.bEnable = 0;

    /*Reset the flag to accept another Read Request */
    gpphTmlNfc_Context->tReadInfo.bThreadBusy=FALSE;
    wStatus = NFCSTATUS_SUCCESS;

    return wStatus;
}

/*******************************************************************************
**
** Function         phTmlNfc_WriteAbort
**
** Description      Aborts pending write request (if any)
**
** Parameters       None
**
** Returns          NFC status:
**                  NFCSTATUS_SUCCESS                    - ongoing write operation aborted
**                  NFCSTATUS_INVALID_PARAMETER          - at least one parameter is invalid
**                  NFCSTATUS_NOT_INITIALIZED            - TML layer is not initialized
**                  NFCSTATUS_BOARD_COMMUNICATION_ERROR  - unable to cancel write operation
**
*******************************************************************************/
NFCSTATUS phTmlNfc_WriteAbort(void)
{
    NFCSTATUS wStatus = NFCSTATUS_INVALID_PARAMETER;

    gpphTmlNfc_Context->tWriteInfo.bEnable = 0;
    /* Stop if any retransmission is in progress */
    bCurrentRetryCount = 0;

    /* Reset the flag to accept another Write Request */
    gpphTmlNfc_Context->tWriteInfo.bThreadBusy=FALSE;
    wStatus = NFCSTATUS_SUCCESS;

    return wStatus;
}

/*******************************************************************************
**
** Function         phTmlNfc_IoCtl
**
** Description      Resets device when insisted by upper layer
**                  Number of bytes to be read and buffer are passed by upper layer
**                  Enables reader thread if there are no read requests pending
**                  Returns successfully once read operation is completed
**                  Notifies upper layer using callback mechanism
**
** Parameters       eControlCode       - control code for a specific operation
**
** Returns          NFC status:
**                  NFCSTATUS_SUCCESS  - ioctl command completed successfully
**                  NFCSTATUS_FAILED   - ioctl command request failed
**
*******************************************************************************/
NFCSTATUS phTmlNfc_IoCtl(phTmlNfc_ControlCode_t eControlCode)
{
    NFCSTATUS wStatus = NFCSTATUS_SUCCESS;

    if (NULL == gpphTmlNfc_Context)
    {
        wStatus = NFCSTATUS_FAILED;
    }
    else
    {
        switch (eControlCode)
        {
            case phTmlNfc_e_ResetDevice:
                {
                    /*Reset PN54X*/
                    phTmlNfc_i2c_reset(gpphTmlNfc_Context->pDevHandle, 1);
                    usleep(100 * 1000);
                    phTmlNfc_i2c_reset(gpphTmlNfc_Context->pDevHandle, 0);
                    usleep(100 * 1000);
                    phTmlNfc_i2c_reset(gpphTmlNfc_Context->pDevHandle, 1);
                    break;
                }
            case phTmlNfc_e_EnableNormalMode:
                {
                    /*Reset PN54X*/
                    phTmlNfc_i2c_reset(gpphTmlNfc_Context->pDevHandle, 0);
                    usleep(10 * 1000);
                    phTmlNfc_i2c_reset(gpphTmlNfc_Context->pDevHandle, 1);
                    usleep(100 * 1000);
                    break;
                }
            case phTmlNfc_e_EnableDownloadMode:
                {
                    phTmlNfc_ConfigNciPktReTx(phTmlNfc_e_DisableRetrans, 0);
                    (void)phTmlNfc_i2c_reset(gpphTmlNfc_Context->pDevHandle,2);
                    usleep(100 * 1000);
                    break;
                }
            default:
                {
                    wStatus = NFCSTATUS_INVALID_PARAMETER;
                    break;
                }
        }
    }

    return wStatus;
}

/*******************************************************************************
**
** Function         phTmlNfc_DeferredCall
**
** Description      Posts message on upper layer thread
**                  upon successful read or write operation
**
** Parameters       dwThreadId  - id of the thread posting message
**                  ptWorkerMsg - message to be posted
**
** Returns          None
**
*******************************************************************************/
void phTmlNfc_DeferredCall(uintptr_t dwThreadId, phLibNfc_Message_t *ptWorkerMsg)
{
    intptr_t bPostStatus;
    UNUSED(dwThreadId);
    /* Post message on the user thread to invoke the callback function */
    sem_wait(&gpphTmlNfc_Context->postMsgSemaphore);
    bPostStatus = phDal4Nfc_msgsnd(gpphTmlNfc_Context->dwCallbackThreadId,
            ptWorkerMsg,
            0
            );
    sem_post(&gpphTmlNfc_Context->postMsgSemaphore);
}

/*******************************************************************************
**
** Function         phTmlNfc_ReadDeferredCb
**
** Description      Read thread call back function
**
** Parameters       pParams - context provided by upper layer
**
** Returns          None
**
*******************************************************************************/
static void phTmlNfc_ReadDeferredCb(void *pParams)
{
    /* Transaction info buffer to be passed to Callback Function */
    phTmlNfc_TransactInfo_t *pTransactionInfo = (phTmlNfc_TransactInfo_t *) pParams;

    /* Reset the flag to accept another Read Request */
    gpphTmlNfc_Context->tReadInfo.bThreadBusy = FALSE;
    gpphTmlNfc_Context->tReadInfo.pThread_Callback(gpphTmlNfc_Context->tReadInfo.pContext,
            pTransactionInfo);

    return;
}

/*******************************************************************************
**
** Function         phTmlNfc_WriteDeferredCb
**
** Description      Write thread call back function
**
** Parameters       pParams - context provided by upper layer
**
** Returns          None
**
*******************************************************************************/
static void phTmlNfc_WriteDeferredCb(void *pParams)
{
    /* Transaction info buffer to be passed to Callback Function */
    phTmlNfc_TransactInfo_t *pTransactionInfo = (phTmlNfc_TransactInfo_t *) pParams;

    /* Reset the flag to accept another Write Request */
    gpphTmlNfc_Context->tWriteInfo.bThreadBusy = FALSE;
    gpphTmlNfc_Context->tWriteInfo.pThread_Callback(gpphTmlNfc_Context->tWriteInfo.pContext,
            pTransactionInfo);

    return;
}

void phTmlNfc_set_fragmentation_enabled(phTmlNfc_i2cfragmentation_t result)
{
    fragmentation_enabled = result;
}

phTmlNfc_i2cfragmentation_t phTmlNfc_get_fragmentation_enabled()
{
    return  fragmentation_enabled;
}