Skip to content

Commit 599a039

Browse files
committed
Use separate keys to store quick connect AP data
What we were doing was clever and saved a few bytes but not backward-compatible and would cause connectivity loss on firmware downgrade.
1 parent 561baac commit 599a039

File tree

2 files changed

+30
-30
lines changed

2 files changed

+30
-30
lines changed

mos.yml

+4-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ config_schema:
3434
- ["wifi.sta.enable", "b", {title: "Connect to existing WiFi"}]
3535
- ["wifi.sta.ssid", "s", {title: "SSID"}]
3636
- ["wifi.sta.pass", "s", {title: "Password", type: "password"}]
37-
- ["wifi.sta.bssid", "s", {title: "Specific AP to connect to. Also used to store BSSID for quick connect (prefixed with '*')."}]
38-
- ["wifi.sta.channel", "i", {title: "Specific channel to use when connecting. Also used to store channel for quick connect (as a negative value)."}]
37+
- ["wifi.sta.bssid", "s", {title: "Specific AP to connect to"}]
38+
- ["wifi.sta.channel", "i", {title: "Specific channel to use when connecting"}]
39+
- ["wifi.sta.last_bssid", "s", {title: "Used to store AP's BSSID for quick connect"}]
40+
- ["wifi.sta.last_channel", "i", {title: "Used to store AP's channel for quick connect"}]
3941
- ["wifi.sta.user", "s", {title: "Username for WPA-PEAP mode"}]
4042
- ["wifi.sta.anon_identity", "s", {title: "Anonymous identity for WPA mode"}]
4143
- ["wifi.sta.cert", "s", {title: "Client certificate for WPA-TTLS mode"}]

src/mgos_wifi_sta.c

+26-28
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ static bool check_ap(const struct mgos_wifi_scan_result *e,
219219
break;
220220
}
221221
// If the config specifies a particular BSSID and/or channel, check them.
222-
if (!mgos_conf_str_empty(cfg->bssid) && cfg->bssid[0] != '*') {
222+
if (!mgos_conf_str_empty(cfg->bssid)) {
223223
char bssid_s[20];
224224
mgos_wifi_sta_bssid_to_str(e->bssid, bssid_s);
225225
if (strcasecmp(cfg->bssid, bssid_s) != 0) {
@@ -492,11 +492,11 @@ static void mgos_wifi_sta_run(int wifi_ev, void *ev_data, bool timeout) {
492492
for (int i = s_num_cfgs - 1; i >= 0; i--) {
493493
struct mgos_config_wifi_sta *cfg = s_cfgs[i];
494494
if (!cfg->enable) continue;
495-
if (mgos_conf_str_empty(cfg->bssid)) continue;
495+
if (mgos_conf_str_empty(cfg->last_bssid) || cfg->last_channel == 0) {
496+
continue;
497+
}
496498
uint8_t bssid[6];
497-
const char *bssid_s = cfg->bssid;
498-
if (bssid_s[0] == '*') bssid_s++;
499-
if (!mgos_wifi_sta_bssid_from_str(bssid_s, bssid)) {
499+
if (!mgos_wifi_sta_bssid_from_str(cfg->last_bssid, bssid)) {
500500
continue;
501501
}
502502
bool found = true;
@@ -509,11 +509,7 @@ static void mgos_wifi_sta_run(int wifi_ev, void *ev_data, bool timeout) {
509509
ape->cfg = cfg;
510510
memcpy(ape->bssid, bssid, sizeof(ape->bssid));
511511
}
512-
if (cfg->channel >= 0) {
513-
ape->channel = cfg->channel;
514-
} else {
515-
ape->channel = -cfg->channel;
516-
}
512+
ape->channel = cfg->last_channel;
517513
if (found) mgos_wifi_sta_remove_history_entry(ape);
518514
SLIST_INSERT_HEAD(&s_ap_queue, ape, next);
519515
}
@@ -644,12 +640,8 @@ static void mgos_wifi_sta_run(int wifi_ev, void *ev_data, bool timeout) {
644640
s_cur_entry = NULL;
645641
// Reset quick connect settings for this config.
646642
{
647-
if (!mgos_conf_str_empty(cfg->bssid) && cfg->bssid[0] == '*') {
648-
mgos_conf_set_str(&cfg->bssid, NULL);
649-
}
650-
if (ape->cfg->channel < 0) {
651-
ape->cfg->channel = 0;
652-
}
643+
mgos_conf_set_str(&cfg->last_bssid, NULL);
644+
ape->cfg->last_channel = 0;
653645
// We do not save config at this point, it will be saved when we
654646
// eventually connect.
655647
}
@@ -681,19 +673,25 @@ static void mgos_wifi_sta_run(int wifi_ev, void *ev_data, bool timeout) {
681673
s_rssi_info.samples[i] = cur_rssi;
682674
}
683675
// Save the AP that we connected to, for quick reconnect.
684-
if (ape->channel != 0 && !BSSID_EMPTY(ape->bssid) &&
685-
cfg->channel <= 0 &&
686-
(mgos_conf_str_empty(cfg->bssid) || cfg->bssid[0] == '*')) {
687-
char bssid_s[20] = {'*'};
688-
mgos_wifi_sta_bssid_to_str(ape->bssid, bssid_s + 1);
689-
bool changed = (-ape->channel != cfg->channel ||
690-
mgos_conf_str_empty(cfg->bssid));
691-
if (!changed) changed = strcasecmp(cfg->bssid, bssid_s);
676+
if (ape->channel != 0 && !BSSID_EMPTY(ape->bssid)) {
677+
char bssid_s[20] = {0};
678+
mgos_wifi_sta_bssid_to_str(ape->bssid, bssid_s);
679+
bool changed = (ape->channel != cfg->last_channel ||
680+
mgos_conf_str_empty(cfg->last_bssid));
681+
if (!changed) changed = strcasecmp(cfg->last_bssid, bssid_s);
692682
if (changed) {
693-
cfg->channel = -ape->channel;
694-
mgos_conf_set_str(&cfg->bssid, bssid_s);
695-
LOG(LL_INFO, ("Saving AP %s %s ch %d", cfg->ssid, bssid_s + 1,
696-
ape->channel));
683+
mgos_conf_set_str(&cfg->last_bssid, bssid_s);
684+
cfg->last_channel = ape->channel;
685+
LOG(LL_INFO,
686+
("Saving AP %s %s ch %d", cfg->ssid, bssid_s, ape->channel));
687+
// Early version of this code was using bssid prefixed with '*' and
688+
// negative channel to store quick connect values.
689+
// This was a bad idea that broke backward compatibility,
690+
// we no longer do this and clean up such values when possible.
691+
if (cfg->channel < 0) cfg->channel = 0;
692+
if (!mgos_conf_str_empty(cfg->bssid) && cfg->bssid[0] == '*') {
693+
mgos_conf_set_str(&cfg->bssid, NULL);
694+
}
697695
mgos_sys_config_save(&mgos_sys_config, false /* try_once */,
698696
NULL /* msg */);
699697
}

0 commit comments

Comments
 (0)