wiznet.h

Dependencies:   mbed

Dependents:   RTOS_WebServer

Fork of WIZnet_Library by WIZnet

Files at this revision

API Documentation at this revision

Comitter:
Ademir501
Date:
Tue Jul 01 17:54:30 2014 +0000
Parent:
1:8138a268fbd2
Commit message:
wiznet.h;

Changed in this revision

WIZnetInterface/Socket/Socket.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
main.cpp Show diff for this revision Revisions of this file
--- a/WIZnetInterface/Socket/Socket.h	Fri May 09 01:17:51 2014 +0000
+++ b/WIZnetInterface/Socket/Socket.h	Tue Jul 01 17:54:30 2014 +0000
@@ -1,59 +1,59 @@
-/* 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.
- */
-#ifndef SOCKET_H_
-#define SOCKET_H_
-
-#include "wiznet.h"
-
-#define htons(x) __REV16(x)
-#define ntohs(x) __REV16(x)
-#define htonl(x) __REV(x)
-#define ntohl(x) __REV(x)
-
-/** Socket file descriptor and select wrapper
-  */
-class Socket {
-public:
-    /** Socket
-     */
-    Socket();
-    
-    /** Set blocking or non-blocking mode of the socket and a timeout on
-        blocking socket operations
-    \param blocking  true for blocking mode, false for non-blocking mode.
-    \param timeout   timeout in ms [Default: (1500)ms].
-    */
-    void set_blocking(bool blocking, unsigned int timeout=1500);
-    
-    /** Close the socket file descriptor
-     */
-    int close();
-    
-    ~Socket();
-    
-protected:
-    int _sock_fd;
-    bool _blocking;
-    int _timeout;
-
-    WIZnet_Chip* eth;
-};
-
-
-#endif /* SOCKET_H_ */
+/* 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.
+ */
+#ifndef SOCKET_H_
+#define SOCKET_H_
+
+#include "wiznet.h"
+
+#define htons(x) __REV16(x)
+#define ntohs(x) __REV16(x)
+#define htonl(x) __REV(x)
+#define ntohl(x) __REV(x)
 
