Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp127942iof; Sun, 5 Jun 2022 23:11:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzGFeLWq1V0BtRoxS3Uxbs+IJtpA/L76ha7/HuvkdfEzqci1M8A+nV7hrVMTl0iMrWXCVch X-Received: by 2002:a65:52cd:0:b0:3f5:f3fb:6780 with SMTP id z13-20020a6552cd000000b003f5f3fb6780mr20154690pgp.150.1654495919077; Sun, 05 Jun 2022 23:11:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654495919; cv=none; d=google.com; s=arc-20160816; b=feInBJqlIs63Eb11ii8hbyDpzf4NCHHiRWylb+5eZrTIh3Oc84WN91RIfrpvalI1A7 dlAzGyB2U9eUuYU1UaUnSPPocSmuVRCJjnjc35ugRF1rdEqggXNqf/r9BZQSH5F2wBJr w+50NvXMHK92EhfgGKezNWy8RwuPxtrcBTvjd3J4qnQk3yk7Awpv3L+p0vc9CghjEUWb Gjm8sXPeUFiQPTSWZOBIc6bwwMPYuD51NbInZyyLCfldiUMpjc6Ut3re7/Qqcet700DS MWnLVGHtUBULZ8G+PZkzR7bLGcplxCP7IGX0rN7w/BxbDfCPNYJcyZ+XHiCC4MHXUViY d0pg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=tql6r0xaSswp5SqrcM4I2Ljmq0RBbtZh/E5gnuh/RHg=; b=Y0HzI73zPspvM7N+PWQyCAXZVRNfeh0jk8ydJhp7FHMhFng/+kCILJxWLPN43Ah+WV 9dLYJk+LrJFas664QQj6NGNUIH7nWLibV04cAgwkNRcyRIHIxT2jEZOU3SIDsZmcazKT G0vUzzn5NoKZ+SEXh2211BRBADR+x7IwN8oIyGrZ0s3Oq+PZNFaQ/R6QlruddoHRAhv8 03JUNQPE+Q4+SgMnAe15rrOWMoS1BcqMwCBmKXMYBh5NDpOXJf1nzzIEUCbfu9LYy48h lijpLxb0iGh+DzzVGf9P/muFuTAXKEM6ZXJ8S7ZpSEUzogh4dZeg8bGOa1e3XdbjTTBU eFuw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=TiHtv5KW; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 2620:137:e000::1:18 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id k63-20020a638442000000b003fcb9d205ecsi19376356pgd.200.2022.06.05.23.11.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Jun 2022 23:11:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=TiHtv5KW; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=collabora.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 7D127318C6B; Sun, 5 Jun 2022 21:51:09 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231258AbiFCOlB (ORCPT + 99 others); Fri, 3 Jun 2022 10:41:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38266 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S245123AbiFCOlA (ORCPT ); Fri, 3 Jun 2022 10:41:00 -0400 Received: from bhuna.collabora.co.uk (bhuna.collabora.co.uk [46.235.227.227]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AFA8550464 for ; Fri, 3 Jun 2022 07:40:59 -0700 (PDT) Received: from [127.0.0.1] (localhost [127.0.0.1]) (Authenticated sender: fdanis) with ESMTPSA id 4F4E61F4596C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1654267258; bh=T/r0QPR6UcSHxHQ4G1IiVFBw1Kug/SbySFkXgtQd8/0=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=TiHtv5KWxUo+21QFEqS6u9Qq2F/Df/thakiBNM3pvglCdvLJnRVryRIKibiw4T0KL FC/dgaFhgBfwxP2YJrwKYF/pgdaKBK2v2Djx3i7kQplEOvVNMjaY/iKC/zsWO7+09A DWxp5GG6tymFIbBjpJNulZTquVYzVKdlXNbpuvUejlP8cb3/qxks/3ZI5ARWsv+UeI bkTooEqpGZUa8Sbx/jFerGtOHQ+lKSms3u6cnHdRgaVjOGR5jrN+zX4/y+gAFHTaiO PsWWgfDSSxvqWubTA0uRBFtn2F496KuPXo8u6RQTZq8ZtEeDsyGuiizMOyQbXNAreR 0LwoO99HZw6NA== Message-ID: Date: Fri, 3 Jun 2022 16:40:55 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.9.1 Subject: Re: [PATCH] btproxy: Allow to select multiple BT controllers Content-Language: en-US To: Luiz Augusto von Dentz Cc: "linux-bluetooth@vger.kernel.org" References: <20220602152613.79718-1-frederic.danis@collabora.com> From: =?UTF-8?Q?Fr=c3=a9d=c3=a9ric_Danis?= In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.2 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,NICE_REPLY_A,RDNS_NONE,SPF_HELO_NONE, T_SCC_BODY_TEXT_LINE,UNPARSEABLE_RELAY autolearn=unavailable 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 Hi Luiz, On 02/06/2022 22:45, Luiz Augusto von Dentz wrote: > Hi Frédéric, > > On Thu, Jun 2, 2022 at 9:08 AM Frédéric Danis > wrote: >> 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 allows to select the controllers to be used by btproxy. > Does it keep the old behavior and adds the possibility to enter -i > command line option multiple times? Yes, it keeps old behavior when used without -i option, in this case it will try to use the first controller available. The only change from the old behavior is that when started with one or more -i option it will not check if a client is already active, so no "Active client already present" error message. >> --- >> tools/btproxy.c | 48 ++++++++++++++++++++++++++++++++++-------------- >> 1 file changed, 34 insertions(+), 14 deletions(-) >> >> diff --git a/tools/btproxy.c b/tools/btproxy.c >> index 94ca1c7fd..c09a504ff 100644 >> --- a/tools/btproxy.c >> +++ b/tools/btproxy.c >> @@ -48,6 +48,7 @@ struct sockaddr_hci { >> }; >> #define HCI_CHANNEL_USER 1 >> #define HCI_INDEX_NONE 0xffff >> +#define HCI_INDEX_MAX 15 >> >> static uint16_t hci_index = HCI_INDEX_NONE; >> static bool client_active = false; >> @@ -533,13 +534,28 @@ static bool setup_proxy(int host_fd, bool host_shutdown, >> return true; >> } >> >> -static int open_channel(uint16_t index) >> +static int get_next_hci_index(int index) >> +{ >> + while (++index <= HCI_INDEX_MAX) { >> + if (hci_index & (1 << index)) >> + return index; >> + } >> + >> + return -1; >> +} > Perhaps use util_get_uid? Yes, this should simplify it >> + >> +static int open_channel(int index) >> { >> struct sockaddr_hci addr; >> int fd, err; >> >> - if (index == HCI_INDEX_NONE) >> - index = 0; >> + if (index == HCI_INDEX_NONE) { >> + index = get_next_hci_index(-1); >> + if (index < 0) { >> + perror("No controller available"); >> + return -1; >> + } >> + } >> >> printf("Opening user channel for hci%u\n", index); >> >> @@ -561,9 +577,10 @@ static int open_channel(uint16_t index) >> /* Open next available controller if no specific index was >> * provided and the error indicates that the controller. >> */ >> - if (hci_index == HCI_INDEX_NONE && >> + index = get_next_hci_index(index); >> + if (index >=0 && >> (err == -EBUSY || err == -EUSERS)) >> - return open_channel(++index); >> + return open_channel(index); >> >> perror("Failed to bind Bluetooth socket"); >> return -1; >> @@ -601,13 +618,7 @@ 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); >> + dev_fd = open_channel(HCI_INDEX_NONE); >> if (dev_fd < 0) { >> close(host_fd); >> return; >> @@ -800,6 +811,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 +856,15 @@ int main(int argc, char *argv[]) >> usage(); >> return EXIT_FAILURE; >> } >> - hci_index = atoi(str); >> + index = atoi(str); >> + if (index > HCI_INDEX_MAX) { >> + fprintf(stderr, "Invalid controller index\n"); >> + usage(); >> + return EXIT_FAILURE; >> + } >> + if (hci_index == HCI_INDEX_NONE) >> + hci_index = 0; >> + hci_index |= 1 << index; >> break; >> case 'a': >> type = HCI_AMP; >> @@ -892,7 +912,7 @@ int main(int argc, char *argv[]) >> if (use_redirect) { >> printf("Creating local redirect\n"); >> >> - dev_fd = open_channel(hci_index); >> + dev_fd = open_channel(HCI_INDEX_NONE); >> } else { >> printf("Connecting to %s:%u\n", connect_address, >> tcp_port); >> -- >> 2.25.1 >> > -- Frédéric Danis Senior Software Engineer Collabora Ltd. Platinum Building, St John's Innovation Park, Cambridge CB4 0DS, United Kingdom Registered in England & Wales, no. 5513718