usbmgmt/usbmgr/device/classdrivers/ncm/classimplementation/ncmpktdrv/pktdrv/src/ncmntb16builder.cpp
branchRCL_3
changeset 16 012cc2ee6408
parent 15 f92a4f87e424
equal deleted inserted replaced
15:f92a4f87e424 16:012cc2ee6408
     1 /*
       
     2 * Copyright (c) 2010 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 * implementation for NTB16 build  class
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 /**
       
    22 @file
       
    23 @internalComponent
       
    24 */
       
    25 
       
    26 
       
    27 
       
    28 #include <es_sock.h>
       
    29 #ifndef __OVER_DUMMYUSBSCLDD__
       
    30 #include <nifmbuf.h>
       
    31 #else
       
    32 #include <usb/testncm/ethinter.h>
       
    33 #endif
       
    34 #include "ncmntb16builder.h"
       
    35 #include "ncmntbbuildpolicy.h"
       
    36 #include "ncmpktdrvcommon.h"
       
    37 #include "OstTraceDefinitions.h"
       
    38 #ifdef OST_TRACE_COMPILER_IN_USE
       
    39 #include "ncmntb16builderTraces.h"
       
    40 #endif
       
    41 
       
    42 
       
    43 const TInt KNtb16InMaxSize = 32768;
       
    44 
       
    45 
       
    46 const TInt KNth16Signature = 0x484D434E;
       
    47 const TInt KNth16Length = 0x0C;
       
    48 
       
    49 const TInt KNdp16Signature = 0x304D434E;
       
    50 const TInt KNdp16HeaderSize = 8;
       
    51 const TInt KNdp16EntrySize = 4;
       
    52 const TInt KNdp16InDivisor = 4;
       
    53 const TInt KNdp16InRemainder = 0;
       
    54 const TInt KNdp16InAlignment = 4;
       
    55 
       
    56 
       
    57 // ======== MEMBER FUNCTIONS ========
       
    58 //
       
    59 
       
    60 
       
    61 CNcmNtbBuilder* CNcmNtb16Builder::NewL(MNcmNtbBuildObserver& aObserver)
       
    62     {
       
    63     OstTraceFunctionEntry1( CNCMNTB16BUILDER_NEWL_ENTRY, ( TUint )&( aObserver ) );
       
    64     CNcmNtb16Builder *self=new (ELeave) CNcmNtb16Builder(aObserver);
       
    65     self->ConstructL();
       
    66     OstTraceFunctionExit0( CNCMNTB16BUILDER_NEWL_EXIT );
       
    67     return self;    
       
    68     }
       
    69 
       
    70 CNcmNtb16Builder::CNcmNtb16Builder(MNcmNtbBuildObserver& aObserver)
       
    71     : CNcmNtbBuilder(aObserver)
       
    72     {
       
    73     }
       
    74 
       
    75 void CNcmNtb16Builder::ConstructL()
       
    76     {
       
    77     iNtbInMaxSize = KNtb16InMaxSize;
       
    78     iNdpInDivisor = KNdp16InDivisor;
       
    79     iNdpInPayloadRemainder = KNdp16InRemainder;
       
    80     iNdpInAlignment = KNdp16InAlignment;  
       
    81     }
       
    82 
       
    83 CNcmNtb16Builder::~CNcmNtb16Builder()
       
    84     {
       
    85     iPacketsArray.Reset();
       
    86     }
       
    87 
       
    88 
       
    89 TInt CNcmNtb16Builder::SetNtbInMaxSize(TInt aSize)
       
    90     {
       
    91     OstTraceFunctionEntry1( CNCMNTB16BUILDER_SETNTBINMAXSIZE_ENTRY, this );
       
    92     if (aSize > KNtb16InMaxSize || aSize < MinNtbInMaxSize())
       
    93         {
       
    94         OstTraceFunctionExit1( CNCMNTB16BUILDER_SETNTBINMAXSIZE_EXIT, this );
       
    95         return KErrArgument;
       
    96         }
       
    97     iNtbInMaxSize = aSize;
       
    98     OstTraceFunctionExit1( CNCMNTB_DUP116BUILDER_SETNTBINMAXSIZE_EXIT, this );
       
    99     return KErrNone;
       
   100     }
       
   101 
       
   102 //
       
   103 //construct NTB header information
       
   104 //
       
   105 
       
   106 void CNcmNtb16Builder::BuildNtbHeader()
       
   107     {
       
   108     OstTraceFunctionEntry1( CNCMNTB16BUILDER_BUILDNTBHEADER_ENTRY, this );
       
   109     TPtr8 buf(iBuffer.iPtr, KNth16Length, KNth16Length);
       
   110     TInt pos = 0;
       
   111     LittleEndian::Put32(&buf[pos], KNth16Signature);
       
   112     pos += 4;
       
   113     LittleEndian::Put16(&buf[pos], KNth16Length);
       
   114     pos += 2;
       
   115     LittleEndian::Put16(&buf[pos], iSequence);
       
   116     pos += 2;
       
   117     LittleEndian::Put32(&buf[pos], iNdpOffset+iNdpLength);
       
   118     pos += 2;
       
   119     LittleEndian::Put16(&buf[pos], iNdpOffset);
       
   120     pos += 2;
       
   121 
       
   122     OstTraceExt3( TRACE_NORMAL, CNCMNTB16BUILDER_BUILDNTBHEADER, "CNcmNtb16Builder::BuildNtbHeader %d %d %d", iSequence, iNdpOffset, iNdpOffset+iNdpLength );
       
   123     OstTraceFunctionExit1( CNCMNTB16BUILDER_BUILDNTBHEADER_EXIT, this );
       
   124     }
       
   125 
       
   126 
       
   127 //
       
   128 //construct NDP inforamtion
       
   129 //
       
   130 
       
   131 void CNcmNtb16Builder::BuildNdp()
       
   132     {
       
   133     OstTraceFunctionEntry1( CNCMNTB16BUILDER_BUILDNDP_ENTRY, this );
       
   134     TPtr8 buf(iBuffer.iPtr+iNdpOffset, iNdpLength, iNdpLength);
       
   135     TInt pos = 0;
       
   136 
       
   137     LittleEndian::Put32(&buf[pos], KNdp16Signature);
       
   138     pos += 4;
       
   139     LittleEndian::Put16(&buf[pos], iNdpLength);
       
   140     pos += 2;
       
   141     LittleEndian::Put16(&buf[pos], 0);
       
   142     pos += 2;
       
   143      
       
   144     TInt i;
       
   145     for (i=0 ; i<iPacketsArray.Count() ; i++)
       
   146         {
       
   147         LittleEndian::Put16(&buf[pos], iPacketsArray[i].iWDatagramIndex);
       
   148         pos += 2;
       
   149         LittleEndian::Put16(&buf[pos], iPacketsArray[i].iWDatagramLength);
       
   150         pos += 2;
       
   151         }
       
   152     LittleEndian::Put16(&buf[pos], 0);
       
   153     pos += 2;
       
   154     LittleEndian::Put16(&buf[pos], 0);
       
   155     pos += 2;
       
   156     OstTraceFunctionExit1( CNCMNTB16BUILDER_BUILDNDP_EXIT, this );
       
   157     }
       
   158 
       
   159 
       
   160 //
       
   161 //Append a datagram to NTB
       
   162 //
       
   163 
       
   164 TInt CNcmNtb16Builder::AppendPacket(const RMBufChain& aPacket)
       
   165     {
       
   166     OstTraceFunctionEntry1( CNCMNTB16BUILDER_APPENDPACKET_ENTRY, this );
       
   167 // datagram must align with  iNdpInDivisor and remainder is iNdpInPayloadRemainder, refer to NCM spec 3.34 for detail   
       
   168     TInt len = aPacket.Length();
       
   169     TInt alignedDataOffset;
       
   170     int remainder = iDatagramOffset%iNdpInDivisor;
       
   171     if (iNdpInPayloadRemainder < remainder)
       
   172         {
       
   173         alignedDataOffset = iDatagramOffset + iNdpInDivisor - remainder + iNdpInPayloadRemainder;  
       
   174         }
       
   175     else
       
   176         {
       
   177         alignedDataOffset = iDatagramOffset + (iNdpInPayloadRemainder-remainder);
       
   178         }
       
   179     TInt newDatagramOffset = alignedDataOffset+len;
       
   180 // NDP must align with  iNdpInAlignment, refer to NCM spec Table 6-3 for detail         
       
   181     TInt newNdpOffset = (newDatagramOffset+iNdpInAlignment-1)&~(iNdpInAlignment-1);
       
   182 
       
   183 //    must have a zero entry in the end of NDP, so the NDP length is added with KNdp16EntrySize
       
   184     TInt newNdpLen = iNdpLength + KNdp16EntrySize;
       
   185 
       
   186 //   iBuffer.iMaxLength and iNtbInMaxSize may not same.
       
   187     if (newNdpLen+newNdpOffset > iBuffer.iMaxLength || newNdpLen+newNdpOffset > iNtbInMaxSize)
       
   188         {
       
   189         CompleteNtbBuild();
       
   190         OstTraceFunctionExit1( CNCMNTB16BUILDER_APPENDPACKET_EXIT, this );
       
   191         return KErrBufferFull;
       
   192         }
       
   193 
       
   194     TPtr8 ptr((TUint8*)(alignedDataOffset+iBuffer.iPtr), len, len);
       
   195     aPacket.CopyOut(ptr);
       
   196     TDatagramEntry entry;
       
   197     entry.iWDatagramIndex = alignedDataOffset;
       
   198     entry.iWDatagramLength = len;
       
   199     iPacketsArray.Append(entry);
       
   200     iNdpOffset = newNdpOffset;
       
   201     iNdpLength = newNdpLen;     
       
   202     iDatagramOffset = newDatagramOffset;
       
   203     if (iBuildPolicy)
       
   204         {
       
   205         iBuildPolicy->UpdateNtb(iNdpOffset+iNdpLength);
       
   206         }
       
   207     OstTraceFunctionExit1( CNCMNTB_DUP116BUILDER_APPENDPACKET_EXIT, this );
       
   208     return KErrNone;
       
   209     }
       
   210 
       
   211 //
       
   212 //complete a NTB, is called by buildpolicy or current NTB is full.
       
   213 //
       
   214 
       
   215 
       
   216 void CNcmNtb16Builder::CompleteNtbBuild()
       
   217     {
       
   218     OstTraceFunctionEntry1( CNCMNTB16BUILDER_COMPLETENTBBUILD_ENTRY, this );
       
   219     
       
   220     BuildNtbHeader();
       
   221     BuildNdp();
       
   222     iBuffer.iLen = iNdpOffset+iNdpLength;
       
   223     SendNtbPayload();
       
   224     CNcmNtbBuilder::CompleteNtbBuild();    
       
   225     OstTraceFunctionExit1( CNCMNTB16BUILDER_COMPLETENTBBUILD_EXIT, this );
       
   226     }
       
   227 
       
   228 
       
   229 //
       
   230 //start a new ntb, is called by sender
       
   231 //
       
   232 
       
   233 void CNcmNtb16Builder::StartNewNtb(const TNcmBuffer& aBuffer)
       
   234     {
       
   235     OstTraceFunctionEntry1( CNCMNTB16BUILDER_STARTNEWNTB_ENTRY, this );
       
   236     
       
   237     iBuffer = aBuffer;
       
   238     iNdpOffset = 0;
       
   239     iNdpLength = 0;
       
   240     iPacketsArray.Reset();
       
   241     iDatagramOffset = KNth16Length;
       
   242 // NDP must align with  iNdpInAlignment, refer to NCM spec Table 6-3 for detail     
       
   243     iNdpOffset = (iDatagramOffset+iNdpInAlignment-1)&~(iNdpInAlignment-1);
       
   244 //    must have a zero entry in the end of NDP, so the NDP length is added with KNdp16EntrySize
       
   245     iNdpLength = KNdp16HeaderSize + KNdp16EntrySize;
       
   246     
       
   247     if (iBuildPolicy)
       
   248         {
       
   249         iBuildPolicy->StartNewNtb();
       
   250         }
       
   251     CNcmNtbBuilder::StartNewNtb(aBuffer);
       
   252     OstTraceFunctionExit1( CNCMNTB16BUILDER_STARTNEWNTB_EXIT, this );
       
   253     }
       
   254 
       
   255 
       
   256 void CNcmNtb16Builder::Reset()
       
   257     {
       
   258     OstTraceFunctionEntry1( CNCMNTB16BUILDER_RESET_ENTRY, this );
       
   259     iNtbInMaxSize = KNtb16InMaxSize;
       
   260     iNdpOffset = 0;
       
   261     iNdpLength = 0;    
       
   262     iDatagramOffset = 0;    
       
   263     iPacketsArray.Reset();
       
   264     CNcmNtbBuilder::Reset();
       
   265     OstTraceFunctionExit1( CNCMNTB16BUILDER_RESET_EXIT, this );
       
   266     }
       
   267     
       
   268 
       
   269