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 233deb3ec6SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 243deb3ec6SMatthias Ringwald * RINGWALD 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 453deb3ec6SMatthias Ringwald #include "att_dispatch.h" 4659c6af15SMatthias Ringwald #include "ble/core.h" 4716ece135SMatthias Ringwald #include "btstack_debug.h" 4859c6af15SMatthias Ringwald #include "l2cap.h" 493deb3ec6SMatthias Ringwald 5053e41ec1SMatthias Ringwald #define ATT_SERVER 0 5153e41ec1SMatthias Ringwald #define ATT_CLIENT 1 527a705a92SMatthias Ringwald #define ATT_MAX 2 53773c4106SMatthias Ringwald 5453e41ec1SMatthias Ringwald struct { 5553e41ec1SMatthias Ringwald btstack_packet_handler_t packet_handler; 5653e41ec1SMatthias Ringwald uint8_t waiting_for_can_send; 577a705a92SMatthias Ringwald } subscriptions[ATT_MAX]; 587a705a92SMatthias Ringwald 597a705a92SMatthias Ringwald // index of subscription that will get can send now first if waiting for it 607a705a92SMatthias Ringwald static uint8_t att_round_robin; 613deb3ec6SMatthias Ringwald 62047a7fd3SMatthias Ringwald // track can send now requests 63047a7fd3SMatthias Ringwald static uint8_t can_send_now_pending; 64047a7fd3SMatthias Ringwald 653deb3ec6SMatthias Ringwald static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){ 667a705a92SMatthias Ringwald uint8_t index; 677a705a92SMatthias Ringwald uint8_t i; 68*838bd521SMatthias Ringwald uint8_t opcode; 69*838bd521SMatthias Ringwald uint8_t method; 70*838bd521SMatthias Ringwald bool for_server; 71*838bd521SMatthias Ringwald bool command; 72*838bd521SMatthias Ringwald bool invalid; 73773c4106SMatthias Ringwald switch (packet_type){ 74773c4106SMatthias Ringwald case ATT_DATA_PACKET: 75*838bd521SMatthias Ringwald // parse opcode 76*838bd521SMatthias Ringwald opcode = packet[0u]; 77*838bd521SMatthias Ringwald method = opcode & 0x03f; 78*838bd521SMatthias Ringwald invalid = method > ATT_MULTIPLE_HANDLE_VALUE_NTF; 79*838bd521SMatthias Ringwald // odd PDUs are sent from server to client - even PDUs are sent from client to server, also let server handle invalid ones 80*838bd521SMatthias Ringwald for_server = ((method & 1u) == 0) || invalid; 81*838bd521SMatthias 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: 86773c4106SMatthias Ringwald if (packet[0] != L2CAP_EVENT_CAN_SEND_NOW) break; 87047a7fd3SMatthias Ringwald can_send_now_pending = 0; 887a705a92SMatthias Ringwald for (i = 0; i < ATT_MAX; i++){ 894ea43905SMatthias Ringwald index = (att_round_robin + i) & 1u; 9053e41ec1SMatthias Ringwald if (subscriptions[index].packet_handler && subscriptions[index].waiting_for_can_send){ 9153e41ec1SMatthias Ringwald subscriptions[index].waiting_for_can_send = 0; 9253e41ec1SMatthias Ringwald subscriptions[index].packet_handler(packet_type, handle, packet, size); 931dab9fd6SMatthias Ringwald // fairness: prioritize next service 944ea43905SMatthias Ringwald att_round_robin = (index + 1u) % ATT_MAX; 95773c4106SMatthias Ringwald // stop if client cannot send anymore 96b947e684SMatthias Ringwald if (!hci_can_send_acl_le_packet_now()) break; 97773c4106SMatthias Ringwald } 98773c4106SMatthias Ringwald } 99047a7fd3SMatthias Ringwald // check if more can send now events are needed 100047a7fd3SMatthias Ringwald if (!can_send_now_pending){ 101047a7fd3SMatthias Ringwald for (i = 0; i < ATT_MAX; i++){ 102bba111e7SMatthias Ringwald if (subscriptions[i].packet_handler && subscriptions[i].waiting_for_can_send){ 103047a7fd3SMatthias Ringwald can_send_now_pending = 1; 104047a7fd3SMatthias Ringwald // note: con_handle is not used, so we can pass in anything 105047a7fd3SMatthias Ringwald l2cap_request_can_send_fix_channel_now_event(0, L2CAP_CID_ATTRIBUTE_PROTOCOL); 106047a7fd3SMatthias Ringwald break; 107047a7fd3SMatthias Ringwald } 108047a7fd3SMatthias Ringwald } 109047a7fd3SMatthias Ringwald } 110773c4106SMatthias Ringwald break; 111773c4106SMatthias Ringwald default: 112773c4106SMatthias Ringwald break; 113773c4106SMatthias Ringwald } 1143deb3ec6SMatthias Ringwald } 1153deb3ec6SMatthias Ringwald 1163deb3ec6SMatthias Ringwald /** 1173deb3ec6SMatthias Ringwald * @brief reset att dispatchter 1183deb3ec6SMatthias Ringwald * @param packet_hander for ATT client packets 1193deb3ec6SMatthias Ringwald */ 1203deb3ec6SMatthias Ringwald void att_dispatch_register_client(btstack_packet_handler_t packet_handler){ 12153e41ec1SMatthias Ringwald subscriptions[ATT_CLIENT].packet_handler = packet_handler; 1223deb3ec6SMatthias Ringwald l2cap_register_fixed_channel(att_packet_handler, L2CAP_CID_ATTRIBUTE_PROTOCOL); 1233deb3ec6SMatthias Ringwald } 1243deb3ec6SMatthias Ringwald 1253deb3ec6SMatthias Ringwald /** 1263deb3ec6SMatthias Ringwald * @brief reset att dispatchter 1273deb3ec6SMatthias Ringwald * @param packet_hander for ATT server packets 1283deb3ec6SMatthias Ringwald */ 1293deb3ec6SMatthias Ringwald void att_dispatch_register_server(btstack_packet_handler_t packet_handler){ 13053e41ec1SMatthias Ringwald subscriptions[ATT_SERVER].packet_handler = packet_handler; 1313deb3ec6SMatthias Ringwald l2cap_register_fixed_channel(att_packet_handler, L2CAP_CID_ATTRIBUTE_PROTOCOL); 1323deb3ec6SMatthias Ringwald } 13319448c0bSMatthias Ringwald 13419448c0bSMatthias Ringwald /** 13519448c0bSMatthias Ringwald * @brief can send packet for client 13619448c0bSMatthias Ringwald * @param handle 13719448c0bSMatthias Ringwald */ 138fc64f94aSMatthias Ringwald int att_dispatch_client_can_send_now(hci_con_handle_t con_handle){ 1390b9d7e78SMatthias Ringwald return l2cap_can_send_fixed_channel_packet_now(con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL); 14019448c0bSMatthias Ringwald } 14119448c0bSMatthias Ringwald 14219448c0bSMatthias Ringwald /** 14319448c0bSMatthias Ringwald * @brief can send packet for server 14419448c0bSMatthias Ringwald * @param handle 14519448c0bSMatthias Ringwald */ 146fc64f94aSMatthias Ringwald int att_dispatch_server_can_send_now(hci_con_handle_t con_handle){ 1470b9d7e78SMatthias Ringwald return l2cap_can_send_fixed_channel_packet_now(con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL); 148773c4106SMatthias Ringwald } 1490b9d7e78SMatthias Ringwald 1500b9d7e78SMatthias Ringwald /** 1510b9d7e78SMatthias Ringwald * @brief Request emission of L2CAP_EVENT_CAN_SEND_NOW as soon as possible for client 1520b9d7e78SMatthias Ringwald * @note L2CAP_EVENT_CAN_SEND_NOW might be emitted during call to this function 1530b9d7e78SMatthias Ringwald * so packet handler should be ready to handle it 1540b9d7e78SMatthias Ringwald * @param con_handle 1550b9d7e78SMatthias Ringwald */ 1560b9d7e78SMatthias Ringwald void att_dispatch_client_request_can_send_now_event(hci_con_handle_t con_handle){ 15753e41ec1SMatthias Ringwald subscriptions[ATT_CLIENT].waiting_for_can_send = 1; 158047a7fd3SMatthias Ringwald if (!can_send_now_pending){ 159047a7fd3SMatthias Ringwald can_send_now_pending = 1; 1600b9d7e78SMatthias Ringwald l2cap_request_can_send_fix_channel_now_event(con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL); 1610b9d7e78SMatthias Ringwald } 162047a7fd3SMatthias Ringwald } 1630b9d7e78SMatthias Ringwald 1640b9d7e78SMatthias Ringwald /** 1650b9d7e78SMatthias Ringwald * @brief Request emission of L2CAP_EVENT_CAN_SEND_NOW as soon as possible for server 1660b9d7e78SMatthias Ringwald * @note L2CAP_EVENT_CAN_SEND_NOW might be emitted during call to this function 1670b9d7e78SMatthias Ringwald * so packet handler should be ready to handle it 1680b9d7e78SMatthias Ringwald * @param con_handle 1690b9d7e78SMatthias Ringwald */ 1700b9d7e78SMatthias Ringwald void att_dispatch_server_request_can_send_now_event(hci_con_handle_t con_handle){ 17153e41ec1SMatthias Ringwald subscriptions[ATT_SERVER].waiting_for_can_send = 1; 172047a7fd3SMatthias Ringwald if (!can_send_now_pending){ 173047a7fd3SMatthias Ringwald can_send_now_pending = 1; 1740b9d7e78SMatthias Ringwald l2cap_request_can_send_fix_channel_now_event(con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL); 17519448c0bSMatthias Ringwald } 176047a7fd3SMatthias Ringwald } 177b12646c5SJakob Krantz 17825684bfcSMatthias Ringwald static void emit_mtu_exchange_complete(btstack_packet_handler_t packet_handler, hci_con_handle_t con_handle, uint16_t new_mtu){ 17925684bfcSMatthias Ringwald if (!packet_handler) return; 180b12646c5SJakob Krantz uint8_t packet[6]; 181b12646c5SJakob Krantz packet[0] = ATT_EVENT_MTU_EXCHANGE_COMPLETE; 1824ea43905SMatthias Ringwald packet[1] = sizeof(packet) - 2u; 183b12646c5SJakob Krantz little_endian_store_16(packet, 2, con_handle); 184b12646c5SJakob Krantz little_endian_store_16(packet, 4, new_mtu); 185f38f43d7SMatthias Ringwald packet_handler(HCI_EVENT_PACKET, con_handle, packet, 6); 18625684bfcSMatthias Ringwald } 18725684bfcSMatthias Ringwald 18825684bfcSMatthias Ringwald void att_dispatch_server_mtu_exchanged(hci_con_handle_t con_handle, uint16_t new_mtu){ 18953e41ec1SMatthias Ringwald emit_mtu_exchange_complete(subscriptions[ATT_CLIENT].packet_handler, con_handle, new_mtu); 190b12646c5SJakob Krantz } 191b12646c5SJakob Krantz 192b12646c5SJakob Krantz void att_dispatch_client_mtu_exchanged(hci_con_handle_t con_handle, uint16_t new_mtu){ 19353e41ec1SMatthias Ringwald emit_mtu_exchange_complete(subscriptions[ATT_SERVER].packet_handler, con_handle, new_mtu); 194b12646c5SJakob Krantz } 195