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 hci_send_cmd(&hci_inquiry, HCI_INQUIRY_LAP, INQUIRY_INTERVAL, 0); 87 state = W4_PEER_COD; 88 } 89 static void stop_scan(void){ 90 printf("Stopping inquiry scan..\n"); 91 hci_send_cmd(&hci_inquiry_cancel); 92 state = W4_SCAN_COMPLETE; 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 // disable page and inquiry scan 176 gap_connectable_control(0); 177 } 178 break; 179 180 case RFCOMM_EVENT_CHANNEL_CLOSED: 181 printf("RFCOMM channel closed\n"); 182 rfcomm_cid = 0; // re-enable page scan 183 gap_connectable_control(1); 184 break; 185 186 case BTSTACK_EVENT_STATE: 187 if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return; 188 start_scan(); 189 break; 190 191 case GAP_EVENT_INQUIRY_RESULT: 192 class_of_device = gap_event_inquiry_result_get_class_of_device(packet); 193 gap_event_inquiry_result_get_bd_addr(packet, event_addr); 194 if (class_of_device == TEST_COD){ 195 memcpy(peer_addr, event_addr, 6); 196 printf("Peer found: %s\n", bd_addr_to_str(peer_addr)); 197 stop_scan(); 198 199 printf("Start to connect\n"); 200 state = W4_RFCOMM_CHANNEL; 201 rfcomm_create_channel(packet_handler, peer_addr, RFCOMM_SERVER_CHANNEL, NULL); 202 } else { 203 printf("Device found: %s with COD: 0x%06x\n", bd_addr_to_str(event_addr), (int) class_of_device); 204 } 205 break; 206 207 case HCI_EVENT_INQUIRY_COMPLETE: 208 printf("Inquiry complete\n"); 209 printf("Peer not found, starting scan again\n"); 210 start_scan(); 211 break; 212 213 default: 214 break; 215 } 216 break; 217 218 case RFCOMM_DATA_PACKET: 219 test_track_transferred(size); 220 #if 0 221 printf("RCV: '"); 222 for (i=0;i<size;i++){ 223 putchar(packet[i]); 224 } 225 printf("'\n"); 226 #endif 227 break; 228 229 default: 230 break; 231 } 232 } 233 234 /* 235 * @section Main Application Setup 236 * 237 * @text As with the packet and the heartbeat handlers, the combined app setup contains the code from the individual example setups. 238 */ 239 240 241 /* LISTING_START(MainConfiguration): Init L2CAP RFCOMM SDO SM ATT Server and start heartbeat timer */ 242 int btstack_main(int argc, const char * argv[]); 243 int btstack_main(int argc, const char * argv[]){ 244 UNUSED(argc); 245 (void)argv; 246 247 // register for HCI events 248 hci_event_callback_registration.callback = &packet_handler; 249 hci_add_event_handler(&hci_event_callback_registration); 250 251 l2cap_init(); 252 253 rfcomm_init(); 254 rfcomm_register_service(packet_handler, RFCOMM_SERVER_CHANNEL, 0xffff); 255 256 // init SDP 257 gap_ssp_set_io_capability(SSP_IO_CAPABILITY_DISPLAY_YES_NO); 258 259 // turn on! 260 hci_power_control(HCI_POWER_ON); 261 262 return 0; 263 } 264 /* LISTING_END */ 265 /* EXAMPLE_END */ 266