a2dp.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761
  1. // SPDX-License-Identifier: LGPL-2.1-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2013-2014 Intel Corporation. All rights reserved.
  7. *
  8. *
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. #include <config.h>
  12. #endif
  13. #include <stdint.h>
  14. #include <stdbool.h>
  15. #include <stdlib.h>
  16. #include <errno.h>
  17. #include <unistd.h>
  18. #include <fcntl.h>
  19. #include <glib.h>
  20. #include "btio/btio.h"
  21. #include "lib/bluetooth.h"
  22. #include "lib/sdp.h"
  23. #include "lib/sdp_lib.h"
  24. #include "profiles/audio/a2dp-codecs.h"
  25. #include "src/shared/queue.h"
  26. #include "src/log.h"
  27. #include "hal-msg.h"
  28. #include "ipc-common.h"
  29. #include "ipc.h"
  30. #include "a2dp.h"
  31. #include "utils.h"
  32. #include "bluetooth.h"
  33. #include "avdtp.h"
  34. #include "avrcp.h"
  35. #include "audio-msg.h"
  36. #define SVC_HINT_CAPTURING 0x08
  37. #define IDLE_TIMEOUT 1
  38. #define AUDIO_RETRY_TIMEOUT 2
  39. static GIOChannel *server = NULL;
  40. static GSList *devices = NULL;
  41. static GSList *endpoints = NULL;
  42. static GSList *setups = NULL;
  43. static bdaddr_t adapter_addr;
  44. static uint32_t record_id = 0;
  45. static guint audio_retry_id = 0;
  46. static bool audio_retrying = false;
  47. static struct ipc *hal_ipc = NULL;
  48. static struct ipc *audio_ipc = NULL;
  49. static struct queue *lseps = NULL;
  50. struct a2dp_preset {
  51. void *data;
  52. int8_t len;
  53. };
  54. struct a2dp_endpoint {
  55. uint8_t id;
  56. uint8_t codec;
  57. struct avdtp_local_sep *sep;
  58. struct a2dp_preset *caps;
  59. GSList *presets;
  60. };
  61. struct a2dp_device {
  62. bdaddr_t dst;
  63. uint8_t state;
  64. GIOChannel *io;
  65. struct avdtp *session;
  66. guint idle_id;
  67. };
  68. struct a2dp_setup {
  69. struct a2dp_device *dev;
  70. struct a2dp_endpoint *endpoint;
  71. struct a2dp_preset *preset;
  72. struct avdtp_stream *stream;
  73. uint8_t state;
  74. };
  75. static int device_cmp(gconstpointer s, gconstpointer user_data)
  76. {
  77. const struct a2dp_device *dev = s;
  78. const bdaddr_t *dst = user_data;
  79. return bacmp(&dev->dst, dst);
  80. }
  81. static void preset_free(void *data)
  82. {
  83. struct a2dp_preset *preset = data;
  84. g_free(preset->data);
  85. g_free(preset);
  86. }
  87. static void unregister_endpoint(void *data)
  88. {
  89. struct a2dp_endpoint *endpoint = data;
  90. if (endpoint->sep)
  91. avdtp_unregister_sep(lseps, endpoint->sep);
  92. if (endpoint->caps)
  93. preset_free(endpoint->caps);
  94. g_slist_free_full(endpoint->presets, preset_free);
  95. g_free(endpoint);
  96. }
  97. static void setup_free(void *data)
  98. {
  99. struct a2dp_setup *setup = data;
  100. if (!g_slist_find(setup->endpoint->presets, setup->preset))
  101. preset_free(setup->preset);
  102. g_free(setup);
  103. }
  104. static void setup_remove(struct a2dp_setup *setup)
  105. {
  106. setups = g_slist_remove(setups, setup);
  107. setup_free(setup);
  108. }
  109. static void setup_remove_all_by_dev(struct a2dp_device *dev)
  110. {
  111. GSList *l = setups;
  112. while (l) {
  113. struct a2dp_setup *setup = l->data;
  114. GSList *next = g_slist_next(l);
  115. if (setup->dev == dev)
  116. setup_remove(setup);
  117. l = next;
  118. }
  119. }
  120. static void a2dp_device_free(void *data)
  121. {
  122. struct a2dp_device *dev = data;
  123. if (dev->idle_id > 0)
  124. g_source_remove(dev->idle_id);
  125. if (dev->session)
  126. avdtp_unref(dev->session);
  127. if (dev->io) {
  128. g_io_channel_shutdown(dev->io, FALSE, NULL);
  129. g_io_channel_unref(dev->io);
  130. }
  131. setup_remove_all_by_dev(dev);
  132. g_free(dev);
  133. }
  134. static void a2dp_device_remove(struct a2dp_device *dev)
  135. {
  136. devices = g_slist_remove(devices, dev);
  137. a2dp_device_free(dev);
  138. }
  139. static struct a2dp_device *a2dp_device_new(const bdaddr_t *dst)
  140. {
  141. struct a2dp_device *dev;
  142. dev = g_new0(struct a2dp_device, 1);
  143. bacpy(&dev->dst, dst);
  144. devices = g_slist_prepend(devices, dev);
  145. return dev;
  146. }
  147. static bool a2dp_device_connect(struct a2dp_device *dev, BtIOConnect cb)
  148. {
  149. GError *err = NULL;
  150. dev->io = bt_io_connect(cb, dev, NULL, &err,
  151. BT_IO_OPT_SOURCE_BDADDR, &adapter_addr,
  152. BT_IO_OPT_DEST_BDADDR, &dev->dst,
  153. BT_IO_OPT_PSM, AVDTP_PSM,
  154. BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM,
  155. BT_IO_OPT_INVALID);
  156. if (err) {
  157. error("%s", err->message);
  158. g_error_free(err);
  159. return false;
  160. }
  161. return true;
  162. }
  163. static void bt_a2dp_notify_state(struct a2dp_device *dev, uint8_t state)
  164. {
  165. struct hal_ev_a2dp_conn_state ev;
  166. char address[18];
  167. if (dev->state == state)
  168. return;
  169. dev->state = state;
  170. ba2str(&dev->dst, address);
  171. DBG("device %s state %u", address, state);
  172. bdaddr2android(&dev->dst, ev.bdaddr);
  173. ev.state = state;
  174. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_A2DP, HAL_EV_A2DP_CONN_STATE,
  175. sizeof(ev), &ev);
  176. if (state != HAL_A2DP_STATE_DISCONNECTED)
  177. return;
  178. bt_avrcp_disconnect(&dev->dst);
  179. a2dp_device_remove(dev);
  180. }
  181. static void bt_audio_notify_state(struct a2dp_setup *setup, uint8_t state)
  182. {
  183. struct hal_ev_a2dp_audio_state ev;
  184. char address[18];
  185. if (setup->state == state)
  186. return;
  187. setup->state = state;
  188. ba2str(&setup->dev->dst, address);
  189. DBG("device %s state %u", address, state);
  190. bdaddr2android(&setup->dev->dst, ev.bdaddr);
  191. ev.state = state;
  192. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_A2DP, HAL_EV_A2DP_AUDIO_STATE,
  193. sizeof(ev), &ev);
  194. }
  195. static void disconnect_cb(void *user_data)
  196. {
  197. struct a2dp_device *dev = user_data;
  198. bt_a2dp_notify_state(dev, HAL_A2DP_STATE_DISCONNECTED);
  199. }
  200. static int sbc_check_config(void *caps, uint8_t caps_len, void *conf,
  201. uint8_t conf_len)
  202. {
  203. a2dp_sbc_t *cap, *config;
  204. if (conf_len != caps_len || conf_len != sizeof(a2dp_sbc_t)) {
  205. error("SBC: Invalid configuration size (%u)", conf_len);
  206. return -EINVAL;
  207. }
  208. cap = caps;
  209. config = conf;
  210. if (!(cap->frequency & config->frequency)) {
  211. error("SBC: Unsupported frequency (%u) by endpoint",
  212. config->frequency);
  213. return -EINVAL;
  214. }
  215. if (!(cap->channel_mode & config->channel_mode)) {
  216. error("SBC: Unsupported channel mode (%u) by endpoint",
  217. config->channel_mode);
  218. return -EINVAL;
  219. }
  220. if (!(cap->block_length & config->block_length)) {
  221. error("SBC: Unsupported block length (%u) by endpoint",
  222. config->block_length);
  223. return -EINVAL;
  224. }
  225. if (!(cap->allocation_method & config->allocation_method)) {
  226. error("SBC: Unsupported allocation method (%u) by endpoint",
  227. config->block_length);
  228. return -EINVAL;
  229. }
  230. if (config->max_bitpool < cap->min_bitpool) {
  231. error("SBC: Invalid maximun bitpool (%u < %u)",
  232. config->max_bitpool, cap->min_bitpool);
  233. return -EINVAL;
  234. }
  235. if (config->min_bitpool > cap->max_bitpool) {
  236. error("SBC: Invalid minimun bitpool (%u > %u)",
  237. config->min_bitpool, cap->min_bitpool);
  238. return -EINVAL;
  239. }
  240. if (config->max_bitpool > cap->max_bitpool)
  241. return -ERANGE;
  242. if (config->min_bitpool < cap->min_bitpool)
  243. return -ERANGE;
  244. return 0;
  245. }
  246. static int aac_check_config(void *caps, uint8_t caps_len, void *conf,
  247. uint8_t conf_len)
  248. {
  249. a2dp_aac_t *cap, *config;
  250. if (conf_len != caps_len || conf_len != sizeof(a2dp_aac_t)) {
  251. error("AAC: Invalid configuration size (%u)", conf_len);
  252. return -EINVAL;
  253. }
  254. cap = caps;
  255. config = conf;
  256. if (!(cap->object_type & config->object_type)) {
  257. error("AAC: Unsupported object type (%u) by endpoint",
  258. config->object_type);
  259. return -EINVAL;
  260. }
  261. if (!(AAC_GET_FREQUENCY(*cap) & AAC_GET_FREQUENCY(*config))) {
  262. error("AAC: Unsupported frequency (%u) by endpoint",
  263. AAC_GET_FREQUENCY(*config));
  264. return -EINVAL;
  265. }
  266. if (!(cap->channels & config->channels)) {
  267. error("AAC: Unsupported channels (%u) by endpoint",
  268. config->channels);
  269. return -EINVAL;
  270. }
  271. /* VBR support in SNK is mandatory but let's make sure we don't try to
  272. * have VBR on remote which for some reason does not support it
  273. */
  274. if (!cap->vbr && config->vbr) {
  275. error("AAC: Unsupported VBR (%u) by endpoint",
  276. config->vbr);
  277. return -EINVAL;
  278. }
  279. if (AAC_GET_BITRATE(*cap) < AAC_GET_BITRATE(*config))
  280. return -ERANGE;
  281. return 0;
  282. }
  283. static int aptx_check_config(void *caps, uint8_t caps_len, void *conf,
  284. uint8_t conf_len)
  285. {
  286. a2dp_aptx_t *cap, *config;
  287. if (conf_len != caps_len || conf_len != sizeof(a2dp_aptx_t)) {
  288. error("APTX: Invalid configuration size (%u)", conf_len);
  289. return -EINVAL;
  290. }
  291. cap = caps;
  292. config = conf;
  293. if (!(cap->frequency & config->frequency)) {
  294. error("APTX: Unsupported frequenct (%u) by endpoint",
  295. config->frequency);
  296. return -EINVAL;
  297. }
  298. if (!(cap->channel_mode & config->channel_mode)) {
  299. error("APTX: Unsupported channel mode (%u) by endpoint",
  300. config->channel_mode);
  301. return -EINVAL;
  302. }
  303. return 0;
  304. }
  305. static int check_capabilities(struct a2dp_preset *preset,
  306. struct avdtp_media_codec_capability *codec,
  307. uint8_t codec_len)
  308. {
  309. a2dp_vendor_codec_t *vndcodec;
  310. /* Codec specific */
  311. switch (codec->media_codec_type) {
  312. case A2DP_CODEC_SBC:
  313. return sbc_check_config(codec->data, codec_len, preset->data,
  314. preset->len);
  315. case A2DP_CODEC_MPEG24:
  316. return aac_check_config(codec->data, codec_len, preset->data,
  317. preset->len);
  318. case A2DP_CODEC_VENDOR:
  319. vndcodec = (void *) codec->data;
  320. if (A2DP_GET_VENDOR_ID(*vndcodec) == APTX_VENDOR_ID &&
  321. A2DP_GET_CODEC_ID(*vndcodec) == APTX_CODEC_ID)
  322. return aptx_check_config(codec->data, codec_len,
  323. preset->data, preset->len);
  324. return -EINVAL;
  325. default:
  326. return -EINVAL;
  327. }
  328. }
  329. static struct a2dp_preset *sbc_select_range(void *caps, uint8_t caps_len,
  330. void *conf, uint8_t conf_len)
  331. {
  332. struct a2dp_preset *p;
  333. a2dp_sbc_t *cap, *config;
  334. cap = caps;
  335. config = conf;
  336. config->min_bitpool = MAX(config->min_bitpool, cap->min_bitpool);
  337. config->max_bitpool = MIN(config->max_bitpool, cap->max_bitpool);
  338. p = g_new0(struct a2dp_preset, 1);
  339. p->len = conf_len;
  340. p->data = g_memdup(conf, p->len);
  341. return p;
  342. }
  343. static struct a2dp_preset *aac_select_range(void *caps, uint8_t caps_len,
  344. void *conf, uint8_t conf_len)
  345. {
  346. struct a2dp_preset *p;
  347. a2dp_aac_t *cap, *config;
  348. uint32_t bitrate;
  349. cap = caps;
  350. config = conf;
  351. bitrate = MIN(AAC_GET_BITRATE(*cap), AAC_GET_BITRATE(*config));
  352. AAC_SET_BITRATE(*config, bitrate);
  353. p = g_new0(struct a2dp_preset, 1);
  354. p->len = conf_len;
  355. p->data = g_memdup(conf, p->len);
  356. return p;
  357. }
  358. static struct a2dp_preset *select_preset_range(struct a2dp_preset *preset,
  359. struct avdtp_media_codec_capability *codec,
  360. uint8_t codec_len)
  361. {
  362. /* Codec specific */
  363. switch (codec->media_codec_type) {
  364. case A2DP_CODEC_SBC:
  365. return sbc_select_range(codec->data, codec_len, preset->data,
  366. preset->len);
  367. case A2DP_CODEC_MPEG24:
  368. return aac_select_range(codec->data, codec_len, preset->data,
  369. preset->len);
  370. default:
  371. return NULL;
  372. }
  373. }
  374. static struct a2dp_preset *select_preset(struct a2dp_endpoint *endpoint,
  375. struct avdtp_remote_sep *rsep)
  376. {
  377. struct avdtp_service_capability *service;
  378. struct avdtp_media_codec_capability *codec;
  379. GSList *l;
  380. uint8_t codec_len;
  381. service = avdtp_get_codec(rsep);
  382. codec = (struct avdtp_media_codec_capability *) service->data;
  383. codec_len = service->length - sizeof(*codec);
  384. for (l = endpoint->presets; l; l = g_slist_next(l)) {
  385. struct a2dp_preset *preset = l->data;
  386. int err;
  387. err = check_capabilities(preset, codec, codec_len);
  388. if (err == 0)
  389. return preset;
  390. if (err == -ERANGE)
  391. return select_preset_range(preset, codec, codec_len);
  392. }
  393. return NULL;
  394. }
  395. static void setup_add(struct a2dp_device *dev, struct a2dp_endpoint *endpoint,
  396. struct a2dp_preset *preset, struct avdtp_stream *stream)
  397. {
  398. struct a2dp_setup *setup;
  399. setup = g_new0(struct a2dp_setup, 1);
  400. setup->dev = dev;
  401. setup->endpoint = endpoint;
  402. setup->preset = preset;
  403. setup->stream = stream;
  404. setups = g_slist_append(setups, setup);
  405. if (dev->idle_id > 0) {
  406. g_source_remove(dev->idle_id);
  407. dev->idle_id = 0;
  408. }
  409. }
  410. static int select_configuration(struct a2dp_device *dev,
  411. struct a2dp_endpoint *endpoint,
  412. struct avdtp_remote_sep *rsep)
  413. {
  414. struct a2dp_preset *preset;
  415. struct avdtp_stream *stream;
  416. struct avdtp_service_capability *service;
  417. struct avdtp_media_codec_capability *codec;
  418. GSList *caps;
  419. int err;
  420. preset = select_preset(endpoint, rsep);
  421. if (!preset) {
  422. error("Unable to select codec preset");
  423. return -EINVAL;
  424. }
  425. service = avdtp_service_cap_new(AVDTP_MEDIA_TRANSPORT, NULL, 0);
  426. caps = g_slist_append(NULL, service);
  427. codec = g_malloc0(sizeof(*codec) + preset->len);
  428. codec->media_type = AVDTP_MEDIA_TYPE_AUDIO;
  429. codec->media_codec_type = endpoint->codec;
  430. memcpy(codec->data, preset->data, preset->len);
  431. service = avdtp_service_cap_new(AVDTP_MEDIA_CODEC, codec,
  432. sizeof(*codec) + preset->len);
  433. caps = g_slist_append(caps, service);
  434. g_free(codec);
  435. err = avdtp_set_configuration(dev->session, rsep, endpoint->sep, caps,
  436. &stream);
  437. g_slist_free_full(caps, g_free);
  438. if (err < 0) {
  439. error("avdtp_set_configuration: %s", strerror(-err));
  440. return err;
  441. }
  442. setup_add(dev, endpoint, preset, stream);
  443. return 0;
  444. }
  445. static void discover_cb(struct avdtp *session, GSList *seps,
  446. struct avdtp_error *err, void *user_data)
  447. {
  448. struct a2dp_device *dev = user_data;
  449. struct a2dp_endpoint *endpoint = NULL;
  450. struct avdtp_remote_sep *rsep = NULL;
  451. GSList *l;
  452. for (l = endpoints; l; l = g_slist_next(l)) {
  453. endpoint = l->data;
  454. rsep = avdtp_find_remote_sep(session, endpoint->sep);
  455. if (rsep)
  456. break;
  457. }
  458. if (!rsep) {
  459. error("Unable to find matching endpoint");
  460. goto failed;
  461. }
  462. if (select_configuration(dev, endpoint, rsep) < 0)
  463. goto failed;
  464. return;
  465. failed:
  466. avdtp_shutdown(session);
  467. }
  468. static gboolean idle_timeout(gpointer user_data)
  469. {
  470. struct a2dp_device *dev = user_data;
  471. int err;
  472. dev->idle_id = 0;
  473. err = avdtp_discover(dev->session, discover_cb, dev);
  474. if (err == 0)
  475. return FALSE;
  476. error("avdtp_discover: %s", strerror(-err));
  477. bt_a2dp_notify_state(dev, HAL_A2DP_STATE_DISCONNECTED);
  478. return FALSE;
  479. }
  480. static void signaling_connect_cb(GIOChannel *chan, GError *err,
  481. gpointer user_data)
  482. {
  483. struct a2dp_device *dev = user_data;
  484. struct avdtp *session;
  485. uint16_t imtu, omtu;
  486. GError *gerr = NULL;
  487. int fd;
  488. if (err) {
  489. bt_a2dp_notify_state(dev, HAL_A2DP_STATE_DISCONNECTED);
  490. error("%s", err->message);
  491. return;
  492. }
  493. bt_io_get(chan, &gerr,
  494. BT_IO_OPT_IMTU, &imtu,
  495. BT_IO_OPT_OMTU, &omtu,
  496. BT_IO_OPT_INVALID);
  497. if (gerr) {
  498. error("%s", gerr->message);
  499. g_error_free(gerr);
  500. goto failed;
  501. }
  502. fd = g_io_channel_unix_get_fd(chan);
  503. /* FIXME: Add proper version */
  504. session = avdtp_new(fd, imtu, omtu, 0x0100, lseps);
  505. if (!session)
  506. goto failed;
  507. dev->session = session;
  508. avdtp_add_disconnect_cb(dev->session, disconnect_cb, dev);
  509. /* Proceed to stream setup if initiator */
  510. if (dev->io) {
  511. int perr;
  512. g_io_channel_unref(dev->io);
  513. dev->io = NULL;
  514. perr = avdtp_discover(dev->session, discover_cb, dev);
  515. if (perr < 0) {
  516. error("avdtp_discover: %s", strerror(-perr));
  517. goto failed;
  518. }
  519. bt_avrcp_connect(&dev->dst);
  520. } else /* Init idle timeout to discover */
  521. dev->idle_id = g_timeout_add_seconds(IDLE_TIMEOUT, idle_timeout,
  522. dev);
  523. return;
  524. failed:
  525. bt_a2dp_notify_state(dev, HAL_A2DP_STATE_DISCONNECTED);
  526. }
  527. static void bt_a2dp_connect(const void *buf, uint16_t len)
  528. {
  529. const struct hal_cmd_a2dp_connect *cmd = buf;
  530. struct a2dp_device *dev;
  531. uint8_t status;
  532. char addr[18];
  533. bdaddr_t dst;
  534. GSList *l;
  535. DBG("");
  536. android2bdaddr(&cmd->bdaddr, &dst);
  537. l = g_slist_find_custom(devices, &dst, device_cmp);
  538. if (l) {
  539. status = HAL_STATUS_FAILED;
  540. goto failed;
  541. }
  542. dev = a2dp_device_new(&dst);
  543. if (!a2dp_device_connect(dev, signaling_connect_cb)) {
  544. a2dp_device_remove(dev);
  545. status = HAL_STATUS_FAILED;
  546. goto failed;
  547. }
  548. ba2str(&dev->dst, addr);
  549. DBG("connecting to %s", addr);
  550. bt_a2dp_notify_state(dev, HAL_A2DP_STATE_CONNECTING);
  551. status = HAL_STATUS_SUCCESS;
  552. failed:
  553. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_A2DP, HAL_OP_A2DP_CONNECT, status);
  554. }
  555. static void bt_a2dp_disconnect(const void *buf, uint16_t len)
  556. {
  557. const struct hal_cmd_a2dp_connect *cmd = buf;
  558. uint8_t status;
  559. struct a2dp_device *dev;
  560. GSList *l;
  561. bdaddr_t dst;
  562. DBG("");
  563. android2bdaddr(&cmd->bdaddr, &dst);
  564. l = g_slist_find_custom(devices, &dst, device_cmp);
  565. if (!l) {
  566. status = HAL_STATUS_FAILED;
  567. goto failed;
  568. }
  569. dev = l->data;
  570. status = HAL_STATUS_SUCCESS;
  571. if (dev->io) {
  572. bt_a2dp_notify_state(dev, HAL_A2DP_STATE_DISCONNECTED);
  573. goto failed;
  574. }
  575. /* Wait AVDTP session to shutdown */
  576. avdtp_shutdown(dev->session);
  577. bt_a2dp_notify_state(dev, HAL_A2DP_STATE_DISCONNECTING);
  578. failed:
  579. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_A2DP, HAL_OP_A2DP_DISCONNECT,
  580. status);
  581. }
  582. static const struct ipc_handler cmd_handlers[] = {
  583. /* HAL_OP_A2DP_CONNECT */
  584. { bt_a2dp_connect, false, sizeof(struct hal_cmd_a2dp_connect) },
  585. /* HAL_OP_A2DP_DISCONNECT */
  586. { bt_a2dp_disconnect, false, sizeof(struct hal_cmd_a2dp_disconnect) },
  587. };
  588. static struct a2dp_setup *find_setup_by_device(struct a2dp_device *dev)
  589. {
  590. GSList *l;
  591. for (l = setups; l; l = g_slist_next(l)) {
  592. struct a2dp_setup *setup = l->data;
  593. if (setup->dev == dev)
  594. return setup;
  595. }
  596. return NULL;
  597. }
  598. static void transport_connect_cb(GIOChannel *chan, GError *err,
  599. gpointer user_data)
  600. {
  601. struct a2dp_device *dev = user_data;
  602. struct a2dp_setup *setup;
  603. uint16_t imtu, omtu;
  604. GError *gerr = NULL;
  605. int fd;
  606. if (err) {
  607. error("%s", err->message);
  608. return;
  609. }
  610. setup = find_setup_by_device(dev);
  611. if (!setup) {
  612. error("Unable to find stream setup");
  613. return;
  614. }
  615. bt_io_get(chan, &gerr,
  616. BT_IO_OPT_IMTU, &imtu,
  617. BT_IO_OPT_OMTU, &omtu,
  618. BT_IO_OPT_INVALID);
  619. if (gerr) {
  620. error("%s", gerr->message);
  621. g_error_free(gerr);
  622. return;
  623. }
  624. fd = g_io_channel_unix_get_fd(chan);
  625. if (!avdtp_stream_set_transport(setup->stream, fd, imtu, omtu)) {
  626. error("avdtp_stream_set_transport: failed");
  627. return;
  628. }
  629. g_io_channel_set_close_on_unref(chan, FALSE);
  630. if (dev->io) {
  631. g_io_channel_unref(dev->io);
  632. dev->io = NULL;
  633. }
  634. bt_a2dp_notify_state(dev, HAL_A2DP_STATE_CONNECTED);
  635. }
  636. static void connect_cb(GIOChannel *chan, GError *err, gpointer user_data)
  637. {
  638. struct a2dp_device *dev;
  639. bdaddr_t dst;
  640. char address[18];
  641. GError *gerr = NULL;
  642. GSList *l;
  643. if (err) {
  644. error("%s", err->message);
  645. return;
  646. }
  647. bt_io_get(chan, &gerr,
  648. BT_IO_OPT_DEST_BDADDR, &dst,
  649. BT_IO_OPT_INVALID);
  650. if (gerr) {
  651. error("%s", gerr->message);
  652. g_error_free(gerr);
  653. g_io_channel_shutdown(chan, TRUE, NULL);
  654. return;
  655. }
  656. ba2str(&dst, address);
  657. DBG("Incoming connection from %s", address);
  658. l = g_slist_find_custom(devices, &dst, device_cmp);
  659. if (l) {
  660. transport_connect_cb(chan, err, l->data);
  661. return;
  662. }
  663. dev = a2dp_device_new(&dst);
  664. bt_a2dp_notify_state(dev, HAL_A2DP_STATE_CONNECTING);
  665. signaling_connect_cb(chan, err, dev);
  666. }
  667. static sdp_record_t *a2dp_record(void)
  668. {
  669. sdp_list_t *svclass_id, *pfseq, *apseq, *root;
  670. uuid_t root_uuid, l2cap_uuid, avdtp_uuid, a2dp_uuid;
  671. sdp_profile_desc_t profile[1];
  672. sdp_list_t *aproto, *proto[2];
  673. sdp_record_t *record;
  674. sdp_data_t *psm, *version, *features;
  675. uint16_t lp = AVDTP_UUID;
  676. uint16_t a2dp_ver = 0x0103, avdtp_ver = 0x0103, feat = 0x000f;
  677. record = sdp_record_alloc();
  678. if (!record)
  679. return NULL;
  680. sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
  681. root = sdp_list_append(NULL, &root_uuid);
  682. sdp_set_browse_groups(record, root);
  683. sdp_uuid16_create(&a2dp_uuid, AUDIO_SOURCE_SVCLASS_ID);
  684. svclass_id = sdp_list_append(NULL, &a2dp_uuid);
  685. sdp_set_service_classes(record, svclass_id);
  686. sdp_uuid16_create(&profile[0].uuid, ADVANCED_AUDIO_PROFILE_ID);
  687. profile[0].version = a2dp_ver;
  688. pfseq = sdp_list_append(NULL, &profile[0]);
  689. sdp_set_profile_descs(record, pfseq);
  690. sdp_uuid16_create(&l2cap_uuid, L2CAP_UUID);
  691. proto[0] = sdp_list_append(NULL, &l2cap_uuid);
  692. psm = sdp_data_alloc(SDP_UINT16, &lp);
  693. proto[0] = sdp_list_append(proto[0], psm);
  694. apseq = sdp_list_append(NULL, proto[0]);
  695. sdp_uuid16_create(&avdtp_uuid, AVDTP_UUID);
  696. proto[1] = sdp_list_append(NULL, &avdtp_uuid);
  697. version = sdp_data_alloc(SDP_UINT16, &avdtp_ver);
  698. proto[1] = sdp_list_append(proto[1], version);
  699. apseq = sdp_list_append(apseq, proto[1]);
  700. aproto = sdp_list_append(NULL, apseq);
  701. sdp_set_access_protos(record, aproto);
  702. features = sdp_data_alloc(SDP_UINT16, &feat);
  703. sdp_attr_add(record, SDP_ATTR_SUPPORTED_FEATURES, features);
  704. sdp_set_info_attr(record, "Audio Source", NULL, NULL);
  705. sdp_data_free(psm);
  706. sdp_data_free(version);
  707. sdp_list_free(proto[0], NULL);
  708. sdp_list_free(proto[1], NULL);
  709. sdp_list_free(apseq, NULL);
  710. sdp_list_free(pfseq, NULL);
  711. sdp_list_free(aproto, NULL);
  712. sdp_list_free(root, NULL);
  713. sdp_list_free(svclass_id, NULL);
  714. return record;
  715. }
  716. static gboolean sep_getcap_ind(struct avdtp *session,
  717. struct avdtp_local_sep *sep,
  718. GSList **caps, uint8_t *err,
  719. void *user_data)
  720. {
  721. struct a2dp_endpoint *endpoint = user_data;
  722. struct a2dp_preset *cap = endpoint->caps;
  723. struct avdtp_service_capability *service;
  724. struct avdtp_media_codec_capability *codec;
  725. *caps = NULL;
  726. service = avdtp_service_cap_new(AVDTP_MEDIA_TRANSPORT, NULL, 0);
  727. *caps = g_slist_append(*caps, service);
  728. codec = g_malloc0(sizeof(*codec) + cap->len);
  729. codec->media_type = AVDTP_MEDIA_TYPE_AUDIO;
  730. codec->media_codec_type = endpoint->codec;
  731. memcpy(codec->data, cap->data, cap->len);
  732. service = avdtp_service_cap_new(AVDTP_MEDIA_CODEC, codec,
  733. sizeof(*codec) + cap->len);
  734. *caps = g_slist_append(*caps, service);
  735. g_free(codec);
  736. return TRUE;
  737. }
  738. static int check_config(struct a2dp_endpoint *endpoint,
  739. struct a2dp_preset *config)
  740. {
  741. GSList *l;
  742. struct a2dp_preset *caps;
  743. for (l = endpoint->presets; l; l = g_slist_next(l)) {
  744. struct a2dp_preset *preset = l->data;
  745. if (preset->len != config->len)
  746. continue;
  747. if (memcmp(preset->data, config->data, preset->len) == 0)
  748. return 0;
  749. }
  750. caps = endpoint->caps;
  751. /* Codec specific */
  752. switch (endpoint->codec) {
  753. case A2DP_CODEC_SBC:
  754. return sbc_check_config(caps->data, caps->len, config->data,
  755. config->len);
  756. default:
  757. return -EINVAL;
  758. }
  759. }
  760. static struct a2dp_device *find_device_by_session(struct avdtp *session)
  761. {
  762. GSList *l;
  763. for (l = devices; l; l = g_slist_next(l)) {
  764. struct a2dp_device *dev = l->data;
  765. if (dev->session == session)
  766. return dev;
  767. }
  768. return NULL;
  769. }
  770. static struct a2dp_setup *find_setup(uint8_t id)
  771. {
  772. GSList *l;
  773. for (l = setups; l; l = g_slist_next(l)) {
  774. struct a2dp_setup *setup = l->data;
  775. if (setup->endpoint->id == id)
  776. return setup;
  777. }
  778. return NULL;
  779. }
  780. static void setup_remove_by_id(uint8_t id)
  781. {
  782. struct a2dp_setup *setup;
  783. setup = find_setup(id);
  784. if (!setup) {
  785. error("Unable to find stream setup for endpoint %u", id);
  786. return;
  787. }
  788. setup_remove(setup);
  789. }
  790. static gboolean sep_setconf_ind(struct avdtp *session,
  791. struct avdtp_local_sep *sep,
  792. struct avdtp_stream *stream,
  793. GSList *caps,
  794. avdtp_set_configuration_cb cb,
  795. void *user_data)
  796. {
  797. struct a2dp_endpoint *endpoint = user_data;
  798. struct a2dp_device *dev;
  799. struct a2dp_preset *preset = NULL;
  800. DBG("");
  801. dev = find_device_by_session(session);
  802. if (!dev) {
  803. error("Unable to find device for session %p", session);
  804. return FALSE;
  805. }
  806. for (; caps != NULL; caps = g_slist_next(caps)) {
  807. struct avdtp_service_capability *cap = caps->data;
  808. struct avdtp_media_codec_capability *codec;
  809. if (cap->category == AVDTP_DELAY_REPORTING)
  810. return FALSE;
  811. if (cap->category != AVDTP_MEDIA_CODEC)
  812. continue;
  813. codec = (struct avdtp_media_codec_capability *) cap->data;
  814. if (codec->media_codec_type != endpoint->codec)
  815. return FALSE;
  816. preset = g_new0(struct a2dp_preset, 1);
  817. preset->len = cap->length - sizeof(*codec);
  818. preset->data = g_memdup(codec->data, preset->len);
  819. if (check_config(endpoint, preset) < 0) {
  820. preset_free(preset);
  821. return FALSE;
  822. }
  823. }
  824. if (!preset)
  825. return FALSE;
  826. setup_add(dev, endpoint, preset, stream);
  827. cb(session, stream, NULL);
  828. return TRUE;
  829. }
  830. static gboolean sep_open_ind(struct avdtp *session, struct avdtp_local_sep *sep,
  831. struct avdtp_stream *stream, uint8_t *err,
  832. void *user_data)
  833. {
  834. struct a2dp_endpoint *endpoint = user_data;
  835. struct a2dp_setup *setup;
  836. DBG("");
  837. setup = find_setup(endpoint->id);
  838. if (!setup) {
  839. error("Unable to find stream setup for endpoint %u",
  840. endpoint->id);
  841. *err = AVDTP_SEP_NOT_IN_USE;
  842. return FALSE;
  843. }
  844. return TRUE;
  845. }
  846. static gboolean sep_close_ind(struct avdtp *session,
  847. struct avdtp_local_sep *sep,
  848. struct avdtp_stream *stream,
  849. uint8_t *err,
  850. void *user_data)
  851. {
  852. struct a2dp_endpoint *endpoint = user_data;
  853. struct a2dp_setup *setup;
  854. DBG("");
  855. setup = find_setup(endpoint->id);
  856. if (!setup) {
  857. error("Unable to find stream setup for endpoint %u",
  858. endpoint->id);
  859. *err = AVDTP_SEP_NOT_IN_USE;
  860. return FALSE;
  861. }
  862. bt_audio_notify_state(setup, HAL_AUDIO_STOPPED);
  863. setup_remove(setup);
  864. return TRUE;
  865. }
  866. static gboolean sep_start_ind(struct avdtp *session,
  867. struct avdtp_local_sep *sep,
  868. struct avdtp_stream *stream,
  869. uint8_t *err,
  870. void *user_data)
  871. {
  872. struct a2dp_endpoint *endpoint = user_data;
  873. struct a2dp_setup *setup;
  874. DBG("");
  875. setup = find_setup(endpoint->id);
  876. if (!setup) {
  877. error("Unable to find stream setup for endpoint %u",
  878. endpoint->id);
  879. *err = AVDTP_SEP_NOT_IN_USE;
  880. return FALSE;
  881. }
  882. bt_audio_notify_state(setup, HAL_AUDIO_STARTED);
  883. return TRUE;
  884. }
  885. static gboolean sep_suspend_ind(struct avdtp *session,
  886. struct avdtp_local_sep *sep,
  887. struct avdtp_stream *stream,
  888. uint8_t *err,
  889. void *user_data)
  890. {
  891. struct a2dp_endpoint *endpoint = user_data;
  892. struct a2dp_setup *setup;
  893. DBG("");
  894. setup = find_setup(endpoint->id);
  895. if (!setup) {
  896. error("Unable to find stream setup for endpoint %u",
  897. endpoint->id);
  898. *err = AVDTP_SEP_NOT_IN_USE;
  899. return FALSE;
  900. }
  901. bt_audio_notify_state(setup, HAL_AUDIO_SUSPEND);
  902. return TRUE;
  903. }
  904. static struct avdtp_sep_ind sep_ind = {
  905. .get_capability = sep_getcap_ind,
  906. .set_configuration = sep_setconf_ind,
  907. .open = sep_open_ind,
  908. .close = sep_close_ind,
  909. .start = sep_start_ind,
  910. .suspend = sep_suspend_ind,
  911. };
  912. static void sep_setconf_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
  913. struct avdtp_stream *stream,
  914. struct avdtp_error *err, void *user_data)
  915. {
  916. struct a2dp_endpoint *endpoint = user_data;
  917. struct a2dp_setup *setup;
  918. int ret;
  919. DBG("");
  920. setup = find_setup(endpoint->id);
  921. if (!setup) {
  922. error("Unable to find stream setup for endpoint %u",
  923. endpoint->id);
  924. return;
  925. }
  926. if (err)
  927. goto failed;
  928. ret = avdtp_open(session, stream);
  929. if (ret < 0) {
  930. error("avdtp_open: %s", strerror(-ret));
  931. goto failed;
  932. }
  933. return;
  934. failed:
  935. setup_remove(setup);
  936. }
  937. static void sep_open_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
  938. struct avdtp_stream *stream, struct avdtp_error *err,
  939. void *user_data)
  940. {
  941. struct a2dp_endpoint *endpoint = user_data;
  942. struct a2dp_device *dev;
  943. DBG("");
  944. if (err)
  945. goto failed;
  946. dev = find_device_by_session(session);
  947. if (!dev) {
  948. error("Unable to find device for session");
  949. goto failed;
  950. }
  951. a2dp_device_connect(dev, transport_connect_cb);
  952. return;
  953. failed:
  954. setup_remove_by_id(endpoint->id);
  955. }
  956. static void sep_start_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
  957. struct avdtp_stream *stream, struct avdtp_error *err,
  958. void *user_data)
  959. {
  960. struct a2dp_endpoint *endpoint = user_data;
  961. struct a2dp_setup *setup;
  962. DBG("");
  963. if (err) {
  964. setup_remove_by_id(endpoint->id);
  965. return;
  966. }
  967. setup = find_setup(endpoint->id);
  968. if (!setup) {
  969. error("Unable to find stream setup for %u endpoint",
  970. endpoint->id);
  971. return;
  972. }
  973. bt_audio_notify_state(setup, HAL_AUDIO_STARTED);
  974. }
  975. static void sep_suspend_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
  976. struct avdtp_stream *stream, struct avdtp_error *err,
  977. void *user_data)
  978. {
  979. struct a2dp_endpoint *endpoint = user_data;
  980. struct a2dp_setup *setup;
  981. DBG("");
  982. if (err) {
  983. setup_remove_by_id(endpoint->id);
  984. return;
  985. }
  986. setup = find_setup(endpoint->id);
  987. if (!setup) {
  988. error("Unable to find stream setup for %u endpoint",
  989. endpoint->id);
  990. return;
  991. }
  992. bt_audio_notify_state(setup, HAL_AUDIO_STOPPED);
  993. }
  994. static void sep_close_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
  995. struct avdtp_stream *stream, struct avdtp_error *err,
  996. void *user_data)
  997. {
  998. struct a2dp_endpoint *endpoint = user_data;
  999. struct a2dp_setup *setup;
  1000. DBG("");
  1001. if (err)
  1002. return;
  1003. setup = find_setup(endpoint->id);
  1004. if (!setup) {
  1005. error("Unable to find stream setup for %u endpoint",
  1006. endpoint->id);
  1007. return;
  1008. }
  1009. bt_audio_notify_state(setup, HAL_AUDIO_STOPPED);
  1010. setup_remove(setup);
  1011. }
  1012. static void sep_abort_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
  1013. struct avdtp_stream *stream, struct avdtp_error *err,
  1014. void *user_data)
  1015. {
  1016. struct a2dp_endpoint *endpoint = user_data;
  1017. DBG("");
  1018. if (err)
  1019. return;
  1020. setup_remove_by_id(endpoint->id);
  1021. }
  1022. static struct avdtp_sep_cfm sep_cfm = {
  1023. .set_configuration = sep_setconf_cfm,
  1024. .open = sep_open_cfm,
  1025. .start = sep_start_cfm,
  1026. .suspend = sep_suspend_cfm,
  1027. .close = sep_close_cfm,
  1028. .abort = sep_abort_cfm,
  1029. };
  1030. static uint8_t register_endpoint(const uint8_t *uuid, uint8_t codec,
  1031. GSList *presets)
  1032. {
  1033. struct a2dp_endpoint *endpoint;
  1034. /* FIXME: Add proper check for uuid */
  1035. endpoint = g_new0(struct a2dp_endpoint, 1);
  1036. endpoint->id = g_slist_length(endpoints) + 1;
  1037. endpoint->codec = codec;
  1038. endpoint->sep = avdtp_register_sep(lseps, AVDTP_SEP_TYPE_SOURCE,
  1039. AVDTP_MEDIA_TYPE_AUDIO,
  1040. codec, FALSE, &sep_ind,
  1041. &sep_cfm, endpoint);
  1042. endpoint->caps = presets->data;
  1043. endpoint->presets = g_slist_copy(g_slist_nth(presets, 1));
  1044. if (endpoint->codec == A2DP_CODEC_VENDOR) {
  1045. a2dp_vendor_codec_t *vndcodec = (void *) endpoint->caps->data;
  1046. avdtp_sep_set_vendor_codec(endpoint->sep,
  1047. A2DP_GET_VENDOR_ID(*vndcodec),
  1048. A2DP_GET_CODEC_ID(*vndcodec));
  1049. }
  1050. endpoints = g_slist_append(endpoints, endpoint);
  1051. return endpoint->id;
  1052. }
  1053. static GSList *parse_presets(const struct audio_preset *p, uint8_t count,
  1054. uint16_t len)
  1055. {
  1056. GSList *l = NULL;
  1057. uint8_t i;
  1058. for (i = 0; count > i; i++) {
  1059. const uint8_t *ptr = (const uint8_t *) p;
  1060. struct a2dp_preset *preset;
  1061. if (len < sizeof(struct audio_preset)) {
  1062. DBG("Invalid preset index %u", i);
  1063. g_slist_free_full(l, preset_free);
  1064. return NULL;
  1065. }
  1066. len -= sizeof(struct audio_preset);
  1067. if (len == 0 || len < p->len) {
  1068. DBG("Invalid preset size of %u for index %u", len, i);
  1069. g_slist_free_full(l, preset_free);
  1070. return NULL;
  1071. }
  1072. preset = g_new0(struct a2dp_preset, 1);
  1073. preset->len = p->len;
  1074. preset->data = g_memdup(p->data, preset->len);
  1075. l = g_slist_append(l, preset);
  1076. len -= preset->len;
  1077. ptr += sizeof(*p) + preset->len;
  1078. p = (const struct audio_preset *) ptr;
  1079. }
  1080. return l;
  1081. }
  1082. static void bt_audio_open(const void *buf, uint16_t len)
  1083. {
  1084. const struct audio_cmd_open *cmd = buf;
  1085. struct audio_rsp_open rsp;
  1086. GSList *presets;
  1087. DBG("");
  1088. audio_retrying = false;
  1089. if (cmd->presets == 0) {
  1090. error("No audio presets found");
  1091. goto failed;
  1092. }
  1093. presets = parse_presets(cmd->preset, cmd->presets, len - sizeof(*cmd));
  1094. if (!presets) {
  1095. error("No audio presets found");
  1096. goto failed;
  1097. }
  1098. rsp.id = register_endpoint(cmd->uuid, cmd->codec, presets);
  1099. if (rsp.id == 0) {
  1100. g_slist_free_full(presets, preset_free);
  1101. error("Unable to register endpoint");
  1102. goto failed;
  1103. }
  1104. g_slist_free(presets);
  1105. ipc_send_rsp_full(audio_ipc, AUDIO_SERVICE_ID, AUDIO_OP_OPEN,
  1106. sizeof(rsp), &rsp, -1);
  1107. return;
  1108. failed:
  1109. ipc_send_rsp(audio_ipc, AUDIO_SERVICE_ID, AUDIO_OP_OPEN,
  1110. AUDIO_STATUS_FAILED);
  1111. }
  1112. static struct a2dp_endpoint *find_endpoint(uint8_t id)
  1113. {
  1114. GSList *l;
  1115. for (l = endpoints; l; l = g_slist_next(l)) {
  1116. struct a2dp_endpoint *endpoint = l->data;
  1117. if (endpoint->id == id)
  1118. return endpoint;
  1119. }
  1120. return NULL;
  1121. }
  1122. static void bt_audio_close(const void *buf, uint16_t len)
  1123. {
  1124. const struct audio_cmd_close *cmd = buf;
  1125. struct a2dp_endpoint *endpoint;
  1126. DBG("");
  1127. endpoint = find_endpoint(cmd->id);
  1128. if (!endpoint) {
  1129. error("Unable to find endpoint %u", cmd->id);
  1130. ipc_send_rsp(audio_ipc, AUDIO_SERVICE_ID, AUDIO_OP_CLOSE,
  1131. AUDIO_STATUS_FAILED);
  1132. return;
  1133. }
  1134. endpoints = g_slist_remove(endpoints, endpoint);
  1135. unregister_endpoint(endpoint);
  1136. ipc_send_rsp(audio_ipc, AUDIO_SERVICE_ID, AUDIO_OP_CLOSE,
  1137. AUDIO_STATUS_SUCCESS);
  1138. }
  1139. static void bt_stream_open(const void *buf, uint16_t len)
  1140. {
  1141. const struct audio_cmd_open_stream *cmd = buf;
  1142. struct audio_rsp_open_stream *rsp;
  1143. struct a2dp_setup *setup;
  1144. int fd;
  1145. uint16_t omtu;
  1146. DBG("");
  1147. if (cmd->id)
  1148. setup = find_setup(cmd->id);
  1149. else
  1150. setup = setups ? setups->data : NULL;
  1151. if (!setup) {
  1152. error("Unable to find stream for endpoint %u", cmd->id);
  1153. ipc_send_rsp(audio_ipc, AUDIO_SERVICE_ID, AUDIO_OP_OPEN_STREAM,
  1154. AUDIO_STATUS_FAILED);
  1155. return;
  1156. }
  1157. if (!avdtp_stream_get_transport(setup->stream, &fd, NULL, &omtu,
  1158. NULL)) {
  1159. error("avdtp_stream_get_transport: failed");
  1160. ipc_send_rsp(audio_ipc, AUDIO_SERVICE_ID, AUDIO_OP_OPEN_STREAM,
  1161. AUDIO_STATUS_FAILED);
  1162. return;
  1163. }
  1164. len = sizeof(struct audio_rsp_open_stream) +
  1165. sizeof(struct audio_preset) + setup->preset->len;
  1166. rsp = g_malloc0(len);
  1167. rsp->id = setup->endpoint->id;
  1168. rsp->mtu = omtu;
  1169. rsp->preset->len = setup->preset->len;
  1170. memcpy(rsp->preset->data, setup->preset->data, setup->preset->len);
  1171. ipc_send_rsp_full(audio_ipc, AUDIO_SERVICE_ID, AUDIO_OP_OPEN_STREAM,
  1172. len, rsp, fd);
  1173. g_free(rsp);
  1174. }
  1175. static void bt_stream_close(const void *buf, uint16_t len)
  1176. {
  1177. const struct audio_cmd_close_stream *cmd = buf;
  1178. struct a2dp_setup *setup;
  1179. int err;
  1180. DBG("");
  1181. setup = find_setup(cmd->id);
  1182. if (!setup) {
  1183. error("Unable to find stream for endpoint %u", cmd->id);
  1184. goto failed;
  1185. }
  1186. err = avdtp_close(setup->dev->session, setup->stream, FALSE);
  1187. if (err < 0) {
  1188. error("avdtp_close: %s", strerror(-err));
  1189. goto failed;
  1190. }
  1191. ipc_send_rsp(audio_ipc, AUDIO_SERVICE_ID, AUDIO_OP_CLOSE_STREAM,
  1192. AUDIO_STATUS_SUCCESS);
  1193. return;
  1194. failed:
  1195. ipc_send_rsp(audio_ipc, AUDIO_SERVICE_ID, AUDIO_OP_CLOSE_STREAM,
  1196. AUDIO_STATUS_FAILED);
  1197. }
  1198. static void bt_stream_resume(const void *buf, uint16_t len)
  1199. {
  1200. const struct audio_cmd_resume_stream *cmd = buf;
  1201. struct a2dp_setup *setup;
  1202. int err;
  1203. DBG("");
  1204. setup = find_setup(cmd->id);
  1205. if (!setup) {
  1206. error("Unable to find stream for endpoint %u", cmd->id);
  1207. goto failed;
  1208. }
  1209. if (setup->state != HAL_AUDIO_STARTED) {
  1210. err = avdtp_start(setup->dev->session, setup->stream);
  1211. if (err < 0) {
  1212. error("avdtp_start: %s", strerror(-err));
  1213. goto failed;
  1214. }
  1215. }
  1216. ipc_send_rsp(audio_ipc, AUDIO_SERVICE_ID, AUDIO_OP_RESUME_STREAM,
  1217. AUDIO_STATUS_SUCCESS);
  1218. return;
  1219. failed:
  1220. ipc_send_rsp(audio_ipc, AUDIO_SERVICE_ID, AUDIO_OP_RESUME_STREAM,
  1221. AUDIO_STATUS_FAILED);
  1222. }
  1223. static void bt_stream_suspend(const void *buf, uint16_t len)
  1224. {
  1225. const struct audio_cmd_suspend_stream *cmd = buf;
  1226. struct a2dp_setup *setup;
  1227. int err;
  1228. DBG("");
  1229. setup = find_setup(cmd->id);
  1230. if (!setup) {
  1231. error("Unable to find stream for endpoint %u", cmd->id);
  1232. goto failed;
  1233. }
  1234. err = avdtp_suspend(setup->dev->session, setup->stream);
  1235. if (err < 0) {
  1236. error("avdtp_suspend: %s", strerror(-err));
  1237. goto failed;
  1238. }
  1239. ipc_send_rsp(audio_ipc, AUDIO_SERVICE_ID, AUDIO_OP_SUSPEND_STREAM,
  1240. AUDIO_STATUS_SUCCESS);
  1241. return;
  1242. failed:
  1243. ipc_send_rsp(audio_ipc, AUDIO_SERVICE_ID, AUDIO_OP_SUSPEND_STREAM,
  1244. AUDIO_STATUS_FAILED);
  1245. }
  1246. static const struct ipc_handler audio_handlers[] = {
  1247. /* AUDIO_OP_OPEN */
  1248. { bt_audio_open, true, sizeof(struct audio_cmd_open) },
  1249. /* AUDIO_OP_CLOSE */
  1250. { bt_audio_close, false, sizeof(struct audio_cmd_close) },
  1251. /* AUDIO_OP_OPEN_STREAM */
  1252. { bt_stream_open, false, sizeof(struct audio_cmd_open_stream) },
  1253. /* AUDIO_OP_CLOSE_STREAM */
  1254. { bt_stream_close, false, sizeof(struct audio_cmd_close_stream) },
  1255. /* AUDIO_OP_RESUME_STREAM */
  1256. { bt_stream_resume, false, sizeof(struct audio_cmd_resume_stream) },
  1257. /* AUDIO_OP_SUSPEND_STREAM */
  1258. { bt_stream_suspend, false, sizeof(struct audio_cmd_suspend_stream) },
  1259. };
  1260. static void bt_audio_unregister(void)
  1261. {
  1262. DBG("");
  1263. if (audio_retry_id > 0)
  1264. g_source_remove(audio_retry_id);
  1265. g_slist_free_full(endpoints, unregister_endpoint);
  1266. endpoints = NULL;
  1267. g_slist_free_full(setups, setup_free);
  1268. setups = NULL;
  1269. ipc_cleanup(audio_ipc);
  1270. audio_ipc = NULL;
  1271. queue_destroy(lseps, NULL);
  1272. }
  1273. static bool bt_audio_register(ipc_disconnect_cb disconnect)
  1274. {
  1275. DBG("");
  1276. audio_ipc = ipc_init(BLUEZ_AUDIO_SK_PATH, sizeof(BLUEZ_AUDIO_SK_PATH),
  1277. AUDIO_SERVICE_ID_MAX, false, disconnect, NULL);
  1278. if (!audio_ipc)
  1279. return false;
  1280. ipc_register(audio_ipc, AUDIO_SERVICE_ID, audio_handlers,
  1281. G_N_ELEMENTS(audio_handlers));
  1282. return true;
  1283. }
  1284. static gboolean audio_retry_register(void *data)
  1285. {
  1286. ipc_disconnect_cb cb = data;
  1287. audio_retry_id = 0;
  1288. audio_retrying = true;
  1289. bt_audio_register(cb);
  1290. return FALSE;
  1291. }
  1292. static void audio_disconnected(void *data)
  1293. {
  1294. GSList *l;
  1295. bool restart;
  1296. DBG("");
  1297. if (audio_retrying)
  1298. goto retry;
  1299. restart = endpoints != NULL ? true : false;
  1300. bt_audio_unregister();
  1301. for (l = devices; l; l = g_slist_next(l)) {
  1302. struct a2dp_device *dev = l->data;
  1303. avdtp_shutdown(dev->session);
  1304. }
  1305. if (!restart)
  1306. return;
  1307. retry:
  1308. audio_retry_id = g_timeout_add_seconds(AUDIO_RETRY_TIMEOUT,
  1309. audio_retry_register,
  1310. audio_disconnected);
  1311. }
  1312. bool bt_a2dp_register(struct ipc *ipc, const bdaddr_t *addr, uint8_t mode)
  1313. {
  1314. GError *err = NULL;
  1315. sdp_record_t *rec;
  1316. DBG("");
  1317. bacpy(&adapter_addr, addr);
  1318. lseps = queue_new();
  1319. server = bt_io_listen(connect_cb, NULL, NULL, NULL, &err,
  1320. BT_IO_OPT_SOURCE_BDADDR, &adapter_addr,
  1321. BT_IO_OPT_PSM, AVDTP_PSM,
  1322. BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM,
  1323. BT_IO_OPT_CENTRAL, true,
  1324. BT_IO_OPT_INVALID);
  1325. if (!server) {
  1326. error("Failed to listen on AVDTP channel: %s", err->message);
  1327. g_error_free(err);
  1328. return false;
  1329. }
  1330. rec = a2dp_record();
  1331. if (!rec) {
  1332. error("Failed to allocate A2DP record");
  1333. goto fail;
  1334. }
  1335. if (bt_adapter_add_record(rec, SVC_HINT_CAPTURING) < 0) {
  1336. error("Failed to register A2DP record");
  1337. sdp_record_free(rec);
  1338. goto fail;
  1339. }
  1340. record_id = rec->handle;
  1341. hal_ipc = ipc;
  1342. ipc_register(hal_ipc, HAL_SERVICE_ID_A2DP, cmd_handlers,
  1343. G_N_ELEMENTS(cmd_handlers));
  1344. if (bt_audio_register(audio_disconnected))
  1345. return true;
  1346. fail:
  1347. g_io_channel_shutdown(server, TRUE, NULL);
  1348. g_io_channel_unref(server);
  1349. server = NULL;
  1350. return false;
  1351. }
  1352. void bt_a2dp_unregister(void)
  1353. {
  1354. DBG("");
  1355. g_slist_free_full(setups, setup_free);
  1356. setups = NULL;
  1357. g_slist_free_full(endpoints, unregister_endpoint);
  1358. endpoints = NULL;
  1359. g_slist_free_full(devices, a2dp_device_free);
  1360. devices = NULL;
  1361. ipc_unregister(hal_ipc, HAL_SERVICE_ID_A2DP);
  1362. hal_ipc = NULL;
  1363. bt_adapter_remove_record(record_id);
  1364. record_id = 0;
  1365. if (server) {
  1366. g_io_channel_shutdown(server, TRUE, NULL);
  1367. g_io_channel_unref(server);
  1368. server = NULL;
  1369. }
  1370. if (audio_ipc) {
  1371. ipc_unregister(audio_ipc, AUDIO_SERVICE_ID);
  1372. ipc_cleanup(audio_ipc);
  1373. audio_ipc = NULL;
  1374. }
  1375. }