spp_counter.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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_counter.c"
  38. // *****************************************************************************
  39. /* EXAMPLE_START(spp_counter): SPP Server - Heartbeat Counter over RFCOMM
  40. *
  41. * @text The Serial port profile (SPP) is widely used as it provides a serial
  42. * port over Bluetooth. The SPP counter example demonstrates how to setup an SPP
  43. * service, and provide a periodic timer over RFCOMM.
  44. *
  45. * @text Note: To test, please run the spp_counter example, and then pair from
  46. * a remote device, and open the Virtual Serial Port.
  47. */
  48. // *****************************************************************************
  49. #include <inttypes.h>
  50. #include <stdint.h>
  51. #include <stdio.h>
  52. #include <stdlib.h>
  53. #include <string.h>
  54. #include "btstack.h"
  55. #define RFCOMM_SERVER_CHANNEL 1
  56. #define HEARTBEAT_PERIOD_MS 1000
  57. static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
  58. static uint16_t rfcomm_channel_id;
  59. static uint8_t spp_service_buffer[150];
  60. static btstack_packet_callback_registration_t hci_event_callback_registration;
  61. /* @section SPP Service Setup
  62. *s
  63. * @text To provide an SPP service, the L2CAP, RFCOMM, and SDP protocol layers
  64. * are required. After setting up an RFCOMM service with channel nubmer
  65. * RFCOMM_SERVER_CHANNEL, an SDP record is created and registered with the SDP server.
  66. * Example code for SPP service setup is
  67. * provided in Listing SPPSetup. The SDP record created by function
  68. * spp_create_sdp_record consists of a basic SPP definition that uses the provided
  69. * RFCOMM channel ID and service name. For more details, please have a look at it
  70. * in \path{src/sdp_util.c}.
  71. * The SDP record is created on the fly in RAM and is deterministic.
  72. * To preserve valuable RAM, the result could be stored as constant data inside the ROM.
  73. */
  74. /* LISTING_START(SPPSetup): SPP service setup */
  75. static void spp_service_setup(void){
  76. // register for HCI events
  77. hci_event_callback_registration.callback = &packet_handler;
  78. hci_add_event_handler(&hci_event_callback_registration);
  79. l2cap_init();
  80. rfcomm_init();
  81. rfcomm_register_service(packet_handler, RFCOMM_SERVER_CHANNEL, 0xffff); // reserved channel, mtu limited by l2cap
  82. // init SDP, create record for SPP and register with SDP
  83. sdp_init();
  84. memset(spp_service_buffer, 0, sizeof(spp_service_buffer));
  85. spp_create_sdp_record(spp_service_buffer, 0x10001, RFCOMM_SERVER_CHANNEL, "SPP Counter");
  86. sdp_register_service(spp_service_buffer);
  87. printf("SDP service record size: %u\n", de_get_len(spp_service_buffer));
  88. }
  89. /* LISTING_END */
  90. /* @section Periodic Timer Setup
  91. *
  92. * @text The heartbeat handler increases the real counter every second,
  93. * and sends a text string with the counter value, as shown in Listing PeriodicCounter.
  94. */
  95. /* LISTING_START(PeriodicCounter): Periodic Counter */
  96. static btstack_timer_source_t heartbeat;
  97. static char lineBuffer[30];
  98. static void heartbeat_handler(struct btstack_timer_source *ts){
  99. static int counter = 0;
  100. if (rfcomm_channel_id){
  101. sprintf(lineBuffer, "BTstack counter %04u\n", ++counter);
  102. printf("%s", lineBuffer);
  103. rfcomm_request_can_send_now_event(rfcomm_channel_id);
  104. }
  105. btstack_run_loop_set_timer(ts, HEARTBEAT_PERIOD_MS);
  106. btstack_run_loop_add_timer(ts);
  107. }
  108. static void one_shot_timer_setup(void){
  109. // set one-shot timer
  110. heartbeat.process = &heartbeat_handler;
  111. btstack_run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS);
  112. btstack_run_loop_add_timer(&heartbeat);
  113. }
  114. /* LISTING_END */
  115. /* @section Bluetooth Logic
  116. * @text The Bluetooth logic is implemented within the
  117. * packet handler, see Listing SppServerPacketHandler. In this example,
  118. * the following events are passed sequentially:
  119. * - BTSTACK_EVENT_STATE,
  120. * - HCI_EVENT_PIN_CODE_REQUEST (Standard pairing) or
  121. * - HCI_EVENT_USER_CONFIRMATION_REQUEST (Secure Simple Pairing),
  122. * - RFCOMM_EVENT_INCOMING_CONNECTION,
  123. * - RFCOMM_EVENT_CHANNEL_OPENED,
  124. * - RFCOMM_EVETN_CAN_SEND_NOW, and
  125. * - RFCOMM_EVENT_CHANNEL_CLOSED
  126. */
  127. /* @text Upon receiving HCI_EVENT_PIN_CODE_REQUEST event, we need to handle
  128. * authentication. Here, we use a fixed PIN code "0000".
  129. *
  130. * When HCI_EVENT_USER_CONFIRMATION_REQUEST is received, the user will be
  131. * asked to accept the pairing request. If the IO capability is set to
  132. * SSP_IO_CAPABILITY_DISPLAY_YES_NO, the request will be automatically accepted.
  133. *
  134. * The RFCOMM_EVENT_INCOMING_CONNECTION event indicates an incoming connection.
  135. * Here, the connection is accepted. More logic is need, if you want to handle connections
  136. * from multiple clients. The incoming RFCOMM connection event contains the RFCOMM
  137. * channel number used during the SPP setup phase and the newly assigned RFCOMM
  138. * channel ID that is used by all BTstack commands and events.
  139. *
  140. * If RFCOMM_EVENT_CHANNEL_OPENED event returns status greater then 0,
  141. * then the channel establishment has failed (rare case, e.g., client crashes).
  142. * On successful connection, the RFCOMM channel ID and MTU for this
  143. * channel are made available to the heartbeat counter. After opening the RFCOMM channel,
  144. * the communication between client and the application
  145. * takes place. In this example, the timer handler increases the real counter every
  146. * second.
  147. *
  148. * RFCOMM_EVENT_CAN_SEND_NOW indicates that it's possible to send an RFCOMM packet
  149. * on the rfcomm_cid that is include
  150. */
  151. /* LISTING_START(SppServerPacketHandler): SPP Server - Heartbeat Counter over RFCOMM */
  152. static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
  153. UNUSED(channel);
  154. /* LISTING_PAUSE */
  155. bd_addr_t event_addr;
  156. uint8_t rfcomm_channel_nr;
  157. uint16_t mtu;
  158. int i;
  159. switch (packet_type) {
  160. case HCI_EVENT_PACKET:
  161. switch (hci_event_packet_get_type(packet)) {
  162. /* LISTING_RESUME */
  163. case HCI_EVENT_PIN_CODE_REQUEST:
  164. // inform about pin code request
  165. printf("Pin code request - using '0000'\n");
  166. hci_event_pin_code_request_get_bd_addr(packet, event_addr);
  167. gap_pin_code_response(event_addr, "0000");
  168. break;
  169. case HCI_EVENT_USER_CONFIRMATION_REQUEST:
  170. // ssp: inform about user confirmation request
  171. printf("SSP User Confirmation Request with numeric value '%06"PRIu32"'\n", little_endian_read_32(packet, 8));
  172. printf("SSP User Confirmation Auto accept\n");
  173. break;
  174. case RFCOMM_EVENT_INCOMING_CONNECTION:
  175. // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16)
  176. rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr);
  177. rfcomm_channel_nr = rfcomm_event_incoming_connection_get_server_channel(packet);
  178. rfcomm_channel_id = rfcomm_event_incoming_connection_get_rfcomm_cid(packet);
  179. printf("RFCOMM channel %u requested for %s\n", rfcomm_channel_nr, bd_addr_to_str(event_addr));
  180. rfcomm_accept_connection(rfcomm_channel_id);
  181. break;
  182. case RFCOMM_EVENT_CHANNEL_OPENED:
  183. // data: event(8), len(8), status (8), address (48), server channel(8), rfcomm_cid(16), max frame size(16)
  184. if (rfcomm_event_channel_opened_get_status(packet)) {
  185. printf("RFCOMM channel open failed, status %u\n", rfcomm_event_channel_opened_get_status(packet));
  186. } else {
  187. rfcomm_channel_id = rfcomm_event_channel_opened_get_rfcomm_cid(packet);
  188. mtu = rfcomm_event_channel_opened_get_max_frame_size(packet);
  189. printf("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u\n", rfcomm_channel_id, mtu);
  190. }
  191. break;
  192. case RFCOMM_EVENT_CAN_SEND_NOW:
  193. rfcomm_send(rfcomm_channel_id, (uint8_t*) lineBuffer, strlen(lineBuffer));
  194. break;
  195. /* LISTING_PAUSE */
  196. case RFCOMM_EVENT_CHANNEL_CLOSED:
  197. printf("RFCOMM channel closed\n");
  198. rfcomm_channel_id = 0;
  199. break;
  200. default:
  201. break;
  202. }
  203. break;
  204. case RFCOMM_DATA_PACKET:
  205. printf("RCV: '");
  206. for (i=0;i<size;i++){
  207. putchar(packet[i]);
  208. }
  209. printf("'\n");
  210. break;
  211. default:
  212. break;
  213. }
  214. /* LISTING_RESUME */
  215. }
  216. /* LISTING_END */
  217. int btstack_main(int argc, const char * argv[]);
  218. int btstack_main(int argc, const char * argv[]){
  219. (void)argc;
  220. (void)argv;
  221. one_shot_timer_setup();
  222. spp_service_setup();
  223. gap_discoverable_control(1);
  224. gap_ssp_set_io_capability(SSP_IO_CAPABILITY_DISPLAY_YES_NO);
  225. gap_set_local_name("SPP Counter 00:00:00:00:00:00");
  226. // turn on!
  227. hci_power_control(HCI_POWER_ON);
  228. return 0;
  229. }
  230. /* EXAMPLE_END */