ワークショップ用のプログラム

Dependencies:   Milkcocoa mbed

Committer:
jksoft
Date:
Thu Sep 22 00:49:42 2016 +0000
Revision:
0:d0b3a5d1ba28
????????

Who changed what in which revision?

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