src.nth.io/

summaryrefslogtreecommitdiff
path: root/main/mdns_service.c
diff options
context:
space:
mode:
Diffstat (limited to 'main/mdns_service.c')
-rw-r--r--main/mdns_service.c81
1 files changed, 31 insertions, 50 deletions
diff --git a/main/mdns_service.c b/main/mdns_service.c
index d389161..21cd464 100644
--- a/main/mdns_service.c
+++ b/main/mdns_service.c
@@ -14,48 +14,30 @@ static const char *SERVICE = "_scrypted-viewport";
static const char *PROTO = "_tcp";
static const uint16_t PORT = 80;
-// Pull the current hostname + TXT values from viewport_state under the lock.
-static void snapshot_state(char *hostname, size_t host_cap,
- const char **resolution,
- const char **orientation,
- char *name, size_t name_cap)
-{
- viewport_state_lock();
- viewport_state_t *st = viewport_state_get();
-
- if (st->viewport_name[0]) {
- snprintf(hostname, host_cap, "viewport-%s", st->viewport_name);
- } else {
- snprintf(hostname, host_cap, "viewport");
- }
- *resolution = viewport_state_resolution_str();
- *orientation = (st->orientation == VIEWPORT_ORIENTATION_PORTRAIT)
- ? "portrait" : "landscape";
- snprintf(name, name_cap, "%s", st->viewport_name[0] ? st->viewport_name : "");
-
- viewport_state_unlock();
-}
-
-static esp_err_t apply_hostname(void)
+// Pull hostname + TXT values from viewport_state under one lock acquisition,
+// then push them at the mDNS service. mdns_service_add() rejects with
+// INVALID_ARG if the hostname isn't set, so order matters on first apply.
+static esp_err_t apply_state(bool include_hostname)
{
char hostname[80];
- const char *resolution, *orientation;
char name[64];
- snapshot_state(hostname, sizeof(hostname),
- &resolution, &orientation,
- name, sizeof(name));
- return mdns_hostname_set(hostname);
-}
-
-static esp_err_t apply_txt(void)
-{
- char hostname[80];
const char *resolution, *orientation;
- char name[64];
- snapshot_state(hostname, sizeof(hostname),
- &resolution, &orientation,
- name, sizeof(name));
+ viewport_state_lock();
+ viewport_state_t *st = viewport_state_get();
+ snprintf(hostname, sizeof(hostname), "viewport%s%s",
+ st->viewport_name[0] ? "-" : "",
+ st->viewport_name[0] ? st->viewport_name : "");
+ snprintf(name, sizeof(name), "%s",
+ st->viewport_name[0] ? st->viewport_name : "");
+ resolution = viewport_state_resolution_str();
+ orientation = (st->orientation == VIEWPORT_ORIENTATION_PORTRAIT)
+ ? "portrait" : "landscape";
+ viewport_state_unlock();
+
+ if (include_hostname) {
+ ESP_RETURN_ON_ERROR(mdns_hostname_set(hostname), TAG, "hostname_set");
+ }
mdns_txt_item_t txt[] = {
{ "version", VIEWPORT_VERSION },
{ "resolution", resolution },
@@ -68,21 +50,21 @@ static esp_err_t apply_txt(void)
esp_err_t mdns_service_start(void)
{
- // Order matters: mdns_service_add() rejects with INVALID_ARG if the
- // hostname isn't set yet. So: init -> hostname -> service_add -> TXT.
- ESP_RETURN_ON_ERROR(mdns_init(), TAG, "mdns_init");
- ESP_RETURN_ON_ERROR(apply_hostname(), TAG, "hostname_set");
+ char hostname[80];
+ viewport_state_lock();
+ viewport_state_t *st = viewport_state_get();
+ snprintf(hostname, sizeof(hostname), "viewport%s%s",
+ st->viewport_name[0] ? "-" : "",
+ st->viewport_name[0] ? st->viewport_name : "");
+ viewport_state_unlock();
+
+ ESP_RETURN_ON_ERROR(mdns_init(), TAG, "mdns_init");
+ ESP_RETURN_ON_ERROR(mdns_hostname_set(hostname), TAG, "hostname_set");
ESP_RETURN_ON_ERROR(
mdns_service_add(NULL, SERVICE, PROTO, PORT, NULL, 0),
TAG, "service_add");
- ESP_RETURN_ON_ERROR(apply_txt(), TAG, "txt_set");
+ ESP_RETURN_ON_ERROR(apply_state(false), TAG, "txt_set");
- char hostname[80];
- const char *resolution, *orientation;
- char name[64];
- snapshot_state(hostname, sizeof(hostname),
- &resolution, &orientation,
- name, sizeof(name));
ESP_LOGI(TAG, "mDNS up — %s.local advertising %s.%s.local on :%u",
hostname, SERVICE, PROTO, PORT);
return ESP_OK;
@@ -90,6 +72,5 @@ esp_err_t mdns_service_start(void)
esp_err_t mdns_service_refresh(void)
{
- ESP_RETURN_ON_ERROR(apply_hostname(), TAG, "hostname_set");
- return apply_txt();
+ return apply_state(true);
}