/******************************************************************************
 * FunctionName : mesh_SuccessCb
 * Description  : callback func when mesh init finished successfully
                  In this demo , we run the platform code after mesh initialization
*******************************************************************************/
void ICACHE_FLASH_ATTR
mesh_SuccessCb(void* arg)
{
    _LINE_DESP();
    MESH_INFO("mesh log: mesh success!\r\n");
    MESH_INFO("CONNECTED, DO RUN ESP PLATFORM...\r\n");
    MESH_INFO("mesh status: %d\r\n",espconn_mesh_get_status());
    _LINE_DESP();
    
    //show light status at the first time mesh enables
    #if ESP_DEBUG_MODE
    	struct ip_info sta_ip;
		wifi_get_ip_info(STATION_IF,&sta_ip);
		if( espconn_mesh_local_addr(&sta_ip.ip)){
			MESH_INFO("THIS IS A MESH SUB NODE..\r\n");
			uint32 mlevel = sta_ip.ip.addr&0xff;
			light_ShowDevLevel(mlevel);//debug
		}else{
			MESH_INFO("THIS IS A MESH ROOT..\r\n");
			light_ShowDevLevel(1);//debug
		}
	#else
    if(mesh_init_flag){
        struct ip_info sta_ip;
        wifi_get_ip_info(STATION_IF,&sta_ip);
        if( espconn_mesh_local_addr(&sta_ip.ip)){
            MESH_INFO("THIS IS A MESH SUB NODE..\r\n");
            uint32 mlevel = sta_ip.ip.addr&0xff;
            light_ShowDevLevel(mlevel);//debug
        }else{
            MESH_INFO("THIS IS A MESH ROOT..\r\n");
            light_ShowDevLevel(1);//debug
        }
        mesh_init_flag = false;
    }else{
    
    }
	#endif
    
    #if ESP_NOW_SUPPORT
        //init ESP-NOW ,so that light can be controlled by ESP-NOW SWITCHER.
        light_EspnowInit();
    #endif
    WIFI_StartCheckIp();//debug
    //run esp-platform procedure,register to server.
    user_esp_platform_connect_ap_cb();//debug
    return;
}
//delay , then call OTA reboot,3s*level
void ICACHE_FLASH_ATTR
	debug_OtaRebootStart(uint32 t_ms)
{
	os_timer_disarm(&reboot_t);
	os_timer_setfn(&reboot_t,debug_OtaReboot,NULL);

	
	struct ip_info sta_ip;
	wifi_get_ip_info(STATION_IF,&sta_ip);
	uint16 mesh_level = 0;
    #if ESP_MESH_SUPPORT						
		if( espconn_mesh_local_addr(&sta_ip.ip)){
			uint32 mlevel = sta_ip.ip.addr&0xff;
			mesh_level = mlevel;
			ESP_DBG("THIS IS A MESH SUB NODE..level: %d\r\n",mesh_level);
		}else if(sta_ip.ip.addr!= 0){
			ESP_DBG("THIS IS A MESH ROOT..\r\n");
			mesh_level = 1;
		}else{
            //why?
            mesh_level = 1;
		}
    #endif

	os_timer_arm(&reboot_t,t_ms+3000*(mesh_level-1),0);
	ESP_DBG("upgrade reboot in %d ms\r\n",t_ms+3000*mesh_level);
}