summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorLuiz Augusto von Dentz <luiz.von.dentz@intel.com>2022-02-25 13:53:19 -0800
committerLuiz Augusto von Dentz <luiz.von.dentz@intel.com>2022-02-25 15:35:03 -0800
commit2ae3ce9e6d6ff83aa422b10e11fb5d0e5674c1a4 (patch)
tree67741ccda346f8c3b0e3ba3799e8b3ff12a6074a /tools
parentbf09e1f048ba1c30888078b9cc71f879b7b6909d (diff)
downloadbluez-2ae3ce9e6d6ff83aa422b10e11fb5d0e5674c1a4.tar.gz
btproxy: Attempt to bind the next index
If no specific index is given attempt to bind the next index if the current one is already in use.
Diffstat (limited to 'tools')
-rw-r--r--tools/btproxy.c21
1 files changed, 17 insertions, 4 deletions
diff --git a/tools/btproxy.c b/tools/btproxy.c
index f6143038c..94ca1c7fd 100644
--- a/tools/btproxy.c
+++ b/tools/btproxy.c
@@ -47,8 +47,9 @@ struct sockaddr_hci {
unsigned short hci_channel;
};
#define HCI_CHANNEL_USER 1
+#define HCI_INDEX_NONE 0xffff
-static uint16_t hci_index = 0;
+static uint16_t hci_index = HCI_INDEX_NONE;
static bool client_active = false;
static bool debug_enabled = false;
static bool emulate_ecc = false;
@@ -535,9 +536,12 @@ static bool setup_proxy(int host_fd, bool host_shutdown,
static int open_channel(uint16_t index)
{
struct sockaddr_hci addr;
- int fd;
+ int fd, err;
+
+ if (index == HCI_INDEX_NONE)
+ index = 0;
- printf("Opening user channel for hci%u\n", hci_index);
+ printf("Opening user channel for hci%u\n", index);
fd = socket(PF_BLUETOOTH, SOCK_RAW | SOCK_CLOEXEC, BTPROTO_HCI);
if (fd < 0) {
@@ -551,7 +555,16 @@ static int open_channel(uint16_t index)
addr.hci_channel = HCI_CHANNEL_USER;
if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
+ err = -errno;
close(fd);
+
+ /* Open next available controller if no specific index was
+ * provided and the error indicates that the controller.
+ */
+ if (hci_index == HCI_INDEX_NONE &&
+ (err == -EBUSY || err == -EUSERS))
+ return open_channel(++index);
+
perror("Failed to bind Bluetooth socket");
return -1;
}
@@ -588,7 +601,7 @@ static void server_callback(int fd, uint32_t events, void *user_data)
return;
}
- if (client_active) {
+ if (client_active && hci_index != HCI_INDEX_NONE) {
fprintf(stderr, "Active client already present\n");
close(host_fd);
return;