From c6f38f04f8eec1aae937cc87c111609772681cb3 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Sat, 2 Nov 2019 21:32:52 +0100 Subject: [PATCH] mdns: add configuration values for task priority, affinity and internal service timeouts closes https://github.com/espressif/esp-idf/issues/4217 --- components/mdns/Kconfig | 48 +++++++++++++++++++ components/mdns/mdns.c | 19 +++++--- .../mdns/private_include/mdns_private.h | 11 ++++- 3 files changed, 70 insertions(+), 8 deletions(-) diff --git a/components/mdns/Kconfig b/components/mdns/Kconfig index 5c97d65f4..ab5cd8012 100644 --- a/components/mdns/Kconfig +++ b/components/mdns/Kconfig @@ -10,4 +10,52 @@ menu "mDNS" the maximum amount of services here. The valid value is from 1 to 64. + config MDNS_TASK_PRIORITY + int "mDNS task priority" + range 1 255 + default 1 + help + Allows setting mDNS task priority. Please do not set the task priority + higher than priorities of system tasks. Compile time warning/error + would be emitted if the chosen task priority were too high. + + choice MDNS_TASK_AFFINITY + prompt "mDNS task affinity" + default MDNS_TASK_AFFINITY_CPU0 + help + Allows setting mDNS tasks affinity, i.e. whether the task is pinned to + CPU0, pinned to CPU1, or allowed to run on any CPU. + + config MDNS_TASK_AFFINITY_NO_AFFINITY + bool "No affinity" + config MDNS_TASK_AFFINITY_CPU0 + bool "CPU0" + config MDNS_TASK_AFFINITY_CPU1 + bool "CPU1" + depends on !FREERTOS_UNICORE + + endchoice + + config MDNS_TASK_AFFINITY + hex + default FREERTOS_NO_AFFINITY if MDNS_TASK_AFFINITY_NO_AFFINITY + default 0x0 if MDNS_TASK_AFFINITY_CPU0 + default 0x1 if MDNS_TASK_AFFINITY_CPU1 + + config MDNS_SERVICE_ADD_TIMEOUT_MS + int "mDNS adding service timeout (ms)" + range 10 30000 + default 2000 + help + Configures timeout for adding a new mDNS service. Adding a service + fails if could not be completed within this time. + + config MDNS_TIMER_PERIOD_MS + int "mDNS timer period (ms)" + range 10 10000 + default 100 + help + Configures period of mDNS timer, which periodically transmits packets + and schedules mDNS searches. + endmenu diff --git a/components/mdns/mdns.c b/components/mdns/mdns.c index 8ee6b5d91..1476ba7ff 100644 --- a/components/mdns/mdns.c +++ b/components/mdns/mdns.c @@ -17,6 +17,7 @@ #include "mdns_networking.h" #include "esp_log.h" #include +#include #ifdef MDNS_ENABLE_DEBUG void mdns_debug_packet(const uint8_t * data, size_t len); @@ -4095,7 +4096,8 @@ static esp_err_t _mdns_service_task_start(void) return ESP_FAIL; } if (!_mdns_service_task_handle) { - xTaskCreatePinnedToCore(_mdns_service_task, "mdns", MDNS_SERVICE_STACK_DEPTH, NULL, 1, (TaskHandle_t * const)(&_mdns_service_task_handle), 0); + xTaskCreatePinnedToCore(_mdns_service_task, "mdns", MDNS_SERVICE_STACK_DEPTH, NULL, MDNS_TASK_PRIORITY, + (TaskHandle_t * const)(&_mdns_service_task_handle), MDNS_TASK_AFFINITY); if (!_mdns_service_task_handle) { _mdns_stop_timer(); MDNS_SERVICE_UNLOCK(); @@ -4400,12 +4402,15 @@ esp_err_t mdns_service_add(const char * instance, const char * service, const ch return ESP_ERR_NO_MEM; } - uint8_t i = 0; - while (_mdns_get_service_item(service, proto) == NULL && i++ < 200) { - vTaskDelay(1); - } - if (i >= 200) { - return ESP_FAIL; + size_t start = xTaskGetTickCount(); + size_t timeout_ticks = pdMS_TO_TICKS(MDNS_SERVICE_ADD_TIMEOUT_MS); + while (_mdns_get_service_item(service, proto) == NULL) + { + uint32_t expired = xTaskGetTickCount() - start; + if (expired >= timeout_ticks) { + return ESP_FAIL; // Timeout + } + vTaskDelay(MIN(10 / portTICK_RATE_MS, timeout_ticks - expired)); } return ESP_OK; diff --git a/components/mdns/private_include/mdns_private.h b/components/mdns/private_include/mdns_private.h index 3c495dd82..4ba5d396e 100644 --- a/components/mdns/private_include/mdns_private.h +++ b/components/mdns/private_include/mdns_private.h @@ -56,6 +56,15 @@ #define MDNS_SERVICE_PORT 5353 // UDP port that the server runs on #define MDNS_SERVICE_STACK_DEPTH 4096 // Stack size for the service thread +#define MDNS_TASK_PRIORITY CONFIG_MDNS_TASK_PRIORITY +#if (MDNS_TASK_PRIORITY > ESP_TASK_PRIO_MAX) +#error "mDNS task priority is higher than ESP_TASK_PRIO_MAX" +#elif (MDNS_TASK_PRIORITY > ESP_TASKD_EVENT_PRIO) +#warning "mDNS task priority is higher than ESP_TASKD_EVENT_PRIO, mDNS library might not work correctly" +#endif +#define MDNS_TASK_AFFINITY CONFIG_MDNS_TASK_AFFINITY +#define MDNS_SERVICE_ADD_TIMEOUT_MS CONFIG_MDNS_SERVICE_ADD_TIMEOUT_MS + #define MDNS_PACKET_QUEUE_LEN 16 // Maximum packets that can be queued for parsing #define MDNS_ACTION_QUEUE_LEN 16 // Maximum actions pending to the server #define MDNS_TXT_MAX_LEN 1024 // Maximum string length of text data in TXT record @@ -82,7 +91,7 @@ #define MDNS_SRV_PORT_OFFSET 4 #define MDNS_SRV_FQDN_OFFSET 6 -#define MDNS_TIMER_PERIOD_US 100000 +#define MDNS_TIMER_PERIOD_US (CONFIG_MDNS_TIMER_PERIOD_MS*1000) #define MDNS_SERVICE_LOCK() xSemaphoreTake(_mdns_service_semaphore, portMAX_DELAY) #define MDNS_SERVICE_UNLOCK() xSemaphoreGive(_mdns_service_semaphore)