bluetoothengine/btmac/src/ATCodec/atcparam.cpp
changeset 0 f63038272f30
equal deleted inserted replaced
-1:000000000000 0:f63038272f30
       
     1 /*
       
     2 * Copyright (c) 2005 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:  The parameter of an AT command. 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 
       
    21 #include <s32strm.h>
       
    22 #include "atcodec.h"
       
    23 #include "debug.h"
       
    24 
       
    25 const TUint KCharDoubleQuote = (TUint) '"';
       
    26 _LIT8(KDesDoubleQuote, "\"");
       
    27 
       
    28 // ================= MEMBER FUNCTIONS =======================
       
    29 
       
    30 EXPORT_C TATParam::TATParam()
       
    31     : iValue(KNullDesC8), iValueInt(0), iType(EATNullParam)
       
    32     {
       
    33     }
       
    34     
       
    35 EXPORT_C TATParam::TATParam(const TDesC8& aValue, TATParamType aType)
       
    36     : iValueInt(0), iType(aType)
       
    37     {
       
    38     iValue.Copy(aValue.Left(iValue.MaxLength()));   // to prevent panic for exceeding iValue length
       
    39     iValue.Trim();
       
    40     if (EATDQStringParam == aType)
       
    41         AddDQ();
       
    42     TRACE_INFO((_L8("param type %d, '%S'"), iType, &iValue))
       
    43     }
       
    44 
       
    45 EXPORT_C TATParam::TATParam(TInt aValue)
       
    46     : iValueInt(aValue), iType(EATIntParam)
       
    47     {
       
    48     iValue.Num(aValue, EDecimal);
       
    49     TRACE_INFO((_L8("param type %d, '%S'"), iType, &iValue))
       
    50     }
       
    51 
       
    52 // -----------------------------------------------------------------------------
       
    53 // TATParam::Int
       
    54 // -----------------------------------------------------------------------------
       
    55 EXPORT_C TInt TATParam::Int(TInt& aValue) const
       
    56     {
       
    57     if (iType == EATIntParam)
       
    58         {
       
    59         aValue = iValueInt;
       
    60         return KErrNone;
       
    61         }
       
    62     return KErrNotFound;
       
    63     }
       
    64 
       
    65 // -----------------------------------------------------------------------------
       
    66 // TATParam::Str
       
    67 // -----------------------------------------------------------------------------
       
    68 EXPORT_C const TDesC8& TATParam::Des() const
       
    69     {
       
    70     return iValue;
       
    71     }
       
    72 
       
    73 // -----------------------------------------------------------------------------
       
    74 // TATParam::Type
       
    75 // -----------------------------------------------------------------------------
       
    76 EXPORT_C TATParamType TATParam::Type() const
       
    77     {
       
    78     return iType;
       
    79     }
       
    80 
       
    81 // -----------------------------------------------------------------------------
       
    82 // TATParam::SetValue
       
    83 // -----------------------------------------------------------------------------
       
    84 EXPORT_C TInt TATParam::SetValue(const TDesC8& aValue, TATParamType aType)
       
    85     {
       
    86     if (aValue.Length() > KMaxATParamSize)
       
    87         {
       
    88         return KErrArgument;
       
    89         }
       
    90         
       
    91     iType = aType;
       
    92     TInt err = KErrNone;
       
    93     switch (iType)
       
    94         {
       
    95         case EATNullParam:
       
    96             {
       
    97             iValue.Zero();
       
    98             break;
       
    99             }
       
   100         case EATIntParam:
       
   101             {
       
   102             TLex8 lex(aValue);
       
   103             err = lex.Val(iValueInt);
       
   104             if (!err)
       
   105                 {
       
   106                 iValue.Copy(aValue);
       
   107                 iValue.Trim();
       
   108                 }
       
   109             break;
       
   110             }
       
   111         case EATStringParam:
       
   112             {
       
   113             iValue.Copy(aValue);
       
   114             iValue.Trim();
       
   115             break;
       
   116             }
       
   117         case EATDQStringParam:
       
   118             {
       
   119             iValue.Copy(aValue);
       
   120             iValue.Trim();
       
   121             AddDQ();
       
   122             break;
       
   123             }
       
   124         }
       
   125     TRACE_INFO((_L8("param type %d, '%S'"), iType, &iValue))
       
   126     return err;
       
   127     }
       
   128 
       
   129 // -----------------------------------------------------------------------------
       
   130 // TATParam::SetValue
       
   131 // -----------------------------------------------------------------------------
       
   132 EXPORT_C void TATParam::SetInt(TInt aValue)
       
   133     {
       
   134     iValueInt = aValue;
       
   135     iValue.Num(aValue, EDecimal);
       
   136     iType = EATIntParam;
       
   137     }
       
   138 
       
   139 EXPORT_C void TATParam::ExternalizeL(RWriteStream& aStream) const
       
   140     {
       
   141     aStream << iValue;
       
   142     aStream.WriteInt32L(KStreamVersion1);
       
   143     aStream.WriteInt32L(iValueInt);
       
   144     aStream.WriteInt32L(iType);
       
   145     }
       
   146         
       
   147 EXPORT_C void TATParam::InternalizeL(RReadStream& aStream)
       
   148     {
       
   149     TInt32 version = aStream.ReadInt32L();
       
   150     
       
   151     if (version == KStreamVersion1)
       
   152         {
       
   153         aStream >> iValue;
       
   154         iValueInt = aStream.ReadInt32L();
       
   155         iType = static_cast<TATParamType>(aStream.ReadInt32L());
       
   156         }
       
   157     }
       
   158 
       
   159 void TATParam::AddDQ()
       
   160     {
       
   161     if (iValue.Length())
       
   162         {
       
   163         if (iValue[0] != KCharDoubleQuote)
       
   164             {
       
   165             iValue.Insert(0, KDesDoubleQuote);
       
   166             }
       
   167         if (iValue[iValue.Length() - 1] != KCharDoubleQuote)
       
   168             {
       
   169             iValue.Append(KDesDoubleQuote);
       
   170             }
       
   171         }
       
   172     else
       
   173         {
       
   174         iValue.Append(KDesDoubleQuote);
       
   175         iValue.Append(KDesDoubleQuote);
       
   176         }
       
   177     }
       
   178 
       
   179 // End of file