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