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 #define LOG_TAG "bt_shim_storage"
18 
19 #include "main/shim/config.h"
20 
21 #include <algorithm>
22 #include <cstdint>
23 #include <cstring>
24 
25 #include "main/shim/entry.h"
26 #include "storage/storage_module.h"
27 
28 using ::bluetooth::shim::GetStorage;
29 using ::bluetooth::storage::ConfigCacheHelper;
30 
31 namespace bluetooth {
32 namespace shim {
33 
HasSection(const std::string & section)34 bool BtifConfigInterface::HasSection(const std::string& section) {
35   return GetStorage()->HasSection(section);
36 }
37 
HasProperty(const std::string & section,const std::string & property)38 bool BtifConfigInterface::HasProperty(const std::string& section, const std::string& property) {
39   return GetStorage()->HasProperty(section, property);
40 }
41 
GetInt(const std::string & section,const std::string & property,int * value)42 bool BtifConfigInterface::GetInt(const std::string& section, const std::string& property,
43                                  int* value) {
44   log::assert_that(value != nullptr, "assert failed: value != nullptr");
45   auto ret = GetStorage()->GetInt(section, property);
46   if (ret) {
47     *value = *ret;
48   }
49   return ret.has_value();
50 }
51 
SetInt(const std::string & section,const std::string & property,int value)52 bool BtifConfigInterface::SetInt(const std::string& section, const std::string& property,
53                                  int value) {
54   GetStorage()->SetInt(section, property, value);
55   return true;
56 }
57 
GetUint64(const std::string & section,const std::string & property,uint64_t * value)58 bool BtifConfigInterface::GetUint64(const std::string& section, const std::string& property,
59                                     uint64_t* value) {
60   log::assert_that(value != nullptr, "assert failed: value != nullptr");
61   auto ret = GetStorage()->GetUint64(section, property);
62   if (ret) {
63     *value = *ret;
64   }
65   return ret.has_value();
66 }
67 
SetUint64(const std::string & section,const std::string & property,uint64_t value)68 bool BtifConfigInterface::SetUint64(const std::string& section, const std::string& property,
69                                     uint64_t value) {
70   GetStorage()->SetUint64(section, property, value);
71   return true;
72 }
73 
GetStr(const std::string & section,const std::string & property,char * value,int * size_bytes)74 bool BtifConfigInterface::GetStr(const std::string& section, const std::string& property,
75                                  char* value, int* size_bytes) {
76   log::assert_that(value != nullptr, "assert failed: value != nullptr");
77   log::assert_that(size_bytes != nullptr, "assert failed: size_bytes != nullptr");
78   if (*size_bytes == 0) {
79     return HasProperty(section, property);
80   }
81   auto str = GetStorage()->GetProperty(section, property);
82   if (!str) {
83     return false;
84   }
85   // std::string::copy does not null-terminate resultant string by default
86   // avoided using strlcpy to prevent extra dependency
87   *size_bytes = str->copy(value, (*size_bytes - 1));
88   value[*size_bytes] = '\0';
89   *size_bytes += 1;
90   return true;
91 }
92 
GetStr(const std::string & section,const std::string & property)93 std::optional<std::string> BtifConfigInterface::GetStr(const std::string& section,
94                                                        const std::string& property) {
95   return GetStorage()->GetProperty(section, property);
96 }
97 
SetStr(const std::string & section,const std::string & property,const std::string & value)98 bool BtifConfigInterface::SetStr(const std::string& section, const std::string& property,
99                                  const std::string& value) {
100   GetStorage()->SetProperty(section, property, value);
101   return true;
102 }
103 
104 // TODO: implement encrypted read
GetBin(const std::string & section,const std::string & property,uint8_t * value,size_t * length)105 bool BtifConfigInterface::GetBin(const std::string& section, const std::string& property,
106                                  uint8_t* value, size_t* length) {
107   log::assert_that(value != nullptr, "assert failed: value != nullptr");
108   log::assert_that(length != nullptr, "assert failed: length != nullptr");
109   auto value_vec = GetStorage()->GetBin(section, property);
110   if (!value_vec) {
111     return false;
112   }
113   *length = std::min(value_vec->size(), *length);
114   std::memcpy(value, value_vec->data(), *length);
115   return true;
116 }
GetBinLength(const std::string & section,const std::string & property)117 size_t BtifConfigInterface::GetBinLength(const std::string& section, const std::string& property) {
118   auto value_vec = GetStorage()->GetBin(section, property);
119   if (!value_vec) {
120     return 0;
121   }
122   return value_vec->size();
123 }
SetBin(const std::string & section,const std::string & property,const uint8_t * value,size_t length)124 bool BtifConfigInterface::SetBin(const std::string& section, const std::string& property,
125                                  const uint8_t* value, size_t length) {
126   log::assert_that(value != nullptr, "assert failed: value != nullptr");
127   std::vector<uint8_t> value_vec(value, value + length);
128   GetStorage()->SetBin(section, property, value_vec);
129   return true;
130 }
RemoveProperty(const std::string & section,const std::string & property)131 bool BtifConfigInterface::RemoveProperty(const std::string& section, const std::string& property) {
132   return GetStorage()->RemoveProperty(section, property);
133 }
134 
RemoveSection(const std::string & section)135 void BtifConfigInterface::RemoveSection(const std::string& section) {
136   GetStorage()->RemoveSection(section);
137 }
138 
RemoveSectionWithProperty(const std::string & property)139 void BtifConfigInterface::RemoveSectionWithProperty(const std::string& property) {
140   GetStorage()->RemoveSectionWithProperty(property);
141 }
142 
GetPersistentDevices()143 std::vector<std::string> BtifConfigInterface::GetPersistentDevices() {
144   return GetStorage()->GetPersistentSections();
145 }
146 
ConvertEncryptOrDecryptKeyIfNeeded()147 void BtifConfigInterface::ConvertEncryptOrDecryptKeyIfNeeded() {
148   GetStorage()->ConvertEncryptOrDecryptKeyIfNeeded();
149 }
150 
Clear()151 void BtifConfigInterface::Clear() { GetStorage()->Clear(); }
152 
153 }  // namespace shim
154 }  // namespace bluetooth
155