Linux bluetooth development
 help / color / mirror / Atom feed
From: Mikel Astiz <mikel.astiz.oss@gmail.com>
To: linux-bluetooth@vger.kernel.org
Cc: Mikel Astiz <mikel.astiz@bmw-carit.de>
Subject: [RFC v1 1/7] avrcp: Refactor server registration
Date: Wed,  6 Feb 2013 10:16:21 +0100	[thread overview]
Message-ID: <1360142187-15347-2-git-send-email-mikel.astiz.oss@gmail.com> (raw)
In-Reply-To: <1360142187-15347-1-git-send-email-mikel.astiz.oss@gmail.com>

From: Mikel Astiz <mikel.astiz@bmw-carit.de>

Use a helper function to install the AVRCP server, just like other audio
profiles such as in a2dp.c do.
---
 profiles/audio/avrcp.c | 54 ++++++++++++++++++++++++++++++--------------------
 1 file changed, 32 insertions(+), 22 deletions(-)

diff --git a/profiles/audio/avrcp.c b/profiles/audio/avrcp.c
index 00eeea1..277f9f3 100644
--- a/profiles/audio/avrcp.c
+++ b/profiles/audio/avrcp.c
@@ -2681,9 +2681,9 @@ void avrcp_disconnect(struct audio_device *dev)
 	avctp_disconnect(session);
 }
 
-int avrcp_register(struct btd_adapter *adapter, GKeyFile *config)
+static struct avrcp_server *avrcp_server_register(struct btd_adapter *adapter,
+							GKeyFile *config)
 {
-	sdp_record_t *record;
 	gboolean tmp, master = TRUE;
 	GError *err = NULL;
 	struct avrcp_server *server;
@@ -2698,18 +2698,39 @@ int avrcp_register(struct btd_adapter *adapter, GKeyFile *config)
 			master = tmp;
 	}
 
+	if (avctp_register(adapter, master) < 0)
+		return NULL;
+
 	server = g_new0(struct avrcp_server, 1);
+	server->adapter = btd_adapter_ref(adapter);
+
+	servers = g_slist_append(servers, server);
+
+	if (!avctp_id)
+		avctp_id = avctp_add_state_cb(state_changed, NULL);
+
+	return server;
+}
+
+int avrcp_register(struct btd_adapter *adapter, GKeyFile *config)
+{
+	sdp_record_t *record;
+	struct avrcp_server *server;
+
+	server = avrcp_server_register(adapter, config);
+	if (server == NULL)
+		return -EPROTONOSUPPORT;
 
 	record = avrcp_tg_record();
 	if (!record) {
 		error("Unable to allocate new service record");
-		g_free(server);
+		avrcp_unregister(adapter);
 		return -1;
 	}
 
 	if (add_record_to_server(adapter_get_address(adapter), record) < 0) {
 		error("Unable to register AVRCP target service record");
-		g_free(server);
+		avrcp_unregister(adapter);
 		sdp_record_free(record);
 		return -1;
 	}
@@ -2718,32 +2739,18 @@ int avrcp_register(struct btd_adapter *adapter, GKeyFile *config)
 	record = avrcp_ct_record();
 	if (!record) {
 		error("Unable to allocate new service record");
-		g_free(server);
+		avrcp_unregister(adapter);
 		return -1;
 	}
 
 	if (add_record_to_server(adapter_get_address(adapter), record) < 0) {
 		error("Unable to register AVRCP service record");
 		sdp_record_free(record);
-		g_free(server);
+		avrcp_unregister(adapter);
 		return -1;
 	}
 	server->ct_record_id = record->handle;
 
-	if (avctp_register(adapter, master) < 0) {
-		remove_record_from_server(server->ct_record_id);
-		remove_record_from_server(server->tg_record_id);
-		g_free(server);
-		return -1;
-	}
-
-	server->adapter = btd_adapter_ref(adapter);
-
-	servers = g_slist_append(servers, server);
-
-	if (!avctp_id)
-		avctp_id = avctp_add_state_cb(state_changed, NULL);
-
 	return 0;
 }
 
@@ -2760,8 +2767,11 @@ void avrcp_unregister(struct btd_adapter *adapter)
 
 	servers = g_slist_remove(servers, server);
 
-	remove_record_from_server(server->ct_record_id);
-	remove_record_from_server(server->tg_record_id);
+	if (server->ct_record_id != 0)
+		remove_record_from_server(server->ct_record_id);
+
+	if (server->tg_record_id != 0)
+		remove_record_from_server(server->tg_record_id);
 
 	avctp_unregister(server->adapter);
 	btd_adapter_unref(server->adapter);
-- 
1.8.1


  reply	other threads:[~2013-02-06  9:16 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-02-06  9:16 [RFC v1 0/7] One remote UUID per btd_profile Mikel Astiz
2013-02-06  9:16 ` Mikel Astiz [this message]
2013-02-06  9:16 ` [RFC v1 2/7] audio: Split AVRCP into two btd_profile Mikel Astiz
2013-02-07  9:43   ` Luiz Augusto von Dentz
2013-02-07 10:43     ` Mikel Astiz
2013-02-06  9:16 ` [RFC v1 3/7] proximity: Split internal monitor registration API Mikel Astiz
2013-02-14 16:37   ` Claudio Takahasi
2013-02-15  7:54     ` Mikel Astiz
2013-02-27  7:46       ` Mikel Astiz
2013-02-28 19:34       ` Claudio Takahasi
2013-02-06  9:16 ` [RFC v1 4/7] proximity: Split monitor into three btd_profile Mikel Astiz
2013-02-06  9:16 ` [RFC v1 5/7] gatt: List only GATT_UUID as remote UUID Mikel Astiz
2013-02-06  9:16 ` [RFC v1 6/7] health: Split health into two btd_profile Mikel Astiz
2013-02-06  9:16 ` [RFC v1 7/7] profile: Limit to one remote UUID per profile Mikel Astiz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1360142187-15347-2-git-send-email-mikel.astiz.oss@gmail.com \
    --to=mikel.astiz.oss@gmail.com \
    --cc=linux-bluetooth@vger.kernel.org \
    --cc=mikel.astiz@bmw-carit.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox