avinfo.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2006-2010 Nokia Corporation
  7. * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
  8. * Copyright (C) 2018 Pali Rohár <pali.rohar@gmail.com>
  9. *
  10. *
  11. */
  12. #ifdef HAVE_CONFIG_H
  13. #include <config.h>
  14. #endif
  15. #include <stdio.h>
  16. #include <errno.h>
  17. #include <ctype.h>
  18. #include <fcntl.h>
  19. #include <unistd.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <getopt.h>
  23. #include <stdint.h>
  24. #include <sys/param.h>
  25. #include <sys/ioctl.h>
  26. #include <sys/socket.h>
  27. #include "lib/bluetooth.h"
  28. #include "lib/hci.h"
  29. #include "lib/hci_lib.h"
  30. #include "lib/l2cap.h"
  31. #include "profiles/audio/a2dp-codecs.h"
  32. #define AVDTP_PSM 25
  33. /* Commands */
  34. #define AVDTP_DISCOVER 0x01
  35. #define AVDTP_GET_CAPABILITIES 0x02
  36. #define AVDTP_PKT_TYPE_SINGLE 0x00
  37. #define AVDTP_MSG_TYPE_COMMAND 0x00
  38. /* SEP capability categories */
  39. #define AVDTP_MEDIA_TRANSPORT 0x01
  40. #define AVDTP_REPORTING 0x02
  41. #define AVDTP_RECOVERY 0x03
  42. #define AVDTP_CONTENT_PROTECTION 0x04
  43. #define AVDTP_HEADER_COMPRESSION 0x05
  44. #define AVDTP_MULTIPLEXING 0x06
  45. #define AVDTP_MEDIA_CODEC 0x07
  46. /* SEP types definitions */
  47. #define AVDTP_SEP_TYPE_SOURCE 0x00
  48. #define AVDTP_SEP_TYPE_SINK 0x01
  49. /* Media types definitions */
  50. #define AVDTP_MEDIA_TYPE_AUDIO 0x00
  51. #define AVDTP_MEDIA_TYPE_VIDEO 0x01
  52. #define AVDTP_MEDIA_TYPE_MULTIMEDIA 0x02
  53. /* Content Protection types definitions */
  54. #define AVDTP_CONTENT_PROTECTION_TYPE_DTCP 0x0001
  55. #define AVDTP_CONTENT_PROTECTION_TYPE_SCMS_T 0x0002
  56. struct avdtp_service_capability {
  57. uint8_t category;
  58. uint8_t length;
  59. uint8_t data[0];
  60. } __attribute__ ((packed));
  61. #if __BYTE_ORDER == __LITTLE_ENDIAN
  62. struct avdtp_header {
  63. uint8_t message_type:2;
  64. uint8_t packet_type:2;
  65. uint8_t transaction:4;
  66. uint8_t signal_id:6;
  67. uint8_t rfa0:2;
  68. } __attribute__ ((packed));
  69. struct seid_info {
  70. uint8_t rfa0:1;
  71. uint8_t inuse:1;
  72. uint8_t seid:6;
  73. uint8_t rfa2:3;
  74. uint8_t type:1;
  75. uint8_t media_type:4;
  76. } __attribute__ ((packed));
  77. struct seid_req {
  78. struct avdtp_header header;
  79. uint8_t rfa0:2;
  80. uint8_t acp_seid:6;
  81. } __attribute__ ((packed));
  82. struct avdtp_media_codec_capability {
  83. uint8_t rfa0:4;
  84. uint8_t media_type:4;
  85. uint8_t media_codec_type;
  86. uint8_t data[0];
  87. } __attribute__ ((packed));
  88. #elif __BYTE_ORDER == __BIG_ENDIAN
  89. struct avdtp_header {
  90. uint8_t transaction:4;
  91. uint8_t packet_type:2;
  92. uint8_t message_type:2;
  93. uint8_t rfa0:2;
  94. uint8_t signal_id:6;
  95. } __attribute__ ((packed));
  96. struct seid_info {
  97. uint8_t seid:6;
  98. uint8_t inuse:1;
  99. uint8_t rfa0:1;
  100. uint8_t media_type:4;
  101. uint8_t type:1;
  102. uint8_t rfa2:3;
  103. } __attribute__ ((packed));
  104. struct seid_req {
  105. struct avdtp_header header;
  106. uint8_t acp_seid:6;
  107. uint8_t rfa0:2;
  108. } __attribute__ ((packed));
  109. struct avdtp_media_codec_capability {
  110. uint8_t media_type:4;
  111. uint8_t rfa0:4;
  112. uint8_t media_codec_type;
  113. uint8_t data[0];
  114. } __attribute__ ((packed));
  115. #else
  116. #error "Unknown byte order"
  117. #endif
  118. struct discover_resp {
  119. struct avdtp_header header;
  120. struct seid_info seps[0];
  121. } __attribute__ ((packed));
  122. struct getcap_resp {
  123. struct avdtp_header header;
  124. uint8_t caps[0];
  125. } __attribute__ ((packed));
  126. struct avdtp_content_protection_capability {
  127. uint16_t content_protection_type;
  128. uint8_t data[0];
  129. } __attribute__ ((packed));
  130. static void print_aptx_common(a2dp_aptx_t *aptx)
  131. {
  132. printf("\n\t\t\tFrequencies: ");
  133. if (aptx->frequency & APTX_SAMPLING_FREQ_16000)
  134. printf("16kHz ");
  135. if (aptx->frequency & APTX_SAMPLING_FREQ_32000)
  136. printf("32kHz ");
  137. if (aptx->frequency & APTX_SAMPLING_FREQ_44100)
  138. printf("44.1kHz ");
  139. if (aptx->frequency & APTX_SAMPLING_FREQ_48000)
  140. printf("48kHz ");
  141. printf("\n\t\t\tChannel modes: ");
  142. if (aptx->channel_mode & APTX_CHANNEL_MODE_MONO)
  143. printf("Mono ");
  144. if (aptx->channel_mode & APTX_CHANNEL_MODE_STEREO)
  145. printf("Stereo ");
  146. }
  147. static void print_aptx(a2dp_aptx_t *aptx, uint8_t size)
  148. {
  149. printf("\t\tVendor Specific Value (aptX)");
  150. if (size < sizeof(*aptx)) {
  151. printf(" (broken)\n");
  152. return;
  153. }
  154. print_aptx_common(aptx);
  155. printf("\n");
  156. }
  157. static void print_faststream(a2dp_faststream_t *faststream, uint8_t size)
  158. {
  159. printf("\t\tVendor Specific Value (FastStream)");
  160. if (size < sizeof(*faststream)) {
  161. printf(" (broken)\n");
  162. return;
  163. }
  164. printf("\n\t\t\tDirections: ");
  165. if (faststream->direction & FASTSTREAM_DIRECTION_SINK)
  166. printf("sink ");
  167. if (faststream->direction & FASTSTREAM_DIRECTION_SOURCE)
  168. printf("source ");
  169. if (faststream->direction & FASTSTREAM_DIRECTION_SINK) {
  170. printf("\n\t\t\tSink Frequencies: ");
  171. if (faststream->sink_frequency &
  172. FASTSTREAM_SINK_SAMPLING_FREQ_44100)
  173. printf("44.1kHz ");
  174. if (faststream->sink_frequency &
  175. FASTSTREAM_SINK_SAMPLING_FREQ_48000)
  176. printf("48kHz ");
  177. }
  178. if (faststream->direction & FASTSTREAM_DIRECTION_SOURCE) {
  179. printf("\n\t\t\tSource Frequencies: ");
  180. if (faststream->source_frequency &
  181. FASTSTREAM_SOURCE_SAMPLING_FREQ_16000)
  182. printf("16kHz ");
  183. }
  184. printf("\n");
  185. }
  186. static void print_aptx_ll(a2dp_aptx_ll_t *aptx_ll, uint8_t size)
  187. {
  188. a2dp_aptx_ll_new_caps_t *aptx_ll_new;
  189. printf("\t\tVendor Specific Value (aptX Low Latency)");
  190. if (size < sizeof(*aptx_ll)) {
  191. printf(" (broken)\n");
  192. return;
  193. }
  194. print_aptx_common(&aptx_ll->aptx);
  195. printf("\n\t\tBidirectional link: %s",
  196. aptx_ll->bidirect_link ? "Yes" : "No");
  197. aptx_ll_new = &aptx_ll->new_caps[0];
  198. if (aptx_ll->has_new_caps &&
  199. size >= sizeof(*aptx_ll) + sizeof(*aptx_ll_new)) {
  200. printf("\n\t\tTarget codec buffer level: %u",
  201. (unsigned int)aptx_ll_new->target_level2 |
  202. ((unsigned int)(aptx_ll_new->target_level1) << 8));
  203. printf("\n\t\tInitial codec buffer level: %u",
  204. (unsigned int)aptx_ll_new->initial_level2 |
  205. ((unsigned int)(aptx_ll_new->initial_level1) << 8));
  206. printf("\n\t\tSRA max rate: %g",
  207. aptx_ll_new->sra_max_rate / 10000.0);
  208. printf("\n\t\tSRA averaging time: %us",
  209. (unsigned int)aptx_ll_new->sra_avg_time);
  210. printf("\n\t\tGood working codec buffer level: %u",
  211. (unsigned int)aptx_ll_new->good_working_level2 |
  212. ((unsigned int)(aptx_ll_new->good_working_level1) << 8)
  213. );
  214. }
  215. printf("\n");
  216. }
  217. static void print_aptx_hd(a2dp_aptx_hd_t *aptx_hd, uint8_t size)
  218. {
  219. printf("\t\tVendor Specific Value (aptX HD)");
  220. if (size < sizeof(*aptx_hd)) {
  221. printf(" (broken)\n");
  222. return;
  223. }
  224. print_aptx_common(&aptx_hd->aptx);
  225. printf("\n");
  226. }
  227. static void print_ldac(a2dp_ldac_t *ldac, uint8_t size)
  228. {
  229. printf("\t\tVendor Specific Value (LDAC)");
  230. if (size < sizeof(*ldac)) {
  231. printf(" (broken)\n");
  232. return;
  233. }
  234. printf("\n\t\t\tFrequencies: ");
  235. if (ldac->frequency & LDAC_SAMPLING_FREQ_44100)
  236. printf("44.1kHz ");
  237. if (ldac->frequency & LDAC_SAMPLING_FREQ_48000)
  238. printf("48kHz ");
  239. if (ldac->frequency & LDAC_SAMPLING_FREQ_88200)
  240. printf("88.2kHz ");
  241. if (ldac->frequency & LDAC_SAMPLING_FREQ_96000)
  242. printf("96kHz ");
  243. if (ldac->frequency & LDAC_SAMPLING_FREQ_176400)
  244. printf("176.4kHz ");
  245. if (ldac->frequency & LDAC_SAMPLING_FREQ_192000)
  246. printf("192kHz ");
  247. printf("\n\t\t\tChannel modes: ");
  248. if (ldac->channel_mode & LDAC_CHANNEL_MODE_MONO)
  249. printf("Mono ");
  250. if (ldac->channel_mode & LDAC_CHANNEL_MODE_DUAL)
  251. printf("Dual ");
  252. if (ldac->channel_mode & LDAC_CHANNEL_MODE_STEREO)
  253. printf("Stereo ");
  254. printf("\n");
  255. }
  256. static void print_vendor(a2dp_vendor_codec_t *vendor, uint8_t size)
  257. {
  258. uint32_t vendor_id;
  259. uint16_t codec_id;
  260. int i;
  261. if (size < sizeof(*vendor)) {
  262. printf("\tMedia Codec: Vendor Specific A2DP Codec (broken)");
  263. return;
  264. }
  265. vendor_id = A2DP_GET_VENDOR_ID(*vendor);
  266. codec_id = A2DP_GET_CODEC_ID(*vendor);
  267. printf("\tMedia Codec: Vendor Specific A2DP Codec");
  268. printf("\n\t\tVendor ID 0x%08x", vendor_id);
  269. printf("\n\t\tVendor Specific Codec ID 0x%04x", codec_id);
  270. printf("\n\t\tVendor Specific Data:");
  271. for (i = 6; i < size; ++i)
  272. printf(" 0x%.02x", ((unsigned char *)vendor)[i]);
  273. printf("\n");
  274. if (vendor_id == APTX_VENDOR_ID && codec_id == APTX_CODEC_ID)
  275. print_aptx((void *) vendor, size);
  276. else if (vendor_id == FASTSTREAM_VENDOR_ID &&
  277. codec_id == FASTSTREAM_CODEC_ID)
  278. print_faststream((void *) vendor, size);
  279. else if (vendor_id == APTX_LL_VENDOR_ID && codec_id == APTX_LL_CODEC_ID)
  280. print_aptx_ll((void *) vendor, size);
  281. else if (vendor_id == APTX_HD_VENDOR_ID && codec_id == APTX_HD_CODEC_ID)
  282. print_aptx_hd((void *) vendor, size);
  283. else if (vendor_id == LDAC_VENDOR_ID && codec_id == LDAC_CODEC_ID)
  284. print_ldac((void *) vendor, size);
  285. }
  286. static void print_mpeg24(a2dp_aac_t *aac, uint8_t size)
  287. {
  288. unsigned int freq, bitrate;
  289. if (size < sizeof(*aac)) {
  290. printf("\tMedia Codec: MPEG24 (broken)\n");
  291. return;
  292. }
  293. freq = AAC_GET_FREQUENCY(*aac);
  294. bitrate = AAC_GET_BITRATE(*aac);
  295. printf("\tMedia Codec: MPEG24\n\t\tObject Types: ");
  296. if (aac->object_type & AAC_OBJECT_TYPE_MPEG2_AAC_LC)
  297. printf("MPEG-2 AAC LC ");
  298. if (aac->object_type & AAC_OBJECT_TYPE_MPEG4_AAC_LC)
  299. printf("MPEG-4 AAC LC ");
  300. if (aac->object_type & AAC_OBJECT_TYPE_MPEG4_AAC_LTP)
  301. printf("MPEG-4 AAC LTP ");
  302. if (aac->object_type & AAC_OBJECT_TYPE_MPEG4_AAC_SCA)
  303. printf("MPEG-4 AAC scalable ");
  304. printf("\n\t\tFrequencies: ");
  305. if (freq & AAC_SAMPLING_FREQ_8000)
  306. printf("8kHz ");
  307. if (freq & AAC_SAMPLING_FREQ_11025)
  308. printf("11.025kHz ");
  309. if (freq & AAC_SAMPLING_FREQ_12000)
  310. printf("12kHz ");
  311. if (freq & AAC_SAMPLING_FREQ_16000)
  312. printf("16kHz ");
  313. if (freq & AAC_SAMPLING_FREQ_22050)
  314. printf("22.05kHz ");
  315. if (freq & AAC_SAMPLING_FREQ_24000)
  316. printf("24kHz ");
  317. if (freq & AAC_SAMPLING_FREQ_32000)
  318. printf("32kHz ");
  319. if (freq & AAC_SAMPLING_FREQ_44100)
  320. printf("44.1kHz ");
  321. if (freq & AAC_SAMPLING_FREQ_48000)
  322. printf("48kHz ");
  323. if (freq & AAC_SAMPLING_FREQ_64000)
  324. printf("64kHz ");
  325. if (freq & AAC_SAMPLING_FREQ_88200)
  326. printf("88.2kHz ");
  327. if (freq & AAC_SAMPLING_FREQ_96000)
  328. printf("96kHz ");
  329. printf("\n\t\tChannels: ");
  330. if (aac->channels & AAC_CHANNELS_1)
  331. printf("1 ");
  332. if (aac->channels & AAC_CHANNELS_2)
  333. printf("2 ");
  334. printf("\n\t\tBitrate: %u", bitrate);
  335. printf("\n\t\tVBR: %s", aac->vbr ? "Yes\n" : "No\n");
  336. }
  337. static void print_mpeg12(a2dp_mpeg_t *mpeg, uint8_t size)
  338. {
  339. uint16_t bitrate;
  340. if (size < sizeof(*mpeg)) {
  341. printf("\tMedia Codec: MPEG12 (broken)\n");
  342. return;
  343. }
  344. bitrate = MPEG_GET_BITRATE(*mpeg);
  345. printf("\tMedia Codec: MPEG12\n\t\tChannel Modes: ");
  346. if (mpeg->channel_mode & MPEG_CHANNEL_MODE_MONO)
  347. printf("Mono ");
  348. if (mpeg->channel_mode & MPEG_CHANNEL_MODE_DUAL_CHANNEL)
  349. printf("DualChannel ");
  350. if (mpeg->channel_mode & MPEG_CHANNEL_MODE_STEREO)
  351. printf("Stereo ");
  352. if (mpeg->channel_mode & MPEG_CHANNEL_MODE_JOINT_STEREO)
  353. printf("JointStereo");
  354. printf("\n\t\tFrequencies: ");
  355. if (mpeg->frequency & MPEG_SAMPLING_FREQ_16000)
  356. printf("16Khz ");
  357. if (mpeg->frequency & MPEG_SAMPLING_FREQ_22050)
  358. printf("22.05Khz ");
  359. if (mpeg->frequency & MPEG_SAMPLING_FREQ_24000)
  360. printf("24Khz ");
  361. if (mpeg->frequency & MPEG_SAMPLING_FREQ_32000)
  362. printf("32Khz ");
  363. if (mpeg->frequency & MPEG_SAMPLING_FREQ_44100)
  364. printf("44.1Khz ");
  365. if (mpeg->frequency & MPEG_SAMPLING_FREQ_48000)
  366. printf("48Khz ");
  367. printf("\n\t\tCRC: %s", mpeg->crc ? "Yes" : "No");
  368. printf("\n\t\tLayer: ");
  369. if (mpeg->layer & MPEG_LAYER_MP1)
  370. printf("1 ");
  371. if (mpeg->layer & MPEG_LAYER_MP2)
  372. printf("2 ");
  373. if (mpeg->layer & MPEG_LAYER_MP3)
  374. printf("3 ");
  375. if (bitrate & MPEG_BIT_RATE_FREE) {
  376. printf("\n\t\tBit Rate: Free format");
  377. } else {
  378. if (mpeg->layer & MPEG_LAYER_MP1) {
  379. printf("\n\t\tLayer 1 Bit Rate: ");
  380. if (bitrate & MPEG_MP1_BIT_RATE_32000)
  381. printf("32kbps ");
  382. if (bitrate & MPEG_MP1_BIT_RATE_64000)
  383. printf("64kbps ");
  384. if (bitrate & MPEG_MP1_BIT_RATE_96000)
  385. printf("96kbps ");
  386. if (bitrate & MPEG_MP1_BIT_RATE_128000)
  387. printf("128kbps ");
  388. if (bitrate & MPEG_MP1_BIT_RATE_160000)
  389. printf("160kbps ");
  390. if (bitrate & MPEG_MP1_BIT_RATE_192000)
  391. printf("192kbps ");
  392. if (bitrate & MPEG_MP1_BIT_RATE_224000)
  393. printf("224kbps ");
  394. if (bitrate & MPEG_MP1_BIT_RATE_256000)
  395. printf("256kbps ");
  396. if (bitrate & MPEG_MP1_BIT_RATE_320000)
  397. printf("320kbps ");
  398. if (bitrate & MPEG_MP1_BIT_RATE_352000)
  399. printf("352kbps ");
  400. if (bitrate & MPEG_MP1_BIT_RATE_384000)
  401. printf("384kbps ");
  402. if (bitrate & MPEG_MP1_BIT_RATE_416000)
  403. printf("416kbps ");
  404. if (bitrate & MPEG_MP1_BIT_RATE_448000)
  405. printf("448kbps ");
  406. }
  407. if (mpeg->layer & MPEG_LAYER_MP2) {
  408. printf("\n\t\tLayer 2 Bit Rate: ");
  409. if (bitrate & MPEG_MP2_BIT_RATE_32000)
  410. printf("32kbps ");
  411. if (bitrate & MPEG_MP2_BIT_RATE_48000)
  412. printf("48kbps ");
  413. if (bitrate & MPEG_MP2_BIT_RATE_56000)
  414. printf("56kbps ");
  415. if (bitrate & MPEG_MP2_BIT_RATE_64000)
  416. printf("64kbps ");
  417. if (bitrate & MPEG_MP2_BIT_RATE_80000)
  418. printf("80kbps ");
  419. if (bitrate & MPEG_MP2_BIT_RATE_96000)
  420. printf("96kbps ");
  421. if (bitrate & MPEG_MP2_BIT_RATE_112000)
  422. printf("112kbps ");
  423. if (bitrate & MPEG_MP2_BIT_RATE_128000)
  424. printf("128kbps ");
  425. if (bitrate & MPEG_MP2_BIT_RATE_160000)
  426. printf("160kbps ");
  427. if (bitrate & MPEG_MP2_BIT_RATE_192000)
  428. printf("192kbps ");
  429. if (bitrate & MPEG_MP2_BIT_RATE_224000)
  430. printf("224kbps ");
  431. if (bitrate & MPEG_MP2_BIT_RATE_256000)
  432. printf("256kbps ");
  433. if (bitrate & MPEG_MP2_BIT_RATE_320000)
  434. printf("320kbps ");
  435. if (bitrate & MPEG_MP2_BIT_RATE_384000)
  436. printf("384kbps ");
  437. }
  438. if (mpeg->layer & MPEG_LAYER_MP3) {
  439. printf("\n\t\tLayer 3 Bit Rate: ");
  440. if (bitrate & MPEG_MP3_BIT_RATE_32000)
  441. printf("32kbps ");
  442. if (bitrate & MPEG_MP3_BIT_RATE_40000)
  443. printf("40kbps ");
  444. if (bitrate & MPEG_MP3_BIT_RATE_48000)
  445. printf("48kbps ");
  446. if (bitrate & MPEG_MP3_BIT_RATE_56000)
  447. printf("56kbps ");
  448. if (bitrate & MPEG_MP3_BIT_RATE_64000)
  449. printf("64kbps ");
  450. if (bitrate & MPEG_MP3_BIT_RATE_80000)
  451. printf("80kbps ");
  452. if (bitrate & MPEG_MP3_BIT_RATE_96000)
  453. printf("96kbps ");
  454. if (bitrate & MPEG_MP3_BIT_RATE_112000)
  455. printf("112kbps ");
  456. if (bitrate & MPEG_MP3_BIT_RATE_128000)
  457. printf("128kbps ");
  458. if (bitrate & MPEG_MP3_BIT_RATE_160000)
  459. printf("160kbps ");
  460. if (bitrate & MPEG_MP3_BIT_RATE_192000)
  461. printf("192kbps ");
  462. if (bitrate & MPEG_MP3_BIT_RATE_224000)
  463. printf("224kbps ");
  464. if (bitrate & MPEG_MP3_BIT_RATE_256000)
  465. printf("256kbps ");
  466. if (bitrate & MPEG_MP3_BIT_RATE_320000)
  467. printf("320kbps ");
  468. }
  469. }
  470. printf("\n\t\tVBR: %s", mpeg->vbr ? "Yes" : "No");
  471. printf("\n\t\tPayload Format: ");
  472. if (mpeg->mpf)
  473. printf("RFC-2250 RFC-3119\n");
  474. else
  475. printf("RFC-2250\n");
  476. }
  477. static void print_sbc(a2dp_sbc_t *sbc, uint8_t size)
  478. {
  479. if (size < sizeof(*sbc)) {
  480. printf("\tMedia Codec: SBC (broken)\n");
  481. return;
  482. }
  483. printf("\tMedia Codec: SBC\n\t\tChannel Modes: ");
  484. if (sbc->channel_mode & SBC_CHANNEL_MODE_MONO)
  485. printf("Mono ");
  486. if (sbc->channel_mode & SBC_CHANNEL_MODE_DUAL_CHANNEL)
  487. printf("DualChannel ");
  488. if (sbc->channel_mode & SBC_CHANNEL_MODE_STEREO)
  489. printf("Stereo ");
  490. if (sbc->channel_mode & SBC_CHANNEL_MODE_JOINT_STEREO)
  491. printf("JointStereo");
  492. printf("\n\t\tFrequencies: ");
  493. if (sbc->frequency & SBC_SAMPLING_FREQ_16000)
  494. printf("16Khz ");
  495. if (sbc->frequency & SBC_SAMPLING_FREQ_32000)
  496. printf("32Khz ");
  497. if (sbc->frequency & SBC_SAMPLING_FREQ_44100)
  498. printf("44.1Khz ");
  499. if (sbc->frequency & SBC_SAMPLING_FREQ_48000)
  500. printf("48Khz ");
  501. printf("\n\t\tSubbands: ");
  502. if (sbc->allocation_method & SBC_SUBBANDS_4)
  503. printf("4 ");
  504. if (sbc->allocation_method & SBC_SUBBANDS_8)
  505. printf("8");
  506. printf("\n\t\tBlocks: ");
  507. if (sbc->block_length & SBC_BLOCK_LENGTH_4)
  508. printf("4 ");
  509. if (sbc->block_length & SBC_BLOCK_LENGTH_8)
  510. printf("8 ");
  511. if (sbc->block_length & SBC_BLOCK_LENGTH_12)
  512. printf("12 ");
  513. if (sbc->block_length & SBC_BLOCK_LENGTH_16)
  514. printf("16 ");
  515. printf("\n\t\tBitpool Range: %d-%d\n",
  516. sbc->min_bitpool, sbc->max_bitpool);
  517. }
  518. static void print_media_codec(
  519. struct avdtp_media_codec_capability *cap,
  520. uint8_t size)
  521. {
  522. int i;
  523. if (size < sizeof(*cap)) {
  524. printf("\tMedia Codec: Unknown (broken)\n");
  525. return;
  526. }
  527. switch (cap->media_codec_type) {
  528. case A2DP_CODEC_SBC:
  529. print_sbc((void *) cap->data, size - 2);
  530. break;
  531. case A2DP_CODEC_MPEG12:
  532. print_mpeg12((void *) cap->data, size - 2);
  533. break;
  534. case A2DP_CODEC_MPEG24:
  535. print_mpeg24((void *) cap->data, size - 2);
  536. break;
  537. case A2DP_CODEC_VENDOR:
  538. print_vendor((void *) cap->data, size - 2);
  539. break;
  540. default:
  541. printf("\tMedia Codec: Unknown\n");
  542. printf("\t\tCodec Data:");
  543. for (i = 0; i < size - 2; ++i)
  544. printf(" 0x%.02x", ((unsigned char *)cap->data)[i]);
  545. printf("\n");
  546. }
  547. }
  548. static void print_content_protection(
  549. struct avdtp_content_protection_capability *cap,
  550. uint8_t size)
  551. {
  552. printf("\tContent Protection: ");
  553. if (size < sizeof(*cap)) {
  554. printf("Unknown (broken)\n");
  555. return;
  556. }
  557. switch (btohs(cap->content_protection_type)) {
  558. case AVDTP_CONTENT_PROTECTION_TYPE_DTCP:
  559. printf("DTCP");
  560. break;
  561. case AVDTP_CONTENT_PROTECTION_TYPE_SCMS_T:
  562. printf("SCMS-T");
  563. break;
  564. default:
  565. printf("Unknown");
  566. }
  567. printf("\n");
  568. }
  569. static void print_caps(void *data, int size)
  570. {
  571. int processed;
  572. int i;
  573. for (processed = 0; processed + 2 < size;) {
  574. struct avdtp_service_capability *cap;
  575. cap = data;
  576. if (processed + 2 + cap->length > size) {
  577. printf("Invalid capability data in getcap resp\n");
  578. break;
  579. }
  580. switch (cap->category) {
  581. case AVDTP_MEDIA_TRANSPORT:
  582. case AVDTP_REPORTING:
  583. case AVDTP_RECOVERY:
  584. case AVDTP_MULTIPLEXING:
  585. /* FIXME: Add proper functions */
  586. break;
  587. default:
  588. printf("\tUnknown category: %d\n", cap->category);
  589. printf("\t\tData:");
  590. for (i = 0; i < cap->length; ++i)
  591. printf(" 0x%.02x",
  592. ((unsigned char *)cap->data)[i]);
  593. printf("\n");
  594. break;
  595. case AVDTP_MEDIA_CODEC:
  596. print_media_codec((void *) cap->data, cap->length);
  597. break;
  598. case AVDTP_CONTENT_PROTECTION:
  599. print_content_protection((void *) cap->data,
  600. cap->length);
  601. break;
  602. }
  603. processed += 2 + cap->length;
  604. data += 2 + cap->length;
  605. }
  606. }
  607. static void init_request(struct avdtp_header *header, int request_id)
  608. {
  609. static int transaction = 0;
  610. header->packet_type = AVDTP_PKT_TYPE_SINGLE;
  611. header->message_type = AVDTP_MSG_TYPE_COMMAND;
  612. header->transaction = transaction;
  613. header->signal_id = request_id;
  614. /* clear rfa bits */
  615. header->rfa0 = 0;
  616. transaction = (transaction + 1) % 16;
  617. }
  618. static ssize_t avdtp_send(int sk, void *data, int len)
  619. {
  620. ssize_t ret;
  621. ret = send(sk, data, len, 0);
  622. if (ret < 0)
  623. ret = -errno;
  624. else if (ret != len)
  625. ret = -EIO;
  626. if (ret < 0) {
  627. printf("Unable to send message: %s (%zd)\n",
  628. strerror(-ret), -ret);
  629. return ret;
  630. }
  631. return ret;
  632. }
  633. static ssize_t avdtp_receive(int sk, void *data, int len)
  634. {
  635. ssize_t ret;
  636. ret = recv(sk, data, len, 0);
  637. if (ret < 0) {
  638. printf("Unable to receive message: %s (%d)\n",
  639. strerror(errno), errno);
  640. return -errno;
  641. }
  642. return ret;
  643. }
  644. static ssize_t avdtp_get_caps(int sk, int seid)
  645. {
  646. struct seid_req req;
  647. char buffer[1024];
  648. struct getcap_resp *caps = (void *) buffer;
  649. ssize_t ret;
  650. memset(&req, 0, sizeof(req));
  651. init_request(&req.header, AVDTP_GET_CAPABILITIES);
  652. req.acp_seid = seid;
  653. ret = avdtp_send(sk, &req, sizeof(req));
  654. if (ret < 0)
  655. return ret;
  656. memset(&buffer, 0, sizeof(buffer));
  657. ret = avdtp_receive(sk, caps, sizeof(buffer));
  658. if (ret < 0)
  659. return ret;
  660. if ((size_t) ret < (sizeof(struct getcap_resp) + 4 +
  661. sizeof(struct avdtp_media_codec_capability))) {
  662. printf("Invalid capabilities\n");
  663. return -1;
  664. }
  665. print_caps(caps->caps, ret - sizeof(struct getcap_resp));
  666. return 0;
  667. }
  668. static ssize_t avdtp_discover(int sk)
  669. {
  670. struct avdtp_header req;
  671. char buffer[256];
  672. struct discover_resp *discover = (void *) buffer;
  673. int seps, i;
  674. ssize_t ret;
  675. memset(&req, 0, sizeof(req));
  676. init_request(&req, AVDTP_DISCOVER);
  677. ret = avdtp_send(sk, &req, sizeof(req));
  678. if (ret < 0)
  679. return ret;
  680. memset(&buffer, 0, sizeof(buffer));
  681. ret = avdtp_receive(sk, discover, sizeof(buffer));
  682. if (ret < 0)
  683. return ret;
  684. seps = (ret - sizeof(struct avdtp_header)) / sizeof(struct seid_info);
  685. for (i = 0; i < seps; i++) {
  686. const char *type, *media;
  687. switch (discover->seps[i].type) {
  688. case AVDTP_SEP_TYPE_SOURCE:
  689. type = "Source";
  690. break;
  691. case AVDTP_SEP_TYPE_SINK:
  692. type = "Sink";
  693. break;
  694. default:
  695. type = "Invalid";
  696. }
  697. switch (discover->seps[i].media_type) {
  698. case AVDTP_MEDIA_TYPE_AUDIO:
  699. media = "Audio";
  700. break;
  701. case AVDTP_MEDIA_TYPE_VIDEO:
  702. media = "Video";
  703. break;
  704. case AVDTP_MEDIA_TYPE_MULTIMEDIA:
  705. media = "Multimedia";
  706. break;
  707. default:
  708. media = "Invalid";
  709. }
  710. printf("Stream End-Point #%d: %s %s %s\n",
  711. discover->seps[i].seid, media, type,
  712. discover->seps[i].inuse ? "*" : "");
  713. avdtp_get_caps(sk, discover->seps[i].seid);
  714. }
  715. return 0;
  716. }
  717. static int l2cap_connect(bdaddr_t *src, bdaddr_t *dst)
  718. {
  719. struct sockaddr_l2 l2a;
  720. int sk;
  721. memset(&l2a, 0, sizeof(l2a));
  722. l2a.l2_family = AF_BLUETOOTH;
  723. bacpy(&l2a.l2_bdaddr, src);
  724. sk = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
  725. if (sk < 0) {
  726. printf("Cannot create L2CAP socket. %s(%d)\n", strerror(errno),
  727. errno);
  728. return -errno;
  729. }
  730. if (bind(sk, (struct sockaddr *) &l2a, sizeof(l2a)) < 0) {
  731. printf("Bind failed. %s (%d)\n", strerror(errno), errno);
  732. close(sk);
  733. return -errno;
  734. }
  735. memset(&l2a, 0, sizeof(l2a));
  736. l2a.l2_family = AF_BLUETOOTH;
  737. bacpy(&l2a.l2_bdaddr, dst);
  738. l2a.l2_psm = htobs(AVDTP_PSM);
  739. if (connect(sk, (struct sockaddr *) &l2a, sizeof(l2a)) < 0) {
  740. printf("Connect failed. %s(%d)\n", strerror(errno), errno);
  741. close(sk);
  742. return -errno;
  743. }
  744. return sk;
  745. }
  746. static void usage(void)
  747. {
  748. printf("avinfo - Audio/Video Info Tool ver %s\n", VERSION);
  749. printf("Usage:\n"
  750. "\tavinfo [options] <remote address>\n");
  751. printf("Options:\n"
  752. "\t-h\t\tDisplay help\n"
  753. "\t-i\t\tSpecify source interface\n");
  754. }
  755. static struct option main_options[] = {
  756. { "help", 0, 0, 'h' },
  757. { "device", 1, 0, 'i' },
  758. { 0, 0, 0, 0 }
  759. };
  760. int main(int argc, char *argv[])
  761. {
  762. bdaddr_t src, dst;
  763. int opt, sk, dev_id;
  764. if (argc < 2) {
  765. usage();
  766. exit(0);
  767. }
  768. bacpy(&src, BDADDR_ANY);
  769. dev_id = hci_get_route(&src);
  770. if ((dev_id < 0) || (hci_devba(dev_id, &src) < 0)) {
  771. printf("Cannot find any local adapter\n");
  772. exit(-1);
  773. }
  774. while ((opt = getopt_long(argc, argv, "+i:h", main_options, NULL)) != -1) {
  775. switch (opt) {
  776. case 'i':
  777. if (!strncmp(optarg, "hci", 3))
  778. hci_devba(atoi(optarg + 3), &src);
  779. else
  780. str2ba(optarg, &src);
  781. break;
  782. case 'h':
  783. default:
  784. usage();
  785. exit(0);
  786. }
  787. }
  788. printf("Connecting ... \n");
  789. if (bachk(argv[optind]) < 0) {
  790. printf("Invalid argument\n");
  791. exit(1);
  792. }
  793. str2ba(argv[optind], &dst);
  794. sk = l2cap_connect(&src, &dst);
  795. if (sk < 0)
  796. exit(1);
  797. if (avdtp_discover(sk) < 0)
  798. exit(1);
  799. return 0;
  800. }