Added monitoring feature of ESP8266's UART

Dependents:   ESP8266_W7500_Example DualNetworkInterface-Basic

Fork of ESP8266Interface by ESP8266

Committer:
sarahmarshy
Date:
Fri Jun 05 16:17:07 2015 +0000
Revision:
46:913d07795182
Parent:
45:c180905b5b79
Debug messages off

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
sarahmarshy 44:3a7b6083210b 26 #if 1
mbedAustin 42:3f62103a4f3c 27 #define DBG(x, ...) printf("[TCPConnection : DBG]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__);
mbedAustin 42:3f62103a4f3c 28 #define WARN(x, ...) printf("[TCPConnection: WARN]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__);
mbedAustin 42:3f62103a4f3c 29 #define ERR(x, ...) printf("[TCPConnection : ERR]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__);
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 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) {
sarahmarshy 44:3a7b6083210b 70 ERR("TCPSocketConnection::receive() - _is_connected is false : you cant receive data until 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 int idx = 0;
mbedAustin 32:cf071dc33972 75 tmr.start();
mbedAustin 32:cf071dc33972 76 while ((tmr.read_ms() < _timeout) || _blocking) {
mbedAustin 32:cf071dc33972 77
mbedAustin 32:cf071dc33972 78 idx += wifi->send(data, length);
mbedAustin 32:cf071dc33972 79
mbedAustin 32:cf071dc33972 80 if (idx == length)
mbedAustin 32:cf071dc33972 81 return idx;
mbedAustin 32:cf071dc33972 82 }
mbedAustin 32:cf071dc33972 83 return (idx == 0) ? -1 : idx;
mbedAustin 32:cf071dc33972 84
mbedAustin 32:cf071dc33972 85 //return wifi->send(data,length);
mbedAustin 32:cf071dc33972 86 //
mbedAustin 32:cf071dc33972 87 // if (!_blocking) {
mbedAustin 32:cf071dc33972 88 // TimeInterval timeout(_timeout);
mbedAustin 32:cf071dc33972 89 // if (wait_writable(timeout) != 0)
mbedAustin 32:cf071dc33972 90 // return -1;
mbedAustin 32:cf071dc33972 91 // }
mbedAustin 32:cf071dc33972 92 //
mbedAustin 32:cf071dc33972 93 // int n = lwip_send(_sock_fd, data, length, 0);
mbedAustin 32:cf071dc33972 94 // _is_connected = (n != 0);
mbedAustin 32:cf071dc33972 95 //
mbedAustin 32:cf071dc33972 96 // return n;
mbedAustin 32:cf071dc33972 97
mbedAustin 32:cf071dc33972 98 }
mbedAustin 32:cf071dc33972 99
mbedAustin 32:cf071dc33972 100 // -1 if unsuccessful, else number of bytes written
mbedAustin 32:cf071dc33972 101 int TCPSocketConnection::send_all(char* data, int length)
mbedAustin 32:cf071dc33972 102 {
mbedAustin 32:cf071dc33972 103 // if ((_sock_fd < 0) || !_is_connected)
mbedAustin 32:cf071dc33972 104 // return -1;
mbedAustin 32:cf071dc33972 105 //
mbedAustin 32:cf071dc33972 106 // int writtenLen = 0;
mbedAustin 32:cf071dc33972 107 // TimeInterval timeout(_timeout);
mbedAustin 32:cf071dc33972 108 // while (writtenLen < length) {
mbedAustin 32:cf071dc33972 109 // if (!_blocking) {
mbedAustin 32:cf071dc33972 110 // // Wait for socket to be writeable
mbedAustin 32:cf071dc33972 111 // if (wait_writable(timeout) != 0)
mbedAustin 32:cf071dc33972 112 // return writtenLen;
mbedAustin 32:cf071dc33972 113 // }
mbedAustin 32:cf071dc33972 114 //
mbedAustin 32:cf071dc33972 115 // int ret = lwip_send(_sock_fd, data + writtenLen, length - writtenLen, 0);
mbedAustin 32:cf071dc33972 116 // if (ret > 0) {
mbedAustin 32:cf071dc33972 117 // writtenLen += ret;
mbedAustin 32:cf071dc33972 118 // continue;
mbedAustin 32:cf071dc33972 119 // } else if (ret == 0) {
mbedAustin 32:cf071dc33972 120 // _is_connected = false;
mbedAustin 32:cf071dc33972 121 // return writtenLen;
mbedAustin 32:cf071dc33972 122 // } else {
mbedAustin 32:cf071dc33972 123 // return -1; //Connnection error
mbedAustin 32:cf071dc33972 124 // }
mbedAustin 32:cf071dc33972 125 // }
mbedAustin 32:cf071dc33972 126 // return writtenLen;
mbedAustin 32:cf071dc33972 127 return send(data,length); // just remap to send
mbedAustin 32:cf071dc33972 128 }
mbedAustin 32:cf071dc33972 129
mbedAustin 32:cf071dc33972 130 int TCPSocketConnection::receive(char* buffer, int length)
mbedAustin 32:cf071dc33972 131 {
mbedAustin 32:cf071dc33972 132 if (!_is_connected) {
sarahmarshy 44:3a7b6083210b 133 ERR("TCPSocketConnection::receive() - _is_connected is false : you cant receive data until you connect to a socket!");
mbedAustin 32:cf071dc33972 134 return -1;
mbedAustin 32:cf071dc33972 135 }
mbedAustin 32:cf071dc33972 136 Timer tmr;
mbedAustin 32:cf071dc33972 137 int idx = 0;
mbedAustin 32:cf071dc33972 138 int nb_available = 0;
mbedAustin 32:cf071dc33972 139 int time = -1;
mbedAustin 32:cf071dc33972 140
mbedAustin 32:cf071dc33972 141 //make this the non-blocking case and return if <= 0
mbedAustin 32:cf071dc33972 142 // remember to change the config to blocking
mbedAustin 32:cf071dc33972 143 // if ( ! _blocking) {
mbedAustin 32:cf071dc33972 144 // if ( wifi.readable <= 0 ) {
mbedAustin 32:cf071dc33972 145 // return (wifi.readable);
mbedAustin 32:cf071dc33972 146 // }
mbedAustin 32:cf071dc33972 147 // }
mbedAustin 32:cf071dc33972 148 //---
mbedAustin 32:cf071dc33972 149 tmr.start();
mbedAustin 32:cf071dc33972 150 if (_blocking) {
mbedAustin 32:cf071dc33972 151 while (1) {
mbedAustin 32:cf071dc33972 152 nb_available = wifi->readable();
mbedAustin 32:cf071dc33972 153 if (nb_available != 0) {
mbedAustin 32:cf071dc33972 154 break;
mbedAustin 32:cf071dc33972 155 }
mbedAustin 32:cf071dc33972 156 }
mbedAustin 32:cf071dc33972 157 }
mbedAustin 32:cf071dc33972 158 //---
mbedAustin 32:cf071dc33972 159 // blocking case
mbedAustin 32:cf071dc33972 160 else {
mbedAustin 32:cf071dc33972 161 tmr.reset();
mbedAustin 32:cf071dc33972 162
mbedAustin 32:cf071dc33972 163 while (time < _timeout) {
mbedAustin 32:cf071dc33972 164 nb_available = wifi->readable();
mbedAustin 32:cf071dc33972 165 if (nb_available < 0) return nb_available;
mbedAustin 32:cf071dc33972 166 if (nb_available > 0) break ;
mbedAustin 32:cf071dc33972 167 time = tmr.read_ms();
mbedAustin 32:cf071dc33972 168 }
mbedAustin 32:cf071dc33972 169
mbedAustin 32:cf071dc33972 170 if (nb_available == 0) return nb_available;
mbedAustin 32:cf071dc33972 171 }
mbedAustin 32:cf071dc33972 172
mbedAustin 32:cf071dc33972 173 // change this to < 20 mS timeout per byte to detect end of packet gap
mbedAustin 32:cf071dc33972 174 // this may not work due to buffering at the UART interface
mbedAustin 32:cf071dc33972 175 tmr.reset();
mbedAustin 32:cf071dc33972 176 // while ( tmr.read_ms() < 20 ) {
mbedAustin 32:cf071dc33972 177 // if ( wifi.readable() && (idx < length) ) {
mbedAustin 32:cf071dc33972 178 // buffer[idx++] = wifi->getc();
mbedAustin 32:cf071dc33972 179 // tmr.reset();
mbedAustin 32:cf071dc33972 180 // }
mbedAustin 32:cf071dc33972 181 // if ( idx == length ) {
mbedAustin 32:cf071dc33972 182 // break;
mbedAustin 32:cf071dc33972 183 // }
mbedAustin 32:cf071dc33972 184 // }
mbedAustin 32:cf071dc33972 185 //---
mbedAustin 32:cf071dc33972 186 while (time < _timeout) {
mbedAustin 32:cf071dc33972 187
mbedAustin 32:cf071dc33972 188 nb_available = wifi->readable();
mbedAustin 32:cf071dc33972 189 //for (int i = 0; i < min(nb_available, length); i++) {
mbedAustin 32:cf071dc33972 190 for (int i = 0; i < min(nb_available, (length-idx)); i++) {
mbedAustin 32:cf071dc33972 191 buffer[idx] = wifi->getc();
mbedAustin 32:cf071dc33972 192 idx++;
mbedAustin 32:cf071dc33972 193 }
mbedAustin 32:cf071dc33972 194 if (idx == length) {
mbedAustin 32:cf071dc33972 195 break;
mbedAustin 32:cf071dc33972 196 }
mbedAustin 32:cf071dc33972 197 time = tmr.read_ms();
mbedAustin 32:cf071dc33972 198 }
mbedAustin 32:cf071dc33972 199 //---
mbedAustin 32:cf071dc33972 200 return (idx == 0) ? -1 : idx;
mbedAustin 32:cf071dc33972 201
mbedAustin 32:cf071dc33972 202 //************************ original code below
mbedAustin 32:cf071dc33972 203 //
mbedAustin 32:cf071dc33972 204 // if (!_blocking) {
mbedAustin 32:cf071dc33972 205 // TimeInterval timeout(_timeout);
mbedAustin 32:cf071dc33972 206 // if (wait_readable(timeout) != 0)
mbedAustin 32:cf071dc33972 207 // return -1;
mbedAustin 32:cf071dc33972 208 // }
mbedAustin 32:cf071dc33972 209 //
mbedAustin 32:cf071dc33972 210 // int n = lwip_recv(_sock_fd, data, length, 0);
mbedAustin 32:cf071dc33972 211 // _is_connected = (n != 0);
mbedAustin 32:cf071dc33972 212 //
mbedAustin 32:cf071dc33972 213 // return n;
mbedAustin 32:cf071dc33972 214
mbedAustin 32:cf071dc33972 215 }
mbedAustin 32:cf071dc33972 216
mbedAustin 32:cf071dc33972 217 // -1 if unsuccessful, else number of bytes received
mbedAustin 32:cf071dc33972 218 int TCPSocketConnection::receive_all(char* data, int length)
mbedAustin 32:cf071dc33972 219 {
sarahmarshy 46:913d07795182 220 //ERR("receive_all() not yet implimented");
mbedAustin 32:cf071dc33972 221 // if ((_sock_fd < 0) || !_is_connected)
mbedAustin 32:cf071dc33972 222 // return -1;
mbedAustin 32:cf071dc33972 223 //
mbedAustin 32:cf071dc33972 224 // int readLen = 0;
mbedAustin 32:cf071dc33972 225 // TimeInterval timeout(_timeout);
mbedAustin 32:cf071dc33972 226 // while (readLen < length) {
mbedAustin 32:cf071dc33972 227 // if (!_blocking) {
mbedAustin 32:cf071dc33972 228 // //Wait for socket to be readable
mbedAustin 32:cf071dc33972 229 // if (wait_readable(timeout) != 0)
mbedAustin 32:cf071dc33972 230 // return readLen;
mbedAustin 32:cf071dc33972 231 // }
mbedAustin 32:cf071dc33972 232 //
mbedAustin 32:cf071dc33972 233 // int ret = lwip_recv(_sock_fd, data + readLen, length - readLen, 0);
mbedAustin 32:cf071dc33972 234 // if (ret > 0) {
mbedAustin 32:cf071dc33972 235 // readLen += ret;
mbedAustin 32:cf071dc33972 236 // } else if (ret == 0) {
mbedAustin 32:cf071dc33972 237 // _is_connected = false;
mbedAustin 32:cf071dc33972 238 // return readLen;
mbedAustin 32:cf071dc33972 239 // } else {
mbedAustin 32:cf071dc33972 240 // return -1; //Connnection error
mbedAustin 32:cf071dc33972 241 // }
mbedAustin 32:cf071dc33972 242 // }
mbedAustin 32:cf071dc33972 243 // return readLen;
sarahmarshy 44:3a7b6083210b 244 receive(data,length);
mbedAustin 32:cf071dc33972 245 }