ワークショップ用のサンプルプログラムです。

Dependencies:   DHT mbed

Committer:
jksoft
Date:
Fri Apr 29 21:07:24 2016 +0000
Revision:
0:a967c8649563
??

Who changed what in which revision?

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