linux-bluetooth.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH BlueZ 1/3] shared/monitor: Add common code to interface with monitor
@ 2018-11-07 16:14 Luiz Augusto von Dentz
  2018-11-07 16:14 ` [PATCH BlueZ 2/3] shared/tester: Make use of shared monitor Luiz Augusto von Dentz
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Luiz Augusto von Dentz @ 2018-11-07 16:14 UTC (permalink / raw)
  To: linux-bluetooth

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

This enables any code using shared to log into monitor.
---
 Makefile.am          |   1 +
 src/shared/monitor.c | 177 +++++++++++++++++++++++++++++++++++++++++++
 src/shared/monitor.h |  30 ++++++++
 3 files changed, 208 insertions(+)
 create mode 100644 src/shared/monitor.c
 create mode 100644 src/shared/monitor.h

diff --git a/Makefile.am b/Makefile.am
index 0ccf393c6..2243248aa 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -119,6 +119,7 @@ shared_sources = src/shared/io.h src/shared/timeout.h \
 			src/shared/gatt-server.h src/shared/gatt-server.c \
 			src/shared/gatt-db.h src/shared/gatt-db.c \
 			src/shared/gap.h src/shared/gap.c \
+			src/shared/monitor.h src/shared/monitor.c \
 			src/shared/tty.h
 
 if READLINE
diff --git a/src/shared/monitor.c b/src/shared/monitor.c
new file mode 100644
index 000000000..78766ed1a
--- /dev/null
+++ b/src/shared/monitor.c
@@ -0,0 +1,177 @@
+/*
+ *
+ *  BlueZ - Bluetooth protocol stack for Linux
+ *
+ *  Copyright (C) 2018  Intel Corporation. All rights reserved.
+ *
+ *
+ *  This library is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU Lesser General Public
+ *  License as published by the Free Software Foundation; either
+ *  version 2.1 of the License, or (at your option) any later version.
+ *
+ *  This library is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  Lesser General Public License for more details.
+ *
+ *  You should have received a copy of the GNU Lesser General Public
+ *  License along with this library; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <errno.h>
+#include <inttypes.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+#include <signal.h>
+#include <sys/socket.h>
+
+#include "lib/bluetooth.h"
+#include "lib/hci.h"
+
+#include "src/shared/util.h"
+#include "src/shared/monitor.h"
+
+struct monitor_hdr {
+	uint16_t opcode;
+	uint16_t index;
+	uint16_t len;
+	uint8_t  priority;
+	uint8_t  ident_len;
+} __attribute__((packed));
+
+struct monitor_l2cap_hdr {
+	uint16_t cid;
+	uint16_t psm;
+} __attribute__((packed));
+
+static int monitor_fd = -1;
+
+int bt_monitor_sendmsg(const char *label, int level, struct iovec *io,
+							size_t io_len)
+{
+	struct monitor_hdr hdr;
+	struct msghdr msg;
+	struct iovec iov[5];
+	size_t i;
+	int err;
+
+	if (io_len > 3)
+		return -EMSGSIZE;
+
+	monitor_fd = bt_monitor_open();
+	if (monitor_fd < 0)
+		return monitor_fd;
+
+	hdr.opcode = cpu_to_le16(0x0000);
+	hdr.index = cpu_to_le16(0xffff);
+	hdr.ident_len = strlen(label) + 1;
+	hdr.len = cpu_to_le16(2 + hdr.ident_len);
+	hdr.priority = level;
+
+	iov[0].iov_base = &hdr;
+	iov[0].iov_len = sizeof(hdr);
+
+	iov[1].iov_base = (void *) label;
+	iov[1].iov_len = hdr.ident_len;
+
+	memset(&msg, 0, sizeof(msg));
+	msg.msg_iov = iov;
+	msg.msg_iovlen = 2;
+
+	for (i = 0; i < io_len; i++) {
+		iov[i + 2] = io[i];
+		hdr.len += io[i].iov_len;
+		msg.msg_iovlen++;
+	}
+
+	err = sendmsg(monitor_fd, &msg, 0);
+	if (err < 0) {
+		err = -errno;
+		close(monitor_fd);
+		monitor_fd = -1;
+	}
+
+	return err;
+}
+
+int bt_monitor_open(void)
+{
+	struct sockaddr_hci addr;
+	int fd;
+	int err;
+
+	if (monitor_fd >= 0)
+		return monitor_fd;
+
+	fd = socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
+	if (fd < 0)
+		return -errno;
+
+	memset(&addr, 0, sizeof(addr));
+	addr.hci_family = AF_BLUETOOTH;
+	addr.hci_dev = HCI_DEV_NONE;
+	addr.hci_channel = HCI_CHANNEL_LOGGING;
+
+	err = bind(fd, (struct sockaddr *) &addr, sizeof(addr));
+	if (err < 0 ){
+		err = -errno;
+		close(fd);
+		return err;
+	}
+
+	monitor_fd = fd;
+
+	return fd;
+}
+
+int bt_monitor_vprintf(const char *label, int level, const char *format,
+								va_list ap)
+{
+	struct iovec iov;
+	char *str;
+	int err;
+
+	err = vasprintf(&str, format, ap);
+	if (err < 0)
+		return errno;
+
+	iov.iov_base = str;
+	iov.iov_len = strlen(str) + 1;
+
+	err = bt_monitor_sendmsg(label, level, &iov, 1);
+
+	free(str);
+
+	return err;
+}
+
+int bt_monitor_printf(const char *label, int level, const char *format, ...)
+{
+	va_list ap;
+	int err;
+
+	va_start(ap, format);
+	err = bt_monitor_vprintf(label, level, format, ap);
+	va_end(ap);
+
+	return err;
+}
+
+void bt_monitor_close(void)
+{
+	if (monitor_fd < 0)
+		return;
+
+	close(monitor_fd);
+	monitor_fd = -1;
+}
diff --git a/src/shared/monitor.h b/src/shared/monitor.h
new file mode 100644
index 000000000..0aa538e36
--- /dev/null
+++ b/src/shared/monitor.h
@@ -0,0 +1,30 @@
+/*
+ *
+ *  BlueZ - Bluetooth protocol stack for Linux
+ *
+ *  Copyright (C) 2018  Intel Corporation. All rights reserved.
+ *
+ *
+ *  This library is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU Lesser General Public
+ *  License as published by the Free Software Foundation; either
+ *  version 2.1 of the License, or (at your option) any later version.
+ *
+ *  This library is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  Lesser General Public License for more details.
+ *
+ *  You should have received a copy of the GNU Lesser General Public
+ *  License along with this library; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+int bt_monitor_open(void);
+int bt_monitor_sendmsg(const char *label, int level, struct iovec *io,
+							size_t io_len);
+int bt_monitor_vprintf(const char *label, int level, const char *format,
+								va_list ap);
+int bt_monitor_printf(const char *label, int level, const char *format, ...);
+void bt_monitor_close(void);
-- 
2.17.2


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH BlueZ 2/3] shared/tester: Make use of shared monitor
  2018-11-07 16:14 [PATCH BlueZ 1/3] shared/monitor: Add common code to interface with monitor Luiz Augusto von Dentz
@ 2018-11-07 16:14 ` Luiz Augusto von Dentz
  2018-11-07 16:14 ` [PATCH BlueZ 3/3] log: Use shared monitor code Luiz Augusto von Dentz
  2018-11-07 18:24 ` [PATCH BlueZ 1/3] shared/monitor: Add common code to interface with monitor Marcel Holtmann
  2 siblings, 0 replies; 4+ messages in thread
