Select one of the symbols to view example projects that use it.
 
Outline
#include <string.h>
#include "protocol_examples_common.h"
#include "example_common_private.h"
#include "esp_log.h"
TAG
s_example_sta_netif
s_semph_get_ip_addrs
s_semph_get_ip6_addrs
s_retry_num
example_handler_on_wifi_disconnect(void *, esp_event_base_t, int32_t, void *)
example_handler_on_wifi_connect(void *, esp_event_base_t, int32_t, void *)
example_handler_on_sta_got_ip(void *, esp_event_base_t, int32_t, void *)
example_handler_on_sta_got_ipv6(void *, esp_event_base_t, int32_t, void *)
example_wifi_start()
example_wifi_stop()
example_wifi_sta_do_connect(wifi_config_t, bool)
example_wifi_sta_do_disconnect()
example_wifi_shutdown()
example_wifi_connect()
Files
loading...
SourceVuESP-IDF Framework and ExamplesESP-IDFexamples/common_components/protocol_examples_common/wifi_connect.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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* * SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 *//* ... */ /* Common functions for protocol examples, to establish Wi-Fi or Ethernet connection. 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 "protocol_examples_common.h" #include "example_common_private.h" #include "esp_log.h" #if CONFIG_EXAMPLE_CONNECT_WIFI static const char *TAG = "example_connect"; static esp_netif_t *s_example_sta_netif = NULL; static SemaphoreHandle_t s_semph_get_ip_addrs = NULL; #if CONFIG_EXAMPLE_CONNECT_IPV6 static SemaphoreHandle_t s_semph_get_ip6_addrs = NULL; #endif static int s_retry_num = 0; static void example_handler_on_wifi_disconnect(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) { s_retry_num++; if (s_retry_num > CONFIG_EXAMPLE_WIFI_CONN_MAX_RETRY) { ESP_LOGI(TAG, "WiFi Connect failed %d times, stop reconnect.", s_retry_num); /* let example_wifi_sta_do_connect() return */ if (s_semph_get_ip_addrs) { xSemaphoreGive(s_semph_get_ip_addrs); }{...} #if CONFIG_EXAMPLE_CONNECT_IPV6 if (s_semph_get_ip6_addrs) { xSemaphoreGive(s_semph_get_ip6_addrs); }{...} #endif/* ... */ example_wifi_sta_do_disconnect(); return; }{...} wifi_event_sta_disconnected_t *disconn = event_data; if (disconn->reason == WIFI_REASON_ROAMING) { ESP_LOGD(TAG, "station roaming, do nothing"); return; }{...} ESP_LOGI(TAG, "Wi-Fi disconnected %d, trying to reconnect...", disconn->reason); esp_err_t err = esp_wifi_connect(); if (err == ESP_ERR_WIFI_NOT_STARTED) { return; }{...} ESP_ERROR_CHECK(err); }{ ... } static void example_handler_on_wifi_connect(void *esp_netif, esp_event_base_t event_base, int32_t event_id, void *event_data) { #if CONFIG_EXAMPLE_CONNECT_IPV6 esp_netif_create_ip6_linklocal(esp_netif); #endif // CONFIG_EXAMPLE_CONNECT_IPV6 }{ ... } static void example_handler_on_sta_got_ip(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) { s_retry_num = 0; ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data; if (!example_is_our_netif(EXAMPLE_NETIF_DESC_STA, event->esp_netif)) { return; }{...} ESP_LOGI(TAG, "Got IPv4 event: Interface \"%s\" address: " IPSTR, esp_netif_get_desc(event->esp_netif), IP2STR(&event->ip_info.ip)); if (s_semph_get_ip_addrs) { xSemaphoreGive(s_semph_get_ip_addrs); }{...} else { ESP_LOGI(TAG, "- IPv4 address: " IPSTR ",", IP2STR(&event->ip_info.ip)); }{...} }{ ... } #if CONFIG_EXAMPLE_CONNECT_IPV6 static void example_handler_on_sta_got_ipv6(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) { ip_event_got_ip6_t *event = (ip_event_got_ip6_t *)event_data; if (!example_is_our_netif(EXAMPLE_NETIF_DESC_STA, event->esp_netif)) { return; }{...} esp_ip6_addr_type_t ipv6_type = esp_netif_ip6_get_addr_type(&event->ip6_info.ip); ESP_LOGI(TAG, "Got IPv6 event: Interface \"%s\" address: " IPV6STR ", type: %s", esp_netif_get_desc(event->esp_netif), IPV62STR(event->ip6_info.ip), example_ipv6_addr_types_to_str[ipv6_type]); if (ipv6_type == EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE) { if (s_semph_get_ip6_addrs) { xSemaphoreGive(s_semph_get_ip6_addrs); }{...} else { ESP_LOGI(TAG, "- IPv6 address: " IPV6STR ", type: %s", IPV62STR(event->ip6_info.ip), example_ipv6_addr_types_to_str[ipv6_type]); }{...} }{...} }{ ... } /* ... */#endif // CONFIG_EXAMPLE_CONNECT_IPV6 void example_wifi_start(void) { wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); esp_netif_inherent_config_t esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_WIFI_STA(); // Warning: the interface desc is used in tests to capture actual connection details (IP, gw, mask) esp_netif_config.if_desc = EXAMPLE_NETIF_DESC_STA; esp_netif_config.route_prio = 128; s_example_sta_netif = esp_netif_create_wifi(WIFI_IF_STA, &esp_netif_config); esp_wifi_set_default_wifi_sta_handlers(); ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); ESP_ERROR_CHECK(esp_wifi_start()); }{ ... } void example_wifi_stop(void) { esp_err_t err = esp_wifi_stop(); if (err == ESP_ERR_WIFI_NOT_INIT) { return; }{...} ESP_ERROR_CHECK(err); ESP_ERROR_CHECK(esp_wifi_deinit()); ESP_ERROR_CHECK(esp_wifi_clear_default_wifi_driver_and_handlers(s_example_sta_netif)); esp_netif_destroy(s_example_sta_netif); s_example_sta_netif = NULL; }{ ... } esp_err_t example_wifi_sta_do_connect(wifi_config_t wifi_config, bool wait) { if (wait) { s_semph_get_ip_addrs = xSemaphoreCreateBinary(); if (s_semph_get_ip_addrs == NULL) { return ESP_ERR_NO_MEM; }{...} #if CONFIG_EXAMPLE_CONNECT_IPV6 s_semph_get_ip6_addrs = xSemaphoreCreateBinary(); if (s_semph_get_ip6_addrs == NULL) { vSemaphoreDelete(s_semph_get_ip_addrs); return ESP_ERR_NO_MEM; }{...} #endif/* ... */ }{...} s_retry_num = 0; ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &example_handler_on_wifi_disconnect, NULL)); ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &example_handler_on_sta_got_ip, NULL)); ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &example_handler_on_wifi_connect, s_example_sta_netif)); #if CONFIG_EXAMPLE_CONNECT_IPV6 ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, &example_handler_on_sta_got_ipv6, NULL)); #endif ESP_LOGI(TAG, "Connecting to %s...", wifi_config.sta.ssid); ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config)); esp_err_t ret = esp_wifi_connect(); if (ret != ESP_OK) { ESP_LOGE(TAG, "WiFi connect failed! ret:%x", ret); return ret; }{...} if (wait) { ESP_LOGI(TAG, "Waiting for IP(s)"); #if CONFIG_EXAMPLE_CONNECT_IPV4 xSemaphoreTake(s_semph_get_ip_addrs, portMAX_DELAY); #endif #if CONFIG_EXAMPLE_CONNECT_IPV6 xSemaphoreTake(s_semph_get_ip6_addrs, portMAX_DELAY); #endif if (s_retry_num > CONFIG_EXAMPLE_WIFI_CONN_MAX_RETRY) { return ESP_FAIL; }{...} }{...} return ESP_OK; }{ ... } esp_err_t example_wifi_sta_do_disconnect(void) { ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &example_handler_on_wifi_disconnect)); ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, &example_handler_on_sta_got_ip)); ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &example_handler_on_wifi_connect)); #if CONFIG_EXAMPLE_CONNECT_IPV6 ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_GOT_IP6, &example_handler_on_sta_got_ipv6)); #endif if (s_semph_get_ip_addrs) { vSemaphoreDelete(s_semph_get_ip_addrs); }{...} #if CONFIG_EXAMPLE_CONNECT_IPV6 if (s_semph_get_ip6_addrs) { vSemaphoreDelete(s_semph_get_ip6_addrs); }{...} #endif/* ... */ return esp_wifi_disconnect(); }{ ... } void example_wifi_shutdown(void) { example_wifi_sta_do_disconnect(); example_wifi_stop(); }{ ... } esp_err_t example_wifi_connect(void) { ESP_LOGI(TAG, "Start example_connect."); example_wifi_start(); wifi_config_t wifi_config = { .sta = { #if !CONFIG_EXAMPLE_WIFI_SSID_PWD_FROM_STDIN .ssid = CONFIG_EXAMPLE_WIFI_SSID, .password = CONFIG_EXAMPLE_WIFI_PASSWORD,/* ... */ #endif .scan_method = EXAMPLE_WIFI_SCAN_METHOD, .sort_method = EXAMPLE_WIFI_CONNECT_AP_SORT_METHOD, .threshold.rssi = CONFIG_EXAMPLE_WIFI_SCAN_RSSI_THRESHOLD, .threshold.authmode = EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD, }{...}, }{...}; #if CONFIG_EXAMPLE_WIFI_SSID_PWD_FROM_STDIN example_configure_stdin_stdout(); char buf[sizeof(wifi_config.sta.ssid)+sizeof(wifi_config.sta.password)+2] = {0}; ESP_LOGI(TAG, "Please input ssid password:"); fgets(buf, sizeof(buf), stdin); int len = strlen(buf); buf[len-1] = '\0'; /* removes '\n' */ memset(wifi_config.sta.ssid, 0, sizeof(wifi_config.sta.ssid)); char *rest = NULL; char *temp = strtok_r(buf, " ", &rest); strncpy((char*)wifi_config.sta.ssid, temp, sizeof(wifi_config.sta.ssid)); memset(wifi_config.sta.password, 0, sizeof(wifi_config.sta.password)); temp = strtok_r(NULL, " ", &rest); if (temp) { strncpy((char*)wifi_config.sta.password, temp, sizeof(wifi_config.sta.password)); }{...} else { wifi_config.sta.threshold.authmode = WIFI_AUTH_OPEN; }{...} #endif/* ... */ return example_wifi_sta_do_connect(wifi_config, true); }{ ... } /* ... */ #endif /* CONFIG_EXAMPLE_CONNECT_WIFI */
Details
Show:
from
Types: Columns:
This file uses the notable symbols shown below. Click anywhere in the file to view more details.