send bin file by 4g

This commit is contained in:
wgzAIIT 2023-05-26 11:23:12 +08:00
parent c7ac5681e4
commit f59139f78e
3 changed files with 668 additions and 29 deletions

View File

@ -1,23 +1,34 @@
/*
* Copyright 2018-2020 NXP
* All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
/**
* @file ota.c
* @brief file ota.c
* @version 2.0
* @author AIIT XUOS Lab
* @date 2023-04-03
* Copyright (c) 2020 AIIT XUOS Lab
* XiUOS is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/**
* @file: ota.c
* @brief: file ota.c
* @version: 1.0
* @author: AIIT XUOS Lab
* @date: 2023/4/23
*
*/
#include <transform.h>
#include "shell.h"
#include "xsconfig.h"
#include "mcuboot.h"
#include "ymodem.h"
#include "ota.h"
#ifdef CONNECTION_ADAPTER_4G
#include <adapter.h>
#endif
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
@ -110,6 +121,29 @@ static uint32_t calculate_crc32(uint32_t addr, uint32_t len)
}
/*******************************************************************************
* : calculate_crc16
* : crc16的值,OTA传输过程中数据帧的校验
* : data:buffer
len:CRC16的数据长度
* : CRC16值
*******************************************************************************/
static uint16_t calculate_crc16(uint8_t * data, uint32_t len)
{
uint16_t reg_crc=0xFFFF;
while(len--) {
reg_crc ^= *data++;
for (int j=0;j<8;j++) {
if(reg_crc & 0x01)
reg_crc=reg_crc >>1 ^ 0xA001;
else
reg_crc=reg_crc >>1;
}
}
printf(" crc = [0x%x]\n",reg_crc);
return reg_crc;
}
/*******************************************************************************
* : UpdateApplication
* : bootloader里进行调用,Flash中Flag分区中的信息决定是否进行版本更新
@ -305,13 +339,13 @@ static status_t UpdateOTAFlag(ota_info_t *ptr)
}
/*******************************************************************************
* : app_ota
* : app中通过命令来进行ota升级,
/*********************************************************************************
* : app_ota_by_iap
* : ota升级,,iap方式传输bin文件
* :
* :
*******************************************************************************/
static void app_ota(void)
*********************************************************************************/
static void app_ota_by_iap(void)
{
int32_t size;
ota_info_t ota_info;
@ -344,7 +378,7 @@ static void app_ota(void)
mcuboot.flash_deinit();
mcuboot.op_reset();
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_PARAM_NUM(0),ota, app_ota, ota function);
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_PARAM_NUM(0),ota, app_ota_by_iap, ota by iap function);
/*******************************************************************************
@ -439,3 +473,160 @@ void ota_entry(void)
}
}
}
#ifdef CONNECTION_ADAPTER_4G
/*******************************************************************************
* : ota_data_recv
* : 4G方式从服务端接收数据
* : adapter:Adapter指针,4G设备
* : 0:,-1:
*******************************************************************************/
static int ota_data_recv(struct Adapter* adapter)
{
struct ota_data recv_msg;
char reply[16] = {0};
int ret = 0;
int try_times = 10;
int frame_cnt = 0;
while(1) {
memset(&recv_msg, 0, sizeof(struct ota_data));
ret = AdapterDeviceRecv(adapter, &recv_msg, sizeof(struct ota_data));
if(ret >= 0 && recv_msg.header.frame_flag == 0x5A5A)
{
if(0 == strncmp("aiit_ota_end",recv_msg.frame.frame_data, strlen("aiit_ota_end"))) //说明当前是结束帧
{
printf("total [%d]frames [%d]Bytes crc[%x],receive successful,\n",frame_cnt,recv_msg.header.total_len,recv_msg.frame.crc);
memset(reply, 0, 16);
memcpy(reply, "ok", strlen("ok"));
AdapterDeviceSend(adapter, reply, strlen(reply));
ret = 0;
break;
}
frame_cnt = recv_msg.frame.frame_id;
if (recv_msg.frame.crc == calculate_crc16(recv_msg.frame.frame_data,recv_msg.frame.frame_len))
{
printf("current [%d] frame,length[%d] Bytes.\n",frame_cnt,recv_msg.frame.frame_len);
/*写入flash待实现*/
}
else
{
printf("current [%d] frame crc check failed,try again!\n",frame_cnt);
goto try_again;
}
send_ok_again:
memset(reply, 0, 16);
memcpy(reply, "ok", strlen("ok"));
ret = AdapterDeviceSend(adapter, reply, strlen(reply));
if(ret < 0){
printf("send ok failed.\n");
goto send_ok_again;
}
printf("send reply[%s] done.\n",reply);
//send ok后把try_times重置为10
try_times = 10;
continue;
}
//没有接收到数据或者接收到的数据帧frame_flag不等于0x5A5A,需要发个retry的命令告诉服务器需要重传
else
{
try_again:
if(try_times == 0)
{
printf("current [%d] frame try 10 times failed,break out!\n",frame_cnt);
ret = -1;
break;
}
memset(reply, 0, 16);
memcpy(reply, "retry", strlen("retry"));
printf("[%d] frame receive failed. retry\n",frame_cnt);
AdapterDeviceSend(adapter, reply, strlen(reply));
try_times--;
continue;
}
}
if(0 == ret) {
printf("ota file done,start application.\n");
//传输完成需要干什么;
}
return ret;
}
/*******************************************************************************
* : OtaKTaskEntry
* : ota升级,,4g方式传输bin文件
* : adapter:Adapter指针,4G设备
* : 0:,-1:
*******************************************************************************/
void app_ota_by_4g(void)
{
struct ota_data recv_msg;
char reply[16] = {0};
int baud_rate = BAUD_RATE_115200;
int len = 0;
int ret = 0;
struct Adapter* adapter = AdapterDeviceFindByName(ADAPTER_4G_NAME);
uint8 server_addr[64] = "115.238.53.60";
uint8 server_port[64] = "7777";
adapter->socket.socket_id = 0;
AdapterDeviceOpen(adapter);
AdapterDeviceControl(adapter, OPE_INT, &baud_rate);
AdapterDeviceConnect(adapter, CLIENT, server_addr, server_port, IPV4);
PrivTaskDelay(100);
while(1)
{
memset(&recv_msg, 0, sizeof(struct ota_data));
/* step1: Confirm the start signal of transmission*/
printf("waiting for start msg...\n");
ret = AdapterDeviceRecv(adapter, &recv_msg, sizeof(struct ota_data));
if(ret >= 0 && recv_msg.header.frame_flag == 0x5A5A)
{
if (0 == strncmp("aiit_ota_start",recv_msg.frame.frame_data, strlen("aiit_ota_start")))
{
memset(reply, 0, 16);
memcpy(reply, "ready", strlen("ready"));
printf("receive start signal,send [ready] signal to server\n");
send_ready_again:
ret = AdapterDeviceSend(adapter, reply, strlen(reply));
if(ret < 0)
{
goto send_ready_again;
}
printf("start receive ota file.\n");
/* step2: start receive bin file */
ret = ota_data_recv(adapter);
if (0 != ret)
{
memset(reply, 0, 16);
memcpy(reply, "ota_restart", strlen("ota_restart"));
AdapterDeviceSend(adapter, reply, strlen(reply));
continue;
}
else
{
break;
}
}
}
else
{
memset(reply, 0, 16);
memcpy(reply, "notready", strlen("notready"));
ret = AdapterDeviceSend(adapter, reply, strlen(reply));
}
}
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_PARAM_NUM(0),ota4g, app_ota_by_4g, ota by 4g function);
#endif

