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