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 "sdkconfig.h"
#include "esp_event.h"
#include "esp_wifi.h"
#include "esp_wifi_default.h"
#include "esp_log.h"
#include "esp_netif.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "lwip/err.h"
#include "lwip/sys.h"
TAG
example_ipv6_addr_types_to_str
example_is_our_netif(const char *, esp_netif_t *)
netif_desc_matches_with(esp_netif_t *, void *)
get_example_netif_from_desc(const char *)
print_all_ips_tcpip(void *)
example_print_all_netif_ips(const char *)
example_connect()
example_disconnect()
Files
loading...
SourceVuESP-IDF Framework and ExamplesESP-IDFexamples/common_components/protocol_examples_common/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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* * SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 *//* ... */ #include <string.h> #include "protocol_examples_common.h" #include "example_common_private.h" #include "sdkconfig.h" #include "esp_event.h" #include "esp_wifi.h" #include "esp_wifi_default.h" #include "esp_log.h" #include "esp_netif.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/event_groups.h" #include "lwip/err.h" #include "lwip/sys.h"14 includes static const char *TAG = "example_common"; #if CONFIG_EXAMPLE_CONNECT_IPV6 /* types of ipv6 addresses to be displayed on ipv6 events */ const char *example_ipv6_addr_types_to_str[6] = { "ESP_IP6_ADDR_IS_UNKNOWN", "ESP_IP6_ADDR_IS_GLOBAL", "ESP_IP6_ADDR_IS_LINK_LOCAL", "ESP_IP6_ADDR_IS_SITE_LOCAL", "ESP_IP6_ADDR_IS_UNIQUE_LOCAL", "ESP_IP6_ADDR_IS_IPV4_MAPPED_IPV6" }{...}; /* ... */#endif /** * @brief Checks the netif description if it contains specified prefix. * All netifs created within common connect component are prefixed with the module TAG, * so it returns true if the specified netif is owned by this module *//* ... */ bool example_is_our_netif(const char *prefix, esp_netif_t *netif) { return strncmp(prefix, esp_netif_get_desc(netif), strlen(prefix) - 1) == 0; }{ ... } static bool netif_desc_matches_with(esp_netif_t *netif, void *ctx) { return strcmp(ctx, esp_netif_get_desc(netif)) == 0; }{ ... } esp_netif_t *get_example_netif_from_desc(const char *desc) { return esp_netif_find_if(netif_desc_matches_with, (void*)desc); }{ ... } static esp_err_t print_all_ips_tcpip(void* ctx) { const char *prefix = ctx; // iterate over active interfaces, and print out IPs of "our" netifs esp_netif_t *netif = NULL; while ((netif = esp_netif_next_unsafe(netif)) != NULL) { if (example_is_our_netif(prefix, netif)) { ESP_LOGI(TAG, "Connected to %s", esp_netif_get_desc(netif)); #if CONFIG_EXAMPLE_CONNECT_IPV4 esp_netif_ip_info_t ip; ESP_ERROR_CHECK(esp_netif_get_ip_info(netif, &ip)); ESP_LOGI(TAG, "- IPv4 address: " IPSTR ",", IP2STR(&ip.ip));/* ... */ #endif #if CONFIG_EXAMPLE_CONNECT_IPV6 esp_ip6_addr_t ip6[MAX_IP6_ADDRS_PER_NETIF]; int ip6_addrs = esp_netif_get_all_ip6(netif, ip6); for (int j = 0; j < ip6_addrs; ++j) { esp_ip6_addr_type_t ipv6_type = esp_netif_ip6_get_addr_type(&(ip6[j])); ESP_LOGI(TAG, "- IPv6 address: " IPV6STR ", type: %s", IPV62STR(ip6[j]), example_ipv6_addr_types_to_str[ipv6_type]); }{...} #endif/* ... */ }{...} }{...} return ESP_OK; }{ ... } void example_print_all_netif_ips(const char *prefix) { // Print all IPs in TCPIP context to avoid potential races of removing/adding netifs when iterating over the list esp_netif_tcpip_exec(print_all_ips_tcpip, (void*) prefix); }{ ... } esp_err_t example_connect(void) { #if CONFIG_EXAMPLE_CONNECT_ETHERNET if (example_ethernet_connect() != ESP_OK) { return ESP_FAIL; }{...} ESP_ERROR_CHECK(esp_register_shutdown_handler(&example_ethernet_shutdown));/* ... */ #endif #if CONFIG_EXAMPLE_CONNECT_WIFI if (example_wifi_connect() != ESP_OK) { return ESP_FAIL; }{...} ESP_ERROR_CHECK(esp_register_shutdown_handler(&example_wifi_shutdown));/* ... */ #endif #if CONFIG_EXAMPLE_CONNECT_THREAD if (example_thread_connect() != ESP_OK) { return ESP_FAIL; }{...} ESP_ERROR_CHECK(esp_register_shutdown_handler(&example_thread_shutdown));/* ... */ #endif #if CONFIG_EXAMPLE_CONNECT_PPP if (example_ppp_connect() != ESP_OK) { return ESP_FAIL; }{...} ESP_ERROR_CHECK(esp_register_shutdown_handler(&example_ppp_shutdown));/* ... */ #endif #if CONFIG_EXAMPLE_CONNECT_ETHERNET example_print_all_netif_ips(EXAMPLE_NETIF_DESC_ETH); #endif #if CONFIG_EXAMPLE_CONNECT_WIFI example_print_all_netif_ips(EXAMPLE_NETIF_DESC_STA); #endif #if CONFIG_EXAMPLE_CONNECT_THREAD example_print_all_netif_ips(EXAMPLE_NETIF_DESC_THREAD); #endif #if CONFIG_EXAMPLE_CONNECT_PPP example_print_all_netif_ips(EXAMPLE_NETIF_DESC_PPP); #endif return ESP_OK; }{ ... } esp_err_t example_disconnect(void) { #if CONFIG_EXAMPLE_CONNECT_ETHERNET example_ethernet_shutdown(); ESP_ERROR_CHECK(esp_unregister_shutdown_handler(&example_ethernet_shutdown));/* ... */ #endif #if CONFIG_EXAMPLE_CONNECT_WIFI example_wifi_shutdown(); ESP_ERROR_CHECK(esp_unregister_shutdown_handler(&example_wifi_shutdown));/* ... */ #endif return ESP_OK; }{ ... }
Details