summaryrefslogtreecommitdiff
path: root/automotive/vehicle/aidl/impl/utils/common/include/VehiclePropertyStore.h
blob: 3d25cd3a414489a838b72489146055c7373ea42c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef android_hardware_automotive_vehicle_aidl_impl_utils_common_include_VehiclePropertyStore_H_
#define android_hardware_automotive_vehicle_aidl_impl_utils_common_include_VehiclePropertyStore_H_

#include <cstdint>
#include <map>
#include <memory>
#include <mutex>
#include <unordered_map>

#include <VehicleHalTypes.h>
#include <VehicleObjectPool.h>
#include <VehicleUtils.h>
#include <android-base/result.h>
#include <android-base/thread_annotations.h>

namespace android {
namespace hardware {
namespace automotive {
namespace vehicle {

// Encapsulates work related to storing and accessing configuration, storing and modifying
// vehicle property values.
//
// VehiclePropertyValues stored in a sorted map thus it makes easier to get range of values, e.g.
// to get value for all areas for particular property.
//
// This class is thread-safe, however it uses blocking synchronization across all methods.
class VehiclePropertyStore final {
  public:
    using ValueResultType = VhalResult<VehiclePropValuePool::RecyclableType>;
    using ValuesResultType = VhalResult<std::vector<VehiclePropValuePool::RecyclableType>>;

    enum class EventMode : uint8_t {
        /**
         * Only invoke OnValueChangeCallback if the new property value (ignoring timestamp) is
         * different than the existing value.
         *
         * This should be used for regular cases.
         */
        ON_VALUE_CHANGE,
        /**
         * Always invoke OnValueChangeCallback.
         *
         * This should be used for the special properties that are used for delivering event, e.g.
         * HW_KEY_INPUT.
         */
        ALWAYS,
        /**
         * Never invoke OnValueChangeCallback.
         *
         * This should be used for continuous property subscription when the sample rate for the
         * subscription is smaller than the refresh rate for the property. E.g., the vehicle speed
         * is refreshed at 20hz, but we are only subscribing at 10hz. In this case, we want to
         * generate the property change event at 10hz, not 20hz, but we still want to refresh the
         * timestamp (via writeValue) at 20hz.
         */
        NEVER,
    };

    explicit VehiclePropertyStore(std::shared_ptr<VehiclePropValuePool> valuePool)
        : mValuePool(valuePool) {}

    ~VehiclePropertyStore();

    // Callback when a property value has been updated or a new value added.
    using OnValueChangeCallback = std::function<void(
            const aidl::android::hardware::automotive::vehicle::VehiclePropValue&)>;

    // Function that used to calculate unique token for given VehiclePropValue.
    using TokenFunction = std::function<int64_t(
            const aidl::android::hardware::automotive::vehicle::VehiclePropValue& value)>;

    // Register the given property according to the config. A property has to be registered first
    // before write/read. If tokenFunc is not nullptr, it would be used to generate a unique
    // property token to act as the key the property store. Otherwise, {propertyID, areaID} would be
    // used as the key.
    void registerProperty(
            const aidl::android::hardware::automotive::vehicle::VehiclePropConfig& config,
            TokenFunction tokenFunc = nullptr);

    // Stores provided value. Returns error if config wasn't registered. If 'updateStatus' is
    // true, the 'status' in 'propValue' would be stored. Otherwise, if this is a new value,
    // 'status' would be initialized to {@code VehiclePropertyStatus::AVAILABLE}, if this is to
    // override an existing value, the status for the existing value would be used for the
    // overridden value.
    // 'EventMode' controls whether the 'OnValueChangeCallback' will be called for this operation.
    VhalResult<void> writeValue(VehiclePropValuePool::RecyclableType propValue,
                                bool updateStatus = false,
                                EventMode mode = EventMode::ON_VALUE_CHANGE);

    // Remove a given property value from the property store. The 'propValue' would be used to
    // generate the key for the value to remove.
    void removeValue(
            const aidl::android::hardware::automotive::vehicle::VehiclePropValue& propValue);

    // Remove all the values for the property.
    void removeValuesForProperty(int32_t propId);

    // Read all the stored values.
    std::vector<VehiclePropValuePool::RecyclableType> readAllValues() const;

    // Read all the values for the property.
    ValuesResultType readValuesForProperty(int32_t propId) const;

    // Read the value for the requested property. Returns {@code StatusCode::NOT_AVAILABLE} if the
    // value has not been set yet. Returns {@code StatusCode::INVALID_ARG} if the property is
    // not configured.
    ValueResultType readValue(
            const aidl::android::hardware::automotive::vehicle::VehiclePropValue& request) const;

    // Read the value for the requested property. Returns {@code StatusCode::NOT_AVAILABLE} if the
    // value has not been set yet. Returns {@code StatusCode::INVALID_ARG} if the property is
    // not configured.
    ValueResultType readValue(int32_t prop, int32_t area = 0, int64_t token = 0) const;

    // Get all property configs.
    std::vector<aidl::android::hardware::automotive::vehicle::VehiclePropConfig> getAllConfigs()
            const;

    // Get the property config for the requested property.
    android::base::Result<const aidl::android::hardware::automotive::vehicle::VehiclePropConfig*,
                          VhalError>
    getConfig(int32_t propId) const;

    // Set a callback that would be called when a property value has been updated.
    void setOnValueChangeCallback(const OnValueChangeCallback& callback);

    inline std::shared_ptr<VehiclePropValuePool> getValuePool() { return mValuePool; }

  private:
    struct RecordId {
        int32_t area;
        int64_t token;

        std::string toString() const;

        bool operator==(const RecordId& other) const;
    };

    struct RecordIdHash {
        size_t operator()(RecordId const& recordId) const;
    };

    struct Record {
        aidl::android::hardware::automotive::vehicle::VehiclePropConfig propConfig;
        TokenFunction tokenFunction;
        std::unordered_map<RecordId, VehiclePropValuePool::RecyclableType, RecordIdHash> values;
    };

    // {@code VehiclePropValuePool} is thread-safe.
    std::shared_ptr<VehiclePropValuePool> mValuePool;
    mutable std::mutex mLock;
    std::unordered_map<int32_t, Record> mRecordsByPropId GUARDED_BY(mLock);
    OnValueChangeCallback mOnValueChangeCallback GUARDED_BY(mLock);

    const Record* getRecordLocked(int32_t propId) const;

    Record* getRecordLocked(int32_t propId);

    RecordId getRecordIdLocked(
            const aidl::android::hardware::automotive::vehicle::VehiclePropValue& propValue,
            const Record& record) const;

    ValueResultType readValueLocked(const RecordId& recId, const Record& record) const;
};

}  // namespace vehicle
}  // namespace automotive
}  // namespace hardware
}  // namespace android

#endif  // android_hardware_automotive_vehicle_aidl_impl_utils_common_include_VehiclePropertyStore_H_