From: Luiz Augusto von Dentz @ 2018-11-07 16:14 UTC (permalink / raw)
  To: linux-bluetooth

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

Use bt_monitor_* to send messages to the monitor.
---
 src/shared/tester.c | 112 ++++----------------------------------------
 1 file changed, 9 insertions(+), 103 deletions(-)

diff --git a/src/shared/tester.c b/src/shared/tester.c
index 05c81a66d..3dac2a59d 100644
--- a/src/shared/tester.c
+++ b/src/shared/tester.c
@@ -46,6 +46,7 @@
 
 #include "src/shared/util.h"
 #include "src/shared/tester.h"
+#include "src/shared/monitor.h"
 
 #define COLOR_OFF	"\x1B[0m"
 #define COLOR_BLACK	"\x1B[0;30m"
@@ -131,8 +132,6 @@ struct monitor_l2cap_hdr {
 	uint16_t psm;
 } __attribute__((packed));
 
-static int monitor_fd = -1;
-
 static void test_destroy(gpointer data)
 {
 	struct test_case *test = data;
@@ -150,102 +149,6 @@ static void test_destroy(gpointer data)
 	free(test);
 }
 
-static int monitor_open(void)
-{
-	struct sockaddr_hci addr;
-	int fd;
-
-	if (!option_monitor)
-		return -1;
-
-	if (monitor_fd >= 0)
-		return monitor_fd;
-
-	fd = socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
-	if (fd < 0)
-		return fd;
-
-	memset(&addr, 0, sizeof(addr));
-	addr.hci_family = AF_BLUETOOTH;
-	addr.hci_dev = HCI_DEV_NONE;
-	addr.hci_channel = HCI_CHANNEL_LOGGING;
-
-	if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
-		option_monitor = FALSE;
-		tester_debug("Failed to open monitor socket: %s",
-			     strerror(errno));
-		close(fd);
-		return -1;
-	}
-
-	monitor_fd = fd;
-
-	return fd;
-}
-
-static void monitor_sendmsg(const char *label, int level, struct iovec *io,
-							size_t io_len)
-{
-	struct monitor_hdr hdr;
-	struct msghdr msg;
-	struct iovec iov[5];
-	size_t i;
-
-	monitor_fd = monitor_open();
-	if (monitor_fd < 0 || io_len > 3)
-		return;
-
-	hdr.opcode = cpu_to_le16(0x0000);
-	hdr.index = cpu_to_le16(0xffff);
-	hdr.ident_len = strlen(label) + 1;
-	hdr.len = cpu_to_le16(2 + hdr.ident_len);
-	hdr.priority = level;
-
-	iov[0].iov_base = &hdr;
-	iov[0].iov_len = sizeof(hdr);
-
-	iov[1].iov_base = (void *) label;
-	iov[1].iov_len = hdr.ident_len;
-
-	memset(&msg, 0, sizeof(msg));
-	msg.msg_iov = iov;
-	msg.msg_iovlen = 2;
-
-	for (i = 0; i < io_len; i++) {
-		iov[i + 2] = io[i];
-		hdr.len += io[i].iov_len;
-		msg.msg_iovlen++;
-	}
-
-	if (sendmsg(monitor_fd, &msg, 0) < 0) {
-		/* Disable monitor */
-		option_monitor = FALSE;
-		tester_debug("Failed to send to monitor: %s", strerror(errno));
-		close(monitor_fd);
-		monitor_fd = -1;
-	}
-}
-
-static void monitor_vprintf(const char *id, int level, const char *format,
-								va_list ap)
-{
-	struct iovec iov;
-	char *str;
-
-	if (!option_monitor)
-		return;
-
-	if (vasprintf(&str, format, ap) < 0)
-		return;
-
-	iov.iov_base = str;
-	iov.iov_len = strlen(str) + 1;
-
-	monitor_sendmsg(id, level, &iov, 1);
-
-	free(str);
-}
-
 static void tester_vprintf(const char *format, va_list ap)
 {
 	if (tester_use_quiet())
@@ -266,7 +169,7 @@ static void tester_log(const char *format, ...)
 	va_end(ap);
 
 	va_start(ap, format);
-	monitor_vprintf(tester_name, LOG_INFO, format, ap);
+	bt_monitor_vprintf(tester_name, LOG_INFO, format, ap);
 	va_end(ap);
 }
 
