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 38bcf00d8fSMatthias Ringwald // ***************************************************************************** 39402122d4SMatthias Ringwald /* EXAMPLE_START(spp_streamer): Send test data via SPP as fast as possible 40402122d4SMatthias Ringwald * @text After RFCOMM connections gets open, request a 41402122d4SMatthias Ringwald * RFCOMM_EVENT_CAN_SEND_NOW via rfcomm_request_can_send_now_event(). 42402122d4SMatthias Ringwald * When we get the RFCOMM_EVENT_CAN_SEND_NOW, send data and request another one. 43402122d4SMatthias Ringwald */ 44bcf00d8fSMatthias Ringwald // ***************************************************************************** 45bcf00d8fSMatthias Ringwald 46bcf00d8fSMatthias Ringwald #include <stdint.h> 47bcf00d8fSMatthias Ringwald #include <stdio.h> 48bcf00d8fSMatthias Ringwald #include <stdlib.h> 49bcf00d8fSMatthias Ringwald #include <string.h> 50402122d4SMatthias Ringwald #include <inttypes.h> 51bcf00d8fSMatthias Ringwald 52235946f1SMatthias Ringwald #include "btstack.h" 53bcf00d8fSMatthias Ringwald 54402122d4SMatthias Ringwald int btstack_main(int argc, const char * argv[]); 55bcf00d8fSMatthias Ringwald 56402122d4SMatthias Ringwald #define RFCOMM_SERVER_CHANNEL 1 57402122d4SMatthias Ringwald 58402122d4SMatthias Ringwald #define TEST_COD 0x1234 59bcf00d8fSMatthias Ringwald #define NUM_ROWS 25 60bcf00d8fSMatthias Ringwald #define NUM_COLS 40 61369c1a52SMatthias Ringwald #define DATA_VOLUME (10 * 1000 * 1000) 62bcf00d8fSMatthias Ringwald 63402122d4SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 64bcf00d8fSMatthias Ringwald 65bcf00d8fSMatthias Ringwald static uint8_t test_data[NUM_ROWS * NUM_COLS]; 66402122d4SMatthias Ringwald 67402122d4SMatthias Ringwald // SPP 68402122d4SMatthias Ringwald static uint8_t spp_service_buffer[150]; 69402122d4SMatthias Ringwald 70402122d4SMatthias Ringwald static uint16_t spp_test_data_len; 71402122d4SMatthias Ringwald static uint16_t rfcomm_mtu; 72bcf00d8fSMatthias Ringwald static uint16_t rfcomm_cid = 0; 73402122d4SMatthias Ringwald // static uint32_t data_to_send = DATA_VOLUME; 74bcf00d8fSMatthias Ringwald 75369c1a52SMatthias Ringwald /* 76369c1a52SMatthias Ringwald * @section Track throughput 77369c1a52SMatthias Ringwald * @text We calculate the throughput by setting a start time and measuring the amount of 78369c1a52SMatthias Ringwald * data sent. After a configurable REPORT_INTERVAL_MS, we print the throughput in kB/s 79369c1a52SMatthias Ringwald * and reset the counter and start time. 80369c1a52SMatthias Ringwald */ 81369c1a52SMatthias Ringwald 82369c1a52SMatthias Ringwald /* LISTING_START(tracking): Tracking throughput */ 83369c1a52SMatthias Ringwald #define REPORT_INTERVAL_MS 3000 84402122d4SMatthias Ringwald static uint32_t test_data_transferred; 85369c1a52SMatthias Ringwald static uint32_t test_data_start; 86369c1a52SMatthias Ringwald 87369c1a52SMatthias Ringwald static void test_reset(void){ 88369c1a52SMatthias Ringwald test_data_start = btstack_run_loop_get_time_ms(); 89402122d4SMatthias Ringwald test_data_transferred = 0; 90369c1a52SMatthias Ringwald } 91369c1a52SMatthias Ringwald 92402122d4SMatthias Ringwald static void test_track_transferred(int bytes_sent){ 93402122d4SMatthias Ringwald test_data_transferred += bytes_sent; 94369c1a52SMatthias Ringwald // evaluate 95369c1a52SMatthias Ringwald uint32_t now = btstack_run_loop_get_time_ms(); 96369c1a52SMatthias Ringwald uint32_t time_passed = now - test_data_start; 97369c1a52SMatthias Ringwald if (time_passed < REPORT_INTERVAL_MS) return; 98369c1a52SMatthias Ringwald // print speed 99402122d4SMatthias Ringwald int bytes_per_second = test_data_transferred * 1000 / time_passed; 100402122d4SMatthias Ringwald printf("%u bytes -> %u.%03u kB/s\n", (int) test_data_transferred, (int) bytes_per_second / 1000, bytes_per_second % 1000); 101369c1a52SMatthias Ringwald 102369c1a52SMatthias Ringwald // restart 103369c1a52SMatthias Ringwald test_data_start = now; 104402122d4SMatthias Ringwald test_data_transferred = 0; 105369c1a52SMatthias Ringwald } 106369c1a52SMatthias Ringwald /* LISTING_END(tracking): Tracking throughput */ 107369c1a52SMatthias Ringwald 108369c1a52SMatthias Ringwald 109402122d4SMatthias Ringwald static void spp_create_test_data(void){ 110bcf00d8fSMatthias Ringwald int x,y; 111bcf00d8fSMatthias Ringwald for (y=0;y<NUM_ROWS;y++){ 112bcf00d8fSMatthias Ringwald for (x=0;x<NUM_COLS-2;x++){ 113bcf00d8fSMatthias Ringwald test_data[y*NUM_COLS+x] = '0' + (x % 10); 114bcf00d8fSMatthias Ringwald } 115bcf00d8fSMatthias Ringwald test_data[y*NUM_COLS+NUM_COLS-2] = '\n'; 116bcf00d8fSMatthias Ringwald test_data[y*NUM_COLS+NUM_COLS-1] = '\r'; 117bcf00d8fSMatthias Ringwald } 118bcf00d8fSMatthias Ringwald } 119bcf00d8fSMatthias Ringwald 120402122d4SMatthias Ringwald static void spp_send_packet(void){ 121402122d4SMatthias Ringwald rfcomm_send(rfcomm_cid, (uint8_t*) test_data, spp_test_data_len); 1226dcd2e77SMatthias Ringwald 123402122d4SMatthias Ringwald test_track_transferred(spp_test_data_len); 124402122d4SMatthias Ringwald #if 0 125402122d4SMatthias Ringwald if (data_to_send <= spp_test_data_len){ 1266dcd2e77SMatthias Ringwald printf("SPP Streamer: enough data send, closing channel\n"); 127bcf00d8fSMatthias Ringwald rfcomm_disconnect(rfcomm_cid); 128bcf00d8fSMatthias Ringwald rfcomm_cid = 0; 129bcf00d8fSMatthias Ringwald return; 130bcf00d8fSMatthias Ringwald } 131402122d4SMatthias Ringwald data_to_send -= spp_test_data_len; 132402122d4SMatthias Ringwald #endif 1335f27b1a1SMatthias Ringwald rfcomm_request_can_send_now_event(rfcomm_cid); 134369c1a52SMatthias Ringwald } 135bcf00d8fSMatthias Ringwald 136402122d4SMatthias Ringwald /* 137402122d4SMatthias Ringwald * @section Packet Handler 138402122d4SMatthias Ringwald * 139402122d4SMatthias Ringwald * @text The packet handler of the combined example is just the combination of the individual packet handlers. 140402122d4SMatthias Ringwald */ 141402122d4SMatthias Ringwald 142bcf00d8fSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1439ec2630cSMatthias Ringwald UNUSED(channel); 1449ec2630cSMatthias Ringwald 145402122d4SMatthias Ringwald bd_addr_t event_addr; 146402122d4SMatthias Ringwald uint8_t rfcomm_channel_nr; 147402122d4SMatthias Ringwald 148402122d4SMatthias Ringwald switch (packet_type) { 149402122d4SMatthias Ringwald case HCI_EVENT_PACKET: 150402122d4SMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 151402122d4SMatthias Ringwald 152402122d4SMatthias Ringwald case HCI_EVENT_PIN_CODE_REQUEST: 153402122d4SMatthias Ringwald // inform about pin code request 154402122d4SMatthias Ringwald printf("Pin code request - using '0000'\n"); 155402122d4SMatthias Ringwald hci_event_pin_code_request_get_bd_addr(packet, event_addr); 156402122d4SMatthias Ringwald gap_pin_code_response(event_addr, "0000"); 157bcf00d8fSMatthias Ringwald break; 158402122d4SMatthias Ringwald 159402122d4SMatthias Ringwald case HCI_EVENT_USER_CONFIRMATION_REQUEST: 160402122d4SMatthias Ringwald // inform about user confirmation request 161402122d4SMatthias Ringwald printf("SSP User Confirmation Request with numeric value '%06"PRIu32"'\n", little_endian_read_32(packet, 8)); 162402122d4SMatthias Ringwald printf("SSP User Confirmation Auto accept\n"); 163402122d4SMatthias Ringwald break; 164402122d4SMatthias Ringwald 165402122d4SMatthias Ringwald case RFCOMM_EVENT_INCOMING_CONNECTION: 166402122d4SMatthias Ringwald // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16) 167402122d4SMatthias Ringwald rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr); 168402122d4SMatthias Ringwald rfcomm_channel_nr = rfcomm_event_incoming_connection_get_server_channel(packet); 169402122d4SMatthias Ringwald rfcomm_cid = rfcomm_event_incoming_connection_get_rfcomm_cid(packet); 170402122d4SMatthias Ringwald printf("RFCOMM channel %u requested for %s\n", rfcomm_channel_nr, bd_addr_to_str(event_addr)); 171402122d4SMatthias Ringwald rfcomm_accept_connection(rfcomm_cid); 172402122d4SMatthias Ringwald break; 173402122d4SMatthias Ringwald 174f8f6a918SMatthias Ringwald case RFCOMM_EVENT_CHANNEL_OPENED: 175402122d4SMatthias Ringwald // data: event(8), len(8), status (8), address (48), server channel(8), rfcomm_cid(16), max frame size(16) 176402122d4SMatthias Ringwald if (rfcomm_event_channel_opened_get_status(packet)) { 177402122d4SMatthias Ringwald printf("RFCOMM channel open failed, status %u\n", rfcomm_event_channel_opened_get_status(packet)); 178bcf00d8fSMatthias Ringwald } else { 179402122d4SMatthias Ringwald rfcomm_cid = rfcomm_event_channel_opened_get_rfcomm_cid(packet); 180402122d4SMatthias Ringwald rfcomm_mtu = rfcomm_event_channel_opened_get_max_frame_size(packet); 181402122d4SMatthias Ringwald printf("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u\n", rfcomm_cid, rfcomm_mtu); 182402122d4SMatthias Ringwald 183402122d4SMatthias Ringwald spp_test_data_len = rfcomm_mtu; 184402122d4SMatthias Ringwald if (spp_test_data_len > sizeof(test_data)){ 185402122d4SMatthias Ringwald spp_test_data_len = sizeof(test_data); 186bcf00d8fSMatthias Ringwald } 187402122d4SMatthias Ringwald 188*5aed585bSMatthias Ringwald // disable page/inquiry scan to get max performance 189*5aed585bSMatthias Ringwald gap_discoverable_control(0); 190*5aed585bSMatthias Ringwald gap_connectable_control(0); 191*5aed585bSMatthias Ringwald 192369c1a52SMatthias Ringwald test_reset(); 1935f27b1a1SMatthias Ringwald rfcomm_request_can_send_now_event(rfcomm_cid); 194bcf00d8fSMatthias Ringwald } 195bcf00d8fSMatthias Ringwald break; 196402122d4SMatthias Ringwald 197bcf00d8fSMatthias Ringwald case RFCOMM_EVENT_CAN_SEND_NOW: 198402122d4SMatthias Ringwald spp_send_packet(); 199bcf00d8fSMatthias Ringwald break; 200402122d4SMatthias Ringwald 2011193857eSMatthias Ringwald case RFCOMM_EVENT_CHANNEL_CLOSED: 202402122d4SMatthias Ringwald printf("RFCOMM channel closed\n"); 203402122d4SMatthias Ringwald rfcomm_cid = 0; 204*5aed585bSMatthias Ringwald 205*5aed585bSMatthias Ringwald // re-enable page/inquiry scan again 206*5aed585bSMatthias Ringwald gap_discoverable_control(1); 207*5aed585bSMatthias Ringwald gap_connectable_control(1); 208402122d4SMatthias Ringwald break; 209402122d4SMatthias Ringwald 210402122d4SMatthias Ringwald default: 211402122d4SMatthias Ringwald break; 2121193857eSMatthias Ringwald } 2131193857eSMatthias Ringwald break; 214402122d4SMatthias Ringwald 215402122d4SMatthias Ringwald case RFCOMM_DATA_PACKET: 216402122d4SMatthias Ringwald test_track_transferred(size); 217402122d4SMatthias Ringwald #if 0 218402122d4SMatthias Ringwald printf("RCV: '"); 219402122d4SMatthias Ringwald for (i=0;i<size;i++){ 220402122d4SMatthias Ringwald putchar(packet[i]); 221402122d4SMatthias Ringwald } 222402122d4SMatthias Ringwald printf("'\n"); 223402122d4SMatthias Ringwald #endif 224402122d4SMatthias Ringwald break; 225402122d4SMatthias Ringwald 226bcf00d8fSMatthias Ringwald default: 227bcf00d8fSMatthias Ringwald break; 228bcf00d8fSMatthias Ringwald } 229bcf00d8fSMatthias Ringwald } 230bcf00d8fSMatthias Ringwald 231402122d4SMatthias Ringwald /* 232402122d4SMatthias Ringwald * @section Main Application Setup 233402122d4SMatthias Ringwald * 234402122d4SMatthias Ringwald * @text As with the packet and the heartbeat handlers, the combined app setup contains the code from the individual example setups. 235402122d4SMatthias Ringwald */ 236bcf00d8fSMatthias Ringwald 237bcf00d8fSMatthias Ringwald 238402122d4SMatthias Ringwald /* LISTING_START(MainConfiguration): Init L2CAP RFCOMM SDP SPP */ 239402122d4SMatthias Ringwald int btstack_main(int argc, const char * argv[]) 240402122d4SMatthias Ringwald { 2419ec2630cSMatthias Ringwald (void)argc; 2429ec2630cSMatthias Ringwald (void)argv; 243bcf00d8fSMatthias Ringwald 244bcf00d8fSMatthias Ringwald // register for HCI events 245bcf00d8fSMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 246bcf00d8fSMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 247bcf00d8fSMatthias Ringwald 248bcf00d8fSMatthias Ringwald l2cap_init(); 249bcf00d8fSMatthias Ringwald 250402122d4SMatthias Ringwald rfcomm_init(); 251402122d4SMatthias Ringwald rfcomm_register_service(packet_handler, RFCOMM_SERVER_CHANNEL, 0xffff); 252402122d4SMatthias Ringwald 253402122d4SMatthias Ringwald // init SDP, create record for SPP and register with SDP 254402122d4SMatthias Ringwald sdp_init(); 255402122d4SMatthias Ringwald memset(spp_service_buffer, 0, sizeof(spp_service_buffer)); 256402122d4SMatthias Ringwald spp_create_sdp_record(spp_service_buffer, 0x10001, RFCOMM_SERVER_CHANNEL, "SPP Streamer"); 257402122d4SMatthias Ringwald sdp_register_service(spp_service_buffer); 258402122d4SMatthias Ringwald // printf("SDP service record size: %u\n", de_get_len(spp_service_buffer)); 259402122d4SMatthias Ringwald 260402122d4SMatthias Ringwald gap_ssp_set_io_capability(SSP_IO_CAPABILITY_DISPLAY_YES_NO); 261402122d4SMatthias Ringwald 262402122d4SMatthias Ringwald // short-cut to find other SPP Streamer 263402122d4SMatthias Ringwald gap_set_class_of_device(TEST_COD); 264402122d4SMatthias Ringwald 265402122d4SMatthias Ringwald gap_discoverable_control(1); 266402122d4SMatthias Ringwald 267402122d4SMatthias Ringwald spp_create_test_data(); 268402122d4SMatthias Ringwald 269bcf00d8fSMatthias Ringwald // turn on! 270bcf00d8fSMatthias Ringwald hci_power_control(HCI_POWER_ON); 271bcf00d8fSMatthias Ringwald 272bcf00d8fSMatthias Ringwald return 0; 273bcf00d8fSMatthias Ringwald } 274402122d4SMatthias Ringwald /* LISTING_END */ 275402122d4SMatthias Ringwald /* EXAMPLE_END */ 276