Tizen 2.0 Release
[framework/connectivity/bluez.git] / monitor / btsnoop.c
1 /*
2  *
3  *  BlueZ - Bluetooth protocol stack for Linux
4  *
5  *  Copyright (C) 2011-2012  Intel Corporation
6  *  Copyright (C) 2004-2010  Marcel Holtmann <marcel@holtmann.org>
7  *
8  *
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.
13  *
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.
18  *
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
22  *
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <stdio.h>
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include <stdint.h>
34 #include <string.h>
35 #include <sys/stat.h>
36 #include <arpa/inet.h>
37
38 #include "btsnoop.h"
39
40 static inline uint64_t ntoh64(uint64_t n)
41 {
42         uint64_t h;
43         uint64_t tmp = ntohl(n & 0x00000000ffffffff);
44
45         h = ntohl(n >> 32);
46         h |= tmp << 32;
47
48         return h;
49 }
50
51 #define hton64(x)     ntoh64(x)
52
53 struct btsnoop_hdr {
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))
59
60 struct btsnoop_pkt {
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))
69
70 static const uint8_t btsnoop_id[] = { 0x62, 0x74, 0x73, 0x6e,
71                                       0x6f, 0x6f, 0x70, 0x00 };
72
73 static const uint32_t btsnoop_version = 1;
74 static const uint32_t btsnoop_type = 1001;
75
76 static int btsnoop_fd = -1;
77 static uint16_t btsnoop_index = 0xffff;
78
79 void btsnoop_open(const char *path)
80 {
81         if (btsnoop_fd >= 0)
82                 return;
83
84         btsnoop_fd = open(path, O_WRONLY | O_CREAT | O_TRUNC,
85                                 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
86 }
87
88 void btsnoop_write(struct timeval *tv, uint16_t index, uint32_t flags,
89                                         const void *data, uint16_t size)
90 {
91         struct btsnoop_hdr hdr;
92         struct btsnoop_pkt pkt;
93         uint64_t ts;
94         ssize_t written;
95
96         if (!tv)
97                 return;
98
99         if (btsnoop_fd < 0)
100                 return;
101
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);
106
107                 written = write(btsnoop_fd, &hdr, BTSNOOP_HDR_SIZE);
108                 if (written < 0)
109                         return;
110
111                 btsnoop_index = index;
112         }
113
114         if (index != btsnoop_index)
115                 return;
116
117         ts = (tv->tv_sec - 946684800ll) * 1000000ll + tv->tv_usec;
118
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);
124
125         written = write(btsnoop_fd, &pkt, BTSNOOP_PKT_SIZE);
126         if (written < 0)
127                 return;
128
129         if (data && size > 0) {
130                 written = write(btsnoop_fd, data, size);
131                 if (written < 0)
132                         return;
133         }
134 }
135
136 void btsnoop_close(void)
137 {
138         if (btsnoop_fd < 0)
139                 return;
140
141         close(btsnoop_fd);
142         btsnoop_fd = -1;
143
144         btsnoop_index = 0xffff;
145 }