ublox_spp_le_counter.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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__ "ublox_spp_le_counter.c"
  38. // *****************************************************************************
  39. /* EXAMPLE_START(ublox_le_counter): LE Peripheral - Nordic SPP-like profile
  40. *
  41. */
  42. // *****************************************************************************
  43. #include <stdint.h>
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46. #include <string.h>
  47. #include "btstack.h"
  48. #include "ble/gatt-service/device_information_service_server.h"
  49. #include "ble/gatt-service/ublox_spp_service_server.h"
  50. // ublox_spp_le_counter.gatt contains the declaration of the provided GATT Services + Characteristics
  51. // ublox_spp_le_counter.h contains the binary representation of ublox_spp_le_counter.gatt
  52. // it is generated by the build system by calling: $BTSTACK_ROOT/tool/compile_gatt.py ublox_spp_le_counter.gatt ublox_spp_le_counter.h
  53. // it needs to be regenerated when the GATT Database declared in ublox_spp_le_counter.gatt file is modified
  54. #include "ublox_spp_le_counter.h"
  55. #define HEARTBEAT_PERIOD_MS 1000
  56. /* @section Main Application Setup
  57. *
  58. * @text Listing MainConfiguration shows main application code.
  59. * It initializes L2CAP, the Security Manager and configures the ATT Server with the pre-compiled
  60. * ATT Database generated from $ublox_le_counter.gatt$.
  61. * Additionally, it enables the Battery Service Server with the current battery level.
  62. * Finally, it configures the advertisements
  63. * and the heartbeat handler and boots the Bluetooth stack.
  64. * In this example, the Advertisement contains the Flags attribute and the device name.
  65. * The flag 0x06 indicates: LE General Discoverable Mode and BR/EDR not supported.
  66. */
  67. /* LISTING_START(MainConfiguration): Init L2CAP SM ATT Server and start heartbeat timer */
  68. static btstack_timer_source_t heartbeat;
  69. static hci_con_handle_t con_handle = HCI_CON_HANDLE_INVALID;
  70. static btstack_context_callback_registration_t send_request;
  71. static btstack_packet_callback_registration_t hci_event_callback_registration;
  72. const uint8_t adv_data[] = {
  73. // Flags general discoverable, BR/EDR not supported
  74. 2, BLUETOOTH_DATA_TYPE_FLAGS, 0x06,
  75. // Name
  76. 5, BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME, '6', '-','5', '6',
  77. // UUID ...
  78. 17, BLUETOOTH_DATA_TYPE_COMPLETE_LIST_OF_128_BIT_SERVICE_CLASS_UUIDS, 0x1, 0xd7, 0xe9, 0x1, 0x4f, 0xf3, 0x44, 0xe7, 0x83, 0x8f, 0xe2, 0x26, 0xb9, 0xe1, 0x56, 0x24,
  79. };
  80. const uint8_t adv_data_len = sizeof(adv_data);
  81. /* LISTING_END */
  82. /*
  83. * @section Heartbeat Handler
  84. *
  85. * @text The heartbeat handler updates the value of the single Characteristic provided in this example,
  86. * and request a ATT_EVENT_CAN_SEND_NOW to send a notification if enabled see Listing heartbeat.
  87. */
  88. /* LISTING_START(heartbeat): Hearbeat Handler */
  89. static int counter = 0;
  90. static char counter_string[30];
  91. static int counter_string_len;
  92. static void beat(void){
  93. counter++;
  94. counter_string_len = sprintf(counter_string, "BTstack counter %03u", counter);
  95. }
  96. static void ublox_can_send(void * context){
  97. UNUSED(context);
  98. beat();
  99. printf("SEND: %s\n", counter_string);
  100. ublox_spp_service_server_send(con_handle, (uint8_t*) counter_string, counter_string_len);
  101. }
  102. static void heartbeat_handler(struct btstack_timer_source *ts){
  103. if (con_handle != HCI_CON_HANDLE_INVALID) {
  104. send_request.callback = &ublox_can_send;
  105. ublox_spp_service_server_request_can_send_now(&send_request, con_handle);
  106. }
  107. btstack_run_loop_set_timer(ts, HEARTBEAT_PERIOD_MS);
  108. btstack_run_loop_add_timer(ts);
  109. }
  110. /* LISTING_END */
  111. static void ublox_data(hci_con_handle_t handle, const uint8_t * data, uint16_t size){
  112. if (size == 0 && con_handle == HCI_CON_HANDLE_INVALID ){
  113. con_handle = handle;
  114. printf("Connected with handle 0x%04x\n", con_handle);
  115. } else {
  116. printf("RECV: ");
  117. printf_hexdump(data, size);
  118. }
  119. }
  120. static void ublox_flow_control(hci_con_handle_t handle, uint16_t credits){
  121. if (con_handle == HCI_CON_HANDLE_INVALID ){
  122. con_handle = handle;
  123. printf("Connected with handle 0x%04x\n", handle);
  124. }
  125. printf("credits %d\n", credits);
  126. }
  127. /*
  128. * @section Packet Handler
  129. *
  130. * @text The packet handler is used to:
  131. * - stop the counter after a disconnect
  132. */
  133. /* LISTING_START(packetHandler): Packet Handler */
  134. static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
  135. UNUSED(channel);
  136. UNUSED(size);
  137. switch (packet_type) {
  138. case HCI_EVENT_PACKET:
  139. switch (hci_event_packet_get_type(packet)) {
  140. case HCI_EVENT_DISCONNECTION_COMPLETE:
  141. con_handle = HCI_CON_HANDLE_INVALID;
  142. break;
  143. default:
  144. break;
  145. }
  146. break;
  147. }
  148. }
  149. /* LISTING_END */
  150. int btstack_main(void);
  151. int btstack_main(void)
  152. {
  153. // register for HCI events
  154. hci_event_callback_registration.callback = &packet_handler;
  155. hci_add_event_handler(&hci_event_callback_registration);
  156. l2cap_init();
  157. // setup LE device DB
  158. le_device_db_init();
  159. // setup SM: Display only
  160. sm_init();
  161. // setup ATT server
  162. att_server_init(profile_data, NULL, NULL);
  163. // setup device information service
  164. device_information_service_server_init();
  165. // setup Nordic SPP service
  166. ublox_spp_service_server_init(&ublox_data, &ublox_flow_control);
  167. // setup advertisements
  168. uint16_t adv_int_min = 0x0030;
  169. uint16_t adv_int_max = 0x0030;
  170. uint8_t adv_type = 0;
  171. bd_addr_t null_addr;
  172. memset(null_addr, 0, 6);
  173. gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00);
  174. gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data);
  175. gap_advertisements_enable(1);
  176. // set one-shot timer
  177. heartbeat.process = &heartbeat_handler;
  178. btstack_run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS);
  179. btstack_run_loop_add_timer(&heartbeat);
  180. // beat once
  181. beat();
  182. // turn on!
  183. hci_power_control(HCI_POWER_ON);
  184. return 0;
  185. }
  186. /* EXAMPLE_END */