monitor/att: Revert treating Notification/Indication as a request
[platform/upstream/bluez.git] / monitor / att.c
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /*
3  *
4  *  BlueZ - Bluetooth protocol stack for Linux
5  *
6  *  Copyright (C) 2011-2014  Intel Corporation
7  *  Copyright (C) 2002-2010  Marcel Holtmann <marcel@holtmann.org>
8  *
9  *
10  */
11
12 #ifdef HAVE_CONFIG_H
13 #include <config.h>
14 #endif
15
16 #include <ctype.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <inttypes.h>
21 #include <stdbool.h>
22 #include <errno.h>
23 #include <linux/limits.h>
24 #include <sys/stat.h>
25
26 #include <glib.h>
27
28 #include "lib/bluetooth.h"
29 #include "lib/uuid.h"
30 #include "lib/hci.h"
31 #include "lib/hci_lib.h"
32
33 #include "src/shared/util.h"
34 #include "src/shared/queue.h"
35 #include "src/shared/att.h"
36 #include "src/shared/gatt-db.h"
37 #include "src/textfile.h"
38 #include "src/settings.h"
39 #include "bt.h"
40 #include "packet.h"
41 #include "display.h"
42 #include "l2cap.h"
43 #include "att.h"
44
45 static void print_uuid(const char *label, const void *data, uint16_t size)
46 {
47         const char *str;
48         char uuidstr[MAX_LEN_UUID_STR];
49
50         switch (size) {
51         case 2:
52                 str = bt_uuid16_to_str(get_le16(data));
53                 print_field("%s: %s (0x%4.4x)", label, str, get_le16(data));
54                 break;
55         case 4:
56                 str = bt_uuid32_to_str(get_le32(data));
57                 print_field("%s: %s (0x%8.8x)", label, str, get_le32(data));
58                 break;
59         case 16:
60                 sprintf(uuidstr, "%8.8x-%4.4x-%4.4x-%4.4x-%8.8x%4.4x",
61                                 get_le32(data + 12), get_le16(data + 10),
62                                 get_le16(data + 8), get_le16(data + 6),
63                                 get_le32(data + 2), get_le16(data + 0));
64                 str = bt_uuidstr_to_str(uuidstr);
65                 print_field("%s: %s (%s)", label, str, uuidstr);
66                 break;
67         default:
68                 packet_hexdump(data, size);
69                 break;
70         }
71 }
72
73 static void print_handle_range(const char *label, const void *data)
74 {
75         print_field("%s: 0x%4.4x-0x%4.4x", label,
76                                 get_le16(data), get_le16(data + 2));
77 }
78
79 static void print_data_list(const char *label, uint8_t length,
80                                         const void *data, uint16_t size)
81 {
82         uint8_t count;
83
84         if (length == 0)
85                 return;
86
87         count = size / length;
88
89         print_field("%s: %u entr%s", label, count, count == 1 ? "y" : "ies");
90
91         while (size >= length) {
92                 print_field("Handle: 0x%4.4x", get_le16(data));
93                 print_hex_field("Value", data + 2, length - 2);
94
95                 data += length;
96                 size -= length;
97         }
98
99         packet_hexdump(data, size);
100 }
101
102 static void print_attribute_info(uint16_t type, const void *data, uint16_t len)
103 {
104         const char *str = bt_uuid16_to_str(type);
105
106         print_field("%s: %s (0x%4.4x)", "Attribute type", str, type);
107
108         switch (type) {
109         case 0x2800:    /* Primary Service */
110         case 0x2801:    /* Secondary Service */
111                 print_uuid("  UUID", data, len);
112                 break;
113         case 0x2802:    /* Include */
114                 if (len < 4) {
115                         print_hex_field("  Value", data, len);
116                         break;
117                 }
118                 print_handle_range("  Handle range", data);
119                 print_uuid("  UUID", data + 4, len - 4);
120                 break;
121         case 0x2803:    /* Characteristic */
122                 if (len < 3) {
123                         print_hex_field("  Value", data, len);
124                         break;
125                 }
126                 print_field("  Properties: 0x%2.2x", *((uint8_t *) data));
127                 print_field("  Handle: 0x%2.2x", get_le16(data + 1));
128                 print_uuid("  UUID", data + 3, len - 3);
129                 break;
130         default:
131                 print_hex_field("Value", data, len);
132                 break;
133         }
134 }
135
136 static const char *att_opcode_to_str(uint8_t opcode);
137
138 static void att_error_response(const struct l2cap_frame *frame)
139 {
140         const struct bt_l2cap_att_error_response *pdu = frame->data;
141         const char *str;
142
143         switch (pdu->error) {
144         case 0x01:
145                 str = "Invalid Handle";
146                 break;
147         case 0x02:
148                 str = "Read Not Permitted";
149                 break;
150         case 0x03:
151                 str = "Write Not Permitted";
152                 break;
153         case 0x04:
154                 str = "Invalid PDU";
155                 break;
156         case 0x05:
157                 str = "Insufficient Authentication";
158                 break;
159         case 0x06:
160                 str = "Request Not Supported";
161                 break;
162         case 0x07:
163                 str = "Invalid Offset";
164                 break;
165         case 0x08:
166                 str = "Insufficient Authorization";
167                 break;
168         case 0x09:
169                 str = "Prepare Queue Full";
170                 break;
171         case 0x0a:
172                 str = "Attribute Not Found";
173                 break;
174         case 0x0b:
175                 str = "Attribute Not Long";
176                 break;
177         case 0x0c:
178                 str = "Insufficient Encryption Key Size";
179                 break;
180         case 0x0d:
181                 str = "Invalid Attribute Value Length";
182                 break;
183         case 0x0e:
184                 str = "Unlikely Error";
185                 break;
186         case 0x0f:
187                 str = "Insufficient Encryption";
188                 break;
189         case 0x10:
190                 str = "Unsupported Group Type";
191                 break;
192         case 0x11:
193                 str = "Insufficient Resources";
194                 break;
195         case 0x12:
196                 str = "Database Out of Sync";
197                 break;
198         case 0x13:
199                 str = "Value Not Allowed";
200                 break;
201         case 0xfd:
202                 str = "CCC Improperly Configured";
203                 break;
204         case 0xfe:
205                 str = "Procedure Already in Progress";
206                 break;
207         case 0xff:
208                 str = "Out of Range";
209                 break;
210         default:
211                 str = "Reserved";
212                 break;
213         }
214
215         print_field("%s (0x%2.2x)", att_opcode_to_str(pdu->request),
216                                                         pdu->request);
217         print_field("Handle: 0x%4.4x", le16_to_cpu(pdu->handle));
218         print_field("Error: %s (0x%2.2x)", str, pdu->error);
219 }
220
221 static const struct bitfield_data ccc_value_table[] = {
222         {  0, "Notification (0x01)"             },
223         {  1, "Indication (0x02)"               },
224         { }
225 };
226
227 static void print_ccc_value(const struct l2cap_frame *frame)
228 {
229         uint8_t value;
230         uint8_t mask;
231
232         if (!l2cap_frame_get_u8((void *)frame, &value)) {
233                 print_text(COLOR_ERROR, "invalid size");
234                 return;
235         }
236
237         mask = print_bitfield(4, value, ccc_value_table);
238         if (mask)
239                 print_text(COLOR_WHITE_BG, "    Unknown fields (0x%2.2x)",
240                                                                 mask);
241 }
242
243 static void ccc_read(const struct l2cap_frame *frame)
244 {
245         print_ccc_value(frame);
246 }
247
248 static void ccc_write(const struct l2cap_frame *frame)
249 {
250         print_ccc_value(frame);
251 }
252
253 static bool print_ase_codec(const struct l2cap_frame *frame)
254 {
255         uint8_t codec_id;
256         uint16_t codec_cid, codec_vid;
257
258         if (!l2cap_frame_get_u8((void *)frame, &codec_id)) {
259                 print_text(COLOR_ERROR, "Codec: invalid size");
260                 return false;
261         }
262
263         packet_print_codec_id("    Codec", codec_id);
264
265         if (!l2cap_frame_get_le16((void *)frame, &codec_cid)) {
266                 print_text(COLOR_ERROR, "Codec Company ID: invalid size");
267                 return false;
268         }
269
270         if (!l2cap_frame_get_le16((void *)frame, &codec_vid)) {
271                 print_text(COLOR_ERROR, "Codec Vendor ID: invalid size");
272                 return false;
273         }
274
275         if (codec_id == 0xff) {
276                 print_field("    Codec Company ID: %s (0x%04x)",
277                                                 bt_compidtostr(codec_cid),
278                                                 codec_cid);
279                 print_field("    Codec Vendor ID: 0x%04x", codec_vid);
280         }
281
282         return true;
283 }
284
285 static bool print_ase_lv(const struct l2cap_frame *frame, const char *label,
286                         struct packet_ltv_decoder *decoder, size_t decoder_len)
287 {
288         struct bt_hci_lv_data *lv;
289
290         lv = l2cap_frame_pull((void *)frame, frame, sizeof(*lv));
291         if (!lv) {
292                 print_text(COLOR_ERROR, "%s: invalid size", label);
293                 return false;
294         }
295
296         if (!l2cap_frame_pull((void *)frame, frame, lv->len)) {
297                 print_text(COLOR_ERROR, "%s: invalid size", label);
298                 return false;
299         }
300
301         packet_print_ltv(label, lv->data, lv->len, decoder, decoder_len);
302
303         return true;
304 }
305
306 static bool print_ase_cc(const struct l2cap_frame *frame, const char *label,
307                         struct packet_ltv_decoder *decoder, size_t decoder_len)
308 {
309         return print_ase_lv(frame, label, decoder, decoder_len);
310 }
311
312 static const struct bitfield_data pac_context_table[] = {
313         {  0, "Unspecified (0x0001)"                    },
314         {  1, "Conversational (0x0002)"                 },
315         {  2, "Media (0x0004)"                          },
316         {  3, "Game (0x0008)"                           },
317         {  4, "Instructional (0x0010)"                  },
318         {  5, "Voice Assistants (0x0020)"               },
319         {  6, "Live (0x0040)"                           },
320         {  7, "Sound Effects (0x0080)"                  },
321         {  8, "Notifications (0x0100)"                  },
322         {  9, "Ringtone (0x0200)"                       },
323         {  10, "Alerts (0x0400)"                        },
324         {  11, "Emergency alarm (0x0800)"               },
325         {  12, "RFU (0x1000)"                           },
326         {  13, "RFU (0x2000)"                           },
327         {  14, "RFU (0x4000)"                           },
328         {  15, "RFU (0x8000)"                           },
329         { }
330 };
331
332 static void print_context(const struct l2cap_frame *frame, const char *label)
333 {
334         uint16_t value;
335         uint16_t mask;
336
337         if (!l2cap_frame_get_le16((void *)frame, &value)) {
338                 print_text(COLOR_ERROR, "    value: invalid size");
339                 goto done;
340         }
341
342         print_field("%s: 0x%4.4x", label, value);
343
344         mask = print_bitfield(8, value, pac_context_table);
345         if (mask)
346                 print_text(COLOR_WHITE_BG, "    Unknown fields (0x%4.4x)",
347                                                                 mask);
348
349 done:
350         if (frame->size)
351                 print_hex_field("    Data", frame->data, frame->size);
352 }
353
354 static void ase_decode_preferred_context(const uint8_t *data, uint8_t len)
355 {
356         struct l2cap_frame frame;
357
358         l2cap_frame_init(&frame, 0, 0, 0, 0, 0, 0, data, len);
359
360         print_context(&frame, "      Preferred Context");
361 }
362
363 static void ase_decode_context(const uint8_t *data, uint8_t len)
364 {
365         struct l2cap_frame frame;
366
367         l2cap_frame_init(&frame, 0, 0, 0, 0, 0, 0, data, len);
368
369         print_context(&frame, "      Context");
370 }
371
372 static void ase_decode_program_info(const uint8_t *data, uint8_t len)
373 {
374         struct l2cap_frame frame;
375         const char *str;
376
377         l2cap_frame_init(&frame, 0, 0, 0, 0, 0, 0, data, len);
378
379         str = l2cap_frame_pull(&frame, &frame, len);
380         if (!str) {
381                 print_text(COLOR_ERROR, "    value: invalid size");
382                 goto done;
383         }
384
385         print_field("      Program Info: %s", str);
386
387 done:
388         if (frame.size)
389                 print_hex_field("    Data", frame.data, frame.size);
390 }
391
392 static void ase_decode_language(const uint8_t *data, uint8_t len)
393 {
394         struct l2cap_frame frame;
395         uint32_t value;
396
397         l2cap_frame_init(&frame, 0, 0, 0, 0, 0, 0, data, len);
398
399         if (!l2cap_frame_get_le24(&frame, &value)) {
400                 print_text(COLOR_ERROR, "    value: invalid size");
401                 goto done;
402         }
403
404         print_field("      Language: 0x%6.6x", value);
405
406 done:
407         if (frame.size)
408                 print_hex_field("    Data", frame.data, frame.size);
409 }
410
411 struct packet_ltv_decoder ase_metadata_table[] = {
412         LTV_DEC(0x01, ase_decode_preferred_context),
413         LTV_DEC(0x02, ase_decode_context),
414         LTV_DEC(0x03, ase_decode_program_info),
415         LTV_DEC(0x04, ase_decode_language)
416 };
417
418 static bool print_ase_metadata(const struct l2cap_frame *frame)
419 {
420         return print_ase_lv(frame, "    Metadata", NULL, 0);
421 }
422
423 static const struct bitfield_data pac_freq_table[] = {
424         {  0, "8 Khz (0x0001)"                          },
425         {  1, "11.25 Khz (0x0002)"                      },
426         {  2, "16 Khz (0x0004)"                         },
427         {  3, "22.05 Khz (0x0008)"                      },
428         {  4, "24 Khz (0x0010)"                         },
429         {  5, "32 Khz (0x0020)"                         },
430         {  6, "44.1 Khz (0x0040)"                       },
431         {  7, "48 Khz (0x0080)"                         },
432         {  8, "88.2 Khz (0x0100)"                       },
433         {  9, "96 Khz (0x0200)"                         },
434         {  10, "176.4 Khz (0x0400)"                     },
435         {  11, "192 Khz (0x0800)"                       },
436         {  12, "384 Khz (0x1000)"                       },
437         {  13, "RFU (0x2000)"                           },
438         {  14, "RFU (0x4000)"                           },
439         {  15, "RFU (0x8000)"                           },
440         { }
441 };
442
443 static void pac_decode_freq(const uint8_t *data, uint8_t len)
444 {
445         struct l2cap_frame frame;
446         uint16_t value;
447         uint16_t mask;
448
449         l2cap_frame_init(&frame, 0, 0, 0, 0, 0, 0, data, len);
450
451         if (!l2cap_frame_get_le16(&frame, &value)) {
452                 print_text(COLOR_ERROR, "    value: invalid size");
453                 goto done;
454         }
455
456         print_field("      Sampling Frequencies: 0x%4.4x", value);
457
458         mask = print_bitfield(8, value, pac_freq_table);
459         if (mask)
460                 print_text(COLOR_WHITE_BG, "    Unknown fields (0x%4.4x)",
461                                                                 mask);
462
463 done:
464         if (frame.size)
465                 print_hex_field("    Data", frame.data, frame.size);
466 }
467
468 static const struct bitfield_data pac_duration_table[] = {
469         {  0, "7.5 ms (0x01)"                           },
470         {  1, "10 ms (0x02)"                            },
471         {  2, "RFU (0x04)"                              },
472         {  3, "RFU (0x08)"                              },
473         {  4, "7.5 ms preferred (0x10)"                 },
474         {  5, "10 ms preferred (0x20)"                  },
475         {  6, "RFU (0x40)"                              },
476         {  7, "RFU (0x80)"                              },
477         { }
478 };
479
480 static void pac_decode_duration(const uint8_t *data, uint8_t len)
481 {
482         struct l2cap_frame frame;
483         uint8_t value;
484         uint8_t mask;
485
486         l2cap_frame_init(&frame, 0, 0, 0, 0, 0, 0, data, len);
487
488         if (!l2cap_frame_get_u8(&frame, &value)) {
489                 print_text(COLOR_ERROR, "    value: invalid size");
490                 goto done;
491         }
492
493         print_field("      Frame Duration: 0x%4.4x", value);
494
495         mask = print_bitfield(8, value, pac_duration_table);
496         if (mask)
497                 print_text(COLOR_WHITE_BG, "    Unknown fields (0x%2.2x)",
498                                                                 mask);
499
500 done:
501         if (frame.size)
502                 print_hex_field("    Data", frame.data, frame.size);
503 }
504
505 static const struct bitfield_data pac_channel_table[] = {
506         {  0, "1 channel (0x01)"                        },
507         {  1, "2 channels (0x02)"                       },
508         {  2, "3 channels (0x04)"                       },
509         {  3, "4 chanenls (0x08)"                       },
510         {  4, "5 channels (0x10)"                       },
511         {  5, "6 channels (0x20)"                       },
512         {  6, "7 channels (0x40)"                       },
513         {  7, "8 channels (0x80)"                       },
514         { }
515 };
516
517 static void pac_decode_channels(const uint8_t *data, uint8_t len)
518 {
519         struct l2cap_frame frame;
520         uint8_t value;
521         uint8_t mask;
522
523         l2cap_frame_init(&frame, 0, 0, 0, 0, 0, 0, data, len);
524
525         if (!l2cap_frame_get_u8(&frame, &value)) {
526                 print_text(COLOR_ERROR, "    value: invalid size");
527                 goto done;
528         }
529
530         print_field("      Audio Channel Count: 0x%2.2x", value);
531
532         mask = print_bitfield(8, value, pac_channel_table);
533         if (mask)
534                 print_text(COLOR_WHITE_BG, "    Unknown fields (0x%2.2x)",
535                                                                 mask);
536
537 done:
538         if (frame.size)
539                 print_hex_field("    Data", frame.data, frame.size);
540 }
541
542 static void pac_decode_frame_length(const uint8_t *data, uint8_t len)
543 {
544         struct l2cap_frame frame;
545         uint16_t min, max;
546
547         l2cap_frame_init(&frame, 0, 0, 0, 0, 0, 0, data, len);
548
549         if (!l2cap_frame_get_le16(&frame, &min)) {
550                 print_text(COLOR_ERROR, "    min: invalid size");
551                 goto done;
552         }
553
554         if (!l2cap_frame_get_le16(&frame, &max)) {
555                 print_text(COLOR_ERROR, "    min: invalid size");
556                 goto done;
557         }
558
559         print_field("      Frame Length: %u (0x%4.4x) - %u (0x%4.4x)",
560                                                         min, min, max, max);
561
562 done:
563         if (frame.size)
564                 print_hex_field("    Data", frame.data, frame.size);
565 }
566
567 static void pac_decode_sdu(const uint8_t *data, uint8_t len)
568 {
569         struct l2cap_frame frame;
570         uint8_t value;
571
572         l2cap_frame_init(&frame, 0, 0, 0, 0, 0, 0, data, len);
573
574         if (!l2cap_frame_get_u8(&frame, &value)) {
575                 print_text(COLOR_ERROR, "    value: invalid size");
576                 goto done;
577         }
578
579         print_field("      Max SDU: %u (0x%2.2x)", value, value);
580
581 done:
582         if (frame.size)
583                 print_hex_field("    Data", frame.data, frame.size);
584 }
585
586 struct packet_ltv_decoder pac_cap_table[] = {
587         LTV_DEC(0x01, pac_decode_freq),
588         LTV_DEC(0x02, pac_decode_duration),
589         LTV_DEC(0x03, pac_decode_channels),
590         LTV_DEC(0x04, pac_decode_frame_length),
591         LTV_DEC(0x05, pac_decode_sdu)
592 };
593
594 static void print_pac(const struct l2cap_frame *frame)
595 {
596         uint8_t num = 0, i;
597
598         if (!l2cap_frame_get_u8((void *)frame, &num)) {
599                 print_text(COLOR_ERROR, "Number of PAC(s): invalid size");
600                 goto done;
601         }
602
603         print_field("  Number of PAC(s): %u", num);
604
605         for (i = 0; i < num; i++) {
606                 print_field("  PAC #%u:", i);
607
608                 if (!print_ase_codec(frame))
609                         goto done;
610
611                 if (!print_ase_cc(frame, "    Codec Specific Capabilities",
612                                 pac_cap_table, ARRAY_SIZE(pac_cap_table)))
613                         break;
614
615                 if (!print_ase_metadata(frame))
616                         break;
617         }
618
619 done:
620         if (frame->size)
621                 print_hex_field("  Data", frame->data, frame->size);
622 }
623
624 static void pac_read(const struct l2cap_frame *frame)
625 {
626         print_pac(frame);
627 }
628
629 static void pac_notify(const struct l2cap_frame *frame)
630 {
631         print_pac(frame);
632 }
633
634 static bool print_prefer_framing(const struct l2cap_frame *frame)
635 {
636         uint8_t framing;
637
638         if (!l2cap_frame_get_u8((void *)frame, &framing)) {
639                 print_text(COLOR_ERROR, "    Framing: invalid size");
640                 return false;
641         }
642
643         switch (framing) {
644         case 0x00:
645                 print_field("    Framing: Unframed PDUs supported (0x00)");
646                 break;
647         case 0x01:
648                 print_field("    Framing: Unframed PDUs not supported (0x01)");
649                 break;
650         default:
651                 print_field("    Framing: Reserved (0x%2.2x)", framing);
652                 break;
653         }
654
655         return true;
656 }
657
658 static const struct bitfield_data prefer_phy_table[] = {
659         {  0, "LE 1M PHY preffered (0x01)"              },
660         {  1, "LE 2M PHY preffered (0x02)"              },
661         {  2, "LE Codec PHY preffered (0x04)"           },
662         { }
663 };
664
665 static bool print_prefer_phy(const struct l2cap_frame *frame)
666 {
667         uint8_t phy, mask;
668
669         if (!l2cap_frame_get_u8((void *)frame, &phy)) {
670                 print_text(COLOR_ERROR, "PHY: invalid size");
671                 return false;
672         }
673
674         print_field("    PHY: 0x%2.2x", phy);
675
676         mask = print_bitfield(4, phy, prefer_phy_table);
677         if (mask)
678                 print_text(COLOR_WHITE_BG, "    Unknown fields (0x%2.2x)",
679                                                                 mask);
680
681         return true;
682 }
683
684 static bool print_ase_rtn(const struct l2cap_frame *frame, const char *label)
685 {
686         uint8_t rtn;
687
688         if (!l2cap_frame_get_u8((void *)frame, &rtn)) {
689                 print_text(COLOR_ERROR, "%s: invalid size", label);
690                 return false;
691         }
692
693         print_field("%s: %u", label, rtn);
694
695         return true;
696 }
697
698 static bool print_ase_latency(const struct l2cap_frame *frame,
699                                                 const char *label)
700 {
701         uint16_t latency;
702
703         if (!l2cap_frame_get_le16((void *)frame, &latency)) {
704                 print_text(COLOR_ERROR, "%s: invalid size", label);
705                 return false;
706         }
707
708         print_field("%s: %u", label, latency);
709
710         return true;
711 }
712
713 static bool print_ase_pd(const struct l2cap_frame *frame, const char *label)
714 {
715         uint32_t pd;
716
717         if (!l2cap_frame_get_le24((void *)frame, &pd)) {
718                 print_text(COLOR_ERROR, "%s: invalid size", label);
719                 return false;
720         }
721
722         print_field("%s: %u us", label, pd);
723
724         return true;
725 }
726
727 static void ase_decode_freq(const uint8_t *data, uint8_t len)
728 {
729         struct l2cap_frame frame;
730         uint8_t value;
731
732         l2cap_frame_init(&frame, 0, 0, 0, 0, 0, 0, data, len);
733
734         if (!l2cap_frame_get_u8(&frame, &value)) {
735                 print_text(COLOR_ERROR, "    value: invalid size");
736                 goto done;
737         }
738
739         switch (value) {
740         case 0x01:
741                 print_field("      Sampling Frequency: 8 Khz (0x01)");
742                 break;
743         case 0x02:
744                 print_field("      Sampling Frequency: 11.25 Khz (0x02)");
745                 break;
746         case 0x03:
747                 print_field("      Sampling Frequency: 16 Khz (0x03)");
748                 break;
749         case 0x04:
750                 print_field("      Sampling Frequency: 22.05 Khz (0x04)");
751                 break;
752         case 0x05:
753                 print_field("      Sampling Frequency: 24 Khz (0x04)");
754                 break;
755         case 0x06:
756                 print_field("      Sampling Frequency: 32 Khz (0x04)");
757                 break;
758         case 0x07:
759                 print_field("      Sampling Frequency: 44.1 Khz (0x04)");
760                 break;
761         case 0x08:
762                 print_field("      Sampling Frequency: 48 Khz (0x04)");
763                 break;
764         case 0x09:
765                 print_field("      Sampling Frequency: 88.2 Khz (0x04)");
766                 break;
767         case 0x0a:
768                 print_field("      Sampling Frequency: 96 Khz (0x04)");
769                 break;
770         case 0x0b:
771                 print_field("      Sampling Frequency: 176.4 Khz (0x04)");
772                 break;
773         case 0x0c:
774                 print_field("      Sampling Frequency: 192 Khz (0x04)");
775                 break;
776         case 0x0d:
777                 print_field("      Sampling Frequency: 384 Khz (0x04)");
778                 break;
779         default:
780                 print_field("      Sampling Frequency: RFU (0x%2.2x)", value);
781                 break;
782         }
783
784 done:
785         if (frame.size)
786                 print_hex_field("    Data", frame.data, frame.size);
787 }
788
789 static void ase_decode_duration(const uint8_t *data, uint8_t len)
790 {
791         struct l2cap_frame frame;
792         uint8_t value;
793
794         l2cap_frame_init(&frame, 0, 0, 0, 0, 0, 0, data, len);
795
796         if (!l2cap_frame_get_u8(&frame, &value)) {
797                 print_text(COLOR_ERROR, "    value: invalid size");
798                 goto done;
799         }
800
801         switch (value) {
802         case 0x00:
803                 print_field("      Frame Duration: 7.5 ms (0x00)");
804                 break;
805         case 0x01:
806                 print_field("      Frame Duration: 10 ms (0x01)");
807                 break;
808         default:
809                 print_field("      Frame Duration: RFU (0x%2.2x)", value);
810                 break;
811         }
812
813 done:
814         if (frame.size)
815                 print_hex_field("    Data", frame.data, frame.size);
816 }
817
818 static const struct bitfield_data channel_location_table[] = {
819         {  0, "Front Left (0x00000001)"                 },
820         {  1, "Front Right (0x00000002)"                },
821         {  2, "Front Center (0x00000004)"               },
822         {  3, "Low Frequency Effects 1 (0x00000008)"    },
823         {  4, "Back Left (0x00000010)"                  },
824         {  5, "Back Right (0x00000020)"                 },
825         {  6, "Front Left of Center (0x00000040)"       },
826         {  7, "Front Right of Center (0x00000080)"      },
827         {  8, "Back Center (0x00000100)"                },
828         {  9, "Low Frequency Effects 2 (0x00000200)"    },
829         {  10, "Side Left (0x00000400)"                 },
830         {  11, "Side Right (0x00000800)"                },
831         {  12, "Top Front Left (0x00001000)"            },
832         {  13, "Top Front Right (0x00002000)"           },
833         {  14, "Top Front Center (0x00004000)"          },
834         {  15, "Top Center (0x00008000)"                },
835         {  16, "Top Back Left (0x00010000)"             },
836         {  17, "Top Back Right (0x00020000)"            },
837         {  18, "Top Side Left (0x00040000)"             },
838         {  19, "Top Side Right (0x00080000)"            },
839         {  20, "Top Back Center (0x00100000)"           },
840         {  21, "Bottom Front Center (0x00200000)"       },
841         {  22, "Bottom Front Left (0x00400000)"         },
842         {  23, "Bottom Front Right (0x00800000)"        },
843         {  24, "Front Left Wide (0x01000000)"           },
844         {  25, "Front Right Wide (0x02000000)"          },
845         {  26, "Left Surround (0x04000000)"             },
846         {  27, "Right Surround (0x08000000)"            },
847         {  28, "RFU (0x10000000)"                       },
848         {  29, "RFU (0x20000000)"                       },
849         {  30, "RFU (0x40000000)"                       },
850         {  31, "RFU (0x80000000)"                       },
851         { }
852 };
853
854 static void print_location(const struct l2cap_frame *frame)
855 {
856         uint32_t value;
857         uint32_t mask;
858
859         if (!l2cap_frame_get_le32((void *)frame, &value)) {
860                 print_text(COLOR_ERROR, "    value: invalid size");
861                 goto done;
862         }
863
864         print_field("   Location: 0x%8.8x", value);
865
866         mask = print_bitfield(6, value, channel_location_table);
867         if (mask)
868                 print_text(COLOR_WHITE_BG, "    Unknown fields (0x%8.8x)",
869                                                                 mask);
870
871 done:
872         if (frame->size)
873                 print_hex_field("  Data", frame->data, frame->size);
874 }
875
876 static void ase_decode_location(const uint8_t *data, uint8_t len)
877 {
878         struct l2cap_frame frame;
879
880         l2cap_frame_init(&frame, 0, 0, 0, 0, 0, 0, data, len);
881
882         print_location(&frame);
883 }
884
885 static void ase_decode_frame_length(const uint8_t *data, uint8_t len)
886 {
887         struct l2cap_frame frame;
888         uint16_t value;
889
890         l2cap_frame_init(&frame, 0, 0, 0, 0, 0, 0, data, len);
891
892         if (!l2cap_frame_get_le16(&frame, &value)) {
893                 print_text(COLOR_ERROR, "    value: invalid size");
894                 goto done;
895         }
896
897         print_field("      Frame Length: %u (0x%4.4x)", value, value);
898
899 done:
900         if (frame.size)
901                 print_hex_field("    Data", frame.data, frame.size);
902 }
903
904 static void ase_decode_blocks(const uint8_t *data, uint8_t len)
905 {
906         struct l2cap_frame frame;
907         uint8_t value;
908
909         l2cap_frame_init(&frame, 0, 0, 0, 0, 0, 0, data, len);
910
911         if (!l2cap_frame_get_u8(&frame, &value)) {
912                 print_text(COLOR_ERROR, "    value: invalid size");
913                 goto done;
914         }
915
916         print_field("      Frame Blocks per SDU: %u (0x%2.2x)", value, value);
917
918 done:
919         if (frame.size)
920                 print_hex_field("    Data", frame.data, frame.size);
921 }
922
923 struct packet_ltv_decoder ase_cc_table[] = {
924         LTV_DEC(0x01, ase_decode_freq),
925         LTV_DEC(0x02, ase_decode_duration),
926         LTV_DEC(0x03, ase_decode_location),
927         LTV_DEC(0x04, ase_decode_frame_length),
928         LTV_DEC(0x05, ase_decode_blocks)
929 };
930
931 static void print_ase_config(const struct l2cap_frame *frame)
932 {
933         if (!print_prefer_framing(frame))
934                 return;
935
936         if (!print_prefer_phy(frame))
937                 return;
938
939         if (!print_ase_rtn(frame, "    RTN"))
940                 return;
941
942         if (!print_ase_latency(frame, "    Max Transport Latency"))
943                 return;
944
945         if (!print_ase_pd(frame, "    Presentation Delay Min"))
946                 return;
947
948         if (!print_ase_pd(frame, "    Presentation Delay Max"))
949                 return;
950
951         if (!print_ase_pd(frame, "    Preferred Presentation Delay Min"))
952                 return;
953
954         if (!print_ase_pd(frame, "    Preferred Presentation Delay Max"))
955                 return;
956
957         if (!print_ase_codec(frame))
958                 return;
959
960         print_ase_cc(frame, "    Codec Specific Configuration",
961                         ase_cc_table, ARRAY_SIZE(ase_cc_table));
962 }
963
964 static bool print_ase_framing(const struct l2cap_frame *frame,
965                                                 const char *label)
966 {
967         uint8_t framing;
968
969         if (!l2cap_frame_get_u8((void *)frame, &framing)) {
970                 print_text(COLOR_ERROR, "%s: invalid size", label);
971                 return false;
972         }
973
974         switch (framing) {
975         case 0x00:
976                 print_field("%s: Unframed (0x00)", label);
977                 break;
978         case 0x01:
979                 print_field("%s: Framed (0x01)", label);
980                 break;
981         default:
982                 print_field("%s: Reserved (0x%2.2x)", label, framing);
983         }
984
985         return true;
986 }
987
988 static const struct bitfield_data phy_table[] = {
989         {  0, "LE 1M PHY (0x01)"                },
990         {  1, "LE 2M PHY (0x02)"                },
991         {  2, "LE Codec PHY (0x04)"             },
992         { }
993 };
994
995 static bool print_ase_phy(const struct l2cap_frame *frame, const char *label)
996 {
997         uint8_t phy, mask;
998
999         if (!l2cap_frame_get_u8((void *)frame, &phy)) {
1000                 print_text(COLOR_ERROR, "%s: invalid size", label);
1001                 return false;
1002         }
1003
1004         print_field("%s: 0x%2.2x", label, phy);
1005
1006         mask = print_bitfield(4, phy, phy_table);
1007         if (mask)
1008                 print_text(COLOR_WHITE_BG, "    Unknown fields (0x%2.2x)",
1009                                                                 mask);
1010
1011         return true;
1012 }
1013
1014 static bool print_ase_interval(const struct l2cap_frame *frame,
1015                                                 const char *label)
1016 {
1017         uint32_t interval;
1018
1019         if (!l2cap_frame_get_le24((void *)frame, &interval)) {
1020                 print_text(COLOR_ERROR, "%s: invalid size", label);
1021                 return false;
1022         }
1023
1024         print_field("%s: %u usec", label, interval);
1025
1026         return true;
1027 }
1028
1029 static bool print_ase_sdu(const struct l2cap_frame *frame, const char *label)
1030 {
1031         uint16_t sdu;
1032
1033         if (!l2cap_frame_get_le16((void *)frame, &sdu)) {
1034                 print_text(COLOR_ERROR, "%s: invalid size", label);
1035                 return false;
1036         }
1037
1038         print_field("%s: %u", label, sdu);
1039
1040         return true;
1041 }
1042
1043 static void print_ase_qos(const struct l2cap_frame *frame)
1044 {
1045         if (!l2cap_frame_print_u8((void *)frame, "    CIG ID"))
1046                 return;
1047
1048         if (!l2cap_frame_print_u8((void *)frame, "    CIS ID"))
1049                 return;
1050
1051         if (!print_ase_interval(frame, "    SDU Interval"))
1052                 return;
1053
1054         if (!print_ase_framing(frame, "    Framing"))
1055                 return;
1056
1057         if (!print_ase_phy(frame, "    PHY"))
1058                 return;
1059
1060         if (!print_ase_sdu(frame, "    Max SDU"))
1061                 return;
1062
1063         if (!print_ase_rtn(frame, "    RTN"))
1064                 return;
1065
1066         if (!print_ase_latency(frame, "    Max Transport Latency"))
1067                 return;
1068
1069         print_ase_pd(frame, "    Presentation Delay");
1070 }
1071
1072 static void print_ase_metadata_status(const struct l2cap_frame *frame)
1073 {
1074         if (!l2cap_frame_print_u8((void *)frame, "    CIG ID"))
1075                 return;
1076
1077         if (!l2cap_frame_print_u8((void *)frame, "    CIS ID"))
1078                 return;
1079
1080         print_ase_metadata(frame);
1081 }
1082
1083 static void print_ase_status(const struct l2cap_frame *frame)
1084 {
1085         uint8_t id, state;
1086
1087         if (!l2cap_frame_get_u8((void *)frame, &id)) {
1088                 print_text(COLOR_ERROR, "ASE ID: invalid size");
1089                 goto done;
1090         }
1091
1092         print_field("    ASE ID: %u", id);
1093
1094         if (!l2cap_frame_get_u8((void *)frame, &state)) {
1095                 print_text(COLOR_ERROR, "ASE State: invalid size");
1096                 goto done;
1097         }
1098
1099         switch (state) {
1100         /* ASE_State = 0x00 (Idle) */
1101         case 0x00:
1102                 print_field("    State: Idle (0x00)");
1103                 break;
1104         /* ASE_State = 0x01 (Codec Configured) */
1105         case 0x01:
1106                 print_field("    State: Codec Configured (0x01)");
1107                 print_ase_config(frame);
1108                 break;
1109         /* ASE_State = 0x02 (QoS Configured) */
1110         case 0x02:
1111                 print_field("    State: QoS Configured (0x02)");
1112                 print_ase_qos(frame);
1113                 break;
1114         /* ASE_Status = 0x03 (Enabling) */
1115         case 0x03:
1116                 print_field("    State: Enabling (0x03)");
1117                 print_ase_metadata_status(frame);
1118                 break;
1119         /* ASE_Status = 0x04 (Streaming) */
1120         case 0x04:
1121                 print_field("    State: Streaming (0x04)");
1122                 print_ase_metadata_status(frame);
1123                 break;
1124         /* ASE_Status = 0x05 (Disabling) */
1125         case 0x05:
1126                 print_field("    State: Disabling (0x05)");
1127                 print_ase_metadata_status(frame);
1128                 break;
1129         /* ASE_Status = 0x06 (Releasing) */
1130         case 0x06:
1131                 print_field("    State: Releasing (0x06)");
1132                 break;
1133         default:
1134                 print_field("    State: Reserved (0x%2.2x)", state);
1135                 break;
1136         }
1137
1138 done:
1139         if (frame->size)
1140                 print_hex_field("  Data", frame->data, frame->size);
1141 }
1142
1143 static void ase_read(const struct l2cap_frame *frame)
1144 {
1145         print_ase_status(frame);
1146 }
1147
1148 static void ase_notify(const struct l2cap_frame *frame)
1149 {
1150         print_ase_status(frame);
1151 }
1152
1153 static bool print_ase_target_latency(const struct l2cap_frame *frame)
1154 {
1155         uint8_t latency;
1156
1157         if (!l2cap_frame_get_u8((void *)frame, &latency)) {
1158                 print_text(COLOR_ERROR, "    Target Latency: invalid size");
1159                 return false;
1160         }
1161
1162         switch (latency) {
1163         case 0x01:
1164                 print_field("    Target Latency: Low Latency (0x01)");
1165                 break;
1166         case 0x02:
1167                 print_field("    Target Latency: Balance Latency/Reliability "
1168                                                                 "(0x02)");
1169                 break;
1170         case 0x03:
1171                 print_field("    Target Latency: High Reliability (0x03)");
1172                 break;
1173         default:
1174                 print_field("    Target Latency: Reserved (0x%2.2x)", latency);
1175                 break;
1176         }
1177
1178         return true;
1179 }
1180
1181 static bool ase_config_cmd(const struct l2cap_frame *frame)
1182 {
1183         if (!l2cap_frame_print_u8((void *)frame, "    ASE ID"))
1184                 return false;
1185
1186         if (!print_ase_target_latency(frame))
1187                 return false;
1188
1189         if (!print_ase_phy(frame, "    PHY"))
1190                 return false;
1191
1192         if (!print_ase_codec(frame))
1193                 return false;
1194
1195         if (!print_ase_cc(frame, "    Codec Specific Configuration",
1196                                 ase_cc_table, ARRAY_SIZE(ase_cc_table)))
1197                 return false;
1198
1199         return true;
1200 }
1201
1202 static bool ase_qos_cmd(const struct l2cap_frame *frame)
1203 {
1204         if (!l2cap_frame_print_u8((void *)frame, "    ASE ID"))
1205                 return false;
1206
1207         if (!l2cap_frame_print_u8((void *)frame, "    CIG ID"))
1208                 return false;
1209
1210         if (!l2cap_frame_print_u8((void *)frame, "    CIS ID"))
1211                 return false;
1212
1213         if (!print_ase_interval(frame, "    SDU Interval"))
1214                 return false;
1215
1216         if (!print_ase_framing(frame, "    Framing"))
1217                 return false;
1218
1219         if (!print_ase_phy(frame, "    PHY"))
1220                 return false;
1221
1222         if (!print_ase_sdu(frame, "    Max SDU"))
1223                 return false;
1224
1225         if (!print_ase_rtn(frame, "    RTN"))
1226                 return false;
1227
1228         if (!print_ase_latency(frame, "    Max Transport Latency"))
1229                 return false;
1230
1231         if (!print_ase_pd(frame, "    Presentation Delay"))
1232                 return false;
1233
1234         return true;
1235 }
1236
1237 static bool ase_enable_cmd(const struct l2cap_frame *frame)
1238 {
1239         if (!l2cap_frame_print_u8((void *)frame, "    ASE ID"))
1240                 return false;
1241
1242         if (!print_ase_metadata(frame))
1243                 return false;
1244
1245         return true;
1246 }
1247
1248 static bool ase_start_cmd(const struct l2cap_frame *frame)
1249 {
1250         if (!l2cap_frame_print_u8((void *)frame, "    ASE ID"))
1251                 return false;
1252
1253         return true;
1254 }
1255
1256 static bool ase_disable_cmd(const struct l2cap_frame *frame)
1257 {
1258         if (!l2cap_frame_print_u8((void *)frame, "    ASE ID"))
1259                 return false;
1260
1261         return true;
1262 }
1263
1264 static bool ase_stop_cmd(const struct l2cap_frame *frame)
1265 {
1266         if (!l2cap_frame_print_u8((void *)frame, "    ASE ID"))
1267                 return false;
1268
1269         return true;
1270 }
1271
1272 static bool ase_metadata_cmd(const struct l2cap_frame *frame)
1273 {
1274         if (!l2cap_frame_print_u8((void *)frame, "    ASE ID"))
1275                 return false;
1276
1277         if (!print_ase_metadata(frame))
1278                 return false;
1279
1280         return true;
1281 }
1282
1283 static bool ase_release_cmd(const struct l2cap_frame *frame)
1284 {
1285         if (!l2cap_frame_print_u8((void *)frame, "    ASE ID"))
1286                 return false;
1287
1288         return true;
1289 }
1290
1291 #define ASE_CMD(_op, _desc, _func) \
1292 [_op] = { \
1293         .desc = _desc, \
1294         .func = _func, \
1295 }
1296
1297 struct ase_cmd {
1298         const char *desc;
1299         bool (*func)(const struct l2cap_frame *frame);
1300 } ase_cmd_table[] = {
1301         /* Opcode = 0x01 (Codec Configuration) */
1302         ASE_CMD(0x01, "Codec Configuration", ase_config_cmd),
1303         /* Opcode = 0x02 (QoS Configuration) */
1304         ASE_CMD(0x02, "QoS Configuration", ase_qos_cmd),
1305         /* Opcode = 0x03 (Enable) */
1306         ASE_CMD(0x03, "Enable", ase_enable_cmd),
1307         /* Opcode = 0x04 (Receiver Start Ready) */
1308         ASE_CMD(0x04, "Receiver Start Ready", ase_start_cmd),
1309         /* Opcode = 0x05 (Disable) */
1310         ASE_CMD(0x05, "Disable", ase_disable_cmd),
1311         /* Opcode = 0x06 (Receiver Stop Ready) */
1312         ASE_CMD(0x06, "Receiver Stop Ready", ase_stop_cmd),
1313         /* Opcode = 0x07 (Update Metadata) */
1314         ASE_CMD(0x07, "Update Metadata", ase_metadata_cmd),
1315         /* Opcode = 0x08 (Release) */
1316         ASE_CMD(0x08, "Release", ase_release_cmd),
1317 };
1318
1319 static struct ase_cmd *ase_get_cmd(uint8_t op)
1320 {
1321         if (op > ARRAY_SIZE(ase_cmd_table))
1322                 return NULL;
1323
1324         return &ase_cmd_table[op];
1325 }
1326
1327 static void print_ase_cmd(const struct l2cap_frame *frame)
1328 {
1329         uint8_t op, num, i;
1330         struct ase_cmd *cmd;
1331
1332         if (!l2cap_frame_get_u8((void *)frame, &op)) {
1333                 print_text(COLOR_ERROR, "opcode: invalid size");
1334                 goto done;
1335         }
1336
1337         if (!l2cap_frame_get_u8((void *)frame, &num)) {
1338                 print_text(COLOR_ERROR, "num: invalid size");
1339                 goto done;
1340         }
1341
1342         cmd = ase_get_cmd(op);
1343         if (!cmd) {
1344                 print_field("    Opcode: Reserved (0x%2.2x)", op);
1345                 goto done;
1346         }
1347
1348         print_field("    Opcode: %s (0x%2.2x)", cmd->desc, op);
1349         print_field("    Number of ASE(s): %u", num);
1350
1351         for (i = 0; i < num && frame->size; i++) {
1352                 print_field("    ASE: #%u", i);
1353
1354                 if (!cmd->func(frame))
1355                         break;
1356         }
1357
1358 done:
1359         if (frame->size)
1360                 print_hex_field("  Data", frame->data, frame->size);
1361 }
1362
1363 static void ase_cp_write(const struct l2cap_frame *frame)
1364 {
1365         print_ase_cmd(frame);
1366 }
1367
1368 static bool print_ase_cp_rsp_code(const struct l2cap_frame *frame)
1369 {
1370         uint8_t code;
1371
1372         if (!l2cap_frame_get_u8((void *)frame, &code)) {
1373                 print_text(COLOR_ERROR, "    ASE Response Code: invalid size");
1374                 return false;
1375         }
1376
1377         switch (code) {
1378         case 0x00:
1379                 print_field("    ASE Response Code: Success (0x00)");
1380                 break;
1381         case 0x01:
1382                 print_field("    ASE Response Code: Unsupported Opcode (0x01)");
1383                 break;
1384         case 0x02:
1385                 print_field("    ASE Response Code: Invalid Length (0x02)");
1386                 break;
1387         case 0x03:
1388                 print_field("    ASE Response Code: Invalid ASE ID (0x03)");
1389                 break;
1390         case 0x04:
1391                 print_field("    ASE Response Code: Invalid ASE State (0x04)");
1392                 break;
1393         case 0x05:
1394                 print_field("    ASE Response Code: Invalid ASE Direction "
1395                                                                 "(0x05)");
1396                 break;
1397         case 0x06:
1398                 print_field("    ASE Response Code: Unsupported Audio "
1399                                                         "Capabilities (0x06)");
1400                 break;
1401         case 0x07:
1402                 print_field("    ASE Response Code: Unsupported Configuration "
1403                                                                 "(0x07)");
1404                 break;
1405         case 0x08:
1406                 print_field("    ASE Response Code: Rejected Configuration "
1407                                                                 "(0x08)");
1408                 break;
1409         case 0x09:
1410                 print_field("    ASE Response Code: Invalid Configuration "
1411                                                                 "(0x09)");
1412                 break;
1413         case 0x0a:
1414                 print_field("    ASE Response Code: Unsupported Metadata "
1415                                                                 "(0x0a)");
1416                 break;
1417         case 0x0b:
1418                 print_field("    ASE Response Code: Rejected Metadata (0x0b)");
1419                 break;
1420         case 0x0c:
1421                 print_field("    ASE Response Code: Invalid Metadata (0x0c)");
1422                 break;
1423         case 0x0d:
1424                 print_field("    ASE Response Code: Insufficient Resources "
1425                                                                 "(0x0d)");
1426                 break;
1427         case 0x0e:
1428                 print_field("    ASE Response Code: Unspecified Error (0x0e)");
1429                 break;
1430         default:
1431                 print_field("    ASE Response Code: Reserved (0x%2.2x)", code);
1432                 break;
1433         }
1434
1435         return true;
1436 }
1437
1438 static bool print_ase_cp_rsp_reason(const struct l2cap_frame *frame)
1439 {
1440         uint8_t reason;
1441
1442         if (!l2cap_frame_get_u8((void *)frame, &reason)) {
1443                 print_text(COLOR_ERROR,
1444                                 "    ASE Response Reason: invalid size");
1445                 return false;
1446         }
1447
1448         switch (reason) {
1449         case 0x00:
1450                 print_field("    ASE Response Reason: None (0x00)");
1451                 break;
1452         case 0x01:
1453                 print_field("    ASE Response Reason: ASE ID (0x01)");
1454                 break;
1455         case 0x02:
1456                 print_field("    ASE Response Reason: Codec Specific "
1457                                                 "Configuration (0x02)");
1458                 break;
1459         case 0x03:
1460                 print_field("    ASE Response Reason: SDU Interval (0x03)");
1461                 break;
1462         case 0x04:
1463                 print_field("    ASE Response Reason: Framing (0x04)");
1464                 break;
1465         case 0x05:
1466                 print_field("    ASE Response Reason: PHY (0x05)");
1467                 break;
1468         case 0x06:
1469                 print_field("    ASE Response Reason: Max SDU (0x06)");
1470                 break;
1471         case 0x07:
1472                 print_field("    ASE Response Reason: RTN (0x07)");
1473                 break;
1474         case 0x08:
1475                 print_field("    ASE Response Reason: Max Transport Latency "
1476                                                                 "(0x08)");
1477                 break;
1478         case 0x09:
1479                 print_field("    ASE Response Reason: Presentation Delay "
1480                                                                 "(0x09)");
1481                 break;
1482         case 0x0a:
1483                 print_field("    ASE Response Reason: Invalid ASE/CIS Mapping "
1484                                                                 "(0x0a)");
1485                 break;
1486         default:
1487                 print_field("    ASE Response Reason: Reserved (0x%2.2x)",
1488                                                                 reason);
1489                 break;
1490         }
1491
1492         return true;
1493 }
1494
1495 static void print_ase_cp_rsp(const struct l2cap_frame *frame)
1496 {
1497         uint8_t op, num, i;
1498         struct ase_cmd *cmd;
1499
1500         if (!l2cap_frame_get_u8((void *)frame, &op)) {
1501                 print_text(COLOR_ERROR, "    opcode: invalid size");
1502                 goto done;
1503         }
1504
1505         if (!l2cap_frame_get_u8((void *)frame, &num)) {
1506                 print_text(COLOR_ERROR, "    Number of ASE(s): invalid size");
1507                 goto done;
1508         }
1509
1510         cmd = ase_get_cmd(op);
1511         if (!cmd) {
1512                 print_field("    Opcode: Reserved (0x%2.2x)", op);
1513                 goto done;
1514         }
1515
1516         print_field("    Opcode: %s (0x%2.2x)", cmd->desc, op);
1517         print_field("    Number of ASE(s): %u", num);
1518
1519         for (i = 0; i < num && frame->size; i++) {
1520                 print_field("    ASE: #%u", i);
1521
1522                 if (!l2cap_frame_print_u8((void *)frame, "    ASE ID"))
1523                         break;
1524
1525                 if (!print_ase_cp_rsp_code(frame))
1526                         break;
1527
1528                 if (!print_ase_cp_rsp_reason(frame))
1529                         break;
1530         }
1531
1532 done:
1533         if (frame->size)
1534                 print_hex_field("  Data", frame->data, frame->size);
1535 }
1536
1537 static void ase_cp_notify(const struct l2cap_frame *frame)
1538 {
1539         print_ase_cp_rsp(frame);
1540 }
1541
1542 static void pac_loc_read(const struct l2cap_frame *frame)
1543 {
1544         print_location(frame);
1545 }
1546
1547 static void pac_loc_notify(const struct l2cap_frame *frame)
1548 {
1549         print_location(frame);
1550 }
1551
1552 static void print_pac_context(const struct l2cap_frame *frame)
1553 {
1554         uint16_t snk, src;
1555         uint16_t mask;
1556
1557         if (!l2cap_frame_get_le16((void *)frame, &snk)) {
1558                 print_text(COLOR_ERROR, "  sink: invalid size");
1559                 goto done;
1560         }
1561
1562         print_field("  Sink Context: 0x%4.4x", snk);
1563
1564         mask = print_bitfield(4, snk, pac_context_table);
1565         if (mask)
1566                 print_text(COLOR_WHITE_BG, "  Unknown fields (0x%4.4x)",
1567                                                                 mask);
1568
1569         if (!l2cap_frame_get_le16((void *)frame, &src)) {
1570                 print_text(COLOR_ERROR, "  source: invalid size");
1571                 goto done;
1572         }
1573
1574         print_field("  Source Context: 0x%4.4x", src);
1575
1576         mask = print_bitfield(4, src, pac_context_table);
1577         if (mask)
1578                 print_text(COLOR_WHITE_BG, "  Unknown fields (0x%4.4x)",
1579                                                                 mask);
1580
1581 done:
1582         if (frame->size)
1583                 print_hex_field("  Data", frame->data, frame->size);
1584 }
1585
1586 static void pac_context_read(const struct l2cap_frame *frame)
1587 {
1588         print_pac_context(frame);
1589 }
1590
1591 static void pac_context_notify(const struct l2cap_frame *frame)
1592 {
1593         print_pac_context(frame);
1594 }
1595
1596 static void print_vcs_state(const struct l2cap_frame *frame)
1597 {
1598         uint8_t vol_set, mute, chng_ctr;
1599
1600         if (!l2cap_frame_get_u8((void *)frame, &vol_set)) {
1601                 print_text(COLOR_ERROR, "Volume Settings: invalid size");
1602                 goto done;
1603         }
1604         print_field("    Volume Setting: %u", vol_set);
1605
1606         if (!l2cap_frame_get_u8((void *)frame, &mute)) {
1607                 print_text(COLOR_ERROR, "Mute Filed: invalid size");
1608                 goto done;
1609         }
1610
1611         switch (mute) {
1612         case 0x00:
1613                 print_field("    Not Muted: %u", mute);
1614                 break;
1615         case 0x01:
1616                 print_field("    Muted: %u", mute);
1617                 break;
1618         default:
1619                 print_field("    Unknown Mute Value: %u", mute);
1620                 break;
1621         }
1622
1623         if (!l2cap_frame_get_u8((void *)frame, &chng_ctr)) {
1624                 print_text(COLOR_ERROR, "Change Counter: invalid size");
1625                 goto done;
1626         }
1627         print_field("    Change Counter: %u", chng_ctr);
1628
1629 done:
1630         if (frame->size)
1631                 print_hex_field("  Data", frame->data, frame->size);
1632 }
1633
1634 static void vol_state_read(const struct l2cap_frame *frame)
1635 {
1636         print_vcs_state(frame);
1637 }
1638
1639 static void vol_state_notify(const struct l2cap_frame *frame)
1640 {
1641         print_vcs_state(frame);
1642 }
1643
1644 static bool vcs_config_cmd(const struct l2cap_frame *frame)
1645 {
1646         if (!l2cap_frame_print_u8((void *)frame, "    Change Counter"))
1647                 return false;
1648
1649         return true;
1650 }
1651
1652 static bool vcs_absolute_cmd(const struct l2cap_frame *frame)
1653 {
1654         if (!l2cap_frame_print_u8((void *)frame, "    Change Counter"))
1655                 return false;
1656
1657         if (!l2cap_frame_print_u8((void *)frame, "    Volume Setting"))
1658                 return false;
1659
1660         return true;
1661 }
1662
1663 #define VCS_CMD(_op, _desc, _func) \
1664 [_op] = { \
1665         .desc = _desc, \
1666         .func = _func, \
1667 }
1668
1669 struct vcs_cmd {
1670         const char *desc;
1671         bool (*func)(const struct l2cap_frame *frame);
1672 } vcs_cmd_table[] = {
1673         /* Opcode = 0x00 (Relative Volume Down) */
1674         VCS_CMD(0x00, "Relative Volume Down", vcs_config_cmd),
1675         /* Opcode = 0x01 (Relative Volume Up) */
1676         VCS_CMD(0x01, "Relative Volume Up", vcs_config_cmd),
1677         /* Opcode = 0x02 (Unmute/Relative Volume Down) */
1678         VCS_CMD(0x02, "Unmute/Relative Volume Down", vcs_config_cmd),
1679         /* Opcode = 0x03 (Unmute/Relative Volume Up) */
1680         VCS_CMD(0x03, "Unmute/Relative Volume Up", vcs_config_cmd),
1681         /* Opcode = 0x04 (Set Absolute Volume) */
1682         VCS_CMD(0x04, "Set Absolute Volume", vcs_absolute_cmd),
1683         /* Opcode = 0x05 (Unmute) */
1684         VCS_CMD(0x05, "Unmute", vcs_config_cmd),
1685         /* Opcode = 0x06 (Mute) */
1686         VCS_CMD(0x06, "Mute", vcs_config_cmd),
1687 };
1688
1689 static struct vcs_cmd *vcs_get_cmd(uint8_t op)
1690 {
1691         if (op > ARRAY_SIZE(vcs_cmd_table))
1692                 return NULL;
1693
1694         return &vcs_cmd_table[op];
1695 }
1696
1697 static void print_vcs_cmd(const struct l2cap_frame *frame)
1698 {
1699         uint8_t op;
1700         struct vcs_cmd *cmd;
1701
1702         if (!l2cap_frame_get_u8((void *)frame, &op)) {
1703                 print_text(COLOR_ERROR, "opcode: invalid size");
1704                 goto done;
1705         }
1706
1707         cmd = vcs_get_cmd(op);
1708         if (!cmd) {
1709                 print_field("    Opcode: Reserved (0x%2.2x)", op);
1710                 goto done;
1711         }
1712
1713         print_field("    Opcode: %s (0x%2.2x)", cmd->desc, op);
1714         if (!cmd->func(frame))
1715                 print_field("    Unknown Opcode");
1716
1717 done:
1718         if (frame->size)
1719                 print_hex_field("  Data", frame->data, frame->size);
1720 }
1721
1722 static void vol_cp_write(const struct l2cap_frame *frame)
1723 {
1724         print_vcs_cmd(frame);
1725 }
1726
1727 static void print_vcs_flag(const struct l2cap_frame *frame)
1728 {
1729         uint8_t vol_flag;
1730
1731         if (!l2cap_frame_get_u8((void *)frame, &vol_flag)) {
1732                 print_text(COLOR_ERROR, "Volume Flag: invalid size");
1733                 goto done;
1734         }
1735         print_field("    Volume Falg: %u", vol_flag);
1736
1737 done:
1738         if (frame->size)
1739                 print_hex_field("  Data", frame->data, frame->size);
1740 }
1741
1742 static void vol_flag_read(const struct l2cap_frame *frame)
1743 {
1744         print_vcs_flag(frame);
1745 }
1746
1747 static void vol_flag_notify(const struct l2cap_frame *frame)
1748 {
1749         print_vcs_flag(frame);
1750 }
1751
1752 static char *name2utf8(const uint8_t *name, uint16_t len)
1753 {
1754         char utf8_name[HCI_MAX_NAME_LENGTH + 2];
1755         int i;
1756
1757         if (g_utf8_validate((const char *) name, len, NULL))
1758                 return g_strndup((char *) name, len);
1759
1760         len = MIN(len, sizeof(utf8_name) - 1);
1761
1762         memset(utf8_name, 0, sizeof(utf8_name));
1763         strncpy(utf8_name, (char *) name, len);
1764
1765         /* Assume ASCII, and replace all non-ASCII with spaces */
1766         for (i = 0; utf8_name[i] != '\0'; i++) {
1767                 if (!isascii(utf8_name[i]))
1768                         utf8_name[i] = ' ';
1769         }
1770
1771         /* Remove leading and trailing whitespace characters */
1772         g_strstrip(utf8_name);
1773
1774         return g_strdup(utf8_name);
1775 }
1776
1777 static void print_mp_name(const struct l2cap_frame *frame)
1778 {
1779         char *name;
1780
1781         name = name2utf8((uint8_t *)frame->data, frame->size);
1782
1783         print_field("  Media Player Name: %s", name);
1784 }
1785
1786 static void mp_name_read(const struct l2cap_frame *frame)
1787 {
1788         print_mp_name(frame);
1789 }
1790
1791 static void mp_name_notify(const struct l2cap_frame *frame)
1792 {
1793         print_mp_name(frame);
1794 }
1795
1796 static void print_track_changed(const struct l2cap_frame *frame)
1797 {
1798         print_field("  Track Changed");
1799 }
1800
1801 static void track_changed_notify(const struct l2cap_frame *frame)
1802 {
1803         print_track_changed(frame);
1804 }
1805
1806 static void print_track_title(const struct l2cap_frame *frame)
1807 {
1808         char *name;
1809
1810         name = name2utf8((uint8_t *)frame->data, frame->size);
1811
1812         print_field("  Track Title: %s", name);
1813 }
1814
1815 static void track_title_read(const struct l2cap_frame *frame)
1816 {
1817         print_track_title(frame);
1818 }
1819
1820 static void track_title_notify(const struct l2cap_frame *frame)
1821 {
1822         print_track_title(frame);
1823 }
1824
1825 static void print_track_duration(const struct l2cap_frame *frame)
1826 {
1827         int32_t duration;
1828
1829         if (!l2cap_frame_get_le32((void *)frame, (uint32_t *)&duration)) {
1830                 print_text(COLOR_ERROR, "  Track Duration: invalid size");
1831                 goto done;
1832         }
1833
1834         print_field("  Track Duration: %u", duration);
1835
1836 done:
1837         if (frame->size)
1838                 print_hex_field("  Data", frame->data, frame->size);
1839 }
1840
1841 static void track_duration_read(const struct l2cap_frame *frame)
1842 {
1843         print_track_duration(frame);
1844 }
1845
1846 static void track_duration_notify(const struct l2cap_frame *frame)
1847 {
1848         print_track_duration(frame);
1849 }
1850
1851 static void print_track_position(const struct l2cap_frame *frame)
1852 {
1853         int32_t position;
1854
1855         if (!l2cap_frame_get_le32((void *)frame, (uint32_t *)&position)) {
1856                 print_text(COLOR_ERROR, "  Track Position: invalid size");
1857                 goto done;
1858         }
1859
1860         print_field("  Track Position: %u", position);
1861
1862 done:
1863         if (frame->size)
1864                 print_hex_field("  Data", frame->data, frame->size);
1865 }
1866
1867 static void track_position_read(const struct l2cap_frame *frame)
1868 {
1869         print_track_position(frame);
1870 }
1871
1872 static void track_position_write(const struct l2cap_frame *frame)
1873 {
1874         print_track_position(frame);
1875 }
1876
1877 static void track_position_notify(const struct l2cap_frame *frame)
1878 {
1879         print_track_position(frame);
1880 }
1881
1882 static void print_playback_speed(const struct l2cap_frame *frame)
1883 {
1884         int8_t playback_speed;
1885
1886         if (!l2cap_frame_get_u8((void *)frame, (uint8_t *)&playback_speed)) {
1887                 print_text(COLOR_ERROR, "  Playback Speed: invalid size");
1888                 goto done;
1889         }
1890
1891         print_field("  Playback Speed: %u", playback_speed);
1892
1893 done:
1894         if (frame->size)
1895                 print_hex_field("  Data", frame->data, frame->size);
1896 }
1897
1898 static void playback_speed_read(const struct l2cap_frame *frame)
1899 {
1900         print_playback_speed(frame);
1901 }
1902
1903 static void playback_speed_write(const struct l2cap_frame *frame)
1904 {
1905         print_playback_speed(frame);
1906 }
1907
1908 static void playback_speed_notify(const struct l2cap_frame *frame)
1909 {
1910         print_playback_speed(frame);
1911 }
1912
1913 static void print_seeking_speed(const struct l2cap_frame *frame)
1914 {
1915         int8_t seeking_speed;
1916
1917         if (!l2cap_frame_get_u8((void *)frame, (uint8_t *)&seeking_speed)) {
1918                 print_text(COLOR_ERROR, "  Seeking Speed: invalid size");
1919                 goto done;
1920         }
1921
1922         print_field("  Seeking Speed: %u", seeking_speed);
1923
1924 done:
1925         if (frame->size)
1926                 print_hex_field("  Data", frame->data, frame->size);
1927 }
1928
1929 static void seeking_speed_read(const struct l2cap_frame *frame)
1930 {
1931         print_seeking_speed(frame);
1932 }
1933
1934 static void seeking_speed_notify(const struct l2cap_frame *frame)
1935 {
1936         print_seeking_speed(frame);
1937 }
1938
1939 static const char *play_order_str(uint8_t order)
1940 {
1941         switch (order) {
1942         case 0x01:
1943                 return "Single once";
1944         case 0x02:
1945                 return "Single repeat";
1946         case 0x03:
1947                 return "In order once";
1948         case 0x04:
1949                 return "In order repeat";
1950         case 0x05:
1951                 return "Oldest once";
1952         case 0x06:
1953                 return "Oldest repeat";
1954         case 0x07:
1955                 return "Newest once";
1956         case 0x08:
1957                 return "Newest repeat";
1958         case 0x09:
1959                 return "Shuffle once";
1960         case 0x0A:
1961                 return "Shuffle repeat";
1962         default:
1963                 return "RFU";
1964         }
1965 }
1966
1967 static void print_playing_order(const struct l2cap_frame *frame)
1968 {
1969         int8_t playing_order;
1970
1971         if (!l2cap_frame_get_u8((void *)frame, (uint8_t *)&playing_order)) {
1972                 print_text(COLOR_ERROR, "  Playing Order: invalid size");
1973                 goto done;
1974         }
1975
1976         print_field("  Playing Order: %s", play_order_str(playing_order));
1977
1978 done:
1979         if (frame->size)
1980                 print_hex_field("  Data", frame->data, frame->size);
1981 }
1982
1983 static void playing_order_read(const struct l2cap_frame *frame)
1984 {
1985         print_playing_order(frame);
1986 }
1987
1988 static void playing_order_write(const struct l2cap_frame *frame)
1989 {
1990         print_playing_order(frame);
1991 }
1992
1993 static void playing_order_notify(const struct l2cap_frame *frame)
1994 {
1995         print_playing_order(frame);
1996 }
1997
1998 static const struct bitfield_data playing_orders_table[] = {
1999         {  0, "Single once (0x0001)"        },
2000         {  1, "Single repeat (0x0002)"          },
2001         {  2, "In order once (0x0004)"          },
2002         {  3, "In Order Repeat (0x0008)"        },
2003         {  4, "Oldest once (0x0010)"            },
2004         {  5, "Oldest repeat (0x0020)"          },
2005         {  6, "Newest once (0x0040)"            },
2006         {  7, "Newest repeat (0x0080)"      },
2007         {  8, "Shuffle once (0x0100)"           },
2008         {  9, "Shuffle repeat (0x0200)"         },
2009         {  10, "RFU (0x0400)"                       },
2010         {  11, "RFU (0x0800)"                   },
2011         {  12, "RFU (0x1000)"                           },
2012         {  13, "RFU (0x2000)"                           },
2013         {  14, "RFU (0x4000)"                           },
2014         {  15, "RFU (0x8000)"                           },
2015         { }
2016 };
2017
2018 static void print_playing_orders_supported(const struct l2cap_frame *frame)
2019 {
2020         uint16_t supported_orders;
2021         uint16_t mask;
2022
2023         if (!l2cap_frame_get_le16((void *)frame, &supported_orders)) {
2024                 print_text(COLOR_ERROR,
2025                                 "    Supported Playing Orders: invalid size");
2026                 goto done;
2027         }
2028
2029         print_field("      Supported Playing Orders: 0x%4.4x",
2030                                 supported_orders);
2031
2032         mask = print_bitfield(8, supported_orders, playing_orders_table);
2033         if (mask)
2034                 print_text(COLOR_WHITE_BG, "    Unknown fields (0x%4.4x)",
2035                                                                 mask);
2036
2037 done:
2038         if (frame->size)
2039                 print_hex_field("    Data", frame->data, frame->size);
2040 }
2041
2042 static void playing_orders_supported_read(const struct l2cap_frame *frame)
2043 {
2044         print_playing_orders_supported(frame);
2045 }
2046
2047 static const char *media_state_str(uint8_t state)
2048 {
2049         switch (state) {
2050         case 0x00:
2051                 return "Inactive";
2052         case 0x01:
2053                 return "Playing";
2054         case 0x02:
2055                 return "Paused";
2056         case 0x03:
2057                 return "Seeking";
2058         default:
2059                 return "RFU";
2060         }
2061 }
2062
2063 static void print_media_state(const struct l2cap_frame *frame)
2064 {
2065         int8_t state;
2066
2067         if (!l2cap_frame_get_u8((void *)frame, (uint8_t *)&state)) {
2068                 print_text(COLOR_ERROR, "  Media State: invalid size");
2069                 goto done;
2070         }
2071
2072         print_field("  Media State: %s", media_state_str(state));
2073
2074 done:
2075         if (frame->size)
2076                 print_hex_field("  Data", frame->data, frame->size);
2077 }
2078
2079 static void media_state_read(const struct l2cap_frame *frame)
2080 {
2081         print_media_state(frame);
2082 }
2083
2084 static void media_state_notify(const struct l2cap_frame *frame)
2085 {
2086         print_media_state(frame);
2087 }
2088
2089 struct media_cp_opcode {
2090         uint8_t opcode;
2091         const char *opcode_str;
2092 } media_cp_opcode_table[] = {
2093         {0x01,  "Play"},
2094         {0x02,  "Pause"},
2095         {0x03,  "Fast Rewind"},
2096         {0x04,  "Fast Forward"},
2097         {0x05,  "Stop"},
2098         {0x10,  "Move Relative"},
2099         {0x20,  "Previous Segment"},
2100         {0x21,  "Next Segment"},
2101         {0x22,  "First Segment"},
2102         {0x23,  "Last Segment"},
2103         {0x24,  "Goto Segment"},
2104         {0x30,  "Previous Track"},
2105         {0x31,  "Next Track"},
2106         {0x32,  "First Track"},
2107         {0x33,  "Last Track"},
2108         {0x34,  "Goto Track"},
2109         {0x40,  "Previous Group"},
2110         {0x41,  "Next Group"},
2111         {0x42,  "First Group"},
2112         {0x43,  "Last Group"},
2113         {0x44,  "Goto Group"},
2114 };
2115
2116 static const char *cp_opcode_str(uint8_t opcode)
2117 {
2118         size_t i;
2119
2120         for (i = 0; i < ARRAY_SIZE(media_cp_opcode_table); i++) {
2121                 const char *str = media_cp_opcode_table[i].opcode_str;
2122
2123                 if (opcode == media_cp_opcode_table[i].opcode)
2124                         return str;
2125         }
2126
2127         return "RFU";
2128 }
2129
2130 static void print_media_cp(const struct l2cap_frame *frame)
2131 {
2132         int8_t opcode;
2133
2134         if (!l2cap_frame_get_u8((void *)frame, (uint8_t *)&opcode)) {
2135                 print_text(COLOR_ERROR, "  Media Control Point: invalid size");
2136                 goto done;
2137         }
2138
2139         print_field("  Media Control Point: %s", cp_opcode_str(opcode));
2140
2141 done:
2142         if (frame->size)
2143                 print_hex_field("  Data", frame->data, frame->size);
2144 }
2145
2146 static void media_cp_write(const struct l2cap_frame *frame)
2147 {
2148         print_media_cp(frame);
2149 }
2150
2151 static void media_cp_notify(const struct l2cap_frame *frame)
2152 {
2153         print_media_cp(frame);
2154 }
2155
2156 static const struct bitfield_data supported_opcodes_table[] = {
2157         {0, "Play (0x00000001)"                         },
2158         {1, "Pause (0x00000002)"                        },
2159         {2, "Fast Rewind        (0x00000004)"   },
2160         {3, "Fast Forward (0x00000008)"         },
2161         {4, "Stop (0x00000010)"                         },
2162         {5, "Move Relative (0x00000020)"        },
2163         {6, "Previous Segment (0x00000040)"     },
2164         {7, "Next Segment (0x00000080)"         },
2165         {8, "First Segment (0x00000100)"        },
2166         {9, "Last Segment (0x00000200)"         },
2167         {10, "Goto Segment (0x00000400)"        },
2168         {11, "Previous Track (0x00000800)"      },
2169         {12, "Next Track (0x00001000)"          },
2170         {13, "First Track (0x00002000)"         },
2171         {14, "Last Track (0x00004000)"          },
2172         {15, "Goto Track (0x00008000)"          },
2173         {16, "Previous Group (0x00010000)"      },
2174         {17, "Next Group (0x00020000)"          },
2175         {18, "First Group (0x00040000)"         },
2176         {19, "Last Group (0x00080000)"          },
2177         {20, "Goto Group (0x00100000)"          },
2178         {21, "RFU (0x00200000)"                         },
2179         {22, "RFU (0x00400000)"                         },
2180         {23, "RFU (0x00800000)"                         },
2181         {24, "RFU (0x01000000)"                         },
2182         {25, "RFU (0x02000000)"                         },
2183         {26, "RFU (0x04000000)"                         },
2184         {27, "RFU (0x08000000)"                         },
2185         {28, "RFU (0x10000000)"                         },
2186         {29, "RFU (0x20000000)"                         },
2187         {30, "RFU (0x40000000)"                         },
2188         {31, "RFU (0x80000000)"                         },
2189         { }
2190 };
2191
2192 static void print_media_cp_op_supported(const struct l2cap_frame *frame)
2193 {
2194         uint32_t supported_opcodes;
2195         uint32_t mask;
2196
2197         if (!l2cap_frame_get_le32((void *)frame, &supported_opcodes)) {
2198                 print_text(COLOR_ERROR, "    value: invalid size");
2199                 goto done;
2200         }
2201
2202         print_field("      Supported Opcodes: 0x%8.8x", supported_opcodes);
2203
2204         mask = print_bitfield(8, supported_opcodes, supported_opcodes_table);
2205         if (mask)
2206                 print_text(COLOR_WHITE_BG, "    Unknown fields (0x%4.4x)",
2207                                                                 mask);
2208
2209 done:
2210         if (frame->size)
2211                 print_hex_field("    Data", frame->data, frame->size);
2212 }
2213
2214 static void media_cp_op_supported_read(const struct l2cap_frame *frame)
2215 {
2216         print_media_cp_op_supported(frame);
2217 }
2218
2219 static void media_cp_op_supported_notify(const struct l2cap_frame *frame)
2220 {
2221         print_media_cp_op_supported(frame);
2222 }
2223
2224 static void print_content_control_id(const struct l2cap_frame *frame)
2225 {
2226         int8_t ccid;
2227
2228         if (!l2cap_frame_get_u8((void *)frame, (uint8_t *)&ccid)) {
2229                 print_text(COLOR_ERROR, "  Content Control ID: invalid size");
2230                 goto done;
2231         }
2232
2233         print_field("  Content Control ID: 0x%2.2x", ccid);
2234
2235 done:
2236         if (frame->size)
2237                 print_hex_field("  Data", frame->data, frame->size);
2238 }
2239
2240 static void content_control_id_read(const struct l2cap_frame *frame)
2241 {
2242         print_content_control_id(frame);
2243 }
2244
2245 #define GATT_HANDLER(_uuid, _read, _write, _notify) \
2246 { \
2247         .uuid = { \
2248                 .type = BT_UUID16, \
2249                 .value.u16 = _uuid, \
2250         }, \
2251         .read = _read, \
2252         .write = _write, \
2253         .notify = _notify \
2254 }
2255
2256 struct gatt_handler {
2257         bt_uuid_t uuid;
2258         void (*read)(const struct l2cap_frame *frame);
2259         void (*write)(const struct l2cap_frame *frame);
2260         void (*notify)(const struct l2cap_frame *frame);
2261 } gatt_handlers[] = {
2262         GATT_HANDLER(0x2902, ccc_read, ccc_write, NULL),
2263         GATT_HANDLER(0x2bc4, ase_read, NULL, ase_notify),
2264         GATT_HANDLER(0x2bc5, ase_read, NULL, ase_notify),
2265         GATT_HANDLER(0x2bc6, NULL, ase_cp_write, ase_cp_notify),
2266         GATT_HANDLER(0x2bc9, pac_read, NULL, pac_notify),
2267         GATT_HANDLER(0x2bca, pac_loc_read, NULL, pac_loc_notify),
2268         GATT_HANDLER(0x2bcb, pac_read, NULL, pac_notify),
2269         GATT_HANDLER(0x2bcc, pac_loc_read, NULL, pac_loc_notify),
2270         GATT_HANDLER(0x2bcd, pac_context_read, NULL, pac_context_notify),
2271         GATT_HANDLER(0x2bce, pac_context_read, NULL, pac_context_notify),
2272         GATT_HANDLER(0x2b7d, vol_state_read, NULL, vol_state_notify),
2273         GATT_HANDLER(0x2b7e, NULL, vol_cp_write, NULL),
2274         GATT_HANDLER(0x2b7f, vol_flag_read, NULL, vol_flag_notify),
2275         GATT_HANDLER(0x2b93, mp_name_read, NULL, mp_name_notify),
2276         GATT_HANDLER(0x2b96, NULL, NULL, track_changed_notify),
2277         GATT_HANDLER(0x2b97, track_title_read, NULL, track_title_notify),
2278         GATT_HANDLER(0x2b98, track_duration_read, NULL, track_duration_notify),
2279         GATT_HANDLER(0x2b99, track_position_read, track_position_write,
2280                                         track_position_notify),
2281         GATT_HANDLER(0x2b9a, playback_speed_read, playback_speed_write,
2282                                         playback_speed_notify),
2283         GATT_HANDLER(0x2b9b, seeking_speed_read, NULL, seeking_speed_notify),
2284         GATT_HANDLER(0x2ba1, playing_order_read, playing_order_write,
2285                                         playing_order_notify),
2286         GATT_HANDLER(0x2ba2, playing_orders_supported_read, NULL, NULL),
2287         GATT_HANDLER(0x2ba3, media_state_read, NULL, media_state_notify),
2288         GATT_HANDLER(0x2ba4, NULL, media_cp_write, media_cp_notify),
2289         GATT_HANDLER(0x2ba5, media_cp_op_supported_read, NULL,
2290                                         media_cp_op_supported_notify),
2291         GATT_HANDLER(0x2bba, content_control_id_read, NULL, NULL),
2292 };
2293
2294 static struct gatt_handler *get_handler(struct gatt_db_attribute *attr)
2295 {
2296         const bt_uuid_t *uuid = gatt_db_attribute_get_type(attr);
2297         size_t i;
2298
2299         for (i = 0; i < ARRAY_SIZE(gatt_handlers); i++) {
2300                 struct gatt_handler *handler = &gatt_handlers[i];
2301
2302                 if (!bt_uuid_cmp(&handler->uuid, uuid))
2303                         return handler;
2304         }
2305
2306         return NULL;
2307 }
2308
2309 static void att_exchange_mtu_req(const struct l2cap_frame *frame)
2310 {
2311         const struct bt_l2cap_att_exchange_mtu_req *pdu = frame->data;
2312
2313         print_field("Client RX MTU: %d", le16_to_cpu(pdu->mtu));
2314 }
2315
2316 static void att_exchange_mtu_rsp(const struct l2cap_frame *frame)
2317 {
2318         const struct bt_l2cap_att_exchange_mtu_rsp *pdu = frame->data;
2319
2320         print_field("Server RX MTU: %d", le16_to_cpu(pdu->mtu));
2321 }
2322
2323 static void att_find_info_req(const struct l2cap_frame *frame)
2324 {
2325         print_handle_range("Handle range", frame->data);
2326 }
2327
2328 static const char *att_format_str(uint8_t format)
2329 {
2330         switch (format) {
2331         case 0x01:
2332                 return "UUID-16";
2333         case 0x02:
2334                 return "UUID-128";
2335         default:
2336                 return "unknown";
2337         }
2338 }
2339
2340 static uint16_t print_info_data_16(const void *data, uint16_t len)
2341 {
2342         while (len >= 4) {
2343                 print_field("Handle: 0x%4.4x", get_le16(data));
2344                 print_uuid("UUID", data + 2, 2);
2345                 data += 4;
2346                 len -= 4;
2347         }
2348
2349         return len;
2350 }
2351
2352 static uint16_t print_info_data_128(const void *data, uint16_t len)
2353 {
2354         while (len >= 18) {
2355                 print_field("Handle: 0x%4.4x", get_le16(data));
2356                 print_uuid("UUID", data + 2, 16);
2357                 data += 18;
2358                 len -= 18;
2359         }
2360
2361         return len;
2362 }
2363
2364 static void att_find_info_rsp(const struct l2cap_frame *frame)
2365 {
2366         const uint8_t *format = frame->data;
2367         uint16_t len;
2368
2369         print_field("Format: %s (0x%2.2x)", att_format_str(*format), *format);
2370
2371         if (*format == 0x01)
2372                 len = print_info_data_16(frame->data + 1, frame->size - 1);
2373         else if (*format == 0x02)
2374                 len = print_info_data_128(frame->data + 1, frame->size - 1);
2375         else
2376                 len = frame->size - 1;
2377
2378         packet_hexdump(frame->data + (frame->size - len), len);
2379 }
2380
2381 static void att_find_by_type_val_req(const struct l2cap_frame *frame)
2382 {
2383         uint16_t type;
2384
2385         print_handle_range("Handle range", frame->data);
2386
2387         type = get_le16(frame->data + 4);
2388         print_attribute_info(type, frame->data + 6, frame->size - 6);
2389 }
2390
2391 static void att_find_by_type_val_rsp(const struct l2cap_frame *frame)
2392 {
2393         const uint8_t *ptr = frame->data;
2394         uint16_t len = frame->size;
2395
2396         while (len >= 4) {
2397                 print_handle_range("Handle range", ptr);
2398                 ptr += 4;
2399                 len -= 4;
2400         }
2401
2402         packet_hexdump(ptr, len);
2403 }
2404
2405 static void att_read_type_req(const struct l2cap_frame *frame)
2406 {
2407         print_handle_range("Handle range", frame->data);
2408         print_uuid("Attribute type", frame->data + 4, frame->size - 4);
2409 }
2410
2411 static void att_read_type_rsp(const struct l2cap_frame *frame)
2412 {
2413         const struct bt_l2cap_att_read_group_type_rsp *pdu = frame->data;
2414
2415         print_field("Attribute data length: %d", pdu->length);
2416         print_data_list("Attribute data list", pdu->length,
2417                                         frame->data + 1, frame->size - 1);
2418 }
2419
2420 struct att_read {
2421         struct gatt_db_attribute *attr;
2422         bool in;
2423         uint16_t chan;
2424         void (*func)(const struct l2cap_frame *frame);
2425 };
2426
2427 struct att_conn_data {
2428         struct gatt_db *ldb;
2429         struct timespec ldb_mtim;
2430         struct gatt_db *rdb;
2431         struct timespec rdb_mtim;
2432         struct queue *reads;
2433 };
2434
2435 static void att_conn_data_free(void *data)
2436 {
2437         struct att_conn_data *att_data = data;
2438
2439         gatt_db_unref(att_data->rdb);
2440         gatt_db_unref(att_data->ldb);
2441         queue_destroy(att_data->reads, free);
2442         free(att_data);
2443 }
2444
2445 static void gatt_load_db(struct gatt_db *db, const char *filename,
2446                                                 struct timespec *mtim)
2447 {
2448         struct stat st;
2449
2450         if (lstat(filename, &st))
2451                 return;
2452
2453         if (!gatt_db_isempty(db)) {
2454                 /* Check if file has been modified since last time */
2455                 if (st.st_mtim.tv_sec == mtim->tv_sec &&
2456                                     st.st_mtim.tv_nsec == mtim->tv_nsec)
2457                         return;
2458                 /* Clear db before reloading */
2459                 gatt_db_clear(db);
2460         }
2461
2462         *mtim = st.st_mtim;
2463
2464         btd_settings_gatt_db_load(db, filename);
2465 }
2466
2467 static void load_gatt_db(struct packet_conn_data *conn)
2468 {
2469         struct att_conn_data *data = conn->data;
2470         char filename[PATH_MAX];
2471         char local[18];
2472         char peer[18];
2473
2474         if (!data) {
2475                 data = new0(struct att_conn_data, 1);
2476                 data->rdb = gatt_db_new();
2477                 data->ldb = gatt_db_new();
2478                 conn->data = data;
2479                 conn->destroy = att_conn_data_free;
2480         }
2481
2482         ba2str((bdaddr_t *)conn->src, local);
2483         ba2str((bdaddr_t *)conn->dst, peer);
2484
2485         create_filename(filename, PATH_MAX, "/%s/attributes", local);
2486         gatt_load_db(data->ldb, filename, &data->ldb_mtim);
2487
2488         create_filename(filename, PATH_MAX, "/%s/cache/%s", local, peer);
2489         gatt_load_db(data->rdb, filename, &data->rdb_mtim);
2490 }
2491
2492 static struct gatt_db_attribute *get_attribute(const struct l2cap_frame *frame,
2493                                                 uint16_t handle, bool rsp)
2494 {
2495         struct packet_conn_data *conn;
2496         struct att_conn_data *data;
2497         struct gatt_db *db;
2498
2499         conn = packet_get_conn_data(frame->handle);
2500         if (!conn)
2501                 return NULL;
2502
2503         /* Try loading local and remote gatt_db if not loaded yet */
2504         load_gatt_db(conn);
2505
2506         data = conn->data;
2507         if (!data)
2508                 return NULL;
2509
2510         if (frame->in) {
2511                 if (rsp)
2512                         db = data->rdb;
2513                 else
2514                         db = data->ldb;
2515         } else {
2516                 if (rsp)
2517                         db = data->ldb;
2518                 else
2519                         db = data->rdb;
2520         }
2521
2522         return gatt_db_get_attribute(db, handle);
2523 }
2524
2525 static void print_attribute(struct gatt_db_attribute *attr)
2526 {
2527         uint16_t handle = gatt_db_attribute_get_handle(attr);
2528         const bt_uuid_t *uuid;
2529         char label[21];
2530
2531         uuid = gatt_db_attribute_get_type(attr);
2532         if (!uuid)
2533                 goto done;
2534
2535         switch (uuid->type) {
2536         case BT_UUID16:
2537                 sprintf(label, "Handle: 0x%4.4x Type", handle);
2538                 print_field("%s: %s (0x%4.4x)", label,
2539                                 bt_uuid16_to_str(uuid->value.u16),
2540                                 uuid->value.u16);
2541                 return;
2542         case BT_UUID128:
2543                 sprintf(label, "Handle: 0x%4.4x Type", handle);
2544                 print_uuid(label, &uuid->value.u128, 16);
2545                 return;
2546         case BT_UUID_UNSPEC:
2547         case BT_UUID32:
2548                 break;
2549         }
2550
2551 done:
2552         print_field("Handle: 0x%4.4x", handle);
2553 }
2554
2555 static void print_handle(const struct l2cap_frame *frame, uint16_t handle,
2556                                                                 bool rsp)
2557 {
2558         struct gatt_db_attribute *attr;
2559
2560         attr = get_attribute(frame, handle, rsp);
2561         if (!attr) {
2562                 print_field("Handle: 0x%4.4x", handle);
2563                 return;
2564         }
2565
2566         print_attribute(attr);
2567 }
2568
2569 static void att_read_req(const struct l2cap_frame *frame)
2570 {
2571         const struct bt_l2cap_att_read_req *pdu = frame->data;
2572         uint16_t handle;
2573         struct packet_conn_data *conn;
2574         struct att_conn_data *data;
2575         struct att_read *read;
2576         struct gatt_db_attribute *attr;
2577         struct gatt_handler *handler;
2578
2579         l2cap_frame_pull((void *)frame, frame, sizeof(*pdu));
2580
2581         handle = le16_to_cpu(pdu->handle);
2582         print_handle(frame, handle, false);
2583
2584         attr = get_attribute(frame, handle, false);
2585         if (!attr)
2586                 return;
2587
2588         handler = get_handler(attr);
2589         if (!handler || !handler->read)
2590                 return;
2591
2592         conn = packet_get_conn_data(frame->handle);
2593         data = conn->data;
2594
2595         if (!data->reads)
2596                 data->reads = queue_new();
2597
2598         read = new0(struct att_read, 1);
2599         read->attr = attr;
2600         read->in = frame->in;
2601         read->chan = frame->chan;
2602         read->func = handler->read;
2603
2604         queue_push_tail(data->reads, read);
2605 }
2606
2607 static bool match_read_frame(const void *data, const void *match_data)
2608 {
2609         const struct att_read *read = data;
2610         const struct l2cap_frame *frame = match_data;
2611
2612         /* Read frame and response frame shall be in the opposite direction to
2613          * match.
2614          */
2615         if (read->in == frame->in)
2616                 return false;
2617
2618         return read->chan == frame->chan;
2619 }
2620
2621 static void att_read_rsp(const struct l2cap_frame *frame)
2622 {
2623         struct packet_conn_data *conn;
2624         struct att_conn_data *data;
2625         struct att_read *read;
2626
2627         print_hex_field("Value", frame->data, frame->size);
2628
2629         conn = packet_get_conn_data(frame->handle);
2630         if (!conn)
2631                 return;
2632
2633         data = conn->data;
2634
2635         read = queue_remove_if(data->reads, match_read_frame, (void *)frame);
2636         if (!read)
2637                 return;
2638
2639         print_attribute(read->attr);
2640
2641         read->func(frame);
2642
2643         free(read);
2644 }
2645
2646 static void att_read_blob_req(const struct l2cap_frame *frame)
2647 {
2648         print_handle(frame, get_le16(frame->data), false);
2649         print_field("Offset: 0x%4.4x", get_le16(frame->data + 2));
2650 }
2651
2652 static void att_read_blob_rsp(const struct l2cap_frame *frame)
2653 {
2654         packet_hexdump(frame->data, frame->size);
2655 }
2656
2657 static void att_read_multiple_req(const struct l2cap_frame *frame)
2658 {
2659         int i, count;
2660
2661         count = frame->size / 2;
2662
2663         for (i = 0; i < count; i++)
2664                 print_handle(frame, get_le16(frame->data + (i * 2)), false);
2665 }
2666
2667 static void att_read_group_type_req(const struct l2cap_frame *frame)
2668 {
2669         print_handle_range("Handle range", frame->data);
2670         print_uuid("Attribute group type", frame->data + 4, frame->size - 4);
2671 }
2672
2673 static void print_group_list(const char *label, uint8_t length,
2674                                         const void *data, uint16_t size)
2675 {
2676         uint8_t count;
2677
2678         if (length == 0)
2679                 return;
2680
2681         count = size / length;
2682
2683         print_field("%s: %u entr%s", label, count, count == 1 ? "y" : "ies");
2684
2685         while (size >= length) {
2686                 print_handle_range("Handle range", data);
2687                 print_uuid("UUID", data + 4, length - 4);
2688
2689                 data += length;
2690                 size -= length;
2691         }
2692
2693         packet_hexdump(data, size);
2694 }
2695
2696 static void att_read_group_type_rsp(const struct l2cap_frame *frame)
2697 {
2698         const struct bt_l2cap_att_read_group_type_rsp *pdu = frame->data;
2699
2700         print_field("Attribute data length: %d", pdu->length);
2701         print_group_list("Attribute group list", pdu->length,
2702                                         frame->data + 1, frame->size - 1);
2703 }
2704
2705 static void print_write(const struct l2cap_frame *frame, uint16_t handle,
2706                                                         size_t len)
2707 {
2708         struct gatt_db_attribute *attr;
2709         struct gatt_handler *handler;
2710
2711         print_handle(frame, handle, false);
2712         print_hex_field("  Data", frame->data, frame->size);
2713
2714         if (len > frame->size) {
2715                 print_text(COLOR_ERROR, "invalid size");
2716                 return;
2717         }
2718
2719         attr = get_attribute(frame, handle, false);
2720         if (!attr)
2721                 return;
2722
2723         handler = get_handler(attr);
2724         if (!handler)
2725                 return;
2726
2727         handler->write(frame);
2728 }
2729
2730 static void att_write_req(const struct l2cap_frame *frame)
2731 {
2732         uint16_t handle;
2733
2734         if (!l2cap_frame_get_le16((void *)frame, &handle)) {
2735                 print_text(COLOR_ERROR, "invalid size");
2736                 return;
2737         }
2738
2739         print_write(frame, handle, frame->size);
2740 }
2741
2742 static void att_write_rsp(const struct l2cap_frame *frame)
2743 {
2744 }
2745
2746 static void att_prepare_write_req(const struct l2cap_frame *frame)
2747 {
2748         print_handle(frame, get_le16(frame->data), false);
2749         print_field("Offset: 0x%4.4x", get_le16(frame->data + 2));
2750         print_hex_field("  Data", frame->data + 4, frame->size - 4);
2751 }
2752
2753 static void att_prepare_write_rsp(const struct l2cap_frame *frame)
2754 {
2755         print_handle(frame, get_le16(frame->data), true);
2756         print_field("Offset: 0x%4.4x", get_le16(frame->data + 2));
2757         print_hex_field("  Data", frame->data + 4, frame->size - 4);
2758 }
2759
2760 static void att_execute_write_req(const struct l2cap_frame *frame)
2761 {
2762         uint8_t flags = *(uint8_t *) frame->data;
2763         const char *flags_str;
2764
2765         switch (flags) {
2766         case 0x00:
2767                 flags_str = "Cancel all prepared writes";
2768                 break;
2769         case 0x01:
2770                 flags_str = "Immediately write all pending values";
2771                 break;
2772         default:
2773                 flags_str = "Unknown";
2774                 break;
2775         }
2776
2777         print_field("Flags: %s (0x%02x)", flags_str, flags);
2778 }
2779
2780 static void print_notify(const struct l2cap_frame *frame, uint16_t handle,
2781                                                                 size_t len)
2782 {
2783         struct gatt_db_attribute *attr;
2784         struct gatt_handler *handler;
2785         struct l2cap_frame clone;
2786
2787         print_handle(frame, handle, true);
2788         print_hex_field("  Data", frame->data, len);
2789
2790         if (len > frame->size) {
2791                 print_text(COLOR_ERROR, "invalid size");
2792                 return;
2793         }
2794
2795         attr = get_attribute(frame, handle, true);
2796         if (!attr)
2797                 return;
2798
2799         handler = get_handler(attr);
2800         if (!handler)
2801                 return;
2802
2803         /* Use a clone if the callback is not expected to parse the whole
2804          * frame.
2805          */
2806         if (len != frame->size) {
2807                 l2cap_frame_clone(&clone, frame);
2808                 clone.size = len;
2809                 frame = &clone;
2810         }
2811
2812         handler->notify(frame);
2813 }
2814
2815 static void att_handle_value_notify(const struct l2cap_frame *frame)
2816 {
2817         uint16_t handle;
2818         const struct bt_l2cap_att_handle_value_notify *pdu = frame->data;
2819
2820         l2cap_frame_pull((void *)frame, frame, sizeof(*pdu));
2821
2822         handle = le16_to_cpu(pdu->handle);
2823         print_notify(frame, handle, frame->size);
2824 }
2825
2826 static void att_handle_value_ind(const struct l2cap_frame *frame)
2827 {
2828         const struct bt_l2cap_att_handle_value_ind *pdu = frame->data;
2829
2830         l2cap_frame_pull((void *)frame, frame, sizeof(*pdu));
2831
2832         print_notify(frame, le16_to_cpu(pdu->handle), frame->size);
2833 }
2834
2835 static void att_handle_value_conf(const struct l2cap_frame *frame)
2836 {
2837 }
2838
2839 static void att_multiple_vl_rsp(const struct l2cap_frame *frame)
2840 {
2841         struct l2cap_frame *f = (void *) frame;
2842
2843         while (frame->size) {
2844                 uint16_t handle;
2845                 uint16_t len;
2846
2847                 if (!l2cap_frame_get_le16(f, &handle))
2848                         return;
2849
2850                 if (!l2cap_frame_get_le16(f, &len))
2851                         return;
2852
2853                 print_field("Length: 0x%4.4x", len);
2854
2855                 print_notify(frame, handle, len);
2856
2857                 l2cap_frame_pull(f, f, len);
2858         }
2859 }
2860
2861 static void att_write_command(const struct l2cap_frame *frame)
2862 {
2863         uint16_t handle;
2864
2865         if (!l2cap_frame_get_le16((void *)frame, &handle)) {
2866                 print_text(COLOR_ERROR, "invalid size");
2867                 return;
2868         }
2869
2870         print_write(frame, handle, frame->size);
2871 }
2872
2873 static void att_signed_write_command(const struct l2cap_frame *frame)
2874 {
2875         uint16_t handle;
2876
2877         if (!l2cap_frame_get_le16((void *)frame, &handle)) {
2878                 print_text(COLOR_ERROR, "invalid size");
2879                 return;
2880         }
2881
2882         print_write(frame, handle, frame->size - 12);
2883         print_hex_field("  Data", frame->data, frame->size - 12);
2884         print_hex_field("  Signature", frame->data + frame->size - 12, 12);
2885 }
2886
2887 struct att_opcode_data {
2888         uint8_t opcode;
2889         const char *str;
2890         void (*func) (const struct l2cap_frame *frame);
2891         uint8_t size;
2892         bool fixed;
2893 };
2894
2895 static const struct att_opcode_data att_opcode_table[] = {
2896         { 0x01, "Error Response",
2897                         att_error_response, 4, true },
2898         { 0x02, "Exchange MTU Request",
2899                         att_exchange_mtu_req, 2, true },
2900         { 0x03, "Exchange MTU Response",
2901                         att_exchange_mtu_rsp, 2, true },
2902         { 0x04, "Find Information Request",
2903                         att_find_info_req, 4, true },
2904         { 0x05, "Find Information Response",
2905                         att_find_info_rsp, 5, false },
2906         { 0x06, "Find By Type Value Request",
2907                         att_find_by_type_val_req, 6, false },
2908         { 0x07, "Find By Type Value Response",
2909                         att_find_by_type_val_rsp, 4, false },
2910         { 0x08, "Read By Type Request",
2911                         att_read_type_req, 6, false },
2912         { 0x09, "Read By Type Response",
2913                         att_read_type_rsp, 3, false },
2914         { 0x0a, "Read Request",
2915                         att_read_req, 2, true },
2916         { 0x0b, "Read Response",
2917                         att_read_rsp, 0, false },
2918         { 0x0c, "Read Blob Request",
2919                         att_read_blob_req, 4, true },
2920         { 0x0d, "Read Blob Response",
2921                         att_read_blob_rsp, 0, false },
2922         { 0x0e, "Read Multiple Request",
2923                         att_read_multiple_req, 4, false },
2924         { 0x0f, "Read Multiple Response"        },
2925         { 0x10, "Read By Group Type Request",
2926                         att_read_group_type_req, 6, false },
2927         { 0x11, "Read By Group Type Response",
2928                         att_read_group_type_rsp, 4, false },
2929         { 0x12, "Write Request" ,
2930                         att_write_req, 2, false },
2931         { 0x13, "Write Response",
2932                         att_write_rsp, 0, true  },
2933         { 0x16, "Prepare Write Request",
2934                         att_prepare_write_req, 4, false },
2935         { 0x17, "Prepare Write Response",
2936                         att_prepare_write_rsp, 4, false },
2937         { 0x18, "Execute Write Request",
2938                         att_execute_write_req, 1, true },
2939         { 0x19, "Execute Write Response"        },
2940         { 0x1b, "Handle Value Notification",
2941                         att_handle_value_notify, 2, false },
2942         { 0x1d, "Handle Value Indication",
2943                         att_handle_value_ind, 2, false },
2944         { 0x1e, "Handle Value Confirmation",
2945                         att_handle_value_conf, 0, true },
2946         { 0x20, "Read Multiple Request Variable Length",
2947                         att_read_multiple_req, 4, false },
2948         { 0x21, "Read Multiple Response Variable Length",
2949                         att_multiple_vl_rsp, 4, false },
2950         { 0x23, "Handle Multiple Value Notification",
2951                         att_multiple_vl_rsp, 4, false },
2952         { 0x52, "Write Command",
2953                         att_write_command, 2, false },
2954         { 0xd2, "Signed Write Command", att_signed_write_command, 14, false },
2955         { }
2956 };
2957
2958 static const char *att_opcode_to_str(uint8_t opcode)
2959 {
2960         int i;
2961
2962         for (i = 0; att_opcode_table[i].str; i++) {
2963                 if (att_opcode_table[i].opcode == opcode)
2964                         return att_opcode_table[i].str;
2965         }
2966
2967         return "Unknown";
2968 }
2969
2970 void att_packet(uint16_t index, bool in, uint16_t handle, uint16_t cid,
2971                                         const void *data, uint16_t size)
2972 {
2973         struct l2cap_frame frame;
2974         uint8_t opcode = *((const uint8_t *) data);
2975         const struct att_opcode_data *opcode_data = NULL;
2976         const char *opcode_color, *opcode_str;
2977         int i;
2978
2979         if (size < 1) {
2980                 print_text(COLOR_ERROR, "malformed attribute packet");
2981                 packet_hexdump(data, size);
2982                 return;
2983         }
2984
2985         for (i = 0; att_opcode_table[i].str; i++) {
2986                 if (att_opcode_table[i].opcode == opcode) {
2987                         opcode_data = &att_opcode_table[i];
2988                         break;
2989                 }
2990         }
2991
2992         if (opcode_data) {
2993                 if (opcode_data->func) {
2994                         if (in)
2995                                 opcode_color = COLOR_MAGENTA;
2996                         else
2997                                 opcode_color = COLOR_BLUE;
2998                 } else
2999                         opcode_color = COLOR_WHITE_BG;
3000                 opcode_str = opcode_data->str;
3001         } else {
3002                 opcode_color = COLOR_WHITE_BG;
3003                 opcode_str = "Unknown";
3004         }
3005
3006         print_indent(6, opcode_color, "ATT: ", opcode_str, COLOR_OFF,
3007                                 " (0x%2.2x) len %d", opcode, size - 1);
3008
3009         if (!opcode_data || !opcode_data->func) {
3010                 packet_hexdump(data + 1, size - 1);
3011                 return;
3012         }
3013
3014         if (opcode_data->fixed) {
3015                 if (size - 1 != opcode_data->size) {
3016                         print_text(COLOR_ERROR, "invalid size");
3017                         packet_hexdump(data + 1, size - 1);
3018                         return;
3019                 }
3020         } else {
3021                 if (size - 1 < opcode_data->size) {
3022                         print_text(COLOR_ERROR, "too short packet");
3023                         packet_hexdump(data + 1, size - 1);
3024                         return;
3025                 }
3026         }
3027
3028         l2cap_frame_init(&frame, index, in, handle, 0, cid, 0,
3029                                                 data + 1, size - 1);
3030         opcode_data->func(&frame);
3031 }