A fork of the original interface for OS/2. Features a correctly-implemented recv (but retains the old behavior via recv2).
Dependents: weather_clock weather_clock
Socket/TCPSocketConnection.cpp@44:16da10e7b3f7, 2015-06-03 (annotated)
- Committer:
- geky
- Date:
- Wed Jun 03 21:44:20 2015 +0000
- Revision:
- 44:16da10e7b3f7
- Parent:
- 33:727aac1996b8
- Child:
- 48:6031f70e3914
Reimplemented ESP8266 driver using the Nodemcu firmware
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbedAustin | 32:cf071dc33972 | 1 | /* Copyright (C) 2012 mbed.org, MIT License |
mbedAustin | 32:cf071dc33972 | 2 | * |
mbedAustin | 32:cf071dc33972 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
mbedAustin | 32:cf071dc33972 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
mbedAustin | 32:cf071dc33972 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
mbedAustin | 32:cf071dc33972 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
mbedAustin | 32:cf071dc33972 | 7 | * furnished to do so, subject to the following conditions: |
mbedAustin | 32:cf071dc33972 | 8 | * |
mbedAustin | 32:cf071dc33972 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
mbedAustin | 32:cf071dc33972 | 10 | * substantial portions of the Software. |
mbedAustin | 32:cf071dc33972 | 11 | * |
mbedAustin | 32:cf071dc33972 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
mbedAustin | 32:cf071dc33972 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
mbedAustin | 32:cf071dc33972 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
mbedAustin | 32:cf071dc33972 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
mbedAustin | 32:cf071dc33972 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
mbedAustin | 32:cf071dc33972 | 17 | */ |
mbedAustin | 32:cf071dc33972 | 18 | #include "TCPSocketConnection.h" |
mbedAustin | 32:cf071dc33972 | 19 | #include <cstring> |
mbedAustin | 32:cf071dc33972 | 20 | #include <algorithm> |
mbedAustin | 32:cf071dc33972 | 21 | |
mbedAustin | 32:cf071dc33972 | 22 | using std::memset; |
mbedAustin | 32:cf071dc33972 | 23 | using std::memcpy; |
mbedAustin | 32:cf071dc33972 | 24 | |
mbedAustin | 32:cf071dc33972 | 25 | //Debug is disabled by default |
mbedAustin | 32:cf071dc33972 | 26 | #ifdef DEBUG |
mbedAustin | 32:cf071dc33972 | 27 | #define DBG(x, ...) printf("[TCPConnection : DBG]"x"\r\n", ##__VA_ARGS__); |
mbedAustin | 32:cf071dc33972 | 28 | #define WARN(x, ...) printf("[TCPConnection: WARN]"x"\r\n", ##__VA_ARGS__); |
mbedAustin | 32:cf071dc33972 | 29 | #define ERR(x, ...) printf("[TCPConnection : ERR]"x"\r\n", ##__VA_ARGS__); |
mbedAustin | 32:cf071dc33972 | 30 | #else |
mbedAustin | 32:cf071dc33972 | 31 | #define DBG(x, ...) |
mbedAustin | 32:cf071dc33972 | 32 | #define WARN(x, ...) |
mbedAustin | 32:cf071dc33972 | 33 | #define ERR(x, ...) |
mbedAustin | 32:cf071dc33972 | 34 | #endif |
mbedAustin | 32:cf071dc33972 | 35 | |
mbedAustin | 32:cf071dc33972 | 36 | TCPSocketConnection::TCPSocketConnection() : |
mbedAustin | 32:cf071dc33972 | 37 | _is_connected(false) |
mbedAustin | 32:cf071dc33972 | 38 | { |
mbedAustin | 32:cf071dc33972 | 39 | } |
mbedAustin | 32:cf071dc33972 | 40 | |
mbedAustin | 32:cf071dc33972 | 41 | int TCPSocketConnection::connect(const char* host, const int port) |
mbedAustin | 32:cf071dc33972 | 42 | { |
mbedAustin | 32:cf071dc33972 | 43 | // if (init_socket(SOCK_STREAM) < 0) |
mbedAustin | 32:cf071dc33972 | 44 | // return -1; |
mbedAustin | 32:cf071dc33972 | 45 | // |
mbedAustin | 32:cf071dc33972 | 46 | if (set_address(host, port) != 0) |
mbedAustin | 32:cf071dc33972 | 47 | return -1; |
mbedAustin | 32:cf071dc33972 | 48 | // |
mbedAustin | 32:cf071dc33972 | 49 | // if (lwip_connect(_sock_fd, (const struct sockaddr *) &_remoteHost, sizeof(_remoteHost)) < 0) { |
mbedAustin | 32:cf071dc33972 | 50 | // close(); |
mbedAustin | 32:cf071dc33972 | 51 | // return -1; |
mbedAustin | 32:cf071dc33972 | 52 | // } |
mbedAustin | 32:cf071dc33972 | 53 | // _is_connected = true; |
geky | 44:16da10e7b3f7 | 54 | _is_connected = ESP8266->open(ESP_TCP_TYPE,_ipAddress,_port); |
mbedAustin | 33:727aac1996b8 | 55 | if(_is_connected) { //success |
mbedAustin | 33:727aac1996b8 | 56 | return 0; |
mbedAustin | 33:727aac1996b8 | 57 | } else { // fail |
mbedAustin | 33:727aac1996b8 | 58 | return -1; |
mbedAustin | 33:727aac1996b8 | 59 | } |
mbedAustin | 32:cf071dc33972 | 60 | } |
mbedAustin | 32:cf071dc33972 | 61 | |
mbedAustin | 32:cf071dc33972 | 62 | bool TCPSocketConnection::is_connected(void) |
mbedAustin | 32:cf071dc33972 | 63 | { |
mbedAustin | 32:cf071dc33972 | 64 | return _is_connected; |
mbedAustin | 32:cf071dc33972 | 65 | } |
mbedAustin | 32:cf071dc33972 | 66 | |
mbedAustin | 32:cf071dc33972 | 67 | int TCPSocketConnection::send(char* data, int length) |
mbedAustin | 32:cf071dc33972 | 68 | { |
mbedAustin | 32:cf071dc33972 | 69 | if (!_is_connected) { |
mbedAustin | 32:cf071dc33972 | 70 | ERR("TCPSocketConnection::receive() - _is_connected is false : you cant receive data untill you connect to a socket!"); |
mbedAustin | 32:cf071dc33972 | 71 | return -1; |
mbedAustin | 32:cf071dc33972 | 72 | } |
mbedAustin | 32:cf071dc33972 | 73 | Timer tmr; |
mbedAustin | 32:cf071dc33972 | 74 | tmr.start(); |
mbedAustin | 32:cf071dc33972 | 75 | while ((tmr.read_ms() < _timeout) || _blocking) { |
mbedAustin | 32:cf071dc33972 | 76 | |
geky | 44:16da10e7b3f7 | 77 | if (wifi->send(data, length)) |
geky | 44:16da10e7b3f7 | 78 | return length; |
mbedAustin | 32:cf071dc33972 | 79 | } |
geky | 44:16da10e7b3f7 | 80 | |
geky | 44:16da10e7b3f7 | 81 | return -1; |
mbedAustin | 32:cf071dc33972 | 82 | |
mbedAustin | 32:cf071dc33972 | 83 | //return wifi->send(data,length); |
mbedAustin | 32:cf071dc33972 | 84 | // |
mbedAustin | 32:cf071dc33972 | 85 | // if (!_blocking) { |
mbedAustin | 32:cf071dc33972 | 86 | // TimeInterval timeout(_timeout); |
mbedAustin | 32:cf071dc33972 | 87 | // if (wait_writable(timeout) != 0) |
mbedAustin | 32:cf071dc33972 | 88 | // return -1; |
mbedAustin | 32:cf071dc33972 | 89 | // } |
mbedAustin | 32:cf071dc33972 | 90 | // |
mbedAustin | 32:cf071dc33972 | 91 | // int n = lwip_send(_sock_fd, data, length, 0); |
mbedAustin | 32:cf071dc33972 | 92 | // _is_connected = (n != 0); |
mbedAustin | 32:cf071dc33972 | 93 | // |
mbedAustin | 32:cf071dc33972 | 94 | // return n; |
mbedAustin | 32:cf071dc33972 | 95 | |
mbedAustin | 32:cf071dc33972 | 96 | } |
mbedAustin | 32:cf071dc33972 | 97 | |
mbedAustin | 32:cf071dc33972 | 98 | // -1 if unsuccessful, else number of bytes written |
mbedAustin | 32:cf071dc33972 | 99 | int TCPSocketConnection::send_all(char* data, int length) |
mbedAustin | 32:cf071dc33972 | 100 | { |
mbedAustin | 32:cf071dc33972 | 101 | // if ((_sock_fd < 0) || !_is_connected) |
mbedAustin | 32:cf071dc33972 | 102 | // return -1; |
mbedAustin | 32:cf071dc33972 | 103 | // |
mbedAustin | 32:cf071dc33972 | 104 | // int writtenLen = 0; |
mbedAustin | 32:cf071dc33972 | 105 | // TimeInterval timeout(_timeout); |
mbedAustin | 32:cf071dc33972 | 106 | // while (writtenLen < length) { |
mbedAustin | 32:cf071dc33972 | 107 | // if (!_blocking) { |
mbedAustin | 32:cf071dc33972 | 108 | // // Wait for socket to be writeable |
mbedAustin | 32:cf071dc33972 | 109 | // if (wait_writable(timeout) != 0) |
mbedAustin | 32:cf071dc33972 | 110 | // return writtenLen; |
mbedAustin | 32:cf071dc33972 | 111 | // } |
mbedAustin | 32:cf071dc33972 | 112 | // |
mbedAustin | 32:cf071dc33972 | 113 | // int ret = lwip_send(_sock_fd, data + writtenLen, length - writtenLen, 0); |
mbedAustin | 32:cf071dc33972 | 114 | // if (ret > 0) { |
mbedAustin | 32:cf071dc33972 | 115 | // writtenLen += ret; |
mbedAustin | 32:cf071dc33972 | 116 | // continue; |
mbedAustin | 32:cf071dc33972 | 117 | // } else if (ret == 0) { |
mbedAustin | 32:cf071dc33972 | 118 | // _is_connected = false; |
mbedAustin | 32:cf071dc33972 | 119 | // return writtenLen; |
mbedAustin | 32:cf071dc33972 | 120 | // } else { |
mbedAustin | 32:cf071dc33972 | 121 | // return -1; //Connnection error |
mbedAustin | 32:cf071dc33972 | 122 | // } |
mbedAustin | 32:cf071dc33972 | 123 | // } |
mbedAustin | 32:cf071dc33972 | 124 | // return writtenLen; |
mbedAustin | 32:cf071dc33972 | 125 | return send(data,length); // just remap to send |
mbedAustin | 32:cf071dc33972 | 126 | } |
mbedAustin | 32:cf071dc33972 | 127 | |
mbedAustin | 32:cf071dc33972 | 128 | int TCPSocketConnection::receive(char* buffer, int length) |
mbedAustin | 32:cf071dc33972 | 129 | { |
mbedAustin | 32:cf071dc33972 | 130 | if (!_is_connected) { |
mbedAustin | 32:cf071dc33972 | 131 | ERR("TCPSocketConnection::receive() - _is_connected is false : you cant receive data untill you connect to a socket!"); |
mbedAustin | 32:cf071dc33972 | 132 | return -1; |
mbedAustin | 32:cf071dc33972 | 133 | } |
mbedAustin | 32:cf071dc33972 | 134 | Timer tmr; |
mbedAustin | 32:cf071dc33972 | 135 | int idx = 0; |
mbedAustin | 32:cf071dc33972 | 136 | int nb_available = 0; |
mbedAustin | 32:cf071dc33972 | 137 | int time = -1; |
mbedAustin | 32:cf071dc33972 | 138 | |
mbedAustin | 32:cf071dc33972 | 139 | //make this the non-blocking case and return if <= 0 |
mbedAustin | 32:cf071dc33972 | 140 | // remember to change the config to blocking |
mbedAustin | 32:cf071dc33972 | 141 | // if ( ! _blocking) { |
mbedAustin | 32:cf071dc33972 | 142 | // if ( wifi.readable <= 0 ) { |
mbedAustin | 32:cf071dc33972 | 143 | // return (wifi.readable); |
mbedAustin | 32:cf071dc33972 | 144 | // } |
mbedAustin | 32:cf071dc33972 | 145 | // } |
mbedAustin | 32:cf071dc33972 | 146 | //--- |
mbedAustin | 32:cf071dc33972 | 147 | tmr.start(); |
mbedAustin | 32:cf071dc33972 | 148 | if (_blocking) { |
mbedAustin | 32:cf071dc33972 | 149 | while (1) { |
mbedAustin | 32:cf071dc33972 | 150 | nb_available = wifi->readable(); |
mbedAustin | 32:cf071dc33972 | 151 | if (nb_available != 0) { |
mbedAustin | 32:cf071dc33972 | 152 | break; |
mbedAustin | 32:cf071dc33972 | 153 | } |
mbedAustin | 32:cf071dc33972 | 154 | } |
mbedAustin | 32:cf071dc33972 | 155 | } |
mbedAustin | 32:cf071dc33972 | 156 | //--- |
mbedAustin | 32:cf071dc33972 | 157 | // blocking case |
mbedAustin | 32:cf071dc33972 | 158 | else { |
mbedAustin | 32:cf071dc33972 | 159 | tmr.reset(); |
mbedAustin | 32:cf071dc33972 | 160 | |
mbedAustin | 32:cf071dc33972 | 161 | while (time < _timeout) { |
mbedAustin | 32:cf071dc33972 | 162 | nb_available = wifi->readable(); |
mbedAustin | 32:cf071dc33972 | 163 | if (nb_available < 0) return nb_available; |
mbedAustin | 32:cf071dc33972 | 164 | if (nb_available > 0) break ; |
mbedAustin | 32:cf071dc33972 | 165 | time = tmr.read_ms(); |
mbedAustin | 32:cf071dc33972 | 166 | } |
mbedAustin | 32:cf071dc33972 | 167 | |
mbedAustin | 32:cf071dc33972 | 168 | if (nb_available == 0) return nb_available; |
mbedAustin | 32:cf071dc33972 | 169 | } |
mbedAustin | 32:cf071dc33972 | 170 | |
mbedAustin | 32:cf071dc33972 | 171 | // change this to < 20 mS timeout per byte to detect end of packet gap |
mbedAustin | 32:cf071dc33972 | 172 | // this may not work due to buffering at the UART interface |
mbedAustin | 32:cf071dc33972 | 173 | tmr.reset(); |
mbedAustin | 32:cf071dc33972 | 174 | // while ( tmr.read_ms() < 20 ) { |
mbedAustin | 32:cf071dc33972 | 175 | // if ( wifi.readable() && (idx < length) ) { |
mbedAustin | 32:cf071dc33972 | 176 | // buffer[idx++] = wifi->getc(); |
mbedAustin | 32:cf071dc33972 | 177 | // tmr.reset(); |
mbedAustin | 32:cf071dc33972 | 178 | // } |
mbedAustin | 32:cf071dc33972 | 179 | // if ( idx == length ) { |
mbedAustin | 32:cf071dc33972 | 180 | // break; |
mbedAustin | 32:cf071dc33972 | 181 | // } |
mbedAustin | 32:cf071dc33972 | 182 | // } |
mbedAustin | 32:cf071dc33972 | 183 | //--- |
mbedAustin | 32:cf071dc33972 | 184 | while (time < _timeout) { |
mbedAustin | 32:cf071dc33972 | 185 | |
mbedAustin | 32:cf071dc33972 | 186 | nb_available = wifi->readable(); |
mbedAustin | 32:cf071dc33972 | 187 | //for (int i = 0; i < min(nb_available, length); i++) { |
mbedAustin | 32:cf071dc33972 | 188 | for (int i = 0; i < min(nb_available, (length-idx)); i++) { |
mbedAustin | 32:cf071dc33972 | 189 | buffer[idx] = wifi->getc(); |
mbedAustin | 32:cf071dc33972 | 190 | idx++; |
mbedAustin | 32:cf071dc33972 | 191 | } |
mbedAustin | 32:cf071dc33972 | 192 | if (idx == length) { |
mbedAustin | 32:cf071dc33972 | 193 | break; |
mbedAustin | 32:cf071dc33972 | 194 | } |
mbedAustin | 32:cf071dc33972 | 195 | time = tmr.read_ms(); |
mbedAustin | 32:cf071dc33972 | 196 | } |
mbedAustin | 32:cf071dc33972 | 197 | //--- |
mbedAustin | 32:cf071dc33972 | 198 | return (idx == 0) ? -1 : idx; |
mbedAustin | 32:cf071dc33972 | 199 | |
mbedAustin | 32:cf071dc33972 | 200 | //************************ original code below |
mbedAustin | 32:cf071dc33972 | 201 | // |
mbedAustin | 32:cf071dc33972 | 202 | // if (!_blocking) { |
mbedAustin | 32:cf071dc33972 | 203 | // TimeInterval timeout(_timeout); |
mbedAustin | 32:cf071dc33972 | 204 | // if (wait_readable(timeout) != 0) |
mbedAustin | 32:cf071dc33972 | 205 | // return -1; |
mbedAustin | 32:cf071dc33972 | 206 | // } |
mbedAustin | 32:cf071dc33972 | 207 | // |
mbedAustin | 32:cf071dc33972 | 208 | // int n = lwip_recv(_sock_fd, data, length, 0); |
mbedAustin | 32:cf071dc33972 | 209 | // _is_connected = (n != 0); |
mbedAustin | 32:cf071dc33972 | 210 | // |
mbedAustin | 32:cf071dc33972 | 211 | // return n; |
mbedAustin | 32:cf071dc33972 | 212 | } |
mbedAustin | 32:cf071dc33972 | 213 | |
mbedAustin | 32:cf071dc33972 | 214 | // -1 if unsuccessful, else number of bytes received |
mbedAustin | 32:cf071dc33972 | 215 | int TCPSocketConnection::receive_all(char* data, int length) |
mbedAustin | 32:cf071dc33972 | 216 | { |
mbedAustin | 32:cf071dc33972 | 217 | // if ((_sock_fd < 0) || !_is_connected) |
mbedAustin | 32:cf071dc33972 | 218 | // return -1; |
mbedAustin | 32:cf071dc33972 | 219 | // |
mbedAustin | 32:cf071dc33972 | 220 | // int readLen = 0; |
mbedAustin | 32:cf071dc33972 | 221 | // TimeInterval timeout(_timeout); |
mbedAustin | 32:cf071dc33972 | 222 | // while (readLen < length) { |
mbedAustin | 32:cf071dc33972 | 223 | // if (!_blocking) { |
mbedAustin | 32:cf071dc33972 | 224 | // //Wait for socket to be readable |
mbedAustin | 32:cf071dc33972 | 225 | // if (wait_readable(timeout) != 0) |
mbedAustin | 32:cf071dc33972 | 226 | // return readLen; |
mbedAustin | 32:cf071dc33972 | 227 | // } |
mbedAustin | 32:cf071dc33972 | 228 | // |
mbedAustin | 32:cf071dc33972 | 229 | // int ret = lwip_recv(_sock_fd, data + readLen, length - readLen, 0); |
mbedAustin | 32:cf071dc33972 | 230 | // if (ret > 0) { |
mbedAustin | 32:cf071dc33972 | 231 | // readLen += ret; |
mbedAustin | 32:cf071dc33972 | 232 | // } else if (ret == 0) { |
mbedAustin | 32:cf071dc33972 | 233 | // _is_connected = false; |
mbedAustin | 32:cf071dc33972 | 234 | // return readLen; |
mbedAustin | 32:cf071dc33972 | 235 | // } else { |
mbedAustin | 32:cf071dc33972 | 236 | // return -1; //Connnection error |
mbedAustin | 32:cf071dc33972 | 237 | // } |
mbedAustin | 32:cf071dc33972 | 238 | // } |
mbedAustin | 32:cf071dc33972 | 239 | // return readLen; |
geky | 44:16da10e7b3f7 | 240 | if (!wifi->recv(data, &length)) |
geky | 44:16da10e7b3f7 | 241 | return -1; |
geky | 44:16da10e7b3f7 | 242 | |
geky | 44:16da10e7b3f7 | 243 | return length; |
mbedAustin | 32:cf071dc33972 | 244 | } |