Udp broadcast test

Dependencies:   EthernetInterface mbed-rtos mbed

Files at this revision

API Documentation at this revision

Comitter:
nkhorman
Date:
Sat Sep 15 00:57:45 2012 +0000
Commit message:
initial commit

Changed in this revision

EthernetInterface.lib Show annotated file Show diff for this revision Revisions of this file
NetService/NetService.h Show annotated file Show diff for this revision Revisions of this file
NetService/UDPBeacon/UDPBeacon.cpp Show annotated file Show diff for this revision Revisions of this file
NetService/UDPBeacon/UDPBeacon.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /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
--- /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
--- /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
--- /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
--- /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();
+}
--- /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
--- /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