[PATCH 1/4] shared/btsnoop: Add btsnoop_create_fd

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This patch adds btsnoop_create_fd function which allows to create
btsnoop on existing file descriptor instead of creating new file like
btsnoop_create does.
---
 src/shared/btsnoop.c | 47 ++++++++++++++++++++++++++++++++++++-----------
 src/shared/btsnoop.h |  1 +
 2 files changed, 37 insertions(+), 11 deletions(-)

diff --git a/src/shared/btsnoop.c b/src/shared/btsnoop.c
index 17a872c..da993c5 100644
--- a/src/shared/btsnoop.c
+++ b/src/shared/btsnoop.c
@@ -128,12 +128,45 @@ failed:
 	return NULL;
 }
 
-struct btsnoop *btsnoop_create(const char *path, uint32_t type)
+static bool btsnoop_init(struct btsnoop *btsnoop, uint32_t type)
 {
-	struct btsnoop *btsnoop;
 	struct btsnoop_hdr hdr;
 	ssize_t written;
 
+	btsnoop->type = type;
+	btsnoop->index = 0xffff;
+
+	memcpy(hdr.id, btsnoop_id, sizeof(btsnoop_id));
+	hdr.version = htobe32(btsnoop_version);
+	hdr.type = htobe32(btsnoop->type);
+
+	written = write(btsnoop->fd, &hdr, BTSNOOP_HDR_SIZE);
+
+	return written == BTSNOOP_HDR_SIZE;
+}
+
+struct btsnoop *btsnoop_create_fd(int fd, uint32_t type)
+{
+	struct btsnoop *btsnoop;
+
+	btsnoop = calloc(1, sizeof(*btsnoop));
+	if (!btsnoop)
+		return NULL;
+
+	btsnoop->fd = fd;
+
+	if (!btsnoop_init(btsnoop, type)) {
+		free(btsnoop);
+		return NULL;
+	}
+
+	return btsnoop_ref(btsnoop);
+}
+
+struct btsnoop *btsnoop_create(const char *path, uint32_t type)
+{
+	struct btsnoop *btsnoop;
+
 	btsnoop = calloc(1, sizeof(*btsnoop));
 	if (!btsnoop)
 		return NULL;
@@ -145,15 +178,7 @@ struct btsnoop *btsnoop_create(const char *path, uint32_t type)
 		return NULL;
 	}
 
-	btsnoop->type = type;
-	btsnoop->index = 0xffff;
-
-	memcpy(hdr.id, btsnoop_id, sizeof(btsnoop_id));
-	hdr.version = htobe32(btsnoop_version);
-	hdr.type = htobe32(btsnoop->type);
-
-	written = write(btsnoop->fd, &hdr, BTSNOOP_HDR_SIZE);
-	if (written < 0) {
+	if (!btsnoop_init(btsnoop, type)) {
 		close(btsnoop->fd);
 		free(btsnoop);
 		return NULL;
diff --git a/src/shared/btsnoop.h b/src/shared/btsnoop.h
index 2c55d02..9e6b854 100644
--- a/src/shared/btsnoop.h
+++ b/src/shared/btsnoop.h
@@ -55,6 +55,7 @@ struct btsnoop;
 
 struct btsnoop *btsnoop_open(const char *path, unsigned long flags);
 struct btsnoop *btsnoop_create(const char *path, uint32_t type);
+struct btsnoop *btsnoop_create_fd(int fd, uint32_t type);
 
 struct btsnoop *btsnoop_ref(struct btsnoop *btsnoop);
 void btsnoop_unref(struct btsnoop *btsnoop);
-- 
1.9.3

--
To unsubscribe from this list: send the line "unsubscribe linux-bluetooth" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Bluez Devel]     [Linux Wireless Networking]     [Linux Wireless Personal Area Networking]     [Linux ATH6KL]     [Linux USB Devel]     [Linux Media Drivers]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [Big List of Linux Books]

  Powered by Linux