eapol/eapol_framework/eapol_common/type/simple_config/simple_config/src/simple_config_tlv_header.cpp
changeset 0 c8830336c852
child 2 1c7bc153c08e
equal deleted inserted replaced
-1:000000000000 0:c8830336c852
       
     1 /*
       
     2 * Copyright (c) 2001-2006 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:  EAP and WLAN authentication protocols.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // This is enumeration of EAPOL source code.
       
    20 #if defined(USE_EAP_MINIMUM_RELEASE_TRACES)
       
    21 	#undef EAP_FILE_NUMBER_ENUM
       
    22 	#define EAP_FILE_NUMBER_ENUM 598 
       
    23 	#undef EAP_FILE_NUMBER_DATE 
       
    24 	#define EAP_FILE_NUMBER_DATE 1127594498 
       
    25 #endif //#if defined(USE_EAP_MINIMUM_RELEASE_TRACES)
       
    26 
       
    27 
       
    28 
       
    29 #include "eap_am_memory.h"
       
    30 #include "simple_config_tlv_header.h"
       
    31 #include "simple_config_types.h"
       
    32 
       
    33 
       
    34 /** @file */
       
    35 
       
    36 
       
    37 /**
       
    38  * The destructor of the simple_config_tlv_header_c class does nothing.
       
    39  */
       
    40 simple_config_tlv_header_c::~simple_config_tlv_header_c()
       
    41 {
       
    42 }
       
    43 
       
    44 /**
       
    45  * The constructor of the simple_config_tlv_header_c class simply initializes the attributes.
       
    46  */
       
    47 simple_config_tlv_header_c::simple_config_tlv_header_c(
       
    48 	abs_eap_am_tools_c * const tools)
       
    49 	: eap_general_header_base_c(tools, 0, 0ul)
       
    50 	, m_am_tools(tools)
       
    51 {
       
    52 }
       
    53 
       
    54 /**
       
    55  * The constructor of the simple_config_tlv_header_c class simply initializes the attributes.
       
    56  */
       
    57 simple_config_tlv_header_c::simple_config_tlv_header_c(
       
    58 	abs_eap_am_tools_c * const tools,
       
    59 	void * const header_begin,
       
    60 	const u32_t header_buffer_length)
       
    61 	: eap_general_header_base_c(tools, header_begin, header_buffer_length)
       
    62 	, m_am_tools(tools)
       
    63 {
       
    64 }
       
    65 
       
    66 /**
       
    67  * This function returns the TLV type flag.
       
    68  */
       
    69 simple_config_Attribute_Type_e simple_config_tlv_header_c::get_tlv_type() const
       
    70 {
       
    71 	const u8_t * const flag_tlv_type_data = get_header_offset(m_tlv_type_offset, sizeof(u16_t));
       
    72 	if (flag_tlv_type_data != 0)
       
    73 	{
       
    74 		return static_cast<simple_config_Attribute_Type_e>(
       
    75 			(static_cast<u16_t>(flag_tlv_type_data[0] << 8)
       
    76 			| (static_cast<u16_t>(flag_tlv_type_data[1]))));
       
    77 	}
       
    78 	else
       
    79 	{
       
    80 		return simple_config_Attribute_Type_None;
       
    81 	}
       
    82 }
       
    83 
       
    84 /**
       
    85  * This function returns the data length of TLV.
       
    86  */
       
    87 u32_t simple_config_tlv_header_c::get_length() const
       
    88 {
       
    89 	return get_header_length() + get_data_length();
       
    90 }
       
    91 
       
    92 /**
       
    93  * This function returns the data length of TLV.
       
    94  */
       
    95 u16_t simple_config_tlv_header_c::get_data_length() const
       
    96 {
       
    97 	const u8_t * const length_data = get_header_offset(m_length_offset, sizeof(u16_t));
       
    98 	if (length_data != 0)
       
    99 	{
       
   100 		return eap_read_u16_t_network_order(length_data, sizeof(u16_t));
       
   101 	}
       
   102 	else
       
   103 	{
       
   104 		return 0ul;
       
   105 	}
       
   106 }
       
   107 
       
   108 /**
       
   109  * This function returns the header length of TLV.
       
   110  */
       
   111 u32_t simple_config_tlv_header_c::get_header_length()
       
   112 {
       
   113 	return m_data_offset;
       
   114 }
       
   115 
       
   116 /**
       
   117  * This function returns pointer to the offset of data of TLV.
       
   118  * @param offset is the offset of queried data in bytes.
       
   119  * @param contignuous_bytes is the length of queried data in bytes.
       
   120  */
       
   121 u8_t * simple_config_tlv_header_c::get_data_offset(const u32_t offset, const u32_t contignuous_bytes) const
       
   122 {
       
   123 	EAP_UNREFERENCED_PARAMETER(m_am_tools);
       
   124 
       
   125 	u32_t data_length = get_data_length(); // Here is removed optional SIMPLE_CONFIG message length.
       
   126 
       
   127 	if (data_length >= offset+contignuous_bytes)
       
   128 	{
       
   129 		u8_t * const data = get_header_offset(m_data_offset, offset+contignuous_bytes);
       
   130 		if (data != 0)
       
   131 		{
       
   132 			return &data[offset];
       
   133 		}
       
   134 		else
       
   135 		{
       
   136 			return 0;
       
   137 		}
       
   138 	}
       
   139 	else
       
   140 	{
       
   141 		EAP_ASSERT_ALWAYS(data_length >= offset+contignuous_bytes);
       
   142 	}
       
   143 	return 0;
       
   144 }
       
   145 
       
   146 
       
   147 /**
       
   148  * This function returns pointer to the offset of data of TLV.
       
   149  * @param contignuous_bytes is the length of queried data in bytes.
       
   150  */
       
   151 u8_t * simple_config_tlv_header_c::get_data(const u32_t contignuous_bytes) const
       
   152 {
       
   153 	return get_data_offset(0u, contignuous_bytes);
       
   154 }
       
   155 
       
   156 
       
   157 /**
       
   158  * This function return pointer to the next TLV header in the same buffer.
       
   159  */
       
   160 u8_t * simple_config_tlv_header_c::get_next_header() const
       
   161 {
       
   162 	if (get_header_buffer_length() >= 2ul*get_header_length()+get_data_length())
       
   163 	{
       
   164 		return get_data_offset(get_data_length(), get_header_length());
       
   165 	}
       
   166 	else
       
   167 	{
       
   168 		return 0;
       
   169 	}
       
   170 }
       
   171 
       
   172 
       
   173 /**
       
   174  * This function checks the header is valid.
       
   175  */
       
   176 eap_status_e simple_config_tlv_header_c::check_header() const
       
   177 {
       
   178 	if (get_tlv_type() < simple_config_Attribute_Type_First_Correct_Value
       
   179 		|| get_tlv_type() > simple_config_Attribute_Type_Last_Correct_Value)
       
   180 	{
       
   181 		return EAP_STATUS_RETURN(m_am_tools, eap_status_header_corrupted);
       
   182 	}
       
   183 	return EAP_STATUS_RETURN(m_am_tools, eap_status_ok);
       
   184 }
       
   185 
       
   186 /**
       
   187  * This function returns debug strings of the TLV type.
       
   188  */
       
   189 eap_const_string simple_config_tlv_header_c::get_tlv_type_string() const
       
   190 {
       
   191 	eap_simple_config_trace_string_c debug_string;
       
   192 
       
   193 	return debug_string.get_attribute_type_string(get_tlv_type());
       
   194 }
       
   195 
       
   196 /**
       
   197  * This function sets the TLV type flag.
       
   198  */
       
   199 void simple_config_tlv_header_c::set_tlv_type(simple_config_Attribute_Type_e type)
       
   200 {
       
   201 	u8_t * const flag_tlv_type_data = get_header_offset(m_tlv_type_offset, sizeof(u16_t));
       
   202 	
       
   203 	EAP_ASSERT(flag_tlv_type_data != 0);
       
   204 	
       
   205 	flag_tlv_type_data[0] = static_cast<u8_t>((type & 0xff00) >> 8);
       
   206 	flag_tlv_type_data[1] = static_cast<u8_t>(type & 0x00ff);
       
   207 }
       
   208 
       
   209 /**
       
   210  * This function sets the TLV data length.
       
   211  */
       
   212 void simple_config_tlv_header_c::set_data_length(const u16_t p_length)
       
   213 {
       
   214 	u8_t * const length_data = get_header_offset(m_length_offset, sizeof(u16_t));
       
   215 	
       
   216 	EAP_ASSERT(length_data != 0);
       
   217 	
       
   218 	length_data[0] = static_cast<u8_t>((p_length & 0xff00) >> 8);
       
   219 	length_data[1] = static_cast<u8_t>((p_length & 0x00ff));
       
   220 }
       
   221 
       
   222 /**
       
   223  * This function resets the TLV header.
       
   224  */
       
   225 void simple_config_tlv_header_c::reset_header(const u16_t buffer_length)
       
   226 {
       
   227 	set_tlv_type(simple_config_Attribute_Type_None);
       
   228 	set_data_length(buffer_length);
       
   229 }
       
   230 
       
   231 
       
   232 
       
   233 
       
   234 // End.