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