wifi_prov_mgr_config_t config = {
.scheme = wifi_prov_scheme_ble,
- .scheme_event_handler = WIFI_PROV_SCHEME_BLE_EVENT_HANDLER_FREE_BTDM,
- .app_event_handler = {
- .event_cb = prov_event_handler,
- .user_data = NULL
- }
+ .scheme_event_handler = WIFI_PROV_SCHEME_BLE_EVENT_HANDLER_FREE_BTDM
};
ESP_ERR_CHECK( wifi_prov_mgr_init(config) );
* ``WIFI_PROV_SCHEME_BLE_EVENT_HANDLER_FREE_BT`` - Free only classic BT. Used when main application requires BLE. In this case freeing happens right when the manager is initialized.
* ``WIFI_PROV_EVENT_HANDLER_NONE`` - Don't use any scheme specific handler. Used when provisioning scheme is not BLE (i.e. SoftAP or Console), or when main application wants to handle the memory reclaiming on its own, or needs both BLE and classic BT to function.
- * `app_event_handler` : Application specific event handler which can be used to execute specific calls depending on the state of the provisioning service. This is to be set to a function of the form ``void app_event_handler(void *user_data, wifi_prov_cb_event_t event, void *event_data)`` along with any user data to be made available at the time of handling. This can also be set to ``WIFI_PROV_EVENT_HANDLER_NONE`` if not used. See definition of ``wifi_prov_cb_event_t`` for the list of events that are generated by the provisioning service. Following is a snippet showing a typical application specific provisioning event handler along with usage of the ``event_data`` parameter :
+ * `app_event_handler` (Deprecated) : It is now recommended to catch ``WIFI_PROV_EVENT``s that are emitted to the default event loop handler. See definition of ``wifi_prov_cb_event_t`` for the list of events that are generated by the provisioning service. Here is an excerpt showing some of the provisioning events:
.. highlight:: c
::
- void prov_event_handler(void *user_data,
- wifi_prov_cb_event_t event,
- void *event_data)
+ static void event_handler(void* arg, esp_event_base_t event_base,
+ int event_id, void* event_data)
{
- switch (event) {
- case WIFI_PROV_INIT:
- ESP_LOGI(TAG, "Manager initialized");
- break;
- case WIFI_PROV_START:
- ESP_LOGI(TAG, "Provisioning started");
- break;
- case WIFI_PROV_CRED_RECV: {
- wifi_sta_config_t *wifi_sta_cfg = (wifi_sta_config_t *)event_data;
- ESP_LOGI(TAG, "Received Wi-Fi credentials"
- "\n\tSSID : %s\n\tPassword : %s",
- (const char *) wifi_sta_cfg->ssid,
- (const char *) wifi_sta_cfg->password);
- break;
- }
- case WIFI_PROV_CRED_FAIL: {
- wifi_prov_sta_fail_reason_t *reason = (wifi_prov_sta_fail_reason_t *)event_data;
- ESP_LOGE(TAG, "Provisioning failed : %s",
- (*reason == WIFI_PROV_STA_AUTH_ERROR) ?
- "Wi-Fi AP password incorrect" :
- "Wi-Fi AP not found");
- break;
+ if (event_base == WIFI_PROV_EVENT) {
+ switch (event_id) {
+ case WIFI_PROV_START:
+ ESP_LOGI(TAG, "Provisioning started");
+ break;
+ case WIFI_PROV_CRED_RECV: {
+ wifi_sta_config_t *wifi_sta_cfg = (wifi_sta_config_t *)event_data;
+ ESP_LOGI(TAG, "Received Wi-Fi credentials"
+ "\n\tSSID : %s\n\tPassword : %s",
+ (const char *) wifi_sta_cfg->ssid,
+ (const char *) wifi_sta_cfg->password);
+ break;
+ }
+ case WIFI_PROV_CRED_FAIL: {
+ wifi_prov_sta_fail_reason_t *reason = (wifi_prov_sta_fail_reason_t *)event_data;
+ ESP_LOGE(TAG, "Provisioning failed!\n\tReason : %s"
+ "\n\tPlease reset to factory and retry provisioning",
+ (*reason == WIFI_PROV_STA_AUTH_ERROR) ?
+ "Wi-Fi station authentication failed" : "Wi-Fi access-point not found");
+ break;
+ }
+ case WIFI_PROV_CRED_SUCCESS:
+ ESP_LOGI(TAG, "Provisioning successful");
+ break;
+ case WIFI_PROV_END:
+ /* De-initialize manager once provisioning is finished */
+ wifi_prov_mgr_deinit();
+ break;
+ default:
+ break;
}
- case WIFI_PROV_CRED_SUCCESS:
- ESP_LOGI(TAG, "Provisioning successful");
- break;
- case WIFI_PROV_END:
- ESP_LOGI(TAG, "Provisioning stopped");
- break;
- case WIFI_PROV_DEINIT:
- ESP_LOGI(TAG, "Manager de-initialized");
- break;
- default:
- break;
}
}
+The manager can be de-initialized at any moment by making a call to :cpp:func:`wifi_prov_mgr_deinit()`.
+
.. _wifi-prov-check-state:
Check Provisioning State
ESP_ERR_CHECK( wifi_prov_mgr_is_provisioned(&provisioned) );
-Event Loop Handling
-^^^^^^^^^^^^^^^^^^^
-
-Presently Wi-Fi provisioning manager cannot directly catch external system events, hence it is necessary to explicitly call :cpp:func:`wifi_prov_mgr_event_handler()` from inside the global event loop handler. See the following snippet :
-
- .. highlight:: c
-
- ::
-
- static esp_err_t global_event_loop_handler(void *ctx, system_event_t *event)
- {
- /* Pass event information to provisioning manager so that it can
- * maintain its internal state depending upon the system event */
- wifi_prov_mgr_event_handler(ctx, event);
-
- /* Event handling logic for main application */
- switch (event->event_id) {
- .....
- .....
- .....
- }
- return ESP_OK;
- }
-
Start Provisioning Service
^^^^^^^^^^^^^^^^^^^^^^^^^^
wifi_prov_mgr_deinit();
-The other way is to use the application specific event handler which is to be configured during initialization, as explained above in :ref:`wifi-prov-mgr-init`.
+The other way is to use the default event loop handler to catch ``WIFI_PROV_EVENT``s and call :cpp:func:`wifi_prov_mgr_deinit()` when event ID is ``WIFI_PROV_END``:
.. highlight:: c
::
- void prov_event_handler(void *user_data, wifi_prov_cb_event_t event, void *event_data)
+ static void event_handler(void* arg, esp_event_base_t event_base,
+ int event_id, void* event_data)
{
- switch (event) {
- case WIFI_PROV_END:
- // De-initialize manager once provisioning is finished
- wifi_prov_mgr_deinit();
- break;
- default:
- break;
+ if (event_base == WIFI_PROV_EVENT && event_id == WIFI_PROV_END) {
+ /* De-initialize manager once provisioning is finished */
+ wifi_prov_mgr_deinit();
}
}