.
Fork of WIZnet_Library by
Revision 6:ca8405b9564d, committed 2015-04-24
- Comitter:
- bangbh
- Date:
- Fri Apr 24 08:19:14 2015 +0000
- Parent:
- 5:89edb39d8707
- Child:
- 7:c1f1122adf7b
- Child:
- 8:7c67a8922ec5
- Commit message:
- "bool is_fin_received()" method added.
Changed in this revision
--- a/WIZnetInterface/Socket/TCPSocketConnection.cpp Mon Oct 06 01:35:09 2014 +0000 +++ b/WIZnetInterface/Socket/TCPSocketConnection.cpp Fri Apr 24 08:19:14 2015 +0000 @@ -1,113 +1,119 @@ -/* 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 "TCPSocketConnection.h" - -TCPSocketConnection::TCPSocketConnection() -{ -} - -int TCPSocketConnection::connect(const char* host, const int port) -{ - if (_sock_fd < 0) { - _sock_fd = eth->new_socket(); - if (_sock_fd < 0) { - return -1; - } - } - if (set_address(host, port) != 0) { - return -1; - } - if (!eth->connect(_sock_fd, get_address(), port)) { - return -1; - } - return 0; -} - -bool TCPSocketConnection::is_connected(void) -{ - return eth->is_connected(_sock_fd); -} - -int TCPSocketConnection::send(char* data, int length) -{ - int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout); - if (size < 0) { - return -1; - } - if (size > length) { - size = length; - } - return eth->send(_sock_fd, data, size); -} - -// -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); - if (size < 0) { - return -1; - } - if (size > (length-writtenLen)) { - size = (length-writtenLen); - } - int ret = eth->send(_sock_fd, data + writtenLen, size); - if (ret < 0) { - return -1; - } - writtenLen += ret; - } - return writtenLen; -} - -// -1 if unsuccessful, else number of bytes received -int TCPSocketConnection::receive(char* data, int length) -{ - int size = eth->wait_readable(_sock_fd, _blocking ? -1 : _timeout); - if (size < 0) { - return -1; - } - if (size > length) { - size = length; - } - return eth->recv(_sock_fd, data, size); -} - -// -1 if unsuccessful, else number of bytes received -int TCPSocketConnection::receive_all(char* data, int length) -{ - int readLen = 0; - while (readLen < length) { - int size = eth->wait_readable(_sock_fd, _blocking ? -1 :_timeout); - if (size <= 0) { - break; - } - if (size > (length - readLen)) { - size = length - readLen; - } - int ret = eth->recv(_sock_fd, data + readLen, size); - if (ret < 0) { - return -1; - } - readLen += ret; - } - return readLen; -} +/* 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 "TCPSocketConnection.h" + +TCPSocketConnection::TCPSocketConnection() +{ +} + +int TCPSocketConnection::connect(const char* host, const int port) +{ + if (_sock_fd < 0) { + _sock_fd = eth->new_socket(); + if (_sock_fd < 0) { + return -1; + } + } + if (set_address(host, port) != 0) { + return -1; + } + if (!eth->connect(_sock_fd, get_address(), port)) { + return -1; + } + return 0; +} + +bool TCPSocketConnection::is_connected(void) +{ + return eth->is_connected(_sock_fd); +} + + +bool TCPSocketConnection::is_fin_received(void) +{ + return eth->is_fin_received(_sock_fd); +} +int TCPSocketConnection::send(char* data, int length) +{ + int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout); + if (size < 0) { + return -1; + } + if (size > length) { + size = length; + } + return eth->send(_sock_fd, data, size); +} + +// -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); + if (size < 0) { + return -1; + } + if (size > (length-writtenLen)) { + size = (length-writtenLen); + } + int ret = eth->send(_sock_fd, data + writtenLen, size); + if (ret < 0) { + return -1; + } + writtenLen += ret; + } + return writtenLen; +} + +// -1 if unsuccessful, else number of bytes received +int TCPSocketConnection::receive(char* data, int length) +{ + int size = eth->wait_readable(_sock_fd, _blocking ? -1 : _timeout); + if (size < 0) { + return -1; + } + if (size > length) { + size = length; + } + return eth->recv(_sock_fd, data, size); +} + +// -1 if unsuccessful, else number of bytes received +int TCPSocketConnection::receive_all(char* data, int length) +{ + int readLen = 0; + while (readLen < length) { + int size = eth->wait_readable(_sock_fd, _blocking ? -1 :_timeout); + if (size <= 0) { + break; + } + if (size > (length - readLen)) { + size = length - readLen; + } + int ret = eth->recv(_sock_fd, data + readLen, size); + if (ret < 0) { + return -1; + } + readLen += ret; + } + return readLen; +} +
--- a/WIZnetInterface/Socket/TCPSocketConnection.h Mon Oct 06 01:35:09 2014 +0000 +++ b/WIZnetInterface/Socket/TCPSocketConnection.h Fri Apr 24 08:19:14 2015 +0000 @@ -1,78 +1,82 @@ -/* 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 TCPSOCKET_H -#define TCPSOCKET_H - -#include "Socket.h" -#include "Endpoint.h" - -/** -TCP socket connection -*/ -class TCPSocketConnection: public Socket, public Endpoint { - friend class TCPSocketServer; - -public: - /** TCP socket connection - */ - TCPSocketConnection(); - - /** Connects this TCP socket to the server - \param host The host to connect to. It can either be an IP Address or a hostname that will be resolved with DNS. - \param port The host's port to connect to. - \return 0 on success, -1 on failure. - */ - int connect(const char* host, const int port); - - /** Check if the socket is connected - \return true if connected, false otherwise. - */ - bool is_connected(void); - - /** Send data to the remote host. - \param data The buffer to send to the host. - \param length The length of the buffer to send. - \return the number of written bytes on success (>=0) or -1 on failure - */ - int send(char* data, int length); - - /** Send all the data to the remote host. - \param data The buffer to send to the host. - \param length The length of the buffer to send. - \return the number of written bytes on success (>=0) or -1 on failure - */ - int send_all(char* data, int length); - - /** Receive data from the remote host. - \param data The buffer in which to store the data received from the host. - \param length The maximum length of the buffer. - \return the number of received bytes on success (>=0) or -1 on failure - */ - int receive(char* data, int length); - - /** Receive all the data from the remote host. - \param data The buffer in which to store the data received from the host. - \param length The maximum length of the buffer. - \return the number of received bytes on success (>=0) or -1 on failure - */ - int receive_all(char* data, int length); -}; - -#endif +/* 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 TCPSOCKET_H +#define TCPSOCKET_H + +#include "Socket.h" +#include "Endpoint.h" +/** +TCP socket connection +*/ +class TCPSocketConnection: public Socket, public Endpoint { + friend class TCPSocketServer; + +public: + /** TCP socket connection + */ + TCPSocketConnection(); + + /** Connects this TCP socket to the server + \param host The host to connect to. It can either be an IP Address or a hostname that will be resolved with DNS. + \param port The host's port to connect to. + \return 0 on success, -1 on failure. + */ + int connect(const char* host, const int port); + + /** Check if the socket is connected + \return true if connected, false otherwise. + */ + bool is_connected(void); + + /** Check if FIN signal received. + \return true if received, false otherwise. + */ + bool is_fin_received(void); + /** Send data to the remote host. + \param data The buffer to send to the host. + \param length The length of the buffer to send. + \return the number of written bytes on success (>=0) or -1 on failure + */ + int send(char* data, int length); + + /** Send all the data to the remote host. + \param data The buffer to send to the host. + \param length The length of the buffer to send. + \return the number of written bytes on success (>=0) or -1 on failure + */ + int send_all(char* data, int length); + + /** Receive data from the remote host. + \param data The buffer in which to store the data received from the host. + \param length The maximum length of the buffer. + \return the number of received bytes on success (>=0) or -1 on failure + */ + int receive(char* data, int length); + + /** Receive all the data from the remote host. + \param data The buffer in which to store the data received from the host. + \param length The maximum length of the buffer. + \return the number of received bytes on success (>=0) or -1 on failure + */ + int receive_all(char* data, int length); +}; + +#endif +
--- a/WIZnetInterface/WIZnet/W5500.cpp Mon Oct 06 01:35:09 2014 +0000 +++ b/WIZnetInterface/WIZnet/W5500.cpp Fri Apr 24 08:19:14 2015 +0000 @@ -126,11 +126,6 @@ bool WIZnet_Chip::is_connected(int socket) { - /* - if (sreg<uint8_t>(socket, Sn_SR) == SOCK_ESTABLISHED) { - return true; - } - */ uint8_t tmpSn_SR; tmpSn_SR = sreg<uint8_t>(socket, Sn_SR); // packet sending is possible, when state is SOCK_CLOSE_WAIT. @@ -140,6 +135,17 @@ return false; } + +bool WIZnet_Chip::is_fin_received(int socket) +{ + uint8_t tmpSn_SR; + tmpSn_SR = sreg<uint8_t>(socket, Sn_SR); + // packet sending is possible, when state is SOCK_CLOSE_WAIT. + if (tmpSn_SR == SOCK_CLOSE_WAIT) { + return true; + } + return false; +} // Reset the chip & set the buffer void WIZnet_Chip::reset() {
--- a/WIZnetInterface/WIZnet/W5500.h Mon Oct 06 01:35:09 2014 +0000 +++ b/WIZnetInterface/WIZnet/W5500.h Fri Apr 24 08:19:14 2015 +0000 @@ -146,6 +146,13 @@ */ bool is_connected(int socket); + /* + * Check if FIN received. + * + * @returns true if successful + */ + bool is_fin_received(int socket); + /* * Close a tcp connection *
--- a/WIZnetInterface/WIZnetInterface.cpp Mon Oct 06 01:35:09 2014 +0000 +++ b/WIZnetInterface/WIZnetInterface.cpp Fri Apr 24 08:19:14 2015 +0000 @@ -114,7 +114,7 @@ if (err == (-1)) { return -1; } -// printf("Connected, IP: %d.%d.%d.%d\n", dhcp.yiaddr[0], dhcp.yiaddr[1], dhcp.yiaddr[2], dhcp.yiaddr[3]); + // printf("Connected, IP: %d.%d.%d.%d\n", dhcp.yiaddr[0], dhcp.yiaddr[1], dhcp.yiaddr[2], dhcp.yiaddr[3]); ip = (dhcp.yiaddr[0] <<24) | (dhcp.yiaddr[1] <<16) | (dhcp.yiaddr[2] <<8) | dhcp.yiaddr[3]; gateway = (dhcp.gateway[0]<<24) | (dhcp.gateway[1]<<16) | (dhcp.gateway[2]<<8) | dhcp.gateway[3]; netmask = (dhcp.netmask[0]<<24) | (dhcp.netmask[1]<<16) | (dhcp.netmask[2]<<8) | dhcp.netmask[3];
--- a/main.cpp Mon Oct 06 01:35:09 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(); - } -}
--- a/mbed.bld Mon Oct 06 01:35:09 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/8a40adfe8776 \ No newline at end of file