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