Skip to contentSkip to conversation

Fix disconnect devices after enabling offline mode

PATCH Superseded Rafal Michalski 2 participants 2 messages Jun 1, 2011 Bluetooth drivers

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 Fix disconnect devices after enabling offline mode

Previously paired and connected devices were disconnected automatically after turning bluetooth off directly via bluetooth UI. This patch makes that also other ways of turning bluetooth off (which should lead to disconnecting paired and connected devices), such as enabling offline mode (which turns bluetooth off as well), can be handled properly.

---
 plugins/maemo6.c |    4 ++--
 src/adapter.c    |   47 ++++++++++++++++++++++++++++++++++++++++++++++-
 src/adapter.h    |    2 +-
 3 files changed, 49 insertions(+), 4 deletions(-)
plugins/maemo6.c +2 -2
index 56f2664..d16ac9f 100644
@@ -77,7 +77,7 @@ static gboolean mce_signal_callback(DBusConnection *connection,
 		if (mce_bt_set)
 			btd_adapter_switch_online(adapter);
 		else
-			btd_adapter_switch_offline(adapter);
+			btd_adapter_switch_offline(adapter, TRUE);
 	}
 
 	return TRUE;
@@ -124,7 +124,7 @@ static void read_radio_states_cb(DBusPendingCall *call, void *user_data)
 	if (mce_bt_set)
 		btd_adapter_switch_online(adapter);
 	else
-		btd_adapter_switch_offline(adapter);
+		btd_adapter_switch_offline(adapter, TRUE);
 
 done:
 	dbus_message_unref(reply);
src/adapter.c +46 -1

98 lines not on this page · raw message

src/adapter.h +1 -1
index 3526849..ca40a1a 100644
@@ -161,7 +161,7 @@ gboolean adapter_powering_down(struct btd_adapter *adapter);
 
 int btd_adapter_restore_powered(struct btd_adapter *adapter);
 int btd_adapter_switch_online(struct btd_adapter *adapter);
-int btd_adapter_switch_offline(struct btd_adapter *adapter);
+int btd_adapter_switch_offline(struct btd_adapter *adapter, gboolean clean);
 
 typedef void (*bt_hci_result_t) (uint8_t status, gpointer user_data);
 
Signature · 1 line
1.6.3.3
#1 lore raw

Hi Rafal,

Quoted 10 lines · Rafal Michalski

Previously paired and connected devices were disconnected automatically after turning bluetooth off directly via bluetooth UI. This patch makes that also other ways of turning bluetooth off (which should lead to disconnecting paired and connected devices), such as enabling offline mode (which turns bluetooth off as well), can be handled properly.

---
 plugins/maemo6.c |    4 ++--
 src/adapter.c    |   47 ++++++++++++++++++++++++++++++++++++++++++++++-
 src/adapter.h    |    2 +-
 3 files changed, 49 insertions(+), 4 deletions(-)

A couple more things:

Quoted 12 lines
plugins/maemo6.c +1 -1
@@ -77,7 +77,7 @@ static gboolean mce_signal_callback(DBusConnection *connection,
 		if (mce_bt_set)
 			btd_adapter_switch_online(adapter);
 		else
-			btd_adapter_switch_offline(adapter);
+			btd_adapter_switch_offline(adapter, TRUE);
 	}
 
 	return TRUE;

plugins/maemo6.c static void read_radio_states_cb(DBusPendingCall *call, void *user_data) · line 127

Quoted 5 lines
plugins/maemo6.c +1 -1
@@ -124,7 +124,7 @@ static void read_radio_states_cb(DBusPendingCall *call, void *user_data)
 	if (mce_bt_set)
 		btd_adapter_switch_online(adapter);
 	else
-		btd_adapter_switch_offline(adapter);
+		btd_adapter_switch_offline(adapter, TRUE);

Since plugins/maemo6.c seems to be the only user of this API and always passes TRUE to it, could we just make this the only possible behavior until a use case shows up where an "unclean" switch_offline is needed? I.e. remove the boolean completely.

In general this kind of boolean APIs aren't really the most readable ones since when looking at the function call you have no idea what the boolean actually does. In this kind of cases it might be worth creating an enum (even though you've just got two possible values). Then something like the following is more clear when looking at the function call:

btd_adapter_switch_offline(adapter, SWITCH_OFFLINE_CLEAN);

However just remove the boolean for now and we don't need to even consider this.

plugins/maemo6.c static void read_radio_states_cb(DBusPendingCall *call, void *user_data)

Quoted 5 lines
plugins/maemo6.c +5 -0
+static void off_timer_free(struct btd_adapter *adapter)
+{
+	g_source_remove(adapter->off_timer);
+	adapter->off_timer = 0;
+}

I think off_timer_remove would be a more appropriate name here.

Quoted 4 lines
plugins/maemo6.c +4 -0
+static void disconnect_device(struct btd_device *device, gpointer user_data)
+{
+	device_request_disconnect(device, NULL);
+}

<snip>

plugins/maemo6.c

Quoted 2 lines
plugins/maemo6.c +2 -0
+		g_slist_foreach(adapter->connections,
+					(GFunc) disconnect_device, NULL);

In general try to avoid function typecasts by defining the target function to the exact needed type. However in this case you don't even need an intermediate function if you do the typecast. Just pass device_request_disconnect directly to g_slist_foreach.

Johan