WIZnet Library for my tests.

Fork of WIZnet_Library by WIZnet

Files at this revision

API Documentation at this revision

Comitter:
Bongjun
Date:
Sun May 31 10:06:41 2015 +0000
Parent:
6:ca8405b9564d
Child:
8:cb8808b47e69
Commit message:
add set_blocking(false) in connect function, and so on.

Changed in this revision

WIZnetInterface/Socket/Endpoint.cpp Show annotated file Show diff for this revision Revisions of this file
WIZnetInterface/Socket/Socket.cpp Show annotated file Show diff for this revision Revisions of this file
WIZnetInterface/Socket/TCPSocketConnection.cpp Show annotated file Show diff for this revision Revisions of this file
WIZnetInterface/Socket/TCPSocketConnection.h Show annotated file Show diff for this revision Revisions of this file
WIZnetInterface/Socket/TCPSocketServer.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/WIZnetInterface/Socket/Endpoint.cpp	Fri Apr 24 08:19:14 2015 +0000
+++ b/WIZnetInterface/Socket/Endpoint.cpp	Sun May 31 10:06:41 2015 +0000
@@ -1,53 +1,54 @@
-/* Copyright (C) 2012 mbed.org, MIT License
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
- * and associated documentation files (the "Software"), to deal in the Software without restriction,
- * including without limitation the rights to use, copy, modify, merge, publish, distribute,
- * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all copies or
- * substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
- * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
- * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- */
-#include "Socket.h"
-#include "Endpoint.h"
-
-Endpoint::Endpoint()  {
-    reset_address();
-}
-Endpoint::~Endpoint() {}
-
-void Endpoint::reset_address(void) {
-    _ipAddress[0] = '\0';
-    _port = 0;
-}
-
-int Endpoint::set_address(const char* host, const int port) {
-    //Resolve DNS address or populate hard-coded IP address
-    WIZnet_Chip* eth = WIZnet_Chip::getInstance();
-    if (eth == NULL) {
-        return -1;
-    }
-    uint32_t addr;
-    if (!eth->gethostbyname(host, &addr)) {
-        return -1;
-    }
-    snprintf(_ipAddress, sizeof(_ipAddress), "%d.%d.%d.%d", (addr>>24)&0xff, (addr>>16)&0xff, (addr>>8)&0xff, addr&0xff);
-    _port = port;
-    return 0;
-}
-
-char* Endpoint::get_address() {
-    return _ipAddress;
-}
-
-int   Endpoint::get_port() {
-    return _port;
-}
+/* Copyright (C) 2012 mbed.org, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include "Socket.h"
+#include "Endpoint.h"
+
+Endpoint::Endpoint()  {
+    reset_address();
+}
+Endpoint::~Endpoint() {}
 
+void Endpoint::reset_address(void) {
+    _ipAddress[0] = '\0';
+    _port = 0;
+}
+
+int Endpoint::set_address(const char* host, const int port) {
+    //Resolve DNS address or populate hard-coded IP address
+    WIZnet_Chip* eth = WIZnet_Chip::getInstance();
+    if (eth == NULL) {
+        error("Endpoint constructor error: no WIZnet chip instance available!\r\n");
+        return -1;
+    }
+    uint32_t addr;
+    if (!eth->gethostbyname(host, &addr)) {
+        return -1;
+    }
+    snprintf(_ipAddress, sizeof(_ipAddress), "%d.%d.%d.%d", (addr>>24)&0xff, (addr>>16)&0xff, (addr>>8)&0xff, addr&0xff);
+    _port = port;
+    return 0;
+}
+
+char* Endpoint::get_address() {
+    return _ipAddress;
+}
+
+int   Endpoint::get_port() {
+    return _port;
+}
+
--- a/WIZnetInterface/Socket/Socket.cpp	Fri Apr 24 08:19:14 2015 +0000
+++ b/WIZnetInterface/Socket/Socket.cpp	Sun May 31 10:06:41 2015 +0000
@@ -1,40 +1,49 @@
-/* Copyright (C) 2012 mbed.org, MIT License
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
- * and associated documentation files (the "Software"), to deal in the Software without restriction,
- * including without limitation the rights to use, copy, modify, merge, publish, distribute,
- * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all copies or
- * substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
- * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
- * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- */
- 
-#include "Socket.h"
-
-Socket::Socket() : _sock_fd(-1),_blocking(true), _timeout(1500) {
-    eth = WIZnet_Chip::getInstance();
-    if (eth == NULL) {
-        error("Socket constructor error: no W5500 instance available!\r\n");
-    }       
-}
-
-void Socket::set_blocking(bool blocking, unsigned int timeout) {
-    _blocking = blocking;
-    _timeout = timeout;
-}
-
-int Socket::close() {
-    return (eth->close(_sock_fd)) ? 0 : -1;
-}
-
-Socket::~Socket() {
-    close(); //Don't want to leak
-}
+/* Copyright (C) 2012 mbed.org, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#include "Socket.h"
 
+Socket::Socket() : _sock_fd(-1),_blocking(true), _timeout(1500)
+{
+    eth = WIZnet_Chip::getInstance();
+    if (eth == NULL) {
+        error("Socket constructor error: no W5500 instance available!\r\n");
+    }
+}
+
+void Socket::set_blocking(bool blocking, unsigned int timeout)
+{
+    _blocking = blocking;
+    _timeout = timeout;
+}
+
+int Socket::close()
+{
+    // add this code refer from EthernetInterface.
+    // update by Patrick Pollet
+    int res;
+    res = eth->close(_sock_fd);
+    _sock_fd = -1;
+    return (res)? 0: -1;
+}
+
+Socket::~Socket()
+{
+    close(); //Don't want to leak
+}
+
--- a/WIZnetInterface/Socket/TCPSocketConnection.cpp	Fri Apr 24 08:19:14 2015 +0000
+++ b/WIZnetInterface/Socket/TCPSocketConnection.cpp	Sun May 31 10:06:41 2015 +0000
@@ -15,10 +15,16 @@
  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  */
