From 4ef6c85f0cac63f2204e849e7852ac2c33d58f0b Mon Sep 17 00:00:00 2001 From: Anurag Kar Date: Tue, 16 Apr 2019 17:14:10 +0530 Subject: [PATCH 1/4] wifi_provisioning : Wi-Fi Provisioning Manager added --- components/wifi_provisioning/CMakeLists.txt | 17 +- components/wifi_provisioning/component.mk | 6 +- .../include/wifi_provisioning/manager.h | 553 ++++++++ .../include/wifi_provisioning/scheme_ble.h | 82 ++ .../wifi_provisioning/scheme_console.h | 34 + .../include/wifi_provisioning/scheme_softap.h | 34 + components/wifi_provisioning/src/handlers.c | 146 +++ components/wifi_provisioning/src/manager.c | 1117 +++++++++++++++++ components/wifi_provisioning/src/scheme_ble.c | 232 ++++ .../wifi_provisioning/src/scheme_console.c | 92 ++ .../wifi_provisioning/src/scheme_softap.c | 202 +++ .../src/wifi_provisioning_priv.h | 44 + 12 files changed, 2555 insertions(+), 4 deletions(-) create mode 100644 components/wifi_provisioning/include/wifi_provisioning/manager.h create mode 100644 components/wifi_provisioning/include/wifi_provisioning/scheme_ble.h create mode 100644 components/wifi_provisioning/include/wifi_provisioning/scheme_console.h create mode 100644 components/wifi_provisioning/include/wifi_provisioning/scheme_softap.h create mode 100644 components/wifi_provisioning/src/handlers.c create mode 100644 components/wifi_provisioning/src/manager.c create mode 100644 components/wifi_provisioning/src/scheme_ble.c create mode 100644 components/wifi_provisioning/src/scheme_console.c create mode 100644 components/wifi_provisioning/src/scheme_softap.c create mode 100644 components/wifi_provisioning/src/wifi_provisioning_priv.h diff --git a/components/wifi_provisioning/CMakeLists.txt b/components/wifi_provisioning/CMakeLists.txt index 0147d685c7..ec129bce3b 100644 --- a/components/wifi_provisioning/CMakeLists.txt +++ b/components/wifi_provisioning/CMakeLists.txt @@ -1,10 +1,21 @@ set(COMPONENT_ADD_INCLUDEDIRS include) -set(COMPONENT_PRIV_INCLUDEDIRS proto-c ../protocomm/proto-c) +set(COMPONENT_PRIV_INCLUDEDIRS src proto-c ../protocomm/proto-c) set(COMPONENT_SRCS "src/wifi_config.c" + "src/manager.c" + "src/handlers.c" + "src/scheme_softap.c" + "src/scheme_console.c" "proto-c/wifi_config.pb-c.c" "proto-c/wifi_constants.pb-c.c") -set(COMPONENT_REQUIRES lwip) -set(COMPONENT_PRIV_REQUIRES protobuf-c protocomm) +set(COMPONENT_REQUIRES lwip protocomm) +set(COMPONENT_PRIV_REQUIRES protobuf-c bt mdns json) + +if(CONFIG_BT_ENABLED) + if(CONFIG_BT_BLUEDROID_ENABLED) + list(APPEND COMPONENT_SRCS + "src/scheme_ble.c") + endif() +endif() register_component() diff --git a/components/wifi_provisioning/component.mk b/components/wifi_provisioning/component.mk index efeb597c37..458d957c76 100644 --- a/components/wifi_provisioning/component.mk +++ b/components/wifi_provisioning/component.mk @@ -1,3 +1,7 @@ COMPONENT_SRCDIRS := src proto-c COMPONENT_ADD_INCLUDEDIRS := include -COMPONENT_PRIV_INCLUDEDIRS := proto-c ../protocomm/proto-c/ +COMPONENT_PRIV_INCLUDEDIRS := src proto-c ../protocomm/proto-c/ + +ifneq ($(filter y, $(CONFIG_BT_ENABLED) $(CONFIG_BT_BLUEDROID_ENABLED)),y y) + COMPONENT_OBJEXCLUDE := src/scheme_ble.o +endif diff --git a/components/wifi_provisioning/include/wifi_provisioning/manager.h b/components/wifi_provisioning/include/wifi_provisioning/manager.h new file mode 100644 index 0000000000..6b535604d4 --- /dev/null +++ b/components/wifi_provisioning/include/wifi_provisioning/manager.h @@ -0,0 +1,553 @@ +// Copyright 2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once + +#include +#include + +#include "wifi_provisioning/wifi_config.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Events generated by manager + * + * These events are generated in order of declaration and, for the + * stretch of time between initialization and de-initialization of + * the manager, each event is signaled only once + */ +typedef enum { + /** + * Emitted when the manager is initialized + */ + WIFI_PROV_INIT, + + /** + * Indicates that provisioning has started + */ + WIFI_PROV_START, + + /** + * Emitted when Wi-Fi AP credentials are received via `protocomm` + * endpoint `wifi_config`. The event data in this case is a pointer + * to the corresponding `wifi_sta_config_t` structure + */ + WIFI_PROV_CRED_RECV, + + /** + * Emitted when device fails to connect to the AP of which the + * credentials were received earlier on event `WIFI_PROV_CRED_RECV`. + * The event data in this case is a pointer to the disconnection + * reason code with type `wifi_prov_sta_fail_reason_t` + */ + WIFI_PROV_CRED_FAIL, + + /** + * Emitted when device successfully connects to the AP of which the + * credentials were received earlier on event `WIFI_PROV_CRED_RECV` + */ + WIFI_PROV_CRED_SUCCESS, + + /** + * Signals that provisioning service has stopped + */ + WIFI_PROV_END, + + /** + * Signals that manager has been de-initialized + */ + WIFI_PROV_DEINIT, +} wifi_prov_cb_event_t; + +typedef void (*wifi_prov_cb_func_t)(void *user_data, wifi_prov_cb_event_t event, void *event_data); + +/** + * @brief Event handler that is used by the manager while + * provisioning service is active + */ +typedef struct { + /** + * Callback function to be executed on provisioning events + */ + wifi_prov_cb_func_t event_cb; + + /** + * User context data to pass as parameter to callback function + */ + void *user_data; +} wifi_prov_event_handler_t; + +/** + * @brief Event handler can be set to none if not used + */ +#define WIFI_PROV_EVENT_HANDLER_NONE { \ + .event_cb = NULL, \ + .user_data = NULL \ +} + +/** + * @brief Structure for specifying the provisioning scheme to be + * followed by the manager + * + * @note Ready to use schemes are available: + * - wifi_prov_scheme_ble : for provisioning over BLE transport + GATT server + * - wifi_prov_scheme_softap : for provisioning over SoftAP transport + HTTP server + * - wifi_prov_scheme_console : for provisioning over Serial UART transport + Console (for debugging) + */ +typedef struct wifi_prov_scheme { + /** + * Function which is to be called by the manager when it is to + * start the provisioning service associated with a protocomm instance + * and a scheme specific configuration + */ + esp_err_t (*prov_start) (protocomm_t *pc, void *config); + + /** + * Function which is to be called by the manager to stop the + * provisioning service previously associated with a protocomm instance + */ + esp_err_t (*prov_stop) (protocomm_t *pc); + + /** + * Function which is to be called by the manager to generate + * a new configuration for the provisioning service, that is + * to be passed to prov_start() + */ + void *(*new_config) (void); + + /** + * Function which is to be called by the manager to delete a + * configuration generated using new_config() + */ + void (*delete_config) (void *config); + + /** + * Function which is to be called by the manager to set the + * service name and key values in the configuration structure + */ + esp_err_t (*set_config_service) (void *config, const char *service_name, const char *service_key); + + /** + * Function which is to be called by the manager to set a protocomm endpoint + * with an identifying name and UUID in the configuration structure + */ + esp_err_t (*set_config_endpoint) (void *config, const char *endpoint_name, uint16_t uuid); + + /** + * Sets mode of operation of Wi-Fi during provisioning + * This is set to : + * - WIFI_MODE_APSTA for SoftAP transport + * - WIFI_MODE_STA for BLE transport + */ + wifi_mode_t wifi_mode; +} wifi_prov_scheme_t; + +/** + * @brief Structure for specifying the manager configuration + */ +typedef struct { + /** + * Provisioning scheme to use. Following schemes are already available: + * - wifi_prov_scheme_ble : for provisioning over BLE transport + GATT server + * - wifi_prov_scheme_softap : for provisioning over SoftAP transport + HTTP server + mDNS (optional) + * - wifi_prov_scheme_console : for provisioning over Serial UART transport + Console (for debugging) + */ + wifi_prov_scheme_t scheme; + + /** + * Event handler required by the scheme for incorporating scheme specific + * behavior while provisioning manager is running. Various options may be + * provided by the scheme for setting this field. Use WIFI_PROV_EVENT_HANDLER_NONE + * when not used. When using scheme wifi_prov_scheme_ble, the following + * options are available: + * - WIFI_PROV_SCHEME_BLE_EVENT_HANDLER_FREE_BTDM + * - WIFI_PROV_SCHEME_BLE_EVENT_HANDLER_FREE_BLE + * - WIFI_PROV_SCHEME_BLE_EVENT_HANDLER_FREE_BT + */ + wifi_prov_event_handler_t scheme_event_handler; + + /** + * Event handler that can be set for the purpose of incorporating application + * specific behavior. Use WIFI_PROV_EVENT_HANDLER_NONE when not used. + */ + wifi_prov_event_handler_t app_event_handler; +} wifi_prov_mgr_config_t; + +/** + * @brief Security modes supported by the Provisioning Manager. + * + * These are same as the security modes provided by protocomm + */ +typedef enum wifi_prov_security { + /** + * No security (plain-text communication) + */ + WIFI_PROV_SECURITY_0 = 0, + + /** + * This secure communication mode consists of + * X25519 key exchange + * + proof of possession (pop) based authentication + * + AES-CTR encryption + */ + WIFI_PROV_SECURITY_1 +} wifi_prov_security_t; + +/** + * @brief Initialize provisioning manager instance + * + * Configures the manager and allocates internal resources + * + * Configuration specifies the provisioning scheme (transport) + * and event handlers + * + * Event WIFI_PROV_INIT is emitted right after initialization + * is complete + * + * @param[in] config Configuration structure + * + * @return + * - ESP_OK : Success + * - ESP_FAIL : Fail + */ +esp_err_t wifi_prov_mgr_init(wifi_prov_mgr_config_t config); + +/** + * @brief Stop provisioning (if running) and release + * resource used by the manager + * + * Event WIFI_PROV_DEINIT is emitted right after de-initialization + * is finished + * + * If provisioning service is still active when this API is called, + * it first stops the service, hence emitting WIFI_PROV_END, and + * then performs the de-initialization + */ +void wifi_prov_mgr_deinit(void); + +/** + * @brief Checks if device is provisioned + * + * This checks if Wi-Fi credentials are present on the NVS + * + * The Wi-Fi credentials are assumed to be kept in the same + * NVS namespace as used by esp_wifi component + * + * If one were to call esp_wifi_set_config() directly instead + * of going through the provisioning process, this function will + * still yield true (i.e. device will be found to be provisioned) + * + * @note Calling wifi_prov_mgr_start_provisioning() automatically + * resets the provision state, irrespective of what the + * state was prior to making the call. + * + * @param[out] provisioned True if provisioned, else false + * + * @return + * - ESP_OK : Retrieved provision state successfully + * - ESP_FAIL : Wi-Fi not initialized + * - ESP_ERR_INVALID_ARG : Null argument supplied + * - ESP_ERR_INVALID_STATE : Manager not initialized + */ +esp_err_t wifi_prov_mgr_is_provisioned(bool *provisioned); + +/** + * @brief Start provisioning service + * + * This starts the provisioning service according to the scheme + * configured at the time of initialization. For scheme : + * - wifi_prov_scheme_ble : This starts protocomm_ble, which internally initializes + * BLE transport and starts GATT server for handling + * provisioning requests + * - wifi_prov_scheme_softap : This activates SoftAP mode of Wi-Fi and starts + * protocomm_httpd, which internally starts an HTTP + * server for handling provisioning requests (If mDNS is + * active it also starts advertising service with type + * _esp_wifi_prov._tcp) + * + * Event WIFI_PROV_START is emitted right after provisioning starts without failure + * + * @note This API will start provisioning service even if device is found to be + * already provisioned, i.e. wifi_prov_mgr_is_provisioned() yields true + * + * @param[in] security Specify which protocomm security scheme to use : + * - WIFI_PROV_SECURITY_0 : For no security + * - WIFI_PROV_SECURITY_1 : x25519 secure handshake for session + * establishment followed by AES-CTR encryption of provisioning messages + * @param[in] pop Pointer to proof of possession string (NULL if not needed). This + * is relevant only for protocomm security 1, in which case it is used + * for authenticating secure session + * @param[in] service_name Unique name of the service. This translates to: + * - Wi-Fi SSID when provisioning mode is softAP + * - Device name when provisioning mode is BLE + * @param[in] service_key Key required by client to access the service (NULL if not needed). + * This translates to: + * - Wi-Fi password when provisioning mode is softAP + * - ignored when provisioning mode is BLE + * + * @return + * - ESP_OK : Provisioning started successfully + * - ESP_FAIL : Failed to start provisioning service + * - ESP_ERR_INVALID_STATE : Provisioning manager not initialized or already started + */ +esp_err_t wifi_prov_mgr_start_provisioning(wifi_prov_security_t security, const char *pop, + const char *service_name, const char *service_key); + +/** + * @brief Stop provisioning service + * + * If provisioning service is active, this API will initiate a process to stop + * the service and return. Once the service actually stops, the event WIFI_PROV_END + * will be emitted. + * + * If wifi_prov_mgr_deinit() is called without calling this API first, it will + * automatically stop the provisioning service and emit the WIFI_PROV_END, followed + * by WIFI_PROV_DEINIT, before returning. + * + * This API will generally be used along with wifi_prov_mgr_disable_auto_stop() + * in the scenario when the main application has registered its own endpoints, + * and wishes that the provisioning service is stopped only when some protocomm + * command from the client side application is received. + * + * Calling this API inside an endpoint handler, with sufficient cleanup_delay, + * will allow the response / acknowledgment to be sent successfully before the + * underlying protocomm service is stopped. + * + * Cleaup_delay is set when calling wifi_prov_mgr_disable_auto_stop(). + * If not specified, it defaults to 1000ms. + * + * For straightforward cases, using this API is usually not necessary as + * provisioning is stopped automatically once WIFI_PROV_CRED_SUCCESS is emitted. + * Stopping is delayed (maximum 30 seconds) thus allowing the client side + * application to query for Wi-Fi state, i.e. after receiving the first query + * and sending `Wi-Fi state connected` response the service is stopped immediately. + */ +void wifi_prov_mgr_stop_provisioning(void); + +/** + * @brief Wait for provisioning service to finish + * + * Calling this API will block until provisioning service is stopped + * i.e. till event WIFI_PROV_END is emitted. + * + * This will not block if provisioning is not started or not initialized. + */ +void wifi_prov_mgr_wait(void); + +/** + * @brief Disable auto stopping of provisioning service upon completion + * + * By default, once provisioning is complete, the provisioning service is automatically + * stopped, and all endpoints (along with those registered by main application) are + * deactivated. + * + * This API is useful in the case when main application wishes to close provisioning service + * only after it receives some protocomm command from the client side app. For example, after + * connecting to Wi-Fi, the device may want to connect to the cloud, and only once that is + * successfully, the device is said to be fully configured. But, then it is upto the main + * application to explicitly call wifi_prov_mgr_stop_provisioning() later when the device is + * fully configured and the provisioning service is no longer required. + * + * @note This must be called before executing wifi_prov_mgr_start_provisioning() + * + * @param[in] cleanup_delay Sets the delay after which the actual cleanup of transport related + * resources is done after a call to wifi_prov_mgr_stop_provisioning() + * returns. Minimum allowed value is 100ms. If not specified, this will + * default to 1000ms. + * + * @return + * - ESP_OK : Success + * - ESP_ERR_INVALID_STATE : Manager not initialized or + * provisioning service already started + */ +esp_err_t wifi_prov_mgr_disable_auto_stop(uint32_t cleanup_delay); + +/** + * @brief Set application version and capabilities in the JSON data returned by + * proto-ver endpoint + * + * This function can be called multiple times, to specify information about the various + * application specific services running on the device, identified by unique labels. + * + * The provisioning service itself registers an entry in the JSON data, by the label "prov", + * containing only provisioning service version and capabilities. Application services should + * use a label other than "prov" so as not to overwrite this. + * + * @note This must be called before executing wifi_prov_mgr_start_provisioning() + * + * @param[in] label String indicating the application name. + * + * @param[in] version String indicating the application version. + * There is no constraint on format. + * + * @param[in] capabilities Array of strings with capabilities. + * These could be used by the client side app to know + * the application registered endpoint capabilities + * + * @param[in] total_capabilities Size of capabilities array + * + * @return + * - ESP_OK : Success + * - ESP_ERR_INVALID_STATE : Manager not initialized or + * provisioning service already started + * - ESP_ERR_NO_MEM : Failed to allocate memory for version string + * - ESP_ERR_INVALID_ARG : Null argument + */ +esp_err_t wifi_prov_mgr_set_app_info(const char *label, const char *version, + const char**capabilities, size_t total_capabilities); + +/** + * @brief Create an additional endpoint and allocate internal resources for it + * + * This API is to be called by the application if it wants to create an additional + * endpoint. All additional endpoints will be assigned UUIDs starting from 0xFF54 + * and so on in the order of execution. + * + * protocomm handler for the created endpoint is to be registered later using + * wifi_prov_mgr_endpoint_register() after provisioning has started. + * + * @note This API can only be called BEFORE provisioning is started + * + * @note Additional endpoints can be used for configuring client provided + * parameters other than Wi-Fi credentials, that are necessary for the + * main application and hence must be set prior to starting the application + * + * @note After session establishment, the additional endpoints must be targeted + * first by the client side application before sending Wi-Fi configuration, + * because once Wi-Fi configuration finishes the provisioning service is + * stopped and hence all endpoints are unregistered + * + * @param[in] ep_name unique name of the endpoint + * + * @return + * - ESP_OK : Success + * - ESP_FAIL : Failure + */ +esp_err_t wifi_prov_mgr_endpoint_create(const char *ep_name); + +/** + * @brief Register a handler for the previously created endpoint + * + * This API can be called by the application to register a protocomm handler + * to any endpoint that was created using wifi_prov_mgr_endpoint_create(). + * + * @note This API can only be called AFTER provisioning has started + * + * @note Additional endpoints can be used for configuring client provided + * parameters other than Wi-Fi credentials, that are necessary for the + * main application and hence must be set prior to starting the application + * + * @note After session establishment, the additional endpoints must be targeted + * first by the client side application before sending Wi-Fi configuration, + * because once Wi-Fi configuration finishes the provisioning service is + * stopped and hence all endpoints are unregistered + * + * @param[in] ep_name Name of the endpoint + * @param[in] handler Endpoint handler function + * @param[in] user_ctx User data + * + * @return + * - ESP_OK : Success + * - ESP_FAIL : Failure + */ +esp_err_t wifi_prov_mgr_endpoint_register(const char *ep_name, + protocomm_req_handler_t handler, + void *user_ctx); + +/** + * @brief Unregister the handler for an endpoint + * + * This API can be called if the application wants to selectively + * unregister the handler of an endpoint while the provisioning + * is still in progress. + * + * All the endpoint handlers are unregistered automatically when + * the provisioning stops. + * + * @param[in] ep_name Name of the endpoint + */ +void wifi_prov_mgr_endpoint_unregister(const char *ep_name); + +/** + * @brief Event handler for provisioning manager + * + * This is called from the main event handler and controls the + * provisioning manager's internal state machine depending on + * incoming Wi-Fi events + * + * @param[in] ctx Event context data + * @param[in] event Event info + * + * @return + * - ESP_OK : Event handled successfully + * - ESP_ERR_FAIL : This event cannot be handled + */ +esp_err_t wifi_prov_mgr_event_handler(void *ctx, system_event_t *event); + +/** + * @brief Get state of Wi-Fi Station during provisioning + * + * @param[out] state Pointer to wifi_prov_sta_state_t + * variable to be filled + * + * @return + * - ESP_OK : Successfully retrieved Wi-Fi state + * - ESP_FAIL : Provisioning app not running + */ +esp_err_t wifi_prov_mgr_get_wifi_state(wifi_prov_sta_state_t *state); + +/** + * @brief Get reason code in case of Wi-Fi station + * disconnection during provisioning + * +* @param[out] reason Pointer to wifi_prov_sta_fail_reason_t +* variable to be filled + * + * @return + * - ESP_OK : Successfully retrieved Wi-Fi disconnect reason + * - ESP_FAIL : Provisioning app not running + */ +esp_err_t wifi_prov_mgr_get_wifi_disconnect_reason(wifi_prov_sta_fail_reason_t *reason); + +/** + * @brief Runs Wi-Fi as Station with the supplied configuration + * + * Configures the Wi-Fi station mode to connect to the AP with + * SSID and password specified in config structure and sets + * Wi-Fi to run as station. + * + * This is automatically called by provisioning service upon + * receiving new credentials. + * + * If credentials are to be supplied to the manager via a + * different mode other than through protocomm, then this + * API needs to be called. + * + * Event WIFI_PROV_CRED_RECV is emitted after credentials have + * been applied and Wi-Fi station started + * + * @param[in] wifi_cfg Pointer to Wi-Fi configuration structure + * + * @return + * - ESP_OK : Wi-Fi configured and started successfully + * - ESP_FAIL : Failed to set configuration + */ +esp_err_t wifi_prov_mgr_configure_sta(wifi_config_t *wifi_cfg); + +#ifdef __cplusplus +} +#endif diff --git a/components/wifi_provisioning/include/wifi_provisioning/scheme_ble.h b/components/wifi_provisioning/include/wifi_provisioning/scheme_ble.h new file mode 100644 index 0000000000..ed30d81815 --- /dev/null +++ b/components/wifi_provisioning/include/wifi_provisioning/scheme_ble.h @@ -0,0 +1,82 @@ +// Copyright 2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once + +#include +#include + +#include "wifi_provisioning/manager.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Scheme that can be used by manager for provisioning + * over BLE transport with GATT server + */ +extern const wifi_prov_scheme_t wifi_prov_scheme_ble; + +/* This scheme specific event handler is to be used when application + * doesn't require BT and BLE after provisioning has finished */ +#define WIFI_PROV_SCHEME_BLE_EVENT_HANDLER_FREE_BTDM { \ + .event_cb = wifi_prov_scheme_ble_event_cb_free_btdm, \ + .user_data = NULL \ +} + +/* This scheme specific event handler is to be used when application + * doesn't require BLE to be active after provisioning has finished */ +#define WIFI_PROV_SCHEME_BLE_EVENT_HANDLER_FREE_BLE { \ + .event_cb = wifi_prov_scheme_ble_event_cb_free_ble, \ + .user_data = NULL \ +} + +/* This scheme specific event handler is to be used when application + * doesn't require BT to be active after provisioning has finished */ +#define WIFI_PROV_SCHEME_BLE_EVENT_HANDLER_FREE_BT { \ + .event_cb = wifi_prov_scheme_ble_event_cb_free_bt, \ + .user_data = NULL \ +} + +void wifi_prov_scheme_ble_event_cb_free_btdm(void *user_data, wifi_prov_cb_event_t event, void *event_data); +void wifi_prov_scheme_ble_event_cb_free_ble (void *user_data, wifi_prov_cb_event_t event, void *event_data); +void wifi_prov_scheme_ble_event_cb_free_bt (void *user_data, wifi_prov_cb_event_t event, void *event_data); + +/** + * @brief Set the 128 bit GATT service UUID used for provisioning + * + * This API is used to override the default 128 bit provisioning + * service UUID, which is 0000ffff-0000-1000-8000-00805f9b34fb. + * + * This must be called before starting provisioning, i.e. before + * making a call to wifi_prov_mgr_start_provisioning(), otherwise + * the default UUID will be used. + * + * @note The data being pointed to by the argument must be valid + * atleast till provisioning is started. Upon start, the + * manager will store an internal copy of this UUID, and + * this data can be freed or invalidated afterwords. + * + * @param[in] uuid128 A custom 128 bit UUID + * + * @return + * - ESP_OK : Success + * - ESP_ERR_INVALID_ARG : Null argument + */ +esp_err_t wifi_prov_scheme_ble_set_service_uuid(uint8_t *uuid128); + +#ifdef __cplusplus +} +#endif diff --git a/components/wifi_provisioning/include/wifi_provisioning/scheme_console.h b/components/wifi_provisioning/include/wifi_provisioning/scheme_console.h new file mode 100644 index 0000000000..760fe3243a --- /dev/null +++ b/components/wifi_provisioning/include/wifi_provisioning/scheme_console.h @@ -0,0 +1,34 @@ +// Copyright 2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once + +#include +#include + +#include "wifi_provisioning/manager.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Scheme that can be used by manager for provisioning + * over console (Serial UART) + */ +extern const wifi_prov_scheme_t wifi_prov_scheme_console; + +#ifdef __cplusplus +} +#endif diff --git a/components/wifi_provisioning/include/wifi_provisioning/scheme_softap.h b/components/wifi_provisioning/include/wifi_provisioning/scheme_softap.h new file mode 100644 index 0000000000..043b14e202 --- /dev/null +++ b/components/wifi_provisioning/include/wifi_provisioning/scheme_softap.h @@ -0,0 +1,34 @@ +// Copyright 2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once + +#include +#include + +#include "wifi_provisioning/manager.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Scheme that can be used by manager for provisioning + * over SoftAP transport with HTTP server + */ +extern const wifi_prov_scheme_t wifi_prov_scheme_softap; + +#ifdef __cplusplus +} +#endif diff --git a/components/wifi_provisioning/src/handlers.c b/components/wifi_provisioning/src/handlers.c new file mode 100644 index 0000000000..0a57ef56cc --- /dev/null +++ b/components/wifi_provisioning/src/handlers.c @@ -0,0 +1,146 @@ +// Copyright 2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include +#include + +#include +#include + +#include "wifi_provisioning/wifi_config.h" +#include "wifi_provisioning/manager.h" +#include "wifi_provisioning_priv.h" + +static const char *TAG = "wifi_prov_handlers"; + +/* Provide definition of wifi_prov_ctx_t */ +struct wifi_prov_ctx { + wifi_config_t wifi_cfg; +}; + +static wifi_config_t *get_config(wifi_prov_ctx_t **ctx) +{ + return (*ctx ? & (*ctx)->wifi_cfg : NULL); +} + +static wifi_config_t *new_config(wifi_prov_ctx_t **ctx) +{ + free(*ctx); + (*ctx) = (wifi_prov_ctx_t *) calloc(1, sizeof(wifi_prov_ctx_t)); + return get_config(ctx); +} + +static void free_config(wifi_prov_ctx_t **ctx) +{ + free(*ctx); + *ctx = NULL; +} + +static esp_err_t get_status_handler(wifi_prov_config_get_data_t *resp_data, wifi_prov_ctx_t **ctx) +{ + /* Initialize to zero */ + memset(resp_data, 0, sizeof(wifi_prov_config_get_data_t)); + + if (wifi_prov_mgr_get_wifi_state(&resp_data->wifi_state) != ESP_OK) { + ESP_LOGW(TAG, "Wi-Fi provisioning manager not running"); + return ESP_ERR_INVALID_STATE; + } + + if (resp_data->wifi_state == WIFI_PROV_STA_CONNECTED) { + ESP_LOGD(TAG, "Got state : connected"); + + /* IP Addr assigned to STA */ + tcpip_adapter_ip_info_t ip_info; + tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip_info); + char *ip_addr = ip4addr_ntoa(&ip_info.ip); + strcpy(resp_data->conn_info.ip_addr, ip_addr); + + /* AP information to which STA is connected */ + wifi_ap_record_t ap_info; + esp_wifi_sta_get_ap_info(&ap_info); + memcpy(resp_data->conn_info.bssid, (char *)ap_info.bssid, sizeof(ap_info.bssid)); + memcpy(resp_data->conn_info.ssid, (char *)ap_info.ssid, sizeof(ap_info.ssid)); + resp_data->conn_info.channel = ap_info.primary; + resp_data->conn_info.auth_mode = ap_info.authmode; + + /* Tell manager to stop provisioning service */ + wifi_prov_mgr_done(); + } else if (resp_data->wifi_state == WIFI_PROV_STA_DISCONNECTED) { + ESP_LOGD(TAG, "Got state : disconnected"); + + /* If disconnected, convey reason */ + wifi_prov_mgr_get_wifi_disconnect_reason(&resp_data->fail_reason); + } else { + ESP_LOGD(TAG, "Got state : connecting"); + } + return ESP_OK; +} + +static esp_err_t set_config_handler(const wifi_prov_config_set_data_t *req_data, wifi_prov_ctx_t **ctx) +{ + wifi_config_t *wifi_cfg = get_config(ctx); + if (wifi_cfg) { + free_config(ctx); + } + + wifi_cfg = new_config(ctx); + if (!wifi_cfg) { + ESP_LOGE(TAG, "Unable to allocate Wi-Fi config"); + return ESP_ERR_NO_MEM; + } + + ESP_LOGD(TAG, "Wi-Fi Credentials Received"); + + /* Using strncpy allows the max SSID length to be 32 bytes (as per 802.11 standard). + * But this doesn't guarantee that the saved SSID will be null terminated, because + * wifi_cfg->sta.ssid is also 32 bytes long (without extra 1 byte for null character) */ + strncpy((char *) wifi_cfg->sta.ssid, req_data->ssid, sizeof(wifi_cfg->sta.ssid)); + + /* Using strlcpy allows both max passphrase length (63 bytes) and ensures null termination + * because size of wifi_cfg->sta.password is 64 bytes (1 extra byte for null character) */ + strlcpy((char *) wifi_cfg->sta.password, req_data->password, sizeof(wifi_cfg->sta.password)); + return ESP_OK; +} + +static esp_err_t apply_config_handler(wifi_prov_ctx_t **ctx) +{ + wifi_config_t *wifi_cfg = get_config(ctx); + if (!wifi_cfg) { + ESP_LOGE(TAG, "Wi-Fi config not set"); + return ESP_ERR_INVALID_STATE; + } + + esp_err_t ret = wifi_prov_mgr_configure_sta(wifi_cfg); + if (ret == ESP_OK) { + ESP_LOGD(TAG, "Wi-Fi Credentials Applied"); + } else { + ESP_LOGE(TAG, "Failed to apply Wi-Fi Credentials"); + } + + free_config(ctx); + return ret; +} + +wifi_prov_config_handlers_t get_wifi_prov_handlers(void) +{ + wifi_prov_config_handlers_t wifi_prov_handlers = { + .get_status_handler = get_status_handler, + .set_config_handler = set_config_handler, + .apply_config_handler = apply_config_handler, + .ctx = NULL + }; + return wifi_prov_handlers; +} diff --git a/components/wifi_provisioning/src/manager.c b/components/wifi_provisioning/src/manager.c new file mode 100644 index 0000000000..5f772a7dff --- /dev/null +++ b/components/wifi_provisioning/src/manager.c @@ -0,0 +1,1117 @@ +// Copyright 2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include +#include +#include + +#include + +#include +#include +#include +#include + +#include +#include +#include + +#include "wifi_provisioning_priv.h" + +#define WIFI_PROV_MGR_VERSION "v1.0" + +static const char *TAG = "wifi_prov_mgr"; + +typedef enum { + WIFI_PROV_STATE_IDLE, + WIFI_PROV_STATE_STARTING, + WIFI_PROV_STATE_STARTED, + WIFI_PROV_STATE_CRED_RECV, + WIFI_PROV_STATE_FAIL, + WIFI_PROV_STATE_SUCCESS, + WIFI_PROV_STATE_STOPPING +} wifi_prov_mgr_state_t; + +/** + * @brief Structure for storing capabilities supported by + * the provisioning service + */ +struct wifi_prov_capabilities { + /* Proof of Possession is not required for establishing session */ + bool no_pop; + + /* Provisioning doesn't stop on it's own after receiving Wi-Fi credentials + * instead application has to explicitly call wifi_prov_mgr_stop_provisioning() */ + bool no_auto_stop; +}; + +/** + * @brief Structure for storing miscellaneous information about + * provisioning service that will be conveyed to clients + */ +struct wifi_prov_info { + const char *version; + struct wifi_prov_capabilities capabilities; +}; + +/** + * @brief Context data for provisioning manager + */ +struct wifi_prov_mgr_ctx { + /* Provisioning manager configuration */ + wifi_prov_mgr_config_t mgr_config; + + /* State of the provisioning service */ + wifi_prov_mgr_state_t prov_state; + + /* Provisioning scheme configuration */ + void *prov_scheme_config; + + /* Protocomm handle */ + protocomm_t *pc; + + /* Type of security to use with protocomm */ + int security; + + /* Pointer to proof of possession */ + protocomm_security_pop_t pop; + + /* Handle to timer */ + esp_timer_handle_t timer; + + /* State of Wi-Fi Station */ + wifi_prov_sta_state_t wifi_state; + + /* Code for Wi-Fi station disconnection (if disconnected) */ + wifi_prov_sta_fail_reason_t wifi_disconnect_reason; + + /* Protocomm handlers for Wi-Fi configuration endpoint */ + wifi_prov_config_handlers_t *wifi_prov_handlers; + + /* Count of used endpoint UUIDs */ + unsigned int endpoint_uuid_used; + + /* Provisioning service information */ + struct wifi_prov_info mgr_info; + + /* Application related information in JSON format */ + cJSON *app_info_json; + + /* Delay after which resources will be cleaned up asynchronously + * upon execution of wifi_prov_mgr_stop_provisioning() */ + uint32_t cleanup_delay; +}; + +/* Mutex to lock/unlock access to provisioning singleton + * context data. This is allocated only once on first init + * and never deleted as wifi_prov_mgr is a singleton */ +static SemaphoreHandle_t prov_ctx_lock = NULL; + +/* Pointer to provisioning context data */ +static struct wifi_prov_mgr_ctx *prov_ctx; + +/* This executes registered app_event_callback for a particular event + * + * NOTE : By the time this fucntion returns, it is possible that + * the manager got de-initialized due to a call to wifi_prov_mgr_deinit() + * either inside the event callbacks or from another thread. Therefore + * post execution of execute_event_cb(), the validity of prov_ctx must + * always be checked. A cleaner way, to avoid this pitfall safely, would + * be to limit the usage of this function to only public APIs, and that + * too at the very end, just before returning. + * + * NOTE: This function should be called only after ensuring that the + * context is valid and the control mutex is locked. */ +static void execute_event_cb(wifi_prov_cb_event_t event_id, void *event_data) +{ + ESP_LOGD(TAG, "execute_event_cb : %d", event_id); + + if (prov_ctx) { + wifi_prov_cb_func_t app_cb = prov_ctx->mgr_config.app_event_handler.event_cb; + void *app_data = prov_ctx->mgr_config.app_event_handler.user_data; + + wifi_prov_cb_func_t scheme_cb = prov_ctx->mgr_config.scheme_event_handler.event_cb; + void *scheme_data = prov_ctx->mgr_config.scheme_event_handler.user_data; + + /* Release the mutex before executing the callbacks. This is done so that + * wifi_prov_mgr_event_handler() doesn't stay blocked for the duration */ + xSemaphoreGiveRecursive(prov_ctx_lock); + + if (scheme_cb) { + /* Call scheme specific event handler */ + scheme_cb(scheme_data, event_id, event_data); + } + + if (app_cb) { + /* Call application specific event handler */ + app_cb(app_data, event_id, event_data); + } + + xSemaphoreTakeRecursive(prov_ctx_lock, portMAX_DELAY); + } +} + +esp_err_t wifi_prov_mgr_set_app_info(const char *label, const char *version, + const char**capabilities, size_t total_capabilities) +{ + if (!label || !version || !capabilities) { + return ESP_ERR_INVALID_ARG; + } + + if (!prov_ctx_lock) { + ESP_LOGE(TAG, "Provisioning manager not initialized"); + return ESP_ERR_INVALID_STATE; + } + + esp_err_t ret = ESP_FAIL; + xSemaphoreTakeRecursive(prov_ctx_lock, portMAX_DELAY); + + if (prov_ctx && prov_ctx->prov_state == WIFI_PROV_STATE_IDLE) { + if (!prov_ctx->app_info_json) { + prov_ctx->app_info_json = cJSON_CreateObject(); + } + + cJSON *new_entry_json = cJSON_CreateObject(); + cJSON *capabilities_json = cJSON_CreateArray(); + cJSON_AddItemToObject(prov_ctx->app_info_json, label, new_entry_json); + + /* Version ("ver") */ + cJSON_AddStringToObject(new_entry_json, "ver", version); + + /* List of capabilities ("cap") */ + cJSON_AddItemToObject(new_entry_json, "cap", capabilities_json); + for (unsigned int i = 0; i < total_capabilities; i++) { + if (capabilities[i]) { + cJSON_AddItemToArray(capabilities_json, cJSON_CreateString(capabilities[i])); + } + } + + } else { + ret = ESP_ERR_INVALID_STATE; + } + + xSemaphoreGiveRecursive(prov_ctx_lock); + return ret; +} + +static cJSON* wifi_prov_get_info_json(void) +{ + cJSON *full_info_json = prov_ctx->app_info_json ? + cJSON_Duplicate(prov_ctx->app_info_json, 1) : cJSON_CreateObject(); + cJSON *prov_info_json = cJSON_CreateObject(); + cJSON *prov_capabilities = cJSON_CreateArray(); + + /* Use label "prov" to indicate provisioning related information */ + cJSON_AddItemToObject(full_info_json, "prov", prov_info_json); + + /* Version field */ + cJSON_AddStringToObject(prov_info_json, "ver", prov_ctx->mgr_info.version); + + /* Capabilities field */ + cJSON_AddItemToObject(prov_info_json, "cap", prov_capabilities); + + /* If Proof of Possession is not used, indicate in capabilities */ + if (prov_ctx->mgr_info.capabilities.no_pop) { + cJSON_AddItemToArray(prov_capabilities, cJSON_CreateString("no_pop")); + } + return full_info_json; +} + +static esp_err_t wifi_prov_mgr_start_service(const char *service_name, const char *service_key) +{ + const wifi_prov_scheme_t *scheme = &prov_ctx->mgr_config.scheme; + esp_err_t ret; + + /* Create new protocomm instance */ + prov_ctx->pc = protocomm_new(); + if (prov_ctx->pc == NULL) { + ESP_LOGE(TAG, "Failed to create new protocomm instance"); + return ESP_FAIL; + } + + ret = scheme->set_config_service(prov_ctx->prov_scheme_config, service_name, service_key); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "Failed to configure service"); + protocomm_delete(prov_ctx->pc); + return ret; + } + + /* Start provisioning */ + ret = scheme->prov_start(prov_ctx->pc, prov_ctx->prov_scheme_config); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "Failed to start service"); + protocomm_delete(prov_ctx->pc); + return ret; + } + + /* Set version information / capabilities of provisioning service and application */ + cJSON *version_json = wifi_prov_get_info_json(); + ret = protocomm_set_version(prov_ctx->pc, "proto-ver", cJSON_Print(version_json)); + cJSON_Delete(version_json); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "Failed to set version endpoint"); + scheme->prov_stop(prov_ctx->pc); + protocomm_delete(prov_ctx->pc); + return ret; + } + + /* Set protocomm security type for endpoint */ + if (prov_ctx->security == 0) { + ret = protocomm_set_security(prov_ctx->pc, "prov-session", + &protocomm_security0, NULL); + } else if (prov_ctx->security == 1) { + ret = protocomm_set_security(prov_ctx->pc, "prov-session", + &protocomm_security1, &prov_ctx->pop); + } else { + ESP_LOGE(TAG, "Unsupported protocomm security version %d", prov_ctx->security); + ret = ESP_ERR_INVALID_ARG; + } + if (ret != ESP_OK) { + ESP_LOGE(TAG, "Failed to set security endpoint"); + scheme->prov_stop(prov_ctx->pc); + protocomm_delete(prov_ctx->pc); + return ret; + } + + prov_ctx->wifi_prov_handlers = malloc(sizeof(wifi_prov_config_handlers_t)); + if (!prov_ctx->wifi_prov_handlers) { + ESP_LOGD(TAG, "Failed to allocate memory for provisioning handlers"); + scheme->prov_stop(prov_ctx->pc); + protocomm_delete(prov_ctx->pc); + } + *prov_ctx->wifi_prov_handlers = get_wifi_prov_handlers(); + + /* Add protocomm endpoint for Wi-Fi station configuration */ + ret = protocomm_add_endpoint(prov_ctx->pc, "prov-config", + wifi_prov_config_data_handler, + prov_ctx->wifi_prov_handlers); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "Failed to set provisioning endpoint"); + free(prov_ctx->wifi_prov_handlers); + scheme->prov_stop(prov_ctx->pc); + protocomm_delete(prov_ctx->pc); + return ret; + } + + ESP_LOGI(TAG, "Provisioning started with service name : %s ", + service_name ? service_name : ""); + return ESP_OK; +} + +esp_err_t wifi_prov_mgr_endpoint_create(const char *ep_name) +{ + if (!prov_ctx_lock) { + ESP_LOGE(TAG, "Provisioning manager not initialized"); + return ESP_ERR_INVALID_STATE; + } + + esp_err_t err = ESP_FAIL; + + xSemaphoreTakeRecursive(prov_ctx_lock, portMAX_DELAY); + if (prov_ctx && + prov_ctx->prov_state == WIFI_PROV_STATE_IDLE) { + err = prov_ctx->mgr_config.scheme.set_config_endpoint( + prov_ctx->prov_scheme_config, ep_name, + prov_ctx->endpoint_uuid_used + 1); + } + if (err != ESP_OK) { + ESP_LOGE(TAG, "Failed to create additional endpoint"); + } else { + prov_ctx->endpoint_uuid_used++; + } + xSemaphoreGiveRecursive(prov_ctx_lock); + return err; +} + +esp_err_t wifi_prov_mgr_endpoint_register(const char *ep_name, protocomm_req_handler_t handler, void *user_ctx) +{ + if (!prov_ctx_lock) { + ESP_LOGE(TAG, "Provisioning manager not initialized"); + return ESP_ERR_INVALID_STATE; + } + + esp_err_t err = ESP_FAIL; + + xSemaphoreTakeRecursive(prov_ctx_lock, portMAX_DELAY); + if (prov_ctx && + prov_ctx->prov_state > WIFI_PROV_STATE_STARTING && + prov_ctx->prov_state < WIFI_PROV_STATE_STOPPING) { + err = protocomm_add_endpoint(prov_ctx->pc, ep_name, handler, user_ctx); + } + xSemaphoreGiveRecursive(prov_ctx_lock); + + if (err != ESP_OK) { + ESP_LOGE(TAG, "Failed to register handler for endpoint"); + } + return err; +} + +void wifi_prov_mgr_endpoint_unregister(const char *ep_name) +{ + if (!prov_ctx_lock) { + ESP_LOGE(TAG, "Provisioning manager not initialized"); + return; + } + + xSemaphoreTakeRecursive(prov_ctx_lock, portMAX_DELAY); + if (prov_ctx && + prov_ctx->prov_state > WIFI_PROV_STATE_STARTING && + prov_ctx->prov_state < WIFI_PROV_STATE_STOPPING) { + protocomm_remove_endpoint(prov_ctx->pc, ep_name); + } + xSemaphoreGiveRecursive(prov_ctx_lock); +} + +static void prov_stop_task(void *arg) +{ + bool is_this_a_task = (bool) arg; + + wifi_prov_cb_func_t app_cb = prov_ctx->mgr_config.app_event_handler.event_cb; + void *app_data = prov_ctx->mgr_config.app_event_handler.user_data; + + wifi_prov_cb_func_t scheme_cb = prov_ctx->mgr_config.scheme_event_handler.event_cb; + void *scheme_data = prov_ctx->mgr_config.scheme_event_handler.user_data; + + /* This delay is so that the client side app is notified first + * and then the provisioning is stopped. Generally 1000ms is enough. */ + uint32_t cleanup_delay = prov_ctx->cleanup_delay > 100 ? prov_ctx->cleanup_delay : 100; + vTaskDelay(cleanup_delay / portTICK_PERIOD_MS); + + /* All the extra application added endpoints are also + * removed automatically when prov_stop is called */ + prov_ctx->mgr_config.scheme.prov_stop(prov_ctx->pc); + + /* Delete protocomm instance */ + protocomm_delete(prov_ctx->pc); + prov_ctx->pc = NULL; + + /* Free provisioning handlers */ + free(prov_ctx->wifi_prov_handlers->ctx); + free(prov_ctx->wifi_prov_handlers); + prov_ctx->wifi_prov_handlers = NULL; + + /* Switch device to Wi-Fi STA mode irrespective of + * whether provisioning was completed or not */ + esp_wifi_set_mode(WIFI_MODE_STA); + ESP_LOGI(TAG, "Provisioning stopped"); + + if (is_this_a_task) { + xSemaphoreTakeRecursive(prov_ctx_lock, portMAX_DELAY); + prov_ctx->prov_state = WIFI_PROV_STATE_IDLE; + xSemaphoreGiveRecursive(prov_ctx_lock); + + ESP_LOGD(TAG, "execute_event_cb : %d", WIFI_PROV_END); + if (scheme_cb) { + scheme_cb(scheme_data, WIFI_PROV_END, NULL); + } + if (app_cb) { + app_cb(app_data, WIFI_PROV_END, NULL); + } + vTaskDelete(NULL); + } +} + +/* This will do one of these: + * 1) if blocking is false, start a task for stopping the provisioning service (returns true) + * 2) if blocking is true, stop provisioning service immediately (returns true) + * 3) if service was already in the process of termination, in blocking mode this will + * wait till the service is stopped (returns false) + * 4) if service was not running, this will return immediately (returns false) + * + * NOTE: This function should be called only after ensuring that the context + * is valid and the control mutex is locked + * + * NOTE: When blocking mode is selected, the event callbacks are not executed. + * This help with de-initialization. + */ +static bool wifi_prov_mgr_stop_service(bool blocking) +{ + if (blocking) { + /* Wait for any ongoing calls to wifi_prov_mgr_start_service() or + * wifi_prov_mgr_stop_service() from another thread to finish */ + while (prov_ctx && ( + prov_ctx->prov_state == WIFI_PROV_STATE_STARTING || + prov_ctx->prov_state == WIFI_PROV_STATE_STOPPING)) { + xSemaphoreGiveRecursive(prov_ctx_lock); + vTaskDelay(100 / portTICK_PERIOD_MS); + xSemaphoreTakeRecursive(prov_ctx_lock, portMAX_DELAY); + } + } else { + /* Wait for any ongoing call to wifi_prov_mgr_start_service() + * from another thread to finish */ + while (prov_ctx && + prov_ctx->prov_state == WIFI_PROV_STATE_STARTING) { + xSemaphoreGiveRecursive(prov_ctx_lock); + vTaskDelay(100 / portTICK_PERIOD_MS); + xSemaphoreTakeRecursive(prov_ctx_lock, portMAX_DELAY); + } + + if (prov_ctx && prov_ctx->prov_state == WIFI_PROV_STATE_STOPPING) { + ESP_LOGD(TAG, "Provisioning is already stopping"); + return false; + } + } + + if (!prov_ctx || prov_ctx->prov_state == WIFI_PROV_STATE_IDLE) { + ESP_LOGD(TAG, "Provisioning not running"); + return false; + } + + /* Timer not needed anymore */ + if (prov_ctx->timer) { + esp_timer_stop(prov_ctx->timer); + esp_timer_delete(prov_ctx->timer); + prov_ctx->timer = NULL; + } + + ESP_LOGD(TAG, "Stopping provisioning"); + prov_ctx->prov_state = WIFI_PROV_STATE_STOPPING; + + /* Free proof of possession */ + if (prov_ctx->pop.data) { + free((void *)prov_ctx->pop.data); + prov_ctx->pop.data = NULL; + } + + if (blocking) { + /* Run the cleanup without launching a separate task. Also the + * WIFI_PROV_END event is not emitted in this case */ + xSemaphoreGiveRecursive(prov_ctx_lock); + prov_stop_task((void *)0); + xSemaphoreTakeRecursive(prov_ctx_lock, portMAX_DELAY); + prov_ctx->prov_state = WIFI_PROV_STATE_IDLE; + } else { + /* Launch cleanup task to perform the cleanup asynchronously. + * It is important to do this asynchronously because, there are + * situations in which the transport level resources have to be + * released - some duration after - returning from a call to + * wifi_prov_mgr_stop_provisioning(), like when it is called + * inside a protocomm handler */ + assert(xTaskCreate(prov_stop_task, "prov_stop_task", 4096, (void *)1, + tskIDLE_PRIORITY, NULL) == pdPASS); + ESP_LOGD(TAG, "Provisioning scheduled for stopping"); + } + return true; +} + +/* Task spawned by timer callback */ +static void stop_prov_timer_cb(void *arg) +{ + wifi_prov_mgr_stop_provisioning(); +} + +esp_err_t wifi_prov_mgr_disable_auto_stop(uint32_t cleanup_delay) +{ + if (!prov_ctx_lock) { + ESP_LOGE(TAG, "Provisioning manager not initialized"); + return ESP_ERR_INVALID_STATE; + } + + esp_err_t ret = ESP_FAIL; + xSemaphoreTakeRecursive(prov_ctx_lock, portMAX_DELAY); + + if (prov_ctx && prov_ctx->prov_state == WIFI_PROV_STATE_IDLE) { + prov_ctx->mgr_info.capabilities.no_auto_stop = true; + prov_ctx->cleanup_delay = cleanup_delay; + } else { + ret = ESP_ERR_INVALID_STATE; + } + + xSemaphoreGiveRecursive(prov_ctx_lock); + return ret; +} + +/* Call this if provisioning is completed before the timeout occurs */ +esp_err_t wifi_prov_mgr_done(void) +{ + if (!prov_ctx_lock) { + ESP_LOGE(TAG, "Provisioning manager not initialized"); + return ESP_ERR_INVALID_STATE; + } + + xSemaphoreTakeRecursive(prov_ctx_lock, portMAX_DELAY); + /* Stop provisioning if auto stop is not disabled */ + if (prov_ctx && !prov_ctx->mgr_info.capabilities.no_auto_stop) { + wifi_prov_mgr_stop_provisioning(); + } + xSemaphoreGiveRecursive(prov_ctx_lock); + return ESP_OK; +} + +/* Event handler for starting/stopping provisioning. + * To be called from within the context of the main + * event handler */ +esp_err_t wifi_prov_mgr_event_handler(void *ctx, system_event_t *event) +{ + /* For accessing reason codes in case of disconnection */ + system_event_info_t *info = &event->event_info; + + if (!prov_ctx_lock) { + ESP_LOGE(TAG, "Provisioning manager not initialized"); + return ESP_ERR_INVALID_STATE; + } + xSemaphoreTakeRecursive(prov_ctx_lock, portMAX_DELAY); + + /* If pointer to provisioning application data is NULL + * then provisioning manager is not running, therefore + * return with error to allow the global handler to act */ + if (!prov_ctx) { + xSemaphoreGiveRecursive(prov_ctx_lock); + return ESP_ERR_INVALID_STATE; + } + + /* Only handle events when credential is received and + * Wi-Fi STA is yet to complete trying the connection */ + if (prov_ctx->prov_state != WIFI_PROV_STATE_CRED_RECV) { + xSemaphoreGiveRecursive(prov_ctx_lock); + return ESP_OK; + } + + esp_err_t ret = ESP_OK; + switch (event->event_id) { + case SYSTEM_EVENT_STA_START: + ESP_LOGD(TAG, "STA Start"); + /* Once configuration is received through protocomm, + * device is started as station. Once station starts, + * wait for connection to establish with configured + * host SSID and password */ + prov_ctx->wifi_state = WIFI_PROV_STA_CONNECTING; + break; + + case SYSTEM_EVENT_STA_GOT_IP: + ESP_LOGD(TAG, "STA Got IP"); + /* Station got IP. That means configuration is successful. */ + prov_ctx->wifi_state = WIFI_PROV_STA_CONNECTED; + prov_ctx->prov_state = WIFI_PROV_STATE_SUCCESS; + + /* If auto stop is enabled (default), schedule timer to + * stop provisioning app after 30 seconds. */ + if (!prov_ctx->mgr_info.capabilities.no_auto_stop) { + ESP_LOGD(TAG, "Starting 30s timer for stop_prov_timer_cb()"); + esp_timer_start_once(prov_ctx->timer, 30000 * 1000U); + } + + /* Execute user registered callback handler */ + execute_event_cb(WIFI_PROV_CRED_SUCCESS, NULL); + break; + + case SYSTEM_EVENT_STA_DISCONNECTED: + ESP_LOGD(TAG, "STA Disconnected"); + /* Station couldn't connect to configured host SSID */ + prov_ctx->wifi_state = WIFI_PROV_STA_DISCONNECTED; + ESP_LOGD(TAG, "Disconnect reason : %d", info->disconnected.reason); + + /* Set code corresponding to the reason for disconnection */ + switch (info->disconnected.reason) { + case WIFI_REASON_AUTH_EXPIRE: + case WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT: + case WIFI_REASON_BEACON_TIMEOUT: + case WIFI_REASON_AUTH_FAIL: + case WIFI_REASON_ASSOC_FAIL: + case WIFI_REASON_HANDSHAKE_TIMEOUT: + ESP_LOGD(TAG, "STA Auth Error"); + prov_ctx->wifi_disconnect_reason = WIFI_PROV_STA_AUTH_ERROR; + break; + case WIFI_REASON_NO_AP_FOUND: + ESP_LOGD(TAG, "STA AP Not found"); + prov_ctx->wifi_disconnect_reason = WIFI_PROV_STA_AP_NOT_FOUND; + break; + default: + /* If none of the expected reasons, + * retry connecting to host SSID */ + prov_ctx->wifi_state = WIFI_PROV_STA_CONNECTING; + esp_wifi_connect(); + } + + /* In case of disconnection, update state of service and + * run the event handler with disconnection reason as data */ + if (prov_ctx->wifi_state == WIFI_PROV_STA_DISCONNECTED) { + prov_ctx->prov_state = WIFI_PROV_STATE_FAIL; + wifi_prov_sta_fail_reason_t reason = prov_ctx->wifi_disconnect_reason; + /* Execute user registered callback handler */ + execute_event_cb(WIFI_PROV_CRED_FAIL, (void *)&reason); + } + break; + + default: + /* This event is not intended to be handled by this handler. + * Return ESP_FAIL to signal global event handler to take + * control */ + ret = ESP_FAIL; + break; + } + + xSemaphoreGiveRecursive(prov_ctx_lock); + return ret; +} + +esp_err_t wifi_prov_mgr_get_wifi_state(wifi_prov_sta_state_t *state) +{ + if (!prov_ctx_lock) { + ESP_LOGE(TAG, "Provisioning manager not initialized"); + return ESP_ERR_INVALID_STATE; + } + + xSemaphoreTakeRecursive(prov_ctx_lock, portMAX_DELAY); + if (prov_ctx == NULL || state == NULL) { + xSemaphoreGiveRecursive(prov_ctx_lock); + return ESP_FAIL; + } + + *state = prov_ctx->wifi_state; + xSemaphoreGiveRecursive(prov_ctx_lock); + return ESP_OK; +} + +esp_err_t wifi_prov_mgr_get_wifi_disconnect_reason(wifi_prov_sta_fail_reason_t *reason) +{ + if (!prov_ctx_lock) { + ESP_LOGE(TAG, "Provisioning manager not initialized"); + return ESP_ERR_INVALID_STATE; + } + + xSemaphoreTakeRecursive(prov_ctx_lock, portMAX_DELAY); + if (prov_ctx == NULL || reason == NULL) { + xSemaphoreGiveRecursive(prov_ctx_lock); + return ESP_FAIL; + } + + if (prov_ctx->wifi_state != WIFI_PROV_STA_DISCONNECTED) { + xSemaphoreGiveRecursive(prov_ctx_lock); + return ESP_FAIL; + } + + *reason = prov_ctx->wifi_disconnect_reason; + xSemaphoreGiveRecursive(prov_ctx_lock); + return ESP_OK; +} + +static void debug_print_wifi_credentials(wifi_sta_config_t sta, const char* pretext) +{ + size_t passlen = strlen((const char*) sta.password); + ESP_LOGD(TAG, "%s Wi-Fi SSID : %.*s", pretext, + strnlen((const char *) sta.ssid, sizeof(sta.ssid)), (const char *) sta.ssid); + + if (passlen) { + /* Mask password partially if longer than 3, else mask it completely */ + memset(sta.password + (passlen > 3), '*', passlen - 2*(passlen > 3)); + ESP_LOGD(TAG, "%s Wi-Fi Password : %s", pretext, (const char *) sta.password); + } +} + +esp_err_t wifi_prov_mgr_is_provisioned(bool *provisioned) +{ + if (!provisioned) { + return ESP_ERR_INVALID_ARG; + } + + *provisioned = false; + + if (!prov_ctx_lock) { + ESP_LOGE(TAG, "Provisioning manager not initialized"); + return ESP_ERR_INVALID_STATE; + } + + /* Get Wi-Fi Station configuration */ + wifi_config_t wifi_cfg; + if (esp_wifi_get_config(ESP_IF_WIFI_STA, &wifi_cfg) != ESP_OK) { + return ESP_FAIL; + } + + if (strlen((const char *) wifi_cfg.sta.ssid)) { + *provisioned = true; + debug_print_wifi_credentials(wifi_cfg.sta, "Found"); + } + return ESP_OK; +} + +esp_err_t wifi_prov_mgr_configure_sta(wifi_config_t *wifi_cfg) +{ + if (!prov_ctx_lock) { + ESP_LOGE(TAG, "Provisioning manager not initialized"); + return ESP_ERR_INVALID_STATE; + } + + xSemaphoreTakeRecursive(prov_ctx_lock, portMAX_DELAY); + if (!prov_ctx) { + ESP_LOGE(TAG, "Invalid state of Provisioning app"); + xSemaphoreGiveRecursive(prov_ctx_lock); + return ESP_FAIL; + } + if (prov_ctx->prov_state >= WIFI_PROV_STATE_CRED_RECV) { + ESP_LOGE(TAG, "Wi-Fi credentials already received by provisioning app"); + xSemaphoreGiveRecursive(prov_ctx_lock); + return ESP_FAIL; + } + debug_print_wifi_credentials(wifi_cfg->sta, "Received"); + + /* Configure Wi-Fi as both AP and/or Station */ + if (esp_wifi_set_mode(prov_ctx->mgr_config.scheme.wifi_mode) != ESP_OK) { + ESP_LOGE(TAG, "Failed to set Wi-Fi mode"); + xSemaphoreGiveRecursive(prov_ctx_lock); + return ESP_FAIL; + } + + /* Don't release mutex yet as it is possible that right after + * esp_wifi_connect() is called below, the related Wi-Fi event + * happens even before manager state is updated in the next + * few lines causing the internal event handler to miss */ + + /* Set Wi-Fi storage again to flash to keep the newly + * provided credentials on NVS */ + if (esp_wifi_set_storage(WIFI_STORAGE_FLASH) != ESP_OK) { + ESP_LOGE(TAG, "Failed to set storage Wi-Fi"); + xSemaphoreGiveRecursive(prov_ctx_lock); + return ESP_FAIL; + } + /* Configure Wi-Fi station with host credentials + * provided during provisioning */ + if (esp_wifi_set_config(ESP_IF_WIFI_STA, wifi_cfg) != ESP_OK) { + ESP_LOGE(TAG, "Failed to set Wi-Fi configuration"); + xSemaphoreGiveRecursive(prov_ctx_lock); + return ESP_FAIL; + } + /* (Re)Start Wi-Fi */ + if (esp_wifi_start() != ESP_OK) { + ESP_LOGE(TAG, "Failed to set Wi-Fi configuration"); + xSemaphoreGiveRecursive(prov_ctx_lock); + return ESP_FAIL; + } + /* Connect to AP */ + if (esp_wifi_connect() != ESP_OK) { + ESP_LOGE(TAG, "Failed to connect Wi-Fi"); + xSemaphoreGiveRecursive(prov_ctx_lock); + return ESP_FAIL; + } + /* This delay allows channel change to complete */ + vTaskDelay(1000 / portTICK_PERIOD_MS); + + /* Reset Wi-Fi station state for provisioning app */ + prov_ctx->wifi_state = WIFI_PROV_STA_CONNECTING; + prov_ctx->prov_state = WIFI_PROV_STATE_CRED_RECV; + /* Execute user registered callback handler */ + execute_event_cb(WIFI_PROV_CRED_RECV, (void *)&wifi_cfg->sta); + xSemaphoreGiveRecursive(prov_ctx_lock); + + return ESP_OK; +} + +esp_err_t wifi_prov_mgr_init(wifi_prov_mgr_config_t config) +{ + if (!prov_ctx_lock) { + /* Create mutex if this is the first time init is being called. + * This is created only once and never deleted because if some + * other thread is trying to take this mutex while it is being + * deleted from another thread then the reference may become + * invalid and cause exception */ + prov_ctx_lock = xSemaphoreCreateRecursiveMutex(); + if (!prov_ctx_lock) { + ESP_LOGE(TAG, "Failed to create mutex"); + return ESP_ERR_NO_MEM; + } + } + + void *fn_ptrs[] = { + config.scheme.prov_stop, + config.scheme.prov_start, + config.scheme.new_config, + config.scheme.delete_config, + config.scheme.set_config_service, + config.scheme.set_config_endpoint + }; + + /* All function pointers in the scheme structure must be non-null */ + for (int i = 0; i < sizeof(fn_ptrs)/sizeof(fn_ptrs[0]); i++) { + if (!fn_ptrs[i]) { + return ESP_ERR_INVALID_ARG; + } + } + + xSemaphoreTakeRecursive(prov_ctx_lock, portMAX_DELAY); + if (prov_ctx) { + ESP_LOGE(TAG, "Provisioning manager already initialized"); + xSemaphoreGiveRecursive(prov_ctx_lock); + return ESP_ERR_INVALID_STATE; + } + + /* Allocate memory for provisioning app data */ + prov_ctx = (struct wifi_prov_mgr_ctx *) calloc(1, sizeof(struct wifi_prov_mgr_ctx)); + if (!prov_ctx) { + ESP_LOGE(TAG, "Error allocating memory for singleton instance"); + xSemaphoreGiveRecursive(prov_ctx_lock); + return ESP_ERR_NO_MEM; + } + + prov_ctx->mgr_config = config; + prov_ctx->prov_state = WIFI_PROV_STATE_IDLE; + prov_ctx->mgr_info.version = WIFI_PROV_MGR_VERSION; + + /* Allocate memory for provisioning scheme configuration */ + const wifi_prov_scheme_t *scheme = &prov_ctx->mgr_config.scheme; + esp_err_t ret = ESP_OK; + prov_ctx->prov_scheme_config = scheme->new_config(); + if (!prov_ctx->prov_scheme_config) { + ESP_LOGE(TAG, "failed to allocate provisioning scheme configuration"); + ret = ESP_ERR_NO_MEM; + goto exit; + } + + ret = scheme->set_config_endpoint(prov_ctx->prov_scheme_config, "prov-session", 0xFF51); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "failed to configure security endpoint"); + goto exit; + } + + ret = scheme->set_config_endpoint(prov_ctx->prov_scheme_config, "prov-config", 0xFF52); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "failed to configure Wi-Fi configuration endpoint"); + goto exit; + } + + ret = scheme->set_config_endpoint(prov_ctx->prov_scheme_config, "proto-ver", 0xFF53); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "failed to configure version endpoint"); + goto exit; + } + + /* Application specific custom endpoints will be assigned + * incremental UUIDs starting after this value */ + prov_ctx->endpoint_uuid_used = 0xFF53; + + /* This delay is so that the client side app is notified first + * and then the provisioning is stopped. Default is 1000ms. */ + prov_ctx->cleanup_delay = 1000; + +exit: + if (ret != ESP_OK) { + if (prov_ctx->prov_scheme_config) { + config.scheme.delete_config(prov_ctx->prov_scheme_config); + } + free(prov_ctx); + } else { + execute_event_cb(WIFI_PROV_INIT, NULL); + } + xSemaphoreGiveRecursive(prov_ctx_lock); + return ret; +} + +void wifi_prov_mgr_wait(void) +{ + if (!prov_ctx_lock) { + ESP_LOGE(TAG, "Provisioning manager not initialized"); + return; + } + + while (1) { + xSemaphoreTakeRecursive(prov_ctx_lock, portMAX_DELAY); + if (prov_ctx && + prov_ctx->prov_state != WIFI_PROV_STATE_IDLE) { + xSemaphoreGiveRecursive(prov_ctx_lock); + vTaskDelay(1000 / portTICK_PERIOD_MS); + continue; + } + break; + } + xSemaphoreGiveRecursive(prov_ctx_lock); +} + +void wifi_prov_mgr_deinit(void) +{ + if (!prov_ctx_lock) { + ESP_LOGE(TAG, "Provisioning manager not initialized"); + return; + } + + xSemaphoreTakeRecursive(prov_ctx_lock, portMAX_DELAY); + + /* This will do one of these: + * 1) if found running, stop the provisioning service (returns true) + * 2) if service was already in the process of termination, this will + * wait till the service is stopped (returns false) + * 3) if service was not running, this will return immediately (returns false) + */ + bool service_was_running = wifi_prov_mgr_stop_service(1); + + /* If service was not running, its also possible that manager + * was not even initialized */ + if (!service_was_running && !prov_ctx) { + ESP_LOGD(TAG, "Manager already de-initialized"); + xSemaphoreGiveRecursive(prov_ctx_lock); + return; + } + + if (prov_ctx->app_info_json) { + cJSON_Delete(prov_ctx->app_info_json); + } + + if (prov_ctx->prov_scheme_config) { + prov_ctx->mgr_config.scheme.delete_config(prov_ctx->prov_scheme_config); + } + + /* Extract the callbacks to be called post deinit */ + wifi_prov_cb_func_t app_cb = prov_ctx->mgr_config.app_event_handler.event_cb; + void *app_data = prov_ctx->mgr_config.app_event_handler.user_data; + + wifi_prov_cb_func_t scheme_cb = prov_ctx->mgr_config.scheme_event_handler.event_cb; + void *scheme_data = prov_ctx->mgr_config.scheme_event_handler.user_data; + + /* Free manager context */ + free(prov_ctx); + prov_ctx = NULL; + xSemaphoreGiveRecursive(prov_ctx_lock); + + /* If a running service was also stopped during de-initialization + * then WIFI_PROV_END event also needs to be emitted before deinit */ + if (service_was_running) { + ESP_LOGD(TAG, "execute_event_cb : %d", WIFI_PROV_END); + if (scheme_cb) { + scheme_cb(scheme_data, WIFI_PROV_END, NULL); + } + if (app_cb) { + app_cb(app_data, WIFI_PROV_END, NULL); + } + } + + ESP_LOGD(TAG, "execute_event_cb : %d", WIFI_PROV_DEINIT); + + /* Execute deinit event callbacks */ + if (scheme_cb) { + scheme_cb(scheme_data, WIFI_PROV_DEINIT, NULL); + } + if (app_cb) { + app_cb(app_data, WIFI_PROV_DEINIT, NULL); + } +} + +esp_err_t wifi_prov_mgr_start_provisioning(wifi_prov_security_t security, const char *pop, + const char *service_name, const char *service_key) +{ + if (!prov_ctx_lock) { + ESP_LOGE(TAG, "Provisioning manager not initialized"); + return ESP_ERR_INVALID_STATE; + } + + xSemaphoreTakeRecursive(prov_ctx_lock, portMAX_DELAY); + if (!prov_ctx) { + ESP_LOGE(TAG, "Provisioning manager not initialized"); + xSemaphoreGiveRecursive(prov_ctx_lock); + return ESP_ERR_INVALID_STATE; + } + + if (prov_ctx->prov_state != WIFI_PROV_STATE_IDLE) { + ESP_LOGE(TAG, "Provisioning service already started"); + xSemaphoreGiveRecursive(prov_ctx_lock); + return ESP_ERR_INVALID_STATE; + } + + esp_err_t ret = ESP_OK; + /* Update state so that parallel call to wifi_prov_mgr_start_provisioning() + * or wifi_prov_mgr_stop_provisioning() or wifi_prov_mgr_deinit() from another + * thread doesn't interfere with this process */ + prov_ctx->prov_state = WIFI_PROV_STATE_STARTING; + + /* Change Wi-Fi storage to RAM temporarily and erase any old + * credentials (i.e. without erasing the copy on NVS). Also + * call disconnect to make sure device doesn't remain connected + * to the AP whose credentials were present earlier */ + wifi_config_t wifi_cfg_empty, wifi_cfg_old; + memset(&wifi_cfg_empty, 0, sizeof(wifi_config_t)); + esp_wifi_get_config(ESP_IF_WIFI_STA, &wifi_cfg_old); + esp_wifi_set_storage(WIFI_STORAGE_RAM); + esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_cfg_empty); + esp_wifi_disconnect(); + + /* Initialize app data */ + if (pop) { + prov_ctx->pop.len = strlen(pop); + prov_ctx->pop.data = malloc(prov_ctx->pop.len); + if (!prov_ctx->pop.data) { + ESP_LOGE(TAG, "Unable to allocate PoP data"); + ret = ESP_ERR_NO_MEM; + goto err; + } + memcpy((void *)prov_ctx->pop.data, pop, prov_ctx->pop.len); + } else { + prov_ctx->mgr_info.capabilities.no_pop = true; + } + prov_ctx->security = security; + + /* If auto stop on completion is enabled (default) create the stopping timer */ + if (!prov_ctx->mgr_info.capabilities.no_auto_stop) { + /* Create timer object as a member of app data */ + esp_timer_create_args_t timer_conf = { + .callback = stop_prov_timer_cb, + .arg = NULL, + .dispatch_method = ESP_TIMER_TASK, + .name = "wifi_prov_mgr_tm" + }; + ret = esp_timer_create(&timer_conf, &prov_ctx->timer); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "Failed to create timer"); + free((void *)prov_ctx->pop.data); + goto err; + } + } + + /* System APIs for BLE / Wi-Fi will be called inside wifi_prov_mgr_start_service(), + * which may trigger system level events. Hence, releasing the context lock will + * ensure that wifi_prov_mgr_event_handler() doesn't block the global event_loop + * handler when system events need to be handled */ + xSemaphoreGiveRecursive(prov_ctx_lock); + + /* Start provisioning service */ + ret = wifi_prov_mgr_start_service(service_name, service_key); + if (ret != ESP_OK) { + esp_timer_delete(prov_ctx->timer); + free((void *)prov_ctx->pop.data); + } + xSemaphoreTakeRecursive(prov_ctx_lock, portMAX_DELAY); + if (ret == ESP_OK) { + prov_ctx->prov_state = WIFI_PROV_STATE_STARTED; + /* Execute user registered callback handler */ + execute_event_cb(WIFI_PROV_START, NULL); + goto exit; + } + +err: + prov_ctx->prov_state = WIFI_PROV_STATE_IDLE; + esp_wifi_set_storage(WIFI_STORAGE_FLASH); + esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_cfg_old); + +exit: + xSemaphoreGiveRecursive(prov_ctx_lock); + return ret; +} + +void wifi_prov_mgr_stop_provisioning(void) +{ + if (!prov_ctx_lock) { + ESP_LOGE(TAG, "Provisioning manager not initialized"); + return; + } + + xSemaphoreTakeRecursive(prov_ctx_lock, portMAX_DELAY); + + /* Launches task for stopping the provisioning service. This will do one of these: + * 1) start a task for stopping the provisioning service (returns true) + * 2) if service was already in the process of termination, this will + * wait till the service is stopped (returns false) + * 3) if service was not running, this will return immediately (returns false) + */ + wifi_prov_mgr_stop_service(0); + + xSemaphoreGiveRecursive(prov_ctx_lock); +} diff --git a/components/wifi_provisioning/src/scheme_ble.c b/components/wifi_provisioning/src/scheme_ble.c new file mode 100644 index 0000000000..add9084016 --- /dev/null +++ b/components/wifi_provisioning/src/scheme_ble.c @@ -0,0 +1,232 @@ +// Copyright 2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include +#include + +#include +#include + +#include "wifi_provisioning/scheme_ble.h" +#include "wifi_provisioning_priv.h" + +static const char *TAG = "wifi_prov_scheme_ble"; + +extern const wifi_prov_scheme_t wifi_prov_scheme_ble; + +static uint8_t *custom_service_uuid; + +static esp_err_t prov_start(protocomm_t *pc, void *config) +{ + if (!pc) { + ESP_LOGE(TAG, "Protocomm handle cannot be null"); + return ESP_ERR_INVALID_ARG; + } + + if (!config) { + ESP_LOGE(TAG, "Cannot start with null configuration"); + return ESP_ERR_INVALID_ARG; + } + + protocomm_ble_config_t *ble_config = (protocomm_ble_config_t *) config; + + /* Start protocomm as BLE service */ + if (protocomm_ble_start(pc, ble_config) != ESP_OK) { + ESP_LOGE(TAG, "Failed to start protocomm BLE service"); + return ESP_FAIL; + } + return ESP_OK; +} + +esp_err_t wifi_prov_scheme_ble_set_service_uuid(uint8_t *uuid128) +{ + if (!uuid128) { + return ESP_ERR_INVALID_ARG; + } + custom_service_uuid = uuid128; + return ESP_OK; +} + +static void *new_config(void) +{ + protocomm_ble_config_t *ble_config = calloc(1, sizeof(protocomm_ble_config_t)); + if (!ble_config) { + ESP_LOGE(TAG, "Error allocating memory for new configuration"); + return NULL; + } + + /* The default provisioning service UUID */ + const uint8_t service_uuid[16] = { + /* LSB <--------------------------------------- + * ---------------------------------------> MSB */ + 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, + 0x00, 0x10, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, + }; + + memcpy(ble_config->service_uuid, service_uuid, sizeof(ble_config->service_uuid)); + return ble_config; +} + +static void delete_config(void *config) +{ + if (!config) { + ESP_LOGE(TAG, "Cannot delete null configuration"); + return; + } + + protocomm_ble_config_t *ble_config = (protocomm_ble_config_t *) config; + for (unsigned int i = 0; i < ble_config->nu_lookup_count; i++) { + free((void *)ble_config->nu_lookup[i].name); + } + free(ble_config->nu_lookup); + free(ble_config); +} + +static esp_err_t set_config_service(void *config, const char *service_name, const char *service_key) +{ + if (!config) { + ESP_LOGE(TAG, "Cannot set null configuration"); + return ESP_ERR_INVALID_ARG; + } + + if (!service_name) { + ESP_LOGE(TAG, "Service name cannot be NULL"); + return ESP_ERR_INVALID_ARG; + } + + protocomm_ble_config_t *ble_config = (protocomm_ble_config_t *) config; + strlcpy(ble_config->device_name, service_name, sizeof(ble_config->device_name)); + + /* If a custom service UUID has been provided, override the default one */ + if (custom_service_uuid) { + memcpy(ble_config->service_uuid, custom_service_uuid, sizeof(ble_config->service_uuid)); + } + return ESP_OK; +} + +static esp_err_t set_config_endpoint(void *config, const char *endpoint_name, uint16_t uuid) +{ + if (!config) { + ESP_LOGE(TAG, "Cannot set null configuration"); + return ESP_ERR_INVALID_ARG; + } + + if (!endpoint_name) { + ESP_LOGE(TAG, "EP name cannot be null"); + return ESP_ERR_INVALID_ARG; + } + + protocomm_ble_config_t *ble_config = (protocomm_ble_config_t *) config; + + char *copy_ep_name = strdup(endpoint_name); + if (!copy_ep_name) { + ESP_LOGE(TAG, "Error allocating memory for EP name"); + return ESP_ERR_NO_MEM; + } + + protocomm_ble_name_uuid_t *lookup_table = ( + realloc(ble_config->nu_lookup, (ble_config->nu_lookup_count + 1) * sizeof(protocomm_ble_name_uuid_t))); + if (!lookup_table) { + ESP_LOGE(TAG, "Error allocating memory for EP-UUID lookup table"); + return ESP_ERR_NO_MEM; + } + + lookup_table[ble_config->nu_lookup_count].name = copy_ep_name; + lookup_table[ble_config->nu_lookup_count].uuid = uuid; + ble_config->nu_lookup = lookup_table; + ble_config->nu_lookup_count += 1; + return ESP_OK; +} + +/* Used when both BT and BLE are not needed by application */ +void wifi_prov_scheme_ble_event_cb_free_btdm(void *user_data, wifi_prov_cb_event_t event, void *event_data) +{ + esp_err_t err; + switch (event) { + case WIFI_PROV_INIT: + /* Release BT memory, as we need only BLE */ + err = esp_bt_mem_release(ESP_BT_MODE_CLASSIC_BT); + if (err != ESP_OK) { + ESP_LOGE(TAG, "bt_mem_release of classic BT failed %d", err); + } else { + ESP_LOGI(TAG, "BT memory released"); + } + break; + + case WIFI_PROV_DEINIT: + /* Release memory used by BLE and Bluedroid host stack */ + err = esp_bt_mem_release(ESP_BT_MODE_BTDM); + if (err != ESP_OK) { + ESP_LOGE(TAG, "bt_mem_release of BTDM failed %d", err); + } else { + ESP_LOGI(TAG, "BTDM memory released"); + } + break; + + default: + break; + } +} + +/* Used when BT is not needed by application */ +void wifi_prov_scheme_ble_event_cb_free_bt(void *user_data, wifi_prov_cb_event_t event, void *event_data) +{ + esp_err_t err; + switch (event) { + case WIFI_PROV_INIT: + /* Release BT memory, as we need only BLE */ + err = esp_bt_mem_release(ESP_BT_MODE_CLASSIC_BT); + if (err != ESP_OK) { + ESP_LOGE(TAG, "bt_mem_release of classic BT failed %d", err); + } else { + ESP_LOGI(TAG, "BT memory released"); + } + break; + + default: + break; + } +} + +/* Used when BLE is not needed by application */ +void wifi_prov_scheme_ble_event_cb_free_ble(void *user_data, wifi_prov_cb_event_t event, void *event_data) +{ + esp_err_t err; + switch (event) { + case WIFI_PROV_DEINIT: + /* Release memory used by BLE stack */ + err = esp_bt_mem_release(ESP_BT_MODE_BLE); + if (err != ESP_OK) { + ESP_LOGE(TAG, "bt_mem_release of BLE failed %d", err); + } else { + ESP_LOGI(TAG, "BLE memory released"); + } + break; + + default: + break; + } +} + +const wifi_prov_scheme_t wifi_prov_scheme_ble = { + .prov_start = prov_start, + .prov_stop = protocomm_ble_stop, + .new_config = new_config, + .delete_config = delete_config, + .set_config_service = set_config_service, + .set_config_endpoint = set_config_endpoint, + .wifi_mode = WIFI_MODE_STA +}; diff --git a/components/wifi_provisioning/src/scheme_console.c b/components/wifi_provisioning/src/scheme_console.c new file mode 100644 index 0000000000..28732847ac --- /dev/null +++ b/components/wifi_provisioning/src/scheme_console.c @@ -0,0 +1,92 @@ +// Copyright 2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include +#include + +#include +#include + +#include "wifi_provisioning/scheme_console.h" +#include "wifi_provisioning_priv.h" + +static const char *TAG = "wifi_prov_scheme_console"; + +extern const wifi_prov_scheme_t wifi_prov_scheme_console; + +static esp_err_t prov_start(protocomm_t *pc, void *config) +{ + if (!pc) { + ESP_LOGE(TAG, "Protocomm handle cannot be null"); + return ESP_ERR_INVALID_ARG; + } + + if (!config) { + ESP_LOGE(TAG, "Cannot start with null configuration"); + return ESP_ERR_INVALID_ARG; + } + + protocomm_console_config_t *console_config = (protocomm_console_config_t *) config; + + /* Start protocomm console */ + esp_err_t err = protocomm_console_start(pc, console_config); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Failed to start protocomm HTTP server"); + return ESP_FAIL; + } + return ESP_OK; +} + +static void *new_config(void) +{ + protocomm_console_config_t *console_config = malloc(sizeof(protocomm_console_config_t)); + if (!console_config) { + ESP_LOGE(TAG, "Error allocating memory for new configuration"); + return NULL; + } + protocomm_console_config_t default_config = PROTOCOMM_CONSOLE_DEFAULT_CONFIG(); + memcpy(console_config, &default_config, sizeof(default_config)); + return console_config; +} + +static void delete_config(void *config) +{ + if (!config) { + ESP_LOGE(TAG, "Cannot delete null configuration"); + return; + } + free(config); +} + +static esp_err_t set_config_service(void *config, const char *service_name, const char *service_key) +{ + return ESP_OK; +} + +static esp_err_t set_config_endpoint(void *config, const char *endpoint_name, uint16_t uuid) +{ + return ESP_OK; +} + +const wifi_prov_scheme_t wifi_prov_scheme_console = { + .prov_start = prov_start, + .prov_stop = protocomm_console_stop, + .new_config = new_config, + .delete_config = delete_config, + .set_config_service = set_config_service, + .set_config_endpoint = set_config_endpoint, + .wifi_mode = WIFI_MODE_STA +}; diff --git a/components/wifi_provisioning/src/scheme_softap.c b/components/wifi_provisioning/src/scheme_softap.c new file mode 100644 index 0000000000..270ef1f587 --- /dev/null +++ b/components/wifi_provisioning/src/scheme_softap.c @@ -0,0 +1,202 @@ +// Copyright 2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include +#include + +#include +#include +#include + +#include "wifi_provisioning/scheme_softap.h" +#include "wifi_provisioning_priv.h" + +typedef struct softap_config { + protocomm_httpd_config_t httpd_config; + char ssid[33]; + char password[65]; +} wifi_prov_softap_config_t; + +static const char *TAG = "wifi_prov_scheme_softap"; + +extern const wifi_prov_scheme_t wifi_prov_scheme_softap; + +static esp_err_t start_wifi_ap(const char *ssid, const char *pass) +{ + /* Build Wi-Fi configuration for AP mode */ + wifi_config_t wifi_config = { + .ap = { + .max_connection = 5, + }, + }; + + strncpy((char *) wifi_config.ap.ssid, ssid, sizeof(wifi_config.ap.ssid)); + wifi_config.ap.ssid_len = strnlen(ssid, sizeof(wifi_config.ap.ssid)); + + if (strlen(pass) == 0) { + memset(wifi_config.ap.password, 0, sizeof(wifi_config.ap.password)); + wifi_config.ap.authmode = WIFI_AUTH_OPEN; + } else { + strlcpy((char *) wifi_config.ap.password, pass, sizeof(wifi_config.ap.password)); + wifi_config.ap.authmode = WIFI_AUTH_WPA_WPA2_PSK; + } + + /* Start Wi-Fi in AP mode with configuration built above */ + esp_err_t err = esp_wifi_set_mode(WIFI_MODE_AP); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Failed to set Wi-Fi mode : %d", err); + return err; + } + err = esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Failed to set Wi-Fi config : %d", err); + return err; + } + err = esp_wifi_start(); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Failed to start Wi-Fi : %d", err); + return err; + } + ESP_LOGD(TAG, "Wi-Fi SoftAP started"); + return ESP_OK; +} + +static esp_err_t prov_start(protocomm_t *pc, void *config) +{ + if (!pc) { + ESP_LOGE(TAG, "Protocomm handle cannot be null"); + return ESP_ERR_INVALID_ARG; + } + + if (!config) { + ESP_LOGE(TAG, "Cannot start with null configuration"); + return ESP_ERR_INVALID_ARG; + } + + wifi_prov_softap_config_t *softap_config = (wifi_prov_softap_config_t *) config; + + protocomm_httpd_config_t *httpd_config = &softap_config->httpd_config; + + /* Start protocomm server on top of HTTP */ + esp_err_t err = protocomm_httpd_start(pc, httpd_config); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Failed to start protocomm HTTP server"); + return err; + } + + /* Start Wi-Fi softAP with specified ssid and password */ + err = start_wifi_ap(softap_config->ssid, softap_config->password); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Failed to start Wi-Fi AP"); + protocomm_httpd_stop(pc); + return err; + } + + /* Add mDNS service for allowing discovery of provisioning + * service on the SoftAP network (Optional). Even though + * this is an http service we identify it by _esp_wifi_prov so + * that application is free to use _http without conflict */ + err = mdns_service_add("Wi-Fi Provisioning Service", "_esp_wifi_prov", "_tcp", + softap_config->httpd_config.data.config.port, NULL, 0); + if (err != ESP_OK) { + /* mDNS is not mandatory for provisioning to work, + * so print warning and return without failure */ + ESP_LOGW(TAG, "Error adding mDNS service! Check if mDNS is running"); + } else { + /* Information to identify the roles of the various + * protocomm endpoint URIs provided by the service */ + err |= mdns_service_txt_item_set("_esp_wifi_prov", "_tcp", "version_endpoint", "/proto-ver"); + err |= mdns_service_txt_item_set("_esp_wifi_prov", "_tcp", "session_endpoint", "/prov-session"); + err |= mdns_service_txt_item_set("_esp_wifi_prov", "_tcp", "config_endpoint", "/prov-config"); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Error adding mDNS service text item"); + } + } + return ESP_OK; +} + +static esp_err_t prov_stop(protocomm_t *pc) +{ + esp_err_t err = protocomm_httpd_stop(pc); + if (err != ESP_OK) { + ESP_LOGW(TAG, "Error occurred while stopping protocomm_httpd"); + } + + mdns_service_remove("_esp_wifi_prov", "_tcp"); + return err; +} + +static void *new_config(void) +{ + wifi_prov_softap_config_t *softap_config = calloc(1, sizeof(wifi_prov_softap_config_t)); + if (!softap_config) { + ESP_LOGE(TAG, "Error allocating memory for new configuration"); + return NULL; + } + protocomm_httpd_config_t default_config = { + .data = { + .config = PROTOCOMM_HTTPD_DEFAULT_CONFIG() + } + }; + softap_config->httpd_config = default_config; + return softap_config; +} + +static void delete_config(void *config) +{ + if (!config) { + ESP_LOGE(TAG, "Cannot delete null configuration"); + return; + } + + wifi_prov_softap_config_t *softap_config = (wifi_prov_softap_config_t *) config; + free(softap_config); +} + +static esp_err_t set_config_service(void *config, const char *service_name, const char *service_key) +{ + if (!config) { + ESP_LOGE(TAG, "Cannot set null configuration"); + return ESP_ERR_INVALID_ARG; + } + + if (!service_name) { + ESP_LOGE(TAG, "Service name cannot be NULL"); + return ESP_ERR_INVALID_ARG; + } + + wifi_prov_softap_config_t *softap_config = (wifi_prov_softap_config_t *) config; + strlcpy(softap_config->ssid, service_name, sizeof(softap_config->ssid)); + if (service_key) { + strlcpy(softap_config->password, service_key, sizeof(softap_config->password)); + } + return ESP_OK; +} + +static esp_err_t set_config_endpoint(void *config, const char *endpoint_name, uint16_t uuid) +{ + return ESP_OK; +} + +const wifi_prov_scheme_t wifi_prov_scheme_softap = { + .prov_start = prov_start, + .prov_stop = prov_stop, + .new_config = new_config, + .delete_config = delete_config, + .set_config_service = set_config_service, + .set_config_endpoint = set_config_endpoint, + .wifi_mode = WIFI_MODE_APSTA +}; diff --git a/components/wifi_provisioning/src/wifi_provisioning_priv.h b/components/wifi_provisioning/src/wifi_provisioning_priv.h new file mode 100644 index 0000000000..e93c0859d9 --- /dev/null +++ b/components/wifi_provisioning/src/wifi_provisioning_priv.h @@ -0,0 +1,44 @@ +// Copyright 2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once + +#include + +#include +#include + +#include "wifi_provisioning/manager.h" +#include "wifi_provisioning/wifi_config.h" + +/** + * @brief Notify manager that provisioning is done + * + * Stops the provisioning. This is called by the get_status_handler() + * when the status is connected. This has no effect if main application + * has disabled auto stop on completion by calling + * wifi_prov_mgr_disable_auto_stop() + * + * @return + * - ESP_OK : Provisioning will be stopped + * - ESP_FAIL : Failed to stop provisioning + */ +esp_err_t wifi_prov_mgr_done(void); + +/** + * @brief Get protocomm handlers for wifi_config provisioning endpoint + * + * @return wifi_prov_config_handlers_t structure + */ +wifi_prov_config_handlers_t get_wifi_prov_handlers(void); From 471a0e93a79f2f0d4a9d6fdcc3779ad4a08c24ce Mon Sep 17 00:00:00 2001 From: Anurag Kar Date: Sun, 21 Apr 2019 20:23:31 +0530 Subject: [PATCH 2/4] wifi_provisioning : Docs updated with information about new provisioning manager --- docs/Doxyfile | 4 + .../provisioning/wifi_provisioning.rst | 320 +++++++++++++++--- 2 files changed, 282 insertions(+), 42 deletions(-) diff --git a/docs/Doxyfile b/docs/Doxyfile index cd92c69096..6a937c2eb9 100644 --- a/docs/Doxyfile +++ b/docs/Doxyfile @@ -118,6 +118,10 @@ INPUT = \ ../../components/protocomm/include/transports/protocomm_console.h \ ../../components/protocomm/include/transports/protocomm_httpd.h \ ## WiFi Provisioning + ../../components/wifi_provisioning/include/wifi_provisioning/manager.h \ + ../../components/wifi_provisioning/include/wifi_provisioning/scheme_ble.h \ + ../../components/wifi_provisioning/include/wifi_provisioning/scheme_softap.h \ + ../../components/wifi_provisioning/include/wifi_provisioning/scheme_console.h \ ../../components/wifi_provisioning/include/wifi_provisioning/wifi_config.h \ ## ## Storage - API Reference diff --git a/docs/en/api-reference/provisioning/wifi_provisioning.rst b/docs/en/api-reference/provisioning/wifi_provisioning.rst index 7f78ee5830..54797d5cfb 100644 --- a/docs/en/api-reference/provisioning/wifi_provisioning.rst +++ b/docs/en/api-reference/provisioning/wifi_provisioning.rst @@ -4,60 +4,296 @@ Wi-Fi Provisioning Overview -------- -This component provides protocomm endpoint handler - `wifi_prov_config_data_handler` - and related protobuf framework which can be used for Wi-Fi configuration in the context of device provisioning, though it may be used in non-provisioning cases as well. +This component provides APIs that control Wi-Fi provisioning service for receiving and configuring Wi-Fi credentials over SoftAP or BLE transport via secure :doc:`Protocol Communication (protocomm)` sessions. The set of ``wifi_prov_mgr_`` APIs help in quickly implementing a provisioning service having necessary features with minimal amount of code and sufficient flexibility. -The configuration consists of three commands : - * `get_status` - For querying the Wi-Fi connection status - * `set_config` - For setting the Wi-Fi connection credentials - * `apply_config` - For applying the credentials saved during `set_config` and (re)start the Wi-Fi station +.. _wifi-prov-mgr-init: -The way this is supposed to work is that the desired Wi-Fi configuration for the ESP32, which is to run as a station and thus connect to an AP with certain credentials, is to be sent during `set_config`. Then `apply_config` is supposed to start (or restart) the Wi-Fi in station mode with the previously set AP credentials. Afterwords, `get_config` command is used to probe the device continuously for Wi-Fi connection status, to ensure that the connection was indeed successful. If the connection failed, then appropriate status code along with disconnection reason, is to be conveyed through `get_config`. +Initialization +^^^^^^^^^^^^^^ -Application Example -------------------- +:cpp:func:`wifi_prov_mgr_init()` is called to configure and initialize the provisioning manager and thus this must be called prior to invoking any other ``wifi_prov_mgr_`` APIs. Note that the manager relies on other components of IDF, namely NVS, TCP/IP, Event Loop and Wi-Fi (and optionally mDNS), hence these must be initialized beforehand. The manager can be de-initialized at any moment by making a call to :cpp:func:`wifi_prov_mgr_deinit()`. .. highlight:: c :: - esp_err_t get_status_handler(wifi_prov_config_get_data_t *resp_data, wifi_prov_ctx_t **ctx) - { - /* Fill the wifi_prov_config_get_data_t structure - * with Wi-Fi station connection status information. */ - - return ESP_OK; - } - - esp_err_t set_config_handler(const wifi_prov_config_set_data_t *req_data, wifi_prov_ctx_t **ctx) - { - /* Copy contents of req_data->ssid and req_data->password - * which are Wi-Fi AP credentials to which the device will connect */ - - return ESP_OK; - } - - esp_err_t apply_config_handler(wifi_prov_ctx_t **ctx) - { - /* Apply the Wi-Fi STA credentials saved during set_config */ - - return ESP_OK; - } - - /* Structure with various config command handlers to be passed - * as private data during endpoint registration with protocomm */ - wifi_prov_config_handlers_t wifi_prov_handlers = { - .get_status_handler = get_status_handler, - .set_config_handler = set_config_handler, - .apply_config_handler = apply_config_handler, - .ctx = NULL + 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 + } }; - /* Set the endpoint handler */ - protocomm_add_endpoint(pc, "wifi_config_endpoint", - wifi_prov_config_data_handler, - (void *) &wifi_prov_handlers); + ESP_ERR_CHECK( wifi_prov_mgr_init(config) ); + + +The configuration structure ``wifi_prov_mgr_config_t`` has a few fields to specify the behavior desired of the manager : + + * `scheme` : This is used to specify the provisioning scheme. Each scheme corresponds to one of the modes of transport supported by protocomm. Hence, we have three options : + + * ``wifi_prov_scheme_ble`` : BLE transport and GATT Server for handling provisioning commands + * ``wifi_prov_scheme_softap`` : Wi-Fi SoftAP transport and HTTP Server for handling provisioning commands + * ``wifi_prov_scheme_console`` : Serial transport and console for handling provisioning commands + + * `scheme_event_handler` : An event handler defined along with scheme. Choosing appropriate scheme specific event handler allows the manager to take care of certain matters automatically. Presently this is not used for either SoftAP or Console based provisioning, but is very convenient for BLE. To understand how, we must recall that Bluetooth requires quite some amount of memory to function and once provisioning is finished, the main application may want to reclaim back this memory (or part of it, if it needs to use either BLE or classic BT). Also, upon every future reboot of a provisioned device, this reclamation of memory needs to be performed again. To reduce this complication in using ``wifi_prov_scheme_ble``, the scheme specific handlers have been defined, and depending upon the chosen handler, the BLE / classic BT / BTDM memory will be freed automatically when the provisioning manager is de-initialized. The available options are: + + * ``WIFI_PROV_SCHEME_BLE_EVENT_HANDLER_FREE_BTDM`` - Free both classic BT and BLE (BTDM) memory. Used when main application doesn't require Bluetooth at all. + * ``WIFI_PROV_SCHEME_BLE_EVENT_HANDLER_FREE_BLE`` - Free only BLE memory. Used when main application requires classic BT. + * ``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 : + + .. highlight:: c + + :: + + void prov_event_handler(void *user_data, + wifi_prov_cb_event_t event, + 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; + } + 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; + } + } + +.. _wifi-prov-check-state: + +Check Provisioning State +^^^^^^^^^^^^^^^^^^^^^^^^ + +Whether device is provisioned or not can be checked at runtime by calling :cpp:func:`wifi_prov_mgr_is_provisioned()`. This internally checks if the Wi-Fi credentials are stored in NVS. + +Note that presently manager does not have its own NVS namespace for storage of Wi-Fi credentials, instead it relies on the ``esp_wifi_`` APIs to set and get the credentials stored in NVS from the default location. + +If provisioning state needs to be reset, any of the following approaches may be taken : + + * the associated part of NVS partition has to be erased manually + * main application must implement some logic to call ``esp_wifi_`` APIs for erasing the credentials at runtime + * main application must implement some logic to force start the provisioning irrespective of the provisioning state + + .. highlight:: c + + :: + + bool provisioned = false; + 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 +^^^^^^^^^^^^^^^^^^^^^^^^^^ + +At the time of starting provisioning we need to specify a service name and the corresponding key. These translate to : + + * Wi-Fi SoftAP SSID and passphrase, respectively, when scheme is ``wifi_prov_scheme_softap`` + * BLE Device name (service key is ignored) when scheme is ``wifi_prov_scheme_ble`` + +Also, since internally the manager uses `protocomm`, we have the option of choosing one of the security features provided by it : + + * Security 1 is secure communication which consists of a prior handshake involving X25519 key exchange along with authentication using a proof of possession (`pop`), followed by AES-CTR for encryption/decryption of subsequent messages + * Security 0 is simply plain text communication. In this case the `pop` is simply ignored + +See :doc:`Provisioning` for details about the security features. + + .. highlight:: c + + :: + + const char *service_name = "my_device"; + const char *service_key = "password"; + + wifi_prov_security_t security = WIFI_PROV_SECURITY_1; + const char *pop = "abcd1234"; + + ESP_ERR_CHECK( wifi_prov_mgr_start_provisioning(security, pop, service_name, service_key) ); + + +The provisioning service will automatically finish only if it receives valid Wi-Fi AP credentials followed by successfully connection of device to the AP (IP obtained). Regardless of that, the provisioning service can be stopped at any moment by making a call to :cpp:func:`wifi_prov_mgr_stop_provisioning()`. + +.. note:: + + If the device fails to connect with the provided credentials, it won't accept new credentials anymore, but the provisioning service will keep on running (only to convey failure to the client), until the device is restarted. Upon restart the provisioning state will turn out to be true this time (as credentials will be found in NVS), but device will again fail to connect with those same credentials (unless an AP with the matching credentials somehow does become available). This situation can be fixed by resetting the credentials in NVS or force starting the provisioning service. This has been explained above in :ref:`wifi-prov-check-state`. + + +Waiting For Completion +^^^^^^^^^^^^^^^^^^^^^^ + +Typically, the main application will wait for the provisioning to finish, then de-initialize the manager to free up resources and finally start executing its own logic. + +There are two ways for making this possible. The simpler way is to use a blocking call to :cpp:func:`wifi_prov_mgr_wait()`. + + .. highlight:: c + + :: + + // Start provisioning service + ESP_ERR_CHECK( wifi_prov_mgr_start_provisioning(security, pop, service_name, service_key) ); + + // Wait for service to complete + wifi_prov_mgr_wait(); + + // Finally de-initialize the manager + 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`. + + .. highlight:: c + + :: + + void prov_event_handler(void *user_data, wifi_prov_cb_event_t event, void *event_data) + { + switch (event) { + case WIFI_PROV_END: + // De-initialize manager once provisioning is finished + wifi_prov_mgr_deinit(); + break; + default: + break; + } + } + + +User Side Implementation +^^^^^^^^^^^^^^^^^^^^^^^^ + +When the service is started, the device to be provisioned is identified by the advertised service name which, depending upon the selected transport, is either the BLE device name or the SoftAP SSID. + +When using SoftAP transport, for allowing service discovery, mDNS must be initialized before starting provisioning. In this case the hostname set by the main application is used, and the service type is internally set to `_esp_wifi_prov`. + +When using BLE transport, a custom 128 bit UUID should be set using :cpp:func:`wifi_prov_scheme_ble_set_service_uuid()`. This UUID will be included in the BLE advertisement and will correspond to the primary GATT service that provides provisioning endpoints as GATT characteristics. Each GATT characteristic will be formed using the primary service UUID as base, with different auto assigned 12th and 13th bytes (assume counting starts from 0th byte). Since, an endpoint characteristic UUID is auto assigned, it shouldn't be used to identify the endpoint. Instead, client side applications should identify the endpoints by reading the User Characteristic Description (0x2901) descriptor for each characteristic, which contains the endpoint name of the characteristic. For example, if the service UUID is set to `55cc035e-fb27-4f80-be02-3c60828b7451`, each endpoint characteristic will be assigned a UUID like `55cc____-fb27-4f80-be02-3c60828b7451`, with unique values at the 12th and 13th bytes. + +Once connected to the device, the provisioning related protocomm endpoints can be identified as follows : + +.. list-table:: Endpoints provided by Provisioning Service + :widths: 10 25 50 + :header-rows: 1 + + * - Endpoint Name (BLE + GATT Server) + - URI (SoftAP + HTTP Server + mDNS) + - Description + * - prov-session + - http://.local/prov-session + - Security endpoint used for session establishment + * - prov-config + - http://.local/prov-config + - Endpoint used for configuring Wi-Fi credentials on device + * - proto-ver + - http://.local/proto-ver + - Endpoint for retrieving version info + +Immediately after connecting, the client application may fetch the version / capabilities information from the `proto-ver` endpoint. All communications to this endpoint are un-encrypted, hence necessary information (that may be relevant for deciding compatibility) can be retrieved before establishing a secure session. The response is in JSON format and looks like : ``prov: { ver: v1.1, cap: [no_pop] }, my_app: { ver: 1.345, cap: [cloud, local_ctrl] },....``. Here label `prov` provides provisioning service version (`ver`) and capabilities (`cap`). For now, only `no_pop` capability is supported, which indicates that the service doesn't require proof of possession for authentication. Any application related version / capabilities will be given by other labels (like `my_app` in this example). These additional fields are set using :cpp:func:`wifi_prov_mgr_set_app_info()`. + +User side applications need to implement the signature handshaking required for establishing and authenticating secure protocomm sessions as per the security scheme configured for use (this is not needed when manager is configured to use protocomm security 0). + +See Unified Provisioning for more details about the secure handshake and encryption used. Applications must use the `.proto` files found under `components/protocomm/proto `_, which define the Protobuf message structures supported by `prov-session` endpoint. + +Once a session is established, Wi-Fi credentials are configured using the following set of commands, serialized as Protobuf messages (the corresponding `.proto` files can be found under `components/wifi_provisioning/proto `_) : + + * `get_status` - For querying the Wi-Fi connection status. The device will respond with a status which will be one of connecting / connected / disconnected. If status is disconnected, a disconnection reason will also be included in the status response. + * `set_config` - For setting the Wi-Fi connection credentials + * `apply_config` - For applying the credentials saved during `set_config` and start the Wi-Fi station + + +Additional Endpoints +^^^^^^^^^^^^^^^^^^^^ + +In case users want to have some additional protocomm endpoints customized to their requirements, this is done in two steps. First is creation of an endpoint with a specific name, and the second step is the registration of a handler for this endpoint. See :doc:`protocomm` for the function signature of an endpoint handler. A custom endpoint must be created after initialization and before starting the provisioning service. Whereas, the protocomm handler is registered for this endpoint only after starting the provisioning service. + + .. highlight:: c + + :: + + wifi_prov_mgr_init(config); + wifi_prov_mgr_endpoint_create("custom-endpoint"); + wifi_prov_mgr_start_provisioning(security, pop, service_name, service_key); + wifi_prov_mgr_endpoint_register("custom-endpoint", custom_ep_handler, custom_ep_data); + + +When the provisioning service stops, the endpoint is unregistered automatically. + +One can also choose to call :cpp:func:`wifi_prov_mgr_endpoint_unregister()` to manually deactivate an endpoint at runtime. This can also be used to deactivate the internal endpoints used by the provisioning service. + +When / How To Stop Provisioning Service? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +The default behavior is that once the device successfully connects using the Wi-Fi credentials set by the `apply_config` command, the provisioning service will be stopped (and BLE / SoftAP turned off) automatically after responding to the next `get_status` command. If `get_status` command is not received by the device, the service will be stopped after a 30s timeout. + +On the other hand, if device was not able to connect using the provided Wi-Fi credentials, due to incorrect SSID / passphrase, the service will keep running, and `get_status` will keep responding with disconnected status and reason for disconnection. Any further attempts to provide another set of Wi-Fi credentials, will be rejected. These credentials will be preserved, unless the provisioning service is force started, or NVS erased. + +If this default behavior is not desired, it can be disabled by calling :cpp:func:`wifi_prov_mgr_disable_auto_stop()`. Now the provisioning service will only be stopped after an explicit call to :cpp:func:`wifi_prov_mgr_stop_provisioning()`, which returns immediately after scheduling a task for stopping the service. The service stops after a certain delay and WIFI_PROV_END event gets emitted. This delay is specified by the argument to :cpp:func:`wifi_prov_mgr_disable_auto_stop()`. + +The customized behavior is useful for applications which want the provisioning service to be stopped some time after the Wi-Fi connection is successfully established. For example, if the application requires the device to connect to some cloud service and obtain another set of credentials, and exchange this credentials over a custom protocomm endpoint, then after sucessfully doing so stop the provisioning service by calling :cpp:func:`wifi_prov_mgr_stop_provisioning()` inside the protocomm handler itself. The right amount of delay ensures that the transport resources are freed only after the response from the protocomm handler reaches the client side application. + +Application Examples +-------------------- + +For complete example implementation see :example:`provisioning/manager` API Reference ------------- +.. include:: /_build/inc/manager.inc +.. include:: /_build/inc/scheme_ble.inc +.. include:: /_build/inc/scheme_softap.inc +.. include:: /_build/inc/scheme_console.inc .. include:: /_build/inc/wifi_config.inc From f90d3f6d936691c32952aeec7d028acb9cf71b78 Mon Sep 17 00:00:00 2001 From: Anurag Kar Date: Tue, 23 Apr 2019 23:15:51 +0530 Subject: [PATCH 3/4] Provisioning : Added Wi-Fi Provisioning Manager example and test script --- examples/provisioning/manager/CMakeLists.txt | 6 + examples/provisioning/manager/Makefile | 9 + examples/provisioning/manager/README.md | 217 ++++++++++++++++ .../provisioning/manager/main/CMakeLists.txt | 4 + examples/provisioning/manager/main/app_main.c | 244 ++++++++++++++++++ .../provisioning/manager/main/component.mk | 8 + examples/provisioning/manager/partitions.csv | 5 + .../provisioning/manager/sdkconfig.defaults | 10 + .../manager/wifi_prov_mgr_test.py | 119 +++++++++ 9 files changed, 622 insertions(+) create mode 100644 examples/provisioning/manager/CMakeLists.txt create mode 100644 examples/provisioning/manager/Makefile create mode 100644 examples/provisioning/manager/README.md create mode 100644 examples/provisioning/manager/main/CMakeLists.txt create mode 100644 examples/provisioning/manager/main/app_main.c create mode 100644 examples/provisioning/manager/main/component.mk create mode 100644 examples/provisioning/manager/partitions.csv create mode 100644 examples/provisioning/manager/sdkconfig.defaults create mode 100644 examples/provisioning/manager/wifi_prov_mgr_test.py diff --git a/examples/provisioning/manager/CMakeLists.txt b/examples/provisioning/manager/CMakeLists.txt new file mode 100644 index 0000000000..b036d9ce60 --- /dev/null +++ b/examples/provisioning/manager/CMakeLists.txt @@ -0,0 +1,6 @@ +# The following lines of boilerplate have to be in your project's CMakeLists +# in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.5) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(wifi_prov_mgr) diff --git a/examples/provisioning/manager/Makefile b/examples/provisioning/manager/Makefile new file mode 100644 index 0000000000..151c25b72e --- /dev/null +++ b/examples/provisioning/manager/Makefile @@ -0,0 +1,9 @@ +# +# This is a project Makefile. It is assumed the directory this Makefile resides in is a +# project subdirectory. +# + +PROJECT_NAME := wifi_prov_mgr + +include $(IDF_PATH)/make/project.mk + diff --git a/examples/provisioning/manager/README.md b/examples/provisioning/manager/README.md new file mode 100644 index 0000000000..5bba6aad4c --- /dev/null +++ b/examples/provisioning/manager/README.md @@ -0,0 +1,217 @@ +# Wi-Fi Provisioning Manager Example + +(See the README.md file in the upper level 'examples' directory for more information about examples.) + +`wifi_prov_mgr` example demonstrates the usage of `wifi_provisioning` manager component for building a provisioning application. + +For this example BLE is chosen as the mode of transport, over which the provisioning related communication is to take place, between the device (to be provisioned) and the client (owner of the device). + +In the provisioning process the device is configured as a Wi-Fi station with specified credentials. Once configured, the device will retain the Wi-Fi configuration, until a flash erase is performed. + +Right after provisioning is complete, BLE is turned off and disabled to free the memory used by the BLE stack. Though, that is specific to this example, and the user can choose to keep BLE stack intact in their own application. + +`wifi_prov_mgr` uses the following components : +* `wifi_provisioning` : provides manager, data structures and protocomm endpoint handlers for Wi-Fi configuration +* `protocomm` : for protocol based communication and secure session establishment +* `protobuf` : Google's protocol buffer library for serialization of protocomm data structures +* `bt` : ESP32 BLE stack for transport of protobuf packets + +This example can be used, as it is, for adding a provisioning service to any application intended for IoT. + +## How to use example + +### Hardware Required + +Example should be able to run on any commonly available ESP32 development board. + +### Application Required + +Provisioning applications are available for various platforms. See below + +#### Platform : Android + +For Android, a provisioning application along with source code is available on GitHub : [esp-idf-provisioning-android](https://github.com/espressif/esp-idf-provisioning-android) + +#### Platform : iOS + +For iOS, a provisioning application along with source code is available on GitHub : [esp-idf-provisioning-ios](https://github.com/espressif/esp-idf-provisioning-ios) + +#### Platform : Linux / Windows / macOS + +To provision the device running this example, the `esp_prov.py` script needs to be run (found under `$IDF_PATH/tools/esp_prov`). Make sure to satisfy all the dependencies prior to running the script. + +Presently, `esp_prov` supports BLE transport only for Linux platform. For Windows/macOS it falls back to console mode and requires another application (for BLE) through which the communication can take place. + +There are various applications, specific to Windows and macOS platform which can be used. The `esp_prov` console will guide you through the provisioning process of locating the correct BLE GATT services and characteristics, the values to write, and input read values. + +### Configure the project + +``` +make menuconfig +``` + +* Set serial port under Serial Flasher Options. + +### Build and Flash + +Build the project and flash it to the board, then run monitor tool to view serial output: + +``` +make -j4 flash monitor +``` + +(To exit the serial monitor, type ``Ctrl-]``.) + +See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects. + +## Example Output + +``` +I (445) app: Starting provisioning +I (1035) app: Provisioning started +I (1045) wifi_prov_mgr: Provisioning started with service name : PROV_261FCC +``` + +Make sure to note down the BLE device name (starting with `PROV_`) displayed in the serial monitor log (eg. PROV_261FCC). This will depend on the MAC ID and will be unique for every device. + +In a separate terminal run the `esp_prov.py` script under `$IDP_PATH/tools/esp_prov` directory (please replace `myssid` and `mypassword` with the credentials of the AP to which the device is supposed to connect to after provisioning). Assuming default example configuration : + +``` +python esp_prov.py --ssid myssid --passphrase mypassword --sec_ver 1 --pop abcd1234 --transport ble --ble_devname PROV_261FCC +``` + +Above command will perform the provisioning steps, and the monitor log should display something like this : + +``` +I (39725) app: Received Wi-Fi credentials + SSID : myssid + Password : mypassword +. +. +. +I (45335) tcpip_adapter: sta ip: 192.168.43.243, mask: 255.255.255.0, gw: 192.168.43.1 +I (45345) app: Provisioning successful +I (45345) app: Connected with IP Address:192.168.43.243 +I (46355) app: Hello World! +I (47355) app: Hello World! +I (48355) app: Hello World! +I (49355) app: Hello World! +. +. +. +I (52315) wifi_prov_mgr: Provisioning stopped +. +. +. +I (52355) app: Hello World! +I (53355) app: Hello World! +I (54355) app: Hello World! +I (55355) app: Hello World! +``` + +## Troubleshooting + +### Provisioning failed + +It is possible that the Wi-Fi credentials provided were incorrect, or the device was not able to establish connection to the network, in which the the `esp_prov` script will notify failure (with reason). Serial monitor log will display the failure along with disconnect reason : + +``` +E (367015) app: Provisioning failed! + Reason : Wi-Fi AP password incorrect + Please reset to factory and retry provisioning +``` + +Once credentials have been applied, even though wrong credentials were provided, the device will no longer go into provisioning mode on subsequent reboots until NVS is erased (see following section). + +### Provisioning does not start + +If the serial monitor log shows the following : + +``` +I (465) app: Already provisioned, starting Wi-Fi STA +``` + +it means either the device has been provisioned earlier with or without success (e.g. scenario covered in above section), or that the Wi-Fi credentials were already set by some other application flashed previously onto your device. On setting the log level to DEBUG this is clearly evident : + +``` +D (455) wifi_prov_mgr: Found Wi-Fi SSID : myssid +D (465) wifi_prov_mgr: Found Wi-Fi Password : m********d +I (465) app: Already provisioned, starting Wi-Fi STA +``` + +To fix this we simple need to erase the NVS partition from flash. First we need to find out its address and size. This can be seen from the monitor log on the top right after reboot. + +``` +I (47) boot: Partition Table: +I (50) boot: ## Label Usage Type ST Offset Length +I (58) boot: 0 nvs WiFi data 01 02 00009000 00006000 +I (65) boot: 1 phy_init RF data 01 01 0000f000 00001000 +I (73) boot: 2 factory factory app 00 00 00010000 00124f80 +I (80) boot: End of partition table +``` + +Now erase NVS partition by running the following commands : + +``` +$IDF_PATH/components/esptool_py/esptool/esptool.py erase_region 0x9000 0x6000 +``` + +### Unsupported platform + +If the platform requirement, for running `esp_prov` is not satisfied, then the script execution will fallback to console mode, in which case the full process (involving user inputs) will look like this : + +``` +==== Esp_Prov Version: v1.0 ==== +BLE client is running in console mode + This could be due to your platform not being supported or dependencies not being met + Please ensure all pre-requisites are met to run the full fledged client +BLECLI >> Please connect to BLE device `PROV_261FCC` manually using your tool of choice +BLECLI >> Was the device connected successfully? [y/n] y +BLECLI >> List available attributes of the connected device +BLECLI >> Is the service UUID '0000ffff-0000-1000-8000-00805f9b34fb' listed among available attributes? [y/n] y +BLECLI >> Is the characteristic UUID '0000ff53-0000-1000-8000-00805f9b34fb' listed among available attributes? [y/n] y +BLECLI >> Is the characteristic UUID '0000ff51-0000-1000-8000-00805f9b34fb' listed among available attributes? [y/n] y +BLECLI >> Is the characteristic UUID '0000ff52-0000-1000-8000-00805f9b34fb' listed among available attributes? [y/n] y + +==== Verifying protocol version ==== +BLECLI >> Write following data to characteristic with UUID '0000ff53-0000-1000-8000-00805f9b34fb' : + >> 56302e31 +BLECLI >> Enter data read from characteristic (in hex) : + << 53554343455353 +==== Verified protocol version successfully ==== + +==== Starting Session ==== +BLECLI >> Write following data to characteristic with UUID '0000ff51-0000-1000-8000-00805f9b34fb' : + >> 10015a25a201220a20ae6d9d5d1029f8c366892252d2d5a0ffa7ce1ee5829312545dd5f2aba057294d +BLECLI >> Enter data read from characteristic (in hex) : + << 10015a390801aa0134122048008bfc365fad4753dc75912e0c764d60749cb26dd609595b6fbc72e12614031a1089733af233c7448e7d7fb7963682c6d8 +BLECLI >> Write following data to characteristic with UUID '0000ff51-0000-1000-8000-00805f9b34fb' : + >> 10015a270802b2012212204051088dc294fe4621fac934a8ea22e948fcc3e8ac458aac088ce705c65dbfb9 +BLECLI >> Enter data read from characteristic (in hex) : + << 10015a270803ba01221a20c8d38059d5206a3d92642973ac6ba8ac2f6ecf2b7a3632964eb35a0f20133adb +==== Session Established ==== + +==== Sending Wifi credential to esp32 ==== +BLECLI >> Write following data to characteristic with UUID '0000ff52-0000-1000-8000-00805f9b34fb' : + >> 98471ac4019a46765c28d87df8c8ae71c1ae6cfe0bc9c615bc6d2c +BLECLI >> Enter data read from characteristic (in hex) : + << 3271f39a +==== Wifi Credentials sent successfully ==== + +==== Applying config to esp32 ==== +BLECLI >> Write following data to characteristic with UUID '0000ff52-0000-1000-8000-00805f9b34fb' : + >> 5355 +BLECLI >> Enter data read from characteristic (in hex) : + << 1664db24 +==== Apply config sent successfully ==== + +==== Wifi connection state ==== +BLECLI >> Write following data to characteristic with UUID '0000ff52-0000-1000-8000-00805f9b34fb' : + >> 290d +BLECLI >> Enter data read from characteristic (in hex) : + << 505f72a9f8521025c1964d7789c4d7edc56aedebd144e1b667bc7c0975757b80cc091aa9f3e95b06eaefbc30290fa1 +++++ WiFi state: connected ++++ +==== Provisioning was successful ==== +``` + +The write data is to be copied from the console output ```>>``` to the platform specific application and the data read from the application is to be pasted at the user input prompt ```<<``` of the console, in the format (hex) indicated in above sample log. diff --git a/examples/provisioning/manager/main/CMakeLists.txt b/examples/provisioning/manager/main/CMakeLists.txt new file mode 100644 index 0000000000..6b03500639 --- /dev/null +++ b/examples/provisioning/manager/main/CMakeLists.txt @@ -0,0 +1,4 @@ +set(COMPONENT_SRCS "app_main.c") +set(COMPONENT_ADD_INCLUDEDIRS ".") + +register_component() diff --git a/examples/provisioning/manager/main/app_main.c b/examples/provisioning/manager/main/app_main.c new file mode 100644 index 0000000000..d6fd9a47e0 --- /dev/null +++ b/examples/provisioning/manager/main/app_main.c @@ -0,0 +1,244 @@ +/* Wi-Fi Provisioning Manager Example + + This example code is in the Public Domain (or CC0 licensed, at your option.) + + Unless required by applicable law or agreed to in writing, this + software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + CONDITIONS OF ANY KIND, either express or implied. +*/ + +#include +#include + +#include +#include +#include + +#include +#include +#include +#include + +#include +#include +#include + +static const char *TAG = "app"; + +/* Signal Wi-Fi events on this event-group */ +const int WIFI_CONNECTED_EVENT = BIT0; +static EventGroupHandle_t wifi_event_group; + +/* Event handler for catching system events */ +static esp_err_t event_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); + + /* Global event handling */ + switch (event->event_id) { + case SYSTEM_EVENT_STA_START: + esp_wifi_connect(); + break; + case SYSTEM_EVENT_STA_GOT_IP: + ESP_LOGI(TAG, "Connected with IP Address:%s", + ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); + xEventGroupSetBits(wifi_event_group, WIFI_CONNECTED_EVENT); + break; + case SYSTEM_EVENT_STA_DISCONNECTED: + ESP_LOGI(TAG, "Disconnected. Connecting to the AP again..."); + esp_wifi_connect(); + break; + default: + break; + } + return ESP_OK; +} + +/* Event handler for catching provisioning manager events */ +static void prov_event_handler(void *user_data, + wifi_prov_cb_event_t event, void *event_data) +{ + switch (event) { + 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; + /* If SSID length is exactly 32 bytes, null termination + * will not be present, so explicitly obtain the length */ + size_t ssid_len = strnlen((const char *)wifi_sta_cfg->ssid, sizeof(wifi_sta_cfg->ssid)); + ESP_LOGI(TAG, "Received Wi-Fi credentials" + "\n\tSSID : %.*s\n\tPassword : %s", + ssid_len, (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 AP password incorrect" : "Wi-Fi AP 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; + } +} + +static void wifi_init_sta() +{ + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); + ESP_ERROR_CHECK(esp_wifi_start()); +} + +static void get_device_service_name(char *service_name, size_t max) +{ + uint8_t eth_mac[6]; + const char *ssid_prefix = "PROV_"; + esp_wifi_get_mac(WIFI_IF_STA, eth_mac); + snprintf(service_name, max, "%s%02X%02X%02X", + ssid_prefix, eth_mac[3], eth_mac[4], eth_mac[5]); +} + +void app_main() +{ + /* Initialize NVS partition */ + esp_err_t ret = nvs_flash_init(); + if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { + /* NVS partition was truncated + * and needs to be erased */ + ESP_ERROR_CHECK(nvs_flash_erase()); + + /* Retry nvs_flash_init */ + ESP_ERROR_CHECK(nvs_flash_init()); + } + + /* Initialize TCP/IP and the event loop */ + tcpip_adapter_init(); + ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL)); + wifi_event_group = xEventGroupCreate(); + + /* Initialize Wi-Fi */ + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + + /* Configuration for the provisioning manager */ + wifi_prov_mgr_config_t config = { + /* What is the Provisioning Scheme that we want ? + * wifi_prov_scheme_softap or wifi_prov_scheme_ble */ + .scheme = wifi_prov_scheme_ble, + + /* Any default scheme specific event handler that you would + * like to choose. Since our example application requires + * neither BT nor BLE, we can choose to release the associated + * memory once provisioning is complete, or not needed + * (in case when device is already provisioned). Choosing + * appropriate scheme specific event handler allows the manager + * to take care of this automatically. This can be set to + * WIFI_PROV_EVENT_HANDLER_NONE when using wifi_prov_scheme_softap*/ + .scheme_event_handler = WIFI_PROV_SCHEME_BLE_EVENT_HANDLER_FREE_BTDM, + + /* Do we want an application specific handler be executed on + * various provisioning related events */ + .app_event_handler = { + .event_cb = prov_event_handler, + .user_data = NULL + } + }; + + /* Initialize provisioning manager with the + * configuration parameters set above */ + ESP_ERROR_CHECK(wifi_prov_mgr_init(config)); + + bool provisioned = false; + /* Let's find out if the device is provisioned */ + ESP_ERROR_CHECK(wifi_prov_mgr_is_provisioned(&provisioned)); + + /* If device is not yet provisioned start provisioning service */ + if (!provisioned) { + ESP_LOGI(TAG, "Starting provisioning"); + + /* What is the Device Service Name that we want + * This translates to : + * - Wi-Fi SSID when scheme is wifi_prov_scheme_softap + * - device name when scheme is wifi_prov_scheme_ble + */ + char service_name[12]; + get_device_service_name(service_name, sizeof(service_name)); + + /* What is the security level that we want (0 or 1): + * - WIFI_PROV_SECURITY_0 is simply plain text communication. + * - WIFI_PROV_SECURITY_1 is secure communication which consists of secure handshake + * using X25519 key exchange and proof of possession (pop) and AES-CTR + * for encryption/decryption of messages. + */ + wifi_prov_security_t security = WIFI_PROV_SECURITY_1; + + /* Do we want a proof-of-possession (ignored if Security 0 is selected): + * - this should be a string with length > 0 + * - NULL if not used + */ + const char *pop = "abcd1234"; + + /* What is the service key (could be NULL) + * This translates to : + * - Wi-Fi password when scheme is wifi_prov_scheme_softap + * - simply ignored when scheme is wifi_prov_scheme_ble + */ + const char *service_key = NULL; + + /* This step is only useful when scheme is wifi_prov_scheme_ble. This will + * set a custom 128 bit UUID which will be included in the BLE advertisement + * and will correspond to the primary GATT service that provides provisioning + * endpoints as GATT characteristics. Each GATT characteristic will be + * formed using the primary service UUID as base, with different auto assigned + * 12th and 13th bytes (assume counting starts from 0th byte). The client side + * applications must identify the endpoints by reading the User Characteristic + * Description descriptor (0x2901) for each characteristic, which contains the + * endpoint name of the characteristic */ + uint8_t custom_service_uuid[] = { + /* LSB <--------------------------------------- + * ---------------------------------------> MSB */ + 0x21, 0x43, 0x65, 0x87, 0x09, 0xba, 0xdc, 0xfe, + 0xef, 0xcd, 0xab, 0x90, 0x78, 0x56, 0x34, 0x12 + }; + wifi_prov_scheme_ble_set_service_uuid(custom_service_uuid); + + /* Start provisioning service */ + ESP_ERROR_CHECK(wifi_prov_mgr_start_provisioning(security, pop, service_name, service_key)); + + /* Uncomment the following to wait for the provisioning to finish and then release + * the resources of the manager. Since in this case de-initialization is triggered + * by the configured prov_event_handler(), we don't need to call the following */ + // wifi_prov_mgr_wait(); + // wifi_prov_mgr_deinit(); + } else { + ESP_LOGI(TAG, "Already provisioned, starting Wi-Fi STA"); + + /* We don't need the manager as device is already provisioned, + * so let's release it's resources */ + wifi_prov_mgr_deinit(); + + /* Start Wi-Fi station */ + wifi_init_sta(); + } + + /* Wait for Wi-Fi connection */ + xEventGroupWaitBits(wifi_event_group, WIFI_CONNECTED_EVENT, false, true, portMAX_DELAY); + + /* Start main application now */ + while (1) { + ESP_LOGI(TAG, "Hello World!"); + vTaskDelay(1000 / portTICK_PERIOD_MS); + } +} diff --git a/examples/provisioning/manager/main/component.mk b/examples/provisioning/manager/main/component.mk new file mode 100644 index 0000000000..61f8990c31 --- /dev/null +++ b/examples/provisioning/manager/main/component.mk @@ -0,0 +1,8 @@ +# +# Main component makefile. +# +# This Makefile can be left empty. By default, it will take the sources in the +# src/ directory, compile them and link them into lib(subdirectory_name).a +# in the build directory. This behaviour is entirely configurable, +# please read the ESP-IDF documents if you need to do this. +# diff --git a/examples/provisioning/manager/partitions.csv b/examples/provisioning/manager/partitions.csv new file mode 100644 index 0000000000..e382463f94 --- /dev/null +++ b/examples/provisioning/manager/partitions.csv @@ -0,0 +1,5 @@ +# Name, Type, SubType, Offset, Size, Flags +# Note: if you change the phy_init or app partition offset, make sure to change the offset in Kconfig.projbuild +nvs, data, nvs, 0x9000, 0x6000, +phy_init, data, phy, 0xf000, 0x1000, +factory, app, factory, 0x10000, 1200000, diff --git a/examples/provisioning/manager/sdkconfig.defaults b/examples/provisioning/manager/sdkconfig.defaults new file mode 100644 index 0000000000..e28cc05069 --- /dev/null +++ b/examples/provisioning/manager/sdkconfig.defaults @@ -0,0 +1,10 @@ +# Override some defaults so BT stack is enabled and +CONFIG_BT_ENABLED=y +CONFIG_BTDM_CTRL_MODE_BLE_ONLY=y +CONFIG_BTDM_CTRL_MODE_BR_EDR_ONLY= +CONFIG_BTDM_CTRL_MODE_BTDM= + +# Binary is larger than default size +CONFIG_PARTITION_TABLE_CUSTOM=y +CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv" +CONFIG_PARTITION_TABLE_FILENAME="partitions.csv" diff --git a/examples/provisioning/manager/wifi_prov_mgr_test.py b/examples/provisioning/manager/wifi_prov_mgr_test.py new file mode 100644 index 0000000000..ef0294250b --- /dev/null +++ b/examples/provisioning/manager/wifi_prov_mgr_test.py @@ -0,0 +1,119 @@ +#!/usr/bin/env python +# +# Copyright 2018 Espressif Systems (Shanghai) PTE LTD +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from __future__ import print_function +import re +import os +import sys +import time + +try: + import IDF +except ImportError: + test_fw_path = os.getenv("TEST_FW_PATH") + if test_fw_path and test_fw_path not in sys.path: + sys.path.insert(0, test_fw_path) + import IDF + +try: + import esp_prov +except ImportError: + esp_prov_path = os.getenv("IDF_PATH") + "/tools/esp_prov" + if esp_prov_path and esp_prov_path not in sys.path: + sys.path.insert(0, esp_prov_path) + import esp_prov + +# Have esp_prov throw exception +esp_prov.config_throw_except = True + + +@IDF.idf_example_test(env_tag="Example_WIFI_BT") +def test_examples_wifi_prov_mgr(env, extra_data): + # Acquire DUT + dut1 = env.get_dut("wifi_prov_mgr", "examples/provisioning/manager") + + # Get binary file + binary_file = os.path.join(dut1.app.binary_path, "wifi_prov_mgr.bin") + bin_size = os.path.getsize(binary_file) + IDF.log_performance("wifi_prov_mgr_bin_size", "{}KB".format(bin_size // 1024)) + IDF.check_performance("wifi_prov_mgr_bin_size", bin_size // 1024) + + # Upload binary and start testing + dut1.start_app() + + # Check if BT memory is released before provisioning starts + dut1.expect("wifi_prov_scheme_ble: BT memory released", timeout=60) + + # Parse BLE devname + devname = dut1.expect(re.compile(r"Provisioning started with service name : (PROV_\S\S\S\S\S\S)"), timeout=30)[0] + print("BLE Device Alias for DUT :", devname) + + print("Starting Provisioning") + verbose = False + protover = "v1.0" + secver = 1 + pop = "abcd1234" + provmode = "ble" + ap_ssid = "myssid" + ap_password = "mypassword" + + print("Getting security") + security = esp_prov.get_security(secver, pop, verbose) + if security is None: + raise RuntimeError("Failed to get security") + + print("Getting transport") + transport = esp_prov.get_transport(provmode, None, devname) + if transport is None: + raise RuntimeError("Failed to get transport") + + print("Verifying protocol version") + if not esp_prov.version_match(transport, protover): + raise RuntimeError("Mismatch in protocol version") + + print("Starting Session") + if not esp_prov.establish_session(transport, security): + raise RuntimeError("Failed to start session") + + print("Sending Wifi credential to DUT") + if not esp_prov.send_wifi_config(transport, security, ap_ssid, ap_password): + raise RuntimeError("Failed to send Wi-Fi config") + + print("Applying config") + if not esp_prov.apply_wifi_config(transport, security): + raise RuntimeError("Failed to send apply config") + + success = False + while True: + time.sleep(5) + print("Wi-Fi connection state") + ret = esp_prov.get_wifi_config(transport, security) + if (ret == 1): + continue + elif (ret == 0): + print("Provisioning was successful") + success = True + break + + if not success: + raise RuntimeError("Provisioning failed") + + # Check if BTDM memory is released after provisioning finishes + dut1.expect("wifi_prov_scheme_ble: BTDM memory released", timeout=30) + + +if __name__ == '__main__': + test_examples_wifi_prov_mgr() From a92ace034f488a6cff9bfb608f1c6f333ba69c38 Mon Sep 17 00:00:00 2001 From: Anurag Kar Date: Tue, 23 Apr 2019 23:17:28 +0530 Subject: [PATCH 4/4] esp_prov : Support new JSON format of version string while maintaining backward compatibility Other changes: * Version check only happens if command line argument is specified * Minor bugfix in processing apply_config response --- tools/esp_prov/esp_prov.py | 42 ++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/tools/esp_prov/esp_prov.py b/tools/esp_prov/esp_prov.py index 88b58c1bb9..0839f9d1df 100644 --- a/tools/esp_prov/esp_prov.py +++ b/tools/esp_prov/esp_prov.py @@ -20,6 +20,7 @@ import argparse import time import os import sys +import json try: import security @@ -81,13 +82,26 @@ def get_transport(sel_transport, softap_endpoint=None, ble_devname=None): return None -def version_match(tp, protover): +def version_match(tp, protover, verbose=False): try: response = tp.send_data('proto-ver', protover) - if response != protover: - return False - return True - except RuntimeError as e: + + if verbose: + print("proto-ver response : ", response) + + # First assume this to be a simple version string + if response.lower() == protover.lower(): + return True + + # Else interpret this as JSON structure containing + # information with versions and capabilities of both + # provisioning service and application + info = json.loads(response) + if info['prov']['ver'].lower() == protover.lower(): + return True + + return False + except Exception as e: on_except(e) return None @@ -132,7 +146,7 @@ def apply_wifi_config(tp, sec): try: message = prov.config_apply_config_request(sec) response = tp.send_data('prov-config', message) - return (prov.config_set_config_response(sec, response) == 0) + return (prov.config_apply_config_response(sec, response) == 0) except RuntimeError as e: on_except(e) return None @@ -159,7 +173,7 @@ if __name__ == '__main__': parser.add_argument("--sec_ver", dest='secver', type=int, help="Security scheme version", default=1) parser.add_argument("--proto_ver", dest='protover', type=str, - help="Protocol version", default='V0.1') + help="Protocol version", default='') parser.add_argument("--pop", dest='pop', type=str, help="Proof of possession", default='') @@ -182,7 +196,8 @@ if __name__ == '__main__': parser.add_argument("-v","--verbose", help="increase output verbosity", action="store_true") args = parser.parse_args() - print("==== Esp_Prov Version: " + args.protover + " ====") + if args.protover != '': + print("==== Esp_Prov Version: " + args.protover + " ====") obj_security = get_security(args.secver, args.pop, args.verbose) if obj_security is None: @@ -194,11 +209,12 @@ if __name__ == '__main__': print("---- Invalid provisioning mode ----") exit(2) - print("\n==== Verifying protocol version ====") - if not version_match(obj_transport, args.protover): - print("---- Error in protocol version matching ----") - exit(3) - print("==== Verified protocol version successfully ====") + if args.protover != '': + print("\n==== Verifying protocol version ====") + if not version_match(obj_transport, args.protover, args.verbose): + print("---- Error in protocol version matching ----") + exit(3) + print("==== Verified protocol version successfully ====") print("\n==== Starting Session ====") if not establish_session(obj_transport, obj_security):