2013-06-25 16:24:33

by Mikel Astiz

[permalink] [raw]
Subject: [RFC BlueZ v0 00/16] Introduce btd_server

From: Mikel Astiz <[email protected]>

This RFC presents a proposal to extend the BlueZ core to simplify the implementation of profiles. It introduces a new core concept (btd_server) as a first step towards providing internal infrastructure in a similar way that external profiles have. This exact purpose of the new struct is described in patch 01.

Currently, it's paradoxical that externally implemented profiles are in a way simpler to implement. There is a lot of helpful code in profile.c which could be reused for internal profiles too.

This first patchset consists of three main parts:
1. The extension of the core (patches 01..03).
2. Some simple examples of adoption (patches 04..10).
3. A more complex adoption (patches 11..16).

This part is fairly simple and doesn't offer great benefits besides the usage of the userdata pointer in btd_server, which can already be handy (see for example 08/16).

Furhter and more interesting changes in the pipeline (but not submitted in v0) include:
4. Handling of incoming connections.
5. Integration with agent authorization.
6. Adoption of new infrastructure for external profiles.

For the record, the result of this whole work does not add more lines of code to maintain, it just moves the code complexity from profile implementations to the core. The result is less duplicated code and data throughout the codebase, hopefully leading to more robust profiles.

Mikel Astiz (16):
core: Add btd_server
adapter: Create btd_server instances when probing
profile: Use btd_server to probe adapters
input: Bypass manager for profile server
input: Use btd_server userdata for input_server
thermometer: Remove boilerplate code
thermometer: Use btd_server userdata
cyclingspeed: Use btd_server userdata
heartrate: Remove boilerplate code
heartrate: Use btd_server userdata
network: Replace list with network_adapter
network: Bypass manager for profile server
network: Simplify search-by-UUID
network: Add a dedicated btd_profile for BNEP
network: Create network_adapter during BNEP probe
network: Use btd_server userdata for network_server

Makefile.am | 1 +
profiles/alert/server.c | 9 +-
profiles/audio/manager.c | 41 +++++---
profiles/cyclingspeed/cyclingspeed.c | 38 +++----
profiles/health/hdp_manager.c | 11 ++-
profiles/heartrate/heartrate.c | 51 +++-------
profiles/input/manager.c | 12 +--
profiles/input/server.c | 28 ++----
profiles/input/server.h | 4 +-
profiles/network/manager.c | 76 ++------------
profiles/network/server.c | 185 +++++++++++++++--------------------
profiles/network/server.h | 8 +-
profiles/proximity/reporter.c | 8 +-
profiles/proximity/reporter.h | 5 +-
profiles/sap/manager.c | 9 +-
profiles/thermometer/thermometer.c | 82 ++++------------
profiles/time/server.c | 8 +-
src/adapter.c | 50 +++++-----
src/adapter.h | 2 +
src/profile.c | 11 ++-
src/profile.h | 7 +-
src/server.c | 108 ++++++++++++++++++++
src/server.h | 38 +++++++
23 files changed, 389 insertions(+), 403 deletions(-)
create mode 100644 src/server.c
create mode 100644 src/server.h

--
1.8.1.4



2013-06-25 16:24:48

by Mikel Astiz

[permalink] [raw]
Subject: [RFC BlueZ v0 15/16] network: Create network_adapter during BNEP probe

From: Mikel Astiz <[email protected]>

The network adapter creation along with the registration of the
corresponding D-Bus interface fit nicely into the newly created
btd_profile instance for BNEP.

As a nice side benefit, the struct network_adapter pointer is assigned
as userdata of the BNEP btd_server, so there is no need to keep a
dedicated adapter list.
---
profiles/network/server.c | 87 ++++++++++++++++++++---------------------------
1 file changed, 36 insertions(+), 51 deletions(-)

diff --git a/profiles/network/server.c b/profiles/network/server.c
index 0e52db0..77e2c7e 100644
--- a/profiles/network/server.c
+++ b/profiles/network/server.c
@@ -83,22 +83,8 @@ struct network_server {
guint watch_id; /* Client service watch */
};

-static GSList *adapters = NULL;
static gboolean security = TRUE;

-static struct network_adapter *find_adapter(GSList *list,
- struct btd_adapter *adapter)
-{
- for (; list; list = list->next) {
- struct network_adapter *na = list->data;
-
- if (na->adapter == adapter)
- return na;
- }
-
- return NULL;
-}
-
static struct network_server *find_server(struct network_adapter *na,
uint16_t id)
{
@@ -716,8 +702,6 @@ static void path_unregister(void *data)
NETWORK_SERVER_INTERFACE, adapter_get_path(na->adapter));

g_slist_free_full(na->servers, server_free);
-
- adapters = g_slist_remove(adapters, na);
adapter_free(na);
}

@@ -762,23 +746,21 @@ static struct network_adapter *create_adapter(struct btd_adapter *adapter)
int network_server_probe(struct btd_server *server)
{
struct btd_adapter *adapter = btd_server_get_adapter(server);
+ struct btd_server *bnep_server;
struct network_adapter *na;
struct network_server *ns;
- const char *path = adapter_get_path(adapter);
const char *uuid = btd_server_get_profile(server)->remote_uuid;
uint16_t id;

- DBG("path %s uuid %s", path, uuid);
+ DBG("path %s uuid %s", adapter_get_path(adapter), uuid);

id = bnep_service_id(uuid);

- na = find_adapter(adapters, adapter);
- if (!na) {
- na = create_adapter(adapter);
- if (!na)
- return -EINVAL;
- adapters = g_slist_append(adapters, na);
- }
+ bnep_server = btd_adapter_get_server(adapter, BNEP_SVC_UUID);
+ if (bnep_server == NULL)
+ return -EINVAL;
+
+ na = btd_server_get_user_data(bnep_server);

ns = find_server(na, id);
if (ns)
@@ -788,23 +770,6 @@ int network_server_probe(struct btd_server *server)

ns->name = g_strdup("Network service");

- if (g_slist_length(na->servers) > 0)
- goto done;
-
- if (!g_dbus_register_interface(btd_get_dbus_connection(),
- path, NETWORK_SERVER_INTERFACE,
- server_methods, NULL, NULL,
- na, path_unregister)) {
- error("D-Bus failed to register %s interface",
- NETWORK_SERVER_INTERFACE);
- server_free(ns);
- return -1;
- }
-
- DBG("Registered interface %s on path %s", NETWORK_SERVER_INTERFACE,
- path);
-
-done:
bacpy(&ns->src, adapter_get_address(adapter));
ns->id = id;
ns->na = na;
@@ -817,6 +782,7 @@ done:
void network_server_remove(struct btd_server *server)
{
struct btd_adapter *adapter = btd_server_get_adapter(server);
+ struct btd_server *bnep_server;
struct network_adapter *na;
struct network_server *ns;
const char *uuid = btd_server_get_profile(server)->remote_uuid;
@@ -826,32 +792,47 @@ void network_server_remove(struct btd_server *server)

id = bnep_service_id(uuid);

- na = find_adapter(adapters, adapter);
- if (!na)
+ bnep_server = btd_adapter_get_server(adapter, BNEP_SVC_UUID);
+ if (bnep_server == NULL)
return;

+ na = btd_server_get_user_data(bnep_server);
+
ns = find_server(na, id);
if (!ns)
return;

na->servers = g_slist_remove(na->servers, ns);
server_free(ns);
-
- if (g_slist_length(na->servers) > 0)
- return;
-
- g_dbus_unregister_interface(btd_get_dbus_connection(),
- adapter_get_path(adapter),
- NETWORK_SERVER_INTERFACE);
}

static int bnep_server_probe(struct btd_server *server)
{
struct btd_adapter *adapter = btd_server_get_adapter(server);
const char *path = adapter_get_path(adapter);
+ struct network_adapter *na;

DBG("path %s", path);

+ na = create_adapter(adapter);
+ if (na == NULL)
+ return -EINVAL;
+
+ if (!g_dbus_register_interface(btd_get_dbus_connection(),
+ path, NETWORK_SERVER_INTERFACE,
+ server_methods, NULL, NULL,
+ na, path_unregister)) {
+ error("D-Bus failed to register %s interface",
+ NETWORK_SERVER_INTERFACE);
+ adapter_free(na);
+ return -EIO;
+ }
+
+ DBG("Registered interface %s on path %s", NETWORK_SERVER_INTERFACE,
+ path);
+
+ btd_server_set_user_data(server, na);
+
return 0;
}

@@ -860,6 +841,10 @@ static void bnep_server_remove(struct btd_server *server)
struct btd_adapter *adapter = btd_server_get_adapter(server);

DBG("path %s", adapter_get_path(adapter));
+
+ g_dbus_unregister_interface(btd_get_dbus_connection(),
+ adapter_get_path(adapter),
+ NETWORK_SERVER_INTERFACE);
}

static struct btd_profile bnep_profile = {
--
1.8.1.4


2013-06-25 16:24:45

by Mikel Astiz

[permalink] [raw]
Subject: [RFC BlueZ v0 12/16] network: Bypass manager for profile server

From: Mikel Astiz <[email protected]>

Trivially adopt the btd_profile signature regarding adapter probe in
server.h in order to avoid boilerplate code in manager.c.
---
profiles/network/manager.c | 72 ++++------------------------------------------
profiles/network/server.c | 32 ++++++++++++++-------
profiles/network/server.h | 4 +--
3 files changed, 30 insertions(+), 78 deletions(-)

diff --git a/profiles/network/manager.c b/profiles/network/manager.c
index 1ec2db2..4f897dd 100644
--- a/profiles/network/manager.c
+++ b/profiles/network/manager.c
@@ -75,66 +75,6 @@ done:
conf_security ? "true" : "false");
}

