Ethernet wrapper

Dependencies:   EthernetNetIf

Dependents:   DodgeRadioEmulatorv30

Files at this revision

API Documentation at this revision

Comitter:
rtgree01
Date:
Mon Aug 20 02:35:40 2012 +0000
Child:
1:e6c911335a8f
Commit message:
[mbed] converted /DodgeRadioEmulatorv30/EthernetWrapperLib

Changed in this revision

Eth.cpp Show annotated file Show diff for this revision Revisions of this file
Eth.h Show annotated file Show diff for this revision Revisions of this file
EthernetNetIf.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Eth.cpp	Mon Aug 20 02:35:40 2012 +0000
@@ -0,0 +1,53 @@
+#include "Eth.h"
+
+Eth::Eth()
+{
+    eth = new EthernetNetIf(IpAddr(10,10,10,2), IpAddr(255,255,255,0), IpAddr(10,10,10,1), IpAddr(10,10,10,1));
+    EthernetErr ethErr = eth->setup();
+    if(ethErr)
+    {
+        printf("Error %d in setup.\n", ethErr);
+        return;
+    }
+    printf("Eth Setup OK\r\n");
+    
+//    checkNetTicker.attach(this, &Eth::Operate, 0.02f);
+ }
+ 
+void Eth::Operate(void)
+{
+    Net::poll();
+}
+
+UDPSock::UDPSock(Host *l, int buff, SocketReceiver *sr)
+{
+    local = l;
+    bufferSize = buff;
+    receiver = sr;
+    buffer = new char[bufferSize];
+    
+    udp.setOnEvent(this, &UDPSock::onUDPSocketEvent);
+    udp.bind(*local);
+}
+
+void UDPSock::SendTo(Host *remote, int size, char *data)
+{
+    udp.sendto(data, size, remote);
+}
+
+void UDPSock::onUDPSocketEvent(UDPSocketEvent e)
+{
+    switch(e)
+    {
+        case UDPSOCKET_READABLE: //The only event for now
+            Host host;
+            while( int len = udp.recvfrom( buffer, bufferSize, &host ) )
+            {
+                if( len <= 0 )
+                    break;
+                    
+                receiver->ReceivedData(true, len, buffer);
+            }
+        break;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Eth.h	Mon Aug 20 02:35:40 2012 +0000
@@ -0,0 +1,45 @@
+#ifndef ETH_H
+#define ETH_H
+
+#include "EthernetNetIf.h"
+#include "UDPSocket.h"
+
+class Eth
+{
+public:
+    Eth();
+    ~Eth() {};
+        
+    void Operate(void);
+
+private:
+
+    EthernetNetIf *eth;
+    Ticker checkNetTicker;
+};
+
+class SocketReceiver
+{
+public:
+    virtual void ReceivedData(int status, int size, char *data) = 0;
+};
+
+class UDPSock
+{
+public:
+    UDPSock(Host *l, int buff, SocketReceiver *sr);
+    ~UDPSock() {};
+    void SendTo(Host *remote, int size, char *data);
+    
+private:
+    Host *local;
+    Host *remote;
+    UDPSocket udp;
+    int bufferSize;
+    char *buffer;
+    SocketReceiver *receiver;
+
+    void onUDPSocketEvent(UDPSocketEvent e);
+ };
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/EthernetNetIf.lib	Mon Aug 20 02:35:40 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mamezu/libraries/EthernetNetIf/ljksa8
\ No newline at end of file