Skip to content

Commit 3c84fd7

Browse files
kapilkedawatespressif-bot
authored andcommitted
fix(esp_wifi): Make sure old DPP listen is cancelled
1 parent ab9304d commit 3c84fd7

File tree

6 files changed

+48
-18
lines changed

6 files changed

+48
-18
lines changed

components/esp_wifi/include/esp_wifi_types_generic.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1525,7 +1525,7 @@ typedef struct {
15251525

15261526
/** Argument structure for WIFI_EVENT_DPP_URI_READY event */
15271527
typedef struct {
1528-
uint32_t uri_len; /**< URI length */
1528+
uint32_t uri_data_len; /**< URI data length including null termination */
15291529
char uri[]; /**< URI data */
15301530
} wifi_event_dpp_uri_ready_t;
15311531

components/esp_wifi/lib

Submodule lib updated from a854d20 to bef6a32

components/wpa_supplicant/esp_supplicant/src/esp_dpp.c

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,11 @@ struct action_rx_param {
3232
};
3333

3434
static void *s_dpp_api_lock = NULL;
35+
static void *s_dpp_event_group = NULL;
3536

36-
static atomic_bool s_dpp_listen_in_progress;
37+
#define DPP_ROC_EVENT_HANDLED BIT0
38+
39+
static atomic_bool roc_in_progress;
3740
static struct esp_dpp_context_t s_dpp_ctx;
3841
static int esp_supp_rx_action(uint8_t *hdr, uint8_t *payload, size_t len, uint8_t channel);
3942
static wifi_action_rx_cb_t s_action_rx_cb = esp_supp_rx_action;
@@ -42,6 +45,7 @@ static void tx_status_handler(void *arg, esp_event_base_t event_base,
4245
int32_t event_id, void *event_data);
4346
static void roc_status_handler(void *arg, esp_event_base_t event_base,
4447
int32_t event_id, void *event_data);
48+
static void dpp_listen_next_channel(void *data, void *user_ctx);
4549
static esp_err_t dpp_api_lock(void)
4650
{
4751
if (!s_dpp_api_lock) {
@@ -103,9 +107,11 @@ static int listen_stop_handler(void *data, void *user_ctx)
103107
{
104108
wifi_roc_req_t req = {0};
105109

106-
atomic_store(&s_dpp_listen_in_progress, false);
110+
wpa_printf(MSG_DEBUG, "DPP: Stoping ROC");
107111
req.ifx = WIFI_IF_STA;
108112
req.type = WIFI_ROC_CANCEL;
113+
eloop_cancel_timeout(dpp_listen_next_channel, NULL, NULL);
114+
s_dpp_ctx.dpp_listen_ongoing = false;
109115

110116
esp_wifi_remain_on_channel(&req);
111117

@@ -328,7 +334,7 @@ static int esp_dpp_handle_config_obj(struct dpp_authentication *auth,
328334
wpa_printf(MSG_INFO, DPP_EVENT_CONNECTOR "%s",
329335
conf->connector);
330336
}
331-
if (atomic_load(&s_dpp_listen_in_progress)) {
337+
if (atomic_load(&roc_in_progress)) {
332338
listen_stop_handler(NULL, NULL);
333339
}
334340
/* deinit AUTH since authentication is done */
@@ -589,7 +595,7 @@ static void esp_dpp_rx_action(void *data, void *user_ctx)
589595
(size_t)(public_action->v.pa_vendor_spec.vendor_data -
590596
(u8 *)rx_param->action_frm);
591597

592-
if (atomic_load(&s_dpp_listen_in_progress)) {
598+
if (atomic_load(&roc_in_progress)) {
593599
listen_stop_handler(NULL, NULL);
594600
}
595601

@@ -615,18 +621,22 @@ static void esp_dpp_rx_action(void *data, void *user_ctx)
615621
}
616622
}
617623

618-
void esp_dpp_listen_next_channel(void *data, void *user_ctx)
624+
static void dpp_listen_next_channel(void *data, void *user_ctx)
619625
{
620626
struct dpp_bootstrap_params_t *p = &s_dpp_ctx.bootstrap_params;
621627
static int counter;
622628
int channel;
623629
esp_err_t ret = 0;
624630
wifi_roc_req_t req = {0};
625631

632+
if (!s_dpp_ctx.dpp_listen_ongoing) {
633+
return;
634+
}
626635
if (p->num_chan <= 0) {
627636
wpa_printf(MSG_ERROR, "Listen channel not set");
628637
return;
629638
}
639+
630640
channel = p->chan_list[counter++ % p->num_chan];
631641

632642
wpa_printf(MSG_DEBUG, "DPP: Starting ROC on channel %d", channel);
@@ -641,6 +651,7 @@ void esp_dpp_listen_next_channel(void *data, void *user_ctx)
641651
wpa_printf(MSG_ERROR, "Failed ROC. error : 0x%x", ret);
642652
return;
643653
}
654+
os_event_group_clear_bits(s_dpp_event_group, DPP_ROC_EVENT_HANDLED);
644655
}
645656

646657
static void esp_dpp_bootstrap_gen(void *data, void *user_ctx)
@@ -666,9 +677,9 @@ static void esp_dpp_bootstrap_gen(void *data, void *user_ctx)
666677
if (!event) {
667678
return;
668679
}
669-
event->uri_len = os_strlen(uri);
670-
os_memcpy(event->uri, uri, event->uri_len);
671-
event->uri[event->uri_len++] = '\0';
680+
event->uri_data_len = os_strlen(uri);
681+
os_memcpy(event->uri, uri, event->uri_data_len);
682+
event->uri[event->uri_data_len++] = '\0';
672683
esp_event_post(WIFI_EVENT, WIFI_EVENT_DPP_URI_READY, event, len, OS_BLOCK);
673684
os_free(event);
674685
os_free(command);
@@ -714,6 +725,10 @@ static int esp_dpp_deinit(void *data, void *user_ctx)
714725
s_dpp_ctx.dpp_init_done = false;
715726
s_dpp_ctx.bootstrap_done = false;
716727
s_dpp_ctx.dpp_event_cb = NULL;
728+
if (s_dpp_event_group) {
729+
os_event_group_delete(s_dpp_event_group);
730+
s_dpp_event_group = NULL;
731+
}
717732

718733
return 0;
719734
}
@@ -808,16 +823,21 @@ static void tx_status_handler(void *arg, esp_event_base_t event_base,
808823
eloop_register_timeout(ESP_GAS_TIMEOUT_SECS, 0, gas_query_timeout, NULL, auth);
809824
}
810825
}
826+
atomic_store(&roc_in_progress, true);
811827
}
812828

