• 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 #ifndef CPP_VHAL_CLIENT_INCLUDE_IVHALCLIENT_H_
18 #define CPP_VHAL_CLIENT_INCLUDE_IVHALCLIENT_H_
19 
20 #include "IHalPropConfig.h"
21 #include "IHalPropValue.h"
22 
23 #include <aidl/android/hardware/automotive/vehicle/StatusCode.h>
24 #include <aidl/android/hardware/automotive/vehicle/SubscribeOptions.h>
25 #include <android-base/result.h>
26 
27 #include <VehicleUtils.h>
28 
29 namespace android {
30 namespace frameworks {
31 namespace automotive {
32 namespace vhal {
33 
34 struct HalPropError {
35     int32_t propId;
36     int32_t areaId;
37     aidl::android::hardware::automotive::vehicle::StatusCode status;
38 };
39 
40 // ISubscriptionCallback is a general interface to delivery property events caused by subscription.
41 class ISubscriptionCallback {
42 public:
43     virtual ~ISubscriptionCallback() = default;
44     /**
45      * Called when new property events happen.
46      */
47     virtual void onPropertyEvent(const std::vector<std::unique_ptr<IHalPropValue>>& values) = 0;
48 
49     /**
50      * Called when property set errors happen.
51      */
52     virtual void onPropertySetError(const std::vector<HalPropError>& errors) = 0;
53 };
54 
55 // ISubscriptionCallback is a client that could be used to subscribe/unsubscribe.
56 class ISubscriptionClient {
57 public:
58     virtual ~ISubscriptionClient() = default;
59 
60     virtual android::hardware::automotive::vehicle::VhalResult<void> subscribe(
61             const std::vector<aidl::android::hardware::automotive::vehicle::SubscribeOptions>&
62                     options) = 0;
63 
64     virtual android::hardware::automotive::vehicle::VhalResult<void> unsubscribe(
65             const std::vector<int32_t>& propIds) = 0;
66 };
67 
68 // IVhalClient is a thread-safe client for AIDL or HIDL VHAL backend.
69 class IVhalClient {
70 public:
71     // Wait for VHAL service and create a client. Return nullptr if failed to connect to VHAL.
72     static std::shared_ptr<IVhalClient> create();
73 
74     // Try to get the VHAL service and create a client. Return nullptr if failed to connect to VHAL.
75     static std::shared_ptr<IVhalClient> tryCreate();
76 
77     // Try to create a client based on the AIDL VHAL service descriptor.
78     static std::shared_ptr<IVhalClient> tryCreateAidlClient(const char* descriptor);
79 
80     // Try to create a client based on the HIDL VHAL service descriptor.
81     static std::shared_ptr<IVhalClient> tryCreateHidlClient(const char* descriptor);
82 
83     // The default timeout for callbacks.
84     constexpr static int64_t DEFAULT_TIMEOUT_IN_SEC = 10;
85 
86     virtual ~IVhalClient() = default;
87 
88     using GetValueCallbackFunc = std::function<void(
89             android::hardware::automotive::vehicle::VhalResult<std::unique_ptr<IHalPropValue>>)>;
90     using SetValueCallbackFunc =
91             std::function<void(android::hardware::automotive::vehicle::VhalResult<void>)>;
92     using OnBinderDiedCallbackFunc = std::function<void()>;
93 
94     /**
95      * Check whether we are connected to AIDL VHAL backend.
96      *
97      * Returns {@code true} if we are connected to AIDL VHAL backend, {@code false} if we are
98      * connected to HIDL backend.
99      */
100     virtual bool isAidlVhal() = 0;
101 
102     /**
103      * Create a new {@code IHalpropValue}.
104      *
105      * @param propId The property ID.
106      * @return The created {@code IHalPropValue}.
107      */
108     virtual std::unique_ptr<IHalPropValue> createHalPropValue(int32_t propId) = 0;
109 
110     /**
111      * Create a new {@code IHalpropValue}.
112      *
113      * @param propId The property ID.
114      * @param areaId The area ID for the property.
115      * @return The created {@code IHalPropValue}.
116      */
117     virtual std::unique_ptr<IHalPropValue> createHalPropValue(int32_t propId, int32_t areaId) = 0;
118 
119     /**
120      * Get a property value asynchronously.
121      *
122      * @param requestValue The value to request.
123      * @param callback The callback that would be called when the result is ready. The callback
124      *    would be called with an okay result with the got value inside on success. The callback
125      *    would be called with an error result with error code as the returned status code on
126      *    failure.
127      */
128     virtual void getValue(const IHalPropValue& requestValue,
129                           std::shared_ptr<GetValueCallbackFunc> callback) = 0;
130 
131     /**
132      * Get a property value synchronously.
133      *
134      * @param requestValue the value to request.
135      * @return An okay result with the returned value on success or an error result with returned
136      *    status code as error code. For AIDL backend, this would return TRY_AGAIN error on timeout.
137      *    For HIDL backend, because HIDL backend is synchronous, timeout does not apply.
138      */
139     virtual android::hardware::automotive::vehicle::VhalResult<std::unique_ptr<IHalPropValue>>
140     getValueSync(const IHalPropValue& requestValue);
141 
142     /**
143      * Set a property value asynchronously.
144      *
145      * @param requestValue The value to set.
146      * @param callback The callback that would be called when the request is processed. The callback
147      *    would be called with an empty okay result on success. The callback would be called with
148      *    an error result with error code as the returned status code on failure.
149      */
150     virtual void setValue(const IHalPropValue& requestValue,
151                           std::shared_ptr<SetValueCallbackFunc> callback) = 0;
152 
153     /**
154      * Set a property value synchronously.
155      *
156      * @param requestValue the value to set.
157      * @return An empty okay result on success or an error result with returned status code as
158      *    error code. For AIDL backend, this would return TRY_AGAIN error on timeout.
159      *    For HIDL backend, because HIDL backend is synchronous, timeout does not apply.
160      */
161     virtual android::hardware::automotive::vehicle::VhalResult<void> setValueSync(
162             const IHalPropValue& requestValue);
163 
164     /**
165      * Add a callback that would be called when the binder connection to VHAL died.
166      *
167      * @param callback The callback that would be called when the binder died.
168      * @return An okay result on success or an error on failure.
169      */
170     virtual android::hardware::automotive::vehicle::VhalResult<void> addOnBinderDiedCallback(
171             std::shared_ptr<OnBinderDiedCallbackFunc> callback) = 0;
172 
173     /**
174      * Remove a previously added OnBinderDied callback.
175      *
176      * @param callback The callback that would be removed.
177      * @return An okay result on success, or an error if the callback is not added before.
178      */
179     virtual android::hardware::automotive::vehicle::VhalResult<void> removeOnBinderDiedCallback(
180             std::shared_ptr<OnBinderDiedCallbackFunc> callback) = 0;
181 
182     /**
183      * Get all the property configurations.
184      *
185      * @return An okay result that contains all property configs on success or an error on failure.
186      */
187     virtual android::hardware::automotive::vehicle::VhalResult<
188             std::vector<std::unique_ptr<IHalPropConfig>>>
189     getAllPropConfigs() = 0;
190 
191     /**
192      * Get the configs for specified properties.
193      *
194      * @param propIds A list of property IDs to get configs for.
195      * @return An okay result that contains property configs for specified properties on success or
196      *    an error if failed to get any of the property configs.
197      */
198     virtual android::hardware::automotive::vehicle::VhalResult<
199             std::vector<std::unique_ptr<IHalPropConfig>>>
200     getPropConfigs(std::vector<int32_t> propIds) = 0;
201 
202     /**
203      * Get a {@code ISubscriptionClient} that could be used to subscribe/unsubscribe to properties.
204      *
205      * @param callback The callback that would be called when property event happens.
206      * @return A {@code ISubscriptionClient} used to subscribe/unsubscribe.
207      */
208     virtual std::unique_ptr<ISubscriptionClient> getSubscriptionClient(
209             std::shared_ptr<ISubscriptionCallback> callback) = 0;
210 };
211 
212 }  // namespace vhal
213 }  // namespace automotive
214 }  // namespace frameworks
215 }  // namespace android
216 
217 #endif  // CPP_VHAL_CLIENT_INCLUDE_IVHALCLIENT_H_
218