1cee62800SMatthias Ringwald /* 2cee62800SMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 3cee62800SMatthias Ringwald * 4cee62800SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 5cee62800SMatthias Ringwald * modification, are permitted provided that the following conditions 6cee62800SMatthias Ringwald * are met: 7cee62800SMatthias Ringwald * 8cee62800SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 9cee62800SMatthias Ringwald * notice, this list of conditions and the following disclaimer. 10cee62800SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 11cee62800SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 12cee62800SMatthias Ringwald * documentation and/or other materials provided with the distribution. 13cee62800SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 14cee62800SMatthias Ringwald * contributors may be used to endorse or promote products derived 15cee62800SMatthias Ringwald * from this software without specific prior written permission. 16cee62800SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 17cee62800SMatthias Ringwald * personal benefit and not for any commercial purpose or for 18cee62800SMatthias Ringwald * monetary gain. 19cee62800SMatthias Ringwald * 20cee62800SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21cee62800SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22cee62800SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23cee62800SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24cee62800SMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25cee62800SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26cee62800SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27cee62800SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28cee62800SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29cee62800SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30cee62800SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31cee62800SMatthias Ringwald * SUCH DAMAGE. 32cee62800SMatthias Ringwald * 33cee62800SMatthias Ringwald * Please inquire about commercial licensing options at 34cee62800SMatthias Ringwald * [email protected] 35cee62800SMatthias Ringwald * 36cee62800SMatthias Ringwald */ 37cee62800SMatthias Ringwald 3815de8206SMilanka Ringwald #define __BTSTACK_FILE__ "spp_streamer_client.c" 3915de8206SMilanka Ringwald 4015de8206SMilanka Ringwald /* 4115de8206SMilanka Ringwald * spp_streamer_client.c 4215de8206SMilanka Ringwald */ 4315de8206SMilanka Ringwald 44cee62800SMatthias Ringwald // ***************************************************************************** 45cee62800SMatthias Ringwald /* EXAMPLE_START(spp_streamer_client): Client for SPP Streamer 46cee62800SMatthias Ringwald * 4758d7a529SMilanka Ringwald * @text Note: The SPP Streamer Client scans for and connects to SPP Streamer, 4858d7a529SMilanka Ringwald * and measures the throughput. 49cee62800SMatthias Ringwald */ 50cee62800SMatthias Ringwald // ***************************************************************************** 51cee62800SMatthias Ringwald 52cee62800SMatthias Ringwald #include <stdint.h> 53cee62800SMatthias Ringwald #include <stdio.h> 54cee62800SMatthias Ringwald #include <stdlib.h> 55cee62800SMatthias Ringwald #include <string.h> 56cee62800SMatthias Ringwald #include <inttypes.h> 57cee62800SMatthias Ringwald 58cee62800SMatthias Ringwald #include "btstack.h" 59cee62800SMatthias Ringwald 60cee62800SMatthias Ringwald #define RFCOMM_SERVER_CHANNEL 1 61cee62800SMatthias Ringwald 62cee62800SMatthias Ringwald #define TEST_COD 0x1234 63cee62800SMatthias Ringwald 64cee62800SMatthias Ringwald typedef enum { 65cee62800SMatthias Ringwald // SPP 66cee62800SMatthias Ringwald W4_PEER_COD, 67cee62800SMatthias Ringwald W4_SCAN_COMPLETE, 68cee62800SMatthias Ringwald W4_SDP_RESULT, 69cee62800SMatthias Ringwald W4_SDP_COMPLETE, 70cee62800SMatthias Ringwald W4_RFCOMM_CHANNEL, 71cee62800SMatthias Ringwald SENDING, 72cee62800SMatthias Ringwald DONE 73cee62800SMatthias Ringwald } state_t; 74cee62800SMatthias Ringwald 75cee62800SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 76cee62800SMatthias Ringwald 77cee62800SMatthias Ringwald static bd_addr_t peer_addr; 78080ca4eeSMatthias Ringwald static state_t state; 79cee62800SMatthias Ringwald 80cee62800SMatthias Ringwald // SPP 81cee62800SMatthias Ringwald static uint16_t rfcomm_mtu; 82cee62800SMatthias Ringwald static uint16_t rfcomm_cid = 0; 83cee62800SMatthias Ringwald // static uint32_t data_to_send = DATA_VOLUME; 84cee62800SMatthias Ringwald 85*8cef84f3SMatthias Ringwald /** 86*8cef84f3SMatthias Ringwald * RFCOMM can make use for ERTM. Due to the need to re-transmit packets, 87*8cef84f3SMatthias Ringwald * a large buffer is needed to still get high throughput 88*8cef84f3SMatthias Ringwald */ 89*8cef84f3SMatthias Ringwald #if defined(ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE) && defined (ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE_FOR_RFCOMM) 90*8cef84f3SMatthias Ringwald static uint8_t ertm_buffer[20000]; 91*8cef84f3SMatthias Ringwald static l2cap_ertm_config_t ertm_config = { 92*8cef84f3SMatthias Ringwald 0, // ertm mandatory 93*8cef84f3SMatthias Ringwald 8, // max transmit 94*8cef84f3SMatthias Ringwald 2000, 95*8cef84f3SMatthias Ringwald 12000, 96*8cef84f3SMatthias Ringwald 1000, // l2cap ertm mtu 97*8cef84f3SMatthias Ringwald 8, 98*8cef84f3SMatthias Ringwald 8, 99*8cef84f3SMatthias Ringwald 0, // No FCS 100*8cef84f3SMatthias Ringwald }; 101*8cef84f3SMatthias Ringwald static int ertm_buffer_in_use; 102*8cef84f3SMatthias Ringwald static void rfcomm_ertm_request_handler(rfcomm_ertm_request_t * ertm_request){ 103*8cef84f3SMatthias Ringwald printf("ERTM Buffer requested, buffer in use %u\n", ertm_buffer_in_use); 104*8cef84f3SMatthias Ringwald if (ertm_buffer_in_use) return; 105*8cef84f3SMatthias Ringwald ertm_buffer_in_use = 1; 106*8cef84f3SMatthias Ringwald ertm_request->ertm_config = &ertm_config; 107*8cef84f3SMatthias Ringwald ertm_request->ertm_buffer = ertm_buffer; 108*8cef84f3SMatthias Ringwald ertm_request->ertm_buffer_size = sizeof(ertm_buffer); 109*8cef84f3SMatthias Ringwald } 110*8cef84f3SMatthias Ringwald static void rfcomm_ertm_released_handler(uint16_t ertm_id){ 111*8cef84f3SMatthias Ringwald printf("ERTM Buffer released, buffer in use %u, ertm_id %x\n", ertm_buffer_in_use, ertm_id); 112*8cef84f3SMatthias Ringwald ertm_buffer_in_use = 0; 113*8cef84f3SMatthias Ringwald } 114*8cef84f3SMatthias Ringwald #endif 115cee62800SMatthias Ringwald 116cee62800SMatthias Ringwald /** 117cee62800SMatthias Ringwald * Find remote peer by COD 118cee62800SMatthias Ringwald */ 119cee62800SMatthias Ringwald #define INQUIRY_INTERVAL 5 120cee62800SMatthias Ringwald static void start_scan(void){ 121cee62800SMatthias Ringwald printf("Starting inquiry scan..\n"); 122cee62800SMatthias Ringwald state = W4_PEER_COD; 123287c758dSMatthias Ringwald gap_inquiry_start(INQUIRY_INTERVAL); 124cee62800SMatthias Ringwald } 125cee62800SMatthias Ringwald static void stop_scan(void){ 126cee62800SMatthias Ringwald printf("Stopping inquiry scan..\n"); 127cee62800SMatthias Ringwald state = W4_SCAN_COMPLETE; 128287c758dSMatthias Ringwald gap_inquiry_stop(); 129cee62800SMatthias Ringwald } 130cee62800SMatthias Ringwald /* 131cee62800SMatthias Ringwald * @section Track throughput 132cee62800SMatthias Ringwald * @text We calculate the throughput by setting a start time and measuring the amount of 133cee62800SMatthias Ringwald * data sent. After a configurable REPORT_INTERVAL_MS, we print the throughput in kB/s 134cee62800SMatthias Ringwald * and reset the counter and start time. 135cee62800SMatthias Ringwald */ 136cee62800SMatthias Ringwald 137cee62800SMatthias Ringwald /* LISTING_START(tracking): Tracking throughput */ 138cee62800SMatthias Ringwald #define REPORT_INTERVAL_MS 3000 139cee62800SMatthias Ringwald static uint32_t test_data_transferred; 140cee62800SMatthias Ringwald static uint32_t test_data_start; 141cee62800SMatthias Ringwald 142cee62800SMatthias Ringwald static void test_reset(void){ 143cee62800SMatthias Ringwald test_data_start = btstack_run_loop_get_time_ms(); 144cee62800SMatthias Ringwald test_data_transferred = 0; 145cee62800SMatthias Ringwald } 146cee62800SMatthias Ringwald 147cee62800SMatthias Ringwald static void test_track_transferred(int bytes_sent){ 148cee62800SMatthias Ringwald test_data_transferred += bytes_sent; 149cee62800SMatthias Ringwald // evaluate 150cee62800SMatthias Ringwald uint32_t now = btstack_run_loop_get_time_ms(); 151cee62800SMatthias Ringwald uint32_t time_passed = now - test_data_start; 152cee62800SMatthias Ringwald if (time_passed < REPORT_INTERVAL_MS) return; 153cee62800SMatthias Ringwald // print speed 154cee62800SMatthias Ringwald int bytes_per_second = test_data_transferred * 1000 / time_passed; 155cee62800SMatthias Ringwald printf("%u bytes -> %u.%03u kB/s\n", (int) test_data_transferred, (int) bytes_per_second / 1000, bytes_per_second % 1000); 156cee62800SMatthias Ringwald 157cee62800SMatthias Ringwald // restart 158cee62800SMatthias Ringwald test_data_start = now; 159cee62800SMatthias Ringwald test_data_transferred = 0; 160cee62800SMatthias Ringwald } 161cee62800SMatthias Ringwald /* LISTING_END(tracking): Tracking throughput */ 162cee62800SMatthias Ringwald 163cee62800SMatthias Ringwald /* 164cee62800SMatthias Ringwald * @section Packet Handler 165cee62800SMatthias Ringwald * 166cee62800SMatthias Ringwald * @text The packet handler of the combined example is just the combination of the individual packet handlers. 167cee62800SMatthias Ringwald */ 168cee62800SMatthias Ringwald 169cee62800SMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 170cee62800SMatthias Ringwald UNUSED(channel); 171cee62800SMatthias Ringwald 172cee62800SMatthias Ringwald bd_addr_t event_addr; 173cee62800SMatthias Ringwald uint8_t rfcomm_channel_nr; 174cee62800SMatthias Ringwald uint32_t class_of_device; 175cee62800SMatthias Ringwald 176cee62800SMatthias Ringwald switch (packet_type) { 177cee62800SMatthias Ringwald case HCI_EVENT_PACKET: 178cee62800SMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 179cee62800SMatthias Ringwald 180cee62800SMatthias Ringwald case HCI_EVENT_PIN_CODE_REQUEST: 181cee62800SMatthias Ringwald // inform about pin code request 182cee62800SMatthias Ringwald printf("Pin code request - using '0000'\n"); 183cee62800SMatthias Ringwald hci_event_pin_code_request_get_bd_addr(packet, event_addr); 184cee62800SMatthias Ringwald gap_pin_code_response(event_addr, "0000"); 185cee62800SMatthias Ringwald break; 186cee62800SMatthias Ringwald 187cee62800SMatthias Ringwald case HCI_EVENT_USER_CONFIRMATION_REQUEST: 188cee62800SMatthias Ringwald // inform about user confirmation request 189cee62800SMatthias Ringwald printf("SSP User Confirmation Request with numeric value '%06"PRIu32"'\n", little_endian_read_32(packet, 8)); 190cee62800SMatthias Ringwald printf("SSP User Confirmation Auto accept\n"); 191cee62800SMatthias Ringwald break; 192cee62800SMatthias Ringwald 193cee62800SMatthias Ringwald case RFCOMM_EVENT_INCOMING_CONNECTION: 194cee62800SMatthias Ringwald // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16) 195cee62800SMatthias Ringwald rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr); 196cee62800SMatthias Ringwald rfcomm_channel_nr = rfcomm_event_incoming_connection_get_server_channel(packet); 197cee62800SMatthias Ringwald rfcomm_cid = rfcomm_event_incoming_connection_get_rfcomm_cid(packet); 198cee62800SMatthias Ringwald printf("RFCOMM channel %u requested for %s\n", rfcomm_channel_nr, bd_addr_to_str(event_addr)); 199cee62800SMatthias Ringwald rfcomm_accept_connection(rfcomm_cid); 200cee62800SMatthias Ringwald break; 201cee62800SMatthias Ringwald 202cee62800SMatthias Ringwald case RFCOMM_EVENT_CHANNEL_OPENED: 203cee62800SMatthias Ringwald // data: event(8), len(8), status (8), address (48), server channel(8), rfcomm_cid(16), max frame size(16) 204cee62800SMatthias Ringwald if (rfcomm_event_channel_opened_get_status(packet)) { 205cee62800SMatthias Ringwald printf("RFCOMM channel open failed, status %u\n", rfcomm_event_channel_opened_get_status(packet)); 206cee62800SMatthias Ringwald } else { 207cee62800SMatthias Ringwald rfcomm_cid = rfcomm_event_channel_opened_get_rfcomm_cid(packet); 208cee62800SMatthias Ringwald rfcomm_mtu = rfcomm_event_channel_opened_get_max_frame_size(packet); 209cee62800SMatthias Ringwald printf("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u\n", rfcomm_cid, rfcomm_mtu); 210cee62800SMatthias Ringwald test_reset(); 2115aed585bSMatthias Ringwald 2125aed585bSMatthias Ringwald // disable page/inquiry scan to get max performance 2135aed585bSMatthias Ringwald gap_discoverable_control(0); 214cee62800SMatthias Ringwald gap_connectable_control(0); 215cee62800SMatthias Ringwald } 216cee62800SMatthias Ringwald break; 217cee62800SMatthias Ringwald 218cee62800SMatthias Ringwald case RFCOMM_EVENT_CHANNEL_CLOSED: 219cee62800SMatthias Ringwald printf("RFCOMM channel closed\n"); 2205aed585bSMatthias Ringwald rfcomm_cid = 0; 2215aed585bSMatthias Ringwald 2225aed585bSMatthias Ringwald // re-enable page/inquiry scan again 2235aed585bSMatthias Ringwald gap_discoverable_control(1); 224cee62800SMatthias Ringwald gap_connectable_control(1); 225cee62800SMatthias Ringwald break; 226cee62800SMatthias Ringwald 227cee62800SMatthias Ringwald case BTSTACK_EVENT_STATE: 228cee62800SMatthias Ringwald if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return; 229cee62800SMatthias Ringwald start_scan(); 230cee62800SMatthias Ringwald break; 231cee62800SMatthias Ringwald 232cee62800SMatthias Ringwald case GAP_EVENT_INQUIRY_RESULT: 2337c03fe59SMatthias Ringwald if (state != W4_PEER_COD) break; 234cee62800SMatthias Ringwald class_of_device = gap_event_inquiry_result_get_class_of_device(packet); 235cee62800SMatthias Ringwald gap_event_inquiry_result_get_bd_addr(packet, event_addr); 236cee62800SMatthias Ringwald if (class_of_device == TEST_COD){ 237cee62800SMatthias Ringwald memcpy(peer_addr, event_addr, 6); 238cee62800SMatthias Ringwald printf("Peer found: %s\n", bd_addr_to_str(peer_addr)); 239cee62800SMatthias Ringwald stop_scan(); 240cee62800SMatthias Ringwald } else { 241cee62800SMatthias Ringwald printf("Device found: %s with COD: 0x%06x\n", bd_addr_to_str(event_addr), (int) class_of_device); 242cee62800SMatthias Ringwald } 243cee62800SMatthias Ringwald break; 244cee62800SMatthias Ringwald 245287c758dSMatthias Ringwald case GAP_EVENT_INQUIRY_COMPLETE: 2467c03fe59SMatthias Ringwald switch (state){ 2477c03fe59SMatthias Ringwald case W4_PEER_COD: 248cee62800SMatthias Ringwald printf("Inquiry complete\n"); 249cee62800SMatthias Ringwald printf("Peer not found, starting scan again\n"); 250cee62800SMatthias Ringwald start_scan(); 251cee62800SMatthias Ringwald break; 2527c03fe59SMatthias Ringwald case W4_SCAN_COMPLETE: 2537c03fe59SMatthias Ringwald printf("Start to connect\n"); 2547c03fe59SMatthias Ringwald state = W4_RFCOMM_CHANNEL; 2557c03fe59SMatthias Ringwald rfcomm_create_channel(packet_handler, peer_addr, RFCOMM_SERVER_CHANNEL, NULL); 2567c03fe59SMatthias Ringwald break; 2577c03fe59SMatthias Ringwald default: 2587c03fe59SMatthias Ringwald break; 2597c03fe59SMatthias Ringwald } 2607c03fe59SMatthias Ringwald if (state == W4_PEER_COD){ 2617c03fe59SMatthias Ringwald } 2627c03fe59SMatthias Ringwald break; 263cee62800SMatthias Ringwald 264cee62800SMatthias Ringwald default: 265cee62800SMatthias Ringwald break; 266cee62800SMatthias Ringwald } 267cee62800SMatthias Ringwald break; 268cee62800SMatthias Ringwald 269cee62800SMatthias Ringwald case RFCOMM_DATA_PACKET: 270cee62800SMatthias Ringwald test_track_transferred(size); 2717c03fe59SMatthias Ringwald 272cee62800SMatthias Ringwald #if 0 273cee62800SMatthias Ringwald printf("RCV: '"); 274cee62800SMatthias Ringwald for (i=0;i<size;i++){ 275cee62800SMatthias Ringwald putchar(packet[i]); 276cee62800SMatthias Ringwald } 277cee62800SMatthias Ringwald printf("'\n"); 278cee62800SMatthias Ringwald #endif 279cee62800SMatthias Ringwald break; 280cee62800SMatthias Ringwald 281cee62800SMatthias Ringwald default: 282cee62800SMatthias Ringwald break; 283cee62800SMatthias Ringwald } 284cee62800SMatthias Ringwald } 285cee62800SMatthias Ringwald 286cee62800SMatthias Ringwald /* 287cee62800SMatthias Ringwald * @section Main Application Setup 288cee62800SMatthias Ringwald * 289cee62800SMatthias Ringwald * @text As with the packet and the heartbeat handlers, the combined app setup contains the code from the individual example setups. 290cee62800SMatthias Ringwald */ 291cee62800SMatthias Ringwald 292cee62800SMatthias Ringwald 293cee62800SMatthias Ringwald /* LISTING_START(MainConfiguration): Init L2CAP RFCOMM SDO SM ATT Server and start heartbeat timer */ 294cee62800SMatthias Ringwald int btstack_main(int argc, const char * argv[]); 295cee62800SMatthias Ringwald int btstack_main(int argc, const char * argv[]){ 296cee62800SMatthias Ringwald UNUSED(argc); 297cee62800SMatthias Ringwald (void)argv; 298cee62800SMatthias Ringwald 299cee62800SMatthias Ringwald l2cap_init(); 300cee62800SMatthias Ringwald 301cee62800SMatthias Ringwald rfcomm_init(); 302cee62800SMatthias Ringwald rfcomm_register_service(packet_handler, RFCOMM_SERVER_CHANNEL, 0xffff); 303cee62800SMatthias Ringwald 304*8cef84f3SMatthias Ringwald #if defined(ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE) && defined (ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE_FOR_RFCOMM) 305*8cef84f3SMatthias Ringwald // setup ERTM management 306*8cef84f3SMatthias Ringwald rfcomm_enable_l2cap_ertm(&rfcomm_ertm_request_handler, &rfcomm_ertm_released_handler); 307*8cef84f3SMatthias Ringwald #endif 308*8cef84f3SMatthias Ringwald 309a4fe6467SMatthias Ringwald // register for HCI events 310a4fe6467SMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 311a4fe6467SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 312a4fe6467SMatthias Ringwald 313cee62800SMatthias Ringwald // init SDP 314cee62800SMatthias Ringwald gap_ssp_set_io_capability(SSP_IO_CAPABILITY_DISPLAY_YES_NO); 315cee62800SMatthias Ringwald 316cee62800SMatthias Ringwald // turn on! 317cee62800SMatthias Ringwald hci_power_control(HCI_POWER_ON); 318cee62800SMatthias Ringwald 319cee62800SMatthias Ringwald return 0; 320cee62800SMatthias Ringwald } 321cee62800SMatthias Ringwald /* LISTING_END */ 322cee62800SMatthias Ringwald /* EXAMPLE_END */ 323