⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 udpserver.c

📁 ucos移植到44b0
💻 C
📖 第 1 页 / 共 2 页
字号:
#ifndef  _UDP_SERVER_C
#define  _UDP_SERVER_C
#include "includes.h"
/*
 * Author hugang <hgxxx@51eda.com>
 */
#include "lwip/api.h"
#include "target.h"

#include <stdio.h>
#include <string.h>
#include "http.h"
#include "UdpServer.h"
//加密文件
#include "WjcDes.h"
extern char key[10][8];
extern char comkey[8];

#define MAX_RECV_BUFFER 500
_UC     recvBuff[MAX_RECV_BUFFER];
extern volatile int isrCount;
_US MessageProc(struct netconn *conn,struct CS_Msg_Struct * pMsg);
_US TempProc(struct netconn *conn,struct CS_Msg_Struct * pMsg);
_US DataProc(struct netconn *conn,struct CS_Msg_Struct * pMsg);
_US sendmessage(struct netconn *conn,struct CS_Msg_Struct * pMsg);
_US UserProc(struct netconn *conn,struct CS_Msg_Struct * pMsg);

extern struct UserInfo userdata[MAX_USER_NUM];
//udp当前用户
struct UserInfo gUser = {"","",0,USER_LOGIN_STATUS,0};
//数据采集显示允许标志
extern _UC  SampleFlag;
//extern void initData(void);
void udp_server_thread(void *arg)
{
	struct netconn *conn;
    struct netbuf *sendbuf;
	struct ip_addr l_ip;
    struct CS_Msg_Struct *pMsg;
	err_t err;
	char strbuf[64], cmdbuf[30];
	u16_t strLen;
	s8_t cmd_c;
    _UL curr = 0;
   
    //initData();
	IP4_ADDR(&l_ip, 192,168,0,100);
  	/* Create a new connection identifier. */
	conn = netconn_new(NETCONN_UDP);

	//netconn_bind(conn, &l_ip, 5000);
	netconn_bind(conn, &l_ip, 5000);
  	/* Tell connection to go into listening mode. */
    memset((_UC *)&gUser,0,sizeof(struct UserInfo));    
    
	while(1)
	{
	    //Uart_Printf("come into udp udp_server_thread\n");
	    
	    if(conn  != NULL)
		{
	  		struct netbuf *buf;
	  		void *data;
	  		u16_t len;
			u8_t *datab;
            //Uart_Printf("conn  != NULL\n");
            while((buf = netconn_recv(conn)) != NULL)
			{
			    //Uart_Printf("come into udp netconn_recv\n");
			    curr = 0;
			    memset(recvBuff,0,MAX_RECV_BUFFER);                
				do
				{
					netbuf_data(buf, &data, &len);
					datab = (unsigned char *) data;
                    memcpy(&(recvBuff[curr]),datab,len);
                    curr += len;
                   #if 0
					if(*datab != 0x39)
					{   
					    INT32U os_time;
					    sendbuf = netbuf_new();
                        if (!sendbuf) 
                        {
                           break;
                        }
                        
					    
        				os_time = OSTimeGet();
        				//strlen = sprintf(strbuf, "\r\nNow, OS time is: %d", os_time);
        				//strlen = sprintf(pagebuf, "", page);
        				//netconn_write(newconn, strbuf, strlen, NETCONN_COPY);	
        				memset(strbuf,0x00,64);
        				strLen = sprintf(strbuf,"wo shi wangbin = %lu",os_time);

                        netbuf_ref(sendbuf, strbuf, strLen);
                        netconn_send(conn, sendbuf);
        				//netconn_send(conn, strbuf, strLen, NETCONN_COPY);	
                        netbuf_delete(buf);
                        netbuf_delete(sendbuf);
                        goto t_exit;
					}
                    else
                    {
                        netbuf_delete(buf);
						goto t_exit;                    
                    }
                    #endif
				}
				while(netbuf_next(buf) >= 0);
				netbuf_delete(buf);
                pMsg = (struct CS_Msg_Struct *)recvBuff;
                MessageProc(conn,pMsg);
	  		}	
            
		}
t_exit:
		//err = netconn_delete(conn);	
        //break;
        OSTimeDly(100);
  	}
}

