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 * rfcomm_echo.c 40 */ 41 42 #include <unistd.h> 43 #include <stdio.h> 44 #include <stdlib.h> 45 #include <strings.h> 46 #include <errno.h> 47 #include <string.h> 48 #include <fcntl.h> 49 #include <sys/types.h> 50 #include <sys/stat.h> 51 52 #include "btstack_client.h" 53 #include "btstack_run_loop_posix.h" 54 #include "classic/sdp_util.h" 55 56 #define NUM_ROWS 25 57 #define NUM_COLS 80 58 59 // input from command line arguments 60 bd_addr_t addr = { }; 61 hci_con_handle_t con_handle; 62 char pin[17]; 63 int counter = 0; 64 uint16_t rfcomm_channel_id = 0; 65 uint16_t mtu = 0; 66 static uint8_t spp_service_buffer[150]; 67 uint8_t test_data[NUM_ROWS * NUM_COLS + 1]; 68 69 void create_test_data(void){ 70 int x,y; 71 for (y=0;y<NUM_ROWS;y++){ 72 for (x=0;x<NUM_COLS-2;x++){ 73 test_data[y*NUM_COLS+x] = '0' + (x % 10); 74 } 75 test_data[y*NUM_COLS+NUM_COLS-2] = '\n'; 76 test_data[y*NUM_COLS+NUM_COLS-1] = '\r'; 77 } 78 test_data[NUM_COLS*NUM_ROWS] = 0; 79 } 80 81 void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 82 bd_addr_t event_addr; 83 uint16_t rfcomm_channel_nr; 84 85 switch (packet_type) { 86 87 case RFCOMM_DATA_PACKET: 88 printf("Received RFCOMM data on channel id %u, size %u\n", channel, size); 89 printf_hexdump(packet, size); 90 bt_send_rfcomm(channel, packet, size); 91 break; 92 93 case HCI_EVENT_PACKET: 94 switch (hci_event_packet_get_type(packet)) { 95 96 case BTSTACK_EVENT_POWERON_FAILED: 97 // handle HCI init failure 98 printf("HCI Init failed - make sure you have turned off Bluetooth in the System Settings\n"); 99 exit(1); 100 break; 101 102 case BTSTACK_EVENT_STATE: 103 // bt stack activated, get started 104 if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){ 105 // get persistent RFCOMM channel 106 printf("HCI_STATE_WORKING\n"); 107 bt_send_cmd(&rfcomm_persistent_channel_for_service_cmd, "ch.ringwald.btstack.rfcomm_test"); 108 } 109 break; 110 111 case DAEMON_EVENT_RFCOMM_PERSISTENT_CHANNEL: 112 rfcomm_channel_nr = packet[3]; 113 printf("RFCOMM channel %u was assigned by BTdaemon\n", rfcomm_channel_nr); 114 bt_send_cmd(&rfcomm_register_service_cmd, rfcomm_channel_nr, 0xffff); // reserved channel, mtu limited by l2cap 115 break; 116 117 case DAEMON_EVENT_RFCOMM_SERVICE_REGISTERED: 118 printf("DAEMON_EVENT_RFCOMM_SERVICE_REGISTERED\n"); 119 rfcomm_channel_nr = packet[3]; 120 // register SDP for our SPP 121 spp_create_sdp_record((uint8_t*) spp_service_buffer, 0x10001, rfcomm_channel_nr, "RFCOMM Test"); 122 bt_send_cmd(&sdp_register_service_record_cmd, spp_service_buffer); 123 bt_send_cmd(&btstack_set_discoverable, 1); 124 break; 125 126 case DAEMON_EVENT_RFCOMM_CREDITS: 127 sprintf((char*)test_data, "\n\r\n\r-> %09u <- ", counter++); 128 bt_send_rfcomm(rfcomm_channel_id, test_data, mtu); 129 break; 130 131 case HCI_EVENT_PIN_CODE_REQUEST: 132 // inform about pin code request 133 printf("Using PIN 0000\n"); 134 hci_event_pin_code_request_get_bd_addr(packet, event_addr); 135 bt_send_cmd(&hci_pin_code_request_reply, &event_addr, 4, "0000"); 136 break; 137 138 case RFCOMM_EVENT_INCOMING_CONNECTION: 139 // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16) 140 rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr); 141 rfcomm_channel_nr = rfcomm_event_incoming_connection_get_server_channel(packet); 142 rfcomm_channel_id = rfcomm_event_incoming_connection_get_rfcomm_cid(packet); 143 printf("RFCOMM channel %u requested for %s\n", rfcomm_channel_nr, bd_addr_to_str(event_addr)); 144 bt_send_cmd(&rfcomm_accept_connection_cmd, rfcomm_channel_id); 145 break; 146 147 case RFCOMM_EVENT_CHANNEL_OPENED: 148 // data: event(8), len(8), status (8), address (48), server channel(8), rfcomm_cid(16), max frame size(16) 149 if (rfcomm_event_channel_opened_get_status(packet)) { 150 printf("RFCOMM channel open failed, status %u\n", rfcomm_event_channel_opened_get_status(packet)); 151 } else { 152 rfcomm_channel_id = rfcomm_event_channel_opened_get_rfcomm_cid(packet); 153 mtu = rfcomm_event_channel_opened_get_max_frame_size(packet); 154 printf("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u\n", rfcomm_channel_id, mtu); 155 } 156 break; 157 158 case HCI_EVENT_DISCONNECTION_COMPLETE: 159 // connection closed -> quit test app 160 printf("Basebank connection closed\n"); 161 break; 162 163 default: 164 break; 165 } 166 break; 167 default: 168 break; 169 } 170 } 171 172 173 int main (int argc, const char * argv[]){ 174 175 create_test_data(); 176 printf("created test data: \n%s\n", test_data); 177 178 btstack_run_loop_init(btstack_run_loop_posix_get_instance()); 179 int err = bt_open(); 180 if (err) { 181 fprintf(stderr,"Failed to open connection to BTdaemon, err %d\n",err); 182 return 1; 183 } 184 bt_register_packet_handler(packet_handler); 185 186 bt_send_cmd(&btstack_set_power_mode, HCI_POWER_ON ); 187 btstack_run_loop_execute(); 188 bt_close(); 189 return 0; 190 } 191