+/** Socket file descriptor and select wrapper
+  */
+class Socket {
+public:
+    /** Socket
+     */
+    Socket();
+    
+    /** Set blocking or non-blocking mode of the socket and a timeout on
+        blocking socket operations
+    \param blocking  true for blocking mode, false for non-blocking mode.
+    \param timeout   timeout in ms [Default: (1500)ms].
+    */
+    void set_blocking(bool blocking, unsigned int timeout=1500);
+    
+    /** Close the socket file descriptor
+     */
+    int close();
+    
+    ~Socket();
+    
+protected:
+    int _sock_fd;
+    bool _blocking;
+    int _timeout;
+
+    WIZnet_Chip* eth;
+};
+
+
+#endif /* SOCKET_H_ */
+
--- a/WIZnetInterface/Socket/TCPSocketServer.cpp	Fri May 09 01:17:51 2014 +0000
+++ b/WIZnetInterface/Socket/TCPSocketServer.cpp	Tue Jul 01 17:54:30 2014 +0000
@@ -1,75 +1,75 @@
-/* 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 "TCPSocketServer.h"
-
-TCPSocketServer::TCPSocketServer() {}
-
-// Server initialization
-int TCPSocketServer::bind(int port) {
-    if (_sock_fd < 0) {
-        _sock_fd = eth->new_socket();
-        if (_sock_fd < 0) {
-            return -1;
-        }
-    }
-    // set TCP protocol
-    eth->setProtocol(_sock_fd, TCP);
-    // set local port
-    eth->sreg<uint16_t>(_sock_fd, Sn_PORT, port);
-    // connect the network
-    eth->scmd(_sock_fd, OPEN);
-    return 0;
-}
-
-int TCPSocketServer::listen(int backlog) {
-    if (_sock_fd < 0) {
-        return -1;
-    }
-    if (backlog != 1) {
-        return -1;
-    }
-    eth->scmd(_sock_fd, LISTEN);
-    return 0;
-}
-
-
-int TCPSocketServer::accept(TCPSocketConnection& connection) {
-    if (_sock_fd < 0) {
-        return -1;
-    }
-    Timer t;
-    t.reset();
-    t.start();
-    while(1) {
-        if (t.read_ms() > _timeout && _blocking == false) {
-            return -1;
-        }
-        if (eth->sreg<uint8_t>(_sock_fd, Sn_SR) == SOCK_ESTABLISHED) {
-            break;
-        }
-    }
-    uint32_t ip = eth->sreg<uint32_t>(_sock_fd, Sn_DIPR);
-    char host[16];
-    snprintf(host, sizeof(host), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff);
-    uint16_t port = eth->sreg<uint16_t>(_sock_fd, Sn_DPORT);
-    connection._sock_fd = _sock_fd;
-    connection.set_address(host, port);
-    return 0;
-}
+/* 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 "TCPSocketServer.h"
+
+TCPSocketServer::TCPSocketServer() {}
 
+// Server initialization
+int TCPSocketServer::bind(int port) {
+    if (_sock_fd < 0) {
+        _sock_fd = eth->new_socket();
+        if (_sock_fd < 0) {
+            return -1;
+        }
+    }
+    // set TCP protocol
+    eth->setProtocol(_sock_fd, TCP);
+    // set local port
+    eth->sreg<uint16_t>(_sock_fd, Sn_PORT, port);
+    // connect the network
+    eth->scmd(_sock_fd, OPEN);
+    return 0;
+}
+
+int TCPSocketServer::listen(int backlog) {
+    if (_sock_fd < 0) {
+        return -1;
+    }
+    if (backlog != 1) {
+        return -1;
+    }
+    eth->scmd(_sock_fd, LISTEN);
+    return 0;
+}
+
+
+int TCPSocketServer::accept(TCPSocketConnection& connection) {
+    if (_sock_fd < 0) {
+        return -1;
+    }
+    Timer t;
+    t.reset();
+    t.start();
+    while(1) {
+        if (t.read_ms()==1500/* > _timeout && _blocking == false*/) {
+            return -1;
+        }
+        if (eth->sreg<uint8_t>(_sock_fd, Sn_SR) == SOCK_ESTABLISHED) {
+            break;
+        }
+    }
+    uint32_t ip = eth->sreg<uint32_t>(_sock_fd, Sn_DIPR);
+    char host[16];
+    snprintf(host, sizeof(host), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff);
+    uint16_t port = eth->sreg<uint16_t>(_sock_fd, Sn_DPORT);
+    connection._sock_fd = _sock_fd;
+    connection.set_address(host, port);
+    return 0;
+}
+
--- a/main.cpp	Fri May 09 01:17:51 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,113 +0,0 @@
-/*
-*
-*  Test program for W5500 mbed Library
-
-*/
-#include "mbed.h"
-#include "WIZnetInterface.h"
-
-#define ECHO_SERVER_PORT   5000
-const char* ECHO_SERVER_ADDRESS = "192.168.1.229";  // Server IP address
-
-/** 
-* Setting DHCP or static
-*/
-//#define USE_DHCP
-
-/** 
-* Setting the platform to test
-*/
-#define LPC
-//#define ST_NUCLEO
-//#define FRDM_KL25Z
-//#define Seeeduino_Arch
-
-#ifdef LPC
-// LPC1768 & LPC11U24
-SPI spi(p5, p6, p7); // mosi, miso, sclk
-WIZnetInterface eth(&spi, p8, p9); // spi, cs, reset
-#endif
-
-#ifdef ST_NUCLEO
-// ST Nucleo
-SPI spi(PA_7, PA_6, PA_5); // mosi, miso, sclk
-WIZnetInterface eth(&spi, PB_6, PA_10); // spi, cs, reset
-#endif
-
-#ifdef FRDM_KL25Z
-// Freescale FRDM KL25Z
-SPI spi(PTD2, PTD3, PTD1); // mosi, miso, sclk
-WIZnetInterface eth(&spi, PTD0, PTA20); // spi, cs, reset
-#endif
-
-#ifdef Seeeduino_Arch
-// Seeedstudio Arch
-SPI spi(P1_22, P1_21, P1_20); // mosi, miso, sclk
-WIZnetInterface eth(&spi, P0_2, P0_0); // spi, cs, reset
-    Serial pc(P1_13, P1_14); // tx, rx
-#else
-    Serial pc(USBTX,USBRX);
-#endif
-
-#ifndef USE_DHCP
-// for static IP setting
-const char * IP_Addr    = "192.168.1.120";
-const char * IP_Subnet  = "255.255.255.0";
-const char * IP_Gateway = "192.168.1.111";
-#endif
-
-
-int main()
-{
-    uint8_t mac[6];
-    
-    mbed_mac_address((char *)mac);     // using the MAC address in LPC11U24 or LPC1178
-//    mac[0] = 0x00; mac[1] = 0x08; mac[2] = 0xDC; mac[3] = 0x00; mac[4] = 0x00; mac[5] = 0x00; 
-// you can alo use WIZ550io's MAC address by enabling "USE_WIZ550IO_MAC" in wiznet.h
-    
-    pc.printf("Start\n");
-    #ifdef USE_DHCP
-      int ret = eth.init(mac); //Use DHCP
-    #else
-      int ret = eth.init(mac, IP_Addr, IP_Subnet, IP_Gateway); // static
-    #endif
-
-    if (!ret) {
-        pc.printf("Initialized, MAC: %s\n", eth.getMACAddress());
-    } else {
-        pc.printf("Error eth.init() - ret = %d\n", ret);
-        return -1;
-    }
-
-    ret = eth.connect();
-    if (!ret) {
-        pc.printf("IP: %s, MASK: %s, GW: %s\n",
-                  eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway());
-    } else {
-        pc.printf("Error eth.connect() - ret = %d\n", ret);
-        return -1;
-    }
-
-    TCPSocketServer server;
-    server.bind(ECHO_SERVER_PORT);
-    server.listen();
-
-    while (true) {
-        pc.printf("\nWait for new connection...\n");
-        TCPSocketConnection client;
-        server.accept(client);
-        //client.set_blocking(false, 1500); // Timeout after (1.5)s
-
-        pc.printf("Connection from: %s\n", client.get_address());
-        char buffer[256];
-        while (true) {
-            int n = client.receive(buffer, sizeof(buffer));
-            if (n <= 0) break;
-
-            client.send_all(buffer, n);
-            if (n <= 0) break;
-        }
-
-        client.close();
-    }
-}