1 /*
2  * Copyright (C) 2015 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 android_hardware_automotive_vehicle_V2_0_VehicleHalManager_H_
18 #define android_hardware_automotive_vehicle_V2_0_VehicleHalManager_H_
19 
20 #include <inttypes.h>
21 #include <stdint.h>
22 #include <sys/types.h>
23 
24 #include <list>
25 #include <map>
26 #include <memory>
27 #include <set>
28 
29 #include <android/hardware/automotive/vehicle/2.0/IVehicle.h>
30 
31 #include "ConcurrentQueue.h"
32 #include "SubscriptionManager.h"
33 #include "VehicleHal.h"
34 #include "VehicleObjectPool.h"
35 #include "VehiclePropConfigIndex.h"
36 
37 namespace android {
38 namespace hardware {
39 namespace automotive {
40 namespace vehicle {
41 namespace V2_0 {
42 
43 /**
44  * This class is a thick proxy between IVehicle HIDL interface and vendor's implementation.
45  *
46  * It has some boilerplate code like batching and caching property values, checking permissions,
47  * etc. Vendors must implement VehicleHal class.
48  */
49 class VehicleHalManager : public IVehicle {
50 public:
VehicleHalManager(VehicleHal * vehicleHal)51     VehicleHalManager(VehicleHal* vehicleHal)
52         : mHal(vehicleHal),
53           mSubscriptionManager(std::bind(&VehicleHalManager::onAllClientsUnsubscribed,
54                                          this, std::placeholders::_1)) {
55         init();
56     }
57 
58     virtual ~VehicleHalManager();
59 
60     void init();
61 
62     // ---------------------------------------------------------------------------------------------
63     // Methods derived from IVehicle
64     Return<void> getAllPropConfigs(getAllPropConfigs_cb _hidl_cb)  override;
65     Return<void> getPropConfigs(const hidl_vec<int32_t>& properties,
66                                 getPropConfigs_cb _hidl_cb)  override;
67     Return<void> get(const VehiclePropValue& requestedPropValue,
68                      get_cb _hidl_cb)  override;
69     Return<StatusCode> set(const VehiclePropValue& value)  override;
70     Return<StatusCode> subscribe(const sp<IVehicleCallback>& callback,
71                                 const hidl_vec<SubscribeOptions>& options)  override;
72     Return<StatusCode> unsubscribe(const sp<IVehicleCallback>& callback,
73                                    int32_t propId)  override;
74     Return<void> debugDump(debugDump_cb _hidl_cb = nullptr) override;
75 
76 private:
77     using VehiclePropValuePtr = VehicleHal::VehiclePropValuePtr;
78     // Returns true if needs to call again shortly.
79     using RetriableAction = std::function<bool()>;
80 
81     // ---------------------------------------------------------------------------------------------
82     // Events received from VehicleHal
83     void onHalEvent(VehiclePropValuePtr  v);
84     void onHalPropertySetError(StatusCode errorCode, int32_t property,
85                                int32_t areaId);
86 
87     // ---------------------------------------------------------------------------------------------
88     // This method will be called from BatchingConsumer thread
89     void onBatchHalEvent(const std::vector<VehiclePropValuePtr >& values);
90 
91     void handlePropertySetEvent(const VehiclePropValue& value);
92 
93     const VehiclePropConfig* getPropConfigOrNull(int32_t prop) const;
94 
95     bool checkWritePermission(const VehiclePropConfig &config) const;
96     bool checkReadPermission(const VehiclePropConfig &config) const;
97     void onAllClientsUnsubscribed(int32_t propertyId);
98 
99     static bool isSubscribable(const VehiclePropConfig& config,
100                                SubscribeFlags flags);
101     static bool isSampleRateFixed(VehiclePropertyChangeMode mode);
102     static float checkSampleRate(const VehiclePropConfig& config,
103                                  float sampleRate);
104     static ClientId getClientId(const sp<IVehicleCallback>& callback);
105 private:
106     VehicleHal* mHal;
107     std::unique_ptr<VehiclePropConfigIndex> mConfigIndex;
108     SubscriptionManager mSubscriptionManager;
109 
110     hidl_vec<VehiclePropValue> mHidlVecOfVehiclePropValuePool;
111 
112     ConcurrentQueue<VehiclePropValuePtr> mEventQueue;
113     BatchingConsumer<VehiclePropValuePtr> mBatchingConsumer;
114     VehiclePropValuePool mValueObjectPool;
115 };
116 
117 }  // namespace V2_0
118 }  // namespace vehicle
119 }  // namespace automotive
120 }  // namespace hardware
121 }  // namespace android
122 
123 
124 #endif // android_hardware_automotive_vehicle_V2_0_VehicleHalManager_H_
125