summaryrefslogtreecommitdiffstats
path: root/src/phHal4Nfc_P2P.c
blob: eee22b08848a60d1f5803358b035e15e6aabe22f (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
/*
 * 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  phHal4Nfc_P2P.c
 * \brief Hal4Nfc_P2P source.
 *
 * Project: NFC-FRI 1.1
 *
 * $Date: Mon May 31 11:43:43 2010 $
 * $Author: ing07385 $
 * $Revision: 1.56 $
 * $Aliases: NFC_FRI1.1_WK1023_R35_1 $
 *
 */

/* ---------------------------Include files ------------------------------------*/
#include <phHal4Nfc.h>
#include <phHal4Nfc_Internal.h>
#include <phOsalNfc.h>
#include <phOsalNfc_Timer.h>
#include <phHciNfc.h>
#include <phNfcConfig.h>

/* ------------------------------- Macros ------------------------------------*/

#ifdef _WIN32
/*Timeout value for recv data timer for P2P.This timer is used for creating 
  Asynchronous behavior in the scenario where the data is received even before 
  the upper layer calls the phHal4Nfc_receive().*/
#define     PH_HAL4NFC_RECV_CB_TIMEOUT       100U
#else
#define     PH_HAL4NFC_RECV_CB_TIMEOUT      0x00U
#endif/*#ifdef _WIN32*/


/* --------------------Structures and enumerations --------------------------*/

/*timer callback to send already buffered receive data to upper layer*/
static void phHal4Nfc_P2PRecvTimerCb(uint32_t P2PRecvTimerId, void *pContext);

/* ---------------------- Function definitions ------------------------------*/

/*  Transfer the user data to another NfcIP device from the host. 
 *  pTransferCallback is called, when all steps in the transfer sequence are 
 *  completed.*/
NFCSTATUS 
phHal4Nfc_Send(            
                phHal_sHwReference_t                    *psHwReference,
                phHal4Nfc_TransactInfo_t                *psTransferInfo,
                phNfc_sData_t                            sTransferData,
                pphHal4Nfc_SendCallback_t                pSendCallback,                
                void                                    *pContext               
                )
{
    NFCSTATUS RetStatus = NFCSTATUS_PENDING;
    phHal4Nfc_Hal4Ctxt_t *Hal4Ctxt = NULL;

    /*NULL checks*/
    if((NULL == psHwReference) 
        ||( NULL == pSendCallback )
        || (NULL == psTransferInfo)
        )
    {
        phOsalNfc_RaiseException(phOsalNfc_e_PrecondFailed,1);
        RetStatus = PHNFCSTVAL(CID_NFC_HAL ,NFCSTATUS_INVALID_PARAMETER);
    }
    /*Check initialised state*/
    else if((NULL == psHwReference->hal_context)
                        || (((phHal4Nfc_Hal4Ctxt_t *)
                                psHwReference->hal_context)->Hal4CurrentState 
                                               < eHal4StateOpenAndReady)
                        || (((phHal4Nfc_Hal4Ctxt_t *)
                                psHwReference->hal_context)->Hal4NextState 
                                               == eHal4StateClosed))
    {
        RetStatus = PHNFCSTVAL(CID_NFC_HAL ,NFCSTATUS_NOT_INITIALISED);     
    }  
    /*Only NfcIp1 Target can call this API*/
    else if(phHal_eNfcIP1_Initiator != psTransferInfo->remotePCDType)
    {
        RetStatus = PHNFCSTVAL(CID_NFC_HAL ,NFCSTATUS_INVALID_DEVICE);
    }
    else
    { 
        Hal4Ctxt = (phHal4Nfc_Hal4Ctxt_t *)psHwReference->hal_context;
        if(NULL == Hal4Ctxt->psTrcvCtxtInfo)
        {
            RetStatus= PHNFCSTVAL(CID_NFC_HAL ,NFCSTATUS_FAILED);
        }
        /*Check Activated*/
        else if(NFC_EVT_ACTIVATED == Hal4Ctxt->sTgtConnectInfo.EmulationState)
        {
            Hal4Ctxt->sUpperLayerInfo.psUpperLayerCtxt = pContext;     
            /*Register upper layer callback*/
            Hal4Ctxt->psTrcvCtxtInfo->pP2PSendCb  = pSendCallback;      
            PHDBG_INFO("NfcIP1 Send");
            /*allocate buffer to store senddata received from upper layer*/
            if (NULL == Hal4Ctxt->psTrcvCtxtInfo->psUpperSendData)
            {
                Hal4Ctxt->psTrcvCtxtInfo->psUpperSendData = (phNfc_sData_t *)
                        phOsalNfc_GetMemory(sizeof(phNfc_sData_t));
                if(NULL != Hal4Ctxt->psTrcvCtxtInfo->psUpperSendData)
                {
                    (void)memset(Hal4Ctxt->psTrcvCtxtInfo->psUpperSendData, 0, 
                                                    sizeof(phNfc_sData_t));
                    Hal4Ctxt->psTrcvCtxtInfo->TransactionTimerId
                                            = PH_OSALNFC_INVALID_TIMER_ID;
                }
            }

            Hal4Ctxt->psTrcvCtxtInfo->psUpperSendData->buffer
                = sTransferData.buffer;
            Hal4Ctxt->psTrcvCtxtInfo->psUpperSendData->length 
                = sTransferData.length;

            /* If data size is less than Peer's Max frame length, then no chaining is required */
            if(Hal4Ctxt->rem_dev_list[0]->RemoteDevInfo.NfcIP_Info.MaxFrameLength >= sTransferData.length)
            {
                Hal4Ctxt->psTrcvCtxtInfo->
                    XchangeInfo.params.nfc_info.more_info = FALSE;
                Hal4Ctxt->psTrcvCtxtInfo->XchangeInfo.tx_length
                    = (uint8_t)sTransferData.length;
                Hal4Ctxt->psTrcvCtxtInfo->XchangeInfo.tx_buffer
                    = sTransferData.buffer;
            }
            else/*set more_info to true,to indicate more data pending to be sent*/
            {
                Hal4Ctxt->psTrcvCtxtInfo->
                    XchangeInfo.params.nfc_info.more_info = TRUE;
                Hal4Ctxt->psTrcvCtxtInfo->XchangeInfo.tx_length
                    = Hal4Ctxt->rem_dev_list[0]->RemoteDevInfo.NfcIP_Info.MaxFrameLength;
                Hal4Ctxt->psTrcvCtxtInfo->XchangeInfo.tx_buffer
                    = sTransferData.buffer;
                Hal4Ctxt->psTrcvCtxtInfo->NumberOfBytesSent
                    += Hal4Ctxt->rem_dev_list[0]->RemoteDevInfo.NfcIP_Info.MaxFrameLength;
            }
            PHDBG_INFO("HAL4:Calling Hci_Send_data()");
            RetStatus = phHciNfc_Send_Data (
                Hal4Ctxt->psHciHandle,
                psHwReference,
                NULL,
                &(Hal4Ctxt->psTrcvCtxtInfo->XchangeInfo)
                );
            /*check return status*/
            if (NFCSTATUS_PENDING == RetStatus)
            {
                /*Set P2P_Send_In_Progress to defer any disconnect call until
                 Send complete occurs*/
                Hal4Ctxt->psTrcvCtxtInfo->P2P_Send_In_Progress = TRUE;
                Hal4Ctxt->Hal4NextState = eHal4StateTransaction;
                /*No of bytes remaining for next send*/
                Hal4Ctxt->psTrcvCtxtInfo->psUpperSendData->length
                    -= Hal4Ctxt->psTrcvCtxtInfo->XchangeInfo.tx_length;
            }           
        }
        else/*Deactivated*/
        {
            RetStatus = PHNFCSTVAL(CID_NFC_HAL ,NFCSTATUS_DESELECTED);
        }
    }
    return RetStatus;
}


/*  Transfer the user data to the another NfcIP device from the host. 
 *  pTransferCallback is called, when all steps in the transfer sequence are 
 *  completed.*/

NFCSTATUS 
phHal4Nfc_Receive(                
                  phHal_sHwReference_t                  *psHwReference,
                  phHal4Nfc_TransactInfo_t              *psRecvInfo,
                  pphHal4Nfc_ReceiveCallback_t          pReceiveCallback,
                  void                                  *pContext
                 )
{
    NFCSTATUS RetStatus = NFCSTATUS_PENDING;
    phHal4Nfc_Hal4Ctxt_t *Hal4Ctxt = NULL;
     /*NULL checks*/
    if((NULL == psHwReference) 
        ||( NULL == pReceiveCallback)
        ||( NULL == psRecvInfo))
    {
        phOsalNfc_RaiseException(phOsalNfc_e_PrecondFailed,1);
        RetStatus = PHNFCSTVAL(CID_NFC_HAL ,NFCSTATUS_INVALID_PARAMETER);
    }
    /*Check initialised state*/
    else if((NULL == psHwReference->hal_context)
                        || (((phHal4Nfc_Hal4Ctxt_t *)
                                psHwReference->hal_context)->Hal4CurrentState 
                                               < eHal4StateOpenAndReady)
                        || (((phHal4Nfc_Hal4Ctxt_t *)
                                psHwReference->hal_context)->Hal4NextState 
                                               == eHal4StateClosed))
    {
        RetStatus = PHNFCSTVAL(CID_NFC_HAL ,NFCSTATUS_NOT_INITIALISED);     
    }   
    else
    {
        Hal4Ctxt = (phHal4Nfc_Hal4Ctxt_t *)psHwReference->hal_context;
        if(NFC_EVT_ACTIVATED == Hal4Ctxt->sTgtConnectInfo.EmulationState)
        {
            /*Following condition gets satisfied only on target side,if receive
              is not already called*/
            if(NULL == Hal4Ctxt->psTrcvCtxtInfo)
            {
                Hal4Ctxt->psTrcvCtxtInfo= (pphHal4Nfc_TrcvCtxtInfo_t)
                    phOsalNfc_GetMemory((uint32_t)
                    (sizeof(phHal4Nfc_TrcvCtxtInfo_t)));
                if(NULL != Hal4Ctxt->psTrcvCtxtInfo)
                {
                    (void)memset(Hal4Ctxt->psTrcvCtxtInfo,0,
                        sizeof(phHal4Nfc_TrcvCtxtInfo_t));
                    Hal4Ctxt->psTrcvCtxtInfo->TransactionTimerId
                        = PH_OSALNFC_INVALID_TIMER_ID;
                    Hal4Ctxt->psTrcvCtxtInfo->RecvDataBufferStatus = NFCSTATUS_PENDING;
                }
            }
            if(NULL == Hal4Ctxt->psTrcvCtxtInfo)
            {
                phOsalNfc_RaiseException(phOsalNfc_e_NoMemory,0);
                RetStatus= PHNFCSTVAL(CID_NFC_HAL , 
                    NFCSTATUS_INSUFFICIENT_RESOURCES);
            }
            else /*Store callback & Return status pending*/
            {
                Hal4Ctxt->sUpperLayerInfo.psUpperLayerCtxt = pContext;     
                /*Register upper layer callback*/
                Hal4Ctxt->psTrcvCtxtInfo->pUpperTranceiveCb = NULL;
                Hal4Ctxt->psTrcvCtxtInfo->pP2PRecvCb = pReceiveCallback;
                if(NFCSTATUS_PENDING != 
                    Hal4Ctxt->psTrcvCtxtInfo->RecvDataBufferStatus)
                {               
                    /**Create a timer to send received data in the callback*/
                    if(Hal4Ctxt->psTrcvCtxtInfo->TransactionTimerId
                        == PH_OSALNFC_INVALID_TIMER_ID)
                    {
                        PHDBG_INFO("HAL4: Transaction Timer Create for Receive");
                        Hal4Ctxt->psTrcvCtxtInfo->TransactionTimerId 
                            = phOsalNfc_Timer_Create();
                    }
                    if(Hal4Ctxt->psTrcvCtxtInfo->TransactionTimerId
                        == PH_OSALNFC_INVALID_TIMER_ID)
                    {
                        RetStatus = PHNFCSTVAL(CID_NFC_HAL ,
                            NFCSTATUS_INSUFFICIENT_RESOURCES);                      
                    }
                    else/*start the timer*/
                    {
                        phOsalNfc_Timer_Start(
                            Hal4Ctxt->psTrcvCtxtInfo->TransactionTimerId,
                            PH_HAL4NFC_RECV_CB_TIMEOUT,
							 phHal4Nfc_P2PRecvTimerCb,
							 NULL
                            );
                    }
                }
            }
        }
        else/*deactivated*/
        {
            RetStatus= PHNFCSTVAL(CID_NFC_HAL ,NFCSTATUS_DESELECTED);
        }
    }   
    return RetStatus;
}

/*Timer callback for recv data timer for P2P.This timer is used for creating 
  Asynchronous behavior in the scenario where the data is received even before 
  the upper layer calls the phHal4Nfc_receive().*/
static void phHal4Nfc_P2PRecvTimerCb(uint32_t P2PRecvTimerId, void *pContext)
{
    phHal4Nfc_Hal4Ctxt_t *Hal4Ctxt = (phHal4Nfc_Hal4Ctxt_t *)(
                                            gpphHal4Nfc_Hwref->hal_context);
    pphHal4Nfc_ReceiveCallback_t pUpperRecvCb = NULL;
    NFCSTATUS RecvDataBufferStatus = NFCSTATUS_PENDING;
	PHNFC_UNUSED_VARIABLE(pContext);

    phOsalNfc_Timer_Stop(P2PRecvTimerId);            
    phOsalNfc_Timer_Delete(P2PRecvTimerId);
    if(NULL != Hal4Ctxt->psTrcvCtxtInfo)
    {
        RecvDataBufferStatus = Hal4Ctxt->psTrcvCtxtInfo->RecvDataBufferStatus;
        Hal4Ctxt->psTrcvCtxtInfo->RecvDataBufferStatus = NFCSTATUS_PENDING;
    
        Hal4Ctxt->psTrcvCtxtInfo->TransactionTimerId 
            = PH_OSALNFC_INVALID_TIMER_ID; 
        /*Update state*/
        Hal4Ctxt->Hal4NextState = (eHal4StateTransaction
             == Hal4Ctxt->Hal4NextState?eHal4StateInvalid:Hal4Ctxt->Hal4NextState);
        /*Provide address of received data to upper layer data pointer*/
        Hal4Ctxt->psTrcvCtxtInfo->psUpperRecvData 
            = &(Hal4Ctxt->psTrcvCtxtInfo->sLowerRecvData);  
        /*Chk NULL and call recv callback*/
        if(Hal4Ctxt->psTrcvCtxtInfo->pP2PRecvCb != NULL)
        {           
            pUpperRecvCb = Hal4Ctxt->psTrcvCtxtInfo->pP2PRecvCb;
            Hal4Ctxt->psTrcvCtxtInfo->pP2PRecvCb = NULL;        
            (*pUpperRecvCb)(
                Hal4Ctxt->sUpperLayerInfo.psUpperLayerCtxt,
                Hal4Ctxt->psTrcvCtxtInfo->psUpperRecvData,
                RecvDataBufferStatus
                );
        }
    }
    return;
}

/**Send complete handler*/
void phHal4Nfc_SendCompleteHandler(phHal4Nfc_Hal4Ctxt_t  *Hal4Ctxt,void *pInfo)
{
    pphHal4Nfc_SendCallback_t pUpperSendCb = NULL;
    pphHal4Nfc_TransceiveCallback_t pUpperTrcvCb = NULL;
    NFCSTATUS SendStatus = ((phNfc_sCompletionInfo_t *)pInfo)->status;
    pphHal4Nfc_DiscntCallback_t pUpperDisconnectCb = NULL;
    Hal4Ctxt->psTrcvCtxtInfo->P2P_Send_In_Progress = FALSE; 
    /*Send status Success or Pending disconnect in HAl4*/
    if((SendStatus != NFCSTATUS_SUCCESS)
        ||(NFC_INVALID_RELEASE_TYPE != Hal4Ctxt->sTgtConnectInfo.ReleaseType))
    {   
        Hal4Ctxt->Hal4NextState = eHal4StateInvalid;
        /*Update Status*/
        SendStatus = (NFCSTATUS)(NFC_INVALID_RELEASE_TYPE != 
          Hal4Ctxt->sTgtConnectInfo.ReleaseType?NFCSTATUS_RELEASED:SendStatus);
        /*Callback For Target Send*/
        if(NULL != Hal4Ctxt->psTrcvCtxtInfo->pP2PSendCb)
        {
            pUpperSendCb = Hal4Ctxt->psTrcvCtxtInfo->pP2PSendCb;
            Hal4Ctxt->psTrcvCtxtInfo->pP2PSendCb = NULL;
            (*pUpperSendCb)(
                Hal4Ctxt->sUpperLayerInfo.psUpperLayerCtxt,
                SendStatus
                );
        }
        else/*Callback For Initiator Send*/
        {
            if(NULL !=  Hal4Ctxt->psTrcvCtxtInfo->pUpperTranceiveCb)
            {
                Hal4Ctxt->psTrcvCtxtInfo->psUpperRecvData->length = 0;
                pUpperTrcvCb = Hal4Ctxt->psTrcvCtxtInfo->pUpperTranceiveCb;
                Hal4Ctxt->psTrcvCtxtInfo->pUpperTranceiveCb = NULL;
                (*pUpperTrcvCb)(
                    Hal4Ctxt->sUpperLayerInfo.psUpperLayerCtxt,
                    Hal4Ctxt->sTgtConnectInfo.psConnectedDevice,
                    Hal4Ctxt->psTrcvCtxtInfo->psUpperRecvData,
                    SendStatus
                    );
            }
        }
        /*Issue Pending disconnect from HAl4*/
        if(NFC_INVALID_RELEASE_TYPE != Hal4Ctxt->sTgtConnectInfo.ReleaseType)
        {
            SendStatus = phHal4Nfc_Disconnect_Execute(gpphHal4Nfc_Hwref);
            if((NFCSTATUS_PENDING != SendStatus) &&
               (NULL != Hal4Ctxt->sTgtConnectInfo.pUpperDisconnectCb))
            {
                pUpperDisconnectCb = 
                    Hal4Ctxt->sTgtConnectInfo.pUpperDisconnectCb;
                Hal4Ctxt->sTgtConnectInfo.pUpperDisconnectCb = NULL;
                (*pUpperDisconnectCb)(
                    Hal4Ctxt->sUpperLayerInfo.psUpperLayerDisconnectCtxt,
                    Hal4Ctxt->sTgtConnectInfo.psConnectedDevice,
                    SendStatus                            
                    );/*Notify disconnect failed to upper layer*/       
            }
        }           
    }
    else 
    {
        /*More info remaining in send buffer.continue with sending remaining 
          bytes*/
        if(Hal4Ctxt->psTrcvCtxtInfo->psUpperSendData->length
                                            > Hal4Ctxt->rem_dev_list[0]->RemoteDevInfo.NfcIP_Info.MaxFrameLength)
        {           
            /*Set more info*/
            Hal4Ctxt->psTrcvCtxtInfo->
                XchangeInfo.params.nfc_info.more_info = TRUE;
            /*copy to tx_buffer ,remaining bytes.NumberOfBytesSent is the 
              number of bytes already sent from current send buffer.*/
            Hal4Ctxt->psTrcvCtxtInfo->XchangeInfo.tx_buffer
                = (Hal4Ctxt->psTrcvCtxtInfo->psUpperSendData->buffer
                   + Hal4Ctxt->psTrcvCtxtInfo->NumberOfBytesSent);
            Hal4Ctxt->psTrcvCtxtInfo->XchangeInfo.tx_length
                = Hal4Ctxt->rem_dev_list[0]->RemoteDevInfo.NfcIP_Info.MaxFrameLength;
            Hal4Ctxt->psTrcvCtxtInfo->NumberOfBytesSent
                += Hal4Ctxt->rem_dev_list[0]->RemoteDevInfo.NfcIP_Info.MaxFrameLength;
            Hal4Ctxt->psTrcvCtxtInfo->psUpperSendData->length
                -= Hal4Ctxt->rem_dev_list[0]->RemoteDevInfo.NfcIP_Info.MaxFrameLength;
            PHDBG_INFO("Hal4:Calling Hci_senddata() from sendcompletehandler1");
            SendStatus = phHciNfc_Send_Data (
                Hal4Ctxt->psHciHandle,
                gpphHal4Nfc_Hwref,
                Hal4Ctxt->sTgtConnectInfo.psConnectedDevice,
                &(Hal4Ctxt->psTrcvCtxtInfo->XchangeInfo)
                );
            if(NFCSTATUS_PENDING == SendStatus)
            {
                Hal4Ctxt->psTrcvCtxtInfo->P2P_Send_In_Progress = TRUE;
            }
        }
        /*Remaining bytes is less than PH_HAL4NFC_MAX_SEND_LEN*/
        else if(Hal4Ctxt->psTrcvCtxtInfo->psUpperSendData->length > 0)
        {
            Hal4Ctxt->psTrcvCtxtInfo->
                XchangeInfo.params.nfc_info.more_info = FALSE;
            Hal4Ctxt->psTrcvCtxtInfo->XchangeInfo.tx_length
                = (uint8_t)Hal4Ctxt->psTrcvCtxtInfo->psUpperSendData->length;
            Hal4Ctxt->psTrcvCtxtInfo->XchangeInfo.tx_buffer
                = (Hal4Ctxt->psTrcvCtxtInfo->psUpperSendData->buffer
                + Hal4Ctxt->psTrcvCtxtInfo->NumberOfBytesSent);
            Hal4Ctxt->psTrcvCtxtInfo->NumberOfBytesSent = 0;
            /*No of bytes remaining for next send*/
            Hal4Ctxt->psTrcvCtxtInfo->psUpperSendData->length = 0;
            PHDBG_INFO("Hal4:Calling Hci_senddata() from sendcompletehandler2");
            SendStatus = phHciNfc_Send_Data (
                Hal4Ctxt->psHciHandle,
                gpphHal4Nfc_Hwref,
                Hal4Ctxt->sTgtConnectInfo.psConnectedDevice,
                &(Hal4Ctxt->psTrcvCtxtInfo->XchangeInfo)
                );
        }
        else/*No more Bytes left.Send complete*/
        {
            Hal4Ctxt->psTrcvCtxtInfo->NumberOfBytesSent = 0;
            /*Callback For Target Send*/
            if(NULL != Hal4Ctxt->psTrcvCtxtInfo->pP2PSendCb)
            {
                pUpperSendCb = Hal4Ctxt->psTrcvCtxtInfo->pP2PSendCb;
                Hal4Ctxt->psTrcvCtxtInfo->pP2PSendCb = NULL;
                (*pUpperSendCb)(
                    Hal4Ctxt->sUpperLayerInfo.psUpperLayerCtxt,
                     SendStatus
                    );
            }
            else
            {
                /**Start timer to keep track of transceive timeout*/
#ifdef TRANSACTION_TIMER
                phOsalNfc_Timer_Start(
                    Hal4Ctxt->psTrcvCtxtInfo->TransactionTimerId,
                    PH_HAL4NFC_TRANSCEIVE_TIMEOUT,
                    phHal4Nfc_TrcvTimeoutHandler
                    );
#endif /*TRANSACTION_TIMER*/
            }
        }
    }
    return;
}

/**Receive complete handler*/
void phHal4Nfc_RecvCompleteHandler(phHal4Nfc_Hal4Ctxt_t  *Hal4Ctxt,void *pInfo)
{
    pphHal4Nfc_ReceiveCallback_t pUpperRecvCb = NULL;
    pphHal4Nfc_TransceiveCallback_t pUpperTrcvCb = NULL;
    NFCSTATUS RecvStatus = ((phNfc_sTransactionInfo_t *)pInfo)->status;
    /*allocate TrcvContext if not already allocated.Required since 
     Receive complete can occur before any other send /receive calls.*/
    if(NULL == Hal4Ctxt->psTrcvCtxtInfo)
    {
        Hal4Ctxt->psTrcvCtxtInfo= (pphHal4Nfc_TrcvCtxtInfo_t)
            phOsalNfc_GetMemory((uint32_t)
            (sizeof(phHal4Nfc_TrcvCtxtInfo_t)));
        if(NULL != Hal4Ctxt->psTrcvCtxtInfo)
        {
            (void)memset(Hal4Ctxt->psTrcvCtxtInfo,0,
                sizeof(phHal4Nfc_TrcvCtxtInfo_t));  
            Hal4Ctxt->psTrcvCtxtInfo->TransactionTimerId
                = PH_OSALNFC_INVALID_TIMER_ID;
            Hal4Ctxt->psTrcvCtxtInfo->RecvDataBufferStatus 
                = NFCSTATUS_PENDING;
        }       
    }
    if(NULL == Hal4Ctxt->psTrcvCtxtInfo)
    {
        phOsalNfc_RaiseException(phOsalNfc_e_NoMemory,0);
        RecvStatus = PHNFCSTVAL(CID_NFC_HAL , 
            NFCSTATUS_INSUFFICIENT_RESOURCES);
    }
    else
    {
        /*Allocate 4K buffer to copy the received data into*/
        if(NULL == Hal4Ctxt->psTrcvCtxtInfo->sLowerRecvData.buffer)
        {
            Hal4Ctxt->psTrcvCtxtInfo->sLowerRecvData.buffer
                = (uint8_t *)phOsalNfc_GetMemory(
                        PH_HAL4NFC_MAX_RECEIVE_BUFFER
                        );
            if(NULL == Hal4Ctxt->psTrcvCtxtInfo->sLowerRecvData.buffer)
            {
                phOsalNfc_RaiseException(phOsalNfc_e_NoMemory,
                    0);
                RecvStatus = NFCSTATUS_INSUFFICIENT_RESOURCES;
            }
            else/*memset*/
            {
                (void)memset(
                    Hal4Ctxt->psTrcvCtxtInfo->sLowerRecvData.buffer,
                    0,
                    PH_HAL4NFC_MAX_RECEIVE_BUFFER
                    );
            }
        }

        if(RecvStatus != NFCSTATUS_INSUFFICIENT_RESOURCES)
        {
            /*Copy the data*/
            (void)memcpy(
                (Hal4Ctxt->psTrcvCtxtInfo->sLowerRecvData.buffer
                + Hal4Ctxt->psTrcvCtxtInfo->P2PRecvLength),
                ((phNfc_sTransactionInfo_t *)pInfo)->buffer,
                ((phNfc_sTransactionInfo_t *)pInfo)->length
                );
            /*Update P2PRecvLength,this also acts as the offset to append more 
              received bytes*/
            Hal4Ctxt->psTrcvCtxtInfo->P2PRecvLength 
                += ((phNfc_sTransactionInfo_t *)pInfo)->length;
            Hal4Ctxt->psTrcvCtxtInfo->sLowerRecvData.length 
                = Hal4Ctxt->psTrcvCtxtInfo->P2PRecvLength;
        }

        if(RecvStatus != NFCSTATUS_MORE_INFORMATION)
        {
            Hal4Ctxt->psTrcvCtxtInfo->P2PRecvLength = 0;          
            Hal4Ctxt->Hal4NextState = (eHal4StateTransaction
             == Hal4Ctxt->Hal4NextState?eHal4StateInvalid:Hal4Ctxt->Hal4NextState);
            if(NFCSTATUS_PENDING == Hal4Ctxt->psTrcvCtxtInfo->RecvDataBufferStatus)
            {
                /*Initiator case*/
                if(NULL != Hal4Ctxt->psTrcvCtxtInfo->pUpperTranceiveCb)
                {
                    RecvStatus =(NFCSTATUS_RF_TIMEOUT == RecvStatus?
                                NFCSTATUS_DESELECTED:RecvStatus);
                    pUpperTrcvCb = Hal4Ctxt->psTrcvCtxtInfo->pUpperTranceiveCb;
                    Hal4Ctxt->psTrcvCtxtInfo->pUpperTranceiveCb = NULL;
                    Hal4Ctxt->psTrcvCtxtInfo->psUpperRecvData 
                        = &(Hal4Ctxt->psTrcvCtxtInfo->sLowerRecvData);
                    (*pUpperTrcvCb)(
                        Hal4Ctxt->sUpperLayerInfo.psUpperLayerCtxt,
                        Hal4Ctxt->sTgtConnectInfo.psConnectedDevice,                
                        Hal4Ctxt->psTrcvCtxtInfo->psUpperRecvData,
                        RecvStatus
                        );
                }
                /*P2P target*/
                else if(NULL != Hal4Ctxt->psTrcvCtxtInfo->pP2PRecvCb)
                {
                    pUpperRecvCb = Hal4Ctxt->psTrcvCtxtInfo->pP2PRecvCb;
                    Hal4Ctxt->psTrcvCtxtInfo->pP2PRecvCb = NULL;
                    Hal4Ctxt->psTrcvCtxtInfo->psUpperRecvData 
                        = &(Hal4Ctxt->psTrcvCtxtInfo->sLowerRecvData);
                    (*pUpperRecvCb)(
                        Hal4Ctxt->sUpperLayerInfo.psUpperLayerCtxt,
                        Hal4Ctxt->psTrcvCtxtInfo->psUpperRecvData,
                        RecvStatus
                        );
                }
                else
                {
                    /*Receive data buffer is complete with data & P2P receive has
                      not yet been called*/
                    Hal4Ctxt->psTrcvCtxtInfo->RecvDataBufferStatus 
                        = NFCSTATUS_SUCCESS;
                }
            }
        }
    }
    return;
}

/*Activation complete handler*/
void phHal4Nfc_P2PActivateComplete(
                             phHal4Nfc_Hal4Ctxt_t  *Hal4Ctxt,
                             void *pInfo
                            )
{
    phHal_sEventInfo_t *psEventInfo = (phHal_sEventInfo_t *)pInfo;
    NFCSTATUS Status = NFCSTATUS_SUCCESS;
    static phHal4Nfc_DiscoveryInfo_t sDiscoveryInfo;
    /*Copy notification info to provide to upper layer*/
    phHal4Nfc_NotificationInfo_t uNotificationInfo = {&sDiscoveryInfo};
    Hal4Ctxt->sTgtConnectInfo.EmulationState = NFC_EVT_ACTIVATED;
    /*if P2p notification is registered*/
    if( NULL != Hal4Ctxt->sUpperLayerInfo.pP2PNotification)
    {
        /*Allocate remote device Info for P2P target*/
        uNotificationInfo.psDiscoveryInfo->NumberOfDevices = 1;
        if(NULL == Hal4Ctxt->rem_dev_list[0])
        {
            Hal4Ctxt->rem_dev_list[0] 
                = (phHal_sRemoteDevInformation_t *)
                    phOsalNfc_GetMemory(
                    sizeof(phHal_sRemoteDevInformation_t)
                    );
        }
        if(NULL == Hal4Ctxt->rem_dev_list[0])
        {
            phOsalNfc_RaiseException(phOsalNfc_e_NoMemory,0);
            Status = PHNFCSTVAL(CID_NFC_HAL , 
                NFCSTATUS_INSUFFICIENT_RESOURCES);
        }
        else
        {
            (void)memset((void *)Hal4Ctxt->rem_dev_list[0],
                                0,sizeof(phHal_sRemoteDevInformation_t));
            /*Copy device info*/
            (void)memcpy(Hal4Ctxt->rem_dev_list[0],
                                psEventInfo->eventInfo.pRemoteDevInfo,
                                sizeof(phHal_sRemoteDevInformation_t)
                                );
            /*Allocate Trcv context info*/
            if(NULL == Hal4Ctxt->psTrcvCtxtInfo)
            {
                Hal4Ctxt->psTrcvCtxtInfo= (pphHal4Nfc_TrcvCtxtInfo_t)
                    phOsalNfc_GetMemory((uint32_t)
                    (sizeof(phHal4Nfc_TrcvCtxtInfo_t)));
                if(NULL != Hal4Ctxt->psTrcvCtxtInfo)
                {
                    (void)memset(Hal4Ctxt->psTrcvCtxtInfo,0,
                        sizeof(phHal4Nfc_TrcvCtxtInfo_t));
                    Hal4Ctxt->psTrcvCtxtInfo->RecvDataBufferStatus 
                        = NFCSTATUS_PENDING;
                    Hal4Ctxt->psTrcvCtxtInfo->TransactionTimerId
                                        = PH_OSALNFC_INVALID_TIMER_ID;
                }
            }
            if(NULL == Hal4Ctxt->psTrcvCtxtInfo)
            {
                phOsalNfc_RaiseException(phOsalNfc_e_NoMemory,0);
                Status= PHNFCSTVAL(CID_NFC_HAL , 
                    NFCSTATUS_INSUFFICIENT_RESOURCES);
            }
            else
            {
                /*Update state*/
                Hal4Ctxt->Hal4CurrentState = eHal4StateEmulation;
                Hal4Ctxt->Hal4NextState = eHal4StateInvalid;
                uNotificationInfo.psDiscoveryInfo->ppRemoteDevInfo
                    = Hal4Ctxt->rem_dev_list;
                /*set session Opened ,this will keep track of whether the session 
                 is alive.will be reset if a Event DEACTIVATED is received*/
                Hal4Ctxt->rem_dev_list[0]->SessionOpened = TRUE;
                (*Hal4Ctxt->sUpperLayerInfo.pP2PNotification)(
                    Hal4Ctxt->sUpperLayerInfo.P2PDiscoveryCtxt,
                    NFC_DISCOVERY_NOTIFICATION,
                    uNotificationInfo,
                    Status
                    );
            }
        }
    }
    return;
}

/*Deactivation complete handler*/
void phHal4Nfc_HandleP2PDeActivate(
                               phHal4Nfc_Hal4Ctxt_t  *Hal4Ctxt,
                               void *pInfo
                               )
{
    pphHal4Nfc_ReceiveCallback_t pUpperRecvCb = NULL;
    pphHal4Nfc_SendCallback_t pUpperSendCb = NULL;
    phHal4Nfc_NotificationInfo_t uNotificationInfo;
    uNotificationInfo.psEventInfo = (phHal_sEventInfo_t *)pInfo;
    /*session is closed*/
    if(NULL != Hal4Ctxt->rem_dev_list[0])
    {
        Hal4Ctxt->rem_dev_list[0]->SessionOpened = FALSE;
        Hal4Ctxt->psADDCtxtInfo->nbr_of_devices = 0;
    }
    Hal4Ctxt->sTgtConnectInfo.psConnectedDevice = NULL;
    /*Update state*/
    Hal4Ctxt->Hal4CurrentState = eHal4StateOpenAndReady;
    Hal4Ctxt->Hal4NextState  = eHal4StateInvalid;
    Hal4Ctxt->sTgtConnectInfo.EmulationState = NFC_EVT_DEACTIVATED;
    /*If Trcv ctxt info is allocated ,free it here*/
    if(NULL != Hal4Ctxt->psTrcvCtxtInfo)
    {
        if(PH_OSALNFC_INVALID_TIMER_ID != 
            Hal4Ctxt->psTrcvCtxtInfo->TransactionTimerId)
        {
            phOsalNfc_Timer_Stop(Hal4Ctxt->psTrcvCtxtInfo->TransactionTimerId);
            phOsalNfc_Timer_Delete(Hal4Ctxt->psTrcvCtxtInfo->TransactionTimerId);
        }
        pUpperRecvCb = Hal4Ctxt->psTrcvCtxtInfo->pP2PRecvCb;
        pUpperSendCb = Hal4Ctxt->psTrcvCtxtInfo->pP2PSendCb;
        /*Free Hal4 resources used by Target*/
        if (NULL != Hal4Ctxt->psTrcvCtxtInfo->sLowerRecvData.buffer)
        {
            phOsalNfc_FreeMemory(Hal4Ctxt->psTrcvCtxtInfo->
                sLowerRecvData.buffer);
        }
        if((NULL == Hal4Ctxt->sTgtConnectInfo.psConnectedDevice) 
            && (NULL != Hal4Ctxt->psTrcvCtxtInfo->psUpperSendData))
        {
            phOsalNfc_FreeMemory(Hal4Ctxt->psTrcvCtxtInfo->psUpperSendData);
        }  
        phOsalNfc_FreeMemory(Hal4Ctxt->psTrcvCtxtInfo);
        Hal4Ctxt->psTrcvCtxtInfo = NULL;
    }
    /*if recv callback is pending*/
    if(NULL != pUpperRecvCb)
    {
        (*pUpperRecvCb)(
                        Hal4Ctxt->sUpperLayerInfo.psUpperLayerCtxt,
                        NULL,
                        NFCSTATUS_DESELECTED
                        );
    }  
    /*if send callback is pending*/
    else if(NULL != pUpperSendCb)
    {
        (*pUpperSendCb)(
                        Hal4Ctxt->sUpperLayerInfo.psUpperLayerCtxt,
                        NFCSTATUS_DESELECTED
                        );
    }  
    /*if pP2PNotification is registered*/
    else if(NULL != Hal4Ctxt->sUpperLayerInfo.pP2PNotification)
    {
        (*Hal4Ctxt->sUpperLayerInfo.pP2PNotification)(
                                Hal4Ctxt->sUpperLayerInfo.P2PDiscoveryCtxt,
                                NFC_EVENT_NOTIFICATION,
                                uNotificationInfo,
                                NFCSTATUS_DESELECTED
                                );
    }
    else/*Call Default event handler*/
    {
        if(NULL != Hal4Ctxt->sUpperLayerInfo.pDefaultEventHandler)
        {
            Hal4Ctxt->sUpperLayerInfo.pDefaultEventHandler(
                Hal4Ctxt->sUpperLayerInfo.DefaultListenerCtxt,
                NFC_EVENT_NOTIFICATION,
                uNotificationInfo,
                NFCSTATUS_DESELECTED
                );
        }
    }
}