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 38cee62800SMatthias Ringwald // ***************************************************************************** 39cee62800SMatthias Ringwald /* EXAMPLE_START(spp_streamer_client): Client for SPP Streamer 40cee62800SMatthias Ringwald * 41cee62800SMatthias Ringwald * @text The SPP Streamer Clients connects to SPP Streamer and tracks the 42cee62800SMatthias Ringwald * amount of received data 43cee62800SMatthias Ringwald */ 44cee62800SMatthias Ringwald // ***************************************************************************** 45cee62800SMatthias Ringwald 46cee62800SMatthias Ringwald #include <stdint.h> 47cee62800SMatthias Ringwald #include <stdio.h> 48cee62800SMatthias Ringwald #include <stdlib.h> 49cee62800SMatthias Ringwald #include <string.h> 50cee62800SMatthias Ringwald #include <inttypes.h> 51cee62800SMatthias Ringwald 52cee62800SMatthias Ringwald #include "btstack.h" 53cee62800SMatthias Ringwald 54cee62800SMatthias Ringwald #define RFCOMM_SERVER_CHANNEL 1 55cee62800SMatthias Ringwald 56cee62800SMatthias Ringwald #define TEST_COD 0x1234 57cee62800SMatthias Ringwald 58cee62800SMatthias Ringwald typedef enum { 59cee62800SMatthias Ringwald // SPP 60cee62800SMatthias Ringwald W4_PEER_COD, 61cee62800SMatthias Ringwald W4_SCAN_COMPLETE, 62cee62800SMatthias Ringwald W4_SDP_RESULT, 63cee62800SMatthias Ringwald W4_SDP_COMPLETE, 64cee62800SMatthias Ringwald W4_RFCOMM_CHANNEL, 65cee62800SMatthias Ringwald SENDING, 66cee62800SMatthias Ringwald DONE 67cee62800SMatthias Ringwald } state_t; 68cee62800SMatthias Ringwald 69cee62800SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 70cee62800SMatthias Ringwald 71cee62800SMatthias Ringwald static bd_addr_t peer_addr; 72cee62800SMatthias Ringwald static state_t state;; 73cee62800SMatthias Ringwald 74cee62800SMatthias Ringwald // SPP 75cee62800SMatthias Ringwald static uint16_t rfcomm_mtu; 76cee62800SMatthias Ringwald static uint16_t rfcomm_cid = 0; 77cee62800SMatthias Ringwald // static uint32_t data_to_send = DATA_VOLUME; 78cee62800SMatthias Ringwald 79cee62800SMatthias Ringwald 80cee62800SMatthias Ringwald /** 81cee62800SMatthias Ringwald * Find remote peer by COD 82cee62800SMatthias Ringwald */ 83cee62800SMatthias Ringwald #define INQUIRY_INTERVAL 5 84cee62800SMatthias Ringwald static void start_scan(void){ 85cee62800SMatthias Ringwald printf("Starting inquiry scan..\n"); 86cee62800SMatthias Ringwald state = W4_PEER_COD; 87287c758dSMatthias Ringwald gap_inquiry_start(INQUIRY_INTERVAL); 88cee62800SMatthias Ringwald } 89cee62800SMatthias Ringwald static void stop_scan(void){ 90cee62800SMatthias Ringwald printf("Stopping inquiry scan..\n"); 91cee62800SMatthias Ringwald state = W4_SCAN_COMPLETE; 92287c758dSMatthias Ringwald gap_inquiry_stop(); 93cee62800SMatthias Ringwald } 94cee62800SMatthias Ringwald /* 95cee62800SMatthias Ringwald * @section Track throughput 96cee62800SMatthias Ringwald * @text We calculate the throughput by setting a start time and measuring the amount of 97cee62800SMatthias Ringwald * data sent. After a configurable REPORT_INTERVAL_MS, we print the throughput in kB/s 98cee62800SMatthias Ringwald * and reset the counter and start time. 99cee62800SMatthias Ringwald */ 100cee62800SMatthias Ringwald 101cee62800SMatthias Ringwald /* LISTING_START(tracking): Tracking throughput */ 102cee62800SMatthias Ringwald #define REPORT_INTERVAL_MS 3000 103cee62800SMatthias Ringwald static uint32_t test_data_transferred; 104cee62800SMatthias Ringwald static uint32_t test_data_start; 105cee62800SMatthias Ringwald 106cee62800SMatthias Ringwald static void test_reset(void){ 107cee62800SMatthias Ringwald test_data_start = btstack_run_loop_get_time_ms(); 108cee62800SMatthias Ringwald test_data_transferred = 0; 109cee62800SMatthias Ringwald } 110cee62800SMatthias Ringwald 111cee62800SMatthias Ringwald static void test_track_transferred(int bytes_sent){ 112cee62800SMatthias Ringwald test_data_transferred += bytes_sent; 113cee62800SMatthias Ringwald // evaluate 114cee62800SMatthias Ringwald uint32_t now = btstack_run_loop_get_time_ms(); 115cee62800SMatthias Ringwald uint32_t time_passed = now - test_data_start; 116cee62800SMatthias Ringwald if (time_passed < REPORT_INTERVAL_MS) return; 117cee62800SMatthias Ringwald // print speed 118cee62800SMatthias Ringwald int bytes_per_second = test_data_transferred * 1000 / time_passed; 119cee62800SMatthias Ringwald printf("%u bytes -> %u.%03u kB/s\n", (int) test_data_transferred, (int) bytes_per_second / 1000, bytes_per_second % 1000); 120cee62800SMatthias Ringwald 121cee62800SMatthias Ringwald // restart 122cee62800SMatthias Ringwald test_data_start = now; 123cee62800SMatthias Ringwald test_data_transferred = 0; 124cee62800SMatthias Ringwald } 125cee62800SMatthias Ringwald /* LISTING_END(tracking): Tracking throughput */ 126cee62800SMatthias Ringwald 127cee62800SMatthias Ringwald /* 128cee62800SMatthias Ringwald * @section Packet Handler 129cee62800SMatthias Ringwald * 130cee62800SMatthias Ringwald * @text The packet handler of the combined example is just the combination of the individual packet handlers. 131cee62800SMatthias Ringwald */ 132cee62800SMatthias Ringwald 133cee62800SMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 134cee62800SMatthias Ringwald UNUSED(channel); 135cee62800SMatthias Ringwald 136cee62800SMatthias Ringwald bd_addr_t event_addr; 137cee62800SMatthias Ringwald uint8_t rfcomm_channel_nr; 138cee62800SMatthias Ringwald uint32_t class_of_device; 139cee62800SMatthias Ringwald 140cee62800SMatthias Ringwald switch (packet_type) { 141cee62800SMatthias Ringwald case HCI_EVENT_PACKET: 142cee62800SMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 143cee62800SMatthias Ringwald 144cee62800SMatthias Ringwald case HCI_EVENT_PIN_CODE_REQUEST: 145cee62800SMatthias Ringwald // inform about pin code request 146cee62800SMatthias Ringwald printf("Pin code request - using '0000'\n"); 147cee62800SMatthias Ringwald hci_event_pin_code_request_get_bd_addr(packet, event_addr); 148cee62800SMatthias Ringwald gap_pin_code_response(event_addr, "0000"); 149cee62800SMatthias Ringwald break; 150cee62800SMatthias Ringwald 151cee62800SMatthias Ringwald case HCI_EVENT_USER_CONFIRMATION_REQUEST: 152cee62800SMatthias Ringwald // inform about user confirmation request 153cee62800SMatthias Ringwald printf("SSP User Confirmation Request with numeric value '%06"PRIu32"'\n", little_endian_read_32(packet, 8)); 154cee62800SMatthias Ringwald printf("SSP User Confirmation Auto accept\n"); 155cee62800SMatthias Ringwald break; 156cee62800SMatthias Ringwald 157cee62800SMatthias Ringwald case RFCOMM_EVENT_INCOMING_CONNECTION: 158cee62800SMatthias Ringwald // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16) 159cee62800SMatthias Ringwald rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr); 160cee62800SMatthias Ringwald rfcomm_channel_nr = rfcomm_event_incoming_connection_get_server_channel(packet); 161cee62800SMatthias Ringwald rfcomm_cid = rfcomm_event_incoming_connection_get_rfcomm_cid(packet); 162cee62800SMatthias Ringwald printf("RFCOMM channel %u requested for %s\n", rfcomm_channel_nr, bd_addr_to_str(event_addr)); 163cee62800SMatthias Ringwald rfcomm_accept_connection(rfcomm_cid); 164cee62800SMatthias Ringwald break; 165cee62800SMatthias Ringwald 166cee62800SMatthias Ringwald case RFCOMM_EVENT_CHANNEL_OPENED: 167cee62800SMatthias Ringwald // data: event(8), len(8), status (8), address (48), server channel(8), rfcomm_cid(16), max frame size(16) 168cee62800SMatthias Ringwald if (rfcomm_event_channel_opened_get_status(packet)) { 169cee62800SMatthias Ringwald printf("RFCOMM channel open failed, status %u\n", rfcomm_event_channel_opened_get_status(packet)); 170cee62800SMatthias Ringwald } else { 171cee62800SMatthias Ringwald rfcomm_cid = rfcomm_event_channel_opened_get_rfcomm_cid(packet); 172cee62800SMatthias Ringwald rfcomm_mtu = rfcomm_event_channel_opened_get_max_frame_size(packet); 173cee62800SMatthias Ringwald printf("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u\n", rfcomm_cid, rfcomm_mtu); 174cee62800SMatthias Ringwald test_reset(); 175cee62800SMatthias Ringwald // disable page and inquiry scan 176cee62800SMatthias Ringwald gap_connectable_control(0); 177cee62800SMatthias Ringwald } 178cee62800SMatthias Ringwald break; 179cee62800SMatthias Ringwald 180cee62800SMatthias Ringwald case RFCOMM_EVENT_CHANNEL_CLOSED: 181cee62800SMatthias Ringwald printf("RFCOMM channel closed\n"); 182cee62800SMatthias Ringwald rfcomm_cid = 0; // re-enable page scan 183cee62800SMatthias Ringwald gap_connectable_control(1); 184cee62800SMatthias Ringwald break; 185cee62800SMatthias Ringwald 186cee62800SMatthias Ringwald case BTSTACK_EVENT_STATE: 187cee62800SMatthias Ringwald if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return; 188cee62800SMatthias Ringwald start_scan(); 189cee62800SMatthias Ringwald break; 190cee62800SMatthias Ringwald 191cee62800SMatthias Ringwald case GAP_EVENT_INQUIRY_RESULT: 192*7c03fe59SMatthias Ringwald if (state != W4_PEER_COD) break; 193cee62800SMatthias Ringwald class_of_device = gap_event_inquiry_result_get_class_of_device(packet); 194cee62800SMatthias Ringwald gap_event_inquiry_result_get_bd_addr(packet, event_addr); 195cee62800SMatthias Ringwald if (class_of_device == TEST_COD){ 196cee62800SMatthias Ringwald memcpy(peer_addr, event_addr, 6); 197cee62800SMatthias Ringwald printf("Peer found: %s\n", bd_addr_to_str(peer_addr)); 198cee62800SMatthias Ringwald stop_scan(); 199cee62800SMatthias Ringwald } else { 200cee62800SMatthias Ringwald printf("Device found: %s with COD: 0x%06x\n", bd_addr_to_str(event_addr), (int) class_of_device); 201cee62800SMatthias Ringwald } 202cee62800SMatthias Ringwald break; 203cee62800SMatthias Ringwald 204287c758dSMatthias Ringwald case GAP_EVENT_INQUIRY_COMPLETE: 205*7c03fe59SMatthias Ringwald switch (state){ 206*7c03fe59SMatthias Ringwald case W4_PEER_COD: 207cee62800SMatthias Ringwald printf("Inquiry complete\n"); 208cee62800SMatthias Ringwald printf("Peer not found, starting scan again\n"); 209cee62800SMatthias Ringwald start_scan(); 210cee62800SMatthias Ringwald break; 211*7c03fe59SMatthias Ringwald case W4_SCAN_COMPLETE: 212*7c03fe59SMatthias Ringwald printf("Start to connect\n"); 213*7c03fe59SMatthias Ringwald state = W4_RFCOMM_CHANNEL; 214*7c03fe59SMatthias Ringwald rfcomm_create_channel(packet_handler, peer_addr, RFCOMM_SERVER_CHANNEL, NULL); 215*7c03fe59SMatthias Ringwald break; 216*7c03fe59SMatthias Ringwald default: 217*7c03fe59SMatthias Ringwald break; 218*7c03fe59SMatthias Ringwald } 219*7c03fe59SMatthias Ringwald if (state == W4_PEER_COD){ 220*7c03fe59SMatthias Ringwald } 221*7c03fe59SMatthias Ringwald break; 222cee62800SMatthias Ringwald 223cee62800SMatthias Ringwald default: 224cee62800SMatthias Ringwald break; 225cee62800SMatthias Ringwald } 226cee62800SMatthias Ringwald break; 227cee62800SMatthias Ringwald 228cee62800SMatthias Ringwald case RFCOMM_DATA_PACKET: 229cee62800SMatthias Ringwald test_track_transferred(size); 230*7c03fe59SMatthias Ringwald 231cee62800SMatthias Ringwald #if 0 232cee62800SMatthias Ringwald printf("RCV: '"); 233cee62800SMatthias Ringwald for (i=0;i<size;i++){ 234cee62800SMatthias Ringwald putchar(packet[i]); 235cee62800SMatthias Ringwald } 236cee62800SMatthias Ringwald printf("'\n"); 237cee62800SMatthias Ringwald #endif 238cee62800SMatthias Ringwald break; 239cee62800SMatthias Ringwald 240cee62800SMatthias Ringwald default: 241cee62800SMatthias Ringwald break; 242cee62800SMatthias Ringwald } 243cee62800SMatthias Ringwald } 244cee62800SMatthias Ringwald 245cee62800SMatthias Ringwald /* 246cee62800SMatthias Ringwald * @section Main Application Setup 247cee62800SMatthias Ringwald * 248cee62800SMatthias Ringwald * @text As with the packet and the heartbeat handlers, the combined app setup contains the code from the individual example setups. 249cee62800SMatthias Ringwald */ 250cee62800SMatthias Ringwald 251cee62800SMatthias Ringwald 252cee62800SMatthias Ringwald /* LISTING_START(MainConfiguration): Init L2CAP RFCOMM SDO SM ATT Server and start heartbeat timer */ 253cee62800SMatthias Ringwald int btstack_main(int argc, const char * argv[]); 254cee62800SMatthias Ringwald int btstack_main(int argc, const char * argv[]){ 255cee62800SMatthias Ringwald UNUSED(argc); 256cee62800SMatthias Ringwald (void)argv; 257cee62800SMatthias Ringwald 258cee62800SMatthias Ringwald // register for HCI events 259cee62800SMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 260cee62800SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 261cee62800SMatthias Ringwald 262cee62800SMatthias Ringwald l2cap_init(); 263cee62800SMatthias Ringwald 264cee62800SMatthias Ringwald rfcomm_init(); 265cee62800SMatthias Ringwald rfcomm_register_service(packet_handler, RFCOMM_SERVER_CHANNEL, 0xffff); 266cee62800SMatthias Ringwald 267cee62800SMatthias Ringwald // init SDP 268cee62800SMatthias Ringwald gap_ssp_set_io_capability(SSP_IO_CAPABILITY_DISPLAY_YES_NO); 269cee62800SMatthias Ringwald 270cee62800SMatthias Ringwald // turn on! 271cee62800SMatthias Ringwald hci_power_control(HCI_POWER_ON); 272cee62800SMatthias Ringwald 273cee62800SMatthias Ringwald return 0; 274cee62800SMatthias Ringwald } 275cee62800SMatthias Ringwald /* LISTING_END */ 276cee62800SMatthias Ringwald /* EXAMPLE_END */ 277