Linux bluetooth development
 help / color / mirror / Atom feed
From: "Frédéric Dalleau" <frederic.dalleau@linux.intel.com>
To: linux-bluetooth@vger.kernel.org
Cc: "Frédéric Dalleau" <frederic.dalleau@linux.intel.com>
Subject: [PATCH v2 2/9] Add state change callback to HandsfreeGateway
Date: Mon, 22 Aug 2011 17:30:40 +0200	[thread overview]
Message-ID: <1314027047-5476-3-git-send-email-frederic.dalleau@linux.intel.com> (raw)
In-Reply-To: <1314027047-5476-1-git-send-email-frederic.dalleau@linux.intel.com>

Media interface needs to monitor state changes of HandsfreeGateway.
---
 audio/gateway.c |   61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 audio/gateway.h |    7 ++++++
 2 files changed, 68 insertions(+), 0 deletions(-)

diff --git a/audio/gateway.c b/audio/gateway.c
index e9485d0..805b8cb 100644
--- a/audio/gateway.c
+++ b/audio/gateway.c
@@ -72,6 +72,14 @@ struct gateway {
 	int version;
 };
 
+struct gateway_state_callback {
+	gateway_state_cb cb;
+	void *user_data;
+	unsigned int id;
+};
+
+static GSList *gateway_callbacks = NULL;
+
 int gateway_close(struct audio_device *device);
 
 static const char *state2str(gateway_state_t state)
@@ -104,16 +112,37 @@ static void change_state(struct audio_device *dev, gateway_state_t new_state)
 {
 	struct gateway *gw = dev->gateway;
 	const char *val;
+	GSList *l;
+	gateway_state_t old_state;
 
 	if (gw->state == new_state)
 		return;
 
 	val = state2str(new_state);
+	old_state = gw->state;
 	gw->state = new_state;
 
 	emit_property_changed(dev->conn, dev->path,
 			AUDIO_GATEWAY_INTERFACE, "State",
 			DBUS_TYPE_STRING, &val);
+
+	for (l = gateway_callbacks; l != NULL; l = l->next) {
+		struct gateway_state_callback *cb = l->data;
+		cb->cb(dev, old_state, new_state, cb->user_data);
+	}
+}
+
+void gateway_set_state(struct audio_device *dev, gateway_state_t new_state)
+{
+	switch (new_state) {
+	case GATEWAY_STATE_DISCONNECTED:
+		gateway_close(dev);
+		break;
+	case GATEWAY_STATE_CONNECTING:
+	case GATEWAY_STATE_CONNECTED:
+	case GATEWAY_STATE_PLAYING:
+		break;
+	}
 }
 
 static void agent_disconnect(struct audio_device *dev, struct hf_agent *agent)
@@ -810,3 +839,35 @@ void gateway_suspend_stream(struct audio_device *dev)
 	gw->sco_start_cb_data = NULL;
 	change_state(dev, GATEWAY_STATE_CONNECTED);
 }
+
+unsigned int gateway_add_state_cb(gateway_state_cb cb, void *user_data)
+{
+	struct gateway_state_callback *state_cb;
+	static unsigned int id = 0;
+
+	state_cb = g_new(struct gateway_state_callback, 1);
+	state_cb->cb = cb;
+	state_cb->user_data = user_data;
+	state_cb->id = ++id;
+
+	gateway_callbacks = g_slist_append(gateway_callbacks, state_cb);
+
+	return state_cb->id;
+}
+
+gboolean gateway_remove_state_cb(unsigned int id)
+{
+	GSList *l;
+
+	for (l = gateway_callbacks; l != NULL; l = l->next) {
+		struct gateway_state_callback *cb = l->data;
+		if (cb && cb->id == id) {
+			gateway_callbacks = g_slist_remove(gateway_callbacks,
+									cb);
+			g_free(cb);
+			return TRUE;
+		}
+	}
+
+	return FALSE;
+}
diff --git a/audio/gateway.h b/audio/gateway.h
index a45ef82..32b5d6d 100644
--- a/audio/gateway.h
+++ b/audio/gateway.h
@@ -33,9 +33,14 @@ typedef enum {
 	GATEWAY_STATE_PLAYING,
 } gateway_state_t;
 
+typedef void (*gateway_state_cb) (struct audio_device *dev,
+					gateway_state_t old_state,
+					gateway_state_t new_state,
+					void *user_data);
 typedef void (*gateway_stream_cb_t) (struct audio_device *dev, GError *err,
 		void *user_data);
 
+void gateway_set_state(struct audio_device *dev, gateway_state_t new_state);
 void gateway_unregister(struct audio_device *dev);
 struct gateway *gateway_init(struct audio_device *device);
 gboolean gateway_is_connected(struct audio_device *dev);
@@ -49,3 +54,5 @@ int gateway_config_stream(struct audio_device *dev, gateway_stream_cb_t cb,
 gboolean gateway_cancel_stream(struct audio_device *dev, unsigned int id);
 int gateway_get_sco_fd(struct audio_device *dev);
 void gateway_suspend_stream(struct audio_device *dev);
+unsigned int gateway_add_state_cb(gateway_state_cb cb, void *user_data);
+gboolean gateway_remove_state_cb(unsigned int id);
-- 
1.7.1


  parent reply	other threads:[~2011-08-22 15:30 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-08-22 15:30 [PATCH v2 0/9] Add support for HandsfreeGateway to Media Frédéric Dalleau
2011-08-22 15:30 ` [PATCH v2 1/9] Fix double free in error case in endpoint_reply Frédéric Dalleau
2011-08-24 10:48   ` Luiz Augusto von Dentz
2011-08-22 15:30 ` Frédéric Dalleau [this message]
2011-08-24 10:51   ` [PATCH v2 2/9] Add state change callback to HandsfreeGateway Luiz Augusto von Dentz
2011-08-22 15:30 ` [PATCH v2 3/9] Use state change callback from media interface Frédéric Dalleau
2011-08-24 10:53   ` Luiz Augusto von Dentz
2011-08-22 15:30 ` [PATCH v2 4/9] Update g_strcmp0 to strcasecmp Frédéric Dalleau
2011-08-24 10:53   ` Luiz Augusto von Dentz
2011-08-22 15:30 ` [PATCH v2 5/9] Fix RFCOMM disconnect on suspend request Frédéric Dalleau
2011-08-24 10:54   ` Luiz Augusto von Dentz
2011-08-22 15:30 ` [PATCH v2 6/9] Set state to "connecting" when AG connects Frédéric Dalleau
2011-08-24 10:54   ` Luiz Augusto von Dentz
2011-08-22 15:30 ` [PATCH v2 7/9] Fix invalid state at SCO disconnect in gateway Frédéric Dalleau
2011-08-24 10:57   ` Luiz Augusto von Dentz
2011-08-22 15:30 ` [PATCH v2 8/9] Introduce gateway locking mechanism Frédéric Dalleau
2011-08-24 10:55   ` Luiz Augusto von Dentz
2011-08-22 15:30 ` [PATCH v2 9/9] Implement media_transport for HFP_HS_UUID Frédéric Dalleau
2011-08-24 10:59   ` Luiz Augusto von Dentz
2011-08-24 11:24 ` [PATCH v2 0/9] Add support for HandsfreeGateway to Media Johan Hedberg
2011-08-24 12:06   ` Dalleau, Frederic

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=1314027047-5476-3-git-send-email-frederic.dalleau@linux.intel.com \
    --to=frederic.dalleau@linux.intel.com \
    --cc=linux-bluetooth@vger.kernel.org \
    /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