![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
First attempt at some form of autodiscovery of an mbed based device by periodically broadcasting our IP in a UDP packet.
Revision 0:c1be031ca851, committed 2010-06-18
- Comitter:
- darran
- Date:
- Fri Jun 18 09:09:57 2010 +0000
- Commit message:
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/AutoDiscoveryBroadcaster.cpp Fri Jun 18 09:09:57 2010 +0000 @@ -0,0 +1,38 @@ +#include "AutoDiscoveryBroadcaster.h" + +AutoDiscoveryBroadcaster::AutoDiscoveryBroadcaster() { + running = 0; + // Create the UDP socket + udpSock = new UDPSocket; +} + +AutoDiscoveryBroadcaster::~AutoDiscoveryBroadcaster() { + stop(); + udpSock->close(); + // Delete the UDP socket + delete udpSock; +} + +void AutoDiscoveryBroadcaster::start(int udpPort, int period) { + if ( !running ) { + msg.init(); + udpSock->bind(Host(IP_ADDR_ANY, udpPort)); + host = Host(IP_ADDR_BROADCAST, udpPort); + //host = Host(IpAddr(192,168,1,141), udpPort); + ticker.attach(this, &AutoDiscoveryBroadcaster::broadcast, period); + running = 1; + // call once now + broadcast(); + } +} + +void AutoDiscoveryBroadcaster::stop() { + if ( running ) { + ticker.detach(); + running = 0; + } +} + +void AutoDiscoveryBroadcaster::broadcast() { + printf("Sending result = %d\r\n", udpSock->sendto(msg.getBytes(), msg.getLength(), &host)); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/AutoDiscoveryBroadcaster.h Fri Jun 18 09:09:57 2010 +0000 @@ -0,0 +1,54 @@ +#ifndef AUTO_DISCOVERY_BROADCASTER_H +#define AUTO_DISCOVERY_BROADCASTER_H + +#include "mbed.h" +#include "UDPSocket.h" +#include "BroadcastMessage.h" + +/* + Class: AutoDiscoveryBroadcaster + Periodically sends out hostname + and IP address on UDP port 2010 +*/ +class AutoDiscoveryBroadcaster { +public: + // Constructor: AutoDiscoveryBroadcaster + // Creates the UDP sockets + AutoDiscoveryBroadcaster(); + // Destructor: ~AutoDiscoveryBroadcaster + // Deletes the UDP socket + ~AutoDiscoveryBroadcaster(); + /* + Function: start + Starts periodically sending out the information datagrams + Parameters: + udpPort - The UDP port to send on (default 2010) + period - The period between broadcasts + */ + void start(int udpPort=2010, int period=5); + /* + Function: stop + Stops sending out the information datagrams + */ + void stop(); +private: + // Variable: udpSock + // The UDP socket + UDPSocket* udpSock; + // Variable: running + // Flag to indicate whether we're running or not + char running; + // Variable: ticker + // Ticker used to periodically call broadcast() + Ticker ticker; + Host host; + + // Function: broadcast + // The method that actually broadcasts the information + // about this mbed + void broadcast(); + + BroadcastMessage msg; +}; + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/BroadcastMessage.cpp Fri Jun 18 09:09:57 2010 +0000 @@ -0,0 +1,26 @@ +#include "BroadcastMessage.h" + +BroadcastMessage::BroadcastMessage() { +} + +BroadcastMessage::~BroadcastMessage() { +} + +void BroadcastMessage::init() { + NetIf* netIf = Net::getDefaultIf(); + IpAddr ip = netIf->getIp(); + len = sprintf(buff, + "mbed microcontroller\r\n" + "IP: %d.%d.%d.%d!\r\n" + "Service: CANWIFI\r\n", + ip[0], ip[1], ip[2], ip[3] + ); +} + +char* BroadcastMessage::getBytes() { + return buff; +} + +int BroadcastMessage::getLength() { + return len; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/BroadcastMessage.h Fri Jun 18 09:09:57 2010 +0000 @@ -0,0 +1,31 @@ +#ifndef BROADCAST_MESSAGE_H +#define BROADCAST_MESSAGE_H + +#include "mbed.h" +#include "UDPSocket.h" + +/* + Class: AutoDiscoveryBroadcaster + Periodically sends out hostname + and IP address on UDP port 2010 +*/ +class BroadcastMessage { +public: + // Constructor: AutoDiscoveryBroadcaster + // Creates the UDP sockets + BroadcastMessage(); + // Destructor: ~AutoDiscoveryBroadcaster + // Deletes the UDP socket + ~BroadcastMessage(); + void init(); + // Function: getBytes + // Returns the message to transmit + char* getBytes(); + int getLength(); +private: + char buff[1024]; + int len; +}; + +#endif + \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/EthernetNetIf.lib Fri Jun 18 09:09:57 2010 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/donatien/code/EthernetNetIf/#bc7df6da7589
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Fri Jun 18 09:09:57 2010 +0000 @@ -0,0 +1,53 @@ +/* + * Auto Discovery + * Broadcasts information about this mbed on port 2010 + * Enables other devices on the same network to find it + */ + +#define LWIP_NETIF_HOSTNAME "mbed_dms" + +#include "mbed.h" +#include "EthernetNetIf.h" + +#include "AutoDiscoveryBroadcaster.h" + +// Our Ethernet interface +EthernetNetIf eth; +// Static IP address information +/* +EthernetNetIf eth( + IpAddr(192,168,1,158), //IP Address + IpAddr(255,255,255,0), //Network Mask + IpAddr(192,168,1,254), //Gateway + IpAddr(192,168,1,2) //DNS +); +*/ +// Our AutoDiscoveryBroadcaster server +AutoDiscoveryBroadcaster adb; + +/* + Function: main + + Sets up the Ethernet interface using DHCP, sets + up the AutoDiscoveryBroadcaster which then + fires out UDP datagrams with info about the mbed +*/ +int main() { + + printf("\r\nSetting up...\r\n"); + EthernetErr ethErr = eth.setup(); + if (ethErr) { + printf("Error %d in setup on DHCP.\r\n", ethErr); + return -1; + } + printf("Trying to get IP address\r\n"); + IpAddr ip = eth.getIp(); + printf("mbed IP Address is %d.%d.%d.%d\r\n", ip[0], ip[1], ip[2], ip[3]); + + adb.start(); + + printf("Entering while loop Net::poll()ing\r\n"); + while (1) { + Net::poll(); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Fri Jun 18 09:09:57 2010 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/029aa53d7323