]> sigrok.org Git - libsigrok.git/commitdiff
bt: apply 20s timeout to BLE connect(2) attempts
authorGerhard Sittig <redacted>
Tue, 29 Oct 2019 16:42:34 +0000 (17:42 +0100)
committerGerhard Sittig <redacted>
Tue, 29 Oct 2019 16:42:34 +0000 (17:42 +0100)
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.

src/bt/bt_bluez.c

index 167216087133ba60c8aa6371444183aa30ee220b..e38727348a0ddaf574fc505e159c47170c1aa847 100644 (file)
@@ -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;