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