Skip to contentSkip to conversation

adapter: Fix discovery trigger for 0 second delay

PATCH Applied Frédéric Danis 3 participants 7 messages Mar 16, 2021 Bluetooth drivers

lore
Share & tools
lore
#0 lore raw

v5 cover adapter: Fix discovery trigger for 0 second delay

When calling StartDiscovery the effective start can take around 10 ms or up to 700 ms.
g_timeout_add_seconds() call doesn't ensure the time for the first call of the timer if the delay is less or equal to 1 second.

v2: Fix issue found by CI
v3: Add a wrapper function for g_timeout_add_seconds and replace calls to it in
src/*, profiles/* and plugins/*
v4: Fix issue found by CI
v5: Fix issue found by CI

Frédéric Danis (4):
shared/timeout: Add timeout_add_seconds abstraction
src: Replace calls to g_timeout_add_seconds by timeout_add_seconds
plugins: Replace calls to g_timeout_add_seconds by timeout_add_seconds
profiles: Replace calls to g_timeout_add_seconds by
timeout_add_seconds

 plugins/policy.c              | 91 ++++++++++++++++++-----------------
 profiles/audio/a2dp.c         | 17 ++++---
 profiles/audio/avctp.c        | 44 +++++++++--------
 profiles/audio/avdtp.c        | 61 ++++++++++++-----------
 profiles/audio/avrcp.c        | 13 ++---
 profiles/health/hdp.c         | 16 +++---
 profiles/health/mcap.c        | 21 ++++----
 profiles/input/device.c       | 33 +++++++------
 profiles/network/bnep.c       | 12 +++--
 profiles/sap/server.c         | 13 ++---
 src/adapter.c                 | 90 ++++++++++++++++++----------------
 src/adv_monitor.c             | 14 +++---
 src/advertising.c             | 28 ++++++-----
 src/device.c                  | 60 ++++++++++++-----------
 src/main.c                    |  7 +--
 src/sdp-client.c              | 13 ++---
 src/shared/tester.c           | 16 +++---
 src/shared/timeout-ell.c      |  6 +++
 src/shared/timeout-glib.c     | 27 +++++++++++
 src/shared/timeout-mainloop.c |  6 +++
 src/shared/timeout.h          |  3 ++
 21 files changed, 332 insertions(+), 259 deletions(-)

--
2.18.0

#1 lore raw

PATCH v5 1/4 shared/timeout: Add timeout_add_seconds abstraction

g_timeout_add_seconds() call doesn't ensure the time for the first call of the timer if the delay is less or equal to 1 second.
In case of a 0 delay call g_idle_add() instead of g_timeout_add_seconds().

---
 src/shared/tester.c           | 16 +++++++++-------
 src/shared/timeout-ell.c      |  6 ++++++
 src/shared/timeout-glib.c     | 27 +++++++++++++++++++++++++++
 src/shared/timeout-mainloop.c |  6 ++++++
 src/shared/timeout.h          |  3 +++
 5 files changed, 51 insertions(+), 7 deletions(-)
src/shared/tester.c +9 -7
index af33a79cd..c07cbc11c 100644
@@ -36,6 +36,7 @@
 #include "src/shared/util.h"
 #include "src/shared/tester.h"
 #include "src/shared/log.h"
+#include "src/shared/timeout.h"
 
 #define COLOR_OFF	"\x1B[0m"
 #define COLOR_BLACK	"\x1B[0;30m"
@@ -126,7 +127,7 @@ static void test_destroy(gpointer data)
 	struct test_case *test = data;
 
 	if (test->timeout_id > 0)
-		g_source_remove(test->timeout_id);
+		timeout_remove(test->timeout_id);
 
 	if (test->teardown_id > 0)
 		g_source_remove(test->teardown_id);
@@ -429,7 +430,7 @@ static gboolean teardown_callback(gpointer user_data)
 	return FALSE;
 }
 
-static gboolean test_timeout(gpointer user_data)
+static bool test_timeout(gpointer user_data)
 {
 	struct test_case *test = user_data;
 
@@ -470,8 +471,9 @@ static void next_test_case(void)
 	test->start_time = g_timer_elapsed(test_timer, NULL);
 
 	if (test->timeout > 0)
-		test->timeout_id = g_timeout_add_seconds(test->timeout,
-							test_timeout, test);
+		test->timeout_id = timeout_add_seconds(test->timeout,
+							test_timeout, test,
+							NULL);
 
 	test->stage = TEST_STAGE_PRE_SETUP;
 
@@ -542,7 +544,7 @@ void tester_pre_setup_failed(void)
 		return;
 
 	if (test->timeout_id > 0) {
-		g_source_remove(test->timeout_id);
+		timeout_remove(test->timeout_id);
 		test->timeout_id = 0;
 	}
 
@@ -583,7 +585,7 @@ void tester_setup_failed(void)
 	test->stage = TEST_STAGE_POST_TEARDOWN;
 
 	if (test->timeout_id > 0) {
-		g_source_remove(test->timeout_id);
+		timeout_remove(test->timeout_id);
 		test->timeout_id = 0;
 	}
 
@@ -606,7 +608,7 @@ static void test_result(enum test_result result)
 		return;
 
 	if (test->timeout_id > 0) {
-		g_source_remove(test->timeout_id);
+		timeout_remove(test->timeout_id);
 		test->timeout_id = 0;
 	}
 
src/shared/timeout-ell.c +6 -0
index 023364069..6416d8590 100644
@@ -101,3 +101,9 @@ void timeout_remove(unsigned int id)
 	if (to)
 		l_timeout_remove(to);
 }
+
+unsigned int timeout_add_seconds(unsigned int timeout, timeout_func_t func,
+			void *user_data, timeout_destroy_func_t destroy)
+{
+	return timeout_add(timeout * 1000, func, user_data, destroy);
+}
src/shared/timeout-glib.c +27 -0
index 8bdb7a662..3268d480c 100644
@@ -71,3 +71,30 @@ void timeout_remove(unsigned int id)
 	if (source)
 		g_source_destroy(source);
 }
+
+unsigned int timeout_add_seconds(unsigned int timeout, timeout_func_t func,
+			void *user_data, timeout_destroy_func_t destroy)
+{
+	struct timeout_data *data;
+	guint id;
+
+	data = g_try_new0(struct timeout_data, 1);
+	if (!data)
+		return 0;
+
+	data->func = func;
+	data->destroy = destroy;
+	data->user_data = user_data;
+
+	if (!timeout)
+		id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, timeout_callback,
+							data, timeout_destroy);
+	else
+		id = g_timeout_add_seconds_full(G_PRIORITY_DEFAULT, timeout,
+							timeout_callback, data,
+							timeout_destroy);
+	if (!id)
+		g_free(data);
+
+	return id;
+}
src/shared/timeout-mainloop.c +6 -0
index 5ffa65c2a..9be803cda 100644
@@ -71,3 +71,9 @@ void timeout_remove(unsigned int id)
 
 	mainloop_remove_timeout((int) id);
 }
+
+unsigned int timeout_add_seconds(unsigned int timeout, timeout_func_t func,
+			void *user_data, timeout_destroy_func_t destroy)
+{
+	return timeout_add(timeout * 1000, func, user_data, destroy);
+}
src/shared/timeout.h +3 -0
index 7e22345dd..0945c3318 100644
@@ -16,3 +16,6 @@ typedef void (*timeout_destroy_func_t)(void *user_data);
 unsigned int timeout_add(unsigned int timeout, timeout_func_t func,
 			void *user_data, timeout_destroy_func_t destroy);
 void timeout_remove(unsigned int id);
+
+unsigned int timeout_add_seconds(unsigned int timeout, timeout_func_t func,
+			void *user_data, timeout_destroy_func_t destroy);
Signature · 1 line
2.18.0
#4 lore raw

PATCH v5 2/4 src: Replace calls to g_timeout_add_seconds by timeout_add_seconds

In reply to Frédéric Danis

Replace calls to g_timeout_add_seconds() by the timeout_add_seconds() wrapper which takes care of 0 delay.

---
 src/adapter.c     | 90 ++++++++++++++++++++++++++---------------------
 src/adv_monitor.c | 14 ++++----
 src/advertising.c | 28 ++++++++-------
 src/device.c      | 60 ++++++++++++++++---------------
 src/main.c        |  7 ++--
 src/sdp-client.c  | 13 +++----
 6 files changed, 115 insertions(+), 97 deletions(-)
src/adapter.c +49 -41
index cc0849f99..2fa06b73c 100644
@@ -46,6 +46,7 @@
 #include "src/shared/queue.h"
 #include "src/shared/att.h"
 #include "src/shared/gatt-db.h"
+#include "src/shared/timeout.h"
 
 #include "btio/btio.h"
 #include "btd.h"
@@ -237,10 +238,12 @@ struct btd_adapter {
 	struct discovery_client *client;	/* active discovery client */
 
 	GSList *discovery_found;	/* list of found devices */