View File

@ -1,16 +1,22 @@
/*
* Copyright 2018-2020 NXP
* All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
* Copyright (c) 2020 AIIT XUOS Lab
* XiUOS is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/**
* @file ota.h
* @brief file ota.h
* @version 2.0
* @author AIIT XUOS Lab
* @date 2023-04-03
* @file: ota.h
* @brief: file ota.h
* @version: 1.0
* @author: AIIT XUOS Lab
* @date: 2023/4/23
*
*/
#ifndef __OTA_DEF_H__
#define __OTA_DEF_H__
@ -19,6 +25,7 @@
#define JUMP_FAILED_FLAG 0XABABABAB
#define JUMP_SUCCESS_FLAG 0XCDCDCDCD
#define LENGTH 64 //每帧数据的数据包长度
typedef enum {
OTA_STATUS_IDLE = 0, // 空闲状态,没有进行OTA升级
@ -52,6 +59,30 @@ typedef struct {
uint8_t error_message[128]; // 错误信息,最多128个字符
} ota_info_t;
/*bin包传输过程中的数据帧相关的结构体*/
struct ota_header_t
{
uint16_t frame_flag; // frame start flag 2 Bytes
uint16_t dev_sid; // device software version
uint32_t total_len; // send data total length caculated from each frame_len
};
struct ota_frame_t
{
uint32_t frame_id; // Current frame id
uint8_t frame_data[LENGTH]; // Current frame data,max length 256
uint16_t frame_len; // Current frame data length
uint16_t crc; // Current frame data crc
};
struct ota_data
{
struct ota_header_t header;
struct ota_frame_t frame;
};
void app_clear_jumpflag(void);
void ota_entry(void);
#endif

View File

@ -0,0 +1,417 @@
/*
* Copyright (c) 2020 AIIT XUOS Lab
* XiUOS is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/**
* @file: ota_server.c
* @brief: a application ota task of system running in Linux
* @version: 1.0
* @author: AIIT XUOS Lab
* @date: 2023/5/26
*
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <pthread.h>
#include <time.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/time.h>
#include <assert.h>
#include <netdb.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <ifaddrs.h>
#define PORT 7777 //socket端口号
#define SIZE 100 //socket链接限制为100
#define LENGTH 64 //每帧数据的数据包长度
#define BIN_PATH "/home/aep05/wgz/XiZi-xidatong-arm32-app.bin" //bin包的路径
struct ota_header_t
{
uint16_t frame_flag; // frame start flag 2 Bytes
uint16_t dev_sid; // device software version
uint32_t total_len; // send data total length caculated from each frame_len
};
struct ota_frame_t
{
uint32_t frame_id; // Current frame id
uint8_t frame_data[LENGTH]; // Current frame data,max length 256
uint16_t frame_len; // Current frame data length
uint16_t crc; // Current frame data crc
};
struct ota_data
{
struct ota_header_t header;
struct ota_frame_t frame;
};
static int serverfd; // 服务器socket
static int clientfd[SIZE] = {0}; // 客户端的socketfd,100个元素clientfd[0]~clientfd[99]
/*******************************************************************************
* : calculate_crc16
* : crc16的值,OTA传输过程中数据帧的校验
* : data:buffer
len:CRC16的数据长度
* : CRC16值
*******************************************************************************/
static uint16_t calculate_crc16(uint8_t * data, uint32_t len)
{
uint16_t reg_crc=0xFFFF;
while(len--) {
reg_crc ^= *data++;
for (int j=0;j<8;j++) {
if(reg_crc & 0x01)
reg_crc=reg_crc >>1 ^ 0xA001;
else
reg_crc=reg_crc >>1;
}
}
printf(" crc = [0x%x]\n",reg_crc);
return reg_crc;
}
/*******************************************************************************
* : sockt_init
* : TCP Server上创建socet监听
* : data:buffer
len:CRC16的数据长度
* : CRC16值
*******************************************************************************/
void sockt_init(void)
{
struct sockaddr_in addr, *sa;//存储套接字的信息
struct ifaddrs *ifap, *ifa;
char *ipaddr;
serverfd = socket(AF_INET,SOCK_STREAM,0);
if (serverfd == -1)
{
perror("创建socket失败");
exit(-1);
}
//为套接字设置ip协议 设置端口号并自动获取本机ip转化为网络ip
addr.sin_family = AF_INET;//地址族
addr.sin_port = htons(PORT);//设置server端端口号,随便设置,当sin_port = 0时系统随机选择一个未被使用的端口号
addr.sin_addr.s_addr = htons(INADDR_ANY);//当sin_addr=INADDR_ANY时表示从本机的任一网卡接收数据
/*显示当前TCP server的*/
getifaddrs(&ifap);
for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
if (ifa->ifa_addr->sa_family == AF_INET) {
sa = (struct sockaddr_in *) ifa->ifa_addr;
ipaddr = inet_ntoa(sa->sin_addr);
printf("Interface: %s\tAddress: %s\n", ifa->ifa_name, ipaddr);
}
}
freeifaddrs(ifap);
//绑定套接字
struct timeval timeout;
timeout.tv_sec = 5;
timeout.tv_usec = 0;
if(setsockopt(serverfd, SOL_SOCKET, SO_REUSEADDR, &timeout, sizeof(struct timeval)) < 0)
{
perror("端口设置失败");
exit(-1);
}
if (bind(serverfd,(struct sockaddr*)&addr,sizeof(addr)) == -1)
{
perror("绑定失败");
exit(-1);
}
//监听最大连接数
if (listen(serverfd,SIZE) == -1)
{
perror("设置监听失败");
exit(-1);
}
}
/*******************************************************************************
* : ota_file_send
* : TCP Server发送bin文件
* : fd:fd
* : 0-1
*******************************************************************************/
int ota_file_send(int fd)
{
unsigned char buf[32] = { 0 };
struct ota_data data;
FILE *file_fd;
int length = 0;
int try_times;
int recv_end_times = 3;
int ret = 0;
int frame_cnt = 0;
int file_length = 0;
char * file_buf = NULL;
file_fd = fopen(BIN_PATH, "r");
if (NULL == file_fd){
printf("open file failed.\n");
return -1;
}
fseek(file_fd, 0, SEEK_SET);
printf("start send file.\n");
while(!feof(file_fd))
{
memset(&data, 0, sizeof(data));
data.header.frame_flag = 0x5A5A;
length = fread(data.frame.frame_data, 1, LENGTH, file_fd);
if(length > 0)
{
printf("read %d Bytes\n",length);
data.frame.frame_id = frame_cnt;
data.frame.frame_len = length;
data.frame.crc = calculate_crc16(data.frame.frame_data, length);
file_length += length;
}
send_again:
printf("ota send current[%d] frame.\n",frame_cnt);
length = send(fd, &data, sizeof(data), MSG_NOSIGNAL);
if(length < 0){
printf("send [%d] frame faile.go to send again\n",frame_cnt);
goto send_again;
}
recv_again:
memset(buf, 0, 32);
length = recv(fd, buf, sizeof(buf), 0);
if(length < 0 ){
printf("[%d] frame waiting for ok timeout,receive again.\n",frame_cnt);
goto recv_again;
}
//接收到的回复不是ok,说明刚发的包有问题,需要再发一次
printf("receive buf[%s] length = %d\n",buf, length);
if(0 == strncmp(buf, "ok", length))
{
try_times = 10;
printf("[%d]frame data send done.\n",frame_cnt);
frame_cnt++;
continue;
}
else
{
if(try_times > 0)
{
try_times--;
goto send_again;
}
else
{
printf("send frame[%d] 10 times failed.\n",frame_cnt);
ret = -1;
break;
}
}
}
/* finally,crc check total bin file.*/
if (ret == 0)
{
printf("total send file length[%d] Bytes [%d] frames.\n",file_length,frame_cnt);
printf("now crc check total bin file.\n");
file_buf = malloc(file_length);
memset(file_buf, 0, file_length);
memset(&data, 0, sizeof(data));
data.header.frame_flag = 0x5A5A;
file_fd = fopen(BIN_PATH, "r");
if (NULL == file_fd){
printf("open file failed.\n");
return -1;
}
fseek(file_fd, 0, SEEK_SET);
length = fread(file_buf,1, file_length, file_fd);
printf("read file length = %d\n",length);
if(length > 0) {
data.frame.frame_id = frame_cnt;
data.header.total_len = file_length;
data.frame.frame_len = strlen("aiit_ota_end");
data.frame.crc = calculate_crc16(file_buf, length);
memcpy(data.frame.frame_data,"aiit_ota_end",strlen("aiit_ota_end"));
}
send_end_signal:
printf("send aiit_ota_end signal.\n");
length = send(fd, &data, sizeof(data), MSG_NOSIGNAL);
if(length < 0){
printf("send end signal faile,send end signal again\n");
goto send_end_signal;
}
recv_end_signal:
memset(buf, 0, 32);
length = recv(fd, buf, sizeof(buf), 0);
if(length < 0 )
{
recv_end_times--;
printf("end signal waiting for ok timeout,receive again.\n");
if(recv_end_times > 0)
{
goto recv_end_signal;
}
else
{
ret = -1;
}
}
if(0 != strncmp(buf, "ok", length))
{
printf("error end !!!\n");
ret = -1;
}
free(file_buf);
}
fclose(file_fd);
return ret;
}
/*******************************************************************************
* : server_thread
* : TCP Server的服务线程入口函数
* : p:
* :
*******************************************************************************/
void* server_thread(void* p)
{
int fd = *(int*)p;
unsigned char buf[32] = { 0 };
struct ota_data data;
int ret = 0;
int length = 0;
printf("pthread = %d\n",fd);
sleep(8);
while(1)
{
memset(&data, 0x0 , sizeof(struct ota_data));
data.header.frame_flag = 0x5A5A;
memcpy(data.frame.frame_data,"aiit_ota_start",strlen("aiit_ota_start"));
data.frame.frame_len = strlen("aiit_ota_start");
printf("send start signal.\n");
ret = send(fd, &data, sizeof(data), MSG_NOSIGNAL);
if (ret > 0){
printf("send %s[%d] Bytes\n",data.frame.frame_data,ret);
}
memset(buf, 0, 32);
length = recv(fd, buf, sizeof(buf), 0);
if (length <= 0)
{
continue;
}
else
{
printf("recv buf %s length %d\n",buf,length);
if(0 == strncmp(buf, "ready", length))
{
ret = ota_file_send(fd);
if (ret == 0) {
printf("ota file send successful.\n");
break;
} else {
/* ota failed then restart the ota process */
continue;
}
}
}
}
printf("exit fd = %d\n",fd);
close(fd);
pthread_exit(0);
}
/*******************************************************************************
* : server
* : TCP Server的服务函数
* :
* :
*******************************************************************************/
void server(void)
{
int i = 0;
printf("ota Server startup\n");
while(1)
{
struct sockaddr_in fromaddr;
socklen_t len = sizeof(fromaddr);
int fd = accept(serverfd,(struct sockaddr*)&fromaddr,&len);//调用accept进入堵塞状态等待客户端的连接
if (fd == -1)
{
printf("The client connection is wrong...\n");
continue;
}
for (i = 0;i < SIZE;i++)
{
if(clientfd[i] == 0)
{
//记录客户端的socket
clientfd[i] = fd;
//有客户端连接之后,启动线程给此客户服务
pthread_t tid;
pthread_create(&tid,0,server_thread,&fd);
break;
}
if (SIZE == i)
{
//发送给客户端聊天室满了
char* str = "Devices full";
printf("%s", str);
send(fd,str,strlen(str),0);
close(fd);
}
}
}
}
int main(void)
{
sockt_init();
server();
}