_US MessageProc(struct netconn *conn,struct CS_Msg_Struct * pMsg)
{
    _US errCode = CI_FAILURE;
    //将客户端发送的数据进行加密后与传过来的code比较
    //加密临时变量
    char enbefore[8];//加密前
    char enafter[8];//加密后
    char enCode;
    unsigned long tempCode[2];
    char *temCodePtr;
    char *tlptr;
    
    if (NULL == pMsg)
    {
        return CI_FAILURE;
    }
    
    tempCode[0] = 0;
	tempCode[1] = 0;	
	memset((char *)enbefore,0,8);
	memset((char *)enafter,0,8);
    //Uart_Printf("pMsg->msgType = %d\n",pMsg->msgType);
    //Uart_Printf("pMsg->msgLenth = %d\n",pMsg->msgLenth);
	tlptr   = (char *)&(pMsg->msgType);
	//将msgType与lenth字段进行加密
	memcpy(enbefore,tlptr,8);
    //gUser
    //请求keyNo时用通用key进行加密,其他消息用得到的key
    if (254 == pMsg->keyNo)
    {
        enCode = Des_Go(enafter,enbefore,8,comkey, sizeof(comkey), ENCRYPT);
    }
	else
	{
	    enCode = Des_Go(enafter,enbefore,8,key[((pMsg->keyNo)%10)], sizeof(key[((pMsg->keyNo)%10)]), ENCRYPT); 
	}

	if(1 != enCode)
    {
        return CI_FAILURE;
    }
	
	//拷贝到code
	temCodePtr = (char *)tempCode;
	memcpy(temCodePtr,enafter,8);
    //Uart_Printf("tempCode[0] = %lx\n",tempCode[0]);
    //Uart_Printf("tempCode[1] = %lx\n",tempCode[1]);
	if((tempCode[0] != pMsg->code[0])
		||(tempCode[1] != pMsg->code[1]))
	{//认证失败
	    return CI_FAILURE;
	}
    
     switch (pMsg->m_id)
     {
         case MID_USER:
		 {
		 	errCode = UserProc(conn,pMsg);
		 	break;
		 }
         
         case MID_DATA:
		 {
            //Uart_Printf("come into MessageProc: DataProc\n");
		 	errCode = DataProc(conn,pMsg);
		 	break;
		 }

		 case MID_TEMP:
		 {
		 	errCode = TempProc(conn,pMsg);
		 	break;
		 }
		 default:
		 {
		 	return CI_FAILURE;
		 }
     }

	 return errCode;
}

_US UserProc(struct netconn *conn,struct CS_Msg_Struct * pMsg)
{
    _US   errCode = CI_FAILURE;   
    struct CS_Msg_Struct  msg;    
    char name[11];
    char psw[7];    
    char tempstr[16];

    //char tempname[11];
    //char temppsw[7];
    //char i;
    
	if ((NULL == pMsg)||(NULL == conn))
     {
         return CI_FAILURE;
     }
	 msg.m_id    = pMsg->m_id;
     msg.msgType = pMsg->msgType;   
     msg.keyNo   = pMsg->keyNo;  
     //Uart_Printf("UserProc: start\n");
     switch (pMsg->msgType)
     {//login->verify->online
         case USER_LOGIN:
		 {
            struct Query_Client_Key clkey;
            clkey.errCode = CI_SUCCESS;
            clkey.keyNo   = (isrCount%10);
            memcpy((_UC *)msg.msgBody,(_UC *)(&clkey),sizeof(struct Query_Client_Key));
            msg.msgLenth = sizeof(struct Query_Client_Key);
            sendmessage(conn,&msg);
            errCode = CI_SUCCESS;
		 	break;          
         }
         
         case USER_VARIFY:
		 {
            
            struct Varify_UserInfo Data;
            struct Varify_UserInfo *pUserData;
            _US    userNo = 0;//检验的用户序号
            _UC    decode;
            pUserData = (struct Varify_UserInfo*)(pMsg->msgBody);            
            memset((_UC *)&Data,0,sizeof(struct Varify_UserInfo));
            
            //Uart_Printf("UserProc: USER_VARIFY\n");	
            memset(name,0,11);
            memset(psw,0,7);
            
            memset(tempstr,0,16);
            memcpy(tempstr,(pUserData->userinfo.name),10);
            memcpy((tempstr+10),(pUserData->userinfo.psw),6);
            
            #if 0
            memset(tempname,0,11);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -