att_delayed_response.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * Copyright (C) 2018 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. * contact@bluekitchen-gmbh.com
  35. *
  36. */
  37. #define BTSTACK_FILE__ "att_delayed_response.c"
  38. // *****************************************************************************
  39. /* EXAMPLE_START(att_delayed_response): LE Peripheral - Delayed Response
  40. *
  41. * @text If the value for a GATT Chararacteristic isn't availabl for read,
  42. * the value ATT_READ_RESPONSE_PENDING can be returned. When the value is available,
  43. * att_server_response_ready is then called to complete the ATT request.
  44. *
  45. * Similarly, the error code ATT_ERROR_WRITE_RESPONSE_PENING can be returned when
  46. * it is unclear if a write can be performed or not. When the decision was made,
  47. * att_server_response_ready is is then called to complete the ATT request.
  48. */
  49. // *****************************************************************************
  50. #include <stdint.h>
  51. #include <stdio.h>
  52. #include <stdlib.h>
  53. #include <string.h>
  54. #include "btstack.h"
  55. // att_delayed_response.gatt contains the declaration of the provided GATT Services + Characteristics
  56. // att_delayed_response.h contains the binary representation of att_delayed_response.gatt
  57. // it is generated by the build system by calling: $BTSTACK_ROOT/tool/compile_gatt.py att_delayed_response.gatt att_delayed_response.h
  58. // it needs to be regenerated when the GATT Database declared in att_delayed_response.gatt file is modified
  59. #include "att_delayed_response.h"
  60. #define ATT_VALUE_DELAY_MS 3000
  61. #define ATT_VALUE_INVALID_MS 5000
  62. /* @section Main Application Setup
  63. *
  64. * @text Listing MainConfiguration shows main application code.
  65. * It initializes L2CAP, the Security Manager and configures the ATT Server with the pre-compiled
  66. * ATT Database generated from $att_delayed_response.gatt$.
  67. * Additionally, it enables the Battery Service Server with the current battery level.
  68. * Finally, it configures the advertisements and boots the Bluetooth stack.
  69. * In this example, the Advertisement contains the Flags attribute and the device name.
  70. * The flag 0x06 indicates: LE General Discoverable Mode and BR/EDR not supported.
  71. */
  72. /* LISTING_START(MainConfiguration): Init L2CAP SM ATT Server */
  73. #ifdef ENABLE_ATT_DELAYED_RESPONSE
  74. static btstack_timer_source_t att_timer;
  75. static hci_con_handle_t con_handle;
  76. static int value_ready;
  77. #endif
  78. static uint16_t att_read_callback(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size);
  79. static int att_write_callback(hci_con_handle_t connection_handle, uint16_t att_handle, uint16_t transaction_mode, uint16_t offset, uint8_t *buffer, uint16_t buffer_size);
  80. const uint8_t adv_data[] = {
  81. // Flags general discoverable, BR/EDR not supported
  82. 0x02, 0x01, 0x06,
  83. // Name
  84. 0x08, 0x09, 'D', 'e', 'l', 'a', 'y', 'e', 'd',
  85. };
  86. const uint8_t adv_data_len = sizeof(adv_data);
  87. const char * test_string = "Delayed response";
  88. static void example_setup(void){
  89. l2cap_init();
  90. // setup le device db
  91. le_device_db_init();
  92. // setup SM: Display only
  93. sm_init();
  94. // setup ATT server
  95. att_server_init(profile_data, att_read_callback, att_write_callback);
  96. // setup advertisements
  97. uint16_t adv_int_min = 0x0030;
  98. uint16_t adv_int_max = 0x0030;
  99. uint8_t adv_type = 0;
  100. bd_addr_t null_addr;
  101. memset(null_addr, 0, 6);
  102. gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00);
  103. gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data);
  104. gap_advertisements_enable(1);
  105. }
  106. /* LISTING_END */
  107. /*
  108. * @section att_invalidate_value Handler
  109. *
  110. * @text The att_invalidate_value handler 'invalidates' the value of the single Characteristic provided in this example
  111. */
  112. #ifdef ENABLE_ATT_DELAYED_RESPONSE
  113. static void att_invalidate_value(struct btstack_timer_source *ts){
  114. UNUSED(ts);
  115. printf("Value got stale\n");
  116. value_ready = 0;
  117. }
  118. #endif
  119. /*
  120. * @section att_update_value Handler
  121. *
  122. * @text The att_update_value handler 'updates' the value of the single Characteristic provided in this example
  123. */
  124. /* LISTING_START(att_read_delay): ATT Read Delay Handler */
  125. #ifdef ENABLE_ATT_DELAYED_RESPONSE
  126. static void att_update_value(struct btstack_timer_source *ts){
  127. UNUSED(ts);
  128. value_ready = 1;
  129. // trigger ATT Server to try request again
  130. int status = att_server_response_ready(con_handle);
  131. printf("Value updated -> complete ATT request - status %02x\n", status);
  132. // simulated value becoming stale again
  133. att_timer.process = &att_invalidate_value;
  134. btstack_run_loop_set_timer(&att_timer, ATT_VALUE_DELAY_MS);
  135. btstack_run_loop_add_timer(&att_timer);
  136. }
  137. #endif
  138. /* LISTING_END */
  139. /*
  140. * @section ATT Read
  141. *
  142. * @text The ATT Server handles all reads to constant data. For dynamic data like the custom characteristic, the registered
  143. * att_read_callback is called. To handle long characteristics and long reads, the att_read_callback is first called
  144. * with buffer == NULL, to request the total value length. Then it will be called again requesting a chunk of the value.
  145. * See Listing attRead.
  146. */
  147. /* LISTING_START(attRead): ATT Read */
  148. // ATT Client Read Callback for Dynamic Data
  149. // - if buffer == NULL, don't copy data, just return size of value
  150. // - if buffer != NULL, copy data and return number bytes copied
  151. // @param offset defines start of attribute value
  152. static uint16_t att_read_callback(hci_con_handle_t connection_handle, uint16_t att_handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){
  153. #ifdef ENABLE_ATT_DELAYED_RESPONSE
  154. switch (att_handle){
  155. case ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE:
  156. if (value_ready){
  157. return att_read_callback_handle_blob((const uint8_t *)test_string, strlen(test_string), offset, buffer, buffer_size);
  158. } else {
  159. printf("Read callback for handle %02x, but value not ready -> report response pending\n", att_handle);
  160. con_handle = connection_handle;
  161. return ATT_READ_RESPONSE_PENDING;
  162. }
  163. break;
  164. case ATT_READ_RESPONSE_PENDING:
  165. // virtual handle indicating all attributes have been queried
  166. printf("Read callback for virtual handle %02x - all attributes have been queried (important for read multiple or read by type) -> start updating values\n", att_handle);
  167. // simulated delayed response for example
  168. att_timer.process = &att_update_value;
  169. btstack_run_loop_set_timer(&att_timer, ATT_VALUE_DELAY_MS);
  170. btstack_run_loop_add_timer(&att_timer);
  171. return 0;
  172. default:
  173. break;
  174. }
  175. #else
  176. UNUSED(connection_handle);
  177. // useless code when ENABLE_ATT_DELAYED_RESPONSE is not defined - but avoids built errors
  178. if (att_handle == ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE){
  179. printf("ENABLE_ATT_DELAYED_RESPONSE not defined in btstack_config.h, responding right away");
  180. return att_read_callback_handle_blob((const uint8_t *)test_string, strlen(test_string), offset, buffer, buffer_size);
  181. }
  182. #endif
  183. return 0;
  184. }
  185. /*
  186. * @section ATT Write
  187. * */
  188. /* LISTING_START(attWrite): ATT Write */
  189. static int att_write_callback(hci_con_handle_t connection_handle, uint16_t att_handle, uint16_t transaction_mode, uint16_t offset, uint8_t *buffer, uint16_t buffer_size){
  190. UNUSED(transaction_mode);
  191. UNUSED(offset);
  192. UNUSED(buffer_size);
  193. UNUSED(connection_handle);
  194. if (att_handle == ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE) {
  195. printf("Write request, value: ");
  196. printf_hexdump(buffer, buffer_size);
  197. #ifdef ENABLE_ATT_DELAYED_RESPONSE
  198. if (value_ready){
  199. printf("Write callback, value ready\n");
  200. return 0;
  201. } else {
  202. printf("Write callback for handle %02x, but not ready -> return response pending\n", att_handle);
  203. }
  204. // simulated delayed response for example
  205. att_timer.process = &att_update_value;
  206. btstack_run_loop_set_timer(&att_timer, ATT_VALUE_DELAY_MS);
  207. btstack_run_loop_add_timer(&att_timer);
  208. return ATT_ERROR_WRITE_RESPONSE_PENDING;
  209. #else
  210. printf("ENABLE_ATT_DELAYED_RESPONSE not defined in btstack_config.h, responding right away");
  211. return 0;
  212. #endif
  213. }
  214. return 0;
  215. }
  216. /* LISTING_END */
  217. int btstack_main(void);
  218. int btstack_main(void)
  219. {
  220. example_setup();
  221. // turn on!
  222. hci_power_control(HCI_POWER_ON);
  223. return 0;
  224. }
  225. /* EXAMPLE_END */