avdtp.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. // SPDX-License-Identifier: LGPL-2.1-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2015 Andrzej Kaczmarek <andrzej.kaczmarek@codecoup.pl>
  7. *
  8. *
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. #include <config.h>
  12. #endif
  13. #define _GNU_SOURCE
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include "lib/bluetooth.h"
  18. #include "src/shared/util.h"
  19. #include "bt.h"
  20. #include "packet.h"
  21. #include "display.h"
  22. #include "l2cap.h"
  23. #include "avdtp.h"
  24. #include "a2dp.h"
  25. /* Message Types */
  26. #define AVDTP_MSG_TYPE_COMMAND 0x00
  27. #define AVDTP_MSG_TYPE_GENERAL_REJECT 0x01
  28. #define AVDTP_MSG_TYPE_RESPONSE_ACCEPT 0x02
  29. #define AVDTP_MSG_TYPE_RESPONSE_REJECT 0x03
  30. /* Signal Identifiers */
  31. #define AVDTP_DISCOVER 0x01
  32. #define AVDTP_GET_CAPABILITIES 0x02
  33. #define AVDTP_SET_CONFIGURATION 0x03
  34. #define AVDTP_GET_CONFIGURATION 0x04
  35. #define AVDTP_RECONFIGURE 0x05
  36. #define AVDTP_OPEN 0x06
  37. #define AVDTP_START 0x07
  38. #define AVDTP_CLOSE 0x08
  39. #define AVDTP_SUSPEND 0x09
  40. #define AVDTP_ABORT 0x0a
  41. #define AVDTP_SECURITY_CONTROL 0x0b
  42. #define AVDTP_GET_ALL_CAPABILITIES 0x0c
  43. #define AVDTP_DELAYREPORT 0x0d
  44. /* Service Categories */
  45. #define AVDTP_MEDIA_TRANSPORT 0x01
  46. #define AVDTP_REPORTING 0x02
  47. #define AVDTP_RECOVERY 0x03
  48. #define AVDTP_CONTENT_PROTECTION 0x04
  49. #define AVDTP_HEADER_COMPRESSION 0x05
  50. #define AVDTP_MULTIPLEXING 0x06
  51. #define AVDTP_MEDIA_CODEC 0x07
  52. #define AVDTP_DELAY_REPORTING 0x08
  53. struct avdtp_frame {
  54. uint8_t hdr;
  55. uint8_t sig_id;
  56. struct l2cap_frame l2cap_frame;
  57. };
  58. static inline bool is_configuration_sig_id(uint8_t sig_id)
  59. {
  60. return (sig_id == AVDTP_SET_CONFIGURATION) ||
  61. (sig_id == AVDTP_GET_CONFIGURATION) ||
  62. (sig_id == AVDTP_RECONFIGURE);
  63. }
  64. static const char *msgtype2str(uint8_t msgtype)
  65. {
  66. switch (msgtype) {
  67. case 0:
  68. return "Command";
  69. case 1:
  70. return "General Reject";
  71. case 2:
  72. return "Response Accept";
  73. case 3:
  74. return "Response Reject";
  75. }
  76. return "";
  77. }
  78. static const char *sigid2str(uint8_t sigid)
  79. {
  80. switch (sigid) {
  81. case AVDTP_DISCOVER:
  82. return "Discover";
  83. case AVDTP_GET_CAPABILITIES:
  84. return "Get Capabilities";
  85. case AVDTP_SET_CONFIGURATION:
  86. return "Set Configuration";
  87. case AVDTP_GET_CONFIGURATION:
  88. return "Get Configuration";
  89. case AVDTP_RECONFIGURE:
  90. return "Reconfigure";
  91. case AVDTP_OPEN:
  92. return "Open";
  93. case AVDTP_START:
  94. return "Start";
  95. case AVDTP_CLOSE:
  96. return "Close";
  97. case AVDTP_SUSPEND:
  98. return "Suspend";
  99. case AVDTP_ABORT:
  100. return "Abort";
  101. case AVDTP_SECURITY_CONTROL:
  102. return "Security Control";
  103. case AVDTP_GET_ALL_CAPABILITIES:
  104. return "Get All Capabilities";
  105. case AVDTP_DELAYREPORT:
  106. return "Delay Report";
  107. default:
  108. return "Reserved";
  109. }
  110. }
  111. static const char *error2str(uint8_t error)
  112. {
  113. switch (error) {
  114. case 0x01:
  115. return "BAD_HEADER_FORMAT";
  116. case 0x11:
  117. return "BAD_LENGTH";
  118. case 0x12:
  119. return "BAD_ACP_SEID";
  120. case 0x13:
  121. return "SEP_IN_USE";
  122. case 0x14:
  123. return "SEP_NOT_IN_USER";
  124. case 0x17:
  125. return "BAD_SERV_CATEGORY";
  126. case 0x18:
  127. return "BAD_PAYLOAD_FORMAT";
  128. case 0x19:
  129. return "NOT_SUPPORTED_COMMAND";
  130. case 0x1a:
  131. return "INVALID_CAPABILITIES";
  132. case 0x22:
  133. return "BAD_RECOVERY_TYPE";
  134. case 0x23:
  135. return "BAD_MEDIA_TRANSPORT_FORMAT";
  136. case 0x25:
  137. return "BAD_RECOVERY_FORMAT";
  138. case 0x26:
  139. return "BAD_ROHC_FORMAT";
  140. case 0x27:
  141. return "BAD_CP_FORMAT";
  142. case 0x28:
  143. return "BAD_MULTIPLEXING_FORMAT";
  144. case 0x29:
  145. return "UNSUPPORTED_CONFIGURATION";
  146. case 0x31:
  147. return "BAD_STATE";
  148. default:
  149. return "Unknown";
  150. }
  151. }
  152. static const char *mediatype2str(uint8_t media_type)
  153. {
  154. switch (media_type) {
  155. case 0x00:
  156. return "Audio";
  157. case 0x01:
  158. return "Video";
  159. case 0x02:
  160. return "Multimedia";
  161. default:
  162. return "Reserved";
  163. }
  164. }
  165. static const char *mediacodec2str(uint8_t codec)
  166. {
  167. switch (codec) {
  168. case 0x00:
  169. return "SBC";
  170. case 0x01:
  171. return "MPEG-1,2 Audio";
  172. case 0x02:
  173. return "MPEG-2,4 AAC";
  174. case 0x04:
  175. return "ATRAC Family";
  176. case 0xff:
  177. return "Non-A2DP";
  178. default:
  179. return "Reserved";
  180. }
  181. }
  182. static const char *cptype2str(uint8_t cp)
  183. {
  184. switch (cp) {
  185. case 0x0001:
  186. return "DTCP";
  187. case 0x0002:
  188. return "SCMS-T";
  189. default:
  190. return "Reserved";
  191. }
  192. }
  193. static const char *servicecat2str(uint8_t service_cat)
  194. {
  195. switch (service_cat) {
  196. case AVDTP_MEDIA_TRANSPORT:
  197. return "Media Transport";
  198. case AVDTP_REPORTING:
  199. return "Reporting";
  200. case AVDTP_RECOVERY:
  201. return "Recovery";
  202. case AVDTP_CONTENT_PROTECTION:
  203. return "Content Protection";
  204. case AVDTP_HEADER_COMPRESSION:
  205. return "Header Compression";
  206. case AVDTP_MULTIPLEXING:
  207. return "Multiplexing";
  208. case AVDTP_MEDIA_CODEC:
  209. return "Media Codec";
  210. case AVDTP_DELAY_REPORTING:
  211. return "Delay Reporting";
  212. default:
  213. return "Reserved";
  214. }
  215. }
  216. static bool avdtp_reject_common(struct avdtp_frame *avdtp_frame)
  217. {
  218. struct l2cap_frame *frame = &avdtp_frame->l2cap_frame;
  219. uint8_t error;
  220. if (!l2cap_frame_get_u8(frame, &error))
  221. return false;
  222. print_field("Error code: %s (0x%02x)", error2str(error), error);
  223. return true;
  224. }
  225. static bool service_content_protection(struct avdtp_frame *avdtp_frame,
  226. uint8_t losc)
  227. {
  228. struct l2cap_frame *frame = &avdtp_frame->l2cap_frame;
  229. uint16_t type = 0;
  230. if (losc < 2)
  231. return false;
  232. if (!l2cap_frame_get_le16(frame, &type))
  233. return false;
  234. losc -= 2;
  235. print_field("%*cContent Protection Type: %s (0x%04x)", 2, ' ',
  236. cptype2str(type), type);
  237. /* TODO: decode protection specific information */
  238. packet_hexdump(frame->data, losc);
  239. l2cap_frame_pull(frame, frame, losc);
  240. return true;
  241. }
  242. static bool service_media_codec(struct avdtp_frame *avdtp_frame, uint8_t losc)
  243. {
  244. struct l2cap_frame *frame = &avdtp_frame->l2cap_frame;
  245. uint8_t type = 0;
  246. uint8_t codec = 0;
  247. if (losc < 2)
  248. return false;
  249. l2cap_frame_get_u8(frame, &type);
  250. l2cap_frame_get_u8(frame, &codec);
  251. losc -= 2;
  252. print_field("%*cMedia Type: %s (0x%02x)", 2, ' ',
  253. mediatype2str(type >> 4), type >> 4);
  254. print_field("%*cMedia Codec: %s (0x%02x)", 2, ' ',
  255. mediacodec2str(codec), codec);
  256. if (is_configuration_sig_id(avdtp_frame->sig_id))
  257. return a2dp_codec_cfg(codec, losc, frame);
  258. else
  259. return a2dp_codec_cap(codec, losc, frame);
  260. }
  261. static bool decode_capabilities(struct avdtp_frame *avdtp_frame)
  262. {
  263. struct l2cap_frame *frame = &avdtp_frame->l2cap_frame;
  264. uint8_t service_cat;
  265. uint8_t losc;
  266. while (l2cap_frame_get_u8(frame, &service_cat)) {
  267. print_field("Service Category: %s (0x%02x)",
  268. servicecat2str(service_cat), service_cat);
  269. if (!l2cap_frame_get_u8(frame, &losc))
  270. return false;
  271. if (frame->size < losc)
  272. return false;
  273. switch (service_cat) {
  274. case AVDTP_CONTENT_PROTECTION:
  275. if (!service_content_protection(avdtp_frame, losc))
  276. return false;
  277. break;
  278. case AVDTP_MEDIA_CODEC:
  279. if (!service_media_codec(avdtp_frame, losc))
  280. return false;
  281. break;
  282. case AVDTP_MEDIA_TRANSPORT:
  283. case AVDTP_REPORTING:
  284. case AVDTP_RECOVERY:
  285. case AVDTP_HEADER_COMPRESSION:
  286. case AVDTP_MULTIPLEXING:
  287. case AVDTP_DELAY_REPORTING:
  288. default:
  289. packet_hexdump(frame->data, losc);
  290. l2cap_frame_pull(frame, frame, losc);
  291. }
  292. }
  293. return true;
  294. }
  295. static bool avdtp_discover(struct avdtp_frame *avdtp_frame)
  296. {
  297. struct l2cap_frame *frame = &avdtp_frame->l2cap_frame;
  298. uint8_t type = avdtp_frame->hdr & 0x03;
  299. uint8_t seid;
  300. uint8_t info;
  301. switch (type) {
  302. case AVDTP_MSG_TYPE_COMMAND:
  303. return true;
  304. case AVDTP_MSG_TYPE_RESPONSE_ACCEPT:
  305. while (l2cap_frame_get_u8(frame, &seid)) {
  306. print_field("ACP SEID: %d", seid >> 2);
  307. if (!l2cap_frame_get_u8(frame, &info))
  308. return false;
  309. print_field("%*cMedia Type: %s (0x%02x)", 2, ' ',
  310. mediatype2str(info >> 4), info >> 4);
  311. print_field("%*cSEP Type: %s (0x%02x)", 2, ' ',
  312. info & 0x08 ? "SNK" : "SRC",
  313. (info >> 3) & 0x01);
  314. print_field("%*cIn use: %s", 2, ' ',
  315. seid & 0x02 ? "Yes" : "No");
  316. }
  317. return true;
  318. case AVDTP_MSG_TYPE_RESPONSE_REJECT:
  319. return avdtp_reject_common(avdtp_frame);
  320. }
  321. return false;
  322. }
  323. static bool avdtp_get_capabilities(struct avdtp_frame *avdtp_frame)
  324. {
  325. struct l2cap_frame *frame = &avdtp_frame->l2cap_frame;
  326. uint8_t type = avdtp_frame->hdr & 0x03;
  327. uint8_t seid;
  328. switch (type) {
  329. case AVDTP_MSG_TYPE_COMMAND:
  330. if (!l2cap_frame_get_u8(frame, &seid))
  331. return false;
  332. print_field("ACP SEID: %d", seid >> 2);
  333. return true;
  334. case AVDTP_MSG_TYPE_RESPONSE_ACCEPT:
  335. return decode_capabilities(avdtp_frame);
  336. case AVDTP_MSG_TYPE_RESPONSE_REJECT:
  337. return avdtp_reject_common(avdtp_frame);
  338. }
  339. return false;
  340. }
  341. static bool avdtp_set_configuration(struct avdtp_frame *avdtp_frame)
  342. {
  343. struct l2cap_frame *frame = &avdtp_frame->l2cap_frame;
  344. uint8_t type = avdtp_frame->hdr & 0x03;
  345. uint8_t acp_seid, int_seid;
  346. uint8_t service_cat;
  347. switch (type) {
  348. case AVDTP_MSG_TYPE_COMMAND:
  349. if (!l2cap_frame_get_u8(frame, &acp_seid))
  350. return false;
  351. print_field("ACP SEID: %d", acp_seid >> 2);
  352. if (!l2cap_frame_get_u8(frame, &int_seid))
  353. return false;
  354. print_field("INT SEID: %d", int_seid >> 2);
  355. return decode_capabilities(avdtp_frame);
  356. case AVDTP_MSG_TYPE_RESPONSE_ACCEPT:
  357. return true;
  358. case AVDTP_MSG_TYPE_RESPONSE_REJECT:
  359. if (!l2cap_frame_get_u8(frame, &service_cat))
  360. return false;
  361. print_field("Service Category: %s (0x%02x)",
  362. servicecat2str(service_cat), service_cat);
  363. return avdtp_reject_common(avdtp_frame);
  364. }
  365. return false;
  366. }
  367. static bool avdtp_get_configuration(struct avdtp_frame *avdtp_frame)
  368. {
  369. struct l2cap_frame *frame = &avdtp_frame->l2cap_frame;
  370. uint8_t type = avdtp_frame->hdr & 0x03;
  371. uint8_t seid;
  372. switch (type) {
  373. case AVDTP_MSG_TYPE_COMMAND:
  374. if (!l2cap_frame_get_u8(frame, &seid))
  375. return false;
  376. print_field("ACP SEID: %d", seid >> 2);
  377. return true;
  378. case AVDTP_MSG_TYPE_RESPONSE_ACCEPT:
  379. return decode_capabilities(avdtp_frame);
  380. case AVDTP_MSG_TYPE_RESPONSE_REJECT:
  381. return avdtp_reject_common(avdtp_frame);
  382. }
  383. return false;
  384. }
  385. static bool avdtp_reconfigure(struct avdtp_frame *avdtp_frame)
  386. {
  387. struct l2cap_frame *frame = &avdtp_frame->l2cap_frame;
  388. uint8_t type = avdtp_frame->hdr & 0x03;
  389. uint8_t seid;
  390. uint8_t service_cat;
  391. switch (type) {
  392. case AVDTP_MSG_TYPE_COMMAND:
  393. if (!l2cap_frame_get_u8(frame, &seid))
  394. return false;
  395. print_field("ACP SEID: %d", seid >> 2);
  396. return decode_capabilities(avdtp_frame);
  397. case AVDTP_MSG_TYPE_RESPONSE_ACCEPT:
  398. return true;
  399. case AVDTP_MSG_TYPE_RESPONSE_REJECT:
  400. if (!l2cap_frame_get_u8(frame, &service_cat))
  401. return false;
  402. print_field("Service Category: %s (0x%02x)",
  403. servicecat2str(service_cat), service_cat);
  404. return avdtp_reject_common(avdtp_frame);
  405. }
  406. return false;
  407. }
  408. static bool avdtp_open(struct avdtp_frame *avdtp_frame)
  409. {
  410. struct l2cap_frame *frame = &avdtp_frame->l2cap_frame;
  411. uint8_t type = avdtp_frame->hdr & 0x03;
  412. uint8_t seid;
  413. switch (type) {
  414. case AVDTP_MSG_TYPE_COMMAND:
  415. if (!l2cap_frame_get_u8(frame, &seid))
  416. return false;
  417. print_field("ACP SEID: %d", seid >> 2);
  418. return true;
  419. case AVDTP_MSG_TYPE_RESPONSE_ACCEPT:
  420. return true;
  421. case AVDTP_MSG_TYPE_RESPONSE_REJECT:
  422. return avdtp_reject_common(avdtp_frame);
  423. }
  424. return false;
  425. }
  426. static bool avdtp_start(struct avdtp_frame *avdtp_frame)
  427. {
  428. struct l2cap_frame *frame = &avdtp_frame->l2cap_frame;
  429. uint8_t type = avdtp_frame->hdr & 0x03;
  430. uint8_t seid;
  431. switch (type) {
  432. case AVDTP_MSG_TYPE_COMMAND:
  433. if (!l2cap_frame_get_u8(frame, &seid))
  434. return false;
  435. print_field("ACP SEID: %d", seid >> 2);
  436. while (l2cap_frame_get_u8(frame, &seid))
  437. print_field("ACP SEID: %d", seid >> 2);
  438. return true;
  439. case AVDTP_MSG_TYPE_RESPONSE_ACCEPT:
  440. return true;
  441. case AVDTP_MSG_TYPE_RESPONSE_REJECT:
  442. if (!l2cap_frame_get_u8(frame, &seid))
  443. return false;
  444. print_field("ACP SEID: %d", seid >> 2);
  445. return avdtp_reject_common(avdtp_frame);
  446. }
  447. return false;
  448. }
  449. static bool avdtp_close(struct avdtp_frame *avdtp_frame)
  450. {
  451. struct l2cap_frame *frame = &avdtp_frame->l2cap_frame;
  452. uint8_t type = avdtp_frame->hdr & 0x03;
  453. uint8_t seid;
  454. switch (type) {
  455. case AVDTP_MSG_TYPE_COMMAND:
  456. if (!l2cap_frame_get_u8(frame, &seid))
  457. return false;
  458. print_field("ACP SEID: %d", seid >> 2);
  459. return true;
  460. case AVDTP_MSG_TYPE_RESPONSE_ACCEPT:
  461. return true;
  462. case AVDTP_MSG_TYPE_RESPONSE_REJECT:
  463. return avdtp_reject_common(avdtp_frame);
  464. }
  465. return false;
  466. }
  467. static bool avdtp_suspend(struct avdtp_frame *avdtp_frame)
  468. {
  469. struct l2cap_frame *frame = &avdtp_frame->l2cap_frame;
  470. uint8_t type = avdtp_frame->hdr & 0x03;
  471. uint8_t seid;
  472. switch (type) {
  473. case AVDTP_MSG_TYPE_COMMAND:
  474. if (!l2cap_frame_get_u8(frame, &seid))
  475. return false;
  476. print_field("ACP SEID: %d", seid >> 2);
  477. while (l2cap_frame_get_u8(frame, &seid))
  478. print_field("ACP SEID: %d", seid >> 2);
  479. return true;
  480. case AVDTP_MSG_TYPE_RESPONSE_ACCEPT:
  481. return true;
  482. case AVDTP_MSG_TYPE_RESPONSE_REJECT:
  483. if (!l2cap_frame_get_u8(frame, &seid))
  484. return false;
  485. print_field("ACP SEID: %d", seid >> 2);
  486. return avdtp_reject_common(avdtp_frame);
  487. }
  488. return false;
  489. }
  490. static bool avdtp_abort(struct avdtp_frame *avdtp_frame)
  491. {
  492. struct l2cap_frame *frame = &avdtp_frame->l2cap_frame;
  493. uint8_t type = avdtp_frame->hdr & 0x03;
  494. uint8_t seid;
  495. switch (type) {
  496. case AVDTP_MSG_TYPE_COMMAND:
  497. if (!l2cap_frame_get_u8(frame, &seid))
  498. return false;
  499. print_field("ACP SEID: %d", seid >> 2);
  500. return true;
  501. case AVDTP_MSG_TYPE_RESPONSE_ACCEPT:
  502. return true;
  503. }
  504. return false;
  505. }
  506. static bool avdtp_security_control(struct avdtp_frame *avdtp_frame)
  507. {
  508. struct l2cap_frame *frame = &avdtp_frame->l2cap_frame;
  509. uint8_t type = avdtp_frame->hdr & 0x03;
  510. uint8_t seid;
  511. switch (type) {
  512. case AVDTP_MSG_TYPE_COMMAND:
  513. if (!l2cap_frame_get_u8(frame, &seid))
  514. return false;
  515. print_field("ACP SEID: %d", seid >> 2);
  516. /* TODO: decode more information */
  517. packet_hexdump(frame->data, frame->size);
  518. return true;
  519. case AVDTP_MSG_TYPE_RESPONSE_ACCEPT:
  520. /* TODO: decode more information */
  521. packet_hexdump(frame->data, frame->size);
  522. return true;
  523. case AVDTP_MSG_TYPE_RESPONSE_REJECT:
  524. return avdtp_reject_common(avdtp_frame);
  525. }
  526. return false;
  527. }
  528. static bool avdtp_delayreport(struct avdtp_frame *avdtp_frame)
  529. {
  530. struct l2cap_frame *frame = &avdtp_frame->l2cap_frame;
  531. uint8_t type = avdtp_frame->hdr & 0x03;
  532. uint8_t seid;
  533. uint16_t delay;
  534. switch (type) {
  535. case AVDTP_MSG_TYPE_COMMAND:
  536. if (!l2cap_frame_get_u8(frame, &seid))
  537. return false;
  538. print_field("ACP SEID: %d", seid >> 2);
  539. if (!l2cap_frame_get_be16(frame, &delay))
  540. return false;
  541. print_field("Delay: %d.%dms", delay / 10, delay % 10);
  542. return true;
  543. case AVDTP_MSG_TYPE_RESPONSE_ACCEPT:
  544. return true;
  545. case AVDTP_MSG_TYPE_RESPONSE_REJECT:
  546. return avdtp_reject_common(avdtp_frame);
  547. }
  548. return false;
  549. }
  550. static bool avdtp_signalling_packet(struct avdtp_frame *avdtp_frame)
  551. {
  552. struct l2cap_frame *frame = &avdtp_frame->l2cap_frame;
  553. const char *pdu_color;
  554. uint8_t hdr;
  555. uint8_t sig_id;
  556. uint8_t nosp = 0;
  557. if (frame->in)
  558. pdu_color = COLOR_MAGENTA;
  559. else
  560. pdu_color = COLOR_BLUE;
  561. if (!l2cap_frame_get_u8(frame, &hdr))
  562. return false;
  563. avdtp_frame->hdr = hdr;
  564. /* Continue Packet || End Packet */
  565. if (((hdr & 0x0c) == 0x08) || ((hdr & 0x0c) == 0x0c)) {
  566. /* TODO: handle fragmentation */
  567. packet_hexdump(frame->data, frame->size);
  568. return true;
  569. }
  570. /* Start Packet */
  571. if ((hdr & 0x0c) == 0x04) {
  572. if (!l2cap_frame_get_u8(frame, &nosp))
  573. return false;
  574. }
  575. if (!l2cap_frame_get_u8(frame, &sig_id))
  576. return false;
  577. sig_id &= 0x3f;
  578. avdtp_frame->sig_id = sig_id;
  579. print_indent(6, pdu_color, "AVDTP: ", sigid2str(sig_id), COLOR_OFF,
  580. " (0x%02x) %s (0x%02x) type 0x%02x label %d nosp %d",
  581. sig_id, msgtype2str(hdr & 0x03), hdr & 0x03,
  582. hdr & 0x0c, hdr >> 4, nosp);
  583. /* Start Packet */
  584. if ((hdr & 0x0c) == 0x04) {
  585. /* TODO: handle fragmentation */
  586. packet_hexdump(frame->data, frame->size);
  587. return true;
  588. }
  589. switch (sig_id) {
  590. case AVDTP_DISCOVER:
  591. return avdtp_discover(avdtp_frame);
  592. case AVDTP_GET_CAPABILITIES:
  593. case AVDTP_GET_ALL_CAPABILITIES:
  594. return avdtp_get_capabilities(avdtp_frame);
  595. case AVDTP_SET_CONFIGURATION:
  596. return avdtp_set_configuration(avdtp_frame);
  597. case AVDTP_GET_CONFIGURATION:
  598. return avdtp_get_configuration(avdtp_frame);
  599. case AVDTP_RECONFIGURE:
  600. return avdtp_reconfigure(avdtp_frame);
  601. case AVDTP_OPEN:
  602. return avdtp_open(avdtp_frame);
  603. case AVDTP_START:
  604. return avdtp_start(avdtp_frame);
  605. case AVDTP_CLOSE:
  606. return avdtp_close(avdtp_frame);
  607. case AVDTP_SUSPEND:
  608. return avdtp_suspend(avdtp_frame);
  609. case AVDTP_ABORT:
  610. return avdtp_abort(avdtp_frame);
  611. case AVDTP_SECURITY_CONTROL:
  612. return avdtp_security_control(avdtp_frame);
  613. case AVDTP_DELAYREPORT:
  614. return avdtp_delayreport(avdtp_frame);
  615. }
  616. packet_hexdump(frame->data, frame->size);
  617. return true;
  618. }
  619. void avdtp_packet(const struct l2cap_frame *frame)
  620. {
  621. struct avdtp_frame avdtp_frame;
  622. bool ret;
  623. l2cap_frame_pull(&avdtp_frame.l2cap_frame, frame, 0);
  624. switch (frame->seq_num) {
  625. case 1:
  626. ret = avdtp_signalling_packet(&avdtp_frame);
  627. break;
  628. default:
  629. if (packet_has_filter(PACKET_FILTER_SHOW_A2DP_STREAM))
  630. packet_hexdump(frame->data, frame->size);
  631. return;
  632. }
  633. if (!ret) {
  634. print_text(COLOR_ERROR, "PDU malformed");
  635. packet_hexdump(frame->data, frame->size);
  636. }
  637. }