• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     /*
74      * Stores provided value. Returns true if value was written returns false if config for
75      * example wasn't registered.
76      *
77      * The property value's timestamp will be set to the current ElapsedRealTimeNano.
78      */
79     bool writeValueWithCurrentTimestamp(VehiclePropValue* propValuePtr, bool updateStatus);
80 
81     std::unique_ptr<VehiclePropValue> refreshTimestamp(int32_t propId, int32_t areaId);
82 
83     void removeValue(const VehiclePropValue& propValue);
84     void removeValuesForProperty(int32_t propId);
85 
86     std::vector<VehiclePropValue> readAllValues() const;
87     std::vector<VehiclePropValue> readValuesForProperty(int32_t propId) const;
88     std::unique_ptr<VehiclePropValue> readValueOrNull(const VehiclePropValue& request) const;
89     std::unique_ptr<VehiclePropValue> readValueOrNull(int32_t prop, int32_t area = 0,
90                                                       int64_t token = 0) const;
91 
92     std::vector<VehiclePropConfig> getAllConfigs() const;
93     const VehiclePropConfig* getConfigOrNull(int32_t propId) const;
94     const VehiclePropConfig* getConfigOrDie(int32_t propId) const;
95 
96 private:
97     RecordId getRecordIdLocked(const VehiclePropValue& valuePrototype) const;
98     const VehiclePropValue* getValueOrNullLocked(const RecordId& recId) const;
99     PropertyMapRange findRangeLocked(int32_t propId) const;
100 
101 private:
102     using MuxGuard = std::lock_guard<std::mutex>;
103     mutable std::mutex mLock;
104     std::unordered_map<int32_t /* VehicleProperty */, RecordConfig> mConfigs;
105 
106     PropertyMap mPropertyValues;  // Sorted map of RecordId : VehiclePropValue.
107 
108     bool writeValueLocked(const VehiclePropValue& propValue, bool updateStatus);
109 };
110 
111 }  // namespace V2_0
112 }  // namespace vehicle
113 }  // namespace automotive
114 }  // namespace hardware
115 }  // namespace android
116 
117 #endif //android_hardware_automotive_vehicle_V2_0_impl_PropertyDb_H_
118