• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  *  BlueZ - Bluetooth protocol stack for Linux
4  *
5  *  Copyright (C) 2000-2002  Maxim Krasnyansky <maxk@qualcomm.com>
6  *  Copyright (C) 2003-2007  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 #ifndef __PARSER_H
26 #define __PARSER_H
27 
28 #include <time.h>
29 #include <sys/time.h>
30 #include <bluetooth/bluetooth.h>
31 #include <netinet/in.h>
32 
33 struct frame {
34 	void		*data;
35 	uint32_t	data_len;
36 	void		*ptr;
37 	uint32_t	len;
38 	uint16_t	dev_id;
39 	uint8_t		in;
40 	uint8_t		master;
41 	uint16_t	handle;
42 	uint16_t	cid;
43 	uint16_t	num;
44 	uint8_t		dlci;
45 	uint8_t		channel;
46 	unsigned long	flags;
47 	struct timeval	ts;
48 	int		pppdump_fd;
49 	int		audio_fd;
50 };
51 
52 /* Parser flags */
53 #define DUMP_WIDTH	20
54 
55 #define DUMP_ASCII	0x0001
56 #define DUMP_HEX	0x0002
57 #define DUMP_EXT	0x0004
58 #define DUMP_RAW	0x0008
59 #define DUMP_BPA	0x0010
60 #define DUMP_TSTAMP	0x0100
61 #define DUMP_VERBOSE	0x0200
62 #define DUMP_BTSNOOP	0x1000
63 #define DUMP_PKTLOG	0x2000
64 #define DUMP_NOVENDOR	0x4000
65 #define DUMP_TYPE_MASK	(DUMP_ASCII | DUMP_HEX | DUMP_EXT)
66 
67 /* Parser filter */
68 #define FILT_LMP	0x0001
69 #define FILT_HCI	0x0002
70 #define FILT_SCO	0x0004
71 #define FILT_L2CAP	0x0008
72 #define FILT_RFCOMM	0x0010
73 #define FILT_SDP	0x0020
74 #define FILT_BNEP	0x0040
75 #define FILT_CMTP	0x0080
76 #define FILT_HIDP	0x0100
77 #define FILT_HCRP	0x0200
78 #define FILT_AVDTP	0x0400
79 #define FILT_AVCTP	0x0800
80 
81 #define FILT_OBEX	0x00010000
82 #define FILT_CAPI	0x00020000
83 #define FILT_PPP	0x00040000
84 #define FILT_ERICSSON	0x10000000
85 #define FILT_CSR	0x1000000a
86 #define FILT_DGA	0x1000000c
87 
88 #define STRUCT_OFFSET(type, member)  ((uint8_t *)&(((type *)NULL)->member) - \
89                                      (uint8_t *)((type *)NULL))
90 
91 #define STRUCT_END(type, member)     (STRUCT_OFFSET(type, member) + \
92                                      sizeof(((type *)NULL)->member))
93 
94 #define DEFAULT_COMPID	65535
95 
96 struct parser_t {
97 	unsigned long flags;
98 	unsigned long filter;
99 	unsigned short defpsm;
100 	unsigned short defcompid;
101 	int state;
102 	int pppdump_fd;
103 	int audio_fd;
104 };
105 
106 extern struct parser_t parser;
107 
108 void init_parser(unsigned long flags, unsigned long filter,
109 		unsigned short defpsm, unsigned short defcompid,
110 		int pppdump_fd, int audio_fd);
111 
p_filter(unsigned long f)112 static inline int p_filter(unsigned long f)
113 {
114 	return !(parser.filter & f);
115 }
116 
p_indent(int level,struct frame * f)117 static inline void p_indent(int level, struct frame *f)
118 {
119 	if (level < 0) {
120 		parser.state = 0;
121 		return;
122 	}
123 
124 	if (!parser.state) {
125 		if (parser.flags & DUMP_TSTAMP) {
126 			if (parser.flags & DUMP_VERBOSE) {
127 				struct tm tm;
128 				time_t t = f->ts.tv_sec;
129 				localtime_r(&t, &tm);
130 				printf("%04d-%02d-%02d %02d:%02d:%02d.%06lu ",
131 					tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
132 					tm.tm_hour, tm.tm_min, tm.tm_sec, f->ts.tv_usec);
133 			} else
134 				printf("%8lu.%06lu ", f->ts.tv_sec, f->ts.tv_usec);
135 		}
136 		printf("%c ", (f->in ? '>' : '<'));
137 		parser.state = 1;
138 	} else
139 		printf("  ");
140 
141 	if (level)
142 		printf("%*c", (level*2), ' ');
143 }
144 
p_ba2str(const bdaddr_t * ba,char * str)145 static inline void p_ba2str(const bdaddr_t *ba, char *str)
146 {
147 	if (parser.flags & DUMP_NOVENDOR) {
148 		uint8_t b[6];
149 
150 		baswap((bdaddr_t *) b, ba);
151 		sprintf(str, "%2.2X:%2.2X:%2.2X:*:*:*", b[0], b[1], b[2]);
152 	} else
153 		ba2str(ba, str);
154 }
155 
156 /* get_uXX functions do byte swaping */
157 
get_u8(struct frame * frm)158 static inline uint8_t get_u8(struct frame *frm)
159 {
160 	uint8_t *u8_ptr = frm->ptr;
161 	frm->ptr += 1;
162 	frm->len -= 1;
163 	return *u8_ptr;
164 }
165 
get_u16(struct frame * frm)166 static inline uint16_t get_u16(struct frame *frm)
167 {
168 	uint16_t *u16_ptr = frm->ptr;
169 	frm->ptr += 2;
170 	frm->len -= 2;
171 	return ntohs(bt_get_unaligned(u16_ptr));
172 }
173 
get_u32(struct frame * frm)174 static inline uint32_t get_u32(struct frame *frm)
175 {
176 	uint32_t *u32_ptr = frm->ptr;
177 	frm->ptr += 4;
178 	frm->len -= 4;
179 	return ntohl(bt_get_unaligned(u32_ptr));
180 }
181 
get_u64(struct frame * frm)182 static inline uint64_t get_u64(struct frame *frm)
183 {
184 	uint64_t *u64_ptr = frm->ptr;
185 	uint64_t u64 = bt_get_unaligned(u64_ptr), tmp;
186 	frm->ptr += 8;
187 	frm->len -= 8;
188 	tmp = ntohl(u64 & 0xffffffff);
189 	u64 = (tmp << 32) | ntohl(u64 >> 32);
190 	return u64;
191 }
192 
get_u128(struct frame * frm,uint64_t * l,uint64_t * h)193 static inline void get_u128(struct frame *frm, uint64_t *l, uint64_t *h)
194 {
195 	*h = get_u64(frm);
196 	*l = get_u64(frm);
197 }
198 
199 char *get_uuid_name(int uuid);
200 
201 void set_proto(uint16_t handle, uint16_t psm, uint8_t channel, uint32_t proto);
202 uint32_t get_proto(uint16_t handle, uint16_t psm, uint8_t channel);
203 
204 struct frame *add_frame(struct frame *frm);
205 void del_frame(uint16_t handle, uint8_t dlci);
206 
207 uint8_t get_opcode(uint16_t handle, uint8_t dlci);
208 void set_opcode(uint16_t handle, uint8_t dlci, uint8_t opcode);
209 
210 uint8_t get_status(uint16_t handle, uint8_t dlci);
211 void set_status(uint16_t handle, uint8_t dlci, uint8_t status);
212 
213 void l2cap_clear(uint16_t handle);
214 
215 void ascii_dump(int level, struct frame *frm, int num);
216 void hex_dump(int level, struct frame *frm, int num);
217 void ext_dump(int level, struct frame *frm, int num);
218 void raw_dump(int level, struct frame *frm);
219 void raw_ndump(int level, struct frame *frm, int num);
220 
221 void lmp_dump(int level, struct frame *frm);
222 void hci_dump(int level, struct frame *frm);
223 void l2cap_dump(int level, struct frame *frm);
224 void rfcomm_dump(int level, struct frame *frm);
225 void sdp_dump(int level, struct frame *frm);
226 void bnep_dump(int level, struct frame *frm);
227 void cmtp_dump(int level, struct frame *frm);
228 void hidp_dump(int level, struct frame *frm);
229 void hcrp_dump(int level, struct frame *frm);
230 void avdtp_dump(int level, struct frame *frm);
231 void avctp_dump(int level, struct frame *frm);
232 
233 void obex_dump(int level, struct frame *frm);
234 void capi_dump(int level, struct frame *frm);
235 void ppp_dump(int level, struct frame *frm);
236 void arp_dump(int level, struct frame *frm);
237 void ip_dump(int level, struct frame *frm);
238 void ericsson_dump(int level, struct frame *frm);
239 void csr_dump(int level, struct frame *frm);
240 void bpa_dump(int level, struct frame *frm);
241 
parse(struct frame * frm)242 static inline void parse(struct frame *frm)
243 {
244 	p_indent(-1, NULL);
245 	if (parser.flags & DUMP_RAW)
246 		raw_dump(0, frm);
247 	else
248 		hci_dump(0, frm);
249 	fflush(stdout);
250 }
251 
252 #endif /* __PARSER_H */
253