datasourcemodules/bluetoothgpspositioningmodule/btgpspsy/src/Init/BTGPSNokGpsDetector.cpp
changeset 0 9cfd9a3ee49c
equal deleted inserted replaced
-1:000000000000 0:9cfd9a3ee49c
       
     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 "BTGPSNokGpsDetector.h"
       
    21 #include "BTGPSDeviceManager.h"
       
    22 #include "BTGPSMessageDef.h"
       
    23 #include "BTGPSConnectManager.h"
       
    24 #include "BTGPSConstantsManager.h"
       
    25 #include "BTGPSRequestHandler.h"
       
    26 #include "BTGPSLogging.h"
       
    27 #include "BTGPSNmeaParser.h"
       
    28 
       
    29 // EXTERNAL DATA STRUCTURES
       
    30 
       
    31 // EXTERNAL FUNCTION PROTOTYPES  
       
    32 
       
    33 // CONSTANTS
       
    34 
       
    35 // Timer for sending switch to NMEA mode
       
    36 const TInt KBTGPSSwitchToNmeaModeTimer = 300000; //300ms
       
    37 
       
    38 // Limit of Switch to NMEA mode message count. If not enough valid
       
    39 // NMEA message was received, then the device is not a GPS device
       
    40 const TInt KBTGPSSwitchToNmeaModeLimit = 10; 
       
    41 
       
    42 // Number of PNOK message that will be sent
       
    43 const TInt KBTGPSNumberOfPnokDetectMsg = 3;
       
    44 
       
    45 
       
    46 
       
    47 // MACROS
       
    48 
       
    49 // LOCAL CONSTANTS AND MACROS
       
    50 
       
    51 // MODULE DATA STRUCTURES
       
    52 
       
    53 // LOCAL FUNCTION PROTOTYPES
       
    54 
       
    55 // FORWARD DECLARATIONS
       
    56 
       
    57 // ============================= LOCAL FUNCTIONS ===============================
       
    58 
       
    59 // ============================ MEMBER FUNCTIONS ===============================
       
    60 
       
    61 
       
    62 // -----------------------------------------------------------------------------
       
    63 // CBTGPSNokGpsDetector::NewL
       
    64 // -----------------------------------------------------------------------------
       
    65 CBTGPSNokGpsDetector* CBTGPSNokGpsDetector::NewL(
       
    66         CBTGPSConnectManager& aConnectManager,
       
    67         CBTGPSDeviceManager& aDeviceManager)
       
    68     {
       
    69     CBTGPSNokGpsDetector* self = new (ELeave) CBTGPSNokGpsDetector(
       
    70         aConnectManager,
       
    71         aDeviceManager);
       
    72     CleanupStack::PushL(self);
       
    73     self->ConstructL();
       
    74     CleanupStack::Pop();
       
    75     return self;
       
    76     }
       
    77 
       
    78 // -----------------------------------------------------------------------------
       
    79 // CBTGPSNokGpsDetector::~CBTGPSNokGpsDetector
       
    80 // -----------------------------------------------------------------------------
       
    81 CBTGPSNokGpsDetector::~CBTGPSNokGpsDetector()
       
    82     {
       
    83     Cancel();
       
    84     iConnectManager.RemoveMessageListener(*this);
       
    85     delete iTimer;
       
    86     }
       
    87 
       
    88 
       
    89 // -----------------------------------------------------------------------------
       
    90 // CBTGPSNokGpsDetector::ConstructL
       
    91 // -----------------------------------------------------------------------------
       
    92 void CBTGPSNokGpsDetector::ConstructL()
       
    93     {
       
    94     iTimer = CPeriodic::NewL(CActive::EPriorityStandard);
       
    95 
       
    96     //Add message listener
       
    97     iConnectManager.AddMessageListenerL(*this);
       
    98 
       
    99     }
       
   100 
       
   101 // -----------------------------------------------------------------------------
       
   102 // CBTGPSNokGpsDetector::CBTGPSNokGpsDetector
       
   103 // C++ default constructor can NOT contain any code, that
       
   104 // might leave.
       
   105 // -----------------------------------------------------------------------------
       
   106 CBTGPSNokGpsDetector::CBTGPSNokGpsDetector(
       
   107         CBTGPSConnectManager& aConnectManager,
       
   108         CBTGPSDeviceManager& aDeviceManager)
       
   109     : iConnectManager(aConnectManager),
       
   110       iDeviceManager(aDeviceManager)
       
   111     {
       
   112     }
       
   113 
       
   114 // -----------------------------------------------------------------------------
       
   115 // CBTGPSNokGpsDetector::Detect
       
   116 // -----------------------------------------------------------------------------
       
   117 TInt CBTGPSNokGpsDetector::Detect()
       
   118     {
       
   119     TRAPD(err, DetectL());
       
   120     return err;
       
   121     }
       
   122 
       
   123 // -----------------------------------------------------------------------------
       
   124 // CBTGPSNokGpsDetector::DetectL
       
   125 // -----------------------------------------------------------------------------
       
   126 void CBTGPSNokGpsDetector::DetectL()
       
   127     {
       
   128     TRACESTRING("CBTGPSNokGpsDetector::DetectL start...")
       
   129     //Start Nok GPS detector if it's not started
       
   130 
       
   131     if(iTimer->IsActive())
       
   132         {
       
   133         return;
       
   134         }
       
   135 
       
   136     //Start timer
       
   137     iTimer->Start(
       
   138         CBTGPSRequestHandler::ConstantsManager().iDelayForPNokGpsDetect, 
       
   139         CBTGPSRequestHandler::ConstantsManager().iDelayForPNokGpsDetect, 
       
   140         TCallBack(TimerCallBack, this));
       
   141     
       
   142     //Send PNok polling message. 
       
   143     iConnectManager.SendMessageL(KNMEAPNOK);
       
   144     iNumPnokPolling = 1;
       
   145     
       
   146     TRACESTRING("CBTGPSNokGpsDetector::DetectL end")
       
   147     }
       
   148 
       
   149 // -----------------------------------------------------------------------------
       
   150 // CBTGPSNokGpsDetector::Cancel
       
   151 // -----------------------------------------------------------------------------
       
   152 void CBTGPSNokGpsDetector::Cancel()
       
   153     {
       
   154     if(iTimer!=NULL)
       
   155         {
       
   156         iTimer->Cancel();
       
   157         }
       
   158     }
       
   159 
       
   160 
       
   161 // -----------------------------------------------------------------------------
       
   162 // CBTGPSNokGpsDetector::HandleMessage
       
   163 // -----------------------------------------------------------------------------
       
   164 void CBTGPSNokGpsDetector::HandleMessage(const TBTGPSNmeaParser& aParser)
       
   165     {
       
   166     if(!iTimer->IsActive())
       
   167         {
       
   168         return;
       
   169         }
       
   170     TInt deviceType = iDeviceManager.DeviceType();
       
   171     if(deviceType==EBTDeviceTypeUnknown && aParser.IsValid())
       
   172         {
       
   173         iValidNmeaMsg++;
       
   174         TInt sentences = CBTGPSRequestHandler::ConstantsManager().iNumberOfTestNmeaSentences;
       
   175         if(iValidNmeaMsg==sentences)
       
   176             {
       
   177             //Non-Nok GPS
       
   178             TRACESTRING("CBTGPSNokGpsDetector::HandleMsg Non Nok Gps")
       
   179             iDeviceManager.SetBTDeviceType(EBTDeviceTypeNonNokGps);
       
   180             }
       
   181         }
       
   182         
       
   183     
       
   184     if(aParser.MessageId() == ENmeaPNokOk)
       
   185         {
       
   186         //The BT device is Nok GPS
       
   187         TRACESTRING("CBTGPSNokGpsDetector::HandleMessage: Nok GPS");
       
   188         iDeviceManager.SetBTDeviceType(EBTDeviceTypeNokGps);
       
   189         Cancel();
       
   190         }
       
   191     }
       
   192 
       
   193 // -----------------------------------------------------------------------------
       
   194 // CBTGPSNokGpsDetector::TimerTick
       
   195 // -----------------------------------------------------------------------------
       
   196 void CBTGPSNokGpsDetector::TimerTick()
       
   197     {
       
   198     //Cancel periodic timer
       
   199     iTimer->Cancel();
       
   200     
       
   201     if(iNumPnokPolling < KBTGPSNumberOfPnokDetectMsg)
       
   202         {
       
   203         iConnectManager.SendMessage(KNMEAPNOK);
       
   204 
       
   205         iNumPnokPolling++;
       
   206         //Start timer
       
   207         iTimer->Start(
       
   208             CBTGPSRequestHandler::ConstantsManager().iDelayForPNokGpsDetect, 
       
   209             CBTGPSRequestHandler::ConstantsManager().iDelayForPNokGpsDetect, 
       
   210             TCallBack(TimerCallBack, this));
       
   211         return;
       
   212         }
       
   213     
       
   214     if(iDeviceManager.DeviceType()!=EBTDeviceTypeUnknown)
       
   215         {
       
   216         //not send switch to NMEA mode message if device
       
   217         //type is known
       
   218         return;
       
   219         }
       
   220 
       
   221     if(iSwitchCount>=KBTGPSSwitchToNmeaModeLimit)
       
   222         {
       
   223         //Not a GPS device
       
   224         TRACESTRING("CBTGPSNokGpsDetector::TimerTick: Not GPS")
       
   225         iDeviceManager.SetBTDeviceType(EBTDeviceTypeNonGPS);
       
   226         }
       
   227     else
       
   228         {
       
   229         //Send switch to NMEA mode message and send set to FullPower mode message
       
   230         iConnectManager.SendMessage(KSirfToNMEA);
       
   231         iSwitchCount++;
       
   232         iTimer->Start(
       
   233             KBTGPSSwitchToNmeaModeTimer,
       
   234             KBTGPSSwitchToNmeaModeTimer,
       
   235             TCallBack(TimerCallBack,this));
       
   236         }
       
   237     }
       
   238         
       
   239 // -----------------------------------------------------------------------------
       
   240 // CBTGPSNokGpsDetector::TimerCallBack
       
   241 // -----------------------------------------------------------------------------
       
   242 TInt CBTGPSNokGpsDetector::TimerCallBack(TAny* aAny)
       
   243     {
       
   244     reinterpret_cast<CBTGPSNokGpsDetector*>(aAny)->TimerTick();
       
   245     return KErrNone;
       
   246     }
       
   247 
       
   248 
       
   249 
       
   250 //  End of File
       
   251 
       
   252 
       
   253