Ethernet interface for W5500 with bug fixed in socket::close()

Fork of EthernetInterfaceW5500 by W5500-Ethernet-Interface Makers

Committer:
Bongjun
Date:
Thu Jul 17 07:10:36 2014 +0000
Revision:
10:cadac6bcd169
Parent:
5:fb15c35d1e28
Child:
14:a089e591e530
Some update & code clean for W5500 only refer from WIZ550ioInterface, WIZnetLibrary and WiflyInterface.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 1:fb4494783863 1 /* Copyright (C) 2012 mbed.org, MIT License
samux 1:fb4494783863 2 *
samux 1:fb4494783863 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
samux 1:fb4494783863 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
samux 1:fb4494783863 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
samux 1:fb4494783863 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
samux 1:fb4494783863 7 * furnished to do so, subject to the following conditions:
samux 1:fb4494783863 8 *
samux 1:fb4494783863 9 * The above copyright notice and this permission notice shall be included in all copies or
samux 1:fb4494783863 10 * substantial portions of the Software.
samux 1:fb4494783863 11 *
samux 1:fb4494783863 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
samux 1:fb4494783863 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
samux 1:fb4494783863 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
samux 1:fb4494783863 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
samux 1:fb4494783863 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
samux 1:fb4494783863 17 */
Bongjun 10:cadac6bcd169 18 #include "TCPSocketConnection.h"
Bongjun 10:cadac6bcd169 19 #include <cstring>
samux 1:fb4494783863 20
Bongjun 10:cadac6bcd169 21 using std::memset;
Bongjun 10:cadac6bcd169 22 using std::memcpy;
samux 1:fb4494783863 23
Bongjun 10:cadac6bcd169 24 // not a big code.
Bongjun 10:cadac6bcd169 25 // refer from EthernetInterface by mbed official driver
Bongjun 10:cadac6bcd169 26 TCPSocketConnection::TCPSocketConnection() :
Bongjun 10:cadac6bcd169 27 _is_connected(false)
va009039 5:fb15c35d1e28 28 {
va009039 5:fb15c35d1e28 29 }
samux 1:fb4494783863 30
samux 1:fb4494783863 31 int TCPSocketConnection::connect(const char* host, const int port)
Bongjun 10:cadac6bcd169 32 {
va009039 5:fb15c35d1e28 33 if (_sock_fd < 0) {
va009039 5:fb15c35d1e28 34 _sock_fd = eth->new_socket();
va009039 5:fb15c35d1e28 35 if (_sock_fd < 0) {
va009039 5:fb15c35d1e28 36 return -1;
va009039 5:fb15c35d1e28 37 }
va009039 5:fb15c35d1e28 38 }
va009039 5:fb15c35d1e28 39 if (set_address(host, port) != 0) {
samux 1:fb4494783863 40 return -1;
Bongjun 10:cadac6bcd169 41 }
va009039 5:fb15c35d1e28 42 if (!eth->connect(_sock_fd, get_address(), port)) {
va009039 5:fb15c35d1e28 43 return -1;
va009039 5:fb15c35d1e28 44 }
Bongjun 10:cadac6bcd169 45 // add code refer from EthernetInterface.
Bongjun 10:cadac6bcd169 46 _is_connected = true;
Bongjun 10:cadac6bcd169 47
samux 1:fb4494783863 48 return 0;
samux 1:fb4494783863 49 }
samux 1:fb4494783863 50
samux 1:fb4494783863 51 bool TCPSocketConnection::is_connected(void)
samux 1:fb4494783863 52 {
Bongjun 10:cadac6bcd169 53 // force update recent state.
Bongjun 10:cadac6bcd169 54 _is_connected = eth->is_connected(_sock_fd);
Bongjun 10:cadac6bcd169 55 return _is_connected;
samux 1:fb4494783863 56 }
samux 1:fb4494783863 57
samux 1:fb4494783863 58 int TCPSocketConnection::send(char* data, int length)
samux 1:fb4494783863 59 {
Bongjun 10:cadac6bcd169 60 // add to cover exception.
Bongjun 10:cadac6bcd169 61 if ((_sock_fd < 0) || !_is_connected)
Bongjun 10:cadac6bcd169 62 return -1;
Bongjun 10:cadac6bcd169 63
va009039 5:fb15c35d1e28 64 int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout);
va009039 5:fb15c35d1e28 65 if (size < 0) {
va009039 5:fb15c35d1e28 66 return -1;
samux 1:fb4494783863 67 }
va009039 5:fb15c35d1e28 68 if (size > length) {
va009039 5:fb15c35d1e28 69 size = length;
va009039 5:fb15c35d1e28 70 }
va009039 5:fb15c35d1e28 71 return eth->send(_sock_fd, data, size);
samux 1:fb4494783863 72 }
samux 1:fb4494783863 73
samux 1:fb4494783863 74 // -1 if unsuccessful, else number of bytes written
samux 1:fb4494783863 75 int TCPSocketConnection::send_all(char* data, int length)
samux 1:fb4494783863 76 {
va009039 5:fb15c35d1e28 77 int writtenLen = 0;
va009039 5:fb15c35d1e28 78 while (writtenLen < length) {
va009039 5:fb15c35d1e28 79 int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout);
va009039 5:fb15c35d1e28 80 if (size < 0) {
va009039 5:fb15c35d1e28 81 return -1;
va009039 5:fb15c35d1e28 82 }
va009039 5:fb15c35d1e28 83 if (size > (length-writtenLen)) {
va009039 5:fb15c35d1e28 84 size = (length-writtenLen);
va009039 5:fb15c35d1e28 85 }
va009039 5:fb15c35d1e28 86 int ret = eth->send(_sock_fd, data + writtenLen, size);
va009039 5:fb15c35d1e28 87 if (ret < 0) {
va009039 5:fb15c35d1e28 88 return -1;
va009039 5:fb15c35d1e28 89 }
va009039 5:fb15c35d1e28 90 writtenLen += ret;
samux 1:fb4494783863 91 }
va009039 5:fb15c35d1e28 92 return writtenLen;
samux 1:fb4494783863 93 }
samux 1:fb4494783863 94
samux 1:fb4494783863 95 // -1 if unsuccessful, else number of bytes received
samux 1:fb4494783863 96 int TCPSocketConnection::receive(char* data, int length)
samux 1:fb4494783863 97 {
Bongjun 10:cadac6bcd169 98 // add to cover exception.
Bongjun 10:cadac6bcd169 99 if ((_sock_fd < 0) || !_is_connected)
Bongjun 10:cadac6bcd169 100 return -1;
Bongjun 10:cadac6bcd169 101
va009039 5:fb15c35d1e28 102 int size = eth->wait_readable(_sock_fd, _blocking ? -1 : _timeout);
va009039 5:fb15c35d1e28 103 if (size < 0) {
va009039 5:fb15c35d1e28 104 return -1;
samux 1:fb4494783863 105 }
va009039 5:fb15c35d1e28 106 if (size > length) {
va009039 5:fb15c35d1e28 107 size = length;
samux 1:fb4494783863 108 }
va009039 5:fb15c35d1e28 109 return eth->recv(_sock_fd, data, size);
samux 1:fb4494783863 110 }
samux 1:fb4494783863 111
samux 1:fb4494783863 112 // -1 if unsuccessful, else number of bytes received
samux 1:fb4494783863 113 int TCPSocketConnection::receive_all(char* data, int length)
samux 1:fb4494783863 114 {
va009039 5:fb15c35d1e28 115 int readLen = 0;
va009039 5:fb15c35d1e28 116 while (readLen < length) {
va009039 5:fb15c35d1e28 117 int size = eth->wait_readable(_sock_fd, _blocking ? -1 :_timeout);
va009039 5:fb15c35d1e28 118 if (size <= 0) {
va009039 5:fb15c35d1e28 119 break;
va009039 5:fb15c35d1e28 120 }
va009039 5:fb15c35d1e28 121 if (size > (length - readLen)) {
va009039 5:fb15c35d1e28 122 size = length - readLen;
samux 1:fb4494783863 123 }
va009039 5:fb15c35d1e28 124 int ret = eth->recv(_sock_fd, data + readLen, size);
va009039 5:fb15c35d1e28 125 if (ret < 0) {
va009039 5:fb15c35d1e28 126 return -1;
va009039 5:fb15c35d1e28 127 }
va009039 5:fb15c35d1e28 128 readLen += ret;
samux 1:fb4494783863 129 }
va009039 5:fb15c35d1e28 130 return readLen;
Bongjun 10:cadac6bcd169 131 }