1 /*
2 * Copyright (c) 2021 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
16 #include "capability_info_manager.h"
17
18 #include "anonymous_string.h"
19 #include "capability_info_event.h"
20 #include "capability_utils.h"
21 #include "constants.h"
22 #include "dh_context.h"
23 #include "dh_utils_tool.h"
24 #include "distributed_hardware_errno.h"
25 #include "distributed_hardware_log.h"
26 #include "distributed_hardware_manager.h"
27 #include "event_bus.h"
28 #include "task_executor.h"
29 #include "task_factory.h"
30
31 namespace OHOS {
32 namespace DistributedHardware {
33 #undef DH_LOG_TAG
34 #define DH_LOG_TAG "CapabilityInfoManager"
35
CapabilityInfoManager()36 CapabilityInfoManager::CapabilityInfoManager() : dbAdapterPtr_(nullptr)
37 {}
38
~CapabilityInfoManager()39 CapabilityInfoManager::~CapabilityInfoManager()
40 {
41 DHLOGI("CapabilityInfoManager Destruction!");
42 }
43
GetInstance()44 std::shared_ptr<CapabilityInfoManager> CapabilityInfoManager::GetInstance()
45 {
46 static std::shared_ptr<CapabilityInfoManager> instance(new CapabilityInfoManager);
47 return instance;
48 }
49
Init()50 int32_t CapabilityInfoManager::Init()
51 {
52 DHLOGI("CapabilityInfoManager instance init!");
53 std::lock_guard<std::mutex> lock(capInfoMgrMutex_);
54 dbAdapterPtr_ = std::make_shared<DBAdapter>(APP_ID, GLOBAL_CAPABILITY_ID, shared_from_this());
55 if (dbAdapterPtr_->Init() != DH_FWK_SUCCESS) {
56 DHLOGE("Init dbAdapterPtr_ failed");
57 return ERR_DH_FWK_RESOURCE_INIT_DB_FAILED;
58 }
59 CapabilityInfoEvent capabilityInfoEvent(*this);
60 DHContext::GetInstance().GetEventBus()->AddHandler<CapabilityInfoEvent>(capabilityInfoEvent.GetType(), *this);
61 DHLOGI("CapabilityInfoManager instance init success");
62 return DH_FWK_SUCCESS;
63 }
64
UnInit()65 int32_t CapabilityInfoManager::UnInit()
66 {
67 DHLOGI("CapabilityInfoManager UnInit");
68 std::lock_guard<std::mutex> lock(capInfoMgrMutex_);
69 if (dbAdapterPtr_ == nullptr) {
70 DHLOGE("dbAdapterPtr_ is null");
71 return ERR_DH_FWK_RESOURCE_UNINIT_DB_FAILED;
72 }
73 dbAdapterPtr_->UnInit();
74 dbAdapterPtr_.reset();
75 return DH_FWK_SUCCESS;
76 }
77
SyncDeviceInfoFromDB(const std::string & deviceId)78 int32_t CapabilityInfoManager::SyncDeviceInfoFromDB(const std::string &deviceId)
79 {
80 DHLOGI("Sync DeviceInfo from DB, deviceId: %s", GetAnonyString(deviceId).c_str());
81 std::lock_guard<std::mutex> lock(capInfoMgrMutex_);
82 if (dbAdapterPtr_ == nullptr) {
83 DHLOGE("dbAdapterPtr_ is null");
84 return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL;
85 }
86 std::vector<std::string> dataVector;
87 if (dbAdapterPtr_->GetDataByKeyPrefix(deviceId, dataVector) != DH_FWK_SUCCESS) {
88 DHLOGE("Query data from DB by deviceId failed, id: %s", GetAnonyString(deviceId).c_str());
89 return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL;
90 }
91 for (const auto &data : dataVector) {
92 std::shared_ptr<CapabilityInfo> capabilityInfo;
93 if (CapabilityUtils::GetCapabilityByValue(data, capabilityInfo) != DH_FWK_SUCCESS) {
94 DHLOGE("Get capability ptr by value failed");
95 continue;
96 }
97 globalCapInfoMap_[capabilityInfo->GetKey()] = capabilityInfo;
98 }
99 return DH_FWK_SUCCESS;
100 }
101
SyncRemoteCapabilityInfos()102 int32_t CapabilityInfoManager::SyncRemoteCapabilityInfos()
103 {
104 DHLOGI("Sync full remote device info from DB");
105 std::lock_guard<std::mutex> lock(capInfoMgrMutex_);
106 if (dbAdapterPtr_ == nullptr) {
107 DHLOGE("dbAdapterPtr_ is null");
108 return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL;
109 }
110 std::vector<std::string> dataVector;
111 if (dbAdapterPtr_->GetDataByKeyPrefix("", dataVector) != DH_FWK_SUCCESS) {
112 DHLOGE("Query all data from DB failed");
113 return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL;
114 }
115 for (const auto &data : dataVector) {
116 std::shared_ptr<CapabilityInfo> capabilityInfo;
117 if (CapabilityUtils::GetCapabilityByValue(data, capabilityInfo) != DH_FWK_SUCCESS) {
118 DHLOGE("Get capability ptr by value failed");
119 continue;
120 }
121 const std::string &deviceId = capabilityInfo->GetDeviceId();
122 const std::string &localDeviceId = DHContext::GetInstance().GetDeviceInfo().deviceId;
123 if (deviceId.compare(localDeviceId) == 0) {
124 DHLOGE("local device info not need sync from db");
125 continue;
126 }
127 if (!DHContext::GetInstance().IsDeviceOnline(deviceId)) {
128 DHLOGE("offline device, no need sync to memory, deviceId : %s ",
129 GetAnonyString(deviceId).c_str());
130 continue;
131 }
132 globalCapInfoMap_[capabilityInfo->GetKey()] = capabilityInfo;
133 }
134 return DH_FWK_SUCCESS;
135 }
136
AddCapability(const std::vector<std::shared_ptr<CapabilityInfo>> & resInfos)137 int32_t CapabilityInfoManager::AddCapability(const std::vector<std::shared_ptr<CapabilityInfo>> &resInfos)
138 {
139 std::lock_guard<std::mutex> lock(capInfoMgrMutex_);
140 if (dbAdapterPtr_ == nullptr) {
141 DHLOGE("dbAdapterPtr_ is null");
142 return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL;
143 }
144 std::vector<std::string> keys;
145 std::vector<std::string> values;
146 for (auto &resInfo : resInfos) {
147 if (!resInfo) {
148 continue;
149 }
150 const std::string key = resInfo->GetKey();
151 DHLOGI("AddCapability, Key: %s", resInfo->GetAnonymousKey().c_str());
152 keys.push_back(key);
153 values.push_back(resInfo->ToJsonString());
154 globalCapInfoMap_[key] = resInfo;
155 }
156 if (dbAdapterPtr_->PutDataBatch(keys, values) != DH_FWK_SUCCESS) {
157 DHLOGE("Fail to storage batch to kv");
158 return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL;
159 }
160 return DH_FWK_SUCCESS;
161 }
162
AddCapabilityInMem(const std::vector<std::shared_ptr<CapabilityInfo>> & resInfos)163 int32_t CapabilityInfoManager::AddCapabilityInMem(const std::vector<std::shared_ptr<CapabilityInfo>> &resInfos)
164 {
165 std::lock_guard<std::mutex> lock(capInfoMgrMutex_);
166 for (auto &resInfo : resInfos) {
167 if (!resInfo) {
168 continue;
169 }
170 const std::string key = resInfo->GetKey();
171 DHLOGI("AddCapabilityInMem, Key: %s", resInfo->GetAnonymousKey().c_str());
172 globalCapInfoMap_[key] = resInfo;
173 }
174 return DH_FWK_SUCCESS;
175 }
176
RemoveCapabilityInfoInDB(const std::string & deviceId)177 int32_t CapabilityInfoManager::RemoveCapabilityInfoInDB(const std::string &deviceId)
178 {
179 DHLOGI("Remove capability device info, deviceId: %s", GetAnonyString(deviceId).c_str());
180 std::lock_guard<std::mutex> lock(capInfoMgrMutex_);
181 if (dbAdapterPtr_ == nullptr) {
182 DHLOGE("dbAdapterPtr_ is null");
183 return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL;
184 }
185 if (deviceId.empty()) {
186 DHLOGE("RemoveCapabilityInfoInDB failed, deviceId is empty");
187 return ERR_DH_FWK_PARA_INVALID;
188 }
189 // 1. Clear the cache in the memory.
190 for (auto iter = globalCapInfoMap_.begin(); iter != globalCapInfoMap_.end();) {
191 if (!CapabilityUtils::IsCapKeyMatchDeviceId(iter->first, deviceId)) {
192 iter++;
193 continue;
194 }
195 DHLOGI("Clear globalCapInfoMap_ iter: %s", GetAnonyString(iter->first).c_str());
196 globalCapInfoMap_.erase(iter++);
197 }
198 // 2. Delete the corresponding record from the database(use UUID).
199 if (dbAdapterPtr_->RemoveDeviceData(deviceId) != DH_FWK_SUCCESS) {
200 DHLOGE("Remove capability Device Data failed, deviceId: %s", GetAnonyString(deviceId).c_str());
201 return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL;
202 }
203 return DH_FWK_SUCCESS;
204 }
205
RemoveCapabilityInfoByKey(const std::string & key)206 int32_t CapabilityInfoManager::RemoveCapabilityInfoByKey(const std::string &key)
207 {
208 DHLOGI("Remove capability device info, key: %s", GetAnonyString(key).c_str());
209 std::lock_guard<std::mutex> lock(capInfoMgrMutex_);
210 if (dbAdapterPtr_ == nullptr) {
211 DHLOGE("dbAdapterPtr_ is null");
212 return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL;
213 }
214 if (key.empty()) {
215 DHLOGE("key is empty");
216 return ERR_DH_FWK_RESOURCE_KEY_IS_EMPTY;
217 }
218 // 1. Clear the cache in the memory.
219 globalCapInfoMap_.erase(key);
220
221 // 2. Delete the corresponding record from the database.(use key)
222 if (dbAdapterPtr_->RemoveDataByKey(key) != DH_FWK_SUCCESS) {
223 DHLOGE("Remove capability Device Data failed, key: %s", GetAnonyString(key).c_str());
224 return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL;
225 }
226 return DH_FWK_SUCCESS;
227 }
228
RemoveCapabilityInfoInMem(const std::string & deviceId)229 int32_t CapabilityInfoManager::RemoveCapabilityInfoInMem(const std::string &deviceId)
230 {
231 DHLOGI("remove capability device info in memory, deviceId: %s", GetAnonyString(deviceId).c_str());
232 std::lock_guard<std::mutex> lock(capInfoMgrMutex_);
233 if (deviceId.empty()) {
234 DHLOGE("RemoveCapabilityInfoInMem failed, deviceId is empty");
235 return ERR_DH_FWK_PARA_INVALID;
236 }
237 for (auto iter = globalCapInfoMap_.begin(); iter != globalCapInfoMap_.end();) {
238 if (!CapabilityUtils::IsCapKeyMatchDeviceId(iter->first, deviceId)) {
239 iter++;
240 continue;
241 }
242 globalCapInfoMap_.erase(iter++);
243 }
244 return DH_FWK_SUCCESS;
245 }
246
QueryCapabilityByFilters(const std::map<CapabilityInfoFilter,std::string> & filters)247 std::map<std::string, std::shared_ptr<CapabilityInfo>> CapabilityInfoManager::QueryCapabilityByFilters(
248 const std::map<CapabilityInfoFilter, std::string> &filters)
249 {
250 std::lock_guard<std::mutex> lock(capInfoMgrMutex_);
251 std::map<std::string, std::shared_ptr<CapabilityInfo>> capMap;
252 bool isMatch = true;
253 for (auto &info : globalCapInfoMap_) {
254 isMatch = true;
255 for (auto &filter : filters) {
256 if (!IsCapabilityMatchFilter(info.second, filter.first, filter.second)) {
257 isMatch = false;
258 break;
259 }
260 }
261 if (isMatch) {
262 capMap.emplace(info.first, info.second);
263 }
264 }
265 return capMap;
266 }
267
CreateManualSyncCount(const std::string & deviceId)268 void CapabilityInfoManager::CreateManualSyncCount(const std::string &deviceId)
269 {
270 std::lock_guard<std::mutex> lock(capInfoMgrMutex_);
271 if (dbAdapterPtr_ == nullptr) {
272 DHLOGE("dbAdapterPtr_ is null");
273 return;
274 }
275 dbAdapterPtr_->CreateManualSyncCount(deviceId);
276 }
277
RemoveManualSyncCount(const std::string & deviceId)278 void CapabilityInfoManager::RemoveManualSyncCount(const std::string &deviceId)
279 {
280 std::lock_guard<std::mutex> lock(capInfoMgrMutex_);
281 if (dbAdapterPtr_ == nullptr) {
282 DHLOGE("dbAdapterPtr_ is null");
283 return;
284 }
285 dbAdapterPtr_->RemoveManualSyncCount(deviceId);
286 }
287
ManualSync(const std::string & networkId)288 int32_t CapabilityInfoManager::ManualSync(const std::string &networkId)
289 {
290 DHLOGI("ManualSync start, networkId: %s", GetAnonyString(networkId).c_str());
291 std::unique_lock<std::mutex> lock(capInfoMgrMutex_);
292 if (dbAdapterPtr_ == nullptr) {
293 DHLOGE("dbAdapterPtr_ is null");
294 return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL;
295 }
296 if (dbAdapterPtr_->ManualSync(networkId) != DH_FWK_SUCCESS) {
297 DHLOGE("ManualSync failed");
298 return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL;
299 }
300 return DH_FWK_SUCCESS;
301 }
302
OnChange(const DistributedKv::ChangeNotification & changeNotification)303 void CapabilityInfoManager::OnChange(const DistributedKv::ChangeNotification &changeNotification)
304 {
305 DHLOGI("CapabilityInfoManager: DB data OnChange");
306 if (!changeNotification.GetInsertEntries().empty()) {
307 DHLOGI("Handle capability data add change");
308 HandleCapabilityAddChange(changeNotification.GetInsertEntries());
309 }
310 if (!changeNotification.GetUpdateEntries().empty()) {
311 DHLOGI("Handle capability data update change");
312 HandleCapabilityUpdateChange(changeNotification.GetUpdateEntries());
313 }
314 if (!changeNotification.GetDeleteEntries().empty()) {
315 DHLOGI("Handle capability data delete change");
316 HandleCapabilityDeleteChange(changeNotification.GetDeleteEntries());
317 }
318 }
319
OnChange(const DistributedKv::ChangeNotification & changeNotification,std::shared_ptr<DistributedKv::KvStoreSnapshot> snapshot)320 void CapabilityInfoManager::OnChange(const DistributedKv::ChangeNotification &changeNotification,
321 std::shared_ptr<DistributedKv::KvStoreSnapshot> snapshot)
322 {}
323
OnEvent(CapabilityInfoEvent & ev)324 void CapabilityInfoManager::OnEvent(CapabilityInfoEvent &ev)
325 {
326 switch (ev.GetAction()) {
327 case CapabilityInfoEvent::EventType::RECOVER:
328 SyncRemoteCapabilityInfos();
329 break;
330 default:
331 DHLOGE("Event is undefined, type is %d", ev.GetAction());
332 break;
333 }
334 }
335
HandleCapabilityAddChange(const std::vector<DistributedKv::Entry> & insertRecords)336 void CapabilityInfoManager::HandleCapabilityAddChange(const std::vector<DistributedKv::Entry> &insertRecords)
337 {
338 std::lock_guard<std::mutex> lock(capInfoMgrMutex_);
339 for (const auto &item : insertRecords) {
340 const std::string value = item.value.ToString();
341 std::shared_ptr<CapabilityInfo> capPtr;
342 if (CapabilityUtils::GetCapabilityByValue(value, capPtr) != DH_FWK_SUCCESS) {
343 DHLOGE("Get capability by value failed");
344 continue;
345 }
346 const auto keyString = capPtr->GetKey();
347 DHLOGI("Add capability key: %s", capPtr->GetAnonymousKey().c_str());
348 globalCapInfoMap_[keyString] = capPtr;
349 std::string uuid = DHContext::GetInstance().GetUUIDByDeviceId(capPtr->GetDeviceId());
350 if (uuid.empty()) {
351 DHLOGI("Find uuid failed and never enable");
352 continue;
353 }
354 std::string networkId = DHContext::GetInstance().GetNetworkIdByUUID(uuid);
355 if (networkId.empty()) {
356 DHLOGI("Find network failed and never enable, deviceId: %s", GetAnonyString(uuid).c_str());
357 continue;
358 }
359 auto task = TaskFactory::GetInstance().CreateTask(TaskType::ENABLE, networkId, uuid,
360 capPtr->GetDHId(), nullptr);
361 TaskExecutor::GetInstance().PushTask(task);
362 }
363 }
364
HandleCapabilityUpdateChange(const std::vector<DistributedKv::Entry> & updateRecords)365 void CapabilityInfoManager::HandleCapabilityUpdateChange(const std::vector<DistributedKv::Entry> &updateRecords)
366 {
367 std::lock_guard<std::mutex> lock(capInfoMgrMutex_);
368 for (const auto &item : updateRecords) {
369 const std::string value = item.value.ToString();
370 std::shared_ptr<CapabilityInfo> capPtr;
371 if (CapabilityUtils::GetCapabilityByValue(value, capPtr) != DH_FWK_SUCCESS) {
372 DHLOGE("Get capability by value failed");
373 continue;
374 }
375 const auto keyString = capPtr->GetKey();
376 DHLOGI("Update capability key: %s", capPtr->GetAnonymousKey().c_str());
377 globalCapInfoMap_[keyString] = capPtr;
378 }
379 }
380
HandleCapabilityDeleteChange(const std::vector<DistributedKv::Entry> & deleteRecords)381 void CapabilityInfoManager::HandleCapabilityDeleteChange(const std::vector<DistributedKv::Entry> &deleteRecords)
382 {
383 std::lock_guard<std::mutex> lock(capInfoMgrMutex_);
384 for (const auto &item : deleteRecords) {
385 const std::string value = item.value.ToString();
386 std::shared_ptr<CapabilityInfo> capPtr;
387 if (CapabilityUtils::GetCapabilityByValue(value, capPtr) != DH_FWK_SUCCESS) {
388 DHLOGE("Get capability by value failed");
389 continue;
390 }
391 const auto keyString = capPtr->GetKey();
392 DHLOGI("Delete capability key: %s", capPtr->GetAnonymousKey().c_str());
393 globalCapInfoMap_.erase(keyString);
394 }
395 }
396
IsCapabilityMatchFilter(const std::shared_ptr<CapabilityInfo> & cap,const CapabilityInfoFilter & filter,const std::string & value)397 bool CapabilityInfoManager::IsCapabilityMatchFilter(const std::shared_ptr<CapabilityInfo> &cap,
398 const CapabilityInfoFilter &filter, const std::string &value)
399 {
400 bool isMatch = false;
401 switch (filter) {
402 case CapabilityInfoFilter::FILTER_DH_ID: {
403 isMatch = cap->GetDHId().compare(value) == 0;
404 break;
405 }
406 case CapabilityInfoFilter::FILTER_DEVICE_ID: {
407 isMatch = cap->GetDeviceId().compare(value) == 0;
408 break;
409 }
410 case CapabilityInfoFilter::FILTER_DEVICE_NAME: {
411 isMatch = cap->GetDeviceName().compare(value) == 0;
412 break;
413 }
414 case CapabilityInfoFilter::FILTER_DEVICE_TYPE: {
415 auto devType = static_cast<uint16_t>(std::stoi(value));
416 isMatch = cap->GetDeviceType() == devType;
417 break;
418 }
419 case CapabilityInfoFilter::FILTER_DH_TYPE: {
420 DHType dhType = (DHType)std::stoi(value);
421 isMatch = cap->GetDHType() == dhType;
422 break;
423 }
424 case CapabilityInfoFilter::FILTER_DH_ATTRS: {
425 isMatch = cap->GetDHAttrs().compare(value) == 0;
426 break;
427 }
428 default: {
429 isMatch = false;
430 break;
431 }
432 }
433 return isMatch;
434 }
435
GetCapabilitiesByDeviceId(const std::string & deviceId,std::vector<std::shared_ptr<CapabilityInfo>> & resInfos)436 void CapabilityInfoManager::GetCapabilitiesByDeviceId(const std::string &deviceId,
437 std::vector<std::shared_ptr<CapabilityInfo>> &resInfos)
438 {
439 std::lock_guard<std::mutex> lock(capInfoMgrMutex_);
440 for (auto &capabilityInfo : globalCapInfoMap_) {
441 if (CapabilityUtils::IsCapKeyMatchDeviceId(capabilityInfo.first, deviceId)) {
442 resInfos.emplace_back(capabilityInfo.second);
443 }
444 }
445 }
446
HasCapability(const std::string & deviceId,const std::string & dhId)447 bool CapabilityInfoManager::HasCapability(const std::string &deviceId, const std::string &dhId)
448 {
449 std::lock_guard<std::mutex> lock(capInfoMgrMutex_);
450 std::string kvKey = CapabilityUtils::GetCapabilityKey(deviceId, dhId);
451 if (globalCapInfoMap_.find(kvKey) == globalCapInfoMap_.end()) {
452 return false;
453 }
454 return true;
455 }
456
GetCapability(const std::string & deviceId,const std::string & dhId,std::shared_ptr<CapabilityInfo> & capPtr)457 int32_t CapabilityInfoManager::GetCapability(const std::string &deviceId, const std::string &dhId,
458 std::shared_ptr<CapabilityInfo> &capPtr)
459 {
460 std::lock_guard<std::mutex> lock(capInfoMgrMutex_);
461 std::string key = CapabilityUtils::GetCapabilityKey(deviceId, dhId);
462 if (globalCapInfoMap_.find(key) == globalCapInfoMap_.end()) {
463 DHLOGE("Can not find capability In globalCapInfoMap_: %s", GetAnonyString(deviceId).c_str());
464 return ERR_DH_FWK_RESOURCE_CAPABILITY_MAP_NOT_FOUND;
465 }
466 capPtr = globalCapInfoMap_[key];
467 return DH_FWK_SUCCESS;
468 }
469
GetDataByKey(const std::string & key,std::shared_ptr<CapabilityInfo> & capInfoPtr)470 int32_t CapabilityInfoManager::GetDataByKey(const std::string &key, std::shared_ptr<CapabilityInfo> &capInfoPtr)
471 {
472 std::lock_guard<std::mutex> lock(capInfoMgrMutex_);
473 if (dbAdapterPtr_ == nullptr) {
474 DHLOGI("dbAdapterPtr_ is null");
475 return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL;
476 }
477 std::string data;
478 if (dbAdapterPtr_->GetDataByKey(key, data) != DH_FWK_SUCCESS) {
479 DHLOGE("Query capability info from db failed, key: %s", GetAnonyString(key).c_str());
480 return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL;
481 }
482 return CapabilityUtils::GetCapabilityByValue(data, capInfoPtr);
483 }
484
GetDataByKeyPrefix(const std::string & keyPrefix,CapabilityInfoMap & capabilityMap)485 int32_t CapabilityInfoManager::GetDataByKeyPrefix(const std::string &keyPrefix, CapabilityInfoMap &capabilityMap)
486 {
487 std::lock_guard<std::mutex> lock(capInfoMgrMutex_);
488 if (dbAdapterPtr_ == nullptr) {
489 DHLOGI("dbAdapterPtr_ is null");
490 return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL;
491 }
492 std::vector<std::string> dataVector;
493 if (dbAdapterPtr_->GetDataByKeyPrefix(keyPrefix, dataVector) != DH_FWK_SUCCESS) {
494 DHLOGE("Query capability info from db failed, key: %s", GetAnonyString(keyPrefix).c_str());
495 return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL;
496 }
497 for (const auto &data : dataVector) {
498 std::shared_ptr<CapabilityInfo> capabilityInfo;
499 if (CapabilityUtils::GetCapabilityByValue(data, capabilityInfo) != DH_FWK_SUCCESS) {
500 DHLOGE("Get capability ptr by value failed");
501 continue;
502 }
503 if (capabilityInfo->FromJsonString(data) != DH_FWK_SUCCESS) {
504 DHLOGE("Wrong data: %s", GetAnonyString(data).c_str());
505 continue;
506 }
507 capabilityMap[capabilityInfo->GetKey()] = capabilityInfo;
508 }
509 return DH_FWK_SUCCESS;
510 }
511 }
512 }
513