1 /* 2 * Copyright (C) 2021 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_aidl_impl_fake_impl_hardware_include_FakeVehicleHardware_H_ 18 #define android_hardware_automotive_vehicle_aidl_impl_fake_impl_hardware_include_FakeVehicleHardware_H_ 19 20 #include <ConcurrentQueue.h> 21 #include <ConfigDeclaration.h> 22 #include <FakeObd2Frame.h> 23 #include <FakeUserHal.h> 24 #include <GeneratorHub.h> 25 #include <IVehicleHardware.h> 26 #include <JsonConfigLoader.h> 27 #include <RecurrentTimer.h> 28 #include <VehicleHalTypes.h> 29 #include <VehiclePropertyStore.h> 30 #include <aidl/android/hardware/automotive/vehicle/VehicleHwKeyInputAction.h> 31 #include <android-base/parseint.h> 32 #include <android-base/result.h> 33 #include <android-base/stringprintf.h> 34 #include <android-base/thread_annotations.h> 35 36 #include <memory> 37 #include <mutex> 38 #include <unordered_map> 39 #include <vector> 40 41 namespace android { 42 namespace hardware { 43 namespace automotive { 44 namespace vehicle { 45 namespace fake { 46 47 class FakeVehicleHardware : public IVehicleHardware { 48 public: 49 using ValueResultType = VhalResult<VehiclePropValuePool::RecyclableType>; 50 51 FakeVehicleHardware(); 52 53 FakeVehicleHardware(std::string defaultConfigDir, std::string overrideConfigDir, 54 bool forceOverride); 55 56 ~FakeVehicleHardware(); 57 58 // Get all the property configs. 59 std::vector<aidl::android::hardware::automotive::vehicle::VehiclePropConfig> 60 getAllPropertyConfigs() const override; 61 62 // Set property values asynchronously. Server could return before the property set requests 63 // are sent to vehicle bus or before property set confirmation is received. The callback is 64 // safe to be called after the function returns and is safe to be called in a different thread. 65 aidl::android::hardware::automotive::vehicle::StatusCode setValues( 66 std::shared_ptr<const SetValuesCallback> callback, 67 const std::vector<aidl::android::hardware::automotive::vehicle::SetValueRequest>& 68 requests) override; 69 70 // Get property values asynchronously. Server could return before the property values are ready. 71 // The callback is safe to be called after the function returns and is safe to be called in a 72 // different thread. 73 aidl::android::hardware::automotive::vehicle::StatusCode getValues( 74 std::shared_ptr<const GetValuesCallback> callback, 75 const std::vector<aidl::android::hardware::automotive::vehicle::GetValueRequest>& 76 requests) const override; 77 78 // Dump debug information in the server. 79 DumpResult dump(const std::vector<std::string>& options) override; 80 81 // Check whether the system is healthy, return {@code StatusCode::OK} for healthy. 82 aidl::android::hardware::automotive::vehicle::StatusCode checkHealth() override; 83 84 // Register a callback that would be called when there is a property change event from vehicle. 85 void registerOnPropertyChangeEvent( 86 std::unique_ptr<const PropertyChangeCallback> callback) override; 87 88 // Register a callback that would be called when there is a property set error event from 89 // vehicle. 90 void registerOnPropertySetErrorEvent( 91 std::unique_ptr<const PropertySetErrorCallback> callback) override; 92 93 // Update the sample rate for the [propId, areaId] pair. 94 aidl::android::hardware::automotive::vehicle::StatusCode updateSampleRate( 95 int32_t propId, int32_t areaId, float sampleRate) override; 96 97 protected: 98 // mValuePool is also used in mServerSidePropStore. 99 const std::shared_ptr<VehiclePropValuePool> mValuePool; 100 const std::shared_ptr<VehiclePropertyStore> mServerSidePropStore; 101 102 ValueResultType getValue( 103 const aidl::android::hardware::automotive::vehicle::VehiclePropValue& value) const; 104 105 VhalResult<void> setValue( 106 const aidl::android::hardware::automotive::vehicle::VehiclePropValue& value); 107 108 private: 109 // Expose private methods to unit test. 110 friend class FakeVehicleHardwareTestHelper; 111 112 template <class CallbackType, class RequestType> 113 struct RequestWithCallback { 114 RequestType request; 115 std::shared_ptr<const CallbackType> callback; 116 }; 117 118 template <class CallbackType, class RequestType> 119 class PendingRequestHandler { 120 public: 121 PendingRequestHandler(FakeVehicleHardware* hardware); 122 123 void addRequest(RequestType request, std::shared_ptr<const CallbackType> callback); 124 125 void stop(); 126 127 private: 128 FakeVehicleHardware* mHardware; 129 std::thread mThread; 130 ConcurrentQueue<RequestWithCallback<CallbackType, RequestType>> mRequests; 131 132 void handleRequestsOnce(); 133 }; 134 135 const std::unique_ptr<obd2frame::FakeObd2Frame> mFakeObd2Frame; 136 const std::unique_ptr<FakeUserHal> mFakeUserHal; 137 // RecurrentTimer is thread-safe. 138 std::unique_ptr<RecurrentTimer> mRecurrentTimer; 139 // GeneratorHub is thread-safe. 140 std::unique_ptr<GeneratorHub> mGeneratorHub; 141 142 // Only allowed to set once. 143 std::unique_ptr<const PropertyChangeCallback> mOnPropertyChangeCallback; 144 std::unique_ptr<const PropertySetErrorCallback> mOnPropertySetErrorCallback; 145 146 std::mutex mLock; 147 std::unordered_map<PropIdAreaId, std::shared_ptr<RecurrentTimer::Callback>, PropIdAreaIdHash> 148 mRecurrentActions GUARDED_BY(mLock); 149 std::unordered_map<PropIdAreaId, VehiclePropValuePool::RecyclableType, PropIdAreaIdHash> 150 mSavedProps GUARDED_BY(mLock); 151 // PendingRequestHandler is thread-safe. 152 mutable PendingRequestHandler<GetValuesCallback, 153 aidl::android::hardware::automotive::vehicle::GetValueRequest> 154 mPendingGetValueRequests; 155 mutable PendingRequestHandler<SetValuesCallback, 156 aidl::android::hardware::automotive::vehicle::SetValueRequest> 157 mPendingSetValueRequests; 158 159 const std::string mDefaultConfigDir; 160 const std::string mOverrideConfigDir; 161 const bool mForceOverride; 162 bool mAddExtraTestVendorConfigs; 163 164 // Only used during initialization. 165 JsonConfigLoader mLoader; 166 167 void init(); 168 // Stores the initial value to property store. 169 void storePropInitialValue(const ConfigDeclaration& config); 170 // The callback that would be called when a vehicle property value change happens. 171 void onValueChangeCallback( 172 const aidl::android::hardware::automotive::vehicle::VehiclePropValue& value); 173 // Load the config files in format '*.json' from the directory and parse the config files 174 // into a map from property ID to ConfigDeclarations. 175 void loadPropConfigsFromDir(const std::string& dirPath, 176 std::unordered_map<int32_t, ConfigDeclaration>* configs); 177 // Function to be called when a value change event comes from vehicle bus. In our fake 178 // implementation, this function is only called during "--inject-event" dump command. 179 void eventFromVehicleBus( 180 const aidl::android::hardware::automotive::vehicle::VehiclePropValue& value); 181 182 int getHvacTempNumIncrements(int requestedTemp, int minTemp, int maxTemp, int increment); 183 void updateHvacTemperatureValueSuggestionInput( 184 const std::vector<int>& hvacTemperatureSetConfigArray, 185 std::vector<float>* hvacTemperatureValueSuggestionInput); 186 VhalResult<void> setHvacTemperatureValueSuggestion( 187 const aidl::android::hardware::automotive::vehicle::VehiclePropValue& 188 hvacTemperatureValueSuggestion); 189 VhalResult<void> maybeSetSpecialValue( 190 const aidl::android::hardware::automotive::vehicle::VehiclePropValue& value, 191 bool* isSpecialValue); 192 ValueResultType maybeGetSpecialValue( 193 const aidl::android::hardware::automotive::vehicle::VehiclePropValue& value, 194 bool* isSpecialValue) const; 195 VhalResult<void> setApPowerStateReport( 196 const aidl::android::hardware::automotive::vehicle::VehiclePropValue& value); 197 VehiclePropValuePool::RecyclableType createApPowerStateReq( 198 aidl::android::hardware::automotive::vehicle::VehicleApPowerStateReq state); 199 VehiclePropValuePool::RecyclableType createAdasStateReq(int32_t propertyId, int32_t areaId, 200 int32_t state); 201 VhalResult<void> setUserHalProp( 202 const aidl::android::hardware::automotive::vehicle::VehiclePropValue& value); 203 ValueResultType getUserHalProp( 204 const aidl::android::hardware::automotive::vehicle::VehiclePropValue& value) const; 205 ValueResultType getEchoReverseBytes( 206 const aidl::android::hardware::automotive::vehicle::VehiclePropValue& value) const; 207 bool isHvacPropAndHvacNotAvailable(int32_t propId, int32_t areaId) const; 208 VhalResult<void> isAdasPropertyAvailable(int32_t adasStatePropertyId) const; 209 210 std::unordered_map<int32_t, ConfigDeclaration> loadConfigDeclarations(); 211 212 std::string dumpAllProperties(); 213 std::string dumpOnePropertyByConfig( 214 int rowNumber, 215 const aidl::android::hardware::automotive::vehicle::VehiclePropConfig& config); 216 std::string dumpOnePropertyById(int32_t propId, int32_t areaId); 217 std::string dumpHelp(); 218 std::string dumpListProperties(); 219 std::string dumpSpecificProperty(const std::vector<std::string>& options); 220 std::string dumpSetProperties(const std::vector<std::string>& options); 221 std::string dumpGetPropertyWithArg(const std::vector<std::string>& options); 222 std::string dumpSaveProperty(const std::vector<std::string>& options); 223 std::string dumpRestoreProperty(const std::vector<std::string>& options); 224 std::string dumpInjectEvent(const std::vector<std::string>& options); 225 226 template <typename T> safelyParseInt(int index,const std::string & s)227 android::base::Result<T> safelyParseInt(int index, const std::string& s) { 228 T out; 229 if (!::android::base::ParseInt(s, &out)) { 230 return android::base::Error() << android::base::StringPrintf( 231 "non-integer argument at index %d: %s\n", index, s.c_str()); 232 } 233 return out; 234 } 235 android::base::Result<float> safelyParseFloat(int index, const std::string& s); 236 std::vector<std::string> getOptionValues(const std::vector<std::string>& options, 237 size_t* index); 238 android::base::Result<aidl::android::hardware::automotive::vehicle::VehiclePropValue> 239 parsePropOptions(const std::vector<std::string>& options); 240 android::base::Result<std::vector<uint8_t>> parseHexString(const std::string& s); 241 242 android::base::Result<void> checkArgumentsSize(const std::vector<std::string>& options, 243 size_t minSize); 244 aidl::android::hardware::automotive::vehicle::GetValueResult handleGetValueRequest( 245 const aidl::android::hardware::automotive::vehicle::GetValueRequest& request); 246 aidl::android::hardware::automotive::vehicle::SetValueResult handleSetValueRequest( 247 const aidl::android::hardware::automotive::vehicle::SetValueRequest& request); 248 249 std::string genFakeDataCommand(const std::vector<std::string>& options); 250 void sendHvacPropertiesCurrentValues(int32_t areaId); 251 void sendAdasPropertiesState(int32_t propertyId, int32_t state); 252 void generateVendorConfigs( 253 std::vector<aidl::android::hardware::automotive::vehicle::VehiclePropConfig>&) const; 254 255 static aidl::android::hardware::automotive::vehicle::VehiclePropValue createHwInputKeyProp( 256 aidl::android::hardware::automotive::vehicle::VehicleHwKeyInputAction action, 257 int32_t keyCode, int32_t targetDisplay); 258 static aidl::android::hardware::automotive::vehicle::VehiclePropValue createHwKeyInputV2Prop( 259 int32_t area, int32_t targetDisplay, int32_t keyCode, int32_t action, 260 int32_t repeatCount); 261 static aidl::android::hardware::automotive::vehicle::VehiclePropValue createHwMotionInputProp( 262 int32_t area, int32_t display, int32_t inputType, int32_t action, int32_t buttonState, 263 int32_t pointerCount, int32_t pointerId[], int32_t toolType[], float xData[], 264 float yData[], float pressure[], float size[]); 265 266 static std::string genFakeDataHelp(); 267 static std::string parseErrMsg(std::string fieldName, std::string value, std::string type); 268 }; 269 270 } // namespace fake 271 } // namespace vehicle 272 } // namespace automotive 273 } // namespace hardware 274 } // namespace android 275 276 #endif // android_hardware_automotive_vehicle_aidl_impl_fake_impl_hardware_include_FakeVehicleHardware_H_ 277