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