/* * Copyright (c) 2022, The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "MockVehicle.h" #include #include #include #include #include namespace android { namespace automotive { namespace watchdog { std::string toString(const std::vector& values) { std::vector strings; for (int32_t value : values) { strings.push_back(std::to_string(value)); } return android::base::StringPrintf("[%s]", android::base::Join(strings, ",").c_str()); } class MockSubscriptionClient final : public android::frameworks::automotive::vhal::ISubscriptionClient { public: MockSubscriptionClient( const std::shared_ptr& hal, const std::shared_ptr& callback) { mHal = hal; mCallback = ndk::SharedRefBase::make< android::frameworks::automotive::vhal::SubscriptionVehicleCallback>(callback); } ~MockSubscriptionClient() { mHal.reset(); mCallback.reset(); } android::frameworks::automotive::vhal::VhalClientResult subscribe( const std::vector& options) override { { std::lock_guard lock(mLock); for (const auto& option : options) { mSubscribedPropIds.insert(option.propId); } } if (auto status = mHal->subscribe(mCallback, options, /*maxSharedMemoryFileCount=*/0); !status.isOk()) { return android::frameworks::automotive::vhal::ClientStatusError( static_cast( status.getServiceSpecificError())); } return {}; } android::frameworks::automotive::vhal::VhalClientResult unsubscribe( const std::vector& propIds) override { if (auto status = mHal->unsubscribe(mCallback, propIds); !status.isOk()) { return android::frameworks::automotive::vhal::ClientStatusError( static_cast( status.getServiceSpecificError())) << "failed to unsubscribe to prop IDs: " << toString(propIds) << ", error: " << status.getMessage(); } return {}; } void unsubscribeAll() override { std::vector propIds; { std::lock_guard lock(mLock); propIds = std::vector(mSubscribedPropIds.begin(), mSubscribedPropIds.end()); } unsubscribe(propIds); } private: std::shared_ptr mHal; std::shared_ptr mCallback; std::mutex mLock; std::unordered_set mSubscribedPropIds GUARDED_BY(mLock); }; } // namespace watchdog } // namespace automotive } // namespace android