datasourcemodules/simulationpositioningmodule/src/EPos_CPosControllerManager.cpp
changeset 0 9cfd9a3ee49c
equal deleted inserted replaced
-1:000000000000 0:9cfd9a3ee49c
       
     1 /*
       
     2 * Copyright (c) 2009 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 #include "EPos_CPosControllerManager.h"
       
    19 #include <e32debug.h> 
       
    20 #include <f32file.h>
       
    21 #include <e32std.h>
       
    22 #include <centralrepository.h>
       
    23 #include "EPos_CPosSimulationPositioner.h"
       
    24 #include "EPos_CPosControllerBase.h"
       
    25 #include "SimulationPSYInternalCRKeys.h"
       
    26 #include "EPos_CPosNmeaController.h"
       
    27 #include "EPos_CPosFixedDataController.h"
       
    28 #include "EPos_CPosDataSimulationController.h"
       
    29 _LIT(KDefaultNmeFileName, "\\resource\\default.nme");
       
    30 
       
    31 // The unit tests (te_lbssimulationsuite.exe) are built as an EXE and cannot 
       
    32 // use TLS.  The macro below redefines Dll to a new class TestDll that provides 
       
    33 // an EXE equivalent.
       
    34 #ifdef SIMPSYTEST
       
    35 #define Dll TestDll
       
    36 class TestDll
       
    37 	{
       
    38 public:
       
    39 	static TAny* Tls() { return reinterpret_cast<TAny*>(iInternal); };
       
    40 	static TInt SetTls(const TAny* aAny) 
       
    41 		{ 
       
    42 		iInternal = reinterpret_cast<TUint32>(aAny); 
       
    43 		return KErrNone;
       
    44 		};
       
    45 	static void FreeTls() { iInternal = 0; };
       
    46 private:
       
    47 	static TUint32 iInternal;
       
    48 	};
       
    49 TUint32 TestDll::iInternal = 0;
       
    50 #endif
       
    51 
       
    52 TUint32 HashFunc(const CPosSimulationPositioner& aSimPos)
       
    53 	{
       
    54 	return DefaultHash::Integer(reinterpret_cast<TInt>(&aSimPos));
       
    55 	}
       
    56 
       
    57 TBool IdentityFunc(const CPosSimulationPositioner& aSimPos1, const CPosSimulationPositioner& aSimPos2)
       
    58 	{
       
    59 	return &aSimPos1 == &aSimPos2;
       
    60 	}
       
    61 
       
    62 // ---------------------------------------------------------------------------
       
    63 // Returns the singleton instance.
       
    64 // ---------------------------------------------------------------------------
       
    65 //
       
    66 CPosControllerManager* CPosControllerManager::InstanceL()
       
    67 	{
       
    68 	CPosControllerManager* self = NULL;
       
    69 
       
    70 	TAny* tlsPtr = Dll::Tls();
       
    71 	if (!tlsPtr)
       
    72 		{
       
    73 		self = new (ELeave) CPosControllerManager;
       
    74 		CleanupStack::PushL(self);
       
    75 		self->ConstructL();
       
    76 		User::LeaveIfError(Dll::SetTls(self));
       
    77 		CleanupStack::Pop(self);
       
    78 		}
       
    79 	else
       
    80 		{
       
    81 		self = static_cast<CPosControllerManager*> (tlsPtr);
       
    82 		++self->iReferenceCount;
       
    83 		}
       
    84 
       
    85 	return self;
       
    86 	}
       
    87 
       
    88 // ---------------------------------------------------------------------------
       
    89 // Releases the singleton instance.
       
    90 // ---------------------------------------------------------------------------
       
    91 //
       
    92 void CPosControllerManager::ReleaseInstance(const CPosSimulationPositioner& aSimPos)
       
    93 	{
       
    94 	//Delete controller only in Independent Data Set mode.
       
    95 	//when there is more then one controller
       
    96 	//In TRP mode the only existing controller is deleted in destructor
       
    97 	if (!iTrp)
       
    98 		{
       
    99 		CPosControllerBase* p = iPositionerControllerMap.Find(aSimPos);
       
   100 		delete p;
       
   101 		iPositionerControllerMap.Remove(&aSimPos);
       
   102 		}
       
   103 	
       
   104 
       
   105 	TAny* tlsPtr = Dll::Tls();
       
   106 	__ASSERT_DEBUG( tlsPtr, User::Panic(
       
   107 					_L( "CPosControllerManager" ), KErrNotFound ) );
       
   108 
       
   109 	CPosControllerManager* self = static_cast<CPosControllerManager*> (tlsPtr);
       
   110 	if (--self->iReferenceCount == 0)
       
   111 		{
       
   112 		Dll::FreeTls();
       
   113 		delete self;
       
   114 		}
       
   115 	}
       
   116 
       
   117 CPosControllerManager::CPosControllerManager() :
       
   118 	iReferenceCount(1),
       
   119 	iPositionerControllerMap(THashFunction32<CPosSimulationPositioner>(&HashFunc), TIdentityRelation<CPosSimulationPositioner>(&IdentityFunc))
       
   120 	{
       
   121 	iLastFixIsPartial = EFalse; 
       
   122 	}
       
   123 
       
   124 void CPosControllerManager::ConstructL()
       
   125 	{
       
   126 	iTrp = RepositoryTrpValueL();
       
   127 	}
       
   128 
       
   129 /**
       
   130  * Private destructor so users cannot directly delete this instance.
       
   131  * They must call ReleaseInstance()
       
   132  */
       
   133 CPosControllerManager::~CPosControllerManager()
       
   134 	{
       
   135 	iPositionerControllerMap.Close();
       
   136 	if (iTrpController && iTrp)
       
   137 		{
       
   138 		delete iTrpController;
       
   139 		}
       
   140 	}
       
   141 
       
   142 /**
       
   143  * Check in central repository if time relative playback (TRP) in on.
       
   144  * @return ETrue if TRP is on, EFalse if it's off.
       
   145  */
       
   146 TBool CPosControllerManager::RepositoryTrpValueL()
       
   147 	{
       
   148 	TInt val = 0;
       
   149 	CRepository* repository = CRepository::NewLC(KCRUidSimulationPSY);
       
   150 	User::LeaveIfError( repository->Get(KCRKeySimPSYCommonDataSet, val) );
       
   151 	CleanupStack::PopAndDestroy(repository);
       
   152 	return static_cast<TBool>(val);
       
   153 	}
       
   154 
       
   155 CPosControllerBase& CPosControllerManager::ControllerL(
       
   156 		const CPosSimulationPositioner& aSimPos)
       
   157 	{
       
   158 	CPosControllerBase* p = NULL;
       
   159 	if(!iTrp)
       
   160 		{
       
   161 		p = iPositionerControllerMap.Find(aSimPos);
       
   162 		}
       
   163 	else
       
   164 		{
       
   165 		p = iTrpController;
       
   166 		}
       
   167 	//if there is no controller associated with give positioner
       
   168 	//just create new one
       
   169 	if(!p)
       
   170 		{
       
   171 		p = CreateControllerL(aSimPos);
       
   172 		//in Time Relative Playback we should have only one controller
       
   173 		//in old mode we need to create each time new one.
       
   174 		if (iTrp)
       
   175 			{
       
   176 			iTrpController = p;
       
   177 			}
       
   178 		}
       
   179 	return *p;
       
   180 	}
       
   181 
       
   182 /**
       
   183  * Create controller and pairs it with given CPosSimulationPositioner 
       
   184  * Controller type is determined by filename given in central repository.
       
   185  * @param aSimPos Pointer to positioner that will be connecter with new controller
       
   186  */
       
   187 CPosControllerBase* CPosControllerManager::CreateControllerL(const CPosSimulationPositioner& aSimPos)
       
   188 	{
       
   189 	CPosControllerBase* retController = NULL;
       
   190 	//getting file name from repository
       
   191 	TFileName fileName;
       
   192 	CRepository* repository = CRepository::NewLC(KCRUidSimulationPSY);
       
   193 	User::LeaveIfError(repository->Get(KCRKeySimPSYSimulationFile, fileName));
       
   194 	CleanupStack::PopAndDestroy(repository);
       
   195 
       
   196 	fileName.LowerCase();
       
   197 	TParsePtrC fileNamePtr(fileName);
       
   198 
       
   199 	//based on file extension creating correct type controller
       
   200 	TInt err = KErrNone;
       
   201 	if (fileNamePtr.Ext().Compare(KPosNMEAFileExtension) == 0)
       
   202 		{
       
   203 			TRAP(err, retController =
       
   204 					CPosNmeaController::NewL(fileName, iTrp));
       
   205 		}
       
   206 	else if (fileNamePtr.Ext().Compare(KPosSimulationFileExtension) == 0)
       
   207 		{
       
   208 			TRAP(err, retController =
       
   209 					CPosDataSimController::NewL(fileName, iTrp));
       
   210 		}
       
   211 	else if (fileName.Length() == 0)
       
   212 		{
       
   213 		// No file selected so use the default.nme file from
       
   214 		// \resource directory
       
   215 		TFileName DllName;
       
   216 		TParse nmeFileName;
       
   217 #ifndef SIMPSYTEST
       
   218 		Dll::FileName(DllName);
       
   219 #else
       
   220 		_LIT(KTestPathTxt, "Z:\\resource\\");
       
   221 		DllName.Copy(KTestPathTxt);
       
   222 #endif
       
   223 
       
   224 		nmeFileName.Set(KDefaultNmeFileName, &DllName, NULL);
       
   225 
       
   226 			TRAP(err, retController =
       
   227 					CPosNmeaController::NewL(nmeFileName.FullName(), iTrp));
       
   228 		if (err == KErrPathNotFound || err == KErrNotFound 
       
   229 				|| err == KErrCorrupt)
       
   230 			{
       
   231 				TRAP(err, retController = CPosFixedDataController::NewL());
       
   232 			}
       
   233 		}
       
   234 	else
       
   235 		{
       
   236 		err = KErrNotSupported;
       
   237 		}
       
   238 	User::LeaveIfError(err);
       
   239 	//after valid controller creation adding it to map of positioners 
       
   240 	//and controllers to be able then properly delete it when it will be unneeded
       
   241 	if(!iTrp)
       
   242 		{
       
   243 		iPositionerControllerMap.InsertL(&aSimPos, retController);
       
   244 		}
       
   245 	return retController;
       
   246 	}
       
   247 
       
   248 TPositionInfo& CPosControllerManager::GetLastFix()
       
   249 	{
       
   250 	return iLastFix;
       
   251 	}
       
   252 
       
   253 void CPosControllerManager::SetLastFix(TPositionInfo& aPosInfo)
       
   254 	{
       
   255 	iLastFix = aPosInfo;
       
   256 	}
       
   257 
       
   258 TBool CPosControllerManager::GetLastFixIsPartial()
       
   259 	{
       
   260 	return iLastFixIsPartial;
       
   261 	}
       
   262 
       
   263 void CPosControllerManager::SetLastFixIsPartial(TBool aPartial)
       
   264 	{
       
   265 	iLastFixIsPartial = aPartial;
       
   266 	}