1 /* 2 * Copyright (C) 2017 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_PropertyDb_H_ 18 #define android_hardware_automotive_vehicle_V2_0_impl_PropertyDb_H_ 19 20 #include <cstdint> 21 #include <unordered_map> 22 #include <map> 23 #include <memory> 24 #include <mutex> 25 26 #include <android/hardware/automotive/vehicle/2.0/types.h> 27 28 namespace android { 29 namespace hardware { 30 namespace automotive { 31 namespace vehicle { 32 namespace V2_0 { 33 34 /** 35 * Encapsulates work related to storing and accessing configuration, storing and modifying 36 * vehicle property values. 37 * 38 * VehiclePropertyValues stored in a sorted map thus it makes easier to get range of values, e.g. 39 * to get value for all areas for particular property. 40 * 41 * This class is thread-safe, however it uses blocking synchronization across all methods. 42 */ 43 class VehiclePropertyStore { 44 public: 45 /* Function that used to calculate unique token for given VehiclePropValue */ 46 using TokenFunction = std::function<int64_t(const VehiclePropValue& value)>; 47 48 private: 49 struct RecordConfig { 50 VehiclePropConfig propConfig; 51 TokenFunction tokenFunction; 52 }; 53 54 struct RecordId { 55 int32_t prop; 56 int32_t area; 57 int64_t token; 58 59 bool operator==(const RecordId& other) const; 60 bool operator<(const RecordId& other) const; 61 }; 62 63 using PropertyMap = std::map<RecordId, VehiclePropValue>; 64 using PropertyMapRange = std::pair<PropertyMap::const_iterator, PropertyMap::const_iterator>; 65 66 public: 67 void registerProperty(const VehiclePropConfig& config, TokenFunction tokenFunc = nullptr); 68 69 /* Stores provided value. Returns true if value was written returns false if config for 70 * example wasn't registered. */ 71 bool writeValue(const VehiclePropValue& propValue, bool updateStatus); 72 73 void removeValue(const VehiclePropValue& propValue); 74 void removeValuesForProperty(int32_t propId); 75 76 std::vector<VehiclePropValue> readAllValues() const; 77 std::vector<VehiclePropValue> readValuesForProperty(int32_t propId) const; 78 std::unique_ptr<VehiclePropValue> readValueOrNull(const VehiclePropValue& request) const; 79 std::unique_ptr<VehiclePropValue> readValueOrNull(int32_t prop, int32_t area = 0, 80 int64_t token = 0) const; 81 82 std::vector<VehiclePropConfig> getAllConfigs() const; 83 const VehiclePropConfig* getConfigOrNull(int32_t propId) const; 84 const VehiclePropConfig* getConfigOrDie(int32_t propId) const; 85 86 private: 87 RecordId getRecordIdLocked(const VehiclePropValue& valuePrototype) const; 88 const VehiclePropValue* getValueOrNullLocked(const RecordId& recId) const; 89 PropertyMapRange findRangeLocked(int32_t propId) const; 90 91 private: 92 using MuxGuard = std::lock_guard<std::mutex>; 93 mutable std::mutex mLock; 94 std::unordered_map<int32_t /* VehicleProperty */, RecordConfig> mConfigs; 95 96 PropertyMap mPropertyValues; // Sorted map of RecordId : VehiclePropValue. 97 }; 98 99 } // namespace V2_0 100 } // namespace vehicle 101 } // namespace automotive 102 } // namespace hardware 103 } // namespace android 104 105 #endif //android_hardware_automotive_vehicle_V2_0_impl_PropertyDb_H_ 106