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 "test/headless/read/name.h"
20 
21 #include <future>
22 
23 #include "stack/btm/neighbor_inquiry.h"
24 #include "stack/include/bt_name.h"
25 #include "stack/include/btm_client_interface.h"
26 #include "stack/include/btm_status.h"
27 #include "stack/include/rnr_interface.h"
28 #include "test/headless/get_options.h"
29 #include "test/headless/headless.h"
30 #include "types/raw_address.h"
31 
32 // TODO(b/369381361) Enfore -Wmissing-prototypes
33 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
34 
35 std::promise<tBTM_REMOTE_DEV_NAME> promise_;
36 
RemoteNameCallback(const tBTM_REMOTE_DEV_NAME * data)37 void RemoteNameCallback(const tBTM_REMOTE_DEV_NAME* data) { promise_.set_value(*data); }
38 
Run()39 int bluetooth::test::headless::Name::Run() {
40   if (options_.loop_ < 1) {
41     fprintf(stdout, "This test requires at least a single loop");
42     options_.Usage();
43     return -1;
44   }
45   if (options_.device_.size() != 1) {
46     fprintf(stdout, "This test requires a single device specified");
47     options_.Usage();
48     return -1;
49   }
50 
51   const RawAddress& raw_address = options_.device_.front();
52 
53   return RunOnHeadlessStack<int>([&raw_address]() {
54     promise_ = std::promise<tBTM_REMOTE_DEV_NAME>();
55 
56     auto future = promise_.get_future();
57 
58     tBTM_STATUS status = get_stack_rnr_interface().BTM_ReadRemoteDeviceName(
59             raw_address, &RemoteNameCallback, BT_TRANSPORT_BR_EDR);
60     if (status != tBTM_STATUS::BTM_CMD_STARTED) {
61       fprintf(stdout, "Failure to start read remote device\n");
62       return -1;
63     }
64 
65     tBTM_REMOTE_DEV_NAME name_packet = future.get();
66     switch (name_packet.btm_status) {
67       case tBTM_STATUS::BTM_SUCCESS: {
68         char buf[BD_NAME_LEN];
69         memcpy(buf, name_packet.remote_bd_name, BD_NAME_LEN);
70         std::string name(buf);
71         fprintf(stdout, "Name result mac:%s name:%s\n", raw_address.ToString().c_str(),
72                 name.c_str());
73       } break;
74       case tBTM_STATUS::BTM_BAD_VALUE_RET:
75         fprintf(stdout, "Name Timeout or other failure");
76         return -2;
77       default:
78         fprintf(stdout, "Unexpected remote name request failure status:%hd",
79                 name_packet.btm_status);
80         return -2;
81     }
82     return 0;
83   });
84 }
85