3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2015 Andrzej Kaczmarek <andrzej.kaczmarek@codecoup.pl>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #include "lib/bluetooth.h"
34 #include "src/shared/util.h"
43 #define AVDTP_MSG_TYPE_COMMAND 0x00
44 #define AVDTP_MSG_TYPE_GENERAL_REJECT 0x01
45 #define AVDTP_MSG_TYPE_RESPONSE_ACCEPT 0x02
46 #define AVDTP_MSG_TYPE_RESPONSE_REJECT 0x03
48 /* Signal Identifiers */
49 #define AVDTP_DISCOVER 0x01
50 #define AVDTP_GET_CAPABILITIES 0x02
51 #define AVDTP_SET_CONFIGURATION 0x03
52 #define AVDTP_GET_CONFIGURATION 0x04
53 #define AVDTP_RECONFIGURE 0x05
54 #define AVDTP_OPEN 0x06
55 #define AVDTP_START 0x07
56 #define AVDTP_CLOSE 0x08
57 #define AVDTP_SUSPEND 0x09
58 #define AVDTP_ABORT 0x0a
59 #define AVDTP_SECURITY_CONTROL 0x0b
60 #define AVDTP_GET_ALL_CAPABILITIES 0x0c
61 #define AVDTP_DELAYREPORT 0x0d
63 /* Service Categories */
64 #define AVDTP_MEDIA_TRANSPORT 0x01
65 #define AVDTP_REPORTING 0x02
66 #define AVDTP_RECOVERY 0x03
67 #define AVDTP_CONTENT_PROTECTION 0x04
68 #define AVDTP_HEADER_COMPRESSION 0x05
69 #define AVDTP_MULTIPLEXING 0x06
70 #define AVDTP_MEDIA_CODEC 0x07
71 #define AVDTP_DELAY_REPORTING 0x08
76 struct l2cap_frame l2cap_frame;
79 static inline bool is_configuration_sig_id(uint8_t sig_id)
81 return (sig_id == AVDTP_SET_CONFIGURATION) ||
82 (sig_id == AVDTP_GET_CONFIGURATION) ||
83 (sig_id == AVDTP_RECONFIGURE);
86 static const char *msgtype2str(uint8_t msgtype)
92 return "General Reject";
94 return "Response Accept";
96 return "Response Reject";
102 static const char *sigid2str(uint8_t sigid)
107 case AVDTP_GET_CAPABILITIES:
108 return "Get Capabilities";
109 case AVDTP_SET_CONFIGURATION:
110 return "Set Configuration";
111 case AVDTP_GET_CONFIGURATION:
112 return "Get Configuration";
113 case AVDTP_RECONFIGURE:
114 return "Reconfigure";
125 case AVDTP_SECURITY_CONTROL:
126 return "Security Control";
127 case AVDTP_GET_ALL_CAPABILITIES:
128 return "Get All Capabilities";
129 case AVDTP_DELAYREPORT:
130 return "Delay Report";
136 static const char *error2str(uint8_t error)
140 return "BAD_HEADER_FORMAT";
144 return "BAD_ACP_SEID";
148 return "SEP_NOT_IN_USER";
150 return "BAD_SERV_CATEGORY";
152 return "BAD_PAYLOAD_FORMAT";
154 return "NOT_SUPPORTED_COMMAND";
156 return "INVALID_CAPABILITIES";
158 return "BAD_RECOVERY_TYPE";
160 return "BAD_MEDIA_TRANSPORT_FORMAT";
162 return "BAD_RECOVERY_FORMAT";
164 return "BAD_ROHC_FORMAT";
166 return "BAD_CP_FORMAT";
168 return "BAD_MULTIPLEXING_FORMAT";
170 return "UNSUPPORTED_CONFIGURATION";
178 static const char *mediatype2str(uint8_t media_type)
180 switch (media_type) {
192 static const char *mediacodec2str(uint8_t codec)
198 return "MPEG-1,2 Audio";
200 return "MPEG-2,4 AAC";
202 return "ATRAC Family";
210 static const char *cptype2str(uint8_t cp)
222 static const char *servicecat2str(uint8_t service_cat)
224 switch (service_cat) {
225 case AVDTP_MEDIA_TRANSPORT:
226 return "Media Transport";
227 case AVDTP_REPORTING:
231 case AVDTP_CONTENT_PROTECTION:
232 return "Content Protection";
233 case AVDTP_HEADER_COMPRESSION:
234 return "Header Compression";
235 case AVDTP_MULTIPLEXING:
236 return "Multiplexing";
237 case AVDTP_MEDIA_CODEC:
238 return "Media Codec";
239 case AVDTP_DELAY_REPORTING:
240 return "Delay Reporting";
246 static bool avdtp_reject_common(struct avdtp_frame *avdtp_frame)
248 struct l2cap_frame *frame = &avdtp_frame->l2cap_frame;
251 if (!l2cap_frame_get_u8(frame, &error))
254 print_field("Error code: %s (0x%02x)", error2str(error), error);
259 static bool service_content_protection(struct avdtp_frame *avdtp_frame,
262 struct l2cap_frame *frame = &avdtp_frame->l2cap_frame;
268 if (!l2cap_frame_get_le16(frame, &type))
273 print_field("%*cContent Protection Type: %s (0x%04x)", 2, ' ',
274 cptype2str(type), type);
276 /* TODO: decode protection specific information */
277 packet_hexdump(frame->data, losc);
279 l2cap_frame_pull(frame, frame, losc);
284 static bool service_media_codec(struct avdtp_frame *avdtp_frame, uint8_t losc)
286 struct l2cap_frame *frame = &avdtp_frame->l2cap_frame;
293 l2cap_frame_get_u8(frame, &type);
294 l2cap_frame_get_u8(frame, &codec);
298 print_field("%*cMedia Type: %s (0x%02x)", 2, ' ',
299 mediatype2str(type >> 4), type >> 4);
301 print_field("%*cMedia Codec: %s (0x%02x)", 2, ' ',
302 mediacodec2str(codec), codec);
304 if (is_configuration_sig_id(avdtp_frame->sig_id))
305 return a2dp_codec_cfg(codec, losc, frame);
307 return a2dp_codec_cap(codec, losc, frame);
310 static bool decode_capabilities(struct avdtp_frame *avdtp_frame)
312 struct l2cap_frame *frame = &avdtp_frame->l2cap_frame;
316 while (l2cap_frame_get_u8(frame, &service_cat)) {
317 print_field("Service Category: %s (0x%02x)",
318 servicecat2str(service_cat), service_cat);
320 if (!l2cap_frame_get_u8(frame, &losc))
323 if (frame->size < losc)
326 switch (service_cat) {
327 case AVDTP_CONTENT_PROTECTION:
328 if (!service_content_protection(avdtp_frame, losc))
331 case AVDTP_MEDIA_CODEC:
332 if (!service_media_codec(avdtp_frame, losc))
335 case AVDTP_MEDIA_TRANSPORT:
336 case AVDTP_REPORTING:
338 case AVDTP_HEADER_COMPRESSION:
339 case AVDTP_MULTIPLEXING:
340 case AVDTP_DELAY_REPORTING:
342 packet_hexdump(frame->data, losc);
343 l2cap_frame_pull(frame, frame, losc);
351 static bool avdtp_discover(struct avdtp_frame *avdtp_frame)
353 struct l2cap_frame *frame = &avdtp_frame->l2cap_frame;
354 uint8_t type = avdtp_frame->hdr & 0x03;
359 case AVDTP_MSG_TYPE_COMMAND:
361 case AVDTP_MSG_TYPE_RESPONSE_ACCEPT:
362 while (l2cap_frame_get_u8(frame, &seid)) {
363 print_field("ACP SEID: %d", seid >> 2);
365 if (!l2cap_frame_get_u8(frame, &info))
368 print_field("%*cMedia Type: %s (0x%02x)", 2, ' ',
369 mediatype2str(info >> 4), info >> 4);
370 print_field("%*cSEP Type: %s (0x%02x)", 2, ' ',
371 info & 0x08 ? "SNK" : "SRC",
373 print_field("%*cIn use: %s", 2, ' ',
374 seid & 0x02 ? "Yes" : "No");
377 case AVDTP_MSG_TYPE_RESPONSE_REJECT:
378 return avdtp_reject_common(avdtp_frame);
384 static bool avdtp_get_capabilities(struct avdtp_frame *avdtp_frame)
386 struct l2cap_frame *frame = &avdtp_frame->l2cap_frame;
387 uint8_t type = avdtp_frame->hdr & 0x03;
391 case AVDTP_MSG_TYPE_COMMAND:
392 if (!l2cap_frame_get_u8(frame, &seid))
395 print_field("ACP SEID: %d", seid >> 2);
398 case AVDTP_MSG_TYPE_RESPONSE_ACCEPT:
399 return decode_capabilities(avdtp_frame);
400 case AVDTP_MSG_TYPE_RESPONSE_REJECT:
401 return avdtp_reject_common(avdtp_frame);
407 static bool avdtp_set_configuration(struct avdtp_frame *avdtp_frame)
409 struct l2cap_frame *frame = &avdtp_frame->l2cap_frame;
410 uint8_t type = avdtp_frame->hdr & 0x03;
411 uint8_t acp_seid, int_seid;
415 case AVDTP_MSG_TYPE_COMMAND:
416 if (!l2cap_frame_get_u8(frame, &acp_seid))
419 print_field("ACP SEID: %d", acp_seid >> 2);
421 if (!l2cap_frame_get_u8(frame, &int_seid))
424 print_field("INT SEID: %d", int_seid >> 2);
426 return decode_capabilities(avdtp_frame);
427 case AVDTP_MSG_TYPE_RESPONSE_ACCEPT:
429 case AVDTP_MSG_TYPE_RESPONSE_REJECT:
430 if (!l2cap_frame_get_u8(frame, &service_cat))
433 print_field("Service Category: %s (0x%02x)",
434 servicecat2str(service_cat), service_cat);
436 return avdtp_reject_common(avdtp_frame);
442 static bool avdtp_get_configuration(struct avdtp_frame *avdtp_frame)
444 struct l2cap_frame *frame = &avdtp_frame->l2cap_frame;
445 uint8_t type = avdtp_frame->hdr & 0x03;
449 case AVDTP_MSG_TYPE_COMMAND:
450 if (!l2cap_frame_get_u8(frame, &seid))
453 print_field("ACP SEID: %d", seid >> 2);
456 case AVDTP_MSG_TYPE_RESPONSE_ACCEPT:
457 return decode_capabilities(avdtp_frame);
458 case AVDTP_MSG_TYPE_RESPONSE_REJECT:
459 return avdtp_reject_common(avdtp_frame);
465 static bool avdtp_reconfigure(struct avdtp_frame *avdtp_frame)
467 struct l2cap_frame *frame = &avdtp_frame->l2cap_frame;
468 uint8_t type = avdtp_frame->hdr & 0x03;
473 case AVDTP_MSG_TYPE_COMMAND:
474 if (!l2cap_frame_get_u8(frame, &seid))
477 print_field("ACP SEID: %d", seid >> 2);
479 return decode_capabilities(avdtp_frame);
480 case AVDTP_MSG_TYPE_RESPONSE_ACCEPT:
482 case AVDTP_MSG_TYPE_RESPONSE_REJECT:
483 if (!l2cap_frame_get_u8(frame, &service_cat))
486 print_field("Service Category: %s (0x%02x)",
487 servicecat2str(service_cat), service_cat);
489 return avdtp_reject_common(avdtp_frame);
495 static bool avdtp_open(struct avdtp_frame *avdtp_frame)
497 struct l2cap_frame *frame = &avdtp_frame->l2cap_frame;
498 uint8_t type = avdtp_frame->hdr & 0x03;
502 case AVDTP_MSG_TYPE_COMMAND:
503 if (!l2cap_frame_get_u8(frame, &seid))
506 print_field("ACP SEID: %d", seid >> 2);
509 case AVDTP_MSG_TYPE_RESPONSE_ACCEPT:
511 case AVDTP_MSG_TYPE_RESPONSE_REJECT:
512 return avdtp_reject_common(avdtp_frame);
518 static bool avdtp_start(struct avdtp_frame *avdtp_frame)
520 struct l2cap_frame *frame = &avdtp_frame->l2cap_frame;
521 uint8_t type = avdtp_frame->hdr & 0x03;
525 case AVDTP_MSG_TYPE_COMMAND:
526 if (!l2cap_frame_get_u8(frame, &seid))
529 print_field("ACP SEID: %d", seid >> 2);
531 while (l2cap_frame_get_u8(frame, &seid))
532 print_field("ACP SEID: %d", seid >> 2);
535 case AVDTP_MSG_TYPE_RESPONSE_ACCEPT:
537 case AVDTP_MSG_TYPE_RESPONSE_REJECT:
538 if (!l2cap_frame_get_u8(frame, &seid))
541 print_field("ACP SEID: %d", seid >> 2);
543 return avdtp_reject_common(avdtp_frame);
549 static bool avdtp_close(struct avdtp_frame *avdtp_frame)
551 struct l2cap_frame *frame = &avdtp_frame->l2cap_frame;
552 uint8_t type = avdtp_frame->hdr & 0x03;
556 case AVDTP_MSG_TYPE_COMMAND:
557 if (!l2cap_frame_get_u8(frame, &seid))
560 print_field("ACP SEID: %d", seid >> 2);
563 case AVDTP_MSG_TYPE_RESPONSE_ACCEPT:
565 case AVDTP_MSG_TYPE_RESPONSE_REJECT:
566 return avdtp_reject_common(avdtp_frame);
572 static bool avdtp_suspend(struct avdtp_frame *avdtp_frame)
574 struct l2cap_frame *frame = &avdtp_frame->l2cap_frame;
575 uint8_t type = avdtp_frame->hdr & 0x03;
579 case AVDTP_MSG_TYPE_COMMAND:
580 if (!l2cap_frame_get_u8(frame, &seid))
583 print_field("ACP SEID: %d", seid >> 2);
585 while (l2cap_frame_get_u8(frame, &seid))
586 print_field("ACP SEID: %d", seid >> 2);
589 case AVDTP_MSG_TYPE_RESPONSE_ACCEPT:
591 case AVDTP_MSG_TYPE_RESPONSE_REJECT:
592 if (!l2cap_frame_get_u8(frame, &seid))
595 print_field("ACP SEID: %d", seid >> 2);
597 return avdtp_reject_common(avdtp_frame);
603 static bool avdtp_abort(struct avdtp_frame *avdtp_frame)
605 struct l2cap_frame *frame = &avdtp_frame->l2cap_frame;
606 uint8_t type = avdtp_frame->hdr & 0x03;
610 case AVDTP_MSG_TYPE_COMMAND:
611 if (!l2cap_frame_get_u8(frame, &seid))
614 print_field("ACP SEID: %d", seid >> 2);
617 case AVDTP_MSG_TYPE_RESPONSE_ACCEPT:
624 static bool avdtp_security_control(struct avdtp_frame *avdtp_frame)
626 struct l2cap_frame *frame = &avdtp_frame->l2cap_frame;
627 uint8_t type = avdtp_frame->hdr & 0x03;
631 case AVDTP_MSG_TYPE_COMMAND:
632 if (!l2cap_frame_get_u8(frame, &seid))
635 print_field("ACP SEID: %d", seid >> 2);
637 /* TODO: decode more information */
638 packet_hexdump(frame->data, frame->size);
640 case AVDTP_MSG_TYPE_RESPONSE_ACCEPT:
641 /* TODO: decode more information */
642 packet_hexdump(frame->data, frame->size);
644 case AVDTP_MSG_TYPE_RESPONSE_REJECT:
645 return avdtp_reject_common(avdtp_frame);
651 static bool avdtp_delayreport(struct avdtp_frame *avdtp_frame)
653 struct l2cap_frame *frame = &avdtp_frame->l2cap_frame;
654 uint8_t type = avdtp_frame->hdr & 0x03;
659 case AVDTP_MSG_TYPE_COMMAND:
660 if (!l2cap_frame_get_u8(frame, &seid))
663 print_field("ACP SEID: %d", seid >> 2);
665 if (!l2cap_frame_get_be16(frame, &delay))
668 print_field("Delay: %d.%dms", delay / 10, delay % 10);
671 case AVDTP_MSG_TYPE_RESPONSE_ACCEPT:
673 case AVDTP_MSG_TYPE_RESPONSE_REJECT:
674 return avdtp_reject_common(avdtp_frame);
680 static bool avdtp_signalling_packet(struct avdtp_frame *avdtp_frame)
682 struct l2cap_frame *frame = &avdtp_frame->l2cap_frame;
683 const char *pdu_color;
689 pdu_color = COLOR_MAGENTA;
691 pdu_color = COLOR_BLUE;
693 if (!l2cap_frame_get_u8(frame, &hdr))
696 avdtp_frame->hdr = hdr;
698 /* Continue Packet || End Packet */
699 if (((hdr & 0x0c) == 0x08) || ((hdr & 0x0c) == 0x0c)) {
700 /* TODO: handle fragmentation */
701 packet_hexdump(frame->data, frame->size);
706 if ((hdr & 0x0c) == 0x04) {
707 if (!l2cap_frame_get_u8(frame, &nosp))
711 if (!l2cap_frame_get_u8(frame, &sig_id))
716 avdtp_frame->sig_id = sig_id;
718 print_indent(6, pdu_color, "AVDTP: ", sigid2str(sig_id), COLOR_OFF,
719 " (0x%02x) %s (0x%02x) type 0x%02x label %d nosp %d",
720 sig_id, msgtype2str(hdr & 0x03), hdr & 0x03,
721 hdr & 0x0c, hdr >> 4, nosp);
724 if ((hdr & 0x0c) == 0x04) {
725 /* TODO: handle fragmentation */
726 packet_hexdump(frame->data, frame->size);
731 if ((hdr & 0x03) == 0x03)
736 return avdtp_discover(avdtp_frame);
737 case AVDTP_GET_CAPABILITIES:
738 case AVDTP_GET_ALL_CAPABILITIES:
739 return avdtp_get_capabilities(avdtp_frame);
740 case AVDTP_SET_CONFIGURATION:
741 return avdtp_set_configuration(avdtp_frame);
742 case AVDTP_GET_CONFIGURATION:
743 return avdtp_get_configuration(avdtp_frame);
744 case AVDTP_RECONFIGURE:
745 return avdtp_reconfigure(avdtp_frame);
747 return avdtp_open(avdtp_frame);
749 return avdtp_start(avdtp_frame);
751 return avdtp_close(avdtp_frame);
753 return avdtp_suspend(avdtp_frame);
755 return avdtp_abort(avdtp_frame);
756 case AVDTP_SECURITY_CONTROL:
757 return avdtp_security_control(avdtp_frame);
758 case AVDTP_DELAYREPORT:
759 return avdtp_delayreport(avdtp_frame);
762 packet_hexdump(frame->data, frame->size);
767 void avdtp_packet(const struct l2cap_frame *frame)
769 struct avdtp_frame avdtp_frame;
772 l2cap_frame_pull(&avdtp_frame.l2cap_frame, frame, 0);
774 switch (frame->seq_num) {
776 ret = avdtp_signalling_packet(&avdtp_frame);
779 if (packet_has_filter(PACKET_FILTER_SHOW_A2DP_STREAM))
780 packet_hexdump(frame->data, frame->size);
785 print_text(COLOR_ERROR, "PDU malformed");
786 packet_hexdump(frame->data, frame->size);