xref: /btstack/example/spp_streamer.c (revision 235946f152c66eb4ba735043adb492f7a538d15c)
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_streamer.c"
39 
40 // *****************************************************************************
41 //
42 // minimal setup for SDP client over USB or UART
43 //
44 // *****************************************************************************
45 
46 #include <stdint.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 
51 #include "btstack.h"
52 
53 
54 #define NUM_ROWS 25
55 #define NUM_COLS 40
56 
57 typedef enum {
58     W4_SDP_RESULT,
59     W4_SDP_COMPLETE,
60     W4_RFCOMM_CHANNEL,
61     SENDING,
62     DONE
63 } state_t;
64 
65 static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
66 
67 #define DATA_VOLUME (10 * 1000 * 1000)
68 
69 // configuration area {
70 static bd_addr_t remote = {0x84, 0x38, 0x35, 0x65, 0xD1, 0x15};     // address of remote device
71 static const char * spp_service_name_prefix = "Bluetooth-Incoming"; // default on OS X
72 // configuration area }
73 
74 static uint8_t  test_data[NUM_ROWS * NUM_COLS];
75 static uint16_t test_data_len = sizeof(test_data);
76 static uint8_t  channel_nr = 0;
77 static uint16_t mtu;
78 static uint16_t rfcomm_cid = 0;
79 static uint32_t data_to_send =  DATA_VOLUME;
80 static state_t state = W4_SDP_RESULT;
81 static btstack_packet_callback_registration_t hci_event_callback_registration;
82 
83 /*
84  * @section Track throughput
85  * @text We calculate the throughput by setting a start time and measuring the amount of
86  * data sent. After a configurable REPORT_INTERVAL_MS, we print the throughput in kB/s
87  * and reset the counter and start time.
88  */
89 
90 /* LISTING_START(tracking): Tracking throughput */
91 #define REPORT_INTERVAL_MS 3000
92 static uint32_t test_data_sent;
93 static uint32_t test_data_start;
94 
95 static void test_reset(void){
96     test_data_start = btstack_run_loop_get_time_ms();
97     test_data_sent = 0;
98 }
99 
100 static void test_track_sent(int bytes_sent){
101     test_data_sent += bytes_sent;
102     // evaluate
103     uint32_t now = btstack_run_loop_get_time_ms();
104     uint32_t time_passed = now - test_data_start;
105     if (time_passed < REPORT_INTERVAL_MS) return;
106     // print speed
107     int bytes_per_second = test_data_sent * 1000 / time_passed;
108     printf("%u bytes sent-> %u.%03u kB/s\n", (int) test_data_sent, (int) bytes_per_second / 1000, bytes_per_second % 1000);
109 
110     // restart
111     test_data_start = now;
112     test_data_sent  = 0;
113 }
114 /* LISTING_END(tracking): Tracking throughput */
115 
116 
117 static void create_test_data(void){
118     int x,y;
119     for (y=0;y<NUM_ROWS;y++){
120         for (x=0;x<NUM_COLS-2;x++){
121             test_data[y*NUM_COLS+x] = '0' + (x % 10);
122         }
123         test_data[y*NUM_COLS+NUM_COLS-2] = '\n';
124         test_data[y*NUM_COLS+NUM_COLS-1] = '\r';
125     }
126 }
127 
128 static void send_packet(void){
129     rfcomm_send(rfcomm_cid, (uint8_t*) test_data, test_data_len);
130 
131     test_track_sent(test_data_len);
132     if (data_to_send <= test_data_len){
133         state = DONE;
134         printf("SPP Streamer: enough data send, closing channel\n");
135         rfcomm_disconnect(rfcomm_cid);
136         rfcomm_cid = 0;
137         return;
138     }
139     data_to_send -= test_data_len;
140     rfcomm_request_can_send_now_event(rfcomm_cid);
141 }
142 
143 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
144     UNUSED(channel);
145     UNUSED(size);
146 
147     if (packet_type != HCI_EVENT_PACKET) return;
148     uint8_t event = hci_event_packet_get_type(packet);
149     switch (event) {
150         case BTSTACK_EVENT_STATE:
151             // bt stack activated, get started
152             if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
153                 printf("SDP Query for RFCOMM services on %s started\n", bd_addr_to_str(remote));
154                 sdp_client_query_rfcomm_channel_and_name_for_uuid(&handle_query_rfcomm_event, remote, BLUETOOTH_ATTRIBUTE_PUBLIC_BROWSE_ROOT);
155             }
156             break;
157         case RFCOMM_EVENT_CHANNEL_OPENED:
158             // data: event(8), len(8), status (8), address (48), handle(16), server channel(8), rfcomm_cid(16), max frame size(16)
159             if (packet[2]) {
160                 state = DONE;
161                 printf("RFCOMM channel open failed, status %u\n", packet[2]);
162             } else {
163                 // data: event(8), len(8), status (8), address (48), handle (16), server channel(8), rfcomm_cid(16), max frame size(16)
164                 state = SENDING;
165                 rfcomm_cid = little_endian_read_16(packet, 12);
166                 mtu = little_endian_read_16(packet, 14);
167                 printf("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u\n", rfcomm_cid, mtu);
168                 if ((test_data_len > mtu)) {
169                     test_data_len = mtu;
170                 }
171                 test_reset();
172                 rfcomm_request_can_send_now_event(rfcomm_cid);
173                 break;
174             }
175             break;
176         case RFCOMM_EVENT_CAN_SEND_NOW:
177             send_packet();
178             break;
179         case RFCOMM_EVENT_CHANNEL_CLOSED:
180             if (state != DONE) {
181                 printf("RFCOMM_EVENT_CHANNEL_CLOSED received before all test data was sent\n");
182                 state = DONE;
183             }
184             break;
185         default:
186             break;
187     }
188 }
189 
190 static void handle_found_service(const char * name, uint8_t port){
191     printf("APP: Service name: '%s', RFCOMM port %u\n", name, port);
192 
193     if (strncmp(name, spp_service_name_prefix, strlen(spp_service_name_prefix)) != 0) return;
194 
195     printf("APP: matches requested SPP Service Name\n");
196     channel_nr = port;
197     state = W4_SDP_COMPLETE;
198 }
199 
200 static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
201     UNUSED(packet_type);
202     UNUSED(channel);
203     UNUSED(size);
204 
205     switch (packet[0]){
206         case SDP_EVENT_QUERY_RFCOMM_SERVICE:
207             handle_found_service(sdp_event_query_rfcomm_service_get_name(packet),
208                                  sdp_event_query_rfcomm_service_get_rfcomm_channel(packet));
209             break;
210         case SDP_EVENT_QUERY_COMPLETE:
211             if (state != W4_SDP_COMPLETE){
212                 printf("Requested SPP Service %s not found \n", spp_service_name_prefix);
213                 break;
214             }
215             // connect
216             printf("Requested SPP Service found, creating RFCOMM channel\n");
217             state = W4_RFCOMM_CHANNEL;
218             rfcomm_create_channel(packet_handler, remote, channel_nr, NULL);
219             break;
220         default:
221             break;
222     }
223 }
224 
225 int btstack_main(int argc, const char * argv[]);
226 int btstack_main(int argc, const char * argv[]){
227     (void)argc;
228     (void)argv;
229 
230     create_test_data();
231 
232     printf("Client HCI init done\r\n");
233 
234     // register for HCI events
235     hci_event_callback_registration.callback = &packet_handler;
236     hci_add_event_handler(&hci_event_callback_registration);
237 
238     // init L2CAP
239     l2cap_init();
240 
241     // turn on!
242     hci_power_control(HCI_POWER_ON);
243 
244     return 0;
245 }
246