eapol/eapol_framework/eapol_symbian/am/common/symbian/eap_am_tools_symbian.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 147 
       
    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_tools_symbian.h"
       
    30 #include "eap_am_types.h"
       
    31 #include <e32math.h>
       
    32 #include <utf.h>
       
    33 
       
    34 const TUint MAX_DB_TRANSACTION_RETRY_COUNT = 10;
       
    35 const u32_t EAP_TIMER_MAX_AFTER_TIME_MILLISECONDS_SYMBIAN = 2100000ul;
       
    36 
       
    37 //--------------------------------------------------
       
    38 
       
    39 #if defined(_WIN32) && !defined(__GNUC__)
       
    40 	#pragma warning( disable : 4355 ) // 'this' : used in base member initializer list
       
    41 #endif
       
    42 
       
    43 //--------------------------------------------------
       
    44 
       
    45 EAP_FUNC_EXPORT eap_am_tools_symbian_c::~eap_am_tools_symbian_c()
       
    46 {
       
    47 	// If multithreading is used the log file is not kept open all the time
       
    48 	// so no need to close the handles here.
       
    49 #if defined(USE_EAP_FILE_TRACE)
       
    50 #if !defined (USE_MULTITHREADING)
       
    51 	m_LogFile.Close();
       
    52 	m_Fs.Close();
       
    53 #endif //#if !defined (USE_MULTITHREADING)
       
    54 #endif //#if defined(USE_EAP_FILE_TRACE)
       
    55 }
       
    56 
       
    57 //--------------------------------------------------
       
    58 
       
    59 //
       
    60 EAP_FUNC_EXPORT eap_am_tools_symbian_c::eap_am_tools_symbian_c(eap_const_string /*pfilename*/)
       
    61 	: eap_am_tools_c()
       
    62 	, CTimer(CTimer::EPriorityStandard)
       
    63 	, m_start_ticks(0)
       
    64 	, m_directory_exists(false)
       
    65 	, m_crypto(this)
       
    66 	, m_timer_queue(this, EAP_TIMER_RESOLUTION)
       
    67 	, m_run_thread(true)
       
    68 	, m_is_valid(false)
       
    69 	, m_configure_called(false)
       
    70 	, iInterval(0ul)
       
    71 	, iStartTime(0ul)
       
    72 	, iLastTime(0ul)
       
    73 	, m_logfile_open(false)	
       
    74 {
       
    75 	EAP_TRACE_BEGIN(this, TRACE_FLAGS_DEFAULT);
       
    76 
       
    77 #if defined(USE_EAP_HARDWARE_TRACE)
       
    78 	set_trace_mask(
       
    79 		eap_am_tools_c::eap_trace_mask_always
       
    80 		| eap_am_tools_c::eap_trace_mask_error
       
    81 		| eap_am_tools_c::eap_trace_mask_debug
       
    82 		| eap_am_tools_c::eap_trace_mask_message_data);
       
    83 #endif //#if defined(USE_EAP_HARDWARE_TRACE)
       
    84 
       
    85 	EAP_TRACE_DEBUG(
       
    86 		this,
       
    87 		TRACE_FLAGS_DEFAULT,
       
    88 		(EAPL("eap_am_tools_symbian_c::eap_am_tools_symbian_c()\n")));
       
    89 
       
    90 	if (m_crypto.get_is_valid() == false)
       
    91 	{
       
    92 		EAP_TRACE_ERROR(
       
    93 			this,
       
    94 			TRACE_FLAGS_DEFAULT,
       
    95 			(EAPL("ERROR: m_crypto.get_is_valid() == false\n")));
       
    96 		return;
       
    97 	}
       
    98 
       
    99 	if (m_timer_queue.get_is_valid() == false)
       
   100 	{
       
   101 		EAP_TRACE_ERROR(
       
   102 			this,
       
   103 			TRACE_FLAGS_DEFAULT,
       
   104 			(EAPL("ERROR: m_timer_queue.get_is_valid() == false\n")));
       
   105 		return;
       
   106 	}
       
   107 
       
   108 	if (m_global_mutex.get_is_valid() == false)
       
   109 	{
       
   110 		EAP_TRACE_ERROR(
       
   111 			this,
       
   112 			TRACE_FLAGS_DEFAULT,
       
   113 			(EAPL("ERROR: m_global_mutex.get_is_valid() == false\n")));
       
   114 		return;
       
   115 	}
       
   116 
       
   117 	if (m_trace_mutex.get_is_valid() == false)
       
   118 	{
       
   119 		EAP_TRACE_ERROR(
       
   120 			this,
       
   121 			TRACE_FLAGS_DEFAULT,
       
   122 			(EAPL("ERROR: m_trace_mutex.get_is_valid() == false\n")));
       
   123 		return;
       
   124 	}
       
   125 
       
   126 	m_is_valid = true;
       
   127 
       
   128 	EAP_TRACE_END(this, TRACE_FLAGS_DEFAULT);
       
   129 }
       
   130 
       
   131 //--------------------------------------------------
       
   132 
       
   133 //
       
   134 EAP_FUNC_EXPORT void eap_am_tools_symbian_c::set_use_eap_milli_second_timer(
       
   135 	const bool use_eap_millisecond_timer)
       
   136 {
       
   137 	m_timer_queue.set_use_eap_milli_second_timer(use_eap_millisecond_timer);
       
   138 }
       
   139 
       
   140 //--------------------------------------------------
       
   141 
       
   142 //
       
   143 EAP_FUNC_EXPORT bool eap_am_tools_symbian_c::get_use_eap_milli_second_timer()
       
   144 {
       
   145 	return m_timer_queue.get_use_eap_milli_second_timer();
       
   146 }
       
   147 
       
   148 //--------------------------------------------------
       
   149 
       
   150 //
       
   151 EAP_FUNC_EXPORT eap_status_e eap_am_tools_symbian_c::configure()
       
   152 {
       
   153 	EAP_TRACE_BEGIN(this, TRACE_FLAGS_DEFAULT);
       
   154 
       
   155 	EAP_TRACE_DEBUG(
       
   156 		this,
       
   157 		TRACE_FLAGS_DEFAULT,
       
   158 		(EAPL("eap_am_tools_symbian_c::configure()\n")));
       
   159 
       
   160 	if (m_configure_called == true)
       
   161 	{
       
   162 		return EAP_STATUS_RETURN(this, eap_status_ok);
       
   163 	}
       
   164 
       
   165 #if defined(USE_EAP_HARDWARE_TRACE)
       
   166 	set_trace_mask(
       
   167 		eap_am_tools_c::eap_trace_mask_always
       
   168 		| eap_am_tools_c::eap_trace_mask_error
       
   169 		| eap_am_tools_c::eap_trace_mask_debug
       
   170 		| eap_am_tools_c::eap_trace_mask_message_data);
       
   171 #endif //#if defined(USE_EAP_HARDWARE_TRACE)
       
   172 
       
   173 	m_start_ticks = get_clock_ticks();
       
   174 	iLastTime = m_start_ticks;
       
   175 
       
   176 	eap_status_e status = m_crypto.configure();
       
   177 	if (status != eap_status_ok)
       
   178 	{
       
   179 		return EAP_STATUS_RETURN(this, status);
       
   180 	}
       
   181 
       
   182 	m_crypto.add_rand_seed_hw_ticks();
       
   183 
       
   184 	if (m_crypto.get_is_valid() == false)
       
   185 	{
       
   186 		EAP_TRACE_END(this, TRACE_FLAGS_DEFAULT);
       
   187 		return EAP_STATUS_RETURN(this, eap_status_allocation_error);
       
   188 	}
       
   189 
       
   190 	if (m_timer_queue.get_is_valid() == false)
       
   191 	{
       
   192 		EAP_TRACE_END(this, TRACE_FLAGS_DEFAULT);
       
   193 		return EAP_STATUS_RETURN(this, eap_status_allocation_error);
       
   194 	}
       
   195 
       
   196 	if (m_global_mutex.get_is_valid() == false)
       
   197 	{
       
   198 		EAP_TRACE_END(this, TRACE_FLAGS_DEFAULT);
       
   199 		return EAP_STATUS_RETURN(this, eap_status_allocation_error);
       
   200 	}
       
   201 
       
   202 	if (m_trace_mutex.get_is_valid() == false)
       
   203 	{
       
   204 		EAP_TRACE_END(this, TRACE_FLAGS_DEFAULT);
       
   205 		return EAP_STATUS_RETURN(this, eap_status_allocation_error);
       
   206 	}
       
   207 
       
   208 #if defined(USE_EAP_FILE_TRACE)
       
   209 #if !defined (USE_MULTITHREADING)
       
   210 	TInt result = m_Fs.Connect();	
       
   211 	if (result != KErrNone)
       
   212 	{
       
   213 		EAP_TRACE_END(this, TRACE_FLAGS_DEFAULT);
       
   214 		return EAP_STATUS_RETURN(this, eap_status_process_general_error);
       
   215 	}
       
   216 #endif //#if !defined (USE_MULTITHREADING)
       
   217 #endif //#if defined(USE_EAP_FILE_TRACE)
       
   218 
       
   219 	{
       
   220 		set_use_timer_queue();
       
   221 		set_use_eap_milli_second_timer(true);
       
   222 		
       
   223 		TRAPD(err, CTimer::ConstructL());
       
   224 		if (err != KErrNone)
       
   225 		{
       
   226 			EAP_TRACE_ERROR(
       
   227 				this,
       
   228 				TRACE_FLAGS_DEFAULT,
       
   229 				(EAPL("CTimer::ConstructL() failed, err %d.\n"),
       
   230 				err));
       
   231 			EAP_TRACE_END(this, TRACE_FLAGS_DEFAULT);	
       
   232 			return EAP_STATUS_RETURN(this, eap_status_process_general_error);
       
   233 		}
       
   234 
       
   235 		CActiveScheduler::Add(this);
       
   236 	}
       
   237 
       
   238 	m_configure_called = true;
       
   239 
       
   240 	EAP_TRACE_END(this, TRACE_FLAGS_DEFAULT);
       
   241 	return eap_status_ok;
       
   242 }
       
   243 
       
   244 //--------------------------------------------------
       
   245 
       
   246 EAP_FUNC_EXPORT void eap_am_tools_symbian_c::enter_global_mutex()
       
   247 {
       
   248 	EAP_ASSERT_ALWAYS_TOOLS(this, m_configure_called == true);
       
   249 
       
   250 	m_global_mutex.mutex_enter();
       
   251 }
       
   252 
       
   253 //--------------------------------------------------
       
   254 
       
   255 EAP_FUNC_EXPORT void eap_am_tools_symbian_c::leave_global_mutex()
       
   256 {
       
   257 	m_global_mutex.mutex_leave(this);
       
   258 }
       
   259 
       
   260 //--------------------------------------------------
       
   261 
       
   262 EAP_FUNC_EXPORT abs_eap_am_mutex_c * eap_am_tools_symbian_c::get_global_mutex()
       
   263 {
       
   264 	return &m_global_mutex;
       
   265 }
       
   266 
       
   267 
       
   268 //--------------------------------------------------
       
   269 
       
   270 EAP_FUNC_EXPORT void eap_am_tools_symbian_c::enter_trace_mutex()
       
   271 {
       
   272 	m_trace_mutex.mutex_enter();
       
   273 }
       
   274 
       
   275 //--------------------------------------------------
       
   276 
       
   277 EAP_FUNC_EXPORT void eap_am_tools_symbian_c::leave_trace_mutex()
       
   278 {
       
   279 	m_trace_mutex.mutex_leave(this);
       
   280 }
       
   281 
       
   282 //--------------------------------------------------
       
   283 
       
   284 EAP_FUNC_EXPORT abs_eap_am_mutex_c * eap_am_tools_symbian_c::get_trace_mutex()
       
   285 {
       
   286 	return &m_trace_mutex;
       
   287 }
       
   288 
       
   289 
       
   290 
       
   291 //--------------------------------------------------
       
   292 
       
   293 EAP_FUNC_EXPORT u32_t eap_am_tools_symbian_c::get_timer_resolution_ms()
       
   294 {
       
   295 	enter_global_mutex();
       
   296 	const u32_t res = m_timer_queue.get_timer_resolution_ms();
       
   297 	leave_global_mutex();
       
   298 	return res;
       
   299 }
       
   300 
       
   301 //--------------------------------------------------
       
   302 
       
   303 //
       
   304 EAP_FUNC_EXPORT void eap_am_tools_symbian_c::set_timer_resolution_ms(const u32_t timer_resolution_ms)
       
   305 {
       
   306 	enter_global_mutex();
       
   307 	m_timer_queue.set_timer_resolution_ms(timer_resolution_ms);
       
   308 	leave_global_mutex();
       
   309 }
       
   310 
       
   311 //--------------------------------------------------
       
   312 
       
   313 EAP_FUNC_EXPORT u32_t eap_am_tools_symbian_c::pulse_timer(const u32_t elapsed_time_in_ms)
       
   314 {
       
   315 	return m_timer_queue.pulse_timer(elapsed_time_in_ms, true);
       
   316 }
       
   317 
       
   318 //--------------------------------------------------
       
   319 
       
   320 //
       
   321 EAP_FUNC_EXPORT void eap_am_tools_symbian_c::sprint(TDes& /*KPrintBuf*/, eap_const_string /*format*/, ...)
       
   322 {
       
   323 }
       
   324 
       
   325 //--------------------------------------------------
       
   326 
       
   327 //
       
   328 EAP_FUNC_EXPORT u32_t eap_am_tools_symbian_c::snprintf(u8_t * const buffer, u32_t buffer_size, eap_format_string format, ...)
       
   329 {
       
   330 	if (buffer == 0)
       
   331 	{
       
   332 		return 0ul;
       
   333 	}
       
   334 
       
   335 	u32_t result = 0;
       
   336 	m_format_buf.Copy((const TUint8 *)format);
       
   337 	VA_LIST args = {0,};
       
   338 	VA_START(args, format);
       
   339 	m_trace_buf.FormatList(m_format_buf, args);
       
   340 	VA_END(args);
       
   341 	result = (static_cast<u32_t>(m_trace_buf.Size()) < buffer_size) ? static_cast<u32_t>(m_trace_buf.Size()) : buffer_size;
       
   342 	memmove(buffer, m_trace_buf.PtrZ(), result);
       
   343 
       
   344 	return result;
       
   345 }
       
   346 
       
   347 //--------------------------------------------------
       
   348 
       
   349 //
       
   350 EAP_FUNC_EXPORT void eap_am_tools_symbian_c::formatted_print(eap_format_string format, ...)
       
   351 {
       
   352 	EAP_UNREFERENCED_PARAMETER(format);
       
   353 	
       
   354 #if defined(USE_EAP_TRACE) || defined(USE_EAP_TRACE_ALWAYS)
       
   355 
       
   356 	enter_trace_mutex();
       
   357 
       
   358 	u64_t time_stamp = get_clock_ticks();
       
   359 	TInt64 *_time_stamp = (TInt64 *)&time_stamp;		
       
   360 	if (get_use_seconds_timestamp_in_traces() == true)
       
   361 	{
       
   362 		u64_t hw_ticks_of_second = get_clock_ticks_of_second();
       
   363 		TInt64 *_hw_ticks_of_second = (TInt64 *)&hw_ticks_of_second;		
       
   364 		time_stamp -= m_start_ticks;
       
   365 
       
   366 		{
       
   367 			TInt64 _seconds = (*_time_stamp) / (*_hw_ticks_of_second);
       
   368 			TInt64 _remainder = (*_time_stamp) % (*_hw_ticks_of_second);
       
   369 			TInt64 _multiplier((TUint)1000000ul);
       
   370 			TInt64 _tmp = _multiplier * _remainder;
       
   371 			TInt64 _micro_seconds = _tmp / (*_hw_ticks_of_second);
       
   372 			TInt64 _div_60((TUint)60ul);
       
   373 
       
   374 			TInt64 _minutes = _seconds / _div_60;
       
   375 			_seconds = _seconds - _minutes * _div_60;
       
   376 			TInt64 _hours = _minutes / _div_60;
       
   377 			_minutes = _minutes - _hours* _div_60;
       
   378 
       
   379 			_LIT8(KFormat1, "%02d:%02d:%02d.%06d:EAPOL:");
       
   380 
       
   381 			m_trace_buf.Format(
       
   382 				KFormat1,
       
   383 				static_cast<TInt32>(_hours),
       
   384 				static_cast<TInt32>(_minutes),
       
   385 				static_cast<TInt32>(_seconds),
       
   386 				static_cast<TInt32>(_micro_seconds));				
       
   387 		}
       
   388 
       
   389 	}
       
   390 	else
       
   391 	{
       
   392 		_LIT8(KFormat2, "%08x%08x:EAPOL:");
       
   393 
       
   394 		u32_t *time_stamp_u32_t = reinterpret_cast<u32_t *>(&time_stamp);
       
   395 		m_trace_buf.Format(KFormat2, time_stamp_u32_t[1], time_stamp_u32_t[0]);		
       
   396 	}
       
   397 
       
   398 	VA_LIST args = {0,};
       
   399 	VA_START(args, format);
       
   400 	m_format_buf.Copy((const TUint8 *)format);
       
   401 	
       
   402 	m_args_buf.FormatList(m_format_buf, args);
       
   403 	m_trace_buf.Append(m_args_buf);			
       
   404 	VA_END(args);
       
   405 
       
   406 
       
   407 #if defined(USE_EAP_HARDWARE_TRACE)
       
   408 
       
   409 	{
       
   410 		#if !defined(USE_EAP_HARDWARE_TRACE_RAW_PRINT)
       
   411 		{
       
   412 			TInt length = m_trace_buf.Length();
       
   413 			if (length >= 2ul)
       
   414 			{
       
   415 				--length;
       
   416 				const TUint8 *last_char = m_trace_buf.Ptr() + length;
       
   417 
       
   418 				if (last_char != 0
       
   419 					&& *last_char == '\n')
       
   420 				{
       
   421 					// This removes the ending new line character.
       
   422 					// RDebug::Print() will write new line automatically.
       
   423 					m_trace_buf.SetLength(length);
       
   424 				}
       
   425 			}
       
   426 		}
       
   427 		#endif //#if !defined(USE_EAP_HARDWARE_TRACE_RAW_PRINT)
       
   428 		
       
   429 		m_trace_buf_16.Copy(m_trace_buf);
       
   430 
       
   431 		#if defined(USE_EAP_HARDWARE_TRACE_RAW_PRINT)
       
   432 			RDebug::RawPrint(m_trace_buf_16);
       
   433 		#else
       
   434 			RDebug::Print(_L("%S"), &m_trace_buf_16);
       
   435 		#endif //#if defined(USE_EAP_HARDWARE_TRACE_RAW_PRINT)
       
   436 	}
       
   437 
       
   438 #endif //#if defined(USE_EAP_HARDWARE_TRACE)
       
   439 
       
   440 
       
   441 #if defined(USE_EAP_FILE_TRACE)
       
   442 
       
   443 	#if defined (USE_MULTITHREADING)
       
   444 		if (m_filename.Length() > 0ul)
       
   445 		{
       
   446 			RFs session;
       
   447 			
       
   448 			TInt result = session.Connect();	
       
   449 			if (result != KErrNone)
       
   450 			{
       
   451 				leave_trace_mutex();
       
   452 				EAP_TRACE_END(this, TRACE_FLAGS_DEFAULT);
       
   453 				return;
       
   454 			}	
       
   455 			
       
   456 			TInt err = m_LogFile.Open(session, m_filename, EFileWrite | EFileShareAny);
       
   457 			if (err != KErrNone)
       
   458 			{			
       
   459 				session.Close();
       
   460 				leave_trace_mutex();
       
   461 				EAP_TRACE_END(this, TRACE_FLAGS_DEFAULT);
       
   462 				return;
       
   463 			}
       
   464 			m_logfile_open = true;
       
   465 		}
       
   466 	#endif //#if defined (USE_MULTITHREADING)
       
   467 
       
   468 
       
   469 	if (m_logfile_open == true)
       
   470 	{
       
   471 		//  possible errors in the write because we can't do anything if
       
   472 		// for example disk is full.
       
   473 		TInt pos = 0;
       
   474 		
       
   475 		// Seek to the end of file for appending
       
   476 		m_LogFile.Seek(ESeekEnd, pos);
       
   477 		m_LogFile.Write(m_trace_buf);
       
   478 		// This flush crashed sometimes for an unknown reason...
       
   479 		m_LogFile.Flush();
       
   480 	}
       
   481 		
       
   482 	#if defined (USE_MULTITHREADING)
       
   483 		if (m_logfile_open == true)
       
   484 		{
       
   485 			m_logfile_open = false;
       
   486 			m_LogFile.Close();
       
   487 		}
       
   488 		session.Close();
       
   489 	#endif //#if defined (USE_MULTITHREADING)
       
   490 
       
   491 #endif //#if defined(USE_EAP_FILE_TRACE)
       
   492 
       
   493 
       
   494 	leave_trace_mutex();
       
   495 
       
   496 #endif //#if defined(USE_EAP_TRACE) || defined(USE_EAP_TRACE_ALWAYS)
       
   497 
       
   498 }
       
   499 
       
   500 
       
   501 //--------------------------------------------------
       
   502 
       
   503 //
       
   504 EAP_FUNC_EXPORT bool eap_am_tools_symbian_c::get_timer_queue_is_empty()
       
   505 {
       
   506 	return m_timer_queue.get_timer_queue_is_empty();
       
   507 }
       
   508 
       
   509 //--------------------------------------------------
       
   510 
       
   511 //
       
   512 EAP_FUNC_EXPORT bool eap_am_tools_symbian_c::get_is_timer_thread_active()
       
   513 {
       
   514 	return m_run_thread;
       
   515 }
       
   516 
       
   517 
       
   518 //--------------------------------------------------
       
   519 
       
   520 //
       
   521 EAP_FUNC_EXPORT eap_status_e eap_am_tools_symbian_c::set_trace_file_name(
       
   522 	const eap_variable_data_c * const trace_output_file)
       
   523 {
       
   524 	EAP_TRACE_BEGIN(this, TRACE_FLAGS_DEFAULT);
       
   525 
       
   526 	EAP_UNREFERENCED_PARAMETER(trace_output_file);
       
   527 	
       
   528 #if defined(USE_EAP_FILE_TRACE)
       
   529 #if defined(USE_EAP_TRACE) || defined(USE_EAP_TRACE_ALWAYS)
       
   530 	TBuf8<64> tmpFilename((TUint8 *)trace_output_file->get_data(trace_output_file->get_data_length()));
       
   531 	tmpFilename.SetLength(trace_output_file->get_data_length());
       
   532 	enter_trace_mutex();
       
   533 
       
   534 	m_filename.Copy(tmpFilename);
       
   535 
       
   536 #if defined (USE_MULTITHREADING)
       
   537 	TInt result = m_Fs.Connect();	
       
   538 	if (result != KErrNone)
       
   539 	{
       
   540 		EAP_TRACE_END(this, TRACE_FLAGS_DEFAULT);
       
   541 		return convert_am_error_to_eapol_error(result);
       
   542 	}
       
   543 #endif //#if defined (USE_MULTITHREADING)
       
   544 
       
   545 	TInt err(KErrPathNotFound);
       
   546 
       
   547 	if (m_filename.Length() > 0ul)
       
   548 	{
       
   549 		err = m_LogFile.Create(m_Fs, m_filename, EFileWrite | EFileShareAny);
       
   550 		if (err == KErrAlreadyExists)
       
   551 		{
       
   552 			err = m_LogFile.Open(m_Fs, m_filename, EFileWrite | EFileShareAny);
       
   553 		}
       
   554 		else if (err == KErrPathNotFound)
       
   555 		{
       
   556 #if !defined(USE_EAP_HARDWARE_TRACE)
       
   557 			// No directory neither RDEBUG -> Disable traces
       
   558 			set_trace_mask(eap_am_tools_c::eap_trace_mask_none);
       
   559 #endif //#if !defined(USE_EAP_HARDWARE_TRACE)
       
   560 		}
       
   561 	}
       
   562 	
       
   563 	if (err != KErrNone)
       
   564 	{
       
   565 		m_logfile_open = false;
       
   566 #if defined (USE_MULTITHREADING)
       
   567 		m_Fs.Close();
       
   568 #endif //#if defined (USE_MULTITHREADING)
       
   569 		leave_trace_mutex();
       
   570 		EAP_TRACE_END(this, TRACE_FLAGS_DEFAULT);
       
   571 		return convert_am_error_to_eapol_error(err);
       
   572 	}
       
   573 	else
       
   574 	{
       
   575 		// Trace file opened OK.
       
   576 		m_logfile_open = true;
       
   577 	}
       
   578 
       
   579 #if defined (USE_MULTITHREADING)
       
   580 	m_LogFile.Close();	
       
   581 	m_logfile_open = false;
       
   582 	m_Fs.Close();
       
   583 #endif //#if defined (USE_MULTITHREADING)
       
   584 
       
   585 	leave_trace_mutex();
       
   586 	
       
   587 #endif //#if defined(USE_EAP_TRACE) || defined(USE_EAP_TRACE_ALWAYS)
       
   588 #endif //#if defined(USE_EAP_FILE_TRACE)
       
   589 
       
   590 
       
   591 	EAP_TRACE_END(this, TRACE_FLAGS_DEFAULT);
       
   592 	return eap_status_ok;
       
   593 }
       
   594 
       
   595 //--------------------------------------------------
       
   596 
       
   597 //
       
   598 EAP_FUNC_EXPORT void eap_am_tools_symbian_c::set_max_trace_file_size(const u32_t max_trace_file_size)
       
   599 {
       
   600 #if defined(USE_EAP_TRACE_FILE_MAX_SIZE)
       
   601 	m_max_trace_file_size = max_trace_file_size;
       
   602 #else
       
   603 	EAP_UNREFERENCED_PARAMETER(max_trace_file_size);
       
   604 #endif //#if defined(USE_EAP_TRACE_FILE_MAX_SIZE)
       
   605 }
       
   606 
       
   607 //--------------------------------------------------
       
   608 
       
   609 //
       
   610 EAP_FUNC_EXPORT void eap_am_tools_symbian_c::memmove(void *dest, const void *src, const u32_t count)
       
   611 {
       
   612 	EAP_TRACE_BEGIN(this, TRACE_FLAGS_DEFAULT);
       
   613 	Mem::Copy(dest, src, count);
       
   614 	EAP_TRACE_END(this, TRACE_FLAGS_DEFAULT);
       
   615 }
       
   616 
       
   617 //--------------------------------------------------
       
   618 
       
   619 //
       
   620 EAP_FUNC_EXPORT i32_t eap_am_tools_symbian_c::memcmp(const void * const dest, const void * const src, const u32_t count)
       
   621 {
       
   622 	EAP_TRACE_BEGIN(this, TRACE_FLAGS_DEFAULT);
       
   623 	EAP_TRACE_END(this, TRACE_FLAGS_DEFAULT);
       
   624 	return Mem::Compare((const TUint8*)dest, count, (const TUint8*)src, count);
       
   625 }
       
   626 
       
   627 //--------------------------------------------------
       
   628 
       
   629 //
       
   630 EAP_FUNC_EXPORT void eap_am_tools_symbian_c::memset(void * const src, const i32_t fill_byte, const u32_t count)
       
   631 {
       
   632 	EAP_TRACE_BEGIN(this, TRACE_FLAGS_DEFAULT);
       
   633 	Mem::Fill(src, count, fill_byte);
       
   634 	EAP_TRACE_END(this, TRACE_FLAGS_DEFAULT);
       
   635 }
       
   636 
       
   637 //--------------------------------------------------
       
   638 
       
   639 //
       
   640 EAP_FUNC_EXPORT void *eap_am_tools_symbian_c::memchr(
       
   641 		const void *buf,
       
   642 		u8_t character,
       
   643 		u32_t count)
       
   644 {
       
   645 	EAP_TRACE_BEGIN(this, TRACE_FLAGS_DEFAULT);
       
   646 
       
   647 	const u8_t * const tmp = reinterpret_cast<const u8_t *>(buf);
       
   648 	
       
   649 	for (u32_t ind = 0u; ind < count; ind++)
       
   650 	{
       
   651 		if (tmp[ind] == character)
       
   652 		{
       
   653 			return reinterpret_cast<void *>(const_cast<u8_t *>(&(tmp[ind])));
       
   654 		}
       
   655 	}
       
   656 
       
   657 	EAP_TRACE_END(this, TRACE_FLAGS_DEFAULT);
       
   658 
       
   659 	return 0;
       
   660 }
       
   661 
       
   662 //--------------------------------------------------
       
   663 
       
   664 //
       
   665 EAP_FUNC_EXPORT void *eap_am_tools_symbian_c::memrchr(
       
   666 		const void *buf,
       
   667 		u8_t character,
       
   668 		u32_t count)
       
   669 {
       
   670 	EAP_TRACE_BEGIN(this, TRACE_FLAGS_DEFAULT);
       
   671 
       
   672 	const u8_t * const tmp = reinterpret_cast<const u8_t *>(buf);
       
   673 	
       
   674 	for (i32_t ind = count-1ul; ind >= 0; ind--)
       
   675 	{
       
   676 		if (tmp[ind] == character)
       
   677 		{
       
   678 			return reinterpret_cast<void *>(const_cast<u8_t *>(&(tmp[ind])));
       
   679 		}
       
   680 	}
       
   681 
       
   682 	EAP_TRACE_END(this, TRACE_FLAGS_DEFAULT);
       
   683 
       
   684 	return 0;
       
   685 }
       
   686 
       
   687 //--------------------------------------------------
       
   688 
       
   689 //
       
   690 EAP_FUNC_EXPORT u32_t eap_am_tools_symbian_c::strlen(
       
   691 		eap_const_string string)
       
   692 {
       
   693 	EAP_TRACE_BEGIN(this, TRACE_FLAGS_DEFAULT);
       
   694 
       
   695 	u32_t count = config_strlen(string);
       
   696 
       
   697 	EAP_TRACE_END(this, TRACE_FLAGS_DEFAULT);
       
   698 	return count;
       
   699 }
       
   700 
       
   701 //--------------------------------------------------
       
   702 
       
   703 //
       
   704 EAP_FUNC_EXPORT u32_t eap_am_tools_symbian_c::config_strlen(
       
   705 		eap_config_string string)
       
   706 {
       
   707 	EAP_TRACE_BEGIN(this, TRACE_FLAGS_DEFAULT);
       
   708 	u32_t count = 0;
       
   709 	const u8_t *tmp = reinterpret_cast<const u8_t *>(string);
       
   710 	while((*tmp) != 0)
       
   711 	{
       
   712 		tmp++;
       
   713 		count++;
       
   714 	}
       
   715 	EAP_TRACE_END(this, TRACE_FLAGS_DEFAULT);
       
   716 	return count;
       
   717 }
       
   718 
       
   719 //--------------------------------------------------
       
   720 
       
   721 //
       
   722 EAP_FUNC_EXPORT abs_eap_am_crypto_c * eap_am_tools_symbian_c::get_crypto()
       
   723 {
       
   724 	EAP_TRACE_BEGIN(this, TRACE_FLAGS_DEFAULT);
       
   725 	EAP_TRACE_END(this, TRACE_FLAGS_DEFAULT);
       
   726 	return &m_crypto;
       
   727 }
       
   728 
       
   729 //--------------------------------------------------
       
   730 
       
   731 //
       
   732 EAP_FUNC_EXPORT u64_t eap_am_tools_symbian_c::get_hardware_ticks()
       
   733 {
       
   734 	return get_clock_ticks();
       
   735 }
       
   736 
       
   737 //--------------------------------------------------
       
   738 
       
   739 //
       
   740 EAP_FUNC_EXPORT u64_t eap_am_tools_symbian_c::get_hardware_ticks_of_second()
       
   741 {
       
   742 	return get_clock_ticks_of_second();
       
   743 }
       
   744 
       
   745 //--------------------------------------------------
       
   746 
       
   747 //
       
   748 EAP_FUNC_EXPORT u64_t eap_am_tools_symbian_c::get_clock_ticks()
       
   749 {
       
   750 	#if defined(__SYMBIAN32__)
       
   751 		TTime time;
       
   752 		time.UniversalTime();
       
   753 		return *(reinterpret_cast<u64_t *>(&time));
       
   754 	#else
       
   755 		#error get_clock_ticks() not supported, please do something
       
   756 	#endif
       
   757 }
       
   758 
       
   759 //--------------------------------------------------
       
   760 
       
   761 //
       
   762 EAP_FUNC_EXPORT u64_t eap_am_tools_symbian_c::get_clock_ticks_of_second()
       
   763 {
       
   764 	return 1000000u;
       
   765 }
       
   766 
       
   767 //--------------------------------------------------
       
   768 
       
   769 /**
       
   770  * This function adds EAP timer to queue and sets the platform specific timer for first timer in the queue.
       
   771  */
       
   772 EAP_FUNC_EXPORT eap_status_e eap_am_tools_symbian_c::am_set_timer(
       
   773 	abs_eap_base_timer_c * const initializer, 
       
   774 	const u32_t id, 
       
   775 	void * const data,
       
   776 	const u32_t p_time_ms)
       
   777 {
       
   778 	EAP_TRACE_BEGIN(this, TRACE_FLAGS_DEFAULT);
       
   779 
       
   780 	if (get_use_timer_queue() == false)
       
   781 	{
       
   782 		// We do not need timers.
       
   783 		initializer->timer_delete_data(id, data);
       
   784 
       
   785 		EAP_TRACE_DEBUG(this, TRACE_FLAGS_TIMER, (EAPL("end am_set_timer().\n")));
       
   786 		EAP_TRACE_END(this, TRACE_FLAGS_DEFAULT);
       
   787 		return EAP_STATUS_RETURN(this, eap_status_not_supported);
       
   788 	}
       
   789 	else
       
   790 	{
       
   791 		const eap_status_e status = m_timer_queue.set_timer(
       
   792 			initializer,
       
   793 			id,
       
   794 			data,
       
   795 			p_time_ms);
       
   796 			
       
   797 		if (IsActive())
       
   798 		{
       
   799 			// This call cancels the current symbian timer.
       
   800 			// We will start new symbian timer with new
       
   801 			// timeout read from timer queue.
       
   802 			Cancel();
       
   803 		}
       
   804 		
       
   805 		u64_t currentTime = get_clock_ticks();
       
   806 		u32_t tics_of_millisecond = static_cast<u32_t>(get_hardware_ticks_of_second())/1000ul;
       
   807 		u32_t spend_time = static_cast<u32_t>(currentTime - iLastTime)/tics_of_millisecond;
       
   808 
       
   809 		iLastTime = currentTime;
       
   810 		
       
   811 		EAP_TRACE_DEBUG(
       
   812 			this,
       
   813 			TRACE_FLAGS_TIMER,
       
   814 			(EAPL("eap_status_e eap_am_tools_symbian_c::am_set_timer(): pulse_timer(%d ms).\n"),
       
   815 			spend_time));
       
   816 
       
   817 		u32_t next_timeout_milliseconds = m_timer_queue.pulse_timer(spend_time, false);
       
   818 
       
   819 		After(limit_microsecond_timeout(next_timeout_milliseconds));
       
   820 		
       
   821 		EAP_TRACE_DEBUG(
       
   822 			this,
       
   823 			TRACE_FLAGS_TIMER,
       
   824 			(EAPL("end eap_am_tools_symbian_c::am_set_timer(): After(%d millisecond).\n"),
       
   825 			next_timeout_milliseconds));
       
   826 
       
   827 		EAP_TRACE_END(this, TRACE_FLAGS_DEFAULT);
       
   828 		return EAP_STATUS_RETURN(this, status);
       
   829 	}
       
   830 }
       
   831 
       
   832 //--------------------------------------------------
       
   833 
       
   834 //
       
   835 EAP_FUNC_EXPORT eap_status_e eap_am_tools_symbian_c::am_cancel_timer(
       
   836 	abs_eap_base_timer_c * const initializer, 
       
   837 	const u32_t id)
       
   838 {
       
   839 	EAP_TRACE_BEGIN(this, TRACE_FLAGS_DEFAULT);
       
   840 
       
   841 	if (get_use_timer_queue() == false)
       
   842 	{
       
   843 		// We do not need timers.
       
   844 		EAP_TRACE_DEBUG(this, TRACE_FLAGS_TIMER, (EAPL("end am_cancel_timer().\n")));
       
   845 		EAP_TRACE_END(this, TRACE_FLAGS_DEFAULT);
       
   846 		return EAP_STATUS_RETURN(this, eap_status_not_supported);
       
   847 	}
       
   848 	else
       
   849 	{
       
   850 		const eap_status_e status = m_timer_queue.cancel_timer(initializer, id);
       
   851 
       
   852 		EAP_TRACE_DEBUG(this, TRACE_FLAGS_TIMER, (EAPL("end am_cancel_timer().\n")));
       
   853 		EAP_TRACE_END(this, TRACE_FLAGS_DEFAULT);
       
   854 		return status;
       
   855 	}
       
   856 }
       
   857 
       
   858 //--------------------------------------------------
       
   859 
       
   860 EAP_FUNC_EXPORT eap_status_e eap_am_tools_symbian_c::am_cancel_all_timers()
       
   861 {
       
   862 	EAP_TRACE_BEGIN(this, TRACE_FLAGS_DEFAULT);
       
   863 
       
   864 	EAP_TRACE_DEBUG(this, TRACE_FLAGS_TIMER, (EAPL("begin am_cancel_all_timers().\n")));
       
   865 
       
   866 	if (get_use_timer_queue() == false)
       
   867 	{
       
   868 		// We do not need timers.
       
   869 		EAP_TRACE_DEBUG(this, TRACE_FLAGS_TIMER, (EAPL("end am_cancel_all_timers().\n")));
       
   870 		EAP_TRACE_END(this, TRACE_FLAGS_DEFAULT);
       
   871 		return EAP_STATUS_RETURN(this, eap_status_not_supported);
       
   872 	}
       
   873 	else
       
   874 	{
       
   875 		enter_global_mutex();
       
   876 		const eap_status_e status = m_timer_queue.cancel_all_timers();
       
   877 		leave_global_mutex();
       
   878 
       
   879 		EAP_TRACE_DEBUG(this, TRACE_FLAGS_TIMER, (EAPL("end am_cancel_all_timers().\n")));
       
   880 		EAP_TRACE_END(this, TRACE_FLAGS_DEFAULT);
       
   881 		return status;
       
   882 	}
       
   883 }
       
   884 
       
   885 //--------------------------------------------------
       
   886 
       
   887 EAP_FUNC_EXPORT eap_status_e eap_am_tools_symbian_c::re_activate_timer_queue()
       
   888 {
       
   889 	EAP_TRACE_BEGIN(this, TRACE_FLAGS_DEFAULT);
       
   890 	EAP_TRACE_DEBUG(this, TRACE_FLAGS_DEFAULT, (EAPL("re_activate_timer_queue().\n")));
       
   891 
       
   892 	const eap_status_e status =  m_timer_queue.re_activate_timer_queue();
       
   893 
       
   894 	EAP_TRACE_END(this, TRACE_FLAGS_DEFAULT);
       
   895 	return EAP_STATUS_RETURN(this, status);
       
   896 }
       
   897 
       
   898 //--------------------------------------------------
       
   899 
       
   900 EAP_FUNC_EXPORT void eap_am_tools_symbian_c::enter_crypto_cs()
       
   901 {
       
   902 
       
   903 }
       
   904 
       
   905 EAP_FUNC_EXPORT void eap_am_tools_symbian_c::leave_crypto_cs()
       
   906 {
       
   907 
       
   908 }
       
   909 
       
   910 //--------------------------------------------------
       
   911 
       
   912 EAP_FUNC_EXPORT void eap_am_tools_symbian_c::timer_sleep(u32_t milli_seconds)
       
   913 {
       
   914 	sleep(milli_seconds);
       
   915 }
       
   916 
       
   917 //--------------------------------------------------
       
   918 
       
   919 EAP_FUNC_EXPORT void eap_am_tools_symbian_c::sleep(u32_t milli_seconds)
       
   920 {
       
   921 	User::After(limit_microsecond_timeout(milli_seconds));
       
   922 }
       
   923 
       
   924 //--------------------------------------------------
       
   925 EAP_FUNC_EXPORT u32_t eap_am_tools_symbian_c::get_gmt_unix_time()
       
   926 {
       
   927 	_LIT(KStart, "19700000:000000.000000");
       
   928 	TTime start(KStart);
       
   929 	TTime now;
       
   930 	now.UniversalTime();
       
   931 	TTimeIntervalSeconds interval;
       
   932 	now.SecondsFrom(start, interval);
       
   933 	return interval.Int();
       
   934 }
       
   935 //--------------------------------------------------
       
   936 
       
   937 EAP_FUNC_EXPORT bool eap_am_tools_symbian_c::get_is_valid() const
       
   938 {
       
   939 	return m_is_valid;
       
   940 }
       
   941 
       
   942 //--------------------------------------------------
       
   943 
       
   944 EAP_FUNC_EXPORT eap_status_e eap_am_tools_symbian_c::convert_am_error_to_eapol_error(const i32_t aErr)
       
   945 {
       
   946 	EAP_TRACE_DEBUG(
       
   947 		this,
       
   948 		TRACE_FLAGS_DEFAULT,
       
   949 		(EAPL("eap_am_tools_symbian_c::convert_am_error_to_eapol_error: error=%d\n"),
       
   950 		aErr));
       
   951 
       
   952 	eap_status_e status;
       
   953 	switch (aErr)
       
   954 	{
       
   955 	case KErrNone:
       
   956 		status = eap_status_ok;
       
   957 		break;
       
   958 
       
   959 	case KErrNoMemory:
       
   960 		status = eap_status_allocation_error;
       
   961 		break;
       
   962 
       
   963 	case KErrNotSupported:
       
   964 		status = eap_status_not_supported;
       
   965 		break;
       
   966 	
       
   967 	case KErrBadHandle:
       
   968 		status = eap_status_illegal_handle;
       
   969 		break;
       
   970 		
       
   971 	case KErrCompletion:
       
   972 		status = eap_status_pending_request;
       
   973 		break;
       
   974 
       
   975 	case KErrNotFound:
       
   976 		status = eap_status_illegal_configure_field;
       
   977 		break;
       
   978 
       
   979 	case KErrGeneral:
       
   980 	case KErrUnknown:
       
   981 	case KErrCancel:
       
   982 	case KErrArgument:
       
   983 	case KErrTotalLossOfPrecision:
       
   984 	case KErrOverflow:
       
   985 	case KErrUnderflow:
       
   986 	case KErrAlreadyExists:
       
   987 	case KErrPathNotFound:
       
   988 	case KErrDied:
       
   989 	case KErrInUse:
       
   990 	case KErrServerTerminated:
       
   991 	case KErrServerBusy:
       
   992 	case KErrNotReady:	
       
   993 	case KErrCorrupt:
       
   994 	case KErrAccessDenied:
       
   995 	case KErrLocked:
       
   996 	case KErrWrite:
       
   997 	case KErrDisMounted:
       
   998 	case KErrEof:
       
   999 	case KErrDiskFull:
       
  1000 	case KErrBadDriver:
       
  1001 	case KErrBadName:
       
  1002 	case KErrCommsLineFail:
       
  1003 	case KErrCommsFrame:
       
  1004 	case KErrCommsOverrun:
       
  1005 	case KErrCommsParity:
       
  1006 	case KErrTimedOut:
       
  1007 	case KErrCouldNotConnect:
       
  1008 	case KErrCouldNotDisconnect:
       
  1009 	case KErrDisconnected:
       
  1010 	case KErrBadLibraryEntryPoint:
       
  1011 	case KErrBadDescriptor:
       
  1012 	case KErrAbort:
       
  1013 	case KErrTooBig:
       
  1014 	case KErrDivideByZero:
       
  1015 	case KErrBadPower:
       
  1016 	case KErrDirFull:
       
  1017 	case KErrHardwareNotAvailable:
       
  1018 	default:
       
  1019 		status = eap_status_process_general_error;
       
  1020 		break;		
       
  1021 	}
       
  1022 	return status;
       
  1023 }
       
  1024 
       
  1025 EAP_FUNC_EXPORT i32_t eap_am_tools_symbian_c::convert_eapol_error_to_am_error(eap_status_e aErr)
       
  1026 {
       
  1027 	EAP_TRACE_DEBUG(
       
  1028 		this,
       
  1029 		TRACE_FLAGS_DEFAULT,
       
  1030 		(EAPL("eap_am_tools_symbian_c::convert_am_error_to_eapol_error: error=%d\n"),
       
  1031 		aErr));
       
  1032 
       
  1033 	TInt status;
       
  1034 	switch (aErr)
       
  1035 	{
       
  1036 	case eap_status_ok: 
       
  1037 	case eap_status_success:
       
  1038 		status = KErrNone;
       
  1039 		break;
       
  1040 	
       
  1041 	case eap_status_allocation_error: 
       
  1042 		status = KErrNoMemory;
       
  1043 		break;
       
  1044 
       
  1045 	case eap_status_not_supported: 
       
  1046 		status = KErrNotSupported;
       
  1047 		break;
       
  1048 
       
  1049 
       
  1050 	case eap_status_illegal_handle:
       
  1051 		status = KErrBadHandle;
       
  1052 		break;
       
  1053 
       
  1054 	case eap_status_pending_request:
       
  1055 		status = KErrCompletion;
       
  1056 		break;
       
  1057 
       
  1058 	case eap_status_illegal_configure_field:
       
  1059 		status = KErrNotFound;
       
  1060 		break;
       
  1061 
       
  1062 	case eap_status_completed_request:
       
  1063 	case eap_status_drop_packet_quietly:	
       
  1064 	case eap_status_process_general_error: 
       
  1065 	case eap_status_type_does_not_exists_error: 
       
  1066 	case eap_status_process_illegal_packet_error:
       
  1067 	case eap_status_ethernet_type_not_supported:
       
  1068 	case eap_status_illegal_eap_code:
       
  1069 	case eap_status_illegal_eap_type:
       
  1070 	case eap_status_illegal_eap_identity:
       
  1071 	case eap_status_authentication_failure:
       
  1072 	case eap_status_encryption_failure:
       
  1073 	case eap_status_illegal_padding:
       
  1074 	case eap_status_randomize_failure:
       
  1075 	case eap_status_handler_exists_error:
       
  1076 	case eap_status_handler_does_not_exists_error:
       
  1077 	case eap_status_wrong_sae_state:
       
  1078 	case eap_status_wrong_eapol_version:
       
  1079 	case eap_status_wrong_eapol_type:
       
  1080 	case eap_status_header_corrupted:
       
  1081 	case eap_status_wrong_sae_sequence_number:
       
  1082 	case eap_status_wrong_isakmp_header_version:
       
  1083 	case eap_status_wrong_isakmp_exchange_type:
       
  1084 	case eap_status_wrong_isakmp_flags:
       
  1085 	case eap_status_wrong_isakmp_message_id:
       
  1086 	case eap_status_wrong_isakmp_cookie:
       
  1087 	case eap_status_unsupported_isakmp_payload:
       
  1088 	case eap_status_key_error:
       
  1089 	case eap_status_too_many_offers:
       
  1090 	case eap_status_send_failed:
       
  1091 	case eap_status_data_length_not_aligned_to_block_size:
       
  1092 	case eap_status_wrong_network_id:	
       
  1093 	case eap_status_wrong_sae_header_version:
       
  1094 	case eap_status_wrong_sae_exchange_type:
       
  1095 	case eap_status_wrong_sae_flags:
       
  1096 	case eap_status_unsupported_sae_payload:
       
  1097 	case eap_status_wrong_sae_cookie:
       
  1098 	case eap_status_illegal_encryption_parameter_size:
       
  1099 	case eap_status_state_reference_count_not_zero:
       
  1100 	case eap_status_illegal_nai:
       
  1101 	case eap_status_illegal_nai_payload:
       
  1102 	case eap_status_illegal_data_payload:
       
  1103 	case eap_status_illegal_payload:
       
  1104 	case eap_status_illegal_hashed_index:
       
  1105 	case eap_status_wrong_gsmsim_state:
       
  1106 	case eap_status_unsupported_gsmsim_payload:
       
  1107 	case eap_status_gsmsim_triplet_query_failed:
       
  1108 	case eap_status_illegal_index:
       
  1109 	case eap_status_timed_out:
       
  1110 	case eap_status_wrong_eap_subtype:
       
  1111 	case eap_status_exit_test:
       
  1112 	case eap_status_no_matching_protocol_version:
       
  1113 	default:
       
  1114 		status = KErrGeneral;
       
  1115 		break;	
       
  1116 	}
       
  1117 	
       
  1118 	return status;
       
  1119 }
       
  1120 
       
  1121 //--------------------------------------------------
       
  1122 
       
  1123 EAP_FUNC_EXPORT eap_status_e eap_am_tools_symbian_c::begin_db_transaction(RDbNamedDatabase& aDatabase)
       
  1124 {
       
  1125 	TUint retries(0);
       
  1126 	TInt result;
       
  1127 	eap_status_e status(eap_status_access_denied);
       
  1128 
       
  1129 	if (aDatabase.InTransaction())
       
  1130 	{
       
  1131 		// Previous database operation failed
       
  1132 		aDatabase.Rollback();
       
  1133 	}
       
  1134 	
       
  1135 	// Seed the PRNG with time
       
  1136 	TTime now;
       
  1137 	now.HomeTime();
       
  1138 	TInt64 seed = now.Int64();
       
  1139 	TInt randomWait; 
       
  1140 	
       
  1141 	// Try to begin transaction MAX_DB_TRANSACTION_RETRY_COUNT times
       
  1142 	while (retries < MAX_DB_TRANSACTION_RETRY_COUNT)
       
  1143 	{
       
  1144 		result = aDatabase.Begin();
       
  1145 		if (result == KErrNone)
       
  1146 		{
       
  1147 			// We got read lock to database!
       
  1148 			status = eap_status_ok;
       
  1149 			break;
       
  1150 		}
       
  1151 		EAP_TRACE_DEBUG(this, TRACE_FLAGS_DEFAULT, (EAPL("begin_db_transaction(): database locked.\n")));
       
  1152 		retries++;
       
  1153 		randomWait = Math::Rand(seed);
       
  1154 		
       
  1155 		// Wait 0 - 524287 microseconds
       
  1156 		randomWait = randomWait & 0x7ffff;
       
  1157 		User::After(randomWait);
       
  1158 	}
       
  1159 	return EAP_STATUS_RETURN(this, status);
       
  1160 }
       
  1161 
       
  1162 //--------------------------------------------------
       
  1163 
       
  1164 EAP_FUNC_EXPORT eap_status_e eap_am_tools_symbian_c::begin_db_update(RDbView& aView)
       
  1165 {
       
  1166 	TUint retries(0);
       
  1167 	eap_status_e status(eap_status_access_denied);
       
  1168 
       
  1169 	// Seed the PRNG with time
       
  1170 	TTime now;
       
  1171 	now.HomeTime();
       
  1172 	TInt64 seed = now.Int64();
       
  1173 	
       
  1174 	TInt randomWait; 
       
  1175 	TInt err;	
       
  1176 	// Try to begin transaction MAX_DB_TRANSACTION_RETRY_COUNT times
       
  1177 	while (retries < MAX_DB_TRANSACTION_RETRY_COUNT)
       
  1178 	{
       
  1179 		TRAP(err, aView.UpdateL());
       
  1180 		if (err == KErrNone)
       
  1181 		{
       
  1182 			// We got write lock to database!
       
  1183 			status = eap_status_ok;
       
  1184 			break;
       
  1185 		}
       
  1186 		EAP_TRACE_DEBUG(this, TRACE_FLAGS_DEFAULT, (EAPL("begin_db_update(): database locked.\n")));
       
  1187 		retries++;
       
  1188 		randomWait = Math::Rand(seed);
       
  1189 		
       
  1190 		// Wait 0 - 524287 microseconds
       
  1191 		randomWait = randomWait & 0x7ffff;
       
  1192 		User::After(randomWait);
       
  1193 	}
       
  1194 	return EAP_STATUS_RETURN(this, status);
       
  1195 }
       
  1196 
       
  1197 //--------------------------------------------------
       
  1198 
       
  1199 EAP_FUNC_EXPORT eap_status_e eap_am_tools_symbian_c::begin_db_delete(RDbView& aView)
       
  1200 {
       
  1201 	TUint retries(0);
       
  1202 	eap_status_e status(eap_status_access_denied);
       
  1203 	
       
  1204 	// Seed the PRNG with time
       
  1205 	TTime now;
       
  1206 	now.HomeTime();
       
  1207 	TInt64 seed = now.Int64();
       
  1208 	
       
  1209 	TInt randomWait; 
       
  1210 	TInt err;	
       
  1211 	// Try to begin transaction MAX_DB_TRANSACTION_RETRY_COUNT times
       
  1212 	while (retries < MAX_DB_TRANSACTION_RETRY_COUNT)
       
  1213 	{
       
  1214 		TRAP(err, aView.DeleteL());
       
  1215 		if (err == KErrNone)
       
  1216 		{
       
  1217 			// We got write lock to database!
       
  1218 			status = eap_status_ok;
       
  1219 			break;
       
  1220 		}
       
  1221 		EAP_TRACE_DEBUG(this, TRACE_FLAGS_DEFAULT, (EAPL("begin_db_delete(): database locked.\n")));
       
  1222 		retries++;
       
  1223 		randomWait = Math::Rand(seed);
       
  1224 		
       
  1225 		// Wait 0 - 524287 microseconds
       
  1226 		randomWait = randomWait & 0x7ffff;
       
  1227 		User::After(randomWait);
       
  1228 	}
       
  1229 	return EAP_STATUS_RETURN(this, status);
       
  1230 }
       
  1231 
       
  1232 //--------------------------------------------------
       
  1233 
       
  1234 /// This function must call eap_am_tools_c::shutdown_am_tools().
       
  1235 EAP_FUNC_EXPORT eap_status_e eap_am_tools_symbian_c::shutdown()
       
  1236 {
       
  1237 	EAP_TRACE_ALWAYS(
       
  1238 		this, 
       
  1239 		TRACE_FLAGS_DEFAULT, 
       
  1240 		(EAPL("eap_am_tools_symbian_c::shutdown(): this = 0x%08x => 0x%08x, "),
       
  1241 		 this,
       
  1242 		 dynamic_cast<abs_eap_base_timer_c *>(this)));
       
  1243 
       
  1244 	StopTimer();
       
  1245 	
       
  1246 	return eap_am_tools_c::shutdown_am_tools();
       
  1247 }
       
  1248 
       
  1249 //--------------------------------------------------
       
  1250 
       
  1251 /// This function converts unicode string to UTF8 string.
       
  1252 EAP_FUNC_EXPORT eap_status_e eap_am_tools_symbian_c::convert_unicode_to_utf8(
       
  1253 	eap_variable_data_c & dest,
       
  1254 	const eap_variable_data_c & src)
       
  1255 {
       
  1256 	eap_status_e status = eap_status_allocation_error;
       
  1257 	
       
  1258 	HBufC8* outputBuffer = NULL;
       
  1259 	// src is unicode string, length = size / 2, one utf8 char takes 1...6 bytes
       
  1260 	TRAPD(err, outputBuffer = HBufC8::NewL((src.get_data_length() / 2) * 6))
       
  1261 	if (err != KErrNone)
       
  1262 	{
       
  1263 		status = convert_am_error_to_eapol_error(err);
       
  1264 		return EAP_STATUS_RETURN(this, status);
       
  1265 	} 
       
  1266 
       
  1267 	const TPtrC16 unicode(
       
  1268 		reinterpret_cast<unsigned short *>(src.get_data(src.get_data_length())),
       
  1269 		src.get_data_length() / 2); // Length in unicode characters
       
  1270 
       
  1271 	TPtr8 utf8 = outputBuffer->Des();
       
  1272 
       
  1273 	CnvUtfConverter::ConvertFromUnicodeToUtf8(utf8, unicode);
       
  1274 
       
  1275 	status = dest.set_copy_of_buffer(outputBuffer->Ptr(), outputBuffer->Length());
       
  1276 
       
  1277 	delete outputBuffer;
       
  1278 
       
  1279 	return EAP_STATUS_RETURN(this, status);
       
  1280 }
       
  1281 
       
  1282 //--------------------------------------------------
       
  1283 
       
  1284 /// This function converts UTF8 string to unicode string.
       
  1285 EAP_FUNC_EXPORT eap_status_e eap_am_tools_symbian_c::convert_utf8_to_unicode(
       
  1286 	eap_variable_data_c & dest,
       
  1287 	const eap_variable_data_c & src)
       
  1288 {
       
  1289 	eap_status_e status = eap_status_allocation_error;
       
  1290 	
       
  1291 	HBufC16* outputBuffer = NULL;
       
  1292 	// src is UTF8 string, unicode max length is then the length of UTF8 string.
       
  1293 	// NOTE, HBufC16 length means count of 16-bit objects.
       
  1294 	TRAPD(err, outputBuffer = HBufC16::NewL(src.get_data_length()))
       
  1295 	if (err != KErrNone)
       
  1296 	{
       
  1297 		status = convert_am_error_to_eapol_error(err);
       
  1298 		return EAP_STATUS_RETURN(this, status);
       
  1299 	} 
       
  1300 
       
  1301 	const TPtrC8 utf8(
       
  1302 		(src.get_data(src.get_data_length())),
       
  1303 		src.get_data_length()); // Length in bytes
       
  1304 
       
  1305 	TPtr16 unicode = outputBuffer->Des();
       
  1306 
       
  1307 	CnvUtfConverter::ConvertToUnicodeFromUtf8(unicode, utf8);
       
  1308 
       
  1309 	status = dest.set_copy_of_buffer(outputBuffer->Ptr(), outputBuffer->Size());
       
  1310 
       
  1311 	delete outputBuffer;
       
  1312 
       
  1313 	return EAP_STATUS_RETURN(this, status);
       
  1314 }
       
  1315 
       
  1316 //--------------------------------------------------
       
  1317 
       
  1318 EAP_FUNC_EXPORT eap_status_e eap_am_tools_symbian_c::getenv(
       
  1319 	const eap_variable_data_c * const /* environment_variable_name */,
       
  1320 	eap_variable_data_c * const /* environment_variable_value */)
       
  1321 {
       
  1322 	return eap_status_not_supported;
       
  1323 }
       
  1324 
       
  1325 //--------------------------------------------------
       
  1326 
       
  1327 EAP_FUNC_EXPORT bool eap_am_tools_symbian_c::isspace(const u8_t character)
       
  1328 {
       
  1329 	if (character >= 0x09 && character <= 0x0D
       
  1330 		|| character == 0x20)
       
  1331 	{
       
  1332 		return true;
       
  1333 	}
       
  1334 	else
       
  1335 	{
       
  1336 		return false;
       
  1337 	}
       
  1338 }
       
  1339 
       
  1340 //--------------------------------------------------
       
  1341 
       
  1342 EAP_FUNC_EXPORT u64_struct eap_am_tools_symbian_c::u64_t_to_u64_struct(const u64_t value)
       
  1343 {
       
  1344 	return *reinterpret_cast<const u64_struct *>(&value);
       
  1345 }
       
  1346 
       
  1347 //--------------------------------------------------
       
  1348 
       
  1349 EAP_FUNC_EXPORT u64_t eap_am_tools_symbian_c::u64_struct_to_u64_t(const u64_struct value)
       
  1350 {
       
  1351 	return *reinterpret_cast<const u64_t *>(&value);
       
  1352 }
       
  1353 
       
  1354 //--------------------------------------------------
       
  1355 //--------------------------------------------------
       
  1356 //--------------------------------------------------
       
  1357 
       
  1358 void eap_am_tools_symbian_c::StartTimer(const TUint aInterval)
       
  1359 {	
       
  1360 	EAP_TRACE_DEBUG(
       
  1361 		this,
       
  1362 		TRACE_FLAGS_TIMER,
       
  1363 		(EAPL("eap_am_tools_symbian_c::StartTimer().\n")));
       
  1364 
       
  1365 	iInterval = aInterval;
       
  1366 	iStartTime = get_clock_ticks();
       
  1367 	
       
  1368 	iLastTime = iStartTime;
       
  1369 	
       
  1370 	// Start the timer
       
  1371 	After(limit_microsecond_timeout(iInterval));
       
  1372 }
       
  1373 
       
  1374 //--------------------------------------------------
       
  1375 
       
  1376 void eap_am_tools_symbian_c::StopTimer()
       
  1377 {
       
  1378 	EAP_TRACE_DEBUG(
       
  1379 		this,
       
  1380 		TRACE_FLAGS_TIMER,
       
  1381 		(EAPL("eap_am_tools_symbian_c::StopTimer().\n")));
       
  1382 
       
  1383 	Cancel();
       
  1384 }
       
  1385 
       
  1386 //--------------------------------------------------
       
  1387 
       
  1388 TBool eap_am_tools_symbian_c::TimerRunning()
       
  1389 {
       
  1390 	if (!IsActive())
       
  1391 	{
       
  1392 		EAP_TRACE_DEBUG(
       
  1393 			this,
       
  1394 			TRACE_FLAGS_TIMER,
       
  1395 			(EAPL("eap_am_tools_symbian_c::TimerRunning(): EFalse.\n")));
       
  1396 
       
  1397 		return EFalse;
       
  1398 	}
       
  1399 	else
       
  1400 	{
       
  1401 		EAP_TRACE_DEBUG(
       
  1402 			this,
       
  1403 			TRACE_FLAGS_TIMER,
       
  1404 			(EAPL("eap_am_tools_symbian_c::TimerRunning(): ETrue.\n")));
       
  1405 
       
  1406 		return ETrue;
       
  1407 	}
       
  1408 }
       
  1409 
       
  1410 //--------------------------------------------------
       
  1411 
       
  1412 u32_t eap_am_tools_symbian_c::limit_microsecond_timeout(u32_t next_timeout_millisecond)
       
  1413 {
       
  1414 	if (next_timeout_millisecond > EAP_TIMER_MAX_AFTER_TIME_MILLISECONDS_SYMBIAN)
       
  1415 	{
       
  1416 		next_timeout_millisecond = EAP_TIMER_MAX_AFTER_TIME_MILLISECONDS_SYMBIAN;
       
  1417 	}
       
  1418 
       
  1419 	u32_t next_timeout_microsecond = next_timeout_millisecond * 1000ul;
       
  1420 
       
  1421 	return (next_timeout_microsecond);
       
  1422 }
       
  1423 
       
  1424 //--------------------------------------------------
       
  1425 
       
  1426 void eap_am_tools_symbian_c::RunL()
       
  1427 {
       
  1428 	EAP_TRACE_DEBUG(
       
  1429 		this,
       
  1430 		TRACE_FLAGS_TIMER,
       
  1431 		(EAPL("eap_am_tools_symbian_c::RunL().\n")));
       
  1432 
       
  1433 	u32_t next_timeout_milliseconds = iInterval;
       
  1434 
       
  1435 	enter_global_mutex();
       
  1436 	if (get_is_timer_thread_active())
       
  1437 	{
       
  1438 		u64_t currentTime = get_clock_ticks();
       
  1439 		u32_t tics_of_millisecond = static_cast<u32_t>(get_hardware_ticks_of_second())/1000ul;
       
  1440 		u32_t spend_time = static_cast<u32_t>(currentTime - iLastTime)/tics_of_millisecond;
       
  1441 
       
  1442 		iLastTime = currentTime;
       
  1443 		
       
  1444 		EAP_TRACE_DEBUG(
       
  1445 			this,
       
  1446 			TRACE_FLAGS_TIMER,
       
  1447 			(EAPL("eap_am_tools_symbian_c::RunL(): pulse_timer(%d ms).\n"),
       
  1448 			spend_time));
       
  1449 
       
  1450 		next_timeout_milliseconds = m_timer_queue.pulse_timer(spend_time, true);
       
  1451 	}
       
  1452 	leave_global_mutex();
       
  1453 
       
  1454 	// Setup timer again (if somebody inside pulse_timer has not already done it...)
       
  1455 	if (!IsActive())
       
  1456 	{
       
  1457 		After(limit_microsecond_timeout(next_timeout_milliseconds));
       
  1458 		
       
  1459 		EAP_TRACE_DEBUG(
       
  1460 			this,
       
  1461 			TRACE_FLAGS_TIMER,
       
  1462 			(EAPL("eap_am_tools_symbian_c::RunL(): After(%d millisecond).\n"),
       
  1463 			next_timeout_milliseconds));
       
  1464 	}
       
  1465 	else
       
  1466 	{
       
  1467 		EAP_TRACE_DEBUG(
       
  1468 			this,
       
  1469 			TRACE_FLAGS_TIMER,
       
  1470 			(EAPL("eap_am_tools_symbian_c::RunL(): is active.\n")));
       
  1471 	}
       
  1472 }
       
  1473 
       
  1474 //--------------------------------------------------
       
  1475 
       
  1476 void eap_am_tools_symbian_c::DoCancel()
       
  1477 {
       
  1478 	EAP_TRACE_DEBUG(
       
  1479 		this,
       
  1480 		TRACE_FLAGS_TIMER,
       
  1481 		(EAPL("eap_am_tools_symbian_c::DoCancel().\n")));
       
  1482 
       
  1483 	  // Base class
       
  1484 	CTimer::DoCancel();
       
  1485 }
       
  1486 
       
  1487 //--------------------------------------------------
       
  1488 //--------------------------------------------------
       
  1489 //--------------------------------------------------
       
  1490 
       
  1491 EAP_FUNC_EXPORT_INTERFACE abs_eap_am_tools_c * abs_eap_am_tools_c::new_abs_eap_am_tools_c()
       
  1492 {
       
  1493 	abs_eap_am_tools_c *am_tools = new eap_am_tools_symbian_c(EAP_DEFAULT_TRACE_FILE);
       
  1494 
       
  1495 	if (am_tools != 0)
       
  1496 	{
       
  1497 		eap_status_e status = am_tools->configure();
       
  1498 
       
  1499 		if (status != eap_status_ok)
       
  1500 		{
       
  1501 			abs_eap_am_tools_c::delete_abs_eap_am_tools_c(am_tools);
       
  1502 			am_tools = 0;
       
  1503 		}
       
  1504 		else
       
  1505 		{
       
  1506 			EAP_TRACE_DEBUG(
       
  1507 				am_tools,
       
  1508 				TRACE_FLAGS_TIMER,
       
  1509 				(EAPL("abs_eap_am_tools_c::new_abs_eap_am_tools_c() => 0x%08x success\n"),
       
  1510 				am_tools));
       
  1511 		}
       
  1512 	}
       
  1513 
       
  1514 	return am_tools;
       
  1515 }
       
  1516 
       
  1517 //--------------------------------------------------
       
  1518 
       
  1519 EAP_FUNC_EXPORT_INTERFACE void abs_eap_am_tools_c::delete_abs_eap_am_tools_c(abs_eap_am_tools_c * const am_tools)
       
  1520 {
       
  1521 	EAP_TRACE_DEBUG(
       
  1522 		am_tools,
       
  1523 		TRACE_FLAGS_TIMER,
       
  1524 		(EAPL("abs_eap_am_tools_c::delete_abs_eap_am_tools_c(0x%08x)\n"),
       
  1525 		am_tools));
       
  1526 
       
  1527 	(void)am_tools->shutdown();
       
  1528 
       
  1529 	delete am_tools;
       
  1530 }
       
  1531 
       
  1532 //--------------------------------------------------
       
  1533 //--------------------------------------------------
       
  1534 //--------------------------------------------------
       
  1535 
       
  1536 
       
  1537 
       
  1538 
       
  1539 // End of file