📄 nettask.c
字号:
void tcpServerTask(void* arg);
void netCltCloseAll(void);
unsigned char net_linkstatus();
void netchecklink(void* p);
void cltnet(void* p);
static unsigned char net_send(int newFd,unsigned char packagetype);
extern unsigned32 semNetLink; /*网络连接出错信号灯*/
/**************************************
* 写电子盘写入文約shilei.txt
*
* **********************************************/
int write_il(void)
{
FILE * fp;
fp = fopen("/tffs0/il.txt","wb+");
if( fp!= NULL )
{
fwrite(net_grx.uRx.sMsg.uNetMsg.Msg.MsgData.c.s4,1,4096, fp);
Flag_Chgil = 1;
fclose(fp);
return 1;
}
else
{
printf("file open failed !\n");
return 0;
}
}
/**********************************************************************
void netTask();
函数描述: 客户端初始化,建立到服务器的通讯通道
参数: 无
返回: 无
调用:
static void net_applayer(void);
static void net_writesocket(void);
static void net_writesocket(void);
被调用:
main();
**********************************************************************/
unsigned32 tid;
unsigned32 my_priority;
OS_STATUS sc;
const unsigned char buf1[4] = {0x00,0x00,0x10,0x00};//TIMEOUT buff
signed int bytetorecv;
unsigned char tcpdatar[NET_RX_LEN];
signed int recnum;
unsigned short inc;
unsigned char state_tcprecv = STATE_INITIAL_DC;
unsigned char len ;
unsigned char infonum = 0;
unsigned char applength;
unsigned char result;
typedef enum State
{
ST_ETH_LINK, //state of link down or link up without TCP connection
ST_TCP_LISTEN, // TCP connected
ST_TCP_IDLE //TCP idle state,ready to receive or send
}ww;
ww tcp_conn_state = ST_ETH_LINK;
int sFd;
int newFd;
void netTask(void* p)
{
while(1)
{
struct sockaddr_in serverAddr ,farAddr;
bzero((char*)&serverAddr,sizeof(serverAddr));
int sockAddrSize;
switch(tcp_conn_state)
{
case ST_ETH_LINK:
tcp_conn_state = ST_TCP_LISTEN;
break;
case ST_TCP_LISTEN:
{
if((sFd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
return;
}
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons (TCP_PORT);
serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
sockAddrSize = sizeof(struct sockaddr_in);
/*绑定服务器*/
if (bind (sFd, (struct sockaddr *)&serverAddr,sizeof(serverAddr)) < 0)
{
perror("bind");
close(sFd);
return;
}
if (listen (sFd, MAX_CONNECTIONS) < 0)
{
perror("listen error\n");
tcp_conn_state = ST_ETH_LINK;
close(sFd);
return;
}
//此处循环接受连接请求
for(; ;) {
sockAddrSize = sizeof(farAddr);
if((newFd = accept(sFd, (struct sockaddr *)&farAddr, &sockAddrSize)) < 0)
{
net_errcount.Connectfail++;
tcp_conn_state = ST_TCP_LISTEN;
OSSemGive(semNetLink);
return;
}
else
{
printf("ACCEPTED:%s\n", inet_ntoa(farAddr.sin_addr));
tcp_conn_state = ST_TCP_IDLE;
}
break;
}
case ST_TCP_IDLE:
{
taskSpawn("tcpServerTask",95,0,8*1024,tcpServerTask,newFd,0,0,0,0,0,0,0,0,0);
OSTaskDelay(1000);
}//end of else
}
}
}
}
/*********************************************************************************************************
//启动网络发送任务
************************************************************************************************************/
void tcpServerTask(void* arg)
{
int fd = (int)arg;
int n;
char status= STATUS_NORMAL;
int bytetorecv;
unsigned char buff[6];
int i;
len = 0;
unsigned char *pBuff=NULL;
SRx * prx=NULL;
STATUS sc;
//循环接收
while(1)
{
int taskid;
//接收前tcpheader中的数据
memset(tcpdatar, 0x00, NET_RX_LEN);
if(n=recv(fd, tcpdatar,NET_RX_LEN,0)<=0)
{
taskid=taskNameToId(TNAME_SERVERTASK);
if(taskid!= ERROR && taskid != taskIdSelf());
taskDelete(taskid);
close (sFd);
close(newFd);
taskid=taskIdSelf();
tcp_conn_state = ST_ETH_LINK;
sc = taskDelete(0);
if (sc != OK) {
printf("Can't delete task \n");
return;
}
}
else
{
//开始状态机
for(inc = 0; inc < 4; inc ++)
{
switch(state_tcprecv)
{
case STATE_INITIAL_DC:
{
if(tcpdatar[inc] == ASCII_CMD1)
{
state_tcprecv = STATE_CMD1_DC;
}
break;
}
case STATE_CMD1_DC:
{
if(tcpdatar[inc] == ASCII_CMD2)
{
state_tcprecv = STATE_CMD2_DC;
}
else if(tcpdatar[inc] == ASCII_CMD1)
{
}
else
{
state_tcprecv = STATE_INITIAL_DC;
}
break;
}
case STATE_CMD2_DC:
{
int i=0;
/*计算需要缓冲的总长度*/
//bytetorecv 仅仅包括消息体本身的长度
bytetorecv = ((tcpdatar[2]*0x1000000)&0xFF000000)+((tcpdatar[3]*0x10000)&0xFF0000)
+((tcpdatar[4]*0x100)&0xFF00)+tcpdatar[5];
printf("the length of messsge body is %d byte \n",bytetorecv);
//接收数据之前清除其数据
memset(net_grx.uRx.u8RxDataBuf, 0x00, NET_RX_LEN);
memcpy(net_grx.uRx.u8RxDataBuf,tcpdatar,NET_RX_LEN);
if (inc==2)
{
//如果正确拷贝,开始扫描消息体
state_tcprecv = STATE_READY_DC;
}
break;
}
case STATE_READY_DC:
{
// 消息处理
net_applayer(fd);
state_tcprecv = STATE_INITIAL_DC;
break;
}
default:
{
state_tcprecv = STATE_INITIAL_DC;
break;
}
}//end switch
}//end for
OSTaskDelay(500);
}//end while
}
}
/**********************************************************************
void netCltCheckLink()
函数说明: 检测网络是否正常,如果不正常,关闭客户端
参数: 无
返回: 无
注意:该任务的优先级较高
**********************************************************************/
void netchecklink(void* p)
{
int taskId;
/*创建计数信号量,并发起通过接收数据监视网络的任务*/
/*如果获得了信号量,表示网络已经断开*/
/*等待send()和recv()出错释放信号灯*/
semTake(semNetLink,WAIT_FOREVER);
/*获得信号灯,表示网络连接断开*/
semDelete(semNetLink);
taskSpawn("netCltCloseAll",102,0, 1024,netCltCloseAll,0,0,0,0,0,0,0,0,0,0);
}
/**********************************************************************
void netCltCloseAll()
函数描述: 客户端退出,结束socket 通讯。
参数: 无
返回: 无
调用: 无
被调用: void netCltCheckLink();
**********************************************************************/
void netCltCloseAll()
{
int taskId;
/*关闭客户端通讯socket*/
/*删除监控任务*/
logMsg("geNetDataCloseAll: socket closed\n",0,0,0,0,0,0);
/*删除网络相关任务*/
taskId = taskNameToId(TNAME_NETSERVER);
if(taskId!=taskIdSelf() && taskId!=ERROR)
{
taskDelete(taskId);
}
taskId = taskNameToId(TNAME_NETCHECKLINK);
if(taskId!=taskIdSelf() && taskId!=ERROR)
{
taskDelete(taskId);
}
close(sFd);
close(newFd);
tcp_conn_state = ST_ETH_LINK;
/*重新初始化网络*/
taskDelete(taskNameToId(TNAME_NETTASK));
taskId =taskIdSelf();
taskDelete(taskId);
taskSpawn("nettask",105,0,8*1024,
(FUNCPTR)netTask,0,0,0,0,0,0,0,0,0,0);
}
/**********************************************************************
void net_send()
函数说明:
参数: 无
返回: 无
调用: 无
被调用:
**********************************************************************/
static unsigned char net_send(int newFd,unsigned char packagetype)
{
//clear the tx buffer and tx length
//前12字节用来控制用
int length;
//int qq;
memset(net_gtx.uTx.u8TxDataBuf,0x0,NET_TX_LEN);//clear tx buf
if(packagetype ==BW_COMMAND_REQ)//send health report
{
net_gtx.uTx.sMsg.sTcpHeader.u8Cmd1 = ASCII_CMD1;
net_gtx.uTx.sMsg.sTcpHeader.u8Cmd2 = ASCII_CMD2;
net_gtx.uTx.sMsg.sTcpHeader.u8Len1 = 0;
net_gtx.uTx.sMsg.sTcpHeader.u8Len2 = 0;
net_gtx.uTx.sMsg.sTcpHeader.u8Len3 = 0;
net_gtx.uTx.sMsg.sTcpHeader.u8Len4 = (char)(0x11);
length = ((net_gtx.uTx.sMsg.sTcpHeader.u8Len1*0x1000000)&0xFF000000)
+((net_gtx.uTx.sMsg.sTcpHeader.u8Len2*0x10000)&0xFF0000)
+((net_gtx.uTx.sMsg.sTcpHeader.u8Len3*0x100)&0xFF00)
+net_gtx.uTx.sMsg.sTcpHeader.u8Len4;
net_gtx.uTx.sMsg.uNetMsg.Msg.MsgData.bwcmd.CmdId=CPO2[6]; //若为1表示上电,否则为下电
net_gtx.uTx.sMsg.uNetMsg.Msg.u8PktNum = BW_COMMAND_PKTNUM ;
net_gtx.uTx.sMsg.uNetMsg.Msg.u8FormatNum = BW_COMMAND_REQ_FMTNUM ;
net_gtx.uTx.sMsg.uNetMsg.Msg.u8FormatVer = BW_COMMAND_FMTVER;
}
/* #define BW_INFOR_PKTNUM 156
#define BW_INFOR_REQ_FMTNUM 0x02
#define BW_DATA_INFOR_FMTNUM 0x02
#define BW_STATE_INFOR_FMTNUM 0x02
#define BW_INFOR_FMTVER 0x05*/
if(packagetype ==BW_INFOR_REQ) //
{
net_gtx.uTx.sMsg.sTcpHeader.u8Cmd1 = ASCII_CMD1;
net_gtx.uTx.sMsg.sTcpHeader.u8Cmd2 = ASCII_CMD2;
net_gtx.uTx.sMsg.sTcpHeader.u8Len1 = 0;
net_gtx.uTx.sMsg.sTcpHeader.u8Len2 = 0;
net_gtx.uTx.sMsg.sTcpHeader.u8Len3 = 0;
net_gtx.uTx.sMsg.sTcpHeader.u8Len4 = (char)(0x11);
length = ((net_gtx.uTx.sMsg.sTcpHeader.u8Len1*0x1000000)&0xFF000000)
+((net_gtx.uTx.sMsg.sTcpHeader.u8Len2*0x10000)&0xFF0000)
+((net_gtx.uTx.sMsg.sTcpHeader.u8Len3*0x100)&0xFF00)
+net_gtx.uTx.sMsg.sTcpHeader.u8Len4;
net_gtx.uTx.sMsg.uNetMsg.Msg.u8PktNum = BW_INFOR_PKTNUM ;
net_gtx.uTx.sMsg.uNetMsg.Msg.u8FormatNum = BW_INFOR_REQ_FMTNUM ;
net_gtx.uTx.sMsg.uNetMsg.Msg.u8FormatVer = BW_INFOR_FMTVER;
}
if(packagetype == HEALTH_REPORT_ACK)//send health report
{
net_gtx.uTx.sMsg.sTcpHeader.u8Cmd1 = ASCII_CMD1;
net_gtx.uTx.sMsg.sTcpHeader.u8Cmd2 = ASCII_CMD2;
net_gtx.uTx.sMsg.sTcpHeader.u8Len1 = 0;
net_gtx.uTx.sMsg.sTcpHeader.u8Len2 = 0;
net_gtx.uTx.sMsg.sTcpHeader.u8Len3 = 0;
net_gtx.uTx.sMsg.sTcpHeader.u8Len4 = (char)(0x11);
length = ((net_gtx.uTx.sMsg.sTcpHeader.u8Len1*0x1000000)&0xFF000000)
+((net_gtx.uTx.sMsg.sTcpHeader.u8Len2*0x10000)&0xFF0000)
+((net_gtx.uTx.sMsg.sTcpHeader.u8Len3*0x100)&0xFF00)
+net_gtx.uTx.sMsg.sTcpHeader.u8Len4;
net_gtx.uTx.sMsg.uNetMsg.Msg.MsgData.e.u8DescriptText[0]='n';
net_gtx.uTx.sMsg.uNetMsg.Msg.MsgData.e.u8DescriptText[1]='e';
net_gtx.uTx.sMsg.uNetMsg.Msg.MsgData.e.u8DescriptText[2]='t';
net_gtx.uTx.sMsg.uNetMsg.Msg.MsgData.e.u8DescriptText[3]='o';
net_gtx.uTx.sMsg.uNetMsg.Msg.MsgData.e.u8DescriptText[4]='k';
net_gtx.uTx.sMsg.uNetMsg.Msg.u8PktNum = HEALTH_REPORT_PKTNUM ;
net_gtx.uTx.sMsg.uNetMsg.Msg.u8FormatNum = HEALTH_REPORT_ACK_FMTNUM ;
net_gtx.uTx.sMsg.uNetMsg.Msg.u8FormatVer = HEALTH_REPORT_FMTVER;
}
else if(packagetype == DC_SLOWDATA_INFOR)
{
net_gtx.uTx.sMsg.sTcpHeader.u8Cmd1 = ASCII_CMD1;
net_gtx.uTx.sMsg.sTcpHeader.u8Cmd2 = ASCII_CMD2;
net_gtx.uTx.sMsg.sTcpHeader.u8Len1 = 0;
net_gtx.uTx.sMsg.sTcpHeader.u8Len2 = 0;
net_gtx.uTx.sMsg.sTcpHeader.u8Len3 = 0;
net_gtx.uTx.sMsg.sTcpHeader.u8Len4 = (char)(0x1C);
length = ((net_gtx.uTx.sMsg.sTcpHeader.u8Len1*0x1000000)&0xFF000000)
+((net_gtx.uTx.sMsg.sTcpHeader.u8Len2*0x10000)&0xFF0000)
+((net_gtx.uTx.sMsg.sTcpHeader.u8Len3*0x100)&0xFF00)
+net_gtx.uTx.sMsg.sTcpHeader.u8Len4;
// memcpy(net_gtx.uTx.sMsg.uNetMsg.Msg.MsgData.k.slowdata,databuf,12);
net_gtx.uTx.sMsg.uNetMsg.Msg.MsgData.k.slowdata[0]=databuf[0];
net_gtx.uTx.sMsg.uNetMsg.Msg.MsgData.k.slowdata[1]=databuf[3];
net_gtx.uTx.sMsg.uNetMsg.Msg.MsgData.k.slowdata[2]=databuf[4];
net_gtx.uTx.sMsg.uNetMsg.Msg.MsgData.k.slowdata[3]=current;
//net_gtx.uTx.sMsg.uNetMsg.Msg.MsgData.k.slowdata[3]=4.966;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -