avctp.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640
  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 Texas Instruments, Inc.
  9. *
  10. *
  11. */
  12. #ifdef HAVE_CONFIG_H
  13. #include <config.h>
  14. #endif
  15. #include <stdlib.h>
  16. #include <stdint.h>
  17. #include <stdbool.h>
  18. #include <errno.h>
  19. #include <unistd.h>
  20. #include <assert.h>
  21. #include <signal.h>
  22. #include <string.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <fcntl.h>
  26. #include <netinet/in.h>
  27. #include <glib.h>
  28. #include "lib/sdp.h"
  29. #include "src/log.h"
  30. #include "src/uinput.h"
  31. #include "avctp.h"
  32. /*
  33. * AV/C Panel 1.23, page 76:
  34. * command with the pressed value is valid for two seconds
  35. */
  36. #define AVC_PRESS_TIMEOUT 2
  37. #define QUIRK_NO_RELEASE 1 << 0
  38. /* Message types */
  39. #define AVCTP_COMMAND 0
  40. #define AVCTP_RESPONSE 1
  41. /* Packet types */
  42. #define AVCTP_PACKET_SINGLE 0
  43. #define AVCTP_PACKET_START 1
  44. #define AVCTP_PACKET_CONTINUE 2
  45. #define AVCTP_PACKET_END 3
  46. #if __BYTE_ORDER == __LITTLE_ENDIAN
  47. struct avctp_header {
  48. uint8_t ipid:1;
  49. uint8_t cr:1;
  50. uint8_t packet_type:2;
  51. uint8_t transaction:4;
  52. uint16_t pid;
  53. } __attribute__ ((packed));
  54. struct avc_header {
  55. uint8_t code:4;
  56. uint8_t _hdr0:4;
  57. uint8_t subunit_id:3;
  58. uint8_t subunit_type:5;
  59. uint8_t opcode;
  60. } __attribute__ ((packed));
  61. #elif __BYTE_ORDER == __BIG_ENDIAN
  62. struct avctp_header {
  63. uint8_t transaction:4;
  64. uint8_t packet_type:2;
  65. uint8_t cr:1;
  66. uint8_t ipid:1;
  67. uint16_t pid;
  68. } __attribute__ ((packed));
  69. struct avc_header {
  70. uint8_t _hdr0:4;
  71. uint8_t code:4;
  72. uint8_t subunit_type:5;
  73. uint8_t subunit_id:3;
  74. uint8_t opcode;
  75. } __attribute__ ((packed));
  76. #else
  77. #error "Unknown byte order"
  78. #endif
  79. struct avctp_control_req {
  80. struct avctp_pending_req *p;
  81. uint8_t code;
  82. uint8_t subunit;
  83. uint8_t op;
  84. struct iovec *iov;
  85. int iov_cnt;
  86. avctp_rsp_cb func;
  87. void *user_data;
  88. };
  89. struct avctp_browsing_req {
  90. struct avctp_pending_req *p;
  91. struct iovec *iov;
  92. int iov_cnt;
  93. avctp_browsing_rsp_cb func;
  94. void *user_data;
  95. };
  96. typedef int (*avctp_process_cb) (void *data);
  97. struct avctp_pending_req {
  98. struct avctp_channel *chan;
  99. uint8_t transaction;
  100. guint timeout;
  101. int err;
  102. avctp_process_cb process;
  103. void *data;
  104. avctp_destroy_cb_t destroy;
  105. };
  106. struct avctp_channel {
  107. struct avctp *session;
  108. GIOChannel *io;
  109. uint8_t transaction;
  110. guint watch;
  111. uint16_t imtu;
  112. uint16_t omtu;
  113. uint8_t *buffer;
  114. GSList *handlers;
  115. struct avctp_pending_req *p;
  116. GQueue *queue;
  117. GSList *processed;
  118. guint process_id;
  119. avctp_destroy_cb_t destroy;
  120. };
  121. struct key_pressed {
  122. uint8_t op;
  123. uint8_t *params;
  124. size_t params_len;
  125. guint timer;
  126. };
  127. struct avctp {
  128. unsigned int ref;
  129. int uinput;
  130. unsigned int passthrough_id;
  131. unsigned int unit_id;
  132. unsigned int subunit_id;
  133. struct avctp_channel *control;
  134. struct avctp_channel *browsing;
  135. struct avctp_passthrough_handler *handler;
  136. uint8_t key_quirks[256];
  137. struct key_pressed key;
  138. uint16_t version;
  139. avctp_destroy_cb_t destroy;
  140. void *data;
  141. };
  142. struct avctp_passthrough_handler {
  143. avctp_passthrough_cb cb;
  144. void *user_data;
  145. unsigned int id;
  146. };
  147. struct avctp_pdu_handler {
  148. uint8_t opcode;
  149. avctp_control_pdu_cb cb;
  150. void *user_data;
  151. unsigned int id;
  152. };
  153. struct avctp_browsing_pdu_handler {
  154. avctp_browsing_pdu_cb cb;
  155. void *user_data;
  156. unsigned int id;
  157. avctp_destroy_cb_t destroy;
  158. };
  159. static struct {
  160. const char *name;
  161. uint8_t avc;
  162. uint16_t uinput;
  163. } key_map[] = {
  164. { "SELECT", AVC_SELECT, KEY_SELECT },
  165. { "UP", AVC_UP, KEY_UP },
  166. { "DOWN", AVC_DOWN, KEY_DOWN },
  167. { "LEFT", AVC_LEFT, KEY_LEFT },
  168. { "RIGHT", AVC_RIGHT, KEY_RIGHT },
  169. { "ROOT MENU", AVC_ROOT_MENU, KEY_MENU },
  170. { "CONTENTS MENU", AVC_CONTENTS_MENU, KEY_PROGRAM },
  171. { "FAVORITE MENU", AVC_FAVORITE_MENU, KEY_FAVORITES },
  172. { "EXIT", AVC_EXIT, KEY_EXIT },
  173. { "ON DEMAND MENU", AVC_ON_DEMAND_MENU, KEY_MENU },
  174. { "APPS MENU", AVC_APPS_MENU, KEY_MENU },
  175. { "0", AVC_0, KEY_0 },
  176. { "1", AVC_1, KEY_1 },
  177. { "2", AVC_2, KEY_2 },
  178. { "3", AVC_3, KEY_3 },
  179. { "4", AVC_4, KEY_4 },
  180. { "5", AVC_5, KEY_5 },
  181. { "6", AVC_6, KEY_6 },
  182. { "7", AVC_7, KEY_7 },
  183. { "8", AVC_8, KEY_8 },
  184. { "9", AVC_9, KEY_9 },
  185. { "DOT", AVC_DOT, KEY_DOT },
  186. { "ENTER", AVC_ENTER, KEY_ENTER },
  187. { "CHANNEL UP", AVC_CHANNEL_UP, KEY_CHANNELUP },
  188. { "CHANNEL DOWN", AVC_CHANNEL_DOWN, KEY_CHANNELDOWN },
  189. { "CHANNEL PREVIOUS", AVC_CHANNEL_PREVIOUS, KEY_LAST },
  190. { "INPUT SELECT", AVC_INPUT_SELECT, KEY_CONFIG },
  191. { "INFO", AVC_INFO, KEY_INFO },
  192. { "HELP", AVC_HELP, KEY_HELP },
  193. { "POWER", AVC_POWER, KEY_POWER2 },
  194. { "VOLUME UP", AVC_VOLUME_UP, KEY_VOLUMEUP },
  195. { "VOLUME DOWN", AVC_VOLUME_DOWN, KEY_VOLUMEDOWN },
  196. { "MUTE", AVC_MUTE, KEY_MUTE },
  197. { "PLAY", AVC_PLAY, KEY_PLAYCD },
  198. { "STOP", AVC_STOP, KEY_STOPCD },
  199. { "PAUSE", AVC_PAUSE, KEY_PAUSECD },
  200. { "FORWARD", AVC_FORWARD, KEY_NEXTSONG },
  201. { "BACKWARD", AVC_BACKWARD, KEY_PREVIOUSSONG },
  202. { "RECORD", AVC_RECORD, KEY_RECORD },
  203. { "REWIND", AVC_REWIND, KEY_REWIND },
  204. { "FAST FORWARD", AVC_FAST_FORWARD, KEY_FASTFORWARD },
  205. { "LIST", AVC_LIST, KEY_LIST },
  206. { "F1", AVC_F1, KEY_F1 },
  207. { "F2", AVC_F2, KEY_F2 },
  208. { "F3", AVC_F3, KEY_F3 },
  209. { "F4", AVC_F4, KEY_F4 },
  210. { "F5", AVC_F5, KEY_F5 },
  211. { "F6", AVC_F6, KEY_F6 },
  212. { "F7", AVC_F7, KEY_F7 },
  213. { "F8", AVC_F8, KEY_F8 },
  214. { "F9", AVC_F9, KEY_F9 },
  215. { "RED", AVC_RED, KEY_RED },
  216. { "GREEN", AVC_GREEN, KEY_GREEN },
  217. { "BLUE", AVC_BLUE, KEY_BLUE },
  218. { "YELLOW", AVC_YELLOW, KEY_YELLOW },
  219. { NULL }
  220. };
  221. static gboolean process_queue(gpointer user_data);
  222. static gboolean avctp_passthrough_rsp(struct avctp *session, uint8_t code,
  223. uint8_t subunit, uint8_t *operands,
  224. size_t operand_count, void *user_data);
  225. static int send_event(int fd, uint16_t type, uint16_t code, int32_t value)
  226. {
  227. struct uinput_event event;
  228. int err;
  229. memset(&event, 0, sizeof(event));
  230. event.type = type;
  231. event.code = code;
  232. event.value = value;
  233. do {
  234. err = write(fd, &event, sizeof(event));
  235. } while (err < 0 && errno == EINTR);
  236. if (err < 0) {
  237. err = -errno;
  238. error("send_event: %s (%d)", strerror(-err), -err);
  239. }
  240. return err;
  241. }
  242. static void send_key(int fd, uint16_t key, int pressed)
  243. {
  244. send_event(fd, EV_KEY, key, pressed);
  245. send_event(fd, EV_SYN, SYN_REPORT, 0);
  246. }
  247. static gboolean auto_release(gpointer user_data)
  248. {
  249. struct avctp *session = user_data;
  250. session->key.timer = 0;
  251. DBG("AV/C: key press timeout");
  252. send_key(session->uinput, session->key.op, 0);
  253. return FALSE;
  254. }
  255. static ssize_t handle_panel_passthrough(struct avctp *session,
  256. uint8_t transaction, uint8_t *code,
  257. uint8_t *subunit, uint8_t *operands,
  258. size_t operand_count, void *user_data)
  259. {
  260. struct avctp_passthrough_handler *handler = session->handler;
  261. const char *status;
  262. int pressed, i;
  263. if (*code != AVC_CTYPE_CONTROL || *subunit != AVC_SUBUNIT_PANEL) {
  264. *code = AVC_CTYPE_REJECTED;
  265. return operand_count;
  266. }
  267. if (operand_count == 0)
  268. goto done;
  269. if (operands[0] & 0x80) {
  270. status = "released";
  271. pressed = 0;
  272. } else {
  273. status = "pressed";
  274. pressed = 1;
  275. }
  276. if (session->key.timer == 0 && handler != NULL) {
  277. if (handler->cb(session, operands[0] & 0x7F,
  278. pressed, handler->user_data))
  279. goto done;
  280. }
  281. if (session->uinput < 0) {
  282. DBG("AV/C: uinput not initialized");
  283. *code = AVC_CTYPE_NOT_IMPLEMENTED;
  284. return 0;
  285. }
  286. for (i = 0; key_map[i].name != NULL; i++) {
  287. uint8_t key_quirks;
  288. if ((operands[0] & 0x7F) != key_map[i].avc)
  289. continue;
  290. DBG("AV/C: %s %s", key_map[i].name, status);
  291. key_quirks = session->key_quirks[key_map[i].avc];
  292. if (key_quirks & QUIRK_NO_RELEASE) {
  293. if (!pressed) {
  294. DBG("AV/C: Ignoring release");
  295. break;
  296. }
  297. DBG("AV/C: treating key press as press + release");
  298. send_key(session->uinput, key_map[i].uinput, 1);
  299. send_key(session->uinput, key_map[i].uinput, 0);
  300. break;
  301. }
  302. if (pressed) {
  303. if (session->key.timer > 0) {
  304. g_source_remove(session->key.timer);
  305. send_key(session->uinput, session->key.op, 0);
  306. }
  307. session->key.op = key_map[i].uinput;
  308. session->key.timer = g_timeout_add_seconds(
  309. AVC_PRESS_TIMEOUT,
  310. auto_release,
  311. session);
  312. } else if (session->key.timer > 0) {
  313. g_source_remove(session->key.timer);
  314. session->key.timer = 0;
  315. }
  316. send_key(session->uinput, key_map[i].uinput, pressed);
  317. break;
  318. }
  319. if (key_map[i].name == NULL) {
  320. DBG("AV/C: unknown button 0x%02X %s",
  321. operands[0] & 0x7F, status);
  322. *code = AVC_CTYPE_NOT_IMPLEMENTED;
  323. return operand_count;
  324. }
  325. done:
  326. *code = AVC_CTYPE_ACCEPTED;
  327. return operand_count;
  328. }
  329. static ssize_t handle_unit_info(struct avctp *session,
  330. uint8_t transaction, uint8_t *code,
  331. uint8_t *subunit, uint8_t *operands,
  332. size_t operand_count, void *user_data)
  333. {
  334. if (*code != AVC_CTYPE_STATUS) {
  335. *code = AVC_CTYPE_REJECTED;
  336. return 0;
  337. }
  338. *code = AVC_CTYPE_STABLE;
  339. /*
  340. * The first operand should be 0x07 for the UNITINFO response.
  341. * Neither AVRCP (section 22.1, page 117) nor AVC Digital
  342. * Interface Command Set (section 9.2.1, page 45) specs
  343. * explain this value but both use it
  344. */
  345. if (operand_count >= 1)
  346. operands[0] = 0x07;
  347. if (operand_count >= 2)
  348. operands[1] = AVC_SUBUNIT_PANEL << 3;
  349. DBG("reply to AVC_OP_UNITINFO");
  350. return operand_count;
  351. }
  352. static ssize_t handle_subunit_info(struct avctp *session,
  353. uint8_t transaction, uint8_t *code,
  354. uint8_t *subunit, uint8_t *operands,
  355. size_t operand_count, void *user_data)
  356. {
  357. if (*code != AVC_CTYPE_STATUS) {
  358. *code = AVC_CTYPE_REJECTED;
  359. return 0;
  360. }
  361. *code = AVC_CTYPE_STABLE;
  362. /*
  363. * The first operand should be 0x07 for the UNITINFO response.
  364. * Neither AVRCP (section 22.1, page 117) nor AVC Digital
  365. * Interface Command Set (section 9.2.1, page 45) specs
  366. * explain this value but both use it
  367. */
  368. if (operand_count >= 2)
  369. operands[1] = AVC_SUBUNIT_PANEL << 3;
  370. DBG("reply to AVC_OP_SUBUNITINFO");
  371. return operand_count;
  372. }
  373. static struct avctp_pdu_handler *find_handler(GSList *list, uint8_t opcode)
  374. {
  375. for (; list; list = list->next) {
  376. struct avctp_pdu_handler *handler = list->data;
  377. if (handler->opcode == opcode)
  378. return handler;
  379. }
  380. return NULL;
  381. }
  382. static void pending_destroy(gpointer data, gpointer user_data)
  383. {
  384. struct avctp_pending_req *req = data;
  385. if (req->destroy)
  386. req->destroy(req->data);
  387. if (req->timeout > 0)
  388. g_source_remove(req->timeout);
  389. g_free(req);
  390. }
  391. static void avctp_channel_destroy(struct avctp_channel *chan)
  392. {
  393. g_io_channel_shutdown(chan->io, TRUE, NULL);
  394. g_io_channel_unref(chan->io);
  395. if (chan->watch)
  396. g_source_remove(chan->watch);
  397. if (chan->p)
  398. pending_destroy(chan->p, NULL);
  399. if (chan->process_id > 0)
  400. g_source_remove(chan->process_id);
  401. if (chan->destroy)
  402. chan->destroy(chan);
  403. g_free(chan->buffer);
  404. g_queue_foreach(chan->queue, pending_destroy, NULL);
  405. g_queue_free(chan->queue);
  406. g_slist_foreach(chan->processed, pending_destroy, NULL);
  407. g_slist_free(chan->processed);
  408. g_slist_free_full(chan->handlers, g_free);
  409. g_free(chan);
  410. }
  411. static int avctp_send(struct avctp_channel *control, uint8_t transaction,
  412. uint8_t cr, uint8_t code,
  413. uint8_t subunit, uint8_t opcode,
  414. const struct iovec *iov, int iov_cnt)
  415. {
  416. struct avctp_header avctp;
  417. struct avc_header avc;
  418. struct msghdr msg;
  419. int sk, err = 0;
  420. struct iovec pdu[iov_cnt + 2];
  421. int i;
  422. size_t len = sizeof(avctp) + sizeof(avc);
  423. DBG("");
  424. pdu[0].iov_base = &avctp;
  425. pdu[0].iov_len = sizeof(avctp);
  426. pdu[1].iov_base = &avc;
  427. pdu[1].iov_len = sizeof(avc);
  428. for (i = 0; i < iov_cnt; i++) {
  429. pdu[i + 2].iov_base = iov[i].iov_base;
  430. pdu[i + 2].iov_len = iov[i].iov_len;
  431. len += iov[i].iov_len;
  432. }
  433. if (control->omtu < len)
  434. return -EOVERFLOW;
  435. sk = g_io_channel_unix_get_fd(control->io);
  436. memset(&avctp, 0, sizeof(avctp));
  437. avctp.transaction = transaction;
  438. avctp.packet_type = AVCTP_PACKET_SINGLE;
  439. avctp.cr = cr;
  440. avctp.pid = htons(AV_REMOTE_SVCLASS_ID);
  441. memset(&avc, 0, sizeof(avc));
  442. avc.code = code;
  443. avc.subunit_type = subunit;
  444. avc.opcode = opcode;
  445. memset(&msg, 0, sizeof(msg));
  446. msg.msg_iov = pdu;
  447. msg.msg_iovlen = iov_cnt + 2;
  448. if (sendmsg(sk, &msg, 0) < 0)
  449. err = -errno;
  450. return err;
  451. }
  452. static int avctp_browsing_send(struct avctp_channel *browsing,
  453. uint8_t transaction, uint8_t cr,
  454. const struct iovec *iov, int iov_cnt)
  455. {
  456. struct avctp_header avctp;
  457. struct msghdr msg;
  458. struct iovec pdu[iov_cnt + 1];
  459. int sk, err = 0;
  460. int i;
  461. size_t len = sizeof(avctp);
  462. for (i = 0; i < iov_cnt; i++) {
  463. pdu[i + 1].iov_base = iov[i].iov_base;
  464. pdu[i + 1].iov_len = iov[i].iov_len;
  465. len += iov[i].iov_len;
  466. }
  467. pdu[0].iov_base = &avctp;
  468. pdu[0].iov_len = sizeof(avctp);
  469. if (browsing->omtu < len)
  470. return -EOVERFLOW;
  471. sk = g_io_channel_unix_get_fd(browsing->io);
  472. memset(&avctp, 0, sizeof(avctp));
  473. avctp.transaction = transaction;
  474. avctp.packet_type = AVCTP_PACKET_SINGLE;
  475. avctp.cr = cr;
  476. avctp.pid = htons(AV_REMOTE_SVCLASS_ID);
  477. memset(&msg, 0, sizeof(msg));
  478. msg.msg_iov = pdu;
  479. msg.msg_iovlen = iov_cnt + 1;
  480. if (sendmsg(sk, &msg, 0) < 0)
  481. err = -errno;
  482. return err;
  483. }
  484. static void control_req_destroy(void *data)
  485. {
  486. struct avctp_control_req *req = data;
  487. struct avctp_pending_req *p = req->p;
  488. struct avctp *session = p->chan->session;
  489. int i;
  490. if (p->err == 0 || req->func == NULL)
  491. goto done;
  492. req->func(session, AVC_CTYPE_REJECTED, req->subunit, NULL, 0,
  493. req->user_data);
  494. done:
  495. for (i = 0; i < req->iov_cnt; i++)
  496. g_free(req->iov[i].iov_base);
  497. g_free(req->iov);
  498. g_free(req);
  499. }
  500. static void browsing_req_destroy(void *data)
  501. {
  502. struct avctp_browsing_req *req = data;
  503. struct avctp_pending_req *p = req->p;
  504. struct avctp *session = p->chan->session;
  505. int i;
  506. if (p->err == 0 || req->func == NULL)
  507. goto done;
  508. req->func(session, NULL, 0, req->user_data);
  509. done:
  510. for (i = 0; i < req->iov_cnt; i++)
  511. g_free(req->iov[i].iov_base);
  512. g_free(req->iov);
  513. g_free(req);
  514. }
  515. static gboolean req_timeout(gpointer user_data)
  516. {
  517. struct avctp_channel *chan = user_data;
  518. struct avctp_pending_req *p = chan->p;
  519. DBG("transaction %u", p->transaction);
  520. p->timeout = 0;
  521. p->err = -ETIMEDOUT;
  522. pending_destroy(p, NULL);
  523. chan->p = NULL;
  524. if (chan->process_id == 0)
  525. chan->process_id = g_idle_add(process_queue, chan);
  526. return FALSE;
  527. }
  528. static int process_control(void *data)
  529. {
  530. struct avctp_control_req *req = data;
  531. struct avctp_pending_req *p = req->p;
  532. return avctp_send(p->chan, p->transaction, AVCTP_COMMAND, req->code,
  533. req->subunit, req->op, req->iov, req->iov_cnt);
  534. }
  535. static int process_browsing(void *data)
  536. {
  537. struct avctp_browsing_req *req = data;
  538. struct avctp_pending_req *p = req->p;
  539. return avctp_browsing_send(p->chan, p->transaction, AVCTP_COMMAND,
  540. req->iov, req->iov_cnt);
  541. }
  542. static gboolean process_queue(void *user_data)
  543. {
  544. struct avctp_channel *chan = user_data;
  545. struct avctp_pending_req *p = chan->p;
  546. chan->process_id = 0;
  547. if (p != NULL)
  548. return FALSE;
  549. while ((p = g_queue_pop_head(chan->queue))) {
  550. if (p->process(p->data) == 0)
  551. break;
  552. pending_destroy(p, NULL);
  553. }
  554. if (p == NULL)
  555. return FALSE;
  556. chan->p = p;
  557. p->timeout = g_timeout_add_seconds(2, req_timeout, chan);
  558. return FALSE;
  559. }
  560. static struct avctp *avctp_ref(struct avctp *session)
  561. {
  562. __sync_fetch_and_add(&session->ref, 1);
  563. DBG("%p: ref=%d", session, session->ref);
  564. return session;
  565. }
  566. static void avctp_unref(struct avctp *session)
  567. {
  568. DBG("%p: ref=%d", session, session->ref);
  569. if (__sync_sub_and_fetch(&session->ref, 1))
  570. return;
  571. if (session->browsing)
  572. avctp_channel_destroy(session->browsing);
  573. if (session->control)
  574. avctp_channel_destroy(session->control);
  575. if (session->destroy)
  576. session->destroy(session->data);
  577. g_free(session->handler);
  578. if (session->key.timer > 0)
  579. g_source_remove(session->key.timer);
  580. if (session->uinput >= 0) {
  581. DBG("AVCTP: closing uinput");
  582. ioctl(session->uinput, UI_DEV_DESTROY);
  583. close(session->uinput);
  584. session->uinput = -1;
  585. }
  586. g_free(session);
  587. }
  588. static void control_response(struct avctp_channel *control,
  589. struct avctp_header *avctp,
  590. struct avc_header *avc,
  591. uint8_t *operands,
  592. size_t operand_count)
  593. {
  594. struct avctp_pending_req *p = control->p;
  595. struct avctp_control_req *req;
  596. GSList *l;
  597. if (p && p->transaction == avctp->transaction) {
  598. control->processed = g_slist_prepend(control->processed, p);
  599. if (p->timeout > 0) {
  600. g_source_remove(p->timeout);
  601. p->timeout = 0;
  602. }
  603. control->p = NULL;
  604. if (control->process_id == 0)
  605. control->process_id = g_idle_add(process_queue,
  606. control);
  607. }
  608. avctp_ref(control->session);
  609. for (l = control->processed; l; l = l->next) {
  610. p = l->data;
  611. req = p->data;
  612. if (p->transaction != avctp->transaction)
  613. continue;
  614. if (req->func && req->func(control->session, avc->code,
  615. avc->subunit_type,
  616. operands, operand_count,
  617. req->user_data))
  618. break;
  619. control->processed = g_slist_remove(control->processed, p);
  620. pending_destroy(p, NULL);
  621. break;
  622. }
  623. avctp_unref(control->session);
  624. }
  625. static void browsing_response(struct avctp_channel *browsing,
  626. struct avctp_header *avctp,
  627. uint8_t *operands,
  628. size_t operand_count)
  629. {
  630. struct avctp_pending_req *p = browsing->p;
  631. struct avctp_browsing_req *req;
  632. GSList *l;
  633. if (p && p->transaction == avctp->transaction) {
  634. browsing->processed = g_slist_prepend(browsing->processed, p);
  635. if (p->timeout > 0) {
  636. g_source_remove(p->timeout);
  637. p->timeout = 0;
  638. }
  639. browsing->p = NULL;
  640. if (browsing->process_id == 0)
  641. browsing->process_id = g_idle_add(process_queue,
  642. browsing);
  643. }
  644. avctp_ref(browsing->session);
  645. for (l = browsing->processed; l; l = l->next) {
  646. p = l->data;
  647. req = p->data;
  648. if (p->transaction != avctp->transaction)
  649. continue;
  650. if (req->func && req->func(browsing->session, operands,
  651. operand_count, req->user_data))
  652. break;
  653. browsing->processed = g_slist_remove(browsing->processed, p);
  654. pending_destroy(p, NULL);
  655. break;
  656. }
  657. avctp_unref(browsing->session);
  658. }
  659. static gboolean session_browsing_cb(GIOChannel *chan, GIOCondition cond,
  660. gpointer data)
  661. {
  662. struct avctp *session = data;
  663. struct avctp_channel *browsing = session->browsing;
  664. uint8_t *buf = browsing->buffer;
  665. uint8_t *operands;
  666. struct avctp_header *avctp;
  667. int sock, ret, packet_size, operand_count;
  668. struct avctp_browsing_pdu_handler *handler;
  669. if (cond & (G_IO_ERR | G_IO_HUP | G_IO_NVAL))
  670. goto failed;
  671. sock = g_io_channel_unix_get_fd(chan);
  672. ret = read(sock, buf, browsing->imtu);
  673. if (ret <= 0)
  674. goto failed;
  675. if (ret < AVCTP_HEADER_LENGTH) {
  676. error("Too small AVCTP packet");
  677. goto failed;
  678. }
  679. avctp = (struct avctp_header *) buf;
  680. if (avctp->packet_type != AVCTP_PACKET_SINGLE) {
  681. error("Invalid packet type");
  682. goto failed;
  683. }
  684. operands = buf + AVCTP_HEADER_LENGTH;
  685. ret -= AVCTP_HEADER_LENGTH;
  686. operand_count = ret;
  687. if (avctp->cr == AVCTP_RESPONSE) {
  688. browsing_response(browsing, avctp, operands, operand_count);
  689. return TRUE;
  690. }
  691. packet_size = AVCTP_HEADER_LENGTH;
  692. avctp->cr = AVCTP_RESPONSE;
  693. handler = g_slist_nth_data(browsing->handlers, 0);
  694. if (handler == NULL) {
  695. DBG("handler not found");
  696. /* FIXME: Add general reject */
  697. /* packet_size += avrcp_browsing_general_reject(operands); */
  698. goto send;
  699. }
  700. ret = handler->cb(session, avctp->transaction, operands, operand_count,
  701. handler->user_data);
  702. if (ret < 0) {
  703. if (ret == -EAGAIN)
  704. return TRUE;
  705. goto failed;
  706. }
  707. packet_size += ret;
  708. send:
  709. if (packet_size != 0) {
  710. ret = write(sock, buf, packet_size);
  711. if (ret != packet_size)
  712. goto failed;
  713. }
  714. return TRUE;
  715. failed:
  716. DBG("AVCTP Browsing: disconnected");
  717. if (session->browsing) {
  718. avctp_channel_destroy(session->browsing);
  719. session->browsing = NULL;
  720. }
  721. return FALSE;
  722. }
  723. static gboolean session_cb(GIOChannel *chan, GIOCondition cond, gpointer data)
  724. {
  725. struct avctp *session = data;
  726. struct avctp_channel *control = session->control;
  727. uint8_t *buf = control->buffer;
  728. uint8_t *operands, code, subunit;
  729. struct avctp_header *avctp;
  730. struct avc_header *avc;
  731. int packet_size, operand_count, sock;
  732. struct avctp_pdu_handler *handler;
  733. ssize_t ret;
  734. if (cond & (G_IO_ERR | G_IO_HUP | G_IO_NVAL))
  735. goto failed;
  736. sock = g_io_channel_unix_get_fd(chan);
  737. ret = read(sock, buf, control->imtu);
  738. if (ret <= 0)
  739. goto failed;
  740. if (ret < AVCTP_HEADER_LENGTH) {
  741. error("Too small AVCTP packet");
  742. goto failed;
  743. }
  744. avctp = (struct avctp_header *) buf;
  745. ret -= AVCTP_HEADER_LENGTH;
  746. if (ret < AVC_HEADER_LENGTH) {
  747. error("Too small AVC packet");
  748. goto failed;
  749. }
  750. avc = (struct avc_header *) (buf + AVCTP_HEADER_LENGTH);
  751. ret -= AVC_HEADER_LENGTH;
  752. operands = (uint8_t *) avc + AVC_HEADER_LENGTH;
  753. operand_count = ret;
  754. if (avctp->cr == AVCTP_RESPONSE) {
  755. control_response(control, avctp, avc, operands, operand_count);
  756. return TRUE;
  757. }
  758. packet_size = AVCTP_HEADER_LENGTH + AVC_HEADER_LENGTH;
  759. avctp->cr = AVCTP_RESPONSE;
  760. if (avctp->packet_type != AVCTP_PACKET_SINGLE) {
  761. avc->code = AVC_CTYPE_NOT_IMPLEMENTED;
  762. goto done;
  763. }
  764. if (avctp->pid != htons(AV_REMOTE_SVCLASS_ID)) {
  765. avctp->ipid = 1;
  766. packet_size = AVCTP_HEADER_LENGTH;
  767. goto done;
  768. }
  769. handler = find_handler(control->handlers, avc->opcode);
  770. if (!handler) {
  771. DBG("handler not found for 0x%02x", avc->opcode);
  772. avc->code = AVC_CTYPE_REJECTED;
  773. goto done;
  774. }
  775. code = avc->code;
  776. subunit = avc->subunit_type;
  777. ret = handler->cb(session, avctp->transaction, &code,
  778. &subunit, operands, operand_count,
  779. handler->user_data);
  780. if (ret < 0) {
  781. if (ret == -EAGAIN)
  782. return TRUE;
  783. goto failed;
  784. }
  785. packet_size += ret;
  786. avc->code = code;
  787. avc->subunit_type = subunit;
  788. done:
  789. ret = write(sock, buf, packet_size);
  790. if (ret != packet_size)
  791. goto failed;
  792. return TRUE;
  793. failed:
  794. DBG("AVCTP session %p got disconnected", session);
  795. avctp_shutdown(session);
  796. return FALSE;
  797. }
  798. static int uinput_create(const char *name)
  799. {
  800. struct uinput_dev dev;
  801. int fd, err, i;
  802. fd = open("/dev/uinput", O_RDWR);
  803. if (fd < 0) {
  804. fd = open("/dev/input/uinput", O_RDWR);
  805. if (fd < 0) {
  806. fd = open("/dev/misc/uinput", O_RDWR);
  807. if (fd < 0) {
  808. err = -errno;
  809. error("Can't open input device: %s (%d)",
  810. strerror(-err), -err);
  811. return err;
  812. }
  813. }
  814. }
  815. memset(&dev, 0, sizeof(dev));
  816. if (name)
  817. strncpy(dev.name, name, UINPUT_MAX_NAME_SIZE - 1);
  818. dev.id.bustype = BUS_BLUETOOTH;
  819. dev.id.vendor = 0x0000;
  820. dev.id.product = 0x0000;
  821. dev.id.version = 0x0000;
  822. if (write(fd, &dev, sizeof(dev)) < 0) {
  823. err = -errno;
  824. error("Can't write device information: %s (%d)",
  825. strerror(-err), -err);
  826. close(fd);
  827. return err;
  828. }
  829. ioctl(fd, UI_SET_EVBIT, EV_KEY);
  830. ioctl(fd, UI_SET_EVBIT, EV_REL);
  831. ioctl(fd, UI_SET_EVBIT, EV_REP);
  832. ioctl(fd, UI_SET_EVBIT, EV_SYN);
  833. for (i = 0; key_map[i].name != NULL; i++)
  834. ioctl(fd, UI_SET_KEYBIT, key_map[i].uinput);
  835. if (ioctl(fd, UI_DEV_CREATE, NULL) < 0) {
  836. err = -errno;
  837. error("Can't create uinput device: %s (%d)",
  838. strerror(-err), -err);
  839. close(fd);
  840. return err;
  841. }
  842. return fd;
  843. }
  844. int avctp_init_uinput(struct avctp *session, const char *name,
  845. const char *address)
  846. {
  847. if (g_str_equal(name, "Nokia CK-20W")) {
  848. session->key_quirks[AVC_FORWARD] |= QUIRK_NO_RELEASE;
  849. session->key_quirks[AVC_BACKWARD] |= QUIRK_NO_RELEASE;
  850. session->key_quirks[AVC_PLAY] |= QUIRK_NO_RELEASE;
  851. session->key_quirks[AVC_PAUSE] |= QUIRK_NO_RELEASE;
  852. }
  853. session->uinput = uinput_create(address);
  854. if (session->uinput < 0) {
  855. error("AVCTP: failed to init uinput for %s", address);
  856. return session->uinput;
  857. }
  858. return 0;
  859. }
  860. static struct avctp_channel *avctp_channel_create(struct avctp *session, int fd,
  861. size_t imtu, size_t omtu,
  862. avctp_destroy_cb_t destroy)
  863. {
  864. struct avctp_channel *chan;
  865. chan = g_new0(struct avctp_channel, 1);
  866. chan->session = session;
  867. chan->io = g_io_channel_unix_new(fd);
  868. chan->queue = g_queue_new();
  869. chan->imtu = imtu;
  870. chan->omtu = omtu;
  871. chan->buffer = g_malloc0(MAX(imtu, omtu));
  872. chan->destroy = destroy;
  873. return chan;
  874. }
  875. static void handler_free(void *data)
  876. {
  877. struct avctp_browsing_pdu_handler *handler = data;
  878. if (handler->destroy)
  879. handler->destroy(handler->user_data);
  880. g_free(data);
  881. }
  882. static void avctp_destroy_browsing(void *data)
  883. {
  884. struct avctp_channel *chan = data;
  885. g_slist_free_full(chan->handlers, handler_free);
  886. chan->handlers = NULL;
  887. }
  888. static struct avctp_pending_req *pending_create(struct avctp_channel *chan,
  889. avctp_process_cb process,
  890. void *data,
  891. avctp_destroy_cb_t destroy)
  892. {
  893. struct avctp_pending_req *p;
  894. GSList *l, *tmp;
  895. if (!chan->processed)
  896. goto done;
  897. tmp = g_slist_copy(chan->processed);
  898. /* Find first unused transaction id */
  899. for (l = tmp; l; l = g_slist_next(l)) {
  900. struct avctp_pending_req *req = l->data;
  901. if (req->transaction == chan->transaction) {
  902. chan->transaction++;
  903. chan->transaction %= 16;
  904. tmp = g_slist_delete_link(tmp, l);
  905. l = tmp;
  906. }
  907. }
  908. g_slist_free(tmp);
  909. done:
  910. p = g_new0(struct avctp_pending_req, 1);
  911. p->chan = chan;
  912. p->transaction = chan->transaction;
  913. p->process = process;
  914. p->data = data;
  915. p->destroy = destroy;
  916. chan->transaction++;
  917. chan->transaction %= 16;
  918. return p;
  919. }
  920. static int avctp_send_req(struct avctp *session, uint8_t code, uint8_t subunit,
  921. uint8_t opcode, const struct iovec *iov, int iov_cnt,
  922. avctp_rsp_cb func, void *user_data)
  923. {
  924. struct avctp_channel *control = session->control;
  925. struct avctp_pending_req *p;
  926. struct avctp_control_req *req;
  927. struct iovec *pdu;
  928. int i;
  929. if (control == NULL)
  930. return -ENOTCONN;
  931. pdu = g_new0(struct iovec, iov_cnt);
  932. for (i = 0; i < iov_cnt; i++) {
  933. pdu[i].iov_len = iov[i].iov_len;
  934. pdu[i].iov_base = g_memdup(iov[i].iov_base, iov[i].iov_len);
  935. }
  936. req = g_new0(struct avctp_control_req, 1);
  937. req->code = code;
  938. req->subunit = subunit;
  939. req->op = opcode;
  940. req->func = func;
  941. req->iov = pdu;
  942. req->iov_cnt = iov_cnt;
  943. req->user_data = user_data;
  944. p = pending_create(control, process_control, req, control_req_destroy);
  945. req->p = p;
  946. g_queue_push_tail(control->queue, p);
  947. if (control->process_id == 0)
  948. control->process_id = g_idle_add(process_queue, control);
  949. return 0;
  950. }
  951. int avctp_send_browsing_req(struct avctp *session,
  952. const struct iovec *iov, int iov_cnt,
  953. avctp_browsing_rsp_cb func, void *user_data)
  954. {
  955. struct avctp_channel *browsing = session->browsing;
  956. struct avctp_pending_req *p;
  957. struct avctp_browsing_req *req;
  958. struct iovec *pdu;
  959. int i;
  960. if (browsing == NULL)
  961. return -ENOTCONN;
  962. pdu = g_new0(struct iovec, iov_cnt);
  963. for (i = 0; i < iov_cnt; i++) {
  964. pdu[i].iov_len = iov[i].iov_len;
  965. pdu[i].iov_base = g_memdup(iov[i].iov_base, iov[i].iov_len);
  966. }
  967. req = g_new0(struct avctp_browsing_req, 1);
  968. req->func = func;
  969. req->iov = pdu;
  970. req->iov_cnt = iov_cnt;
  971. req->user_data = user_data;
  972. p = pending_create(browsing, process_browsing, req,
  973. browsing_req_destroy);
  974. req->p = p;
  975. g_queue_push_tail(browsing->queue, p);
  976. /* Connection did not complete, delay process of the request */
  977. if (browsing->watch == 0)
  978. return 0;
  979. if (browsing->process_id == 0)
  980. browsing->process_id = g_idle_add(process_queue, browsing);
  981. return 0;
  982. }
  983. int avctp_send_browsing(struct avctp *session, uint8_t transaction,
  984. const struct iovec *iov, int iov_cnt)
  985. {
  986. struct avctp_channel *browsing = session->browsing;
  987. if (browsing == NULL)
  988. return -ENOTCONN;
  989. return avctp_browsing_send(browsing, transaction, AVCTP_RESPONSE,
  990. iov, iov_cnt);
  991. }
  992. static const char *op2str(uint8_t op)
  993. {
  994. int i;
  995. for (i = 0; key_map[i].name != NULL; i++) {
  996. if ((op & 0x7F) == key_map[i].avc)
  997. return key_map[i].name;
  998. }
  999. return "UNKNOWN";
  1000. }
  1001. static int avctp_passthrough_press(struct avctp *session, uint8_t op,
  1002. uint8_t *params, size_t params_len)
  1003. {
  1004. struct iovec iov[2];
  1005. int iov_cnt;
  1006. uint8_t operands[2];
  1007. DBG("%s", op2str(op));
  1008. iov[0].iov_base = operands;
  1009. iov[0].iov_len = sizeof(operands);
  1010. /* Button pressed */
  1011. operands[0] = op & 0x7f;
  1012. if (params_len > 0) {
  1013. iov[1].iov_base = params;
  1014. iov[1].iov_len = params_len;
  1015. iov_cnt = 2;
  1016. operands[1] = params_len;
  1017. } else {
  1018. iov_cnt = 1;
  1019. operands[1] = 0;
  1020. }
  1021. return avctp_send_req(session, AVC_CTYPE_CONTROL,
  1022. AVC_SUBUNIT_PANEL, AVC_OP_PASSTHROUGH,
  1023. iov, iov_cnt, avctp_passthrough_rsp, NULL);
  1024. }
  1025. static int avctp_passthrough_release(struct avctp *session, uint8_t op,
  1026. uint8_t *params, size_t params_len)
  1027. {
  1028. struct iovec iov[2];
  1029. int iov_cnt;
  1030. uint8_t operands[2];
  1031. DBG("%s", op2str(op));
  1032. iov[0].iov_base = operands;
  1033. iov[0].iov_len = sizeof(operands);
  1034. /* Button released */
  1035. operands[0] = op | 0x80;
  1036. if (params_len > 0) {
  1037. iov[1].iov_base = params;
  1038. iov[1].iov_len = params_len;
  1039. iov_cnt = 2;
  1040. operands[1] = params_len;
  1041. } else {
  1042. iov_cnt = 1;
  1043. operands[1] = 0;
  1044. }
  1045. return avctp_send_req(session, AVC_CTYPE_CONTROL,
  1046. AVC_SUBUNIT_PANEL, AVC_OP_PASSTHROUGH,
  1047. iov, iov_cnt, NULL, NULL);
  1048. }
  1049. static gboolean repeat_timeout(gpointer user_data)
  1050. {
  1051. struct avctp *session = user_data;
  1052. avctp_passthrough_release(session, session->key.op, session->key.params,
  1053. session->key.params_len);
  1054. avctp_passthrough_press(session, session->key.op, session->key.params,
  1055. session->key.params_len);
  1056. return TRUE;
  1057. }
  1058. static void release_pressed(struct avctp *session)
  1059. {
  1060. avctp_passthrough_release(session, session->key.op, session->key.params,
  1061. session->key.params_len);
  1062. if (session->key.timer > 0)
  1063. g_source_remove(session->key.timer);
  1064. session->key.timer = 0;
  1065. }
  1066. static bool set_pressed(struct avctp *session, uint8_t op, uint8_t *params,
  1067. size_t params_len)
  1068. {
  1069. if (session->key.timer > 0) {
  1070. if (session->key.op == op)
  1071. return TRUE;
  1072. release_pressed(session);
  1073. }
  1074. if (op != AVC_FAST_FORWARD && op != AVC_REWIND)
  1075. return FALSE;
  1076. session->key.op = op;
  1077. session->key.params = params;
  1078. session->key.params_len = params_len;
  1079. session->key.timer = g_timeout_add_seconds(AVC_PRESS_TIMEOUT,
  1080. repeat_timeout,
  1081. session);
  1082. return TRUE;
  1083. }
  1084. static gboolean avctp_passthrough_rsp(struct avctp *session, uint8_t code,
  1085. uint8_t subunit, uint8_t *operands,
  1086. size_t operand_count, void *user_data)
  1087. {
  1088. uint8_t *params;
  1089. size_t params_len;
  1090. DBG("code 0x%02x operand_count %zd", code, operand_count);
  1091. if (code != AVC_CTYPE_ACCEPTED)
  1092. return FALSE;
  1093. if (operands[0] == AVC_VENDOR_UNIQUE) {
  1094. params = &operands[2];
  1095. params_len = operand_count - 2;
  1096. } else {
  1097. params = NULL;
  1098. params_len = 0;
  1099. }
  1100. if (set_pressed(session, operands[0], params, params_len))
  1101. return FALSE;
  1102. avctp_passthrough_release(session, operands[0], params, params_len);
  1103. return FALSE;
  1104. }
  1105. int avctp_send_passthrough(struct avctp *session, uint8_t op, uint8_t *params,
  1106. size_t params_len)
  1107. {
  1108. /* Auto release if key pressed */
  1109. if (session->key.timer > 0)
  1110. release_pressed(session);
  1111. return avctp_passthrough_press(session, op, params, params_len);
  1112. }
  1113. int avctp_send_vendor(struct avctp *session, uint8_t transaction,
  1114. uint8_t code, uint8_t subunit,
  1115. const struct iovec *iov, int iov_cnt)
  1116. {
  1117. struct avctp_channel *control = session->control;
  1118. if (control == NULL)
  1119. return -ENOTCONN;
  1120. return avctp_send(control, transaction, AVCTP_RESPONSE, code, subunit,
  1121. AVC_OP_VENDORDEP, iov, iov_cnt);
  1122. }
  1123. int avctp_send_vendor_req(struct avctp *session, uint8_t code, uint8_t subunit,
  1124. const struct iovec *iov, int iov_cnt,
  1125. avctp_rsp_cb func, void *user_data)
  1126. {
  1127. return avctp_send_req(session, code, subunit, AVC_OP_VENDORDEP, iov,
  1128. iov_cnt, func, user_data);
  1129. }
  1130. unsigned int avctp_register_passthrough_handler(struct avctp *session,
  1131. avctp_passthrough_cb cb,
  1132. void *user_data)
  1133. {
  1134. struct avctp_channel *control = session->control;
  1135. struct avctp_passthrough_handler *handler;
  1136. static unsigned int id = 0;
  1137. if (control == NULL || session->handler != NULL)
  1138. return 0;
  1139. handler = g_new(struct avctp_passthrough_handler, 1);
  1140. handler->cb = cb;
  1141. handler->user_data = user_data;
  1142. handler->id = ++id;
  1143. session->handler = handler;
  1144. return handler->id;
  1145. }
  1146. bool avctp_unregister_passthrough_handler(struct avctp *session,
  1147. unsigned int id)
  1148. {
  1149. if (session->handler == NULL)
  1150. return false;
  1151. if (session->handler->id != id)
  1152. return false;
  1153. g_free(session->handler);
  1154. session->handler = NULL;
  1155. return true;
  1156. }
  1157. unsigned int avctp_register_pdu_handler(struct avctp *session, uint8_t opcode,
  1158. avctp_control_pdu_cb cb,
  1159. void *user_data)
  1160. {
  1161. struct avctp_channel *control = session->control;
  1162. struct avctp_pdu_handler *handler;
  1163. static unsigned int id = 0;
  1164. if (control == NULL)
  1165. return 0;
  1166. handler = find_handler(control->handlers, opcode);
  1167. if (handler)
  1168. return 0;
  1169. handler = g_new(struct avctp_pdu_handler, 1);
  1170. handler->opcode = opcode;
  1171. handler->cb = cb;
  1172. handler->user_data = user_data;
  1173. handler->id = ++id;
  1174. control->handlers = g_slist_append(control->handlers, handler);
  1175. return handler->id;
  1176. }
  1177. unsigned int avctp_register_browsing_pdu_handler(struct avctp *session,
  1178. avctp_browsing_pdu_cb cb,
  1179. void *user_data,
  1180. avctp_destroy_cb_t destroy)
  1181. {
  1182. struct avctp_channel *browsing = session->browsing;
  1183. struct avctp_browsing_pdu_handler *handler;
  1184. static unsigned int id = 0;
  1185. if (browsing == NULL)
  1186. return 0;
  1187. if (browsing->handlers != NULL)
  1188. return 0;
  1189. handler = g_new(struct avctp_browsing_pdu_handler, 1);
  1190. handler->cb = cb;
  1191. handler->user_data = user_data;
  1192. handler->id = ++id;
  1193. handler->destroy = destroy;
  1194. browsing->handlers = g_slist_append(browsing->handlers, handler);
  1195. return handler->id;
  1196. }
  1197. bool avctp_unregister_pdu_handler(struct avctp *session, unsigned int id)
  1198. {
  1199. struct avctp_channel *control = session->control;
  1200. GSList *l;
  1201. if (!control)
  1202. return false;
  1203. for (l = control->handlers; l; l = g_slist_next(l)) {
  1204. struct avctp_pdu_handler *handler = l->data;
  1205. if (handler->id != id)
  1206. continue;
  1207. control->handlers = g_slist_remove(control->handlers, handler);
  1208. g_free(handler);
  1209. return true;
  1210. }
  1211. return false;
  1212. }
  1213. bool avctp_unregister_browsing_pdu_handler(struct avctp *session,
  1214. unsigned int id)
  1215. {
  1216. struct avctp_channel *browsing = session->browsing;
  1217. GSList *l;
  1218. if (browsing == NULL)
  1219. return false;
  1220. for (l = browsing->handlers; l; l = g_slist_next(l)) {
  1221. struct avctp_browsing_pdu_handler *handler = l->data;
  1222. if (handler->id != id)
  1223. continue;
  1224. browsing->handlers = g_slist_remove(browsing->handlers,
  1225. handler);
  1226. g_free(handler);
  1227. return true;
  1228. }
  1229. return false;
  1230. }
  1231. struct avctp *avctp_new(int fd, size_t imtu, size_t omtu, uint16_t version)
  1232. {
  1233. struct avctp *session;
  1234. struct avctp_channel *control;
  1235. GIOCondition cond = G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
  1236. session = g_new0(struct avctp, 1);
  1237. session->version = version;
  1238. control = avctp_channel_create(session, fd, imtu, omtu, NULL);
  1239. if (!control) {
  1240. g_free(session);
  1241. return NULL;
  1242. }
  1243. session->uinput = -1;
  1244. session->control = control;
  1245. session->passthrough_id = avctp_register_pdu_handler(session,
  1246. AVC_OP_PASSTHROUGH,
  1247. handle_panel_passthrough,
  1248. NULL);
  1249. session->unit_id = avctp_register_pdu_handler(session,
  1250. AVC_OP_UNITINFO,
  1251. handle_unit_info,
  1252. NULL);
  1253. session->subunit_id = avctp_register_pdu_handler(session,
  1254. AVC_OP_SUBUNITINFO,
  1255. handle_subunit_info,
  1256. NULL);
  1257. control->watch = g_io_add_watch(session->control->io, cond,
  1258. (GIOFunc) session_cb, session);
  1259. return avctp_ref(session);
  1260. }
  1261. int avctp_connect_browsing(struct avctp *session, int fd, size_t imtu,
  1262. size_t omtu)
  1263. {
  1264. struct avctp_channel *browsing;
  1265. GIOCondition cond = G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
  1266. if (session->browsing)
  1267. return -EISCONN;
  1268. browsing = avctp_channel_create(session, fd, imtu, omtu,
  1269. avctp_destroy_browsing);
  1270. if (!browsing)
  1271. return -EINVAL;
  1272. session->browsing = browsing;
  1273. browsing->watch = g_io_add_watch(session->browsing->io, cond,
  1274. (GIOFunc) session_browsing_cb, session);
  1275. return 0;
  1276. }
  1277. void avctp_set_destroy_cb(struct avctp *session, avctp_destroy_cb_t cb,
  1278. void *user_data)
  1279. {
  1280. session->destroy = cb;
  1281. session->data = user_data;
  1282. }
  1283. void avctp_shutdown(struct avctp *session)
  1284. {
  1285. if (!session)
  1286. return;
  1287. avctp_unref(session);
  1288. }