summaryrefslogtreecommitdiffstats
path: root/src/phLlcNfc_Interface.c
blob: 92be48d890129f3aca2020c426222cf4ed4bcc19 (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
/*
 * Copyright (C) 2010 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.
 */

/*!
* \file  phLlcNfc_Interface.c
* \brief Interface for both LLC and transport layer
*
* Project: NFC-FRI-1.1
*
* $Date: Tue Jun  1 14:41:26 2010 $
* $Author: ing02260 $
* $Revision: 1.75 $
* $Aliases: NFC_FRI1.1_WK1023_R35_1 $
*
*/

/*************************** Includes *******************************/
#include <phNfcTypes.h>
#include <phNfcStatus.h>
#include <phOsalNfc.h>
#include <phNfcInterface.h>
#include <phLlcNfc_DataTypes.h>
#include <phLlcNfc_Timer.h>
#include <phLlcNfc_Frame.h>
#include <phLlcNfc.h>
#include <phLlcNfc_Interface.h>
#ifdef PH_LLCNFC_STUB
#include <phDalNfc_Stub.h>
#endif
#ifdef PH_LLCNFC_DALINT
#include <phDal4Nfc.h>
#endif
/*********************** End of includes ****************************/

/***************************** Macros *******************************/
#define PH_LLCNFC_APPEND_LEN                        (4)
#define LLC_NS_FRAME_HEADER_MASK                    (0x38U)

/************************ End of macros *****************************/

/*********************** Local functions ****************************/
static
void 
phLlcNfc_WrResp_Cb(
                   void                        *pContext, 
                   void                        *pHwInfo, 
                   phNfc_sTransactionInfo_t    *pCompInfo
                   );

static
void 
phLlcNfc_RdResp_Cb(
                   void                        *pContext,
                   void                        *pHwInfo, 
                   phNfc_sTransactionInfo_t    *pCompInfo
                   );

/******************** End of Local functions ************************/

/********************** Global variables ****************************/

/******************** End of Global Variables ***********************/

NFCSTATUS 
phLlcNfc_Interface_Register(
    phLlcNfc_Context_t          *psLlcCtxt, 
    phNfcLayer_sCfg_t           *psIFConfig
)
{
    NFCSTATUS                   result = NFCSTATUS_SUCCESS;
    phNfcIF_sCallBack_t         if_cb;
    phNfcIF_sReference_t        sreference;
    
    if ((NULL == psLlcCtxt) || (NULL == psIFConfig))
    {
        result = PHNFCSTVAL(CID_NFC_LLC, 
                            NFCSTATUS_INVALID_PARAMETER);
    }
    else 
    {
        result = NFCSTATUS_SUCCESS;
        if_cb.notify = NULL;
        if_cb.receive_complete = (pphNfcIF_Transact_Completion_CB_t)&phLlcNfc_RdResp_Cb;
        if_cb.send_complete = (pphNfcIF_Transact_Completion_CB_t)&phLlcNfc_WrResp_Cb;
        if_cb.pif_ctxt = psLlcCtxt; 
        sreference.plower_if = &(psLlcCtxt->lower_if);
        result = PHNFCSTVAL(CID_NFC_LLC, NFCSTATUS_INVALID_PARAMETER);
#ifdef PH_LLCNFC_STUB
        result = phDalNfc_StubRegister(&sreference, if_cb, psIFConfig->layer_next);
#endif /* #ifdef PH_LLCNFC_STUB */
#ifdef PH_LLCNFC_DALINT
        result = phDal4Nfc_Register(&sreference, if_cb, psIFConfig->layer_next);
#else
        if ((NULL != psIFConfig->layer_next) && 
            (NULL != psIFConfig->layer_next->layer_registry))
        {
            result = psIFConfig->layer_next->layer_registry(
                        &sreference, 
                        if_cb, 
                        (void *)&psIFConfig[(psIFConfig->layer_index - 1)]);
        }
#endif /* #ifdef PH_LLCNFC_DALINT */
    }
    PH_LLCNFC_DEBUG("Llc Dal Interface Register result : 0x%x\n", result);
    return result;
}
 
NFCSTATUS 
phLlcNfc_Interface_Init(
    phLlcNfc_Context_t      *psLlcCtxt
)
{
    /*
        1. Get the pointer of the main llc context
    */
    NFCSTATUS   result = NFCSTATUS_SUCCESS;
    if ((NULL == psLlcCtxt) ||  
        (NULL == psLlcCtxt->lower_if.init))
    {
        result = PHNFCSTVAL(CID_NFC_LLC, 
                            NFCSTATUS_INVALID_PARAMETER);
    }
    else
    {        
        /* Initialise the main context */
        result = psLlcCtxt->lower_if.init(  psLlcCtxt->lower_if.pcontext, 
                                            psLlcCtxt->phwinfo);        
    }
    PH_LLCNFC_DEBUG("Llc Dal Interface Init result : 0x%x\n", result);
    return result;
}

NFCSTATUS 
phLlcNfc_Interface_Read(
    phLlcNfc_Context_t      *psLlcCtxt, 
    uint8_t                 readWaitOn, 
    uint8_t                 *pLlcBuffer, 
    uint32_t                llcBufferLength
)
{
    NFCSTATUS   result = NFCSTATUS_PENDING;
    /*
        1. Call DAL or TL read with "phLlcNfc_LlcTl_RdResp_Cb" as 
            callback function
    */
    PH_LLCNFC_PRINT("Llc Dal Interface Read called\n");
    if ((NULL == psLlcCtxt) || (NULL == pLlcBuffer) || 
        (0 == llcBufferLength) || (NULL == psLlcCtxt->lower_if.receive) || 
        (readWaitOn > PH_LLCNFC_READWAIT_ON))
    {
        result = PHNFCSTVAL(CID_NFC_LLC, NFCSTATUS_INVALID_PARAMETER);
    }
    else if (PH_LLCNFC_READPEND_FLAG_OFF != 
            psLlcCtxt->s_frameinfo.read_pending)
    {
        /* do nothing */
    } 
    else 
    {
        if (PH_LLCNFC_READWAIT_OFF == readWaitOn)
        {   
            result = psLlcCtxt->lower_if.receive(
                            psLlcCtxt->lower_if.pcontext, 
                            psLlcCtxt->phwinfo, 
                            pLlcBuffer, 
                            (uint8_t)llcBufferLength);
        } 
        else
        {
            result = psLlcCtxt->lower_if.receive_wait(
                            psLlcCtxt->lower_if.pcontext, 
                            psLlcCtxt->phwinfo, 
                            pLlcBuffer, 
                            (uint16_t)llcBufferLength);
        }

        if(NFCSTATUS_PENDING == result)
        {
            if (PH_LLCNFC_READPEND_ONE_BYTE == llcBufferLength)
            {
                psLlcCtxt->s_frameinfo.read_pending = 
                                    PH_LLCNFC_READPEND_ONE_BYTE;
            }
            else
            {
                psLlcCtxt->s_frameinfo.read_pending = 
                                    PH_LLCNFC_READPEND_REMAIN_BYTE;
            }
        }
    }
    PH_LLCNFC_DEBUG("Llc Dal Interface Read result : 0x%x\n", result);
    return result;
}

NFCSTATUS 
phLlcNfc_Interface_Write(
    phLlcNfc_Context_t      *psLlcCtxt,
    uint8_t             *pLlcBuffer, 
    uint32_t            llcBufferLength
)
{
    NFCSTATUS   result = NFCSTATUS_PENDING;

    PH_LLCNFC_PRINT("Llc Dal Interface Write called\n");
    /*
        1. Call DAL or TL write with "phLlcNfc_LlcTl_WrResp_Cb" as 
            callback function
    */
    if ((NULL == psLlcCtxt) || (NULL == pLlcBuffer) || 
        (0 == llcBufferLength) || 
        (NULL == psLlcCtxt->lower_if.send))
    {
        PH_LLCNFC_DEBUG ("psLlcCtxt : 0x%08X\n", psLlcCtxt);
        PH_LLCNFC_DEBUG ("pLlcBuffer : 0x%08X\n", pLlcBuffer);
        PH_LLCNFC_DEBUG ("llcBufferLength : 0x%08X\n", llcBufferLength);
        result = PHNFCSTVAL(CID_NFC_LLC, NFCSTATUS_INVALID_PARAMETER);
    }
    else 
    {        
        PH_LLCNFC_PRINT("Buffer to be send to Dal : \n");
        PH_LLCNFC_PRINT_BUFFER(pLlcBuffer, llcBufferLength);

        if ((TRUE == psLlcCtxt->s_frameinfo.write_pending) || 
            (PH_LLCNFC_READPEND_REMAIN_BYTE == 
            psLlcCtxt->s_frameinfo.read_pending))
        {
            result = PHNFCSTVAL(CID_NFC_LLC, NFCSTATUS_BUSY);
        }
        else
        {
#ifdef LLC_DATA_BYTES

            PH_LLCNFC_PRINT_DATA (pLlcBuffer, llcBufferLength);
            PH_LLCNFC_STRING (";\n");
    
#endif /* LLC_DATA_BYTES */
            result = psLlcCtxt->lower_if.send(psLlcCtxt->lower_if.pcontext, 
                                                psLlcCtxt->phwinfo, 
                                                pLlcBuffer, 
                                                (uint16_t)llcBufferLength);
            if(NFCSTATUS_PENDING == result)
            {
                psLlcCtxt->s_frameinfo.write_pending = TRUE;
            }
        }
    }
    PH_LLCNFC_DEBUG("Llc Dal Interface Write result : 0x%x\n", result);
    return result;
}

static
void 
phLlcNfc_WrResp_Cb(
    void                        *pContext, 
    void                        *pHwInfo, 
    phNfc_sTransactionInfo_t    *pCompInfo
)
{
    /* 
        1. Check the window size, if window size = windows
        1. Call the send callback, which has been registered by upper 
            layer
    */
    NFCSTATUS                   result = NFCSTATUS_SUCCESS;
    phLlcNfc_Context_t          *ps_llc_ctxt = (phLlcNfc_Context_t*)pContext;
    phLlcNfc_Frame_t            *ps_frame_info = NULL;
    phLlcNfc_LlcPacket_t        *ps_recv_pkt = NULL;
    phLlcNfc_StoreIFrame_t      *ps_store_frame = NULL;
    phNfc_sCompletionInfo_t     notifyinfo;
    uint8_t                     count = 0;

    PH_LLCNFC_PRINT("\n\nLLC : WRITE RESP CB CALLED\n\n");
    
    if ((NULL != ps_llc_ctxt) && (NULL != pCompInfo) && (NULL != pHwInfo))
    {
        ps_llc_ctxt->s_frameinfo.write_pending = FALSE;

        PHNFC_UNUSED_VARIABLE(result);
        
        if(NFCSTATUS_SUCCESS == pCompInfo->status)
        {
            ps_frame_info = &(ps_llc_ctxt->s_frameinfo);
            ps_recv_pkt = &(ps_frame_info->s_recvpacket);
            ps_store_frame = &(ps_frame_info->s_send_store);
            count = ps_frame_info->s_send_store.start_pos;

            PH_LLCNFC_DEBUG("RECEIVE length : 0x%02X\n", ps_recv_pkt->llcbuf_len);
            PH_LLCNFC_DEBUG("SENT frame type : 0x%02X\n", ps_frame_info->sent_frame_type);
            PH_LLCNFC_DEBUG("WRITE PENDING : 0x%02X\n", ps_frame_info->write_pending);
            PH_LLCNFC_DEBUG("WRITE PENDING status : 0x%04X\n", ps_frame_info->write_status);
            PH_LLCNFC_DEBUG("WRITE wait frame type : 0x%02X\n", ps_frame_info->write_wait_call);
            PH_LLCNFC_DEBUG("NS START POS : 0x%02X\n", ps_store_frame->start_pos);
            PH_LLCNFC_DEBUG("WIN SIZE : 0x%02X\n", ps_store_frame->winsize_cnt);

            switch(ps_frame_info->sent_frame_type)
            {
                case init_u_rset_frame:
                {
                    /* First U frame sent properly, update sent frame type 
                        in the callback */
                    result = phLlcNfc_Interface_Read (ps_llc_ctxt, 
                                    PH_LLCNFC_READWAIT_OFF, 
                                    &(ps_recv_pkt->s_llcbuf.llc_length_byte),
                                    (uint8_t)PH_LLCNFC_BYTES_INIT_READ);

                    if (NFCSTATUS_BUSY == 
                        PHNFCSTATUS (ps_frame_info->write_status))
                    {
                        ps_frame_info->write_status = NFCSTATUS_PENDING;
                        result = phLlcNfc_H_WriteWaitCall (ps_llc_ctxt);
                    }
                    break;
                }

                case init_u_a_frame:
                {
                    /* First UA frame sent properly, update sent frame type 
                        in the callback. Send the notification to the 
                        upper layer */
                    ps_frame_info->sent_frame_type = write_resp_received;
                    result = phLlcNfc_Interface_Read (ps_llc_ctxt, 
                                    PH_LLCNFC_READWAIT_OFF, 
                                    &(ps_recv_pkt->s_llcbuf.llc_length_byte),
                                    (uint8_t)PH_LLCNFC_BYTES_INIT_READ);

                    if(NULL != ps_llc_ctxt->cb_for_if.notify)
                    {
                        notifyinfo.status = NFCSTATUS_SUCCESS;
                        ps_llc_ctxt->cb_for_if.notify (
                                        ps_llc_ctxt->cb_for_if.pif_ctxt, 
                                        ps_llc_ctxt->phwinfo, 
                                        NFC_NOTIFY_INIT_COMPLETED, 
                                        &notifyinfo);
                    }                    
                    break;
                }

                case u_rset_frame:
                {
                    /* Retries has failed to work, so U frame is sent */
                    ps_frame_info->sent_frame_type = write_resp_received;

                    if (NFCSTATUS_BUSY == 
                        PHNFCSTATUS (ps_frame_info->write_status))
                    {
                        ps_frame_info->write_status = NFCSTATUS_PENDING;
                        result = phLlcNfc_H_WriteWaitCall (ps_llc_ctxt);
                    }
                    break;
                }

                case user_i_frame:
                {
                    /* Send complete */
                    count = ps_frame_info->n_s;

                    ps_store_frame->s_llcpacket[count].frame_to_send = 
                    ps_frame_info->sent_frame_type = write_resp_received;

                    /* N(S) shall be incremented now, because, this callback
                        ensures that packet is sent */
                    count = 
                    ps_frame_info->n_s = ((ps_frame_info->n_s + 1) % 
                                            PH_LLCNFC_MOD_NS_NR);

                    result = phLlcNfc_Interface_Read(ps_llc_ctxt, 
                                    PH_LLCNFC_READWAIT_OFF, 
                                    &(ps_recv_pkt->s_llcbuf.llc_length_byte),
                                    (uint8_t)PH_LLCNFC_BYTES_INIT_READ);

                    if (NFCSTATUS_BUSY == 
                        PHNFCSTATUS (ps_frame_info->write_status))
                    {
                        ps_frame_info->write_status = NFCSTATUS_PENDING;
                        result = phLlcNfc_H_WriteWaitCall (ps_llc_ctxt);
                    }
                   

                    if ((((ps_store_frame->start_pos + ps_store_frame->winsize_cnt) % 
                        PH_LLCNFC_MOD_NS_NR) == ps_frame_info->n_s) && 
                        (ps_frame_info->window_size == ps_store_frame->winsize_cnt))
                    {
                        /* Don't call the upper layer send completion callback, 
                            because last sent frame is the maximum that can be 
                            held by LLC due to windowing
                            store the callback info, call send completion shall 
                            be sent to upper layer only after the ACK is received for the 
                            I frames */
                        ps_llc_ctxt->send_cb_len = (pCompInfo->length - 
                                                    PH_LLCNFC_APPEND_LEN);
                    }
                    else 
                    {
                        /* Send completion is sent to upper layer 
                        Actually, this allows the upper layer to send data, if any
                        */
                        if (NULL != ps_llc_ctxt->cb_for_if.send_complete) 
                        {                            
                            pCompInfo->length = (pCompInfo->length - 
                                                PH_LLCNFC_APPEND_LEN);
                            ps_llc_ctxt->cb_for_if.send_complete (
                                        ps_llc_ctxt->cb_for_if.pif_ctxt, 
                                        pHwInfo, pCompInfo);
                        }
                    }
                    break;
                }

                case s_frame:
                {
#if 0
                    uint8_t         i_frame_ns_value = 0;
#endif /* #if 0 */
                    /* S frame is only sent when ever a I frame is received from  
                        the PN544 in the read response callback, so the received I 
                        frame is acknowledged with S frame. The write response 
                        callback for sent S frame is in progress. */
                    ps_frame_info->sent_frame_type = write_resp_received;

#if 0
                    i_frame_ns_value = 
                        ((ps_store_frame->s_llcpacket[count].s_llcbuf.sllcpayload.llcheader 
                        & LLC_NS_FRAME_HEADER_MASK) >> PH_LLCNFC_NS_START_BIT_POS);


                     PH_LLCNFC_DEBUG("Actual ns value : 0x%02X\n",
                                                  i_frame_ns_value);
#endif /* #if 0 */

                     PH_LLCNFC_DEBUG("Window size : 0x%02X\n",
                                       ps_frame_info->s_send_store.winsize_cnt);
                     PH_LLCNFC_DEBUG("frame to send : 0x%02X\n",
                              ps_store_frame->s_llcpacket[count].frame_to_send);

                    if (NFCSTATUS_BUSY == 
                        PHNFCSTATUS(ps_frame_info->write_status))
                    {
                        ps_frame_info->write_status = NFCSTATUS_PENDING;
                        result = phLlcNfc_H_WriteWaitCall (ps_llc_ctxt);
                    }
#ifdef LLC_UPP_LAYER_NTFY_WRITE_RSP_CB
                    phLlcNfc_H_SendInfo (ps_llc_ctxt);
#endif /* #ifdef LLC_UPP_LAYER_NTFY_WRITE_RSP_CB */
                    break;
                }

#ifdef LLC_RR_INSTEAD_OF_REJ
                case rej_rr_s_frame:
                {
                    if (NFCSTATUS_BUSY == 
                        PHNFCSTATUS(ps_frame_info->write_status))
                    {
                        ps_frame_info->write_status = NFCSTATUS_PENDING;
                        result = phLlcNfc_H_WriteWaitCall (ps_llc_ctxt);
                    }
                    break;
                }
#endif /* #ifdef LLC_RR_INSTEAD_OF_REJ */

                case resend_i_frame:
                {
                    /* The code reaches here, only if stored I frame is sent 
                        No changes here, but send next I frame from the stored list, 
                        in the read response callback, only if proper S or I frame 
                        is received from the PN544 */                    
                    result = phLlcNfc_Interface_Read(ps_llc_ctxt, 
                                    PH_LLCNFC_READWAIT_OFF, 
                                    &(ps_recv_pkt->s_llcbuf.llc_length_byte),
                                    (uint8_t)PH_LLCNFC_BYTES_INIT_READ);

                    if (NFCSTATUS_BUSY == PHNFCSTATUS(ps_frame_info->write_status))
                    {
                        ps_frame_info->write_status = NFCSTATUS_PENDING;
                        result = phLlcNfc_H_WriteWaitCall (ps_llc_ctxt);
                    }

                    if (ps_store_frame->winsize_cnt == 
                        ps_frame_info->window_size)
                    {
                        /* Don't call the upper layer send completion callback, 
                            store the callback info, call send completion after 
                            ack for written frame 
                        ps_llc_ctxt->send_cb_len = pCompInfo->length; */
                    }
                    else 
                    {
                        if(NULL != ps_llc_ctxt->cb_for_if.send_complete) 
                        {
                            pCompInfo->length = (pCompInfo->length - 
                                                PH_LLCNFC_APPEND_LEN);
                            ps_llc_ctxt->cb_for_if.send_complete(
                                        ps_llc_ctxt->cb_for_if.pif_ctxt, 
                                        pHwInfo, pCompInfo);
                        }
                    } 

                    if(user_i_frame == 
                        ps_store_frame->s_llcpacket[count].frame_to_send)
                    {
                        /* Send complete */
                        ps_store_frame->s_llcpacket[count].frame_to_send = 
                                                            resend_i_frame;
                    }
                    break;
                }

                case rejected_i_frame:
                {
                    /* Update the sent frame type, if window size count is 0 */
                    ps_frame_info->sent_frame_type = write_resp_received;
                    /* The code enters here, whenever a I frame is resent and for 
                        this resent I frame, an I frame received from PN544. 
                        So the S frame is sent as the acknowledgment */
                    if (NFCSTATUS_BUSY == 
                            PHNFCSTATUS(ps_frame_info->write_status))
                    {
                        ps_frame_info->write_status = NFCSTATUS_PENDING;
                        result = phLlcNfc_H_WriteWaitCall (ps_llc_ctxt);
                    }
                    break;
                }

                case resend_s_frame:
                {
                    /* Update the sent frame type, if window size count is 0 */
                    ps_frame_info->sent_frame_type = write_resp_received;
                    /* The code enters here, whenever a I frame is resent and for 
                        this resent I frame, an I frame received from PN544. 
                        So the S frame is sent as the acknowledgment */
                    if (NFCSTATUS_BUSY == 
                            PHNFCSTATUS(ps_frame_info->write_status))
                    {
                        ps_frame_info->write_status = NFCSTATUS_PENDING;
                        result = phLlcNfc_H_WriteWaitCall (ps_llc_ctxt);
                    }
                    
#ifdef LLC_UPP_LAYER_NTFY_WRITE_RSP_CB
                    phLlcNfc_H_SendInfo (ps_llc_ctxt);
#endif /* #ifdef LLC_UPP_LAYER_NTFY_WRITE_RSP_CB */
                    break;
                }

                case reject_s_frame:
                {
                    result = phLlcNfc_Interface_Read(ps_llc_ctxt, 
                                    PH_LLCNFC_READWAIT_OFF, 
                                    &(ps_recv_pkt->s_llcbuf.llc_length_byte),
                                    (uint8_t)PH_LLCNFC_BYTES_INIT_READ);

                    if (NFCSTATUS_BUSY == 
                        PHNFCSTATUS(ps_frame_info->write_status))
                    {
                        ps_frame_info->write_status = NFCSTATUS_PENDING;
                        result = phLlcNfc_H_WriteWaitCall (ps_llc_ctxt);
                    }
                    break;
                }

                case u_a_frame:
                {
                    result = phLlcNfc_Interface_Read(ps_llc_ctxt, 
                                    PH_LLCNFC_READWAIT_OFF, 
                                    &(ps_recv_pkt->s_llcbuf.llc_length_byte),
                                    (uint8_t)PH_LLCNFC_BYTES_INIT_READ);

                    PH_LLCNFC_DEBUG("WIN SIZE : 0x%02X\n", ps_frame_info->s_send_store.winsize_cnt);

                    if(ps_frame_info->s_send_store.winsize_cnt > 0)
                    {
                        result = phLlcNfc_H_SendUserIFrame (ps_llc_ctxt,
                                            &(ps_frame_info->s_send_store));
                    }
                    break;
                }

                case resend_rej_s_frame:
                {
                    result = phLlcNfc_Interface_Read(ps_llc_ctxt, 
                                    PH_LLCNFC_READWAIT_OFF, 
                                    &(ps_recv_pkt->s_llcbuf.llc_length_byte),
                                    (uint8_t)PH_LLCNFC_BYTES_INIT_READ);

                    PH_LLCNFC_DEBUG("WIN SIZE : 0x%02X\n", ps_frame_info->s_send_store.winsize_cnt);

                    if(ps_frame_info->s_send_store.winsize_cnt > 0)
                    {
                        result = phLlcNfc_H_SendTimedOutIFrame (ps_llc_ctxt,
                                            &(ps_frame_info->s_send_store), 0);
                    }
                    break;
                }

                default :
                {
                    break;
                }
            }
        }
        else
        {
            /* Write not successful */
            if(NULL != ps_llc_ctxt->cb_for_if.send_complete)
            {
                phLlcNfc_StopTimers(PH_LLCNFC_GUARDTIMER, 
                                    ps_llc_ctxt->s_timerinfo.guard_to_count);
                PH_LLCNFC_DEBUG("Error status received : 0x%x\n", pCompInfo->status);
                ps_llc_ctxt->cb_for_if.send_complete(
                                    ps_llc_ctxt->cb_for_if.pif_ctxt, 
                                    pHwInfo, pCompInfo);
            }
        }
    }
    PH_LLCNFC_PRINT("\n\nLLC : WRITE RESP CB END\n\n");
}

static
void 
phLlcNfc_RdResp_Cb(
    void                        *pContext,
    void                        *pHwInfo,
    phNfc_sTransactionInfo_t    *pCompInfo
)
{
    /*
        1. LLC Receive has been called by the upper layer, the response 
            for this function is called by the lower layer
        2. Get the frame information from the receive buffer
        3. Depending on the received frame type, process the received 
            buffer
    */
    NFCSTATUS                   result = NFCSTATUS_SUCCESS;
    phLlcNfc_Context_t          *ps_llc_ctxt = (phLlcNfc_Context_t*)pContext;
    void                        *p_upperctxt = NULL;
    uint8_t                     crc1 = 0, 
                                crc2 = 0;
    phLlcNfc_Frame_t            *ps_frame_info = NULL;
    phLlcNfc_LlcPacket_t        *ps_recv_pkt = NULL;
    phLlcNfc_Payload_t          *ps_llc_payload = NULL;
    pphNfcIF_Notification_CB_t  notifyul = NULL;
    phNfc_sCompletionInfo_t     notifyinfo;

    PH_LLCNFC_PRINT("\n\nLLC : READ RESP CB CALLED\n\n");
    
    if ((NULL != ps_llc_ctxt) && (NULL != pCompInfo) && (NULL != pHwInfo)
       && (pCompInfo->length != 0) && (NULL != pCompInfo->buffer))
    {
        ps_frame_info = &(ps_llc_ctxt->s_frameinfo);
        ps_recv_pkt = &(ps_frame_info->s_recvpacket);
        ps_llc_payload = &(ps_recv_pkt->s_llcbuf.sllcpayload);

        ps_llc_ctxt->s_frameinfo.read_pending = PH_LLCNFC_READPEND_FLAG_OFF;
        
        if (NFCSTATUS_SUCCESS == pCompInfo->status)
        {
            if ((PH_LLCNFC_MIN_BUFLEN_RECVD == pCompInfo->length) &&
                (((PH_LLCNFC_MIN_BUFLEN_RECVD + 1) < *(pCompInfo->buffer)) &&
                (PH_LLCNFC_MAX_BUFLEN_RECV_SEND > *(pCompInfo->buffer))))
            {
                PH_LLCNFC_PRINT("Buffer received : \n");
                PH_LLCNFC_PRINT_BUFFER(pCompInfo->buffer, pCompInfo->length);

#if 0
                /* Received length is 1 and receive buffer 
                contains the length field which is greater than 2, 
                so read the remaining bytes*/
                ps_recv_pkt->s_llcbuf.llc_length_byte = pCompInfo->buffer[0];
#endif
                result = phLlcNfc_Interface_Read(ps_llc_ctxt, 
                                PH_LLCNFC_READWAIT_OFF, 
                                (uint8_t *)ps_llc_payload, 
                                (uint32_t)(ps_recv_pkt->s_llcbuf.llc_length_byte));

                if ((init_u_rset_frame == ps_frame_info->sent_frame_type) && 
                    (NFCSTATUS_PENDING != result) && 
                    (NULL != ps_llc_ctxt->cb_for_if.notify))
                {
                    PH_LLCNFC_PRINT("Initialised error\n");
                    notifyinfo.status = result;
                    /* Copy the upper layer callback pointer and the upper 
                        layer context, after that call release */
                    notifyul = ps_llc_ctxt->cb_for_if.notify;
                    p_upperctxt = ps_llc_ctxt->cb_for_if.pif_ctxt;
                    result = phLlcNfc_Release(ps_llc_ctxt, pHwInfo);

                    /* Wrong result, so Init failed sent */
                    notifyul(p_upperctxt, pHwInfo, 
                            NFC_NOTIFY_INIT_FAILED, &notifyinfo);
                }
            }
            else if (TRUE == ps_llc_ctxt->s_frameinfo.write_pending)
            {
                /* Ignore the bytes as write is not complete and 
                pend a read for reading 1 byte */
                result = phLlcNfc_Interface_Read(ps_llc_ctxt, 
                                PH_LLCNFC_READWAIT_OFF, 
                                (uint8_t *)&(
                                ps_recv_pkt->s_llcbuf.llc_length_byte), 
                                PH_LLCNFC_MIN_BUFLEN_RECVD);
            }
            else if (((PH_LLCNFC_MIN_BUFLEN_RECVD + 1) < pCompInfo->length) &&
                (PH_LLCNFC_MAX_BUFLEN_RECV_SEND > pCompInfo->length) && 
                (pCompInfo->length == ps_recv_pkt->s_llcbuf.llc_length_byte))
            {
                PH_LLCNFC_PRINT("Buffer received : \n");
                PH_LLCNFC_PRINT_BUFFER(pCompInfo->buffer, pCompInfo->length);
                PH_LLCNFC_DEBUG("WIN SIZE : 0x%02X\n", ps_frame_info->s_send_store.winsize_cnt);

                /* Receive is complete, so move the state to INITIALISED */
                if (phLlcNfc_Resend_State != ps_llc_ctxt->state)
                {
                    result = phLlcNfc_H_ChangeState(ps_llc_ctxt, 
                                                    phLlcNfc_Initialised_State);
                }
                /* Copy the received buffer and length */
                ps_recv_pkt->llcbuf_len = (uint8_t)
                                (ps_recv_pkt->s_llcbuf.llc_length_byte + 1);
#if 0
                (void)memcpy(ps_llc_payload, pCompInfo->buffer, 
                            pCompInfo->length);
#endif

                /* 
                Check the CRC
                ps_llc_ctxt->s_frameinfo.s_recvpacket.s_llcbuf : 
                        consists llc length byte + llc header + data + CRC 
                        (which needs to be calculated by the below function)
                ps_llc_ctxt->s_frameinfo.s_recvpacket.llcbuf_len : 
                        Total length of the above buffer
                ps_llc_ctxt->s_frameinfo.s_recvpacket.llcbuf_len - 2 : 
                        -2 because the CRC has to be calculated, only for the 
                        bytes which has llc length byte + llc header + data. 
                        But total length (llcbuf_len) consists of above mentioned 
                        things with 2 byte CRC 
                ps_llc_ctxt->s_frameinfo.s_recvpacket.s_llcbuf.sllcpayload.llcpayload : 
                        consists only data (no length byte and no llc header)
                        (psllcctxt->s_frameinfo.s_recvpacket.llcbuf_len - 4) : 
                        is the array index of the first CRC byte to be calculated
                        (psllcctxt->s_frameinfo.s_recvpacket.llcbuf_len - 3) : 
                        is the array index of the second CRC byte to be calculated
                */
                phLlcNfc_H_ComputeCrc((uint8_t *)&(ps_recv_pkt->s_llcbuf), 
                                    (ps_recv_pkt->llcbuf_len - 2), 
                                    &crc1, &crc2);

                if ((crc1 == ps_llc_payload->llcpayload[
                            (ps_recv_pkt->llcbuf_len - 4)]) 
                    && (crc2 == ps_llc_payload->llcpayload[
                            (ps_recv_pkt->llcbuf_len - 3)]))
                {
                    result = phLlcNfc_H_ProRecvFrame(ps_llc_ctxt);
                }
#ifdef LLC_DISABLE_CRC
                else
                {   
                    result = phLlcNfc_H_ProRecvFrame(ps_llc_ctxt);
                }
#else
                else if (ps_frame_info->recv_error_count < 
                    PH_LLCNFC_MAX_REJ_RETRY_COUNT)
                {
                    PH_LLCNFC_PRINT("CRC ERROR RECVD \n");
                    PH_LLCNFC_DEBUG("RECV ERROR COUNT : 0x%02X\n", ps_frame_info->recv_error_count);

                    ps_frame_info->recv_error_count = (uint8_t)
                                    (ps_frame_info->recv_error_count + 1);

                    result = phLlcNfc_Interface_Read(ps_llc_ctxt, 
                        PH_LLCNFC_READWAIT_OFF, 
                        (uint8_t *)&(ps_recv_pkt->s_llcbuf.llc_length_byte), 
                        PH_LLCNFC_BYTES_INIT_READ);

#ifdef CRC_ERROR_REJ

                    /* Send REJ (S frame), as the CRC received has error  */
                    result = phLlcNfc_H_SendRejectFrame (ps_llc_ctxt);

#endif /* #ifdef CRC_ERROR_REJ */

                }
                else
                {
                    result = phLlcNfc_Interface_Read (ps_llc_ctxt, 
                                PH_LLCNFC_READWAIT_OFF, 
                                (uint8_t *)&(ps_recv_pkt->s_llcbuf.llc_length_byte), 
                                PH_LLCNFC_BYTES_INIT_READ);

                    /* Raise the exception for CRC error received from the  */
                    notifyinfo.status = PHNFCSTVAL(CID_NFC_LLC, 
                                            NFCSTATUS_BOARD_COMMUNICATION_ERROR);
#if 0
                    phOsalNfc_RaiseException(phOsalNfc_e_UnrecovFirmwareErr,1); 
#endif /* #if 0 */
                        /* Resend done, no answer from the device */
                    ps_llc_ctxt->cb_for_if.notify (
                                    ps_llc_ctxt->cb_for_if.pif_ctxt,
                                    ps_llc_ctxt->phwinfo, 
                                    NFC_NOTIFY_DEVICE_ERROR, 
                                    &notifyinfo);
                }

#endif /* #ifdef LLC_DISABLE_CRC */
            } /* read more than 1 byte */
            else if (ps_frame_info->recv_error_count >= 
                    PH_LLCNFC_MAX_REJ_RETRY_COUNT)
            {
                result = phLlcNfc_Interface_Read (ps_llc_ctxt, 
                        PH_LLCNFC_READWAIT_OFF, 
                        (uint8_t *)&(ps_recv_pkt->s_llcbuf.llc_length_byte), 
                        PH_LLCNFC_BYTES_INIT_READ);

                /* Raise the exception for CRC error received from the  */
                notifyinfo.status = PHNFCSTVAL(CID_NFC_LLC, 
                                        NFCSTATUS_BOARD_COMMUNICATION_ERROR);
#if 0
                phOsalNfc_RaiseException(phOsalNfc_e_UnrecovFirmwareErr,1); 
#endif /* #if 0 */
                    /* Resend done, no answer from the device */
                ps_llc_ctxt->cb_for_if.notify (
                                ps_llc_ctxt->cb_for_if.pif_ctxt,
                                ps_llc_ctxt->phwinfo, 
                                NFC_NOTIFY_DEVICE_ERROR, 
                                &notifyinfo);
            }
            else if (((PH_LLCNFC_MIN_BUFLEN_RECVD + 1) < pCompInfo->length) &&
                (PH_LLCNFC_MAX_BUFLEN_RECV_SEND > pCompInfo->length) && 
                (pCompInfo->length != ps_recv_pkt->s_llcbuf.llc_length_byte))
            {
                ps_frame_info->recv_error_count = (uint8_t)
                                    (ps_frame_info->recv_error_count + 1);

                result = phLlcNfc_Interface_Read(ps_llc_ctxt, 
                        PH_LLCNFC_READWAIT_OFF, 
                        (uint8_t *)&(ps_recv_pkt->s_llcbuf.llc_length_byte), 
                        PH_LLCNFC_BYTES_INIT_READ);

#ifdef CRC_ERROR_REJ

                /* Send REJ (S frame), as the CRC received has error  */
                result = phLlcNfc_H_SendRejectFrame (ps_llc_ctxt);

#endif /* #ifdef CRC_ERROR_REJ */
            }
            else if ((PH_LLCNFC_MIN_BUFLEN_RECVD == pCompInfo->length) &&
                ((*(pCompInfo->buffer) > (PH_LLCNFC_MAX_BUFLEN_RECV_SEND - 1))
                ||(*(pCompInfo->buffer) <= (PH_LLCNFC_MIN_BUFLEN_RECVD + 1))))
            {
                /* Temporary fix for the 0xFF data received  
                    Solution for the read one byte, giving error in buffer
                    PH_LLCNFC_MAX_BUFLEN_RECV_SEND (0x21) is the maximum 
                    bytes expected by LLC, if the buffer 
                    value is greater than (0x21 - 1), then pend a read to 
                    get 1 byte again
                */
                ps_frame_info->recv_error_count = (uint8_t)
                                    (ps_frame_info->recv_error_count + 1);

                result = phLlcNfc_Interface_Read(ps_llc_ctxt, 
                        PH_LLCNFC_READWAIT_OFF, 
                        (uint8_t *)&(ps_recv_pkt->s_llcbuf.llc_length_byte), 
                        PH_LLCNFC_BYTES_INIT_READ);
            }
            else
            {
                ps_frame_info->recv_error_count = (uint8_t)
                                    (ps_frame_info->recv_error_count + 1);

                phLlcNfc_StopTimers(PH_LLCNFC_GUARDTIMER, 
                                    ps_llc_ctxt->s_timerinfo.guard_to_count);
                pCompInfo->status = PHNFCSTVAL(CID_NFC_LLC, 
                                                NFCSTATUS_INVALID_FORMAT);
                pCompInfo->buffer = NULL;
                pCompInfo->length = 0;
                result = phLlcNfc_Interface_Read(ps_llc_ctxt, 
                        PH_LLCNFC_READWAIT_OFF, 
                        (uint8_t *)&(ps_recv_pkt->s_llcbuf.llc_length_byte), 
                        PH_LLCNFC_BYTES_INIT_READ);
                if (NULL != ps_llc_ctxt->cb_for_if.receive_complete)
                {
                    ps_llc_ctxt->cb_for_if.receive_complete(
                                        ps_llc_ctxt->cb_for_if.pif_ctxt, 
                                        pHwInfo, pCompInfo);
                }
            }
        }
        else
        {
            ps_frame_info->recv_error_count = (uint8_t)
                                    (ps_frame_info->recv_error_count + 1);

            phLlcNfc_StopTimers(PH_LLCNFC_GUARDTIMER, 
                                ps_llc_ctxt->s_timerinfo.guard_to_count);
            PH_LLCNFC_DEBUG("Status Error : 0x%x\n", pCompInfo->status);
            if (NULL != ps_llc_ctxt->cb_for_if.receive_complete)
            {
                ps_llc_ctxt->cb_for_if.receive_complete(
                                    ps_llc_ctxt->cb_for_if.pif_ctxt, 
                                    pHwInfo, pCompInfo);
            }
        }
    }
    else
    {
        if ((NULL != ps_llc_ctxt) && (NULL != pCompInfo) 
            && (NULL != ps_llc_ctxt->cb_for_if.receive_complete))
        {
            ps_llc_ctxt->cb_for_if.receive_complete(
                                    ps_llc_ctxt->cb_for_if.pif_ctxt, 
                                    pHwInfo, pCompInfo);
        }
    }

    PH_LLCNFC_PRINT("\n\nLLC : READ RESP CB END\n\n");
}

void 
phLlcNfc_H_SendInfo (
                    phLlcNfc_Context_t          *psLlcCtxt
                    )
{
    phLlcNfc_LlcPacket_t        *ps_recv_pkt = NULL;
    phLlcNfc_Frame_t            *ps_frame_info = NULL;
    phNfc_sTransactionInfo_t    comp_info;

    ps_frame_info = &(psLlcCtxt->s_frameinfo);
    ps_recv_pkt = &(ps_frame_info->s_recvpacket);

    if ((ps_recv_pkt->llcbuf_len > 0) && 
        (ps_recv_pkt->llcbuf_len <= PH_LLCNFC_MAX_LLC_PAYLOAD))
    {
        comp_info.status = NFCSTATUS_SUCCESS;
        /* Chop the extra Llc bytes received */
#if 0
        comp_info.length = (ps_recv_pkt->llcbuf_len - 
                            PH_LLCNFC_LEN_APPEND);
#else
        comp_info.length = (uint16_t)psLlcCtxt->recvbuf_length;
#endif /*  */

        if (0 != comp_info.length)
        {
#if 0
            (void)memcpy ((void *)psLlcCtxt->precv_buf, (void *)(
                        ps_recv_pkt->s_llcbuf.sllcpayload.llcpayload), 
                        comp_info.length);
#endif /* #if 0 */
            comp_info.buffer = psLlcCtxt->precv_buf;
        }
        else
        {
            comp_info.buffer = NULL;
        }
    }
    else
    {
        comp_info.status = PHNFCSTVAL(CID_NFC_LLC, 
                                    NFCSTATUS_INVALID_FORMAT);
        comp_info.length = 0;
        comp_info.buffer = NULL;
    }

    (void)phLlcNfc_Interface_Read(psLlcCtxt, 
                        PH_LLCNFC_READWAIT_OFF, 
                        &(ps_recv_pkt->s_llcbuf.llc_length_byte),
                        (uint8_t)PH_LLCNFC_BYTES_INIT_READ);

    if ((NFCSTATUS_SUCCESS == comp_info.status) && 
        (0 == comp_info.length))
    {
        /* May be a NULL I frame received from PN544, so dont do 
            any thing */
    }
    else
    {
        if ((NULL != psLlcCtxt->cb_for_if.receive_complete) && 
            (TRUE == ps_frame_info->upper_recv_call))
        {
            ps_frame_info->upper_recv_call = FALSE;
            psLlcCtxt->cb_for_if.receive_complete(
                                psLlcCtxt->cb_for_if.pif_ctxt, 
                                psLlcCtxt->phwinfo, 
                                &comp_info);
        }
        else
        {
            if (NULL != psLlcCtxt->cb_for_if.notify)
            {
                    psLlcCtxt->cb_for_if.notify(
                            psLlcCtxt->cb_for_if.pif_ctxt, 
                            psLlcCtxt->phwinfo, 
                            NFC_NOTIFY_RECV_COMPLETED, 
                            &comp_info);
            }
        }
    }
    return;
}