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
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>
50 static void free_data(void *user_data)
52 struct hcidump_data *data = user_data;
59 static int open_hci_dev(uint16_t index)
61 struct sockaddr_hci addr;
62 struct hci_filter flt;
65 fd = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
67 perror("Failed to open channel");
72 hci_filter_clear(&flt);
73 hci_filter_all_ptypes(&flt);
74 hci_filter_all_events(&flt);
76 if (setsockopt(fd, SOL_HCI, HCI_FILTER, &flt, sizeof(flt)) < 0) {
77 perror("Failed to set HCI filter");
82 if (setsockopt(fd, SOL_HCI, HCI_DATA_DIR, &opt, sizeof(opt)) < 0) {
83 perror("Failed to enable HCI data direction info");
88 if (setsockopt(fd, SOL_HCI, HCI_TIME_STAMP, &opt, sizeof(opt)) < 0) {
89 perror("Failed to enable HCI time stamps");
94 memset(&addr, 0, sizeof(addr));
95 addr.hci_family = AF_BLUETOOTH;
97 addr.hci_channel = HCI_CHANNEL_RAW;
99 if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
100 perror("Failed to bind channel");
108 static void device_callback(int fd, uint32_t events, void *user_data)
110 struct hcidump_data *data = user_data;
111 unsigned char buf[HCI_MAX_FRAME_SIZE];
112 unsigned char control[64];
116 if (events & (EPOLLERR | EPOLLHUP)) {
117 mainloop_remove_fd(fd);
122 iov.iov_len = sizeof(buf);
124 memset(&msg, 0, sizeof(msg));
127 msg.msg_control = control;
128 msg.msg_controllen = sizeof(control);
131 struct cmsghdr *cmsg;
132 struct timeval *tv = NULL;
136 len = recvmsg(fd, &msg, MSG_DONTWAIT);
140 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
141 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
142 if (cmsg->cmsg_level != SOL_HCI)
145 switch (cmsg->cmsg_type) {
147 dir = (int *) CMSG_DATA(cmsg);
149 case HCI_CMSG_TSTAMP:
150 tv = (struct timeval *) CMSG_DATA(cmsg);
159 case HCI_COMMAND_PKT:
160 packet_hci_command(tv, data->index, buf + 1, len - 1);
163 packet_hci_event(tv, data->index, buf + 1, len - 1);
165 case HCI_ACLDATA_PKT:
166 packet_hci_acldata(tv, data->index, !!(*dir),
169 case HCI_SCODATA_PKT:
170 packet_hci_scodata(tv, data->index, !!(*dir),
177 static void open_device(uint16_t index)
179 struct hcidump_data *data;
181 data = malloc(sizeof(*data));
185 memset(data, 0, sizeof(*data));
188 data->fd = open_hci_dev(index);
194 mainloop_add_fd(data->fd, EPOLLIN, device_callback, data, free_data);
197 static void device_info(int fd, uint16_t index, uint8_t *type, uint8_t *bus,
198 bdaddr_t *bdaddr, char *name)
200 struct hci_dev_info di;
202 memset(&di, 0, sizeof(di));
205 if (ioctl(fd, HCIGETDEVINFO, (void *) &di) < 0) {
206 perror("Failed to get device information");
210 *type = di.type >> 4;
211 *bus = di.type & 0x0f;
213 bacpy(bdaddr, &di.bdaddr);
214 memcpy(name, di.name, 8);
217 static void device_list(int fd, int max_dev)
219 struct hci_dev_list_req *dl;
220 struct hci_dev_req *dr;
223 dl = malloc(max_dev * sizeof(*dr) + sizeof(*dl));
225 perror("Failed to allocate device list memory");
229 memset(dl, 0, max_dev * sizeof(*dr) + sizeof(*dl));
230 dl->dev_num = max_dev;
234 if (ioctl(fd, HCIGETDEVLIST, (void *) dl) < 0) {
235 perror("Failed to get device list");
239 for (i = 0; i < dl->dev_num; i++, dr++) {
240 struct timeval tmp_tv, *tv = NULL;
241 uint8_t type = 0xff, bus = 0xff;
242 char str[18], name[8] = "";
245 bacpy(&bdaddr, BDADDR_ANY);
247 if (!gettimeofday(&tmp_tv, NULL))
250 device_info(fd, dr->dev_id, &type, &bus, &bdaddr, name);
251 ba2str(&bdaddr, str);
252 packet_new_index(tv, dr->dev_id, str, type, bus, name);
253 open_device(dr->dev_id);
260 static int open_stack_internal(void)
262 struct sockaddr_hci addr;
263 struct hci_filter flt;
266 fd = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
268 perror("Failed to open channel");
273 hci_filter_clear(&flt);
274 hci_filter_set_ptype(HCI_EVENT_PKT, &flt);
275 hci_filter_set_event(EVT_STACK_INTERNAL, &flt);
277 if (setsockopt(fd, SOL_HCI, HCI_FILTER, &flt, sizeof(flt)) < 0) {
278 perror("Failed to set HCI filter");
283 if (setsockopt(fd, SOL_HCI, HCI_TIME_STAMP, &opt, sizeof(opt)) < 0) {
284 perror("Failed to enable HCI time stamps");
289 memset(&addr, 0, sizeof(addr));
290 addr.hci_family = AF_BLUETOOTH;
291 addr.hci_dev = HCI_DEV_NONE;
292 addr.hci_channel = HCI_CHANNEL_RAW;
294 if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
295 perror("Failed to bind channel");
300 device_list(fd, HCI_MAX_DEV);
305 static void stack_internal_callback(int fd, uint32_t events, void *user_data)
307 unsigned char buf[HCI_MAX_FRAME_SIZE];
308 unsigned char control[32];
311 struct cmsghdr *cmsg;
314 evt_stack_internal *si;
316 struct timeval *tv = NULL;
317 uint8_t type = 0xff, bus = 0xff;
318 char str[18], name[8] = "";
321 bacpy(&bdaddr, BDADDR_ANY);
323 if (events & (EPOLLERR | EPOLLHUP)) {
324 mainloop_remove_fd(fd);
329 iov.iov_len = sizeof(buf);
331 memset(&msg, 0, sizeof(msg));
334 msg.msg_control = control;
335 msg.msg_controllen = sizeof(control);
337 len = recvmsg(fd, &msg, MSG_DONTWAIT);
341 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
342 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
343 if (cmsg->cmsg_level != SOL_HCI)
346 switch (cmsg->cmsg_type) {
347 case HCI_CMSG_TSTAMP:
348 tv = (struct timeval *) CMSG_DATA(cmsg);
353 if (len < 1 + HCI_EVENT_HDR_SIZE + EVT_STACK_INTERNAL_SIZE +
357 if (buf[0] != HCI_EVENT_PKT)
360 eh = (hci_event_hdr *) (buf + 1);
361 if (eh->evt != EVT_STACK_INTERNAL)
364 si = (evt_stack_internal *) (buf + 1 + HCI_EVENT_HDR_SIZE);
365 if (si->type != EVT_SI_DEVICE)
368 sd = (evt_si_device *) &si->data;
372 device_info(fd, sd->dev_id, &type, &bus, &bdaddr, name);
373 ba2str(&bdaddr, str);
374 packet_new_index(tv, sd->dev_id, str, type, bus, name);
375 open_device(sd->dev_id);
378 ba2str(&bdaddr, str);
379 packet_del_index(tv, sd->dev_id, str);
384 int hcidump_tracing(void)
386 struct hcidump_data *data;
388 data = malloc(sizeof(*data));
392 memset(data, 0, sizeof(*data));
393 data->index = HCI_DEV_NONE;
395 data->fd = open_stack_internal();
401 mainloop_add_fd(data->fd, EPOLLIN, stack_internal_callback,