linux-bluetooth.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Luiz Augusto von Dentz <luiz.dentz@gmail.com>
To: linux-bluetooth@vger.kernel.org
Subject: [PATCH BlueZ 3/7] vhci: Read the controller index
Date: Thu, 14 Oct 2021 22:09:25 -0700	[thread overview]
Message-ID: <20211015050929.3130100-3-luiz.dentz@gmail.com> (raw)
In-Reply-To: <20211015050929.3130100-1-luiz.dentz@gmail.com>

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

This makes vhci instance read its controller index assigned by the
kernel and also introduces vhci_get_btdev so it can be used by the
likes of hciemu.
---
 emulator/main.c | 12 ++++----
 emulator/vhci.c | 75 +++++++++++++++++++++++++++++--------------------
 emulator/vhci.h | 11 ++------
 3 files changed, 54 insertions(+), 44 deletions(-)

diff --git a/emulator/main.c b/emulator/main.c
index aa269c3f0..f64d46a5e 100644
--- a/emulator/main.c
+++ b/emulator/main.c
@@ -17,12 +17,14 @@
 #include <stdlib.h>
 #include <stdbool.h>
 #include <getopt.h>
+#include <sys/uio.h>
 
 #include "src/shared/mainloop.h"
 #include "src/shared/util.h"
 
 #include "serial.h"
 #include "server.h"
+#include "btdev.h"
 #include "vhci.h"
 #include "amp.h"
 #include "le.h"
@@ -90,7 +92,7 @@ int main(int argc, char *argv[])
 	int letest_count = 0;
 	int amptest_count = 0;
 	int vhci_count = 0;
-	enum vhci_type vhci_type = VHCI_TYPE_BREDRLE;
+	enum btdev_type type = BTDEV_TYPE_BREDRLE52;
 	int i;
 
 	mainloop_init();
@@ -120,13 +122,13 @@ int main(int argc, char *argv[])
 				vhci_count = 1;
 			break;
 		case 'L':
-			vhci_type = VHCI_TYPE_LE;
+			type = BTDEV_TYPE_LE;
 			break;
 		case 'B':
-			vhci_type = VHCI_TYPE_BREDR;
+			type = BTDEV_TYPE_BREDR;
 			break;
 		case 'A':
-			vhci_type = VHCI_TYPE_AMP;
+			type = BTDEV_TYPE_AMP;
 			break;
 		case 'U':
 			if (optarg)
