1 /*
2 * Copyright (c) 2021-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 "version_manager.h"
17
18 #include "anonymous_string.h"
19 #include "component_loader.h"
20 #include "dh_context.h"
21 #include "dh_utils_tool.h"
22 #include "distributed_hardware_log.h"
23
24 namespace OHOS {
25 namespace DistributedHardware {
26 #undef DH_LOG_TAG
27 #define DH_LOG_TAG "VersionManager"
28
29 const std::string DH_LOCAL_VERSION = "1.0";
30
31 IMPLEMENT_SINGLE_INSTANCE(VersionManager);
32
Init()33 int32_t VersionManager::Init()
34 {
35 DHLOGI("start");
36 DHVersion dhVersion;
37 int32_t ret = ComponentLoader::GetInstance().GetLocalDHVersion(dhVersion);
38 if (ret != DH_FWK_SUCCESS) {
39 DHLOGE("GetLocalDHVersion fail");
40 return ret;
41 }
42 dhVersion.dhVersion = GetLocalDeviceVersion();
43 ShowLocalVersion(dhVersion);
44 std::string strUUID = DHContext::GetInstance().GetDeviceInfo().uuid;
45 AddDHVersion(strUUID, dhVersion);
46 return DH_FWK_SUCCESS;
47 }
48
UnInit()49 void VersionManager::UnInit()
50 {
51 DHLOGI("start");
52 std::lock_guard<std::mutex> lock(versionMutex_);
53 dhVersions_.clear();
54 }
55
ShowLocalVersion(const DHVersion & dhVersion) const56 void VersionManager::ShowLocalVersion(const DHVersion &dhVersion) const
57 {
58 for (const auto &item : dhVersion.compVersions) {
59 DHLOGI("LocalDHVersion = %{public}s, DHtype = %{public}#X, handlerVersion = %{public}s, "
60 "sourceVersion = %{public}s, sinkVersion = %{public}s",
61 dhVersion.dhVersion.c_str(), item.first, item.second.handlerVersion.c_str(),
62 item.second.sourceVersion.c_str(), item.second.sinkVersion.c_str());
63 }
64 }
65
AddDHVersion(const std::string & uuid,const DHVersion & dhVersion)66 int32_t VersionManager::AddDHVersion(const std::string &uuid, const DHVersion &dhVersion)
67 {
68 if (!IsIdLengthValid(uuid)) {
69 return ERR_DH_FWK_PARA_INVALID;
70 }
71 DHLOGI("addDHVersion uuid: %{public}s", GetAnonyString(uuid).c_str());
72 std::lock_guard<std::mutex> lock(versionMutex_);
73 dhVersions_[uuid] = dhVersion;
74 return DH_FWK_SUCCESS;
75 }
76
RemoveDHVersion(const std::string & uuid)77 int32_t VersionManager::RemoveDHVersion(const std::string &uuid)
78 {
79 if (!IsIdLengthValid(uuid)) {
80 return ERR_DH_FWK_PARA_INVALID;
81 }
82 DHLOGI("uuid: %{public}s", GetAnonyString(uuid).c_str());
83 std::lock_guard<std::mutex> lock(versionMutex_);
84 std::unordered_map<std::string, DHVersion>::iterator iter = dhVersions_.find(uuid);
85 if (iter == dhVersions_.end()) {
86 DHLOGE("there is no uuid: %{public}s, remove fail", GetAnonyString(uuid).c_str());
87 return ERR_DH_FWK_VERSION_DEVICE_ID_NOT_EXIST;
88 }
89 dhVersions_.erase(iter);
90 return DH_FWK_SUCCESS;
91 }
92
GetDHVersion(const std::string & uuid,DHVersion & dhVersion)93 int32_t VersionManager::GetDHVersion(const std::string &uuid, DHVersion &dhVersion)
94 {
95 if (!IsIdLengthValid(uuid)) {
96 return ERR_DH_FWK_PARA_INVALID;
97 }
98 DHLOGI("uuid: %{public}s", GetAnonyString(uuid).c_str());
99 std::lock_guard<std::mutex> lock(versionMutex_);
100 std::unordered_map<std::string, DHVersion>::iterator iter = dhVersions_.find(uuid);
101 if (iter == dhVersions_.end()) {
102 DHLOGE("there is no uuid: %{public}s, get version fail", GetAnonyString(uuid).c_str());
103 return ERR_DH_FWK_VERSION_DEVICE_ID_NOT_EXIST;
104 } else {
105 dhVersion = dhVersions_[uuid];
106 return DH_FWK_SUCCESS;
107 }
108 }
109
GetCompVersion(const std::string & uuid,const DHType dhType,CompVersion & compVersion)110 int32_t VersionManager::GetCompVersion(const std::string &uuid, const DHType dhType, CompVersion &compVersion)
111 {
112 if (!IsIdLengthValid(uuid)) {
113 return ERR_DH_FWK_PARA_INVALID;
114 }
115 DHVersion dhVersion;
116 int32_t ret = GetDHVersion(uuid, dhVersion);
117 if (ret != DH_FWK_SUCCESS) {
118 DHLOGE("GetDHVersion fail, uuid: %{public}s", GetAnonyString(uuid).c_str());
119 return ret;
120 }
121 if (dhVersion.compVersions.find(dhType) == dhVersion.compVersions.end()) {
122 DHLOGE("not find dhType: %{public}#X", dhType);
123 return ERR_DH_FWK_TYPE_NOT_EXIST;
124 }
125
126 DHLOGI("GetCompVersion success, uuid: %{public}s, dhType: %{public}#X", GetAnonyString(uuid).c_str(), dhType);
127 compVersion = dhVersion.compVersions[dhType];
128 return DH_FWK_SUCCESS;
129 }
130
GetLocalDeviceVersion()131 std::string VersionManager::GetLocalDeviceVersion()
132 {
133 return DH_LOCAL_VERSION;
134 }
135 } // namespace DistributedHardware
136 } // namespace OHOS
137