修改事项: 1、新增TFTP IAP升级功能,只是代码移植完毕,没有测试使用 2、代码空间编译优化,零等待区域空间已满,而应用层代码已全部挪移到非零等待区域中,但还是会增加零等待区的空间占用。 待优化
1101 lines
41 KiB
C
1101 lines
41 KiB
C
/*
|
||
* net_function.c
|
||
*
|
||
* Created on: 2025Äê5ÔÂ21ÈÕ
|
||
* Author: cc
|
||
*/
|
||
#include "includes.h"
|
||
#include <string.h>
|
||
#include <ctype.h>
|
||
|
||
uint8_t MACAddr[6]; //MAC address
|
||
uint8_t IPAddr[4] = {172, 16, 4, 100}; //IP address
|
||
uint8_t GWIPAddr[4] = {172, 16, 4, 254}; //Gateway IP address
|
||
uint8_t IPMask[4] = {255, 255, 0, 0}; //subnet mask
|
||
uint8_t DESIP[4] = {172, 16, 4, 55}; //destination IP address
|
||
uint16_t desport = 1000; //destination port
|
||
uint16_t srcport = 1000; //source port
|
||
|
||
uint8_t SocketId;
|
||
uint8_t socket[WCHNET_MAX_SOCKET_NUM]; //Save the currently connected socket
|
||
uint8_t SocketRecvBuf[WCHNET_MAX_SOCKET_NUM][RECE_BUF_LEN]; //socket receive buffer
|
||
|
||
//É豸µÄÍøÂçÐÅÏ¢
|
||
WCHNET_INFO_T g_netinfo = {
|
||
.device_ip = {192,168,1,200},
|
||
.gateway = {192,168,1,1},
|
||
.subnet = {255,255,0,0},
|
||
.mac_addr = {0x34,0xD0,0xB8,0x11,0x11,0x11},
|
||
.SocketId = {0,0,0,0},
|
||
};
|
||
|
||
DEVICE_NET_APPINFO server_info = {
|
||
.net_sta = 0,
|
||
|
||
};
|
||
|
||
TFTP_LOG tftp_log;
|
||
|
||
void ETH_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
|
||
void TIM2_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
|
||
|
||
/*******************************************************************************
|
||
* Function Name : mStopIfError
|
||
* Description : check if error.
|
||
* Input :
|
||
* iError - error constants.
|
||
*******************************************************************************/
|
||
void mStopIfError(uint8_t iError)
|
||
{
|
||
if (iError == WCHNET_ERR_SUCCESS) return;
|
||
|
||
printf("Error: %X\r\n", (uint16_t) iError);
|
||
}
|
||
|
||
|
||
/*******************************************************************************
|
||
* Function Name : TIM2_Init
|
||
* Description : Initializes TIM2.
|
||
*******************************************************************************/
|
||
__attribute__((section(".non_0_wait"))) void TIM2_Init(void)
|
||
{
|
||
TMR2_ClrCurrentCount();
|
||
R32_TMR2_CNT_END = SystemCoreClock / 1000 * WCHNETTIMERPERIOD;
|
||
R8_TMR2_CTRL_MOD = RB_TMR_COUNT_EN;
|
||
TMR2_ITCfg(ENABLE, RB_TMR_IE_CYC_END);
|
||
NVIC_EnableIRQ(TIM2_IRQn);
|
||
}
|
||
|
||
/*******************************************************************************
|
||
* Function Name : ETH_IRQHandler
|
||
* Description : This function handles ETH exception.
|
||
*******************************************************************************/
|
||
void ETH_IRQHandler(void)
|
||
{
|
||
WCHNET_ETHIsr();
|
||
}
|
||
|
||
/*******************************************************************************
|
||
* Function Name : TIM2_IRQHandler
|
||
* Description : This function handles TIM2 exception.
|
||
*******************************************************************************/
|
||
void TIM2_IRQHandler(void)
|
||
{
|
||
WCHNET_TimeIsr(WCHNETTIMERPERIOD);
|
||
TMR2_ClearITFlag(RB_TMR_IF_CYC_END);
|
||
}
|
||
|
||
/*******************************************************************************
|
||
* Function Name : WCHNET_CreateUdpSocket
|
||
* Description : ´´½¨UDPÌ×½Ó×Ö
|
||
* Input :
|
||
* S - socketË÷ÒýÖµ
|
||
* SourPort - socketÔ´¶Ë¿Ú
|
||
* cb - socket»Øµ÷º¯Êý
|
||
* Return : None
|
||
*******************************************************************************/
|
||
__attribute__((section(".non_0_wait"))) void WCHNET_CreateUdpSocket(uint8_t* S, uint16_t SourPort, pSockRecv cb)
|
||
{
|
||
uint8_t i;
|
||
SOCK_INF TmpSocketInf; /* ´´½¨ÁÙʱsocket±äÁ¿ */
|
||
|
||
memset((void *)&TmpSocketInf,0,sizeof(SOCK_INF)); /* ¿âÄÚ²¿»á½«´Ë±äÁ¿¸´ÖÆ£¬ËùÒÔ×îºÃ½«ÁÙʱ±äÁ¿ÏÈÈ«²¿ÇåÁã */
|
||
TmpSocketInf.IPAddr[0] = 0xFF;
|
||
TmpSocketInf.IPAddr[1] = 0xFF;
|
||
TmpSocketInf.IPAddr[2] = 0xFF;
|
||
TmpSocketInf.IPAddr[3] = 0xFF;
|
||
TmpSocketInf.DesPort = SourPort; /* ÉèÖÃÄ¿µÄ¶Ë¿Ú */
|
||
TmpSocketInf.SourPort = SourPort; /* ÉèÖÃÔ´¶Ë¿Ú */
|
||
TmpSocketInf.ProtoType = PROTO_TYPE_UDP; /* ÉèÖÃsocketÀàÐÍ */
|
||
TmpSocketInf.RecvBufLen = RECE_BUF_LEN; /* ÉèÖÃsocket ½ÓÊÕ»º³åÇø³¤¶È */
|
||
TmpSocketInf.AppCallBack = cb; /* ÉèÖýÓÊջص÷º¯Êý */
|
||
|
||
i = WCHNET_SocketCreat(S, &TmpSocketInf); /* ´´½¨socket£¬½«·µ»ØµÄsocketË÷Òý±£´æÔÚSocketIdÖÐ */
|
||
mStopIfError(i);
|
||
WCHNET_ModifyRecvBuf(SocketId, (uint32_t) SocketRecvBuf[SocketId], RECE_BUF_LEN);
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"%s - %d",__func__, *S);
|
||
}
|
||
|
||
/*******************************************************************************
|
||
* Function Name : WCHNET_CreateTcpSocket
|
||
* Description : ´´½¨TCPÌ×½Ó×Ö
|
||
* Input :
|
||
* S - socketË÷ÒýÖµ
|
||
* Return : None
|
||
*******************************************************************************/
|
||
__attribute__((section(".non_0_wait"))) void WCHNET_CreateTcpSocket(uint8_t* S)
|
||
{
|
||
uint8_t i;
|
||
SOCK_INF TmpSocketInf;
|
||
|
||
memset((void *) &TmpSocketInf, 0, sizeof(SOCK_INF));
|
||
memcpy((void *) TmpSocketInf.IPAddr, DESIP, 4);
|
||
TmpSocketInf.DesPort = desport;
|
||
TmpSocketInf.SourPort = srcport;
|
||
TmpSocketInf.ProtoType = PROTO_TYPE_TCP;
|
||
TmpSocketInf.RecvStartPoint = (uint32_t) SocketRecvBuf[0];
|
||
TmpSocketInf.RecvBufLen = RECE_BUF_LEN;
|
||
i = WCHNET_SocketCreat(S, &TmpSocketInf);
|
||
printf("TCP SocketId %d\r\n", *S);
|
||
mStopIfError(i);
|
||
i = WCHNET_SocketConnect(*S); //make a TCP connection
|
||
mStopIfError(i);
|
||
}
|
||
|
||
void UDPSocket1_AppCallBack( struct _SOCK_INF * SocketInf,uint32_t ipaddr,uint16_t port,uint8_t *buff,uint32_t len)
|
||
{
|
||
uint8_t ip[4];
|
||
|
||
ip[0] = ipaddr;
|
||
ip[1] = ipaddr>>8;
|
||
ip[2] = ipaddr>>16;
|
||
ip[3] = ipaddr>>24;
|
||
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"ip:%d.%d.%d.%d, port:%d",ip[0], ip[1], ip[2], ip[3], port);
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"Socket1 len:%ld",len);
|
||
|
||
Dbg_Print_Buff(DBG_BIT_NET_STATUS_bit,"data :",buff,len);
|
||
//Udp_Internal_Analysis(buff, len, ip, port);
|
||
|
||
if(buff[0] == 0xAA)
|
||
{
|
||
switch(buff[1])
|
||
{
|
||
case 0x55: //¾ÉµÄ·þÎñÆ÷ÐÒé
|
||
//Udp_Internal_Analysis(buff, len, ip, port);
|
||
break;
|
||
case 0x66: //UDP·þÎñÐÒé - 2022-05-31
|
||
//UDP_NetServer_Data_Analysis(buff, len, ip, port);
|
||
break;
|
||
}
|
||
|
||
}
|
||
}
|
||
|
||
/*******************************************************************************
|
||
* Function Name : WCHNET_DataLoopback
|
||
* Description : Data loopback function.
|
||
* Input :
|
||
* id - socket id.
|
||
* Return : None
|
||
*******************************************************************************/
|
||
__attribute__((section(".non_0_wait"))) void WCHNET_DataLoopback(uint8_t id)
|
||
{
|
||
uint8_t i;
|
||
uint32_t len;
|
||
uint32_t endAddr = SocketInf[id].RecvStartPoint + SocketInf[id].RecvBufLen; //Receive buffer end address
|
||
|
||
if ((SocketInf[id].RecvReadPoint + SocketInf[id].RecvRemLen) > endAddr) { //Calculate the length of the received data
|
||
len = endAddr - SocketInf[id].RecvReadPoint;
|
||
}
|
||
else {
|
||
len = SocketInf[id].RecvRemLen;
|
||
}
|
||
i = WCHNET_SocketSend(id, (uint8_t *) SocketInf[id].RecvReadPoint, &len); //send data
|
||
if (i == WCHNET_ERR_SUCCESS) {
|
||
WCHNET_SocketRecv(id, NULL, &len); //Clear sent data
|
||
}
|
||
|
||
}
|
||
|
||
/*******************************************************************************
|
||
* Function Name : WCHNET_HandleSockInt
|
||
* Description : Socket Interrupt Handle
|
||
* Input :
|
||
* socketid - socket id.
|
||
* intstat - interrupt status
|
||
* Return : None
|
||
*******************************************************************************/
|
||
__attribute__((section(".non_0_wait"))) void WCHNET_HandleSockInt(uint8_t socketid, uint8_t intstat)
|
||
{
|
||
uint8_t i;
|
||
|
||
if (intstat & SINT_STAT_RECV) //receive data
|
||
{
|
||
WCHNET_DataLoopback(socketid); //Data loopback
|
||
}
|
||
if (intstat & SINT_STAT_CONNECT) //connect successfully
|
||
{
|
||
#if KEEPALIVE_ENABLE
|
||
WCHNET_SocketSetKeepLive(socketid, ENABLE);
|
||
#endif
|
||
|
||
for (i = 0; i < WCHNET_MAX_SOCKET_NUM; i++) {
|
||
if (socket[i] == 0xff) { //save connected socket id
|
||
socket[i] = socketid;
|
||
|
||
//ÐèҪעÒâÒªÊÇÌ×½Ó×ÖID ³¬³öÊý×鷶ΧµÄ»°£¬ÎÞÐèÖØÐ·ÖÅä½ÓÊÕ»º³åÇø
|
||
// if(socketid < WCHNET_MAX_SOCKET_NUM)
|
||
// {
|
||
// WCHNET_ModifyRecvBuf(socketid, (uint32_t) SocketRecvBuf[socketid], RECE_BUF_LEN);
|
||
// }
|
||
break;
|
||
}
|
||
}
|
||
printf("TCP Connect Success\r\n");
|
||
printf("socket id: %d\r\n", socket[i]);
|
||
}
|
||
if (intstat & SINT_STAT_DISCONNECT) //disconnect
|
||
{
|
||
for (i = 0; i < WCHNET_MAX_SOCKET_NUM; i++) { //delete disconnected socket id
|
||
if (socket[i] == socketid) {
|
||
socket[i] = 0xff;
|
||
break;
|
||
}
|
||
}
|
||
printf("TCP Disconnect\r\n");
|
||
}
|
||
if (intstat & SINT_STAT_TIM_OUT) //timeout disconnect
|
||
{
|
||
for (i = 0; i < WCHNET_MAX_SOCKET_NUM; i++) { //delete disconnected socket id
|
||
if (socket[i] == socketid) {
|
||
socket[i] = 0xff;
|
||
break;
|
||
}
|
||
}
|
||
printf("TCP Timeout\r\n");
|
||
}
|
||
}
|
||
|
||
/*******************************************************************************
|
||
* Function Name : WCHNET_Get_PHY_Linked_Status
|
||
* Description : »ñÈ¡PHYÁ´Â·×´Ì¬
|
||
* Return £º·µ»ØPHY״̬£º 0x00 - PHY Link Succ,0x01 - PHY Link Fail
|
||
*******************************************************************************/
|
||
__attribute__((section(".non_0_wait"))) uint8_t WCHNET_Get_PHY_Linked_Status(void)
|
||
{
|
||
uint16_t rev = 0;
|
||
rev = WCHNET_GetPHYStatus();
|
||
|
||
if(rev & PHY_Linked_Status) return 0x00;
|
||
|
||
return 0x01;
|
||
}
|
||
|
||
/*******************************************************************************
|
||
* Function Name : WCHNET_HandleGlobalInt
|
||
* Description : Global Interrupt Handle
|
||
*******************************************************************************/
|
||
__attribute__((section(".non_0_wait"))) void WCHNET_HandleGlobalInt(void)
|
||
{
|
||
uint8_t intstat;
|
||
uint16_t i;
|
||
uint8_t socketint;
|
||
|
||
intstat = WCHNET_GetGlobalInt(); //get global interrupt flag
|
||
if (intstat & GINT_STAT_UNREACH) //Unreachable interrupt
|
||
{
|
||
printf("GINT_STAT_UNREACH\r\n");
|
||
}
|
||
if (intstat & GINT_STAT_IP_CONFLI) //IP conflict
|
||
{
|
||
printf("GINT_STAT_IP_CONFLI\r\n");
|
||
}
|
||
if (intstat & GINT_STAT_PHY_CHANGE) //PHY status change
|
||
{
|
||
i = WCHNET_GetPHYStatus();
|
||
if (i & PHY_Linked_Status) Dbg_Println(DBG_BIT_NET_STATUS_bit,"PHY Link Success");
|
||
else{
|
||
//ÍøÏßÒѰγö
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"PHY¶Ï¿ª,ÐèÖØÐ³õʼ»¯ÍøÂç");
|
||
server_info.init_flag = 0; //PHY¶Ï¿ª,ÐèÖØÐ³õʼ»¯ÍøÂç
|
||
LOG_SYS_PHY_Change_Record(0x00); //ÍøÏ߰γö
|
||
}
|
||
}
|
||
if (intstat & GINT_STAT_SOCKET) { //socket related interrupt
|
||
for (i = 0; i < WCHNET_MAX_SOCKET_NUM; i++) {
|
||
socketint = WCHNET_GetSocketInt(i);
|
||
if (socketint)
|
||
WCHNET_HandleSockInt(i, socketint);
|
||
}
|
||
}
|
||
}
|
||
|
||
/*******************************************************************************
|
||
* Function Name : WCHNET_DHCPCallBack
|
||
* Description : DHCPCallBack
|
||
* Input :
|
||
status - status returned by DHCP
|
||
arg - Data returned by DHCP
|
||
* Return : DHCP status
|
||
*******************************************************************************/
|
||
uint8_t WCHNET_DHCPCallBack(uint8_t status, void *arg)
|
||
{
|
||
uint8_t *p;
|
||
uint8_t tmp[4] = {0, 0, 0, 0};
|
||
|
||
if(!status)
|
||
{
|
||
p = arg;
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"DHCP Success");
|
||
/*If the obtained IP is the same as the last IP, exit this function.*/
|
||
if(!memcmp(g_netinfo.device_ip, p ,sizeof(IPAddr))) {
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"DHCP »ñÈ¡IPÓ뵱ǰIPÏàͬ!");
|
||
return ETH_SUCCESS;
|
||
}
|
||
/*Determine whether it is the first successful IP acquisition*/
|
||
if(memcmp(g_netinfo.device_ip, tmp ,sizeof(IPAddr))){
|
||
/*The obtained IP is different from the last value,
|
||
* then disconnect the last connection.*/
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"DHCP »ñÈ¡IPÓ뵱ǰIP²»Í¬ ¶Ï¿ªTCPÁ´½Ó\r\n");
|
||
|
||
WCHNET_SocketClose(SocketId, TCP_CLOSE_NORMAL); //ÌØ±ðÇ¿µ÷£¬¸ÃÊÍ·ÅÌ×½Ó×ÖÓëDHCPÍøÂçµ×²ã²Ù×÷Óйأ¬É¾³ý»áµ¼Ö³öÏÖÍøÂçÒì³£
|
||
}
|
||
memcpy(g_netinfo.device_ip, p, 4);
|
||
memcpy(g_netinfo.gateway, &p[4], 4);
|
||
memcpy(g_netinfo.subnet, &p[8], 4);
|
||
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"IPAddr = %d.%d.%d.%d ", g_netinfo.device_ip[0], g_netinfo.device_ip[1], g_netinfo.device_ip[2], g_netinfo.device_ip[3]);
|
||
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"GWIPAddr = %d.%d.%d.%d ", g_netinfo.gateway[0], g_netinfo.gateway[1], g_netinfo.gateway[2], g_netinfo.gateway[3]);
|
||
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"IPMask = %d.%d.%d.%d ", g_netinfo.subnet[0], g_netinfo.subnet[1], g_netinfo.subnet[2], g_netinfo.subnet[3]);
|
||
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"DNS2: %d.%d.%d.%d ", p[16], p[17], p[18], p[19]);
|
||
|
||
WCHNET_DHCPStop(); //¹Ø±ÕDHCP¹¦ÄÜ
|
||
|
||
server_info.net_retry_num = 0;
|
||
|
||
server_info.net_sta = NET_SOCKET_WAIT;
|
||
server_info.wait_cot = SysTick_1ms;
|
||
|
||
return ETH_SUCCESS;
|
||
}
|
||
else
|
||
{
|
||
/*DHCP»ñȡʧ°Ü */
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"DHCP Fail %02x ", status);
|
||
/*Determine whether it is the first successful IP acquisition*/
|
||
if(memcmp(IPAddr, tmp ,sizeof(IPAddr))){
|
||
/*The obtained IP is different from the last value*/
|
||
//WCHNET_SocketClose(SocketId, TCP_CLOSE_NORMAL);
|
||
}
|
||
return ETH_ERROR;
|
||
}
|
||
}
|
||
|
||
/*******************************************************************************
|
||
* Function Name : WCHNET_DNSCallBack_1
|
||
* Description : ½âÎöÔÆ¶Ë·þÎñÆ÷ÓòÃû»Øµ÷º¯Êý
|
||
* Input :
|
||
name - DNS½âÎöÓòÃû
|
||
ipaddr - DNS½âÎöÓòÃûµÄIP
|
||
* Return : DHCP status
|
||
*******************************************************************************/
|
||
__attribute__((section(".non_0_wait"))) void WCHNET_DNSCallBack_1(const char *name, uint8_t *ipaddr, void *callback_arg)
|
||
{
|
||
if(ipaddr == NULL)
|
||
{
|
||
server_info.net_retry_num++;
|
||
if(server_info.net_retry_num >= 3)
|
||
{
|
||
server_info.net_retry_num = 0;
|
||
server_info.net_sta = NET_TFTP; //½âÎöʧ°Ü£¬ÏÂÒ»¸ö
|
||
|
||
WCHNET_DNSStop(); //stop DNS,and release socket
|
||
}
|
||
Dbg_Print(DBG_BIT_NET_STATUS_bit,"DNS Fail\r\n");
|
||
return;
|
||
}
|
||
|
||
server_info.dis_ip[0] = ipaddr[0];
|
||
server_info.dis_ip[1] = ipaddr[1];
|
||
server_info.dis_ip[2] = ipaddr[2];
|
||
server_info.dis_ip[3] = ipaddr[3];
|
||
|
||
Dbg_Print(DBG_BIT_NET_STATUS_bit,"Host Name = %s\r\n", name);
|
||
Dbg_Print(DBG_BIT_NET_STATUS_bit,"IP= %d.%d.%d.%d\r\n", ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]);
|
||
|
||
if(callback_arg != NULL)
|
||
{
|
||
Dbg_Print(DBG_BIT_NET_STATUS_bit,"callback_arg = %02x\r\n", (*(uint8_t *)callback_arg));
|
||
}
|
||
|
||
WCHNET_DNSStop(); //stop DNS,and release socket
|
||
|
||
server_info.server_dns_flag = 0x01; //½âÎö³É¹¦±ê־λ
|
||
server_info.net_sta = NET_TFTP; //½âÎö³É¹¦£¬ÏÂÒ»¸ö
|
||
}
|
||
|
||
|
||
/*******************************************************************************
|
||
* Function Name : WCHNET_DNSCallBack_2
|
||
* Description : ½âÎöTFTP·þÎñÆ÷ÓòÃû»Øµ÷º¯Êý - ÍøÂçÆô¶¯Ê±½âÎöÓòÃû
|
||
* Input :
|
||
name - DNS½âÎöÓòÃû
|
||
ipaddr - DNS½âÎöÓòÃûµÄIP
|
||
* Return : DHCP status
|
||
*******************************************************************************/
|
||
__attribute__((section(".non_0_wait"))) void WCHNET_DNSCallBack_2(const char *name, uint8_t *ipaddr, void *callback_arg)
|
||
{
|
||
if(ipaddr == NULL)
|
||
{
|
||
server_info.net_retry_num++;
|
||
if(server_info.net_retry_num >= 3)
|
||
{
|
||
server_info.net_retry_num = 0;
|
||
server_info.net_sta = NET_COMPLETE;
|
||
server_info.init_flag = 0x01;
|
||
}
|
||
Dbg_Print(DBG_BIT_NET_STATUS_bit,"DNS Fail\r\n");
|
||
return;
|
||
}
|
||
|
||
server_info.tftp_ip[0] = ipaddr[0];
|
||
server_info.tftp_ip[1] = ipaddr[1];
|
||
server_info.tftp_ip[2] = ipaddr[2];
|
||
server_info.tftp_ip[3] = ipaddr[3];
|
||
|
||
Dbg_Print(DBG_BIT_NET_STATUS_bit,"Host Name = %s\r\n", name);
|
||
Dbg_Print(DBG_BIT_NET_STATUS_bit,"IP= %d.%d.%d.%d\r\n", ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]);
|
||
if(callback_arg != NULL)
|
||
{
|
||
Dbg_Print(DBG_BIT_NET_STATUS_bit,"callback_arg = %02x\r\n", (*(uint8_t *)callback_arg));
|
||
}
|
||
|
||
WCHNET_DNSStop(); //stop DNS,and release socket
|
||
|
||
server_info.tftp_dns_flag = 0x01; //½âÎö³É¹¦±ê־λ
|
||
server_info.net_sta = NET_COMPLETE;
|
||
server_info.init_flag = 0x01;
|
||
}
|
||
|
||
/*******************************************************************************
|
||
* Function Name : WCHNET_DNSCallBack_3
|
||
* Description : ½âÎöTFTP·þÎñÆ÷ÓòÃû»Øµ÷º¯Êý - TFTP·¢Éúǰ½âÎöÓòÃû
|
||
* Input :
|
||
name - DNS½âÎöÓòÃû
|
||
ipaddr - DNS½âÎöÓòÃûµÄIP
|
||
* Return : DHCP status
|
||
*******************************************************************************/
|
||
__attribute__((section(".non_0_wait"))) void WCHNET_DNSCallBack_3(const char *name, uint8_t *ipaddr, void *callback_arg)
|
||
{
|
||
if(ipaddr == NULL)
|
||
{
|
||
server_info.net_retry_num++;
|
||
if(server_info.net_retry_num >= 3)
|
||
{
|
||
server_info.net_retry_num = 0;
|
||
|
||
}
|
||
Dbg_Print(DBG_BIT_NET_STATUS_bit,"DNS Fail\r\n");
|
||
return;
|
||
}
|
||
|
||
server_info.tftp_ip[0] = ipaddr[0];
|
||
server_info.tftp_ip[1] = ipaddr[1];
|
||
server_info.tftp_ip[2] = ipaddr[2];
|
||
server_info.tftp_ip[3] = ipaddr[3];
|
||
|
||
Dbg_Print(DBG_BIT_NET_STATUS_bit,"Host Name = %s\r\n", name);
|
||
Dbg_Print(DBG_BIT_NET_STATUS_bit,"IP= %d.%d.%d.%d\r\n", ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]);
|
||
if(callback_arg != NULL)
|
||
{
|
||
Dbg_Print(DBG_BIT_NET_STATUS_bit,"callback_arg = %02x\r\n", (*(uint8_t *)callback_arg));
|
||
}
|
||
|
||
WCHNET_DNSStop(); //stop DNS,and release socket
|
||
|
||
server_info.tftp_dns_flag = 0x01; //½âÎö³É¹¦±ê־λ
|
||
}
|
||
|
||
/*******************************************************************************
|
||
* Function Name : is_valid_domain
|
||
* Description : ÅжÏÓòÃû
|
||
* Input :
|
||
domain - ÐèÒªÅжϵÄÓòÃû
|
||
* Return : 0x00:ºÏ¸ñÓòÃû£¬0x01:·Ç·¨ÓòÃû
|
||
*******************************************************************************/
|
||
__attribute__((section(".non_0_wait"))) uint8_t is_valid_domain(const char *domain)
|
||
{
|
||
const char *c = domain;
|
||
int parts = 0;
|
||
int dashes = 0;
|
||
|
||
//Dbg_Println(DBG_BIT_NET_STATUS_bit,"is_valid_domain_0:%s",domain);
|
||
|
||
// ¼ì²éÊÇ·ñÓÐÁ¬ÐøµÄµã»òÁ¬×Ö·û
|
||
if (strstr(domain, "..") || strstr(domain, "--"))
|
||
{
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"is_valid_domain_1");
|
||
return 0x01;
|
||
}
|
||
|
||
// ¼ì²éÿ¸ö×Ö·û
|
||
while (*c)
|
||
{
|
||
if (isalpha(*c) || isdigit(*c) || *c == '-' || *c == '.')
|
||
{
|
||
if (*c == '-')
|
||
{
|
||
// ²»ÔÊÐíÔÚ¿ªÍ·»ò½áβ³öÏÖÁ¬×Ö·û
|
||
if (c == domain || *(c + 1) == '\0' || dashes == 0)
|
||
{
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"is_valid_domain_2");
|
||
return 0x01;
|
||
}
|
||
dashes = 0;
|
||
}
|
||
else
|
||
{
|
||
dashes = 1;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
// ·Ç·¨×Ö·û
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"is_valid_domain_3:%c",*c);
|
||
return 0x01;
|
||
}
|
||
|
||
if (*c == '.')
|
||
{
|
||
parts++; // ·Ö¸ô·û¼ÆÊý
|
||
if (c == domain || *(c + 1) == '\0')
|
||
{
|
||
// ²»ÔÊÐíÔÚ¿ªÍ·¡¢½áβ»òÕßÔÚÁ½¸öµãÖ®¼äΪ¿Õ
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"is_valid_domain_4");
|
||
return 0x01;
|
||
}
|
||
}
|
||
c++;
|
||
}
|
||
|
||
// ÖÁÉÙÐèÒªÁ½¸ö²¿·Ö£¨ÀýÈç example.com£©
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"is_valid_domain_5:%d",parts);
|
||
|
||
if(parts > 1)
|
||
{
|
||
return 0x00; //ºÏ¸ñÓòÃû
|
||
}else {
|
||
return 0x01; //·Ç·¨ÓòÃû
|
||
}
|
||
}
|
||
|
||
/*******************************************************************************
|
||
* Function Name : NetWork_Parameter_Get
|
||
* Description : ÍøÂçÈÎÎñ »ñÈ¡ÍøÂç³õʼ»¯²ÎÊýÒÔ¼°ÍøÂç³õʼ»¯¹¦ÄÜ
|
||
* Input : None
|
||
* Output : None
|
||
* Return : None
|
||
* Óà ·¨ : ³õʼ»¯µ÷ÓÃ
|
||
*******************************************************************************/
|
||
__attribute__((section(".non_0_wait"))) void NetWork_Parameter_Get(void)
|
||
{
|
||
uint8_t Arge_Flag = 0;
|
||
uint32_t temp = 0;
|
||
|
||
/*ÅжÏÍøÂçÊÇ·ñʹÓÃÉèÖòÎÊýΪĬÈÏIPµØÖ·*/
|
||
Arge_Flag = SRAM_Read_Byte(SRAM_Register_Start_ADDRESS + Register_NetInfo_EN_OFFSET + 1);
|
||
if(Arge_Flag == 0x01)
|
||
{
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"NET ÍøÂç²ÎÊýʹÓÃPCÉèÖòÎÊý!");
|
||
SRAM_DMA_Read_Buff(g_netinfo.device_ip, 4, SRAM_Register_Start_ADDRESS + Register_NetIP_OFFSET);
|
||
SRAM_DMA_Read_Buff(g_netinfo.gateway,4,SRAM_Register_Start_ADDRESS + Register_NetGateway_OFFSET);
|
||
SRAM_DMA_Read_Buff(g_netinfo.subnet,4,SRAM_Register_Start_ADDRESS + Register_NetMask_OFFSET);
|
||
SRAM_DMA_Read_Buff(g_netinfo.dns_server_ip,4,SRAM_Register_Start_ADDRESS + Register_DNSServerIP_OFFSET);
|
||
}else {
|
||
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"NET ÍøÂç²ÎÊýʹÓñ¾»úĬÈϲÎÊý!");
|
||
//MCU³õʼ»¯IP
|
||
g_netinfo.device_ip[0] = 192;
|
||
g_netinfo.device_ip[1] = 168;
|
||
g_netinfo.device_ip[2] = MACAddr[4];
|
||
g_netinfo.device_ip[3] = MACAddr[5];
|
||
//MCU³õÊ¼Íø¹Ø
|
||
g_netinfo.gateway[0] = 192;
|
||
g_netinfo.gateway[1] = 168;
|
||
g_netinfo.gateway[2] = MACAddr[4];
|
||
g_netinfo.gateway[3] = 1;
|
||
//MCU³õʼ×ÓÍøÑÚÂë
|
||
g_netinfo.subnet[0] = 255;
|
||
g_netinfo.subnet[1] = 255;
|
||
g_netinfo.subnet[2] = 0;
|
||
g_netinfo.subnet[3] = 0;
|
||
//MCU DNSµØÖ·
|
||
g_netinfo.dns_server_ip[0] = 223;
|
||
g_netinfo.dns_server_ip[1] = 5;
|
||
g_netinfo.dns_server_ip[2] = 5;
|
||
g_netinfo.dns_server_ip[3] = 5;
|
||
}
|
||
|
||
/*ÆôÓÃDHCP*/
|
||
if(SRAM_Read_Byte(SRAM_Register_Start_ADDRESS + Register_NetInfo_EN_OFFSET) != 0x02)
|
||
{
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"DHCP¹¦ÄÜʹÄÜ!");
|
||
server_info.dhcp_en = 0x01;
|
||
}else{ /*²»ÆôÓÃ*/
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"²»ÆôÓÃDHCP¹¦ÄÜ!");
|
||
server_info.dhcp_en = 0x00;
|
||
}
|
||
|
||
/*Åжϵ±Ç°Ê¹Óñ¾µØ·þÎñÆ÷»¹ÊÇÔÆ¶Ë·þÎñÆ÷*/
|
||
temp = SRAM_Read_Byte(SRAM_Register_Start_ADDRESS + Register_NetInfo_EN_OFFSET + 2);
|
||
|
||
if(temp == 0x01) //ʹÓñ¾µØ¶Ë·þÎñÆ÷
|
||
{
|
||
server_info.server_select = 0x01;
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"ʹÓñ¾µØ¶Ë·þÎñÆ÷");
|
||
}else //ʹÓÃÔÆ¶Ë¶Ë·þÎñÆ÷
|
||
{
|
||
server_info.server_select = 0x00;
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"ʹÓÃÔÆ¶Ë¶Ë·þÎñÆ÷");
|
||
}
|
||
|
||
memset(&tftp_log,0,sizeof(TFTP_LOG));
|
||
|
||
SRAM_DMA_Read_Buff((uint8_t*)&tftp_log.Time,2,SRAM_Register_Start_ADDRESS + Register_TFTPLOGTime_OFFSET);
|
||
SRAM_DMA_Read_Buff((uint8_t*)&tftp_log.Port,2,SRAM_Register_Start_ADDRESS + Register_TFTPLOGPort_OFFSET);
|
||
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"tftp log port:%d time:%dh",tftp_log.Port,tftp_log.Time);
|
||
|
||
if((tftp_log.Port == 0xFFFF) || (tftp_log.Port == 0x00))
|
||
{
|
||
tftp_log.Port = TFTP_Destination_Port;
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"tftp log default port:%d",tftp_log.Port);
|
||
}
|
||
|
||
if((tftp_log.Time == 0x00) || (tftp_log.Time > 720))
|
||
{
|
||
tftp_log.Time = 720; //1¸öÔÂ
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"tftp log default time:%dh",tftp_log.Time);
|
||
}
|
||
|
||
tftp_log.DN_Lens = SRAM_Read_Byte(SRAM_Register_Start_ADDRESS + Register_TFTPDmLens_OFFSET);
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"tftp log domain name lens:%d",tftp_log.DN_Lens);
|
||
if(tftp_log.DN_Lens < DOMAINNAME_MAX)
|
||
{
|
||
SRAM_DMA_Read_Buff((uint8_t*)tftp_log.DomainName,tftp_log.DN_Lens,SRAM_Register_Start_ADDRESS + Register_TFTPDmName_OFFSET);
|
||
tftp_log.DomainName[tftp_log.DN_Lens] = '\0';
|
||
}
|
||
|
||
if((tftp_log.DN_Lens == 0x00) || (is_valid_domain(tftp_log.DomainName) != 0x00)) //ÓòÃû²»ºÏ·¨Ê¹ÓÃĬÈÏ
|
||
{
|
||
tftp_log.DN_Lens = sizeof(TFTPSERVER_NAME_DNS);
|
||
if(tftp_log.DN_Lens < DOMAINNAME_MAX)
|
||
{
|
||
memcpy(tftp_log.DomainName,(char*)TFTPSERVER_NAME_DNS,sizeof(TFTPSERVER_NAME_DNS));
|
||
tftp_log.DomainName[tftp_log.DN_Lens] = '\0';
|
||
}
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"tftp domain name err,use default:%s",tftp_log.DomainName);
|
||
}
|
||
|
||
//¶ÁÈ¡·þÎñÆ÷ÀëÏßÅж¨Ê±¼ä
|
||
server_info.udp_online_time = SRAM_Read_DW(SRAM_Register_Start_ADDRESS + Register_NetOfflineTime_OFFSET);
|
||
if( (server_info.udp_online_time < 1000) || (server_info.udp_online_time > 7200000) )
|
||
{
|
||
server_info.udp_online_time = 600000;
|
||
}
|
||
|
||
//¶ÁÈ¡UDP ¶¨ÆÚÉϱ¨Ê±¼ä¼ä¸ô
|
||
temp = SRAM_Read_DW(SRAM_Register_Start_ADDRESS + Register_UDPPeriodicTime_OFFSET);
|
||
if( (temp < 10000) || (temp > 7200000) )
|
||
{
|
||
server_info.udp_periodic_time = 60;
|
||
}else {
|
||
server_info.udp_periodic_time = temp / 1000;
|
||
}
|
||
|
||
//¶ÁÈ¡·þÎñÆ÷ÓòÃûÓë¶Ë¿Ú
|
||
temp = SRAM_Read_DW(SRAM_Register_Start_ADDRESS + Register_WebServerPort_OFFSET);
|
||
if( (temp == 0x00) || (temp >= 0xFFFF) )
|
||
{
|
||
//·þÎñÆ÷¶Ë¿Ú²»ºÏ·¨£¬Ê¹ÓÃĬÈÏ¶Ë¿Ú 3339
|
||
server_info.dis_port = SERVER_COMM_Port;
|
||
}else {
|
||
server_info.dis_port = temp & 0xFFFF;
|
||
}
|
||
|
||
temp = SRAM_Read_Byte(SRAM_Register_Start_ADDRESS + Register_BLVServerDmName_OFFSET);
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"BLVServerDmName lens:%d",temp);
|
||
if(temp < DOMAINNAME_MAX)
|
||
{
|
||
memset(server_info.ServerDename,0,sizeof(server_info.ServerDename));
|
||
SRAM_DMA_Read_Buff((uint8_t*)server_info.ServerDename,temp,SRAM_Register_Start_ADDRESS + Register_BLVServerDmName_OFFSET);
|
||
server_info.ServerDename[temp] = '\0';
|
||
}
|
||
|
||
if((temp == 0x00) || (is_valid_domain((char *)server_info.ServerDename) != 0x00)) //ÓòÃû²»ºÏ·¨Ê¹ÓÃĬÈÏ
|
||
{
|
||
temp = sizeof(SERVER_NAME_DNS);
|
||
if(temp < DOMAINNAME_MAX)
|
||
{
|
||
memcpy(server_info.ServerDename,(char*)SERVER_NAME_DNS,sizeof(SERVER_NAME_DNS));
|
||
server_info.ServerDename[temp] = '\0';
|
||
}
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"BLVServerDmName err,use default:%s",server_info.ServerDename);
|
||
}
|
||
|
||
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"MCU³õʼIPµØÖ· : %d.%d.%d.%d",g_netinfo.device_ip[0],g_netinfo.device_ip[1],g_netinfo.device_ip[2],g_netinfo.device_ip[3]);
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"MCU³õÊ¼Íø¹Ø : %d.%d.%d.%d",g_netinfo.gateway[0],g_netinfo.gateway[1],g_netinfo.gateway[2],g_netinfo.gateway[3]);
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"MCU³õʼ×ÓÍøÑÚÂë : %d.%d.%d.%d",g_netinfo.subnet[0],g_netinfo.subnet[1],g_netinfo.subnet[2],g_netinfo.subnet[3]);
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"MCU DNSµØÖ· : %d.%d.%d.%d",g_netinfo.dns_server_ip[0],g_netinfo.dns_server_ip[1],g_netinfo.dns_server_ip[2],g_netinfo.dns_server_ip[3]);
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"TFTP Log Domain name:%s ",tftp_log.DomainName);
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"BLVServerDmName : %s:%d ",server_info.ServerDename,server_info.dis_port);
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"online_time:%d periodic_time:%d",server_info.udp_online_time,server_info.udp_periodic_time);
|
||
LOG_SYS_NET_Argc_Init_Record(g_netinfo.device_ip,g_netinfo.gateway,g_netinfo.subnet,g_netinfo.dns_server_ip,Arge_Flag,server_info.dhcp_en,server_info.server_select);
|
||
}
|
||
|
||
/*******************************************************************************
|
||
* Function Name : WCHNET_SocketInf_Printf
|
||
* Description : WCHÍøÂç¿â Ì×½Ó×Ö ²ÎÊýÐÅÏ¢´òÓ¡
|
||
*******************************************************************************/
|
||
__attribute__((section(".non_0_wait"))) void WCHNET_SocketInf_Printf(void)
|
||
{
|
||
for(uint8_t i=0;i<WCHNET_MAX_SOCKET_NUM;i++){
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"***************************");
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"ID:%d SocketID:%d ProtoType:%d Port:%d",i,SocketInf[i].SockIndex,SocketInf[i].ProtoType,SocketInf[i].DesPort);
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"***************************");
|
||
}
|
||
}
|
||
|
||
/*******************************************************************************
|
||
* Function Name : WCHNET_LIB_Init
|
||
* Description : WCHÍøÂç¿â ²ÎÊý - ³õʼ»¯
|
||
*******************************************************************************/
|
||
__attribute__((section(".non_0_wait"))) uint8_t WCHNET_LIB_Init(void)
|
||
{
|
||
uint8_t rev = 0;
|
||
|
||
server_info.init_flag = 0; //ÍøÂ翪ʼ³õʼ»¯
|
||
server_info.register_num = 0;
|
||
|
||
server_info.dhcp_flg = 0;
|
||
server_info.server_dns_flag = 0;
|
||
server_info.tftp_dns_flag = 0;
|
||
server_info.mqtt_dns_flag = 0;
|
||
server_info.udp_sta = 0x01;
|
||
server_info.Udp_Internal_sta = STA_INIT; //UDPÌ×½Ó×Ö״̬
|
||
server_info.online_state = 0; //2023-10-07
|
||
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"WCHNET version:%x\n", WCHNET_GetVer());
|
||
if (WCHNET_LIB_VER != WCHNET_GetVer()) {
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"version error.\n");
|
||
}
|
||
|
||
//¶ÁȡоƬ×Ô´øµÄMAC µØÖ·
|
||
// GetMACAddress(MACAddr);
|
||
// Dbg_Print_Buff(DBG_BIT_NET_STATUS_bit, "NET MAC:", MACAddr, 6);
|
||
|
||
NetWork_Parameter_Get();
|
||
|
||
TIM2_Init();
|
||
rev = ETH_LibInit(g_netinfo.device_ip, g_netinfo.gateway, g_netinfo.subnet, g_netinfo.mac_addr); //Ethernet library initialize
|
||
mStopIfError(rev);
|
||
if (rev != WCHNET_ERR_SUCCESS) {
|
||
printf("WCHNET_LibInit Fail\r\n");
|
||
return 0x01;
|
||
}
|
||
|
||
printf("WCHNET_LibInit Success\r\n");
|
||
|
||
#if KEEPALIVE_ENABLE //Configure keep alive parameters
|
||
{
|
||
struct _KEEP_CFG cfg;
|
||
|
||
cfg.KLIdle = 20000;
|
||
cfg.KLIntvl = 15000;
|
||
cfg.KLCount = 9;
|
||
WCHNET_ConfigKeepLive(&cfg);
|
||
}
|
||
#endif
|
||
|
||
server_info.wait_cot = SysTick_1ms;
|
||
server_info.con_tick = SysTick_1s;
|
||
|
||
server_info.udp_sta = 0x01;
|
||
server_info.register_num = 0x00;
|
||
server_info.udp_timesync_cnt = 0x00;
|
||
server_info.udp_periodic_cnt = 0x00;
|
||
server_info.udp_idle_tick = SysTick_1ms;
|
||
server_info.udp_send_flag = 0x00;
|
||
|
||
server_info.net_sta = NET_PHY_WAIT;
|
||
|
||
return 0;
|
||
}
|
||
|
||
__attribute__((section(".non_0_wait"))) void NetWork_Task(void)
|
||
{
|
||
switch(server_info.net_sta)
|
||
{
|
||
case NET_INIT:
|
||
/*WCH¿â ³õʼ»¯»áÖØÆôMAC,ËùÒÔÎÞÐèÔٴθ´Î»MAC*/
|
||
WCHNET_LIB_Init();
|
||
break;
|
||
case NET_PHY_WAIT: //µÈ´ýPHYÐÉÌ£¬Èç¹ûÐÉ̲»³É¹¦£¬ÍøÂ罫²»¿ÉÓÃ
|
||
if( WCHNET_Get_PHY_Linked_Status() == 0x01 )
|
||
{
|
||
if(SysTick_1ms - server_info.wait_cot > 30000)
|
||
{
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"ÍøÂçPHYÐÉÌʧ°Ü£¬×¼±¸ÖØÐ³õʼ»¯ÍøÂç");
|
||
server_info.net_sta = NET_WAIT_MAC_RESTART;
|
||
}
|
||
break;
|
||
}else {
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"ÍøÏß²åÈëÒѲåÈ룬´´½¨Ö±Á¬Ì×½Ó×Ö");
|
||
|
||
WCHNET_CreateUdpSocket(&g_netinfo.SocketId[SocketIdnex_BLVSeriver], 3341, UDPSocket1_AppCallBack); //´´½¨Ì×½Ó×Ö,¾ÖÓòÍøµÈ´ý½ÓÊÕÉý¼¶»òÅäÖ÷¢²¼µÈÃüÁî
|
||
server_info.online_state = 1; //2023-10-07
|
||
server_info.wait_cot = SysTick_1ms;
|
||
server_info.net_sta = NET_CON_WAIT;
|
||
LOG_SYS_PHY_Change_Record(0x01); //ÍøÏß²åÈë
|
||
}
|
||
break;
|
||
case NET_WAIT_MAC_RESTART:
|
||
if(SysTick_1ms - server_info.wait_cot > 2000)
|
||
{
|
||
server_info.wait_cot = SysTick_1ms;
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"ÖØÆôÍøÂçÖÐ");
|
||
server_info.net_sta = NET_INIT;
|
||
}
|
||
break;
|
||
case NET_CON_WAIT:
|
||
if(SysTick_1ms - server_info.wait_cot > 200)
|
||
{
|
||
server_info.wait_cot = SysTick_1ms;
|
||
if(server_info.dhcp_en == 0x01)
|
||
{
|
||
server_info.net_sta = NET_DHCP;
|
||
}else {
|
||
server_info.net_sta = NET_DNS;
|
||
}
|
||
}
|
||
break;
|
||
case NET_DHCP: //Æô¶¯DHCP·ÖÅäip
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"Æô¶¯DHCP·ÖÅä...");
|
||
|
||
WCHNET_DHCPSetHostname("BLV_RCU");
|
||
WCHNET_DHCPStart(WCHNET_DHCPCallBack); /* Æô¶¯DHCP */
|
||
|
||
server_info.wait_cot = SysTick_1ms;
|
||
server_info.net_sta = NET_DHCP_WAIT;
|
||
break;
|
||
case NET_DHCP_WAIT:
|
||
if(SysTick_1ms - server_info.wait_cot >= 20000)
|
||
{
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"DHCP ºÄʱ£º%dms", SysTick_1ms - server_info.wait_cot);
|
||
server_info.wait_cot = SysTick_1ms;
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"DHCP»ñȡʧ°Ü!");
|
||
WCHNET_DHCPStop();
|
||
server_info.net_retry_num++;
|
||
server_info.net_sta = NET_DHCP;
|
||
|
||
if(server_info.net_retry_num >= 3)
|
||
{
|
||
server_info.net_retry_num = 0;
|
||
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"DHCPÖØÊÔ´ÎÊýÒÑ´ïÉÏÏÞ!");
|
||
server_info.net_sta = NET_INIT; //DHCP·ÖÅäʧ°Ü£¬Ö±½ÓÖØÐ³õʼ»¯ÍøÂç;
|
||
|
||
WCHNET_SocketClose(g_netinfo.SocketId[SocketIdnex_BLVSeriver],0x00);
|
||
}
|
||
}
|
||
|
||
/* Èç¹ûÔÙ´ÎÆÚ¼ä°Î³öÍøÏߣ¬PHYÁ´½Ó¶Ï¿ª£¬ÐèÖØÐ³õʼ»¯ÍøÂç
|
||
* - ÔÚÖØÐ³õʼ»¯ÍøÂçǰ£¬ÐèҪֹͣDHCP²Ù×÷
|
||
* */
|
||
break;
|
||
case NET_SOCKET_WAIT:
|
||
if(SysTick_1ms - server_info.wait_cot >= 200)
|
||
{
|
||
Dbg_Print(DBG_BIT_NET_STATUS_bit,"DHCP³É¹¦£¬ÖØÐ´´½¨Í¨Ñ¶Ì×½Ó×Ö\n");
|
||
WCHNET_CreateUdpSocket(&g_netinfo.SocketId[SocketIdnex_BLVSeriver], 3341, UDPSocket1_AppCallBack); //´´½¨Ì×½Ó×Ö,¾ÖÓòÍøµÈ´ý½ÓÊÕÉý¼¶»òÅäÖ÷¢²¼µÈÃüÁî
|
||
WCHNET_SocketInf_Printf();
|
||
server_info.net_sta = NET_DNS;
|
||
}
|
||
break;
|
||
case NET_DNS:
|
||
if(server_info.server_select == 0x00)
|
||
{
|
||
/*½âÎö·þÎñÆ÷ÓòÃû */
|
||
Dbg_Print(DBG_BIT_NET_STATUS_bit,"Æô¶¯DNSÓòÃû½âÎö:%s Flag:%d...\n",server_info.ServerDename,server_info.net_retry_flag);
|
||
|
||
//Set DNS server IP address, and DNS server port is 53
|
||
if(server_info.net_retry_flag == 0x00) {
|
||
|
||
Dbg_Print(DBG_BIT_NET_STATUS_bit,"DNS½âÎö·þÎñÆ÷:%d.%d.%d.%d\n",g_netinfo.dns_server_ip[0],\
|
||
g_netinfo.dns_server_ip[1],\
|
||
g_netinfo.dns_server_ip[2],\
|
||
g_netinfo.dns_server_ip[3]);
|
||
|
||
WCHNET_InitDNS(g_netinfo.dns_server_ip, Net_DNS_Port);
|
||
}else if(server_info.net_retry_flag == 0x01) {
|
||
|
||
Dbg_Print(DBG_BIT_NET_STATUS_bit,"DNS½âÎö·þÎñÆ÷:%d.%d.%d.%d\n",g_netinfo.dns_server2_ip[0],\
|
||
g_netinfo.dns_server2_ip[1],\
|
||
g_netinfo.dns_server2_ip[2],\
|
||
g_netinfo.dns_server2_ip[3]);
|
||
|
||
WCHNET_InitDNS(g_netinfo.dns_server2_ip, Net_DNS_Port);
|
||
}
|
||
|
||
server_info.dns_sta = 0x01;
|
||
WCHNET_HostNameGetIp(server_info.ServerDename, RemoteIp, WCHNET_DNSCallBack_1, NULL); //Start DNS
|
||
|
||
server_info.wait_cot = SysTick_1ms;
|
||
server_info.net_sta = NET_DNS_WAIT;
|
||
}else if(server_info.server_select == 0x01)
|
||
{
|
||
/*²ÉÓñ¾µØ·þÎñÆ÷ - Ö±½ÓÉèÖ÷þÎñÆ÷IPµØÖ·£¬Ìø¹ý½âÎö·þÎñÆ÷ÓòÃûÕâÒ»²½£¬½âÎöÓòÃû±ê־λÔÝʱûÓÃÉÏ*/
|
||
|
||
server_info.dis_ip[0] = SRAM_Read_Byte(SRAM_Register_Start_ADDRESS + Register_WebServerIP_OFFSET + 0);
|
||
server_info.dis_ip[1] = SRAM_Read_Byte(SRAM_Register_Start_ADDRESS + Register_WebServerIP_OFFSET + 1);
|
||
server_info.dis_ip[2] = SRAM_Read_Byte(SRAM_Register_Start_ADDRESS + Register_WebServerIP_OFFSET + 2);
|
||
server_info.dis_ip[3] = SRAM_Read_Byte(SRAM_Register_Start_ADDRESS + Register_WebServerIP_OFFSET + 3);
|
||
|
||
server_info.server_dns_flag = 0x00;
|
||
server_info.net_sta = NET_TFTP;
|
||
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"²ÉÓñ¾µØ·þÎñÆ÷:%d.%d.%d.%d",server_info.dis_ip[0],server_info.dis_ip[1],server_info.dis_ip[2],server_info.dis_ip[3]);
|
||
}
|
||
break;
|
||
case NET_TFTP:
|
||
Dbg_Print(DBG_BIT_NET_STATUS_bit,"Æô¶¯DNSÓòÃû½âÎö:%s Flag:%d...\n",tftp_log.DomainName,server_info.net_retry_flag);
|
||
|
||
//Set DNS server IP address, and DNS server port is 53
|
||
if(server_info.net_retry_flag == 0x00) {
|
||
|
||
Dbg_Print(DBG_BIT_NET_STATUS_bit,"DNS½âÎö·þÎñÆ÷:%d.%d.%d.%d\n",g_netinfo.dns_server_ip[0],\
|
||
g_netinfo.dns_server_ip[1],\
|
||
g_netinfo.dns_server_ip[2],\
|
||
g_netinfo.dns_server_ip[3]);
|
||
|
||
WCHNET_InitDNS(g_netinfo.dns_server_ip, Net_DNS_Port);
|
||
}else if(server_info.net_retry_flag == 0x01) {
|
||
|
||
Dbg_Print(DBG_BIT_NET_STATUS_bit,"DNS½âÎö·þÎñÆ÷:%d.%d.%d.%d\n",g_netinfo.dns_server2_ip[0],\
|
||
g_netinfo.dns_server2_ip[1],\
|
||
g_netinfo.dns_server2_ip[2],\
|
||
g_netinfo.dns_server2_ip[3]);
|
||
|
||
WCHNET_InitDNS(g_netinfo.dns_server2_ip, Net_DNS_Port);
|
||
}
|
||
|
||
server_info.dns_sta = 0x02;
|
||
WCHNET_HostNameGetIp(tftp_log.DomainName, RemoteIp, WCHNET_DNSCallBack_2, NULL); //Start DNS
|
||
server_info.wait_cot = SysTick_1ms;
|
||
server_info.net_sta = NET_DNS_WAIT;
|
||
|
||
break;
|
||
case NET_DNS_WAIT:
|
||
if(SysTick_1ms - server_info.wait_cot >= 5000)
|
||
{
|
||
server_info.wait_cot = SysTick_1ms;
|
||
|
||
switch(server_info.dns_sta)
|
||
{
|
||
case 0x01:
|
||
if(server_info.net_retry_num >= 3)
|
||
{
|
||
server_info.net_retry_num = 0;
|
||
|
||
if(server_info.net_retry_flag == 0x00)
|
||
{
|
||
//»»¸öDNS·þÎñÆ÷²âÊÔÒ»ÏÂ
|
||
server_info.net_retry_flag = 0x01;
|
||
server_info.net_sta = NET_DNS;
|
||
}else {
|
||
//»¹ÊǽâÎö²»³öÀ´£¬ÄÇËãÁË£¬ÏÂÒ»¸ö
|
||
server_info.net_retry_flag = 0x00;
|
||
server_info.net_sta = NET_TFTP;
|
||
}
|
||
|
||
WCHNET_DNSStop(); //stop DNS,and release socket
|
||
}else {
|
||
server_info.net_retry_num++;
|
||
server_info.net_sta = NET_DNS;
|
||
|
||
WCHNET_DNSStop(); //stop DNS,and release socket
|
||
}
|
||
break;
|
||
|
||
case 0x02:
|
||
if(server_info.net_retry_num >= 3)
|
||
{
|
||
server_info.net_retry_num = 0;
|
||
|
||
if(server_info.net_retry_flag == 0x00)
|
||
{
|
||
//»»¸öDNS·þÎñÆ÷²âÊÔÒ»ÏÂ
|
||
server_info.net_retry_flag = 0x01;
|
||
server_info.net_sta = NET_TFTP;
|
||
}else {
|
||
//»¹ÊǽâÎö²»³öÀ´£¬ÄÇËãÁË
|
||
server_info.net_retry_flag = 0x00;
|
||
server_info.net_sta = NET_COMPLETE;
|
||
server_info.init_flag = 0x01;
|
||
WCHNET_SocketInf_Printf();
|
||
}
|
||
|
||
WCHNET_DNSStop(); //stop DNS,and release socket
|
||
}else {
|
||
server_info.net_retry_num++;
|
||
server_info.net_sta = NET_TFTP;
|
||
|
||
WCHNET_DNSStop(); //stop DNS,and release socket
|
||
}
|
||
break;
|
||
default: //Èç¹û»¹ÓÐÆäËûÓòÃû½âÎöÒÀ´ÎÏòºóÌí¼Ó¼´¿É
|
||
break;
|
||
}
|
||
}
|
||
|
||
/* Èç¹ûÔÙ´ÎÆÚ¼ä°Î³öÍøÏߣ¬PHYÁ´½Ó¶Ï¿ª£¬ÐèÖØÐ³õʼ»¯ÍøÂç
|
||
* - ÔÚÖØÐ³õʼ»¯ÍøÂçǰ£¬ÐèҪֹͣDNS²Ù×÷
|
||
* */
|
||
break;
|
||
case NET_COMPLETE:
|
||
|
||
Internal_TFTP_Task(); //TFTP ÈÎÎñ
|
||
|
||
BLV_UDP_Comm_Task(); //BLV UDPͨѶÈÎÎñ
|
||
|
||
//Udp_Internal_Task(); //ÄÚÍø´¦ÀíÈÎÎñ£¨Éý¼¶£¬·¢²¼ÅäÖã©
|
||
|
||
if(SysTick_1s - server_info.con_tick > 10)
|
||
{
|
||
//²âÊÔʹÓÃ
|
||
server_info.con_tick = SysTick_1s;
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"ÍøÂç´¦ÀíÖÐ...\n");
|
||
}
|
||
|
||
/* -¸ÃÓÃÓÚÍøÂçÖ±Á¬³¬Ê±ºó£¬ÖØÆôÍøÂç
|
||
* -´¦ÓÚÍøÂçÖ±Á¬×´Ì¬Ï£¬PC¹¤¾ß»áÏòÖ÷»ú¶¨ÆÚ·¢ËͲéѯÊý¾Ý
|
||
* */
|
||
if(server_info.con_flag==0x01)
|
||
{
|
||
if(SysTick_1s - server_info.con_tick > 10)
|
||
{
|
||
server_info.con_flag = 0x00;
|
||
server_info.con_tick = SysTick_1s;
|
||
server_info.init_flag = 0x00;
|
||
Dbg_Print(DBG_BIT_NET_STATUS_bit,"Ö±Á¬³¬Ê±£¬ÖØÆôÍøÂç\n");
|
||
}
|
||
}
|
||
|
||
if(server_info.init_flag == 0x00) //ÍøÂçÐèÒªÖØÐ³õʼ»¯
|
||
{
|
||
Dbg_Print(DBG_BIT_NET_STATUS_bit,"ÍøÂçÐèÒªÖØÐ³õʼ»¯£¬¹Ø±ÕMAC\n");
|
||
|
||
server_info.online_state = 0;
|
||
server_info.wait_cot = SysTick_1ms;
|
||
server_info.net_sta = NET_WAIT_MAC_RESTART;
|
||
|
||
/*ÊÍ·ÅÌ×½Ó×Ö*/
|
||
Dbg_Println(DBG_BIT_NET_STATUS_bit,"ÊÍ·ÅÌ×½Ó×Ö");
|
||
WCHNET_SocketClose(g_netinfo.SocketId[SocketIdnex_BLVSeriver],0x00);
|
||
|
||
}
|
||
break;
|
||
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
|