• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "business_event_adapter.h"
16 
17 #include <cinttypes>
18 #include <mutex>
19 
20 #include "datetime_ex.h"
21 #include "string_ex.h"
22 #include "ffrt.h"
23 
24 #include "distributed_device_profile_errors.h"
25 #include "distributed_device_profile_log.h"
26 #include "distributed_device_profile_constants.h"
27 #include "profile_cache.h"
28 
29 namespace OHOS {
30 namespace DistributedDeviceProfile {
31 using namespace OHOS::DistributedKv;
32 namespace {
33     constexpr int32_t MAX_INIT_RETRY_TIMES = 30;
34     constexpr int32_t INIT_RETRY_SLEEP_INTERVAL = 200 * 1000;
35     constexpr uint32_t MAX_BATCH_SIZE = 128;
36     const std::string DATABASE_DIR = "/data/service/el1/public/database/distributed_device_profile_service";
37     const std::string TAG = "BusinessEventAdapter";
38     const std::string APP_ID = "distributed_device_profile_service";
39     const std::string STORE_ID = "dp_kv_store_business";
40 }
41 
BusinessEventAdapter(const std::shared_ptr<DistributedKv::KvStoreDeathRecipient> & deathListener,DistributedKv::DataType dataType)42 BusinessEventAdapter::BusinessEventAdapter(
43     const std::shared_ptr<DistributedKv::KvStoreDeathRecipient> &deathListener, DistributedKv::DataType dataType)
44 {
45     this->deathRecipient_ = deathListener;
46     this->dataType_ = dataType;
47     HILOGD("BusinessEventAdapter Constructor Success");
48 }
49 
Init()50 int32_t BusinessEventAdapter::Init()
51 {
52     HILOGI("Init local DB, dataType: %{public}d", static_cast<int32_t>(dataType_));
53     if (isInited_.load()) {
54         HILOGI("Local DB already inited.");
55         return DP_SUCCESS;
56     }
57     this->appId_.appId = APP_ID;
58     this->storeId_.storeId = STORE_ID;
59     int32_t tryTimes = MAX_INIT_RETRY_TIMES;
60     int64_t beginTime = GetTickCount();
61     while (tryTimes > 0) {
62         DistributedKv::Status status = GetKvStorePtr();
63         if (status == DistributedKv::Status::SUCCESS && kvStorePtr_ != nullptr) {
64             HILOGI("Init KvStorePtr Success");
65             RegisterKvStoreDeathListener();
66             isInited_.store(true);
67             return DP_SUCCESS;
68         }
69         HILOGI("CheckKvStore, left times: %{public}d, status: %{public}d", tryTimes, status);
70         usleep(INIT_RETRY_SLEEP_INTERVAL);
71         tryTimes--;
72     }
73     if (kvStorePtr_ == nullptr) {
74         HILOGE("kvStorePtr is nullptr!");
75         return DP_KV_DB_PTR_NULL;
76     }
77     isInited_.store(true);
78     return DP_SUCCESS;
79 }
80 
UnInit()81 int32_t BusinessEventAdapter::UnInit()
82 {
83     HILOGI("BusinessEventAdapter UnInit");
84     if (isInited_.load()) {
85         if (kvStorePtr_ == nullptr) {
86             HILOGE("kvStorePtr is nullptr!");
87             return DP_KV_DB_PTR_NULL;
88         }
89         UnregisterKvStoreDeathListener();
90         kvStorePtr_.reset();
91         isInited_.store(false);
92     }
93     return DP_SUCCESS;
94 }
95 
ReInit()96 int32_t BusinessEventAdapter::ReInit()
97 {
98     HILOGI("BusinessEventAdapter ReInit");
99     UnInit();
100     return Init();
101 }
102 
Put(const std::string & key,const std::string & value)103 int32_t BusinessEventAdapter::Put(const std::string& key, const std::string& value)
104 {
105     if (key.empty() || key.size() > MAX_STRING_LEN || value.empty() || value.size() > MAX_STRING_LEN) {
106         HILOGE("Param is invalid: key or value is empty or too long");
107         return DP_INVALID_PARAMS;
108     }
109     DistributedKv::Status status;
110     {
111         std::lock_guard<std::mutex> lock(BusinessAdapterMutex_);
112         if (kvStorePtr_ == nullptr) {
113             HILOGE("kvDBPtr is null!");
114             return DP_KV_DB_PTR_NULL;
115         }
116 
117         DistributedKv::Key kvKey(key);
118         DistributedKv::Value kvValue(value);
119         status = kvStorePtr_->Put(kvKey, kvValue);
120     }
121     if (status != DistributedKv::Status::SUCCESS) {
122         HILOGE("Put kv to db failed, ret: %{public}d", status);
123         return DP_PUT_KV_DB_FAIL;
124     }
125     return DP_SUCCESS;
126 }
127 
Get(const std::string & key,std::string & value)128 int32_t BusinessEventAdapter::Get(const std::string& key, std::string& value)
129 {
130     HILOGI("Get data by key: %{public}s", ProfileUtils::GetDbKeyAnonyString(key).c_str());
131     DistributedKv::Key kvKey(key);
132     DistributedKv::Value kvValue;
133     DistributedKv::Status status;
134     {
135         std::lock_guard<std::mutex> lock(BusinessAdapterMutex_);
136         if (kvStorePtr_ == nullptr) {
137             HILOGE("kvStoragePtr_ is null");
138             return DP_KV_DB_PTR_NULL;
139         }
140         status = kvStorePtr_->Get(kvKey, kvValue);
141     }
142     if (status != DistributedKv::Status::SUCCESS) {
143         HILOGE("Get data from kv failed, key: %{public}s", ProfileUtils::GetDbKeyAnonyString(key).c_str());
144         return DP_GET_KV_DB_FAIL;
145     }
146     value = kvValue.ToString();
147     return DP_SUCCESS;
148 }
149 
OnRemoteDied()150 void BusinessEventAdapter::OnRemoteDied()
151 {
152     HILOGI("OnRemoteDied, recover db begin");
153     auto reInitTask = [this]() {
154         HILOGI("ReInit, storeId:%{public}s", storeId_.storeId.c_str());
155         ReInit();
156     };
157     ffrt::submit(reInitTask);
158 }
159 
GetKvStorePtr()160 DistributedKv::Status BusinessEventAdapter::GetKvStorePtr()
161 {
162     HILOGI("called");
163     DistributedKv::Options options = {
164         .createIfMissing = true,
165         .encrypt = false,
166         .autoSync = false,
167         .securityLevel = DistributedKv::SecurityLevel::S1,
168         .area = 1,
169         .kvStoreType = DistributedKv::KvStoreType::SINGLE_VERSION,
170         .baseDir = DATABASE_DIR
171     };
172     DistributedKv::Status status;
173     {
174         std::lock_guard<std::mutex> lock(BusinessAdapterMutex_);
175         status = kvDataMgr_.GetSingleKvStore(options, appId_, storeId_, kvStorePtr_);
176         if (status == DistributedKv::Status::SUCCESS && kvStorePtr_ == nullptr) {
177             status = DistributedKv::Status::ERROR;
178         }
179     }
180     return status;
181 }
182 
RegisterKvStoreDeathListener()183 int32_t BusinessEventAdapter::RegisterKvStoreDeathListener()
184 {
185     HILOGI("Register death listener");
186     {
187         std::lock_guard<std::mutex> lock(BusinessAdapterMutex_);
188         kvDataMgr_.RegisterKvStoreServiceDeathRecipient(deathRecipient_);
189     }
190     return DP_SUCCESS;
191 }
192 
UnregisterKvStoreDeathListener()193 int32_t BusinessEventAdapter::UnregisterKvStoreDeathListener()
194 {
195     HILOGI("UnRegister death listener");
196     {
197         std::lock_guard<std::mutex> lock(BusinessAdapterMutex_);
198         kvDataMgr_.UnRegisterKvStoreServiceDeathRecipient(deathRecipient_);
199     }
200     return DP_SUCCESS;
201 }
202 
Delete(const std::string & key)203 int32_t BusinessEventAdapter::Delete(const std::string& key)
204 {
205     HILOGI("key: %{public}s", ProfileUtils::GetDbKeyAnonyString(key).c_str());
206     DistributedKv::Status status;
207     {
208         std::lock_guard<std::mutex> lock(BusinessAdapterMutex_);
209         if (kvStorePtr_ == nullptr) {
210             HILOGE("kvDBPtr is null!");
211             return DP_KV_DB_PTR_NULL;
212         }
213         DistributedKv::Key kvKey(key);
214         status = kvStorePtr_->Delete(kvKey);
215     }
216     if (status != DistributedKv::Status::SUCCESS) {
217         HILOGE("Delete kv by key failed!");
218         return DP_DEL_KV_DB_FAIL;
219     }
220     HILOGD("Delete kv by key success!");
221     return DP_SUCCESS;
222 }
223 
DeleteBatch(const std::vector<std::string> & keys)224 int32_t BusinessEventAdapter::DeleteBatch(const std::vector<std::string>& keys)
225 {
226     (void)keys;
227     return DP_SUCCESS;
228 }
229 
PutBatch(const std::map<std::string,std::string> & values)230 int32_t BusinessEventAdapter::PutBatch(const std::map<std::string, std::string>& values)
231 {
232     (void)values;
233     return DP_SUCCESS;
234 }
235 
GetByPrefix(const std::string & keyPrefix,std::map<std::string,std::string> & values)236 int32_t BusinessEventAdapter::GetByPrefix(const std::string& keyPrefix, std::map<std::string, std::string>& values)
237 {
238     (void)keyPrefix;
239     (void)values;
240     return DP_SUCCESS;
241 }
242 
DeleteByPrefix(const std::string & keyPrefix)243 int32_t BusinessEventAdapter::DeleteByPrefix(const std::string& keyPrefix)
244 {
245     (void)keyPrefix;
246     return DP_SUCCESS;
247 }
248 
Sync(const std::vector<std::string> & deviceList,SyncMode syncMode)249 int32_t BusinessEventAdapter::Sync(const std::vector<std::string>& deviceList, SyncMode syncMode)
250 {
251     (void)deviceList;
252     (void)syncMode;
253     return DP_SUCCESS;
254 }
255 
GetDeviceEntries(const std::string & udid,std::map<std::string,std::string> & values)256 int32_t BusinessEventAdapter::GetDeviceEntries(const std::string& udid, std::map<std::string, std::string>& values)
257 {
258     (void)udid;
259     (void)values;
260     return DP_SUCCESS;
261 }
262 
RemoveDeviceData(const std::string & uuid)263 int32_t BusinessEventAdapter::RemoveDeviceData(const std::string& uuid)
264 {
265     (void)uuid;
266     return DP_SUCCESS;
267 }
268 } // namespace DistributedHardware
269 } // namespace OHOS
270