int main(int argc, char *argv[]) { GDBusClient *client; guint signal; signal = setup_signalfd(); if (signal == 0) return -errno; connection = g_dbus_setup_bus(DBUS_BUS_SYSTEM, NULL, NULL); main_loop = g_main_loop_new(NULL, FALSE); g_dbus_attach_object_manager(connection); printf("gatt-service unique name: %s\n", dbus_bus_get_unique_name(connection)); create_services(); client = g_dbus_client_new(connection, "org.bluez", "/org/bluez"); g_dbus_client_set_connect_watch(client, connect_handler, NULL); g_main_loop_run(main_loop); g_dbus_client_unref(client); g_source_remove(signal); g_slist_free_full(services, g_free); dbus_connection_unref(connection); return 0; }
static void client_callback(AvahiClient * c, AvahiClientState state, AVAHI_GCC_UNUSED void *userdata) { Game *game = (Game *) userdata; g_assert(c != NULL); /* Called whenever the client or server state changes */ switch (state) { case AVAHI_CLIENT_S_RUNNING: /* The server has startup successfully and registered its host * name on the network, so it's time to create our services */ if (!group) create_services(c, game); break; case AVAHI_CLIENT_S_COLLISION: /* Let's drop our registered services. When the server is back * in AVAHI_SERVER_RUNNING state we will register them * again with the new host name. */ if (group) avahi_entry_group_reset(group); break; case AVAHI_CLIENT_FAILURE: log_message(MSG_ERROR, _("Avahi error: %s, %s\n"), "Client failure", avahi_strerror(avahi_client_errno(c))); avahi_glib_poll_free(glib_poll); break; case AVAHI_CLIENT_CONNECTING: case AVAHI_CLIENT_S_REGISTERING: ; } }
static void client_callback (AvahiClient *c, AvahiClientState state, void *userdata) { g_return_if_fail (c); switch (state) { case AVAHI_CLIENT_S_RUNNING: if (!group) { create_services (c); } break; case AVAHI_CLIENT_S_COLLISION: printf ("Client collision, terminating..."); g_main_loop_quit (ml); break; case AVAHI_CLIENT_FAILURE: printf ("Client failure: %s\n", avahi_strerror (avahi_client_errno (c))); g_main_loop_quit (ml); break; case AVAHI_CLIENT_CONNECTING: case AVAHI_CLIENT_S_REGISTERING: ; } }
gboolean dmap_mdns_publisher_publish (DMAPMdnsPublisher * publisher, const char *name, guint port, const char *type_of_service, gboolean password_required, gchar ** txt_records, GError ** error) { struct DMAPMdnsPublisherService *service; if (publisher->priv->client == NULL) { g_set_error (error, DMAP_MDNS_PUBLISHER_ERROR, DMAP_MDNS_PUBLISHER_ERROR_NOT_RUNNING, "%s", _("The avahi MDNS service is not running")); return FALSE; } service = g_new (struct DMAPMdnsPublisherService, 1); service->name = g_strdup (name); service->port = port; service->type_of_service = g_strdup (type_of_service); service->password_required = password_required; service->txt_records = g_strdupv (txt_records); publisher->priv->service = g_slist_append (publisher->priv->service, service); return create_services (publisher, error); }
static void server_callback(AvahiServer *s, AvahiServerState state, AVAHI_GCC_UNUSED void * userdata) { assert(s); /* Called whenever the server state changes */ switch (state) { case AVAHI_SERVER_RUNNING: /* The serve has startup successfully and registered its host * name on the network, so it's time to create our services */ if (!group) create_services(s); break; case AVAHI_SERVER_COLLISION: { char *n; int r; /* A host name collision happened. Let's pick a new name for the server */ n = avahi_alternative_host_name(avahi_server_get_host_name(s)); fprintf(stderr, "Host name collision, retrying with '%s'\n", n); r = avahi_server_set_host_name(s, n); avahi_free(n); if (r < 0) { fprintf(stderr, "Failed to set new host name: %s\n", avahi_strerror(r)); avahi_simple_poll_quit(simple_poll); return; } } /* Fall through */ case AVAHI_SERVER_REGISTERING: /* Let's drop our registered services. When the server is back * in AVAHI_SERVER_RUNNING state we will register them * again with the new host name. */ if (group) avahi_s_entry_group_reset(group); break; case AVAHI_SERVER_FAILURE: /* Terminate on failure */ fprintf(stderr, "Server failure: %s\n", avahi_strerror(avahi_server_errno(s))); avahi_simple_poll_quit(simple_poll); break; case AVAHI_SERVER_INVALID: ; } }
void create_services(AvahiClient *c) { char r[128]; int ret; assert(c); /* If this is the first time we're called, let's create a new * entry group if necessary */ if (!group) if (!(group = avahi_entry_group_new(c, entry_group_callback, NULL))) { std::cerr << "avahi_entry_group_new() failed: " << avahi_strerror(avahi_client_errno(c)) << std::endl; } /* If the group is empty (either because it was just created, or * because it was reset previously, add our entries. */ if (avahi_entry_group_is_empty(group)) { std::cerr << "Adding service '" << name << "'" << std::endl; /* Create some random TXT data */ snprintf(r, sizeof(r), "random=%i", rand()); /* We will now add two services and one subtype to the entry * group. The two services have the same name, but differ in * the service type (IPP vs. BSD LPR). Only services with the * same name should be put in the same entry group. */ if ((ret = avahi_entry_group_add_service(group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, (AvahiPublishFlags)0, name.c_str(), "_heatSync._tcp", NULL, NULL, PORTINT, "version=0.1", NULL, NULL )) < 0) { //todo: magically put in version no. if (ret == AVAHI_ERR_COLLISION) { name = avahi_alternative_service_name(name.c_str()); std::cerr << "Service name collision, renaming service to '" << name << "'" << std::endl; avahi_entry_group_reset(group); create_services(c); return; } std::cerr << "Failed to add " << name << " service: " << avahi_strerror(ret) << std::endl; } /* Tell the server to register the service */ if ((ret = avahi_entry_group_commit(group)) < 0) { std::cerr << "Failed to commit entry group: " << avahi_strerror(ret) << std::endl; } } avahi_simple_poll_quit(simple_publish_poll); }
void btd_gatt_client_ready(struct btd_gatt_client *client) { if (!client) return; if (!client->gatt) { error("GATT client not initialized"); return; } client->ready = true; DBG("GATT client ready"); create_services(client); }
static void entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) { assert(g == group || group == NULL); group = g; /* Called whenever the entry group state changes */ switch (state) { case AVAHI_ENTRY_GROUP_ESTABLISHED : /* The entry group has been established successfully */ tvhlog(LOG_INFO, "AVAHI", "Service '%s' successfully established.", name); break; case AVAHI_ENTRY_GROUP_COLLISION : { char *n; /* A service name collision with a remote service * happened. Let's pick a new name */ n = avahi_alternative_service_name(name); avahi_free(name); name = n; tvhlog(LOG_ERR, "AVAHI", "Service name collision, renaming service to '%s'", name); /* And recreate the services */ create_services(avahi_entry_group_get_client(g)); break; } case AVAHI_ENTRY_GROUP_FAILURE : tvhlog(LOG_ERR, "AVAHI", "Entry group failure: %s", avahi_strerror(avahi_client_errno(avahi_entry_group_get_client(g)))); break; case AVAHI_ENTRY_GROUP_UNCOMMITED: case AVAHI_ENTRY_GROUP_REGISTERING: ; } }
static void entry_group_callback(AvahiServer *s, AvahiSEntryGroup *g, AvahiEntryGroupState state, AVAHI_GCC_UNUSED void *userdata) { assert(s); assert(g == group); /* Called whenever the entry group state changes */ switch (state) { case AVAHI_ENTRY_GROUP_ESTABLISHED: /* The entry group has been established successfully */ fprintf(stderr, "Service '%s' successfully established.\n", name); break; case AVAHI_ENTRY_GROUP_COLLISION: { char *n; /* A service name collision happened. Let's pick a new name */ n = avahi_alternative_service_name(name); avahi_free(name); name = n; fprintf(stderr, "Service name collision, renaming service to '%s'\n", name); /* And recreate the services */ create_services(s); break; } case AVAHI_ENTRY_GROUP_FAILURE : fprintf(stderr, "Entry group failure: %s\n", avahi_strerror(avahi_server_errno(s))); /* Some kind of failure happened while we were registering our services */ avahi_simple_poll_quit(simple_poll); break; case AVAHI_ENTRY_GROUP_UNCOMMITED: case AVAHI_ENTRY_GROUP_REGISTERING: ; } }
void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) { assert(c); /* Called whenever the client or server state changes */ switch (state) { case AVAHI_CLIENT_S_RUNNING: /* The server has startup successfully and registered its host * name on the network, so it's time to create our services */ create_services(c); break; case AVAHI_CLIENT_FAILURE: l_debug("Client failure: %s", avahi_strerror(avahi_client_errno(c))); avahi_simple_poll_quit(simple_poll); break; case AVAHI_CLIENT_S_COLLISION: /* Let's drop our registered services. When the server is back * in AVAHI_SERVER_RUNNING state we will register them * again with the new host name. */ case AVAHI_CLIENT_S_REGISTERING: /* The server records are now being established. This * might be caused by a host name change. We need to wait * for our own records to register until the host name is * properly esatblished. */ if (group) avahi_entry_group_reset(group); break; case AVAHI_CLIENT_CONNECTING: ; } }
void btd_gatt_client_ready(struct btd_gatt_client *client) { struct bt_gatt_client *gatt; if (!client) return; gatt = btd_device_get_gatt_client(client->device); if (!gatt) { error("GATT client not initialized"); return; } bt_gatt_client_unref(client->gatt); client->gatt = bt_gatt_client_ref(gatt); client->ready = true; create_services(client); }
static void modify_callback(AVAHI_GCC_UNUSED AvahiTimeout *e, void *userdata) { AvahiClient *client = userdata; fprintf(stderr, "Doing some weird modification\n"); avahi_free(name); name = avahi_strdup("Modified MegaPrinter"); /* If the server is currently running, we need to remove our * service and create it anew */ if (avahi_client_get_state(client) == AVAHI_CLIENT_S_RUNNING) { /* Remove the old services */ if (group) avahi_entry_group_reset(group); /* And create them again with the new name */ create_services(client); } }
void entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, AVAHI_GCC_UNUSED void *userdata) { assert(g == group || group == NULL); group = g; /* Called whenever the entry group state changes */ switch (state) { case AVAHI_ENTRY_GROUP_ESTABLISHED : /* The entry group has been established successfully */ l_debug("Service '%s' successfully established.", name); break; case AVAHI_ENTRY_GROUP_COLLISION : { char *n; /* A service name collision with a remote service * happened. Let's pick a new name */ n = avahi_alternative_service_name(name); avahi_free(name); name = n; l_debug("Service name collision, renaming service to '%s'", name); /* And recreate the services */ create_services(avahi_entry_group_get_client(g)); break; } case AVAHI_ENTRY_GROUP_FAILURE : l_debug("Entry group failure: %s", avahi_strerror(avahi_client_errno(avahi_entry_group_get_client(g)))); /* Some kind of failure happened while we were registering our services */ avahi_simple_poll_quit(simple_poll); break; case AVAHI_ENTRY_GROUP_UNCOMMITED: case AVAHI_ENTRY_GROUP_REGISTERING: ; } }
gboolean dmap_mdns_publisher_withdraw (DMAPMdnsPublisher * publisher, guint port, GError ** error) { struct DMAPMdnsPublisherService *ptr; if (publisher->priv->client == NULL) { g_set_error (error, DMAP_MDNS_PUBLISHER_ERROR, DMAP_MDNS_PUBLISHER_ERROR_NOT_RUNNING, "%s", _("The avahi MDNS service is not running")); return FALSE; } if (publisher->priv->entry_group == NULL || !(ptr = find_service_by_port (publisher->priv->service, port))) { g_set_error (error, DMAP_MDNS_PUBLISHER_ERROR, DMAP_MDNS_PUBLISHER_ERROR_FAILED, "%s", _("The MDNS service is not published")); return FALSE; } free_service (ptr, NULL); publisher->priv->service = g_slist_remove (publisher->priv->service, ptr); if (publisher->priv->service == NULL) { avahi_entry_group_reset (publisher->priv->entry_group); avahi_entry_group_free (publisher->priv->entry_group); publisher->priv->entry_group = NULL; } else { create_services (publisher, error); if (error != NULL) return FALSE; } return TRUE; }
static void entry_group_callback(AvahiEntryGroup * g, AvahiEntryGroupState state, void *userdata) { Game *game = (Game *) userdata; /* Called whenever the entry group state changes */ switch (state) { case AVAHI_ENTRY_GROUP_ESTABLISHED: /* The entry group has been established successfully */ log_message(MSG_INFO, _("Avahi registration successful.\n")); break; case AVAHI_ENTRY_GROUP_COLLISION:{ /* A service name collision happened. Let's pick a new name */ gchar *n = avahi_alternative_service_name(name); avahi_free(name); name = n; log_message(MSG_INFO, _ ("Avahi service name collision, renaming service to '%s'.\n"), name); /* And recreate the services */ create_services(avahi_entry_group_get_client(g), game); break; } case AVAHI_ENTRY_GROUP_FAILURE: /* Some kind of failure happened while we were registering */ log_message(MSG_ERROR, _("Avahi error: %s\n"), "Some kind of failure happened while we were registering"); break; case AVAHI_ENTRY_GROUP_UNCOMMITED: case AVAHI_ENTRY_GROUP_REGISTERING: break; } }
static void create_services(AvahiClient *c) { char *n, r[128]; int ret; AvahiProtocol protocol; assert(c); /* If this is the first time we're called, let's create a new * entry group if necessary */ if (!group) if (!(group = avahi_entry_group_new(c, entry_group_callback, NULL))) { logging_printf( LOGGING_ERROR, "avahi_entry_group_new() failed: %s\n", avahi_strerror(avahi_client_errno(c))); goto fail; } /* If the group is empty (either because it was just created, or * because it was reset previously, add our entries. */ if (avahi_entry_group_is_empty(group)) { logging_printf(LOGGING_INFO, "Adding service '%s.%s'\n", sd_name_copy, sd_service_copy ); /* Create some random TXT data */ snprintf(r, sizeof(r), "random=%i", rand()); if( use_ipv4 && use_ipv6 ) { protocol = AVAHI_PROTO_UNSPEC; } else if( use_ipv4 ) { protocol = AVAHI_PROTO_INET; } else { protocol = AVAHI_PROTO_INET6; } if ((ret = avahi_entry_group_add_service(group, AVAHI_IF_UNSPEC, protocol, 0, sd_name_copy , sd_service_copy , NULL, NULL, sd_port, "test=blah", r, NULL)) < 0) { if (ret == AVAHI_ERR_COLLISION) goto collision; logging_printf(LOGGING_ERROR, "Failed to add %s service: %s\n", sd_service_copy,avahi_strerror(ret)); goto fail; } /* Tell the server to register the service */ if ((ret = avahi_entry_group_commit(group)) < 0) { logging_printf(LOGGING_ERROR, "Failed to commit entry group: %s\n", avahi_strerror(ret)); goto fail; } } return; collision: /* A service name collision with a local service happened. Let's * pick a new name */ n = avahi_alternative_service_name( sd_name_copy ); avahi_free(sd_name_copy); sd_name_copy = n; logging_printf(LOGGING_WARN, "Service name collision, renaming service to '%s'\n", sd_name_copy ); avahi_entry_group_reset(group); create_services(c); return; fail: avahi_threaded_poll_quit(threaded_poll); }
static void create_services(AvahiClient *c) { char *n, r[128]; int ret; assert(c); /* If this is the first time we're called, let's create a new * entry group if necessary */ if (!group) if (!(group = avahi_entry_group_new(c, entry_group_callback, NULL))) { fprintf(stderr, "avahi_entry_group_new() failed: %s\n", avahi_strerror(avahi_client_errno(c))); goto fail; } /* If the group is empty (either because it was just created, or * because it was reset previously, add our entries. */ if (avahi_entry_group_is_empty(group)) { fprintf(stderr, "Adding service '%s'\n", name); /* Create some random TXT data */ snprintf(r, sizeof(r), "random=%i", rand()); /* We will now add two services and one subtype to the entry * group. The two services have the same name, but differ in * the service type (IPP vs. BSD LPR). Only services with the * same name should be put in the same entry group. */ /* Add the service for IPP */ if ((ret = avahi_entry_group_add_service(group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, name, "_ipp._tcp", NULL, NULL, 651, "test=blah", r, NULL)) < 0) { if (ret == AVAHI_ERR_COLLISION) goto collision; fprintf(stderr, "Failed to add _ipp._tcp service: %s\n", avahi_strerror(ret)); goto fail; } /* Add the same service for BSD LPR */ if ((ret = avahi_entry_group_add_service(group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, name, "_printer._tcp", NULL, NULL, 515, NULL)) < 0) { if (ret == AVAHI_ERR_COLLISION) goto collision; fprintf(stderr, "Failed to add _printer._tcp service: %s\n", avahi_strerror(ret)); goto fail; } /* Add an additional (hypothetic) subtype */ if ((ret = avahi_entry_group_add_service_subtype(group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, name, "_printer._tcp", NULL, "_magic._sub._printer._tcp") < 0)) { fprintf(stderr, "Failed to add subtype _magic._sub._printer._tcp: %s\n", avahi_strerror(ret)); goto fail; } /* Tell the server to register the service */ if ((ret = avahi_entry_group_commit(group)) < 0) { fprintf(stderr, "Failed to commit entry group: %s\n", avahi_strerror(ret)); goto fail; } } return; collision: /* A service name collision with a local service happened. Let's * pick a new name */ n = avahi_alternative_service_name(name); avahi_free(name); name = n; fprintf(stderr, "Service name collision, renaming service to '%s'\n", name); avahi_entry_group_reset(group); create_services(c); return; fail: avahi_simple_poll_quit(simple_poll); }
void PublishAvahi::create_services(AvahiClient *c) { assert(c); char *n; /// If this is the first time we're called, let's create a new entry group if necessary if (!group) { if (!(group = avahi_entry_group_new(c, entry_group_callback, this))) { logE << "avahi_entry_group_new() failed: " << avahi_strerror(avahi_client_errno(c)) << "\n"; goto fail; } } /// If the group is empty (either because it was just created, or because it was reset previously, add our entries. int ret; if (avahi_entry_group_is_empty(group)) { logO << "Adding service '" << name << "'\n"; /// We will now add two services and one subtype to the entry group for (const auto& service: services_) { if ((ret = avahi_entry_group_add_service(group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, AvahiPublishFlags(0), name, service.name_.c_str(), NULL, NULL, service.port_, NULL)) < 0) { if (ret == AVAHI_ERR_COLLISION) goto collision; logE << "Failed to add " << service.name_ << " service: " << avahi_strerror(ret) << "\n"; goto fail; } } /// Add an additional (hypothetic) subtype /* if ((ret = avahi_entry_group_add_service_subtype(group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, AvahiPublishFlags(0), name, "_printer._tcp", NULL, "_magic._sub._printer._tcp") < 0)) { fprintf(stderr, "Failed to add subtype _magic._sub._printer._tcp: %s\n", avahi_strerror(ret)); goto fail; } */ /// Tell the server to register the service if ((ret = avahi_entry_group_commit(group)) < 0) { logE << "Failed to commit entry group: " << avahi_strerror(ret) << "\n"; goto fail; } } return; collision: /// A service name collision with a local service happened. Let's pick a new name n = avahi_alternative_service_name(name); avahi_free(name); name = n; logO << "Service name collision, renaming service to '" << name << "'\n"; avahi_entry_group_reset(group); create_services(c); return; fail: avahi_simple_poll_quit(simple_poll); }
static void create_services(AvahiClient *c) { char *n; char *path = NULL; int ret; assert(c); /* If this is the first time we're called, let's create a new * entry group if necessary */ if (!group) if (!(group = avahi_entry_group_new(c, entry_group_callback, NULL))) { tvhlog(LOG_ERR, "AVAHI", "avahi_enty_group_new() failed: %s", avahi_strerror(avahi_client_errno(c))); goto fail; } /* If the group is empty (either because it was just created, or * because it was reset previously, add our entries. */ if (avahi_entry_group_is_empty(group)) { tvhlog(LOG_DEBUG, "AVAHI", "Adding service '%s'", name); /* Add the service for HTSP */ if ((ret = avahi_entry_group_add_service(group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, name, "_htsp._tcp", NULL, NULL,tvheadend_htsp_port, NULL)) < 0) { if (ret == AVAHI_ERR_COLLISION) goto collision; tvhlog(LOG_ERR, "AVAHI", "Failed to add _htsp._tcp service: %s", avahi_strerror(ret)); goto fail; } if (tvheadend_webroot) { path = malloc(strlen(tvheadend_webroot) + 6); sprintf(path, "path=%s", tvheadend_webroot); } else { path = strdup("path=/"); } /* Add the service for HTTP */ if ((ret = avahi_entry_group_add_service(group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, name, "_http._tcp", NULL, NULL, tvheadend_webui_port, path, NULL)) < 0) { if (ret == AVAHI_ERR_COLLISION) goto collision; tvhlog(LOG_ERR, "AVAHI", "Failed to add _http._tcp service: %s", avahi_strerror(ret)); goto fail; } /* Tell the server to register the service */ if ((ret = avahi_entry_group_commit(group)) < 0) { tvhlog(LOG_ERR, "AVAHI", "Failed to commit entry group: %s", avahi_strerror(ret)); goto fail; } } free(path); return; collision: /* A service name collision with a local service happened. Let's * pick a new name */ n = avahi_alternative_service_name(name); avahi_free(name); name = n; tvhlog(LOG_ERR, "AVAHI", "Service name collision, renaming service to '%s'", name); avahi_entry_group_reset(group); create_services(c); return; fail: free(path); }
void create_services(AvahiClient *c) { char *n; gchar *type_txt, *profile_txt, *data_txt; int ret; assert(c); /* If this is the first time we're called, let's create a new * entry group if necessary */ if (!group) if (!(group = avahi_entry_group_new(c, entry_group_callback, NULL))) { l_debug("avahi_entry_group_new() failed: %s", avahi_strerror(avahi_client_errno(c))); goto fail; } /* If the group is empty (either because it was just created, or * because it was reset previously, add our entries. */ if (avahi_entry_group_is_empty(group)) { l_debug("Adding service '%s' type '%s'", name, type_in); /* Set type of service */ profile_txt = g_strdup_printf("profile=%s", profile); type_txt = g_strdup_printf("type=%s", type_in); if (extra_data == NULL) { extra_data = g_strdup(""); } data_txt = g_strdup_printf("data=%s", extra_data); /* Add the service for Lyricue Display */ if ((ret = avahi_entry_group_add_service(group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, name, "_lyricue._tcp", NULL, NULL, port, type_txt, profile_txt, data_txt, NULL)) < 0) { if (ret == AVAHI_ERR_COLLISION) goto collision; l_debug("Failed to add _lyricue._tcp service: %s", avahi_strerror(ret)); goto fail; } g_free(profile_txt); g_free(type_txt); g_free(data_txt); /* Tell the server to register the service */ if ((ret = avahi_entry_group_commit(group)) < 0) { l_debug("Failed to commit entry group: %s", avahi_strerror(ret)); goto fail; } } return; collision: /* A service name collision with a local service happened. Let's * pick a new name */ n = avahi_alternative_service_name(name); avahi_free(name); name = n; l_debug("Service name collision, renaming service to '%s'", name); avahi_entry_group_reset(group); create_services(c); return; fail: avahi_simple_poll_quit(simple_poll); }
static gboolean refresh_services (DMAPMdnsPublisher * publisher, GError ** error) { return create_services (publisher, error); }