Blob: ccp.c
Blob id: 069c880b18c26729744e826754453af9dfae68e7
Size: 28.4 KB
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 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 | // SPDX-License-Identifier: LGPL-2.1-or-later /* * * BlueZ - Bluetooth protocol stack for Linux * * Copyright (C) 2022 Intel Corporation. All rights reserved. * */ #define _GNU_SOURCE #include <inttypes.h> #include <string.h> #include <stdlib.h> #include <stdbool.h> #include <unistd.h> #include <errno.h> #include "bluetooth/bluetooth.h" #include "bluetooth/uuid.h" #include "bluetooth/hci.h" #include "src/shared/queue.h" #include "src/shared/util.h" #include "src/shared/timeout.h" #include "src/shared/att.h" #include "src/shared/gatt-db.h" #include "src/shared/gatt-server.h" #include "src/shared/gatt-client.h" #include "src/shared/ccp.h" #define DBG(_ccp, fmt, arg...) \ ccp_debug(_ccp, "%s:%s() " fmt, __FILE__, __func__, ## arg) struct bt_ccp_db { struct gatt_db *db; struct bt_ccs *ccs; }; struct bt_ccp_pending { unsigned int id; struct bt_ccp *ccp; bt_gatt_client_read_callback_t func; void *user_data; }; struct event_callback { const struct bt_ccp_event_callback *cbs; void *user_data; }; struct bt_ccp { int ref_count; struct bt_gatt_client *client; struct bt_ccp_db *ldb; struct bt_ccp_db *rdb; unsigned int bearer_name_id; unsigned int bearer_uci_id; unsigned int bearer_technology_id; unsigned int bearer_uri_schemes_list_id; unsigned int signal_strength_id; unsigned int signal_reporting_intrvl_id; unsigned int current_call_list_id; unsigned int ccid_id; unsigned int status_flag_id; unsigned int target_bearer_uri_id; unsigned int call_state_id; unsigned int call_control_pt_id; unsigned int call_control_opt_opcode_id; unsigned int termination_reason_id; unsigned int incoming_call_id; unsigned int friendly_name_id; struct event_callback *cb; struct queue *pending; bt_ccp_debug_func_t debug_func; bt_ccp_destroy_func_t debug_destroy; void *debug_data; void *user_data; }; struct bt_ccs { struct bt_ccp_db *mdb; struct gatt_db_attribute *service; struct gatt_db_attribute *bearer_name; struct gatt_db_attribute *bearer_name_ccc; struct gatt_db_attribute *bearer_uci; struct gatt_db_attribute *bearer_technology; struct gatt_db_attribute *bearer_technology_ccc; struct gatt_db_attribute *bearer_uri_schemes_list; struct gatt_db_attribute *signal_strength; struct gatt_db_attribute *signal_strength_ccc; struct gatt_db_attribute *signal_reporting_intrvl; struct gatt_db_attribute *current_call_list; struct gatt_db_attribute *current_call_list_ccc; struct gatt_db_attribute *ccid; struct gatt_db_attribute *status_flag; struct gatt_db_attribute *status_flag_ccc; struct gatt_db_attribute *target_bearer_uri; struct gatt_db_attribute *call_state; struct gatt_db_attribute *call_state_ccc; struct gatt_db_attribute *call_ctrl_point; struct gatt_db_attribute *call_ctrl_point_ccc; struct gatt_db_attribute *call_ctrl_opt_opcode; struct gatt_db_attribute *termination_reason; struct gatt_db_attribute *termination_reason_ccc; struct gatt_db_attribute *incoming_call; struct gatt_db_attribute *incoming_call_ccc; struct gatt_db_attribute *friendly_name; struct gatt_db_attribute *friendly_name_ccc; }; static struct queue *ccp_db; static void ccp_debug(struct bt_ccp *ccp, const char *format, ...) { va_list ap; if (!ccp || !format || !ccp->debug_func) return; va_start(ap, format); util_debug_va(ccp->debug_func, ccp->debug_data, format, ap); va_end(ap); } static bool ccp_db_match(const void *data, const void *match_data) { const struct bt_ccp_db *mdb = data; const struct gatt_db *db = match_data; return (mdb->db == db); } static void ccp_db_free(void *data) { struct bt_ccp_db *bdb = data; if (!bdb) return; gatt_db_unref(bdb->db); free(bdb->ccs); free(bdb); } static void ccp_free(void *data) { struct bt_ccp *ccp = data; DBG(ccp, ""); bt_ccp_detach(ccp); ccp_db_free(ccp->rdb); queue_destroy(ccp->pending, NULL); free(ccp); } struct bt_ccp *bt_ccp_ref(struct bt_ccp *ccp) { if (!ccp) return NULL; __sync_fetch_and_add(&ccp->ref_count, 1); return ccp; } void bt_ccp_unref(struct bt_ccp *ccp) { if (!ccp) return; if (__sync_sub_and_fetch(&ccp->ref_count, 1)) return; ccp_free(ccp); } bool bt_ccp_set_user_data(struct bt_ccp *ccp, void *user_data) { if (!ccp) return false; ccp->user_data = user_data; return true; } void *bt_ccp_get_user_data(struct bt_ccp *ccp) { if (!ccp) return NULL; return ccp->user_data; } bool bt_ccp_set_debug(struct bt_ccp *ccp, bt_ccp_debug_func_t func, void *user_data, bt_ccp_destroy_func_t destroy) { if (!ccp) return false; if (ccp->debug_destroy) ccp->debug_destroy(ccp->debug_data); ccp->debug_func = func; ccp->debug_destroy = destroy; ccp->debug_data = user_data; return true; } static void ccs_call_state_read(struct gatt_db_attribute *attrib, unsigned int id, uint16_t offset, uint8_t opcode, struct bt_att *att, void *user_data) { int call_state = 0; struct iovec iov; iov.iov_base = &call_state; iov.iov_len = sizeof(int); gatt_db_attribute_read_result(attrib, id, 0, iov.iov_base, iov.iov_len); } static void ccs_call_state_write(struct gatt_db_attribute *attrib, unsigned int id, uint16_t offset, const uint8_t *value, size_t len, uint8_t opcode, struct bt_att *att, void *user_data) { gatt_db_attribute_write_result(attrib, id, BT_ATT_ERROR_INSUFFICIENT_RESOURCES); } static struct bt_ccs *ccs_new(struct gatt_db *db) { struct bt_ccs *ccs; bt_uuid_t uuid; if (!db) return NULL; ccs = new0(struct bt_ccs, 1); /* Populate DB with ccs attributes */ bt_uuid16_create(&uuid, GTBS_UUID); ccs->service = gatt_db_add_service(db, &uuid, true, 42); bt_uuid16_create(&uuid, BEARER_PROVIDER_NAME_CHRC_UUID); ccs->bearer_name = gatt_db_service_add_characteristic(ccs->service, &uuid, BT_ATT_PERM_READ, BT_GATT_CHRC_PROP_READ | BT_GATT_CHRC_PROP_NOTIFY, ccs_call_state_read, NULL, ccs); ccs->bearer_name_ccc = gatt_db_service_add_ccc(ccs->service, BT_ATT_PERM_READ | BT_ATT_PERM_WRITE); bt_uuid16_create(&uuid, BEARER_UCI_CHRC_UUID); ccs->bearer_uci = gatt_db_service_add_characteristic(ccs->service, &uuid, BT_ATT_PERM_READ, BT_GATT_CHRC_PROP_READ, ccs_call_state_read, NULL, ccs); bt_uuid16_create(&uuid, BEARER_TECH_CHRC_UUID); ccs->bearer_technology = gatt_db_service_add_characteristic(ccs->service, &uuid, BT_ATT_PERM_READ, BT_GATT_CHRC_PROP_READ | BT_GATT_CHRC_PROP_NOTIFY, ccs_call_state_read, NULL, ccs); ccs->bearer_technology_ccc = gatt_db_service_add_ccc(ccs->service, BT_ATT_PERM_READ | BT_ATT_PERM_WRITE); bt_uuid16_create(&uuid, BEARER_URI_SCHEME_CHRC_UUID); ccs->bearer_uri_schemes_list = gatt_db_service_add_characteristic(ccs->service, &uuid, BT_ATT_PERM_READ, BT_GATT_CHRC_PROP_READ, ccs_call_state_read, NULL, ccs); bt_uuid16_create(&uuid, BEARER_SIGNAL_STR_CHRC_UUID); ccs->signal_strength = gatt_db_service_add_characteristic(ccs->service, &uuid, BT_ATT_PERM_READ, BT_GATT_CHRC_PROP_READ | BT_GATT_CHRC_PROP_NOTIFY, ccs_call_state_read, NULL, ccs); ccs->signal_strength_ccc = gatt_db_service_add_ccc(ccs->service, BT_ATT_PERM_READ | BT_ATT_PERM_WRITE); bt_uuid16_create(&uuid, BEARER_SIGNAL_INTRVL_CHRC_UUID); ccs->signal_reporting_intrvl = gatt_db_service_add_characteristic(ccs->service, &uuid, BT_ATT_PERM_READ | BT_ATT_PERM_WRITE, BT_GATT_CHRC_PROP_READ | BT_GATT_CHRC_PROP_WRITE | BT_GATT_CHRC_PROP_WRITE_WITHOUT_RESP, ccs_call_state_read, ccs_call_state_write, ccs); bt_uuid16_create(&uuid, CURR_CALL_LIST_CHRC_UUID); ccs->current_call_list = gatt_db_service_add_characteristic(ccs->service, &uuid, BT_ATT_PERM_READ, BT_GATT_CHRC_PROP_READ | BT_GATT_CHRC_PROP_NOTIFY, ccs_call_state_read, NULL, ccs); ccs->current_call_list_ccc = gatt_db_service_add_ccc(ccs->service, BT_ATT_PERM_READ | BT_ATT_PERM_WRITE); bt_uuid16_create(&uuid, BEARER_CCID_CHRC_UUID); ccs->ccid = gatt_db_service_add_characteristic(ccs->service, &uuid, BT_ATT_PERM_READ, BT_GATT_CHRC_PROP_READ, ccs_call_state_read, NULL, ccs); bt_uuid16_create(&uuid, CALL_STATUS_FLAG_CHRC_UUID); ccs->status_flag = gatt_db_service_add_characteristic(ccs->service, &uuid, BT_ATT_PERM_READ, BT_GATT_CHRC_PROP_READ | BT_GATT_CHRC_PROP_NOTIFY, ccs_call_state_read, NULL, ccs); ccs->status_flag_ccc = gatt_db_service_add_ccc(ccs->service, BT_ATT_PERM_READ | BT_ATT_PERM_WRITE); bt_uuid16_create(&uuid, INCOM_CALL_TARGET_URI_CHRC_UUID); ccs->target_bearer_uri = gatt_db_service_add_characteristic(ccs->service, &uuid, BT_ATT_PERM_READ, BT_GATT_CHRC_PROP_READ, ccs_call_state_read, NULL, ccs); bt_uuid16_create(&uuid, CALL_STATE_CHRC_UUID); ccs->call_ctrl_point = gatt_db_service_add_characteristic(ccs->service, &uuid, BT_ATT_PERM_READ, BT_GATT_CHRC_PROP_READ | BT_GATT_CHRC_PROP_NOTIFY, ccs_call_state_read, NULL, ccs); ccs->call_ctrl_point_ccc = gatt_db_service_add_ccc(ccs->service, BT_ATT_PERM_READ | BT_ATT_PERM_WRITE); bt_uuid16_create(&uuid, CALL_CTRL_POINT_CHRC_UUID); ccs->call_ctrl_opt_opcode = gatt_db_service_add_characteristic(ccs->service, &uuid, BT_ATT_PERM_WRITE, BT_GATT_CHRC_PROP_WRITE | BT_GATT_CHRC_PROP_WRITE_WITHOUT_RESP | BT_GATT_CHRC_PROP_NOTIFY, NULL, ccs_call_state_write, ccs); bt_uuid16_create(&uuid, CALL_CTRL_POINT_OPT_OPCODE_CHRC_UUID); ccs->call_ctrl_opt_opcode = gatt_db_service_add_characteristic(ccs->service, &uuid, BT_ATT_PERM_READ, BT_GATT_CHRC_PROP_READ, ccs_call_state_read, NULL, ccs); bt_uuid16_create(&uuid, TERMINATION_REASON_CHRC_UUID); ccs->termination_reason = gatt_db_service_add_characteristic(ccs->service, &uuid, BT_ATT_PERM_READ, BT_GATT_CHRC_PROP_READ | BT_GATT_CHRC_PROP_NOTIFY, ccs_call_state_read, NULL, ccs); bt_uuid16_create(&uuid, INCOMING_CALL_CHRC_UUID); ccs->incoming_call = gatt_db_service_add_characteristic(ccs->service, &uuid, BT_ATT_PERM_NONE, BT_GATT_CHRC_PROP_NOTIFY, NULL, NULL, ccs); ccs->incoming_call_ccc = gatt_db_service_add_ccc(ccs->service, BT_ATT_PERM_READ | BT_ATT_PERM_WRITE); bt_uuid16_create(&uuid, CALL_FRIENDLY_NAME_CHRC_UUID); ccs->friendly_name = gatt_db_service_add_characteristic(ccs->service, &uuid, BT_ATT_PERM_READ, BT_GATT_CHRC_PROP_READ | BT_GATT_CHRC_PROP_NOTIFY, ccs_call_state_read, NULL, ccs); ccs->friendly_name_ccc = gatt_db_service_add_ccc(ccs->service, BT_ATT_PERM_READ | BT_ATT_PERM_WRITE); gatt_db_service_set_active(ccs->service, false); return ccs; } static struct bt_ccs *ccp_get_ccs(struct bt_ccp *ccp) { if (!ccp) return NULL; if (ccp->rdb->ccs) return ccp->rdb->ccs; ccp->rdb->ccs = new0(struct bt_ccs, 1); ccp->rdb->ccs->mdb = ccp->rdb; return ccp->rdb->ccs; } static void ccp_pending_destroy(void *data) { struct bt_ccp_pending *pending = data; struct bt_ccp *ccp = pending->ccp; queue_remove_if(ccp->pending, NULL, pending); } static void ccp_pending_complete(bool success, uint8_t att_ecode, const uint8_t *value, uint16_t length, void *user_data) { struct bt_ccp_pending *pending = user_data; if (pending->func) pending->func(success, att_ecode, value, length, pending->user_data); } static void ccp_read_value(struct bt_ccp *ccp, uint16_t value_handle, bt_gatt_client_read_callback_t func, void *user_data) { struct bt_ccp_pending *pending; pending = new0(struct bt_ccp_pending, 1); pending->ccp = ccp; pending->func = func; pending->user_data = user_data; pending->id = bt_gatt_client_read_value(ccp->client, value_handle, ccp_pending_complete, pending, ccp_pending_destroy); if (!pending->id) { DBG(ccp, "Unable to send Read request"); free(pending); return; } queue_push_tail(ccp->pending, pending); } static void read_call_back(bool success, uint8_t att_ecode, const uint8_t *value, uint16_t length, void *user_data) { struct bt_ccp *ccp = user_data; DBG(ccp, ""); if (!success) { DBG(ccp, "Unable to read call state: error 0x%02x", att_ecode); return; } } static void ccp_cb_register(uint16_t att_ecode, void *user_data) { struct bt_ccp *ccp = user_data; if (att_ecode) DBG(ccp, "ccp cb notification failed: 0x%04x", att_ecode); /* TODO: generic handler for non-mandatory CCP call backs */ } static void ccp_cb_notify(uint16_t value_handle, const uint8_t *value, uint16_t length, void *user_data) { /* TODO: generic handler for non-mandatory CCP notifications */ } static void ccp_cb_status_flag_register(uint16_t att_ecode, void *user_data) { struct bt_ccp *ccp = user_data; if (att_ecode) DBG(ccp, "ccp cb notification failed: 0x%04x", att_ecode); } static void ccp_cb_status_flag_notify(uint16_t value_handle, const uint8_t *value, uint16_t length, void *user_data) { struct bt_ccp *ccp = user_data; DBG(ccp, ""); if (!length) return; } static void ccp_cb_terminate_register(uint16_t att_ecode, void *user_data) { struct bt_ccp *ccp = user_data; if (att_ecode) DBG(ccp, "ccp cb notification failed: 0x%04x", att_ecode); } static void ccp_cb_terminate_notify(uint16_t value_handle, const uint8_t *value, uint16_t length, void *user_data) { struct bt_ccp *ccp = user_data; DBG(ccp, ""); if (!length) return; /* TODO: update call state in Local context */ } static void ccp_cb_bearer_name_register(uint16_t att_ecode, void *user_data) { struct bt_ccp *ccp = user_data; DBG(ccp, ""); if (att_ecode) DBG(ccp, "ccp cb notification failed: 0x%04x", att_ecode); } static void ccp_cb_bearer_name_notify(uint16_t value_handle, const uint8_t *value, uint16_t length, void *user_data) { struct bt_ccp *ccp = user_data; DBG(ccp, ""); if (!length) return; /* TODO: update call details in Local context */ } static void ccp_cb_call_list_register(uint16_t att_ecode, void *user_data) { struct bt_ccp *ccp = user_data; DBG(ccp, ""); if (att_ecode) DBG(ccp, "ccp cb notification failed: 0x%04x", att_ecode); } static void ccp_cb_call_list_notify(uint16_t value_handle, const uint8_t *value, uint16_t length, void *user_data) { struct bt_ccp *ccp = user_data; DBG(ccp, ""); if (!length) return; /* TODO: update call list in Local context */ } static void ccp_cb_call_state_register(uint16_t att_ecode, void *user_data) { struct bt_ccp *ccp = user_data; DBG(ccp, ""); if (att_ecode) DBG(ccp, "ccp cb notification failed: 0x%04x", att_ecode); } static void ccp_cb_call_state_notify(uint16_t value_handle, const uint8_t *value, uint16_t length, void *user_data) { struct bt_ccp *ccp = user_data; DBG(ccp, ""); if (!length) return; /* TODO: update call state in Local context */ } static void ccp_cb_incom_call_register(uint16_t att_ecode, void *user_data) { struct bt_ccp *ccp = user_data; DBG(ccp, ""); if (att_ecode) DBG(ccp, "ccp cb notification failed: 0x%04x", att_ecode); } static void ccp_cb_incom_call_notify(uint16_t value_handle, const uint8_t *value, uint16_t length, void *user_data) { struct bt_ccp *ccp = user_data; DBG(ccp, ""); if (!length) return; /* TODO: Handle incoming call notofiation, Answer/reject etc */ } static void bt_ccp_incom_call_attach(struct bt_ccp *ccp) { uint16_t value_handle; struct bt_ccs *ccs = ccp_get_ccs(ccp); DBG(ccp, ""); if (!gatt_db_attribute_get_char_data(ccs->incoming_call, NULL, &value_handle, NULL, NULL, NULL)) return; ccp_read_value(ccp, value_handle, read_call_back, ccp); ccp->incoming_call_id = bt_gatt_client_register_notify(ccp->client, value_handle, ccp_cb_incom_call_register, ccp_cb_incom_call_notify, ccp, NULL); } static void bt_ccp_call_state_attach(struct bt_ccp *ccp) { uint16_t value_handle; struct bt_ccs *ccs = ccp_get_ccs(ccp); DBG(ccp, ""); if (!gatt_db_attribute_get_char_data(ccs->call_state, NULL, &value_handle, NULL, NULL, NULL)) return; ccp_read_value(ccp, value_handle, read_call_back, ccp); ccp->call_state_id = bt_gatt_client_register_notify(ccp->client, value_handle, ccp_cb_call_state_register, ccp_cb_call_state_notify, ccp, NULL); } static void bt_ccp_call_list_attach(struct bt_ccp *ccp) { uint16_t value_handle; struct bt_ccs *ccs = ccp_get_ccs(ccp); DBG(ccp, ""); if (!gatt_db_attribute_get_char_data(ccs->current_call_list, NULL, &value_handle, NULL, NULL, NULL)) return; ccp_read_value(ccp, value_handle, read_call_back, ccp); ccp->current_call_list_id = bt_gatt_client_register_notify(ccp->client, value_handle, ccp_cb_call_list_register, ccp_cb_call_list_notify, ccp, NULL); } static void bt_ccp_name_attach(struct bt_ccp *ccp) { uint16_t value_handle; struct bt_ccs *ccs = ccp_get_ccs(ccp); DBG(ccp, ""); if (!gatt_db_attribute_get_char_data(ccs->bearer_name, NULL, &value_handle, NULL, NULL, NULL)) return; ccp_read_value(ccp, value_handle, read_call_back, ccp); ccp->bearer_name_id = bt_gatt_client_register_notify(ccp->client, value_handle, ccp_cb_bearer_name_register, ccp_cb_bearer_name_notify, ccp, NULL); } static void bt_ccp_term_reason_attach(struct bt_ccp *ccp) { uint16_t value_handle; struct bt_ccs *ccs = ccp_get_ccs(ccp); DBG(ccp, ""); if (!gatt_db_attribute_get_char_data(ccs->termination_reason, NULL, &value_handle, NULL, NULL, NULL)) return; ccp_read_value(ccp, value_handle, read_call_back, ccp); ccp->termination_reason_id = bt_gatt_client_register_notify(ccp->client, value_handle, ccp_cb_terminate_register, ccp_cb_terminate_notify, ccp, NULL); } static void bt_ccp_status_attach(struct bt_ccp *ccp) { uint16_t value_handle; struct bt_ccs *ccs = ccp_get_ccs(ccp); DBG(ccp, ""); if (!gatt_db_attribute_get_char_data(ccs->status_flag, NULL, &value_handle, NULL, NULL, NULL)) return; ccp_read_value(ccp, value_handle, read_call_back, ccp); ccp->status_flag_id = bt_gatt_client_register_notify(ccp->client, value_handle, ccp_cb_status_flag_register, ccp_cb_status_flag_notify, ccp, NULL); } static void bt_ccp_uci_attach(struct bt_ccp *ccp) { uint16_t value_handle; struct bt_ccs *ccs = ccp_get_ccs(ccp); DBG(ccp, ""); if (!gatt_db_attribute_get_char_data(ccs->bearer_uci, NULL, &value_handle, NULL, NULL, NULL)) return; ccp_read_value(ccp, value_handle, read_call_back, ccp); ccp->bearer_uci_id = bt_gatt_client_register_notify(ccp->client, value_handle, ccp_cb_register, ccp_cb_notify, ccp, NULL); } static void bt_ccp_technology_attach(struct bt_ccp *ccp) { uint16_t value_handle; struct bt_ccs *ccs = ccp_get_ccs(ccp); DBG(ccp, ""); if (!gatt_db_attribute_get_char_data(ccs->bearer_technology, NULL, &value_handle, NULL, NULL, NULL)) return; ccp_read_value(ccp, value_handle, read_call_back, ccp); ccp->bearer_technology_id = bt_gatt_client_register_notify(ccp->client, value_handle, ccp_cb_register, ccp_cb_notify, ccp, NULL); } static void bt_ccp_strength_attach(struct bt_ccp *ccp) { uint16_t value_handle; struct bt_ccs *ccs = ccp_get_ccs(ccp); DBG(ccp, ""); if (!gatt_db_attribute_get_char_data(ccs->signal_strength, NULL, &value_handle, NULL, NULL, NULL)) return; ccp_read_value(ccp, value_handle, read_call_back, ccp); ccp->signal_strength_id = bt_gatt_client_register_notify(ccp->client, value_handle, ccp_cb_register, ccp_cb_notify, ccp, NULL); } static void bt_ccp_ccid_attach(struct bt_ccp *ccp) { uint16_t value_handle; struct bt_ccs *ccs = ccp_get_ccs(ccp); DBG(ccp, ""); if (!gatt_db_attribute_get_char_data(ccs->ccid, NULL, &value_handle, NULL, NULL, NULL)) return; ccp_read_value(ccp, value_handle, read_call_back, ccp); ccp->ccid_id = bt_gatt_client_register_notify(ccp->client, value_handle, ccp_cb_register, ccp_cb_notify, ccp, NULL); } static void bt_ccp_tar_uri_attach(struct bt_ccp *ccp) { uint16_t value_handle; struct bt_ccs *ccs = ccp_get_ccs(ccp); DBG(ccp, ""); if (!gatt_db_attribute_get_char_data(ccs->target_bearer_uri, NULL, &value_handle, NULL, NULL, NULL)) return; ccp_read_value(ccp, value_handle, read_call_back, ccp); ccp->target_bearer_uri_id = bt_gatt_client_register_notify(ccp->client, value_handle, ccp_cb_register, ccp_cb_notify, ccp, NULL); } static void bt_ccp_ctrl_point_attach(struct bt_ccp *ccp) { uint16_t value_handle; struct bt_ccs *ccs = ccp_get_ccs(ccp); DBG(ccp, ""); if (!gatt_db_attribute_get_char_data(ccs->call_ctrl_point, NULL, &value_handle, NULL, NULL, NULL)) return; ccp_read_value(ccp, value_handle, read_call_back, ccp); ccp->call_control_pt_id = bt_gatt_client_register_notify(ccp->client, value_handle, ccp_cb_register, ccp_cb_notify, ccp, NULL); } static void bt_ccp_ctrl_opcode_attach(struct bt_ccp *ccp) { uint16_t value_handle; struct bt_ccs *ccs = ccp_get_ccs(ccp); DBG(ccp, ""); if (!gatt_db_attribute_get_char_data(ccs->call_ctrl_opt_opcode, NULL, &value_handle, NULL, NULL, NULL)) return; ccp_read_value(ccp, value_handle, read_call_back, ccp); ccp->call_control_opt_opcode_id = bt_gatt_client_register_notify(ccp->client, value_handle, ccp_cb_register, ccp_cb_notify, ccp, NULL); } static void bt_ccp_friendly_name_attach(struct bt_ccp *ccp) { uint16_t value_handle; struct bt_ccs *ccs = ccp_get_ccs(ccp); DBG(ccp, ""); if (!gatt_db_attribute_get_char_data(ccs->friendly_name, NULL, &value_handle, NULL, NULL, NULL)) return; ccp_read_value(ccp, value_handle, read_call_back, ccp); ccp->friendly_name_id = bt_gatt_client_register_notify(ccp->client, value_handle, ccp_cb_register, ccp_cb_notify, ccp, NULL); } static void bt_ccp_signal_intrvl_attach(struct bt_ccp *ccp) { uint16_t value_handle; struct bt_ccs *ccs = ccp_get_ccs(ccp); DBG(ccp, ""); if (!gatt_db_attribute_get_char_data(ccs->signal_reporting_intrvl, NULL, &value_handle, NULL, NULL, NULL)) return; ccp_read_value(ccp, value_handle, read_call_back, ccp); ccp->signal_reporting_intrvl_id = bt_gatt_client_register_notify(ccp->client, value_handle, ccp_cb_register, ccp_cb_notify, ccp, NULL); } static void bt_ccp_uri_list_attach(struct bt_ccp *ccp) { uint16_t value_handle; struct bt_ccs *ccs = ccp_get_ccs(ccp); DBG(ccp, ""); if (!gatt_db_attribute_get_char_data(ccs->bearer_uri_schemes_list, NULL, &value_handle, NULL, NULL, NULL)) return; ccp_read_value(ccp, value_handle, read_call_back, ccp); ccp->bearer_uri_schemes_list_id = bt_gatt_client_register_notify(ccp->client, value_handle, ccp_cb_register, ccp_cb_notify, ccp, NULL); } static void foreach_ccs_char(struct gatt_db_attribute *attr, void *user_data) { struct bt_ccp *ccp = user_data; struct bt_ccs *ccs; uint16_t value_handle; bt_uuid_t uuid; if (!gatt_db_attribute_get_char_data(attr, NULL, &value_handle, NULL, NULL, &uuid)) return; ccs = ccp_get_ccs(ccp); if (!ccs || ccs->call_state) return; if (bt_uuid16_cmp(&uuid, BEARER_PROVIDER_NAME_CHRC_UUID)) { DBG(ccp, "Found Bearer Name, handle 0x%04x", value_handle); ccs->bearer_name = attr; bt_ccp_name_attach(ccp); } if (bt_uuid16_cmp(&uuid, BEARER_UCI_CHRC_UUID)) { DBG(ccp, "Found Bearer Uci, handle 0x%04x", value_handle); ccs->bearer_uci = attr; bt_ccp_uci_attach(ccp); } if (bt_uuid16_cmp(&uuid, BEARER_TECH_CHRC_UUID)) { DBG(ccp, "Found Bearer Technology, handle %x", value_handle); ccs->bearer_technology = attr; bt_ccp_technology_attach(ccp); } if (bt_uuid16_cmp(&uuid, BEARER_SIGNAL_STR_CHRC_UUID)) { DBG(ccp, "Found Signal Strength, handle 0x%04x", value_handle); ccs->signal_strength = attr; bt_ccp_strength_attach(ccp); } if (bt_uuid16_cmp(&uuid, BEARER_SIGNAL_INTRVL_CHRC_UUID)) { DBG(ccp, "Found Signal Interval, handle 0x%04x", value_handle); ccs->signal_reporting_intrvl = attr; bt_ccp_signal_intrvl_attach(ccp); } if (bt_uuid16_cmp(&uuid, CALL_STATUS_FLAG_CHRC_UUID)) { DBG(ccp, "Found Status Flag, handle 0x%04x", value_handle); ccs->status_flag = attr; bt_ccp_status_attach(ccp); } if (bt_uuid16_cmp(&uuid, BEARER_URI_SCHEME_CHRC_UUID)) { DBG(ccp, "Found URI Scheme, handle 0x%04x", value_handle); ccs->bearer_uri_schemes_list = attr; bt_ccp_uri_list_attach(ccp); } if (bt_uuid16_cmp(&uuid, CURR_CALL_LIST_CHRC_UUID)) { DBG(ccp, "Found Call List, handle 0x%04x", value_handle); ccs->current_call_list = attr; bt_ccp_call_list_attach(ccp); } if (bt_uuid16_cmp(&uuid, BEARER_CCID_CHRC_UUID)) { DBG(ccp, "Found CCID, handle 0x%04x", value_handle); ccs->ccid = attr; bt_ccp_ccid_attach(ccp); } if (bt_uuid16_cmp(&uuid, INCOM_CALL_TARGET_URI_CHRC_UUID)) { DBG(ccp, "Found Bearer Uri, handle 0x%04x", value_handle); ccs->target_bearer_uri = attr; bt_ccp_tar_uri_attach(ccp); } if (bt_uuid16_cmp(&uuid, CALL_CTRL_POINT_CHRC_UUID)) { DBG(ccp, "Found Control Point, handle 0x%04x", value_handle); ccs->call_ctrl_point = attr; bt_ccp_ctrl_point_attach(ccp); } if (bt_uuid16_cmp(&uuid, CALL_CTRL_POINT_OPT_OPCODE_CHRC_UUID)) { DBG(ccp, "Found Control opcode, handle 0x%04x", value_handle); ccs->call_ctrl_opt_opcode = attr; bt_ccp_ctrl_opcode_attach(ccp); } if (bt_uuid16_cmp(&uuid, TERMINATION_REASON_CHRC_UUID)) { DBG(ccp, "Found Termination Reason, handle %x", value_handle); ccs->termination_reason = attr; bt_ccp_term_reason_attach(ccp); } if (bt_uuid16_cmp(&uuid, INCOMING_CALL_CHRC_UUID)) { DBG(ccp, "Found Incoming call, handle 0x%04x", value_handle); ccs->incoming_call = attr; bt_ccp_incom_call_attach(ccp); } if (bt_uuid16_cmp(&uuid, CALL_FRIENDLY_NAME_CHRC_UUID)) { DBG(ccp, "Found Friendly name, handle 0x%04x", value_handle); ccs->friendly_name = attr; bt_ccp_friendly_name_attach(ccp); } } void bt_ccp_set_event_callbacks(struct bt_ccp *ccp, const struct bt_ccp_event_callback *cbs, void *user_data) { struct event_callback *cb; if (ccp->cb) free(ccp->cb); cb = new0(struct event_callback, 1); cb->cbs = cbs; cb->user_data = user_data; ccp->cb = cb; } static void foreach_ccs_service(struct gatt_db_attribute *attr, void *user_data) { struct bt_ccp *ccp = user_data; struct bt_ccs *ccs = ccp_get_ccs(ccp); ccs->service = attr; gatt_db_service_foreach_char(attr, foreach_ccs_char, ccp); } static struct bt_ccp_db *ccp_db_new(struct gatt_db *db) { struct bt_ccp_db *mdb; if (!db) return NULL; mdb = new0(struct bt_ccp_db, 1); mdb->db = gatt_db_ref(db); if (!ccp_db) ccp_db = queue_new(); queue_push_tail(ccp_db, mdb); mdb->ccs = ccs_new(db); return mdb; } static struct bt_ccp_db *ccp_get_db(struct gatt_db *db) { struct bt_ccp_db *mdb; mdb = queue_find(ccp_db, ccp_db_match, db); if (mdb) return mdb; return ccp_db_new(db); } struct bt_ccp *bt_ccp_new(struct gatt_db *ldb, struct gatt_db *rdb) { struct bt_ccp *ccp; struct bt_ccp_db *mdb; if (!ldb) return NULL; mdb = ccp_get_db(ldb); if (!mdb) return NULL; ccp = new0(struct bt_ccp, 1); ccp->ldb = mdb; ccp->pending = queue_new(); if (!rdb) goto done; mdb = new0(struct bt_ccp_db, 1); mdb->db = gatt_db_ref(rdb); ccp->rdb = mdb; done: bt_ccp_ref(ccp); return ccp; } void bt_ccp_register(struct gatt_db *db) { ccp_db_new(db); } bool bt_ccp_attach(struct bt_ccp *ccp, struct bt_gatt_client *client) { bt_uuid_t uuid; DBG(ccp, "ccp %p", ccp); ccp->client = bt_gatt_client_clone(client); if (!ccp->client) return false; if (ccp->rdb->ccs) { bt_ccp_call_state_attach(ccp); return true; } bt_uuid16_create(&uuid, GTBS_UUID); gatt_db_foreach_service(ccp->rdb->db, &uuid, foreach_ccs_service, ccp); return true; } void bt_ccp_detach(struct bt_ccp *ccp) { DBG(ccp, "%p", ccp); bt_gatt_client_unref(ccp->client); ccp->client = NULL; } |