datasourcemodules/bluetoothgpspositioningmodule/btgpspsy/src/Utils/BTGPSMessageReceiver.cpp
changeset 0 9cfd9a3ee49c
equal deleted inserted replaced
-1:000000000000 0:9cfd9a3ee49c
       
     1 // Copyright (c) 2004-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 <es_sock.h>
       
    21 #include "BTGPSMessageReceiver.h"
       
    22 #include "BTGPSMessageListener.h"
       
    23 #include "BTGPSDeviceManager.h"
       
    24 #include "BTGPSMessageDef.h"
       
    25 #include "BTGPSLogging.h"
       
    26 #include "BTGPSNmeaParser.h"
       
    27 
       
    28 // EXTERNAL DATA STRUCTURES
       
    29 
       
    30 // EXTERNAL FUNCTION PROTOTYPES  
       
    31 
       
    32 // CONSTANTS
       
    33 
       
    34 // MACROS
       
    35 
       
    36 // LOCAL CONSTANTS AND MACROS
       
    37 
       
    38 // MODULE DATA STRUCTURES
       
    39 
       
    40 // LOCAL FUNCTION PROTOTYPES
       
    41 
       
    42 // FORWARD DECLARATIONS
       
    43 
       
    44 // ============================= LOCAL FUNCTIONS ===============================
       
    45 
       
    46 // ============================ MEMBER FUNCTIONS ===============================
       
    47 
       
    48 
       
    49 // -----------------------------------------------------------------------------
       
    50 // CBTGPSMessageReceiver::NewL
       
    51 // -----------------------------------------------------------------------------
       
    52 CBTGPSMessageReceiver* CBTGPSMessageReceiver::NewL(
       
    53     RSocket& aSocket,
       
    54     CBTGPSDeviceManager& aDeviceManager)
       
    55     {
       
    56     CBTGPSMessageReceiver* self = new (ELeave) CBTGPSMessageReceiver(
       
    57         aSocket,
       
    58         aDeviceManager);
       
    59     CleanupStack::PushL(self);
       
    60     self->ConstructL();
       
    61     CleanupStack::Pop();
       
    62     return self;
       
    63     }
       
    64 
       
    65 // -----------------------------------------------------------------------------
       
    66 // CBTGPSMessageReceiver::~CBTGPSMessageReceiver
       
    67 // -----------------------------------------------------------------------------
       
    68 CBTGPSMessageReceiver::~CBTGPSMessageReceiver()
       
    69     {
       
    70     Cancel();    
       
    71     iDeviceManager.RemoveListener(*this);
       
    72     iListenerArray.Close();
       
    73     }
       
    74 
       
    75 // -----------------------------------------------------------------------------
       
    76 // CBTGPSMessageReceiver::ConstructL
       
    77 // -----------------------------------------------------------------------------
       
    78 void CBTGPSMessageReceiver::ConstructL()
       
    79     {
       
    80     iDeviceManager.AddListenerL(*this);
       
    81     }
       
    82 
       
    83 // -----------------------------------------------------------------------------
       
    84 // CBTGPSMessageReceiver::CBTGPSMessageReceiver
       
    85 // C++ default constructor can NOT contain any code, that
       
    86 // might leave.
       
    87 // -----------------------------------------------------------------------------
       
    88 CBTGPSMessageReceiver::CBTGPSMessageReceiver(
       
    89         RSocket& aSocket,
       
    90         CBTGPSDeviceManager& aDeviceManager)
       
    91     : CActive(EPriorityNormal),
       
    92       iSocket(aSocket),
       
    93       iDeviceManager(aDeviceManager)
       
    94     {
       
    95     CActiveScheduler::Add(this);
       
    96     }
       
    97 
       
    98 // -----------------------------------------------------------------------------
       
    99 // CBTGPSMessageReceiver::AddMessageListenerL
       
   100 // -----------------------------------------------------------------------------
       
   101 void CBTGPSMessageReceiver::AddMessageListenerL(MBTGPSMessageListener& aListener)
       
   102     {
       
   103     //Add listener to array
       
   104     User::LeaveIfError(iListenerArray.Append(&aListener));
       
   105 
       
   106     //Start listening if device is connected
       
   107     if(!IsActive() && iDeviceManager.DeviceConnectStatus() == EBTDeviceConnected)
       
   108         {
       
   109         Receive();
       
   110         }
       
   111     }
       
   112 
       
   113 // -----------------------------------------------------------------------------
       
   114 // CBTGPSMessageReceiver::AddMessageListenerL
       
   115 // -----------------------------------------------------------------------------
       
   116 void CBTGPSMessageReceiver::RemoveMessageListener(MBTGPSMessageListener& aListener)
       
   117     {
       
   118     //remove listener from array
       
   119     TInt index = iListenerArray.Find(&aListener);
       
   120     if(index!=KErrNotFound)
       
   121         {
       
   122         iListenerArray.Remove(index);
       
   123         }
       
   124         
       
   125     //If no listener anymore, then remove receiving
       
   126     if(iListenerArray.Count()==0)
       
   127         {
       
   128         Cancel();
       
   129         }
       
   130     }
       
   131 
       
   132 // -----------------------------------------------------------------------------
       
   133 // CBTGPSMessageReceiver::RunL
       
   134 // -----------------------------------------------------------------------------
       
   135 void CBTGPSMessageReceiver::RunL()
       
   136     {
       
   137     TRACESTRING2("CBTGPSMessageReceiver::RunL start...%d", iStatus.Int())
       
   138     if(iStatus == KErrNone)
       
   139         {
       
   140         HandleReceivedMessage();
       
   141         Receive();
       
   142         }
       
   143     else
       
   144         {
       
   145         //If connecting error then we report connecting status
       
   146         iDeviceManager.SetBTDeviceConnectStatus(
       
   147             EBTDeviceDisconnected,
       
   148             iStatus.Int());
       
   149         }
       
   150     TRACESTRING("CBTGPSMessageReceiver::RunL end")
       
   151     }
       
   152         
       
   153 // -----------------------------------------------------------------------------
       
   154 // CBTGPSMessageReceiver::DoCancel
       
   155 // -----------------------------------------------------------------------------
       
   156 void CBTGPSMessageReceiver::DoCancel()
       
   157     {
       
   158     iSocket.CancelRecv();
       
   159     }
       
   160         
       
   161 // -----------------------------------------------------------------------------
       
   162 // CBTGPSMessageReceiver::RunError
       
   163 // -----------------------------------------------------------------------------
       
   164 TInt CBTGPSMessageReceiver::RunError(TInt /*aError*/)
       
   165     {
       
   166     //should never be called
       
   167     return KErrNone;
       
   168     }
       
   169 
       
   170 // -----------------------------------------------------------------------------
       
   171 // CBTGPSMessageReceiver::BTDeviceStatusChanged
       
   172 // -----------------------------------------------------------------------------
       
   173 void CBTGPSMessageReceiver::BTDeviceStatusChanged(
       
   174             TInt aConnectStatus, 
       
   175             TInt /*aDeviceType*/,
       
   176             TInt /*aErr*/)
       
   177     {
       
   178     //Start listening if device is connected
       
   179     if( aConnectStatus == EBTDeviceConnected 
       
   180         && iListenerArray.Count()!=0)
       
   181         {
       
   182         Receive();
       
   183         }
       
   184     else
       
   185         {
       
   186         Cancel();
       
   187         }
       
   188     }
       
   189 
       
   190 // -----------------------------------------------------------------------------
       
   191 // CBTGPSMessageReceiver::Receive
       
   192 // -----------------------------------------------------------------------------
       
   193 void CBTGPSMessageReceiver::Receive()
       
   194     {
       
   195     if(!IsActive())
       
   196         {
       
   197         iSocket.RecvOneOrMore(iBuffer, 0, iStatus, iRecLen);
       
   198         SetActive();
       
   199         }
       
   200     }
       
   201 
       
   202 // -----------------------------------------------------------------------------
       
   203 // CBTGPSMessageReceiver::HandleReceivedMessage
       
   204 // -----------------------------------------------------------------------------
       
   205 void CBTGPSMessageReceiver::HandleReceivedMessage()
       
   206     {
       
   207     for(TInt i=0; i<iBuffer.Length(); i++)
       
   208         {
       
   209         TUint8 next = iBuffer[i];
       
   210         TInt nmeaBufLen = iNmeaBuf.Length();
       
   211         if((next==KNmeaSentenceTerminator1 || 
       
   212             next == KNmeaSentenceTerminator2))
       
   213             {
       
   214             if(nmeaBufLen>0)
       
   215                 {
       
   216                 //We have a valid nmea message. Inform listeners
       
   217                 InformListeners();
       
   218                 iNmeaBuf.Zero();
       
   219                 }
       
   220             }
       
   221         else
       
   222             {
       
   223             if(nmeaBufLen>=KMaxNmeaMessageSize)
       
   224                 {
       
   225                 //Empty nmea message buffer if it's overflow
       
   226                 iNmeaBuf.Zero();
       
   227                 }
       
   228             iNmeaBuf.Append(next);
       
   229             }
       
   230         }
       
   231     //Empty receiving buffer
       
   232     iBuffer.Zero();
       
   233     }
       
   234 
       
   235 // -----------------------------------------------------------------------------
       
   236 // CBTGPSMessageReceiver::InformListeners
       
   237 // -----------------------------------------------------------------------------
       
   238 void CBTGPSMessageReceiver::InformListeners()
       
   239     {
       
   240     TRACESTRING("CBTGPSMessageReceiver::InformListeners start...")
       
   241     TRACESTRING8(iNmeaBuf)
       
   242     TBTGPSNmeaParser parser;
       
   243     parser.SetNmeaSentence(iNmeaBuf);
       
   244     if(parser.IsValid())
       
   245         {
       
   246         TInt count = iListenerArray.Count();
       
   247         for(TInt i=count-1; i>=0; i--)
       
   248             {
       
   249             iListenerArray[i]->HandleMessage(parser);
       
   250             }
       
   251         }
       
   252     TRACESTRING("CBTGPSMessageReceiver::InformListeners end")
       
   253     }
       
   254 
       
   255 //  End of File
       
   256 
       
   257 
       
   258