-static int panu_server_probe(struct btd_server *server)
-{
- struct btd_adapter *adapter = btd_server_get_adapter(server);
- const char *path = adapter_get_path(adapter);
-
- DBG("path %s", path);
-
- return server_register(adapter, BNEP_SVC_PANU);
-}
-
-static void panu_server_remove(struct btd_server *server)
-{
- struct btd_adapter *adapter = btd_server_get_adapter(server);
- const char *path = adapter_get_path(adapter);
-
- DBG("path %s", path);
-
- server_unregister(adapter, BNEP_SVC_PANU);
-}
-
-static int gn_server_probe(struct btd_server *server)
-{
- struct btd_adapter *adapter = btd_server_get_adapter(server);
- const char *path = adapter_get_path(adapter);
-
- DBG("path %s", path);
-
- return server_register(adapter, BNEP_SVC_GN);
-}
-
-static void gn_server_remove(struct btd_server *server)
-{
- struct btd_adapter *adapter = btd_server_get_adapter(server);
- const char *path = adapter_get_path(adapter);
-
- DBG("path %s", path);
-
- server_unregister(adapter, BNEP_SVC_GN);
-}
-
-static int nap_server_probe(struct btd_server *server)
-{
- struct btd_adapter *adapter = btd_server_get_adapter(server);
- const char *path = adapter_get_path(adapter);
-
- DBG("path %s", path);
-
- return server_register(adapter, BNEP_SVC_NAP);
-}
-
-static void nap_server_remove(struct btd_server *server)
-{
- struct btd_adapter *adapter = btd_server_get_adapter(server);
- const char *path = adapter_get_path(adapter);
-
- DBG("path %s", path);
-
- server_unregister(adapter, BNEP_SVC_NAP);
-}
-
static struct btd_profile panu_profile = {
.name = "network-panu",
.local_uuid = NAP_UUID,
@@ -143,8 +83,8 @@ static struct btd_profile panu_profile = {
.device_remove = connection_unregister,
.connect = connection_connect,
.disconnect = connection_disconnect,
- .adapter_probe = panu_server_probe,
- .adapter_remove = panu_server_remove,
+ .adapter_probe = network_server_probe,
+ .adapter_remove = network_server_remove,
};

static struct btd_profile gn_profile = {
@@ -155,8 +95,8 @@ static struct btd_profile gn_profile = {
.device_remove = connection_unregister,
.connect = connection_connect,
.disconnect = connection_disconnect,
- .adapter_probe = gn_server_probe,
- .adapter_remove = gn_server_remove,
+ .adapter_probe = network_server_probe,
+ .adapter_remove = network_server_remove,
};

static struct btd_profile nap_profile = {
@@ -167,8 +107,8 @@ static struct btd_profile nap_profile = {
.device_remove = connection_unregister,
.connect = connection_connect,
.disconnect = connection_disconnect,
- .adapter_probe = nap_server_probe,
- .adapter_remove = nap_server_remove,
+ .adapter_probe = network_server_probe,
+ .adapter_remove = network_server_remove,
};

static int network_init(void)
diff --git a/profiles/network/server.c b/profiles/network/server.c
index ace31c5..a0b7754 100644
--- a/profiles/network/server.c
+++ b/profiles/network/server.c
@@ -43,6 +43,8 @@
#include "lib/uuid.h"
#include "../src/dbus-common.h"
#include "../src/adapter.h"
+#include "../src/profile.h"
+#include "../src/server.h"

#include "log.h"
#include "error.h"
@@ -786,11 +788,18 @@ static struct network_adapter *create_adapter(struct btd_adapter *adapter)
return na;
}

-int server_register(struct btd_adapter *adapter, uint16_t id)
+int network_server_probe(struct btd_server *server)
{
+ struct btd_adapter *adapter = btd_server_get_adapter(server);
struct network_adapter *na;
struct network_server *ns;
- const char *path;
+ const char *path = adapter_get_path(adapter);
+ const char *uuid = btd_server_get_profile(server)->remote_uuid;
+ uint16_t id;
+
+ DBG("path %s uuid %s", path, uuid);
+
+ id = bnep_service_id(uuid);

na = find_adapter(adapters, adapter);
if (!na) {
@@ -808,8 +817,6 @@ int server_register(struct btd_adapter *adapter, uint16_t id)

ns->name = g_strdup("Network service");

- path = adapter_get_path(adapter);
-
if (g_slist_length(na->servers) > 0)
goto done;

@@ -836,28 +843,33 @@ done:
return 0;
}

-int server_unregister(struct btd_adapter *adapter, uint16_t id)
+void network_server_remove(struct btd_server *server)
{
+ struct btd_adapter *adapter = btd_server_get_adapter(server);
struct network_adapter *na;
struct network_server *ns;
+ const char *uuid = btd_server_get_profile(server)->remote_uuid;
+ uint16_t id;
+
+ DBG("path %s uuid %s", adapter_get_path(adapter), uuid);
+
+ id = bnep_service_id(uuid);

na = find_adapter(adapters, adapter);
if (!na)
- return -EINVAL;
+ return;

ns = find_server(na, id);
if (!ns)
- return -EINVAL;
+ return;

na->servers = g_slist_remove(na->servers, ns);
server_free(ns);

if (g_slist_length(na->servers) > 0)
- return 0;
+ return;

g_dbus_unregister_interface(btd_get_dbus_connection(),
adapter_get_path(adapter),
NETWORK_SERVER_INTERFACE);
-
- return 0;
}
diff --git a/profiles/network/server.h b/profiles/network/server.h
index 2edd342..2d8b41c 100644
--- a/profiles/network/server.h
+++ b/profiles/network/server.h
@@ -23,5 +23,5 @@

int server_init(gboolean secure);
void server_exit(void);
-int server_register(struct btd_adapter *adapter, uint16_t id);
-int server_unregister(struct btd_adapter *adapter, uint16_t id);
+int network_server_probe(struct btd_server *server);
+void network_server_remove(struct btd_server *server);
--
1.8.1.4


2013-06-25 16:24:49

by Mikel Astiz

[permalink] [raw]
Subject: [RFC BlueZ v0 16/16] network: Use btd_server userdata for network_server

From: Mikel Astiz <[email protected]>

Exploit the userdata pointer to avoid maintainig a list with instances
of struct network_server.
---
profiles/network/server.c | 56 ++++++++++-------------------------------------
1 file changed, 11 insertions(+), 45 deletions(-)

diff --git a/profiles/network/server.c b/profiles/network/server.c
index 77e2c7e..8ca9013 100644
--- a/profiles/network/server.c
+++ b/profiles/network/server.c
@@ -68,7 +68,6 @@ struct network_adapter {
struct btd_adapter *adapter; /* Adapter pointer */
GIOChannel *io; /* Bnep socket */
struct network_session *setup; /* Setup in progress */
- GSList *servers; /* Server register to adapter */
};

/* Main server structure */
@@ -88,16 +87,13 @@ static gboolean security = TRUE;
static struct network_server *find_server(struct network_adapter *na,
uint16_t id)
{
- GSList *list;
-
- for (list = na->servers; list; list = list->next) {
- struct network_server *ns = list->data;
+ const char *uuid = bnep_uuid(id);
+ struct btd_server *server = btd_adapter_get_server(na->adapter, uuid);

- if (ns->id == id)
- return ns;
- }
+ if (server == NULL)
+ return NULL;

- return NULL;
+ return btd_server_get_user_data(server);
}

static sdp_record_t *server_record_new(const char *name, uint16_t id)
@@ -676,10 +672,8 @@ static void adapter_free(struct network_adapter *na)
g_free(na);
}

-static void server_free(void *data)
+static void server_free(struct network_server *ns)
{
- struct network_server *ns = data;
-
if (!ns)
return;

@@ -701,7 +695,6 @@ static void path_unregister(void *data)
DBG("Unregistered interface %s on path %s",
NETWORK_SERVER_INTERFACE, adapter_get_path(na->adapter));

- g_slist_free_full(na->servers, server_free);
adapter_free(na);
}

@@ -747,34 +740,23 @@ int network_server_probe(struct btd_server *server)
{
struct btd_adapter *adapter = btd_server_get_adapter(server);
struct btd_server *bnep_server;
- struct network_adapter *na;
struct network_server *ns;
const char *uuid = btd_server_get_profile(server)->remote_uuid;
- uint16_t id;

DBG("path %s uuid %s", adapter_get_path(adapter), uuid);

- id = bnep_service_id(uuid);
-
bnep_server = btd_adapter_get_server(adapter, BNEP_SVC_UUID);
if (bnep_server == NULL)
return -EINVAL;

- na = btd_server_get_user_data(bnep_server);
-
- ns = find_server(na, id);
- if (ns)
- return 0;
-
ns = g_new0(struct network_server, 1);
-
ns->name = g_strdup("Network service");
-
bacpy(&ns->src, adapter_get_address(adapter));
- ns->id = id;
- ns->na = na;
+ ns->id = bnep_service_id(uuid);
+ ns->na = btd_server_get_user_data(bnep_server);
ns->record_id = 0;
- na->servers = g_slist_append(na->servers, ns);
+
+ btd_server_set_user_data(server, ns);

return 0;
}
@@ -782,27 +764,11 @@ int network_server_probe(struct btd_server *server)
void network_server_remove(struct btd_server *server)
{
struct btd_adapter *adapter = btd_server_get_adapter(server);
- struct btd_server *bnep_server;
- struct network_adapter *na;
- struct network_server *ns;
+ struct network_server *ns = btd_server_get_user_data(server);
const char *uuid = btd_server_get_profile(server)->remote_uuid;
- uint16_t id;

DBG("path %s uuid %s", adapter_get_path(adapter), uuid);

- id = bnep_service_id(uuid);
-
- bnep_server = btd_adapter_get_server(adapter, BNEP_SVC_UUID);
- if (bnep_server == NULL)
- return;
-
- na = btd_server_get_user_data(bnep_server);
-
- ns = find_server(na, id);
- if (!ns)
- return;
-
- na->servers = g_slist_remove(na->servers, ns);
server_free(ns);
}

--
1.8.1.4


2013-06-25 16:24:47

by Mikel Astiz

[permalink] [raw]
Subject: [RFC BlueZ v0 14/16] network: Add a dedicated btd_profile for BNEP

From: Mikel Astiz <[email protected]>

The profile is currently a dummy profile.
---
profiles/network/manager.c | 6 +++---
profiles/network/server.c | 47 +++++++++++++++++++++++++++++++++++-----------
profiles/network/server.h | 4 ++--
3 files changed, 41 insertions(+), 16 deletions(-)

diff --git a/profiles/network/manager.c b/profiles/network/manager.c
index 4f897dd..ca1e797 100644
--- a/profiles/network/manager.c
+++ b/profiles/network/manager.c
@@ -127,7 +127,7 @@ static int network_init(void)
* field that defines which service the source is connecting to.
*/

- if (server_init(conf_security) < 0)
+ if (network_server_init(conf_security) < 0)
return -1;

btd_profile_register(&panu_profile);
@@ -139,12 +139,12 @@ static int network_init(void)

static void network_exit(void)
{
- server_exit();
-
btd_profile_unregister(&panu_profile);
btd_profile_unregister(&gn_profile);
btd_profile_unregister(&nap_profile);

+ network_server_exit();
+
bnep_cleanup();
}

diff --git a/profiles/network/server.c b/profiles/network/server.c
index 02a74ab..0e52db0 100644
--- a/profiles/network/server.c
+++ b/profiles/network/server.c
@@ -555,17 +555,6 @@ drop:
g_io_channel_shutdown(chan, TRUE, NULL);
}

-int server_init(gboolean secure)
-{
- security = secure;
-
- return 0;
-}
-
-void server_exit(void)
-{
-}
-
static uint32_t register_server_record(struct network_server *ns)
{
sdp_record_t *record;
@@ -855,3 +844,39 @@ void network_server_remove(struct btd_server *server)
adapter_get_path(adapter),
NETWORK_SERVER_INTERFACE);
}
+
+static int bnep_server_probe(struct btd_server *server)
+{
+ struct btd_adapter *adapter = btd_server_get_adapter(server);
+ const char *path = adapter_get_path(adapter);
+
+ DBG("path %s", path);
+
+ return 0;
+}
+
+static void bnep_server_remove(struct btd_server *server)
+{
+ struct btd_adapter *adapter = btd_server_get_adapter(server);
+
+ DBG("path %s", adapter_get_path(adapter));
+}
+
+static struct btd_profile bnep_profile = {
+ .name = "network-bnep",
+ .local_uuid = BNEP_SVC_UUID,
+ .adapter_probe = bnep_server_probe,
+ .adapter_remove = bnep_server_remove,
+};
+
+int network_server_init(gboolean secure)
+{
+ security = secure;
+
+ return btd_profile_register(&bnep_profile);
+}
+
+void network_server_exit(void)
+{
+ btd_profile_unregister(&bnep_profile);
+}
diff --git a/profiles/network/server.h b/profiles/network/server.h
index 2d8b41c..438f74a 100644
--- a/profiles/network/server.h
+++ b/profiles/network/server.h
@@ -21,7 +21,7 @@
*
*/