-	guint discovery_idle_timeout;	/* timeout between discovery runs */
-	guint passive_scan_timeout;	/* timeout between passive scans */
+	unsigned int discovery_idle_timeout; /* timeout between discovery
+					      * runs
+					      */
+	unsigned int passive_scan_timeout; /* timeout between passive scans */
 
-	guint pairable_timeout_id;	/* pairable timeout id */
+	unsigned int pairable_timeout_id;	/* pairable timeout id */
 	guint auth_idle_id;		/* Pending authorization dequeue */
 	GQueue *auths;			/* Ongoing and pending auths */
 	bool pincode_requested;		/* PIN requested during last bonding */
@@ -268,13 +271,13 @@ struct btd_adapter {
 	struct oob_handler *oob_handler;
 
 	unsigned int load_ltks_id;
-	guint load_ltks_timeout;
+	unsigned int load_ltks_timeout;
 
 	unsigned int confirm_name_id;
-	guint confirm_name_timeout;
+	unsigned int confirm_name_timeout;
 
 	unsigned int pair_device_id;
-	guint pair_device_timeout;
+	unsigned int pair_device_timeout;
 
 	unsigned int db_id;		/* Service event handler for GATT db */
 
@@ -695,7 +698,7 @@ static bool set_discoverable(struct btd_adapter *adapter, uint8_t mode,
 	return false;
 }
 
-static gboolean pairable_timeout_handler(gpointer user_data)
+static bool pairable_timeout_handler(gpointer user_data)
 {
 	struct btd_adapter *adapter = user_data;
 
@@ -709,7 +712,7 @@ static gboolean pairable_timeout_handler(gpointer user_data)
 static void trigger_pairable_timeout(struct btd_adapter *adapter)
 {
 	if (adapter->pairable_timeout_id > 0) {
-		g_source_remove(adapter->pairable_timeout_id);
+		timeout_remove(adapter->pairable_timeout_id);
 		adapter->pairable_timeout_id = 0;
 	}
 
@@ -718,8 +721,9 @@ static void trigger_pairable_timeout(struct btd_adapter *adapter)
 
 	if (adapter->pairable_timeout > 0)
 		adapter->pairable_timeout_id =
-			g_timeout_add_seconds(adapter->pairable_timeout,
-					pairable_timeout_handler, adapter);
+			timeout_add_seconds(adapter->pairable_timeout,
+					pairable_timeout_handler, adapter,
+					NULL);
 }
 
 static void local_name_changed_callback(uint16_t index, uint16_t length,
@@ -1323,7 +1327,7 @@ static void passive_scanning_complete(uint8_t status, uint16_t length,
 	}
 }
 
-static gboolean passive_scanning_timeout(gpointer user_data)
+static bool passive_scanning_timeout(gpointer user_data)
 {
 	struct btd_adapter *adapter = user_data;
 	struct mgmt_cp_start_discovery cp;
@@ -1347,7 +1351,7 @@ static void trigger_passive_scanning(struct btd_adapter *adapter)
 	DBG("");
 
 	if (adapter->passive_scan_timeout > 0) {
-		g_source_remove(adapter->passive_scan_timeout);
+		timeout_remove(adapter->passive_scan_timeout);
 		adapter->passive_scan_timeout = 0;
 	}
 
@@ -1386,8 +1390,9 @@ static void trigger_passive_scanning(struct btd_adapter *adapter)
 	if (!adapter->connect_list)
 		return;
 
-	adapter->passive_scan_timeout = g_timeout_add_seconds(CONN_SCAN_TIMEOUT,
-					passive_scanning_timeout, adapter);
+	adapter->passive_scan_timeout = timeout_add_seconds(CONN_SCAN_TIMEOUT,
+					passive_scanning_timeout, adapter,
+					NULL);
 }
 
 static void stop_passive_scanning_complete(uint8_t status, uint16_t length,
@@ -1467,7 +1472,7 @@ static void cancel_passive_scanning(struct btd_adapter *adapter)
 	DBG("");
 
 	if (adapter->passive_scan_timeout > 0) {
-		g_source_remove(adapter->passive_scan_timeout);
+		timeout_remove(adapter->passive_scan_timeout);
 		adapter->passive_scan_timeout = 0;
 	}
 }
@@ -1512,7 +1517,7 @@ static void discovery_cleanup(struct btd_adapter *adapter, int timeout)
 	adapter->discovery_type = 0x00;
 
 	if (adapter->discovery_idle_timeout > 0) {
-		g_source_remove(adapter->discovery_idle_timeout);
+		timeout_remove(adapter->discovery_idle_timeout);
 		adapter->discovery_idle_timeout = 0;
 	}
 
@@ -1688,7 +1693,7 @@ static void start_discovery_complete(uint8_t status, uint16_t length,
 	trigger_start_discovery(adapter, IDLE_DISCOV_TIMEOUT * 2);
 }
 
-static gboolean start_discovery_timeout(gpointer user_data)
+static bool start_discovery_timeout(gpointer user_data)
 {
 	struct btd_adapter *adapter = user_data;
 	struct mgmt_cp_start_service_discovery *sd_cp;
@@ -1784,7 +1789,7 @@ static void trigger_start_discovery(struct btd_adapter *adapter, guint delay)
 	cancel_passive_scanning(adapter);
 
 	if (adapter->discovery_idle_timeout > 0) {
-		g_source_remove(adapter->discovery_idle_timeout);
+		timeout_remove(adapter->discovery_idle_timeout);
 		adapter->discovery_idle_timeout = 0;
 	}
 
@@ -1797,8 +1802,8 @@ static void trigger_start_discovery(struct btd_adapter *adapter, guint delay)
 	if (!btd_adapter_get_powered(adapter))
 		return;
 
-	adapter->discovery_idle_timeout = g_timeout_add_seconds(delay,
-					start_discovery_timeout, adapter);
+	adapter->discovery_idle_timeout = timeout_add_seconds(delay,
+					start_discovery_timeout, adapter, NULL);
 }
 
 static void suspend_discovery_complete(uint8_t status, uint16_t length,
@@ -1837,7 +1842,7 @@ static void suspend_discovery(struct btd_adapter *adapter)
 	 * The restart will be triggered when the discovery is resumed.
 	 */
 	if (adapter->discovery_idle_timeout > 0) {
-		g_source_remove(adapter->discovery_idle_timeout);
+		timeout_remove(adapter->discovery_idle_timeout);
 		adapter->discovery_idle_timeout = 0;
 	}
 
@@ -1918,7 +1923,7 @@ static void discovering_callback(uint16_t index, uint16_t length,
 
 	case 0x01:
 		if (adapter->discovery_idle_timeout > 0) {
-			g_source_remove(adapter->discovery_idle_timeout);
+			timeout_remove(adapter->discovery_idle_timeout);
 			adapter->discovery_idle_timeout = 0;
 		}
 
@@ -2404,7 +2409,7 @@ static bool parse_pathloss(DBusMessageIter *value,
 	return true;
 }
 
-static bool parse_transport(DBusMessageIter *value, 
+static bool parse_transport(DBusMessageIter *value,
 					struct discovery_filter *filter)
 {
 	char *transport_str;
@@ -3931,7 +3936,7 @@ static void load_link_keys(struct btd_adapter *adapter, GSList *keys,
 							adapter->dev_id);
 }
 
-static gboolean load_ltks_timeout(gpointer user_data)
+static bool load_ltks_timeout(gpointer user_data)
 {
 	struct btd_adapter *adapter = user_data;
 
@@ -3959,7 +3964,7 @@ static void load_ltks_complete(uint8_t status, uint16_t length,
 
 	adapter->load_ltks_id = 0;
 
-	g_source_remove(adapter->load_ltks_timeout);
+	timeout_remove(adapter->load_ltks_timeout);
 	adapter->load_ltks_timeout = 0;
 
 	DBG("LTKs loaded for hci%u", adapter->dev_id);
@@ -4036,8 +4041,9 @@ static void load_ltks(struct btd_adapter *adapter, GSList *keys)
 	 * and forgets to send a command complete response. However in
 	 * case of failures it does send a command status.
 	 */
-	adapter->load_ltks_timeout = g_timeout_add_seconds(2,
-						load_ltks_timeout, adapter);
+	adapter->load_ltks_timeout = timeout_add_seconds(2,
+						load_ltks_timeout, adapter,
+						NULL);
 }
 
 static void load_irks_complete(uint8_t status, uint16_t length,
@@ -5337,23 +5343,23 @@ static void adapter_free(gpointer user_data)
 	remove_discovery_list(adapter);
 
 	if (adapter->pairable_timeout_id > 0) {
-		g_source_remove(adapter->pairable_timeout_id);
+		timeout_remove(adapter->pairable_timeout_id);
 		adapter->pairable_timeout_id = 0;
 	}
 
 	if (adapter->passive_scan_timeout > 0) {
-		g_source_remove(adapter->passive_scan_timeout);
+		timeout_remove(adapter->passive_scan_timeout);
 		adapter->passive_scan_timeout = 0;
 	}
 
 	if (adapter->load_ltks_timeout > 0)
-		g_source_remove(adapter->load_ltks_timeout);
+		timeout_remove(adapter->load_ltks_timeout);
 
 	if (adapter->confirm_name_timeout > 0)
-		g_source_remove(adapter->confirm_name_timeout);
+		timeout_remove(adapter->confirm_name_timeout);
 
 	if (adapter->pair_device_timeout > 0)
-		g_source_remove(adapter->pair_device_timeout);
+		timeout_remove(adapter->pair_device_timeout);
 
 	if (adapter->auth_idle_id)
 		g_source_remove(adapter->auth_idle_id);
@@ -6386,7 +6392,7 @@ const bdaddr_t *btd_adapter_get_address(struct btd_adapter *adapter)
 	return &adapter->bdaddr;
 }
 
-static gboolean confirm_name_timeout(gpointer user_data)
+static bool confirm_name_timeout(gpointer user_data)
 {
 	struct btd_adapter *adapter = user_data;
 
@@ -6414,7 +6420,7 @@ static void confirm_name_complete(uint8_t status, uint16_t length,
 
 	adapter->confirm_name_id = 0;
 
-	g_source_remove(adapter->confirm_name_timeout);
+	timeout_remove(adapter->confirm_name_timeout);
 	adapter->confirm_name_timeout = 0;
 
 	DBG("Confirm name complete for hci%u", adapter->dev_id);
@@ -6445,7 +6451,7 @@ static void confirm_name(struct btd_adapter *adapter, const bdaddr_t *bdaddr,
 	}
 
 	if (adapter->confirm_name_timeout > 0) {
-		g_source_remove(adapter->confirm_name_timeout);
+		timeout_remove(adapter->confirm_name_timeout);
 		adapter->confirm_name_timeout = 0;
 	}
 
@@ -6470,8 +6476,9 @@ static void confirm_name(struct btd_adapter *adapter, const bdaddr_t *bdaddr,
 	 * and forgets to send a command complete response. However in
 	 * case of failures it does send a command status.
 	 */
-	adapter->confirm_name_timeout = g_timeout_add_seconds(2,
-						confirm_name_timeout, adapter);
+	adapter->confirm_name_timeout = timeout_add_seconds(2,
+						confirm_name_timeout, adapter,
+						NULL);
 }
 
 static void adapter_msd_notify(struct btd_adapter *adapter,
@@ -7722,7 +7729,7 @@ static void free_pair_device_data(void *user_data)
 	g_free(data);
 }
 
-static gboolean pair_device_timeout(gpointer user_data)
+static bool pair_device_timeout(gpointer user_data)
 {
 	struct pair_device_data *data = user_data;
 	struct btd_adapter *adapter = data->adapter;
@@ -7749,7 +7756,7 @@ static void pair_device_complete(uint8_t status, uint16_t length,
 	adapter->pair_device_id = 0;
 
 	if (adapter->pair_device_timeout > 0) {
-		g_source_remove(adapter->pair_device_timeout);
+		timeout_remove(adapter->pair_device_timeout);
 		adapter->pair_device_timeout = 0;
 	}
 
@@ -7835,8 +7842,9 @@ int adapter_bonding_attempt(struct btd_adapter *adapter, const bdaddr_t *bdaddr,
 	 * request never times out. Therefore, add a timer to clean up
 	 * if no response arrives
 	 */
-	adapter->pair_device_timeout = g_timeout_add_seconds(BONDING_TIMEOUT,
-						pair_device_timeout, data);
+	adapter->pair_device_timeout = timeout_add_seconds(BONDING_TIMEOUT,
+						pair_device_timeout, data,
+						NULL);
 
 	return 0;
 }
src/adv_monitor.c +8 -6
index 54751db0b..33e7c8454 100644
@@ -31,6 +31,7 @@
 #include "src/error.h"
 #include "src/shared/mgmt.h"
 #include "src/shared/queue.h"
+#include "src/shared/timeout.h"
 #include "src/shared/util.h"
 
 #include "adv_monitor.h"
@@ -125,7 +126,7 @@ struct adv_monitor_device {
 					 */
 	time_t last_seen;		/* Time when last Adv was received */
 	bool found;			/* State of the device - lost/found */
-	guint lost_timer;		/* Timer to track if the device goes
+	unsigned int lost_timer;	/* Timer to track if the device goes
 					 * offline/out-of-range
 					 */
 };
@@ -1385,7 +1386,7 @@ static void monitor_device_free(void *data)
 	}
 
 	if (dev->lost_timer) {
-		g_source_remove(dev->lost_timer);
+		timeout_remove(dev->lost_timer);
 		dev->lost_timer = 0;
 	}
 
@@ -1468,7 +1469,7 @@ static void report_device_state_setup(DBusMessageIter *iter, void *user_data)
 }
 
 /* Handles a situation where the device goes offline/out-of-range */
-static gboolean handle_device_lost_timeout(gpointer user_data)
+static bool handle_device_lost_timeout(gpointer user_data)
 {
 	struct adv_monitor_device *dev = user_data;
 	struct adv_monitor *monitor = dev->monitor;
@@ -1534,7 +1535,7 @@ static void adv_monitor_filter_rssi(struct adv_monitor *monitor,
 	}
 
 	if (dev->lost_timer) {
-		g_source_remove(dev->lost_timer);
+		timeout_remove(dev->lost_timer);
 		dev->lost_timer = 0;
 	}
 
@@ -1609,7 +1610,8 @@ static void adv_monitor_filter_rssi(struct adv_monitor *monitor,
 	 */
 	if (dev->found) {
 		dev->lost_timer =
-			g_timeout_add_seconds(monitor->low_rssi_timeout,
-					      handle_device_lost_timeout, dev);
+			timeout_add_seconds(monitor->low_rssi_timeout,
+					    handle_device_lost_timeout, dev,
+							NULL);
 	}
 }
src/advertising.c +15 -13
index 15a343e52..d76e97a74 100644
@@ -32,6 +32,7 @@
 #include "src/shared/ad.h"
 #include "src/shared/mgmt.h"
 #include "src/shared/queue.h"
+#include "src/shared/timeout.h"
 #include "src/shared/util.h"
 #include "advertising.h"
 
@@ -111,10 +112,10 @@ static void client_free(void *data)
 	struct btd_adv_client *client = data;
 
 	if (client->to_id > 0)
-		g_source_remove(client->to_id);
+		timeout_remove(client->to_id);
 
 	if (client->disc_to_id > 0)
-		g_source_remove(client->disc_to_id);
+		timeout_remove(client->disc_to_id);
 
 	if (client->client) {
 		g_dbus_client_set_disconnect_watch(client->client, NULL, NULL);
@@ -574,7 +575,7 @@ static bool parse_duration(DBusMessageIter *iter,
 	return true;
 }
 
-static gboolean client_timeout(void *user_data)
+static bool client_timeout(void *user_data)
 {
 	struct btd_adv_client *client = user_data;
 
@@ -593,7 +594,7 @@ static bool parse_timeout(DBusMessageIter *iter,
 {
 	if (!iter) {
 		client->timeout = 0;
-		g_source_remove(client->to_id);
+		timeout_remove(client->to_id);
 		client->to_id = 0;
 		return true;
 	}
@@ -604,11 +605,12 @@ static bool parse_timeout(DBusMessageIter *iter,
 	dbus_message_iter_get_basic(iter, &client->timeout);
 
 	if (client->to_id)
-		g_source_remove(client->to_id);
+		timeout_remove(client->to_id);
 
 	if (client->timeout > 0)
-		client->to_id = g_timeout_add_seconds(client->timeout,
-							client_timeout, client);
+		client->to_id = timeout_add_seconds(client->timeout,
+							client_timeout, client,
+							NULL);
 
 	return true;
 }
@@ -945,7 +947,7 @@ static int refresh_advertisement(struct btd_adv_client *client,
 	return refresh_legacy_adv(client, func, mgmt_id);
 }
 
-static gboolean client_discoverable_timeout(void *user_data)
+static bool client_discoverable_timeout(void *user_data)
 {
 	struct btd_adv_client *client = user_data;
 
@@ -965,7 +967,7 @@ static bool parse_discoverable_timeout(DBusMessageIter *iter,
 {
 	if (!iter) {
 		client->discoverable_to = 0;
-		g_source_remove(client->disc_to_id);
+		timeout_remove(client->disc_to_id);
 		client->disc_to_id = 0;
 		return true;
 	}
@@ -976,11 +978,11 @@ static bool parse_discoverable_timeout(DBusMessageIter *iter,
 	dbus_message_iter_get_basic(iter, &client->discoverable_to);
 
 	if (client->disc_to_id)
-		g_source_remove(client->disc_to_id);
+		timeout_remove(client->disc_to_id);
 
-	client->disc_to_id = g_timeout_add_seconds(client->discoverable_to,
+	client->disc_to_id = timeout_add_seconds(client->discoverable_to,
 						client_discoverable_timeout,
-						client);
+						client, NULL);
 
 	return true;
 }
@@ -1361,7 +1363,7 @@ static DBusMessage *parse_advertisement(struct btd_adv_client *client)
 		}
 	} else if (client->disc_to_id) {
 		/* Ignore DiscoverableTimeout if not discoverable */
-		g_source_remove(client->disc_to_id);
+		timeout_remove(client->disc_to_id);
 		client->disc_to_id = 0;
 		client->discoverable_to = 0;
 	}
src/device.c +32 -28

182 lines not on this page · raw message

src/main.c +4 -3
index 572dc939c..c32bda7d4 100644
@@ -41,6 +41,7 @@
 
 #include "shared/att-types.h"
 #include "shared/mainloop.h"
+#include "shared/timeout.h"
 #include "lib/uuid.h"
 #include "shared/util.h"
 #include "btd.h"
@@ -853,7 +854,7 @@ void btd_exit(void)
 	mainloop_quit();
 }
 
-static gboolean quit_eventloop(gpointer user_data)
+static bool quit_eventloop(gpointer user_data)
 {
 	btd_exit();
 	return FALSE;
@@ -868,8 +869,8 @@ static void signal_callback(int signum, void *user_data)
 	case SIGTERM:
 		if (!terminated) {
 			info("Terminating");
-			g_timeout_add_seconds(SHUTDOWN_GRACE_SECONDS,
-							quit_eventloop, NULL);
+			timeout_add_seconds(SHUTDOWN_GRACE_SECONDS,
+						quit_eventloop, NULL, NULL);
 
 			mainloop_sd_notify("STATUS=Powering down");
 			adapter_shutdown();
src/sdp-client.c +7 -6
index 55f5bc323..71d3d9e95 100644
@@ -21,6 +21,7 @@
 #include "lib/sdp_lib.h"
 
 #include "btio/btio.h"
+#include "shared/timeout.h"
 #include "log.h"
 #include "sdp-client.h"
 
@@ -31,7 +32,7 @@ struct cached_sdp_session {
 	bdaddr_t src;
 	bdaddr_t dst;
 	sdp_session_t *session;
-	guint timer;
+	unsigned int timer;
 	guint io_id;
 };
 
@@ -44,7 +45,7 @@ static void cleanup_cached_session(struct cached_sdp_session *cached)
 	g_free(cached);
 }
 
-static gboolean cached_session_expired(gpointer user_data)
+static bool cached_session_expired(gpointer user_data)
 {
 	struct cached_sdp_session *cached = user_data;
 
@@ -66,7 +67,7 @@ static sdp_session_t *get_cached_sdp_session(const bdaddr_t *src,
 		if (bacmp(&c->src, src) || bacmp(&c->dst, dst))
 			continue;
 
-		g_source_remove(c->timer);
+		timeout_remove(c->timer);
 		g_source_remove(c->io_id);
 
 		session = c->session;
@@ -85,7 +86,7 @@ static gboolean disconnect_watch(GIOChannel *chan, GIOCondition cond,
 {
 	struct cached_sdp_session *cached = user_data;
 
-	g_source_remove(cached->timer);
+	timeout_remove(cached->timer);
 	cleanup_cached_session(cached);
 
 	return FALSE;
@@ -107,9 +108,9 @@ static void cache_sdp_session(bdaddr_t *src, bdaddr_t *dst,
 
 	cached_sdp_sessions = g_slist_append(cached_sdp_sessions, cached);
 
-	cached->timer = g_timeout_add_seconds(CACHE_TIMEOUT,
+	cached->timer = timeout_add_seconds(CACHE_TIMEOUT,
 						cached_session_expired,
-						cached);
+						cached, NULL);
 
 	/* Watch the connection state during cache timeout */
 	sk = sdp_get_socket(session);
Signature · 1 line
2.18.0
#5 lore raw

PATCH v5 3/4 plugins: Replace calls to g_timeout_add_seconds by timeout_add_seconds

In reply to Frédéric Danis

Replace calls to g_timeout_add_seconds() by the timeout_add_seconds() wrapper which takes care of 0 delay.

---
 plugins/policy.c | 91 ++++++++++++++++++++++++------------------------
 1 file changed, 46 insertions(+), 45 deletions(-)
plugins/policy.c +46 -45
index ba9e1be02..bf93df096 100644
@@ -31,6 +31,7 @@
 #include "src/service.h"
 #include "src/profile.h"
 #include "src/btd.h"
+#include "src/shared/timeout.h"
 
 #define CONTROL_CONNECT_TIMEOUT 2
 #define SOURCE_RETRY_TIMEOUT 2
@@ -46,7 +47,7 @@ struct reconnect_data {
 	struct btd_device *dev;
 	bool reconnect;
 	GSList *services;
-	guint timer;
+	unsigned int timer;
 	bool active;
 	unsigned int attempt;
 	bool on_resume;
@@ -77,13 +78,13 @@ static bool auto_enable = false;
 struct policy_data {
 	struct btd_device *dev;
 
-	guint source_timer;
+	unsigned int source_timer;
 	uint8_t source_retries;
-	guint sink_timer;
+	unsigned int sink_timer;
 	uint8_t sink_retries;
-	guint ct_timer;
+	unsigned int ct_timer;
 	uint8_t ct_retries;
-	guint tg_timer;
+	unsigned int tg_timer;
 	uint8_t tg_retries;
 };
 
@@ -126,7 +127,7 @@ static void policy_disconnect(struct policy_data *data,
 	btd_service_disconnect(service);
 }
 
-static gboolean policy_connect_ct(gpointer user_data)
+static bool policy_connect_ct(gpointer user_data)
 {
 	struct policy_data *data = user_data;
 	struct btd_service *service;
@@ -144,10 +145,10 @@ static gboolean policy_connect_ct(gpointer user_data)
 static void policy_set_ct_timer(struct policy_data *data, int timeout)
 {
 	if (data->ct_timer > 0)
-		g_source_remove(data->ct_timer);
+		timeout_remove(data->ct_timer);
 
-	data->ct_timer = g_timeout_add_seconds(timeout, policy_connect_ct,
-									data);
+	data->ct_timer = timeout_add_seconds(timeout, policy_connect_ct,
+						data, NULL);
 }
 
 static struct policy_data *find_data(struct btd_device *dev)
@@ -169,16 +170,16 @@ static void policy_remove(void *user_data)
 	struct policy_data *data = user_data;
 
 	if (data->source_timer > 0)
-		g_source_remove(data->source_timer);
+		timeout_remove(data->source_timer);
 
 	if (data->sink_timer > 0)
-		g_source_remove(data->sink_timer);
+		timeout_remove(data->sink_timer);
 
 	if (data->ct_timer > 0)
-		g_source_remove(data->ct_timer);
+		timeout_remove(data->ct_timer);
 
 	if (data->tg_timer > 0)
-		g_source_remove(data->tg_timer);
+		timeout_remove(data->tg_timer);
 
 	g_free(data);
 }
@@ -199,7 +200,7 @@ static struct policy_data *policy_get_data(struct btd_device *dev)
 	return data;
 }
 
-static gboolean policy_connect_sink(gpointer user_data)
+static bool policy_connect_sink(gpointer user_data)
 {
 	struct policy_data *data = user_data;
 	struct btd_service *service;
@@ -217,11 +218,11 @@ static gboolean policy_connect_sink(gpointer user_data)
 static void policy_set_sink_timer(struct policy_data *data)
 {
 	if (data->sink_timer > 0)
-		g_source_remove(data->sink_timer);
+		timeout_remove(data->sink_timer);
 
-	data->sink_timer = g_timeout_add_seconds(SINK_RETRY_TIMEOUT,
+	data->sink_timer = timeout_add_seconds(SINK_RETRY_TIMEOUT,
 							policy_connect_sink,
-							data);
+							data, NULL);
 }
 
 static void sink_cb(struct btd_service *service, btd_service_state_t old_state,
@@ -240,7 +241,7 @@ static void sink_cb(struct btd_service *service, btd_service_state_t old_state,
 	switch (new_state) {
 	case BTD_SERVICE_STATE_UNAVAILABLE:
 		if (data->sink_timer > 0) {
-			g_source_remove(data->sink_timer);
+			timeout_remove(data->sink_timer);
 			data->sink_timer = 0;
 		}
 		break;
@@ -255,13 +256,13 @@ static void sink_cb(struct btd_service *service, btd_service_state_t old_state,
 					data->sink_retries = 0;
 				break;
 			} else if (data->sink_timer > 0) {
-				g_source_remove(data->sink_timer);
+				timeout_remove(data->sink_timer);
 				data->sink_timer = 0;
 			}
 		}
 
 		if (data->ct_timer > 0) {
-			g_source_remove(data->ct_timer);
+			timeout_remove(data->ct_timer);
 			data->ct_timer = 0;
 		} else if (btd_service_get_state(controller) !=
 						BTD_SERVICE_STATE_DISCONNECTED)
@@ -271,7 +272,7 @@ static void sink_cb(struct btd_service *service, btd_service_state_t old_state,
 		break;
 	case BTD_SERVICE_STATE_CONNECTED:
 		if (data->sink_timer > 0) {
-			g_source_remove(data->sink_timer);
+			timeout_remove(data->sink_timer);
 			data->sink_timer = 0;
 		}
 
@@ -325,7 +326,7 @@ static void hs_cb(struct btd_service *service, btd_service_state_t old_state,
 	}
 }
 
-static gboolean policy_connect_tg(gpointer user_data)
+static bool policy_connect_tg(gpointer user_data)
 {
 	struct policy_data *data = user_data;
 	struct btd_service *service;
@@ -343,13 +344,13 @@ static gboolean policy_connect_tg(gpointer user_data)
 static void policy_set_tg_timer(struct policy_data *data, int timeout)
 {
 	if (data->tg_timer > 0)
-		g_source_remove(data->tg_timer);
+		timeout_remove(data->tg_timer);
 
-	data->tg_timer = g_timeout_add_seconds(timeout, policy_connect_tg,
-							data);
+	data->tg_timer = timeout_add_seconds(timeout, policy_connect_tg,
+							data, NULL);
 }
 
-static gboolean policy_connect_source(gpointer user_data)
+static bool policy_connect_source(gpointer user_data)
 {
 	struct policy_data *data = user_data;
 	struct btd_service *service;
@@ -367,11 +368,11 @@ static gboolean policy_connect_source(gpointer user_data)
 static void policy_set_source_timer(struct policy_data *data)
 {
 	if (data->source_timer > 0)
-		g_source_remove(data->source_timer);
+		timeout_remove(data->source_timer);
 
-	data->source_timer = g_timeout_add_seconds(SOURCE_RETRY_TIMEOUT,
+	data->source_timer = timeout_add_seconds(SOURCE_RETRY_TIMEOUT,
 							policy_connect_source,
-							data);
+							data, NULL);
 }
 
 static void source_cb(struct btd_service *service,
@@ -391,7 +392,7 @@ static void source_cb(struct btd_service *service,
 	switch (new_state) {
 	case BTD_SERVICE_STATE_UNAVAILABLE:
 		if (data->source_timer > 0) {
-			g_source_remove(data->source_timer);
+			timeout_remove(data->source_timer);
 			data->source_timer = 0;
 		}
 		break;
@@ -406,13 +407,13 @@ static void source_cb(struct btd_service *service,
 					data->source_retries = 0;
 				break;
 			} else if (data->source_timer > 0) {
-				g_source_remove(data->source_timer);
+				timeout_remove(data->source_timer);
 				data->source_timer = 0;
 			}
 		}
 
 		if (data->tg_timer > 0) {
-			g_source_remove(data->tg_timer);
+			timeout_remove(data->tg_timer);
 			data->tg_timer = 0;
 		} else if (btd_service_get_state(target) !=
 						BTD_SERVICE_STATE_DISCONNECTED)
@@ -422,7 +423,7 @@ static void source_cb(struct btd_service *service,
 		break;
 	case BTD_SERVICE_STATE_CONNECTED:
 		if (data->source_timer > 0) {
-			g_source_remove(data->source_timer);
+			timeout_remove(data->source_timer);
 			data->source_timer = 0;
 		}
 
@@ -454,7 +455,7 @@ static void controller_cb(struct btd_service *service,
 	switch (new_state) {
 	case BTD_SERVICE_STATE_UNAVAILABLE:
 		if (data->ct_timer > 0) {
-			g_source_remove(data->ct_timer);
+			timeout_remove(data->ct_timer);
 			data->ct_timer = 0;
 		}
 		break;
@@ -470,7 +471,7 @@ static void controller_cb(struct btd_service *service,
 					data->ct_retries = 0;
 				break;
 			} else if (data->ct_timer > 0) {
-				g_source_remove(data->ct_timer);
+				timeout_remove(data->ct_timer);
 				data->ct_timer = 0;
 			}
 		} else if (old_state == BTD_SERVICE_STATE_CONNECTED) {
@@ -481,7 +482,7 @@ static void controller_cb(struct btd_service *service,
 		break;
 	case BTD_SERVICE_STATE_CONNECTED:
 		if (data->ct_timer > 0) {
-			g_source_remove(data->ct_timer);
+			timeout_remove(data->ct_timer);
 			data->ct_timer = 0;
 		}
 		break;
@@ -504,7 +505,7 @@ static void target_cb(struct btd_service *service,
 	switch (new_state) {
 	case BTD_SERVICE_STATE_UNAVAILABLE:
 		if (data->tg_timer > 0) {
-			g_source_remove(data->tg_timer);
+			timeout_remove(data->tg_timer);
 			data->tg_timer = 0;
 		}
 		break;
@@ -520,7 +521,7 @@ static void target_cb(struct btd_service *service,
 					data->tg_retries = 0;
 				break;
 			} else if (data->tg_timer > 0) {
-				g_source_remove(data->tg_timer);
+				timeout_remove(data->tg_timer);
 				data->tg_timer = 0;
 			}
 		} else if (old_state == BTD_SERVICE_STATE_CONNECTED) {
@@ -531,7 +532,7 @@ static void target_cb(struct btd_service *service,
 		break;
 	case BTD_SERVICE_STATE_CONNECTED:
 		if (data->tg_timer > 0) {
-			g_source_remove(data->tg_timer);
+			timeout_remove(data->tg_timer);
 			data->tg_timer = 0;
 		}
 		break;
@@ -546,7 +547,7 @@ static void reconnect_reset(struct reconnect_data *reconnect)
 	reconnect->active = false;
 
 	if (reconnect->timer > 0) {
-		g_source_remove(reconnect->timer);
+		timeout_remove(reconnect->timer);
 		reconnect->timer = 0;
 	}
 }
@@ -592,7 +593,7 @@ static void reconnect_destroy(gpointer data)
 	struct reconnect_data *reconnect = data;
 
 	if (reconnect->timer > 0)
-		g_source_remove(reconnect->timer);
+		timeout_remove(reconnect->timer);
 
 	g_slist_free_full(reconnect->services,
 					(GDestroyNotify) btd_service_unref);
@@ -622,7 +623,7 @@ static void reconnect_remove(struct btd_service *service)
 	reconnects = g_slist_remove(reconnects, reconnect);
 
 	if (reconnect->timer > 0)
-		g_source_remove(reconnect->timer);
+		timeout_remove(reconnect->timer);
 
 	g_free(reconnect);
 }
@@ -693,7 +694,7 @@ static void service_cb(struct btd_service *service,
 	DBG("Added %s reconnect %u", profile->name, reconnect->reconnect);
 }
 
-static gboolean reconnect_timeout(gpointer data)
+static bool reconnect_timeout(gpointer data)
 {
 	struct reconnect_data *reconnect = data;
 	int err;
@@ -734,8 +735,8 @@ static void reconnect_set_timer(struct reconnect_data *reconnect, int timeout)
 	DBG("attempt %u/%zu %d seconds", reconnect->attempt + 1,
 						reconnect_attempts, timeout);
 
-	reconnect->timer = g_timeout_add_seconds(timeout, reconnect_timeout,
-								reconnect);
+	reconnect->timer = timeout_add_seconds(timeout, reconnect_timeout,
+						reconnect, NULL);
 }
 
 static void disconnect_cb(struct btd_device *dev, uint8_t reason)
Signature · 1 line
2.18.0
#6 lore raw

PATCH v5 4/4 profiles: Replace calls to g_timeout_add_seconds by timeout_add_seconds

In reply to Frédéric Danis

Replace calls to g_timeout_add_seconds() by the timeout_add_seconds() wrapper which takes care of 0 delay.

---
 profiles/audio/a2dp.c   | 17 ++++++------
 profiles/audio/avctp.c  | 44 +++++++++++++++--------------
 profiles/audio/avdtp.c  | 61 ++++++++++++++++++++---------------------
 profiles/audio/avrcp.c  | 13 +++++----
 profiles/health/hdp.c   | 16 +++++------
 profiles/health/mcap.c  | 21 +++++++-------
 profiles/input/device.c | 33 ++++++++++++----------
 profiles/network/bnep.c | 12 ++++----
 profiles/sap/server.c   | 13 +++++----
 9 files changed, 120 insertions(+), 110 deletions(-)
profiles/audio/a2dp.c +9 -8
index 98cae97b9..d31ed845c 100644
@@ -41,6 +41,7 @@
 #include "src/log.h"
 #include "src/sdpd.h"
 #include "src/shared/queue.h"
+#include "src/shared/timeout.h"
 #include "src/shared/util.h"
 
 #include "btio/btio.h"
@@ -69,7 +70,7 @@ struct a2dp_sep {
 	struct avdtp_local_sep *lsep;
 	struct avdtp *session;
 	struct avdtp_stream *stream;
-	guint suspend_timer;
+	unsigned int suspend_timer;
 	gboolean delay_reporting;
 	gboolean locked;
 	gboolean suspending;
@@ -480,7 +481,7 @@ static void stream_state_changed(struct avdtp_stream *stream,
 		return;
 
 	if (sep->suspend_timer) {
-		g_source_remove(sep->suspend_timer);
+		timeout_remove(sep->suspend_timer);
 		sep->suspend_timer = 0;
 	}
 
@@ -970,7 +971,7 @@ static void open_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
 	return;
 }
 
-static gboolean suspend_timeout(struct a2dp_sep *sep)
+static bool suspend_timeout(struct a2dp_sep *sep)
 {
 	if (avdtp_suspend(sep->session, sep->stream) == 0)
 		sep->suspending = TRUE;
@@ -997,9 +998,9 @@ static gboolean start_ind(struct avdtp *session, struct avdtp_local_sep *sep,
 
 	if (!a2dp_sep->locked) {
 		a2dp_sep->session = avdtp_ref(session);
-		a2dp_sep->suspend_timer = g_timeout_add_seconds(SUSPEND_TIMEOUT,
-						(GSourceFunc) suspend_timeout,
-						a2dp_sep);
+		a2dp_sep->suspend_timer = timeout_add_seconds(SUSPEND_TIMEOUT,
+					(timeout_func_t) suspend_timeout,
+					a2dp_sep, NULL);
 	}
 
 	if (!a2dp_sep->starting)
@@ -1055,7 +1056,7 @@ static gboolean suspend_ind(struct avdtp *session, struct avdtp_local_sep *sep,
 		DBG("Source %p: Suspend_Ind", sep);
 
 	if (a2dp_sep->suspend_timer) {
-		g_source_remove(a2dp_sep->suspend_timer);
+		timeout_remove(a2dp_sep->suspend_timer);
 		a2dp_sep->suspend_timer = 0;
 		avdtp_unref(a2dp_sep->session);
 		a2dp_sep->session = NULL;
@@ -2995,7 +2996,7 @@ unsigned int a2dp_resume(struct avdtp *session, struct a2dp_sep *sep,
 		break;
 	case AVDTP_STATE_STREAMING:
 		if (!sep->suspending && sep->suspend_timer) {
-			g_source_remove(sep->suspend_timer);
+			timeout_remove(sep->suspend_timer);
 			sep->suspend_timer = 0;
 			avdtp_unref(sep->session);
 			sep->session = NULL;
profiles/audio/avctp.c +23 -21

146 lines not on this page · raw message

profiles/audio/avdtp.c +30 -31

201 lines not on this page · raw message

profiles/audio/avrcp.c +7 -6
index c093deac8..05dd791de 100644
@@ -46,6 +46,7 @@
 #include "src/error.h"
 #include "src/sdpd.h"
 #include "src/dbus-common.h"
+#include "src/shared/timeout.h"
 #include "src/shared/util.h"
 
 #include "avctp.h"
@@ -3942,7 +3943,7 @@ static gboolean avrcp_get_capabilities_resp(struct avctp *conn, uint8_t code,
 
 	/* Connect browsing if pending */
 	if (session->browsing_timer > 0) {
-		g_source_remove(session->browsing_timer);
+		timeout_remove(session->browsing_timer);
 		session->browsing_timer = 0;
 		avctp_connect_browsing(session->conn);
 	}
@@ -4037,7 +4038,7 @@ static void destroy_browsing(void *data)
 static void session_init_browsing(struct avrcp *session)
 {
 	if (session->browsing_timer > 0) {
-		g_source_remove(session->browsing_timer);
+		timeout_remove(session->browsing_timer);
 		session->browsing_timer = 0;
 	}
 
@@ -4072,7 +4073,7 @@ static struct avrcp_data *data_init(struct avrcp *session, const char *uuid)
 	return data;
 }
 
-static gboolean connect_browsing(gpointer user_data)
+static bool connect_browsing(gpointer user_data)
 {
 	struct avrcp *session = user_data;
 
@@ -4096,9 +4097,9 @@ static void avrcp_connect_browsing(struct avrcp *session)
 	if (session->browsing_timer > 0)
 		return;
 
-	session->browsing_timer = g_timeout_add_seconds(AVRCP_BROWSING_TIMEOUT,
+	session->browsing_timer = timeout_add_seconds(AVRCP_BROWSING_TIMEOUT,
 							connect_browsing,
-							session);
+							session, NULL);
 }
 
 static void target_init(struct avrcp *session)
@@ -4261,7 +4262,7 @@ static void session_destroy(struct avrcp *session, int err)
 	}
 
 	if (session->browsing_timer > 0)
-		g_source_remove(session->browsing_timer);
+		timeout_remove(session->browsing_timer);
 
 	if (session->controller != NULL)
 		controller_destroy(session);
profiles/health/hdp.c +8 -8
index 9f906932d..6bc41946f 100644
@@ -31,6 +31,7 @@
 #include "src/adapter.h"
 #include "src/device.h"
 #include "src/sdpd.h"
+#include "src/shared/timeout.h"
 #include "btio/btio.h"
 
 #include "hdp_types.h"
@@ -70,7 +71,7 @@ struct hdp_tmp_dc_data {
 struct hdp_echo_data {
 	gboolean		echo_done;	/* Is a echo was already done */
 	gpointer		buf;		/* echo packet sent */
-	guint			tid;		/* echo timeout */
+	unsigned int		tid;		/* echo timeout */
 };
 
 static struct hdp_channel *hdp_channel_ref(struct hdp_channel *chan)
@@ -683,7 +684,7 @@ static void free_echo_data(struct hdp_echo_data *edata)
 		return;
 
 	if (edata->tid > 0)
-		g_source_remove(edata->tid);
+		timeout_remove(edata->tid);
 
 	if (edata->buf != NULL)
 		g_free(edata->buf);
@@ -1524,7 +1525,7 @@ end:
 	reply = g_dbus_create_reply(hdp_conn->msg, DBUS_TYPE_BOOLEAN, &value,
 							DBUS_TYPE_INVALID);
 	g_dbus_send_message(btd_get_dbus_connection(), reply);
-	g_source_remove(edata->tid);
+	timeout_remove(edata->tid);
 	edata->tid = 0;
 	g_free(edata->buf);
 	edata->buf = NULL;
@@ -1538,7 +1539,7 @@ end:
 	return FALSE;
 }
 
-static gboolean echo_timeout(gpointer data)
+static bool echo_timeout(gpointer data)
 {
 	struct hdp_channel *chan = data;
 	GIOChannel *io;
@@ -1606,10 +1607,9 @@ static void hdp_echo_connect_cb(struct mcap_mdl *mdl, GError *err,
 	g_io_add_watch(io, G_IO_ERR | G_IO_HUP | G_IO_NVAL | G_IO_IN,
 			check_echo, hdp_tmp_dc_data_ref(hdp_conn));
 
-	edata->tid = g_timeout_add_seconds_full(G_PRIORITY_DEFAULT,
-					ECHO_TIMEOUT, echo_timeout,
-					hdp_channel_ref(hdp_conn->hdp_chann),
-					(GDestroyNotify) hdp_channel_unref);
+	edata->tid = timeout_add_seconds(ECHO_TIMEOUT, echo_timeout,
+				hdp_channel_ref(hdp_conn->hdp_chann),
+				(timeout_destroy_func_t) hdp_channel_unref);
 
 	g_io_channel_unref(io);
 }
profiles/health/mcap.c +11 -10
index 32365fd50..be13af37a 100644
@@ -26,6 +26,7 @@
 #include "bluetooth/l2cap.h"
 #include "btio/btio.h"
 #include "src/log.h"
+#include "src/shared/timeout.h"
 
 #include "mcap.h"
 
@@ -43,7 +44,7 @@
 
 #define RELEASE_TIMER(__mcl) do {		\
 	if (__mcl->tid) {			\
-		g_source_remove(__mcl->tid);	\
+		timeout_remove(__mcl->tid);	\
 		__mcl->tid = 0;			\
 	}					\
 } while(0)
@@ -483,7 +484,7 @@ static int compare_mdl(gconstpointer a, gconstpointer b)
 		return 1;
 }
 
-static gboolean wait_response_timer(gpointer data)
+static bool wait_response_timer(gpointer data)
 {
 	struct mcap_mcl *mcl = data;
 
@@ -549,8 +550,8 @@ gboolean mcap_create_mdl(struct mcap_mcl *mcl,
 
 	mcl->mdls = g_slist_insert_sorted(mcl->mdls, mcap_mdl_ref(mdl),
 								compare_mdl);
-	mcl->tid = g_timeout_add_seconds(RESPONSE_TIMER, wait_response_timer,
-									mcl);
+	mcl->tid = timeout_add_seconds(RESPONSE_TIMER, wait_response_timer,
+					mcl, NULL);
 	return TRUE;
 }
 
@@ -587,8 +588,8 @@ gboolean mcap_reconnect_mdl(struct mcap_mdl *mdl,
 	mcl->state = MCL_ACTIVE;
 	mcl->priv_data = con;
 
-	mcl->tid = g_timeout_add_seconds(RESPONSE_TIMER, wait_response_timer,
-									mcl);
+	mcl->tid = timeout_add_seconds(RESPONSE_TIMER, wait_response_timer,
+					mcl, NULL);
 	return TRUE;
 }
 
@@ -607,8 +608,8 @@ static gboolean send_delete_req(struct mcap_mcl *mcl,
 
 	mcl->priv_data = con;
 
-	mcl->tid = g_timeout_add_seconds(RESPONSE_TIMER, wait_response_timer,
-									mcl);
+	mcl->tid = timeout_add_seconds(RESPONSE_TIMER, wait_response_timer,
+					mcl, NULL);
 	return TRUE;
 }
 
@@ -718,8 +719,8 @@ gboolean mcap_mdl_abort(struct mcap_mdl *mdl, mcap_mdl_notify_cb abort_cb,
 	con->user_data = user_data;
 
 	mcl->priv_data = con;
-	mcl->tid = g_timeout_add_seconds(RESPONSE_TIMER, wait_response_timer,
-									mcl);
+	mcl->tid = timeout_add_seconds(RESPONSE_TIMER, wait_response_timer,
+					mcl, NULL);
 	return TRUE;
 }
 
profiles/input/device.c +18 -15
index 326a3bcb9..50ae51855 100644
@@ -39,6 +39,7 @@
 #include "src/dbus-common.h"
 #include "src/error.h"
 #include "src/sdp-client.h"
+#include "src/shared/timeout.h"
 #include "src/shared/uhid.h"
 
 #include "device.h"
@@ -68,12 +69,12 @@ struct input_device {
 	struct hidp_connadd_req *req;
 	bool			disable_sdp;
 	enum reconnect_mode_t	reconnect_mode;
-	guint			reconnect_timer;
+	unsigned int		reconnect_timer;
 	uint32_t		reconnect_attempt;
 	struct bt_uhid		*uhid;
 	bool			uhid_created;
 	uint8_t			report_req_pending;
-	guint			report_req_timer;
+	unsigned int		report_req_timer;
 	uint32_t		report_rsp_id;
 	bool			virtual_cable_unplug;
 };
@@ -140,10 +141,10 @@ static void input_device_free(struct input_device *idev)
 	}
 
 	if (idev->reconnect_timer > 0)
-		g_source_remove(idev->reconnect_timer);
+		timeout_remove(idev->reconnect_timer);
 
 	if (idev->report_req_timer > 0)
-		g_source_remove(idev->report_req_timer);
+		timeout_remove(idev->report_req_timer);
 
 	g_free(idev);
 }
@@ -439,7 +440,7 @@ static void hidp_recv_ctrl_handshake(struct input_device *idev, uint8_t param)
 	if (pending_req_complete) {
 		idev->report_req_pending = 0;
 		if (idev->report_req_timer > 0) {
-			g_source_remove(idev->report_req_timer);
+			timeout_remove(idev->report_req_timer);
 			idev->report_req_timer = 0;
 		}
 		idev->report_rsp_id = 0;
@@ -499,7 +500,7 @@ static void hidp_recv_ctrl_data(struct input_device *idev, uint8_t param,
 
 	idev->report_req_pending = 0;
 	if (idev->report_req_timer > 0) {
-		g_source_remove(idev->report_req_timer);
+		timeout_remove(idev->report_req_timer);
 		idev->report_req_timer = 0;
 	}
 	idev->report_rsp_id = 0;
@@ -588,7 +589,7 @@ static gboolean ctrl_watch_cb(GIOChannel *chan, GIOCondition cond, gpointer data
 
 #define REPORT_REQ_TIMEOUT  3
 
-static gboolean hidp_report_req_timeout(gpointer data)
+static bool hidp_report_req_timeout(gpointer data)
 {
 	struct input_device *idev = data;
 	uint8_t pending_req_type;
@@ -669,8 +670,8 @@ static void hidp_send_set_report(struct uhid_event *ev, void *user_data)
 	if (sent) {
 		idev->report_req_pending = hdr;
 		idev->report_req_timer =
-			g_timeout_add_seconds(REPORT_REQ_TIMEOUT,
-					hidp_report_req_timeout, idev);
+			timeout_add_seconds(REPORT_REQ_TIMEOUT,
+					hidp_report_req_timeout, idev, NULL);
 		idev->report_rsp_id = ev->u.set_report.id;
 	} else
 		uhid_send_set_report_reply(idev, ev->u.set_report.id, EIO);
@@ -712,8 +713,9 @@ static void hidp_send_get_report(struct uhid_event *ev, void *user_data)
 	if (sent) {
 		idev->report_req_pending = hdr;
 		idev->report_req_timer =
-			g_timeout_add_seconds(REPORT_REQ_TIMEOUT,
-						hidp_report_req_timeout, idev);
+			timeout_add_seconds(REPORT_REQ_TIMEOUT,
+						hidp_report_req_timeout, idev,
+						NULL);
 		idev->report_rsp_id = ev->u.get_report.id;
 	} else
 		uhid_send_get_report_reply(idev, NULL, 0, ev->u.get_report.id,
@@ -1282,7 +1284,7 @@ static int dev_connect(struct input_device *idev)
 	return -EIO;
 }
 
-static gboolean input_device_auto_reconnect(gpointer user_data)
+static bool input_device_auto_reconnect(gpointer user_data)
 {
 	struct input_device *idev = user_data;
 
@@ -1352,12 +1354,13 @@ static void input_device_enter_reconnect_mode(struct input_device *idev)
 		return;
 
 	if (idev->reconnect_timer > 0)
-		g_source_remove(idev->reconnect_timer);
+		timeout_remove(idev->reconnect_timer);
 
 	DBG("registering auto-reconnect");
 	idev->reconnect_attempt = 0;
-	idev->reconnect_timer = g_timeout_add_seconds(30,
-					input_device_auto_reconnect, idev);
+	idev->reconnect_timer = timeout_add_seconds(30,
+					input_device_auto_reconnect, idev,
+					NULL);
 
 }
 
profiles/network/bnep.c +7 -5
index 7e777e29c..f94f1da8a 100644
@@ -32,6 +32,7 @@
 #include "lib/uuid.h"
 
 #include "src/log.h"
+#include "src/shared/timeout.h"
 #include "src/shared/util.h"
 #include "btio/btio.h"
 
@@ -54,7 +55,7 @@ struct bnep {
 	bdaddr_t	dst_addr;
 	char	iface[16];
 	guint	attempts;
-	guint	setup_to;
+	unsigned int	setup_to;
 	guint	watch;
 	bnep_connect_cb	conn_cb;
 	void	*conn_data;
@@ -209,7 +210,7 @@ static gboolean bnep_setup_cb(GIOChannel *chan, GIOCondition cond,
 		return FALSE;
 
 	if (session->setup_to > 0) {
-		g_source_remove(session->setup_to);
+		timeout_remove(session->setup_to);
 		session->setup_to = 0;
 	}
 
@@ -313,7 +314,7 @@ static int bnep_setup_conn_req(struct bnep *session)
 	return 0;
 }
 
-static gboolean bnep_conn_req_to(gpointer user_data)
+static bool bnep_conn_req_to(gpointer user_data)
 {
 	struct bnep *session = user_data;
 
@@ -402,8 +403,9 @@ int bnep_connect(struct bnep *session, bnep_connect_cb conn_cb,
 	if (err < 0)
 		return err;
 
-	session->setup_to = g_timeout_add_seconds(CON_SETUP_TO,
-						bnep_conn_req_to, session);
+	session->setup_to = timeout_add_seconds(CON_SETUP_TO,
+						bnep_conn_req_to, session,
+						NULL);
 	return 0;
 }
 
profiles/sap/server.c +7 -6
index b0a454ced..82365fca9 100644
@@ -31,6 +31,7 @@
 #include "src/log.h"
 #include "src/error.h"
 #include "src/dbus-common.h"
+#include "src/shared/timeout.h"
 #include "src/shared/util.h"
 
 #include "sap.h"
@@ -62,7 +63,7 @@ struct sap_connection {
 	GIOChannel *io;
 	uint32_t state;
 	uint8_t processing_req;
-	guint timer_id;
+	unsigned int timer_id;
 };
 
 struct sap_server {
@@ -74,7 +75,7 @@ struct sap_server {
 
 static void start_guard_timer(struct sap_server *server, guint interval);
 static void stop_guard_timer(struct sap_server *server);
-static gboolean guard_timeout(gpointer data);
+static bool guard_timeout(gpointer data);
 
 static size_t add_result_parameter(uint8_t result,
 					struct sap_parameter *param)
@@ -554,8 +555,8 @@ static void start_guard_timer(struct sap_server *server, guint interval)
 		return;
 
 	if (!conn->timer_id)
-		conn->timer_id = g_timeout_add_seconds(interval, guard_timeout,
-								server);
+		conn->timer_id = timeout_add_seconds(interval, guard_timeout,
+								server, NULL);
 	else
 		error("Timer is already active.");
 }
@@ -565,12 +566,12 @@ static void stop_guard_timer(struct sap_server *server)
 	struct sap_connection *conn = server->conn;
 
 	if (conn  && conn->timer_id) {
-		g_source_remove(conn->timer_id);
+		timeout_remove(conn->timer_id);
 		conn->timer_id = 0;
 	}
 }
 
-static gboolean guard_timeout(gpointer data)
+static bool guard_timeout(gpointer data)
 {
 	struct sap_server *server = data;
 	struct sap_connection *conn = server->conn;
Signature · 1 line
2.18.0
#2 lore raw
In reply to Frédéric Danis

This is automated email and please do not reply to this email!

Dear submitter,

Thank you for submitting the patches to the linux bluetooth mailing list. This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/project/bluetooth/list/?series=449327

---Test result---

##############################
Test: CheckPatch - PASS

##############################
Test: CheckGitLint - PASS

##############################
Test: CheckBuild - PASS

##############################
Test: MakeCheck - PASS

---
Regards,
Linux Bluetooth

maintainer Applied
#3 lore raw

Re: [PATCH Bluez v5 1/4] shared/timeout: Add timeout_add_seconds abstraction

In reply to Frédéric Danis

Hi Frédéric,

Quoted 156 lines · Frédéric Danis

g_timeout_add_seconds() call doesn't ensure the time for the first call of the timer if the delay is less or equal to 1 second.
In case of a 0 delay call g_idle_add() instead of g_timeout_add_seconds().

---
 src/shared/tester.c           | 16 +++++++++-------
 src/shared/timeout-ell.c      |  6 ++++++
 src/shared/timeout-glib.c     | 27 +++++++++++++++++++++++++++
 src/shared/timeout-mainloop.c |  6 ++++++
 src/shared/timeout.h          |  3 +++
 5 files changed, 51 insertions(+), 7 deletions(-)
src/shared/tester.c +9 -7
index af33a79cd..c07cbc11c 100644
@@ -36,6 +36,7 @@
 #include "src/shared/util.h"
 #include "src/shared/tester.h"
 #include "src/shared/log.h"
+#include "src/shared/timeout.h"
 
 #define COLOR_OFF      "\x1B[0m"
 #define COLOR_BLACK    "\x1B[0;30m"
@@ -126,7 +127,7 @@ static void test_destroy(gpointer data)
        struct test_case *test = data;
 
        if (test->timeout_id > 0)
-               g_source_remove(test->timeout_id);
+               timeout_remove(test->timeout_id);
 
        if (test->teardown_id > 0)
                g_source_remove(test->teardown_id);
@@ -429,7 +430,7 @@ static gboolean teardown_callback(gpointer user_data)
        return FALSE;
 }
 
-static gboolean test_timeout(gpointer user_data)
+static bool test_timeout(gpointer user_data)
 {
        struct test_case *test = user_data;
 
@@ -470,8 +471,9 @@ static void next_test_case(void)
        test->start_time = g_timer_elapsed(test_timer, NULL);
 
        if (test->timeout > 0)
-               test->timeout_id = g_timeout_add_seconds(test->timeout,
-                                                       test_timeout, test);
+               test->timeout_id = timeout_add_seconds(test->timeout,
+                                                       test_timeout, test,
+                                                       NULL);
 
        test->stage = TEST_STAGE_PRE_SETUP;
 
@@ -542,7 +544,7 @@ void tester_pre_setup_failed(void)
                return;
 
        if (test->timeout_id > 0) {
-               g_source_remove(test->timeout_id);
+               timeout_remove(test->timeout_id);
                test->timeout_id = 0;
        }
 
@@ -583,7 +585,7 @@ void tester_setup_failed(void)
        test->stage = TEST_STAGE_POST_TEARDOWN;
 
        if (test->timeout_id > 0) {
-               g_source_remove(test->timeout_id);
+               timeout_remove(test->timeout_id);
                test->timeout_id = 0;
        }
 
@@ -606,7 +608,7 @@ static void test_result(enum test_result result)
                return;
 
        if (test->timeout_id > 0) {
-               g_source_remove(test->timeout_id);
+               timeout_remove(test->timeout_id);
                test->timeout_id = 0;
        }
 
src/shared/timeout-ell.c +6 -0
index 023364069..6416d8590 100644
@@ -101,3 +101,9 @@ void timeout_remove(unsigned int id)
        if (to)
                l_timeout_remove(to);
 }
+
+unsigned int timeout_add_seconds(unsigned int timeout, timeout_func_t func,
+                       void *user_data, timeout_destroy_func_t destroy)
+{
+       return timeout_add(timeout * 1000, func, user_data, destroy);
+}
src/shared/timeout-glib.c +27 -0
index 8bdb7a662..3268d480c 100644
@@ -71,3 +71,30 @@ void timeout_remove(unsigned int id)
        if (source)
                g_source_destroy(source);
 }
+
+unsigned int timeout_add_seconds(unsigned int timeout, timeout_func_t func,
+                       void *user_data, timeout_destroy_func_t destroy)
+{
+       struct timeout_data *data;
+       guint id;
+
+       data = g_try_new0(struct timeout_data, 1);
+       if (!data)
+               return 0;
+
+       data->func = func;
+       data->destroy = destroy;
+       data->user_data = user_data;
+
+       if (!timeout)
+               id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, timeout_callback,
+                                                       data, timeout_destroy);
+       else
+               id = g_timeout_add_seconds_full(G_PRIORITY_DEFAULT, timeout,
+                                                       timeout_callback, data,
+                                                       timeout_destroy);
+       if (!id)
+               g_free(data);
+
+       return id;
+}
src/shared/timeout-mainloop.c +6 -0
index 5ffa65c2a..9be803cda 100644
@@ -71,3 +71,9 @@ void timeout_remove(unsigned int id)
 
        mainloop_remove_timeout((int) id);
 }
+
+unsigned int timeout_add_seconds(unsigned int timeout, timeout_func_t func,
+                       void *user_data, timeout_destroy_func_t destroy)
+{
+       return timeout_add(timeout * 1000, func, user_data, destroy);
+}
src/shared/timeout.h +3 -1
index 7e22345dd..0945c3318 100644
@@ -16,3 +16,6 @@ typedef void (*timeout_destroy_func_t)(void *user_data);
 unsigned int timeout_add(unsigned int timeout, timeout_func_t func,
                        void *user_data, timeout_destroy_func_t destroy);
 void timeout_remove(unsigned int id);
+
+unsigned int timeout_add_seconds(unsigned int timeout, timeout_func_t func,
+                       void *user_data, timeout_destroy_func_t destroy);
--
Quoted 1 line

2.18.0

Applied, note that I changed the commit message a little bit.

Signature · 1 line
Luiz Augusto von Dentz