@@ -279,7 +182,7 @@ void tester_print(const char *format, ...)
 	va_end(ap);
 
 	va_start(ap, format);
-	monitor_vprintf(tester_name, LOG_INFO, format, ap);
+	bt_monitor_vprintf(tester_name, LOG_INFO, format, ap);
 	va_end(ap);
 }
 
@@ -292,7 +195,7 @@ void tester_debug(const char *format, ...)
 	va_end(ap);
 
 	va_start(ap, format);
-	monitor_vprintf(tester_name, LOG_DEBUG, format, ap);
+	bt_monitor_vprintf(tester_name, LOG_DEBUG, format, ap);
 	va_end(ap);
 }
 
@@ -305,7 +208,7 @@ void tester_warn(const char *format, ...)
 	va_end(ap);
 
 	va_start(ap, format);
-	monitor_vprintf(tester_name, LOG_WARNING, format, ap);
+	bt_monitor_vprintf(tester_name, LOG_WARNING, format, ap);
 	va_end(ap);
 }
 
@@ -340,7 +243,7 @@ static void monitor_log(char dir, uint16_t cid, uint16_t psm, const void *data,
 	iov[2].iov_base = &term;
 	iov[2].iov_len = sizeof(term);
 
-	monitor_sendmsg(label, LOG_INFO, iov, 3);
+	bt_monitor_sendmsg(label, LOG_INFO, iov, 3);
 }
 
 void tester_monitor(char dir, uint16_t cid, uint16_t psm, const void *data,
@@ -1032,5 +935,8 @@ int tester_run(void)
 
 	g_list_free_full(test_list, test_destroy);
 
+	if (option_monitor)
+		bt_monitor_close();
+
 	return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
 }
