1 // Copyright 2019 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <string.h>
16 #include "esp_netif.h"
17 #include "esp_event.h"
18 #include "esp_err.h"
19 #include "esp_log.h"
20 #include "esp_netif_private.h"
21
22 //
23 // Purpose of this module is to define general event handler primitives
24 // enabling application code to them either directly or with minimal modification
25 // for example with a separate pre/post handler.
26 // This module has no dependency on a specific network stack (lwip)
27 //
28
29 static const char *TAG = "esp_netif_handlers";
30
esp_netif_action_start(void * esp_netif,esp_event_base_t base,int32_t event_id,void * data)31 void esp_netif_action_start(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data)
32 {
33 ESP_LOGD(TAG, "esp_netif action has started with netif%p from event_id=%d", esp_netif, event_id);
34 esp_netif_start(esp_netif);
35 }
36
esp_netif_action_stop(void * esp_netif,esp_event_base_t base,int32_t event_id,void * data)37 void esp_netif_action_stop(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data)
38 {
39 ESP_LOGD(TAG, "esp_netif action stopped with netif%p from event_id=%d", esp_netif, event_id);
40 esp_netif_stop(esp_netif);
41 }
42
esp_netif_action_connected(void * esp_netif,esp_event_base_t base,int32_t event_id,void * data)43 void esp_netif_action_connected(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data)
44 {
45 esp_netif_dhcp_status_t status;
46
47 ESP_LOGD(TAG, "esp_netif action connected with netif%p from event_id=%d", esp_netif, event_id);
48 esp_netif_up(esp_netif);
49
50 if (!(esp_netif_get_flags(esp_netif) & ESP_NETIF_DHCP_CLIENT)) {
51 // No more actions for interfaces without DHCP client flag
52 return;
53 }
54
55 ESP_NETIF_CALL_CHECK("connected action: dhcpc failed", esp_netif_dhcpc_get_status(esp_netif, &status), ESP_OK);
56 if (status == ESP_NETIF_DHCP_INIT) {
57 esp_netif_dhcpc_start(esp_netif);
58 } else if (status == ESP_NETIF_DHCP_STOPPED) {
59 //
60 esp_netif_ip_info_t ip;
61 esp_netif_ip_info_t old_ip;
62
63 esp_netif_get_ip_info(esp_netif, &ip);
64 esp_netif_get_old_ip_info(esp_netif, &old_ip);
65
66 if (esp_netif_is_valid_static_ip(&ip)) {
67 ip_event_got_ip_t evt = {
68 .esp_netif = esp_netif,
69 .if_index = -1, // to indicate ptr to if used
70 .ip_changed = false,
71 };
72
73 if (memcmp(&ip, &old_ip, sizeof(ip))) {
74 evt.ip_changed = true;
75 }
76
77 memcpy(&evt.ip_info, &ip, sizeof(esp_netif_ip_info_t));
78 esp_netif_set_old_ip_info(esp_netif, &ip);
79
80 ESP_NETIF_CALL_CHECK("esp_event_send_internal in esp_netif_action_connected",
81 esp_event_send_internal(IP_EVENT, esp_netif_get_event_id(esp_netif, ESP_NETIF_IP_EVENT_GOT_IP) ,
82 &evt, sizeof(evt), 0), ESP_OK);
83 ESP_LOGD(TAG, "static ip: ip changed=%d", evt.ip_changed);
84 } else {
85 ESP_LOGE(TAG, "invalid static ip");
86 }
87 }
88 }
89
esp_netif_action_disconnected(void * esp_netif,esp_event_base_t base,int32_t event_id,void * data)90 void esp_netif_action_disconnected(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data)
91 {
92 ESP_LOGD(TAG, "esp_netif action disconnected with netif%p from event_id=%d", esp_netif, event_id);
93 esp_netif_down(esp_netif);
94
95 }
96
esp_netif_action_got_ip(void * esp_netif,esp_event_base_t base,int32_t event_id,void * data)97 void esp_netif_action_got_ip(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data)
98 {
99 ESP_LOGD(TAG, "esp_netif action got_ip with netif%p from event_id=%d", esp_netif, event_id);
100 const ip_event_got_ip_t *event = (const ip_event_got_ip_t *) data;
101 ESP_LOGI(TAG, "%s ip: " IPSTR ", mask: " IPSTR ", gw: " IPSTR, esp_netif_get_desc(esp_netif),
102 IP2STR(&event->ip_info.ip),
103 IP2STR(&event->ip_info.netmask),
104 IP2STR(&event->ip_info.gw));
105 }
106
esp_netif_action_join_ip6_multicast_group(void * esp_netif,esp_event_base_t base,int32_t event_id,void * data)107 void esp_netif_action_join_ip6_multicast_group(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data)
108 {
109 ESP_LOGD(TAG, "esp_netif action join_ip6_multicast group with netif%p from event_id=%d", esp_netif, event_id);
110 const esp_ip6_addr_t *addr = (const esp_ip6_addr_t *)data;
111 esp_netif_join_ip6_multicast_group(esp_netif, addr);
112 }
113
esp_netif_action_leave_ip6_multicast_group(void * esp_netif,esp_event_base_t base,int32_t event_id,void * data)114 void esp_netif_action_leave_ip6_multicast_group(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data)
115 {
116 ESP_LOGD(TAG, "esp_netif action leave_ip6_multicast_group with netif%p from event_id=%d", esp_netif, event_id);
117 const esp_ip6_addr_t *addr = (const esp_ip6_addr_t *)data;
118 esp_netif_leave_ip6_multicast_group(esp_netif, addr);
119 }
120
esp_netif_action_add_ip6_address(void * esp_netif,esp_event_base_t base,int32_t event_id,void * data)121 void esp_netif_action_add_ip6_address(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data)
122 {
123 ESP_LOGD(TAG, "esp_netif action add_ip6_address with netif%p from event_id=%d", esp_netif, event_id);
124 const ip_event_add_ip6_t *addr = (const ip_event_add_ip6_t *)data;
125 esp_netif_add_ip6_address(esp_netif, addr);
126 }
127
esp_netif_action_remove_ip6_address(void * esp_netif,esp_event_base_t base,int32_t event_id,void * data)128 void esp_netif_action_remove_ip6_address(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data)
129 {
130 ESP_LOGD(TAG, "esp_netif action remove_ip6_address with netif%p from event_id=%d", esp_netif, event_id);
131 const esp_ip6_addr_t *addr = (const esp_ip6_addr_t *)data;
132 esp_netif_remove_ip6_address(esp_netif, addr);
133 }
134