xref: /btstack/src/ble/att_dispatch.c (revision 6973c35a87cc7f398c429d8942b18d0a401e28a3)
1 /*
2  * Copyright (C) 2014 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 #define __BTSTACK_FILE__ "att_dispatch.c"
39 
40 
41 /**
42  * Dispatcher for independent implementation of ATT client and server
43  */
44 
45 #include "att_dispatch.h"
46 #include "ble/core.h"
47 #include "btstack_debug.h"
48 #include "l2cap.h"
49 
50 static btstack_packet_handler_t att_client_handler;
51 static btstack_packet_handler_t att_server_handler;
52 
53 static uint8_t att_client_waiting_for_can_send;
54 static uint8_t att_server_waiting_for_can_send;
55 
56 static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){
57 	switch (packet_type){
58 		case ATT_DATA_PACKET:
59 			// log_info("att_data_packet with opcode 0x%x", packet[0]);
60 			if (packet[0] & 1){
61 				// odd PDUs are sent from server to client
62 				if (!att_client_handler) return;
63 				att_client_handler(packet_type, handle, packet, size);
64 			} else {
65 				// even PDUs are sent from client to server
66 				if (!att_server_handler) return;
67 				att_server_handler(packet_type, handle, packet, size);
68 			}
69 			break;
70 		case HCI_EVENT_PACKET:
71 			if (packet[0] != L2CAP_EVENT_CAN_SEND_NOW) break;
72 			if (att_server_handler && att_server_waiting_for_can_send){
73 				att_server_waiting_for_can_send = 0;
74 				att_server_handler(packet_type, handle, packet, size);
75 				// stop if client cannot send anymore
76 				if (!hci_can_send_acl_le_packet_now()) break;
77 			}
78 			if (att_client_handler && att_client_waiting_for_can_send){
79 				att_client_waiting_for_can_send = 0;
80 				att_client_handler(packet_type, handle, packet, size);
81 			}
82 			break;
83 		default:
84 			break;
85 	}
86 }
87 
88 /**
89  * @brief reset att dispatchter
90  * @param packet_hander for ATT client packets
91  */
92 void att_dispatch_register_client(btstack_packet_handler_t packet_handler){
93 	att_client_handler = packet_handler;
94 	l2cap_register_fixed_channel(att_packet_handler, L2CAP_CID_ATTRIBUTE_PROTOCOL);
95 }
96 
97 /**
98  * @brief reset att dispatchter
99  * @param packet_hander for ATT server packets
100  */
101 void att_dispatch_register_server(btstack_packet_handler_t packet_handler){
102 	att_server_handler = packet_handler;
103 	l2cap_register_fixed_channel(att_packet_handler, L2CAP_CID_ATTRIBUTE_PROTOCOL);
104 }
105 
106 /**
107  * @brief can send packet for client
108  * @param handle
109  */
110 int att_dispatch_client_can_send_now(hci_con_handle_t con_handle){
111 	return l2cap_can_send_fixed_channel_packet_now(con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL);
112 }
113 
114 /**
115  * @brief can send packet for server
116  * @param handle
117  */
118 int att_dispatch_server_can_send_now(hci_con_handle_t con_handle){
119 	return l2cap_can_send_fixed_channel_packet_now(con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL);
120 }
121 
122 /**
123  * @brief Request emission of L2CAP_EVENT_CAN_SEND_NOW as soon as possible for client
124  * @note L2CAP_EVENT_CAN_SEND_NOW might be emitted during call to this function
125  *       so packet handler should be ready to handle it
126  * @param con_handle
127  */
128 void att_dispatch_client_request_can_send_now_event(hci_con_handle_t con_handle){
129 	att_client_waiting_for_can_send = 1;
130 	l2cap_request_can_send_fix_channel_now_event(con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL);
131 }
132 
133 /**
134  * @brief Request emission of L2CAP_EVENT_CAN_SEND_NOW as soon as possible for server
135  * @note L2CAP_EVENT_CAN_SEND_NOW might be emitted during call to this function
136  *       so packet handler should be ready to handle it
137  * @param con_handle
138  */
139 void att_dispatch_server_request_can_send_now_event(hci_con_handle_t con_handle){
140 	att_server_waiting_for_can_send = 1;
141 	l2cap_request_can_send_fix_channel_now_event(con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL);
142 }
143 
144 void att_dispatch_server_mtu_exchanged(hci_con_handle_t con_handle, uint16_t new_mtu){
145     if (!att_client_handler) return;
146     uint8_t packet[6];
147     packet[0] = ATT_EVENT_MTU_EXCHANGE_COMPLETE;
148     packet[1] = sizeof(packet) - 2;
149     little_endian_store_16(packet, 2, con_handle);
150     little_endian_store_16(packet, 4, new_mtu);
151     att_client_handler(ATT_DATA_PACKET, con_handle, packet, 1);
152 }
153 
154 void att_dispatch_client_mtu_exchanged(hci_con_handle_t con_handle, uint16_t new_mtu){
155     if (!att_server_handler) return;
156     uint8_t packet[6];
157     packet[0] = ATT_EVENT_MTU_EXCHANGE_COMPLETE;
158     packet[1] = sizeof(packet) - 2;
159     little_endian_store_16(packet, 2, con_handle);
160     little_endian_store_16(packet, 4, new_mtu);
161     att_server_handler(ATT_DATA_PACKET, con_handle, packet, 1);
162 }
163