Udp broadcast test
Dependencies: EthernetInterface mbed-rtos mbed
Revision 0:89513b6107d7, committed 2012-09-15
- Comitter:
- nkhorman
- Date:
- Sat Sep 15 00:57:45 2012 +0000
- Commit message:
- initial commit
Changed in this revision
diff -r 000000000000 -r 89513b6107d7 EthernetInterface.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/EthernetInterface.lib Sat Sep 15 00:57:45 2012 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/EthernetInterface/#a0ee3ae75cfa
diff -r 000000000000 -r 89513b6107d7 NetService/NetService.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/NetService/NetService.h Sat Sep 15 00:57:45 2012 +0000 @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2012 Neal Horman - http://www.wanlink.com + * + * Warranty; + * No warranty of fitness is given, express or implied, + * use at your own risk. + * + * License; + * Use and modify as you wish as long as you keep this statement, + * the Warranty, and my Copyright above. + * + */ + +#ifndef _NETSERVICE_H_ +#define _NETSERVICE_H_ + +class NetService +{ +public: + NetService() : mbRunning(false) {}; + + virtual bool Start() = 0; + virtual void Poll() = 0; + virtual void Stop() = 0; + +protected: + bool mbRunning; +}; + +#endif \ No newline at end of file
diff -r 000000000000 -r 89513b6107d7 NetService/UDPBeacon/UDPBeacon.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/NetService/UDPBeacon/UDPBeacon.cpp Sat Sep 15 00:57:45 2012 +0000 @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2012 Neal Horman - http://www.wanlink.com + * + * Warranty; + * No warranty of fitness is given, express or implied, + * use at your own risk. + * + * License; + * Use and modify as you wish as long as you keep this statement, + * the Warranty, and my Copyright above. + * + */ + +#include "UDPBeacon.h" + +bool UDPBeacon::Start() +{ + if(!mbRunning) + { + mbRunning = ( + mServer.bind(mPort) == 0 + && mServer.set_broadcast() == 0 + && mEndpoint.set_address(0xFFFFFFFF,mPort) == 0 + ); + if(mbRunning) + mServer.set_blocking(false,10); + } + + return mbRunning; +} + +void UDPBeacon::Stop() +{ + if(mbRunning) + { + mbRunning = false; + mServer.close(); + } +} + +void UDPBeacon::Poll() +{ + if(mbRunning) + { + time_t timenow = time(NULL); + if(timenow >= mTick) + { + mTick = timenow+1; + + mServer.sendTo(mEndpoint,"hello",5); + } + } +} \ No newline at end of file
diff -r 000000000000 -r 89513b6107d7 NetService/UDPBeacon/UDPBeacon.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/NetService/UDPBeacon/UDPBeacon.h Sat Sep 15 00:57:45 2012 +0000 @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2012 Neal Horman - http://www.wanlink.com + * + * Warranty; + * No warranty of fitness is given, express or implied, + * use at your own risk. + * + * License; + * Use and modify as you wish as long as you keep this statement, + * the Warranty, and my Copyright above. + * + */ + +#ifndef _UDPBEACON_H_ +#define _UDPBEACON_H_ + +#include "mbed.h" +#include "UDPSocket.h" +#include "NetService.h" + +class Endpoint2 : public Endpoint +{ +public: + int set_address(uint32_t ip, const int port) + { + reset_address(); + + _remoteHost.sin_addr.s_addr = htonl(ip); + _remoteHost.sin_family = AF_INET; + _remoteHost.sin_port = htons(port); + + return 0; + } +}; + +class UDPSocket2 : public UDPSocket +{ +public: + int set_broadcast(bool bBroadcast = true) + { int val = bBroadcast; + + return lwip_setsockopt(_sock_fd,SOL_SOCKET,SO_BROADCAST,&val,sizeof(val)); + } +}; + +class UDPBeacon : public NetService +{ +public: + UDPBeacon(uint16_t port) + : NetService() + , mPort(port) + , mTick(0) + {}; + virtual bool Start(); + virtual void Poll(); + virtual void Stop(); +protected: + UDPSocket2 mServer; + Endpoint2 mEndpoint; + uint16_t mPort; + time_t mTick; +}; +#endif \ No newline at end of file
diff -r 000000000000 -r 89513b6107d7 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sat Sep 15 00:57:45 2012 +0000 @@ -0,0 +1,15 @@ +#include "mbed.h" + +#include "UDPBeacon.h" + +UDPBeacon gBeacon(1234); + +int main() +{ + while(gBeacon.Start()) + { + gBeacon.Poll(); + } + + gBeacon.Stop(); +}
diff -r 000000000000 -r 89513b6107d7 mbed-rtos.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-rtos.lib Sat Sep 15 00:57:45 2012 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed-rtos/#9654a71f5a90
diff -r 000000000000 -r 89513b6107d7 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Sat Sep 15 00:57:45 2012 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/cd19af002ccc \ No newline at end of file