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,37 +47,33 @@
#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){
int index;
switch (packet_type){ switch (packet_type){
case ATT_DATA_PACKET: 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]); // log_info("att_data_packet with opcode 0x%x", packet[0]);
if (packet[0] & 1){ if (!subscriptions[index].packet_handler) return;
// odd PDUs are sent from server to client subscriptions[index].packet_handler(packet_type, handle, packet, size);
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; break;
case HCI_EVENT_PACKET: case HCI_EVENT_PACKET:
if (packet[0] != L2CAP_EVENT_CAN_SEND_NOW) break; if (packet[0] != L2CAP_EVENT_CAN_SEND_NOW) break;
if (att_server_handler && att_server_waiting_for_can_send){ for (index = 0; index < 2 ; index++){
att_server_waiting_for_can_send = 0; if (subscriptions[index].packet_handler && subscriptions[index].waiting_for_can_send){
att_server_handler(packet_type, handle, packet, size); subscriptions[index].waiting_for_can_send = 0;
subscriptions[index].packet_handler(packet_type, handle, packet, size);
// stop if client cannot send anymore // stop if client cannot send anymore
if (!hci_can_send_acl_le_packet_now()) break; 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; break;
default: default:
@ -90,7 +86,7 @@ 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,7 +95,7 @@ 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);
} }
@ -126,7 +122,7 @@ 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,7 +133,7 @@ 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);
} }
@ -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);
} }