diff mbox series

[BlueZ,3/4] emulator: add option to listen on TCP

Message ID 20250609143811.566331-4-d3dx12.xx@gmail.com
State New
Headers show
Series btvirt improvements for LE Audio | expand

Commit Message

Dmitrii Sharshakov June 9, 2025, 2:38 p.m. UTC
---
 emulator/main.c   | 20 ++++++++++++++++++--
 emulator/server.c |  8 ++++----
 emulator/server.h |  2 +-
 3 files changed, 23 insertions(+), 7 deletions(-)
diff mbox series

Patch

diff --git a/emulator/main.c b/emulator/main.c
index 928588254..0d2f1e34f 100644
--- a/emulator/main.c
+++ b/emulator/main.c
@@ -47,6 +47,7 @@  static void usage(void)
 		"\t-d                    Enable debug\n"
 		"\t-S                    Create local serial port\n"
 		"\t-s                    Create local server sockets\n"
+		"\t-t[port=45550]        Create a TCP server\n"
 		"\t-l[num]               Number of local controllers\n"
 		"\t-L                    Create LE only controller\n"
 		"\t-U[num]               Number of test LE controllers\n"
@@ -60,6 +61,7 @@  static const struct option main_options[] = {
 	{ "debug",   no_argument,       NULL, 'd' },
 	{ "serial",  no_argument,       NULL, 'S' },
 	{ "server",  no_argument,       NULL, 's' },
+	{ "tcp",     optional_argument, NULL, 't' },
 	{ "local",   optional_argument, NULL, 'l' },
 	{ "le",      no_argument,       NULL, 'L' },
 	{ "bredr",   no_argument,       NULL, 'B' },
@@ -86,6 +88,7 @@  int main(int argc, char *argv[])
 	struct server *server5;
 	bool debug_enabled = false;
 	bool server_enabled = false;
+	uint16_t tcp_port = 0;
 	bool serial_enabled = false;
 	int letest_count = 0;
 	int vhci_count = 0;
@@ -97,7 +100,7 @@  int main(int argc, char *argv[])
 	for (;;) {
 		int opt;
 
-		opt = getopt_long(argc, argv, "dSsl::LBAU::T::vh",
+		opt = getopt_long(argc, argv, "dSst::l::LBAU::T::vh",
 						main_options, NULL);
 		if (opt < 0)
 			break;
@@ -112,6 +115,12 @@  int main(int argc, char *argv[])
 		case 's':
 			server_enabled = true;
 			break;
+		case 't':
+			if (optarg)
+				tcp_port = atoi(optarg);
+			else
+				tcp_port = 45550;
+			break;
 		case 'l':
 			if (optarg)
 				vhci_count = atoi(optarg);
@@ -145,7 +154,7 @@  int main(int argc, char *argv[])
 	}
 
 	if (letest_count < 1 && vhci_count < 1 && !server_enabled &&
-						!serial_enabled) {
+						!tcp_port && !serial_enabled) {
 		fprintf(stderr, "No emulator specified\n");
 		return EXIT_FAILURE;
 	}
@@ -213,5 +222,12 @@  int main(int argc, char *argv[])
 			fprintf(stderr, "Failed to open monitor server\n");
 	}
 
+	if (tcp_port) {
+		struct server *tcp_server = server_open_tcp(SERVER_TYPE_BREDRLE, tcp_port);
+		if (!tcp_server)
+			fprintf(stderr, "Failed to open TCP port\n");
+		fprintf(stderr, "Listening TCP on 127.0.0.1:%d\n", tcp_port);
+	}
+
 	return mainloop_run_with_signal(signal_callback, NULL);
 }
diff --git a/emulator/server.c b/emulator/server.c
index ceb417a40..0ca7d42a3 100644
--- a/emulator/server.c
+++ b/emulator/server.c
@@ -311,7 +311,7 @@  struct server *server_open_unix(enum server_type type, const char *path)
 	return server;
 }
 
-static int open_tcp(void)
+static int open_tcp(uint16_t port)
 {
 	struct sockaddr_in addr;
 	int fd, opt = 1;
@@ -332,7 +332,7 @@  static int open_tcp(void)
 	addr.sin_family = AF_INET;
 	addr.sin_addr.s_addr = INADDR_ANY;
 	addr.sin_addr.s_addr = inet_addr("127.0.0.1");
-	addr.sin_port = htons(45550);
+	addr.sin_port = htons(port);
 
 	if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
 		perror("Failed to bind server socket");
@@ -349,7 +349,7 @@  static int open_tcp(void)
 	return fd;
 }
 
-struct server *server_open_tcp(enum server_type type)
+struct server *server_open_tcp(enum server_type type, uint16_t port)
 {
 	struct server *server;
 
@@ -361,7 +361,7 @@  struct server *server_open_tcp(enum server_type type)
 	server->type = type;
 	server->id = 0x43;
 
-	server->fd = open_tcp();
+	server->fd = open_tcp(port);
 	if (server->fd < 0) {
 		free(server);
 		return NULL;
diff --git a/emulator/server.h b/emulator/server.h
index 294e86525..7d6b7be74 100644
--- a/emulator/server.h
+++ b/emulator/server.h
@@ -22,5 +22,5 @@  enum server_type {
 struct server;
 
 struct server *server_open_unix(enum server_type type, const char *path);
-struct server *server_open_tcp(enum server_type type);
+struct server *server_open_tcp(enum server_type type, uint16_t port);
 void server_close(struct server *server);