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     Return<void> debug(const hidl_handle& fd, const hidl_vec<hidl_string>& options) override;
77 
78   private:
79     using VehiclePropValuePtr = VehicleHal::VehiclePropValuePtr;
80     // Returns true if needs to call again shortly.
81     using RetriableAction = std::function<bool()>;
82 
83     // ---------------------------------------------------------------------------------------------
84     // Events received from VehicleHal
85     void onHalEvent(VehiclePropValuePtr  v);
86     void onHalPropertySetError(StatusCode errorCode, int32_t property,
87                                int32_t areaId);
88 
89     // ---------------------------------------------------------------------------------------------
90     // This method will be called from BatchingConsumer thread
91     void onBatchHalEvent(const std::vector<VehiclePropValuePtr >& values);
92 
93     void handlePropertySetEvent(const VehiclePropValue& value);
94 
95     const VehiclePropConfig* getPropConfigOrNull(int32_t prop) const;
96 
97     bool checkWritePermission(const VehiclePropConfig &config) const;
98     bool checkReadPermission(const VehiclePropConfig &config) const;
99     void onAllClientsUnsubscribed(int32_t propertyId);
100 
101     // Dump and commands
102     // TODO: most functions below (exception dump() and cmdSetOne()) should be const, but they rely
103     // on IVehicle.get(), which isn't...
104     void cmdDump(int fd, const hidl_vec<hidl_string>& options);
105     void cmdDumpOneProperty(int fd, int32_t prop, int32_t areaId);
106     void cmdDumpOneProperty(int fd, int rowNumber, const VehiclePropConfig& config);
107 
108     static bool checkArgumentsSize(int fd, const hidl_vec<hidl_string>& options, size_t minSize);
109     static bool checkCallerHasWritePermissions(int fd);
110     static bool safelyParseInt(int fd, int index, std::string s, int* out);
111     void cmdHelp(int fd) const;
112     void cmdListAllProperties(int fd) const;
113     void cmdDumpAllProperties(int fd);
114     void cmdDumpSpecificProperties(int fd, const hidl_vec<hidl_string>& options);
115     void cmdSetOneProperty(int fd, const hidl_vec<hidl_string>& options);
116 
117     static bool isSubscribable(const VehiclePropConfig& config,
118                                SubscribeFlags flags);
119     static bool isSampleRateFixed(VehiclePropertyChangeMode mode);
120     static float checkSampleRate(const VehiclePropConfig& config,
121                                  float sampleRate);
122     static ClientId getClientId(const sp<IVehicleCallback>& callback);
123 private:
124     VehicleHal* mHal;
125     std::unique_ptr<VehiclePropConfigIndex> mConfigIndex;
126     SubscriptionManager mSubscriptionManager;
127 
128     hidl_vec<VehiclePropValue> mHidlVecOfVehiclePropValuePool;
129 
130     ConcurrentQueue<VehiclePropValuePtr> mEventQueue;
131     BatchingConsumer<VehiclePropValuePtr> mBatchingConsumer;
132     VehiclePropValuePool mValueObjectPool;
133 };
134 
135 }  // namespace V2_0
136 }  // namespace vehicle
137 }  // namespace automotive
138 }  // namespace hardware
139 }  // namespace android
140 
141 
142 #endif // android_hardware_automotive_vehicle_V2_0_VehicleHalManager_H_
143