mirror of
https://github.com/espressif/esp-protocols.git
synced 2025-07-18 21:12:22 +02:00
mdns: Stabilization of mdns test app
* Original commit: espressif/esp-idf@93a902ba80
This commit is contained in:
committed by
suren-gabrielyan-espressif
parent
ec491ec536
commit
d39a4c744e
@ -1,4 +1,4 @@
|
|||||||
# SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD
|
# SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
|
||||||
# SPDX-License-Identifier: Apache-2.0
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
import re
|
import re
|
||||||
@ -26,6 +26,9 @@ HOST_NAME = u'tinytester.local'
|
|||||||
# This servce answer sent by host, when there is query from board
|
# This servce answer sent by host, when there is query from board
|
||||||
MDNS_HOST_SERVICE = u'ESP32._http._tcp.local'
|
MDNS_HOST_SERVICE = u'ESP32._http._tcp.local'
|
||||||
|
|
||||||
|
# Number of retries to receive mdns answear
|
||||||
|
RETRY_COUNT = 10
|
||||||
|
|
||||||
stop_mdns_listener = Event()
|
stop_mdns_listener = Event()
|
||||||
start_mdns_listener = Event()
|
start_mdns_listener = Event()
|
||||||
esp_service_answered = Event()
|
esp_service_answered = Event()
|
||||||
@ -136,6 +139,7 @@ def mdns_listener(esp_host): # type:(str) -> None
|
|||||||
continue
|
continue
|
||||||
data, _ = sock.recvfrom(1024)
|
data, _ = sock.recvfrom(1024)
|
||||||
dns = dpkt.dns.DNS(data)
|
dns = dpkt.dns.DNS(data)
|
||||||
|
# Receives queries from esp board and sends answers
|
||||||
if len(dns.qd) > 0:
|
if len(dns.qd) > 0:
|
||||||
if dns.qd[0].name == HOST_NAME:
|
if dns.qd[0].name == HOST_NAME:
|
||||||
console_log('Received query: {} '.format(dns.__repr__()))
|
console_log('Received query: {} '.format(dns.__repr__()))
|
||||||
@ -147,12 +151,13 @@ def mdns_listener(esp_host): # type:(str) -> None
|
|||||||
print(dns.qd[0].name)
|
print(dns.qd[0].name)
|
||||||
console_log('Received query: {} '.format(dns.__repr__()))
|
console_log('Received query: {} '.format(dns.__repr__()))
|
||||||
sock.sendto(get_dns_answer_to_service_query(MDNS_HOST_SERVICE), (MCAST_GRP,UDP_PORT))
|
sock.sendto(get_dns_answer_to_service_query(MDNS_HOST_SERVICE), (MCAST_GRP,UDP_PORT))
|
||||||
|
# Receives answers from esp board and sets event flags for python test cases
|
||||||
if len(dns.an) == 1:
|
if len(dns.an) == 1:
|
||||||
if dns.an[0].name == SERVICE_NAME:
|
if dns.an[0].name.startswith(SERVICE_NAME):
|
||||||
console_log('Received answer to service query: {}'.format(dns.__repr__()))
|
console_log('Received answer to service query: {}'.format(dns.__repr__()))
|
||||||
esp_service_answered.set()
|
esp_service_answered.set()
|
||||||
if len(dns.an) > 1:
|
if len(dns.an) > 1:
|
||||||
if dns.an[1].name == SUB_SERVICE_NAME:
|
if dns.an[1].name.startswith(SUB_SERVICE_NAME):
|
||||||
console_log('Received answer for sub service query: {}'.format(dns.__repr__()))
|
console_log('Received answer for sub service query: {}'.format(dns.__repr__()))
|
||||||
esp_sub_service_answered.set()
|
esp_sub_service_answered.set()
|
||||||
if len(dns.an) > 0 and dns.an[0].type == dpkt.dns.DNS_A:
|
if len(dns.an) > 0 and dns.an[0].type == dpkt.dns.DNS_A:
|
||||||
@ -183,33 +188,49 @@ def create_socket(): # type:() -> socket.socket
|
|||||||
def test_query_dns_http_service(service): # type: (str) -> None
|
def test_query_dns_http_service(service): # type: (str) -> None
|
||||||
print('SRV: Query {}'.format(service))
|
print('SRV: Query {}'.format(service))
|
||||||
sock = create_socket()
|
sock = create_socket()
|
||||||
sock.sendto(get_mdns_service_query(service), (MCAST_GRP,UDP_PORT))
|
packet = get_mdns_service_query(service)
|
||||||
if not esp_service_answered.wait(timeout=25):
|
for _ in range(RETRY_COUNT):
|
||||||
raise ValueError('Test has failed: did not receive mdns answer within timeout')
|
if esp_service_answered.wait(timeout=25):
|
||||||
|
break
|
||||||
|
sock.sendto(packet, (MCAST_GRP,UDP_PORT))
|
||||||
|
else:
|
||||||
|
raise RuntimeError('Test has failed: did not receive mdns answer within timeout')
|
||||||
|
|
||||||
|
|
||||||
def test_query_dns_sub_service(sub_service): # type: (str) -> None
|
def test_query_dns_sub_service(sub_service): # type: (str) -> None
|
||||||
print('PTR: Query {}'.format(sub_service))
|
print('PTR: Query {}'.format(sub_service))
|
||||||
sock = create_socket()
|
sock = create_socket()
|
||||||
sock.sendto(get_mdns_sub_service_query(sub_service), (MCAST_GRP,UDP_PORT))
|
packet = get_mdns_sub_service_query(sub_service)
|
||||||
if not esp_sub_service_answered.wait(timeout=25):
|
for _ in range(RETRY_COUNT):
|
||||||
raise ValueError('Test has failed: did not receive mdns answer within timeout')
|
if esp_sub_service_answered.wait(timeout=25):
|
||||||
|
break
|
||||||
|
sock.sendto(packet, (MCAST_GRP,UDP_PORT))
|
||||||
|
else:
|
||||||
|
raise RuntimeError('Test has failed: did not receive mdns answer within timeout')
|
||||||
|
|
||||||
|
|
||||||
def test_query_dns_host(esp_host): # type: (str) -> None
|
def test_query_dns_host(esp_host): # type: (str) -> None
|
||||||
print('A: {}'.format(esp_host))
|
print('A: {}'.format(esp_host))
|
||||||
sock = create_socket()
|
sock = create_socket()
|
||||||
sock.sendto(get_dns_query_for_esp(esp_host), (MCAST_GRP,UDP_PORT))
|
packet = get_dns_query_for_esp(esp_host)
|
||||||
if not esp_host_answered.wait(timeout=25):
|
for _ in range(RETRY_COUNT):
|
||||||
raise ValueError('Test has failed: did not receive mdns answer within timeout')
|
if esp_host_answered.wait(timeout=25):
|
||||||
|
break
|
||||||
|
sock.sendto(packet, (MCAST_GRP,UDP_PORT))
|
||||||
|
else:
|
||||||
|
raise RuntimeError('Test has failed: did not receive mdns answer within timeout')
|
||||||
|
|
||||||
|
|
||||||
def test_query_dns_host_delegated(esp_host): # type: (str) -> None
|
def test_query_dns_host_delegated(esp_host): # type: (str) -> None
|
||||||
print('A: {}'.format(esp_host))
|
print('A: {}'.format(esp_host))
|
||||||
sock = create_socket()
|
sock = create_socket()
|
||||||
sock.sendto(get_dns_query_for_esp(esp_host + '-delegated'), (MCAST_GRP,UDP_PORT))
|
packet = get_dns_query_for_esp(esp_host + '-delegated')
|
||||||
if not esp_delegated_host_answered.wait(timeout=25):
|
for _ in range(RETRY_COUNT):
|
||||||
raise ValueError('Test has failed: did not receive mdns answer within timeout')
|
if esp_delegated_host_answered.wait(timeout=25):
|
||||||
|
break
|
||||||
|
sock.sendto(packet, (MCAST_GRP,UDP_PORT))
|
||||||
|
else:
|
||||||
|
raise RuntimeError('Test has failed: did not receive mdns answer within timeout')
|
||||||
|
|
||||||
|
|
||||||
@ttfw_idf.idf_custom_test(env_tag='Example_WIFI', group='test-apps')
|
@ttfw_idf.idf_custom_test(env_tag='Example_WIFI', group='test-apps')
|
||||||
@ -229,7 +250,7 @@ def test_app_esp_mdns(env, _): # type: (ttfw_idf.TinyFW.Env, None) -> None
|
|||||||
def start_case(case, desc, result): # type: (str, str, str) -> None
|
def start_case(case, desc, result): # type: (str, str, str) -> None
|
||||||
print('Starting {}: {}'.format(case, desc))
|
print('Starting {}: {}'.format(case, desc))
|
||||||
dut1.write(case)
|
dut1.write(case)
|
||||||
dut1.expect(re.compile(result), timeout=10)
|
dut1.expect(re.compile(result), timeout=30)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# start dns listener thread
|
# start dns listener thread
|
||||||
@ -251,14 +272,15 @@ def test_app_esp_mdns(env, _): # type: (ttfw_idf.TinyFW.Env, None) -> None
|
|||||||
# query dns host name delegated, answer should be received from esp board
|
# query dns host name delegated, answer should be received from esp board
|
||||||
test_query_dns_host_delegated(specific_host)
|
test_query_dns_host_delegated(specific_host)
|
||||||
|
|
||||||
|
# query service from esp board, answer should be received from host
|
||||||
|
start_case('CONFIG_TEST_QUERY_SERVICE', 'Query SRV ESP32._http._tcp.local', 'SRV:ESP32')
|
||||||
|
|
||||||
# query dns-host from esp board, answer should be received from host
|
# query dns-host from esp board, answer should be received from host
|
||||||
start_case('CONFIG_TEST_QUERY_HOST', 'Query tinytester.local', 'tinytester.local resolved to: 127.0.0.1')
|
start_case('CONFIG_TEST_QUERY_HOST', 'Query tinytester.local', 'tinytester.local resolved to: 127.0.0.1')
|
||||||
|
|
||||||
# query dns-host aynchrounusely from esp board, answer should be received from host
|
# query dns-host aynchrounusely from esp board, answer should be received from host
|
||||||
start_case('CONFIG_TEST_QUERY_HOST_ASYNC', 'Query tinytester.local async', 'Async query resolved to A:127.0.0.1')
|
start_case('CONFIG_TEST_QUERY_HOST_ASYNC', 'Query tinytester.local async', 'Async query resolved to A:127.0.0.1')
|
||||||
|
|
||||||
# query service from esp board, answer should be received from host
|
|
||||||
start_case('CONFIG_TEST_QUERY_SERVICE', 'Query SRV ESP32._http._tcp.local', 'SRV:ESP32')
|
|
||||||
finally:
|
finally:
|
||||||
stop_mdns_listener.set()
|
stop_mdns_listener.set()
|
||||||
mdns_responder.join()
|
mdns_responder.join()
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD
|
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
@ -28,7 +28,7 @@ static void get_string(char *line, size_t size)
|
|||||||
line[count] = c;
|
line[count] = c;
|
||||||
++count;
|
++count;
|
||||||
}
|
}
|
||||||
vTaskDelay(50 / portTICK_PERIOD_MS);
|
vTaskDelay(20 / portTICK_PERIOD_MS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -92,8 +92,6 @@ static void initialise_mdns(void)
|
|||||||
|
|
||||||
void app_main(void)
|
void app_main(void)
|
||||||
{
|
{
|
||||||
char line[256];
|
|
||||||
|
|
||||||
ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size());
|
ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size());
|
||||||
ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version());
|
ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version());
|
||||||
|
|
||||||
@ -110,6 +108,8 @@ void app_main(void)
|
|||||||
ESP_ERROR_CHECK(example_connect());
|
ESP_ERROR_CHECK(example_connect());
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
|
char line[256];
|
||||||
|
|
||||||
get_string(line, sizeof(line));
|
get_string(line, sizeof(line));
|
||||||
mdns_test(line);
|
mdns_test(line);
|
||||||
continue;
|
continue;
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD
|
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
@ -9,6 +9,7 @@
|
|||||||
#include "esp_netif.h"
|
#include "esp_netif.h"
|
||||||
|
|
||||||
static const char * TAG = "MDNS_TEST_APP";
|
static const char * TAG = "MDNS_TEST_APP";
|
||||||
|
static const int RETRY_COUNT = 10;
|
||||||
|
|
||||||
static void mdns_print_results(mdns_result_t *results)
|
static void mdns_print_results(mdns_result_t *results)
|
||||||
{
|
{
|
||||||
@ -51,7 +52,7 @@ static bool check_and_print_result(mdns_search_once_t *search)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!result) { // search timeout, but no result
|
if (!result) { // search timeout, but no result
|
||||||
return true;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If yes, print the result
|
// If yes, print the result
|
||||||
@ -69,9 +70,10 @@ static bool check_and_print_result(mdns_search_once_t *search)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void query_mdns_hosts_async(const char * host_name)
|
static bool query_mdns_hosts_async(const char * host_name)
|
||||||
{
|
{
|
||||||
ESP_LOGI(TAG, "Query both A and AAA: %s.local", host_name);
|
ESP_LOGI(TAG, "Query both A and AAA: %s.local", host_name);
|
||||||
|
bool res = false;
|
||||||
|
|
||||||
mdns_search_once_t *s_a = mdns_query_async_new(host_name, NULL, NULL, MDNS_TYPE_A, 1000, 1, NULL);
|
mdns_search_once_t *s_a = mdns_query_async_new(host_name, NULL, NULL, MDNS_TYPE_A, 1000, 1, NULL);
|
||||||
mdns_query_async_delete(s_a);
|
mdns_query_async_delete(s_a);
|
||||||
@ -81,16 +83,19 @@ static void query_mdns_hosts_async(const char * host_name)
|
|||||||
ESP_LOGI(TAG, "Query A %s.local finished", host_name);
|
ESP_LOGI(TAG, "Query A %s.local finished", host_name);
|
||||||
mdns_query_async_delete(s_a);
|
mdns_query_async_delete(s_a);
|
||||||
s_a = NULL;
|
s_a = NULL;
|
||||||
|
res = true;
|
||||||
}
|
}
|
||||||
if (s_aaaa && check_and_print_result(s_aaaa)) {
|
if (s_aaaa && check_and_print_result(s_aaaa)) {
|
||||||
ESP_LOGI(TAG, "Query AAAA %s.local finished", host_name);
|
ESP_LOGI(TAG, "Query AAAA %s.local finished", host_name);
|
||||||
mdns_query_async_delete(s_aaaa);
|
mdns_query_async_delete(s_aaaa);
|
||||||
s_aaaa = NULL;
|
s_aaaa = NULL;
|
||||||
|
res = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void query_mdns_host(const char * host_name)
|
static esp_err_t query_mdns_host(const char * host_name)
|
||||||
{
|
{
|
||||||
ESP_LOGI(TAG, "Query A: %s.local", host_name);
|
ESP_LOGI(TAG, "Query A: %s.local", host_name);
|
||||||
|
|
||||||
@ -101,16 +106,16 @@ static void query_mdns_host(const char * host_name)
|
|||||||
if(err){
|
if(err){
|
||||||
if(err == ESP_ERR_NOT_FOUND){
|
if(err == ESP_ERR_NOT_FOUND){
|
||||||
ESP_LOGW(TAG, "%s: Host was not found!", esp_err_to_name(err));
|
ESP_LOGW(TAG, "%s: Host was not found!", esp_err_to_name(err));
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
ESP_LOGE(TAG, "Query Failed: %s", esp_err_to_name(err));
|
ESP_LOGE(TAG, "Query Failed: %s", esp_err_to_name(err));
|
||||||
return;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
ESP_LOGI(TAG, "Query A: %s.local resolved to: " IPSTR, host_name, IP2STR(&addr));
|
ESP_LOGI(TAG, "Query A: %s.local resolved to: " IPSTR, host_name, IP2STR(&addr));
|
||||||
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void query_mdns_service(const char * instance, const char * service_name, const char * proto)
|
static esp_err_t query_mdns_service(const char * instance, const char * service_name, const char * proto)
|
||||||
{
|
{
|
||||||
ESP_LOGI(TAG, "Query SRV: %s.%s.local", service_name, proto);
|
ESP_LOGI(TAG, "Query SRV: %s.%s.local", service_name, proto);
|
||||||
|
|
||||||
@ -118,15 +123,15 @@ static void query_mdns_service(const char * instance, const char * service_name,
|
|||||||
esp_err_t err = mdns_query_srv(instance, service_name, proto, 3000, &results);
|
esp_err_t err = mdns_query_srv(instance, service_name, proto, 3000, &results);
|
||||||
if(err){
|
if(err){
|
||||||
ESP_LOGE(TAG, "Query Failed: %s", esp_err_to_name(err));
|
ESP_LOGE(TAG, "Query Failed: %s", esp_err_to_name(err));
|
||||||
return;
|
return err;
|
||||||
}
|
}
|
||||||
if(!results){
|
if(!results){
|
||||||
ESP_LOGW(TAG, "No results found!");
|
ESP_LOGW(TAG, "No results found!");
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mdns_print_results(results);
|
mdns_print_results(results);
|
||||||
mdns_query_results_free(results);
|
mdns_query_results_free(results);
|
||||||
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
void query_mdns_service_sub_type(const char * subtype, const char * service_name, const char * proto) {
|
void query_mdns_service_sub_type(const char * subtype, const char * service_name, const char * proto) {
|
||||||
@ -136,11 +141,9 @@ void query_mdns_service_sub_type(const char * subtype, const char * service_name
|
|||||||
esp_err_t err = mdns_query_ptr(service_name, proto, 3000, 20, &results);
|
esp_err_t err = mdns_query_ptr(service_name, proto, 3000, 20, &results);
|
||||||
if(err){
|
if(err){
|
||||||
ESP_LOGE(TAG, "Query Failed: %s", esp_err_to_name(err));
|
ESP_LOGE(TAG, "Query Failed: %s", esp_err_to_name(err));
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
if(!results){
|
if(!results){
|
||||||
ESP_LOGW(TAG, "No results found!");
|
ESP_LOGW(TAG, "No results found!");
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mdns_print_results(results);
|
mdns_print_results(results);
|
||||||
@ -150,16 +153,33 @@ void query_mdns_service_sub_type(const char * subtype, const char * service_name
|
|||||||
void mdns_test(const char *line)
|
void mdns_test(const char *line)
|
||||||
{
|
{
|
||||||
char test_case[32];
|
char test_case[32];
|
||||||
|
int i = 0;
|
||||||
|
const TickType_t xDelay = 1000 / portTICK_PERIOD_MS;
|
||||||
|
|
||||||
sscanf(line, "%s", test_case);
|
sscanf(line, "%s", test_case);
|
||||||
ESP_LOGI(TAG, "test case = %s", test_case);
|
ESP_LOGI(TAG, "test case = %s", test_case);
|
||||||
|
|
||||||
if (strcmp(test_case, "CONFIG_TEST_QUERY_HOST") == 0) {
|
if (strcmp(test_case, "CONFIG_TEST_QUERY_HOST") == 0) {
|
||||||
query_mdns_host("tinytester");
|
i = 0;
|
||||||
|
while (query_mdns_host("tinytester") != ESP_OK && i != RETRY_COUNT) {
|
||||||
|
query_mdns_host("tinytester");
|
||||||
|
i++;
|
||||||
|
vTaskDelay(xDelay);
|
||||||
|
}
|
||||||
} else if (strcmp(test_case, "CONFIG_TEST_QUERY_HOST_ASYNC") == 0) {
|
} else if (strcmp(test_case, "CONFIG_TEST_QUERY_HOST_ASYNC") == 0) {
|
||||||
query_mdns_hosts_async("tinytester");
|
i = 0;
|
||||||
|
while (query_mdns_hosts_async("tinytester") == false && i != RETRY_COUNT) {
|
||||||
|
query_mdns_hosts_async("tinytester");
|
||||||
|
i++;
|
||||||
|
vTaskDelay(xDelay);
|
||||||
|
}
|
||||||
} else if (strcmp(test_case, "CONFIG_TEST_QUERY_SERVICE") == 0) {
|
} else if (strcmp(test_case, "CONFIG_TEST_QUERY_SERVICE") == 0) {
|
||||||
query_mdns_service("ESP32", "_http", "_tcp");
|
i = 0;
|
||||||
|
while (query_mdns_service("ESP32", "_http", "_tcp") != ESP_OK && i != RETRY_COUNT) {
|
||||||
|
query_mdns_service("ESP32", "_http", "_tcp");
|
||||||
|
i++;
|
||||||
|
vTaskDelay(xDelay);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
ESP_LOGE(TAG, "%s: No such test case", test_case);
|
ESP_LOGE(TAG, "%s: No such test case", test_case);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user