Skip to contentSkip to conversation

shared/log: Add common code to interface with logging channel

PATCH Applied Luiz Augusto von Dentz 1 participant 5 messages Nov 20, 2018 – Nov 21, 2018

Subsystems (2)
lore
Share & tools
lore

Luiz Augusto von Dentz

author maintainer
#0 lore raw

PATCH v2 1/4 shared/log: Add common code to interface with logging channel

From: Luiz Augusto von Dentz <[email protected]>

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
Makefile.am +1 -0
index 0ccf393c6..3f613a617 100644
@@ -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
src/shared/log.c new file +191 -0

191 lines not on this page · raw message

src/shared/log.h new file +31 -0
new file mode 100644
index 000000000..c72ab735b
@@ -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);
Signature · 1 line
2.17.2
author maintainer
#1 lore raw

PATCH v2 2/4 shared/tester: Make use of shared log

From: Luiz Augusto von Dentz <[email protected]>

Use bt_monitor_* to send messages to the logging channel.

---
 src/shared/tester.c | 112 ++++----------------------------------------
 1 file changed, 9 insertions(+), 103 deletions(-)
src/shared/tester.c +9 -103
index 05c81a66d..168d0de91 100644
@@ -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;
 }
Signature · 1 line
2.17.2
author maintainer
#2 lore raw

PATCH v2 3/4 shared/shell: Add option to print to monitor

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

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(-)
src/shared/shell.c +27 -3
index 241046440..89a2e82bc 100644
@@ -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;
Signature · 1 line
2.17.2
author maintainer
#3 lore raw

PATCH v2 4/4 log: Use shared log code

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

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(-)
Makefile.tools +4 -2
index f81fd0a4c..231a4b09b 100644
@@ -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
android/Android.mk +1 -0
index 76a826b47..8f842e775 100644
@@ -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 \
android/Makefile.am +1 -0
index 154f8db56..d665d1e14 100644
@@ -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@
src/log.c +13 -117

210 lines not on this page · raw message

Signature · 1 line
2.17.2
author maintainer Applied
#4 lore raw
In reply to Luiz Augusto von Dentz

Hi,

Quoted 260 lines · Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

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
Makefile.am +1 -0
index 0ccf393c6..3f613a617 100644
@@ -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
src/shared/log.c new file +191 -0
new file mode 100644
index 000000000..d42bae713
@@ -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;
+}
src/shared/log.h new file +31 -1
new file mode 100644
index 000000000..c72ab735b
@@ -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);
--
Quoted 1 line

2.17.2

Applied.

Signature · 1 line
Luiz Augusto von Dentz