Upgrade bluez5_37 :Merge the code from private
[platform/upstream/bluez.git] / android / avrcp.c
1 /*
2  *
3  *  BlueZ - Bluetooth protocol stack for Linux
4  *
5  *  Copyright (C) 2013-2014  Intel Corporation. All rights reserved.
6  *
7  *
8  *  This library is free software; you can redistribute it and/or
9  *  modify it under the terms of the GNU Lesser General Public
10  *  License as published by the Free Software Foundation; either
11  *  version 2.1 of the License, or (at your option) any later version.
12  *
13  *  This library is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  *  Lesser General Public License for more details.
17  *
18  *  You should have received a copy of the GNU Lesser General Public
19  *  License along with this library; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdlib.h>
29 #include <stdbool.h>
30 #include <errno.h>
31 #include <glib.h>
32
33 #include "btio/btio.h"
34 #include "lib/bluetooth.h"
35 #include "lib/sdp.h"
36 #include "lib/sdp_lib.h"
37 #include "src/sdp-client.h"
38 #include "src/shared/util.h"
39 #include "src/log.h"
40
41 #include "avctp.h"
42 #include "avrcp-lib.h"
43 #include "hal-msg.h"
44 #include "ipc-common.h"
45 #include "ipc.h"
46 #include "bluetooth.h"
47 #include "avrcp.h"
48 #include "utils.h"
49
50 #define L2CAP_PSM_AVCTP 0x17
51
52 static bdaddr_t adapter_addr;
53 static uint32_t record_tg_id = 0;
54 static uint32_t record_ct_id = 0;
55 static GSList *devices = NULL;
56 static GIOChannel *server = NULL;
57 static struct ipc *hal_ipc = NULL;
58
59 struct avrcp_request {
60         struct avrcp_device *dev;
61         uint8_t pdu_id;
62         uint8_t event_id;
63         uint8_t transaction;
64 };
65
66 struct avrcp_device {
67         bdaddr_t        dst;
68         uint16_t        version;
69         uint16_t        features;
70         struct avrcp    *session;
71         GIOChannel      *io;
72         GQueue          *queue;
73 };
74
75 static struct avrcp_request *pop_request(uint8_t pdu_id, uint8_t event_id,
76                                                                 bool peek)
77 {
78         GSList *l;
79
80         for (l = devices; l; l = g_slist_next(l)) {
81                 struct avrcp_device *dev = l->data;
82                 GList *reqs = g_queue_peek_head_link(dev->queue);
83                 int i;
84
85                 for (i = 0; reqs; reqs = g_list_next(reqs), i++) {
86                         struct avrcp_request *req = reqs->data;
87
88                         if (req->pdu_id != pdu_id || req->event_id != event_id)
89                                 continue;
90
91                         if (!peek)
92                                 g_queue_pop_nth(dev->queue, i);
93
94                         return req;
95                 }
96         }
97
98         return NULL;
99 }
100
101 static void handle_get_play_status(const void *buf, uint16_t len)
102 {
103         const struct hal_cmd_avrcp_get_play_status *cmd = buf;
104         uint8_t status;
105         struct avrcp_request *req;
106         int ret;
107
108         DBG("");
109
110         req = pop_request(AVRCP_GET_PLAY_STATUS, 0, false);
111         if (!req) {
112                 status = HAL_STATUS_FAILED;
113                 goto done;
114         }
115
116         ret = avrcp_get_play_status_rsp(req->dev->session, req->transaction,
117                                         cmd->position, cmd->duration,
118                                         cmd->status);
119         if (ret < 0) {
120                 status = HAL_STATUS_FAILED;
121                 g_free(req);
122                 goto done;
123         }
124
125         status = HAL_STATUS_SUCCESS;
126         g_free(req);
127
128 done:
129         ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_AVRCP,
130                                 HAL_OP_AVRCP_GET_PLAY_STATUS, status);
131 }
132
133 static void handle_list_player_attrs(const void *buf, uint16_t len)
134 {
135         DBG("");
136
137         ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_AVRCP,
138                         HAL_OP_AVRCP_LIST_PLAYER_ATTRS, HAL_STATUS_FAILED);
139 }
140
141 static void handle_list_player_values(const void *buf, uint16_t len)
142 {
143         DBG("");
144
145         ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_AVRCP,
146                         HAL_OP_AVRCP_LIST_PLAYER_VALUES, HAL_STATUS_FAILED);
147 }
148
149 static void handle_get_player_attrs(const void *buf, uint16_t len)
150 {
151         DBG("");
152
153         ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_AVRCP,
154                         HAL_OP_AVRCP_GET_PLAYER_ATTRS, HAL_STATUS_FAILED);
155 }
156
157 static void handle_get_player_attrs_text(const void *buf, uint16_t len)
158 {
159         DBG("");
160
161         ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_AVRCP,
162                         HAL_OP_AVRCP_GET_PLAYER_ATTRS_TEXT, HAL_STATUS_FAILED);
163 }
164
165 static void handle_get_player_values_text(const void *buf, uint16_t len)
166 {
167         DBG("");
168
169         ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_AVRCP,
170                         HAL_OP_AVRCP_GET_PLAYER_VALUES_TEXT, HAL_STATUS_FAILED);
171 }
172
173 static size_t write_element_text(uint8_t id, uint8_t text_len, uint8_t *text,
174                                                 uint8_t *pdu)
175 {
176         uint16_t charset = 106;
177         size_t len = 0;
178
179         put_be32(id, pdu);
180         pdu += 4;
181         len += 4;
182
183         put_be16(charset, pdu);
184         pdu += 2;
185         len += 2;
186
187         put_be16(text_len, pdu);
188         pdu += 2;
189         len += 2;
190
191         memcpy(pdu, text, text_len);
192         len += text_len;
193
194         return len;
195 }
196
197 static void write_element_attrs(uint8_t *ptr, uint8_t number, uint8_t *pdu,
198                                                                 size_t *len)
199 {
200         int i;
201
202         *pdu = number;
203         pdu++;
204         *len += 1;
205
206         for (i = 0; i < number; i++) {
207                 struct hal_avrcp_player_setting_text *text = (void *) ptr;
208                 size_t ret;
209
210                 ret = write_element_text(text->id, text->len, text->text, pdu);
211
212                 ptr += sizeof(*text) + text->len;
213                 pdu += ret;
214                 *len += ret;
215         }
216 }
217
218 static void handle_get_element_attrs_text(const void *buf, uint16_t len)
219 {
220         struct hal_cmd_avrcp_get_element_attrs_text *cmd = (void *) buf;
221         uint8_t status;
222         struct avrcp_request *req;
223         uint8_t pdu[IPC_MTU];
224         uint8_t *ptr;
225         size_t pdu_len;
226         int ret;
227
228         DBG("");
229
230         req = pop_request(AVRCP_GET_ELEMENT_ATTRIBUTES, 0, false);
231         if (!req) {
232                 status = HAL_STATUS_FAILED;
233                 goto done;
234         }
235
236         ptr = (uint8_t *) &cmd->values[0];
237         pdu_len = 0;
238         write_element_attrs(ptr, cmd->number, pdu, &pdu_len);
239
240         ret = avrcp_get_element_attrs_rsp(req->dev->session, req->transaction,
241                                                                 pdu, pdu_len);
242         if (ret < 0) {
243                 status = HAL_STATUS_FAILED;
244                 g_free(req);
245                 goto done;
246         }
247
248         status = HAL_STATUS_SUCCESS;
249         g_free(req);
250
251 done:
252         ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_AVRCP,
253                         HAL_OP_AVRCP_GET_ELEMENT_ATTRS_TEXT, status);
254 }
255
256 static void handle_set_player_attrs_value(const void *buf, uint16_t len)
257 {
258         DBG("");
259
260         ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_AVRCP,
261                         HAL_OP_AVRCP_SET_PLAYER_ATTRS_VALUE, HAL_STATUS_FAILED);
262 }
263
264 static void handle_register_notification(const void *buf, uint16_t len)
265 {
266         struct hal_cmd_avrcp_register_notification *cmd = (void *) buf;
267         uint8_t status;
268         struct avrcp_request *req;
269         uint8_t code;
270         bool peek = false;
271         int ret;
272
273         DBG("");
274
275         switch (cmd->type) {
276         case HAL_AVRCP_EVENT_TYPE_INTERIM:
277                 code = AVC_CTYPE_INTERIM;
278                 peek = true;
279                 break;
280         case HAL_AVRCP_EVENT_TYPE_CHANGED:
281                 code = AVC_CTYPE_CHANGED;
282                 break;
283         default:
284                 status = HAL_STATUS_FAILED;
285                 goto done;
286         }
287
288         req = pop_request(AVRCP_REGISTER_NOTIFICATION, cmd->event, peek);
289         if (!req) {
290                 status = HAL_STATUS_FAILED;
291                 goto done;
292         }
293
294         ret = avrcp_register_notification_rsp(req->dev->session,
295                                                 req->transaction, code,
296                                                 cmd->event, cmd->data,
297                                                 cmd->len);
298         if (ret < 0) {
299                 status = HAL_STATUS_FAILED;
300                 if (!peek)
301                         g_free(req);
302                 goto done;
303         }
304
305         status = HAL_STATUS_SUCCESS;
306         if (!peek)
307                 g_free(req);
308
309 done:
310         ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_AVRCP,
311                         HAL_OP_AVRCP_REGISTER_NOTIFICATION, status);
312 }
313
314 static void handle_set_volume(const void *buf, uint16_t len)
315 {
316         struct hal_cmd_avrcp_set_volume *cmd = (void *) buf;
317         struct avrcp_device *dev;
318         uint8_t status;
319         int ret;
320
321         DBG("");
322
323         if (!devices) {
324                 error("AVRCP: No device found to set volume");
325                 status = HAL_STATUS_FAILED;
326                 goto done;
327         }
328
329         /*
330          * Peek the first device since the HAL cannot really address a specific
331          * device it might mean there could only be one connected.
332          */
333         dev = devices->data;
334
335         ret = avrcp_set_volume(dev->session, cmd->value & 0x7f);
336         if (ret < 0) {
337                 status = HAL_STATUS_FAILED;
338                 goto done;
339         }
340
341         status = HAL_STATUS_SUCCESS;
342
343 done:
344         ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_AVRCP, HAL_OP_AVRCP_SET_VOLUME,
345                                                                 status);
346 }
347
348 static const struct ipc_handler cmd_handlers[] = {
349         /* HAL_OP_AVRCP_GET_PLAY_STATUS */
350         { handle_get_play_status, false,
351                         sizeof(struct hal_cmd_avrcp_get_play_status) },
352         /* HAL_OP_AVRCP_LIST_PLAYER_ATTRS */
353         { handle_list_player_attrs, true,
354                         sizeof(struct hal_cmd_avrcp_list_player_attrs) },
355         /* HAL_OP_AVRCP_LIST_PLAYER_VALUES */
356         { handle_list_player_values, true,
357                         sizeof(struct hal_cmd_avrcp_list_player_values) },
358         /* HAL_OP_AVRCP_GET_PLAYER_ATTRS */
359         { handle_get_player_attrs, true,
360                         sizeof(struct hal_cmd_avrcp_get_player_attrs) },
361         /* HAL_OP_AVRCP_GET_PLAYER_ATTRS_TEXT */
362         { handle_get_player_attrs_text, true,
363                         sizeof(struct hal_cmd_avrcp_get_player_attrs_text) },
364         /* HAL_OP_AVRCP_GET_PLAYER_VALUES_TEXT */
365         { handle_get_player_values_text, true,
366                         sizeof(struct hal_cmd_avrcp_get_player_values_text) },
367         /* HAL_OP_AVRCP_GET_ELEMENT_ATTRS_TEXT */
368         { handle_get_element_attrs_text, true,
369                         sizeof(struct hal_cmd_avrcp_get_element_attrs_text) },
370         /* HAL_OP_AVRCP_SET_PLAYER_ATTRS_VALUE */
371         { handle_set_player_attrs_value, true,
372                         sizeof(struct hal_cmd_avrcp_set_player_attrs_value) },
373         /* HAL_OP_AVRCP_REGISTER_NOTIFICATION */
374         { handle_register_notification, true,
375                         sizeof(struct hal_cmd_avrcp_register_notification) },
376         /* HAL_OP_AVRCP_SET_VOLUME */
377         { handle_set_volume, false, sizeof(struct hal_cmd_avrcp_set_volume) },
378 };
379
380 static sdp_record_t *avrcp_tg_record(void)
381 {
382         sdp_list_t *svclass_id, *pfseq, *apseq, *root;
383         uuid_t root_uuid, l2cap, avctp, avrtg;
384         sdp_profile_desc_t profile[1];
385         sdp_list_t *aproto_control, *proto_control[2];
386         sdp_record_t *record;
387         sdp_data_t *psm, *version, *features;
388         uint16_t lp = L2CAP_PSM_AVCTP;
389         uint16_t avrcp_ver = 0x0105, avctp_ver = 0x0104;
390         uint16_t feat = (AVRCP_FEATURE_CATEGORY_1 |
391                                         AVRCP_FEATURE_CATEGORY_2 |
392                                         AVRCP_FEATURE_CATEGORY_3 |
393                                         AVRCP_FEATURE_CATEGORY_4);
394
395         record = sdp_record_alloc();
396         if (!record)
397                 return NULL;
398
399         sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
400         root = sdp_list_append(NULL, &root_uuid);
401         sdp_set_browse_groups(record, root);
402
403         /* Service Class ID List */
404         sdp_uuid16_create(&avrtg, AV_REMOTE_TARGET_SVCLASS_ID);
405         svclass_id = sdp_list_append(NULL, &avrtg);
406         sdp_set_service_classes(record, svclass_id);
407
408         /* Protocol Descriptor List */
409         sdp_uuid16_create(&l2cap, L2CAP_UUID);
410         proto_control[0] = sdp_list_append(NULL, &l2cap);
411         psm = sdp_data_alloc(SDP_UINT16, &lp);
412         proto_control[0] = sdp_list_append(proto_control[0], psm);
413         apseq = sdp_list_append(NULL, proto_control[0]);
414
415         sdp_uuid16_create(&avctp, AVCTP_UUID);
416         proto_control[1] = sdp_list_append(NULL, &avctp);
417         version = sdp_data_alloc(SDP_UINT16, &avctp_ver);
418         proto_control[1] = sdp_list_append(proto_control[1], version);
419         apseq = sdp_list_append(apseq, proto_control[1]);
420
421         aproto_control = sdp_list_append(NULL, apseq);
422         sdp_set_access_protos(record, aproto_control);
423
424         /* Bluetooth Profile Descriptor List */
425         sdp_uuid16_create(&profile[0].uuid, AV_REMOTE_PROFILE_ID);
426         profile[0].version = avrcp_ver;
427         pfseq = sdp_list_append(NULL, &profile[0]);
428         sdp_set_profile_descs(record, pfseq);
429
430         features = sdp_data_alloc(SDP_UINT16, &feat);
431         sdp_attr_add(record, SDP_ATTR_SUPPORTED_FEATURES, features);
432
433         sdp_set_info_attr(record, "AVRCP TG", NULL, NULL);
434
435         sdp_data_free(psm);
436         sdp_data_free(version);
437         sdp_list_free(proto_control[0], NULL);
438         sdp_list_free(proto_control[1], NULL);
439         sdp_list_free(apseq, NULL);
440         sdp_list_free(aproto_control, NULL);
441         sdp_list_free(pfseq, NULL);
442         sdp_list_free(root, NULL);
443         sdp_list_free(svclass_id, NULL);
444
445         return record;
446 }
447
448 static sdp_record_t *avrcp_ct_record(void)
449 {
450         sdp_list_t *svclass_id, *pfseq, *apseq, *root;
451         uuid_t root_uuid, l2cap, avctp, avrct, avrctr;
452         sdp_profile_desc_t profile[1];
453         sdp_list_t *aproto, *proto[2];
454         sdp_record_t *record;
455         sdp_data_t *psm, *version, *features;
456         uint16_t lp = AVCTP_CONTROL_PSM;
457         uint16_t avrcp_ver = 0x0105, avctp_ver = 0x0104;
458         uint16_t feat = ( AVRCP_FEATURE_CATEGORY_1 |
459                                                 AVRCP_FEATURE_CATEGORY_2 |
460                                                 AVRCP_FEATURE_CATEGORY_3 |
461                                                 AVRCP_FEATURE_CATEGORY_4);
462
463         record = sdp_record_alloc();
464         if (!record)
465                 return NULL;
466
467         sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
468         root = sdp_list_append(NULL, &root_uuid);
469         sdp_set_browse_groups(record, root);
470
471         /* Service Class ID List */
472         sdp_uuid16_create(&avrct, AV_REMOTE_SVCLASS_ID);
473         svclass_id = sdp_list_append(NULL, &avrct);
474         sdp_uuid16_create(&avrctr, AV_REMOTE_CONTROLLER_SVCLASS_ID);
475         svclass_id = sdp_list_append(svclass_id, &avrctr);
476         sdp_set_service_classes(record, svclass_id);
477
478         /* Protocol Descriptor List */
479         sdp_uuid16_create(&l2cap, L2CAP_UUID);
480         proto[0] = sdp_list_append(NULL, &l2cap);
481         psm = sdp_data_alloc(SDP_UINT16, &lp);
482         proto[0] = sdp_list_append(proto[0], psm);
483         apseq = sdp_list_append(NULL, proto[0]);
484
485         sdp_uuid16_create(&avctp, AVCTP_UUID);
486         proto[1] = sdp_list_append(NULL, &avctp);
487         version = sdp_data_alloc(SDP_UINT16, &avctp_ver);
488         proto[1] = sdp_list_append(proto[1], version);
489         apseq = sdp_list_append(apseq, proto[1]);
490
491         aproto = sdp_list_append(NULL, apseq);
492         sdp_set_access_protos(record, aproto);
493
494         /* Bluetooth Profile Descriptor List */
495         sdp_uuid16_create(&profile[0].uuid, AV_REMOTE_PROFILE_ID);
496         profile[0].version = avrcp_ver;
497         pfseq = sdp_list_append(NULL, &profile[0]);
498         sdp_set_profile_descs(record, pfseq);
499
500         features = sdp_data_alloc(SDP_UINT16, &feat);
501         sdp_attr_add(record, SDP_ATTR_SUPPORTED_FEATURES, features);
502
503         sdp_set_info_attr(record, "AVRCP CT", NULL, NULL);
504
505         free(psm);
506         free(version);
507         sdp_list_free(proto[0], NULL);
508         sdp_list_free(proto[1], NULL);
509         sdp_list_free(apseq, NULL);
510         sdp_list_free(pfseq, NULL);
511         sdp_list_free(aproto, NULL);
512         sdp_list_free(root, NULL);
513         sdp_list_free(svclass_id, NULL);
514
515         return record;
516 }
517
518 static void avrcp_device_free(void *data)
519 {
520         struct avrcp_device *dev = data;
521
522         if (dev->queue) {
523                 g_queue_foreach(dev->queue, (GFunc) g_free, NULL);
524                 g_queue_free(dev->queue);
525         }
526
527         if (dev->session)
528                 avrcp_shutdown(dev->session);
529
530         if (dev->io) {
531                 g_io_channel_shutdown(dev->io, FALSE, NULL);
532                 g_io_channel_unref(dev->io);
533         }
534
535         g_free(dev);
536 }
537
538 static void avrcp_device_remove(struct avrcp_device *dev)
539 {
540         devices = g_slist_remove(devices, dev);
541         avrcp_device_free(dev);
542 }
543
544 static struct avrcp_device *avrcp_device_new(const bdaddr_t *dst)
545 {
546         struct avrcp_device *dev;
547
548         dev = g_new0(struct avrcp_device, 1);
549         bacpy(&dev->dst, dst);
550         devices = g_slist_prepend(devices, dev);
551
552         return dev;
553 }
554
555 static int device_cmp(gconstpointer s, gconstpointer user_data)
556 {
557         const struct avrcp_device *dev = s;
558         const bdaddr_t *dst = user_data;
559
560         return bacmp(&dev->dst, dst);
561 }
562
563 static struct avrcp_device *avrcp_device_find(const bdaddr_t *dst)
564 {
565         GSList *l;
566
567         l = g_slist_find_custom(devices, dst, device_cmp);
568         if (!l)
569                 return NULL;
570
571         return l->data;
572 }
573
574 static void disconnect_cb(void *data)
575 {
576         struct avrcp_device *dev = data;
577
578         DBG("");
579
580         dev->session = NULL;
581
582         avrcp_device_remove(dev);
583 }
584
585 static bool handle_fast_forward(struct avrcp *session, bool pressed,
586                                                         void *user_data)
587 {
588         struct hal_ev_avrcp_passthrough_cmd ev;
589
590         DBG("pressed %s", pressed ? "true" : "false");
591
592         ev.id = AVC_FAST_FORWARD;
593         ev.state = pressed;
594
595         ipc_send_notif(hal_ipc, HAL_SERVICE_ID_AVRCP,
596                         HAL_EV_AVRCP_PASSTHROUGH_CMD, sizeof(ev), &ev);
597
598         return true;
599 }
600
601 static bool handle_rewind(struct avrcp *session, bool pressed,
602                                                         void *user_data)
603 {
604         struct hal_ev_avrcp_passthrough_cmd ev;
605
606         DBG("pressed %s", pressed ? "true" : "false");
607
608         ev.id = AVC_REWIND;
609         ev.state = pressed;
610
611         ipc_send_notif(hal_ipc, HAL_SERVICE_ID_AVRCP,
612                         HAL_EV_AVRCP_PASSTHROUGH_CMD, sizeof(ev), &ev);
613
614         return true;
615 }
616
617 static const struct avrcp_passthrough_handler passthrough_handlers[] = {
618                 { AVC_FAST_FORWARD, handle_fast_forward },
619                 { AVC_REWIND, handle_rewind },
620                 { },
621 };
622
623 static int handle_get_capabilities_cmd(struct avrcp *session,
624                                         uint8_t transaction, void *user_data)
625 {
626         uint8_t events[] = { AVRCP_EVENT_STATUS_CHANGED,
627                                         AVRCP_EVENT_TRACK_CHANGED,
628                                         AVRCP_EVENT_PLAYBACK_POS_CHANGED  };
629
630         DBG("");
631
632         /*
633          * Android do not provide this info via HAL so the list most
634          * be hardcoded according to what RegisterNotification can
635          * actually handle
636          */
637         avrcp_get_capabilities_rsp(session, transaction, sizeof(events),
638                                                                 events);
639
640         return 0;
641 }
642
643 static void push_request(struct avrcp_device *dev, uint8_t pdu_id,
644                                         uint8_t event_id, uint8_t transaction)
645 {
646         struct avrcp_request *req;
647
648         req = g_new0(struct avrcp_request, 1);
649         req->dev = dev;
650         req->pdu_id = pdu_id;
651         req->event_id = event_id;
652         req->transaction = transaction;
653
654         g_queue_push_tail(dev->queue, req);
655 }
656
657 static int handle_get_play_status_cmd(struct avrcp *session,
658                                         uint8_t transaction, void *user_data)
659 {
660         struct avrcp_device *dev = user_data;
661
662         DBG("");
663
664         ipc_send_notif(hal_ipc, HAL_SERVICE_ID_AVRCP,
665                                         HAL_EV_AVRCP_GET_PLAY_STATUS, 0, NULL);
666
667         push_request(dev, AVRCP_GET_PLAY_STATUS, 0, transaction);
668
669         return 0;
670 }
671
672 static int handle_get_element_attrs_cmd(struct avrcp *session,
673                                         uint8_t transaction, uint64_t uid,
674                                         uint8_t number, uint32_t *attrs,
675                                         void *user_data)
676 {
677         struct avrcp_device *dev = user_data;
678         uint8_t buf[IPC_MTU];
679         struct hal_ev_avrcp_get_element_attrs *ev = (void *) buf;
680         int i;
681
682         DBG("");
683
684         ev->number = number;
685         /* Set everything in case of empty list */
686         if (ev->number == 0) {
687                 for (i = 0; i < HAL_AVRCP_MEDIA_ATTR_DURATION; i++) {
688                         /* Skip 0x00 as the attributes start with 0x01 */
689                         ev->attrs[i] = i + 1;
690                 }
691                 ev->number = i;
692                 goto done;
693         }
694
695         for (i = 0; i < number; i++)
696                 ev->attrs[i] = attrs[i];
697
698 done:
699         ipc_send_notif(hal_ipc, HAL_SERVICE_ID_AVRCP,
700                                         HAL_EV_AVRCP_GET_ELEMENT_ATTRS,
701                                         sizeof(*ev) + ev->number, ev);
702
703         push_request(dev, AVRCP_GET_ELEMENT_ATTRIBUTES, 0, transaction);
704
705         return 0;
706
707 }
708
709 static int handle_register_notification_cmd(struct avrcp *session,
710                                                 uint8_t transaction,
711                                                 uint8_t event,
712                                                 uint32_t interval,
713                                                 void *user_data)
714 {
715         struct avrcp_device *dev = user_data;
716         struct hal_ev_avrcp_register_notification ev;
717
718         DBG("");
719
720         /* TODO: Add any missing events supported by Android */
721         switch (event) {
722         case AVRCP_EVENT_STATUS_CHANGED:
723         case AVRCP_EVENT_TRACK_CHANGED:
724         case AVRCP_EVENT_PLAYBACK_POS_CHANGED:
725                 break;
726         default:
727                 return -EINVAL;
728         }
729
730         ev.event = event;
731         ev.param = interval;
732
733         ipc_send_notif(hal_ipc, HAL_SERVICE_ID_AVRCP,
734                                         HAL_EV_AVRCP_REGISTER_NOTIFICATION,
735                                         sizeof(ev), &ev);
736
737         push_request(dev, AVRCP_REGISTER_NOTIFICATION, event, transaction);
738
739         return 0;
740 }
741
742 static const struct avrcp_control_ind control_ind = {
743         .get_capabilities = handle_get_capabilities_cmd,
744         .get_play_status = handle_get_play_status_cmd,
745         .get_element_attributes = handle_get_element_attrs_cmd,
746         .register_notification = handle_register_notification_cmd,
747 };
748
749 static bool handle_register_notification_rsp(struct avrcp *session, int err,
750                                                 uint8_t code, uint8_t event,
751                                                 void *params,
752                                                 void *user_data)
753 {
754         struct avrcp_device *dev = user_data;
755         struct hal_ev_avrcp_volume_changed ev;
756         uint8_t *volume = params;
757
758         if (err < 0) {
759                 error("AVRCP: %s", strerror(-err));
760                 return false;
761         }
762
763         if (code != AVC_CTYPE_INTERIM && code != AVC_CTYPE_CHANGED)
764                 return false;
765
766         if (event != AVRCP_EVENT_VOLUME_CHANGED)
767                 return false;
768
769         ev.type = code;
770         ev.volume = volume[0];
771
772         ipc_send_notif(hal_ipc, HAL_SERVICE_ID_AVRCP,
773                                         HAL_EV_AVRCP_VOLUME_CHANGED,
774                                         sizeof(ev), &ev);
775
776         if (code == AVC_CTYPE_INTERIM)
777                 return true;
778
779         avrcp_register_notification(dev->session, event, 0);
780         return false;
781 }
782
783 static void handle_get_capabilities_rsp(struct avrcp *session, int err,
784                                         uint8_t number, uint8_t *events,
785                                         void *user_data)
786 {
787         struct avrcp_device *dev = user_data;
788         int i;
789
790         if (err < 0) {
791                 error("AVRCP: %s", strerror(-err));
792                 return;
793         }
794
795         for (i = 0; i < number; i++) {
796                 if (events[i] != AVRCP_EVENT_VOLUME_CHANGED)
797                         continue;
798
799                 avrcp_register_notification(dev->session, events[i], 0);
800                 break;
801         }
802
803         return;
804 }
805
806 static void handle_set_volume_rsp(struct avrcp *session, int err,
807                                                 uint8_t value, void *user_data)
808 {
809         struct hal_ev_avrcp_volume_changed ev;
810
811         if (err < 0) {
812                 ev.volume = 0;
813                 ev.type = AVC_CTYPE_REJECTED;
814                 goto done;
815         }
816
817         ev.volume = value;
818         ev.type = AVC_CTYPE_ACCEPTED;
819
820 done:
821         ipc_send_notif(hal_ipc, HAL_SERVICE_ID_AVRCP,
822                                         HAL_EV_AVRCP_VOLUME_CHANGED,
823                                         sizeof(ev), &ev);
824 }
825
826 static const struct avrcp_control_cfm control_cfm = {
827         .get_capabilities = handle_get_capabilities_rsp,
828         .register_notification = handle_register_notification_rsp,
829         .set_volume = handle_set_volume_rsp,
830 };
831
832 static int avrcp_device_add_session(struct avrcp_device *dev, int fd,
833                                                 uint16_t imtu, uint16_t omtu)
834 {
835         struct hal_ev_avrcp_remote_features ev;
836         char address[18];
837
838         dev->session = avrcp_new(fd, imtu, omtu, dev->version);
839         if (!dev->session)
840                 return -EINVAL;
841
842         avrcp_set_destroy_cb(dev->session, disconnect_cb, dev);
843         avrcp_set_passthrough_handlers(dev->session, passthrough_handlers,
844                                                                         dev);
845         avrcp_register_player(dev->session, &control_ind, &control_cfm, dev);
846
847         dev->queue = g_queue_new();
848
849         ba2str(&dev->dst, address);
850
851         /* FIXME: get the real name of the device */
852         avrcp_init_uinput(dev->session, "bluetooth", address);
853
854         bdaddr2android(&dev->dst, ev.bdaddr);
855         ev.features = HAL_AVRCP_FEATURE_NONE;
856
857         DBG("version 0x%02x", dev->version);
858
859         if (dev->version < 0x0103)
860                 goto done;
861
862         ev.features |= HAL_AVRCP_FEATURE_METADATA;
863
864         if (dev->version < 0x0104)
865                 goto done;
866
867         ev.features |= HAL_AVRCP_FEATURE_ABSOLUTE_VOLUME;
868
869         avrcp_get_capabilities(dev->session, CAP_EVENTS_SUPPORTED);
870
871 done:
872         ipc_send_notif(hal_ipc, HAL_SERVICE_ID_AVRCP,
873                                         HAL_EV_AVRCP_REMOTE_FEATURES,
874                                         sizeof(ev), &ev);
875
876         return 0;
877 }
878
879 static void connect_cb(GIOChannel *chan, GError *err, gpointer user_data)
880 {
881         struct avrcp_device *dev = user_data;
882         uint16_t imtu, omtu;
883         char address[18];
884         GError *gerr = NULL;
885         int fd;
886
887         if (err) {
888                 error("%s", err->message);
889                 return;
890         }
891
892         bt_io_get(chan, &gerr,
893                         BT_IO_OPT_DEST, address,
894                         BT_IO_OPT_IMTU, &imtu,
895                         BT_IO_OPT_OMTU, &omtu,
896                         BT_IO_OPT_INVALID);
897         if (gerr) {
898                 error("%s", gerr->message);
899                 g_error_free(gerr);
900                 g_io_channel_shutdown(chan, TRUE, NULL);
901                 return;
902         }
903
904         fd = g_io_channel_unix_get_fd(chan);
905         if (avrcp_device_add_session(dev, fd, imtu, omtu) < 0) {
906                 avrcp_device_free(dev);
907                 return;
908         }
909
910         g_io_channel_set_close_on_unref(chan, FALSE);
911
912         if (dev->io) {
913                 g_io_channel_unref(dev->io);
914                 dev->io = NULL;
915         }
916
917         DBG("%s connected", address);
918 }
919
920 static bool avrcp_device_connect(struct avrcp_device *dev, BtIOConnect cb)
921 {
922         GError *err = NULL;
923
924         dev->io = bt_io_connect(cb, dev, NULL, &err,
925                                         BT_IO_OPT_SOURCE_BDADDR, &adapter_addr,
926                                         BT_IO_OPT_DEST_BDADDR, &dev->dst,
927                                         BT_IO_OPT_PSM, L2CAP_PSM_AVCTP,
928                                         BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM,
929                                         BT_IO_OPT_INVALID);
930         if (err) {
931                 error("%s", err->message);
932                 g_error_free(err);
933                 return false;
934         }
935
936         return true;
937 }
938
939 static void search_cb(sdp_list_t *recs, int err, gpointer data)
940 {
941         struct avrcp_device *dev = data;
942         sdp_list_t *list;
943
944         DBG("");
945
946         if (!g_slist_find(devices, dev))
947                 return;
948
949         if (err < 0) {
950                 error("Unable to get AV_REMOTE_SVCLASS_ID SDP record: %s",
951                                                         strerror(-err));
952                 goto fail;
953         }
954
955         if (!recs || !recs->data) {
956                 error("No AVRCP records found");
957                 goto fail;
958         }
959
960         for (list = recs; list; list = list->next) {
961                 sdp_record_t *rec = list->data;
962                 sdp_list_t *l;
963                 sdp_profile_desc_t *desc;
964                 int features;
965
966                 if (sdp_get_profile_descs(rec, &l) < 0)
967                         continue;
968
969                 desc = l->data;
970                 dev->version = desc->version;
971
972                 if (sdp_get_int_attr(rec, SDP_ATTR_SUPPORTED_FEATURES,
973                                                         &features) == 0)
974                         dev->features = features;
975
976                 sdp_list_free(l, free);
977                 break;
978         }
979
980         if (dev->io) {
981                 GError *gerr = NULL;
982                 if (!bt_io_accept(dev->io, connect_cb, dev, NULL, &gerr)) {
983                         error("bt_io_accept: %s", gerr->message);
984                         g_error_free(gerr);
985                         goto fail;
986                 }
987                 return;
988         }
989
990         if (!avrcp_device_connect(dev, connect_cb)) {
991                 error("Unable to connect to AVRCP");
992                 goto fail;
993         }
994
995         return;
996
997 fail:
998         avrcp_device_remove(dev);
999 }
1000
1001 static int avrcp_device_search(struct avrcp_device *dev)
1002 {
1003         uuid_t uuid;
1004
1005         sdp_uuid16_create(&uuid, AV_REMOTE_SVCLASS_ID);
1006
1007         return bt_search_service(&adapter_addr, &dev->dst, &uuid, search_cb,
1008                                                                 dev, NULL, 0);
1009 }
1010
1011 static void confirm_cb(GIOChannel *chan, gpointer data)
1012 {
1013         struct avrcp_device *dev;
1014         char address[18];
1015         bdaddr_t dst;
1016         GError *err = NULL;
1017
1018         bt_io_get(chan, &err,
1019                         BT_IO_OPT_DEST_BDADDR, &dst,
1020                         BT_IO_OPT_DEST, address,
1021                         BT_IO_OPT_INVALID);
1022         if (err) {
1023                 error("%s", err->message);
1024                 g_error_free(err);
1025                 g_io_channel_shutdown(chan, TRUE, NULL);
1026                 return;
1027         }
1028
1029         DBG("incoming connect from %s", address);
1030
1031         dev = avrcp_device_find(&dst);
1032         if (dev && dev->session) {
1033                 error("AVRCP: Refusing unexpected connect");
1034                 g_io_channel_shutdown(chan, TRUE, NULL);
1035                 return;
1036         }
1037
1038         dev = avrcp_device_new(&dst);
1039         if (avrcp_device_search(dev) < 0) {
1040                 error("AVRCP: Failed to search SDP details");
1041                 avrcp_device_free(dev);
1042                 g_io_channel_shutdown(chan, TRUE, NULL);
1043         }
1044
1045         dev->io = g_io_channel_ref(chan);
1046 }
1047
1048 bool bt_avrcp_register(struct ipc *ipc, const bdaddr_t *addr, uint8_t mode)
1049 {
1050         GError *err = NULL;
1051         sdp_record_t *rec;
1052
1053         DBG("");
1054
1055         bacpy(&adapter_addr, addr);
1056
1057         server = bt_io_listen(NULL, confirm_cb, NULL, NULL, &err,
1058                                 BT_IO_OPT_SOURCE_BDADDR, &adapter_addr,
1059                                 BT_IO_OPT_PSM, L2CAP_PSM_AVCTP,
1060                                 BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM,
1061                                 BT_IO_OPT_INVALID);
1062         if (!server) {
1063                 error("Failed to listen on AVDTP channel: %s", err->message);
1064                 g_error_free(err);
1065                 return false;
1066         }
1067
1068         rec = avrcp_tg_record();
1069         if (!rec) {
1070                 error("Failed to allocate AVRCP TG record");
1071                 goto fail;
1072         }
1073
1074         if (bt_adapter_add_record(rec, 0) < 0) {
1075                 error("Failed to register AVRCP TG record");
1076                 sdp_record_free(rec);
1077                 goto fail;
1078         }
1079         record_tg_id = rec->handle;
1080
1081         rec = avrcp_ct_record();
1082         if (!rec) {
1083                 error("Failed to allocate AVRCP CT record");
1084                 bt_adapter_remove_record(record_tg_id);
1085                 goto fail;
1086         }
1087
1088         if (bt_adapter_add_record(rec, 0) < 0) {
1089                 error("Failed to register AVRCP CT record");
1090                 bt_adapter_remove_record(record_tg_id);
1091                 sdp_record_free(rec);
1092                 goto fail;
1093         }
1094         record_ct_id = rec->handle;
1095
1096         hal_ipc = ipc;
1097
1098         ipc_register(hal_ipc, HAL_SERVICE_ID_AVRCP, cmd_handlers,
1099                                                 G_N_ELEMENTS(cmd_handlers));
1100
1101         return true;
1102 fail:
1103         g_io_channel_shutdown(server, TRUE, NULL);
1104         g_io_channel_unref(server);
1105         server = NULL;
1106
1107         return false;
1108 }
1109
1110 void bt_avrcp_unregister(void)
1111 {
1112         DBG("");
1113
1114         g_slist_free_full(devices, avrcp_device_free);
1115         devices = NULL;
1116
1117         ipc_unregister(hal_ipc, HAL_SERVICE_ID_AVRCP);
1118         hal_ipc = NULL;
1119
1120         bt_adapter_remove_record(record_tg_id);
1121         record_tg_id = 0;
1122
1123         bt_adapter_remove_record(record_ct_id);
1124         record_ct_id = 0;
1125
1126         if (server) {
1127                 g_io_channel_shutdown(server, TRUE, NULL);
1128                 g_io_channel_unref(server);
1129                 server = NULL;
1130         }
1131 }
1132
1133 void bt_avrcp_connect(const bdaddr_t *dst)
1134 {
1135         struct avrcp_device *dev;
1136         char addr[18];
1137
1138         DBG("");
1139
1140         if (avrcp_device_find(dst))
1141                 return;
1142
1143         dev = avrcp_device_new(dst);
1144         if (avrcp_device_search(dev) < 0) {
1145                 error("AVRCP: Failed to search SDP details");
1146                 avrcp_device_free(dev);
1147         }
1148
1149         ba2str(&dev->dst, addr);
1150         DBG("connecting to %s", addr);
1151 }
1152
1153 void bt_avrcp_disconnect(const bdaddr_t *dst)
1154 {
1155         struct avrcp_device *dev;
1156
1157         DBG("");
1158
1159         dev = avrcp_device_find(dst);
1160         if (!dev)
1161                 return;
1162
1163         if (dev->session) {
1164                 avrcp_shutdown(dev->session);
1165                 return;
1166         }
1167
1168         avrcp_device_remove(dev);
1169 }