Simple IoT Board用のライブラリです。 ESP8266ライブラリの軽量化 送信のみのソフトシリアルライブラリを含んでいます。

Dependents:   SITB_HttpGetSample SITB_IFTTTSample SITB_INA226PRC AmbientExampleSITB ... more

Committer:
jksoft
Date:
Sun Nov 15 13:36:44 2015 +0000
Revision:
0:890c12951e96
??

Who changed what in which revision?

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