Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp7716743ioo; Fri, 3 Jun 2022 11:56:31 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzQbDr5VyTDVPBvbYKjiVfEwYj+dEuXTZzPBSmfJfV8P+CDSd+fSrdII8iALR3DhVKzmdVx X-Received: by 2002:a17:90a:ba0b:b0:1e0:97b4:e0ad with SMTP id s11-20020a17090aba0b00b001e097b4e0admr47387439pjr.19.1654282591536; Fri, 03 Jun 2022 11:56:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654282591; cv=none; d=google.com; s=arc-20160816; b=WAmaqsBgoUvonANMH+kDsMkQ5Xc1XCSHgBQKH6O09x1NZPtbu/hYsXAllBF1yZ864n YkamfY+aCvOil6GvbXBm3jegt+XSbAfGzKqx++KgsEwI6OF0/smL9w1flv/ZyE1FEtqJ eIn3Ctz4XSwpGaroo9oeUudDPkbO6ZvogeaFZeVZ0doUHd592cDJ6UKB21ISMQk0rCBs CMJ9iQPMrqmmZBucDyWxEVUiq4MDGyX8PycBDGd9FcgGE2G/eVRpXr3EDdOpypbw95tl 3Y3LKdzevBFat8UBFhacEDmX2LTeBj0V0WkX0d98ZU5hItrBpVPnwWTZl03VtKhXAWyz 1Eig== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:to:from:dkim-signature; bh=zL58F+Dyveu8FDnT1Hco+AQ+UirZSNpReA0Ka/7dVug=; b=PVDZ56+X3smU1VgFXTmw4yHX5lcpUQlZ1Pyro1s5S/31nn1weoumYxy4zPyJW3H5PB rd/dXXb/Nuc1D9IUN/ZaVMPXN5QBAfGKdNC+zAs2+KJjGABIkUs+mpyh4XLhcEYdxpX4 Skmn5z8tqEh9I1enC8K7W5/KHTpudcoaNrzY/Eryql7TIjtSaKaYDjStov9sGK1Q17f9 Na0OJJynffr41i53RQmqHDPo0JMDqxq4lTTYNdd9avsw6dPzpBmSeDKMJqKDRUAmywmP AKWqRFXnReMbzC1oDDS/UcNrH1cxMRCaIqIoxu5g22VjCjHPHiuaFJ6yk/a7OIAcC5z0 ENKg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=JSqGp3NR; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=collabora.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d19-20020a170902e15300b00163efcfadf2si9731213pla.288.2022.06.03.11.56.02; Fri, 03 Jun 2022 11:56:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=JSqGp3NR; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=collabora.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245238AbiFCOzC (ORCPT + 99 others); Fri, 3 Jun 2022 10:55:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41454 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230474AbiFCOy6 (ORCPT ); Fri, 3 Jun 2022 10:54:58 -0400 Received: from bhuna.collabora.co.uk (bhuna.collabora.co.uk [46.235.227.227]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E302939140 for ; Fri, 3 Jun 2022 07:54:56 -0700 (PDT) Received: from [127.0.0.1] (localhost [127.0.0.1]) (Authenticated sender: fdanis) with ESMTPSA id B41D61F45734 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1654268095; bh=yemU3lMc3LYLi4U6FkOON5xHPf49RSlOlszwblj3jhE=; h=From:To:Subject:Date:From; b=JSqGp3NR2u2eMgTsCjnH0VXHWUc0lySW/QfWzAajd1Ci6D6hdu8CIRCmJw8sy1TiJ wZpfEOOlMwI5rC+wd1UiwyW6fmFI4V0BkcqsEE6+x2ibK5NjJYzxrjkXtoK1hkXn+p PptwBKs/n/9JiwSH1FSvowtkMWPUA1R9LdPM5yHE4LtUNNQYP/+0dJRP+mtj+ukOyb TGLltR2WZ4/lLviJjgDvKk4+hRX+GzIirVJCG4CJ0kpax/jLgdTisD6HArE3OIaqMQ LzbgT2hR7wa5ErywnAFZd6cWc6dDbpjpRtxlSuLT02m9NY+PPSn3LoYHdgWv9H5Tc1 fS3hhywFZiAWQ== From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Danis?= To: linux-bluetooth@vger.kernel.org Subject: [PATCH v3] btproxy: Allow to select multiple BT controllers Date: Fri, 3 Jun 2022 16:54:47 +0200 Message-Id: <20220603145447.45655-1-frederic.danis@collabora.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE,UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org When running on a computer with a real Bluetooth controller (e.g. hci0) and multiple emulators (e.g. hci1 and hci2) it isn't possible to use the emulators with 2 test-runner vms. If btproxy is started without index parameter the first test-runner will use hci0, and btprox can't be started with multiple index parameters (e.g. -i1 -i2). This patch keeps the old beahvior when used without -i option, in this case it will try to use the first controller available. It also allows to select multiple controllers to be used by btproxy. --- changes since v2: - replace get_next_hci_index() by util_get_uid() tools/btproxy.c | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/tools/btproxy.c b/tools/btproxy.c index 94ca1c7fd..305ef7893 100644 --- a/tools/btproxy.c +++ b/tools/btproxy.c @@ -48,8 +48,9 @@ struct sockaddr_hci { }; #define HCI_CHANNEL_USER 1 #define HCI_INDEX_NONE 0xffff +#define HCI_INDEX_MAX 16 -static uint16_t hci_index = HCI_INDEX_NONE; +static uint64_t hci_index = HCI_INDEX_NONE; static bool client_active = false; static bool debug_enabled = false; static bool emulate_ecc = false; @@ -533,13 +534,18 @@ static bool setup_proxy(int host_fd, bool host_shutdown, return true; } -static int open_channel(uint16_t index) +static int open_channel(uint64_t hci_index) { struct sockaddr_hci addr; int fd, err; + uint8_t index; - if (index == HCI_INDEX_NONE) - index = 0; + index = util_get_uid(&hci_index, HCI_INDEX_MAX); + if (index == 0) { + perror("No controller available"); + return -1; + } + index--; printf("Opening user channel for hci%u\n", index); @@ -558,12 +564,11 @@ static int open_channel(uint16_t index) err = -errno; close(fd); - /* Open next available controller if no specific index was - * provided and the error indicates that the controller. + /* Open next available controller if the error indicates + * that the controller is in use. */ - if (hci_index == HCI_INDEX_NONE && - (err == -EBUSY || err == -EUSERS)) - return open_channel(++index); + if (err == -EBUSY || err == -EUSERS) + return open_channel(hci_index); perror("Failed to bind Bluetooth socket"); return -1; @@ -601,12 +606,6 @@ static void server_callback(int fd, uint32_t events, void *user_data) return; } - if (client_active && hci_index != HCI_INDEX_NONE) { - fprintf(stderr, "Active client already present\n"); - close(host_fd); - return; - } - dev_fd = open_channel(hci_index); if (dev_fd < 0) { close(host_fd); @@ -800,6 +799,7 @@ int main(int argc, char *argv[]) for (;;) { int opt; + int index; opt = getopt_long(argc, argv, "rc:l::u::p:i:aezdvh", main_options, NULL); @@ -844,7 +844,13 @@ int main(int argc, char *argv[]) usage(); return EXIT_FAILURE; } - hci_index = atoi(str); + index = atoi(str) + 1; + if (index > HCI_INDEX_MAX) { + fprintf(stderr, "Invalid controller index\n"); + usage(); + return EXIT_FAILURE; + } + util_clear_uid(&hci_index, index); break; case 'a': type = HCI_AMP; @@ -884,6 +890,9 @@ int main(int argc, char *argv[]) return EXIT_FAILURE; } + if (hci_index == HCI_INDEX_NONE) + hci_index = 0; + mainloop_init(); if (connect_address || use_redirect) { -- 2.25.1