mirror of
https://github.com/espressif/esp-protocols.git
synced 2025-06-26 01:41:32 +02:00
feat(mdns): Console test for add/remove delegated service APIs
This commit is contained in:
@ -6079,7 +6079,7 @@ esp_err_t mdns_instance_name_set(const char *instance)
|
|||||||
esp_err_t mdns_service_add_for_host(const char *instance, const char *service, const char *proto, const char *hostname,
|
esp_err_t mdns_service_add_for_host(const char *instance, const char *service, const char *proto, const char *hostname,
|
||||||
uint16_t port, mdns_txt_item_t txt[], size_t num_items)
|
uint16_t port, mdns_txt_item_t txt[], size_t num_items)
|
||||||
{
|
{
|
||||||
if (!_mdns_server || _str_null_or_empty(service) || _str_null_or_empty(proto) || !port || !hostname) {
|
if (!_mdns_server || _str_null_or_empty(service) || _str_null_or_empty(proto) || !port || !_mdns_server->hostname) {
|
||||||
return ESP_ERR_INVALID_ARG;
|
return ESP_ERR_INVALID_ARG;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -6089,6 +6089,10 @@ esp_err_t mdns_service_add_for_host(const char *instance, const char *service, c
|
|||||||
return ESP_ERR_NO_MEM;
|
return ESP_ERR_NO_MEM;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!hostname) {
|
||||||
|
hostname = _mdns_server->hostname;
|
||||||
|
}
|
||||||
|
|
||||||
mdns_srv_item_t *item = _mdns_get_service_item_instance(instance, service, proto, hostname);
|
mdns_srv_item_t *item = _mdns_get_service_item_instance(instance, service, proto, hostname);
|
||||||
MDNS_SERVICE_UNLOCK();
|
MDNS_SERVICE_UNLOCK();
|
||||||
if (item) {
|
if (item) {
|
||||||
|
@ -697,6 +697,7 @@ static struct {
|
|||||||
struct arg_str *proto;
|
struct arg_str *proto;
|
||||||
struct arg_int *port;
|
struct arg_int *port;
|
||||||
struct arg_str *instance;
|
struct arg_str *instance;
|
||||||
|
struct arg_str *host;
|
||||||
struct arg_str *txt;
|
struct arg_str *txt;
|
||||||
struct arg_end *end;
|
struct arg_end *end;
|
||||||
} mdns_add_args;
|
} mdns_add_args;
|
||||||
@ -718,6 +719,11 @@ static int cmd_mdns_service_add(int argc, char **argv)
|
|||||||
instance = mdns_add_args.instance->sval[0];
|
instance = mdns_add_args.instance->sval[0];
|
||||||
printf("MDNS: Service Instance: %s\n", instance);
|
printf("MDNS: Service Instance: %s\n", instance);
|
||||||
}
|
}
|
||||||
|
const char *host = NULL;
|
||||||
|
if (mdns_add_args.host->count && mdns_add_args.host->sval[0]) {
|
||||||
|
host = mdns_add_args.host->sval[0];
|
||||||
|
printf("MDNS: Service for delegated host: %s\n", host);
|
||||||
|
}
|
||||||
mdns_txt_item_t *items = NULL;
|
mdns_txt_item_t *items = NULL;
|
||||||
if (mdns_add_args.txt->count) {
|
if (mdns_add_args.txt->count) {
|
||||||
items = _convert_items(mdns_add_args.txt->sval, mdns_add_args.txt->count);
|
items = _convert_items(mdns_add_args.txt->sval, mdns_add_args.txt->count);
|
||||||
@ -728,7 +734,8 @@ static int cmd_mdns_service_add(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ESP_ERROR_CHECK( mdns_service_add(instance, mdns_add_args.service->sval[0], mdns_add_args.proto->sval[0], mdns_add_args.port->ival[0], items, mdns_add_args.txt->count) );
|
ESP_ERROR_CHECK( mdns_service_add_for_host(instance, mdns_add_args.service->sval[0], mdns_add_args.proto->sval[0],
|
||||||
|
host, mdns_add_args.port->ival[0], items, mdns_add_args.txt->count) );
|
||||||
free(items);
|
free(items);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -739,6 +746,7 @@ static void register_mdns_service_add(void)
|
|||||||
mdns_add_args.proto = arg_str1(NULL, NULL, "<proto>", "IP Protocol");
|
mdns_add_args.proto = arg_str1(NULL, NULL, "<proto>", "IP Protocol");
|
||||||
mdns_add_args.port = arg_int1(NULL, NULL, "<port>", "Service Port");
|
mdns_add_args.port = arg_int1(NULL, NULL, "<port>", "Service Port");
|
||||||
mdns_add_args.instance = arg_str0("i", "instance", "<instance>", "Instance name");
|
mdns_add_args.instance = arg_str0("i", "instance", "<instance>", "Instance name");
|
||||||
|
mdns_add_args.host = arg_str0("h", "host", "<hostname>", "Service for this (delegated) host");
|
||||||
mdns_add_args.txt = arg_strn(NULL, NULL, "item", 0, 30, "TXT Items (name=value)");
|
mdns_add_args.txt = arg_strn(NULL, NULL, "item", 0, 30, "TXT Items (name=value)");
|
||||||
mdns_add_args.end = arg_end(2);
|
mdns_add_args.end = arg_end(2);
|
||||||
|
|
||||||
@ -754,8 +762,10 @@ static void register_mdns_service_add(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static struct {
|
static struct {
|
||||||
|
struct arg_str *instance;
|
||||||
struct arg_str *service;
|
struct arg_str *service;
|
||||||
struct arg_str *proto;
|
struct arg_str *proto;
|
||||||
|
struct arg_str *host;
|
||||||
struct arg_end *end;
|
struct arg_end *end;
|
||||||
} mdns_remove_args;
|
} mdns_remove_args;
|
||||||
|
|
||||||
@ -772,7 +782,16 @@ static int cmd_mdns_service_remove(int argc, char **argv)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ESP_ERROR_CHECK( mdns_service_remove(mdns_remove_args.service->sval[0], mdns_remove_args.proto->sval[0]) );
|
const char *instance = NULL;
|
||||||
|
if (mdns_remove_args.instance->count && mdns_remove_args.instance->sval[0]) {
|
||||||
|
instance = mdns_remove_args.instance->sval[0];
|
||||||
|
}
|
||||||
|
const char *host = NULL;
|
||||||
|
if (mdns_remove_args.host->count && mdns_remove_args.host->sval[0]) {
|
||||||
|
host = mdns_remove_args.host->sval[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
ESP_ERROR_CHECK( mdns_service_remove_for_host(instance, mdns_remove_args.service->sval[0], mdns_remove_args.proto->sval[0], host) );
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -780,7 +799,9 @@ static void register_mdns_service_remove(void)
|
|||||||
{
|
{
|
||||||
mdns_remove_args.service = arg_str1(NULL, NULL, "<service>", "MDNS Service");
|
mdns_remove_args.service = arg_str1(NULL, NULL, "<service>", "MDNS Service");
|
||||||
mdns_remove_args.proto = arg_str1(NULL, NULL, "<proto>", "IP Protocol");
|
mdns_remove_args.proto = arg_str1(NULL, NULL, "<proto>", "IP Protocol");
|
||||||
mdns_remove_args.end = arg_end(2);
|
mdns_remove_args.host = arg_str0("h", "host", "<hostname>", "Service for this (delegated) host");
|
||||||
|
mdns_remove_args.instance = arg_str0("i", "instance", "<instance>", "Instance name");
|
||||||
|
mdns_remove_args.end = arg_end(4);
|
||||||
|
|
||||||
const esp_console_cmd_t cmd_remove = {
|
const esp_console_cmd_t cmd_remove = {
|
||||||
.command = "mdns_service_remove",
|
.command = "mdns_service_remove",
|
||||||
|
@ -82,5 +82,27 @@ def test_undelegate_host(mdns_console, dig_app):
|
|||||||
dig_app.check_record('delegated.local', query_type='A', expected=False)
|
dig_app.check_record('delegated.local', query_type='A', expected=False)
|
||||||
|
|
||||||
|
|
||||||
|
def test_add_delegated_service(mdns_console, dig_app):
|
||||||
|
mdns_console.send_input('mdns_delegate_host delegated 1.2.3.4')
|
||||||
|
dig_app.check_record('delegated.local', query_type='A', expected=True)
|
||||||
|
mdns_console.send_input('mdns_service_add _test _tcp 80 -i local')
|
||||||
|
mdns_console.get_output('MDNS: Service Instance: local')
|
||||||
|
mdns_console.send_input('mdns_service_add _test2 _tcp 80 -i extern -h delegated')
|
||||||
|
mdns_console.get_output('MDNS: Service Instance: extern')
|
||||||
|
mdns_console.send_input('mdns_service_lookup _test _tcp')
|
||||||
|
mdns_console.get_output('PTR : local')
|
||||||
|
mdns_console.send_input('mdns_service_lookup _test2 _tcp -d')
|
||||||
|
mdns_console.get_output('PTR : extern')
|
||||||
|
dig_app.check_record('_test2._tcp.local', query_type='PTR', expected=True)
|
||||||
|
dig_app.check_record('extern._test2._tcp.local', query_type='SRV', expected=True)
|
||||||
|
|
||||||
|
|
||||||
|
def test_remove_delegated_service(mdns_console, dig_app):
|
||||||
|
mdns_console.send_input('mdns_service_remove _test2 _tcp -h delegated')
|
||||||
|
mdns_console.send_input('mdns_service_lookup _test2 _tcp -d')
|
||||||
|
mdns_console.get_output('No results found!')
|
||||||
|
dig_app.check_record('_test2._tcp.local', query_type='PTR', expected=False)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
pytest.main(['-s', 'test_mdns.py'])
|
pytest.main(['-s', 'test_mdns.py'])
|
||||||
|
Reference in New Issue
Block a user