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@39:7e85bf8003fa, 2016-02-24 (annotated)
- Committer:
- Christopher Haster
- Date:
- Wed Feb 24 19:07:08 2016 -0600
- Branch:
- api-changes
- Revision:
- 39:7e85bf8003fa
- Parent:
- 38:eb1e46561a19
- Child:
- 40:83c6b4129468
Fixed memory leak in destroySocket
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" |
Christopher Haster |
34:9c26a3dcdc1f | 18 | #include "ESP8266SocketInterface.h" |
sam_grove | 11:288c15b80a26 | 19 | |
Christopher Haster |
34:9c26a3dcdc1f | 20 | ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug) |
Christopher Haster |
34:9c26a3dcdc1f | 21 | : _esp(tx, rx, debug) |
sam_grove | 24:37504440f296 | 22 | { |
Christopher Haster |
34:9c26a3dcdc1f | 23 | memset(_ids, 0, sizeof(_ids)); |
sam_grove | 11:288c15b80a26 | 24 | } |
sam_grove | 11:288c15b80a26 | 25 | |
Christopher Haster |
34:9c26a3dcdc1f | 26 | int32_t ESP8266Interface::connect( |
Christopher Haster |
34:9c26a3dcdc1f | 27 | const char *ap, |
Christopher Haster |
34:9c26a3dcdc1f | 28 | const char *pass_phrase, |
Christopher Haster |
38:eb1e46561a19 | 29 | wifi_security_t) |
sam_grove | 11:288c15b80a26 | 30 | { |
Christopher Haster |
38:eb1e46561a19 | 31 | _esp.setTimeout(getTimeout()); |
Christopher Haster |
34:9c26a3dcdc1f | 32 | |
Christopher Haster |
34:9c26a3dcdc1f | 33 | if (!_esp.startup(3)) return -1; |
Christopher Haster |
34:9c26a3dcdc1f | 34 | if (!_esp.dhcp(true, 1)) return -1; |
Christopher Haster |
34:9c26a3dcdc1f | 35 | if (!_esp.connect(ap, pass_phrase)) return -1; |
Christopher Haster |
34:9c26a3dcdc1f | 36 | |
Christopher Haster |
34:9c26a3dcdc1f | 37 | _ip_address = _esp.getIPAddress(); |
Christopher Haster |
34:9c26a3dcdc1f | 38 | _mac_address = _esp.getMACAddress(); |
Christopher Haster |
34:9c26a3dcdc1f | 39 | if (!_ip_address || !_mac_address) return -1; |
Christopher Haster |
34:9c26a3dcdc1f | 40 | |
sam_grove | 24:37504440f296 | 41 | return 0; |
sam_grove | 11:288c15b80a26 | 42 | } |
sam_grove | 11:288c15b80a26 | 43 | |
Christopher Haster |
34:9c26a3dcdc1f | 44 | int32_t ESP8266Interface::disconnect() |
sam_grove | 11:288c15b80a26 | 45 | { |
Christopher Haster |
34:9c26a3dcdc1f | 46 | if (!_esp.disconnect()) return -1; |
Christopher Haster |
34:9c26a3dcdc1f | 47 | |
sarahmarshy | 22:312453862371 | 48 | return 0; |
sam_grove | 11:288c15b80a26 | 49 | } |
sam_grove | 11:288c15b80a26 | 50 | |
Christopher Haster |
34:9c26a3dcdc1f | 51 | const char *ESP8266Interface::getIPAddress() |
sarahmarshy | 18:9fc7976c7b27 | 52 | { |
Christopher Haster |
34:9c26a3dcdc1f | 53 | return _ip_address; |
sarahmarshy | 18:9fc7976c7b27 | 54 | } |
sarahmarshy | 18:9fc7976c7b27 | 55 | |
Christopher Haster |
34:9c26a3dcdc1f | 56 | const char *ESP8266Interface::getMACAddress() |
sam_grove | 11:288c15b80a26 | 57 | { |
Christopher Haster |
34:9c26a3dcdc1f | 58 | return _mac_address; |
sam_grove | 11:288c15b80a26 | 59 | } |
sam_grove | 11:288c15b80a26 | 60 | |
Christopher Haster |
38:eb1e46561a19 | 61 | void ESP8266Interface::setTimeout(uint32_t timeout) |
Christopher Haster |
38:eb1e46561a19 | 62 | { |
Christopher Haster |
38:eb1e46561a19 | 63 | NetworkInterface::setTimeout(timeout); |
Christopher Haster |
38:eb1e46561a19 | 64 | _esp.setTimeout(timeout); |
Christopher Haster |
38:eb1e46561a19 | 65 | } |
Christopher Haster |
38:eb1e46561a19 | 66 | |
Christopher Haster |
34:9c26a3dcdc1f | 67 | SocketInterface *ESP8266Interface::createSocket(socket_protocol_t proto) |
sam_grove | 11:288c15b80a26 | 68 | { |
Christopher Haster |
34:9c26a3dcdc1f | 69 | // Look for an unused socket |
sarahmarshy | 18:9fc7976c7b27 | 70 | int id = -1; |
Christopher Haster |
34:9c26a3dcdc1f | 71 | |
Christopher Haster |
34:9c26a3dcdc1f | 72 | for (int i = 0; i < ESP8266_SOCKET_COUNT; i++) { |
Christopher Haster |
34:9c26a3dcdc1f | 73 | if (!_ids[i]) { |
sam_grove | 24:37504440f296 | 74 | id = i; |
Christopher Haster |
34:9c26a3dcdc1f | 75 | _ids[i] = true; |
sarahmarshy | 18:9fc7976c7b27 | 76 | break; |
sarahmarshy | 18:9fc7976c7b27 | 77 | } |
sarahmarshy | 18:9fc7976c7b27 | 78 | } |
Christopher Haster |
34:9c26a3dcdc1f | 79 | |
sam_grove | 24:37504440f296 | 80 | if (id == -1) { |
Christopher Haster |
34:9c26a3dcdc1f | 81 | return 0; |
sarahmarshy | 22:312453862371 | 82 | } |
bridadan | 16:b2f781416464 | 83 | |
Christopher Haster |
34:9c26a3dcdc1f | 84 | return new ESP8266SocketInterface(&_esp, proto, id); |
bridadan | 16:b2f781416464 | 85 | } |
bridadan | 16:b2f781416464 | 86 | |
Christopher Haster |
34:9c26a3dcdc1f | 87 | void ESP8266Interface::destroySocket(SocketInterface *iface) |
sarahmarshy | 18:9fc7976c7b27 | 88 | { |
Christopher Haster |
34:9c26a3dcdc1f | 89 | ESP8266SocketInterface *socket = (ESP8266SocketInterface *)iface; |
Christopher Haster |
34:9c26a3dcdc1f | 90 | _ids[socket->getID()] = false; |
Christopher Haster |
39:7e85bf8003fa | 91 | delete socket; |
sam_grove | 13:0186e9e35a24 | 92 | } |
sam_grove | 13:0186e9e35a24 | 93 |