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