xref: /btstack/src/ble/att_dispatch.c (revision 19448c0b45b19632d9c9af4e04b06b65ca5064b2)
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 "l2cap.h"
443deb3ec6SMatthias Ringwald 
453deb3ec6SMatthias Ringwald #include "att_dispatch.h"
4616ece135SMatthias Ringwald #include "btstack_debug.h"
473deb3ec6SMatthias Ringwald 
48*19448c0bSMatthias Ringwald static btstack_packet_handler_t att_client_handler;
49*19448c0bSMatthias Ringwald static btstack_packet_handler_t att_server_handler;
50*19448c0bSMatthias Ringwald // static uint8_t att_client_waiting_for_can_send;
51*19448c0bSMatthias Ringwald // static uint8_t att_server_waiting_for_can_send;
523deb3ec6SMatthias Ringwald 
533deb3ec6SMatthias Ringwald static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){
54ca7f8243SMatthias Ringwald 	if (packet_type != ATT_DATA_PACKET) return;
55ca7f8243SMatthias Ringwald 
563deb3ec6SMatthias Ringwald 	// log_info("att_data_packet with opcode 0x%x", packet[0]);
573deb3ec6SMatthias Ringwald 	uint8_t att_pdu_odd = packet[0] & 1;
583deb3ec6SMatthias Ringwald 	if (att_pdu_odd){
593deb3ec6SMatthias Ringwald 		// odd PDUs are sent from server to client
60*19448c0bSMatthias 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
64*19448c0bSMatthias Ringwald 		if (!att_server_handler) return;
653deb3ec6SMatthias Ringwald 		att_server_handler(packet_type, handle, packet, size);
663deb3ec6SMatthias Ringwald 	}
673deb3ec6SMatthias Ringwald }
683deb3ec6SMatthias Ringwald 
693deb3ec6SMatthias Ringwald /**
703deb3ec6SMatthias Ringwald  * @brief reset att dispatchter
713deb3ec6SMatthias Ringwald  * @param packet_hander for ATT client packets
723deb3ec6SMatthias Ringwald  */
733deb3ec6SMatthias Ringwald void att_dispatch_register_client(btstack_packet_handler_t packet_handler){
743deb3ec6SMatthias Ringwald 	att_client_handler = packet_handler;
753deb3ec6SMatthias Ringwald 	l2cap_register_fixed_channel(att_packet_handler, L2CAP_CID_ATTRIBUTE_PROTOCOL);
763deb3ec6SMatthias Ringwald }
773deb3ec6SMatthias Ringwald 
783deb3ec6SMatthias Ringwald /**
793deb3ec6SMatthias Ringwald  * @brief reset att dispatchter
803deb3ec6SMatthias Ringwald  * @param packet_hander for ATT server packets
813deb3ec6SMatthias Ringwald  */
823deb3ec6SMatthias Ringwald void att_dispatch_register_server(btstack_packet_handler_t packet_handler){
833deb3ec6SMatthias Ringwald 	att_server_handler = packet_handler;
843deb3ec6SMatthias Ringwald 	l2cap_register_fixed_channel(att_packet_handler, L2CAP_CID_ATTRIBUTE_PROTOCOL);
853deb3ec6SMatthias Ringwald }
86*19448c0bSMatthias Ringwald 
87*19448c0bSMatthias Ringwald /**
88*19448c0bSMatthias Ringwald  * @brief can send packet for client
89*19448c0bSMatthias Ringwald  * @param handle
90*19448c0bSMatthias Ringwald  */
91*19448c0bSMatthias Ringwald int att_dispatch_client_can_send_now(uint16_t handle){
92*19448c0bSMatthias Ringwald 	int res = l2cap_can_send_fixed_channel_packet_now(handle);
93*19448c0bSMatthias Ringwald 	// if (!res){
94*19448c0bSMatthias Ringwald 	// 	att_client_waiting_for_can_send =1;
95*19448c0bSMatthias Ringwald 	// }
96*19448c0bSMatthias Ringwald 	return res;
97*19448c0bSMatthias Ringwald }
98*19448c0bSMatthias Ringwald 
99*19448c0bSMatthias Ringwald /**
100*19448c0bSMatthias Ringwald  * @brief can send packet for server
101*19448c0bSMatthias Ringwald  * @param handle
102*19448c0bSMatthias Ringwald  */
103*19448c0bSMatthias Ringwald int att_dispatch_server_can_send_now(uint16_t handle){
104*19448c0bSMatthias Ringwald 	int res = l2cap_can_send_fixed_channel_packet_now(handle);
105*19448c0bSMatthias Ringwald 	// if (!res){
106*19448c0bSMatthias Ringwald 	// 	att_server_waiting_for_can_send =1;
107*19448c0bSMatthias Ringwald 	// }
108*19448c0bSMatthias Ringwald 	return res;
109*19448c0bSMatthias Ringwald }
110