| 2 |      1 | /*
 | 
|  |      2 |  * Copyright (c) 2007-2010 Sebastian Brannstrom, Lars Persson, EmbedDev AB
 | 
|  |      3 |  *
 | 
|  |      4 |  * All rights reserved.
 | 
|  |      5 |  * This component and the accompanying materials are made available
 | 
|  |      6 |  * under the terms of the License "Eclipse Public License v1.0"
 | 
|  |      7 |  * which accompanies this distribution, and is available
 | 
|  |      8 |  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
 | 
|  |      9 |  *
 | 
|  |     10 |  * Initial Contributors:
 | 
|  |     11 |  * EmbedDev AB - initial contribution.
 | 
|  |     12 |  *
 | 
|  |     13 |  * Contributors:
 | 
|  |     14 |  *
 | 
|  |     15 |  * Description:
 | 
|  |     16 |  *
 | 
|  |     17 |  */
 | 
|  |     18 | 
 | 
|  |     19 | #include "connectionengine.h"
 | 
|  |     20 | #include "settingsengine.h"
 | 
|  |     21 | #include "podcastmodel.h"
 | 
|  |     22 | 
 | 
|  |     23 | CConnectionEngine* CConnectionEngine::NewL(CPodcastModel& aPodcastModel)
 | 
|  |     24 | 	{
 | 
|  |     25 | 	CConnectionEngine* self = new (ELeave) CConnectionEngine(aPodcastModel);
 | 
|  |     26 | 	CleanupStack::PushL(self);
 | 
|  |     27 | 	self->ConstructL();
 | 
|  |     28 | 	CleanupStack::Pop(self);
 | 
|  |     29 | 	return self;
 | 
|  |     30 | 	}
 | 
|  |     31 | 
 | 
|  |     32 | CConnectionEngine::~CConnectionEngine()
 | 
|  |     33 | 	{
 | 
|  |     34 | 	delete iMobility;
 | 
|  |     35 | 
 | 
|  |     36 | 	Cancel();
 | 
|  |     37 | 	
 | 
|  |     38 | 	iConnection.Close();
 | 
|  |     39 | 
 | 
|  |     40 | 	iSocketServer.Close();
 | 
|  |     41 | 	
 | 
|  |     42 | 	iObserverArray.Close();
 | 
|  |     43 | 	}
 | 
|  |     44 | 
 | 
|  |     45 | CConnectionEngine::CConnectionEngine(CPodcastModel& aPodcastModel):
 | 
|  |     46 | 					CActive(CActive::EPriorityStandard),iPodcastModel(aPodcastModel)
 | 
|  |     47 | 	{
 | 
|  |     48 | 	CActiveScheduler::Add(this);	
 | 
|  |     49 | 	}
 | 
|  |     50 | 
 | 
|  |     51 | void CConnectionEngine::ConstructL()
 | 
|  |     52 | 	{
 | 
|  |     53 | 	User::LeaveIfError(iSocketServer.Connect());
 | 
|  |     54 | 	User::LeaveIfError( iConnection.Open( iSocketServer ) );
 | 
|  |     55 | 	}
 | 
|  |     56 | 
 | 
|  |     57 | void CConnectionEngine::RunL()
 | 
|  |     58 | 	{		
 | 
|  |     59 | 	if ( iStatus.Int() == KErrNone )
 | 
|  |     60 | 		{		
 | 
|  |     61 | 		delete iMobility;
 | 
|  |     62 | 		iMobility = NULL;
 | 
|  |     63 | 		iMobility = CActiveCommsMobilityApiExt::NewL( iConnection, *this );
 | 
|  |     64 | 		}
 | 
|  |     65 | 	
 | 
|  |     66 | 	iConnectionState = iStatus.Int() == KErrNone?CConnectionEngine::EConnected:CConnectionEngine::ENotConnected;
 | 
|  |     67 | 	ReportConnection( iStatus.Int() );
 | 
|  |     68 | 	}
 | 
|  |     69 | 
 | 
|  |     70 | void CConnectionEngine::DoCancel()
 | 
