2021-12-02 05:19:28 -05:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2019-2022 Espressif Systems (Shanghai) CO LTD
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2019-06-28 10:47:34 -04:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include "esp_netif.h"
|
|
|
|
#include "esp_event.h"
|
|
|
|
#include "esp_err.h"
|
|
|
|
#include "esp_log.h"
|
|
|
|
#include "esp_netif_private.h"
|
|
|
|
|
|
|
|
//
|
|
|
|
// Purpose of this module is to define general event handler primitives
|
|
|
|
// enabling application code to them either directly or with minimal modification
|
|
|
|
// for example with a separate pre/post handler.
|
|
|
|
// This module has no dependency on a specific network stack (lwip)
|
|
|
|
//
|
|
|
|
|
|
|
|
static const char *TAG = "esp_netif_handlers";
|
|
|
|
|
|
|
|
void esp_netif_action_start(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data)
|
|
|
|
{
|
|
|
|
ESP_LOGD(TAG, "esp_netif action has started with netif%p from event_id=%d", esp_netif, event_id);
|
|
|
|
esp_netif_start(esp_netif);
|
|
|
|
}
|
|
|
|
|
|
|
|
void esp_netif_action_stop(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data)
|
|
|
|
{
|
|
|
|
ESP_LOGD(TAG, "esp_netif action stopped with netif%p from event_id=%d", esp_netif, event_id);
|
|
|
|
esp_netif_stop(esp_netif);
|
|
|
|
}
|
|
|
|
|
|
|
|
void esp_netif_action_connected(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data)
|
|
|
|
{
|
|
|
|
esp_netif_dhcp_status_t status;
|
|
|
|
|
|
|
|
ESP_LOGD(TAG, "esp_netif action connected with netif%p from event_id=%d", esp_netif, event_id);
|
|
|
|
esp_netif_up(esp_netif);
|
|
|
|
|
2020-04-30 08:13:51 -04:00
|
|
|
if (!(esp_netif_get_flags(esp_netif) & ESP_NETIF_DHCP_CLIENT)) {
|
|
|
|
// No more actions for interfaces without DHCP client flag
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ESP_NETIF_CALL_CHECK("connected action: dhcpc failed", esp_netif_dhcpc_get_status(esp_netif, &status), ESP_OK);
|
2019-06-28 10:47:34 -04:00
|
|
|
if (status == ESP_NETIF_DHCP_INIT) {
|
|
|
|
esp_netif_dhcpc_start(esp_netif);
|
|
|
|
} else if (status == ESP_NETIF_DHCP_STOPPED) {
|
|
|
|
//
|
|
|
|
esp_netif_ip_info_t ip;
|
|
|
|
esp_netif_ip_info_t old_ip;
|
|
|
|
|
|
|
|
esp_netif_get_ip_info(esp_netif, &ip);
|
|
|
|
esp_netif_get_old_ip_info(esp_netif, &old_ip);
|
|
|
|
|
|
|
|
if (esp_netif_is_valid_static_ip(&ip)) {
|
|
|
|
ip_event_got_ip_t evt = {
|
|
|
|
.esp_netif = esp_netif,
|
|
|
|
.if_index = -1, // to indicate ptr to if used
|
|
|
|
.ip_changed = false,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (memcmp(&ip, &old_ip, sizeof(ip))) {
|
|
|
|
evt.ip_changed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&evt.ip_info, &ip, sizeof(esp_netif_ip_info_t));
|
|
|
|
esp_netif_set_old_ip_info(esp_netif, &ip);
|
|
|
|
|
2021-12-02 05:19:28 -05:00
|
|
|
ESP_NETIF_CALL_CHECK("esp_event_post in esp_netif_action_connected",
|
|
|
|
esp_event_post(IP_EVENT, esp_netif_get_event_id(esp_netif, ESP_NETIF_IP_EVENT_GOT_IP) ,
|
2019-06-28 10:47:34 -04:00
|
|
|
&evt, sizeof(evt), 0), ESP_OK);
|
|
|
|
ESP_LOGD(TAG, "static ip: ip changed=%d", evt.ip_changed);
|
|
|
|
} else {
|
|
|
|
ESP_LOGE(TAG, "invalid static ip");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void esp_netif_action_disconnected(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data)
|
|
|
|
{
|
2019-09-04 07:58:29 -04:00
|
|
|
ESP_LOGD(TAG, "esp_netif action disconnected with netif%p from event_id=%d", esp_netif, event_id);
|
2019-06-28 10:47:34 -04:00
|
|
|
esp_netif_down(esp_netif);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void esp_netif_action_got_ip(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data)
|
|
|
|
{
|
|
|
|
ESP_LOGD(TAG, "esp_netif action got_ip with netif%p from event_id=%d", esp_netif, event_id);
|
|
|
|
const ip_event_got_ip_t *event = (const ip_event_got_ip_t *) data;
|
2019-09-13 09:44:23 -04:00
|
|
|
ESP_LOGI(TAG, "%s ip: " IPSTR ", mask: " IPSTR ", gw: " IPSTR, esp_netif_get_desc(esp_netif),
|
2019-06-28 10:47:34 -04:00
|
|
|
IP2STR(&event->ip_info.ip),
|
|
|
|
IP2STR(&event->ip_info.netmask),
|
|
|
|
IP2STR(&event->ip_info.gw));
|
|
|
|
}
|
2021-05-18 23:52:51 -04:00
|
|
|
|
|
|
|
void esp_netif_action_join_ip6_multicast_group(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data)
|
|
|
|
{
|
|
|
|
ESP_LOGD(TAG, "esp_netif action join_ip6_multicast group with netif%p from event_id=%d", esp_netif, event_id);
|
|
|
|
const esp_ip6_addr_t *addr = (const esp_ip6_addr_t *)data;
|
|
|
|
esp_netif_join_ip6_multicast_group(esp_netif, addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void esp_netif_action_leave_ip6_multicast_group(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data)
|
|
|
|
{
|
|
|
|
ESP_LOGD(TAG, "esp_netif action leave_ip6_multicast_group with netif%p from event_id=%d", esp_netif, event_id);
|
|
|
|
const esp_ip6_addr_t *addr = (const esp_ip6_addr_t *)data;
|
|
|
|
esp_netif_leave_ip6_multicast_group(esp_netif, addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void esp_netif_action_add_ip6_address(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data)
|
|
|
|
{
|
|
|
|
ESP_LOGD(TAG, "esp_netif action add_ip6_address with netif%p from event_id=%d", esp_netif, event_id);
|
|
|
|
const ip_event_add_ip6_t *addr = (const ip_event_add_ip6_t *)data;
|
|
|
|
esp_netif_add_ip6_address(esp_netif, addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void esp_netif_action_remove_ip6_address(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data)
|
|
|
|
{
|
|
|
|
ESP_LOGD(TAG, "esp_netif action remove_ip6_address with netif%p from event_id=%d", esp_netif, event_id);
|
|
|
|
const esp_ip6_addr_t *addr = (const esp_ip6_addr_t *)data;
|
|
|
|
esp_netif_remove_ip6_address(esp_netif, addr);
|
|
|
|
}
|