datasourcemodules/bluetoothgpspositioningmodule/btgpspsy/src/Utils/BTGPSNmeaBuffer.cpp
changeset 36 b47902b73a93
parent 0 9cfd9a3ee49c
equal deleted inserted replaced
35:a2efdd544abf 36:b47902b73a93
       
     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 "BTGPSNmeaBuffer.h"
       
    20 #include "BTGPSMessageDef.h"
       
    21 #include "BTGPSPanic.h"
       
    22 
       
    23 // EXTERNAL DATA STRUCTURES
       
    24 
       
    25 // EXTERNAL FUNCTION PROTOTYPES  
       
    26 
       
    27 // CONSTANTS
       
    28 
       
    29 // Granularity
       
    30 
       
    31 // MACROS
       
    32 
       
    33 // LOCAL CONSTANTS AND MACROS
       
    34 
       
    35 // MODULE DATA STRUCTURES
       
    36 
       
    37 // LOCAL FUNCTION PROTOTYPES
       
    38 
       
    39 // FORWARD DECLARATIONS
       
    40 
       
    41 // ============================= LOCAL FUNCTIONS ===============================
       
    42 
       
    43 // ============================ MEMBER FUNCTIONS ===============================
       
    44 
       
    45 
       
    46 // -----------------------------------------------------------------------------
       
    47 // CBTGPSNmeaBuffer::NewL
       
    48 // -----------------------------------------------------------------------------
       
    49 CBTGPSNmeaBuffer* CBTGPSNmeaBuffer::NewL(TInt aSize)
       
    50     {
       
    51     CBTGPSNmeaBuffer* self = new (ELeave) CBTGPSNmeaBuffer();
       
    52     CleanupStack::PushL(self);
       
    53     self->ConstructL(aSize);
       
    54     CleanupStack::Pop();
       
    55     return self;
       
    56     }
       
    57 
       
    58 // -----------------------------------------------------------------------------
       
    59 // CBTGPSNmeaBuffer::~CBTGPSNmeaBuffer
       
    60 // -----------------------------------------------------------------------------
       
    61 CBTGPSNmeaBuffer::~CBTGPSNmeaBuffer()
       
    62     {
       
    63     delete iBuffer;
       
    64     }
       
    65 
       
    66 // -----------------------------------------------------------------------------
       
    67 // CBTGPSNmeaBuffer::ConstructL
       
    68 // -----------------------------------------------------------------------------
       
    69 void CBTGPSNmeaBuffer::ConstructL(TInt aSize)
       
    70     {
       
    71     iBuffer = (TUint8*)User::AllocZL(aSize);
       
    72     iSize = aSize;
       
    73     }
       
    74 
       
    75 // -----------------------------------------------------------------------------
       
    76 // CBTGPSNmeaBuffer::CBTGPSNmeaBuffer
       
    77 // C++ default constructor can NOT contain any code, that
       
    78 // might leave.
       
    79 // -----------------------------------------------------------------------------
       
    80 CBTGPSNmeaBuffer::CBTGPSNmeaBuffer()
       
    81     {
       
    82     }
       
    83 
       
    84 // -----------------------------------------------------------------------------
       
    85 // CBTGPSNmeaBuffer::ResetBuffer
       
    86 // -----------------------------------------------------------------------------
       
    87 void CBTGPSNmeaBuffer::ResetBuffer()
       
    88     {
       
    89     Mem::FillZ(iBuffer,iSize);
       
    90     iBottom = 0;
       
    91     }
       
    92 
       
    93 // -----------------------------------------------------------------------------
       
    94 // CBTGPSNmeaBuffer::CurrentIndex
       
    95 // -----------------------------------------------------------------------------
       
    96 TInt CBTGPSNmeaBuffer::CurrentIndex() const
       
    97     {
       
    98     return iBottom;
       
    99     }
       
   100 
       
   101 // -----------------------------------------------------------------------------
       
   102 // CBTGPSNmeaBuffer::AddSentences
       
   103 // -----------------------------------------------------------------------------
       
   104 void CBTGPSNmeaBuffer::AddSentences(const TDesC8& aNmea)
       
   105     {
       
   106     TPtrC8 srcDes(aNmea);
       
   107     TInt srcLen = srcDes.Length();
       
   108     
       
   109     while(srcLen>0)
       
   110         {
       
   111         TInt maxSize = iSize-iBottom;
       
   112         TPtr8 ptr(iBuffer+iBottom, maxSize);
       
   113         TInt cpLen = Min(maxSize,srcLen);
       
   114         ptr.Copy(srcDes.Left(cpLen));
       
   115         iBottom = (iBottom+cpLen)%iSize;
       
   116         srcDes.Set(srcDes.Right(srcLen-cpLen));
       
   117         srcLen = srcDes.Length();
       
   118         }
       
   119     }
       
   120 
       
   121 // -----------------------------------------------------------------------------
       
   122 // CBTGPSNmeaBuffer::ReadSentences
       
   123 // -----------------------------------------------------------------------------
       
   124 TInt CBTGPSNmeaBuffer::ReadSentences(
       
   125             TDes8& aDest, 
       
   126             TInt& aBeginning) const
       
   127     {
       
   128     __ASSERT_DEBUG(aBeginning<iSize, Panic(EPanicIndexOutOfRange));
       
   129     
       
   130     //Clear destination descriptor
       
   131     aDest.Zero();
       
   132 
       
   133     TBool started = EFalse;
       
   134     TBool ended = EFalse;
       
   135     
       
   136     if(aBeginning==iBottom)
       
   137         {
       
   138         return KErrEof;
       
   139         }
       
   140     
       
   141     if(aBeginning == KBTGPSNmeaIndexNotSet)
       
   142         {
       
   143         if(iBuffer[iBottom]!=0)
       
   144             {
       
   145             //Buffer has been rotated
       
   146             aBeginning = iBottom;
       
   147             }
       
   148         else
       
   149             {
       
   150             //Read from 0 to iBottom
       
   151             aBeginning = 0;
       
   152             if(iBottom == 0)
       
   153                 {
       
   154                 return KErrEof;
       
   155                 }
       
   156             }
       
   157         }
       
   158         
       
   159     do
       
   160         {
       
   161         TUint8 nextChar = iBuffer[aBeginning];
       
   162         if(nextChar == KNmeaSentenceLead)
       
   163             {
       
   164             //Mark start of NMEA sentences
       
   165             started = ETrue;
       
   166             }
       
   167             
       
   168         if(started && aDest.Length()<aDest.MaxLength())
       
   169             {
       
   170             aDest.Append(nextChar);
       
   171             }
       
   172             
       
   173         aBeginning ++;
       
   174         aBeginning = aBeginning%iSize;
       
   175 
       
   176         if(started && nextChar == KNmeaSentenceTerminator2)
       
   177             {
       
   178             //Mark end of the sentence.
       
   179             ended = ETrue;
       
   180             }
       
   181         }while(aBeginning!=iBottom && !ended);
       
   182     
       
   183     
       
   184     if(aDest.Length()!=0)
       
   185         {
       
   186         return KErrNone;
       
   187         }
       
   188         
       
   189     return KErrEof;
       
   190     }
       
   191         
       
   192 
       
   193 //  End of File
       
   194 
       
   195 
       
   196