Generic Pelion Device Management example for various Advantech modules.

This example is known to work great on the following platforms:

Example Functionality

This example showcases the following device functionality:

  • On timer button increment, simulate Pelion LWM2M button resource change

Use this example with Mbed CLI

1. Import the application into your desktop:

mbed import https://os.mbed.com/teams/Advantech/code/pelion-example-common
cd pelion-example-common

2. Download your developer certificate from pelion portal

3. Compile the program

mbed compile -t <toolchain> -m <TARGET_BOARD>

(supported toolchains : GCC_ARM / ARM / IAR)

4. Copy the binary file pelion-example-common.bin to your mbed device.

Committer:
chuanga
Date:
Tue Mar 12 13:48:39 2019 +0800
Revision:
0:43ff9e3bc244
copying sources from github repository

Who changed what in which revision?

UserRevisionLine numberNew contents of line
chuanga 0:43ff9e3bc244 1 /* ESP32 Example
chuanga 0:43ff9e3bc244 2 * Copyright (c) 2015 ARM Limited
chuanga 0:43ff9e3bc244 3 * Copyright (c) 2017 Renesas Electronics Corporation
chuanga 0:43ff9e3bc244 4 *
chuanga 0:43ff9e3bc244 5 * Licensed under the Apache License, Version 2.0 (the "License");
chuanga 0:43ff9e3bc244 6 * you may not use this file except in compliance with the License.
chuanga 0:43ff9e3bc244 7 * You may obtain a copy of the License at
chuanga 0:43ff9e3bc244 8 *
chuanga 0:43ff9e3bc244 9 * http://www.apache.org/licenses/LICENSE-2.0
chuanga 0:43ff9e3bc244 10 *
chuanga 0:43ff9e3bc244 11 * Unless required by applicable law or agreed to in writing, software
chuanga 0:43ff9e3bc244 12 * distributed under the License is distributed on an "AS IS" BASIS,
chuanga 0:43ff9e3bc244 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
chuanga 0:43ff9e3bc244 14 * See the License for the specific language governing permissions and
chuanga 0:43ff9e3bc244 15 * limitations under the License.
chuanga 0:43ff9e3bc244 16 */
chuanga 0:43ff9e3bc244 17
chuanga 0:43ff9e3bc244 18 #if DEVICE_SERIAL && defined(MBED_CONF_EVENTS_PRESENT) && defined(MBED_CONF_NSAPI_PRESENT) && defined(MBED_CONF_RTOS_PRESENT)
chuanga 0:43ff9e3bc244 19 #include "ESP32.h"
chuanga 0:43ff9e3bc244 20
chuanga 0:43ff9e3bc244 21 #define ESP32_DEFAULT_BAUD_RATE 115200
chuanga 0:43ff9e3bc244 22 #define ESP32_ALL_SOCKET_IDS -1
chuanga 0:43ff9e3bc244 23
chuanga 0:43ff9e3bc244 24 using namespace mbed;
chuanga 0:43ff9e3bc244 25 using namespace rtos;
chuanga 0:43ff9e3bc244 26
chuanga 0:43ff9e3bc244 27 ESP32 * ESP32::instESP32 = NULL;
chuanga 0:43ff9e3bc244 28
chuanga 0:43ff9e3bc244 29 ESP32 * ESP32::getESP32Inst(PinName en, PinName io0, PinName tx, PinName rx, bool debug,
chuanga 0:43ff9e3bc244 30 PinName rts, PinName cts, int baudrate)
chuanga 0:43ff9e3bc244 31 {
chuanga 0:43ff9e3bc244 32 if (instESP32 == NULL) {
chuanga 0:43ff9e3bc244 33 instESP32 = new ESP32(en, io0, tx, rx, debug, rts, cts, baudrate);
chuanga 0:43ff9e3bc244 34 } else {
chuanga 0:43ff9e3bc244 35 if (debug) {
chuanga 0:43ff9e3bc244 36 instESP32->debugOn(debug);
chuanga 0:43ff9e3bc244 37 }
chuanga 0:43ff9e3bc244 38 }
chuanga 0:43ff9e3bc244 39 return instESP32;
chuanga 0:43ff9e3bc244 40 }
chuanga 0:43ff9e3bc244 41
chuanga 0:43ff9e3bc244 42 ESP32::ESP32(PinName en, PinName io0, PinName tx, PinName rx, bool debug,
chuanga 0:43ff9e3bc244 43 PinName rts, PinName cts, int baudrate)
chuanga 0:43ff9e3bc244 44 : _p_wifi_en(NULL), _p_wifi_io0(NULL), init_end(false)
chuanga 0:43ff9e3bc244 45 , _serial(tx, rx, ESP32_DEFAULT_BAUD_RATE), _parser(&_serial, "\r\n")
chuanga 0:43ff9e3bc244 46 , _packets(0), _packets_end(&_packets)
chuanga 0:43ff9e3bc244 47 , _id_bits(0), _id_bits_close(0), _server_act(false)
chuanga 0:43ff9e3bc244 48 , _wifi_status(STATUS_DISCONNECTED)
chuanga 0:43ff9e3bc244 49 , _wifi_status_cb(NULL)
chuanga 0:43ff9e3bc244 50 {
chuanga 0:43ff9e3bc244 51 if ((int)en != NC) {
chuanga 0:43ff9e3bc244 52 _p_wifi_en = new DigitalOut(en);
chuanga 0:43ff9e3bc244 53 }
chuanga 0:43ff9e3bc244 54 if ((int)io0 != NC) {
chuanga 0:43ff9e3bc244 55 _p_wifi_io0 = new DigitalOut(io0);
chuanga 0:43ff9e3bc244 56 }
chuanga 0:43ff9e3bc244 57
chuanga 0:43ff9e3bc244 58 _wifi_mode = WIFIMODE_STATION;
chuanga 0:43ff9e3bc244 59 _baudrate = baudrate;
chuanga 0:43ff9e3bc244 60 memset(_ids, 0, sizeof(_ids));
chuanga 0:43ff9e3bc244 61 memset(_cbs, 0, sizeof(_cbs));
chuanga 0:43ff9e3bc244 62
chuanga 0:43ff9e3bc244 63 _rts = rts;
chuanga 0:43ff9e3bc244 64 _cts = cts;
chuanga 0:43ff9e3bc244 65
chuanga 0:43ff9e3bc244 66 if ((_rts != NC) && (_cts != NC)) {
chuanga 0:43ff9e3bc244 67 _flow_control = 3;
chuanga 0:43ff9e3bc244 68 } else if (_rts != NC) {
chuanga 0:43ff9e3bc244 69 _flow_control = 1;
chuanga 0:43ff9e3bc244 70 } else if (_cts != NC) {
chuanga 0:43ff9e3bc244 71 _flow_control = 2;
chuanga 0:43ff9e3bc244 72 } else {
chuanga 0:43ff9e3bc244 73 _flow_control = 0;
chuanga 0:43ff9e3bc244 74 }
chuanga 0:43ff9e3bc244 75
chuanga 0:43ff9e3bc244 76 _serial.set_baud(ESP32_DEFAULT_BAUD_RATE);
chuanga 0:43ff9e3bc244 77 debugOn(debug);
chuanga 0:43ff9e3bc244 78
chuanga 0:43ff9e3bc244 79 _parser.oob("+IPD", callback(this, &ESP32::_packet_handler));
chuanga 0:43ff9e3bc244 80 _parser.oob("0,CONNECT", callback(this, &ESP32::_connect_handler_0));
chuanga 0:43ff9e3bc244 81 _parser.oob("1,CONNECT", callback(this, &ESP32::_connect_handler_1));
chuanga 0:43ff9e3bc244 82 _parser.oob("2,CONNECT", callback(this, &ESP32::_connect_handler_2));
chuanga 0:43ff9e3bc244 83 _parser.oob("3,CONNECT", callback(this, &ESP32::_connect_handler_3));
chuanga 0:43ff9e3bc244 84 _parser.oob("4,CONNECT", callback(this, &ESP32::_connect_handler_4));
chuanga 0:43ff9e3bc244 85 _parser.oob("0,CLOSED", callback(this, &ESP32::_closed_handler_0));
chuanga 0:43ff9e3bc244 86 _parser.oob("1,CLOSED", callback(this, &ESP32::_closed_handler_1));
chuanga 0:43ff9e3bc244 87 _parser.oob("2,CLOSED", callback(this, &ESP32::_closed_handler_2));
chuanga 0:43ff9e3bc244 88 _parser.oob("3,CLOSED", callback(this, &ESP32::_closed_handler_3));
chuanga 0:43ff9e3bc244 89 _parser.oob("4,CLOSED", callback(this, &ESP32::_closed_handler_4));
chuanga 0:43ff9e3bc244 90 _parser.oob("WIFI ", callback(this, &ESP32::_connection_status_handler));
chuanga 0:43ff9e3bc244 91
chuanga 0:43ff9e3bc244 92 _serial.sigio(Callback<void()>(this, &ESP32::event));
chuanga 0:43ff9e3bc244 93
chuanga 0:43ff9e3bc244 94 setTimeout();
chuanga 0:43ff9e3bc244 95 }
chuanga 0:43ff9e3bc244 96
chuanga 0:43ff9e3bc244 97 void ESP32::debugOn(bool debug)
chuanga 0:43ff9e3bc244 98 {
chuanga 0:43ff9e3bc244 99 _parser.debug_on((debug) ? 1 : 0);
chuanga 0:43ff9e3bc244 100 }
chuanga 0:43ff9e3bc244 101
chuanga 0:43ff9e3bc244 102 int ESP32::get_firmware_version()
chuanga 0:43ff9e3bc244 103 {
chuanga 0:43ff9e3bc244 104 int version;
chuanga 0:43ff9e3bc244 105
chuanga 0:43ff9e3bc244 106 _smutex.lock();
chuanga 0:43ff9e3bc244 107 startup();
chuanga 0:43ff9e3bc244 108 bool done = _parser.send("AT+GMR")
chuanga 0:43ff9e3bc244 109 && _parser.recv("SDK version:%d", &version)
chuanga 0:43ff9e3bc244 110 && _parser.recv("OK");
chuanga 0:43ff9e3bc244 111 _smutex.unlock();
chuanga 0:43ff9e3bc244 112
chuanga 0:43ff9e3bc244 113 if(done) {
chuanga 0:43ff9e3bc244 114 return version;
chuanga 0:43ff9e3bc244 115 } else {
chuanga 0:43ff9e3bc244 116 // Older firmware versions do not prefix the version with "SDK version: "
chuanga 0:43ff9e3bc244 117 return -1;
chuanga 0:43ff9e3bc244 118 }
chuanga 0:43ff9e3bc244 119 }
chuanga 0:43ff9e3bc244 120
chuanga 0:43ff9e3bc244 121 bool ESP32::startup()
chuanga 0:43ff9e3bc244 122 {
chuanga 0:43ff9e3bc244 123 if (init_end) {
chuanga 0:43ff9e3bc244 124 return true;
chuanga 0:43ff9e3bc244 125 }
chuanga 0:43ff9e3bc244 126
chuanga 0:43ff9e3bc244 127 if (_p_wifi_io0 != NULL) {
chuanga 0:43ff9e3bc244 128 _p_wifi_io0->write(1);
chuanga 0:43ff9e3bc244 129 }
chuanga 0:43ff9e3bc244 130 if (_p_wifi_en != NULL) {
chuanga 0:43ff9e3bc244 131 _p_wifi_en->write(0);
chuanga 0:43ff9e3bc244 132 ThisThread::sleep_for(10);
chuanga 0:43ff9e3bc244 133 _p_wifi_en->write(1);
chuanga 0:43ff9e3bc244 134 _parser.recv("ready");
chuanga 0:43ff9e3bc244 135 } else {
chuanga 0:43ff9e3bc244 136 setTimeout(100);
chuanga 0:43ff9e3bc244 137 _parser.recv("ready");
chuanga 0:43ff9e3bc244 138 }
chuanga 0:43ff9e3bc244 139
chuanga 0:43ff9e3bc244 140 reset();
chuanga 0:43ff9e3bc244 141 bool success = _parser.send("AT+CWMODE=%d", _wifi_mode)
chuanga 0:43ff9e3bc244 142 && _parser.recv("OK")
chuanga 0:43ff9e3bc244 143 && _parser.send("AT+CIPMUX=1")
chuanga 0:43ff9e3bc244 144 && _parser.recv("OK")
chuanga 0:43ff9e3bc244 145 && _parser.send("AT+CWAUTOCONN=0")
chuanga 0:43ff9e3bc244 146 && _parser.recv("OK")
chuanga 0:43ff9e3bc244 147 && _parser.send("AT+CWQAP")
chuanga 0:43ff9e3bc244 148 && _parser.recv("OK");
chuanga 0:43ff9e3bc244 149 if (success) {
chuanga 0:43ff9e3bc244 150 init_end = true;
chuanga 0:43ff9e3bc244 151 }
chuanga 0:43ff9e3bc244 152
chuanga 0:43ff9e3bc244 153 return success;
chuanga 0:43ff9e3bc244 154 }
chuanga 0:43ff9e3bc244 155
chuanga 0:43ff9e3bc244 156 bool ESP32::restart()
chuanga 0:43ff9e3bc244 157 {
chuanga 0:43ff9e3bc244 158 bool success;
chuanga 0:43ff9e3bc244 159
chuanga 0:43ff9e3bc244 160 _smutex.lock();
chuanga 0:43ff9e3bc244 161 if (!init_end) {
chuanga 0:43ff9e3bc244 162 success = startup();
chuanga 0:43ff9e3bc244 163 } else {
chuanga 0:43ff9e3bc244 164 reset();
chuanga 0:43ff9e3bc244 165 success = _parser.send("AT+CWMODE=%d", _wifi_mode)
chuanga 0:43ff9e3bc244 166 && _parser.recv("OK")
chuanga 0:43ff9e3bc244 167 && _parser.send("AT+CIPMUX=1")
chuanga 0:43ff9e3bc244 168 && _parser.recv("OK");
chuanga 0:43ff9e3bc244 169 }
chuanga 0:43ff9e3bc244 170 _smutex.unlock();
chuanga 0:43ff9e3bc244 171
chuanga 0:43ff9e3bc244 172 return success;
chuanga 0:43ff9e3bc244 173 }
chuanga 0:43ff9e3bc244 174
chuanga 0:43ff9e3bc244 175 bool ESP32::set_mode(int mode)
chuanga 0:43ff9e3bc244 176 {
chuanga 0:43ff9e3bc244 177 //only 3 valid modes
chuanga 0:43ff9e3bc244 178 if (mode < 1 || mode > 3) {
chuanga 0:43ff9e3bc244 179 return false;
chuanga 0:43ff9e3bc244 180 }
chuanga 0:43ff9e3bc244 181 if (_wifi_mode != mode) {
chuanga 0:43ff9e3bc244 182 _wifi_mode = mode;
chuanga 0:43ff9e3bc244 183 return restart();
chuanga 0:43ff9e3bc244 184 }
chuanga 0:43ff9e3bc244 185 return true;
chuanga 0:43ff9e3bc244 186 }
chuanga 0:43ff9e3bc244 187
chuanga 0:43ff9e3bc244 188 bool ESP32::cre_server(int port)
chuanga 0:43ff9e3bc244 189 {
chuanga 0:43ff9e3bc244 190 if (_server_act) {
chuanga 0:43ff9e3bc244 191 return false;
chuanga 0:43ff9e3bc244 192 }
chuanga 0:43ff9e3bc244 193 _smutex.lock();
chuanga 0:43ff9e3bc244 194 startup();
chuanga 0:43ff9e3bc244 195 if (!(_parser.send("AT+CIPSERVER=1,%d", port)
chuanga 0:43ff9e3bc244 196 && _parser.recv("OK"))) {
chuanga 0:43ff9e3bc244 197 _smutex.unlock();
chuanga 0:43ff9e3bc244 198 return false;
chuanga 0:43ff9e3bc244 199 }
chuanga 0:43ff9e3bc244 200 _server_act = true;
chuanga 0:43ff9e3bc244 201 _smutex.unlock();
chuanga 0:43ff9e3bc244 202 return true;
chuanga 0:43ff9e3bc244 203 }
chuanga 0:43ff9e3bc244 204
chuanga 0:43ff9e3bc244 205 bool ESP32::del_server()
chuanga 0:43ff9e3bc244 206 {
chuanga 0:43ff9e3bc244 207 _smutex.lock();
chuanga 0:43ff9e3bc244 208 startup();
chuanga 0:43ff9e3bc244 209 if (!(_parser.send("AT+CIPSERVER=0")
chuanga 0:43ff9e3bc244 210 && _parser.recv("OK"))) {
chuanga 0:43ff9e3bc244 211 _smutex.unlock();
chuanga 0:43ff9e3bc244 212 return false;
chuanga 0:43ff9e3bc244 213 }
chuanga 0:43ff9e3bc244 214 _server_act = false;
chuanga 0:43ff9e3bc244 215 _smutex.unlock();
chuanga 0:43ff9e3bc244 216 return true;
chuanga 0:43ff9e3bc244 217 }
chuanga 0:43ff9e3bc244 218
chuanga 0:43ff9e3bc244 219 void ESP32::socket_handler(bool connect, int id)
chuanga 0:43ff9e3bc244 220 {
chuanga 0:43ff9e3bc244 221 _cbs[id].Notified = 0;
chuanga 0:43ff9e3bc244 222 if (connect) {
chuanga 0:43ff9e3bc244 223 _id_bits |= (1 << id);
chuanga 0:43ff9e3bc244 224 if (_server_act) {
chuanga 0:43ff9e3bc244 225 _accept_id.push_back(id);
chuanga 0:43ff9e3bc244 226 }
chuanga 0:43ff9e3bc244 227 } else {
chuanga 0:43ff9e3bc244 228 _id_bits &= ~(1 << id);
chuanga 0:43ff9e3bc244 229 _id_bits_close |= (1 << id);
chuanga 0:43ff9e3bc244 230 if (_server_act) {
chuanga 0:43ff9e3bc244 231 for (size_t i = 0; i < _accept_id.size(); i++) {
chuanga 0:43ff9e3bc244 232 if (id == _accept_id[i]) {
chuanga 0:43ff9e3bc244 233 _accept_id.erase(_accept_id.begin() + i);
chuanga 0:43ff9e3bc244 234 }
chuanga 0:43ff9e3bc244 235 }
chuanga 0:43ff9e3bc244 236 }
chuanga 0:43ff9e3bc244 237 }
chuanga 0:43ff9e3bc244 238 }
chuanga 0:43ff9e3bc244 239
chuanga 0:43ff9e3bc244 240 bool ESP32::accept(int * p_id)
chuanga 0:43ff9e3bc244 241 {
chuanga 0:43ff9e3bc244 242 bool ret = false;
chuanga 0:43ff9e3bc244 243
chuanga 0:43ff9e3bc244 244 while (!ret) {
chuanga 0:43ff9e3bc244 245 if (!_server_act) {
chuanga 0:43ff9e3bc244 246 break;
chuanga 0:43ff9e3bc244 247 }
chuanga 0:43ff9e3bc244 248
chuanga 0:43ff9e3bc244 249 _smutex.lock();
chuanga 0:43ff9e3bc244 250 startup();
chuanga 0:43ff9e3bc244 251 if (!_accept_id.empty()) {
chuanga 0:43ff9e3bc244 252 ret = true;
chuanga 0:43ff9e3bc244 253 } else {
chuanga 0:43ff9e3bc244 254 _parser.process_oob(); // Poll for inbound packets
chuanga 0:43ff9e3bc244 255 if (!_accept_id.empty()) {
chuanga 0:43ff9e3bc244 256 ret = true;
chuanga 0:43ff9e3bc244 257 }
chuanga 0:43ff9e3bc244 258 }
chuanga 0:43ff9e3bc244 259 if (ret) {
chuanga 0:43ff9e3bc244 260 *p_id = _accept_id[0];
chuanga 0:43ff9e3bc244 261 _accept_id.erase(_accept_id.begin());
chuanga 0:43ff9e3bc244 262 }
chuanga 0:43ff9e3bc244 263 _smutex.unlock();
chuanga 0:43ff9e3bc244 264 if (!ret) {
chuanga 0:43ff9e3bc244 265 ThisThread::sleep_for(5);
chuanga 0:43ff9e3bc244 266 }
chuanga 0:43ff9e3bc244 267 }
chuanga 0:43ff9e3bc244 268
chuanga 0:43ff9e3bc244 269 if (ret) {
chuanga 0:43ff9e3bc244 270 for (int i = 0; i < 50; i++) {
chuanga 0:43ff9e3bc244 271 if ((_id_bits_close & (1 << *p_id)) == 0) {
chuanga 0:43ff9e3bc244 272 break;
chuanga 0:43ff9e3bc244 273 }
chuanga 0:43ff9e3bc244 274 ThisThread::sleep_for(10);
chuanga 0:43ff9e3bc244 275 }
chuanga 0:43ff9e3bc244 276 }
chuanga 0:43ff9e3bc244 277
chuanga 0:43ff9e3bc244 278 return ret;
chuanga 0:43ff9e3bc244 279 }
chuanga 0:43ff9e3bc244 280
chuanga 0:43ff9e3bc244 281 bool ESP32::reset(void)
chuanga 0:43ff9e3bc244 282 {
chuanga 0:43ff9e3bc244 283 for (int i = 0; i < 2; i++) {
chuanga 0:43ff9e3bc244 284 if (_parser.send("AT+RST")
chuanga 0:43ff9e3bc244 285 && _parser.recv("OK")) {
chuanga 0:43ff9e3bc244 286 _serial.set_baud(ESP32_DEFAULT_BAUD_RATE);
chuanga 0:43ff9e3bc244 287 #if DEVICE_SERIAL_FC
chuanga 0:43ff9e3bc244 288 _serial.set_flow_control(SerialBase::Disabled);
chuanga 0:43ff9e3bc244 289 #endif
chuanga 0:43ff9e3bc244 290 _parser.recv("ready");
chuanga 0:43ff9e3bc244 291 _clear_socket_packets(ESP32_ALL_SOCKET_IDS);
chuanga 0:43ff9e3bc244 292
chuanga 0:43ff9e3bc244 293 if (_parser.send("AT+UART_CUR=%d,8,1,0,%d", _baudrate, _flow_control)
chuanga 0:43ff9e3bc244 294 && _parser.recv("OK")) {
chuanga 0:43ff9e3bc244 295 _serial.set_baud(_baudrate);
chuanga 0:43ff9e3bc244 296 #if DEVICE_SERIAL_FC
chuanga 0:43ff9e3bc244 297 switch (_flow_control) {
chuanga 0:43ff9e3bc244 298 case 1:
chuanga 0:43ff9e3bc244 299 _serial.set_flow_control(SerialBase::RTS, _rts);
chuanga 0:43ff9e3bc244 300 break;
chuanga 0:43ff9e3bc244 301 case 2:
chuanga 0:43ff9e3bc244 302 _serial.set_flow_control(SerialBase::CTS, _cts);
chuanga 0:43ff9e3bc244 303 break;
chuanga 0:43ff9e3bc244 304 case 3:
chuanga 0:43ff9e3bc244 305 _serial.set_flow_control(SerialBase::RTSCTS, _rts, _cts);
chuanga 0:43ff9e3bc244 306 break;
chuanga 0:43ff9e3bc244 307 case 0:
chuanga 0:43ff9e3bc244 308 default:
chuanga 0:43ff9e3bc244 309 // do nothing
chuanga 0:43ff9e3bc244 310 break;
chuanga 0:43ff9e3bc244 311 }
chuanga 0:43ff9e3bc244 312 #endif
chuanga 0:43ff9e3bc244 313 }
chuanga 0:43ff9e3bc244 314
chuanga 0:43ff9e3bc244 315 return true;
chuanga 0:43ff9e3bc244 316 }
chuanga 0:43ff9e3bc244 317 }
chuanga 0:43ff9e3bc244 318
chuanga 0:43ff9e3bc244 319 return false;
chuanga 0:43ff9e3bc244 320 }
chuanga 0:43ff9e3bc244 321
chuanga 0:43ff9e3bc244 322 bool ESP32::dhcp(bool enabled, int mode)
chuanga 0:43ff9e3bc244 323 {
chuanga 0:43ff9e3bc244 324 //only 3 valid modes
chuanga 0:43ff9e3bc244 325 if (mode < 0 || mode > 2) {
chuanga 0:43ff9e3bc244 326 return false;
chuanga 0:43ff9e3bc244 327 }
chuanga 0:43ff9e3bc244 328
chuanga 0:43ff9e3bc244 329 _smutex.lock();
chuanga 0:43ff9e3bc244 330 startup();
chuanga 0:43ff9e3bc244 331 bool done = _parser.send("AT+CWDHCP=%d,%d", enabled?1:0, mode)
chuanga 0:43ff9e3bc244 332 && _parser.recv("OK");
chuanga 0:43ff9e3bc244 333 _smutex.unlock();
chuanga 0:43ff9e3bc244 334
chuanga 0:43ff9e3bc244 335 return done;
chuanga 0:43ff9e3bc244 336 }
chuanga 0:43ff9e3bc244 337
chuanga 0:43ff9e3bc244 338 bool ESP32::connect(const char *ap, const char *passPhrase)
chuanga 0:43ff9e3bc244 339 {
chuanga 0:43ff9e3bc244 340 bool ret;
chuanga 0:43ff9e3bc244 341
chuanga 0:43ff9e3bc244 342 _wifi_status = STATUS_DISCONNECTED;
chuanga 0:43ff9e3bc244 343
chuanga 0:43ff9e3bc244 344 _smutex.lock();
chuanga 0:43ff9e3bc244 345 startup();
chuanga 0:43ff9e3bc244 346
chuanga 0:43ff9e3bc244 347 setTimeout(ESP32_CONNECT_TIMEOUT);
chuanga 0:43ff9e3bc244 348 ret = _parser.send("AT+CWJAP=\"%s\",\"%s\"", ap, passPhrase)
chuanga 0:43ff9e3bc244 349 && _parser.recv("OK");
chuanga 0:43ff9e3bc244 350 setTimeout();
chuanga 0:43ff9e3bc244 351 _smutex.unlock();
chuanga 0:43ff9e3bc244 352 return ret;
chuanga 0:43ff9e3bc244 353 }
chuanga 0:43ff9e3bc244 354
chuanga 0:43ff9e3bc244 355 bool ESP32::config_soft_ap(const char *ap, const char *passPhrase, uint8_t chl, uint8_t ecn)
chuanga 0:43ff9e3bc244 356 {
chuanga 0:43ff9e3bc244 357 bool ret;
chuanga 0:43ff9e3bc244 358
chuanga 0:43ff9e3bc244 359 _smutex.lock();
chuanga 0:43ff9e3bc244 360 startup();
chuanga 0:43ff9e3bc244 361 ret = _parser.send("AT+CWSAP=\"%s\",\"%s\",%hhu,%hhu", ap, passPhrase, chl, ecn)
chuanga 0:43ff9e3bc244 362 && _parser.recv("OK");
chuanga 0:43ff9e3bc244 363 _smutex.unlock();
chuanga 0:43ff9e3bc244 364 return ret;
chuanga 0:43ff9e3bc244 365 }
chuanga 0:43ff9e3bc244 366
chuanga 0:43ff9e3bc244 367 bool ESP32::get_ssid(char *ap)
chuanga 0:43ff9e3bc244 368 {
chuanga 0:43ff9e3bc244 369 bool ret;
chuanga 0:43ff9e3bc244 370
chuanga 0:43ff9e3bc244 371 _smutex.lock();
chuanga 0:43ff9e3bc244 372 startup();
chuanga 0:43ff9e3bc244 373 ret = _parser.send("AT+CWJAP?")
chuanga 0:43ff9e3bc244 374 && _parser.recv("+CWJAP:\"%33[^\"]\",", ap)
chuanga 0:43ff9e3bc244 375 && _parser.recv("OK");
chuanga 0:43ff9e3bc244 376 _smutex.unlock();
chuanga 0:43ff9e3bc244 377 return ret;
chuanga 0:43ff9e3bc244 378 }
chuanga 0:43ff9e3bc244 379
chuanga 0:43ff9e3bc244 380 bool ESP32::disconnect(void)
chuanga 0:43ff9e3bc244 381 {
chuanga 0:43ff9e3bc244 382 bool ret;
chuanga 0:43ff9e3bc244 383
chuanga 0:43ff9e3bc244 384 _smutex.lock();
chuanga 0:43ff9e3bc244 385 startup();
chuanga 0:43ff9e3bc244 386 ret = _parser.send("AT+CWQAP") && _parser.recv("OK");
chuanga 0:43ff9e3bc244 387 _smutex.unlock();
chuanga 0:43ff9e3bc244 388 return ret;
chuanga 0:43ff9e3bc244 389 }
chuanga 0:43ff9e3bc244 390
chuanga 0:43ff9e3bc244 391 const char *ESP32::getIPAddress(void)
chuanga 0:43ff9e3bc244 392 {
chuanga 0:43ff9e3bc244 393 bool ret;
chuanga 0:43ff9e3bc244 394
chuanga 0:43ff9e3bc244 395 _smutex.lock();
chuanga 0:43ff9e3bc244 396 startup();
chuanga 0:43ff9e3bc244 397 ret = _parser.send("AT+CIFSR")
chuanga 0:43ff9e3bc244 398 && _parser.recv("+CIFSR:STAIP,\"%15[^\"]\"", _ip_buffer)
chuanga 0:43ff9e3bc244 399 && _parser.recv("OK");
chuanga 0:43ff9e3bc244 400 _smutex.unlock();
chuanga 0:43ff9e3bc244 401 if (!ret) {
chuanga 0:43ff9e3bc244 402 return 0;
chuanga 0:43ff9e3bc244 403 }
chuanga 0:43ff9e3bc244 404 return _ip_buffer;
chuanga 0:43ff9e3bc244 405 }
chuanga 0:43ff9e3bc244 406
chuanga 0:43ff9e3bc244 407 const char *ESP32::getIPAddress_ap(void)
chuanga 0:43ff9e3bc244 408 {
chuanga 0:43ff9e3bc244 409 bool ret;
chuanga 0:43ff9e3bc244 410
chuanga 0:43ff9e3bc244 411 _smutex.lock();
chuanga 0:43ff9e3bc244 412 startup();
chuanga 0:43ff9e3bc244 413 ret = _parser.send("AT+CIFSR")
chuanga 0:43ff9e3bc244 414 && _parser.recv("+CIFSR:APIP,\"%15[^\"]\"", _ip_buffer_ap)
chuanga 0:43ff9e3bc244 415 && _parser.recv("OK");
chuanga 0:43ff9e3bc244 416 _smutex.unlock();
chuanga 0:43ff9e3bc244 417 if (!ret) {
chuanga 0:43ff9e3bc244 418 return 0;
chuanga 0:43ff9e3bc244 419 }
chuanga 0:43ff9e3bc244 420 return _ip_buffer_ap;
chuanga 0:43ff9e3bc244 421 }
chuanga 0:43ff9e3bc244 422
chuanga 0:43ff9e3bc244 423 const char *ESP32::getMACAddress(void)
chuanga 0:43ff9e3bc244 424 {
chuanga 0:43ff9e3bc244 425 bool ret;
chuanga 0:43ff9e3bc244 426
chuanga 0:43ff9e3bc244 427 _smutex.lock();
chuanga 0:43ff9e3bc244 428 startup();
chuanga 0:43ff9e3bc244 429 ret = _parser.send("AT+CIFSR")
chuanga 0:43ff9e3bc244 430 && _parser.recv("+CIFSR:STAMAC,\"%17[^\"]\"", _mac_buffer)
chuanga 0:43ff9e3bc244 431 && _parser.recv("OK");
chuanga 0:43ff9e3bc244 432 _smutex.unlock();
chuanga 0:43ff9e3bc244 433
chuanga 0:43ff9e3bc244 434 if (!ret) {
chuanga 0:43ff9e3bc244 435 return 0;
chuanga 0:43ff9e3bc244 436 }
chuanga 0:43ff9e3bc244 437 return _mac_buffer;
chuanga 0:43ff9e3bc244 438 }
chuanga 0:43ff9e3bc244 439
chuanga 0:43ff9e3bc244 440 const char *ESP32::getMACAddress_ap(void)
chuanga 0:43ff9e3bc244 441 {
chuanga 0:43ff9e3bc244 442 bool ret;
chuanga 0:43ff9e3bc244 443
chuanga 0:43ff9e3bc244 444 _smutex.lock();
chuanga 0:43ff9e3bc244 445 startup();
chuanga 0:43ff9e3bc244 446 ret = _parser.send("AT+CIFSR")
chuanga 0:43ff9e3bc244 447 && _parser.recv("+CIFSR:APMAC,\"%17[^\"]\"", _mac_buffer_ap)
chuanga 0:43ff9e3bc244 448 && _parser.recv("OK");
chuanga 0:43ff9e3bc244 449 _smutex.unlock();
chuanga 0:43ff9e3bc244 450
chuanga 0:43ff9e3bc244 451 if (!ret) {
chuanga 0:43ff9e3bc244 452 return 0;
chuanga 0:43ff9e3bc244 453 }
chuanga 0:43ff9e3bc244 454 return _mac_buffer_ap;
chuanga 0:43ff9e3bc244 455 }
chuanga 0:43ff9e3bc244 456
chuanga 0:43ff9e3bc244 457 const char *ESP32::getGateway()
chuanga 0:43ff9e3bc244 458 {
chuanga 0:43ff9e3bc244 459 bool ret;
chuanga 0:43ff9e3bc244 460
chuanga 0:43ff9e3bc244 461 _smutex.lock();
chuanga 0:43ff9e3bc244 462 startup();
chuanga 0:43ff9e3bc244 463 ret = _parser.send("AT+CIPSTA?")
chuanga 0:43ff9e3bc244 464 && _parser.recv("+CIPSTA:gateway:\"%15[^\"]\"", _gateway_buffer)
chuanga 0:43ff9e3bc244 465 && _parser.recv("OK");
chuanga 0:43ff9e3bc244 466 _smutex.unlock();
chuanga 0:43ff9e3bc244 467
chuanga 0:43ff9e3bc244 468 if (!ret) {
chuanga 0:43ff9e3bc244 469 return 0;
chuanga 0:43ff9e3bc244 470 }
chuanga 0:43ff9e3bc244 471 return _gateway_buffer;
chuanga 0:43ff9e3bc244 472 }
chuanga 0:43ff9e3bc244 473
chuanga 0:43ff9e3bc244 474 const char *ESP32::getGateway_ap()
chuanga 0:43ff9e3bc244 475 {
chuanga 0:43ff9e3bc244 476 bool ret;
chuanga 0:43ff9e3bc244 477
chuanga 0:43ff9e3bc244 478 _smutex.lock();
chuanga 0:43ff9e3bc244 479 startup();
chuanga 0:43ff9e3bc244 480 ret = _parser.send("AT+CIPAP?")
chuanga 0:43ff9e3bc244 481 && _parser.recv("+CIPAP:gateway:\"%15[^\"]\"", _gateway_buffer_ap)
chuanga 0:43ff9e3bc244 482 && _parser.recv("OK");
chuanga 0:43ff9e3bc244 483 _smutex.unlock();
chuanga 0:43ff9e3bc244 484
chuanga 0:43ff9e3bc244 485 if (!ret) {
chuanga 0:43ff9e3bc244 486 return 0;
chuanga 0:43ff9e3bc244 487 }
chuanga 0:43ff9e3bc244 488 return _gateway_buffer_ap;
chuanga 0:43ff9e3bc244 489 }
chuanga 0:43ff9e3bc244 490
chuanga 0:43ff9e3bc244 491 const char *ESP32::getNetmask()
chuanga 0:43ff9e3bc244 492 {
chuanga 0:43ff9e3bc244 493 bool ret;
chuanga 0:43ff9e3bc244 494
chuanga 0:43ff9e3bc244 495 _smutex.lock();
chuanga 0:43ff9e3bc244 496 startup();
chuanga 0:43ff9e3bc244 497 ret = _parser.send("AT+CIPSTA?")
chuanga 0:43ff9e3bc244 498 && _parser.recv("+CIPSTA:netmask:\"%15[^\"]\"", _netmask_buffer)
chuanga 0:43ff9e3bc244 499 && _parser.recv("OK");
chuanga 0:43ff9e3bc244 500 _smutex.unlock();
chuanga 0:43ff9e3bc244 501
chuanga 0:43ff9e3bc244 502 if (!ret) {
chuanga 0:43ff9e3bc244 503 return 0;
chuanga 0:43ff9e3bc244 504 }
chuanga 0:43ff9e3bc244 505 return _netmask_buffer;
chuanga 0:43ff9e3bc244 506 }
chuanga 0:43ff9e3bc244 507
chuanga 0:43ff9e3bc244 508 const char *ESP32::getNetmask_ap()
chuanga 0:43ff9e3bc244 509 {
chuanga 0:43ff9e3bc244 510 bool ret;
chuanga 0:43ff9e3bc244 511
chuanga 0:43ff9e3bc244 512 _smutex.lock();
chuanga 0:43ff9e3bc244 513 startup();
chuanga 0:43ff9e3bc244 514 ret = _parser.send("AT+CIPAP?")
chuanga 0:43ff9e3bc244 515 && _parser.recv("+CIPAP:netmask:\"%15[^\"]\"", _netmask_buffer_ap)
chuanga 0:43ff9e3bc244 516 && _parser.recv("OK");
chuanga 0:43ff9e3bc244 517 _smutex.unlock();
chuanga 0:43ff9e3bc244 518
chuanga 0:43ff9e3bc244 519 if (!ret) {
chuanga 0:43ff9e3bc244 520 return 0;
chuanga 0:43ff9e3bc244 521 }
chuanga 0:43ff9e3bc244 522 return _netmask_buffer_ap;
chuanga 0:43ff9e3bc244 523 }
chuanga 0:43ff9e3bc244 524
chuanga 0:43ff9e3bc244 525 int8_t ESP32::getRSSI()
chuanga 0:43ff9e3bc244 526 {
chuanga 0:43ff9e3bc244 527 bool ret;
chuanga 0:43ff9e3bc244 528 int8_t rssi;
chuanga 0:43ff9e3bc244 529 char ssid[33];
chuanga 0:43ff9e3bc244 530 char bssid[18];
chuanga 0:43ff9e3bc244 531
chuanga 0:43ff9e3bc244 532 _smutex.lock();
chuanga 0:43ff9e3bc244 533 startup();
chuanga 0:43ff9e3bc244 534 ret = _parser.send("AT+CWJAP?")
chuanga 0:43ff9e3bc244 535 && _parser.recv("+CWJAP:\"%32[^\"]\",\"%17[^\"]\"", ssid, bssid)
chuanga 0:43ff9e3bc244 536 && _parser.recv("OK");
chuanga 0:43ff9e3bc244 537 if (!ret) {
chuanga 0:43ff9e3bc244 538 _smutex.unlock();
chuanga 0:43ff9e3bc244 539 return 0;
chuanga 0:43ff9e3bc244 540 }
chuanga 0:43ff9e3bc244 541
chuanga 0:43ff9e3bc244 542 ret = _parser.send("AT+CWLAP=\"%s\",\"%s\"", ssid, bssid)
chuanga 0:43ff9e3bc244 543 && _parser.recv("+CWLAP:(%*d,\"%*[^\"]\",%hhd,", &rssi)
chuanga 0:43ff9e3bc244 544 && _parser.recv("OK");
chuanga 0:43ff9e3bc244 545 _smutex.unlock();
chuanga 0:43ff9e3bc244 546
chuanga 0:43ff9e3bc244 547 if (!ret) {
chuanga 0:43ff9e3bc244 548 return 0;
chuanga 0:43ff9e3bc244 549 }
chuanga 0:43ff9e3bc244 550
chuanga 0:43ff9e3bc244 551 return rssi;
chuanga 0:43ff9e3bc244 552 }
chuanga 0:43ff9e3bc244 553
chuanga 0:43ff9e3bc244 554 int ESP32::scan(WiFiAccessPoint *res, unsigned limit)
chuanga 0:43ff9e3bc244 555 {
chuanga 0:43ff9e3bc244 556 unsigned cnt = 0;
chuanga 0:43ff9e3bc244 557 nsapi_wifi_ap_t ap;
chuanga 0:43ff9e3bc244 558
chuanga 0:43ff9e3bc244 559 if (!init_end) {
chuanga 0:43ff9e3bc244 560 _smutex.lock();
chuanga 0:43ff9e3bc244 561 startup();
chuanga 0:43ff9e3bc244 562 _smutex.unlock();
chuanga 0:43ff9e3bc244 563 ThisThread::sleep_for(1500);
chuanga 0:43ff9e3bc244 564 }
chuanga 0:43ff9e3bc244 565
chuanga 0:43ff9e3bc244 566 _smutex.lock();
chuanga 0:43ff9e3bc244 567 setTimeout(5000);
chuanga 0:43ff9e3bc244 568 if (!_parser.send("AT+CWLAP")) {
chuanga 0:43ff9e3bc244 569 _smutex.unlock();
chuanga 0:43ff9e3bc244 570 return NSAPI_ERROR_DEVICE_ERROR;
chuanga 0:43ff9e3bc244 571 }
chuanga 0:43ff9e3bc244 572
chuanga 0:43ff9e3bc244 573 while (recv_ap(&ap)) {
chuanga 0:43ff9e3bc244 574 if (cnt < limit) {
chuanga 0:43ff9e3bc244 575 res[cnt] = WiFiAccessPoint(ap);
chuanga 0:43ff9e3bc244 576 }
chuanga 0:43ff9e3bc244 577
chuanga 0:43ff9e3bc244 578 cnt++;
chuanga 0:43ff9e3bc244 579 if ((limit != 0) && (cnt >= limit)) {
chuanga 0:43ff9e3bc244 580 break;
chuanga 0:43ff9e3bc244 581 }
chuanga 0:43ff9e3bc244 582 setTimeout(500);
chuanga 0:43ff9e3bc244 583 }
chuanga 0:43ff9e3bc244 584 setTimeout(10);
chuanga 0:43ff9e3bc244 585 _parser.recv("OK");
chuanga 0:43ff9e3bc244 586 setTimeout();
chuanga 0:43ff9e3bc244 587 _smutex.unlock();
chuanga 0:43ff9e3bc244 588
chuanga 0:43ff9e3bc244 589 return cnt;
chuanga 0:43ff9e3bc244 590 }
chuanga 0:43ff9e3bc244 591
chuanga 0:43ff9e3bc244 592 bool ESP32::isConnected(void)
chuanga 0:43ff9e3bc244 593 {
chuanga 0:43ff9e3bc244 594 return getIPAddress() != 0;
chuanga 0:43ff9e3bc244 595 }
chuanga 0:43ff9e3bc244 596
chuanga 0:43ff9e3bc244 597 bool ESP32::open(const char *type, int id, const char* addr, int port, int opt)
chuanga 0:43ff9e3bc244 598 {
chuanga 0:43ff9e3bc244 599 bool ret;
chuanga 0:43ff9e3bc244 600
chuanga 0:43ff9e3bc244 601 if (id >= SOCKET_COUNT) {
chuanga 0:43ff9e3bc244 602 return false;
chuanga 0:43ff9e3bc244 603 }
chuanga 0:43ff9e3bc244 604 _cbs[id].Notified = 0;
chuanga 0:43ff9e3bc244 605
chuanga 0:43ff9e3bc244 606 _smutex.lock();
chuanga 0:43ff9e3bc244 607 startup();
chuanga 0:43ff9e3bc244 608 setTimeout(500);
chuanga 0:43ff9e3bc244 609 if (opt != 0) {
chuanga 0:43ff9e3bc244 610 ret = _parser.send("AT+CIPSTART=%d,\"%s\",\"%s\",%d, %d", id, type, addr, port, opt)
chuanga 0:43ff9e3bc244 611 && _parser.recv("OK");
chuanga 0:43ff9e3bc244 612 } else {
chuanga 0:43ff9e3bc244 613 ret = _parser.send("AT+CIPSTART=%d,\"%s\",\"%s\",%d", id, type, addr, port)
chuanga 0:43ff9e3bc244 614 && _parser.recv("OK");
chuanga 0:43ff9e3bc244 615 }
chuanga 0:43ff9e3bc244 616 setTimeout();
chuanga 0:43ff9e3bc244 617 _clear_socket_packets(id);
chuanga 0:43ff9e3bc244 618 _smutex.unlock();
chuanga 0:43ff9e3bc244 619
chuanga 0:43ff9e3bc244 620 return ret;
chuanga 0:43ff9e3bc244 621 }
chuanga 0:43ff9e3bc244 622
chuanga 0:43ff9e3bc244 623 bool ESP32::send(int id, const void *data, uint32_t amount)
chuanga 0:43ff9e3bc244 624 {
chuanga 0:43ff9e3bc244 625 int send_size;
chuanga 0:43ff9e3bc244 626 bool ret;
chuanga 0:43ff9e3bc244 627 int error_cnt = 0;
chuanga 0:43ff9e3bc244 628 int index = 0;
chuanga 0:43ff9e3bc244 629
chuanga 0:43ff9e3bc244 630 _cbs[id].Notified = 0;
chuanga 0:43ff9e3bc244 631 if (amount == 0) {
chuanga 0:43ff9e3bc244 632 return true;
chuanga 0:43ff9e3bc244 633 }
chuanga 0:43ff9e3bc244 634
chuanga 0:43ff9e3bc244 635 //May take a second try if device is busy
chuanga 0:43ff9e3bc244 636 _smutex.lock();
chuanga 0:43ff9e3bc244 637 while (error_cnt < 2) {
chuanga 0:43ff9e3bc244 638 if (((_id_bits & (1 << id)) == 0)
chuanga 0:43ff9e3bc244 639 || ((_id_bits_close & (1 << id)) != 0)) {
chuanga 0:43ff9e3bc244 640 _smutex.unlock();
chuanga 0:43ff9e3bc244 641 return false;
chuanga 0:43ff9e3bc244 642 }
chuanga 0:43ff9e3bc244 643 send_size = amount;
chuanga 0:43ff9e3bc244 644 if (send_size > 2048) {
chuanga 0:43ff9e3bc244 645 send_size = 2048;
chuanga 0:43ff9e3bc244 646 }
chuanga 0:43ff9e3bc244 647 startup();
chuanga 0:43ff9e3bc244 648 setTimeout(ESP32_SEND_TIMEOUT);
chuanga 0:43ff9e3bc244 649 ret = _parser.send("AT+CIPSEND=%d,%d", id, send_size)
chuanga 0:43ff9e3bc244 650 && _parser.recv(">")
chuanga 0:43ff9e3bc244 651 && (_parser.write((char*)data + index, (int)send_size) >= 0)
chuanga 0:43ff9e3bc244 652 && _parser.recv("SEND OK");
chuanga 0:43ff9e3bc244 653 setTimeout();
chuanga 0:43ff9e3bc244 654 if (ret) {
chuanga 0:43ff9e3bc244 655 amount -= send_size;
chuanga 0:43ff9e3bc244 656 index += send_size;
chuanga 0:43ff9e3bc244 657 error_cnt = 0;
chuanga 0:43ff9e3bc244 658 if (amount == 0) {
chuanga 0:43ff9e3bc244 659 _smutex.unlock();
chuanga 0:43ff9e3bc244 660 return true;
chuanga 0:43ff9e3bc244 661 }
chuanga 0:43ff9e3bc244 662 } else {
chuanga 0:43ff9e3bc244 663 error_cnt++;
chuanga 0:43ff9e3bc244 664 }
chuanga 0:43ff9e3bc244 665 }
chuanga 0:43ff9e3bc244 666 _smutex.unlock();
chuanga 0:43ff9e3bc244 667
chuanga 0:43ff9e3bc244 668 return false;
chuanga 0:43ff9e3bc244 669 }
chuanga 0:43ff9e3bc244 670
chuanga 0:43ff9e3bc244 671 void ESP32::_packet_handler()
chuanga 0:43ff9e3bc244 672 {
chuanga 0:43ff9e3bc244 673 int id;
chuanga 0:43ff9e3bc244 674 int amount;
chuanga 0:43ff9e3bc244 675 uint32_t tmp_timeout;
chuanga 0:43ff9e3bc244 676
chuanga 0:43ff9e3bc244 677 // parse out the packet
chuanga 0:43ff9e3bc244 678 if (!_parser.recv(",%d,%d:", &id, &amount)) {
chuanga 0:43ff9e3bc244 679 return;
chuanga 0:43ff9e3bc244 680 }
chuanga 0:43ff9e3bc244 681
chuanga 0:43ff9e3bc244 682 struct packet *packet = (struct packet*)malloc(
chuanga 0:43ff9e3bc244 683 sizeof(struct packet) + amount);
chuanga 0:43ff9e3bc244 684 if (!packet) {
chuanga 0:43ff9e3bc244 685 return;
chuanga 0:43ff9e3bc244 686 }
chuanga 0:43ff9e3bc244 687
chuanga 0:43ff9e3bc244 688 packet->id = id;
chuanga 0:43ff9e3bc244 689 packet->len = amount;
chuanga 0:43ff9e3bc244 690 packet->next = 0;
chuanga 0:43ff9e3bc244 691 packet->index = 0;
chuanga 0:43ff9e3bc244 692
chuanga 0:43ff9e3bc244 693 tmp_timeout = last_timeout_ms;
chuanga 0:43ff9e3bc244 694 setTimeout(500);
chuanga 0:43ff9e3bc244 695 if (!(_parser.read((char*)(packet + 1), amount))) {
chuanga 0:43ff9e3bc244 696 free(packet);
chuanga 0:43ff9e3bc244 697 setTimeout(tmp_timeout);
chuanga 0:43ff9e3bc244 698 return;
chuanga 0:43ff9e3bc244 699 }
chuanga 0:43ff9e3bc244 700
chuanga 0:43ff9e3bc244 701 // append to packet list
chuanga 0:43ff9e3bc244 702 *_packets_end = packet;
chuanga 0:43ff9e3bc244 703 _packets_end = &packet->next;
chuanga 0:43ff9e3bc244 704 }
chuanga 0:43ff9e3bc244 705
chuanga 0:43ff9e3bc244 706 int32_t ESP32::recv(int id, void *data, uint32_t amount, uint32_t timeout)
chuanga 0:43ff9e3bc244 707 {
chuanga 0:43ff9e3bc244 708 struct packet **p;
chuanga 0:43ff9e3bc244 709 uint32_t idx = 0;
chuanga 0:43ff9e3bc244 710
chuanga 0:43ff9e3bc244 711 _cbs[id].Notified = 0;
chuanga 0:43ff9e3bc244 712
chuanga 0:43ff9e3bc244 713 _smutex.lock();
chuanga 0:43ff9e3bc244 714 setTimeout(timeout);
chuanga 0:43ff9e3bc244 715 if (_rts == NC) {
chuanga 0:43ff9e3bc244 716 while (_parser.process_oob()); // Poll for inbound packets
chuanga 0:43ff9e3bc244 717 } else {
chuanga 0:43ff9e3bc244 718 _parser.process_oob(); // Poll for inbound packets
chuanga 0:43ff9e3bc244 719 }
chuanga 0:43ff9e3bc244 720 setTimeout();
chuanga 0:43ff9e3bc244 721
chuanga 0:43ff9e3bc244 722 // check if any packets are ready for us
chuanga 0:43ff9e3bc244 723 p = &_packets;
chuanga 0:43ff9e3bc244 724 while (*p) {
chuanga 0:43ff9e3bc244 725 if ((*p)->id == id) {
chuanga 0:43ff9e3bc244 726 struct packet *q = *p;
chuanga 0:43ff9e3bc244 727
chuanga 0:43ff9e3bc244 728 if (q->len <= amount) { // Return and remove full packet
chuanga 0:43ff9e3bc244 729 memcpy(&(((uint8_t *)data)[idx]), (uint8_t*)(q+1) + q->index, q->len);
chuanga 0:43ff9e3bc244 730 if (_packets_end == &(*p)->next) {
chuanga 0:43ff9e3bc244 731 _packets_end = p;
chuanga 0:43ff9e3bc244 732 }
chuanga 0:43ff9e3bc244 733 *p = (*p)->next;
chuanga 0:43ff9e3bc244 734 idx += q->len;
chuanga 0:43ff9e3bc244 735 amount -= q->len;
chuanga 0:43ff9e3bc244 736 free(q);
chuanga 0:43ff9e3bc244 737 } else { // return only partial packet
chuanga 0:43ff9e3bc244 738 memcpy(&(((uint8_t *)data)[idx]), (uint8_t*)(q+1) + q->index, amount);
chuanga 0:43ff9e3bc244 739 q->len -= amount;
chuanga 0:43ff9e3bc244 740 q->index += amount;
chuanga 0:43ff9e3bc244 741 idx += amount;
chuanga 0:43ff9e3bc244 742 break;
chuanga 0:43ff9e3bc244 743 }
chuanga 0:43ff9e3bc244 744 } else {
chuanga 0:43ff9e3bc244 745 p = &(*p)->next;
chuanga 0:43ff9e3bc244 746 }
chuanga 0:43ff9e3bc244 747 }
chuanga 0:43ff9e3bc244 748 _smutex.unlock();
chuanga 0:43ff9e3bc244 749
chuanga 0:43ff9e3bc244 750 if (idx > 0) {
chuanga 0:43ff9e3bc244 751 return idx;
chuanga 0:43ff9e3bc244 752 } else if (((_id_bits & (1 << id)) == 0) || ((_id_bits_close & (1 << id)) != 0)) {
chuanga 0:43ff9e3bc244 753 return -2;
chuanga 0:43ff9e3bc244 754 } else {
chuanga 0:43ff9e3bc244 755 return -1;
chuanga 0:43ff9e3bc244 756 }
chuanga 0:43ff9e3bc244 757 }
chuanga 0:43ff9e3bc244 758
chuanga 0:43ff9e3bc244 759 void ESP32::_clear_socket_packets(int id)
chuanga 0:43ff9e3bc244 760 {
chuanga 0:43ff9e3bc244 761 struct packet **p = &_packets;
chuanga 0:43ff9e3bc244 762
chuanga 0:43ff9e3bc244 763 while (*p) {
chuanga 0:43ff9e3bc244 764 if ((*p)->id == id || id == ESP32_ALL_SOCKET_IDS) {
chuanga 0:43ff9e3bc244 765 struct packet *q = *p;
chuanga 0:43ff9e3bc244 766
chuanga 0:43ff9e3bc244 767 if (_packets_end == &(*p)->next) {
chuanga 0:43ff9e3bc244 768 _packets_end = p; // Set last packet next field/_packets
chuanga 0:43ff9e3bc244 769 }
chuanga 0:43ff9e3bc244 770 *p = (*p)->next;
chuanga 0:43ff9e3bc244 771
chuanga 0:43ff9e3bc244 772 free(q);
chuanga 0:43ff9e3bc244 773 } else {
chuanga 0:43ff9e3bc244 774 // Point to last packet next field
chuanga 0:43ff9e3bc244 775 p = &(*p)->next;
chuanga 0:43ff9e3bc244 776 }
chuanga 0:43ff9e3bc244 777 }
chuanga 0:43ff9e3bc244 778 }
chuanga 0:43ff9e3bc244 779
chuanga 0:43ff9e3bc244 780 bool ESP32::close(int id, bool wait_close)
chuanga 0:43ff9e3bc244 781 {
chuanga 0:43ff9e3bc244 782 if (wait_close) {
chuanga 0:43ff9e3bc244 783 _smutex.lock();
chuanga 0:43ff9e3bc244 784 for (int j = 0; j < 2; j++) {
chuanga 0:43ff9e3bc244 785 if (((_id_bits & (1 << id)) == 0)
chuanga 0:43ff9e3bc244 786 || ((_id_bits_close & (1 << id)) != 0)) {
chuanga 0:43ff9e3bc244 787 _id_bits_close &= ~(1 << id);
chuanga 0:43ff9e3bc244 788 _ids[id] = false;
chuanga 0:43ff9e3bc244 789 _clear_socket_packets(id);
chuanga 0:43ff9e3bc244 790 _smutex.unlock();
chuanga 0:43ff9e3bc244 791 return true;
chuanga 0:43ff9e3bc244 792 }
chuanga 0:43ff9e3bc244 793 startup();
chuanga 0:43ff9e3bc244 794 setTimeout(500);
chuanga 0:43ff9e3bc244 795 _parser.process_oob(); // Poll for inbound packets
chuanga 0:43ff9e3bc244 796 setTimeout();
chuanga 0:43ff9e3bc244 797 }
chuanga 0:43ff9e3bc244 798 _smutex.unlock();
chuanga 0:43ff9e3bc244 799 }
chuanga 0:43ff9e3bc244 800
chuanga 0:43ff9e3bc244 801 //May take a second try if device is busy
chuanga 0:43ff9e3bc244 802 for (unsigned i = 0; i < 2; i++) {
chuanga 0:43ff9e3bc244 803 _smutex.lock();
chuanga 0:43ff9e3bc244 804 if ((_id_bits & (1 << id)) == 0) {
chuanga 0:43ff9e3bc244 805 _id_bits_close &= ~(1 << id);
chuanga 0:43ff9e3bc244 806 _ids[id] = false;
chuanga 0:43ff9e3bc244 807 _clear_socket_packets(id);
chuanga 0:43ff9e3bc244 808 _smutex.unlock();
chuanga 0:43ff9e3bc244 809 return true;
chuanga 0:43ff9e3bc244 810 }
chuanga 0:43ff9e3bc244 811 startup();
chuanga 0:43ff9e3bc244 812 setTimeout(500);
chuanga 0:43ff9e3bc244 813 if (_parser.send("AT+CIPCLOSE=%d", id)
chuanga 0:43ff9e3bc244 814 && _parser.recv("OK")) {
chuanga 0:43ff9e3bc244 815 setTimeout();
chuanga 0:43ff9e3bc244 816 _clear_socket_packets(id);
chuanga 0:43ff9e3bc244 817 _id_bits_close &= ~(1 << id);
chuanga 0:43ff9e3bc244 818 _ids[id] = false;
chuanga 0:43ff9e3bc244 819 _smutex.unlock();
chuanga 0:43ff9e3bc244 820 return true;
chuanga 0:43ff9e3bc244 821 }
chuanga 0:43ff9e3bc244 822 setTimeout();
chuanga 0:43ff9e3bc244 823 _smutex.unlock();
chuanga 0:43ff9e3bc244 824 }
chuanga 0:43ff9e3bc244 825
chuanga 0:43ff9e3bc244 826 _ids[id] = false;
chuanga 0:43ff9e3bc244 827 return false;
chuanga 0:43ff9e3bc244 828 }
chuanga 0:43ff9e3bc244 829
chuanga 0:43ff9e3bc244 830 void ESP32::setTimeout(uint32_t timeout_ms)
chuanga 0:43ff9e3bc244 831 {
chuanga 0:43ff9e3bc244 832 last_timeout_ms = timeout_ms;
chuanga 0:43ff9e3bc244 833 _parser.set_timeout(timeout_ms);
chuanga 0:43ff9e3bc244 834 }
chuanga 0:43ff9e3bc244 835
chuanga 0:43ff9e3bc244 836 bool ESP32::readable()
chuanga 0:43ff9e3bc244 837 {
chuanga 0:43ff9e3bc244 838 return _serial.FileHandle::readable();
chuanga 0:43ff9e3bc244 839 }
chuanga 0:43ff9e3bc244 840
chuanga 0:43ff9e3bc244 841 bool ESP32::writeable()
chuanga 0:43ff9e3bc244 842 {
chuanga 0:43ff9e3bc244 843 return _serial.FileHandle::writable();
chuanga 0:43ff9e3bc244 844 }
chuanga 0:43ff9e3bc244 845
chuanga 0:43ff9e3bc244 846 void ESP32::socket_attach(int id, void (*callback)(void *), void *data)
chuanga 0:43ff9e3bc244 847 {
chuanga 0:43ff9e3bc244 848 _cbs[id].callback = callback;
chuanga 0:43ff9e3bc244 849 _cbs[id].data = data;
chuanga 0:43ff9e3bc244 850 _cbs[id].Notified = 0;
chuanga 0:43ff9e3bc244 851 }
chuanga 0:43ff9e3bc244 852
chuanga 0:43ff9e3bc244 853 bool ESP32::recv_ap(nsapi_wifi_ap_t *ap)
chuanga 0:43ff9e3bc244 854 {
chuanga 0:43ff9e3bc244 855 int sec;
chuanga 0:43ff9e3bc244 856 bool ret = _parser.recv("+CWLAP:(%d,\"%32[^\"]\",%hhd,\"%hhx:%hhx:%hhx:%hhx:%hhx:%hhx\",%hhu)", &sec, ap->ssid,
chuanga 0:43ff9e3bc244 857 &ap->rssi, &ap->bssid[0], &ap->bssid[1], &ap->bssid[2], &ap->bssid[3], &ap->bssid[4],
chuanga 0:43ff9e3bc244 858 &ap->bssid[5], &ap->channel);
chuanga 0:43ff9e3bc244 859
chuanga 0:43ff9e3bc244 860 ap->security = sec < 5 ? (nsapi_security_t)sec : NSAPI_SECURITY_UNKNOWN;
chuanga 0:43ff9e3bc244 861
chuanga 0:43ff9e3bc244 862 return ret;
chuanga 0:43ff9e3bc244 863 }
chuanga 0:43ff9e3bc244 864
chuanga 0:43ff9e3bc244 865 void ESP32::_connect_handler_0() { socket_handler(true, 0); }
chuanga 0:43ff9e3bc244 866 void ESP32::_connect_handler_1() { socket_handler(true, 1); }
chuanga 0:43ff9e3bc244 867 void ESP32::_connect_handler_2() { socket_handler(true, 2); }
chuanga 0:43ff9e3bc244 868 void ESP32::_connect_handler_3() { socket_handler(true, 3); }
chuanga 0:43ff9e3bc244 869 void ESP32::_connect_handler_4() { socket_handler(true, 4); }
chuanga 0:43ff9e3bc244 870 void ESP32::_closed_handler_0() { socket_handler(false, 0); }
chuanga 0:43ff9e3bc244 871 void ESP32::_closed_handler_1() { socket_handler(false, 1); }
chuanga 0:43ff9e3bc244 872 void ESP32::_closed_handler_2() { socket_handler(false, 2); }
chuanga 0:43ff9e3bc244 873 void ESP32::_closed_handler_3() { socket_handler(false, 3); }
chuanga 0:43ff9e3bc244 874 void ESP32::_closed_handler_4() { socket_handler(false, 4); }
chuanga 0:43ff9e3bc244 875
chuanga 0:43ff9e3bc244 876 void ESP32::_connection_status_handler()
chuanga 0:43ff9e3bc244 877 {
chuanga 0:43ff9e3bc244 878 char status[13];
chuanga 0:43ff9e3bc244 879 if (_parser.recv("%12[^\"]\n", status)) {
chuanga 0:43ff9e3bc244 880 if (strcmp(status, "CONNECTED\n") == 0) {
chuanga 0:43ff9e3bc244 881 _wifi_status = STATUS_CONNECTED;
chuanga 0:43ff9e3bc244 882 } else if (strcmp(status, "GOT IP\n") == 0) {
chuanga 0:43ff9e3bc244 883 _wifi_status = STATUS_GOT_IP;
chuanga 0:43ff9e3bc244 884 } else if (strcmp(status, "DISCONNECT\n") == 0) {
chuanga 0:43ff9e3bc244 885 _wifi_status = STATUS_DISCONNECTED;
chuanga 0:43ff9e3bc244 886 } else {
chuanga 0:43ff9e3bc244 887 return;
chuanga 0:43ff9e3bc244 888 }
chuanga 0:43ff9e3bc244 889
chuanga 0:43ff9e3bc244 890 if(_wifi_status_cb) {
chuanga 0:43ff9e3bc244 891 _wifi_status_cb(_wifi_status);
chuanga 0:43ff9e3bc244 892 }
chuanga 0:43ff9e3bc244 893 }
chuanga 0:43ff9e3bc244 894 }
chuanga 0:43ff9e3bc244 895
chuanga 0:43ff9e3bc244 896 int ESP32::get_free_id()
chuanga 0:43ff9e3bc244 897 {
chuanga 0:43ff9e3bc244 898 // Look for an unused socket
chuanga 0:43ff9e3bc244 899 int id = -1;
chuanga 0:43ff9e3bc244 900
chuanga 0:43ff9e3bc244 901 for (int i = 0; i < SOCKET_COUNT; i++) {
chuanga 0:43ff9e3bc244 902 if ((!_ids[i]) && ((_id_bits & (1 << i)) == 0)) {
chuanga 0:43ff9e3bc244 903 id = i;
chuanga 0:43ff9e3bc244 904 _ids[i] = true;
chuanga 0:43ff9e3bc244 905 break;
chuanga 0:43ff9e3bc244 906 }
chuanga 0:43ff9e3bc244 907 }
chuanga 0:43ff9e3bc244 908
chuanga 0:43ff9e3bc244 909 return id;
chuanga 0:43ff9e3bc244 910 }
chuanga 0:43ff9e3bc244 911
chuanga 0:43ff9e3bc244 912 void ESP32::event() {
chuanga 0:43ff9e3bc244 913 for (int i = 0; i < SOCKET_COUNT; i++) {
chuanga 0:43ff9e3bc244 914 if ((_cbs[i].callback) && (_cbs[i].Notified == 0)) {
chuanga 0:43ff9e3bc244 915 _cbs[i].callback(_cbs[i].data);
chuanga 0:43ff9e3bc244 916 _cbs[i].Notified = 1;
chuanga 0:43ff9e3bc244 917 }
chuanga 0:43ff9e3bc244 918 }
chuanga 0:43ff9e3bc244 919 }
chuanga 0:43ff9e3bc244 920
chuanga 0:43ff9e3bc244 921 bool ESP32::set_network(const char *ip_address, const char *netmask, const char *gateway)
chuanga 0:43ff9e3bc244 922 {
chuanga 0:43ff9e3bc244 923 bool ret;
chuanga 0:43ff9e3bc244 924
chuanga 0:43ff9e3bc244 925 if (ip_address == NULL) {
chuanga 0:43ff9e3bc244 926 return false;
chuanga 0:43ff9e3bc244 927 }
chuanga 0:43ff9e3bc244 928
chuanga 0:43ff9e3bc244 929 _smutex.lock();
chuanga 0:43ff9e3bc244 930 if ((netmask != NULL) && (gateway != NULL)) {
chuanga 0:43ff9e3bc244 931 ret = _parser.send("AT+CIPSTA=\"%s\",\"%s\",\"%s\"", ip_address, gateway, netmask)
chuanga 0:43ff9e3bc244 932 && _parser.recv("OK");
chuanga 0:43ff9e3bc244 933 } else {
chuanga 0:43ff9e3bc244 934 ret = _parser.send("AT+CIPSTA=\"%s\"", ip_address)
chuanga 0:43ff9e3bc244 935 && _parser.recv("OK");
chuanga 0:43ff9e3bc244 936 }
chuanga 0:43ff9e3bc244 937 _smutex.unlock();
chuanga 0:43ff9e3bc244 938
chuanga 0:43ff9e3bc244 939 return ret;
chuanga 0:43ff9e3bc244 940 }
chuanga 0:43ff9e3bc244 941
chuanga 0:43ff9e3bc244 942 bool ESP32::set_network_ap(const char *ip_address, const char *netmask, const char *gateway)
chuanga 0:43ff9e3bc244 943 {
chuanga 0:43ff9e3bc244 944 bool ret;
chuanga 0:43ff9e3bc244 945
chuanga 0:43ff9e3bc244 946 if (ip_address == NULL) {
chuanga 0:43ff9e3bc244 947 return false;
chuanga 0:43ff9e3bc244 948 }
chuanga 0:43ff9e3bc244 949
chuanga 0:43ff9e3bc244 950 _smutex.lock();
chuanga 0:43ff9e3bc244 951 if ((netmask != NULL) && (gateway != NULL)) {
chuanga 0:43ff9e3bc244 952 ret = _parser.send("AT+CIPAP=\"%s\",\"%s\",\"%s\"", ip_address, gateway, netmask)
chuanga 0:43ff9e3bc244 953 && _parser.recv("OK");
chuanga 0:43ff9e3bc244 954 } else {
chuanga 0:43ff9e3bc244 955 ret = _parser.send("AT+CIPAP=\"%s\"", ip_address)
chuanga 0:43ff9e3bc244 956 && _parser.recv("OK");
chuanga 0:43ff9e3bc244 957 }
chuanga 0:43ff9e3bc244 958 _smutex.unlock();
chuanga 0:43ff9e3bc244 959
chuanga 0:43ff9e3bc244 960 return ret;
chuanga 0:43ff9e3bc244 961 }
chuanga 0:43ff9e3bc244 962
chuanga 0:43ff9e3bc244 963 void ESP32::attach_wifi_status(mbed::Callback<void(int8_t)> status_cb)
chuanga 0:43ff9e3bc244 964 {
chuanga 0:43ff9e3bc244 965 _wifi_status_cb = status_cb;
chuanga 0:43ff9e3bc244 966 }
chuanga 0:43ff9e3bc244 967
chuanga 0:43ff9e3bc244 968 int8_t ESP32::get_wifi_status() const
chuanga 0:43ff9e3bc244 969 {
chuanga 0:43ff9e3bc244 970 return _wifi_status;
chuanga 0:43ff9e3bc244 971 }
chuanga 0:43ff9e3bc244 972 #endif
chuanga 0:43ff9e3bc244 973