xref: /btstack/example/spp_streamer.c (revision a4fe6467953bdb173fdf96a604f6527ed88f81c3)
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  */
37ab2c6ae4SMatthias Ringwald 
3815de8206SMilanka Ringwald #define __BTSTACK_FILE__ "spp_streamer.c"
3915de8206SMilanka Ringwald 
4015de8206SMilanka Ringwald /*
4115de8206SMilanka Ringwald  * spp_streamer.c
4215de8206SMilanka Ringwald  */
4315de8206SMilanka Ringwald 
44bcf00d8fSMatthias Ringwald // *****************************************************************************
4558d7a529SMilanka Ringwald /* EXAMPLE_START(spp_streamer): Send test data via SPP as fast as possible.
4615de8206SMilanka Ringwald  *
47402122d4SMatthias Ringwald  * @text After RFCOMM connections gets open, request a
48402122d4SMatthias Ringwald  * RFCOMM_EVENT_CAN_SEND_NOW via rfcomm_request_can_send_now_event().
4958d7a529SMilanka Ringwald  * @text When we get the RFCOMM_EVENT_CAN_SEND_NOW, send data and request another one.
5058d7a529SMilanka Ringwald  *
5158d7a529SMilanka Ringwald  * @text Note: To test, run the example, pair from a remote
5258d7a529SMilanka Ringwald  * device, and open the Virtual Serial Port.
53402122d4SMatthias Ringwald  */
54bcf00d8fSMatthias Ringwald // *****************************************************************************
55bcf00d8fSMatthias Ringwald 
56bcf00d8fSMatthias Ringwald #include <stdint.h>
57bcf00d8fSMatthias Ringwald #include <stdio.h>
58bcf00d8fSMatthias Ringwald #include <stdlib.h>
59bcf00d8fSMatthias Ringwald #include <string.h>
60402122d4SMatthias Ringwald #include <inttypes.h>
61bcf00d8fSMatthias Ringwald 
62235946f1SMatthias Ringwald #include "btstack.h"
63bcf00d8fSMatthias Ringwald 
64402122d4SMatthias Ringwald int btstack_main(int argc, const char * argv[]);
65bcf00d8fSMatthias Ringwald 
66402122d4SMatthias Ringwald #define RFCOMM_SERVER_CHANNEL 1
67402122d4SMatthias Ringwald 
68402122d4SMatthias Ringwald #define TEST_COD 0x1234
69bcf00d8fSMatthias Ringwald #define NUM_ROWS 25
70bcf00d8fSMatthias Ringwald #define NUM_COLS 40
71369c1a52SMatthias Ringwald #define DATA_VOLUME (10 * 1000 * 1000)
72bcf00d8fSMatthias Ringwald 
73402122d4SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
74bcf00d8fSMatthias Ringwald 
75bcf00d8fSMatthias Ringwald static uint8_t  test_data[NUM_ROWS * NUM_COLS];
76402122d4SMatthias Ringwald 
77402122d4SMatthias Ringwald // SPP
78402122d4SMatthias Ringwald static uint8_t   spp_service_buffer[150];
79402122d4SMatthias Ringwald 
80402122d4SMatthias Ringwald static uint16_t  spp_test_data_len;
81402122d4SMatthias Ringwald static uint16_t  rfcomm_mtu;
82bcf00d8fSMatthias Ringwald static uint16_t  rfcomm_cid = 0;
83402122d4SMatthias Ringwald // static uint32_t  data_to_send =  DATA_VOLUME;
84bcf00d8fSMatthias Ringwald 
85369c1a52SMatthias Ringwald /*
86369c1a52SMatthias Ringwald  * @section Track throughput
87369c1a52SMatthias Ringwald  * @text We calculate the throughput by setting a start time and measuring the amount of
88369c1a52SMatthias Ringwald  * data sent. After a configurable REPORT_INTERVAL_MS, we print the throughput in kB/s
89369c1a52SMatthias Ringwald  * and reset the counter and start time.
90369c1a52SMatthias Ringwald  */
91369c1a52SMatthias Ringwald 
92369c1a52SMatthias Ringwald /* LISTING_START(tracking): Tracking throughput */
93369c1a52SMatthias Ringwald #define REPORT_INTERVAL_MS 3000
94402122d4SMatthias Ringwald static uint32_t test_data_transferred;
95369c1a52SMatthias Ringwald static uint32_t test_data_start;
96369c1a52SMatthias Ringwald 
97369c1a52SMatthias Ringwald static void test_reset(void){
98369c1a52SMatthias Ringwald     test_data_start = btstack_run_loop_get_time_ms();
99402122d4SMatthias Ringwald     test_data_transferred = 0;
100369c1a52SMatthias Ringwald }
101369c1a52SMatthias Ringwald 
102402122d4SMatthias Ringwald static void test_track_transferred(int bytes_sent){
103402122d4SMatthias Ringwald     test_data_transferred += bytes_sent;
104369c1a52SMatthias Ringwald     // evaluate
105369c1a52SMatthias Ringwald     uint32_t now = btstack_run_loop_get_time_ms();
106369c1a52SMatthias Ringwald     uint32_t time_passed = now - test_data_start;
107369c1a52SMatthias Ringwald     if (time_passed < REPORT_INTERVAL_MS) return;
108369c1a52SMatthias Ringwald     // print speed
109402122d4SMatthias Ringwald     int bytes_per_second = test_data_transferred * 1000 / time_passed;
110402122d4SMatthias Ringwald     printf("%u bytes -> %u.%03u kB/s\n", (int) test_data_transferred, (int) bytes_per_second / 1000, bytes_per_second % 1000);
111369c1a52SMatthias Ringwald 
112369c1a52SMatthias Ringwald     // restart
113369c1a52SMatthias Ringwald     test_data_start = now;
114402122d4SMatthias Ringwald     test_data_transferred  = 0;
115369c1a52SMatthias Ringwald }
116369c1a52SMatthias Ringwald /* LISTING_END(tracking): Tracking throughput */
117369c1a52SMatthias Ringwald 
118369c1a52SMatthias Ringwald 
119402122d4SMatthias Ringwald static void spp_create_test_data(void){
120bcf00d8fSMatthias Ringwald     int x,y;
121bcf00d8fSMatthias Ringwald     for (y=0;y<NUM_ROWS;y++){
122bcf00d8fSMatthias Ringwald         for (x=0;x<NUM_COLS-2;x++){
123bcf00d8fSMatthias Ringwald             test_data[y*NUM_COLS+x] = '0' + (x % 10);
124bcf00d8fSMatthias Ringwald         }
125bcf00d8fSMatthias Ringwald         test_data[y*NUM_COLS+NUM_COLS-2] = '\n';
126bcf00d8fSMatthias Ringwald         test_data[y*NUM_COLS+NUM_COLS-1] = '\r';
127bcf00d8fSMatthias Ringwald     }
128bcf00d8fSMatthias Ringwald }
129bcf00d8fSMatthias Ringwald 
130402122d4SMatthias Ringwald static void spp_send_packet(void){
131402122d4SMatthias Ringwald     rfcomm_send(rfcomm_cid, (uint8_t*) test_data, spp_test_data_len);
1326dcd2e77SMatthias Ringwald 
133402122d4SMatthias Ringwald     test_track_transferred(spp_test_data_len);
134402122d4SMatthias Ringwald #if 0
135402122d4SMatthias Ringwald     if (data_to_send <= spp_test_data_len){
1366dcd2e77SMatthias Ringwald         printf("SPP Streamer: enough data send, closing channel\n");
137bcf00d8fSMatthias Ringwald         rfcomm_disconnect(rfcomm_cid);
138bcf00d8fSMatthias Ringwald         rfcomm_cid = 0;
139bcf00d8fSMatthias Ringwald         return;
140bcf00d8fSMatthias Ringwald     }
141402122d4SMatthias Ringwald     data_to_send -= spp_test_data_len;
142402122d4SMatthias Ringwald #endif
1435f27b1a1SMatthias Ringwald     rfcomm_request_can_send_now_event(rfcomm_cid);
144369c1a52SMatthias Ringwald }
145bcf00d8fSMatthias Ringwald 
146402122d4SMatthias Ringwald /*
147402122d4SMatthias Ringwald  * @section Packet Handler
148402122d4SMatthias Ringwald  *
149402122d4SMatthias Ringwald  * @text The packet handler of the combined example is just the combination of the individual packet handlers.
150402122d4SMatthias Ringwald  */
151402122d4SMatthias Ringwald 
152bcf00d8fSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1539ec2630cSMatthias Ringwald     UNUSED(channel);
1549ec2630cSMatthias Ringwald 
155402122d4SMatthias Ringwald     bd_addr_t event_addr;
156402122d4SMatthias Ringwald     uint8_t   rfcomm_channel_nr;
157402122d4SMatthias Ringwald 
158402122d4SMatthias Ringwald 	switch (packet_type) {
159402122d4SMatthias Ringwald 		case HCI_EVENT_PACKET:
160402122d4SMatthias Ringwald 			switch (hci_event_packet_get_type(packet)) {
161402122d4SMatthias Ringwald 
162402122d4SMatthias Ringwald                 case HCI_EVENT_PIN_CODE_REQUEST:
163402122d4SMatthias Ringwald                     // inform about pin code request
164402122d4SMatthias Ringwald                     printf("Pin code request - using '0000'\n");
165402122d4SMatthias Ringwald                     hci_event_pin_code_request_get_bd_addr(packet, event_addr);
166402122d4SMatthias Ringwald                     gap_pin_code_response(event_addr, "0000");
167bcf00d8fSMatthias Ringwald                     break;
168402122d4SMatthias Ringwald 
169402122d4SMatthias Ringwald                 case HCI_EVENT_USER_CONFIRMATION_REQUEST:
170402122d4SMatthias Ringwald                     // inform about user confirmation request
171402122d4SMatthias Ringwald                     printf("SSP User Confirmation Request with numeric value '%06"PRIu32"'\n", little_endian_read_32(packet, 8));
172402122d4SMatthias Ringwald                     printf("SSP User Confirmation Auto accept\n");
173402122d4SMatthias Ringwald                     break;
174402122d4SMatthias Ringwald 
175402122d4SMatthias Ringwald                 case RFCOMM_EVENT_INCOMING_CONNECTION:
176402122d4SMatthias Ringwald 					// data: event (8), len(8), address(48), channel (8), rfcomm_cid (16)
177402122d4SMatthias Ringwald                     rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr);
178402122d4SMatthias Ringwald                     rfcomm_channel_nr = rfcomm_event_incoming_connection_get_server_channel(packet);
179402122d4SMatthias Ringwald                     rfcomm_cid = rfcomm_event_incoming_connection_get_rfcomm_cid(packet);
180402122d4SMatthias Ringwald                     printf("RFCOMM channel %u requested for %s\n", rfcomm_channel_nr, bd_addr_to_str(event_addr));
181402122d4SMatthias Ringwald                     rfcomm_accept_connection(rfcomm_cid);
182402122d4SMatthias Ringwald 					break;
183402122d4SMatthias Ringwald 
184f8f6a918SMatthias Ringwald 				case RFCOMM_EVENT_CHANNEL_OPENED:
185402122d4SMatthias Ringwald 					// data: event(8), len(8), status (8), address (48), server channel(8), rfcomm_cid(16), max frame size(16)
186402122d4SMatthias Ringwald 					if (rfcomm_event_channel_opened_get_status(packet)) {
187402122d4SMatthias Ringwald                         printf("RFCOMM channel open failed, status %u\n", rfcomm_event_channel_opened_get_status(packet));
188bcf00d8fSMatthias Ringwald                     } else {
189402122d4SMatthias Ringwald                         rfcomm_cid = rfcomm_event_channel_opened_get_rfcomm_cid(packet);
190402122d4SMatthias Ringwald                         rfcomm_mtu = rfcomm_event_channel_opened_get_max_frame_size(packet);
191402122d4SMatthias Ringwald                         printf("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u\n", rfcomm_cid, rfcomm_mtu);
192402122d4SMatthias Ringwald 
193402122d4SMatthias Ringwald                         spp_test_data_len = rfcomm_mtu;
194402122d4SMatthias Ringwald                         if (spp_test_data_len > sizeof(test_data)){
195402122d4SMatthias Ringwald                             spp_test_data_len = sizeof(test_data);
196bcf00d8fSMatthias Ringwald                         }
197402122d4SMatthias Ringwald 
1985aed585bSMatthias Ringwald                         // disable page/inquiry scan to get max performance
1995aed585bSMatthias Ringwald                         gap_discoverable_control(0);
2005aed585bSMatthias Ringwald                         gap_connectable_control(0);
2015aed585bSMatthias Ringwald 
202369c1a52SMatthias Ringwald                         test_reset();
2035f27b1a1SMatthias Ringwald                         rfcomm_request_can_send_now_event(rfcomm_cid);
204bcf00d8fSMatthias Ringwald                     }
205bcf00d8fSMatthias Ringwald 					break;
206402122d4SMatthias Ringwald 
207bcf00d8fSMatthias Ringwald                 case RFCOMM_EVENT_CAN_SEND_NOW:
208402122d4SMatthias Ringwald                     spp_send_packet();
209bcf00d8fSMatthias Ringwald                     break;
210402122d4SMatthias Ringwald 
2111193857eSMatthias Ringwald                 case RFCOMM_EVENT_CHANNEL_CLOSED:
212402122d4SMatthias Ringwald                     printf("RFCOMM channel closed\n");
213402122d4SMatthias Ringwald                     rfcomm_cid = 0;
2145aed585bSMatthias Ringwald 
2155aed585bSMatthias Ringwald                     // re-enable page/inquiry scan again
2165aed585bSMatthias Ringwald                     gap_discoverable_control(1);
2175aed585bSMatthias Ringwald                     gap_connectable_control(1);
218402122d4SMatthias Ringwald                     break;
219402122d4SMatthias Ringwald 
220402122d4SMatthias Ringwald                 default:
221402122d4SMatthias Ringwald                     break;
2221193857eSMatthias Ringwald 			}
2231193857eSMatthias Ringwald             break;
224402122d4SMatthias Ringwald 
225402122d4SMatthias Ringwald         case RFCOMM_DATA_PACKET:
226402122d4SMatthias Ringwald             test_track_transferred(size);
227402122d4SMatthias Ringwald #if 0
228402122d4SMatthias Ringwald             printf("RCV: '");
229402122d4SMatthias Ringwald             for (i=0;i<size;i++){
230402122d4SMatthias Ringwald                 putchar(packet[i]);
231402122d4SMatthias Ringwald             }
232402122d4SMatthias Ringwald             printf("'\n");
233402122d4SMatthias Ringwald #endif
234402122d4SMatthias Ringwald             break;
235402122d4SMatthias Ringwald 
236bcf00d8fSMatthias Ringwald         default:
237bcf00d8fSMatthias Ringwald             break;
238bcf00d8fSMatthias Ringwald 	}
239bcf00d8fSMatthias Ringwald }
240bcf00d8fSMatthias Ringwald 
241402122d4SMatthias Ringwald /*
242402122d4SMatthias Ringwald  * @section Main Application Setup
243402122d4SMatthias Ringwald  *
244402122d4SMatthias Ringwald  * @text As with the packet and the heartbeat handlers, the combined app setup contains the code from the individual example setups.
245402122d4SMatthias Ringwald  */
246bcf00d8fSMatthias Ringwald 
247bcf00d8fSMatthias Ringwald 
248402122d4SMatthias Ringwald /* LISTING_START(MainConfiguration): Init L2CAP RFCOMM SDP SPP */
249402122d4SMatthias Ringwald int btstack_main(int argc, const char * argv[])
250402122d4SMatthias Ringwald {
2519ec2630cSMatthias Ringwald     (void)argc;
2529ec2630cSMatthias Ringwald     (void)argv;
253bcf00d8fSMatthias Ringwald 
254bcf00d8fSMatthias Ringwald     l2cap_init();
255bcf00d8fSMatthias Ringwald 
256402122d4SMatthias Ringwald     rfcomm_init();
257402122d4SMatthias Ringwald     rfcomm_register_service(packet_handler, RFCOMM_SERVER_CHANNEL, 0xffff);
258402122d4SMatthias Ringwald 
259402122d4SMatthias Ringwald     // init SDP, create record for SPP and register with SDP
260402122d4SMatthias Ringwald     sdp_init();
261402122d4SMatthias Ringwald     memset(spp_service_buffer, 0, sizeof(spp_service_buffer));
262402122d4SMatthias Ringwald     spp_create_sdp_record(spp_service_buffer, 0x10001, RFCOMM_SERVER_CHANNEL, "SPP Streamer");
263402122d4SMatthias Ringwald     sdp_register_service(spp_service_buffer);
264402122d4SMatthias Ringwald     // printf("SDP service record size: %u\n", de_get_len(spp_service_buffer));
265402122d4SMatthias Ringwald 
266*a4fe6467SMatthias Ringwald     // register for HCI events
267*a4fe6467SMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
268*a4fe6467SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
269*a4fe6467SMatthias Ringwald 
270402122d4SMatthias Ringwald     // short-cut to find other SPP Streamer
271402122d4SMatthias Ringwald     gap_set_class_of_device(TEST_COD);
272402122d4SMatthias Ringwald 
2730c2b8870SMatthias Ringwald     gap_ssp_set_io_capability(SSP_IO_CAPABILITY_DISPLAY_YES_NO);
2740c2b8870SMatthias Ringwald     gap_set_local_name("SPP Streamer 00:00:00:00:00:00");
275402122d4SMatthias Ringwald     gap_discoverable_control(1);
276402122d4SMatthias Ringwald 
277402122d4SMatthias Ringwald     spp_create_test_data();
278402122d4SMatthias Ringwald 
279bcf00d8fSMatthias Ringwald     // turn on!
280bcf00d8fSMatthias Ringwald 	hci_power_control(HCI_POWER_ON);
281bcf00d8fSMatthias Ringwald 
282bcf00d8fSMatthias Ringwald     return 0;
283bcf00d8fSMatthias Ringwald }
284402122d4SMatthias Ringwald /* LISTING_END */
285402122d4SMatthias Ringwald /* EXAMPLE_END */
286