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
ESP8266Interface.cpp@29:f2ae8f47729b, 2015-08-27 (annotated)
- Committer:
- sarahmarshy
- Date:
- Thu Aug 27 14:32:50 2015 +0000
- Revision:
- 29:f2ae8f47729b
- Parent:
- 28:6426873b21bd
- Child:
- 30:c19f1e61063b
DnsQuery Structrue Change
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sarahmarshy | 18:9fc7976c7b27 | 1 | /* ESP8266Interface Example |
sam_grove | 11:288c15b80a26 | 2 | * Copyright (c) 2015 ARM Limited |
sam_grove | 11:288c15b80a26 | 3 | * |
sam_grove | 11:288c15b80a26 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
sam_grove | 11:288c15b80a26 | 5 | * you may not use this file except in compliance with the License. |
sam_grove | 11:288c15b80a26 | 6 | * You may obtain a copy of the License at |
sam_grove | 11:288c15b80a26 | 7 | * |
sam_grove | 11:288c15b80a26 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
sam_grove | 11:288c15b80a26 | 9 | * |
sam_grove | 11:288c15b80a26 | 10 | * Unless required by applicable law or agreed to in writing, software |
sam_grove | 11:288c15b80a26 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
sam_grove | 11:288c15b80a26 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
sam_grove | 11:288c15b80a26 | 13 | * See the License for the specific language governing permissions and |
sam_grove | 11:288c15b80a26 | 14 | * limitations under the License. |
sam_grove | 11:288c15b80a26 | 15 | */ |
sam_grove | 24:37504440f296 | 16 | |
sarahmarshy | 18:9fc7976c7b27 | 17 | #include "ESP8266Interface.h" |
sam_grove | 11:288c15b80a26 | 18 | |
sam_grove | 24:37504440f296 | 19 | ESP8266Interface::ESP8266Interface(PinName tx, PinName rx) : esp8266(tx, rx) |
sam_grove | 11:288c15b80a26 | 20 | { |
sarahmarshy | 22:312453862371 | 21 | uuidCounter = 0; |
sam_grove | 24:37504440f296 | 22 | std::fill_n(availableID, sizeof(availableID)/sizeof(int), -1); |
sam_grove | 11:288c15b80a26 | 23 | } |
sam_grove | 11:288c15b80a26 | 24 | |
sam_grove | 24:37504440f296 | 25 | int32_t ESP8266Interface::init(void) |
sam_grove | 24:37504440f296 | 26 | { |
sam_grove | 24:37504440f296 | 27 | if (!esp8266.startup()) { |
sarahmarshy | 18:9fc7976c7b27 | 28 | return -1; |
sam_grove | 24:37504440f296 | 29 | } |
sam_grove | 24:37504440f296 | 30 | if (!esp8266.reset()) { |
sarahmarshy | 18:9fc7976c7b27 | 31 | return -1; |
sam_grove | 24:37504440f296 | 32 | } |
sam_grove | 24:37504440f296 | 33 | if (!esp8266.wifiMode(3)) { |
sarahmarshy | 18:9fc7976c7b27 | 34 | return -1; |
sam_grove | 24:37504440f296 | 35 | } |
sam_grove | 24:37504440f296 | 36 | if (!esp8266.multipleConnections(true)) { |
sarahmarshy | 18:9fc7976c7b27 | 37 | return -1; |
sam_grove | 24:37504440f296 | 38 | } |
sarahmarshy | 18:9fc7976c7b27 | 39 | return 0; |
sam_grove | 11:288c15b80a26 | 40 | } |
sam_grove | 11:288c15b80a26 | 41 | |
sam_grove | 24:37504440f296 | 42 | int32_t ESP8266Interface::init(const char *ip, const char *mask, const char *gateway) |
sam_grove | 11:288c15b80a26 | 43 | { |
sarahmarshy | 23:fd0f3197c30b | 44 | return -1; |
sam_grove | 11:288c15b80a26 | 45 | } |
sam_grove | 11:288c15b80a26 | 46 | |
sam_grove | 24:37504440f296 | 47 | int32_t ESP8266Interface::connect(uint32_t timeout_ms) |
sam_grove | 11:288c15b80a26 | 48 | { |
sam_grove | 11:288c15b80a26 | 49 | return -1; |
sam_grove | 11:288c15b80a26 | 50 | } |
sam_grove | 11:288c15b80a26 | 51 | |
sarahmarshy | 18:9fc7976c7b27 | 52 | int32_t ESP8266Interface::connect(const char *ap, const char *pass_phrase, wifi_security_t security, uint32_t timeout_ms) |
sam_grove | 11:288c15b80a26 | 53 | { |
sarahmarshy | 23:fd0f3197c30b | 54 | esp8266.setTimeout(timeout_ms); |
sam_grove | 24:37504440f296 | 55 | if (!esp8266.dhcp(1, true)) { |
sarahmarshy | 18:9fc7976c7b27 | 56 | return -1; |
sarahmarshy | 18:9fc7976c7b27 | 57 | } |
sam_grove | 24:37504440f296 | 58 | if (!esp8266.connect(ap, pass_phrase)) { |
sam_grove | 24:37504440f296 | 59 | return -1; |
sam_grove | 24:37504440f296 | 60 | } |
sam_grove | 24:37504440f296 | 61 | return 0; |
sam_grove | 11:288c15b80a26 | 62 | } |
sam_grove | 11:288c15b80a26 | 63 | |
sarahmarshy | 22:312453862371 | 64 | int32_t ESP8266Interface::disconnect(void) |
sam_grove | 11:288c15b80a26 | 65 | { |
sam_grove | 24:37504440f296 | 66 | if (!esp8266.disconnect()) { |
sarahmarshy | 22:312453862371 | 67 | return -1; |
sam_grove | 24:37504440f296 | 68 | } |
sam_grove | 24:37504440f296 | 69 | for(int i=0; i<numSockets; i++) { |
sarahmarshy | 22:312453862371 | 70 | SocketInterface *socket = sockets[availableID[i]]; |
sarahmarshy | 22:312453862371 | 71 | deallocateSocket(socket); |
sarahmarshy | 22:312453862371 | 72 | } |
sarahmarshy | 22:312453862371 | 73 | return 0; |
sam_grove | 11:288c15b80a26 | 74 | } |
sam_grove | 11:288c15b80a26 | 75 | |
sarahmarshy | 18:9fc7976c7b27 | 76 | char *ESP8266Interface::getIPAddress(void) |
sarahmarshy | 18:9fc7976c7b27 | 77 | { |
sam_grove | 24:37504440f296 | 78 | if(!esp8266.getIPAddress(ip)) { |
sarahmarshy | 18:9fc7976c7b27 | 79 | return NULL; |
sam_grove | 24:37504440f296 | 80 | } |
sarahmarshy | 18:9fc7976c7b27 | 81 | return ip; |
sarahmarshy | 18:9fc7976c7b27 | 82 | } |
sarahmarshy | 18:9fc7976c7b27 | 83 | |
sarahmarshy | 18:9fc7976c7b27 | 84 | char *ESP8266Interface::getGateway(void) const |
sam_grove | 11:288c15b80a26 | 85 | { |
sam_grove | 11:288c15b80a26 | 86 | return 0; |
sam_grove | 11:288c15b80a26 | 87 | } |
sam_grove | 11:288c15b80a26 | 88 | |
sarahmarshy | 18:9fc7976c7b27 | 89 | char *ESP8266Interface::getNetworkMask(void) const |
sam_grove | 11:288c15b80a26 | 90 | { |
sam_grove | 11:288c15b80a26 | 91 | return 0; |
sam_grove | 11:288c15b80a26 | 92 | } |
sam_grove | 11:288c15b80a26 | 93 | |
sarahmarshy | 18:9fc7976c7b27 | 94 | char *ESP8266Interface::getMACAddress(void) const |
sam_grove | 11:288c15b80a26 | 95 | { |
sam_grove | 11:288c15b80a26 | 96 | return 0; |
sam_grove | 11:288c15b80a26 | 97 | } |
sam_grove | 11:288c15b80a26 | 98 | |
sam_grove | 24:37504440f296 | 99 | int32_t ESP8266Interface::isConnected(void) |
sam_grove | 11:288c15b80a26 | 100 | { |
sam_grove | 24:37504440f296 | 101 | return (getIPAddress() == NULL) ? -1 : 0; |
sam_grove | 11:288c15b80a26 | 102 | } |
sam_grove | 11:288c15b80a26 | 103 | |
sam_grove | 24:37504440f296 | 104 | SocketInterface *ESP8266Interface::allocateSocket(socket_protocol_t socketProtocol) |
sam_grove | 24:37504440f296 | 105 | { |
sarahmarshy | 18:9fc7976c7b27 | 106 | int id = -1; |
sarahmarshy | 18:9fc7976c7b27 | 107 | //Look through the array of available sockets for an unused ID |
sam_grove | 24:37504440f296 | 108 | for(int i=0; i<numSockets; i++) { |
sam_grove | 24:37504440f296 | 109 | if (availableID[i] == -1) { |
sam_grove | 24:37504440f296 | 110 | id = i; |
sarahmarshy | 18:9fc7976c7b27 | 111 | availableID[i] = uuidCounter; |
sarahmarshy | 18:9fc7976c7b27 | 112 | break; |
sarahmarshy | 18:9fc7976c7b27 | 113 | } |
sarahmarshy | 18:9fc7976c7b27 | 114 | } |
sam_grove | 24:37504440f296 | 115 | if (id == -1) { |
sam_grove | 24:37504440f296 | 116 | return NULL;//tried to allocate more than the maximum 5 sockets |
sarahmarshy | 18:9fc7976c7b27 | 117 | } |
sam_grove | 24:37504440f296 | 118 | ESP8266Socket *socket = new ESP8266Socket(uuidCounter++, esp8266, socketProtocol, (uint8_t)id); |
sarahmarshy | 22:312453862371 | 119 | sockets[socket->getHandle()] = socket; |
sarahmarshy | 18:9fc7976c7b27 | 120 | return socket; |
sam_grove | 11:288c15b80a26 | 121 | } |
sam_grove | 13:0186e9e35a24 | 122 | |
sam_grove | 24:37504440f296 | 123 | int ESP8266Interface::deallocateSocket(SocketInterface *socket) |
bridadan | 16:b2f781416464 | 124 | { |
sarahmarshy | 18:9fc7976c7b27 | 125 | int id = (int)static_cast<ESP8266Socket*>(socket)->getID(); |
sarahmarshy | 18:9fc7976c7b27 | 126 | availableID[id] = -1; |
sam_grove | 24:37504440f296 | 127 | |
sarahmarshy | 22:312453862371 | 128 | std::map<uint32_t, SocketInterface*>::iterator it; |
sam_grove | 24:37504440f296 | 129 | |
sarahmarshy | 22:312453862371 | 130 | // Check if socket is owned by WiFiRadioInterface |
sarahmarshy | 22:312453862371 | 131 | it = sockets.find(socket->getHandle()); |
sam_grove | 24:37504440f296 | 132 | |
sarahmarshy | 22:312453862371 | 133 | if (it != sockets.end()) { |
sarahmarshy | 22:312453862371 | 134 | // If so, erase it from the internal socket map and deallocate the socket |
sarahmarshy | 22:312453862371 | 135 | sockets.erase(it); |
sarahmarshy | 22:312453862371 | 136 | delete socket; |
sarahmarshy | 22:312453862371 | 137 | } else { |
sarahmarshy | 22:312453862371 | 138 | // Socket is not owned by WiFiRadioInterface, so return -1 error |
sarahmarshy | 22:312453862371 | 139 | return -1; |
sarahmarshy | 22:312453862371 | 140 | } |
sam_grove | 24:37504440f296 | 141 | return 0; |
bridadan | 16:b2f781416464 | 142 | } |
sarahmarshy | 25:91c4e9d34b77 | 143 | void ESP8266Interface::getHostByName(const char *name, char* hostIP) |
sarahmarshy | 25:91c4e9d34b77 | 144 | { |
sarahmarshy | 25:91c4e9d34b77 | 145 | SocketInterface* sock = this->allocateSocket(SOCK_UDP); |
sarahmarshy | 29:f2ae8f47729b | 146 | DnsQuery dns(sock, name, hostIP); |
sarahmarshy | 25:91c4e9d34b77 | 147 | this->deallocateSocket(sock); |
sarahmarshy | 25:91c4e9d34b77 | 148 | } |
bridadan | 16:b2f781416464 | 149 | |
sam_grove | 24:37504440f296 | 150 | ESP8266Socket::ESP8266Socket(uint32_t handle, ESP8266 &driver, socket_protocol_t type, uint8_t id) |
sarahmarshy | 18:9fc7976c7b27 | 151 | { |
sarahmarshy | 22:312453862371 | 152 | _handle = handle; |
sam_grove | 24:37504440f296 | 153 | _driver = &driver; |
sam_grove | 24:37504440f296 | 154 | _type = type; |
sam_grove | 24:37504440f296 | 155 | _id = id; |
sarahmarshy | 18:9fc7976c7b27 | 156 | } |
sarahmarshy | 18:9fc7976c7b27 | 157 | |
sarahmarshy | 18:9fc7976c7b27 | 158 | const char *ESP8266Socket::getHostByName(const char *name) const |
sam_grove | 13:0186e9e35a24 | 159 | { |
sam_grove | 13:0186e9e35a24 | 160 | return 0; |
sam_grove | 13:0186e9e35a24 | 161 | } |
sam_grove | 13:0186e9e35a24 | 162 | |
sarahmarshy | 18:9fc7976c7b27 | 163 | void ESP8266Socket::setAddress(const char* addr) |
bridadan | 16:b2f781416464 | 164 | { |
sam_grove | 24:37504440f296 | 165 | _addr = addr; |
sarahmarshy | 18:9fc7976c7b27 | 166 | } |
sarahmarshy | 18:9fc7976c7b27 | 167 | |
sam_grove | 24:37504440f296 | 168 | void ESP8266Socket::setPort(uint16_t port) |
sarahmarshy | 18:9fc7976c7b27 | 169 | { |
sam_grove | 24:37504440f296 | 170 | _port = port; |
bridadan | 16:b2f781416464 | 171 | } |
bridadan | 16:b2f781416464 | 172 | |
sarahmarshy | 18:9fc7976c7b27 | 173 | void ESP8266Socket::setAddressPort(const char* addr, uint16_t port) |
bridadan | 16:b2f781416464 | 174 | { |
sam_grove | 24:37504440f296 | 175 | _addr = addr; |
sarahmarshy | 18:9fc7976c7b27 | 176 | _port = port; |
bridadan | 16:b2f781416464 | 177 | } |
bridadan | 16:b2f781416464 | 178 | |
sarahmarshy | 18:9fc7976c7b27 | 179 | const char *ESP8266Socket::getAddress(void) const |
sarahmarshy | 18:9fc7976c7b27 | 180 | { |
sam_grove | 24:37504440f296 | 181 | return _addr; |
sarahmarshy | 18:9fc7976c7b27 | 182 | } |
sarahmarshy | 18:9fc7976c7b27 | 183 | |
sarahmarshy | 18:9fc7976c7b27 | 184 | uint16_t ESP8266Socket::getPort(void) const |
sarahmarshy | 18:9fc7976c7b27 | 185 | { |
sarahmarshy | 18:9fc7976c7b27 | 186 | return _port; |
sarahmarshy | 18:9fc7976c7b27 | 187 | } |
sarahmarshy | 18:9fc7976c7b27 | 188 | |
sarahmarshy | 18:9fc7976c7b27 | 189 | |
sarahmarshy | 18:9fc7976c7b27 | 190 | int32_t ESP8266Socket::bind(uint16_t port) const |
sam_grove | 13:0186e9e35a24 | 191 | { |
sam_grove | 13:0186e9e35a24 | 192 | return -1; |
sam_grove | 13:0186e9e35a24 | 193 | } |
sam_grove | 13:0186e9e35a24 | 194 | |
sarahmarshy | 18:9fc7976c7b27 | 195 | int32_t ESP8266Socket::listen(void) const |
sam_grove | 13:0186e9e35a24 | 196 | { |
sam_grove | 13:0186e9e35a24 | 197 | return -1; |
sam_grove | 13:0186e9e35a24 | 198 | } |
sam_grove | 13:0186e9e35a24 | 199 | |
sarahmarshy | 18:9fc7976c7b27 | 200 | int32_t ESP8266Socket::accept() const |
sam_grove | 13:0186e9e35a24 | 201 | { |
sam_grove | 13:0186e9e35a24 | 202 | return -1; |
sam_grove | 13:0186e9e35a24 | 203 | } |
sam_grove | 13:0186e9e35a24 | 204 | |
sam_grove | 24:37504440f296 | 205 | int32_t ESP8266Socket::open() |
sam_grove | 13:0186e9e35a24 | 206 | { |
sarahmarshy | 26:6e36dd3cec3f | 207 | |
sam_grove | 24:37504440f296 | 208 | string sock_type = (SOCK_UDP == _type) ? "UDP" : "TCP"; |
sam_grove | 24:37504440f296 | 209 | if (!_driver->openSocket(sock_type, _id, _port, _addr)) { |
sam_grove | 24:37504440f296 | 210 | return -1; |
sarahmarshy | 18:9fc7976c7b27 | 211 | } |
sam_grove | 13:0186e9e35a24 | 212 | return 0; |
sam_grove | 24:37504440f296 | 213 | |
sam_grove | 13:0186e9e35a24 | 214 | } |
sam_grove | 13:0186e9e35a24 | 215 | |
sam_grove | 24:37504440f296 | 216 | int32_t ESP8266Socket::send(const void *data, uint32_t amount, uint32_t timeout_ms) |
bridadan | 16:b2f781416464 | 217 | { |
sam_grove | 24:37504440f296 | 218 | _driver->setTimeout((int)timeout_ms); |
sam_grove | 24:37504440f296 | 219 | if(!_driver->sendData(_id, data, amount)) { |
sarahmarshy | 18:9fc7976c7b27 | 220 | return -1; |
sarahmarshy | 18:9fc7976c7b27 | 221 | } |
bridadan | 16:b2f781416464 | 222 | return 0; |
bridadan | 16:b2f781416464 | 223 | } |
bridadan | 16:b2f781416464 | 224 | |
sarahmarshy | 18:9fc7976c7b27 | 225 | uint32_t ESP8266Socket::recv(void *data, uint32_t amount, uint32_t timeout_ms) |
sam_grove | 13:0186e9e35a24 | 226 | { |
sarahmarshy | 27:eaeecaaae611 | 227 | _driver->setTimeout((int)timeout_ms); |
sam_grove | 24:37504440f296 | 228 | return _driver->recv(data, amount); |
sam_grove | 13:0186e9e35a24 | 229 | } |
sam_grove | 13:0186e9e35a24 | 230 | |
sarahmarshy | 18:9fc7976c7b27 | 231 | int32_t ESP8266Socket::close() const |
sam_grove | 13:0186e9e35a24 | 232 | { |
sam_grove | 24:37504440f296 | 233 | if (!_driver->close(_id)) { |
sarahmarshy | 23:fd0f3197c30b | 234 | return -1;//closing socket not succesful |
sarahmarshy | 18:9fc7976c7b27 | 235 | } |
sam_grove | 13:0186e9e35a24 | 236 | return 0; |
sam_grove | 13:0186e9e35a24 | 237 | } |
sarahmarshy | 22:312453862371 | 238 | |
sarahmarshy | 22:312453862371 | 239 | uint32_t ESP8266Socket::getHandle()const |
sarahmarshy | 22:312453862371 | 240 | { |
sarahmarshy | 22:312453862371 | 241 | return _handle; |
sarahmarshy | 22:312453862371 | 242 | } |
sarahmarshy | 22:312453862371 | 243 | |
sarahmarshy | 18:9fc7976c7b27 | 244 | uint8_t ESP8266Socket::getID() |
sam_grove | 13:0186e9e35a24 | 245 | { |
sam_grove | 24:37504440f296 | 246 | return _id; |
sarahmarshy | 25:91c4e9d34b77 | 247 | } |