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