Diff between c16853b3fd2f7c533062fc151bf6e80805d2e64e and bf7446cbcf1be81c6410fc02b2649ea1a12e2700

Changed Files

File Additions Deletions Status
src/adapter.c +35 -5 modified

Full Patch

diff --git a/src/adapter.c b/src/adapter.c
index 5ba983f..1baf957 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -84,6 +84,7 @@
 #define MODE_DISCOVERABLE	0x02
 #define MODE_UNKNOWN		0xff
 
+#define CONN_SCAN_TIMEOUT (3)
 #define IDLE_DISCOV_TIMEOUT (5)
 #define TEMP_DEV_TIMEOUT (3 * 60)
 
@@ -150,6 +151,7 @@ struct btd_adapter {
 	GSList *discovery_list;		/* list of discovery clients */
 	GSList *discovery_found;	/* list of found devices */
 	guint discovery_idle_timeout;	/* timeout between discovery runs */
+	guint passive_scan_timeout;	/* timeout between passive scans */
 	guint temp_devices_timeout;	/* timeout for temporary devices */
 
 	guint pairable_timeout_id;	/* pairable timeout id */
@@ -1103,15 +1105,34 @@ static void passive_scanning_complete(uint8_t status, uint16_t length,
 	}
 }
 
-static void trigger_passive_scanning(struct btd_adapter *adapter)
+static gboolean passive_scanning_timeout(gpointer user_data)
 {
+	struct btd_adapter *adapter = user_data;
 	struct mgmt_cp_start_discovery cp;
 
+	adapter->passive_scan_timeout = 0;
+
+	cp.type = (1 << BDADDR_LE_PUBLIC) | (1 << BDADDR_LE_RANDOM);
+
+	mgmt_send(adapter->mgmt, MGMT_OP_START_DISCOVERY,
+				adapter->dev_id, sizeof(cp), &cp,
+				passive_scanning_complete, adapter, NULL);
+
+	return FALSE;
+}
+
+static void trigger_passive_scanning(struct btd_adapter *adapter)
+{
 	if (!(adapter->current_settings & MGMT_SETTING_LE))
 		return;
 
 	DBG("");
 
+	if (adapter->passive_scan_timeout > 0) {
+		g_source_remove(adapter->passive_scan_timeout);
+		adapter->passive_scan_timeout = 0;
+	}
+
 	/*
 	 * If any client is running a discovery right now, then do not
 	 * even try to start passive scanning.
@@ -1139,11 +1160,16 @@ static void trigger_passive_scanning(struct btd_adapter *adapter)
 	if (!adapter->connect_list)
 		return;
 
-	cp.type = (1 << BDADDR_LE_PUBLIC) | (1 << BDADDR_LE_RANDOM);
+	adapter->passive_scan_timeout = g_timeout_add_seconds(CONN_SCAN_TIMEOUT,
+					passive_scanning_timeout, adapter);
+}
 
-	mgmt_send(adapter->mgmt, MGMT_OP_START_DISCOVERY,
-				adapter->dev_id, sizeof(cp), &cp,
-				passive_scanning_complete, adapter, NULL);
+static void cancel_passive_scanning(struct btd_adapter *adapter)
+{
+	if (adapter->passive_scan_timeout > 0) {
+		g_source_remove(adapter->passive_scan_timeout);
+		adapter->passive_scan_timeout = 0;
+	}
 }
 
 static void trigger_start_discovery(struct btd_adapter *adapter, guint delay);
@@ -1242,6 +1268,8 @@ static void trigger_start_discovery(struct btd_adapter *adapter, guint delay)
 
 	DBG("");
 
+	cancel_passive_scanning(adapter);
+
 	if (adapter->discovery_idle_timeout > 0) {
 		g_source_remove(adapter->discovery_idle_timeout);
 		adapter->discovery_idle_timeout = 0;
@@ -4074,6 +4102,8 @@ static void adapter_stop(struct btd_adapter *adapter)
 	/* check pending requests */
 	reply_pending_requests(adapter);
 
+	cancel_passive_scanning(adapter);
+
 	if (adapter->discovery_list) {
 		g_slist_free_full(adapter->discovery_list, discovery_free);
 		adapter->discovery_list = NULL;