データの保存、更新、取得ができるWebサービス「milkcocoa」に接続し、データのプッシュ、送信、取得ができるライブラリを使ったサンプルです。 https://mlkcca.com/

Dependencies:   Milkcocoa mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TCPSocketConnection.cpp Source File

TCPSocketConnection.cpp

00001 /* Copyright (C) 2012 mbed.org, MIT License
00002  *
00003  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00004  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00005  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00006  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00007  * furnished to do so, subject to the following conditions:
00008  *
00009  * The above copyright notice and this permission notice shall be included in all copies or
00010  * substantial portions of the Software.
00011  *
00012  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00013  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00014  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00015  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00016  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017  */
00018 #include "TCPSocketConnection.h"
00019 #include <cstring>
00020 #include <algorithm>
00021 #include "SoftSerialSendOnry.h"
00022 
00023 extern SoftSerialSendOnry pc;
00024 
00025 using std::memset;
00026 using std::memcpy;
00027 
00028 //Debug is disabled by default
00029 #if 0
00030 #define DBG(x, ...)  pc.printf("[TCPConnection : DBG]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__); 
00031 #define WARN(x, ...) pc.printf("[TCPConnection: WARN]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__); 
00032 #define ERR(x, ...)  pc.printf("[TCPConnection : ERR]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__); 
00033 #else
00034 #define DBG(x, ...)
00035 #define WARN(x, ...)
00036 #define ERR(x, ...)
00037 #endif
00038 
00039 TCPSocketConnection::TCPSocketConnection() :
00040     _is_connected(false)
00041 {
00042 }
00043 
00044 int TCPSocketConnection::connect(const char* host, const int port)
00045 {
00046 //    if (init_socket(SOCK_STREAM) < 0)
00047 //        return -1;
00048 //
00049     if (set_address(host, port) != 0)
00050         return -1;
00051 //
00052 //    if (lwip_connect(_sock_fd, (const struct sockaddr *) &_remoteHost, sizeof(_remoteHost)) < 0) {
00053 //        close();
00054 //        return -1;
00055 //    }
00056 //    _is_connected = true;
00057 
00058     _is_connected = ESP8266->start(ESP_TCP_TYPE,_ipAddress,_port);
00059     if(_is_connected) { //success
00060         return 0;
00061     } else { // fail
00062         return -1;
00063     }
00064 }
00065 
00066 bool TCPSocketConnection::is_connected(void)
00067 {
00068     return _is_connected;
00069 }
00070 
00071 int TCPSocketConnection::send(char* data, int length)
00072 {
00073     if (!_is_connected) {
00074         ERR("TCPSocketConnection::receive() - _is_connected is false : you cant receive data until you connect to a socket!");
00075         return -1;
00076     }
00077     Timer tmr;
00078     int idx = 0;
00079     tmr.start();
00080     while ((tmr.read_ms() < _timeout) || _blocking) {
00081 
00082         idx += wifi->send(data, length);
00083 
00084         if (idx == length)
00085             return idx;
00086     }
00087     return (idx == 0) ? -1 : idx;
00088 
00089     //return wifi->send(data,length);
00090 //
00091 //    if (!_blocking) {
00092 //        TimeInterval timeout(_timeout);
00093 //        if (wait_writable(timeout) != 0)
00094 //            return -1;
00095 //    }
00096 //
00097 //    int n = lwip_send(_sock_fd, data, length, 0);
00098 //    _is_connected = (n != 0);
00099 //
00100 //    return n;
00101 
00102 }
00103 
00104 // -1 if unsuccessful, else number of bytes written
00105 int TCPSocketConnection::send_all(char* data, int length)
00106 {
00107 //   if ((_sock_fd < 0) || !_is_connected)
00108 //        return -1;
00109 //
00110 //    int writtenLen = 0;
00111 //    TimeInterval timeout(_timeout);
00112 //    while (writtenLen < length) {
00113 //        if (!_blocking) {
00114 //            // Wait for socket to be writeable
00115 //            if (wait_writable(timeout) != 0)
00116 //                return writtenLen;
00117 //        }
00118 //
00119 //        int ret = lwip_send(_sock_fd, data + writtenLen, length - writtenLen, 0);
00120 //        if (ret > 0) {
00121 //            writtenLen += ret;
00122 //            continue;
00123 //        } else if (ret == 0) {
00124 //            _is_connected = false;
00125 //            return writtenLen;
00126 //        } else {
00127 //            return -1; //Connnection error
00128 //        }
00129 //    }
00130 //    return writtenLen;
00131     return send(data,length); // just remap to send
00132 }
00133 
00134 int TCPSocketConnection::receive(char* buffer, int length)
00135 {
00136     if (!_is_connected) {
00137         ERR("TCPSocketConnection::receive() - _is_connected is false : you cant receive data until you connect to a socket!");
00138         return -1;
00139     }
00140     Timer tmr;
00141     int idx = 0;
00142     int nb_available = 0;
00143     int time = -1;
00144 
00145     //make this the non-blocking case and return if <= 0
00146     // remember to change the config to blocking
00147     // if ( ! _blocking) {
00148     // if ( wifi.readable <= 0 ) {
00149     // return (wifi.readable);
00150     // }
00151     // }
00152     //---
00153     tmr.start();
00154     if (_blocking) {
00155         while (1) {
00156             nb_available = wifi->readable();
00157             if (nb_available != 0) {
00158                 break;
00159             }
00160         }
00161     }
00162     //---
00163     // blocking case
00164     else {
00165         tmr.reset();
00166 
00167         while (time < _timeout) {
00168             nb_available = wifi->readable();
00169             if (nb_available < 0) return nb_available;
00170             if (nb_available > 0) break ;
00171             time = tmr.read_ms();
00172         }
00173 
00174         if (nb_available == 0) return nb_available;
00175     }
00176 
00177     // change this to < 20 mS timeout per byte to detect end of packet gap
00178     // this may not work due to buffering at the UART interface
00179     tmr.reset();
00180     // while ( tmr.read_ms() < 20 ) {
00181     // if ( wifi.readable() && (idx < length) ) {
00182     // buffer[idx++] = wifi->getc();
00183     // tmr.reset();
00184     // }
00185     // if ( idx == length ) {
00186     // break;
00187     // }
00188     // }
00189     //---
00190     while (time < _timeout) {
00191 
00192         nb_available = wifi->readable();
00193         //for (int i = 0; i < min(nb_available, length); i++) {
00194         for (int i = 0; i < min(nb_available, (length-idx)); i++) {
00195             buffer[idx] = wifi->getc();
00196             idx++;
00197         }
00198         if (idx == length) {
00199             break;
00200         }
00201         time = tmr.read_ms();
00202     }
00203     //---
00204     return (idx == 0) ? -1 : idx;
00205 
00206 //************************ original code below
00207 //
00208 //    if (!_blocking) {
00209 //        TimeInterval timeout(_timeout);
00210 //        if (wait_readable(timeout) != 0)
00211 //            return -1;
00212 //    }
00213 //
00214 //    int n = lwip_recv(_sock_fd, data, length, 0);
00215 //    _is_connected = (n != 0);
00216 //
00217 //    return n;
00218 
00219 }
00220 
00221 // -1 if unsuccessful, else number of bytes received
00222 int TCPSocketConnection::receive_all(char* data, int length)
00223 {
00224     //ERR("receive_all() not yet implimented");
00225     //  if ((_sock_fd < 0) || !_is_connected)
00226 //        return -1;
00227 //
00228 //    int readLen = 0;
00229 //    TimeInterval timeout(_timeout);
00230 //    while (readLen < length) {
00231 //        if (!_blocking) {
00232 //            //Wait for socket to be readable
00233 //            if (wait_readable(timeout) != 0)
00234 //                return readLen;
00235 //        }
00236 //
00237 //        int ret = lwip_recv(_sock_fd, data + readLen, length - readLen, 0);
00238 //        if (ret > 0) {
00239 //            readLen += ret;
00240 //        } else if (ret == 0) {
00241 //            _is_connected = false;
00242 //            return readLen;
00243 //        } else {
00244 //            return -1; //Connnection error
00245 //        }
00246 //    }
00247 //    return readLen;
00248     receive(data,length);
00249 }