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 "os/log.h"
27 #include "storage/storage_module.h"
28
29 using ::bluetooth::shim::GetStorage;
30 using ::bluetooth::storage::ConfigCacheHelper;
31
32 namespace bluetooth {
33 namespace shim {
34
HasSection(const std::string & section)35 bool BtifConfigInterface::HasSection(const std::string& section) {
36 return GetStorage()->HasSection(section);
37 }
38
HasProperty(const std::string & section,const std::string & property)39 bool BtifConfigInterface::HasProperty(const std::string& section,
40 const std::string& property) {
41 return GetStorage()->HasProperty(section, property);
42 }
43
GetInt(const std::string & section,const std::string & property,int * value)44 bool BtifConfigInterface::GetInt(const std::string& section,
45 const std::string& property, int* value) {
46 log::assert_that(value != nullptr, "assert failed: value != nullptr");
47 auto ret = GetStorage()->GetInt(section, property);
48 if (ret) {
49 *value = *ret;
50 }
51 return ret.has_value();
52 }
53
SetInt(const std::string & section,const std::string & property,int value)54 bool BtifConfigInterface::SetInt(const std::string& section,
55 const std::string& property, int value) {
56 GetStorage()->SetInt(section, property, value);
57 return true;
58 }
59
GetUint64(const std::string & section,const std::string & property,uint64_t * value)60 bool BtifConfigInterface::GetUint64(const std::string& section,
61 const std::string& property,
62 uint64_t* value) {
63 log::assert_that(value != nullptr, "assert failed: value != nullptr");
64 auto ret = GetStorage()->GetUint64(section, property);
65 if (ret) {
66 *value = *ret;
67 }
68 return ret.has_value();
69 }
70
SetUint64(const std::string & section,const std::string & property,uint64_t value)71 bool BtifConfigInterface::SetUint64(const std::string& section,
72 const std::string& property,
73 uint64_t value) {
74 GetStorage()->SetUint64(section, property, value);
75 return true;
76 }
77
GetStr(const std::string & section,const std::string & property,char * value,int * size_bytes)78 bool BtifConfigInterface::GetStr(const std::string& section,
79 const std::string& property, char* value,
80 int* size_bytes) {
81 log::assert_that(value != nullptr, "assert failed: value != nullptr");
82 log::assert_that(size_bytes != nullptr,
83 "assert failed: 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 log::assert_that(value != nullptr, "assert failed: value != nullptr");
116 log::assert_that(length != nullptr, "assert failed: 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 log::assert_that(value != nullptr, "assert failed: 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
RemoveSection(const std::string & section)146 void BtifConfigInterface::RemoveSection(const std::string& section) {
147 GetStorage()->RemoveSection(section);
148 }
149
GetPersistentDevices()150 std::vector<std::string> BtifConfigInterface::GetPersistentDevices() {
151 return GetStorage()->GetPersistentSections();
152 }
153
ConvertEncryptOrDecryptKeyIfNeeded()154 void BtifConfigInterface::ConvertEncryptOrDecryptKeyIfNeeded() {
155 GetStorage()->ConvertEncryptOrDecryptKeyIfNeeded();
156 }
157
Clear()158 void BtifConfigInterface::Clear() { GetStorage()->Clear(); }
159
160 } // namespace shim
161 } // namespace bluetooth
162