|  |     71 | 	{
 | 
|  |     72 | 	}
 | 
|  |     73 | 
 | 
|  |     74 | TInt CConnectionEngine::RunError( TInt /*aError*/ )
 | 
|  |     75 | 	{
 | 
|  |     76 | 	return KErrNone;
 | 
|  |     77 | 	}
 | 
|  |     78 | 
 | 
|  |     79 | void CConnectionEngine::PreferredCarrierAvailable( TAccessPointInfo /*aOldAPInfo*/,
 | 
|  |     80 | 		TAccessPointInfo /*aNewAPInfo*/,
 | 
|  |     81 | 		TBool aIsUpgrade,
 | 
|  |     82 | 		TBool aIsSeamless )
 | 
|  |     83 | 	{   
 | 
|  |     84 | 	if ( aIsUpgrade )
 | 
|  |     85 | 		{        
 | 
|  |     86 | 		}
 | 
|  |     87 | 	else
 | 
|  |     88 | 		{       
 | 
|  |     89 | 		}
 | 
|  |     90 | 
 | 
|  |     91 | 	if ( aIsSeamless )
 | 
|  |     92 | 		{
 | 
|  |     93 | 		// in S60 3.2, this situation cannot occur.        
 | 
|  |     94 | 		}
 | 
|  |     95 | 	else
 | 
|  |     96 | 		{       
 | 
|  |     97 | 		// Sockets have to be closed at this point.        
 | 
|  |     98 | 
 | 
|  |     99 | 		iMobility->MigrateToPreferredCarrier();
 | 
|  |    100 | 		}
 | 
|  |    101 | 
 | 
|  |    102 | 	}
 | 
|  |    103 | 
 | 
|  |    104 | void CConnectionEngine::NewCarrierActive( TAccessPointInfo /*aNewAPInfo*/, TBool aIsSeamless )
 | 
|  |    105 | 	{    
 | 
|  |    106 | 	if ( aIsSeamless )
 | 
|  |    107 | 		{
 | 
|  |    108 | 		// in S60 3.2, this situation cannot occur.
 | 
|  |    109 | 
 | 
|  |    110 | 		}
 | 
|  |    111 | 	else
 | 
|  |    112 | 		{        
 | 
|  |    113 | 		// Sockets have to be re-opened and check they can connect
 | 
|  |    114 | 		// to their server at this point.        
 | 
|  |    115 | 
 | 
|  |    116 | 		iMobility->NewCarrierAccepted();
 | 
|  |    117 | 		}
 | 
|  |    118 | 	}
 | 
|  |    119 | 
 | 
|  |    120 | void CConnectionEngine::Error( TInt /*aError*/ )
 | 
|  |    121 | 	{
 | 
|  |    122 | 
 | 
|  |    123 | 	}
 | 
|  |    124 | 
 | 
|  |    125 | TBool CConnectionEngine::ConnectionSettingL()
 | 
|  |    126 | 	{
 | 
|  |    127 | 	TBool selected( EFalse );
 | 
|  |    128 | 
 | 
|  |    129 | 	CCmApplicationSettingsUi* settings = CCmApplicationSettingsUi::NewL();
 | 
|  |    130 | 	CleanupStack::PushL( settings );
 | 
|  |    131 | 
 | 
|  |    132 | 	TUint listedItems = 
 | 
|  |    133 | 	CMManager::EShowDefaultConnection |
 | 
|  |    134 | 	CMManager::EShowDestinations;
 | 
|  |    135 | 
 | 
|  |    136 | 	TBearerFilterArray filter;
 | 
|  |    137 | 
 | 
|  |    138 | 	selected = settings->RunApplicationSettingsL( iUserSelection,
 | 
|  |    139 | 			listedItems,
 | 
|  |    140 | 			filter );
 | 
|  |    141 | 
 | 
|  |    142 | 	CleanupStack::PopAndDestroy( settings );
 | 
|  |    143 | 
 | 
|  |    144 | 	return selected;
 | 
|  |    145 | 	}
 | 
|  |    146 | 
 | 
|  |    147 | 
 | 
