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 <linux/limits.h>
29 #include "lib/bluetooth.h"
32 #include "lib/hci_lib.h"
34 #include "src/shared/util.h"
35 #include "src/shared/queue.h"
36 #include "src/shared/att.h"
37 #include "src/shared/gatt-db.h"
38 #include "src/textfile.h"
39 #include "src/settings.h"
48 struct gatt_db_attribute *attr;
51 void (*func)(const struct l2cap_frame *frame);
54 struct att_conn_data {
56 struct timespec ldb_mtim;
58 struct timespec rdb_mtim;
62 static void print_uuid(const char *label, const void *data, uint16_t size)
65 char uuidstr[MAX_LEN_UUID_STR];
69 str = bt_uuid16_to_str(get_le16(data));
70 print_field("%s: %s (0x%4.4x)", label, str, get_le16(data));
73 str = bt_uuid32_to_str(get_le32(data));
74 print_field("%s: %s (0x%8.8x)", label, str, get_le32(data));
77 sprintf(uuidstr, "%8.8x-%4.4x-%4.4x-%4.4x-%8.8x%4.4x",
78 get_le32(data + 12), get_le16(data + 10),
79 get_le16(data + 8), get_le16(data + 6),
80 get_le32(data + 2), get_le16(data + 0));
81 str = bt_uuidstr_to_str(uuidstr);
82 print_field("%s: %s (%s)", label, str, uuidstr);
85 packet_hexdump(data, size);
90 static void print_handle_range(const char *label, const void *data)
92 print_field("%s: 0x%4.4x-0x%4.4x", label,
93 get_le16(data), get_le16(data + 2));
96 static bool match_read_frame(const void *data, const void *match_data)
98 const struct att_read *read = data;
99 const struct l2cap_frame *frame = match_data;
101 /* Read frame and response frame shall be in the opposite direction to
104 if (read->in == frame->in)
107 return read->chan == frame->chan;
110 static struct att_read *att_get_read(const struct l2cap_frame *frame)
112 struct packet_conn_data *conn;
113 struct att_conn_data *data;
115 conn = packet_get_conn_data(frame->handle);
123 return queue_remove_if(data->reads, match_read_frame, (void *)frame);
126 static void print_value(struct gatt_db_attribute *attr)
129 struct gatt_db_attribute *val;
130 const bt_uuid_t *uuid;
137 uuid = gatt_db_attribute_get_type(attr);
141 /* Skip in case of characteristic declaration since it already prints
142 * the value handle and properties.
144 if (!bt_uuid_cmp(uuid, &chrc))
147 val = gatt_db_attribute_get_value(attr);
148 if (!val || val == attr)
151 uuid = gatt_db_attribute_get_type(val);
155 handle = gatt_db_attribute_get_handle(val);
159 switch (uuid->type) {
161 sprintf(label, "Value Handle: 0x%4.4x Type", handle);
162 print_field("%s: %s (0x%4.4x)", label,
163 bt_uuid16_to_str(uuid->value.u16),
167 sprintf(label, "Value Handle: 0x%4.4x Type", handle);
168 print_uuid(label, &uuid->value.u128, 16);
176 static void print_attribute(struct gatt_db_attribute *attr)
179 const bt_uuid_t *uuid;
182 handle = gatt_db_attribute_get_handle(attr);
186 uuid = gatt_db_attribute_get_type(attr);
190 switch (uuid->type) {
192 sprintf(label, "Handle: 0x%4.4x Type", handle);
193 print_field("%s: %s (0x%4.4x)", label,
194 bt_uuid16_to_str(uuid->value.u16),
199 sprintf(label, "Handle: 0x%4.4x Type", handle);
200 print_uuid(label, &uuid->value.u128, 16);
209 print_field("Handle: 0x%4.4x", handle);
212 static void print_data_list(const char *label, uint8_t length,
213 const struct l2cap_frame *frame)
215 struct att_read *read;
221 read = att_get_read(frame);
223 count = frame->size / length;
225 print_field("%s: %u entr%s", label, count, count == 1 ? "y" : "ies");
227 while (frame->size >= length) {
228 if (!l2cap_frame_print_le16((void *)frame, "Handle"))
231 print_hex_field("Value", frame->data, length - 2);
234 struct l2cap_frame f;
236 l2cap_frame_clone_size(&f, frame, length - 2);
241 if (!l2cap_frame_pull((void *)frame, frame, length - 2))
245 packet_hexdump(frame->data, frame->size);
249 static void print_attribute_info(uint16_t type, const void *data, uint16_t len)
251 const char *str = bt_uuid16_to_str(type);
253 print_field("%s: %s (0x%4.4x)", "Attribute type", str, type);
256 case 0x2800: /* Primary Service */
257 case 0x2801: /* Secondary Service */
258 print_uuid(" UUID", data, len);
260 case 0x2802: /* Include */
262 print_hex_field(" Value", data, len);
265 print_handle_range(" Handle range", data);
266 print_uuid(" UUID", data + 4, len - 4);
268 case 0x2803: /* Characteristic */
270 print_hex_field(" Value", data, len);
273 print_field(" Properties: 0x%2.2x", *((uint8_t *) data));
274 print_field(" Handle: 0x%2.2x", get_le16(data + 1));
275 print_uuid(" UUID", data + 3, len - 3);
278 print_hex_field("Value", data, len);
283 static const char *att_opcode_to_str(uint8_t opcode);
285 static void att_error_response(const struct l2cap_frame *frame)
287 const struct bt_l2cap_att_error_response *pdu = frame->data;
290 switch (pdu->error) {
292 str = "Invalid Handle";
295 str = "Read Not Permitted";
298 str = "Write Not Permitted";
304 str = "Insufficient Authentication";
307 str = "Request Not Supported";
310 str = "Invalid Offset";
313 str = "Insufficient Authorization";
316 str = "Prepare Queue Full";
319 str = "Attribute Not Found";
322 str = "Attribute Not Long";
325 str = "Insufficient Encryption Key Size";
328 str = "Invalid Attribute Value Length";
331 str = "Unlikely Error";
334 str = "Insufficient Encryption";
337 str = "Unsupported Group Type";
340 str = "Insufficient Resources";
343 str = "Database Out of Sync";
346 str = "Value Not Allowed";
349 str = "CCC Improperly Configured";
352 str = "Procedure Already in Progress";
355 str = "Out of Range";
362 print_field("%s (0x%2.2x)", att_opcode_to_str(pdu->request),
364 print_field("Handle: 0x%4.4x", le16_to_cpu(pdu->handle));
365 print_field("Error: %s (0x%2.2x)", str, pdu->error);
367 /* Read/Read By Type/Read By Group Type may create a read object which
368 * needs to be dequeued and freed in case the operation fails.
370 if (pdu->request == 0x08 || pdu->request == 0x0a ||
371 pdu->request == 0x10)
372 free(att_get_read(frame));
375 static const struct bitfield_data chrc_prop_table[] = {
376 { 0, "Broadcast (0x01)" },
377 { 1, "Read (0x02)" },
378 { 2, "Write Without Response (0x04)" },
379 { 3, "Write (0x08)" },
380 { 4, "Notify (0x10)" },
381 { 5, "Indicate (0x20)" },
382 { 6, "Authorize (0x40)" },
383 { 6, "Extended Properties (0x80)" },
387 static void att_conn_data_free(void *data)
389 struct att_conn_data *att_data = data;
391 gatt_db_unref(att_data->rdb);
392 gatt_db_unref(att_data->ldb);
393 queue_destroy(att_data->reads, free);
397 static struct att_conn_data *att_get_conn_data(struct packet_conn_data *conn)
399 struct att_conn_data *data;
409 data = new0(struct att_conn_data, 1);
410 data->rdb = gatt_db_new();
411 data->ldb = gatt_db_new();
413 conn->destroy = att_conn_data_free;
418 static void gatt_load_db(struct gatt_db *db, const char *filename,
419 struct timespec *mtim)
423 if (lstat(filename, &st))
426 if (!gatt_db_isempty(db)) {
427 /* Check if file has been modified since last time */
428 if (st.st_mtim.tv_sec == mtim->tv_sec &&
429 st.st_mtim.tv_nsec == mtim->tv_nsec)
431 /* Clear db before reloading */
437 btd_settings_gatt_db_load(db, filename);
440 static void load_gatt_db(struct packet_conn_data *conn)
442 struct att_conn_data *data = att_get_conn_data(conn);
443 char filename[PATH_MAX];
446 uint8_t id[6], id_type;
448 ba2str((bdaddr_t *)conn->src, local);
450 if (keys_resolve_identity(conn->dst, id, &id_type))
451 ba2str((bdaddr_t *)id, peer);
453 ba2str((bdaddr_t *)conn->dst, peer);
455 create_filename(filename, PATH_MAX, "/%s/attributes", local);
456 gatt_load_db(data->ldb, filename, &data->ldb_mtim);
458 create_filename(filename, PATH_MAX, "/%s/cache/%s", local, peer);
459 gatt_load_db(data->rdb, filename, &data->rdb_mtim);
462 static struct gatt_db *get_db(const struct l2cap_frame *frame, bool rsp)
464 struct packet_conn_data *conn;
465 struct att_conn_data *data;
468 conn = packet_get_conn_data(frame->handle);
472 /* Try loading local and remote gatt_db if not loaded yet */
494 static struct gatt_db_attribute *insert_chrc(const struct l2cap_frame *frame,
496 bt_uuid_t *uuid, uint8_t prop,
501 db = get_db(frame, rsp);
505 return gatt_db_insert_characteristic(db, handle, uuid, 0, prop, NULL,
509 static int bt_uuid_from_data(bt_uuid_t *uuid, const void *data, uint16_t size)
518 return bt_uuid16_create(uuid, get_le16(data));
520 return bt_uuid32_create(uuid, get_le32(data));
522 memcpy(u128.data, data, sizeof(u128.data));
523 return bt_uuid128_create(uuid, u128);
529 static bool svc_read(const struct l2cap_frame *frame, uint16_t *start,
530 uint16_t *end, bt_uuid_t *uuid)
532 if (!l2cap_frame_get_le16((void *)frame, start))
535 if (!l2cap_frame_get_le16((void *)frame, end))
538 return !bt_uuid_from_data(uuid, frame->data, frame->size);
541 static struct gatt_db_attribute *insert_svc(const struct l2cap_frame *frame,
543 bt_uuid_t *uuid, bool primary,
544 bool rsp, uint16_t num_handles)
548 db = get_db(frame, rsp);
552 return gatt_db_insert_service(db, handle, uuid, primary, num_handles);
555 static void pri_svc_read(const struct l2cap_frame *frame)
560 if (!svc_read(frame, &start, &end, &uuid))
563 insert_svc(frame, start, &uuid, true, true, end - start + 1);
566 static void sec_svc_read(const struct l2cap_frame *frame)
571 if (!svc_read(frame, &start, &end, &uuid))
574 insert_svc(frame, start, &uuid, true, false, end - start + 1);
577 static void print_chrc(const struct l2cap_frame *frame)
584 if (!l2cap_frame_get_u8((void *)frame, &prop)) {
585 print_text(COLOR_ERROR, "Property: invalid size");
589 print_field(" Properties: 0x%2.2x", prop);
591 mask = print_bitfield(6, prop, chrc_prop_table);
593 print_text(COLOR_WHITE_BG, " Unknown fields (0x%2.2x)",
596 if (!l2cap_frame_get_le16((void *)frame, &handle)) {
597 print_text(COLOR_ERROR, " Value Handle: invalid size");
601 print_field(" Value Handle: 0x%4.4x", handle);
602 print_uuid(" Value UUID", frame->data, frame->size);
603 bt_uuid_from_data(&uuid, frame->data, frame->size);
605 insert_chrc(frame, handle, &uuid, prop, true);
608 static void chrc_read(const struct l2cap_frame *frame)
613 static const struct bitfield_data ccc_value_table[] = {
614 { 0, "Notification (0x01)" },
615 { 1, "Indication (0x02)" },
619 static void print_ccc_value(const struct l2cap_frame *frame)
624 if (!l2cap_frame_get_u8((void *)frame, &value)) {
625 print_text(COLOR_ERROR, "invalid size");
629 mask = print_bitfield(4, value, ccc_value_table);
631 print_text(COLOR_WHITE_BG, " Unknown fields (0x%2.2x)",
635 static void ccc_read(const struct l2cap_frame *frame)
637 print_ccc_value(frame);
640 static void ccc_write(const struct l2cap_frame *frame)
642 print_ccc_value(frame);
645 static bool print_ase_codec(const struct l2cap_frame *frame)
648 uint16_t codec_cid, codec_vid;
650 if (!l2cap_frame_get_u8((void *)frame, &codec_id)) {
651 print_text(COLOR_ERROR, "Codec: invalid size");
655 packet_print_codec_id(" Codec", codec_id);
657 if (!l2cap_frame_get_le16((void *)frame, &codec_cid)) {
658 print_text(COLOR_ERROR, "Codec Company ID: invalid size");
662 if (!l2cap_frame_get_le16((void *)frame, &codec_vid)) {
663 print_text(COLOR_ERROR, "Codec Vendor ID: invalid size");
667 if (codec_id == 0xff) {
668 print_field(" Codec Company ID: %s (0x%04x)",
669 bt_compidtostr(codec_cid),
671 print_field(" Codec Vendor ID: 0x%04x", codec_vid);
677 static bool print_ase_lv(const struct l2cap_frame *frame, const char *label,
678 struct packet_ltv_decoder *decoder, size_t decoder_len)
680 struct bt_hci_lv_data *lv;
682 lv = l2cap_frame_pull((void *)frame, frame, sizeof(*lv));
684 print_text(COLOR_ERROR, "%s: invalid size", label);
688 if (!l2cap_frame_pull((void *)frame, frame, lv->len)) {
689 print_text(COLOR_ERROR, "%s: invalid size", label);
693 packet_print_ltv(label, lv->data, lv->len, decoder, decoder_len);
698 static bool print_ase_cc(const struct l2cap_frame *frame, const char *label,
699 struct packet_ltv_decoder *decoder, size_t decoder_len)
701 return print_ase_lv(frame, label, decoder, decoder_len);
704 static const struct bitfield_data pac_context_table[] = {
705 { 0, "Unspecified (0x0001)" },
706 { 1, "Conversational (0x0002)" },
707 { 2, "Media (0x0004)" },
708 { 3, "Game (0x0008)" },
709 { 4, "Instructional (0x0010)" },
710 { 5, "Voice Assistants (0x0020)" },
711 { 6, "Live (0x0040)" },
712 { 7, "Sound Effects (0x0080)" },
713 { 8, "Notifications (0x0100)" },
714 { 9, "Ringtone (0x0200)" },
715 { 10, "Alerts (0x0400)" },
716 { 11, "Emergency alarm (0x0800)" },
717 { 12, "RFU (0x1000)" },
718 { 13, "RFU (0x2000)" },
719 { 14, "RFU (0x4000)" },
720 { 15, "RFU (0x8000)" },
724 static void print_context(const struct l2cap_frame *frame, const char *label)
729 if (!l2cap_frame_get_le16((void *)frame, &value)) {
730 print_text(COLOR_ERROR, " value: invalid size");
734 print_field("%s: 0x%4.4x", label, value);
736 mask = print_bitfield(8, value, pac_context_table);
738 print_text(COLOR_WHITE_BG, " Unknown fields (0x%4.4x)",
743 print_hex_field(" Data", frame->data, frame->size);
746 static void ase_decode_preferred_context(const uint8_t *data, uint8_t len)
748 struct l2cap_frame frame;
750 l2cap_frame_init(&frame, 0, 0, 0, 0, 0, 0, data, len);
752 print_context(&frame, " Preferred Context");
755 static void ase_decode_context(const uint8_t *data, uint8_t len)
757 struct l2cap_frame frame;
759 l2cap_frame_init(&frame, 0, 0, 0, 0, 0, 0, data, len);
761 print_context(&frame, " Context");
764 static void ase_decode_program_info(const uint8_t *data, uint8_t len)
766 struct l2cap_frame frame;
769 l2cap_frame_init(&frame, 0, 0, 0, 0, 0, 0, data, len);
771 str = l2cap_frame_pull(&frame, &frame, len);
773 print_text(COLOR_ERROR, " value: invalid size");
777 print_field(" Program Info: %s", str);
781 print_hex_field(" Data", frame.data, frame.size);
784 static void ase_decode_language(const uint8_t *data, uint8_t len)
786 struct l2cap_frame frame;
789 l2cap_frame_init(&frame, 0, 0, 0, 0, 0, 0, data, len);
791 if (!l2cap_frame_get_le24(&frame, &value)) {
792 print_text(COLOR_ERROR, " value: invalid size");
796 print_field(" Language: 0x%6.6x", value);
800 print_hex_field(" Data", frame.data, frame.size);
803 struct packet_ltv_decoder ase_metadata_table[] = {
804 LTV_DEC(0x01, ase_decode_preferred_context),
805 LTV_DEC(0x02, ase_decode_context),
806 LTV_DEC(0x03, ase_decode_program_info),
807 LTV_DEC(0x04, ase_decode_language)
810 static bool print_ase_metadata(const struct l2cap_frame *frame)
812 return print_ase_lv(frame, " Metadata", NULL, 0);
815 static const struct bitfield_data pac_freq_table[] = {
816 { 0, "8 Khz (0x0001)" },
817 { 1, "11.25 Khz (0x0002)" },
818 { 2, "16 Khz (0x0004)" },
819 { 3, "22.05 Khz (0x0008)" },
820 { 4, "24 Khz (0x0010)" },
821 { 5, "32 Khz (0x0020)" },
822 { 6, "44.1 Khz (0x0040)" },
823 { 7, "48 Khz (0x0080)" },
824 { 8, "88.2 Khz (0x0100)" },
825 { 9, "96 Khz (0x0200)" },
826 { 10, "176.4 Khz (0x0400)" },
827 { 11, "192 Khz (0x0800)" },
828 { 12, "384 Khz (0x1000)" },
829 { 13, "RFU (0x2000)" },
830 { 14, "RFU (0x4000)" },
831 { 15, "RFU (0x8000)" },
835 static void pac_decode_freq(const uint8_t *data, uint8_t len)
837 struct l2cap_frame frame;
841 l2cap_frame_init(&frame, 0, 0, 0, 0, 0, 0, data, len);
843 if (!l2cap_frame_get_le16(&frame, &value)) {
844 print_text(COLOR_ERROR, " value: invalid size");
848 print_field(" Sampling Frequencies: 0x%4.4x", value);
850 mask = print_bitfield(8, value, pac_freq_table);
852 print_text(COLOR_WHITE_BG, " Unknown fields (0x%4.4x)",
857 print_hex_field(" Data", frame.data, frame.size);
860 static const struct bitfield_data pac_duration_table[] = {
861 { 0, "7.5 ms (0x01)" },
862 { 1, "10 ms (0x02)" },
865 { 4, "7.5 ms preferred (0x10)" },
866 { 5, "10 ms preferred (0x20)" },
872 static void pac_decode_duration(const uint8_t *data, uint8_t len)
874 struct l2cap_frame frame;
878 l2cap_frame_init(&frame, 0, 0, 0, 0, 0, 0, data, len);
880 if (!l2cap_frame_get_u8(&frame, &value)) {
881 print_text(COLOR_ERROR, " value: invalid size");
885 print_field(" Frame Duration: 0x%4.4x", value);
887 mask = print_bitfield(8, value, pac_duration_table);
889 print_text(COLOR_WHITE_BG, " Unknown fields (0x%2.2x)",
894 print_hex_field(" Data", frame.data, frame.size);
897 static const struct bitfield_data pac_channel_table[] = {
898 { 0, "1 channel (0x01)" },
899 { 1, "2 channels (0x02)" },
900 { 2, "3 channels (0x04)" },
901 { 3, "4 chanenls (0x08)" },
902 { 4, "5 channels (0x10)" },
903 { 5, "6 channels (0x20)" },
904 { 6, "7 channels (0x40)" },
905 { 7, "8 channels (0x80)" },
909 static void pac_decode_channels(const uint8_t *data, uint8_t len)
911 struct l2cap_frame frame;
915 l2cap_frame_init(&frame, 0, 0, 0, 0, 0, 0, data, len);
917 if (!l2cap_frame_get_u8(&frame, &value)) {
918 print_text(COLOR_ERROR, " value: invalid size");
922 print_field(" Audio Channel Count: 0x%2.2x", value);
924 mask = print_bitfield(8, value, pac_channel_table);
926 print_text(COLOR_WHITE_BG, " Unknown fields (0x%2.2x)",
931 print_hex_field(" Data", frame.data, frame.size);
934 static void pac_decode_frame_length(const uint8_t *data, uint8_t len)
936 struct l2cap_frame frame;
939 l2cap_frame_init(&frame, 0, 0, 0, 0, 0, 0, data, len);
941 if (!l2cap_frame_get_le16(&frame, &min)) {
942 print_text(COLOR_ERROR, " min: invalid size");
946 if (!l2cap_frame_get_le16(&frame, &max)) {
947 print_text(COLOR_ERROR, " min: invalid size");
951 print_field(" Frame Length: %u (0x%4.4x) - %u (0x%4.4x)",
956 print_hex_field(" Data", frame.data, frame.size);
959 static void pac_decode_sdu(const uint8_t *data, uint8_t len)
961 struct l2cap_frame frame;
964 l2cap_frame_init(&frame, 0, 0, 0, 0, 0, 0, data, len);
966 if (!l2cap_frame_get_u8(&frame, &value)) {
967 print_text(COLOR_ERROR, " value: invalid size");
971 print_field(" Max SDU: %u (0x%2.2x)", value, value);
975 print_hex_field(" Data", frame.data, frame.size);
978 struct packet_ltv_decoder pac_cap_table[] = {
979 LTV_DEC(0x01, pac_decode_freq),
980 LTV_DEC(0x02, pac_decode_duration),
981 LTV_DEC(0x03, pac_decode_channels),
982 LTV_DEC(0x04, pac_decode_frame_length),
983 LTV_DEC(0x05, pac_decode_sdu)
986 static void print_pac(const struct l2cap_frame *frame)
990 if (!l2cap_frame_get_u8((void *)frame, &num)) {
991 print_text(COLOR_ERROR, "Number of PAC(s): invalid size");
995 print_field(" Number of PAC(s): %u", num);
997 for (i = 0; i < num; i++) {
998 print_field(" PAC #%u:", i);
1000 if (!print_ase_codec(frame))
1003 if (!print_ase_cc(frame, " Codec Specific Capabilities",
1004 pac_cap_table, ARRAY_SIZE(pac_cap_table)))
1007 if (!print_ase_metadata(frame))
1013 print_hex_field(" Data", frame->data, frame->size);
1016 static void pac_read(const struct l2cap_frame *frame)
1021 static void pac_notify(const struct l2cap_frame *frame)
1026 static bool print_prefer_framing(const struct l2cap_frame *frame)
1030 if (!l2cap_frame_get_u8((void *)frame, &framing)) {
1031 print_text(COLOR_ERROR, " Framing: invalid size");
1037 print_field(" Framing: Unframed PDUs supported (0x00)");
1040 print_field(" Framing: Unframed PDUs not supported (0x01)");
1043 print_field(" Framing: Reserved (0x%2.2x)", framing);
1050 static const struct bitfield_data prefer_phy_table[] = {
1051 { 0, "LE 1M PHY preffered (0x01)" },
1052 { 1, "LE 2M PHY preffered (0x02)" },
1053 { 2, "LE Codec PHY preffered (0x04)" },
1057 static bool print_prefer_phy(const struct l2cap_frame *frame)
1061 if (!l2cap_frame_get_u8((void *)frame, &phy)) {
1062 print_text(COLOR_ERROR, "PHY: invalid size");
1066 print_field(" PHY: 0x%2.2x", phy);
1068 mask = print_bitfield(4, phy, prefer_phy_table);
1070 print_text(COLOR_WHITE_BG, " Unknown fields (0x%2.2x)",
1076 static bool print_ase_rtn(const struct l2cap_frame *frame, const char *label)
1080 if (!l2cap_frame_get_u8((void *)frame, &rtn)) {
1081 print_text(COLOR_ERROR, "%s: invalid size", label);
1085 print_field("%s: %u", label, rtn);
1090 static bool print_ase_latency(const struct l2cap_frame *frame,
1095 if (!l2cap_frame_get_le16((void *)frame, &latency)) {
1096 print_text(COLOR_ERROR, "%s: invalid size", label);
1100 print_field("%s: %u", label, latency);
1105 static bool print_ase_pd(const struct l2cap_frame *frame, const char *label)
1109 if (!l2cap_frame_get_le24((void *)frame, &pd)) {
1110 print_text(COLOR_ERROR, "%s: invalid size", label);
1114 print_field("%s: %u us", label, pd);
1119 static void ase_decode_freq(const uint8_t *data, uint8_t len)
1121 struct l2cap_frame frame;
1124 l2cap_frame_init(&frame, 0, 0, 0, 0, 0, 0, data, len);
1126 if (!l2cap_frame_get_u8(&frame, &value)) {
1127 print_text(COLOR_ERROR, " value: invalid size");
1133 print_field(" Sampling Frequency: 8 Khz (0x01)");
1136 print_field(" Sampling Frequency: 11.25 Khz (0x02)");
1139 print_field(" Sampling Frequency: 16 Khz (0x03)");
1142 print_field(" Sampling Frequency: 22.05 Khz (0x04)");
1145 print_field(" Sampling Frequency: 24 Khz (0x05)");
1148 print_field(" Sampling Frequency: 32 Khz (0x06)");
1151 print_field(" Sampling Frequency: 44.1 Khz (0x07)");
1154 print_field(" Sampling Frequency: 48 Khz (0x08)");
1157 print_field(" Sampling Frequency: 88.2 Khz (0x09)");
1160 print_field(" Sampling Frequency: 96 Khz (0x0a)");
1163 print_field(" Sampling Frequency: 176.4 Khz (0x0b)");
1166 print_field(" Sampling Frequency: 192 Khz (0x0c)");
1169 print_field(" Sampling Frequency: 384 Khz (0x0d)");
1172 print_field(" Sampling Frequency: RFU (0x%2.2x)", value);
1178 print_hex_field(" Data", frame.data, frame.size);
1181 static void ase_decode_duration(const uint8_t *data, uint8_t len)
1183 struct l2cap_frame frame;
1186 l2cap_frame_init(&frame, 0, 0, 0, 0, 0, 0, data, len);
1188 if (!l2cap_frame_get_u8(&frame, &value)) {
1189 print_text(COLOR_ERROR, " value: invalid size");
1195 print_field(" Frame Duration: 7.5 ms (0x00)");
1198 print_field(" Frame Duration: 10 ms (0x01)");
1201 print_field(" Frame Duration: RFU (0x%2.2x)", value);
1207 print_hex_field(" Data", frame.data, frame.size);
1210 static const struct bitfield_data channel_location_table[] = {
1211 { 0, "Front Left (0x00000001)" },
1212 { 1, "Front Right (0x00000002)" },
1213 { 2, "Front Center (0x00000004)" },
1214 { 3, "Low Frequency Effects 1 (0x00000008)" },
1215 { 4, "Back Left (0x00000010)" },
1216 { 5, "Back Right (0x00000020)" },
1217 { 6, "Front Left of Center (0x00000040)" },
1218 { 7, "Front Right of Center (0x00000080)" },
1219 { 8, "Back Center (0x00000100)" },
1220 { 9, "Low Frequency Effects 2 (0x00000200)" },
1221 { 10, "Side Left (0x00000400)" },
1222 { 11, "Side Right (0x00000800)" },
1223 { 12, "Top Front Left (0x00001000)" },
1224 { 13, "Top Front Right (0x00002000)" },
1225 { 14, "Top Front Center (0x00004000)" },
1226 { 15, "Top Center (0x00008000)" },
1227 { 16, "Top Back Left (0x00010000)" },
1228 { 17, "Top Back Right (0x00020000)" },
1229 { 18, "Top Side Left (0x00040000)" },
1230 { 19, "Top Side Right (0x00080000)" },
1231 { 20, "Top Back Center (0x00100000)" },
1232 { 21, "Bottom Front Center (0x00200000)" },
1233 { 22, "Bottom Front Left (0x00400000)" },
1234 { 23, "Bottom Front Right (0x00800000)" },
1235 { 24, "Front Left Wide (0x01000000)" },
1236 { 25, "Front Right Wide (0x02000000)" },
1237 { 26, "Left Surround (0x04000000)" },
1238 { 27, "Right Surround (0x08000000)" },
1239 { 28, "RFU (0x10000000)" },
1240 { 29, "RFU (0x20000000)" },
1241 { 30, "RFU (0x40000000)" },
1242 { 31, "RFU (0x80000000)" },
1246 static void print_location(const struct l2cap_frame *frame)
1251 if (!l2cap_frame_get_le32((void *)frame, &value)) {
1252 print_text(COLOR_ERROR, " value: invalid size");
1256 print_field(" Location: 0x%8.8x", value);
1258 mask = print_bitfield(6, value, channel_location_table);
1260 print_text(COLOR_WHITE_BG, " Unknown fields (0x%8.8x)",
1265 print_hex_field(" Data", frame->data, frame->size);
1268 static void ase_decode_location(const uint8_t *data, uint8_t len)
1270 struct l2cap_frame frame;
1272 l2cap_frame_init(&frame, 0, 0, 0, 0, 0, 0, data, len);
1274 print_location(&frame);
1277 static void ase_decode_frame_length(const uint8_t *data, uint8_t len)
1279 struct l2cap_frame frame;
1282 l2cap_frame_init(&frame, 0, 0, 0, 0, 0, 0, data, len);
1284 if (!l2cap_frame_get_le16(&frame, &value)) {
1285 print_text(COLOR_ERROR, " value: invalid size");
1289 print_field(" Frame Length: %u (0x%4.4x)", value, value);
1293 print_hex_field(" Data", frame.data, frame.size);
1296 static void ase_decode_blocks(const uint8_t *data, uint8_t len)
1298 struct l2cap_frame frame;
1301 l2cap_frame_init(&frame, 0, 0, 0, 0, 0, 0, data, len);
1303 if (!l2cap_frame_get_u8(&frame, &value)) {
1304 print_text(COLOR_ERROR, " value: invalid size");
1308 print_field(" Frame Blocks per SDU: %u (0x%2.2x)", value, value);
1312 print_hex_field(" Data", frame.data, frame.size);
1315 struct packet_ltv_decoder ase_cc_table[] = {
1316 LTV_DEC(0x01, ase_decode_freq),
1317 LTV_DEC(0x02, ase_decode_duration),
1318 LTV_DEC(0x03, ase_decode_location),
1319 LTV_DEC(0x04, ase_decode_frame_length),
1320 LTV_DEC(0x05, ase_decode_blocks)
1323 static void print_ase_config(const struct l2cap_frame *frame)
1325 if (!print_prefer_framing(frame))
1328 if (!print_prefer_phy(frame))
1331 if (!print_ase_rtn(frame, " RTN"))
1334 if (!print_ase_latency(frame, " Max Transport Latency"))
1337 if (!print_ase_pd(frame, " Presentation Delay Min"))
1340 if (!print_ase_pd(frame, " Presentation Delay Max"))
1343 if (!print_ase_pd(frame, " Preferred Presentation Delay Min"))
1346 if (!print_ase_pd(frame, " Preferred Presentation Delay Max"))
1349 if (!print_ase_codec(frame))
1352 print_ase_cc(frame, " Codec Specific Configuration",
1353 ase_cc_table, ARRAY_SIZE(ase_cc_table));
1356 static bool print_ase_framing(const struct l2cap_frame *frame,
1361 if (!l2cap_frame_get_u8((void *)frame, &framing)) {
1362 print_text(COLOR_ERROR, "%s: invalid size", label);
1368 print_field("%s: Unframed (0x00)", label);
1371 print_field("%s: Framed (0x01)", label);
1374 print_field("%s: Reserved (0x%2.2x)", label, framing);
1380 static const struct bitfield_data phy_table[] = {
1381 { 0, "LE 1M PHY (0x01)" },
1382 { 1, "LE 2M PHY (0x02)" },
1383 { 2, "LE Codec PHY (0x04)" },
1387 static bool print_ase_phy(const struct l2cap_frame *frame, const char *label)
1391 if (!l2cap_frame_get_u8((void *)frame, &phy)) {
1392 print_text(COLOR_ERROR, "%s: invalid size", label);
1396 print_field("%s: 0x%2.2x", label, phy);
1398 mask = print_bitfield(4, phy, phy_table);
1400 print_text(COLOR_WHITE_BG, " Unknown fields (0x%2.2x)",
1406 static bool print_ase_interval(const struct l2cap_frame *frame,
1411 if (!l2cap_frame_get_le24((void *)frame, &interval)) {
1412 print_text(COLOR_ERROR, "%s: invalid size", label);
1416 print_field("%s: %u usec", label, interval);
1421 static bool print_ase_sdu(const struct l2cap_frame *frame, const char *label)
1425 if (!l2cap_frame_get_le16((void *)frame, &sdu)) {
1426 print_text(COLOR_ERROR, "%s: invalid size", label);
1430 print_field("%s: %u", label, sdu);
1435 static void print_ase_qos(const struct l2cap_frame *frame)
1437 if (!l2cap_frame_print_u8((void *)frame, " CIG ID"))
1440 if (!l2cap_frame_print_u8((void *)frame, " CIS ID"))
1443 if (!print_ase_interval(frame, " SDU Interval"))
1446 if (!print_ase_framing(frame, " Framing"))
1449 if (!print_ase_phy(frame, " PHY"))
1452 if (!print_ase_sdu(frame, " Max SDU"))
1455 if (!print_ase_rtn(frame, " RTN"))
1458 if (!print_ase_latency(frame, " Max Transport Latency"))
1461 print_ase_pd(frame, " Presentation Delay");
1464 static void print_ase_metadata_status(const struct l2cap_frame *frame)
1466 if (!l2cap_frame_print_u8((void *)frame, " CIG ID"))
1469 if (!l2cap_frame_print_u8((void *)frame, " CIS ID"))
1472 print_ase_metadata(frame);
1475 static void print_ase_status(const struct l2cap_frame *frame)
1479 if (!l2cap_frame_get_u8((void *)frame, &id)) {
1480 print_text(COLOR_ERROR, "ASE ID: invalid size");
1484 print_field(" ASE ID: %u", id);
1486 if (!l2cap_frame_get_u8((void *)frame, &state)) {
1487 print_text(COLOR_ERROR, "ASE State: invalid size");
1492 /* ASE_State = 0x00 (Idle) */
1494 print_field(" State: Idle (0x00)");
1496 /* ASE_State = 0x01 (Codec Configured) */
1498 print_field(" State: Codec Configured (0x01)");
1499 print_ase_config(frame);
1501 /* ASE_State = 0x02 (QoS Configured) */
1503 print_field(" State: QoS Configured (0x02)");
1504 print_ase_qos(frame);
1506 /* ASE_Status = 0x03 (Enabling) */
1508 print_field(" State: Enabling (0x03)");
1509 print_ase_metadata_status(frame);
1511 /* ASE_Status = 0x04 (Streaming) */
1513 print_field(" State: Streaming (0x04)");
1514 print_ase_metadata_status(frame);
1516 /* ASE_Status = 0x05 (Disabling) */
1518 print_field(" State: Disabling (0x05)");
1519 print_ase_metadata_status(frame);
1521 /* ASE_Status = 0x06 (Releasing) */
1523 print_field(" State: Releasing (0x06)");
1526 print_field(" State: Reserved (0x%2.2x)", state);
1532 print_hex_field(" Data", frame->data, frame->size);
1535 static void ase_read(const struct l2cap_frame *frame)
1537 print_ase_status(frame);
1540 static void ase_notify(const struct l2cap_frame *frame)
1542 print_ase_status(frame);
1545 static bool print_ase_target_latency(const struct l2cap_frame *frame)
1549 if (!l2cap_frame_get_u8((void *)frame, &latency)) {
1550 print_text(COLOR_ERROR, " Target Latency: invalid size");
1556 print_field(" Target Latency: Low Latency (0x01)");
1559 print_field(" Target Latency: Balance Latency/Reliability "
1563 print_field(" Target Latency: High Reliability (0x03)");
1566 print_field(" Target Latency: Reserved (0x%2.2x)", latency);
1573 static bool ase_config_cmd(const struct l2cap_frame *frame)
1575 if (!l2cap_frame_print_u8((void *)frame, " ASE ID"))
1578 if (!print_ase_target_latency(frame))
1581 if (!print_ase_phy(frame, " PHY"))
1584 if (!print_ase_codec(frame))
1587 if (!print_ase_cc(frame, " Codec Specific Configuration",
1588 ase_cc_table, ARRAY_SIZE(ase_cc_table)))
1594 static bool ase_qos_cmd(const struct l2cap_frame *frame)
1596 if (!l2cap_frame_print_u8((void *)frame, " ASE ID"))
1599 if (!l2cap_frame_print_u8((void *)frame, " CIG ID"))
1602 if (!l2cap_frame_print_u8((void *)frame, " CIS ID"))
1605 if (!print_ase_interval(frame, " SDU Interval"))
1608 if (!print_ase_framing(frame, " Framing"))
1611 if (!print_ase_phy(frame, " PHY"))
1614 if (!print_ase_sdu(frame, " Max SDU"))
1617 if (!print_ase_rtn(frame, " RTN"))
1620 if (!print_ase_latency(frame, " Max Transport Latency"))
1623 if (!print_ase_pd(frame, " Presentation Delay"))
1629 static bool ase_enable_cmd(const struct l2cap_frame *frame)
1631 if (!l2cap_frame_print_u8((void *)frame, " ASE ID"))
1634 if (!print_ase_metadata(frame))
1640 static bool ase_start_cmd(const struct l2cap_frame *frame)
1642 if (!l2cap_frame_print_u8((void *)frame, " ASE ID"))
1648 static bool ase_disable_cmd(const struct l2cap_frame *frame)
1650 if (!l2cap_frame_print_u8((void *)frame, " ASE ID"))
1656 static bool ase_stop_cmd(const struct l2cap_frame *frame)
1658 if (!l2cap_frame_print_u8((void *)frame, " ASE ID"))
1664 static bool ase_metadata_cmd(const struct l2cap_frame *frame)
1666 if (!l2cap_frame_print_u8((void *)frame, " ASE ID"))
1669 if (!print_ase_metadata(frame))
1675 static bool ase_release_cmd(const struct l2cap_frame *frame)
1677 if (!l2cap_frame_print_u8((void *)frame, " ASE ID"))
1683 #define ASE_CMD(_op, _desc, _func) \
1691 bool (*func)(const struct l2cap_frame *frame);
1692 } ase_cmd_table[] = {
1693 /* Opcode = 0x01 (Codec Configuration) */
1694 ASE_CMD(0x01, "Codec Configuration", ase_config_cmd),
1695 /* Opcode = 0x02 (QoS Configuration) */
1696 ASE_CMD(0x02, "QoS Configuration", ase_qos_cmd),
1697 /* Opcode = 0x03 (Enable) */
1698 ASE_CMD(0x03, "Enable", ase_enable_cmd),
1699 /* Opcode = 0x04 (Receiver Start Ready) */
1700 ASE_CMD(0x04, "Receiver Start Ready", ase_start_cmd),
1701 /* Opcode = 0x05 (Disable) */
1702 ASE_CMD(0x05, "Disable", ase_disable_cmd),
1703 /* Opcode = 0x06 (Receiver Stop Ready) */
1704 ASE_CMD(0x06, "Receiver Stop Ready", ase_stop_cmd),
1705 /* Opcode = 0x07 (Update Metadata) */
1706 ASE_CMD(0x07, "Update Metadata", ase_metadata_cmd),
1707 /* Opcode = 0x08 (Release) */
1708 ASE_CMD(0x08, "Release", ase_release_cmd),
1711 static struct ase_cmd *ase_get_cmd(uint8_t op)
1713 if (op > ARRAY_SIZE(ase_cmd_table))
1716 return &ase_cmd_table[op];
1719 static void print_ase_cmd(const struct l2cap_frame *frame)
1722 struct ase_cmd *cmd;
1724 if (!l2cap_frame_get_u8((void *)frame, &op)) {
1725 print_text(COLOR_ERROR, "opcode: invalid size");
1729 if (!l2cap_frame_get_u8((void *)frame, &num)) {
1730 print_text(COLOR_ERROR, "num: invalid size");
1734 cmd = ase_get_cmd(op);
1736 print_field(" Opcode: Reserved (0x%2.2x)", op);
1740 print_field(" Opcode: %s (0x%2.2x)", cmd->desc, op);
1741 print_field(" Number of ASE(s): %u", num);
1743 for (i = 0; i < num && frame->size; i++) {
1744 print_field(" ASE: #%u", i);
1746 if (!cmd->func(frame))
1752 print_hex_field(" Data", frame->data, frame->size);
1755 static void ase_cp_write(const struct l2cap_frame *frame)
1757 print_ase_cmd(frame);
1760 static bool print_ase_cp_rsp_code(const struct l2cap_frame *frame)
1764 if (!l2cap_frame_get_u8((void *)frame, &code)) {
1765 print_text(COLOR_ERROR, " ASE Response Code: invalid size");
1771 print_field(" ASE Response Code: Success (0x00)");
1774 print_field(" ASE Response Code: Unsupported Opcode (0x01)");
1777 print_field(" ASE Response Code: Invalid Length (0x02)");
1780 print_field(" ASE Response Code: Invalid ASE ID (0x03)");
1783 print_field(" ASE Response Code: Invalid ASE State (0x04)");
1786 print_field(" ASE Response Code: Invalid ASE Direction "
1790 print_field(" ASE Response Code: Unsupported Audio "
1791 "Capabilities (0x06)");
1794 print_field(" ASE Response Code: Unsupported Configuration "
1798 print_field(" ASE Response Code: Rejected Configuration "
1802 print_field(" ASE Response Code: Invalid Configuration "
1806 print_field(" ASE Response Code: Unsupported Metadata "
1810 print_field(" ASE Response Code: Rejected Metadata (0x0b)");
1813 print_field(" ASE Response Code: Invalid Metadata (0x0c)");
1816 print_field(" ASE Response Code: Insufficient Resources "
1820 print_field(" ASE Response Code: Unspecified Error (0x0e)");
1823 print_field(" ASE Response Code: Reserved (0x%2.2x)", code);
1830 static bool print_ase_cp_rsp_reason(const struct l2cap_frame *frame)
1834 if (!l2cap_frame_get_u8((void *)frame, &reason)) {
1835 print_text(COLOR_ERROR,
1836 " ASE Response Reason: invalid size");
1842 print_field(" ASE Response Reason: None (0x00)");
1845 print_field(" ASE Response Reason: ASE ID (0x01)");
1848 print_field(" ASE Response Reason: Codec Specific "
1849 "Configuration (0x02)");
1852 print_field(" ASE Response Reason: SDU Interval (0x03)");
1855 print_field(" ASE Response Reason: Framing (0x04)");
1858 print_field(" ASE Response Reason: PHY (0x05)");
1861 print_field(" ASE Response Reason: Max SDU (0x06)");
1864 print_field(" ASE Response Reason: RTN (0x07)");
1867 print_field(" ASE Response Reason: Max Transport Latency "
1871 print_field(" ASE Response Reason: Presentation Delay "
1875 print_field(" ASE Response Reason: Invalid ASE/CIS Mapping "
1879 print_field(" ASE Response Reason: Reserved (0x%2.2x)",
1887 static void print_ase_cp_rsp(const struct l2cap_frame *frame)
1890 struct ase_cmd *cmd;
1892 if (!l2cap_frame_get_u8((void *)frame, &op)) {
1893 print_text(COLOR_ERROR, " opcode: invalid size");
1897 if (!l2cap_frame_get_u8((void *)frame, &num)) {
1898 print_text(COLOR_ERROR, " Number of ASE(s): invalid size");
1902 cmd = ase_get_cmd(op);
1904 print_field(" Opcode: Reserved (0x%2.2x)", op);
1908 print_field(" Opcode: %s (0x%2.2x)", cmd->desc, op);
1909 print_field(" Number of ASE(s): %u", num);
1911 for (i = 0; i < num && frame->size; i++) {
1912 print_field(" ASE: #%u", i);
1914 if (!l2cap_frame_print_u8((void *)frame, " ASE ID"))
1917 if (!print_ase_cp_rsp_code(frame))
1920 if (!print_ase_cp_rsp_reason(frame))
1926 print_hex_field(" Data", frame->data, frame->size);
1929 static void ase_cp_notify(const struct l2cap_frame *frame)
1931 print_ase_cp_rsp(frame);
1934 static void pac_loc_read(const struct l2cap_frame *frame)
1936 print_location(frame);
1939 static void pac_loc_notify(const struct l2cap_frame *frame)
1941 print_location(frame);
1944 static void print_pac_context(const struct l2cap_frame *frame)
1949 if (!l2cap_frame_get_le16((void *)frame, &snk)) {
1950 print_text(COLOR_ERROR, " sink: invalid size");
1954 print_field(" Sink Context: 0x%4.4x", snk);
1956 mask = print_bitfield(4, snk, pac_context_table);
1958 print_text(COLOR_WHITE_BG, " Unknown fields (0x%4.4x)",
1961 if (!l2cap_frame_get_le16((void *)frame, &src)) {
1962 print_text(COLOR_ERROR, " source: invalid size");
1966 print_field(" Source Context: 0x%4.4x", src);
1968 mask = print_bitfield(4, src, pac_context_table);
1970 print_text(COLOR_WHITE_BG, " Unknown fields (0x%4.4x)",
1975 print_hex_field(" Data", frame->data, frame->size);
1978 static void pac_context_read(const struct l2cap_frame *frame)
1980 print_pac_context(frame);
1983 static void pac_context_notify(const struct l2cap_frame *frame)
1985 print_pac_context(frame);
1988 static void csip_rank_read(const struct l2cap_frame *frame)
1992 if (!l2cap_frame_get_u8((void *)frame, &rank)) {
1993 print_text(COLOR_ERROR, "Rank: invalid size");
1997 print_field(" Rank: 0x%02x", rank);
2001 print_hex_field(" Data", frame->data, frame->size);
2004 static void csip_lock_read(const struct l2cap_frame *frame)
2008 if (!l2cap_frame_get_u8((void *)frame, &lock)) {
2009 print_text(COLOR_ERROR, "Lock: invalid size");
2015 print_field(" Unlocked (0x%02x)", lock);
2018 print_field(" Locked (0x%02x)", lock);
2021 print_field(" RFU (0x%02x)", lock);
2027 print_hex_field(" Data", frame->data, frame->size);
2030 static void print_csip_size(const struct l2cap_frame *frame)
2034 if (!l2cap_frame_get_u8((void *)frame, &size)) {
2035 print_text(COLOR_ERROR, "Size: invalid size");
2038 print_field(" Size: 0x%02x", size);
2042 print_hex_field(" Data", frame->data, frame->size);
2045 static void csip_size_read(const struct l2cap_frame *frame)
2047 print_csip_size(frame);
2050 static void csip_size_notify(const struct l2cap_frame *frame)
2052 print_csip_size(frame);
2055 static void csip_sirk_read(const struct l2cap_frame *frame)
2058 print_hex_field(" SIRK", frame->data, frame->size);
2061 static void csip_sirk_notify(const struct l2cap_frame *frame)
2064 print_hex_field(" SIRK", frame->data, frame->size);
2067 static void print_vcs_state(const struct l2cap_frame *frame)
2069 uint8_t vol_set, mute, chng_ctr;
2071 if (!l2cap_frame_get_u8((void *)frame, &vol_set)) {
2072 print_text(COLOR_ERROR, "Volume Settings: invalid size");
2075 print_field(" Volume Setting: %u", vol_set);
2077 if (!l2cap_frame_get_u8((void *)frame, &mute)) {
2078 print_text(COLOR_ERROR, "Mute Filed: invalid size");
2084 print_field(" Not Muted: %u", mute);
2087 print_field(" Muted: %u", mute);
2090 print_field(" Unknown Mute Value: %u", mute);
2094 if (!l2cap_frame_get_u8((void *)frame, &chng_ctr)) {
2095 print_text(COLOR_ERROR, "Change Counter: invalid size");
2098 print_field(" Change Counter: %u", chng_ctr);
2102 print_hex_field(" Data", frame->data, frame->size);
2105 static void vol_state_read(const struct l2cap_frame *frame)
2107 print_vcs_state(frame);
2110 static void vol_state_notify(const struct l2cap_frame *frame)
2112 print_vcs_state(frame);
2115 static bool vcs_config_cmd(const struct l2cap_frame *frame)
2117 if (!l2cap_frame_print_u8((void *)frame, " Change Counter"))
2123 static bool vcs_absolute_cmd(const struct l2cap_frame *frame)
2125 if (!l2cap_frame_print_u8((void *)frame, " Change Counter"))
2128 if (!l2cap_frame_print_u8((void *)frame, " Volume Setting"))
2134 #define VCS_CMD(_op, _desc, _func) \
2142 bool (*func)(const struct l2cap_frame *frame);
2143 } vcs_cmd_table[] = {
2144 /* Opcode = 0x00 (Relative Volume Down) */
2145 VCS_CMD(0x00, "Relative Volume Down", vcs_config_cmd),
2146 /* Opcode = 0x01 (Relative Volume Up) */
2147 VCS_CMD(0x01, "Relative Volume Up", vcs_config_cmd),
2148 /* Opcode = 0x02 (Unmute/Relative Volume Down) */
2149 VCS_CMD(0x02, "Unmute/Relative Volume Down", vcs_config_cmd),
2150 /* Opcode = 0x03 (Unmute/Relative Volume Up) */
2151 VCS_CMD(0x03, "Unmute/Relative Volume Up", vcs_config_cmd),
2152 /* Opcode = 0x04 (Set Absolute Volume) */
2153 VCS_CMD(0x04, "Set Absolute Volume", vcs_absolute_cmd),
2154 /* Opcode = 0x05 (Unmute) */
2155 VCS_CMD(0x05, "Unmute", vcs_config_cmd),
2156 /* Opcode = 0x06 (Mute) */
2157 VCS_CMD(0x06, "Mute", vcs_config_cmd),
2160 static struct vcs_cmd *vcs_get_cmd(uint8_t op)
2162 if (op > ARRAY_SIZE(vcs_cmd_table))
2165 return &vcs_cmd_table[op];
2168 static void print_vcs_cmd(const struct l2cap_frame *frame)
2171 struct vcs_cmd *cmd;
2173 if (!l2cap_frame_get_u8((void *)frame, &op)) {
2174 print_text(COLOR_ERROR, "opcode: invalid size");
2178 cmd = vcs_get_cmd(op);
2180 print_field(" Opcode: Reserved (0x%2.2x)", op);
2184 print_field(" Opcode: %s (0x%2.2x)", cmd->desc, op);
2185 if (!cmd->func(frame))
2186 print_field(" Unknown Opcode");
2190 print_hex_field(" Data", frame->data, frame->size);
2193 static void vol_cp_write(const struct l2cap_frame *frame)
2195 print_vcs_cmd(frame);
2198 static void print_vcs_flag(const struct l2cap_frame *frame)
2202 if (!l2cap_frame_get_u8((void *)frame, &vol_flag)) {
2203 print_text(COLOR_ERROR, "Volume Flag: invalid size");
2206 print_field(" Volume Falg: %u", vol_flag);
2210 print_hex_field(" Data", frame->data, frame->size);
2213 static void vol_flag_read(const struct l2cap_frame *frame)
2215 print_vcs_flag(frame);
2218 static void vol_flag_notify(const struct l2cap_frame *frame)
2220 print_vcs_flag(frame);
2223 static char *name2utf8(const uint8_t *name, uint16_t len)
2225 char utf8_name[HCI_MAX_NAME_LENGTH + 2];
2228 if (g_utf8_validate((const char *) name, len, NULL))
2229 return g_strndup((char *) name, len);
2231 len = MIN(len, sizeof(utf8_name) - 1);
2233 memset(utf8_name, 0, sizeof(utf8_name));
2234 strncpy(utf8_name, (char *) name, len);
2236 /* Assume ASCII, and replace all non-ASCII with spaces */
2237 for (i = 0; utf8_name[i] != '\0'; i++) {
2238 if (!isascii(utf8_name[i]))
2242 /* Remove leading and trailing whitespace characters */
2243 g_strstrip(utf8_name);
2245 return g_strdup(utf8_name);
2248 static void print_mp_name(const struct l2cap_frame *frame)
2252 name = name2utf8((uint8_t *)frame->data, frame->size);
2254 print_field(" Media Player Name: %s", name);
2257 static void mp_name_read(const struct l2cap_frame *frame)
2259 print_mp_name(frame);
2262 static void mp_name_notify(const struct l2cap_frame *frame)
2264 print_mp_name(frame);
2267 static void print_track_changed(const struct l2cap_frame *frame)
2269 print_field(" Track Changed");
2272 static void track_changed_notify(const struct l2cap_frame *frame)
2274 print_track_changed(frame);
2277 static void print_track_title(const struct l2cap_frame *frame)
2281 name = name2utf8((uint8_t *)frame->data, frame->size);
2283 print_field(" Track Title: %s", name);
2286 static void track_title_read(const struct l2cap_frame *frame)
2288 print_track_title(frame);
2291 static void track_title_notify(const struct l2cap_frame *frame)
2293 print_track_title(frame);
2296 static void print_track_duration(const struct l2cap_frame *frame)
2300 if (!l2cap_frame_get_le32((void *)frame, (uint32_t *)&duration)) {
2301 print_text(COLOR_ERROR, " Track Duration: invalid size");
2305 print_field(" Track Duration: %u", duration);
2309 print_hex_field(" Data", frame->data, frame->size);
2312 static void track_duration_read(const struct l2cap_frame *frame)
2314 print_track_duration(frame);
2317 static void track_duration_notify(const struct l2cap_frame *frame)
2319 print_track_duration(frame);
2322 static void print_track_position(const struct l2cap_frame *frame)
2326 if (!l2cap_frame_get_le32((void *)frame, (uint32_t *)&position)) {
2327 print_text(COLOR_ERROR, " Track Position: invalid size");
2331 print_field(" Track Position: %u", position);
2335 print_hex_field(" Data", frame->data, frame->size);
2338 static void track_position_read(const struct l2cap_frame *frame)
2340 print_track_position(frame);
2343 static void track_position_write(const struct l2cap_frame *frame)
2345 print_track_position(frame);
2348 static void track_position_notify(const struct l2cap_frame *frame)
2350 print_track_position(frame);
2353 static void print_playback_speed(const struct l2cap_frame *frame)
2355 int8_t playback_speed;
2357 if (!l2cap_frame_get_u8((void *)frame, (uint8_t *)&playback_speed)) {
2358 print_text(COLOR_ERROR, " Playback Speed: invalid size");
2362 print_field(" Playback Speed: %u", playback_speed);
2366 print_hex_field(" Data", frame->data, frame->size);
2369 static void playback_speed_read(const struct l2cap_frame *frame)
2371 print_playback_speed(frame);
2374 static void playback_speed_write(const struct l2cap_frame *frame)
2376 print_playback_speed(frame);
2379 static void playback_speed_notify(const struct l2cap_frame *frame)
2381 print_playback_speed(frame);
2384 static void print_seeking_speed(const struct l2cap_frame *frame)
2386 int8_t seeking_speed;
2388 if (!l2cap_frame_get_u8((void *)frame, (uint8_t *)&seeking_speed)) {
2389 print_text(COLOR_ERROR, " Seeking Speed: invalid size");
2393 print_field(" Seeking Speed: %u", seeking_speed);
2397 print_hex_field(" Data", frame->data, frame->size);
2400 static void seeking_speed_read(const struct l2cap_frame *frame)
2402 print_seeking_speed(frame);
2405 static void seeking_speed_notify(const struct l2cap_frame *frame)
2407 print_seeking_speed(frame);
2410 static const char *play_order_str(uint8_t order)
2414 return "Single once";
2416 return "Single repeat";
2418 return "In order once";
2420 return "In order repeat";
2422 return "Oldest once";
2424 return "Oldest repeat";
2426 return "Newest once";
2428 return "Newest repeat";
2430 return "Shuffle once";
2432 return "Shuffle repeat";
2438 static void print_playing_order(const struct l2cap_frame *frame)
2440 int8_t playing_order;
2442 if (!l2cap_frame_get_u8((void *)frame, (uint8_t *)&playing_order)) {
2443 print_text(COLOR_ERROR, " Playing Order: invalid size");
2447 print_field(" Playing Order: %s", play_order_str(playing_order));
2451 print_hex_field(" Data", frame->data, frame->size);
2454 static void playing_order_read(const struct l2cap_frame *frame)
2456 print_playing_order(frame);
2459 static void playing_order_write(const struct l2cap_frame *frame)
2461 print_playing_order(frame);
2464 static void playing_order_notify(const struct l2cap_frame *frame)
2466 print_playing_order(frame);
2469 static const struct bitfield_data playing_orders_table[] = {
2470 { 0, "Single once (0x0001)" },
2471 { 1, "Single repeat (0x0002)" },
2472 { 2, "In order once (0x0004)" },
2473 { 3, "In Order Repeat (0x0008)" },
2474 { 4, "Oldest once (0x0010)" },
2475 { 5, "Oldest repeat (0x0020)" },
2476 { 6, "Newest once (0x0040)" },
2477 { 7, "Newest repeat (0x0080)" },
2478 { 8, "Shuffle once (0x0100)" },
2479 { 9, "Shuffle repeat (0x0200)" },
2480 { 10, "RFU (0x0400)" },
2481 { 11, "RFU (0x0800)" },
2482 { 12, "RFU (0x1000)" },
2483 { 13, "RFU (0x2000)" },
2484 { 14, "RFU (0x4000)" },
2485 { 15, "RFU (0x8000)" },
2489 static void print_playing_orders_supported(const struct l2cap_frame *frame)
2491 uint16_t supported_orders;
2494 if (!l2cap_frame_get_le16((void *)frame, &supported_orders)) {
2495 print_text(COLOR_ERROR,
2496 " Supported Playing Orders: invalid size");
2500 print_field(" Supported Playing Orders: 0x%4.4x",
2503 mask = print_bitfield(8, supported_orders, playing_orders_table);
2505 print_text(COLOR_WHITE_BG, " Unknown fields (0x%4.4x)",
2510 print_hex_field(" Data", frame->data, frame->size);
2513 static void playing_orders_supported_read(const struct l2cap_frame *frame)
2515 print_playing_orders_supported(frame);
2518 static const char *media_state_str(uint8_t state)
2534 static void print_media_state(const struct l2cap_frame *frame)
2538 if (!l2cap_frame_get_u8((void *)frame, (uint8_t *)&state)) {
2539 print_text(COLOR_ERROR, " Media State: invalid size");
2543 print_field(" Media State: %s", media_state_str(state));
2547 print_hex_field(" Data", frame->data, frame->size);
2550 static void media_state_read(const struct l2cap_frame *frame)
2552 print_media_state(frame);
2555 static void media_state_notify(const struct l2cap_frame *frame)
2557 print_media_state(frame);
2560 struct media_cp_opcode {
2562 const char *opcode_str;
2563 } media_cp_opcode_table[] = {
2566 {0x03, "Fast Rewind"},
2567 {0x04, "Fast Forward"},
2569 {0x10, "Move Relative"},
2570 {0x20, "Previous Segment"},
2571 {0x21, "Next Segment"},
2572 {0x22, "First Segment"},
2573 {0x23, "Last Segment"},
2574 {0x24, "Goto Segment"},
2575 {0x30, "Previous Track"},
2576 {0x31, "Next Track"},
2577 {0x32, "First Track"},
2578 {0x33, "Last Track"},
2579 {0x34, "Goto Track"},
2580 {0x40, "Previous Group"},
2581 {0x41, "Next Group"},
2582 {0x42, "First Group"},
2583 {0x43, "Last Group"},
2584 {0x44, "Goto Group"},
2587 static const char *cp_opcode_str(uint8_t opcode)
2591 for (i = 0; i < ARRAY_SIZE(media_cp_opcode_table); i++) {
2592 const char *str = media_cp_opcode_table[i].opcode_str;
2594 if (opcode == media_cp_opcode_table[i].opcode)
2601 static void print_media_cp(const struct l2cap_frame *frame)
2605 if (!l2cap_frame_get_u8((void *)frame, (uint8_t *)&opcode)) {
2606 print_text(COLOR_ERROR, " Media Control Point: invalid size");
2610 print_field(" Media Control Point: %s", cp_opcode_str(opcode));
2614 print_hex_field(" Data", frame->data, frame->size);
2617 static void media_cp_write(const struct l2cap_frame *frame)
2619 print_media_cp(frame);
2622 static void media_cp_notify(const struct l2cap_frame *frame)
2624 print_media_cp(frame);
2627 static const struct bitfield_data supported_opcodes_table[] = {
2628 {0, "Play (0x00000001)" },
2629 {1, "Pause (0x00000002)" },
2630 {2, "Fast Rewind (0x00000004)" },
2631 {3, "Fast Forward (0x00000008)" },
2632 {4, "Stop (0x00000010)" },
2633 {5, "Move Relative (0x00000020)" },
2634 {6, "Previous Segment (0x00000040)" },
2635 {7, "Next Segment (0x00000080)" },
2636 {8, "First Segment (0x00000100)" },
2637 {9, "Last Segment (0x00000200)" },
2638 {10, "Goto Segment (0x00000400)" },
2639 {11, "Previous Track (0x00000800)" },
2640 {12, "Next Track (0x00001000)" },
2641 {13, "First Track (0x00002000)" },
2642 {14, "Last Track (0x00004000)" },
2643 {15, "Goto Track (0x00008000)" },
2644 {16, "Previous Group (0x00010000)" },
2645 {17, "Next Group (0x00020000)" },
2646 {18, "First Group (0x00040000)" },
2647 {19, "Last Group (0x00080000)" },
2648 {20, "Goto Group (0x00100000)" },
2649 {21, "RFU (0x00200000)" },
2650 {22, "RFU (0x00400000)" },
2651 {23, "RFU (0x00800000)" },
2652 {24, "RFU (0x01000000)" },
2653 {25, "RFU (0x02000000)" },
2654 {26, "RFU (0x04000000)" },
2655 {27, "RFU (0x08000000)" },
2656 {28, "RFU (0x10000000)" },
2657 {29, "RFU (0x20000000)" },
2658 {30, "RFU (0x40000000)" },
2659 {31, "RFU (0x80000000)" },
2663 static void print_media_cp_op_supported(const struct l2cap_frame *frame)
2665 uint32_t supported_opcodes;
2668 if (!l2cap_frame_get_le32((void *)frame, &supported_opcodes)) {
2669 print_text(COLOR_ERROR, " value: invalid size");
2673 print_field(" Supported Opcodes: 0x%8.8x", supported_opcodes);
2675 mask = print_bitfield(8, supported_opcodes, supported_opcodes_table);
2677 print_text(COLOR_WHITE_BG, " Unknown fields (0x%4.4x)",
2682 print_hex_field(" Data", frame->data, frame->size);
2685 static void media_cp_op_supported_read(const struct l2cap_frame *frame)
2687 print_media_cp_op_supported(frame);
2690 static void media_cp_op_supported_notify(const struct l2cap_frame *frame)
2692 print_media_cp_op_supported(frame);
2695 static void print_content_control_id(const struct l2cap_frame *frame)
2699 if (!l2cap_frame_get_u8((void *)frame, (uint8_t *)&ccid)) {
2700 print_text(COLOR_ERROR, " Content Control ID: invalid size");
2704 print_field(" Content Control ID: 0x%2.2x", ccid);
2708 print_hex_field(" Data", frame->data, frame->size);
2711 static void content_control_id_read(const struct l2cap_frame *frame)
2713 print_content_control_id(frame);
2716 static const struct pa_sync_state_decoder {
2719 } pa_sync_state_decoders[] = {
2720 { 0x00, "Not synchronized to PA" },
2721 { 0x01, "SyncInfo Request" },
2722 { 0x02, "Synchronized to PA" },
2723 { 0x03, "Failed to synchronize to PA" },
2724 { 0x04, "No PAST" },
2727 static const struct cp_pa_sync_state_decoder {
2730 } cp_pa_sync_state_decoders[] = {
2731 { 0x00, "Do not synchronize to PA" },
2732 { 0x01, "Synchronize to PA - PAST available" },
2733 { 0x02, "Synchronize to PA - PAST not available" },
2736 static const struct big_enc_decoder {
2739 } big_enc_decoders[] = {
2740 { 0x00, "Not encrypted" },
2741 { 0x01, "Broadcast_Code required" },
2742 { 0x02, "Decrypting" },
2743 { 0x03, "Bad_Code (incorrect encryption key)" },
2746 static bool print_subgroup_lv(const struct l2cap_frame *frame,
2747 const char *label, struct packet_ltv_decoder *decoder,
2750 struct bt_hci_lv_data *lv;
2752 lv = l2cap_frame_pull((void *)frame, frame, sizeof(*lv));
2754 print_text(COLOR_ERROR, "%s: invalid size", label);
2758 if (!l2cap_frame_pull((void *)frame, frame, lv->len)) {
2759 print_text(COLOR_ERROR, "%s: invalid size", label);
2763 packet_print_ltv(label, lv->data, lv->len, decoder, decoder_len);
2768 static bool print_subgroup_metadata(const char *label,
2769 const struct l2cap_frame *frame)
2771 return print_subgroup_lv(frame, label, NULL, 0);
2774 static void print_bcast_recv_state(const struct l2cap_frame *frame)
2782 uint8_t pa_sync_state;
2785 uint8_t num_subgroups = 0;
2786 uint32_t bis_sync_state;
2788 if (frame->size == 0) {
2789 print_field(" Empty characteristic");
2793 if (!l2cap_frame_get_u8((void *)frame, &id)) {
2794 print_text(COLOR_ERROR, "Source_ID: invalid size");
2798 print_field(" Source_ID: %u", id);
2800 if (!l2cap_frame_get_u8((void *)frame, &addr_type)) {
2801 print_text(COLOR_ERROR, "Source_Address_Type: invalid size");
2805 print_field(" Source_Address_Type: %u", addr_type);
2807 addr = l2cap_frame_pull((void *)frame, frame, sizeof(bdaddr_t));
2809 print_text(COLOR_ERROR, "Source_Address: invalid size");
2813 print_field(" Source_Address: %2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
2818 if (!l2cap_frame_get_u8((void *)frame, &sid)) {
2819 print_text(COLOR_ERROR, "Source_Adv_SID: invalid size");
2823 print_field(" Source_Adv_SID: %u", sid);
2825 if (!l2cap_frame_get_le24((void *)frame, &bid)) {
2826 print_text(COLOR_ERROR, "Broadcast_ID: invalid size");
2830 print_field(" Broadcast_ID: 0x%06x", bid);
2832 if (!l2cap_frame_get_u8((void *)frame, &pa_sync_state)) {
2833 print_text(COLOR_ERROR, "PA_Sync_State: invalid size");
2837 for (i = 0; i < ARRAY_SIZE(pa_sync_state_decoders); i++) {
2838 const struct pa_sync_state_decoder *decoder;
2840 decoder = &pa_sync_state_decoders[i];
2842 if (decoder->code == pa_sync_state) {
2843 print_field(" PA_Sync_State: %s", decoder->value);
2848 if (i == ARRAY_SIZE(pa_sync_state_decoders))
2849 print_field(" PA_Sync_State: %s", "Invalid value");
2851 if (!l2cap_frame_get_u8((void *)frame, &enc)) {
2852 print_text(COLOR_ERROR, "BIG_Encryption: invalid size");
2856 for (i = 0; i < ARRAY_SIZE(big_enc_decoders); i++) {
2857 const struct big_enc_decoder *decoder;
2859 decoder = &big_enc_decoders[i];
2861 if (decoder->code == enc) {
2862 print_field(" BIG_Encryption: %s", decoder->value);
2867 if (i == ARRAY_SIZE(big_enc_decoders))
2868 print_field(" BIG_Encryption: %s", "Invalid value");
2871 bad_code = l2cap_frame_pull((void *)frame, frame, 16);
2873 print_text(COLOR_ERROR, "Bad_Code: invalid size");
2877 print_hex_field(" Bad_Code", bad_code, 16);
2880 if (!l2cap_frame_get_u8((void *)frame, &num_subgroups)) {
2881 print_text(COLOR_ERROR, "Num_Subgroups: invalid size");
2885 print_field(" Num_Subgroups: %u", num_subgroups);
2887 for (i = 0; i < num_subgroups; i++) {
2888 print_field(" Subgroup #%u:", i);
2890 if (!l2cap_frame_get_le32((void *)frame, &bis_sync_state)) {
2891 print_text(COLOR_ERROR, "BIS_Sync State: invalid size");
2895 print_field(" BIS_Sync State: 0x%8.8x", bis_sync_state);
2897 if (!print_subgroup_metadata(" Metadata", frame))
2903 print_hex_field(" Data", frame->data, frame->size);
2906 static void bcast_recv_state_read(const struct l2cap_frame *frame)
2908 print_bcast_recv_state(frame);
2911 static void bcast_recv_state_notify(const struct l2cap_frame *frame)
2913 print_bcast_recv_state(frame);
2916 #define BCAST_AUDIO_SCAN_CP_CMD(_op, _desc, _func) \
2922 static void bcast_audio_scan_cp_add_src_cmd(const struct l2cap_frame *frame)
2929 uint8_t pa_sync_state;
2930 uint16_t pa_interval;
2931 uint8_t num_subgroups = 0;
2932 uint32_t bis_sync_state;
2934 if (!l2cap_frame_get_u8((void *)frame, &addr_type)) {
2935 print_text(COLOR_ERROR, "Source_Address_Type: invalid size");
2939 print_field(" Source_Address_Type: %u", addr_type);
2941 addr = l2cap_frame_pull((void *)frame, frame, sizeof(bdaddr_t));
2943 print_text(COLOR_ERROR, "Source_Address: invalid size");
2947 print_field(" Source_Address: %2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
2952 if (!l2cap_frame_get_u8((void *)frame, &sid)) {
2953 print_text(COLOR_ERROR, "Source_Adv_SID: invalid size");
2957 print_field(" Source_Adv_SID: %u", sid);
2959 if (!l2cap_frame_get_le24((void *)frame, &bid)) {
2960 print_text(COLOR_ERROR, "Broadcast_ID: invalid size");
2964 print_field(" Broadcast_ID: 0x%06x", bid);
2966 if (!l2cap_frame_get_u8((void *)frame, &pa_sync_state)) {
2967 print_text(COLOR_ERROR, "PA_Sync_State: invalid size");
2971 for (i = 0; i < ARRAY_SIZE(cp_pa_sync_state_decoders); i++) {
2972 const struct cp_pa_sync_state_decoder *decoder;
2974 decoder = &cp_pa_sync_state_decoders[i];
2976 if (decoder->code == pa_sync_state) {
2977 print_field(" PA_Sync_State: %s", decoder->value);
2982 if (i == ARRAY_SIZE(cp_pa_sync_state_decoders))
2983 print_field(" PA_Sync_State: %s", "Invalid value");
2985 if (!l2cap_frame_get_le16((void *)frame, &pa_interval)) {
2986 print_text(COLOR_ERROR, "PA_Interval: invalid size");
2990 print_field(" PA_Interval: 0x%04x", pa_interval);
2992 if (!l2cap_frame_get_u8((void *)frame, &num_subgroups)) {
2993 print_text(COLOR_ERROR, "Num_Subgroups: invalid size");
2997 print_field(" Num_Subgroups: %u", num_subgroups);
2999 for (i = 0; i < num_subgroups; i++) {
3000 print_field(" Subgroup #%u:", i);
3002 if (!l2cap_frame_get_le32((void *)frame, &bis_sync_state)) {
3003 print_text(COLOR_ERROR, "BIS_Sync State: invalid size");
3007 print_field(" BIS_Sync State: 0x%8.8x", bis_sync_state);
3009 if (!print_subgroup_metadata(" Metadata", frame))
3014 static void bcast_audio_scan_cp_mod_src_cmd(const struct l2cap_frame *frame)
3018 uint8_t pa_sync_state;
3019 uint16_t pa_interval;
3020 uint8_t num_subgroups = 0;
3021 uint32_t bis_sync_state;
3023 if (!l2cap_frame_get_u8((void *)frame, &id)) {
3024 print_text(COLOR_ERROR, "Source_ID: invalid size");
3028 print_field(" Source_ID: %u", id);
3030 if (!l2cap_frame_get_u8((void *)frame, &pa_sync_state)) {
3031 print_text(COLOR_ERROR, "PA_Sync_State: invalid size");
3035 for (i = 0; i < ARRAY_SIZE(cp_pa_sync_state_decoders); i++) {
3036 const struct cp_pa_sync_state_decoder *decoder;
3038 decoder = &cp_pa_sync_state_decoders[i];
3040 if (decoder->code == pa_sync_state) {
3041 print_field(" PA_Sync_State: %s", decoder->value);
3046 if (i == ARRAY_SIZE(cp_pa_sync_state_decoders))
3047 print_field(" PA_Sync_State: %s", "Invalid value");
3049 if (!l2cap_frame_get_le16((void *)frame, &pa_interval)) {
3050 print_text(COLOR_ERROR, "PA_Interval: invalid size");
3054 print_field(" PA_Interval: 0x%04x", pa_interval);
3056 if (!l2cap_frame_get_u8((void *)frame, &num_subgroups)) {
3057 print_text(COLOR_ERROR, "Num_Subgroups: invalid size");
3061 print_field(" Num_Subgroups: %u", num_subgroups);
3063 for (i = 0; i < num_subgroups; i++) {
3064 print_field(" Subgroup #%u:", i);
3066 if (!l2cap_frame_get_le32((void *)frame, &bis_sync_state)) {
3067 print_text(COLOR_ERROR, "BIS_Sync State: invalid size");
3071 print_field(" BIS_Sync State: 0x%8.8x", bis_sync_state);
3073 if (!print_subgroup_metadata(" Metadata", frame))
3078 static void bcast_audio_scan_cp_set_bcode_cmd(const struct l2cap_frame *frame)
3081 uint8_t *bcast_code;
3083 if (!l2cap_frame_get_u8((void *)frame, &id)) {
3084 print_text(COLOR_ERROR, "Source_ID: invalid size");
3088 print_field(" Source_ID: %u", id);
3090 bcast_code = l2cap_frame_pull((void *)frame, frame, 16);
3092 print_text(COLOR_ERROR, "Broadcast_Code: invalid size");
3096 print_hex_field(" Broadcast_Code", bcast_code, 16);
3100 static void bcast_audio_scan_cp_remove_src_cmd(const struct l2cap_frame *frame)
3104 if (!l2cap_frame_get_u8((void *)frame, &id)) {
3105 print_text(COLOR_ERROR, "Source_ID: invalid size");
3109 print_field(" Source_ID: %u", id);
3112 struct bcast_audio_scan_cp_cmd {
3114 void (*func)(const struct l2cap_frame *frame);
3115 } bcast_audio_scan_cp_cmd_table[] = {
3116 /* Opcode = 0x00 (Remote Scan Stopped) */
3117 BCAST_AUDIO_SCAN_CP_CMD(0x00, "Remote Scan Stopped", NULL),
3118 /* Opcode = 0x01 (Remote Scan Started) */
3119 BCAST_AUDIO_SCAN_CP_CMD(0x01, "Remote Scan Started", NULL),
3120 /* Opcode = 0x02 (Add Source) */
3121 BCAST_AUDIO_SCAN_CP_CMD(0x02, "Add Source",
3122 bcast_audio_scan_cp_add_src_cmd),
3123 /* Opcode = 0x03 (Modify Source) */
3124 BCAST_AUDIO_SCAN_CP_CMD(0x03, "Modify Source",
3125 bcast_audio_scan_cp_mod_src_cmd),
3126 /* Opcode = 0x04 (Set Broadcast_Code) */
3127 BCAST_AUDIO_SCAN_CP_CMD(0x04, "Set Broadcast_Code",
3128 bcast_audio_scan_cp_set_bcode_cmd),
3129 /* Opcode = 0x05 (Remove Source) */
3130 BCAST_AUDIO_SCAN_CP_CMD(0x05, "Remove Source",
3131 bcast_audio_scan_cp_remove_src_cmd),
3134 static struct bcast_audio_scan_cp_cmd *bcast_audio_scan_cp_get_cmd(uint8_t op)
3136 if (op > ARRAY_SIZE(bcast_audio_scan_cp_cmd_table))
3139 return &bcast_audio_scan_cp_cmd_table[op];
3142 static void print_bcast_audio_scan_cp_cmd(const struct l2cap_frame *frame)
3145 struct bcast_audio_scan_cp_cmd *cmd;
3147 if (!l2cap_frame_get_u8((void *)frame, &op)) {
3148 print_text(COLOR_ERROR, "Opcode: invalid size");
3152 cmd = bcast_audio_scan_cp_get_cmd(op);
3154 print_field(" Opcode: Reserved (0x%2.2x)", op);
3158 print_field(" Opcode: %s (0x%2.2x)", cmd->desc, op);
3164 print_hex_field(" Data", frame->data, frame->size);
3167 static void bcast_audio_scan_cp_write(const struct l2cap_frame *frame)
3169 print_bcast_audio_scan_cp_cmd(frame);
3172 #define GATT_HANDLER(_uuid, _read, _write, _notify) \
3175 .type = BT_UUID16, \
3176 .value.u16 = _uuid, \
3183 struct gatt_handler {
3185 void (*read)(const struct l2cap_frame *frame);
3186 void (*write)(const struct l2cap_frame *frame);
3187 void (*notify)(const struct l2cap_frame *frame);
3188 } gatt_handlers[] = {
3189 GATT_HANDLER(0x2800, pri_svc_read, NULL, NULL),
3190 GATT_HANDLER(0x2801, sec_svc_read, NULL, NULL),
3191 GATT_HANDLER(0x2803, chrc_read, NULL, NULL),
3192 GATT_HANDLER(0x2902, ccc_read, ccc_write, NULL),
3193 GATT_HANDLER(0x2bc4, ase_read, NULL, ase_notify),
3194 GATT_HANDLER(0x2bc5, ase_read, NULL, ase_notify),
3195 GATT_HANDLER(0x2bc6, NULL, ase_cp_write, ase_cp_notify),
3196 GATT_HANDLER(0x2bc9, pac_read, NULL, pac_notify),
3197 GATT_HANDLER(0x2bca, pac_loc_read, NULL, pac_loc_notify),
3198 GATT_HANDLER(0x2bcb, pac_read, NULL, pac_notify),
3199 GATT_HANDLER(0x2bcc, pac_loc_read, NULL, pac_loc_notify),
3200 GATT_HANDLER(0x2bcd, pac_context_read, NULL, pac_context_notify),
3201 GATT_HANDLER(0x2bce, pac_context_read, NULL, pac_context_notify),
3202 GATT_HANDLER(0x2b7d, vol_state_read, NULL, vol_state_notify),
3203 GATT_HANDLER(0x2b7e, NULL, vol_cp_write, NULL),
3204 GATT_HANDLER(0x2b7f, vol_flag_read, NULL, vol_flag_notify),
3206 GATT_HANDLER(0x2b84, csip_sirk_read, NULL, csip_sirk_notify),
3207 GATT_HANDLER(0x2b85, csip_size_read, NULL, csip_size_notify),
3208 GATT_HANDLER(0x2b86, csip_lock_read, NULL, NULL),
3209 GATT_HANDLER(0x2b87, csip_rank_read, NULL, NULL),
3211 GATT_HANDLER(0x2b93, mp_name_read, NULL, mp_name_notify),
3212 GATT_HANDLER(0x2b96, NULL, NULL, track_changed_notify),
3213 GATT_HANDLER(0x2b97, track_title_read, NULL, track_title_notify),
3214 GATT_HANDLER(0x2b98, track_duration_read, NULL, track_duration_notify),
3215 GATT_HANDLER(0x2b99, track_position_read, track_position_write,
3216 track_position_notify),
3217 GATT_HANDLER(0x2b9a, playback_speed_read, playback_speed_write,
3218 playback_speed_notify),
3219 GATT_HANDLER(0x2b9b, seeking_speed_read, NULL, seeking_speed_notify),
3220 GATT_HANDLER(0x2ba1, playing_order_read, playing_order_write,
3221 playing_order_notify),
3222 GATT_HANDLER(0x2ba2, playing_orders_supported_read, NULL, NULL),
3223 GATT_HANDLER(0x2ba3, media_state_read, NULL, media_state_notify),
3224 GATT_HANDLER(0x2ba4, NULL, media_cp_write, media_cp_notify),
3225 GATT_HANDLER(0x2ba5, media_cp_op_supported_read, NULL,
3226 media_cp_op_supported_notify),
3227 GATT_HANDLER(0x2bba, content_control_id_read, NULL, NULL),
3229 GATT_HANDLER(0x2bc7, NULL, bcast_audio_scan_cp_write, NULL),
3230 GATT_HANDLER(0x2bc8, bcast_recv_state_read, NULL,
3231 bcast_recv_state_notify),
3234 static struct gatt_handler *get_handler_uuid(const bt_uuid_t *uuid)
3241 for (i = 0; i < ARRAY_SIZE(gatt_handlers); i++) {
3242 struct gatt_handler *handler = &gatt_handlers[i];
3244 if (!bt_uuid_cmp(&handler->uuid, uuid))
3251 static struct gatt_handler *get_handler(struct gatt_db_attribute *attr)
3253 return get_handler_uuid(gatt_db_attribute_get_type(attr));
3256 static void att_exchange_mtu_req(const struct l2cap_frame *frame)
3258 const struct bt_l2cap_att_exchange_mtu_req *pdu = frame->data;
3260 print_field("Client RX MTU: %d", le16_to_cpu(pdu->mtu));
3263 static void att_exchange_mtu_rsp(const struct l2cap_frame *frame)
3265 const struct bt_l2cap_att_exchange_mtu_rsp *pdu = frame->data;
3267 print_field("Server RX MTU: %d", le16_to_cpu(pdu->mtu));
3270 static void att_find_info_req(const struct l2cap_frame *frame)
3272 print_handle_range("Handle range", frame->data);
3275 static const char *att_format_str(uint8_t format)
3287 static struct gatt_db_attribute *insert_desc(const struct l2cap_frame *frame,
3289 bt_uuid_t *uuid, bool rsp)
3293 db = get_db(frame, rsp);
3297 return gatt_db_insert_descriptor(db, handle, uuid, 0, NULL, NULL, NULL);
3300 static void att_find_info_rsp_16(const struct l2cap_frame *frame)
3302 while (frame->size >= 4) {
3307 if (!l2cap_frame_get_le16((void *)frame, &handle)) {
3308 print_text(COLOR_ERROR, " Handle: invalid size");
3312 if (!l2cap_frame_get_le16((void *)frame, &u16)) {
3313 print_text(COLOR_ERROR, " UUID: invalid size");
3317 print_field("Handle: 0x%4.4x", handle);
3318 print_uuid("UUID", &u16, 2);
3320 bt_uuid16_create(&uuid, u16);
3322 insert_desc(frame, handle, &uuid, true);
3326 static void att_find_info_rsp_128(const struct l2cap_frame *frame)
3328 while (frame->size >= 18) {
3332 if (!l2cap_frame_get_le16((void *)frame, &handle)) {
3333 print_text(COLOR_ERROR, " Handle: invalid size");
3337 if (frame->size < 16) {
3338 print_text(COLOR_ERROR, " UUID: invalid size");
3342 print_field("Handle: 0x%4.4x", handle);
3343 print_uuid("UUID", frame->data, 16);
3345 bt_uuid_from_data(&uuid, frame->data, 16);
3347 if (!l2cap_frame_pull((void *)frame, frame, 16))
3350 insert_desc(frame, handle, &uuid, true);
3354 static void att_find_info_rsp(const struct l2cap_frame *frame)
3358 if (!l2cap_frame_get_u8((void *)frame, &format)) {
3359 print_text(COLOR_ERROR, " Format: invalid size");
3363 print_field("Format: %s (0x%2.2x)", att_format_str(format), format);
3367 att_find_info_rsp_16(frame);
3370 att_find_info_rsp_128(frame);
3376 packet_hexdump(frame->data, frame->size);
3379 static void att_find_by_type_val_req(const struct l2cap_frame *frame)
3383 print_handle_range("Handle range", frame->data);
3385 type = get_le16(frame->data + 4);
3386 print_attribute_info(type, frame->data + 6, frame->size - 6);
3389 static void att_find_by_type_val_rsp(const struct l2cap_frame *frame)
3391 const uint8_t *ptr = frame->data;
3392 uint16_t len = frame->size;
3395 print_handle_range("Handle range", ptr);
3400 packet_hexdump(ptr, len);
3403 static struct gatt_db_attribute *get_attribute(const struct l2cap_frame *frame,
3404 uint16_t handle, bool rsp)
3408 db = get_db(frame, rsp);
3412 return gatt_db_get_attribute(db, handle);
3415 static void queue_read(const struct l2cap_frame *frame, bt_uuid_t *uuid,
3418 struct packet_conn_data *conn;
3419 struct att_conn_data *data;
3420 struct att_read *read;
3421 struct gatt_db_attribute *attr = NULL;
3422 struct gatt_handler *handler;
3425 attr = get_attribute(frame, handle, false);
3430 handler = attr ? get_handler(attr) : get_handler_uuid(uuid);
3431 if (!handler || !handler->read)
3434 conn = packet_get_conn_data(frame->handle);
3435 data = att_get_conn_data(conn);
3440 data->reads = queue_new();
3442 read = new0(struct att_read, 1);
3444 read->in = frame->in;
3445 read->chan = frame->chan;
3446 read->func = handler->read;
3448 queue_push_tail(data->reads, read);
3451 static void att_read_type_req(const struct l2cap_frame *frame)
3455 print_handle_range("Handle range", frame->data);
3456 print_uuid("Attribute type", frame->data + 4, frame->size - 4);
3458 if (bt_uuid_from_data(&uuid, frame->data + 4, frame->size - 4))
3461 queue_read(frame, &uuid, 0x0000);
3464 static void att_read_type_rsp(const struct l2cap_frame *frame)
3468 if (!l2cap_frame_get_u8((void *)frame, &len)) {
3469 print_text(COLOR_ERROR, "invalid size");
3473 print_field("Attribute data length: %d", len);
3474 print_data_list("Attribute data list", len, frame);
3477 static void print_handle(const struct l2cap_frame *frame, uint16_t handle,
3480 struct gatt_db_attribute *attr;
3482 attr = get_attribute(frame, handle, rsp);
3484 print_field("Handle: 0x%4.4x", handle);
3488 print_attribute(attr);
3491 static void att_read_req(const struct l2cap_frame *frame)
3493 const struct bt_l2cap_att_read_req *pdu = frame->data;
3496 l2cap_frame_pull((void *)frame, frame, sizeof(*pdu));
3498 handle = le16_to_cpu(pdu->handle);
3499 print_handle(frame, handle, false);
3501 queue_read(frame, NULL, handle);
3504 static void att_read_rsp(const struct l2cap_frame *frame)
3506 struct att_read *read;
3508 read = att_get_read(frame);
3512 print_attribute(read->attr);
3513 print_hex_field("Value", frame->data, frame->size);
3520 static void att_read_blob_req(const struct l2cap_frame *frame)
3522 print_handle(frame, get_le16(frame->data), false);
3523 print_field("Offset: 0x%4.4x", get_le16(frame->data + 2));
3526 static void att_read_blob_rsp(const struct l2cap_frame *frame)
3528 packet_hexdump(frame->data, frame->size);
3531 static void att_read_multiple_req(const struct l2cap_frame *frame)
3535 count = frame->size / 2;
3537 for (i = 0; i < count; i++)
3538 print_handle(frame, get_le16(frame->data + (i * 2)), false);
3541 static void att_read_group_type_req(const struct l2cap_frame *frame)
3545 print_handle_range("Handle range", frame->data);
3546 print_uuid("Attribute group type", frame->data + 4, frame->size - 4);
3548 if (bt_uuid_from_data(&uuid, frame->data + 4, frame->size - 4))
3551 queue_read(frame, &uuid, 0x0000);
3554 static void print_group_list(const char *label, uint8_t length,
3555 const struct l2cap_frame *frame)
3557 struct att_read *read;
3563 read = att_get_read(frame);
3565 count = frame->size / length;
3567 print_field("%s: %u entr%s", label, count, count == 1 ? "y" : "ies");
3569 while (frame->size >= length) {
3570 print_handle_range("Handle range", frame->data);
3571 print_uuid("UUID", frame->data + 4, length - 4);
3574 struct l2cap_frame f;
3576 l2cap_frame_clone_size(&f, frame, length);
3581 if (!l2cap_frame_pull((void *)frame, frame, length))
3585 packet_hexdump(frame->data, frame->size);
3589 static void att_read_group_type_rsp(const struct l2cap_frame *frame)
3591 const struct bt_l2cap_att_read_group_type_rsp *pdu = frame->data;
3593 l2cap_frame_pull((void *)frame, frame, sizeof(*pdu));
3595 print_field("Attribute data length: %d", pdu->length);
3596 print_group_list("Attribute group list", pdu->length, frame);
3599 static void print_write(const struct l2cap_frame *frame, uint16_t handle,
3602 struct gatt_db_attribute *attr;
3603 struct gatt_handler *handler;
3605 print_handle(frame, handle, false);
3607 if (len > frame->size) {
3608 print_text(COLOR_ERROR, "invalid size");
3612 print_hex_field(" Data", frame->data, len);
3614 attr = get_attribute(frame, handle, false);
3618 handler = get_handler(attr);
3619 if (!handler || !handler->write)
3622 handler->write(frame);
3625 static void att_write_req(const struct l2cap_frame *frame)
3629 if (!l2cap_frame_get_le16((void *)frame, &handle)) {
3630 print_text(COLOR_ERROR, "invalid size");
3634 print_write(frame, handle, frame->size);
3637 static void att_write_rsp(const struct l2cap_frame *frame)
3641 static void att_prepare_write_req(const struct l2cap_frame *frame)
3643 print_handle(frame, get_le16(frame->data), false);
3644 print_field("Offset: 0x%4.4x", get_le16(frame->data + 2));
3645 print_hex_field(" Data", frame->data + 4, frame->size - 4);
3648 static void att_prepare_write_rsp(const struct l2cap_frame *frame)
3650 print_handle(frame, get_le16(frame->data), true);
3651 print_field("Offset: 0x%4.4x", get_le16(frame->data + 2));
3652 print_hex_field(" Data", frame->data + 4, frame->size - 4);
3655 static void att_execute_write_req(const struct l2cap_frame *frame)
3657 uint8_t flags = *(uint8_t *) frame->data;
3658 const char *flags_str;
3662 flags_str = "Cancel all prepared writes";
3665 flags_str = "Immediately write all pending values";
3668 flags_str = "Unknown";
3672 print_field("Flags: %s (0x%02x)", flags_str, flags);
3675 static void print_notify(const struct l2cap_frame *frame, uint16_t handle,
3678 struct gatt_db_attribute *attr;
3679 struct gatt_handler *handler;
3680 struct l2cap_frame clone;
3682 print_handle(frame, handle, true);
3683 print_hex_field(" Data", frame->data, len);
3685 if (len > frame->size) {
3686 print_text(COLOR_ERROR, "invalid size");
3690 attr = get_attribute(frame, handle, true);
3694 handler = get_handler(attr);
3698 /* Use a clone if the callback is not expected to parse the whole
3701 if (len != frame->size) {
3702 l2cap_frame_clone(&clone, frame);
3707 handler->notify(frame);
3710 static void att_handle_value_notify(const struct l2cap_frame *frame)
3713 const struct bt_l2cap_att_handle_value_notify *pdu = frame->data;
3715 l2cap_frame_pull((void *)frame, frame, sizeof(*pdu));
3717 handle = le16_to_cpu(pdu->handle);
3718 print_notify(frame, handle, frame->size);
3721 static void att_handle_value_ind(const struct l2cap_frame *frame)
3723 const struct bt_l2cap_att_handle_value_ind *pdu = frame->data;
3725 l2cap_frame_pull((void *)frame, frame, sizeof(*pdu));
3727 print_notify(frame, le16_to_cpu(pdu->handle), frame->size);
3730 static void att_handle_value_conf(const struct l2cap_frame *frame)
3734 static void att_multiple_vl_rsp(const struct l2cap_frame *frame)
3736 struct l2cap_frame *f = (void *) frame;
3738 while (frame->size) {
3742 if (!l2cap_frame_get_le16(f, &handle))
3745 if (!l2cap_frame_get_le16(f, &len))
3748 print_field("Length: 0x%4.4x", len);
3750 print_notify(frame, handle, len);
3752 l2cap_frame_pull(f, f, len);
3756 static void att_write_command(const struct l2cap_frame *frame)
3760 if (!l2cap_frame_get_le16((void *)frame, &handle)) {
3761 print_text(COLOR_ERROR, "invalid size");
3765 print_write(frame, handle, frame->size);
3768 static void att_signed_write_command(const struct l2cap_frame *frame)
3772 if (!l2cap_frame_get_le16((void *)frame, &handle)) {
3773 print_text(COLOR_ERROR, "invalid size");
3777 print_write(frame, handle, frame->size - 12);
3778 print_hex_field(" Signature", frame->data + frame->size - 12, 12);
3781 struct att_opcode_data {
3784 void (*func) (const struct l2cap_frame *frame);
3789 static const struct att_opcode_data att_opcode_table[] = {
3790 { 0x01, "Error Response",
3791 att_error_response, 4, true },
3792 { 0x02, "Exchange MTU Request",
3793 att_exchange_mtu_req, 2, true },
3794 { 0x03, "Exchange MTU Response",
3795 att_exchange_mtu_rsp, 2, true },
3796 { 0x04, "Find Information Request",
3797 att_find_info_req, 4, true },
3798 { 0x05, "Find Information Response",
3799 att_find_info_rsp, 5, false },
3800 { 0x06, "Find By Type Value Request",
3801 att_find_by_type_val_req, 6, false },
3802 { 0x07, "Find By Type Value Response",
3803 att_find_by_type_val_rsp, 4, false },
3804 { 0x08, "Read By Type Request",
3805 att_read_type_req, 6, false },
3806 { 0x09, "Read By Type Response",
3807 att_read_type_rsp, 3, false },
3808 { 0x0a, "Read Request",
3809 att_read_req, 2, true },
3810 { 0x0b, "Read Response",
3811 att_read_rsp, 0, false },
3812 { 0x0c, "Read Blob Request",
3813 att_read_blob_req, 4, true },
3814 { 0x0d, "Read Blob Response",
3815 att_read_blob_rsp, 0, false },
3816 { 0x0e, "Read Multiple Request",
3817 att_read_multiple_req, 4, false },
3818 { 0x0f, "Read Multiple Response" },
3819 { 0x10, "Read By Group Type Request",
3820 att_read_group_type_req, 6, false },
3821 { 0x11, "Read By Group Type Response",
3822 att_read_group_type_rsp, 4, false },
3823 { 0x12, "Write Request" ,
3824 att_write_req, 2, false },
3825 { 0x13, "Write Response",
3826 att_write_rsp, 0, true },
3827 { 0x16, "Prepare Write Request",
3828 att_prepare_write_req, 4, false },
3829 { 0x17, "Prepare Write Response",
3830 att_prepare_write_rsp, 4, false },
3831 { 0x18, "Execute Write Request",
3832 att_execute_write_req, 1, true },
3833 { 0x19, "Execute Write Response" },
3834 { 0x1b, "Handle Value Notification",
3835 att_handle_value_notify, 2, false },
3836 { 0x1d, "Handle Value Indication",
3837 att_handle_value_ind, 2, false },
3838 { 0x1e, "Handle Value Confirmation",
3839 att_handle_value_conf, 0, true },
3840 { 0x20, "Read Multiple Request Variable Length",
3841 att_read_multiple_req, 4, false },
3842 { 0x21, "Read Multiple Response Variable Length",
3843 att_multiple_vl_rsp, 4, false },
3844 { 0x23, "Handle Multiple Value Notification",
3845 att_multiple_vl_rsp, 4, false },
3846 { 0x52, "Write Command",
3847 att_write_command, 2, false },
3848 { 0xd2, "Signed Write Command", att_signed_write_command, 14, false },
3852 static const char *att_opcode_to_str(uint8_t opcode)
3856 for (i = 0; att_opcode_table[i].str; i++) {
3857 if (att_opcode_table[i].opcode == opcode)
3858 return att_opcode_table[i].str;
3864 void att_packet(uint16_t index, bool in, uint16_t handle, uint16_t cid,
3865 const void *data, uint16_t size)
3867 struct l2cap_frame frame;
3868 uint8_t opcode = *((const uint8_t *) data);
3869 const struct att_opcode_data *opcode_data = NULL;
3870 const char *opcode_color, *opcode_str;
3874 print_text(COLOR_ERROR, "malformed attribute packet");
3875 packet_hexdump(data, size);
3879 for (i = 0; att_opcode_table[i].str; i++) {
3880 if (att_opcode_table[i].opcode == opcode) {
3881 opcode_data = &att_opcode_table[i];
3887 if (opcode_data->func) {
3889 opcode_color = COLOR_MAGENTA;
3891 opcode_color = COLOR_BLUE;
3893 opcode_color = COLOR_WHITE_BG;
3894 opcode_str = opcode_data->str;
3896 opcode_color = COLOR_WHITE_BG;
3897 opcode_str = "Unknown";
3900 print_indent(6, opcode_color, "ATT: ", opcode_str, COLOR_OFF,
3901 " (0x%2.2x) len %d", opcode, size - 1);
3903 if (!opcode_data || !opcode_data->func) {
3904 packet_hexdump(data + 1, size - 1);
3908 if (opcode_data->fixed) {
3909 if (size - 1 != opcode_data->size) {
3910 print_text(COLOR_ERROR, "invalid size");
3911 packet_hexdump(data + 1, size - 1);
3915 if (size - 1 < opcode_data->size) {
3916 print_text(COLOR_ERROR, "too short packet");
3917 packet_hexdump(data + 1, size - 1);
3922 l2cap_frame_init(&frame, index, in, handle, 0, cid, 0,
3923 data + 1, size - 1);
3924 opcode_data->func(&frame);