usbmgmt/usbmgr/device/classdrivers/ncm/classimplementation/ncmpktdrv/pktdrv/src/ncmcomminterfacesenderandreceiver.cpp
branchRCL_3
changeset 16 012cc2ee6408
parent 15 f92a4f87e424
equal deleted inserted replaced
15:f92a4f87e424 16:012cc2ee6408
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 * implementation for read and write data from share chunk LDD 
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 /**
       
    21 @internalComponent
       
    22 */
       
    23 
       
    24 #include "ncmcommunicationinterface.h"
       
    25 #include "ncmengine.h"
       
    26 #include "OstTraceDefinitions.h"
       
    27 #ifdef OST_TRACE_COMPILER_IN_USE
       
    28 #include "ncmcomminterfacesenderandreceiverTraces.h"
       
    29 #endif
       
    30 
       
    31 
       
    32 
       
    33 // ======== MEMBER FUNCTIONS ========
       
    34 //
       
    35 
       
    36 CNcmCommInterfaceSenderAndReceiver::CNcmCommInterfaceSenderAndReceiver(RDevUsbcScClient& aPort, CNcmCommunicationInterface& aComm) 
       
    37     : CActive(CActive::EPriorityStandard),
       
    38     iPort(aPort),
       
    39     iCommInterface(aComm),
       
    40     iState(EUnInit)
       
    41     {
       
    42     CActiveScheduler::Add(this);
       
    43     }
       
    44 
       
    45 CNcmCommInterfaceSenderAndReceiver* CNcmCommInterfaceSenderAndReceiver::NewL(RDevUsbcScClient& aPort, CNcmCommunicationInterface& aComm)
       
    46     {
       
    47     return new(ELeave) CNcmCommInterfaceSenderAndReceiver(aPort, aComm);
       
    48     }
       
    49 
       
    50 CNcmCommInterfaceSenderAndReceiver::~CNcmCommInterfaceSenderAndReceiver()
       
    51     {
       
    52     Stop();
       
    53     }
       
    54 
       
    55 void CNcmCommInterfaceSenderAndReceiver::Start()
       
    56     {
       
    57     OstTraceFunctionEntry1( CNCMCOMMINTERFACESENDERANDRECEIVER_START_ENTRY, this );
       
    58     
       
    59     TInt ret = iPort.OpenEndpoint(iEp0Buffer, KEp0Number);
       
    60     if (ret != KErrNone)
       
    61         {
       
    62         OstTrace1( TRACE_FATAL, CNCMCOMMINTERFACESENDERANDRECEIVER_START1, "OpenEndpoint error %d", ret);
       
    63         iCommInterface.ControlMsgError(EInternalError);
       
    64         OstTraceFunctionExit1( CNCMCOMMINTERFACESENDERANDRECEIVER_START_EXIT, this );
       
    65         return;
       
    66         }
       
    67     iState = EIdle;
       
    68     OstTraceFunctionExit1( CNCMCOMMINTERFACESENDERANDRECEIVER_START_EXIT_DUP1, this );
       
    69     }
       
    70 
       
    71 void CNcmCommInterfaceSenderAndReceiver::Stop()
       
    72     {
       
    73     OstTraceFunctionEntry0( CNCMCOMMINTERFACESENDERANDRECEIVER_STOP_ENTRY );
       
    74     
       
    75     iState = EIdle;
       
    76     Cancel();    
       
    77     iEp0Buffer.Close();
       
    78     OstTraceFunctionExit0( CNCMCOMMINTERFACESENDERANDRECEIVER_STOP_EXIT );
       
    79     }
       
    80 
       
    81 
       
    82 //
       
    83 //Read data of aLength from LDD
       
    84 //
       
    85 
       
    86 TInt CNcmCommInterfaceSenderAndReceiver::Read(TRequestStatus& aStatus, TDes8& aBuf, TInt aLength)
       
    87     {
       
    88     OstTraceFunctionEntry1( CNCMCOMMINTERFACESENDERANDRECEIVER_READ_ENTRY, this );   
       
    89     OstTrace1( TRACE_NORMAL, CNCMCOMMINTERFACESENDERANDRECEIVER_READ, "CNcmCommInterfaceSenderAndReceiver::Read length %d", aLength );
       
    90         
       
    91     iCompleteStatus = &aStatus;
       
    92     *iCompleteStatus = KRequestPending;
       
    93     if (iState != EIdle)
       
    94         {
       
    95         OstTrace0( TRACE_FATAL, CNCMCOMMINTERFACESENDERANDRECEIVER_READ_DUP1, "CNcmCommInterfaceSenderAndReceiver Read In Use" );
       
    96         OstTraceFunctionExit1( CNCMCOMMINTERFACESENDERANDRECEIVER_READ_EXIT, this );
       
    97         return KErrInUse;
       
    98         }
       
    99     iReceiveBuf = &aBuf;
       
   100     iReceiveBuf->Zero();
       
   101     iToReceiveLength = aLength;
       
   102     iState = EReceiving;
       
   103     ReadData();
       
   104     OstTraceFunctionExit1( CNCMCOMMINTERFACESENDERANDRECEIVER_READ_EXIT_DUP1, this );
       
   105     return KErrNone;
       
   106     }
       
   107 
       
   108 //
       
   109 //write aLength data to ldd
       
   110 //
       
   111 TInt CNcmCommInterfaceSenderAndReceiver::Write(TRequestStatus& aStatus, TDesC8& aBuf, TInt aLength)
       
   112     {
       
   113     OstTraceFunctionEntry1( CNCMCOMMINTERFACESENDERANDRECEIVER_WRITE_ENTRY, this );
       
   114     OstTrace1( TRACE_NORMAL, CNCMCOMMINTERFACESENDERANDRECEIVER_WRITE, "CNcmCommInterfaceSenderAndReceiver::Write length %d", aLength );
       
   115 
       
   116     iCompleteStatus = &aStatus;
       
   117     *iCompleteStatus = KRequestPending;
       
   118 
       
   119     if (iState != EIdle)
       
   120         {
       
   121         OstTrace0( TRACE_FATAL, CNCMCOMMINTERFACESENDERANDRECEIVER_WRITE1, "CNcmCommInterfaceSenderAndReceiver::Write in Use" );        
       
   122         OstTraceFunctionExit1( CNCMCOMMINTERFACESENDERANDRECEIVER_WRITE_EXIT, this );
       
   123         return KErrInUse;
       
   124         }
       
   125 
       
   126     TAny *buf;
       
   127     TUint size;
       
   128     TInt ret = iEp0Buffer.GetInBufferRange(buf, size);
       
   129     if (ret != KErrNone)
       
   130         {
       
   131         OstTrace0( TRACE_FATAL, CNCMCOMMINTERFACESENDERANDRECEIVER_WRITE2, "CNcmCommInterfaceSenderAndReceiver::Write in Use" );        
       
   132         OstTraceFunctionExit1( CNCMCOMMINTERFACESENDERANDRECEIVER_WRITE_EXIT_DUP1, this );
       
   133         return ret;
       
   134         }
       
   135     else if (size < aLength)
       
   136         {
       
   137         OstTrace0( TRACE_FATAL, CNCMCOMMINTERFACESENDERANDRECEIVER_WRITE3, "write data is bigger than ldd buffer size" );        
       
   138         OstTraceFunctionExit1( CNCMCOMMINTERFACESENDERANDRECEIVER_WRITE_EXIT_DUP2, this );
       
   139         return KErrArgument;
       
   140         }
       
   141 
       
   142     TPtr8 writeBuf((TUint8 *)buf, size);
       
   143     writeBuf.Copy(aBuf.Ptr(), aLength);    
       
   144     ret = iEp0Buffer.WriteBuffer(buf, writeBuf.Size(), ETrue, iStatus);
       
   145     if (ret != KErrNone)
       
   146         {
       
   147         OstTrace1( TRACE_FATAL, CNCMCOMMINTERFACESENDERANDRECEIVER_WRITE4, "WriteBuffer error %d", ret );
       
   148         OstTraceFunctionExit1( CNCMCOMMINTERFACESENDERANDRECEIVER_WRITE_EXIT_DUP3, this );
       
   149         return ret;
       
   150         }
       
   151     iState = ESending;
       
   152     SetActive();
       
   153     OstTraceFunctionExit1( CNCMCOMMINTERFACESENDERANDRECEIVER_WRITE_EXIT_DUP4, this );
       
   154     return KErrNone;
       
   155     }
       
   156 
       
   157 
       
   158 
       
   159 //
       
   160 //Read data from USB share chunk LDD
       
   161 //
       
   162 void CNcmCommInterfaceSenderAndReceiver::ReadData()
       
   163     {
       
   164     OstTraceFunctionEntry1( CNCMCOMMINTERFACESENDERANDRECEIVER_READDATA_ENTRY, this );
       
   165     TInt ret;
       
   166     TAny* buf;
       
   167     TUint8* receivebuf; 
       
   168     TUint receiveLen;
       
   169     TBool zlp;
       
   170 
       
   171     FOREVER
       
   172         {
       
   173         ret = iEp0Buffer.GetBuffer(buf, receiveLen, zlp, iStatus, iToReceiveLength);
       
   174         receivebuf = (TUint8*)buf; 
       
   175         if (ret == TEndpointBuffer::KStateChange)
       
   176             {
       
   177             OstTrace0( TRACE_NORMAL, CNCMCOMMINTERFACESENDERANDRECEIVER_READDATA, "receive state change, discard it" );
       
   178             continue;
       
   179             }
       
   180 
       
   181         else if (ret == KErrCompletion)
       
   182             {
       
   183             OstTrace1( TRACE_NORMAL, CNCMCOMMINTERFACESENDERANDRECEIVER_READDATA1, "read data length %d from ldd", receiveLen );
       
   184             if (receiveLen > iToReceiveLength)
       
   185                 {
       
   186                 User::RequestComplete(iCompleteStatus, KErrOverflow);
       
   187                 iState = EIdle;
       
   188                 OstTrace0( TRACE_NORMAL, CNCMCOMMINTERFACESENDERANDRECEIVER_READDATA2,"data from ldd is bigger than receive buffer" );
       
   189                 OstTraceFunctionExit1( CNCMCOMMINTERFACESENDERANDRECEIVER_READDATA_EXIT, this );
       
   190                 return;
       
   191                 }
       
   192             
       
   193             iReceiveBuf->Append(receivebuf, receiveLen);
       
   194             iToReceiveLength -= receiveLen;
       
   195             if (!iToReceiveLength)
       
   196                 {        
       
   197                 User::RequestComplete(iCompleteStatus, KErrNone);
       
   198                 iState = EIdle;
       
   199                 OstTrace0( TRACE_NORMAL, CNCMCOMMINTERFACESENDERANDRECEIVER_READDATA3,"Complete read request" );
       
   200                 OstTraceFunctionExit1( CNCMCOMMINTERFACESENDERANDRECEIVER_READDATA_EXIT_DUP1, this );
       
   201                 return;
       
   202                 }
       
   203             continue;
       
   204             }
       
   205         else if (ret == KErrNone)
       
   206             {
       
   207             break;
       
   208             }
       
   209         else
       
   210             {
       
   211             OstTrace1( TRACE_NORMAL, CNCMCOMMINTERFACESENDERANDRECEIVER_READDATA4, "GetBuffer error %d", ret);
       
   212             User::RequestComplete(iCompleteStatus, KErrNone);
       
   213             iState = EIdle;
       
   214             OstTraceFunctionExit1( CNCMCOMMINTERFACESENDERANDRECEIVER_READDATA_EXIT_DUP2, this );
       
   215             return;
       
   216             }
       
   217         }
       
   218     SetActive();
       
   219     OstTraceFunctionExit1( CNCMCOMMINTERFACESENDERANDRECEIVER_READDATA_EXIT_DUP3, this );
       
   220     }
       
   221 
       
   222 //
       
   223 //RunL, looply receive data from share chunk LDD until receive all required data
       
   224 //
       
   225 void CNcmCommInterfaceSenderAndReceiver::RunL()
       
   226     {
       
   227     OstTraceFunctionEntry1( CNCMCOMMINTERFACESENDERANDRECEIVER_RUNL_ENTRY, this );
       
   228     if(iStatus.Int() != KErrNone)
       
   229         {
       
   230         if (KErrCancel == iStatus.Int() )
       
   231             {
       
   232             OstTrace0( TRACE_NORMAL, CNCMCOMMINTERFACESENDERANDRECEIVER_RUNL,"CNcmCommInterfaceSenderAndReceiver, control channel is cancelled" );
       
   233             }
       
   234         else
       
   235             {
       
   236             OstTrace1( TRACE_NORMAL, CNCMCOMMINTERFACESENDERANDRECEIVER_RUNL1, "CNcmCommInterfaceSenderAndReceiver::RunL error %d", iStatus.Int());
       
   237             }
       
   238         User::RequestComplete(iCompleteStatus, iStatus.Int());
       
   239         }
       
   240     else
       
   241         {
       
   242         if (iState == EReceiving)
       
   243             {
       
   244             ReadData();
       
   245             }
       
   246         else if (iState == ESending)
       
   247             {
       
   248             User::RequestComplete(iCompleteStatus, KErrNone);
       
   249             iState = EIdle;
       
   250             OstTrace0( TRACE_NORMAL, CNCMCOMMINTERFACESENDERANDRECEIVER_RUNL2,"Complete write request" );
       
   251             }
       
   252         }
       
   253     OstTraceFunctionExit1( CNCMCOMMINTERFACESENDERANDRECEIVER_RUNL_EXIT, this );
       
   254     }
       
   255 
       
   256 //
       
   257 //Cancel the outgoing read request
       
   258 //
       
   259 void CNcmCommInterfaceSenderAndReceiver::DoCancel()
       
   260     {    
       
   261     OstTraceFunctionEntry1( CNCMCOMMINTERFACESENDERANDRECEIVER_DOCANCEL_ENTRY, this );
       
   262     iPort.ReadCancel(KUsbcScEndpointZero);
       
   263     iPort.WriteCancel(KUsbcScEndpointZero);
       
   264     OstTraceFunctionExit1( CNCMCOMMINTERFACESENDERANDRECEIVER_DOCANCEL_EXIT, this );
       
   265     }