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     // Set unit test class as friend class to test private functions.
80     friend class VehicleHalManagerTestHelper;
81 
82     using VehiclePropValuePtr = VehicleHal::VehiclePropValuePtr;
83     // Returns true if needs to call again shortly.
84     using RetriableAction = std::function<bool()>;
85 
86     // ---------------------------------------------------------------------------------------------
87     // Events received from VehicleHal
88     void onHalEvent(VehiclePropValuePtr  v);
89     void onHalPropertySetError(StatusCode errorCode, int32_t property,
90                                int32_t areaId);
91 
92     // ---------------------------------------------------------------------------------------------
93     // This method will be called from BatchingConsumer thread
94     void onBatchHalEvent(const std::vector<VehiclePropValuePtr >& values);
95 
96     void handlePropertySetEvent(const VehiclePropValue& value);
97 
98     const VehiclePropConfig* getPropConfigOrNull(int32_t prop) const;
99 
100     bool checkWritePermission(const VehiclePropConfig &config) const;
101     bool checkReadPermission(const VehiclePropConfig &config) const;
102     void onAllClientsUnsubscribed(int32_t propertyId);
103 
104     // Dump and commands
105     // TODO: most functions below (exception dump() and cmdSetOne()) should be const, but they rely
106     // on IVehicle.get(), which isn't...
107     void cmdDump(int fd, const hidl_vec<hidl_string>& options);
108     void cmdDumpOneProperty(int fd, int32_t prop, int32_t areaId);
109     void cmdDumpOneProperty(int fd, int rowNumber, const VehiclePropConfig& config);
110 
111     bool cmdSetOneProperty(int fd, const hidl_vec<hidl_string>& options);
112 
113     static bool checkArgumentsSize(int fd, const hidl_vec<hidl_string>& options, size_t minSize);
114     static bool checkCallerHasWritePermissions(int fd);
115     template <typename T>
116     static bool safelyParseInt(int fd, int index, const std::string& s, T* out);
117     static bool safelyParseFloat(int fd, int index, const std::string& s, float* out);
118     // Parses "s" as a hex string and populate "*bytes". The hex string must be in the format of
119     // valid hex format, e.g. "0xABCD".
120     static bool parseHexString(int fd, const std::string& s, std::vector<uint8_t>* bytes);
121     void cmdHelp(int fd) const;
122     void cmdListAllProperties(int fd) const;
123     void cmdDumpAllProperties(int fd);
124     void cmdDumpSpecificProperties(int fd, const hidl_vec<hidl_string>& options);
125 
126     static bool isSubscribable(const VehiclePropConfig& config,
127                                SubscribeFlags flags);
128     static bool isSampleRateFixed(VehiclePropertyChangeMode mode);
129     static float checkSampleRate(const VehiclePropConfig& config,
130                                  float sampleRate);
131     static ClientId getClientId(const sp<IVehicleCallback>& callback);
132 
133     // Parses the cmdline options for "--set" command. "*prop" would be populated with the
134     // the properties to be set. Returns true when the cmdline options are valid, false otherwise.
135     static bool parseSetPropOptions(int fd, const hidl_vec<hidl_string>& options,
136                                     VehiclePropValue* prop);
137     // Parses the options and get the values for the current option specified by "*index". "*index"
138     // would advance to the next option field (e.g., the next "-f"). Returns a list of values for
139     // the current option.
140     static std::vector<std::string> getOptionValues(const hidl_vec<hidl_string>& options,
141                                                     size_t* index);
142 
143   private:
144     VehicleHal* mHal;
145     std::unique_ptr<VehiclePropConfigIndex> mConfigIndex;
146     SubscriptionManager mSubscriptionManager;
147 
148     hidl_vec<VehiclePropValue> mHidlVecOfVehiclePropValuePool;
149 
150     ConcurrentQueue<VehiclePropValuePtr> mEventQueue;
151     BatchingConsumer<VehiclePropValuePtr> mBatchingConsumer;
152     VehiclePropValuePool mValueObjectPool;
153 };
154 
155 }  // namespace V2_0
156 }  // namespace vehicle
157 }  // namespace automotive
158 }  // namespace hardware
159 }  // namespace android
160 
161 
162 #endif // android_hardware_automotive_vehicle_V2_0_VehicleHalManager_H_
163