2018-11-20 11:40:26 -05:00
|
|
|
/* 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 "sdkconfig.h"
|
|
|
|
#include "esp_event.h"
|
|
|
|
#include "esp_wifi.h"
|
2019-09-04 07:58:29 -04:00
|
|
|
#include "esp_wifi_default.h"
|
2019-06-16 23:50:37 -04:00
|
|
|
#if CONFIG_EXAMPLE_CONNECT_ETHERNET
|
2018-11-20 11:40:26 -05:00
|
|
|
#include "esp_eth.h"
|
2020-11-06 03:06:20 -05:00
|
|
|
#if CONFIG_ETH_USE_SPI_ETHERNET
|
|
|
|
#include "driver/spi_master.h"
|
|
|
|
#endif // CONFIG_ETH_USE_SPI_ETHERNET
|
|
|
|
#endif // CONFIG_EXAMPLE_CONNECT_ETHERNET
|
2018-11-20 11:40:26 -05:00
|
|
|
#include "esp_log.h"
|
2019-08-31 10:08:46 -04:00
|
|
|
#include "esp_netif.h"
|
2019-11-13 23:03:14 -05:00
|
|
|
#include "driver/gpio.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"
|
|
|
|
|
|
|
|
#ifdef CONFIG_EXAMPLE_CONNECT_IPV6
|
2020-04-23 10:23:46 -04:00
|
|
|
#define MAX_IP6_ADDRS_PER_NETIF (5)
|
|
|
|
#define NR_OF_IP_ADDRESSES_TO_WAIT_FOR (s_active_interfaces*2)
|
2020-02-07 03:19:55 -05:00
|
|
|
|
|
|
|
#if defined(CONFIG_EXAMPLE_CONNECT_IPV6_PREF_LOCAL_LINK)
|
|
|
|
#define EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE ESP_IP6_ADDR_IS_LINK_LOCAL
|
|
|
|
#elif defined(CONFIG_EXAMPLE_CONNECT_IPV6_PREF_GLOBAL)
|
|
|
|
#define EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE ESP_IP6_ADDR_IS_GLOBAL
|
|
|
|
#elif defined(CONFIG_EXAMPLE_CONNECT_IPV6_PREF_SITE_LOCAL)
|
|
|
|
#define EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE ESP_IP6_ADDR_IS_SITE_LOCAL
|
|
|
|
#elif defined(CONFIG_EXAMPLE_CONNECT_IPV6_PREF_UNIQUE_LOCAL)
|
|
|
|
#define EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE ESP_IP6_ADDR_IS_UNIQUE_LOCAL
|
|
|
|
#endif // if-elif CONFIG_EXAMPLE_CONNECT_IPV6_PREF_...
|
|
|
|
|
2018-11-20 11:40:26 -05:00
|
|
|
#else
|
2020-04-23 10:23:46 -04:00
|
|
|
#define NR_OF_IP_ADDRESSES_TO_WAIT_FOR (s_active_interfaces)
|
2018-11-20 11:40:26 -05:00
|
|
|
#endif
|
|
|
|
|
2020-06-05 08:29:32 -04:00
|
|
|
#define EXAMPLE_DO_CONNECT CONFIG_EXAMPLE_CONNECT_WIFI || CONFIG_EXAMPLE_CONNECT_ETHERNET
|
|
|
|
|
2021-02-25 06:47:53 -05:00
|
|
|
#if CONFIG_EXAMPLE_WIFI_SCAN_METHOD_FAST
|
|
|
|
#define EXAMPLE_WIFI_SCAN_METHOD WIFI_FAST_SCAN
|
|
|
|
#elif CONFIG_EXAMPLE_WIFI_SCAN_METHOD_ALL_CHANNEL
|
|
|
|
#define EXAMPLE_WIFI_SCAN_METHOD WIFI_ALL_CHANNEL_SCAN
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CONFIG_EXAMPLE_WIFI_CONNECT_AP_BY_SIGNAL
|
|
|
|
#define EXAMPLE_WIFI_CONNECT_AP_SORT_METHOD WIFI_CONNECT_AP_BY_SIGNAL
|
|
|
|
#elif CONFIG_EXAMPLE_WIFI_CONNECT_AP_BY_SECURITY
|
|
|
|
#define EXAMPLE_WIFI_CONNECT_AP_SORT_METHOD WIFI_CONNECT_AP_BY_SECURITY
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CONFIG_EXAMPLE_WIFI_AUTH_OPEN
|
|
|
|
#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_OPEN
|
|
|
|
#elif CONFIG_EXAMPLE_WIFI_AUTH_WEP
|
|
|
|
#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WEP
|
|
|
|
#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA_PSK
|
|
|
|
#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_PSK
|
|
|
|
#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA2_PSK
|
|
|
|
#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_PSK
|
|
|
|
#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA_WPA2_PSK
|
|
|
|
#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_WPA2_PSK
|
|
|
|
#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA2_ENTERPRISE
|
|
|
|
#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_ENTERPRISE
|
|
|
|
#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA3_PSK
|
|
|
|
#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA3_PSK
|
|
|
|
#elif CONFIG_EXAMPLE_WIFI_AUTH_WPA2_WPA3_PSK
|
|
|
|
#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_WPA3_PSK
|
|
|
|
#elif CONFIG_EXAMPLE_WIFI_AUTH_WAPI_PSK
|
|
|
|
#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WAPI_PSK
|
|
|
|
#endif
|
|
|
|
|
2020-04-23 10:23:46 -04:00
|
|
|
static int s_active_interfaces = 0;
|
|
|
|
static xSemaphoreHandle s_semph_get_ip_addrs;
|
2019-08-31 10:08:46 -04:00
|
|
|
static esp_netif_t *s_example_esp_netif = NULL;
|
2018-11-20 11:40:26 -05:00
|
|
|
|
|
|
|
#ifdef CONFIG_EXAMPLE_CONNECT_IPV6
|
2019-08-31 10:08:46 -04:00
|
|
|
static esp_ip6_addr_t s_ipv6_addr;
|
2020-02-07 03:19:55 -05:00
|
|
|
|
|
|
|
/* types of ipv6 addresses to be displayed on ipv6 events */
|
|
|
|
static const char *s_ipv6_addr_types[] = {
|
|
|
|
"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
|
|
|
|
|
|
|
|
static const char *TAG = "example_connect";
|
|
|
|
|
2020-04-23 10:23:46 -04:00
|
|
|
#if CONFIG_EXAMPLE_CONNECT_WIFI
|
2020-11-06 03:06:20 -05:00
|
|
|
static esp_netif_t *wifi_start(void);
|
2020-04-23 10:23:46 -04:00
|
|
|
static void wifi_stop(void);
|
|
|
|
#endif
|
|
|
|
#if CONFIG_EXAMPLE_CONNECT_ETHERNET
|
2020-11-06 03:06:20 -05:00
|
|
|
static esp_netif_t *eth_start(void);
|
2020-04-23 10:23:46 -04:00
|
|
|
static void eth_stop(void);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
static bool is_our_netif(const char *prefix, esp_netif_t *netif)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/* set up connection, Wi-Fi and/or Ethernet */
|
|
|
|
static void start(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
#if CONFIG_EXAMPLE_CONNECT_WIFI
|
|
|
|
s_example_esp_netif = wifi_start();
|
|
|
|
s_active_interfaces++;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CONFIG_EXAMPLE_CONNECT_ETHERNET
|
|
|
|
s_example_esp_netif = eth_start();
|
|
|
|
s_active_interfaces++;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CONFIG_EXAMPLE_CONNECT_WIFI && CONFIG_EXAMPLE_CONNECT_ETHERNET
|
|
|
|
/* if both intefaces at once, clear out to indicate that multiple netifs are active */
|
|
|
|
s_example_esp_netif = NULL;
|
|
|
|
#endif
|
|
|
|
|
2020-06-05 08:29:32 -04:00
|
|
|
#if EXAMPLE_DO_CONNECT
|
|
|
|
/* create semaphore if at least one interface is active */
|
2020-04-23 10:23:46 -04:00
|
|
|
s_semph_get_ip_addrs = xSemaphoreCreateCounting(NR_OF_IP_ADDRESSES_TO_WAIT_FOR, 0);
|
2020-06-05 08:29:32 -04:00
|
|
|
#endif
|
|
|
|
|
2020-04-23 10:23:46 -04:00
|
|
|
}
|
2018-11-20 11:40:26 -05:00
|
|
|
|
|
|
|
/* tear down connection, release resources */
|
2020-04-23 10:23:46 -04:00
|
|
|
static void stop(void)
|
|
|
|
{
|
|
|
|
#if CONFIG_EXAMPLE_CONNECT_WIFI
|
|
|
|
wifi_stop();
|
|
|
|
s_active_interfaces--;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CONFIG_EXAMPLE_CONNECT_ETHERNET
|
|
|
|
eth_stop();
|
|
|
|
s_active_interfaces--;
|
|
|
|
#endif
|
|
|
|
}
|
2018-11-20 11:40:26 -05:00
|
|
|
|
2020-06-05 08:29:32 -04:00
|
|
|
#if EXAMPLE_DO_CONNECT
|
|
|
|
static esp_ip4_addr_t s_ip_addr;
|
|
|
|
|
2019-04-10 04:24:50 -04:00
|
|
|
static void on_got_ip(void *arg, esp_event_base_t event_base,
|
|
|
|
int32_t event_id, void *event_data)
|
2018-11-20 11:40:26 -05:00
|
|
|
{
|
2019-04-10 04:24:50 -04:00
|
|
|
ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
|
2020-04-23 10:23:46 -04:00
|
|
|
if (!is_our_netif(TAG, event->esp_netif)) {
|
|
|
|
ESP_LOGW(TAG, "Got IPv4 from another interface \"%s\": ignored", esp_netif_get_desc(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));
|
2018-11-20 11:40:26 -05:00
|
|
|
memcpy(&s_ip_addr, &event->ip_info.ip, sizeof(s_ip_addr));
|
2020-04-23 10:23:46 -04:00
|
|
|
xSemaphoreGive(s_semph_get_ip_addrs);
|
2018-11-20 11:40:26 -05:00
|
|
|
}
|
2020-06-05 08:29:32 -04:00
|
|
|
#endif
|
2018-11-20 11:40:26 -05:00
|
|
|
|
|
|
|
#ifdef CONFIG_EXAMPLE_CONNECT_IPV6
|
|
|
|
|
2019-04-10 04:24:50 -04:00
|
|
|
static void on_got_ipv6(void *arg, esp_event_base_t event_base,
|
|
|
|
int32_t event_id, void *event_data)
|
2018-11-20 11:40:26 -05:00
|
|
|
{
|
2019-04-10 04:24:50 -04:00
|
|
|
ip_event_got_ip6_t *event = (ip_event_got_ip6_t *)event_data;
|
2020-04-23 10:23:46 -04:00
|
|
|
if (!is_our_netif(TAG, event->esp_netif)) {
|
|
|
|
ESP_LOGW(TAG, "Got IPv6 from another netif: ignored");
|
2020-03-03 07:57:32 -05:00
|
|
|
return;
|
|
|
|
}
|
2020-02-07 03:19:55 -05:00
|
|
|
esp_ip6_addr_type_t ipv6_type = esp_netif_ip6_get_addr_type(&event->ip6_info.ip);
|
2020-04-23 10:23:46 -04:00
|
|
|
ESP_LOGI(TAG, "Got IPv6 event: Interface \"%s\" address: " IPV6STR ", type: %s", esp_netif_get_desc(event->esp_netif),
|
2020-11-06 03:06:20 -05:00
|
|
|
IPV62STR(event->ip6_info.ip), s_ipv6_addr_types[ipv6_type]);
|
2020-02-07 03:19:55 -05:00
|
|
|
if (ipv6_type == EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE) {
|
|
|
|
memcpy(&s_ipv6_addr, &event->ip6_info.ip, sizeof(s_ipv6_addr));
|
2020-04-23 10:23:46 -04:00
|
|
|
xSemaphoreGive(s_semph_get_ip_addrs);
|
2020-02-07 03:19:55 -05:00
|
|
|
}
|
2018-11-20 11:40:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // CONFIG_EXAMPLE_CONNECT_IPV6
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
esp_err_t example_connect(void)
|
2018-11-20 11:40:26 -05:00
|
|
|
{
|
2020-06-05 08:29:32 -04:00
|
|
|
#if EXAMPLE_DO_CONNECT
|
2020-04-23 10:23:46 -04:00
|
|
|
if (s_semph_get_ip_addrs != NULL) {
|
2018-11-20 11:40:26 -05:00
|
|
|
return ESP_ERR_INVALID_STATE;
|
|
|
|
}
|
2020-06-05 08:29:32 -04:00
|
|
|
#endif
|
2018-11-20 11:40:26 -05:00
|
|
|
start();
|
2019-10-07 10:46:25 -04:00
|
|
|
ESP_ERROR_CHECK(esp_register_shutdown_handler(&stop));
|
2020-04-23 10:23:46 -04:00
|
|
|
ESP_LOGI(TAG, "Waiting for IP(s)");
|
2020-11-06 03:06:20 -05:00
|
|
|
for (int i = 0; i < NR_OF_IP_ADDRESSES_TO_WAIT_FOR; ++i) {
|
2020-04-23 10:23:46 -04:00
|
|
|
xSemaphoreTake(s_semph_get_ip_addrs, portMAX_DELAY);
|
|
|
|
}
|
|
|
|
// iterate over active interfaces, and print out IPs of "our" netifs
|
|
|
|
esp_netif_t *netif = NULL;
|
|
|
|
esp_netif_ip_info_t ip;
|
2020-11-06 03:06:20 -05:00
|
|
|
for (int i = 0; i < esp_netif_get_nr_of_ifs(); ++i) {
|
2020-04-23 10:23:46 -04:00
|
|
|
netif = esp_netif_next(netif);
|
|
|
|
if (is_our_netif(TAG, netif)) {
|
|
|
|
ESP_LOGI(TAG, "Connected to %s", esp_netif_get_desc(netif));
|
|
|
|
ESP_ERROR_CHECK(esp_netif_get_ip_info(netif, &ip));
|
|
|
|
|
|
|
|
ESP_LOGI(TAG, "- IPv4 address: " IPSTR, IP2STR(&ip.ip));
|
2018-11-20 11:40:26 -05:00
|
|
|
#ifdef 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]));
|
|
|
|
ESP_LOGI(TAG, "- IPv6 address: " IPV6STR ", type: %s", IPV62STR(ip6[j]), s_ipv6_addr_types[ipv6_type]);
|
|
|
|
}
|
2018-11-20 11:40:26 -05:00
|
|
|
#endif
|
2020-04-23 10:23:46 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2018-11-20 11:40:26 -05:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
esp_err_t example_disconnect(void)
|
2018-11-20 11:40:26 -05:00
|
|
|
{
|
2020-04-23 10:23:46 -04:00
|
|
|
if (s_semph_get_ip_addrs == NULL) {
|
2018-11-20 11:40:26 -05:00
|
|
|
return ESP_ERR_INVALID_STATE;
|
|
|
|
}
|
2020-04-23 10:23:46 -04:00
|
|
|
vSemaphoreDelete(s_semph_get_ip_addrs);
|
|
|
|
s_semph_get_ip_addrs = NULL;
|
2018-11-20 11:40:26 -05:00
|
|
|
stop();
|
2020-07-29 04:18:34 -04:00
|
|
|
ESP_ERROR_CHECK(esp_unregister_shutdown_handler(&stop));
|
2018-11-20 11:40:26 -05:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_EXAMPLE_CONNECT_WIFI
|
|
|
|
|
2019-04-10 04:24:50 -04:00
|
|
|
static void on_wifi_disconnect(void *arg, esp_event_base_t event_base,
|
|
|
|
int32_t event_id, void *event_data)
|
2018-11-20 11:40:26 -05:00
|
|
|
{
|
|
|
|
ESP_LOGI(TAG, "Wi-Fi disconnected, trying to reconnect...");
|
2019-10-07 10:46:25 -04:00
|
|
|
esp_err_t err = esp_wifi_connect();
|
|
|
|
if (err == ESP_ERR_WIFI_NOT_STARTED) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ESP_ERROR_CHECK(err);
|
2018-11-20 11:40:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_EXAMPLE_CONNECT_IPV6
|
|
|
|
|
2019-08-31 10:08:46 -04:00
|
|
|
static void on_wifi_connect(void *esp_netif, esp_event_base_t event_base,
|
2019-04-10 04:24:50 -04:00
|
|
|
int32_t event_id, void *event_data)
|
2018-11-20 11:40:26 -05:00
|
|
|
{
|
2019-08-31 10:08:46 -04:00
|
|
|
esp_netif_create_ip6_linklocal(esp_netif);
|
2018-11-20 11:40:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // CONFIG_EXAMPLE_CONNECT_IPV6
|
|
|
|
|
2020-11-06 03:06:20 -05:00
|
|
|
static esp_netif_t *wifi_start(void)
|
2018-11-20 11:40:26 -05:00
|
|
|
{
|
2020-04-23 10:23:46 -04:00
|
|
|
char *desc;
|
2018-11-20 11:40:26 -05:00
|
|
|
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
|
|
|
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
|
|
|
|
2020-04-23 10:23:46 -04:00
|
|
|
esp_netif_inherent_config_t esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_WIFI_STA();
|
|
|
|
// Prefix the interface description with the module TAG
|
|
|
|
// Warning: the interface desc is used in tests to capture actual connection details (IP, gw, mask)
|
|
|
|
asprintf(&desc, "%s: %s", TAG, esp_netif_config.if_desc);
|
|
|
|
esp_netif_config.if_desc = desc;
|
|
|
|
esp_netif_config.route_prio = 128;
|
|
|
|
esp_netif_t *netif = esp_netif_create_wifi(WIFI_IF_STA, &esp_netif_config);
|
|
|
|
free(desc);
|
2019-09-15 13:49:45 -04:00
|
|
|
esp_wifi_set_default_wifi_sta_handlers();
|
2019-08-31 10:08:46 -04:00
|
|
|
|
2018-11-20 11:40:26 -05:00
|
|
|
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &on_wifi_disconnect, NULL));
|
|
|
|
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &on_got_ip, NULL));
|
|
|
|
#ifdef CONFIG_EXAMPLE_CONNECT_IPV6
|
2019-08-31 10:08:46 -04:00
|
|
|
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &on_wifi_connect, netif));
|
2018-11-20 11:40:26 -05:00
|
|
|
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, &on_got_ipv6, NULL));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
|
|
|
|
wifi_config_t wifi_config = {
|
|
|
|
.sta = {
|
|
|
|
.ssid = CONFIG_EXAMPLE_WIFI_SSID,
|
|
|
|
.password = CONFIG_EXAMPLE_WIFI_PASSWORD,
|
2021-02-25 06:47:53 -05:00
|
|
|
.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,
|
2018-11-20 11:40:26 -05:00
|
|
|
},
|
|
|
|
};
|
|
|
|
ESP_LOGI(TAG, "Connecting to %s...", wifi_config.sta.ssid);
|
|
|
|
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
2021-01-18 22:55:44 -05:00
|
|
|
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
|
2018-11-20 11:40:26 -05:00
|
|
|
ESP_ERROR_CHECK(esp_wifi_start());
|
2021-01-11 01:35:29 -05:00
|
|
|
esp_wifi_connect();
|
2020-04-23 10:23:46 -04:00
|
|
|
return netif;
|
2018-11-20 11:40:26 -05:00
|
|
|
}
|
|
|
|
|
2020-04-23 10:23:46 -04:00
|
|
|
static void wifi_stop(void)
|
2018-11-20 11:40:26 -05:00
|
|
|
{
|
2020-04-23 10:23:46 -04:00
|
|
|
esp_netif_t *wifi_netif = get_example_netif_from_desc("sta");
|
2018-11-20 11:40:26 -05:00
|
|
|
ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &on_wifi_disconnect));
|
|
|
|
ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, &on_got_ip));
|
|
|
|
#ifdef CONFIG_EXAMPLE_CONNECT_IPV6
|
|
|
|
ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_GOT_IP6, &on_got_ipv6));
|
|
|
|
ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &on_wifi_connect));
|
|
|
|
#endif
|
2019-10-07 10:46:25 -04:00
|
|
|
esp_err_t err = esp_wifi_stop();
|
|
|
|
if (err == ESP_ERR_WIFI_NOT_INIT) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ESP_ERROR_CHECK(err);
|
2018-11-20 11:40:26 -05:00
|
|
|
ESP_ERROR_CHECK(esp_wifi_deinit());
|
2020-04-23 10:23:46 -04:00
|
|
|
ESP_ERROR_CHECK(esp_wifi_clear_default_wifi_driver_and_handlers(wifi_netif));
|
|
|
|
esp_netif_destroy(wifi_netif);
|
2019-08-31 10:08:46 -04:00
|
|
|
s_example_esp_netif = NULL;
|
2018-11-20 11:40:26 -05:00
|
|
|
}
|
|
|
|
#endif // CONFIG_EXAMPLE_CONNECT_WIFI
|
|
|
|
|
|
|
|
#ifdef CONFIG_EXAMPLE_CONNECT_ETHERNET
|
|
|
|
|
|
|
|
#ifdef CONFIG_EXAMPLE_CONNECT_IPV6
|
|
|
|
|
|
|
|
/** Event handler for Ethernet events */
|
2019-08-31 10:08:46 -04:00
|
|
|
static void on_eth_event(void *esp_netif, esp_event_base_t event_base,
|
2019-04-10 04:24:50 -04:00
|
|
|
int32_t event_id, void *event_data)
|
2018-11-20 11:40:26 -05:00
|
|
|
{
|
|
|
|
switch (event_id) {
|
|
|
|
case ETHERNET_EVENT_CONNECTED:
|
|
|
|
ESP_LOGI(TAG, "Ethernet Link Up");
|
2021-08-03 07:34:52 -04:00
|
|
|
ESP_ERROR_CHECK(esp_netif_create_ip6_linklocal(esp_netif));
|
2018-11-20 11:40:26 -05:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // CONFIG_EXAMPLE_CONNECT_IPV6
|
|
|
|
|
2019-04-10 04:24:50 -04:00
|
|
|
static esp_eth_handle_t s_eth_handle = NULL;
|
|
|
|
static esp_eth_mac_t *s_mac = NULL;
|
|
|
|
static esp_eth_phy_t *s_phy = NULL;
|
2021-08-03 07:34:52 -04:00
|
|
|
static esp_eth_netif_glue_handle_t s_eth_glue = NULL;
|
2019-04-10 04:24:50 -04:00
|
|
|
|
2020-11-06 03:06:20 -05:00
|
|
|
static esp_netif_t *eth_start(void)
|
2018-11-20 11:40:26 -05:00
|
|
|
{
|
2020-04-23 10:23:46 -04:00
|
|
|
char *desc;
|
|
|
|
esp_netif_inherent_config_t esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_ETH();
|
|
|
|
// Prefix the interface description with the module TAG
|
|
|
|
// Warning: the interface desc is used in tests to capture actual connection details (IP, gw, mask)
|
|
|
|
asprintf(&desc, "%s: %s", TAG, esp_netif_config.if_desc);
|
|
|
|
esp_netif_config.if_desc = desc;
|
|
|
|
esp_netif_config.route_prio = 64;
|
|
|
|
esp_netif_config_t netif_config = {
|
2020-11-06 03:06:20 -05:00
|
|
|
.base = &esp_netif_config,
|
|
|
|
.stack = ESP_NETIF_NETSTACK_DEFAULT_ETH
|
2020-04-23 10:23:46 -04:00
|
|
|
};
|
2019-11-26 04:48:38 -05:00
|
|
|
esp_netif_t *netif = esp_netif_new(&netif_config);
|
2019-08-31 10:08:46 -04:00
|
|
|
assert(netif);
|
2020-04-23 10:23:46 -04:00
|
|
|
free(desc);
|
2021-08-03 07:34:52 -04:00
|
|
|
|
2019-04-10 04:24:50 -04:00
|
|
|
eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
|
|
|
|
eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
|
2019-11-13 23:03:14 -05:00
|
|
|
phy_config.phy_addr = CONFIG_EXAMPLE_ETH_PHY_ADDR;
|
|
|
|
phy_config.reset_gpio_num = CONFIG_EXAMPLE_ETH_PHY_RST_GPIO;
|
2019-06-25 07:36:56 -04:00
|
|
|
#if CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET
|
2019-11-13 23:03:14 -05:00
|
|
|
mac_config.smi_mdc_gpio_num = CONFIG_EXAMPLE_ETH_MDC_GPIO;
|
|
|
|
mac_config.smi_mdio_gpio_num = CONFIG_EXAMPLE_ETH_MDIO_GPIO;
|
2019-06-25 07:36:56 -04:00
|
|
|
s_mac = esp_eth_mac_new_esp32(&mac_config);
|
2019-04-10 04:24:50 -04:00
|
|
|
#if CONFIG_EXAMPLE_ETH_PHY_IP101
|
|
|
|
s_phy = esp_eth_phy_new_ip101(&phy_config);
|
|
|
|
#elif CONFIG_EXAMPLE_ETH_PHY_RTL8201
|
|
|
|
s_phy = esp_eth_phy_new_rtl8201(&phy_config);
|
2021-06-24 05:03:11 -04:00
|
|
|
#elif CONFIG_EXAMPLE_ETH_PHY_LAN87XX
|
|
|
|
s_phy = esp_eth_phy_new_lan87xx(&phy_config);
|
2019-04-10 04:24:50 -04:00
|
|
|
#elif CONFIG_EXAMPLE_ETH_PHY_DP83848
|
|
|
|
s_phy = esp_eth_phy_new_dp83848(&phy_config);
|
2021-11-16 10:18:43 -05:00
|
|
|
#elif CONFIG_EXAMPLE_ETH_PHY_KSZ80XX
|
|
|
|
s_phy = esp_eth_phy_new_ksz80xx(&phy_config);
|
2019-06-25 07:36:56 -04:00
|
|
|
#endif
|
2021-08-15 12:58:42 -04:00
|
|
|
#elif CONFIG_EXAMPLE_USE_SPI_ETHERNET
|
2019-06-25 07:36:56 -04:00
|
|
|
gpio_install_isr_service(0);
|
|
|
|
spi_device_handle_t spi_handle = NULL;
|
|
|
|
spi_bus_config_t buscfg = {
|
2020-11-06 03:06:20 -05:00
|
|
|
.miso_io_num = CONFIG_EXAMPLE_ETH_SPI_MISO_GPIO,
|
|
|
|
.mosi_io_num = CONFIG_EXAMPLE_ETH_SPI_MOSI_GPIO,
|
|
|
|
.sclk_io_num = CONFIG_EXAMPLE_ETH_SPI_SCLK_GPIO,
|
2019-06-25 07:36:56 -04:00
|
|
|
.quadwp_io_num = -1,
|
|
|
|
.quadhd_io_num = -1,
|
|
|
|
};
|
2020-11-06 03:06:20 -05:00
|
|
|
ESP_ERROR_CHECK(spi_bus_initialize(CONFIG_EXAMPLE_ETH_SPI_HOST, &buscfg, 1));
|
|
|
|
#if CONFIG_EXAMPLE_USE_DM9051
|
2019-06-25 07:36:56 -04:00
|
|
|
spi_device_interface_config_t devcfg = {
|
|
|
|
.command_bits = 1,
|
|
|
|
.address_bits = 7,
|
|
|
|
.mode = 0,
|
2020-11-06 03:06:20 -05:00
|
|
|
.clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000,
|
|
|
|
.spics_io_num = CONFIG_EXAMPLE_ETH_SPI_CS_GPIO,
|
2019-06-25 07:36:56 -04:00
|
|
|
.queue_size = 20
|
|
|
|
};
|
2020-11-06 03:06:20 -05:00
|
|
|
ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_EXAMPLE_ETH_SPI_HOST, &devcfg, &spi_handle));
|
2019-09-18 06:28:55 -04:00
|
|
|
/* dm9051 ethernet driver is based on spi driver */
|
|
|
|
eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(spi_handle);
|
2020-11-06 03:06:20 -05:00
|
|
|
dm9051_config.int_gpio_num = CONFIG_EXAMPLE_ETH_SPI_INT_GPIO;
|
2019-09-18 06:28:55 -04:00
|
|
|
s_mac = esp_eth_mac_new_dm9051(&dm9051_config, &mac_config);
|
2019-06-25 07:36:56 -04:00
|
|
|
s_phy = esp_eth_phy_new_dm9051(&phy_config);
|
2020-11-06 03:06:20 -05:00
|
|
|
#elif CONFIG_EXAMPLE_USE_W5500
|
|
|
|
spi_device_interface_config_t devcfg = {
|
|
|
|
.command_bits = 16, // Actually it's the address phase in W5500 SPI frame
|
|
|
|
.address_bits = 8, // Actually it's the control phase in W5500 SPI frame
|
|
|
|
.mode = 0,
|
|
|
|
.clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000,
|
|
|
|
.spics_io_num = CONFIG_EXAMPLE_ETH_SPI_CS_GPIO,
|
|
|
|
.queue_size = 20
|
|
|
|
};
|
|
|
|
ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_EXAMPLE_ETH_SPI_HOST, &devcfg, &spi_handle));
|
|
|
|
/* w5500 ethernet driver is based on spi driver */
|
|
|
|
eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(spi_handle);
|
|
|
|
w5500_config.int_gpio_num = CONFIG_EXAMPLE_ETH_SPI_INT_GPIO;
|
|
|
|
s_mac = esp_eth_mac_new_w5500(&w5500_config, &mac_config);
|
|
|
|
s_phy = esp_eth_phy_new_w5500(&phy_config);
|
|
|
|
#endif
|
2019-10-01 12:50:34 -04:00
|
|
|
#elif CONFIG_EXAMPLE_USE_OPENETH
|
|
|
|
phy_config.autonego_timeout_ms = 100;
|
|
|
|
s_mac = esp_eth_mac_new_openeth(&mac_config);
|
|
|
|
s_phy = esp_eth_phy_new_dp83848(&phy_config);
|
2019-04-10 04:24:50 -04:00
|
|
|
#endif
|
2019-10-01 12:50:34 -04:00
|
|
|
|
2019-11-26 04:48:38 -05:00
|
|
|
// Install Ethernet driver
|
2019-04-10 04:24:50 -04:00
|
|
|
esp_eth_config_t config = ETH_DEFAULT_CONFIG(s_mac, s_phy);
|
|
|
|
ESP_ERROR_CHECK(esp_eth_driver_install(&config, &s_eth_handle));
|
2020-11-06 03:06:20 -05:00
|
|
|
#if !CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET
|
|
|
|
/* The SPI Ethernet module might doesn't have a burned factory MAC address, we cat to set it manually.
|
|
|
|
02:00:00 is a Locally Administered OUI range so should not be used except when testing on a LAN under your control.
|
|
|
|
*/
|
|
|
|
ESP_ERROR_CHECK(esp_eth_ioctl(s_eth_handle, ETH_CMD_S_MAC_ADDR, (uint8_t[]) {
|
|
|
|
0x02, 0x00, 0x00, 0x12, 0x34, 0x56
|
|
|
|
}));
|
|
|
|
#endif
|
2019-11-26 04:48:38 -05:00
|
|
|
// combine driver with netif
|
2020-01-13 08:34:23 -05:00
|
|
|
s_eth_glue = esp_eth_new_netif_glue(s_eth_handle);
|
|
|
|
esp_netif_attach(netif, s_eth_glue);
|
2021-08-03 07:34:52 -04:00
|
|
|
|
|
|
|
// Register user defined event handers
|
|
|
|
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &on_got_ip, NULL));
|
|
|
|
#ifdef CONFIG_EXAMPLE_CONNECT_IPV6
|
|
|
|
ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_CONNECTED, &on_eth_event, netif));
|
|
|
|
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, &on_got_ipv6, NULL));
|
|
|
|
#endif
|
|
|
|
|
2019-11-26 04:48:38 -05:00
|
|
|
esp_eth_start(s_eth_handle);
|
2020-04-23 10:23:46 -04:00
|
|
|
return netif;
|
2018-11-20 11:40:26 -05:00
|
|
|
}
|
|
|
|
|
2020-04-23 10:23:46 -04:00
|
|
|
static void eth_stop(void)
|
2018-11-20 11:40:26 -05:00
|
|
|
{
|
2020-04-23 10:23:46 -04:00
|
|
|
esp_netif_t *eth_netif = get_example_netif_from_desc("eth");
|
2018-11-20 11:40:26 -05:00
|
|
|
ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_ETH_GOT_IP, &on_got_ip));
|
|
|
|
#ifdef CONFIG_EXAMPLE_CONNECT_IPV6
|
|
|
|
ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_GOT_IP6, &on_got_ipv6));
|
|
|
|
ESP_ERROR_CHECK(esp_event_handler_unregister(ETH_EVENT, ETHERNET_EVENT_CONNECTED, &on_eth_event));
|
|
|
|
#endif
|
2019-12-03 02:37:34 -05:00
|
|
|
ESP_ERROR_CHECK(esp_eth_stop(s_eth_handle));
|
2020-01-13 08:34:23 -05:00
|
|
|
ESP_ERROR_CHECK(esp_eth_del_netif_glue(s_eth_glue));
|
2019-04-10 04:24:50 -04:00
|
|
|
ESP_ERROR_CHECK(esp_eth_driver_uninstall(s_eth_handle));
|
|
|
|
ESP_ERROR_CHECK(s_phy->del(s_phy));
|
|
|
|
ESP_ERROR_CHECK(s_mac->del(s_mac));
|
2019-08-31 10:08:46 -04:00
|
|
|
|
2020-04-23 10:23:46 -04:00
|
|
|
esp_netif_destroy(eth_netif);
|
2019-08-31 10:08:46 -04:00
|
|
|
s_example_esp_netif = NULL;
|
2018-11-20 11:40:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // CONFIG_EXAMPLE_CONNECT_ETHERNET
|
2019-08-31 10:08:46 -04:00
|
|
|
|
|
|
|
esp_netif_t *get_example_netif(void)
|
|
|
|
{
|
|
|
|
return s_example_esp_netif;
|
2019-11-13 23:03:14 -05:00
|
|
|
}
|
2020-04-23 10:23:46 -04:00
|
|
|
|
|
|
|
esp_netif_t *get_example_netif_from_desc(const char *desc)
|
|
|
|
{
|
|
|
|
esp_netif_t *netif = NULL;
|
|
|
|
char *expected_desc;
|
|
|
|
asprintf(&expected_desc, "%s: %s", TAG, desc);
|
|
|
|
while ((netif = esp_netif_next(netif)) != NULL) {
|
|
|
|
if (strcmp(esp_netif_get_desc(netif), expected_desc) == 0) {
|
|
|
|
free(expected_desc);
|
|
|
|
return netif;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(expected_desc);
|
|
|
|
return netif;
|
|
|
|
}
|