13deb3ec6SMatthias Ringwald /* 23deb3ec6SMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 33deb3ec6SMatthias Ringwald * 43deb3ec6SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 53deb3ec6SMatthias Ringwald * modification, are permitted provided that the following conditions 63deb3ec6SMatthias Ringwald * are met: 73deb3ec6SMatthias Ringwald * 83deb3ec6SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 93deb3ec6SMatthias Ringwald * notice, this list of conditions and the following disclaimer. 103deb3ec6SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 113deb3ec6SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 123deb3ec6SMatthias Ringwald * documentation and/or other materials provided with the distribution. 133deb3ec6SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 143deb3ec6SMatthias Ringwald * contributors may be used to endorse or promote products derived 153deb3ec6SMatthias Ringwald * from this software without specific prior written permission. 163deb3ec6SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 173deb3ec6SMatthias Ringwald * personal benefit and not for any commercial purpose or for 183deb3ec6SMatthias Ringwald * monetary gain. 193deb3ec6SMatthias Ringwald * 203deb3ec6SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 213deb3ec6SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 223deb3ec6SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 232fca4dadSMilanka Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN 242fca4dadSMilanka Ringwald * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 253deb3ec6SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 263deb3ec6SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 273deb3ec6SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 283deb3ec6SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 293deb3ec6SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 303deb3ec6SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 313deb3ec6SMatthias Ringwald * SUCH DAMAGE. 323deb3ec6SMatthias Ringwald * 333deb3ec6SMatthias Ringwald * Please inquire about commercial licensing options at 343deb3ec6SMatthias Ringwald * [email protected] 353deb3ec6SMatthias Ringwald * 363deb3ec6SMatthias Ringwald */ 373deb3ec6SMatthias Ringwald 38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "att_dispatch.c" 39ab2c6ae4SMatthias Ringwald 403deb3ec6SMatthias Ringwald 413deb3ec6SMatthias Ringwald /** 423deb3ec6SMatthias Ringwald * Dispatcher for independent implementation of ATT client and server 433deb3ec6SMatthias Ringwald */ 443deb3ec6SMatthias Ringwald 45296289e3SMatthias Ringwald #include "ble/att_dispatch.h" 4659c6af15SMatthias Ringwald #include "ble/core.h" 4716ece135SMatthias Ringwald #include "btstack_debug.h" 4859c6af15SMatthias Ringwald #include "l2cap.h" 4998926299SMatthias Ringwald #include "btstack_event.h" 503deb3ec6SMatthias Ringwald 5138b893aaSMilanka Ringwald #define ATT_SERVER 0u 5238b893aaSMilanka Ringwald #define ATT_CLIENT 1u 5338b893aaSMilanka Ringwald #define ATT_MAX 2u 54773c4106SMatthias Ringwald 5553e41ec1SMatthias Ringwald struct { 5653e41ec1SMatthias Ringwald btstack_packet_handler_t packet_handler; 57429d695fSMilanka Ringwald bool waiting_for_can_send; 587a705a92SMatthias Ringwald } subscriptions[ATT_MAX]; 597a705a92SMatthias Ringwald 607a705a92SMatthias Ringwald // index of subscription that will get can send now first if waiting for it 617a705a92SMatthias Ringwald static uint8_t att_round_robin; 623deb3ec6SMatthias Ringwald 63047a7fd3SMatthias Ringwald // track can send now requests 64429d695fSMilanka Ringwald static bool can_send_now_pending; 65047a7fd3SMatthias Ringwald 669eba80a6SMatthias Ringwald static void att_dispatch_handle_can_send_now(uint8_t *packet, uint16_t size){ 677a705a92SMatthias Ringwald uint8_t i; 689eba80a6SMatthias Ringwald uint8_t index; 699eba80a6SMatthias Ringwald uint16_t l2cap_cid = l2cap_event_can_send_now_get_local_cid(packet); 70*72df296cSMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 71*72df296cSMatthias Ringwald if (l2cap_cid != L2CAP_CID_ATTRIBUTE_PROTOCOL){ 72*72df296cSMatthias Ringwald // lookup att_server in hci_connection for l2cap_cid 73*72df296cSMatthias Ringwald btstack_linked_list_iterator_t it; 74*72df296cSMatthias Ringwald hci_connections_get_iterator(&it); 75*72df296cSMatthias Ringwald while(btstack_linked_list_iterator_has_next(&it)) { 76*72df296cSMatthias Ringwald hci_connection_t *hci_connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 77*72df296cSMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 78*72df296cSMatthias Ringwald if (att_server->l2cap_cid == l2cap_cid){ 79*72df296cSMatthias Ringwald for (i = 0u; i < ATT_MAX; i++) { 80*72df296cSMatthias Ringwald index = (att_round_robin + i) & 1u; 81*72df296cSMatthias Ringwald if (att_server->send_requests[index]) { 82*72df296cSMatthias Ringwald att_server->send_requests[index] = false; 83*72df296cSMatthias Ringwald // registered packet handlers from Unenhanced LE 84*72df296cSMatthias Ringwald subscriptions[index].packet_handler(HCI_EVENT_PACKET, l2cap_cid, packet, size); 85*72df296cSMatthias Ringwald // fairness: prioritize next service 86*72df296cSMatthias Ringwald att_round_robin = (index + 1u) % ATT_MAX; 87*72df296cSMatthias Ringwald // stop if client cannot send anymore 88*72df296cSMatthias Ringwald if (!l2cap_can_send_packet_now(l2cap_cid)) break; 89*72df296cSMatthias Ringwald } 90*72df296cSMatthias Ringwald } 91*72df296cSMatthias Ringwald // check if more can send now events are needed 92*72df296cSMatthias Ringwald bool send_request_pending = att_server->send_requests[ATT_CLIENT] 93*72df296cSMatthias Ringwald || att_server->send_requests[ATT_SERVER]; 94*72df296cSMatthias Ringwald if (send_request_pending){ 95*72df296cSMatthias Ringwald l2cap_request_can_send_now_event(att_server->l2cap_cid); 96*72df296cSMatthias Ringwald } 97*72df296cSMatthias Ringwald return; 98*72df296cSMatthias Ringwald } 99*72df296cSMatthias Ringwald } 100*72df296cSMatthias Ringwald } 101*72df296cSMatthias Ringwald #endif 102*72df296cSMatthias Ringwald can_send_now_pending = false; 10338b893aaSMilanka Ringwald for (i = 0u; i < ATT_MAX; i++){ 1044ea43905SMatthias Ringwald index = (att_round_robin + i) & 1u; 105429d695fSMilanka Ringwald if ( (subscriptions[index].packet_handler != NULL) && subscriptions[index].waiting_for_can_send){ 106429d695fSMilanka Ringwald subscriptions[index].waiting_for_can_send = false; 1079eba80a6SMatthias Ringwald subscriptions[index].packet_handler(HCI_EVENT_PACKET, l2cap_cid, packet, size); 1081dab9fd6SMatthias Ringwald // fairness: prioritize next service 1094ea43905SMatthias Ringwald att_round_robin = (index + 1u) % ATT_MAX; 110773c4106SMatthias Ringwald // stop if client cannot send anymore 111b947e684SMatthias Ringwald if (!hci_can_send_acl_le_packet_now()) break; 112773c4106SMatthias Ringwald } 113773c4106SMatthias Ringwald } 114047a7fd3SMatthias Ringwald // check if more can send now events are needed 115047a7fd3SMatthias Ringwald if (!can_send_now_pending){ 11638b893aaSMilanka Ringwald for (i = 0u; i < ATT_MAX; i++){ 117429d695fSMilanka Ringwald if ((subscriptions[i].packet_handler != NULL) && subscriptions[i].waiting_for_can_send){ 118429d695fSMilanka Ringwald can_send_now_pending = true; 119047a7fd3SMatthias Ringwald // note: con_handle is not used, so we can pass in anything 120047a7fd3SMatthias Ringwald l2cap_request_can_send_fix_channel_now_event(0, L2CAP_CID_ATTRIBUTE_PROTOCOL); 121047a7fd3SMatthias Ringwald break; 122047a7fd3SMatthias Ringwald } 123047a7fd3SMatthias Ringwald } 124047a7fd3SMatthias Ringwald } 1259eba80a6SMatthias Ringwald } 1269eba80a6SMatthias Ringwald 1279eba80a6SMatthias Ringwald static void att_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1289eba80a6SMatthias Ringwald uint8_t opcode; 1299eba80a6SMatthias Ringwald uint8_t method; 1309eba80a6SMatthias Ringwald bool for_server; 1319eba80a6SMatthias Ringwald bool invalid; 1329eba80a6SMatthias Ringwald uint8_t index; 1339eba80a6SMatthias Ringwald switch (packet_type){ 1349eba80a6SMatthias Ringwald case ATT_DATA_PACKET: 1359eba80a6SMatthias Ringwald // parse opcode 1369eba80a6SMatthias Ringwald opcode = packet[0u]; 1379eba80a6SMatthias Ringwald method = opcode & 0x03fu; 1389eba80a6SMatthias Ringwald invalid = method > ATT_MULTIPLE_HANDLE_VALUE_NTF; 1399eba80a6SMatthias Ringwald // odd PDUs are sent from server to client - even PDUs are sent from client to server, also let server handle invalid ones 1409eba80a6SMatthias Ringwald for_server = ((method & 1u) == 0u) || invalid; 1419eba80a6SMatthias Ringwald index = for_server ? ATT_SERVER : ATT_CLIENT; 1429eba80a6SMatthias Ringwald if (!subscriptions[index].packet_handler) return; 1439eba80a6SMatthias Ringwald subscriptions[index].packet_handler(packet_type, channel, packet, size); 1449eba80a6SMatthias Ringwald break; 1459eba80a6SMatthias Ringwald case HCI_EVENT_PACKET: 1469eba80a6SMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 1479eba80a6SMatthias Ringwald case L2CAP_EVENT_CAN_SEND_NOW: 1489eba80a6SMatthias Ringwald att_dispatch_handle_can_send_now(packet, size); 149773c4106SMatthias Ringwald break; 150773c4106SMatthias Ringwald default: 151773c4106SMatthias Ringwald break; 152773c4106SMatthias Ringwald } 15398926299SMatthias Ringwald break; 15498926299SMatthias Ringwald default: 15598926299SMatthias Ringwald break; 15698926299SMatthias Ringwald } 1573deb3ec6SMatthias Ringwald } 1583deb3ec6SMatthias Ringwald 1593deb3ec6SMatthias Ringwald void att_dispatch_register_client(btstack_packet_handler_t packet_handler){ 16053e41ec1SMatthias Ringwald subscriptions[ATT_CLIENT].packet_handler = packet_handler; 1613deb3ec6SMatthias Ringwald l2cap_register_fixed_channel(att_packet_handler, L2CAP_CID_ATTRIBUTE_PROTOCOL); 1623deb3ec6SMatthias Ringwald } 1633deb3ec6SMatthias Ringwald 1643deb3ec6SMatthias Ringwald void att_dispatch_register_server(btstack_packet_handler_t packet_handler){ 16553e41ec1SMatthias Ringwald subscriptions[ATT_SERVER].packet_handler = packet_handler; 1663deb3ec6SMatthias Ringwald l2cap_register_fixed_channel(att_packet_handler, L2CAP_CID_ATTRIBUTE_PROTOCOL); 1673deb3ec6SMatthias Ringwald } 16819448c0bSMatthias Ringwald 1690f376572SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 1700cd55b6cSMatthias Ringwald static uint16_t att_dispatch_classic_get_l2cap_cid(hci_con_handle_t con_handle){ 1710f376572SMatthias Ringwald // get l2cap_cid from att_server in hci_connection 1720f376572SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 1730f376572SMatthias Ringwald if (hci_connection != NULL) { 1740cd55b6cSMatthias Ringwald return hci_connection->att_server.l2cap_cid; 1750cd55b6cSMatthias Ringwald } else { 1760cd55b6cSMatthias Ringwald return 0; 1770cd55b6cSMatthias Ringwald } 1780cd55b6cSMatthias Ringwald } 1790cd55b6cSMatthias Ringwald #endif 1800cd55b6cSMatthias Ringwald 1810cd55b6cSMatthias Ringwald 1820cd55b6cSMatthias Ringwald static bool att_dispatch_can_send_now(hci_con_handle_t con_handle) { 1830cd55b6cSMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 1840cd55b6cSMatthias Ringwald uint16_t l2cap_cid = att_dispatch_classic_get_l2cap_cid(con_handle); 1850f376572SMatthias Ringwald if (l2cap_cid != 0){ 1860f376572SMatthias Ringwald return l2cap_can_send_packet_now(l2cap_cid); 1870f376572SMatthias Ringwald } 1880f376572SMatthias Ringwald #endif 1890b9d7e78SMatthias Ringwald return l2cap_can_send_fixed_channel_packet_now(con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL); 19019448c0bSMatthias Ringwald } 19119448c0bSMatthias Ringwald 192e9e0c21eSMatthias Ringwald bool att_dispatch_client_can_send_now(hci_con_handle_t con_handle){ 193e9e0c21eSMatthias Ringwald return att_dispatch_can_send_now(con_handle); 194e9e0c21eSMatthias Ringwald } 195e9e0c21eSMatthias Ringwald 1964a7a258fSMatthias Ringwald bool att_dispatch_server_can_send_now(hci_con_handle_t con_handle){ 197e9e0c21eSMatthias Ringwald return att_dispatch_can_send_now(con_handle); 198773c4106SMatthias Ringwald } 1990b9d7e78SMatthias Ringwald 200eb65899cSMatthias Ringwald static void att_dispatch_request_can_send_now_event(hci_con_handle_t con_handle, uint8_t type) { 2015fd2be40SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 2025fd2be40SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 2035fd2be40SMatthias Ringwald if (hci_connection != NULL) { 2045fd2be40SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 2055fd2be40SMatthias Ringwald if (att_server->l2cap_cid != 0){ 2065fd2be40SMatthias Ringwald bool send_request_pending = att_server->send_requests[ATT_CLIENT] 2075fd2be40SMatthias Ringwald || att_server->send_requests[ATT_SERVER]; 2085fd2be40SMatthias Ringwald att_server->send_requests[type] = true; 2095fd2be40SMatthias Ringwald if (send_request_pending == false){ 2105fd2be40SMatthias Ringwald l2cap_request_can_send_now_event(att_server->l2cap_cid); 2115fd2be40SMatthias Ringwald } 2125fd2be40SMatthias Ringwald return; 2135fd2be40SMatthias Ringwald } 2145fd2be40SMatthias Ringwald } 2155fd2be40SMatthias Ringwald #endif 216eb65899cSMatthias Ringwald subscriptions[type].waiting_for_can_send = true; 2175fd2be40SMatthias Ringwald if (can_send_now_pending == false) { 218429d695fSMilanka Ringwald can_send_now_pending = true; 2190b9d7e78SMatthias Ringwald l2cap_request_can_send_fix_channel_now_event(con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL); 2200b9d7e78SMatthias Ringwald } 221047a7fd3SMatthias Ringwald } 2220b9d7e78SMatthias Ringwald 223eb65899cSMatthias Ringwald void att_dispatch_client_request_can_send_now_event(hci_con_handle_t con_handle){ 224eb65899cSMatthias Ringwald att_dispatch_request_can_send_now_event(con_handle, ATT_CLIENT); 22519448c0bSMatthias Ringwald } 226eb65899cSMatthias Ringwald 227eb65899cSMatthias Ringwald void att_dispatch_server_request_can_send_now_event(hci_con_handle_t con_handle){ 228eb65899cSMatthias Ringwald att_dispatch_request_can_send_now_event(con_handle, ATT_SERVER); 229047a7fd3SMatthias Ringwald } 230b12646c5SJakob Krantz 23125684bfcSMatthias Ringwald static void emit_mtu_exchange_complete(btstack_packet_handler_t packet_handler, hci_con_handle_t con_handle, uint16_t new_mtu){ 23225684bfcSMatthias Ringwald if (!packet_handler) return; 233b12646c5SJakob Krantz uint8_t packet[6]; 234b12646c5SJakob Krantz packet[0] = ATT_EVENT_MTU_EXCHANGE_COMPLETE; 2354ea43905SMatthias Ringwald packet[1] = sizeof(packet) - 2u; 236b12646c5SJakob Krantz little_endian_store_16(packet, 2, con_handle); 237b12646c5SJakob Krantz little_endian_store_16(packet, 4, new_mtu); 238f38f43d7SMatthias Ringwald packet_handler(HCI_EVENT_PACKET, con_handle, packet, 6); 23925684bfcSMatthias Ringwald } 24025684bfcSMatthias Ringwald 24125684bfcSMatthias Ringwald void att_dispatch_server_mtu_exchanged(hci_con_handle_t con_handle, uint16_t new_mtu){ 24253e41ec1SMatthias Ringwald emit_mtu_exchange_complete(subscriptions[ATT_CLIENT].packet_handler, con_handle, new_mtu); 243b12646c5SJakob Krantz } 244b12646c5SJakob Krantz 245b12646c5SJakob Krantz void att_dispatch_client_mtu_exchanged(hci_con_handle_t con_handle, uint16_t new_mtu){ 24653e41ec1SMatthias Ringwald emit_mtu_exchange_complete(subscriptions[ATT_SERVER].packet_handler, con_handle, new_mtu); 247b12646c5SJakob Krantz } 248