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