• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 #pragma once
18 
19 #include <sys/cdefs.h>
20 
21 #include <chrono>
22 #include <limits>
23 #include <string>
24 
25 namespace android {
26 namespace base {
27 
28 // Returns the current value of the system property `key`,
29 // or `default_value` if the property is empty or doesn't exist.
30 std::string GetProperty(const std::string& key, const std::string& default_value);
31 
32 // Returns true if the system property `key` has the value "1", "y", "yes", "on", or "true",
33 // false for "0", "n", "no", "off", or "false", or `default_value` otherwise.
34 bool GetBoolProperty(const std::string& key, bool default_value);
35 
36 // Returns the signed integer corresponding to the system property `key`.
37 // If the property is empty, doesn't exist, doesn't have an integer value, or is outside
38 // the optional bounds, returns `default_value`.
39 template <typename T> T GetIntProperty(const std::string& key,
40                                        T default_value,
41                                        T min = std::numeric_limits<T>::min(),
42                                        T max = std::numeric_limits<T>::max());
43 
44 // Returns the unsigned integer corresponding to the system property `key`.
45 // If the property is empty, doesn't exist, doesn't have an integer value, or is outside
46 // the optional bound, returns `default_value`.
47 template <typename T> T GetUintProperty(const std::string& key,
48                                         T default_value,
49                                         T max = std::numeric_limits<T>::max());
50 
51 // Sets the system property `key` to `value`.
52 // Note that system property setting is inherently asynchronous so a return value of `true`
53 // isn't particularly meaningful, and immediately reading back the value won't necessarily
54 // tell you whether or not your call succeeded. A `false` return value definitely means failure.
55 bool SetProperty(const std::string& key, const std::string& value);
56 
57 // Waits for the system property `key` to have the value `expected_value`.
58 // Times out after `relative_timeout`.
59 // Returns true on success, false on timeout.
60 #if defined(__BIONIC__)
61 bool WaitForProperty(const std::string& key, const std::string& expected_value,
62                      std::chrono::milliseconds relative_timeout = std::chrono::milliseconds::max());
63 #endif
64 
65 // Waits for the system property `key` to be created.
66 // Times out after `relative_timeout`.
67 // Returns true on success, false on timeout.
68 #if defined(__BIONIC__)
69 bool WaitForPropertyCreation(const std::string& key, std::chrono::milliseconds relative_timeout =
70                                                          std::chrono::milliseconds::max());
71 #endif
72 
73 } // namespace base
74 } // namespace android
75