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 #include "IVhalClient.h"
18
19 #include "AidlVhalClient.h"
20 #include "HidlVhalClient.h"
21
22 #include <android-base/stringprintf.h>
23 #include <android/hardware/automotive/vehicle/2.0/IVehicle.h>
24
25 #include <condition_variable> // NOLINT
26 #include <mutex> // NOLINT
27
28 namespace android {
29 namespace frameworks {
30 namespace automotive {
31 namespace vhal {
32
33 using ::android::base::StringPrintf;
34
create(bool startThreadPool)35 std::shared_ptr<IVhalClient> IVhalClient::create(bool startThreadPool) {
36 auto client = AidlVhalClient::create(startThreadPool);
37 if (client != nullptr) {
38 return client;
39 }
40
41 return HidlVhalClient::create();
42 }
43
tryCreate(bool startThreadPool)44 std::shared_ptr<IVhalClient> IVhalClient::tryCreate(bool startThreadPool) {
45 auto client = AidlVhalClient::tryCreate(startThreadPool);
46 if (client != nullptr) {
47 return client;
48 }
49
50 return HidlVhalClient::tryCreate();
51 }
52
tryCreateAidlClient(const char * descriptor,bool startThreadPool)53 std::shared_ptr<IVhalClient> IVhalClient::tryCreateAidlClient(const char* descriptor,
54 bool startThreadPool) {
55 return AidlVhalClient::tryCreate(descriptor, startThreadPool);
56 }
57
tryCreateHidlClient(const char * descriptor)58 std::shared_ptr<IVhalClient> IVhalClient::tryCreateHidlClient(const char* descriptor) {
59 return HidlVhalClient::tryCreate(descriptor);
60 }
61
getValueSync(const IHalPropValue & requestValue)62 VhalClientResult<std::unique_ptr<IHalPropValue>> IVhalClient::getValueSync(
63 const IHalPropValue& requestValue) {
64 struct {
65 std::mutex lock;
66 std::condition_variable cv;
67 VhalClientResult<std::unique_ptr<IHalPropValue>> result;
68 bool gotResult = false;
69 } s;
70
71 auto callback = std::make_shared<IVhalClient::GetValueCallbackFunc>(
72 [&s](VhalClientResult<std::unique_ptr<IHalPropValue>> r) {
73 {
74 std::lock_guard<std::mutex> lockGuard(s.lock);
75 s.result = std::move(r);
76 s.gotResult = true;
77 s.cv.notify_one();
78 }
79 });
80
81 getValue(requestValue, callback);
82
83 std::unique_lock<std::mutex> lk(s.lock);
84 s.cv.wait(lk, [&s] { return s.gotResult; });
85
86 return std::move(s.result);
87 }
88
setValueSync(const IHalPropValue & requestValue)89 VhalClientResult<void> IVhalClient::setValueSync(const IHalPropValue& requestValue) {
90 struct {
91 std::mutex lock;
92 std::condition_variable cv;
93 VhalClientResult<void> result;
94 bool gotResult = false;
95 } s;
96
97 auto callback =
98 std::make_shared<IVhalClient::SetValueCallbackFunc>([&s](VhalClientResult<void> r) {
99 {
100 std::lock_guard<std::mutex> lockGuard(s.lock);
101 s.result = std::move(r);
102 s.gotResult = true;
103 s.cv.notify_one();
104 }
105 });
106
107 setValue(requestValue, callback);
108
109 std::unique_lock<std::mutex> lk(s.lock);
110 s.cv.wait(lk, [&s] { return s.gotResult; });
111
112 return std::move(s.result);
113 }
114
value() const115 ErrorCode VhalClientError::value() const {
116 return mCode;
117 }
118
toString(ErrorCode code)119 std::string VhalClientError::toString(ErrorCode code) {
120 switch (code) {
121 case ErrorCode::OK:
122 return "OK";
123 case ErrorCode::INVALID_ARG:
124 return "INVALID_ARG";
125 case ErrorCode::TIMEOUT:
126 return "TIMEOUT";
127 case ErrorCode::TRANSACTION_ERROR:
128 return "TRANSACTION_ERROR";
129 case ErrorCode::TRY_AGAIN_FROM_VHAL:
130 return "TRY_AGAIN_FROM_VHAL";
131 case ErrorCode::NOT_AVAILABLE_FROM_VHAL:
132 return "NOT_AVAILABLE_FROM_VHAL";
133 case ErrorCode::ACCESS_DENIED_FROM_VHAL:
134 return "ACCESS_DENIED_FROM_VHAL";
135 case ErrorCode::INTERNAL_ERROR_FROM_VHAL:
136 return "INTERNAL_ERROR_FROM_VHAL";
137 case ErrorCode::NOT_SUPPORTED:
138 return "NOT_SUPPORTED";
139 default:
140 return StringPrintf("Unknown error. Code: %d", static_cast<int>(code));
141 }
142 }
143
print() const144 std::string VhalClientError::print() const {
145 return VhalClientError::toString(mCode);
146 }
147
148 } // namespace vhal
149 } // namespace automotive
150 } // namespace frameworks
151 } // namespace android
152