This is WIZnet Ethernet Interface using Hardware TCP/IP chip, W5500, W5200 and W5100. One of them can be selected by enabling it in wiznet.h.

Dependents:   bigthingRecfinal

Fork of WIZnet_Library by WIZnet

Committer:
rohangala
Date:
Tue Mar 15 11:36:55 2016 +0000
Revision:
9:8b3bc4434031
Parent:
7:7c67a8922ec5
final without caliberated;

Who changed what in which revision?

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