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

📄 cfgdemo.c

📁 本软件是TI公司免费提供的网络开发包 现在好象很难找到,有黑心的公司把它改一改,就卖价5000元,对网络开发和网络驱动开发有参考价值
💻 C
字号:
/*
 *  Copyright 2006 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.91.00.08 08-22-2006 (ndk-a08)
 */
//--------------------------------------------------------------------------
// IP Stack Test Program
//--------------------------------------------------------------------------
// CfgDemo.c
//
// TCP/IP Client Configuration Demo
//
// Author: Michael A. Denio
// Copyright 1999, 2000 by Texas Instruments Inc.
//-------------------------------------------------------------------------
#include <stdio.h>
#include <netmain.h>
#include <_stack.h>
#include <common/console/console.h>
#include <common/servers/servers.h>
#include "cfgdemo.h"

//---------------------------------------------------------------------------
// Version String
//
char *VerStr = "\nTCP/IP Stack Client Configuration Demo\n";

// Simulate a non-volatile configuration
//=====================================
UINT8 MainConfig[512];
int   MainConfigSize   = 0;
int   MainConfigValid  = 0;
//=====================================

// Our simulated boot entry point
static void   NetBoot();

// Routine to get IP address from Ping packet
static void GetIP( uint IfIdx );

// NC Callback Functions
static void   NetworkOpen();
static void   NetworkClose();
static void   NetworkIPAddr( IPN IPAddr, uint IfIdx, uint fAdd );

// Fun reporting function
void   ServiceReport( uint Item, uint Status, uint Report, HANDLE hCfgEntry );


//---------------------------------------------------------------------
// Main Entry Point
//---------------------------------------------------------------------
int main()
{}

//
// Main Thread
//
int StackTest()
{
    HANDLE   hCfg;
    char     *hn = "tidsp";
    int      rc;
    uint     tmp;

    //
    // 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 a banner
    printf(VerStr);

    // Enable the EDMA interrupt - since the EDMA interrupt
    // is configurable, this one line of code has no home.
    //
    // NOTE: This code is not required for DM642, nor any
    //       driver environment that does not use EMDA sharing.
    //       However, it doesn't hurt to turn it on.
    C62_enableIER( 1<<8 );

    //
    // We'll build a phony "default" configuration in this routine.
    // It will simulate having a default configuration stored in
    // non-volatile storage
    //

    // Create a new configuration
    hCfg = CfgNew();
    if( !hCfg )
    {
        printf("Unable to open configuration\n");
        goto main_exit;
    }

    // Add our global hostname (to be claimed in all connected domains)
    CfgAddEntry( hCfg, CFGTAG_SYSINFO, CFGITEM_DHCP_HOSTNAME, 0,
                 strlen(hn), (UINT8 *)hn, 0 );

    // Specify TELNET service
    {
        CI_SERVICE_TELNET telnet;

        bzero( &telnet, sizeof(telnet) );
        telnet.cisargs.IPAddr = INADDR_ANY;
        telnet.cisargs.pCbSrv = &ServiceReport;
        telnet.param.MaxCon   = 2;
        telnet.param.Callback = &ConsoleOpen;
        CfgAddEntry( hCfg, CFGTAG_SERVICE, CFGITEM_SERVICE_TELNET, 0,
                     sizeof(telnet), (UINT8 *)&telnet, 0 );
    }

    // Create RAM based WEB files for HTTP
    AddWebFiles();

    // Specify HTTP service
    {
        CI_SERVICE_HTTP http;

        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 );
    }

    // We don't want to see debug messages less than WARNINGS
    tmp = DBG_WARN;
    CfgAddEntry( hCfg, CFGTAG_OS, CFGITEM_OS_DBGPRINTLEVEL,
                 CFG_ADDMODE_UNIQUE, sizeof(uint), (UINT8 *)&tmp, 0 );

    // Save the configuration to a linear buffer
    MainConfigSize = 0;
    CfgSave( hCfg, &MainConfigSize, 0 );
    printf("%d bytes required for save\n",MainConfigSize);
    if( MainConfigSize > sizeof( MainConfig ) )
        printf("FATAL: Config buffer too small\n");
    else
    {
        CfgSave( hCfg, &MainConfigSize, MainConfig );
        CfgFree( hCfg );

        // Now call what would really be the "boot" function
        NetBoot();
    }

    // Free the WEB files
    RemoveWebFiles();

    // Close the OS
main_exit:
    NC_SystemClose();
    return(0);
}


