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_client.c" 39 40 /* 41 * spp_streamer_client.c 42 */ 43 44 // ***************************************************************************** 45 /* EXAMPLE_START(spp_streamer_client): Client for SPP Streamer 46 * 47 * @text Note: The SPP Streamer Client scans for and connects to SPP Streamer, 48 * and measures the throughput. 49 */ 50 // ***************************************************************************** 51 52 #include <stdint.h> 53 #include <stdio.h> 54 #include <stdlib.h> 55 #include <string.h> 56 #include <inttypes.h> 57 58 #include "btstack.h" 59 60 #define RFCOMM_SERVER_CHANNEL 1 61 62 #define TEST_COD 0x1234 63 64 typedef enum { 65 // SPP 66 W4_PEER_COD, 67 W4_SCAN_COMPLETE, 68 W4_SDP_RESULT, 69 W4_SDP_COMPLETE, 70 W4_RFCOMM_CHANNEL, 71 SENDING, 72 DONE 73 } state_t; 74 75 static btstack_packet_callback_registration_t hci_event_callback_registration; 76 77 static bd_addr_t peer_addr; 78 static state_t state; 79 80 // SPP 81 static uint16_t rfcomm_mtu; 82 static uint16_t rfcomm_cid = 0; 83 // static uint32_t data_to_send = DATA_VOLUME; 84 85 86 /** 87 * Find remote peer by COD 88 */ 89 #define INQUIRY_INTERVAL 5 90 static void start_scan(void){ 91 printf("Starting inquiry scan..\n"); 92 state = W4_PEER_COD; 93 gap_inquiry_start(INQUIRY_INTERVAL); 94 } 95 static void stop_scan(void){ 96 printf("Stopping inquiry scan..\n"); 97 state = W4_SCAN_COMPLETE; 98 gap_inquiry_stop(); 99 } 100 /* 101 * @section Track throughput 102 * @text We calculate the throughput by setting a start time and measuring the amount of 103 * data sent. After a configurable REPORT_INTERVAL_MS, we print the throughput in kB/s 104 * and reset the counter and start time. 105 */ 106 107 /* LISTING_START(tracking): Tracking throughput */ 108 #define REPORT_INTERVAL_MS 3000 109 static uint32_t test_data_transferred; 110 static uint32_t test_data_start; 111 112 static void test_reset(void){ 113 test_data_start = btstack_run_loop_get_time_ms(); 114 test_data_transferred = 0; 115 } 116 117 static void test_track_transferred(int bytes_sent){ 118 test_data_transferred += bytes_sent; 119 // evaluate 120 uint32_t now = btstack_run_loop_get_time_ms(); 121 uint32_t time_passed = now - test_data_start; 122 if (time_passed < REPORT_INTERVAL_MS) return; 123 // print speed 124 int bytes_per_second = test_data_transferred * 1000 / time_passed; 125 printf("%u bytes -> %u.%03u kB/s\n", (int) test_data_transferred, (int) bytes_per_second / 1000, bytes_per_second % 1000); 126 127 // restart 128 test_data_start = now; 129 test_data_transferred = 0; 130 } 131 /* LISTING_END(tracking): Tracking throughput */ 132 133 /* 134 * @section Packet Handler 135 * 136 * @text The packet handler of the combined example is just the combination of the individual packet handlers. 137 */ 138 139 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 140 UNUSED(channel); 141 142 bd_addr_t event_addr; 143 uint8_t rfcomm_channel_nr; 144 uint32_t class_of_device; 145 146 switch (packet_type) { 147 case HCI_EVENT_PACKET: 148 switch (hci_event_packet_get_type(packet)) { 149 150 case HCI_EVENT_PIN_CODE_REQUEST: 151 // inform about pin code request 152 printf("Pin code request - using '0000'\n"); 153 hci_event_pin_code_request_get_bd_addr(packet, event_addr); 154 gap_pin_code_response(event_addr, "0000"); 155 break; 156 157 case HCI_EVENT_USER_CONFIRMATION_REQUEST: 158 // inform about user confirmation request 159 printf("SSP User Confirmation Request with numeric value '%06"PRIu32"'\n", little_endian_read_32(packet, 8)); 160 printf("SSP User Confirmation Auto accept\n"); 161 break; 162 163 case RFCOMM_EVENT_INCOMING_CONNECTION: 164 // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16) 165 rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr); 166 rfcomm_channel_nr = rfcomm_event_incoming_connection_get_server_channel(packet); 167 rfcomm_cid = rfcomm_event_incoming_connection_get_rfcomm_cid(packet); 168 printf("RFCOMM channel %u requested for %s\n", rfcomm_channel_nr, bd_addr_to_str(event_addr)); 169 rfcomm_accept_connection(rfcomm_cid); 170 break; 171 172 case RFCOMM_EVENT_CHANNEL_OPENED: 173 // data: event(8), len(8), status (8), address (48), server channel(8), rfcomm_cid(16), max frame size(16) 174 if (rfcomm_event_channel_opened_get_status(packet)) { 175 printf("RFCOMM channel open failed, status %u\n", rfcomm_event_channel_opened_get_status(packet)); 176 } else { 177 rfcomm_cid = rfcomm_event_channel_opened_get_rfcomm_cid(packet); 178 rfcomm_mtu = rfcomm_event_channel_opened_get_max_frame_size(packet); 179 printf("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u\n", rfcomm_cid, rfcomm_mtu); 180 test_reset(); 181 182 // disable page/inquiry scan to get max performance 183 gap_discoverable_control(0); 184 gap_connectable_control(0); 185 } 186 break; 187 188 case RFCOMM_EVENT_CHANNEL_CLOSED: 189 printf("RFCOMM channel closed\n"); 190 rfcomm_cid = 0; 191 192 // re-enable page/inquiry scan again 193 gap_discoverable_control(1); 194 gap_connectable_control(1); 195 break; 196 197 case BTSTACK_EVENT_STATE: 198 if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return; 199 start_scan(); 200 break; 201 202 case GAP_EVENT_INQUIRY_RESULT: 203 if (state != W4_PEER_COD) break; 204 class_of_device = gap_event_inquiry_result_get_class_of_device(packet); 205 gap_event_inquiry_result_get_bd_addr(packet, event_addr); 206 if (class_of_device == TEST_COD){ 207 memcpy(peer_addr, event_addr, 6); 208 printf("Peer found: %s\n", bd_addr_to_str(peer_addr)); 209 stop_scan(); 210 } else { 211 printf("Device found: %s with COD: 0x%06x\n", bd_addr_to_str(event_addr), (int) class_of_device); 212 } 213 break; 214 215 case GAP_EVENT_INQUIRY_COMPLETE: 216 switch (state){ 217 case W4_PEER_COD: 218 printf("Inquiry complete\n"); 219 printf("Peer not found, starting scan again\n"); 220 start_scan(); 221 break; 222 case W4_SCAN_COMPLETE: 223 printf("Start to connect\n"); 224 state = W4_RFCOMM_CHANNEL; 225 rfcomm_create_channel(packet_handler, peer_addr, RFCOMM_SERVER_CHANNEL, NULL); 226 break; 227 default: 228 break; 229 } 230 if (state == W4_PEER_COD){ 231 } 232 break; 233 234 default: 235 break; 236 } 237 break; 238 239 case RFCOMM_DATA_PACKET: 240 test_track_transferred(size); 241 242 #if 0 243 printf("RCV: '"); 244 for (i=0;i<size;i++){ 245 putchar(packet[i]); 246 } 247 printf("'\n"); 248 #endif 249 break; 250 251 default: 252 break; 253 } 254 } 255 256 /* 257 * @section Main Application Setup 258 * 259 * @text As with the packet and the heartbeat handlers, the combined app setup contains the code from the individual example setups. 260 */ 261 262 263 /* LISTING_START(MainConfiguration): Init L2CAP RFCOMM SDO SM ATT Server and start heartbeat timer */ 264 int btstack_main(int argc, const char * argv[]); 265 int btstack_main(int argc, const char * argv[]){ 266 UNUSED(argc); 267 (void)argv; 268 269 l2cap_init(); 270 271 rfcomm_init(); 272 rfcomm_register_service(packet_handler, RFCOMM_SERVER_CHANNEL, 0xffff); 273 274 // register for HCI events 275 hci_event_callback_registration.callback = &packet_handler; 276 hci_add_event_handler(&hci_event_callback_registration); 277 278 // init SDP 279 gap_ssp_set_io_capability(SSP_IO_CAPABILITY_DISPLAY_YES_NO); 280 281 // turn on! 282 hci_power_control(HCI_POWER_ON); 283 284 return 0; 285 } 286 /* LISTING_END */ 287 /* EXAMPLE_END */ 288