1 /*
2  * Copyright 2022 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 <string>
18 
19 #include "common/strings.h"
20 #include "os/system_properties.h"
21 
22 namespace bluetooth {
23 namespace os {
24 
GetSystemPropertyUint32(const std::string & property,uint32_t default_value)25 uint32_t GetSystemPropertyUint32(const std::string& property, uint32_t default_value) {
26   return GetSystemPropertyUint32Base(property, default_value, 10);
27 }
28 
GetSystemPropertyUint32Base(const std::string & property,uint32_t default_value,int base)29 uint32_t GetSystemPropertyUint32Base(
30     const std::string& property, uint32_t default_value, int base) {
31   std::optional<std::string> result = GetSystemProperty(property);
32   if (result.has_value()) {
33     return static_cast<uint32_t>(std::stoul(*result, nullptr, base));
34   }
35   return default_value;
36 }
37 
GetSystemPropertyBool(const std::string & property,bool default_value)38 bool GetSystemPropertyBool(const std::string& property, bool default_value) {
39   std::optional<std::string> result = GetSystemProperty(property);
40   if (result.has_value()) {
41     std::string trimmed_val = common::StringTrim(result.value());
42     if (trimmed_val == "true" || trimmed_val == "1") {
43       return true;
44     }
45     if (trimmed_val == "false" || trimmed_val == "0") {
46       return false;
47     }
48   }
49   return default_value;
50 }
51 
52 }  // namespace os
53 }  // namespace bluetooth
54