-- 
2.17.2


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH BlueZ 3/3] log: Use shared monitor code
  2018-11-07 16:14 [PATCH BlueZ 1/3] shared/monitor: Add common code to interface with monitor Luiz Augusto von Dentz
  2018-11-07 16:14 ` [PATCH BlueZ 2/3] shared/tester: Make use of shared monitor Luiz Augusto von Dentz
@ 2018-11-07 16:14 ` Luiz Augusto von Dentz
  2018-11-07 18:24 ` [PATCH BlueZ 1/3] shared/monitor: Add common code to interface with monitor Marcel Holtmann
  2 siblings, 0 replies; 4+ messages in thread
From: Luiz Augusto von Dentz @ 2018-11-07 16:14 UTC (permalink / raw)
  To: linux-bluetooth

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

Use bt_monitor_* to send messages to the monitor.
---
 android/Makefile.am  |   1 +
 src/log.c            | 132 +++++--------------------------------------
 src/shared/monitor.c |  28 +++++----
 src/shared/monitor.h |  11 ++--
 src/shared/tester.c  |  10 ++--
 5 files changed, 43 insertions(+), 139 deletions(-)

diff --git a/android/Makefile.am b/android/Makefile.am
index 154f8db56..e9a6e0242 100644
--- a/android/Makefile.am
+++ b/android/Makefile.am
@@ -107,6 +107,7 @@ android_avdtptest_SOURCES = android/avdtptest.c \
 				btio/btio.h btio/btio.c \
 				src/shared/util.h src/shared/util.c \
 				src/shared/queue.h src/shared/queue.c \
+				src/shared/monitor.h src/shared/monitor.c \
 				android/avdtp.h android/avdtp.c
 android_avdtptest_CFLAGS = $(AM_CFLAGS)
 android_avdtptest_LDADD = lib/libbluetooth-internal.la @GLIB_LIBS@
diff --git a/src/log.c b/src/log.c
index d2a20de78..7dceeb617 100644
--- a/src/log.c
+++ b/src/log.c
@@ -41,95 +41,15 @@
 #include "lib/hci.h"
 
 #include "src/shared/util.h"
+#include "src/shared/monitor.h"
 #include "log.h"
 
 #define LOG_IDENT "bluetoothd"
-#define LOG_IDENT_LEN sizeof(LOG_IDENT)
 
-struct log_hdr {
-	uint16_t opcode;
-	uint16_t index;
-	uint16_t len;
-	uint8_t  priority;
-	uint8_t  ident_len;
-} __attribute__((packed));
-
-static int logging_fd = -1;
-
-static void logging_open(void)
+static void monitor_log(uint16_t index, int level, const char *format,
+							va_list ap)
 {
-	struct sockaddr_hci addr;
-	int fd;
-
-	if (logging_fd >= 0)
-		return;
-
-	fd = socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
-	if (fd < 0)
-		return;
-
-	memset(&addr, 0, sizeof(addr));
-	addr.hci_family = AF_BLUETOOTH;
-	addr.hci_dev = HCI_DEV_NONE;
-	addr.hci_channel = HCI_CHANNEL_LOGGING;
-
-	if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
-		close(fd);
-		return;
-	}
-
-	logging_fd = fd;
-}
-
-static void logging_close(void)
-{
-	if (logging_fd >= 0) {
-		close(logging_fd);
-		logging_fd = -1;
-	}
-}
-
-static void logging_log(uint16_t index, int priority,
-					const char *format, va_list ap)
-{
-	struct log_hdr hdr;
-	struct msghdr msg;
-	struct iovec iov[3];
-	uint16_t len;
-	char *str;
-
-	if (vasprintf(&str, format, ap) < 0)
-		return;
-
-	len = strlen(str) + 1;
-
-	hdr.opcode = cpu_to_le16(0x0000);
-	hdr.index = cpu_to_le16(index);
-	hdr.len = cpu_to_le16(2 + LOG_IDENT_LEN + len);
-	hdr.priority = priority;
-	hdr.ident_len = LOG_IDENT_LEN;
-
-	iov[0].iov_base = &hdr;
-	iov[0].iov_len = sizeof(hdr);
-
-	iov[1].iov_base = LOG_IDENT;
-	iov[1].iov_len = LOG_IDENT_LEN;
-
-	iov[2].iov_base = str;
-	iov[2].iov_len = len;
-
-	memset(&msg, 0, sizeof(msg));
-	msg.msg_iov = iov;
-	msg.msg_iovlen = 3;
-
-	if (sendmsg(logging_fd, &msg, 0) < 0) {
-		if (errno != ENODEV) {
-			close(logging_fd);
-			logging_fd = -1;
-		}
-	}
-
-	free(str);
+	bt_monitor_vprintf(index, LOG_IDENT, level, format, ap);
 }
 
 void error(const char *format, ...)
@@ -140,11 +60,8 @@ void error(const char *format, ...)
 	vsyslog(LOG_ERR, format, ap);
 	va_end(ap);
 
-	if (logging_fd < 0)
-		return;
-
 	va_start(ap, format);
-	logging_log(HCI_DEV_NONE, LOG_ERR, format, ap);
+	monitor_log(HCI_DEV_NONE, LOG_ERR, format, ap);
 	va_end(ap);
 }
 
@@ -156,11 +73,8 @@ void warn(const char *format, ...)
 	vsyslog(LOG_WARNING, format, ap);
 	va_end(ap);
 
-	if (logging_fd < 0)
-		return;
-
 	va_start(ap, format);
-	logging_log(HCI_DEV_NONE, LOG_WARNING, format, ap);
+	monitor_log(HCI_DEV_NONE, LOG_WARNING, format, ap);
 	va_end(ap);
 }
 
@@ -172,11 +86,8 @@ void info(const char *format, ...)
 	vsyslog(LOG_INFO, format, ap);
 	va_end(ap);
 
-	if (logging_fd < 0)
-		return;
-
 	va_start(ap, format);
-	logging_log(HCI_DEV_NONE, LOG_INFO, format, ap);
+	monitor_log(HCI_DEV_NONE, LOG_INFO, format, ap);
 	va_end(ap);
 }
 
@@ -188,11 +99,8 @@ void btd_log(uint16_t index, int priority, const char *format, ...)
 	vsyslog(priority, format, ap);
 	va_end(ap);
 
-	if (logging_fd < 0)
-		return;
-
 	va_start(ap, format);
-	logging_log(index, priority, format, ap);
+	monitor_log(index, priority, format, ap);
 	va_end(ap);
 }
 
@@ -204,11 +112,8 @@ void btd_error(uint16_t index, const char *format, ...)
 	vsyslog(LOG_ERR, format, ap);
 	va_end(ap);
 
-	if (logging_fd < 0)
-		return;
-
 	va_start(ap, format);
-	logging_log(index, LOG_ERR, format, ap);
+	monitor_log(index, LOG_ERR, format, ap);
 	va_end(ap);
 }
 
@@ -220,11 +125,8 @@ void btd_warn(uint16_t index, const char *format, ...)
 	vsyslog(LOG_WARNING, format, ap);
 	va_end(ap);
 
-	if (logging_fd < 0)
-		return;
-
 	va_start(ap, format);
-	logging_log(index, LOG_WARNING, format, ap);
+	monitor_log(index, LOG_WARNING, format, ap);
 	va_end(ap);
 }
 
@@ -236,11 +138,8 @@ void btd_info(uint16_t index, const char *format, ...)
 	vsyslog(LOG_INFO, format, ap);
 	va_end(ap);
 
-	if (logging_fd < 0)
-		return;
-
 	va_start(ap, format);
-	logging_log(index, LOG_INFO, format, ap);
+	monitor_log(index, LOG_INFO, format, ap);
 	va_end(ap);
 }
 
@@ -252,11 +151,8 @@ void btd_debug(uint16_t index, const char *format, ...)
 	vsyslog(LOG_DEBUG, format, ap);
 	va_end(ap);
 
-	if (logging_fd < 0)
-		return;
-
 	va_start(ap, format);
-	logging_log(index, LOG_DEBUG, format, ap);
+	monitor_log(index, LOG_DEBUG, format, ap);
 	va_end(ap);
 }
 
@@ -311,7 +207,7 @@ void __btd_log_init(const char *debug, int detach)
 
 	__btd_enable_debug(__start___debug, __stop___debug);
 
-	logging_open();
+	bt_monitor_open();
 
 	if (!detach)
 		option |= LOG_PERROR;
@@ -325,7 +221,7 @@ void __btd_log_cleanup(void)
 {
 	closelog();
 
-	logging_close();
+	bt_monitor_close();
 
 	g_strfreev(enabled);
 }
diff --git a/src/shared/monitor.c b/src/shared/monitor.c
index 78766ed1a..019147ab3 100644
--- a/src/shared/monitor.c
+++ b/src/shared/monitor.c
@@ -56,8 +56,8 @@ struct monitor_l2cap_hdr {
 
 static int monitor_fd = -1;
 
-int bt_monitor_sendmsg(const char *label, int level, struct iovec *io,
-							size_t io_len)
+int bt_monitor_sendmsg(uint16_t index, const char *label, int level,
+					struct iovec *io, size_t io_len)
 {
 	struct monitor_hdr hdr;
 	struct msghdr msg;
@@ -73,7 +73,7 @@ int bt_monitor_sendmsg(const char *label, int level, struct iovec *io,
 		return monitor_fd;
 
 	hdr.opcode = cpu_to_le16(0x0000);
-	hdr.index = cpu_to_le16(0xffff);
+	hdr.index = cpu_to_le16(index);
 	hdr.ident_len = strlen(label) + 1;
 	hdr.len = cpu_to_le16(2 + hdr.ident_len);
 	hdr.priority = level;
@@ -108,14 +108,19 @@ int bt_monitor_open(void)
 {
 	struct sockaddr_hci addr;
 	int fd;
-	int err;
+	static int err;
+
+	if (err < 0)
+		return err;
 
 	if (monitor_fd >= 0)
 		return monitor_fd;
 
 	fd = socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
-	if (fd < 0)
+	if (fd < 0) {
+		err = -errno;
 		return -errno;
+	}
 
 	memset(&addr, 0, sizeof(addr));
 	addr.hci_family = AF_BLUETOOTH;
@@ -123,7 +128,7 @@ int bt_monitor_open(void)
 	addr.hci_channel = HCI_CHANNEL_LOGGING;
 
 	err = bind(fd, (struct sockaddr *) &addr, sizeof(addr));
-	if (err < 0 ){
+	if (err < 0) {
 		err = -errno;
 		close(fd);
 		return err;
@@ -134,8 +139,8 @@ int bt_monitor_open(void)
 	return fd;
 }
 
-int bt_monitor_vprintf(const char *label, int level, const char *format,
-								va_list ap)
+int bt_monitor_vprintf(uint16_t index, const char *label, int level,
+					const char *format, va_list ap)
 {
 	struct iovec iov;
 	char *str;
@@ -148,20 +153,21 @@ int bt_monitor_vprintf(const char *label, int level, const char *format,
 	iov.iov_base = str;
 	iov.iov_len = strlen(str) + 1;
 
-	err = bt_monitor_sendmsg(label, level, &iov, 1);
+	err = bt_monitor_sendmsg(index, label, level, &iov, 1);
 
 	free(str);
 
 	return err;
 }
 
-int bt_monitor_printf(const char *label, int level, const char *format, ...)
+int bt_monitor_printf(uint16_t index, const char *label, int level,
+						const char *format, ...)
 {
 	va_list ap;
 	int err;
 
 	va_start(ap, format);
-	err = bt_monitor_vprintf(label, level, format, ap);
+	err = bt_monitor_vprintf(index, label, level, format, ap);
 	va_end(ap);
 
 	return err;
diff --git a/src/shared/monitor.h b/src/shared/monitor.h
index 0aa538e36..2a4cc768a 100644
--- a/src/shared/monitor.h
+++ b/src/shared/monitor.h
@@ -22,9 +22,10 @@
  */
 
 int bt_monitor_open(void);
-int bt_monitor_sendmsg(const char *label, int level, struct iovec *io,
-							size_t io_len);
-int bt_monitor_vprintf(const char *label, int level, const char *format,
-								va_list ap);
-int bt_monitor_printf(const char *label, int level, const char *format, ...);
+int bt_monitor_sendmsg(uint16_t index, const char *label, int level,
+					struct iovec *io, size_t io_len);
+int bt_monitor_vprintf(uint16_t index, const char *label, int level,
+					const char *format, va_list ap);
+int bt_monitor_printf(uint16_t index, const char *label, int level,
+					const char *format, ...);
 void bt_monitor_close(void);
diff --git a/src/shared/tester.c b/src/shared/tester.c
index 3dac2a59d..8e406cb1f 100644
--- a/src/shared/tester.c
+++ b/src/shared/tester.c
@@ -169,7 +169,7 @@ static void tester_log(const char *format, ...)
 	va_end(ap);
 
 	va_start(ap, format);
-	bt_monitor_vprintf(tester_name, LOG_INFO, format, ap);
+	bt_monitor_vprintf(HCI_DEV_NONE, tester_name, LOG_INFO, format, ap);
 	va_end(ap);
 }
 
@@ -182,7 +182,7 @@ void tester_print(const char *format, ...)
 	va_end(ap);
 
 	va_start(ap, format);
-	bt_monitor_vprintf(tester_name, LOG_INFO, format, ap);
+	bt_monitor_vprintf(HCI_DEV_NONE, tester_name, LOG_INFO, format, ap);
 	va_end(ap);
 }
 
@@ -195,7 +195,7 @@ void tester_debug(const char *format, ...)
 	va_end(ap);
 
 	va_start(ap, format);
-	bt_monitor_vprintf(tester_name, LOG_DEBUG, format, ap);
+	bt_monitor_vprintf(HCI_DEV_NONE, tester_name, LOG_DEBUG, format, ap);
 	va_end(ap);
 }
 
@@ -208,7 +208,7 @@ void tester_warn(const char *format, ...)
 	va_end(ap);
 
 	va_start(ap, format);
-	bt_monitor_vprintf(tester_name, LOG_WARNING, format, ap);
+	bt_monitor_vprintf(HCI_DEV_NONE, tester_name, LOG_WARNING, format, ap);
 	va_end(ap);
 }
 
@@ -243,7 +243,7 @@ static void monitor_log(char dir, uint16_t cid, uint16_t psm, const void *data,
 	iov[2].iov_base = &term;
 	iov[2].iov_len = sizeof(term);
 
-	bt_monitor_sendmsg(label, LOG_INFO, iov, 3);
+	bt_monitor_sendmsg(HCI_DEV_NONE, label, LOG_INFO, iov, 3);
 }
 
 void tester_monitor(char dir, uint16_t cid, uint16_t psm, const void *data,
-- 
2.17.2


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH BlueZ 1/3] shared/monitor: Add common code to interface with monitor
  2018-11-07 16:14 [PATCH BlueZ 1/3] shared/monitor: Add common code to interface with monitor Luiz Augusto von Dentz
  2018-11-07 16:14 ` [PATCH BlueZ 2/3] shared/tester: Make use of shared monitor Luiz Augusto von Dentz
  2018-11-07 16:14 ` [PATCH BlueZ 3/3] log: Use shared monitor code Luiz Augusto von Dentz
@ 2018-11-07 18:24 ` Marcel Holtmann
  2 siblings, 0 replies; 4+ messages in thread
