modified by ohneta

Dependencies:   ESP8266

Dependents:   HelloESP8266Interface_mine

Fork of ESP8266Interface by NetworkSocketAPI

Committer:
ohneta
Date:
Tue Nov 10 14:46:06 2015 +0000
Revision:
30:fd77d067f5ab
Parent:
29:f2ae8f47729b
Child:
32:1d16668d116c
for AT_V0.50/SDK_V1.4.0 ; modified to work normally

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