datasourcemodules/bluetoothgpspositioningmodule/btgpspsy/src/RequestHandler/BTGPSRequestManager.cpp
changeset 36 b47902b73a93
parent 0 9cfd9a3ee49c
equal deleted inserted replaced
35:a2efdd544abf 36:b47902b73a93
       
     1 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 
       
    17 
       
    18 // INCLUDE FILES
       
    19 #include <e32std.h>
       
    20 #include "BTGPSRequestManager.h"
       
    21 #include "BTGPSConnectManager.h"
       
    22 #include "BTGPSConstantsManager.h"
       
    23 #include "BTGPSRequestHandler.h"
       
    24 #include "BTGPSFix.h"
       
    25 #include "BTGPSNmeaBuffer.h"
       
    26 #include "BTGPSNmeaParser.h"
       
    27 #include "BTGPSLocationFixListener.h"
       
    28 #include "BTGPSLogging.h"
       
    29 
       
    30 // EXTERNAL DATA STRUCTURES
       
    31 
       
    32 // EXTERNAL FUNCTION PROTOTYPES  
       
    33 
       
    34 // CONSTANTS
       
    35 //GSV wait time. If GSV sentence is not available, system
       
    36 //wait wait this much time before the fix is delivered.
       
    37 const TInt KBTGPSGsvWaitTime = 1000000; //1 second
       
    38 
       
    39 //NMEA sentence terminator
       
    40 _LIT8(KBTGPSNmeaTerminator, "\x0d\x0a");
       
    41 
       
    42 // MACROS
       
    43 
       
    44 // LOCAL CONSTANTS AND MACROS
       
    45 
       
    46 // MODULE DATA STRUCTURES
       
    47 
       
    48 // LOCAL FUNCTION PROTOTYPES
       
    49 
       
    50 // FORWARD DECLARATIONS
       
    51 
       
    52 // ============================= LOCAL FUNCTIONS ===============================
       
    53 
       
    54 // ============================ MEMBER FUNCTIONS ===============================
       
    55 
       
    56 
       
    57 // -----------------------------------------------------------------------------
       
    58 // CBTGPSRequestManager::NewL
       
    59 // -----------------------------------------------------------------------------
       
    60 CBTGPSRequestManager* CBTGPSRequestManager::NewL(
       
    61     CBTGPSConnectManager& aConnectManager,
       
    62     CBTGPSConstantsManager& aConstantsManager)
       
    63     {
       
    64     CBTGPSRequestManager* self = new (ELeave) CBTGPSRequestManager(
       
    65         aConnectManager,
       
    66         aConstantsManager);
       
    67     CleanupStack::PushL(self);
       
    68     self->ConstructL();
       
    69     CleanupStack::Pop();
       
    70     return self;
       
    71     }
       
    72 
       
    73 // -----------------------------------------------------------------------------
       
    74 // CBTGPSRequestManager::~CBTGPSRequestManager
       
    75 // -----------------------------------------------------------------------------
       
    76 CBTGPSRequestManager::~CBTGPSRequestManager()
       
    77     {
       
    78     if(iTimer!=NULL)
       
    79         {
       
    80         iTimer->Cancel();
       
    81         delete iTimer;
       
    82         }
       
    83         
       
    84     delete iFix;
       
    85     delete iLastFix;
       
    86     iListenerArray.Close();
       
    87     delete iNmeaBuffer;
       
    88     }
       
    89 
       
    90 // -----------------------------------------------------------------------------
       
    91 // CBTGPSRequestManager::ConstructL
       
    92 // -----------------------------------------------------------------------------
       
    93 void CBTGPSRequestManager::ConstructL()
       
    94     {
       
    95     iNmeaBuffer = CBTGPSNmeaBuffer::NewL(
       
    96         iConstantsManager.iNmeaBufferSize);
       
    97 
       
    98     iFix = new (ELeave) CBTGPSFix();
       
    99     iFix->SetNmeaBuffer(iNmeaBuffer);
       
   100     
       
   101     iLastFix = new (ELeave) CBTGPSFix();
       
   102     iTimer = CPeriodic::NewL(CActive::EPriorityStandard);
       
   103     
       
   104     }
       
   105 
       
   106 // -----------------------------------------------------------------------------
       
   107 // CBTGPSRequestManager::CBTGPSRequestManager
       
   108 // C++ default constructor can NOT contain any code, that
       
   109 // might leave.
       
   110 // -----------------------------------------------------------------------------
       
   111 CBTGPSRequestManager::CBTGPSRequestManager(
       
   112     CBTGPSConnectManager& aConnectManager,
       
   113     CBTGPSConstantsManager& aConstantsManager)
       
   114     : iConnectManager(aConnectManager),
       
   115       iConstantsManager(aConstantsManager)
       
   116     {
       
   117     iPrevBottom = 0;
       
   118     }
       
   119 
       
   120 // -----------------------------------------------------------------------------
       
   121 // CBTGPSRequestManager::RetrieveFixL
       
   122 // -----------------------------------------------------------------------------
       
   123 void CBTGPSRequestManager::RetrieveFixL()
       
   124     {
       
   125     if(!iReceivingMessage)
       
   126         {
       
   127         iConnectManager.AddMessageListenerL(*this);
       
   128         iReceivingMessage = ETrue;
       
   129         }
       
   130     }
       
   131 
       
   132 // -----------------------------------------------------------------------------
       
   133 // CBTGPSRequestManager::StopRetrievingFix
       
   134 // -----------------------------------------------------------------------------
       
   135 void CBTGPSRequestManager::StopRetrievingFix()
       
   136     {
       
   137     //For better performance, system handles received message
       
   138     //when device is connected.
       
   139     }
       
   140 
       
   141 
       
   142 // -----------------------------------------------------------------------------
       
   143 // CBTGPSRequestManager::HandleMessage
       
   144 // -----------------------------------------------------------------------------
       
   145 void CBTGPSRequestManager::HandleMessage(const TBTGPSNmeaParser& aParser)
       
   146     {
       
   147     //Add the received message to buffer
       
   148     iNmeaBuffer->AddSentences(aParser.NmeaSentence());
       
   149     //Add 0x0d 0x0a
       
   150     iNmeaBuffer->AddSentences(KBTGPSNmeaTerminator);
       
   151     
       
   152     //Parse the message
       
   153     CBTGPSFix::TParsingStatus err = iFix->ParseMessage(aParser);
       
   154 
       
   155     //Check if we have received a valid sentence
       
   156     if(err == CBTGPSFix::EInfoUpdated)
       
   157         {
       
   158         if(iFix->IfFullNmeaPatternReceived())
       
   159             {
       
   160             TTime gsvTime = iFix->GsvTime();
       
   161             TTime now;
       
   162             now.UniversalTime();
       
   163             TRACESTRING2("CBTGPSRequestManager:: Now time = %d", now.Int64())
       
   164             TRACESTRING2("CBTGPSRequestManager:: GSV time = %d", gsvTime.Int64())
       
   165             if(gsvTime!=0 && now.MicroSecondsFrom(gsvTime)< 
       
   166                 CBTGPSRequestHandler::ConstantsManager().iSatelliteInfoLifeTime)
       
   167                 {
       
   168                 //GSV information is still valid, set fix as valid
       
   169                 TRACESTRING("CBTGPSRequestManager:: GSV information is still valid")
       
   170                 iTimer->Cancel();
       
   171                 InformListeners();
       
   172                 }
       
   173             else
       
   174                 {
       
   175                 if(!iTimer->IsActive())
       
   176                     {
       
   177                     //GSV information is not valid, start timer
       
   178                     iTimer->Start(
       
   179                         KBTGPSGsvWaitTime,
       
   180                         KBTGPSGsvWaitTime,
       
   181                         TCallBack(TimerCallback, this));
       
   182                     }
       
   183                 }
       
   184             }
       
   185         }
       
   186     }
       
   187 
       
   188 // -----------------------------------------------------------------------------
       
   189 // CBTGPSRequestManager::LastLocation
       
   190 // -----------------------------------------------------------------------------
       
   191 TInt CBTGPSRequestManager::LastLocation(
       
   192             const CBTGPSFix*& aFix,
       
   193             const TTime& aMaxAge,
       
   194             const TBool /*aAcceptPartial*/)
       
   195     {
       
   196     if(aMaxAge.Int64() == 0)
       
   197         {
       
   198         return KErrNotFound;
       
   199         }
       
   200         
       
   201     if(iLastFix->IsFixValid())
       
   202         {
       
   203         if(iLastFix->FixTime()>=aMaxAge)
       
   204             {
       
   205             aFix = iLastFix;
       
   206             return KErrNone;
       
   207             }
       
   208         }
       
   209     return KErrNotFound;
       
   210     }
       
   211 
       
   212 // -----------------------------------------------------------------------------
       
   213 // CBTGPSRequestManager::SystemTimeChange
       
   214 // -----------------------------------------------------------------------------
       
   215 void CBTGPSRequestManager::SystemTimeChange()
       
   216     {
       
   217     iLastFix->Reset();
       
   218     
       
   219     //Reset GSV time
       
   220     iFix->ResetGSVTime();
       
   221     }
       
   222 
       
   223 // -----------------------------------------------------------------------------
       
   224 // CBTGPSRequestManager::AddListenerL
       
   225 // -----------------------------------------------------------------------------
       
   226 void CBTGPSRequestManager::AddListenerL(MBTGPSLocationFixListener& aListener)
       
   227     {
       
   228     User::LeaveIfError(iListenerArray.Append(&aListener));
       
   229     }
       
   230         
       
   231 // -----------------------------------------------------------------------------
       
   232 // CBTGPSRequestManager::RemoveListener
       
   233 // -----------------------------------------------------------------------------
       
   234 void CBTGPSRequestManager::RemoveListener(MBTGPSLocationFixListener& aListener)
       
   235     {
       
   236     TInt index = iListenerArray.Find(&aListener);
       
   237     if(index!=KErrNotFound)
       
   238         {
       
   239         iListenerArray.Remove(index);
       
   240         }
       
   241     }
       
   242 
       
   243 // -----------------------------------------------------------------------------
       
   244 // CBTGPSRequestManager::InformListeners
       
   245 // -----------------------------------------------------------------------------
       
   246 void CBTGPSRequestManager::InformListeners()
       
   247     {
       
   248     TRACESTRING("CBTGPSRequestManager::InformListeners start...")
       
   249 
       
   250     //Inform listeners
       
   251     TInt count = iListenerArray.Count();
       
   252     for(TInt i=count-1; i>=0; i--)
       
   253         {
       
   254         iListenerArray[i]->HandleLocationFixUpdate(*iFix);
       
   255         }
       
   256         
       
   257     //If fix is valid then save it to last fix
       
   258     if(iFix->IsFixValid())
       
   259         {
       
   260         *iLastFix = *iFix;
       
   261         }
       
   262         
       
   263     //Reset fix after listeners are informed.
       
   264     iFix->ResetFix();
       
   265     
       
   266     //Set previous bottom of NMEA buffer
       
   267     iPrevBottom = iNmeaBuffer->CurrentIndex();
       
   268 
       
   269     TRACESTRING("CBTGPSRequestManager::InformListeners end")
       
   270     }
       
   271 
       
   272 // -----------------------------------------------------------------------------
       
   273 // CBTGPSRequestManager::GetPreviousNmeaBufferBottom
       
   274 // -----------------------------------------------------------------------------
       
   275 TInt CBTGPSRequestManager::GetPreviousNmeaBufferBottom() const
       
   276     {
       
   277     return iPrevBottom;
       
   278     }
       
   279 
       
   280 
       
   281 // -----------------------------------------------------------------------------
       
   282 // CBTGPSRequestManager::TimerTick
       
   283 // -----------------------------------------------------------------------------
       
   284 void CBTGPSRequestManager::TimerTick()
       
   285     {
       
   286     //Deliver fix even the satellite information is not valid
       
   287     TRACESTRING("CBTGPSRequestManager::TimerTick GSV information not valid")
       
   288 
       
   289     InformListeners();
       
   290     iTimer->Cancel();
       
   291     }
       
   292        
       
   293 // -----------------------------------------------------------------------------
       
   294 // CBTGPSRequestManager::TimerCallback
       
   295 // -----------------------------------------------------------------------------
       
   296 TInt CBTGPSRequestManager::TimerCallback(TAny* aAny)
       
   297     {
       
   298     reinterpret_cast<CBTGPSRequestManager*>(aAny)->TimerTick();
       
   299     return KErrNone; //Stop periodic timer
       
   300     }
       
   301 
       
   302 //  End of File
       
   303 
       
   304 
       
   305