Library for ethernet

Dependents:   VC0706_FTP_Client_Ethernet_MQTT

Fork of WIZnetInterface by Akshay Tom

Committer:
Albinarackal
Date:
Mon Jul 16 09:21:17 2018 +0000
Revision:
22:01ea4b74d8eb
Parent:
21:584efa51ee60
For MQTT and FTP connection

Who changed what in which revision?

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