1 /*
2  * Copyright (C) 2021 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 std::shared_ptrecific language governing permissions and
14  * limitations under the License.
15  */
16 #include <aidl/Gtest.h>
17 #include <aidl/Vintf.h>
18 #include <aidl/android/hardware/uwb/BnUwbClientCallback.h>
19 #include <aidl/android/hardware/uwb/IUwb.h>
20 #include <aidl/android/hardware/uwb/IUwbChip.h>
21 #include <aidl/android/hardware/uwb/IUwbClientCallback.h>
22 #include <android/binder_auto_utils.h>
23 #include <android/binder_manager.h>
24 #include <binder/IServiceManager.h>
25 #include <binder/ProcessState.h>
26 
27 #include <future>
28 
29 using aidl::android::hardware::uwb::BnUwbClientCallback;
30 using aidl::android::hardware::uwb::IUwb;
31 using aidl::android::hardware::uwb::IUwbChip;
32 using aidl::android::hardware::uwb::IUwbClientCallback;
33 using aidl::android::hardware::uwb::UwbEvent;
34 using aidl::android::hardware::uwb::UwbStatus;
35 using android::ProcessState;
36 using android::String16;
37 using ndk::ScopedAStatus;
38 using ndk::SpAIBinder;
39 
40 namespace {
41 constexpr static int kCallbackTimeoutMs = 250;
42 // this timeout should be same as AOSP stack timeout (HAL_OPEN_TIMEOUT_MS)
43 constexpr static int kOpenCallbackTimeoutMs = 20000;
44 }  // namespace
45 
46 class UwbClientCallback : public BnUwbClientCallback {
47   public:
UwbClientCallback(const std::function<void (const std::vector<uint8_t> &)> & on_uci_message_cb,const std::function<void (UwbEvent,UwbStatus)> & on_hal_event_cb)48     UwbClientCallback(const std::function<void(const std::vector<uint8_t>&)>& on_uci_message_cb,
49                       const std::function<void(UwbEvent, UwbStatus)>& on_hal_event_cb)
50         : on_uci_message_cb_(on_uci_message_cb), on_hal_event_cb_(on_hal_event_cb) {}
51 
onUciMessage(const std::vector<uint8_t> & data)52     ScopedAStatus onUciMessage(const std::vector<uint8_t>& data) override {
53         on_uci_message_cb_(data);
54         return ScopedAStatus::ok();
55     }
56 
onHalEvent(UwbEvent uwb_event,UwbStatus uwb_status)57     ScopedAStatus onHalEvent(UwbEvent uwb_event, UwbStatus uwb_status) override {
58         on_hal_event_cb_(uwb_event, uwb_status);
59         return ScopedAStatus::ok();
60     }
61 
62   private:
63     std::function<void(const std::vector<uint8_t>&)> on_uci_message_cb_;
64     std::function<void(UwbEvent, UwbStatus)> on_hal_event_cb_;
65 };
66 
67 class UwbAidl : public testing::TestWithParam<std::string> {
68   public:
SetUp()69     virtual void SetUp() override {
70         iuwb_ = IUwb::fromBinder(SpAIBinder(AServiceManager_waitForService(GetParam().c_str())));
71         ASSERT_NE(iuwb_, nullptr);
72     }
TearDown()73     virtual void TearDown() override {
74         // Trigger HAL close at end of each test.
75         const auto iuwb_chip = getAnyChip();
76         iuwb_chip->close();
77     }
78     std::shared_ptr<IUwb> iuwb_;
79 
80     // TODO (b/197638976): We pick the first chip here. Need to fix this
81     // for supporting multiple chips in the future.
getAnyChipName()82     std::string getAnyChipName() {
83         std::vector<std::string> chip_names;
84         ScopedAStatus status = iuwb_->getChips(&chip_names);
85         EXPECT_TRUE(status.isOk());
86         EXPECT_FALSE(chip_names.empty());
87         return chip_names[0];
88     }
89 
90     // TODO (b/197638976): We pick the first chip here. Need to fix this
91     // for supporting multiple chips in the future.
getAnyChip()92     std::shared_ptr<IUwbChip> getAnyChip() {
93         std::shared_ptr<IUwbChip> iuwb_chip;
94         ScopedAStatus status = iuwb_->getChip(getAnyChipName(), &iuwb_chip);
95         EXPECT_TRUE(status.isOk());
96         EXPECT_NE(iuwb_chip, nullptr);
97         return iuwb_chip;
98     }
99 
getAnyChipAndOpen()100     std::shared_ptr<IUwbChip> getAnyChipAndOpen() {
101         std::promise<void> open_cb_promise;
102         std::future<void> open_cb_future{open_cb_promise.get_future()};
103         std::shared_ptr<UwbClientCallback> callback = ndk::SharedRefBase::make<UwbClientCallback>(
104                 [](auto /* data */) {},
105                 [&open_cb_promise](auto event, auto /* status */) {
106                     if (event == UwbEvent::OPEN_CPLT) {
107                         open_cb_promise.set_value();
108                     }
109                 });
110         std::chrono::milliseconds timeout{kOpenCallbackTimeoutMs};
111         const auto iuwb_chip = getAnyChip();
112         EXPECT_TRUE(iuwb_chip->open(callback).isOk());
113         EXPECT_EQ(open_cb_future.wait_for(timeout), std::future_status::ready);
114         return iuwb_chip;
115     }
116 };
117 
TEST_P(UwbAidl,GetChips)118 TEST_P(UwbAidl, GetChips) {
119     std::vector<std::string> chip_names;
120     ScopedAStatus status = iuwb_->getChips(&chip_names);
121     EXPECT_TRUE(status.isOk());
122     EXPECT_FALSE(chip_names.empty());
123 }
124 
TEST_P(UwbAidl,GetChip)125 TEST_P(UwbAidl, GetChip) {
126     std::shared_ptr<IUwbChip> iuwb_chip;
127     ScopedAStatus status = iuwb_->getChip(getAnyChipName(), &iuwb_chip);
128     EXPECT_TRUE(status.isOk());
129     EXPECT_NE(iuwb_chip, nullptr);
130 }
131 
TEST_P(UwbAidl,ChipOpen)132 TEST_P(UwbAidl, ChipOpen) {
133     std::promise<void> open_cb_promise;
134     std::future<void> open_cb_future{open_cb_promise.get_future()};
135     std::shared_ptr<UwbClientCallback> callback = ndk::SharedRefBase::make<UwbClientCallback>(
136             [](auto /* data */) {},
137             [&open_cb_promise](auto event, auto /* status */) {
138                 if (event == UwbEvent::OPEN_CPLT) {
139                     open_cb_promise.set_value();
140                 }
141             });
142     std::chrono::milliseconds timeout{kOpenCallbackTimeoutMs};
143     const auto iuwb_chip = getAnyChip();
144     EXPECT_TRUE(iuwb_chip->open(callback).isOk());
145     EXPECT_EQ(open_cb_future.wait_for(timeout), std::future_status::ready);
146 }
147 
TEST_P(UwbAidl,ChipClose)148 TEST_P(UwbAidl, ChipClose) {
149     std::promise<void> open_cb_promise;
150     std::future<void> open_cb_future{open_cb_promise.get_future()};
151     std::promise<void> close_cb_promise;
152     std::future<void> close_cb_future{close_cb_promise.get_future()};
153     std::shared_ptr<UwbClientCallback> callback = ndk::SharedRefBase::make<UwbClientCallback>(
154             [](auto /* data */) {},
155             [&open_cb_promise, &close_cb_promise](auto event, auto /* status */) {
156                 if (event == UwbEvent::OPEN_CPLT) {
157                     open_cb_promise.set_value();
158                 }
159                 if (event == UwbEvent::CLOSE_CPLT) {
160                     close_cb_promise.set_value();
161                 }
162             });
163     std::chrono::milliseconds open_timeout{kOpenCallbackTimeoutMs};
164     std::chrono::milliseconds close_timeout{kCallbackTimeoutMs};
165     const auto iuwb_chip = getAnyChip();
166     EXPECT_TRUE(iuwb_chip->open(callback).isOk());
167     EXPECT_EQ(open_cb_future.wait_for(open_timeout), std::future_status::ready);
168     EXPECT_TRUE(iuwb_chip->close().isOk());
169     EXPECT_EQ(close_cb_future.wait_for(close_timeout), std::future_status::ready);
170 }
171 
TEST_P(UwbAidl,ChipCoreInit)172 TEST_P(UwbAidl, ChipCoreInit) {
173     const auto iuwb_chip = getAnyChipAndOpen();
174     EXPECT_TRUE(iuwb_chip->coreInit().isOk());
175 }
176 
TEST_P(UwbAidl,ChipSessionInit)177 TEST_P(UwbAidl, ChipSessionInit) {
178     const auto iuwb_chip = getAnyChipAndOpen();
179     EXPECT_TRUE(iuwb_chip->sessionInit(0).isOk());
180 }
181 
TEST_P(UwbAidl,ChipGetSupportedAndroidUciVersion)182 TEST_P(UwbAidl, ChipGetSupportedAndroidUciVersion) {
183     const auto iuwb_chip = getAnyChipAndOpen();
184     EXPECT_TRUE(iuwb_chip->coreInit().isOk());
185 
186     int32_t version;
187     EXPECT_TRUE(iuwb_chip->getSupportedAndroidUciVersion(&version).isOk());
188     EXPECT_GT(version, 0);
189 }
190 
TEST_P(UwbAidl,ChipGetName)191 TEST_P(UwbAidl, ChipGetName) {
192     std::string chip_name = getAnyChipName();
193     std::shared_ptr<IUwbChip> iuwb_chip;
194     ScopedAStatus status = iuwb_->getChip(chip_name, &iuwb_chip);
195     EXPECT_TRUE(status.isOk());
196     EXPECT_NE(iuwb_chip, nullptr);
197 
198     std::string retrieved_chip_name;
199     status = iuwb_chip->getName(&retrieved_chip_name);
200     EXPECT_TRUE(status.isOk());
201     EXPECT_EQ(retrieved_chip_name, chip_name);
202 }
203 
204 /**
205 TEST_P(UwbAidl, ChipSendUciMessage_GetDeviceInfo) {
206 const auto iuwb_chip = getAnyChipAndOpen(callback);
207 EXPECT_TRUE(iuwb_chip->coreInit(callback).isOk());
208 
209 const std::vector<uint8_t>
210 EXPECT_TRUE(iuwb_chip->sendUciMessage().isOk());
211 } */
212 
213 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(UwbAidl);
214 INSTANTIATE_TEST_SUITE_P(Uwb, UwbAidl,
215                          testing::ValuesIn(android::getAidlHalInstanceNames(IUwb::descriptor)),
216                          android::PrintInstanceNameToString);
217 
main(int argc,char ** argv)218 int main(int argc, char** argv) {
219     ::testing::InitGoogleTest(&argc, argv);
220     ProcessState::self()->setThreadPoolMaxThreadCount(1);
221     ProcessState::self()->startThreadPool();
222     // UWB HAL only allows 1 client, make sure framework
223     // does not have UWB HAL open before running
224     std::system("/system/bin/cmd uwb disable-uwb");
225     sleep(3);
226     auto status = RUN_ALL_TESTS();
227     sleep(3);
228     std::system("/system/bin/cmd uwb enable-uwb");
229     return status;
230 }
231