ex_udp_client.c

This example program shows how to use the TCP/IP stack in client mode. It sends a single UDP datagram after the server's MAC address has been resolved via ARP. The datagram is sent to socket 10.1.0.101:54124 (IP address 10.1.0.101, port 54124).

#include "net\stacktsk.h"
#include "net\tick.h"
#include "net\helpers.h"
#include "net\udp.h"
#include "net\arp.h"
#include "net\arptsk.h"

//Create a UDP socket for receiving and sending data
static UDP_SOCKET udpSocketUser;

//UDP State machine
#define SM_UDP_SEND_ARP     0
#define SM_UDP_WAIT_RESOLVE 1
#define SM_UDP_RESOLVED     2
#define SM_UDP_SENT         3

static BYTE smUdp = SM_UDP_SEND_ARP;

//Timers
TICK8  tsecWait = 0;    //General purpose wait timer

void udpExample(void)
{
    BYTE c;
    NODE_INFO udpServerNode;

    //Initialize remote IP and address with 10.1.0.101. The MAC address is
    //is not intialized yet, but after we receive an ARP responce.
    //Configure for local port 54123 and remote port 54124.
    udpServerNode.IPAddr.v[0] = 10;
    udpServerNode.IPAddr.v[1] = 1;
    udpServerNode.IPAddr.v[2] = 0;
    udpServerNode.IPAddr.v[3] = 101;
    udpSocketUser = UDPOpen(54123, &udpServerNode, 54124);
    
    //An error occurred during the UDPOpen() function
    if (udpSocketUser == INVALID_UDP_SOCKET) {
        //Add user code here to take action if required!
    }

    //Infinite loop. Check if anything is received on UDP port
    while(1)
    {
        switch (smUdp) {
        case SM_UDP_SEND_ARP:
            if (ARPIsTxReady()) {
                //Remember when we sent last request
                tsecWait = TickGet8bitSec();
                
                //Send ARP request for given IP address
                ARPResolve(&udpServerNode.IPAddr);
                
                smUdp = SM_UDP_WAIT_RESOLVE;
            }
            break;
        case SM_UDP_WAIT_RESOLVE:
            //The IP address has been resolved, we now have the MAC address of the
            //node at 10.1.0.101
            if (ARPIsResolved(&udpServerNode.IPAddr, &udpServerNode.MACAddr)) {
                smUdp = SM_UDP_RESOLVED;
            }
            //If not resolved after 2 seconds, send next request
            else {
                if (TickGetDiff8bitSec(tsecWait) >= (TICK8)2) {
                    smUdp = SM_UDP_SEND_ARP;
                }
            }
            break;
        case SM_UDP_RESOLVED:
            //Checks if there is a transmit buffer ready for accepting data
            if (UDPIsPutReady(udpSocketUser)) {
                //Send a UDP Datagram with "Hello"
                UDPPut('H');
                UDPPut('e');
                UDPPut('l');
                UDPPut('l');
                UDPPut('o');

                //Send contents of transmit buffer, and free buffer
                UDPFlush();
        
                smUdp = SM_UDP_SENT;    //Set to "message sent" state
            }
            break;
        case SM_UDP_SENT:
            //Message sent state. If the user wants to resend the message, smUdp must be reset
            //to SM_UDP_RESOLVED
            break;
        }


        //This task performs normal stack task including checking for incoming
        //packet, type of packet and calling appropriate stack entity to
        //process it.
        StackTask();
    }
}

Generated on Wed Feb 3 12:45:33 2010 for SBC65EC Web Server by  doxygen 1.5.8