spp_and_gatt_counter.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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__ "spp_and_gatt_counter.c"
  38. // *****************************************************************************
  39. /* EXAMPLE_START(spp_and_le_counter): Dual mode example
  40. *
  41. * @text The SPP and LE Counter example combines the Bluetooth Classic SPP Counter
  42. * and the Bluetooth LE Counter into a single application.
  43. *
  44. * @text In this Section, we only point out the differences to the individual examples
  45. * and how the stack is configured.
  46. *
  47. * @text Note: To test, please run the example, and then:
  48. * - for SPP pair from a remote device, and open the Virtual Serial Port,
  49. * - for LE use some GATT Explorer, e.g. LightBlue, BLExplr, to enable notifications.
  50. */
  51. // *****************************************************************************
  52. #include <stdint.h>
  53. #include <stdio.h>
  54. #include <stdlib.h>
  55. #include <string.h>
  56. #include <inttypes.h>
  57. #include "btstack.h"
  58. #include "spp_and_gatt_counter.h"
  59. #define RFCOMM_SERVER_CHANNEL 1
  60. #define HEARTBEAT_PERIOD_MS 1000
  61. static uint16_t rfcomm_channel_id;
  62. static uint8_t spp_service_buffer[150];
  63. static int le_notification_enabled;
  64. static hci_con_handle_t att_con_handle;
  65. // THE Couner
  66. static btstack_timer_source_t heartbeat;
  67. static int counter = 0;
  68. static char counter_string[30];
  69. static int counter_string_len;
  70. static btstack_packet_callback_registration_t hci_event_callback_registration;
  71. #ifdef ENABLE_GATT_OVER_CLASSIC
  72. static uint8_t gatt_service_buffer[70];
  73. #endif
  74. /*
  75. * @section Advertisements
  76. *
  77. * @text The Flags attribute in the Advertisement Data indicates if a device is in dual-mode or not.
  78. * Flag 0x06 indicates LE General Discoverable, BR/EDR not supported although we're actually using BR/EDR.
  79. * In the past, there have been problems with Anrdoid devices when the flag was not set.
  80. * Setting it should prevent the remote implementation to try to use GATT over LE/EDR, which is not
  81. * implemented by BTstack. So, setting the flag seems like the safer choice (while it's technically incorrect).
  82. */
  83. /* LISTING_START(advertisements): Advertisement data: Flag 0x06 indicates LE-only device */
  84. const uint8_t adv_data[] = {
  85. // Flags general discoverable, BR/EDR not supported
  86. 0x02, BLUETOOTH_DATA_TYPE_FLAGS, 0x06,
  87. // Name
  88. 0x0b, BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME, 'L', 'E', ' ', 'C', 'o', 'u', 'n', 't', 'e', 'r',
  89. // Incomplete List of 16-bit Service Class UUIDs -- FF10 - only valid for testing!
  90. 0x03, BLUETOOTH_DATA_TYPE_INCOMPLETE_LIST_OF_16_BIT_SERVICE_CLASS_UUIDS, 0x10, 0xff,
  91. };
  92. /* LISTING_END */
  93. uint8_t adv_data_len = sizeof(adv_data);
  94. /*
  95. * @section Packet Handler
  96. *
  97. * @text The packet handler of the combined example is just the combination of the individual packet handlers.
  98. */
  99. static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
  100. UNUSED(channel);
  101. bd_addr_t event_addr;
  102. uint8_t rfcomm_channel_nr;
  103. uint16_t mtu;
  104. int i;
  105. switch (packet_type) {
  106. case HCI_EVENT_PACKET:
  107. switch (hci_event_packet_get_type(packet)) {
  108. case HCI_EVENT_PIN_CODE_REQUEST:
  109. // inform about pin code request
  110. printf("Pin code request - using '0000'\n");
  111. hci_event_pin_code_request_get_bd_addr(packet, event_addr);
  112. gap_pin_code_response(event_addr, "0000");
  113. break;
  114. case HCI_EVENT_USER_CONFIRMATION_REQUEST:
  115. // inform about user confirmation request
  116. printf("SSP User Confirmation Request with numeric value '%06"PRIu32"'\n", little_endian_read_32(packet, 8));
  117. printf("SSP User Confirmation Auto accept\n");
  118. break;
  119. case HCI_EVENT_DISCONNECTION_COMPLETE:
  120. le_notification_enabled = 0;
  121. break;
  122. case ATT_EVENT_CAN_SEND_NOW:
  123. att_server_notify(att_con_handle, ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE, (uint8_t*) counter_string, counter_string_len);
  124. break;
  125. case RFCOMM_EVENT_INCOMING_CONNECTION:
  126. // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16)
  127. rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr);
  128. rfcomm_channel_nr = rfcomm_event_incoming_connection_get_server_channel(packet);
  129. rfcomm_channel_id = rfcomm_event_incoming_connection_get_rfcomm_cid(packet);
  130. printf("RFCOMM channel %u requested for %s\n", rfcomm_channel_nr, bd_addr_to_str(event_addr));
  131. rfcomm_accept_connection(rfcomm_channel_id);
  132. break;
  133. case RFCOMM_EVENT_CHANNEL_OPENED:
  134. // data: event(8), len(8), status (8), address (48), server channel(8), rfcomm_cid(16), max frame size(16)
  135. if (rfcomm_event_channel_opened_get_status(packet)) {
  136. printf("RFCOMM channel open failed, status %u\n", rfcomm_event_channel_opened_get_status(packet));
  137. } else {
  138. rfcomm_channel_id = rfcomm_event_channel_opened_get_rfcomm_cid(packet);
  139. mtu = rfcomm_event_channel_opened_get_max_frame_size(packet);
  140. printf("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u\n", rfcomm_channel_id, mtu);
  141. }
  142. break;
  143. case RFCOMM_EVENT_CAN_SEND_NOW:
  144. rfcomm_send(rfcomm_channel_id, (uint8_t*) counter_string, counter_string_len);
  145. break;
  146. case RFCOMM_EVENT_CHANNEL_CLOSED:
  147. printf("RFCOMM channel closed\n");
  148. rfcomm_channel_id = 0;
  149. break;
  150. default:
  151. break;
  152. }
  153. break;
  154. case RFCOMM_DATA_PACKET:
  155. printf("RCV: '");
  156. for (i=0;i<size;i++){
  157. putchar(packet[i]);
  158. }
  159. printf("'\n");
  160. break;
  161. default:
  162. break;
  163. }
  164. }
  165. // ATT Client Read Callback for Dynamic Data
  166. // - if buffer == NULL, don't copy data, just return size of value
  167. // - if buffer != NULL, copy data and return number bytes copied
  168. // @param offset defines start of attribute value
  169. 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){
  170. UNUSED(con_handle);
  171. if (att_handle == ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE){
  172. return att_read_callback_handle_blob((const uint8_t *)counter_string, counter_string_len, offset, buffer, buffer_size);
  173. }
  174. return 0;
  175. }
  176. // write requests
  177. 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){
  178. // ignore cancel sent for new connections
  179. if (transaction_mode == ATT_TRANSACTION_MODE_CANCEL) return 0;
  180. // find characteristic for handle
  181. switch (att_handle){
  182. case ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_CLIENT_CONFIGURATION_HANDLE:
  183. le_notification_enabled = little_endian_read_16(buffer, 0) == GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION;
  184. att_con_handle = con_handle;
  185. return 0;
  186. case ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE:
  187. printf("Write on test characteristic: ");
  188. printf_hexdump(buffer, buffer_size);
  189. return 0;
  190. default:
  191. printf("WRITE Callback, handle %04x, mode %u, offset %u, data: ", con_handle, transaction_mode, offset);
  192. printf_hexdump(buffer, buffer_size);
  193. return 0;
  194. }
  195. }
  196. static void beat(void){
  197. counter++;
  198. counter_string_len = sprintf(counter_string, "BTstack counter %04u", counter);
  199. puts(counter_string);
  200. }
  201. /*
  202. * @section Heartbeat Handler
  203. *
  204. * @text Similar to the packet handler, the heartbeat handler is the combination of the individual ones.
  205. * After updating the counter, it requests an ATT_EVENT_CAN_SEND_NOW and/or RFCOMM_EVENT_CAN_SEND_NOW
  206. */
  207. /* LISTING_START(heartbeat): Combined Heartbeat handler */
  208. static void heartbeat_handler(struct btstack_timer_source *ts){
  209. if (rfcomm_channel_id || le_notification_enabled) {
  210. beat();
  211. }
  212. if (rfcomm_channel_id){
  213. rfcomm_request_can_send_now_event(rfcomm_channel_id);
  214. }
  215. if (le_notification_enabled) {
  216. att_server_request_can_send_now_event(att_con_handle);
  217. }
  218. btstack_run_loop_set_timer(ts, HEARTBEAT_PERIOD_MS);
  219. btstack_run_loop_add_timer(ts);
  220. }
  221. /* LISTING_END */
  222. /*
  223. * @section Main Application Setup
  224. *
  225. * @text As with the packet and the heartbeat handlers, the combined app setup contains the code from the individual example setups.
  226. */
  227. /* LISTING_START(MainConfiguration): Init L2CAP RFCOMM SDO SM ATT Server and start heartbeat timer */
  228. int btstack_main(void);
  229. int btstack_main(void)
  230. {
  231. l2cap_init();
  232. rfcomm_init();
  233. rfcomm_register_service(packet_handler, RFCOMM_SERVER_CHANNEL, 0xffff);
  234. // init SDP, create record for SPP and register with SDP
  235. sdp_init();
  236. memset(spp_service_buffer, 0, sizeof(spp_service_buffer));
  237. spp_create_sdp_record(spp_service_buffer, 0x10001, RFCOMM_SERVER_CHANNEL, "SPP Counter");
  238. sdp_register_service(spp_service_buffer);
  239. printf("SDP service record size: %u\n", de_get_len(spp_service_buffer));
  240. #ifdef ENABLE_GATT_OVER_CLASSIC
  241. // init SDP, create record for GATT and register with SDP
  242. memset(gatt_service_buffer, 0, sizeof(gatt_service_buffer));
  243. gatt_create_sdp_record(gatt_service_buffer, 0x10001, ATT_SERVICE_GATT_SERVICE_START_HANDLE, ATT_SERVICE_GATT_SERVICE_END_HANDLE);
  244. sdp_register_service(gatt_service_buffer);
  245. printf("SDP service record size: %u\n", de_get_len(gatt_service_buffer));
  246. #endif
  247. gap_set_local_name("SPP and LE Counter 00:00:00:00:00:00");
  248. gap_ssp_set_io_capability(SSP_IO_CAPABILITY_DISPLAY_YES_NO);
  249. gap_discoverable_control(1);
  250. // setup le device db
  251. le_device_db_init();
  252. // setup SM: Display only
  253. sm_init();
  254. // setup ATT server
  255. att_server_init(profile_data, att_read_callback, att_write_callback);
  256. // register for HCI events
  257. hci_event_callback_registration.callback = &packet_handler;
  258. hci_add_event_handler(&hci_event_callback_registration);
  259. // register for ATT events
  260. att_server_register_packet_handler(packet_handler);
  261. // setup advertisements
  262. uint16_t adv_int_min = 0x0030;
  263. uint16_t adv_int_max = 0x0030;
  264. uint8_t adv_type = 0;
  265. bd_addr_t null_addr;
  266. memset(null_addr, 0, 6);
  267. gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00);
  268. gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data);
  269. gap_advertisements_enable(1);
  270. // set one-shot timer
  271. heartbeat.process = &heartbeat_handler;
  272. btstack_run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS);
  273. btstack_run_loop_add_timer(&heartbeat);
  274. // beat once
  275. beat();
  276. // turn on!
  277. hci_power_control(HCI_POWER_ON);
  278. return 0;
  279. }
  280. /* LISTING_END */
  281. /* EXAMPLE_END */