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