Modify the tcp server to send 1024 bytes each time

This commit is contained in:
wgzAIIT 2023-05-31 14:07:45 +08:00
parent c6af847c73
commit 938d46d5d9
5 changed files with 65 additions and 49 deletions

View File

@ -26,12 +26,6 @@
#include <stdio.h> #include <stdio.h>
#include <sys/types.h> #include <sys/types.h>
#if defined(CONNECTION_ADAPTER_4G) && defined(TOOL_USING_OTA)
#define ENTM_RECV_MAX 1024
#else
#define ENTM_RECV_MAX 256
#endif
#define REPLY_TIME_OUT 10 #define REPLY_TIME_OUT 10
enum ReceiveMode enum ReceiveMode
@ -76,6 +70,7 @@ struct ATAgent
#endif #endif
pthread_t at_handler; pthread_t at_handler;
#define ENTM_RECV_MAX 2048
char entm_recv_buf[ENTM_RECV_MAX]; char entm_recv_buf[ENTM_RECV_MAX];
uint32 entm_recv_len; uint32 entm_recv_len;
enum ReceiveMode receive_mode; enum ReceiveMode receive_mode;

View File

@ -67,7 +67,7 @@ CONFIG___STACKSIZE__=4096
# #
CONFIG_RESOURCES_SERIAL=y CONFIG_RESOURCES_SERIAL=y
CONFIG_SERIAL_USING_DMA=y CONFIG_SERIAL_USING_DMA=y
CONFIG_SERIAL_RB_BUFSZ=128 CONFIG_SERIAL_RB_BUFSZ=256
CONFIG_RESOURCES_PIN=y CONFIG_RESOURCES_PIN=y
# #

View File

