W5200(WIZ820io) network interface

Revision:
1:803123933c5a
Parent:
0:61831b843b44
--- a/MyNetUdpSocket.cpp	Sat Apr 14 17:21:11 2012 +0000
+++ b/MyNetUdpSocket.cpp	Tue Apr 17 12:13:15 2012 +0000
@@ -1,9 +1,9 @@
-// MyNetUdpSocket.cpp 2012/4/15
+// MyNetUdpSocket.cpp 2012/4/16
 #include "mbed.h"
 #include "MyNetUdpSocket.h"
 #include "w5100.h"
 
-#define DEBUG
+//#define DEBUG
 
 #ifdef DEBUG
 #include "Utils.h"
@@ -12,19 +12,31 @@
 #define PRINT_FUNC()
 #endif //DEBUG
 
-MyNetUdpSocket::MyNetUdpSocket() : NetUdpSocket()  {
+extern int w5200_new_socket(); // MyNetTcpSocket.cpp
+
+MyNetUdpSocket::MyNetUdpSocket(int socket) : NetUdpSocket(),_socket(socket)  {
     PRINT_FUNC();
-    _socket = 0;
-    W5100.writeSnMR(_socket, SnMR::UDP); // set UDP mode
+    if (_socket == (-1)) {
+        _socket = w5200_new_socket();
+    }
+    if (_socket != (-1)) {
+        W5100.writeSnMR(_socket, SnMR::UDP); // set UDP mode
+    }
 }
 
 MyNetUdpSocket::~MyNetUdpSocket() {
     PRINT_FUNC();
     close();
+    if (_socket != (-1)) {
+        W5100.writeSnMR(_socket, SnMR::CLOSE);
+    }
 }
 
 NetUdpSocketErr MyNetUdpSocket::bind(const Host& me) {
     PRINT_FUNC();
+    if (_socket == (-1)) {
+        return NETUDPSOCKET_MEM;
+    }
     int port = me.getPort();
     W5100.writeSnPORT(_socket, port);
     W5100.execCmdSn( _socket, Sock_OPEN); // set OPEN command
@@ -33,6 +45,9 @@
 
 int /*if < 0 : NetUdpSocketErr*/ MyNetUdpSocket::sendto(const char* buf, int len, Host* pHost) {
     PRINT_FUNC();
+    if (_socket == (-1)) {
+        return NETUDPSOCKET_MEM;
+    }
     uint8_t ip[4];
     ip[0] = pHost->getIp()[0];
     ip[1] = pHost->getIp()[1];
@@ -55,6 +70,9 @@
 
 int /*if < 0 : NetUdpSocketErr*/ MyNetUdpSocket::recvfrom(char* buf, int len, Host* pHost) {
     PRINT_FUNC();
+    if (_socket == (-1)) {
+        return NETUDPSOCKET_MEM;
+    }
     int size = W5100.getRXReceivedSize(_socket);
     if (size < 8) {
         return -1;
@@ -82,8 +100,12 @@
     if(m_closed) {
         return NETUDPSOCKET_OK;
     }
+    if (_socket == (-1)) {
+        return NETUDPSOCKET_MEM;
+    }
     m_closed = true;
     cleanUp();
+    W5100.writeSnMR(_socket, SnMR::CLOSE);
     return NETUDPSOCKET_OK;
 }
 
@@ -91,22 +113,27 @@
     PRINT_FUNC();
     NetUdpSocket::flushEvents();
 #ifdef DEBUG
-    printf("socket:%d SnMR:%02x SnIR:%02x SnSR:%02x\n", _socket, 
-        W5100.readSnMR(_socket), W5100.readSnIR(_socket), W5100.readSnSR(_socket));
-    uint8_t ip[4];
-    W5100.readSnDIPR(_socket, ip);
-    printf("Sn_DIPR: %d.%d.%d.%d Sn_DPORT: %d\n", ip[0], ip[1], ip[2], ip[3], W5100.readSnDPORT(_socket));
-    uint8_t mac[6];
-    W5100.readSnDHAR(_socket, mac);
-    printf("Sn_DHAR: %02x:%02x:%02x:%02x:%02x:%02x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
-    printf("Sn_RX_RSR:%d, Sn_RX_RD:%d, Sn_RX_WR:%d\n",
+    printf("%p socket:%d\n", this,_socket); 
+    if (_socket != (-1)) {
+        printf("SnMR:%02x SnIR:%02x SnSR:%02x\n",  
+            W5100.readSnMR(_socket), W5100.readSnIR(_socket), W5100.readSnSR(_socket));
+        uint8_t ip[4];
+        W5100.readSnDIPR(_socket, ip);
+        printf("Sn_DIPR: %d.%d.%d.%d Sn_DPORT: %d\n", ip[0], ip[1], ip[2], ip[3], W5100.readSnDPORT(_socket));
+        uint8_t mac[6];
+        W5100.readSnDHAR(_socket, mac);
+        printf("Sn_DHAR: %02x:%02x:%02x:%02x:%02x:%02x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
+        printf("Sn_RX_RSR:%5d, Sn_RX_RD:%5d, Sn_RX_WR:%5d\n",
                 W5100.readSnRX_RSR(_socket), W5100.readSnRX_RD(_socket), W5100.readSnRX_WR(_socket));
-    printf("Sn_TX_FSR:%d, Sn_TX_RD:%d, Sn_TX_WR:%d\n",
+        printf("Sn_TX_FSR:%5d, Sn_TX_RD:%5d, Sn_TX_WR:%5d\n",
                 W5100.readSnTX_FSR(_socket), W5100.readSnTX_RD(_socket), W5100.readSnTX_WR(_socket));
-    wait_ms(500);    
+    }
+    wait_ms(200);    
 #endif //DEBUG
-    if (W5100.getRXReceivedSize(_socket) > 0) {
-        queueEvent(NETUDPSOCKET_READABLE);
+    if (_socket != (-1)) {
+        if (W5100.getRXReceivedSize(_socket) > 0) {
+            queueEvent(NETUDPSOCKET_READABLE);
+        }
     }
     return NETUDPSOCKET_OK;
 }
@@ -114,13 +141,12 @@
 void MyNetUdpSocket::cleanUp() //Flush input buffer
 {
     PRINT_FUNC();
-    while(1) {
-        int len = W5100.getRXReceivedSize(_socket);
-        if (len <= 0) {
-            break;
-        }
-        uint8_t temp[8];
-        W5100.recv_data_processing(_socket, temp, sizeof(temp));
+    if (_socket == (-1)) {
+        return;
+    }
+    while(W5100.getRXReceivedSize(_socket) > 0) {
+        uint8_t temp[1];
+        W5100.recv_data_processing(_socket, temp, 1);
         W5100.execCmdSn(_socket, Sock_RECV);
     }    
 }