@@ -708,12 +708,50 @@ void jswrap_ESP8266_wifi_createAP(
708
708
os_printf ("< jswrap_ESP8266_wifi_createAP\n" );
709
709
}
710
710
711
+ /*JSON{
712
+ "type" : "staticmethod",
713
+ "class" : "wifi",
714
+ "name" : "getStatus",
715
+ "generate" : "jswrap_ESP8266_wifi_getStatus",
716
+ "return" : ["JsVar", "A boolean representing our WiFi status "]
717
+ }
718
+ * Retrieve the status of the current WiFi environment.
719
+ * The object returned by this function will have some or all of the following properties:
720
+ * * `isStation` - True if the ESP8266 is being a Station
721
+ * * `isAP` - True if the ESP8266 is being an Access Point.
722
+ * * `connectedStations` - An array of the stations connected to us if we are being an access point. This
723
+ * array may be empty. Each entry in the array will itself be an object describing the station which,
724
+ * at a minimum will contain `ip` being the IP address of the station.
725
+ */
726
+ JsVar * jswrap_ESP8266_wifi_getStatus () {
727
+ os_printf ("> jswrap_ESP8266_wifi_getStatus\n" );
728
+ JsVar * jsWiFiStatus = jspNewObject (NULL , "WiFiStatus" );
729
+ uint8 opMode = wifi_get_opmode ();
730
+ jsvObjectSetChildAndUnLock (
731
+ jsWiFiStatus ,
732
+ "isStation" ,
733
+ jsvNewFromBool (opMode == STATION_MODE || opMode == STATIONAP_MODE ));
734
+ jsvObjectSetChildAndUnLock (
735
+ jsWiFiStatus ,
736
+ "isAP" ,
737
+ jsvNewFromBool (opMode == SOFTAP_MODE || opMode == STATIONAP_MODE ));
738
+ if (opMode == SOFTAP_MODE || opMode == STATIONAP_MODE ) {
739
+ jsvObjectSetChildAndUnLock (
740
+ jsWiFiStatus ,
741
+ "connectedStations" ,
742
+ jswrap_ESP8266WiFi_getConnectedStations ());
743
+ }
744
+ os_printf ("< jswrap_ESP8266_wifi_getStatus\n" );
745
+ return jsWiFiStatus ;
746
+ }
747
+
748
+
711
749
/*JSON{
712
750
"type" : "staticmethod",
713
751
"class" : "wifi",
714
752
"name" : "getIP",
715
753
"generate" : "jswrap_ESP8266_wifi_getIP",
716
- "return" : ["JsVar", "A boolean representing our auto connect status "]
754
+ "return" : ["JsVar", "A boolean representing our IP information "]
717
755
}
718
756
* Return IP information in an object which contains:
719
757
* * ip - IP address
@@ -725,12 +763,12 @@ JsVar *jswrap_ESP8266_wifi_getIP() {
725
763
struct ip_info info ;
726
764
wifi_get_ip_info (0 , & info );
727
765
728
- JsVar * ipInfo = jspNewObject (NULL , "Restart" );
729
- jsvObjectSetChildAndUnLock (ipInfo , "ip" , jsvNewFromInteger (info .ip .addr ));
730
- jsvObjectSetChildAndUnLock (ipInfo , "netmask" , jsvNewFromInteger (info .netmask .addr ));
731
- jsvObjectSetChildAndUnLock (ipInfo , "gw" , jsvNewFromInteger (info .gw .addr ));
766
+ JsVar * jsIpInfo = jspNewObject (NULL , "Restart" );
767
+ jsvObjectSetChildAndUnLock (jsIpInfo , "ip" , jsvNewFromInteger (info .ip .addr ));
768
+ jsvObjectSetChildAndUnLock (jsIpInfo , "netmask" , jsvNewFromInteger (info .netmask .addr ));
769
+ jsvObjectSetChildAndUnLock (jsIpInfo , "gw" , jsvNewFromInteger (info .gw .addr ));
732
770
os_printf ("< jswrap_ESP8266_wifi_getIP\n" );
733
- return ipInfo ;
771
+ return jsIpInfo ;
734
772
}
735
773
736
774
@@ -1868,14 +1906,18 @@ static void scanCB(void *arg, STATUS status) {
1868
1906
*/
1869
1907
1870
1908
os_printf (">> scanCB\n" );
1909
+ if (g_jsScanCallback == NULL ) {
1910
+ os_printf ("<< scanCB\n" );
1911
+ return ;
1912
+ }
1871
1913
1872
1914
// Set the opmode back to the value it was prior to the request for a scan.
1873
1915
assert (g_preWiFiScanMode != -1 );
1874
1916
wifi_set_opmode_current (g_preWiFiScanMode );
1875
1917
g_preWiFiScanMode = -1 ;
1876
1918
1877
1919
// Create the Empty JS array that will be passed as a parameter to the callback.
1878
- JsVar * accessPointArray = jsvNewArray (NULL , 0 );
1920
+ JsVar * jsAccessPointArray = jsvNewArray (NULL , 0 );
1879
1921
struct bss_info * bssInfo ;
1880
1922
1881
1923
bssInfo = (struct bss_info * )arg ;
@@ -1897,29 +1939,38 @@ static void scanCB(void *arg, STATUS status) {
1897
1939
// sint16 freq_offset
1898
1940
// ---
1899
1941
// Create, populate and add a child ...
1900
- JsVar * currentAccessPoint = jspNewObject (NULL , "AccessPoint" );
1901
- jsvObjectSetChildAndUnLock (currentAccessPoint , "rssi" , jsvNewFromInteger (bssInfo -> rssi ));
1902
- jsvObjectSetChildAndUnLock (currentAccessPoint , "channel" , jsvNewFromInteger (bssInfo -> channel ));
1903
- jsvObjectSetChildAndUnLock (currentAccessPoint , "authMode" , jsvNewFromInteger (bssInfo -> authmode ));
1904
- jsvObjectSetChildAndUnLock (currentAccessPoint , "isHidden" , jsvNewFromBool (bssInfo -> is_hidden ));
1942
+ JsVar * jsCurrentAccessPoint = jspNewObject (NULL , "AccessPoint" );
1943
+ jsvObjectSetChildAndUnLock (jsCurrentAccessPoint , "rssi" , jsvNewFromInteger (bssInfo -> rssi ));
1944
+ jsvObjectSetChildAndUnLock (jsCurrentAccessPoint , "channel" , jsvNewFromInteger (bssInfo -> channel ));
1945
+ jsvObjectSetChildAndUnLock (jsCurrentAccessPoint , "authMode" , jsvNewFromInteger (bssInfo -> authmode ));
1946
+ jsvObjectSetChildAndUnLock (jsCurrentAccessPoint , "isHidden" , jsvNewFromBool (bssInfo -> is_hidden ));
1905
1947
// The SSID may **NOT** be NULL terminated ... so handle that.
1906
1948
char ssid [sizeof (bssInfo -> ssid ) + 1 ];
1907
1949
os_strncpy ((char * )ssid , (char * )bssInfo -> ssid , sizeof (bssInfo -> ssid ));
1908
1950
ssid [sizeof (ssid )- 1 ] = '\0' ;
1909
- jsvObjectSetChildAndUnLock (currentAccessPoint , "ssid" , jsvNewFromString (ssid ));
1951
+ jsvObjectSetChildAndUnLock (jsCurrentAccessPoint , "ssid" , jsvNewFromString (ssid ));
1910
1952
1911
1953
// Add the new record to the array
1912
- jsvArrayPush (accessPointArray , currentAccessPoint );
1954
+ jsvArrayPush (jsAccessPointArray , jsCurrentAccessPoint );
1955
+ jsvUnLock (jsCurrentAccessPoint );
1913
1956
1914
1957
os_printf (" - ssid: %s\n" , bssInfo -> ssid );
1915
1958
bssInfo = STAILQ_NEXT (bssInfo , next );
1916
1959
}
1917
1960
1918
1961
// We have now completed the scan callback, so now we can invoke the JS callback.
1919
- JsVar * params [1 ];
1920
- params [0 ] = accessPointArray ;
1921
- jsiQueueEvents (NULL , g_jsScanCallback , params , 1 );
1962
+ // The parameters to the callback are:
1963
+ // * err - An error indication - always null as we can't fail.
1964
+ // * accessPointArray - An array of access point records.
1965
+ JsVar * params [2 ];
1966
+ params [0 ] = jsvNewNull ();
1967
+ params [1 ] = jsAccessPointArray ;
1968
+ jsiQueueEvents (NULL , g_jsScanCallback , params , 2 );
1969
+
1970
+ jsvUnLock (jsAccessPointArray );
1922
1971
jsvUnLock (g_jsScanCallback );
1972
+ jsvUnLock (params [0 ]);
1973
+ g_jsScanCallback = NULL ;
1923
1974
os_printf ("<< scanCB\n" );
1924
1975
}
1925
1976
0 commit comments