spp_flowcontrol.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 - 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_COMMAND_COMPLETE:
  126. if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_bd_addr)){
  127. reverse_bd_addr(&packet[6], event_addr);
  128. printf("BD-ADDR: %s\n\r", bd_addr_to_str(event_addr));
  129. break;
  130. }
  131. break;
  132. case HCI_EVENT_PIN_CODE_REQUEST:
  133. // inform about pin code request
  134. printf("Pin code request - using '0000'\n");
  135. hci_event_pin_code_request_get_bd_addr(packet, event_addr);
  136. gap_pin_code_response(event_addr, "0000");
  137. break;
  138. case RFCOMM_EVENT_INCOMING_CONNECTION:
  139. // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16)
  140. rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr);
  141. rfcomm_channel_nr = rfcomm_event_incoming_connection_get_server_channel(packet);
  142. rfcomm_channel_id = rfcomm_event_incoming_connection_get_rfcomm_cid(packet);
  143. printf("RFCOMM channel %u requested for %s\n", rfcomm_channel_nr, bd_addr_to_str(event_addr));
  144. rfcomm_accept_connection(rfcomm_channel_id);
  145. break;
  146. case RFCOMM_EVENT_CHANNEL_OPENED:
  147. // data: event(8), len(8), status (8), address (48), server channel(8), rfcomm_cid(16), max frame size(16)
  148. if (rfcomm_event_channel_opened_get_status(packet)) {
  149. printf("RFCOMM channel open failed, status %u\n", rfcomm_event_channel_opened_get_status(packet));
  150. } else {
  151. rfcomm_channel_id = rfcomm_event_channel_opened_get_rfcomm_cid(packet);
  152. mtu = rfcomm_event_channel_opened_get_max_frame_size(packet);
  153. printf("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u\n", rfcomm_channel_id, mtu);
  154. }
  155. break;
  156. case RFCOMM_EVENT_CHANNEL_CLOSED:
  157. rfcomm_channel_id = 0;
  158. break;
  159. default:
  160. break;
  161. }
  162. break;
  163. /* LISTING_RESUME */
  164. case RFCOMM_DATA_PACKET:
  165. for (i=0;i<size;i++){
  166. putchar(packet[i]);
  167. };
  168. putchar('\n');
  169. rfcomm_send_credit = 1;
  170. break;
  171. /* LISTING_PAUSE */
  172. default:
  173. break;
  174. }
  175. /* LISTING_RESUME */
  176. }
  177. /* LISTING_END */
  178. int btstack_main(int argc, const char * argv[]);
  179. int btstack_main(int argc, const char * argv[]){
  180. (void)argc;
  181. (void)argv;
  182. spp_service_setup();
  183. one_shot_timer_setup();
  184. puts("SPP FlowControl Demo: simulates processing on received data...\n\r");
  185. gap_set_local_name("SPP Flowcontrol 00:00:00:00:00:00");
  186. gap_discoverable_control(1);
  187. // short-cut to find other SPP Streamer
  188. gap_set_class_of_device(TEST_COD);
  189. // turn on!
  190. hci_power_control(HCI_POWER_ON);
  191. return 0;
  192. }
  193. /* EXAMPLE_END */