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__ "rfcomm_cat.c" 39 40 /* 41 * rfcomm.c 42 * 43 * Command line parsing and debug option 44 * added by Vladimir Vyskocil <[email protected]> 45 * 46 */ 47 48 #include <unistd.h> 49 #include <stdio.h> 50 #include <stdlib.h> 51 #include <strings.h> 52 #include <errno.h> 53 #include <string.h> 54 #include <fcntl.h> 55 #include <sys/types.h> 56 #include <sys/stat.h> 57 58 #include "btstack_client.h" 59 #include "btstack_run_loop_posix.h" 60 #include "classic/sdp_util.h" 61 62 // input from command line arguments 63 bd_addr_t addr = { }; 64 hci_con_handle_t con_handle; 65 int rfcomm_channel = 1; 66 char pin[17]; 67 68 void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 69 bd_addr_t event_addr; 70 uint16_t mtu; 71 uint16_t rfcomm_channel_id; 72 73 switch (packet_type) { 74 75 case RFCOMM_DATA_PACKET: 76 printf("Received RFCOMM data on channel id %u, size %u\n", channel, size); 77 printf_hexdump(packet, size); 78 break; 79 80 case HCI_EVENT_PACKET: 81 switch (hci_event_packet_get_type(packet)) { 82 83 case BTSTACK_EVENT_POWERON_FAILED: 84 // handle HCI init failure 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(&rfcomm_create_channel_cmd, addr, rfcomm_channel); 93 } 94 break; 95 96 case HCI_EVENT_PIN_CODE_REQUEST: 97 // inform about pin code request 98 printf("Using PIN 0000\n"); 99 hci_event_pin_code_request_get_bd_addr(packet, event_addr); 100 bt_send_cmd(&hci_pin_code_request_reply, &event_addr, 4, "0000"); 101 break; 102 103 case RFCOMM_EVENT_CHANNEL_OPENED: 104 // data: event(8), len(8), status (8), address (48), handle(16), server channel(8), rfcomm_cid(16), max frame size(16) 105 if (packet[2]) { 106 printf("RFCOMM channel open failed, status %u\n", packet[2]); 107 } else { 108 rfcomm_channel_id = little_endian_read_16(packet, 12); 109 mtu = little_endian_read_16(packet, 14); 110 printf("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u\n", rfcomm_channel_id, mtu); 111 } 112 break; 113 114 case HCI_EVENT_DISCONNECTION_COMPLETE: 115 // connection closed -> quit test app 116 printf("Basebank connection closed\n"); 117 break; 118 119 default: 120 break; 121 } 122 break; 123 default: 124 break; 125 } 126 } 127 128 void usage(const char *name){ 129 fprintf(stderr, "Usage : %s [-a|--address aa:bb:cc:dd:ee:ff] [-c|--channel n] [-p|--pin nnnn]\n", name); 130 } 131 132 int main (int argc, const char * argv[]){ 133 134 int arg = 1; 135 136 if (argc == 1){ 137 usage(argv[0]); 138 return 1; } 139 140 while (arg < argc) { 141 if(!strcmp(argv[arg], "-a") || !strcmp(argv[arg], "--address")){ 142 arg++; 143 if(arg >= argc || !sscanf_bd_addr(argv[arg], addr)){ 144 usage(argv[0]); 145 return 1; 146 } 147 } else if (!strcmp(argv[arg], "-c") || !strcmp(argv[arg], "--channel")) { 148 arg++; 149 if(arg >= argc || !sscanf(argv[arg], "%d", &rfcomm_channel)){ 150 usage(argv[0]); 151 return 1; 152 } 153 } else if (!strcmp(argv[arg], "-p") || !strcmp(argv[arg], "--pin")) { 154 arg++; 155 if(arg >= argc) { 156 usage(argv[0]); 157 return 1; 158 } 159 strncpy(pin, argv[arg], 16); 160 pin[16] = 0; 161 } else { 162 usage(argv[0]); 163 return 1; 164 } 165 arg++; 166 } 167 168 btstack_run_loop_init(btstack_run_loop_posix_get_instance()); 169 int err = bt_open(); 170 if (err) { 171 fprintf(stderr,"Failed to open connection to BTdaemon, err %d\n",err); 172 return 1; 173 } 174 bt_register_packet_handler(packet_handler); 175 176 printf("Trying to connect to %s, channel %d\n", bd_addr_to_str(addr), rfcomm_channel); 177 178 bt_send_cmd(&btstack_set_power_mode, HCI_POWER_ON ); 179 btstack_run_loop_execute(); 180 bt_close(); 181 return 0; 182 } 183