EC600U_esp32_iap_uart/EC600U_t2n/udp_ota_shell.c

214 lines
5.1 KiB
C
Raw Permalink Normal View History

2024-02-05 17:39:56 +08:00
#include "ModbusS.h"
#include "lwip/ip_addr.h"
#include "lwip/udp.h"
#include "fupdate_config.h"
#include "osi_api.h"
#include "ql_log.h"
#define LOGD(msg, ...) QL_LOG(QL_LOG_LEVEL_DEBUG, "udp_ota_shell", msg, ##__VA_ARGS__)
#define LOGI(msg, ...) QL_LOG(QL_LOG_LEVEL_INFO, "udp_ota_shell", msg, ##__VA_ARGS__)
#define LOGW(msg, ...) QL_LOG(QL_LOG_LEVEL_WARN, "udp_ota_shell", msg, ##__VA_ARGS__)
#define LOGE(msg, ...) QL_LOG(QL_LOG_LEVEL_ERROR, "udp_ota_shell", msg, ##__VA_ARGS__)
extern osiThread_t *uart1_thread;
static struct udp_pcb *udp_ota_shell_pcb = NULL;
static struct ip_addr remote_addr;
static u16_t remote_port;
extern void loader_port_reset_target(void);
enum log_level
{
LDEBUG = 0,
LINFO = 1,
LWARN = 2,
LERROR = 3,
LRESP = 99
};
int CFG_LOG_PRINT_LEVEL = LDEBUG;
int esp32_ota_log(enum log_level level, char *format, ...)
{
if (level < CFG_LOG_PRINT_LEVEL)
{ // >config level, then printf
return 0; // no printf
}
if (udp_ota_shell_pcb == NULL || remote_port == 0)
{
return 0;
}
struct pbuf *pkt_txbuf = pbuf_alloc(PBUF_TRANSPORT, 1400, PBUF_RAM);
// ## init
char *buf = (char *)pkt_txbuf->payload;
int offset = 0;
// ## level
const char *evel_cr = "";
if (level == LDEBUG)
{
evel_cr = "[debug]";
}
else if (level == LINFO)
{
evel_cr = "[info]";
}
else if (level == LWARN)
{
evel_cr = "[warn]";
}
else if (level == LERROR)
{
evel_cr = "[error]";
}
// ## conv
offset = sprintf(buf, "%s", evel_cr);
// ## output
// int i = 0;
va_list vArgList;
va_start(vArgList, format);
char *sb = buf;
offset += vsnprintf(sb + offset, 1400 - offset, format, vArgList);
va_end(vArgList);
pkt_txbuf->len = offset;
pkt_txbuf->tot_len = offset;
err_t err = udp_sendto(udp_ota_shell_pcb, pkt_txbuf, &remote_addr, remote_port);
pbuf_free(pkt_txbuf);
return err;
}
static void recv_callback_ota_shell_udp(void *arg, struct udp_pcb *upcb, struct pbuf *pkt_buf, ip_addr_t *addr, u16_t port)
{
LOGI("recv_callback_ota_shell_udp start");
// err_t err = ERR_OK;
char *cmd;
// char *resp;
int rxlen;
cmd = (char *)pkt_buf->payload; // 获取数据包的负载部分
rxlen = pkt_buf->len; // 获取数据长度
*(cmd + rxlen) = '\0';
// 通过文件刷写esp32s3程序,发送id = 0x3203
if (strncmp(cmd, "esp32_update_file", strlen("esp32_update_file")) == 0)
{
if (uart1_thread)
{
osiEvent_t event;
event.id = 0x3203;
osiEventSend(uart1_thread, &event);
LOGE("esp32_update_file");
}
else
{
LOGE("esp32_update_file uart1_thread is NULL");
}
}
//event.id = 0x3299;获取url后作为事件参数发送给事件处理
else if (strncmp(cmd, "esp32_update_http", strlen("esp32_update_http")) == 0)
{
LOGI("shell: exec esp32_app_update cmd parm=%s", cmd + strlen("esp32_app_update") + 1);
if (uart1_thread)
{
osiEvent_t event;
cmd += strlen("esp32_update_http") + 1;
char *url = strstr(cmd, "http");
if (url != NULL)
{
char *str = malloc(strlen(url) + 1);
if (str != NULL)
{
strcpy(str, url);
}
event.param1 = (uint32_t)str;//这个指针指向的手动开辟的内存空间在uart1_modbus_master中释放
event.id = 0x3299;
osiEventSend(uart1_thread, &event);
LOGI("esp32_update_http url=%s", url);
}
}
LOGI("esp32_update_http");
}
//event.id = 0x5700;
else if (strncmp(cmd, "m5700_update", strlen("m5700_update")) == 0)
{
LOGI("shell: exec m5700_update cmd parm=%s", cmd + strlen("m5700_update") + 1);
if (uart1_thread)
{
osiEvent_t event;
cmd += strlen("m5700_update") + 1;
char *url = strstr(cmd, "http");
if (url != NULL)
{
// char *addr = strstr(url, " 0x");
// if (addr != NULL)
// {
char *str = malloc(strlen(url) + 1);
if (str != NULL)
{
strcpy(str, url);
}
event.param1 = (uint32_t)str;
event.id = 0x5700;
osiEventSend(uart1_thread, &event);
// }
esp32_ota_log(-1, "m5700_update url=%s", url);
}
}
}
#if 0
else if (strncmp(cmd, "rm esp32_ota_file", strlen("rm esp32_ota_file")) == 0)
{
int n = LSAPI_FS_Unlink("/esp32s3-app.bin");
LOGI("rm esp32_ota_file %d", n);
}
else if (strncmp(cmd, "rm 5700_ota_file", strlen("rm 5700_ota_file")) == 0)
{
int n = LSAPI_FS_Unlink(FUPDATE_PACK_FILE_NAME);
LOGI("rm 5700_ota_file %d", n);
}
else if (strncmp(cmd, "gps_reset", strlen("gps_reset")) == 0){
gpsReset();
LOGI("gps_reset");
}
else if (strncmp(cmd, "mcu_reset", strlen("setReset")) == 0){
loader_port_reset_target();
LOGI("mcu_reset Done");
}
#endif
else{
LOGI("cmd error");
}
/* free the buffer pbuf */
pbuf_free(pkt_buf);
udp_ota_shell_pcb = upcb;
remote_port = port;
remote_addr = *addr;
LOGI("recv_callback_ota_shell_udp end");
}
void udp_ota_shell_init(void)
{
LOGI("udp_ota_shell_init start");
unsigned int port = 9999;
struct udp_pcb *UDPpcb;
/* create a new UDP PCB structure */
UDPpcb = udp_new();
if (!UDPpcb)
{
/* Error creating PCB. Out of Memory */
return;
}
/* Bind this PCB to port 5002 */
err_t err = udp_bind(UDPpcb, IP4_ADDR_ANY, port);
if (err != ERR_OK)
{
/* Unable to bind to port */
return;
}
/* TFTP server start */
udp_recv(UDPpcb, recv_callback_ota_shell_udp, NULL);
LOGI("udp_ota_shell_init end");
}