Fix BLE HoG reconnect issues

Combination of three patches sent upstream:
[PATCH 1/3] core: Fix stop_passive_scanning_complete()

Consider MGMT_STATUS_REJECTED as success for stop_passive_scanning().
MGMT_STATUS_REJECTED may be returned from kernel because the passive
scan timer had expired in kernel and passive scan was disabled just
around the time we called stop_passive_scanning().

This issue can be seen with BLE HoG devices trying to reconnect to BlueZ:
bluetoothd[931]: src/adapter.c:device_found_callback() hci0 addr ...
bluetoothd[931]: src/device.c:device_set_legacy() legacy 0
bluetoothd[931]: src/device.c:device_set_rssi() rssi -75
bluetoothd[931]: src/adapter.c:stop_passive_scanning()
bluetoothd[931]: src/adapter.c:discovering_callback() hci0 type 6 discovering 0
bluetoothd[931]: src/adapter.c:trigger_passive_scanning()
bluetoothd[931]: src/adapter.c:stop_passive_scanning_complete() status 0x0b (Rejected)
bluetoothd[931]: Stopping passive scanning failed: Rejected

[PATCH 2/3] core: Fix discovering_callback()

discovering_callback() should not call trigger_passive_scanning() when
outgoing BLE connection is pending.

This issue can be seen with BLE HoG devices trying to reconnect to BlueZ:
bluetoothd[931]: src/adapter.c:device_found_callback() hci0 addr ...
bluetoothd[931]: src/device.c:device_set_legacy() legacy 0
bluetoothd[931]: src/device.c:device_set_rssi() rssi -75
bluetoothd[931]: src/adapter.c:stop_passive_scanning()
bluetoothd[931]: src/adapter.c:discovering_callback() hci0 type 6 discovering 0
bluetoothd[931]: src/adapter.c:trigger_passive_scanning()
bluetoothd[931]: src/adapter.c:stop_passive_scanning_complete() status 0x0b (Rejected)
bluetoothd[931]: Stopping passive scanning failed: Rejected

[PATCH 3/3] core: Fix adapter_connect_list_add()

adapter_connect_list_add() needs to ensure that trigger_passive_scanning() is
called even for existing devices on the connect list.

This issue can be seen with BLE HoG remotes trying to reconnect to BlueZ. If
the connection fails, att_error_cb() adds the failed device back to connect
list, but since the device is already on the list, no code ends up calling
trigger_passive_scanning(). After this point, passive scanning is disabled
and remains disabled for good, and BLE reconnects to BlueZ no longer work.

bluetoothd[931]: src/adapter.c:device_found_callback() hci0 addr ...
bluetoothd[931]: src/device.c:device_set_legacy() legacy 0
bluetoothd[931]: src/adapter.c:stop_passive_scanning()
bluetoothd[931]: src/adapter.c:stop_passive_scanning_complete() status 0x00 (Success)
bluetoothd[931]: src/device.c:device_connect_le() Connection attempt to: ...
bluetoothd[931]: src/adapter.c:discovering_callback() hci0 type 6 discovering 0
bluetoothd[931]: src/adapter.c:connect_failed_callback() hci0 ... status 2
bluetoothd[931]: plugins/policy.c:conn_fail_cb() status 2
bluetoothd[931]: src/adapter.c:bonding_attempt_complete() hci0 bdaddr ... type 1 status 0x2
bluetoothd[931]: src/device.c:device_bonding_complete() bonding (nil) status 0x02
bluetoothd[931]: src/device.c:device_bonding_failed() status 2
bluetoothd[931]: src/adapter.c:resume_discovery()
bluetoothd[931]: src/device.c:att_connect_cb() connect error: Transport endpoint is not connected (134)
bluetoothd[931]: src/device.c:att_error_cb() Enabling automatic connections
bluetoothd[931]: src/adapter.c:adapter_connect_list_add() ignoring already added device ...
... passive scan is disabled and remains disabled for good ...

Change-Id: I0d3ea3554541673f5bd96a27ce5f63f3eb633b83
diff --git a/src/adapter.c b/src/adapter.c
index b4878a3..3eb53c0 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -1228,7 +1228,12 @@
 	dev = adapter->connect_le;
 	adapter->connect_le = NULL;
 
-	if (status != MGMT_STATUS_SUCCESS) {
+	/*
+	 * MGMT_STATUS_REJECTED may be returned from kernel because the passive
+	 * scan timer had expired in kernel and passive scan was disabled just
+	 * around the time we called stop_passive_scanning().
+	 */
+	if (status != MGMT_STATUS_SUCCESS && status != MGMT_STATUS_REJECTED) {
 		error("Stopping passive scanning failed: %s",
 							mgmt_errstr(status));
 		return;
@@ -1500,7 +1505,8 @@
 	 * passive scanning attempt.
 	 */
 	if (!adapter->discovery_list) {
-		trigger_passive_scanning(adapter);
+		if (!adapter->connect_le)
+			trigger_passive_scanning(adapter);
 		return;
 	}
 
@@ -3019,7 +3025,7 @@
 	if (g_slist_find(adapter->connect_list, device)) {
 		DBG("ignoring already added device %s",
 						device_get_path(device));
-		return 0;
+		goto done;
 	}
 
 	if (!(adapter->supported_settings & MGMT_SETTING_LE)) {
@@ -3032,6 +3038,7 @@
 	DBG("%s added to %s's connect_list", device_get_path(device),
 							adapter->system_name);
 
+done:
 	if (!(adapter->current_settings & MGMT_SETTING_POWERED))
 		return 0;