Select one of the symbols to view example projects that use it.
 
Outline
#include <stdio.h>
#include <string.h>
#include <esp_err.h>
#include <esp_log.h>
#include "wifi_constants.pb-c.h"
#include "wifi_config.pb-c.h"
#include <wifi_provisioning/wifi_config.h>
TAG
wifi_prov_config_cmd
cmd_table
cmd_get_status_handler(WiFiConfigPayload *, WiFiConfigPayload *, void *)
cmd_set_config_handler(WiFiConfigPayload *, WiFiConfigPayload *, void *)
cmd_apply_config_handler(WiFiConfigPayload *, WiFiConfigPayload *, void *)
lookup_cmd_handler(int)
wifi_prov_config_command_cleanup(WiFiConfigPayload *, void *)
wifi_prov_config_command_dispatcher(WiFiConfigPayload *, WiFiConfigPayload *, void *)
wifi_prov_config_data_handler(uint32_t, const uint8_t *, ssize_t, uint8_t **, ssize_t *, void *)
Files
loading...
SourceVuESP-IDF Framework and ExamplesESP-IDFcomponents/wifi_provisioning/src/wifi_config.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* * SPDX-FileCopyrightText: 2018-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 *//* ... */ #include <stdio.h> #include <string.h> #include <esp_err.h> #include <esp_log.h> #include "wifi_constants.pb-c.h" #include "wifi_config.pb-c.h" #include <wifi_provisioning/wifi_config.h>7 includes static const char* TAG = "WiFiProvConfig"; typedef struct wifi_prov_config_cmd { int cmd_num; esp_err_t (*command_handler)(WiFiConfigPayload *req, WiFiConfigPayload *resp, void *priv_data); }{ ... } wifi_prov_config_cmd_t; static esp_err_t cmd_get_status_handler(WiFiConfigPayload *req, WiFiConfigPayload *resp, void *priv_data); static esp_err_t cmd_set_config_handler(WiFiConfigPayload *req, WiFiConfigPayload *resp, void *priv_data); static esp_err_t cmd_apply_config_handler(WiFiConfigPayload *req, WiFiConfigPayload *resp, void *priv_data); static wifi_prov_config_cmd_t cmd_table[] = { { .cmd_num = WI_FI_CONFIG_MSG_TYPE__TypeCmdGetStatus, .command_handler = cmd_get_status_handler }{...}, { .cmd_num = WI_FI_CONFIG_MSG_TYPE__TypeCmdSetConfig, .command_handler = cmd_set_config_handler }{...}, { .cmd_num = WI_FI_CONFIG_MSG_TYPE__TypeCmdApplyConfig, .command_handler = cmd_apply_config_handler }{...} }{...}; static esp_err_t cmd_get_status_handler(WiFiConfigPayload *req, WiFiConfigPayload *resp, void *priv_data) { ESP_LOGD(TAG, "Enter cmd_get_status_handler"); wifi_prov_config_handlers_t *h = (wifi_prov_config_handlers_t *) priv_data; if (!h) { ESP_LOGE(TAG, "Command invoked without handlers"); return ESP_ERR_INVALID_STATE; }{...} RespGetStatus *resp_payload = (RespGetStatus *) malloc(sizeof(RespGetStatus)); if (!resp_payload) { ESP_LOGE(TAG, "Error allocating memory"); return ESP_ERR_NO_MEM; }{...} resp_get_status__init(resp_payload); wifi_prov_config_get_data_t resp_data; if (h->get_status_handler(&resp_data, &h->ctx) == ESP_OK) { if (resp_data.wifi_state == WIFI_PROV_STA_CONNECTING) { resp_payload->sta_state = WIFI_STATION_STATE__Connecting; resp_payload->state_case = RESP_GET_STATUS__STATE_CONNECTED; }{...} else if (resp_data.wifi_state == WIFI_PROV_STA_CONNECTED) { resp_payload->sta_state = WIFI_STATION_STATE__Connected; resp_payload->state_case = RESP_GET_STATUS__STATE_CONNECTED; WifiConnectedState *connected = (WifiConnectedState *)( malloc(sizeof(WifiConnectedState))); if (!connected) { free(resp_payload); ESP_LOGE(TAG, "Error allocating memory"); return ESP_ERR_NO_MEM; }{...} resp_payload->connected = connected; wifi_connected_state__init(connected); connected->ip4_addr = strdup(resp_data.conn_info.ip_addr); if (connected->ip4_addr == NULL) { free(resp_payload); return ESP_ERR_NO_MEM; }{...} connected->bssid.len = sizeof(resp_data.conn_info.bssid); connected->bssid.data = (uint8_t *) strndup(resp_data.conn_info.bssid, sizeof(resp_data.conn_info.bssid)); if (connected->bssid.data == NULL) { free(connected->ip4_addr); free(resp_payload); return ESP_ERR_NO_MEM; }{...} connected->ssid.len = strlen(resp_data.conn_info.ssid); connected->ssid.data = (uint8_t *) strdup(resp_data.conn_info.ssid); if (connected->ssid.data == NULL) { free(connected->bssid.data); free(connected->ip4_addr); free(resp_payload); return ESP_ERR_NO_MEM; }{...} connected->channel = resp_data.conn_info.channel; connected->auth_mode = resp_data.conn_info.auth_mode; }{...} else if (resp_data.wifi_state == WIFI_PROV_STA_DISCONNECTED) { resp_payload->sta_state = WIFI_STATION_STATE__ConnectionFailed; resp_payload->state_case = RESP_GET_STATUS__STATE_FAIL_REASON; if (resp_data.fail_reason == WIFI_PROV_STA_AUTH_ERROR) { resp_payload->fail_reason = WIFI_CONNECT_FAILED_REASON__AuthError; }{...} else if (resp_data.fail_reason == WIFI_PROV_STA_AP_NOT_FOUND) { resp_payload->fail_reason = WIFI_CONNECT_FAILED_REASON__NetworkNotFound; }{...} }{...} else if (resp_data.wifi_state == WIFI_PROV_STA_CONN_ATTEMPT_FAILED) { resp_payload->sta_state = WIFI_STATION_STATE__Connecting; resp_payload->state_case = RESP_GET_STATUS__STATE_ATTEMPT_FAILED; WifiAttemptFailed *attempt_failed = (WifiAttemptFailed *)( calloc(1, sizeof(WifiAttemptFailed))); if (!attempt_failed) { free(resp_payload); ESP_LOGE(TAG, "Error allocating memory"); return ESP_ERR_NO_MEM; }{...} wifi_attempt_failed__init(attempt_failed); attempt_failed->attempts_remaining = resp_data.connecting_info.attempts_remaining; resp_payload->attempt_failed = attempt_failed; }{...} resp_payload->status = STATUS__Success; }{...} resp->payload_case = WI_FI_CONFIG_PAYLOAD__PAYLOAD_RESP_GET_STATUS; resp->resp_get_status = resp_payload; return ESP_OK; }{ ... } static esp_err_t cmd_set_config_handler(WiFiConfigPayload *req, WiFiConfigPayload *resp, void *priv_data) { ESP_LOGD(TAG, "Enter cmd_set_config_handler"); wifi_prov_config_handlers_t *h = (wifi_prov_config_handlers_t *) priv_data; if (!h) { ESP_LOGE(TAG, "Command invoked without handlers"); return ESP_ERR_INVALID_STATE; }{...} RespSetConfig *resp_payload = (RespSetConfig *) malloc(sizeof(RespSetConfig)); if (resp_payload == NULL) { ESP_LOGE(TAG, "Error allocating memory"); return ESP_ERR_NO_MEM; }{...} resp_set_config__init(resp_payload); wifi_prov_config_set_data_t req_data; memset(&req_data, 0, sizeof(req_data)); /* Check arguments provided in protobuf packet: * - SSID / Passphrase string length must be within the standard limits * - BSSID must either be NULL or have length equal to that imposed by the standard * If any of these conditions are not satisfied, don't invoke the handler and * send error status without closing connection *//* ... */ resp_payload->status = STATUS__InvalidArgument; if (req->cmd_set_config->bssid.len != 0 && req->cmd_set_config->bssid.len != sizeof(req_data.bssid)) { ESP_LOGD(TAG, "Received invalid BSSID"); }{...} else if (req->cmd_set_config->ssid.len >= sizeof(req_data.ssid)) { ESP_LOGD(TAG, "Received invalid SSID"); }{...} else if (req->cmd_set_config->passphrase.len >= sizeof(req_data.password)) { ESP_LOGD(TAG, "Received invalid Passphrase"); }{...} else { /* The received SSID and Passphrase are not NULL terminated so * we memcpy over zeroed out arrays. Above length checks ensure * that there is at least 1 extra byte for null termination *//* ... */ memcpy(req_data.ssid, req->cmd_set_config->ssid.data, req->cmd_set_config->ssid.len); memcpy(req_data.password, req->cmd_set_config->passphrase.data, req->cmd_set_config->passphrase.len); memcpy(req_data.bssid, req->cmd_set_config->bssid.data, req->cmd_set_config->bssid.len); req_data.channel = req->cmd_set_config->channel; if (h->set_config_handler(&req_data, &h->ctx) == ESP_OK) { resp_payload->status = STATUS__Success; }{...} else { resp_payload->status = STATUS__InternalError; }{...} }{...} resp->payload_case = WI_FI_CONFIG_PAYLOAD__PAYLOAD_RESP_SET_CONFIG; resp->resp_set_config = resp_payload; return ESP_OK; }{ ... } static esp_err_t cmd_apply_config_handler(WiFiConfigPayload *req, WiFiConfigPayload *resp, void *priv_data) { ESP_LOGD(TAG, "Enter cmd_apply_config_handler"); wifi_prov_config_handlers_t *h = (wifi_prov_config_handlers_t *) priv_data; if (!h) { ESP_LOGE(TAG, "Command invoked without handlers"); return ESP_ERR_INVALID_STATE; }{...} RespApplyConfig *resp_payload = (RespApplyConfig *) malloc(sizeof(RespApplyConfig)); if (!resp_payload) { ESP_LOGE(TAG, "Error allocating memory"); return ESP_ERR_NO_MEM; }{...} resp_apply_config__init(resp_payload); if (h->apply_config_handler(&h->ctx) == ESP_OK) { resp_payload->status = STATUS__Success; }{...} else { resp_payload->status = STATUS__InternalError; }{...} resp->payload_case = WI_FI_CONFIG_PAYLOAD__PAYLOAD_RESP_APPLY_CONFIG; resp->resp_apply_config = resp_payload; return ESP_OK; }{ ... } static int lookup_cmd_handler(int cmd_id) { for (size_t i = 0; i < sizeof(cmd_table)/sizeof(wifi_prov_config_cmd_t); i++) { if (cmd_table[i].cmd_num == cmd_id) { return i; }{...} }{...} return -1; }{ ... } static void wifi_prov_config_command_cleanup(WiFiConfigPayload *resp, void *priv_data) { if (!resp) { return; }{...} switch (resp->msg) { case WI_FI_CONFIG_MSG_TYPE__TypeRespGetStatus: { switch (resp->resp_get_status->sta_state) { case WIFI_STATION_STATE__Connecting: break;... case WIFI_STATION_STATE__Connected: if (resp->resp_get_status->connected) { if (resp->resp_get_status->connected->ip4_addr) { free(resp->resp_get_status->connected->ip4_addr); }{...} if (resp->resp_get_status->connected->bssid.data) { free(resp->resp_get_status->connected->bssid.data); }{...} if (resp->resp_get_status->connected->ssid.data) { free(resp->resp_get_status->connected->ssid.data); }{...} free(resp->resp_get_status->connected); }{...} break;... case WIFI_STATION_STATE__ConnectionFailed: break;... default: break;... }{...} free(resp->resp_get_status); }{...} break;... case WI_FI_CONFIG_MSG_TYPE__TypeRespSetConfig: { free(resp->resp_set_config); }{...} break;... case WI_FI_CONFIG_MSG_TYPE__TypeRespApplyConfig: { free(resp->resp_apply_config); }{...} break;... default: ESP_LOGE(TAG, "Unsupported response type in cleanup_handler"); break;... }{...} return; }{ ... } static esp_err_t wifi_prov_config_command_dispatcher(WiFiConfigPayload *req, WiFiConfigPayload *resp, void *priv_data) { esp_err_t ret; ESP_LOGD(TAG, "In wifi_prov_config_command_dispatcher Cmd=%d", req->msg); int cmd_index = lookup_cmd_handler(req->msg); if (cmd_index < 0) { ESP_LOGE(TAG, "Invalid command handler lookup"); return ESP_FAIL; }{...} ret = cmd_table[cmd_index].command_handler(req, resp, priv_data); if (ret != ESP_OK) { ESP_LOGE(TAG, "Error executing command handler"); return ESP_FAIL; }{...} return ESP_OK; }{ ... } esp_err_t wifi_prov_config_data_handler(uint32_t session_id, const uint8_t *inbuf, ssize_t inlen, uint8_t **outbuf, ssize_t *outlen, void *priv_data) { WiFiConfigPayload *req; WiFiConfigPayload resp; esp_err_t ret; req = wi_fi_config_payload__unpack(NULL, inlen, inbuf); if (!req) { ESP_LOGE(TAG, "Unable to unpack config data"); return ESP_ERR_INVALID_ARG; }{...} wi_fi_config_payload__init(&resp); ret = wifi_prov_config_command_dispatcher(req, &resp, priv_data); if (ret != ESP_OK) { ESP_LOGE(TAG, "Proto command dispatcher error %d", ret); return ESP_FAIL; }{...} resp.msg = req->msg + 1; /* Response is request + 1 */ wi_fi_config_payload__free_unpacked(req, NULL); *outlen = wi_fi_config_payload__get_packed_size(&resp); if (*outlen <= 0) { ESP_LOGE(TAG, "Invalid encoding for response"); return ESP_FAIL; }{...} *outbuf = (uint8_t *) malloc(*outlen); if (!*outbuf) { ESP_LOGE(TAG, "System out of memory"); return ESP_ERR_NO_MEM; }{...} wi_fi_config_payload__pack(&resp, *outbuf); wifi_prov_config_command_cleanup(&resp, priv_data); return ESP_OK; }{ ... }
Details
Show:
from
Types: Columns:
This file uses the notable symbols shown below. Click anywhere in the file to view more details.