usbengines/usbwatcher/src/cusbactivepersonalityhandler.cpp
changeset 0 1e05558e2206
child 1 705ec7b86991
equal deleted inserted replaced
-1:000000000000 0:1e05558e2206
       
     1 /*
       
     2 * Copyright (c) 2002-2009 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:  This implements UsbActivePersonalityHandler class.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <etelmm.h>             // for fetching the IMEI code
       
    21 #include <mmtsy_names.h>        // for RTelServer names
       
    22 #include <UsbWatcherInternalCRKeys.h>
       
    23 #include <cusbpersonalitynotifier.h>
       
    24 #include <cusbpersonalityplugin.h>
       
    25 #include <tusbpersonalityparams.h>
       
    26 #include <startupdomainpskeys.h> //for global system state
       
    27 #include <rusb.h> // for KUsbmanSvrUid
       
    28 #include "cusbactivepersonalityhandler.h"
       
    29 #include "cusbglobalsystemstateobserver.h"
       
    30 
       
    31 // CONSTANTS
       
    32 // const TInt KSerialNumberLength = 12;
       
    33 const TInt KContainerIdLength = 16;
       
    34 
       
    35 // ============================ MEMBER FUNCTIONS ==============================
       
    36 
       
    37 // ----------------------------------------------------------------------------
       
    38 // C++ default constructor can NOT contain any code, that might leave.
       
    39 // ----------------------------------------------------------------------------
       
    40 //
       
    41 CUsbActivePersonalityHandler::CUsbActivePersonalityHandler(
       
    42     RUsb& aUsbMan, CUsbWatcher& aOwner )
       
    43     : CActive( EPriorityStandard )
       
    44     , iUsbMan( aUsbMan )
       
    45     , iOwner( aOwner )
       
    46     {
       
    47     CActiveScheduler::Add( this );
       
    48     }
       
    49 
       
    50 // ----------------------------------------------------------------------------
       
    51 // Symbian 2nd phase constructor can leave.
       
    52 // ----------------------------------------------------------------------------
       
    53 //
       
    54 void CUsbActivePersonalityHandler::ConstructL()
       
    55     {
       
    56     LOG_FUNC
       
    57 
       
    58     iPersonalityNotifier = CUsbPersonalityNotifier::NewL();
       
    59     iPersonalityParams = new ( ELeave ) TUsbPersonalityParams( iUsbMan,
       
    60             *iPersonalityNotifier );
       
    61 
       
    62     ConstructUsbSerialNumberL();
       
    63     }
       
    64 
       
    65 // ----------------------------------------------------------------------------
       
    66 // Two-phased constructor.
       
    67 // ----------------------------------------------------------------------------
       
    68 //
       
    69 CUsbActivePersonalityHandler* CUsbActivePersonalityHandler::NewL(
       
    70         RUsb& aUsbMan, CUsbWatcher& aOwner )
       
    71     {
       
    72     LOG_FUNC
       
    73 
       
    74     CUsbActivePersonalityHandler* self = new ( ELeave )
       
    75             CUsbActivePersonalityHandler( aUsbMan, aOwner );
       
    76     CleanupStack::PushL( self );
       
    77     self->ConstructL();
       
    78     CleanupStack::Pop(); // pop self
       
    79     return self;
       
    80     }
       
    81 
       
    82 // Destructor
       
    83 CUsbActivePersonalityHandler::~CUsbActivePersonalityHandler()
       
    84     {
       
    85     LOG_FUNC
       
    86 
       
    87     Cancel(); // cancel any outstanding requests
       
    88 
       
    89     delete iCurrentPersonalityHandler;
       
    90     delete iPersonalityNotifier;
       
    91     delete iPersonalityParams;
       
    92     }
       
    93 
       
    94 // ----------------------------------------------------------------------------
       
    95 // Construct USB serial number. Some of the personalities may need this.
       
    96 // ----------------------------------------------------------------------------
       
    97 //
       
    98 void CUsbActivePersonalityHandler::ConstructUsbSerialNumberL()
       
    99     {
       
   100     LOG_FUNC
       
   101 
       
   102     RTelServer telServer;
       
   103     RMobilePhone phone;
       
   104 
       
   105     LEAVEIFERROR( telServer.Connect() );
       
   106     CleanupClosePushL( telServer );
       
   107 
       
   108     LEAVEIFERROR( telServer.LoadPhoneModule( KMmTsyModuleName ) );
       
   109 
       
   110     LEAVEIFERROR( phone.Open( telServer, KMmTsyPhoneName ) );
       
   111     CleanupClosePushL( phone );
       
   112 
       
   113     // to store the serial number to be published as the USB device's serial
       
   114     // number, fetch it from telephony server.
       
   115     TRequestStatus status;
       
   116     phone.GetPhoneId( status, iPhoneInfo );
       
   117     User::WaitForRequest( status );
       
   118 
       
   119     // make sure that the serial number fulfills USB requirements and then
       
   120     // convert the serial number so that it can be printed to log
       
   121     TLex lex( iPhoneInfo.iSerialNumber );
       
   122     TInt length = iPhoneInfo.iSerialNumber.Length();
       
   123 
       
   124     // currently the serial number is used for the USB container ID
       
   125     // the container ID length must be at least 16 bytes
       
   126     // also, even when serial number is not used for the container id
       
   127     // it must be at least 12, see below
       
   128     if( length < KContainerIdLength )
       
   129         {
       
   130         // In GSM, the complete IMEI can used as USB serial number. But in
       
   131         // CDMA, the ESN is too short for a valid Mass Storage serial number
       
   132         // (Mass-Storage and Bulk Only Transport specs both require minimum
       
   133         // 12 byte number), so it is extended with leading zeroes. When
       
   134         // doing this, make sure not to write anything over descriptor's
       
   135         // max length
       
   136         if( iPhoneInfo.iSerialNumber.MaxLength() < KContainerIdLength )
       
   137             {
       
   138             iPhoneInfo.iSerialNumber.SetLength( KContainerIdLength );
       
   139             }
       
   140         while( length < KContainerIdLength )
       
   141             {
       
   142             iPhoneInfo.iSerialNumber.Append( '0' );
       
   143             ++length;
       
   144             }
       
   145         }
       
   146 
       
   147     CleanupStack::PopAndDestroy( 2, &telServer );
       
   148     }
       
   149 
       
   150 // ----------------------------------------------------------------------------
       
   151 // Confirm that personality can be loaded.
       
   152 // ----------------------------------------------------------------------------
       
   153 //
       
   154 void CUsbActivePersonalityHandler::ConfirmPersonalityUnload( TRequestStatus&
       
   155         aStatus )
       
   156     {
       
   157     LOG_FUNC
       
   158 
       
   159     aStatus = KRequestPending;
       
   160     iRequestStatus = &aStatus;
       
   161 
       
   162     if( iState != EUsbPersonalityStarted )
       
   163         {
       
   164         TRequestStatus* status = &iStatus;
       
   165         SetActive();
       
   166         User::RequestComplete( status, KErrGeneral );
       
   167         return;
       
   168         }
       
   169 
       
   170     // Cancel all notes before confirmation
       
   171     iPersonalityParams->PersonalityNotifier().CancelAll();
       
   172 
       
   173     if( iCurrentPersonalityHandler )
       
   174         {
       
   175         iCurrentPersonalityHandler->ConfirmPersonalityUnload( iStatus );
       
   176         SetActive();
       
   177         }
       
   178     else
       
   179         {
       
   180         TRequestStatus* status = &iStatus;
       
   181         SetActive();
       
   182         User::RequestComplete( status, KErrNone );
       
   183         }
       
   184     }
       
   185 
       
   186 // ----------------------------------------------------------------------------
       
   187 // Start personality.
       
   188 // ----------------------------------------------------------------------------
       
   189 //
       
   190 void CUsbActivePersonalityHandler::StartPersonality( TInt& aPersonalityId,
       
   191         TInt aAskOnConnectionSetting, TRequestStatus& aStatus )
       
   192     {
       
   193     LOG_FUNC
       
   194 
       
   195     LOG2( "PersonalityId = %d, AskOnConnectionSetting = %d", aPersonalityId,
       
   196             aAskOnConnectionSetting );
       
   197 
       
   198     // Remove all notes.
       
   199     iPersonalityNotifier->CancelAll();
       
   200      
       
   201 
       
   202     iPersonalityId = &aPersonalityId;
       
   203     iAskOnConnectionSetting = aAskOnConnectionSetting;
       
   204     aStatus = KRequestPending;
       
   205     iRequestStatus = &aStatus;
       
   206 
       
   207     // prepare current personality for start and return
       
   208     if( iCurrentPersonalityHandler )
       
   209         {
       
   210         LOG( "Previous PersonalityHandler not deleted" );
       
   211         User::RequestComplete( iRequestStatus, KErrGeneral );
       
   212         return;
       
   213         }
       
   214 
       
   215     TRAPD( ret, ( iCurrentPersonalityHandler = NewPersonalityHandlerL(
       
   216             *iPersonalityId ) ) );
       
   217 
       
   218     if( ( ret == KErrNone) && iCurrentPersonalityHandler )
       
   219         {
       
   220         LOG( "PersonalityHandler created" );
       
   221         iCurrentPersonalityHandler->PreparePersonalityStart( iStatus );
       
   222         iState = EUsbPersonalityPrepareStart;
       
   223         SetActive();
       
   224         }
       
   225     else
       
   226         {
       
   227         LOG( "Error: PersonalityHandler create failed" );
       
   228         User::RequestComplete( iRequestStatus, KErrGeneral );
       
   229         }
       
   230     }
       
   231 
       
   232 // ----------------------------------------------------------------------------
       
   233 // Stop current personality.
       
   234 // ----------------------------------------------------------------------------
       
   235 //
       
   236 void CUsbActivePersonalityHandler::StopPersonality( TRequestStatus& aStatus )
       
   237     {
       
   238     LOG_FUNC
       
   239 
       
   240     aStatus = KRequestPending;
       
   241     iRequestStatus = &aStatus;
       
   242 
       
   243     iState = EUsbPersonalityPrepareStop;
       
   244 
       
   245     // prepare current personality for stop and return
       
   246     if( iCurrentPersonalityHandler )
       
   247         {
       
   248         LOG( "Call PersonalityPlugin to prepare stop" );
       
   249         iCurrentPersonalityHandler->PreparePersonalityStop( iStatus );
       
   250         SetActive();
       
   251         }
       
   252     else
       
   253         {
       
   254         LOG( "No current PersonalityPlugin, return immediately" );
       
   255         TRequestStatus* status = &iStatus;
       
   256         SetActive();
       
   257         User::RequestComplete( status, KErrNone );
       
   258         }
       
   259     }
       
   260 
       
   261 // ----------------------------------------------------------------------------
       
   262 // Indicates USB device state change to personality when USB is started.
       
   263 // ----------------------------------------------------------------------------
       
   264 //
       
   265 void CUsbActivePersonalityHandler::StateChangeNotify(
       
   266         TUsbDeviceState aStateOld, TUsbDeviceState aStateNew )
       
   267     {
       
   268     LOG_FUNC
       
   269 
       
   270     iDeviceState = aStateNew;        
       
   271     switch ( aStateNew )
       
   272         {
       
   273         case EUsbDeviceStateAddress:
       
   274             {
       
   275             if( iAskOnConnectionSetting &&
       
   276                     ( aStateOld != EUsbDeviceStateSuspended ) &&
       
   277                     ( aStateOld != EUsbDeviceStateConfigured ) 
       
   278                      )
       
   279                 {
       
   280                 iPersonalityParams->PersonalityNotifier().ShowQuery(
       
   281                         KCableConnectedNotifierUid, iDummy,
       
   282                         iPersonalityPckgBuf);
       
   283                 }
       
   284             break;
       
   285             }
       
   286        default:
       
   287             // We do not handle other state here
       
   288 			LOG( "DeviceStatechange ignored by ActivePersonalityhandler or EUsbDeviceStateConfigured" );
       
   289             break;
       
   290         }
       
   291 
       
   292     if( iCurrentPersonalityHandler )
       
   293         {
       
   294         LOG( "Notifying PersonalityPlugin of the new state" );
       
   295         iCurrentPersonalityHandler->StateChangeNotify( aStateNew );
       
   296         }
       
   297     }
       
   298 
       
   299 // ----------------------------------------------------------------------------
       
   300 // Standard active object error function. Handle error and complete
       
   301 // outstanding request. We must not come here.
       
   302 // ----------------------------------------------------------------------------
       
   303 //
       
   304 TInt CUsbActivePersonalityHandler::RunError( TInt /*aError*/ )
       
   305     {
       
   306     LOG_FUNC
       
   307 
       
   308     return KErrNone;
       
   309     }
       
   310 
       
   311 // ----------------------------------------------------------------------------
       
   312 // Executed when iStatus is completed.
       
   313 // ----------------------------------------------------------------------------
       
   314 //
       
   315 void CUsbActivePersonalityHandler::RunL()
       
   316     {
       
   317     LOG_FUNC
       
   318 
       
   319     TInt ret = iStatus.Int();
       
   320     if( KErrNone != ret )
       
   321         {
       
   322         LOG1( "ERROR: CUsbActivePersonalityHandler::RunL iStatus = %d", ret );
       
   323         }
       
   324 
       
   325     switch( iState )
       
   326         {
       
   327         case EUsbPersonalityIdle:
       
   328             break;
       
   329 
       
   330         case EUsbPersonalityPrepareStart:
       
   331             {
       
   332             LOG( "EUsbPersonalityPrepareStart" );
       
   333             // then write the serial number to P&S for USB Manager
       
   334             PublishSerialNumber();
       
   335             iUsbMan.TryStart( *iPersonalityId, iStatus );
       
   336             iState = EUsbPersonalityStartUsb;
       
   337             SetActive();
       
   338             }
       
   339             break;
       
   340 
       
   341         case EUsbPersonalityStartUsb:
       
   342             LOG( "EUsbPersonalityStartUsb" );
       
   343             iState = EUsbPersonalityFinishStart;
       
   344             if( iCurrentPersonalityHandler )
       
   345                 {
       
   346                 iCurrentPersonalityHandler->FinishPersonalityStart( iStatus );
       
   347                 SetActive();
       
   348                 }
       
   349             else
       
   350                 {
       
   351                 TRequestStatus* status = &iStatus;
       
   352                 SetActive();
       
   353                 User::RequestComplete( status, KErrNone );
       
   354                 }
       
   355             break;
       
   356 
       
   357         case EUsbPersonalityFinishStart:
       
   358             LOG( "EUsbPersonalityFinishStart" );
       
   359             User::RequestComplete( iRequestStatus, ret );
       
   360             iState = EUsbPersonalityStarted;
       
   361             break;
       
   362 
       
   363         case EUsbPersonalityPrepareStop:
       
   364             LOG( "EUsbPersonalityPrepareStop" );
       
   365             iUsbMan.TryStop( iStatus );
       
   366             iState = EUsbPersonalityStopUsb;
       
   367             SetActive();
       
   368             break;
       
   369 
       
   370         case EUsbPersonalityStopUsb:
       
   371             LOG( "EUsbPersonalityStopUsb" );
       
   372             iState = EUsbPersonalityFinishStop;
       
   373             if( iCurrentPersonalityHandler )
       
   374                 {
       
   375                 iCurrentPersonalityHandler->FinishPersonalityStop( iStatus );
       
   376                 SetActive();
       
   377                 }
       
   378             else
       
   379                 {
       
   380                 TRequestStatus* status = &iStatus;
       
   381                 SetActive();
       
   382                 User::RequestComplete( status, KErrNone );
       
   383                 }
       
   384             break;
       
   385 
       
   386         case EUsbPersonalityFinishStop:
       
   387             LOG( "EUsbPersonalityFinishStop" );
       
   388 
       
   389             delete iCurrentPersonalityHandler;
       
   390             iCurrentPersonalityHandler = NULL;
       
   391 
       
   392             //iAskOnConnectionSetting may be have been set to off
       
   393             if ( iDeviceState == EUsbDeviceStateUndefined )
       
   394                 {
       
   395 	            iPersonalityParams->PersonalityNotifier().CancelQuery(
       
   396 	                    KCableConnectedNotifierUid );
       
   397                 }
       
   398             User::RequestComplete( iRequestStatus, ret );
       
   399 
       
   400             iState = EUsbPersonalityIdle;
       
   401             break;
       
   402 
       
   403         case EUsbPersonalityStarted:
       
   404             // This must unload event. Let's complete.
       
   405             User::RequestComplete( iRequestStatus, ret );
       
   406             break;
       
   407 
       
   408         default:
       
   409             LOG( "ERROR: unexpected state" );
       
   410             PANIC( KErrGeneral );
       
   411             break;
       
   412         }
       
   413     }
       
   414 
       
   415 // ----------------------------------------------------------------------------
       
   416 // Standard active object cancellation function.
       
   417 // ----------------------------------------------------------------------------
       
   418 //
       
   419 void CUsbActivePersonalityHandler::DoCancel()
       
   420     {
       
   421     LOG_FUNC
       
   422 
       
   423     LOG1( "CUsbActivePersonalityHandler::iState = %d", iState );
       
   424     switch( iState )
       
   425         {
       
   426         case EUsbPersonalityFinishStart:
       
   427             {
       
   428             TRequestStatus status;
       
   429 
       
   430             iUsbMan.TryStop( status );
       
   431             SetActive();
       
   432             User::WaitForRequest( status );
       
   433             }
       
   434             // Don't break. We need to cancel outstanding request.
       
   435 
       
   436         case EUsbPersonalityStarted:
       
   437         case EUsbPersonalityPrepareStop:
       
   438         case EUsbPersonalityFinishStop:
       
   439             if( iCurrentPersonalityHandler )
       
   440                 {
       
   441                 iCurrentPersonalityHandler->Cancel();
       
   442                 }
       
   443             break;
       
   444 
       
   445         case EUsbPersonalityStopUsb:
       
   446             iUsbMan.CancelInterest( RUsb::ETryStop );
       
   447             break;
       
   448 
       
   449         case EUsbPersonalityStartUsb:
       
   450             iUsbMan.StartCancel();
       
   451             break;
       
   452 
       
   453         default:
       
   454             break;
       
   455         }
       
   456 
       
   457     if( iCurrentPersonalityHandler && ( iState != EUsbPersonalityStarted ) )
       
   458         {
       
   459         delete iCurrentPersonalityHandler;
       
   460         iCurrentPersonalityHandler = NULL;
       
   461         }
       
   462 
       
   463     // if started then this must unload confirmation
       
   464     if( iState != EUsbPersonalityStarted )
       
   465         {
       
   466         iState = EUsbPersonalityIdle;
       
   467         }
       
   468 
       
   469     // When cancel happens it means that we can cancel all queries & notes
       
   470     iPersonalityParams->PersonalityNotifier().CancelAll();
       
   471 
       
   472     User::RequestComplete( iRequestStatus, KErrCancel );
       
   473     }
       
   474 
       
   475 // ----------------------------------------------------------------------------
       
   476 // Creates and returns a class handler object for the given personality.
       
   477 // ----------------------------------------------------------------------------
       
   478 //
       
   479 CUsbPersonality* CUsbActivePersonalityHandler::NewPersonalityHandlerL(
       
   480         TInt aPersonality )
       
   481     {
       
   482     LOG_FUNC
       
   483 
       
   484     TInt personalityCount = iOwner.Personalities().Count();
       
   485 
       
   486     for (TInt i = 0; i < personalityCount; i++)
       
   487         {
       
   488         const TUsbSupportedPersonalityInf& pinf = iOwner.Personalities()[i];
       
   489         if( pinf.iPersonalityId == aPersonality )
       
   490             {
       
   491             iPersonalityParams->SetPersonalityId( aPersonality );
       
   492             CUsbPersonalityPlugin* plugin = CUsbPersonalityPlugin::NewL(
       
   493                     *iPersonalityParams, pinf.iPersonalityUid );
       
   494             iUseSerialNumber = pinf.iUseSerialNumber;
       
   495             return plugin;
       
   496             }
       
   497         }
       
   498 
       
   499     return NULL;
       
   500     }
       
   501 
       
   502 // ----------------------------------------------------------------------------
       
   503 // Publish serial number for USB Manager, if needed.
       
   504 // ----------------------------------------------------------------------------
       
   505 //
       
   506 TInt CUsbActivePersonalityHandler::PublishSerialNumber()
       
   507     {
       
   508     LOG_FUNC
       
   509 
       
   510     TInt err = KErrNone;
       
   511 
       
   512     if( !iUseSerialNumber && iSerialNumberWritten )
       
   513         {
       
   514         // We are either in test mode or going to start up PC Suite
       
   515         // personality -> delete USB Manager's serial number P&S key
       
   516         // (if necessary)
       
   517         LOG( "Deleting published USB serial number" );
       
   518         err = RProperty::Delete( KUidSystemCategory, KUsbmanSvrUid.iUid );
       
   519         iSerialNumberWritten = EFalse;
       
   520         }
       
   521     else if( iUseSerialNumber && !iSerialNumberWritten )
       
   522         {
       
   523         // to finish, define and write the serial number P&S so that USB
       
   524         // Manager can fetch it
       
   525         _LIT_SECURITY_POLICY_PASS( KAPReadPolicy );
       
   526         _LIT_SECURITY_POLICY_PASS( KAPWritePolicy );
       
   527 
       
   528         err = RProperty::Define( KUidSystemCategory, KUsbmanSvrUid.iUid,
       
   529                 RProperty::EText, KAPReadPolicy, KAPWritePolicy,
       
   530                 KUsbStringDescStringMaxSize );
       
   531 
       
   532         if( !err )
       
   533             {
       
   534             err = RProperty::Set( KUidSystemCategory, KUsbmanSvrUid.iUid,
       
   535                     iPhoneInfo.iSerialNumber );
       
   536             iSerialNumberWritten = ( err == KErrNone );
       
   537             }
       
   538         }
       
   539 
       
   540     LOG1(" ret = %d", err );
       
   541 
       
   542     return err;
       
   543     }
       
   544 
       
   545 // ----------------------------------------------------------------------------
       
   546 // Cancel cable connected notifier
       
   547 // ----------------------------------------------------------------------------
       
   548 //
       
   549 void CUsbActivePersonalityHandler::CancelCableConnectedNotifier()
       
   550     {
       
   551     LOG_FUNC
       
   552 
       
   553     iPersonalityParams->PersonalityNotifier().CancelQuery(
       
   554             KCableConnectedNotifierUid );
       
   555     }
       
   556 
       
   557 // End of file