mtpdataproviders/mtppictbridgedp/pictbridgeengine/src/dpsusbnotifier.cpp
branchRCL_3
changeset 19 0aa8cc770c8a
equal deleted inserted replaced
18:453dfc402455 19:0aa8cc770c8a
       
     1 /*
       
     2 * Copyright (c) 2006, 2007 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 class implements functions of set mtp personality, the 
       
    15 *                notification of the MTP printer connection and the
       
    16 *				 notification of the MTP printer disconnection. 
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 #include <e32debug.h>
       
    22 #include <usbstates.h>
       
    23 #include <rptp.h>
       
    24 #include "dpsusbnotifier.h"
       
    25 #include "dpsconst.h"
       
    26 #include "dpsptpnotifier.h"
       
    27 #include "dpsconnectnotifier.h"
       
    28 
       
    29 #ifdef _DEBUG
       
    30 #	define IF_DEBUG(t) {RDebug::t;}
       
    31 #else
       
    32 #	define IF_DEBUG(t)
       
    33 #endif
       
    34 
       
    35 const TInt KUnknownPersonality = 0;
       
    36 
       
    37 // ---------------------------------------------------------------------------
       
    38 // 
       
    39 // ---------------------------------------------------------------------------
       
    40 //
       
    41 CDpsUsbNotifier* CDpsUsbNotifier::NewL(CDpsEngine* aEngine)
       
    42 	{
       
    43 	IF_DEBUG(Print(_L("CDpsUsbNotifier::NewL")));
       
    44 	CDpsUsbNotifier* self = new(ELeave) CDpsUsbNotifier(aEngine);
       
    45 	CleanupStack::PushL(self);
       
    46 	self->ConstructL();
       
    47 	CleanupStack::Pop();
       
    48 	return self;	
       
    49 	}
       
    50 
       
    51 // ---------------------------------------------------------------------------
       
    52 // 
       
    53 // ---------------------------------------------------------------------------
       
    54 //
       
    55 CDpsUsbNotifier::CDpsUsbNotifier(CDpsEngine* aEngine) :
       
    56     CActive(EPriorityNormal), iEngine(aEngine), 
       
    57     iPersonality(KUnknownPersonality), 
       
    58     iConnectState(EUsbDeviceStateUndefined), iConfigured(EFalse), 
       
    59     iConnection(CDpsEngine::ENotConnected), iRollback(EFalse)
       
    60     {
       
    61     IF_DEBUG(Print(_L(">>>CDpsUsbNotifier::Ctor")));
       
    62     CActiveScheduler::Add(this);        
       
    63     IF_DEBUG(Print(_L("<<<CDpsUsbNotifier::Ctor")));
       
    64     }  
       
    65   
       
    66 // ---------------------------------------------------------------------------
       
    67 // 
       
    68 // ---------------------------------------------------------------------------
       
    69 //	
       
    70 CDpsUsbNotifier::~CDpsUsbNotifier()
       
    71 	{
       
    72 	IF_DEBUG(Print(_L(">>>~CDpsUsbNotifier")));
       
    73 	Cancel();
       
    74 	Rollback();
       
    75     delete iPtpP; iPtpP = NULL;
       
    76     delete iConnectP; iConnectP = NULL;	
       
    77     iUsbM.Close();
       
    78     iUsbW.Close();
       
    79     IF_DEBUG(Print(_L("<<<~CDpsUsbNotifier")));
       
    80 	}
       
    81 
       
    82 // ---------------------------------------------------------------------------
       
    83 // 
       
    84 // ---------------------------------------------------------------------------
       
    85 //	
       
    86 void CDpsUsbNotifier::ConstructL()
       
    87     {
       
    88     IF_DEBUG(Print(_L(">>>CDpsUsbNotifier::ConstructL")));
       
    89     User::LeaveIfError(iUsbM.Connect());
       
    90     User::LeaveIfError(iUsbW.Connect());
       
    91     iPtpP = CDpsPtpNotifier::NewL(this);
       
    92     iConnectP = CDpsConnectNotifier::NewL(this);
       
    93     IF_DEBUG(Print(_L("<<<CDpsUsbNotifier::ConstructL")));
       
    94     }    
       
    95 
       
    96 // ---------------------------------------------------------------------------
       
    97 // 
       
    98 // ---------------------------------------------------------------------------
       
    99 //
       
   100 void CDpsUsbNotifier::WaitForPrinterNotify()
       
   101     {
       
   102     IF_DEBUG(Print(_L("CDpsUsbNotifier::WaitForPrinterNotify")));
       
   103     iPtpP->ChangePtpPersonality();
       
   104     }
       
   105   
       
   106 // ---------------------------------------------------------------------------
       
   107 // 
       
   108 // ---------------------------------------------------------------------------
       
   109 //  
       
   110 void CDpsUsbNotifier::CancelPrinterNotify()
       
   111     {
       
   112     IF_DEBUG(Print(_L(">>>CDpsUsbNotifier::CancelPrinterNotify")));
       
   113     if (CDpsEngine::ENotConnected == iConnection)
       
   114         {
       
   115         iPtpP->Cancel(); 
       
   116         iConfigured = EFalse;    
       
   117         }
       
   118     else if (CDpsEngine::EPrinterConnected == iConnection)
       
   119         {
       
   120         iConnectP->Cancel();
       
   121         }
       
   122     // if the request is replied through RunL before the cancel
       
   123     // iPrinterConnectRequest will be NULL and we don't need to cancel anything
       
   124     if (iEngine->PrinterConnectRequest())
       
   125         {
       
   126 	    User::RequestComplete(iEngine->PrinterConnectRequest(), KErrCancel);   
       
   127         }    
       
   128     IF_DEBUG(Print(_L("<<<CDpsUsbNotifier::CancelWaitForPrinterNotify")));
       
   129     }
       
   130 
       
   131 // ---------------------------------------------------------------------------
       
   132 // 
       
   133 // ---------------------------------------------------------------------------
       
   134 //
       
   135 void CDpsUsbNotifier::ConnectNotify()
       
   136     {
       
   137     IF_DEBUG(Print(_L(">>>CDpsUsbNotifier::ConnectNotify")));
       
   138     iConnectP->ConnectNotify();
       
   139     IF_DEBUG(Print(_L("<<<CDpsUsbNotifier::ConnectNotify")));
       
   140     }
       
   141 
       
   142 // ---------------------------------------------------------------------------
       
   143 // 
       
   144 // ---------------------------------------------------------------------------
       
   145 //	
       
   146 void CDpsUsbNotifier::Rollback()
       
   147     {
       
   148     IF_DEBUG(Print(_L(">>>CDpsUsbNotifier::Rollback")));
       
   149     // only when the personality has changed, we switch back to the previous
       
   150     // personality
       
   151     if (iPersonality)
       
   152         {
       
   153         TInt personalityId = KUsbPersonalityIdMTP;
       
   154         iUsbM.GetCurrentPersonalityId(personalityId);
       
   155         IF_DEBUG(Print(_L("CDpsUsbNotifier::Rollback, current personality= %d"), personalityId));
       
   156         if(KUsbPersonalityIdPCSuiteMTP != personalityId)
       
   157             {
       
   158             if (!iConfigured || iRollback)
       
   159                 {
       
   160                 iUsbW.SetPreviousPersonality();
       
   161                 }
       
   162             else
       
   163                 {
       
   164                 iUsbW.SetPreviousPersonalityOnDisconnect();
       
   165                 }
       
   166             }
       
   167         }
       
   168     IF_DEBUG(Print(_L("<<<CDpsUsbNotifier::Rollback")));
       
   169     }
       
   170 
       
   171 // ---------------------------------------------------------------------------
       
   172 // 
       
   173 // ---------------------------------------------------------------------------
       
   174 //
       
   175 void CDpsUsbNotifier::PtpNotify(TInt aErr)
       
   176     {
       
   177     IF_DEBUG(Print(_L(">>>CDpsUsbNotifier::PtpNotify %x %d"), 
       
   178         iConnectState, aErr));
       
   179     if (aErr == KErrNone)
       
   180         {
       
   181         // personality changed to MTP, but cable is not connected        
       
   182         if (iConnectState != EUsbDeviceStateUndefined)
       
   183             {
       
   184             if (!IsActive())
       
   185                 {
       
   186                 iEngine->Ptp().IsDpsPrinter(iStatus);
       
   187                 SetActive();    
       
   188                 }
       
   189             }
       
   190         else
       
   191             {
       
   192             iConnection = CDpsEngine::ENotConnected;
       
   193             User::RequestComplete(iEngine->PrinterConnectRequest(), iConnection);
       
   194             }    
       
   195         }
       
   196     else
       
   197         {
       
   198         iConnection = CDpsEngine::EWrongPrintModeConnected;
       
   199         User::RequestComplete(iEngine->PrinterConnectRequest(), iConnection); 
       
   200         }
       
   201         	
       
   202     IF_DEBUG(Print(_L("<<<CDpsUsbNotifier::PtpNotify")));        
       
   203     }
       
   204 
       
   205 // ---------------------------------------------------------------------------
       
   206 // 
       
   207 // ---------------------------------------------------------------------------
       
   208 //    
       
   209 void CDpsUsbNotifier::PersonalityChanged()
       
   210     {
       
   211     IF_DEBUG(Print(_L(">>>CDpsUsbNotifier::PersonalityChanged %x"), 
       
   212                                                               iPersonality));     
       
   213     if (iPersonality != KUsbPersonalityIdMTP)
       
   214         {
       
   215         iConnection = CDpsEngine::EWrongPrintModeConnected;
       
   216         iConfigured = EFalse;
       
   217         if (iEngine->PrinterConnectRequest())
       
   218             {
       
   219             User::RequestComplete(iEngine->PrinterConnectRequest(), 
       
   220                 iConnection);     
       
   221             }
       
   222         }
       
   223     // when UI gets this notification, it must quit. As the result, the dps 
       
   224     // engine will be deleted so we do not need to care the further change.
       
   225     
       
   226     IF_DEBUG(Print(_L(">>>CDpsUsbNotifier::PersonalityChanged ")));     
       
   227     }
       
   228 
       
   229 // ---------------------------------------------------------------------------
       
   230 // 
       
   231 // ---------------------------------------------------------------------------
       
   232 //    
       
   233 void CDpsUsbNotifier::RunL()
       
   234     {
       
   235     IF_DEBUG(Print(_L(">>>CDpsUsbNotifier::RunL")));
       
   236     
       
   237     if (EPrinterAvailable == iStatus.Int())
       
   238         {
       
   239         iConnection = CDpsEngine::EPrinterConnected;
       
   240         iConfigured = ETrue;	
       
   241         iEngine->SetDpsFolder(iEngine->Ptp().PtpFolder());			            
       
   242         }
       
   243     else if (iStatus.Int() != KErrCancel)
       
   244         {
       
   245         iConnection = CDpsEngine::EOtherConnected;
       
   246         }
       
   247     User::RequestComplete(iEngine->PrinterConnectRequest(), iConnection);    
       
   248         
       
   249     IF_DEBUG(Print(_L("<<<CDpsUsbNotifier::RunL")));
       
   250     }
       
   251     
       
   252 // ---------------------------------------------------------------------------
       
   253 // 
       
   254 // ---------------------------------------------------------------------------
       
   255 //       
       
   256 void CDpsUsbNotifier::DoCancel()
       
   257     {
       
   258     IF_DEBUG(Print(_L(">>>CDpsUsbNotifier::DoCancel")));
       
   259     iEngine->Ptp().CancelIsDpsPrinter();
       
   260     IF_DEBUG(Print(_L(">>>CDpsUsbNotifier::DoCancel")));
       
   261     }
       
   262     
       
   263 // ---------------------------------------------------------------------------
       
   264 // 
       
   265 // ---------------------------------------------------------------------------
       
   266 //       
       
   267 TInt CDpsUsbNotifier::RunError(TInt aErr)
       
   268     {
       
   269     IF_DEBUG(Print(_L("CDpsUsbNotifier::RunError is %d"), aErr));
       
   270     return aErr;
       
   271     }
       
   272     
       
   273 // ---------------------------------------------------------------------------
       
   274 // 
       
   275 // ---------------------------------------------------------------------------
       
   276 //    
       
   277 void CDpsUsbNotifier::DisconnectNotify(TUsbDeviceState aState)
       
   278     {
       
   279     IF_DEBUG(Print(_L(">>>CDpsUsbNotifier::DisconnectNotify %d"), aState));
       
   280     if (iConfigured)
       
   281         {
       
   282         iConnection = CDpsEngine::EPrinterDisconnected;
       
   283         }
       
   284     else
       
   285         {
       
   286         iConnection = CDpsEngine::ENotConnected;
       
   287         }
       
   288     iConfigured = EFalse;
       
   289     if (EUsbDeviceStateUndefined == aState)
       
   290         {
       
   291         iRollback = ETrue;
       
   292         }
       
   293     if (iEngine->PrinterConnectRequest())
       
   294         {
       
   295         User::RequestComplete(iEngine->PrinterConnectRequest(), iConnection);    
       
   296         }
       
   297     
       
   298     IF_DEBUG(Print(_L("<<<CDpsUsbNotifier::DisconnectNotify")));
       
   299     }
       
   300 
       
   301 // ---------------------------------------------------------------------------
       
   302 // 
       
   303 // ---------------------------------------------------------------------------
       
   304 //
       
   305 TInt CDpsUsbNotifier::ConnectState()
       
   306     {
       
   307     IF_DEBUG(Print(_L(">>>CDpsUsbNotifier::ConnectState")));    
       
   308     TInt ret = iUsbM.GetDeviceState(iConnectState);
       
   309 	IF_DEBUG(Print(_L("<<<CDpsUsbNotifier::ConnectState %x"), iConnectState));    
       
   310 	return ret;    
       
   311     }
       
   312 
       
   313 // ---------------------------------------------------------------------------
       
   314 // 
       
   315 // ---------------------------------------------------------------------------
       
   316 //
       
   317 TBool CDpsUsbNotifier::IsSetPrintModeIssued()
       
   318     {
       
   319     return (iPersonality != KUnknownPersonality);
       
   320     }
       
   321 
       
   322 // ---------------------------------------------------------------------------
       
   323 // 
       
   324 // ---------------------------------------------------------------------------
       
   325 //
       
   326 TBool CDpsUsbNotifier::IsConfigured() const
       
   327     {
       
   328     return iConfigured;
       
   329     }