syncmlfw/common/obex/obexclient/src/Btobexsearcher.cpp
changeset 0 b497e44ab2fc
child 23 4af31167ea77
equal deleted inserted replaced
-1:000000000000 0:b497e44ab2fc
       
     1 /*
       
     2 * Copyright (c) 2002 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:  BT device and service searcher
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include    <btnotif.h>
       
    22 #include    "Btobexsearcher.h"
       
    23 #include	"NSmlObexServiceSearcher.h"
       
    24 #include	"ExtBTSearcherObserver.h"
       
    25 
       
    26 // CONSTANTS
       
    27 _LIT( KBTTransportName,"RFCOMM" );
       
    28 
       
    29 // LOCAL CONSTANTS AND MACROS
       
    30 _LIT( KBTObexSearcher, "BT Obex searcher" );
       
    31 const TInt KInvalidStatePanicVal = -1;
       
    32 
       
    33 
       
    34 // ============================ MEMBER FUNCTIONS ===============================
       
    35 
       
    36 // -----------------------------------------------------------------------------
       
    37 // CBTObexSearcher::CBTObexSearcher
       
    38 // C++ default constructor can NOT contain any code, that
       
    39 // might leave.
       
    40 // -----------------------------------------------------------------------------
       
    41 //
       
    42 CBTObexSearcher::CBTObexSearcher()
       
    43     {
       
    44     }
       
    45 
       
    46 // -----------------------------------------------------------------------------
       
    47 // CBTObexSearcher::ConstructL
       
    48 // Symbian 2nd phase constructor can leave.
       
    49 // -----------------------------------------------------------------------------
       
    50 //
       
    51 void CBTObexSearcher::ConstructL( const TBTConnInfo& aBTConnInfo )
       
    52     {
       
    53 	BaseConstructL();
       
    54     iServiceSearcher = CNSmlObexServiceSearcher::NewL( aBTConnInfo );
       
    55     iSettings = CBTEngSettings::NewL( this );
       
    56     }
       
    57 
       
    58 // -----------------------------------------------------------------------------
       
    59 // CBTObexSearcher::NewL
       
    60 // Two-phased constructor.
       
    61 // -----------------------------------------------------------------------------
       
    62 //
       
    63 CBTObexSearcher* CBTObexSearcher::NewL( const TBTConnInfo& aBTConnInfo )
       
    64     {
       
    65     CBTObexSearcher* self = new( ELeave ) CBTObexSearcher;
       
    66     
       
    67     CleanupStack::PushL( self );
       
    68     self->ConstructL( aBTConnInfo );
       
    69     CleanupStack::Pop();
       
    70 
       
    71     return self;
       
    72     }
       
    73 
       
    74 //----------------------------------------------------------------------------
       
    75 // ~CBTObexSearcher()
       
    76 //----------------------------------------------------------------------------
       
    77 //
       
    78 CBTObexSearcher::~CBTObexSearcher()
       
    79     {
       
    80 	Cancel();
       
    81     delete iServiceSearcher;
       
    82     delete iSettings;   
       
    83     if ( iNotifier.Handle() )
       
    84         {
       
    85         iNotifier.Close();
       
    86         }
       
    87     }
       
    88 
       
    89 //----------------------------------------------------------------------------
       
    90 // CBTObexSearcher::RunL()
       
    91 //----------------------------------------------------------------------------
       
    92 //
       
    93 void CBTObexSearcher::RunL()
       
    94 	{
       
    95 
       
    96 	TInt err( iStatus.Int() );
       
    97 	if ( err != KErrNone )
       
    98 		{
       
    99 		switch ( iState )
       
   100 			{
       
   101             case EIdle:
       
   102                 {
       
   103                 if( iWaitingForBTPower )
       
   104                     {
       
   105                     NotifyDeviceErrorL( err );
       
   106                     }
       
   107                 break;
       
   108                 }
       
   109 			case EDeviceSearch:
       
   110 				{
       
   111 				NotifyDeviceErrorL( err );
       
   112 				//Notify external observer
       
   113 				if ( iExtObserver )
       
   114 					{
       
   115 					iExtObserver->BTDeviceErrorL( err );
       
   116 					}
       
   117 				break;
       
   118 				}
       
   119 			case EServiceSearch:
       
   120 				{
       
   121 				NotifyServiceErrorL( err );
       
   122 				break;
       
   123 				}
       
   124 			default:
       
   125 				{
       
   126 				User::Panic( KBTObexSearcher, KInvalidStatePanicVal );
       
   127 				break;
       
   128 				}
       
   129 
       
   130 			}
       
   131 		}
       
   132 	else
       
   133 		{
       
   134 		switch ( iState )
       
   135 			{
       
   136             case EIdle:
       
   137                 {
       
   138                 if( iWaitingForBTPower && iOffline() != EFalse )
       
   139                     {
       
   140                     if ( iNotifier.Handle() )
       
   141 				        {
       
   142 				        iNotifier.Close();
       
   143 				        }
       
   144                     TInt err = iSettings->ChangePowerStateTemporarily( );
       
   145                     if( err )
       
   146                         {
       
   147                         NotifyDeviceErrorL( err );
       
   148                         }
       
   149                     }
       
   150                 else
       
   151                     {
       
   152                     NotifyDeviceErrorL( KErrCancel );
       
   153                     }
       
   154                 break;
       
   155                 }
       
   156 			case EDeviceSearch:
       
   157 				{
       
   158 				NotifyDeviceFoundL();
       
   159 				//Notify external observer
       
   160 				if ( iExtObserver )
       
   161 					{
       
   162 					iExtObserver->BTDeviceFoundL( iServiceSearcher->ResponseParams() );
       
   163 					}
       
   164 
       
   165 				break;
       
   166 				}
       
   167 			case EServiceSearch:
       
   168 				{
       
   169 				NotifyServiceFoundL();
       
   170 				break;
       
   171 				}
       
   172 			default:
       
   173 				{
       
   174 				User::Panic( KBTObexSearcher, KInvalidStatePanicVal );
       
   175 				break;
       
   176 				}
       
   177 
       
   178 			}
       
   179 		}
       
   180 	}
       
   181 
       
   182 //----------------------------------------------------------------------------
       
   183 // CBTObexSearcher::DoCancel()
       
   184 //----------------------------------------------------------------------------
       
   185 //
       
   186 void CBTObexSearcher::DoCancel()
       
   187 	{	
       
   188 	delete iServiceSearcher;
       
   189 	iServiceSearcher = 0;
       
   190 	}
       
   191 
       
   192 //----------------------------------------------------------------------------
       
   193 // CBTObexSearcher::SearchDeviceL()
       
   194 //----------------------------------------------------------------------------
       
   195 //
       
   196 void CBTObexSearcher::SearchDeviceL()
       
   197 	{
       
   198     iWaitingForBTPower = ETrue;
       
   199     TBTPowerStateValue power = EBTPowerOff;
       
   200     User::LeaveIfError( iSettings->GetPowerState( power ) );
       
   201     TBool offline = EFalse;
       
   202     
       
   203     if( power == EBTPowerOff )
       
   204         {
       
   205         offline = CheckOfflineModeL();
       
   206         }
       
   207     if( !offline )
       
   208         {
       
   209     	// Even if BT is already on, we still register as "temp BT user".
       
   210         User::LeaveIfError( iSettings->ChangePowerStateTemporarily(  ) );
       
   211         }
       
   212         if( power == EBTPowerOn )
       
   213         {
       
   214         // start to search immediately.
       
   215         PowerStateChanged( power );
       
   216         }
       
   217    	}
       
   218 
       
   219 //----------------------------------------------------------------------------
       
   220 // CBTObexSearcher::SearchServiceL()
       
   221 //----------------------------------------------------------------------------
       
   222 //
       
   223 void CBTObexSearcher::SearchServiceL()
       
   224 	{
       
   225 	SetActive();
       
   226 	iState = EServiceSearch;
       
   227 	iStatus = KRequestPending;
       
   228 	iServiceSearcher->FindServiceL( iStatus );
       
   229 	}
       
   230 
       
   231 //----------------------------------------------------------------------------
       
   232 // CBTObexSearcher::CreateObexClientL()
       
   233 //----------------------------------------------------------------------------
       
   234 //
       
   235 CObexClient* CBTObexSearcher::CreateObexClientL()
       
   236 	{
       
   237 	TObexBluetoothProtocolInfo protocolInfo;
       
   238 
       
   239 	protocolInfo.iTransport.Copy( KBTTransportName );
       
   240 	protocolInfo.iAddr.SetBTAddr( iServiceSearcher->BTDevAddr() );
       
   241 	protocolInfo.iAddr.SetPort( iServiceSearcher->Port() );
       
   242 
       
   243 	CObexClient* obexClient = CObexClient::NewL( protocolInfo );
       
   244 	return obexClient;
       
   245 	}
       
   246 
       
   247 
       
   248 //----------------------------------------------------------------------------
       
   249 // CBTObexSearcher::PowerStateChanged
       
   250 // Bluetooth power state has changed; start searching devices if it is on.
       
   251 //----------------------------------------------------------------------------
       
   252 //
       
   253 void CBTObexSearcher::PowerStateChanged( TBTPowerStateValue aState )
       
   254     {
       
   255     if( iWaitingForBTPower && aState == EBTPowerOn )
       
   256         {
       
   257         iWaitingForBTPower = EFalse;
       
   258         TRAPD( err, iServiceSearcher->SelectDeviceByDiscoveryL( iStatus ) );
       
   259         if( !err )
       
   260             {
       
   261             iState = EDeviceSearch;
       
   262             SetActive();
       
   263             }
       
   264         else
       
   265             {
       
   266             TRAP_IGNORE(NotifyDeviceErrorL( err ));
       
   267             }
       
   268         }
       
   269     }
       
   270 
       
   271 
       
   272 //----------------------------------------------------------------------------
       
   273 // CBTObexSearcher::VisibilityModeChanged
       
   274 // Notification of changes in the discoverability mode, ignored.
       
   275 //----------------------------------------------------------------------------
       
   276 //
       
   277 void CBTObexSearcher::VisibilityModeChanged( TBTVisibilityMode /*aState*/ )
       
   278     {
       
   279     
       
   280     }
       
   281 
       
   282 
       
   283 //----------------------------------------------------------------------------
       
   284 // CBTObexSearcher::CheckOfflineModeL
       
   285 // Checks the offline mode and queries the user for turning BT on if required.
       
   286 //----------------------------------------------------------------------------
       
   287 //
       
   288 TBool CBTObexSearcher::CheckOfflineModeL()
       
   289     {
       
   290     TCoreAppUIsNetworkConnectionAllowed offline = ECoreAppUIsNetworkConnectionNotAllowed;
       
   291     TBTEnabledInOfflineMode offlineAllowed = EBTDisabledInOfflineMode;
       
   292     User::LeaveIfError( iSettings->GetOfflineModeSettings( offline, offlineAllowed ) );
       
   293     if( offline == ECoreAppUIsNetworkConnectionNotAllowed && 
       
   294          offlineAllowed == EBTEnabledInOfflineMode )
       
   295         {
       
   296         User::LeaveIfError( iNotifier.Connect() );
       
   297         TBTGenericQueryNotiferParamsPckg pckg;
       
   298         pckg().iMessageType = EBTActivateOffLineQuery;
       
   299         pckg().iNameExists = EFalse;
       
   300         iNotifier.StartNotifierAndGetResponse( iStatus, KBTGenericQueryNotifierUid, 
       
   301                                                 pckg, iOffline );
       
   302         SetActive();
       
   303         }
       
   304     else if( offline == ECoreAppUIsNetworkConnectionNotAllowed && 
       
   305             offlineAllowed == EBTDisabledInOfflineMode )
       
   306            {
       
   307            NotifyDeviceErrorL( KErrNotSupported );
       
   308            }
       
   309     return ( offline == ECoreAppUIsNetworkConnectionNotAllowed );
       
   310     }
       
   311 //  End of File