localconnectivityservice/dun/plugins/src/usb/DunUsbListen.cpp
branchRCL_3
changeset 20 4a793f564d72
parent 19 0aa8cc770c8a
child 21 74aa6861c87d
equal deleted inserted replaced
19:0aa8cc770c8a 20:4a793f564d72
     1 /*
       
     2 * Copyright (c) 2006-2008 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:  DUN USB plugin's listener
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <e32std.h>
       
    20 #include "DunUtils.h"
       
    21 #include "DunUsbListen.h"
       
    22 #include "DunDebug.h"
       
    23 
       
    24 const TUint KDunUsbDeviceStateMask = 0x00ff;
       
    25 
       
    26 // ======== MEMBER FUNCTIONS ========
       
    27 
       
    28 // ---------------------------------------------------------------------------
       
    29 // Two-phased constructor.
       
    30 // ---------------------------------------------------------------------------
       
    31 //
       
    32 CDunUsbListen* CDunUsbListen::NewL( MDunServerCallback* aServer,
       
    33                                     MDunListenCallback* aParent,
       
    34                                     RUsb& aUsbServer )
       
    35     {
       
    36     CDunUsbListen* self = new (ELeave) CDunUsbListen( aServer,
       
    37                                                       aParent,
       
    38                                                       aUsbServer );
       
    39     CleanupStack::PushL( self );
       
    40     self->ConstructL();
       
    41     CleanupStack::Pop( self );
       
    42     return self;
       
    43     }
       
    44 
       
    45 // ---------------------------------------------------------------------------
       
    46 // Destructor.
       
    47 // ---------------------------------------------------------------------------
       
    48 //
       
    49 CDunUsbListen::~CDunUsbListen()
       
    50     {
       
    51     FTRACE(FPrint( _L( "CDunUsbListen::~CDunUsbListen()" ) ));
       
    52     ResetData();
       
    53     FTRACE(FPrint( _L( "CDunUsbListen::~CDunUsbListen() complete" ) ));
       
    54     }
       
    55 
       
    56 // ---------------------------------------------------------------------------
       
    57 // Resets data to initial values
       
    58 // ---------------------------------------------------------------------------
       
    59 //
       
    60 void CDunUsbListen::ResetData()
       
    61     {
       
    62     // APIs affecting this:
       
    63     // IssueRequestL()
       
    64     Stop();
       
    65     // Internal
       
    66     Initialize();
       
    67     }
       
    68 
       
    69 // ---------------------------------------------------------------------------
       
    70 // IssueRequest to USB server for device state change notifications
       
    71 // ---------------------------------------------------------------------------
       
    72 //
       
    73 TInt CDunUsbListen::IssueRequestL()
       
    74     {
       
    75     FTRACE(FPrint( _L( "CDunUsbListen::IssueRequestL()" )));
       
    76 
       
    77     if ( iUsbServer.Handle() == KNullHandle )
       
    78         {
       
    79         FTRACE(FPrint( _L( "CDunUsbListen::IssueRequestL() (iUsbServer) complete" ) ));
       
    80         User::Leave( KErrGeneral );
       
    81         }
       
    82 
       
    83     TUsbDeviceState usbDeviceState;
       
    84     User::LeaveIfError( iUsbServer.GetDeviceState(usbDeviceState) );
       
    85     FTRACE(FPrint( _L( "CDunUsbListen::IssueRequestL() Usb device state = %X" ), usbDeviceState));
       
    86     if ( iDeviceState == EUsbDeviceStateUndefined )
       
    87         {
       
    88         iDeviceState = usbDeviceState;
       
    89         }
       
    90 
       
    91     // USB device state now set; if configured already, notify parent
       
    92     // If not yet configured, start listening
       
    93 
       
    94     if ( usbDeviceState == EUsbDeviceStateConfigured )
       
    95         {
       
    96         FTRACE(FPrint( _L( "CDunUsbListen::IssueRequestL() (already exists) complete" ) ));
       
    97         return KErrAlreadyExists;
       
    98         }
       
    99 
       
   100     Activate();
       
   101 
       
   102     FTRACE(FPrint( _L( "CDunUsbListen::IssueRequestL() complete" ) ));
       
   103     return KErrNone;
       
   104     }
       
   105 
       
   106 // ---------------------------------------------------------------------------
       
   107 // Stops listening
       
   108 // ---------------------------------------------------------------------------
       
   109 //
       
   110 TInt CDunUsbListen::Stop()
       
   111     {
       
   112     FTRACE(FPrint( _L( "CDunUsbListen::Stop()" ) ));
       
   113     if ( iListenState != EUsbListenStateListening )
       
   114         {
       
   115         FTRACE(FPrint( _L( "CDunUsbListen::Stop() (not ready) complete" ) ));
       
   116         return KErrNotReady;
       
   117         }
       
   118     iUsbServer.DeviceStateNotificationCancel();
       
   119     Cancel();
       
   120     iListenState = EUsbListenStateIdle;
       
   121     iDeviceState = EUsbDeviceStateUndefined;
       
   122     iDeviceStatePrev = EUsbDeviceStateUndefined;
       
   123     FTRACE(FPrint( _L( "CDunUsbListen::Stop() complete" ) ));
       
   124     return KErrNone;
       
   125     }
       
   126 
       
   127 // ---------------------------------------------------------------------------
       
   128 // CDunUsbListen::CDunUsbListen
       
   129 // ---------------------------------------------------------------------------
       
   130 //
       
   131 CDunUsbListen::CDunUsbListen( MDunServerCallback* aServer,
       
   132                               MDunListenCallback* aParent,
       
   133                               RUsb& aUsbServer ) :
       
   134     CActive( EPriorityStandard ),
       
   135     iServer( aServer ),
       
   136     iParent( aParent ),
       
   137     iUsbServer( aUsbServer )
       
   138     {
       
   139     Initialize();
       
   140     }
       
   141 
       
   142 // ---------------------------------------------------------------------------
       
   143 // CDunUsbListen::ConstructL
       
   144 // ---------------------------------------------------------------------------
       
   145 //
       
   146 void CDunUsbListen::ConstructL()
       
   147     {
       
   148     FTRACE(FPrint( _L( "CDunUsbListen::ConstructL()" ) ));
       
   149     if ( !iServer || !iParent )
       
   150         {
       
   151         User::Leave( KErrGeneral );
       
   152         }
       
   153     CActiveScheduler::Add( this );
       
   154     FTRACE(FPrint( _L( "CDunUsbListen::ConstructL() complete" ) ));
       
   155     }
       
   156 
       
   157 // ---------------------------------------------------------------------------
       
   158 // Initializes this class
       
   159 // ---------------------------------------------------------------------------
       
   160 //
       
   161 void CDunUsbListen::Initialize()
       
   162     {
       
   163     // Don't initialize iServer here (it is set through NewL)
       
   164     // Don't initialize iParent here (it is set through NewL)
       
   165     // Don't initialize iUsbServer here (it is set through NewL)
       
   166     iDeviceState = EUsbDeviceStateUndefined;
       
   167     iDeviceStatePrev = EUsbDeviceStateUndefined;
       
   168     }
       
   169 
       
   170 // ---------------------------------------------------------------------------
       
   171 // Activates listening request
       
   172 // ---------------------------------------------------------------------------
       
   173 //
       
   174 TInt CDunUsbListen::Activate()
       
   175     {
       
   176     FTRACE(FPrint( _L( "CDunUsbListen::Activate()" ) ));
       
   177 
       
   178     if ( iListenState != EUsbListenStateIdle )
       
   179         {
       
   180         FTRACE(FPrint( _L( "CDunUsbListen::Activate() (not ready) complete" ) ));
       
   181         return KErrNotReady;
       
   182         }
       
   183     iDeviceStatePrev = iDeviceState;
       
   184     iStatus = KRequestPending;
       
   185     iUsbServer.DeviceStateNotification( KDunUsbDeviceStateMask,
       
   186                                         iDeviceState,
       
   187                                         iStatus );
       
   188     SetActive();
       
   189     iListenState = EUsbListenStateListening;
       
   190     FTRACE(FPrint( _L( "CDunUsbListen::Activate() complete" ) ));
       
   191     return KErrNone;
       
   192     }
       
   193 
       
   194 // ---------------------------------------------------------------------------
       
   195 // From class CActive.
       
   196 // Called when read or write operation is ready.
       
   197 // ---------------------------------------------------------------------------
       
   198 //
       
   199 void CDunUsbListen::RunL()
       
   200     {
       
   201     FTRACE(FPrint( _L( "CDunUsbListen::RunL() iStatus=%d"), iStatus.Int() ));
       
   202     iListenState = EUsbListenStateIdle;
       
   203 
       
   204     if ( iStatus.Int() != KErrNone )
       
   205         {
       
   206         FTRACE(FPrint( _L( "CDunUsbListen::RunL() (ERROR) complete" )));
       
   207         iServer->NotifyPluginCloseRequest( KDunUsbPluginUid, ETrue );
       
   208         return;
       
   209         }
       
   210 
       
   211     FTRACE(FPrint( _L( "CDunUsbListen::RunL() Usb device state =%X, Issue request" ), iDeviceState));
       
   212 
       
   213     TDunPluginState parentState = iServer->GetPluginStateByUid( KDunUsbPluginUid );
       
   214 
       
   215     TInt retTemp = KErrNone;
       
   216     if ( iDeviceState == EUsbDeviceStateConfigured &&
       
   217          iDeviceStatePrev != EUsbDeviceStateConfigured &&
       
   218          parentState != EDunStateChanneled )
       
   219         {
       
   220         // USB has been connected&configured and we are in PC Suite mode
       
   221         FTRACE(FPrint( _L( "CDunUsbListen::RunL() DeviceState is configured -> open connection" ) ));
       
   222         TBool noFreeChans = EFalse;
       
   223         // noFreeChans will be omitted (not needed to set to RComm)
       
   224         retTemp = iParent->NotifyChannelAllocate( noFreeChans );
       
   225         if ( retTemp != KErrNone )
       
   226             {
       
   227             FTRACE(FPrint( _L( "CDunUsbListen::RunL() channel allocation failed!" ) ));
       
   228             iServer->NotifyPluginCloseRequest( KDunUsbPluginUid, ETrue );
       
   229             return;
       
   230             }
       
   231         }
       
   232     else if ( iDeviceState < EUsbDeviceStateConfigured &&
       
   233               iDeviceStatePrev >= EUsbDeviceStateConfigured &&
       
   234               parentState == EDunStateChanneled )
       
   235         {
       
   236         FTRACE(FPrint( _L( "CDunUsbListen::RunL() DeviceState is not configured -> close connection" ) ));
       
   237         retTemp = iParent->NotifyChannelFree();
       
   238         if ( retTemp != KErrNone )
       
   239             {
       
   240             FTRACE(FPrint( _L( "CDunUsbListen::RunL() channel free failed!" ) ));
       
   241             iServer->NotifyPluginCloseRequest( KDunUsbPluginUid, ETrue );
       
   242             return;
       
   243             }
       
   244         }
       
   245 
       
   246     // Start listening again
       
   247     Activate();
       
   248 
       
   249     FTRACE(FPrint( _L( "CDunUsbListen::RunL() complete" )));
       
   250     }
       
   251 
       
   252 // ---------------------------------------------------------------------------
       
   253 // From class CActive.
       
   254 // Cancel current activity.
       
   255 // ---------------------------------------------------------------------------
       
   256 //
       
   257 void CDunUsbListen::DoCancel()
       
   258     {
       
   259     }