Roshan Pius | 90f5171 | 2021-09-21 11:09:12 -0700 | [diff] [blame] | 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; |
Bhakthavatsala Raghavendra | 2ad3630 | 2024-03-12 16:12:32 -0700 | [diff] [blame] | 42 | // this timeout should be same as AOSP stack timeout (HAL_OPEN_TIMEOUT_MS) |
| 43 | constexpr static int kOpenCallbackTimeoutMs = 20000; |
Roshan Pius | 90f5171 | 2021-09-21 11:09:12 -0700 | [diff] [blame] | 44 | } // namespace |
| 45 | |
| 46 | class UwbClientCallback : public BnUwbClientCallback { |
| 47 | public: |
| 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 | |
| 52 | ScopedAStatus onUciMessage(const std::vector<uint8_t>& data) override { |
| 53 | on_uci_message_cb_(data); |
| 54 | return ScopedAStatus::ok(); |
| 55 | } |
| 56 | |
| 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: |
| 69 | virtual void SetUp() override { |
| 70 | iuwb_ = IUwb::fromBinder(SpAIBinder(AServiceManager_waitForService(GetParam().c_str()))); |
| 71 | ASSERT_NE(iuwb_, nullptr); |
| 72 | } |
Roshan Pius | 3ba0e63 | 2022-06-10 19:57:41 +0000 | [diff] [blame] | 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 | } |
Roshan Pius | 90f5171 | 2021-09-21 11:09:12 -0700 | [diff] [blame] | 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. |
| 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. |
| 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 | |
| 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 | }); |
Bhakthavatsala Raghavendra | 2ad3630 | 2024-03-12 16:12:32 -0700 | [diff] [blame] | 110 | std::chrono::milliseconds timeout{kOpenCallbackTimeoutMs}; |
Roshan Pius | 90f5171 | 2021-09-21 11:09:12 -0700 | [diff] [blame] | 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 | |
| 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 | |
| 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 | |
| 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 | }); |
Bhakthavatsala Raghavendra | 2ad3630 | 2024-03-12 16:12:32 -0700 | [diff] [blame] | 142 | std::chrono::milliseconds timeout{kOpenCallbackTimeoutMs}; |
Roshan Pius | 90f5171 | 2021-09-21 11:09:12 -0700 | [diff] [blame] | 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 | |
| 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 | }); |
Bhakthavatsala Raghavendra | 2ad3630 | 2024-03-12 16:12:32 -0700 | [diff] [blame] | 163 | std::chrono::milliseconds open_timeout{kOpenCallbackTimeoutMs}; |
| 164 | std::chrono::milliseconds close_timeout{kCallbackTimeoutMs}; |
Roshan Pius | 90f5171 | 2021-09-21 11:09:12 -0700 | [diff] [blame] | 165 | const auto iuwb_chip = getAnyChip(); |
| 166 | EXPECT_TRUE(iuwb_chip->open(callback).isOk()); |
Bhakthavatsala Raghavendra | 2ad3630 | 2024-03-12 16:12:32 -0700 | [diff] [blame] | 167 | EXPECT_EQ(open_cb_future.wait_for(open_timeout), std::future_status::ready); |
Roshan Pius | 90f5171 | 2021-09-21 11:09:12 -0700 | [diff] [blame] | 168 | EXPECT_TRUE(iuwb_chip->close().isOk()); |
Bhakthavatsala Raghavendra | 2ad3630 | 2024-03-12 16:12:32 -0700 | [diff] [blame] | 169 | EXPECT_EQ(close_cb_future.wait_for(close_timeout), std::future_status::ready); |
Roshan Pius | 90f5171 | 2021-09-21 11:09:12 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | TEST_P(UwbAidl, ChipCoreInit) { |
| 173 | const auto iuwb_chip = getAnyChipAndOpen(); |
| 174 | EXPECT_TRUE(iuwb_chip->coreInit().isOk()); |
| 175 | } |
| 176 | |
Roshan Pius | f2c2939 | 2021-12-09 10:18:39 -0800 | [diff] [blame] | 177 | TEST_P(UwbAidl, ChipSessionInit) { |
| 178 | const auto iuwb_chip = getAnyChipAndOpen(); |
| 179 | EXPECT_TRUE(iuwb_chip->sessionInit(0).isOk()); |
| 180 | } |
| 181 | |
Roshan Pius | 1e1c842 | 2021-11-04 12:59:07 -0700 | [diff] [blame] | 182 | TEST_P(UwbAidl, ChipGetSupportedAndroidUciVersion) { |
Roshan Pius | 90f5171 | 2021-09-21 11:09:12 -0700 | [diff] [blame] | 183 | const auto iuwb_chip = getAnyChipAndOpen(); |
| 184 | EXPECT_TRUE(iuwb_chip->coreInit().isOk()); |
| 185 | |
Roshan Pius | 1e1c842 | 2021-11-04 12:59:07 -0700 | [diff] [blame] | 186 | int32_t version; |
| 187 | EXPECT_TRUE(iuwb_chip->getSupportedAndroidUciVersion(&version).isOk()); |
Roshan Pius | 90f5171 | 2021-09-21 11:09:12 -0700 | [diff] [blame] | 188 | EXPECT_GT(version, 0); |
| 189 | } |
| 190 | |
Roshan Pius | 90f5171 | 2021-09-21 11:09:12 -0700 | [diff] [blame] | 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 | |
Roshan Pius | 90f5171 | 2021-09-21 11:09:12 -0700 | [diff] [blame] | 204 | TEST_P(UwbAidl, ChipSendUciMessage_GetDeviceInfo) { |
Shreshta Manu | 28ba6d7 | 2024-09-08 16:14:52 +0000 | [diff] [blame] | 205 | const auto iuwb_chip = getAnyChipAndOpen(); |
| 206 | EXPECT_TRUE(iuwb_chip->coreInit().isOk()); |
Roshan Pius | 90f5171 | 2021-09-21 11:09:12 -0700 | [diff] [blame] | 207 | |
Shreshta Manu | 28ba6d7 | 2024-09-08 16:14:52 +0000 | [diff] [blame] | 208 | std::vector<uint8_t> uciMessage = {0x20, 0x02, 0x00, 0x00}; /** CoreGetDeviceInfo */ |
| 209 | int32_t* return_status = new int32_t; |
| 210 | EXPECT_TRUE(iuwb_chip->sendUciMessage(uciMessage, return_status).isOk()); |
| 211 | EXPECT_EQ(*return_status, 4 /* Status Ok */); |
| 212 | } |
Roshan Pius | 90f5171 | 2021-09-21 11:09:12 -0700 | [diff] [blame] | 213 | |
| 214 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(UwbAidl); |
| 215 | INSTANTIATE_TEST_SUITE_P(Uwb, UwbAidl, |
| 216 | testing::ValuesIn(android::getAidlHalInstanceNames(IUwb::descriptor)), |
| 217 | android::PrintInstanceNameToString); |
| 218 | |
| 219 | int main(int argc, char** argv) { |
| 220 | ::testing::InitGoogleTest(&argc, argv); |
| 221 | ProcessState::self()->setThreadPoolMaxThreadCount(1); |
| 222 | ProcessState::self()->startThreadPool(); |
Mathieu Mandret | 6bab73f | 2024-03-13 14:51:32 +0100 | [diff] [blame] | 223 | // UWB HAL only allows 1 client, make sure framework |
| 224 | // does not have UWB HAL open before running |
| 225 | std::system("/system/bin/cmd uwb disable-uwb"); |
| 226 | sleep(3); |
| 227 | auto status = RUN_ALL_TESTS(); |
| 228 | sleep(3); |
| 229 | std::system("/system/bin/cmd uwb enable-uwb"); |
| 230 | return status; |
Roshan Pius | 90f5171 | 2021-09-21 11:09:12 -0700 | [diff] [blame] | 231 | } |