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