1
+ // Because the ESP8266 JS wrapper is assured to be running on an ESP8266 we
2
+ // can assume that inclusion of ESP8266 headers will be acceptable.
1
3
#include <c_types.h>
2
4
#include <user_interface.h>
3
5
#include <mem.h>
@@ -60,7 +62,13 @@ static struct ping_option pingOpt;
60
62
["gotIpCallback", "JsVar", "An optional callback invoked when we have an IP"]
61
63
]
62
64
}*/
63
- void jswrap_ESP8266WiFi_connect (JsVar * jsv_ssid , JsVar * jsv_password , JsVar * gotIpCallback ) {
65
+ void jswrap_ESP8266WiFi_connect (
66
+ JsVar * jsv_ssid , //!< The SSID of the access point to connect.
67
+ JsVar * jsv_password , //!< The password for the access point.
68
+ JsVar * gotIpCallback //!< The Callback function to be called when we are connected.
69
+ ) {
70
+ os_printf ("> jswrap_ESP8266WiFi_connect\n" );
71
+
64
72
// Check that the ssid and password values aren't obviously in error.
65
73
if (jsv_ssid == NULL || !jsvIsString (jsv_ssid )) {
66
74
jsExceptionHere (JSET_ERROR , "No SSID." );
@@ -86,6 +94,8 @@ void jswrap_ESP8266WiFi_connect(JsVar *jsv_ssid, JsVar *jsv_password, JsVar *got
86
94
jsvUnLock (jsGotIpCallback );
87
95
jsGotIpCallback = NULL ;
88
96
}
97
+
98
+ // What does this do?
89
99
if (gotIpCallback != NULL ) {
90
100
jsGotIpCallback = jsvLockAgainSafe (gotIpCallback );
91
101
}
@@ -98,8 +108,7 @@ void jswrap_ESP8266WiFi_connect(JsVar *jsv_ssid, JsVar *jsv_password, JsVar *got
98
108
len = jsvGetString (jsv_password , password , sizeof (password )- 1 );
99
109
password [len ]= '\0' ;
100
110
101
- jsiConsolePrintf ("jswrap_ESP8266WiFi_connect: %s - %s\r\n" , ssid , password );
102
-
111
+ os_printf ("> - ssid=%s, password=%s\n" , ssid , password );
103
112
// Set the WiFi mode of the ESP8266
104
113
wifi_set_opmode_current (STATION_MODE );
105
114
@@ -115,10 +124,11 @@ void jswrap_ESP8266WiFi_connect(JsVar *jsv_ssid, JsVar *jsv_password, JsVar *got
115
124
// Set the WiFi configuration
116
125
wifi_station_set_config (& stationConfig );
117
126
118
- // Register the event handler
127
+ // Register the event handler for callbacks from ESP8266
119
128
wifi_set_event_handler_cb (wifiEventHandler );
120
129
121
130
wifi_station_connect ();
131
+ os_printf ("< jswrap_ESP8266WiFi_connect\n" );
122
132
} // End of jswrap_ESP8266WiFi_connect
123
133
124
134
@@ -209,24 +219,26 @@ void jswrap_ESP8266WiFi_beAccessPoint(
209
219
void jswrap_ESP8266WiFi_getAccessPoints (
210
220
JsVar * callback //!< Function to call back when access points retrieved.
211
221
) {
212
- jsiConsolePrint ("> ESP8266WiFi_getAccessPoints\n" );
222
+ os_printf ("> ESP8266WiFi_getAccessPoints\n" );
213
223
if (callback == NULL || !jsvIsFunction (callback )) {
214
224
jsExceptionHere (JSET_ERROR , "No callback." );
215
225
return ;
216
226
}
217
227
218
- // Save the callback for the scan
228
+ // Save the callback for the scan in the global variable called jsScanCallback.
219
229
jsScanCallback = jsvLockAgainSafe (callback );
220
230
221
231
// Ask the ESP8266 to perform a network scan after first entering
222
- // station mode. This will result in an eventual callback which is where
232
+ // station mode. The network scan will eventually result in a callback
233
+ // being executed (scanCB) which will contain the results.
234
+
223
235
// Ensure we are in station mode
224
236
wifi_set_opmode_current (STATION_MODE );
225
237
226
238
// Request a scan of the network calling "scanCB" on completion
227
239
wifi_station_scan (NULL , scanCB );
228
240
229
- jsiConsolePrint ("< ESP8266WiFi_getAccessPoints\n" );
241
+ os_printf ("< ESP8266WiFi_getAccessPoints\n" );
230
242
} // End of jswrap_ESP8266WiFi_getAccessPoints
231
243
232
244
@@ -812,6 +824,7 @@ static void scanCB(void *arg, STATUS status) {
812
824
* of records.
813
825
*/
814
826
827
+ os_printf (">> scanCB\n" );
815
828
// Create the Empty JS array that will be passed as a parameter to the callback.
816
829
JsVar * accessPointArray = jsvNewArray (NULL , 0 );
817
830
struct bss_info * bssInfo ;
@@ -851,7 +864,7 @@ static void scanCB(void *arg, STATUS status) {
851
864
// Add the new record to the array
852
865
jsvArrayPush (accessPointArray , currentAccessPoint );
853
866
854
- os_printf ("ssid: %s\n" , bssInfo -> ssid );
867
+ os_printf (" - ssid: %s\n" , bssInfo -> ssid );
855
868
bssInfo = STAILQ_NEXT (bssInfo , next );
856
869
} // End of loop over the records.
857
870
@@ -860,6 +873,7 @@ static void scanCB(void *arg, STATUS status) {
860
873
params [0 ] = accessPointArray ;
861
874
jsiQueueEvents (NULL , jsScanCallback , params , 1 );
862
875
jsvUnLock (jsScanCallback );
876
+ os_printf ("<< scanCB\n" );
863
877
} // End of scanCB
864
878
865
879
@@ -894,16 +908,19 @@ static void sendWifiEvent(uint32 eventType, JsVar *details) {
894
908
/**
895
909
* \brief ESP8266 WiFi Event handler.
896
910
* This function is called by the ESP8266
897
- * environment when significant events happend related to the WiFi environment.
911
+ * environment when significant events happen related to the WiFi environment.
898
912
* The event handler is registered with a call to wifi_set_event_handler_cb()
899
913
* that is provided by the ESP8266 SDK.
900
914
*/
901
915
static void wifiEventHandler (System_Event_t * event ) {
902
916
switch (event -> event ) {
917
+ // We have connected to an access point.
903
918
case EVENT_STAMODE_CONNECTED :
904
919
os_printf ("Event: EVENT_STAMODE_CONNECTED\n" );
905
920
sendWifiEvent (event -> event , jsvNewNull ());
906
921
break ;
922
+
923
+ // We have disconnected or been disconnected from an access point.
907
924
case EVENT_STAMODE_DISCONNECTED :
908
925
os_printf ("Event: EVENT_STAMODE_DISCONNECTED\n" );
909
926
JsVar * details = jspNewObject (NULL , "EventDetails" );
@@ -913,10 +930,14 @@ static void wifiEventHandler(System_Event_t *event) {
913
930
ssid [ event -> event_info .disconnected .ssid_len ] = '\0' ;
914
931
sendWifiEvent (event -> event , details );
915
932
break ;
933
+
934
+ // The authentication information at the access point has changed.
916
935
case EVENT_STAMODE_AUTHMODE_CHANGE :
917
936
os_printf ("Event: EVENT_STAMODE_AUTHMODE_CHANGE\n" );
918
937
sendWifiEvent (event -> event , jsvNewNull ());
919
938
break ;
939
+
940
+ // We have been allocated an IP address.
920
941
case EVENT_STAMODE_GOT_IP :
921
942
os_printf ("Event: EVENT_STAMODE_GOT_IP\n" );
922
943
sendWifiEvent (event -> event , jsvNewNull ());
0 commit comments