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): Send test data via SPP as fast as possible 40 * @text After RFCOMM connections gets open, request a 41 * RFCOMM_EVENT_CAN_SEND_NOW via rfcomm_request_can_send_now_event(). 42 * When we get the RFCOMM_EVENT_CAN_SEND_NOW, send data and request another one. 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 int btstack_main(int argc, const char * argv[]); 55 56 #define RFCOMM_SERVER_CHANNEL 1 57 58 #define TEST_COD 0x1234 59 #define NUM_ROWS 25 60 #define NUM_COLS 40 61 #define DATA_VOLUME (10 * 1000 * 1000) 62 63 static btstack_packet_callback_registration_t hci_event_callback_registration; 64 65 static uint8_t test_data[NUM_ROWS * NUM_COLS]; 66 67 // SPP 68 static uint8_t spp_service_buffer[150]; 69 70 static uint16_t spp_test_data_len; 71 static uint16_t rfcomm_mtu; 72 static uint16_t rfcomm_cid = 0; 73 // static uint32_t data_to_send = DATA_VOLUME; 74 75 /* 76 * @section Track throughput 77 * @text We calculate the throughput by setting a start time and measuring the amount of 78 * data sent. After a configurable REPORT_INTERVAL_MS, we print the throughput in kB/s 79 * and reset the counter and start time. 80 */ 81 82 /* LISTING_START(tracking): Tracking throughput */ 83 #define REPORT_INTERVAL_MS 3000 84 static uint32_t test_data_transferred; 85 static uint32_t test_data_start; 86 87 static void test_reset(void){ 88 test_data_start = btstack_run_loop_get_time_ms(); 89 test_data_transferred = 0; 90 } 91 92 static void test_track_transferred(int bytes_sent){ 93 test_data_transferred += bytes_sent; 94 // evaluate 95 uint32_t now = btstack_run_loop_get_time_ms(); 96 uint32_t time_passed = now - test_data_start; 97 if (time_passed < REPORT_INTERVAL_MS) return; 98 // print speed 99 int bytes_per_second = test_data_transferred * 1000 / time_passed; 100 printf("%u bytes -> %u.%03u kB/s\n", (int) test_data_transferred, (int) bytes_per_second / 1000, bytes_per_second % 1000); 101 102 // restart 103 test_data_start = now; 104 test_data_transferred = 0; 105 } 106 /* LISTING_END(tracking): Tracking throughput */ 107 108 109 static void spp_create_test_data(void){ 110 int x,y; 111 for (y=0;y<NUM_ROWS;y++){ 112 for (x=0;x<NUM_COLS-2;x++){ 113 test_data[y*NUM_COLS+x] = '0' + (x % 10); 114 } 115 test_data[y*NUM_COLS+NUM_COLS-2] = '\n'; 116 test_data[y*NUM_COLS+NUM_COLS-1] = '\r'; 117 } 118 } 119 120 static void spp_send_packet(void){ 121 rfcomm_send(rfcomm_cid, (uint8_t*) test_data, spp_test_data_len); 122 123 test_track_transferred(spp_test_data_len); 124 #if 0 125 if (data_to_send <= spp_test_data_len){ 126 printf("SPP Streamer: enough data send, closing channel\n"); 127 rfcomm_disconnect(rfcomm_cid); 128 rfcomm_cid = 0; 129 return; 130 } 131 data_to_send -= spp_test_data_len; 132 #endif 133 rfcomm_request_can_send_now_event(rfcomm_cid); 134 } 135 136 /* 137 * @section Packet Handler 138 * 139 * @text The packet handler of the combined example is just the combination of the individual packet handlers. 140 */ 141 142 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 143 UNUSED(channel); 144 145 bd_addr_t event_addr; 146 uint8_t rfcomm_channel_nr; 147 148 switch (packet_type) { 149 case HCI_EVENT_PACKET: 150 switch (hci_event_packet_get_type(packet)) { 151 152 case HCI_EVENT_PIN_CODE_REQUEST: 153 // inform about pin code request 154 printf("Pin code request - using '0000'\n"); 155 hci_event_pin_code_request_get_bd_addr(packet, event_addr); 156 gap_pin_code_response(event_addr, "0000"); 157 break; 158 159 case HCI_EVENT_USER_CONFIRMATION_REQUEST: 160 // inform about user confirmation request 161 printf("SSP User Confirmation Request with numeric value '%06"PRIu32"'\n", little_endian_read_32(packet, 8)); 162 printf("SSP User Confirmation Auto accept\n"); 163 break; 164 165 case RFCOMM_EVENT_INCOMING_CONNECTION: 166 // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16) 167 rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr); 168 rfcomm_channel_nr = rfcomm_event_incoming_connection_get_server_channel(packet); 169 rfcomm_cid = rfcomm_event_incoming_connection_get_rfcomm_cid(packet); 170 printf("RFCOMM channel %u requested for %s\n", rfcomm_channel_nr, bd_addr_to_str(event_addr)); 171 rfcomm_accept_connection(rfcomm_cid); 172 break; 173 174 case RFCOMM_EVENT_CHANNEL_OPENED: 175 // data: event(8), len(8), status (8), address (48), server channel(8), rfcomm_cid(16), max frame size(16) 176 if (rfcomm_event_channel_opened_get_status(packet)) { 177 printf("RFCOMM channel open failed, status %u\n", rfcomm_event_channel_opened_get_status(packet)); 178 } else { 179 rfcomm_cid = rfcomm_event_channel_opened_get_rfcomm_cid(packet); 180 rfcomm_mtu = rfcomm_event_channel_opened_get_max_frame_size(packet); 181 printf("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u\n", rfcomm_cid, rfcomm_mtu); 182 183 spp_test_data_len = rfcomm_mtu; 184 if (spp_test_data_len > sizeof(test_data)){ 185 spp_test_data_len = sizeof(test_data); 186 } 187 188 // disable page/inquiry scan to get max performance 189 gap_discoverable_control(0); 190 gap_connectable_control(0); 191 192 test_reset(); 193 rfcomm_request_can_send_now_event(rfcomm_cid); 194 } 195 break; 196 197 case RFCOMM_EVENT_CAN_SEND_NOW: 198 spp_send_packet(); 199 break; 200 201 case RFCOMM_EVENT_CHANNEL_CLOSED: 202 printf("RFCOMM channel closed\n"); 203 rfcomm_cid = 0; 204 205 // re-enable page/inquiry scan again 206 gap_discoverable_control(1); 207 gap_connectable_control(1); 208 break; 209 210 default: 211 break; 212 } 213 break; 214 215 case RFCOMM_DATA_PACKET: 216 test_track_transferred(size); 217 #if 0 218 printf("RCV: '"); 219 for (i=0;i<size;i++){ 220 putchar(packet[i]); 221 } 222 printf("'\n"); 223 #endif 224 break; 225 226 default: 227 break; 228 } 229 } 230 231 /* 232 * @section Main Application Setup 233 * 234 * @text As with the packet and the heartbeat handlers, the combined app setup contains the code from the individual example setups. 235 */ 236 237 238 /* LISTING_START(MainConfiguration): Init L2CAP RFCOMM SDP SPP */ 239 int btstack_main(int argc, const char * argv[]) 240 { 241 (void)argc; 242 (void)argv; 243 244 // register for HCI events 245 hci_event_callback_registration.callback = &packet_handler; 246 hci_add_event_handler(&hci_event_callback_registration); 247 248 l2cap_init(); 249 250 rfcomm_init(); 251 rfcomm_register_service(packet_handler, RFCOMM_SERVER_CHANNEL, 0xffff); 252 253 // init SDP, create record for SPP and register with SDP 254 sdp_init(); 255 memset(spp_service_buffer, 0, sizeof(spp_service_buffer)); 256 spp_create_sdp_record(spp_service_buffer, 0x10001, RFCOMM_SERVER_CHANNEL, "SPP Streamer"); 257 sdp_register_service(spp_service_buffer); 258 // printf("SDP service record size: %u\n", de_get_len(spp_service_buffer)); 259 260 // short-cut to find other SPP Streamer 261 gap_set_class_of_device(TEST_COD); 262 263 gap_ssp_set_io_capability(SSP_IO_CAPABILITY_DISPLAY_YES_NO); 264 gap_set_local_name("SPP Streamer 00:00:00:00:00:00"); 265 gap_discoverable_control(1); 266 267 spp_create_test_data(); 268 269 // turn on! 270 hci_power_control(HCI_POWER_ON); 271 272 return 0; 273 } 274 /* LISTING_END */ 275 /* EXAMPLE_END */ 276