Skip to content

Commit 7ef7aff

Browse files
committed
Port esp2866 driver to new WiFiInterface API
1 parent 1999dae commit 7ef7aff

File tree

4 files changed

+179
-23
lines changed

4 files changed

+179
-23
lines changed

ESP8266/ESP8266.cpp

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ bool ESP8266::disconnect(void)
7979
const char *ESP8266::getIPAddress(void)
8080
{
8181
if (!(_parser.send("AT+CIFSR")
82-
&& _parser.recv("+CIFSR:STAIP,\"%[^\"]\"", _ip_buffer)
82+
&& _parser.recv("+CIFSR:STAIP,\"%15[^\"]\"", _ip_buffer)
8383
&& _parser.recv("OK"))) {
8484
return 0;
8585
}
@@ -90,19 +90,62 @@ const char *ESP8266::getIPAddress(void)
9090
const char *ESP8266::getMACAddress(void)
9191
{
9292
if (!(_parser.send("AT+CIFSR")
93-
&& _parser.recv("+CIFSR:STAMAC,\"%[^\"]\"", _mac_buffer)
93+
&& _parser.recv("+CIFSR:STAMAC,\"%17[^\"]\"", _mac_buffer)
9494
&& _parser.recv("OK"))) {
9595
return 0;
9696
}
9797

9898
return _mac_buffer;
9999
}
100100

101+
int8_t ESP8266::getRSSI()
102+
{
103+
int8_t rssi;
104+
char bssid[18];
105+
106+
if (!(_parser.send("AT+CWJAP?")
107+
&& _parser.recv("+CWJAP:\"%*[^\"]\",\"%17[^\"]\"", bssid)
108+
&& _parser.recv("OK"))) {
109+
return 0;
110+
}
111+
112+
if (!(_parser.send("AT+CWLAP=\"\",\"%s\",", bssid)
113+
&& _parser.recv("+CWLAP:(%*d,\"%*[^\"]\",%hhd,", &rssi)
114+
&& _parser.recv("OK"))) {
115+
return 0;
116+
}
117+
118+
return rssi;
119+
}
120+
101121
bool ESP8266::isConnected(void)
102122
{
103123
return getIPAddress() != 0;
104124
}
105125

126+
int ESP8266::scan(WiFiAccessPoint *res, unsigned limit)
127+
{
128+
unsigned cnt = 0;
129+
nsapi_wifi_ap_t ap;
130+
131+
if (!_parser.send("AT+CWLAP")) {
132+
return NSAPI_ERROR_DEVICE_ERROR;
133+
}
134+
135+
while (recv_ap(&ap)) {
136+
if (cnt < limit) {
137+
res[cnt] = WiFiAccessPoint(ap);
138+
}
139+
140+
cnt++;
141+
if (limit != 0 && cnt >= limit) {
142+
break;
143+
}
144+
}
145+
146+
return cnt;
147+
}
148+
106149
bool ESP8266::open(const char *type, int id, const char* addr, int port)
107150
{
108151
//IDs only 0-4
@@ -227,3 +270,15 @@ void ESP8266::attach(Callback<void()> func)
227270
{
228271
_serial.attach(func);
229272
}
273+
274+
bool ESP8266::recv_ap(nsapi_wifi_ap_t *ap)
275+
{
276+
int sec;
277+
bool ret = _parser.recv("+CWLAP:(%d,\"%32[^\"]\",%hhd,\"%hhx:%hhx:%hhx:%hhx:%hhx:%hhx\",%d", &sec, ap->ssid,
278+
&ap->rssi, &ap->bssid[0], &ap->bssid[1], &ap->bssid[2], &ap->bssid[3], &ap->bssid[4],
279+
&ap->bssid[5], &ap->channel);
280+
281+
ap->security = sec < 5 ? (nsapi_security_t)sec : NSAPI_SECURITY_UNKNOWN;
282+
283+
return ret;
284+
}

ESP8266/ESP8266.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,28 @@ class ESP8266
8181
*/
8282
const char *getMACAddress(void);
8383

84+
/* Return RSSI for active connection
85+
*
86+
* @return Measured RSSI
87+
*/
88+
int8_t getRSSI();
89+
8490
/**
8591
* Check if ESP8266 is conenected
8692
*
8793
* @return true only if the chip has an IP address
8894
*/
8995
bool isConnected(void);
9096

97+
/** Scan for available networks
98+
*
99+
* @param ap Pointer to allocated array to store discovered AP
100+
* @param limit Size of allocated @a res array, or 0 to only count available AP
101+
* @return Number of entries in @a res, or if @a count was 0 number of available networks, negative on error
102+
* see @a nsapi_error
103+
*/
104+
int scan(WiFiAccessPoint *res, unsigned limit);
105+
91106
/**
92107
* Open a socketed connection
93108
*
@@ -173,6 +188,7 @@ class ESP8266
173188
// data follows
174189
} *_packets, **_packets_end;
175190
void _packet_handler();
191+
bool recv_ap(nsapi_wifi_ap_t *ap);
176192

177193
char _ip_buffer[16];
178194
char _mac_buffer[18];

ESP8266Interface.cpp

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
1716

17+
#include <string.h>
1818
#include "ESP8266Interface.h"
1919

2020
// Various timeouts for different ESP8266 operations
@@ -23,7 +23,6 @@
2323
#define ESP8266_RECV_TIMEOUT 0
2424
#define ESP8266_MISC_TIMEOUT 500
2525

26-
2726
// ESP8266Interface implementation
2827
ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug)
2928
: _esp(tx, rx, debug)
@@ -34,10 +33,18 @@ ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug)
3433
_esp.attach(this, &ESP8266Interface::event);
3534
}
3635

37-
int ESP8266Interface::connect(
38-
const char *ssid,
39-
const char *pass,
40-
nsapi_security_t security)
36+
int ESP8266Interface::connect(const char *ssid, const char *pass, nsapi_security_t security,
37+
uint8_t channel)
38+
{
39+
if (channel != 0) {
40+
return NSAPI_ERROR_UNSUPPORTED;
41+
}
42+
43+
set_credentials(ssid, pass, security);
44+
return connect();
45+
}
46+
47+
int ESP8266Interface::connect()
4148
{
4249
_esp.setTimeout(ESP8266_CONNECT_TIMEOUT);
4350

@@ -49,17 +56,36 @@ int ESP8266Interface::connect(
4956
return NSAPI_ERROR_DHCP_FAILURE;
5057
}
5158

52-
if (!_esp.connect(ssid, pass)) {
59+
if (!_esp.connect(ap_ssid, ap_pass)) {
5360
return NSAPI_ERROR_NO_CONNECTION;
5461
}
5562

5663
if (!_esp.getIPAddress()) {
5764
return NSAPI_ERROR_DHCP_FAILURE;
5865
}
5966

67+
return NSAPI_ERROR_OK;
68+
}
69+
70+
int ESP8266Interface::set_credentials(const char *ssid, const char *pass, nsapi_security_t security)
71+
{
72+
memset(ap_ssid, 0, sizeof(ap_ssid));
73+
strncpy(ap_ssid, ssid, sizeof(ap_ssid));
74+
75+
memset(ap_pass, 0, sizeof(ap_pass));
76+
strncpy(ap_pass, pass, sizeof(ap_pass));
77+
78+
ap_sec = security;
79+
6080
return 0;
6181
}
6282

83+
int ESP8266Interface::set_channel(uint8_t channel)
84+
{
85+
return NSAPI_ERROR_UNSUPPORTED;
86+
}
87+
88+
6389
int ESP8266Interface::disconnect()
6490
{
6591
_esp.setTimeout(ESP8266_MISC_TIMEOUT);
@@ -68,7 +94,7 @@ int ESP8266Interface::disconnect()
6894
return NSAPI_ERROR_DEVICE_ERROR;
6995
}
7096

71-
return 0;
97+
return NSAPI_ERROR_OK;
7298
}
7399

74100
const char* ESP8266Interface::get_ip_address()
@@ -81,6 +107,16 @@ const char* ESP8266Interface::get_mac_address()
81107
return _esp.getMACAddress();
82108
}
83109

110+
int8_t ESP8266Interface::get_rssi()
111+
{
112+
return _esp.getRSSI();
113+
}
114+
115+
int ESP8266Interface::scan(WiFiAccessPoint *res, unsigned count)
116+
{
117+
return _esp.scan(res, count);
118+
}
119+
84120
struct esp8266_socket {
85121
int id;
86122
nsapi_protocol_t proto;

ESP8266Interface.h

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
#ifndef ESP8266_INTERFACE_H
1818
#define ESP8266_INTERFACE_H
1919

20-
#include "network-socket/NetworkStack.h"
21-
#include "network-socket/WiFiInterface.h"
20+
#include "NetworkStack.h"
21+
#include "WiFiInterface.h"
2222
#include "ESP8266.h"
2323

2424

@@ -39,18 +39,44 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface
3939

4040
/** Start the interface
4141
*
42-
* Attempts to connect to a WiFi network. If passphrase is invalid,
43-
* NSAPI_ERROR_AUTH_ERROR is returned.
42+
* Attempts to connect to a WiFi network. Requires ssid and passphrase to be set.
43+
* If passphrase is invalid, NSAPI_ERROR_AUTH_ERROR is returned.
44+
*
45+
* @return 0 on success, negative error code on failure
46+
*/
47+
virtual int connect();
48+
49+
/** Start the interface
50+
*
51+
* Attempts to connect to a WiFi network.
52+
*
53+
* @param ssid Name of the network to connect to
54+
* @param pass Security passphrase to connect to the network
55+
* @param security Type of encryption for connection (Default: NSAPI_SECURITY_NONE)
56+
* @param channel This parameter is not supported, setting it to anything else than 0 will result in NSAPI_ERROR_UNSUPPORTED
57+
* @return 0 on success, or error code on failure
58+
*/
59+
virtual int connect(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE,
60+
uint8_t channel = 0);
61+
62+
/** Set the WiFi network credentials
4463
*
4564
* @param ssid Name of the network to connect to
4665
* @param pass Security passphrase to connect to the network
4766
* @param security Type of encryption for connection
48-
* @return 0 on success, negative error code on failure
67+
* (defaults to NSAPI_SECURITY_NONE)
68+
* @return 0 on success, or error code on failure
4969
*/
50-
virtual int connect(
51-
const char *ssid,
52-
const char *pass,
53-
nsapi_security_t security = NSAPI_SECURITY_NONE);
70+
virtual int set_credentials(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE);
71+
72+
/** Set the WiFi network channel - NOT SUPPORTED
73+
*
74+
* This function is not supported and will return NSAPI_ERROR_UNSUPPORTED
75+
*
76+
* @param channel Channel on which the connection is to be made, or 0 for any (Default: 0)
77+
* @return Not supported, returns NSAPI_ERROR_UNSUPPORTED
78+
*/
79+
virtual int set_channel(uint8_t channel);
5480

5581
/** Stop the interface
5682
* @return 0 on success, negative on failure
@@ -67,6 +93,24 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface
6793
*/
6894
virtual const char *get_mac_address();
6995

96+
/** Gets the current radio signal strength for active connection
97+
*
98+
* @return Connection strength in dBm (negative value)
99+
*/
100+
virtual int8_t get_rssi();
101+
102+
/** Scan for available networks
103+
*
104+
* This function will block.
105+
*
106+
* @param ap Pointer to allocated array to store discovered AP
107+
* @param count Size of allocated @a res array, or 0 to only count available AP
108+
* @param timeout Timeout in milliseconds; 0 for no timeout (Default: 0)
109+
* @return Number of entries in @a, or if @a count was 0 number of available networks, negative on error
110+
* see @a nsapi_error
111+
*/
112+
virtual int scan(WiFiAccessPoint *res, unsigned count);
113+
70114
protected:
71115
/** Open a socket
72116
* @param handle Handle in which to store new socket
@@ -78,14 +122,14 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface
78122
/** Close the socket
79123
* @param handle Socket handle
80124
* @return 0 on success, negative on failure
81-
* @note On failure, any memory associated with the socket must still
125+
* @note On failure, any memory associated with the socket must still
82126
* be cleaned up
83127
*/
84128
virtual int socket_close(void *handle);
85129

86130
/** Bind a server socket to a specific port
87131
* @param handle Socket handle
88-
* @param address Local address to listen for incoming connections on
132+
* @param address Local address to listen for incoming connections on
89133
* @return 0 on success, negative on failure.
90134
*/
91135
virtual int socket_bind(void *handle, const SocketAddress &address);
@@ -174,17 +218,22 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface
174218
{
175219
return this;
176220
}
177-
221+
178222
private:
179223
ESP8266 _esp;
180224
bool _ids[ESP8266_SOCKET_COUNT];
181225

226+
char ap_ssid[33]; /* 32 is what 802.11 defines as longest possible name; +1 for the \0 */
227+
nsapi_security_t ap_sec;
228+
uint8_t ap_ch;
229+
char ap_pass[64]; /* The longest allowed passphrase */
230+
182231
void event();
232+
183233
struct {
184234
void (*callback)(void *);
185235
void *data;
186236
} _cbs[ESP8266_SOCKET_COUNT];
187237
};
188238

189-
190239
#endif

0 commit comments

Comments
 (0)