-int server_init(gboolean secure);
-void server_exit(void);
+int network_server_init(gboolean secure);
+void network_server_exit(void);
int network_server_probe(struct btd_server *server);
void network_server_remove(struct btd_server *server);
--
1.8.1.4


2013-06-25 16:24:46

by Mikel Astiz

[permalink] [raw]
Subject: [RFC BlueZ v0 13/16] network: Simplify search-by-UUID

From: Mikel Astiz <[email protected]>

The helper function bnep_service_id() provides enough functionality to
avoid a specific helper function to search by UUID.
---
profiles/network/server.c | 22 ++--------------------
1 file changed, 2 insertions(+), 20 deletions(-)

diff --git a/profiles/network/server.c b/profiles/network/server.c
index a0b7754..02a74ab 100644
--- a/profiles/network/server.c
+++ b/profiles/network/server.c
@@ -114,24 +114,6 @@ static struct network_server *find_server(struct network_adapter *na,
return NULL;
}

-static struct network_server *find_server_by_uuid(struct network_adapter *na,
- const char *uuid)
-{
- GSList *list;
-
- for (list = na->servers; list; list = list->next) {
- struct network_server *ns = list->data;
-
- if (strcasecmp(uuid, bnep_uuid(ns->id)) == 0)
- return ns;
-
- if (strcasecmp(uuid, bnep_name(ns->id)) == 0)
- return ns;
- }
-
- return NULL;
-}
-
static sdp_record_t *server_record_new(const char *name, uint16_t id)
{
sdp_list_t *svclass, *pfseq, *apseq, *root, *aproto;
@@ -655,7 +637,7 @@ static DBusMessage *register_server(DBusConnection *conn,
DBUS_TYPE_STRING, &bridge, DBUS_TYPE_INVALID))
return btd_error_invalid_args(msg);

- ns = find_server_by_uuid(na, uuid);
+ ns = find_server(na, bnep_service_id(uuid));
if (ns == NULL)
return btd_error_failed(msg, "Invalid UUID");

@@ -692,7 +674,7 @@ static DBusMessage *unregister_server(DBusConnection *conn,
DBUS_TYPE_INVALID))
return btd_error_invalid_args(msg);

- ns = find_server_by_uuid(na, uuid);
+ ns = find_server(na, bnep_service_id(uuid));
if (!ns)
return btd_error_failed(msg, "Invalid UUID");

--
1.8.1.4


2013-06-25 16:24:44

by Mikel Astiz

[permalink] [raw]
Subject: [RFC BlueZ v0 11/16] network: Replace list with network_adapter

From: Mikel Astiz <[email protected]>

Trivially refactor the code such that find_server() receives a pointer
to struct network_adapter instead of a GSList, given that the codebase
always searches within the network_adapter->servers list.
---
profiles/network/server.c | 25 +++++++++++++++----------
1 file changed, 15 insertions(+), 10 deletions(-)

diff --git a/profiles/network/server.c b/profiles/network/server.c
index de48bec..ace31c5 100644
--- a/profiles/network/server.c
+++ b/profiles/network/server.c
@@ -97,9 +97,12 @@ static struct network_adapter *find_adapter(GSList *list,
return NULL;
}

