1 /*
2 * Copyright 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define LOG_TAG "bt_headless_sdp"
18
19 #include <future>
20
21 #include "base/logging.h" // LOG() stdout and android log
22 #include "osi/include/log.h" // android log only
23 #include "stack/include/btm_api.h"
24 #include "stack/include/btm_api_types.h"
25 #include "test/headless/get_options.h"
26 #include "test/headless/headless.h"
27 #include "test/headless/read/name.h"
28 #include "types/raw_address.h"
29
30 std::promise<tBTM_REMOTE_DEV_NAME> promise_;
31
RemoteNameCallback(void * data)32 void RemoteNameCallback(void* data) {
33 promise_.set_value(*static_cast<tBTM_REMOTE_DEV_NAME*>(data));
34 }
35
Run()36 int bluetooth::test::headless::Name::Run() {
37 if (options_.loop_ < 1) {
38 fprintf(stdout, "This test requires at least a single loop");
39 options_.Usage();
40 return -1;
41 }
42 if (options_.device_.size() != 1) {
43 fprintf(stdout, "This test requires a single device specified");
44 options_.Usage();
45 return -1;
46 }
47
48 const RawAddress& raw_address = options_.device_.front();
49
50 return RunOnHeadlessStack<int>([&raw_address]() {
51 promise_ = std::promise<tBTM_REMOTE_DEV_NAME>();
52
53 auto future = promise_.get_future();
54
55 tBTM_STATUS status = BTM_ReadRemoteDeviceName(
56 raw_address, &RemoteNameCallback, BT_TRANSPORT_BR_EDR);
57 if (status != BTM_CMD_STARTED) {
58 fprintf(stdout, "Failure to start read remote device\n");
59 return -1;
60 }
61
62 tBTM_REMOTE_DEV_NAME name_packet = future.get();
63 switch (name_packet.status) {
64 case BTM_SUCCESS: {
65 char buf[BD_NAME_LEN];
66 memcpy(buf, name_packet.remote_bd_name, BD_NAME_LEN);
67 std::string name(buf);
68 fprintf(stdout, "Name result mac:%s name:%s\n",
69 raw_address.ToString().c_str(), name.c_str());
70 } break;
71 case BTM_BAD_VALUE_RET:
72 fprintf(stdout, "Name Timeout or other failure");
73 return -2;
74 default:
75 fprintf(stdout, "Unexpected remote name request failure status:%hd",
76 name_packet.status);
77 return -2;
78 }
79 return 0;
80 });
81 }
82