1 /*
2 * Copyright 2019 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 #include <cstdlib>
18 #include <string>
19
20 #include "Log.h"
21
22 #include <sys/system_properties.h>
23
24 #define LOG_TAG "SysProp"
25
getSystemPropViaGet(const char * key,std::string default_value="")26 std::string getSystemPropViaGet(const char* key, std::string default_value = "") {
27 char buffer[PROP_VALUE_MAX + 1];
28 int bufferLen = __system_property_get(key, buffer);
29 if (bufferLen > PROP_VALUE_MAX || bufferLen == 0) {
30 return default_value;
31 }
32 return std::string(buffer, bufferLen);
33 }
34
getSystemPropViaGetAsInt(const char * key,int default_value=0)35 int getSystemPropViaGetAsInt(const char* key, int default_value = 0) {
36 char buffer[PROP_VALUE_MAX + 1];
37 int bufferLen = __system_property_get(key, buffer);
38 if (bufferLen > PROP_VALUE_MAX || bufferLen == 0) {
39 return default_value;
40 }
41 return ::atoi(buffer);
42 }
43
getSystemPropViaGetAsBool(const char * key,bool default_value=false)44 int getSystemPropViaGetAsBool(const char* key, bool default_value = false) {
45 return getSystemPropViaGetAsInt(key, default_value) != 0;
46 }
47