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