blob: 7e8f8027dec4dbdfc906c664ca2911ef01e98bd7 [file] [log] [blame]
Chris Mantona6cee152014-10-09 16:04:05 -07001/******************************************************************************
2 *
3 * Copyright (C) 2014 Google, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19#include <getopt.h>
20#include <signal.h>
21#include <stdlib.h>
22#include <unistd.h>
23
24#include "btcore/include/bdaddr.h"
25#include "btcore/include/property.h"
26#include "osi/include/osi.h"
27#include "test/suite/support/callbacks.h"
28#include "test/suite/support/hal.h"
29
Myles Watson911d1ae2016-11-28 16:44:40 -080030static const bt_uuid_t HFP_UUID = {{0x00, 0x00, 0x11, 0x1E, 0x00, 0x00, 0x10,
31 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B,
32 0x34, 0xFB}};
33static const bt_uuid_t HFP_AG_UUID = {{0x00, 0x00, 0x11, 0x1F, 0x00, 0x00, 0x10,
34 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B,
35 0x34, 0xFB}};
Sharvil Nanavati0cb995b2014-12-11 23:19:44 -080036
Myles Watson911d1ae2016-11-28 16:44:40 -080037const bt_interface_t* bt_interface;
Chris Mantona6cee152014-10-09 16:04:05 -070038
Jakub Pawlowskia484a882017-06-24 17:30:18 -070039RawAddress bt_remote_bdaddr;
Chris Mantona6cee152014-10-09 16:04:05 -070040
41static int f_verbose;
42static bool discover = false;
43static bool discoverable = false;
44static bool bond = false;
45static bool up = false;
46static bool get_name = false;
47static bool set_name = false;
Sharvil Nanavati0cb995b2014-12-11 23:19:44 -080048static bool sco_listen = false;
49static bool sco_connect = false;
Chris Mantona6cee152014-10-09 16:04:05 -070050
51static int timeout_in_sec = 30;
Myles Watson911d1ae2016-11-28 16:44:40 -080052static char* bd_name;
Chris Mantona6cee152014-10-09 16:04:05 -070053
54static struct option long_options[] = {
Myles Watson911d1ae2016-11-28 16:44:40 -080055 {"bdaddr", required_argument, 0, 0}, {"discover", no_argument, 0, 0},
56 {"discoverable", no_argument, 0, 0}, {"time", required_argument, 0, 0},
57 {"bond", no_argument, 0, 0}, {"up", no_argument, 0, 0},
58 {"verbose", no_argument, 0, 0}, {"get_name", no_argument, 0, 0},
59 {"set_name", required_argument, 0, 0}, {"sco_listen", no_argument, 0, 0},
60 {"sco_connect", no_argument, 0, 0}, {0, 0, 0, 0}};
Chris Mantona6cee152014-10-09 16:04:05 -070061
Myles Watson911d1ae2016-11-28 16:44:40 -080062static void usage(const char* name);
63static bool parse_args(int argc, char** argv);
Chris Mantona6cee152014-10-09 16:04:05 -070064static void sig_handler(int signo);
65
Myles Watson911d1ae2016-11-28 16:44:40 -080066bt_property_t* adapter_get_property(bt_property_type_t type);
Chris Mantona6cee152014-10-09 16:04:05 -070067
Myles Watson911d1ae2016-11-28 16:44:40 -080068int main(int argc, char** argv) {
Chris Mantona6cee152014-10-09 16:04:05 -070069 if (!parse_args(argc, argv)) {
70 usage(argv[0]);
71 }
72
73 if (bond && discoverable) {
74 fprintf(stderr, "Can only select either bond or discoverable, not both\n");
75 usage(argv[0]);
76 }
77
Sharvil Nanavati0cb995b2014-12-11 23:19:44 -080078 if (sco_listen && sco_connect) {
Myles Watson911d1ae2016-11-28 16:44:40 -080079 fprintf(stderr,
80 "Can only select either sco_listen or sco_connect, not both\n");
Sharvil Nanavati0cb995b2014-12-11 23:19:44 -080081 usage(argv[0]);
82 }
83
Myles Watson911d1ae2016-11-28 16:44:40 -080084 if (!bond && !discover && !discoverable && !up && !get_name && !set_name &&
85 !sco_listen && !sco_connect) {
Chris Mantona6cee152014-10-09 16:04:05 -070086 fprintf(stderr, "Must specify one command\n");
87 usage(argv[0]);
88 }
89
90 if (signal(SIGINT, sig_handler) == SIG_ERR) {
91 fprintf(stderr, "Will be unable to catch signals\n");
92 }
93
94 fprintf(stdout, "Bringing up bluetooth adapter\n");
95 if (!hal_open(callbacks_get_adapter_struct())) {
96 fprintf(stderr, "Unable to open Bluetooth HAL.\n");
97 return 1;
98 }
99
100 if (discover) {
101 CALL_AND_WAIT(bt_interface->enable(), adapter_state_changed);
102 fprintf(stdout, "BT adapter is up\n");
103
104 fprintf(stdout, "Starting to start discovery\n");
105 CALL_AND_WAIT(bt_interface->start_discovery(), discovery_state_changed);
106 fprintf(stdout, "Started discovery for %d seconds\n", timeout_in_sec);
107
108 sleep(timeout_in_sec);
109
110 fprintf(stdout, "Starting to cancel discovery\n");
111 CALL_AND_WAIT(bt_interface->cancel_discovery(), discovery_state_changed);
112 fprintf(stdout, "Cancelled discovery after %d seconds\n", timeout_in_sec);
113 }
114
115 if (discoverable) {
116 CALL_AND_WAIT(bt_interface->enable(), adapter_state_changed);
117 fprintf(stdout, "BT adapter is up\n");
118
Myles Watson911d1ae2016-11-28 16:44:40 -0800119 bt_property_t* property =
120 property_new_scan_mode(BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
Chris Mantona6cee152014-10-09 16:04:05 -0700121
122 int rc = bt_interface->set_adapter_property(property);
Myles Watson911d1ae2016-11-28 16:44:40 -0800123 fprintf(stdout, "Set rc:%d device as discoverable for %d seconds\n", rc,
124 timeout_in_sec);
Chris Mantona6cee152014-10-09 16:04:05 -0700125
126 sleep(timeout_in_sec);
127
128 property_free(property);
129 }
130
Myles Watson911d1ae2016-11-28 16:44:40 -0800131 if (bond) {
Chris Mantona6cee152014-10-09 16:04:05 -0700132 if (bdaddr_is_empty(&bt_remote_bdaddr)) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800133 fprintf(stderr,
134 "Must specify a remote device address [ "
135 "--bdaddr=xx:yy:zz:aa:bb:cc ]\n");
Chris Mantona6cee152014-10-09 16:04:05 -0700136 exit(1);
137 }
138
139 CALL_AND_WAIT(bt_interface->enable(), adapter_state_changed);
140 fprintf(stdout, "BT adapter is up\n");
141
Myles Watson911d1ae2016-11-28 16:44:40 -0800142 int rc = bt_interface->create_bond(
143 &bt_remote_bdaddr, 0 /* UNKNOWN; Currently not documented :( */);
Chris Mantona6cee152014-10-09 16:04:05 -0700144 fprintf(stdout, "Started bonding:%d for %d seconds\n", rc, timeout_in_sec);
145
146 sleep(timeout_in_sec);
147 }
148
149 if (up) {
150 CALL_AND_WAIT(bt_interface->enable(), adapter_state_changed);
151 fprintf(stdout, "BT adapter is up\n");
152
153 fprintf(stdout, "Waiting for %d seconds\n", timeout_in_sec);
154 sleep(timeout_in_sec);
155 }
156
157 if (get_name) {
158 CALL_AND_WAIT(bt_interface->enable(), adapter_state_changed);
159 fprintf(stdout, "BT adapter is up\n");
Chris Mantona2253362014-10-17 10:58:53 -0700160 int error;
Myles Watson911d1ae2016-11-28 16:44:40 -0800161 CALL_AND_WAIT(
162 error = bt_interface->get_adapter_property(BT_PROPERTY_BDNAME),
163 adapter_properties);
Chris Mantona2253362014-10-17 10:58:53 -0700164 if (error != BT_STATUS_SUCCESS) {
165 fprintf(stderr, "Unable to get adapter property\n");
166 exit(1);
167 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800168 bt_property_t* property = adapter_get_property(BT_PROPERTY_BDNAME);
169 const bt_bdname_t* name = property_as_name(property);
Chris Manton3252cf02015-01-08 10:47:59 -0800170 if (name)
171 printf("Queried bluetooth device name:%s\n", name->name);
Chris Mantona6cee152014-10-09 16:04:05 -0700172 else
173 printf("No name\n");
174 }
175
176 if (set_name) {
177 CALL_AND_WAIT(bt_interface->enable(), adapter_state_changed);
178 fprintf(stdout, "BT adapter is up\n");
179
Myles Watson911d1ae2016-11-28 16:44:40 -0800180 bt_property_t* property = property_new_name(bd_name);
Chris Mantona6cee152014-10-09 16:04:05 -0700181 printf("Setting bluetooth device name to:%s\n", bd_name);
Chris Mantona2253362014-10-17 10:58:53 -0700182 int error;
Myles Watson911d1ae2016-11-28 16:44:40 -0800183 CALL_AND_WAIT(error = bt_interface->set_adapter_property(property),
184 adapter_properties);
Chris Mantona2253362014-10-17 10:58:53 -0700185 if (error != BT_STATUS_SUCCESS) {
186 fprintf(stderr, "Unable to set adapter property\n");
187 exit(1);
188 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800189 CALL_AND_WAIT(
190 error = bt_interface->get_adapter_property(BT_PROPERTY_BDNAME),
191 adapter_properties);
Chris Mantona2253362014-10-17 10:58:53 -0700192 if (error != BT_STATUS_SUCCESS) {
193 fprintf(stderr, "Unable to get adapter property\n");
194 exit(1);
195 }
Chris Mantona6cee152014-10-09 16:04:05 -0700196 property_free(property);
197 sleep(timeout_in_sec);
198 }
199
Adam Lesinski0620f972015-12-02 22:15:08 -0800200 const int app_uid = 0;
201
Sharvil Nanavati0cb995b2014-12-11 23:19:44 -0800202 if (sco_listen) {
203 CALL_AND_WAIT(bt_interface->enable(), adapter_state_changed);
204 fprintf(stdout, "BT adapter is up\n");
205
Myles Watson911d1ae2016-11-28 16:44:40 -0800206 bt_property_t* property =
207 property_new_scan_mode(BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
208 CALL_AND_WAIT(bt_interface->set_adapter_property(property),
209 adapter_properties);
Sharvil Nanavati0cb995b2014-12-11 23:19:44 -0800210 property_free(property);
211
Myles Watson911d1ae2016-11-28 16:44:40 -0800212 const btsock_interface_t* sock =
213 bt_interface->get_profile_interface(BT_PROFILE_SOCKETS_ID);
Sharvil Nanavati0cb995b2014-12-11 23:19:44 -0800214
215 int rfcomm_fd = INVALID_FD;
Myles Watson911d1ae2016-11-28 16:44:40 -0800216 int error =
217 sock->listen(BTSOCK_RFCOMM, "meow", (const uint8_t*)&HFP_AG_UUID, 0,
218 &rfcomm_fd, 0, app_uid);
Sharvil Nanavati0cb995b2014-12-11 23:19:44 -0800219 if (error != BT_STATUS_SUCCESS) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800220 fprintf(stderr, "Unable to listen for incoming RFCOMM socket: %d\n",
221 error);
Sharvil Nanavati0cb995b2014-12-11 23:19:44 -0800222 exit(1);
223 }
224
225 int sock_fd = INVALID_FD;
Adam Lesinski0620f972015-12-02 22:15:08 -0800226 error = sock->listen(BTSOCK_SCO, NULL, NULL, 5, &sock_fd, 0, app_uid);
Sharvil Nanavati0cb995b2014-12-11 23:19:44 -0800227 if (error != BT_STATUS_SUCCESS) {
228 fprintf(stderr, "Unable to listen for incoming SCO sockets: %d\n", error);
229 exit(1);
230 }
231 fprintf(stdout, "Waiting for incoming SCO connections...\n");
232 sleep(timeout_in_sec);
233 }
234
235 if (sco_connect) {
236 if (bdaddr_is_empty(&bt_remote_bdaddr)) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800237 fprintf(stderr,
238 "Must specify a remote device address [ "
239 "--bdaddr=xx:yy:zz:aa:bb:cc ]\n");
Sharvil Nanavati0cb995b2014-12-11 23:19:44 -0800240 exit(1);
241 }
242
243 CALL_AND_WAIT(bt_interface->enable(), adapter_state_changed);
244 fprintf(stdout, "BT adapter is up\n");
245
Myles Watson911d1ae2016-11-28 16:44:40 -0800246 const btsock_interface_t* sock =
247 bt_interface->get_profile_interface(BT_PROFILE_SOCKETS_ID);
Sharvil Nanavati0cb995b2014-12-11 23:19:44 -0800248
249 int rfcomm_fd = INVALID_FD;
Myles Watson911d1ae2016-11-28 16:44:40 -0800250 int error =
251 sock->connect(&bt_remote_bdaddr, BTSOCK_RFCOMM,
252 (const uint8_t*)&HFP_AG_UUID, 0, &rfcomm_fd, 0, app_uid);
Sharvil Nanavati0cb995b2014-12-11 23:19:44 -0800253 if (error != BT_STATUS_SUCCESS) {
254 fprintf(stderr, "Unable to connect to RFCOMM socket: %d.\n", error);
255 exit(1);
256 }
257
258 WAIT(acl_state_changed);
259
260 fprintf(stdout, "Establishing SCO connection...\n");
261
262 int sock_fd = INVALID_FD;
Myles Watson911d1ae2016-11-28 16:44:40 -0800263 error = sock->connect(&bt_remote_bdaddr, BTSOCK_SCO, NULL, 5, &sock_fd, 0,
264 app_uid);
Sharvil Nanavati0cb995b2014-12-11 23:19:44 -0800265 if (error != BT_STATUS_SUCCESS) {
266 fprintf(stderr, "Unable to connect to SCO socket: %d.\n", error);
267 exit(1);
268 }
269 sleep(timeout_in_sec);
270 }
271
Chris Mantona6cee152014-10-09 16:04:05 -0700272 CALL_AND_WAIT(bt_interface->disable(), adapter_state_changed);
273 fprintf(stdout, "BT adapter is down\n");
274}
275
276static void sig_handler(int signo) {
277 if (signo == SIGINT) {
278 fprintf(stderr, "Received SIGINT\n");
279 CALL_AND_WAIT(bt_interface->disable(), adapter_state_changed);
280 fprintf(stderr, "BT adapter is down\n");
281 exit(1);
282 }
283}
284
Myles Watson911d1ae2016-11-28 16:44:40 -0800285static void usage(const char* name) {
286 fprintf(stderr,
287 "Usage: %s "
288 "[--bond|--discover|--discoverable|--up|--sco_listen|--sco_connect] "
289 "[--bdaddr=<bdaddr>] [--time=<time_in_sec>] --verbose\n",
290 name);
Chris Mantona6cee152014-10-09 16:04:05 -0700291 fprintf(stderr, " bond: Discover actively advertising devices\n");
292 fprintf(stderr, " discover: Discover actively advertising devices\n");
Myles Watson911d1ae2016-11-28 16:44:40 -0800293 fprintf(stderr,
294 " discoverable: Set into a connectable and discoverable mode\n");
Chris Mantona6cee152014-10-09 16:04:05 -0700295 fprintf(stderr, " up: Only bring up stack\n");
Sharvil Nanavati0cb995b2014-12-11 23:19:44 -0800296 fprintf(stderr, " sco_listen: Listen for incoming SCO connections\n");
Myles Watson911d1ae2016-11-28 16:44:40 -0800297 fprintf(stderr,
298 " sco_connect: Establish a SCO connection with another device\n");
Chris Mantona6cee152014-10-09 16:04:05 -0700299 fprintf(stderr, " time: Time to hold in the specified mode\n");
300 exit(1);
301}
302
Myles Watson911d1ae2016-11-28 16:44:40 -0800303static bool parse_args(int argc, char** argv) {
Chris Mantona6cee152014-10-09 16:04:05 -0700304 while (1) {
305 int option_index = 0;
306 int c = getopt_long_only(argc, argv, "", long_options, &option_index);
Myles Watson911d1ae2016-11-28 16:44:40 -0800307 if (c != 0) break;
Chris Mantona6cee152014-10-09 16:04:05 -0700308
309 switch (c) {
310 case 0:
311 if (option_index == 0) {
312 if (!string_to_bdaddr(optarg, &bt_remote_bdaddr)) {
313 return false;
314 }
315 }
316 if (option_index == 1) {
317 discover = true;
318 }
319 if (option_index == 2) {
320 discoverable = true;
321 }
322 if (option_index == 3) {
323 timeout_in_sec = atoi(optarg);
324 }
325 if (option_index == 4) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800326 bond = true;
Chris Mantona6cee152014-10-09 16:04:05 -0700327 }
328 if (option_index == 5) {
329 up = true;
330 }
331 if (option_index == 6) {
332 f_verbose++;
333 }
334 if (option_index == 7) {
335 get_name = true;
336 }
337 if (option_index == 8) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800338 bd_name = (char*)optarg;
Chris Mantona6cee152014-10-09 16:04:05 -0700339 set_name = true;
340 }
Sharvil Nanavati0cb995b2014-12-11 23:19:44 -0800341 if (option_index == 9) {
342 sco_listen = true;
343 }
344 if (option_index == 10) {
345 sco_connect = true;
346 }
347 break;
Chris Mantona6cee152014-10-09 16:04:05 -0700348
349 default:
350 fprintf(stderr, "?? getopt returned character code 0%o ??\n", c);
351 }
352 }
353
354 if (optind < argc) {
355 fprintf(stderr, "non-option ARGV-elements: ");
Myles Watson911d1ae2016-11-28 16:44:40 -0800356 while (optind < argc) fprintf(stderr, "%s ", argv[optind++]);
Chris Mantona6cee152014-10-09 16:04:05 -0700357 fprintf(stderr, "\n");
358 return false;
359 }
360 return true;
361}