From: Gerhard Sittig Date: Tue, 29 Oct 2019 16:42:34 +0000 (+0100) Subject: bt: apply 20s timeout to BLE connect(2) attempts X-Git-Url: http://sigrok.org/gitweb/?a=commitdiff_plain;h=113de3a57290347b652c93eebc7ec0b32b1bea6d;p=libsigrok.git bt: apply 20s timeout to BLE connect(2) attempts Even working BLE devices won't immediately succeed in the first call to connect(2), which is why the "in progress" phase was added. But absent peers made the previous implementation try forever, getting stuck in the sr_bt_connect_ble() call. Try to balance these two constraints. Do terminate BLE connect attempts after a generous timeout. When this 20s period passed, there probably is a configuration error or unresponsive peer. Yet the timeout needs to be in this ballpark to not erroneously fail for working setups. --- diff --git a/src/bt/bt_bluez.c b/src/bt/bt_bluez.c index 16721608..e3872734 100644 --- a/src/bt/bt_bluez.c +++ b/src/bt/bt_bluez.c @@ -91,6 +91,7 @@ #define LOG_PREFIX "bt-bluez" /** @endcond */ +#define CONNECT_BLE_TIMEOUT 20 /* Connect timeout in seconds. */ #define STORE_MAC_REVERSE 1 #define ACCEPT_NONSEP_MAC 1 @@ -668,6 +669,7 @@ SR_PRIV int sr_bt_connect_ble(struct sr_bt_desc *desc) struct sockaddr_l2 sl2; bdaddr_t mac; int s, ret; + gint64 deadline; if (!desc) return -1; @@ -710,6 +712,8 @@ SR_PRIV int sr_bt_connect_ble(struct sr_bt_desc *desc) } } + deadline = g_get_monotonic_time(); + deadline += CONNECT_BLE_TIMEOUT * 1000 * 1000; str2ba(desc->remote_addr, &mac); memcpy(&sl2.l2_bdaddr, &mac, sizeof(sl2.l2_bdaddr)); sl2.l2_bdaddr_type = BDADDR_LE_PUBLIC; @@ -745,6 +749,10 @@ SR_PRIV int sr_bt_connect_ble(struct sr_bt_desc *desc) continue; if (!(fds[0].revents & POLLOUT)) continue; + if (g_get_monotonic_time() >= deadline) { + sr_warn("Connect attempt timed out"); + return SR_ERR_IO; + } } while (1); memset(fds, 0, sizeof(fds)); fds[0].fd = s;