|  |    148 | void CConnectionEngine::StartL(TConnectionType aConnectionType)
 | 
|  |    149 | 	{
 | 
|  |    150 | 	DP1("CConnectionEngine::StartL, aConnectionType=%d", aConnectionType);
 | 
|  |    151 | 	
 | 
|  |    152 | 	iConnection.Close();
 | 
|  |    153 | 	User::LeaveIfError( iConnection.Open( iSocketServer ) );
 | 
|  |    154 | 	// Connect using UI Setting
 | 
|  |    155 | 	if(aConnectionType == EDefaultConnection)
 | 
|  |    156 | 		{
 | 
|  |    157 | 		iConnection.Start( iStatus );
 | 
|  |    158 | 		SetActive();
 | 
|  |    159 | 		}
 | 
|  |    160 | 	else if(aConnectionType == EUserSelectConnection)
 | 
|  |    161 | 		{				
 | 
|  |    162 | 		TBool selected = ConnectionSettingL();
 | 
|  |    163 | 
 | 
|  |    164 | 		if ( selected )
 | 
|  |    165 | 			{
 | 
|  |    166 | 			switch ( iUserSelection.iResult )
 | 
|  |    167 | 				{
 | 
|  |    168 | 				case CMManager::EDestination:
 | 
|  |    169 | 					{					
 | 
|  |    170 | 					iSnapPreference.SetSnap( iUserSelection.iId );
 | 
|  |    171 | 					iConnection.Start( iSnapPreference, iStatus );
 | 
|  |    172 | 					aConnectionType = ESNAPConnection;
 | 
|  |    173 | 					break;
 | 
|  |    174 | 					}				
 | 
|  |    175 | 				default: // CMManager::EAlwaysAsk
 | 
|  |    176 | 				case CMManager::EDefaultConnection:
 | 
|  |    177 | 					{					
 | 
|  |    178 | 					iConnection.Start( iStatus );
 | 
|  |    179 | 					break;
 | 
|  |    180 | 					}									
 | 
|  |    181 | 				}						
 | 
|  |    182 | 			}
 | 
|  |    183 | 		else
 | 
|  |    184 | 			{
 | 
|  |    185 | 			TRequestStatus* status = &iStatus;
 | 
|  |    186 | 			User::RequestComplete(status, KErrCancel);
 | 
|  |    187 | 			}
 | 
|  |    188 | 		
 | 
|  |    189 | 			SetActive();
 | 
|  |    190 | 		}	
 | 
|  |    191 | 	else if (aConnectionType == EIAPConnection)
 | 
|  |    192 | 		{
 | 
|  |    193 | 		iCommdbPreference.SetIapId((iPodcastModel.SettingsEngine().SpecificIAP()& KUseIAPMask));
 | 
|  |    194 | 		iCommdbPreference.SetDialogPreference(ECommDbDialogPrefDoNotPrompt);
 | 
|  |    195 | 		iCommdbPreference.SetDirection(ECommDbConnectionDirectionOutgoing);
 | 
|  |    196 | 		iConnection.Start( iCommdbPreference, iStatus );
 | 
|  |    197 | 		SetActive();
 | 
|  |    198 | 		}
 | 
|  |    199 | 	// Connect using SNAP 
 | 
|  |    200 | 	else
 | 
|  |    201 | 		{
 | 
|  |    202 | 		iSnapPreference.SetSnap(iPodcastModel.SettingsEngine().SpecificIAP());
 | 
|  |    203 | 		iConnection.Start( iSnapPreference, iStatus );
 | 
|  |    204 | 		SetActive();	
 | 
|  |    205 | 		}		
 | 
|  |    206 | 	
 | 
|  |    207 | 	iConnectionType = aConnectionType;
 | 
|  |    208 | 	iConnectionState = CConnectionEngine::EConnecting;
 | 
|  |    209 | 	}
 | 
|  |    210 | 
 | 
|  |    211 | RConnection& CConnectionEngine::Connection()
 | 
|  |    212 | 	{
 | 
|  |    213 | 	return iConnection;	
 | 
|  |    214 | 	}
 | 
|  |    215 | 
 | 
