📄 helloworld.c
字号:
/* * Copyright 2007 by Texas Instruments Incorporated. * All rights reserved. Property of Texas Instruments Incorporated. * Restricted rights to use, duplicate or disclose this code are * granted through contract. * * @(#) TCP/IP_Network_Developers_Kit 1.92.00.22 01-10-2007 (ndk-b22) *//*^M * ======== helloWorld.c ========^M *^M * *///--------------------------------------------------------------------------// IP Stack 'Hello World!' Example//// This is a skeleton application, intended to provide application // programmers with a basic Stack setup, to which they can start // adding their code.//// To test it as is, use with helloWorld.exe from \winapps directory////--------------------------------------------------------------------------// helloWorld.c//// Stack initialization functions//// Author: Michael A. Denio// Magdalena B. Iovescu// Copyright 2000, 2001 by Texas Instruments Inc.//--------------------------------------------------------------------------#include <std.h>#include <stdio.h>#include <c62.h>#include <netmain.h>//---------------------------------------------------------------------------// Title String//char *VerStr = "\nTCP/IP Stack 'Hello World!' Application\n\n";// Our NETCTRL callback functionsstatic void NetworkOpen();static void NetworkClose();static void NetworkIPAddr( IPN IPAddr, uint IfIdx, uint fAdd );// Fun reporting functionstatic void ServiceReport( uint Item, uint Status, uint Report, HANDLE hCfgEntry );// External referencesextern int dtask_udp_hello();//---------------------------------------------------------------------------// Configuration//char *HostName = "tidsp";char *LocalIPAddr = "0.0.0.0"; // Set to "0.0.0.0" for DHCPchar *LocalIPMask = "255.255.254.0"; // Not used when using DHCPchar *GatewayIP = "0.0.0.0"; // Not used when using DHCPchar *DomainName = "demo.net"; // Not used when using DHCPchar *DNSServer = "0.0.0.0"; // Used when set to anything but zero//---------------------------------------------------------------------// Main Entry Point//---------------------------------------------------------------------int main(){}// This callback function is called by the evaluation stack 5 min before the// evaluation period times out.void evalCallBack(){ printf("The Stack is going to shutdown in 5 min\n");}//// Main Thread 主要的进程,优先级定为5//int StackTest(){ int rc; HANDLE hCfg; // // THIS MUST BE THE ABSOLUTE FIRST THING DONE IN AN APPLICATION!! // rc = NC_SystemOpen( NC_PRIORITY_LOW, NC_OPMODE_INTERRUPT );//优先级为最低,采用中断调用模式 if( rc ) { printf("NC_SystemOpen Failed (%d)\n",rc); for(;;); //进入空循环 } // Print out our banner(标语) printf(VerStr); // // Create and build the system configuration from scratch. // // Create a new configuration hCfg = CfgNew(); if( !hCfg ) { printf("Unable to create configuration\n"); goto main_exit; } // The evaluation version of TCP/IP Stack restricts usage of stack !!!! // to maximum of 24 Hours. If application wants to be notified 5 min // before the timeout, it can register a callback function by using // the following configuration code section.导入配置参数 { void (*pFxn)() = &evalCallBack;//pFxn为指向函数的指针,改函数返回一个空值 CfgAddEntry( hCfg, CFGTAG_SYSINFO, CFGITEM_SYSINFO_EVALCALLBACK, 0, sizeof(void(*)()), (UINT8*) &pFxn, 0 ); } // We better validate the length of the supplied names if( strlen( DomainName ) >= CFG_DOMAIN_MAX || strlen( HostName ) >= CFG_HOSTNAME_MAX ) { printf("Names too long\n"); goto main_exit; } // Add our global hostname to hCfg (to be claimed in all connected domains) CfgAddEntry( hCfg, CFGTAG_SYSINFO, CFGITEM_DHCP_HOSTNAME, 0, strlen(HostName), (UINT8 *)HostName, 0 ); // If the IP address is specified, manually configure IP and Gateway设置IP和网关 if( inet_addr(LocalIPAddr) ) //返回值为1则IP地址分配成功 { CI_IPNET NA; //IPNET例程 CI_ROUTE RT; //路由例程 IPN IPTmp; // Setup manual IP address bzero( &NA, sizeof(NA) ); // ?????不知道是什么,可能是把NA中全部清零 NA.IPAddr = inet_addr(LocalIPAddr); NA.IPMask = inet_addr(LocalIPMask); strcpy( NA.Domain, DomainName ); NA.NetType = 0; // Add the address to interface 1,即ipnetwork 1 CfgAddEntry( hCfg, CFGTAG_IPNET, 1, 0, sizeof(CI_IPNET), (UINT8 *)&NA, 0 );// Address created by DHCP CLIENT // Add the default gateway. Since it is the default, the // destination address and mask are both zero (we go ahead // and show the assignment for clarity). bzero( &RT, sizeof(RT) ); RT.IPDestAddr = 0; RT.IPDestMask = 0; RT.IPGateAddr = inet_addr(GatewayIP); // Add the route CfgAddEntry( hCfg, CFGTAG_ROUTE, 0, 0, //增加静态路由 sizeof(CI_ROUTE), (UINT8 *)&RT, 0 ); // Manually add the DNS server when specified//添加 Stack's DNS servers IPTmp = inet_addr(DNSServer); if( IPTmp ) CfgAddEntry( hCfg, CFGTAG_SYSINFO, CFGITEM_DHCP_DOMAINNAMESERVER, 0, sizeof(IPTmp), (UINT8 *)&IPTmp, 0 ); } // Else we specify DHCP else { 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; CfgAddEntry( hCfg, CFGTAG_SERVICE, CFGITEM_SERVICE_DHCPCLIENT, 0, sizeof(dhcpc), (UINT8 *)&dhcpc, 0 ); } // // Configure IPStack/OS Options,配置IPStack/OS 选项 // // We don't want to see debug messages less than WARNINGS rc = DBG_WARN; CfgAddEntry( hCfg, CFGTAG_OS, CFGITEM_OS_DBGPRINTLEVEL, CFG_ADDMODE_UNIQUE, sizeof(uint), (UINT8 *)&rc, 0 ); // // This code sets up the TCP and UDP buffer sizes,配置TCP和UDP缓存大小 // (Note 8192 is actually the default. This code is here to // illustrate how the buffer and limit sizes are configured.) // // UDP Receive limit rc = 8192; CfgAddEntry( hCfg, CFGTAG_IP, CFGITEM_IP_SOCKUDPRXLIMIT, CFG_ADDMODE_UNIQUE, sizeof(uint), (UINT8 *)&rc, 0 );//到这里位置系统配置完毕 // // Boot the system using this configuration // // We keep booting until the function returns 0. This allows // us to have a "reboot" command. 开启stack,即唤醒network // do { rc = NC_NetStart( hCfg, NetworkOpen, NetworkClose, NetworkIPAddr ); } while( rc > 0 ); // Delete Configuration CfgFree( hCfg ); // Close the OSmain_exit: NC_SystemClose(); return(0);}//// System Task Code [ Server Daemon Servers ]//static HANDLE hHello=0;//// NetworkOpen//// This function is called after the configuration has booted//static void NetworkOpen(){ // Create our local server创建一个TCP或UDP服务器入口,该服务即为 udpHello.c中内容 hHello = DaemonNew( SOCK_DGRAM, 0, 7, dtask_udp_hello, OS_TASKPRINORM, OS_TASKSTKNORM, 0, 1 );}//// NetworkClose//// This function is called when the network is shutting down,// or when it no longer has any IP addresses assigned to it.//static void NetworkClose(){ DaemonFree( hHello );}//// NetworkIPAddr//// This function is called whenever an IP address binding is// added or removed from the system.//static void NetworkIPAddr( IPN IPAddr, uint IfIdx, uint fAdd ){ IPN IPTmp; if( fAdd ) printf("Network Added: "); else printf("Network Removed: "); // Print a message IPTmp = ntohl( IPAddr ); printf("If-%d:%d.%d.%d.%d\n", IfIdx, (UINT8)(IPTmp>>24)&0xFF, (UINT8)(IPTmp>>16)&0xFF, (UINT8)(IPTmp>>8)&0xFF, (UINT8)IPTmp&0xFF );}//// Service Status Reports//// Here's a quick example of using service status updates//static char *TaskName[] = { "Telnet","HTTP","NAT","DHCPS","DHCPC","DNS" };static char *ReportStr[] = { "","Running","Updated","Complete","Fault" };static char *StatusStr[] = { "Disabled","Waiting","IPTerm","Failed","Enabled" };static void ServiceReport( uint Item, uint Status, uint Report, HANDLE h ){ printf( "Service Status: %-9s: %-9s: %-9s: %03d\n", TaskName[Item-1], StatusStr[Status], ReportStr[Report/256], Report&0xFF ); // // Example of adding to the DHCP configuration space // // When using the DHCP client, the client has full control over access // to the first 256 entries in the CFGTAG_SYSINFO space. // // Note that the DHCP client will erase all CFGTAG_SYSINFO tags except // CFGITEM_DHCP_HOSTNAME. If the application needs to keep manual // entries in the DHCP tag range, then the code to maintain them should // be placed here. // // Here, we want to manually add a DNS server to the configuration, but // we can only do it once DHCP has finished its programming. // if( Item == CFGITEM_SERVICE_DHCPCLIENT && Status == CIS_SRV_STATUS_ENABLED && (Report == (NETTOOLS_STAT_RUNNING|DHCPCODE_IPADD) || Report == (NETTOOLS_STAT_RUNNING|DHCPCODE_IPRENEW)) ) { IPN IPTmp; // Manually add the DNS server when specified IPTmp = inet_addr(DNSServer); if( IPTmp ) CfgAddEntry( 0, CFGTAG_SYSINFO, CFGITEM_DHCP_DOMAINNAMESERVER, 0, sizeof(IPTmp), (UINT8 *)&IPTmp, 0 ); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -