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__ "l2cap_throughput.c" 39 40 /* 41 * l2cap_throughput.c 42 * 43 * Created by Matthias Ringwald on 7/14/09. 44 */ 45 46 #include <unistd.h> 47 #include <stdio.h> 48 #include <stdlib.h> 49 #include <string.h> 50 51 #include "btstack_client.h" 52 #include "btstack_run_loop_posix.h" 53 #include "hci_cmd.h" 54 55 #define PSM_TEST 0xdead 56 #define PACKET_SIZE 1000 57 58 int serverMode = 1; 59 bd_addr_t addr = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; 60 uint8_t packet[PACKET_SIZE]; 61 uint32_t counter = 0; 62 63 btstack_timer_source_t timer; 64 65 void update_packet(void){ 66 big_endian_store_32( packet, 0, counter++); 67 } 68 69 void prepare_packet(void){ 70 int i; 71 counter = 0; 72 big_endian_store_32( packet, 0, 0); 73 for (i=4;i<PACKET_SIZE;i++) 74 packet[i] = i-4; 75 } 76 void timer_handler(struct btstack_timer_source *ts){ 77 bt_send_cmd(&hci_read_bd_addr); 78 btstack_run_loop_set_timer(&timer, 3000); 79 btstack_run_loop_add_timer(&timer); 80 }; 81 82 void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 83 84 bd_addr_t event_addr; 85 hci_con_handle_t con_handle; 86 uint16_t psm; 87 uint16_t local_cid; 88 char pin[20]; 89 int i; 90 91 switch (packet_type) { 92 93 case L2CAP_DATA_PACKET: 94 // measure data rate 95 break; 96 97 case HCI_EVENT_PACKET: 98 99 switch (hci_event_packet_get_type(packet)) { 100 101 case BTSTACK_EVENT_POWERON_FAILED: 102 printf("HCI Init failed - make sure you have turned off Bluetooth in the System Settings\n"); 103 exit(1); 104 break; 105 106 case BTSTACK_EVENT_STATE: 107 // bt stack activated, get started 108 if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){ 109 if (serverMode) { 110 printf("Waiting for incoming L2CAP connection on PSM %04x...\n", PSM_TEST); 111 timer.process = timer_handler; 112 btstack_run_loop_set_timer(&timer, 3000); 113 // btstack_run_loop_add_timer(&timer); 114 } else { 115 bt_send_cmd(&hci_write_class_of_device, 0x38010c); 116 } 117 } 118 break; 119 120 case HCI_EVENT_COMMAND_COMPLETE: 121 // use pairing yes/no 122 if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_write_class_of_device)) { 123 bt_send_cmd(&l2cap_create_channel_mtu_cmd, addr, PSM_TEST, PACKET_SIZE); 124 } 125 break; 126 127 case L2CAP_EVENT_INCOMING_CONNECTION: 128 l2cap_event_incoming_connection_get_address(packet, event_addr); 129 con_handle = l2cap_event_incoming_connection_get_handle(packet); 130 psm = l2cap_event_incoming_connection_get_psm(packet); 131 local_cid = l2cap_event_incoming_connection_get_local_cid(packet); 132 printf("L2CAP_EVENT_INCOMING_CONNECTION %s, handle 0x%02x, psm 0x%02x, local cid 0x%02x, remote cid 0x%02x\n", 133 bd_addr_to_str(event_addr), con_handle, psm, local_cid, l2cap_event_incoming_connection_get_remote_cid(packet)); 134 135 // accept 136 bt_send_cmd(&l2cap_accept_connection_cmd, local_cid); 137 break; 138 139 case HCI_EVENT_LINK_KEY_REQUEST: 140 // link key request 141 hci_event_link_key_request_get_bd_addr(packet, event_addr); 142 bt_send_cmd(&hci_link_key_request_negative_reply, &event_addr); 143 break; 144 145 case HCI_EVENT_PIN_CODE_REQUEST: 146 // inform about pin code request 147 printf("Please enter PIN here: "); 148 fgets(pin, 20, stdin); 149 i = strlen(pin); 150 if( pin[i-1] == '\n' || pin[i-1] == '\r') { 151 pin[i-1] = '\0'; 152 i--; 153 } 154 printf("PIN (%u)= '%s'\n", i, pin); 155 hci_event_pin_code_request_get_bd_addr(packet, event_addr); 156 bt_send_cmd(&hci_pin_code_request_reply, &event_addr, i, pin); 157 break; 158 159 case L2CAP_EVENT_CHANNEL_OPENED: 160 // inform about new l2cap connection 161 l2cap_event_channel_opened_get_address(packet, event_addr); 162 163 if (l2cap_event_channel_opened_get_status(packet)){ 164 printf("L2CAP connection to device %s failed. status code %u\n", 165 bd_addr_to_str(event_addr), l2cap_event_channel_opened_get_status(packet)); 166 exit(1); 167 } 168 169 psm = little_endian_read_16(packet, 11); 170 local_cid = little_endian_read_16(packet, 13); 171 con_handle = little_endian_read_16(packet, 9); 172 printf("Channel successfully opened: %s, handle 0x%02x, psm 0x%02x, local cid 0x%02x, remote cid 0x%02x\n", 173 bd_addr_to_str(event_addr), con_handle, psm, local_cid, l2cap_event_channel_opened_get_remote_cid(packet)); 174 175 break; 176 177 case HCI_EVENT_DISCONNECTION_COMPLETE: 178 printf("Basebank connection closed\n"); 179 break; 180 181 case DAEMON_EVENT_L2CAP_CREDITS: 182 if (!serverMode) { 183 // can send! (assuming single credits are handet out) 184 update_packet(); 185 local_cid = little_endian_read_16(packet, 2); 186 bt_send_l2cap( local_cid, packet, PACKET_SIZE); 187 } 188 break; 189 190 default: 191 // other event 192 break; 193 } 194 break; 195 196 default: 197 // other packet type 198 break; 199 } 200 } 201 int main (int argc, const char * argv[]){ 202 // handle remote addr 203 if (argc > 1){ 204 if (sscanf_bd_addr(argv[1], addr)){ 205 serverMode = 0; 206 prepare_packet(); 207 } 208 } 209 210 btstack_run_loop_init(btstack_run_loop_posix_get_instance()); 211 int err = bt_open(); 212 if (err) { 213 printf("Failed to open connection to BTdaemon\n"); 214 return err; 215 } 216 bt_register_packet_handler(packet_handler); 217 bt_send_cmd(&l2cap_register_service_cmd, PSM_TEST, PACKET_SIZE); 218 bt_send_cmd(&btstack_set_power_mode, HCI_POWER_ON ); 219 220 // banner 221 printf("L2CAP Throughput Test (compatible with Apple's Bluetooth Explorer)\n"); 222 if (serverMode) { 223 printf(" * Running in Server mode. For client mode, specify remote addr 11:22:33:44:55:66\n"); 224 } 225 printf(" * MTU: 1000 bytes\n"); 226 227 btstack_run_loop_execute(); 228 bt_close(); 229 return 0; 230 } 231