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
32 #include "src/shared/util.h"
38 #define COLOR_OPCODE COLOR_MAGENTA
39 #define COLOR_OPCODE_UNKNOWN COLOR_WHITE_BG
41 static const char *get_opcode_str(uint16_t opcode);
43 static void print_opcode(uint16_t opcode)
47 str = get_opcode_str(opcode);
52 print_field("Operation: %s (%u/%u)", str,
53 opcode >> 8, opcode & 0xff);
55 print_field("Operation: %s (%u)", str, opcode);
58 static void name_req(const void *data, uint8_t size)
60 const struct bt_lmp_name_req *pdu = data;
62 print_field("Offset: %u", pdu->offset);
65 static void name_rsp(const void *data, uint8_t size)
67 const struct bt_lmp_name_rsp *pdu = data;
70 memcpy(str, pdu->fragment, 14);
73 print_field("Offset: %u", pdu->offset);
74 print_field("Length: %u", pdu->length);
75 print_field("Fragment: %s", str);
78 static void accepted(const void *data, uint8_t size)
80 const struct bt_lmp_accepted *pdu = data;
82 print_opcode(pdu->opcode);
85 static void not_accepted(const void *data, uint8_t size)
87 const struct bt_lmp_not_accepted *pdu = data;
89 print_opcode(pdu->opcode);
90 packet_print_error("Error code", pdu->error);
93 static void clkoffset_req(const void *data, uint8_t size)
97 static void clkoffset_rsp(const void *data, uint8_t size)
99 const struct bt_lmp_clkoffset_rsp *pdu = data;
101 print_field("Clock offset: 0x%4.4x", le16_to_cpu(pdu->offset));
104 static void detach(const void *data, uint8_t size)
106 const struct bt_lmp_detach *pdu = data;
108 packet_print_error("Error code", pdu->error);
111 static void au_rand(const void *data, uint8_t size)
113 const struct bt_lmp_au_rand *pdu = data;
115 packet_hexdump(pdu->number, 16);
118 static void sres(const void *data, uint8_t size)
120 const struct bt_lmp_sres *pdu = data;
122 packet_hexdump(pdu->response, 4);
125 static void encryption_mode_req(const void *data, uint8_t size)
127 const struct bt_lmp_encryption_mode_req *pdu = data;
132 str = "No encryption";
145 print_field("Mode: %s (%u)", str, pdu->mode);
148 static void encryption_key_size_req(const void *data, uint8_t size)
150 const struct bt_lmp_encryption_key_size_req *pdu = data;
152 print_field("Key size: %u", pdu->key_size);
155 static void start_encryption_req(const void *data, uint8_t size)
157 const struct bt_lmp_start_encryption_req *pdu = data;
159 packet_hexdump(pdu->number, 16);
162 static void stop_encryption_req(const void *data, uint8_t size)
166 static void switch_req(const void *data, uint8_t size)
168 const struct bt_lmp_switch_req *pdu = data;
170 print_field("Instant: 0x%8.8x", le32_to_cpu(pdu->instant));
173 static void unsniff_req(const void *data, uint8_t size)
177 static void max_power(const void *data, uint8_t size)
181 static void min_power(const void *data, uint8_t size)
185 static void auto_rate(const void *data, uint8_t size)
189 static void preferred_rate(const void *data, uint8_t size)
191 const struct bt_lmp_preferred_rate *pdu = data;
194 str = (pdu->rate & 0x01) ? "do not use FEC" : "use FEC";
196 print_field("Basic data rate: %s (0x%02x)", str, pdu->rate & 0x01);
198 switch ((pdu->rate & 0x06) >> 1) {
200 str = "No packet-size preference available";
203 str = "use 1-slot packets";
206 str = "use 3-slot packets";
209 str = "use 5-slot packets";
213 print_field("Basic data rate: %s (0x%02x)", str, pdu->rate & 0x06);
215 switch ((pdu->rate & 0x11) >> 3) {
217 str = "use DM1 packets";
220 str = "use 2 Mb/s packets";
223 str = "use 3 MB/s packets";
230 print_field("Enhanced data rate: %s (0x%2.2x)", str, pdu->rate & 0x11);
232 switch ((pdu->rate & 0x60) >> 5) {
234 str = "No packet-size preference available";
237 str = "use 1-slot packets";
240 str = "use 3-slot packets";
243 str = "use 5-slot packets";
247 print_field("Enhanced data rate: %s (0x%2.2x)", str, pdu->rate & 0x60);
250 static void version_req(const void *data, uint8_t size)
252 const struct bt_lmp_version_req *pdu = data;
254 packet_print_version("Version", pdu->version,
255 "Subversion", le16_to_cpu(pdu->subversion));
256 packet_print_company("Company", le16_to_cpu(pdu->company));
259 static void version_res(const void *data, uint8_t size)
261 const struct bt_lmp_version_res *pdu = data;
263 packet_print_version("Version", pdu->version,
264 "Subversion", le16_to_cpu(pdu->subversion));
265 packet_print_company("Company", le16_to_cpu(pdu->company));
268 static void features_req(const void *data, uint8_t size)
270 const struct bt_lmp_features_req *pdu = data;
272 packet_print_features_lmp(pdu->features, 0x00);
275 static void features_res(const void *data, uint8_t size)
277 const struct bt_lmp_features_res *pdu = data;
279 packet_print_features_lmp(pdu->features, 0x00);
282 static void max_slot(const void *data, uint8_t size)
284 const struct bt_lmp_max_slot *pdu = data;
286 print_field("Slots: 0x%4.4x", pdu->slots);
289 static void max_slot_req(const void *data, uint8_t size)
291 const struct bt_lmp_max_slot_req *pdu = data;
293 print_field("Slots: 0x%4.4x", pdu->slots);
296 static void timing_accuracy_req(const void *data, uint8_t size)
300 static void timing_accuracy_res(const void *data, uint8_t size)
302 const struct bt_lmp_timing_accuracy_res *pdu = data;
304 print_field("Drift: %u ppm", pdu->drift);
305 print_field("Jitter: %u usec", pdu->jitter);
308 static void setup_complete(const void *data, uint8_t size)
312 static void use_semi_permanent_key(const void *data, uint8_t size)
316 static void host_connection_req(const void *data, uint8_t size)
320 static void slot_offset(const void *data, uint8_t size)
322 const struct bt_lmp_slot_offset *pdu = data;
324 print_field("Offset: %u usec", le16_to_cpu(pdu->offset));
325 packet_print_addr("Address", pdu->bdaddr, false);
328 static void page_scan_mode_req(const void *data, uint8_t size)
330 const struct bt_lmp_page_scan_mode_req *pdu = data;
333 switch (pdu->scheme) {
342 print_field("Paging scheme: %s (%u)", str, pdu->scheme);
344 if (pdu->scheme == 0x00) {
345 switch (pdu->settings) {
362 print_field("Paging scheme settings: %s (%u)", str, pdu->settings);
365 static void test_activate(const void *data, uint8_t size)
369 static void encryption_key_size_mask_req(const void *data, uint8_t size)
373 static void set_afh(const void *data, uint8_t size)
375 const struct bt_lmp_set_afh *pdu = data;
378 print_field("Instant: %u", le32_to_cpu(pdu->instant));
392 print_field("Mode: %s (0x%2.2x)", str, pdu->mode);
393 packet_print_channel_map_lmp(pdu->map);
396 static void encapsulated_header(const void *data, uint8_t size)
398 const struct bt_lmp_encapsulated_header *pdu = data;
401 print_field("Major type: %u", pdu->major);
402 print_field("Minor type: %u", pdu->minor);
404 if (pdu->major == 0x01) {
405 switch (pdu->minor) {
407 str = "P-192 Public Key";
410 str = "P-256 Public Key";
417 print_field(" %s", str);
420 print_field("Length: %u", pdu->length);
423 static void encapsulated_payload(const void *data, uint8_t size)
425 const struct bt_lmp_encapsulated_payload *pdu = data;
427 packet_hexdump(pdu->data, 16);
430 static void simple_pairing_confirm(const void *data, uint8_t size)
432 const struct bt_lmp_simple_pairing_confirm *pdu = data;
434 packet_hexdump(pdu->value, 16);
437 static void simple_pairing_number(const void *data, uint8_t size)
439 const struct bt_lmp_simple_pairing_number *pdu = data;
441 packet_hexdump(pdu->value, 16);
444 static void dhkey_check(const void *data, uint8_t size)
446 const struct bt_lmp_dhkey_check *pdu = data;
448 packet_hexdump(pdu->value, 16);
451 static void accepted_ext(const void *data, uint8_t size)
453 const struct bt_lmp_accepted_ext *pdu = data;
456 switch (pdu->escape) {
458 opcode = LMP_ESC4(pdu->opcode);
464 print_opcode(opcode);
467 static void not_accepted_ext(const void *data, uint8_t size)
469 const struct bt_lmp_not_accepted_ext *pdu = data;
472 switch (pdu->escape) {
474 opcode = LMP_ESC4(pdu->opcode);
480 print_opcode(opcode);
481 print_field("Error code: %u", pdu->error);
484 static void features_req_ext(const void *data, uint8_t size)
486 const struct bt_lmp_features_req_ext *pdu = data;
488 print_field("Features page: %u", pdu->page);
489 print_field("Max supported page: %u", pdu->max_page);
490 packet_print_features_lmp(pdu->features, pdu->page);
493 static void features_res_ext(const void *data, uint8_t size)
495 const struct bt_lmp_features_res_ext *pdu = data;
497 print_field("Features page: %u", pdu->page);
498 print_field("Max supported page: %u", pdu->max_page);
499 packet_print_features_lmp(pdu->features, pdu->page);
502 static void packet_type_table_req(const void *data, uint8_t size)
504 const struct bt_lmp_packet_type_table_req *pdu = data;
507 switch (pdu->table) {
519 print_field("Table: %s (0x%2.2x)", str, pdu->table);
522 static void channel_classification_req(const void *data, uint8_t size)
524 const struct bt_lmp_channel_classification_req *pdu = data;
539 print_field("Reporting mode: %s (0x%2.2x)", str, pdu->mode);
540 print_field("Min interval: 0x%2.2x", pdu->min_interval);
541 print_field("Max interval: 0x%2.2x", pdu->max_interval);
544 static void channel_classification(const void *data, uint8_t size)
546 const struct bt_lmp_channel_classification *pdu = data;
550 for (i = 0; i < 10; i++)
551 sprintf(str + (i * 2), "%2.2x", pdu->classification[i]);
553 print_field("Classification: 0x%s", str);
556 static void pause_encryption_req(const void *data, uint8_t size)
560 static void resume_encryption_req(const void *data, uint8_t size)
564 static void io_capability_req(const void *data, uint8_t size)
566 const struct bt_lmp_io_capability_req *pdu = data;
569 packet_print_io_capability(pdu->capability);
571 switch (pdu->oob_data) {
573 str = "No authentication data received";
576 str = "Authentication data received";
583 print_field("OOB data: %s (0x%2.2x)", str, pdu->oob_data);
585 packet_print_io_authentication(pdu->authentication);
588 static void io_capability_res(const void *data, uint8_t size)
590 const struct bt_lmp_io_capability_res *pdu = data;
593 packet_print_io_capability(pdu->capability);
595 switch (pdu->oob_data) {
597 str = "No authentication data received";
600 str = "Authentication data received";
607 print_field("OOB data: %s (0x%2.2x)", str, pdu->oob_data);
609 packet_print_io_authentication(pdu->authentication);
612 static void numeric_comparison_failed(const void *data, uint8_t size)
616 static void passkey_failed(const void *data, uint8_t size)
620 static void oob_failed(const void *data, uint8_t size)
624 static void power_control_req(const void *data, uint8_t size)
626 const struct bt_lmp_power_control_req *pdu = data;
629 switch (pdu->request) {
631 str = "Decrement power one step";
634 str = "Increment power one step";
637 str = "Increase to maximum power";
644 print_field("Request: %s (0x%2.2x)", str, pdu->request);
647 static void power_control_res(const void *data, uint8_t size)
649 const struct bt_lmp_power_control_res *pdu = data;
652 print_field("Response: 0x%2.2x", pdu->response);
654 switch (pdu->response & 0x03) {
656 str = "Not supported";
659 str = "Changed one step";
672 print_field(" GFSK: %s", str);
674 switch ((pdu->response & 0x0c) >> 2) {
676 str = "Not supported";
679 str = "Changed one step";
692 print_field(" DQPSK: %s", str);
694 switch ((pdu->response & 0x30) >> 4) {
696 str = "Not supported";
699 str = "Changed one step";
712 print_field(" 8DPSK: %s", str);
715 static void ping_req(const void *data, uint8_t size)
719 static void ping_res(const void *data, uint8_t size)
726 void (*func) (const void *data, uint8_t size);
731 static const struct lmp_data lmp_table[] = {
732 { 1, "LMP_name_req", name_req, 1, true },
733 { 2, "LMP_name_res", name_rsp, 16, true },
734 { 3, "LMP_accepted", accepted, 1, true },
735 { 4, "LMP_not_accepted", not_accepted, 2, true },
736 { 5, "LMP_clkoffset_req", clkoffset_req, 0, true },
737 { 6, "LMP_clkoffset_res", clkoffset_rsp, 2, true },
738 { 7, "LMP_detach", detach, 1, true },
739 { 8, "LMP_in_rand" },
740 { 9, "LMP_comb_key" },
741 { 10, "LMP_unit_key" },
742 { 11, "LMP_au_rand", au_rand, 16, true },
743 { 12, "LMP_sres", sres, 4, true },
744 { 13, "LMP_temp_rand" },
745 { 14, "LMP_temp_key" },
746 { 15, "LMP_encryption_mode_req", encryption_mode_req, 1, true },
747 { 16, "LMP_encryption_key_size_req", encryption_key_size_req, 1, true },
748 { 17, "LMP_start_encryption_req", start_encryption_req, 16, true },
749 { 18, "LMP_stop_encryption_req", stop_encryption_req, 0, true },
750 { 19, "LMP_switch_req", switch_req, 4, true },
752 { 21, "LMP_hold_req" },
754 { 23, "LMP_sniff_req" },
755 { 24, "LMP_unsniff_req", unsniff_req, 0, true },
756 { 25, "LMP_park_req" },
758 { 27, "LMP_set_broadcast_scan_window" },
759 { 28, "LMP_modify_beacon" },
760 { 29, "LMP_unpark_BD_ADDR_req" },
761 { 30, "LMP_unpark_PM_ADDR_req" },
762 { 31, "LMP_incr_power_req" },
763 { 32, "LMP_decr_power_req" },
764 { 33, "LMP_max_power", max_power, 0, true },
765 { 34, "LMP_min_power", min_power, 0, true },
766 { 35, "LMP_auto_rate", auto_rate, 0, true },
767 { 36, "LMP_preferred_rate", preferred_rate, 1, true },
768 { 37, "LMP_version_req", version_req, 5, true },
769 { 38, "LMP_version_res", version_res, 5, true },
770 { 39, "LMP_features_req", features_req, 8, true },
771 { 40, "LMP_features_res", features_res, 8, true },
772 { 41, "LMP_quality_of_service" },
773 { 42, "LMP_quality_of_service_req" },
774 { 43, "LMP_SCO_link_req" },
775 { 44, "LMP_remove_SCO_link_req" },
776 { 45, "LMP_max_slot", max_slot, 1, true },
777 { 46, "LMP_max_slot_req", max_slot_req, 1, true },
778 { 47, "LMP_timing_accuracy_req", timing_accuracy_req, 0, true },
779 { 48, "LMP_timing_accuracy_res", timing_accuracy_res, 2, true },
780 { 49, "LMP_setup_complete", setup_complete, 0, true },
781 { 50, "LMP_use_semi_permanent_key", use_semi_permanent_key, 0, true },
782 { 51, "LMP_host_connection_req", host_connection_req, 0, true },
783 { 52, "LMP_slot_offset", slot_offset, 8, true },
784 { 53, "LMP_page_mode_req" },
785 { 54, "LMP_page_scan_mode_req", page_scan_mode_req, 2, true },
786 { 55, "LMP_supervision_timeout" },
787 { 56, "LMP_test_activate", test_activate, 0, true },
788 { 57, "LMP_test_control" },
789 { 58, "LMP_encryption_key_size_mask_req", encryption_key_size_mask_req, 0, true },
790 { 59, "LMP_encryption_key_size_mask_res" },
791 { 60, "LMP_set_AFH", set_afh, 15, true },
792 { 61, "LMP_encapsulated_header", encapsulated_header, 3, true },
793 { 62, "LMP_encapsulated_payload", encapsulated_payload, 16, true },
794 { 63, "LMP_simple_pairing_confirm", simple_pairing_confirm, 16, true },
795 { 64, "LMP_simple_pairing_number", simple_pairing_number, 16, true },
796 { 65, "LMP_DHkey_check", dhkey_check, 16, true },
797 { 66, "LMP_pause_encryption_aes_req" },
798 { LMP_ESC4(1), "LMP_accepted_ext", accepted_ext, 2, true },
799 { LMP_ESC4(2), "LMP_not_accepted_ext", not_accepted_ext, 3, true },
800 { LMP_ESC4(3), "LMP_features_req_ext", features_req_ext, 10, true },
801 { LMP_ESC4(4), "LMP_features_res_ext", features_res_ext, 10, true },
802 { LMP_ESC4(5), "LMP_clk_adj" },
803 { LMP_ESC4(6), "LMP_clk_adj_ack" },
804 { LMP_ESC4(7), "LMP_clk_adj_req" },
805 { LMP_ESC4(11), "LMP_packet_type_table_req", packet_type_table_req, 1, true },
806 { LMP_ESC4(12), "LMP_eSCO_link_req" },
807 { LMP_ESC4(13), "LMP_remove_eSCO_link_req" },
808 { LMP_ESC4(16), "LMP_channel_classification_req", channel_classification_req, 5, true },
809 { LMP_ESC4(17), "LMP_channel_classification", channel_classification, 10, true },
810 { LMP_ESC4(21), "LMP_sniff_subrating_req" },
811 { LMP_ESC4(22), "LMP_sniff_subrating_res" },
812 { LMP_ESC4(23), "LMP_pause_encryption_req", pause_encryption_req, 0, true },
813 { LMP_ESC4(24), "LMP_resume_encryption_req", resume_encryption_req, 0, true },
814 { LMP_ESC4(25), "LMP_IO_capability_req", io_capability_req, 3, true },
815 { LMP_ESC4(26), "LMP_IO_capability_res", io_capability_res, 3, true },
816 { LMP_ESC4(27), "LMP_numeric_comparison_failed", numeric_comparison_failed, 0, true },
817 { LMP_ESC4(28), "LMP_passkey_failed", passkey_failed, 0, true },
818 { LMP_ESC4(29), "LMP_oob_failed", oob_failed, 0, true },
819 { LMP_ESC4(30), "LMP_keypress_notification" },
820 { LMP_ESC4(31), "LMP_power_control_req", power_control_req, 1, true },
821 { LMP_ESC4(32), "LMP_power_control_res", power_control_res, 1, true },
822 { LMP_ESC4(33), "LMP_ping_req", ping_req, 0, true },
823 { LMP_ESC4(34), "LMP_ping_res", ping_res, 0, true },
827 static const char *get_opcode_str(uint16_t opcode)
831 for (i = 0; lmp_table[i].str; i++) {
832 if (lmp_table[i].opcode == opcode)
833 return lmp_table[i].str;
839 void lmp_packet(const void *data, uint8_t size, bool padded)
841 const struct lmp_data *lmp_data = NULL;
842 const char *opcode_color, *opcode_str;
848 tid = ((const uint8_t *) data)[0] & 0x01;
849 opcode = (((const uint8_t *) data)[0] & 0xfe) >> 1;
851 tid_str = tid == 0x00 ? "Master" : "Slave";
856 print_text(COLOR_ERROR, "extended opcode too short");
857 packet_hexdump(data, size);
860 opcode = LMP_ESC4(((const uint8_t *) data)[1]);
872 for (i = 0; lmp_table[i].str; i++) {
873 if (lmp_table[i].opcode == opcode) {
874 lmp_data = &lmp_table[i];
881 opcode_color = COLOR_OPCODE;
883 opcode_color = COLOR_OPCODE_UNKNOWN;
884 opcode_str = lmp_data->str;
886 opcode_color = COLOR_OPCODE_UNKNOWN;
887 opcode_str = "Unknown";
891 print_indent(6, opcode_color, "", opcode_str, COLOR_OFF,
892 " (%u/%u) %s transaction (%u)",
893 opcode >> 8, opcode & 0xff, tid_str, tid);
895 print_indent(6, opcode_color, "", opcode_str, COLOR_OFF,
896 " (%u) %s transaction (%d)",
897 opcode, tid_str, tid);
899 if (!lmp_data || !lmp_data->func) {
900 packet_hexdump(data + off, size - off);
904 if (lmp_data->fixed && !padded) {
905 if (size - off != lmp_data->size) {
906 print_text(COLOR_ERROR, "invalid packet size");
907 packet_hexdump(data + off, size - off);
911 if (size - off < lmp_data->size) {
912 print_text(COLOR_ERROR, "too short packet");
913 packet_hexdump(data + off, size - off);
918 lmp_data->func(data + off, size - off);
925 printf("LMP operations with missing decodings:\n");
927 for (i = 0; lmp_table[i].str; i++) {
928 if (lmp_table[i].func)
931 printf("\t%s\n", lmp_table[i].str);