diff --git a/examples/provisioning/ble_prov/main/app_main.c b/examples/provisioning/ble_prov/main/app_main.c index 37e669c9d0..6341c9fe3d 100644 --- a/examples/provisioning/ble_prov/main/app_main.c +++ b/examples/provisioning/ble_prov/main/app_main.c @@ -82,9 +82,7 @@ static void wifi_init_sta() ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, event_handler, NULL)); ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, event_handler, NULL)); - /* Start wifi in station mode with credentials set during provisioning */ - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + /* Start Wi-Fi in station mode with credentials set during provisioning */ ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); ESP_ERROR_CHECK(esp_wifi_start()); } @@ -121,6 +119,13 @@ void app_main() * main app and the provisioning service */ ESP_ERROR_CHECK(esp_event_loop_create_default()); + /* Initialize NVS needed by Wi-Fi */ + ESP_ERROR_CHECK(nvs_flash_init()); + + /* Initialize Wi-Fi with default config */ + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + /* Check if device is provisioned */ bool provisioned; if (app_prov_is_provisioned(&provisioned) != ESP_OK) { @@ -135,6 +140,6 @@ void app_main() } else { /* Else start as station with credentials set during provisioning */ ESP_LOGI(TAG, "Starting WiFi station"); - wifi_init_sta(NULL); + wifi_init_sta(); } } diff --git a/examples/provisioning/ble_prov/main/app_prov.c b/examples/provisioning/ble_prov/main/app_prov.c index a416a2d3f1..be9b0d2f81 100644 --- a/examples/provisioning/ble_prov/main/app_prov.c +++ b/examples/provisioning/ble_prov/main/app_prov.c @@ -137,7 +137,7 @@ static void app_prov_stop_service(void) /* Remove event handler */ esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, app_prov_event_handler); - esp_event_handler_unregister(IP_EVENT, ESP_EVENT_ANY_ID, app_prov_event_handler); + esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, app_prov_event_handler); /* Release memory used by BT stack */ esp_bt_mem_release(ESP_BT_MODE_BTDM); @@ -257,17 +257,6 @@ esp_err_t app_prov_is_provisioned(bool *provisioned) nvs_flash_erase(); #endif - if (nvs_flash_init() != ESP_OK) { - ESP_LOGE(TAG, "Failed to init NVS"); - return ESP_FAIL; - } - - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - if (esp_wifi_init(&cfg) != ESP_OK) { - ESP_LOGE(TAG, "Failed to init wifi"); - return ESP_FAIL; - } - /* Get WiFi Station configuration */ wifi_config_t wifi_cfg; if (esp_wifi_get_config(ESP_IF_WIFI_STA, &wifi_cfg) != ESP_OK) { @@ -284,12 +273,6 @@ esp_err_t app_prov_is_provisioned(bool *provisioned) esp_err_t app_prov_configure_sta(wifi_config_t *wifi_cfg) { - /* Initialize WiFi with default config */ - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - if (esp_wifi_init(&cfg) != ESP_OK) { - ESP_LOGE(TAG, "Failed to init WiFi"); - return ESP_FAIL; - } /* Configure WiFi as station */ if (esp_wifi_set_mode(WIFI_MODE_STA) != ESP_OK) { ESP_LOGE(TAG, "Failed to set WiFi mode"); @@ -358,7 +341,7 @@ esp_err_t app_prov_start_ble_provisioning(int security, const protocomm_security return err; } - err = esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, app_prov_event_handler, NULL); + err = esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, app_prov_event_handler, NULL); if (err != ESP_OK) { ESP_LOGE(TAG, "Failed to register IP event handler"); return err; diff --git a/examples/provisioning/console_prov/main/app_main.c b/examples/provisioning/console_prov/main/app_main.c index a0ebf13cd7..b149e5cee5 100644 --- a/examples/provisioning/console_prov/main/app_main.c +++ b/examples/provisioning/console_prov/main/app_main.c @@ -53,9 +53,7 @@ static void wifi_init_sta() ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, event_handler, NULL)); ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, event_handler, NULL)); - /* Start wifi in station mode with credentials set during provisioning */ - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + /* Start Wi-Fi in station mode with credentials set during provisioning */ ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); ESP_ERROR_CHECK(esp_wifi_start()); } @@ -92,6 +90,13 @@ void app_main() * main app and the provisioning service */ ESP_ERROR_CHECK(esp_event_loop_create_default()); + /* Initialize NVS needed by Wi-Fi */ + ESP_ERROR_CHECK(nvs_flash_init()); + + /* Initialize Wi-Fi with default config */ + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + /* Check if device is provisioned */ bool provisioned; if (app_prov_is_provisioned(&provisioned) != ESP_OK) { @@ -106,6 +111,6 @@ void app_main() } else { /* Else start as station with credentials set during provisioning */ ESP_LOGI(TAG, "Starting WiFi station"); - wifi_init_sta(NULL); + wifi_init_sta(); } } diff --git a/examples/provisioning/console_prov/main/app_prov.c b/examples/provisioning/console_prov/main/app_prov.c index f80576c22d..ce3e520430 100644 --- a/examples/provisioning/console_prov/main/app_prov.c +++ b/examples/provisioning/console_prov/main/app_prov.c @@ -106,7 +106,7 @@ static void app_prov_stop_service(void) /* Remove event handler */ esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, app_prov_event_handler); - esp_event_handler_unregister(IP_EVENT, ESP_EVENT_ANY_ID, app_prov_event_handler); + esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, app_prov_event_handler); } /* Task spawned by timer callback */ @@ -223,17 +223,6 @@ esp_err_t app_prov_is_provisioned(bool *provisioned) nvs_flash_erase(); #endif - if (nvs_flash_init() != ESP_OK) { - ESP_LOGE(TAG, "Failed to init NVS"); - return ESP_FAIL; - } - - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - if (esp_wifi_init(&cfg) != ESP_OK) { - ESP_LOGE(TAG, "Failed to init wifi"); - return ESP_FAIL; - } - /* Get WiFi Station configuration */ wifi_config_t wifi_cfg; if (esp_wifi_get_config(ESP_IF_WIFI_STA, &wifi_cfg) != ESP_OK) { @@ -250,12 +239,6 @@ esp_err_t app_prov_is_provisioned(bool *provisioned) esp_err_t app_prov_configure_sta(wifi_config_t *wifi_cfg) { - /* Initialize WiFi with default config */ - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - if (esp_wifi_init(&cfg) != ESP_OK) { - ESP_LOGE(TAG, "Failed to init WiFi"); - return ESP_FAIL; - } /* Configure WiFi as station */ if (esp_wifi_set_mode(WIFI_MODE_STA) != ESP_OK) { ESP_LOGE(TAG, "Failed to set WiFi mode"); @@ -324,7 +307,7 @@ esp_err_t app_prov_start_console_provisioning(int security, const protocomm_secu return err; } - err = esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, app_prov_event_handler, NULL); + err = esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, app_prov_event_handler, NULL); if (err != ESP_OK) { ESP_LOGE(TAG, "Failed to register IP event handler"); return err; diff --git a/examples/provisioning/custom_config/main/app_main.c b/examples/provisioning/custom_config/main/app_main.c index a59047d4c5..583894e767 100644 --- a/examples/provisioning/custom_config/main/app_main.c +++ b/examples/provisioning/custom_config/main/app_main.c @@ -53,9 +53,7 @@ static void wifi_init_sta() ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, event_handler, NULL)); ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, event_handler, NULL)); - /* Start wifi in station mode with credentials set during provisioning */ - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + /* Start Wi-Fi in station mode with credentials set during provisioning */ ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); ESP_ERROR_CHECK(esp_wifi_start()); } @@ -93,6 +91,13 @@ void app_main() * main app and the provisioning service */ ESP_ERROR_CHECK(esp_event_loop_create_default()); + /* Initialize NVS needed by Wi-Fi */ + ESP_ERROR_CHECK(nvs_flash_init()); + + /* Initialize Wi-Fi with default config */ + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + /* Check if device is provisioned */ bool provisioned; if (app_prov_is_provisioned(&provisioned) != ESP_OK) { @@ -107,6 +112,6 @@ void app_main() } else { /* Start WiFi station with credentials set during provisioning */ ESP_LOGI(TAG, "Starting WiFi station"); - wifi_init_sta(NULL); + wifi_init_sta(); } } diff --git a/examples/provisioning/custom_config/main/app_prov.c b/examples/provisioning/custom_config/main/app_prov.c index 44f395f328..e7bb4086c8 100644 --- a/examples/provisioning/custom_config/main/app_prov.c +++ b/examples/provisioning/custom_config/main/app_prov.c @@ -121,7 +121,7 @@ static void app_prov_stop_service(void) /* Remove event handler */ esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, app_prov_event_handler); - esp_event_handler_unregister(IP_EVENT, ESP_EVENT_ANY_ID, app_prov_event_handler); + esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, app_prov_event_handler); } /* Task spawned by timer callback */ @@ -248,17 +248,6 @@ esp_err_t app_prov_is_provisioned(bool *provisioned) nvs_flash_erase(); #endif - if (nvs_flash_init() != ESP_OK) { - ESP_LOGE(TAG, "Failed to init NVS"); - return ESP_FAIL; - } - - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - if (esp_wifi_init(&cfg) != ESP_OK) { - ESP_LOGE(TAG, "Failed to init wifi"); - return ESP_FAIL; - } - /* Get WiFi Station configuration */ wifi_config_t wifi_cfg; if (esp_wifi_get_config(ESP_IF_WIFI_STA, &wifi_cfg) != ESP_OK) { @@ -306,14 +295,6 @@ esp_err_t app_prov_configure_sta(wifi_config_t *wifi_cfg) static esp_err_t start_wifi_ap(const char *ssid, const char *pass) { - /* Initialize WiFi with default configuration */ - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - esp_err_t err = esp_wifi_init(&cfg); - if (err != ESP_OK) { - ESP_LOGE(TAG, "Failed to init WiFi : %d", err); - return err; - } - /* Build WiFi configuration for AP mode */ wifi_config_t wifi_config = { .ap = { @@ -333,7 +314,7 @@ static esp_err_t start_wifi_ap(const char *ssid, const char *pass) } /* Start WiFi in AP mode with configuration built above */ - err = esp_wifi_set_mode(WIFI_MODE_AP); + esp_err_t err = esp_wifi_set_mode(WIFI_MODE_AP); if (err != ESP_OK) { ESP_LOGE(TAG, "Failed to set WiFi mode : %d", err); return err; @@ -392,7 +373,7 @@ esp_err_t app_prov_start_softap_provisioning(const char *ssid, const char *pass, return err; } - err = esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, app_prov_event_handler, NULL); + err = esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, app_prov_event_handler, NULL); if (err != ESP_OK) { ESP_LOGE(TAG, "Failed to register IP event handler"); return err; diff --git a/examples/provisioning/softap_prov/main/app_main.c b/examples/provisioning/softap_prov/main/app_main.c index e0b104ea33..4037f5091a 100644 --- a/examples/provisioning/softap_prov/main/app_main.c +++ b/examples/provisioning/softap_prov/main/app_main.c @@ -53,9 +53,7 @@ static void wifi_init_sta() ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, event_handler, NULL)); ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, event_handler, NULL)); - /* Start wifi in station mode with credentials set during provisioning */ - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + /* Start Wi-Fi in station mode with credentials set during provisioning */ ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); ESP_ERROR_CHECK(esp_wifi_start()); } @@ -108,6 +106,13 @@ void app_main() * main app and the provisioning service */ ESP_ERROR_CHECK(esp_event_loop_create_default()); + /* Initialize NVS needed by Wi-Fi */ + ESP_ERROR_CHECK(nvs_flash_init()); + + /* Initialize Wi-Fi with default config */ + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + /* Check if device is provisioned */ bool provisioned; if (app_prov_is_provisioned(&provisioned) != ESP_OK) { @@ -122,6 +127,6 @@ void app_main() } else { /* Start WiFi station with credentials set during provisioning */ ESP_LOGI(TAG, "Starting WiFi station"); - wifi_init_sta(NULL); + wifi_init_sta(); } } diff --git a/examples/provisioning/softap_prov/main/app_prov.c b/examples/provisioning/softap_prov/main/app_prov.c index 573dad96fc..2d625edce0 100644 --- a/examples/provisioning/softap_prov/main/app_prov.c +++ b/examples/provisioning/softap_prov/main/app_prov.c @@ -107,7 +107,7 @@ static void app_prov_stop_service(void) /* Remove event handler */ esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, app_prov_event_handler); - esp_event_handler_unregister(IP_EVENT, ESP_EVENT_ANY_ID, app_prov_event_handler); + esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, app_prov_event_handler); } /* Task spawned by timer callback */ @@ -234,17 +234,6 @@ esp_err_t app_prov_is_provisioned(bool *provisioned) nvs_flash_erase(); #endif - if (nvs_flash_init() != ESP_OK) { - ESP_LOGE(TAG, "Failed to init NVS"); - return ESP_FAIL; - } - - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - if (esp_wifi_init(&cfg) != ESP_OK) { - ESP_LOGE(TAG, "Failed to init wifi"); - return ESP_FAIL; - } - /* Get WiFi Station configuration */ wifi_config_t wifi_cfg; if (esp_wifi_get_config(ESP_IF_WIFI_STA, &wifi_cfg) != ESP_OK) { @@ -292,14 +281,6 @@ esp_err_t app_prov_configure_sta(wifi_config_t *wifi_cfg) static esp_err_t start_wifi_ap(const char *ssid, const char *pass) { - /* Initialize WiFi with default configuration */ - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - esp_err_t err = esp_wifi_init(&cfg); - if (err != ESP_OK) { - ESP_LOGE(TAG, "Failed to init WiFi : %d", err); - return err; - } - /* Build WiFi configuration for AP mode */ wifi_config_t wifi_config = { .ap = { @@ -319,7 +300,7 @@ static esp_err_t start_wifi_ap(const char *ssid, const char *pass) } /* Start WiFi in AP mode with configuration built above */ - err = esp_wifi_set_mode(WIFI_MODE_AP); + esp_err_t err = esp_wifi_set_mode(WIFI_MODE_AP); if (err != ESP_OK) { ESP_LOGE(TAG, "Failed to set WiFi mode : %d", err); return err; @@ -378,7 +359,7 @@ esp_err_t app_prov_start_softap_provisioning(const char *ssid, const char *pass, return err; } - err = esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, app_prov_event_handler, NULL); + err = esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, app_prov_event_handler, NULL); if (err != ESP_OK) { ESP_LOGE(TAG, "Failed to register IP event handler"); return err;