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 663deb3ec6SMatthias Ringwald static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){ 677a705a92SMatthias Ringwald uint8_t index; 687a705a92SMatthias Ringwald uint8_t i; 69838bd521SMatthias Ringwald uint8_t opcode; 70838bd521SMatthias Ringwald uint8_t method; 71838bd521SMatthias Ringwald bool for_server; 72838bd521SMatthias Ringwald bool invalid; 73773c4106SMatthias Ringwald switch (packet_type){ 74773c4106SMatthias Ringwald case ATT_DATA_PACKET: 75838bd521SMatthias Ringwald // parse opcode 76838bd521SMatthias Ringwald opcode = packet[0u]; 7738b893aaSMilanka Ringwald method = opcode & 0x03fu; 78ed68ae9dSMilanka Ringwald invalid = method > ATT_MULTIPLE_HANDLE_VALUE_NTF; 79838bd521SMatthias Ringwald // odd PDUs are sent from server to client - even PDUs are sent from client to server, also let server handle invalid ones 8038b893aaSMilanka Ringwald for_server = ((method & 1u) == 0u) || invalid; 81838bd521SMatthias Ringwald index = for_server ? ATT_SERVER : ATT_CLIENT; 8253e41ec1SMatthias Ringwald if (!subscriptions[index].packet_handler) return; 8353e41ec1SMatthias Ringwald subscriptions[index].packet_handler(packet_type, handle, packet, size); 84202c8a4cSMatthias Ringwald break; 85773c4106SMatthias Ringwald case HCI_EVENT_PACKET: 8698926299SMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 8798926299SMatthias Ringwald case L2CAP_EVENT_CAN_SEND_NOW: 8838b893aaSMilanka Ringwald can_send_now_pending = false; 8938b893aaSMilanka Ringwald for (i = 0u; i < ATT_MAX; i++){ 904ea43905SMatthias Ringwald index = (att_round_robin + i) & 1u; 91429d695fSMilanka Ringwald if ( (subscriptions[index].packet_handler != NULL) && subscriptions[index].waiting_for_can_send){ 92429d695fSMilanka Ringwald subscriptions[index].waiting_for_can_send = false; 9353e41ec1SMatthias Ringwald subscriptions[index].packet_handler(packet_type, handle, packet, size); 941dab9fd6SMatthias Ringwald // fairness: prioritize next service 954ea43905SMatthias Ringwald att_round_robin = (index + 1u) % ATT_MAX; 96773c4106SMatthias Ringwald // stop if client cannot send anymore 97b947e684SMatthias Ringwald if (!hci_can_send_acl_le_packet_now()) break; 98773c4106SMatthias Ringwald } 99773c4106SMatthias Ringwald } 100047a7fd3SMatthias Ringwald // check if more can send now events are needed 101047a7fd3SMatthias Ringwald if (!can_send_now_pending){ 10238b893aaSMilanka Ringwald for (i = 0u; i < ATT_MAX; i++){ 103429d695fSMilanka Ringwald if ((subscriptions[i].packet_handler != NULL) && subscriptions[i].waiting_for_can_send){ 104429d695fSMilanka Ringwald can_send_now_pending = true; 105047a7fd3SMatthias Ringwald // note: con_handle is not used, so we can pass in anything 106047a7fd3SMatthias Ringwald l2cap_request_can_send_fix_channel_now_event(0, L2CAP_CID_ATTRIBUTE_PROTOCOL); 107047a7fd3SMatthias Ringwald break; 108047a7fd3SMatthias Ringwald } 109047a7fd3SMatthias Ringwald } 110047a7fd3SMatthias Ringwald } 111773c4106SMatthias Ringwald break; 112773c4106SMatthias Ringwald default: 113773c4106SMatthias Ringwald break; 114773c4106SMatthias Ringwald } 11598926299SMatthias Ringwald break; 11698926299SMatthias Ringwald default: 11798926299SMatthias Ringwald break; 11898926299SMatthias Ringwald } 1193deb3ec6SMatthias Ringwald } 1203deb3ec6SMatthias Ringwald 1213deb3ec6SMatthias Ringwald void att_dispatch_register_client(btstack_packet_handler_t packet_handler){ 12253e41ec1SMatthias Ringwald subscriptions[ATT_CLIENT].packet_handler = packet_handler; 1233deb3ec6SMatthias Ringwald l2cap_register_fixed_channel(att_packet_handler, L2CAP_CID_ATTRIBUTE_PROTOCOL); 1243deb3ec6SMatthias Ringwald } 1253deb3ec6SMatthias Ringwald 1263deb3ec6SMatthias Ringwald void att_dispatch_register_server(btstack_packet_handler_t packet_handler){ 12753e41ec1SMatthias Ringwald subscriptions[ATT_SERVER].packet_handler = packet_handler; 1283deb3ec6SMatthias Ringwald l2cap_register_fixed_channel(att_packet_handler, L2CAP_CID_ATTRIBUTE_PROTOCOL); 1293deb3ec6SMatthias Ringwald } 13019448c0bSMatthias Ringwald 1310f376572SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 1320cd55b6cSMatthias Ringwald static uint16_t att_dispatch_classic_get_l2cap_cid(hci_con_handle_t con_handle){ 1330f376572SMatthias Ringwald // get l2cap_cid from att_server in hci_connection 1340f376572SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 1350f376572SMatthias Ringwald if (hci_connection != NULL) { 1360cd55b6cSMatthias Ringwald return hci_connection->att_server.l2cap_cid; 1370cd55b6cSMatthias Ringwald } else { 1380cd55b6cSMatthias Ringwald return 0; 1390cd55b6cSMatthias Ringwald } 1400cd55b6cSMatthias Ringwald } 1410cd55b6cSMatthias Ringwald #endif 1420cd55b6cSMatthias Ringwald 1430cd55b6cSMatthias Ringwald 1440cd55b6cSMatthias Ringwald static bool att_dispatch_can_send_now(hci_con_handle_t con_handle) { 1450cd55b6cSMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 1460cd55b6cSMatthias Ringwald uint16_t l2cap_cid = att_dispatch_classic_get_l2cap_cid(con_handle); 1470f376572SMatthias Ringwald if (l2cap_cid != 0){ 1480f376572SMatthias Ringwald return l2cap_can_send_packet_now(l2cap_cid); 1490f376572SMatthias Ringwald } 1500f376572SMatthias Ringwald #endif 1510b9d7e78SMatthias Ringwald return l2cap_can_send_fixed_channel_packet_now(con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL); 15219448c0bSMatthias Ringwald } 15319448c0bSMatthias Ringwald 154e9e0c21eSMatthias Ringwald bool att_dispatch_client_can_send_now(hci_con_handle_t con_handle){ 155e9e0c21eSMatthias Ringwald return att_dispatch_can_send_now(con_handle); 156e9e0c21eSMatthias Ringwald } 157e9e0c21eSMatthias Ringwald 1584a7a258fSMatthias Ringwald bool att_dispatch_server_can_send_now(hci_con_handle_t con_handle){ 159e9e0c21eSMatthias Ringwald return att_dispatch_can_send_now(con_handle); 160773c4106SMatthias Ringwald } 1610b9d7e78SMatthias Ringwald 162eb65899cSMatthias Ringwald static void att_dispatch_request_can_send_now_event(hci_con_handle_t con_handle, uint8_t type) { 163*5fd2be40SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 164*5fd2be40SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 165*5fd2be40SMatthias Ringwald if (hci_connection != NULL) { 166*5fd2be40SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 167*5fd2be40SMatthias Ringwald if (att_server->l2cap_cid != 0){ 168*5fd2be40SMatthias Ringwald bool send_request_pending = att_server->send_requests[ATT_CLIENT] 169*5fd2be40SMatthias Ringwald || att_server->send_requests[ATT_SERVER]; 170*5fd2be40SMatthias Ringwald att_server->send_requests[type] = true; 171*5fd2be40SMatthias Ringwald if (send_request_pending == false){ 172*5fd2be40SMatthias Ringwald l2cap_request_can_send_now_event(att_server->l2cap_cid); 173*5fd2be40SMatthias Ringwald } 174*5fd2be40SMatthias Ringwald return; 175*5fd2be40SMatthias Ringwald } 176*5fd2be40SMatthias Ringwald } 177*5fd2be40SMatthias Ringwald #endif 178eb65899cSMatthias Ringwald subscriptions[type].waiting_for_can_send = true; 179*5fd2be40SMatthias Ringwald if (can_send_now_pending == false) { 180429d695fSMilanka Ringwald can_send_now_pending = true; 1810b9d7e78SMatthias Ringwald l2cap_request_can_send_fix_channel_now_event(con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL); 1820b9d7e78SMatthias Ringwald } 183047a7fd3SMatthias Ringwald } 1840b9d7e78SMatthias Ringwald 185eb65899cSMatthias Ringwald void att_dispatch_client_request_can_send_now_event(hci_con_handle_t con_handle){ 186eb65899cSMatthias Ringwald att_dispatch_request_can_send_now_event(con_handle, ATT_CLIENT); 18719448c0bSMatthias Ringwald } 188eb65899cSMatthias Ringwald 189eb65899cSMatthias Ringwald void att_dispatch_server_request_can_send_now_event(hci_con_handle_t con_handle){ 190eb65899cSMatthias Ringwald att_dispatch_request_can_send_now_event(con_handle, ATT_SERVER); 191047a7fd3SMatthias Ringwald } 192b12646c5SJakob Krantz 19325684bfcSMatthias Ringwald static void emit_mtu_exchange_complete(btstack_packet_handler_t packet_handler, hci_con_handle_t con_handle, uint16_t new_mtu){ 19425684bfcSMatthias Ringwald if (!packet_handler) return; 195b12646c5SJakob Krantz uint8_t packet[6]; 196b12646c5SJakob Krantz packet[0] = ATT_EVENT_MTU_EXCHANGE_COMPLETE; 1974ea43905SMatthias Ringwald packet[1] = sizeof(packet) - 2u; 198b12646c5SJakob Krantz little_endian_store_16(packet, 2, con_handle); 199b12646c5SJakob Krantz little_endian_store_16(packet, 4, new_mtu); 200f38f43d7SMatthias Ringwald packet_handler(HCI_EVENT_PACKET, con_handle, packet, 6); 20125684bfcSMatthias Ringwald } 20225684bfcSMatthias Ringwald 20325684bfcSMatthias Ringwald void att_dispatch_server_mtu_exchanged(hci_con_handle_t con_handle, uint16_t new_mtu){ 20453e41ec1SMatthias Ringwald emit_mtu_exchange_complete(subscriptions[ATT_CLIENT].packet_handler, con_handle, new_mtu); 205b12646c5SJakob Krantz } 206b12646c5SJakob Krantz 207b12646c5SJakob Krantz void att_dispatch_client_mtu_exchanged(hci_con_handle_t con_handle, uint16_t new_mtu){ 20853e41ec1SMatthias Ringwald emit_mtu_exchange_complete(subscriptions[ATT_SERVER].packet_handler, con_handle, new_mtu); 209b12646c5SJakob Krantz } 210