blabla
Fork of ESP8266Interface by
Revision 30:c035696b9397, committed 2015-04-29
- Comitter:
- mbedAustin
- Date:
- Wed Apr 29 22:43:03 2015 +0000
- Parent:
- 29:939372104145
- Child:
- 31:fd0eaf273b11
- Commit message:
- Safety Save
Changed in this revision
--- a/ESP8266/ESP8266.cpp Tue Apr 28 20:17:51 2015 +0000 +++ b/ESP8266/ESP8266.cpp Wed Apr 29 22:43:03 2015 +0000 @@ -39,7 +39,8 @@ #define INFO(x, ...) #endif -#define MAX_TRY_JOIN 3 +#define ESP_MAX_TRY_JOIN 3 +#define ESP_MAXID 4 // the largest possible ID Value (max num of sockets possible) extern Serial pc; @@ -97,6 +98,52 @@ return true; } +bool ESP8266::start(bool type,char* ip, int port, int id) +{ + // Error Check + if(id > ESP_MAXID) { + ERR("startUDPMulti: max id is: %d, id given is %d",ESP_MAXID,id); + return false; + } + // Single Connection Mode + if(id < 0) { + DBG("Start Single Connection Mode"); + char portstr[5]; + char idstr[2]; + bool check [3] = {0}; + sprintf(idstr,"%d",id); + sprintf(portstr, "%d", port); + switch(type) { + case ESP_UDP_TYPE : //UDP + check[0] = sendCommand(( "AT+CIPSTART=" + (string) idstr + ",\"UDP\",\"" + (string) ip + "\"," + (string) portstr ).c_str(), "OK", NULL, 10000); + break; + case ESP_TCP_TYPE : //TCP + check[0] = sendCommand(( "AT+CIPSTART=" + (string) idstr + ",\"TCP\",\"" + (string) ip + "\"," + (string) portstr ).c_str(), "OK", NULL, 10000); + break; + default: + ERR("Default hit for starting connection, this shouldnt be possible!!"); + break; + } + check[1] = sendCommand("AT+CIPMODE=1", "OK", NULL, 1000);// go into transparent mode + check[2] = sendCommand("AT+CIPSEND", ">", NULL, 1000);// go into transparent mode + // check that all commands were sucessful + if(check[0] && check[1] && check[2]) { + pc.printf("Data Mode\r\n"); + state.cmdMode = false; + return true; + } else { + DBG("\r\nstartUDPTransparent Failed for ip:%s, port:%d\r\n",ip,port); + return false; + } + } + // Multi Connection Mode + else { + //TODO: impliment Multi Connection Mode + ERR("Not currently Supported!"); + return false; + } +} + bool ESP8266::startUDP(char* ip, int port) { char portstr[5]; @@ -105,7 +152,7 @@ sendCommand("AT+CIPMODE=1", "OK", NULL, 1000);// go into transparent mode sendCommand("AT+CIPSEND", ">", NULL, 1000);// go into transparent mode - printf("Data Mode\r\n"); + pc.printf("Data Mode\r\n"); state.cmdMode = false; return true; @@ -184,6 +231,7 @@ return true; } else { // dns needed, not currently available + ERR("gethostbyname(): DNS Not currently available, only use IP Addresses!"); return false; } }
--- a/ESP8266/ESP8266.h Tue Apr 28 20:17:51 2015 +0000 +++ b/ESP8266/ESP8266.h Wed Apr 29 22:43:03 2015 +0000 @@ -31,6 +31,8 @@ #include "CBuffer.h" #define DEFAULT_WAIT_RESP_TIMEOUT 500 +#define ESP_TCP_TYPE 1 +#define ESP_UDP_TYPE 0 /** * The ESP8266 class @@ -78,9 +80,21 @@ bool disconnect(); /* - * Start up a UDP Connection + * Start up a UDP or TCP Connection + * @param type 0 for UDP, 1 for TCP + * @param ip A string that contains the IP, no quotes + * @param port Numerical port number to connect to + * @param id number between 0-4, if defined it denotes ID to use in multimode (Default to Single connection mode with -1) + * @return true if sucessful, 0 if fail */ - bool startUDP(char* ip, int port); + bool start(bool type, char* ip, int port, int id = -1); + + /* + * Legacy Start for UDP only connection in transparent mode + * @param ip A string that contains the IP, no quotes + * @param port Numerical port number to connect to + */ + bool ESP8266::startUDP(char* ip, int port); /** * Close a connection
--- a/Socket/Endpoint.cpp Tue Apr 28 20:17:51 2015 +0000 +++ b/Socket/Endpoint.cpp Wed Apr 29 22:43:03 2015 +0000 @@ -34,6 +34,7 @@ void Endpoint::reset_address(void) { _ipAddress[0] = '\0'; _port = 0; + _id = -1; } int Endpoint::set_address(const char* host, const int port) { @@ -41,7 +42,7 @@ ESP8266->gethostbyname(host, _ipAddress); _port = port; //Start the UDP Endpoint - ESP8266->startUDP(_ipAddress,_port); + ESP8266->start(ESP_UDP_TYPE,_ipAddress,_port); return 0; } @@ -52,3 +53,7 @@ int Endpoint::get_port() { return _port; } + +int Endpoint::get_id(){ + return _id; +}
--- a/Socket/Endpoint.h Tue Apr 28 20:17:51 2015 +0000 +++ b/Socket/Endpoint.h Wed Apr 29 22:43:03 2015 +0000 @@ -55,10 +55,16 @@ \return The port of this endpoint */ int get_port(void); + + /** Get the id of this endpoint + \return The id of this endpoint + */ + int get_id(void); protected: char _ipAddress[16]; int _port; + int _id = -1; ESP8266 * ESP8266; };
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Socket/TCPSocketConnection.h Wed Apr 29 22:43:03 2015 +0000 @@ -0,0 +1,82 @@ +/* Copyright (C) 2012 mbed.org, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef TCPSOCKET_H +#define TCPSOCKET_H + +#include "Socket/Socket.h" +#include "Socket/Endpoint.h" + +/** +TCP socket connection +*/ +class TCPSocketConnection : public Socket, public Endpoint { + friend class TCPSocketServer; + +public: + /** TCP socket connection + */ + TCPSocketConnection(); + + /** Connects this TCP socket to the server + \param host The host to connect to. It can either be an IP Address or a hostname that will be resolved with DNS. + \param port The host's port to connect to. + \return 0 on success, -1 on failure. + */ + int connect(const char* host, const int port); + + /** Check if the socket is connected + \return true if connected, false otherwise. + */ + bool is_connected(void); + + /** Send data to the remote host. + \param data The buffer to send to the host. + \param length The length of the buffer to send. + \return the number of written bytes on success (>=0) or -1 on failure + */ + int send(char* data, int length); + + /** Send all the data to the remote host. + \param data The buffer to send to the host. + \param length The length of the buffer to send. + \return the number of written bytes on success (>=0) or -1 on failure + */ + int send_all(char* data, int length); + + /** Receive data from the remote host. + \param data The buffer in which to store the data received from the host. + \param length The maximum length of the buffer. + \return the number of received bytes on success (>=0) or -1 on failure + */ + int receive(char* data, int length); + + /** Receive all the data from the remote host. + \param data The buffer in which to store the data received from the host. + \param length The maximum length of the buffer. + \return the number of received bytes on success (>=0) or -1 on failure + */ + int receive_all(char* data, int length); + +private: + bool _is_connected; + +}; + +#endif + \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Socket/TCSocketConnection.cpp Wed Apr 29 22:43:03 2015 +0000 @@ -0,0 +1,145 @@ +/* Copyright (C) 2012 mbed.org, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +#include "TCPSocketConnection.h" +#include <cstring> + +using std::memset; +using std::memcpy; + +TCPSocketConnection::TCPSocketConnection() : + _is_connected(false) +{ +} + +int TCPSocketConnection::connect(const char* host, const int port) +{ +// if (init_socket(SOCK_STREAM) < 0) +// return -1; +// +// if (set_address(host, port) != 0) +// return -1; +// +// if (lwip_connect(_sock_fd, (const struct sockaddr *) &_remoteHost, sizeof(_remoteHost)) < 0) { +// close(); +// return -1; +// } +// _is_connected = true; + _is_connected(); + + return 0; +} + +bool TCPSocketConnection::is_connected(void) +{ + return _is_connected; +} + +int TCPSocketConnection::send(char* data, int length) +{ +// if ((_sock_fd < 0) || !_is_connected) +// return -1; +// +// if (!_blocking) { +// TimeInterval timeout(_timeout); +// if (wait_writable(timeout) != 0) +// return -1; +// } +// +// int n = lwip_send(_sock_fd, data, length, 0); +// _is_connected = (n != 0); +// +// return n; + return 0; +} + +// -1 if unsuccessful, else number of bytes written +int TCPSocketConnection::send_all(char* data, int length) +{ +// if ((_sock_fd < 0) || !_is_connected) +// return -1; +// +// int writtenLen = 0; +// TimeInterval timeout(_timeout); +// while (writtenLen < length) { +// if (!_blocking) { +// // Wait for socket to be writeable +// if (wait_writable(timeout) != 0) +// return writtenLen; +// } +// +// int ret = lwip_send(_sock_fd, data + writtenLen, length - writtenLen, 0); +// if (ret > 0) { +// writtenLen += ret; +// continue; +// } else if (ret == 0) { +// _is_connected = false; +// return writtenLen; +// } else { +// return -1; //Connnection error +// } +// } +// return writtenLen; + return 0; +} + +int TCPSocketConnection::receive(char* data, int length) +{ +// if ((_sock_fd < 0) || !_is_connected) +// return -1; +// +// if (!_blocking) { +// TimeInterval timeout(_timeout); +// if (wait_readable(timeout) != 0) +// return -1; +// } +// +// int n = lwip_recv(_sock_fd, data, length, 0); +// _is_connected = (n != 0); +// +// return n; + return 0; +} + +// -1 if unsuccessful, else number of bytes received +int TCPSocketConnection::receive_all(char* data, int length) +{ + // if ((_sock_fd < 0) || !_is_connected) +// return -1; +// +// int readLen = 0; +// TimeInterval timeout(_timeout); +// while (readLen < length) { +// if (!_blocking) { +// //Wait for socket to be readable +// if (wait_readable(timeout) != 0) +// return readLen; +// } +// +// int ret = lwip_recv(_sock_fd, data + readLen, length - readLen, 0); +// if (ret > 0) { +// readLen += ret; +// } else if (ret == 0) { +// _is_connected = false; +// return readLen; +// } else { +// return -1; //Connnection error +// } +// } +// return readLen; + return 0; +}
--- a/Socket/UDPSocket.cpp Tue Apr 28 20:17:51 2015 +0000 +++ b/Socket/UDPSocket.cpp Wed Apr 29 22:43:03 2015 +0000 @@ -53,7 +53,7 @@ while ((tmr.read_ms() < _timeout) || _blocking) { - idx += wifi->send(packet, length); + idx += wifi->sendUDP(packet, length); if (idx == length) return idx;