xref: /btstack/example/spp_streamer.c (revision 1a6822024624aab466a9aff5f67325f81ed6d5e2)
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 += bytes_sent;
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     UNUSED(channel);
154     UNUSED(size);
155 
156     if (packet_type != HCI_EVENT_PACKET) return;
157     uint8_t event = hci_event_packet_get_type(packet);
158     switch (event) {
159         case BTSTACK_EVENT_STATE:
160             // bt stack activated, get started
161             if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
162                 printf("SDP Query for RFCOMM services on %s started\n", bd_addr_to_str(remote));
163                 sdp_client_query_rfcomm_channel_and_name_for_uuid(&handle_query_rfcomm_event, remote, SDP_PublicBrowseGroup);
164             }
165             break;
166         case RFCOMM_EVENT_CHANNEL_OPENED:
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                 state = DONE;
170                 printf("RFCOMM channel open failed, status %u\n", packet[2]);
171             } else {
172                 // data: event(8), len(8), status (8), address (48), handle (16), server channel(8), rfcomm_cid(16), max frame size(16)
173                 state = SENDING;
174                 rfcomm_cid = little_endian_read_16(packet, 12);
175                 mtu = little_endian_read_16(packet, 14);
176                 printf("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u\n", rfcomm_cid, mtu);
177                 if ((test_data_len > mtu)) {
178                     test_data_len = mtu;
179                 }
180                 test_reset();
181                 rfcomm_request_can_send_now_event(rfcomm_cid);
182                 break;
183             }
184             break;
185         case RFCOMM_EVENT_CAN_SEND_NOW:
186             send_packet();
187             break;
188         case RFCOMM_EVENT_CHANNEL_CLOSED:
189             if (state != DONE) {
190                 printf("RFCOMM_EVENT_CHANNEL_CLOSED received before all test data was sent\n");
191                 state = DONE;
192             }
193             break;
194         default:
195             break;
196     }
197 }
198 
199 static void handle_found_service(const char * name, uint8_t port){
200     printf("APP: Service name: '%s', RFCOMM port %u\n", name, port);
201 
202     if (strncmp(name, spp_service_name_prefix, strlen(spp_service_name_prefix)) != 0) return;
203 
204     printf("APP: matches requested SPP Service Name\n");
205     channel_nr = port;
206     state = W4_SDP_COMPLETE;
207 }
208 
209 static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
210     UNUSED(packet_type);
211     UNUSED(channel);
212     UNUSED(size);
213 
214     switch (packet[0]){
215         case SDP_EVENT_QUERY_RFCOMM_SERVICE:
216             handle_found_service(sdp_event_query_rfcomm_service_get_name(packet),
217                                  sdp_event_query_rfcomm_service_get_rfcomm_channel(packet));
218             break;
219         case SDP_EVENT_QUERY_COMPLETE:
220             if (state != W4_SDP_COMPLETE){
221                 printf("Requested SPP Service %s not found \n", spp_service_name_prefix);
222                 break;
223             }
224             // connect
225             printf("Requested SPP Service found, creating RFCOMM channel\n");
226             state = W4_RFCOMM_CHANNEL;
227             rfcomm_create_channel(packet_handler, remote, channel_nr, NULL);
228             break;
229         default:
230             break;
231     }
232 }
233 
234 int btstack_main(int argc, const char * argv[]);
235 int btstack_main(int argc, const char * argv[]){
236     (void)argc;
237     (void)argv;
238 
239     create_test_data();
240 
241     printf("Client HCI init done\r\n");
242 
243     // register for HCI events
244     hci_event_callback_registration.callback = &packet_handler;
245     hci_add_event_handler(&hci_event_callback_registration);
246 
247     // init L2CAP
248     l2cap_init();
249 
250     // turn on!
251     hci_power_control(HCI_POWER_ON);
252 
253     return 0;
254 }
255