added dns function check

This commit is contained in:
janik
2021-09-28 08:58:15 +02:00
parent a0a65b63f5
commit dd926981a7
3 changed files with 60 additions and 8 deletions

View File

@ -18,6 +18,9 @@
#include "driver/gpio.h"
#include <driver/adc.h>
#include "sdkconfig.h"
#include "lwip/inet.h"
#include "lwip/netdb.h"
#include "lwip/sockets.h"
#include "ssd1306.h"
#include "font8x8_basic.h"
@ -46,7 +49,7 @@ esp_ip4_addr_t ip_snm;
esp_ip4_addr_t ip_gw;
esp_netif_t *eth_netif;
bool dns_resolv = false;
int dns_resolv = -1;
bool blink_mod = false;
@ -57,16 +60,28 @@ static uint8_t get_byte(uint32_t addr, int n)
#define IP4ZCHK(ipaddr) (esp_ip4_addr1_16(ipaddr) | esp_ip4_addr2_16(ipaddr) | esp_ip4_addr3_16(ipaddr) | esp_ip4_addr4_16(ipaddr))
static bool check_dns()
static int check_dns()
{
esp_netif_dns_info_t dns_info;
ESP_ERROR_CHECK(esp_netif_get_dns_info(eth_netif, ESP_NETIF_DNS_MAIN, &dns_info));
// ESP_LOGI(TAG, IP_FSTRING, IP2STR(&dns_info.ip.u_addr.ip4));
// ESP_LOGI(TAG, "%d", IP4ZCHK(&dns_info.ip.u_addr.ip4));
if (IP4ZCHK(&dns_info.ip.u_addr.ip4) != 0)
return true;
return false;
return 1;
return 0;
}
static void check_resolution(void *arg)
{
struct addrinfo hint;
struct addrinfo *res = NULL;
memset(&hint, 0, sizeof(hint));
while (1) {
dns_resolv = getaddrinfo("www.google.de", NULL, &hint, &res);
vTaskDelay(250 / portTICK_RATE_MS);
}
}
static void refresh_display()
@ -136,8 +151,10 @@ static void refresh_display()
bool poe = (adc1_get_raw(ADC1_CHANNEL_3) > 2048); //arbitrary value, battery power is ~800
bool eth = (eth_state == ETHERNET_EVENT_CONNECTED);
bool addr = (ip_addr.addr != 0) & eth;
bool dns = ((check_dns()) && addr && blink_mod) || dns_resolv;
bool dns = ((check_dns()) && addr && blink_mod) || (dns_resolv == 0);
ssd1306_clear_line(&display, LINE_STATUS_FLAGS3, false);
ssd1306_display_text(&display, LINE_STATUS_FLAGS3, "POE", 3, poe, 13);
ssd1306_display_text(&display, LINE_STATUS_FLAGS3, "LNK", 3, eth, 9);
ssd1306_display_text(&display, LINE_STATUS_FLAGS3, "DHCP", 4, addr, 4);
@ -162,12 +179,14 @@ static void eth_event_handler(void *arg, esp_event_base_t event_base,
break;
case ETHERNET_EVENT_DISCONNECTED:
ESP_LOGI(TAG, "Link Down");
dns_resolv = -1;
break;
case ETHERNET_EVENT_START:
ESP_LOGI(TAG, "Ethernet Started");
break;
case ETHERNET_EVENT_STOP:
ESP_LOGI(TAG, "Ethernet Stopped");
dns_resolv = -1;
break;
default:
break;
@ -222,6 +241,35 @@ static void task_signals(void* arg)
}
}
// void icmp_ping(in_addr_t ip)
// {
// /* convert URL to IP address */
// ip_addr_t target_addr;
// struct addrinfo hint;
// struct addrinfo *res = NULL;
// memset(&hint, 0, sizeof(hint));
// memset(&target_addr, 0, sizeof(target_addr));
// getaddrinfo("www.espressif.com", NULL, &hint, &res);
// struct in_addr addr4 = ((struct sockaddr_in *) (res->ai_addr))->sin_addr;
// inet_addr_to_ip4addr(ip_2_ip4(&target_addr), &addr4);
// freeaddrinfo(res);
// esp_ping_config_t ping_config = ESP_PING_DEFAULT_CONFIG();
// ping_config.target_addr = target_addr; // target IP address
// ping_config.count = ESP_PING_COUNT_INFINITE; // ping in infinite mode, esp_ping_stop can stop it
// /* set callback functions */
// esp_ping_callbacks_t cbs;
// cbs.on_ping_success = test_on_ping_success;
// cbs.on_ping_timeout = test_on_ping_timeout;
// cbs.on_ping_end = test_on_ping_end;
// cbs.cb_args = "foo"; // arguments that will feed to all callback functions, can be NULL
// cbs.cb_args = eth_event_group;
// esp_ping_handle_t ping;
// esp_ping_new_session(&ping_config, &cbs, &ping);
// }
#define PIN_PHY_POWER 12
void app_main(void)
{
@ -350,7 +398,7 @@ void app_main(void)
adc1_config_channel_atten(ADC1_CHANNEL_3, ADC_ATTEN_DB_11);
// gpio setup
//zero-initialize the config structure.
//zero-initialize the config structure.
gpio_config_t io_conf = {};
//interrupt of rising edge
@ -372,7 +420,11 @@ void app_main(void)
//start signal task
xTaskCreate(task_signals, "signals", 2048, NULL, 10, NULL);
// start task to continually check dns resolution
xTaskCreate(check_resolution, "dns_res", 2048, NULL, 10, NULL);
// continuously refresh display
// not all events trigger refreshing the disply
while (1) {
blink_mod = !blink_mod;
refresh_display();