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