@@ -182,7 +184,7 @@ int main(int argc, char *argv[])
 	for (i = 0; i < vhci_count; i++) {
 		struct vhci *vhci;
 
-		vhci = vhci_open(vhci_type);
+		vhci = vhci_open(type);
 		if (!vhci) {
 			fprintf(stderr, "Failed to open Virtual HCI device\n");
 			return EXIT_FAILURE;
diff --git a/emulator/vhci.c b/emulator/vhci.c
index 33f674d79..28cdef633 100644
--- a/emulator/vhci.c
+++ b/emulator/vhci.c
@@ -29,10 +29,8 @@
 #include "btdev.h"
 #include "vhci.h"
 
-#define uninitialized_var(x) x = x
-
 struct vhci {
-	enum vhci_type type;
+	enum btdev_type type;
 	int fd;
 	struct btdev *btdev;
 };
@@ -91,32 +89,22 @@ bool vhci_set_debug(struct vhci *vhci, vhci_debug_func_t callback,
 	return btdev_set_debug(vhci->btdev, callback, user_data, destroy);
 }
 
-struct vhci *vhci_open(enum vhci_type type)
+struct vhci_create_req {
+	uint8_t  pkt_type;
+	uint8_t  opcode;
+} __attribute__((packed));
+
+struct vhci_create_rsp {
+	uint8_t  pkt_type;
+	uint8_t  opcode;
+	uint16_t index;
+} __attribute__((packed));
+
+struct vhci *vhci_open(uint8_t type)
 {
 	struct vhci *vhci;
-	enum btdev_type uninitialized_var(btdev_type);
-	unsigned char uninitialized_var(ctrl_type);
-	unsigned char setup_cmd[2];
-	static uint8_t id = 0x23;
-
-	switch (type) {
-	case VHCI_TYPE_BREDRLE:
-		btdev_type = BTDEV_TYPE_BREDRLE52;
-		ctrl_type = HCI_PRIMARY;
-		break;
-	case VHCI_TYPE_BREDR:
-		btdev_type = BTDEV_TYPE_BREDR;
-		ctrl_type = HCI_PRIMARY;
-		break;
-	case VHCI_TYPE_LE:
-		btdev_type = BTDEV_TYPE_LE;
-		ctrl_type = HCI_PRIMARY;
-		break;
-	case VHCI_TYPE_AMP:
-		btdev_type = BTDEV_TYPE_AMP;
-		ctrl_type = HCI_AMP;
-		break;
-	}
+	struct vhci_create_req req;
+	struct vhci_create_rsp rsp;
 
 	vhci = malloc(sizeof(*vhci));
 	if (!vhci)
@@ -131,16 +119,33 @@ struct vhci *vhci_open(enum vhci_type type)
 		return NULL;
 	}
 
-	setup_cmd[0] = HCI_VENDOR_PKT;
-	setup_cmd[1] = ctrl_type;
+	memset(&req, 0, sizeof(req));
+	req.pkt_type = HCI_VENDOR_PKT;
 
-	if (write(vhci->fd, setup_cmd, sizeof(setup_cmd)) < 0) {
+	switch (type) {
+	case BTDEV_TYPE_AMP:
+		req.opcode = HCI_AMP;
+		break;
+	default:
+		req.opcode = HCI_PRIMARY;
+		break;
+	}
+
+	if (write(vhci->fd, &req, sizeof(req)) < 0) {
+		close(vhci->fd);
+		free(vhci);
+		return NULL;
+	}
+
+	memset(&rsp, 0, sizeof(rsp));
+
+	if (read(vhci->fd, &rsp, sizeof(rsp)) < 0) {
 		close(vhci->fd);
 		free(vhci);
 		return NULL;
 	}
 
-	vhci->btdev = btdev_create(btdev_type, id++);
+	vhci->btdev = btdev_create(type, rsp.index);
 	if (!vhci->btdev) {
 		close(vhci->fd);
 		free(vhci);
@@ -167,3 +172,11 @@ void vhci_close(struct vhci *vhci)
 
 	mainloop_remove_fd(vhci->fd);
 }
+
+struct btdev *vhci_get_btdev(struct vhci *vhci)
+{
+	if (!vhci)
+		return NULL;
+
+	return vhci->btdev;
+}
diff --git a/emulator/vhci.h b/emulator/vhci.h
index 7dfea25df..0554121e8 100644
--- a/emulator/vhci.h
+++ b/emulator/vhci.h
@@ -11,13 +11,6 @@
 
 #include <stdint.h>
 
-enum vhci_type {
-	VHCI_TYPE_BREDRLE,
-	VHCI_TYPE_BREDR,
-	VHCI_TYPE_LE,
-	VHCI_TYPE_AMP,
-};
-
 struct vhci;
 
 typedef void (*vhci_debug_func_t)(const char *str, void *user_data);
@@ -25,5 +18,7 @@ typedef void (*vhci_destroy_func_t)(void *user_data);
 bool vhci_set_debug(struct vhci *vhci, vhci_debug_func_t callback,
 			void *user_data, vhci_destroy_func_t destroy);
 
-struct vhci *vhci_open(enum vhci_type type);
+struct vhci *vhci_open(uint8_t type);
 void vhci_close(struct vhci *vhci);
+
+struct btdev *vhci_get_btdev(struct vhci *vhci);
-- 
2.31.1


  parent reply	other threads:[~2021-10-15  5:09 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-15  5:09 [PATCH BlueZ 1/7] monitor: Add packet definitions for MSFT extension Luiz Augusto von Dentz
2021-10-15  5:09 ` [PATCH BlueZ 2/7] monitor: Make use of MSFT packet definitions Luiz Augusto von Dentz
2021-10-15  5:09 ` Luiz Augusto von Dentz [this message]
2021-10-15  5:09 ` [PATCH BlueZ 4/7] vhci: Use io.h instead of mainloop.h Luiz Augusto von Dentz
2021-10-15  5:09 ` [PATCH BlueZ 5/7] hciemu: Use vhci_open to instanciate a vhci btdev Luiz Augusto von Dentz
2021-10-15  5:09 ` [PATCH BlueZ 6/7] vhci: Add functions to interface with debugfs Luiz Augusto von Dentz
2021-10-15  5:09 ` [PATCH BlueZ 7/7] mgmt-tester: Make use of vhci_set_force_suspend/vhci_set_force_wakeup Luiz Augusto von Dentz
2021-10-15  5:36 ` [BlueZ,1/7] monitor: Add packet definitions for MSFT extension bluez.test.bot
2021-10-15 20:10   ` Luiz Augusto von Dentz
     [not found] ` <CAGPPCLDFYFeiwfiyRX=6PquYYQ-Fp_LpN4Gw2745jyWzQKEBRQ@mail.gmail.com>
2021-10-18 16:24   ` [PATCH BlueZ 1/7] " Luiz Augusto von Dentz
2021-10-18 20:05     ` Luiz Augusto von Dentz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211015050929.3130100-3-luiz.dentz@gmail.com \
    --to=luiz.dentz@gmail.com \
    --cc=linux-bluetooth@vger.kernel.org \
    --subject='Re: [PATCH BlueZ 3/7] vhci: Read the controller index' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).