Initial commit: TXW82x FPV v2.7.0.7-42229 SDK + project sources
This commit is contained in:
180
sdk/app/update/file_ota.c
Normal file
180
sdk/app/update/file_ota.c
Normal file
@@ -0,0 +1,180 @@
|
||||
#include "basic_include.h"
|
||||
#include "osal_file.h"
|
||||
#include "lib/ota/fw.h"
|
||||
|
||||
|
||||
|
||||
|
||||
#define OTA_FILE_DIR "ota"
|
||||
#define OTA_FILE_NAME "ota.bin"
|
||||
#define OTA_CACHE_SIZE (4096)
|
||||
|
||||
static uint8_t g_file_ota = 0;
|
||||
static struct os_work fileota_work;
|
||||
|
||||
uint32_t file_ota()
|
||||
{
|
||||
if(g_file_ota) {
|
||||
os_printf(KERN_DEBUG"File OTA already done.\n");
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
char path[64];
|
||||
os_sprintf(path, "0:%s/%s", OTA_FILE_DIR, OTA_FILE_NAME);
|
||||
|
||||
void *fp = osal_fopen(path, "rb");
|
||||
if (!fp) {
|
||||
os_printf(KERN_ERR"OTA file not found: %s\n", path);
|
||||
return RET_ERR;
|
||||
}
|
||||
|
||||
uint32_t file_size = osal_fsize(fp);
|
||||
if (file_size < 1024) {
|
||||
osal_fclose(fp);
|
||||
os_printf(KERN_ERR"OTA file size is too small: %d bytes\n", file_size);
|
||||
return RET_ERR;
|
||||
}
|
||||
|
||||
uint8_t *ota_cache_buff = (uint8_t *)os_malloc(OTA_CACHE_SIZE);
|
||||
|
||||
if (!ota_cache_buff) {
|
||||
osal_fclose(fp);
|
||||
os_printf(KERN_ERR"Failed to allocate memory for OTA cache buffer.\n");
|
||||
return RET_ERR;
|
||||
}
|
||||
|
||||
uint32_t read_size = 0;
|
||||
uint32_t ota_offset = 0;
|
||||
uint32_t filesize_tmp = file_size;
|
||||
|
||||
os_printf(KERN_DEBUG"OTA file start processing, size: %d bytes, cache buffer: %x\n", file_size, ota_cache_buff);
|
||||
|
||||
while(file_size)
|
||||
{
|
||||
if (file_size < OTA_CACHE_SIZE) {
|
||||
read_size = file_size;
|
||||
} else {
|
||||
read_size = OTA_CACHE_SIZE;
|
||||
}
|
||||
|
||||
osal_fread(ota_cache_buff, read_size, 1, fp);
|
||||
int res = libota_write_fw(filesize_tmp, ota_offset, ota_cache_buff, read_size);
|
||||
if (res) {
|
||||
os_printf(KERN_DEBUG"libota_write_fw failed at offset %d, res: %d\n", ota_offset, res);
|
||||
break;
|
||||
}
|
||||
os_printf("OTA file size: %d bytes, cache buffer: %x\n", file_size, ota_cache_buff);
|
||||
|
||||
file_size -= read_size;
|
||||
ota_offset += read_size;
|
||||
}
|
||||
|
||||
if(file_size == 0) {
|
||||
g_file_ota = 1;
|
||||
os_printf(KERN_DEBUG"OTA file successfully processed.\n");
|
||||
} else {
|
||||
os_printf(KERN_ERR"OTA file processing incomplete.\n");
|
||||
}
|
||||
|
||||
|
||||
if (fp) {
|
||||
osal_fclose(fp);
|
||||
}
|
||||
|
||||
if (ota_cache_buff) {
|
||||
os_free(ota_cache_buff);
|
||||
}
|
||||
|
||||
if (g_file_ota) {
|
||||
osal_unlink(path);
|
||||
mcu_reset();
|
||||
}
|
||||
|
||||
return (file_size == 0) ? RET_OK : RET_ERR;
|
||||
}
|
||||
|
||||
static int32 file_ota_work(struct os_work *work)
|
||||
{
|
||||
if(g_file_ota) {
|
||||
os_printf(KERN_DEBUG"File OTA already done.\n");
|
||||
goto __file_ota_work_end;
|
||||
}
|
||||
|
||||
char path[64];
|
||||
os_sprintf(path, "0:%s/%s", OTA_FILE_DIR, OTA_FILE_NAME);
|
||||
|
||||
void *fp = osal_fopen(path, "r");
|
||||
if (!fp) {
|
||||
os_printf(KERN_ERR"OTA file not found: %s\n", path);
|
||||
goto __file_ota_work_end;
|
||||
}
|
||||
|
||||
uint32_t file_size = osal_fsize(fp);
|
||||
if (file_size < 1024) {
|
||||
osal_fclose(fp);
|
||||
fp = NULL;
|
||||
os_printf(KERN_ERR"OTA file size is too small: %d bytes\n", file_size);
|
||||
goto __file_ota_work_end;
|
||||
}
|
||||
|
||||
uint8_t *ota_cache_buff = (uint8_t *)os_malloc(OTA_CACHE_SIZE);
|
||||
if (!ota_cache_buff) {
|
||||
os_printf(KERN_ERR"Failed to allocate memory for OTA cache buffer.\n");
|
||||
goto __file_ota_end;
|
||||
}
|
||||
os_printf("OTA file size: %d bytes, cache buffer: %x\n", file_size, ota_cache_buff);
|
||||
uint32_t read_size = 0;
|
||||
uint32_t ota_offset = 0;
|
||||
uint32_t filesize_tmp = file_size;
|
||||
|
||||
while(file_size)
|
||||
{
|
||||
if (file_size < OTA_CACHE_SIZE) {
|
||||
read_size = file_size;
|
||||
} else {
|
||||
read_size = OTA_CACHE_SIZE;
|
||||
}
|
||||
|
||||
osal_fread(ota_cache_buff, read_size, 1, fp);
|
||||
int res = libota_write_fw(filesize_tmp, ota_offset, ota_cache_buff, read_size);
|
||||
if (res) {
|
||||
os_printf(KERN_DEBUG"libota_write_fw failed at offset %d, res: %d\n", ota_offset, res);
|
||||
break;
|
||||
}
|
||||
os_printf("read_size:%d file_size:%d ota_offset:%d\n", read_size, file_size, ota_offset);
|
||||
file_size -= read_size;
|
||||
ota_offset += read_size;
|
||||
}
|
||||
|
||||
if(file_size == 0) {
|
||||
g_file_ota = 1;
|
||||
os_printf(KERN_DEBUG"OTA file successfully processed.\n");
|
||||
} else {
|
||||
os_printf(KERN_ERR"OTA file processing incomplete.\n");
|
||||
}
|
||||
|
||||
if (g_file_ota) {
|
||||
osal_unlink(path);
|
||||
}
|
||||
|
||||
__file_ota_end:
|
||||
if (fp) {
|
||||
osal_fclose(fp);
|
||||
}
|
||||
|
||||
if (ota_cache_buff) {
|
||||
os_free(ota_cache_buff);
|
||||
}
|
||||
|
||||
__file_ota_work_end:
|
||||
|
||||
os_run_work_delay(work, 2000);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void file_ota_work_init()
|
||||
{
|
||||
OS_WORK_INIT(&fileota_work, file_ota_work, 0);
|
||||
os_run_work_delay(&fileota_work, 1);
|
||||
}
|
||||
292
sdk/app/update/ota.c
Normal file
292
sdk/app/update/ota.c
Normal file
@@ -0,0 +1,292 @@
|
||||
#include <stdio.h>
|
||||
#include "sys_config.h"
|
||||
#include "lwip\sockets.h"
|
||||
#include "lwip\netif.h"
|
||||
#include "lwip\dns.h"
|
||||
//#include "lwip\fcntl.h"
|
||||
#include "lwip\api.h"
|
||||
#include "lwip\tcp.h"
|
||||
|
||||
#include <event.h>
|
||||
#include <log.h>
|
||||
#include <frame.h>
|
||||
#include <rtp.h>
|
||||
#include "spook_config.h"
|
||||
//#include <linux/linux_mutex.h>
|
||||
#include "lwip/api.h"
|
||||
#include <csi_kernel.h>
|
||||
#include "ota.h"
|
||||
//#include "flash.h"
|
||||
#include "socket_module.h"
|
||||
#include "dev/csi/hgdvp.h"
|
||||
#include "hal/spi_nor.h"
|
||||
#include "lib/ota/fw.h"
|
||||
|
||||
// 结构体申请空间函数
|
||||
#define STREAM_LIBC_MALLOC os_malloc
|
||||
#define STREAM_LIBC_FREE os_free
|
||||
#define STREAM_LIBC_ZALLOC os_zalloc
|
||||
|
||||
#define RECV_MAX_SIZE 1460
|
||||
|
||||
enum TcpServerCmd
|
||||
{
|
||||
REQ_NET_UPGRADE = 0xA0,
|
||||
ACK_NET_UPGRADE = 0xA1,
|
||||
};
|
||||
|
||||
typedef struct tcpServerHeader
|
||||
{
|
||||
unsigned int Command;
|
||||
unsigned int Reserved[8];
|
||||
}TCP_SERVER_H;
|
||||
|
||||
typedef struct tcpNetUpgradeInfo
|
||||
{
|
||||
char version[16];
|
||||
int fileSize;
|
||||
}NetUpgrade_Info;
|
||||
|
||||
|
||||
int InitSrv(uint16_t port)
|
||||
{
|
||||
int srvsock;
|
||||
srvsock = creat_tcp_socket(port);
|
||||
if(listen(srvsock,3)== -1)
|
||||
{
|
||||
//perror("listen()error\n");
|
||||
return -1;
|
||||
}
|
||||
return srvsock;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void tcpNetUpgrade(int tcp_connect_fd)
|
||||
{
|
||||
int res = 0;
|
||||
uint8 *tcp_rcv_buff = NULL;
|
||||
int size = 0;
|
||||
int offset = 0;
|
||||
tcp_rcv_buff = (uint8*)STREAM_LIBC_MALLOC(RECV_MAX_SIZE + 4);
|
||||
memset(tcp_rcv_buff, 0, RECV_MAX_SIZE + 4);
|
||||
TCP_SERVER_H tcpHeader;
|
||||
memset(&tcpHeader, 0, sizeof(TCP_SERVER_H));
|
||||
tcpHeader.Command = ACK_NET_UPGRADE;
|
||||
|
||||
|
||||
//spi_nor_open(flash);
|
||||
if(send(tcp_connect_fd, (char*)&tcpHeader, sizeof(TCP_SERVER_H), 0) > 0)
|
||||
{
|
||||
NetUpgrade_Info netUpgradeInfo;
|
||||
memset(&netUpgradeInfo, 0, sizeof(NetUpgrade_Info));
|
||||
recv(tcp_connect_fd, (char*)&netUpgradeInfo, sizeof(NetUpgrade_Info), 0);
|
||||
while(offset < netUpgradeInfo.fileSize)
|
||||
{
|
||||
memset(tcp_rcv_buff, 0, RECV_MAX_SIZE + 4);
|
||||
size = recv(tcp_connect_fd, tcp_rcv_buff, RECV_MAX_SIZE, 0);
|
||||
if(size <= 0)
|
||||
{
|
||||
_os_printf("%s:%d err,offset:%d\n",__FUNCTION__,__LINE__,offset);
|
||||
break;
|
||||
}
|
||||
|
||||
//res = libotaV2_write_fw(netUpgradeInfo.fileSize,1,0,0,offset, tcp_rcv_buff, size);
|
||||
res = libota_write_fw(netUpgradeInfo.fileSize,offset,tcp_rcv_buff,size);
|
||||
if(res)
|
||||
{
|
||||
send(tcp_connect_fd, "Upgrade_err", 12, 0);
|
||||
_os_printf("%s:%d libota_write_fw err,offset:%d,res:%d\n",__FUNCTION__,__LINE__,offset,res);
|
||||
break;
|
||||
}
|
||||
|
||||
offset += size;
|
||||
//_os_printf("recv size: %d, offset: %d(%d%%)\n", size, offset, offset*100/netUpgradeInfo.fileSize);
|
||||
|
||||
};
|
||||
if(offset == netUpgradeInfo.fileSize)
|
||||
{
|
||||
//将数据头回写,代表已经完成
|
||||
//spi_nor_write(flash,update_adr+0,(unsigned char*)&ota_flag,sizeof(ota_flag));
|
||||
size = send(tcp_connect_fd, "Upgrade_ok", 11, 0);
|
||||
_os_printf("NetUpgrade ok ###\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
size = send(tcp_connect_fd, "Upgrade_err", 12, 0);
|
||||
_os_printf("NetUpgrade err offset = %d, fileSize = %d ###\n", offset, netUpgradeInfo.fileSize);
|
||||
}
|
||||
}
|
||||
STREAM_LIBC_FREE(tcp_rcv_buff);
|
||||
close(tcp_connect_fd);
|
||||
//spi_nor_close(flash);
|
||||
}
|
||||
|
||||
|
||||
|
||||
extern void tcp_update(void *d);
|
||||
void tcpServerAccept(void *e, void *d)
|
||||
{
|
||||
int tcp_server_fd = (int)d;
|
||||
int tcp_connect_fd = -1;
|
||||
struct sockaddr_in client;
|
||||
socklen_t addrlen;
|
||||
|
||||
TCP_SERVER_H tcpHeader;
|
||||
memset(&tcpHeader, 0, sizeof(TCP_SERVER_H));
|
||||
|
||||
addrlen =sizeof(client);
|
||||
tcp_connect_fd = accept(tcp_server_fd, (struct sockaddr*)&client, &addrlen);
|
||||
if(tcp_connect_fd < 0)
|
||||
{
|
||||
_os_printf("accept()error\n");
|
||||
}
|
||||
os_task_create("tcp_update", tcp_update, (void*)tcp_connect_fd, OS_TASK_PRIORITY_NORMAL, 0, NULL, 2048);
|
||||
|
||||
}
|
||||
|
||||
|
||||
void ota_Tcp_Server()
|
||||
{
|
||||
uint16_t port = 5007;
|
||||
int tcp_server_fd;
|
||||
|
||||
_os_printf("[test] init tcp server: port: %d\n", port);
|
||||
|
||||
tcp_server_fd = InitSrv(port);
|
||||
|
||||
eloop_add_fd( tcp_server_fd, EVENT_READ, EVENT_F_ENABLED, tcpServerAccept, (void*)tcp_server_fd );
|
||||
}
|
||||
|
||||
|
||||
void tcp_recv_data(void *d)
|
||||
{
|
||||
|
||||
int tcp_connect_fd = (int)d;
|
||||
uint8_t *tcp_rcv_buff;
|
||||
int size;
|
||||
tcp_rcv_buff = (uint8_t*)STREAM_LIBC_MALLOC(1024 + 4);
|
||||
memset(tcp_rcv_buff, 0, sizeof(1024+4));
|
||||
|
||||
|
||||
#if DVP_EN
|
||||
//dvp_close();
|
||||
void *dvp = (void *)dev_get(HG_DVP_DEVID);
|
||||
if(dvp)
|
||||
{
|
||||
dvp_close(dvp);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if USB_EN
|
||||
SYSCTRL_REG_OPT(sysctrl_usb20_clk_close());
|
||||
#endif
|
||||
while(1){
|
||||
size = recv(tcp_connect_fd, tcp_rcv_buff, 1024, 0);
|
||||
if(size > 0){
|
||||
//_os_printf("tcp_rcv_buff(%d):%x %x %x %x\r\n",size,tcp_rcv_buff[0],tcp_rcv_buff[1],tcp_rcv_buff[2],tcp_rcv_buff[3]);
|
||||
}else{
|
||||
_os_printf("recv error\r\n");
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
|
||||
end:
|
||||
close(tcp_connect_fd);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
void tcpAccept(void *e, void *d)
|
||||
{
|
||||
int fd = (int)d;
|
||||
int tcp_connect_fd = -1;
|
||||
struct sockaddr_in client;
|
||||
socklen_t addrlen;
|
||||
|
||||
TCP_SERVER_H tcpHeader;
|
||||
memset(&tcpHeader, 0, sizeof(TCP_SERVER_H));
|
||||
|
||||
addrlen =sizeof(client);
|
||||
tcp_connect_fd = accept(fd, (struct sockaddr*)&client, &addrlen);
|
||||
if(tcp_connect_fd < 0)
|
||||
{
|
||||
_os_printf("accept()error\n");
|
||||
}
|
||||
os_task_create("tcp_recv", tcp_recv_data, (void*)tcp_connect_fd, OS_TASK_PRIORITY_NORMAL, 0, NULL, 2048);
|
||||
}
|
||||
|
||||
|
||||
void Tcp_Server_test()
|
||||
{
|
||||
uint16_t port = 8090;
|
||||
|
||||
_os_printf("[test] init tcp server: port: %d\n", port);
|
||||
|
||||
int fd = InitSrv(port);
|
||||
|
||||
eloop_add_fd( fd, EVENT_READ, EVENT_F_ENABLED, tcpAccept, (void*)fd );
|
||||
}
|
||||
|
||||
|
||||
|
||||
static int8_t tcp_update_running = 0;
|
||||
int get_tcp_update_status()
|
||||
{
|
||||
return tcp_update_running;
|
||||
}
|
||||
|
||||
void tcp_update(void *d)
|
||||
{
|
||||
|
||||
int tcp_connect_fd = (int)d;
|
||||
|
||||
if(tcp_update_running)
|
||||
{
|
||||
goto end;
|
||||
}
|
||||
tcp_update_running = 1;
|
||||
TCP_SERVER_H tcpHeader;
|
||||
memset(&tcpHeader, 0, sizeof(TCP_SERVER_H));
|
||||
|
||||
|
||||
#if DVP_EN
|
||||
//dvp_close();
|
||||
void *dvp = (void *)dev_get(HG_DVP_DEVID);
|
||||
if(dvp)
|
||||
{
|
||||
dvp_close(dvp);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if USB_EN
|
||||
SYSCTRL_REG_OPT(sysctrl_usb20_clk_close());
|
||||
#endif
|
||||
if(recv(tcp_connect_fd, (char*)&tcpHeader, sizeof(TCP_SERVER_H), 0) > 0)
|
||||
{
|
||||
_os_printf("tcpHeader.Command = %x ###\n", tcpHeader.Command);
|
||||
switch(tcpHeader.Command)
|
||||
{
|
||||
case REQ_NET_UPGRADE:
|
||||
{
|
||||
tcpNetUpgrade(tcp_connect_fd);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
tcp_update_running = 0;
|
||||
end:
|
||||
|
||||
close(tcp_connect_fd);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
18
sdk/app/update/ota.h
Normal file
18
sdk/app/update/ota.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#ifndef _OTA_H_
|
||||
#define _OTA_H_
|
||||
|
||||
|
||||
|
||||
|
||||
void ota_Tcp_Server();
|
||||
|
||||
uint32_t file_ota();
|
||||
void file_ota_work_init();
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user