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 fgets(pin, 20, stdin); 140 i = strlen(pin)-1; 141 if( pin[i] == '\n') { 142 pin[i] = '\0'; 143 } 144 printf("PIN = '%s'\n", pin); 145 hci_event_pin_code_request_get_bd_addr(packet, event_addr); 146 bt_send_cmd(&hci_pin_code_request_reply, &event_addr, strlen(pin), pin); 147 break; 148 149 case L2CAP_EVENT_CHANNEL_OPENED: 150 // inform about new l2cap connection 151 l2cap_event_channel_opened_get_address(packet, event_addr); 152 153 if (l2cap_event_channel_opened_get_status(packet)){ 154 printf("L2CAP connection to device %s failed. status code %u\n", 155 bd_addr_to_str(event_addr), l2cap_event_channel_opened_get_status(packet)); 156 exit(1); 157 } 158 psm = l2cap_event_channel_opened_get_psm(packet); 159 local_cid = l2cap_event_channel_opened_get_local_cid(packet); 160 con_handle = l2cap_event_channel_opened_get_handle(packet); 161 162 printf("Channel successfully opened: %s, handle 0x%02x, psm 0x%02x, local cid 0x%02x, remote cid 0x%02x\n", 163 bd_addr_to_str(event_addr), con_handle, psm, local_cid, l2cap_event_channel_opened_get_remote_cid(packet)); 164 165 if (psm == PSM_HID_CONTROL){ 166 hid_control = local_cid; 167 } 168 if (psm == PSM_HID_INTERRUPT){ 169 hid_interrupt = local_cid; 170 } 171 if (hid_control && hid_interrupt){ 172 bt_send_cmd(&hci_switch_role_command, &event_addr, 0); 173 } 174 break; 175 176 case HCI_EVENT_ROLE_CHANGE: { 177 //HID Control: 0x06 bytes - SET_FEATURE_REPORT [ 53 F4 42 03 00 00 ] 178 uint8_t set_feature_report[] = { 0x53, 0xf4, 0x42, 0x03, 0x00, 0x00}; 179 bt_send_l2cap(hid_control, (uint8_t*) &set_feature_report, sizeof(set_feature_report)); 180 break; 181 } 182 183 case HCI_EVENT_DISCONNECTION_COMPLETE: 184 // connection closed -> quit tes app 185 printf("Basebank connection closed\n"); 186 187 // exit(0); 188 break; 189 190 case HCI_EVENT_COMMAND_COMPLETE: 191 if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_write_class_of_device)) { 192 printf("Ready\n"); 193 } 194 default: 195 // other event 196 break; 197 } 198 break; 199 200 default: 201 // other packet type 202 break; 203 } 204 } 205 206 int main (int argc, const char * argv[]){ 207 #ifdef _WIN32 208 btstack_run_loop_init(btstack_run_loop_windows_get_instance()); 209 #else 210 btstack_run_loop_init(btstack_run_loop_posix_get_instance()); 211 #endif 212 int err = bt_open(); 213 if (err) { 214 printf("Failed to open connection to BTdaemon\n"); 215 return err; 216 } 217 bt_register_packet_handler(packet_handler); 218 bt_send_cmd(&l2cap_register_service_cmd, PSM_HID_CONTROL, 250); 219 bt_send_cmd(&l2cap_register_service_cmd, PSM_HID_INTERRUPT, 250); 220 221 bt_send_cmd(&btstack_set_power_mode, HCI_POWER_ON ); 222 btstack_run_loop_execute(); 223 bt_close(); 224 return 0; 225 } 226