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
Diff: ESP8266Interface.cpp
- Branch:
- api-changes
- Revision:
- 34:9c26a3dcdc1f
- Parent:
- 33:276cb279df57
- Child:
- 38:eb1e46561a19
--- a/ESP8266Interface.cpp Mon Feb 22 23:56:33 2016 +0000 +++ b/ESP8266Interface.cpp Mon Feb 22 23:53:46 2016 -0600 @@ -15,230 +15,73 @@ */ #include "ESP8266Interface.h" - -ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, uint8_t trace) : esp8266(tx, rx, trace) -{ - uuidCounter = 0; - std::fill_n(availableID, sizeof(availableID)/sizeof(int), -1); -} +#include "ESP8266SocketInterface.h" -int32_t ESP8266Interface::init(void) +ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug) + : _esp(tx, rx, debug) { - return 0; -} - -int32_t ESP8266Interface::init(const char *ip, const char *mask, const char *gateway) -{ - return -1; + memset(_ids, 0, sizeof(_ids)); } -int32_t ESP8266Interface::connect(uint32_t timeout_ms) -{ - return -1; -} - -int32_t ESP8266Interface::connect(const char *ap, const char *pass_phrase, wifi_security_t security, uint32_t timeout_ms) +int32_t ESP8266Interface::connect( + const char *ap, + const char *pass_phrase, + wifi_security_t, + uint32_t timeout_ms) { - esp8266.setTimeout(timeout_ms); - if (!esp8266.startup(3)) { - return -1; - } - if (!esp8266.dhcp(true, 1)) { - return -1; - } - if (!esp8266.connect(ap, pass_phrase)) { - return -1; - } - if (!esp8266.isConnected()) { - return -1; - } + _esp.setTimeout(timeout_ms); + + if (!_esp.startup(3)) return -1; + if (!_esp.dhcp(true, 1)) return -1; + if (!_esp.connect(ap, pass_phrase)) return -1; + + _ip_address = _esp.getIPAddress(); + _mac_address = _esp.getMACAddress(); + if (!_ip_address || !_mac_address) return -1; + return 0; } -int32_t ESP8266Interface::disconnect(void) +int32_t ESP8266Interface::disconnect() { - if (!esp8266.disconnect()) { - return -1; - } - for (int i=0; i<numSockets; i++) { - SocketInterface *socket = sockets[availableID[i]]; - if (socket) { - deallocateSocket(socket); - } - } + if (!_esp.disconnect()) return -1; + return 0; } -char *ESP8266Interface::getIPAddress(void) +const char *ESP8266Interface::getIPAddress() { - const char *src = esp8266.getIPAddress(); - if (!src) return 0; - - strcpy(ip, src); - return ip; + return _ip_address; } -char *ESP8266Interface::getGateway(void) const +const char *ESP8266Interface::getMACAddress() { - return 0; -} - -char *ESP8266Interface::getNetworkMask(void) const -{ - return 0; + return _mac_address; } -char *ESP8266Interface::getMACAddress(void) const -{ - return 0; -} - -int32_t ESP8266Interface::isConnected(void) +SocketInterface *ESP8266Interface::createSocket(socket_protocol_t proto) { - return (getIPAddress() == NULL) ? -1 : 0; -} - -SocketInterface *ESP8266Interface::allocateSocket(socket_protocol_t socketProtocol) -{ + // Look for an unused socket int id = -1; - //Look through the array of available sockets for an unused ID - for(int i=0; i<numSockets; i++) { - if (availableID[i] == -1) { + + for (int i = 0; i < ESP8266_SOCKET_COUNT; i++) { + if (!_ids[i]) { id = i; - availableID[i] = uuidCounter; + _ids[i] = true; break; } } + if (id == -1) { - return NULL;//tried to allocate more than the maximum 5 sockets - } - ESP8266Socket *socket = new ESP8266Socket(uuidCounter++, esp8266, socketProtocol, (uint8_t)id); - sockets[socket->getHandle()] = socket; - return socket; -} - -int ESP8266Interface::deallocateSocket(SocketInterface *socket) -{ - int id = (int)static_cast<ESP8266Socket*>(socket)->getID(); - availableID[id] = -1; - - std::map<uint32_t, SocketInterface*>::iterator it; - - // Check if socket is owned by WiFiRadioInterface - it = sockets.find(socket->getHandle()); - - if (it != sockets.end()) { - // If so, erase it from the internal socket map and deallocate the socket - sockets.erase(it); - delete socket; - } else { - // Socket is not owned by WiFiRadioInterface, so return -1 error - return -1; + return 0; } - return 0; -} -void ESP8266Interface::getHostByName(const char *name, char* hostIP) -{ - SocketInterface* sock = this->allocateSocket(SOCK_UDP); - DnsQuery dns(sock, name, hostIP); - this->deallocateSocket(sock); -} -ESP8266Socket::ESP8266Socket(uint32_t handle, ESP8266 &driver, socket_protocol_t type, uint8_t id) -{ - _handle = handle; - _driver = &driver; - _type = type; - _id = id; -} - -const char *ESP8266Socket::getHostByName(const char *name) const -{ - return 0; -} - -void ESP8266Socket::setAddress(const char* addr) -{ - _addr = addr; -} - -void ESP8266Socket::setPort(uint16_t port) -{ - _port = port; -} - -void ESP8266Socket::setAddressPort(const char* addr, uint16_t port) -{ - _addr = addr; - _port = port; + return new ESP8266SocketInterface(&_esp, proto, id); } -const char *ESP8266Socket::getAddress(void) const -{ - return _addr; -} - -uint16_t ESP8266Socket::getPort(void) const +void ESP8266Interface::destroySocket(SocketInterface *iface) { - return _port; -} - - -int32_t ESP8266Socket::bind(uint16_t port) const -{ - return -1; -} - -int32_t ESP8266Socket::listen(void) const -{ - return -1; -} - -int32_t ESP8266Socket::accept() const -{ - return -1; + ESP8266SocketInterface *socket = (ESP8266SocketInterface *)iface; + _ids[socket->getID()] = false; } -int32_t ESP8266Socket::open() -{ - - const char *sock_type = (SOCK_UDP == _type) ? "UDP" : "TCP"; - if (!_driver->open(sock_type, _id, _addr, _port)) { - return -1; - } - return 0; - -} - -int32_t ESP8266Socket::send(const void *data, uint32_t amount, uint32_t timeout_ms) -{ - _driver->setTimeout((int)timeout_ms); - if(!_driver->send(_id, data, amount)) { - return -1; - } - return 0; -} - -uint32_t ESP8266Socket::recv(void *data, uint32_t amount, uint32_t timeout_ms) -{ - _driver->setTimeout((int)timeout_ms); - return _driver->recv(_id, data, amount); -} - -int32_t ESP8266Socket::close() const -{ - if (!_driver->close(_id)) { - return -1;//closing socket not succesful - } - return 0; -} - -uint32_t ESP8266Socket::getHandle()const -{ - return _handle; -} - -uint8_t ESP8266Socket::getID() -{ - return _id; -}