javaextensions/location/position/src/clocationprovider.cpp
branchRCL_3
changeset 14 04becd199f91
equal deleted inserted replaced
13:f5050f1da672 14:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2008 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:  Handles LocationProvider operations
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <e32std.h>
       
    21 #include <f32file.h>
       
    22 #include "clocationprovider.h"
       
    23 #include "cpositioner.h"
       
    24 #include "ctrackingpositioner.h"
       
    25 #include "cdelaystatechangetimer.h"
       
    26 #include "javax_microedition_location_LocationProvider.h"
       
    27 #include "javax_microedition_location_Location.h"
       
    28 #include "com_nokia_mj_impl_location_LocationProviderImpl.h"
       
    29 #include "locationfunctionserver.h"
       
    30 #include "fs_methodcall.h"
       
    31 #include "logger.h"
       
    32 
       
    33 using namespace java::location;
       
    34 
       
    35 // CONSTANTS
       
    36 const TInt KAvailable =
       
    37     com_nokia_mj_impl_location_LocationProviderImpl_AVAILABLE;
       
    38 const TInt KTemporarilyUnavailable =
       
    39     com_nokia_mj_impl_location_LocationProviderImpl_TEMPORARILY_UNAVAILABLE;
       
    40 const TInt KOutOfService =
       
    41     com_nokia_mj_impl_location_LocationProviderImpl_OUT_OF_SERVICE;
       
    42 
       
    43 const TInt KAddressInfo =
       
    44     com_nokia_mj_impl_location_LocationProviderImpl_ADDRESS_REQUIRED;
       
    45 const TInt KCostAllowed =
       
    46     com_nokia_mj_impl_location_LocationProviderImpl_COST_ALLOWED;
       
    47 const TInt KAltitude =
       
    48     com_nokia_mj_impl_location_LocationProviderImpl_ALTITUDE_REQUIRED;
       
    49 const TInt KSpeedAndCourse =
       
    50     com_nokia_mj_impl_location_LocationProviderImpl_SPEED_AND_COURSE_REQUIRED;
       
    51 
       
    52 const TInt KMteSatellite = javax_microedition_location_Location_MTE_SATELLITE;
       
    53 const TInt KMtyTerminalbased =
       
    54     javax_microedition_location_Location_MTY_TERMINALBASED;
       
    55 const TInt KMtyNetworkbased =
       
    56     javax_microedition_location_Location_MTY_NETWORKBASED;
       
    57 const TInt KMtaAssisted = javax_microedition_location_Location_MTA_ASSISTED;
       
    58 const TInt KMtaUnassisted = javax_microedition_location_Location_MTA_UNASSISTED;
       
    59 
       
    60 const TInt KAvailableToTempUnavailableDelay = 5000000; // 5 seconds
       
    61 const TInt KTempUnavailableToAvailableDelay = 2000000; // 2 seconds
       
    62 
       
    63 // ============================ MEMBER FUNCTIONS ===============================
       
    64 
       
    65 // -----------------------------------------------------------------------------
       
    66 // CLocationProvider::CLocationProvider
       
    67 // C++ default constructor can NOT contain any code, that
       
    68 // might leave.
       
    69 // -----------------------------------------------------------------------------
       
    70 //
       
    71 CLocationProvider::CLocationProvider(LocationFunctionServer* aFunctionSource) :
       
    72         CActive(EPriorityNormal), mFunctionServer(aFunctionSource), iStatusEvent(
       
    73             TPositionModuleStatusEvent::EEventAll), iState(KAvailable)
       
    74 {
       
    75 }
       
    76 
       
    77 // -----------------------------------------------------------------------------
       
    78 // CLocationProvider::ConstructL
       
    79 // Symbian 2nd phase constructor can leave.
       
    80 // -----------------------------------------------------------------------------
       
    81 //
       
    82 void CLocationProvider::ConstructL()
       
    83 {
       
    84     CActiveScheduler::Add(this);
       
    85     JELOG2(EJavaLocation);
       
    86     User::LeaveIfError(iPositionServer.Connect());
       
    87     iStateTimer = CDelayStateChangeTimer::NewL(this);
       
    88 }
       
    89 
       
    90 // -----------------------------------------------------------------------------
       
    91 // CLocationProvider::NewL
       
    92 // Two-phased constructor.
       
    93 // -----------------------------------------------------------------------------
       
    94 //
       
    95 void CLocationProvider::NewL(LocationFunctionServer* aFunctionSource,
       
    96                              TInt* aHandle)
       
    97 {
       
    98     JELOG2(EJavaLocation);
       
    99 
       
   100     CLocationProvider* self = new(ELeave) CLocationProvider(aFunctionSource);
       
   101 
       
   102     CleanupStack::PushL(self);
       
   103     CallMethodL(self, &CLocationProvider::ConstructL, self->mFunctionServer);
       
   104     CleanupStack::Pop(self);
       
   105 
       
   106     *aHandle = reinterpret_cast<TInt>(self);
       
   107 }
       
   108 
       
   109 // Destructor
       
   110 CLocationProvider::~CLocationProvider()
       
   111 {
       
   112     JELOG2(EJavaLocation);
       
   113     Cancel();
       
   114     delete iStateTimer;
       
   115     delete iTrackingPositioner;
       
   116     iPositionServer.Close();
       
   117 }
       
   118 
       
   119 // -----------------------------------------------------------------------------
       
   120 // CLocationProvider::StaticCreatePositionerL
       
   121 // (other items were commented in a header).
       
   122 // -----------------------------------------------------------------------------
       
   123 //
       
   124 void CLocationProvider::StaticCreatePositionerL(JNIEnv* aJni,
       
   125         jobject aJavaPositioner, CLocationProvider* aSelf, TInt* aHandle,
       
   126         LocationFunctionServer* aFunctionSource)
       
   127 {
       
   128     JELOG2(EJavaLocation);
       
   129 
       
   130     CPositioner* positioner = NULL;
       
   131     TRAPD(error, CallMethodL(positioner, aSelf,
       
   132                              &CLocationProvider::CreatePositionerL, aFunctionSource));
       
   133 
       
   134     if ((error != KErrNone) || (positioner == NULL))
       
   135     {
       
   136         ELOG1(EJavaLocation, "StaticCreatePositionerL - error=%d", error);
       
   137         *aHandle = error;
       
   138         return;
       
   139     }
       
   140 
       
   141     // Positioner takes ownership of the event since it is reusable
       
   142     positioner->SetCallBackData(aJavaPositioner, aJni);
       
   143 
       
   144     *aHandle = reinterpret_cast<TInt>(positioner);
       
   145 }
       
   146 
       
   147 // -----------------------------------------------------------------------------
       
   148 // CLocationProvider::RunL
       
   149 // (other items were commented in a header).
       
   150 // -----------------------------------------------------------------------------
       
   151 //
       
   152 void CLocationProvider::RunL()
       
   153 {
       
   154     JELOG2(EJavaLocation);
       
   155     if (iStatus == KErrNone)
       
   156     {
       
   157         iStateTimer->Cancel();
       
   158 
       
   159         TPositionModuleStatus moduleStatus;
       
   160         iStatusEvent.GetModuleStatus(moduleStatus);
       
   161         TInt newState = StatusToState(moduleStatus);
       
   162 
       
   163         if (newState == KTemporarilyUnavailable && iState == KAvailable)
       
   164         {
       
   165             iStateTimer->ChangeStateAfter(KAvailableToTempUnavailableDelay,
       
   166                                           newState);
       
   167         }
       
   168         else if (newState == KAvailable && iState == KTemporarilyUnavailable)
       
   169         {
       
   170             iStateTimer->ChangeStateAfter(KTempUnavailableToAvailableDelay,
       
   171                                           newState);
       
   172         }
       
   173         else
       
   174         {
       
   175             ChangeState(newState);
       
   176         }
       
   177     }
       
   178 
       
   179     RequestModuleStatus();
       
   180 }
       
   181 
       
   182 // -----------------------------------------------------------------------------
       
   183 // CLocationProvider::DoCancel
       
   184 // (other items were commented in a header).
       
   185 // -----------------------------------------------------------------------------
       
   186 //
       
   187 void CLocationProvider::DoCancel()
       
   188 {
       
   189     JELOG2(EJavaLocation);
       
   190     iStateTimer->Cancel();
       
   191     iPositionServer.CancelRequest(EPositionServerNotifyModuleStatusEvent);
       
   192 }
       
   193 
       
   194 // -----------------------------------------------------------------------------
       
   195 // CLocationProvider::CreatePositionerL
       
   196 // (other items were commented in a header).
       
   197 // -----------------------------------------------------------------------------
       
   198 //
       
   199 CPositioner* CLocationProvider::CreatePositionerL()
       
   200 {
       
   201     JELOG2(EJavaLocation);
       
   202     CPositioner* positioner = CPositioner::NewL(mFunctionServer,
       
   203                               iPositionServer, iModuleId, iCapabilities);
       
   204     return positioner;
       
   205 }
       
   206 
       
   207 // -----------------------------------------------------------------------------
       
   208 // CLocationProvider::SelectModuleL
       
   209 //
       
   210 // Selects a positioning module. Prefers modules in AVAILABLE state.
       
   211 //   Leaves with KErrNotSupported if no module match the requirements.
       
   212 //   Leaves with KErrNotFound if no modules are available.
       
   213 // (other items were commented in a header).
       
   214 // -----------------------------------------------------------------------------
       
   215 //
       
   216 void CLocationProvider::SelectModuleL(TInt aHacc, TInt aVacc, TInt aRespTime,
       
   217                                       TInt aPower, TInt aRequiredFlags, TInt* aMethodTimeout)
       
   218 {
       
   219     JELOG2(EJavaLocation);
       
   220     mJni = mFunctionServer->getValidJniEnv();
       
   221     mPeer = mFunctionServer->getPeer();
       
   222 
       
   223     jclass peerClass = (*mJni).GetObjectClass(mPeer);
       
   224 
       
   225     //Get Method ID of Azimuth Data Callback
       
   226     mStateChangMethod = mJni->GetMethodID(peerClass, "stateChange", "(I)V");
       
   227 
       
   228     if (AllLocationRequestsDeniedL())
       
   229     {
       
   230         User::Leave(KErrNotFound); // Location is off, no modules can be used
       
   231     }
       
   232 
       
   233     TBool allModulesOutOfService(ETrue);
       
   234     TPositionModuleInfo modInfo;
       
   235 
       
   236     // Check if requirements are met for available modules
       
   237     TUint numModules(0);
       
   238     User::LeaveIfError(iPositionServer.GetNumModules(numModules));
       
   239 
       
   240     for (TUint i = 0; i < numModules; ++i)
       
   241     {
       
   242         User::LeaveIfError(iPositionServer.GetModuleInfoByIndex(i, modInfo));
       
   243 
       
   244         if (modInfo.IsAvailable())
       
   245         {
       
   246             TPositionQuality moduleQuality;
       
   247             modInfo.GetPositionQuality(moduleQuality);
       
   248             if (CheckRequirements(moduleQuality, modInfo.Capabilities(), aHacc,
       
   249                                   aVacc, aRespTime, aPower, aRequiredFlags))
       
   250             {
       
   251                 iModuleId = modInfo.ModuleId();
       
   252                 break;
       
   253             }
       
   254 
       
   255             allModulesOutOfService = EFalse;
       
   256         }
       
   257     }
       
   258 
       
   259     if (iModuleId == KPositionNullModuleId) // No module found
       
   260     {
       
   261         User::Leave(allModulesOutOfService ? KErrNotFound : KErrNotSupported);
       
   262     }
       
   263 
       
   264     RequestModuleStatus();
       
   265 
       
   266     iCapabilities = modInfo.Capabilities();
       
   267 
       
   268     iTrackingPositioner = CTrackingPositioner::NewL(mFunctionServer,
       
   269                           iPositionServer, iModuleId, iCapabilities);
       
   270 
       
   271     TInt locationMethod = (iCapabilities
       
   272                            & TPositionModuleInfo::ECapabilitySatellite) ? KMteSatellite : 0;
       
   273 
       
   274     TInt tech = modInfo.TechnologyType();
       
   275     TBool networkBased = EFalse;
       
   276 
       
   277     if (tech & TPositionModuleInfo::ETechnologyTerminal)
       
   278     {
       
   279         locationMethod |= KMtyTerminalbased;
       
   280     }
       
   281     if (tech & TPositionModuleInfo::ETechnologyNetwork)
       
   282     {
       
   283         locationMethod |= KMtyNetworkbased;
       
   284         networkBased = ETrue;
       
   285     }
       
   286 
       
   287     TPositionQuality quality;
       
   288     modInfo.GetPositionQuality(quality);
       
   289     iTrackingPositioner->SetDefaultValues(quality.TimeToNextFix(), networkBased);
       
   290 
       
   291     locationMethod
       
   292     |= (tech & TPositionModuleInfo::ETechnologyAssisted) ? KMtaAssisted
       
   293        : KMtaUnassisted;
       
   294 
       
   295     aMethodTimeout[0] = locationMethod;
       
   296     // Default timeout is set to TTFF * 2
       
   297     aMethodTimeout[1] = (I64INT(quality.TimeToFirstFix().Int64()) * 2)
       
   298                         / 1000000;
       
   299 }
       
   300 
       
   301 // -----------------------------------------------------------------------------
       
   302 // CLocationProvider::CheckRequirements
       
   303 //
       
   304 // Returns ETrue if requirements are met, EFalse otherwise.
       
   305 // (other items were commented in a header).
       
   306 // -----------------------------------------------------------------------------
       
   307 //
       
   308 TBool CLocationProvider::CheckRequirements(const TPositionQuality& aQuality,
       
   309         TPositionModuleInfo::TCapabilities aCaps, TInt aHacc, TInt aVacc,
       
   310         TInt aRespTime, TInt aPower, TInt aRequiredFlags)
       
   311 {
       
   312     JELOG2(EJavaLocation);
       
   313     if ((aRequiredFlags & KAddressInfo) && !(aCaps
       
   314             & (TPositionModuleInfo::ECapabilityAddress
       
   315                | TPositionModuleInfo::ECapabilityBuilding
       
   316                | TPositionModuleInfo::ECapabilityMedia)))
       
   317     {
       
   318         return EFalse;
       
   319     }
       
   320     if ((aRequiredFlags & KAltitude) && !(aCaps
       
   321                                           & TPositionModuleInfo::ECapabilityVertical))
       
   322     {
       
   323         return EFalse;
       
   324     }
       
   325     if ((aRequiredFlags & KSpeedAndCourse) && !((aCaps
       
   326             & TPositionModuleInfo::ECapabilitySpeed) && (aCaps
       
   327                     & TPositionModuleInfo::ECapabilityDirection)))
       
   328     {
       
   329         return EFalse;
       
   330     }
       
   331     if (!(aRequiredFlags & KCostAllowed) && aQuality.CostIndicator()
       
   332             != TPositionQuality::ECostZero)
       
   333     {
       
   334         return EFalse;
       
   335     }
       
   336     if (aHacc != 0 && aQuality.HorizontalAccuracy() > aHacc)
       
   337     {
       
   338         return EFalse;
       
   339     }
       
   340     if (aVacc != 0 && aQuality.VerticalAccuracy() > aVacc)
       
   341     {
       
   342         return EFalse;
       
   343     }
       
   344     if (aRespTime != 0 && (aQuality.TimeToNextFix().Int64() / 1000) > aRespTime)
       
   345     {
       
   346         return EFalse;
       
   347     }
       
   348 
       
   349     if (aPower > 0 && aPower < 3) // LOW or MEDIUM
       
   350     {
       
   351         switch (aQuality.PowerConsumption())
       
   352         {
       
   353         case TPositionQuality::EPowerHigh: // Fall through
       
   354         case TPositionQuality::EPowerUnknown:
       
   355             return EFalse;
       
   356         case TPositionQuality::EPowerMedium:
       
   357             if (aPower == 1)
       
   358             {
       
   359                 return EFalse;
       
   360             }
       
   361         default:
       
   362             break;
       
   363         }
       
   364     }
       
   365 
       
   366     return ETrue;
       
   367 }
       
   368 
       
   369 // -----------------------------------------------------------------------------
       
   370 // CLocationProvider::RequestModuleStatus
       
   371 // (other items were commented in a header).
       
   372 // -----------------------------------------------------------------------------
       
   373 //
       
   374 void CLocationProvider::RequestModuleStatus()
       
   375 {
       
   376     JELOG2(EJavaLocation);
       
   377     iPositionServer.NotifyModuleStatusEvent(iStatusEvent, iStatus, iModuleId);
       
   378     SetActive();
       
   379 }
       
   380 
       
   381 // -----------------------------------------------------------------------------
       
   382 // CLocationProvider::StatusToState
       
   383 // (other items were commented in a header).
       
   384 // -----------------------------------------------------------------------------
       
   385 //
       
   386 TInt CLocationProvider::StatusToState(TPositionModuleStatus& aStatus)
       
   387 {
       
   388     JELOG2(EJavaLocation);
       
   389     switch (aStatus.DeviceStatus())
       
   390     {
       
   391     case TPositionModuleStatus::EDeviceUnknown: // Illegal state => Out of service
       
   392     case TPositionModuleStatus::EDeviceError:
       
   393     case TPositionModuleStatus::EDeviceDisabled:
       
   394         return KOutOfService;
       
   395     case TPositionModuleStatus::EDeviceInactive:
       
   396     case TPositionModuleStatus::EDeviceInitialising:
       
   397         return KAvailable;
       
   398     case TPositionModuleStatus::EDeviceStandBy:
       
   399     case TPositionModuleStatus::EDeviceReady:
       
   400     case TPositionModuleStatus::EDeviceActive:
       
   401         if (aStatus.DataQualityStatus()
       
   402                 == TPositionModuleStatus::EDataQualityLoss)
       
   403         {
       
   404             return KTemporarilyUnavailable;
       
   405         }
       
   406         else
       
   407         {
       
   408             return KAvailable;
       
   409         }
       
   410     default:
       
   411         return KOutOfService;
       
   412     }
       
   413 }
       
   414 
       
   415 // -----------------------------------------------------------------------------
       
   416 // CLocationProvider::ChangeState
       
   417 // (other items were commented in a header).
       
   418 // -----------------------------------------------------------------------------
       
   419 //
       
   420 void CLocationProvider::ChangeState(TInt aState)
       
   421 {
       
   422     if (aState != iState)
       
   423     {
       
   424         mJni = mFunctionServer->getValidJniEnv();
       
   425         mPeer = mFunctionServer->getPeer();
       
   426 
       
   427         (*mJni).CallVoidMethod(mPeer, mStateChangMethod, aState);
       
   428         iState = aState;
       
   429     }
       
   430 }
       
   431 
       
   432 // -----------------------------------------------------------------------------
       
   433 // CLocationProvider::AllLocationRequestsDeniedL
       
   434 // (other items were commented in a header).
       
   435 // -----------------------------------------------------------------------------
       
   436 //
       
   437 TBool CLocationProvider::AllLocationRequestsDeniedL()
       
   438 {
       
   439     // Global privacy not used anymore
       
   440     return EFalse;
       
   441 }
       
   442