ncdengine/debuglogger/obex/src/objectexchangeclient.cpp
changeset 0 ba25891c3a9e
child 25 7333d7932ef7
equal deleted inserted replaced
-1:000000000000 0:ba25891c3a9e
       
     1 /*
       
     2 * Copyright (c) 2006 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:  
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 /** Add TRAP harnesses for leaving functions such as iServiceSearcher->ConnectL 
       
    20 Decide what to do with Copyrighted Nokia code */
       
    21 
       
    22 
       
    23 #include "ObjectExchangeClient.h"
       
    24 #include "ObjectExchangeServiceSearcher.h"
       
    25 #include "BTObjectExchange.pan"
       
    26 
       
    27 #include <e32svr.h>
       
    28 
       
    29 // Define DEBUG trace for debug builds
       
    30 #ifdef _DEBUG
       
    31 #define TRACE(a) RDebug::Print a
       
    32 #else
       
    33 #define TRACE(a)
       
    34 #endif
       
    35 
       
    36 CObjectExchangeClient* CObjectExchangeClient::NewL()
       
    37 {
       
    38     CObjectExchangeClient* self = NewLC();
       
    39     CleanupStack::Pop(self);
       
    40     return self;
       
    41 }
       
    42 
       
    43 CObjectExchangeClient* CObjectExchangeClient::NewLC()
       
    44 {
       
    45     CObjectExchangeClient* self = new (ELeave) CObjectExchangeClient();
       
    46     CleanupStack::PushL(self);
       
    47     self->ConstructL();
       
    48     return self;
       
    49 }
       
    50 
       
    51 CObjectExchangeClient::CObjectExchangeClient()
       
    52 : CActive(CActive::EPriorityStandard),
       
    53 iState(EWaitingToGetDevice)  // Go to initial state and wait for ConnectL function call
       
    54 {
       
    55     CActiveScheduler::Add(this);
       
    56 }
       
    57 
       
    58 void CObjectExchangeClient::ConstructL()
       
    59 {
       
    60     iServiceSearcher = CObjectExchangeServiceSearcher::NewL(); 
       
    61 }
       
    62 
       
    63 
       
    64 CObjectExchangeClient::~CObjectExchangeClient()
       
    65 {
       
    66     Cancel();           // Cancel any outstanding request on this object
       
    67     
       
    68     delete iCurrObject;
       
    69     iCurrObject = NULL;
       
    70     
       
    71     delete iServiceSearcher; // iServiceSearcher's destructor completes any outstanding request
       
    72     iServiceSearcher = NULL;
       
    73     
       
    74     delete iClient;
       
    75     iClient = NULL;
       
    76 
       
    77 }
       
    78 
       
    79 void CObjectExchangeClient::DoCancel()
       
    80 {
       
    81 
       
    82     // Add Cancelling for outstanding request caused by 
       
    83     // iServiceSearcher->SelectDeviceByDiscoveryL(iStatus)
       
    84     // and iServiceSearcher->FindServiceL(iStatus);
       
    85     // NOTE: works now if the iServiceSearcher is deleted, but not if cancelled
       
    86     // SOLUTION: delete and recreate iServiceSearcher in DoCancel? Or implement
       
    87     // Cancel to iServiceSearcer!
       
    88 
       
    89     if (iState != EWaitingToGetDevice && iClient)
       
    90     {
       
    91         iClient->Abort();
       
    92 
       
    93 	
       
    94 	delete iClient; // must be deleted so that connection is not left open
       
    95 					// if sender cancels before receiver accepts connection
       
    96 	iClient = NULL;
       
    97 		
       
    98     }
       
    99 
       
   100 
       
   101 	iServiceSearcher->Cancel();
       
   102 
       
   103 	iStatus = KErrNone;  // At least emulated version does not work without this    
       
   104 
       
   105 	iState = EWaitingToGetDevice;
       
   106 	User::RequestComplete( iObserverStatus, KErrCancel );
       
   107 }
       
   108 
       
   109 
       
   110 void CObjectExchangeClient::RunL()
       
   111 {   
       
   112    
       
   113     if (iStatus != KErrNone) 
       
   114     {
       
   115         switch (iState) 
       
   116         {
       
   117         case EGettingDevice: 
       
   118         case EGettingService:
       
   119         case EGettingConnection:
       
   120         case EDisconnecting: 
       
   121         case EWaitingToSend:    
       
   122             User::RequestComplete(iObserverStatus, iStatus.Int()); // Relay error codes
       
   123             iState = EWaitingToGetDevice; // Return always to initial state
       
   124             break;
       
   125        
       
   126         default:            
       
   127             Panic(EBTObjectExchangeUnexpectedLogicState); 
       
   128             break;
       
   129         }
       
   130     }    
       
   131     else 
       
   132     {
       
   133         switch (iState)
       
   134         {   
       
   135         case EGettingDevice:                                 
       
   136 
       
   137             iState = EGettingService;   
       
   138             iStatus = KRequestPending;  // NOTE: Setting this should be the responsibility 
       
   139                                         // of the called function
       
   140                                         // move to CBTServiceSearcher::FindServiceL?
       
   141 
       
   142             iServiceSearcher->FindServiceL(iStatus); // Find the OBEX service on the target device
       
   143             SetActive();
       
   144             break;
       
   145             
       
   146         case EGettingService:
       
   147             iState = EGettingConnection;    
       
   148             ConnectToServerL();         // Connect to the OBEX service port on the target device
       
   149             break;
       
   150             
       
   151         case EGettingConnection:
       
   152             iState = EWaitingToSend;               
       
   153             User::RequestComplete(iObserverStatus, KErrNone);  // ConnectL was succesfull                          
       
   154             break;
       
   155             
       
   156         case EWaitingToSend:                 
       
   157             User::RequestComplete(iObserverStatus, KErrNone); // SendObjectL was successfull
       
   158             break;
       
   159             
       
   160         case EDisconnecting:         
       
   161             iState = EWaitingToGetDevice;            
       
   162             User::RequestComplete(iObserverStatus, KErrNone); // DisconnectL succeeded
       
   163             break;
       
   164             
       
   165         default:
       
   166             Panic(EBTObjectExchangeSdpRecordDelete);
       
   167             break;
       
   168         };
       
   169     }
       
   170 }
       
   171 
       
   172 void CObjectExchangeClient::ConnectL(TRequestStatus& aObsRequestStatus)
       
   173 {
       
   174     aObsRequestStatus = KRequestPending;  // Have the observer wait for this request to complete
       
   175     iObserverStatus = &aObsRequestStatus; // Store the observer's TRequestStatus for later use 
       
   176     
       
   177     if (iState == EWaitingToGetDevice && !IsActive())
       
   178     {
       
   179         iState = EGettingDevice;
       
   180         iServiceSearcher->SelectDeviceByDiscoveryL(iStatus); // Show a device selection dialog
       
   181         SetActive();
       
   182 
       
   183     }
       
   184     else
       
   185     {
       
   186         // Inform the observer that connection cannot be initiated
       
   187         User::RequestComplete(iObserverStatus, KErrInUse );  
       
   188         return;
       
   189     }
       
   190 }
       
   191 
       
   192 void CObjectExchangeClient::ConnectToServerL()
       
   193 {  
       
   194 
       
   195     TObexBluetoothProtocolInfo protocolInfo;
       
   196     
       
   197     protocolInfo.iTransport.Copy(KServerTransportName);
       
   198     protocolInfo.iAddr.SetBTAddr(iServiceSearcher->BTDevAddr());
       
   199     protocolInfo.iAddr.SetPort(iServiceSearcher->Port());
       
   200     
       
   201     if (iClient)
       
   202     {
       
   203         delete iClient;
       
   204         iClient = NULL;
       
   205     }
       
   206     iClient = CObexClient::NewL(protocolInfo);
       
   207     
       
   208     iClient->Connect(iStatus);
       
   209     SetActive();
       
   210 
       
   211 }
       
   212 
       
   213 void CObjectExchangeClient::SendObjectL( const TDesC& aFileName, TRequestStatus& aObsRequestStatus)
       
   214 {
       
   215     aObsRequestStatus = KRequestPending;  // Have the observer wait for this request to complete
       
   216     iObserverStatus = &aObsRequestStatus; // Store the observer's TRequestStatus for later use 
       
   217     
       
   218     if (iState != EWaitingToSend)
       
   219     {
       
   220         // Inform the user that sending is not possible because there is no connection
       
   221         User::RequestComplete(iObserverStatus, KErrDisconnected);  
       
   222         return;
       
   223      
       
   224     }
       
   225     else if (IsActive()) 
       
   226     {
       
   227         // Inform the user that there is already an outstanding request on this object
       
   228         User::RequestComplete(iObserverStatus, KErrInUse);              
       
   229         return;
       
   230     }
       
   231     
       
   232     delete iCurrObject;
       
   233     iCurrObject = NULL;
       
   234     
       
   235     TRACE((aFileName));
       
   236 
       
   237 	iParse.Set( aFileName, NULL, NULL );
       
   238     
       
   239     // Create the OBEX object from the file
       
   240     TRAPD(err, iCurrObject = CObexFileObject::NewL(aFileName));
       
   241     if (err) 
       
   242     {
       
   243         TRACE((_L("_DEBUG: Error creating CObexFileObject"))); 
       
   244 
       
   245         // Inform the observer that file was not found
       
   246         User::RequestComplete(iObserverStatus, err);
       
   247         return;
       
   248         
       
   249     }
       
   250     else
       
   251     {
       
   252 
       
   253         /* Set proper values for the object info ? */
       
   254 
       
   255         // Fill in the object info        
       
   256         iCurrObject->SetDescriptionL(_L("OBEX file object")); 
       
   257         iCurrObject->SetNameL( iParse.NameAndExt());  
       
   258         iCurrObject->SetTypeL(_L8("text/plain"));     // NOTE: Sending everything as text/plain
       
   259         
       
   260          // Update the header mask
       
   261         TObexHeaderMask headermask = iCurrObject->HeaderMask(); // get current header mask
       
   262         headermask = headermask | KObexHdrName | KObexHdrType | KObexHdrDescription;
       
   263         iCurrObject->SetHeaderMask(headermask);    
       
   264         
       
   265         
       
   266     }    
       
   267 
       
   268     // Send the object
       
   269     iClient->Put(*iCurrObject, iStatus);
       
   270     SetActive();
       
   271     
       
   272 }
       
   273 
       
   274 
       
   275 void CObjectExchangeClient::StopL()
       
   276 {
       
   277 
       
   278     if (iClient && iClient->IsConnected())
       
   279     {
       
   280         iClient->Abort();
       
   281         iState = EWaitingToGetDevice;
       
   282     }
       
   283 }
       
   284 
       
   285 void CObjectExchangeClient::DisconnectL(TRequestStatus& aObsRequestStatus)
       
   286 {    
       
   287     aObsRequestStatus = KRequestPending;  // Have the observer wait for this request to complete
       
   288     iObserverStatus = &aObsRequestStatus; // Store the observer's TRequestStatus for later use 
       
   289 
       
   290     if (iState == EWaitingToGetDevice)
       
   291     {
       
   292         // Not connected, inform the observer
       
   293         User::RequestComplete(iObserverStatus, KErrNone);
       
   294         return;                           
       
   295     }
       
   296 
       
   297     if (iState == EWaitingToSend)
       
   298     {
       
   299         iState = EDisconnecting;
       
   300         iClient->Disconnect(iStatus);
       
   301         SetActive();
       
   302 
       
   303     }
       
   304     
       
   305     
       
   306     else 
       
   307     {
       
   308         // Busy connecting or disconnecting
       
   309         User::RequestComplete(iObserverStatus, KErrInUse);
       
   310         return;
       
   311         
       
   312         
       
   313     }
       
   314 }
       
   315 
       
   316 TBool CObjectExchangeClient::IsBusy()
       
   317 {
       
   318     return IsActive();
       
   319 }
       
   320 
       
   321 TBool CObjectExchangeClient::IsConnected()
       
   322 {
       
   323     return iState == EWaitingToSend;
       
   324 
       
   325 }
       
   326