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 38*15de8206SMilanka Ringwald #define __BTSTACK_FILE__ "spp_streamer.c" 39*15de8206SMilanka Ringwald 40*15de8206SMilanka Ringwald /* 41*15de8206SMilanka Ringwald * spp_streamer.c 42*15de8206SMilanka Ringwald */ 43*15de8206SMilanka Ringwald 44bcf00d8fSMatthias Ringwald // ***************************************************************************** 45402122d4SMatthias Ringwald /* EXAMPLE_START(spp_streamer): Send test data via SPP as fast as possible 46*15de8206SMilanka 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(). 49402122d4SMatthias Ringwald * When we get the RFCOMM_EVENT_CAN_SEND_NOW, send data and request another one. 50402122d4SMatthias Ringwald */ 51bcf00d8fSMatthias Ringwald // ***************************************************************************** 52bcf00d8fSMatthias Ringwald 53bcf00d8fSMatthias Ringwald #include <stdint.h> 54bcf00d8fSMatthias Ringwald #include <stdio.h> 55bcf00d8fSMatthias Ringwald #include <stdlib.h> 56bcf00d8fSMatthias Ringwald #include <string.h> 57402122d4SMatthias Ringwald #include <inttypes.h> 58bcf00d8fSMatthias Ringwald 59235946f1SMatthias Ringwald #include "btstack.h" 60bcf00d8fSMatthias Ringwald 61402122d4SMatthias Ringwald int btstack_main(int argc, const char * argv[]); 62bcf00d8fSMatthias Ringwald 63402122d4SMatthias Ringwald #define RFCOMM_SERVER_CHANNEL 1 64402122d4SMatthias Ringwald 65402122d4SMatthias Ringwald #define TEST_COD 0x1234 66bcf00d8fSMatthias Ringwald #define NUM_ROWS 25 67bcf00d8fSMatthias Ringwald #define NUM_COLS 40 68369c1a52SMatthias Ringwald #define DATA_VOLUME (10 * 1000 * 1000) 69bcf00d8fSMatthias Ringwald 70402122d4SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 71bcf00d8fSMatthias Ringwald 72bcf00d8fSMatthias Ringwald static uint8_t test_data[NUM_ROWS * NUM_COLS]; 73402122d4SMatthias Ringwald 74402122d4SMatthias Ringwald // SPP 75402122d4SMatthias Ringwald static uint8_t spp_service_buffer[150]; 76402122d4SMatthias Ringwald 77402122d4SMatthias Ringwald static uint16_t spp_test_data_len; 78402122d4SMatthias Ringwald static uint16_t rfcomm_mtu; 79bcf00d8fSMatthias Ringwald static uint16_t rfcomm_cid = 0; 80402122d4SMatthias Ringwald // static uint32_t data_to_send = DATA_VOLUME; 81bcf00d8fSMatthias Ringwald 82369c1a52SMatthias Ringwald /* 83369c1a52SMatthias Ringwald * @section Track throughput 84369c1a52SMatthias Ringwald * @text We calculate the throughput by setting a start time and measuring the amount of 85369c1a52SMatthias Ringwald * data sent. After a configurable REPORT_INTERVAL_MS, we print the throughput in kB/s 86369c1a52SMatthias Ringwald * and reset the counter and start time. 87369c1a52SMatthias Ringwald */ 88369c1a52SMatthias Ringwald 89369c1a52SMatthias Ringwald /* LISTING_START(tracking): Tracking throughput */ 90369c1a52SMatthias Ringwald #define REPORT_INTERVAL_MS 3000 91402122d4SMatthias Ringwald static uint32_t test_data_transferred; 92369c1a52SMatthias Ringwald static uint32_t test_data_start; 93369c1a52SMatthias Ringwald 94369c1a52SMatthias Ringwald static void test_reset(void){ 95369c1a52SMatthias Ringwald test_data_start = btstack_run_loop_get_time_ms(); 96402122d4SMatthias Ringwald test_data_transferred = 0; 97369c1a52SMatthias Ringwald } 98369c1a52SMatthias Ringwald 99402122d4SMatthias Ringwald static void test_track_transferred(int bytes_sent){ 100402122d4SMatthias Ringwald test_data_transferred += bytes_sent; 101369c1a52SMatthias Ringwald // evaluate 102369c1a52SMatthias Ringwald uint32_t now = btstack_run_loop_get_time_ms(); 103369c1a52SMatthias Ringwald uint32_t time_passed = now - test_data_start; 104369c1a52SMatthias Ringwald if (time_passed < REPORT_INTERVAL_MS) return; 105369c1a52SMatthias Ringwald // print speed 106402122d4SMatthias Ringwald int bytes_per_second = test_data_transferred * 1000 / time_passed; 107402122d4SMatthias Ringwald printf("%u bytes -> %u.%03u kB/s\n", (int) test_data_transferred, (int) bytes_per_second / 1000, bytes_per_second % 1000); 108369c1a52SMatthias Ringwald 109369c1a52SMatthias Ringwald // restart 110369c1a52SMatthias Ringwald test_data_start = now; 111402122d4SMatthias Ringwald test_data_transferred = 0; 112369c1a52SMatthias Ringwald } 113369c1a52SMatthias Ringwald /* LISTING_END(tracking): Tracking throughput */ 114369c1a52SMatthias Ringwald 115369c1a52SMatthias Ringwald 116402122d4SMatthias Ringwald static void spp_create_test_data(void){ 117bcf00d8fSMatthias Ringwald int x,y; 118bcf00d8fSMatthias Ringwald for (y=0;y<NUM_ROWS;y++){ 119bcf00d8fSMatthias Ringwald for (x=0;x<NUM_COLS-2;x++){ 120bcf00d8fSMatthias Ringwald test_data[y*NUM_COLS+x] = '0' + (x % 10); 121bcf00d8fSMatthias Ringwald } 122bcf00d8fSMatthias Ringwald test_data[y*NUM_COLS+NUM_COLS-2] = '\n'; 123bcf00d8fSMatthias Ringwald test_data[y*NUM_COLS+NUM_COLS-1] = '\r'; 124bcf00d8fSMatthias Ringwald } 125bcf00d8fSMatthias Ringwald } 126bcf00d8fSMatthias Ringwald 127402122d4SMatthias Ringwald static void spp_send_packet(void){ 128402122d4SMatthias Ringwald rfcomm_send(rfcomm_cid, (uint8_t*) test_data, spp_test_data_len); 1296dcd2e77SMatthias Ringwald 130402122d4SMatthias Ringwald test_track_transferred(spp_test_data_len); 131402122d4SMatthias Ringwald #if 0 132402122d4SMatthias Ringwald if (data_to_send <= spp_test_data_len){ 1336dcd2e77SMatthias Ringwald printf("SPP Streamer: enough data send, closing channel\n"); 134bcf00d8fSMatthias Ringwald rfcomm_disconnect(rfcomm_cid); 135bcf00d8fSMatthias Ringwald rfcomm_cid = 0; 136bcf00d8fSMatthias Ringwald return; 137bcf00d8fSMatthias Ringwald } 138402122d4SMatthias Ringwald data_to_send -= spp_test_data_len; 139402122d4SMatthias Ringwald #endif 1405f27b1a1SMatthias Ringwald rfcomm_request_can_send_now_event(rfcomm_cid); 141369c1a52SMatthias Ringwald } 142bcf00d8fSMatthias Ringwald 143402122d4SMatthias Ringwald /* 144402122d4SMatthias Ringwald * @section Packet Handler 145402122d4SMatthias Ringwald * 146402122d4SMatthias Ringwald * @text The packet handler of the combined example is just the combination of the individual packet handlers. 147402122d4SMatthias Ringwald */ 148402122d4SMatthias Ringwald 149bcf00d8fSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1509ec2630cSMatthias Ringwald UNUSED(channel); 1519ec2630cSMatthias Ringwald 152402122d4SMatthias Ringwald bd_addr_t event_addr; 153402122d4SMatthias Ringwald uint8_t rfcomm_channel_nr; 154402122d4SMatthias Ringwald 155402122d4SMatthias Ringwald switch (packet_type) { 156402122d4SMatthias Ringwald case HCI_EVENT_PACKET: 157402122d4SMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 158402122d4SMatthias Ringwald 159402122d4SMatthias Ringwald case HCI_EVENT_PIN_CODE_REQUEST: 160402122d4SMatthias Ringwald // inform about pin code request 161402122d4SMatthias Ringwald printf("Pin code request - using '0000'\n"); 162402122d4SMatthias Ringwald hci_event_pin_code_request_get_bd_addr(packet, event_addr); 163402122d4SMatthias Ringwald gap_pin_code_response(event_addr, "0000"); 164bcf00d8fSMatthias Ringwald break; 165402122d4SMatthias Ringwald 166402122d4SMatthias Ringwald case HCI_EVENT_USER_CONFIRMATION_REQUEST: 167402122d4SMatthias Ringwald // inform about user confirmation request 168402122d4SMatthias Ringwald printf("SSP User Confirmation Request with numeric value '%06"PRIu32"'\n", little_endian_read_32(packet, 8)); 169402122d4SMatthias Ringwald printf("SSP User Confirmation Auto accept\n"); 170402122d4SMatthias Ringwald break; 171402122d4SMatthias Ringwald 172402122d4SMatthias Ringwald case RFCOMM_EVENT_INCOMING_CONNECTION: 173402122d4SMatthias Ringwald // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16) 174402122d4SMatthias Ringwald rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr); 175402122d4SMatthias Ringwald rfcomm_channel_nr = rfcomm_event_incoming_connection_get_server_channel(packet); 176402122d4SMatthias Ringwald rfcomm_cid = rfcomm_event_incoming_connection_get_rfcomm_cid(packet); 177402122d4SMatthias Ringwald printf("RFCOMM channel %u requested for %s\n", rfcomm_channel_nr, bd_addr_to_str(event_addr)); 178402122d4SMatthias Ringwald rfcomm_accept_connection(rfcomm_cid); 179402122d4SMatthias Ringwald break; 180402122d4SMatthias Ringwald 181f8f6a918SMatthias Ringwald case RFCOMM_EVENT_CHANNEL_OPENED: 182402122d4SMatthias Ringwald // data: event(8), len(8), status (8), address (48), server channel(8), rfcomm_cid(16), max frame size(16) 183402122d4SMatthias Ringwald if (rfcomm_event_channel_opened_get_status(packet)) { 184402122d4SMatthias Ringwald printf("RFCOMM channel open failed, status %u\n", rfcomm_event_channel_opened_get_status(packet)); 185bcf00d8fSMatthias Ringwald } else { 186402122d4SMatthias Ringwald rfcomm_cid = rfcomm_event_channel_opened_get_rfcomm_cid(packet); 187402122d4SMatthias Ringwald rfcomm_mtu = rfcomm_event_channel_opened_get_max_frame_size(packet); 188402122d4SMatthias Ringwald printf("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u\n", rfcomm_cid, rfcomm_mtu); 189402122d4SMatthias Ringwald 190402122d4SMatthias Ringwald spp_test_data_len = rfcomm_mtu; 191402122d4SMatthias Ringwald if (spp_test_data_len > sizeof(test_data)){ 192402122d4SMatthias Ringwald spp_test_data_len = sizeof(test_data); 193bcf00d8fSMatthias Ringwald } 194402122d4SMatthias Ringwald 1955aed585bSMatthias Ringwald // disable page/inquiry scan to get max performance 1965aed585bSMatthias Ringwald gap_discoverable_control(0); 1975aed585bSMatthias Ringwald gap_connectable_control(0); 1985aed585bSMatthias Ringwald 199369c1a52SMatthias Ringwald test_reset(); 2005f27b1a1SMatthias Ringwald rfcomm_request_can_send_now_event(rfcomm_cid); 201bcf00d8fSMatthias Ringwald } 202bcf00d8fSMatthias Ringwald break; 203402122d4SMatthias Ringwald 204bcf00d8fSMatthias Ringwald case RFCOMM_EVENT_CAN_SEND_NOW: 205402122d4SMatthias Ringwald spp_send_packet(); 206bcf00d8fSMatthias Ringwald break; 207402122d4SMatthias Ringwald 2081193857eSMatthias Ringwald case RFCOMM_EVENT_CHANNEL_CLOSED: 209402122d4SMatthias Ringwald printf("RFCOMM channel closed\n"); 210402122d4SMatthias Ringwald rfcomm_cid = 0; 2115aed585bSMatthias Ringwald 2125aed585bSMatthias Ringwald // re-enable page/inquiry scan again 2135aed585bSMatthias Ringwald gap_discoverable_control(1); 2145aed585bSMatthias Ringwald gap_connectable_control(1); 215402122d4SMatthias Ringwald break; 216402122d4SMatthias Ringwald 217402122d4SMatthias Ringwald default: 218402122d4SMatthias Ringwald break; 2191193857eSMatthias Ringwald } 2201193857eSMatthias Ringwald break; 221402122d4SMatthias Ringwald 222402122d4SMatthias Ringwald case RFCOMM_DATA_PACKET: 223402122d4SMatthias Ringwald test_track_transferred(size); 224402122d4SMatthias Ringwald #if 0 225402122d4SMatthias Ringwald printf("RCV: '"); 226402122d4SMatthias Ringwald for (i=0;i<size;i++){ 227402122d4SMatthias Ringwald putchar(packet[i]); 228402122d4SMatthias Ringwald } 229402122d4SMatthias Ringwald printf("'\n"); 230402122d4SMatthias Ringwald #endif 231402122d4SMatthias Ringwald break; 232402122d4SMatthias Ringwald 233bcf00d8fSMatthias Ringwald default: 234bcf00d8fSMatthias Ringwald break; 235bcf00d8fSMatthias Ringwald } 236bcf00d8fSMatthias Ringwald } 237bcf00d8fSMatthias Ringwald 238402122d4SMatthias Ringwald /* 239402122d4SMatthias Ringwald * @section Main Application Setup 240402122d4SMatthias Ringwald * 241402122d4SMatthias Ringwald * @text As with the packet and the heartbeat handlers, the combined app setup contains the code from the individual example setups. 242402122d4SMatthias Ringwald */ 243bcf00d8fSMatthias Ringwald 244bcf00d8fSMatthias Ringwald 245402122d4SMatthias Ringwald /* LISTING_START(MainConfiguration): Init L2CAP RFCOMM SDP SPP */ 246402122d4SMatthias Ringwald int btstack_main(int argc, const char * argv[]) 247402122d4SMatthias Ringwald { 2489ec2630cSMatthias Ringwald (void)argc; 2499ec2630cSMatthias Ringwald (void)argv; 250bcf00d8fSMatthias Ringwald 251bcf00d8fSMatthias Ringwald // register for HCI events 252bcf00d8fSMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 253bcf00d8fSMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 254bcf00d8fSMatthias Ringwald 255bcf00d8fSMatthias Ringwald l2cap_init(); 256bcf00d8fSMatthias Ringwald 257402122d4SMatthias Ringwald rfcomm_init(); 258402122d4SMatthias Ringwald rfcomm_register_service(packet_handler, RFCOMM_SERVER_CHANNEL, 0xffff); 259402122d4SMatthias Ringwald 260402122d4SMatthias Ringwald // init SDP, create record for SPP and register with SDP 261402122d4SMatthias Ringwald sdp_init(); 262402122d4SMatthias Ringwald memset(spp_service_buffer, 0, sizeof(spp_service_buffer)); 263402122d4SMatthias Ringwald spp_create_sdp_record(spp_service_buffer, 0x10001, RFCOMM_SERVER_CHANNEL, "SPP Streamer"); 264402122d4SMatthias Ringwald sdp_register_service(spp_service_buffer); 265402122d4SMatthias Ringwald // printf("SDP service record size: %u\n", de_get_len(spp_service_buffer)); 266402122d4SMatthias Ringwald 267402122d4SMatthias Ringwald // short-cut to find other SPP Streamer 268402122d4SMatthias Ringwald gap_set_class_of_device(TEST_COD); 269402122d4SMatthias Ringwald 2700c2b8870SMatthias Ringwald gap_ssp_set_io_capability(SSP_IO_CAPABILITY_DISPLAY_YES_NO); 2710c2b8870SMatthias Ringwald gap_set_local_name("SPP Streamer 00:00:00:00:00:00"); 272402122d4SMatthias Ringwald gap_discoverable_control(1); 273402122d4SMatthias Ringwald 274402122d4SMatthias Ringwald spp_create_test_data(); 275402122d4SMatthias Ringwald 276bcf00d8fSMatthias Ringwald // turn on! 277bcf00d8fSMatthias Ringwald hci_power_control(HCI_POWER_ON); 278bcf00d8fSMatthias Ringwald 279bcf00d8fSMatthias Ringwald return 0; 280bcf00d8fSMatthias Ringwald } 281402122d4SMatthias Ringwald /* LISTING_END */ 282402122d4SMatthias Ringwald /* EXAMPLE_END */ 283