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