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 <bluetooth/log.h>
20 
21 #include "common/numbers.h"
22 #include "common/strings.h"
23 
24 namespace bluetooth {
25 namespace storage {
26 
SetBool(const std::string & section,const std::string & property,bool value)27 void ConfigCacheHelper::SetBool(const std::string& section, const std::string& property,
28                                 bool value) {
29   config_cache_.SetProperty(section, property, value ? "true" : "false");
30 }
31 
GetBool(const std::string & section,const std::string & property) const32 std::optional<bool> ConfigCacheHelper::GetBool(const std::string& section,
33                                                const std::string& property) const {
34   auto value_str = config_cache_.GetProperty(section, property);
35   if (!value_str) {
36     return std::nullopt;
37   }
38   if (*value_str == "true") {
39     return true;
40   } else if (*value_str == "false") {
41     return false;
42   } else {
43     return std::nullopt;
44   }
45 }
46 
SetUint64(const std::string & section,const std::string & property,uint64_t value)47 void ConfigCacheHelper::SetUint64(const std::string& section, const std::string& property,
48                                   uint64_t value) {
49   config_cache_.SetProperty(section, property, std::to_string(value));
50 }
51 
GetUint64(const std::string & section,const std::string & property) const52 std::optional<uint64_t> ConfigCacheHelper::GetUint64(const std::string& section,
53                                                      const std::string& property) const {
54   auto value_str = config_cache_.GetProperty(section, property);
55   if (!value_str) {
56     return std::nullopt;
57   }
58   return common::Uint64FromString(*value_str);
59 }
60 
SetUint32(const std::string & section,const std::string & property,uint32_t value)61 void ConfigCacheHelper::SetUint32(const std::string& section, const std::string& property,
62                                   uint32_t value) {
63   config_cache_.SetProperty(section, property, std::to_string(value));
64 }
65 
GetUint32(const std::string & section,const std::string & property) const66 std::optional<uint32_t> ConfigCacheHelper::GetUint32(const std::string& section,
67                                                      const std::string& property) const {
68   auto value_str = config_cache_.GetProperty(section, property);
69   if (!value_str) {
70     return std::nullopt;
71   }
72   auto large_value = GetUint64(section, property);
73   if (!large_value) {
74     return std::nullopt;
75   }
76   if (!common::IsNumberInNumericLimits<uint32_t>(*large_value)) {
77     return std::nullopt;
78   }
79   return static_cast<uint32_t>(*large_value);
80 }
81 
SetInt64(const std::string & section,const std::string & property,int64_t value)82 void ConfigCacheHelper::SetInt64(const std::string& section, const std::string& property,
83                                  int64_t value) {
84   config_cache_.SetProperty(section, property, std::to_string(value));
85 }
86 
GetInt64(const std::string & section,const std::string & property) const87 std::optional<int64_t> ConfigCacheHelper::GetInt64(const std::string& section,
88                                                    const std::string& property) const {
89   auto value_str = config_cache_.GetProperty(section, property);
90   if (!value_str) {
91     return std::nullopt;
92   }
93   return common::Int64FromString(*value_str);
94 }
95 
SetInt(const std::string & section,const std::string & property,int value)96 void ConfigCacheHelper::SetInt(const std::string& section, const std::string& property, int value) {
97   config_cache_.SetProperty(section, property, std::to_string(value));
98 }
99 
GetInt(const std::string & section,const std::string & property) const100 std::optional<int> ConfigCacheHelper::GetInt(const std::string& section,
101                                              const std::string& property) const {
102   auto value_str = config_cache_.GetProperty(section, property);
103   if (!value_str) {
104     return std::nullopt;
105   }
106   auto large_value = GetInt64(section, property);
107   if (!large_value) {
108     return std::nullopt;
109   }
110   if (!common::IsNumberInNumericLimits<int>(*large_value)) {
111     return std::nullopt;
112   }
113   return static_cast<uint32_t>(*large_value);
114 }
115 
SetBin(const std::string & section,const std::string & property,const std::vector<uint8_t> & value)116 void ConfigCacheHelper::SetBin(const std::string& section, const std::string& property,
117                                const std::vector<uint8_t>& value) {
118   config_cache_.SetProperty(section, property, common::ToHexString(value));
119 }
120 
GetBin(const std::string & section,const std::string & property) const121 std::optional<std::vector<uint8_t>> ConfigCacheHelper::GetBin(const std::string& section,
122                                                               const std::string& property) const {
123   auto value_str = config_cache_.GetProperty(section, property);
124   if (!value_str) {
125     return std::nullopt;
126   }
127   auto value = common::FromHexString(*value_str);
128   if (!value) {
129     log::warn("value_str cannot be parsed to std::vector<uint8_t>");
130   }
131   return value;
132 }
133 
134 }  // namespace storage
135 }  // namespace bluetooth
136