2022-07-03 03:24:39 -04:00
|
|
|
/*
|
2022-10-03 11:28:01 -04:00
|
|
|
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
|
2022-07-03 03:24:39 -04:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Unlicense OR CC0-1.0
|
2018-11-20 11:40:26 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include "protocol_examples_common.h"
|
2022-07-03 03:24:39 -04:00
|
|
|
#include "example_common_private.h"
|
2018-11-20 11:40:26 -05:00
|
|
|
#include "sdkconfig.h"
|
|
|
|
#include "esp_event.h"
|
|
|
|
#include "esp_wifi.h"
|
2019-09-04 07:58:29 -04:00
|
|
|
#include "esp_wifi_default.h"
|
2018-11-20 11:40:26 -05:00
|
|
|
#include "esp_log.h"
|
2019-08-31 10:08:46 -04:00
|
|
|
#include "esp_netif.h"
|
2018-11-20 11:40:26 -05:00
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
|
|
|
#include "freertos/event_groups.h"
|
|
|
|
#include "lwip/err.h"
|
|
|
|
#include "lwip/sys.h"
|
|
|
|
|
2022-07-03 03:24:39 -04:00
|
|
|
static const char *TAG = "example_common";
|
2020-02-07 03:19:55 -05:00
|
|
|
|
2022-07-03 03:24:39 -04:00
|
|
|
#if CONFIG_EXAMPLE_CONNECT_IPV6
|
2020-02-07 03:19:55 -05:00
|
|
|
/* types of ipv6 addresses to be displayed on ipv6 events */
|
2022-07-03 03:24:39 -04:00
|
|
|
const char *example_ipv6_addr_types_to_str[6] = {
|
2020-02-07 03:19:55 -05:00
|
|
|
"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"
|
2020-11-06 03:06:20 -05:00
|
|
|
};
|
2018-11-20 11:40:26 -05:00
|
|
|
#endif
|
|
|
|
|
2020-04-23 10:23:46 -04:00
|
|
|
/**
|
|
|
|
* @brief Checks the netif description if it contains specified prefix.
|
|
|
|
* All netifs created withing common connect component are prefixed with the module TAG,
|
|
|
|
* so it returns true if the specified netif is owned by this module
|
|
|
|
*/
|
2022-07-03 03:24:39 -04:00
|
|
|
bool example_is_our_netif(const char *prefix, esp_netif_t *netif)
|
2020-04-23 10:23:46 -04:00
|
|
|
{
|
2020-11-06 03:06:20 -05:00
|
|
|
return strncmp(prefix, esp_netif_get_desc(netif), strlen(prefix) - 1) == 0;
|
2020-04-23 10:23:46 -04:00
|
|
|
}
|
|
|
|
|
2023-10-03 11:38:18 -04:00
|
|
|
static bool netif_desc_matches_with(esp_netif_t *netif, void *ctx)
|
|
|
|
{
|
|
|
|
return strcmp(ctx, esp_netif_get_desc(netif)) == 0;
|
|
|
|
}
|
|
|
|
|
2022-07-03 03:24:39 -04:00
|
|
|
esp_netif_t *get_example_netif_from_desc(const char *desc)
|
2018-11-20 11:40:26 -05:00
|
|
|
{
|
2023-10-03 11:38:18 -04:00
|
|
|
return esp_netif_find_if(netif_desc_matches_with, (void*)desc);
|
2018-11-20 11:40:26 -05:00
|
|
|
}
|
|
|
|
|
2023-10-03 11:38:18 -04:00
|
|
|
static esp_err_t print_all_ips_tcpip(void* ctx)
|
2018-11-20 11:40:26 -05:00
|
|
|
{
|
2023-10-03 11:38:18 -04:00
|
|
|
const char *prefix = ctx;
|
2020-04-23 10:23:46 -04:00
|
|
|
// iterate over active interfaces, and print out IPs of "our" netifs
|
|
|
|
esp_netif_t *netif = NULL;
|
2023-10-03 11:38:18 -04:00
|
|
|
while ((netif = esp_netif_next_unsafe(netif)) != NULL) {
|
2022-07-03 03:24:39 -04:00
|
|
|
if (example_is_our_netif(prefix, netif)) {
|
2020-04-23 10:23:46 -04:00
|
|
|
ESP_LOGI(TAG, "Connected to %s", esp_netif_get_desc(netif));
|
2022-10-03 11:28:01 -04:00
|
|
|
#if CONFIG_LWIP_IPV4
|
|
|
|
esp_netif_ip_info_t ip;
|
2020-04-23 10:23:46 -04:00
|
|
|
ESP_ERROR_CHECK(esp_netif_get_ip_info(netif, &ip));
|
|
|
|
|
2022-07-03 03:24:39 -04:00
|
|
|
ESP_LOGI(TAG, "- IPv4 address: " IPSTR ",", IP2STR(&ip.ip));
|
2022-10-03 11:28:01 -04:00
|
|
|
#endif
|
2022-07-03 03:24:39 -04:00
|
|
|
#if CONFIG_EXAMPLE_CONNECT_IPV6
|
2020-05-22 04:41:55 -04:00
|
|
|
esp_ip6_addr_t ip6[MAX_IP6_ADDRS_PER_NETIF];
|
2020-04-23 10:23:46 -04:00
|
|
|
int ip6_addrs = esp_netif_get_all_ip6(netif, ip6);
|
2020-11-06 03:06:20 -05:00
|
|
|
for (int j = 0; j < ip6_addrs; ++j) {
|
2020-04-23 10:23:46 -04:00
|
|
|
esp_ip6_addr_type_t ipv6_type = esp_netif_ip6_get_addr_type(&(ip6[j]));
|
2022-07-03 03:24:39 -04:00
|
|
|
ESP_LOGI(TAG, "- IPv6 address: " IPV6STR ", type: %s", IPV62STR(ip6[j]), example_ipv6_addr_types_to_str[ipv6_type]);
|
2020-04-23 10:23:46 -04:00
|
|
|
}
|
2018-11-20 11:40:26 -05:00
|
|
|
#endif
|
2020-04-23 10:23:46 -04:00
|
|
|
}
|
|
|
|
}
|
2023-10-03 11:38:18 -04:00
|
|
|
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);
|
2018-11-20 11:40:26 -05:00
|
|
|
}
|
|
|
|
|
2019-04-10 04:24:50 -04:00
|
|
|
|
2022-07-03 03:24:39 -04:00
|
|
|
esp_err_t example_connect(void)
|
2018-11-20 11:40:26 -05:00
|
|
|
{
|
2022-07-03 03:24:39 -04:00
|
|
|
#if CONFIG_EXAMPLE_CONNECT_ETHERNET
|
2022-07-04 10:13:16 -04:00
|
|
|
if (example_ethernet_connect() != ESP_OK) {
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
2022-07-03 03:24:39 -04:00
|
|
|
ESP_ERROR_CHECK(esp_register_shutdown_handler(&example_ethernet_shutdown));
|
2019-04-10 04:24:50 -04:00
|
|
|
#endif
|
2022-07-03 03:24:39 -04:00
|
|
|
#if CONFIG_EXAMPLE_CONNECT_WIFI
|
2022-07-04 10:13:16 -04:00
|
|
|
if (example_wifi_connect() != ESP_OK) {
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
2022-07-03 03:24:39 -04:00
|
|
|
ESP_ERROR_CHECK(esp_register_shutdown_handler(&example_wifi_shutdown));
|
2020-11-06 03:06:20 -05:00
|
|
|
#endif
|
2023-10-04 11:35:57 -04:00
|
|
|
#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
|
2021-08-03 07:34:52 -04:00
|
|
|
|
2022-07-03 03:24:39 -04:00
|
|
|
#if CONFIG_EXAMPLE_CONNECT_ETHERNET
|
|
|
|
example_print_all_netif_ips(EXAMPLE_NETIF_DESC_ETH);
|
2021-08-03 07:34:52 -04:00
|
|
|
#endif
|
2022-07-04 10:13:16 -04:00
|
|
|
|
2022-07-03 03:24:39 -04:00
|
|
|
#if CONFIG_EXAMPLE_CONNECT_WIFI
|
|
|
|
example_print_all_netif_ips(EXAMPLE_NETIF_DESC_STA);
|
2018-11-20 11:40:26 -05:00
|
|
|
#endif
|
|
|
|
|
2023-10-04 11:35:57 -04:00
|
|
|
#if CONFIG_EXAMPLE_CONNECT_PPP
|
|
|
|
example_print_all_netif_ips(EXAMPLE_NETIF_DESC_PPP);
|
|
|
|
#endif
|
|
|
|
|
2022-07-03 03:24:39 -04:00
|
|
|
return ESP_OK;
|
2022-03-31 08:52:27 -04:00
|
|
|
}
|
|
|
|
|
2019-08-31 10:08:46 -04:00
|
|
|
|
2022-07-03 03:24:39 -04:00
|
|
|
esp_err_t example_disconnect(void)
|
2020-04-23 10:23:46 -04:00
|
|
|
{
|
2022-07-03 03:24:39 -04:00
|
|
|
#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;
|
2020-04-23 10:23:46 -04:00
|
|
|
}
|