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