1 /* 2 * Copyright (C) 2014 BlueKitchen GmbH 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the copyright holders nor the names of 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 4. Any redistribution, use, or modification is done solely for 17 * personal benefit and not for any commercial purpose or for 18 * monetary gain. 19 * 20 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * Please inquire about commercial licensing options at 34 * [email protected] 35 * 36 */ 37 38 // ***************************************************************************** 39 /* EXAMPLE_START(spp_streamer_client): Client for SPP Streamer 40 * 41 * @text The SPP Streamer Clients connects to SPP Streamer and tracks the 42 * amount of received data 43 */ 44 // ***************************************************************************** 45 46 #include <stdint.h> 47 #include <stdio.h> 48 #include <stdlib.h> 49 #include <string.h> 50 #include <inttypes.h> 51 52 #include "btstack.h" 53 54 #define RFCOMM_SERVER_CHANNEL 1 55 56 #define TEST_COD 0x1234 57 58 typedef enum { 59 // SPP 60 W4_PEER_COD, 61 W4_SCAN_COMPLETE, 62 W4_SDP_RESULT, 63 W4_SDP_COMPLETE, 64 W4_RFCOMM_CHANNEL, 65 SENDING, 66 DONE 67 } state_t; 68 69 static btstack_packet_callback_registration_t hci_event_callback_registration; 70 71 static bd_addr_t peer_addr; 72 static state_t state;; 73 74 // SPP 75 static uint16_t rfcomm_mtu; 76 static uint16_t rfcomm_cid = 0; 77 // static uint32_t data_to_send = DATA_VOLUME; 78 79 80 /** 81 * Find remote peer by COD 82 */ 83 #define INQUIRY_INTERVAL 5 84 static void start_scan(void){ 85 printf("Starting inquiry scan..\n"); 86 state = W4_PEER_COD; 87 gap_inquiry_start(INQUIRY_INTERVAL); 88 } 89 static void stop_scan(void){ 90 printf("Stopping inquiry scan..\n"); 91 state = W4_SCAN_COMPLETE; 92 gap_inquiry_stop(); 93 } 94 /* 95 * @section Track throughput 96 * @text We calculate the throughput by setting a start time and measuring the amount of 97 * data sent. After a configurable REPORT_INTERVAL_MS, we print the throughput in kB/s 98 * and reset the counter and start time. 99 */ 100 101 /* LISTING_START(tracking): Tracking throughput */ 102 #define REPORT_INTERVAL_MS 3000 103 static uint32_t test_data_transferred; 104 static uint32_t test_data_start; 105 106 static void test_reset(void){ 107 test_data_start = btstack_run_loop_get_time_ms(); 108 test_data_transferred = 0; 109 } 110 111 static void test_track_transferred(int bytes_sent){ 112 test_data_transferred += bytes_sent; 113 // evaluate 114 uint32_t now = btstack_run_loop_get_time_ms(); 115 uint32_t time_passed = now - test_data_start; 116 if (time_passed < REPORT_INTERVAL_MS) return; 117 // print speed 118 int bytes_per_second = test_data_transferred * 1000 / time_passed; 119 printf("%u bytes -> %u.%03u kB/s\n", (int) test_data_transferred, (int) bytes_per_second / 1000, bytes_per_second % 1000); 120 121 // restart 122 test_data_start = now; 123 test_data_transferred = 0; 124 } 125 /* LISTING_END(tracking): Tracking throughput */ 126 127 /* 128 * @section Packet Handler 129 * 130 * @text The packet handler of the combined example is just the combination of the individual packet handlers. 131 */ 132 133 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 134 UNUSED(channel); 135 136 bd_addr_t event_addr; 137 uint8_t rfcomm_channel_nr; 138 uint32_t class_of_device; 139 140 switch (packet_type) { 141 case HCI_EVENT_PACKET: 142 switch (hci_event_packet_get_type(packet)) { 143 144 case HCI_EVENT_PIN_CODE_REQUEST: 145 // inform about pin code request 146 printf("Pin code request - using '0000'\n"); 147 hci_event_pin_code_request_get_bd_addr(packet, event_addr); 148 gap_pin_code_response(event_addr, "0000"); 149 break; 150 151 case HCI_EVENT_USER_CONFIRMATION_REQUEST: 152 // inform about user confirmation request 153 printf("SSP User Confirmation Request with numeric value '%06"PRIu32"'\n", little_endian_read_32(packet, 8)); 154 printf("SSP User Confirmation Auto accept\n"); 155 break; 156 157 case RFCOMM_EVENT_INCOMING_CONNECTION: 158 // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16) 159 rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr); 160 rfcomm_channel_nr = rfcomm_event_incoming_connection_get_server_channel(packet); 161 rfcomm_cid = rfcomm_event_incoming_connection_get_rfcomm_cid(packet); 162 printf("RFCOMM channel %u requested for %s\n", rfcomm_channel_nr, bd_addr_to_str(event_addr)); 163 rfcomm_accept_connection(rfcomm_cid); 164 break; 165 166 case RFCOMM_EVENT_CHANNEL_OPENED: 167 // data: event(8), len(8), status (8), address (48), server channel(8), rfcomm_cid(16), max frame size(16) 168 if (rfcomm_event_channel_opened_get_status(packet)) { 169 printf("RFCOMM channel open failed, status %u\n", rfcomm_event_channel_opened_get_status(packet)); 170 } else { 171 rfcomm_cid = rfcomm_event_channel_opened_get_rfcomm_cid(packet); 172 rfcomm_mtu = rfcomm_event_channel_opened_get_max_frame_size(packet); 173 printf("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u\n", rfcomm_cid, rfcomm_mtu); 174 test_reset(); 175 176 // disable page/inquiry scan to get max performance 177 gap_discoverable_control(0); 178 gap_connectable_control(0); 179 } 180 break; 181 182 case RFCOMM_EVENT_CHANNEL_CLOSED: 183 printf("RFCOMM channel closed\n"); 184 rfcomm_cid = 0; 185 186 // re-enable page/inquiry scan again 187 gap_discoverable_control(1); 188 gap_connectable_control(1); 189 break; 190 191 case BTSTACK_EVENT_STATE: 192 if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return; 193 start_scan(); 194 break; 195 196 case GAP_EVENT_INQUIRY_RESULT: 197 if (state != W4_PEER_COD) break; 198 class_of_device = gap_event_inquiry_result_get_class_of_device(packet); 199 gap_event_inquiry_result_get_bd_addr(packet, event_addr); 200 if (class_of_device == TEST_COD){ 201 memcpy(peer_addr, event_addr, 6); 202 printf("Peer found: %s\n", bd_addr_to_str(peer_addr)); 203 stop_scan(); 204 } else { 205 printf("Device found: %s with COD: 0x%06x\n", bd_addr_to_str(event_addr), (int) class_of_device); 206 } 207 break; 208 209 case GAP_EVENT_INQUIRY_COMPLETE: 210 switch (state){ 211 case W4_PEER_COD: 212 printf("Inquiry complete\n"); 213 printf("Peer not found, starting scan again\n"); 214 start_scan(); 215 break; 216 case W4_SCAN_COMPLETE: 217 printf("Start to connect\n"); 218 state = W4_RFCOMM_CHANNEL; 219 rfcomm_create_channel(packet_handler, peer_addr, RFCOMM_SERVER_CHANNEL, NULL); 220 break; 221 default: 222 break; 223 } 224 if (state == W4_PEER_COD){ 225 } 226 break; 227 228 default: 229 break; 230 } 231 break; 232 233 case RFCOMM_DATA_PACKET: 234 test_track_transferred(size); 235 236 #if 0 237 printf("RCV: '"); 238 for (i=0;i<size;i++){ 239 putchar(packet[i]); 240 } 241 printf("'\n"); 242 #endif 243 break; 244 245 default: 246 break; 247 } 248 } 249 250 /* 251 * @section Main Application Setup 252 * 253 * @text As with the packet and the heartbeat handlers, the combined app setup contains the code from the individual example setups. 254 */ 255 256 257 /* LISTING_START(MainConfiguration): Init L2CAP RFCOMM SDO SM ATT Server and start heartbeat timer */ 258 int btstack_main(int argc, const char * argv[]); 259 int btstack_main(int argc, const char * argv[]){ 260 UNUSED(argc); 261 (void)argv; 262 263 // register for HCI events 264 hci_event_callback_registration.callback = &packet_handler; 265 hci_add_event_handler(&hci_event_callback_registration); 266 267 l2cap_init(); 268 269 rfcomm_init(); 270 rfcomm_register_service(packet_handler, RFCOMM_SERVER_CHANNEL, 0xffff); 271 272 // init SDP 273 gap_ssp_set_io_capability(SSP_IO_CAPABILITY_DISPLAY_YES_NO); 274 275 // turn on! 276 hci_power_control(HCI_POWER_ON); 277 278 return 0; 279 } 280 /* LISTING_END */ 281 /* EXAMPLE_END */ 282