syncmlfw/common/obex/obexclient/src/Irdaobexsearcher.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:  IrDA device and service searcher
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "Irdaobexsearcher.h"
       
    22 #include "ExtBTSearcherObserver.h"
       
    23 #include <obex.h>
       
    24 
       
    25 
       
    26 // CONSTANTS
       
    27 _LIT( KIrTransportName,"IrTinyTP" );
       
    28 _LIT8( KClassName,"SYNCML-SYNC" );
       
    29 _LIT( KHostName, "*" );
       
    30 
       
    31 _LIT( KIrObexSearcher, "Ir Obex searcher" );
       
    32 const TInt KInvalidStatePanicVal = -1;
       
    33 const TInt KDeviceSearchRetries = 10;		// amount of tries to find the device
       
    34 const TInt KDeviceSearchFirstTry = 1;
       
    35 const TInt KDeviceSearchTimeout = 500000;	// microseconds
       
    36 
       
    37 
       
    38 // ============================ MEMBER FUNCTIONS ===============================
       
    39 
       
    40 // -----------------------------------------------------------------------------
       
    41 // CIrDAObexSearcher::CIrDAObexSearcher
       
    42 // C++ default constructor can NOT contain any code, that
       
    43 // might leave.
       
    44 // -----------------------------------------------------------------------------
       
    45 //
       
    46 CIrDAObexSearcher::CIrDAObexSearcher()
       
    47 	: iConnectionRetry( KDeviceSearchFirstTry )
       
    48     {
       
    49     }
       
    50 
       
    51 // -----------------------------------------------------------------------------
       
    52 // CIrDAObexSearcher::ConstructL
       
    53 // Symbian 2nd phase constructor can leave.
       
    54 // -----------------------------------------------------------------------------
       
    55 //
       
    56 void CIrDAObexSearcher::ConstructL()
       
    57     {
       
    58 	BaseConstructL();
       
    59 
       
    60 	User::LeaveIfError( iSocketServer.Connect() );
       
    61 
       
    62 	iSocketServer.FindProtocol( KIrTransportName(), iProtocolInfo );
       
    63 
       
    64 	User::LeaveIfError( iHostResolver.Open(	iSocketServer, 
       
    65 		iProtocolInfo.iAddrFamily, iProtocolInfo.iProtocol ) );
       
    66 
       
    67 	User::LeaveIfError( iSocket.Open( iSocketServer, 
       
    68 		iProtocolInfo.iAddrFamily, iProtocolInfo.iSockType, iProtocolInfo.iProtocol ) );
       
    69 
       
    70 	TPckgBuf<TUint> buf(1);
       
    71 	User::LeaveIfError( iSocket.SetOpt( KDiscoverySlotsOpt, KLevelIrlap, buf ) );
       
    72 
       
    73 	iRetryTimer.CreateLocal();
       
    74     }
       
    75 
       
    76 // -----------------------------------------------------------------------------
       
    77 // CIrDAObexSearcher::NewL
       
    78 // Two-phased constructor.
       
    79 // -----------------------------------------------------------------------------
       
    80 //
       
    81 CIrDAObexSearcher* CIrDAObexSearcher::NewL()
       
    82     {
       
    83     CIrDAObexSearcher* self = new( ELeave ) CIrDAObexSearcher;
       
    84     
       
    85     CleanupStack::PushL( self );
       
    86     self->ConstructL();
       
    87     CleanupStack::Pop();
       
    88 
       
    89     return self;
       
    90     }
       
    91 
       
    92 //----------------------------------------------------------------------------
       
    93 // ~CIrDAObexSearcher()
       
    94 //----------------------------------------------------------------------------
       
    95 //
       
    96 CIrDAObexSearcher::~CIrDAObexSearcher()
       
    97     {
       
    98 	Cancel();
       
    99 	iRetryTimer.Close();
       
   100 	iSocket.Close();
       
   101 	iHostResolver.Close();
       
   102 	iSocketServer.Close();
       
   103     }
       
   104 
       
   105 //----------------------------------------------------------------------------
       
   106 // CIrDAObexSearcher::RunL()
       
   107 //----------------------------------------------------------------------------
       
   108 //
       
   109 void CIrDAObexSearcher::RunL()
       
   110 	{
       
   111 	TInt err( iStatus.Int() );
       
   112 
       
   113 	if ( iState != EDeviceSearchTimeout )
       
   114 		{
       
   115 		iInitialErr = err;
       
   116 		}
       
   117 
       
   118 	if ( err != KErrNone )
       
   119 		{
       
   120 		switch ( iState )
       
   121 			{
       
   122 			case EDeviceSearch:
       
   123 				{
       
   124 				if ( iConnectionRetry < KDeviceSearchRetries )
       
   125 					{
       
   126 					// Try again after short pause
       
   127 					iConnectionRetry++;
       
   128 					//DBG_ARGS8( "Failed, trying again %d more time(s)",
       
   129 					//				KDeviceSearchRetries - iConnectionRetry );
       
   130 
       
   131 					iState = EDeviceSearchTimeout;
       
   132 
       
   133 					iRetryTimer.After( iStatus, KDeviceSearchTimeout );
       
   134 					SetActive();
       
   135 					}
       
   136 				else
       
   137 					{
       
   138 					NotifyDeviceErrorL( iInitialErr );
       
   139 					// Reinitialize retry counter
       
   140 					iConnectionRetry = KDeviceSearchFirstTry;
       
   141 					}
       
   142 				break;
       
   143 				}
       
   144 			case EDeviceSearchTimeout:
       
   145 				{
       
   146 				// We should never end up here, but if we do, continue
       
   147 				// as if nothing happened.
       
   148 				SearchDeviceL();
       
   149 				break;
       
   150 				}
       
   151 			case EServiceSearch:
       
   152 				{
       
   153 				NotifyServiceErrorL( iInitialErr );
       
   154 				break;
       
   155 				}
       
   156 			default:
       
   157 				{
       
   158 				User::Panic( KIrObexSearcher, KInvalidStatePanicVal );
       
   159 				break;
       
   160 				}
       
   161 			}
       
   162 		}
       
   163 	else
       
   164 		{
       
   165 		switch ( iState )
       
   166 			{
       
   167 			case EDeviceSearch:
       
   168 				{
       
   169 				NotifyDeviceFoundL();
       
   170 				break;
       
   171 				}
       
   172 			case EDeviceSearchTimeout:
       
   173 				{
       
   174 				SearchDeviceL();
       
   175 				break;
       
   176 				}
       
   177 			case EServiceSearch:
       
   178 				{
       
   179 				NotifyServiceFoundL();
       
   180 				break;
       
   181 				}
       
   182 			default:
       
   183 				{
       
   184 				User::Panic( KIrObexSearcher, KInvalidStatePanicVal );
       
   185 				break;
       
   186 				}
       
   187 
       
   188 			}
       
   189 		}
       
   190 	}
       
   191 
       
   192 //----------------------------------------------------------------------------
       
   193 // CIrDAObexSearcher::DoCancel()
       
   194 //----------------------------------------------------------------------------
       
   195 //
       
   196 void CIrDAObexSearcher::DoCancel()
       
   197 	{
       
   198 	iRetryTimer.Cancel();
       
   199 	}
       
   200 
       
   201 //----------------------------------------------------------------------------
       
   202 // CIrDAObexSearcher::SearchDeviceL()
       
   203 //----------------------------------------------------------------------------
       
   204 //
       
   205 void CIrDAObexSearcher::SearchDeviceL()
       
   206 	{
       
   207 	//DBG_FILE( "CIrDAObexSearcher::SearchDeviceL()" );
       
   208 	
       
   209 	iState = EDeviceSearch;
       
   210 
       
   211 	iHostResolver.GetByName( KHostName, iLog, iStatus ); // Finds all IrLAP stations.
       
   212 	SetActive();
       
   213 	}
       
   214 
       
   215 //----------------------------------------------------------------------------
       
   216 // CIrDAObexSearcher::SearchServiceL()
       
   217 //----------------------------------------------------------------------------
       
   218 //
       
   219 void CIrDAObexSearcher::SearchServiceL()
       
   220 	{
       
   221 	//DBG_FILE( "CIrDAObexSearcher::SearchServiceL()" );
       
   222 
       
   223 	//Just simulate successful service search.
       
   224 	//This function could be advanced by executing
       
   225 	//IAS-queries to remote phone and deciding
       
   226 	//the success of SearchServiceL depending on the
       
   227 	//responses of the IAS queries.
       
   228 	iState = EServiceSearch;
       
   229 	TRequestStatus* status = &iStatus;
       
   230 	User::RequestComplete( status, KErrNone );
       
   231 	SetActive();
       
   232 	}
       
   233  
       
   234 //----------------------------------------------------------------------------
       
   235 // CIrDAObexSearcher::CreateObexClientL()
       
   236 //----------------------------------------------------------------------------
       
   237 //
       
   238 CObexClient* CIrDAObexSearcher::CreateObexClientL()
       
   239 	{
       
   240 	//DBG_FILE( "CIrDAObexSearcher::CreateObexClientL()" );
       
   241 
       
   242 	TObexIrProtocolInfo pInfo;
       
   243 	pInfo.iTransport.Copy( KIrTransportName );
       
   244 
       
   245 	if( iRetryConn )
       
   246 		{
       
   247 		// ISA device
       
   248 		pInfo.iClassName.Copy( _L("OBEX") );
       
   249 		}
       
   250 	else
       
   251 		{
       
   252 		pInfo.iClassName.Copy( KClassName );
       
   253 		iRetryConn = ETrue;
       
   254 		}
       
   255 
       
   256 	pInfo.iAttributeName.Copy( _L8("IrDA:TinyTP:LsapSel") );
       
   257 	pInfo.iAddr = iLog().iAddr;
       
   258 
       
   259 	CObexClient* client = CObexClient::NewL( pInfo );
       
   260 	return client;
       
   261 	}
       
   262 
       
   263 
       
   264 //  End of File