1 /*
2 *
3 * BlueZ - Bluetooth protocol stack for Linux
4 *
5 * Copyright (C) 2000-2002 Maxim Krasnyansky <maxk@qualcomm.com>
6 * Copyright (C) 2003-2011 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 #define FILT_ATT 0x1000
81
82 #define FILT_OBEX 0x00010000
83 #define FILT_CAPI 0x00020000
84 #define FILT_PPP 0x00040000
85 #define FILT_ERICSSON 0x10000000
86 #define FILT_CSR 0x1000000a
87 #define FILT_DGA 0x1000000c
88
89 #define STRUCT_OFFSET(type, member) ((uint8_t *)&(((type *)NULL)->member) - \
90 (uint8_t *)((type *)NULL))
91
92 #define STRUCT_END(type, member) (STRUCT_OFFSET(type, member) + \
93 sizeof(((type *)NULL)->member))
94
95 #define DEFAULT_COMPID 65535
96
97 struct parser_t {
98 unsigned long flags;
99 unsigned long filter;
100 unsigned short defpsm;
101 unsigned short defcompid;
102 int state;
103 int pppdump_fd;
104 int audio_fd;
105 };
106
107 extern struct parser_t parser;
108
109 void init_parser(unsigned long flags, unsigned long filter,
110 unsigned short defpsm, unsigned short defcompid,
111 int pppdump_fd, int audio_fd);
112
p_filter(unsigned long f)113 static inline int p_filter(unsigned long f)
114 {
115 return !(parser.filter & f);
116 }
117
p_indent(int level,struct frame * f)118 static inline void p_indent(int level, struct frame *f)
119 {
120 if (level < 0) {
121 parser.state = 0;
122 return;
123 }
124
125 if (!parser.state) {
126 if (parser.flags & DUMP_TSTAMP) {
127 if (parser.flags & DUMP_VERBOSE) {
128 struct tm tm;
129 time_t t = f->ts.tv_sec;
130 localtime_r(&t, &tm);
131 printf("%04d-%02d-%02d %02d:%02d:%02d.%06lu ",
132 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
133 tm.tm_hour, tm.tm_min, tm.tm_sec, f->ts.tv_usec);
134 } else
135 printf("%8lu.%06lu ", f->ts.tv_sec, f->ts.tv_usec);
136 }
137 printf("%c ", (f->in ? '>' : '<'));
138 parser.state = 1;
139 } else
140 printf(" ");
141
142 if (level)
143 printf("%*c", (level*2), ' ');
144 }
145
p_ba2str(const bdaddr_t * ba,char * str)146 static inline void p_ba2str(const bdaddr_t *ba, char *str)
147 {
148 if (parser.flags & DUMP_NOVENDOR) {
149 uint8_t b[6];
150
151 baswap((bdaddr_t *) b, ba);
152 sprintf(str, "%2.2X:%2.2X:%2.2X:*:*:*", b[0], b[1], b[2]);
153 } else
154 ba2str(ba, str);
155 }
156
157 /* get_uXX functions do byte swaping */
158
get_u8(struct frame * frm)159 static inline uint8_t get_u8(struct frame *frm)
160 {
161 uint8_t *u8_ptr = frm->ptr;
162 frm->ptr += 1;
163 frm->len -= 1;
164 return *u8_ptr;
165 }
166
get_u16(struct frame * frm)167 static inline uint16_t get_u16(struct frame *frm)
168 {
169 uint16_t *u16_ptr = frm->ptr;
170 frm->ptr += 2;
171 frm->len -= 2;
172 return ntohs(bt_get_unaligned(u16_ptr));
173 }
174
get_u32(struct frame * frm)175 static inline uint32_t get_u32(struct frame *frm)
176 {
177 uint32_t *u32_ptr = frm->ptr;
178 frm->ptr += 4;
179 frm->len -= 4;
180 return ntohl(bt_get_unaligned(u32_ptr));
181 }
182
get_u64(struct frame * frm)183 static inline uint64_t get_u64(struct frame *frm)
184 {
185 uint64_t *u64_ptr = frm->ptr;
186 uint64_t u64 = bt_get_unaligned(u64_ptr), tmp;
187 frm->ptr += 8;
188 frm->len -= 8;
189 tmp = ntohl(u64 & 0xffffffff);
190 u64 = (tmp << 32) | ntohl(u64 >> 32);
191 return u64;
192 }
193
get_u128(struct frame * frm,uint64_t * l,uint64_t * h)194 static inline void get_u128(struct frame *frm, uint64_t *l, uint64_t *h)
195 {
196 *h = get_u64(frm);
197 *l = get_u64(frm);
198 }
199
200 char *get_uuid_name(int uuid);
201
202 void set_proto(uint16_t handle, uint16_t psm, uint8_t channel, uint32_t proto);
203 uint32_t get_proto(uint16_t handle, uint16_t psm, uint8_t channel);
204
205 struct frame *add_frame(struct frame *frm);
206 void del_frame(uint16_t handle, uint8_t dlci);
207
208 uint8_t get_opcode(uint16_t handle, uint8_t dlci);
209 void set_opcode(uint16_t handle, uint8_t dlci, uint8_t opcode);
210
211 uint8_t get_status(uint16_t handle, uint8_t dlci);
212 void set_status(uint16_t handle, uint8_t dlci, uint8_t status);
213
214 void l2cap_clear(uint16_t handle);
215
216 void ascii_dump(int level, struct frame *frm, int num);
217 void hex_dump(int level, struct frame *frm, int num);
218 void ext_dump(int level, struct frame *frm, int num);
219 void raw_dump(int level, struct frame *frm);
220 void raw_ndump(int level, struct frame *frm, int num);
221
222 void lmp_dump(int level, struct frame *frm);
223 void hci_dump(int level, struct frame *frm);
224 void l2cap_dump(int level, struct frame *frm);
225 void rfcomm_dump(int level, struct frame *frm);
226 void sdp_dump(int level, struct frame *frm);
227 void bnep_dump(int level, struct frame *frm);
228 void cmtp_dump(int level, struct frame *frm);
229 void hidp_dump(int level, struct frame *frm);
230 void hcrp_dump(int level, struct frame *frm);
231 void avdtp_dump(int level, struct frame *frm);
232 void avctp_dump(int level, struct frame *frm);
233 void att_dump(int level, struct frame *frm);
234
235 void obex_dump(int level, struct frame *frm);
236 void capi_dump(int level, struct frame *frm);
237 void ppp_dump(int level, struct frame *frm);
238 void arp_dump(int level, struct frame *frm);
239 void ip_dump(int level, struct frame *frm);
240 void ericsson_dump(int level, struct frame *frm);
241 void csr_dump(int level, struct frame *frm);
242 void bpa_dump(int level, struct frame *frm);
243
parse(struct frame * frm)244 static inline void parse(struct frame *frm)
245 {
246 p_indent(-1, NULL);
247 if (parser.flags & DUMP_RAW)
248 raw_dump(0, frm);
249 else
250 hci_dump(0, frm);
251 fflush(stdout);
252 }
253
254 #endif /* __PARSER_H */
255