//---------------------------------------------------------------------
// Simulated Boot Entry Point
//---------------------------------------------------------------------
static void NetBoot()
{
    HANDLE   hCfg;
    int      rc;

    //
    // Initialize the OS
    //
    // Note - we normally call NC_SystemOpen() here, but its already
    // been done in this simulation.
    //

    //
    // Boot the system using stored configuration
    //
    // We keep booting until the function returns 0. This allows
    // us to have a "reboot" command.
    //
    do
    {
        // Create a new configuration
        hCfg = CfgNew();
        if( !hCfg )
        {
            printf("Unable to open configuration\n");
            break;
        }

        // Load the configuration
        CfgLoad( hCfg, MainConfigSize, MainConfig );

        // Start the stack
        rc = NC_NetStart( hCfg, NetworkOpen, NetworkClose, NetworkIPAddr );

        // Delete Configuration
        CfgFree( hCfg );
    } while( rc > 0 );

    //
    // Close the OS
    //
    // Note - we normally call NC_SystemClose() here, but its done for
    // us in this simulation.
    //
}

//
// System Task Code
//
static HANDLE hGetIP=0,hEcho=0,hData=0,hNull=0,hOob=0;

//
// NetworkOpen
//
// This function is called after the configuration has booted
//
static void NetworkOpen()
{
    // If we don't have any kind of IP in our configuration, we
    // do a "config by ping" function. The calling parameter is the
    // interface index to configure.
    if( !MainConfigValid )
        hGetIP = TaskCreate( GetIP, "GetIP", OS_TASKPRINORM, 0x1000, 1, 0, 0 );

    // Create our local servers
    hEcho = TaskCreate( echosrv, "EchoSrv", OS_TASKPRINORM, 0x1400, 0, 0, 0 );
    hData = TaskCreate( datasrv, "DataSrv", OS_TASKPRINORM, 0x1400, 0, 0, 0 );
    hNull = TaskCreate( nullsrv, "NullSrv", OS_TASKPRINORM, 0x1400, 0, 0, 0 );
    hOob  = TaskCreate( oobsrv,  "OobSrv", OS_TASKPRINORM, 0x1000, 0, 0, 0 );
}

//
// 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()
{
    // Kill the GetIP task only if it did not complete
    if( hGetIP )
        fdCloseSession( hGetIP );
    fdCloseSession( hOob );
    fdCloseSession( hNull );
    fdCloseSession( hData );
    fdCloseSession( hEcho );

    // Kill any active console
    ConsoleClose();

    // If we opened NETCTRL as NC_PRIORITY_HIGH, we can't
    // kill our task threads until we've given them the
    // opportunity to shutdown. We do this by manually
    // setting our task priority to NC_PRIORITY_LOW.
    TaskSetPri( TaskSelf(), NC_PRIORITY_LOW );

    if( hGetIP )
        TaskDestroy( hGetIP );
    TaskDestroy( hOob );
    TaskDestroy( hNull );
    TaskDestroy( hData );
    TaskDestroy( hEcho );
}

//
// 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" };
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 );
}


//
// GetIP()
//
// Use ICMP ECHO request to get IP address
//
#define MAXPACKET 1000 // max packet size
static void GetIP( uint IfIdx )
{
    SOCKET   s;
    struct   sockaddr from;
    char     *pBuf = 0;
    int      cc, fromlen;
    IPN      IPMe;
    ICMPHDR  *pIcHdr;
    IPHDR    *pIpHdr;
    int      IPHdrLen;
    CI_IPNET NA;

    // Allocate FDT
    fdOpenSession( TaskSelf() );

    // Create the ICMP Socket
    s = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
    if( s == INVALID_SOCKET )
        goto abort;

    // Initialize the "from" address
    bzero( &from, sizeof(struct sockaddr));
    from.sin_family     = AF_INET;
    from.sin_len        = sizeof( from );

    // Allocate a working buffer
    if( !(pBuf = mmBulkAlloc( MAXPACKET )) )
        goto abort;

    printf("GetIP Ready\n");

    while(1)
    {
        // Receive ICMP packet data
        fromlen = sizeof(from);
        cc = (int)recvfrom( s, pBuf, MAXPACKET, 0, &from, &fromlen );
        if( cc < 0 )
            goto abort;

        // Get header pointers
        pIpHdr   = (IPHDR *)pBuf;
        IPHdrLen = (pIpHdr->VerLen & 0xF) * 4;
        pIcHdr   = (ICMPHDR *)(pBuf+IPHdrLen);

        // Verify the ICMP type is ECHO request
        if( pIcHdr->Type != ICMP_ECHO )
            continue;

        // Use the destination address as our IP address
        IPMe = RdNet32( &pIpHdr->IPDst );

        // Add the IP to the configuration
        // Assume the netmask is 255.255.254.0
        bzero( &NA, sizeof(NA) );
        NA.IPAddr = IPMe;
        NA.IPMask = inet_addr("255.255.254.0");
        strcpy( NA.Domain, "default.net" );
        if( CfgAddEntry( 0, CFGTAG_IPNET, IfIdx, 0,
                         sizeof(CI_IPNET), (UINT8 *)&NA, 0 ) >= 0 )
            break;
    }

abort:
    hGetIP = 0;

    printf("GetIP Closing\n");

    if( pBuf )
        mmBulkFree( pBuf );
    if( s != INVALID_SOCKET )
        fdClose( s );

    fdCloseSession( TaskSelf() );
    TaskExit();
}

⌨️ 快捷键说明

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