3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2004-2007 Marcel Holtmann <marcel@holtmann.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34 #include <sys/ioctl.h>
35 #include <sys/socket.h>
37 #include <bluetooth/bluetooth.h>
38 #include <bluetooth/hci.h>
39 #include <bluetooth/hci_lib.h>
41 static volatile sig_atomic_t __io_canceled = 0;
43 static void sig_hup(int sig)
47 static void sig_term(int sig)
57 { 195, 1, "2001-11-27" },
58 { 220, 2, "2002-01-03" },
59 { 269, 3, "2002-02-22" },
60 { 270, 4, "2002-02-26" },
61 { 284, 5, "2002-03-12" },
62 { 292, 6, "2002-03-20" },
63 { 305, 7, "2002-04-12" },
64 { 306, 8, "2002-04-12" },
65 { 343, 9, "2002-05-02" },
66 { 346, 10, "2002-05-03" },
67 { 355, 11, "2002-05-16" },
68 { 256, 11, "2002-05-16" },
69 { 390, 12, "2002-06-26" },
70 { 450, 13, "2002-08-16" },
71 { 451, 13, "2002-08-16" },
72 { 533, 14, "2002-10-11" },
73 { 580, 15, "2002-11-14" },
74 { 623, 16, "2002-12-12" },
75 { 678, 17, "2003-01-29" },
76 { 847, 18, "2003-04-17" },
77 { 876, 19, "2003-06-10" },
78 { 997, 22, "2003-09-05" },
79 { 1027, 23, "2003-10-03" },
80 { 1029, 24, "2003-10-03" },
81 { 1112, 25, "2003-12-03" },
82 { 1113, 25, "2003-12-03" },
83 { 1133, 26, "2003-12-18" },
84 { 1134, 26, "2003-12-18" },
85 { 1223, 27, "2004-03-08" },
86 { 1224, 27, "2004-03-08" },
87 { 1319, 31, "2004-04-22" },
88 { 1320, 31, "2004-04-22" },
89 { 1427, 34, "2004-06-16" },
90 { 1508, 35, "2004-07-19" },
91 { 1509, 35, "2004-07-19" },
92 { 1587, 36, "2004-08-18" },
93 { 1588, 36, "2004-08-18" },
94 { 1641, 37, "2004-09-16" },
95 { 1642, 37, "2004-09-16" },
96 { 1699, 38, "2004-10-07" },
97 { 1700, 38, "2004-10-07" },
98 { 1752, 39, "2004-11-02" },
99 { 1753, 39, "2004-11-02" },
100 { 1759, 40, "2004-11-03" },
101 { 1760, 40, "2004-11-03" },
102 { 1761, 40, "2004-11-03" },
103 { 2009, 41, "2005-04-06" },
104 { 2010, 41, "2005-04-06" },
105 { 2011, 41, "2005-04-06" },
106 { 2016, 42, "2005-04-11" },
107 { 2017, 42, "2005-04-11" },
108 { 2018, 42, "2005-04-11" },
109 { 2023, 43, "2005-04-14" },
110 { 2024, 43, "2005-04-14" },
111 { 2025, 43, "2005-04-14" },
112 { 2032, 44, "2005-04-18" },
113 { 2033, 44, "2005-04-18" },
114 { 2034, 44, "2005-04-18" },
115 { 2288, 45, "2005-07-08" },
116 { 2289, 45, "2005-07-08" },
117 { 2290, 45, "2005-07-08" },
118 { 2388, 46, "2005-08-17" },
119 { 2389, 46, "2005-08-17" },
120 { 2390, 46, "2005-08-17" },
121 { 2869, 47, "2006-02-15" },
122 { 2870, 47, "2006-02-15" },
123 { 2871, 47, "2006-02-15" },
124 { 3214, 48, "2006-02-16" },
125 { 3215, 48, "2006-02-16" },
126 { 3216, 48, "2006-02-16" },
130 static int id2ver(uint16_t id)
134 for (i = 0; firmware_map[i].id; i++)
135 if (firmware_map[i].id == id)
136 return firmware_map[i].ver;
141 static void usage(void)
143 printf("csrsniff - Utility for the CSR BlueCore sniffers\n\n");
145 "\tcsrsniff [-i <dev>] <master-bdaddr> [slave-bdaddr]\n");
148 static struct option main_options[] = {
149 { "help", 0, 0, 'h' },
150 { "device", 1, 0, 'i' },
154 int main(int argc, char *argv[])
157 struct hci_dev_info di;
158 struct hci_version ver;
159 struct hci_filter flt;
160 bdaddr_t bdaddr, master, slave;
162 int dd, opt, dev = 0;
164 bacpy(&slave, BDADDR_ANY);
166 while ((opt=getopt_long(argc, argv, "+i:h", main_options, NULL)) != -1) {
169 dev = hci_devid(optarg);
171 perror("Invalid device");
192 str2ba(argv[0], &master);
195 str2ba(argv[1], &slave);
197 dd = hci_open_dev(dev);
199 fprintf(stderr, "Can't open device hci%d: %s (%d)\n",
200 dev, strerror(errno), errno);
204 if (hci_devinfo(dev, &di) < 0) {
205 fprintf(stderr, "Can't get device info for hci%d: %s (%d)\n",
206 dev, strerror(errno), errno);
211 if (hci_read_local_version(dd, &ver, 1000) < 0) {
212 fprintf(stderr, "Can't read version for hci%d: %s (%d)\n",
213 dev, strerror(errno), errno);
218 if (ver.manufacturer != 10 || id2ver(ver.hci_rev) < 0) {
219 fprintf(stderr, "Can't find sniffer at hci%d: %s (%d)\n",
220 dev, strerror(ENOSYS), ENOSYS);
225 if (!bacmp(&di.bdaddr, BDADDR_ANY)) {
226 if (hci_read_bd_addr(dd, &bdaddr, 1000) < 0) {
227 fprintf(stderr, "Can't read address for hci%d: %s (%d)\n",
228 dev, strerror(errno), errno);
233 bacpy(&bdaddr, &di.bdaddr);
235 need_raw = !hci_test_bit(HCI_RAW, &di.flags);
237 hci_filter_clear(&flt);
238 hci_filter_set_ptype(HCI_ACLDATA_PKT, &flt);
239 hci_filter_set_ptype(HCI_EVENT_PKT, &flt);
240 hci_filter_set_event(EVT_VENDOR, &flt);
242 if (setsockopt(dd, SOL_HCI, HCI_FILTER, &flt, sizeof(flt)) < 0) {
243 fprintf(stderr, "Can't set filter for hci%d: %s (%d)\n",
244 dev, strerror(errno), errno);
249 memset(&sa, 0, sizeof(sa));
250 sa.sa_flags = SA_NOCLDSTOP;
251 sa.sa_handler = SIG_IGN;
252 sigaction(SIGCHLD, &sa, NULL);
253 sigaction(SIGPIPE, &sa, NULL);
255 sa.sa_handler = sig_term;
256 sigaction(SIGTERM, &sa, NULL);
257 sigaction(SIGINT, &sa, NULL);
259 sa.sa_handler = sig_hup;
260 sigaction(SIGHUP, &sa, NULL);
263 if (ioctl(dd, HCISETRAW, 1) < 0) {
264 fprintf(stderr, "Can't set raw mode on hci%d: %s (%d)\n",
265 dev, strerror(errno), errno);
271 printf("CSR sniffer - Bluetooth packet analyzer ver %s\n", VERSION);
274 if (ioctl(dd, HCISETRAW, 0) < 0)
275 fprintf(stderr, "Can't clear raw mode on hci%d: %s (%d)\n",
276 dev, strerror(errno), errno);