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