259 lines
6.9 KiB
C
Executable File
259 lines
6.9 KiB
C
Executable File
#include "mcu_update.h"
|
|
#include "string.h"
|
|
#include "hilink_open_ota_mcu_adapter.h"
|
|
#include "hilink_device.h"
|
|
|
|
mcu_info_t hf_mcu_info = {0};
|
|
unsigned char hf_mcu_step = OTA_STEP_NULL;
|
|
static int ota_wait_time=100;
|
|
|
|
int hf_atcmd_mcu(pat_session_t s,int argc,char *argv[],char *rsp,int len)
|
|
{
|
|
if(argc==0)
|
|
{
|
|
if(hf_mcu_info.ota == 0)
|
|
sprintf(rsp,"=%s","off");
|
|
else if(hf_mcu_info.ota == 1)
|
|
sprintf(rsp,"=%s","on");
|
|
return 0;
|
|
}
|
|
else if(argc==1)
|
|
{
|
|
if(strcasecmp(argv[0],"on") == 0)
|
|
{
|
|
if(hf_mcu_info.ota == 0)
|
|
{
|
|
hf_mcu_info.ota=1;
|
|
hf_save_mcu_info();
|
|
}
|
|
return 0;
|
|
}
|
|
else if(strcasecmp(argv[0],"off") == 0)
|
|
{
|
|
if(hf_mcu_info.ota == 1)
|
|
{
|
|
hf_mcu_info.ota=0;
|
|
hf_save_mcu_info();
|
|
}
|
|
return 0;
|
|
}
|
|
return -4;
|
|
}
|
|
return -3;
|
|
}
|
|
int hf_atcmd_mcuver(pat_session_t s,int argc,char *argv[],char *rsp,int len)
|
|
{
|
|
if(argc == 1)
|
|
{
|
|
if(strlen(argv[0]) > 10)
|
|
return -4;
|
|
hf_set_mcu_version((char *)argv[0]);
|
|
return 0;
|
|
}
|
|
return -3;
|
|
}
|
|
|
|
int hf_atcmd_ota_wait_time(pat_session_t s,int argc,char *argv[],char *rsp,int len)
|
|
{
|
|
if(argc == 0)
|
|
{
|
|
sprintf(rsp,"=%d",ota_wait_time);
|
|
return 0;
|
|
}
|
|
else if(argc == 1)
|
|
{
|
|
int time_wait=atoi(argv[0]);
|
|
if(time_wait<SEND_UART_WAIT_TIME_MIN || time_wait>SEND_UART_WAIT_TIME_MAX)
|
|
return -4;
|
|
ota_wait_time=time_wait;
|
|
if(hf_mcu_info.wait_time != time_wait)
|
|
{
|
|
hf_mcu_info.wait_time=ota_wait_time;
|
|
hf_save_mcu_info();
|
|
}
|
|
return 0;
|
|
}
|
|
return -3;
|
|
}
|
|
const char *hf_get_mcu_ver(void)
|
|
{
|
|
if(hf_mcu_info.head != MCU_INFO_HEAD)
|
|
{
|
|
memset(&hf_mcu_info, 0, sizeof(hf_mcu_info));
|
|
strcpy((char *)hf_mcu_info.ver, MCU_VER);
|
|
hf_save_mcu_info();
|
|
}
|
|
return (const char *)hf_mcu_info.ver;
|
|
}
|
|
void hf_set_mcu_version(char *ver)
|
|
{
|
|
if(strlen(ver) >= MCU_VER_MAX_SIZE)
|
|
return ;
|
|
strcpy((char *)hf_mcu_info.ver,ver);
|
|
if(hf_mcu_info.head != MCU_INFO_HEAD || strcmp((char *)hf_mcu_info.ver,ver) != 0)
|
|
{
|
|
memset((char *)hf_mcu_info.ver,0,sizeof(hf_mcu_info.ver));
|
|
strcpy((char *)hf_mcu_info.ver,ver);
|
|
hf_save_mcu_info();
|
|
}
|
|
u_printf("get mcu ver:%s\r\n",hf_mcu_info.ver);
|
|
}
|
|
int hf_save_mcu_info(void)
|
|
{
|
|
hf_mcu_info.head=MCU_INFO_HEAD;
|
|
hfuflash_erase_page(MCU_INFO_FILE,1);
|
|
hfuflash_write(MCU_INFO_FILE,&hf_mcu_info,sizeof(mcu_info_t));
|
|
return 0;
|
|
}
|
|
static int hf_mcu_ota_is_vaild(void)
|
|
{
|
|
return hf_mcu_info.ota;
|
|
}
|
|
void hf_save_mcu_status(unsigned char flag, int len)
|
|
{
|
|
if(flag == START_SEND_DATA)
|
|
{
|
|
u_printf("ready to recv mcu firmware...\n");
|
|
}
|
|
else if(flag == STOP_SEND_DATA)
|
|
{
|
|
u_printf("recv mcu firmware over...\n");
|
|
hf_mcu_info.updateFlag = 1;
|
|
hf_mcu_info.len = len;
|
|
hf_save_mcu_info();
|
|
}
|
|
else if(flag == SEND_DATA_ERROR)
|
|
{
|
|
u_printf("recv mcu firmware error...\n");
|
|
}
|
|
}
|
|
|
|
static void mcu_upgrade_thread(void *arg)
|
|
{
|
|
int i = 0;
|
|
int ret = 0;
|
|
int seq = 0;
|
|
int offset = 0;
|
|
unsigned char *buff = hfmem_malloc(512);
|
|
unsigned char *data = hfmem_malloc(1024);
|
|
if(data == NULL || buff == NULL)
|
|
return ;
|
|
|
|
u_printf("mcu ver:%s\r\n",hf_mcu_info.ver);
|
|
|
|
if((hf_mcu_info.updateFlag > 0) && (hf_mcu_info.len > 0))
|
|
hf_mcu_step = OTA_STEP_START;
|
|
|
|
if(hf_mcu_info.wait_time >= SEND_UART_WAIT_TIME_MIN && hf_mcu_info.wait_time <= SEND_UART_WAIT_TIME_MAX)
|
|
ota_wait_time=hf_mcu_info.wait_time;
|
|
else
|
|
ota_wait_time=100;
|
|
while(1)
|
|
{
|
|
switch (hf_mcu_step)
|
|
{
|
|
case OTA_STEP_START:
|
|
{
|
|
memset(buff, 0, 512);
|
|
if(HILINK_GetDevStatus() == HILINK_M2M_CLOUD_ONLINE)
|
|
{
|
|
sprintf((char *)buff, "AT+OTASTART=%d\r\n", hf_mcu_info.len);
|
|
hfuart_send(HFUART0,buff, strlen((char *)buff),0);
|
|
u_printf("mcu ota OTA_STEP_START\r\n");
|
|
hf_mcu_step += 0x01;
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
case OTA_STEP_TRANS:
|
|
{
|
|
memset(buff, 0, 512);
|
|
memset(data, 0, 1024);
|
|
|
|
if(hf_mcu_info.len - offset >= OTA_READ_LEN)
|
|
{
|
|
flash_read(UPGRADE_ADDRESS + offset, buff, OTA_READ_LEN,0);
|
|
offset += OTA_READ_LEN;
|
|
seq += 0x01;
|
|
u_printf("mcu send data count :%d\r\n",offset/OTA_READ_LEN);
|
|
sprintf((char *)data, "AT+OTADATA=%d,%d,", seq, OTA_READ_LEN);
|
|
ret = strlen((char *)data);
|
|
for(i = 0; i < OTA_READ_LEN; i++)
|
|
sprintf((char *)data + ret + 2 * i, "%02x", buff[i]);
|
|
|
|
strncat((char *)data, "\r\n", 2);
|
|
|
|
hfuart_send(HFUART0,data, ret + (OTA_READ_LEN * 2) + 2,0);
|
|
}
|
|
else
|
|
{
|
|
flash_read(UPGRADE_ADDRESS + offset, buff, hf_mcu_info.len - offset,0);
|
|
seq += 0x01;
|
|
|
|
sprintf((char *)data, "AT+OTADATA=%d,%d,", seq, hf_mcu_info.len - offset);
|
|
ret = strlen((char *)data);
|
|
for(i = 0; i < (hf_mcu_info.len - offset); i++)
|
|
sprintf((char *)data + ret + 2 * i, "%02x", buff[i]);
|
|
|
|
strncat((char *)data, "\r\n", 2);
|
|
|
|
hfuart_send(HFUART0,data, ret + ((hf_mcu_info.len - offset) * 2) + 2,0);
|
|
|
|
offset = hf_mcu_info.len;
|
|
|
|
hf_mcu_step += 0x01;
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
case OTA_STEP_END:
|
|
{
|
|
hfuart_send(HFUART0,(unsigned char *)"AT+OTAEND\r\n", strlen("AT+OTAEND\r\n"),0);
|
|
seq = 0;
|
|
offset = 0;
|
|
hf_mcu_step = OTA_STEP_NULL;
|
|
hf_mcu_info.updateFlag = 0;
|
|
hf_save_mcu_info();
|
|
goto EXIT;
|
|
break;
|
|
}
|
|
|
|
case OTA_STEP_ERROR:
|
|
{
|
|
hfuart_send(HFUART0,(unsigned char *)"AT+OTAERR\r\n", strlen("AT+OTAERR\r\n"),0);
|
|
hf_mcu_step = OTA_STEP_NULL;
|
|
goto EXIT;
|
|
break;
|
|
}
|
|
|
|
default:
|
|
break;
|
|
}
|
|
msleep(ota_wait_time);
|
|
}
|
|
EXIT:
|
|
hfmem_free(data);
|
|
hfmem_free(buff);
|
|
hfthread_destroy(0);
|
|
}
|
|
|
|
void hf_mcu_thread(void)
|
|
{
|
|
hfthread_create(mcu_upgrade_thread, "hf-mcu-thread", 1024, NULL, HFTHREAD_PRIORITIES_LOW, NULL, NULL);
|
|
}
|
|
|
|
int hf_mcu_init(void)
|
|
{
|
|
hfuflash_read(MCU_INFO_FILE,&hf_mcu_info,sizeof(mcu_info_t));
|
|
if(hf_mcu_ota_is_vaild())
|
|
{
|
|
return -1;
|
|
}
|
|
char *get_mcu_ver="AT+GETMCUVER\r\n";
|
|
hfuart_send(HFUART0,(unsigned char *)get_mcu_ver, strlen(get_mcu_ver),0);
|
|
if(hf_mcu_info.updateFlag && hf_mcu_info.len)
|
|
hf_mcu_thread();
|
|
return HF_SUCCESS;
|
|
} |