/* * Copyright (C) 2014 BlueKitchen GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the copyright holders nor the names of * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * 4. Any redistribution, use, or modification is done solely for * personal benefit and not for any commercial purpose or for * monetary gain. * * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * Please inquire about commercial licensing options at * contact@bluekitchen-gmbh.com * */ #define BTSTACK_FILE__ "spp_streamer_client.c" /* * spp_streamer_client.c */ // ***************************************************************************** /* EXAMPLE_START(spp_streamer_client): Performance - Stream Data over SPP (Client) * * @text Note: The SPP Streamer Client scans for and connects to SPP Streamer, * and measures the throughput. */ // ***************************************************************************** #include #include #include #include #include #include "btstack.h" // prototypes static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); static uint8_t rfcomm_server_channel; #define NUM_ROWS 25 #define NUM_COLS 40 #define TEST_COD 0x1234 #define TEST_MODE_SEND 1 #define TEST_MODE_RECEIVE 2 #define TEST_MODE_DUPLEX 3 // configure test mode: send only, receive only, full duplex #define TEST_MODE TEST_MODE_SEND typedef enum { // SPP W4_PEER_COD, W4_SCAN_COMPLETE, W4_SDP_RESULT, W2_SEND_SDP_QUERY, W4_RFCOMM_CHANNEL, SENDING, DONE } state_t; static uint8_t test_data[NUM_ROWS * NUM_COLS]; static uint16_t spp_test_data_len; static btstack_packet_callback_registration_t hci_event_callback_registration; static btstack_context_callback_registration_t handle_sdp_client_query_request; static bd_addr_t peer_addr; static state_t state; // SPP static uint16_t rfcomm_mtu; static uint16_t rfcomm_cid = 0; // static uint32_t data_to_send = DATA_VOLUME; /** * RFCOMM can make use for ERTM. Due to the need to re-transmit packets, * a large buffer is needed to still get high throughput */ #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE_FOR_RFCOMM static uint8_t ertm_buffer[20000]; static l2cap_ertm_config_t ertm_config = { 0, // ertm mandatory 8, // max transmit 2000, 12000, 1000, // l2cap ertm mtu 8, 8, 0, // No FCS }; static int ertm_buffer_in_use; static void rfcomm_ertm_request_handler(rfcomm_ertm_request_t * ertm_request){ printf("ERTM Buffer requested, buffer in use %u\n", ertm_buffer_in_use); if (ertm_buffer_in_use) return; ertm_buffer_in_use = 1; ertm_request->ertm_config = &ertm_config; ertm_request->ertm_buffer = ertm_buffer; ertm_request->ertm_buffer_size = sizeof(ertm_buffer); } static void rfcomm_ertm_released_handler(uint16_t ertm_id){ printf("ERTM Buffer released, buffer in use %u, ertm_id %x\n", ertm_buffer_in_use, ertm_id); ertm_buffer_in_use = 0; } #endif /** * Find remote peer by COD */ #define INQUIRY_INTERVAL 5 static void start_scan(void){ printf("Starting inquiry scan..\n"); state = W4_PEER_COD; gap_inquiry_start(INQUIRY_INTERVAL); } static void stop_scan(void){ printf("Stopping inquiry scan..\n"); state = W4_SCAN_COMPLETE; gap_inquiry_stop(); } /* * @section Track throughput * @text We calculate the throughput by setting a start time and measuring the amount of * data sent. After a configurable REPORT_INTERVAL_MS, we print the throughput in kB/s * and reset the counter and start time. */ /* LISTING_START(tracking): Tracking throughput */ #define REPORT_INTERVAL_MS 3000 static uint32_t test_data_transferred; static uint32_t test_data_start; static void test_reset(void){ test_data_start = btstack_run_loop_get_time_ms(); test_data_transferred = 0; } static void test_track_transferred(int bytes_sent){ test_data_transferred += bytes_sent; // evaluate uint32_t now = btstack_run_loop_get_time_ms(); uint32_t time_passed = now - test_data_start; if (time_passed < REPORT_INTERVAL_MS) return; // print speed int bytes_per_second = test_data_transferred * 1000 / time_passed; printf("%u bytes -> %u.%03u kB/s\n", (int) test_data_transferred, (int) bytes_per_second / 1000, bytes_per_second % 1000); // restart test_data_start = now; test_data_transferred = 0; } /* LISTING_END(tracking): Tracking throughput */ #if (TEST_MODE & TEST_MODE_SEND) static void spp_create_test_data(void){ int x,y; for (y=0;y sizeof(test_data)){ spp_test_data_len = sizeof(test_data); } spp_create_test_data(); state = SENDING; // start sending rfcomm_request_can_send_now_event(rfcomm_cid); #endif } break; #if (TEST_MODE & TEST_MODE_SEND) case RFCOMM_EVENT_CAN_SEND_NOW: spp_send_packet(); break; #endif case RFCOMM_EVENT_CHANNEL_CLOSED: printf("RFCOMM channel closed\n"); rfcomm_cid = 0; // re-enable page/inquiry scan again gap_discoverable_control(1); gap_connectable_control(1); break; default: break; } break; case RFCOMM_DATA_PACKET: test_track_transferred(size); #if 0 printf("RCV: '"); for (i=0;i