1bcf00d8fSMatthias Ringwald /* 2bcf00d8fSMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 3bcf00d8fSMatthias Ringwald * 4bcf00d8fSMatthias Ringwald * Redistribution and use in source and binary forms, with or without 5bcf00d8fSMatthias Ringwald * modification, are permitted provided that the following conditions 6bcf00d8fSMatthias Ringwald * are met: 7bcf00d8fSMatthias Ringwald * 8bcf00d8fSMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 9bcf00d8fSMatthias Ringwald * notice, this list of conditions and the following disclaimer. 10bcf00d8fSMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 11bcf00d8fSMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 12bcf00d8fSMatthias Ringwald * documentation and/or other materials provided with the distribution. 13bcf00d8fSMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 14bcf00d8fSMatthias Ringwald * contributors may be used to endorse or promote products derived 15bcf00d8fSMatthias Ringwald * from this software without specific prior written permission. 16bcf00d8fSMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 17bcf00d8fSMatthias Ringwald * personal benefit and not for any commercial purpose or for 18bcf00d8fSMatthias Ringwald * monetary gain. 19bcf00d8fSMatthias Ringwald * 20bcf00d8fSMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21bcf00d8fSMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22bcf00d8fSMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23bcf00d8fSMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24bcf00d8fSMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25bcf00d8fSMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26bcf00d8fSMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27bcf00d8fSMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28bcf00d8fSMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29bcf00d8fSMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30bcf00d8fSMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31bcf00d8fSMatthias Ringwald * SUCH DAMAGE. 32bcf00d8fSMatthias Ringwald * 33bcf00d8fSMatthias Ringwald * Please inquire about commercial licensing options at 34bcf00d8fSMatthias Ringwald * [email protected] 35bcf00d8fSMatthias Ringwald * 36bcf00d8fSMatthias Ringwald */ 37ab2c6ae4SMatthias Ringwald 38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "spp_streamer.c" 3915de8206SMilanka Ringwald 4015de8206SMilanka Ringwald /* 4115de8206SMilanka Ringwald * spp_streamer.c 4215de8206SMilanka Ringwald */ 4315de8206SMilanka Ringwald 44bcf00d8fSMatthias Ringwald // ***************************************************************************** 45ec8ae085SMilanka Ringwald /* EXAMPLE_START(spp_streamer): Performance - Stream Data over SPP (Server) 4615de8206SMilanka Ringwald * 47402122d4SMatthias Ringwald * @text After RFCOMM connections gets open, request a 48402122d4SMatthias Ringwald * RFCOMM_EVENT_CAN_SEND_NOW via rfcomm_request_can_send_now_event(). 4958d7a529SMilanka Ringwald * @text When we get the RFCOMM_EVENT_CAN_SEND_NOW, send data and request another one. 5058d7a529SMilanka Ringwald * 5158d7a529SMilanka Ringwald * @text Note: To test, run the example, pair from a remote 5258d7a529SMilanka Ringwald * device, and open the Virtual Serial Port. 53402122d4SMatthias Ringwald */ 54bcf00d8fSMatthias Ringwald // ***************************************************************************** 55bcf00d8fSMatthias Ringwald 56bcf00d8fSMatthias Ringwald #include <stdint.h> 57bcf00d8fSMatthias Ringwald #include <stdio.h> 58bcf00d8fSMatthias Ringwald #include <stdlib.h> 59bcf00d8fSMatthias Ringwald #include <string.h> 60402122d4SMatthias Ringwald #include <inttypes.h> 61bcf00d8fSMatthias Ringwald 62235946f1SMatthias Ringwald #include "btstack.h" 63bcf00d8fSMatthias Ringwald 64402122d4SMatthias Ringwald int btstack_main(int argc, const char * argv[]); 65bcf00d8fSMatthias Ringwald 66402122d4SMatthias Ringwald #define RFCOMM_SERVER_CHANNEL 1 67402122d4SMatthias Ringwald 68402122d4SMatthias Ringwald #define TEST_COD 0x1234 69bcf00d8fSMatthias Ringwald #define NUM_ROWS 25 70bcf00d8fSMatthias Ringwald #define NUM_COLS 40 71369c1a52SMatthias Ringwald #define DATA_VOLUME (10 * 1000 * 1000) 72bcf00d8fSMatthias Ringwald 73402122d4SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 74bcf00d8fSMatthias Ringwald 75bcf00d8fSMatthias Ringwald static uint8_t test_data[NUM_ROWS * NUM_COLS]; 76402122d4SMatthias Ringwald 77402122d4SMatthias Ringwald // SPP 78402122d4SMatthias Ringwald static uint8_t spp_service_buffer[150]; 79402122d4SMatthias Ringwald 80402122d4SMatthias Ringwald static uint16_t spp_test_data_len; 81402122d4SMatthias Ringwald static uint16_t rfcomm_mtu; 82bcf00d8fSMatthias Ringwald static uint16_t rfcomm_cid = 0; 83402122d4SMatthias Ringwald // static uint32_t data_to_send = DATA_VOLUME; 84bcf00d8fSMatthias Ringwald 858cef84f3SMatthias Ringwald /** 868cef84f3SMatthias Ringwald * RFCOMM can make use for ERTM. Due to the need to re-transmit packets, 878cef84f3SMatthias Ringwald * a large buffer is needed to still get high throughput 888cef84f3SMatthias Ringwald */ 89278494d6SMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE_FOR_RFCOMM 908cef84f3SMatthias Ringwald static uint8_t ertm_buffer[20000]; 918cef84f3SMatthias Ringwald static l2cap_ertm_config_t ertm_config = { 928cef84f3SMatthias Ringwald 0, // ertm mandatory 938cef84f3SMatthias Ringwald 8, // max transmit 948cef84f3SMatthias Ringwald 2000, 958cef84f3SMatthias Ringwald 12000, 968cef84f3SMatthias Ringwald 1000, // l2cap ertm mtu 978cef84f3SMatthias Ringwald 8, 988cef84f3SMatthias Ringwald 8, 998cef84f3SMatthias Ringwald 0, // No FCS 1008cef84f3SMatthias Ringwald }; 1018cef84f3SMatthias Ringwald static int ertm_buffer_in_use; 1028cef84f3SMatthias Ringwald static void rfcomm_ertm_request_handler(rfcomm_ertm_request_t * ertm_request){ 1038cef84f3SMatthias Ringwald printf("ERTM Buffer requested, buffer in use %u\n", ertm_buffer_in_use); 1048cef84f3SMatthias Ringwald if (ertm_buffer_in_use) return; 1058cef84f3SMatthias Ringwald ertm_buffer_in_use = 1; 1068cef84f3SMatthias Ringwald ertm_request->ertm_config = &ertm_config; 1078cef84f3SMatthias Ringwald ertm_request->ertm_buffer = ertm_buffer; 1088cef84f3SMatthias Ringwald ertm_request->ertm_buffer_size = sizeof(ertm_buffer); 1098cef84f3SMatthias Ringwald } 1108cef84f3SMatthias Ringwald static void rfcomm_ertm_released_handler(uint16_t ertm_id){ 1118cef84f3SMatthias Ringwald printf("ERTM Buffer released, buffer in use %u, ertm_id %x\n", ertm_buffer_in_use, ertm_id); 1128cef84f3SMatthias Ringwald ertm_buffer_in_use = 0; 1138cef84f3SMatthias Ringwald } 1148cef84f3SMatthias Ringwald #endif 1158cef84f3SMatthias Ringwald 116369c1a52SMatthias Ringwald /* 117369c1a52SMatthias Ringwald * @section Track throughput 118369c1a52SMatthias Ringwald * @text We calculate the throughput by setting a start time and measuring the amount of 119369c1a52SMatthias Ringwald * data sent. After a configurable REPORT_INTERVAL_MS, we print the throughput in kB/s 120369c1a52SMatthias Ringwald * and reset the counter and start time. 121369c1a52SMatthias Ringwald */ 122369c1a52SMatthias Ringwald 123369c1a52SMatthias Ringwald /* LISTING_START(tracking): Tracking throughput */ 124369c1a52SMatthias Ringwald #define REPORT_INTERVAL_MS 3000 125402122d4SMatthias Ringwald static uint32_t test_data_transferred; 126369c1a52SMatthias Ringwald static uint32_t test_data_start; 127369c1a52SMatthias Ringwald 128369c1a52SMatthias Ringwald static void test_reset(void){ 129369c1a52SMatthias Ringwald test_data_start = btstack_run_loop_get_time_ms(); 130402122d4SMatthias Ringwald test_data_transferred = 0; 131369c1a52SMatthias Ringwald } 132369c1a52SMatthias Ringwald 133402122d4SMatthias Ringwald static void test_track_transferred(int bytes_sent){ 134402122d4SMatthias Ringwald test_data_transferred += bytes_sent; 135369c1a52SMatthias Ringwald // evaluate 136369c1a52SMatthias Ringwald uint32_t now = btstack_run_loop_get_time_ms(); 137369c1a52SMatthias Ringwald uint32_t time_passed = now - test_data_start; 138369c1a52SMatthias Ringwald if (time_passed < REPORT_INTERVAL_MS) return; 139369c1a52SMatthias Ringwald // print speed 140402122d4SMatthias Ringwald int bytes_per_second = test_data_transferred * 1000 / time_passed; 141402122d4SMatthias Ringwald printf("%u bytes -> %u.%03u kB/s\n", (int) test_data_transferred, (int) bytes_per_second / 1000, bytes_per_second % 1000); 142369c1a52SMatthias Ringwald 143369c1a52SMatthias Ringwald // restart 144369c1a52SMatthias Ringwald test_data_start = now; 145402122d4SMatthias Ringwald test_data_transferred = 0; 146369c1a52SMatthias Ringwald } 147369c1a52SMatthias Ringwald /* LISTING_END(tracking): Tracking throughput */ 148369c1a52SMatthias Ringwald 149369c1a52SMatthias Ringwald 150402122d4SMatthias Ringwald static void spp_create_test_data(void){ 151bcf00d8fSMatthias Ringwald int x,y; 152bcf00d8fSMatthias Ringwald for (y=0;y<NUM_ROWS;y++){ 153bcf00d8fSMatthias Ringwald for (x=0;x<NUM_COLS-2;x++){ 154bcf00d8fSMatthias Ringwald test_data[y*NUM_COLS+x] = '0' + (x % 10); 155bcf00d8fSMatthias Ringwald } 156bcf00d8fSMatthias Ringwald test_data[y*NUM_COLS+NUM_COLS-2] = '\n'; 157bcf00d8fSMatthias Ringwald test_data[y*NUM_COLS+NUM_COLS-1] = '\r'; 158bcf00d8fSMatthias Ringwald } 159bcf00d8fSMatthias Ringwald } 160bcf00d8fSMatthias Ringwald 161402122d4SMatthias Ringwald static void spp_send_packet(void){ 162402122d4SMatthias Ringwald rfcomm_send(rfcomm_cid, (uint8_t*) test_data, spp_test_data_len); 1636dcd2e77SMatthias Ringwald 164402122d4SMatthias Ringwald test_track_transferred(spp_test_data_len); 165402122d4SMatthias Ringwald #if 0 166402122d4SMatthias Ringwald if (data_to_send <= spp_test_data_len){ 1676dcd2e77SMatthias Ringwald printf("SPP Streamer: enough data send, closing channel\n"); 168bcf00d8fSMatthias Ringwald rfcomm_disconnect(rfcomm_cid); 169bcf00d8fSMatthias Ringwald rfcomm_cid = 0; 170bcf00d8fSMatthias Ringwald return; 171bcf00d8fSMatthias Ringwald } 172402122d4SMatthias Ringwald data_to_send -= spp_test_data_len; 173402122d4SMatthias Ringwald #endif 1745f27b1a1SMatthias Ringwald rfcomm_request_can_send_now_event(rfcomm_cid); 175369c1a52SMatthias Ringwald } 176bcf00d8fSMatthias Ringwald 177402122d4SMatthias Ringwald /* 178402122d4SMatthias Ringwald * @section Packet Handler 179402122d4SMatthias Ringwald * 180402122d4SMatthias Ringwald * @text The packet handler of the combined example is just the combination of the individual packet handlers. 181402122d4SMatthias Ringwald */ 182402122d4SMatthias Ringwald 183bcf00d8fSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1849ec2630cSMatthias Ringwald UNUSED(channel); 1859ec2630cSMatthias Ringwald 186402122d4SMatthias Ringwald bd_addr_t event_addr; 187402122d4SMatthias Ringwald uint8_t rfcomm_channel_nr; 188402122d4SMatthias Ringwald 189402122d4SMatthias Ringwald switch (packet_type) { 190402122d4SMatthias Ringwald case HCI_EVENT_PACKET: 191402122d4SMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 192402122d4SMatthias Ringwald 193402122d4SMatthias Ringwald case HCI_EVENT_PIN_CODE_REQUEST: 194402122d4SMatthias Ringwald // inform about pin code request 195402122d4SMatthias Ringwald printf("Pin code request - using '0000'\n"); 196402122d4SMatthias Ringwald hci_event_pin_code_request_get_bd_addr(packet, event_addr); 197402122d4SMatthias Ringwald gap_pin_code_response(event_addr, "0000"); 198bcf00d8fSMatthias Ringwald break; 199402122d4SMatthias Ringwald 200402122d4SMatthias Ringwald case HCI_EVENT_USER_CONFIRMATION_REQUEST: 201402122d4SMatthias Ringwald // inform about user confirmation request 202402122d4SMatthias Ringwald printf("SSP User Confirmation Request with numeric value '%06"PRIu32"'\n", little_endian_read_32(packet, 8)); 203402122d4SMatthias Ringwald printf("SSP User Confirmation Auto accept\n"); 204402122d4SMatthias Ringwald break; 205402122d4SMatthias Ringwald 206402122d4SMatthias Ringwald case RFCOMM_EVENT_INCOMING_CONNECTION: 207402122d4SMatthias Ringwald rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr); 208402122d4SMatthias Ringwald rfcomm_channel_nr = rfcomm_event_incoming_connection_get_server_channel(packet); 209402122d4SMatthias Ringwald rfcomm_cid = rfcomm_event_incoming_connection_get_rfcomm_cid(packet); 210402122d4SMatthias Ringwald printf("RFCOMM channel %u requested for %s\n", rfcomm_channel_nr, bd_addr_to_str(event_addr)); 211402122d4SMatthias Ringwald rfcomm_accept_connection(rfcomm_cid); 212402122d4SMatthias Ringwald break; 213402122d4SMatthias Ringwald 214f8f6a918SMatthias Ringwald case RFCOMM_EVENT_CHANNEL_OPENED: 215402122d4SMatthias Ringwald if (rfcomm_event_channel_opened_get_status(packet)) { 216402122d4SMatthias Ringwald printf("RFCOMM channel open failed, status %u\n", rfcomm_event_channel_opened_get_status(packet)); 217bcf00d8fSMatthias Ringwald } else { 218402122d4SMatthias Ringwald rfcomm_cid = rfcomm_event_channel_opened_get_rfcomm_cid(packet); 219402122d4SMatthias Ringwald rfcomm_mtu = rfcomm_event_channel_opened_get_max_frame_size(packet); 220402122d4SMatthias Ringwald printf("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u\n", rfcomm_cid, rfcomm_mtu); 221402122d4SMatthias Ringwald 222402122d4SMatthias Ringwald spp_test_data_len = rfcomm_mtu; 223402122d4SMatthias Ringwald if (spp_test_data_len > sizeof(test_data)){ 224402122d4SMatthias Ringwald spp_test_data_len = sizeof(test_data); 225bcf00d8fSMatthias Ringwald } 226402122d4SMatthias Ringwald 2275aed585bSMatthias Ringwald // disable page/inquiry scan to get max performance 2285aed585bSMatthias Ringwald gap_discoverable_control(0); 2295aed585bSMatthias Ringwald gap_connectable_control(0); 2305aed585bSMatthias Ringwald 231369c1a52SMatthias Ringwald test_reset(); 2325f27b1a1SMatthias Ringwald rfcomm_request_can_send_now_event(rfcomm_cid); 233bcf00d8fSMatthias Ringwald } 234bcf00d8fSMatthias Ringwald break; 235402122d4SMatthias Ringwald 236bcf00d8fSMatthias Ringwald case RFCOMM_EVENT_CAN_SEND_NOW: 237402122d4SMatthias Ringwald spp_send_packet(); 238bcf00d8fSMatthias Ringwald break; 239402122d4SMatthias Ringwald 2401193857eSMatthias Ringwald case RFCOMM_EVENT_CHANNEL_CLOSED: 241402122d4SMatthias Ringwald printf("RFCOMM channel closed\n"); 242402122d4SMatthias Ringwald rfcomm_cid = 0; 2435aed585bSMatthias Ringwald 2445aed585bSMatthias Ringwald // re-enable page/inquiry scan again 2455aed585bSMatthias Ringwald gap_discoverable_control(1); 2465aed585bSMatthias Ringwald gap_connectable_control(1); 247402122d4SMatthias Ringwald break; 248402122d4SMatthias Ringwald 249402122d4SMatthias Ringwald default: 250402122d4SMatthias Ringwald break; 2511193857eSMatthias Ringwald } 2521193857eSMatthias Ringwald break; 253402122d4SMatthias Ringwald 254402122d4SMatthias Ringwald case RFCOMM_DATA_PACKET: 255402122d4SMatthias Ringwald test_track_transferred(size); 256402122d4SMatthias Ringwald #if 0 257*6058cb0dSMatthias Ringwald // optional: print received data as ASCII text 258402122d4SMatthias Ringwald printf("RCV: '"); 259402122d4SMatthias Ringwald for (i=0;i<size;i++){ 260402122d4SMatthias Ringwald putchar(packet[i]); 261402122d4SMatthias Ringwald } 262402122d4SMatthias Ringwald printf("'\n"); 263402122d4SMatthias Ringwald #endif 264402122d4SMatthias Ringwald break; 265402122d4SMatthias Ringwald 266bcf00d8fSMatthias Ringwald default: 267bcf00d8fSMatthias Ringwald break; 268bcf00d8fSMatthias Ringwald } 269bcf00d8fSMatthias Ringwald } 270bcf00d8fSMatthias Ringwald 271402122d4SMatthias Ringwald /* 272402122d4SMatthias Ringwald * @section Main Application Setup 273402122d4SMatthias Ringwald * 274402122d4SMatthias Ringwald * @text As with the packet and the heartbeat handlers, the combined app setup contains the code from the individual example setups. 275402122d4SMatthias Ringwald */ 276bcf00d8fSMatthias Ringwald 277bcf00d8fSMatthias Ringwald 278402122d4SMatthias Ringwald /* LISTING_START(MainConfiguration): Init L2CAP RFCOMM SDP SPP */ 279402122d4SMatthias Ringwald int btstack_main(int argc, const char * argv[]) 280402122d4SMatthias Ringwald { 2819ec2630cSMatthias Ringwald (void)argc; 2829ec2630cSMatthias Ringwald (void)argv; 283bcf00d8fSMatthias Ringwald 284bcf00d8fSMatthias Ringwald l2cap_init(); 285bcf00d8fSMatthias Ringwald 286402122d4SMatthias Ringwald rfcomm_init(); 287402122d4SMatthias Ringwald rfcomm_register_service(packet_handler, RFCOMM_SERVER_CHANNEL, 0xffff); 288402122d4SMatthias Ringwald 289278494d6SMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE_FOR_RFCOMM 2908cef84f3SMatthias Ringwald // setup ERTM management 2918cef84f3SMatthias Ringwald rfcomm_enable_l2cap_ertm(&rfcomm_ertm_request_handler, &rfcomm_ertm_released_handler); 2928cef84f3SMatthias Ringwald #endif 2938cef84f3SMatthias Ringwald 294402122d4SMatthias Ringwald // init SDP, create record for SPP and register with SDP 295402122d4SMatthias Ringwald sdp_init(); 296402122d4SMatthias Ringwald memset(spp_service_buffer, 0, sizeof(spp_service_buffer)); 297402122d4SMatthias Ringwald spp_create_sdp_record(spp_service_buffer, 0x10001, RFCOMM_SERVER_CHANNEL, "SPP Streamer"); 298402122d4SMatthias Ringwald sdp_register_service(spp_service_buffer); 299402122d4SMatthias Ringwald // printf("SDP service record size: %u\n", de_get_len(spp_service_buffer)); 300402122d4SMatthias Ringwald 301a4fe6467SMatthias Ringwald // register for HCI events 302a4fe6467SMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 303a4fe6467SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 304a4fe6467SMatthias Ringwald 305402122d4SMatthias Ringwald // short-cut to find other SPP Streamer 306402122d4SMatthias Ringwald gap_set_class_of_device(TEST_COD); 307402122d4SMatthias Ringwald 3080c2b8870SMatthias Ringwald gap_ssp_set_io_capability(SSP_IO_CAPABILITY_DISPLAY_YES_NO); 3090c2b8870SMatthias Ringwald gap_set_local_name("SPP Streamer 00:00:00:00:00:00"); 310402122d4SMatthias Ringwald gap_discoverable_control(1); 311402122d4SMatthias Ringwald 312402122d4SMatthias Ringwald spp_create_test_data(); 313402122d4SMatthias Ringwald 314bcf00d8fSMatthias Ringwald // turn on! 315bcf00d8fSMatthias Ringwald hci_power_control(HCI_POWER_ON); 316bcf00d8fSMatthias Ringwald 317bcf00d8fSMatthias Ringwald return 0; 318bcf00d8fSMatthias Ringwald } 319402122d4SMatthias Ringwald /* LISTING_END */ 320402122d4SMatthias Ringwald /* EXAMPLE_END */ 321