• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022, 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 #pragma once
18 
19 #include "MockVehicle.h"
20 
21 #include <android-base/stringprintf.h>
22 #include <android-base/strings.h>
23 #include <android/binder_interface_utils.h>
24 #include <gmock/gmock.h>
25 
26 #include <AidlVhalClient.h>
27 
28 namespace android {
29 namespace automotive {
30 namespace watchdog {
31 
toString(const std::vector<int32_t> & values)32 std::string toString(const std::vector<int32_t>& values) {
33     std::vector<std::string> strings;
34     for (int32_t value : values) {
35         strings.push_back(std::to_string(value));
36     }
37     return android::base::StringPrintf("[%s]", android::base::Join(strings, ",").c_str());
38 }
39 
40 class MockSubscriptionClient final :
41       public android::frameworks::automotive::vhal::ISubscriptionClient {
42 public:
MockSubscriptionClient(const std::shared_ptr<MockVehicle> & hal,const std::shared_ptr<android::frameworks::automotive::vhal::ISubscriptionCallback> & callback)43     MockSubscriptionClient(
44             const std::shared_ptr<MockVehicle>& hal,
45             const std::shared_ptr<android::frameworks::automotive::vhal::ISubscriptionCallback>&
46                     callback) {
47         mHal = hal;
48         mCallback = ndk::SharedRefBase::make<
49                 android::frameworks::automotive::vhal::SubscriptionVehicleCallback>(callback);
50     }
~MockSubscriptionClient()51     ~MockSubscriptionClient() {
52         mHal.reset();
53         mCallback.reset();
54     }
subscribe(const std::vector<aidl::android::hardware::automotive::vehicle::SubscribeOptions> & options)55     android::frameworks::automotive::vhal::VhalClientResult<void> subscribe(
56             const std::vector<aidl::android::hardware::automotive::vehicle::SubscribeOptions>&
57                     options) override {
58         {
59             std::lock_guard<std::mutex> lock(mLock);
60             for (const auto& option : options) {
61                 mSubscribedPropIds.insert(option.propId);
62             }
63         }
64 
65         if (auto status = mHal->subscribe(mCallback, options, /*maxSharedMemoryFileCount=*/0);
66             !status.isOk()) {
67             return android::frameworks::automotive::vhal::ClientStatusError(
68                     static_cast<aidl::android::hardware::automotive::vehicle::StatusCode>(
69                             status.getServiceSpecificError()));
70         }
71         return {};
72     }
73 
unsubscribe(const std::vector<int32_t> & propIds)74     android::frameworks::automotive::vhal::VhalClientResult<void> unsubscribe(
75             const std::vector<int32_t>& propIds) override {
76         if (auto status = mHal->unsubscribe(mCallback, propIds); !status.isOk()) {
77             return android::frameworks::automotive::vhal::ClientStatusError(
78                            static_cast<aidl::android::hardware::automotive::vehicle::StatusCode>(
79                                    status.getServiceSpecificError()))
80                     << "failed to unsubscribe to prop IDs: " << toString(propIds)
81                     << ", error: " << status.getMessage();
82         }
83         return {};
84     }
85 
unsubscribeAll()86     void unsubscribeAll() override {
87         std::vector<int32_t> propIds;
88         {
89             std::lock_guard<std::mutex> lock(mLock);
90             propIds = std::vector<int32_t>(mSubscribedPropIds.begin(), mSubscribedPropIds.end());
91         }
92         unsubscribe(propIds);
93     }
94 
95 private:
96     std::shared_ptr<MockVehicle> mHal;
97     std::shared_ptr<android::frameworks::automotive::vhal::SubscriptionVehicleCallback> mCallback;
98 
99     std::mutex mLock;
100     std::unordered_set<int32_t> mSubscribedPropIds GUARDED_BY(mLock);
101 };
102 
103 }  // namespace watchdog
104 }  // namespace automotive
105 }  // namespace android
106