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.
#define LOG_PREFIX "bt-bluez"
/** @endcond */
#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
#define STORE_MAC_REVERSE 1
#define ACCEPT_NONSEP_MAC 1
struct sockaddr_l2 sl2;
bdaddr_t mac;
int s, ret;
struct sockaddr_l2 sl2;
bdaddr_t mac;
int s, ret;
+ 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;
str2ba(desc->remote_addr, &mac);
memcpy(&sl2.l2_bdaddr, &mac, sizeof(sl2.l2_bdaddr));
sl2.l2_bdaddr_type = BDADDR_LE_PUBLIC;
continue;
if (!(fds[0].revents & POLLOUT))
continue;
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;
} while (1);
memset(fds, 0, sizeof(fds));
fds[0].fd = s;