3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2011-2014 Intel Corporation
6 * Copyright (C) 2002-2010 Marcel Holtmann <marcel@holtmann.org>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library 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 GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
36 #include <sys/socket.h>
39 #include "lib/bluetooth.h"
43 #include "src/shared/util.h"
44 #include "src/shared/btsnoop.h"
45 #include "src/shared/mainloop.h"
53 static struct btsnoop *btsnoop_file = NULL;
54 static bool hcidump_fallback = false;
59 unsigned char buf[BTSNOOP_MAX_PACKET_SIZE];
63 static void free_data(void *user_data)
65 struct control_data *data = user_data;
72 static void mgmt_index_added(uint16_t len, const void *buf)
74 printf("@ Index Added\n");
76 packet_hexdump(buf, len);
79 static void mgmt_index_removed(uint16_t len, const void *buf)
81 printf("@ Index Removed\n");
83 packet_hexdump(buf, len);
86 static void mgmt_unconf_index_added(uint16_t len, const void *buf)
88 printf("@ Unconfigured Index Added\n");
90 packet_hexdump(buf, len);
93 static void mgmt_unconf_index_removed(uint16_t len, const void *buf)
95 printf("@ Unconfigured Index Removed\n");
97 packet_hexdump(buf, len);
100 static void mgmt_ext_index_added(uint16_t len, const void *buf)
102 const struct mgmt_ev_ext_index_added *ev = buf;
104 if (len < sizeof(*ev)) {
105 printf("* Malformed Extended Index Added control\n");
109 printf("@ Extended Index Added: %u (%u)\n", ev->type, ev->bus);
114 packet_hexdump(buf, len);
117 static void mgmt_ext_index_removed(uint16_t len, const void *buf)
119 const struct mgmt_ev_ext_index_removed *ev = buf;
121 if (len < sizeof(*ev)) {
122 printf("* Malformed Extended Index Removed control\n");
126 printf("@ Extended Index Removed: %u (%u)\n", ev->type, ev->bus);
131 packet_hexdump(buf, len);
134 static void mgmt_controller_error(uint16_t len, const void *buf)
136 const struct mgmt_ev_controller_error *ev = buf;
138 if (len < sizeof(*ev)) {
139 printf("* Malformed Controller Error control\n");
143 printf("@ Controller Error: 0x%2.2x\n", ev->error_code);
148 packet_hexdump(buf, len);
152 #define NELEM(x) (sizeof(x) / sizeof((x)[0]))
155 static const char *config_options_str[] = {
156 "external", "public-address",
159 static void mgmt_new_config_options(uint16_t len, const void *buf)
165 printf("* Malformed New Configuration Options control\n");
169 options = get_le32(buf);
171 printf("@ New Configuration Options: 0x%4.4x\n", options);
174 printf("%-12c", ' ');
175 for (i = 0; i < NELEM(config_options_str); i++) {
176 if (options & (1 << i))
177 printf("%s ", config_options_str[i]);
185 packet_hexdump(buf, len);
188 static const char *settings_str[] = {
189 "powered", "connectable", "fast-connectable", "discoverable",
190 "bondable", "link-security", "ssp", "br/edr", "hs", "le",
191 "advertising", "secure-conn", "debug-keys", "privacy",
192 "configuration", "static-addr",
195 static void mgmt_new_settings(uint16_t len, const void *buf)
201 printf("* Malformed New Settings control\n");
205 settings = get_le32(buf);
207 printf("@ New Settings: 0x%4.4x\n", settings);
210 printf("%-12c", ' ');
211 for (i = 0; i < NELEM(settings_str); i++) {
212 if (settings & (1 << i))
213 printf("%s ", settings_str[i]);
221 packet_hexdump(buf, len);
224 static void mgmt_class_of_dev_changed(uint16_t len, const void *buf)
226 const struct mgmt_ev_class_of_dev_changed *ev = buf;
228 if (len < sizeof(*ev)) {
229 printf("* Malformed Class of Device Changed control\n");
233 printf("@ Class of Device Changed: 0x%2.2x%2.2x%2.2x\n",
241 packet_hexdump(buf, len);
244 static void mgmt_local_name_changed(uint16_t len, const void *buf)
246 const struct mgmt_ev_local_name_changed *ev = buf;
248 if (len < sizeof(*ev)) {
249 printf("* Malformed Local Name Changed control\n");
253 printf("@ Local Name Changed: %s (%s)\n", ev->name, ev->short_name);
258 packet_hexdump(buf, len);
261 static void mgmt_new_link_key(uint16_t len, const void *buf)
263 const struct mgmt_ev_new_link_key *ev = buf;
266 static const char *types[] = {
270 "Debug Combination key",
271 "Unauthenticated Combination key from P-192",
272 "Authenticated Combination key from P-192",
273 "Changed Combination key",
274 "Unauthenticated Combination key from P-256",
275 "Authenticated Combination key from P-256",
278 if (len < sizeof(*ev)) {
279 printf("* Malformed New Link Key control\n");
283 if (ev->key.type < NELEM(types))
284 type = types[ev->key.type];
288 ba2str(&ev->key.addr.bdaddr, str);
290 printf("@ New Link Key: %s (%d) %s (%u)\n", str,
291 ev->key.addr.type, type, ev->key.type);
296 packet_hexdump(buf, len);
299 static void mgmt_new_long_term_key(uint16_t len, const void *buf)
301 const struct mgmt_ev_new_long_term_key *ev = buf;
305 if (len < sizeof(*ev)) {
306 printf("* Malformed New Long Term Key control\n");
310 /* LE SC keys are both for master and slave */
311 switch (ev->key.type) {
314 type = "Master (Unauthenticated)";
316 type = "Slave (Unauthenticated)";
320 type = "Master (Authenticated)";
322 type = "Slave (Authenticated)";
325 type = "SC (Unauthenticated)";
328 type = "SC (Authenticated)";
338 ba2str(&ev->key.addr.bdaddr, str);
340 printf("@ New Long Term Key: %s (%d) %s 0x%02x\n", str,
341 ev->key.addr.type, type, ev->key.type);
346 packet_hexdump(buf, len);
349 static void mgmt_device_connected(uint16_t len, const void *buf)
351 const struct mgmt_ev_device_connected *ev = buf;
355 if (len < sizeof(*ev)) {
356 printf("* Malformed Device Connected control\n");
360 flags = le32_to_cpu(ev->flags);
361 ba2str(&ev->addr.bdaddr, str);
363 printf("@ Device Connected: %s (%d) flags 0x%4.4x\n",
364 str, ev->addr.type, flags);
369 packet_hexdump(buf, len);
372 static void mgmt_device_disconnected(uint16_t len, const void *buf)
374 const struct mgmt_ev_device_disconnected *ev = buf;
377 uint16_t consumed_len;
379 if (len < sizeof(struct mgmt_addr_info)) {
380 printf("* Malformed Device Disconnected control\n");
384 if (len < sizeof(*ev)) {
385 reason = MGMT_DEV_DISCONN_UNKNOWN;
389 consumed_len = sizeof(*ev);
392 ba2str(&ev->addr.bdaddr, str);
394 printf("@ Device Disconnected: %s (%d) reason %u\n", str, ev->addr.type,
400 packet_hexdump(buf, len);
403 static void mgmt_connect_failed(uint16_t len, const void *buf)
405 const struct mgmt_ev_connect_failed *ev = buf;
408 if (len < sizeof(*ev)) {
409 printf("* Malformed Connect Failed control\n");
413 ba2str(&ev->addr.bdaddr, str);
415 printf("@ Connect Failed: %s (%d) status 0x%2.2x\n",
416 str, ev->addr.type, ev->status);
421 packet_hexdump(buf, len);
424 static void mgmt_pin_code_request(uint16_t len, const void *buf)
426 const struct mgmt_ev_pin_code_request *ev = buf;
429 if (len < sizeof(*ev)) {
430 printf("* Malformed PIN Code Request control\n");
434 ba2str(&ev->addr.bdaddr, str);
436 printf("@ PIN Code Request: %s (%d) secure 0x%2.2x\n",
437 str, ev->addr.type, ev->secure);
442 packet_hexdump(buf, len);
445 static void mgmt_user_confirm_request(uint16_t len, const void *buf)
447 const struct mgmt_ev_user_confirm_request *ev = buf;
450 if (len < sizeof(*ev)) {
451 printf("* Malformed User Confirmation Request control\n");
455 ba2str(&ev->addr.bdaddr, str);
457 printf("@ User Confirmation Request: %s (%d) hint %d value %d\n",
458 str, ev->addr.type, ev->confirm_hint, ev->value);
463 packet_hexdump(buf, len);
466 static void mgmt_user_passkey_request(uint16_t len, const void *buf)
468 const struct mgmt_ev_user_passkey_request *ev = buf;
471 if (len < sizeof(*ev)) {
472 printf("* Malformed User Passkey Request control\n");
476 ba2str(&ev->addr.bdaddr, str);
478 printf("@ User Passkey Request: %s (%d)\n", str, ev->addr.type);
483 packet_hexdump(buf, len);
486 static void mgmt_auth_failed(uint16_t len, const void *buf)
488 const struct mgmt_ev_auth_failed *ev = buf;
491 if (len < sizeof(*ev)) {
492 printf("* Malformed Authentication Failed control\n");
496 ba2str(&ev->addr.bdaddr, str);
498 printf("@ Authentication Failed: %s (%d) status 0x%2.2x\n",
499 str, ev->addr.type, ev->status);
504 packet_hexdump(buf, len);
507 static void mgmt_device_found(uint16_t len, const void *buf)
509 const struct mgmt_ev_device_found *ev = buf;
513 if (len < sizeof(*ev)) {
514 printf("* Malformed Device Found control\n");
518 flags = le32_to_cpu(ev->flags);
519 ba2str(&ev->addr.bdaddr, str);
521 printf("@ Device Found: %s (%d) rssi %d flags 0x%4.4x\n",
522 str, ev->addr.type, ev->rssi, flags);
527 packet_hexdump(buf, len);
530 static void mgmt_discovering(uint16_t len, const void *buf)
532 const struct mgmt_ev_discovering *ev = buf;
534 if (len < sizeof(*ev)) {
535 printf("* Malformed Discovering control\n");
539 printf("@ Discovering: 0x%2.2x (%d)\n", ev->discovering, ev->type);
544 packet_hexdump(buf, len);
547 static void mgmt_device_blocked(uint16_t len, const void *buf)
549 const struct mgmt_ev_device_blocked *ev = buf;
552 if (len < sizeof(*ev)) {
553 printf("* Malformed Device Blocked control\n");
557 ba2str(&ev->addr.bdaddr, str);
559 printf("@ Device Blocked: %s (%d)\n", str, ev->addr.type);
564 packet_hexdump(buf, len);
567 static void mgmt_device_unblocked(uint16_t len, const void *buf)
569 const struct mgmt_ev_device_unblocked *ev = buf;
572 if (len < sizeof(*ev)) {
573 printf("* Malformed Device Unblocked control\n");
577 ba2str(&ev->addr.bdaddr, str);
579 printf("@ Device Unblocked: %s (%d)\n", str, ev->addr.type);
584 packet_hexdump(buf, len);
587 static void mgmt_device_unpaired(uint16_t len, const void *buf)
589 const struct mgmt_ev_device_unpaired *ev = buf;
592 if (len < sizeof(*ev)) {
593 printf("* Malformed Device Unpaired control\n");
597 ba2str(&ev->addr.bdaddr, str);
599 printf("@ Device Unpaired: %s (%d)\n", str, ev->addr.type);
604 packet_hexdump(buf, len);
607 static void mgmt_passkey_notify(uint16_t len, const void *buf)
609 const struct mgmt_ev_passkey_notify *ev = buf;
613 if (len < sizeof(*ev)) {
614 printf("* Malformed Passkey Notify control\n");
618 ba2str(&ev->addr.bdaddr, str);
620 passkey = le32_to_cpu(ev->passkey);
622 printf("@ Passkey Notify: %s (%d) passkey %06u entered %u\n",
623 str, ev->addr.type, passkey, ev->entered);
628 packet_hexdump(buf, len);
631 static void mgmt_new_irk(uint16_t len, const void *buf)
633 const struct mgmt_ev_new_irk *ev = buf;
634 char addr[18], rpa[18];
636 if (len < sizeof(*ev)) {
637 printf("* Malformed New IRK control\n");
641 ba2str(&ev->rpa, rpa);
642 ba2str(&ev->key.addr.bdaddr, addr);
644 printf("@ New IRK: %s (%d) %s\n", addr, ev->key.addr.type, rpa);
649 packet_hexdump(buf, len);
652 static void mgmt_new_csrk(uint16_t len, const void *buf)
654 const struct mgmt_ev_new_csrk *ev = buf;
658 if (len < sizeof(*ev)) {
659 printf("* Malformed New CSRK control\n");
663 ba2str(&ev->key.addr.bdaddr, addr);
665 switch (ev->key.type) {
667 type = "Local Unauthenticated";
670 type = "Remote Unauthenticated";
673 type = "Local Authenticated";
676 type = "Remote Authenticated";
683 printf("@ New CSRK: %s (%d) %s (%u)\n", addr, ev->key.addr.type,
689 packet_hexdump(buf, len);
692 static void mgmt_device_added(uint16_t len, const void *buf)
694 const struct mgmt_ev_device_added *ev = buf;
697 if (len < sizeof(*ev)) {
698 printf("* Malformed Device Added control\n");
702 ba2str(&ev->addr.bdaddr, str);
704 printf("@ Device Added: %s (%d) %d\n", str, ev->addr.type, ev->action);
709 packet_hexdump(buf, len);
712 static void mgmt_device_removed(uint16_t len, const void *buf)
714 const struct mgmt_ev_device_removed *ev = buf;
717 if (len < sizeof(*ev)) {
718 printf("* Malformed Device Removed control\n");
722 ba2str(&ev->addr.bdaddr, str);
724 printf("@ Device Removed: %s (%d)\n", str, ev->addr.type);
729 packet_hexdump(buf, len);
732 static void mgmt_new_conn_param(uint16_t len, const void *buf)
734 const struct mgmt_ev_new_conn_param *ev = buf;
736 uint16_t min, max, latency, timeout;
738 if (len < sizeof(*ev)) {
739 printf("* Malformed New Connection Parameter control\n");
743 ba2str(&ev->addr.bdaddr, addr);
744 min = le16_to_cpu(ev->min_interval);
745 max = le16_to_cpu(ev->max_interval);
746 latency = le16_to_cpu(ev->latency);
747 timeout = le16_to_cpu(ev->timeout);
749 printf("@ New Conn Param: %s (%d) hint %d min 0x%4.4x max 0x%4.4x "
750 "latency 0x%4.4x timeout 0x%4.4x\n", addr, ev->addr.type,
751 ev->store_hint, min, max, latency, timeout);
756 packet_hexdump(buf, len);
759 static void mgmt_advertising_added(uint16_t len, const void *buf)
761 const struct mgmt_ev_advertising_added *ev = buf;
763 if (len < sizeof(*ev)) {
764 printf("* Malformed Advertising Added control\n");
768 printf("@ Advertising Added: %u\n", ev->instance);
773 packet_hexdump(buf, len);
776 static void mgmt_advertising_removed(uint16_t len, const void *buf)
778 const struct mgmt_ev_advertising_removed *ev = buf;
780 if (len < sizeof(*ev)) {
781 printf("* Malformed Advertising Removed control\n");
785 printf("@ Advertising Removed: %u\n", ev->instance);
790 packet_hexdump(buf, len);
793 void control_message(uint16_t opcode, const void *data, uint16_t size)
796 case MGMT_EV_INDEX_ADDED:
797 mgmt_index_added(size, data);
799 case MGMT_EV_INDEX_REMOVED:
800 mgmt_index_removed(size, data);
802 case MGMT_EV_CONTROLLER_ERROR:
803 mgmt_controller_error(size, data);
805 case MGMT_EV_NEW_SETTINGS:
806 mgmt_new_settings(size, data);
808 case MGMT_EV_CLASS_OF_DEV_CHANGED:
809 mgmt_class_of_dev_changed(size, data);
811 case MGMT_EV_LOCAL_NAME_CHANGED:
812 mgmt_local_name_changed(size, data);
814 case MGMT_EV_NEW_LINK_KEY:
815 mgmt_new_link_key(size, data);
817 case MGMT_EV_NEW_LONG_TERM_KEY:
818 mgmt_new_long_term_key(size, data);
820 case MGMT_EV_DEVICE_CONNECTED:
821 mgmt_device_connected(size, data);
823 case MGMT_EV_DEVICE_DISCONNECTED:
824 mgmt_device_disconnected(size, data);
826 case MGMT_EV_CONNECT_FAILED:
827 mgmt_connect_failed(size, data);
829 case MGMT_EV_PIN_CODE_REQUEST:
830 mgmt_pin_code_request(size, data);
832 case MGMT_EV_USER_CONFIRM_REQUEST:
833 mgmt_user_confirm_request(size, data);
835 case MGMT_EV_USER_PASSKEY_REQUEST:
836 mgmt_user_passkey_request(size, data);
838 case MGMT_EV_AUTH_FAILED:
839 mgmt_auth_failed(size, data);
841 case MGMT_EV_DEVICE_FOUND:
842 mgmt_device_found(size, data);
844 case MGMT_EV_DISCOVERING:
845 mgmt_discovering(size, data);
847 case MGMT_EV_DEVICE_BLOCKED:
848 mgmt_device_blocked(size, data);
850 case MGMT_EV_DEVICE_UNBLOCKED:
851 mgmt_device_unblocked(size, data);
853 case MGMT_EV_DEVICE_UNPAIRED:
854 mgmt_device_unpaired(size, data);
856 case MGMT_EV_PASSKEY_NOTIFY:
857 mgmt_passkey_notify(size, data);
859 case MGMT_EV_NEW_IRK:
860 mgmt_new_irk(size, data);
862 case MGMT_EV_NEW_CSRK:
863 mgmt_new_csrk(size, data);
865 case MGMT_EV_DEVICE_ADDED:
866 mgmt_device_added(size, data);
868 case MGMT_EV_DEVICE_REMOVED:
869 mgmt_device_removed(size, data);
871 case MGMT_EV_NEW_CONN_PARAM:
872 mgmt_new_conn_param(size, data);
874 case MGMT_EV_UNCONF_INDEX_ADDED:
875 mgmt_unconf_index_added(size, data);
877 case MGMT_EV_UNCONF_INDEX_REMOVED:
878 mgmt_unconf_index_removed(size, data);
880 case MGMT_EV_NEW_CONFIG_OPTIONS:
881 mgmt_new_config_options(size, data);
883 case MGMT_EV_EXT_INDEX_ADDED:
884 mgmt_ext_index_added(size, data);
886 case MGMT_EV_EXT_INDEX_REMOVED:
887 mgmt_ext_index_removed(size, data);
889 case MGMT_EV_ADVERTISING_ADDED:
890 mgmt_advertising_added(size, data);
892 case MGMT_EV_ADVERTISING_REMOVED:
893 mgmt_advertising_removed(size, data);
896 printf("* Unknown control (code %d len %d)\n", opcode, size);
897 packet_hexdump(data, size);
902 static void data_callback(int fd, uint32_t events, void *user_data)
904 struct control_data *data = user_data;
905 unsigned char control[64];
910 if (events & (EPOLLERR | EPOLLHUP)) {
911 mainloop_remove_fd(data->fd);
915 iov[0].iov_base = &hdr;
916 iov[0].iov_len = MGMT_HDR_SIZE;
917 iov[1].iov_base = data->buf;
918 iov[1].iov_len = sizeof(data->buf);
920 memset(&msg, 0, sizeof(msg));
923 msg.msg_control = control;
924 msg.msg_controllen = sizeof(control);
927 struct cmsghdr *cmsg;
928 struct timeval *tv = NULL;
930 struct ucred *cred = NULL;
932 uint16_t opcode, index, pktlen;
935 len = recvmsg(data->fd, &msg, MSG_DONTWAIT);
939 if (len < MGMT_HDR_SIZE)
942 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
943 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
944 if (cmsg->cmsg_level != SOL_SOCKET)
947 if (cmsg->cmsg_type == SCM_TIMESTAMP) {
948 memcpy(&ctv, CMSG_DATA(cmsg), sizeof(ctv));
952 if (cmsg->cmsg_type == SCM_CREDENTIALS) {
953 memcpy(&ccred, CMSG_DATA(cmsg), sizeof(ccred));
958 opcode = le16_to_cpu(hdr.opcode);
959 index = le16_to_cpu(hdr.index);
960 pktlen = le16_to_cpu(hdr.len);
962 switch (data->channel) {
963 case HCI_CHANNEL_CONTROL:
964 packet_control(tv, cred, index, opcode,
967 case HCI_CHANNEL_MONITOR:
968 btsnoop_write_hci(btsnoop_file, tv, index, opcode,
970 ellisys_inject_hci(tv, index, opcode,
972 packet_monitor(tv, cred, index, opcode,
979 static int open_socket(uint16_t channel)
981 struct sockaddr_hci addr;
984 fd = socket(AF_BLUETOOTH, SOCK_RAW | SOCK_CLOEXEC, BTPROTO_HCI);
986 perror("Failed to open channel");
990 memset(&addr, 0, sizeof(addr));
991 addr.hci_family = AF_BLUETOOTH;
992 addr.hci_dev = HCI_DEV_NONE;
993 addr.hci_channel = channel;
995 if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
996 if (errno == EINVAL) {
997 /* Fallback to hcidump support */
998 hcidump_fallback = true;
1002 perror("Failed to bind channel");
1007 if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMP, &opt, sizeof(opt)) < 0) {
1008 perror("Failed to enable timestamps");
1013 if (setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &opt, sizeof(opt)) < 0) {
1014 perror("Failed to enable credentials");
1022 static int open_channel(uint16_t channel)
1024 struct control_data *data;
1026 data = malloc(sizeof(*data));
1030 memset(data, 0, sizeof(*data));
1031 data->channel = channel;
1033 data->fd = open_socket(channel);
1039 mainloop_add_fd(data->fd, EPOLLIN, data_callback, data, free_data);
1044 static void client_callback(int fd, uint32_t events, void *user_data)
1046 struct control_data *data = user_data;
1049 if (events & (EPOLLERR | EPOLLHUP)) {
1050 mainloop_remove_fd(data->fd);
1054 len = recv(data->fd, data->buf + data->offset,
1055 sizeof(data->buf) - data->offset, MSG_DONTWAIT);
1059 data->offset += len;
1061 if (data->offset > MGMT_HDR_SIZE) {
1062 struct mgmt_hdr *hdr = (struct mgmt_hdr *) data->buf;
1063 uint16_t pktlen = le16_to_cpu(hdr->len);
1065 if (data->offset > pktlen + MGMT_HDR_SIZE) {
1066 uint16_t opcode = le16_to_cpu(hdr->opcode);
1067 uint16_t index = le16_to_cpu(hdr->index);
1069 packet_monitor(NULL, NULL, index, opcode,
1070 data->buf + MGMT_HDR_SIZE, pktlen);
1072 data->offset -= pktlen + MGMT_HDR_SIZE;
1074 if (data->offset > 0)
1075 memmove(data->buf, data->buf +
1076 MGMT_HDR_SIZE + pktlen, data->offset);
1081 static void server_accept_callback(int fd, uint32_t events, void *user_data)
1083 struct control_data *data;
1084 struct sockaddr_un addr;
1088 if (events & (EPOLLERR | EPOLLHUP)) {
1089 mainloop_remove_fd(fd);
1093 memset(&addr, 0, sizeof(addr));
1096 nfd = accept(fd, (struct sockaddr *) &addr, &len);
1098 perror("Failed to accept client socket");
1102 printf("--- New monitor connection ---\n");
1104 data = malloc(sizeof(*data));
1110 memset(data, 0, sizeof(*data));
1111 data->channel = HCI_CHANNEL_MONITOR;
1114 mainloop_add_fd(data->fd, EPOLLIN, client_callback, data, free_data);
1117 static int server_fd = -1;
1119 void control_server(const char *path)
1121 struct sockaddr_un addr;
1129 fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
1131 perror("Failed to open server socket");
1135 memset(&addr, 0, sizeof(addr));
1136 addr.sun_family = AF_UNIX;
1137 strcpy(addr.sun_path, path);
1139 if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1140 perror("Failed to bind server socket");
1145 if (listen(fd, 5) < 0) {
1146 perror("Failed to listen server socket");
1151 if (mainloop_add_fd(fd, EPOLLIN, server_accept_callback,
1160 #ifdef __TIZEN_PATCH__
1161 bool control_writer(const char *path, int16_t rotate_count, ssize_t file_size)
1163 bool control_writer(const char *path)
1166 #ifdef __TIZEN_PATCH__
1167 btsnoop_file = btsnoop_create(path, BTSNOOP_FORMAT_MONITOR,
1168 rotate_count, file_size);
1170 btsnoop_file = btsnoop_create(path, BTSNOOP_FORMAT_MONITOR);
1173 return !!btsnoop_file;
1176 void control_reader(const char *path)
1178 unsigned char buf[BTSNOOP_MAX_PACKET_SIZE];
1183 btsnoop_file = btsnoop_open(path, BTSNOOP_FLAG_PKLG_SUPPORT);
1187 format = btsnoop_get_format(btsnoop_file);
1190 case BTSNOOP_FORMAT_HCI:
1191 case BTSNOOP_FORMAT_UART:
1192 case BTSNOOP_FORMAT_SIMULATOR:
1193 packet_del_filter(PACKET_FILTER_SHOW_INDEX);
1196 case BTSNOOP_FORMAT_MONITOR:
1197 packet_add_filter(PACKET_FILTER_SHOW_INDEX);
1201 #ifdef __TIZEN_PATCH__
1202 setenv("PAGER", "cat", 0);
1207 case BTSNOOP_FORMAT_HCI:
1208 case BTSNOOP_FORMAT_UART:
1209 case BTSNOOP_FORMAT_MONITOR:
1211 uint16_t index, opcode;
1213 if (!btsnoop_read_hci(btsnoop_file, &tv, &index,
1214 &opcode, buf, &pktlen))
1217 if (opcode == 0xffff)
1220 packet_monitor(&tv, NULL, index, opcode, buf, pktlen);
1221 ellisys_inject_hci(&tv, index, opcode, buf, pktlen);
1225 case BTSNOOP_FORMAT_SIMULATOR:
1229 if (!btsnoop_read_phy(btsnoop_file, &tv, &frequency,
1233 packet_simulator(&tv, frequency, buf, pktlen);
1240 btsnoop_unref(btsnoop_file);
1243 int control_tracing(void)
1245 packet_add_filter(PACKET_FILTER_SHOW_INDEX);
1250 if (open_channel(HCI_CHANNEL_MONITOR) < 0) {
1251 if (!hcidump_fallback)
1253 if (hcidump_tracing() < 0)
1258 open_channel(HCI_CHANNEL_CONTROL);