1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "kv_adapter_manager.h"
16
17 #include <mutex>
18 #include <unistd.h>
19
20 #include "datetime_ex.h"
21 #include "string_ex.h"
22
23 #include "dm_anonymous.h"
24 #include "dm_error_type.h"
25 #include "dm_log.h"
26
27 namespace OHOS {
28 namespace DistributedHardware {
29 namespace {
30 constexpr const char* DM_KV_STORE_PREFIX = "DM2_";
31 constexpr const char* DM_KV_STORE_FREEZE_PREFIX = "anti_ddos_local_";
32 constexpr const char* DB_KEY_DELIMITER = "###";
33 constexpr int64_t DM_KV_STORE_REFRESH_TIME = 24 * 60 * 60; // one day
34 constexpr int64_t MAX_SUPPORTED_EXIST_TIME = 3 * 24 * 60 * 60; // 3days
35 constexpr const char* DM_OSTYPE_PREFIX = "ostype";
36 constexpr const char* DM_UDID_PREFIX = "udid";
37 }
38
39 DM_IMPLEMENT_SINGLE_INSTANCE(KVAdapterManager);
40
ComposeOsTypePrefix()41 std::string ComposeOsTypePrefix()
42 {
43 std::string dmKey = std::string(DM_OSTYPE_PREFIX) + std::string(DB_KEY_DELIMITER) + std::string(DM_UDID_PREFIX) +
44 std::string(DB_KEY_DELIMITER);
45 return dmKey;
46 }
47
Init()48 DM_EXPORT int32_t KVAdapterManager::Init()
49 {
50 LOGI("Init Kv-Adapter manager");
51 {
52 std::lock_guard<std::mutex> lock(idCacheMapMtx_);
53 idCacheMap_.clear();
54 }
55 kvAdapter_ = std::make_shared<KVAdapter>();
56 return kvAdapter_->Init();
57 }
58
UnInit()59 DM_EXPORT void KVAdapterManager::UnInit()
60 {
61 LOGI("Uninit Kv-Adapter manager");
62 CHECK_NULL_VOID(kvAdapter_);
63 kvAdapter_->UnInit();
64 kvAdapter_ = nullptr;
65 }
66
ReInit()67 DM_EXPORT void KVAdapterManager::ReInit()
68 {
69 LOGI("Re init kv adapter");
70 CHECK_NULL_VOID(kvAdapter_);
71 kvAdapter_->ReInit();
72 }
73
PutByAnoyDeviceId(const std::string & key,const DmKVValue & value)74 int32_t KVAdapterManager::PutByAnoyDeviceId(const std::string &key, const DmKVValue &value)
75 {
76 std::string dmKey = DM_KV_STORE_PREFIX + key;
77 std::lock_guard<std::mutex> lock(idCacheMapMtx_);
78 auto idIter = idCacheMap_.find(dmKey);
79 if (idIter != idCacheMap_.end() && !IsTimeOut(idIter->second.lastModifyTime, value.lastModifyTime,
80 DM_KV_STORE_REFRESH_TIME)) {
81 LOGD("Kv value is existed");
82 return DM_OK;
83 }
84 idCacheMap_[dmKey] = value;
85 std::string prefixKey = DM_KV_STORE_PREFIX + value.appID + DB_KEY_DELIMITER + value.udidHash;
86 idCacheMap_[prefixKey] = value;
87 std::string valueStr = "";
88 ConvertDmKVValueToJson(value, valueStr);
89 CHECK_NULL_RETURN(kvAdapter_, ERR_DM_POINT_NULL);
90 if (kvAdapter_->Put(dmKey, valueStr) != DM_OK) {
91 LOGE("Insert value to DB for dmKey failed");
92 return ERR_DM_FAILED;
93 }
94 if (kvAdapter_->Put(prefixKey, valueStr) != DM_OK) {
95 LOGE("Insert value to DB for prefixKey failed");
96 return ERR_DM_FAILED;
97 }
98 return DM_OK;
99 }
100
Get(const std::string & key,DmKVValue & value)101 DM_EXPORT int32_t KVAdapterManager::Get(const std::string &key, DmKVValue &value)
102 {
103 std::string dmKey = DM_KV_STORE_PREFIX + key;
104 std::lock_guard<std::mutex> lock(idCacheMapMtx_);
105 auto idIter = idCacheMap_.find(dmKey);
106 if (idIter != idCacheMap_.end()) {
107 value = idIter->second;
108 return DM_OK;
109 }
110 CHECK_NULL_RETURN(kvAdapter_, ERR_DM_POINT_NULL);
111 std::string valueStr;
112 if (kvAdapter_->Get(dmKey, valueStr) != DM_OK) {
113 LOGE("Get kv value failed, dmKey: %{public}s", GetAnonyString(dmKey).c_str());
114 return ERR_DM_FAILED;
115 }
116 ConvertJsonToDmKVValue(valueStr, value);
117 idCacheMap_[dmKey] = value;
118 std::string prefixKey = DM_KV_STORE_PREFIX + value.appID + DB_KEY_DELIMITER + value.udidHash;
119 idCacheMap_[prefixKey] = value;
120 return DM_OK;
121 }
122
DeleteAgedEntry()123 DM_EXPORT int32_t KVAdapterManager::DeleteAgedEntry()
124 {
125 int64_t nowTime = GetSecondsSince1970ToNow();
126 std::lock_guard<std::mutex> lock(idCacheMapMtx_);
127 for (auto it = idCacheMap_.begin(); it != idCacheMap_.end();) {
128 if (IsTimeOut(it->second.lastModifyTime, nowTime, MAX_SUPPORTED_EXIST_TIME)) {
129 it = idCacheMap_.erase(it);
130 } else {
131 ++it;
132 }
133 }
134 return DM_OK;
135 }
136
IsTimeOut(int64_t sourceTime,int64_t targetTime,int64_t timeOut)137 inline bool KVAdapterManager::IsTimeOut(int64_t sourceTime, int64_t targetTime, int64_t timeOut)
138 {
139 return targetTime - sourceTime >= timeOut ? true : false;
140 }
141
AppUnintall(const std::string & appId)142 DM_EXPORT int32_t KVAdapterManager::AppUnintall(const std::string &appId)
143 {
144 LOGI("appId %{public}s.", GetAnonyString(appId).c_str());
145 std::lock_guard<std::mutex> lock(idCacheMapMtx_);
146 for (auto it = idCacheMap_.begin(); it != idCacheMap_.end();) {
147 if (it->second.appID == appId) {
148 it = idCacheMap_.erase(it);
149 } else {
150 ++it;
151 }
152 }
153 CHECK_NULL_RETURN(kvAdapter_, ERR_DM_POINT_NULL);
154 if (kvAdapter_->DeleteByAppId(appId, DM_KV_STORE_PREFIX) != DM_OK) {
155 LOGE("DeleteByAppId failed");
156 return ERR_DM_FAILED;
157 }
158 return DM_OK;
159 }
160
GetFreezeData(const std::string & key,std::string & value)161 DM_EXPORT int32_t KVAdapterManager::GetFreezeData(const std::string &key, std::string &value)
162 {
163 std::string dmKey = DM_KV_STORE_FREEZE_PREFIX + key;
164 CHECK_NULL_RETURN(kvAdapter_, ERR_DM_POINT_NULL);
165 if (kvAdapter_->Get(dmKey, value) != DM_OK) {
166 LOGE("Get freeze data failed, dmKey: %{public}s", GetAnonyString(dmKey).c_str());
167 return ERR_DM_FAILED;
168 }
169 return DM_OK;
170 }
171
PutFreezeData(const std::string & key,std::string & value)172 DM_EXPORT int32_t KVAdapterManager::PutFreezeData(const std::string &key, std::string &value)
173 {
174 std::string dmKey = DM_KV_STORE_FREEZE_PREFIX + key;
175 CHECK_NULL_RETURN(kvAdapter_, ERR_DM_POINT_NULL);
176 if (kvAdapter_->Put(dmKey, value) != DM_OK) {
177 LOGE("Insert freeze data failed, k:%{public}s, v:%{public}s", dmKey.c_str(), value.c_str());
178 return ERR_DM_FAILED;
179 }
180 LOGI("Insert freeze data success, k:%{public}s, v:%{public}s", dmKey.c_str(), value.c_str());
181 return DM_OK;
182 }
183
DeleteFreezeData(const std::string & key)184 DM_EXPORT int32_t KVAdapterManager::DeleteFreezeData(const std::string &key)
185 {
186 std::string dmKey = DM_KV_STORE_FREEZE_PREFIX + key;
187 CHECK_NULL_RETURN(kvAdapter_, ERR_DM_POINT_NULL);
188 if (kvAdapter_->Delete(dmKey) != DM_OK) {
189 LOGE("delete freeze data failed, dmKey: %{public}s", GetAnonyString(dmKey).c_str());
190 return ERR_DM_FAILED;
191 }
192 return DM_OK;
193 }
194
GetAllOstypeData(std::vector<std::string> & values)195 DM_EXPORT int32_t KVAdapterManager::GetAllOstypeData(std::vector<std::string> &values)
196 {
197 std::string dmKey = ComposeOsTypePrefix();
198 CHECK_NULL_RETURN(kvAdapter_, ERR_DM_POINT_NULL);
199 if (kvAdapter_->GetAllOstypeData(dmKey, values) != DM_OK) {
200 LOGE("Get all data failed, dmKey: %{public}s", GetAnonyString(dmKey).c_str());
201 return ERR_DM_FAILED;
202 }
203 return DM_OK;
204 }
205
PutOstypeData(const std::string & key,const std::string & value)206 DM_EXPORT int32_t KVAdapterManager::PutOstypeData(const std::string &key, const std::string &value)
207 {
208 LOGI("key %{public}s, value %{public}s.", GetAnonyString(key).c_str(), value.c_str());
209 std::string dmKey = ComposeOsTypePrefix() + key;
210 CHECK_NULL_RETURN(kvAdapter_, ERR_DM_POINT_NULL);
211 if (kvAdapter_->Put(dmKey, value) != DM_OK) {
212 LOGE("Insert data failed, k:%{public}s, v:%{public}s", GetAnonyString(dmKey).c_str(), value.c_str());
213 return ERR_DM_FAILED;
214 }
215 return DM_OK;
216 }
217
DeleteOstypeData(const std::string & key)218 DM_EXPORT int32_t KVAdapterManager::DeleteOstypeData(const std::string &key)
219 {
220 LOGI("key %{public}s.", GetAnonyString(key).c_str());
221 std::string dmKey = ComposeOsTypePrefix() + key;
222 CHECK_NULL_RETURN(kvAdapter_, ERR_DM_POINT_NULL);
223 if (kvAdapter_->Delete(dmKey) != DM_OK) {
224 LOGE("delete data failed, dmKey: %{public}s", GetAnonyString(dmKey).c_str());
225 return ERR_DM_FAILED;
226 }
227 return DM_OK;
228 }
229
GetLocalUserIdData(const std::string & key,std::string & value)230 DM_EXPORT int32_t KVAdapterManager::GetLocalUserIdData(const std::string &key, std::string &value)
231 {
232 LOGI("key %{public}s.", GetAnonyString(key).c_str());
233 CHECK_NULL_RETURN(kvAdapter_, ERR_DM_POINT_NULL);
234 if (kvAdapter_->Get(key, value) != DM_OK) {
235 LOGE("Get data failed, key: %{public}s", GetAnonyString(key).c_str());
236 return ERR_DM_FAILED;
237 }
238 return DM_OK;
239 }
240
PutLocalUserIdData(const std::string & key,const std::string & value)241 DM_EXPORT int32_t KVAdapterManager::PutLocalUserIdData(const std::string &key, const std::string &value)
242 {
243 LOGI("key %{public}s, value %{public}s.", GetAnonyString(key).c_str(), value.c_str());
244 CHECK_NULL_RETURN(kvAdapter_, ERR_DM_POINT_NULL);
245 if (kvAdapter_->Put(key, value) != DM_OK) {
246 LOGE("Put data failed, key:%{public}s, value:%{public}s", GetAnonyString(key).c_str(), value.c_str());
247 return ERR_DM_FAILED;
248 }
249 return DM_OK;
250 }
251
GetOsTypeCount(int32_t & count)252 DM_EXPORT int32_t KVAdapterManager::GetOsTypeCount(int32_t &count)
253 {
254 CHECK_NULL_RETURN(kvAdapter_, ERR_DM_POINT_NULL);
255 std::string osTypePrefix = ComposeOsTypePrefix();
256 if (kvAdapter_->GetOstypeCountByPrefix(osTypePrefix, count) != DM_OK) {
257 LOGE("GetOstypeCountByPrefix failed, osTypePrefix:%{public}s.", osTypePrefix.c_str());
258 return ERR_DM_FAILED;
259 }
260 LOGI("count %{public}d.", count);
261 return DM_OK;
262 }
263 } // namespace DistributedHardware
264 } // namespace OHOS
265