Example program showing how to use the mbedNet library with UDP sockets. This demo broadcasts a message each second with a counter value incrementing at every call while the application loop displays the datagrams received on the same socket.

Dependencies:   mbed

main.cpp

Committer:
Benoit
Date:
2011-06-14
Revision:
0:1b4d6b9e92a9
Child:
1:552bcce1761d

File content as of revision 0:1b4d6b9e92a9:

#include <mbed.h>
#include <Sockets.h>
#include <mbedNetIF.h>
#include <ARP.h>
#include <ICMPv4.h>
#include <UDPv4.h>
#include <Debug.h>


#define	NET_TIMER_PERIOD		1
#define	BUFFER_LEN				64


Serial 				pc(USBTX, USBRX); // tx, rx
Ticker 				netTicker, echoTicker;
Ethernet_Addr_t		myMAC = {0x00, 0x10, 0x33, 0x2e, 0x18, 0x7f};
IPv4_Addr_t			ip = {192, 168, 0, 2},
					netmask = {255, 255, 255, 0},
					gateway = {192, 168, 0, 1},
					broadcast = {192, 168, 0, 255};
NetIF_t				*mbedNetIF;
Socket_t			udpSocket;
Socket_AddrIn_t		localAddress, senderAddress, broadcastAddress;


void NetTicker(void)
{
	NetIF_ProcessTimers(NET_TIMER_PERIOD);
}


void EchoTicker(void)
{
	char 			buffer[BUFFER_LEN];
	static int32_t	counter = 0;
	
	sprintf(buffer, "counter = %d\n", counter++);
	Sockets_SendTo(udpSocket, (uint8_t *)buffer, strlen(buffer), 0, (Socket_Addr_t *)&broadcastAddress, broadcastAddress.len);
}


int main() 
{
	int32_t		byteCount, senderAddressLength;
	char		buffer[BUFFER_LEN];

	// serial output speed to 115200 baud
	pc.baud(115200);

	// Clears screen and display a welcome message
	printf("%cStarting!\r\n", 12);
    
	// Set debug levels to have the NetIF networking layer display the interface status
	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);	// Registers the ethernet drivers as a network interface
	
	ethernet.RegisterProtocol(&arp);					// We need ARP over ethernet to resolve IP addresses with MAC addresses
	ethernet.RegisterProtocol(&ipv4);					// We need IPv4 (over ethernet) of course
	ipv4.RegisterProtocol(&icmpv4);						// Then ICMPv4 over IPv4 to answer ping requests
	ipv4.RegisterProtocol(&udpv4);						// Next let's register UDPv4 over IPv4
	udpv4.RegisterAPI(&sockets);						// Finally let's register the Sockets API (it is available for UDP at the moment)
	ARP_AddStaticEntry(mbedNetIF, broadcast, &ethernet_Addr_Broadcast);	// We add a static entry to the broadcast address to the ARP cache
	netTicker.attach(&NetTicker, NET_TIMER_PERIOD);		// A 1 second periodic timer to process network house keeping routines (like expiration in the ARP cache, timeouts for TCP in the future, ...)
    
	ARP_DisplayCache();									// Display the ARP cache contents		
	
    NetIF_Up(mbedNetIF);								// Finally we bring the network interface up
	
	udpSocket = Sockets_Open(AF_INET, SOCK_DGRAM, 0);	// Create an UDP socket
	
	localAddress.family = AF_INET;
	localAddress.len = sizeof(Socket_AddrIn_t);
	localAddress.port = htons(12345);
	localAddress.address = ipv4_Addr_Any;
	
	broadcastAddress.family = AF_INET;
	broadcastAddress.len = sizeof(Socket_AddrIn_t);
	broadcastAddress.port = htons(54321);
	broadcastAddress.address = broadcast;

	Sockets_Bind(udpSocket, (Socket_Addr_t *)&localAddress, localAddress.len);	// We bind the socket to port 12345 and any local IP address

	echoTicker.attach(&EchoTicker, 2);					// Every two seconds, broadcast an UDP message to port 54321

	// Then do whatever you need for your application. here we simply check if we have received a datagram from the udpSocket and display its informations
	senderAddressLength = sizeof(Socket_AddrIn_t);
	while(true)
	{
		byteCount = Sockets_RecvFrom(udpSocket, (uint8_t *)buffer, BUFFER_LEN, 0, (Socket_Addr_t *)&senderAddress, &senderAddressLength);
		if (byteCount > 0)
		{
			buffer[byteCount] = '\0';
			printf("We have received '%s' (%d bytes) from %d.%d.%d.%d:%d\r\n",
				buffer,
				byteCount,
				senderAddress.address.IP0,
				senderAddress.address.IP1,
				senderAddress.address.IP2,
				senderAddress.address.IP3,
				ntohs(senderAddress.port)
				);
		}
	}
}