From: Marcel Holtmann @ 2018-11-07 18:24 UTC (permalink / raw)
  To: Luiz Augusto von Dentz; +Cc: linux-bluetooth

Hi Luiz,

> This enables any code using shared to log into monitor.
> ---
> Makefile.am          |   1 +
> src/shared/monitor.c | 177 +++++++++++++++++++++++++++++++++++++++++++
> src/shared/monitor.h |  30 ++++++++
> 3 files changed, 208 insertions(+)
> create mode 100644 src/shared/monitor.c
> create mode 100644 src/shared/monitor.h
> 
> diff --git a/Makefile.am b/Makefile.am
> index 0ccf393c6..2243248aa 100644
> --- a/Makefile.am
> +++ b/Makefile.am
> @@ -119,6 +119,7 @@ shared_sources = src/shared/io.h src/shared/timeout.h \
> 			src/shared/gatt-server.h src/shared/gatt-server.c \
> 			src/shared/gatt-db.h src/shared/gatt-db.c \
> 			src/shared/gap.h src/shared/gap.c \
> +			src/shared/monitor.h src/shared/monitor.c \
> 			src/shared/tty.h
> 
> if READLINE
> diff --git a/src/shared/monitor.c b/src/shared/monitor.c
> new file mode 100644
> index 000000000..78766ed1a
> --- /dev/null
> +++ b/src/shared/monitor.c
> @@ -0,0 +1,177 @@
> +/*
> + *
> + *  BlueZ - Bluetooth protocol stack for Linux
> + *
> + *  Copyright (C) 2018  Intel Corporation. All rights reserved.
> + *
> + *
> + *  This library is free software; you can redistribute it and/or
> + *  modify it under the terms of the GNU Lesser General Public
> + *  License as published by the Free Software Foundation; either
> + *  version 2.1 of the License, or (at your option) any later version.
> + *
> + *  This library is distributed in the hope that it will be useful,
> + *  but WITHOUT ANY WARRANTY; without even the implied warranty of
> + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> + *  Lesser General Public License for more details.
> + *
> + *  You should have received a copy of the GNU Lesser General Public
> + *  License along with this library; if not, write to the Free Software
> + *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
> + *
> + */
> +
> +#ifdef HAVE_CONFIG_H
> +#include <config.h>
> +#endif
> +
> +#include <stdio.h>
> +#include <errno.h>
> +#include <inttypes.h>
> +#include <unistd.h>
> +#include <stdlib.h>
> +#include <stdarg.h>
> +#include <string.h>
> +#include <signal.h>
> +#include <sys/socket.h>
> +
> +#include "lib/bluetooth.h"
> +#include "lib/hci.h"
> +
> +#include "src/shared/util.h"
> +#include "src/shared/monitor.h"
> +
> +struct monitor_hdr {
> +	uint16_t opcode;
> +	uint16_t index;
> +	uint16_t len;
> +	uint8_t  priority;
> +	uint8_t  ident_len;
> +} __attribute__((packed));
> +
> +struct monitor_l2cap_hdr {
> +	uint16_t cid;
> +	uint16_t psm;
> +} __attribute__((packed));
> +
> +static int monitor_fd = -1;
> +
> +int bt_monitor_sendmsg(const char *label, int level, struct iovec *io,
> +							size_t io_len)
> +{
> +	struct monitor_hdr hdr;
> +	struct msghdr msg;
> +	struct iovec iov[5];
> +	size_t i;
> +	int err;
> +
> +	if (io_len > 3)
> +		return -EMSGSIZE;
> +
> +	monitor_fd = bt_monitor_open();
> +	if (monitor_fd < 0)
> +		return monitor_fd;
> +
> +	hdr.opcode = cpu_to_le16(0x0000);
> +	hdr.index = cpu_to_le16(0xffff);
> +	hdr.ident_len = strlen(label) + 1;
> +	hdr.len = cpu_to_le16(2 + hdr.ident_len);
> +	hdr.priority = level;
> +
> +	iov[0].iov_base = &hdr;
> +	iov[0].iov_len = sizeof(hdr);
> +
> +	iov[1].iov_base = (void *) label;
> +	iov[1].iov_len = hdr.ident_len;
> +
> +	memset(&msg, 0, sizeof(msg));
> +	msg.msg_iov = iov;
> +	msg.msg_iovlen = 2;
> +
> +	for (i = 0; i < io_len; i++) {
> +		iov[i + 2] = io[i];
> +		hdr.len += io[i].iov_len;
> +		msg.msg_iovlen++;
> +	}
> +
> +	err = sendmsg(monitor_fd, &msg, 0);
> +	if (err < 0) {
> +		err = -errno;
> +		close(monitor_fd);
> +		monitor_fd = -1;
> +	}
> +
> +	return err;
> +}
> +
> +int bt_monitor_open(void)
> +{
> +	struct sockaddr_hci addr;
> +	int fd;
> +	int err;
> +
> +	if (monitor_fd >= 0)
> +		return monitor_fd;
> +
> +	fd = socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
> +	if (fd < 0)
> +		return -errno;
> +
> +	memset(&addr, 0, sizeof(addr));
> +	addr.hci_family = AF_BLUETOOTH;
> +	addr.hci_dev = HCI_DEV_NONE;
> +	addr.hci_channel = HCI_CHANNEL_LOGGING;
> +
> +	err = bind(fd, (struct sockaddr *) &addr, sizeof(addr));
> +	if (err < 0 ){
> +		err = -errno;
> +		close(fd);
> +		return err;
> +	}
> +
> +	monitor_fd = fd;
> +
> +	return fd;
> +}
> +
> +int bt_monitor_vprintf(const char *label, int level, const char *format,
> +								va_list ap)
> +{
> +	struct iovec iov;
> +	char *str;
> +	int err;
> +
> +	err = vasprintf(&str, format, ap);
> +	if (err < 0)
> +		return errno;
> +
> +	iov.iov_base = str;
> +	iov.iov_len = strlen(str) + 1;
> +
> +	err = bt_monitor_sendmsg(label, level, &iov, 1);
> +
> +	free(str);
> +
> +	return err;
> +}
> +
> +int bt_monitor_printf(const char *label, int level, const char *format, ...)
> +{
> +	va_list ap;
> +	int err;
> +
> +	va_start(ap, format);
> +	err = bt_monitor_vprintf(label, level, format, ap);
> +	va_end(ap);
> +
> +	return err;
> +}
> +
> +void bt_monitor_close(void)
> +{
> +	if (monitor_fd < 0)
> +		return;
> +
> +	close(monitor_fd);
> +	monitor_fd = -1;
> +}
> diff --git a/src/shared/monitor.h b/src/shared/monitor.h
> new file mode 100644
> index 000000000..0aa538e36
> --- /dev/null
> +++ b/src/shared/monitor.h
> @@ -0,0 +1,30 @@
> +/*
> + *
> + *  BlueZ - Bluetooth protocol stack for Linux
> + *
> + *  Copyright (C) 2018  Intel Corporation. All rights reserved.
> + *
> + *
> + *  This library is free software; you can redistribute it and/or
> + *  modify it under the terms of the GNU Lesser General Public
> + *  License as published by the Free Software Foundation; either
> + *  version 2.1 of the License, or (at your option) any later version.
> + *
> + *  This library is distributed in the hope that it will be useful,
> + *  but WITHOUT ANY WARRANTY; without even the implied warranty of
> + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> + *  Lesser General Public License for more details.
> + *
> + *  You should have received a copy of the GNU Lesser General Public
> + *  License along with this library; if not, write to the Free Software
> + *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
> + *
> + */
> +
> +int bt_monitor_open(void);
> +int bt_monitor_sendmsg(const char *label, int level, struct iovec *io,
> +							size_t io_len);
> +int bt_monitor_vprintf(const char *label, int level, const char *format,
> +								va_list ap);
> +int bt_monitor_printf(const char *label, int level, const char *format, ...);
> +void bt_monitor_close(void);

so I do not like the naming here. This has nothing to do with the monitor actually. It is the logging into the kernel API. So better make this src/shared/log.[ch] and the bt_log_.

Essentially you want a generic logging that does the right thing with sending data to the logging interface and optionally also sending it to syslog / stdout.

Regards

Marcel


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2018-11-07 18:24 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-11-07 16:14 [PATCH BlueZ 1/3] shared/monitor: Add common code to interface with monitor Luiz Augusto von Dentz
2018-11-07 16:14 ` [PATCH BlueZ 2/3] shared/tester: Make use of shared monitor Luiz Augusto von Dentz
2018-11-07 16:14 ` [PATCH BlueZ 3/3] log: Use shared monitor code Luiz Augusto von Dentz
2018-11-07 18:24 ` [PATCH BlueZ 1/3] shared/monitor: Add common code to interface with monitor Marcel Holtmann

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).