GSwifiInterface library (interface for GainSpan Wi-Fi GS1011 modules) Please see https://mbed.org/users/gsfan/notebook/GSwifiInterface/
Dependents: GSwifiInterface_HelloWorld GSwifiInterface_HelloServo GSwifiInterface_UDPEchoServer GSwifiInterface_UDPEchoClient ... more
Fork of WiflyInterface by
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 /* Copyright (C) 2013 gsfan, MIT License 00019 * port to the GainSpan Wi-FI module GS1011 00020 */ 00021 00022 #include "TCPSocketConnection.h" 00023 #include <algorithm> 00024 00025 TCPSocketConnection::TCPSocketConnection() {} 00026 00027 int TCPSocketConnection::connect(const char* host, const int port) 00028 { 00029 if (set_address(host, port) != 0) return -1; 00030 00031 _server = false; 00032 _cid = _wifi->open(GSwifi::PROTO_TCP, get_address(), get_port()); 00033 if (_cid < 0) return -1; 00034 00035 return 0; 00036 } 00037 00038 bool TCPSocketConnection::is_connected(void) 00039 { 00040 bool _is_connected = _wifi->isConnected(_cid); 00041 if (!_is_connected) _cid = -1; 00042 return _is_connected; 00043 } 00044 00045 int TCPSocketConnection::send(char* data, int length) 00046 { 00047 if (_cid < 0 || !is_connected()) return -1; 00048 00049 // TCP Client/Server 00050 return _wifi->send(_cid, data, length); 00051 } 00052 00053 // -1 if unsuccessful, else number of bytes written 00054 int TCPSocketConnection::send_all(char* data, int length) 00055 { 00056 Timer tmr; 00057 int idx = 0; 00058 00059 if (_cid < 0 || !is_connected()) return -1; 00060 00061 tmr.start(); 00062 // TCP Client/Server 00063 while ((tmr.read_ms() < _timeout) || _blocking) { 00064 00065 idx += _wifi->send(_cid, &data[idx], length - idx); 00066 if (idx < 0) return -1; 00067 00068 if (idx == length) 00069 return idx; 00070 } 00071 return (idx == 0) ? -1 : idx; 00072 } 00073 00074 // -1 if unsuccessful, else number of bytes received 00075 int TCPSocketConnection::receive(char* data, int length) 00076 { 00077 Timer tmr; 00078 int time = -1; 00079 00080 if (_cid < 0 || !is_connected()) return -1; 00081 00082 if (_blocking) { 00083 tmr.start(); 00084 while (time < _timeout + 20) { 00085 if (_wifi->readable(_cid)) { 00086 DBG("receive readable"); 00087 break; 00088 } 00089 Thread::wait(1); 00090 time = tmr.read_ms(); 00091 } 00092 if (time >= _timeout + 20) { 00093 DBG("receive timeout"); 00094 return 0; 00095 } 00096 } 00097 00098 int nb_available = _wifi->recv(_cid, data, length); 00099 00100 return nb_available; 00101 } 00102 00103 00104 // -1 if unsuccessful, else number of bytes received 00105 int TCPSocketConnection::receive_all(char* data, int length) 00106 { 00107 Timer tmr; 00108 int idx = 0; 00109 int time = -1; 00110 00111 if (_cid < 0 || !is_connected()) return -1; 00112 00113 tmr.start(); 00114 00115 while (time < _timeout || _blocking) { 00116 00117 idx += _wifi->recv(_cid, &data[idx], length - idx); 00118 if (idx < 0) return -1; 00119 00120 if (idx == length) 00121 break; 00122 00123 Thread::wait(1); 00124 time = tmr.read_ms(); 00125 } 00126 00127 return (idx == 0) ? -1 : idx; 00128 } 00129 00130 void TCPSocketConnection::acceptCID (int cid) { 00131 char *ip; 00132 int port; 00133 _server = true; 00134 _cid = cid; 00135 if (!_wifi->getRemote(_cid, &ip, &port)) { 00136 set_address(ip, port); 00137 } 00138 }
Generated on Thu Jul 14 2022 07:53:37 by 1.7.2