Add DPP Enrollee example

1. Add Example for DPP Enrollee
2. Use DPP Supplicant API's to setup connection
3. Add support for multiple channels in Bootstrapping
4. Add Unity testcase for testing Offchannel operations

Closes https://github.com/espressif/esp-idf/issues/5654
This commit is contained in:
Nachiket Kukade
2020-10-27 18:23:19 +05:30
parent 87205dc2f4
commit 76b2cb28d2
23 changed files with 882 additions and 194 deletions

View File

@@ -1,5 +1,3 @@
idf_component_register(SRCS "esp_qrcode_main.c" "esp_qrcode_wrapper.c" "qrcodegen.c"
INCLUDE_DIRS "include"
REQUIRES
PRIV_REQUIRES )
INCLUDE_DIRS "include"
)

View File

@@ -0,0 +1,9 @@
# QR Code generator component
This directory contains a QR code generator component written in C. This component is based on [QR-Code-generator](https://github.com/nayuki/QR-Code-generator).
This component is used as part of the following ESP-IDF examples:
- [DPP Enrollee Example](../../wifi/wifi_easy_connect/dpp-enrollee/).
To learn more about how to use this component, please check API Documentation from header file [qrcode.h](./include/qrcode.h).
Please note that this component is not considered to be a part of ESP-IDF stable API. It may change and may be removed in the future releases.

View File

@@ -1,4 +1,4 @@
// Copyright 2019 Espressif Systems (Shanghai) PTE LTD
// Copyright 2020 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.
@@ -14,10 +14,13 @@
#include <stdio.h>
#include <esp_err.h>
#include "esp_log.h"
#include "qrcodegen.h"
#include "qrcode.h"
static const char *TAG = "QRCODE";
static const char *lt[] = {
/* 0 */ " ",
/* 1 */ "\u2580 ",
@@ -67,13 +70,14 @@ void esp_qrcode_print_console(esp_qrcode_handle_t qrcode)
esp_err_t esp_qrcode_generate(esp_qrcode_config_t *cfg, const char *text)
{
enum qrcodegen_Ecc ecc_lvl;
enum qrcodegen_Ecc ecc_lvl;
uint8_t *qrcode, *tempbuf;
esp_err_t err = ESP_FAIL;
qrcode = calloc(1, qrcodegen_BUFFER_LEN_FOR_VERSION(cfg->max_qrcode_version));
if (!qrcode)
if (!qrcode) {
return ESP_ERR_NO_MEM;
}
tempbuf = calloc(1, qrcodegen_BUFFER_LEN_FOR_VERSION(cfg->max_qrcode_version));
if (!tempbuf) {
@@ -99,12 +103,15 @@ esp_err_t esp_qrcode_generate(esp_qrcode_config_t *cfg, const char *text)
break;
}
// Make and print the QR Code symbol
bool ok = qrcodegen_encodeText(text, tempbuf, qrcode, ecc_lvl,
qrcodegen_VERSION_MIN, cfg->max_qrcode_version,
ESP_LOGI(TAG, "Encoding below text with ECC LVL %d & QR Code Version %d",
ecc_lvl, cfg->max_qrcode_version);
ESP_LOGI(TAG, "%s", text);
// Make and print the QR Code symbol
bool ok = qrcodegen_encodeText(text, tempbuf, qrcode, ecc_lvl,
qrcodegen_VERSION_MIN, cfg->max_qrcode_version,
qrcodegen_Mask_AUTO, true);
if (ok && cfg->display_func) {
cfg->display_func((esp_qrcode_handle_t)qrcode);
if (ok && cfg->display_func) {
cfg->display_func((esp_qrcode_handle_t)qrcode);
err = ESP_OK;
}

View File

@@ -1,4 +1,4 @@
// Copyright 2019 Espressif Systems (Shanghai) PTE LTD
// Copyright 2020 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.

View File

@@ -1,4 +1,4 @@
// Copyright 2019 Espressif Systems (Shanghai) PTE LTD
// Copyright 2020 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.
@@ -95,9 +95,9 @@ bool esp_qrcode_get_module(esp_qrcode_handle_t qrcode, int x, int y);
#define ESP_QRCODE_CONFIG_DEFAULT() (esp_qrcode_config_t) { \
.display_func = esp_qrcode_print_console, \
.qrcode_ecc_level = ESP_QRCODE_ECC_LOW, \
.max_qrcode_version = 10, \
} \
.qrcode_ecc_level = ESP_QRCODE_ECC_LOW, \
}
#ifdef __cplusplus
}

View File

@@ -0,0 +1,8 @@
# 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)
set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/qrcode)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(dpp-enrollee)

View File

@@ -0,0 +1,10 @@
#
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
# project subdirectory.
#
PROJECT_NAME := dpp-enrollee
EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/qrcode
include $(IDF_PATH)/make/project.mk

View File

@@ -0,0 +1,66 @@
# Device Provisioning Protocol (Enrollee) Example
This example shows how to configure ESP32 as an enrollee using Device Provisioning Protocol(DPP) also known as Wi-Fi Easy Connect.
DPP provides a simple and secure way to onboard ESP32 to a network.
We now support Responder-Enrollee mode of DPP with PSK mode of authentication.
You need a Wi-Fi Easy Connect with Initiator mode capable device to make use of this example. Some Android 10+ devices have this capability. (Vendor specific)
To run the example with an Android 10+ device follow below steps -
1. Compile and flash the example on ESP32, a QR code will appear on your console.
2. Connect your phone to the network, say named "Example-AP".
3. Now go to Settings->WiFi & Internet->Wi-Fi->Example-AP->Advanced->Add Device.
4. Scan QR Code using the scanner, which will make ESP32 connect to Example-AP.
Optional configuration available
*Note:*
- QR Code should be displayed as dark on a white/light background to work properly.
- If displayed QR Code had line gaps, try switching to a new font or a diiferent Terminal program. See below QR Code for for checking beforehand.
### Example output
Here is an example of the console output.
```
I (807) wifi:mode : sta (24:0a:c4:23:da:20)
I (807) wifi dpp-enrollee: Started listening on Channel 11 for DPP Authentication
I (1157) wifi dpp-enrollee: Scan below QR Code to configure the enrollee:
█▀▀▀▀▀█ ██▄▄▄█▄▀██▄▄█▄ ▀ ▀▄ █▄▄ █▀▀▀▀▀█
█ ███ █ ██▀█▀ ▀▀██▀█▄█▀▄▀ ██▀▀█ ▄ █ ███ █
█ ▀▀▀ █ ▄█▀▄▄ ▄▄▀ █▄▀ ▄ ▄ ▄▀▄ ██ █ ▀▀▀ █
▀▀▀▀▀▀▀ ▀ █▄▀ ▀ ▀▄▀▄▀▄▀ █ ▀ ▀▄█ ▀ ▀▀▀▀▀▀▀
█▀ ▄██▀ ▄█ ▀█ ▄▀▄▄▄ ▀▀█▄ ▄▀█▄█▀▀▄▄▄▀▄██▀█
█▄▀ ▄ ▀▄█▄ ▀▀█▀▀█ ▀▄ ▄█▀▀▀▀█▀▄▄▄ ██▄ ▄█
▀█▀█▀ ▀▀ ▀ ▄▀▄▀▀ ▄ ▄▀▀▀ █▄ ▄▄ ▀█▄▀▄ █
▀ ▀ ▀▀▀█▄ █▀▀ █▄▄▄ █▄ █▄▀ ██▄ ▄▄▀█▄▀ ▄█
▀██▀▄█▀▄ ▄█ ▀▄▀ █ ▄ ▄█▄▀▄▀▄▄▀▄ ▄▄▄▀▄▄
▀▀▄█▀█▄▀▀█▄ ▄▀ █▄ ▀█▄█▄▀ ▀█▄▄ ▄▀▄ █▄▀ █
▄▀▀ ▀█▀▀▀ ▄ ▀█▀▀▄ ▀ ▄▄█▄ █ ██▀▄▀▀▄▄▄▄█▀▄
▀ ███▀▀▄ ▄ ▄ ▀█▄▄▀█▀▀▀ ▀▀▄▄ ▀ █▄ ▄█
█ ▀▄▄ ▀▀▀▀▄▀▀▀▄█▄▄ ▄▀▄▀ ▀▄▀▄▀█▀▀▄▀ ▄█▄▀
███ ▄▀▄▀▀▄▀▀█▀▀▄ ▀▄ ████ █▀▄█▄▄ ▀█▄ ▀▀ ▀
▄▀█▀▀▀▀█▀ ▄█▄▀▀ ▄ ▀█▀▀ ▀ ▄▀▀ ▀▄█ ▄ ▀
█ ▀▀▀▄██▄█▀ ▀█▄█▄ ▀██▀▄▀▄▀ █▀ ▀ ▄▄▀█ ▄█
▀▀▀ ▀▀▀▀▄▄█▄▀█▄ ▄ ▄ ▀▀▀█▄▄▀▀▀ █▀▀▀██▀▀▄
█▀▀▀▀▀█ ▄▄▀█▀ ▄█▄█▄▄█▄ ▀ ▀▀▀█▄ ▀█ ▀ █ ▀ █
█ ███ █ ▀█▀ ▀█▀▀▄▄▀ ▀▄█▀▀ ██▀█▀▀▀█▀▄▄▄█
█ ▀▀▀ █ ▄▀█ ▄ ▄ ▀█▄ ▀▄▀█ ▀▄██▄ ▀ ▄█ ▄▀▄█
▀▀▀▀▀▀▀ ▀ ▀ ▀ ▀ ▀▀▀ ▀▀▀▀▀ ▀ ▀ ▀
I (6357) wifi dpp-enrollee: DPP Authentication successful, connecting to AP : DigitalFortress
I (6477) wifi:new:<1,0>, old:<1,0>, ap:<255,255>, sta:<1,0>, prof:1
I (7277) wifi:state: init -> auth (b0)
I (7277) wifi:state: auth -> assoc (0)
I (7287) wifi:state: assoc -> run (10)
I (7317) wifi:connected with DigitalFortress, aid = 4, channel 1, BW20, bssid = 04:d4:c4:5e:22:f0
I (7317) wifi:security type: 3, phy: bgn, rssi: -60
I (7427) wifi:pm start, type: 1
I (7427) wifi:AP's beacon interval = 102400 us, DTIM period = 1
I (11617) esp_netif_handlers: sta ip: 192.168.1.216, mask: 255.255.255.0, gw: 192.168.1.1
I (11617) wifi dpp-enrollee: got ip:192.168.1.216
I (11617) wifi dpp-enrollee: connected to ap SSID:DigitalFortress password:password
```

View File

@@ -0,0 +1,2 @@
idf_component_register(SRCS "dpp_enrollee_main.c"
INCLUDE_DIRS ".")

View File

@@ -0,0 +1,17 @@
menu "Example Configuration"
config ESP_DPP_LISTEN_CHANNEL_LIST
string "DPP Listen channel list"
default "6"
help
DPP Bootstrapping listen channels separated by commas.
config ESP_DPP_BOOTSTRAPPING_KEY
string "Bootstrapping key"
help
Private key string for DPP Bootstrapping in PEM format.
config ESP_DPP_DEVICE_INFO
string "Additional Device Info"
help
Additional ancillary information to be included in QR Code.
endmenu

View File

@@ -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.
#

View File

@@ -0,0 +1,169 @@
/* DPP Enrollee 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 <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_dpp.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "qrcode.h"
#ifdef CONFIG_ESP_DPP_LISTEN_CHANNEL
#define EXAMPLE_DPP_LISTEN_CHANNEL_LIST CONFIG_ESP_DPP_LISTEN_CHANNEL_LIST
#else
#define EXAMPLE_DPP_LISTEN_CHANNEL_LIST "6"
#endif
#ifdef CONFIG_ESP_DPP_BOOTSTRAPPING_KEY
#define EXAMPLE_DPP_BOOTSTRAPPING_KEY CONFIG_ESP_DPP_BOOTSTRAPPING_KEY
#else
#define EXAMPLE_DPP_BOOTSTRAPPING_KEY 0
#endif
#ifdef CONFIG_ESP_DPP_DEVICE_INFO
#define EXAMPLE_DPP_DEVICE_INFO CONFIG_ESP_DPP_DEVICE_INFO
#else
#define EXAMPLE_DPP_DEVICE_INFO 0
#endif
static const char *TAG = "wifi dpp-enrollee";
wifi_config_t s_dpp_wifi_config;
static int s_retry_num = 0;
/* FreeRTOS event group to signal when we are connected*/
static EventGroupHandle_t s_dpp_event_group;
#define DPP_CONNECTED_BIT BIT0
#define DPP_CONNECT_FAIL_BIT BIT1
#define DPP_AUTH_FAIL_BIT BIT2
static void event_handler(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data)
{
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
ESP_ERROR_CHECK(esp_supp_dpp_start_listen());
ESP_LOGI(TAG, "Started listening for DPP Authentication");
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
if (s_retry_num < 5) {
esp_wifi_connect();
s_retry_num++;
ESP_LOGI(TAG, "retry to connect to the AP");
} else {
xEventGroupSetBits(s_dpp_event_group, DPP_CONNECT_FAIL_BIT);
}
ESP_LOGI(TAG, "connect to the AP fail");
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
ip_event_got_ip_t *event = (ip_event_got_ip_t *) event_data;
ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
s_retry_num = 0;
xEventGroupSetBits(s_dpp_event_group, DPP_CONNECTED_BIT);
}
}
void dpp_enrollee_event_cb(esp_supp_dpp_event_t event, void *data)
{
switch (event) {
case ESP_SUPP_DPP_URI_READY:
if (data != NULL) {
esp_qrcode_config_t cfg = ESP_QRCODE_CONFIG_DEFAULT();
ESP_LOGI(TAG, "Scan below QR Code to configure the enrollee:\n");
esp_qrcode_generate(&cfg, (const char *)data);
}
break;
case ESP_SUPP_DPP_CFG_RECVD:
memcpy(&s_dpp_wifi_config, data, sizeof(s_dpp_wifi_config));
esp_wifi_set_config(ESP_IF_WIFI_STA, &s_dpp_wifi_config);
ESP_LOGI(TAG, "DPP Authentication successful, connecting to AP : %s",
s_dpp_wifi_config.sta.ssid);
s_retry_num = 0;
esp_wifi_connect();
break;
case ESP_SUPP_DPP_FAIL:
if (s_retry_num < 5) {
ESP_LOGI(TAG, "DPP Auth failed (Reason: %s), retry...", esp_err_to_name((int)data));
ESP_ERROR_CHECK(esp_supp_dpp_start_listen());
s_retry_num++;
} else {
xEventGroupSetBits(s_dpp_event_group, DPP_AUTH_FAIL_BIT);
}
break;
default:
break;
}
}
void dpp_enrollee_init(void)
{
s_dpp_event_group = xEventGroupCreate();
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_netif_create_default_wifi_sta();
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL));
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_supp_dpp_init(dpp_enrollee_event_cb));
/* Currently only supported method is QR Code */
ESP_ERROR_CHECK(esp_supp_dpp_bootstrap_gen(EXAMPLE_DPP_LISTEN_CHANNEL_LIST, DPP_BOOTSTRAP_QR_CODE,
EXAMPLE_DPP_BOOTSTRAPPING_KEY, EXAMPLE_DPP_DEVICE_INFO));
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_start());
/* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum
* number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */
EventBits_t bits = xEventGroupWaitBits(s_dpp_event_group,
DPP_CONNECTED_BIT | DPP_CONNECT_FAIL_BIT | DPP_AUTH_FAIL_BIT,
pdFALSE,
pdFALSE,
portMAX_DELAY);
/* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
* happened. */
if (bits & DPP_CONNECTED_BIT) {
ESP_LOGI(TAG, "connected to ap SSID:%s password:%s",
s_dpp_wifi_config.sta.ssid, s_dpp_wifi_config.sta.password);
} else if (bits & DPP_CONNECT_FAIL_BIT) {
ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s",
s_dpp_wifi_config.sta.ssid, s_dpp_wifi_config.sta.password);
} else if (bits & DPP_AUTH_FAIL_BIT) {
ESP_LOGI(TAG, "DPP Authentication failed after %d retries", s_retry_num);
} else {
ESP_LOGE(TAG, "UNEXPECTED EVENT");
}
esp_supp_dpp_deinit();
ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler));
ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler));
vEventGroupDelete(s_dpp_event_group);
}
void app_main(void)
{
//Initialize NVS
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);
dpp_enrollee_init();
}