813829
static void roc_status_handler(void *arg, esp_event_base_t event_base,
814830
int32_t event_id, void *event_data)
815831
{
816832
wifi_event_roc_done_t *evt = (wifi_event_roc_done_t *)event_data;
817833

818-
if (atomic_load(&s_dpp_listen_in_progress) && evt->context == (uint32_t)s_action_rx_cb) {
819-
eloop_register_timeout(0, 0, esp_dpp_listen_next_channel, NULL, NULL);
834+
if (evt->context == (uint32_t)s_action_rx_cb) {
835+
eloop_cancel_timeout(dpp_listen_next_channel, NULL, NULL);
836+
eloop_register_timeout(0, 0, dpp_listen_next_channel, NULL, NULL);
820837
}
838+
839+
atomic_store(&roc_in_progress, false);
840+
os_event_group_set_bits(s_dpp_event_group, DPP_ROC_EVENT_HANDLED);
821841
}
822842

823843
static char *esp_dpp_parse_chan_list(const char *chan_list)
@@ -977,6 +997,12 @@ esp_supp_dpp_bootstrap_gen(const char *chan_list, enum dpp_bootstrap_type type,
977997
return ret;
978998
}
979999

1000+
static void dpp_listen_start(void *ctx, void *data)
1001+
{
1002+
s_dpp_ctx.dpp_listen_ongoing = true;
1003+
dpp_listen_next_channel(NULL, NULL);
1004+
}
1005+
9801006
esp_err_t esp_supp_dpp_start_listen(void)
9811007
{
9821008
int ret = dpp_api_lock();
@@ -994,12 +1020,14 @@ esp_err_t esp_supp_dpp_start_listen(void)
9941020
wpa_printf(MSG_ERROR, "DPP: ROC not possible before wifi is started");
9951021
return ESP_ERR_INVALID_STATE;
9961022
}
997-
wpa_printf(MSG_DEBUG, "DPP: Starting ROC");
9981023

9991024
/* cancel previous ROC if ongoing */
10001025
esp_supp_dpp_stop_listen();
1001-
atomic_store(&s_dpp_listen_in_progress, true);
1002-
eloop_register_timeout(0, 0, esp_dpp_listen_next_channel, NULL, NULL);
1026+
1027+
/* Give ample time to set the bit, timeout is necessary when ROC is not running previously */
1028+
os_event_group_wait_bits(s_dpp_event_group, DPP_ROC_EVENT_HANDLED, 0, 0, os_task_ms_to_tick(100));
1029+
wpa_printf(MSG_DEBUG, "DPP: Starting ROC");
1030+
eloop_register_timeout(0, 0, dpp_listen_start, NULL, NULL);
10031031
return 0;
10041032
}
10051033

