235 lines
5.7 KiB
C
235 lines
5.7 KiB
C
|
|
/*
|
|
@STAY_FINAL_PROC
|
|
*/
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
版权所有 (C), 2001-2011, 上海全波通信技术有限公司
|
|
|
|
******************************************************************************
|
|
文 件 名 : swap_data.c
|
|
版 本 号 : 初稿
|
|
作 者 : 郭金涛
|
|
生成日期 : 2013年11月5日
|
|
最近修改 :
|
|
功能描述 : 实现数据导入导出交换功能,导出为二进制格式.
|
|
函数列表 :
|
|
修改历史 :
|
|
1.日 期 : 2013年11月5日
|
|
作 者 : 郭金涛
|
|
修改内容 : 创建文件
|
|
|
|
******************************************************************************/
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include "type.h"
|
|
#include "config.h"
|
|
|
|
#if USE_SWAP_DATA
|
|
//#include "stm32f4x7_eth.h"
|
|
//#include "stm32f4x7_eth_bsp.h"
|
|
#include "stm32f4xx_hal.h"
|
|
|
|
#include "sockets.h"
|
|
#include "os_task.h"
|
|
#include "tcp_task.h"
|
|
#include "cgifuncs.h"
|
|
#include "os_task.h"
|
|
#include "eeprom.h"
|
|
#include "lcd_task.h"
|
|
#include "i2c_drv.h"
|
|
#include "uart_user.h"
|
|
#include "tcp.h"
|
|
#include "delay.h"
|
|
#include "general_api.h"
|
|
#include "sys_config.h"
|
|
#include "swap_data.h"
|
|
|
|
static uint8 swap_rec_buf[SWAP_REC_BUF_MAX];
|
|
static uint8 swap_send_buf[SWAP_SEND_BUF_MAX];
|
|
|
|
|
|
static uint32 swap_frame_check(uint32 len)
|
|
{
|
|
S_SWAP_FRAME *pframe= (S_SWAP_FRAME*)swap_rec_buf;
|
|
|
|
|
|
if(len<SWAP_FRAME_MIN_LEN)
|
|
return 1;
|
|
|
|
if(pframe->header!=SWAP_FRAME_HEADER)
|
|
return 2;
|
|
|
|
if(pframe->im_ex>=2)
|
|
return 3;
|
|
|
|
if(pframe->std!=EXCITER_ISDBT) //需根据当前激励器类型进行更改
|
|
return 4;
|
|
|
|
if(swap_rec_buf[pframe->len+SWAP_FRAME_MIN_LEN-2]!=get_check_sum(swap_rec_buf,pframe->len+SWAP_FRAME_MIN_LEN-2))
|
|
return 5;
|
|
|
|
if(swap_rec_buf[pframe->len+SWAP_FRAME_MIN_LEN-1]!=SWAP_FRAME_TAIL)
|
|
return 6;
|
|
|
|
return 0;
|
|
}
|
|
|
|
/*****************************************************************************
|
|
函 数 名 : swap_ex_exciter_para
|
|
功能描述 : 激励器参数导出实现主函数
|
|
输入参数 : void
|
|
输出参数 : 无
|
|
返 回 值 : static
|
|
调用函数 :
|
|
被调函数 :
|
|
|
|
修改历史 :
|
|
1.日 期 : 2013年11月11日
|
|
作 者 : 郭金涛
|
|
修改内容 : 新生成函数
|
|
|
|
*****************************************************************************/
|
|
static uint32 swap_ex_exciter_para(void)
|
|
{
|
|
S_SWAP_FRAME *pframe = (S_SWAP_FRAME*)swap_rec_buf;
|
|
S_SWAP_FRAME *psend = (S_SWAP_FRAME*)swap_send_buf;
|
|
uint32 send_len = 0;
|
|
|
|
|
|
switch(pframe->cmd&0x7f)
|
|
{
|
|
case SWAP_FRAME_DATA_PARA:
|
|
psend->len = sizeof(S_SYSTEM_PARA);
|
|
psend->index = 0;
|
|
psend->total = 0;
|
|
memcpy(&psend->data[0],&atsc_para.system_para,sizeof(S_SYSTEM_PARA));
|
|
break;
|
|
case SWAP_FRAME_DATA_DPD:
|
|
break;
|
|
default:
|
|
return 0;
|
|
}
|
|
send_len = psend->len+SWAP_FRAME_MIN_LEN;
|
|
return send_len;
|
|
}
|
|
|
|
/*****************************************************************************
|
|
函 数 名 : swap_im_exciter_para
|
|
功能描述 : 激励器数据导入实现主函数
|
|
输入参数 : void
|
|
输出参数 : 无
|
|
返 回 值 : static
|
|
调用函数 :
|
|
被调函数 :
|
|
|
|
修改历史 :
|
|
1.日 期 : 2013年11月11日
|
|
作 者 : 郭金涛
|
|
修改内容 : 新生成函数
|
|
|
|
*****************************************************************************/
|
|
static uint32 swap_im_exciter_para(void)
|
|
{
|
|
return 0;
|
|
}
|
|
static uint32 swap_frame_decode(void)
|
|
{
|
|
uint32 send_len = 0;
|
|
S_SWAP_FRAME *pframe = (S_SWAP_FRAME*)swap_rec_buf;
|
|
|
|
if(pframe->im_ex==SWAP_FRAME_EXPORT)
|
|
{
|
|
send_len = swap_ex_exciter_para();
|
|
}
|
|
else
|
|
{
|
|
send_len = swap_im_exciter_para();
|
|
}
|
|
return send_len;
|
|
}
|
|
static uint32 swap_frame_process(uint32 rec_len)
|
|
{
|
|
uint32 ret = 0;
|
|
|
|
ret = swap_frame_check(rec_len);
|
|
if(ret!=0)
|
|
{
|
|
swap_printf("swap rec frame format err! code:%d\n",ret);
|
|
return 0;
|
|
}
|
|
return swap_frame_decode();
|
|
}
|
|
|
|
void swap_data_task(void * pvParameters)
|
|
{
|
|
struct sockaddr_in server_addr;
|
|
struct sockaddr_in conn_addr;
|
|
int sock_fd;
|
|
int sock_conn;
|
|
socklen_t addr_len;
|
|
int err;
|
|
int length;
|
|
task_set_ignore_chk(SWAP_TASK_ID);
|
|
|
|
(void)pvParameters;
|
|
vTaskDelay(15);
|
|
printf("%s init start...\n",__FUNCTION__);
|
|
|
|
|
|
sock_fd = lwip_socket(AF_INET, SOCK_STREAM, 0);
|
|
if (sock_fd == -1)
|
|
{
|
|
printf("%s failed to create swap_data sock_fd!\n",__FUNCTION__);
|
|
}
|
|
|
|
memset(&server_addr, 0, sizeof(server_addr));
|
|
server_addr.sin_family = AF_INET;
|
|
server_addr.sin_addr.s_addr =htonl(INADDR_ANY);
|
|
server_addr.sin_port = htons(SWAP_TCP_PORT);
|
|
|
|
err = lwip_bind(sock_fd, (struct sockaddr *)&server_addr, sizeof(server_addr));
|
|
if (err < 0)
|
|
{
|
|
printf("%s failed to bind sock_fd!\n",__FUNCTION__);
|
|
}
|
|
|
|
err = lwip_listen(sock_fd,1);
|
|
if (err < 0)
|
|
{
|
|
printf("%s failed to listen sock_fd!\n",__FUNCTION__);
|
|
}
|
|
|
|
addr_len = sizeof(struct sockaddr_in);
|
|
|
|
printf("%s init over.\n",__FUNCTION__);
|
|
|
|
while(1)
|
|
{
|
|
sock_conn = lwip_accept(sock_fd, (struct sockaddr *)&conn_addr, &addr_len);
|
|
while(sock_conn!=-1)
|
|
{
|
|
length = lwip_recv(sock_conn, swap_rec_buf, SWAP_REC_BUF_MAX, 0);
|
|
if(length)
|
|
{
|
|
length = swap_frame_process(length);
|
|
if(length)
|
|
{
|
|
lwip_send(sock_conn, swap_send_buf,length, 0);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
lwip_close(sock_conn);
|
|
sock_conn = -1;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#endif
|
|
|