xref: /btstack/example/spp_streamer.c (revision 402122d41326704da0b58e48dc27384720da11ac)
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 
38bcf00d8fSMatthias Ringwald // *****************************************************************************
39*402122d4SMatthias Ringwald /* EXAMPLE_START(spp_streamer): Send test data via SPP as fast as possible
40*402122d4SMatthias Ringwald  * @text After RFCOMM connections gets open, request a
41*402122d4SMatthias Ringwald  * RFCOMM_EVENT_CAN_SEND_NOW via rfcomm_request_can_send_now_event().
42*402122d4SMatthias Ringwald  * When we get the RFCOMM_EVENT_CAN_SEND_NOW, send data and request another one.
43*402122d4SMatthias Ringwald  */
44bcf00d8fSMatthias Ringwald // *****************************************************************************
45bcf00d8fSMatthias Ringwald 
46bcf00d8fSMatthias Ringwald #include <stdint.h>
47bcf00d8fSMatthias Ringwald #include <stdio.h>
48bcf00d8fSMatthias Ringwald #include <stdlib.h>
49bcf00d8fSMatthias Ringwald #include <string.h>
50*402122d4SMatthias Ringwald #include <inttypes.h>
51bcf00d8fSMatthias Ringwald 
52235946f1SMatthias Ringwald #include "btstack.h"
53bcf00d8fSMatthias Ringwald 
54*402122d4SMatthias Ringwald int btstack_main(int argc, const char * argv[]);
55bcf00d8fSMatthias Ringwald 
56*402122d4SMatthias Ringwald #define RFCOMM_SERVER_CHANNEL 1
57*402122d4SMatthias Ringwald 
58*402122d4SMatthias Ringwald #define TEST_COD 0x1234
59bcf00d8fSMatthias Ringwald #define NUM_ROWS 25
60bcf00d8fSMatthias Ringwald #define NUM_COLS 40
61369c1a52SMatthias Ringwald #define DATA_VOLUME (10 * 1000 * 1000)
62bcf00d8fSMatthias Ringwald 
63*402122d4SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
64bcf00d8fSMatthias Ringwald 
65bcf00d8fSMatthias Ringwald static uint8_t  test_data[NUM_ROWS * NUM_COLS];
66*402122d4SMatthias Ringwald 
67*402122d4SMatthias Ringwald // SPP
68*402122d4SMatthias Ringwald static uint8_t   spp_service_buffer[150];
69*402122d4SMatthias Ringwald 
70*402122d4SMatthias Ringwald static uint16_t  spp_test_data_len;
71*402122d4SMatthias Ringwald static uint16_t  rfcomm_mtu;
72bcf00d8fSMatthias Ringwald static uint16_t  rfcomm_cid = 0;
73*402122d4SMatthias Ringwald // static uint32_t  data_to_send =  DATA_VOLUME;
74bcf00d8fSMatthias Ringwald 
75369c1a52SMatthias Ringwald /*
76369c1a52SMatthias Ringwald  * @section Track throughput
77369c1a52SMatthias Ringwald  * @text We calculate the throughput by setting a start time and measuring the amount of
78369c1a52SMatthias Ringwald  * data sent. After a configurable REPORT_INTERVAL_MS, we print the throughput in kB/s
79369c1a52SMatthias Ringwald  * and reset the counter and start time.
80369c1a52SMatthias Ringwald  */
81369c1a52SMatthias Ringwald 
82369c1a52SMatthias Ringwald /* LISTING_START(tracking): Tracking throughput */
83369c1a52SMatthias Ringwald #define REPORT_INTERVAL_MS 3000
84*402122d4SMatthias Ringwald static uint32_t test_data_transferred;
85369c1a52SMatthias Ringwald static uint32_t test_data_start;
86369c1a52SMatthias Ringwald 
87369c1a52SMatthias Ringwald static void test_reset(void){
88369c1a52SMatthias Ringwald     test_data_start = btstack_run_loop_get_time_ms();
89*402122d4SMatthias Ringwald     test_data_transferred = 0;
90369c1a52SMatthias Ringwald }
91369c1a52SMatthias Ringwald 
92*402122d4SMatthias Ringwald static void test_track_transferred(int bytes_sent){
93*402122d4SMatthias Ringwald     test_data_transferred += bytes_sent;
94369c1a52SMatthias Ringwald     // evaluate
95369c1a52SMatthias Ringwald     uint32_t now = btstack_run_loop_get_time_ms();
96369c1a52SMatthias Ringwald     uint32_t time_passed = now - test_data_start;
97369c1a52SMatthias Ringwald     if (time_passed < REPORT_INTERVAL_MS) return;
98369c1a52SMatthias Ringwald     // print speed
99*402122d4SMatthias Ringwald     int bytes_per_second = test_data_transferred * 1000 / time_passed;
100*402122d4SMatthias Ringwald     printf("%u bytes -> %u.%03u kB/s\n", (int) test_data_transferred, (int) bytes_per_second / 1000, bytes_per_second % 1000);
101369c1a52SMatthias Ringwald 
102369c1a52SMatthias Ringwald     // restart
103369c1a52SMatthias Ringwald     test_data_start = now;
104*402122d4SMatthias Ringwald     test_data_transferred  = 0;
105369c1a52SMatthias Ringwald }
106369c1a52SMatthias Ringwald /* LISTING_END(tracking): Tracking throughput */
107369c1a52SMatthias Ringwald 
108369c1a52SMatthias Ringwald 
109*402122d4SMatthias Ringwald static void spp_create_test_data(void){
110bcf00d8fSMatthias Ringwald     int x,y;
111bcf00d8fSMatthias Ringwald     for (y=0;y<NUM_ROWS;y++){
112bcf00d8fSMatthias Ringwald         for (x=0;x<NUM_COLS-2;x++){
113bcf00d8fSMatthias Ringwald             test_data[y*NUM_COLS+x] = '0' + (x % 10);
114bcf00d8fSMatthias Ringwald         }
115bcf00d8fSMatthias Ringwald         test_data[y*NUM_COLS+NUM_COLS-2] = '\n';
116bcf00d8fSMatthias Ringwald         test_data[y*NUM_COLS+NUM_COLS-1] = '\r';
117bcf00d8fSMatthias Ringwald     }
118bcf00d8fSMatthias Ringwald }
119bcf00d8fSMatthias Ringwald 
120*402122d4SMatthias Ringwald static void spp_send_packet(void){
121*402122d4SMatthias Ringwald     rfcomm_send(rfcomm_cid, (uint8_t*) test_data, spp_test_data_len);
1226dcd2e77SMatthias Ringwald 
123*402122d4SMatthias Ringwald     test_track_transferred(spp_test_data_len);
124*402122d4SMatthias Ringwald #if 0
125*402122d4SMatthias Ringwald     if (data_to_send <= spp_test_data_len){
1266dcd2e77SMatthias Ringwald         printf("SPP Streamer: enough data send, closing channel\n");
127bcf00d8fSMatthias Ringwald         rfcomm_disconnect(rfcomm_cid);
128bcf00d8fSMatthias Ringwald         rfcomm_cid = 0;
129bcf00d8fSMatthias Ringwald         return;
130bcf00d8fSMatthias Ringwald     }
131*402122d4SMatthias Ringwald     data_to_send -= spp_test_data_len;
132*402122d4SMatthias Ringwald #endif
1335f27b1a1SMatthias Ringwald     rfcomm_request_can_send_now_event(rfcomm_cid);
134369c1a52SMatthias Ringwald }
135bcf00d8fSMatthias Ringwald 
136*402122d4SMatthias Ringwald /*
137*402122d4SMatthias Ringwald  * @section Packet Handler
138*402122d4SMatthias Ringwald  *
139*402122d4SMatthias Ringwald  * @text The packet handler of the combined example is just the combination of the individual packet handlers.
140*402122d4SMatthias Ringwald  */
141*402122d4SMatthias Ringwald 
142bcf00d8fSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1439ec2630cSMatthias Ringwald     UNUSED(channel);
1449ec2630cSMatthias Ringwald 
145*402122d4SMatthias Ringwald     bd_addr_t event_addr;
146*402122d4SMatthias Ringwald     uint8_t   rfcomm_channel_nr;
147*402122d4SMatthias Ringwald 
148*402122d4SMatthias Ringwald 	switch (packet_type) {
149*402122d4SMatthias Ringwald 		case HCI_EVENT_PACKET:
150*402122d4SMatthias Ringwald 			switch (hci_event_packet_get_type(packet)) {
151*402122d4SMatthias Ringwald 
152*402122d4SMatthias Ringwald                 case HCI_EVENT_PIN_CODE_REQUEST:
153*402122d4SMatthias Ringwald                     // inform about pin code request
154*402122d4SMatthias Ringwald                     printf("Pin code request - using '0000'\n");
155*402122d4SMatthias Ringwald                     hci_event_pin_code_request_get_bd_addr(packet, event_addr);
156*402122d4SMatthias Ringwald                     gap_pin_code_response(event_addr, "0000");
157bcf00d8fSMatthias Ringwald                     break;
158*402122d4SMatthias Ringwald 
159*402122d4SMatthias Ringwald                 case HCI_EVENT_USER_CONFIRMATION_REQUEST:
160*402122d4SMatthias Ringwald                     // inform about user confirmation request
161*402122d4SMatthias Ringwald                     printf("SSP User Confirmation Request with numeric value '%06"PRIu32"'\n", little_endian_read_32(packet, 8));
162*402122d4SMatthias Ringwald                     printf("SSP User Confirmation Auto accept\n");
163*402122d4SMatthias Ringwald                     break;
164*402122d4SMatthias Ringwald 
165*402122d4SMatthias Ringwald                 case RFCOMM_EVENT_INCOMING_CONNECTION:
166*402122d4SMatthias Ringwald 					// data: event (8), len(8), address(48), channel (8), rfcomm_cid (16)
167*402122d4SMatthias Ringwald                     rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr);
168*402122d4SMatthias Ringwald                     rfcomm_channel_nr = rfcomm_event_incoming_connection_get_server_channel(packet);
169*402122d4SMatthias Ringwald                     rfcomm_cid = rfcomm_event_incoming_connection_get_rfcomm_cid(packet);
170*402122d4SMatthias Ringwald                     printf("RFCOMM channel %u requested for %s\n", rfcomm_channel_nr, bd_addr_to_str(event_addr));
171*402122d4SMatthias Ringwald                     rfcomm_accept_connection(rfcomm_cid);
172*402122d4SMatthias Ringwald 					break;
173*402122d4SMatthias Ringwald 
174f8f6a918SMatthias Ringwald 				case RFCOMM_EVENT_CHANNEL_OPENED:
175*402122d4SMatthias Ringwald 					// data: event(8), len(8), status (8), address (48), server channel(8), rfcomm_cid(16), max frame size(16)
176*402122d4SMatthias Ringwald 					if (rfcomm_event_channel_opened_get_status(packet)) {
177*402122d4SMatthias Ringwald                         printf("RFCOMM channel open failed, status %u\n", rfcomm_event_channel_opened_get_status(packet));
178bcf00d8fSMatthias Ringwald                     } else {
179*402122d4SMatthias Ringwald                         rfcomm_cid = rfcomm_event_channel_opened_get_rfcomm_cid(packet);
180*402122d4SMatthias Ringwald                         rfcomm_mtu = rfcomm_event_channel_opened_get_max_frame_size(packet);
181*402122d4SMatthias Ringwald                         printf("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u\n", rfcomm_cid, rfcomm_mtu);
182*402122d4SMatthias Ringwald 
183*402122d4SMatthias Ringwald                         spp_test_data_len = rfcomm_mtu;
184*402122d4SMatthias Ringwald                         if (spp_test_data_len > sizeof(test_data)){
185*402122d4SMatthias Ringwald                             spp_test_data_len = sizeof(test_data);
186bcf00d8fSMatthias Ringwald                         }
187*402122d4SMatthias Ringwald 
188369c1a52SMatthias Ringwald                         test_reset();
1895f27b1a1SMatthias Ringwald                         rfcomm_request_can_send_now_event(rfcomm_cid);
190bcf00d8fSMatthias Ringwald                     }
191bcf00d8fSMatthias Ringwald 					break;
192*402122d4SMatthias Ringwald 
193bcf00d8fSMatthias Ringwald                 case RFCOMM_EVENT_CAN_SEND_NOW:
194*402122d4SMatthias Ringwald                     spp_send_packet();
195bcf00d8fSMatthias Ringwald                     break;
196*402122d4SMatthias Ringwald 
1971193857eSMatthias Ringwald                 case RFCOMM_EVENT_CHANNEL_CLOSED:
198*402122d4SMatthias Ringwald                     printf("RFCOMM channel closed\n");
199*402122d4SMatthias Ringwald                     rfcomm_cid = 0;
200*402122d4SMatthias Ringwald                     break;
201*402122d4SMatthias Ringwald 
202*402122d4SMatthias Ringwald                 default:
203*402122d4SMatthias Ringwald                     break;
2041193857eSMatthias Ringwald 			}
2051193857eSMatthias Ringwald             break;
206*402122d4SMatthias Ringwald 
207*402122d4SMatthias Ringwald         case RFCOMM_DATA_PACKET:
208*402122d4SMatthias Ringwald             test_track_transferred(size);
209*402122d4SMatthias Ringwald #if 0
210*402122d4SMatthias Ringwald             printf("RCV: '");
211*402122d4SMatthias Ringwald             for (i=0;i<size;i++){
212*402122d4SMatthias Ringwald                 putchar(packet[i]);
213*402122d4SMatthias Ringwald             }
214*402122d4SMatthias Ringwald             printf("'\n");
215*402122d4SMatthias Ringwald #endif
216*402122d4SMatthias Ringwald             break;
217*402122d4SMatthias Ringwald 
218bcf00d8fSMatthias Ringwald         default:
219bcf00d8fSMatthias Ringwald             break;
220bcf00d8fSMatthias Ringwald 	}
221bcf00d8fSMatthias Ringwald }
222bcf00d8fSMatthias Ringwald 
223*402122d4SMatthias Ringwald /*
224*402122d4SMatthias Ringwald  * @section Main Application Setup
225*402122d4SMatthias Ringwald  *
226*402122d4SMatthias Ringwald  * @text As with the packet and the heartbeat handlers, the combined app setup contains the code from the individual example setups.
227*402122d4SMatthias Ringwald  */
228bcf00d8fSMatthias Ringwald 
229bcf00d8fSMatthias Ringwald 
230*402122d4SMatthias Ringwald /* LISTING_START(MainConfiguration): Init L2CAP RFCOMM SDP SPP */
231*402122d4SMatthias Ringwald int btstack_main(int argc, const char * argv[])
232*402122d4SMatthias Ringwald {
2339ec2630cSMatthias Ringwald     (void)argc;
2349ec2630cSMatthias Ringwald     (void)argv;
235bcf00d8fSMatthias Ringwald 
236bcf00d8fSMatthias Ringwald     // register for HCI events
237bcf00d8fSMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
238bcf00d8fSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
239bcf00d8fSMatthias Ringwald 
240bcf00d8fSMatthias Ringwald     l2cap_init();
241bcf00d8fSMatthias Ringwald 
242*402122d4SMatthias Ringwald     rfcomm_init();
243*402122d4SMatthias Ringwald     rfcomm_register_service(packet_handler, RFCOMM_SERVER_CHANNEL, 0xffff);
244*402122d4SMatthias Ringwald 
245*402122d4SMatthias Ringwald     // init SDP, create record for SPP and register with SDP
246*402122d4SMatthias Ringwald     sdp_init();
247*402122d4SMatthias Ringwald     memset(spp_service_buffer, 0, sizeof(spp_service_buffer));
248*402122d4SMatthias Ringwald     spp_create_sdp_record(spp_service_buffer, 0x10001, RFCOMM_SERVER_CHANNEL, "SPP Streamer");
249*402122d4SMatthias Ringwald     sdp_register_service(spp_service_buffer);
250*402122d4SMatthias Ringwald     // printf("SDP service record size: %u\n", de_get_len(spp_service_buffer));
251*402122d4SMatthias Ringwald 
252*402122d4SMatthias Ringwald     gap_ssp_set_io_capability(SSP_IO_CAPABILITY_DISPLAY_YES_NO);
253*402122d4SMatthias Ringwald 
254*402122d4SMatthias Ringwald     // short-cut to find other SPP Streamer
255*402122d4SMatthias Ringwald     gap_set_class_of_device(TEST_COD);
256*402122d4SMatthias Ringwald 
257*402122d4SMatthias Ringwald     gap_discoverable_control(1);
258*402122d4SMatthias Ringwald 
259*402122d4SMatthias Ringwald     spp_create_test_data();
260*402122d4SMatthias Ringwald 
261bcf00d8fSMatthias Ringwald     // turn on!
262bcf00d8fSMatthias Ringwald 	hci_power_control(HCI_POWER_ON);
263bcf00d8fSMatthias Ringwald 
264bcf00d8fSMatthias Ringwald     return 0;
265bcf00d8fSMatthias Ringwald }
266*402122d4SMatthias Ringwald /* LISTING_END */
267*402122d4SMatthias Ringwald /* EXAMPLE_END */
268