xref: /btstack/platform/daemon/example/l2cap_server.c (revision a6ef64ba22bce34968d6e68d4dcc73d429482da9)
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 L2CAP_EVENT_SERVICE_REGISTERED:
97 					status = packet[2];
98 					psm = little_endian_read_16(packet, 3);
99 					printf("L2CAP_EVENT_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 					// data: event(8), len(8), address(48), handle (16),  psm (16), source cid(16) dest cid(16)
112 					reverse_bd_addr(&packet[2],
113 							event_addr);
114 					con_handle = little_endian_read_16(packet, 8);
115 					psm        = little_endian_read_16(packet, 10);
116 					local_cid  = little_endian_read_16(packet, 12);
117 					remote_cid = little_endian_read_16(packet, 14);
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 					reverse_bd_addr(&packet[2],
128 							event_addr);
129 					bt_send_cmd(&hci_link_key_request_negative_reply, &event_addr);
130 					break;
131 
132 				case HCI_EVENT_PIN_CODE_REQUEST:
133 					// inform about pin code request
134 					printf("Please enter PIN here: ");
135 					fgets(pin, 20, stdin);
136 					i = strlen(pin)-1;
137 					if( pin[i] == '\n') {
138 						pin[i] = '\0';
139 					}
140 					printf("PIN = '%s'\n", pin);
141 					hci_event_pin_code_request_get_bd_addr(packet, event_addr);
142 					bt_send_cmd(&hci_pin_code_request_reply, &event_addr, strlen(pin), pin);
143 					break;
144 
145 				case L2CAP_EVENT_CHANNEL_OPENED:
146 					// inform about new l2cap connection
147 					reverse_bd_addr(&packet[3],
148 							event_addr);
149 					psm = little_endian_read_16(packet, 11);
150 					local_cid = little_endian_read_16(packet, 13);
151 					con_handle = little_endian_read_16(packet, 9);
152 					if (packet[2] == 0) {
153 						printf("Channel successfully opened: %s, handle 0x%02x, psm 0x%02x, local cid 0x%02x, remote cid 0x%02x\n",
154 							   bd_addr_to_str(event_addr), con_handle, psm, local_cid,  little_endian_read_16(packet, 15));
155 
156 						if (psm == PSM_HID_CONTROL){
157 							hid_control = local_cid;
158 						}
159 						if (psm == PSM_HID_INTERRUPT){
160 							hid_interrupt = local_cid;
161 						}
162 						if (hid_control && hid_interrupt){
163 							bt_send_cmd(&hci_switch_role_command, &event_addr, 0);
164 						}
165 					} else {
166 						printf("L2CAP connection to device %s failed. status code %u\n", bd_addr_to_str(event_addr), packet[2]);
167 						exit(1);
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