Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
TCPSocketConnection.cpp
00001 /* Copyright (C) 2012 mbed.org, MIT License 00002 * 00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00004 * and associated documentation files (the "Software"), to deal in the Software without restriction, 00005 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 00006 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 00007 * furnished to do so, subject to the following conditions: 00008 * 00009 * The above copyright notice and this permission notice shall be included in all copies or 00010 * substantial portions of the Software. 00011 * 00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00017 */ 00018 #include "TCPSocketConnection.h" 00019 #include <cstring> 00020 #include <algorithm> 00021 00022 using std::memset; 00023 using std::memcpy; 00024 00025 //Debug is disabled by default 00026 #if 1 00027 #define DBG(x, ...) printf("[TCPConnection : DBG]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__); 00028 #define WARN(x, ...) printf("[TCPConnection: WARN]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__); 00029 #define ERR(x, ...) printf("[TCPConnection : ERR]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__); 00030 #else 00031 #define DBG(x, ...) 00032 #define WARN(x, ...) 00033 #define ERR(x, ...) 00034 #endif 00035 00036 TCPSocketConnection::TCPSocketConnection() : 00037 _is_connected(false) 00038 { 00039 } 00040 00041 int TCPSocketConnection::connect(const char* host, const int port) 00042 { 00043 // if (init_socket(SOCK_STREAM) < 0) 00044 // return -1; 00045 // 00046 if (set_address(host, port) != 0) 00047 return -1; 00048 // 00049 // if (lwip_connect(_sock_fd, (const struct sockaddr *) &_remoteHost, sizeof(_remoteHost)) < 0) { 00050 // close(); 00051 // return -1; 00052 // } 00053 // _is_connected = true; 00054 _is_connected = ESP8266->start(ESP_TCP_TYPE,_ipAddress,_port); 00055 if(_is_connected) { //success 00056 return 0; 00057 } else { // fail 00058 return -1; 00059 } 00060 } 00061 00062 bool TCPSocketConnection::is_connected(void) 00063 { 00064 return _is_connected; 00065 } 00066 00067 int TCPSocketConnection::send(char* data, int length) 00068 { 00069 if (!_is_connected) { 00070 ERR("TCPSocketConnection::receive() - _is_connected is false : you cant receive data until you connect to a socket!"); 00071 return -1; 00072 } 00073 Timer tmr; 00074 int idx = 0; 00075 tmr.start(); 00076 while ((tmr.read_ms() < _timeout) || _blocking) { 00077 00078 idx += wifi->send(data, length); 00079 00080 if (idx == length) 00081 return idx; 00082 } 00083 return (idx == 0) ? -1 : idx; 00084 00085 //return wifi->send(data,length); 00086 // 00087 // if (!_blocking) { 00088 // TimeInterval timeout(_timeout); 00089 // if (wait_writable(timeout) != 0) 00090 // return -1; 00091 // } 00092 // 00093 // int n = lwip_send(_sock_fd, data, length, 0); 00094 // _is_connected = (n != 0); 00095 // 00096 // return n; 00097 00098 } 00099 00100 // -1 if unsuccessful, else number of bytes written 00101 int TCPSocketConnection::send_all(char* data, int length) 00102 { 00103 // if ((_sock_fd < 0) || !_is_connected) 00104 // return -1; 00105 // 00106 // int writtenLen = 0; 00107 // TimeInterval timeout(_timeout); 00108 // while (writtenLen < length) { 00109 // if (!_blocking) { 00110 // // Wait for socket to be writeable 00111 // if (wait_writable(timeout) != 0) 00112 // return writtenLen; 00113 // } 00114 // 00115 // int ret = lwip_send(_sock_fd, data + writtenLen, length - writtenLen, 0); 00116 // if (ret > 0) { 00117 // writtenLen += ret; 00118 // continue; 00119 // } else if (ret == 0) { 00120 // _is_connected = false; 00121 // return writtenLen; 00122 // } else { 00123 // return -1; //Connnection error 00124 // } 00125 // } 00126 // return writtenLen; 00127 return send(data,length); // just remap to send 00128 } 00129 00130 int TCPSocketConnection::receive(char* buffer, int length) 00131 { 00132 if (!_is_connected) { 00133 ERR("TCPSocketConnection::receive() - _is_connected is false : you cant receive data until you connect to a socket!"); 00134 return -1; 00135 } 00136 Timer tmr; 00137 int idx = 0; 00138 int nb_available = 0; 00139 int time = -1; 00140 00141 //make this the non-blocking case and return if <= 0 00142 // remember to change the config to blocking 00143 // if ( ! _blocking) { 00144 // if ( wifi.readable <= 0 ) { 00145 // return (wifi.readable); 00146 // } 00147 // } 00148 //--- 00149 tmr.start(); 00150 if (_blocking) { 00151 while (1) { 00152 nb_available = wifi->readable(); 00153 if (nb_available != 0) { 00154 break; 00155 } 00156 } 00157 } 00158 //--- 00159 // blocking case 00160 else { 00161 tmr.reset(); 00162 00163 while (time < _timeout) { 00164 nb_available = wifi->readable(); 00165 if (nb_available < 0) return nb_available; 00166 if (nb_available > 0) break ; 00167 time = tmr.read_ms(); 00168 } 00169 00170 if (nb_available == 0) return nb_available; 00171 } 00172 00173 // change this to < 20 mS timeout per byte to detect end of packet gap 00174 // this may not work due to buffering at the UART interface 00175 tmr.reset(); 00176 // while ( tmr.read_ms() < 20 ) { 00177 // if ( wifi.readable() && (idx < length) ) { 00178 // buffer[idx++] = wifi->getc(); 00179 // tmr.reset(); 00180 // } 00181 // if ( idx == length ) { 00182 // break; 00183 // } 00184 // } 00185 //--- 00186 while (time < _timeout) { 00187 00188 nb_available = wifi->readable(); 00189 //for (int i = 0; i < min(nb_available, length); i++) { 00190 for (int i = 0; i < min(nb_available, (length-idx)); i++) { 00191 buffer[idx] = wifi->getc(); 00192 idx++; 00193 } 00194 if (idx == length) { 00195 break; 00196 } 00197 time = tmr.read_ms(); 00198 } 00199 //--- 00200 return (idx == 0) ? -1 : idx; 00201 00202 //************************ original code below 00203 // 00204 // if (!_blocking) { 00205 // TimeInterval timeout(_timeout); 00206 // if (wait_readable(timeout) != 0) 00207 // return -1; 00208 // } 00209 // 00210 // int n = lwip_recv(_sock_fd, data, length, 0); 00211 // _is_connected = (n != 0); 00212 // 00213 // return n; 00214 00215 } 00216 00217 // -1 if unsuccessful, else number of bytes received 00218 int TCPSocketConnection::receive_all(char* data, int length) 00219 { 00220 //ERR("receive_all() not yet implimented"); 00221 // if ((_sock_fd < 0) || !_is_connected) 00222 // return -1; 00223 // 00224 // int readLen = 0; 00225 // TimeInterval timeout(_timeout); 00226 // while (readLen < length) { 00227 // if (!_blocking) { 00228 // //Wait for socket to be readable 00229 // if (wait_readable(timeout) != 0) 00230 // return readLen; 00231 // } 00232 // 00233 // int ret = lwip_recv(_sock_fd, data + readLen, length - readLen, 0); 00234 // if (ret > 0) { 00235 // readLen += ret; 00236 // } else if (ret == 0) { 00237 // _is_connected = false; 00238 // return readLen; 00239 // } else { 00240 // return -1; //Connnection error 00241 // } 00242 // } 00243 // return readLen; 00244 receive(data,length); 00245 }
Generated on Wed Jul 13 2022 05:04:33 by
1.7.2