xref: /btstack/platform/daemon/example/l2cap_server.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  * contact@bluekitchen-gmbh.com
352531c97eSMatthias Ringwald  *
362531c97eSMatthias Ringwald  */
372531c97eSMatthias Ringwald 
38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "l2cap_server.c"
39ab2c6ae4SMatthias Ringwald 
402531c97eSMatthias Ringwald /*
412531c97eSMatthias Ringwald  *  l2cap_server.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 #include "classic/sdp_util.h"
546259b5dfSMatthias Ringwald #include "bluetooth_psm.h"
552531c97eSMatthias Ringwald 
56fa968b99SMatthias Ringwald #ifdef _WIN32
572ca78d18SMatthias Ringwald #include "btstack_run_loop_windows.h"
58fa968b99SMatthias Ringwald #else
59fa968b99SMatthias Ringwald #include "btstack_run_loop_posix.h"
60fa968b99SMatthias Ringwald #endif
61fa968b99SMatthias Ringwald 
622531c97eSMatthias Ringwald int l2cap_reg_fail = 0;
632531c97eSMatthias Ringwald 
642531c97eSMatthias Ringwald hci_con_handle_t con_handle;
652531c97eSMatthias Ringwald 
662531c97eSMatthias Ringwald uint16_t hid_control = 0;
672531c97eSMatthias Ringwald uint16_t hid_interrupt = 0;
682531c97eSMatthias Ringwald 
packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)692531c97eSMatthias Ringwald void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
702531c97eSMatthias Ringwald 
712531c97eSMatthias Ringwald 	bd_addr_t event_addr;
72fc64f94aSMatthias Ringwald 	hci_con_handle_t con_handle;
732531c97eSMatthias Ringwald 	uint16_t psm;
742531c97eSMatthias Ringwald 	uint16_t local_cid;
752531c97eSMatthias Ringwald 	uint16_t remote_cid;
762531c97eSMatthias Ringwald 	char pin[20];
772531c97eSMatthias Ringwald 	int i;
782531c97eSMatthias Ringwald 	uint8_t status;
792531c97eSMatthias Ringwald 
802531c97eSMatthias Ringwald 	switch (packet_type) {
812531c97eSMatthias Ringwald 
822531c97eSMatthias Ringwald 		case L2CAP_DATA_PACKET:
832531c97eSMatthias Ringwald 			// just dump data for now
842531c97eSMatthias Ringwald 			printf("source cid %x -- ", channel);
852531c97eSMatthias Ringwald 			printf_hexdump( packet, size );
862531c97eSMatthias Ringwald 			break;
872531c97eSMatthias Ringwald 
882531c97eSMatthias Ringwald 		case HCI_EVENT_PACKET:
892531c97eSMatthias Ringwald 
900e2df43fSMatthias Ringwald 			switch (hci_event_packet_get_type(packet)) {
912531c97eSMatthias Ringwald 
922531c97eSMatthias Ringwald 				case BTSTACK_EVENT_POWERON_FAILED:
932531c97eSMatthias Ringwald 					printf("HCI Init failed - make sure you have turned off Bluetooth in the System Settings\n");
942531c97eSMatthias Ringwald 					exit(1);
952531c97eSMatthias Ringwald 					break;
962531c97eSMatthias Ringwald 
972531c97eSMatthias Ringwald 				case BTSTACK_EVENT_STATE:
982531c97eSMatthias Ringwald 					// bt stack activated, get started
99be7cc9a0SMilanka Ringwald 					if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
1002531c97eSMatthias Ringwald 						bt_send_cmd(&hci_write_class_of_device, 0x2540);
1012531c97eSMatthias Ringwald 					}
1022531c97eSMatthias Ringwald 					break;
1032531c97eSMatthias Ringwald 
10462c64df1SMatthias Ringwald 				case DAEMON_EVENT_L2CAP_SERVICE_REGISTERED:
1052531c97eSMatthias Ringwald 					status = packet[2];
1062531c97eSMatthias Ringwald 					psm = little_endian_read_16(packet, 3);
10762c64df1SMatthias Ringwald 					printf("DAEMON_EVENT_L2CAP_SERVICE_REGISTERED psm: 0x%02x, status: 0x%02x\n", psm, status);
1082531c97eSMatthias Ringwald 					if (status) {
1092531c97eSMatthias Ringwald 						l2cap_reg_fail = 1;
1102531c97eSMatthias Ringwald 					} else {
1116259b5dfSMatthias Ringwald 						if (psm == BLUETOOTH_PSM_HID_INTERRUPT && !l2cap_reg_fail) { // The second of the two
1122531c97eSMatthias Ringwald 							bt_send_cmd(&btstack_set_discoverable, 1);
1132531c97eSMatthias Ringwald 							printf("Both PSMs registered.\n");
1142531c97eSMatthias Ringwald 						}
1152531c97eSMatthias Ringwald 					}
1162531c97eSMatthias Ringwald 					break;
1172531c97eSMatthias Ringwald 
1182531c97eSMatthias Ringwald 				case L2CAP_EVENT_INCOMING_CONNECTION:
119b6d4e5c9SMilanka Ringwald 					l2cap_event_incoming_connection_get_address(packet, event_addr);
120b6d4e5c9SMilanka Ringwald 					con_handle = l2cap_event_incoming_connection_get_handle(packet);
121b6d4e5c9SMilanka Ringwald 					psm        = l2cap_event_incoming_connection_get_psm(packet);
122b6d4e5c9SMilanka Ringwald 					local_cid  = l2cap_event_incoming_connection_get_local_cid(packet);
123b6d4e5c9SMilanka Ringwald 					remote_cid = l2cap_event_incoming_connection_get_remote_cid(packet);
1242531c97eSMatthias Ringwald 					printf("L2CAP_EVENT_INCOMING_CONNECTION %s, handle 0x%02x, psm 0x%02x, local cid 0x%02x, remote cid 0x%02x\n",
125fc64f94aSMatthias Ringwald 						bd_addr_to_str(event_addr), con_handle, psm, local_cid, remote_cid);
1262531c97eSMatthias Ringwald 
1272531c97eSMatthias Ringwald 					// accept
1282531c97eSMatthias Ringwald 					bt_send_cmd(&l2cap_accept_connection_cmd, local_cid);
1292531c97eSMatthias Ringwald 					break;
1302531c97eSMatthias Ringwald 
1312531c97eSMatthias Ringwald 				case HCI_EVENT_LINK_KEY_REQUEST:
1322531c97eSMatthias Ringwald 					// link key request
13332fe0279SMilanka Ringwald 					hci_event_link_key_request_get_bd_addr(packet, event_addr);
1342531c97eSMatthias Ringwald 					bt_send_cmd(&hci_link_key_request_negative_reply, &event_addr);
1352531c97eSMatthias Ringwald 					break;
1362531c97eSMatthias Ringwald 
1372531c97eSMatthias Ringwald 				case HCI_EVENT_PIN_CODE_REQUEST:
1382531c97eSMatthias Ringwald 					// inform about pin code request
1392531c97eSMatthias Ringwald 					printf("Please enter PIN here: ");
140cb93c223SMatthias Ringwald 					// avoid -Wunused-result
141cb93c223SMatthias Ringwald 					char* res = fgets(pin, 20, stdin);
142cb93c223SMatthias Ringwald 					UNUSED(res);
1432531c97eSMatthias Ringwald 					i = strlen(pin)-1;
1442531c97eSMatthias Ringwald 					if( pin[i] == '\n') {
1452531c97eSMatthias Ringwald 						pin[i] = '\0';
1462531c97eSMatthias Ringwald 					}
1472531c97eSMatthias Ringwald 					printf("PIN = '%s'\n", pin);
148a6ef64baSMilanka Ringwald 					hci_event_pin_code_request_get_bd_addr(packet, event_addr);
1492531c97eSMatthias Ringwald 					bt_send_cmd(&hci_pin_code_request_reply, &event_addr, strlen(pin), pin);
1502531c97eSMatthias Ringwald 					break;
1512531c97eSMatthias Ringwald 
1522531c97eSMatthias Ringwald 				case L2CAP_EVENT_CHANNEL_OPENED:
1532531c97eSMatthias Ringwald 					// inform about new l2cap connection
154b6d4e5c9SMilanka Ringwald 					l2cap_event_channel_opened_get_address(packet, event_addr);
155b6d4e5c9SMilanka Ringwald 
156b6d4e5c9SMilanka Ringwald 					if (l2cap_event_channel_opened_get_status(packet)){
157b6d4e5c9SMilanka Ringwald 						printf("L2CAP connection to device %s failed. status code %u\n",
158b6d4e5c9SMilanka Ringwald 							bd_addr_to_str(event_addr), l2cap_event_channel_opened_get_status(packet));
159b6d4e5c9SMilanka Ringwald 						exit(1);
160b6d4e5c9SMilanka Ringwald 					}
161b6d4e5c9SMilanka Ringwald 					psm = l2cap_event_channel_opened_get_psm(packet);
162b6d4e5c9SMilanka Ringwald 					local_cid = l2cap_event_channel_opened_get_local_cid(packet);
163b6d4e5c9SMilanka Ringwald 					con_handle = l2cap_event_channel_opened_get_handle(packet);
164b6d4e5c9SMilanka Ringwald 
1652531c97eSMatthias Ringwald 					printf("Channel successfully opened: %s, handle 0x%02x, psm 0x%02x, local cid 0x%02x, remote cid 0x%02x\n",
166b6d4e5c9SMilanka Ringwald 						   bd_addr_to_str(event_addr), con_handle, psm, local_cid,  l2cap_event_channel_opened_get_remote_cid(packet));
1672531c97eSMatthias Ringwald 
1686259b5dfSMatthias Ringwald 					if (psm == BLUETOOTH_PSM_HID_CONTROL){
1692531c97eSMatthias Ringwald 						hid_control = local_cid;
1702531c97eSMatthias Ringwald 					}
1716259b5dfSMatthias Ringwald 					if (psm == BLUETOOTH_PSM_HID_INTERRUPT){
1722531c97eSMatthias Ringwald 						hid_interrupt = local_cid;
1732531c97eSMatthias Ringwald 					}
1742531c97eSMatthias Ringwald 					if (hid_control && hid_interrupt){
1752531c97eSMatthias Ringwald 						bt_send_cmd(&hci_switch_role_command, &event_addr, 0);
1762531c97eSMatthias Ringwald 					}
1772531c97eSMatthias Ringwald 					break;
1782531c97eSMatthias Ringwald 
1792531c97eSMatthias Ringwald 				case HCI_EVENT_ROLE_CHANGE: {
1802531c97eSMatthias Ringwald 					//HID Control: 0x06 bytes - SET_FEATURE_REPORT [ 53 F4 42 03 00 00 ]
1812531c97eSMatthias Ringwald 					uint8_t set_feature_report[] = { 0x53, 0xf4, 0x42, 0x03, 0x00, 0x00};
1822531c97eSMatthias Ringwald 					bt_send_l2cap(hid_control, (uint8_t*) &set_feature_report, sizeof(set_feature_report));
1832531c97eSMatthias Ringwald 					break;
1842531c97eSMatthias Ringwald 				}
1852531c97eSMatthias Ringwald 
1862531c97eSMatthias Ringwald 				case HCI_EVENT_DISCONNECTION_COMPLETE:
1872531c97eSMatthias Ringwald 					// connection closed -> quit tes app
1882531c97eSMatthias Ringwald 					printf("Basebank connection closed\n");
1892531c97eSMatthias Ringwald 
1902531c97eSMatthias Ringwald 					// exit(0);
1912531c97eSMatthias Ringwald 					break;
1922531c97eSMatthias Ringwald 
1932531c97eSMatthias Ringwald 				case HCI_EVENT_COMMAND_COMPLETE:
194*db09c08dSMatthias Ringwald 					if (hci_event_command_complete_get_command_opcode(packet) == HCI_OPCODE_HCI_WRITE_CLASS_OF_DEVICE){
1952531c97eSMatthias Ringwald 						printf("Ready\n");
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 }
2082531c97eSMatthias Ringwald 
main(int argc,const char * argv[])2092531c97eSMatthias Ringwald int main (int argc, const char * argv[]){
210b9dcd1ccSMatthias Ringwald #ifdef _WIN32
211b9dcd1ccSMatthias Ringwald 	btstack_run_loop_init(btstack_run_loop_windows_get_instance());
212fa968b99SMatthias Ringwald #else
213fa968b99SMatthias Ringwald 	btstack_run_loop_init(btstack_run_loop_posix_get_instance());
214b9dcd1ccSMatthias Ringwald #endif
2152531c97eSMatthias Ringwald 	int err = bt_open();
2162531c97eSMatthias Ringwald 	if (err) {
2172531c97eSMatthias Ringwald 		printf("Failed to open connection to BTdaemon\n");
2182531c97eSMatthias Ringwald 		return err;
2192531c97eSMatthias Ringwald 	}
2202531c97eSMatthias Ringwald 	bt_register_packet_handler(packet_handler);
2216259b5dfSMatthias Ringwald 	bt_send_cmd(&l2cap_register_service_cmd, BLUETOOTH_PSM_HID_CONTROL, 250);
2226259b5dfSMatthias Ringwald 	bt_send_cmd(&l2cap_register_service_cmd, BLUETOOTH_PSM_HID_INTERRUPT, 250);
2232531c97eSMatthias Ringwald 
2242531c97eSMatthias Ringwald 	bt_send_cmd(&btstack_set_power_mode, HCI_POWER_ON );
2252531c97eSMatthias Ringwald 	btstack_run_loop_execute();
2262531c97eSMatthias Ringwald 	bt_close();
2272531c97eSMatthias Ringwald 	return 0;
2282531c97eSMatthias Ringwald }
229