• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 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     std::lock_guard<std::mutex> lock(versionMutex_);
50     dhVersions_.clear();
51 }
52 
ShowLocalVersion(const DHVersion & dhVersion) const53 void VersionManager::ShowLocalVersion(const DHVersion &dhVersion) const
54 {
55     for (const auto &item : dhVersion.compVersions) {
56         DHLOGI("LocalDHVersion = %{public}s, DHtype = %{public}#X, handlerVersion = %{public}s, "
57             "sourceVersion = %{public}s, sinkVersion = %{public}s",
58             dhVersion.dhVersion.c_str(), item.first, item.second.handlerVersion.c_str(),
59             item.second.sourceVersion.c_str(), item.second.sinkVersion.c_str());
60     }
61 }
62 
AddDHVersion(const std::string & uuid,const DHVersion & dhVersion)63 int32_t VersionManager::AddDHVersion(const std::string &uuid, const DHVersion &dhVersion)
64 {
65     if (!IsIdLengthValid(uuid)) {
66         return ERR_DH_FWK_PARA_INVALID;
67     }
68     DHLOGI("addDHVersion uuid: %{public}s", GetAnonyString(uuid).c_str());
69     std::lock_guard<std::mutex> lock(versionMutex_);
70     dhVersions_[uuid] = dhVersion;
71     return DH_FWK_SUCCESS;
72 }
73 
RemoveDHVersion(const std::string & uuid)74 int32_t VersionManager::RemoveDHVersion(const std::string &uuid)
75 {
76     if (!IsIdLengthValid(uuid)) {
77         return ERR_DH_FWK_PARA_INVALID;
78     }
79     DHLOGI("uuid: %{public}s", GetAnonyString(uuid).c_str());
80     std::lock_guard<std::mutex> lock(versionMutex_);
81     std::unordered_map<std::string, DHVersion>::iterator iter = dhVersions_.find(uuid);
82     if (iter == dhVersions_.end()) {
83         DHLOGE("there is no uuid: %{public}s, remove fail", GetAnonyString(uuid).c_str());
84         return ERR_DH_FWK_VERSION_DEVICE_ID_NOT_EXIST;
85     }
86     dhVersions_.erase(iter);
87     return DH_FWK_SUCCESS;
88 }
89 
GetDHVersion(const std::string & uuid,DHVersion & dhVersion)90 int32_t VersionManager::GetDHVersion(const std::string &uuid, DHVersion &dhVersion)
91 {
92     if (!IsIdLengthValid(uuid)) {
93         return ERR_DH_FWK_PARA_INVALID;
94     }
95     DHLOGI("uuid: %{public}s", GetAnonyString(uuid).c_str());
96     std::lock_guard<std::mutex> lock(versionMutex_);
97     std::unordered_map<std::string, DHVersion>::iterator iter = dhVersions_.find(uuid);
98     if (iter == dhVersions_.end()) {
99         DHLOGE("there is no uuid: %{public}s, get version fail", GetAnonyString(uuid).c_str());
100         return ERR_DH_FWK_VERSION_DEVICE_ID_NOT_EXIST;
101     } else {
102         dhVersion = dhVersions_[uuid];
103         return DH_FWK_SUCCESS;
104     }
105 }
106 
GetCompVersion(const std::string & uuid,const DHType dhType,CompVersion & compVersion)107 int32_t VersionManager::GetCompVersion(const std::string &uuid, const DHType dhType, CompVersion &compVersion)
108 {
109     if (!IsIdLengthValid(uuid)) {
110         return ERR_DH_FWK_PARA_INVALID;
111     }
112     DHVersion dhVersion;
113     int32_t ret = GetDHVersion(uuid, dhVersion);
114     if (ret != DH_FWK_SUCCESS) {
115         DHLOGE("GetDHVersion fail, uuid: %{public}s", GetAnonyString(uuid).c_str());
116         return ret;
117     }
118     if (dhVersion.compVersions.find(dhType) == dhVersion.compVersions.end()) {
119         DHLOGE("not find dhType: %{public}#X", dhType);
120         return ERR_DH_FWK_TYPE_NOT_EXIST;
121     }
122 
123     DHLOGI("GetCompVersion success, uuid: %{public}s, dhType: %{public}#X", GetAnonyString(uuid).c_str(), dhType);
124     compVersion = dhVersion.compVersions[dhType];
125     return DH_FWK_SUCCESS;
126 }
127 
GetLocalDeviceVersion()128 std::string VersionManager::GetLocalDeviceVersion()
129 {
130     return DH_LOCAL_VERSION;
131 }
132 } // namespace DistributedHardware
133 } // namespace OHOS
134