3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2011-2012 Intel Corporation
6 * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
36 #include <arpa/inet.h>
40 static inline uint64_t ntoh64(uint64_t n)
43 uint64_t tmp = ntohl(n & 0x00000000ffffffff);
51 #define hton64(x) ntoh64(x)
54 uint8_t id[8]; /* Identification Pattern */
55 uint32_t version; /* Version Number = 1 */
56 uint32_t type; /* Datalink Type */
57 } __attribute__ ((packed));
58 #define BTSNOOP_HDR_SIZE (sizeof(struct btsnoop_hdr))
61 uint32_t size; /* Original Length */
62 uint32_t len; /* Included Length */
63 uint32_t flags; /* Packet Flags */
64 uint32_t drops; /* Cumulative Drops */
65 uint64_t ts; /* Timestamp microseconds */
66 uint8_t data[0]; /* Packet Data */
67 } __attribute__ ((packed));
68 #define BTSNOOP_PKT_SIZE (sizeof(struct btsnoop_pkt))
70 static const uint8_t btsnoop_id[] = { 0x62, 0x74, 0x73, 0x6e,
71 0x6f, 0x6f, 0x70, 0x00 };
73 static const uint32_t btsnoop_version = 1;
74 static const uint32_t btsnoop_type = 1001;
76 static int btsnoop_fd = -1;
77 static uint16_t btsnoop_index = 0xffff;
79 void btsnoop_open(const char *path)
84 btsnoop_fd = open(path, O_WRONLY | O_CREAT | O_TRUNC,
85 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
88 void btsnoop_write(struct timeval *tv, uint16_t index, uint32_t flags,
89 const void *data, uint16_t size)
91 struct btsnoop_hdr hdr;
92 struct btsnoop_pkt pkt;
102 if (btsnoop_index == 0xffff) {
103 memcpy(hdr.id, btsnoop_id, sizeof(btsnoop_id));
104 hdr.version = htonl(btsnoop_version);
105 hdr.type = htonl(btsnoop_type);
107 written = write(btsnoop_fd, &hdr, BTSNOOP_HDR_SIZE);
111 btsnoop_index = index;
114 if (index != btsnoop_index)
117 ts = (tv->tv_sec - 946684800ll) * 1000000ll + tv->tv_usec;
119 pkt.size = htonl(size);
120 pkt.len = htonl(size);
121 pkt.flags = htonl(flags);
122 pkt.drops = htonl(0);
123 pkt.ts = hton64(ts + 0x00E03AB44A676000ll);
125 written = write(btsnoop_fd, &pkt, BTSNOOP_PKT_SIZE);
129 if (data && size > 0) {
130 written = write(btsnoop_fd, data, size);
136 void btsnoop_close(void)
144 btsnoop_index = 0xffff;