Skip to content

Commit be4fdd9

Browse files
committed
Merge pull request #682 from nkolban/master
Issue #589 changes to the default station and access point creations.
2 parents 20a8b9d + f10b99d commit be4fdd9

File tree

4 files changed

+139
-54
lines changed

4 files changed

+139
-54
lines changed

libs/network/esp8266/jswrap_esp8266.c

+103-43
Original file line numberDiff line numberDiff line change
@@ -162,17 +162,33 @@ JsVar *jswrap_ESP8266_getAddressAsString(
162162
"name" : "disconnect",
163163
"generate" : "jswrap_ESP8266_wifi_disconnect",
164164
"params" : [
165+
["options", "JsVar", "Options controlling the function (optional)."],
165166
["callback", "JsVar", "A function to be called back on completion (optional)."]
166167
]
167-
}*/
168-
void jswrap_ESP8266_wifi_disconnect(JsVar *jsCallback) {
168+
}
169+
* The options is a JavaScript object that contains the following properties:
170+
* * `default` - If set to true, then auto connect of this device as a station will be
171+
* disabled. The default of `default` is false.
172+
*/
173+
void jswrap_ESP8266_wifi_disconnect(JsVar *jsOptions, JsVar *jsCallback) {
169174
os_printf("> jswrap_ESP8266_wifi_disconnect\n");
170175
if (g_jsDisconnectCallback != NULL) {
171176
jsvUnLock(g_jsDisconnectCallback);
172177
g_jsDisconnectCallback = NULL;
173178
}
174179
g_jsDisconnectCallback = jsvLockAgainSafe(jsCallback);
175180

181+
bool setDefault = false;
182+
if (jsOptions != NULL && jsvIsObject(jsOptions)) {
183+
JsVar *jsDefault = jsvObjectGetChild(jsOptions, "default", 0);
184+
if (jsvIsBoolean(jsDefault)) {
185+
if(jsvGetBool(jsDefault)) {
186+
wifi_station_set_auto_connect(0);
187+
}
188+
}
189+
jsvUnLock(jsDefault);
190+
}
191+
176192
bool rc = wifi_station_disconnect();
177193
JsVar *params[1];
178194

@@ -197,12 +213,17 @@ void jswrap_ESP8266_wifi_disconnect(JsVar *jsCallback) {
197213
"name" : "stopAP",
198214
"generate" : "jswrap_ESP8266_wifi_stopAP",
199215
"params" : [
216+
["options", "JsVar", "Options controlling the stop of an access point."],
200217
["callback", "JsVar", "A function to be called back on completion (optional)."]
201218
]
202219
}
203220
* Stop being an access point.
221+
* The options contain:
222+
* * default - A boolean. If set to true, then the access point will be stopped at boot time.
223+
* The default for `default` is false which means that the start/stop of an access point at boot time
224+
* will not be changed.
204225
*/
205-
void jswrap_ESP8266_wifi_stopAP(JsVar *jsCallback) {
226+
void jswrap_ESP8266_wifi_stopAP(JsVar *jsOptions, JsVar *jsCallback) {
206227
os_printf("> jswrap_ESP8266_wifi_stopAP\n");
207228

208229
if (g_jsStopAPCallback != NULL) {
@@ -219,12 +240,30 @@ void jswrap_ESP8266_wifi_stopAP(JsVar *jsCallback) {
219240
// SOFTAP_MODE NULL_MODE
220241
// STATIONAP_MODE STATION_MODE
221242

243+
bool setDefault = false;
244+
if (jsOptions != NULL && jsvIsObject(jsOptions)) {
245+
JsVar *jsDefault = jsvObjectGetChild(jsOptions, "default", 0);
246+
if (jsvIsBoolean(jsDefault)) {
247+
setDefault = jsvGetBool(jsDefault);
248+
}
249+
jsvUnLock(jsDefault);
250+
}
251+
222252
if (currentMode == STATION_MODE || currentMode == STATIONAP_MODE) {
223-
wifi_set_opmode_current(STATION_MODE);
253+
if (setDefault == false) {
254+
wifi_set_opmode_current(STATION_MODE);
255+
} else {
256+
wifi_set_opmode(STATION_MODE);
257+
}
224258
} else {
225-
wifi_set_opmode_current(NULL_MODE);
259+
if (setDefault == false) {
260+
wifi_set_opmode_current(NULL_MODE);
261+
} else {
262+
wifi_set_opmode(NULL_MODE);
263+
}
226264
}
227265

266+
228267
// No real return code to check, so hard code to be true.
229268
bool rc = true;
230269

@@ -258,7 +297,7 @@ void jswrap_ESP8266_wifi_stopAP(JsVar *jsCallback) {
258297
* Connect to an access point as a station.
259298
* When the outcome is known, the callback function is invoked. The options object contains
260299
* properties that control the connection. Included in this object are:
261-
* * autoConnect (Boolean) - When true, these settings will be used to autoConnect on next boot.
300+
* * default (Boolean) - When true, these settings will be used to connect on next boot.
262301
* * dnsServers (array of String) - An array of up to two DNS servers in dotted decimal format string.
263302
*/
264303
void jswrap_ESP8266_wifi_connect(
@@ -324,21 +363,7 @@ void jswrap_ESP8266_wifi_connect(
324363

325364
os_printf("> - ssid=%s, password=\"%s\"\n", ssid, password);
326365

327-
// Set the WiFi mode of the ESP8266
328-
329-
// Current Resulting
330-
// -------------- --------------
331-
// NULL_MODE STATION_MODE
332-
// STATION_MODE STATION_MODE
333-
// SOFTAP_MODE STATIONAP_MODE
334-
// STATIONAP_MODE STATIONAP_MODE
335366

336-
uint8 currentMode = wifi_get_opmode();
337-
if (currentMode == SOFTAP_MODE || currentMode == STATIONAP_MODE) {
338-
wifi_set_opmode_current(STATIONAP_MODE);
339-
} else {
340-
wifi_set_opmode_current(STATION_MODE);
341-
}
342367

343368
struct station_config stationConfig;
344369
memset(&stationConfig, 0, sizeof(stationConfig));
@@ -382,27 +407,23 @@ void jswrap_ESP8266_wifi_connect(
382407
#elif ISSUE_618 == 3
383408
// Add a return code to the function and return an already connected error.
384409
#endif
385-
}
386-
387-
388-
if (jsOptions != NULL && jsvIsObject(jsOptions)) {
389-
// Do we have a child property called autoConnect?
390-
JsVar *jsAutoConnect = jsvObjectGetChild(jsOptions, "autoConnect", 0);
391-
if (jsvIsBoolean(jsAutoConnect)) {
392-
if (jsvGetBool(jsAutoConnect) != false) {
393-
// autoConnect == true
394-
os_printf(" - Setting auto connect to true\n");
395-
wifi_station_set_auto_connect(true);
396-
// Set the WiFi configuration (includes setting default)
397-
wifi_station_set_config(&stationConfig);
398-
} else {
399-
// autoConnect == false
400-
os_printf(" - Setting auto connect to false\n");
401-
wifi_station_set_auto_connect(false);
402-
}
410+
}
411+
bool isDefault = false;
412+
if (jsOptions != NULL && jsvIsObject(jsOptions)) {
413+
// Do we have a child property called autoConnect?
414+
JsVar *jsDefault = jsvObjectGetChild(jsOptions, "default", 0);
415+
if (jsvIsBoolean(jsDefault)) {
416+
isDefault = jsvGetBool(jsDefault);
417+
if (isDefault) {
418+
// autoConnect == true
419+
os_printf(" - Setting auto connect to true\n");
420+
wifi_station_set_auto_connect(true);
421+
// Set the WiFi configuration (includes setting default)
422+
wifi_station_set_config(&stationConfig);
423+
}
403424
}
404-
jsvUnLock(jsAutoConnect);
405-
// End of autoConnect processing
425+
jsvUnLock(jsDefault);
426+
// End of default processing
406427

407428
// Do we have a child property called dnsServers?
408429
JsVar *jsDNSServers = jsvObjectGetChild(jsOptions, "dnsServers", 0);
@@ -443,7 +464,31 @@ void jswrap_ESP8266_wifi_connect(
443464
}
444465
}
445466
jsvUnLock(jsDNSServers);
446-
} // End of dnsServers processing
467+
} // End of options processing
468+
469+
// Set the WiFi mode of the ESP8266
470+
471+
// Current Resulting
472+
// -------------- --------------
473+
// NULL_MODE STATION_MODE
474+
// STATION_MODE STATION_MODE
475+
// SOFTAP_MODE STATIONAP_MODE
476+
// STATIONAP_MODE STATIONAP_MODE
477+
478+
uint8 currentMode = wifi_get_opmode();
479+
if (currentMode == SOFTAP_MODE || currentMode == STATIONAP_MODE) {
480+
if(isDefault) {
481+
wifi_set_opmode(STATIONAP_MODE);
482+
} else {
483+
wifi_set_opmode_current(STATIONAP_MODE);
484+
}
485+
} else {
486+
if (isDefault) {
487+
wifi_set_opmode(STATION_MODE);
488+
} else {
489+
wifi_set_opmode_current(STATION_MODE);
490+
}
491+
}
447492

448493
// Perform the network level connection.
449494
wifi_station_connect();
@@ -519,6 +564,7 @@ void jswrap_ESP8266_wifi_scan(
519564
* Create a logical access point allowing WiFi stations to connect.
520565
* The `options` object can contain the following properties.
521566
* * authMode - The authentication mode to use. Can be one of "open", "wpa2", "wpa", "wpa_wpa2".
567+
* * `default` - When true, sets the creation of an access point as the default at boot.
522568
*/
523569
void jswrap_ESP8266_wifi_createAP(
524570
JsVar *jsSsid, //!< The network SSID that we will use to listen as.
@@ -547,6 +593,7 @@ void jswrap_ESP8266_wifi_createAP(
547593

548594
softApConfig.authmode = (AUTH_MODE)-1;
549595

596+
bool isDefault = false;
550597
// Handle any options that may have been supplied.
551598
if (jsOptions != NULL && jsvIsObject(jsOptions)) {
552599

@@ -576,6 +623,11 @@ void jswrap_ESP8266_wifi_createAP(
576623
jsvUnLock(jsAuth);
577624
assert(softApConfig.authmode != (AUTH_MODE)-1);
578625
} // End of jsAuth is present.
626+
627+
JsVar *jsDefault = jsvObjectGetChild(jsOptions, "default", 0);
628+
if (jsvIsBoolean(jsDefault)) {
629+
isDefault = jsvGetBool(jsDefault);
630+
}
579631
} // End of jsOptions is present
580632

581633
// If no password has been supplied, then be open. Otherwise, use WPA2 and the
@@ -618,9 +670,17 @@ void jswrap_ESP8266_wifi_createAP(
618670
// STATIONAP_MODE STATIONAP_MODE
619671

620672
if (currentMode == STATION_MODE || currentMode == STATIONAP_MODE) {
621-
wifi_set_opmode_current(STATIONAP_MODE);
673+
if (isDefault) {
674+
wifi_set_opmode(STATIONAP_MODE);
675+
} else {
676+
wifi_set_opmode_current(STATIONAP_MODE);
677+
}
622678
} else {
623-
wifi_set_opmode_current(SOFTAP_MODE);
679+
if (isDefault) {
680+
wifi_set_opmode(SOFTAP_MODE);
681+
} else {
682+
wifi_set_opmode_current(SOFTAP_MODE);
683+
}
624684
}
625685

626686
softApConfig.ssid_len = 0; // Null terminated SSID

libs/network/esp8266/jswrap_esp8266.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ void jswrap_ESP8266WiFi_stopAP();
4848

4949
void jswrap_ESP8266_wifi_connect(JsVar *jsSsid, JsVar *jsPassword, JsVar *jsOptions, JsVar *jsCallback);
5050
void jswrap_ESP8266_wifi_createAP(JsVar *jsSsid, JsVar *jsPassword, JsVar *jsOptions, JsVar *jsCallback);
51-
void jswrap_ESP8266_wifi_disconnect();
51+
void jswrap_ESP8266_wifi_disconnect(JsVar *jsOptions, JsVar *jsCallback);
5252
JsVar *jswrap_ESP8266_wifi_getIP();
5353
void jswrap_ESP8266_wifi_scan(JsVar *jsCallback);
54-
void jswrap_ESP8266_wifi_stopAP(JsVar *jsCallback);
54+
void jswrap_ESP8266_wifi_stopAP(JsVar *jsOptions, JsVar *jsCallback);
5555

5656
void jswrap_ESP8266_dumpAllSocketData();
5757
void jswrap_ESP8266_dumpSocket(JsVar *jsSocketId);

libs/network/esp8266/network_esp8266.c

+16-1
Original file line numberDiff line numberDiff line change
@@ -583,10 +583,11 @@ static void doClose(
583583

584584

585585
if (pSocketData->creationType == SOCKET_CREATED_SERVER) {
586+
dumpEspConn(pSocketData->pEspconn);
586587
int rc = espconn_delete(pSocketData->pEspconn);
587588
if (rc != 0) {
588589
os_printf("espconn_delete: rc=%s (%d)\n",esp8266_errorToString(rc), rc);
589-
setSocketInError(socketId, "espconn_disconnect", rc);
590+
setSocketInError(socketId, "espconn_delete", rc);
590591
}
591592
pSocketData->state = SOCKET_STATE_CLOSING;
592593
}
@@ -845,6 +846,13 @@ static void esp8266_callback_recvCB(
845846
pSocketData->rxBuf = pNewBuf;
846847
pSocketData->rxBufLen += len;
847848
} // End of new data allocated.
849+
// Now that we have received some data, stop receiving any further data until this data is consumed.
850+
/*
851+
int rc = espconn_recv_hold(pEspconn);
852+
if (rc != 0) {
853+
os_printf(" - espconn_recv_hold: %d\n", rc);
854+
}
855+
*/
848856
dumpEspConn(pEspconn);
849857
os_printf("<< recvCB\n");
850858
}
@@ -931,6 +939,13 @@ int net_ESP8266_BOARD_recv(
931939
os_free(pSocketData->rxBuf);
932940
pSocketData->rxBuf = NULL;
933941
//os_printf("RX - A\n");
942+
943+
/*
944+
int rc = espconn_recv_unhold(pSocketData->pEspconn);
945+
if (rc != 0) {
946+
os_printf("espconn_recv_unhold: %d\n", rc);
947+
}
948+
*/
934949
return retLen;
935950
}
936951

targets/esp8266/docs/05-User_Guide.md

+18-8
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ and
134134

135135
Connect to a named access point.
136136

137-
`wifi.connect(ssid, password, options, callback)`
137+
`wifi.connect(ssid ,password [,options [,callback]])`
138138

139139
When called, this function places the ESP8266 in station mode. This means that we will not be an access point.
140140
Once done, we then connect to the named access point using the network and password parameters supplied by `ssid` and `password`. The optional callback is a function that is invoked when an IP address has been assigned to us meaning that we are now ready for TCP/IP based work.
@@ -146,10 +146,8 @@ Once done, we then connect to the named access point using the network and passw
146146

147147
The `options` object can contain the following optional properties:
148148

149-
* `autoConnect` - A boolean value. When `true`, a reconnect to the supplied access point
150-
will be performed each time the ESP8266 boots. When `false`, reconnection to the access point will not
151-
be attempted when the device boots. When not set, the current `autoConnect` settings will be
152-
maintained.
149+
* `default` - A boolean value. When `true`, a reconnect to the supplied access point
150+
will be performed each time the ESP8266 boots.
153151
* `dnsServers` - An array of strings. Specify an array of up to two IP addresses supplied as
154152
dotted decimal strings. Each entry represents the server address of a DNS server to be used for
155153
DNS lookups.
@@ -176,7 +174,7 @@ For example:
176174

177175
Become an access point.
178176

179-
`wifi.createAP(ssid [,password] [,options] [,callback])`
177+
`wifi.createAP(ssid [,password [,options [,callback]]])`
180178

181179
Become an access point for the network supplied by `ssid` with a password of `password`. If no
182180
password is supplied or is null, then the access point is considered open and be connected to by
@@ -186,6 +184,7 @@ eight characters in size.
186184
The `options` allows us to supply options.
187185

188186
* `authMode` - The authentication mode to use. Can be one of `open`, `wpa2`, `wpa` or `wpa_wpa2`. If not supplied then the value will be `wpa2` if a password is supplied and `open` if no password is supplied.
187+
* `default` - If true then we will automatically be an access point at boot time. The default is false in which case the current boot settings will be maintained.
189188

190189
The `callback` is a a function with the following signature:
191190

@@ -198,14 +197,20 @@ The `err` parameter is an object with properties of `errorCode` and `errorMessag
198197
##wifi.disconnect
199198
Disconnect the ESP8266 from the access point.
200199

201-
`wifi.disconnect(callback)`
200+
`wifi.disconnect([options [,callback]])`
202201

203202
For example:
204203

205204
var wifi = require("wifi");
206205
// ... connect ...
207206
wifi.disconnect();
208207

208+
The optional `options` is a JavaScript object that controls the operation of this function. It
209+
may be `null` if not needed. If supplied, it may contain the following properties:
210+
211+
* `default` - A boolean which, if set to true, will result in the device not attempt to connect on
212+
subsequent boots.
213+
209214
The optional `callback` function will be invoked when the disconnect outcome is known. The signature of
210215
the callback function is:
211216

@@ -266,11 +271,16 @@ For example:
266271

267272
Don't be an access point any longer.
268273

269-
`wifi.stopAP([callback])`
274+
`wifi.stopAP([options[,callback]])`
270275

271276
If we are playing the role of an access point, stop performing that function. Connected stations will be
272277
disconnected and we will no longer be visible as a WiFi network.
273278

279+
The `options` is a JavaScript object controlling the options for the function. It may be `null`. The
280+
following properties are honored:
281+
282+
* `default` - A boolean. If set to true, then the access point will not be started at boot time.
283+
274284
The optional `callback` is a callback function to be invoked when the device is no longer an
275285
access point. The signature for the callback is:
276286

0 commit comments

Comments
 (0)