forked from xuos/xiuos
modify ota function dir
This commit is contained in:
7
APP_Framework/Applications/ota/Kconfig
Executable file → Normal file
7
APP_Framework/Applications/ota/Kconfig
Executable file → Normal file
@@ -1,7 +0,0 @@
|
||||
menu "ota app "
|
||||
menuconfig APPLICATION_OTA
|
||||
bool "Using app bin ota"
|
||||
default n
|
||||
|
||||
|
||||
endmenu
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
SRC_FILES := ota.c
|
||||
|
||||
include $(KERNEL_ROOT)/compiler.mk
|
||||
include $(KERNEL_ROOT)/compiler.mk
|
||||
@@ -1,19 +0,0 @@
|
||||
# OTA README
|
||||
|
||||
xiuos当前的ota功能允许应用bin文件可以通过4G实现远程的bin文件更新(限制:1、bin文件存放在设备SD卡并且应用从SD卡启动;2、暂且支持4G实现;3、暂时只支持aiit终端;4、只支持xiuos内核)。
|
||||
|
||||
## 文件说明
|
||||
|
||||
| 名称 | 说明 |
|
||||
| -- | -- |
|
||||
| ota.c| xiuos设备OTA代码 |
|
||||
| ota_server.c | pc服务端的实例代码供参考 |
|
||||
|
||||
|
||||
## 使用说明
|
||||
xiuos的应用bin文件更新依赖上层的adapter框架,因此需要在menuconfig同时配置以下选项:
|
||||
1、ota开关APPLICATION_OTA打开;
|
||||
2、adapter的4G功能开关;
|
||||
3、拆分的应用启动SEPARATE_COMPILE开关从SD卡启动的配置开关APP_STARTUP_FROM_SDCARD。
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
101
APP_Framework/Applications/ota/ota.h
Normal file
101
APP_Framework/Applications/ota/ota.h
Normal file
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
* 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: 1.0
|
||||
* @author: AIIT XUOS Lab
|
||||
* @date: 2023/4/23
|
||||
*
|
||||
*/
|
||||
#ifndef __OTA_DEF_H__
|
||||
#define __OTA_DEF_H__
|
||||
|
||||
#include "flash_ops.h"
|
||||
|
||||
#define JUMP_FAILED_FLAG 0XABABABAB
|
||||
#define JUMP_SUCCESS_FLAG 0XCDCDCDCD
|
||||
#define STARTFLAG 0x1A2B //数据帧开始信号标记
|
||||
#define DATAFLAG 0x3C4D //数据帧数据信号标记
|
||||
#define ENDTFLAG 0x5E6F //数据帧结束信号标记
|
||||
#define LENGTH 1024 //每帧数据的数据包长度
|
||||
|
||||
typedef enum {
|
||||
OTA_STATUS_IDLE = 0, // 空闲状态,没有进行OTA升级
|
||||
OTA_STATUS_READY, // 准备状态,可以进行OTA升级
|
||||
OTA_STATUS_DOWNLOADING, // 正在下载固件
|
||||
OTA_STATUS_DOWNLOADED, // 固件下载完成
|
||||
OTA_STATUS_UPDATING, // 正在进行OTA升级
|
||||
OTA_STATUS_BACKUP, // 正在版本回退
|
||||
OTA_STATUS_ERROR, // 出现错误,升级失败
|
||||
} ota_status_t;
|
||||
|
||||
|
||||
/* Flash分区中保存固件的属性描述 */
|
||||
typedef struct {
|
||||
uint32_t size; // 应用程序大小,记录分区固件的大小
|
||||
uint32_t crc32; // 应用程序CRC32校验值,记录分区固件的crc32值
|
||||
uint8_t version[32]; // 应用程序版本号,记录分区固件的版本
|
||||
uint8_t description[32]; // 固件的描述信息,最多32个字符
|
||||
} firmware_t;
|
||||
|
||||
|
||||
/* OTA升级过程中的信息结构体 */
|
||||
typedef struct {
|
||||
firmware_t os; // XiUOS System分区属性信息
|
||||
firmware_t bak; // Bakup分区属性信息
|
||||
firmware_t down; // Download分区属性信息
|
||||
uint32_t status; // 升级状态,取值来自于ota_status_t类型
|
||||
uint32_t lastjumpflag; // bootloaer跳转失败的标志,bootloader里置0xABABABAB,跳转成功后在应用里置0xCDCDCDCD
|
||||
uint8_t error_message[64]; // 错误信息,最多64个字符
|
||||
} ota_info_t;
|
||||
|
||||
|
||||
#ifdef OTA_BY_TCPSERVER
|
||||
/*bin包传输过程中的数据帧相关的结构体*/
|
||||
typedef struct
|
||||
{
|
||||
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
|
||||
} ota_header_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint32_t frame_id; // Current frame id
|
||||
uint8_t frame_data[LENGTH]; // Current frame data
|
||||
uint16_t frame_len; // Current frame data length
|
||||
uint16_t crc; // Current frame data crc
|
||||
} ota_frame_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
ota_header_t header;
|
||||
ota_frame_t frame;
|
||||
} ota_data;
|
||||
#endif
|
||||
|
||||
#ifdef OTA_BY_PLATFORM
|
||||
typedef struct{
|
||||
uint32_t size; //OTA固件大小
|
||||
uint32_t streamId; //OTA固件下载时ID编号
|
||||
uint32_t counter; //OTA总下载次数
|
||||
uint32_t num; //OTA当前下载次数
|
||||
uint32_t downlen; //OTA当前下载次数的下载量
|
||||
uint8_t version[32]; //OTA下载时存储版本号的缓存区
|
||||
}OTA_TCB;
|
||||
#endif
|
||||
|
||||
void app_clear_jumpflag(void);
|
||||
void ota_entry(void);
|
||||
#endif
|
||||
@@ -1,385 +0,0 @@
|
||||
/*
|
||||
* 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: 2021/11/3
|
||||
*
|
||||
*/
|
||||
|
||||
#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>
|
||||
|
||||
typedef int BOOL;
|
||||
#define true 1
|
||||
#define false 0
|
||||
|
||||
int serverfd;//服务器socket
|
||||
int clientfd[100000];//客户端的socketfd,100个元素,clientfd[0]~clientfd[99]
|
||||
int size = 99999;//用来控制进入聊天室的人数为50以内
|
||||
int PORT = 9898;//端口号
|
||||
typedef struct sockaddr meng;
|
||||
|
||||
struct ota_header_t
|
||||
{
|
||||
int16_t frame_flag; ///< frame start flag 2 Bytes
|
||||
uint8_t dev_type; ///< device type
|
||||
uint8_t burn_mode; ///< data burn way
|
||||
uint32_t total_len; ///< send data total length caculated from each frame_len
|
||||
uint32_t dev_hid; ///< device hardware version
|
||||
uint32_t dev_sid; ///< device software version
|
||||
char resv[8]; ///< reserve
|
||||
};
|
||||
|
||||
struct ota_frame_t
|
||||
{
|
||||
uint32_t frame_id; ///< Current frame id
|
||||
uint32_t frame_len; ///< Current frame data length
|
||||
char frame_data[64]; ///< Current frame data,max length 224
|
||||
uint32_t crc; ///< Current frame data crc
|
||||
};
|
||||
|
||||
struct ota_data
|
||||
{
|
||||
struct ota_header_t header;
|
||||
struct ota_frame_t frame;
|
||||
char end[4];
|
||||
};
|
||||
|
||||
pthread_t ota_ktask;
|
||||
|
||||
/**
|
||||
* @description: CRC16 check
|
||||
* @param data data buffer
|
||||
* @param length data length
|
||||
* @return check code
|
||||
*/
|
||||
uint32_t OtaCrc16(uint8_t * data, uint32_t length)
|
||||
{
|
||||
int j;
|
||||
unsigned int reg_crc=0xFFFF;
|
||||
|
||||
printf("crc data length[%d] Bytes,",length);
|
||||
|
||||
while (length--) {
|
||||
reg_crc ^= *data++;
|
||||
for (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;
|
||||
}
|
||||
|
||||
void init(void)
|
||||
{
|
||||
serverfd = socket(PF_INET,SOCK_STREAM,0);
|
||||
|
||||
if (serverfd == -1)
|
||||
{
|
||||
perror("创建socket失败");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
//为套接字设置ip协议 设置端口号 并自动获取本机ip转化为网络ip
|
||||
|
||||
struct sockaddr_in addr;//存储套接字的信息
|
||||
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时,表示从本机的任一网卡接收数据
|
||||
|
||||
//绑定套接字
|
||||
// int on = 1;
|
||||
struct timeval timeout;
|
||||
timeout.tv_sec = 5;
|
||||
timeout.tv_usec = 0;
|
||||
if(setsockopt(serverfd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(struct timeval)) < 0)
|
||||
{
|
||||
perror("端口设置失败");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
if (bind(serverfd,(meng*)&addr,sizeof(addr)) == -1)
|
||||
{
|
||||
perror("绑定失败");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
if (listen(serverfd,100) == -1)
|
||||
{//监听最大连接数
|
||||
perror("设置监听失败");
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
int OtaFileSend(int fd)
|
||||
{
|
||||
unsigned char buf[32] = { 0 };
|
||||
struct ota_data data;
|
||||
FILE *file_fd;
|
||||
char ch;
|
||||
int length = 0;
|
||||
int try_times = 10;
|
||||
int recv_end_times = 3;
|
||||
int ret = 0;
|
||||
int frame_cnt = 0;
|
||||
int file_length = 0;
|
||||
char * file_buf = NULL;
|
||||
|
||||
file_fd = fopen("/home/aep04/wwg/XiUOS_aiit-arm32-board_app.bin", "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, 64, 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 = OtaCrc16(data.frame.frame_data, length);
|
||||
file_length += length;
|
||||
}
|
||||
|
||||
send_again:
|
||||
usleep(50000);
|
||||
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;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
sleep(1);
|
||||
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("/home/aep04/wwg/XiUOS_aiit-arm32-board_app.bin", "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 = OtaCrc16(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;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
// sleep(1);
|
||||
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 = OtaFileSend(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);
|
||||
}
|
||||
|
||||
void server(void)
|
||||
{
|
||||
printf("ota Server startup\n");
|
||||
while(1)
|
||||
{
|
||||
struct sockaddr_in fromaddr;
|
||||
socklen_t len = sizeof(fromaddr);
|
||||
int fd = accept(serverfd,(meng*)&fromaddr,&len);
|
||||
|
||||
//调用accept进入堵塞状态,等待客户端的连接
|
||||
|
||||
if (fd == -1)
|
||||
{
|
||||
// printf("The client connection is wrong...\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
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)
|
||||
{
|
||||
init();
|
||||
server();
|
||||
}
|
||||
|
||||
508
APP_Framework/Applications/ota/server_tcp.c
Normal file
508
APP_Framework/Applications/ota/server_tcp.c
Normal file
@@ -0,0 +1,508 @@
|
||||
/*
|
||||
* 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 <libgen.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 STARTFLAG 0x1A2B //数据帧开始信号标记
|
||||
#define DATAFLAG 0x3C4D //数据帧数据信号标记
|
||||
#define ENDTFLAG 0x5E6F //数据帧结束信号标记
|
||||
#define PORT 7777 //socket端口号
|
||||
#define SIZE 100 //socket链接限制为100
|
||||
#define LENGTH 1024 //每帧数据的数据包长度
|
||||
#define BIN_PATH "/home/aep05/wgz/XiZi-xidatong-arm32-app.bin" //bin包的路径
|
||||
|
||||
typedef struct
|
||||
{
|
||||
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
|
||||
} ota_header_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint32_t frame_id; // Current frame id
|
||||
uint8_t frame_data[LENGTH]; // Current frame data
|
||||
uint16_t frame_len; // Current frame data length
|
||||
uint16_t crc; // Current frame data crc
|
||||
} ota_frame_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
ota_header_t header;
|
||||
ota_frame_t frame;
|
||||
} ota_data;
|
||||
|
||||
|
||||
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("Failed to create 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:%-16s Address:%-16s\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("Failed to set setsock option");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
if(bind(serverfd,(struct sockaddr*)&addr,sizeof(addr)) == -1)
|
||||
{
|
||||
perror("Failed to bind socket port");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
//监听最大连接数
|
||||
if(listen(serverfd,SIZE) == -1)
|
||||
{
|
||||
perror("Failed to set socket listening");
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
* 函 数 名: ota_start_signal
|
||||
* 功能描述: 发送开始信号,等待接收端回应ready
|
||||
* 形 参: fd:监听的客户端连接的fd
|
||||
* 返 回 值: 0:成功,-1:失败
|
||||
*******************************************************************************/
|
||||
void ota_start_signal(int fd)
|
||||
{
|
||||
ota_data data;
|
||||
struct stat st;
|
||||
uint8_t buf[32];
|
||||
int file_size = 0, file_frame_cnt = 0, length = 0;
|
||||
|
||||
if (access(BIN_PATH, F_OK) == 0)
|
||||
{
|
||||
printf("%s exists.\n", basename(BIN_PATH));
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("%s does not exist,please cheack!\n", BIN_PATH);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
//获取文件大小(以字节为单位)
|
||||
if(stat(BIN_PATH, &st) == 0)
|
||||
{
|
||||
file_size = st.st_size;
|
||||
file_frame_cnt = ((file_size%LENGTH) != 0)? (file_size/LENGTH + 1):(file_size/LENGTH);
|
||||
printf("%s size is %d bytes,frame count is %d!\n",basename(BIN_PATH), file_size, file_frame_cnt);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Failed to get file size\n");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
while(1)
|
||||
{
|
||||
memset(&data, 0x0, sizeof(ota_data));
|
||||
data.header.frame_flag = STARTFLAG;
|
||||
//发送起始帧时把bin文件的大小一并发送出去
|
||||
data.header.total_len = file_size;
|
||||
|
||||
while(send(fd, &data, sizeof(data), MSG_NOSIGNAL) <= 0);
|
||||
printf("send start signal to client %d.\n", fd);
|
||||
|
||||
memset(buf, 0, sizeof(buf));
|
||||
length = recv(fd, buf, sizeof(buf), 0);
|
||||
if(length == 0)
|
||||
{
|
||||
printf("The current socket %d is disconnected,please check it!\n",fd);
|
||||
close(fd);
|
||||
pthread_exit(0);
|
||||
}
|
||||
else if(length > 0 && (0 == strncmp(buf, "ready", length)))
|
||||
{
|
||||
printf("recv buf %s length %d from client %d.\n", buf, length, fd);
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
* 函 数 名: ota_file_send
|
||||
* 功能描述: 用于在TCP Server发送bin文件
|
||||
* 形 参: fd:监听的客户端连接的fd
|
||||
* 返 回 值: 发送成功返回0,失败返回-1
|
||||
*******************************************************************************/
|
||||
int ota_file_send(int fd)
|
||||
{
|
||||
unsigned char buf[32] = { 0 };
|
||||
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");
|
||||
fclose(file_fd);
|
||||
return -1;
|
||||
}
|
||||
fseek(file_fd, 0, SEEK_SET);
|
||||
printf("start send bin file to client %d.\n", fd);
|
||||
|
||||
|
||||
while(!feof(file_fd))
|
||||
{
|
||||
memset(&data, 0, sizeof(data));
|
||||
|
||||
data.header.frame_flag = DATAFLAG;
|
||||
length = fread(data.frame.frame_data, 1, LENGTH, file_fd);
|
||||
if(length == LENGTH)
|
||||
{
|
||||
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;
|
||||
}
|
||||
else if(length > 0 && length < LENGTH)
|
||||
{
|
||||
if(ferror(file_fd))
|
||||
{
|
||||
printf("read %s file error!\n", basename(BIN_PATH));
|
||||
ret = -1;
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
//fread返回值为0,此时是个空包,不需要再发送了否则是冗余数据
|
||||
else
|
||||
{
|
||||
printf("read %s file done!\n", basename(BIN_PATH));
|
||||
break;
|
||||
}
|
||||
|
||||
send_again:
|
||||
printf("send frame[%d] to client %d.\n", frame_cnt, fd);
|
||||
length = send(fd, &data, sizeof(data), MSG_NOSIGNAL);
|
||||
if(length < 0)
|
||||
{
|
||||
printf("send frame[%d] to client %d failed,send again\n", frame_cnt, fd);
|
||||
goto send_again;
|
||||
}
|
||||
|
||||
recv_again:
|
||||
memset(buf, 0, sizeof(buf));
|
||||
length = recv(fd, buf, sizeof(buf), 0);
|
||||
if(length == 0)
|
||||
{
|
||||
printf("current socket %d is disconnected,please check it!\n", fd);
|
||||
ret = -1;
|
||||
close(fd);
|
||||
pthread_exit(0);
|
||||
break;
|
||||
}
|
||||
else if(length < 0 )
|
||||
{
|
||||
printf("send frame[%d] to client %d waiting for ok timeout,receive again.\n", frame_cnt, fd);
|
||||
goto recv_again;
|
||||
}
|
||||
else if(0 == strncmp(buf, "ok", length))
|
||||
{
|
||||
printf("receive buf[%s] length %d from client %d.\n", buf, length, fd);
|
||||
try_times = 5;
|
||||
printf("send to client %d frame[%d] data send done.\n",fd, frame_cnt);
|
||||
frame_cnt++;
|
||||
continue;
|
||||
}
|
||||
|
||||
//接收到的回复不是ok,说明刚发的包有问题,需要再发一次
|
||||
else
|
||||
{
|
||||
if(try_times > 0)
|
||||
{
|
||||
try_times--;
|
||||
goto send_again;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("send to client %d frame[%d] 5 times failed.\n",fd, frame_cnt);
|
||||
ret = -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* finally,crc check total bin file.*/
|
||||
if(ret == 0)
|
||||
{
|
||||
printf("total send file length %d bytes, %d frames to client %d.\n", file_length, frame_cnt, fd);
|
||||
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 = ENDTFLAG;
|
||||
|
||||
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.crc = calculate_crc16(file_buf, length);
|
||||
}
|
||||
|
||||
send_end_signal:
|
||||
printf("send ota end signal to client %d.\n", fd);
|
||||
length = send(fd, &data, sizeof(data), MSG_NOSIGNAL);
|
||||
if(length < 0)
|
||||
{
|
||||
printf("send to client %d ota end signal faile,send again\n",fd);
|
||||
goto send_end_signal;
|
||||
}
|
||||
|
||||
recv_end_signal:
|
||||
memset(buf, 0, sizeof(buf));
|
||||
length = recv(fd, buf, sizeof(buf), 0);
|
||||
if(length == 0)
|
||||
{
|
||||
printf("current socket %d is disconnected,please check it!\n",fd);
|
||||
ret = -1;
|
||||
free(file_buf);
|
||||
fclose(file_fd);
|
||||
close(fd);
|
||||
pthread_exit(0);
|
||||
|
||||
}
|
||||
if(length < 0 || (0 != strncmp(buf, "ok", length)))
|
||||
{
|
||||
recv_end_times--;
|
||||
printf("from client %d end signal waiting for ok timeout,receive again.\n", fd);
|
||||
if(recv_end_times > 0)
|
||||
{
|
||||
goto recv_end_signal;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("client %d error end !!!\n", fd);
|
||||
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 };
|
||||
ota_data data;
|
||||
|
||||
printf("pthread = %d\n",fd);
|
||||
sleep(3);
|
||||
while(1)
|
||||
{
|
||||
/* if ota failed then restart the ota process */
|
||||
ota_start_signal(fd);
|
||||
sleep(5);
|
||||
if(0 == ota_file_send(fd))
|
||||
{
|
||||
printf("ota file send to client %d successful.\n", fd);
|
||||
break;
|
||||
}
|
||||
}
|
||||
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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user