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