network_main.c
上传用户:dahaojd
上传日期:2008-01-29
资源大小:14357k
文件大小:8k
- /* ======================================================================== */
- /* Basic header files */
- /* ======================================================================== */
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <time.h>
- #include "netmain.h" /*change*/
- #include "_oskern.h" /*change*/
- // Build WEB page
- extern void AddWebFiles(void);
- extern void RemoveWebFiles(void);
- //---------------------------------------------------------------------------
- // Title String
- //
- char *VerStr = "nTCP/IP Stack NetCam Examplen";
- // Our NETCTRL callback functions
- static void NetworkOpen();
- static void NetworkClose();
- static void NetworkIPAddr( IPN IPAddr, uint IfIdx, uint fAdd );
- // Fun reporting function
- static void ServiceReport( uint Item, uint Status, uint Report, HANDLE hCfgEntry );
- //---------------------------------------------------------------------------
- // Configuration
- //
- char *HostName = "mikecam";
- //char *LocalIPAddr = "0.0.0.0"; // Set to "0.0.0.0" for DHCP /*change*/
- char *LocalIPAddr = "192.168.1.77"; // Set to "0.0.0.0" for DHCP
- char *LocalIPMask = "255.255.255.0"; // Not used when using DHCP
- char *GatewayIP = "192.168.0.1"; // Not used when using DHCP
- char *DomainName = "demo.net"; // Not used when using DHCP
- char *DNSServer = "0.0.0.0"; // Used when set to anything but zero
- // Defined in our CDB file
- extern uint extHeap;
- //
- // Main Thread
- //
- void network_main()
- {
- int rc;
- HANDLE hCfg;
- CI_SERVICE_HTTP http;
- //
- // THIS MUST BE THE ABSOLUTE FIRST THING DONE IN AN APPLICATION!!
- //
- NC_SystemOpen();
- _mmBulkAllocSeg( extHeap );
- // Print out our banner
- printf(VerStr);
- // Enable the EDMA interrupt - since the EDMA interrupt
- // is configurable, this one line of code has no home.
- C62_enableIER( 1<<8 );
- //
- // Create and build the system configuration from scratch.
- //
- // Create a new configuration
- hCfg = CfgNew();
- if( !hCfg )
- {
- printf("Unable to create configurationn");
- goto main_exit;
- }
- // We better validate the length of the supplied names
- if( strlen( DomainName ) >= CFG_DOMAIN_MAX ||
- strlen( HostName ) >= CFG_HOSTNAME_MAX )
- {
- printf("Names too longn");
- 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
- if( inet_addr(LocalIPAddr) )
- {
- CI_IPNET NA;
- CI_ROUTE RT;
- IPN IPTmp;
- // Setup manual IP address
- bzero( &NA, sizeof(NA) );
- NA.IPAddr = inet_addr(LocalIPAddr);
- NA.IPMask = inet_addr(LocalIPMask);
- strcpy( NA.Domain, DomainName );
- NA.NetType = 0;
- // Add the address to interface 1
- CfgAddEntry( hCfg, CFGTAG_IPNET, 1, 0,
- sizeof(CI_IPNET), (UINT8 *)&NA, 0 );
- // 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
- 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 );
- }
- // Create RAM based WEB files for HTTP
- AddWebFiles();
- // Specify HTTP service
- bzero( &http, sizeof(http) );
- http.cisargs.IPAddr = INADDR_ANY;
- http.cisargs.pCbSrv = &ServiceReport;
- CfgAddEntry( hCfg, CFGTAG_SERVICE, CFGITEM_SERVICE_HTTP, 0,
- sizeof(http), (UINT8 *)&http, 0 );
- //
- // Configure IPStack/OS Options
- //
- // 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 );
- // Change our socket buffer size to 8704 bytes
- rc = 8760;
- CfgAddEntry( hCfg, CFGTAG_IP, CFGITEM_IP_SOCKBUFMAX,
- 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.
- //
- do
- {
- rc = NC_NetStart( hCfg, NetworkOpen, NetworkClose, NetworkIPAddr );
- } while( rc > 0 );
- // Free the WEB files
- RemoveWebFiles();
- // Delete Configuration
- CfgFree( hCfg );
- // Close the OS
- main_exit:
- NC_SystemClose();
- }
- //
- // System Task Code
- //
- //
- // NetworkOpen
- //
- // This function is called after the configuration has booted
- //
- static void NetworkOpen()
- {
- }
- //
- // 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()
- {
- }
- extern void tskNetwork();
- //
- // 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 )
- {
- static uint fSystemReady = 0;
- IPN IPTmp;
- if( fAdd )
- printf("Network Added: ");
- else
- printf("Network Removed: ");
- // Print a message
- IPTmp = ntohl( IPAddr );
- printf("If-%d:%d.%d.%d.%dn", IfIdx,
- (UINT8)(IPTmp>>24)&0xFF, (UINT8)(IPTmp>>16)&0xFF,
- (UINT8)(IPTmp>>8)&0xFF, (UINT8)IPTmp&0xFF );
- // This is a good time to initialize anything that can't
- // run until we have an IP Address
- if( fAdd && !fSystemReady )
- {
- fSystemReady = 1;
- TaskCreate( tskNetwork, "NetRF5", 5, 0x1000, 0, 0, 0 );
- }
- }
- //
- // 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: %03dn",
- TaskName[Item-1], StatusStr[Status],
- ReportStr[Report/256], Report&0xFF );
- // Example of adding to the DHCP configuration space
- //
- // When using the DHCP clinent, the client has full control over access
- // to the first 256 entries in the CFGTAG_SYSINFO space. 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 );
- }
- }