a2dp.c 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510
  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) 2011 BMW Car IT GmbH. All rights reserved.
  9. *
  10. *
  11. */
  12. #ifdef HAVE_CONFIG_H
  13. #include <config.h>
  14. #endif
  15. #define _GNU_SOURCE
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <errno.h>
  19. #include <dbus/dbus.h>
  20. #include <glib.h>
  21. #include "lib/bluetooth.h"
  22. #include "lib/sdp.h"
  23. #include "lib/sdp_lib.h"
  24. #include "lib/uuid.h"
  25. #include "gdbus/gdbus.h"
  26. #include "src/btd.h"
  27. #include "src/plugin.h"
  28. #include "src/adapter.h"
  29. #include "src/device.h"
  30. #include "src/dbus-common.h"
  31. #include "src/error.h"
  32. #include "src/profile.h"
  33. #include "src/service.h"
  34. #include "src/log.h"
  35. #include "src/sdpd.h"
  36. #include "src/shared/queue.h"
  37. #include "src/shared/timeout.h"
  38. #include "src/shared/util.h"
  39. #include "btio/btio.h"
  40. #include "avdtp.h"
  41. #include "sink.h"
  42. #include "source.h"
  43. #include "a2dp.h"
  44. #include "a2dp-codecs.h"
  45. #include "media.h"
  46. /* The duration that streams without users are allowed to stay in
  47. * STREAMING state. */
  48. #define SUSPEND_TIMEOUT 5
  49. #define RECONFIGURE_TIMEOUT 500
  50. #define AVDTP_PSM 25
  51. #define MEDIA_ENDPOINT_INTERFACE "org.bluez.MediaEndpoint1"
  52. struct a2dp_sep {
  53. struct a2dp_server *server;
  54. struct a2dp_endpoint *endpoint;
  55. uint8_t type;
  56. uint8_t codec;
  57. struct avdtp_local_sep *lsep;
  58. struct avdtp *session;
  59. struct avdtp_stream *stream;
  60. unsigned int suspend_timer;
  61. gboolean delay_reporting;
  62. gboolean locked;
  63. gboolean suspending;
  64. gboolean starting;
  65. void *user_data;
  66. GDestroyNotify destroy;
  67. };
  68. struct a2dp_setup_cb {
  69. struct a2dp_setup *setup;
  70. a2dp_discover_cb_t discover_cb;
  71. a2dp_select_cb_t select_cb;
  72. a2dp_config_cb_t config_cb;
  73. a2dp_stream_cb_t resume_cb;
  74. a2dp_stream_cb_t suspend_cb;
  75. guint source_id;
  76. void *user_data;
  77. unsigned int id;
  78. };
  79. struct a2dp_setup {
  80. struct a2dp_channel *chan;
  81. struct avdtp *session;
  82. struct queue *eps;
  83. struct a2dp_sep *sep;
  84. struct a2dp_remote_sep *rsep;
  85. struct avdtp_stream *stream;
  86. struct avdtp_error *err;
  87. avdtp_set_configuration_cb setconf_cb;
  88. GSList *seps;
  89. GSList *caps;
  90. gboolean reconfigure;
  91. gboolean start;
  92. GSList *cb;
  93. GIOChannel *io;
  94. guint id;
  95. int ref;
  96. };
  97. struct a2dp_server {
  98. struct btd_adapter *adapter;
  99. GSList *sinks;
  100. GSList *sources;
  101. uint32_t source_record_id;
  102. uint32_t sink_record_id;
  103. gboolean sink_enabled;
  104. gboolean source_enabled;
  105. uint64_t seid_pool;
  106. GIOChannel *io;
  107. struct queue *seps;
  108. struct queue *channels;
  109. };
  110. struct a2dp_remote_sep {
  111. struct a2dp_channel *chan;
  112. char *path;
  113. struct avdtp_remote_sep *sep;
  114. bool from_cache;
  115. };
  116. struct a2dp_last_used {
  117. struct a2dp_sep *lsep;
  118. struct a2dp_remote_sep *rsep;
  119. };
  120. struct a2dp_channel {
  121. struct a2dp_server *server;
  122. struct btd_device *device;
  123. GIOChannel *io;
  124. guint io_id;
  125. unsigned int state_id;
  126. unsigned int auth_id;
  127. struct avdtp *session;
  128. struct queue *seps;
  129. struct a2dp_last_used *last_used;
  130. };
  131. static GSList *servers = NULL;
  132. static GSList *setups = NULL;
  133. static unsigned int cb_id = 0;
  134. static struct a2dp_setup *setup_ref(struct a2dp_setup *setup)
  135. {
  136. setup->ref++;
  137. DBG("%p: ref=%d", setup, setup->ref);
  138. return setup;
  139. }
  140. static bool match_by_session(const void *data, const void *user_data)
  141. {
  142. const struct a2dp_channel *chan = data;
  143. const struct avdtp *session = user_data;
  144. return chan->session == session;
  145. }
  146. static struct a2dp_channel *find_channel(struct avdtp *session)
  147. {
  148. GSList *l;
  149. for (l = servers; l; l = g_slist_next(l)) {
  150. struct a2dp_server *server = l->data;
  151. struct a2dp_channel *chan;
  152. chan = queue_find(server->channels, match_by_session, session);
  153. if (chan)
  154. return chan;
  155. }
  156. return NULL;
  157. }
  158. static struct a2dp_setup *setup_new(struct avdtp *session)
  159. {
  160. struct a2dp_setup *setup;
  161. struct a2dp_channel *chan;
  162. chan = find_channel(session);
  163. if (!chan)
  164. return NULL;
  165. setup = g_new0(struct a2dp_setup, 1);
  166. setup->session = avdtp_ref(session);
  167. setup->chan = find_channel(session);
  168. setups = g_slist_append(setups, setup);
  169. return setup;
  170. }
  171. static void setup_free(struct a2dp_setup *s)
  172. {
  173. DBG("%p", s);
  174. if (s->io) {
  175. g_io_channel_shutdown(s->io, TRUE, NULL);
  176. g_io_channel_unref(s->io);
  177. }
  178. if (s->id)
  179. g_source_remove(s->id);
  180. queue_destroy(s->eps, NULL);
  181. setups = g_slist_remove(setups, s);
  182. if (s->session)
  183. avdtp_unref(s->session);
  184. g_slist_free_full(s->cb, g_free);
  185. g_slist_free_full(s->caps, g_free);
  186. g_free(s);
  187. }
  188. static void setup_unref(struct a2dp_setup *setup)
  189. {
  190. setup->ref--;
  191. DBG("%p: ref=%d", setup, setup->ref);
  192. if (setup->ref > 0)
  193. return;
  194. setup_free(setup);
  195. }
  196. static struct a2dp_setup_cb *setup_cb_new(struct a2dp_setup *setup)
  197. {
  198. struct a2dp_setup_cb *cb;
  199. cb = g_new0(struct a2dp_setup_cb, 1);
  200. cb->setup = setup;
  201. cb->id = ++cb_id;
  202. setup->cb = g_slist_append(setup->cb, cb);
  203. return cb;
  204. }
  205. static void setup_cb_free(struct a2dp_setup_cb *cb)
  206. {
  207. struct a2dp_setup *setup = cb->setup;
  208. if (cb->source_id)
  209. g_source_remove(cb->source_id);
  210. setup->cb = g_slist_remove(setup->cb, cb);
  211. setup_unref(cb->setup);
  212. g_free(cb);
  213. }
  214. static void finalize_setup_errno(struct a2dp_setup *s, int err,
  215. GSourceFunc cb1, ...)
  216. {
  217. GSourceFunc finalize;
  218. va_list args;
  219. struct avdtp_error avdtp_err;
  220. if (err < 0) {
  221. avdtp_error_init(&avdtp_err, AVDTP_ERRNO, -err);
  222. s->err = &avdtp_err;
  223. }
  224. va_start(args, cb1);
  225. finalize = cb1;
  226. setup_ref(s);
  227. while (finalize != NULL) {
  228. finalize(s);
  229. finalize = va_arg(args, GSourceFunc);
  230. }
  231. setup_unref(s);
  232. va_end(args);
  233. }
  234. static int error_to_errno(struct avdtp_error *err)
  235. {
  236. int perr;
  237. if (!err)
  238. return 0;
  239. if (avdtp_error_category(err) != AVDTP_ERRNO)
  240. return -EIO;
  241. perr = avdtp_error_posix_errno(err);
  242. switch (perr) {
  243. case EHOSTDOWN:
  244. case ECONNABORTED:
  245. return -perr;
  246. default:
  247. /*
  248. * An unexpect error has occurred setup may be attempted again.
  249. */
  250. return -EAGAIN;
  251. }
  252. }
  253. static gboolean finalize_config(gpointer data)
  254. {
  255. struct a2dp_setup *s = data;
  256. GSList *l;
  257. struct avdtp_stream *stream = s->err ? NULL : s->stream;
  258. for (l = s->cb; l != NULL; ) {
  259. struct a2dp_setup_cb *cb = l->data;
  260. l = l->next;
  261. if (!cb->config_cb)
  262. continue;
  263. cb->config_cb(s->session, s->sep, stream,
  264. error_to_errno(s->err), cb->user_data);
  265. setup_cb_free(cb);
  266. }
  267. return FALSE;
  268. }
  269. static gboolean finalize_resume(gpointer data)
  270. {
  271. struct a2dp_setup *s = data;
  272. GSList *l;
  273. for (l = s->cb; l != NULL; ) {
  274. struct a2dp_setup_cb *cb = l->data;
  275. l = l->next;
  276. if (!cb->resume_cb)
  277. continue;
  278. cb->resume_cb(s->session, error_to_errno(s->err),
  279. cb->user_data);
  280. setup_cb_free(cb);
  281. }
  282. return FALSE;
  283. }
  284. static gboolean finalize_suspend(gpointer data)
  285. {
  286. struct a2dp_setup *s = data;
  287. GSList *l;
  288. for (l = s->cb; l != NULL; ) {
  289. struct a2dp_setup_cb *cb = l->data;
  290. l = l->next;
  291. if (!cb->suspend_cb)
  292. continue;
  293. cb->suspend_cb(s->session, error_to_errno(s->err),
  294. cb->user_data);
  295. setup_cb_free(cb);
  296. }
  297. return FALSE;
  298. }
  299. static void finalize_select(struct a2dp_setup *s)
  300. {
  301. GSList *l;
  302. for (l = s->cb; l != NULL; ) {
  303. struct a2dp_setup_cb *cb = l->data;
  304. l = l->next;
  305. if (!cb->select_cb)
  306. continue;
  307. cb->select_cb(s->session, s->sep, s->caps,
  308. error_to_errno(s->err), cb->user_data);
  309. setup_cb_free(cb);
  310. }
  311. }
  312. static void finalize_discover(struct a2dp_setup *s)
  313. {
  314. GSList *l;
  315. for (l = s->cb; l != NULL; ) {
  316. struct a2dp_setup_cb *cb = l->data;
  317. l = l->next;
  318. if (!cb->discover_cb)
  319. continue;
  320. cb->discover_cb(s->session, s->seps, error_to_errno(s->err),
  321. cb->user_data);
  322. setup_cb_free(cb);
  323. }
  324. }
  325. static gboolean finalize_all(gpointer data)
  326. {
  327. struct a2dp_setup *s = data;
  328. struct avdtp_stream *stream = s->err ? NULL : s->stream;
  329. GSList *l;
  330. for (l = s->cb; l != NULL; ) {
  331. struct a2dp_setup_cb *cb = l->data;
  332. l = l->next;
  333. if (cb->discover_cb) {
  334. cb->discover_cb(s->session, s->seps,
  335. error_to_errno(s->err), cb->user_data);
  336. } else if (cb->select_cb) {
  337. cb->select_cb(s->session, s->sep, s->caps,
  338. error_to_errno(s->err), cb->user_data);
  339. } else if (cb->suspend_cb) {
  340. cb->suspend_cb(s->session,
  341. error_to_errno(s->err), cb->user_data);
  342. } else if (cb->resume_cb) {
  343. cb->resume_cb(s->session,
  344. error_to_errno(s->err), cb->user_data);
  345. } else if (cb->config_cb) {
  346. cb->config_cb(s->session, s->sep, stream,
  347. error_to_errno(s->err), cb->user_data);
  348. } else
  349. warn("setup_cb doesn't have any callback function");
  350. setup_cb_free(cb);
  351. }
  352. return FALSE;
  353. }
  354. static struct a2dp_setup *find_setup_by_session(struct avdtp *session)
  355. {
  356. GSList *l;
  357. for (l = setups; l != NULL; l = l->next) {
  358. struct a2dp_setup *setup = l->data;
  359. if (setup->session == session)
  360. return setup;
  361. }
  362. return NULL;
  363. }
  364. static struct a2dp_setup *a2dp_setup_get(struct avdtp *session)
  365. {
  366. struct a2dp_setup *setup;
  367. setup = find_setup_by_session(session);
  368. if (!setup) {
  369. setup = setup_new(session);
  370. if (!setup)
  371. return NULL;
  372. }
  373. return setup_ref(setup);
  374. }
  375. static struct a2dp_setup *find_setup_by_stream(struct avdtp_stream *stream)
  376. {
  377. GSList *l;
  378. for (l = setups; l != NULL; l = l->next) {
  379. struct a2dp_setup *setup = l->data;
  380. if (setup->stream == stream)
  381. return setup;
  382. }
  383. return NULL;
  384. }
  385. static void stream_state_changed(struct avdtp_stream *stream,
  386. avdtp_state_t old_state,
  387. avdtp_state_t new_state,
  388. struct avdtp_error *err,
  389. void *user_data)
  390. {
  391. struct a2dp_sep *sep = user_data;
  392. if (new_state == AVDTP_STATE_OPEN) {
  393. struct a2dp_setup *setup;
  394. int err;
  395. setup = find_setup_by_stream(stream);
  396. if (!setup || !setup->start || setup->err)
  397. return;
  398. setup->start = FALSE;
  399. err = avdtp_start(setup->session, stream);
  400. if (err < 0 && err != -EINPROGRESS) {
  401. error("avdtp_start: %s (%d)", strerror(-err), -err);
  402. finalize_setup_errno(setup, err, finalize_resume,
  403. NULL);
  404. return;
  405. }
  406. sep->starting = TRUE;
  407. return;
  408. }
  409. if (new_state != AVDTP_STATE_IDLE)
  410. return;
  411. if (sep->suspend_timer) {
  412. timeout_remove(sep->suspend_timer);
  413. sep->suspend_timer = 0;
  414. }
  415. if (sep->session) {
  416. avdtp_unref(sep->session);
  417. sep->session = NULL;
  418. }
  419. sep->stream = NULL;
  420. if (sep->endpoint && sep->endpoint->clear_configuration)
  421. sep->endpoint->clear_configuration(sep, sep->user_data);
  422. }
  423. static gboolean auto_config(gpointer data)
  424. {
  425. struct a2dp_setup *setup = data;
  426. struct btd_device *dev = NULL;
  427. struct btd_service *service;
  428. /* Check if configuration was aborted */
  429. if (setup->sep->stream == NULL)
  430. return FALSE;
  431. if (setup->err != NULL)
  432. goto done;
  433. dev = avdtp_get_device(setup->session);
  434. avdtp_stream_add_cb(setup->session, setup->stream,
  435. stream_state_changed, setup->sep);
  436. if (setup->sep->type == AVDTP_SEP_TYPE_SOURCE) {
  437. service = btd_device_get_service(dev, A2DP_SINK_UUID);
  438. sink_new_stream(service, setup->session, setup->stream);
  439. } else {
  440. service = btd_device_get_service(dev, A2DP_SOURCE_UUID);
  441. source_new_stream(service, setup->session, setup->stream);
  442. }
  443. done:
  444. if (setup->setconf_cb)
  445. setup->setconf_cb(setup->session, setup->stream, setup->err);
  446. finalize_config(setup);
  447. if (setup->err) {
  448. g_free(setup->err);
  449. setup->err = NULL;
  450. }
  451. setup_unref(setup);
  452. return FALSE;
  453. }
  454. static void endpoint_setconf_cb(struct a2dp_setup *setup, gboolean ret)
  455. {
  456. if (ret == FALSE) {
  457. setup->err = g_new(struct avdtp_error, 1);
  458. avdtp_error_init(setup->err, AVDTP_MEDIA_CODEC,
  459. AVDTP_UNSUPPORTED_CONFIGURATION);
  460. }
  461. auto_config(setup);
  462. setup_unref(setup);
  463. }
  464. static gboolean endpoint_match_codec_ind(struct avdtp *session,
  465. struct avdtp_media_codec_capability *codec,
  466. void *user_data)
  467. {
  468. struct a2dp_sep *sep = user_data;
  469. a2dp_vendor_codec_t *remote_codec;
  470. a2dp_vendor_codec_t *local_codec;
  471. uint8_t *capabilities;
  472. size_t length;
  473. if (codec->media_codec_type != A2DP_CODEC_VENDOR)
  474. return TRUE;
  475. if (sep->endpoint == NULL)
  476. return FALSE;
  477. length = sep->endpoint->get_capabilities(sep, &capabilities,
  478. sep->user_data);
  479. if (length < sizeof(a2dp_vendor_codec_t))
  480. return FALSE;
  481. local_codec = (a2dp_vendor_codec_t *) capabilities;
  482. remote_codec = (a2dp_vendor_codec_t *) codec->data;
  483. if (A2DP_GET_VENDOR_ID(*remote_codec) !=
  484. A2DP_GET_VENDOR_ID(*local_codec))
  485. return FALSE;
  486. if (A2DP_GET_CODEC_ID(*remote_codec) != A2DP_GET_CODEC_ID(*local_codec))
  487. return FALSE;
  488. DBG("vendor 0x%08x codec 0x%04x", A2DP_GET_VENDOR_ID(*remote_codec),
  489. A2DP_GET_CODEC_ID(*remote_codec));
  490. return TRUE;
  491. }
  492. static void reverse_discover(struct avdtp *session, GSList *seps, int err,
  493. void *user_data)
  494. {
  495. DBG("err %d", err);
  496. }
  497. static gboolean endpoint_setconf_ind(struct avdtp *session,
  498. struct avdtp_local_sep *sep,
  499. struct avdtp_stream *stream,
  500. GSList *caps,
  501. avdtp_set_configuration_cb cb,
  502. void *user_data)
  503. {
  504. struct a2dp_sep *a2dp_sep = user_data;
  505. struct a2dp_setup *setup;
  506. if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
  507. DBG("Sink %p: Set_Configuration_Ind", sep);
  508. else
  509. DBG("Source %p: Set_Configuration_Ind", sep);
  510. setup = a2dp_setup_get(session);
  511. if (!setup)
  512. return FALSE;
  513. a2dp_sep->stream = stream;
  514. setup->sep = a2dp_sep;
  515. setup->stream = stream;
  516. setup->setconf_cb = cb;
  517. for (; caps != NULL; caps = g_slist_next(caps)) {
  518. struct avdtp_service_capability *cap = caps->data;
  519. struct avdtp_media_codec_capability *codec;
  520. gboolean ret;
  521. if (cap->category == AVDTP_DELAY_REPORTING &&
  522. !a2dp_sep->delay_reporting) {
  523. setup->err = g_new(struct avdtp_error, 1);
  524. avdtp_error_init(setup->err, AVDTP_DELAY_REPORTING,
  525. AVDTP_UNSUPPORTED_CONFIGURATION);
  526. goto done;
  527. }
  528. if (cap->category != AVDTP_MEDIA_CODEC)
  529. continue;
  530. codec = (struct avdtp_media_codec_capability *) cap->data;
  531. if (codec->media_codec_type != a2dp_sep->codec) {
  532. setup->err = g_new(struct avdtp_error, 1);
  533. avdtp_error_init(setup->err, AVDTP_MEDIA_CODEC,
  534. AVDTP_UNSUPPORTED_CONFIGURATION);
  535. goto done;
  536. }
  537. ret = a2dp_sep->endpoint->set_configuration(a2dp_sep,
  538. codec->data,
  539. cap->length - sizeof(*codec),
  540. setup_ref(setup),
  541. endpoint_setconf_cb,
  542. a2dp_sep->user_data);
  543. if (ret == 0) {
  544. /* Attempt to reverse discover if there are no remote
  545. * SEPs.
  546. */
  547. if (queue_isempty(setup->chan->seps))
  548. a2dp_discover(session, reverse_discover, NULL);
  549. return TRUE;
  550. }
  551. setup_unref(setup);
  552. setup->err = g_new(struct avdtp_error, 1);
  553. avdtp_error_init(setup->err, AVDTP_MEDIA_CODEC,
  554. AVDTP_UNSUPPORTED_CONFIGURATION);
  555. break;
  556. }
  557. done:
  558. g_idle_add(auto_config, setup);
  559. return TRUE;
  560. }
  561. static gboolean endpoint_getcap_ind(struct avdtp *session,
  562. struct avdtp_local_sep *sep,
  563. gboolean get_all, GSList **caps,
  564. uint8_t *err, void *user_data)
  565. {
  566. struct a2dp_sep *a2dp_sep = user_data;
  567. struct avdtp_service_capability *media_transport, *media_codec;
  568. struct avdtp_media_codec_capability *codec_caps;
  569. uint8_t *capabilities;
  570. size_t length;
  571. if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
  572. DBG("Sink %p: Get_Capability_Ind", sep);
  573. else
  574. DBG("Source %p: Get_Capability_Ind", sep);
  575. *caps = NULL;
  576. media_transport = avdtp_service_cap_new(AVDTP_MEDIA_TRANSPORT,
  577. NULL, 0);
  578. *caps = g_slist_append(*caps, media_transport);
  579. length = a2dp_sep->endpoint->get_capabilities(a2dp_sep, &capabilities,
  580. a2dp_sep->user_data);
  581. codec_caps = g_malloc0(sizeof(*codec_caps) + length);
  582. codec_caps->media_type = AVDTP_MEDIA_TYPE_AUDIO;
  583. codec_caps->media_codec_type = a2dp_sep->codec;
  584. memcpy(codec_caps->data, capabilities, length);
  585. media_codec = avdtp_service_cap_new(AVDTP_MEDIA_CODEC, codec_caps,
  586. sizeof(*codec_caps) + length);
  587. *caps = g_slist_append(*caps, media_codec);
  588. g_free(codec_caps);
  589. if (get_all) {
  590. struct avdtp_service_capability *delay_reporting;
  591. delay_reporting = avdtp_service_cap_new(AVDTP_DELAY_REPORTING,
  592. NULL, 0);
  593. *caps = g_slist_append(*caps, delay_reporting);
  594. }
  595. return TRUE;
  596. }
  597. static void endpoint_open_cb(struct a2dp_setup *setup, gboolean ret)
  598. {
  599. int err = error_to_errno(setup->err);
  600. if (ret == FALSE) {
  601. setup->stream = NULL;
  602. finalize_setup_errno(setup, -EPERM, finalize_config, NULL);
  603. goto done;
  604. }
  605. if (err == 0)
  606. err = avdtp_open(setup->session, setup->stream);
  607. if (err == 0)
  608. goto done;
  609. error("avdtp_open %s (%d)", strerror(-err), -err);
  610. setup->stream = NULL;
  611. finalize_setup_errno(setup, err, finalize_config, NULL);
  612. done:
  613. setup_unref(setup);
  614. }
  615. static bool match_remote_sep(const void *data, const void *user_data)
  616. {
  617. const struct a2dp_remote_sep *sep = data;
  618. const struct avdtp_remote_sep *rsep = user_data;
  619. return sep->sep == rsep;
  620. }
  621. static void store_remote_sep(void *data, void *user_data)
  622. {
  623. struct a2dp_remote_sep *sep = data;
  624. GKeyFile *key_file = user_data;
  625. char seid[4], value[256];
  626. struct avdtp_service_capability *service = avdtp_get_codec(sep->sep);
  627. struct avdtp_media_codec_capability *codec;
  628. unsigned int i;
  629. ssize_t offset;
  630. if (!service)
  631. return;
  632. codec = (void *) service->data;
  633. sprintf(seid, "%02hhx", avdtp_get_seid(sep->sep));
  634. offset = sprintf(value, "%02hhx:%02hhx:%02hhx:",
  635. avdtp_get_type(sep->sep), codec->media_codec_type,
  636. avdtp_get_delay_reporting(sep->sep));
  637. for (i = 0; i < service->length - sizeof(*codec); i++)
  638. offset += sprintf(value + offset, "%02hhx", codec->data[i]);
  639. g_key_file_set_string(key_file, "Endpoints", seid, value);
  640. }
  641. static void store_remote_seps(struct a2dp_channel *chan)
  642. {
  643. struct btd_device *device = chan->device;
  644. char filename[PATH_MAX];
  645. char dst_addr[18];
  646. GKeyFile *key_file;
  647. char *data;
  648. gsize length = 0;
  649. if (queue_isempty(chan->seps))
  650. return;
  651. ba2str(device_get_address(device), dst_addr);
  652. snprintf(filename, PATH_MAX, STORAGEDIR "/%s/cache/%s",
  653. btd_adapter_get_storage_dir(device_get_adapter(device)),
  654. dst_addr);
  655. key_file = g_key_file_new();
  656. g_key_file_load_from_file(key_file, filename, 0, NULL);
  657. data = g_key_file_get_string(key_file, "Endpoints", "LastUsed",
  658. NULL);
  659. /* Remove current endpoints since it might have changed */
  660. g_key_file_remove_group(key_file, "Endpoints", NULL);
  661. queue_foreach(chan->seps, store_remote_sep, key_file);
  662. if (data) {
  663. g_key_file_set_string(key_file, "Endpoints", "LastUsed",
  664. data);
  665. g_free(data);
  666. }
  667. data = g_key_file_to_data(key_file, &length, NULL);
  668. g_file_set_contents(filename, data, length, NULL);
  669. g_free(data);
  670. g_key_file_free(key_file);
  671. }
  672. static void invalidate_remote_cache(struct a2dp_setup *setup,
  673. struct avdtp_error *err)
  674. {
  675. if (err->category == AVDTP_ERRNO ||
  676. err->err.error_code != AVDTP_UNSUPPORTED_CONFIGURATION)
  677. return;
  678. /* Attempt to unregister Remote SEP if configuration
  679. * fails with Unsupported Configuration and it was
  680. * loaded from cache.
  681. */
  682. if (setup->rsep && setup->rsep->from_cache) {
  683. warn("Invalidating Remote SEP from cache");
  684. avdtp_unregister_remote_sep(setup->session, setup->rsep->sep);
  685. /* Update cache */
  686. store_remote_seps(setup->chan);
  687. /* Set error to -EAGAIN so the likes of policy plugin can
  688. * reattempt to connect.
  689. */
  690. avdtp_error_init(setup->err, AVDTP_ERRNO, -EAGAIN);
  691. }
  692. }
  693. static void setconf_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
  694. struct avdtp_stream *stream,
  695. struct avdtp_error *err, void *user_data)
  696. {
  697. struct a2dp_sep *a2dp_sep = user_data;
  698. struct a2dp_setup *setup;
  699. struct btd_device *dev;
  700. struct btd_service *service;
  701. int ret;
  702. if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
  703. DBG("Sink %p: Set_Configuration_Cfm", sep);
  704. else
  705. DBG("Source %p: Set_Configuration_Cfm", sep);
  706. setup = find_setup_by_session(session);
  707. if (err) {
  708. if (setup) {
  709. setup_ref(setup);
  710. setup->err = err;
  711. invalidate_remote_cache(setup, err);
  712. finalize_config(setup);
  713. setup->err = NULL;
  714. setup_unref(setup);
  715. }
  716. return;
  717. }
  718. avdtp_stream_add_cb(session, stream, stream_state_changed, a2dp_sep);
  719. a2dp_sep->stream = stream;
  720. if (!setup)
  721. return;
  722. dev = avdtp_get_device(session);
  723. /* Notify D-Bus interface of the new stream */
  724. if (a2dp_sep->type == AVDTP_SEP_TYPE_SOURCE) {
  725. service = btd_device_get_service(dev, A2DP_SINK_UUID);
  726. sink_new_stream(service, session, setup->stream);
  727. } else {
  728. service = btd_device_get_service(dev, A2DP_SOURCE_UUID);
  729. source_new_stream(service, session, setup->stream);
  730. }
  731. /* Notify Endpoint */
  732. if (a2dp_sep->endpoint) {
  733. struct avdtp_service_capability *service;
  734. struct avdtp_media_codec_capability *codec;
  735. int err;
  736. service = avdtp_stream_get_codec(stream);
  737. codec = (struct avdtp_media_codec_capability *) service->data;
  738. err = a2dp_sep->endpoint->set_configuration(a2dp_sep,
  739. codec->data, service->length -
  740. sizeof(*codec),
  741. setup_ref(setup),
  742. endpoint_open_cb,
  743. a2dp_sep->user_data);
  744. if (err == 0)
  745. return;
  746. setup->stream = NULL;
  747. finalize_setup_errno(setup, -EPERM, finalize_config, NULL);
  748. setup_unref(setup);
  749. return;
  750. }
  751. ret = avdtp_open(session, stream);
  752. if (ret < 0) {
  753. error("avdtp_open %s (%d)", strerror(-ret), -ret);
  754. setup->stream = NULL;
  755. finalize_setup_errno(setup, ret, finalize_config, NULL);
  756. }
  757. }
  758. static gboolean getconf_ind(struct avdtp *session, struct avdtp_local_sep *sep,
  759. uint8_t *err, void *user_data)
  760. {
  761. struct a2dp_sep *a2dp_sep = user_data;
  762. if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
  763. DBG("Sink %p: Get_Configuration_Ind", sep);
  764. else
  765. DBG("Source %p: Get_Configuration_Ind", sep);
  766. return TRUE;
  767. }
  768. static void getconf_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
  769. struct avdtp_stream *stream, struct avdtp_error *err,
  770. void *user_data)
  771. {
  772. struct a2dp_sep *a2dp_sep = user_data;
  773. if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
  774. DBG("Sink %p: Set_Configuration_Cfm", sep);
  775. else
  776. DBG("Source %p: Set_Configuration_Cfm", sep);
  777. }
  778. static void store_last_used(struct a2dp_channel *chan, uint8_t lseid,
  779. uint8_t rseid)
  780. {
  781. GKeyFile *key_file;
  782. char filename[PATH_MAX];
  783. char dst_addr[18];
  784. char value[6];
  785. char *data;
  786. size_t len = 0;
  787. ba2str(device_get_address(chan->device), dst_addr);
  788. snprintf(filename, PATH_MAX, STORAGEDIR "/%s/cache/%s",
  789. btd_adapter_get_storage_dir(device_get_adapter(chan->device)),
  790. dst_addr);
  791. key_file = g_key_file_new();
  792. g_key_file_load_from_file(key_file, filename, 0, NULL);
  793. sprintf(value, "%02hhx:%02hhx", lseid, rseid);
  794. g_key_file_set_string(key_file, "Endpoints", "LastUsed", value);
  795. data = g_key_file_to_data(key_file, &len, NULL);
  796. g_file_set_contents(filename, data, len, NULL);
  797. g_free(data);
  798. g_key_file_free(key_file);
  799. }
  800. static void add_last_used(struct a2dp_channel *chan, struct a2dp_sep *lsep,
  801. struct a2dp_remote_sep *rsep)
  802. {
  803. if (!chan->last_used)
  804. chan->last_used = new0(struct a2dp_last_used, 1);
  805. chan->last_used->lsep = lsep;
  806. chan->last_used->rsep = rsep;
  807. }
  808. static void update_last_used(struct a2dp_channel *chan, struct a2dp_sep *lsep,
  809. struct avdtp_stream *stream)
  810. {
  811. struct avdtp_remote_sep *rsep;
  812. struct a2dp_remote_sep *sep;
  813. rsep = avdtp_stream_get_remote_sep(stream);
  814. sep = queue_find(chan->seps, match_remote_sep, rsep);
  815. if (!sep) {
  816. error("Unable to find remote SEP");
  817. return;
  818. }
  819. /* Check if already stored then skip */
  820. if (chan->last_used && (chan->last_used->lsep == lsep &&
  821. chan->last_used->rsep == sep))
  822. return;
  823. add_last_used(chan, lsep, sep);
  824. store_last_used(chan, avdtp_sep_get_seid(lsep->lsep),
  825. avdtp_get_seid(rsep));
  826. }
  827. static gboolean open_ind(struct avdtp *session, struct avdtp_local_sep *sep,
  828. struct avdtp_stream *stream, uint8_t *err,
  829. void *user_data)
  830. {
  831. struct a2dp_sep *a2dp_sep = user_data;
  832. struct a2dp_setup *setup;
  833. if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
  834. DBG("Sink %p: Open_Ind", sep);
  835. else
  836. DBG("Source %p: Open_Ind", sep);
  837. setup = a2dp_setup_get(session);
  838. if (!setup)
  839. return FALSE;
  840. setup->stream = stream;
  841. if (!err && setup->chan)
  842. update_last_used(setup->chan, a2dp_sep, stream);
  843. if (setup->reconfigure)
  844. setup->reconfigure = FALSE;
  845. finalize_config(setup);
  846. return TRUE;
  847. }
  848. static void open_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
  849. struct avdtp_stream *stream, struct avdtp_error *err,
  850. void *user_data)
  851. {
  852. struct a2dp_sep *a2dp_sep = user_data;
  853. struct a2dp_setup *setup;
  854. if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
  855. DBG("Sink %p: Open_Cfm", sep);
  856. else
  857. DBG("Source %p: Open_Cfm", sep);
  858. setup = find_setup_by_session(session);
  859. if (!setup)
  860. return;
  861. if (setup->reconfigure)
  862. setup->reconfigure = FALSE;
  863. if (err) {
  864. setup->stream = NULL;
  865. setup->err = err;
  866. if (setup->start)
  867. finalize_resume(setup);
  868. } else if (setup->chan)
  869. update_last_used(setup->chan, a2dp_sep, stream);
  870. finalize_config(setup);
  871. return;
  872. }
  873. static bool suspend_timeout(struct a2dp_sep *sep)
  874. {
  875. if (avdtp_suspend(sep->session, sep->stream) == 0)
  876. sep->suspending = TRUE;
  877. sep->suspend_timer = 0;
  878. avdtp_unref(sep->session);
  879. sep->session = NULL;
  880. return FALSE;
  881. }
  882. static gboolean start_ind(struct avdtp *session, struct avdtp_local_sep *sep,
  883. struct avdtp_stream *stream, uint8_t *err,
  884. void *user_data)
  885. {
  886. struct a2dp_sep *a2dp_sep = user_data;
  887. struct a2dp_setup *setup;
  888. if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
  889. DBG("Sink %p: Start_Ind", sep);
  890. else
  891. DBG("Source %p: Start_Ind", sep);
  892. if (!a2dp_sep->locked) {
  893. a2dp_sep->session = avdtp_ref(session);
  894. a2dp_sep->suspend_timer = timeout_add_seconds(SUSPEND_TIMEOUT,
  895. (timeout_func_t) suspend_timeout,
  896. a2dp_sep, NULL);
  897. }
  898. if (!a2dp_sep->starting)
  899. return TRUE;
  900. a2dp_sep->starting = FALSE;
  901. setup = find_setup_by_session(session);
  902. if (setup)
  903. finalize_resume(setup);
  904. return TRUE;
  905. }
  906. static void start_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
  907. struct avdtp_stream *stream, struct avdtp_error *err,
  908. void *user_data)
  909. {
  910. struct a2dp_sep *a2dp_sep = user_data;
  911. struct a2dp_setup *setup;
  912. if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
  913. DBG("Sink %p: Start_Cfm", sep);
  914. else
  915. DBG("Source %p: Start_Cfm", sep);
  916. a2dp_sep->starting = FALSE;
  917. setup = find_setup_by_session(session);
  918. if (!setup)
  919. return;
  920. if (err) {
  921. setup->stream = NULL;
  922. setup->err = err;
  923. }
  924. finalize_resume(setup);
  925. }
  926. static gboolean suspend_ind(struct avdtp *session, struct avdtp_local_sep *sep,
  927. struct avdtp_stream *stream, uint8_t *err,
  928. void *user_data)
  929. {
  930. struct a2dp_sep *a2dp_sep = user_data;
  931. struct a2dp_setup *setup;
  932. gboolean start;
  933. int start_err;
  934. if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
  935. DBG("Sink %p: Suspend_Ind", sep);
  936. else
  937. DBG("Source %p: Suspend_Ind", sep);
  938. if (a2dp_sep->suspend_timer) {
  939. timeout_remove(a2dp_sep->suspend_timer);
  940. a2dp_sep->suspend_timer = 0;
  941. avdtp_unref(a2dp_sep->session);
  942. a2dp_sep->session = NULL;
  943. }
  944. if (!a2dp_sep->suspending)
  945. return TRUE;
  946. a2dp_sep->suspending = FALSE;
  947. setup = find_setup_by_session(session);
  948. if (!setup)
  949. return TRUE;
  950. start = setup->start;
  951. setup->start = FALSE;
  952. finalize_suspend(setup);
  953. if (!start)
  954. return TRUE;
  955. start_err = avdtp_start(session, a2dp_sep->stream);
  956. if (start_err < 0 && start_err != -EINPROGRESS) {
  957. error("avdtp_start: %s (%d)", strerror(-start_err),
  958. -start_err);
  959. finalize_setup_errno(setup, start_err, finalize_resume, NULL);
  960. }
  961. return TRUE;
  962. }
  963. static void suspend_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
  964. struct avdtp_stream *stream, struct avdtp_error *err,
  965. void *user_data)
  966. {
  967. struct a2dp_sep *a2dp_sep = user_data;
  968. struct a2dp_setup *setup;
  969. gboolean start;
  970. int start_err;
  971. if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
  972. DBG("Sink %p: Suspend_Cfm", sep);
  973. else
  974. DBG("Source %p: Suspend_Cfm", sep);
  975. a2dp_sep->suspending = FALSE;
  976. setup = find_setup_by_session(session);
  977. if (!setup)
  978. return;
  979. start = setup->start;
  980. setup->start = FALSE;
  981. if (err) {
  982. setup->stream = NULL;
  983. setup->err = err;
  984. }
  985. finalize_suspend(setup);
  986. if (!start)
  987. return;
  988. if (err) {
  989. finalize_resume(setup);
  990. return;
  991. }
  992. start_err = avdtp_start(session, a2dp_sep->stream);
  993. if (start_err < 0 && start_err != -EINPROGRESS) {
  994. error("avdtp_start: %s (%d)", strerror(-start_err), -start_err);
  995. finalize_setup_errno(setup, start_err, finalize_suspend, NULL);
  996. }
  997. }
  998. static gboolean close_ind(struct avdtp *session, struct avdtp_local_sep *sep,
  999. struct avdtp_stream *stream, uint8_t *err,
  1000. void *user_data)
  1001. {
  1002. struct a2dp_sep *a2dp_sep = user_data;
  1003. struct a2dp_setup *setup;
  1004. if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
  1005. DBG("Sink %p: Close_Ind", sep);
  1006. else
  1007. DBG("Source %p: Close_Ind", sep);
  1008. setup = find_setup_by_session(session);
  1009. if (!setup)
  1010. return TRUE;
  1011. finalize_setup_errno(setup, -ECONNRESET, finalize_suspend,
  1012. finalize_resume, NULL);
  1013. return TRUE;
  1014. }
  1015. static struct a2dp_remote_sep *find_remote_sep(struct a2dp_channel *chan,
  1016. struct a2dp_sep *sep)
  1017. {
  1018. struct avdtp_remote_sep *rsep;
  1019. rsep = avdtp_find_remote_sep(chan->session, sep->lsep);
  1020. return queue_find(chan->seps, match_remote_sep, rsep);
  1021. }
  1022. static gboolean a2dp_reconfigure(gpointer data)
  1023. {
  1024. struct a2dp_setup *setup = data;
  1025. struct a2dp_sep *sep = setup->sep;
  1026. int posix_err;
  1027. struct avdtp_media_codec_capability *rsep_codec;
  1028. struct avdtp_service_capability *cap;
  1029. setup->id = 0;
  1030. if (setup->err) {
  1031. posix_err = error_to_errno(setup->err);
  1032. goto failed;
  1033. }
  1034. if (!sep->lsep) {
  1035. error("no valid local SEP");
  1036. posix_err = -EINVAL;
  1037. goto failed;
  1038. }
  1039. if (setup->rsep) {
  1040. cap = avdtp_get_codec(setup->rsep->sep);
  1041. rsep_codec = (struct avdtp_media_codec_capability *) cap->data;
  1042. }
  1043. if (!setup->rsep || sep->codec != rsep_codec->media_codec_type)
  1044. setup->rsep = find_remote_sep(setup->chan, sep);
  1045. if (!setup->rsep) {
  1046. error("unable to find remote SEP");
  1047. posix_err = -EINVAL;
  1048. goto failed;
  1049. }
  1050. posix_err = avdtp_set_configuration(setup->session, setup->rsep->sep,
  1051. sep->lsep,
  1052. setup->caps,
  1053. &setup->stream);
  1054. if (posix_err < 0) {
  1055. error("avdtp_set_configuration: %s", strerror(-posix_err));
  1056. goto failed;
  1057. }
  1058. return FALSE;
  1059. failed:
  1060. finalize_setup_errno(setup, posix_err, finalize_config, NULL);
  1061. return FALSE;
  1062. }
  1063. static bool setup_reconfigure(struct a2dp_setup *setup)
  1064. {
  1065. if (!setup->reconfigure || setup->id)
  1066. return false;
  1067. DBG("%p", setup);
  1068. setup->id = g_timeout_add(RECONFIGURE_TIMEOUT, a2dp_reconfigure, setup);
  1069. setup->reconfigure = FALSE;
  1070. return true;
  1071. }
  1072. static struct a2dp_remote_sep *get_remote_sep(struct a2dp_channel *chan,
  1073. struct avdtp_stream *stream)
  1074. {
  1075. struct avdtp_remote_sep *rsep;
  1076. rsep = avdtp_stream_get_remote_sep(stream);
  1077. return queue_find(chan->seps, match_remote_sep, rsep);
  1078. }
  1079. static void close_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
  1080. struct avdtp_stream *stream, struct avdtp_error *err,
  1081. void *user_data)
  1082. {
  1083. struct a2dp_sep *a2dp_sep = user_data;
  1084. struct a2dp_setup *setup;
  1085. if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
  1086. DBG("Sink %p: Close_Cfm", sep);
  1087. else
  1088. DBG("Source %p: Close_Cfm", sep);
  1089. setup = find_setup_by_session(session);
  1090. if (!setup)
  1091. return;
  1092. if (err) {
  1093. setup->stream = NULL;
  1094. setup->err = err;
  1095. finalize_config(setup);
  1096. return;
  1097. }
  1098. if (!setup->rsep)
  1099. setup->rsep = get_remote_sep(setup->chan, stream);
  1100. setup_reconfigure(setup);
  1101. }
  1102. static void abort_ind(struct avdtp *session, struct avdtp_local_sep *sep,
  1103. struct avdtp_stream *stream, uint8_t *err,
  1104. void *user_data)
  1105. {
  1106. struct a2dp_sep *a2dp_sep = user_data;
  1107. struct a2dp_setup *setup;
  1108. if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
  1109. DBG("Sink %p: Abort_Ind", sep);
  1110. else
  1111. DBG("Source %p: Abort_Ind", sep);
  1112. a2dp_sep->stream = NULL;
  1113. setup = find_setup_by_session(session);
  1114. if (!setup)
  1115. return;
  1116. finalize_setup_errno(setup, -ECONNRESET, finalize_suspend,
  1117. finalize_resume,
  1118. finalize_config, NULL);
  1119. return;
  1120. }
  1121. static void abort_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
  1122. struct avdtp_stream *stream, struct avdtp_error *err,
  1123. void *user_data)
  1124. {
  1125. struct a2dp_sep *a2dp_sep = user_data;
  1126. struct a2dp_setup *setup;
  1127. if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
  1128. DBG("Sink %p: Abort_Cfm", sep);
  1129. else
  1130. DBG("Source %p: Abort_Cfm", sep);
  1131. setup = find_setup_by_session(session);
  1132. if (!setup)
  1133. return;
  1134. if (setup_reconfigure(setup))
  1135. return;
  1136. setup_unref(setup);
  1137. }
  1138. static gboolean reconf_ind(struct avdtp *session, struct avdtp_local_sep *sep,
  1139. uint8_t *err, void *user_data)
  1140. {
  1141. struct a2dp_sep *a2dp_sep = user_data;
  1142. if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
  1143. DBG("Sink %p: ReConfigure_Ind", sep);
  1144. else
  1145. DBG("Source %p: ReConfigure_Ind", sep);
  1146. return TRUE;
  1147. }
  1148. static gboolean endpoint_delayreport_ind(struct avdtp *session,
  1149. struct avdtp_local_sep *sep,
  1150. uint8_t rseid, uint16_t delay,
  1151. uint8_t *err, void *user_data)
  1152. {
  1153. struct a2dp_sep *a2dp_sep = user_data;
  1154. if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
  1155. DBG("Sink %p: DelayReport_Ind", sep);
  1156. else
  1157. DBG("Source %p: DelayReport_Ind", sep);
  1158. if (a2dp_sep->endpoint == NULL ||
  1159. a2dp_sep->endpoint->set_delay == NULL)
  1160. return FALSE;
  1161. a2dp_sep->endpoint->set_delay(a2dp_sep, delay, a2dp_sep->user_data);
  1162. return TRUE;
  1163. }
  1164. static void reconf_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
  1165. struct avdtp_stream *stream, struct avdtp_error *err,
  1166. void *user_data)
  1167. {
  1168. struct a2dp_sep *a2dp_sep = user_data;
  1169. struct a2dp_setup *setup;
  1170. if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
  1171. DBG("Sink %p: ReConfigure_Cfm", sep);
  1172. else
  1173. DBG("Source %p: ReConfigure_Cfm", sep);
  1174. setup = find_setup_by_session(session);
  1175. if (!setup)
  1176. return;
  1177. if (err) {
  1178. setup->stream = NULL;
  1179. setup->err = err;
  1180. }
  1181. finalize_config(setup);
  1182. }
  1183. static void delay_report_cfm(struct avdtp *session, struct avdtp_local_sep *sep,
  1184. struct avdtp_stream *stream,
  1185. struct avdtp_error *err, void *user_data)
  1186. {
  1187. struct a2dp_sep *a2dp_sep = user_data;
  1188. if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)
  1189. DBG("Sink %p: DelayReport_Cfm", sep);
  1190. else
  1191. DBG("Source %p: DelayReport_Cfm", sep);
  1192. }
  1193. static struct avdtp_sep_cfm cfm = {
  1194. .set_configuration = setconf_cfm,
  1195. .get_configuration = getconf_cfm,
  1196. .open = open_cfm,
  1197. .start = start_cfm,
  1198. .suspend = suspend_cfm,
  1199. .close = close_cfm,
  1200. .abort = abort_cfm,
  1201. .reconfigure = reconf_cfm,
  1202. .delay_report = delay_report_cfm,
  1203. };
  1204. static struct avdtp_sep_ind endpoint_ind = {
  1205. .match_codec = endpoint_match_codec_ind,
  1206. .get_capability = endpoint_getcap_ind,
  1207. .set_configuration = endpoint_setconf_ind,
  1208. .get_configuration = getconf_ind,
  1209. .open = open_ind,
  1210. .start = start_ind,
  1211. .suspend = suspend_ind,
  1212. .close = close_ind,
  1213. .abort = abort_ind,
  1214. .reconfigure = reconf_ind,
  1215. .delayreport = endpoint_delayreport_ind,
  1216. };
  1217. static sdp_record_t *a2dp_record(uint8_t type)
  1218. {
  1219. sdp_list_t *svclass_id, *pfseq, *apseq, *root;
  1220. uuid_t root_uuid, l2cap_uuid, avdtp_uuid, a2dp_uuid;
  1221. sdp_profile_desc_t profile[1];
  1222. sdp_list_t *aproto, *proto[2];
  1223. sdp_record_t *record;
  1224. sdp_data_t *psm, *version, *features;
  1225. uint16_t lp = AVDTP_UUID;
  1226. uint16_t a2dp_ver = 0x0103, avdtp_ver = 0x0103, feat = 0x000f;
  1227. record = sdp_record_alloc();
  1228. if (!record)
  1229. return NULL;
  1230. sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
  1231. root = sdp_list_append(0, &root_uuid);
  1232. sdp_set_browse_groups(record, root);
  1233. if (type == AVDTP_SEP_TYPE_SOURCE)
  1234. sdp_uuid16_create(&a2dp_uuid, AUDIO_SOURCE_SVCLASS_ID);
  1235. else
  1236. sdp_uuid16_create(&a2dp_uuid, AUDIO_SINK_SVCLASS_ID);
  1237. svclass_id = sdp_list_append(0, &a2dp_uuid);
  1238. sdp_set_service_classes(record, svclass_id);
  1239. sdp_uuid16_create(&profile[0].uuid, ADVANCED_AUDIO_PROFILE_ID);
  1240. profile[0].version = a2dp_ver;
  1241. pfseq = sdp_list_append(0, &profile[0]);
  1242. sdp_set_profile_descs(record, pfseq);
  1243. sdp_uuid16_create(&l2cap_uuid, L2CAP_UUID);
  1244. proto[0] = sdp_list_append(0, &l2cap_uuid);
  1245. psm = sdp_data_alloc(SDP_UINT16, &lp);
  1246. proto[0] = sdp_list_append(proto[0], psm);
  1247. apseq = sdp_list_append(0, proto[0]);
  1248. sdp_uuid16_create(&avdtp_uuid, AVDTP_UUID);
  1249. proto[1] = sdp_list_append(0, &avdtp_uuid);
  1250. version = sdp_data_alloc(SDP_UINT16, &avdtp_ver);
  1251. proto[1] = sdp_list_append(proto[1], version);
  1252. apseq = sdp_list_append(apseq, proto[1]);
  1253. aproto = sdp_list_append(0, apseq);
  1254. sdp_set_access_protos(record, aproto);
  1255. features = sdp_data_alloc(SDP_UINT16, &feat);
  1256. sdp_attr_add(record, SDP_ATTR_SUPPORTED_FEATURES, features);
  1257. if (type == AVDTP_SEP_TYPE_SOURCE)
  1258. sdp_set_info_attr(record, "Audio Source", 0, 0);
  1259. else
  1260. sdp_set_info_attr(record, "Audio Sink", 0, 0);
  1261. free(psm);
  1262. free(version);
  1263. sdp_list_free(proto[0], 0);
  1264. sdp_list_free(proto[1], 0);
  1265. sdp_list_free(apseq, 0);
  1266. sdp_list_free(pfseq, 0);
  1267. sdp_list_free(aproto, 0);
  1268. sdp_list_free(root, 0);
  1269. sdp_list_free(svclass_id, 0);
  1270. return record;
  1271. }
  1272. static struct a2dp_server *find_server(GSList *list, struct btd_adapter *a)
  1273. {
  1274. for (; list; list = list->next) {
  1275. struct a2dp_server *server = list->data;
  1276. if (server->adapter == a)
  1277. return server;
  1278. }
  1279. return NULL;
  1280. }
  1281. static void remote_sep_free(void *data)
  1282. {
  1283. struct a2dp_remote_sep *sep = data;
  1284. avdtp_remote_sep_set_destroy(sep->sep, NULL, NULL);
  1285. free(sep->path);
  1286. free(sep);
  1287. }
  1288. static void remove_remote_sep(void *data)
  1289. {
  1290. struct a2dp_remote_sep *sep = data;
  1291. if (!sep->path) {
  1292. remote_sep_free(sep);
  1293. return;
  1294. }
  1295. g_dbus_unregister_interface(btd_get_dbus_connection(), sep->path,
  1296. MEDIA_ENDPOINT_INTERFACE);
  1297. }
  1298. static void channel_free(void *data)
  1299. {
  1300. struct a2dp_channel *chan = data;
  1301. struct a2dp_setup *setup;
  1302. if (chan->auth_id > 0)
  1303. btd_cancel_authorization(chan->auth_id);
  1304. if (chan->io_id > 0)
  1305. g_source_remove(chan->io_id);
  1306. if (chan->io) {
  1307. g_io_channel_shutdown(chan->io, TRUE, NULL);
  1308. g_io_channel_unref(chan->io);
  1309. }
  1310. avdtp_remove_state_cb(chan->state_id);
  1311. queue_destroy(chan->seps, remove_remote_sep);
  1312. free(chan->last_used);
  1313. setup = find_setup_by_session(chan->session);
  1314. if (setup) {
  1315. setup->chan = NULL;
  1316. setup_ref(setup);
  1317. /* Finalize pending commands before we NULL setup->session */
  1318. finalize_setup_errno(setup, -ENOTCONN, finalize_all, NULL);
  1319. avdtp_unref(setup->session);
  1320. setup->session = NULL;
  1321. setup_unref(setup);
  1322. }
  1323. g_free(chan);
  1324. }
  1325. static void channel_remove(struct a2dp_channel *chan)
  1326. {
  1327. struct a2dp_server *server = chan->server;
  1328. DBG("chan %p", chan);
  1329. queue_remove(server->channels, chan);
  1330. channel_free(chan);
  1331. }
  1332. static gboolean disconnect_cb(GIOChannel *io, GIOCondition cond, gpointer data)
  1333. {
  1334. struct a2dp_channel *chan = data;
  1335. DBG("chan %p", chan);
  1336. chan->io_id = 0;
  1337. channel_remove(chan);
  1338. return FALSE;
  1339. }
  1340. static void caps_add_codec(GSList **l, uint8_t codec, uint8_t *caps,
  1341. size_t size)
  1342. {
  1343. struct avdtp_service_capability *media_transport, *media_codec;
  1344. struct avdtp_media_codec_capability *cap;
  1345. media_transport = avdtp_service_cap_new(AVDTP_MEDIA_TRANSPORT,
  1346. NULL, 0);
  1347. *l = g_slist_append(*l, media_transport);
  1348. cap = g_malloc0(sizeof(*cap) + size);
  1349. cap->media_type = AVDTP_MEDIA_TYPE_AUDIO;
  1350. cap->media_codec_type = codec;
  1351. memcpy(cap->data, caps, size);
  1352. media_codec = avdtp_service_cap_new(AVDTP_MEDIA_CODEC, cap,
  1353. sizeof(*cap) + size);
  1354. *l = g_slist_append(*l, media_codec);
  1355. g_free(cap);
  1356. }
  1357. struct client {
  1358. const char *sender;
  1359. const char *path;
  1360. };
  1361. static int match_client(const void *data, const void *user_data)
  1362. {
  1363. struct a2dp_sep *sep = (void *) data;
  1364. const struct a2dp_endpoint *endpoint = sep->endpoint;
  1365. const struct client *client = user_data;
  1366. if (strcmp(client->sender, endpoint->get_name(sep, sep->user_data)))
  1367. return -1;
  1368. return strcmp(client->path, endpoint->get_path(sep, sep->user_data));
  1369. }
  1370. static struct a2dp_sep *find_sep(struct a2dp_server *server, uint8_t type,
  1371. const char *sender, const char *path)
  1372. {
  1373. GSList *l;
  1374. struct client client = { sender, path };
  1375. l = type == AVDTP_SEP_TYPE_SINK ? server->sources : server->sinks;
  1376. l = g_slist_find_custom(l, &client, match_client);
  1377. if (l)
  1378. return l->data;
  1379. return NULL;
  1380. }
  1381. static int parse_properties(DBusMessageIter *props, uint8_t **caps, int *size)
  1382. {
  1383. while (dbus_message_iter_get_arg_type(props) == DBUS_TYPE_DICT_ENTRY) {
  1384. const char *key;
  1385. DBusMessageIter value, entry;
  1386. int var;
  1387. dbus_message_iter_recurse(props, &entry);
  1388. dbus_message_iter_get_basic(&entry, &key);
  1389. dbus_message_iter_next(&entry);
  1390. dbus_message_iter_recurse(&entry, &value);
  1391. var = dbus_message_iter_get_arg_type(&value);
  1392. if (strcasecmp(key, "Capabilities") == 0) {
  1393. DBusMessageIter array;
  1394. if (var != DBUS_TYPE_ARRAY)
  1395. return -EINVAL;
  1396. dbus_message_iter_recurse(&value, &array);
  1397. dbus_message_iter_get_fixed_array(&array, caps, size);
  1398. return 0;
  1399. }
  1400. dbus_message_iter_next(props);
  1401. }
  1402. return -EINVAL;
  1403. }
  1404. static void reconfig_cb(struct avdtp *session, struct a2dp_sep *sep,
  1405. struct avdtp_stream *stream, int err, void *user_data)
  1406. {
  1407. DBusMessage *msg = user_data;
  1408. if (err)
  1409. g_dbus_send_message(btd_get_dbus_connection(),
  1410. btd_error_failed(msg, strerror(-err)));
  1411. else
  1412. g_dbus_send_reply(btd_get_dbus_connection(), msg,
  1413. DBUS_TYPE_INVALID);
  1414. dbus_message_unref(msg);
  1415. }
  1416. static int a2dp_reconfig(struct a2dp_channel *chan, const char *sender,
  1417. struct a2dp_sep *lsep, struct a2dp_remote_sep *rsep,
  1418. uint8_t *caps, int size, void *user_data)
  1419. {
  1420. struct a2dp_setup *setup;
  1421. struct a2dp_setup_cb *cb_data;
  1422. GSList *l;
  1423. int err;
  1424. setup = a2dp_setup_get(chan->session);
  1425. if (!setup)
  1426. return -ENOMEM;
  1427. cb_data = setup_cb_new(setup);
  1428. cb_data->config_cb = reconfig_cb;
  1429. cb_data->user_data = user_data;
  1430. setup->sep = lsep;
  1431. setup->rsep = rsep;
  1432. g_slist_free_full(setup->caps, g_free);
  1433. setup->caps = NULL;
  1434. caps_add_codec(&setup->caps, setup->sep->codec, caps, size);
  1435. l = avdtp_get_type(rsep->sep) == AVDTP_SEP_TYPE_SINK ?
  1436. chan->server->sources :
  1437. chan->server->sinks;
  1438. /* Check for existing stream and close it */
  1439. for (; l; l = g_slist_next(l)) {
  1440. struct a2dp_sep *tmp = l->data;
  1441. /* Attempt to reconfigure if a stream already exists */
  1442. if (tmp->stream) {
  1443. /* Only allow switching sep from the same sender */
  1444. if (strcmp(sender, tmp->endpoint->get_name(tmp,
  1445. tmp->user_data)))
  1446. return -EPERM;
  1447. /* Check if stream is for the channel */
  1448. if (!avdtp_has_stream(chan->session, tmp->stream))
  1449. continue;
  1450. err = avdtp_close(chan->session, tmp->stream, FALSE);
  1451. if (err < 0) {
  1452. err = avdtp_abort(chan->session, tmp->stream);
  1453. if (err < 0) {
  1454. error("avdtp_abort: %s",
  1455. strerror(-err));
  1456. goto fail;
  1457. }
  1458. }
  1459. setup->reconfigure = TRUE;
  1460. return 0;
  1461. }
  1462. }
  1463. err = avdtp_set_configuration(setup->session, setup->rsep->sep,
  1464. lsep->lsep,
  1465. setup->caps,
  1466. &setup->stream);
  1467. if (err < 0) {
  1468. error("avdtp_set_configuration: %s", strerror(-err));
  1469. goto fail;
  1470. }
  1471. return 0;
  1472. fail:
  1473. setup_cb_free(cb_data);
  1474. return err;
  1475. }
  1476. static DBusMessage *set_configuration(DBusConnection *conn, DBusMessage *msg,
  1477. void *data)
  1478. {
  1479. struct a2dp_remote_sep *rsep = data;
  1480. struct a2dp_channel *chan = rsep->chan;
  1481. struct a2dp_sep *lsep = NULL;
  1482. struct avdtp_service_capability *service;
  1483. struct avdtp_media_codec_capability *codec;
  1484. DBusMessageIter args, props;
  1485. const char *sender, *path;
  1486. uint8_t *caps;
  1487. int err, size = 0;
  1488. sender = dbus_message_get_sender(msg);
  1489. dbus_message_iter_init(msg, &args);
  1490. dbus_message_iter_get_basic(&args, &path);
  1491. dbus_message_iter_next(&args);
  1492. lsep = find_sep(chan->server, avdtp_get_type(rsep->sep), sender, path);
  1493. if (!lsep)
  1494. return btd_error_invalid_args(msg);
  1495. service = avdtp_get_codec(rsep->sep);
  1496. codec = (struct avdtp_media_codec_capability *) service->data;
  1497. /* Check if codec really matches */
  1498. if (!endpoint_match_codec_ind(chan->session, codec, lsep))
  1499. return btd_error_invalid_args(msg);
  1500. dbus_message_iter_recurse(&args, &props);
  1501. if (dbus_message_iter_get_arg_type(&props) != DBUS_TYPE_DICT_ENTRY)
  1502. return btd_error_invalid_args(msg);
  1503. if (parse_properties(&props, &caps, &size) < 0)
  1504. return btd_error_invalid_args(msg);
  1505. err = a2dp_reconfig(chan, sender, lsep, rsep, caps, size,
  1506. dbus_message_ref(msg));
  1507. if (err < 0) {
  1508. dbus_message_unref(msg);
  1509. return btd_error_failed(msg, strerror(-err));
  1510. }
  1511. return NULL;
  1512. }
  1513. static const GDBusMethodTable sep_methods[] = {
  1514. { GDBUS_ASYNC_METHOD("SetConfiguration",
  1515. GDBUS_ARGS({ "endpoint", "o" },
  1516. { "properties", "a{sv}" } ),
  1517. NULL, set_configuration) },
  1518. { },
  1519. };
  1520. static gboolean get_uuid(const GDBusPropertyTable *property,
  1521. DBusMessageIter *iter, void *data)
  1522. {
  1523. struct a2dp_remote_sep *sep = data;
  1524. const char *uuid;
  1525. switch (avdtp_get_type(sep->sep)) {
  1526. case AVDTP_SEP_TYPE_SOURCE:
  1527. uuid = A2DP_SOURCE_UUID;
  1528. break;
  1529. case AVDTP_SEP_TYPE_SINK:
  1530. uuid = A2DP_SINK_UUID;
  1531. break;
  1532. default:
  1533. uuid = "";
  1534. }
  1535. dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &uuid);
  1536. return TRUE;
  1537. }
  1538. static gboolean get_codec(const GDBusPropertyTable *property,
  1539. DBusMessageIter *iter, void *data)
  1540. {
  1541. struct a2dp_remote_sep *sep = data;
  1542. struct avdtp_service_capability *cap = avdtp_get_codec(sep->sep);
  1543. struct avdtp_media_codec_capability *codec = (void *) cap->data;
  1544. dbus_message_iter_append_basic(iter, DBUS_TYPE_BYTE,
  1545. &codec->media_codec_type);
  1546. return TRUE;
  1547. }
  1548. static gboolean get_capabilities(const GDBusPropertyTable *property,
  1549. DBusMessageIter *iter, void *data)
  1550. {
  1551. struct a2dp_remote_sep *sep = data;
  1552. struct avdtp_service_capability *service = avdtp_get_codec(sep->sep);
  1553. struct avdtp_media_codec_capability *codec = (void *) service->data;
  1554. uint8_t *caps = codec->data;
  1555. DBusMessageIter array;
  1556. dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
  1557. DBUS_TYPE_BYTE_AS_STRING, &array);
  1558. dbus_message_iter_append_fixed_array(&array, DBUS_TYPE_BYTE, &caps,
  1559. service->length - sizeof(*codec));
  1560. dbus_message_iter_close_container(iter, &array);
  1561. return TRUE;
  1562. }
  1563. static gboolean get_device(const GDBusPropertyTable *property,
  1564. DBusMessageIter *iter, void *data)
  1565. {
  1566. struct a2dp_remote_sep *sep = data;
  1567. const char *path;
  1568. path = device_get_path(sep->chan->device);
  1569. dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);
  1570. return TRUE;
  1571. }
  1572. static gboolean get_delay_reporting(const GDBusPropertyTable *property,
  1573. DBusMessageIter *iter, void *data)
  1574. {
  1575. struct a2dp_remote_sep *sep = data;
  1576. dbus_bool_t delay_report;
  1577. delay_report = avdtp_get_delay_reporting(sep->sep);
  1578. dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, &delay_report);
  1579. return TRUE;
  1580. }
  1581. static const GDBusPropertyTable sep_properties[] = {
  1582. { "UUID", "s", get_uuid, NULL, NULL },
  1583. { "Codec", "y", get_codec, NULL, NULL },
  1584. { "Capabilities", "ay", get_capabilities, NULL, NULL },
  1585. { "Device", "o", get_device, NULL, NULL },
  1586. { "DelayReporting", "b", get_delay_reporting, NULL, NULL },
  1587. { }
  1588. };
  1589. static void remote_sep_destroy(void *user_data)
  1590. {
  1591. struct a2dp_remote_sep *sep = user_data;
  1592. if (queue_remove(sep->chan->seps, sep))
  1593. remove_remote_sep(sep);
  1594. }
  1595. static struct a2dp_remote_sep *register_remote_sep(void *data, void *user_data)
  1596. {
  1597. struct avdtp_remote_sep *rsep = data;
  1598. struct a2dp_channel *chan = user_data;
  1599. struct a2dp_remote_sep *sep;
  1600. sep = queue_find(chan->seps, match_remote_sep, rsep);
  1601. if (sep)
  1602. return sep;
  1603. sep = new0(struct a2dp_remote_sep, 1);
  1604. sep->chan = chan;
  1605. sep->sep = rsep;
  1606. if (asprintf(&sep->path, "%s/sep%d",
  1607. device_get_path(chan->device),
  1608. avdtp_get_seid(rsep)) < 0) {
  1609. error("Could not allocate path for remote sep %s/sep%d",
  1610. device_get_path(chan->device),
  1611. avdtp_get_seid(rsep));
  1612. sep->path = NULL;
  1613. goto done;
  1614. }
  1615. if (g_dbus_register_interface(btd_get_dbus_connection(),
  1616. sep->path, MEDIA_ENDPOINT_INTERFACE,
  1617. sep_methods, NULL, sep_properties,
  1618. sep, remote_sep_free) == FALSE) {
  1619. error("Could not register remote sep %s", sep->path);
  1620. free(sep->path);
  1621. free(sep);
  1622. return NULL;
  1623. }
  1624. DBG("Found remote SEP: %s", sep->path);
  1625. avdtp_remote_sep_set_destroy(rsep, sep, remote_sep_destroy);
  1626. done:
  1627. queue_push_tail(chan->seps, sep);
  1628. return sep;
  1629. }
  1630. static bool match_seid(const void *data, const void *user_data)
  1631. {
  1632. const struct a2dp_remote_sep *sep = data;
  1633. const uint8_t *seid = user_data;
  1634. return avdtp_get_seid(sep->sep) == *seid;
  1635. }
  1636. static int match_sep(const void *data, const void *user_data)
  1637. {
  1638. struct a2dp_sep *sep = (void *) data;
  1639. const uint8_t *seid = user_data;
  1640. return *seid - avdtp_sep_get_seid(sep->lsep);
  1641. }
  1642. static struct a2dp_sep *find_sep_by_seid(struct a2dp_server *server,
  1643. uint8_t seid)
  1644. {
  1645. GSList *l;
  1646. l = g_slist_find_custom(server->sources, &seid, match_sep);
  1647. if (l)
  1648. return l->data;
  1649. l = g_slist_find_custom(server->sinks, &seid, match_sep);
  1650. if (l)
  1651. return l->data;
  1652. return NULL;
  1653. }
  1654. static void load_remote_sep(struct a2dp_channel *chan, GKeyFile *key_file,
  1655. char **seids)
  1656. {
  1657. struct a2dp_sep *lsep;
  1658. struct a2dp_remote_sep *sep;
  1659. struct avdtp_remote_sep *rsep;
  1660. uint8_t lseid, rseid;
  1661. char *value;
  1662. if (!seids)
  1663. return;
  1664. for (; *seids; seids++) {
  1665. uint8_t type;
  1666. uint8_t codec;
  1667. uint8_t delay_reporting;
  1668. GSList *l = NULL;
  1669. char caps[256];
  1670. uint8_t data[128];
  1671. int i, size;
  1672. if (sscanf(*seids, "%02hhx", &rseid) != 1)
  1673. continue;
  1674. value = g_key_file_get_string(key_file, "Endpoints", *seids,
  1675. NULL);
  1676. if (!value)
  1677. continue;
  1678. /* Try loading with delay_reporting first */
  1679. if (sscanf(value, "%02hhx:%02hhx:%02hhx:%s", &type, &codec,
  1680. &delay_reporting, caps) != 4) {
  1681. /* Try old format */
  1682. if (sscanf(value, "%02hhx:%02hhx:%s", &type, &codec,
  1683. caps) != 3) {
  1684. warn("Unable to load Endpoint: seid %u", rseid);
  1685. g_free(value);
  1686. continue;
  1687. }
  1688. delay_reporting = false;
  1689. }
  1690. for (i = 0, size = strlen(caps); i < size; i += 2) {
  1691. uint8_t *tmp = data + i / 2;
  1692. if (sscanf(caps + i, "%02hhx", tmp) != 1) {
  1693. warn("Unable to load Endpoint: seid %u", rseid);
  1694. break;
  1695. }
  1696. }
  1697. g_free(value);
  1698. if (i != size)
  1699. continue;
  1700. caps_add_codec(&l, codec, data, size / 2);
  1701. rsep = avdtp_register_remote_sep(chan->session, rseid, type, l,
  1702. delay_reporting);
  1703. if (!rsep) {
  1704. warn("Unable to register Endpoint: seid %u", rseid);
  1705. continue;
  1706. }
  1707. sep = register_remote_sep(rsep, chan);
  1708. if (sep)
  1709. sep->from_cache = true;
  1710. }
  1711. value = g_key_file_get_string(key_file, "Endpoints", "LastUsed", NULL);
  1712. if (!value)
  1713. return;
  1714. if (sscanf(value, "%02hhx:%02hhx", &lseid, &rseid) != 2) {
  1715. warn("Unable to load LastUsed");
  1716. g_free(value);
  1717. return;
  1718. }
  1719. g_free(value);
  1720. lsep = find_sep_by_seid(chan->server, lseid);
  1721. if (!lsep) {
  1722. warn("Unable to load LastUsed: lseid %u not found", lseid);
  1723. return;
  1724. }
  1725. sep = queue_find(chan->seps, match_seid, &rseid);
  1726. if (!sep) {
  1727. warn("Unable to load LastUsed: rseid %u not found", rseid);
  1728. return;
  1729. }
  1730. DBG("LastUsed: lseid %u rseid %u", lseid, rseid);
  1731. add_last_used(chan, lsep, sep);
  1732. }
  1733. static void load_remote_seps(struct a2dp_channel *chan)
  1734. {
  1735. struct btd_device *device = chan->device;
  1736. char filename[PATH_MAX];
  1737. char dst_addr[18];
  1738. char **keys;
  1739. GKeyFile *key_file;
  1740. ba2str(device_get_address(device), dst_addr);
  1741. snprintf(filename, PATH_MAX, STORAGEDIR "/%s/cache/%s",
  1742. btd_adapter_get_storage_dir(device_get_adapter(device)),
  1743. dst_addr);
  1744. key_file = g_key_file_new();
  1745. g_key_file_load_from_file(key_file, filename, 0, NULL);
  1746. keys = g_key_file_get_keys(key_file, "Endpoints", NULL, NULL);
  1747. load_remote_sep(chan, key_file, keys);
  1748. g_strfreev(keys);
  1749. g_key_file_free(key_file);
  1750. }
  1751. static void avdtp_state_cb(struct btd_device *dev, struct avdtp *session,
  1752. avdtp_session_state_t old_state,
  1753. avdtp_session_state_t new_state,
  1754. void *user_data)
  1755. {
  1756. struct a2dp_channel *chan = user_data;
  1757. switch (new_state) {
  1758. case AVDTP_SESSION_STATE_DISCONNECTED:
  1759. if (chan->session == session)
  1760. channel_remove(chan);
  1761. break;
  1762. case AVDTP_SESSION_STATE_CONNECTING:
  1763. break;
  1764. case AVDTP_SESSION_STATE_CONNECTED:
  1765. if (!chan->session)
  1766. chan->session = session;
  1767. load_remote_seps(chan);
  1768. break;
  1769. }
  1770. }
  1771. static struct a2dp_channel *channel_new(struct a2dp_server *server,
  1772. struct btd_device *device,
  1773. GIOChannel *io)
  1774. {
  1775. struct a2dp_channel *chan;
  1776. chan = g_new0(struct a2dp_channel, 1);
  1777. chan->server = server;
  1778. chan->device = device;
  1779. chan->seps = queue_new();
  1780. chan->state_id = avdtp_add_state_cb(device, avdtp_state_cb, chan);
  1781. if (!queue_push_tail(server->channels, chan)) {
  1782. g_free(chan);
  1783. return NULL;
  1784. }
  1785. if (!io)
  1786. return chan;
  1787. chan->io = g_io_channel_ref(io);
  1788. chan->io_id = g_io_add_watch(io, G_IO_ERR | G_IO_HUP | G_IO_NVAL,
  1789. (GIOFunc) disconnect_cb, chan);
  1790. return chan;
  1791. }
  1792. static bool match_by_device(const void *data, const void *user_data)
  1793. {
  1794. const struct a2dp_channel *chan = data;
  1795. const struct btd_device *device = user_data;
  1796. return chan->device == device;
  1797. }
  1798. struct avdtp *a2dp_avdtp_get(struct btd_device *device)
  1799. {
  1800. struct a2dp_server *server;
  1801. struct a2dp_channel *chan;
  1802. const struct queue_entry *entry;
  1803. server = find_server(servers, device_get_adapter(device));
  1804. if (server == NULL)
  1805. return NULL;
  1806. chan = queue_find(server->channels, match_by_device, device);
  1807. if (!chan) {
  1808. chan = channel_new(server, device, NULL);
  1809. if (!chan)
  1810. return NULL;
  1811. }
  1812. if (chan->session)
  1813. return avdtp_ref(chan->session);
  1814. /* Check if there is any SEP available */
  1815. for (entry = queue_get_entries(server->seps); entry;
  1816. entry = entry->next) {
  1817. struct avdtp_local_sep *sep = entry->data;
  1818. if (avdtp_sep_get_state(sep) == AVDTP_STATE_IDLE)
  1819. goto found;
  1820. }
  1821. DBG("Unable to find any available SEP");
  1822. return NULL;
  1823. found:
  1824. chan->session = avdtp_new(chan->io, device, server->seps);
  1825. if (!chan->session) {
  1826. channel_remove(chan);
  1827. return NULL;
  1828. }
  1829. return avdtp_ref(chan->session);
  1830. }
  1831. static void connect_cb(GIOChannel *io, GError *err, gpointer user_data)
  1832. {
  1833. struct a2dp_channel *chan = user_data;
  1834. if (err) {
  1835. error("%s", err->message);
  1836. goto fail;
  1837. }
  1838. if (!chan->session) {
  1839. chan->session = avdtp_new(chan->io, chan->device,
  1840. chan->server->seps);
  1841. if (!chan->session) {
  1842. error("Unable to create AVDTP session");
  1843. goto fail;
  1844. }
  1845. }
  1846. g_io_channel_unref(chan->io);
  1847. chan->io = NULL;
  1848. g_source_remove(chan->io_id);
  1849. chan->io_id = 0;
  1850. return;
  1851. fail:
  1852. channel_remove(chan);
  1853. }
  1854. static void auth_cb(DBusError *derr, void *user_data)
  1855. {
  1856. struct a2dp_channel *chan = user_data;
  1857. GError *err = NULL;
  1858. chan->auth_id = 0;
  1859. if (derr && dbus_error_is_set(derr)) {
  1860. error("Access denied: %s", derr->message);
  1861. goto fail;
  1862. }
  1863. if (!bt_io_accept(chan->io, connect_cb, chan, NULL, &err)) {
  1864. error("bt_io_accept: %s", err->message);
  1865. g_error_free(err);
  1866. goto fail;
  1867. }
  1868. return;
  1869. fail:
  1870. channel_remove(chan);
  1871. }
  1872. static void transport_cb(GIOChannel *io, GError *err, gpointer user_data)
  1873. {
  1874. struct a2dp_setup *setup = user_data;
  1875. uint16_t omtu, imtu;
  1876. if (!g_slist_find(setups, setup)) {
  1877. warn("bt_io_accept: setup %p no longer valid", setup);
  1878. g_io_channel_shutdown(io, TRUE, NULL);
  1879. return;
  1880. }
  1881. if (err) {
  1882. error("%s", err->message);
  1883. goto drop;
  1884. }
  1885. bt_io_get(io, &err, BT_IO_OPT_OMTU, &omtu, BT_IO_OPT_IMTU, &imtu,
  1886. BT_IO_OPT_INVALID);
  1887. if (err) {
  1888. error("%s", err->message);
  1889. g_error_free(err);
  1890. goto drop;
  1891. }
  1892. if (!avdtp_stream_set_transport(setup->stream,
  1893. g_io_channel_unix_get_fd(io),
  1894. imtu, omtu))
  1895. goto drop;
  1896. g_io_channel_set_close_on_unref(io, FALSE);
  1897. g_io_channel_unref(setup->io);
  1898. setup->io = NULL;
  1899. setup_unref(setup);
  1900. return;
  1901. drop:
  1902. setup_unref(setup);
  1903. g_io_channel_shutdown(io, TRUE, NULL);
  1904. }
  1905. static void confirm_cb(GIOChannel *io, gpointer data)
  1906. {
  1907. struct a2dp_server *server = data;
  1908. struct a2dp_channel *chan;
  1909. char address[18];
  1910. bdaddr_t src, dst;
  1911. GError *err = NULL;
  1912. struct btd_device *device;
  1913. bt_io_get(io, &err,
  1914. BT_IO_OPT_SOURCE_BDADDR, &src,
  1915. BT_IO_OPT_DEST_BDADDR, &dst,
  1916. BT_IO_OPT_DEST, address,
  1917. BT_IO_OPT_INVALID);
  1918. if (err) {
  1919. error("%s", err->message);
  1920. g_error_free(err);
  1921. goto drop;
  1922. }
  1923. DBG("AVDTP: incoming connect from %s", address);
  1924. device = btd_adapter_find_device(adapter_find(&src), &dst,
  1925. BDADDR_BREDR);
  1926. if (!device)
  1927. goto drop;
  1928. chan = queue_find(server->channels, match_by_device, device);
  1929. if (chan) {
  1930. struct a2dp_setup *setup;
  1931. setup = find_setup_by_session(chan->session);
  1932. if (!setup || !setup->stream)
  1933. goto drop;
  1934. if (setup->io) {
  1935. error("transport channel already exists");
  1936. goto drop;
  1937. }
  1938. if (!bt_io_accept(io, transport_cb, setup, NULL, &err)) {
  1939. error("bt_io_accept: %s", err->message);
  1940. g_error_free(err);
  1941. goto drop;
  1942. }
  1943. /*
  1944. * Reference the channel so it can be shutdown properly
  1945. * stopping bt_io_accept from calling the callback with invalid
  1946. * setup pointer.
  1947. */
  1948. setup->io = g_io_channel_ref(io);
  1949. return;
  1950. }
  1951. chan = channel_new(server, device, io);
  1952. if (!chan)
  1953. goto drop;
  1954. chan->auth_id = btd_request_authorization(&src, &dst,
  1955. ADVANCED_AUDIO_UUID,
  1956. auth_cb, chan);
  1957. if (chan->auth_id == 0 && !chan->session)
  1958. channel_remove(chan);
  1959. return;
  1960. drop:
  1961. g_io_channel_shutdown(io, TRUE, NULL);
  1962. }
  1963. static bool a2dp_server_listen(struct a2dp_server *server)
  1964. {
  1965. GError *err = NULL;
  1966. BtIOMode mode;
  1967. if (server->io)
  1968. return true;
  1969. mode = btd_opts.avdtp.session_mode;
  1970. server->io = bt_io_listen(NULL, confirm_cb, server, NULL, &err,
  1971. BT_IO_OPT_SOURCE_BDADDR,
  1972. btd_adapter_get_address(server->adapter),
  1973. BT_IO_OPT_PSM, AVDTP_PSM,
  1974. BT_IO_OPT_MODE, mode,
  1975. BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM,
  1976. BT_IO_OPT_CENTRAL, true,
  1977. BT_IO_OPT_INVALID);
  1978. if (server->io)
  1979. return true;
  1980. error("%s", err->message);
  1981. g_error_free(err);
  1982. return false;
  1983. }
  1984. static struct a2dp_server *a2dp_server_register(struct btd_adapter *adapter)
  1985. {
  1986. struct a2dp_server *server;
  1987. server = g_new0(struct a2dp_server, 1);
  1988. server->adapter = btd_adapter_ref(adapter);
  1989. server->seps = queue_new();
  1990. server->channels = queue_new();
  1991. servers = g_slist_append(servers, server);
  1992. return server;
  1993. }
  1994. static void a2dp_unregister_sep(struct a2dp_sep *sep)
  1995. {
  1996. struct a2dp_server *server = sep->server;
  1997. if (sep->destroy) {
  1998. sep->destroy(sep->user_data);
  1999. sep->endpoint = NULL;
  2000. }
  2001. avdtp_unregister_sep(server->seps, &server->seid_pool, sep->lsep);
  2002. g_free(sep);
  2003. if (!queue_isempty(server->seps))
  2004. return;
  2005. if (server->io) {
  2006. g_io_channel_shutdown(server->io, TRUE, NULL);
  2007. g_io_channel_unref(server->io);
  2008. server->io = NULL;
  2009. }
  2010. }
  2011. static void a2dp_server_unregister(struct a2dp_server *server)
  2012. {
  2013. servers = g_slist_remove(servers, server);
  2014. queue_destroy(server->channels, channel_free);
  2015. queue_destroy(server->seps, NULL);
  2016. if (server->io) {
  2017. g_io_channel_shutdown(server->io, TRUE, NULL);
  2018. g_io_channel_unref(server->io);
  2019. }
  2020. btd_adapter_unref(server->adapter);
  2021. g_free(server);
  2022. }
  2023. struct a2dp_sep *a2dp_add_sep(struct btd_adapter *adapter, uint8_t type,
  2024. uint8_t codec, gboolean delay_reporting,
  2025. struct a2dp_endpoint *endpoint,
  2026. void *user_data, GDestroyNotify destroy,
  2027. int *err)
  2028. {
  2029. struct a2dp_server *server;
  2030. struct a2dp_sep *sep;
  2031. GSList **l;
  2032. uint32_t *record_id;
  2033. sdp_record_t *record;
  2034. server = find_server(servers, adapter);
  2035. if (server == NULL) {
  2036. if (err)
  2037. *err = -EPROTONOSUPPORT;
  2038. return NULL;
  2039. }
  2040. if (type == AVDTP_SEP_TYPE_SINK && !server->sink_enabled) {
  2041. if (err)
  2042. *err = -EPROTONOSUPPORT;
  2043. return NULL;
  2044. }
  2045. if (type == AVDTP_SEP_TYPE_SOURCE && !server->source_enabled) {
  2046. if (err)
  2047. *err = -EPROTONOSUPPORT;
  2048. return NULL;
  2049. }
  2050. sep = g_new0(struct a2dp_sep, 1);
  2051. sep->lsep = avdtp_register_sep(server->seps, &server->seid_pool, type,
  2052. AVDTP_MEDIA_TYPE_AUDIO, codec,
  2053. delay_reporting, &endpoint_ind,
  2054. &cfm, sep);
  2055. if (sep->lsep == NULL) {
  2056. g_free(sep);
  2057. if (err)
  2058. *err = -EINVAL;
  2059. return NULL;
  2060. }
  2061. sep->server = server;
  2062. sep->endpoint = endpoint;
  2063. sep->codec = codec;
  2064. sep->type = type;
  2065. sep->delay_reporting = delay_reporting;
  2066. sep->user_data = user_data;
  2067. sep->destroy = destroy;
  2068. if (type == AVDTP_SEP_TYPE_SOURCE) {
  2069. l = &server->sources;
  2070. record_id = &server->source_record_id;
  2071. } else {
  2072. l = &server->sinks;
  2073. record_id = &server->sink_record_id;
  2074. }
  2075. if (*record_id != 0)
  2076. goto add;
  2077. record = a2dp_record(type);
  2078. if (!record) {
  2079. error("Unable to allocate new service record");
  2080. a2dp_unregister_sep(sep);
  2081. if (err)
  2082. *err = -EINVAL;
  2083. return NULL;
  2084. }
  2085. if (adapter_service_add(server->adapter, record) < 0) {
  2086. error("Unable to register A2DP service record");
  2087. sdp_record_free(record);
  2088. a2dp_unregister_sep(sep);
  2089. if (err)
  2090. *err = -EINVAL;
  2091. return NULL;
  2092. }
  2093. if (!a2dp_server_listen(server)) {
  2094. sdp_record_free(record);
  2095. a2dp_unregister_sep(sep);
  2096. if (err)
  2097. *err = -EINVAL;
  2098. return NULL;
  2099. }
  2100. *record_id = record->handle;
  2101. add:
  2102. *l = g_slist_append(*l, sep);
  2103. if (err)
  2104. *err = 0;
  2105. return sep;
  2106. }
  2107. void a2dp_remove_sep(struct a2dp_sep *sep)
  2108. {
  2109. struct a2dp_server *server = sep->server;
  2110. if (sep->type == AVDTP_SEP_TYPE_SOURCE) {
  2111. if (g_slist_find(server->sources, sep) == NULL)
  2112. return;
  2113. server->sources = g_slist_remove(server->sources, sep);
  2114. if (server->sources == NULL && server->source_record_id) {
  2115. adapter_service_remove(server->adapter,
  2116. server->source_record_id);
  2117. server->source_record_id = 0;
  2118. }
  2119. } else {
  2120. if (g_slist_find(server->sinks, sep) == NULL)
  2121. return;
  2122. server->sinks = g_slist_remove(server->sinks, sep);
  2123. if (server->sinks == NULL && server->sink_record_id) {
  2124. adapter_service_remove(server->adapter,
  2125. server->sink_record_id);
  2126. server->sink_record_id = 0;
  2127. }
  2128. }
  2129. if (sep->locked)
  2130. return;
  2131. a2dp_unregister_sep(sep);
  2132. }
  2133. static void select_cb(struct a2dp_setup *setup, void *ret, int size)
  2134. {
  2135. struct avdtp_service_capability *service;
  2136. struct avdtp_media_codec_capability *codec;
  2137. int err;
  2138. if (setup->err)
  2139. goto done;
  2140. if (size >= 0) {
  2141. caps_add_codec(&setup->caps, setup->sep->codec, ret, size);
  2142. goto done;
  2143. }
  2144. setup->sep = queue_pop_head(setup->eps);
  2145. if (!setup->sep) {
  2146. error("Unable to select a valid configuration");
  2147. goto done;
  2148. }
  2149. setup->rsep = find_remote_sep(setup->chan, setup->sep);
  2150. service = avdtp_get_codec(setup->rsep->sep);
  2151. codec = (struct avdtp_media_codec_capability *) service->data;
  2152. err = setup->sep->endpoint->select_configuration(setup->sep,
  2153. codec->data,
  2154. service->length - sizeof(*codec),
  2155. setup,
  2156. select_cb, setup->sep->user_data);
  2157. if (err == 0)
  2158. return;
  2159. done:
  2160. finalize_select(setup);
  2161. setup_unref(setup);
  2162. }
  2163. static struct queue *a2dp_find_eps(struct avdtp *session, GSList *list,
  2164. const char *sender)
  2165. {
  2166. struct a2dp_channel *chan = find_channel(session);
  2167. struct queue *seps = NULL;
  2168. for (; list; list = list->next) {
  2169. struct a2dp_sep *sep = list->data;
  2170. struct avdtp_remote_sep *rsep;
  2171. /* Use sender's endpoint if available */
  2172. if (sender) {
  2173. const char *name;
  2174. if (sep->endpoint == NULL)
  2175. continue;
  2176. name = sep->endpoint->get_name(sep, sep->user_data);
  2177. if (g_strcmp0(sender, name) != 0)
  2178. continue;
  2179. }
  2180. rsep = avdtp_find_remote_sep(session, sep->lsep);
  2181. if (!rsep)
  2182. continue;
  2183. if (!seps)
  2184. seps = queue_new();
  2185. /* Prepend last used so it is preferred over others */
  2186. if (chan->last_used && (chan->last_used->lsep == sep &&
  2187. chan->last_used->rsep->sep == rsep))
  2188. queue_push_head(seps, sep);
  2189. else
  2190. queue_push_tail(seps, sep);
  2191. }
  2192. return seps;
  2193. }
  2194. static struct queue *a2dp_select_eps(struct avdtp *session, uint8_t type,
  2195. const char *sender)
  2196. {
  2197. struct a2dp_server *server;
  2198. struct queue *seps;
  2199. GSList *l;
  2200. server = find_server(servers, avdtp_get_adapter(session));
  2201. if (!server)
  2202. return NULL;
  2203. l = type == AVDTP_SEP_TYPE_SINK ? server->sources : server->sinks;
  2204. /* Check sender's seps first */
  2205. seps = a2dp_find_eps(session, l, sender);
  2206. if (seps != NULL)
  2207. return seps;
  2208. return a2dp_find_eps(session, l, NULL);
  2209. }
  2210. static void foreach_register_remote_sep(void *data, void *user_data)
  2211. {
  2212. register_remote_sep(data, user_data);
  2213. }
  2214. static void discover_cb(struct avdtp *session, GSList *seps,
  2215. struct avdtp_error *err, void *user_data)
  2216. {
  2217. struct a2dp_setup *setup = user_data;
  2218. uint16_t version = avdtp_get_version(session);
  2219. DBG("version 0x%04x err %p", version, err);
  2220. setup->seps = seps;
  2221. if (err)
  2222. setup->err = err;
  2223. if (!err) {
  2224. g_slist_foreach(seps, foreach_register_remote_sep, setup->chan);
  2225. /* Only store version has been initialized as features like
  2226. * Delay Reporting may not be queried if the version in
  2227. * unknown.
  2228. */
  2229. if (version)
  2230. store_remote_seps(setup->chan);
  2231. }
  2232. finalize_discover(setup);
  2233. }
  2234. unsigned int a2dp_discover(struct avdtp *session, a2dp_discover_cb_t cb,
  2235. void *user_data)
  2236. {
  2237. struct a2dp_setup *setup;
  2238. struct a2dp_setup_cb *cb_data;
  2239. setup = a2dp_setup_get(session);
  2240. if (!setup)
  2241. return 0;
  2242. cb_data = setup_cb_new(setup);
  2243. cb_data->discover_cb = cb;
  2244. cb_data->user_data = user_data;
  2245. if (avdtp_discover(session, discover_cb, setup) == 0)
  2246. return cb_data->id;
  2247. setup_cb_free(cb_data);
  2248. return 0;
  2249. }
  2250. unsigned int a2dp_select_capabilities(struct avdtp *session,
  2251. uint8_t type, const char *sender,
  2252. a2dp_select_cb_t cb,
  2253. void *user_data)
  2254. {
  2255. struct a2dp_setup *setup;
  2256. struct a2dp_setup_cb *cb_data;
  2257. struct queue *eps;
  2258. struct avdtp_service_capability *service;
  2259. struct avdtp_media_codec_capability *codec;
  2260. int err;
  2261. eps = a2dp_select_eps(session, type, sender);
  2262. if (!eps) {
  2263. error("Unable to select SEP");
  2264. return 0;
  2265. }
  2266. setup = a2dp_setup_get(session);
  2267. if (!setup)
  2268. return 0;
  2269. cb_data = setup_cb_new(setup);
  2270. cb_data->select_cb = cb;
  2271. cb_data->user_data = user_data;
  2272. setup->eps = eps;
  2273. setup->sep = queue_pop_head(eps);
  2274. setup->rsep = find_remote_sep(setup->chan, setup->sep);
  2275. if (setup->rsep == NULL) {
  2276. error("Could not find remote sep");
  2277. goto fail;
  2278. }
  2279. service = avdtp_get_codec(setup->rsep->sep);
  2280. codec = (struct avdtp_media_codec_capability *) service->data;
  2281. err = setup->sep->endpoint->select_configuration(setup->sep,
  2282. codec->data,
  2283. service->length -
  2284. sizeof(*codec),
  2285. setup_ref(setup),
  2286. select_cb,
  2287. setup->sep->user_data);
  2288. if (err == 0)
  2289. return cb_data->id;
  2290. setup_unref(setup);
  2291. fail:
  2292. setup_cb_free(cb_data);
  2293. return 0;
  2294. }
  2295. unsigned int a2dp_config(struct avdtp *session, struct a2dp_sep *sep,
  2296. a2dp_config_cb_t cb, GSList *caps,
  2297. void *user_data)
  2298. {
  2299. struct a2dp_setup_cb *cb_data;
  2300. GSList *l;
  2301. struct a2dp_server *server;
  2302. struct a2dp_setup *setup;
  2303. struct a2dp_sep *tmp;
  2304. struct avdtp_service_capability *cap;
  2305. struct avdtp_media_codec_capability *codec_cap = NULL;
  2306. int posix_err;
  2307. server = find_server(servers, avdtp_get_adapter(session));
  2308. if (!server)
  2309. return 0;
  2310. for (l = caps; l != NULL; l = l->next) {
  2311. cap = l->data;
  2312. if (cap->category != AVDTP_MEDIA_CODEC)
  2313. continue;
  2314. codec_cap = (void *) cap->data;
  2315. break;
  2316. }
  2317. if (!codec_cap)
  2318. return 0;
  2319. if (sep->codec != codec_cap->media_codec_type)
  2320. return 0;
  2321. DBG("a2dp_config: selected SEP %p", sep->lsep);
  2322. setup = a2dp_setup_get(session);
  2323. if (!setup)
  2324. return 0;
  2325. cb_data = setup_cb_new(setup);
  2326. cb_data->config_cb = cb;
  2327. cb_data->user_data = user_data;
  2328. setup->sep = sep;
  2329. setup->stream = sep->stream;
  2330. /* Copy given caps if they are different than current caps */
  2331. if (setup->caps != caps) {
  2332. g_slist_free_full(setup->caps, g_free);
  2333. setup->caps = g_slist_copy(caps);
  2334. }
  2335. switch (avdtp_sep_get_state(sep->lsep)) {
  2336. case AVDTP_STATE_IDLE:
  2337. if (sep->type == AVDTP_SEP_TYPE_SOURCE)
  2338. l = server->sources;
  2339. else
  2340. l = server->sinks;
  2341. for (; l != NULL; l = l->next) {
  2342. tmp = l->data;
  2343. if (avdtp_has_stream(session, tmp->stream))
  2344. break;
  2345. }
  2346. if (l != NULL) {
  2347. if (tmp->locked)
  2348. goto failed;
  2349. setup->reconfigure = TRUE;
  2350. if (avdtp_close(session, tmp->stream, FALSE) < 0) {
  2351. error("avdtp_close failed");
  2352. goto failed;
  2353. }
  2354. break;
  2355. }
  2356. setup->rsep = find_remote_sep(setup->chan, sep);
  2357. if (setup->rsep == NULL) {
  2358. error("No matching ACP and INT SEPs found");
  2359. goto failed;
  2360. }
  2361. posix_err = avdtp_set_configuration(session, setup->rsep->sep,
  2362. sep->lsep, caps,
  2363. &setup->stream);
  2364. if (posix_err < 0) {
  2365. error("avdtp_set_configuration: %s",
  2366. strerror(-posix_err));
  2367. goto failed;
  2368. }
  2369. break;
  2370. case AVDTP_STATE_OPEN:
  2371. case AVDTP_STATE_STREAMING:
  2372. if (avdtp_stream_has_capabilities(setup->stream, caps)) {
  2373. DBG("Configuration match: resuming");
  2374. cb_data->source_id = g_idle_add(finalize_config,
  2375. setup);
  2376. } else if (!setup->reconfigure) {
  2377. setup->reconfigure = TRUE;
  2378. if (avdtp_close(session, sep->stream, FALSE) < 0) {
  2379. error("avdtp_close failed");
  2380. goto failed;
  2381. }
  2382. }
  2383. break;
  2384. case AVDTP_STATE_CONFIGURED:
  2385. case AVDTP_STATE_CLOSING:
  2386. case AVDTP_STATE_ABORTING:
  2387. default:
  2388. error("SEP in bad state for requesting a new stream");
  2389. goto failed;
  2390. }
  2391. return cb_data->id;
  2392. failed:
  2393. setup_cb_free(cb_data);
  2394. return 0;
  2395. }
  2396. unsigned int a2dp_resume(struct avdtp *session, struct a2dp_sep *sep,
  2397. a2dp_stream_cb_t cb, void *user_data)
  2398. {
  2399. struct a2dp_setup_cb *cb_data;
  2400. struct a2dp_setup *setup;
  2401. setup = a2dp_setup_get(session);
  2402. if (!setup)
  2403. return 0;
  2404. cb_data = setup_cb_new(setup);
  2405. cb_data->resume_cb = cb;
  2406. cb_data->user_data = user_data;
  2407. setup->sep = sep;
  2408. setup->stream = sep->stream;
  2409. switch (avdtp_sep_get_state(sep->lsep)) {
  2410. case AVDTP_STATE_IDLE:
  2411. goto failed;
  2412. break;
  2413. case AVDTP_STATE_CONFIGURED:
  2414. setup->start = TRUE;
  2415. break;
  2416. case AVDTP_STATE_OPEN:
  2417. if (avdtp_start(session, sep->stream) < 0) {
  2418. error("avdtp_start failed");
  2419. goto failed;
  2420. }
  2421. sep->starting = TRUE;
  2422. break;
  2423. case AVDTP_STATE_STREAMING:
  2424. if (!sep->suspending && sep->suspend_timer) {
  2425. timeout_remove(sep->suspend_timer);
  2426. sep->suspend_timer = 0;
  2427. avdtp_unref(sep->session);
  2428. sep->session = NULL;
  2429. }
  2430. if (sep->suspending)
  2431. setup->start = TRUE;
  2432. else
  2433. cb_data->source_id = g_idle_add(finalize_resume,
  2434. setup);
  2435. break;
  2436. case AVDTP_STATE_CLOSING:
  2437. case AVDTP_STATE_ABORTING:
  2438. default:
  2439. error("SEP in bad state for resume");
  2440. goto failed;
  2441. }
  2442. return cb_data->id;
  2443. failed:
  2444. setup_cb_free(cb_data);
  2445. return 0;
  2446. }
  2447. unsigned int a2dp_suspend(struct avdtp *session, struct a2dp_sep *sep,
  2448. a2dp_stream_cb_t cb, void *user_data)
  2449. {
  2450. struct a2dp_setup_cb *cb_data;
  2451. struct a2dp_setup *setup;
  2452. setup = a2dp_setup_get(session);
  2453. if (!setup)
  2454. return 0;
  2455. cb_data = setup_cb_new(setup);
  2456. cb_data->suspend_cb = cb;
  2457. cb_data->user_data = user_data;
  2458. setup->sep = sep;
  2459. setup->stream = sep->stream;
  2460. switch (avdtp_sep_get_state(sep->lsep)) {
  2461. case AVDTP_STATE_IDLE:
  2462. error("a2dp_suspend: no stream to suspend");
  2463. goto failed;
  2464. break;
  2465. case AVDTP_STATE_OPEN:
  2466. cb_data->source_id = g_idle_add(finalize_suspend, setup);
  2467. break;
  2468. case AVDTP_STATE_STREAMING:
  2469. if (avdtp_suspend(session, sep->stream) < 0) {
  2470. error("avdtp_suspend failed");
  2471. goto failed;
  2472. }
  2473. sep->suspending = TRUE;
  2474. break;
  2475. case AVDTP_STATE_CONFIGURED:
  2476. case AVDTP_STATE_CLOSING:
  2477. case AVDTP_STATE_ABORTING:
  2478. default:
  2479. error("SEP in bad state for suspend");
  2480. goto failed;
  2481. }
  2482. return cb_data->id;
  2483. failed:
  2484. setup_cb_free(cb_data);
  2485. return 0;
  2486. }
  2487. gboolean a2dp_cancel(unsigned int id)
  2488. {
  2489. GSList *ls;
  2490. for (ls = setups; ls != NULL; ls = g_slist_next(ls)) {
  2491. struct a2dp_setup *setup = ls->data;
  2492. GSList *l;
  2493. for (l = setup->cb; l != NULL; l = g_slist_next(l)) {
  2494. struct a2dp_setup_cb *cb = l->data;
  2495. if (cb->id != id)
  2496. continue;
  2497. setup_ref(setup);
  2498. setup_cb_free(cb);
  2499. if (!setup->cb) {
  2500. DBG("aborting setup %p", setup);
  2501. if (!avdtp_abort(setup->session, setup->stream))
  2502. return TRUE;
  2503. }
  2504. setup_unref(setup);
  2505. return TRUE;
  2506. }
  2507. }
  2508. return FALSE;
  2509. }
  2510. gboolean a2dp_sep_lock(struct a2dp_sep *sep, struct avdtp *session)
  2511. {
  2512. if (sep->locked)
  2513. return FALSE;
  2514. DBG("SEP %p locked", sep->lsep);
  2515. sep->locked = TRUE;
  2516. return TRUE;
  2517. }
  2518. gboolean a2dp_sep_unlock(struct a2dp_sep *sep, struct avdtp *session)
  2519. {
  2520. struct a2dp_server *server = sep->server;
  2521. avdtp_state_t state;
  2522. GSList *l;
  2523. state = avdtp_sep_get_state(sep->lsep);
  2524. sep->locked = FALSE;
  2525. DBG("SEP %p unlocked", sep->lsep);
  2526. if (sep->type == AVDTP_SEP_TYPE_SOURCE)
  2527. l = server->sources;
  2528. else
  2529. l = server->sinks;
  2530. /* Unregister sep if it was removed */
  2531. if (g_slist_find(l, sep) == NULL) {
  2532. a2dp_unregister_sep(sep);
  2533. return TRUE;
  2534. }
  2535. if (!sep->stream || state == AVDTP_STATE_IDLE)
  2536. return TRUE;
  2537. switch (state) {
  2538. case AVDTP_STATE_OPEN:
  2539. /* Set timer here */
  2540. break;
  2541. case AVDTP_STATE_STREAMING:
  2542. if (avdtp_suspend(session, sep->stream) == 0)
  2543. sep->suspending = TRUE;
  2544. break;
  2545. case AVDTP_STATE_IDLE:
  2546. case AVDTP_STATE_CONFIGURED:
  2547. case AVDTP_STATE_CLOSING:
  2548. case AVDTP_STATE_ABORTING:
  2549. default:
  2550. break;
  2551. }
  2552. return TRUE;
  2553. }
  2554. struct avdtp_stream *a2dp_sep_get_stream(struct a2dp_sep *sep)
  2555. {
  2556. return sep->stream;
  2557. }
  2558. struct btd_device *a2dp_setup_get_device(struct a2dp_setup *setup)
  2559. {
  2560. if (setup->session == NULL)
  2561. return NULL;
  2562. return avdtp_get_device(setup->session);
  2563. }
  2564. const char *a2dp_setup_remote_path(struct a2dp_setup *setup)
  2565. {
  2566. if (setup->rsep) {
  2567. return setup->rsep->path;
  2568. }
  2569. return NULL;
  2570. }
  2571. static int a2dp_source_probe(struct btd_service *service)
  2572. {
  2573. struct btd_device *dev = btd_service_get_device(service);
  2574. DBG("path %s", device_get_path(dev));
  2575. source_init(service);
  2576. return 0;
  2577. }
  2578. static void a2dp_source_remove(struct btd_service *service)
  2579. {
  2580. source_unregister(service);
  2581. }
  2582. static int a2dp_sink_probe(struct btd_service *service)
  2583. {
  2584. struct btd_device *dev = btd_service_get_device(service);
  2585. DBG("path %s", device_get_path(dev));
  2586. return sink_init(service);
  2587. }
  2588. static void a2dp_sink_remove(struct btd_service *service)
  2589. {
  2590. sink_unregister(service);
  2591. }
  2592. static int a2dp_source_connect(struct btd_service *service)
  2593. {
  2594. struct btd_device *dev = btd_service_get_device(service);
  2595. struct btd_adapter *adapter = device_get_adapter(dev);
  2596. struct a2dp_server *server;
  2597. const char *path = device_get_path(dev);
  2598. DBG("path %s", path);
  2599. server = find_server(servers, adapter);
  2600. if (!server || !server->sink_enabled) {
  2601. DBG("Unexpected error: cannot find server");
  2602. return -EPROTONOSUPPORT;
  2603. }
  2604. /* Return protocol not available if no record/endpoint exists */
  2605. if (server->sink_record_id == 0)
  2606. return -ENOPROTOOPT;
  2607. return source_connect(service);
  2608. }
  2609. static int a2dp_source_disconnect(struct btd_service *service)
  2610. {
  2611. struct btd_device *dev = btd_service_get_device(service);
  2612. const char *path = device_get_path(dev);
  2613. DBG("path %s", path);
  2614. return source_disconnect(service);
  2615. }
  2616. static int a2dp_sink_connect(struct btd_service *service)
  2617. {
  2618. struct btd_device *dev = btd_service_get_device(service);
  2619. struct btd_adapter *adapter = device_get_adapter(dev);
  2620. struct a2dp_server *server;
  2621. const char *path = device_get_path(dev);
  2622. DBG("path %s", path);
  2623. server = find_server(servers, adapter);
  2624. if (!server || !server->source_enabled) {
  2625. DBG("Unexpected error: cannot find server");
  2626. return -EPROTONOSUPPORT;
  2627. }
  2628. /* Return protocol not available if no record/endpoint exists */
  2629. if (server->source_record_id == 0)
  2630. return -ENOPROTOOPT;
  2631. return sink_connect(service);
  2632. }
  2633. static int a2dp_sink_disconnect(struct btd_service *service)
  2634. {
  2635. struct btd_device *dev = btd_service_get_device(service);
  2636. const char *path = device_get_path(dev);
  2637. DBG("path %s", path);
  2638. return sink_disconnect(service);
  2639. }
  2640. static int a2dp_source_server_probe(struct btd_profile *p,
  2641. struct btd_adapter *adapter)
  2642. {
  2643. struct a2dp_server *server;
  2644. DBG("path %s", adapter_get_path(adapter));
  2645. server = find_server(servers, adapter);
  2646. if (server != NULL)
  2647. goto done;
  2648. server = a2dp_server_register(adapter);
  2649. if (server == NULL)
  2650. return -EPROTONOSUPPORT;
  2651. done:
  2652. server->source_enabled = TRUE;
  2653. return 0;
  2654. }
  2655. static void a2dp_source_server_remove(struct btd_profile *p,
  2656. struct btd_adapter *adapter)
  2657. {
  2658. struct a2dp_server *server;
  2659. DBG("path %s", adapter_get_path(adapter));
  2660. server = find_server(servers, adapter);
  2661. if (!server)
  2662. return;
  2663. g_slist_free_full(server->sources,
  2664. (GDestroyNotify) a2dp_unregister_sep);
  2665. if (server->source_record_id) {
  2666. adapter_service_remove(server->adapter,
  2667. server->source_record_id);
  2668. server->source_record_id = 0;
  2669. }
  2670. if (server->sink_record_id)
  2671. return;
  2672. a2dp_server_unregister(server);
  2673. }
  2674. static int a2dp_sink_server_probe(struct btd_profile *p,
  2675. struct btd_adapter *adapter)
  2676. {
  2677. struct a2dp_server *server;
  2678. DBG("path %s", adapter_get_path(adapter));
  2679. server = find_server(servers, adapter);
  2680. if (server != NULL)
  2681. goto done;
  2682. server = a2dp_server_register(adapter);
  2683. if (server == NULL)
  2684. return -EPROTONOSUPPORT;
  2685. done:
  2686. server->sink_enabled = TRUE;
  2687. return 0;
  2688. }
  2689. static void a2dp_sink_server_remove(struct btd_profile *p,
  2690. struct btd_adapter *adapter)
  2691. {
  2692. struct a2dp_server *server;
  2693. DBG("path %s", adapter_get_path(adapter));
  2694. server = find_server(servers, adapter);
  2695. if (!server)
  2696. return;
  2697. g_slist_free_full(server->sinks, (GDestroyNotify) a2dp_unregister_sep);
  2698. if (server->sink_record_id) {
  2699. adapter_service_remove(server->adapter, server->sink_record_id);
  2700. server->sink_record_id = 0;
  2701. }
  2702. if (server->source_record_id)
  2703. return;
  2704. a2dp_server_unregister(server);
  2705. }
  2706. static int media_server_probe(struct btd_adapter *adapter)
  2707. {
  2708. DBG("path %s", adapter_get_path(adapter));
  2709. return media_register(adapter);
  2710. }
  2711. static void media_server_remove(struct btd_adapter *adapter)
  2712. {
  2713. DBG("path %s", adapter_get_path(adapter));
  2714. media_unregister(adapter);
  2715. }
  2716. static struct btd_profile a2dp_source_profile = {
  2717. .name = "a2dp-source",
  2718. .priority = BTD_PROFILE_PRIORITY_MEDIUM,
  2719. .remote_uuid = A2DP_SOURCE_UUID,
  2720. .device_probe = a2dp_source_probe,
  2721. .device_remove = a2dp_source_remove,
  2722. .auto_connect = true,
  2723. .connect = a2dp_source_connect,
  2724. .disconnect = a2dp_source_disconnect,
  2725. .adapter_probe = a2dp_sink_server_probe,
  2726. .adapter_remove = a2dp_sink_server_remove,
  2727. };
  2728. static struct btd_profile a2dp_sink_profile = {
  2729. .name = "a2dp-sink",
  2730. .priority = BTD_PROFILE_PRIORITY_MEDIUM,
  2731. .remote_uuid = A2DP_SINK_UUID,
  2732. .device_probe = a2dp_sink_probe,
  2733. .device_remove = a2dp_sink_remove,
  2734. .auto_connect = true,
  2735. .connect = a2dp_sink_connect,
  2736. .disconnect = a2dp_sink_disconnect,
  2737. .adapter_probe = a2dp_source_server_probe,
  2738. .adapter_remove = a2dp_source_server_remove,
  2739. };
  2740. static struct btd_adapter_driver media_driver = {
  2741. .name = "media",
  2742. .probe = media_server_probe,
  2743. .remove = media_server_remove,
  2744. };
  2745. static int a2dp_init(void)
  2746. {
  2747. btd_register_adapter_driver(&media_driver);
  2748. btd_profile_register(&a2dp_source_profile);
  2749. btd_profile_register(&a2dp_sink_profile);
  2750. return 0;
  2751. }
  2752. static void a2dp_exit(void)
  2753. {
  2754. btd_unregister_adapter_driver(&media_driver);
  2755. btd_profile_unregister(&a2dp_source_profile);
  2756. btd_profile_unregister(&a2dp_sink_profile);
  2757. }
  2758. BLUETOOTH_PLUGIN_DEFINE(a2dp, VERSION, BLUETOOTH_PLUGIN_PRIORITY_DEFAULT,
  2759. a2dp_init, a2dp_exit)