Custom and bugfix

Dependents:   HTTPClient_HelloWorld_WIZ820io NTPClient_HelloWorld_WIZ820io TinyHTTPServer_WIZ820io

Fork of WIZ820ioInterface by Norimasa Okamoto

Revision:
5:fb15c35d1e28
Parent:
4:0bcec6272784
Child:
7:c0cd6680bcb7
diff -r 0bcec6272784 -r fb15c35d1e28 Socket/UDPSocket.cpp
--- a/Socket/UDPSocket.cpp	Thu Dec 20 15:08:58 2012 +0000
+++ b/Socket/UDPSocket.cpp	Tue Aug 27 12:50:11 2013 +0000
@@ -18,168 +18,79 @@
 
 #include "UDPSocket.h"
 
-#include <string>
-#include <algorithm>
-
 UDPSocket::UDPSocket()
 {
-    endpoint_configured = false;
-    endpoint_read = false;
 }
 
 int UDPSocket::init(void)
 {
-    wifi->setProtocol(UDP);
-    wifi->exit();
+    if (_sock_fd < 0) {
+        _sock_fd = eth->new_socket();
+    }
+    eth->setProtocol(_sock_fd, UDP);
     return 0;
 }
 
 // Server initialization
 int UDPSocket::bind(int port)
 {
-    char cmd[17];
-    
+    if (_sock_fd < 0) {
+        _sock_fd = eth->new_socket();
+        if (_sock_fd < 0) {
+            return -1;
+        }
+    }
     // set local port
-    sprintf(cmd, "set i l %d\r", port);
-    if (!wifi->sendCommand(cmd, "AOK"))
-        return -1;
-        
-    // save
-    if (!wifi->sendCommand("save\r", "Stor"))
-        return -1;
-    
-    // reboot
-    wifi->reboot();
-    
+    eth->sreg<uint16_t>(_sock_fd, Sn_PORT, port);
     // set udp protocol
-    wifi->setProtocol(UDP);
-    
-    // connect the network
-    if (wifi->isDHCP()) {
-        if (!wifi->sendCommand("join\r", "DHCP=ON", NULL, 10000))
-            return -1;
-    } else {
-        if (!wifi->sendCommand("join\r", "Associated", NULL, 10000))
-            return -1;
-    }
-        
-    // exit
-    wifi->exit();
-    wifi->flush();
+    eth->setProtocol(_sock_fd, UDP);
+    eth->scmd(_sock_fd, OPEN);
     return 0;
 }
 
 // -1 if unsuccessful, else number of bytes written
 int UDPSocket::sendTo(Endpoint &remote, char *packet, int length)
 {
-    Timer tmr;
-    int idx = 0;
-
+    int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout, length-1);
+    if (size < 0) {
+        return -1;
+    }
     confEndpoint(remote);
-
-    tmr.start();
-
-    while ((tmr.read_ms() < _timeout) || _blocking) {
-
-        idx += wifi->send(packet, length);
-
-        if (idx == length)
-            return idx;
-    }
-    return (idx == 0) ? -1 : idx;
+    int ret = eth->send(_sock_fd, packet, length);
+    return ret;
 }
 
 // -1 if unsuccessful, else number of bytes received
 int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length)
 {
-    Timer tmr;
-    int idx = 0;
-    int nb_available = 0;
-    int time = -1;
-
-    if (_blocking) {
-        while (1) {
-            nb_available = wifi->readable();
-            if (nb_available != 0) {
-                break;
-            }
-        }
+    uint8_t info[8];
+    int size = eth->wait_readable(_sock_fd, _blocking ? -1 : _timeout, sizeof(info));
+    if (size < 0) {
+        return -1;
     }
-
-    tmr.start();
-
-    while (time < _timeout) {
-
-        nb_available = wifi->readable();
-        for (int i = 0; i < min(nb_available, length); i++) {
-            buffer[idx] = wifi->getc();
-            idx++;
-        }
-
-        if (idx == length) {
-            break;
-        }
-        
-        time = tmr.read_ms();
+    eth->recv(_sock_fd, (char*)info, sizeof(info));
+    readEndpoint(remote, info);
+    int udp_size = info[6]<<8|info[7]; 
+    //TEST_ASSERT(udp_size <= (size-sizeof(info)));
+    if (udp_size > (size-sizeof(info))) {
+        return -1;
     }
-
-    readEndpoint(remote);
-    return (idx == 0) ? -1 : idx;
+    return eth->recv(_sock_fd, buffer, udp_size);
 }
 
-bool UDPSocket::confEndpoint(Endpoint & ep)
+void UDPSocket::confEndpoint(Endpoint & ep)
 {
-    char * host;
-    char cmd[30];
-    if (!endpoint_configured) {
-        host = ep.get_address();
-        if (host[0] != '\0') {
-            // set host
-            sprintf(cmd, "set i h %s\r", host);
-            if (!wifi->sendCommand(cmd, "AOK"))
-                return false;
-                
-            // set remote port
-            sprintf(cmd, "set i r %d\r", ep.get_port());
-            if (!wifi->sendCommand(cmd, "AOK"))
-                return false;
-                
-            wifi->exit();
-            endpoint_configured = true;
-            return true;
-        }
-    }
-    return true;
+    char * host = ep.get_address();
+    // set remote host
+    eth->sreg_ip(_sock_fd, Sn_DIPR, host);
+    // set remote port
+    eth->sreg<uint16_t>(_sock_fd, Sn_DPORT, ep.get_port());
 }
 
-bool UDPSocket::readEndpoint(Endpoint & ep)
+void UDPSocket::readEndpoint(Endpoint & ep, uint8_t info[])
 {
-    char recv[256];
-    int begin = 0;
-    int end = 0;
-    string str;
-    string addr;
-    int port;
-    if (!endpoint_read) {
-        if (!wifi->sendCommand("get ip\r", NULL, recv))
-            return false;
-        wifi->exit();
-        str = recv;
-        begin = str.find("HOST=");
-        end = str.find("PROTO=");
-        if (begin != string::npos && end != string::npos) {
-            str = str.substr(begin + 5, end - begin - 5);
-            int pos = str.find(":");
-            if (pos != string::npos) {
-                addr = str.substr(0, pos);
-                port = atoi(str.substr(pos + 1).c_str());
-                ep.set_address(addr.c_str(), port);
-                endpoint_read = true;
-                wifi->flush();
-                return true;
-            }
-        }
-        wifi->flush();
-    }
-    return false;
+    char addr[17];
+    snprintf(addr, sizeof(addr), "%d.%d.%d.%d", info[0], info[1], info[2], info[3]);
+    uint16_t port = info[4]<<8|info[5];
+    ep.set_address(addr, port);
 }