mbedNet example

#include "mbed.h"
#include "mbedNet.h"
#include "Sockets.h"
#include "mbedNetIF.h"
#include "ARP.h"
#include "ICMPv4.h"
#include "UDPv4.h"
#include "TCPv4.h"
#include "Debug.h"


#define NET_TICKER_PERIOD   1


Ethernet_Addr_t    myMAC = {0x00, 0x10, 0x33, 0x2e, 0x18, 0x7f};
IPv4_Addr_t        ip = {10, 0, 1, 55},
                netmask = {255, 255, 255, 0},
                gateway = {10, 0, 1, 2},
                broadcast = {10, 0, 1, 255};
NetIF_t            *mbedNetIF;
Ticker          netTicker;
Serial          pc(USBTX, USBRX);


/* Ticker to process net time-related stuff (like ARP cache so far) */
void NetTicker(void)
{
    NetIF_ProcessTimers(NET_TICKER_PERIOD);
}


int main()
{
    int32_t         byteCount, remoteLen;
    static uint8_t  buffer[128];
    Socket_t        socket;
    Socket_AddrIn_t localAddrIn, 
                    remoteAddrIn;
    char            c;
    
    pc.baud(115200);
    
    printf("%cStarting!\r\n", 12);
    
    Debug_SetMasks(DEBUG_MODULE_NETIF, DEBUG_LEVEL_ALL  & ~(DEBUG_LEVEL_VERBOSE0 | DEBUG_LEVEL_VERBOSE1 | DEBUG_LEVEL_VERBOSE2));

    mbedNetIF = NetIF_RegisterInterface(&ip, &netmask, &gateway, &mbedNetIF_Driver, (void *)&myMAC);
    ethernet.RegisterProtocol(&arp);
    ethernet.RegisterProtocol(&ipv4);
    ipv4.RegisterProtocol(&icmpv4);
    ipv4.RegisterProtocol(&udpv4);
    udpv4.RegisterAPI(&sockets);
    
    ARP_AddStaticEntry(mbedNetIF, broadcast, &ethernet_Addr_Broadcast);
    
    netTicker.attach(&NetTicker, NET_TICKER_PERIOD);
    
    NetIF_Up(mbedNetIF);

    socket = Sockets_Open(AF_INET, SOCK_DGRAM, 0);
    localAddrIn.family = AF_INET;
    localAddrIn.len = sizeof(Socket_AddrIn_t);
    localAddrIn.port = htons(12345);
    localAddrIn.address = ipv4_Addr_Any;
    Sockets_Bind(socket, (Socket_Addr_t *)&localAddrIn, sizeof(Socket_AddrIn_t));

    while(1) 
    {
        remoteLen = sizeof(Socket_AddrIn_t);
        byteCount = Sockets_RecvFrom(socket, buffer, 128, 0, (Socket_Addr_t *)&remoteAddrIn, &remoteLen);
        if (byteCount > 0)
        {
            buffer[byteCount - 1] = '\0';
            printf("got %d bytes ('%s') from %d:%d.%d.%d:%d\r\n",
                byteCount,
                buffer,
                remoteAddrIn.address.IP0,
                remoteAddrIn.address.IP1,
                remoteAddrIn.address.IP2,
                remoteAddrIn.address.IP3,
                ntohs(remoteAddrIn.port)
            );
        }
        if (pc.readable())
        {
            c = pc.getc();
            switch(c)
            {
                case 'h':
                    printf("commands:\r\n");
                    printf("d:   display ARP cache table\r\n");
                    printf("a:   send ARP request for %d.%d.%d.%d\r\n", gateway.IP0, gateway.IP1, gateway.IP2, gateway.IP3);
                    printf("f:   flush ARP table\r\n");
                    break;
            
                case 'd':
                    ARP_DisplayCache();
                    break;
                    
                case 'f':
                    ARP_FlushCache(False);
                    break;
                    
                case 'a':
                    ARP_ResolveIPv4Address(mbedNetIF, gateway, NULL);
                    break;
            }
        }
        
    }
}




Please log in to post comments.