C_RESULT ardrone_tool_init( const char* ardrone_ip, size_t n, AT_CODEC_FUNCTIONS_PTRS *ptrs, const char *appname, const char *usrname)
{	
	// Initalize mutex and condition
	vp_os_mutex_init(&ardrone_tool_mutex);
	ardrone_tool_in_pause = FALSE;

	// Initialize ardrone_control_config structures;
	ardrone_tool_reset_configuration();
	// ardrone_control_config_default initialisation. Sould not be modified after that !
	vp_os_memcpy ((void *)&ardrone_control_config_default, (const void *)&ardrone_control_config, sizeof (ardrone_control_config_default));
	// initialization of application defined default values
	vp_os_memcpy ((void *)&ardrone_application_default_config, (const void *)&ardrone_control_config, sizeof (ardrone_application_default_config));
	
	//Fill structure AT codec and built the library AT commands.
   if( ptrs != NULL )
	   ardrone_at_init_with_funcs( ardrone_ip, n, ptrs );
   else	
      ardrone_at_init( ardrone_ip, n );

	// Save appname/appid for reconnections
	if (NULL != appname)
	{
	  ardrone_gen_appid (appname, __SDK_VERSION__, app_id, app_name, sizeof (app_name));
	}
	// Save usrname/usrid for reconnections
	if (NULL != usrname)
	{
		ardrone_gen_usrid (usrname, usr_id, usr_name, sizeof (usr_name));
	}
	// Create pseudorandom session id
	ardrone_gen_sessionid (ses_id, ses_name, sizeof (ses_name));

	// Init subsystems
	ardrone_timer_reset(&ardrone_tool_timer);
	ardrone_timer_update(&ardrone_tool_timer);
	
	ardrone_tool_input_init();
	ardrone_control_init();
	ardrone_tool_configuration_init();
	ardrone_navdata_client_init();


   //Opens a connection to AT port.
	ardrone_at_open();

	START_THREAD(navdata_update, 0);
	START_THREAD(ardrone_control, 0);

	// Send start up configuration
	ardrone_at_set_pmode( MiscVar[0] );
	ardrone_at_set_ui_misc( MiscVar[0], MiscVar[1], MiscVar[2], MiscVar[3] );

	return C_OK;
}
void ardrone_switch_to_user(const char *new_user)
{
    if (1 == droneSupportsMulticonfig)
    {
        ardrone_gen_usrid (new_user, usr_id, usr_name, USER_NAME_SIZE);
        appSwitch = 0;
        usrSwitch = 1;
        sesSwitch = 0;
        navdataNeeded = 0;
        configState = MULTICONFIG_GOT_DRONE_VERSION;
    }
}
C_RESULT ardrone_tool_init( const char* ardrone_ip, size_t n, AT_CODEC_FUNCTIONS_PTRS *ptrs, const char *appname, const char *usrname, const char *rootdir, const char *flightdir, int flight_storing_size, academy_download_new_media academy_download_new_media_func)
{
	C_RESULT res;

	// Initalize mutex and condition
	vp_os_mutex_init(&ardrone_tool_mutex);
	ardrone_tool_in_pause = FALSE;

	// Initialize ardrone_control_config structures;
	ardrone_tool_reset_configuration();
	// ardrone_control_config_default initialisation. Sould not be modified after that !
	vp_os_memcpy ((void *)&ardrone_control_config_default, (const void *)&ardrone_control_config, sizeof (ardrone_control_config_default));
	// initialization of application defined default values
	vp_os_memcpy ((void *)&ardrone_application_default_config, (const void *)&ardrone_control_config, sizeof (ardrone_application_default_config));
	
	// Save appname/appid for reconnections
	if (NULL != appname)
	{
        ardrone_gen_appid (appname, __SDK_VERSION__, app_id, app_name, sizeof (app_name));
	}
	// Save usrname/usrid for reconnections
	if (NULL != usrname)
	{
		ardrone_gen_usrid (usrname, usr_id, usr_name, sizeof (usr_name));
	}
	// Create pseudorandom session id
	ardrone_gen_sessionid (ses_id, ses_name, sizeof (ses_name));
    
	// copy root directory
	if((rootdir != NULL) && strlen(rootdir) < ROOT_NAME_SIZE)
		strcpy(root_dir, rootdir);
    
	//Fill structure AT codec and built the library AT commands.
   if( ptrs != NULL )
	   ardrone_at_init_with_funcs( ardrone_ip, n, ptrs );
   else	
      ardrone_at_init( ardrone_ip, n );

	// Init subsystems
	ardrone_timer_reset(&ardrone_tool_timer);
	ardrone_timer_update(&ardrone_tool_timer);
	
	ardrone_tool_input_init();
	ardrone_control_init();
	ardrone_tool_configuration_init();
	ardrone_navdata_client_init();
	PRINT("Before usbdata_client_init\n");
	ardrone_usbdata_client_init(); //add Alex

    // Init custom tool
    res = ardrone_tool_init_custom();

   //Opens a connection to AT port.
	ardrone_at_open();

	START_THREAD(navdata_update, 0);
	START_THREAD(usbdata_update, 0);
	START_THREAD(ardrone_control, 0);

	// Send start up configuration
	ardrone_at_set_pmode( MiscVar[0] );
	ardrone_at_set_ui_misc( MiscVar[0], MiscVar[1], MiscVar[2], MiscVar[3] );

	academy_download_init(academy_download_new_media_func);
	academy_init(flightdir, flight_storing_size);

	return res;
}