+#include "TCPSocketConnection.h"
+#include <cstring>
 
-#include "TCPSocketConnection.h"
+using std::memset;
+using std::memcpy;
 
-TCPSocketConnection::TCPSocketConnection()
+// not a big code.
+// refer from EthernetInterface by mbed official driver
+TCPSocketConnection::TCPSocketConnection() :
+    _is_connected(false)
 {
 }
 
@@ -36,12 +42,17 @@
     if (!eth->connect(_sock_fd, get_address(), port)) {
         return -1;
     }
+    set_blocking(false);
+    // add code refer from EthernetInterface.
+    _is_connected = true;
     return 0;
 }
 
 bool TCPSocketConnection::is_connected(void)
 {
-    return eth->is_connected(_sock_fd);
+    // force update recent state.
+    _is_connected = eth->is_connected(_sock_fd);
+    return _is_connected;
 }
 
 
@@ -52,6 +63,10 @@
 
 int TCPSocketConnection::send(char* data, int length)
 {
+    // add to cover exception.
+    if ((_sock_fd < 0) || !_is_connected)
+        return -1;
+
     int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout);
     if (size < 0) {
         return -1;
@@ -65,6 +80,7 @@
 // -1 if unsuccessful, else number of bytes written
 int TCPSocketConnection::send_all(char* data, int length)
 {
+
     int writtenLen = 0;
     while (writtenLen < length) {
         int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout);
@@ -86,6 +102,10 @@
 // -1 if unsuccessful, else number of bytes received
 int TCPSocketConnection::receive(char* data, int length)
 {
+    // add to cover exception.
+    if ((_sock_fd < 0) || !_is_connected)
+        return -1;
+
     int size = eth->wait_readable(_sock_fd, _blocking ? -1 : _timeout);
     if (size < 0) {
         return -1;
--- a/WIZnetInterface/Socket/TCPSocketConnection.h	Fri Apr 24 08:19:14 2015 +0000
+++ b/WIZnetInterface/Socket/TCPSocketConnection.h	Sun May 31 10:06:41 2015 +0000
@@ -76,6 +76,9 @@
     \return the number of received bytes on success (>=0) or -1 on failure
     */
     int receive_all(char* data, int length);
+
+private:
+    bool _is_connected;    
 };
 
 #endif
--- a/WIZnetInterface/Socket/TCPSocketServer.cpp	Fri Apr 24 08:19:14 2015 +0000
+++ b/WIZnetInterface/Socket/TCPSocketServer.cpp	Sun May 31 10:06:41 2015 +0000
@@ -76,16 +76,21 @@
     uint16_t port = eth->sreg<uint16_t>(_sock_fd, Sn_DPORT);
     // change this server socket to connection socket.
     connection._sock_fd = _sock_fd;
+    connection._is_connected = true;
     connection.set_address(host, port);
 
     // and then, for the next connection, server socket should be assigned new one.
     _sock_fd = -1; // want to assign new available _sock_fd.
     if(bind(listen_port) < 0) {
-        error("No more socket for listening");
+        // modified by Patrick Pollet
+        error("No more socket for listening, bind error");
+        return -1;
     } else {
         //return -1;
         if(listen(1) < 0) {
-            error("No more socket for listening");
+            // modified by Patrick Pollet
+            error("No more socket for listening, listen error");
+            return -1;
         }
     }