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