1 /*
2 * Copyright (c) 2024-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
16 #include "meta_info_manager.h"
17
18 #include "anonymous_string.h"
19 #include "capability_utils.h"
20 #include "constants.h"
21 #include "dh_context.h"
22 #include "dh_utils_tool.h"
23 #include "distributed_hardware_errno.h"
24 #include "distributed_hardware_log.h"
25 #include "distributed_hardware_manager.h"
26 #include "task_executor.h"
27 #include "task_factory.h"
28 #include "task_board.h"
29
30 namespace OHOS {
31 namespace DistributedHardware {
32
33 #undef DH_LOG_TAG
34 #define DH_LOG_TAG "MetaInfoManager"
35
36 constexpr const char *GLOBAL_META_INFO_KEY = "global_meta_info";
37
MetaInfoManager()38 MetaInfoManager::MetaInfoManager() : dbAdapterPtr_(nullptr)
39 {
40 DHLOGI("MetaInfoManager construction!");
41 }
42
~MetaInfoManager()43 MetaInfoManager::~MetaInfoManager()
44 {
45 DHLOGI("MetaInfoManager destruction!");
46 }
47
GetInstance()48 std::shared_ptr<MetaInfoManager> MetaInfoManager::GetInstance()
49 {
50 static std::shared_ptr<MetaInfoManager> instance = std::make_shared<MetaInfoManager>();
51 return instance;
52 }
53
MetaInfoManagerEventHandler(const std::shared_ptr<AppExecFwk::EventRunner> runner,std::shared_ptr<MetaInfoManager> metaInfoMgrPtr)54 MetaInfoManager::MetaInfoManagerEventHandler::MetaInfoManagerEventHandler(
55 const std::shared_ptr<AppExecFwk::EventRunner> runner, std::shared_ptr<MetaInfoManager> metaInfoMgrPtr)
56 : AppExecFwk::EventHandler(runner), metaInfoMgrWPtr_(metaInfoMgrPtr)
57 {
58 DHLOGI("Ctor MetaInfoManagerEventHandler");
59 }
60
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)61 void MetaInfoManager::MetaInfoManagerEventHandler::ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event)
62 {
63 if (event == nullptr) {
64 DHLOGE("event is null.");
65 return;
66 }
67 uint32_t eventId = event->GetInnerEventId();
68 auto selfPtr = metaInfoMgrWPtr_.lock();
69 if (!selfPtr) {
70 DHLOGE("Can not get strong self ptr");
71 return;
72 }
73 switch (eventId) {
74 case EVENT_META_INFO_DB_RECOVER:
75 selfPtr->SyncRemoteMetaInfos();
76 break;
77 default:
78 DHLOGE("event is undefined, id is %{public}d", eventId);
79 break;
80 }
81 }
82
GetEventHandler()83 std::shared_ptr<MetaInfoManager::MetaInfoManagerEventHandler> MetaInfoManager::GetEventHandler()
84 {
85 return this->eventHandler_;
86 }
87
Init()88 int32_t MetaInfoManager::Init()
89 {
90 DHLOGI("MetaInfoManager instance init!");
91 std::lock_guard<std::mutex> lock(metaInfoMgrMutex_);
92 dbAdapterPtr_ = std::make_shared<DBAdapter>(APP_ID, GLOBAL_META_INFO_KEY, shared_from_this());
93 if (dbAdapterPtr_ == nullptr) {
94 DHLOGE("dbAdapterPtr_ is null");
95 return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL;
96 }
97 if (dbAdapterPtr_->Init(false, DistributedKv::DataType::TYPE_STATICS) != DH_FWK_SUCCESS) {
98 DHLOGE("Init dbAdapterPtr_ failed");
99 return ERR_DH_FWK_RESOURCE_INIT_DB_FAILED;
100 }
101 std::shared_ptr<AppExecFwk::EventRunner> runner = AppExecFwk::EventRunner::Create(true);
102 eventHandler_ = std::make_shared<MetaInfoManager::MetaInfoManagerEventHandler>(runner, shared_from_this());
103 DHLOGI("MetaInfoManager instance init success");
104 return DH_FWK_SUCCESS;
105 }
106
UnInit()107 int32_t MetaInfoManager::UnInit()
108 {
109 DHLOGI("MetaInfoManager UnInit");
110 std::lock_guard<std::mutex> lock(metaInfoMgrMutex_);
111 if (dbAdapterPtr_ == nullptr) {
112 DHLOGE("dbAdapterPtr_ is null");
113 return ERR_DH_FWK_RESOURCE_UNINIT_DB_FAILED;
114 }
115 dbAdapterPtr_->UnInit();
116 dbAdapterPtr_.reset();
117 return DH_FWK_SUCCESS;
118 }
119
AddMetaCapInfos(const std::vector<std::shared_ptr<MetaCapabilityInfo>> & metaCapInfos)120 int32_t MetaInfoManager::AddMetaCapInfos(const std::vector<std::shared_ptr<MetaCapabilityInfo>> &metaCapInfos)
121 {
122 if (metaCapInfos.empty() || metaCapInfos.size() > MAX_DB_RECORD_SIZE) {
123 DHLOGE("MetaCapInfos is empty or too large!");
124 return ERR_DH_FWK_RESOURCE_RES_DB_DATA_INVALID;
125 }
126 std::lock_guard<std::mutex> lock(metaInfoMgrMutex_);
127 if (dbAdapterPtr_ == nullptr) {
128 DHLOGE("dbAdapterPtr_ is null");
129 return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL;
130 }
131 std::vector<std::string> keys;
132 std::vector<std::string> values;
133 std::string key;
134 std::string data;
135 for (auto &metaCapInfo : metaCapInfos) {
136 if (metaCapInfo == nullptr) {
137 continue;
138 }
139 key = metaCapInfo->GetKey();
140 globalMetaInfoMap_[key] = metaCapInfo;
141 if (dbAdapterPtr_->GetDataByKey(key, data) == DH_FWK_SUCCESS && data == metaCapInfo->ToJsonString()) {
142 DHLOGI("this record is exist, Key: %{public}s", metaCapInfo->GetAnonymousKey().c_str());
143 continue;
144 }
145 DHLOGI("AddMetaCapability, Key: %{public}s", metaCapInfo->GetAnonymousKey().c_str());
146 keys.push_back(key);
147 values.push_back(metaCapInfo->ToJsonString());
148 }
149 if (keys.empty() || values.empty()) {
150 DHLOGD("Records are empty, No need add data to db!");
151 return DH_FWK_SUCCESS;
152 }
153 if (dbAdapterPtr_->PutDataBatch(keys, values) != DH_FWK_SUCCESS) {
154 DHLOGE("Fail to storage batch to kv");
155 return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL;
156 }
157 return DH_FWK_SUCCESS;
158 }
159
SyncMetaInfoFromDB(const std::string & udidHash)160 int32_t MetaInfoManager::SyncMetaInfoFromDB(const std::string &udidHash)
161 {
162 if (!IsHashSizeValid(udidHash)) {
163 return ERR_DH_FWK_PARA_INVALID;
164 }
165 DHLOGI("Sync MetaInfo from DB, udidHash: %{public}s", GetAnonyString(udidHash).c_str());
166 std::lock_guard<std::mutex> lock(metaInfoMgrMutex_);
167 if (dbAdapterPtr_ == nullptr) {
168 DHLOGE("dbAdapterPtr_ is null");
169 return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL;
170 }
171 std::vector<std::string> dataVector;
172 if (dbAdapterPtr_->GetDataByKeyPrefix(udidHash, dataVector) != DH_FWK_SUCCESS) {
173 DHLOGE("Query Metadata from DB by udidHash failed, udidHash: %{public}s", GetAnonyString(udidHash).c_str());
174 return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL;
175 }
176 if (dataVector.empty() || dataVector.size() > MAX_DB_RECORD_SIZE) {
177 DHLOGE("dataVector size: %{public}zu is invalid, maybe empty or too large.", dataVector.size());
178 return ERR_DH_FWK_RESOURCE_RES_DB_DATA_INVALID;
179 }
180 for (const auto &data : dataVector) {
181 std::shared_ptr<MetaCapabilityInfo> metaCapInfo;
182 if (GetMetaCapByValue(data, metaCapInfo) != DH_FWK_SUCCESS) {
183 DHLOGE("Get capability ptr by value failed");
184 continue;
185 }
186 globalMetaInfoMap_[metaCapInfo->GetKey()] = metaCapInfo;
187 }
188 return DH_FWK_SUCCESS;
189 }
190
SyncRemoteMetaInfos()191 int32_t MetaInfoManager::SyncRemoteMetaInfos()
192 {
193 DHLOGI("Sync full remote device Metainfo from DB");
194 std::lock_guard<std::mutex> lock(metaInfoMgrMutex_);
195 if (dbAdapterPtr_ == nullptr) {
196 DHLOGE("dbAdapterPtr_ is null");
197 return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL;
198 }
199 std::vector<std::string> udidHashVec;
200 DHContext::GetInstance().GetOnlineDeviceUdidHash(udidHashVec);
201 for (const auto &udidHash : udidHashVec) {
202 std::vector<std::string> dataVector;
203 if (dbAdapterPtr_->GetDataByKeyPrefix(udidHash, dataVector) != DH_FWK_SUCCESS) {
204 DHLOGE("Query the udidHash: %{public}s data from DB failed", GetAnonyString(udidHash).c_str());
205 continue;
206 }
207 if (dataVector.empty() || dataVector.size() > MAX_DB_RECORD_SIZE) {
208 DHLOGE("dataVector size: %{public}zu is invalid, maybe empty or too large.", dataVector.size());
209 continue;
210 }
211 for (const auto &data : dataVector) {
212 std::shared_ptr<MetaCapabilityInfo> metaCapInfo;
213 if (GetMetaCapByValue(data, metaCapInfo) != DH_FWK_SUCCESS) {
214 DHLOGE("Get Metainfo ptr by value failed");
215 continue;
216 }
217 const std::string udidHash = metaCapInfo->GetUdidHash();
218 const std::string localUdidHash = DHContext::GetInstance().GetDeviceInfo().udidHash;
219 if (udidHash.compare(localUdidHash) == 0) {
220 DHLOGE("device MetaInfo not need sync from db");
221 continue;
222 }
223 globalMetaInfoMap_[metaCapInfo->GetKey()] = metaCapInfo;
224 }
225 }
226 return DH_FWK_SUCCESS;
227 }
228
GetDataByKeyPrefix(const std::string & keyPrefix,MetaCapInfoMap & metaCapMap)229 int32_t MetaInfoManager::GetDataByKeyPrefix(const std::string &keyPrefix, MetaCapInfoMap &metaCapMap)
230 {
231 if (!IsKeySizeValid(keyPrefix)) {
232 return ERR_DH_FWK_PARA_INVALID;
233 }
234 std::lock_guard<std::mutex> lock(metaInfoMgrMutex_);
235 if (dbAdapterPtr_ == nullptr) {
236 DHLOGE("dbAdapterPtr is null");
237 return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL;
238 }
239 std::vector<std::string> dataVector;
240 if (dbAdapterPtr_->GetDataByKeyPrefix(keyPrefix, dataVector) != DH_FWK_SUCCESS) {
241 DHLOGE("Query metaInfo from db failed, keyPrefix: %{public}s", GetAnonyString(keyPrefix).c_str());
242 return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL;
243 }
244 if (dataVector.empty() || dataVector.size() > MAX_DB_RECORD_SIZE) {
245 DHLOGE("On dataVector error, maybe empty or too large.");
246 return ERR_DH_FWK_RESOURCE_RES_DB_DATA_INVALID;
247 }
248 for (const auto &data : dataVector) {
249 std::shared_ptr<MetaCapabilityInfo> metaCapInfo;
250 if (GetMetaCapByValue(data, metaCapInfo) != DH_FWK_SUCCESS) {
251 DHLOGE("Get Metainfo ptr by value failed");
252 continue;
253 }
254 metaCapMap[metaCapInfo->GetKey()] = metaCapInfo;
255 }
256 return DH_FWK_SUCCESS;
257 }
258
GetMetaCapInfo(const std::string & udidHash,const std::string & dhId,std::shared_ptr<MetaCapabilityInfo> & metaCapPtr)259 int32_t MetaInfoManager::GetMetaCapInfo(const std::string &udidHash,
260 const std::string &dhId, std::shared_ptr<MetaCapabilityInfo> &metaCapPtr)
261 {
262 if (!IsHashSizeValid(udidHash) || !IsIdLengthValid(dhId)) {
263 return ERR_DH_FWK_PARA_INVALID;
264 }
265 std::lock_guard<std::mutex> lock(metaInfoMgrMutex_);
266 std::string key = GetCapabilityKey(udidHash, dhId);
267 if (globalMetaInfoMap_.find(key) == globalMetaInfoMap_.end()) {
268 DHLOGE("Can not find capability In globalMetaInfoMap_: %{public}s", GetAnonyString(udidHash).c_str());
269 return ERR_DH_FWK_RESOURCE_CAPABILITY_MAP_NOT_FOUND;
270 }
271 metaCapPtr = globalMetaInfoMap_[key];
272 return DH_FWK_SUCCESS;
273 }
274
GetMetaCapInfosByUdidHash(const std::string & udidHash,std::vector<std::shared_ptr<MetaCapabilityInfo>> & metaCapInfos)275 void MetaInfoManager::GetMetaCapInfosByUdidHash(const std::string &udidHash,
276 std::vector<std::shared_ptr<MetaCapabilityInfo>> &metaCapInfos)
277 {
278 if (!IsHashSizeValid(udidHash)) {
279 return;
280 }
281 std::lock_guard<std::mutex> lock(metaInfoMgrMutex_);
282 for (auto &metaCapInfo : globalMetaInfoMap_) {
283 if (IsCapKeyMatchDeviceId(metaCapInfo.first, udidHash)) {
284 metaCapInfos.emplace_back(metaCapInfo.second);
285 }
286 }
287 }
288
GetMetaCapByValue(const std::string & value,std::shared_ptr<MetaCapabilityInfo> & metaCapPtr)289 int32_t MetaInfoManager::GetMetaCapByValue(const std::string &value, std::shared_ptr<MetaCapabilityInfo> &metaCapPtr)
290 {
291 if (!IsMessageLengthValid(value)) {
292 return ERR_DH_FWK_PARA_INVALID;
293 }
294 if (metaCapPtr == nullptr) {
295 metaCapPtr = std::make_shared<MetaCapabilityInfo>();
296 }
297 return metaCapPtr->FromJsonString(value);
298 }
299
GetMetaDataByDHType(const DHType dhType,MetaCapInfoMap & metaInfoMap)300 int32_t MetaInfoManager::GetMetaDataByDHType(const DHType dhType, MetaCapInfoMap &metaInfoMap)
301 {
302 std::lock_guard<std::mutex> lock(metaInfoMgrMutex_);
303 for (const auto &metaCapInfo : globalMetaInfoMap_) {
304 if (metaCapInfo.second->GetDHType() != dhType) {
305 continue;
306 }
307 metaInfoMap[metaCapInfo.first] = metaCapInfo.second;
308 }
309 return DH_FWK_SUCCESS;
310 }
311
SyncDataByNetworkId(const std::string & networkId)312 int32_t MetaInfoManager::SyncDataByNetworkId(const std::string &networkId)
313 {
314 if (!IsIdLengthValid(networkId)) {
315 DHLOGE("networId: %{public}s is invalid", GetAnonyString(networkId).c_str());
316 return ERR_DH_FWK_PARA_INVALID;
317 }
318 if (dbAdapterPtr_ == nullptr) {
319 DHLOGE("dbAdapterPtr is null");
320 return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL;
321 }
322 dbAdapterPtr_->SyncDataByNetworkId(networkId);
323 return DH_FWK_SUCCESS;
324 }
325
RemoveMetaInfoInMemByUdid(const std::string & peerudid)326 int32_t MetaInfoManager::RemoveMetaInfoInMemByUdid(const std::string &peerudid)
327 {
328 DHLOGI("remove device metainfo in memory, peerudid: %{public}s", GetAnonyString(peerudid).c_str());
329 std::string udIdHash = Sha256(peerudid);
330 for (auto iter = globalMetaInfoMap_.begin(); iter != globalMetaInfoMap_.end();) {
331 if (!IsCapKeyMatchDeviceId(iter->first, udIdHash)) {
332 DHLOGI("not find udIdHash: %{public}s", GetAnonyString(udIdHash).c_str());
333 iter++;
334 continue;
335 }
336 globalMetaInfoMap_.erase(iter++);
337 }
338 return DH_FWK_SUCCESS;
339 }
340
ClearRemoteDeviceMetaInfoData(const std::string & peerudid,const std::string & peeruuid)341 int32_t MetaInfoManager::ClearRemoteDeviceMetaInfoData(const std::string &peerudid, const std::string &peeruuid)
342 {
343 std::lock_guard<std::mutex> lock(metaInfoMgrMutex_);
344 if (dbAdapterPtr_ == nullptr) {
345 DHLOGE("dbAdapterPtr is null");
346 return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL;
347 }
348 dbAdapterPtr_->ClearDataByPrefix(peerudid);
349 if (dbAdapterPtr_->RemoveDeviceData(peeruuid) != DH_FWK_SUCCESS) {
350 DHLOGE("Remove Device Data failed, peeruuid: %{public}s", GetAnonyString(peeruuid).c_str());
351 return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL;
352 }
353 RemoveMetaInfoInMemByUdid(peerudid);
354 return DH_FWK_SUCCESS;
355 }
356
OnChange(const DistributedKv::ChangeNotification & changeNotification)357 void MetaInfoManager::OnChange(const DistributedKv::ChangeNotification &changeNotification)
358 {
359 DHLOGI("MetaInfoManager: DB data OnChange");
360 if (!changeNotification.GetInsertEntries().empty() &&
361 changeNotification.GetInsertEntries().size() <= MAX_DB_RECORD_SIZE) {
362 DHLOGI("MetaInfoManager Handle capability data add change");
363 HandleMetaCapabilityAddChange(changeNotification.GetInsertEntries());
364 }
365 if (!changeNotification.GetUpdateEntries().empty() &&
366 changeNotification.GetUpdateEntries().size() <= MAX_DB_RECORD_SIZE) {
367 DHLOGI("MetaInfoManager Handle capability data update change");
368 HandleMetaCapabilityUpdateChange(changeNotification.GetUpdateEntries());
369 }
370 if (!changeNotification.GetDeleteEntries().empty() &&
371 changeNotification.GetDeleteEntries().size() <= MAX_DB_RECORD_SIZE) {
372 DHLOGI("MetaInfoManager Handle capability data delete change");
373 HandleMetaCapabilityDeleteChange(changeNotification.GetDeleteEntries());
374 }
375 }
376
OnChange(const DistributedKv::DataOrigin & origin,Keys && keys)377 void MetaInfoManager::OnChange(const DistributedKv::DataOrigin &origin, Keys &&keys)
378 {
379 DHLOGI("MetaInfoManager: Cloud data OnChange.");
380 std::vector<DistributedKv::Entry> insertRecords = GetEntriesByKeys(keys[ChangeOp::OP_INSERT]);
381 if (!insertRecords.empty() && insertRecords.size() <= MAX_DB_RECORD_SIZE) {
382 DHLOGI("MetaInfoManager Handle capability data add change");
383 HandleMetaCapabilityAddChange(insertRecords);
384 }
385 std::vector<DistributedKv::Entry> updateRecords = GetEntriesByKeys(keys[ChangeOp::OP_UPDATE]);
386 if (!updateRecords.empty() && updateRecords.size() <= MAX_DB_RECORD_SIZE) {
387 DHLOGI("MetaInfoManager Handle capability data update change");
388 HandleMetaCapabilityUpdateChange(updateRecords);
389 }
390 std::vector<std::string> delKeys = keys[ChangeOp::OP_DELETE];
391 if (!delKeys.empty() && delKeys.size() <= MAX_DB_RECORD_SIZE) {
392 std::vector<DistributedKv::Entry> deleteRecords;
393 for (const auto &key : delKeys) {
394 DistributedKv::Entry entry;
395 DistributedKv::Key kvKey(key);
396 entry.key = kvKey;
397 deleteRecords.emplace_back(entry);
398 }
399 DHLOGI("MetaInfoManager Handle capability data delete change");
400 HandleMetaCapabilityDeleteChange(deleteRecords);
401 }
402 }
403
HandleMetaCapabilityAddChange(const std::vector<DistributedKv::Entry> & insertRecords)404 void MetaInfoManager::HandleMetaCapabilityAddChange(const std::vector<DistributedKv::Entry> &insertRecords)
405 {
406 std::lock_guard<std::mutex> lock(metaInfoMgrMutex_);
407 for (const auto &item : insertRecords) {
408 const std::string value = item.value.ToString();
409 std::shared_ptr<MetaCapabilityInfo> capPtr;
410 if (GetCapabilityByValue<MetaCapabilityInfo>(value, capPtr) != DH_FWK_SUCCESS) {
411 DHLOGE("Get Meta capability by value failed");
412 continue;
413 }
414 std::string uuid = DHContext::GetInstance().GetUUIDByDeviceId(capPtr->GetDeviceId());
415 if (uuid.empty()) {
416 DHLOGE("Find uuid failed and never enable, deviceId: %{public}s",
417 GetAnonyString(capPtr->GetDeviceId()).c_str());
418 continue;
419 }
420 std::string networkId = DHContext::GetInstance().GetNetworkIdByUUID(uuid);
421 if (networkId.empty()) {
422 DHLOGE("Find network failed and never enable, uuid: %{public}s", GetAnonyString(uuid).c_str());
423 continue;
424 }
425
426 const auto keyString = capPtr->GetKey();
427 DHLOGI("Add MetaCapability key: %{public}s", capPtr->GetAnonymousKey().c_str());
428 globalMetaInfoMap_[keyString] = capPtr;
429 TaskParam taskParam = {
430 .networkId = networkId,
431 .uuid = uuid,
432 .dhId = capPtr->GetDHId(),
433 .dhType = capPtr->GetDHType()
434 };
435 auto task = TaskFactory::GetInstance().CreateTask(TaskType::ENABLE, taskParam, nullptr);
436 TaskExecutor::GetInstance().PushTask(task);
437 }
438 }
439
HandleMetaCapabilityUpdateChange(const std::vector<DistributedKv::Entry> & updateRecords)440 void MetaInfoManager::HandleMetaCapabilityUpdateChange(const std::vector<DistributedKv::Entry> &updateRecords)
441 {
442 std::lock_guard<std::mutex> lock(metaInfoMgrMutex_);
443 for (const auto &item : updateRecords) {
444 const std::string value = item.value.ToString();
445 std::shared_ptr<MetaCapabilityInfo> capPtr;
446 if (GetCapabilityByValue<MetaCapabilityInfo>(value, capPtr) != DH_FWK_SUCCESS) {
447 DHLOGE("Get Meta capability by value failed");
448 continue;
449 }
450 std::string uuid = DHContext::GetInstance().GetUUIDByDeviceId(capPtr->GetDeviceId());
451 if (uuid.empty()) {
452 DHLOGE("Find uuid failed and never enable, deviceId: %{public}s",
453 GetAnonyString(capPtr->GetDeviceId()).c_str());
454 continue;
455 }
456 std::string networkId = DHContext::GetInstance().GetNetworkIdByUUID(uuid);
457 if (networkId.empty()) {
458 DHLOGE("Find network failed and never enable, uuid: %{public}s", GetAnonyString(uuid).c_str());
459 continue;
460 }
461 std::string enabledDeviceKey = GetCapabilityKey(capPtr->GetDeviceId(), capPtr->GetDHId());
462 if (TaskBoard::GetInstance().IsEnabledDevice(enabledDeviceKey)) {
463 DHLOGI("The deviceKey: %{public}s is enabled.", GetAnonyString(enabledDeviceKey).c_str());
464 continue;
465 }
466 const auto keyString = capPtr->GetKey();
467 DHLOGI("Update MetaCapability key: %{public}s", capPtr->GetAnonymousKey().c_str());
468 globalMetaInfoMap_[keyString] = capPtr;
469 TaskParam taskParam = {
470 .networkId = networkId,
471 .uuid = uuid,
472 .dhId = capPtr->GetDHId(),
473 .dhType = capPtr->GetDHType()
474 };
475 auto task = TaskFactory::GetInstance().CreateTask(TaskType::ENABLE, taskParam, nullptr);
476 TaskExecutor::GetInstance().PushTask(task);
477 }
478 }
479
HandleMetaCapabilityDeleteChange(const std::vector<DistributedKv::Entry> & deleteRecords)480 void MetaInfoManager::HandleMetaCapabilityDeleteChange(const std::vector<DistributedKv::Entry> &deleteRecords)
481 {
482 std::lock_guard<std::mutex> lock(metaInfoMgrMutex_);
483 for (const auto &item : deleteRecords) {
484 const std::string value = item.value.ToString();
485 std::shared_ptr<MetaCapabilityInfo> capPtr;
486 if (GetCapabilityByValue<MetaCapabilityInfo>(value, capPtr) != DH_FWK_SUCCESS) {
487 DHLOGE("Get Meta capability by value failed");
488 continue;
489 }
490 const auto keyString = capPtr->GetKey();
491 DHLOGI("Delete MetaCapability key: %{public}s", capPtr->GetAnonymousKey().c_str());
492 globalMetaInfoMap_.erase(keyString);
493 }
494 }
495
GetEntriesByKeys(const std::vector<std::string> & keys)496 std::vector<DistributedKv::Entry> MetaInfoManager::GetEntriesByKeys(const std::vector<std::string> &keys)
497 {
498 if (!IsArrayLengthValid(keys)) {
499 return {};
500 }
501 DHLOGI("call");
502 std::lock_guard<std::mutex> lock(metaInfoMgrMutex_);
503 if (dbAdapterPtr_ == nullptr) {
504 DHLOGE("dbAdapterPtr_ is null");
505 return {};
506 }
507 return dbAdapterPtr_->GetEntriesByKeys(keys);
508 }
509 }
510 }