/******************************************************************************
 * FunctionName : mesh_InitStatusCheck
 * Description  : Only used in mesh init, to check the current status of mesh initialization,
                  and handle different situation accordingly
*******************************************************************************/
void ICACHE_FLASH_ATTR
mesh_InitStatusCheck()
{
    os_timer_disarm(&mesh_check_t);
    sint8 mesh_status = espconn_mesh_get_status();
    MESH_INFO("--------------\r\n");
    MESH_INFO("mesh status: %d ; %d\r\n",mesh_status,system_get_free_heap_size());
    MESH_INFO("--------------\r\n");
    
    if(mesh_status == MESH_DISABLE){
        MESH_INFO("MESH DISABLE , RUN FAIL CB ,retry:%d \r\n",LightMeshProc.init_retry);
        if(LightMeshProc.init_retry<MESH_INIT_RETRY_LIMIT 
			#if MESH_INIT_TIMEOUT_SET
			&& (mesh_GetStartMs()<MESH_INIT_TIME_LIMIT)
			#endif
			){
            LightMeshProc.init_retry+=1;
            espconn_mesh_enable(mesh_EnableCb, MESH_ONLINE);
            MESH_INFO("MESH RETRY : %d \r\n",LightMeshProc.init_retry);
        }else{
            mesh_StopCheckTimer();
            MESH_INFO("MESH INIT RETRY FAIL...\r\n");
            if(LightMeshProc.mesh_fail_cb){
                LightMeshProc.mesh_fail_cb(NULL);
            }
            LightMeshProc.init_retry = 0;
            return;
        }
    }
    else if(mesh_status==MESH_NET_CONN){
        MESH_INFO("MESH WIFI CONNECTED\r\n");
        mesh_StopCheckTimer();
    }
    os_timer_arm(&mesh_check_t,MESH_STATUS_CHECK_MS,0);
}
void ICACHE_FLASH_ATTR
    WIFI_ConnectCb(uint8_t status)
{
#if 0
    if(0){//(wifiReconFlg && (ap_cache_if==false)){
        INFO("------------------\r\n");
        INFO("001\r\n");
        wifiReconFlg = false;
        INFO("WIFI RECONN FLG: %d \r\n",wifiReconFlg);
        INFO("DO AP CACHE\r\n");
        os_timer_disarm(&ap_cache_t);
        os_timer_setfn(&ap_cache_t,APCache_TimeoutCb,NULL);
        os_timer_arm(&ap_cache_t,AP_CACHE_TOUT_MS,0);
        APCache_Connect();  
        return;
    }
#endif
    
    if(status == STATION_GOT_IP){
        #if ESP_TOUCH_SUPPORT
        esptouch_setAckFlag(true);
        #endif
        //os_timer_disarm(&ap_cache_t);
        INFO("WIFI CONNECTED , RUN ESP PLATFORM...\r\n");
        #if ESP_MESH_SUPPORT
            if(MESH_DISABLE == espconn_mesh_get_status() ){
                _LINE_DESP();
                INFO("CONNECTED TO ROUTER, ENABLE MESH\r\n");
                _LINE_DESP();
                espconn_mesh_enable(mesh_en_cb,MESH_ONLINE);//debug
            }
        #else
            user_esp_platform_connect_ap_cb();
        #endif
    }else{
        if(status != STATION_CONNECTING){
            INFO("---------------------\r\n");
            INFO("STATION STATUS: %d \r\n",status);
			INFO("MESH ENABLE.\r\n");
            INFO("---------------------\r\n");
            //APCache_Connect();
            wifiReconFlg = false;
			wifi_SetReconnProcessAck();
			wifi_RestartMeshScan(2000);
        }

    }
}
void ICACHE_FLASH_ATTR
user_MeshStart()
{
    os_printf("test: %s\n", __func__);
    //user_MeshSetInfo();
    espconn_mesh_enable(mesh_EnableCb, MESH_ONLINE);
    wifi_ClrReconnProcessAck();
	#if MESH_INIT_TIMEOUT_SET
    if(LightMeshProc.mesh_init_tout_cb){
        os_timer_disarm(&mesh_tout_t);
        os_timer_setfn(&mesh_tout_t,LightMeshProc.mesh_init_tout_cb,NULL);
        os_timer_arm(&mesh_tout_t,MESH_TIME_OUT_MS,0);
    }
	#endif
    os_timer_disarm(&mesh_check_t);
    os_timer_setfn(&mesh_check_t,mesh_InitStatusCheck,NULL);
    os_timer_arm(&mesh_check_t,MESH_STATUS_CHECK_MS,0);
}
Example #4
0
/******************************************************************************
 * FunctionName : user_init
 * Description  : entry of user application, init user function here
 * Parameters   : none
 * Returns      : none
*******************************************************************************/
void user_init(void)
{
    /*
     * set uart baut ratio
     */
    uart_div_modify(0, UART_CLK_FREQ / UART_BAUT_RATIO);
    uart_div_modify(1, UART_CLK_FREQ / UART_BAUT_RATIO);

    if (!esp_mesh_demo_init())
        return;
    
    user_devicefind_init();

    /*
     * enable mesh
     * after enable mesh, you should wait for the mesh_enable_cb to be triggered.
     */
    espconn_mesh_enable(mesh_enable_cb, MESH_ONLINE);
}
/******************************************************************************
 * FunctionName : mesh_ReconCheck
 * Description  : in case that some router would record the DNS info.
                  If we got the IP addr from DNS , and still can't connect to esp-server
                  Call this check func to try enable and connect again every 20s
*******************************************************************************/
void ICACHE_FLASH_ATTR
mesh_ReconCheck()
{
    MESH_INFO("---------\r\n");
    MESH_INFO("mesh_ReconCheck\r\n");
    MESH_INFO("---------\r\n");
    os_timer_disarm(&mesh_user_t);
    sint8 mesh_status = espconn_mesh_get_status();
    MESH_INFO("MESH STATUS CHECK: %d \r\n",mesh_status);
    
    if(mesh_status == MESH_ONLINE_AVAIL){
        MESH_INFO("MESH ONLINE AVAIL\r\n");
        //user_esp_platform_sent_data();
        user_esp_platform_connect_ap_cb();
    }else{
        espconn_mesh_enable(mesh_EnableCb, MESH_ONLINE);
        os_timer_setfn(&mesh_user_t,mesh_ReconCheck,NULL);
        os_timer_arm(&mesh_user_t,20000,1);    
    }
}
Example #6
0
void ICACHE_FLASH_ATTR mesh_enable_cb(int8_t res)
{
	MESH_DEMO_PRINT("mesh_enable_cb\n");

    if (res == MESH_OP_FAILURE) {
        MESH_DEMO_PRINT("enable mesh fail, re-enable\n");
        espconn_mesh_enable(mesh_enable_cb, MESH_ONLINE);
        return;
    }

    if (espconn_mesh_get_usr_context() &&
        espconn_mesh_is_root() &&
        res == MESH_LOCAL_SUC)
        goto TEST_SCENARIO;

    /*
     * try to estable user virtual connect
     * user can to use the virtual connect to sent packet to any node, server or mobile.
     * if you want to sent packet to one node in mesh, please build p2p packet
     * if you want to sent packet to server/mobile, please build normal packet (uincast packet)
     * if you want to sent bcast/mcast packet, please build bcast/mcast packet
     */
    MESH_DEMO_MEMSET(&g_ser_conn, 0 ,sizeof(g_ser_conn));
    MESH_DEMO_MEMSET(&ser_tcp, 0, sizeof(ser_tcp));

    MESH_DEMO_MEMCPY(ser_tcp.remote_ip, server_ip, sizeof(server_ip));
    ser_tcp.remote_port = server_port;
    ser_tcp.local_port = espconn_port();
    g_ser_conn.proto.tcp = &ser_tcp;

    if (espconn_regist_connectcb(&g_ser_conn, esp_mesh_demo_con_cb)) {
        MESH_DEMO_PRINT("regist con_cb err\n");
        espconn_mesh_disable(NULL);
        return;
    }

    if (espconn_regist_recvcb(&g_ser_conn, esp_recv_entrance)) {
        MESH_DEMO_PRINT("regist recv_cb err\n");
        espconn_mesh_disable(NULL);
        return;
    }

    /*
     * regist the other callback
     * sent_cb, reconnect_cb, disconnect_cb
     * if you donn't need the above cb, you donn't need to register them.
     */

    if (espconn_mesh_connect(&g_ser_conn)) {
        MESH_DEMO_PRINT("connect err\n");
        if (espconn_mesh_is_root())
            espconn_mesh_enable(mesh_enable_cb, MESH_LOCAL);
        else
            espconn_mesh_enable(mesh_enable_cb, MESH_ONLINE);
        return;
    }

TEST_SCENARIO:
    mesh_device_list_init();
    mesh_topo_test_init();
    mesh_json_mcast_test_init();
    mesh_json_bcast_test_init();
    mesh_json_p2p_test_init();
}
void ICACHE_FLASH_ATTR mesh_enable_task()
{
    espconn_mesh_enable(NULL, MESH_ONLINE);
}