xref: /btstack/platform/daemon/example/rfcomm_echo.c (revision 8257e5f9e1a9f15ac2fe1e69adc5d2eeeadf3fff)
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 // input from command line arguments
57 bd_addr_t addr = { };
58 hci_con_handle_t con_handle;
59 uint16_t mtu;
60 char pin[17];
61 int counter = 0;
62 uint16_t rfcomm_channel_nr;
63 uint16_t rfcomm_channel_id;
64 
65 uint8_t service_buffer[150];
66 
67 void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
68 	bd_addr_t event_addr;
69 
70 	switch (packet_type) {
71 
72 		case RFCOMM_DATA_PACKET:
73 			printf("Received RFCOMM data on channel id %u, size %u\n", channel, size);
74 			printf_hexdump(packet, size);
75             bt_send_rfcomm(channel, packet, size);
76 			break;
77 
78 		case HCI_EVENT_PACKET:
79 			switch (hci_event_packet_get_type(packet)) {
80 
81 				case BTSTACK_EVENT_POWERON_FAILED:
82 					// handle HCI init failure
83 					printf("HCI Init failed - make sure you have turned off Bluetooth in the System Settings\n");
84 					exit(1);
85 					break;
86 
87 				case BTSTACK_EVENT_STATE:
88 					// bt stack activated, get started
89                     if (packet[2] == HCI_STATE_WORKING) {
90                         // get persistent RFCOMM channel
91                         printf("HCI_STATE_WORKING\n");
92                         bt_send_cmd(&rfcomm_persistent_channel_for_service_cmd, "ch.ringwald.btstack.rfcomm_echo2");
93                   	}
94 					break;
95 
96                 case RFCOMM_EVENT_PERSISTENT_CHANNEL:
97                     rfcomm_channel_nr = packet[3];
98                     printf("RFCOMM channel %u was assigned by BTdaemon\n", rfcomm_channel_nr);
99                     bt_send_cmd(&rfcomm_register_service_cmd, rfcomm_channel_nr, 0xffff);  // reserved channel, mtu limited by l2cap
100                     break;
101 
102                 case RFCOMM_EVENT_SERVICE_REGISTERED:
103                     printf("RFCOMM_EVENT_SERVICE_REGISTERED channel: %u, status: 0x%02x\n", packet[3], packet[2]);
104                     // register SDP for our SPP
105                     sdp_create_spp_service((uint8_t*)service_buffer, 0x10001, rfcomm_channel_nr, "SPP ECHO");
106                     bt_send_cmd(&sdp_register_service_record_cmd, service_buffer);
107                     bt_send_cmd(&btstack_set_discoverable, 1);
108                     break;
109 
110 				case HCI_EVENT_PIN_CODE_REQUEST:
111 					// inform about pin code request
112 					printf("Using PIN 0000\n");
113 					reverse_bd_addr(&packet[2],
114 							event_addr);
115 					bt_send_cmd(&hci_pin_code_request_reply, &event_addr, 4, "0000");
116 					break;
117 
118 				case RFCOMM_EVENT_INCOMING_CONNECTION:
119 					// data: event (8), len(8), address(48), channel (8), rfcomm_cid (16)
120 					reverse_bd_addr(&packet[2],
121 							event_addr);
122 					rfcomm_channel_nr = packet[8];
123 					rfcomm_channel_id = little_endian_read_16(packet, 9);
124 					printf("RFCOMM channel %u requested for %s\n", rfcomm_channel_nr, bd_addr_to_str(event_addr));
125 					bt_send_cmd(&rfcomm_accept_connection_cmd, rfcomm_channel_id);
126 					break;
127 
128 				case RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE:
129 					// data: event(8), len(8), status (8), address (48), handle(16), server channel(8), rfcomm_cid(16), max frame size(16)
130 					if (packet[2]) {
131 						printf("RFCOMM channel open failed, status %u\n", packet[2]);
132 					} else {
133 						rfcomm_channel_id = little_endian_read_16(packet, 12);
134 						mtu = little_endian_read_16(packet, 14);
135 						printf("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u\n", rfcomm_channel_id, mtu);
136 					}
137 					break;
138 
139 				case HCI_EVENT_DISCONNECTION_COMPLETE:
140 					// connection closed -> quit test app
141 					printf("Basebank connection closed\n");
142 					break;
143 
144 				default:
145 					break;
146 			}
147 			break;
148 		default:
149 			break;
150 	}
151 }
152 
153 
154 int main (int argc, const char * argv[]){
155 
156 	btstack_run_loop_init(btstack_run_loop_posix_get_instance());
157 	int err = bt_open();
158 	if (err) {
159 		fprintf(stderr,"Failed to open connection to BTdaemon, err %d\n",err);
160 		return 1;
161 	}
162 	bt_register_packet_handler(packet_handler);
163 
164 	bt_send_cmd(&btstack_set_power_mode, HCI_POWER_ON );
165 	btstack_run_loop_execute();
166 	bt_close();
167     return 0;
168 }
169