modified by ohneta

Dependencies:   ESP8266

Dependents:   HelloESP8266Interface_mine

Fork of ESP8266Interface by NetworkSocketAPI

Committer:
ohneta
Date:
Wed Nov 11 05:59:49 2015 +0000
Revision:
33:dcc7d9bd1ae2
Parent:
32:1d16668d116c
modiried by ohneta

Who changed what in which revision?

UserRevisionLine numberNew 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);
ohneta 30:fd77d067f5ab 23 preferred_dnsip = NULL;
sam_grove 11:288c15b80a26 24 }
sam_grove 11:288c15b80a26 25
sam_grove 24:37504440f296 26 int32_t ESP8266Interface::init(void)
sam_grove 24:37504440f296 27 {
sam_grove 24:37504440f296 28 if (!esp8266.startup()) {
sarahmarshy 18:9fc7976c7b27 29 return -1;
sam_grove 24:37504440f296 30 }
sam_grove 24:37504440f296 31 if (!esp8266.reset()) {
sarahmarshy 18:9fc7976c7b27 32 return -1;
sam_grove 24:37504440f296 33 }
sam_grove 24:37504440f296 34 if (!esp8266.wifiMode(3)) {
sarahmarshy 18:9fc7976c7b27 35 return -1;
sam_grove 24:37504440f296 36 }
sam_grove 24:37504440f296 37 if (!esp8266.multipleConnections(true)) {
sarahmarshy 18:9fc7976c7b27 38 return -1;
sam_grove 24:37504440f296 39 }
sarahmarshy 18:9fc7976c7b27 40 return 0;
sam_grove 11:288c15b80a26 41 }
sam_grove 11:288c15b80a26 42
sam_grove 24:37504440f296 43 int32_t ESP8266Interface::init(const char *ip, const char *mask, const char *gateway)
sam_grove 11:288c15b80a26 44 {
sarahmarshy 23:fd0f3197c30b 45 return -1;
sam_grove 11:288c15b80a26 46 }
sam_grove 11:288c15b80a26 47
sam_grove 24:37504440f296 48 int32_t ESP8266Interface::connect(uint32_t timeout_ms)
sam_grove 11:288c15b80a26 49 {
sam_grove 11:288c15b80a26 50 return -1;
sam_grove 11:288c15b80a26 51 }
sam_grove 11:288c15b80a26 52
sarahmarshy 18:9fc7976c7b27 53 int32_t ESP8266Interface::connect(const char *ap, const char *pass_phrase, wifi_security_t security, uint32_t timeout_ms)
sam_grove 11:288c15b80a26 54 {
sarahmarshy 23:fd0f3197c30b 55 esp8266.setTimeout(timeout_ms);
sam_grove 24:37504440f296 56 if (!esp8266.dhcp(1, true)) {
sarahmarshy 18:9fc7976c7b27 57 return -1;
sarahmarshy 18:9fc7976c7b27 58 }
sam_grove 24:37504440f296 59 if (!esp8266.connect(ap, pass_phrase)) {
sam_grove 24:37504440f296 60 return -1;
sam_grove 24:37504440f296 61 }
sam_grove 24:37504440f296 62 return 0;
sam_grove 11:288c15b80a26 63 }
sam_grove 11:288c15b80a26 64
sarahmarshy 22:312453862371 65 int32_t ESP8266Interface::disconnect(void)
sam_grove 11:288c15b80a26 66 {
sam_grove 24:37504440f296 67 if (!esp8266.disconnect()) {
sarahmarshy 22:312453862371 68 return -1;
sam_grove 24:37504440f296 69 }
ohneta 32:1d16668d116c 70
sam_grove 24:37504440f296 71 for(int i=0; i<numSockets; i++) {
sarahmarshy 22:312453862371 72 SocketInterface *socket = sockets[availableID[i]];
ohneta 32:1d16668d116c 73 if (availableID[i] != -1) {
ohneta 32:1d16668d116c 74 deallocateSocket(socket);
ohneta 32:1d16668d116c 75 }
sarahmarshy 22:312453862371 76 }
sarahmarshy 22:312453862371 77 return 0;
sam_grove 11:288c15b80a26 78 }
sam_grove 11:288c15b80a26 79
sarahmarshy 18:9fc7976c7b27 80 char *ESP8266Interface::getIPAddress(void)
sarahmarshy 18:9fc7976c7b27 81 {
sam_grove 24:37504440f296 82 if(!esp8266.getIPAddress(ip)) {
sarahmarshy 18:9fc7976c7b27 83 return NULL;
sam_grove 24:37504440f296 84 }
sarahmarshy 18:9fc7976c7b27 85 return ip;
sarahmarshy 18:9fc7976c7b27 86 }
sarahmarshy 18:9fc7976c7b27 87
sarahmarshy 18:9fc7976c7b27 88 char *ESP8266Interface::getGateway(void) const
sam_grove 11:288c15b80a26 89 {
sam_grove 11:288c15b80a26 90 return 0;
sam_grove 11:288c15b80a26 91 }
sam_grove 11:288c15b80a26 92
sarahmarshy 18:9fc7976c7b27 93 char *ESP8266Interface::getNetworkMask(void) const
sam_grove 11:288c15b80a26 94 {
sam_grove 11:288c15b80a26 95 return 0;
sam_grove 11:288c15b80a26 96 }
sam_grove 11:288c15b80a26 97
sarahmarshy 18:9fc7976c7b27 98 char *ESP8266Interface::getMACAddress(void) const
sam_grove 11:288c15b80a26 99 {
sam_grove 11:288c15b80a26 100 return 0;
sam_grove 11:288c15b80a26 101 }
sam_grove 11:288c15b80a26 102
sam_grove 24:37504440f296 103 int32_t ESP8266Interface::isConnected(void)
sam_grove 11:288c15b80a26 104 {
sam_grove 24:37504440f296 105 return (getIPAddress() == NULL) ? -1 : 0;
sam_grove 11:288c15b80a26 106 }
sam_grove 11:288c15b80a26 107
sam_grove 24:37504440f296 108 SocketInterface *ESP8266Interface::allocateSocket(socket_protocol_t socketProtocol)
sam_grove 24:37504440f296 109 {
sarahmarshy 18:9fc7976c7b27 110 int id = -1;
sarahmarshy 18:9fc7976c7b27 111 //Look through the array of available sockets for an unused ID
sam_grove 24:37504440f296 112 for(int i=0; i<numSockets; i++) {
sam_grove 24:37504440f296 113 if (availableID[i] == -1) {
sam_grove 24:37504440f296 114 id = i;
sarahmarshy 18:9fc7976c7b27 115 availableID[i] = uuidCounter;
sarahmarshy 18:9fc7976c7b27 116 break;
sarahmarshy 18:9fc7976c7b27 117 }
sarahmarshy 18:9fc7976c7b27 118 }
sam_grove 24:37504440f296 119 if (id == -1) {
sam_grove 24:37504440f296 120 return NULL;//tried to allocate more than the maximum 5 sockets
sarahmarshy 18:9fc7976c7b27 121 }
sam_grove 24:37504440f296 122 ESP8266Socket *socket = new ESP8266Socket(uuidCounter++, esp8266, socketProtocol, (uint8_t)id);
sarahmarshy 22:312453862371 123 sockets[socket->getHandle()] = socket;
sarahmarshy 18:9fc7976c7b27 124 return socket;
sam_grove 11:288c15b80a26 125 }
sam_grove 13:0186e9e35a24 126
sam_grove 24:37504440f296 127 int ESP8266Interface::deallocateSocket(SocketInterface *socket)
bridadan 16:b2f781416464 128 {
sarahmarshy 18:9fc7976c7b27 129 int id = (int)static_cast<ESP8266Socket*>(socket)->getID();
sarahmarshy 18:9fc7976c7b27 130 availableID[id] = -1;
sam_grove 24:37504440f296 131
sarahmarshy 22:312453862371 132 std::map<uint32_t, SocketInterface*>::iterator it;
sam_grove 24:37504440f296 133
sarahmarshy 22:312453862371 134 // Check if socket is owned by WiFiRadioInterface
sarahmarshy 22:312453862371 135 it = sockets.find(socket->getHandle());
sam_grove 24:37504440f296 136
sarahmarshy 22:312453862371 137 if (it != sockets.end()) {
sarahmarshy 22:312453862371 138 // If so, erase it from the internal socket map and deallocate the socket
sarahmarshy 22:312453862371 139 sockets.erase(it);
sarahmarshy 22:312453862371 140 delete socket;
sarahmarshy 22:312453862371 141 } else {
sarahmarshy 22:312453862371 142 // Socket is not owned by WiFiRadioInterface, so return -1 error
sarahmarshy 22:312453862371 143 return -1;
sarahmarshy 22:312453862371 144 }
sam_grove 24:37504440f296 145 return 0;
bridadan 16:b2f781416464 146 }
sarahmarshy 25:91c4e9d34b77 147 void ESP8266Interface::getHostByName(const char *name, char* hostIP)
sarahmarshy 25:91c4e9d34b77 148 {
sarahmarshy 25:91c4e9d34b77 149 SocketInterface* sock = this->allocateSocket(SOCK_UDP);
ohneta 30:fd77d067f5ab 150 DnsQuery dns(sock, name, hostIP, this->preferred_dnsip);
sarahmarshy 25:91c4e9d34b77 151 this->deallocateSocket(sock);
sarahmarshy 25:91c4e9d34b77 152 }
bridadan 16:b2f781416464 153
ohneta 30:fd77d067f5ab 154 void ESP8266Interface::setPreferredDns(const char *preferredDnsIP)
ohneta 30:fd77d067f5ab 155 {
ohneta 30:fd77d067f5ab 156 this->preferred_dnsip = preferredDnsIP;
ohneta 30:fd77d067f5ab 157 }
ohneta 30:fd77d067f5ab 158
sam_grove 24:37504440f296 159 ESP8266Socket::ESP8266Socket(uint32_t handle, ESP8266 &driver, socket_protocol_t type, uint8_t id)
sarahmarshy 18:9fc7976c7b27 160 {
sarahmarshy 22:312453862371 161 _handle = handle;
sam_grove 24:37504440f296 162 _driver = &driver;
sam_grove 24:37504440f296 163 _type = type;
sam_grove 24:37504440f296 164 _id = id;
sarahmarshy 18:9fc7976c7b27 165 }
sarahmarshy 18:9fc7976c7b27 166
sarahmarshy 18:9fc7976c7b27 167 const char *ESP8266Socket::getHostByName(const char *name) const
sam_grove 13:0186e9e35a24 168 {
sam_grove 13:0186e9e35a24 169 return 0;
sam_grove 13:0186e9e35a24 170 }
sam_grove 13:0186e9e35a24 171
sarahmarshy 18:9fc7976c7b27 172 void ESP8266Socket::setAddress(const char* addr)
bridadan 16:b2f781416464 173 {
sam_grove 24:37504440f296 174 _addr = addr;
sarahmarshy 18:9fc7976c7b27 175 }
sarahmarshy 18:9fc7976c7b27 176
sam_grove 24:37504440f296 177 void ESP8266Socket::setPort(uint16_t port)
sarahmarshy 18:9fc7976c7b27 178 {
sam_grove 24:37504440f296 179 _port = port;
bridadan 16:b2f781416464 180 }
bridadan 16:b2f781416464 181
sarahmarshy 18:9fc7976c7b27 182 void ESP8266Socket::setAddressPort(const char* addr, uint16_t port)
bridadan 16:b2f781416464 183 {
sam_grove 24:37504440f296 184 _addr = addr;
sarahmarshy 18:9fc7976c7b27 185 _port = port;
bridadan 16:b2f781416464 186 }
bridadan 16:b2f781416464 187
sarahmarshy 18:9fc7976c7b27 188 const char *ESP8266Socket::getAddress(void) const
sarahmarshy 18:9fc7976c7b27 189 {
sam_grove 24:37504440f296 190 return _addr;
sarahmarshy 18:9fc7976c7b27 191 }
sarahmarshy 18:9fc7976c7b27 192
sarahmarshy 18:9fc7976c7b27 193 uint16_t ESP8266Socket::getPort(void) const
sarahmarshy 18:9fc7976c7b27 194 {
sarahmarshy 18:9fc7976c7b27 195 return _port;
sarahmarshy 18:9fc7976c7b27 196 }
sarahmarshy 18:9fc7976c7b27 197
sarahmarshy 18:9fc7976c7b27 198
sarahmarshy 18:9fc7976c7b27 199 int32_t ESP8266Socket::bind(uint16_t port) const
sam_grove 13:0186e9e35a24 200 {
sam_grove 13:0186e9e35a24 201 return -1;
sam_grove 13:0186e9e35a24 202 }
sam_grove 13:0186e9e35a24 203
sarahmarshy 18:9fc7976c7b27 204 int32_t ESP8266Socket::listen(void) const
sam_grove 13:0186e9e35a24 205 {
sam_grove 13:0186e9e35a24 206 return -1;
sam_grove 13:0186e9e35a24 207 }
sam_grove 13:0186e9e35a24 208
sarahmarshy 18:9fc7976c7b27 209 int32_t ESP8266Socket::accept() const
sam_grove 13:0186e9e35a24 210 {
sam_grove 13:0186e9e35a24 211 return -1;
sam_grove 13:0186e9e35a24 212 }
sam_grove 13:0186e9e35a24 213
sam_grove 24:37504440f296 214 int32_t ESP8266Socket::open()
sam_grove 13:0186e9e35a24 215 {
sarahmarshy 26:6e36dd3cec3f 216
sam_grove 24:37504440f296 217 string sock_type = (SOCK_UDP == _type) ? "UDP" : "TCP";
sam_grove 24:37504440f296 218 if (!_driver->openSocket(sock_type, _id, _port, _addr)) {
sam_grove 24:37504440f296 219 return -1;
sarahmarshy 18:9fc7976c7b27 220 }
sam_grove 13:0186e9e35a24 221 return 0;
sam_grove 24:37504440f296 222
sam_grove 13:0186e9e35a24 223 }
sam_grove 13:0186e9e35a24 224
sam_grove 24:37504440f296 225 int32_t ESP8266Socket::send(const void *data, uint32_t amount, uint32_t timeout_ms)
bridadan 16:b2f781416464 226 {
sam_grove 24:37504440f296 227 _driver->setTimeout((int)timeout_ms);
sam_grove 24:37504440f296 228 if(!_driver->sendData(_id, data, amount)) {
sarahmarshy 18:9fc7976c7b27 229 return -1;
sarahmarshy 18:9fc7976c7b27 230 }
bridadan 16:b2f781416464 231 return 0;
bridadan 16:b2f781416464 232 }
bridadan 16:b2f781416464 233
sarahmarshy 18:9fc7976c7b27 234 uint32_t ESP8266Socket::recv(void *data, uint32_t amount, uint32_t timeout_ms)
sam_grove 13:0186e9e35a24 235 {
sarahmarshy 27:eaeecaaae611 236 _driver->setTimeout((int)timeout_ms);
sam_grove 24:37504440f296 237 return _driver->recv(data, amount);
sam_grove 13:0186e9e35a24 238 }
sam_grove 13:0186e9e35a24 239
sarahmarshy 18:9fc7976c7b27 240 int32_t ESP8266Socket::close() const
sam_grove 13:0186e9e35a24 241 {
sam_grove 24:37504440f296 242 if (!_driver->close(_id)) {
sarahmarshy 23:fd0f3197c30b 243 return -1;//closing socket not succesful
sarahmarshy 18:9fc7976c7b27 244 }
sam_grove 13:0186e9e35a24 245 return 0;
sam_grove 13:0186e9e35a24 246 }
sarahmarshy 22:312453862371 247
sarahmarshy 22:312453862371 248 uint32_t ESP8266Socket::getHandle()const
sarahmarshy 22:312453862371 249 {
sarahmarshy 22:312453862371 250 return _handle;
sarahmarshy 22:312453862371 251 }
sarahmarshy 22:312453862371 252
sarahmarshy 18:9fc7976c7b27 253 uint8_t ESP8266Socket::getID()
sam_grove 13:0186e9e35a24 254 {
sam_grove 24:37504440f296 255 return _id;
sarahmarshy 25:91c4e9d34b77 256 }