|  |    216 | CConnectionEngine::TConnectionState CConnectionEngine::ConnectionState()
 | 
|  |    217 | 	{
 | 
|  |    218 | 	TInt selectedConn = (TInt) iSnapPreference.Snap();
 | 
|  |    219 | 	TInt specIAPSNAP = iPodcastModel.SettingsEngine().SpecificIAP();
 | 
|  |    220 | 	// If we have IAP preference then get that from our current connection and mask out the selected iap
 | 
|  |    221 | 	if((specIAPSNAP&KUseIAPFlag))
 | 
|  |    222 | 		{
 | 
|  |    223 | 		selectedConn = iCommdbPreference.IapId();
 | 
|  |    224 | 		specIAPSNAP = specIAPSNAP&KUseIAPMask;
 | 
|  |    225 | 		}
 | 
|  |    226 | 	
 | 
|  |    227 | 	// IAPSNAP must be > 0 and then  if the IAP/Sel conn IAP differs
 | 
|  |    228 | 	if(specIAPSNAP >0 && specIAPSNAP !=  selectedConn )
 | 
|  |    229 | 		{
 | 
|  |    230 | 		if(iConnection.SubSessionHandle() != 0)
 | 
|  |    231 | 			{
 | 
|  |    232 | 			iConnection.Stop();
 | 
|  |    233 | 			}
 | 
|  |    234 | 		
 | 
|  |    235 | 		iConnectionState = CConnectionEngine::ENotConnected;
 | 
|  |    236 | 		}
 | 
|  |    237 | 	else
 | 
|  |    238 | 		{
 | 
|  |    239 | 		// We have a user selection or default selction, check our current connection state.
 | 
|  |    240 | 		TNifProgress progress;
 | 
|  |    241 | 		if(iConnection.Progress(progress) == KErrNone)
 | 
|  |    242 | 			{
 | 
|  |    243 | 			if(progress.iError == KErrNone && progress.iStage != 0)
 | 
|  |    244 | 				{
 | 
|  |    245 | 				if(progress.iStage == KLinkLayerOpen)
 | 
|  |    246 | 					{
 | 
|  |    247 | 					iConnectionState = CConnectionEngine::EConnected;
 | 
|  |    248 | 					}				
 | 
|  |    249 | 				}
 | 
|  |    250 | 			else if(iConnectionState != CConnectionEngine::EConnecting)
 | 
|  |    251 | 				{
 | 
|  |    252 | 				iConnectionState = CConnectionEngine::ENotConnected;
 | 
|  |    253 | 				}
 | 
|  |    254 | 			}
 | 
|  |    255 | 		else
 | 
|  |    256 | 			{
 | 
|  |    257 | 			iConnectionState = CConnectionEngine::ENotConnected;
 | 
|  |    258 | 			}
 | 
|  |    259 | 		}
 | 
|  |    260 | 	
 | 
|  |    261 | 	return iConnectionState;
 | 
|  |    262 | 	}
 | 
|  |    263 | 
 | 
|  |    264 | void CConnectionEngine::AddObserver(MConnectionObserver* aObserver)
 | 
|  |    265 | 	{
 | 
|  |    266 | 	iObserverArray.Append(aObserver);
 | 
|  |    267 | 	}
 | 
|  |    268 | 
 | 
|  |    269 | RSocketServ& CConnectionEngine::SockServ()
 | 
|  |    270 | 	{
 | 
|  |    271 | 	return iSocketServer;
 | 
|  |    272 | 	}
 | 
|  |    273 | 
 | 
|  |    274 | 
 | 
|  |    275 | void CConnectionEngine::ReportConnection(TInt aError)
 | 
|  |    276 | 	{
 | 
|  |    277 | 	TInt noObservers = iObserverArray.Count();
 | 
|  |    278 | 	while(noObservers)
 | 
|  |    279 | 		{
 | 
|  |    280 | 		noObservers--;
 | 
|  |    281 | 		iObserverArray[noObservers]->ConnectCompleteL(aError);
 | 
|  |    282 | 		}
 | 
|  |    283 | 	}
 |