forked from ruuvi/ruuvi.gateway_esp.c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ethernet.c
380 lines (334 loc) · 10.1 KB
/
ethernet.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/**
* @file ethernet.c
* @author Jukka Saari
* @date 2020-01-27
* @copyright Ruuvi Innovations Ltd, license BSD-3-Clause.
*/
#include "ethernet.h"
#include "driver/gpio.h"
#include "esp_err.h"
#include "esp_eth.h"
#include "esp_eth_com.h"
#include "esp_event.h"
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include "freertos/task.h"
#include "leds.h"
#include "mqtt.h"
#include "ruuvi_gateway.h"
#include "sdkconfig.h"
#include "tcpip_adapter.h"
#include "time_task.h"
#include "wifi_manager.h"
#include <stdio.h>
#include <string.h>
#define LOG_LOCAL_LEVEL LOG_LEVEL_DEBUG
#include "log.h"
#define LAN_CLOCK_ENABLE 2
#define ETH_PHY_ADDR 0
#define ETH_MDC_GPIO 23
#define ETH_MDIO_GPIO 18
#define ETH_PHY_RST_GPIO -1 // disabled
#define SW_RESET_TIMEOUT_MS 500
// Cloudfare public DNS
const char *dns_fallback_server = "1.1.1.1";
static const char *TAG = "ETH";
static esp_eth_handle_t g_eth_handle;
static ethernet_cb_link_up_t g_ethernet_link_up_cb;
static ethernet_cb_link_down_t g_ethernet_link_down_cb;
static ethernet_cb_connection_ok_t g_ethernet_connection_ok_cb;
static void
eth_on_event_connected(esp_eth_handle_t eth_handle)
{
mac_address_bin_t mac_bin = { 0 };
esp_eth_ioctl(eth_handle, ETH_CMD_G_MAC_ADDR, &mac_bin.mac[0]);
LOG_INFO("Ethernet Link Up");
const mac_address_str_t mac_str = mac_address_to_str(&mac_bin);
LOG_INFO("Ethernet HW Addr %s", mac_str.str_buf);
g_ethernet_link_up_cb();
}
/** Event handler for Ethernet events */
static void
eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
{
(void)arg;
(void)event_base;
/* we can get the ethernet driver handle from event data */
esp_eth_handle_t eth_handle = *(esp_eth_handle_t *)event_data;
switch (event_id)
{
case ETHERNET_EVENT_CONNECTED:
eth_on_event_connected(eth_handle);
break;
case ETHERNET_EVENT_DISCONNECTED:
LOG_INFO("Ethernet Link Down");
g_ethernet_link_down_cb();
break;
case ETHERNET_EVENT_START:
LOG_INFO("Ethernet Started");
break;
case ETHERNET_EVENT_STOP:
LOG_INFO("Ethernet Stopped");
break;
default:
break;
}
}
/** Event handler for IP_EVENT_ETH_GOT_IP */
static void
got_ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
{
(void)arg;
(void)event_base;
(void)event_id;
const ip_event_got_ip_t * p_event = (ip_event_got_ip_t *)event_data;
const tcpip_adapter_ip_info_t *p_ip_info = &p_event->ip_info;
LOG_INFO("Ethernet Got IP Address");
LOG_INFO("~~~~~~~~~~~");
LOG_INFO("ETH:IP:" IPSTR, IP2STR(&p_ip_info->ip));
LOG_INFO("ETH:MASK:" IPSTR, IP2STR(&p_ip_info->netmask));
LOG_INFO("ETH:GW:" IPSTR, IP2STR(&p_ip_info->gw));
LOG_INFO("~~~~~~~~~~~");
g_ethernet_connection_ok_cb(p_ip_info);
}
static bool
ethernet_update_ip_dhcp(void)
{
LOG_INFO("Using ETH DHCP");
const esp_err_t ret = tcpip_adapter_dhcpc_start(TCPIP_ADAPTER_IF_ETH);
if (ESP_OK != ret)
{
LOG_ERR_ESP(ret, "%s failed", "tcpip_adapter_dhcpc_start");
return false;
}
return true;
}
static bool
eth_tcpip_adapter_dhcpc_stop(void)
{
const esp_err_t ret = tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_ETH);
if (ESP_OK != ret)
{
if ((ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STOPPED) == ret)
{
LOG_WARN("DHCP client already stopped");
}
else
{
LOG_ERR_ESP(ret, "%s failed", "tcpip_adapter_dhcpc_stop");
return false;
}
}
return true;
}
static void
eth_tcpip_adapter_set_dns_info(const char *p_dns_ip, const tcpip_adapter_dns_type_t type)
{
const char *dns_server = "Undef";
switch (type)
{
case TCPIP_ADAPTER_DNS_MAIN:
dns_server = "DNS1";
break;
case TCPIP_ADAPTER_DNS_BACKUP:
dns_server = "DNS2";
break;
case TCPIP_ADAPTER_DNS_FALLBACK:
dns_server = "DNS_FALLBACK";
break;
default:
break;
}
tcpip_adapter_dns_info_t dns_info = {
.ip = {
.u_addr = {
.ip4 = {
.addr = 0,
},
},
.type = IPADDR_TYPE_V4,
},
};
if (0 == ip4addr_aton(p_dns_ip, &dns_info.ip.u_addr.ip4))
{
LOG_ERR("Set DNS info failed for %s, invalid server: %s", dns_server, p_dns_ip);
return;
}
const esp_err_t ret = tcpip_adapter_set_dns_info(TCPIP_ADAPTER_IF_ETH, type, &dns_info);
if (ESP_OK != ret)
{
LOG_ERR_ESP(ret, "%s failed for DNS: '%s'", "tcpip_adapter_set_dns_info", dns_server);
return;
}
}
static bool
ethernet_update_ip_static(void)
{
ruuvi_gateway_config_t *p_gw_cfg = &g_gateway_config;
tcpip_adapter_ip_info_t ip_info = { 0 };
LOG_INFO("Using static IP");
if (0 == ip4addr_aton(p_gw_cfg->eth.eth_static_ip, &ip_info.ip))
{
LOG_ERR("Invalid eth static ip: %s", p_gw_cfg->eth.eth_static_ip);
return false;
}
if (0 == ip4addr_aton(p_gw_cfg->eth.eth_netmask, &ip_info.netmask))
{
LOG_ERR("invalid eth netmask: %s", p_gw_cfg->eth.eth_netmask);
return false;
}
if (0 == ip4addr_aton(p_gw_cfg->eth.eth_gw, &ip_info.gw))
{
LOG_ERR("invalid eth gw: %s", p_gw_cfg->eth.eth_gw);
return false;
}
if (!eth_tcpip_adapter_dhcpc_stop())
{
return false;
}
const esp_err_t ret = tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_ETH, &ip_info);
if (ESP_OK != ret)
{
LOG_ERR_ESP(ret, "%s failed", "tcpip_adapter_set_ip_info");
return false;
}
eth_tcpip_adapter_set_dns_info(p_gw_cfg->eth.eth_dns1, TCPIP_ADAPTER_DNS_MAIN);
eth_tcpip_adapter_set_dns_info(p_gw_cfg->eth.eth_dns2, TCPIP_ADAPTER_DNS_BACKUP);
return true;
}
void
ethernet_update_ip(void)
{
if (g_gateway_config.eth.eth_dhcp)
{
if (!ethernet_update_ip_dhcp())
{
return;
}
}
else
{
if (!ethernet_update_ip_static())
{
return;
}
}
// set DNS fallback also for DHCP settings
eth_tcpip_adapter_set_dns_info(dns_fallback_server, TCPIP_ADAPTER_DNS_FALLBACK);
LOG_INFO("ETH ip updated");
}
bool
ethernet_init(
ethernet_cb_link_up_t ethernet_link_up_cb,
ethernet_cb_link_down_t ethernet_link_down_cb,
ethernet_cb_connection_ok_t ethernet_connection_ok_cb)
{
LOG_INFO("Ethernet init");
g_ethernet_link_up_cb = ethernet_link_up_cb;
g_ethernet_link_down_cb = ethernet_link_down_cb;
g_ethernet_connection_ok_cb = ethernet_connection_ok_cb;
gpio_set_direction(LAN_CLOCK_ENABLE, GPIO_MODE_OUTPUT);
gpio_set_level(LAN_CLOCK_ENABLE, 1);
ethernet_update_ip();
tcpip_adapter_init();
esp_err_t err_code = tcpip_adapter_set_default_eth_handlers();
if (ESP_OK != err_code)
{
LOG_ERR_ESP(err_code, "%s failed", "tcpip_adapter_set_default_eth_handlers");
return false;
}
err_code = esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, ð_event_handler, NULL);
if (ESP_OK != err_code)
{
LOG_ERR_ESP(err_code, "%s failed", "esp_event_handler_register");
return false;
}
err_code = esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, NULL);
if (ESP_OK != err_code)
{
LOG_ERR_ESP(err_code, "%s failed", "esp_event_handler_register");
return false;
}
eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
phy_config.phy_addr = ETH_PHY_ADDR;
phy_config.reset_gpio_num = ETH_PHY_RST_GPIO;
mac_config.smi_mdc_gpio_num = ETH_MDC_GPIO;
mac_config.smi_mdio_gpio_num = ETH_MDIO_GPIO;
mac_config.sw_reset_timeout_ms = SW_RESET_TIMEOUT_MS;
esp_eth_mac_t * mac = esp_eth_mac_new_esp32(&mac_config);
esp_eth_phy_t * phy = esp_eth_phy_new_lan8720(&phy_config);
esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy);
err_code = esp_eth_driver_install(&config, &g_eth_handle);
if (ESP_OK != err_code)
{
LOG_ERR_ESP(err_code, "Ethernet driver install failed");
return false;
}
return true;
}
void
ethernet_deinit(void)
{
if (NULL == g_eth_handle)
{
return;
}
LOG_INFO("Ethernet deinit");
esp_err_t err_code = esp_eth_stop(g_eth_handle);
if (ESP_OK != err_code)
{
LOG_ERR_ESP(err_code, "Ethernet stop failed");
}
err_code = esp_eth_driver_uninstall(g_eth_handle);
g_eth_handle = NULL;
if (ESP_OK != err_code)
{
LOG_ERR_ESP(err_code, "Ethernet driver uninstall failed");
}
err_code = esp_event_handler_unregister(ETH_EVENT, ESP_EVENT_ANY_ID, ð_event_handler);
if (ESP_OK != err_code)
{
LOG_ERR_ESP(err_code, "Ethernet event handler unregister failed: %s", "ESP_EVENT_ANY_ID");
}
esp_event_handler_unregister(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler);
if (ESP_OK != err_code)
{
LOG_ERR_ESP(err_code, "Ethernet event handler unregister failed: %s", "IP_EVENT_ETH_GOT_IP");
}
tcpip_adapter_clear_default_eth_handlers();
}
void
ethernet_start(const char *const hostname)
{
if (NULL == g_eth_handle)
{
return;
}
LOG_INFO("Ethernet start");
esp_err_t err = esp_eth_start(g_eth_handle);
if (ESP_OK != err)
{
LOG_ERR_ESP(err, "Ethernet start failed");
}
LOG_INFO("Set hostname for Ethernet interface: %s", hostname);
err = tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_ETH, hostname);
if (ESP_OK != err)
{
LOG_ERR_ESP(err, "%s failed", "tcpip_adapter_set_hostname");
}
}
void
ethernet_stop(void)
{
if (NULL == g_eth_handle)
{
return;
}
LOG_INFO("Ethernet stop");
esp_err_t err_code = esp_eth_stop(g_eth_handle);
if (ESP_OK != err_code)
{
LOG_ERR_ESP(err_code, "Ethernet stop failed");
}
}