fmradio/fmradioengine/src/fmradiomobilenetworkinfolistener.cpp
branchRCL_3
changeset 20 93c594350b9a
parent 0 f3d95d9c00ab
equal deleted inserted replaced
19:cce62ebc198e 20:93c594350b9a
       
     1 /*
       
     2 * Copyright (c) 2004-2007 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: Gets country code and network info from mobile network
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDES
       
    20 #ifdef __WINS__
       
    21 #include <centralrepository.h>
       
    22 #else
       
    23 #include <commsdattypesv1_1.h>
       
    24 #include <etel.h>
       
    25 #include <etelmm.h>
       
    26 #include <commsdat_partner.h>
       
    27 #endif
       
    28 
       
    29 #include "fmradionetworkchangeobserver.h"
       
    30 #include "fmradiomobilenetworkinfolistener.h"
       
    31 #include "fmradioenginecrkeys.h"
       
    32 
       
    33 using namespace CommsDat;
       
    34 
       
    35 
       
    36 #ifdef __WINS__
       
    37 // Default size for the cenrep query
       
    38 const TInt KFMRadioDefaultNetworkInfoBufferSize = 0x80; 
       
    39 #endif
       
    40 
       
    41 // ================= MEMBER FUNCTIONS =======================
       
    42 
       
    43 // C++ default constructor can NOT contain any code, that
       
    44 // might leave.
       
    45 //
       
    46 #ifdef __WINS__
       
    47 CFMRadioMobileNetworkInfoListener::CFMRadioMobileNetworkInfoListener(MFMRadioNetworkChangeObserver & aObserver) : 
       
    48     CActive(CActive::EPriorityStandard), iNetworkChangeObserver(aObserver)
       
    49 #else
       
    50 CFMRadioMobileNetworkInfoListener::CFMRadioMobileNetworkInfoListener(MFMRadioNetworkChangeObserver & aObserver) : 
       
    51     CActive(CActive::EPriorityStandard), iNetworkChangeObserver(aObserver), iNetworkInfo(), iNetworkInfoPckg(iNetworkInfo)
       
    52 #endif
       
    53     {
       
    54     }
       
    55 
       
    56 // EPOC default constructor can leave.
       
    57 void CFMRadioMobileNetworkInfoListener::ConstructL()
       
    58     {
       
    59 
       
    60 #ifdef __WINS__
       
    61     iSubscriberId = KNullDesC().AllocL();
       
    62     
       
    63     TRAPD( err, iRepository = CRepository::NewL( KCRUidFMRadioEngine ) )
       
    64     if ( err == KErrNotFound )
       
    65         {
       
    66         // Central Repository not found, so don't request a notification
       
    67         }
       
    68     else if ( err != KErrNone )
       
    69         {
       
    70         User::Leave( err );
       
    71         }
       
    72     else 
       
    73         {
       
    74         // Read initial values from Central Repository
       
    75         GetCenRepNetworkInfoL();
       
    76 		SaveNetworkInfo();
       
    77         // Start listening
       
    78         CActiveScheduler::Add( this );
       
    79         User::LeaveIfError( iRepository->NotifyRequest( KFMRadioCREmulatorNetworkInfo, iStatus ) );
       
    80         SetActive();
       
    81         }
       
    82 #else
       
    83 	// Get default TSY from database.
       
    84 		
       
    85 	CMDBSession * db = CMDBSession::NewL(CMDBSession::LatestVersion());
       
    86 	CleanupStack::PushL(db);
       
    87 	
       
    88 	CMDBRecordSet<CCDModemBearerRecord> * set = new (ELeave) CMDBRecordSet<CCDModemBearerRecord>(KCDTIdModemBearerRecord);
       
    89 	CleanupStack::PushL(set);
       
    90 			
       
    91 	set->LoadL(*db);
       
    92 	
       
    93 	TInt index = 0;	
       
    94 	CCDModemBearerRecord * record = static_cast<CCDModemBearerRecord*>(set->iRecords[index++]);
       
    95 	
       
    96 	if(record->iTsyName.IsNull() && set->iRecords.Count() > index) // the first item is normally only a template
       
    97 		{
       
    98 		record = static_cast<CCDModemBearerRecord*>(set->iRecords[index++]);
       
    99 		}
       
   100 
       
   101 	TPtrC name(static_cast<const TDesC&>(record->iTsyName));
       
   102 	TBuf<KMaxTextLength> tsyName;
       
   103 	tsyName.Copy(name);
       
   104 	
       
   105 	CleanupStack::PopAndDestroy(set);
       
   106 	CleanupStack::PopAndDestroy(db);	
       
   107 
       
   108 	//  Open phone 
       
   109 	User::LeaveIfError(iTelServer.Connect());
       
   110 
       
   111 	// TSY module gets automatically unloaded when tel.Close() is called ( and tel is is CU-stack),
       
   112 	// so loaded TSY is also leave-safe
       
   113 	User::LeaveIfError(iTelServer.LoadPhoneModule(tsyName));
       
   114 	
       
   115 	// Get number of phones.
       
   116 	TInt phones(0);
       
   117 	User::LeaveIfError(iTelServer.EnumeratePhones(phones));
       
   118 
       
   119 	// Get phone info of first legal phone.
       
   120 	TInt legalPhoneIndex = KErrNotFound;
       
   121 	RTelServer::TPhoneInfo phoneInfo;
       
   122 	for (TInt i=0; i<phones && legalPhoneIndex == KErrNotFound; i++)
       
   123 		{
       
   124 		if (iTelServer.GetPhoneInfo(i, phoneInfo) == KErrNone)
       
   125 			{
       
   126 			if (phoneInfo.iNetworkType == RTelServer::ENetworkTypeMobileDigital)
       
   127 				{
       
   128 				legalPhoneIndex = i;
       
   129 				}
       
   130 			}
       
   131 		}
       
   132 	User::LeaveIfError(legalPhoneIndex);
       
   133 
       
   134 	// Open legal phone.
       
   135 	User::LeaveIfError(iPhone.Open(iTelServer, phoneInfo.iName));
       
   136 
       
   137 	TUint32 networkCaps;
       
   138 	User::LeaveIfError(iPhone.GetNetworkCaps(networkCaps));
       
   139 	TUint32 identityCaps;
       
   140 	User::LeaveIfError(iPhone.GetIdentityCaps(identityCaps));
       
   141 	// Check if we are allowed to get network info.
       
   142 	if (networkCaps & RMobilePhone::KCapsGetCurrentNetwork)
       
   143 		{
       
   144 		// Gather initial information synchronically.
       
   145 		iPhone.GetCurrentNetwork(iStatus, iNetworkInfoPckg, iLocation);
       
   146 		User::WaitForRequest(iStatus);
       
   147 		User::LeaveIfError(iStatus.Int());
       
   148 		SaveNetworkInfo();
       
   149 		// Start first notify request (if allowed).
       
   150 		if (networkCaps & RMobilePhone::KCapsNotifyCurrentNetwork)
       
   151 			{
       
   152 			CActiveScheduler::Add(this);
       
   153 			iPhone.NotifyCurrentNetworkChange(iStatus, iNetworkInfoPckg, iLocation);
       
   154 			SetActive();
       
   155 			}
       
   156 		}
       
   157 	else
       
   158 		{
       
   159 		// Leave if we are not allowed to get network info.
       
   160 		// Other action could also be considered!!!!
       
   161 		User::Leave(KErrNotSupported);
       
   162 		}
       
   163 	if(identityCaps & RMobilePhone::KCapsGetSubscriberId)
       
   164 		{
       
   165 		TRequestStatus stat;
       
   166 		RMobilePhone::TMobilePhoneSubscriberId subId;
       
   167 		iPhone.GetSubscriberId(stat, subId);
       
   168 		User::WaitForRequest(stat);
       
   169 		if(!stat.Int())
       
   170 			{
       
   171 			iSubscriberId = subId.AllocL();
       
   172 			}
       
   173 		else
       
   174 			{
       
   175 			iSubscriberId = KNullDesC().AllocL();
       
   176 			}
       
   177 		}
       
   178 #endif	
       
   179   }
       
   180 
       
   181 
       
   182 // Two-phased constructor.
       
   183 CFMRadioMobileNetworkInfoListener* CFMRadioMobileNetworkInfoListener::NewL(MFMRadioNetworkChangeObserver & aObserver)
       
   184     {
       
   185     CFMRadioMobileNetworkInfoListener* self = new (ELeave) CFMRadioMobileNetworkInfoListener(aObserver);   
       
   186     CleanupStack::PushL(self);
       
   187     self->ConstructL();
       
   188     CleanupStack::Pop();
       
   189     return self;
       
   190     }
       
   191 
       
   192     
       
   193 // Destructor
       
   194 CFMRadioMobileNetworkInfoListener::~CFMRadioMobileNetworkInfoListener()
       
   195 	{
       
   196 	Cancel();
       
   197 #ifdef __WINS__
       
   198     delete iRepository;
       
   199     delete iEmulatorNetworkInfo;
       
   200 #else
       
   201     iPhone.Close();
       
   202     iTelServer.Close();
       
   203 #endif
       
   204     delete iSubscriberId;
       
   205 	}
       
   206 
       
   207 
       
   208 // ---------------------------------------------------------
       
   209 // CFMRadioMobileNetworkInfoListener::RunL
       
   210 // ?implementation_description
       
   211 // (other items were commented in a header).
       
   212 // ---------------------------------------------------------
       
   213 //
       
   214 void CFMRadioMobileNetworkInfoListener::RunL()
       
   215 	{
       
   216 #ifdef __WINS__
       
   217     GetCenRepNetworkInfoL();
       
   218     SaveNetworkInfo();
       
   219 
       
   220     User::LeaveIfError( iRepository->NotifyRequest( KFMRadioCREmulatorNetworkInfo, iStatus ) );
       
   221     SetActive();
       
   222 #else
       
   223 	TInt statusCode = iStatus.Int();
       
   224 	if (statusCode == KErrNone)		// No other codes are returned (except error).
       
   225 		{
       
   226 		SaveNetworkInfo();
       
   227 		}
       
   228 	// Start new notify request.
       
   229 	iPhone.NotifyCurrentNetworkChange(iStatus, iNetworkInfoPckg, iLocation);
       
   230 	SetActive();
       
   231 #endif
       
   232 	}
       
   233 
       
   234 
       
   235 // ---------------------------------------------------------
       
   236 // CFMRadioMobileNetworkInfoListener::DoCancel
       
   237 // ?implementation_description
       
   238 // (other items were commented in a header).
       
   239 // ---------------------------------------------------------
       
   240 //
       
   241 void CFMRadioMobileNetworkInfoListener::DoCancel()
       
   242 	{
       
   243 #ifdef __WINS__
       
   244     iRepository->NotifyCancel( KFMRadioCREmulatorNetworkInfo );
       
   245 #else
       
   246 	iPhone.CancelAsyncRequest(EMobilePhoneNotifyCurrentNetworkChange);
       
   247 #endif
       
   248 	}
       
   249 
       
   250 // ---------------------------------------------------------
       
   251 // CFMRadioMobileNetworkInfoListener::SaveNetworkInfo
       
   252 // ?implementation_description
       
   253 // (other items were commented in a header).
       
   254 // ---------------------------------------------------------
       
   255 //
       
   256 void CFMRadioMobileNetworkInfoListener::SaveNetworkInfo()
       
   257 	{
       
   258 #ifdef __WINS__
       
   259     // iEmulatorNetworkInfo contains country code + network ID
       
   260     if ( iEmulatorNetworkInfo && iEmulatorNetworkInfo->Length() > KFMRadioCREmulatorCountryCodeLength )
       
   261         {
       
   262         iCountryCode.Copy( iEmulatorNetworkInfo->Left( KFMRadioCREmulatorCountryCodeLength ) );
       
   263         iNetworkId.Copy( iEmulatorNetworkInfo->Mid( KFMRadioCREmulatorCountryCodeLength, 
       
   264             Min( iEmulatorNetworkInfo->Length() - KFMRadioCREmulatorCountryCodeLength, 
       
   265                  iNetworkId.MaxLength() ) ) );
       
   266         }
       
   267     else
       
   268         {
       
   269         iCountryCode.Zero();
       
   270         iNetworkId.Zero();
       
   271         }
       
   272 #else
       
   273 	iNetworkInfo = iNetworkInfoPckg();
       
   274 	iNetworkId.Copy(iNetworkInfo.iNetworkId);
       
   275 	iCountryCode.Copy(iNetworkInfo.iCountryCode);
       
   276 #endif
       
   277 
       
   278 	// Create combine network ID and country code.
       
   279 	TBuf<20> compNetworkId;
       
   280 	compNetworkId.Append(iCountryCode);
       
   281 	// have to add '0' digit to the string to get correct id
       
   282 	// 24401 instead 2441, for example
       
   283 	if(iNetworkId.Length() < 2)
       
   284 		{
       
   285 		compNetworkId.AppendNum(static_cast<TInt64>(0));
       
   286 		}
       
   287 	compNetworkId.Append(iNetworkId);
       
   288 	TLex lexer(compNetworkId);
       
   289 	lexer.Val(iCompoundNetworkId);
       
   290 	if(iCompoundNetworkId != 0 && iPreviousCompoundNetworkId != iCompoundNetworkId)
       
   291 		{
       
   292 		if(iPreviousCompoundNetworkId != 0) 
       
   293 			{
       
   294 			iNetworkChangeObserver.NetworkIdChanged();
       
   295 			}
       
   296 		iPreviousCompoundNetworkId = iCompoundNetworkId;
       
   297 		}
       
   298 	}
       
   299 
       
   300 
       
   301 // ---------------------------------------------------------------------------
       
   302 // Reads network info from Central Repository
       
   303 // ---------------------------------------------------------------------------
       
   304 //
       
   305 #ifdef __WINS__
       
   306 void CFMRadioMobileNetworkInfoListener::GetCenRepNetworkInfoL()
       
   307     {
       
   308     TInt length = KFMRadioDefaultNetworkInfoBufferSize;
       
   309     HBufC* valueString = HBufC::NewLC( length );
       
   310     TInt error( KErrTooBig );
       
   311 
       
   312     do
       
   313         {
       
   314         TPtr ptr = valueString->Des();
       
   315 
       
   316         error = iRepository->Get( KFMRadioCREmulatorNetworkInfo, ptr );
       
   317         if ( error == KErrTooBig )
       
   318             {
       
   319             CleanupStack::PopAndDestroy( valueString );
       
   320             length = 2 * length;
       
   321             valueString = HBufC::NewLC( length );
       
   322             }
       
   323         } while ( error == KErrTooBig );
       
   324 
       
   325     if ( error ) 
       
   326         {
       
   327         TPtr ptr = valueString->Des();
       
   328         ptr.Copy( KNullDesC() );	
       
   329         }
       
   330 
       
   331     CleanupStack::Pop( valueString );
       
   332     delete iEmulatorNetworkInfo;
       
   333     iEmulatorNetworkInfo = valueString;
       
   334     }
       
   335 #endif
       
   336 
       
   337 
       
   338 // ---------------------------------------------------------
       
   339 // CFMRadioMobileNetworkInfoListener::CompoundNetworkId
       
   340 // ?implementation_description
       
   341 // (other items were commented in a header).
       
   342 // ---------------------------------------------------------
       
   343 //
       
   344 TInt CFMRadioMobileNetworkInfoListener::CompoundNetworkId() const
       
   345 	{
       
   346 	return iCompoundNetworkId;
       
   347 	}
       
   348 
       
   349 // ---------------------------------------------------------
       
   350 // CFMRadioMobileNetworkInfoListener::CountryCode
       
   351 // ?implementation_description
       
   352 // (other items were commented in a header).
       
   353 // ---------------------------------------------------------
       
   354 //
       
   355 const TDesC& CFMRadioMobileNetworkInfoListener::CountryCode() const
       
   356 	{
       
   357 	return iCountryCode;
       
   358 	}
       
   359 	
       
   360 // ---------------------------------------------------------
       
   361 // CFMRadioMobileNetworkInfoListener::SubscriberId
       
   362 // ?implementation_description
       
   363 // (other items were commented in a header).
       
   364 // ---------------------------------------------------------
       
   365 //
       
   366 const TDesC& CFMRadioMobileNetworkInfoListener::SubscriberId() const
       
   367 	{
       
   368 	return *iSubscriberId;
       
   369 	}
       
   370 
       
   371 //  End of File