1 // SPDX-License-Identifier: LGPL-2.1-or-later
4 * BlueZ - Bluetooth protocol stack for Linux
6 * Copyright (C) 2011-2014 Intel Corporation
7 * Copyright (C) 2002-2010 Marcel Holtmann <marcel@holtmann.org>
24 #include <sys/socket.h>
26 #include <sys/types.h>
30 #include <linux/filter.h>
32 #include "lib/bluetooth.h"
36 #include "src/shared/util.h"
37 #include "src/shared/btsnoop.h"
38 #include "src/shared/mainloop.h"
48 static struct btsnoop *btsnoop_file = NULL;
49 static bool hcidump_fallback = false;
50 static bool decode_control = true;
51 static uint16_t filter_index = HCI_DEV_NONE;
56 unsigned char buf[BTSNOOP_MAX_PACKET_SIZE];
60 static void free_data(void *user_data)
62 struct control_data *data = user_data;
69 static void mgmt_index_added(uint16_t len, const void *buf)
71 printf("@ Index Added\n");
73 packet_hexdump(buf, len);
76 static void mgmt_index_removed(uint16_t len, const void *buf)
78 printf("@ Index Removed\n");
80 packet_hexdump(buf, len);
83 static void mgmt_unconf_index_added(uint16_t len, const void *buf)
85 printf("@ Unconfigured Index Added\n");
87 packet_hexdump(buf, len);
90 static void mgmt_unconf_index_removed(uint16_t len, const void *buf)
92 printf("@ Unconfigured Index Removed\n");
94 packet_hexdump(buf, len);
97 static void mgmt_ext_index_added(uint16_t len, const void *buf)
99 const struct mgmt_ev_ext_index_added *ev = buf;
101 if (len < sizeof(*ev)) {
102 printf("* Malformed Extended Index Added control\n");
106 printf("@ Extended Index Added: %u (%u)\n", ev->type, ev->bus);
111 packet_hexdump(buf, len);
114 static void mgmt_ext_index_removed(uint16_t len, const void *buf)
116 const struct mgmt_ev_ext_index_removed *ev = buf;
118 if (len < sizeof(*ev)) {
119 printf("* Malformed Extended Index Removed control\n");
123 printf("@ Extended Index Removed: %u (%u)\n", ev->type, ev->bus);
128 packet_hexdump(buf, len);
131 static void mgmt_controller_error(uint16_t len, const void *buf)
133 const struct mgmt_ev_controller_error *ev = buf;
135 if (len < sizeof(*ev)) {
136 printf("* Malformed Controller Error control\n");
140 printf("@ Controller Error: 0x%2.2x\n", ev->error_code);
145 packet_hexdump(buf, len);
149 #define NELEM(x) (sizeof(x) / sizeof((x)[0]))
152 static const char *config_options_str[] = {
153 "external", "public-address",
156 static void mgmt_new_config_options(uint16_t len, const void *buf)
162 printf("* Malformed New Configuration Options control\n");
166 options = get_le32(buf);
168 printf("@ New Configuration Options: 0x%4.4x\n", options);
171 printf("%-12c", ' ');
172 for (i = 0; i < NELEM(config_options_str); i++) {
173 if (options & (1 << i))
174 printf("%s ", config_options_str[i]);
182 packet_hexdump(buf, len);
185 static const char *settings_str[] = {
186 "powered", "connectable", "fast-connectable", "discoverable",
187 "bondable", "link-security", "ssp", "br/edr", "hs", "le",
188 "advertising", "secure-conn", "debug-keys", "privacy",
189 "configuration", "static-addr", "phy", "wbs"
192 static void mgmt_new_settings(uint16_t len, const void *buf)
198 printf("* Malformed New Settings control\n");
202 settings = get_le32(buf);
204 printf("@ New Settings: 0x%4.4x\n", settings);
207 printf("%-12c", ' ');
208 for (i = 0; i < NELEM(settings_str); i++) {
209 if (settings & (1 << i))
210 printf("%s ", settings_str[i]);
218 packet_hexdump(buf, len);
221 static void mgmt_class_of_dev_changed(uint16_t len, const void *buf)
223 const struct mgmt_ev_class_of_dev_changed *ev = buf;
225 if (len < sizeof(*ev)) {
226 printf("* Malformed Class of Device Changed control\n");
230 printf("@ Class of Device Changed: 0x%2.2x%2.2x%2.2x\n",
238 packet_hexdump(buf, len);
241 static void mgmt_local_name_changed(uint16_t len, const void *buf)
243 const struct mgmt_ev_local_name_changed *ev = buf;
245 if (len < sizeof(*ev)) {
246 printf("* Malformed Local Name Changed control\n");
250 printf("@ Local Name Changed: %s (%s)\n", ev->name, ev->short_name);
255 packet_hexdump(buf, len);
258 static void mgmt_new_link_key(uint16_t len, const void *buf)
260 const struct mgmt_ev_new_link_key *ev = buf;
263 static const char *types[] = {
267 "Debug Combination key",
268 "Unauthenticated Combination key from P-192",
269 "Authenticated Combination key from P-192",
270 "Changed Combination key",
271 "Unauthenticated Combination key from P-256",
272 "Authenticated Combination key from P-256",
275 if (len < sizeof(*ev)) {
276 printf("* Malformed New Link Key control\n");
280 if (ev->key.type < NELEM(types))
281 type = types[ev->key.type];
285 ba2str(&ev->key.addr.bdaddr, str);
287 printf("@ New Link Key: %s (%d) %s (%u)\n", str,
288 ev->key.addr.type, type, ev->key.type);
293 packet_hexdump(buf, len);
296 static void mgmt_new_long_term_key(uint16_t len, const void *buf)
298 const struct mgmt_ev_new_long_term_key *ev = buf;
302 if (len < sizeof(*ev)) {
303 printf("* Malformed New Long Term Key control\n");
307 /* LE SC keys are both for central and peripheral */
308 switch (ev->key.type) {
311 type = "Central (Unauthenticated)";
313 type = "Peripheral (Unauthenticated)";
317 type = "Central (Authenticated)";
319 type = "Peripheral (Authenticated)";
322 type = "SC (Unauthenticated)";
325 type = "SC (Authenticated)";
335 ba2str(&ev->key.addr.bdaddr, str);
337 printf("@ New Long Term Key: %s (%d) %s 0x%02x\n", str,
338 ev->key.addr.type, type, ev->key.type);
343 packet_hexdump(buf, len);
346 static void mgmt_device_connected(uint16_t len, const void *buf)
348 const struct mgmt_ev_device_connected *ev = buf;
352 if (len < sizeof(*ev)) {
353 printf("* Malformed Device Connected control\n");
357 flags = le32_to_cpu(ev->flags);
358 ba2str(&ev->addr.bdaddr, str);
360 printf("@ Device Connected: %s (%d) flags 0x%4.4x\n",
361 str, ev->addr.type, flags);
366 packet_hexdump(buf, len);
369 static void mgmt_device_disconnected(uint16_t len, const void *buf)
371 const struct mgmt_ev_device_disconnected *ev = buf;
374 uint16_t consumed_len;
376 if (len < sizeof(struct mgmt_addr_info)) {
377 printf("* Malformed Device Disconnected control\n");
381 if (len < sizeof(*ev)) {
382 reason = MGMT_DEV_DISCONN_UNKNOWN;
386 consumed_len = sizeof(*ev);
389 ba2str(&ev->addr.bdaddr, str);
391 printf("@ Device Disconnected: %s (%d) reason %u\n", str, ev->addr.type,
397 packet_hexdump(buf, len);
400 static void mgmt_connect_failed(uint16_t len, const void *buf)
402 const struct mgmt_ev_connect_failed *ev = buf;
405 if (len < sizeof(*ev)) {
406 printf("* Malformed Connect Failed control\n");
410 ba2str(&ev->addr.bdaddr, str);
412 printf("@ Connect Failed: %s (%d) status 0x%2.2x\n",
413 str, ev->addr.type, ev->status);
418 packet_hexdump(buf, len);
421 static void mgmt_pin_code_request(uint16_t len, const void *buf)
423 const struct mgmt_ev_pin_code_request *ev = buf;
426 if (len < sizeof(*ev)) {
427 printf("* Malformed PIN Code Request control\n");
431 ba2str(&ev->addr.bdaddr, str);
433 printf("@ PIN Code Request: %s (%d) secure 0x%2.2x\n",
434 str, ev->addr.type, ev->secure);
439 packet_hexdump(buf, len);
442 static void mgmt_user_confirm_request(uint16_t len, const void *buf)
444 const struct mgmt_ev_user_confirm_request *ev = buf;
447 if (len < sizeof(*ev)) {
448 printf("* Malformed User Confirmation Request control\n");
452 ba2str(&ev->addr.bdaddr, str);
454 printf("@ User Confirmation Request: %s (%d) hint %d value %d\n",
455 str, ev->addr.type, ev->confirm_hint, ev->value);
460 packet_hexdump(buf, len);
463 static void mgmt_user_passkey_request(uint16_t len, const void *buf)
465 const struct mgmt_ev_user_passkey_request *ev = buf;
468 if (len < sizeof(*ev)) {
469 printf("* Malformed User Passkey Request control\n");
473 ba2str(&ev->addr.bdaddr, str);
475 printf("@ User Passkey Request: %s (%d)\n", str, ev->addr.type);
480 packet_hexdump(buf, len);
483 static void mgmt_auth_failed(uint16_t len, const void *buf)
485 const struct mgmt_ev_auth_failed *ev = buf;
488 if (len < sizeof(*ev)) {
489 printf("* Malformed Authentication Failed control\n");
493 ba2str(&ev->addr.bdaddr, str);
495 printf("@ Authentication Failed: %s (%d) status 0x%2.2x\n",
496 str, ev->addr.type, ev->status);
501 packet_hexdump(buf, len);
504 static void mgmt_device_found(uint16_t len, const void *buf)
506 const struct mgmt_ev_device_found *ev = buf;
510 if (len < sizeof(*ev)) {
511 printf("* Malformed Device Found control\n");
515 flags = le32_to_cpu(ev->flags);
516 ba2str(&ev->addr.bdaddr, str);
518 printf("@ Device Found: %s (%d) rssi %d flags 0x%4.4x\n",
519 str, ev->addr.type, ev->rssi, flags);
524 packet_hexdump(buf, len);
527 static void mgmt_discovering(uint16_t len, const void *buf)
529 const struct mgmt_ev_discovering *ev = buf;
531 if (len < sizeof(*ev)) {
532 printf("* Malformed Discovering control\n");
536 printf("@ Discovering: 0x%2.2x (%d)\n", ev->discovering, ev->type);
541 packet_hexdump(buf, len);
544 static void mgmt_device_blocked(uint16_t len, const void *buf)
546 const struct mgmt_ev_device_blocked *ev = buf;
549 if (len < sizeof(*ev)) {
550 printf("* Malformed Device Blocked control\n");
554 ba2str(&ev->addr.bdaddr, str);
556 printf("@ Device Blocked: %s (%d)\n", str, ev->addr.type);
561 packet_hexdump(buf, len);
564 static void mgmt_device_unblocked(uint16_t len, const void *buf)
566 const struct mgmt_ev_device_unblocked *ev = buf;
569 if (len < sizeof(*ev)) {
570 printf("* Malformed Device Unblocked control\n");
574 ba2str(&ev->addr.bdaddr, str);
576 printf("@ Device Unblocked: %s (%d)\n", str, ev->addr.type);
581 packet_hexdump(buf, len);
584 static void mgmt_device_unpaired(uint16_t len, const void *buf)
586 const struct mgmt_ev_device_unpaired *ev = buf;
589 if (len < sizeof(*ev)) {
590 printf("* Malformed Device Unpaired control\n");
594 ba2str(&ev->addr.bdaddr, str);
596 printf("@ Device Unpaired: %s (%d)\n", str, ev->addr.type);
601 packet_hexdump(buf, len);
604 static void mgmt_passkey_notify(uint16_t len, const void *buf)
606 const struct mgmt_ev_passkey_notify *ev = buf;
610 if (len < sizeof(*ev)) {
611 printf("* Malformed Passkey Notify control\n");
615 ba2str(&ev->addr.bdaddr, str);
617 passkey = le32_to_cpu(ev->passkey);
619 printf("@ Passkey Notify: %s (%d) passkey %06u entered %u\n",
620 str, ev->addr.type, passkey, ev->entered);
625 packet_hexdump(buf, len);
628 static void mgmt_new_irk(uint16_t len, const void *buf)
630 const struct mgmt_ev_new_irk *ev = buf;
631 char addr[18], rpa[18];
633 if (len < sizeof(*ev)) {
634 printf("* Malformed New IRK control\n");
638 ba2str(&ev->rpa, rpa);
639 ba2str(&ev->key.addr.bdaddr, addr);
641 printf("@ New IRK: %s (%d) %s\n", addr, ev->key.addr.type, rpa);
646 packet_hexdump(buf, len);
649 static void mgmt_new_csrk(uint16_t len, const void *buf)
651 const struct mgmt_ev_new_csrk *ev = buf;
655 if (len < sizeof(*ev)) {
656 printf("* Malformed New CSRK control\n");
660 ba2str(&ev->key.addr.bdaddr, addr);
662 switch (ev->key.type) {
664 type = "Local Unauthenticated";
667 type = "Remote Unauthenticated";
670 type = "Local Authenticated";
673 type = "Remote Authenticated";
680 printf("@ New CSRK: %s (%d) %s (%u)\n", addr, ev->key.addr.type,
686 packet_hexdump(buf, len);
689 static void mgmt_device_added(uint16_t len, const void *buf)
691 const struct mgmt_ev_device_added *ev = buf;
694 if (len < sizeof(*ev)) {
695 printf("* Malformed Device Added control\n");
699 ba2str(&ev->addr.bdaddr, str);
701 printf("@ Device Added: %s (%d) %d\n", str, ev->addr.type, ev->action);
706 packet_hexdump(buf, len);
709 static void mgmt_device_removed(uint16_t len, const void *buf)
711 const struct mgmt_ev_device_removed *ev = buf;
714 if (len < sizeof(*ev)) {
715 printf("* Malformed Device Removed control\n");
719 ba2str(&ev->addr.bdaddr, str);
721 printf("@ Device Removed: %s (%d)\n", str, ev->addr.type);
726 packet_hexdump(buf, len);
729 static void mgmt_new_conn_param(uint16_t len, const void *buf)
731 const struct mgmt_ev_new_conn_param *ev = buf;
733 uint16_t min, max, latency, timeout;
735 if (len < sizeof(*ev)) {
736 printf("* Malformed New Connection Parameter control\n");
740 ba2str(&ev->addr.bdaddr, addr);
741 min = le16_to_cpu(ev->min_interval);
742 max = le16_to_cpu(ev->max_interval);
743 latency = le16_to_cpu(ev->latency);
744 timeout = le16_to_cpu(ev->timeout);
746 printf("@ New Conn Param: %s (%d) hint %d min 0x%4.4x max 0x%4.4x "
747 "latency 0x%4.4x timeout 0x%4.4x\n", addr, ev->addr.type,
748 ev->store_hint, min, max, latency, timeout);
753 packet_hexdump(buf, len);
756 static void mgmt_advertising_added(uint16_t len, const void *buf)
758 const struct mgmt_ev_advertising_added *ev = buf;
760 if (len < sizeof(*ev)) {
761 printf("* Malformed Advertising Added control\n");
765 printf("@ Advertising Added: %u\n", ev->instance);
770 packet_hexdump(buf, len);
773 static void mgmt_advertising_removed(uint16_t len, const void *buf)
775 const struct mgmt_ev_advertising_removed *ev = buf;
777 if (len < sizeof(*ev)) {
778 printf("* Malformed Advertising Removed control\n");
782 printf("@ Advertising Removed: %u\n", ev->instance);
787 packet_hexdump(buf, len);
790 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, 0,
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 void attach_index_filter(int fd, uint16_t index)
1024 struct sock_filter filters[] = {
1028 BPF_STMT(BPF_LD + BPF_B + BPF_ABS,
1029 offsetof(struct mgmt_hdr, index)),
1030 /* Accept if index is HCI_DEV_NONE:
1033 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, HCI_DEV_NONE, 0, 1),
1035 BPF_STMT(BPF_RET|BPF_K, 0x0fffffff), /* pass */
1036 /* Accept if index match:
1039 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, index, 0, 1),
1041 BPF_STMT(BPF_RET|BPF_K, 0x0fffffff), /* pass */
1042 BPF_STMT(BPF_RET|BPF_K, 0), /* reject */
1044 struct sock_fprog fprog = {
1045 .len = sizeof(filters) / sizeof(filters[0]),
1046 /* casting const away: */
1050 setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &fprog, sizeof(fprog));
1053 static int open_channel(uint16_t channel)
1055 struct control_data *data;
1057 data = malloc(sizeof(*data));
1061 memset(data, 0, sizeof(*data));
1062 data->channel = channel;
1064 data->fd = open_socket(channel);
1070 if (filter_index != HCI_DEV_NONE)
1071 attach_index_filter(data->fd, filter_index);
1073 if (mainloop_add_fd(data->fd, EPOLLIN, data_callback,
1074 data, free_data) < 0) {
1083 static void client_callback(int fd, uint32_t events, void *user_data)
1085 struct control_data *data = user_data;
1088 if (events & (EPOLLERR | EPOLLHUP)) {
1089 mainloop_remove_fd(data->fd);
1093 len = recv(data->fd, data->buf + data->offset,
1094 sizeof(data->buf) - data->offset, MSG_DONTWAIT);
1098 data->offset += len;
1100 while (data->offset >= MGMT_HDR_SIZE) {
1101 struct mgmt_hdr *hdr = (struct mgmt_hdr *) data->buf;
1102 uint16_t pktlen = le16_to_cpu(hdr->len);
1103 uint16_t opcode, index;
1105 if (data->offset < pktlen + MGMT_HDR_SIZE)
1108 opcode = le16_to_cpu(hdr->opcode);
1109 index = le16_to_cpu(hdr->index);
1111 packet_monitor(NULL, NULL, index, opcode,
1112 data->buf + MGMT_HDR_SIZE, pktlen);
1114 data->offset -= pktlen + MGMT_HDR_SIZE;
1116 if (data->offset > 0)
1117 memmove(data->buf, data->buf + MGMT_HDR_SIZE + pktlen,
1122 static void server_accept_callback(int fd, uint32_t events, void *user_data)
1124 struct control_data *data;
1125 struct sockaddr_un addr;
1129 if (events & (EPOLLERR | EPOLLHUP)) {
1130 mainloop_remove_fd(fd);
1134 memset(&addr, 0, sizeof(addr));
1137 nfd = accept(fd, (struct sockaddr *) &addr, &len);
1139 perror("Failed to accept client socket");
1143 printf("--- New monitor connection ---\n");
1145 data = malloc(sizeof(*data));
1151 memset(data, 0, sizeof(*data));
1152 data->channel = HCI_CHANNEL_MONITOR;
1155 if (mainloop_add_fd(data->fd, EPOLLIN, client_callback,
1156 data, free_data) < 0) {
1162 static int server_fd = -1;
1164 void control_server(const char *path)
1166 struct sockaddr_un addr;
1172 if (strlen(path) > sizeof(addr.sun_path) - 1) {
1173 fprintf(stderr, "Socket name too long\n");
1179 fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
1181 perror("Failed to open server socket");
1185 memset(&addr, 0, sizeof(addr));
1186 addr.sun_family = AF_UNIX;
1187 strcpy(addr.sun_path, path);
1189 if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1190 perror("Failed to bind server socket");
1195 if (listen(fd, 5) < 0) {
1196 perror("Failed to listen server socket");
1201 if (mainloop_add_fd(fd, EPOLLIN, server_accept_callback,
1210 static bool parse_drops(uint8_t **data, uint8_t *len, uint8_t *drops,
1224 static bool tty_parse_header(uint8_t *hdr, uint8_t len, struct timeval **tv,
1225 struct timeval *ctv, uint32_t *drops)
1238 uint8_t type = hdr[0];
1243 case TTY_EXTHDR_COMMAND_DROPS:
1244 if (!parse_drops(&hdr, &len, &cmd, &total))
1247 case TTY_EXTHDR_EVENT_DROPS:
1248 if (!parse_drops(&hdr, &len, &evt, &total))
1251 case TTY_EXTHDR_ACL_TX_DROPS:
1252 if (!parse_drops(&hdr, &len, &acl_tx, &total))
1255 case TTY_EXTHDR_ACL_RX_DROPS:
1256 if (!parse_drops(&hdr, &len, &acl_rx, &total))
1259 case TTY_EXTHDR_SCO_TX_DROPS:
1260 if (!parse_drops(&hdr, &len, &sco_tx, &total))
1263 case TTY_EXTHDR_SCO_RX_DROPS:
1264 if (!parse_drops(&hdr, &len, &sco_rx, &total))
1267 case TTY_EXTHDR_OTHER_DROPS:
1268 if (!parse_drops(&hdr, &len, &other, &total))
1271 case TTY_EXTHDR_TS32:
1272 if (len < sizeof(ts32))
1274 ts32 = get_le32(hdr);
1275 hdr += sizeof(ts32); len -= sizeof(ts32);
1276 /* ts32 is in units of 1/10th of a millisecond */
1277 ctv->tv_sec = ts32 / 10000;
1278 ctv->tv_usec = (ts32 % 10000) * 100;
1282 printf("Unknown extended header type %u\n", type);
1289 printf("* Drops: cmd %u evt %u acl_tx %u acl_rx %u sco_tx %u "
1290 "sco_rx %u other %u\n", cmd, evt, acl_tx, acl_rx,
1291 sco_tx, sco_rx, other);
1297 static void process_data(struct control_data *data)
1299 while (data->offset >= sizeof(struct tty_hdr)) {
1300 struct tty_hdr *hdr = (struct tty_hdr *) data->buf;
1301 uint16_t pktlen, opcode, data_len;
1302 struct timeval *tv = NULL;
1306 data_len = le16_to_cpu(hdr->data_len);
1308 if (data->offset < 2 + data_len)
1311 if (data->offset < sizeof(*hdr) + hdr->hdr_len) {
1312 fprintf(stderr, "Received corrupted data from TTY\n");
1313 memmove(data->buf, data->buf + 2 + data_len,
1318 if (!tty_parse_header(hdr->ext_hdr, hdr->hdr_len,
1320 fprintf(stderr, "Unable to parse extended header\n");
1322 opcode = le16_to_cpu(hdr->opcode);
1323 pktlen = data_len - 4 - hdr->hdr_len;
1325 btsnoop_write_hci(btsnoop_file, tv, 0, opcode, drops,
1326 hdr->ext_hdr + hdr->hdr_len, pktlen);
1327 ellisys_inject_hci(tv, 0, opcode, hdr->ext_hdr + hdr->hdr_len,
1329 packet_monitor(tv, NULL, 0, opcode,
1330 hdr->ext_hdr + hdr->hdr_len, pktlen);
1332 data->offset -= 2 + data_len;
1334 if (data->offset > 0)
1335 memmove(data->buf, data->buf + 2 + data_len,
1340 static void tty_callback(int fd, uint32_t events, void *user_data)
1342 struct control_data *data = user_data;
1345 if (events & (EPOLLERR | EPOLLHUP)) {
1346 mainloop_remove_fd(data->fd);
1350 len = read(data->fd, data->buf + data->offset,
1351 sizeof(data->buf) - data->offset);
1355 data->offset += len;
1360 int control_tty(const char *path, unsigned int speed)
1362 struct control_data *data;
1366 fd = open(path, O_RDWR | O_NOCTTY | O_NONBLOCK);
1369 perror("Failed to open serial port");
1373 if (tcflush(fd, TCIOFLUSH) < 0) {
1375 perror("Failed to flush serial port");
1380 memset(&ti, 0, sizeof(ti));
1381 /* Switch TTY to raw mode */
1384 ti.c_cflag |= (CLOCAL | CREAD);
1385 ti.c_cflag &= ~CRTSCTS;
1387 cfsetspeed(&ti, speed);
1389 if (tcsetattr(fd, TCSANOW, &ti) < 0) {
1391 perror("Failed to set serial port settings");
1396 printf("--- %s opened ---\n", path);
1398 data = malloc(sizeof(*data));
1404 memset(data, 0, sizeof(*data));
1405 data->channel = HCI_CHANNEL_MONITOR;
1408 if (mainloop_add_fd(data->fd, EPOLLIN, tty_callback,
1409 data, free_data) < 0) {
1418 static void rtt_callback(int id, void *user_data)
1420 struct control_data *data = user_data;
1424 len = jlink_rtt_read(data->buf + data->offset,
1425 sizeof(data->buf) - data->offset);
1426 data->offset += len;
1430 if (mainloop_modify_timeout(id, 1) < 0)
1431 mainloop_exit_failure();
1434 int control_rtt(char *jlink, char *rtt)
1436 struct control_data *data;
1438 if (jlink_init() < 0) {
1439 fprintf(stderr, "Failed to initialize J-Link library\n");
1443 if (jlink_connect(jlink) < 0) {
1444 fprintf(stderr, "Failed to connect to target device\n");
1448 if (jlink_start_rtt(rtt) < 0) {
1449 fprintf(stderr, "Failed to initialize RTT\n");
1453 printf("--- RTT opened ---\n");
1455 data = new0(struct control_data, 1);
1456 data->channel = HCI_CHANNEL_MONITOR;
1459 if (mainloop_add_timeout(1, rtt_callback, data, free_data) < 0) {
1467 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
1468 bool control_writer(const char *path, int16_t rotate_count, ssize_t file_size)
1470 bool control_writer(const char *path)
1473 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
1474 btsnoop_file = btsnoop_create(path, BTSNOOP_FORMAT_MONITOR,
1475 rotate_count, file_size);
1477 btsnoop_file = btsnoop_create(path, 0, 0, BTSNOOP_FORMAT_MONITOR);
1480 return !!btsnoop_file;
1483 void control_reader(const char *path, bool pager)
1485 unsigned char buf[BTSNOOP_MAX_PACKET_SIZE];
1490 btsnoop_file = btsnoop_open(path, BTSNOOP_FLAG_PKLG_SUPPORT);
1494 format = btsnoop_get_format(btsnoop_file);
1497 case BTSNOOP_FORMAT_HCI:
1498 case BTSNOOP_FORMAT_UART:
1499 case BTSNOOP_FORMAT_SIMULATOR:
1500 packet_del_filter(PACKET_FILTER_SHOW_INDEX);
1503 case BTSNOOP_FORMAT_MONITOR:
1504 packet_add_filter(PACKET_FILTER_SHOW_INDEX);
1508 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
1509 setenv("PAGER", "cat", 0);
1515 case BTSNOOP_FORMAT_HCI:
1516 case BTSNOOP_FORMAT_UART:
1517 case BTSNOOP_FORMAT_MONITOR:
1519 uint16_t index, opcode;
1521 if (!btsnoop_read_hci(btsnoop_file, &tv, &index,
1522 &opcode, buf, &pktlen))
1525 if (opcode == 0xffff)
1528 packet_monitor(&tv, NULL, index, opcode, buf, pktlen);
1529 ellisys_inject_hci(&tv, index, opcode, buf, pktlen);
1533 case BTSNOOP_FORMAT_SIMULATOR:
1537 if (!btsnoop_read_phy(btsnoop_file, &tv, &frequency,
1541 packet_simulator(&tv, frequency, buf, pktlen);
1549 btsnoop_unref(btsnoop_file);
1552 int control_tracing(void)
1554 packet_add_filter(PACKET_FILTER_SHOW_INDEX);
1559 if (open_channel(HCI_CHANNEL_MONITOR) < 0) {
1560 if (!hcidump_fallback)
1562 if (hcidump_tracing() < 0)
1567 if (packet_has_filter(PACKET_FILTER_SHOW_MGMT_SOCKET))
1568 open_channel(HCI_CHANNEL_CONTROL);
1573 void control_disable_decoding(void)
1575 decode_control = false;
1578 void control_filter_index(uint16_t index)
1580 filter_index = index;