1*8f0ba417SAndroid Build Coastguard Worker /*
2*8f0ba417SAndroid Build Coastguard Worker * Copyright (C) 2016 The Android Open Source Project
3*8f0ba417SAndroid Build Coastguard Worker *
4*8f0ba417SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*8f0ba417SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*8f0ba417SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*8f0ba417SAndroid Build Coastguard Worker *
8*8f0ba417SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*8f0ba417SAndroid Build Coastguard Worker *
10*8f0ba417SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*8f0ba417SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*8f0ba417SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*8f0ba417SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*8f0ba417SAndroid Build Coastguard Worker * limitations under the License.
15*8f0ba417SAndroid Build Coastguard Worker */
16*8f0ba417SAndroid Build Coastguard Worker
17*8f0ba417SAndroid Build Coastguard Worker #pragma once
18*8f0ba417SAndroid Build Coastguard Worker
19*8f0ba417SAndroid Build Coastguard Worker #include <sys/cdefs.h>
20*8f0ba417SAndroid Build Coastguard Worker
21*8f0ba417SAndroid Build Coastguard Worker #include <chrono>
22*8f0ba417SAndroid Build Coastguard Worker #include <limits>
23*8f0ba417SAndroid Build Coastguard Worker #include <mutex>
24*8f0ba417SAndroid Build Coastguard Worker #include <optional>
25*8f0ba417SAndroid Build Coastguard Worker #include <string>
26*8f0ba417SAndroid Build Coastguard Worker
27*8f0ba417SAndroid Build Coastguard Worker struct prop_info;
28*8f0ba417SAndroid Build Coastguard Worker
29*8f0ba417SAndroid Build Coastguard Worker namespace android {
30*8f0ba417SAndroid Build Coastguard Worker namespace base {
31*8f0ba417SAndroid Build Coastguard Worker
32*8f0ba417SAndroid Build Coastguard Worker // Returns the current value of the system property `key`,
33*8f0ba417SAndroid Build Coastguard Worker // or `default_value` if the property is empty or doesn't exist.
34*8f0ba417SAndroid Build Coastguard Worker std::string GetProperty(const std::string& key, const std::string& default_value);
35*8f0ba417SAndroid Build Coastguard Worker
36*8f0ba417SAndroid Build Coastguard Worker // Returns true if the system property `key` has the value "1", "y", "yes", "on", or "true",
37*8f0ba417SAndroid Build Coastguard Worker // false for "0", "n", "no", "off", or "false", or `default_value` otherwise.
38*8f0ba417SAndroid Build Coastguard Worker bool GetBoolProperty(const std::string& key, bool default_value);
39*8f0ba417SAndroid Build Coastguard Worker
40*8f0ba417SAndroid Build Coastguard Worker // Returns the signed integer corresponding to the system property `key`.
41*8f0ba417SAndroid Build Coastguard Worker // If the property is empty, doesn't exist, doesn't have an integer value, or is outside
42*8f0ba417SAndroid Build Coastguard Worker // the optional bounds, returns `default_value`.
43*8f0ba417SAndroid Build Coastguard Worker template <typename T> T GetIntProperty(const std::string& key,
44*8f0ba417SAndroid Build Coastguard Worker T default_value,
45*8f0ba417SAndroid Build Coastguard Worker T min = std::numeric_limits<T>::min(),
46*8f0ba417SAndroid Build Coastguard Worker T max = std::numeric_limits<T>::max());
47*8f0ba417SAndroid Build Coastguard Worker
48*8f0ba417SAndroid Build Coastguard Worker // Returns the unsigned integer corresponding to the system property `key`.
49*8f0ba417SAndroid Build Coastguard Worker // If the property is empty, doesn't exist, doesn't have an integer value, or is outside
50*8f0ba417SAndroid Build Coastguard Worker // the optional bound, returns `default_value`.
51*8f0ba417SAndroid Build Coastguard Worker template <typename T> T GetUintProperty(const std::string& key,
52*8f0ba417SAndroid Build Coastguard Worker T default_value,
53*8f0ba417SAndroid Build Coastguard Worker T max = std::numeric_limits<T>::max());
54*8f0ba417SAndroid Build Coastguard Worker
55*8f0ba417SAndroid Build Coastguard Worker // Sets the system property `key` to `value`.
56*8f0ba417SAndroid Build Coastguard Worker bool SetProperty(const std::string& key, const std::string& value);
57*8f0ba417SAndroid Build Coastguard Worker
58*8f0ba417SAndroid Build Coastguard Worker // Waits for the system property `key` to have the value `expected_value`.
59*8f0ba417SAndroid Build Coastguard Worker // Times out after `relative_timeout`.
60*8f0ba417SAndroid Build Coastguard Worker // Returns true on success, false on timeout.
61*8f0ba417SAndroid Build Coastguard Worker #if defined(__BIONIC__)
62*8f0ba417SAndroid Build Coastguard Worker bool WaitForProperty(const std::string& key, const std::string& expected_value,
63*8f0ba417SAndroid Build Coastguard Worker std::chrono::milliseconds relative_timeout = std::chrono::milliseconds::max());
64*8f0ba417SAndroid Build Coastguard Worker #endif
65*8f0ba417SAndroid Build Coastguard Worker
66*8f0ba417SAndroid Build Coastguard Worker // Waits for the system property `key` to be created.
67*8f0ba417SAndroid Build Coastguard Worker // Times out after `relative_timeout`.
68*8f0ba417SAndroid Build Coastguard Worker // Returns true on success, false on timeout.
69*8f0ba417SAndroid Build Coastguard Worker #if defined(__BIONIC__)
70*8f0ba417SAndroid Build Coastguard Worker bool WaitForPropertyCreation(const std::string& key, std::chrono::milliseconds relative_timeout =
71*8f0ba417SAndroid Build Coastguard Worker std::chrono::milliseconds::max());
72*8f0ba417SAndroid Build Coastguard Worker #endif
73*8f0ba417SAndroid Build Coastguard Worker
74*8f0ba417SAndroid Build Coastguard Worker #if defined(__BIONIC__) && __cplusplus >= 201703L
75*8f0ba417SAndroid Build Coastguard Worker // Cached system property lookup. For code that needs to read the same property multiple times,
76*8f0ba417SAndroid Build Coastguard Worker // this class helps optimize those lookups.
77*8f0ba417SAndroid Build Coastguard Worker class CachedProperty {
78*8f0ba417SAndroid Build Coastguard Worker public:
79*8f0ba417SAndroid Build Coastguard Worker explicit CachedProperty(std::string property_name);
80*8f0ba417SAndroid Build Coastguard Worker
81*8f0ba417SAndroid Build Coastguard Worker // Kept for ABI compatibility.
82*8f0ba417SAndroid Build Coastguard Worker explicit CachedProperty(const char* property_name);
83*8f0ba417SAndroid Build Coastguard Worker
84*8f0ba417SAndroid Build Coastguard Worker // Returns the current value of the underlying system property as cheaply as possible.
85*8f0ba417SAndroid Build Coastguard Worker // The returned pointer is valid until the next call to Get. Because most callers are going
86*8f0ba417SAndroid Build Coastguard Worker // to want to parse the string returned here and cache that as well, this function performs
87*8f0ba417SAndroid Build Coastguard Worker // no locking, and is completely thread unsafe. It is the caller's responsibility to provide a
88*8f0ba417SAndroid Build Coastguard Worker // lock for thread-safety.
89*8f0ba417SAndroid Build Coastguard Worker //
90*8f0ba417SAndroid Build Coastguard Worker // Note: *changed can be set to true even if the contents of the property remain the same.
91*8f0ba417SAndroid Build Coastguard Worker const char* Get(bool* changed = nullptr);
92*8f0ba417SAndroid Build Coastguard Worker
93*8f0ba417SAndroid Build Coastguard Worker // Waits for the property to be changed and then reads its value.
94*8f0ba417SAndroid Build Coastguard Worker // Times out returning nullptr, after `relative_timeout`
95*8f0ba417SAndroid Build Coastguard Worker //
96*8f0ba417SAndroid Build Coastguard Worker // Note: this can return the same value multiple times in a row if the property was set to the
97*8f0ba417SAndroid Build Coastguard Worker // same value or if multiple changes happened before the current thread was resumed.
98*8f0ba417SAndroid Build Coastguard Worker const char* WaitForChange(
99*8f0ba417SAndroid Build Coastguard Worker std::chrono::milliseconds relative_timeout = std::chrono::milliseconds::max());
100*8f0ba417SAndroid Build Coastguard Worker
101*8f0ba417SAndroid Build Coastguard Worker private:
102*8f0ba417SAndroid Build Coastguard Worker std::string property_name_;
103*8f0ba417SAndroid Build Coastguard Worker const prop_info* prop_info_;
104*8f0ba417SAndroid Build Coastguard Worker std::optional<uint32_t> cached_area_serial_;
105*8f0ba417SAndroid Build Coastguard Worker std::optional<uint32_t> cached_property_serial_;
106*8f0ba417SAndroid Build Coastguard Worker char cached_value_[92];
107*8f0ba417SAndroid Build Coastguard Worker bool is_read_only_;
108*8f0ba417SAndroid Build Coastguard Worker const char* read_only_property_;
109*8f0ba417SAndroid Build Coastguard Worker };
110*8f0ba417SAndroid Build Coastguard Worker
111*8f0ba417SAndroid Build Coastguard Worker // Helper class for passing the output of CachedProperty to a parser function, and then caching
112*8f0ba417SAndroid Build Coastguard Worker // that as well.
113*8f0ba417SAndroid Build Coastguard Worker template <typename Parser>
114*8f0ba417SAndroid Build Coastguard Worker class CachedParsedProperty {
115*8f0ba417SAndroid Build Coastguard Worker public:
116*8f0ba417SAndroid Build Coastguard Worker using value_type = std::remove_reference_t<std::invoke_result_t<Parser, const char*>>;
117*8f0ba417SAndroid Build Coastguard Worker
CachedParsedProperty(std::string property_name,Parser parser)118*8f0ba417SAndroid Build Coastguard Worker CachedParsedProperty(std::string property_name, Parser parser)
119*8f0ba417SAndroid Build Coastguard Worker : cached_property_(std::move(property_name)), parser_(std::move(parser)) {}
120*8f0ba417SAndroid Build Coastguard Worker
121*8f0ba417SAndroid Build Coastguard Worker // Returns the parsed value.
122*8f0ba417SAndroid Build Coastguard Worker // This function is internally-synchronized, so use from multiple threads is safe (but ordering
123*8f0ba417SAndroid Build Coastguard Worker // of course cannot be guaranteed without external synchronization).
124*8f0ba417SAndroid Build Coastguard Worker value_type Get(bool* changed = nullptr) {
125*8f0ba417SAndroid Build Coastguard Worker std::lock_guard<std::mutex> lock(mutex_);
126*8f0ba417SAndroid Build Coastguard Worker bool local_changed = false;
127*8f0ba417SAndroid Build Coastguard Worker const char* value = cached_property_.Get(&local_changed);
128*8f0ba417SAndroid Build Coastguard Worker if (!cached_result_ || local_changed) {
129*8f0ba417SAndroid Build Coastguard Worker cached_result_ = parser_(value);
130*8f0ba417SAndroid Build Coastguard Worker }
131*8f0ba417SAndroid Build Coastguard Worker
132*8f0ba417SAndroid Build Coastguard Worker if (changed) *changed = local_changed;
133*8f0ba417SAndroid Build Coastguard Worker return *cached_result_;
134*8f0ba417SAndroid Build Coastguard Worker }
135*8f0ba417SAndroid Build Coastguard Worker
136*8f0ba417SAndroid Build Coastguard Worker private:
137*8f0ba417SAndroid Build Coastguard Worker std::mutex mutex_;
138*8f0ba417SAndroid Build Coastguard Worker CachedProperty cached_property_;
139*8f0ba417SAndroid Build Coastguard Worker std::optional<value_type> cached_result_;
140*8f0ba417SAndroid Build Coastguard Worker
141*8f0ba417SAndroid Build Coastguard Worker Parser parser_;
142*8f0ba417SAndroid Build Coastguard Worker };
143*8f0ba417SAndroid Build Coastguard Worker
144*8f0ba417SAndroid Build Coastguard Worker // Helper for CachedParsedProperty that uses android::base::ParseBool.
145*8f0ba417SAndroid Build Coastguard Worker class CachedBoolProperty {
146*8f0ba417SAndroid Build Coastguard Worker public:
147*8f0ba417SAndroid Build Coastguard Worker explicit CachedBoolProperty(std::string property_name);
148*8f0ba417SAndroid Build Coastguard Worker
149*8f0ba417SAndroid Build Coastguard Worker // Returns the parsed bool, or std::nullopt if it wasn't set or couldn't be parsed.
150*8f0ba417SAndroid Build Coastguard Worker std::optional<bool> GetOptional();
151*8f0ba417SAndroid Build Coastguard Worker
152*8f0ba417SAndroid Build Coastguard Worker // Returns the parsed bool, or default_value if it wasn't set or couldn't be parsed.
153*8f0ba417SAndroid Build Coastguard Worker bool Get(bool default_value);
154*8f0ba417SAndroid Build Coastguard Worker
155*8f0ba417SAndroid Build Coastguard Worker private:
156*8f0ba417SAndroid Build Coastguard Worker CachedParsedProperty<std::optional<bool> (*)(const char*)> cached_parsed_property_;
157*8f0ba417SAndroid Build Coastguard Worker };
158*8f0ba417SAndroid Build Coastguard Worker
159*8f0ba417SAndroid Build Coastguard Worker #endif
160*8f0ba417SAndroid Build Coastguard Worker
HwTimeoutMultiplier()161*8f0ba417SAndroid Build Coastguard Worker static inline int HwTimeoutMultiplier() {
162*8f0ba417SAndroid Build Coastguard Worker return android::base::GetIntProperty("ro.hw_timeout_multiplier", 1);
163*8f0ba417SAndroid Build Coastguard Worker }
164*8f0ba417SAndroid Build Coastguard Worker
165*8f0ba417SAndroid Build Coastguard Worker } // namespace base
166*8f0ba417SAndroid Build Coastguard Worker } // namespace android
167*8f0ba417SAndroid Build Coastguard Worker
168*8f0ba417SAndroid Build Coastguard Worker #if !defined(__BIONIC__)
169*8f0ba417SAndroid Build Coastguard Worker /** Implementation detail. */
170*8f0ba417SAndroid Build Coastguard Worker extern "C" int __system_property_set(const char*, const char*);
171*8f0ba417SAndroid Build Coastguard Worker /** Implementation detail. */
172*8f0ba417SAndroid Build Coastguard Worker extern "C" int __system_property_get(const char*, char*);
173*8f0ba417SAndroid Build Coastguard Worker /** Implementation detail. */
174*8f0ba417SAndroid Build Coastguard Worker extern "C" const prop_info* __system_property_find(const char*);
175*8f0ba417SAndroid Build Coastguard Worker /** Implementation detail. */
176*8f0ba417SAndroid Build Coastguard Worker extern "C" void __system_property_read_callback(const prop_info*,
177*8f0ba417SAndroid Build Coastguard Worker void (*)(void*, const char*, const char*, uint32_t),
178*8f0ba417SAndroid Build Coastguard Worker void*);
179*8f0ba417SAndroid Build Coastguard Worker #endif
180