📄 net.c
字号:
#include "CommonDef.h"
#include "Net.h"
#pragma DATA_SECTION(dataBuf, "DATABUFFER")
#pragma DATA_ALIGN(dataBuf, 8)
far char dataBuf[5242880];
extern void NormCmdHandle();
//extern void IPHandle();
//extern void PrioCmdHandle();
extern void SerialHandle();
volatile int g_nNormStatus = 0;
volatile int g_nPrioStatus = 0;
volatile int g_nNeedReply = 0;
volatile int g_nMakeCfgStatus = 0;
volatile int g_nNetStackStoped = 0;
volatile int g_nDhcpEnable = 0;
//volatile int g_nDhcpStatus = 0;
volatile unsigned int g_nDhcpIpAddress = 0;
volatile int g_nNetOK = 0;
CONFIG_HDR g_cfgIPHeader;
//判断网络是否是由网络连接断状态恢复的
volatile int g_nNetLinkStatus = 0;
static unsigned char strMainConfig[512];
static int nMainConfigSize = 0;
//static HANDLE hPrioHandle = NULL;
static HANDLE hNormHandle = NULL;
//static HANDLE hIPHandle = NULL;
//static HANDLE hSerialHandle = NULL;
// The callback Functions by the network driver.
CSLAPI unsigned int MDIO_phyRegWrite( unsigned int phyIdx, unsigned int phyReg, unsigned short data );
CSLAPI unsigned int MDIO_phyRegRead( unsigned int, unsigned int, unsigned int* );
static char *LinkStr[] = { "No Link",
"10Mb/s Half Duplex",
"10Mb/s Full Duplex",
"100Mb/s Half Duplex",
"100Mb/s Full Duplex" };
static unsigned char def_MacAddr[8] = {0x00,0x00,0x55,0x66,0x77,0x88};
static char *TaskName[] = { "Telnet","HTTP","NAT","DHCPS","DHCPC","DNS" };
static char *ReportStr[] = { "","Running","Updated","Complete","Fault" };
static char *StatusStr[] = { "Disabled","Waiting","IPTerm","Failed","Enabled" };
char *DNSServer = "0.0.0.0";
//Create ram base html
static void AddWebFiles()
{
return;
}
static void RemoveWebFiles()
{
return;
}
static void ServiceReport(unsigned int Item, unsigned int Status, unsigned int Report, HANDLE h)
{
#ifdef _DEBUG
printf( "Service Status: %-9s: %-9s: %-9s: %03d\n",
TaskName[Item-1], StatusStr[Status],
ReportStr[Report/256], Report&0xFF );
#endif
return;
}
//This is a callback from the Ethernet driver. This function
void DM642EMAC_getConfig(unsigned char *pMacAddr, unsigned int *pIntVector)
{
if(g_pEthInfo != NULL)
{
#ifdef _DEBUG
printf("Using MAC Address: %02x-%02x-%02x-%02x-%02x-%02x\n",
g_pEthInfo->bMacAddr[0], g_pEthInfo->bMacAddr[1],
g_pEthInfo->bMacAddr[2], g_pEthInfo->bMacAddr[3],
g_pEthInfo->bMacAddr[4], g_pEthInfo->bMacAddr[5]);
#endif
//We fill in the two pointers here. We'll use int 15 for EMAC
mmCopy(pMacAddr, g_pEthInfo->bMacAddr, 6);
}
else
{
#ifdef _DEBUG
printf("Using MAC Address: %02x-%02x-%02x-%02x-%02x-%02x\n",
def_MacAddr[0], def_MacAddr[1], def_MacAddr[2],
def_MacAddr[3], def_MacAddr[4], def_MacAddr[5]);
#endif
//We fill in the two pointers here. We'll use int 15 for EMAC
mmCopy( pMacAddr, def_MacAddr, 6 );
}
*pIntVector = 15;
}
//This is a callback from the Ethernet driver. This function
void DM642EMAC_linkStatus(unsigned int phy, unsigned int linkStatus)
{
if(linkStatus)
{
g_nNetOK = 1;
}
else
{
if(g_nNetOK)
{
g_nNetLinkStatus = 1;
}
g_nNetOK = 0;
}
#ifdef _DEBUG
printf("Link Status: %s\n",LinkStr[linkStatus]);
#endif
//Since we know we're using the Intel PHY, we'll go ahead and program the LEDs to make sense
MDIO_phyRegWrite(phy, 0x14, 0xd5d0);
}
void NetworkOpen()
{
hNormHandle = TaskCreate(NormCmdHandle, "Normal", OS_TASKPRIHIGH_DEF, 0x4000, 0, 0, 0);
// hPrioHandle = TaskCreate(PrioCmdHandle, "Prior", OS_TASKPRIHIGH_DEF, 0x4000, 0, 0, 0);
// hIPHandle = TaskCreate(IPHandle, "IP", OS_TASKPRIHIGH_DEF, 0x4000, 0, 0, 0);
// hSerialHandle = TaskCreate(SerialHandle, "Serial", OS_TASKPRILOW_DEF, 0x4000, 0, 0, 0);
}
void NetworkClose()
{
#ifdef _DEBUG
printf("Net work closed\n");
#endif
fdCloseSession(hNormHandle);
// fdCloseSession(hSerialHandle);
TaskDestroy(hNormHandle);
// TaskDestroy(hSerialHandle);
}
void NetworkIPAddr(IPN IPAddr, unsigned int IfIdx, unsigned int fAdd)
{
IPN IPTmp;
IPTmp = ntohl(IPAddr);
if(fAdd)
{
#ifdef _DEBUG
printf("Network Added: ");
#endif
g_nDhcpEnable = 0;
g_cfgIPHeader.c_ip = htonl(IPAddr);
NtIPN2Str(IPAddr, g_pEthInfo->strLocalIPAddr);
}
else
{
#ifdef _DEBUG
printf("Network Removed: ");
#endif
}
#ifdef _DEBUG
printf("If-%d:%d.%d.%d.%d\n", IfIdx,
(unsigned char)(IPTmp>>24)&0xFF, (unsigned char)(IPTmp>>16)&0xFF,
(unsigned char)(IPTmp>>8)&0xFF, (unsigned char)IPTmp&0xFF );
#endif
}
// The net stack initialize functions
void NetInit()
{
int rc;
HANDLE hCfg;
rc = NC_SystemOpen(NC_PRIORITY_LOW, NC_OPMODE_INTERRUPT);
if( rc )
{
#ifdef _DEBUG
printf("NC_SystemOpen Failed (%d)\n",rc);
#endif
for(;;);
}
hCfg = CfgNew();
if(!hCfg )
{
#ifdef _DEBUG
printf("Unable to create configuration\n");
#endif
goto main_exit;
}
if( strlen(g_pEthInfo->strDomainName) >= CFG_DOMAIN_MAX ||
strlen(g_pEthInfo->strHostName) >= CFG_HOSTNAME_MAX )
{
printf("Names too long\n");
goto main_exit;
}
if(CfgAddEntry(hCfg, CFGTAG_SYSINFO, CFGITEM_DHCP_HOSTNAME, 0, strlen(g_pEthInfo->strHostName), (unsigned char*)g_pEthInfo->strHostName, 0) < 0)
goto main_exit;
AddWebFiles();
//Config http service
{
CI_SERVICE_HTTP http;
bzero(&http, sizeof(http));
http.cisargs.IPAddr = INADDR_ANY;
http.cisargs.pCbSrv = &ServiceReport;
if(CfgAddEntry(hCfg, CFGTAG_SERVICE, CFGITEM_SERVICE_HTTP, 0, sizeof(http), (unsigned char*)&http, 0) < 0)
goto main_exit;
}
rc = DBG_WARN;
if(CfgAddEntry(hCfg, CFGTAG_OS, CFGITEM_OS_DBGPRINTLEVEL, CFG_ADDMODE_UNIQUE, sizeof(unsigned int), (unsigned char*)&rc, 0 ) < 0)
goto main_exit;
rc = 8192;
if(CfgAddEntry(hCfg, CFGTAG_IP, CFGITEM_IP_SOCKTCPTXBUF, CFG_ADDMODE_UNIQUE, sizeof(unsigned int), (unsigned char*)&rc, 0) < 0)
goto main_exit;
rc = 8192;
if(CfgAddEntry(hCfg, CFGTAG_IP, CFGITEM_IP_SOCKTCPRXBUF, CFG_ADDMODE_UNIQUE, sizeof(unsigned int), (unsigned char*)&rc, 0 ) < 0)
goto main_exit;
//Save the configuration to a linear buffer
nMainConfigSize = 0;
CfgSave(hCfg, &nMainConfigSize, 0);
if(nMainConfigSize > sizeof(strMainConfig))
{
#ifdef _DEBUG
printf("FATAL: Config buffer too small\n");
#endif
CfgFree(hCfg);
goto main_exit;
}
else
{
if(CfgSave(hCfg, &nMainConfigSize, strMainConfig) <= 0)
goto main_exit;
CfgFree(hCfg);
}
//Start to config ip address
{
CI_IPNET NA;
CI_ROUTE RT;
IPN IPTmp;
HANDLE hCfg2;
do
{
hCfg2 = CfgNew();
if(!hCfg2 )
{
#ifdef _DEBUG
printf("Unable to create configuration\n");
#endif
goto main_exit;
}
if(CfgLoad(hCfg2, nMainConfigSize, strMainConfig) != nMainConfigSize)
goto main_exit;
if(g_nDhcpEnable == 1)
{
CI_SERVICE_DHCPC dhcpc;
//Specify DHCP Service on IF-1
bzero(&dhcpc, sizeof(dhcpc));
dhcpc.cisargs.Mode = CIS_FLG_IFIDXVALID;
dhcpc.cisargs.IfIdx = 1;
dhcpc.cisargs.pCbSrv = &ServiceReport;
if(CfgAddEntry(hCfg2, CFGTAG_SERVICE, CFGITEM_SERVICE_DHCPCLIENT, 0, sizeof(dhcpc), (UINT8 *)&dhcpc, 0) < 0)
goto main_exit;
g_nMakeCfgStatus = 1;
}
else
{
bzero(&NA, sizeof(NA));
NA.IPAddr = inet_addr(g_pEthInfo->strLocalIPAddr);
NA.IPMask = inet_addr("255.0.0.0");
strcpy(NA.Domain, g_pEthInfo->strDomainName);
NA.NetType = 0;
//Add the address to interface 1
if(CfgAddEntry(hCfg2, CFGTAG_IPNET, 1, 0, sizeof(CI_IPNET), (unsigned char *)&NA, 0) < 0)
goto main_exit;
bzero( &RT, sizeof(RT) );
RT.IPDestAddr = 0;
RT.IPDestMask = 0;
RT.IPGateAddr = inet_addr(g_pEthInfo->strGatewayIP);
if(CfgAddEntry(hCfg2, CFGTAG_ROUTE, 0, 0, sizeof(CI_ROUTE), (unsigned char*)&RT, 0) < 0)
goto main_exit;
IPTmp = inet_addr(DNSServer);
if(IPTmp)
if(CfgAddEntry(hCfg2, CFGTAG_SYSINFO, CFGITEM_DHCP_DOMAINNAMESERVER, 0, sizeof(IPTmp), (unsigned char*)&IPTmp, 0) < 0)
goto main_exit;
//Change the config status
g_nMakeCfgStatus = 1;
}
rc = NC_NetStart(hCfg2, NetworkOpen, NetworkClose, NetworkIPAddr);
CfgFree(hCfg2);
} while( rc > 0 );
}
RemoveWebFiles();
main_exit:
NC_SystemClose();
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -