乐鑫SDK iot_demo提取dns解析tcp_client连接

#include “espconn.h”
#include “osapi.h”
#include “user_interface.h”
#include “c_types.h”
LOCAL struct espconn user_conn;
LOCAL struct _esp_tcp user_tcp;
LOCAL os_timer_t client_timer;
LOCAL void ICACHE_FLASH_ATTR
user_esp_platform_connect_cb(void *arg)
{
struct espconn *pespconn = arg;

ESP_DBG("user_esp_platform_connect_cb\n");
if (wifi_get_opmode() ==  STATIONAP_MODE ) {
    wifi_set_opmode(STATION_MODE);
}
espconn_regist_recvcb(pespconn, user_esp_platform_recv_cb);
espconn_regist_sentcb(pespconn, user_esp_platform_sent_cb);
user_esp_platform_sent(pespconn);

}
LOCAL void ICACHE_FLASH_ATTR
void ICACHE_FLASH_ATTR
user_esp_platform_check_ip(void)
{
struct ip_info ipconfig;

os_timer_disarm(&client_timer);//关闭定时器

wifi_get_ip_info(STATION_IF, &ipconfig);//查询station接口所获取的ip地址

//已连接上路由器且station接口获取到了ip地址
if (wifi_station_get_connect_status() == STATION_GOT_IP && ipconfig.ip.addr != 0) {
user_conn.proto.tcp = &user_tcp;
user_conn.type = ESPCONN_TCP;
user_conn.state = ESPCONN_NONE;
user_esp_platform_start_dns(&user_conn);//dns开始解析
}
else {//重开定时器
os_timer_setfn(&client_timer, (os_timer_func_t *)user_esp_platform_check_ip, NULL);
os_timer_arm(&client_timer, 100, 0);
}
}
user_esp_platform_reconnect(struct espconn *pespconn)//重连。
{
os_printf(“user_esp_platform_reconnect\n”);

user_esp_platform_check_ip();

}
LOCAL void ICACHE_FLASH_ATTR
user_esp_platform_discon_cb(void *arg)
{
struct espconn *pespconn = arg;
struct ip_info ipconfig;
struct dhcp_client_info dhcp_info;
os_printf(“user_esp_platform_discon_cb\n”);

if (pespconn == NULL) {
    return;
}
pespconn->proto.tcp->local_port = espconn_port();

user_esp_platform_reconnect(pespconn);

}
LOCAL void ICACHE_FLASH_ATTR
user_esp_platform_recon_cb(void *arg, sint8 err)
{
struct espconn *pespconn = (struct espconn *)arg;

os_printf("user_esp_platform_recon_cb\n");
os_timer_disarm(&client_timer);
os_timer_setfn(&client_timer, (os_timer_func_t *)user_esp_platform_reconnect, pespconn);
os_timer_arm(&client_timer, 1000, 0);

}
LOCAL void ICACHE_FLASH_ATTR
user_esp_platform_connect(struct espconn *pespconn)
{
os_printf(“user_esp_platform_connect\n”);
espconn_connect(pespconn);
}
LOCAL void ICACHE_FLASH_ATTR
user_esp_platform_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)
{
struct espconn *pespconn = (struct espconn *)arg;
//解析成功才关闭client_timer定时器
if (ipaddr == NULL) {//如果解析失败,退出
os_printf(“user_esp_platform_dns_found NULL\n”);
return;
}

if (esp_server_ip.addr == 0 && ipaddr->addr != 0) {//解析成功
    os_timer_disarm(&client_timer);//关闭dns_check定时器
    //初始化espconn参数
    esp_server_ip.addr = ipaddr->addr;
    os_memcpy(pespconn->proto.tcp->remote_ip, &ipaddr->addr,4);//拷贝远程ip地址

    pespconn->proto.tcp->local_port = espconn_port();//获取可用端口

    pespconn->proto.tcp->remote_port = 8000;

    espconn_regist_connectcb(pespconn, user_esp_platform_connect_cb);//注册连接成功的回调函数
    espconn_regist_disconcb(pespconn, user_esp_platform_discon_cb);////注册连接成功的回调函数
    //注册 TCP连接发生异常断开时的回调函数,可以在回调函数中进行重连。
    espconn_regist_reconcb(pespconn, user_esp_platform_recon_cb);
    user_esp_platform_connect(pespconn);
}

}
LOCAL void ICACHE_FLASH_ATTR
user_esp_platform_dns_check_cb(void *arg)
{
struct espconn *pespconn = arg;

os_printf("user_esp_platform_dns_check_cb\n");

espconn_gethostbyname(pespconn, ESP_DOMAIN, &esp_server_ip, user_esp_platform_dns_found);

os_timer_arm(&client_timer, 1000, 0);

}
LOCAL void ICACHE_FLASH_ATTR
user_esp_platform_start_dns(struct espconn *pespconn)
{
esp_server_ip.addr = 0;//全局变量
espconn_gethostbyname(pespconn,“iot.espressif.cn”, &esp_server_ip, user_esp_platform_dns_found);

os_timer_disarm(&client_timer);//开启client_timer定时器
os_timer_setfn(&client_timer, (os_timer_func_t *)user_esp_platform_dns_check_cb, pespconn);
os_timer_arm(&client_timer, 1000, 0);

}

void ICACHE_FLASH_ATTR
user_esp_platform_init(void){
if (wifi_get_opmode() != SOFTAP_MODE) {
os_timer_disarm(&client_timer);
os_timer_setfn(&client_timer, (os_timer_func_t *)user_esp_platform_check_ip, NULL);
os_timer_arm(&client_timer, 100, 0);
}
}

你可能感兴趣的:(esp8266,安信可,>Esp8266)