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