natfw/natfwunsaf_protocols/unsaf_codec/src/natfwunsafunknownattributesattribute.cpp
changeset 0 1bce908db942
equal deleted inserted replaced
-1:000000000000 0:1bce908db942
       
     1 /*
       
     2 * Copyright (c) 2006-2007 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:   
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 #include <es_sock.h>
       
    22 #include "natfwunsafunknownattributesattribute.h"
       
    23 #include "natfwunsafutils.h"
       
    24 
       
    25 // -----------------------------------------------------------------------------
       
    26 // CNATFWUNSAFUnknownAttributesAttribute::NewL
       
    27 // -----------------------------------------------------------------------------
       
    28 //
       
    29 EXPORT_C CNATFWUNSAFUnknownAttributesAttribute*
       
    30 CNATFWUNSAFUnknownAttributesAttribute::NewL()
       
    31     {
       
    32     CNATFWUNSAFUnknownAttributesAttribute* self =
       
    33         CNATFWUNSAFUnknownAttributesAttribute::NewLC();
       
    34     CleanupStack::Pop(self);
       
    35     return self;
       
    36     }
       
    37 
       
    38 // -----------------------------------------------------------------------------
       
    39 // CNATFWUNSAFUnknownAttributesAttribute::NewLC
       
    40 // -----------------------------------------------------------------------------
       
    41 //
       
    42 EXPORT_C CNATFWUNSAFUnknownAttributesAttribute*
       
    43 CNATFWUNSAFUnknownAttributesAttribute::NewLC()
       
    44     {
       
    45     CNATFWUNSAFUnknownAttributesAttribute* self =
       
    46         new (ELeave) CNATFWUNSAFUnknownAttributesAttribute();
       
    47     CleanupStack::PushL(self);
       
    48     return self;
       
    49     }
       
    50 
       
    51 // -----------------------------------------------------------------------------
       
    52 // CNATFWUNSAFUnknownAttributesAttribute::DecodeAttributeL
       
    53 // -----------------------------------------------------------------------------
       
    54 //
       
    55 CNATFWUNSAFAttribute*
       
    56 CNATFWUNSAFUnknownAttributesAttribute::DecodeAttributeL(
       
    57     const TDesC8& aByteStream)
       
    58     {
       
    59     TUint16 valueLength = ParseLengthL(aByteStream);
       
    60 
       
    61     //Stream has to have enough data for the whole value element
       
    62     __ASSERT_ALWAYS(aByteStream.Length() >= EValueOffset + valueLength,
       
    63                     User::Leave(KErrArgument));
       
    64 
       
    65     //Check the value length is valid for this attribute
       
    66     CheckLengthL(valueLength);
       
    67 
       
    68     CNATFWUNSAFUnknownAttributesAttribute* self =
       
    69         new (ELeave) CNATFWUNSAFUnknownAttributesAttribute();
       
    70     CleanupStack::PushL(self);
       
    71 
       
    72     self->DecodeValueL(aByteStream.Mid(EValueOffset, valueLength));
       
    73     CleanupStack::Pop(self);
       
    74     return self;
       
    75     }
       
    76 
       
    77 // -----------------------------------------------------------------------------
       
    78 // CNATFWUNSAFUnknownAttributesAttribute::CNATFWUNSAFUnknownAttributesAttribute
       
    79 // -----------------------------------------------------------------------------
       
    80 //
       
    81 CNATFWUNSAFUnknownAttributesAttribute::CNATFWUNSAFUnknownAttributesAttribute() :
       
    82     CNATFWUNSAFAttribute()
       
    83 #ifdef TEST_EUNIT
       
    84     , iContainedAttributes(1)
       
    85 #endif
       
    86     {
       
    87     }
       
    88 
       
    89 // -----------------------------------------------------------------------------
       
    90 // CNATFWUNSAFUnknownAttributesAttribute::~CNATFWUNSAFUnknownAttributesAttribute
       
    91 // -----------------------------------------------------------------------------
       
    92 //
       
    93 EXPORT_C CNATFWUNSAFUnknownAttributesAttribute::
       
    94 ~CNATFWUNSAFUnknownAttributesAttribute()
       
    95     {
       
    96     iContainedAttributes.Close();
       
    97     }
       
    98 
       
    99 // -----------------------------------------------------------------------------
       
   100 // CNATFWUNSAFUnknownAttributesAttribute::Type
       
   101 // -----------------------------------------------------------------------------
       
   102 //
       
   103 CNATFWUNSAFAttribute::TType CNATFWUNSAFUnknownAttributesAttribute::Type() const
       
   104     {
       
   105     __TEST_INVARIANT;
       
   106 
       
   107     return EUnknownAttributes;
       
   108     }
       
   109 
       
   110 // -----------------------------------------------------------------------------
       
   111 // CNATFWUNSAFUnknownAttributesAttribute::EncodeValueL
       
   112 // If the number of attributes is an odd number, one attribute is repeated.
       
   113 // -----------------------------------------------------------------------------
       
   114 //
       
   115 HBufC8* CNATFWUNSAFUnknownAttributesAttribute::EncodeValueL() const
       
   116     {
       
   117     __TEST_INVARIANT;
       
   118 
       
   119     TInt count = iContainedAttributes.Count();
       
   120     if (count & 0x1)
       
   121         {
       
   122         count++;
       
   123         }
       
   124 
       
   125     HBufC8* encodedValue = HBufC8::NewLC(count * EElementSize);
       
   126     TPtr8 ptr = encodedValue->Des();
       
   127     ptr.FillZ(count * EElementSize);
       
   128 
       
   129     for (TInt i = 0; i < iContainedAttributes.Count(); ++i)
       
   130         {
       
   131         TUint16 value = static_cast<TUint16>(iContainedAttributes[i]);
       
   132         NATFWUNSAFUtils::WriteNetworkOrder16L(ptr,
       
   133                                          i * EElementSize,
       
   134                                          value);
       
   135         }
       
   136     if (count > iContainedAttributes.Count())
       
   137         {
       
   138         TUint16 value = static_cast<TUint16>(iContainedAttributes[0]);
       
   139         NATFWUNSAFUtils::WriteNetworkOrder16L(ptr,
       
   140                                  iContainedAttributes.Count() * EElementSize,
       
   141                                  value);
       
   142         }
       
   143 
       
   144     CleanupStack::Pop(encodedValue);
       
   145     return encodedValue;
       
   146     }
       
   147 
       
   148 // -----------------------------------------------------------------------------
       
   149 // CNATFWUNSAFUnknownAttributesAttribute::AddContainedAttributeL
       
   150 // -----------------------------------------------------------------------------
       
   151 //
       
   152 void CNATFWUNSAFUnknownAttributesAttribute::AddContainedAttributeL(
       
   153     TUint16 aAttributeType)
       
   154     {
       
   155     __ASSERT_ALWAYS(iContainedAttributes.Find(aAttributeType) == KErrNotFound,
       
   156                     User::Leave(KErrAlreadyExists));
       
   157 
       
   158     iContainedAttributes.AppendL(aAttributeType);
       
   159     }
       
   160 
       
   161 // -----------------------------------------------------------------------------
       
   162 // CNATFWUNSAFUnknownAttributesAttribute::RemoveContainedAttribute
       
   163 // -----------------------------------------------------------------------------
       
   164 //
       
   165 TInt CNATFWUNSAFUnknownAttributesAttribute::RemoveContainedAttribute(
       
   166     TUint16 aAttributeType)
       
   167     {
       
   168     TInt index = iContainedAttributes.Find(aAttributeType);
       
   169 
       
   170     if (index == KErrNotFound)
       
   171         {
       
   172         return KErrNotFound;
       
   173         }
       
   174 
       
   175     iContainedAttributes.Remove(index);
       
   176     return KErrNone;
       
   177     }
       
   178 
       
   179 // -----------------------------------------------------------------------------
       
   180 // CNATFWUNSAFUnknownAttributesAttribute::ContainedAttributes
       
   181 // -----------------------------------------------------------------------------
       
   182 //
       
   183 EXPORT_C const RArray<TUint32>& CNATFWUNSAFUnknownAttributesAttribute::
       
   184 ContainedAttributes() const
       
   185     {
       
   186     __TEST_INVARIANT;
       
   187     return iContainedAttributes;
       
   188     }
       
   189 
       
   190 // -----------------------------------------------------------------------------
       
   191 // CNATFWUNSAFUnknownAttributesAttribute::CheckLengthL
       
   192 // -----------------------------------------------------------------------------
       
   193 //
       
   194 void CNATFWUNSAFUnknownAttributesAttribute::CheckLengthL(TInt aLength)
       
   195     {
       
   196     __ASSERT_ALWAYS((aLength % EGranularity) == 0, User::Leave(KErrCorrupt));
       
   197     }
       
   198 
       
   199 // -----------------------------------------------------------------------------
       
   200 // CNATFWUNSAFUnknownAttributesAttribute::DecodeValueL
       
   201 // If the number of attributes would be an odd number, one attribute is
       
   202 // repeated. The repeated attribute is not placed into iContainedAttributes
       
   203 // array.
       
   204 // -----------------------------------------------------------------------------
       
   205 //
       
   206 void CNATFWUNSAFUnknownAttributesAttribute::DecodeValueL(const TDesC8& aValue)
       
   207     {
       
   208     for (TInt pos = 0; pos < aValue.Length(); pos = pos + EElementSize)
       
   209         {
       
   210         TUint attrType = BigEndian::Get16(aValue.Mid(pos).Ptr());
       
   211 
       
   212         if (iContainedAttributes.Find(attrType) == KErrNotFound)
       
   213             {
       
   214             iContainedAttributes.AppendL(attrType);
       
   215             }
       
   216         }
       
   217     }
       
   218 
       
   219 // -----------------------------------------------------------------------------
       
   220 // CNATFWUNSAFUnknownAttributesAttribute::__DbgTestInvariant
       
   221 // -----------------------------------------------------------------------------
       
   222 //
       
   223 void CNATFWUNSAFUnknownAttributesAttribute::__DbgTestInvariant() const
       
   224     {
       
   225 #if defined(_DEBUG)
       
   226 /*    if ( error )
       
   227         {
       
   228         User::Invariant();
       
   229         } */
       
   230 #endif
       
   231     }