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