xref: /btstack/platform/daemon/example/l2cap_server.c (revision 62c64df103800dffeb1f9ba60fd255ece0fbc125)
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 					// 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 					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 					reverse_bd_addr(&packet[3],
147 							event_addr);
148 					psm = little_endian_read_16(packet, 11);
149 					local_cid = little_endian_read_16(packet, 13);
150 					con_handle = little_endian_read_16(packet, 9);
151 					if (packet[2] == 0) {
152 						printf("Channel successfully opened: %s, handle 0x%02x, psm 0x%02x, local cid 0x%02x, remote cid 0x%02x\n",
153 							   bd_addr_to_str(event_addr), con_handle, psm, local_cid,  little_endian_read_16(packet, 15));
154 
155 						if (psm == PSM_HID_CONTROL){
156 							hid_control = local_cid;
157 						}
158 						if (psm == PSM_HID_INTERRUPT){
159 							hid_interrupt = local_cid;
160 						}
161 						if (hid_control && hid_interrupt){
162 							bt_send_cmd(&hci_switch_role_command, &event_addr, 0);
163 						}
164 					} else {
165 						printf("L2CAP connection to device %s failed. status code %u\n", bd_addr_to_str(event_addr), packet[2]);
166 						exit(1);
167 					}
168 					break;
169 
170 				case HCI_EVENT_ROLE_CHANGE: {
171 					//HID Control: 0x06 bytes - SET_FEATURE_REPORT [ 53 F4 42 03 00 00 ]
172 					uint8_t set_feature_report[] = { 0x53, 0xf4, 0x42, 0x03, 0x00, 0x00};
173 					bt_send_l2cap(hid_control, (uint8_t*) &set_feature_report, sizeof(set_feature_report));
174 					break;
175 				}
176 
177 				case HCI_EVENT_DISCONNECTION_COMPLETE:
178 					// connection closed -> quit tes app
179 					printf("Basebank connection closed\n");
180 
181 					// exit(0);
182 					break;
183 
184 				case HCI_EVENT_COMMAND_COMPLETE:
185 					if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_write_class_of_device)) {
186 						printf("Ready\n");
187 					}
188 				default:
189 					// other event
190 					break;
191 			}
192 			break;
193 
194 		default:
195 			// other packet type
196 			break;
197 	}
198 }
199 
200 int main (int argc, const char * argv[]){
201 	btstack_run_loop_init(btstack_run_loop_posix_get_instance());
202 	int err = bt_open();
203 	if (err) {
204 		printf("Failed to open connection to BTdaemon\n");
205 		return err;
206 	}
207 	bt_register_packet_handler(packet_handler);
208 	bt_send_cmd(&l2cap_register_service_cmd, PSM_HID_CONTROL, 250);
209 	bt_send_cmd(&l2cap_register_service_cmd, PSM_HID_INTERRUPT, 250);
210 
211 	bt_send_cmd(&btstack_set_power_mode, HCI_POWER_ON );
212 	btstack_run_loop_execute();
213 	bt_close();
214 	return 0;
215 }
216