From 53e41ec1de51517ea55427d10f85970d1690d2bd Mon Sep 17 00:00:00 2001 From: Matthias Ringwald Date: Thu, 1 Feb 2018 12:08:41 +0100 Subject: [PATCH] att_dispatch: store client and server in array, simplify packet handler --- src/ble/att_dispatch.c | 86 ++++++++++++++++++++---------------------- 1 file changed, 41 insertions(+), 45 deletions(-) diff --git a/src/ble/att_dispatch.c b/src/ble/att_dispatch.c index 9c6da5d1c..42cfc78d9 100644 --- a/src/ble/att_dispatch.c +++ b/src/ble/att_dispatch.c @@ -47,42 +47,38 @@ #include "btstack_debug.h" #include "l2cap.h" -static btstack_packet_handler_t att_client_handler; -static btstack_packet_handler_t att_server_handler; +#define ATT_SERVER 0 +#define ATT_CLIENT 1 -static uint8_t att_client_waiting_for_can_send; -static uint8_t att_server_waiting_for_can_send; +struct { + btstack_packet_handler_t packet_handler; + uint8_t waiting_for_can_send; +} subscriptions[2]; static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){ - switch (packet_type){ - case ATT_DATA_PACKET: - // log_info("att_data_packet with opcode 0x%x", packet[0]); - if (packet[0] & 1){ - // odd PDUs are sent from server to client - if (!att_client_handler) return; - att_client_handler(packet_type, handle, packet, size); - } else { - // even PDUs are sent from client to server - if (!att_server_handler) return; - att_server_handler(packet_type, handle, packet, size); - } - break; - case HCI_EVENT_PACKET: - if (packet[0] != L2CAP_EVENT_CAN_SEND_NOW) break; - if (att_server_handler && att_server_waiting_for_can_send){ - att_server_waiting_for_can_send = 0; - att_server_handler(packet_type, handle, packet, size); - // stop if client cannot send anymore - if (!hci_can_send_acl_le_packet_now()) break; - } - if (att_client_handler && att_client_waiting_for_can_send){ - att_client_waiting_for_can_send = 0; - att_client_handler(packet_type, handle, packet, size); - } - break; - default: - break; - } + int index; + switch (packet_type){ + case ATT_DATA_PACKET: + // odd PDUs are sent from server to client - even PDUs are sent from client to server + index = packet[0] & 1; + // log_info("att_data_packet with opcode 0x%x", packet[0]); + if (!subscriptions[index].packet_handler) return; + subscriptions[index].packet_handler(packet_type, handle, packet, size); + break; + case HCI_EVENT_PACKET: + if (packet[0] != L2CAP_EVENT_CAN_SEND_NOW) break; + for (index = 0; index < 2 ; index++){ + if (subscriptions[index].packet_handler && subscriptions[index].waiting_for_can_send){ + subscriptions[index].waiting_for_can_send = 0; + subscriptions[index].packet_handler(packet_type, handle, packet, size); + // stop if client cannot send anymore + if (!hci_can_send_acl_le_packet_now()) break; + } + } + break; + default: + break; + } } /** @@ -90,8 +86,8 @@ static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *pa * @param packet_hander for ATT client packets */ void att_dispatch_register_client(btstack_packet_handler_t packet_handler){ - att_client_handler = packet_handler; - l2cap_register_fixed_channel(att_packet_handler, L2CAP_CID_ATTRIBUTE_PROTOCOL); + subscriptions[ATT_CLIENT].packet_handler = packet_handler; + l2cap_register_fixed_channel(att_packet_handler, L2CAP_CID_ATTRIBUTE_PROTOCOL); } /** @@ -99,8 +95,8 @@ void att_dispatch_register_client(btstack_packet_handler_t packet_handler){ * @param packet_hander for ATT server packets */ void att_dispatch_register_server(btstack_packet_handler_t packet_handler){ - att_server_handler = packet_handler; - l2cap_register_fixed_channel(att_packet_handler, L2CAP_CID_ATTRIBUTE_PROTOCOL); + subscriptions[ATT_SERVER].packet_handler = packet_handler; + l2cap_register_fixed_channel(att_packet_handler, L2CAP_CID_ATTRIBUTE_PROTOCOL); } /** @@ -108,7 +104,7 @@ void att_dispatch_register_server(btstack_packet_handler_t packet_handler){ * @param handle */ int att_dispatch_client_can_send_now(hci_con_handle_t con_handle){ - return l2cap_can_send_fixed_channel_packet_now(con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL); + return l2cap_can_send_fixed_channel_packet_now(con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL); } /** @@ -116,7 +112,7 @@ int att_dispatch_client_can_send_now(hci_con_handle_t con_handle){ * @param handle */ int att_dispatch_server_can_send_now(hci_con_handle_t con_handle){ - return l2cap_can_send_fixed_channel_packet_now(con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL); + return l2cap_can_send_fixed_channel_packet_now(con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL); } /** @@ -126,8 +122,8 @@ int att_dispatch_server_can_send_now(hci_con_handle_t con_handle){ * @param con_handle */ void att_dispatch_client_request_can_send_now_event(hci_con_handle_t con_handle){ - att_client_waiting_for_can_send = 1; - l2cap_request_can_send_fix_channel_now_event(con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL); + subscriptions[ATT_CLIENT].waiting_for_can_send = 1; + l2cap_request_can_send_fix_channel_now_event(con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL); } /** @@ -137,8 +133,8 @@ void att_dispatch_client_request_can_send_now_event(hci_con_handle_t con_handle) * @param con_handle */ void att_dispatch_server_request_can_send_now_event(hci_con_handle_t con_handle){ - att_server_waiting_for_can_send = 1; - l2cap_request_can_send_fix_channel_now_event(con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL); + subscriptions[ATT_SERVER].waiting_for_can_send = 1; + l2cap_request_can_send_fix_channel_now_event(con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL); } static void emit_mtu_exchange_complete(btstack_packet_handler_t packet_handler, hci_con_handle_t con_handle, uint16_t new_mtu){ @@ -152,9 +148,9 @@ static void emit_mtu_exchange_complete(btstack_packet_handler_t packet_handler, } void att_dispatch_server_mtu_exchanged(hci_con_handle_t con_handle, uint16_t new_mtu){ - emit_mtu_exchange_complete(att_client_handler, con_handle, new_mtu); + emit_mtu_exchange_complete(subscriptions[ATT_CLIENT].packet_handler, con_handle, new_mtu); } void att_dispatch_client_mtu_exchanged(hci_con_handle_t con_handle, uint16_t new_mtu){ - emit_mtu_exchange_complete(att_server_handler, con_handle, new_mtu); + emit_mtu_exchange_complete(subscriptions[ATT_SERVER].packet_handler, con_handle, new_mtu); }