1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
/******************************************************************************
*
* Copyright 2014 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
#include <getopt.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include "btcore/include/property.h"
#include "osi/include/osi.h"
#include "test/suite/support/callbacks.h"
#include "test/suite/support/hal.h"
#include "types/bluetooth/uuid.h"
#include "types/raw_address.h"
static const Uuid HFP_UUID =
Uuid::From128BitBE({{0x00, 0x00, 0x11, 0x1E, 0x00, 0x00, 0x10, 0x00, 0x80,
0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB}});
static const Uuid HFP_AG_UUID =
Uuid::From128BitBE({{0x00, 0x00, 0x11, 0x1F, 0x00, 0x00, 0x10, 0x00, 0x80,
0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB}});
const bt_interface_t* bt_interface;
RawAddress bt_remote_bdaddr;
static int f_verbose;
static bool discover = false;
static bool discoverable = false;
static bool bond = false;
static bool up = false;
static bool get_name = false;
static bool set_name = false;
static bool sco_listen = false;
static bool sco_connect = false;
static int timeout_in_sec = 30;
static char* bd_name;
static struct option long_options[] = {
{"bdaddr", required_argument, 0, 0}, {"discover", no_argument, 0, 0},
{"discoverable", no_argument, 0, 0}, {"time", required_argument, 0, 0},
{"bond", no_argument, 0, 0}, {"up", no_argument, 0, 0},
{"verbose", no_argument, 0, 0}, {"get_name", no_argument, 0, 0},
{"set_name", required_argument, 0, 0}, {"sco_listen", no_argument, 0, 0},
{"sco_connect", no_argument, 0, 0}, {0, 0, 0, 0}};
static void usage(const char* name);
static bool parse_args(int argc, char** argv);
static void sig_handler(int signo);
bt_property_t* adapter_get_property(bt_property_type_t type);
int main(int argc, char** argv) {
if (!parse_args(argc, argv)) {
usage(argv[0]);
}
if (bond && discoverable) {
fprintf(stderr, "Can only select either bond or discoverable, not both\n");
usage(argv[0]);
}
if (sco_listen && sco_connect) {
fprintf(stderr,
"Can only select either sco_listen or sco_connect, not both\n");
usage(argv[0]);
}
if (!bond && !discover && !discoverable && !up && !get_name && !set_name &&
!sco_listen && !sco_connect) {
fprintf(stderr, "Must specify one command\n");
usage(argv[0]);
}
if (signal(SIGINT, sig_handler) == SIG_ERR) {
fprintf(stderr, "Will be unable to catch signals\n");
}
fprintf(stdout, "Bringing up bluetooth adapter\n");
if (!hal_open(callbacks_get_adapter_struct())) {
fprintf(stderr, "Unable to open Bluetooth HAL.\n");
return 1;
}
if (discover) {
CALL_AND_WAIT(bt_interface->enable(), adapter_state_changed);
fprintf(stdout, "BT adapter is up\n");
fprintf(stdout, "Starting to start discovery\n");
CALL_AND_WAIT(bt_interface->start_discovery(), discovery_state_changed);
fprintf(stdout, "Started discovery for %d seconds\n", timeout_in_sec);
sleep(timeout_in_sec);
fprintf(stdout, "Starting to cancel discovery\n");
CALL_AND_WAIT(bt_interface->cancel_discovery(), discovery_state_changed);
fprintf(stdout, "Cancelled discovery after %d seconds\n", timeout_in_sec);
}
if (discoverable) {
CALL_AND_WAIT(bt_interface->enable(), adapter_state_changed);
fprintf(stdout, "BT adapter is up\n");
bt_property_t* property =
property_new_scan_mode(BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
int rc = bt_interface->set_adapter_property(property);
fprintf(stdout, "Set rc:%d device as discoverable for %d seconds\n", rc,
timeout_in_sec);
sleep(timeout_in_sec);
property_free(property);
}
if (bond) {
if (bdaddr_is_empty(&bt_remote_bdaddr)) {
fprintf(stderr,
"Must specify a remote device address [ "
"--bdaddr=xx:yy:zz:aa:bb:cc ]\n");
exit(1);
}
CALL_AND_WAIT(bt_interface->enable(), adapter_state_changed);
fprintf(stdout, "BT adapter is up\n");
int rc = bt_interface->create_bond(
&bt_remote_bdaddr, 0 /* UNKNOWN; Currently not documented :( */);
fprintf(stdout, "Started bonding:%d for %d seconds\n", rc, timeout_in_sec);
sleep(timeout_in_sec);
}
if (up) {
CALL_AND_WAIT(bt_interface->enable(), adapter_state_changed);
fprintf(stdout, "BT adapter is up\n");
fprintf(stdout, "Waiting for %d seconds\n", timeout_in_sec);
sleep(timeout_in_sec);
}
if (get_name) {
CALL_AND_WAIT(bt_interface->enable(), adapter_state_changed);
fprintf(stdout, "BT adapter is up\n");
int error;
CALL_AND_WAIT(
error = bt_interface->get_adapter_property(BT_PROPERTY_BDNAME),
adapter_properties);
if (error != BT_STATUS_SUCCESS) {
fprintf(stderr, "Unable to get adapter property\n");
exit(1);
}
bt_property_t* property = adapter_get_property(BT_PROPERTY_BDNAME);
const bt_bdname_t* name = property_as_name(property);
if (name)
printf("Queried bluetooth device name:%s\n", name->name);
else
printf("No name\n");
}
if (set_name) {
CALL_AND_WAIT(bt_interface->enable(), adapter_state_changed);
fprintf(stdout, "BT adapter is up\n");
bt_property_t* property = property_new_name(bd_name);
printf("Setting bluetooth device name to:%s\n", bd_name);
int error;
CALL_AND_WAIT(error = bt_interface->set_adapter_property(property),
adapter_properties);
if (error != BT_STATUS_SUCCESS) {
fprintf(stderr, "Unable to set adapter property\n");
exit(1);
}
CALL_AND_WAIT(
error = bt_interface->get_adapter_property(BT_PROPERTY_BDNAME),
adapter_properties);
if (error != BT_STATUS_SUCCESS) {
fprintf(stderr, "Unable to get adapter property\n");
exit(1);
}
property_free(property);
sleep(timeout_in_sec);
}
const int app_uid = 0;
if (sco_listen) {
CALL_AND_WAIT(bt_interface->enable(), adapter_state_changed);
fprintf(stdout, "BT adapter is up\n");
bt_property_t* property =
property_new_scan_mode(BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
CALL_AND_WAIT(bt_interface->set_adapter_property(property),
adapter_properties);
property_free(property);
const btsock_interface_t* sock =
bt_interface->get_profile_interface(BT_PROFILE_SOCKETS_ID);
int rfcomm_fd = INVALID_FD;
int error =
sock->listen(BTSOCK_RFCOMM, "meow", (const uint8_t*)&HFP_AG_UUID, 0,
&rfcomm_fd, 0, app_uid);
if (error != BT_STATUS_SUCCESS) {
fprintf(stderr, "Unable to listen for incoming RFCOMM socket: %d\n",
error);
exit(1);
}
int sock_fd = INVALID_FD;
error = sock->listen(BTSOCK_SCO, NULL, NULL, 5, &sock_fd, 0, app_uid);
if (error != BT_STATUS_SUCCESS) {
fprintf(stderr, "Unable to listen for incoming SCO sockets: %d\n", error);
exit(1);
}
fprintf(stdout, "Waiting for incoming SCO connections...\n");
sleep(timeout_in_sec);
}
if (sco_connect) {
if (bdaddr_is_empty(&bt_remote_bdaddr)) {
fprintf(stderr,
"Must specify a remote device address [ "
"--bdaddr=xx:yy:zz:aa:bb:cc ]\n");
exit(1);
}
CALL_AND_WAIT(bt_interface->enable(), adapter_state_changed);
fprintf(stdout, "BT adapter is up\n");
const btsock_interface_t* sock =
bt_interface->get_profile_interface(BT_PROFILE_SOCKETS_ID);
int rfcomm_fd = INVALID_FD;
int error =
sock->connect(&bt_remote_bdaddr, BTSOCK_RFCOMM,
(const uint8_t*)&HFP_AG_UUID, 0, &rfcomm_fd, 0, app_uid);
if (error != BT_STATUS_SUCCESS) {
fprintf(stderr, "Unable to connect to RFCOMM socket: %d.\n", error);
exit(1);
}
WAIT(acl_state_changed);
fprintf(stdout, "Establishing SCO connection...\n");
int sock_fd = INVALID_FD;
error = sock->connect(&bt_remote_bdaddr, BTSOCK_SCO, NULL, 5, &sock_fd, 0,
app_uid);
if (error != BT_STATUS_SUCCESS) {
fprintf(stderr, "Unable to connect to SCO socket: %d.\n", error);
exit(1);
}
sleep(timeout_in_sec);
}
CALL_AND_WAIT(bt_interface->disable(), adapter_state_changed);
fprintf(stdout, "BT adapter is down\n");
}
static void sig_handler(int signo) {
if (signo == SIGINT) {
fprintf(stderr, "Received SIGINT\n");
CALL_AND_WAIT(bt_interface->disable(), adapter_state_changed);
fprintf(stderr, "BT adapter is down\n");
exit(1);
}
}
static void usage(const char* name) {
fprintf(stderr,
"Usage: %s "
"[--bond|--discover|--discoverable|--up|--sco_listen|--sco_connect] "
"[--bdaddr=<bdaddr>] [--time=<time_in_sec>] --verbose\n",
name);
fprintf(stderr, " bond: Discover actively advertising devices\n");
fprintf(stderr, " discover: Discover actively advertising devices\n");
fprintf(stderr,
" discoverable: Set into a connectable and discoverable mode\n");
fprintf(stderr, " up: Only bring up stack\n");
fprintf(stderr, " sco_listen: Listen for incoming SCO connections\n");
fprintf(stderr,
" sco_connect: Establish a SCO connection with another device\n");
fprintf(stderr, " time: Time to hold in the specified mode\n");
exit(1);
}
static bool parse_args(int argc, char** argv) {
while (1) {
int option_index = 0;
int c = getopt_long_only(argc, argv, "", long_options, &option_index);
if (c != 0) break;
switch (c) {
case 0:
if (option_index == 0) {
if (!string_to_bdaddr(optarg, &bt_remote_bdaddr)) {
return false;
}
}
if (option_index == 1) {
discover = true;
}
if (option_index == 2) {
discoverable = true;
}
if (option_index == 3) {
timeout_in_sec = atoi(optarg);
}
if (option_index == 4) {
bond = true;
}
if (option_index == 5) {
up = true;
}
if (option_index == 6) {
f_verbose++;
}
if (option_index == 7) {
get_name = true;
}
if (option_index == 8) {
bd_name = (char*)optarg;
set_name = true;
}
if (option_index == 9) {
sco_listen = true;
}
if (option_index == 10) {
sco_connect = true;
}
break;
default:
fprintf(stderr, "?? getopt returned character code 0%o ??\n", c);
}
}
if (optind < argc) {
fprintf(stderr, "non-option ARGV-elements: ");
while (optind < argc) fprintf(stderr, "%s ", argv[optind++]);
fprintf(stderr, "\n");
return false;
}
return true;
}
|