-static struct network_server *find_server(GSList *list, uint16_t id)
+static struct network_server *find_server(struct network_adapter *na,
+ uint16_t id)
{
- for (; list; list = list->next) {
+ GSList *list;
+
+ for (list = na->servers; list; list = list->next) {
struct network_server *ns = list->data;

if (ns->id == id)
@@ -109,10 +112,12 @@ static struct network_server *find_server(GSList *list, uint16_t id)
return NULL;
}

-static struct network_server *find_server_by_uuid(GSList *list,
+static struct network_server *find_server_by_uuid(struct network_adapter *na,
const char *uuid)
{
- for (; list; list = list->next) {
+ GSList *list;
+
+ for (list = na->servers; list; list = list->next) {
struct network_server *ns = list->data;

if (strcasecmp(uuid, bnep_uuid(ns->id)) == 0)
@@ -441,7 +446,7 @@ static gboolean bnep_setup(GIOChannel *chan,

rsp = BNEP_CONN_NOT_ALLOWED;

- ns = find_server(na->servers, dst_role);
+ ns = find_server(na, dst_role);
if (!ns) {
error("Server unavailable: (0x%x)", dst_role);
goto reply;
@@ -538,7 +543,7 @@ static void confirm_event(GIOChannel *chan, gpointer user_data)
goto drop;
}

- ns = find_server(na->servers, BNEP_SVC_NAP);
+ ns = find_server(na, BNEP_SVC_NAP);
if (!ns)
goto drop;

@@ -648,7 +653,7 @@ static DBusMessage *register_server(DBusConnection *conn,
DBUS_TYPE_STRING, &bridge, DBUS_TYPE_INVALID))
return btd_error_invalid_args(msg);

- ns = find_server_by_uuid(na->servers, uuid);
+ ns = find_server_by_uuid(na, uuid);
if (ns == NULL)
return btd_error_failed(msg, "Invalid UUID");

@@ -685,7 +690,7 @@ static DBusMessage *unregister_server(DBusConnection *conn,
DBUS_TYPE_INVALID))
return btd_error_invalid_args(msg);

- ns = find_server_by_uuid(na->servers, uuid);
+ ns = find_server_by_uuid(na, uuid);
if (!ns)
return btd_error_failed(msg, "Invalid UUID");

@@ -795,7 +800,7 @@ int server_register(struct btd_adapter *adapter, uint16_t id)
adapters = g_slist_append(adapters, na);
}

- ns = find_server(na->servers, id);
+ ns = find_server(na, id);
if (ns)
return 0;

@@ -840,7 +845,7 @@ int server_unregister(struct btd_adapter *adapter, uint16_t id)
if (!na)
return -EINVAL;

- ns = find_server(na->servers, id);
+ ns = find_server(na, id);
if (!ns)
return -EINVAL;

--
1.8.1.4


2013-06-25 16:24:42

by Mikel Astiz

[permalink] [raw]
Subject: [RFC BlueZ v0 09/16] heartrate: Remove boilerplate code

From: Mikel Astiz <[email protected]>

The helper fuctions to adapt btd_profile signatures with internal APIs
add boilerplate code of little use, so remove them.
---
profiles/heartrate/heartrate.c | 20 ++++----------------
1 file changed, 4 insertions(+), 16 deletions(-)

diff --git a/profiles/heartrate/heartrate.c b/profiles/heartrate/heartrate.c
index 1d902a8..81ca2a8 100644
--- a/profiles/heartrate/heartrate.c
+++ b/profiles/heartrate/heartrate.c
@@ -724,8 +724,9 @@ static const GDBusPropertyTable heartrate_device_properties[] = {
{ }
};

-static int heartrate_adapter_register(struct btd_adapter *adapter)
+static int heartrate_adapter_probe(struct btd_server *server)
{
+ struct btd_adapter *adapter = btd_server_get_adapter(server);
struct heartrate_adapter *hradapter;

hradapter = g_new0(struct heartrate_adapter, 1);
@@ -748,8 +749,9 @@ static int heartrate_adapter_register(struct btd_adapter *adapter)
return 0;
}

-static void heartrate_adapter_unregister(struct btd_adapter *adapter)
+static void heartrate_adapter_remove(struct btd_server *server)
{
+ struct btd_adapter *adapter = btd_server_get_adapter(server);
struct heartrate_adapter *hradapter;

hradapter = find_heartrate_adapter(adapter);
@@ -831,20 +833,6 @@ static void heartrate_device_unregister(struct btd_device *device)
device_get_path(device), HEART_RATE_INTERFACE);
}

-static int heartrate_adapter_probe(struct btd_server *server)
-{
- struct btd_adapter *adapter = btd_server_get_adapter(server);
-
- return heartrate_adapter_register(adapter);
-}
-
-static void heartrate_adapter_remove(struct btd_server *server)
-{
- struct btd_adapter *adapter = btd_server_get_adapter(server);
-
- heartrate_adapter_unregister(adapter);
-}
-
static int heartrate_device_probe(struct btd_service *service)
{
struct btd_device *device = btd_service_get_device(service);
--
1.8.1.4


2013-06-25 16:24:43

by Mikel Astiz

[permalink] [raw]
Subject: [RFC BlueZ v0 10/16] heartrate: Use btd_server userdata

From: Mikel Astiz <[email protected]>

Exploit the userdata pointer to avoid maintainig a list with instances
of struct heartrate_adapter.
---
profiles/heartrate/heartrate.c | 34 ++++++++--------------------------
1 file changed, 8 insertions(+), 26 deletions(-)

diff --git a/profiles/heartrate/heartrate.c b/profiles/heartrate/heartrate.c
index 81ca2a8..99721352f 100644
--- a/profiles/heartrate/heartrate.c
+++ b/profiles/heartrate/heartrate.c
@@ -94,8 +94,6 @@ struct measurement {
uint16_t *interval;
};

-static GSList *heartrate_adapters = NULL;
-
static const char * const location_enum[] = {
"other",
"chest",
@@ -116,17 +114,6 @@ static const char *location2str(uint8_t value)
return NULL;
}

-static int cmp_adapter(gconstpointer a, gconstpointer b)
-{
- const struct heartrate_adapter *hradapter = a;
- const struct btd_adapter *adapter = b;
-
- if (adapter == hradapter->adapter)
- return 0;
-
- return -1;
-}
-
static int cmp_device(gconstpointer a, gconstpointer b)
{
const struct heartrate *hr = a;
@@ -154,12 +141,13 @@ static int cmp_watcher(gconstpointer a, gconstpointer b)
static struct heartrate_adapter *
find_heartrate_adapter(struct btd_adapter *adapter)
{
- GSList *l = g_slist_find_custom(heartrate_adapters, adapter,
- cmp_adapter);
- if (!l)
+ struct btd_server *server;
+
+ server = btd_adapter_get_server(adapter, HEART_RATE_UUID);
+ if (server == NULL)
return NULL;

- return l->data;
+ return btd_server_get_user_data(server);
}

static void destroy_watcher(gpointer user_data)
@@ -744,21 +732,14 @@ static int heartrate_adapter_probe(struct btd_server *server)
return -EIO;
}

- heartrate_adapters = g_slist_prepend(heartrate_adapters, hradapter);
+ btd_server_set_user_data(server, hradapter);

return 0;
}

static void heartrate_adapter_remove(struct btd_server *server)
{
- struct btd_adapter *adapter = btd_server_get_adapter(server);
- struct heartrate_adapter *hradapter;
-
- hradapter = find_heartrate_adapter(adapter);
- if (hradapter == NULL)
- return;
-
- heartrate_adapters = g_slist_remove(heartrate_adapters, hradapter);
+ struct heartrate_adapter *hradapter = btd_server_get_user_data(server);

g_dbus_unregister_interface(btd_get_dbus_connection(),
adapter_get_path(hradapter->adapter),
@@ -854,6 +835,7 @@ static void heartrate_device_remove(struct btd_service *service)

static struct btd_profile hrp_profile = {
.name = "Heart Rate GATT Driver",
+ .local_uuid = HEART_RATE_UUID,
.remote_uuid = HEART_RATE_UUID,

.device_probe = heartrate_device_probe,
--
1.8.1.4


2013-06-25 16:24:40

by Mikel Astiz

[permalink] [raw]
Subject: [RFC BlueZ v0 07/16] thermometer: Use btd_server userdata

From: Mikel Astiz <[email protected]>

Exploit the userdata pointer to avoid maintainig a list with instances
of struct thermometer_adapter.
---
profiles/thermometer/thermometer.c | 34 ++++++++--------------------------
1 file changed, 8 insertions(+), 26 deletions(-)

diff --git a/profiles/thermometer/thermometer.c b/profiles/thermometer/thermometer.c
index 034e50b..f1aa9e0 100644
--- a/profiles/thermometer/thermometer.c
+++ b/profiles/thermometer/thermometer.c
@@ -119,8 +119,6 @@ struct tmp_interval_data {
uint16_t interval;
};

-static GSList *thermometer_adapters = NULL;
-
static const char * const temp_type[] = {
"<reserved>",
"armpit",
@@ -191,17 +189,6 @@ static void destroy_thermometer_adapter(gpointer user_data)
g_free(tadapter);
}

-static int cmp_adapter(gconstpointer a, gconstpointer b)
-{
- const struct thermometer_adapter *tadapter = a;
- const struct btd_adapter *adapter = b;
-
- if (adapter == tadapter->adapter)
- return 0;
-
- return -1;
-}
-
static int cmp_device(gconstpointer a, gconstpointer b)
{
const struct thermometer *t = a;
@@ -229,12 +216,13 @@ static int cmp_watcher(gconstpointer a, gconstpointer b)
static struct thermometer_adapter *
find_thermometer_adapter(struct btd_adapter *adapter)
{
- GSList *l = g_slist_find_custom(thermometer_adapters, adapter,
- cmp_adapter);
- if (!l)
+ struct btd_server *server;
+
+ server = btd_adapter_get_server(adapter, HEALTH_THERMOMETER_UUID);
+ if (server == NULL)
return NULL;

- return l->data;
+ return btd_server_get_user_data(server);
}

static void change_property(struct thermometer *t, const char *name,
@@ -1269,21 +1257,14 @@ static int thermometer_adapter_probe(struct btd_server *server)
return -EIO;
}

- thermometer_adapters = g_slist_prepend(thermometer_adapters, tadapter);
+ btd_server_set_user_data(server, tadapter);

return 0;
}

static void thermometer_adapter_remove(struct btd_server *server)
{
- struct btd_adapter *adapter = btd_server_get_adapter(server);
- struct thermometer_adapter *tadapter;
-
- tadapter = find_thermometer_adapter(adapter);
- if (tadapter == NULL)
- return;
-
- thermometer_adapters = g_slist_remove(thermometer_adapters, tadapter);
+ struct thermometer_adapter *tadapter = btd_server_get_user_data(server);

g_dbus_unregister_interface(btd_get_dbus_connection(),
adapter_get_path(tadapter->adapter),
@@ -1292,6 +1273,7 @@ static void thermometer_adapter_remove(struct btd_server *server)

static struct btd_profile thermometer_profile = {
.name = "Health Thermometer GATT driver",
+ .local_uuid = HEALTH_THERMOMETER_UUID,
.remote_uuid = HEALTH_THERMOMETER_UUID,
.device_probe = thermometer_device_probe,
.device_remove = thermometer_device_remove,
--
1.8.1.4


2013-06-25 16:24:41

by Mikel Astiz

[permalink] [raw]
Subject: [RFC BlueZ v0 08/16] cyclingspeed: Use btd_server userdata

From: Mikel Astiz <[email protected]>

Exploit the userdata pointer to avoid maintainig a list with instances
of struct csc_adapter.
---
profiles/cyclingspeed/cyclingspeed.c | 32 +++++++-------------------------
1 file changed, 7 insertions(+), 25 deletions(-)

diff --git a/profiles/cyclingspeed/cyclingspeed.c b/profiles/cyclingspeed/cyclingspeed.c
index 14c2df7..8b7e2e1 100644
--- a/profiles/cyclingspeed/cyclingspeed.c
+++ b/profiles/cyclingspeed/cyclingspeed.c
@@ -138,8 +138,6 @@ struct characteristic {
char uuid[MAX_LEN_UUID_STR + 1];
};

-static GSList *csc_adapters = NULL;
-
static const char * const location_enum[] = {
"other", "top-of-shoe", "in-shoe", "hip", "front-wheel", "left-crank",
"right-crank", "left-pedal", "right-pedal", "front-hub",
@@ -167,17 +165,6 @@ static int str2location(const char *location)
return -1;
}

-static int cmp_adapter(gconstpointer a, gconstpointer b)
-{
- const struct csc_adapter *cadapter = a;
- const struct btd_adapter *adapter = b;
-
- if (adapter == cadapter->adapter)
- return 0;
-
- return -1;
-}
-
static int cmp_device(gconstpointer a, gconstpointer b)
{
const struct csc *csc = a;
@@ -204,12 +191,13 @@ static int cmp_watcher(gconstpointer a, gconstpointer b)

static struct csc_adapter *find_csc_adapter(struct btd_adapter *adapter)
{
- GSList *l = g_slist_find_custom(csc_adapters, adapter, cmp_adapter);
+ struct btd_server *server;

- if (!l)
+ server = btd_adapter_get_server(adapter, CYCLING_SC_UUID);
+ if (server == NULL)
return NULL;

- return l->data;
+ return btd_server_get_user_data(server);
}

static void destroy_watcher(gpointer user_data)
@@ -978,21 +966,14 @@ static int csc_adapter_probe(struct btd_server *server)
return -EIO;
}

- csc_adapters = g_slist_prepend(csc_adapters, cadapter);
+ btd_server_set_user_data(server, cadapter);

return 0;
}

static void csc_adapter_remove(struct btd_server *server)
{
- struct btd_adapter *adapter = btd_server_get_adapter(server);
- struct csc_adapter *cadapter;
-
- cadapter = find_csc_adapter(adapter);
- if (cadapter == NULL)
- return;
-
- csc_adapters = g_slist_remove(csc_adapters, cadapter);
+ struct csc_adapter *cadapter = btd_server_get_user_data(server);

g_dbus_unregister_interface(btd_get_dbus_connection(),
adapter_get_path(cadapter->adapter),
@@ -1259,6 +1240,7 @@ static void csc_device_remove(struct btd_service *service)

static struct btd_profile cscp_profile = {
.name = "Cycling Speed and Cadence GATT Driver",
+ .local_uuid = CYCLING_SC_UUID,
.remote_uuid = CYCLING_SC_UUID,

.adapter_probe = csc_adapter_probe,
--
1.8.1.4


2013-06-25 16:24:38

by Mikel Astiz

[permalink] [raw]
Subject: [RFC BlueZ v0 05/16] input: Use btd_server userdata for input_server

From: Mikel Astiz <[email protected]>

Avoid maintaining an internal list of probed adapters and use instead
the userdata pointer provided by btd_server.
---
profiles/input/server.c | 23 ++---------------------
1 file changed, 2 insertions(+), 21 deletions(-)

diff --git a/profiles/input/server.c b/profiles/input/server.c
index 077429c..e1dbfba 100644
--- a/profiles/input/server.c
+++ b/profiles/input/server.c
@@ -48,7 +48,6 @@
#include "device.h"
#include "server.h"

-static GSList *servers = NULL;
struct input_server {
bdaddr_t src;
GIOChannel *ctrl;
@@ -56,14 +55,6 @@ struct input_server {
GIOChannel *confirm;
};

-static int server_cmp(gconstpointer s, gconstpointer user_data)
-{
- const struct input_server *server = s;
- const bdaddr_t *src = user_data;
-
- return bacmp(&server->src, src);
-}
-
static void connect_event_cb(GIOChannel *chan, GError *err, gpointer data)
{
uint16_t psm;
@@ -231,23 +222,14 @@ int hid_server_probe(struct btd_server *btd_server)
return -1;
}

- servers = g_slist_append(servers, server);
+ btd_server_set_user_data(btd_server, server);

return 0;
}

void hid_server_remove(struct btd_server *btd_server)
{
- struct btd_adapter *adapter = btd_server_get_adapter(btd_server);
- const bdaddr_t *src = adapter_get_address(adapter);
- struct input_server *server;
- GSList *l;
-
- l = g_slist_find_custom(servers, src, server_cmp);
- if (!l)
- return;
-
- server = l->data;
+ struct input_server *server = btd_server_get_user_data(btd_server);

g_io_channel_shutdown(server->intr, TRUE, NULL);
g_io_channel_unref(server->intr);
@@ -255,6 +237,5 @@ void hid_server_remove(struct btd_server *btd_server)
g_io_channel_shutdown(server->ctrl, TRUE, NULL);
g_io_channel_unref(server->ctrl);

- servers = g_slist_remove(servers, server);
g_free(server);
}
--
1.8.1.4


2013-06-25 16:24:39

by Mikel Astiz

[permalink] [raw]
Subject: [RFC BlueZ v0 06/16] thermometer: Remove boilerplate code

From: Mikel Astiz <[email protected]>

The helper fuctions to adapt btd_profile signatures with internal APIs
add boilerplate code of little use, so remove them.
---
profiles/thermometer/thermometer.c | 51 ++++++++++----------------------------
1 file changed, 13 insertions(+), 38 deletions(-)

diff --git a/profiles/thermometer/thermometer.c b/profiles/thermometer/thermometer.c
index 44f5e79..034e50b 100644
--- a/profiles/thermometer/thermometer.c
+++ b/profiles/thermometer/thermometer.c
@@ -1162,14 +1162,19 @@ static void attio_disconnected_cb(gpointer user_data)
t->attrib = NULL;
}

-static int thermometer_register(struct btd_device *device,
- struct gatt_primary *tattr)
+static int thermometer_device_probe(struct btd_service *service)
{
+ struct btd_device *device = btd_service_get_device(service);
const char *path = device_get_path(device);
+ struct gatt_primary *tattr;
struct thermometer *t;
struct btd_adapter *adapter;
struct thermometer_adapter *tadapter;

+ tattr = btd_device_get_primary(device, HEALTH_THERMOMETER_UUID);
+ if (tattr == NULL)
+ return -EINVAL;
+
adapter = device_get_adapter(device);

tadapter = find_thermometer_adapter(adapter);
@@ -1201,8 +1206,9 @@ static int thermometer_register(struct btd_device *device,
return 0;
}

-static void thermometer_unregister(struct btd_device *device)
+static void thermometer_device_remove(struct btd_service *service)
{
+ struct btd_device *device = btd_service_get_device(service);
struct thermometer *t;
struct btd_adapter *adapter;
struct thermometer_adapter *tadapter;
@@ -1243,8 +1249,9 @@ static const GDBusMethodTable thermometer_manager_methods[] = {
{ }
};

-static int thermometer_adapter_register(struct btd_adapter *adapter)
+static int thermometer_adapter_probe(struct btd_server *server)
{
+ struct btd_adapter *adapter = btd_server_get_adapter(server);
struct thermometer_adapter *tadapter;

tadapter = g_new0(struct thermometer_adapter, 1);
@@ -1267,8 +1274,9 @@ static int thermometer_adapter_register(struct btd_adapter *adapter)
return 0;
}

-static void thermometer_adapter_unregister(struct btd_adapter *adapter)
+static void thermometer_adapter_remove(struct btd_server *server)
{
+ struct btd_adapter *adapter = btd_server_get_adapter(server);
struct thermometer_adapter *tadapter;

tadapter = find_thermometer_adapter(adapter);
@@ -1282,39 +1290,6 @@ static void thermometer_adapter_unregister(struct btd_adapter *adapter)
THERMOMETER_MANAGER_INTERFACE);
}

-static int thermometer_device_probe(struct btd_service *service)
-{
- struct btd_device *device = btd_service_get_device(service);
- struct gatt_primary *tattr;
-
- tattr = btd_device_get_primary(device, HEALTH_THERMOMETER_UUID);
- if (tattr == NULL)
- return -EINVAL;
-
- return thermometer_register(device, tattr);
-}
-
-static void thermometer_device_remove(struct btd_service *service)
-{
- struct btd_device *device = btd_service_get_device(service);
-
- thermometer_unregister(device);
-}
-
-static int thermometer_adapter_probe(struct btd_server *server)
-{
- struct btd_adapter *adapter = btd_server_get_adapter(server);
-
- return thermometer_adapter_register(adapter);
-}
-
-static void thermometer_adapter_remove(struct btd_server *server)
-{
- struct btd_adapter *adapter = btd_server_get_adapter(server);
-
- thermometer_adapter_unregister(adapter);
-}
-
static struct btd_profile thermometer_profile = {
.name = "Health Thermometer GATT driver",
.remote_uuid = HEALTH_THERMOMETER_UUID,
--
1.8.1.4


2013-06-25 16:24:36

by Mikel Astiz

[permalink] [raw]
Subject: [RFC BlueZ v0 03/16] profile: Use btd_server to probe adapters

From: Mikel Astiz <[email protected]>

Modify the btd_profile callbacks to receive a btd_server pointer,
trivially replacing the previous parameters.

This allows the profile implementations to set the userdata pointer
during probe, in case they want to associate a private data type to the
probed adapter (which is fairly common).
---
profiles/alert/server.c | 9 ++++----
profiles/audio/manager.c | 41 ++++++++++++++++++++++--------------
profiles/cyclingspeed/cyclingspeed.c | 8 ++++---
profiles/health/hdp_manager.c | 11 ++++++----
profiles/heartrate/heartrate.c | 11 ++++++----
profiles/input/manager.c | 10 ++++++---
profiles/network/manager.c | 22 +++++++++++--------
profiles/proximity/reporter.c | 8 ++++---
profiles/proximity/reporter.h | 5 ++---
profiles/sap/manager.c | 9 ++++----
profiles/thermometer/thermometer.c | 11 ++++++----
profiles/time/server.c | 8 ++++---
src/profile.c | 11 ++++++----
src/profile.h | 7 +++---
src/server.c | 5 ++---
15 files changed, 105 insertions(+), 71 deletions(-)

diff --git a/profiles/alert/server.c b/profiles/alert/server.c
index 8da1928..4f84669 100644
--- a/profiles/alert/server.c
+++ b/profiles/alert/server.c
@@ -45,6 +45,7 @@
#include "attrib-server.h"
#include "attrib/gatt.h"
#include "profile.h"
+#include "server.h"
#include "error.h"
#include "textfile.h"
#include "attio.h"
@@ -927,9 +928,9 @@ static void register_alert_notif_service(struct alert_adapter *al_adapter)
GATT_OPT_INVALID);
}

-static int alert_server_probe(struct btd_profile *p,
- struct btd_adapter *adapter)
+static int alert_server_probe(struct btd_server *server)
{
+ struct btd_adapter *adapter = btd_server_get_adapter(server);
struct alert_adapter *al_adapter;

al_adapter = g_new0(struct alert_adapter, 1);
@@ -943,9 +944,9 @@ static int alert_server_probe(struct btd_profile *p,
return 0;
}

-static void alert_server_remove(struct btd_profile *p,
- struct btd_adapter *adapter)
+static void alert_server_remove(struct btd_server *server)
{
+ struct btd_adapter *adapter = btd_server_get_adapter(server);
struct alert_adapter *al_adapter;

al_adapter = find_alert_adapter(adapter);
diff --git a/profiles/audio/manager.c b/profiles/audio/manager.c
index 7f02fbd..23d0ee4 100644
--- a/profiles/audio/manager.c
+++ b/profiles/audio/manager.c
@@ -54,6 +54,7 @@
#include "../src/device.h"
#include "../src/profile.h"
#include "../src/service.h"
+#include "../src/server.h"

#include "log.h"
#include "device.h"
@@ -272,65 +273,73 @@ static int avrcp_target_disconnect(struct btd_service *service)
return control_disconnect(audio_dev);
}

-static int a2dp_source_server_probe(struct btd_profile *p,
- struct btd_adapter *adapter)
+static int a2dp_source_server_probe(struct btd_server *server)
{
+ struct btd_adapter *adapter = btd_server_get_adapter(server);
+
DBG("path %s", adapter_get_path(adapter));

return a2dp_source_register(adapter, config);
}

-static void a2dp_source_server_remove(struct btd_profile *p,
- struct btd_adapter *adapter)
+static void a2dp_source_server_remove(struct btd_server *server)
{
+ struct btd_adapter *adapter = btd_server_get_adapter(server);
+
DBG("path %s", adapter_get_path(adapter));

a2dp_source_unregister(adapter);
}

-static int a2dp_sink_server_probe(struct btd_profile *p,
- struct btd_adapter *adapter)
+static int a2dp_sink_server_probe(struct btd_server *server)
{
+ struct btd_adapter *adapter = btd_server_get_adapter(server);
+
DBG("path %s", adapter_get_path(adapter));

return a2dp_sink_register(adapter, config);
}

-static void a2dp_sink_server_remove(struct btd_profile *p,
- struct btd_adapter *adapter)
+static void a2dp_sink_server_remove(struct btd_server *server)
{
+ struct btd_adapter *adapter = btd_server_get_adapter(server);
+
DBG("path %s", adapter_get_path(adapter));

a2dp_sink_unregister(adapter);
}

-static int avrcp_target_server_probe(struct btd_profile *p,
- struct btd_adapter *adapter)
+static int avrcp_target_server_probe(struct btd_server *server)
{
+ struct btd_adapter *adapter = btd_server_get_adapter(server);
+
DBG("path %s", adapter_get_path(adapter));

return avrcp_target_register(adapter, config);
}

-static int avrcp_remote_server_probe(struct btd_profile *p,
- struct btd_adapter *adapter)
+static int avrcp_remote_server_probe(struct btd_server *server)
{
+ struct btd_adapter *adapter = btd_server_get_adapter(server);
+
DBG("path %s", adapter_get_path(adapter));

return avrcp_remote_register(adapter, config);
}

-static void avrcp_target_server_remove(struct btd_profile *p,
- struct btd_adapter *adapter)
+static void avrcp_target_server_remove(struct btd_server *server)
{
+ struct btd_adapter *adapter = btd_server_get_adapter(server);
+
DBG("path %s", adapter_get_path(adapter));

avrcp_target_unregister(adapter);
}

-static void avrcp_remote_server_remove(struct btd_profile *p,
- struct btd_adapter *adapter)
+static void avrcp_remote_server_remove(struct btd_server *server)
{
+ struct btd_adapter *adapter = btd_server_get_adapter(server);
+
DBG("path %s", adapter_get_path(adapter));

avrcp_remote_unregister(adapter);
diff --git a/profiles/cyclingspeed/cyclingspeed.c b/profiles/cyclingspeed/cyclingspeed.c
index ea6076c..14c2df7 100644
--- a/profiles/cyclingspeed/cyclingspeed.c
+++ b/profiles/cyclingspeed/cyclingspeed.c
@@ -35,6 +35,7 @@
#include "device.h"
#include "profile.h"
#include "service.h"
+#include "server.h"
#include "dbus-common.h"
#include "error.h"
#include "attrib/gattrib.h"
@@ -957,8 +958,9 @@ static const GDBusMethodTable cyclingspeed_manager_methods[] = {
{ }
};

-static int csc_adapter_probe(struct btd_profile *p, struct btd_adapter *adapter)
+static int csc_adapter_probe(struct btd_server *server)
{
+ struct btd_adapter *adapter = btd_server_get_adapter(server);
struct csc_adapter *cadapter;

cadapter = g_new0(struct csc_adapter, 1);
@@ -981,9 +983,9 @@ static int csc_adapter_probe(struct btd_profile *p, struct btd_adapter *adapter)
return 0;
}

-static void csc_adapter_remove(struct btd_profile *p,
- struct btd_adapter *adapter)
+static void csc_adapter_remove(struct btd_server *server)
{
+ struct btd_adapter *adapter = btd_server_get_adapter(server);
struct csc_adapter *cadapter;

cadapter = find_csc_adapter(adapter);
diff --git a/profiles/health/hdp_manager.c b/profiles/health/hdp_manager.c
index 1bb6007..06cf33f 100644
--- a/profiles/health/hdp_manager.c
+++ b/profiles/health/hdp_manager.c
@@ -35,6 +35,7 @@
#include <device.h>
#include <profile.h>
#include <service.h>
+#include <server.h>
#include <glib-helper.h>
#include <log.h>

@@ -43,15 +44,17 @@
#include "hdp_manager.h"
#include "hdp.h"

-static int hdp_adapter_probe(struct btd_profile *p,
- struct btd_adapter *adapter)
+static int hdp_adapter_probe(struct btd_server *server)
{
+ struct btd_adapter *adapter = btd_server_get_adapter(server);
+
return hdp_adapter_register(adapter);
}

-static void hdp_adapter_remove(struct btd_profile *p,
- struct btd_adapter *adapter)
+static void hdp_adapter_remove(struct btd_server *server)
{
+ struct btd_adapter *adapter = btd_server_get_adapter(server);
+
hdp_adapter_unregister(adapter);
}

diff --git a/profiles/heartrate/heartrate.c b/profiles/heartrate/heartrate.c
index 34ec9bc..1d902a8 100644
--- a/profiles/heartrate/heartrate.c
+++ b/profiles/heartrate/heartrate.c
@@ -36,6 +36,7 @@
#include "device.h"
#include "profile.h"
#include "service.h"
+#include "server.h"
#include "error.h"
#include "attrib/gattrib.h"
#include "attrib/att.h"
@@ -830,15 +831,17 @@ static void heartrate_device_unregister(struct btd_device *device)
device_get_path(device), HEART_RATE_INTERFACE);
}

-static int heartrate_adapter_probe(struct btd_profile *p,
- struct btd_adapter *adapter)
+static int heartrate_adapter_probe(struct btd_server *server)
{
+ struct btd_adapter *adapter = btd_server_get_adapter(server);
+
return heartrate_adapter_register(adapter);
}

-static void heartrate_adapter_remove(struct btd_profile *p,
- struct btd_adapter *adapter)
+static void heartrate_adapter_remove(struct btd_server *server)
{
+ struct btd_adapter *adapter = btd_server_get_adapter(server);
+
heartrate_adapter_unregister(adapter);
}

diff --git a/profiles/input/manager.c b/profiles/input/manager.c
index 689ccdd..4c32919 100644
--- a/profiles/input/manager.c
+++ b/profiles/input/manager.c
@@ -40,18 +40,22 @@
#include "../src/device.h"
#include "../src/profile.h"
#include "../src/service.h"
+#include "../src/server.h"

#include "device.h"
#include "server.h"

-static int hid_server_probe(struct btd_profile *p, struct btd_adapter *adapter)
+static int hid_server_probe(struct btd_server *server)
{
+ struct btd_adapter *adapter = btd_server_get_adapter(server);
+
return server_start(adapter_get_address(adapter));
}

-static void hid_server_remove(struct btd_profile *p,
- struct btd_adapter *adapter)
+static void hid_server_remove(struct btd_server *server)
{
+ struct btd_adapter *adapter = btd_server_get_adapter(server);
+
server_stop(adapter_get_address(adapter));
}

diff --git a/profiles/network/manager.c b/profiles/network/manager.c
index 03b1b3d..1ec2db2 100644
--- a/profiles/network/manager.c
+++ b/profiles/network/manager.c
@@ -42,6 +42,7 @@
#include "device.h"
#include "profile.h"
#include "service.h"
+#include "../src/server.h"
#include "common.h"
#include "connection.h"
#include "server.h"
@@ -74,8 +75,9 @@ done:
conf_security ? "true" : "false");
}

-static int panu_server_probe(struct btd_profile *p, struct btd_adapter *adapter)
+static int panu_server_probe(struct btd_server *server)
{
+ struct btd_adapter *adapter = btd_server_get_adapter(server);
const char *path = adapter_get_path(adapter);

DBG("path %s", path);
@@ -83,9 +85,9 @@ static int panu_server_probe(struct btd_profile *p, struct btd_adapter *adapter)
return server_register(adapter, BNEP_SVC_PANU);
}

-static void panu_server_remove(struct btd_profile *p,
- struct btd_adapter *adapter)
+static void panu_server_remove(struct btd_server *server)
{
+ struct btd_adapter *adapter = btd_server_get_adapter(server);
const char *path = adapter_get_path(adapter);

DBG("path %s", path);
@@ -93,8 +95,9 @@ static void panu_server_remove(struct btd_profile *p,
server_unregister(adapter, BNEP_SVC_PANU);
}

-static int gn_server_probe(struct btd_profile *p, struct btd_adapter *adapter)
+static int gn_server_probe(struct btd_server *server)
{
+ struct btd_adapter *adapter = btd_server_get_adapter(server);
const char *path = adapter_get_path(adapter);

DBG("path %s", path);
@@ -102,9 +105,9 @@ static int gn_server_probe(struct btd_profile *p, struct btd_adapter *adapter)
return server_register(adapter, BNEP_SVC_GN);
}

-static void gn_server_remove(struct btd_profile *p,
- struct btd_adapter *adapter)
+static void gn_server_remove(struct btd_server *server)
{
+ struct btd_adapter *adapter = btd_server_get_adapter(server);
const char *path = adapter_get_path(adapter);

DBG("path %s", path);
@@ -112,8 +115,9 @@ static void gn_server_remove(struct btd_profile *p,
server_unregister(adapter, BNEP_SVC_GN);
}

-static int nap_server_probe(struct btd_profile *p, struct btd_adapter *adapter)
+static int nap_server_probe(struct btd_server *server)
{
+ struct btd_adapter *adapter = btd_server_get_adapter(server);
const char *path = adapter_get_path(adapter);

DBG("path %s", path);
@@ -121,9 +125,9 @@ static int nap_server_probe(struct btd_profile *p, struct btd_adapter *adapter)
return server_register(adapter, BNEP_SVC_NAP);
}

-static void nap_server_remove(struct btd_profile *p,
- struct btd_adapter *adapter)
+static void nap_server_remove(struct btd_server *server)
{
+ struct btd_adapter *adapter = btd_server_get_adapter(server);
const char *path = adapter_get_path(adapter);

DBG("path %s", path);
diff --git a/profiles/proximity/reporter.c b/profiles/proximity/reporter.c
index dbb593a..3f6ba75 100644
--- a/profiles/proximity/reporter.c
+++ b/profiles/proximity/reporter.c
@@ -43,6 +43,7 @@
#include "device.h"
#include "profile.h"
#include "service.h"
+#include "server.h"
#include "hcid.h"
#include "attrib/gattrib.h"
#include "attrib/att.h"
@@ -232,8 +233,9 @@ void reporter_device_remove(struct btd_service *service)
unregister_reporter_device(device, radapter);
}

-int reporter_adapter_probe(struct btd_profile *p, struct btd_adapter *adapter)
+int reporter_adapter_probe(struct btd_server *server)
{
+ struct btd_adapter *adapter = btd_server_get_adapter(server);
struct reporter_adapter *radapter;

radapter = g_new0(struct reporter_adapter, 1);
@@ -249,9 +251,9 @@ int reporter_adapter_probe(struct btd_profile *p, struct btd_adapter *adapter)
return 0;
}

-void reporter_adapter_remove(struct btd_profile *p,
- struct btd_adapter *adapter)
+void reporter_adapter_remove(struct btd_server *server)
{
+ struct btd_adapter *adapter = btd_server_get_adapter(server);
struct reporter_adapter *radapter = find_reporter_adapter(adapter);
if (!radapter)
return;
diff --git a/profiles/proximity/reporter.h b/profiles/proximity/reporter.h
index a8e1aac..4f94e2b 100644
--- a/profiles/proximity/reporter.h
+++ b/profiles/proximity/reporter.h
@@ -39,8 +39,7 @@ enum {
void reporter_device_remove(struct btd_service *service);
int reporter_device_probe(struct btd_service *service);

-int reporter_adapter_probe(struct btd_profile *p, struct btd_adapter *adapter);
-void reporter_adapter_remove(struct btd_profile *p,
- struct btd_adapter *adapter);
+int reporter_adapter_probe(struct btd_server *server);
+void reporter_adapter_remove(struct btd_server *server);

const char *get_alert_level_string(uint8_t level);
diff --git a/profiles/sap/manager.c b/profiles/sap/manager.c
index 24b73e7..4d6cfdc 100644
--- a/profiles/sap/manager.c
+++ b/profiles/sap/manager.c
@@ -29,12 +29,14 @@
#include "device.h"
#include "profile.h"
#include "service.h"
+#include "../src/server.h"

#include "manager.h"
#include "server.h"

-static int sap_server_probe(struct btd_profile *p, struct btd_adapter *adapter)
+static int sap_server_probe(struct btd_server *server)
{
+ struct btd_adapter *adapter = btd_server_get_adapter(server);
const char *path = adapter_get_path(adapter);

DBG("path %s", path);
@@ -42,10 +44,9 @@ static int sap_server_probe(struct btd_profile *p, struct btd_adapter *adapter)
return sap_server_register(path, adapter_get_address(adapter));
}

-static void sap_server_remove(struct btd_profile *p,
- struct btd_adapter *adapter)
+static void sap_server_remove(struct btd_server *server)
{
- const char *path = adapter_get_path(adapter);
+ const char *path = adapter_get_path(btd_server_get_adapter(server));

DBG("path %s", path);

diff --git a/profiles/thermometer/thermometer.c b/profiles/thermometer/thermometer.c
index c2ca251..44f5e79 100644
--- a/profiles/thermometer/thermometer.c
+++ b/profiles/thermometer/thermometer.c
@@ -36,6 +36,7 @@
#include "device.h"
#include "profile.h"
#include "service.h"
+#include "server.h"
#include "error.h"
#include "log.h"
#include "attrib/gattrib.h"
@@ -1300,15 +1301,17 @@ static void thermometer_device_remove(struct btd_service *service)
thermometer_unregister(device);
}

-static int thermometer_adapter_probe(struct btd_profile *p,
- struct btd_adapter *adapter)
+static int thermometer_adapter_probe(struct btd_server *server)
{
+ struct btd_adapter *adapter = btd_server_get_adapter(server);
+
return thermometer_adapter_register(adapter);
}

-static void thermometer_adapter_remove(struct btd_profile *p,
- struct btd_adapter *adapter)
+static void thermometer_adapter_remove(struct btd_server *server)
{
+ struct btd_adapter *adapter = btd_server_get_adapter(server);
+
thermometer_adapter_unregister(adapter);
}

diff --git a/profiles/time/server.c b/profiles/time/server.c
index 518a29f..b5f14ce 100644
--- a/profiles/time/server.c
+++ b/profiles/time/server.c
@@ -34,6 +34,7 @@
#include <adapter.h>
#include <device.h>
#include <profile.h>
+#include <server.h>
#include <plugin.h>

#include "lib/uuid.h"
@@ -230,8 +231,9 @@ static gboolean register_ref_time_update_service(struct btd_adapter *adapter)
GATT_OPT_INVALID);
}

-static int time_server_init(struct btd_profile *p, struct btd_adapter *adapter)
+static int time_server_init(struct btd_server *server)
{
+ struct btd_adapter *adapter = btd_server_get_adapter(server);
const char *path = adapter_get_path(adapter);

DBG("path %s", path);
@@ -249,9 +251,9 @@ static int time_server_init(struct btd_profile *p, struct btd_adapter *adapter)
return 0;
}

-static void time_server_exit(struct btd_profile *p,
- struct btd_adapter *adapter)
+static void time_server_exit(struct btd_server *server)
{
+ struct btd_adapter *adapter = btd_server_get_adapter(server);
const char *path = adapter_get_path(adapter);

DBG("path %s", path);
diff --git a/src/profile.c b/src/profile.c
index c745811..ae18887 100644
--- a/src/profile.c
+++ b/src/profile.c
@@ -51,6 +51,7 @@
#include "device.h"
#include "profile.h"
#include "service.h"
+#include "server.h"

#define DUN_DEFAULT_CHANNEL 1
#define SPP_DEFAULT_CHANNEL 3
@@ -1349,9 +1350,10 @@ static struct ext_profile *find_ext(struct btd_profile *p)
return l->data;
}

-static int ext_adapter_probe(struct btd_profile *p,
- struct btd_adapter *adapter)
+static int ext_adapter_probe(struct btd_server *server)
{
+ struct btd_adapter *adapter = btd_server_get_adapter(server);
+ struct btd_profile *p = btd_server_get_profile(server);
struct ext_profile *ext;
struct ext_record *rec;
uint32_t handle;
@@ -1396,9 +1398,10 @@ static void ext_remove_records(struct ext_profile *ext,
}
}

-static void ext_adapter_remove(struct btd_profile *p,
- struct btd_adapter *adapter)
+static void ext_adapter_remove(struct btd_server *server)
{
+ struct btd_adapter *adapter = btd_server_get_adapter(server);
+ struct btd_profile *p = btd_server_get_profile(server);
struct ext_profile *ext;
GSList *l, *next;

diff --git a/src/profile.h b/src/profile.h
index 9aec27e..683e3a9 100644
--- a/src/profile.h
+++ b/src/profile.h
@@ -25,6 +25,7 @@
#define BTD_PROFILE_PRIORITY_MEDIUM 1
#define BTD_PROFILE_PRIORITY_HIGH 2

+struct btd_server;
struct btd_service;

struct btd_profile {
@@ -42,10 +43,8 @@ struct btd_profile {
int (*connect) (struct btd_service *service);
int (*disconnect) (struct btd_service *service);

- int (*adapter_probe) (struct btd_profile *p,
- struct btd_adapter *adapter);
- void (*adapter_remove) (struct btd_profile *p,
- struct btd_adapter *adapter);
+ int (*adapter_probe) (struct btd_server *server);
+ void (*adapter_remove) (struct btd_server *server);
};

void btd_profile_foreach(void (*func)(struct btd_profile *p, void *data),
diff --git a/src/server.c b/src/server.c
index 9337ff6..7c5b445 100644
--- a/src/server.c
+++ b/src/server.c
@@ -67,7 +67,7 @@ struct btd_server *server_create(struct btd_adapter *adapter,
server->adapter = adapter; /* Weak ref */
server->profile = profile;

- err = profile->adapter_probe(server->profile, server->adapter);
+ err = profile->adapter_probe(server);
if (err == 0)
return server;

@@ -82,8 +82,7 @@ struct btd_server *server_create(struct btd_adapter *adapter,
void server_destroy(struct btd_server *server)
{
if (server->profile->adapter_remove != NULL)
- server->profile->adapter_remove(server->profile,
- server->adapter);
+ server->profile->adapter_remove(server);

g_free(server);
}
--
1.8.1.4


2013-06-25 16:24:37

by Mikel Astiz

[permalink] [raw]
Subject: [RFC BlueZ v0 04/16] input: Bypass manager for profile server

From: Mikel Astiz <[email protected]>

Trivially adopt the btd_profile signature in server.h in order to avoid
boilerplate code in manager.c.
---
profiles/input/manager.c | 14 --------------
profiles/input/server.c | 9 +++++++--
profiles/input/server.h | 4 ++--
3 files changed, 9 insertions(+), 18 deletions(-)

diff --git a/profiles/input/manager.c b/profiles/input/manager.c
index 4c32919..7c627aa 100644
--- a/profiles/input/manager.c
+++ b/profiles/input/manager.c
@@ -45,20 +45,6 @@
#include "device.h"
#include "server.h"

-static int hid_server_probe(struct btd_server *server)
-{
- struct btd_adapter *adapter = btd_server_get_adapter(server);
-
- return server_start(adapter_get_address(adapter));
-}
-
-static void hid_server_remove(struct btd_server *server)
-{
- struct btd_adapter *adapter = btd_server_get_adapter(server);
-
- server_stop(adapter_get_address(adapter));
-}
-
static struct btd_profile input_profile = {
.name = "input-hid",
.local_uuid = HID_UUID,
diff --git a/profiles/input/server.c b/profiles/input/server.c
index 5c98573..077429c 100644
--- a/profiles/input/server.c
+++ b/profiles/input/server.c
@@ -43,6 +43,7 @@
#include "../src/adapter.h"
#include "../src/device.h"
#include "../src/profile.h"
+#include "../src/server.h"

#include "device.h"
#include "server.h"
@@ -193,8 +194,10 @@ drop:
g_io_channel_shutdown(chan, TRUE, NULL);
}

-int server_start(const bdaddr_t *src)
+int hid_server_probe(struct btd_server *btd_server)
{
+ struct btd_adapter *adapter = btd_server_get_adapter(btd_server);
+ const bdaddr_t *src = adapter_get_address(adapter);
struct input_server *server;
GError *err = NULL;

@@ -233,8 +236,10 @@ int server_start(const bdaddr_t *src)
return 0;
}

-void server_stop(const bdaddr_t *src)
+void hid_server_remove(struct btd_server *btd_server)
{
+ struct btd_adapter *adapter = btd_server_get_adapter(btd_server);
+ const bdaddr_t *src = adapter_get_address(adapter);
struct input_server *server;
GSList *l;

diff --git a/profiles/input/server.h b/profiles/input/server.h
index 74159bb..8eefc5f 100644
--- a/profiles/input/server.h
+++ b/profiles/input/server.h
@@ -21,5 +21,5 @@
*
*/

-int server_start(const bdaddr_t *src);
-void server_stop(const bdaddr_t *src);
+int hid_server_probe(struct btd_server *btd_server);
+void hid_server_remove(struct btd_server *btd_server);
--
1.8.1.4


2013-06-25 16:24:35

by Mikel Astiz

[permalink] [raw]
Subject: [RFC BlueZ v0 02/16] adapter: Create btd_server instances when probing

From: Mikel Astiz <[email protected]>

Instead of storing a list of probed btd_profile instances, maintain a
similar list but with btd_server instances instead. This makes use of
server_create() which guarantees that all existing instances of
btd_server represent a probed (adapter, profile) pair.
---
src/adapter.c | 50 ++++++++++++++++++++++++++++----------------------
src/adapter.h | 2 ++
2 files changed, 30 insertions(+), 22 deletions(-)

diff --git a/src/adapter.c b/src/adapter.c
index 17f5508..246b510 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -57,6 +57,7 @@
#include "adapter.h"
#include "device.h"
#include "profile.h"
+#include "server.h"
#include "dbus-common.h"
#include "error.h"
#include "glib-helper.h"
@@ -177,7 +178,7 @@ struct btd_adapter {
GSList *pin_callbacks;

GSList *drivers;
- GSList *profiles;
+ GSList *servers;

struct oob_handler *oob_handler;

@@ -2616,18 +2617,16 @@ static void load_drivers(struct btd_adapter *adapter)
static void probe_profile(struct btd_profile *profile, void *data)
{
struct btd_adapter *adapter = data;
- int err;
+ struct btd_server *server;

if (profile->adapter_probe == NULL)
return;

- err = profile->adapter_probe(profile, adapter);
- if (err < 0) {
- error("%s: %s (%d)", profile->name, strerror(-err), -err);
+ server = server_create(adapter, profile);
+ if (server == NULL)
return;
- }

- adapter->profiles = g_slist_prepend(adapter->profiles, profile);
+ adapter->servers = g_slist_prepend(adapter->servers, server);
}

void adapter_add_profile(struct btd_adapter *adapter, gpointer p)
@@ -2645,6 +2644,7 @@ void adapter_add_profile(struct btd_adapter *adapter, gpointer p)
void adapter_remove_profile(struct btd_adapter *adapter, gpointer p)
{
struct btd_profile *profile = p;
+ struct btd_server *server;

if (!adapter->initialized)
return;
@@ -2652,10 +2652,26 @@ void adapter_remove_profile(struct btd_adapter *adapter, gpointer p)
if (profile->device_remove)
g_slist_foreach(adapter->devices, device_remove_profile, p);

- adapter->profiles = g_slist_remove(adapter->profiles, profile);
+ server = btd_adapter_get_server(adapter, profile->local_uuid);
+ adapter->servers = g_slist_remove(adapter->servers, server);

- if (profile->adapter_remove)
- profile->adapter_remove(profile, adapter);
+ server_destroy(server);
+}
+
+struct btd_server *btd_adapter_get_server(struct btd_adapter *adapter,
+ const char *local_uuid)
+{
+ GSList *l;
+
+ for (l = adapter->servers; l != NULL; l = g_slist_next(l)) {
+ struct btd_server *server = l->data;
+ struct btd_profile *p = btd_server_get_profile(server);
+
+ if (g_strcmp0(p->local_uuid, local_uuid) == 0)
+ return server;
+ }
+
+ return NULL;
}

static void adapter_add_connection(struct btd_adapter *adapter,
@@ -2868,24 +2884,14 @@ static void remove_driver(gpointer data, gpointer user_data)
driver->remove(adapter);
}

-static void remove_profile(gpointer data, gpointer user_data)
-{
- struct btd_profile *profile = data;
- struct btd_adapter *adapter = user_data;
-
- if (profile->adapter_remove)
- profile->adapter_remove(profile, adapter);
-}
-
static void unload_drivers(struct btd_adapter *adapter)
{
g_slist_foreach(adapter->drivers, remove_driver, adapter);
g_slist_free(adapter->drivers);
adapter->drivers = NULL;

- g_slist_foreach(adapter->profiles, remove_profile, adapter);
- g_slist_free(adapter->profiles);
- adapter->profiles = NULL;
+ g_slist_free_full(adapter->servers, (GDestroyNotify) server_destroy);
+ adapter->servers = NULL;
}

static void free_service_auth(gpointer data, gpointer user_data)
diff --git a/src/adapter.h b/src/adapter.h
index 32b12c0..7fdf055 100644
--- a/src/adapter.h
+++ b/src/adapter.h
@@ -123,6 +123,8 @@ typedef void (*service_auth_cb) (DBusError *derr, void *user_data);

void adapter_add_profile(struct btd_adapter *adapter, gpointer p);
void adapter_remove_profile(struct btd_adapter *adapter, gpointer p);
+struct btd_server *btd_adapter_get_server(struct btd_adapter *adapter,
+ const char *local_uuid);
int btd_register_adapter_driver(struct btd_adapter_driver *driver);
void btd_unregister_adapter_driver(struct btd_adapter_driver *driver);
guint btd_request_authorization(const bdaddr_t *src, const bdaddr_t *dst,
--
1.8.1.4


2013-06-25 16:24:34

by Mikel Astiz

[permalink] [raw]
Subject: [RFC BlueZ v0 01/16] core: Add btd_server

From: Mikel Astiz <[email protected]>

Add a core object representing a (adapter, profile) pair in a similar
way that btd_service couples a (device, profile) pair. As opposed to
btd_service, which represents the *remote* support of a profile,
btd_server is introduced to represent a *locally* supported profile.

The new struct should allow removing a lot of boilerplate code in the
profile implementations, such as;
- Maintaining an internal list of probed adapters.
- The agent authorization procedure.
- IO channel handling (listen, shutdown, error-cases, etc.).

Eventually, btd_server is also a step towards supporting multiple local
instances of the same UUID, which could be probed/removed independently.
This extension is however not addressed yet.
---
Makefile.am | 1 +
src/server.c | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
src/server.h | 38 +++++++++++++++++++++
3 files changed, 148 insertions(+)
create mode 100644 src/server.c
create mode 100644 src/server.h

diff --git a/Makefile.am b/Makefile.am
index 80edafd..9aa21c0 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -143,6 +143,7 @@ src_bluetoothd_SOURCES = $(gdbus_sources) $(builtin_sources) \
src/adapter.h src/adapter.c \
src/profile.h src/profile.c \
src/service.h src/service.c \
+ src/server.h src/server.c \
src/device.h src/device.c src/attio.h \
src/dbus-common.c src/dbus-common.h \
src/eir.h src/eir.c \
diff --git a/src/server.c b/src/server.c
new file mode 100644
index 0000000..9337ff6
--- /dev/null
+++ b/src/server.c
@@ -0,0 +1,109 @@
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2012-2013 BMW Car IT GmbH. All rights reserved.
+ *
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <assert.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <stdbool.h>
+#include <sys/stat.h>
+#include <sys/ioctl.h>
+#include <errno.h>
+
+#include <bluetooth/bluetooth.h>
+
+#include <glib.h>
+
+#include "log.h"
+
+#include "adapter.h"
+#include "profile.h"
+#include "server.h"
+
+struct btd_server {
+ struct btd_adapter *adapter;
+ struct btd_profile *profile;
+ void *user_data;
+};
+
+struct btd_server *server_create(struct btd_adapter *adapter,
+ struct btd_profile *profile)
+{
+ struct btd_server *server;
+ char addr[18];
+ int err;
+
+ server = g_try_new0(struct btd_server, 1);
+ if (!server) {
+ error("server_create: failed to alloc memory");
+ return NULL;
+ }
+
+ server->adapter = adapter; /* Weak ref */
+ server->profile = profile;
+
+ err = profile->adapter_probe(server->profile, server->adapter);
+ if (err == 0)
+ return server;
+
+ ba2str(adapter_get_address(server->adapter), addr);
+ error("%s profile probe failed for %s (%d)", profile->name, addr, err);
+
+ g_free(server);
+
+ return NULL;
+}
+
+void server_destroy(struct btd_server *server)
+{
+ if (server->profile->adapter_remove != NULL)
+ server->profile->adapter_remove(server->profile,
+ server->adapter);
+
+ g_free(server);
+}
+
+struct btd_adapter *btd_server_get_adapter(const struct btd_server *server)
+{
+ return server->adapter;
+}
+
+struct btd_profile *btd_server_get_profile(const struct btd_server *server)
+{
+ return server->profile;
+}
+
+void btd_server_set_user_data(struct btd_server *server, void *user_data)
+{
+ server->user_data = user_data;
+}
+
+void *btd_server_get_user_data(const struct btd_server *server)
+{
+ return server->user_data;
+}
diff --git a/src/server.h b/src/server.h
new file mode 100644
index 0000000..9fd4891
--- /dev/null
+++ b/src/server.h
@@ -0,0 +1,38 @@
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2012-2013 BMW Car IT GmbH. All rights reserved.
+ *
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+struct btd_adapter;
+struct btd_profile;
+
+/* Server management functions used by the core */
+struct btd_server *server_create(struct btd_adapter *adapter,
+ struct btd_profile *profile);
+void server_destroy(struct btd_server *server);
+
+/* Public member access */
+struct btd_adapter *btd_server_get_adapter(const struct btd_server *server);
+struct btd_profile *btd_server_get_profile(const struct btd_server *server);
+
+/* Functions used by profile implementation */
+void btd_server_set_user_data(struct btd_server *server, void *user_data);
+void *btd_server_get_user_data(const struct btd_server *server);
--
1.8.1.4


2013-07-02 07:11:50

by Mikel Astiz

[permalink] [raw]
Subject: Re: [RFC BlueZ v0 00/16] Introduce btd_server

On Tue, Jun 25, 2013 at 6:24 PM, Mikel Astiz <[email protected]> wrote:
> From: Mikel Astiz <[email protected]>
>
> This RFC presents a proposal to extend the BlueZ core to simplify the implementation of profiles. It introduces a new core concept (btd_server) as a first step towards providing internal infrastructure in a similar way that external profiles have. This exact purpose of the new struct is described in patch 01.
>
> Currently, it's paradoxical that externally implemented profiles are in a way simpler to implement. There is a lot of helpful code in profile.c which could be reused for internal profiles too.
>
> This first patchset consists of three main parts:
> 1. The extension of the core (patches 01..03).
> 2. Some simple examples of adoption (patches 04..10).
> 3. A more complex adoption (patches 11..16).
>
> This part is fairly simple and doesn't offer great benefits besides the usage of the userdata pointer in btd_server, which can already be handy (see for example 08/16).
>
> Furhter and more interesting changes in the pipeline (but not submitted in v0) include:
> 4. Handling of incoming connections.
> 5. Integration with agent authorization.
> 6. Adoption of new infrastructure for external profiles.
>
> For the record, the result of this whole work does not add more lines of code to maintain, it just moves the code complexity from profile implementations to the core. The result is less duplicated code and data throughout the codebase, hopefully leading to more robust profiles.
>
> Mikel Astiz (16):
> core: Add btd_server
> adapter: Create btd_server instances when probing
> profile: Use btd_server to probe adapters
> input: Bypass manager for profile server
> input: Use btd_server userdata for input_server
> thermometer: Remove boilerplate code
> thermometer: Use btd_server userdata
> cyclingspeed: Use btd_server userdata
> heartrate: Remove boilerplate code
> heartrate: Use btd_server userdata
> network: Replace list with network_adapter
> network: Bypass manager for profile server
> network: Simplify search-by-UUID
> network: Add a dedicated btd_profile for BNEP
> network: Create network_adapter during BNEP probe
> network: Use btd_server userdata for network_server
>
> Makefile.am | 1 +
> profiles/alert/server.c | 9 +-
> profiles/audio/manager.c | 41 +++++---
> profiles/cyclingspeed/cyclingspeed.c | 38 +++----
> profiles/health/hdp_manager.c | 11 ++-
> profiles/heartrate/heartrate.c | 51 +++-------
> profiles/input/manager.c | 12 +--
> profiles/input/server.c | 28 ++----
> profiles/input/server.h | 4 +-
> profiles/network/manager.c | 76 ++------------
> profiles/network/server.c | 185 +++++++++++++++--------------------
> profiles/network/server.h | 8 +-
> profiles/proximity/reporter.c | 8 +-
> profiles/proximity/reporter.h | 5 +-
> profiles/sap/manager.c | 9 +-
> profiles/thermometer/thermometer.c | 82 ++++------------
> profiles/time/server.c | 8 +-
> src/adapter.c | 50 +++++-----
> src/adapter.h | 2 +
> src/profile.c | 11 ++-
> src/profile.h | 7 +-
> src/server.c | 108 ++++++++++++++++++++
> src/server.h | 38 +++++++
> 23 files changed, 389 insertions(+), 403 deletions(-)
> create mode 100644 src/server.c
> create mode 100644 src/server.h
>
> --
> 1.8.1.4
>

Ping.

Side note: if there's missing context information for this patchset,
and the next steps are considered relevant while reviewing this part,
I can also submit the rest or make it available in github.

Cheers,
Mikel