xref: /btstack/platform/daemon/example/l2cap_server.c (revision b3fcedb9c9ccbcc9680da165cd86f394be5d84a8)
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_server.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 #include "classic/sdp_util.h"
53 
54 int l2cap_reg_fail = 0;
55 
56 hci_con_handle_t con_handle;
57 
58 uint16_t hid_control = 0;
59 uint16_t hid_interrupt = 0;
60 
61 void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
62 
63 	bd_addr_t event_addr;
64 	hci_con_handle_t con_handle;
65 	uint16_t psm;
66 	uint16_t local_cid;
67 	uint16_t remote_cid;
68 	char pin[20];
69 	int i;
70 	uint8_t status;
71 
72 	switch (packet_type) {
73 
74 		case L2CAP_DATA_PACKET:
75 			// just dump data for now
76 			printf("source cid %x -- ", channel);
77 			printf_hexdump( packet, size );
78 			break;
79 
80 		case HCI_EVENT_PACKET:
81 
82 			switch (hci_event_packet_get_type(packet)) {
83 
84 				case BTSTACK_EVENT_POWERON_FAILED:
85 					printf("HCI Init failed - make sure you have turned off Bluetooth in the System Settings\n");
86 					exit(1);
87 					break;
88 
89 				case BTSTACK_EVENT_STATE:
90 					// bt stack activated, get started
91 					if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
92 						bt_send_cmd(&hci_write_class_of_device, 0x2540);
93 					}
94 					break;
95 
96 				case DAEMON_EVENT_L2CAP_SERVICE_REGISTERED:
97 					status = packet[2];
98 					psm = little_endian_read_16(packet, 3);
99 					printf("DAEMON_EVENT_L2CAP_SERVICE_REGISTERED psm: 0x%02x, status: 0x%02x\n", psm, status);
100 					if (status) {
101 						l2cap_reg_fail = 1;
102 					} else {
103 						if (psm == PSM_HID_INTERRUPT && !l2cap_reg_fail) { // The second of the two
104 							bt_send_cmd(&btstack_set_discoverable, 1);
105 							printf("Both PSMs registered.\n");
106 						}
107 					}
108 					break;
109 
110 				case L2CAP_EVENT_INCOMING_CONNECTION:
111 					l2cap_event_incoming_connection_get_address(packet, event_addr);
112 					con_handle = l2cap_event_incoming_connection_get_handle(packet);
113 					psm        = l2cap_event_incoming_connection_get_psm(packet);
114 					local_cid  = l2cap_event_incoming_connection_get_local_cid(packet);
115 					remote_cid = l2cap_event_incoming_connection_get_remote_cid(packet);
116 					printf("L2CAP_EVENT_INCOMING_CONNECTION %s, handle 0x%02x, psm 0x%02x, local cid 0x%02x, remote cid 0x%02x\n",
117 						bd_addr_to_str(event_addr), con_handle, psm, local_cid, remote_cid);
118 
119 					// accept
120 					bt_send_cmd(&l2cap_accept_connection_cmd, local_cid);
121 					break;
122 
123 				case HCI_EVENT_LINK_KEY_REQUEST:
124 					// link key request
125 					hci_event_link_key_request_get_bd_addr(packet, event_addr);
126 					bt_send_cmd(&hci_link_key_request_negative_reply, &event_addr);
127 					break;
128 
129 				case HCI_EVENT_PIN_CODE_REQUEST:
130 					// inform about pin code request
131 					printf("Please enter PIN here: ");
132 					fgets(pin, 20, stdin);
133 					i = strlen(pin)-1;
134 					if( pin[i] == '\n') {
135 						pin[i] = '\0';
136 					}
137 					printf("PIN = '%s'\n", pin);
138 					hci_event_pin_code_request_get_bd_addr(packet, event_addr);
139 					bt_send_cmd(&hci_pin_code_request_reply, &event_addr, strlen(pin), pin);
140 					break;
141 
142 				case L2CAP_EVENT_CHANNEL_OPENED:
143 					// inform about new l2cap connection
144 					l2cap_event_channel_opened_get_address(packet, event_addr);
145 
146 					if (l2cap_event_channel_opened_get_status(packet)){
147 						printf("L2CAP connection to device %s failed. status code %u\n",
148 							bd_addr_to_str(event_addr), l2cap_event_channel_opened_get_status(packet));
149 						exit(1);
150 					}
151 					psm = l2cap_event_channel_opened_get_psm(packet);
152 					local_cid = l2cap_event_channel_opened_get_local_cid(packet);
153 					con_handle = l2cap_event_channel_opened_get_handle(packet);
154 
155 					printf("Channel successfully opened: %s, handle 0x%02x, psm 0x%02x, local cid 0x%02x, remote cid 0x%02x\n",
156 						   bd_addr_to_str(event_addr), con_handle, psm, local_cid,  l2cap_event_channel_opened_get_remote_cid(packet));
157 
158 					if (psm == PSM_HID_CONTROL){
159 						hid_control = local_cid;
160 					}
161 					if (psm == PSM_HID_INTERRUPT){
162 						hid_interrupt = local_cid;
163 					}
164 					if (hid_control && hid_interrupt){
165 						bt_send_cmd(&hci_switch_role_command, &event_addr, 0);
166 					}
167 					break;
168 
169 				case HCI_EVENT_ROLE_CHANGE: {
170 					//HID Control: 0x06 bytes - SET_FEATURE_REPORT [ 53 F4 42 03 00 00 ]
171 					uint8_t set_feature_report[] = { 0x53, 0xf4, 0x42, 0x03, 0x00, 0x00};
172 					bt_send_l2cap(hid_control, (uint8_t*) &set_feature_report, sizeof(set_feature_report));
173 					break;
174 				}
175 
176 				case HCI_EVENT_DISCONNECTION_COMPLETE:
177 					// connection closed -> quit tes app
178 					printf("Basebank connection closed\n");
179 
180 					// exit(0);
181 					break;
182 
183 				case HCI_EVENT_COMMAND_COMPLETE:
184 					if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_write_class_of_device)) {
185 						printf("Ready\n");
186 					}
187 				default:
188 					// other event
189 					break;
190 			}
191 			break;
192 
193 		default:
194 			// other packet type
195 			break;
196 	}
197 }
198 
199 int main (int argc, const char * argv[]){
200 	btstack_run_loop_init(btstack_run_loop_posix_get_instance());
201 	int err = bt_open();
202 	if (err) {
203 		printf("Failed to open connection to BTdaemon\n");
204 		return err;
205 	}
206 	bt_register_packet_handler(packet_handler);
207 	bt_send_cmd(&l2cap_register_service_cmd, PSM_HID_CONTROL, 250);
208 	bt_send_cmd(&l2cap_register_service_cmd, PSM_HID_INTERRUPT, 250);
209 
210 	bt_send_cmd(&btstack_set_power_mode, HCI_POWER_ON );
211 	btstack_run_loop_execute();
212 	bt_close();
213 	return 0;
214 }
215