• 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 #include "IVhalClient.h"
18 
19 #include "AidlVhalClient.h"
20 #include "HidlVhalClient.h"
21 
22 #include <android/hardware/automotive/vehicle/2.0/IVehicle.h>
23 
24 #include <condition_variable>  // NOLINT
25 #include <mutex>               // NOLINT
26 
27 namespace android {
28 namespace frameworks {
29 namespace automotive {
30 namespace vhal {
31 
32 using ::android::hardware::automotive::vehicle::VhalResult;
33 
create()34 std::shared_ptr<IVhalClient> IVhalClient::create() {
35     auto client = AidlVhalClient::create();
36     if (client != nullptr) {
37         return client;
38     }
39 
40     return HidlVhalClient::create();
41 }
42 
tryCreate()43 std::shared_ptr<IVhalClient> IVhalClient::tryCreate() {
44     auto client = AidlVhalClient::tryCreate();
45     if (client != nullptr) {
46         return client;
47     }
48 
49     return HidlVhalClient::tryCreate();
50 }
51 
tryCreateAidlClient(const char * descriptor)52 std::shared_ptr<IVhalClient> IVhalClient::tryCreateAidlClient(const char* descriptor) {
53     return AidlVhalClient::tryCreate(descriptor);
54 }
55 
tryCreateHidlClient(const char * descriptor)56 std::shared_ptr<IVhalClient> IVhalClient::tryCreateHidlClient(const char* descriptor) {
57     return HidlVhalClient::tryCreate(descriptor);
58 }
59 
getValueSync(const IHalPropValue & requestValue)60 VhalResult<std::unique_ptr<IHalPropValue>> IVhalClient::getValueSync(
61         const IHalPropValue& requestValue) {
62     struct {
63         std::mutex lock;
64         std::condition_variable cv;
65         VhalResult<std::unique_ptr<IHalPropValue>> result;
66         bool gotResult = false;
67     } s;
68 
69     auto callback = std::make_shared<IVhalClient::GetValueCallbackFunc>(
70             [&s](VhalResult<std::unique_ptr<IHalPropValue>> r) {
71                 {
72                     std::lock_guard<std::mutex> lockGuard(s.lock);
73                     s.result = std::move(r);
74                     s.gotResult = true;
75                     s.cv.notify_one();
76                 }
77             });
78 
79     getValue(requestValue, callback);
80 
81     std::unique_lock<std::mutex> lk(s.lock);
82     s.cv.wait(lk, [&s] { return s.gotResult; });
83 
84     return std::move(s.result);
85 }
86 
setValueSync(const IHalPropValue & requestValue)87 VhalResult<void> IVhalClient::setValueSync(const IHalPropValue& requestValue) {
88     struct {
89         std::mutex lock;
90         std::condition_variable cv;
91         VhalResult<void> result;
92         bool gotResult = false;
93     } s;
94 
95     auto callback = std::make_shared<IVhalClient::SetValueCallbackFunc>([&s](VhalResult<void> r) {
96         {
97             std::lock_guard<std::mutex> lockGuard(s.lock);
98             s.result = std::move(r);
99             s.gotResult = true;
100             s.cv.notify_one();
101         }
102     });
103 
104     setValue(requestValue, callback);
105 
106     std::unique_lock<std::mutex> lk(s.lock);
107     s.cv.wait(lk, [&s] { return s.gotResult; });
108 
109     return std::move(s.result);
110 }
111 
112 }  // namespace vhal
113 }  // namespace automotive
114 }  // namespace frameworks
115 }  // namespace android
116