//----------------------------- Includes ------------------------------
#include "ets_sys.h"
#include "os_type.h"
#include "mem.h"
#include "osapi.h"
#include "user_interface.h"
#include "espconn.h"
#include "user_esp_platform.h"
#include "user_iot_version.h"
#include "upgrade.h"
#include "user_ota_upgrade.h"
#define ESP_DEBUG
#ifdef ESP_DEBUG
#define ESP_DBG os_printf
#else
#define ESP_DBG
#endif
//----------------------------- Varuable ------------------------------
LOCAL os_timer_t ota_timer;
char g_html_buf[2048];
para_get_from_web_st web_para, web_para_defualt;
//----------------------------- Functions Declaration ------------------
LOCAL void user_ota_data_send(void *arg, bool responseOK, char *psend);
LOCAL void user_ota_upgrade_rsp(void *arg);
LOCAL void user_ota_upgrade_begin(struct espconn *pespconn, struct upgrade_server_info *server);
//----------------------------- Functions Definition -------------------
void user_error_printf(char *buf)
{
os_printf("\n----------------------- ERROR ----------------------\n");
os_printf("\nERROR-> %s\n", buf);
os_printf("\n----------------------------------------------------\n");
}
void user_ota_connect_to_router(char *ssid, char *passwd)
{
// char ssid[32] = AP_SSID; // 设置WIFI的名称
// char passwd[64] = AP_PASSWD; // 设置WIFI的密码
struct station_config stationConf;
wifi_set_opmode(STATIONAP_MODE); // 设置station ap 模式
// 检测 ssid and passwd 的合法性
if(os_strlen(ssid)>32)
{
os_printf("ssid exceeds the max length(32bytes)\n");
}
if(os_strlen(passwd)>64)
{
os_printf("passwd exceeds the max length(64bytes)\n");
}
stationConf.bssid_set = 1; // need check MAC address of AP
os_memcpy(&stationConf.ssid, ssid, os_strlen(ssid)); // ssid
os_memcpy(&stationConf.password, passwd, os_strlen(passwd)); // passwd
os_printf("AP_SSID:%s\n", stationConf.ssid);
os_printf("AP_PASSWD:%s\n", stationConf.password);
wifi_station_set_config(&stationConf); // 设置wifi 信息
// 连接该wifi
wifi_station_connect();
// 自动连接
wifi_station_set_auto_connect(1); // 设置 自动连接已经记录的AP
}
void user_ota_init()
{
os_printf("user_ota_init...\n");
if (wifi_get_opmode() != SOFTAP_MODE)
{
os_timer_disarm(&ota_timer);
os_timer_setfn(&ota_timer, (os_timer_func_t *)user_ota_updata_check, NULL);
os_timer_arm(&ota_timer, 100, 0);
}
else
{
os_printf("wifi opmode : SOFTAP_MODE\n");
}
}
void user_ota_updata_check()
{
struct ip_info ipconfig;
os_printf("user_ota_updata_check...\n");
os_timer_disarm(&ota_timer);
wifi_get_ip_info(STATION_IF, &ipconfig);
if (ipconfig.ip.addr != 0)
{
struct espconn *pespconn = NULL;
struct upgrade_server_info *server = NULL;
server = (struct upgrade_server_info *)os_zalloc(sizeof(struct upgrade_server_info));
user_ota_upgrade_begin(pespconn , server);
}
else
{
os_timer_setfn(&ota_timer, (os_timer_func_t *)user_ota_updata_check, NULL);
os_timer_arm(&ota_timer, 100, 0);
}
}
// 解析web来的数据
// 源数据: server_ip=192.168.1.11&server_port=80&B1=Update
bool user_get_web_para(char *pbuf)
{
char *pstr1;
char *pstr2;
int len=0;
char ip1[4];
char i;
bool flag_legal_para=true;
os_printf("user_get_web_para .....\n");
os_memset(&web_para, 0, sizeof(web_para)); // 清零 参数值
if((pstr1 = (char *)os_strstr(pbuf, "server_ip=")) != NULL) // 找到 server_ip=,
{
pstr1 = pstr1+sizeof("server_ip=")-1; // 将指针移到字符串server_ip=" 等号的后面, 并将改指针赋值给 pstr1
os_printf("sizeof(\"server_ip=\")=%d.\n", sizeof("server_ip="));
if((pstr2 = (char *)os_strstr(pstr1, "&")) != NULL) // 找到 server_ip 后面的第一个 &,
{
len = pstr2 - pstr1; // 计算 数据长度
os_printf("len = %d\n", len); // 打印长度
if(len==0)
{
flag_legal_para=false;
user_error_printf("strlen(server_ip) = 0.");
return flag_legal_para;
}
if(len >= sizeof(web_para.server_ip_original)) // 如果长度大于设定值 长度取设定最大值
{
len = sizeof(web_para.server_ip_original)-1; // 减一是为了防止数组越界
}
os_memcpy(web_para.server_ip_original, pstr1, len); // 将值复制到 web_para.server_ip
web_para.server_ip_original[len] = 0; // 添加结束符
os_printf("web_para.server_ip_original = %s\n",web_para.server_ip_original); // 打印接收到的数据
for(i=0;i<4;i++)
{
if(i==0) {pstr1 = web_para.server_ip_original;}
else {pstr1 = pstr2+1;}
if(i==3)
{
len = os_strlen(pstr1);
os_memcpy(ip1, pstr1, len);
ip1[len] = 0;
web_para.server_ip[i] = atoi(ip1);
// os_printf("web_para.server_ip[%d]=%d\n", i, web_para.server_ip[i]);
}
else // 有一个为空,则跳出
{
if((pstr2 = (char *)os_strstr(pstr1, ".")) != NULL) // 找到 第一个 .,
{
len = pstr2 - pstr1;
os_memcpy(ip1, pstr1, len);
ip1[len] = 0;
web_para.server_ip[i] = atoi(ip1);
// os_printf("web_para.server_ip[%d]=%d\n", i, web_para.server_ip[i]);
}
else
{
flag_legal_para=false;
user_error_printf("illegal server_ip, please input legal server_ip (eg: 192.168.1.65).");
return flag_legal_para;
}
}
}
if(flag_legal_para)
{
os_printf("web_para.server_ip:"IPSTR"\n", IP2STR(web_para.server_ip));
}
}
}
if((pstr1 = (char *)os_strstr(pbuf, "server_port=")) != NULL) // 找到 server_port=,
{
pstr1 = pstr1+sizeof("server_port=")-1; // 将指针移到字符串server_port=" 等号的后面, 并将改指针赋值给 pstr1
if((pstr2 = (char *)os_strstr(pstr1, "&")) != NULL) // 找到 server_port 后面的第一个 &,
{
len = pstr2 - pstr1; // 计算 数据长度
os_printf("len = %d\n", len); // 打印长度
if(len==0)
{
flag_legal_para=false;
user_error_printf("strlen(server_port) = 0.");
return flag_legal_para;
}
if(len >= sizeof(web_para.server_port_original)) // 如果长度大于设定值 长度取设定最大值
{
len = sizeof(web_para.server_port_original)-1; // 减一是为了防止数组越界
}
os_memcpy(web_para.server_port_original, pstr1, len); // 将值复制到 web_para.server_port
web_para.server_port_original[len] = 0; // 添加结束符
os_memcpy(web_para_defualt.server_port_original, web_para.server_port_original, sizeof(web_para.server_port_original)); // 赋值给 web_para_defualt
os_printf("web_para.server_port_original = %s\n", web_para.server_port_original); // 打印接收到的数据
web_para.server_port = atoi(web_para.server_port_original);
os_printf("web_para.server_port = %d\n", web_para.server_port); // 打印接收到的数据
}
// cmd
if((pstr1 = (char *)os_strstr(pbuf, "cmd=")) != NULL) // 找到 cmd=,
{
pstr1 = pstr1+sizeof("cmd=")-1; // 将指针移到字符串cmd=" 等号的后面, 并将改指针赋值给 pstr1
len = os_strlen(pstr1); // 计算 数据长度
os_printf("len = %d\n", len); // 打印长度
if(len >= sizeof(web_para.cmd)) // 如果长度大于设定值 长度取设定最大值
{
len = sizeof(web_para.cmd)-1; // 减一是为了防止数组越界
}
os_memcpy(web_para.cmd, pstr1, len); // 将值复制到 web_para.cmd
web_para.cmd[len] = 0; // 添加结束符
os_printf("web_para.cmd = %s\n", web_para.cmd); // 打印接收到的数据
}
}
os_printf("user_get_web_para end .....\n");
return flag_legal_para;
}
void user_process_web_para(para_get_from_web_st web_para_buf)
{
struct softap_config ap_config;
struct station_config sta_config;
uint32_t baud_rate;
bool flag_esp_ap=0, flag_esp_sta=0, flag_baudrate=0;
char i=0;
os_printf("-> user_process_web_para...\n")