xref: /btstack/example/spp_flowcontrol.c (revision 726a19abef3d48084105c802d0e1ba157d2aee8f)
1bcf00d8fSMatthias Ringwald /*
2bcf00d8fSMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
3bcf00d8fSMatthias Ringwald  *
4bcf00d8fSMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5bcf00d8fSMatthias Ringwald  * modification, are permitted provided that the following conditions
6bcf00d8fSMatthias Ringwald  * are met:
7bcf00d8fSMatthias Ringwald  *
8bcf00d8fSMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9bcf00d8fSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10bcf00d8fSMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11bcf00d8fSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12bcf00d8fSMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13bcf00d8fSMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14bcf00d8fSMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15bcf00d8fSMatthias Ringwald  *    from this software without specific prior written permission.
16bcf00d8fSMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17bcf00d8fSMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18bcf00d8fSMatthias Ringwald  *    monetary gain.
19bcf00d8fSMatthias Ringwald  *
20bcf00d8fSMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21bcf00d8fSMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22bcf00d8fSMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23bcf00d8fSMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24bcf00d8fSMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25bcf00d8fSMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26bcf00d8fSMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27bcf00d8fSMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28bcf00d8fSMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29bcf00d8fSMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30bcf00d8fSMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31bcf00d8fSMatthias Ringwald  * SUCH DAMAGE.
32bcf00d8fSMatthias Ringwald  *
33bcf00d8fSMatthias Ringwald  * Please inquire about commercial licensing options at
34bcf00d8fSMatthias Ringwald  * [email protected]
35bcf00d8fSMatthias Ringwald  *
36bcf00d8fSMatthias Ringwald  */
37bcf00d8fSMatthias Ringwald 
38bcf00d8fSMatthias Ringwald // *****************************************************************************
39bcf00d8fSMatthias Ringwald /* EXAMPLE_START(spp_flowcontrol): SPP Server - Flow Control
40bcf00d8fSMatthias Ringwald  *
41bcf00d8fSMatthias Ringwald  * @text This example adds explicit flow control for incoming RFCOMM data to the
42bcf00d8fSMatthias Ringwald  * SPP heartbeat counter example. We will highlight the changes compared to the
43bcf00d8fSMatthias Ringwald  * SPP counter example.
44bcf00d8fSMatthias Ringwald  */
45bcf00d8fSMatthias Ringwald  // *****************************************************************************
46bcf00d8fSMatthias Ringwald 
47bcf00d8fSMatthias Ringwald #include <stdint.h>
48bcf00d8fSMatthias Ringwald #include <stdio.h>
49bcf00d8fSMatthias Ringwald #include <stdlib.h>
50bcf00d8fSMatthias Ringwald #include <string.h>
51bcf00d8fSMatthias Ringwald 
52*726a19abSMatthias Ringwald #include "btstack.h"
53bcf00d8fSMatthias Ringwald 
54bcf00d8fSMatthias Ringwald #define HEARTBEAT_PERIOD_MS 500
55bcf00d8fSMatthias Ringwald 
56bcf00d8fSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
57bcf00d8fSMatthias Ringwald 
58bcf00d8fSMatthias Ringwald static uint8_t  rfcomm_channel_nr = 1;
59bcf00d8fSMatthias Ringwald static uint16_t rfcomm_channel_id;
60bcf00d8fSMatthias Ringwald static uint8_t  rfcomm_send_credit = 0;
61bcf00d8fSMatthias Ringwald static uint8_t  spp_service_buffer[150];
62bcf00d8fSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
63bcf00d8fSMatthias Ringwald 
64bcf00d8fSMatthias Ringwald /* @section SPP Service Setup
65bcf00d8fSMatthias Ringwald  *
66bcf00d8fSMatthias Ringwald  * @text Listing explicitFlowControl shows how to
67bcf00d8fSMatthias Ringwald  * provide one initial credit during RFCOMM service initialization. Please note
68bcf00d8fSMatthias Ringwald  * that providing a single credit effectively reduces the credit-based (sliding
69bcf00d8fSMatthias Ringwald  * window) flow control to a stop-and-wait flow control that limits the data
70bcf00d8fSMatthias Ringwald  * throughput substantially.
71bcf00d8fSMatthias Ringwald  */
72bcf00d8fSMatthias Ringwald 
73bcf00d8fSMatthias Ringwald /* LISTING_START(explicitFlowControl): Providing one initial credit during RFCOMM service initialization */
74bcf00d8fSMatthias Ringwald static void spp_service_setup(void){
75bcf00d8fSMatthias Ringwald 
76bcf00d8fSMatthias Ringwald     // register for HCI events
77bcf00d8fSMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
78bcf00d8fSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
79bcf00d8fSMatthias Ringwald 
80bcf00d8fSMatthias Ringwald     // init L2CAP
81bcf00d8fSMatthias Ringwald     l2cap_init();
82bcf00d8fSMatthias Ringwald 
83bcf00d8fSMatthias Ringwald     // init RFCOMM
84bcf00d8fSMatthias Ringwald     rfcomm_init();
85bcf00d8fSMatthias Ringwald     // reserved channel, mtu limited by l2cap, 1 credit
86*726a19abSMatthias Ringwald     rfcomm_register_service_with_initial_credits(&packet_handler, rfcomm_channel_nr, 0xffff, 1);
87bcf00d8fSMatthias Ringwald 
88bcf00d8fSMatthias Ringwald     // init SDP, create record for SPP and register with SDP
89bcf00d8fSMatthias Ringwald     sdp_init();
90bcf00d8fSMatthias Ringwald     memset(spp_service_buffer, 0, sizeof(spp_service_buffer));
91bcf00d8fSMatthias Ringwald     sdp_create_spp_service(spp_service_buffer, 0x10001, 1, "SPP Counter");
92bcf00d8fSMatthias Ringwald     sdp_register_service(spp_service_buffer);
93bcf00d8fSMatthias Ringwald     printf("SDP service buffer size: %u\n\r", (uint16_t) de_get_len(spp_service_buffer));
94bcf00d8fSMatthias Ringwald }
95bcf00d8fSMatthias Ringwald /* LISTING_END */
96bcf00d8fSMatthias Ringwald 
97bcf00d8fSMatthias Ringwald /* @section Periodic Timer Setup
98bcf00d8fSMatthias Ringwald  *
99bcf00d8fSMatthias Ringwald  * @text Explicit credit management is
100bcf00d8fSMatthias Ringwald  * recommended when received RFCOMM data cannot be processed immediately. In this
101bcf00d8fSMatthias Ringwald  * example, delayed processing of received data is simulated with the help of a
102bcf00d8fSMatthias Ringwald  * periodic timer as follows. When the packet handler receives a data packet, it
103bcf00d8fSMatthias Ringwald  * does not provide a new credit, it sets a flag instead, see Listing phManual.
104bcf00d8fSMatthias Ringwald  * If the flag is set, a new
105bcf00d8fSMatthias Ringwald  * credit will be granted by the heartbeat handler, introducing a delay of up to 1
106bcf00d8fSMatthias Ringwald  * second. The heartbeat handler code is shown in Listing hbhManual.
107bcf00d8fSMatthias Ringwald  */
108bcf00d8fSMatthias Ringwald 
109bcf00d8fSMatthias Ringwald static btstack_timer_source_t heartbeat;
110bcf00d8fSMatthias Ringwald 
111bcf00d8fSMatthias Ringwald /* LISTING_START(hbhManual): Heartbeat handler with manual credit management */
112bcf00d8fSMatthias Ringwald static void  heartbeat_handler(struct btstack_timer_source *ts){
113bcf00d8fSMatthias Ringwald     if (rfcomm_send_credit){
114bcf00d8fSMatthias Ringwald         rfcomm_grant_credits(rfcomm_channel_id, 1);
115bcf00d8fSMatthias Ringwald         rfcomm_send_credit = 0;
116bcf00d8fSMatthias Ringwald     }
117bcf00d8fSMatthias Ringwald     btstack_run_loop_set_timer(ts, HEARTBEAT_PERIOD_MS);
118bcf00d8fSMatthias Ringwald     btstack_run_loop_add_timer(ts);
119bcf00d8fSMatthias Ringwald }
120bcf00d8fSMatthias Ringwald /* LISTING_END */
121bcf00d8fSMatthias Ringwald 
122bcf00d8fSMatthias Ringwald static void one_shot_timer_setup(void){
123bcf00d8fSMatthias Ringwald     heartbeat.process = &heartbeat_handler;
124bcf00d8fSMatthias Ringwald     btstack_run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS);
125bcf00d8fSMatthias Ringwald     btstack_run_loop_add_timer(&heartbeat);
126bcf00d8fSMatthias Ringwald }
127bcf00d8fSMatthias Ringwald 
128bcf00d8fSMatthias Ringwald /* LISTING_START(phManual): Packet handler with manual credit management */
129bcf00d8fSMatthias Ringwald // Bluetooth logic
130bcf00d8fSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
131bcf00d8fSMatthias Ringwald /* LISTING_PAUSE */
132bcf00d8fSMatthias Ringwald     bd_addr_t event_addr;
133bcf00d8fSMatthias Ringwald     uint8_t   rfcomm_channel_nr;
134bcf00d8fSMatthias Ringwald     uint16_t  mtu;
135bcf00d8fSMatthias Ringwald     int i;
136bcf00d8fSMatthias Ringwald 
137bcf00d8fSMatthias Ringwald     switch (packet_type) {
138bcf00d8fSMatthias Ringwald         case HCI_EVENT_PACKET:
1390e2df43fSMatthias Ringwald             switch (hci_event_packet_get_type(packet)) {
140bcf00d8fSMatthias Ringwald 
141bcf00d8fSMatthias Ringwald                 case BTSTACK_EVENT_STATE:
142bcf00d8fSMatthias Ringwald                     if (packet[2] == HCI_STATE_WORKING) {
143bcf00d8fSMatthias Ringwald                         printf("BTstack is up and running\n");
144bcf00d8fSMatthias Ringwald                     }
145bcf00d8fSMatthias Ringwald                     break;
146bcf00d8fSMatthias Ringwald 
147bcf00d8fSMatthias Ringwald                 case HCI_EVENT_COMMAND_COMPLETE:
148bcf00d8fSMatthias Ringwald                     if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_bd_addr)){
149bcf00d8fSMatthias Ringwald                         reverse_bd_addr(&packet[6], event_addr);
150bcf00d8fSMatthias Ringwald                         printf("BD-ADDR: %s\n\r", bd_addr_to_str(event_addr));
151bcf00d8fSMatthias Ringwald                         break;
152bcf00d8fSMatthias Ringwald                     }
153bcf00d8fSMatthias Ringwald                     break;
154bcf00d8fSMatthias Ringwald 
155bcf00d8fSMatthias Ringwald                 case HCI_EVENT_LINK_KEY_REQUEST:
156bcf00d8fSMatthias Ringwald                     // deny link key request
157bcf00d8fSMatthias Ringwald                     printf("Link key request\n\r");
158bcf00d8fSMatthias Ringwald                     reverse_bd_addr(&packet[2], event_addr);
159bcf00d8fSMatthias Ringwald                     hci_send_cmd(&hci_link_key_request_negative_reply, &event_addr);
160bcf00d8fSMatthias Ringwald                     break;
161bcf00d8fSMatthias Ringwald 
162bcf00d8fSMatthias Ringwald                 case HCI_EVENT_PIN_CODE_REQUEST:
163bcf00d8fSMatthias Ringwald                     // inform about pin code request
164bcf00d8fSMatthias Ringwald                     printf("Pin code request - using '0000'\n\r");
165bcf00d8fSMatthias Ringwald                     reverse_bd_addr(&packet[2], event_addr);
166bcf00d8fSMatthias Ringwald                     hci_send_cmd(&hci_pin_code_request_reply, &event_addr, 4, "0000");
167bcf00d8fSMatthias Ringwald                     break;
168bcf00d8fSMatthias Ringwald 
169bcf00d8fSMatthias Ringwald                 case RFCOMM_EVENT_INCOMING_CONNECTION:
170bcf00d8fSMatthias Ringwald                     // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16)
171bcf00d8fSMatthias Ringwald                     reverse_bd_addr(&packet[2], event_addr);
172bcf00d8fSMatthias Ringwald                     rfcomm_channel_nr = packet[8];
173bcf00d8fSMatthias Ringwald                     rfcomm_channel_id = little_endian_read_16(packet, 9);
174bcf00d8fSMatthias Ringwald                     printf("RFCOMM channel %u requested for %s\n\r", rfcomm_channel_nr, bd_addr_to_str(event_addr));
175bcf00d8fSMatthias Ringwald                     rfcomm_accept_connection(rfcomm_channel_id);
176bcf00d8fSMatthias Ringwald                     break;
177bcf00d8fSMatthias Ringwald 
178bcf00d8fSMatthias Ringwald                 case RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE:
179bcf00d8fSMatthias Ringwald                     // data: event(8), len(8), status (8), address (48), server channel(8), rfcomm_cid(16), max frame size(16)
180bcf00d8fSMatthias Ringwald                     if (packet[2]) {
181bcf00d8fSMatthias Ringwald                         printf("RFCOMM channel open failed, status %u\n\r", packet[2]);
182bcf00d8fSMatthias Ringwald                     } else {
183bcf00d8fSMatthias Ringwald                         rfcomm_channel_id = little_endian_read_16(packet, 12);
184bcf00d8fSMatthias Ringwald                         mtu = little_endian_read_16(packet, 14);
185bcf00d8fSMatthias Ringwald                         printf("\n\rRFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u\n\r", rfcomm_channel_id, mtu);
186bcf00d8fSMatthias Ringwald                     }
187bcf00d8fSMatthias Ringwald                     break;
188bcf00d8fSMatthias Ringwald 
189bcf00d8fSMatthias Ringwald                 case RFCOMM_EVENT_CHANNEL_CLOSED:
190bcf00d8fSMatthias Ringwald                     rfcomm_channel_id = 0;
191bcf00d8fSMatthias Ringwald                     break;
192bcf00d8fSMatthias Ringwald 
193bcf00d8fSMatthias Ringwald                 default:
194bcf00d8fSMatthias Ringwald                     break;
195bcf00d8fSMatthias Ringwald             }
196bcf00d8fSMatthias Ringwald             break;
197bcf00d8fSMatthias Ringwald /* LISTING_RESUME */
198bcf00d8fSMatthias Ringwald         case RFCOMM_DATA_PACKET:
199bcf00d8fSMatthias Ringwald             for (i=0;i<size;i++){
200bcf00d8fSMatthias Ringwald                 putchar(packet[i]);
201bcf00d8fSMatthias Ringwald             };
202bcf00d8fSMatthias Ringwald             putchar('\n');
203bcf00d8fSMatthias Ringwald             rfcomm_send_credit = 1;
204bcf00d8fSMatthias Ringwald             break;
205bcf00d8fSMatthias Ringwald /* LISTING_PAUSE */
206bcf00d8fSMatthias Ringwald         default:
207bcf00d8fSMatthias Ringwald             break;
208bcf00d8fSMatthias Ringwald     }
209bcf00d8fSMatthias Ringwald /* LISTING_RESUME */
210bcf00d8fSMatthias Ringwald }
211bcf00d8fSMatthias Ringwald /* LISTING_END */
212bcf00d8fSMatthias Ringwald 
213bcf00d8fSMatthias Ringwald 
214bcf00d8fSMatthias Ringwald 
215bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]);
216bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]){
217bcf00d8fSMatthias Ringwald 
218bcf00d8fSMatthias Ringwald     spp_service_setup();
219bcf00d8fSMatthias Ringwald     one_shot_timer_setup();
220bcf00d8fSMatthias Ringwald 
221bcf00d8fSMatthias Ringwald     puts("SPP FlowControl Demo: simulates processing on received data...\n\r");
222bcf00d8fSMatthias Ringwald     gap_set_local_name("BTstack SPP Flow Control");
223bcf00d8fSMatthias Ringwald     gap_discoverable_control(1);
224bcf00d8fSMatthias Ringwald 
225bcf00d8fSMatthias Ringwald     // turn on!
226bcf00d8fSMatthias Ringwald     hci_power_control(HCI_POWER_ON);
227bcf00d8fSMatthias Ringwald 
228bcf00d8fSMatthias Ringwald     return 0;
229bcf00d8fSMatthias Ringwald }
230bcf00d8fSMatthias Ringwald /* EXAMPLE_END */
231bcf00d8fSMatthias Ringwald 
232bcf00d8fSMatthias Ringwald 
233