wlan_bearer/wlanengine/wlan_common/wlanengine_common_3.1/src/core_frame_ethernet.cpp
changeset 0 c40eb8fe8501
equal deleted inserted replaced
-1:000000000000 0:c40eb8fe8501
       
     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 the License "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:  Class for parsing Ethernet frames
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "core_frame_ethernet.h"
       
    20 #include "core_tools.h"
       
    21 #include "am_debug.h"
       
    22 
       
    23 const u16_t CORE_ETHERNET_MIN_LENGTH = 14;
       
    24 const u16_t CORE_ETHERNET_DESTINATION_INDEX = 0;
       
    25 const u16_t CORE_ETHERNET_SOURCE_INDEX = 6;
       
    26 const u16_t CORE_ETHERNET_TYPE_INDEX = 12;
       
    27 
       
    28 // ---------------------------------------------------------------------------
       
    29 // ---------------------------------------------------------------------------
       
    30 //
       
    31 core_frame_ethernet_c* core_frame_ethernet_c::instance(
       
    32     u16_t data_length,
       
    33     const u8_t* data,
       
    34     bool_t is_copied )
       
    35     {
       
    36     DEBUG( "core_frame_ethernet_c::instance()" );
       
    37     DEBUG1( "core_frame_ethernet_c::instance() - is_copied %u",
       
    38         is_copied );   
       
    39     
       
    40     if ( data_length < CORE_ETHERNET_MIN_LENGTH )
       
    41         {
       
    42         DEBUG( "core_frame_ethernet_c::instance() - not a valid Ethernet frame, frame is too short" );
       
    43         
       
    44         return NULL;
       
    45         }
       
    46 
       
    47     u8_t* buffer = const_cast<u8_t*>( data );
       
    48     u16_t buffer_length( 0 );
       
    49 
       
    50     if ( is_copied )
       
    51         {
       
    52         buffer_length = data_length;
       
    53         buffer = new u8_t[buffer_length];
       
    54         
       
    55         if ( !buffer )
       
    56             {
       
    57             DEBUG( "core_frame_ethernet_c::instance() - not able to allocate buffer for copying" );
       
    58         
       
    59             return NULL;            
       
    60             }
       
    61 
       
    62         core_tools_c::copy(
       
    63             buffer,
       
    64             data,
       
    65             buffer_length );
       
    66         }
       
    67 
       
    68     core_frame_ethernet_c* instance = new core_frame_ethernet_c(
       
    69         data_length,
       
    70         buffer,
       
    71         buffer_length );
       
    72     if ( !instance )
       
    73         {
       
    74         DEBUG( "core_frame_ethernet_c::instance() - unable to create an instance" );
       
    75         if ( is_copied )
       
    76             {
       
    77             delete[] buffer;
       
    78             }
       
    79 
       
    80         return NULL;
       
    81         }
       
    82 
       
    83     return instance;
       
    84     }
       
    85 
       
    86 // ---------------------------------------------------------------------------
       
    87 // ---------------------------------------------------------------------------
       
    88 // 
       
    89 core_frame_ethernet_c* core_frame_ethernet_c::instance(
       
    90     u16_t max_data_length,
       
    91     const core_mac_address_s& destination,
       
    92     const core_mac_address_s& source,
       
    93     u16_t type )
       
    94     {
       
    95     DEBUG( "core_frame_ethernet_c::instance()" );
       
    96 
       
    97     u8_t* buffer = new u8_t[max_data_length];
       
    98     if ( !buffer )
       
    99         {
       
   100         DEBUG( "core_frame_ethernet_c::instance() - unable create the internal buffer" );
       
   101         }
       
   102 
       
   103     core_frame_ethernet_c* instance =
       
   104         new core_frame_ethernet_c( 0, buffer, max_data_length );
       
   105     if ( !instance )
       
   106         {
       
   107         DEBUG( "core_frame_ethernet_c::instance() - unable to create an instance" );
       
   108         delete[] buffer;
       
   109 
       
   110         return NULL;
       
   111         }
       
   112 
       
   113     instance->generate(
       
   114         destination,
       
   115         source,
       
   116         type );
       
   117 
       
   118     return instance;
       
   119     }
       
   120 
       
   121 // ---------------------------------------------------------------------------
       
   122 // ---------------------------------------------------------------------------
       
   123 //      
       
   124 core_frame_ethernet_c::~core_frame_ethernet_c()
       
   125     {
       
   126     DEBUG( "core_frame_ethernet_c::~core_frame_ethernet_c" );
       
   127 
       
   128     /** 
       
   129      * If maximum frame length has been defined, we have allocated
       
   130      * the frame buffer ourselves.
       
   131      */    
       
   132     if ( max_data_length_m )
       
   133         {
       
   134         delete[] data_m;
       
   135         }    
       
   136 
       
   137     data_m = NULL;
       
   138     }
       
   139 
       
   140 // ---------------------------------------------------------------------------
       
   141 // ---------------------------------------------------------------------------
       
   142 //
       
   143 core_mac_address_s core_frame_ethernet_c::destination() const
       
   144     {
       
   145     return mac_address( CORE_ETHERNET_DESTINATION_INDEX );
       
   146     }
       
   147 
       
   148 // ---------------------------------------------------------------------------
       
   149 // ---------------------------------------------------------------------------
       
   150 //
       
   151 core_mac_address_s core_frame_ethernet_c::source() const
       
   152     {
       
   153     return mac_address( CORE_ETHERNET_SOURCE_INDEX );
       
   154     }
       
   155 
       
   156 // ---------------------------------------------------------------------------
       
   157 // ---------------------------------------------------------------------------
       
   158 //
       
   159 u16_t core_frame_ethernet_c::type() const
       
   160     {
       
   161     return core_tools_c::get_u16_big_endian( data_m, CORE_ETHERNET_TYPE_INDEX );
       
   162     }
       
   163 
       
   164 // ---------------------------------------------------------------------------
       
   165 // ---------------------------------------------------------------------------
       
   166 //
       
   167 core_mac_address_s core_frame_ethernet_c::mac_address(
       
   168     u16_t index ) const
       
   169     {
       
   170     core_mac_address_s mac( ZERO_MAC_ADDR );
       
   171     
       
   172     core_tools_c::copy(
       
   173         &mac.addr[0],
       
   174         &data_m[index],
       
   175         MAC_ADDR_LEN );
       
   176 
       
   177     return mac;
       
   178     }
       
   179 
       
   180 // ---------------------------------------------------------------------------
       
   181 // ---------------------------------------------------------------------------
       
   182 //
       
   183 u16_t core_frame_ethernet_c::data_length() const
       
   184     {
       
   185     return data_length_m;
       
   186     }
       
   187 
       
   188 // ---------------------------------------------------------------------------
       
   189 // ---------------------------------------------------------------------------
       
   190 //   
       
   191 const u8_t* core_frame_ethernet_c::data() const
       
   192     {
       
   193     return data_m;
       
   194     }
       
   195 
       
   196 // ---------------------------------------------------------------------------
       
   197 // ---------------------------------------------------------------------------
       
   198 //
       
   199 u16_t core_frame_ethernet_c::payload_data_length() const
       
   200     {
       
   201     return data_length_m - CORE_ETHERNET_MIN_LENGTH;
       
   202     }
       
   203 
       
   204 // ---------------------------------------------------------------------------
       
   205 // ---------------------------------------------------------------------------
       
   206 //
       
   207 const u8_t* core_frame_ethernet_c::payload_data() const
       
   208     {
       
   209     return data_m + CORE_ETHERNET_MIN_LENGTH;
       
   210     }
       
   211 
       
   212 // ---------------------------------------------------------------------------
       
   213 // ---------------------------------------------------------------------------
       
   214 //
       
   215 void core_frame_ethernet_c::core_frame_ethernet_c::generate(
       
   216     const core_mac_address_s& destination,
       
   217     const core_mac_address_s& source,
       
   218     u16_t type )
       
   219     {
       
   220     ASSERT( !data_length_m );
       
   221     ASSERT( max_data_length_m );   
       
   222 
       
   223     // Destination MAC
       
   224     core_tools_c::copy(
       
   225         &data_m[data_length_m],
       
   226         &destination.addr[0],
       
   227         MAC_ADDR_LEN );
       
   228     data_length_m += MAC_ADDR_LEN;        
       
   229 
       
   230     // Source MAC
       
   231     core_tools_c::copy(
       
   232         &data_m[data_length_m],
       
   233         &source.addr[0],
       
   234         MAC_ADDR_LEN );
       
   235     data_length_m += MAC_ADDR_LEN;    
       
   236 
       
   237     // Ethernet Type
       
   238     u16_t temp16 = core_tools_c::convert_host_to_big_endian(
       
   239         type  );
       
   240     core_tools_c::copy(
       
   241         &data_m[data_length_m],
       
   242         reinterpret_cast<u8_t*>( &temp16 ),
       
   243         sizeof( temp16 ) );
       
   244     data_length_m += sizeof( temp16 );
       
   245     }
       
   246 
       
   247 // ---------------------------------------------------------------------------
       
   248 // ---------------------------------------------------------------------------
       
   249 //
       
   250 core_frame_ethernet_c::core_frame_ethernet_c(
       
   251     u16_t data_length,
       
   252     const u8_t* data,
       
   253     u16_t max_data_length ) :
       
   254     data_length_m( data_length ),
       
   255     data_m( NULL ),
       
   256     max_data_length_m( max_data_length )
       
   257     {
       
   258     DEBUG( "core_frame_ethernet_c::core_frame_ethernet_c" );
       
   259 
       
   260     /**
       
   261      * The reason the const is discarded is that the same pointer
       
   262      * is used when we have allocated the frame buffer ourselves.
       
   263      */
       
   264     data_m = const_cast<u8_t*>( data );
       
   265     }