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; |
| 42 | } // namespace |
| 43 | |
| 44 | class UwbClientCallback : public BnUwbClientCallback { |
| 45 | public: |
| 46 | UwbClientCallback(const std::function<void(const std::vector<uint8_t>&)>& on_uci_message_cb, |
| 47 | const std::function<void(UwbEvent, UwbStatus)>& on_hal_event_cb) |
| 48 | : on_uci_message_cb_(on_uci_message_cb), on_hal_event_cb_(on_hal_event_cb) {} |
| 49 | |
| 50 | ScopedAStatus onUciMessage(const std::vector<uint8_t>& data) override { |
| 51 | on_uci_message_cb_(data); |
| 52 | return ScopedAStatus::ok(); |
| 53 | } |
| 54 | |
| 55 | ScopedAStatus onHalEvent(UwbEvent uwb_event, UwbStatus uwb_status) override { |
| 56 | on_hal_event_cb_(uwb_event, uwb_status); |
| 57 | return ScopedAStatus::ok(); |
| 58 | } |
| 59 | |
| 60 | private: |
| 61 | std::function<void(const std::vector<uint8_t>&)> on_uci_message_cb_; |
| 62 | std::function<void(UwbEvent, UwbStatus)> on_hal_event_cb_; |
| 63 | }; |
| 64 | |
| 65 | class UwbAidl : public testing::TestWithParam<std::string> { |
| 66 | public: |
| 67 | virtual void SetUp() override { |
| 68 | iuwb_ = IUwb::fromBinder(SpAIBinder(AServiceManager_waitForService(GetParam().c_str()))); |
| 69 | ASSERT_NE(iuwb_, nullptr); |
| 70 | } |
| 71 | std::shared_ptr<IUwb> iuwb_; |
| 72 | |
| 73 | // TODO (b/197638976): We pick the first chip here. Need to fix this |
| 74 | // for supporting multiple chips in the future. |
| 75 | std::string getAnyChipName() { |
| 76 | std::vector<std::string> chip_names; |
| 77 | ScopedAStatus status = iuwb_->getChips(&chip_names); |
| 78 | EXPECT_TRUE(status.isOk()); |
| 79 | EXPECT_FALSE(chip_names.empty()); |
| 80 | return chip_names[0]; |
| 81 | } |
| 82 | |
| 83 | // TODO (b/197638976): We pick the first chip here. Need to fix this |
| 84 | // for supporting multiple chips in the future. |
| 85 | std::shared_ptr<IUwbChip> getAnyChip() { |
| 86 | std::shared_ptr<IUwbChip> iuwb_chip; |
| 87 | ScopedAStatus status = iuwb_->getChip(getAnyChipName(), &iuwb_chip); |
| 88 | EXPECT_TRUE(status.isOk()); |
| 89 | EXPECT_NE(iuwb_chip, nullptr); |
| 90 | return iuwb_chip; |
| 91 | } |
| 92 | |
| 93 | std::shared_ptr<IUwbChip> getAnyChipAndOpen() { |
| 94 | std::promise<void> open_cb_promise; |
| 95 | std::future<void> open_cb_future{open_cb_promise.get_future()}; |
| 96 | std::shared_ptr<UwbClientCallback> callback = ndk::SharedRefBase::make<UwbClientCallback>( |
| 97 | [](auto /* data */) {}, |
| 98 | [&open_cb_promise](auto event, auto /* status */) { |
| 99 | if (event == UwbEvent::OPEN_CPLT) { |
| 100 | open_cb_promise.set_value(); |
| 101 | } |
| 102 | }); |
| 103 | std::chrono::milliseconds timeout{kCallbackTimeoutMs}; |
| 104 | const auto iuwb_chip = getAnyChip(); |
| 105 | EXPECT_TRUE(iuwb_chip->open(callback).isOk()); |
| 106 | EXPECT_EQ(open_cb_future.wait_for(timeout), std::future_status::ready); |
| 107 | return iuwb_chip; |
| 108 | } |
| 109 | }; |
| 110 | |
| 111 | TEST_P(UwbAidl, GetChips) { |
| 112 | std::vector<std::string> chip_names; |
| 113 | ScopedAStatus status = iuwb_->getChips(&chip_names); |
| 114 | EXPECT_TRUE(status.isOk()); |
| 115 | EXPECT_FALSE(chip_names.empty()); |
| 116 | } |
| 117 | |
| 118 | TEST_P(UwbAidl, GetChip) { |
| 119 | std::shared_ptr<IUwbChip> iuwb_chip; |
| 120 | ScopedAStatus status = iuwb_->getChip(getAnyChipName(), &iuwb_chip); |
| 121 | EXPECT_TRUE(status.isOk()); |
| 122 | EXPECT_NE(iuwb_chip, nullptr); |
| 123 | } |
| 124 | |
| 125 | TEST_P(UwbAidl, ChipOpen) { |
| 126 | std::promise<void> open_cb_promise; |
| 127 | std::future<void> open_cb_future{open_cb_promise.get_future()}; |
| 128 | std::shared_ptr<UwbClientCallback> callback = ndk::SharedRefBase::make<UwbClientCallback>( |
| 129 | [](auto /* data */) {}, |
| 130 | [&open_cb_promise](auto event, auto /* status */) { |
| 131 | if (event == UwbEvent::OPEN_CPLT) { |
| 132 | open_cb_promise.set_value(); |
| 133 | } |
| 134 | }); |
| 135 | std::chrono::milliseconds timeout{kCallbackTimeoutMs}; |
| 136 | const auto iuwb_chip = getAnyChip(); |
| 137 | EXPECT_TRUE(iuwb_chip->open(callback).isOk()); |
| 138 | EXPECT_EQ(open_cb_future.wait_for(timeout), std::future_status::ready); |
| 139 | } |
| 140 | |
| 141 | TEST_P(UwbAidl, ChipClose) { |
| 142 | std::promise<void> open_cb_promise; |
| 143 | std::future<void> open_cb_future{open_cb_promise.get_future()}; |
| 144 | std::promise<void> close_cb_promise; |
| 145 | std::future<void> close_cb_future{close_cb_promise.get_future()}; |
| 146 | std::shared_ptr<UwbClientCallback> callback = ndk::SharedRefBase::make<UwbClientCallback>( |
| 147 | [](auto /* data */) {}, |
| 148 | [&open_cb_promise, &close_cb_promise](auto event, auto /* status */) { |
| 149 | if (event == UwbEvent::OPEN_CPLT) { |
| 150 | open_cb_promise.set_value(); |
| 151 | } |
| 152 | if (event == UwbEvent::CLOSE_CPLT) { |
| 153 | close_cb_promise.set_value(); |
| 154 | } |
| 155 | }); |
| 156 | std::chrono::milliseconds timeout{kCallbackTimeoutMs}; |
| 157 | const auto iuwb_chip = getAnyChip(); |
| 158 | EXPECT_TRUE(iuwb_chip->open(callback).isOk()); |
| 159 | EXPECT_EQ(open_cb_future.wait_for(timeout), std::future_status::ready); |
| 160 | EXPECT_TRUE(iuwb_chip->close().isOk()); |
| 161 | EXPECT_EQ(close_cb_future.wait_for(timeout), std::future_status::ready); |
| 162 | } |
| 163 | |
| 164 | TEST_P(UwbAidl, ChipCoreInit) { |
| 165 | const auto iuwb_chip = getAnyChipAndOpen(); |
| 166 | EXPECT_TRUE(iuwb_chip->coreInit().isOk()); |
| 167 | } |
| 168 | |
| 169 | TEST_P(UwbAidl, ChipGetSupportedVendorUciVersion) { |
| 170 | const auto iuwb_chip = getAnyChipAndOpen(); |
| 171 | EXPECT_TRUE(iuwb_chip->coreInit().isOk()); |
| 172 | |
| 173 | int version; |
| 174 | EXPECT_TRUE(iuwb_chip->getSupportedVendorUciVersion(&version).isOk()); |
| 175 | EXPECT_GT(version, 0); |
| 176 | } |
| 177 | |
| 178 | TEST_P(UwbAidl, ChipGetName) { |
| 179 | std::string chip_name = getAnyChipName(); |
| 180 | std::shared_ptr<IUwbChip> iuwb_chip; |
| 181 | ScopedAStatus status = iuwb_->getChip(chip_name, &iuwb_chip); |
| 182 | EXPECT_TRUE(status.isOk()); |
| 183 | EXPECT_NE(iuwb_chip, nullptr); |
| 184 | |
| 185 | std::string retrieved_chip_name; |
| 186 | status = iuwb_chip->getName(&retrieved_chip_name); |
| 187 | EXPECT_TRUE(status.isOk()); |
| 188 | EXPECT_EQ(retrieved_chip_name, chip_name); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | TEST_P(UwbAidl, ChipSendUciMessage_GetDeviceInfo) { |
| 193 | const auto iuwb_chip = getAnyChipAndOpen(callback); |
| 194 | EXPECT_TRUE(iuwb_chip->coreInit(callback).isOk()); |
| 195 | |
| 196 | const std::vector<uint8_t> |
| 197 | EXPECT_TRUE(iuwb_chip->sendUciMessage().isOk()); |
| 198 | } */ |
| 199 | |
| 200 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(UwbAidl); |
| 201 | INSTANTIATE_TEST_SUITE_P(Uwb, UwbAidl, |
| 202 | testing::ValuesIn(android::getAidlHalInstanceNames(IUwb::descriptor)), |
| 203 | android::PrintInstanceNameToString); |
| 204 | |
| 205 | int main(int argc, char** argv) { |
| 206 | ::testing::InitGoogleTest(&argc, argv); |
| 207 | ProcessState::self()->setThreadPoolMaxThreadCount(1); |
| 208 | ProcessState::self()->startThreadPool(); |
| 209 | return RUN_ALL_TESTS(); |
| 210 | } |