gatt_counter.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. * contact@bluekitchen-gmbh.com
  35. *
  36. */
  37. #define BTSTACK_FILE__ "gatt_counter.c"
  38. // *****************************************************************************
  39. /* EXAMPLE_START(le_counter): LE Peripheral - Heartbeat Counter over GATT
  40. *
  41. * @text All newer operating systems provide GATT Client functionality.
  42. * The LE Counter examples demonstrates how to specify a minimal GATT Database
  43. * with a custom GATT Service and a custom Characteristic that sends periodic
  44. * notifications.
  45. */
  46. // *****************************************************************************
  47. #include <stdint.h>
  48. #include <stdio.h>
  49. #include <stdlib.h>
  50. #include <string.h>
  51. #include "gatt_counter.h"
  52. #include "btstack.h"
  53. #include "ble/gatt-service/battery_service_server.h"
  54. #define HEARTBEAT_PERIOD_MS 1000
  55. /* @section Main Application Setup
  56. *
  57. * @text Listing MainConfiguration shows main application code.
  58. * It initializes L2CAP, the Security Manager and configures the ATT Server with the pre-compiled
  59. * ATT Database generated from $le_counter.gatt$.
  60. * Additionally, it enables the Battery Service Server with the current battery level.
  61. * Finally, it configures the advertisements
  62. * and the heartbeat handler and boots the Bluetooth stack.
  63. * In this example, the Advertisement contains the Flags attribute and the device name.
  64. * The flag 0x06 indicates: LE General Discoverable Mode and BR/EDR not supported.
  65. */
  66. /* LISTING_START(MainConfiguration): Init L2CAP SM ATT Server and start heartbeat timer */
  67. static int le_notification_enabled;
  68. static btstack_timer_source_t heartbeat;
  69. static btstack_packet_callback_registration_t hci_event_callback_registration;
  70. static hci_con_handle_t con_handle;
  71. static uint8_t battery = 100;
  72. #ifdef ENABLE_GATT_OVER_CLASSIC
  73. static uint8_t gatt_service_buffer[70];
  74. #endif
  75. static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
  76. 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);
  77. static int att_write_callback(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t transaction_mode, uint16_t offset, uint8_t *buffer, uint16_t buffer_size);
  78. static void heartbeat_handler(struct btstack_timer_source *ts);
  79. static void beat(void);
  80. const uint8_t adv_data[] = {
  81. // Flags general discoverable, BR/EDR not supported
  82. 0x02, BLUETOOTH_DATA_TYPE_FLAGS, 0x06,
  83. // Name
  84. 0x0b, BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME, 'L', 'E', ' ', 'C', 'o', 'u', 'n', 't', 'e', 'r',
  85. // Incomplete List of 16-bit Service Class UUIDs -- FF10 - only valid for testing!
  86. 0x03, BLUETOOTH_DATA_TYPE_INCOMPLETE_LIST_OF_16_BIT_SERVICE_CLASS_UUIDS, 0x10, 0xff,
  87. };
  88. const uint8_t adv_data_len = sizeof(adv_data);
  89. static void le_counter_setup(void){
  90. l2cap_init();
  91. // setup le device db
  92. le_device_db_init();
  93. // setup SM: Display only
  94. sm_init();
  95. #ifdef ENABLE_GATT_OVER_CLASSIC
  96. // init SDP, create record for GATT and register with SDP
  97. sdp_init();
  98. memset(gatt_service_buffer, 0, sizeof(gatt_service_buffer));
  99. gatt_create_sdp_record(gatt_service_buffer, 0x10001, ATT_SERVICE_GATT_SERVICE_START_HANDLE, ATT_SERVICE_GATT_SERVICE_END_HANDLE);
  100. sdp_register_service(gatt_service_buffer);
  101. printf("SDP service record size: %u\n", de_get_len(gatt_service_buffer));
  102. // configure Classic GAP
  103. gap_set_local_name("GATT Counter BR/EDR 00:00:00:00:00:00");
  104. gap_ssp_set_io_capability(SSP_IO_CAPABILITY_DISPLAY_YES_NO);
  105. gap_discoverable_control(1);
  106. #endif
  107. // setup ATT server
  108. att_server_init(profile_data, att_read_callback, att_write_callback);
  109. // setup battery service
  110. battery_service_server_init(battery);
  111. // setup advertisements
  112. uint16_t adv_int_min = 0x0030;
  113. uint16_t adv_int_max = 0x0030;
  114. uint8_t adv_type = 0;
  115. bd_addr_t null_addr;
  116. memset(null_addr, 0, 6);
  117. gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00);
  118. gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data);
  119. gap_advertisements_enable(1);
  120. // register for HCI events
  121. hci_event_callback_registration.callback = &packet_handler;
  122. hci_add_event_handler(&hci_event_callback_registration);
  123. // register for ATT event
  124. att_server_register_packet_handler(packet_handler);
  125. // set one-shot timer
  126. heartbeat.process = &heartbeat_handler;
  127. btstack_run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS);
  128. btstack_run_loop_add_timer(&heartbeat);
  129. // beat once
  130. beat();
  131. }
  132. /* LISTING_END */
  133. /*
  134. * @section Heartbeat Handler
  135. *
  136. * @text The heartbeat handler updates the value of the single Characteristic provided in this example,
  137. * and request a ATT_EVENT_CAN_SEND_NOW to send a notification if enabled see Listing heartbeat.
  138. */
  139. /* LISTING_START(heartbeat): Hearbeat Handler */
  140. static int counter = 0;
  141. static char counter_string[30];
  142. static int counter_string_len;
  143. static void beat(void){
  144. counter++;
  145. counter_string_len = sprintf(counter_string, "BTstack counter %04u", counter);
  146. puts(counter_string);
  147. }
  148. static void heartbeat_handler(struct btstack_timer_source *ts){
  149. if (le_notification_enabled) {
  150. beat();
  151. att_server_request_can_send_now_event(con_handle);
  152. }
  153. // simulate battery drain
  154. battery--;
  155. if (battery < 50) {
  156. battery = 100;
  157. }
  158. battery_service_server_set_battery_value(battery);
  159. btstack_run_loop_set_timer(ts, HEARTBEAT_PERIOD_MS);
  160. btstack_run_loop_add_timer(ts);
  161. }
  162. /* LISTING_END */
  163. /*
  164. * @section Packet Handler
  165. *
  166. * @text The packet handler is used to:
  167. * - stop the counter after a disconnect
  168. * - send a notification when the requested ATT_EVENT_CAN_SEND_NOW is received
  169. */
  170. /* LISTING_START(packetHandler): Packet Handler */
  171. static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
  172. UNUSED(channel);
  173. UNUSED(size);
  174. switch (packet_type) {
  175. case HCI_EVENT_PACKET:
  176. switch (hci_event_packet_get_type(packet)) {
  177. case HCI_EVENT_DISCONNECTION_COMPLETE:
  178. le_notification_enabled = 0;
  179. break;
  180. case ATT_EVENT_CAN_SEND_NOW:
  181. att_server_notify(con_handle, ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE, (uint8_t*) counter_string, counter_string_len);
  182. break;
  183. }
  184. break;
  185. }
  186. }
  187. /* LISTING_END */
  188. /*
  189. * @section ATT Read
  190. *
  191. * @text The ATT Server handles all reads to constant data. For dynamic data like the custom characteristic, the registered
  192. * att_read_callback is called. To handle long characteristics and long reads, the att_read_callback is first called
  193. * with buffer == NULL, to request the total value length. Then it will be called again requesting a chunk of the value.
  194. * See Listing attRead.
  195. */
  196. /* LISTING_START(attRead): ATT Read */
  197. // ATT Client Read Callback for Dynamic Data
  198. // - if buffer == NULL, don't copy data, just return size of value
  199. // - if buffer != NULL, copy data and return number bytes copied
  200. // @param offset defines start of attribute value
  201. 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){
  202. UNUSED(connection_handle);
  203. if (att_handle == ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE){
  204. return att_read_callback_handle_blob((const uint8_t *)counter_string, counter_string_len, offset, buffer, buffer_size);
  205. }
  206. return 0;
  207. }
  208. /* LISTING_END */
  209. /*
  210. * @section ATT Write
  211. *
  212. * @text The only valid ATT write in this example is to the Client Characteristic Configuration, which configures notification
  213. * and indication. If the ATT handle matches the client configuration handle, the new configuration value is stored and used
  214. * in the heartbeat handler to decide if a new value should be sent. See Listing attWrite.
  215. */
  216. /* LISTING_START(attWrite): ATT Write */
  217. 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){
  218. UNUSED(transaction_mode);
  219. UNUSED(offset);
  220. UNUSED(buffer_size);
  221. if (att_handle != ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_CLIENT_CONFIGURATION_HANDLE) return 0;
  222. le_notification_enabled = little_endian_read_16(buffer, 0) == GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION;
  223. con_handle = connection_handle;
  224. return 0;
  225. }
  226. /* LISTING_END */
  227. int btstack_main(void);
  228. int btstack_main(void)
  229. {
  230. le_counter_setup();
  231. // turn on!
  232. hci_power_control(HCI_POWER_ON);
  233. return 0;
  234. }
  235. /* EXAMPLE_END */