Main Page   Modules   Data Structures   File List   Data Fields   Globals   Related Pages   Examples  

main_demo.c

This is an example main.c file demonstrating basically two things: initialization sequence and main loop organization.

Initialization sequence
When performing initialization, all OpenTCP initialization functions must be invoked, starting from the lowest-level modules and continuing upwards (because it may be assumed that higher-level modules might already rely during their initialization on proper functioning of lower-level modules). Usual Initialization sequence looks something like:
Main loop organization
In main program loop three things must be accomplished:
More details about this example:

#include <inet/debug.h>
#include <inet/debug.h>
#include <inet/arch/config.h>
#include <inet/datatypes.h>
#include <inet/timers.h>
#include <inet/system.h>
#include <inet/ethernet.h>
#include <inet/ip.h>
#include <inet/tcp_ip.h>

/* Network Interface definition. Must be somewhere so why not here? :-)*/
struct netif localmachine;

/* main stuff */
void main(void)
{
        UINT16 len;
        
        /* initialize processor-dependant stuff (I/O ports, timers...).
         * This will normally be some function under the arch/xxxMCU dir. Most
         * important things to do in this function as far as the TCP/IP stack
         * is concerned is to:
         *  - initialize some timer so it executes decrement_timers
         *      on every 10ms (TODO: Throw out this dependency from several files
         *      so that frequency can be adjusted more freely!!!)
         *  - not mess too much with ports allocated for Ethernet controller
         */
        init();
         
        /* Set our network information. This is for static configuration.
        * if using BOOTP or DHCP this will be a bit different.
        */
        
        /* IP address */
        localmachine.localip = 0xAC1006E9;      /* 172.16.6.233 */
        /* Default gateway */
        localmachine.defgw =    0xAC100101;
        /* Subnet mask */
        localmachine.netmask = 0xFFFF0000;
        /* Ethernet (MAC) address */
        localmachine.localHW[5] = 0x00;
        localmachine.localHW[4] = 0x06;
        localmachine.localHW[3] = 0x70;
        localmachine.localHW[2] = 0xBA;
        localmachine.localHW[1] = 0xBE;
        localmachine.localHW[0] = 0xEE;
        

        /* Init system services         */    
        timer_pool_init();
                
        /*interrupts can be enabled AFTER timer pool has been initialized */
        
        /* Initialize all network layers        */
        NE2000Init(&localmachine.localHW[0]);
        arp_init();
        udp_init();
        tcp_init();

        /* Initialize applications      */
        udp_demo_init();
        tpcc_demo_init();
        tcps_demo_init();
    

        DEBUGOUT(">>>>>>>>>Entering to MAIN LOOP>>>>>>>>>\n\r");
  
        /***    MAIN LOOP       ***/
    
        while(1) {
                /* take care of watchdog stuff */
                        
                /* do some stuff here
                * .........
                */
         
             
                /* Try to receive Ethernet Frame        */
        
                if( NETWORK_CHECK_IF_RECEIVED() == TRUE )       {
                                
                        switch( received_frame.protocol) {
                        
                                case PROTOCOL_ARP:
                                        process_arp(&received_frame);   
                                        break;
                        
                        
                                case PROTOCOL_IP:                       
                                        len = process_ip_in(&received_frame);
                                
                                        if(len < 0)
                                                break;
                                
                                        switch(received_ip_packet.protocol){
                                                case IP_ICMP:
                                                        process_icmp_in (&received_ip_packet, len);                                     
                                                        break;
                                                case IP_UDP:
                                                        process_udp_in (&received_ip_packet,len);
                                                        break;
                                                case IP_TCP:
                                                        process_tcp_in (&received_ip_packet, len);                              
                                                        break;
                                                default:
                                                        break;
                                        }
                        break;

                        default:
                        
                                break;
                }
                
                /* discard received frame */                    
                NETWORK_RECEIVE_END();
        }
         
        /* Application main loops */
        /* Do not forget this!!! These don't get invoked magically :-) */
        udp_demo_run();
        tcpc_demo_run();
        tcps_demo_run();
        

        /* TCP/IP stack Periodic tasks  */
        /* Check possible overflow in Ethernet controller */
        NE2000CheckOverFlow();
        /* manage arp cache tables */
        arp_manage();
        /* manage opened TCP connections (retransmissions, timeouts,...)*/
        tcp_poll();
    }
    
}


Generated on Sun Aug 3 20:32:59 2003 for OpenTCP by doxygen1.2.18