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_obd2frame_include_Obd2SensorStore_H_ 18 #define android_hardware_automotive_vehicle_aidl_impl_fake_impl_obd2frame_include_Obd2SensorStore_H_ 19 20 #include <VehicleHalTypes.h> 21 #include <VehicleObjectPool.h> 22 #include <VehicleUtils.h> 23 24 #include <android-base/result.h> 25 26 #include <memory> 27 #include <vector> 28 29 namespace android { 30 namespace hardware { 31 namespace automotive { 32 namespace vehicle { 33 namespace fake { 34 namespace obd2frame { 35 36 // This class wraps all the logic required to create an OBD2 frame. 37 // It allows storing sensor values, setting appropriate bitmasks as needed, and returning 38 // appropriately laid out storage of sensor values suitable for being returned via a VehicleHal 39 // implementation. 40 class Obd2SensorStore final { 41 public: 42 // Creates a sensor storage with a given number of vendor-specific sensors. 43 Obd2SensorStore(std::shared_ptr<VehiclePropValuePool> valuePool, size_t numVendorIntegerSensors, 44 size_t numVendorFloatSensors); 45 46 template <class T> getLastIndex()47 static int getLastIndex() { 48 auto range = ndk::enum_range<T>(); 49 auto it = range.begin(); 50 while (std::next(it) != range.end()) { 51 it++; 52 } 53 return toInt(*it); 54 } 55 56 // Stores an integer-valued sensor. 57 aidl::android::hardware::automotive::vehicle::StatusCode setIntegerSensor( 58 aidl::android::hardware::automotive::vehicle::DiagnosticIntegerSensorIndex index, 59 int32_t value); 60 // Stores an integer-valued sensor. 61 aidl::android::hardware::automotive::vehicle::StatusCode setIntegerSensor(size_t index, 62 int32_t value); 63 // Stores a float-valued sensor. 64 aidl::android::hardware::automotive::vehicle::StatusCode setFloatSensor( 65 aidl::android::hardware::automotive::vehicle::DiagnosticFloatSensorIndex index, 66 float value); 67 // Stores a float-valued sensor. 68 aidl::android::hardware::automotive::vehicle::StatusCode setFloatSensor(size_t index, 69 float value); 70 71 // Returns a sensor property value using the given DTC. 72 VehiclePropValuePool::RecyclableType getSensorProperty(const std::string& dtc) const; 73 74 private: 75 class BitmaskInVector final { 76 public: 77 explicit BitmaskInVector(size_t numBits = 0); 78 void resize(size_t numBits); 79 android::base::Result<bool> get(size_t index) const; 80 android::base::Result<void> set(size_t index, bool value); 81 82 const std::vector<uint8_t>& getBitmask() const; 83 84 private: 85 std::vector<uint8_t> mStorage; 86 size_t mNumBits; 87 }; 88 89 std::vector<int32_t> mIntegerSensors; 90 std::vector<float> mFloatSensors; 91 BitmaskInVector mSensorsBitmask; 92 std::shared_ptr<VehiclePropValuePool> mValuePool; 93 94 // Returns a vector that contains all integer sensors stored. 95 const std::vector<int32_t>& getIntegerSensors() const; 96 // Returns a vector that contains all float sensors stored. 97 const std::vector<float>& getFloatSensors() const; 98 // Returns a vector that contains a bitmask for all stored sensors. 99 const std::vector<uint8_t>& getSensorsBitmask() const; 100 }; 101 102 } // namespace obd2frame 103 } // namespace fake 104 } // namespace vehicle 105 } // namespace automotive 106 } // namespace hardware 107 } // namespace android 108 109 #endif // android_hardware_automotive_vehicle_aidl_impl_fake_impl_obd2frame_include_Obd2SensorStore_H_ 110