bluetoothcommsprofiles/btpan/bnep/CSocketReader.cpp
changeset 0 29b1cd4cb562
equal deleted inserted replaced
-1:000000000000 0:29b1cd4cb562
       
     1 // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 /**
       
    17  @file
       
    18  @internalComponent
       
    19 */
       
    20 
       
    21 #include <bluetooth/logger.h>
       
    22 #include <es_mbuf.h>
       
    23 #include "CBnepLink.h"
       
    24 #include "CSocketReader.h"
       
    25 
       
    26 #ifdef __FLOG_ACTIVE
       
    27 _LIT8(KLogComponent, LOG_COMPONENT_PAN_BNEP);
       
    28 #endif
       
    29 
       
    30 // Class CSocketReader 
       
    31 
       
    32 CSocketReader::CSocketReader (RInternalSocket& aSocket, CBnepLink& aOwner)
       
    33     : CActive(KSocketReaderPriority), iSocket(aSocket), iBnepLink(aOwner)
       
    34     {
       
    35     LOG_FUNC
       
    36     CActiveScheduler::Add(this); 
       
    37     }
       
    38 
       
    39 
       
    40 /**
       
    41 Cancels any pending reads.
       
    42 @internalComponent
       
    43 */
       
    44 CSocketReader::~CSocketReader()
       
    45     {
       
    46     LOG_FUNC
       
    47     iRecvMsg.Free();
       
    48     Cancel();
       
    49     }
       
    50 
       
    51 
       
    52 /**
       
    53 Does not create the socket reader's buffer, as this initialised 
       
    54 as a side effect of the RInternalSocket::Read(), 
       
    55 which has a hardcoded buffer size for Bluetooth - Maintainer BEWARE!!!!!.
       
    56 @internalComponent
       
    57 */
       
    58 void CSocketReader::ConstructL ()
       
    59     {
       
    60     LOG_FUNC
       
    61     }
       
    62 
       
    63 
       
    64 /**
       
    65 Cancel a read.
       
    66 @internalComponent
       
    67 */
       
    68 void CSocketReader::DoCancel ()
       
    69     {
       
    70     LOG_FUNC
       
    71     iSocket.CancelRead(); 
       
    72     }
       
    73 
       
    74     
       
    75 /**
       
    76 Create a socket reader.
       
    77 @internalComponent
       
    78 */
       
    79 CSocketReader* CSocketReader::NewL (RInternalSocket& aSocket, CBnepLink& aOwner)
       
    80     {
       
    81     LOG_STATIC_FUNC
       
    82     CSocketReader* self = new(ELeave) CSocketReader(aSocket, aOwner);
       
    83     CleanupStack::PushL(self);
       
    84     self->ConstructL();
       
    85     CleanupStack::Pop(self);
       
    86     return self; 
       
    87     }
       
    88 
       
    89 
       
    90 /**
       
    91 Queue a read.
       
    92 @internalComponent
       
    93 */
       
    94 void CSocketReader::Read ()
       
    95     {
       
    96     LOG_FUNC
       
    97     LOG1(_L8("Reader 0x%08x -- queueing read"), this);
       
    98     if (!IsActive())
       
    99         {
       
   100         // Ensure that the MBufChain that we are receiving into is available
       
   101         // by free-ing any data currently in it.
       
   102         iRecvMsg.Free();
       
   103         
       
   104         // As RMBufChain is essentially a pointer, the data returned must be consumed
       
   105         // later either by re-assigning or by free-ing it.  This is a bit unlike a
       
   106         // real RSocket, where the descriptor is simply overwritten.
       
   107         iSocket.Read(iRecvMsg, iStatus);
       
   108         SetActive(); 
       
   109         }
       
   110     else
       
   111         {
       
   112         LOG(_L8("Read queued but reader already active.  Discarding"));
       
   113         }
       
   114     }
       
   115 
       
   116 
       
   117 /**
       
   118 Read complete.
       
   119 Pass the received data up to the BNEP link, and signal disconnections.
       
   120 
       
   121 This method effectively triggers a long sequence of synchronous calls 
       
   122 into BNEP that culminate either in the completion of packet processing 
       
   123 or an asynchronous hiatus while waiting for the agent to respond to a 
       
   124 request.
       
   125 @internalComponent
       
   126 */
       
   127 void CSocketReader::RunL ()
       
   128     {
       
   129     LOG_FUNC
       
   130     
       
   131     // Save iStatus value to allow us to queue a new read prior to finishing processing
       
   132     // this one
       
   133     TInt err = iStatus.Int();
       
   134     LOG2(_L8("Length %d error %d"),iRecvMsg.Length(), err);
       
   135     
       
   136     if (iRecvMsg.Length())	// send any data up
       
   137         {
       
   138         // This is what kicks off the long processing 
       
   139         // sequence that may culminate in a write.
       
   140         // Note that this call may cause calls back down to CSocketReader
       
   141         iBnepLink.ReadComplete(iRecvMsg);
       
   142         LOG1(_L8("After ReadComplete Length %d"),iRecvMsg.Length());
       
   143         }
       
   144     
       
   145     if (err != KErrNone)	// if the connection has failed for some reason
       
   146         {
       
   147         LOG(_L8("Attempting a remote device disconnect"));
       
   148         
       
   149         // Cancel any new read queued
       
   150         Cancel();
       
   151         iBnepLink.RemoteDeviceDisconnect(err); // then notify the upper layer of the disconnect
       
   152         }
       
   153     }