xref: /btstack/platform/daemon/example/l2cap_throughput.c (revision db09c08d745c978bc51e1abcf0215f8727d1e684)
12531c97eSMatthias Ringwald /*
22531c97eSMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
32531c97eSMatthias Ringwald  *
42531c97eSMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
52531c97eSMatthias Ringwald  * modification, are permitted provided that the following conditions
62531c97eSMatthias Ringwald  * are met:
72531c97eSMatthias Ringwald  *
82531c97eSMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
92531c97eSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
102531c97eSMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
112531c97eSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
122531c97eSMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
132531c97eSMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
142531c97eSMatthias Ringwald  *    contributors may be used to endorse or promote products derived
152531c97eSMatthias Ringwald  *    from this software without specific prior written permission.
162531c97eSMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
172531c97eSMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
182531c97eSMatthias Ringwald  *    monetary gain.
192531c97eSMatthias Ringwald  *
202531c97eSMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
212531c97eSMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
222531c97eSMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
232fca4dadSMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka Ringwald  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
252531c97eSMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
262531c97eSMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
272531c97eSMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
282531c97eSMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
292531c97eSMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
302531c97eSMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
312531c97eSMatthias Ringwald  * SUCH DAMAGE.
322531c97eSMatthias Ringwald  *
332531c97eSMatthias Ringwald  * Please inquire about commercial licensing options at
342531c97eSMatthias Ringwald  * [email protected]
352531c97eSMatthias Ringwald  *
362531c97eSMatthias Ringwald  */
372531c97eSMatthias Ringwald 
38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "l2cap_throughput.c"
39ab2c6ae4SMatthias Ringwald 
402531c97eSMatthias Ringwald /*
412531c97eSMatthias Ringwald  *  l2cap_throughput.c
422531c97eSMatthias Ringwald  *
432531c97eSMatthias Ringwald  *  Created by Matthias Ringwald on 7/14/09.
442531c97eSMatthias Ringwald  */
452531c97eSMatthias Ringwald 
462531c97eSMatthias Ringwald #include <unistd.h>
472531c97eSMatthias Ringwald #include <stdio.h>
482531c97eSMatthias Ringwald #include <stdlib.h>
492531c97eSMatthias Ringwald #include <string.h>
502531c97eSMatthias Ringwald 
512531c97eSMatthias Ringwald #include "btstack_client.h"
522531c97eSMatthias Ringwald #include "hci_cmd.h"
532531c97eSMatthias Ringwald 
54fa968b99SMatthias Ringwald #ifdef _WIN32
552ca78d18SMatthias Ringwald #include "btstack_run_loop_windows.h"
56fa968b99SMatthias Ringwald #else
57fa968b99SMatthias Ringwald #include "btstack_run_loop_posix.h"
58fa968b99SMatthias Ringwald #endif
59fa968b99SMatthias Ringwald 
602531c97eSMatthias Ringwald #define PSM_TEST 0xdead
612531c97eSMatthias Ringwald #define PACKET_SIZE 1000
622531c97eSMatthias Ringwald 
632531c97eSMatthias Ringwald int serverMode = 1;
642531c97eSMatthias Ringwald bd_addr_t addr = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
652531c97eSMatthias Ringwald uint8_t packet[PACKET_SIZE];
662531c97eSMatthias Ringwald uint32_t counter = 0;
672531c97eSMatthias Ringwald 
682531c97eSMatthias Ringwald btstack_timer_source_t timer;
692531c97eSMatthias Ringwald 
update_packet(void)702531c97eSMatthias Ringwald void update_packet(void){
712531c97eSMatthias Ringwald     big_endian_store_32( packet, 0, counter++);
722531c97eSMatthias Ringwald }
732531c97eSMatthias Ringwald 
prepare_packet(void)742531c97eSMatthias Ringwald void prepare_packet(void){
752531c97eSMatthias Ringwald     int i;
762531c97eSMatthias Ringwald     counter = 0;
772531c97eSMatthias Ringwald     big_endian_store_32( packet, 0, 0);
782531c97eSMatthias Ringwald     for (i=4;i<PACKET_SIZE;i++)
792531c97eSMatthias Ringwald         packet[i] = i-4;
802531c97eSMatthias Ringwald }
timer_handler(struct btstack_timer_source * ts)812531c97eSMatthias Ringwald void  timer_handler(struct btstack_timer_source *ts){
822531c97eSMatthias Ringwald 	bt_send_cmd(&hci_read_bd_addr);
832531c97eSMatthias Ringwald 	btstack_run_loop_set_timer(&timer, 3000);
842531c97eSMatthias Ringwald 	btstack_run_loop_add_timer(&timer);
852531c97eSMatthias Ringwald };
862531c97eSMatthias Ringwald 
packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)872531c97eSMatthias Ringwald void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
882531c97eSMatthias Ringwald 
892531c97eSMatthias Ringwald 	bd_addr_t event_addr;
90fc64f94aSMatthias Ringwald 	hci_con_handle_t con_handle;
912531c97eSMatthias Ringwald 	uint16_t psm;
922531c97eSMatthias Ringwald 	uint16_t local_cid;
932531c97eSMatthias Ringwald 	char pin[20];
942531c97eSMatthias Ringwald 	int i;
952531c97eSMatthias Ringwald 
962531c97eSMatthias Ringwald 	switch (packet_type) {
972531c97eSMatthias Ringwald 
982531c97eSMatthias Ringwald 		case L2CAP_DATA_PACKET:
992531c97eSMatthias Ringwald 			// measure data rate
1002531c97eSMatthias Ringwald 			break;
1012531c97eSMatthias Ringwald 
1022531c97eSMatthias Ringwald 		case HCI_EVENT_PACKET:
1032531c97eSMatthias Ringwald 
1040e2df43fSMatthias Ringwald 			switch (hci_event_packet_get_type(packet)) {
1052531c97eSMatthias Ringwald 
1062531c97eSMatthias Ringwald 				case BTSTACK_EVENT_POWERON_FAILED:
1072531c97eSMatthias Ringwald 					printf("HCI Init failed - make sure you have turned off Bluetooth in the System Settings\n");
1082531c97eSMatthias Ringwald 					exit(1);
1092531c97eSMatthias Ringwald 					break;
1102531c97eSMatthias Ringwald 
1112531c97eSMatthias Ringwald 				case BTSTACK_EVENT_STATE:
1122531c97eSMatthias Ringwald 					// bt stack activated, get started
113be7cc9a0SMilanka Ringwald 	                if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
1142531c97eSMatthias Ringwald 	                   if (serverMode) {
1152531c97eSMatthias Ringwald 						   printf("Waiting for incoming L2CAP connection on PSM %04x...\n", PSM_TEST);
1162531c97eSMatthias Ringwald 						   timer.process = timer_handler;
1172531c97eSMatthias Ringwald 						   btstack_run_loop_set_timer(&timer, 3000);
1182531c97eSMatthias Ringwald 						   // btstack_run_loop_add_timer(&timer);
1192531c97eSMatthias Ringwald 				        } else {
1202531c97eSMatthias Ringwald 				        	bt_send_cmd(&hci_write_class_of_device, 0x38010c);
1212531c97eSMatthias Ringwald 				        }
1222531c97eSMatthias Ringwald 					}
1232531c97eSMatthias Ringwald 					break;
1242531c97eSMatthias Ringwald 
1252531c97eSMatthias Ringwald                 case HCI_EVENT_COMMAND_COMPLETE:
1262531c97eSMatthias Ringwald 					// use pairing yes/no
127*db09c08dSMatthias Ringwald 					if (hci_event_command_complete_get_command_opcode(packet) == HCI_OPCODE_HCI_WRITE_CLASS_OF_DEVICE){
1282531c97eSMatthias Ringwald     				    bt_send_cmd(&l2cap_create_channel_mtu_cmd, addr, PSM_TEST, PACKET_SIZE);
1292531c97eSMatthias Ringwald 					}
1302531c97eSMatthias Ringwald 					break;
1312531c97eSMatthias Ringwald 
1322531c97eSMatthias Ringwald 				case L2CAP_EVENT_INCOMING_CONNECTION:
133b6d4e5c9SMilanka Ringwald 					l2cap_event_incoming_connection_get_address(packet, event_addr);
134b6d4e5c9SMilanka Ringwald 					con_handle = l2cap_event_incoming_connection_get_handle(packet);
135b6d4e5c9SMilanka Ringwald 					psm        = l2cap_event_incoming_connection_get_psm(packet);
136b6d4e5c9SMilanka Ringwald 					local_cid  = l2cap_event_incoming_connection_get_local_cid(packet);
137b6d4e5c9SMilanka Ringwald 					printf("L2CAP_EVENT_INCOMING_CONNECTION %s, handle 0x%02x, psm 0x%02x, local cid 0x%02x, remote cid 0x%02x\n",
1388121ebc2SMilanka Ringwald 						bd_addr_to_str(event_addr), con_handle, psm, local_cid, l2cap_event_incoming_connection_get_remote_cid(packet));
139b6d4e5c9SMilanka Ringwald 
1402531c97eSMatthias Ringwald 					// accept
1412531c97eSMatthias Ringwald 					bt_send_cmd(&l2cap_accept_connection_cmd, local_cid);
1422531c97eSMatthias Ringwald 					break;
1432531c97eSMatthias Ringwald 
1442531c97eSMatthias Ringwald 				case HCI_EVENT_LINK_KEY_REQUEST:
1452531c97eSMatthias Ringwald 					// link key request
14632fe0279SMilanka Ringwald 					hci_event_link_key_request_get_bd_addr(packet, event_addr);
1472531c97eSMatthias Ringwald 					bt_send_cmd(&hci_link_key_request_negative_reply, &event_addr);
1482531c97eSMatthias Ringwald 					break;
1492531c97eSMatthias Ringwald 
1502531c97eSMatthias Ringwald 				case HCI_EVENT_PIN_CODE_REQUEST:
1512531c97eSMatthias Ringwald 					// inform about pin code request
1522531c97eSMatthias Ringwald 					printf("Please enter PIN here: ");
153cb93c223SMatthias Ringwald 					// avoid -Wunused-result
154cb93c223SMatthias Ringwald 					char* res = fgets(pin, 20, stdin);
155cb93c223SMatthias Ringwald 					UNUSED(res);
1562531c97eSMatthias Ringwald 					i = strlen(pin);
1572531c97eSMatthias Ringwald 					if( pin[i-1] == '\n' || pin[i-1] == '\r') {
1582531c97eSMatthias Ringwald 						pin[i-1] = '\0';
1592531c97eSMatthias Ringwald 						i--;
1602531c97eSMatthias Ringwald 					}
1612531c97eSMatthias Ringwald 					printf("PIN (%u)= '%s'\n", i, pin);
162a6ef64baSMilanka Ringwald 					hci_event_pin_code_request_get_bd_addr(packet, event_addr);
1632531c97eSMatthias Ringwald 					bt_send_cmd(&hci_pin_code_request_reply, &event_addr, i, pin);
1642531c97eSMatthias Ringwald 					break;
1652531c97eSMatthias Ringwald 
1662531c97eSMatthias Ringwald 				case L2CAP_EVENT_CHANNEL_OPENED:
1672531c97eSMatthias Ringwald 					// inform about new l2cap connection
168b6d4e5c9SMilanka Ringwald 					l2cap_event_channel_opened_get_address(packet, event_addr);
169b6d4e5c9SMilanka Ringwald 
170b6d4e5c9SMilanka Ringwald 					if (l2cap_event_channel_opened_get_status(packet)){
171b6d4e5c9SMilanka Ringwald 						printf("L2CAP connection to device %s failed. status code %u\n",
172b6d4e5c9SMilanka Ringwald 							bd_addr_to_str(event_addr), l2cap_event_channel_opened_get_status(packet));
173b6d4e5c9SMilanka Ringwald 						exit(1);
174b6d4e5c9SMilanka Ringwald 					}
175b6d4e5c9SMilanka Ringwald 
1762531c97eSMatthias Ringwald 					psm = little_endian_read_16(packet, 11);
1772531c97eSMatthias Ringwald 					local_cid = little_endian_read_16(packet, 13);
178fc64f94aSMatthias Ringwald 					con_handle = little_endian_read_16(packet, 9);
1792531c97eSMatthias Ringwald 					printf("Channel successfully opened: %s, handle 0x%02x, psm 0x%02x, local cid 0x%02x, remote cid 0x%02x\n",
180b6d4e5c9SMilanka Ringwald 						   bd_addr_to_str(event_addr), con_handle, psm, local_cid,  l2cap_event_channel_opened_get_remote_cid(packet));
181b6d4e5c9SMilanka Ringwald 
1822531c97eSMatthias Ringwald 					break;
1832531c97eSMatthias Ringwald 
1842531c97eSMatthias Ringwald 				case HCI_EVENT_DISCONNECTION_COMPLETE:
1852531c97eSMatthias Ringwald 					printf("Basebank connection closed\n");
1862531c97eSMatthias Ringwald 					break;
1872531c97eSMatthias Ringwald 
18862c64df1SMatthias Ringwald 				case DAEMON_EVENT_L2CAP_CREDITS:
1892531c97eSMatthias Ringwald 					if (!serverMode) {
1902531c97eSMatthias Ringwald 						// can send! (assuming single credits are handet out)
1912531c97eSMatthias Ringwald 						update_packet();
1922531c97eSMatthias Ringwald 						local_cid = little_endian_read_16(packet, 2);
1932531c97eSMatthias Ringwald 						bt_send_l2cap( local_cid, packet, PACKET_SIZE);
1942531c97eSMatthias Ringwald 					}
1952531c97eSMatthias Ringwald 				    break;
1962531c97eSMatthias Ringwald 
1972531c97eSMatthias Ringwald 				default:
1982531c97eSMatthias Ringwald 					// other event
1992531c97eSMatthias Ringwald 					break;
2002531c97eSMatthias Ringwald 			}
2012531c97eSMatthias Ringwald 			break;
2022531c97eSMatthias Ringwald 
2032531c97eSMatthias Ringwald 		default:
2042531c97eSMatthias Ringwald 			// other packet type
2052531c97eSMatthias Ringwald 			break;
2062531c97eSMatthias Ringwald 	}
2072531c97eSMatthias Ringwald }
main(int argc,const char * argv[])2082531c97eSMatthias Ringwald int main (int argc, const char * argv[]){
2092531c97eSMatthias Ringwald     // handle remote addr
2102531c97eSMatthias Ringwald     if (argc > 1){
211a6efb919SMatthias Ringwald         if (sscanf_bd_addr(argv[1], addr)){
2122531c97eSMatthias Ringwald             serverMode = 0;
2132531c97eSMatthias Ringwald             prepare_packet();
2142531c97eSMatthias Ringwald         }
2152531c97eSMatthias Ringwald     }
2162531c97eSMatthias Ringwald 
217b9dcd1ccSMatthias Ringwald #ifdef _WIN32
218b9dcd1ccSMatthias Ringwald 	btstack_run_loop_init(btstack_run_loop_windows_get_instance());
219fa968b99SMatthias Ringwald #else
220fa968b99SMatthias Ringwald 	btstack_run_loop_init(btstack_run_loop_posix_get_instance());
221b9dcd1ccSMatthias Ringwald #endif
2222531c97eSMatthias Ringwald 	int err = bt_open();
2232531c97eSMatthias Ringwald 	if (err) {
2242531c97eSMatthias Ringwald 		printf("Failed to open connection to BTdaemon\n");
2252531c97eSMatthias Ringwald 		return err;
2262531c97eSMatthias Ringwald 	}
2272531c97eSMatthias Ringwald 	bt_register_packet_handler(packet_handler);
2282531c97eSMatthias Ringwald 	bt_send_cmd(&l2cap_register_service_cmd, PSM_TEST, PACKET_SIZE);
2292531c97eSMatthias Ringwald 	bt_send_cmd(&btstack_set_power_mode, HCI_POWER_ON );
2302531c97eSMatthias Ringwald 
2312531c97eSMatthias Ringwald 	// banner
2322531c97eSMatthias Ringwald 	printf("L2CAP Throughput Test (compatible with Apple's Bluetooth Explorer)\n");
2332531c97eSMatthias Ringwald 	if (serverMode) {
2342531c97eSMatthias Ringwald 	   printf(" * Running in Server mode. For client mode, specify remote addr 11:22:33:44:55:66\n");
2352531c97eSMatthias Ringwald     }
2362531c97eSMatthias Ringwald     printf(" * MTU: 1000 bytes\n");
2372531c97eSMatthias Ringwald 
2382531c97eSMatthias Ringwald 	btstack_run_loop_execute();
2392531c97eSMatthias Ringwald 	bt_close();
2402531c97eSMatthias Ringwald 	return 0;
2412531c97eSMatthias Ringwald }
242