Select one of the symbols to view example projects that use it.
 
Outline
#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"
#define EXAMPLE_DPP_LISTEN_CHANNEL_LIST
#define EXAMPLE_DPP_LISTEN_CHANNEL_LIST
#define EXAMPLE_DPP_BOOTSTRAPPING_KEY
#define EXAMPLE_DPP_BOOTSTRAPPING_KEY
#define EXAMPLE_DPP_DEVICE_INFO
#define EXAMPLE_DPP_DEVICE_INFO
#define CURVE_SEC256R1_PKEY_HEX_DIGITS
TAG
s_dpp_wifi_config
s_retry_num
s_dpp_event_group
#define DPP_CONNECTED_BIT
#define DPP_CONNECT_FAIL_BIT
#define DPP_AUTH_FAIL_BIT
#define WIFI_MAX_RETRY_NUM
event_handler(void *, esp_event_base_t, int32_t, void *)
dpp_enrollee_event_cb(esp_supp_dpp_event_t, void *)
dpp_enrollee_bootstrap()
dpp_enrollee_init()
app_main()
Files
loading (2/4)...
SourceVuESP-IDF Framework and Examplesdpp-enrollee samplemain/dpp_enrollee_main.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* 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"11 includes #ifdef CONFIG_ESP_DPP_LISTEN_CHANNEL_LIST #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 #define CURVE_SEC256R1_PKEY_HEX_DIGITS 64 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 #define WIFI_MAX_RETRY_NUM 3 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 < WIFI_MAX_RETRY_NUM) { 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 == WIFI_EVENT && event_id == WIFI_EVENT_STA_CONNECTED) { ESP_LOGI(TAG, "Successfully connected to the AP ssid : %s ", s_dpp_wifi_config.sta.ssid); }{...} 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:"); 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)); s_retry_num = 0; esp_wifi_set_config(ESP_IF_WIFI_STA, &s_dpp_wifi_config); 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;... }{...} }{ ... } esp_err_t dpp_enrollee_bootstrap(void) { esp_err_t ret; size_t pkey_len = strlen(EXAMPLE_DPP_BOOTSTRAPPING_KEY); char *key = NULL; if (pkey_len) { /* Currently only NIST P-256 curve is supported, add prefix/postfix accordingly */ char prefix[] = "30310201010420"; char postfix[] = "a00a06082a8648ce3d030107"; if (pkey_len != CURVE_SEC256R1_PKEY_HEX_DIGITS) { ESP_LOGI(TAG, "Invalid key length! Private key needs to be 32 bytes (or 64 hex digits) long"); return ESP_FAIL; }{...} key = malloc(sizeof(prefix) + pkey_len + sizeof(postfix)); if (!key) { ESP_LOGI(TAG, "Failed to allocate for bootstrapping key"); return ESP_ERR_NO_MEM; }{...} sprintf(key, "%s%s%s", prefix, EXAMPLE_DPP_BOOTSTRAPPING_KEY, postfix); }{...} /* Currently only supported method is QR Code */ ret = esp_supp_dpp_bootstrap_gen(EXAMPLE_DPP_LISTEN_CHANNEL_LIST, DPP_BOOTSTRAP_QR_CODE, key, EXAMPLE_DPP_DEVICE_INFO); if (key) free(key); return ret; }{ ... } 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_wifi_set_mode(WIFI_MODE_STA)); ESP_ERROR_CHECK(esp_supp_dpp_init(dpp_enrollee_event_cb)); ESP_ERROR_CHECK(dpp_enrollee_bootstrap()); 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(); }{ ... }
Details
Show:
from
Types: Columns:
This file uses the notable symbols shown below. Click anywhere in the file to view more details.