Skip to content

Instantly share code, notes, and snippets.

@MaBecker
Last active May 2, 2018 11:31
Show Gist options
  • Save MaBecker/e6600ebd6e647dc4edf32de06b5ca86b to your computer and use it in GitHub Desktop.
Save MaBecker/e6600ebd6e647dc4edf32de06b5ca86b to your computer and use it in GitHub Desktop.
jswrap_wifi_save.c
void jswrap_wifi_restore(void) {
DBG("Wifi.restore\n");
JsVar *name = jsvNewFromString(WIFI_CONFIG_STORAGE_NAME);
JsVar *o = jswrap_storage_readJSON(name);
if (!o) { // no data
jsvUnLock2(name,o);
return;
}
JsVar *v;
v = jsvObjectGetChild(o,"mode",0);
savedMode = jsvGetInteger(v);
jsvUnLock(v);
wifi_set_opmode_current(savedMode);
v = jsvObjectGetChild(o,"phyMode",0);
wifi_set_phy_mode(jsvGetInteger(v));
jsvUnLock(v);
v = jsvObjectGetChild(o,"sleepType",0);
wifi_set_sleep_type(jsvGetInteger(v));
jsvUnLock(v);
if (savedMode & SOFTAP_MODE) {
struct softap_config ap_config;
os_memset(&ap_config, 0, sizeof(ap_config));
v = jsvObjectGetChild(o,"authmodeAP",0);
ap_config.authmode =jsvGetInteger(v);
jsvUnLock(v);
v = jsvObjectGetChild(o,"hiddenAP",0);
ap_config.ssid_hidden = jsvGetInteger(v);
jsvUnLock(v);
v = jsvObjectGetChild(o,"ssid",0);
jsvGetString(v, (char *)ap_config.ssid, sizeof(ap_config.ssid));
ap_config.ssid_len = jsvGetStringLength(v);
jsvUnLock(v);
v = jsvObjectGetChild(o,"passwordAP",0);
jsvGetString(v, (char *)ap_config.password, sizeof(ap_config.password));
jsvUnLock(v);
ap_config.channel = 1;
ap_config.max_connection = 4;
ap_config.beacon_interval = 100;
wifi_softap_set_config_current(&ap_config);
DBG("Wifi.restore: AP=%s\n", ap_config.ssid);
}
if (savedMode & STATION_MODE) {
v = jsvObjectGetChild(o,"hostname",0);
if (v) {
char hostname[64];
jsvGetString(v, hostname, sizeof(hostname));
DBG("Wifi.restore: hostname=%s\n", hostname);
wifi_station_set_hostname(hostname);
}
jsvUnLock(v);
struct station_config sta_config;
os_memset(&sta_config, 0, sizeof(sta_config));
v = jsvObjectGetChild(o,"ssid",0);
jsvGetString(v, (char *)sta_config.ssid, sizeof(sta_config.ssid));
jsvUnLock(v);
v = jsvObjectGetChild(o,"password",0);
jsvGetString(v, (char *)sta_config.password, sizeof(sta_config.password));
jsvUnLock(v);
wifi_station_set_config_current(&sta_config);
DBG("Wifi.restore: STA=%s\n", sta_config.ssid);
wifi_station_connect(); // we're not supposed to call this from user_init but it doesn't harm
// and we need it when invoked from JS
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment