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