diff --git a/common/utils/include/constants.h b/common/utils/include/constants.h index 37b1dfa6f0edaf12eeed62337ae408d884d4e98c..18fda5b9bbd8e38f646c6574fec73aee0329b15f 100644 --- a/common/utils/include/constants.h +++ b/common/utils/include/constants.h @@ -28,6 +28,7 @@ namespace DistributedHardware { const std::string COMPONENTSLOAD_PROFILE_PATH = R"(/etc/distributed_hardware_components_cfg.json)"; const std::string APP_ID = "dtbhardware_manager_service"; const std::string GLOBAL_CAPABILITY_ID = "global_capability_info"; + const std::string GLOBAL_VERSION_ID = "global_version_info"; const std::string RESOURCE_SEPARATOR = "###"; const std::string DH_ID = "dh_id"; const std::string DEV_ID = "dev_id"; @@ -36,6 +37,13 @@ namespace DistributedHardware { const std::string DH_TYPE = "dh_type"; const std::string DH_ATTRS = "dh_attrs"; const std::string DH_LOG_TITLE_TAG = "DHFWK"; + const std::string DH_VER = "dh_ver"; + const std::string COMP_VER = "comp_ver"; + const std::string NAME = "name"; + const std::string TYPE = "type"; + const std::string HANDLER = "handler"; + const std::string SOURCE_VER = "source_ver"; + const std::string SINK_VER = "sink_ver"; const std::string DH_TASK_NAME_PREFIX = "Task_"; const std::string DH_FWK_PKG_NAME = "ohos.dhardware"; const std::string DH_COMPONENT_VERSIONS = "componentVersions"; diff --git a/common/utils/include/device_type.h b/common/utils/include/device_type.h index 0391994a2122d4e57beee74d6a4e7b364383c547..aa205318c1e3776a8268d8338a0fac5bb5c84066 100644 --- a/common/utils/include/device_type.h +++ b/common/utils/include/device_type.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -28,7 +28,7 @@ enum class DHType : uint32_t { SPEAKER = 0x04, // Speaker DISPLAY = 0x08, // Display GPS = 0x10, // GPS - INPUT = 0x20, // Key board + INPUT = 0x20, // Key board HFP = 0x40, // HFP External device A2D = 0x80, // A2DP External device VIRMODEM_MIC = 0x100, // Cellular call MIC diff --git a/common/utils/include/distributed_hardware_errno.h b/common/utils/include/distributed_hardware_errno.h index 96a88bfe39b3e2a247f8d8f02b5d75878a64b0c0..04fa2e52dc5677d9c691c58b075d97bd970d9ac1 100644 --- a/common/utils/include/distributed_hardware_errno.h +++ b/common/utils/include/distributed_hardware_errno.h @@ -44,6 +44,7 @@ namespace DistributedHardware { constexpr int32_t ERR_DH_FWK_COMPONENT_UNREGISTER_FAILED = -10007; constexpr int32_t ERR_DH_FWK_COMPONENT_GET_REMOTE_SA_FAILED = -10008; constexpr int32_t ERR_DH_FWK_COMPONENT_GET_SINK_VERSION_FAILED = -10009; + constexpr int32_t ERR_DH_FWK_COMPONENT_DHTYPE_NOT_FOUND = -10010; /* ResourceManager errno, range: [-10400, -10499] */ constexpr int32_t ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL = -10400; @@ -57,6 +58,8 @@ namespace DistributedHardware { constexpr int32_t ERR_DH_FWK_RESOURCE_REGISTER_DB_FAILED = -10408; constexpr int32_t ERR_DH_FWK_RESOURCE_UNREGISTER_DB_FAILED = -10409; constexpr int32_t ERR_DH_FWK_RESOURCE_KEY_IS_EMPTY = -10410; + constexpr int32_t ERR_DH_FWK_RESOURCE_SYNC_VERSIONINFO_FAIL = -10411; + constexpr int32_t ERR_DH_FWK_RESOURCE_DEVICE_ID_NOT_EXIST = -10412; /* DistributedHardwareManager errno, range: [-10500, -10599] */ constexpr int32_t ERR_DH_FWK_HARDWARE_MANAGER_DEVICE_REPEAT_ONLINE = -10500; diff --git a/services/distributedhardwarefwkserviceimpl/BUILD.gn b/services/distributedhardwarefwkserviceimpl/BUILD.gn index 83328024ead464c63820022252b8344039436970..2b1baf686af4a223f87f79fa31775a556c3bb741 100644 --- a/services/distributedhardwarefwkserviceimpl/BUILD.gn +++ b/services/distributedhardwarefwkserviceimpl/BUILD.gn @@ -52,6 +52,8 @@ ohos_shared_library("distributedhardwarefwksvr_impl") { "src/resourcemanager/capability_info_manager.cpp", "src/resourcemanager/capability_utils.cpp", "src/resourcemanager/db_adapter.cpp", + "src/resourcemanager/version_info.cpp", + "src/resourcemanager/version_info_manager.cpp", "src/task/disable_task.cpp", "src/task/enable_task.cpp", "src/task/monitor_task_timer.cpp", diff --git a/services/distributedhardwarefwkserviceimpl/include/componentloader/component_loader.h b/services/distributedhardwarefwkserviceimpl/include/componentloader/component_loader.h index b0dca951e9797874a244576732c2286600334a46..1ef1ea69b96b37a5b5210bd34358e33f5fb0e773 100644 --- a/services/distributedhardwarefwkserviceimpl/include/componentloader/component_loader.h +++ b/services/distributedhardwarefwkserviceimpl/include/componentloader/component_loader.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -75,6 +75,7 @@ private: int32_t GetCompPathAndVersion(const std::string &jsonStr, std::map &dhtypeMap); CompVersion GetCompVersionFromComConfig(const CompConfig& cCfg); int32_t ParseConfig(); + void StoreLocalDHVersionInDB(); bool IsDHTypeExist(DHType dhType); std::string Readfile(const std::string &filePath); diff --git a/services/distributedhardwarefwkserviceimpl/include/componentmanager/component_manager.h b/services/distributedhardwarefwkserviceimpl/include/componentmanager/component_manager.h index 8a592df899ef97f93bab471bf023c7a6d30af70e..005008e6e96cd9bfa29ae43fde9071a02bed6b4a 100644 --- a/services/distributedhardwarefwkserviceimpl/include/componentmanager/component_manager.h +++ b/services/distributedhardwarefwkserviceimpl/include/componentmanager/component_manager.h @@ -27,6 +27,7 @@ #include "idistributed_hardware.h" #include "idistributed_hardware_sink.h" #include "idistributed_hardware_source.h" +#include "version_info.h" namespace OHOS { namespace DistributedHardware { @@ -67,16 +68,21 @@ private: bool WaitForResult(const Action &action, ActionResult result); int32_t GetEnableParam(const std::string &networkId, const std::string &uuid, const std::string &dhId, DHType dhType, EnableParam ¶m); - std::string GetSinkVersion(const std::string &networkId, const std::string &uuid, DHType dhType); - std::string GetVersionFromCache(const std::string &uuid, DHType dhType); - int32_t UpdateVersionCache(const std::string &networkId, const std::string &uuid); + int32_t GetSinkVersionFromVerMgr(const std::string &uuid, const DHType dhType, + std::string &sinkVersion); + int32_t GetSinkVersionFromVerInfoMgr(const std::string &uuid, const DHType dhType, + std::string &sinkVersion); + int32_t GetSinkVersionFromRPC(const std::string &networkId, const std::string &uuid, + DHType dhType, std::string &sinkVersion); + int32_t GetSinkVersion(const std::string &networkId, const std::string &uuid, + DHType dhType, std::string &sinkVersion); + void UpdateVersionCache(const std::string &uuid, const VersionInfo &versionInfo); + void UpdateVersionCache(const std::string &uuid, const std::unordered_map &versions); sptr GetRemoteDHMS(const std::string &networkId) const; private: std::map compSource_; std::map compSink_; - std::unordered_map> sinkVersions_; - std::mutex sinkVersionMutex_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info.h b/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info.h new file mode 100644 index 0000000000000000000000000000000000000000..0890922789fd2edcaecc68fc9562dc5abd87a3ba --- /dev/null +++ b/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DISTRIBUTED_HARDWARE_VERSION_INFO_H +#define OHOS_DISTRIBUTED_HARDWARE_VERSION_INFO_H + +#include "nlohmann/json.hpp" + +#include "impl_utils.h" + +namespace OHOS { +namespace DistributedHardware { +struct VersionInfo { + std::string deviceId; + std::string dhVersion; + std::unordered_map compVersions; + + void FromJsonString(const std::string &jsonStr); + std::string ToJsonString() const; +}; + +void ToJson(nlohmann::json &jsonObject, const VersionInfo &versionInfo); +void FromJson(const nlohmann::json &jsonObject, CompVersion &compVer); +void FromJson(const nlohmann::json &jsonObject, VersionInfo &versionInfo); +} // namespace DistributedHardware +} // namespace OHOS +#endif diff --git a/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_event.h b/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_event.h new file mode 100644 index 0000000000000000000000000000000000000000..607b13ba0f5a43095170747987e528eeca1f8bfe --- /dev/null +++ b/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_event.h @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DISTRIBUTED_HARDWARE_VERSION_INFO_EVENT_H +#define OHOS_DISTRIBUTED_HARDWARE_VERSION_INFO_EVENT_H + +#include + +#include "distributed_hardware_log.h" +#include "event.h" +#include "event_sender.h" + +namespace OHOS { +namespace DistributedHardware { +class VersionInfoEvent : public Event { + TYPEINDENT(VersionInfoEvent) + +public: + enum class EventType : uint32_t { + UNDEFINED = 0, + RECOVER = 1, + }; + +public: + explicit VersionInfoEvent(EventSender &sender) : Event(sender) + { + action_ = EventType::UNDEFINED; + } + + VersionInfoEvent(EventSender &sender, EventType action) : Event(sender), action_(action) {} + + virtual ~VersionInfoEvent() {} + + EventType GetAction() const + { + return action_; + } + +private: + EventType action_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif diff --git a/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_manager.h b/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_manager.h new file mode 100644 index 0000000000000000000000000000000000000000..3871740d12bbe6b555b34e0f294d7720dfce1160 --- /dev/null +++ b/services/distributedhardwarefwkserviceimpl/include/resourcemanager/version_info_manager.h @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DISTRIBUTED_HARDWARE_VERSION_INFO_MANAGER_H +#define OHOS_DISTRIBUTED_HARDWARE_VERSION_INFO_MANAGER_H + +#include +#include +#include + +#include "kvstore_observer.h" + +#include "db_adapter.h" +#include "event.h" +#include "eventbus_handler.h" +#include "event_bus.h" +#include "event_sender.h" +#include "impl_utils.h" +#include "single_instance.h" +#include "version_info.h" +#include "version_info_event.h" + +class DBAdapter; +namespace OHOS { +namespace DistributedHardware { +class VersionInfoManager : public std::enable_shared_from_this, + public EventSender, + public DistributedKv::KvStoreObserver, + public EventBusHandler { +public: + VersionInfoManager(const VersionInfoManager &) = delete; + VersionInfoManager &operator = (const VersionInfoManager &) = delete; + VersionInfoManager(VersionInfoManager &&) = delete; + VersionInfoManager &operator = (VersionInfoManager &&) = delete; + static std::shared_ptr GetInstance(); + virtual ~VersionInfoManager(); + + int32_t Init(); + int32_t UnInit(); + + int32_t AddVersion(const VersionInfo &versionInfo); + int32_t GetVersionInfoByDeviceId(const std::string &deviceId, VersionInfo &versionInfo); + int32_t SyncVersionInfoFromDB(const std::string &deviceId); + int32_t SyncRemoteVersionInfos(); + + void CreateManualSyncCount(const std::string &deviceId); + void RemoveManualSyncCount(const std::string &deviceId); + int32_t ManualSync(const std::string &networkId); + + void OnChange(const DistributedKv::ChangeNotification &changeNotification) override; + void OnEvent(VersionInfoEvent &ev) override; + +private: + VersionInfoManager(); + void UpdateVersionCache(const VersionInfo &versionInfo); + void HandleVersionAddChange(const std::vector &insertRecords); + void HandleVersionUpdateChange(const std::vector &updateRecords); + void HandleVersionDeleteChange(const std::vector &deleteRecords); + +private: + mutable std::mutex verInfoMgrMutex_; + std::shared_ptr dbAdapterPtr_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif diff --git a/services/distributedhardwarefwkserviceimpl/include/utils/impl_utils.h b/services/distributedhardwarefwkserviceimpl/include/utils/impl_utils.h index 992cc601c746b6f6deafbd659850927dfaf99b23..632505cd9af8f9ee0185a2fb54b06921a8ebeabb 100644 --- a/services/distributedhardwarefwkserviceimpl/include/utils/impl_utils.h +++ b/services/distributedhardwarefwkserviceimpl/include/utils/impl_utils.h @@ -19,6 +19,7 @@ #include #include "device_type.h" +#include "constants.h" namespace OHOS { namespace DistributedHardware { diff --git a/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h b/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h index 0626567add6c46c250f476ef07f4250f74005a03..3d36bd1f0c858d852b23063ef0804d7a490ff6e2 100644 --- a/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h +++ b/services/distributedhardwarefwkserviceimpl/include/versionmanager/version_manager.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -40,6 +40,7 @@ public: int32_t RemoveDHVersion(const std::string &uuid); int32_t GetDHVersion(const std::string &uuid, DHVersion &dhVersion); int32_t GetCompVersion(const std::string &uuid, const DHType dhType, CompVersion &compVersion); + int32_t SyncDHVersionFromDB(const std::string &uuid, DHVersion &dhVersion); std::string GetLocalDeviceVersion(); void ShowLocalVersion(const DHVersion &dhVersion) const; diff --git a/services/distributedhardwarefwkserviceimpl/src/componentloader/component_loader.cpp b/services/distributedhardwarefwkserviceimpl/src/componentloader/component_loader.cpp index 02e9c091a4502c41f12c16a7b36af92c09ccadbb..05de0cb874a692bf28b5a241783d36ba493caa17 100644 --- a/services/distributedhardwarefwkserviceimpl/src/componentloader/component_loader.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/componentloader/component_loader.cpp @@ -22,10 +22,14 @@ #include "nlohmann/json.hpp" #include "constants.h" +#include "dh_context.h" #include "dh_utils_hitrace.h" -#include "distributed_hardware_log.h" #include "dh_utils_hisysevent.h" #include "hidump_helper.h" +#include "distributed_hardware_log.h" +#include "version_info.h" +#include "version_info_manager.h" +#include "version_manager.h" using nlohmann::json; @@ -67,8 +71,10 @@ std::map g_mapDhTypeName = { int32_t ComponentLoader::Init() { + DHLOGI("start"); DHTraceStart(COMPONENT_LOAD_START); int32_t ret = ParseConfig(); + StoreLocalDHVersionInDB(); DHTraceEnd(); return ret; @@ -136,6 +142,19 @@ int32_t ComponentLoader::GetLocalDHVersion(DHVersion &dhVersion) return DH_FWK_SUCCESS; } +void ComponentLoader::StoreLocalDHVersionInDB() +{ + if (!isLocalVersionInit_.load()) { + DHLOGE("Store local DHVersion fail"); + return; + } + VersionInfo versionInfo; + versionInfo.dhVersion = VersionManager::GetInstance().GetLocalDeviceVersion(); + versionInfo.deviceId = DHContext::GetInstance().GetDeviceInfo().deviceId; + versionInfo.compVersions = localDHVersion_.compVersions; + VersionInfoManager::GetInstance()->AddVersion(versionInfo); +} + void *ComponentLoader::GetHandler(const std::string &soName) { if (soName.length() <= 0) { @@ -256,8 +275,11 @@ int32_t ComponentLoader::ParseConfig() return ERR_DH_FWK_LOADER_COMPONENT_PROFILE_IS_EMPTY; } ret = GetCompPathAndVersion(jsonStr, dhtypeMap); + if (ret != DH_FWK_SUCCESS) { + return ret; + } GetAllHandler(dhtypeMap); - return ret; + return DH_FWK_SUCCESS; } int32_t ComponentLoader::ReleaseHandler(void *&handler) diff --git a/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp index 7f79eb5c76c844ecefb74c81c571637ebe72e654..5bb89fa84e5cd9bfd5d7f7a5fb92a93e3d0f54b2 100644 --- a/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp @@ -35,6 +35,7 @@ #include "iservice_registry.h" #include "monitor_task_timer.h" #include "system_ability_definition.h" +#include "version_info_manager.h" #include "version_manager.h" namespace OHOS { @@ -105,10 +106,7 @@ int32_t ComponentManager::UnInit() compSource_.clear(); compSink_.clear(); - { - std::lock_guard lock(sinkVersionMutex_); - sinkVersions_.clear(); - } + MonitorTaskTimer::GetInstance().StopTimer(); DHLOGI("Release component success"); return DH_FWK_SUCCESS; @@ -341,58 +339,62 @@ int32_t ComponentManager::GetEnableParam(const std::string &networkId, const std } param.attrs = capability->GetDHAttrs(); - param.version = GetSinkVersion(networkId, uuid, dhType); - if (param.version.empty()) { - DHLOGI("Get Sink Version failed, uuid = %s, dhId = %s", GetAnonyString(uuid).c_str(), - GetAnonyString(dhId).c_str()); + std::string sinkVersion(""); + ret = GetSinkVersion(networkId, uuid, dhType, sinkVersion); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("Get sink version failed, uuid = %s, dhId = %s, dhType = %#X,", GetAnonyString(uuid).c_str(), + GetAnonyString(dhId).c_str(), dhType); return ERR_DH_FWK_COMPONENT_GET_SINK_VERSION_FAILED; } - + param.version = sinkVersion; DHLOGI("success. uuid =%s, dhId = %s, version = %s", GetAnonyString(uuid).c_str(), GetAnonyString(dhId).c_str(), param.version.c_str()); return DH_FWK_SUCCESS; } -std::string ComponentManager::GetSinkVersion(const std::string &networkId, const std::string &uuid, DHType dhType) +int32_t ComponentManager::GetSinkVersionFromVerMgr(const std::string &uuid, const DHType dhType, + std::string &sinkVersion) { - DHLOGI("networkId = %s ", GetAnonyString(networkId).c_str()); - auto sinkVersion = GetVersionFromCache(uuid, dhType); - if (!sinkVersion.empty()) { - DHLOGI("GetVersionFromCache success, sinkVersion = %s, uuid = %s, dhType = %#X", sinkVersion.c_str(), - GetAnonyString(uuid).c_str(), dhType); - return sinkVersion; - } - - auto updateResult = UpdateVersionCache(networkId, uuid); - if (updateResult != DH_FWK_SUCCESS) { - DHLOGE("UpdateVersionCache failed, uuid = %s, errCode = %d", GetAnonyString(uuid).c_str(), updateResult); - return ""; + CompVersion compversion; + int32_t ret = VersionManager::GetInstance().GetCompVersion(uuid, dhType, compversion); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("Get sink version from version Manager failed, uuid =%s, dhType = %#X, errCode = %d", + GetAnonyString(uuid).c_str(), dhType, ret); + return ret; } - - sinkVersion = GetVersionFromCache(uuid, dhType); - return sinkVersion; + DHLOGI("Get sink version from version mgr success, sinkVersion = %s, uuid = %s, dhType = %#X", + compversion.sinkVersion.c_str(), GetAnonyString(uuid).c_str(), dhType); + sinkVersion = compversion.sinkVersion; + return DH_FWK_SUCCESS; } -std::string ComponentManager::GetVersionFromCache(const std::string &uuid, DHType dhType) +int32_t ComponentManager::GetSinkVersionFromVerInfoMgr(const std::string &uuid, const DHType dhType, + std::string &sinkVersion) { - std::lock_guard lock(sinkVersionMutex_); - auto iter = sinkVersions_.find(uuid); - if (iter == sinkVersions_.end()) { - DHLOGE("can not find component version for uuid = %s", GetAnonyString(uuid).c_str()); - return ""; + VersionInfo versionInfo; + int32_t ret = VersionInfoManager::GetInstance()->GetVersionInfoByDeviceId(GetDeviceIdByUUID(uuid), versionInfo); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("Get sink version from Version info Manager failed, uuid =%s, dhType = %#X, errCode = %d", + GetAnonyString(uuid).c_str(), dhType, ret); + return ret; } - - auto find = iter->second.find(dhType); - if (find == iter->second.end()) { - DHLOGE("can not find component version for uuid = %s, dhType = %#X", uuid.c_str(), dhType); - return ""; + auto iter = versionInfo.compVersions.find(dhType); + if (iter == versionInfo.compVersions.end()) { + DHLOGE("can not find component version for dhType = %d", dhType); + return ERR_DH_FWK_COMPONENT_DHTYPE_NOT_FOUND; } - return find->second; + DHLOGI("Get SinkVersion from version info mgr success, sinkVersion = %s, uuid = %s, dhType = %#X", + iter->second.sinkVersion.c_str(), GetAnonyString(uuid).c_str(), dhType); + UpdateVersionCache(uuid, versionInfo); + sinkVersion = iter->second.sinkVersion; + return DH_FWK_SUCCESS; } -int32_t ComponentManager::UpdateVersionCache(const std::string &networkId, const std::string &uuid) +int32_t ComponentManager::GetSinkVersionFromRPC(const std::string &networkId, const std::string &uuid, + DHType dhType, std::string &sinkVersion) { + DHLOGI("networkId = %s ", GetAnonyString(networkId).c_str()); sptr dhms = GetRemoteDHMS(networkId); if (dhms == nullptr) { DHLOGI("GetRemoteDHMS failed, networkId = %s", GetAnonyString(networkId).c_str()); @@ -405,14 +407,65 @@ int32_t ComponentManager::UpdateVersionCache(const std::string &networkId, const DHLOGE("QuerySinkVersion failed, errCode = %d", ret); return ret; } - { - std::lock_guard lock(sinkVersionMutex_); - sinkVersions_.emplace(uuid, versions); + + auto iter = versions.find(dhType); + if (iter == versions.end()) { + DHLOGE("can not find component version for uuid = %s, dhType = %#X", + GetAnonyString(uuid).c_str(), dhType); + return ERR_DH_FWK_COMPONENT_DHTYPE_NOT_FOUND; } - DHLOGI("QuerySinkVersion success"); + DHLOGI("QuerySinkVersion success, sinkVersion = %s, uuid = %s, dhType = %#X", + iter->second.c_str(), GetAnonyString(uuid).c_str(), dhType); + UpdateVersionCache(uuid, versions); + sinkVersion = iter->second; + return DH_FWK_SUCCESS; } +int32_t ComponentManager::GetSinkVersion(const std::string &networkId, const std::string &uuid, + DHType dhType, std::string &sinkVersion) +{ + int32_t ret = GetSinkVersionFromVerMgr(uuid, dhType, sinkVersion); + if ((ret == DH_FWK_SUCCESS) && (!sinkVersion.empty())) { + return DH_FWK_SUCCESS; + } + + ret = GetSinkVersionFromVerInfoMgr(uuid, dhType, sinkVersion); + if ((ret == DH_FWK_SUCCESS) && (!sinkVersion.empty())) { + return DH_FWK_SUCCESS; + } + + ret = GetSinkVersionFromRPC(networkId, uuid, dhType, sinkVersion); + if ((ret == DH_FWK_SUCCESS) && (!sinkVersion.empty())) { + return DH_FWK_SUCCESS; + } + + return ret; +} + +void ComponentManager::UpdateVersionCache(const std::string &uuid, const VersionInfo &versionInfo) +{ + DHVersion dhVersion; + dhVersion.uuid = uuid; + dhVersion.dhVersion = versionInfo.dhVersion; + dhVersion.compVersions = versionInfo.compVersions; + VersionManager::GetInstance().AddDHVersion(uuid, dhVersion); +} + +void ComponentManager::UpdateVersionCache(const std::string &uuid, + const std::unordered_map &versions) +{ + DHVersion dhVersion; + dhVersion.uuid = uuid; + for (const auto &versionPair : versions) { + CompVersion compVersion; + compVersion.dhType = versionPair.first; + compVersion.sinkVersion = versionPair.second; + dhVersion.compVersions.insert(std::pair(compVersion.dhType, compVersion)); + } + VersionManager::GetInstance().AddDHVersion(uuid, dhVersion); +} + sptr ComponentManager::GetRemoteDHMS(const std::string &networkId) const { DHLOGI("start, networkId = %s", GetAnonyString(networkId).c_str()); diff --git a/services/distributedhardwarefwkserviceimpl/src/distributed_hardware_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/distributed_hardware_manager.cpp index 8b0270a3080cf14890ac15743143e19862ce057d..9139cd9cf5b1b60c59d0b1070c1e9c6a3a6a832b 100644 --- a/services/distributedhardwarefwkserviceimpl/src/distributed_hardware_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/distributed_hardware_manager.cpp @@ -29,6 +29,7 @@ #include "task_board.h" #include "task_executor.h" #include "task_factory.h" +#include "version_info_manager.h" #include "version_manager.h" namespace OHOS { @@ -54,6 +55,8 @@ int32_t DistributedHardwareManager::Initialize() DHLOGI("start"); CapabilityInfoManager::GetInstance()->Init(); + VersionInfoManager::GetInstance()->Init(); + ComponentLoader::GetInstance().Init(); LocalHardwareManager::GetInstance().Init(); @@ -78,6 +81,8 @@ int32_t DistributedHardwareManager::Release() ComponentLoader::GetInstance().UnInit(); + VersionInfoManager::GetInstance()->UnInit(); + CapabilityInfoManager::GetInstance()->UnInit(); return DH_FWK_SUCCESS; @@ -113,6 +118,7 @@ int32_t DistributedHardwareManager::SendOnLineEvent(const std::string &networkId TaskExecutor::GetInstance().PushTask(task); DHContext::GetInstance().AddOnlineDevice(uuid, networkId); CapabilityInfoManager::GetInstance()->CreateManualSyncCount(GetDeviceIdByUUID(uuid)); + VersionInfoManager::GetInstance()->CreateManualSyncCount(GetDeviceIdByUUID(uuid)); return DH_FWK_SUCCESS; } @@ -158,6 +164,7 @@ int32_t DistributedHardwareManager::SendOffLineEvent(const std::string &networkI DHContext::GetInstance().RemoveOnlineDevice(realUUID); CapabilityInfoManager::GetInstance()->RemoveManualSyncCount(GetDeviceIdByUUID(realUUID)); + VersionInfoManager::GetInstance()->RemoveManualSyncCount(GetDeviceIdByUUID(realUUID)); HiSysEventWriteCompOfflineMsg(DHFWK_DEV_OFFLINE, OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR, GetAnonyString(networkId), "dhfwk device offline event."); diff --git a/services/distributedhardwarefwkserviceimpl/src/localhardwaremanager/local_hardware_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/localhardwaremanager/local_hardware_manager.cpp index 2239ba7e4c13008a34e41c482245aa9a6e38d01f..547c206caa501c12c246af7e82df684bb78fa6ac 100644 --- a/services/distributedhardwarefwkserviceimpl/src/localhardwaremanager/local_hardware_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/localhardwaremanager/local_hardware_manager.cpp @@ -38,6 +38,7 @@ LocalHardwareManager::~LocalHardwareManager() {} void LocalHardwareManager::Init() { + DHLOGI("start"); std::vector allCompTypes = ComponentLoader::GetInstance().GetAllCompTypes(); for (auto dhType : allCompTypes) { IHardwareHandler *hardwareHandler = nullptr; diff --git a/services/distributedhardwarefwkserviceimpl/src/resourcemanager/db_adapter.cpp b/services/distributedhardwarefwkserviceimpl/src/resourcemanager/db_adapter.cpp index 092d2dab4a374e71b1f1512bc20dc176c5a30850..db668f7e9bfda12b05d48dcc077d654729141fd8 100644 --- a/services/distributedhardwarefwkserviceimpl/src/resourcemanager/db_adapter.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/resourcemanager/db_adapter.cpp @@ -24,6 +24,7 @@ #include "distributed_hardware_errno.h" #include "distributed_hardware_log.h" #include "event_bus.h" +#include "version_info_event.h" namespace OHOS { namespace DistributedHardware { @@ -263,8 +264,15 @@ int32_t DBAdapter::ManualSync(const std::string &networkId) void DBAdapter::SyncDBForRecover() { DHLOGI("Sync store id: %s after db recover", storeId_.storeId.c_str()); - CapabilityInfoEvent recoverEvent(*this, CapabilityInfoEvent::EventType::RECOVER); - DHContext::GetInstance().GetEventBus()->PostEvent(recoverEvent); + if (storeId_.storeId == GLOBAL_CAPABILITY_ID) { + CapabilityInfoEvent recoverEvent(*this, CapabilityInfoEvent::EventType::RECOVER); + DHContext::GetInstance().GetEventBus()->PostEvent(recoverEvent); + } + + if (storeId_.storeId == GLOBAL_VERSION_ID) { + VersionInfoEvent recoverEvent(*this, VersionInfoEvent::EventType::RECOVER); + DHContext::GetInstance().GetEventBus()->PostEvent(recoverEvent); + } } int32_t DBAdapter::RegisterChangeListener() diff --git a/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info.cpp b/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info.cpp new file mode 100644 index 0000000000000000000000000000000000000000..649f3519e15f60809248e2fc125e83a4aed6917e --- /dev/null +++ b/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info.cpp @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "version_info.h" + +#include + +#include "nlohmann/json.hpp" + +#include "anonymous_string.h" +#include "constants.h" +#include "distributed_hardware_errno.h" +#include "distributed_hardware_log.h" + +namespace OHOS { +namespace DistributedHardware { +#undef DH_LOG_TAG +#define DH_LOG_TAG "VersionInfo" + +void VersionInfo::FromJsonString(const std::string &jsonStr) +{ + nlohmann::json jsonObj = nlohmann::json::parse(jsonStr); + FromJson(jsonObj, *this); +} + +std::string VersionInfo::ToJsonString() const +{ + nlohmann::json jsonObj; + ToJson(jsonObj, *this); + return jsonObj.dump(); +} + +void ToJson(nlohmann::json &jsonObject, const VersionInfo &versionInfo) +{ + jsonObject[DEV_ID] = versionInfo.deviceId; + jsonObject[DH_VER] = versionInfo.dhVersion; + + nlohmann::json compVers; + for (const auto &compVersion : versionInfo.compVersions) { + nlohmann::json compVer; + compVer[NAME] = compVersion.second.name; + compVer[TYPE] = compVersion.second.dhType; + compVer[HANDLER] = compVersion.second.handlerVersion; + compVer[SOURCE_VER] = compVersion.second.sourceVersion; + compVer[SINK_VER] = compVersion.second.sinkVersion; + compVers.push_back(compVer); + } + + jsonObject[COMP_VER] = compVers; +} + +void FromJson(const nlohmann::json &jsonObject, CompVersion &compVer) +{ + if (jsonObject.find(NAME) != jsonObject.end()) { + compVer.name = jsonObject.at(NAME).get(); + } + if (jsonObject.find(TYPE) != jsonObject.end()) { + compVer.dhType = jsonObject.at(TYPE).get(); + } + if (jsonObject.find(HANDLER) != jsonObject.end()) { + compVer.handlerVersion = jsonObject.at(HANDLER).get(); + } + if (jsonObject.find(SOURCE_VER) != jsonObject.end()) { + compVer.sourceVersion = jsonObject.at(SOURCE_VER).get(); + } + if (jsonObject.find(SINK_VER) != jsonObject.end()) { + compVer.sinkVersion = jsonObject.at(SINK_VER).get(); + } +} + +void FromJson(const nlohmann::json &jsonObject, VersionInfo &versionInfo) +{ + if (jsonObject.find(DEV_ID) != jsonObject.end()) { + versionInfo.deviceId = jsonObject.at(DEV_ID).get(); + } + + if (jsonObject.find(DH_VER) != jsonObject.end()) { + versionInfo.dhVersion = jsonObject.at(DH_VER).get(); + } + + if (jsonObject.find(COMP_VER) != jsonObject.end()) { + for (const auto &compVerObj : jsonObject.at(COMP_VER)) { + CompVersion compVer; + FromJson(compVerObj, compVer); + versionInfo.compVersions.insert(std::pair(compVer.dhType, compVer)); + } + } +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_manager.cpp new file mode 100644 index 0000000000000000000000000000000000000000..28907dc453039f16ec024887236e991e69377d9e --- /dev/null +++ b/services/distributedhardwarefwkserviceimpl/src/resourcemanager/version_info_manager.cpp @@ -0,0 +1,296 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "version_info_manager.h" + +#include "anonymous_string.h" +#include "constants.h" +#include "dh_context.h" +#include "dh_utils_tool.h" +#include "distributed_hardware_errno.h" +#include "distributed_hardware_log.h" +#include "event_bus.h" +#include "task_executor.h" +#include "task_factory.h" +#include "version_info_event.h" +#include "version_manager.h" + +class DBAdapter; +namespace OHOS { +namespace DistributedHardware { +#undef DH_LOG_TAG +#define DH_LOG_TAG "VersionInfoManager" + +VersionInfoManager::VersionInfoManager() : dbAdapterPtr_(nullptr) +{} + +VersionInfoManager::~VersionInfoManager() +{ + DHLOGI("VersionInfoManager Destruction!"); +} + +std::shared_ptr VersionInfoManager::GetInstance() +{ + static std::shared_ptr instance(new(std::nothrow) VersionInfoManager); + if (instance == nullptr) { + DHLOGE("instance is nullptr, because applying memory fail!"); + return nullptr; + } + return instance; +} + +int32_t VersionInfoManager::Init() +{ + DHLOGI("VersionInfoManager instance init!"); + std::lock_guard lock(verInfoMgrMutex_); + dbAdapterPtr_ = std::make_shared(APP_ID, GLOBAL_VERSION_ID, shared_from_this()); + if (dbAdapterPtr_->Init() != DH_FWK_SUCCESS) { + DHLOGE("Init dbAdapterPtr_ failed"); + return ERR_DH_FWK_RESOURCE_INIT_DB_FAILED; + } + VersionInfoEvent versionInfoEvent(*this); + DHContext::GetInstance().GetEventBus()->AddHandler(versionInfoEvent.GetType(), *this); + DHLOGI("VersionInfoManager instance init success"); + return DH_FWK_SUCCESS; +} + +int32_t VersionInfoManager::UnInit() +{ + DHLOGI("VersionInfoManager UnInit"); + std::lock_guard lock(verInfoMgrMutex_); + if (dbAdapterPtr_ == nullptr) { + DHLOGE("dbAdapterPtr_ is null"); + return ERR_DH_FWK_RESOURCE_UNINIT_DB_FAILED; + } + dbAdapterPtr_->UnInit(); + dbAdapterPtr_.reset(); + return DH_FWK_SUCCESS; +} + +int32_t VersionInfoManager::AddVersion(const VersionInfo &versionInfo) +{ + std::lock_guard lock(verInfoMgrMutex_); + if (dbAdapterPtr_ == nullptr) { + DHLOGE("dbAdapterPtr_ is null"); + return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL; + } + + std::string data(""); + dbAdapterPtr_->GetDataByKey(versionInfo.deviceId, data); + if (data.compare(versionInfo.ToJsonString()) == 0) { + DHLOGI("dhversion already stored, Key: %s", GetAnonyString(versionInfo.deviceId).c_str()); + return DH_FWK_SUCCESS; + } + + std::string key = versionInfo.deviceId; + std::string value = versionInfo.ToJsonString(); + DHLOGI("AddVersion, Key: %s", GetAnonyString(versionInfo.deviceId).c_str()); + if (dbAdapterPtr_->PutData(key, value) != DH_FWK_SUCCESS) { + DHLOGE("Fail to storage to kv"); + return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; + } + return DH_FWK_SUCCESS; +} + +int32_t VersionInfoManager::GetVersionInfoByDeviceId(const std::string &deviceId, VersionInfo &versionInfo) +{ + std::lock_guard lock(verInfoMgrMutex_); + if (dbAdapterPtr_ == nullptr) { + DHLOGE("dbAdapterPtr_ is null"); + return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL; + } + std::string data(""); + if (dbAdapterPtr_->GetDataByKey(deviceId, data) != DH_FWK_SUCCESS) { + DHLOGE("Query data from DB by deviceId failed, deviceId: %s", GetAnonyString(deviceId).c_str()); + return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; + } + versionInfo.FromJsonString(data); + return DH_FWK_SUCCESS; +} + +void VersionInfoManager::UpdateVersionCache(const VersionInfo &versionInfo) +{ + std::string uuid = DHContext::GetInstance().GetUUIDByDeviceId(versionInfo.deviceId); + if (uuid.empty()) { + DHLOGI("Find uuid failed, deviceId: %s", GetAnonyString(versionInfo.deviceId).c_str()); + return; + } + DHVersion dhVersion; + dhVersion.uuid = uuid; + dhVersion.dhVersion = versionInfo.dhVersion; + dhVersion.compVersions = versionInfo.compVersions; + VersionManager::GetInstance().AddDHVersion(uuid, dhVersion); +} + +int32_t VersionInfoManager::SyncVersionInfoFromDB(const std::string &deviceId) +{ + DHLOGI("Sync versionInfo from DB, deviceId: %s", GetAnonyString(deviceId).c_str()); + + std::lock_guard lock(verInfoMgrMutex_); + if (dbAdapterPtr_ == nullptr) { + DHLOGE("dbAdapterPtr_ is null"); + return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL; + } + std::string data(""); + if (dbAdapterPtr_->GetDataByKey(deviceId, data) != DH_FWK_SUCCESS) { + DHLOGE("Query data from DB by deviceId failed, deviceId: %s", GetAnonyString(deviceId).c_str()); + return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; + } + + DHLOGI("Query data from DB by deviceId success, deviceId: %s", GetAnonyString(deviceId).c_str()); + VersionInfo versionInfo; + versionInfo.FromJsonString(data); + UpdateVersionCache(versionInfo); + return DH_FWK_SUCCESS; +} + +int32_t VersionInfoManager::SyncRemoteVersionInfos() +{ + DHLOGI("Sync full remote version info from DB"); + std::lock_guard lock(verInfoMgrMutex_); + if (dbAdapterPtr_ == nullptr) { + DHLOGE("dbAdapterPtr_ is null"); + return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL; + } + std::vector dataVector; + if (dbAdapterPtr_->GetDataByKeyPrefix("", dataVector) != DH_FWK_SUCCESS) { + DHLOGE("Query all data from DB failed"); + return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; + } + + for (const auto &data : dataVector) { + VersionInfo versionInfo; + versionInfo.FromJsonString(data); + const std::string &deviceId = versionInfo.deviceId; + const std::string &localDeviceId = DHContext::GetInstance().GetDeviceInfo().deviceId; + if (deviceId.compare(localDeviceId) == 0) { + DHLOGE("Local device info not need sync from db"); + continue; + } + if (!DHContext::GetInstance().IsDeviceOnline(deviceId)) { + DHLOGE("Offline device, no need sync to memory, deviceId : %s ", + GetAnonyString(deviceId).c_str()); + continue; + } + UpdateVersionCache(versionInfo); + } + return DH_FWK_SUCCESS; +} + +void VersionInfoManager::CreateManualSyncCount(const std::string &deviceId) +{ + std::lock_guard lock(verInfoMgrMutex_); + if (dbAdapterPtr_ == nullptr) { + DHLOGE("dbAdapterPtr_ is null"); + return; + } + dbAdapterPtr_->CreateManualSyncCount(deviceId); +} + +void VersionInfoManager::RemoveManualSyncCount(const std::string &deviceId) +{ + std::lock_guard lock(verInfoMgrMutex_); + if (dbAdapterPtr_ == nullptr) { + DHLOGE("dbAdapterPtr_ is null"); + return; + } + dbAdapterPtr_->RemoveManualSyncCount(deviceId); +} + +int32_t VersionInfoManager::ManualSync(const std::string &networkId) +{ + DHLOGI("ManualSync start, networkId: %s", GetAnonyString(networkId).c_str()); + std::unique_lock lock(verInfoMgrMutex_); + if (dbAdapterPtr_ == nullptr) { + DHLOGE("dbAdapterPtr_ is null"); + return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL; + } + if (dbAdapterPtr_->ManualSync(networkId) != DH_FWK_SUCCESS) { + DHLOGE("ManualSync failed"); + return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; + } + return DH_FWK_SUCCESS; +} + +void VersionInfoManager::OnChange(const DistributedKv::ChangeNotification &changeNotification) +{ + DHLOGI("DB data OnChange"); + if (!changeNotification.GetInsertEntries().empty()) { + DHLOGI("Handle version data add change"); + HandleVersionAddChange(changeNotification.GetInsertEntries()); + } + if (!changeNotification.GetUpdateEntries().empty()) { + DHLOGI("Handle version data update change"); + HandleVersionUpdateChange(changeNotification.GetUpdateEntries()); + } + if (!changeNotification.GetDeleteEntries().empty()) { + DHLOGI("Handle version data delete change"); + HandleVersionDeleteChange(changeNotification.GetDeleteEntries()); + } +} + +void VersionInfoManager::HandleVersionAddChange(const std::vector &insertRecords) +{ + DHLOGI("Version add change"); + for (const auto &item : insertRecords) { + const std::string value = item.value.ToString(); + VersionInfo versionInfo; + versionInfo.FromJsonString(value); + UpdateVersionCache(versionInfo); + } +} + +void VersionInfoManager::HandleVersionUpdateChange(const std::vector &updateRecords) +{ + DHLOGI("Version update change"); + for (const auto &item : updateRecords) { + const std::string value = item.value.ToString(); + VersionInfo versionInfo; + versionInfo.FromJsonString(value); + UpdateVersionCache(versionInfo); + } +} + +void VersionInfoManager::HandleVersionDeleteChange(const std::vector &deleteRecords) +{ + DHLOGI("Version delete change"); + for (const auto &item : deleteRecords) { + const std::string value = item.value.ToString(); + VersionInfo dhVersion; + dhVersion.FromJsonString(value); + const std::string &deviceId = dhVersion.deviceId; + std::string uuid = DHContext::GetInstance().GetUUIDByDeviceId(deviceId); + if (uuid.empty()) { + DHLOGI("Find uuid failed, deviceId: %s", GetAnonyString(deviceId).c_str()); + continue; + } + DHLOGI("Delete version ,uuid: %s", GetAnonyString(uuid).c_str()); + VersionManager::GetInstance().RemoveDHVersion(uuid); + } +} + +void VersionInfoManager::OnEvent(VersionInfoEvent &ev) +{ + switch (ev.GetAction()) { + case VersionInfoEvent::EventType::RECOVER: + SyncRemoteVersionInfos(); + break; + default: + DHLOGE("Event is undefined, type is %d", ev.GetAction()); + break; + } +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/distributedhardwarefwkserviceimpl/src/task/online_task.cpp b/services/distributedhardwarefwkserviceimpl/src/task/online_task.cpp index a576cabe39e3569f44effcaf4ac0a86e1ad3317e..8f65bb3699528d823f684a60c7147e0c994ac710 100644 --- a/services/distributedhardwarefwkserviceimpl/src/task/online_task.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/task/online_task.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -23,6 +23,7 @@ #include "task_board.h" #include "task_executor.h" #include "task_factory.h" +#include "version_info_manager.h" namespace OHOS { namespace DistributedHardware { @@ -73,10 +74,20 @@ void OnLineTask::DoSyncInfo() if (ret != DH_FWK_SUCCESS) { DHLOGW("ManualSync failed, uuid = %s, errCode = %d", GetAnonyString(GetUUID()).c_str(), ret); } + + ret = VersionInfoManager::GetInstance()->ManualSync(GetNetworkId()); + if (ret != DH_FWK_SUCCESS) { + DHLOGW("ManualSync version failed, uuid = %s, errCode = %d", GetAnonyString(GetUUID()).c_str(), ret); + } + ret = CapabilityInfoManager::GetInstance()->SyncDeviceInfoFromDB(GetDeviceIdByUUID(GetUUID())); if (ret != DH_FWK_SUCCESS) { DHLOGE("SyncDeviceInfoFromDB failed, uuid = %s, errCode = %d", GetAnonyString(GetUUID()).c_str(), ret); - return; + } + + ret = VersionInfoManager::GetInstance()->SyncVersionInfoFromDB(GetDeviceIdByUUID(GetUUID())); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("SyncVersionInfoFromDB failed, uuid = %s, errCode = %d", GetAnonyString(GetUUID()).c_str(), ret); } } diff --git a/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp index c965b41331a2d43b718e339394dbf4234656fd8b..34e39e4745b181695548f16ffd509ca63e155ac7 100644 --- a/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/versionmanager/version_manager.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -13,10 +13,10 @@ * limitations under the License. */ -#include "versionmanager/version_manager.h" +#include "version_manager.h" #include "anonymous_string.h" -#include "componentloader/component_loader.h" +#include "component_loader.h" #include "dh_context.h" #include "distributed_hardware_log.h" @@ -35,7 +35,7 @@ int32_t VersionManager::Init() DHLOGE("GetLocalDHVersion fail"); return ret; } - dhVersion.dhVersion = DH_LOCAL_VERSION; + dhVersion.dhVersion = GetLocalDeviceVersion(); ShowLocalVersion(dhVersion); std::string strUUID = DHContext::GetInstance().GetDeviceInfo().uuid; AddDHVersion(strUUID, dhVersion); @@ -59,9 +59,9 @@ void VersionManager::ShowLocalVersion(const DHVersion &dhVersion) const int32_t VersionManager::AddDHVersion(const std::string &uuid, const DHVersion &dhVersion) { - DHLOGI("uuid: %s", GetAnonyString(uuid).c_str()); + DHLOGI("addDHVersion uuid: %s", GetAnonyString(uuid).c_str()); std::lock_guard lock(versionMutex_); - dhVersions_.insert(std::pair(uuid, dhVersion)); + dhVersions_[uuid] = dhVersion; return DH_FWK_SUCCESS; } @@ -104,6 +104,8 @@ int32_t VersionManager::GetCompVersion(const std::string &uuid, const DHType dhT DHLOGE("not find dhType: %#X", dhType); return ERR_DH_FWK_TYPE_NOT_EXIST; } + + DHLOGI("GetCompVersion success, uuid: %s, dhType: %#X", GetAnonyString(uuid).c_str(), dhType); compVersion = dhVersion.compVersions[dhType]; return DH_FWK_SUCCESS; } diff --git a/services/distributedhardwarefwkserviceimpl/test/fuzztest/componentmanager_fuzzer/BUILD.gn b/services/distributedhardwarefwkserviceimpl/test/fuzztest/componentmanager_fuzzer/BUILD.gn index 9955a866f8e23b3a6eee5e920928effd9b01f23b..ba5f1b9af8dd7e8c4e03fb578d4aaeec80cbb874 100644 --- a/services/distributedhardwarefwkserviceimpl/test/fuzztest/componentmanager_fuzzer/BUILD.gn +++ b/services/distributedhardwarefwkserviceimpl/test/fuzztest/componentmanager_fuzzer/BUILD.gn @@ -27,9 +27,11 @@ ohos_fuzztest("ComponentmanagerFuzzTest") { "${utils_path}/include/log", "${services_path}/distributedhardwarefwkserviceimpl/include", "${services_path}/distributedhardwarefwkserviceimpl/include/componentmanager", + "${services_path}/distributedhardwarefwkserviceimpl/include/resourcemanager", "${services_path}/distributedhardwarefwkserviceimpl/include/utils", "${common_path}/utils/include", "${common_path}/log/include", + "//third_party/json/include", "//utils/native/base/include", ] cflags = [ diff --git a/services/distributedhardwarefwkserviceimpl/test/unittest/common/BUILD.gn b/services/distributedhardwarefwkserviceimpl/test/unittest/common/BUILD.gn index 035bc692c97122a1b27131aba15e3045a477119d..ae61b46c0e2ba6fe92ab7c8b4c99eaf554c187d3 100644 --- a/services/distributedhardwarefwkserviceimpl/test/unittest/common/BUILD.gn +++ b/services/distributedhardwarefwkserviceimpl/test/unittest/common/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. +# Copyright (c) 2021-2022 Huawei Device Co., Ltd. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at @@ -29,6 +29,7 @@ config("module_private_config") { "${services_path}/distributedhardwarefwkserviceimpl/include", "${services_path}/distributedhardwarefwkserviceimpl/include/task", "${services_path}/distributedhardwarefwkserviceimpl/include/utils", + "//third_party/json/include", "//utils/native/base/include", ] } diff --git a/services/distributedhardwarefwkserviceimpl/test/unittest/common/componentloader/BUILD.gn b/services/distributedhardwarefwkserviceimpl/test/unittest/common/componentloader/BUILD.gn index e83d680cff7148867f9c23ea1722c695849dd476..62b22927d1c9bbdfdde7cdded4461ac6c6310d36 100644 --- a/services/distributedhardwarefwkserviceimpl/test/unittest/common/componentloader/BUILD.gn +++ b/services/distributedhardwarefwkserviceimpl/test/unittest/common/componentloader/BUILD.gn @@ -22,13 +22,16 @@ config("module_private_config") { include_dirs = [ "include", "${utils_path}/include", + "${utils_path}/include/eventbus", "${utils_path}/include/log", "${services_path}/distributedhardwarefwkserviceimpl/include", "${services_path}/distributedhardwarefwkserviceimpl/include/versionmanager", + "${services_path}/distributedhardwarefwkserviceimpl/include/resourcemanager", "${services_path}/distributedhardwarefwkserviceimpl/include/componentloader", "${services_path}/distributedhardwarefwkserviceimpl/include/utils", "${common_path}/utils/include", "${common_path}/log/include", + "//third_party/json/include", ] } @@ -44,7 +47,11 @@ ohos_unittest("ComponentLoaderTest") { "//third_party/googletest:gtest_main", ] - external_deps = [ "hisysevent_native:libhisysevent" ] + external_deps = [ + "distributeddatamgr:distributeddata_inner", + "eventhandler:libeventhandler", + "hisysevent_native:libhisysevent", + ] defines = [ "HI_LOG_ENABLE", diff --git a/services/distributedhardwarefwkserviceimpl/test/unittest/common/componentmanager/BUILD.gn b/services/distributedhardwarefwkserviceimpl/test/unittest/common/componentmanager/BUILD.gn index f37e19bdc2fd2da700f7b2872bed6e3347a433cb..2c886df0787de7febaff41091211d8b3ea7c361a 100644 --- a/services/distributedhardwarefwkserviceimpl/test/unittest/common/componentmanager/BUILD.gn +++ b/services/distributedhardwarefwkserviceimpl/test/unittest/common/componentmanager/BUILD.gn @@ -25,9 +25,11 @@ config("module_private_config") { "${utils_path}/include/log", "${services_path}/distributedhardwarefwkserviceimpl/include", "${services_path}/distributedhardwarefwkserviceimpl/include/componentmanager", + "${services_path}/distributedhardwarefwkserviceimpl/include/resourcemanager", "${services_path}/distributedhardwarefwkserviceimpl/include/utils", "${common_path}/utils/include", "${common_path}/log/include", + "//third_party/json/include", "//utils/native/base/include", ] } diff --git a/services/distributedhardwarefwkserviceimpl/test/unittest/common/task/BUILD.gn b/services/distributedhardwarefwkserviceimpl/test/unittest/common/task/BUILD.gn index bbddc9ad8a451a49fdf8bfba0fcf1c4f8bb1ef1c..2c9012a59cd8d45ee3cef70ed244fdb45fd5774c 100644 --- a/services/distributedhardwarefwkserviceimpl/test/unittest/common/task/BUILD.gn +++ b/services/distributedhardwarefwkserviceimpl/test/unittest/common/task/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. +# Copyright (c) 2021-2022 Huawei Device Co., Ltd. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at @@ -21,6 +21,7 @@ config("module_private_config") { visibility = [ ":*" ] include_dirs = [ "include", + "//third_party/json/include", "${utils_path}/include", "${utils_path}/include/log", "${services_path}/distributedhardwarefwkserviceimpl/include", diff --git a/services/distributedhardwarefwkserviceimpl/test/unittest/common/versionmanager/BUILD.gn b/services/distributedhardwarefwkserviceimpl/test/unittest/common/versionmanager/BUILD.gn index 60f92bc841ef6708d23e55676944542d4fb7c442..0d51813930d8505648273ad5ee38ab8c13406a92 100644 --- a/services/distributedhardwarefwkserviceimpl/test/unittest/common/versionmanager/BUILD.gn +++ b/services/distributedhardwarefwkserviceimpl/test/unittest/common/versionmanager/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. +# Copyright (c) 2021-2022 Huawei Device Co., Ltd. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at @@ -22,13 +22,16 @@ config("module_private_config") { include_dirs = [ "include", "${utils_path}/include", + "${utils_path}/include/eventbus", "${utils_path}/include/log", "${services_path}/distributedhardwarefwkserviceimpl/include", "${services_path}/distributedhardwarefwkserviceimpl/include/componentloader", + "${services_path}/distributedhardwarefwkserviceimpl/include/resourcemanager", "${services_path}/distributedhardwarefwkserviceimpl/include/versionmanager", "${services_path}/distributedhardwarefwkserviceimpl/include/utils", "${common_path}/utils/include", "${common_path}/log/include", + "//third_party/json/include", "//utils/native/base/include", ] } @@ -45,6 +48,11 @@ ohos_unittest("VersionManagerTest") { "//third_party/googletest:gtest_main", ] + external_deps = [ + "distributeddatamgr:distributeddata_inner", + "eventhandler:libeventhandler", + ] + defines = [ "HI_LOG_ENABLE", "DH_LOG_TAG=\"VersionManagerTest\"",