mirror of
https://github.com/espruino/Espruino.git
synced 2025-12-08 19:06:15 +00:00
Addition of kill support for unlocking vars. #603
This commit is contained in:
parent
d0fed2be6d
commit
ebd6d90935
@ -44,13 +44,13 @@ static void setupJsNetwork();
|
||||
static void pingRecvCB();
|
||||
static char *wifiConnectStatusToString(uint8 status);
|
||||
|
||||
static JsVar *jsScanCallback;
|
||||
static JsVar *jsWiFiEventCallback;
|
||||
static JsVar *g_jsScanCallback;
|
||||
static JsVar *g_jsWiFiEventCallback;
|
||||
|
||||
// A callback function to be invoked when we have an IP address.
|
||||
static JsVar *g_jsGotIpCallback;
|
||||
|
||||
static JsVar *jsPingCallback;
|
||||
static JsVar *g_jsPingCallback;
|
||||
|
||||
// Global data structure for ping request
|
||||
static struct ping_option pingOpt;
|
||||
@ -88,7 +88,19 @@ static char *wifiPhy[] = { 0, "11b", "11g", "11n" };
|
||||
* Register a handler to be called when the ESP8266 is reset.
|
||||
*/
|
||||
void jswrap_ESP8266WiFi_kill() {
|
||||
// When the Espruino environment is reset, this callback function will be invoked.
|
||||
// The purpose is to reset the environment by cleaning up whatever might be needed
|
||||
// to be cleaned up.
|
||||
os_printf("> jswrap_esp8266_kill\n");
|
||||
|
||||
// Handle the g_jsGotIpCallback - If it is not null, then it contains a reference
|
||||
// to a locked JS variable that is a function pointer for the "got ip" callback.
|
||||
// We want to unlock this and delete the reference.
|
||||
if (g_jsGotIpCallback != NULL) {
|
||||
jsvUnLock(g_jsGotIpCallback);
|
||||
g_jsGotIpCallback = NULL;
|
||||
}
|
||||
|
||||
os_printf("< jswrap_esp8266_kill\n");
|
||||
}
|
||||
|
||||
@ -133,9 +145,9 @@ void jswrap_ESP8266WiFi_connect(
|
||||
gotIpCallback = NULL;
|
||||
}
|
||||
|
||||
// Set the global which is the gotIP callback to null
|
||||
// Set the global which is the gotIP callback to null but first unlock it.
|
||||
if (g_jsGotIpCallback != NULL) {
|
||||
//jsvUnLock(g_jsGotIpCallback);
|
||||
jsvUnLock(g_jsGotIpCallback);
|
||||
g_jsGotIpCallback = NULL;
|
||||
}
|
||||
|
||||
@ -308,7 +320,7 @@ void jswrap_ESP8266WiFi_getAccessPoints(
|
||||
}
|
||||
|
||||
// Save the callback for the scan in the global variable called jsScanCallback.
|
||||
jsScanCallback = jsvLockAgainSafe(callback);
|
||||
g_jsScanCallback = jsvLockAgainSafe(callback);
|
||||
|
||||
// Ask the ESP8266 to perform a network scan after first entering
|
||||
// station mode. The network scan will eventually result in a callback
|
||||
@ -369,10 +381,10 @@ void jswrap_ESP8266WiFi_onWiFiEvent(
|
||||
) {
|
||||
// If the callback is null
|
||||
if (callback == NULL || jsvIsNull(callback)) {
|
||||
if (jsWiFiEventCallback != NULL) {
|
||||
jsvUnLock(jsWiFiEventCallback);
|
||||
if (g_jsWiFiEventCallback != NULL) {
|
||||
jsvUnLock(g_jsWiFiEventCallback);
|
||||
}
|
||||
jsWiFiEventCallback = NULL;
|
||||
g_jsWiFiEventCallback = NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
@ -383,12 +395,12 @@ void jswrap_ESP8266WiFi_onWiFiEvent(
|
||||
|
||||
// We are about to save a new global WiFi even callback handler. If we have previously
|
||||
// had one, we need to unlock it so that we don't leak memory.
|
||||
if (jsWiFiEventCallback != NULL) {
|
||||
jsvUnLock(jsWiFiEventCallback);
|
||||
if (g_jsWiFiEventCallback != NULL) {
|
||||
jsvUnLock(g_jsWiFiEventCallback);
|
||||
}
|
||||
|
||||
// Save the global WiFi event callback handler.
|
||||
jsWiFiEventCallback = jsvLockAgainSafe(callback);
|
||||
g_jsWiFiEventCallback = jsvLockAgainSafe(callback);
|
||||
}
|
||||
|
||||
|
||||
@ -865,19 +877,19 @@ void jswrap_ESP8266WiFi_ping(
|
||||
}
|
||||
|
||||
if (jsvIsUndefined(pingCallback) || jsvIsNull(pingCallback)) {
|
||||
if (jsPingCallback != NULL) {
|
||||
jsvUnLock(jsPingCallback);
|
||||
if (g_jsPingCallback != NULL) {
|
||||
jsvUnLock(g_jsPingCallback);
|
||||
}
|
||||
jsPingCallback = NULL;
|
||||
g_jsPingCallback = NULL;
|
||||
} else if (!jsvIsFunction(pingCallback)) {
|
||||
jsExceptionHere(JSET_ERROR, "Callback is not a function.");
|
||||
return;
|
||||
} else {
|
||||
if (jsPingCallback != NULL) {
|
||||
jsvUnLock(jsPingCallback);
|
||||
if (g_jsPingCallback != NULL) {
|
||||
jsvUnLock(g_jsPingCallback);
|
||||
}
|
||||
jsPingCallback = pingCallback;
|
||||
jsvLockAgainSafe(jsPingCallback);
|
||||
g_jsPingCallback = pingCallback;
|
||||
jsvLockAgainSafe(g_jsPingCallback);
|
||||
}
|
||||
|
||||
// We now have an IP address to ping ... so ping.
|
||||
@ -942,7 +954,7 @@ static void setupJsNetwork() {
|
||||
static void pingRecvCB(void *pingOpt, void *pingResponse) {
|
||||
struct ping_resp *pingResp = (struct ping_resp *)pingResponse;
|
||||
os_printf("Received a ping response!\n");
|
||||
if (jsPingCallback != NULL) {
|
||||
if (g_jsPingCallback != NULL) {
|
||||
JsVar *jsPingResponse = jspNewObject(NULL, "PingResponse");
|
||||
jsvUnLock(jsvObjectSetChild(jsPingResponse, "totalCount", jsvNewFromInteger(pingResp->total_count)));
|
||||
jsvUnLock(jsvObjectSetChild(jsPingResponse, "totalBytes", jsvNewFromInteger(pingResp->total_bytes)));
|
||||
@ -954,7 +966,7 @@ static void pingRecvCB(void *pingOpt, void *pingResponse) {
|
||||
jsvUnLock(jsvObjectSetChild(jsPingResponse, "error", jsvNewFromInteger(pingResp->ping_err)));
|
||||
JsVar *params[1];
|
||||
params[0] = jsPingResponse;
|
||||
jsiQueueEvents(NULL, jsPingCallback, params, 1);
|
||||
jsiQueueEvents(NULL, g_jsPingCallback, params, 1);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1020,8 +1032,8 @@ static void scanCB(void *arg, STATUS status) {
|
||||
// We have now completed the scan callback, so now we can invoke the JS callback.
|
||||
JsVar *params[1];
|
||||
params[0] = accessPointArray;
|
||||
jsiQueueEvents(NULL, jsScanCallback, params, 1);
|
||||
jsvUnLock(jsScanCallback);
|
||||
jsiQueueEvents(NULL, g_jsScanCallback, params, 1);
|
||||
jsvUnLock(g_jsScanCallback);
|
||||
os_printf("<< scanCB\n");
|
||||
}
|
||||
|
||||
@ -1033,12 +1045,12 @@ static void scanCB(void *arg, STATUS status) {
|
||||
static void sendWifiEvent(uint32 eventType, JsVar *details) {
|
||||
// We need to check that we actually have an event callback handler because
|
||||
// it might have been disabled/removed.
|
||||
if (jsWiFiEventCallback != NULL) {
|
||||
if (g_jsWiFiEventCallback != NULL) {
|
||||
// Build a callback event.
|
||||
JsVar *params[2];
|
||||
params[0] = jsvNewFromInteger(eventType);
|
||||
params[1] = details;
|
||||
jsiQueueEvents(NULL, jsWiFiEventCallback, params, 2);
|
||||
jsiQueueEvents(NULL, g_jsWiFiEventCallback, params, 2);
|
||||
}
|
||||
|
||||
if (g_jsGotIpCallback != NULL && eventType == EVENT_STAMODE_GOT_IP) {
|
||||
|
||||
@ -18,30 +18,30 @@
|
||||
#define LIBS_NETWORK_ESP8266_JSWRAP_ESP8266_H_
|
||||
#include "jsvar.h"
|
||||
|
||||
void jswrap_ESP8266WiFi_beAccessPoint(JsVar *jsv_ssid, JsVar *jsv_password);
|
||||
void jswrap_ESP8266WiFi_connect(JsVar *jsv_ssid, JsVar *jsv_password, JsVar *gotIpCallback);
|
||||
void jswrap_ESP8266WiFi_getAccessPoints(JsVar *callback);
|
||||
void jswrap_ESP8266WiFi_disconnect();
|
||||
void jswrap_ESP8266WiFi_restart();
|
||||
JsVar *jswrap_ESP8266WiFi_getRstInfo();
|
||||
JsVar *jswrap_ESP8266WiFi_getIPInfo();
|
||||
void jswrap_ESP8266WiFi_setAutoConnect(JsVar *autoconnect);
|
||||
JsVar *jswrap_ESP8266WiFi_getAutoConnect();
|
||||
JsVar *jswrap_ESP8266WiFi_getStationConfig();
|
||||
void jswrap_ESP8266WiFi_onWiFiEvent(JsVar *callback);
|
||||
void jswrap_ESP8266WiFi_dumpSocket(JsVar *socketId);
|
||||
void jswrap_ESP8266WiFi_getAccessPoints(JsVar *callback);
|
||||
JsVar *jswrap_ESP8266WiFi_getAddressAsString(JsVar *address);
|
||||
void jswrap_ESP8266WiFi_init();
|
||||
JsVar *jswrap_ESP8266WiFi_getAutoConnect();
|
||||
JsVar *jswrap_ESP8266WiFi_getConnectedStations();
|
||||
JsVar *jswrap_ESP8266WiFi_getConnectStatus();
|
||||
JsVar *jswrap_ESP8266WiFi_getDHCPHostname();
|
||||
JsVar *jswrap_ESP8266WiFi_getIPInfo();
|
||||
JsVar *jswrap_ESP8266WiFi_getRSSI();
|
||||
JsVar *jswrap_ESP8266WiFi_getRstInfo();
|
||||
JsVar *jswrap_ESP8266WiFi_getState();
|
||||
JsVar *jswrap_ESP8266WiFi_getStationConfig();
|
||||
void jswrap_ESP8266WiFi_init();
|
||||
void jswrap_ESP8266WiFi_kill();
|
||||
void jswrap_ESP8266WiFi_onWiFiEvent(JsVar *callback);
|
||||
void jswrap_ESP8266WiFi_ping(JsVar *ipAddr, JsVar *pingCallback);
|
||||
void jswrap_ESP8266WiFi_restart();
|
||||
void jswrap_ESP8266WiFi_setAutoConnect(JsVar *autoconnect);
|
||||
void jswrap_ESP8266WiFi_setDHCPHostname(JsVar *jsHostname);
|
||||
JsVar *jswrap_ESP8266WiFi_socketConnect(JsVar *options, JsVar *callback);
|
||||
void jswrap_ESP8266WiFi_socketEnd(JsVar *socket, JsVar *data);
|
||||
void jswrap_ESP8266WiFi_ping(JsVar *ipAddr, JsVar *pingCallback);
|
||||
void jswrap_ESP8266WiFi_beAccessPoint(JsVar *jsv_ssid, JsVar *jsv_password);
|
||||
JsVar *jswrap_ESP8266WiFi_getConnectedStations();
|
||||
JsVar *jswrap_ESP8266WiFi_getRSSI();
|
||||
JsVar *jswrap_ESP8266WiFi_getState();
|
||||
void jswrap_ESP8266WiFi_dumpSocket(JsVar *socketId);
|
||||
void jswrap_ESP8266WiFi_updateCPUFreq(JsVar *jsFreq);
|
||||
void jswrap_ESP8266WiFi_kill();
|
||||
void jswrap_ESP8266WiFi_setDHCPHostname(JsVar *jsHostname);
|
||||
JsVar *jswrap_ESP8266WiFi_getDHCPHostname();
|
||||
|
||||
#endif /* LIBS_NETWORK_ESP8266_JSWRAP_ESP8266_H_ */
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user