usbengines/usbotgwatcher/src/cusbpersonalityswitch.cpp
changeset 0 1e05558e2206
equal deleted inserted replaced
-1:000000000000 0:1e05558e2206
       
     1 /*
       
     2 * Copyright (c) 2008-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:  Switches personality
       
    15  *
       
    16 */
       
    17 
       
    18 #include <e32base.h>
       
    19 #include <usb.h>
       
    20 #include <usbpersonalityids.h>
       
    21 
       
    22 #include "cusbpersonalityswitch.h"
       
    23 
       
    24 #include "panic.h"
       
    25 #include "debug.h"
       
    26 
       
    27 // ---------------------------------------------------------------------------
       
    28 // 
       
    29 // ---------------------------------------------------------------------------
       
    30 //
       
    31 CUsbPersonalitySwitch::CUsbPersonalitySwitch(
       
    32         MUsbPersonalitySwitchObserver* aObserver) :
       
    33     CActive(CActive::EPriorityStandard), iObserver(aObserver), iState(EIdle)
       
    34     {
       
    35     CActiveScheduler::Add(this);
       
    36     }
       
    37 
       
    38 // ---------------------------------------------------------------------------
       
    39 // 
       
    40 // ---------------------------------------------------------------------------
       
    41 //
       
    42 CUsbPersonalitySwitch::~CUsbPersonalitySwitch()
       
    43     {
       
    44         FLOG( _L( "[USBOTGWATCHER]\tCUsbPersonalitySwitch::~CUsbPersonalitySwitch" ) );
       
    45     Cancel();
       
    46     iUsbInterface.Close();
       
    47     iSendData.Close();
       
    48     iRcvData.Close();
       
    49     }
       
    50 
       
    51 // ---------------------------------------------------------------------------
       
    52 // 
       
    53 // ---------------------------------------------------------------------------
       
    54 //
       
    55 void CUsbPersonalitySwitch::ConstructL()
       
    56     {
       
    57         FLOG( _L( "[USBOTGWATCHER]\tCUsbPersonalitySwitch::ConstructL" ) );
       
    58     }
       
    59 
       
    60 // ---------------------------------------------------------------------------
       
    61 // 
       
    62 // ---------------------------------------------------------------------------
       
    63 //
       
    64 CUsbPersonalitySwitch* CUsbPersonalitySwitch::NewL(
       
    65         MUsbPersonalitySwitchObserver* aObserver)
       
    66     {
       
    67         FLOG( _L( "[USBOTGWATCHER]\tCUsbPersonalitySwitch::NewL" ) );
       
    68 
       
    69     CUsbPersonalitySwitch* self = new (ELeave) CUsbPersonalitySwitch(
       
    70             aObserver);
       
    71     CleanupStack::PushL(self);
       
    72     self->ConstructL();
       
    73     CleanupStack::Pop(self); // pop self
       
    74     return self;
       
    75     }
       
    76 
       
    77 // ---------------------------------------------------------------------------
       
    78 // 
       
    79 // ---------------------------------------------------------------------------
       
    80 //
       
    81 TInt CUsbPersonalitySwitch::SwitchPersonalityL(TUint32 aDeviceId,
       
    82         TInt aPersonalityToBeSet)
       
    83     {
       
    84         FTRACE(FPrint(_L( "[USBOTGWATCHER]\tCUsbPersonalitySwitch::SwitchPersonality aDeviceId %d, aPersonalityToBeSet %d" ), aDeviceId, aPersonalityToBeSet))
       
    85 
       
    86     if (IsActive() || EIdle != iState)
       
    87         {
       
    88         Cancel();
       
    89         iObserver->UsbPersonalitySwitchStateChangedL(
       
    90                 MUsbPersonalitySwitchObserver::ERequestCancelled, KErrNone);
       
    91         }
       
    92 
       
    93     // create usb interface for the device id
       
    94     TUint32 token(0); // = RFdc.InterfaceToken();
       
    95     TInt err = iUsbInterface.Open(token);
       
    96 
       
    97         FTRACE(FPrint(_L("[USBOTGWATCHER]\tCUsbPersonalitySwitch::SwitchPersonality UsbInterface Open err = %d" ), err));
       
    98     if (KErrNone != err)
       
    99         {
       
   100         return err;
       
   101         }
       
   102 
       
   103     iPersonalityToBeSet = aPersonalityToBeSet;
       
   104 
       
   105     // get list of supported personalities
       
   106     iTransfer.iRequestType = KUsbRequestType_DirToHost
       
   107             | KUsbRequestType_TypeVendor | KUsbRequestType_DestDevice;
       
   108 
       
   109     iTransfer.iRequest = KGetAllPersonalitiesReq;
       
   110     iTransfer.iValue = 0;
       
   111     iTransfer.iIndex = 0;
       
   112     iTransfer.iFlags
       
   113             = RUsbInterface::TUsbTransferRequestDetails::EShortTransferOk;
       
   114 
       
   115     Reset();
       
   116 
       
   117     iState = EGetAllPersonalities;
       
   118 
       
   119         FLOG( _L( "[USBOTGWATCHER]\tCUsbPersonalitySwitch::SwitchPersonalityL Sending GetAllPersonalities request." ) );
       
   120     iUsbInterface.Ep0Transfer(iTransfer, iSendData, iRcvData, iStatus);
       
   121     SetActive();
       
   122 
       
   123     iObserver->UsbPersonalitySwitchStateChangedL(
       
   124             MUsbPersonalitySwitchObserver::ERequestingAllPersonalities,
       
   125             KErrNone);
       
   126 
       
   127     return KErrNone;
       
   128     }
       
   129 
       
   130 void CUsbPersonalitySwitch::CancelSwitchPersonalityL()
       
   131     {
       
   132         FLOG(_L( "[USBOTGWATCHER]\tCUsbPersonalitySwitch::CancelSwitchPersonalityL"));
       
   133 
       
   134     if (IsActive())
       
   135         {
       
   136         Cancel();
       
   137         iObserver->UsbPersonalitySwitchStateChangedL(
       
   138                 MUsbPersonalitySwitchObserver::ERequestCancelled, KErrNone);
       
   139         }
       
   140     }
       
   141 
       
   142 // ---------------------------------------------------------------------------
       
   143 // 
       
   144 // ---------------------------------------------------------------------------
       
   145 //
       
   146 void CUsbPersonalitySwitch::RunL()
       
   147     {
       
   148 
       
   149     if (KErrNone != iStatus.Int())
       
   150         {
       
   151             FTRACE(FPrint(_L( "[USBOTGWATCHER]\tCUsbPersonalitySwitch::RunL iStatus %d" ), iStatus.Int()));
       
   152         User::Leave(iStatus.Int());
       
   153         }
       
   154 
       
   155     switch (iState)
       
   156         {
       
   157 
       
   158         case EGetAllPersonalities:
       
   159             {
       
   160                 FLOG(_L( "[USBOTGWATCHER]\tCUsbPersonalitySwitch::RunL GetAllPersonalities completed."));
       
   161 
       
   162             TInt numOfPersonalities = iRcvData[KNumOfPersShift];
       
   163                 FTRACE(FPrint(_L("[USBOTGWATCHER]\tCUsbPersonalitySwitch::RunL Amount of personalities in peripheral = %d" ), numOfPersonalities));
       
   164 
       
   165             // if no personalities then return
       
   166             if (0 == numOfPersonalities)
       
   167                 {
       
   168                     FLOG(_L( "[USBOTGWATCHER]\tCUsbPersonalitySwitch::RunL No personailies found in peripheral."));
       
   169                 Reset();
       
   170                 iObserver->UsbPersonalitySwitchStateChangedL(
       
   171                         MUsbPersonalitySwitchObserver::EGetAllPersonalitiesCompleted,
       
   172                         KErrNotFound);
       
   173                 return;
       
   174                 }
       
   175 
       
   176             // if needed pers already set, then return
       
   177             TInt currentPersonality = iRcvData[KCurrPersShift];
       
   178             if (iPersonalityToBeSet == currentPersonality)
       
   179                 {
       
   180                     FLOG(_L( "[USBOTGWATCHER]\tCUsbPersonalitySwitch::RunL Needed personality already set in peripheral."));
       
   181                 Reset();
       
   182                 iObserver->UsbPersonalitySwitchStateChangedL(
       
   183                         MUsbPersonalitySwitchObserver::EGetAllPersonalitiesCompleted,
       
   184                         KErrInUse);
       
   185                 return;
       
   186                 }
       
   187 
       
   188             // check if device supports needed personality
       
   189             TInt count(0);
       
   190             while (count < numOfPersonalities && iPersonalityToBeSet
       
   191                     != iRcvData[KFirstPersonalityIdShift
       
   192                             + KLenghtOfPersonalityData * count])
       
   193                 {
       
   194                 ++count;
       
   195                 }
       
   196 
       
   197             if (count == numOfPersonalities) // not found == not supported
       
   198                 {
       
   199                     FLOG(_L( "[USBOTGWATCHER]\tCUsbPersonalitySwitch::RunL Peripheral does not support needed personality."));
       
   200                 Reset();
       
   201                 iObserver->UsbPersonalitySwitchStateChangedL(
       
   202                         MUsbPersonalitySwitchObserver::EGetAllPersonalitiesCompleted,
       
   203                         KErrNotFound);
       
   204                 return;
       
   205                 }
       
   206 
       
   207             iObserver->UsbPersonalitySwitchStateChangedL(
       
   208                     MUsbPersonalitySwitchObserver::EGetAllPersonalitiesCompleted,
       
   209                     KErrNone);
       
   210 
       
   211             // set personality
       
   212             iTransfer.iRequestType = KUsbRequestType_DirToDev
       
   213                     | KUsbRequestType_TypeVendor | KUsbRequestType_DestDevice;
       
   214 
       
   215             iTransfer.iRequest = KSetPersonalityReq;
       
   216             iTransfer.iValue = iPersonalityToBeSet;
       
   217             iTransfer.iIndex = 0;
       
   218             iTransfer.iFlags
       
   219                     = RUsbInterface::TUsbTransferRequestDetails::EShortTransferOk;
       
   220 
       
   221             iSendData.Close();
       
   222             iRcvData.Close();
       
   223 
       
   224             iState = ESetPersonality;
       
   225 
       
   226                 FLOG( _L( "[USBOTGWATCHER]\tCUsbPersonalitySwitch::RunL Sending SetPersonality request." ) );
       
   227             iUsbInterface.Ep0Transfer(iTransfer, iSendData, iRcvData, iStatus);
       
   228             SetActive();
       
   229             iObserver->UsbPersonalitySwitchStateChangedL(
       
   230                     MUsbPersonalitySwitchObserver::ERequestingSetPersonality,
       
   231                     KErrNone);
       
   232 
       
   233             break;
       
   234             }
       
   235         case ESetPersonality:
       
   236             {
       
   237                 FLOG(_L( "[USBOTGWATCHER]\tCUsbPersonalitySwitch::RunL SetPersonality completed."));
       
   238             Reset();
       
   239             iObserver->UsbPersonalitySwitchStateChangedL(
       
   240                     MUsbPersonalitySwitchObserver::ESetPersonalityCompleted,
       
   241                     KErrNone);
       
   242 
       
   243             break;
       
   244             }
       
   245             /*  case EGetAllPersResult:
       
   246              {
       
   247              FLOG( "[USBOTGWATCHER]\tCUsbPersonalitySwitch::RunL GetAllPersResult completed.");
       
   248 
       
   249              break;
       
   250              }
       
   251              case EGetSetPersResult:
       
   252              {
       
   253              FLOG( "[USBOTGWATCHER]\tCUsbPersonalitySwitch::RunL GetSetPersResult completed.");
       
   254 
       
   255              break;
       
   256              }  */
       
   257 
       
   258         case EIdle: // do not break 
       
   259         default:
       
   260             {
       
   261             Panic(EUnexpectedUsbSwitchPersonalityState);
       
   262             }
       
   263         }
       
   264 
       
   265     }
       
   266 
       
   267 // ---------------------------------------------------------------------------
       
   268 // 
       
   269 // ---------------------------------------------------------------------------
       
   270 //
       
   271 TInt CUsbPersonalitySwitch::RunError(TInt aError)
       
   272     {
       
   273         FTRACE(FPrint(_L( "[USBOTGWATCHER]\tCUsbPersonalitySwitch::RunError aError %d" ), aError ));
       
   274     Reset();
       
   275     TRAP_IGNORE (iObserver->UsbPersonalitySwitchStateChangedL(
       
   276             MUsbPersonalitySwitchObserver::ERequestFailed, aError) );
       
   277     return KErrNone;
       
   278     }
       
   279 
       
   280 // ---------------------------------------------------------------------------
       
   281 // 
       
   282 // ---------------------------------------------------------------------------
       
   283 //
       
   284 void CUsbPersonalitySwitch::DoCancel()
       
   285     {
       
   286         FLOG( _L( "[USBOTGWATCHER]\tCUsbPersonalitySwitch::DoCancel" ) )
       
   287 
       
   288     iUsbInterface.CancelEP0Transfer();
       
   289     Reset();
       
   290     }
       
   291 
       
   292 void CUsbPersonalitySwitch::Reset()
       
   293     {
       
   294         FLOG( _L( "[USBOTGWATCHER]\tCUsbPersonalitySwitch::Reset" ) )
       
   295 
       
   296     iState = EIdle;
       
   297     iSendData.Close();
       
   298     iRcvData.Close();
       
   299     }