Added monitoring feature of ESP8266's UART

Dependents:   ESP8266_W7500_Example DualNetworkInterface-Basic

Fork of ESP8266Interface by ESP8266

Committer:
mbedAustin
Date:
Thu Apr 30 19:07:37 2015 +0000
Revision:
32:cf071dc33972
Child:
33:727aac1996b8
TCP Send / Recieve Working

Who changed what in which revision?

UserRevisionLine numberNew 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;
mbedAustin 32:cf071dc33972 54 _is_connected = ESP8266->start(ESP_TCP_TYPE,_ipAddress,_port);
mbedAustin 32:cf071dc33972 55
mbedAustin 32:cf071dc33972 56 return _is_connected;
mbedAustin 32:cf071dc33972 57 }
mbedAustin 32:cf071dc33972 58
mbedAustin 32:cf071dc33972 59 bool TCPSocketConnection::is_connected(void)
mbedAustin 32:cf071dc33972 60 {
mbedAustin 32:cf071dc33972 61 return _is_connected;
mbedAustin 32:cf071dc33972 62 }
mbedAustin 32:cf071dc33972 63
mbedAustin 32:cf071dc33972 64 int TCPSocketConnection::send(char* data, int length)
mbedAustin 32:cf071dc33972 65 {
mbedAustin 32:cf071dc33972 66 if (!_is_connected) {
mbedAustin 32:cf071dc33972 67 ERR("TCPSocketConnection::receive() - _is_connected is false : you cant receive data untill you connect to a socket!");
mbedAustin 32:cf071dc33972 68 return -1;
mbedAustin 32:cf071dc33972 69 }
mbedAustin 32:cf071dc33972 70 Timer tmr;
mbedAustin 32:cf071dc33972 71 int idx = 0;
mbedAustin 32:cf071dc33972 72 tmr.start();
mbedAustin 32:cf071dc33972 73 while ((tmr.read_ms() < _timeout) || _blocking) {
mbedAustin 32:cf071dc33972 74
mbedAustin 32:cf071dc33972 75 idx += wifi->send(data, length);
mbedAustin 32:cf071dc33972 76
mbedAustin 32:cf071dc33972 77 if (idx == length)
mbedAustin 32:cf071dc33972 78 return idx;
mbedAustin 32:cf071dc33972 79 }
mbedAustin 32:cf071dc33972 80 return (idx == 0) ? -1 : idx;
mbedAustin 32:cf071dc33972 81
mbedAustin 32:cf071dc33972 82 //return wifi->send(data,length);
mbedAustin 32:cf071dc33972 83 //
mbedAustin 32:cf071dc33972 84 // if (!_blocking) {
mbedAustin 32:cf071dc33972 85 // TimeInterval timeout(_timeout);
mbedAustin 32:cf071dc33972 86 // if (wait_writable(timeout) != 0)
mbedAustin 32:cf071dc33972 87 // return -1;
mbedAustin 32:cf071dc33972 88 // }
mbedAustin 32:cf071dc33972 89 //
mbedAustin 32:cf071dc33972 90 // int n = lwip_send(_sock_fd, data, length, 0);
mbedAustin 32:cf071dc33972 91 // _is_connected = (n != 0);
mbedAustin 32:cf071dc33972 92 //
mbedAustin 32:cf071dc33972 93 // return n;
mbedAustin 32:cf071dc33972 94
mbedAustin 32:cf071dc33972 95 }
mbedAustin 32:cf071dc33972 96
mbedAustin 32:cf071dc33972 97 // -1 if unsuccessful, else number of bytes written
mbedAustin 32:cf071dc33972 98 int TCPSocketConnection::send_all(char* data, int length)
mbedAustin 32:cf071dc33972 99 {
mbedAustin 32:cf071dc33972 100 // if ((_sock_fd < 0) || !_is_connected)
mbedAustin 32:cf071dc33972 101 // return -1;
mbedAustin 32:cf071dc33972 102 //
mbedAustin 32:cf071dc33972 103 // int writtenLen = 0;
mbedAustin 32:cf071dc33972 104 // TimeInterval timeout(_timeout);
mbedAustin 32:cf071dc33972 105 // while (writtenLen < length) {
mbedAustin 32:cf071dc33972 106 // if (!_blocking) {
mbedAustin 32:cf071dc33972 107 // // Wait for socket to be writeable
mbedAustin 32:cf071dc33972 108 // if (wait_writable(timeout) != 0)
mbedAustin 32:cf071dc33972 109 // return writtenLen;
mbedAustin 32:cf071dc33972 110 // }
mbedAustin 32:cf071dc33972 111 //
mbedAustin 32:cf071dc33972 112 // int ret = lwip_send(_sock_fd, data + writtenLen, length - writtenLen, 0);
mbedAustin 32:cf071dc33972 113 // if (ret > 0) {
mbedAustin 32:cf071dc33972 114 // writtenLen += ret;
mbedAustin 32:cf071dc33972 115 // continue;
mbedAustin 32:cf071dc33972 116 // } else if (ret == 0) {
mbedAustin 32:cf071dc33972 117 // _is_connected = false;
mbedAustin 32:cf071dc33972 118 // return writtenLen;
mbedAustin 32:cf071dc33972 119 // } else {
mbedAustin 32:cf071dc33972 120 // return -1; //Connnection error
mbedAustin 32:cf071dc33972 121 // }
mbedAustin 32:cf071dc33972 122 // }
mbedAustin 32:cf071dc33972 123 // return writtenLen;
mbedAustin 32:cf071dc33972 124 return send(data,length); // just remap to send
mbedAustin 32:cf071dc33972 125 }
mbedAustin 32:cf071dc33972 126
mbedAustin 32:cf071dc33972 127 int TCPSocketConnection::receive(char* buffer, int length)
mbedAustin 32:cf071dc33972 128 {
mbedAustin 32:cf071dc33972 129 if (!_is_connected) {
mbedAustin 32:cf071dc33972 130 ERR("TCPSocketConnection::receive() - _is_connected is false : you cant receive data untill you connect to a socket!");
mbedAustin 32:cf071dc33972 131 return -1;
mbedAustin 32:cf071dc33972 132 }
mbedAustin 32:cf071dc33972 133 Timer tmr;
mbedAustin 32:cf071dc33972 134 int idx = 0;
mbedAustin 32:cf071dc33972 135 int nb_available = 0;
mbedAustin 32:cf071dc33972 136 int time = -1;
mbedAustin 32:cf071dc33972 137
mbedAustin 32:cf071dc33972 138 //make this the non-blocking case and return if <= 0
mbedAustin 32:cf071dc33972 139 // remember to change the config to blocking
mbedAustin 32:cf071dc33972 140 // if ( ! _blocking) {
mbedAustin 32:cf071dc33972 141 // if ( wifi.readable <= 0 ) {
mbedAustin 32:cf071dc33972 142 // return (wifi.readable);
mbedAustin 32:cf071dc33972 143 // }
mbedAustin 32:cf071dc33972 144 // }
mbedAustin 32:cf071dc33972 145 //---
mbedAustin 32:cf071dc33972 146 tmr.start();
mbedAustin 32:cf071dc33972 147 if (_blocking) {
mbedAustin 32:cf071dc33972 148 while (1) {
mbedAustin 32:cf071dc33972 149 nb_available = wifi->readable();
mbedAustin 32:cf071dc33972 150 if (nb_available != 0) {
mbedAustin 32:cf071dc33972 151 break;
mbedAustin 32:cf071dc33972 152 }
mbedAustin 32:cf071dc33972 153 }
mbedAustin 32:cf071dc33972 154 }
mbedAustin 32:cf071dc33972 155 //---
mbedAustin 32:cf071dc33972 156 // blocking case
mbedAustin 32:cf071dc33972 157 else {
mbedAustin 32:cf071dc33972 158 tmr.reset();
mbedAustin 32:cf071dc33972 159
mbedAustin 32:cf071dc33972 160 while (time < _timeout) {
mbedAustin 32:cf071dc33972 161 nb_available = wifi->readable();
mbedAustin 32:cf071dc33972 162 if (nb_available < 0) return nb_available;
mbedAustin 32:cf071dc33972 163 if (nb_available > 0) break ;
mbedAustin 32:cf071dc33972 164 time = tmr.read_ms();
mbedAustin 32:cf071dc33972 165 }
mbedAustin 32:cf071dc33972 166
mbedAustin 32:cf071dc33972 167 if (nb_available == 0) return nb_available;
mbedAustin 32:cf071dc33972 168 }
mbedAustin 32:cf071dc33972 169
mbedAustin 32:cf071dc33972 170 // change this to < 20 mS timeout per byte to detect end of packet gap
mbedAustin 32:cf071dc33972 171 // this may not work due to buffering at the UART interface
mbedAustin 32:cf071dc33972 172 tmr.reset();
mbedAustin 32:cf071dc33972 173 // while ( tmr.read_ms() < 20 ) {
mbedAustin 32:cf071dc33972 174 // if ( wifi.readable() && (idx < length) ) {
mbedAustin 32:cf071dc33972 175 // buffer[idx++] = wifi->getc();
mbedAustin 32:cf071dc33972 176 // tmr.reset();
mbedAustin 32:cf071dc33972 177 // }
mbedAustin 32:cf071dc33972 178 // if ( idx == length ) {
mbedAustin 32:cf071dc33972 179 // break;
mbedAustin 32:cf071dc33972 180 // }
mbedAustin 32:cf071dc33972 181 // }
mbedAustin 32:cf071dc33972 182 //---
mbedAustin 32:cf071dc33972 183 while (time < _timeout) {
mbedAustin 32:cf071dc33972 184
mbedAustin 32:cf071dc33972 185 nb_available = wifi->readable();
mbedAustin 32:cf071dc33972 186 //for (int i = 0; i < min(nb_available, length); i++) {
mbedAustin 32:cf071dc33972 187 for (int i = 0; i < min(nb_available, (length-idx)); i++) {
mbedAustin 32:cf071dc33972 188 buffer[idx] = wifi->getc();
mbedAustin 32:cf071dc33972 189 idx++;
mbedAustin 32:cf071dc33972 190 }
mbedAustin 32:cf071dc33972 191 if (idx == length) {
mbedAustin 32:cf071dc33972 192 break;
mbedAustin 32:cf071dc33972 193 }
mbedAustin 32:cf071dc33972 194 time = tmr.read_ms();
mbedAustin 32:cf071dc33972 195 }
mbedAustin 32:cf071dc33972 196 //---
mbedAustin 32:cf071dc33972 197 return (idx == 0) ? -1 : idx;
mbedAustin 32:cf071dc33972 198
mbedAustin 32:cf071dc33972 199 //************************ original code below
mbedAustin 32:cf071dc33972 200 //
mbedAustin 32:cf071dc33972 201 // if (!_blocking) {
mbedAustin 32:cf071dc33972 202 // TimeInterval timeout(_timeout);
mbedAustin 32:cf071dc33972 203 // if (wait_readable(timeout) != 0)
mbedAustin 32:cf071dc33972 204 // return -1;
mbedAustin 32:cf071dc33972 205 // }
mbedAustin 32:cf071dc33972 206 //
mbedAustin 32:cf071dc33972 207 // int n = lwip_recv(_sock_fd, data, length, 0);
mbedAustin 32:cf071dc33972 208 // _is_connected = (n != 0);
mbedAustin 32:cf071dc33972 209 //
mbedAustin 32:cf071dc33972 210 // return n;
mbedAustin 32:cf071dc33972 211
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;
mbedAustin 32:cf071dc33972 240 return 0;
mbedAustin 32:cf071dc33972 241 }