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