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

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

This enables any code using shared to log information using the logging
channel which can then be decoded by the likes of btmon.
---
 Makefile.am      |   1 +
 src/shared/log.c | 191 +++++++++++++++++++++++++++++++++++++++++++++++
 src/shared/log.h |  31 ++++++++
 3 files changed, 223 insertions(+)
 create mode 100644 src/shared/log.c
 create mode 100644 src/shared/log.h

diff --git a/Makefile.am b/Makefile.am
index 0ccf393c6..3f613a617 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/log.h src/shared/log.c \
 			src/shared/tty.h
 
 if READLINE
diff --git a/src/shared/log.c b/src/shared/log.c
new file mode 100644
index 000000000..d42bae713
--- /dev/null
+++ b/src/shared/log.c
@@ -0,0 +1,191 @@
+/*
+ *
+ *  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/log.h"
+
+struct log_hdr {
+	uint16_t opcode;
+	uint16_t index;
+	uint16_t len;
+	uint8_t  priority;
+	uint8_t  ident_len;
+} __attribute__((packed));
+
+struct log_l2cap_hdr {
+	uint16_t cid;
+	uint16_t psm;
+} __attribute__((packed));
+
+static int log_fd = -1;
+
+int bt_log_sendmsg(uint16_t index, const char *label, int level,
+					struct iovec *io, size_t io_len)
+{
+	struct log_hdr hdr;
+	struct msghdr msg;
+	struct iovec iov[5];
+	size_t i;
+	int err;
+
+	if (io_len > 3)
+		return -EMSGSIZE;
+
+	log_fd = bt_log_open();
+	if (log_fd < 0)
+		return log_fd;
+
+	hdr.opcode = cpu_to_le16(0x0000);
+	hdr.index = cpu_to_le16(index);
+	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(log_fd, &msg, 0);
+	if (err < 0) {
+		err = -errno;
+		close(log_fd);
+		log_fd = -1;
+	}
+
+	return err;
+}
+
+int bt_log_open(void)
+{
+	struct sockaddr_hci addr;
+	int fd;
+	static int err;
+
+	if (err < 0)
+		return err;
+
+	if (log_fd >= 0)
+		return log_fd;
+
+	fd = socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
+	if (fd < 0) {
+		err = -errno;
+		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;
+	}
+
+	log_fd = fd;
+
+	return fd;
+}
+
+int bt_log_vprintf(uint16_t index, const char *label, int level,
+					const char *format, va_list ap)
+{
+	struct iovec iov;
+	char *str;
+	int len;
+
+	len = vasprintf(&str, format, ap);
+	if (len < 0)
+		return errno;
+
+	len = strlen(str);
+
+	/* Replace new line since btmon already adds it */
+	if (len > 1 && str[len - 1] == '\n') {
+		str[len - 1] = '\0';
+		len--;
+	}
+
+	iov.iov_base = str;
+	iov.iov_len = len + 1;
+
+	len = bt_log_sendmsg(index, label, level, &iov, 1);
+
+	free(str);
+
+	return len;
+}
+
+int bt_log_printf(uint16_t index, const char *label, int level,
+						const char *format, ...)
+{
+	va_list ap;
+	int err;
+
+	va_start(ap, format);
+	err = bt_log_vprintf(index, label, level, format, ap);
+	va_end(ap);
+
+	return err;
+}
+
+void bt_log_close(void)
+{
+	if (log_fd < 0)
+		return;
+
+	close(log_fd);
+	log_fd = -1;
+}
diff --git a/src/shared/log.h b/src/shared/log.h
new file mode 100644
index 000000000..c72ab735b
--- /dev/null
+++ b/src/shared/log.h
@@ -0,0 +1,31 @@
+/*
+ *
+ *  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_log_open(void);
+int bt_log_sendmsg(uint16_t index, const char *label, int level,
+					struct iovec *io, size_t io_len);
+int bt_log_vprintf(uint16_t index, const char *label, int level,
+					const char *format, va_list ap);
+int bt_log_printf(uint16_t index, const char *label, int level,
+					const char *format, ...);
+void bt_log_close(void);
-- 
2.17.2


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

* [PATCH v2 2/4] shared/tester: Make use of shared log
  2018-11-20 13:58 [PATCH v2 1/4] shared/log: Add common code to interface with logging channel Luiz Augusto von Dentz
@ 2018-11-20 13:58 ` Luiz Augusto von Dentz
  2018-11-20 13:58 ` [PATCH v2 3/4] shared/shell: Add option to print to monitor Luiz Augusto von Dentz
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Luiz Augusto von Dentz @ 2018-11-20 13:58 UTC (permalink / raw)
  To: linux-bluetooth

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

Use bt_monitor_* to send messages to the logging channel.
---
 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..168d0de91 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/log.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_log_vprintf(HCI_DEV_NONE, 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_log_vprintf(HCI_DEV_NONE, 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_log_vprintf(HCI_DEV_NONE, 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_log_vprintf(HCI_DEV_NONE, 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_log_sendmsg(HCI_DEV_NONE, 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_log_close();
+
 	return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
 }
-- 
2.17.2


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

* [PATCH v2 3/4] shared/shell: Add option to print to monitor
  2018-11-20 13:58 [PATCH v2 1/4] shared/log: Add common code to interface with logging channel Luiz Augusto von Dentz
  2018-11-20 13:58 ` [PATCH v2 2/4] shared/tester: Make use of shared log Luiz Augusto von Dentz
@ 2018-11-20 13:58 ` Luiz Augusto von Dentz
  2018-11-20 13:58 ` [PATCH v2 4/4] log: Use shared log code Luiz Augusto von Dentz
  2018-11-21  9:16 ` [PATCH v2 1/4] shared/log: Add common code to interface with logging channel Luiz Augusto von Dentz
  3 siblings, 0 replies; 5+ messages in thread
From: Luiz Augusto von Dentz @ 2018-11-20 13:58 UTC (permalink / raw)
  To: linux-bluetooth

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

This adds option -m/--monitor which send output to btmon using
libshared bt_log:

= bluetoothctl: power on
= bluetoothctl: Changing power on succeeded
---
 src/shared/shell.c | 30 +++++++++++++++++++++++++++---
 1 file changed, 27 insertions(+), 3 deletions(-)

diff --git a/src/shared/shell.c b/src/shared/shell.c
index 241046440..89a2e82bc 100644
--- a/src/shared/shell.c
+++ b/src/shared/shell.c
@@ -27,6 +27,7 @@
 
 #include <stdio.h>
 #include <errno.h>
+#include <syslog.h>
 #include <unistd.h>
 #include <stdlib.h>
 #include <stdarg.h>
@@ -45,6 +46,7 @@
 #include "src/shared/util.h"
 #include "src/shared/queue.h"
 #include "src/shared/shell.h"
+#include "src/shared/log.h"
 
 #define CMD_LENGTH	48
 #define print_text(color, fmt, args...) \
@@ -70,6 +72,7 @@ static struct {
 	int argc;
 	char **argv;
 	bool mode;
+	bool monitor;
 	int timeout;
 	struct io *input;
 
@@ -521,6 +524,12 @@ void bt_shell_printf(const char *fmt, ...)
 	vprintf(fmt, args);
 	va_end(args);
 
+	if (data.monitor) {
+		va_start(args, fmt);
+		bt_log_vprintf(0xffff, data.name, LOG_INFO, fmt, args);
+		va_end(args);
+	}
+
 	if (save_input) {
 		if (!data.saved_prompt)
 			rl_restore_prompt();
@@ -613,6 +622,9 @@ static void rl_handler(char *input)
 	if (history_search(input, -1))
 		add_history(input);
 
+	if (data.monitor)
+		bt_log_printf(0xffff, data.name, LOG_INFO, "%s", input);
+
 	if (wordexp(input, &w, WRDE_NOCMD))
 		goto done;
 
@@ -988,6 +1000,7 @@ static const struct option main_options[] = {
 	{ "version",	no_argument, 0, 'v' },
 	{ "help",	no_argument, 0, 'h' },
 	{ "timeout",	required_argument, 0, 't' },
+	{ "monitor",	no_argument, 0, 'm' },
 };
 
 static void usage(int argc, char **argv, const struct bt_shell_opt *opt)
@@ -1003,7 +1016,8 @@ static void usage(int argc, char **argv, const struct bt_shell_opt *opt)
 	for (i = 0; opt && opt->options[i].name; i++)
 		printf("\t--%s \t%s\n", opt->options[i].name, opt->help[i]);
 
-	printf("\t--timeout \tTimeout in seconds for non-interactive mode\n"
+	printf("\t--monitor \tEnable monitor output\n"
+		"\t--timeout \tTimeout in seconds for non-interactive mode\n"
 		"\t--version \tDisplay version\n"
 		"\t--help \t\tDisplay help\n");
 }
@@ -1022,9 +1036,9 @@ void bt_shell_init(int argc, char **argv, const struct bt_shell_opt *opt)
 	if (opt) {
 		memcpy(options + offset, opt->options,
 				sizeof(struct option) * opt->optno);
-		snprintf(optstr, sizeof(optstr), "+hvt:%s", opt->optstr);
+		snprintf(optstr, sizeof(optstr), "+mhvt:%s", opt->optstr);
 	} else
-		snprintf(optstr, sizeof(optstr), "+hvt:");
+		snprintf(optstr, sizeof(optstr), "+mhvt:");
 
 	data.name = strrchr(argv[0], '/');
 	if (!data.name)
@@ -1047,6 +1061,13 @@ void bt_shell_init(int argc, char **argv, const struct bt_shell_opt *opt)
 		case 't':
 			data.timeout = atoi(optarg);
 			break;
+		case 'm':
+			data.monitor = true;
+			if (bt_log_open() < 0) {
+				data.monitor = false;
+				printf("Unable to open logging channel\n");
+			}
+			break;
 		default:
 			if (index < 0) {
 				for (index = 0; options[index].val; index++) {
@@ -1131,6 +1152,9 @@ void bt_shell_cleanup(void)
 		data.envs = NULL;
 	}
 
+	if (data.monitor)
+		bt_log_close();
+
 	rl_cleanup();
 
 	data.init = false;
-- 
2.17.2


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

* [PATCH v2 4/4] log: Use shared log code
  2018-11-20 13:58 [PATCH v2 1/4] shared/log: Add common code to interface with logging channel Luiz Augusto von Dentz
  2018-11-20 13:58 ` [PATCH v2 2/4] shared/tester: Make use of shared log Luiz Augusto von Dentz
  2018-11-20 13:58 ` [PATCH v2 3/4] shared/shell: Add option to print to monitor Luiz Augusto von Dentz
@ 2018-11-20 13:58 ` Luiz Augusto von Dentz
  2018-11-21  9:16 ` [PATCH v2 1/4] shared/log: Add common code to interface with logging channel Luiz Augusto von Dentz
  3 siblings, 0 replies; 5+ messages in thread
From: Luiz Augusto von Dentz @ 2018-11-20 13:58 UTC (permalink / raw)
  To: linux-bluetooth

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

Use bt_log_* to send messages to the logging channel.
---
 Makefile.tools      |   6 +-
 android/Android.mk  |   1 +
 android/Makefile.am |   1 +
 src/log.c           | 130 +++++---------------------------------------
 4 files changed, 19 insertions(+), 119 deletions(-)

diff --git a/Makefile.tools b/Makefile.tools
index f81fd0a4c..231a4b09b 100644
--- a/Makefile.tools
+++ b/Makefile.tools
@@ -223,13 +223,15 @@ tools_mcaptest_SOURCES = tools/mcaptest.c \
 				btio/btio.h btio/btio.c \
 				src/log.c src/log.h \
 				profiles/health/mcap.h profiles/health/mcap.c
-tools_mcaptest_LDADD = lib/libbluetooth-internal.la @GLIB_LIBS@ -lrt
+tools_mcaptest_LDADD = lib/libbluetooth-internal.la @GLIB_LIBS@ \
+				src/libshared-mainloop.la -lrt
 
 tools_bneptest_SOURCES = tools/bneptest.c \
 				btio/btio.h btio/btio.c \
 				src/log.h src/log.c \
 				profiles/network/bnep.h profiles/network/bnep.c
-tools_bneptest_LDADD = lib/libbluetooth-internal.la @GLIB_LIBS@
+tools_bneptest_LDADD = lib/libbluetooth-internal.la @GLIB_LIBS@ \
+				src/libshared-mainloop.la
 
 tools_cltest_SOURCES = tools/cltest.c
 tools_cltest_LDADD = lib/libbluetooth-internal.la src/libshared-mainloop.la
diff --git a/android/Android.mk b/android/Android.mk
index 76a826b47..8f842e775 100644
--- a/android/Android.mk
+++ b/android/Android.mk
@@ -228,6 +228,7 @@ include $(BUILD_EXECUTABLE)
 include $(CLEAR_VARS)
 
 LOCAL_SRC_FILES := \
+	bluez/src/shared/log.c \
 	bluez/src/log.c \
 	bluez/btio/btio.c \
 	bluez/lib/bluetooth.c \
diff --git a/android/Makefile.am b/android/Makefile.am
index 154f8db56..d665d1e14 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/log.h src/shared/log.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..ea639cbd6 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/log.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)
-{
-	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,
+static void monitor_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_log_vprintf(index, LOG_IDENT, priority, 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_log_open();
 
 	if (!detach)
 		option |= LOG_PERROR;
@@ -325,7 +221,7 @@ void __btd_log_cleanup(void)
 {
 	closelog();
 
-	logging_close();
+	bt_log_close();
 
 	g_strfreev(enabled);
 }
-- 
2.17.2


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

* Re: [PATCH v2 1/4] shared/log: Add common code to interface with logging channel
  2018-11-20 13:58 [PATCH v2 1/4] shared/log: Add common code to interface with logging channel Luiz Augusto von Dentz
                   ` (2 preceding siblings ...)
  2018-11-20 13:58 ` [PATCH v2 4/4] log: Use shared log code Luiz Augusto von Dentz
@ 2018-11-21  9:16 ` Luiz Augusto von Dentz
  3 siblings, 0 replies; 5+ messages in thread
From: Luiz Augusto von Dentz @ 2018-11-21  9:16 UTC (permalink / raw)
  To: linux-bluetooth

Hi,
On Tue, Nov 20, 2018 at 3:58 PM Luiz Augusto von Dentz
<luiz.dentz@gmail.com> wrote:
>
> From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
>
> This enables any code using shared to log information using the logging
> channel which can then be decoded by the likes of btmon.
> ---
>  Makefile.am      |   1 +
>  src/shared/log.c | 191 +++++++++++++++++++++++++++++++++++++++++++++++
>  src/shared/log.h |  31 ++++++++
>  3 files changed, 223 insertions(+)
>  create mode 100644 src/shared/log.c
>  create mode 100644 src/shared/log.h
>
> diff --git a/Makefile.am b/Makefile.am
> index 0ccf393c6..3f613a617 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/log.h src/shared/log.c \
>                         src/shared/tty.h
>
>  if READLINE
> diff --git a/src/shared/log.c b/src/shared/log.c
> new file mode 100644
> index 000000000..d42bae713
> --- /dev/null
> +++ b/src/shared/log.c
> @@ -0,0 +1,191 @@
> +/*
> + *
> + *  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/log.h"
> +
> +struct log_hdr {
> +       uint16_t opcode;
> +       uint16_t index;
> +       uint16_t len;
> +       uint8_t  priority;
> +       uint8_t  ident_len;
> +} __attribute__((packed));
> +
> +struct log_l2cap_hdr {
> +       uint16_t cid;
> +       uint16_t psm;
> +} __attribute__((packed));
> +
> +static int log_fd = -1;
> +
> +int bt_log_sendmsg(uint16_t index, const char *label, int level,
> +                                       struct iovec *io, size_t io_len)
> +{
> +       struct log_hdr hdr;
> +       struct msghdr msg;
> +       struct iovec iov[5];
> +       size_t i;
> +       int err;
> +
> +       if (io_len > 3)
> +               return -EMSGSIZE;
> +
> +       log_fd = bt_log_open();
> +       if (log_fd < 0)
> +               return log_fd;
> +
> +       hdr.opcode = cpu_to_le16(0x0000);
> +       hdr.index = cpu_to_le16(index);
> +       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(log_fd, &msg, 0);
> +       if (err < 0) {
> +               err = -errno;
> +               close(log_fd);
> +               log_fd = -1;
> +       }
> +
> +       return err;
> +}
> +
> +int bt_log_open(void)
> +{
> +       struct sockaddr_hci addr;
> +       int fd;
> +       static int err;
> +
> +       if (err < 0)
> +               return err;
> +
> +       if (log_fd >= 0)
> +               return log_fd;
> +
> +       fd = socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
> +       if (fd < 0) {
> +               err = -errno;
> +               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;
> +       }
> +
> +       log_fd = fd;
> +
> +       return fd;
> +}
> +
> +int bt_log_vprintf(uint16_t index, const char *label, int level,
> +                                       const char *format, va_list ap)
> +{
> +       struct iovec iov;
> +       char *str;
> +       int len;
> +
> +       len = vasprintf(&str, format, ap);
> +       if (len < 0)
> +               return errno;
> +
> +       len = strlen(str);
> +
> +       /* Replace new line since btmon already adds it */
> +       if (len > 1 && str[len - 1] == '\n') {
> +               str[len - 1] = '\0';
> +               len--;
> +       }
> +
> +       iov.iov_base = str;
> +       iov.iov_len = len + 1;
> +
> +       len = bt_log_sendmsg(index, label, level, &iov, 1);
> +
> +       free(str);
> +
> +       return len;
> +}
> +
> +int bt_log_printf(uint16_t index, const char *label, int level,
> +                                               const char *format, ...)
> +{
> +       va_list ap;
> +       int err;
> +
> +       va_start(ap, format);
> +       err = bt_log_vprintf(index, label, level, format, ap);
> +       va_end(ap);
> +
> +       return err;
> +}
> +
> +void bt_log_close(void)
> +{
> +       if (log_fd < 0)
> +               return;
> +
> +       close(log_fd);
> +       log_fd = -1;
> +}
> diff --git a/src/shared/log.h b/src/shared/log.h
> new file mode 100644
> index 000000000..c72ab735b
> --- /dev/null
> +++ b/src/shared/log.h
> @@ -0,0 +1,31 @@
> +/*
> + *
> + *  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_log_open(void);
> +int bt_log_sendmsg(uint16_t index, const char *label, int level,
> +                                       struct iovec *io, size_t io_len);
> +int bt_log_vprintf(uint16_t index, const char *label, int level,
> +                                       const char *format, va_list ap);
> +int bt_log_printf(uint16_t index, const char *label, int level,
> +                                       const char *format, ...);
> +void bt_log_close(void);
> --
> 2.17.2

Applied.

-- 
Luiz Augusto von Dentz

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

end of thread, other threads:[~2018-11-21  9:16 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-11-20 13:58 [PATCH v2 1/4] shared/log: Add common code to interface with logging channel Luiz Augusto von Dentz
2018-11-20 13:58 ` [PATCH v2 2/4] shared/tester: Make use of shared log Luiz Augusto von Dentz
2018-11-20 13:58 ` [PATCH v2 3/4] shared/shell: Add option to print to monitor Luiz Augusto von Dentz
2018-11-20 13:58 ` [PATCH v2 4/4] log: Use shared log code Luiz Augusto von Dentz
2018-11-21  9:16 ` [PATCH v2 1/4] shared/log: Add common code to interface with logging channel Luiz Augusto von Dentz

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