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, bool value) {
28   config_cache_.SetProperty(section, property, value ? "true" : "false");
29 }
30 
GetBool(const std::string & section,const std::string & property) const31 std::optional<bool> ConfigCacheHelper::GetBool(const std::string& section, const std::string& property) const {
32   auto value_str = config_cache_.GetProperty(section, property);
33   if (!value_str) {
34     return std::nullopt;
35   }
36   if (*value_str == "true") {
37     return true;
38   } else if (*value_str == "false") {
39     return false;
40   } else {
41     return std::nullopt;
42   }
43 }
44 
SetUint64(const std::string & section,const std::string & property,uint64_t value)45 void ConfigCacheHelper::SetUint64(const std::string& section, const std::string& property, uint64_t value) {
46   config_cache_.SetProperty(section, property, std::to_string(value));
47 }
48 
GetUint64(const std::string & section,const std::string & property) const49 std::optional<uint64_t> ConfigCacheHelper::GetUint64(const std::string& section, const std::string& property) const {
50   auto value_str = config_cache_.GetProperty(section, property);
51   if (!value_str) {
52     return std::nullopt;
53   }
54   return common::Uint64FromString(*value_str);
55 }
56 
SetUint32(const std::string & section,const std::string & property,uint32_t value)57 void ConfigCacheHelper::SetUint32(const std::string& section, const std::string& property, uint32_t value) {
58   config_cache_.SetProperty(section, property, std::to_string(value));
59 }
60 
GetUint32(const std::string & section,const std::string & property) const61 std::optional<uint32_t> ConfigCacheHelper::GetUint32(const std::string& section, const std::string& property) const {
62   auto value_str = config_cache_.GetProperty(section, property);
63   if (!value_str) {
64     return std::nullopt;
65   }
66   auto large_value = GetUint64(section, property);
67   if (!large_value) {
68     return std::nullopt;
69   }
70   if (!common::IsNumberInNumericLimits<uint32_t>(*large_value)) {
71     return std::nullopt;
72   }
73   return static_cast<uint32_t>(*large_value);
74 }
75 
SetInt64(const std::string & section,const std::string & property,int64_t value)76 void ConfigCacheHelper::SetInt64(const std::string& section, const std::string& property, int64_t value) {
77   config_cache_.SetProperty(section, property, std::to_string(value));
78 }
79 
GetInt64(const std::string & section,const std::string & property) const80 std::optional<int64_t> ConfigCacheHelper::GetInt64(const std::string& section, const std::string& property) const {
81   auto value_str = config_cache_.GetProperty(section, property);
82   if (!value_str) {
83     return std::nullopt;
84   }
85   return common::Int64FromString(*value_str);
86 }
87 
SetInt(const std::string & section,const std::string & property,int value)88 void ConfigCacheHelper::SetInt(const std::string& section, const std::string& property, int value) {
89   config_cache_.SetProperty(section, property, std::to_string(value));
90 }
91 
GetInt(const std::string & section,const std::string & property) const92 std::optional<int> ConfigCacheHelper::GetInt(const std::string& section, const std::string& property) const {
93   auto value_str = config_cache_.GetProperty(section, property);
94   if (!value_str) {
95     return std::nullopt;
96   }
97   auto large_value = GetInt64(section, property);
98   if (!large_value) {
99     return std::nullopt;
100   }
101   if (!common::IsNumberInNumericLimits<int>(*large_value)) {
102     return std::nullopt;
103   }
104   return static_cast<uint32_t>(*large_value);
105 }
106 
SetBin(const std::string & section,const std::string & property,const std::vector<uint8_t> & value)107 void ConfigCacheHelper::SetBin(
108     const std::string& section, const std::string& property, const std::vector<uint8_t>& value) {
109   config_cache_.SetProperty(section, property, common::ToHexString(value));
110 }
111 
GetBin(const std::string & section,const std::string & property) const112 std::optional<std::vector<uint8_t>> ConfigCacheHelper::GetBin(
113     const std::string& section, const std::string& property) const {
114   auto value_str = config_cache_.GetProperty(section, property);
115   if (!value_str) {
116     return std::nullopt;
117   }
118   auto value = common::FromHexString(*value_str);
119   if (!value) {
120     log::warn("value_str cannot be parsed to std::vector<uint8_t>");
121   }
122   return value;
123 }
124 
125 }  // namespace storage
126 }  // namespace bluetooth
127