Skip to content

Commit 52306e9

Browse files
gjc13suren-gabrielyan-espressif
authored andcommitted
mdns: add notification callback for async APIs
* Original commit: espressif/esp-idf@986603c
1 parent d37ab6d commit 52306e9

File tree

5 files changed

+24
-10
lines changed

5 files changed

+24
-10
lines changed

components/mdns/include/mdns.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ typedef struct mdns_result_s {
9393
mdns_ip_addr_t * addr; /*!< linked list of IP addresses found */
9494
} mdns_result_t;
9595

96+
typedef void (*mdns_query_notify_t)(mdns_search_once_t *search);
97+
9698
/**
9799
* @brief Initialize mDNS on given interface
98100
*
@@ -522,11 +524,13 @@ bool mdns_query_async_get_results(mdns_search_once_t* search, uint32_t timeout,
522524
* @param type type of query (MDNS_TYPE_*)
523525
* @param timeout time in milliseconds during which mDNS query is active
524526
* @param max_results maximum results to be collected
527+
* @param notifier Notification function to be called when the result is ready, can be NULL
525528
*
526529
* @return mdns_search_once_s pointer to new search object if query initiated successfully.
527530
* NULL otherwise.
528531
*/
529-
mdns_search_once_t* mdns_query_async_new(const char * name, const char * service_type, const char * proto, uint16_t type, uint32_t timeout, size_t max_results);
532+
mdns_search_once_t *mdns_query_async_new(const char *name, const char *service_type, const char *proto, uint16_t type,
533+
uint32_t timeout, size_t max_results, mdns_query_notify_t notifier);
530534