@ -131,16 +131,18 @@ static uint32_t calculate_crc32(uint32_t addr, uint32_t len)
static uint16_t calculate_crc16(uint8_t * data, uint32_t len) static uint16_t calculate_crc16(uint8_t * data, uint32_t len)
{ {
uint16_t reg_crc=0xFFFF; uint16_t reg_crc=0xFFFF;
while(len--) { while(len--)
{
reg_crc ^= *data++; reg_crc ^= *data++;
for (int j=0;j<8;j++) { for(int j=0;j<8;j++)
{
if(reg_crc & 0x01) if(reg_crc & 0x01)
reg_crc=reg_crc >>1 ^ 0xA001; reg_crc=reg_crc >>1 ^ 0xA001;
else else
reg_crc=reg_crc >>1; reg_crc=reg_crc >>1;
} }
} }
printf(" crc = [0x%x]\n",reg_crc); printf("crc = [0x%x]\n",reg_crc);
return reg_crc; return reg_crc;
} }
@ -490,7 +492,8 @@ static int ota_data_recv(struct Adapter* adapter)
int try_times = 10; int try_times = 10;
int frame_cnt = 0; int frame_cnt = 0;
while(1) { while(1)
{
memset(&recv_msg, 0, sizeof(struct ota_data)); memset(&recv_msg, 0, sizeof(struct ota_data));
ret = AdapterDeviceRecv(adapter, &recv_msg, sizeof(struct ota_data)); ret = AdapterDeviceRecv(adapter, &recv_msg, sizeof(struct ota_data));
if(ret >= 0 && recv_msg.header.frame_flag == 0x5A5A) if(ret >= 0 && recv_msg.header.frame_flag == 0x5A5A)
@ -506,14 +509,13 @@ static int ota_data_recv(struct Adapter* adapter)
printf("total [%d]frames [%d]Bytes crc[%x],receive successful,\n",frame_cnt,recv_msg.header.total_len,recv_msg.frame.crc); 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); memset(reply, 0, 16);
memcpy(reply, "ok", strlen("ok")); memcpy(reply, "ok", strlen("ok"));
AdapterDeviceSend(adapter, reply, strlen(reply)); AdapterDeviceSend(adapter, reply, strlen(reply));
ret = 0; ret = 0;
break; break;
} }
frame_cnt = recv_msg.frame.frame_id; frame_cnt = recv_msg.frame.frame_id;
if (recv_msg.frame.crc == calculate_crc16(recv_msg.frame.frame_data,recv_msg.frame.frame_len)) 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); printf("current [%d] frame,length[%d] Bytes.\n",frame_cnt,recv_msg.frame.frame_len);
/*写入flash待实现*/ /*写入flash待实现*/
@ -529,7 +531,8 @@ send_ok_again:
memcpy(reply, "ok", strlen("ok")); memcpy(reply, "ok", strlen("ok"));
ret = AdapterDeviceSend(adapter, reply, strlen(reply)); ret = AdapterDeviceSend(adapter, reply, strlen(reply));
if(ret < 0){ if(ret < 0)
{
printf("send ok failed.\n"); printf("send ok failed.\n");
goto send_ok_again; goto send_ok_again;
} }
@ -558,7 +561,8 @@ try_again:
} }
} }
if(0 == ret) { if(0 == ret)
{
printf("ota file done,start application.\n"); printf("ota file done,start application.\n");
//传输完成需要干什么; //传输完成需要干什么;
} }
@ -593,12 +597,12 @@ void app_ota_by_4g(void)
while(1) while(1)
{ {
memset(&recv_msg, 0, sizeof(struct ota_data)); memset(&recv_msg, 0, sizeof(struct ota_data));
/* step1: Confirm the start signal of transmission*/ /* step1:Confirm the start signal of transmission. */
printf("waiting for start msg...\n"); printf("waiting for start msg...\n");
ret = AdapterDeviceRecv(adapter, &recv_msg, sizeof(struct ota_data)); ret = AdapterDeviceRecv(adapter, &recv_msg, sizeof(struct ota_data));
if(ret >= 0 && recv_msg.header.frame_flag == 0x5A5A) if(ret >= 0 && recv_msg.header.frame_flag == 0x5A5A)
{ {
if (0 == strncmp("aiit_ota_start",recv_msg.frame.frame_data, strlen("aiit_ota_start"))) if(0 == strncmp("aiit_ota_start",recv_msg.frame.frame_data, strlen("aiit_ota_start")))
{ {
memset(reply, 0, 16); memset(reply, 0, 16);
memcpy(reply, "ready", strlen("ready")); memcpy(reply, "ready", strlen("ready"));
@ -610,10 +614,10 @@ send_ready_again:
goto send_ready_again; goto send_ready_again;
} }
printf("start receive ota file.\n"); printf("start receive ota file.\n");
/* step2: start receive bin file,first wait for 5s*/ /* step2:start receive bin file,first wait for 4s. */
PrivTaskDelay(5000); PrivTaskDelay(4000);
ret = ota_data_recv(adapter); ret = ota_data_recv(adapter);
if (0 != ret) if(0 != ret)
{ {
memset(reply, 0, 16); memset(reply, 0, 16);
memcpy(reply, "ota_restart", strlen("ota_restart")); memcpy(reply, "ota_restart", strlen("ota_restart"));

View File

@ -25,7 +25,7 @@
#define JUMP_FAILED_FLAG 0XABABABAB #define JUMP_FAILED_FLAG 0XABABABAB
#define JUMP_SUCCESS_FLAG 0XCDCDCDCD #define JUMP_SUCCESS_FLAG 0XCDCDCDCD
#define LENGTH 256 //每帧数据的数据包长度 #define LENGTH 1024 //每帧数据的数据包长度
typedef enum { typedef enum {
OTA_STATUS_IDLE = 0, // 空闲状态,没有进行OTA升级 OTA_STATUS_IDLE = 0, // 空闲状态,没有进行OTA升级

View File

@ -38,9 +38,9 @@
#include <ifaddrs.h> #include <ifaddrs.h>
#define PORT 7777 //socket端口号 #define PORT 7777 //socket端口号
#define SIZE 100 //socket链接限制为100 #define SIZE 100 //socket链接限制为100
#define LENGTH 256 //每帧数据的数据包长度 #define LENGTH 1024 //每帧数据的数据包长度
#define BIN_PATH "/home/aep05/wgz/XiZi-xidatong-arm32-app.bin" //bin包的路径 #define BIN_PATH "/home/aep05/wgz/XiZi-xidatong-arm32-app.bin" //bin包的路径
struct ota_header_t struct ota_header_t
@ -79,16 +79,18 @@ static int clientfd[SIZE] = {0}; // 客户端的socketfd,100个元素clientfd
static uint16_t calculate_crc16(uint8_t * data, uint32_t len) static uint16_t calculate_crc16(uint8_t * data, uint32_t len)
{ {
uint16_t reg_crc=0xFFFF; uint16_t reg_crc=0xFFFF;
while(len--) { while(len--)
{
reg_crc ^= *data++; reg_crc ^= *data++;
for (int j=0;j<8;j++) { for(int j=0;j<8;j++)
{
if(reg_crc & 0x01) if(reg_crc & 0x01)
reg_crc=reg_crc >>1 ^ 0xA001; reg_crc=reg_crc >>1 ^ 0xA001;
else else
reg_crc=reg_crc >>1; reg_crc=reg_crc >>1;
} }
} }
printf(" crc = [0x%x]\n",reg_crc); printf("crc = [0x%x]\n",reg_crc);
return reg_crc; return reg_crc;
} }
@ -108,7 +110,7 @@ void sockt_init(void)
serverfd = socket(AF_INET,SOCK_STREAM,0); serverfd = socket(AF_INET,SOCK_STREAM,0);
if (serverfd == -1) if(serverfd == -1)
{ {
perror("创建socket失败"); perror("创建socket失败");
exit(-1); exit(-1);
@ -121,8 +123,10 @@ void sockt_init(void)
/*显示当前TCP server的*/ /*显示当前TCP server的*/
getifaddrs(&ifap); getifaddrs(&ifap);
for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) { for(ifa = ifap; ifa != NULL; ifa = ifa->ifa_next)
if (ifa->ifa_addr->sa_family == AF_INET) { {
if(ifa->ifa_addr->sa_family == AF_INET)
{
sa = (struct sockaddr_in *) ifa->ifa_addr; sa = (struct sockaddr_in *) ifa->ifa_addr;
ipaddr = inet_ntoa(sa->sin_addr); ipaddr = inet_ntoa(sa->sin_addr);
printf("Interface: %s\tAddress: %s\n", ifa->ifa_name, ipaddr); printf("Interface: %s\tAddress: %s\n", ifa->ifa_name, ipaddr);
@ -140,14 +144,14 @@ void sockt_init(void)
exit(-1); exit(-1);
} }
if (bind(serverfd,(struct sockaddr*)&addr,sizeof(addr)) == -1) if(bind(serverfd,(struct sockaddr*)&addr,sizeof(addr)) == -1)
{ {
perror("绑定失败"); perror("绑定失败");
exit(-1); exit(-1);
} }
//监听最大连接数 //监听最大连接数
if (listen(serverfd,SIZE) == -1) if(listen(serverfd,SIZE) == -1)
{ {
perror("设置监听失败"); perror("设置监听失败");
exit(-1); exit(-1);
@ -177,13 +181,14 @@ void ota_start_signal(int fd)
printf("send start signal.\n"); printf("send start signal.\n");
ret = send(fd, &data, sizeof(data), MSG_NOSIGNAL); ret = send(fd, &data, sizeof(data), MSG_NOSIGNAL);
if (ret > 0){ if(ret > 0)
{
printf("send %s[%d] Bytes\n",data.frame.frame_data,ret); printf("send %s[%d] Bytes\n",data.frame.frame_data,ret);
} }
memset(buf, 0, 32); memset(buf, 0, 32);
length = recv(fd, buf, sizeof(buf), 0); length = recv(fd, buf, sizeof(buf), 0);
if (length > 0 && (0 == strncmp(buf, "ready", length))) if(length > 0 && (0 == strncmp(buf, "ready", length)))
{ {
printf("recv buf %s length %d\n",buf,length); printf("recv buf %s length %d\n",buf,length);
break; break;
@ -219,17 +224,21 @@ int ota_file_send(int fd)
int file_length = 0; int file_length = 0;
char * file_buf = NULL; char * file_buf = NULL;
if (stat(BIN_PATH, &st) == 0) { if(stat(BIN_PATH, &st) == 0)
{
//获取文件大小(以字节为单位) //获取文件大小(以字节为单位)
file_frame_cnt = (st.st_size%LENGTH != 0)? (st.st_size/LENGTH + 1):(st.st_size/LENGTH); file_frame_cnt = (st.st_size%LENGTH != 0)? (st.st_size/LENGTH + 1):(st.st_size/LENGTH);
printf("File size is %ld bytes,file frame count is %d!\n", st.st_size, file_frame_cnt); printf("File size is %ld bytes,file frame count is %d!\n", st.st_size, file_frame_cnt);
}else{ }
else
{
printf("get file size failed.\n"); printf("get file size failed.\n");
return -1; return -1;
} }
file_fd = fopen(BIN_PATH, "r"); file_fd = fopen(BIN_PATH, "r");
if (NULL == file_fd){ if(NULL == file_fd)
{
printf("open file failed.\n"); printf("open file failed.\n");
return -1; return -1;
} }
@ -255,7 +264,8 @@ int ota_file_send(int fd)
send_again: send_again:
printf("ota send current[%d] frame.\n",frame_cnt); printf("ota send current[%d] frame.\n",frame_cnt);
length = send(fd, &data, sizeof(data), MSG_NOSIGNAL); length = send(fd, &data, sizeof(data), MSG_NOSIGNAL);
if(length < 0){ if(length < 0)
{
printf("send [%d] frame faile.go to send again\n",frame_cnt); printf("send [%d] frame faile.go to send again\n",frame_cnt);
goto send_again; goto send_again;
} }
@ -263,7 +273,8 @@ send_again:
recv_again: recv_again:
memset(buf, 0, 32); memset(buf, 0, 32);
length = recv(fd, buf, sizeof(buf), 0); length = recv(fd, buf, sizeof(buf), 0);
if(length < 0 ){ if(length < 0 )
{
printf("[%d] frame waiting for ok timeout,receive again.\n",frame_cnt); printf("[%d] frame waiting for ok timeout,receive again.\n",frame_cnt);
goto recv_again; goto recv_again;
} }
@ -294,7 +305,7 @@ recv_again:
} }
/* finally,crc check total bin file.*/ /* finally,crc check total bin file.*/
if (ret == 0) if(ret == 0)
{ {
printf("total send file length[%d] Bytes [%d] frames.\n",file_length,frame_cnt); printf("total send file length[%d] Bytes [%d] frames.\n",file_length,frame_cnt);
printf("now crc check total bin file.\n"); printf("now crc check total bin file.\n");
@ -305,14 +316,16 @@ recv_again:
data.header.frame_flag = 0x5A5A; data.header.frame_flag = 0x5A5A;
file_fd = fopen(BIN_PATH, "r"); file_fd = fopen(BIN_PATH, "r");
if (NULL == file_fd){ if(NULL == file_fd)
{
printf("open file failed.\n"); printf("open file failed.\n");
return -1; return -1;
} }
fseek(file_fd, 0, SEEK_SET); fseek(file_fd, 0, SEEK_SET);
length = fread(file_buf,1, file_length, file_fd); length = fread(file_buf,1, file_length, file_fd);
printf("read file length = %d\n",length); printf("read file length = %d\n",length);
if(length > 0) { if(length > 0)
{
data.frame.frame_id = frame_cnt; data.frame.frame_id = frame_cnt;
data.header.total_len = file_length; data.header.total_len = file_length;
data.frame.frame_len = strlen("aiit_ota_end"); data.frame.frame_len = strlen("aiit_ota_end");
@ -323,7 +336,8 @@ recv_again:
send_end_signal: send_end_signal:
printf("send aiit_ota_end signal.\n"); printf("send aiit_ota_end signal.\n");
length = send(fd, &data, sizeof(data), MSG_NOSIGNAL); length = send(fd, &data, sizeof(data), MSG_NOSIGNAL);
if(length < 0){ if(length < 0)
{
printf("send end signal faile,send end signal again\n"); printf("send end signal faile,send end signal again\n");
goto send_end_signal; goto send_end_signal;
} }
@ -374,16 +388,19 @@ void* server_thread(void* p)
int length = 0; int length = 0;
printf("pthread = %d\n",fd); printf("pthread = %d\n",fd);
sleep(5); sleep(3);
ota_start_signal(fd); ota_start_signal(fd);
sleep(5); sleep(5);
while(1) while(1)
{ {
ret = ota_file_send(fd); ret = ota_file_send(fd);
if (ret == 0) { if(ret == 0)
{
printf("ota file send successful.\n"); printf("ota file send successful.\n");
break; break;
} else { }
else
{
/* ota failed then restart the ota process */ /* ota failed then restart the ota process */
continue; continue;
} }
@ -410,13 +427,13 @@ void server(void)
socklen_t len = sizeof(fromaddr); socklen_t len = sizeof(fromaddr);
int fd = accept(serverfd,(struct sockaddr*)&fromaddr,&len);//调用accept进入堵塞状态等待客户端的连接 int fd = accept(serverfd,(struct sockaddr*)&fromaddr,&len);//调用accept进入堵塞状态等待客户端的连接
if (fd == -1) if(fd == -1)
{ {
printf("The client connection is wrong...\n"); printf("The client connection is wrong...\n");
continue; continue;
} }
for (i = 0;i < SIZE;i++) for(i = 0;i < SIZE;i++)
{ {
if(clientfd[i] == 0) if(clientfd[i] == 0)
{ {
@ -429,7 +446,7 @@ void server(void)
break; break;
} }
if (SIZE == i) if(SIZE == i)
{ {
//发送给客户端聊天室满了 //发送给客户端聊天室满了
char* str = "Devices full"; char* str = "Devices full";