spp_flowcontrol.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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_flowcontrol.c"
  38. // *****************************************************************************
  39. /* EXAMPLE_START(spp_flowcontrol): SPP Server - RFCOMM Flow Control
  40. *
  41. * @text This example adds explicit flow control for incoming RFCOMM data to the
  42. * SPP heartbeat counter example. We will highlight the changes compared to the
  43. * SPP counter example.
  44. */
  45. // *****************************************************************************
  46. #include <stdint.h>
  47. #include <stdio.h>
  48. #include <stdlib.h>
  49. #include <string.h>
  50. #include "btstack.h"
  51. #define HEARTBEAT_PERIOD_MS 500
  52. #define TEST_COD 0x1234
  53. #define RFCOMM_SERVER_CHANNEL 1
  54. static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
  55. static uint16_t rfcomm_channel_id;
  56. static uint8_t rfcomm_send_credit = 0;
  57. static uint8_t spp_service_buffer[150];
  58. static btstack_packet_callback_registration_t hci_event_callback_registration;
  59. /* @section SPP Service Setup
  60. *
  61. * @text Listing explicitFlowControl shows how to
  62. * provide one initial credit during RFCOMM service initialization. Please note
  63. * that providing a single credit effectively reduces the credit-based (sliding
  64. * window) flow control to a stop-and-wait flow control that limits the data
  65. * throughput substantially.
  66. */
  67. /* LISTING_START(explicitFlowControl): Providing one initial credit during RFCOMM service initialization */
  68. static void spp_service_setup(void){
  69. // register for HCI events
  70. hci_event_callback_registration.callback = &packet_handler;
  71. hci_add_event_handler(&hci_event_callback_registration);
  72. // init L2CAP
  73. l2cap_init();
  74. // init RFCOMM
  75. rfcomm_init();
  76. // reserved channel, mtu limited by l2cap, 1 credit
  77. rfcomm_register_service_with_initial_credits(&packet_handler, RFCOMM_SERVER_CHANNEL, 0xffff, 1);
  78. // init SDP, create record for SPP and register with SDP
  79. sdp_init();
  80. memset(spp_service_buffer, 0, sizeof(spp_service_buffer));
  81. spp_create_sdp_record(spp_service_buffer, 0x10001, 1, "SPP Counter");
  82. sdp_register_service(spp_service_buffer);
  83. printf("SDP service buffer size: %u\n\r", (uint16_t) de_get_len(spp_service_buffer));
  84. }
  85. /* LISTING_END */
  86. /* @section Periodic Timer Setup
  87. *
  88. * @text Explicit credit management is
  89. * recommended when received RFCOMM data cannot be processed immediately. In this
  90. * example, delayed processing of received data is simulated with the help of a
  91. * periodic timer as follows. When the packet handler receives a data packet, it
  92. * does not provide a new credit, it sets a flag instead, see Listing phManual.
  93. * If the flag is set, a new
  94. * credit will be granted by the heartbeat handler, introducing a delay of up to 1
  95. * second. The heartbeat handler code is shown in Listing hbhManual.
  96. */
  97. static btstack_timer_source_t heartbeat;
  98. /* LISTING_START(hbhManual): Heartbeat handler with manual credit management */
  99. static void heartbeat_handler(struct btstack_timer_source *ts){
  100. if (rfcomm_send_credit){
  101. rfcomm_grant_credits(rfcomm_channel_id, 1);
  102. rfcomm_send_credit = 0;
  103. }
  104. btstack_run_loop_set_timer(ts, HEARTBEAT_PERIOD_MS);
  105. btstack_run_loop_add_timer(ts);
  106. }
  107. /* LISTING_END */
  108. static void one_shot_timer_setup(void){
  109. heartbeat.process = &heartbeat_handler;
  110. btstack_run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS);
  111. btstack_run_loop_add_timer(&heartbeat);
  112. }
  113. /* LISTING_START(phManual): Packet handler with manual credit management */
  114. // Bluetooth logic
  115. static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
  116. /* LISTING_PAUSE */
  117. UNUSED(channel);
  118. bd_addr_t event_addr;
  119. uint8_t rfcomm_channel_nr;
  120. uint16_t mtu;
  121. int i;
  122. switch (packet_type) {
  123. case HCI_EVENT_PACKET:
  124. switch (hci_event_packet_get_type(packet)) {
  125. case HCI_EVENT_PIN_CODE_REQUEST:
  126. // inform about pin code request
  127. printf("Pin code request - using '0000'\n");
  128. hci_event_pin_code_request_get_bd_addr(packet, event_addr);
  129. gap_pin_code_response(event_addr, "0000");
  130. break;
  131. case RFCOMM_EVENT_INCOMING_CONNECTION:
  132. rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr);
  133. rfcomm_channel_nr = rfcomm_event_incoming_connection_get_server_channel(packet);
  134. rfcomm_channel_id = rfcomm_event_incoming_connection_get_rfcomm_cid(packet);
  135. printf("RFCOMM channel %u requested for %s\n", rfcomm_channel_nr, bd_addr_to_str(event_addr));
  136. rfcomm_accept_connection(rfcomm_channel_id);
  137. break;
  138. case RFCOMM_EVENT_CHANNEL_OPENED:
  139. if (rfcomm_event_channel_opened_get_status(packet)) {
  140. printf("RFCOMM channel open failed, status %u\n", rfcomm_event_channel_opened_get_status(packet));
  141. } else {
  142. rfcomm_channel_id = rfcomm_event_channel_opened_get_rfcomm_cid(packet);
  143. mtu = rfcomm_event_channel_opened_get_max_frame_size(packet);
  144. printf("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u\n", rfcomm_channel_id, mtu);
  145. }
  146. break;
  147. case RFCOMM_EVENT_CHANNEL_CLOSED:
  148. rfcomm_channel_id = 0;
  149. break;
  150. default:
  151. break;
  152. }
  153. break;
  154. /* LISTING_RESUME */
  155. case RFCOMM_DATA_PACKET:
  156. for (i=0;i<size;i++){
  157. putchar(packet[i]);
  158. };
  159. putchar('\n');
  160. rfcomm_send_credit = 1;
  161. break;
  162. /* LISTING_PAUSE */
  163. default:
  164. break;
  165. }
  166. /* LISTING_RESUME */
  167. }
  168. /* LISTING_END */
  169. int btstack_main(int argc, const char * argv[]);
  170. int btstack_main(int argc, const char * argv[]){
  171. (void)argc;
  172. (void)argv;
  173. spp_service_setup();
  174. one_shot_timer_setup();
  175. puts("SPP FlowControl Demo: simulates processing on received data...\n\r");
  176. gap_set_local_name("SPP Flowcontrol 00:00:00:00:00:00");
  177. gap_discoverable_control(1);
  178. // short-cut to find other SPP Streamer
  179. gap_set_class_of_device(TEST_COD);
  180. // turn on!
  181. hci_power_control(HCI_POWER_ON);
  182. return 0;
  183. }
  184. /* EXAMPLE_END */