531535
/**
532536
* @brief Query mDNS for host or service

components/mdns/mdns.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3601,7 +3601,8 @@ static void _mdns_search_free(mdns_search_once_t * search)
36013601
/**
36023602
* @brief Allocate new search structure
36033603
*/
3604-
static mdns_search_once_t * _mdns_search_init(const char * name, const char * service, const char * proto, uint16_t type, uint32_t timeout, uint8_t max_results)
3604+
static mdns_search_once_t *_mdns_search_init(const char *name, const char *service, const char *proto, uint16_t type,
3605+
uint32_t timeout, uint8_t max_results, mdns_query_notify_t notifier)
36053606
{
36063607
mdns_search_once_t * search = (mdns_search_once_t *)malloc(sizeof(mdns_search_once_t));
36073608
if (!search) {
@@ -3648,6 +3649,7 @@ static mdns_search_once_t * _mdns_search_init(const char * name, const char * se
36483649
search->state = SEARCH_INIT;
36493650
search->sent_at = 0;
36503651
search->started_at = xTaskGetTickCount() * portTICK_PERIOD_MS;
3652+
search->notifier = notifier;
36513653
search->next = NULL;
36523654

36533655
return search;
@@ -3660,6 +3662,9 @@ static void _mdns_search_finish(mdns_search_once_t * search)
36603662
{
36613663
search->state = SEARCH_OFF;
36623664
queueDetach(mdns_search_once_t, _mdns_server->search_once, search);
3665+
if (search->notifier) {
3666+
search->notifier(search);
3667+
}
36633668
xSemaphoreGive(search->done_semaphore);
36643669
}
36653670

@@ -5349,15 +5354,16 @@ bool mdns_query_async_get_results(mdns_search_once_t* search, uint32_t timeout,
53495354
return false;
53505355
}
53515356

5352-
mdns_search_once_t* mdns_query_async_new(const char * name, const char * service, const char * proto, uint16_t type, uint32_t timeout, size_t max_results)
5357+
mdns_search_once_t *mdns_query_async_new(const char *name, const char *service, const char *proto, uint16_t type,
5358+
uint32_t timeout, size_t max_results, mdns_query_notify_t notifier)
53535359
{
53545360
mdns_search_once_t *search = NULL;
53555361

53565362
if (!_mdns_server || !timeout || _str_null_or_empty(service) != _str_null_or_empty(proto)) {
53575363
return NULL;
53585364
}
53595365

5360-
search = _mdns_search_init(name, service, proto, type, timeout, max_results);
5366+
search = _mdns_search_init(name, service, proto, type, timeout, max_results, notifier);
53615367
if (!search) {
53625368
return NULL;
53635369
}
@@ -5384,7 +5390,7 @@ esp_err_t mdns_query(const char * name, const char * service, const char * proto
53845390
return ESP_ERR_INVALID_ARG;
53855391
}
53865392

5387-
search = _mdns_search_init(name, service, proto, type, timeout, max_results);
5393+
search = _mdns_search_init(name, service, proto, type, timeout, max_results, NULL);
53885394
if (!search) {
53895395
return ESP_ERR_NO_MEM;
53905396
}

components/mdns/private_include/mdns_private.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ typedef struct mdns_search_once_s {
365365
uint32_t started_at;
366366
uint32_t sent_at;
367367
uint32_t timeout;
368+
mdns_query_notify_t notifier;
368369
SemaphoreHandle_t done_semaphore;
369370
uint16_t type;
370371
uint8_t max_results;

components/mdns/test_afl_fuzz_host/mdns_di.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@
88

99
void (*mdns_test_static_execute_action)(mdns_action_t *) = NULL;
1010
mdns_srv_item_t * (*mdns_test_static_mdns_get_service_item)(const char * service, const char * proto, const char *hostname) = NULL;
11-
mdns_search_once_t * (*mdns_test_static_search_init)(const char * name, const char * service, const char * proto, uint16_t type, uint32_t timeout, uint8_t max_results) = NULL;
11+
mdns_search_once_t *(*mdns_test_static_search_init)(const char *name, const char *service, const char *proto,
12+
uint16_t type, uint32_t timeout, uint8_t max_results,
13+
mdns_query_notify_t notifier) = NULL;
1214
esp_err_t (*mdns_test_static_send_search_action)(mdns_action_type_t type, mdns_search_once_t * search) = NULL;
1315
void (*mdns_test_static_search_free)(mdns_search_once_t * search) = NULL;
1416

1517
static void _mdns_execute_action(mdns_action_t * action);
1618
static mdns_srv_item_t * _mdns_get_service_item(const char * service, const char * proto, const char *hostname);
17-
static mdns_search_once_t * _mdns_search_init(const char * name, const char * service, const char * proto, uint16_t type, uint32_t timeout, uint8_t max_results);
19+
static mdns_search_once_t *_mdns_search_init(const char *name, const char *service, const char *proto, uint16_t type,
20+
uint32_t timeout, uint8_t max_results, mdns_query_notify_t notifier);
1821
static esp_err_t _mdns_send_search_action(mdns_action_type_t type, mdns_search_once_t * search);
1922
static void _mdns_search_free(mdns_search_once_t * search);
2023

@@ -44,7 +47,7 @@ esp_err_t mdns_test_send_search_action(mdns_action_type_t type, mdns_search_once
4447

4548
mdns_search_once_t * mdns_test_search_init(const char * name, const char * service, const char * proto, uint16_t type, uint32_t timeout, uint8_t max_results)
4649
{
47-
return mdns_test_static_search_init(name, service, proto, type, timeout, max_results);
50+
return mdns_test_static_search_init(name, service, proto, type, timeout, max_results, NULL);
4851
}
4952

5053
mdns_srv_item_t * mdns_test_mdns_get_service_item(const char * service, const char * proto)

examples/protocols/mdns/main/mdns_example_main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,9 @@ static void query_mdns_hosts_async(const char * host_name)
169169
{
170170
ESP_LOGI(TAG, "Query both A and AAA: %s.local", host_name);
171171

172-
mdns_search_once_t *s_a = mdns_query_async_new(host_name, NULL, NULL, MDNS_TYPE_A, 1000, 1);
172+
mdns_search_once_t *s_a = mdns_query_async_new(host_name, NULL, NULL, MDNS_TYPE_A, 1000, 1, NULL);
173173
mdns_query_async_delete(s_a);
174-
mdns_search_once_t *s_aaaa = mdns_query_async_new(host_name, NULL, NULL, MDNS_TYPE_AAAA, 1000, 1);
174+
mdns_search_once_t *s_aaaa = mdns_query_async_new(host_name, NULL, NULL, MDNS_TYPE_AAAA, 1000, 1, NULL);
175175
while (s_a || s_aaaa) {
176176
if (s_a && check_and_print_result(s_a)) {
177177
ESP_LOGI(TAG, "Query A %s.local finished", host_name);

0 commit comments

Comments
 (0)