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