@@ -1045,7 +1073,6 @@ static int esp_dpp_init(void *eloop_data, void *user_ctx)
10451073
goto init_fail;
10461074
}
10471075

1048-
atomic_store(&s_dpp_listen_in_progress, false);
10491076
s_dpp_ctx.dpp_event_cb = cb;
10501077

10511078
if (cb) {
@@ -1061,6 +1088,7 @@ static int esp_dpp_init(void *eloop_data, void *user_ctx)
10611088
esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_ROC_DONE,
10621089
&roc_status_handler, NULL);
10631090

1091+
s_dpp_event_group = os_event_group_create();
10641092
wpa_printf(MSG_INFO, "DPP: dpp init done");
10651093
s_dpp_ctx.dpp_init_done = true;
10661094

components/wpa_supplicant/esp_supplicant/src/esp_dpp_i.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ struct esp_dpp_context_t {
4040
int id;
4141
bool dpp_init_done;
4242
bool bootstrap_done;
43+
bool dpp_listen_ongoing;
4344
};
4445

4546
#ifdef CONFIG_TESTING_OPTIONS

components/wpa_supplicant/port/include/os.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ extern const wifi_osi_funcs_t *wifi_funcs;
376376
#define os_timer_get_time(void) wifi_funcs->_esp_timer_get_time(void)
377377

378378
#define os_event_group_create(void) wifi_funcs->_event_group_create(void)
379-
#define os_event_group_delete(void) wifi_funcs->_event_group_delete(void)
379+
#define os_event_group_delete(a) wifi_funcs->_event_group_delete((a))
380380
#define os_event_group_wait_bits(a, b, c, d, e) wifi_funcs->_event_group_wait_bits((a), (b), (c), (d), (e))
381381
#define os_event_group_clear_bits(a, b) wifi_funcs->_event_group_clear_bits((a), (b))
382382
#define os_event_group_set_bits(a, b) wifi_funcs->_event_group_set_bits((a), (b))

examples/wifi/wifi_easy_connect/dpp-enrollee/main/dpp_enrollee_main.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,8 @@ void dpp_enrollee_init(void)
175175

176176
/* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
177177
* happened. */
178-
if (bits & DPP_CONNECT_FAIL_BIT) {
178+
if (bits & DPP_CONNECTED_BIT) {
179+
} else if (bits & DPP_CONNECT_FAIL_BIT) {
179180
ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s",
180181
s_dpp_wifi_config.sta.ssid, s_dpp_wifi_config.sta.password);
181182
} else if (bits & DPP_AUTH_FAIL_BIT) {

0 commit comments

Comments
 (0)