xref: /btstack/src/ble/att_server.h (revision 29f2ca21493f94395d6c62229db4a1caca6355b7)
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
232fca4dadSMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka Ringwald  * GMBH 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  */
37fe5a6c4eSMilanka Ringwald 
38fe5a6c4eSMilanka Ringwald /**
39fe5a6c4eSMilanka Ringwald  * @title ATT Server
40fe5a6c4eSMilanka Ringwald  *
41fe5a6c4eSMilanka Ringwald  */
42fe5a6c4eSMilanka Ringwald 
4380e33422SMatthias Ringwald #ifndef ATT_SERVER_H
4480e33422SMatthias Ringwald #define ATT_SERVER_H
453deb3ec6SMatthias Ringwald 
463deb3ec6SMatthias Ringwald #include <stdint.h>
47*29f2ca21SMatthias Ringwald #include "hci.h"
48591423b2SMatthias Ringwald #include "ble/att_db.h"
4913e6e052SMatthias Ringwald #include "btstack_defines.h"
50eb40c7dbSMilanka Ringwald #include "btstack_config.h"
513deb3ec6SMatthias Ringwald 
523deb3ec6SMatthias Ringwald #if defined __cplusplus
533deb3ec6SMatthias Ringwald extern "C" {
543deb3ec6SMatthias Ringwald #endif
553deb3ec6SMatthias Ringwald 
56*29f2ca21SMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT
57*29f2ca21SMatthias Ringwald typedef struct {
58*29f2ca21SMatthias Ringwald     btstack_linked_item_t item;
59*29f2ca21SMatthias Ringwald     att_server_t     att_server;
60*29f2ca21SMatthias Ringwald     att_connection_t att_connection;
61*29f2ca21SMatthias Ringwald     uint8_t * receive_buffer;
62*29f2ca21SMatthias Ringwald     uint8_t * send_buffer;
63*29f2ca21SMatthias Ringwald } att_server_eatt_bearer_t;
64*29f2ca21SMatthias Ringwald #endif
65*29f2ca21SMatthias Ringwald 
663deb3ec6SMatthias Ringwald /* API_START */
673deb3ec6SMatthias Ringwald /*
683deb3ec6SMatthias Ringwald  * @brief setup ATT server
69acc8ef23SMatthias Ringwald  * @param db attribute database created by compile-gatt.py
70591423b2SMatthias Ringwald  * @param read_callback, see att_db.h, can be NULL
713deb3ec6SMatthias Ringwald  * @param write_callback, see attl.h, can be NULL
723deb3ec6SMatthias Ringwald  */
733deb3ec6SMatthias Ringwald void att_server_init(uint8_t const * db, att_read_callback_t read_callback, att_write_callback_t write_callback);
743deb3ec6SMatthias Ringwald 
7520d4358dSMatthias Ringwald /**
7620d4358dSMatthias Ringwald  * @brief Enable support for Enhanced ATT bearer
7720d4358dSMatthias Ringwald  * @note Requires ENABLE_GATT_OVER_EATT
7820d4358dSMatthias Ringwald  * @param num_eatt_bearers
7920d4358dSMatthias Ringwald  * @param storage_buffer
80*29f2ca21SMatthias Ringwald  * @param storage_size must be >= num_eatt_bearers * sizeof(att_server_eatt_bearer_t)
81*29f2ca21SMatthias Ringwald  * @return status   ERROR_CODE_SUCCESS
82*29f2ca21SMatthias Ringwald  *                  ERROR_CODE_MEMORY_CAPACITY_EXCEEDED if buffer too small or no entry in l2cap service pool
83*29f2ca21SMatthias Ringwald  *                  L2CAP_SERVICE_ALREADY_REGISTERED if called twice
8420d4358dSMatthias Ringwald  */
8520d4358dSMatthias Ringwald uint8_t att_server_eatt_init(uint8_t num_eatt_bearers, uint8_t * storage_buffer, uint16_t storage_size);
8620d4358dSMatthias Ringwald 
873deb3ec6SMatthias Ringwald /*
88dc98f6d3SMatthias Ringwald  * @brief register packet handler for ATT server events:
89bb38f057SMatthias Ringwald  *        - ATT_EVENT_CAN_SEND_NOW
90dc98f6d3SMatthias Ringwald  *        - ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE
91bb38f057SMatthias Ringwald  *        - ATT_EVENT_MTU_EXCHANGE_COMPLETE
923deb3ec6SMatthias Ringwald  * @param handler
933deb3ec6SMatthias Ringwald  */
943deb3ec6SMatthias Ringwald void att_server_register_packet_handler(btstack_packet_handler_t handler);
953deb3ec6SMatthias Ringwald 
963d71c7a4SMatthias Ringwald /**
973d71c7a4SMatthias Ringwald  * @brief register read/write callbacks for specific handle range
983d71c7a4SMatthias Ringwald  * @param att_service_handler_t
993d71c7a4SMatthias Ringwald  */
1003d71c7a4SMatthias Ringwald void att_server_register_service_handler(att_service_handler_t * handler);
1013d71c7a4SMatthias Ringwald 
102bb38f057SMatthias Ringwald /**
103bb38f057SMatthias Ringwald  * @brief Request callback when sending is possible
104bb38f057SMatthias Ringwald  * @note callback might happend during call to this function
105bb38f057SMatthias Ringwald  * @param callback_registration to point to callback function and context information
106bb38f057SMatthias Ringwald  * @param con_handle
107969a5bbaSMatthias Ringwald  * @return 0 if ok, error otherwise
108bb38f057SMatthias Ringwald  */
1099816429dSMatthias Ringwald uint8_t att_server_register_can_send_now_callback(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle);
110bb38f057SMatthias Ringwald 
1118f9132b5SMilanka Ringwald /**
1128f9132b5SMilanka Ringwald  * @brief Return ATT MTU
1138f9132b5SMilanka Ringwald  * @param con_handle
1148f9132b5SMilanka Ringwald  * @return mtu if ok, 0 otherwise
1158f9132b5SMilanka Ringwald  */
1168f9132b5SMilanka Ringwald uint16_t att_server_get_mtu(hci_con_handle_t con_handle);
117cbbb12d9SMatthias Ringwald 
118cbbb12d9SMatthias Ringwald /**
119cbbb12d9SMatthias Ringwald  * @brief Request callback when sending notifcation is possible
120cbbb12d9SMatthias Ringwald  * @note callback might happend during call to this function
121cbbb12d9SMatthias Ringwald  * @param callback_registration to point to callback function and context information
122cbbb12d9SMatthias Ringwald  * @param con_handle
123c37df6f6SMatthias Ringwald  * @return ERROR_CODE_SUCCESS if ok, ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER if handle unknown, and ERROR_CODE_COMMAND_DISALLOWED if callback already registered
124cbbb12d9SMatthias Ringwald  */
1259816429dSMatthias Ringwald uint8_t att_server_request_to_send_notification(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle);
126cbbb12d9SMatthias Ringwald 
127cbbb12d9SMatthias Ringwald /**
128cbbb12d9SMatthias Ringwald  * @brief Request callback when sending indication is possible
129cbbb12d9SMatthias Ringwald  * @note callback might happend during call to this function
130cbbb12d9SMatthias Ringwald  * @param callback_registration to point to callback function and context information
131cbbb12d9SMatthias Ringwald  * @param con_handle
132c37df6f6SMatthias Ringwald  * @return ERROR_CODE_SUCCESS if ok, ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER if handle unknown, and ERROR_CODE_COMMAND_DISALLOWED if callback already registered
133cbbb12d9SMatthias Ringwald  */
1349816429dSMatthias Ringwald uint8_t att_server_request_to_send_indication(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle);
135cbbb12d9SMatthias Ringwald 
13635d7f2dfSMilanka Ringwald /**
1373deb3ec6SMatthias Ringwald  * @brief notify client about attribute value change
138c141988cSMatthias Ringwald  * @param con_handle
139fc64f94aSMatthias Ringwald  * @param attribute_handle
140fc64f94aSMatthias Ringwald  * @param value
141fc64f94aSMatthias Ringwald  * @param value_len
1423deb3ec6SMatthias Ringwald  * @return 0 if ok, error otherwise
1433deb3ec6SMatthias Ringwald  */
1449816429dSMatthias Ringwald uint8_t att_server_notify(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t *value, uint16_t value_len);
1453deb3ec6SMatthias Ringwald 
14635d7f2dfSMilanka Ringwald /**
14740f8d8f2SMatthias Ringwald  * @brief notify client about multiple attribute value changes
14840f8d8f2SMatthias Ringwald  * @param con_handle
14940f8d8f2SMatthias Ringwald  * @param num_attributes
15040f8d8f2SMatthias Ringwald  * @param attribute_handles[]
15140f8d8f2SMatthias Ringwald  * @param values_data[]
15240f8d8f2SMatthias Ringwald  * @param values_len[]
15340f8d8f2SMatthias Ringwald  * @return 0 if ok, error otherwise
15440f8d8f2SMatthias Ringwald  */
15540f8d8f2SMatthias Ringwald uint8_t att_server_multiple_notify(hci_con_handle_t con_handle, uint8_t num_attributes,
15640f8d8f2SMatthias Ringwald                                    const uint16_t * attribute_handles, const uint8_t ** values_data, const uint16_t * values_len);
15740f8d8f2SMatthias Ringwald 
15840f8d8f2SMatthias Ringwald /**
1593deb3ec6SMatthias Ringwald  * @brief indicate value change to client. client is supposed to reply with an indication_response
160c141988cSMatthias Ringwald  * @param con_handle
161fc64f94aSMatthias Ringwald  * @param attribute_handle
162fc64f94aSMatthias Ringwald  * @param value
163fc64f94aSMatthias Ringwald  * @param value_len
1643deb3ec6SMatthias Ringwald  * @return 0 if ok, error otherwise
1653deb3ec6SMatthias Ringwald  */
1669816429dSMatthias Ringwald uint8_t att_server_indicate(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t *value, uint16_t value_len);
1673deb3ec6SMatthias Ringwald 
168beb20288SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE
16935d7f2dfSMilanka Ringwald /**
170beb20288SMatthias Ringwald  * @brief response ready - called after returning ATT_READ__RESPONSE_PENDING in an att_read_callback or
171beb20288SMatthias Ringwald  * ATT_ERROR_WRITE_REQUEST_PENDING IN att_write_callback before to trigger callback again and complete the transaction
172e404a688SMatthias Ringwald  * @nore The ATT Server will retry handling the current ATT request
173e404a688SMatthias Ringwald  * @param con_handle
174e404a688SMatthias Ringwald  * @return 0 if ok, error otherwise
175e404a688SMatthias Ringwald  */
1769816429dSMatthias Ringwald uint8_t att_server_response_ready(hci_con_handle_t con_handle);
177e404a688SMatthias Ringwald #endif
178e404a688SMatthias Ringwald 
17935d7f2dfSMilanka Ringwald /**
18035d7f2dfSMilanka Ringwald  * De-Init ATT Server
18135d7f2dfSMilanka Ringwald  */
18235d7f2dfSMilanka Ringwald void att_server_deinit(void);
18335d7f2dfSMilanka Ringwald 
1848f9132b5SMilanka Ringwald // the following functions will be removed soon
1858f9132b5SMilanka Ringwald 
18635d7f2dfSMilanka Ringwald /**
1878f9132b5SMilanka Ringwald  * @brief tests if a notification or indication can be send right now
1888f9132b5SMilanka Ringwald  * @param con_handle
1898f9132b5SMilanka Ringwald  * @return 1, if packet can be sent
1908f9132b5SMilanka Ringwald  */
1918f9132b5SMilanka Ringwald int  att_server_can_send_packet_now(hci_con_handle_t con_handle);
1928f9132b5SMilanka Ringwald 
1938f9132b5SMilanka Ringwald /**
1948f9132b5SMilanka Ringwald  * @brief Request emission of ATT_EVENT_CAN_SEND_NOW as soon as possible
1958f9132b5SMilanka Ringwald  * @note ATT_EVENT_CAN_SEND_NOW might be emitted during call to this function
1968f9132b5SMilanka Ringwald  *       so packet handler should be ready to handle it
1978f9132b5SMilanka Ringwald  * @param con_handle
1988f9132b5SMilanka Ringwald  */
1998f9132b5SMilanka Ringwald void att_server_request_can_send_now_event(hci_con_handle_t con_handle);
2008f9132b5SMilanka Ringwald // end of deprecated functions
2018f9132b5SMilanka Ringwald 
2023deb3ec6SMatthias Ringwald /* API_END */
2033deb3ec6SMatthias Ringwald 
2043deb3ec6SMatthias Ringwald #if defined __cplusplus
2053deb3ec6SMatthias Ringwald }
2063deb3ec6SMatthias Ringwald #endif
2073deb3ec6SMatthias Ringwald 
20880e33422SMatthias Ringwald #endif // ATT_SERVER_H
209