mtpdataproviders/mtppictbridgedp/src/cptpsession.cpp
changeset 0 d0791faffa3f
equal deleted inserted replaced
-1:000000000000 0:d0791faffa3f
       
     1 // Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 
       
    17 #include <mtp/mmtpdataproviderframework.h>
       
    18 #include <f32file.h>
       
    19 #include "cptpsession.h"
       
    20 #include "cptpserver.h"
       
    21 #include "cptpreceivedmsghandler.h"
       
    22 #include "cmtppictbridgeprinter.h"
       
    23 #include "cptptimer.h"
       
    24 #include "mtppictbridgedpconst.h"
       
    25 #include "ptpdef.h" 
       
    26 
       
    27 // --------------------------------------------------------------------------
       
    28 // 
       
    29 // 2-phased constructor.
       
    30 // --------------------------------------------------------------------------
       
    31 //
       
    32 CPtpSession* CPtpSession::NewL(CPtpServer* aServer)
       
    33     {
       
    34     CPtpSession* self= new (ELeave) CPtpSession(aServer);
       
    35     CleanupStack::PushL(self);
       
    36     self->ConstructL();
       
    37     CleanupStack::Pop(self);
       
    38     return self;
       
    39     }
       
    40 
       
    41 // --------------------------------------------------------------------------
       
    42 // 
       
    43 // C++ constructor.
       
    44 // --------------------------------------------------------------------------
       
    45 //
       
    46 CPtpSession::CPtpSession(CPtpServer* aServer) : iServerP(aServer) 
       
    47     {
       
    48     iServerP->Printer()->RegisterObserver(this); // since PTP register service 
       
    49                   // is deprecated we register the observer at session creation
       
    50     iServerP->IncrementSessionCount();                  
       
    51     }
       
    52 
       
    53 // --------------------------------------------------------------------------
       
    54 // 
       
    55 // --------------------------------------------------------------------------
       
    56 //
       
    57 void CPtpSession::ConstructL()
       
    58     {
       
    59     __FLOG_OPEN(KMTPSubsystem, KPtpServerLog);
       
    60     iTimerP=CPtpTimer::NewL(*this);
       
    61     }
       
    62 // --------------------------------------------------------------------------
       
    63 // 
       
    64 // C++ destructor.
       
    65 // --------------------------------------------------------------------------
       
    66 //
       
    67 CPtpSession::~CPtpSession()
       
    68     {
       
    69     __FLOG(_L8(">>>CPtpSession::~"));
       
    70     delete iTimerP;
       
    71     CancelOutstandingRequest();
       
    72     TRAP_IGNORE(CleanupL()); // there is not much we can do at this phase if the removal fails, so just ignore
       
    73     if(iServerP->NumSession())
       
    74         {
       
    75         iServerP->DecrementSessionCount();
       
    76         }
       
    77     __FLOG(_L8("<<<CPtpSession::~"));
       
    78     __FLOG_CLOSE;
       
    79     }
       
    80 
       
    81 // --------------------------------------------------------------------------
       
    82 // 
       
    83 // From CSession2, passes the request forward to DispatchMessageL.
       
    84 // --------------------------------------------------------------------------
       
    85 //
       
    86 void CPtpSession::ServiceL( const RMessage2& aMessage )
       
    87     {
       
    88     __FLOG(_L8(">>>CPtpSession::ServiceL"));
       
    89     DispatchMessageL(aMessage);
       
    90     __FLOG(_L8("<<<CPtpSession::ServiceL"));
       
    91     }
       
    92 
       
    93 // --------------------------------------------------------------------------
       
    94 //  Cleans up the previously received DPS file, since the files are used only 
       
    95 //  for communication
       
    96 // --------------------------------------------------------------------------
       
    97 //
       
    98 void CPtpSession::CleanupL()
       
    99     {
       
   100     __FLOG(_L8(">>>CPtpSession::Cleanup"));
       
   101     if(iReceivedFile.Size())
       
   102         {
       
   103         __FLOG_VA((_L16("   deleting file %S"), &iReceivedFile));
       
   104         User::LeaveIfError(iServerP->Framework().Fs().Delete(iReceivedFile));
       
   105         __FLOG(_L8("   removing from DB"));
       
   106         iServerP->RemoveObjectL(iReceivedFile);
       
   107         iReceivedFile.Zero();
       
   108         }
       
   109     __FLOG(_L8("<<<CPtpSession::Cleanup"));
       
   110     }
       
   111 
       
   112 // --------------------------------------------------------------------------
       
   113 // Handles the request from client.
       
   114 // --------------------------------------------------------------------------
       
   115 //
       
   116 void CPtpSession::DispatchMessageL( const RMessage2& aMessage )
       
   117     {
       
   118     __FLOG_VA((_L8(">>>CPtpSession::DispatchMessageL %d"), aMessage.Function()));
       
   119     TInt ret = KErrNone;
       
   120     TBool complete = ETrue;        
       
   121     CleanupL(); // calling this here assumes that the client never makes a new call 
       
   122                 // before it has handled the received DPS message
       
   123     switch( aMessage.Function() )
       
   124         {        
       
   125         case EIsDpsPrinter:
       
   126             ret = IsDpsPrinter(aMessage, complete);
       
   127             break;
       
   128 
       
   129         case ECancelIsDpsPrinter:
       
   130             CancelIsDpsPrinter();
       
   131             break;
       
   132 
       
   133         case EGetObjectHandleByName:  
       
   134             GetObjectHandleByNameL(aMessage);
       
   135             break;
       
   136 
       
   137         case EGetNameByObjectHandle:
       
   138             GetNameByObjectHandleL(aMessage);
       
   139             break;
       
   140 
       
   141         case ESendObject:
       
   142             ret = SendObject(aMessage, complete);
       
   143             break;
       
   144 
       
   145         case ECancelSendObject:
       
   146             CancelSendObject();
       
   147             break;
       
   148 
       
   149         case EObjectReceivedNotify:
       
   150             ret = ObjectReceivedNotify(aMessage, complete);
       
   151             break;
       
   152 
       
   153         case ECancelObjectReceivedNotify:
       
   154             CancelObjectReceivedNotify();
       
   155             break;
       
   156 
       
   157         case EPtpFolder:
       
   158             ret = PtpFolder(aMessage);
       
   159             break;
       
   160 
       
   161         default:
       
   162             __FLOG(_L8("!!!Error: ---Wrong param from client!!!"));
       
   163             aMessage.Panic(KPTPClientPanicCategory, EBadRequest);
       
   164             break;
       
   165         }
       
   166         
       
   167     if (complete)
       
   168         {
       
   169         aMessage.Complete(ret);
       
   170         }
       
   171     __FLOG_VA((_L8("<<<PtpSession::DispatchMessageL ret=%d"), ret));
       
   172     }
       
   173 
       
   174 // --------------------------------------------------------------------------
       
   175 // CPtpSession::CancelIsDpsPrinter()
       
   176 // Cancels Asynchronous request IsDpsPrinter
       
   177 // --------------------------------------------------------------------------
       
   178 //
       
   179 void CPtpSession::CancelIsDpsPrinter()
       
   180     {
       
   181     __FLOG(_L8(">>>CPtpSession::CancelIsDpsPrinter"));
       
   182     if (iDpsPrinterMsg.Handle())
       
   183         {
       
   184         iDpsPrinterMsg.Complete(KErrCancel);
       
   185         iServerP->Printer()->DeRegisterDpsPrinterNotify(this);
       
   186         iTimerP->Cancel();
       
   187         iServerP->CancelNotifyOnMtpSessionOpen(this);
       
   188         } 
       
   189     __FLOG(_L8("<<<CPtpSession::CancelIsDpsPrinter"));
       
   190     }
       
   191     
       
   192 // --------------------------------------------------------------------------
       
   193 // CPtpSession::CancelSendObject()
       
   194 // Cancel Asynchronous request send Object
       
   195 // --------------------------------------------------------------------------
       
   196 //
       
   197 void CPtpSession::CancelSendObject()
       
   198     {
       
   199     __FLOG(_L8(">>>CancelSendObject"));
       
   200     if (iSendObjectMsg.Handle())
       
   201         {
       
   202         iServerP->Printer()->CancelSendDpsFile();
       
   203         iSendObjectMsg.Complete(KErrCancel);
       
   204         iTimerP->Cancel();
       
   205         }
       
   206     __FLOG(_L8("<<<CancelSendObject"));    
       
   207     }
       
   208     
       
   209 // --------------------------------------------------------------------------
       
   210 // CPtpSession::CancelObjectReceivedNotify()
       
   211 // Deregisters for Object received notification
       
   212 // --------------------------------------------------------------------------
       
   213 //
       
   214 void CPtpSession::CancelObjectReceivedNotify()
       
   215     {
       
   216     __FLOG(_L8(">>>CancelObjectReceivedNotify"));       
       
   217     if (iObjectReceivedNotifyMsg.Handle())
       
   218         {
       
   219         __FLOG_VA((_L8("the handle is 0x%x"), iObjectReceivedNotifyMsg.Handle()));
       
   220         iServerP->Printer()->MsgHandlerP()->DeRegisterReceiveObjectNotify();
       
   221         iObjectReceivedNotifyMsg.Complete(KErrCancel);                    
       
   222         }
       
   223     __FLOG(_L8("<<<CancelObjectReceivedNotifiy"));
       
   224     }
       
   225     
       
   226 // --------------------------------------------------------------------------
       
   227 // CPtpSession::IsDpsPrinter()
       
   228 // --------------------------------------------------------------------------
       
   229 //    
       
   230 TInt CPtpSession::IsDpsPrinter(const RMessage2& aMessage, TBool& aComplete)
       
   231     {
       
   232     __FLOG(_L8(">>>IsDpsPrinter"));
       
   233     TInt ret=EPrinterNotAvailable;
       
   234     if (!iDpsPrinterMsg.Handle()) // not already pending
       
   235         {
       
   236         switch (iServerP->Printer()->Status())
       
   237             {   
       
   238             case CMTPPictBridgePrinter::ENotConnected:
       
   239                 iDpsPrinterMsg = aMessage;    
       
   240                 iServerP->Printer()->RegisterDpsPrinterNotify(this);
       
   241                 aComplete = EFalse;
       
   242                 if(iServerP->MtpSessionOpen())
       
   243                     {
       
   244                     if (!iTimerP->IsActive()) 
       
   245                         {
       
   246                         iTimerP->After(KDiscoveryTime);
       
   247                         }
       
   248                     }
       
   249                 else
       
   250                     {
       
   251                     iServerP->NotifyOnMtpSessionOpen(this);
       
   252                     }                    
       
   253                 // we do not set ret since the  value does not really matter, we will be waiting for the discovery to complete
       
   254                 __FLOG(_L8(" waiting"));
       
   255                 break;
       
   256                 
       
   257             case CMTPPictBridgePrinter::EConnected:
       
   258                 ret=EPrinterAvailable;
       
   259                 aComplete = ETrue;
       
   260                 __FLOG(_L8(" connected"));
       
   261                 break;
       
   262 
       
   263             case CMTPPictBridgePrinter::ENotPrinter:
       
   264                 ret=EPrinterNotAvailable;
       
   265                 aComplete = ETrue;
       
   266                 __FLOG(_L8(" not connected"));
       
   267                 break;
       
   268 
       
   269             default:
       
   270                 break;                
       
   271             }
       
   272         }
       
   273     else
       
   274         {
       
   275         __FLOG(_L8("!!!Error: client message error, duplicated IsDpsPrinter"));                        
       
   276         aMessage.Panic(KPTPClientPanicCategory, ERequestPending);
       
   277         aComplete = EFalse;
       
   278         }
       
   279     __FLOG(_L8("<<<IsDpsPrinter"));
       
   280     return ret;
       
   281     }
       
   282 
       
   283 // --------------------------------------------------------------------------
       
   284 // start the timer for printer detection, since we have now session open and 
       
   285 // we are ready to communicate wioth the host
       
   286 // --------------------------------------------------------------------------
       
   287 void CPtpSession::MTPSessionOpened()
       
   288     {
       
   289     __FLOG(_L8(">>>CPtpSession::MTPSessionOpened"));
       
   290     if (!iTimerP->IsActive() && iDpsPrinterMsg.Handle()) 
       
   291         {
       
   292         __FLOG(_L8("   CPtpSession::MTPSessionOpened timer started"));
       
   293         iTimerP->After(KDiscoveryTime);
       
   294         }        
       
   295     __FLOG(_L8("<<<CPtpSession::MTPSessionOpened"));
       
   296     }
       
   297     
       
   298 // --------------------------------------------------------------------------
       
   299 // CPtpSession::GetObjectHandleByNameL()
       
   300 // 
       
   301 // --------------------------------------------------------------------------
       
   302 //
       
   303 void CPtpSession::GetObjectHandleByNameL(const RMessage2& aMessage)
       
   304     {
       
   305     __FLOG(_L8(">>>CPtpSession::GetObjectHandleByNameL"));
       
   306     TFileName file;
       
   307     User::LeaveIfError(aMessage.Read(0, file));
       
   308     __FLOG_VA((_L16("--the file is %S"), &file));
       
   309     TUint32 handle=0;
       
   310     TRAP_IGNORE(iServerP->GetObjectHandleByNameL(file, handle));
       
   311     TPckgBuf<TUint32> handlePckg(handle);
       
   312     aMessage.WriteL(1, handlePckg);     
       
   313     __FLOG_VA((_L16("<<<CPtpSession::GetObjectHandleByNameL handle=%d"), handle));
       
   314     }
       
   315     
       
   316 // --------------------------------------------------------------------------
       
   317 // CPtpSession::GetNameByObjectHandle()
       
   318 
       
   319 // --------------------------------------------------------------------------
       
   320 //
       
   321 void CPtpSession::GetNameByObjectHandleL(const RMessage2& aMessage)
       
   322     {
       
   323     __FLOG(_L8(">>>CPtpSession::GetNameByObjectHandle"));               
       
   324     TUint32 handle = 0;
       
   325     TPckgBuf<TUint32> pckgHandle(handle);
       
   326     User::LeaveIfError(aMessage.Read(1, pckgHandle));
       
   327     TFileName file; 
       
   328     handle = pckgHandle();
       
   329     __FLOG_VA((_L8("---handle is %x"), handle));
       
   330     TRAP_IGNORE(iServerP->GetObjectNameByHandleL(file, handle));
       
   331     __FLOG_VA((_L16("the file is %S"), &file));
       
   332     aMessage.WriteL(0, file);
       
   333     
       
   334     __FLOG(_L8("<<<CPtpSession::GetNameByObjectHandle"));               
       
   335     }
       
   336               
       
   337 // --------------------------------------------------------------------------
       
   338 // CPtpSession::SendObject()
       
   339 // Asynch. request send Object
       
   340 // --------------------------------------------------------------------------
       
   341 //
       
   342 TInt CPtpSession::SendObject(const RMessage2& aMessage, TBool& aComplete)
       
   343     {
       
   344     __FLOG(_L8(">>>CPtpSession::SendObject"));                      
       
   345     TInt err(KErrNone);
       
   346     
       
   347     if (iSendObjectMsg.Handle())
       
   348         {
       
   349         __FLOG(_L8("!!!!Error: client message error, duplicated SendObject"));
       
   350         aMessage.Panic(KPTPClientPanicCategory, ERequestPending);
       
   351         aComplete = EFalse;
       
   352         return KErrNone;
       
   353         }
       
   354     else
       
   355         {
       
   356         // Parameter add is depracated. We do not send Object added and we do not keep ther DPS object permanently in
       
   357         // our system.
       
   358         //
       
   359         // Sending ObjectAdded Event is not mandatory ( See Appendix B page 78. DPS Usage of USB and PTP in CIPA DC-001-2003)
       
   360 
       
   361         TBool timeout = aMessage.Int2();    
       
   362         __FLOG_VA((_L8("---timeout is %d"), timeout));    
       
   363         TFileName file; 
       
   364         err = aMessage.Read(0, file);
       
   365         if (err == KErrNone)
       
   366             {
       
   367             __FLOG_VA((_L16("---the file is %S"), &file));
       
   368             TInt size = aMessage.Int3();
       
   369             __FLOG_VA((_L8("---the file size is %d"), size)); // size is deprecated and not used anymore
       
   370             TRAP(err, iServerP->Printer()->SendDpsFileL(file, timeout, size));
       
   371             if (err == KErrNone)
       
   372                 {
       
   373                 iSendObjectMsg = aMessage;
       
   374                 aComplete = EFalse;    
       
   375                 }
       
   376             }
       
   377         if ((EFalse != timeout) && !iTimerP->IsActive())
       
   378             {
       
   379             iTimerP->After(KSendTimeout);
       
   380             }
       
   381         __FLOG_VA((_L8("<<<CPtpSession::SendObject err=%d"), err));
       
   382         return err;    
       
   383         }    
       
   384     }             
       
   385 
       
   386 // --------------------------------------------------------------------------
       
   387 // CPtpSession::ObjectReceivedNotify()
       
   388 // 
       
   389 // --------------------------------------------------------------------------
       
   390 //   
       
   391 TInt CPtpSession::ObjectReceivedNotify(const RMessage2& aMessage, 
       
   392                                        TBool& aComplete)
       
   393     {
       
   394     __FLOG(_L8(">>>CPtpSession::ObjectReceivedNotify"));                        
       
   395     if (iObjectReceivedNotifyMsg.Handle())
       
   396         {
       
   397         __FLOG(_L8("!!!!Error: client message error, duplicated ObjectReceivedNotify"));
       
   398         aMessage.Panic(KPTPClientPanicCategory, ERequestPending);
       
   399         aComplete = EFalse;
       
   400         return KErrNone;
       
   401         }
       
   402     else
       
   403         {
       
   404         //TBool del = aMessage.Int2();
       
   405         //__FLOG_VA((_L8("---the del is %d"), del));    
       
   406 
       
   407         TBuf<KFileExtLength> ext; 
       
   408         TInt err = aMessage.Read(0, ext);
       
   409         if (err == KErrNone)
       
   410             {
       
   411             __FLOG_VA((_L16("the extension is %S"), &ext));
       
   412             
       
   413             iObjectReceivedNotifyMsg = aMessage; 
       
   414             aComplete = EFalse;
       
   415             iServerP->Printer()->MsgHandlerP()->RegisterReceiveObjectNotify(ext);
       
   416             }
       
   417         __FLOG(_L8("<<<CPtpSession::ObjectReceivedNotify"));                            
       
   418         return err;
       
   419         }
       
   420     }
       
   421     
       
   422 // --------------------------------------------------------------------------
       
   423 // CPtpSession::PtpFolder()
       
   424 // Returns PtpFolder Name and Path
       
   425 // --------------------------------------------------------------------------
       
   426 //    
       
   427 TInt CPtpSession::PtpFolder(const RMessage2& aMessage)
       
   428     {
       
   429     __FLOG(_L8(">>>CPtpSession::PtpFolder"));
       
   430     TInt err(KErrNotReady);
       
   431     TFileName folder = iServerP->PtpFolder();
       
   432     err = aMessage.Write(0,folder);
       
   433     __FLOG_VA((_L16("<<<CPtpSession::PtpFolder %S err(%d)"), &folder, err));
       
   434     return err;
       
   435     }
       
   436     
       
   437 // --------------------------------------------------------------------------
       
   438 // CPtpSession::SendObjectCompleted()
       
   439 // 
       
   440 // --------------------------------------------------------------------------
       
   441 //    
       
   442 void CPtpSession::SendObjectCompleted(TInt aStatus)
       
   443     {
       
   444     __FLOG_VA((_L16(">>>CPtpSession::SendObjectCompleted status(%d)"), aStatus));
       
   445     if (iSendObjectMsg.Handle())
       
   446         {
       
   447         iSendObjectMsg.Complete(aStatus);    
       
   448         iTimerP->Cancel();
       
   449         }
       
   450     else
       
   451         {
       
   452         __FLOG(_L8("!!!Warning: CPtpSession::SendObjectCompleted: UNEXPECTED CALL"));
       
   453         }
       
   454     __FLOG(_L8("<<<CPtpSession::SendObjectCompleted")); 
       
   455     }
       
   456 
       
   457 // --------------------------------------------------------------------------
       
   458 // CPtpSession::IsDpsPrinterCompleted()
       
   459 // 
       
   460 // --------------------------------------------------------------------------
       
   461 //
       
   462 void CPtpSession::IsDpsPrinterCompleted(TDpsPrinterState aState)
       
   463     {
       
   464     __FLOG(_L8(">>>CPtpSession::IsDpsPrinterCompleted"));    
       
   465     if (iDpsPrinterMsg.Handle())
       
   466         {
       
   467         iDpsPrinterMsg.Complete(aState);
       
   468         iTimerP->Cancel();
       
   469         iServerP->Printer()->DeRegisterDpsPrinterNotify(this);
       
   470         }
       
   471     else
       
   472         {
       
   473         __FLOG(_L8("!!!Warning: CPtpSession::IsDpsPrinterCompleted: UNEXPECTED CALL"));
       
   474         }
       
   475     __FLOG(_L8("<<<CPtpSession::IsDpsPrinterCompleted"));    
       
   476     }
       
   477 
       
   478 // --------------------------------------------------------------------------
       
   479 // CPtpSession::ReceivedObjectCompleted()
       
   480 // 
       
   481 // --------------------------------------------------------------------------
       
   482 //
       
   483 void CPtpSession::ReceivedObjectCompleted(TDes& aFile)
       
   484     {
       
   485     __FLOG(_L8(">>>CPtpSession::ReceivedObjectCompleted"));
       
   486     if (iObjectReceivedNotifyMsg.Handle())
       
   487         {
       
   488         TInt err = iObjectReceivedNotifyMsg.Write(1, aFile);
       
   489         iReceivedFile.Copy(aFile);
       
   490         __FLOG_VA((_L8("***CPtpSession::ReceivedObjectCompleted err=%d"), err));
       
   491         iObjectReceivedNotifyMsg.Complete(err);
       
   492         }
       
   493     else
       
   494         {
       
   495         __FLOG(_L8("!!!Warning: Strange Happened!!!"));    
       
   496         }
       
   497     __FLOG(_L8("<<<CPtpSession::ReceivedObjectCompleted"));
       
   498     }
       
   499 
       
   500 // --------------------------------------------------------------------------
       
   501 // 
       
   502 // Cancels outstanding request
       
   503 // --------------------------------------------------------------------------
       
   504 //
       
   505 void CPtpSession::CancelOutstandingRequest()
       
   506     {
       
   507     __FLOG(_L8(">>>CPtpSession::CancelOutstandingRequest"));
       
   508     if (iSendObjectMsg.Handle())
       
   509         {
       
   510         iSendObjectMsg.Complete(KErrCancel);
       
   511         }
       
   512     if (iObjectReceivedNotifyMsg.Handle())
       
   513         {
       
   514         iObjectReceivedNotifyMsg.Complete(KErrCancel);
       
   515         }
       
   516     if (iDpsPrinterMsg.Handle())
       
   517         {
       
   518         iDpsPrinterMsg.Complete(KErrCancel);
       
   519         }
       
   520     __FLOG(_L8("<<<CPtpSession::CancelOutstandingRequest"));    
       
   521     }
       
   522 // --------------------------------------------------------------------------
       
   523 // 
       
   524 // --------------------------------------------------------------------------
       
   525 //
       
   526 CPtpServer* CPtpSession::ServerP() const
       
   527     {
       
   528     return iServerP;    
       
   529     }
       
   530