• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2020 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 "storage/config_cache_helper.h"
18 
19 #include "common/numbers.h"
20 #include "common/strings.h"
21 
22 namespace bluetooth {
23 namespace storage {
24 
SetBool(const std::string & section,const std::string & property,bool value)25 void ConfigCacheHelper::SetBool(const std::string& section, const std::string& property, bool value) {
26   config_cache_.SetProperty(section, property, value ? "true" : "false");
27 }
28 
GetBool(const std::string & section,const std::string & property) const29 std::optional<bool> ConfigCacheHelper::GetBool(const std::string& section, const std::string& property) const {
30   auto value_str = config_cache_.GetProperty(section, property);
31   if (!value_str) {
32     return std::nullopt;
33   }
34   if (*value_str == "true") {
35     return true;
36   } else if (*value_str == "false") {
37     return false;
38   } else {
39     return std::nullopt;
40   }
41 }
42 
SetUint64(const std::string & section,const std::string & property,uint64_t value)43 void ConfigCacheHelper::SetUint64(const std::string& section, const std::string& property, uint64_t value) {
44   config_cache_.SetProperty(section, property, std::to_string(value));
45 }
46 
GetUint64(const std::string & section,const std::string & property) const47 std::optional<uint64_t> ConfigCacheHelper::GetUint64(const std::string& section, const std::string& property) const {
48   auto value_str = config_cache_.GetProperty(section, property);
49   if (!value_str) {
50     return std::nullopt;
51   }
52   return common::Uint64FromString(*value_str);
53 }
54 
SetUint32(const std::string & section,const std::string & property,uint32_t value)55 void ConfigCacheHelper::SetUint32(const std::string& section, const std::string& property, uint32_t value) {
56   config_cache_.SetProperty(section, property, std::to_string(value));
57 }
58 
GetUint32(const std::string & section,const std::string & property) const59 std::optional<uint32_t> ConfigCacheHelper::GetUint32(const std::string& section, const std::string& property) const {
60   auto value_str = config_cache_.GetProperty(section, property);
61   if (!value_str) {
62     return std::nullopt;
63   }
64   auto large_value = GetUint64(section, property);
65   if (!large_value) {
66     return std::nullopt;
67   }
68   if (!common::IsNumberInNumericLimits<uint32_t>(*large_value)) {
69     return std::nullopt;
70   }
71   return static_cast<uint32_t>(*large_value);
72 }
73 
SetInt64(const std::string & section,const std::string & property,int64_t value)74 void ConfigCacheHelper::SetInt64(const std::string& section, const std::string& property, int64_t value) {
75   config_cache_.SetProperty(section, property, std::to_string(value));
76 }
77 
GetInt64(const std::string & section,const std::string & property) const78 std::optional<int64_t> ConfigCacheHelper::GetInt64(const std::string& section, const std::string& property) const {
79   auto value_str = config_cache_.GetProperty(section, property);
80   if (!value_str) {
81     return std::nullopt;
82   }
83   return common::Int64FromString(*value_str);
84 }
85 
SetInt(const std::string & section,const std::string & property,int value)86 void ConfigCacheHelper::SetInt(const std::string& section, const std::string& property, int value) {
87   config_cache_.SetProperty(section, property, std::to_string(value));
88 }
89 
GetInt(const std::string & section,const std::string & property) const90 std::optional<int> ConfigCacheHelper::GetInt(const std::string& section, const std::string& property) const {
91   auto value_str = config_cache_.GetProperty(section, property);
92   if (!value_str) {
93     return std::nullopt;
94   }
95   auto large_value = GetInt64(section, property);
96   if (!large_value) {
97     return std::nullopt;
98   }
99   if (!common::IsNumberInNumericLimits<int>(*large_value)) {
100     return std::nullopt;
101   }
102   return static_cast<uint32_t>(*large_value);
103 }
104 
SetBin(const std::string & section,const std::string & property,const std::vector<uint8_t> & value)105 void ConfigCacheHelper::SetBin(
106     const std::string& section, const std::string& property, const std::vector<uint8_t>& value) {
107   config_cache_.SetProperty(section, property, common::ToHexString(value));
108 }
109 
GetBin(const std::string & section,const std::string & property) const110 std::optional<std::vector<uint8_t>> ConfigCacheHelper::GetBin(
111     const std::string& section, const std::string& property) const {
112   auto value_str = config_cache_.GetProperty(section, property);
113   if (!value_str) {
114     return std::nullopt;
115   }
116   auto value = common::FromHexString(*value_str);
117   if (!value) {
118     LOG_WARN("value_str cannot be parsed to std::vector<uint8_t>");
119   }
120   return value;
121 }
122 
123 }  // namespace storage
124 }  // namespace bluetooth