mirror of
https://github.com/albfan/miraclecast.git
synced 2025-02-12 16:41:59 +00:00
miracled: friendly-name support for local links
The friendly-name is used as name for local links. Default to a random string and try to read the local hostname during startup. Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
This commit is contained in:
parent
062c73ecf3
commit
2e50d006ad
6 changed files with 119 additions and 2 deletions
|
@ -303,7 +303,15 @@ static int link_dbus_set_name(sd_bus *bus,
|
|||
void *data,
|
||||
sd_bus_error *err)
|
||||
{
|
||||
return -EACCES;
|
||||
struct link *l = data;
|
||||
int r;
|
||||
const char *name;
|
||||
|
||||
r = sd_bus_message_read(value, "s", &name);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
return link_set_friendly_name(l, name);
|
||||
}
|
||||
|
||||
static const sd_bus_vtable link_dbus_vtable[] = {
|
||||
|
|
|
@ -152,6 +152,10 @@ static int link_wifi_init(struct link *l)
|
|||
r = wifi_open(l->w, path);
|
||||
free(path);
|
||||
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
r = wifi_set_name(l->w, l->friendly_name);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
|
@ -212,7 +216,7 @@ int link_new(struct manager *m,
|
|||
goto error;
|
||||
}
|
||||
|
||||
l->friendly_name = strdup("");
|
||||
l->friendly_name = strdup(m->friendly_name);
|
||||
if (!l->friendly_name) {
|
||||
r = log_ENOMEM();
|
||||
goto error;
|
||||
|
@ -270,3 +274,33 @@ void link_free(struct link *l)
|
|||
free(l->interface);
|
||||
free(l);
|
||||
}
|
||||
|
||||
int link_set_friendly_name(struct link *l, const char *name)
|
||||
{
|
||||
char *dup;
|
||||
int r;
|
||||
|
||||
if (!l || !name)
|
||||
return log_EINVAL();
|
||||
|
||||
dup = strdup(name);
|
||||
if (!dup)
|
||||
return log_ENOMEM();
|
||||
|
||||
r = 0;
|
||||
switch (l->type) {
|
||||
case LINK_WIFI:
|
||||
r = wifi_set_name(l->w, name);
|
||||
break;
|
||||
}
|
||||
|
||||
if (r < 0) {
|
||||
free(dup);
|
||||
return r;
|
||||
}
|
||||
|
||||
free(l->friendly_name);
|
||||
l->friendly_name = dup;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -804,6 +804,14 @@ int wifi_set_discoverable(struct wifi *w, bool on)
|
|||
return 0;
|
||||
}
|
||||
|
||||
int wifi_set_name(struct wifi *w, const char *name)
|
||||
{
|
||||
if (!w || !wifi_is_open(w) || !name || !*name)
|
||||
return log_EINVAL();
|
||||
|
||||
return wifi_requestf_ok(w, "SET device_name %s", name);
|
||||
}
|
||||
|
||||
struct wifi_dev *wifi_get_devs(struct wifi *w)
|
||||
{
|
||||
if (!w || shl_dlist_empty(&w->devs))
|
||||
|
|
|
@ -101,6 +101,7 @@ int wifi_open(struct wifi *w, const char *wpa_path);
|
|||
void wifi_close(struct wifi *w);
|
||||
|
||||
int wifi_set_discoverable(struct wifi *w, bool on);
|
||||
int wifi_set_name(struct wifi *w, const char *name);
|
||||
|
||||
struct wifi_dev *wifi_get_devs(struct wifi *w);
|
||||
struct wifi_dev *wifi_dev_next(struct wifi_dev *d);
|
||||
|
|
|
@ -35,6 +35,7 @@
|
|||
#include <systemd/sd-bus.h>
|
||||
#include <systemd/sd-daemon.h>
|
||||
#include <systemd/sd-event.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
#include "miracled.h"
|
||||
#include "shl_htable.h"
|
||||
|
@ -112,11 +113,13 @@ static void manager_free(struct manager *m)
|
|||
sd_event_source_unref(m->sigs[i]);
|
||||
sd_bus_unref(m->bus);
|
||||
sd_event_unref(m->event);
|
||||
free(m->friendly_name);
|
||||
free(m);
|
||||
}
|
||||
|
||||
static int manager_new(struct manager **out)
|
||||
{
|
||||
char buf[64] = { };
|
||||
struct manager *m;
|
||||
static const int sigs[] = {
|
||||
SIGINT, SIGTERM, SIGQUIT, SIGHUP, SIGPIPE, SIGCHLD, 0
|
||||
|
@ -132,6 +135,13 @@ static int manager_new(struct manager **out)
|
|||
shl_htable_init_str(&m->links);
|
||||
shl_htable_init_str(&m->peers);
|
||||
|
||||
snprintf(buf, sizeof(buf) - 1, "unknown-%u", rand());
|
||||
m->friendly_name = strdup(buf);
|
||||
if (!m->friendly_name) {
|
||||
r = log_ENOMEM();
|
||||
goto error;
|
||||
}
|
||||
|
||||
r = sd_event_default(&m->event);
|
||||
if (r < 0) {
|
||||
log_vERR(r);
|
||||
|
@ -184,8 +194,58 @@ error:
|
|||
return r;
|
||||
}
|
||||
|
||||
static void manager_read_name(struct manager *m)
|
||||
{
|
||||
_cleanup_sd_bus_error_ sd_bus_error err = SD_BUS_ERROR_NULL;
|
||||
_cleanup_sd_bus_message_ sd_bus_message *rep = NULL;
|
||||
const char *name;
|
||||
char *str;
|
||||
int r;
|
||||
|
||||
r = sd_bus_call_method(m->bus,
|
||||
"org.freedesktop.hostname1",
|
||||
"/org/freedesktop/hostname1",
|
||||
"org.freedesktop.DBus.Properties",
|
||||
"Get",
|
||||
&err,
|
||||
&rep,
|
||||
"ss", "org.freedesktop.hostname1", "Hostname");
|
||||
if (r < 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
r = sd_bus_message_enter_container(rep, 'v', "s");
|
||||
if (r < 0)
|
||||
goto error;
|
||||
|
||||
r = sd_bus_message_read(rep, "s", &name);
|
||||
if (r < 0)
|
||||
goto error;
|
||||
|
||||
if (!name || !*name) {
|
||||
log_warning("no hostname set on systemd.hostname1, using: %s",
|
||||
m->friendly_name);
|
||||
return;
|
||||
}
|
||||
|
||||
str = strdup(name);
|
||||
if (!str)
|
||||
return log_vENOMEM();
|
||||
|
||||
free(m->friendly_name);
|
||||
m->friendly_name = str;
|
||||
log_debug("friendly-name from local hostname: %s", str);
|
||||
|
||||
return;
|
||||
|
||||
error:
|
||||
log_warning("cannot read hostname from systemd.hostname1: %s",
|
||||
bus_error_message(&err, r));
|
||||
}
|
||||
|
||||
static int manager_run(struct manager *m)
|
||||
{
|
||||
manager_read_name(m);
|
||||
return sd_event_loop(m->event);
|
||||
}
|
||||
|
||||
|
@ -257,6 +317,8 @@ int main(int argc, char **argv)
|
|||
struct manager *m = NULL;
|
||||
int r;
|
||||
|
||||
srand(time(NULL));
|
||||
|
||||
r = parse_argv(argc, argv);
|
||||
if (r < 0)
|
||||
return EXIT_FAILURE;
|
||||
|
|
|
@ -101,6 +101,8 @@ int link_new(struct manager *m,
|
|||
struct link **out);
|
||||
void link_free(struct link *l);
|
||||
|
||||
int link_set_friendly_name(struct link *l, const char *name);
|
||||
|
||||
/* manager */
|
||||
|
||||
struct manager {
|
||||
|
@ -114,6 +116,8 @@ struct manager {
|
|||
size_t peer_cnt;
|
||||
struct shl_htable links;
|
||||
struct shl_htable peers;
|
||||
|
||||
char *friendly_name;
|
||||
};
|
||||
|
||||
#define MANAGER_FIRST_LINK(_m) \
|
||||
|
|
Loading…
Reference in a new issue