summaryrefslogtreecommitdiffstats
path: root/src/connectivity/nfc/symbian/llcpsockettype1_symbian.cpp
blob: 3f73cbd990d2ad86ced662670de4b093ea320240 (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
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the Qt Mobility Components.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <llcpprovider.h>                   // CLlcpProvider
#include <llcpconnlesstransporter.h>        // MLlcpConnLessTransporter

#include "nearfieldutility_symbian.h"
#include "llcpsockettype1_symbian.h"
#include "debug.h"


/*
    CLlcpSocketType1::NewL()
*/
CLlcpSocketType1* CLlcpSocketType1::NewL(QtMobility::QLlcpSocketPrivate& aCallback)
    {
    CLlcpSocketType1* self = CLlcpSocketType1::NewLC(aCallback);
    CleanupStack::Pop(self);
    return self;
    }

/*
    CLlcpSocketType1::NewLC()
*/
CLlcpSocketType1* CLlcpSocketType1::NewLC(QtMobility::QLlcpSocketPrivate& aCallback)
    {
    CLlcpSocketType1* self = new (ELeave) CLlcpSocketType1(aCallback);
    CleanupStack::PushL(self);
    self->ConstructL();
    return self;
    }

/*
    CLlcpSocketType1::CLlcpSocketType1()
*/
CLlcpSocketType1::CLlcpSocketType1(QtMobility::QLlcpSocketPrivate& aCallback)
    : iWaitStatus(ENone),
      iPortBinded(EFalse),
      iCallback(aCallback)
    {
    }

/*
    CLlcpSocketPrivate::ContructL()
*/
void CLlcpSocketType1::ConstructL()
    {
    User::LeaveIfError(iNfcServer.Open());
    iLlcp = CLlcpProvider::NewL(iNfcServer);
    iWait = new (ELeave) CActiveSchedulerWait;
    }

/*
    Destroys the LLCP socket.
*/
CLlcpSocketType1::~CLlcpSocketType1()
    {
    BEGIN
    // Destroy connection
    Cleanup();

    if (iLlcp)
        {
        iLlcp->StopListeningConnLessRequest(iLocalPort);
        delete iLlcp;
        }
    iNfcServer.Close();
    delete iTimer;
    delete iWait;

    END
    }

/*
    Cancel the Receive/Transfer and destroy the local/remote connection.
*/
void CLlcpSocketType1::Cleanup()
    {
    BEGIN
    // Deleting connection
    if (iConnectionWrapper)
        {
        iConnectionWrapper->TransferCancel();
        iConnectionWrapper->ReceiveCancel();
        delete iConnectionWrapper;
        iConnectionWrapper = NULL;
        }
    END
    }

/*
    Start to listen the port as given, set as local port which is used to read datagram
*/
TBool CLlcpSocketType1::Bind(TUint8 aPortNum)
    {
    BEGIN
    TBool bindOK = EFalse;
    if (!iPortBinded)
        {
        TInt error = KErrNone;
        TRAP(error, iLlcp->StartListeningConnLessRequestL(*this,aPortNum));
        if (KErrNone == error)
            {
            iPortBinded = ETrue;
            iLocalPort = aPortNum;
            bindOK = ETrue;
            }
        }
    END
    return bindOK;
    }

/*
    Sends the datagram at aData  to the service that this socket is connected to.
    Returns the number of bytes sent on success; otherwise return -1;
*/
TInt CLlcpSocketType1::StartWriteDatagram(const TDesC8& aData,TUint8 aPortNum)
    {
    BEGIN
    TInt val = -1;

    if (iConnectionWrapper != NULL && iRemotePort != aPortNum)
        {
        return val;
        }

    if (KErrNone == CreateConnection(aPortNum))
        {
        TInt error = KErrNone;
        QT_TRYCATCH_ERROR(error , iConnectionWrapper->TransferL(aData));

        if (KErrNone == error)
            {
            iCallback.m_writeDatagramRefCount++;
            val =  0;
            }
        }
    END
    return val;
    }


TInt CLlcpSocketType1::ReadDatagram(TDes8& aData, TUint8& aRemotePortNum)
    {
    BEGIN
    aRemotePortNum = iRemotePort;
    TInt val = ReadDatagram(aData);
    END
    return val;
    }

TInt CLlcpSocketType1::ReadDatagram(TDes8& aData)
    {
    BEGIN
    TInt readSize = -1;
    if (NULL != iConnectionWrapper)
        {
        readSize = iConnectionWrapper->ReceiveDataFromBuf(aData);

        // Start receiving data again
        TInt error = KErrNone;
        error = iConnectionWrapper->Receive();
        if (KErrNone != error)
            {
            readSize =  -1;
            }
        }
    END
    return readSize;
    }

TBool CLlcpSocketType1::HasPendingDatagrams() const
    {
    BEGIN
    TBool val = EFalse;
    if (NULL != iConnectionWrapper)
        {
        val = iConnectionWrapper->HasPendingDatagrams();
        }
    END
    return val;
    }

TInt64 CLlcpSocketType1::PendingDatagramSize() const
    {
    BEGIN
    TInt64 val = -1;
    if (NULL != iConnectionWrapper)
        {
        val = iConnectionWrapper->PendingDatagramSize();
        }
    END
    return val;
    }

/*
    Call back from MLlcpConnLessListener
*/
void CLlcpSocketType1::FrameReceived(MLlcpConnLessTransporter* aConnection)
    {
    BEGIN
    iRemotePort = aConnection->SsapL();
//    StartTransportAndReceive(aConnection);
    // Only accepting one incoming remote connection
    TInt error = KErrNone;
    if (iConnectionWrapper)
        {
        delete iConnectionWrapper;
        iConnectionWrapper = NULL;
        }
    // Creating wrapper for connection.
    TRAP(error, iConnectionWrapper = COwnLlcpConnectionWrapper::NewL(aConnection, *this));

    if (error == KErrNone && iConnectionWrapper != NULL)
        {
        error = iConnectionWrapper->Receive();
        }
    if (error != KErrNone)
        {
        QT_TRYCATCH_ERROR(error,iCallback.invokeError());
        }
    END
    }

/*
    Call back from MLlcpReadWriteCb
*/
void CLlcpSocketType1::ReceiveComplete(TInt aError)
    {
    BEGIN
    TInt err = KErrNone;
    if (KErrNone == aError)
        {
        QT_TRYCATCH_ERROR(err,iCallback.invokeReadyRead());
        }
    else
        {
        LOG("err = "<<err);
        QT_TRYCATCH_ERROR(err,iCallback.invokeError());
        }
    Q_UNUSED(err);
    END
    }

/*
    Call back from MLlcpReadWriteCb
*/
void CLlcpSocketType1::WriteComplete(TInt aError, TInt aSize)
    {
    BEGIN
    if (iWaitStatus == EWaitForBytesWritten)
        {
        StopWaitNow(EWaitForBytesWritten);
        }

    TInt err = KErrNone;
    if (KErrNone == aError)
        {
        iCallback.m_writeDatagramRefCount--;
        QT_TRYCATCH_ERROR(err,iCallback.invokeBytesWritten(aSize));
        }
    else
        {
        QT_TRYCATCH_ERROR(err,iCallback.invokeError());
        }

    if (err == aError && iConnectionWrapper != NULL
        && iConnectionWrapper->HasQueuedWrittenDatagram())
        {
        iConnectionWrapper->TransferQueued();
        }
    END
    }

void CLlcpSocketType1::StopWaitNow(TWaitStatus aWaitStatus)
    {
    BEGIN
    if (iWaitStatus == aWaitStatus)
        {
        if (iWait->IsStarted())
            {
            iWait->AsyncStop();
            }
        if (iTimer)//stop the timer
            {
            delete iTimer;
            iTimer = NULL;
            }
        }
    END
    }

/*
    Creating MLlcpConnLessTransporter object if  connection type connectionless,
    Creating Creating wrapper for local peer connection.
*/
TInt CLlcpSocketType1::CreateConnection(TUint8 portNum)
    {
    BEGIN
    TInt error = KErrNone;
    MLlcpConnLessTransporter* llcpConnection = NULL;

    if (iConnectionWrapper)
        {
        return error;
        }

    TRAP(error, llcpConnection = iLlcp->CreateConnLessTransporterL(portNum));

    if (error == KErrNone)
        {
          iRemotePort = portNum;
          error = StartTransportAndReceive(llcpConnection);
        }
    END
    return error;
    }

TInt CLlcpSocketType1::StartTransportAndReceive(MLlcpConnLessTransporter* aConnection)
    {
    BEGIN
    TInt error = KErrNone;

     // Only accepting one incoming remote connection
     if (!iConnectionWrapper)
         {
         // Creating wrapper for connection.
         TRAP(error, iConnectionWrapper = COwnLlcpConnectionWrapper::NewL(aConnection, *this));
         }

     if (error == KErrNone && iConnectionWrapper != NULL)
         {
         error = iConnectionWrapper->Receive();
         }
     END
     return error;
    }


TBool CLlcpSocketType1::WaitForBytesWritten(TInt aMilliSeconds)
    {
    BEGIN_END
    return WaitForOperationReady(EWaitForBytesWritten, aMilliSeconds);
    }

TBool CLlcpSocketType1::WaitForOperationReady(TWaitStatus aWaitStatus,TInt aMilliSeconds)
    {
    BEGIN
    TBool ret = EFalse;
    if (iWaitStatus != ENone || iWait->IsStarted())
        {
        return ret;
        }
    iWaitStatus = aWaitStatus;

    if (iTimer)
        {
        delete iTimer;
        iTimer = NULL;
        }
    if (aMilliSeconds > 0)
        {
        TRAPD(err, iTimer = CLlcpTimer::NewL(*iWait));
        if (err != KErrNone)
            {
            return ret;
            }
        iTimer->Start(aMilliSeconds);
        }
    iWait->Start();

    //control is back here when iWait->AsyncStop() is called by the timer or the callback function
    iWaitStatus = ENone;

    if (!iTimer)
        {
        //iTimer == NULL means this CActiveSchedulerWait
        //AsyncStop is fired by the call back of ReadyRead
        ret = ETrue;
        }
    else
        {
        delete iTimer;
        iTimer = NULL;
        }

    END
    return ret;
    }


/*
    Construct the wrapper for connectionLess transport.
*/
COwnLlcpConnectionWrapper* COwnLlcpConnectionWrapper::NewL(MLlcpConnLessTransporter* aConnection
                                                           , MLlcpReadWriteCb& aCallBack)
    {
    COwnLlcpConnectionWrapper* self = COwnLlcpConnectionWrapper::NewLC(aConnection, aCallBack);
    CleanupStack::Pop(self);
    return self;
    }

/*
    Construct the new wrapper for connectionLess transport.
*/
COwnLlcpConnectionWrapper* COwnLlcpConnectionWrapper::NewLC(MLlcpConnLessTransporter* aConnection
                                                            , MLlcpReadWriteCb& aCallBack)
    {
    COwnLlcpConnectionWrapper* self = new (ELeave) COwnLlcpConnectionWrapper(aConnection);
    CleanupStack::PushL(self);
    self->ConstructL(aCallBack);
    return self;
    }

/*
    Constructor
*/
COwnLlcpConnectionWrapper::COwnLlcpConnectionWrapper(MLlcpConnLessTransporter* aConnection)
    :  iConnection(aConnection)
    {
    }

/*
    ConstructL
*/
void COwnLlcpConnectionWrapper::ConstructL(MLlcpReadWriteCb& aCallBack)
    {
    if (NULL == iConnection)
        {
        User::Leave(KErrArgument);
        }
    // Create the transmitter AO
    iSenderAO = CLlcpSenderType1::NewL(*iConnection, aCallBack);
     // Create the receiver AO
    iReceiverAO = CLlcpReceiverType1::NewL(*iConnection, aCallBack);
    }

/*
    Destroy the new wrapper for connectionLess transport.
*/
COwnLlcpConnectionWrapper::~COwnLlcpConnectionWrapper()
    {
    BEGIN
    iSendBufArray.ResetAndDestroy();
    iSendBufArray.Close();

    delete iSenderAO;
    delete iReceiverAO;

    if (iConnection)
        {
        delete iConnection;
        iConnection = NULL;
        }
    END
    }

/*
    Send data from queued buffer
*/
bool COwnLlcpConnectionWrapper::TransferQueued()
    {
    BEGIN
    bool ret = false;
    if (iSendBufArray.Count() == 0 || iSenderAO->IsActive())
        return ret;

    HBufC8* bufRef = iSendBufArray[0];
    if (NULL == bufRef)
        return ret;

    TPtrC8 ptr(bufRef->Ptr(), bufRef->Length());
    if(!iSenderAO->IsActive() && iSenderAO->Transfer(ptr) == KErrNone)
        {
        ret = true;
        }
    iSendBufArray.Remove(0);
    delete bufRef;
    bufRef = NULL;

    END
    return ret;
    }

/*
    Send data from local peer to remote peer via connectionLess transport
*/
TInt COwnLlcpConnectionWrapper::TransferL(const TDesC8& aData)
    {
    BEGIN
    TInt error = KErrNone;
    // Pass message into transmitter AO
    if (!iSenderAO->IsActive())
        {
        error = iSenderAO->Transfer(aData);
        }
    else if (aData.Length() > 0)
        {
        HBufC8* buf = HBufC8::NewLC(aData.Length());
        buf->Des().Copy(aData);
        error = iSendBufArray.Append(buf);
        CleanupStack::Pop(buf);
        }
    END
    return error;
    }


/*
 *  Trigger the receiver AO to start receive datagram
    Receive data from remote peer to local peer via connectionLess transport
*/
TInt COwnLlcpConnectionWrapper::Receive()
    {
    BEGIN
    TInt error = KErrInUse;
    // Pass message on to transmit AO
    if (!iReceiverAO->IsActive())
        {
        error = iReceiverAO->Receive();
        }

    END
    return error;
    }

/*
    Retrieve data from the buffer of the connection less socket
*/
TInt COwnLlcpConnectionWrapper::ReceiveDataFromBuf(TDes8& aData)
    {
    return iReceiverAO->ReceiveDataFromBuf(aData);
    }

bool COwnLlcpConnectionWrapper::HasPendingDatagrams() const
    {
    return iReceiverAO->HasPendingDatagrams();
    }

bool COwnLlcpConnectionWrapper::HasQueuedWrittenDatagram() const
    {
    bool hasData = iSendBufArray.Count() > 0 ? true : false;
    return hasData;
    }

TInt64 COwnLlcpConnectionWrapper::PendingDatagramSize() const
    {
    return iReceiverAO->PendingDatagramSize();
    }

/*
    Cancel data transfer from local peer to remote peer via connectionLess transport
*/
void COwnLlcpConnectionWrapper::TransferCancel()
    {
    BEGIN
    if (iSenderAO->IsActive())
        {
        iSenderAO->Cancel();
        }
    END
    }

/*
    Cancel data receive from local peer to remote peer via connectionLess transport
*/
void COwnLlcpConnectionWrapper::ReceiveCancel()
    {
    BEGIN
    if (iReceiverAO->IsActive())
        {
        iReceiverAO->Cancel();
        }
    END
    }

/*
    Start of implementation of Sender AO & Receiver AO for connection less mode (type1)
*/

/*
    Constructor
*/
CLlcpSenderType1::CLlcpSenderType1(MLlcpConnLessTransporter& aConnection, MLlcpReadWriteCb& aCallBack)
                       : CActive(CActive::EPriorityStandard)
                       , iConnection(aConnection)
                       , iSendObserver(aCallBack)
    {
    }

/*
    Constructor
*/
CLlcpSenderType1* CLlcpSenderType1::NewL(MLlcpConnLessTransporter& iConnection, MLlcpReadWriteCb& aCallBack)
    {
    CLlcpSenderType1* self = new(ELeave) CLlcpSenderType1(iConnection, aCallBack);
    CActiveScheduler::Add(self);
    return self;
    }

/*
    Destructor
*/
CLlcpSenderType1::~CLlcpSenderType1()
    {
    BEGIN
    Cancel();   // Cancel ANY outstanding request at time of destruction
    iTransmitBuf.Close();
    iTempSendBuf.Close();
    END
    }

TInt CLlcpSenderType1::Transfer(const TDesC8& aData)
    {
    BEGIN

    if (aData.Length() == 0)
        {
        return KErrArgument;
        }

    TInt supportedDataLength = iConnection.SupportedDataLength();
    if (supportedDataLength <= 0)
        {
        return KErrNotReady;
        }
    // Reset pos to start
    iCurrentSendBufPos = 0;
    TInt error = KErrNone;
    // Copying data to internal buffer.
    iTransmitBuf.Zero();
    error = iTransmitBuf.ReAlloc(aData.Length());

    if (error == KErrNone)
        {
        iTransmitBuf.Append(aData);

        if (iTransmitBuf.Length() > supportedDataLength)
            {
            iCurrentSendBufPtr.Set(iTransmitBuf.Ptr(), supportedDataLength);
            }
        else
            {
            iCurrentSendBufPtr.Set(iTransmitBuf.Ptr(), iTransmitBuf.Length());
            }
		iCurrentSendBufPos = iCurrentSendBufPtr.Length();
        // Sending data, don't need check active, external func has checked it
        iTempSendBuf.Close();
        iTempSendBuf.Create(iCurrentSendBufPtr);
        iConnection.Transmit(iStatus, iTempSendBuf);
        SetActive();
        }
    else
        {
        error = KErrNoMemory;
        }
    END
    return error;
    }

void CLlcpSenderType1::RunL(void)
    {
    BEGIN
    TInt error = iStatus.Int();
    // Sending error, notify user
    if (KErrNone != error)
        {
        // Return buffer's length which has been sent successfully.
        iSendObserver.WriteComplete(error, iCurrentSendBufPos - iCurrentSendBufPtr.Length());
        return;
        }

    TInt bytesWritten =  iCurrentSendBufPtr.Length();

    // Still have some buffer need send, don't stop
    if (iCurrentSendBufPos < iTransmitBuf.Length())
        {
        TInt supportedDataLength = iConnection.SupportedDataLength();
        if (supportedDataLength <= 0)
            {
            iSendObserver.WriteComplete(KErrGeneral, iCurrentSendBufPtr.Length());
            return;
            }

        if (iTransmitBuf.Length() > iCurrentSendBufPos + supportedDataLength)
            {
            // Still left some buffer
            iCurrentSendBufPtr.Set(iTransmitBuf.Ptr() + iCurrentSendBufPos, supportedDataLength);
            iCurrentSendBufPos += supportedDataLength;
            }
        else
            {
            // All buffer will be sent in this time
            iCurrentSendBufPtr.Set(iTransmitBuf.Ptr() + iCurrentSendBufPos
                                 , iTransmitBuf.Length() - iCurrentSendBufPos);
            iCurrentSendBufPos = iTransmitBuf.Length();
            }
        // Sending data
        iTempSendBuf.Close();
        iTempSendBuf.Create(iCurrentSendBufPtr);
        iConnection.Transmit(iStatus, iTempSendBuf);
        SetActive();
        }
    // Sent signal for each successful sending
    iSendObserver.WriteComplete(error, bytesWritten);
    END
    }

void CLlcpSenderType1::DoCancel(void)
    {
    BEGIN
    // Cancel any outstanding write request on iSocket at this time.
    iConnection.TransmitCancel();
    END
    }

/*
    Start of implementation of Receiver AO for connection less mode (type1) - CLlcpReceiverType1
*/

/*
    Constructor
*/
CLlcpReceiverType1::CLlcpReceiverType1(MLlcpConnLessTransporter& aConnection, MLlcpReadWriteCb& aCallBack)
                       : CActive(CActive::EPriorityStandard)
                       , iConnection(aConnection)
                       , iReceiveObserver(aCallBack)
    {
    }

/*
    Constructor
*/
CLlcpReceiverType1* CLlcpReceiverType1::NewL(MLlcpConnLessTransporter& aConnection, MLlcpReadWriteCb& aCallBack)
    {
    CLlcpReceiverType1* self = new (ELeave) CLlcpReceiverType1(aConnection, aCallBack);
    CActiveScheduler::Add(self);
    return self;
    }


/*
    Set active for the receiver AO
    Receive complete callback function "cb" registered
*/
TInt CLlcpReceiverType1::Receive()
    {
    BEGIN
    TInt error = KErrNotReady;
    TInt length = 0;
    length = iConnection.SupportedDataLength();
    iReceiveBuf.Zero();
    if (length > 0)
        error = iReceiveBuf.ReAlloc(length);

    if (error == KErrNone)
        {
        iConnection.Receive(iStatus, iReceiveBuf);
        SetActive();
        }

    END
    return error;
    }

void CLlcpReceiverType1::RunL(void)
    {
    BEGIN
    TInt error = iStatus.Int();
     // Call back functions of notifying the llcp receiver completed.
    iReceiveObserver.ReceiveComplete(error);
    END
    }

CLlcpReceiverType1::~CLlcpReceiverType1()
    {
    BEGIN
    // cancel ANY outstanding request at time of destruction
    Cancel();
    iReceiveBuf.Close();
    END
    }

TInt CLlcpReceiverType1::ReceiveDataFromBuf(TDes8& aData)
    {
    BEGIN
    if (iReceiveBuf.Size() == 0)
        return 0;

    TInt requiredLength =  aData.MaxLength() - aData.Length();
    TInt bufLength =  iReceiveBuf.Length();
    TInt readLength = requiredLength < bufLength ? requiredLength : bufLength;

    TPtrC8 ptr(iReceiveBuf.Ptr(),readLength);
    aData.Append(ptr);

    //Empty the buffer as long as receive data request issued.
    iReceiveBuf.Zero();
    END
    return readLength;
    }

bool CLlcpReceiverType1::HasPendingDatagrams() const
    {
    return iReceiveBuf.Size() > 0 ? true : false;
    }

TInt64 CLlcpReceiverType1::PendingDatagramSize() const
    {
    return iReceiveBuf.Size();
    }

void CLlcpReceiverType1::DoCancel(void)
    {
    BEGIN
    // Cancel any outstanding write request on iSocket at this time.
    iConnection.ReceiveCancel();
    END
    }