xref: /btstack/src/ble/att_dispatch.c (revision 202c8a4c4c7638e8dcfc3147506868bad36804a0)
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 
383deb3ec6SMatthias Ringwald 
393deb3ec6SMatthias Ringwald /**
403deb3ec6SMatthias Ringwald  * Dispatcher for independent implementation of ATT client and server
413deb3ec6SMatthias Ringwald  */
423deb3ec6SMatthias Ringwald 
433deb3ec6SMatthias Ringwald #include "att_dispatch.h"
4459c6af15SMatthias Ringwald #include "ble/core.h"
4516ece135SMatthias Ringwald #include "btstack_debug.h"
4659c6af15SMatthias Ringwald #include "l2cap.h"
473deb3ec6SMatthias Ringwald 
4819448c0bSMatthias Ringwald static btstack_packet_handler_t att_client_handler;
4919448c0bSMatthias Ringwald static btstack_packet_handler_t att_server_handler;
50773c4106SMatthias Ringwald 
51773c4106SMatthias Ringwald static uint8_t att_client_waiting_for_can_send;
52773c4106SMatthias Ringwald static uint8_t att_server_waiting_for_can_send;
533deb3ec6SMatthias Ringwald 
543deb3ec6SMatthias Ringwald static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){
55773c4106SMatthias Ringwald 	switch (packet_type){
56773c4106SMatthias Ringwald 		case ATT_DATA_PACKET:
573deb3ec6SMatthias Ringwald 			// log_info("att_data_packet with opcode 0x%x", packet[0]);
58773c4106SMatthias Ringwald 			if (packet[0] & 1){
593deb3ec6SMatthias Ringwald 				// odd PDUs are sent from server to client
6019448c0bSMatthias Ringwald 				if (!att_client_handler) return;
613deb3ec6SMatthias Ringwald 				att_client_handler(packet_type, handle, packet, size);
623deb3ec6SMatthias Ringwald 			} else {
633deb3ec6SMatthias Ringwald 				// even PDUs are sent from client to server
6419448c0bSMatthias Ringwald 				if (!att_server_handler) return;
653deb3ec6SMatthias Ringwald 				att_server_handler(packet_type, handle, packet, size);
663deb3ec6SMatthias Ringwald 			}
67*202c8a4cSMatthias Ringwald 			break;
68773c4106SMatthias Ringwald 		case HCI_EVENT_PACKET:
69773c4106SMatthias Ringwald 			if (packet[0] != L2CAP_EVENT_CAN_SEND_NOW) break;
70773c4106SMatthias Ringwald 			if (att_server_handler && att_server_waiting_for_can_send){
71773c4106SMatthias Ringwald 				att_server_waiting_for_can_send = 0;
72773c4106SMatthias Ringwald 				att_server_handler(packet_type, handle, packet, size);
73773c4106SMatthias Ringwald 				// stop if client cannot send anymore
74b947e684SMatthias Ringwald 				if (!hci_can_send_acl_le_packet_now()) break;
75773c4106SMatthias Ringwald 			}
76773c4106SMatthias Ringwald 			if (att_client_handler && att_client_waiting_for_can_send){
77773c4106SMatthias Ringwald 				att_client_waiting_for_can_send = 0;
78773c4106SMatthias Ringwald 				att_client_handler(packet_type, handle, packet, size);
79773c4106SMatthias Ringwald 			}
80773c4106SMatthias Ringwald 			break;
81773c4106SMatthias Ringwald 		default:
82773c4106SMatthias Ringwald 			break;
83773c4106SMatthias Ringwald 	}
843deb3ec6SMatthias Ringwald }
853deb3ec6SMatthias Ringwald 
863deb3ec6SMatthias Ringwald /**
873deb3ec6SMatthias Ringwald  * @brief reset att dispatchter
883deb3ec6SMatthias Ringwald  * @param packet_hander for ATT client packets
893deb3ec6SMatthias Ringwald  */
903deb3ec6SMatthias Ringwald void att_dispatch_register_client(btstack_packet_handler_t packet_handler){
913deb3ec6SMatthias Ringwald 	att_client_handler = packet_handler;
923deb3ec6SMatthias Ringwald 	l2cap_register_fixed_channel(att_packet_handler, L2CAP_CID_ATTRIBUTE_PROTOCOL);
933deb3ec6SMatthias Ringwald }
943deb3ec6SMatthias Ringwald 
953deb3ec6SMatthias Ringwald /**
963deb3ec6SMatthias Ringwald  * @brief reset att dispatchter
973deb3ec6SMatthias Ringwald  * @param packet_hander for ATT server packets
983deb3ec6SMatthias Ringwald  */
993deb3ec6SMatthias Ringwald void att_dispatch_register_server(btstack_packet_handler_t packet_handler){
1003deb3ec6SMatthias Ringwald 	att_server_handler = packet_handler;
1013deb3ec6SMatthias Ringwald 	l2cap_register_fixed_channel(att_packet_handler, L2CAP_CID_ATTRIBUTE_PROTOCOL);
1023deb3ec6SMatthias Ringwald }
10319448c0bSMatthias Ringwald 
10419448c0bSMatthias Ringwald /**
10519448c0bSMatthias Ringwald  * @brief can send packet for client
10619448c0bSMatthias Ringwald  * @param handle
10719448c0bSMatthias Ringwald  */
108fc64f94aSMatthias Ringwald int att_dispatch_client_can_send_now(hci_con_handle_t con_handle){
1090b9d7e78SMatthias Ringwald 	return l2cap_can_send_fixed_channel_packet_now(con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL);
11019448c0bSMatthias Ringwald }
11119448c0bSMatthias Ringwald 
11219448c0bSMatthias Ringwald /**
11319448c0bSMatthias Ringwald  * @brief can send packet for server
11419448c0bSMatthias Ringwald  * @param handle
11519448c0bSMatthias Ringwald  */
116fc64f94aSMatthias Ringwald int att_dispatch_server_can_send_now(hci_con_handle_t con_handle){
1170b9d7e78SMatthias Ringwald 	return l2cap_can_send_fixed_channel_packet_now(con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL);
118773c4106SMatthias Ringwald }
1190b9d7e78SMatthias Ringwald 
1200b9d7e78SMatthias Ringwald /**
1210b9d7e78SMatthias Ringwald  * @brief Request emission of L2CAP_EVENT_CAN_SEND_NOW as soon as possible for client
1220b9d7e78SMatthias Ringwald  * @note L2CAP_EVENT_CAN_SEND_NOW might be emitted during call to this function
1230b9d7e78SMatthias Ringwald  *       so packet handler should be ready to handle it
1240b9d7e78SMatthias Ringwald  * @param con_handle
1250b9d7e78SMatthias Ringwald  */
1260b9d7e78SMatthias Ringwald void att_dispatch_client_request_can_send_now_event(hci_con_handle_t con_handle){
1270b9d7e78SMatthias Ringwald 	att_client_waiting_for_can_send = 1;
1280b9d7e78SMatthias Ringwald 	l2cap_request_can_send_fix_channel_now_event(con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL);
1290b9d7e78SMatthias Ringwald }
1300b9d7e78SMatthias Ringwald 
1310b9d7e78SMatthias Ringwald /**
1320b9d7e78SMatthias Ringwald  * @brief Request emission of L2CAP_EVENT_CAN_SEND_NOW as soon as possible for server
1330b9d7e78SMatthias Ringwald  * @note L2CAP_EVENT_CAN_SEND_NOW might be emitted during call to this function
1340b9d7e78SMatthias Ringwald  *       so packet handler should be ready to handle it
1350b9d7e78SMatthias Ringwald  * @param con_handle
1360b9d7e78SMatthias Ringwald  */
1370b9d7e78SMatthias Ringwald void att_dispatch_server_request_can_send_now_event(hci_con_handle_t con_handle){
1380b9d7e78SMatthias Ringwald 	att_server_waiting_for_can_send = 1;
1390b9d7e78SMatthias Ringwald 	l2cap_request_can_send_fix_channel_now_event(con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL);
14019448c0bSMatthias Ringwald }
141