xref: /btstack/example/spp_streamer.c (revision 235946f152c66eb4ba735043adb492f7a538d15c)
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 
38ab2c6ae4SMatthias Ringwald #define __BTSTACK_FILE__ "spp_streamer.c"
39bcf00d8fSMatthias Ringwald 
40bcf00d8fSMatthias Ringwald // *****************************************************************************
41bcf00d8fSMatthias Ringwald //
42bcf00d8fSMatthias Ringwald // minimal setup for SDP client over USB or UART
43bcf00d8fSMatthias 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>
50bcf00d8fSMatthias Ringwald 
51*235946f1SMatthias Ringwald #include "btstack.h"
52bcf00d8fSMatthias Ringwald 
53bcf00d8fSMatthias Ringwald 
54bcf00d8fSMatthias Ringwald #define NUM_ROWS 25
55bcf00d8fSMatthias Ringwald #define NUM_COLS 40
56bcf00d8fSMatthias Ringwald 
57bcf00d8fSMatthias Ringwald typedef enum {
58bcf00d8fSMatthias Ringwald     W4_SDP_RESULT,
59bcf00d8fSMatthias Ringwald     W4_SDP_COMPLETE,
60bcf00d8fSMatthias Ringwald     W4_RFCOMM_CHANNEL,
611193857eSMatthias Ringwald     SENDING,
62bcf00d8fSMatthias Ringwald     DONE
63bcf00d8fSMatthias Ringwald } state_t;
64bcf00d8fSMatthias Ringwald 
65bcf00d8fSMatthias Ringwald static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
66bcf00d8fSMatthias Ringwald 
67369c1a52SMatthias Ringwald #define DATA_VOLUME (10 * 1000 * 1000)
68bcf00d8fSMatthias Ringwald 
69bcf00d8fSMatthias Ringwald // configuration area {
70bcf00d8fSMatthias Ringwald static bd_addr_t remote = {0x84, 0x38, 0x35, 0x65, 0xD1, 0x15};     // address of remote device
71bcf00d8fSMatthias Ringwald static const char * spp_service_name_prefix = "Bluetooth-Incoming"; // default on OS X
72bcf00d8fSMatthias Ringwald // configuration area }
73bcf00d8fSMatthias Ringwald 
74bcf00d8fSMatthias Ringwald static uint8_t  test_data[NUM_ROWS * NUM_COLS];
75bcf00d8fSMatthias Ringwald static uint16_t test_data_len = sizeof(test_data);
76bcf00d8fSMatthias Ringwald static uint8_t  channel_nr = 0;
77bcf00d8fSMatthias Ringwald static uint16_t mtu;
78bcf00d8fSMatthias Ringwald static uint16_t rfcomm_cid = 0;
79bcf00d8fSMatthias Ringwald static uint32_t data_to_send =  DATA_VOLUME;
80bcf00d8fSMatthias Ringwald static state_t state = W4_SDP_RESULT;
81bcf00d8fSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
82bcf00d8fSMatthias Ringwald 
83369c1a52SMatthias Ringwald /*
84369c1a52SMatthias Ringwald  * @section Track throughput
85369c1a52SMatthias Ringwald  * @text We calculate the throughput by setting a start time and measuring the amount of
86369c1a52SMatthias Ringwald  * data sent. After a configurable REPORT_INTERVAL_MS, we print the throughput in kB/s
87369c1a52SMatthias Ringwald  * and reset the counter and start time.
88369c1a52SMatthias Ringwald  */
89369c1a52SMatthias Ringwald 
90369c1a52SMatthias Ringwald /* LISTING_START(tracking): Tracking throughput */
91369c1a52SMatthias Ringwald #define REPORT_INTERVAL_MS 3000
92369c1a52SMatthias Ringwald static uint32_t test_data_sent;
93369c1a52SMatthias Ringwald static uint32_t test_data_start;
94369c1a52SMatthias Ringwald 
95369c1a52SMatthias Ringwald static void test_reset(void){
96369c1a52SMatthias Ringwald     test_data_start = btstack_run_loop_get_time_ms();
97369c1a52SMatthias Ringwald     test_data_sent = 0;
98369c1a52SMatthias Ringwald }
99369c1a52SMatthias Ringwald 
100369c1a52SMatthias Ringwald static void test_track_sent(int bytes_sent){
1019ec2630cSMatthias Ringwald     test_data_sent += bytes_sent;
102369c1a52SMatthias Ringwald     // evaluate
103369c1a52SMatthias Ringwald     uint32_t now = btstack_run_loop_get_time_ms();
104369c1a52SMatthias Ringwald     uint32_t time_passed = now - test_data_start;
105369c1a52SMatthias Ringwald     if (time_passed < REPORT_INTERVAL_MS) return;
106369c1a52SMatthias Ringwald     // print speed
107369c1a52SMatthias Ringwald     int bytes_per_second = test_data_sent * 1000 / time_passed;
1083c64bdf9SMatthias Ringwald     printf("%u bytes sent-> %u.%03u kB/s\n", (int) test_data_sent, (int) bytes_per_second / 1000, bytes_per_second % 1000);
109369c1a52SMatthias Ringwald 
110369c1a52SMatthias Ringwald     // restart
111369c1a52SMatthias Ringwald     test_data_start = now;
112369c1a52SMatthias Ringwald     test_data_sent  = 0;
113369c1a52SMatthias Ringwald }
114369c1a52SMatthias Ringwald /* LISTING_END(tracking): Tracking throughput */
115369c1a52SMatthias Ringwald 
116369c1a52SMatthias Ringwald 
117bcf00d8fSMatthias Ringwald static void create_test_data(void){
118bcf00d8fSMatthias Ringwald     int x,y;
119bcf00d8fSMatthias Ringwald     for (y=0;y<NUM_ROWS;y++){
120bcf00d8fSMatthias Ringwald         for (x=0;x<NUM_COLS-2;x++){
121bcf00d8fSMatthias Ringwald             test_data[y*NUM_COLS+x] = '0' + (x % 10);
122bcf00d8fSMatthias Ringwald         }
123bcf00d8fSMatthias Ringwald         test_data[y*NUM_COLS+NUM_COLS-2] = '\n';
124bcf00d8fSMatthias Ringwald         test_data[y*NUM_COLS+NUM_COLS-1] = '\r';
125bcf00d8fSMatthias Ringwald     }
126bcf00d8fSMatthias Ringwald }
127bcf00d8fSMatthias Ringwald 
1285f27b1a1SMatthias Ringwald static void send_packet(void){
1296dcd2e77SMatthias Ringwald     rfcomm_send(rfcomm_cid, (uint8_t*) test_data, test_data_len);
1306dcd2e77SMatthias Ringwald 
131369c1a52SMatthias Ringwald     test_track_sent(test_data_len);
1326dcd2e77SMatthias Ringwald     if (data_to_send <= test_data_len){
1331193857eSMatthias Ringwald         state = DONE;
1346dcd2e77SMatthias Ringwald         printf("SPP Streamer: enough data send, closing channel\n");
135bcf00d8fSMatthias Ringwald         rfcomm_disconnect(rfcomm_cid);
136bcf00d8fSMatthias Ringwald         rfcomm_cid = 0;
137bcf00d8fSMatthias Ringwald         return;
138bcf00d8fSMatthias Ringwald     }
139bcf00d8fSMatthias Ringwald     data_to_send -= test_data_len;
1405f27b1a1SMatthias Ringwald     rfcomm_request_can_send_now_event(rfcomm_cid);
141369c1a52SMatthias Ringwald }
142bcf00d8fSMatthias Ringwald 
143bcf00d8fSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1449ec2630cSMatthias Ringwald     UNUSED(channel);
1459ec2630cSMatthias Ringwald     UNUSED(size);
1469ec2630cSMatthias Ringwald 
147bcf00d8fSMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
1480e2df43fSMatthias Ringwald     uint8_t event = hci_event_packet_get_type(packet);
149bcf00d8fSMatthias Ringwald     switch (event) {
150bcf00d8fSMatthias Ringwald         case BTSTACK_EVENT_STATE:
151bcf00d8fSMatthias Ringwald             // bt stack activated, get started
152fb42b6e5SMilanka Ringwald             if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
1536dcd2e77SMatthias Ringwald                 printf("SDP Query for RFCOMM services on %s started\n", bd_addr_to_str(remote));
154*235946f1SMatthias Ringwald                 sdp_client_query_rfcomm_channel_and_name_for_uuid(&handle_query_rfcomm_event, remote, BLUETOOTH_ATTRIBUTE_PUBLIC_BROWSE_ROOT);
155bcf00d8fSMatthias Ringwald             }
156bcf00d8fSMatthias Ringwald             break;
157f8f6a918SMatthias Ringwald         case RFCOMM_EVENT_CHANNEL_OPENED:
158bcf00d8fSMatthias Ringwald             // data: event(8), len(8), status (8), address (48), handle(16), server channel(8), rfcomm_cid(16), max frame size(16)
159bcf00d8fSMatthias Ringwald             if (packet[2]) {
1601193857eSMatthias Ringwald                 state = DONE;
161bcf00d8fSMatthias Ringwald                 printf("RFCOMM channel open failed, status %u\n", packet[2]);
162bcf00d8fSMatthias Ringwald             } else {
163bcf00d8fSMatthias Ringwald                 // data: event(8), len(8), status (8), address (48), handle (16), server channel(8), rfcomm_cid(16), max frame size(16)
1641193857eSMatthias Ringwald                 state = SENDING;
165bcf00d8fSMatthias Ringwald                 rfcomm_cid = little_endian_read_16(packet, 12);
166bcf00d8fSMatthias Ringwald                 mtu = little_endian_read_16(packet, 14);
167bcf00d8fSMatthias Ringwald                 printf("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u\n", rfcomm_cid, mtu);
168bcf00d8fSMatthias Ringwald                 if ((test_data_len > mtu)) {
169bcf00d8fSMatthias Ringwald                     test_data_len = mtu;
170bcf00d8fSMatthias Ringwald                 }
171369c1a52SMatthias Ringwald                 test_reset();
1725f27b1a1SMatthias Ringwald                 rfcomm_request_can_send_now_event(rfcomm_cid);
173bcf00d8fSMatthias Ringwald                 break;
174bcf00d8fSMatthias Ringwald             }
175bcf00d8fSMatthias Ringwald             break;
176bcf00d8fSMatthias Ringwald         case RFCOMM_EVENT_CAN_SEND_NOW:
1775f27b1a1SMatthias Ringwald             send_packet();
178bcf00d8fSMatthias Ringwald             break;
1791193857eSMatthias Ringwald         case RFCOMM_EVENT_CHANNEL_CLOSED:
1801193857eSMatthias Ringwald             if (state != DONE) {
1811193857eSMatthias Ringwald                 printf("RFCOMM_EVENT_CHANNEL_CLOSED received before all test data was sent\n");
1821193857eSMatthias Ringwald                 state = DONE;
1831193857eSMatthias Ringwald             }
1841193857eSMatthias Ringwald             break;
185bcf00d8fSMatthias Ringwald         default:
186bcf00d8fSMatthias Ringwald             break;
187bcf00d8fSMatthias Ringwald     }
188bcf00d8fSMatthias Ringwald }
189bcf00d8fSMatthias Ringwald 
190bcf00d8fSMatthias Ringwald static void handle_found_service(const char * name, uint8_t port){
191bcf00d8fSMatthias Ringwald     printf("APP: Service name: '%s', RFCOMM port %u\n", name, port);
192bcf00d8fSMatthias Ringwald 
193bcf00d8fSMatthias Ringwald     if (strncmp(name, spp_service_name_prefix, strlen(spp_service_name_prefix)) != 0) return;
194bcf00d8fSMatthias Ringwald 
195bcf00d8fSMatthias Ringwald     printf("APP: matches requested SPP Service Name\n");
196bcf00d8fSMatthias Ringwald     channel_nr = port;
197bcf00d8fSMatthias Ringwald     state = W4_SDP_COMPLETE;
198bcf00d8fSMatthias Ringwald }
199bcf00d8fSMatthias Ringwald 
200bcf00d8fSMatthias Ringwald static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
2019ec2630cSMatthias Ringwald     UNUSED(packet_type);
2029ec2630cSMatthias Ringwald     UNUSED(channel);
2039ec2630cSMatthias Ringwald     UNUSED(size);
2049ec2630cSMatthias Ringwald 
205bcf00d8fSMatthias Ringwald     switch (packet[0]){
206bcf00d8fSMatthias Ringwald         case SDP_EVENT_QUERY_RFCOMM_SERVICE:
207bcf00d8fSMatthias Ringwald             handle_found_service(sdp_event_query_rfcomm_service_get_name(packet),
208bcf00d8fSMatthias Ringwald                                  sdp_event_query_rfcomm_service_get_rfcomm_channel(packet));
209bcf00d8fSMatthias Ringwald             break;
210bcf00d8fSMatthias Ringwald         case SDP_EVENT_QUERY_COMPLETE:
211bcf00d8fSMatthias Ringwald             if (state != W4_SDP_COMPLETE){
212bcf00d8fSMatthias Ringwald                 printf("Requested SPP Service %s not found \n", spp_service_name_prefix);
213bcf00d8fSMatthias Ringwald                 break;
214bcf00d8fSMatthias Ringwald             }
215bcf00d8fSMatthias Ringwald             // connect
216bcf00d8fSMatthias Ringwald             printf("Requested SPP Service found, creating RFCOMM channel\n");
217bcf00d8fSMatthias Ringwald             state = W4_RFCOMM_CHANNEL;
218bcf00d8fSMatthias Ringwald             rfcomm_create_channel(packet_handler, remote, channel_nr, NULL);
219bcf00d8fSMatthias Ringwald             break;
220bcf00d8fSMatthias Ringwald         default:
221bcf00d8fSMatthias Ringwald             break;
222bcf00d8fSMatthias Ringwald     }
223bcf00d8fSMatthias Ringwald }
224bcf00d8fSMatthias Ringwald 
225bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]);
226bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]){
2279ec2630cSMatthias Ringwald     (void)argc;
2289ec2630cSMatthias Ringwald     (void)argv;
229bcf00d8fSMatthias Ringwald 
230bcf00d8fSMatthias Ringwald     create_test_data();
231bcf00d8fSMatthias Ringwald 
232bcf00d8fSMatthias Ringwald     printf("Client HCI init done\r\n");
233bcf00d8fSMatthias Ringwald 
234bcf00d8fSMatthias Ringwald     // register for HCI events
235bcf00d8fSMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
236bcf00d8fSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
237bcf00d8fSMatthias Ringwald 
238bcf00d8fSMatthias Ringwald     // init L2CAP
239bcf00d8fSMatthias Ringwald     l2cap_init();
240bcf00d8fSMatthias Ringwald 
241bcf00d8fSMatthias Ringwald     // turn on!
242bcf00d8fSMatthias Ringwald     hci_power_control(HCI_POWER_ON);
243bcf00d8fSMatthias Ringwald 
244bcf00d8fSMatthias Ringwald     return 0;
245bcf00d8fSMatthias Ringwald }
246