![](/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.
Diff: AutoDiscoveryBroadcaster.cpp
- Revision:
- 0:c1be031ca851
--- /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)); +}