Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of ESP8266Interface by
Revision 49:965a410f538d, committed 2017-02-06
- Comitter:
- blownelco
- Date:
- Mon Feb 06 08:22:23 2017 +0000
- Parent:
- 47:1f4dd0e91837
- Commit message:
- voor arne
Changed in this revision
--- a/Socket/Endpoint.cpp Mon Jun 08 21:32:12 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,64 +0,0 @@ -/* 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 "Socket/Socket.h" -#include "Socket/Endpoint.h" -#include <cstring> - -using std::memset; - -Endpoint::Endpoint() -{ - ESP8266 = ESP8266::getInstance(); - if (ESP8266 == NULL) - error("Endpoint constructor error: no ESP8266 instance available!\r\n"); - reset_address(); -} -Endpoint::~Endpoint() {} - -void Endpoint::reset_address(void) -{ - _ipAddress[0] = '\0'; - _port = 0; - _id = -1; -} - -int Endpoint::set_address(const char* host, const int port) -{ - //Resolve DNS address or populate hard-coded IP address - if(ESP8266->gethostbyname(host, _ipAddress)) { - _port = port; - return 0; - } else { - return -1; - } -} - -char* Endpoint::get_address() -{ - return _ipAddress; -} - -int Endpoint::get_port() -{ - return _port; -} - -int Endpoint::get_id() -{ - return _id; -}
--- a/Socket/Endpoint.h Mon Jun 08 21:32:12 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,72 +0,0 @@ -/* 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 ENDPOINT_H -#define ENDPOINT_H - -#include "ESP8266.h" - -class UDPSocket; - -/** -IP Endpoint (address, port) -*/ -class Endpoint { - friend class UDPSocket; - -public: - /** IP Endpoint (address, port) - */ - Endpoint(void); - - ~Endpoint(void); - - /** Reset the address of this endpoint - */ - void reset_address(void); - - /** Set the address of this endpoint - \param host The endpoint address (it can either be an IP Address or a hostname that will be resolved with DNS). - \param port The endpoint port - \return 0 on success, -1 on failure (when an hostname cannot be resolved by DNS). - */ - int set_address(const char* host, const int port); - - /** Get the IP address of this endpoint - \return The IP address of this endpoint. - */ - char* get_address(void); - - /** Get the port of this endpoint - \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; - - ESP8266 * ESP8266; -}; - -#endif
--- a/Socket/Socket.cpp Mon Jun 08 21:32:12 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,57 +0,0 @@ -/* 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 "Socket.h" -#include <cstring> - -//Debug is disabled by default -#if 0 -//Enable debug -#include <cstdio> -#define DBG(x, ...) std::printf("[Socket : DBG]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__); -#define WARN(x, ...) std::printf("[Socket : WARN]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__); -#define ERR(x, ...) std::printf("[Socket : ERR]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__); - -#else -//Disable debug -#define DBG(x, ...) -#define WARN(x, ...) -#define ERR(x, ...) - -#endif - -Socket::Socket() : _blocking(true), _timeout(1500) { - wifi = ESP8266::getInstance(); - if (wifi == NULL) - ERR("Socket constructor error: no ESP8266 instance available!"); -} - -void Socket::set_blocking(bool blocking, unsigned int timeout) { - DBG("set blocking: %d %d", blocking, timeout); - _blocking = blocking; - _timeout = timeout; -} - -int Socket::close() { - - return (wifi->close()) ? 0 : -1; -} - -Socket::~Socket() { - close(); //Don't want to leak -}
--- a/Socket/Socket.h Mon Jun 08 21:32:12 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,51 +0,0 @@ -/* 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 SOCKET_H_ -#define SOCKET_H_ - -#include "ESP8266.h" - -/** Socket file descriptor and select wrapper - */ -class Socket { -public: - /** Socket - */ - Socket(); - - /** Set blocking or non-blocking mode of the socket and a timeout on - blocking socket operations - \param blocking true for blocking mode, false for non-blocking mode. - \param timeout timeout in ms [Default: (1500)ms]. - */ - void set_blocking(bool blocking, unsigned int timeout=1500); - - /** Close the socket file descriptor - */ - int close(); - - ~Socket(); - -protected: - bool _blocking; - int _timeout; - ESP8266 * wifi; -}; - - -#endif /* SOCKET_H_ */
--- a/Socket/TCPSocketConnection.cpp Mon Jun 08 21:32:12 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,245 +0,0 @@ -/* 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> -#include <algorithm> - -using std::memset; -using std::memcpy; - -//Debug is disabled by default -#if 1 -#define DBG(x, ...) printf("[TCPConnection : DBG]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__); -#define WARN(x, ...) printf("[TCPConnection: WARN]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__); -#define ERR(x, ...) printf("[TCPConnection : ERR]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__); -#else -#define DBG(x, ...) -#define WARN(x, ...) -#define ERR(x, ...) -#endif - -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 = ESP8266->start(ESP_TCP_TYPE,_ipAddress,_port); - if(_is_connected) { //success - return 0; - } else { // fail - return -1; - } -} - -bool TCPSocketConnection::is_connected(void) -{ - return _is_connected; -} - -int TCPSocketConnection::send(char* data, int length) -{ - if (!_is_connected) { - ERR("TCPSocketConnection::receive() - _is_connected is false : you cant receive data until you connect to a socket!"); - return -1; - } - Timer tmr; - int idx = 0; - tmr.start(); - while ((tmr.read_ms() < _timeout) || _blocking) { - - idx += wifi->send(data, length); - - if (idx == length) - return idx; - } - return (idx == 0) ? -1 : idx; - - //return wifi->send(data,length); -// -// 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; - -} - -// -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 send(data,length); // just remap to send -} - -int TCPSocketConnection::receive(char* buffer, int length) -{ - if (!_is_connected) { - ERR("TCPSocketConnection::receive() - _is_connected is false : you cant receive data until you connect to a socket!"); - return -1; - } - Timer tmr; - int idx = 0; - int nb_available = 0; - int time = -1; - - //make this the non-blocking case and return if <= 0 - // remember to change the config to blocking - // if ( ! _blocking) { - // if ( wifi.readable <= 0 ) { - // return (wifi.readable); - // } - // } - //--- - tmr.start(); - if (_blocking) { - while (1) { - nb_available = wifi->readable(); - if (nb_available != 0) { - break; - } - } - } - //--- - // blocking case - else { - tmr.reset(); - - while (time < _timeout) { - nb_available = wifi->readable(); - if (nb_available < 0) return nb_available; - if (nb_available > 0) break ; - time = tmr.read_ms(); - } - - if (nb_available == 0) return nb_available; - } - - // change this to < 20 mS timeout per byte to detect end of packet gap - // this may not work due to buffering at the UART interface - tmr.reset(); - // while ( tmr.read_ms() < 20 ) { - // if ( wifi.readable() && (idx < length) ) { - // buffer[idx++] = wifi->getc(); - // tmr.reset(); - // } - // if ( idx == length ) { - // break; - // } - // } - //--- - while (time < _timeout) { - - nb_available = wifi->readable(); - //for (int i = 0; i < min(nb_available, length); i++) { - for (int i = 0; i < min(nb_available, (length-idx)); i++) { - buffer[idx] = wifi->getc(); - idx++; - } - if (idx == length) { - break; - } - time = tmr.read_ms(); - } - //--- - return (idx == 0) ? -1 : idx; - -//************************ original code below -// -// 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; - -} - -// -1 if unsuccessful, else number of bytes received -int TCPSocketConnection::receive_all(char* data, int length) -{ - //ERR("receive_all() not yet implimented"); - // 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; - receive(data,length); -}
--- a/Socket/TCPSocketConnection.h Mon Jun 08 21:32:12 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,82 +0,0 @@ -/* 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
--- a/Socket/TCPSocketServer.cpp Mon Jun 08 21:32:12 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,44 +0,0 @@ -/* 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 "TCPSocketServer.h" - -#include <cstring> - -using std::memset; -using std::memcpy; - -TCPSocketServer::TCPSocketServer() { - -} - -int TCPSocketServer::bind(int port) { - if(!wifi->startTCPServer(port)) { - return(-1); - } - _port = port; - return 0; -} - -int TCPSocketServer::listen(int max) { - - return 0; -} - -int TCPSocketServer::accept(TCPSocketConnection& connection) { - return 0; -} \ No newline at end of file
--- a/Socket/TCPSocketServer.h Mon Jun 08 21:32:12 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,55 +0,0 @@ -/* 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 TCPSOCKETSERVER_H -#define TCPSOCKETSERVER_H - -#include "Socket/Socket.h" -#include "TCPSocketConnection.h" - -/** TCP Server. - */ -class TCPSocketServer : public Socket { - friend class TCPSocketConnection; - public: - /** Instantiate a TCP Server. - */ - TCPSocketServer(); - - /** Bind a socket to a specific port. - \param port The port to listen for incoming connections on. - \return 0 on success, -1 on failure. - */ - int bind(int port); - - /** Start listening for incoming connections. - \param backlog number of pending connections that can be queued up at any - one time [Default: 1]. - \return 0 on success, -1 on failure. - */ - int listen(int backlog=1); - - /** Accept a new connection. - \param connection A TCPSocketConnection instance that will handle the incoming connection. - \return 0 on success, -1 on failure. - */ - int accept(TCPSocketConnection& connection); -private: - int _port; -}; - -#endif \ No newline at end of file
--- a/Socket/UDPSocket.cpp Mon Jun 08 21:32:12 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,154 +0,0 @@ -/* 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 "UDPSocket.h" - -#include <string> -#include <algorithm> - -UDPSocket::UDPSocket() -{ - endpoint_configured = false; - endpoint_read = false; - Endpoint currentEndpoint; -} - -int UDPSocket::init(void) -{ - return 0; -} - -// Server initialization -int UDPSocket::bind(int port) -{ - return 0; -} - -// -1 if unsuccessful, else number of bytes written -int UDPSocket::sendTo(Endpoint &remote, char *packet, int length) -{ - Timer tmr; - int idx = 0; - - - confEndpoint(remote); - - // initialize transparent mode if not already done - if(!endpoint_configured) { - // initialize UDP (default id of -1 means transparent mode) - //!wifi->start(ESP_UDP_TYPE, remote._ipAddress, remote._port, remote._id - if(!wifi->startUDP(remote._ipAddress, remote._port, 0,length)) { - return(-1); - } - endpoint_configured = true; - } - - tmr.start(); - - while ((tmr.read_ms() < _timeout) || _blocking) { - - idx += wifi->send(packet, length); - - if (idx == length) - return idx; - } - return (idx == 0) ? -1 : idx; -} - -// -1 if unsuccessful, else number of bytes received -int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length) -{ - Timer tmr; - int idx = 0; - int nb_available = 0; - int time = -1; - - //make this the non-blocking case and return if <= 0 - // remember to change the config to blocking - // if ( ! _blocking) { - // if ( wifi.readable <= 0 ) { - // return (wifi.readable); - // } - // } - //--- - tmr.start(); - if (_blocking) { - while (1) { - nb_available = wifi->readable(); - if (nb_available != 0) { - break; - } - } - } - //--- - // blocking case - else { - tmr.reset(); - - while (time < _timeout) { - nb_available = wifi->readable(); - if (nb_available < 0) return nb_available; - if (nb_available > 0) break ; - time = tmr.read_ms(); - } - - if (nb_available == 0) return nb_available; - } - - // change this to < 20 mS timeout per byte to detect end of packet gap - // this may not work due to buffering at the UART interface - tmr.reset(); - // while ( tmr.read_ms() < 20 ) { - // if ( wifi.readable() && (idx < length) ) { - // buffer[idx++] = wifi->getc(); - // tmr.reset(); - // } - // if ( idx == length ) { - // break; - // } - // } - //--- - while (time < _timeout) { - - nb_available = wifi->readable(); - //for (int i = 0; i < min(nb_available, length); i++) { - for (int i = 0; i < min(nb_available, (length-idx)); i++) { - buffer[idx] = wifi->getc(); - idx++; - } - if (idx == length) { - break; - } - time = tmr.read_ms(); - } - //--- - readEndpoint(remote); - return (idx == 0) ? -1 : idx; -} - -bool UDPSocket::confEndpoint(Endpoint & ep) -{ - currentEndpoint = ep; - return true; -} - -bool UDPSocket::readEndpoint(Endpoint & ep) -{ - ep = currentEndpoint; - return true; -}
--- a/Socket/UDPSocket.h Mon Jun 08 21:32:12 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,76 +0,0 @@ -/* 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 UDPSOCKET_H -#define UDPSOCKET_H - -#include "Endpoint.h" -#include "Socket.h" - -#include <cstdint> - -/** -UDP Socket -*/ -class UDPSocket: public Socket { - -public: - /** Instantiate an UDP Socket. - */ - UDPSocket(); - - /** Init the UDP Client Socket without binding it to any specific port - \return 0 on success, -1 on failure. - */ - int init(void); - - /** Bind a UDP Server Socket to a specific port - \param port The port to listen for incoming connections on - \return 0 on success, -1 on failure. - */ - int bind(int port = -1); - - /** Send a packet to a remote endpoint - \param remote The remote endpoint - \param packet The packet to be sent - \param length The length of the packet to be sent - \return the number of written bytes on success (>=0) or -1 on failure - */ - int sendTo(Endpoint &remote, char *packet, int length); - - /** Receive a packet from a remote endpoint - \param remote The remote endpoint - \param buffer The buffer for storing the incoming packet data. If a packet - is too long to fit in the supplied buffer, excess bytes are discarded - \param length The length of the buffer - \return the number of received bytes on success (>=0) or -1 on failure - */ - int receiveFrom(Endpoint &remote, char *buffer, int length); - -private: - bool confEndpoint(Endpoint & ep); - bool readEndpoint(Endpoint & ep); - bool endpoint_configured; - bool endpoint_read; - Endpoint currentEndpoint; - -}; - -#include "def.h" - -#endif