g0b1vetx-board/Application/upgradethread.c
2023-11-30 08:10:01 +08:00

164 lines
3.2 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "main.h"
#include "os-app.h"
#include <stdio.h>
#include <rtthread.h>
#include "usart-board.h"
#include "data.h"
#include "easyflash.h"
#include <string.h>
#include "mqtt-board.h"
#include <stdlib.h>
ALIGN (RT_ALIGN_SIZE)
static char thread_stack[2048];
static struct rt_thread thread;
extern MqttDrv_t gMqttDrv;
static void YmodemC (void)
{
gMqttDrv.txbuff[0] = 0x43;
gMqttDrv.tx_size = 1;
gMqttDrv.tx_index = 0;
MQTT_SendData();
}
static void YmodemAkc (void)
{
gMqttDrv.txbuff[0] = 0x06;
gMqttDrv.tx_size = 1;
gMqttDrv.tx_index = 0;
MQTT_SendData();
}
static void YmodemNak (void)
{
gMqttDrv.txbuff[0] = 0x15;
gMqttDrv.tx_size = 1;
gMqttDrv.tx_index = 0;
MQTT_SendData();
}
static void YmodemFail (void)
{
gMqttDrv.txbuff[0] = 0x19;
gMqttDrv.tx_size = 1;
gMqttDrv.tx_index = 0;
MQTT_SendData();
}
static void YmodemSucc (void)
{
gMqttDrv.txbuff[0] = 0x17;
gMqttDrv.tx_size = 1;
gMqttDrv.tx_index = 0;
MQTT_SendData();
}
static int file_size = 0;
static char file_name[64];
static int cur_size = 0;
static char envbuf[128];
static void thread_entry (void * argument)
{
int tx_size ;
int packet_index = 0;
// 发送文件名给升级服务器
gMqttDrv.tx_size = rt_sprintf (gMqttDrv.txbuff, "N %s,%d", SysData.fileName, 1024);
gMqttDrv.tx_index = 0;
MQTT_SendData();
g_ifupdate = 1;
packet_index = 0;
cur_size = 0;
char * p;
while (g_ifupdate == 1)
{
if (RT_EOK == rt_event_recv (gTrigEvents, EVENT_TRIG_UPGRADE_RX, RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR, RT_WAITING_NO, RT_NULL))
{
switch (gMqttDrv.rx_size)
{
case 0x01:
if (gMqttDrv.rxbuff[0] == 0x06)
{
YmodemC();
}
else if (gMqttDrv.rxbuff[0] == 0x04)
{
YmodemNak();
rt_thread_mdelay (300);
YmodemSucc();
ef_set_env("iap_need_copy_app","1");
rt_sprintf(envbuf,"%d",file_size);
ef_set_env("iap_copy_app_size",envbuf);
ef_set_env("iap_app_filename",file_name);
ef_save_env(); // 存储环境变量
rt_kprintf("update Success\r\n");
rt_thread_mdelay(2000);
NVIC_SystemReset();
g_ifupdate = 0;
}
break;
case 133: // 第一包数据
p = &gMqttDrv.rxbuff[3];
rt_sprintf(file_name,"%s",p);
p = p + strlen(p) +1;
file_size = atoi(p);
// 擦除备份区数据
ef_erase_bak_app(file_size);
YmodemAkc();
rt_thread_mdelay (300);
YmodemC();
break;
case 1029: // 数据包
rt_kprintf ("1029packet,index:%d\r\n", gMqttDrv.rxbuff[1]);
if (packet_index + 1 != gMqttDrv.rxbuff[1]) // 数据丢包,升级失败
{
YmodemFail();
g_ifupdate = 0;
}
else
{
// 写程序到备份区
ef_write_data_to_bak((uint8_t *)&gMqttDrv.rxbuff[3],1024,(size_t *)&cur_size,file_size);
rt_kprintf("cursize:%d\r\n",cur_size) ;
YmodemAkc();
}
packet_index++;
break;
default:
break;
}
}
rt_thread_mdelay (100);
}
rt_kprintf ("upgrade end!\r\n");
}
//char * WriteEasyFlash(char *p)
//{
// if ()
//}
void UpgradeThreadStart (void)
{
/* 创建线程1名称是thread1入口是thread1_entry*/
rt_thread_init (&thread, "upgradethread",
thread_entry, RT_NULL,
&thread_stack[0],
sizeof (thread_stack),
UPGRADE_THREAD_PRIORITY, UPGRADE_THREAD_TIMESLICE);
rt_thread_startup (&thread);
return ;
}