Loading...
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 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 | /* att.c - Attribute protocol handling */ /* * Copyright (c) 2015-2016 Intel Corporation * * SPDX-License-Identifier: Apache-2.0 */ #include <zephyr.h> #include <string.h> #include <errno.h> #include <stdbool.h> #include <atomic.h> #include <misc/byteorder.h> #include <misc/util.h> #include <bluetooth/hci.h> #include <bluetooth/bluetooth.h> #include <bluetooth/uuid.h> #include <bluetooth/gatt.h> #include <bluetooth/hci_driver.h> #define BT_DBG_ENABLED IS_ENABLED(CONFIG_BT_DEBUG_ATT) #define LOG_MODULE_NAME bt_att #include "common/log.h" #include "hci_core.h" #include "conn_internal.h" #include "l2cap_internal.h" #include "smp.h" #include "att_internal.h" #include "gatt_internal.h" #define ATT_CHAN(_ch) CONTAINER_OF(_ch, struct bt_att, chan.chan) #define ATT_REQ(_node) CONTAINER_OF(_node, struct bt_att_req, node) #define BT_GATT_PERM_READ_MASK (BT_GATT_PERM_READ | \ BT_GATT_PERM_READ_ENCRYPT | \ BT_GATT_PERM_READ_AUTHEN) #define BT_GATT_PERM_WRITE_MASK (BT_GATT_PERM_WRITE | \ BT_GATT_PERM_WRITE_ENCRYPT | \ BT_GATT_PERM_WRITE_AUTHEN) #define BT_GATT_PERM_ENCRYPT_MASK (BT_GATT_PERM_READ_ENCRYPT | \ BT_GATT_PERM_WRITE_ENCRYPT) #define BT_GATT_PERM_AUTHEN_MASK (BT_GATT_PERM_READ_AUTHEN | \ BT_GATT_PERM_WRITE_AUTHEN) #define ATT_CMD_MASK 0x40 #define ATT_TIMEOUT K_SECONDS(30) typedef enum __packed { ATT_COMMAND, ATT_REQUEST, ATT_RESPONSE, ATT_NOTIFICATION, ATT_CONFIRMATION, ATT_INDICATION, ATT_UNKNOWN, } att_type_t; static att_type_t att_op_get_type(u8_t op); #if CONFIG_BT_ATT_PREPARE_COUNT > 0 struct bt_attr_data { u16_t handle; u16_t offset; }; /* Pool for incoming ATT packets */ NET_BUF_POOL_DEFINE(prep_pool, CONFIG_BT_ATT_PREPARE_COUNT, BT_ATT_MTU, sizeof(struct bt_attr_data), NULL); #endif /* CONFIG_BT_ATT_PREPARE_COUNT */ enum { ATT_PENDING_RSP, ATT_PENDING_CFM, ATT_DISCONNECTED, /* Total number of flags - must be at the end of the enum */ ATT_NUM_FLAGS, }; /* ATT channel specific context */ struct bt_att { /* The channel this context is associated with */ struct bt_l2cap_le_chan chan; ATOMIC_DEFINE(flags, ATT_NUM_FLAGS); struct bt_att_req *req; sys_slist_t reqs; struct k_delayed_work timeout_work; struct k_sem tx_sem; #if CONFIG_BT_ATT_PREPARE_COUNT > 0 struct k_fifo prep_queue; #endif }; static struct bt_att bt_req_pool[CONFIG_BT_MAX_CONN]; static struct bt_att_req cancel; static void att_req_destroy(struct bt_att_req *req) { BT_DBG("req %p", req); if (req->buf) { net_buf_unref(req->buf); } if (req->destroy) { req->destroy(req); } (void)memset(req, 0, sizeof(*req)); } static struct bt_att *att_get(struct bt_conn *conn) { struct bt_l2cap_chan *chan; chan = bt_l2cap_le_lookup_tx_cid(conn, BT_L2CAP_CID_ATT); __ASSERT(chan, "No ATT channel found"); return CONTAINER_OF(chan, struct bt_att, chan); } static void att_cfm_sent(struct bt_conn *conn) { struct bt_att *att = att_get(conn); BT_DBG("conn %p att %p", conn, att); #if defined(CONFIG_BT_ATT_ENFORCE_FLOW) atomic_clear_bit(att->flags, ATT_PENDING_CFM); #endif /* CONFIG_BT_ATT_ENFORCE_FLOW */ k_sem_give(&att->tx_sem); } static void att_rsp_sent(struct bt_conn *conn) { struct bt_att *att = att_get(conn); BT_DBG("conn %p att %p", conn, att); #if defined(CONFIG_BT_ATT_ENFORCE_FLOW) atomic_clear_bit(att->flags, ATT_PENDING_RSP); #endif /* CONFIG_BT_ATT_ENFORCE_FLOW */ k_sem_give(&att->tx_sem); } static void att_req_sent(struct bt_conn *conn) { struct bt_att *att = att_get(conn); BT_DBG("conn %p att %p att->req %p", conn, att, att->req); k_sem_give(&att->tx_sem); /* Start timeout work */ if (att->req) { k_delayed_work_submit(&att->timeout_work, ATT_TIMEOUT); } } static void att_pdu_sent(struct bt_conn *conn) { struct bt_att *att = att_get(conn); BT_DBG("conn %p att %p", conn, att); k_sem_give(&att->tx_sem); } static bt_conn_tx_cb_t att_cb(struct net_buf *buf) { switch (att_op_get_type(buf->data[0])) { case ATT_RESPONSE: return att_rsp_sent; case ATT_CONFIRMATION: return att_cfm_sent; case ATT_REQUEST: case ATT_INDICATION: return att_req_sent; default: return att_pdu_sent; } } static void send_err_rsp(struct bt_conn *conn, u8_t req, u16_t handle, u8_t err) { struct bt_att_error_rsp *rsp; struct net_buf *buf; /* Ignore opcode 0x00 */ if (!req) { return; } buf = bt_att_create_pdu(conn, BT_ATT_OP_ERROR_RSP, sizeof(*rsp)); if (!buf) { return; } rsp = net_buf_add(buf, sizeof(*rsp)); rsp->request = req; rsp->handle = sys_cpu_to_le16(handle); rsp->error = err; bt_l2cap_send_cb(conn, BT_L2CAP_CID_ATT, buf, att_rsp_sent); } static u8_t att_mtu_req(struct bt_att *att, struct net_buf *buf) { struct bt_conn *conn = att->chan.chan.conn; struct bt_att_exchange_mtu_req *req; struct bt_att_exchange_mtu_rsp *rsp; struct net_buf *pdu; u16_t mtu_client, mtu_server; req = (void *)buf->data; mtu_client = sys_le16_to_cpu(req->mtu); BT_DBG("Client MTU %u", mtu_client); /* Check if MTU is valid */ if (mtu_client < BT_ATT_DEFAULT_LE_MTU) { return BT_ATT_ERR_INVALID_PDU; } pdu = bt_att_create_pdu(conn, BT_ATT_OP_MTU_RSP, sizeof(*rsp)); if (!pdu) { return BT_ATT_ERR_UNLIKELY; } mtu_server = BT_ATT_MTU; BT_DBG("Server MTU %u", mtu_server); rsp = net_buf_add(pdu, sizeof(*rsp)); rsp->mtu = sys_cpu_to_le16(mtu_server); bt_l2cap_send_cb(conn, BT_L2CAP_CID_ATT, pdu, att_rsp_sent); /* BLUETOOTH SPECIFICATION Version 4.2 [Vol 3, Part F] page 484: * * A device's Exchange MTU Request shall contain the same MTU as the * device's Exchange MTU Response (i.e. the MTU shall be symmetric). */ att->chan.rx.mtu = MIN(mtu_client, mtu_server); att->chan.tx.mtu = att->chan.rx.mtu; BT_DBG("Negotiated MTU %u", att->chan.rx.mtu); return 0; } static inline bool att_is_connected(struct bt_att *att) { return (att->chan.chan.conn->state != BT_CONN_CONNECTED || !atomic_test_bit(att->flags, ATT_DISCONNECTED)); } static int att_send_req(struct bt_att *att, struct bt_att_req *req) { __ASSERT_NO_MSG(req); __ASSERT_NO_MSG(req->func); __ASSERT_NO_MSG(!att->req); BT_DBG("req %p", req); att->req = req; k_sem_take(&att->tx_sem, K_FOREVER); if (!att_is_connected(att)) { BT_WARN("Disconnected"); k_sem_give(&att->tx_sem); return -ENOTCONN; } /* Save request state so it can be resent */ net_buf_simple_save(&req->buf->b, &req->state); /* Keep a reference for resending in case of an error */ bt_l2cap_send_cb(att->chan.chan.conn, BT_L2CAP_CID_ATT, net_buf_ref(req->buf), att_cb(req->buf)); return 0; } static void att_process(struct bt_att *att) { sys_snode_t *node; BT_DBG(""); /* Pull next request from the list */ node = sys_slist_get(&att->reqs); if (!node) { return; } att_send_req(att, ATT_REQ(node)); } static u8_t att_handle_rsp(struct bt_att *att, void *pdu, u16_t len, u8_t err) { bt_att_func_t func; BT_DBG("err %u len %u: %s", err, len, bt_hex(pdu, len)); /* Cancel timeout if ongoing */ k_delayed_work_cancel(&att->timeout_work); if (!att->req) { BT_WARN("No pending ATT request"); goto process; } /* Check if request has been cancelled */ if (att->req == &cancel) { att->req = NULL; goto process; } /* Release original buffer */ if (att->req->buf) { net_buf_unref(att->req->buf); att->req->buf = NULL; } /* Reset func so it can be reused by the callback */ func = att->req->func; att->req->func = NULL; func(att->chan.chan.conn, err, pdu, len, att->req); /* Don't destroy if callback had reused the request */ if (!att->req->func) { att_req_destroy(att->req); } att->req = NULL; process: /* Process pending requests */ att_process(att); return 0; } #if defined(CONFIG_BT_GATT_CLIENT) static u8_t att_mtu_rsp(struct bt_att *att, struct net_buf *buf) { struct bt_att_exchange_mtu_rsp *rsp; u16_t mtu; if (!att) { return 0; } rsp = (void *)buf->data; mtu = sys_le16_to_cpu(rsp->mtu); BT_DBG("Server MTU %u", mtu); /* Check if MTU is valid */ if (mtu < BT_ATT_DEFAULT_LE_MTU) { return att_handle_rsp(att, NULL, 0, BT_ATT_ERR_INVALID_PDU); } att->chan.rx.mtu = MIN(mtu, BT_ATT_MTU); /* BLUETOOTH SPECIFICATION Version 4.2 [Vol 3, Part F] page 484: * * A device's Exchange MTU Request shall contain the same MTU as the * device's Exchange MTU Response (i.e. the MTU shall be symmetric). */ att->chan.tx.mtu = att->chan.rx.mtu; BT_DBG("Negotiated MTU %u", att->chan.rx.mtu); return att_handle_rsp(att, rsp, buf->len, 0); } #endif /* CONFIG_BT_GATT_CLIENT */ static bool range_is_valid(u16_t start, u16_t end, u16_t *err) { /* Handle 0 is invalid */ if (!start || !end) { if (err) { *err = 0U; } return false; } /* Check if range is valid */ if (start > end) { if (err) { *err = start; } return false; } return true; } struct find_info_data { struct bt_att *att; struct net_buf *buf; struct bt_att_find_info_rsp *rsp; union { struct bt_att_info_16 *info16; struct bt_att_info_128 *info128; }; }; static u8_t find_info_cb(const struct bt_gatt_attr *attr, void *user_data) { struct find_info_data *data = user_data; struct bt_att *att = data->att; BT_DBG("handle 0x%04x", attr->handle); /* Initialize rsp at first entry */ if (!data->rsp) { data->rsp = net_buf_add(data->buf, sizeof(*data->rsp)); data->rsp->format = (attr->uuid->type == BT_UUID_TYPE_16) ? BT_ATT_INFO_16 : BT_ATT_INFO_128; } switch (data->rsp->format) { case BT_ATT_INFO_16: if (attr->uuid->type != BT_UUID_TYPE_16) { return BT_GATT_ITER_STOP; } /* Fast foward to next item position */ data->info16 = net_buf_add(data->buf, sizeof(*data->info16)); data->info16->handle = sys_cpu_to_le16(attr->handle); data->info16->uuid = sys_cpu_to_le16(BT_UUID_16(attr->uuid)->val); if (att->chan.tx.mtu - data->buf->len > sizeof(*data->info16)) { return BT_GATT_ITER_CONTINUE; } break; case BT_ATT_INFO_128: if (attr->uuid->type != BT_UUID_TYPE_128) { return BT_GATT_ITER_STOP; } /* Fast foward to next item position */ data->info128 = net_buf_add(data->buf, sizeof(*data->info128)); data->info128->handle = sys_cpu_to_le16(attr->handle); memcpy(data->info128->uuid, BT_UUID_128(attr->uuid)->val, sizeof(data->info128->uuid)); if (att->chan.tx.mtu - data->buf->len > sizeof(*data->info128)) { return BT_GATT_ITER_CONTINUE; } } return BT_GATT_ITER_STOP; } static u8_t att_find_info_rsp(struct bt_att *att, u16_t start_handle, u16_t end_handle) { struct bt_conn *conn = att->chan.chan.conn; struct find_info_data data; (void)memset(&data, 0, sizeof(data)); data.buf = bt_att_create_pdu(conn, BT_ATT_OP_FIND_INFO_RSP, 0); if (!data.buf) { return BT_ATT_ERR_UNLIKELY; } data.att = att; bt_gatt_foreach_attr(start_handle, end_handle, find_info_cb, &data); if (!data.rsp) { net_buf_unref(data.buf); /* Respond since handle is set */ send_err_rsp(conn, BT_ATT_OP_FIND_INFO_REQ, start_handle, BT_ATT_ERR_ATTRIBUTE_NOT_FOUND); return 0; } bt_l2cap_send_cb(conn, BT_L2CAP_CID_ATT, data.buf, att_rsp_sent); return 0; } static u8_t att_find_info_req(struct bt_att *att, struct net_buf *buf) { struct bt_conn *conn = att->chan.chan.conn; struct bt_att_find_info_req *req; u16_t start_handle, end_handle, err_handle; req = (void *)buf->data; start_handle = sys_le16_to_cpu(req->start_handle); end_handle = sys_le16_to_cpu(req->end_handle); BT_DBG("start_handle 0x%04x end_handle 0x%04x", start_handle, end_handle); if (!range_is_valid(start_handle, end_handle, &err_handle)) { send_err_rsp(conn, BT_ATT_OP_FIND_INFO_REQ, err_handle, BT_ATT_ERR_INVALID_HANDLE); return 0; } return att_find_info_rsp(att, start_handle, end_handle); } struct find_type_data { struct bt_att *att; struct net_buf *buf; struct bt_att_handle_group *group; const void *value; u8_t value_len; u8_t err; }; static u8_t find_type_cb(const struct bt_gatt_attr *attr, void *user_data) { struct find_type_data *data = user_data; struct bt_att *att = data->att; struct bt_conn *conn = att->chan.chan.conn; int read; u8_t uuid[16]; /* Skip secondary services */ if (!bt_uuid_cmp(attr->uuid, BT_UUID_GATT_SECONDARY)) { data->group = NULL; return BT_GATT_ITER_CONTINUE; } /* Update group end_handle if not a primary service */ if (bt_uuid_cmp(attr->uuid, BT_UUID_GATT_PRIMARY)) { if (data->group && attr->handle > sys_le16_to_cpu(data->group->end_handle)) { data->group->end_handle = sys_cpu_to_le16(attr->handle); } return BT_GATT_ITER_CONTINUE; } BT_DBG("handle 0x%04x", attr->handle); /* stop if there is no space left */ if (att->chan.tx.mtu - data->buf->len < sizeof(*data->group)) { return BT_GATT_ITER_STOP; } /* Read attribute value and store in the buffer */ read = attr->read(conn, attr, uuid, sizeof(uuid), 0); if (read < 0) { /* * Since we don't know if it is the service with requested UUID, * we cannot respond with an error to this request. */ data->group = NULL; return BT_GATT_ITER_CONTINUE; } /* Check if data matches */ if (read != data->value_len || memcmp(data->value, uuid, read)) { data->group = NULL; return BT_GATT_ITER_CONTINUE; } /* If service has been found, error should be cleared */ data->err = 0x00; /* Fast foward to next item position */ data->group = net_buf_add(data->buf, sizeof(*data->group)); data->group->start_handle = sys_cpu_to_le16(attr->handle); data->group->end_handle = sys_cpu_to_le16(attr->handle); /* continue to find the end_handle */ return BT_GATT_ITER_CONTINUE; } static u8_t att_find_type_rsp(struct bt_att *att, u16_t start_handle, u16_t end_handle, const void *value, u8_t value_len) { struct bt_conn *conn = att->chan.chan.conn; struct find_type_data data; (void)memset(&data, 0, sizeof(data)); data.buf = bt_att_create_pdu(conn, BT_ATT_OP_FIND_TYPE_RSP, 0); if (!data.buf) { return BT_ATT_ERR_UNLIKELY; } data.att = att; data.group = NULL; data.value = value; data.value_len = value_len; /* Pre-set error in case no service will be found */ data.err = BT_ATT_ERR_ATTRIBUTE_NOT_FOUND; bt_gatt_foreach_attr(start_handle, end_handle, find_type_cb, &data); /* If error has not been cleared, no service has been found */ if (data.err) { net_buf_unref(data.buf); /* Respond since handle is set */ send_err_rsp(conn, BT_ATT_OP_FIND_TYPE_REQ, start_handle, data.err); return 0; } bt_l2cap_send_cb(conn, BT_L2CAP_CID_ATT, data.buf, att_rsp_sent); return 0; } static u8_t att_find_type_req(struct bt_att *att, struct net_buf *buf) { struct bt_conn *conn = att->chan.chan.conn; struct bt_att_find_type_req *req; u16_t start_handle, end_handle, err_handle, type; u8_t *value; req = net_buf_pull_mem(buf, sizeof(*req)); start_handle = sys_le16_to_cpu(req->start_handle); end_handle = sys_le16_to_cpu(req->end_handle); type = sys_le16_to_cpu(req->type); value = buf->data; BT_DBG("start_handle 0x%04x end_handle 0x%04x type %u", start_handle, end_handle, type); if (!range_is_valid(start_handle, end_handle, &err_handle)) { send_err_rsp(conn, BT_ATT_OP_FIND_TYPE_REQ, err_handle, BT_ATT_ERR_INVALID_HANDLE); return 0; } /* The Attribute Protocol Find By Type Value Request shall be used with * the Attribute Type parameter set to the UUID for "Primary Service" * and the Attribute Value set to the 16-bit Bluetooth UUID or 128-bit * UUID for the specific primary service. */ if (bt_uuid_cmp(BT_UUID_DECLARE_16(type), BT_UUID_GATT_PRIMARY)) { send_err_rsp(conn, BT_ATT_OP_FIND_TYPE_REQ, start_handle, BT_ATT_ERR_ATTRIBUTE_NOT_FOUND); return 0; } return att_find_type_rsp(att, start_handle, end_handle, value, buf->len); } static bool uuid_create(struct bt_uuid *uuid, struct net_buf *buf) { switch (buf->len) { case 2: uuid->type = BT_UUID_TYPE_16; BT_UUID_16(uuid)->val = net_buf_pull_le16(buf); return true; case 16: uuid->type = BT_UUID_TYPE_128; memcpy(BT_UUID_128(uuid)->val, buf->data, buf->len); return true; } return false; } static u8_t check_perm(struct bt_conn *conn, const struct bt_gatt_attr *attr, u8_t mask) { if ((mask & BT_GATT_PERM_READ) && (!(attr->perm & BT_GATT_PERM_READ_MASK) || !attr->read)) { return BT_ATT_ERR_READ_NOT_PERMITTED; } if ((mask & BT_GATT_PERM_WRITE) && (!(attr->perm & BT_GATT_PERM_WRITE_MASK) || !attr->write)) { return BT_ATT_ERR_WRITE_NOT_PERMITTED; } mask &= attr->perm; if (mask & BT_GATT_PERM_AUTHEN_MASK) { #if defined(CONFIG_BT_SMP) if (conn->sec_level < BT_SECURITY_HIGH) { return BT_ATT_ERR_AUTHENTICATION; } #else return BT_ATT_ERR_AUTHENTICATION; #endif /* CONFIG_BT_SMP */ } if ((mask & BT_GATT_PERM_ENCRYPT_MASK)) { #if defined(CONFIG_BT_SMP) if (!conn->encrypt) { return BT_ATT_ERR_INSUFFICIENT_ENCRYPTION; } #else return BT_ATT_ERR_INSUFFICIENT_ENCRYPTION; #endif /* CONFIG_BT_SMP */ } return 0; } static u8_t err_to_att(int err) { BT_DBG("%d", err); if (err < 0 && err >= -0xff) { return -err; } return BT_ATT_ERR_UNLIKELY; } struct read_type_data { struct bt_att *att; struct bt_uuid *uuid; struct net_buf *buf; struct bt_att_read_type_rsp *rsp; struct bt_att_data *item; u8_t err; }; static u8_t read_type_cb(const struct bt_gatt_attr *attr, void *user_data) { struct read_type_data *data = user_data; struct bt_att *att = data->att; struct bt_conn *conn = att->chan.chan.conn; int read; /* Skip if doesn't match */ if (bt_uuid_cmp(attr->uuid, data->uuid)) { return BT_GATT_ITER_CONTINUE; } BT_DBG("handle 0x%04x", attr->handle); /* * If an attribute in the set of requested attributes would cause an * Error Response then this attribute cannot be included in a * Read By Type Response and the attributes before this attribute * shall be returned * * If the first attribute in the set of requested attributes would * cause an Error Response then no other attributes in the requested * attributes can be considered. */ data->err = check_perm(conn, attr, BT_GATT_PERM_READ_MASK); if (data->err) { if (data->rsp->len) { data->err = 0x00; } return BT_GATT_ITER_STOP; } /* * If any attribute is founded in handle range it means that error * should be changed from pre-set: attr not found error to no error. */ data->err = 0x00; /* Fast foward to next item position */ data->item = net_buf_add(data->buf, sizeof(*data->item)); data->item->handle = sys_cpu_to_le16(attr->handle); /* Read attribute value and store in the buffer */ read = attr->read(conn, attr, data->buf->data + data->buf->len, att->chan.tx.mtu - data->buf->len, 0); if (read < 0) { data->err = err_to_att(read); return BT_GATT_ITER_STOP; } if (!data->rsp->len) { /* Set len to be the first item found */ data->rsp->len = read + sizeof(*data->item); } else if (data->rsp->len != read + sizeof(*data->item)) { /* All items should have the same size */ data->buf->len -= sizeof(*data->item); return BT_GATT_ITER_STOP; } net_buf_add(data->buf, read); /* return true only if there are still space for more items */ return att->chan.tx.mtu - data->buf->len > data->rsp->len ? BT_GATT_ITER_CONTINUE : BT_GATT_ITER_STOP; } static u8_t att_read_type_rsp(struct bt_att *att, struct bt_uuid *uuid, u16_t start_handle, u16_t end_handle) { struct bt_conn *conn = att->chan.chan.conn; struct read_type_data data; (void)memset(&data, 0, sizeof(data)); data.buf = bt_att_create_pdu(conn, BT_ATT_OP_READ_TYPE_RSP, sizeof(*data.rsp)); if (!data.buf) { return BT_ATT_ERR_UNLIKELY; } data.att = att; data.uuid = uuid; data.rsp = net_buf_add(data.buf, sizeof(*data.rsp)); data.rsp->len = 0U; /* Pre-set error if no attr will be found in handle */ data.err = BT_ATT_ERR_ATTRIBUTE_NOT_FOUND; bt_gatt_foreach_attr(start_handle, end_handle, read_type_cb, &data); if (data.err) { net_buf_unref(data.buf); /* Response here since handle is set */ send_err_rsp(conn, BT_ATT_OP_READ_TYPE_REQ, start_handle, data.err); return 0; } bt_l2cap_send_cb(conn, BT_L2CAP_CID_ATT, data.buf, att_rsp_sent); return 0; } static u8_t att_read_type_req(struct bt_att *att, struct net_buf *buf) { struct bt_conn *conn = att->chan.chan.conn; struct bt_att_read_type_req *req; u16_t start_handle, end_handle, err_handle; union { struct bt_uuid uuid; struct bt_uuid_16 u16; struct bt_uuid_128 u128; } u; /* Type can only be UUID16 or UUID128 */ if (buf->len != sizeof(*req) + 2 && buf->len != sizeof(*req) + 16) { return BT_ATT_ERR_INVALID_PDU; } req = net_buf_pull_mem(buf, sizeof(*req)); start_handle = sys_le16_to_cpu(req->start_handle); end_handle = sys_le16_to_cpu(req->end_handle); if (!uuid_create(&u.uuid, buf)) { return BT_ATT_ERR_UNLIKELY; } BT_DBG("start_handle 0x%04x end_handle 0x%04x type %s", start_handle, end_handle, bt_uuid_str(&u.uuid)); if (!range_is_valid(start_handle, end_handle, &err_handle)) { send_err_rsp(conn, BT_ATT_OP_READ_TYPE_REQ, err_handle, BT_ATT_ERR_INVALID_HANDLE); return 0; } return att_read_type_rsp(att, &u.uuid, start_handle, end_handle); } struct read_data { struct bt_att *att; u16_t offset; struct net_buf *buf; struct bt_att_read_rsp *rsp; u8_t err; }; static u8_t read_cb(const struct bt_gatt_attr *attr, void *user_data) { struct read_data *data = user_data; struct bt_att *att = data->att; struct bt_conn *conn = att->chan.chan.conn; int read; BT_DBG("handle 0x%04x", attr->handle); data->rsp = net_buf_add(data->buf, sizeof(*data->rsp)); /* * If any attribute is founded in handle range it means that error * should be changed from pre-set: invalid handle error to no error. */ data->err = 0x00; /* Check attribute permissions */ data->err = check_perm(conn, attr, BT_GATT_PERM_READ_MASK); if (data->err) { return BT_GATT_ITER_STOP; } /* Read attribute value and store in the buffer */ read = attr->read(conn, attr, data->buf->data + data->buf->len, att->chan.tx.mtu - data->buf->len, data->offset); if (read < 0) { data->err = err_to_att(read); return BT_GATT_ITER_STOP; } net_buf_add(data->buf, read); return BT_GATT_ITER_CONTINUE; } static u8_t att_read_rsp(struct bt_att *att, u8_t op, u8_t rsp, u16_t handle, u16_t offset) { struct bt_conn *conn = att->chan.chan.conn; struct read_data data; if (!bt_gatt_change_aware(conn, true)) { return BT_ATT_ERR_DB_OUT_OF_SYNC; } if (!handle) { return BT_ATT_ERR_INVALID_HANDLE; } (void)memset(&data, 0, sizeof(data)); data.buf = bt_att_create_pdu(conn, rsp, 0); if (!data.buf) { return BT_ATT_ERR_UNLIKELY; } data.att = att; data.offset = offset; /* Pre-set error if no attr will be found in handle */ data.err = BT_ATT_ERR_INVALID_HANDLE; bt_gatt_foreach_attr(handle, handle, read_cb, &data); /* In case of error discard data and respond with an error */ if (data.err) { net_buf_unref(data.buf); /* Respond here since handle is set */ send_err_rsp(conn, op, handle, data.err); return 0; } bt_l2cap_send_cb(conn, BT_L2CAP_CID_ATT, data.buf, att_rsp_sent); return 0; } static u8_t att_read_req(struct bt_att *att, struct net_buf *buf) { struct bt_att_read_req *req; u16_t handle; req = (void *)buf->data; handle = sys_le16_to_cpu(req->handle); BT_DBG("handle 0x%04x", handle); return att_read_rsp(att, BT_ATT_OP_READ_REQ, BT_ATT_OP_READ_RSP, handle, 0); } static u8_t att_read_blob_req(struct bt_att *att, struct net_buf *buf) { struct bt_att_read_blob_req *req; u16_t handle, offset; req = (void *)buf->data; handle = sys_le16_to_cpu(req->handle); offset = sys_le16_to_cpu(req->offset); BT_DBG("handle 0x%04x offset %u", handle, offset); return att_read_rsp(att, BT_ATT_OP_READ_BLOB_REQ, BT_ATT_OP_READ_BLOB_RSP, handle, offset); } #if defined(CONFIG_BT_GATT_READ_MULTIPLE) static u8_t att_read_mult_req(struct bt_att *att, struct net_buf *buf) { struct bt_conn *conn = att->chan.chan.conn; struct read_data data; u16_t handle; (void)memset(&data, 0, sizeof(data)); data.buf = bt_att_create_pdu(conn, BT_ATT_OP_READ_MULT_RSP, 0); if (!data.buf) { return BT_ATT_ERR_UNLIKELY; } data.att = att; while (buf->len >= sizeof(u16_t)) { handle = net_buf_pull_le16(buf); BT_DBG("handle 0x%04x ", handle); /* An Error Response shall be sent by the server in response to * the Read Multiple Request [....] if a read operation is not * permitted on any of the Characteristic Values. * * If handle is not valid then return invalid handle error. * If handle is found error will be cleared by read_cb. */ data.err = BT_ATT_ERR_INVALID_HANDLE; bt_gatt_foreach_attr(handle, handle, read_cb, &data); /* Stop reading in case of error */ if (data.err) { net_buf_unref(data.buf); /* Respond here since handle is set */ send_err_rsp(conn, BT_ATT_OP_READ_MULT_REQ, handle, data.err); return 0; } } bt_l2cap_send_cb(conn, BT_L2CAP_CID_ATT, data.buf, att_rsp_sent); return 0; } #endif /* CONFIG_BT_GATT_READ_MULTIPLE */ struct read_group_data { struct bt_att *att; struct bt_uuid *uuid; struct net_buf *buf; struct bt_att_read_group_rsp *rsp; struct bt_att_group_data *group; }; static u8_t read_group_cb(const struct bt_gatt_attr *attr, void *user_data) { struct read_group_data *data = user_data; struct bt_att *att = data->att; struct bt_conn *conn = att->chan.chan.conn; int read; /* Update group end_handle if attribute is not a service */ if (bt_uuid_cmp(attr->uuid, BT_UUID_GATT_PRIMARY) && bt_uuid_cmp(attr->uuid, BT_UUID_GATT_SECONDARY)) { if (data->group && attr->handle > sys_le16_to_cpu(data->group->end_handle)) { data->group->end_handle = sys_cpu_to_le16(attr->handle); } return BT_GATT_ITER_CONTINUE; } /* If Group Type don't match skip */ if (bt_uuid_cmp(attr->uuid, data->uuid)) { data->group = NULL; return BT_GATT_ITER_CONTINUE; } BT_DBG("handle 0x%04x", attr->handle); /* Stop if there is no space left */ if (data->rsp->len && att->chan.tx.mtu - data->buf->len < data->rsp->len) { return BT_GATT_ITER_STOP; } /* Fast foward to next group position */ data->group = net_buf_add(data->buf, sizeof(*data->group)); /* Initialize group handle range */ data->group->start_handle = sys_cpu_to_le16(attr->handle); data->group->end_handle = sys_cpu_to_le16(attr->handle); /* Read attribute value and store in the buffer */ read = attr->read(conn, attr, data->buf->data + data->buf->len, att->chan.tx.mtu - data->buf->len, 0); if (read < 0) { /* TODO: Handle read errors */ return BT_GATT_ITER_STOP; } if (!data->rsp->len) { /* Set len to be the first group found */ data->rsp->len = read + sizeof(*data->group); } else if (data->rsp->len != read + sizeof(*data->group)) { /* All groups entries should have the same size */ data->buf->len -= sizeof(*data->group); return false; } net_buf_add(data->buf, read); /* Continue to find the end handle */ return BT_GATT_ITER_CONTINUE; } static u8_t att_read_group_rsp(struct bt_att *att, struct bt_uuid *uuid, u16_t start_handle, u16_t end_handle) { struct bt_conn *conn = att->chan.chan.conn; struct read_group_data data; (void)memset(&data, 0, sizeof(data)); data.buf = bt_att_create_pdu(conn, BT_ATT_OP_READ_GROUP_RSP, sizeof(*data.rsp)); if (!data.buf) { return BT_ATT_ERR_UNLIKELY; } data.att = att; data.uuid = uuid; data.rsp = net_buf_add(data.buf, sizeof(*data.rsp)); data.rsp->len = 0U; data.group = NULL; bt_gatt_foreach_attr(start_handle, end_handle, read_group_cb, &data); if (!data.rsp->len) { net_buf_unref(data.buf); /* Respond here since handle is set */ send_err_rsp(conn, BT_ATT_OP_READ_GROUP_REQ, start_handle, BT_ATT_ERR_ATTRIBUTE_NOT_FOUND); return 0; } bt_l2cap_send_cb(conn, BT_L2CAP_CID_ATT, data.buf, att_rsp_sent); return 0; } static u8_t att_read_group_req(struct bt_att *att, struct net_buf *buf) { struct bt_conn *conn = att->chan.chan.conn; struct bt_att_read_group_req *req; u16_t start_handle, end_handle, err_handle; union { struct bt_uuid uuid; struct bt_uuid_16 u16; struct bt_uuid_128 u128; } u; /* Type can only be UUID16 or UUID128 */ if (buf->len != sizeof(*req) + 2 && buf->len != sizeof(*req) + 16) { return BT_ATT_ERR_INVALID_PDU; } req = net_buf_pull_mem(buf, sizeof(*req)); start_handle = sys_le16_to_cpu(req->start_handle); end_handle = sys_le16_to_cpu(req->end_handle); if (!uuid_create(&u.uuid, buf)) { return BT_ATT_ERR_UNLIKELY; } BT_DBG("start_handle 0x%04x end_handle 0x%04x type %s", start_handle, end_handle, bt_uuid_str(&u.uuid)); if (!range_is_valid(start_handle, end_handle, &err_handle)) { send_err_rsp(conn, BT_ATT_OP_READ_GROUP_REQ, err_handle, BT_ATT_ERR_INVALID_HANDLE); return 0; } /* Core v4.2, Vol 3, sec 2.5.3 Attribute Grouping: * Not all of the grouping attributes can be used in the ATT * Read By Group Type Request. The "Primary Service" and "Secondary * Service" grouping types may be used in the Read By Group Type * Request. The "Characteristic" grouping type shall not be used in * the ATT Read By Group Type Request. */ if (bt_uuid_cmp(&u.uuid, BT_UUID_GATT_PRIMARY) && bt_uuid_cmp(&u.uuid, BT_UUID_GATT_SECONDARY)) { send_err_rsp(conn, BT_ATT_OP_READ_GROUP_REQ, start_handle, BT_ATT_ERR_UNSUPPORTED_GROUP_TYPE); return 0; } return att_read_group_rsp(att, &u.uuid, start_handle, end_handle); } struct write_data { struct bt_conn *conn; struct net_buf *buf; u8_t req; const void *value; u8_t len; u16_t offset; u8_t err; }; static u8_t write_cb(const struct bt_gatt_attr *attr, void *user_data) { struct write_data *data = user_data; int write; u8_t flags = 0U; BT_DBG("handle 0x%04x offset %u", attr->handle, data->offset); /* Check attribute permissions */ data->err = check_perm(data->conn, attr, BT_GATT_PERM_WRITE_MASK); if (data->err) { return BT_GATT_ITER_STOP; } /* Set command flag if not a request */ if (!data->req) { flags |= BT_GATT_WRITE_FLAG_CMD; } /* Write attribute value */ write = attr->write(data->conn, attr, data->value, data->len, data->offset, flags); if (write < 0 || write != data->len) { data->err = err_to_att(write); return BT_GATT_ITER_STOP; } data->err = 0U; return BT_GATT_ITER_CONTINUE; } static u8_t att_write_rsp(struct bt_conn *conn, u8_t req, u8_t rsp, u16_t handle, u16_t offset, const void *value, u8_t len) { struct write_data data; if (!bt_gatt_change_aware(conn, req ? true : false)) { return BT_ATT_ERR_DB_OUT_OF_SYNC; } if (!handle) { return BT_ATT_ERR_INVALID_HANDLE; } (void)memset(&data, 0, sizeof(data)); /* Only allocate buf if required to respond */ if (rsp) { data.buf = bt_att_create_pdu(conn, rsp, 0); if (!data.buf) { return BT_ATT_ERR_UNLIKELY; } } data.conn = conn; data.req = req; data.offset = offset; data.value = value; data.len = len; data.err = BT_ATT_ERR_INVALID_HANDLE; bt_gatt_foreach_attr(handle, handle, write_cb, &data); if (data.err) { /* In case of error discard data and respond with an error */ if (rsp) { net_buf_unref(data.buf); /* Respond here since handle is set */ send_err_rsp(conn, req, handle, data.err); } return req == BT_ATT_OP_EXEC_WRITE_REQ ? data.err : 0; } if (data.buf) { bt_l2cap_send_cb(conn, BT_L2CAP_CID_ATT, data.buf, att_rsp_sent); } return 0; } static u8_t att_write_req(struct bt_att *att, struct net_buf *buf) { struct bt_conn *conn = att->chan.chan.conn; u16_t handle; handle = net_buf_pull_le16(buf); BT_DBG("handle 0x%04x", handle); return att_write_rsp(conn, BT_ATT_OP_WRITE_REQ, BT_ATT_OP_WRITE_RSP, handle, 0, buf->data, buf->len); } #if CONFIG_BT_ATT_PREPARE_COUNT > 0 struct prep_data { struct bt_conn *conn; struct net_buf *buf; const void *value; u8_t len; u16_t offset; u8_t err; }; static u8_t prep_write_cb(const struct bt_gatt_attr *attr, void *user_data) { struct prep_data *data = user_data; struct bt_attr_data *attr_data; int write; BT_DBG("handle 0x%04x offset %u", attr->handle, data->offset); /* Check attribute permissions */ data->err = check_perm(data->conn, attr, BT_GATT_PERM_WRITE_MASK); if (data->err) { return BT_GATT_ITER_STOP; } /* Check if attribute requires handler to accept the data */ if (!(attr->perm & BT_GATT_PERM_PREPARE_WRITE)) { goto append; } /* Write attribute value to check if device is authorized */ write = attr->write(data->conn, attr, data->value, data->len, data->offset, BT_GATT_WRITE_FLAG_PREPARE); if (write != 0) { data->err = err_to_att(write); return BT_GATT_ITER_STOP; } append: /* Copy data into the outstanding queue */ data->buf = net_buf_alloc(&prep_pool, K_NO_WAIT); if (!data->buf) { data->err = BT_ATT_ERR_PREPARE_QUEUE_FULL; return BT_GATT_ITER_STOP; } attr_data = net_buf_user_data(data->buf); attr_data->handle = attr->handle; attr_data->offset = data->offset; net_buf_add_mem(data->buf, data->value, data->len); data->err = 0U; return BT_GATT_ITER_CONTINUE; } static u8_t att_prep_write_rsp(struct bt_att *att, u16_t handle, u16_t offset, const void *value, u8_t len) { struct bt_conn *conn = att->chan.chan.conn; struct prep_data data; struct bt_att_prepare_write_rsp *rsp; if (!bt_gatt_change_aware(conn, true)) { return BT_ATT_ERR_DB_OUT_OF_SYNC; } if (!handle) { return BT_ATT_ERR_INVALID_HANDLE; } (void)memset(&data, 0, sizeof(data)); data.conn = conn; data.offset = offset; data.value = value; data.len = len; data.err = BT_ATT_ERR_INVALID_HANDLE; bt_gatt_foreach_attr(handle, handle, prep_write_cb, &data); if (data.err) { /* Respond here since handle is set */ send_err_rsp(conn, BT_ATT_OP_PREPARE_WRITE_REQ, handle, data.err); return 0; } BT_DBG("buf %p handle 0x%04x offset %u", data.buf, handle, offset); /* Store buffer in the outstanding queue */ net_buf_put(&att->prep_queue, data.buf); /* Generate response */ data.buf = bt_att_create_pdu(conn, BT_ATT_OP_PREPARE_WRITE_RSP, 0); if (!data.buf) { return BT_ATT_ERR_UNLIKELY; } rsp = net_buf_add(data.buf, sizeof(*rsp)); rsp->handle = sys_cpu_to_le16(handle); rsp->offset = sys_cpu_to_le16(offset); net_buf_add(data.buf, len); memcpy(rsp->value, value, len); bt_l2cap_send_cb(conn, BT_L2CAP_CID_ATT, data.buf, att_rsp_sent); return 0; } #endif /* CONFIG_BT_ATT_PREPARE_COUNT */ static u8_t att_prepare_write_req(struct bt_att *att, struct net_buf *buf) { #if CONFIG_BT_ATT_PREPARE_COUNT == 0 return BT_ATT_ERR_NOT_SUPPORTED; #else struct bt_att_prepare_write_req *req; u16_t handle, offset; req = net_buf_pull_mem(buf, sizeof(*req)); handle = sys_le16_to_cpu(req->handle); offset = sys_le16_to_cpu(req->offset); BT_DBG("handle 0x%04x offset %u", handle, offset); return att_prep_write_rsp(att, handle, offset, buf->data, buf->len); #endif /* CONFIG_BT_ATT_PREPARE_COUNT */ } #if CONFIG_BT_ATT_PREPARE_COUNT > 0 static u8_t att_exec_write_rsp(struct bt_att *att, u8_t flags) { struct bt_conn *conn = att->chan.chan.conn; struct net_buf *buf; u8_t err = 0U; while ((buf = net_buf_get(&att->prep_queue, K_NO_WAIT))) { struct bt_attr_data *data = net_buf_user_data(buf); BT_DBG("buf %p handle 0x%04x offset %u", buf, data->handle, data->offset); /* Just discard the data if an error was set */ if (!err && flags == BT_ATT_FLAG_EXEC) { err = att_write_rsp(conn, BT_ATT_OP_EXEC_WRITE_REQ, 0, data->handle, data->offset, buf->data, buf->len); if (err) { /* Respond here since handle is set */ send_err_rsp(conn, BT_ATT_OP_EXEC_WRITE_REQ, data->handle, err); } } net_buf_unref(buf); } if (err) { return 0; } /* Generate response */ buf = bt_att_create_pdu(conn, BT_ATT_OP_EXEC_WRITE_RSP, 0); if (!buf) { return BT_ATT_ERR_UNLIKELY; } bt_l2cap_send_cb(conn, BT_L2CAP_CID_ATT, buf, att_rsp_sent); return 0; } #endif /* CONFIG_BT_ATT_PREPARE_COUNT */ static u8_t att_exec_write_req(struct bt_att *att, struct net_buf *buf) { #if CONFIG_BT_ATT_PREPARE_COUNT == 0 return BT_ATT_ERR_NOT_SUPPORTED; #else struct bt_att_exec_write_req *req; req = (void *)buf->data; BT_DBG("flags 0x%02x", req->flags); return att_exec_write_rsp(att, req->flags); #endif /* CONFIG_BT_ATT_PREPARE_COUNT */ } static u8_t att_write_cmd(struct bt_att *att, struct net_buf *buf) { struct bt_conn *conn = att->chan.chan.conn; u16_t handle; handle = net_buf_pull_le16(buf); BT_DBG("handle 0x%04x", handle); return att_write_rsp(conn, 0, 0, handle, 0, buf->data, buf->len); } #if defined(CONFIG_BT_SIGNING) static u8_t att_signed_write_cmd(struct bt_att *att, struct net_buf *buf) { struct bt_conn *conn = att->chan.chan.conn; struct bt_att_signed_write_cmd *req; u16_t handle; int err; req = (void *)buf->data; handle = sys_le16_to_cpu(req->handle); BT_DBG("handle 0x%04x", handle); /* Verifying data requires full buffer including attribute header */ net_buf_push(buf, sizeof(struct bt_att_hdr)); err = bt_smp_sign_verify(conn, buf); if (err) { BT_ERR("Error verifying data"); /* No response for this command */ return 0; } net_buf_pull(buf, sizeof(struct bt_att_hdr)); net_buf_pull(buf, sizeof(*req)); return att_write_rsp(conn, 0, 0, handle, 0, buf->data, buf->len - sizeof(struct bt_att_signature)); } #endif /* CONFIG_BT_SIGNING */ #if defined(CONFIG_BT_GATT_CLIENT) #if defined(CONFIG_BT_SMP) static int att_change_security(struct bt_conn *conn, u8_t err) { bt_security_t sec; switch (err) { case BT_ATT_ERR_INSUFFICIENT_ENCRYPTION: if (conn->sec_level >= BT_SECURITY_MEDIUM) return -EALREADY; sec = BT_SECURITY_MEDIUM; break; case BT_ATT_ERR_AUTHENTICATION: if (conn->sec_level < BT_SECURITY_MEDIUM) { /* BLUETOOTH SPECIFICATION Version 4.2 [Vol 3, Part C] * page 375: * * If an LTK is not available, the service request * shall be rejected with the error code 'Insufficient * Authentication'. * Note: When the link is not encrypted, the error code * "Insufficient Authentication" does not indicate that * MITM protection is required. */ sec = BT_SECURITY_MEDIUM; } else if (conn->sec_level < BT_SECURITY_HIGH) { /* BLUETOOTH SPECIFICATION Version 4.2 [Vol 3, Part C] * page 375: * * If an authenticated pairing is required but only an * unauthenticated pairing has occurred and the link is * currently encrypted, the service request shall be * rejected with the error code 'Insufficient * Authentication'. * Note: When unauthenticated pairing has occurred and * the link is currently encrypted, the error code * 'Insufficient Authentication' indicates that MITM * protection is required. */ sec = BT_SECURITY_HIGH; } else if (conn->sec_level < BT_SECURITY_FIPS) { /* BLUETOOTH SPECIFICATION Version 4.2 [Vol 3, Part C] * page 375: * * If LE Secure Connections authenticated pairing is * required but LE legacy pairing has occurred and the * link is currently encrypted, the service request * shall be rejected with the error code ''Insufficient * Authentication'. */ sec = BT_SECURITY_FIPS; } else { return -EALREADY; } break; default: return -EINVAL; } return bt_conn_security(conn, sec); } #endif /* CONFIG_BT_SMP */ static u8_t att_error_rsp(struct bt_att *att, struct net_buf *buf) { struct bt_att_error_rsp *rsp; u8_t err; rsp = (void *)buf->data; BT_DBG("request 0x%02x handle 0x%04x error 0x%02x", rsp->request, sys_le16_to_cpu(rsp->handle), rsp->error); /* Don't retry if there is no req pending or it has been cancelled */ if (!att->req || att->req == &cancel) { err = BT_ATT_ERR_UNLIKELY; goto done; } if (att->req->buf) { /* Restore state to be resent */ net_buf_simple_restore(&att->req->buf->b, &att->req->state); } err = rsp->error; #if defined(CONFIG_BT_SMP) if (att->req->retrying) { goto done; } /* Check if security needs to be changed */ if (!att_change_security(att->chan.chan.conn, err)) { att->req->retrying = true; /* Wait security_changed: TODO: Handle fail case */ return 0; } #endif /* CONFIG_BT_SMP */ done: return att_handle_rsp(att, NULL, 0, err); } static u8_t att_handle_find_info_rsp(struct bt_att *att, struct net_buf *buf) { BT_DBG(""); return att_handle_rsp(att, buf->data, buf->len, 0); } static u8_t att_handle_find_type_rsp(struct bt_att *att, struct net_buf *buf) { BT_DBG(""); return att_handle_rsp(att, buf->data, buf->len, 0); } static u8_t att_handle_read_type_rsp(struct bt_att *att, struct net_buf *buf) { BT_DBG(""); return att_handle_rsp(att, buf->data, buf->len, 0); } static u8_t att_handle_read_rsp(struct bt_att *att, struct net_buf *buf) { BT_DBG(""); return att_handle_rsp(att, buf->data, buf->len, 0); } static u8_t att_handle_read_blob_rsp(struct bt_att *att, struct net_buf *buf) { BT_DBG(""); return att_handle_rsp(att, buf->data, buf->len, 0); } #if defined(CONFIG_BT_GATT_READ_MULTIPLE) static u8_t att_handle_read_mult_rsp(struct bt_att *att, struct net_buf *buf) { BT_DBG(""); return att_handle_rsp(att, buf->data, buf->len, 0); } #endif /* CONFIG_BT_GATT_READ_MULTIPLE */ static u8_t att_handle_read_group_rsp(struct bt_att *att, struct net_buf *buf) { BT_DBG(""); return att_handle_rsp(att, buf->data, buf->len, 0); } static u8_t att_handle_write_rsp(struct bt_att *att, struct net_buf *buf) { BT_DBG(""); return att_handle_rsp(att, buf->data, buf->len, 0); } static u8_t att_handle_prepare_write_rsp(struct bt_att *att, struct net_buf *buf) { BT_DBG(""); return att_handle_rsp(att, buf->data, buf->len, 0); } static u8_t att_handle_exec_write_rsp(struct bt_att *att, struct net_buf *buf) { BT_DBG(""); return att_handle_rsp(att, buf->data, buf->len, 0); } static u8_t att_notify(struct bt_att *att, struct net_buf *buf) { struct bt_conn *conn = att->chan.chan.conn; u16_t handle; handle = net_buf_pull_le16(buf); BT_DBG("handle 0x%04x", handle); bt_gatt_notification(conn, handle, buf->data, buf->len); return 0; } static u8_t att_indicate(struct bt_att *att, struct net_buf *buf) { struct bt_conn *conn = att->chan.chan.conn; u16_t handle; handle = net_buf_pull_le16(buf); BT_DBG("handle 0x%04x", handle); bt_gatt_notification(conn, handle, buf->data, buf->len); buf = bt_att_create_pdu(conn, BT_ATT_OP_CONFIRM, 0); if (!buf) { return 0; } bt_l2cap_send_cb(conn, BT_L2CAP_CID_ATT, buf, att_cfm_sent); return 0; } #endif /* CONFIG_BT_GATT_CLIENT */ static u8_t att_confirm(struct bt_att *att, struct net_buf *buf) { BT_DBG(""); return att_handle_rsp(att, buf->data, buf->len, 0); } static const struct att_handler { u8_t op; u8_t expect_len; att_type_t type; u8_t (*func)(struct bt_att *att, struct net_buf *buf); } handlers[] = { { BT_ATT_OP_MTU_REQ, sizeof(struct bt_att_exchange_mtu_req), ATT_REQUEST, att_mtu_req }, { BT_ATT_OP_FIND_INFO_REQ, sizeof(struct bt_att_find_info_req), ATT_REQUEST, att_find_info_req }, { BT_ATT_OP_FIND_TYPE_REQ, sizeof(struct bt_att_find_type_req), ATT_REQUEST, att_find_type_req }, { BT_ATT_OP_READ_TYPE_REQ, sizeof(struct bt_att_read_type_req), ATT_REQUEST, att_read_type_req }, { BT_ATT_OP_READ_REQ, sizeof(struct bt_att_read_req), ATT_REQUEST, att_read_req }, { BT_ATT_OP_READ_BLOB_REQ, sizeof(struct bt_att_read_blob_req), ATT_REQUEST, att_read_blob_req }, #if defined(CONFIG_BT_GATT_READ_MULTIPLE) { BT_ATT_OP_READ_MULT_REQ, BT_ATT_READ_MULT_MIN_LEN_REQ, ATT_REQUEST, att_read_mult_req }, #endif /* CONFIG_BT_GATT_READ_MULTIPLE */ { BT_ATT_OP_READ_GROUP_REQ, sizeof(struct bt_att_read_group_req), ATT_REQUEST, att_read_group_req }, { BT_ATT_OP_WRITE_REQ, sizeof(struct bt_att_write_req), ATT_REQUEST, att_write_req }, { BT_ATT_OP_PREPARE_WRITE_REQ, sizeof(struct bt_att_prepare_write_req), ATT_REQUEST, att_prepare_write_req }, { BT_ATT_OP_EXEC_WRITE_REQ, sizeof(struct bt_att_exec_write_req), ATT_REQUEST, att_exec_write_req }, { BT_ATT_OP_CONFIRM, 0, ATT_CONFIRMATION, att_confirm }, { BT_ATT_OP_WRITE_CMD, sizeof(struct bt_att_write_cmd), ATT_COMMAND, att_write_cmd }, #if defined(CONFIG_BT_SIGNING) { BT_ATT_OP_SIGNED_WRITE_CMD, (sizeof(struct bt_att_write_cmd) + sizeof(struct bt_att_signature)), ATT_COMMAND, att_signed_write_cmd }, #endif /* CONFIG_BT_SIGNING */ #if defined(CONFIG_BT_GATT_CLIENT) { BT_ATT_OP_ERROR_RSP, sizeof(struct bt_att_error_rsp), ATT_RESPONSE, att_error_rsp }, { BT_ATT_OP_MTU_RSP, sizeof(struct bt_att_exchange_mtu_rsp), ATT_RESPONSE, att_mtu_rsp }, { BT_ATT_OP_FIND_INFO_RSP, sizeof(struct bt_att_find_info_rsp), ATT_RESPONSE, att_handle_find_info_rsp }, { BT_ATT_OP_FIND_TYPE_RSP, sizeof(struct bt_att_find_type_rsp), ATT_RESPONSE, att_handle_find_type_rsp }, { BT_ATT_OP_READ_TYPE_RSP, sizeof(struct bt_att_read_type_rsp), ATT_RESPONSE, att_handle_read_type_rsp }, { BT_ATT_OP_READ_RSP, sizeof(struct bt_att_read_rsp), ATT_RESPONSE, att_handle_read_rsp }, { BT_ATT_OP_READ_BLOB_RSP, sizeof(struct bt_att_read_blob_rsp), ATT_RESPONSE, att_handle_read_blob_rsp }, #if defined(CONFIG_BT_GATT_READ_MULTIPLE) { BT_ATT_OP_READ_MULT_RSP, sizeof(struct bt_att_read_mult_rsp), ATT_RESPONSE, att_handle_read_mult_rsp }, #endif /* CONFIG_BT_GATT_READ_MULTIPLE */ { BT_ATT_OP_READ_GROUP_RSP, sizeof(struct bt_att_read_group_rsp), ATT_RESPONSE, att_handle_read_group_rsp }, { BT_ATT_OP_WRITE_RSP, 0, ATT_RESPONSE, att_handle_write_rsp }, { BT_ATT_OP_PREPARE_WRITE_RSP, sizeof(struct bt_att_prepare_write_rsp), ATT_RESPONSE, att_handle_prepare_write_rsp }, { BT_ATT_OP_EXEC_WRITE_RSP, 0, ATT_RESPONSE, att_handle_exec_write_rsp }, { BT_ATT_OP_NOTIFY, sizeof(struct bt_att_notify), ATT_NOTIFICATION, att_notify }, { BT_ATT_OP_INDICATE, sizeof(struct bt_att_indicate), ATT_INDICATION, att_indicate }, #endif /* CONFIG_BT_GATT_CLIENT */ }; static att_type_t att_op_get_type(u8_t op) { const struct att_handler *handler; int i; for (i = 0, handler = NULL; i < ARRAY_SIZE(handlers); i++) { if (op == handlers[i].op) { return handlers[i].type; } } if (op & ATT_CMD_MASK) { return ATT_COMMAND; } return ATT_UNKNOWN; } static int bt_att_recv(struct bt_l2cap_chan *chan, struct net_buf *buf) { struct bt_att *att = ATT_CHAN(chan); struct bt_att_hdr *hdr; const struct att_handler *handler; u8_t err; size_t i; if (buf->len < sizeof(*hdr)) { BT_ERR("Too small ATT PDU received"); return 0; } hdr = net_buf_pull_mem(buf, sizeof(*hdr)); BT_DBG("Received ATT code 0x%02x len %u", hdr->code, buf->len); for (i = 0, handler = NULL; i < ARRAY_SIZE(handlers); i++) { if (hdr->code == handlers[i].op) { handler = &handlers[i]; break; } } if (!handler) { BT_WARN("Unknown ATT code 0x%02x", hdr->code); if (att_op_get_type(hdr->code) != ATT_COMMAND) { send_err_rsp(chan->conn, hdr->code, 0, BT_ATT_ERR_NOT_SUPPORTED); } return 0; } if (IS_ENABLED(CONFIG_BT_ATT_ENFORCE_FLOW)) { if (handler->type == ATT_REQUEST && atomic_test_and_set_bit(att->flags, ATT_PENDING_RSP)) { BT_WARN("Ignoring unexpected request"); return 0; } else if (handler->type == ATT_INDICATION && atomic_test_and_set_bit(att->flags, ATT_PENDING_CFM)) { BT_WARN("Ignoring unexpected indication"); return 0; } } if (buf->len < handler->expect_len) { BT_ERR("Invalid len %u for code 0x%02x", buf->len, hdr->code); err = BT_ATT_ERR_INVALID_PDU; } else { err = handler->func(att, buf); } if (handler->type == ATT_REQUEST && err) { BT_DBG("ATT error 0x%02x", err); send_err_rsp(chan->conn, hdr->code, 0, err); } return 0; } static struct bt_att *att_chan_get(struct bt_conn *conn) { struct bt_l2cap_chan *chan; struct bt_att *att; if (conn->state != BT_CONN_CONNECTED) { BT_WARN("Not connected"); return NULL; } chan = bt_l2cap_le_lookup_rx_cid(conn, BT_L2CAP_CID_ATT); if (!chan) { BT_ERR("Unable to find ATT channel"); return NULL; } att = ATT_CHAN(chan); if (atomic_test_bit(att->flags, ATT_DISCONNECTED)) { BT_WARN("ATT context flagged as disconnected"); return NULL; } return att; } struct net_buf *bt_att_create_pdu(struct bt_conn *conn, u8_t op, size_t len) { struct bt_att_hdr *hdr; struct net_buf *buf; struct bt_att *att; att = att_chan_get(conn); if (!att) { return NULL; } if (len + sizeof(op) > att->chan.tx.mtu) { BT_WARN("ATT MTU exceeded, max %u, wanted %zu", att->chan.tx.mtu, len + sizeof(op)); return NULL; } buf = bt_l2cap_create_pdu(NULL, 0); hdr = net_buf_add(buf, sizeof(*hdr)); hdr->code = op; return buf; } static void att_reset(struct bt_att *att) { struct bt_att_req *req, *tmp; int i; #if CONFIG_BT_ATT_PREPARE_COUNT > 0 struct net_buf *buf; /* Discard queued buffers */ while ((buf = k_fifo_get(&att->prep_queue, K_NO_WAIT))) { net_buf_unref(buf); } #endif atomic_set_bit(att->flags, ATT_DISCONNECTED); /* Ensure that any waiters are woken up */ for (i = 0; i < CONFIG_BT_ATT_TX_MAX; i++) { k_sem_give(&att->tx_sem); } /* Notify pending requests */ SYS_SLIST_FOR_EACH_CONTAINER_SAFE(&att->reqs, req, tmp, node) { if (req->func) { req->func(NULL, BT_ATT_ERR_UNLIKELY, NULL, 0, req); } att_req_destroy(req); } /* Reset list */ sys_slist_init(&att->reqs); if (!att->req) { return; } /* Notify outstanding request */ att_handle_rsp(att, NULL, 0, BT_ATT_ERR_UNLIKELY); } static void att_timeout(struct k_work *work) { struct bt_att *att = CONTAINER_OF(work, struct bt_att, timeout_work); struct bt_l2cap_le_chan *ch = &att->chan; BT_ERR("ATT Timeout"); /* BLUETOOTH SPECIFICATION Version 4.2 [Vol 3, Part F] page 480: * * A transaction not completed within 30 seconds shall time out. Such a * transaction shall be considered to have failed and the local higher * layers shall be informed of this failure. No more attribute protocol * requests, commands, indications or notifications shall be sent to the * target device on this ATT Bearer. */ att_reset(att); /* Consider the channel disconnected */ bt_gatt_disconnected(ch->chan.conn); ch->chan.conn = NULL; } static void bt_att_connected(struct bt_l2cap_chan *chan) { struct bt_att *att = ATT_CHAN(chan); struct bt_l2cap_le_chan *ch = BT_L2CAP_LE_CHAN(chan); BT_DBG("chan %p cid 0x%04x", ch, ch->tx.cid); #if CONFIG_BT_ATT_PREPARE_COUNT > 0 k_fifo_init(&att->prep_queue); #endif ch->tx.mtu = BT_ATT_DEFAULT_LE_MTU; ch->rx.mtu = BT_ATT_DEFAULT_LE_MTU; k_delayed_work_init(&att->timeout_work, att_timeout); sys_slist_init(&att->reqs); } static void bt_att_disconnected(struct bt_l2cap_chan *chan) { struct bt_att *att = ATT_CHAN(chan); struct bt_l2cap_le_chan *ch = BT_L2CAP_LE_CHAN(chan); BT_DBG("chan %p cid 0x%04x", ch, ch->tx.cid); att_reset(att); bt_gatt_disconnected(ch->chan.conn); } #if defined(CONFIG_BT_SMP) static void bt_att_encrypt_change(struct bt_l2cap_chan *chan, u8_t hci_status) { struct bt_att *att = ATT_CHAN(chan); struct bt_l2cap_le_chan *ch = BT_L2CAP_LE_CHAN(chan); struct bt_conn *conn = ch->chan.conn; BT_DBG("chan %p conn %p handle %u sec_level 0x%02x status 0x%02x", ch, conn, conn->handle, conn->sec_level, hci_status); /* * If status (HCI status of security procedure) is non-zero, notify * outstanding request about security failure. */ if (hci_status) { att_handle_rsp(att, NULL, 0, BT_ATT_ERR_AUTHENTICATION); return; } if (conn->sec_level == BT_SECURITY_LOW) { return; } if (!att->req || !att->req->retrying) { return; } k_sem_take(&att->tx_sem, K_FOREVER); if (!att_is_connected(att)) { BT_WARN("Disconnected"); k_sem_give(&att->tx_sem); return; } BT_DBG("Retrying"); /* Resend buffer */ bt_l2cap_send_cb(conn, BT_L2CAP_CID_ATT, att->req->buf, att_cb(att->req->buf)); att->req->buf = NULL; } #endif /* CONFIG_BT_SMP */ static int bt_att_accept(struct bt_conn *conn, struct bt_l2cap_chan **chan) { int i; static struct bt_l2cap_chan_ops ops = { .connected = bt_att_connected, .disconnected = bt_att_disconnected, .recv = bt_att_recv, #if defined(CONFIG_BT_SMP) .encrypt_change = bt_att_encrypt_change, #endif /* CONFIG_BT_SMP */ }; BT_DBG("conn %p handle %u", conn, conn->handle); for (i = 0; i < ARRAY_SIZE(bt_req_pool); i++) { struct bt_att *att = &bt_req_pool[i]; if (att->chan.chan.conn) { continue; } (void)memset(att, 0, sizeof(*att)); att->chan.chan.ops = &ops; k_sem_init(&att->tx_sem, CONFIG_BT_ATT_TX_MAX, CONFIG_BT_ATT_TX_MAX); *chan = &att->chan.chan; return 0; } BT_ERR("No available ATT context for conn %p", conn); return -ENOMEM; } void bt_att_init(void) { static struct bt_l2cap_fixed_chan chan = { .cid = BT_L2CAP_CID_ATT, .accept = bt_att_accept, }; bt_l2cap_le_fixed_chan_register(&chan); bt_gatt_init(); } u16_t bt_att_get_mtu(struct bt_conn *conn) { struct bt_att *att; att = att_chan_get(conn); if (!att) { return 0; } /* tx and rx MTU shall be symmetric */ return att->chan.tx.mtu; } int bt_att_send(struct bt_conn *conn, struct net_buf *buf, bt_conn_tx_cb_t cb) { struct bt_att *att; struct bt_att_hdr *hdr; if (!conn || !buf) { return -EINVAL; } att = att_chan_get(conn); if (!att) { return -ENOTCONN; } /* Don't use tx_sem if caller has set it own callback */ if (!cb) { k_sem_take(&att->tx_sem, K_FOREVER); if (!att_is_connected(att)) { BT_WARN("Disconnected"); k_sem_give(&att->tx_sem); return -ENOTCONN; } } hdr = (void *)buf->data; BT_DBG("code 0x%02x", hdr->code); if (hdr->code == BT_ATT_OP_SIGNED_WRITE_CMD) { int err; err = bt_smp_sign(conn, buf); if (err) { BT_ERR("Error signing data"); k_sem_give(&att->tx_sem); return err; } } bt_l2cap_send_cb(conn, BT_L2CAP_CID_ATT, buf, cb ? cb : att_cb(buf)); return 0; } int bt_att_req_send(struct bt_conn *conn, struct bt_att_req *req) { struct bt_att *att; BT_DBG("conn %p req %p", conn, req); if (!conn || !req) { return -EINVAL; } att = att_chan_get(conn); if (!att) { return -ENOTCONN; } /* Check if there is a request outstanding */ if (att->req) { /* Queue the request to be send later */ sys_slist_append(&att->reqs, &req->node); return 0; } return att_send_req(att, req); } void bt_att_req_cancel(struct bt_conn *conn, struct bt_att_req *req) { struct bt_att *att; BT_DBG("req %p", req); if (!conn || !req) { return; } att = att_chan_get(conn); if (!att) { return; } /* Check if request is outstanding */ if (att->req == req) { att->req = &cancel; } else { /* Remove request from the list */ sys_slist_find_and_remove(&att->reqs, &req->node); } att_req_destroy(req); } |