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_V2_0_impl_DefaultVehicleHal_H_ 18 #define android_hardware_automotive_vehicle_V2_0_impl_DefaultVehicleHal_H_ 19 20 #include <vhal_v2_0/RecurrentTimer.h> 21 #include <vhal_v2_0/VehicleHal.h> 22 #include <vhal_v2_0/VehiclePropertyStore.h> 23 24 #include "FakeUserHal.h" 25 #include "VehicleHalClient.h" 26 27 namespace android { 28 namespace hardware { 29 namespace automotive { 30 namespace vehicle { 31 namespace V2_0 { 32 33 namespace impl { 34 35 /** Implementation of VehicleHal that connected to emulator instead of real vehicle network. */ 36 class DefaultVehicleHal : public VehicleHal { 37 public: 38 DefaultVehicleHal(VehiclePropertyStore* propStore, VehicleHalClient* client); 39 ~DefaultVehicleHal(); 40 41 // Initialize VHAL. Should always call registerHeartBeatEvent() during onCreate. 42 void onCreate() override; 43 std::vector<VehiclePropConfig> listProperties() override; 44 VehiclePropValuePtr get(const VehiclePropValue& requestedPropValue, 45 StatusCode* outStatus) override; 46 StatusCode set(const VehiclePropValue& propValue) override; 47 StatusCode subscribe(int32_t property, float sampleRate) override; 48 StatusCode unsubscribe(int32_t property) override; 49 bool dump(const hidl_handle& fd, const hidl_vec<hidl_string>& options) override; 50 51 protected: hertzToNanoseconds(float hz)52 constexpr std::chrono::nanoseconds hertzToNanoseconds(float hz) const { 53 return std::chrono::nanoseconds(static_cast<int64_t>(1000000000L / hz)); 54 } 55 56 VehiclePropertyStore* mPropStore; 57 RecurrentTimer mRecurrentTimer; 58 VehicleHalClient* mVehicleClient; 59 FakeUserHal mFakeUserHal; 60 61 // The callback that would be called when a property value is updated. This function could 62 // be extended to handle specific property update event. 63 virtual void onPropertyValue(const VehiclePropValue& value, bool updateStatus); 64 // Do an internal health check, vendor should add health check logic in this function. 65 virtual VehicleHal::VehiclePropValuePtr doInternalHealthCheck(); 66 67 // The callback that would be called for every event generated by 'subscribe' or heartbeat. 68 // Properties contains a list of properties that need to be handled. 69 void onContinuousPropertyTimer(const std::vector<int32_t>& properties); 70 // Initiate config for all properties, would be called during onCreate(). 71 void initStaticConfig(); 72 // Whether the property is a continuous property. 73 bool isContinuousProperty(int32_t propId) const; 74 // Returns a lambda that could be used in mRecurrentTimer. 75 RecurrentTimer::Action getTimerAction(); 76 // Check whether a propValue is valid according to its type. 77 StatusCode checkPropValue(const VehiclePropValue& propValue, const VehiclePropConfig* config); 78 // Check whether the property value is within the range according to area config. 79 StatusCode checkValueRange(const VehiclePropValue& propValue, 80 const VehicleAreaConfig* areaConfig); 81 // Register the heart beat event to be sent every 3s. This is required to inform watch dog that 82 // VHAL is alive. Subclasses should always calls this function during onCreate. 83 void registerHeartBeatEvent(); 84 // Get a user HAL property. 85 VehiclePropValuePtr getUserHalProp(const VehiclePropValue& requestedPropValue, 86 StatusCode* outStatus); 87 // Set a user HAL property. 88 StatusCode setUserHalProp(const VehiclePropValue& propValue); 89 // Create a VHAL heart beat property. 90 VehicleHal::VehiclePropValuePtr createVhalHeartBeatProp(); 91 92 private: 93 // Check whether a vendor mixed value property is valid according to its config array. 94 // See 'VehiclePropertyType' documentation in 'types.hal' for detail. 95 StatusCode checkVendorMixedPropValue(const VehiclePropValue& value, 96 const VehiclePropConfig* config); 97 // Read the override properties from a config file. 98 void getAllPropertiesOverride(); 99 }; 100 101 } // namespace impl 102 103 } // namespace V2_0 104 } // namespace vehicle 105 } // namespace automotive 106 } // namespace hardware 107 } // namespace android 108 109 #endif // android_hardware_automotive_vehicle_V2_0_impl_DefaultVehicleHal_H_ 110