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