att_dispatch: store client and server in array, simplify packet handler

This commit is contained in:
Matthias Ringwald 2018-02-01 12:08:41 +01:00
parent 25684bfca7
commit 53e41ec1de

View File

@ -47,42 +47,38 @@
#include "btstack_debug.h" #include "btstack_debug.h"
#include "l2cap.h" #include "l2cap.h"
static btstack_packet_handler_t att_client_handler; #define ATT_SERVER 0
static btstack_packet_handler_t att_server_handler; #define ATT_CLIENT 1
static uint8_t att_client_waiting_for_can_send; struct {
static uint8_t att_server_waiting_for_can_send; 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){ static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){
switch (packet_type){ int index;
case ATT_DATA_PACKET: switch (packet_type){
// log_info("att_data_packet with opcode 0x%x", packet[0]); case ATT_DATA_PACKET:
if (packet[0] & 1){ // odd PDUs are sent from server to client - even PDUs are sent from client to server
// odd PDUs are sent from server to client index = packet[0] & 1;
if (!att_client_handler) return; // log_info("att_data_packet with opcode 0x%x", packet[0]);
att_client_handler(packet_type, handle, packet, size); if (!subscriptions[index].packet_handler) return;
} else { subscriptions[index].packet_handler(packet_type, handle, packet, size);
// even PDUs are sent from client to server break;
if (!att_server_handler) return; case HCI_EVENT_PACKET:
att_server_handler(packet_type, handle, packet, size); if (packet[0] != L2CAP_EVENT_CAN_SEND_NOW) break;
} for (index = 0; index < 2 ; index++){
break; if (subscriptions[index].packet_handler && subscriptions[index].waiting_for_can_send){
case HCI_EVENT_PACKET: subscriptions[index].waiting_for_can_send = 0;
if (packet[0] != L2CAP_EVENT_CAN_SEND_NOW) break; subscriptions[index].packet_handler(packet_type, handle, packet, size);
if (att_server_handler && att_server_waiting_for_can_send){ // stop if client cannot send anymore
att_server_waiting_for_can_send = 0; if (!hci_can_send_acl_le_packet_now()) break;
att_server_handler(packet_type, handle, packet, size); }
// stop if client cannot send anymore }
if (!hci_can_send_acl_le_packet_now()) break; break;
} default:
if (att_client_handler && att_client_waiting_for_can_send){ break;
att_client_waiting_for_can_send = 0; }
att_client_handler(packet_type, handle, packet, size);
}
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 * @param packet_hander for ATT client packets
*/ */
void att_dispatch_register_client(btstack_packet_handler_t packet_handler){ void att_dispatch_register_client(btstack_packet_handler_t packet_handler){
att_client_handler = packet_handler; subscriptions[ATT_CLIENT].packet_handler = packet_handler;
l2cap_register_fixed_channel(att_packet_handler, L2CAP_CID_ATTRIBUTE_PROTOCOL); 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 * @param packet_hander for ATT server packets
*/ */
void att_dispatch_register_server(btstack_packet_handler_t packet_handler){ void att_dispatch_register_server(btstack_packet_handler_t packet_handler){
att_server_handler = packet_handler; subscriptions[ATT_SERVER].packet_handler = packet_handler;
l2cap_register_fixed_channel(att_packet_handler, L2CAP_CID_ATTRIBUTE_PROTOCOL); 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 * @param handle
*/ */
int att_dispatch_client_can_send_now(hci_con_handle_t con_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 * @param handle
*/ */
int att_dispatch_server_can_send_now(hci_con_handle_t con_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 * @param con_handle
*/ */
void att_dispatch_client_request_can_send_now_event(hci_con_handle_t con_handle){ void att_dispatch_client_request_can_send_now_event(hci_con_handle_t con_handle){
att_client_waiting_for_can_send = 1; subscriptions[ATT_CLIENT].waiting_for_can_send = 1;
l2cap_request_can_send_fix_channel_now_event(con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL); 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 * @param con_handle
*/ */
void att_dispatch_server_request_can_send_now_event(hci_con_handle_t con_handle){ void att_dispatch_server_request_can_send_now_event(hci_con_handle_t con_handle){
att_server_waiting_for_can_send = 1; subscriptions[ATT_SERVER].waiting_for_can_send = 1;
l2cap_request_can_send_fix_channel_now_event(con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL); 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){ 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){ 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){ 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);
} }