• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "distributed_hardware_manager_factory.h"
17 
18 #include <dlfcn.h>
19 #include <memory>
20 
21 #include "distributed_hardware_errno.h"
22 #include "distributed_hardware_log.h"
23 
24 namespace OHOS {
25 namespace DistributedHardware {
26 const char* LIB_NAME = "libdistributedhardwarefwksvr_impl.z.so";
27 const std::string FUNC_GET_INSTANCE = "GetDistributedHardwareManagerInstance";
28 
29 #undef DH_LOG_TAG
30 #define DH_LOG_TAG "DistributedHardwareManagerFactory"
31 
32 using GetMgrFunc = IDistributedHardwareManager *(*)();
33 
34 IMPLEMENT_SINGLE_INSTANCE(DistributedHardwareManagerFactory);
35 
Init()36 bool DistributedHardwareManagerFactory::Init()
37 {
38     DHLOGI("start");
39 
40     auto loadResult = LoadLibrary();
41     if (loadResult != DH_FWK_SUCCESS) {
42         DHLOGE("LoadLibrary failed, errCode = %d", loadResult);
43         return false;
44     }
45 
46     auto initResult = distributedHardwareMgrPtr_->Initialize();
47     if (initResult != DH_FWK_SUCCESS) {
48         DHLOGE("Initialize failed, errCode = %d", initResult);
49         return false;
50     }
51     DHLOGD("success");
52 
53     return true;
54 }
55 
UnInit()56 void DistributedHardwareManagerFactory::UnInit()
57 {
58     DHLOGI("start");
59 
60     // release all the resources synchronously
61     distributedHardwareMgrPtr_->Release();
62 
63     CloseLibrary();
64     DHLOGD("success");
65 }
66 
IsInit()67 bool DistributedHardwareManagerFactory::IsInit()
68 {
69     std::lock_guard<std::mutex> lock(mutex_);
70     if (distributedHardwareMgrPtr_ == nullptr) {
71         DHLOGE("distributedHardwareMgr is not Initialize");
72         return false;
73     }
74     return true;
75 }
76 
SendOnLineEvent(const std::string & networkId,const std::string & uuid,uint16_t deviceType)77 int32_t DistributedHardwareManagerFactory::SendOnLineEvent(const std::string &networkId, const std::string &uuid,
78     uint16_t deviceType)
79 {
80     if (networkId.empty()) {
81         DHLOGE("networkId is empty");
82         return ERR_DH_FWK_REMOTE_NETWORK_ID_IS_EMPTY;
83     }
84 
85     if (uuid.empty()) {
86         DHLOGE("uuid is empty");
87         return ERR_DH_FWK_REMOTE_DEVICE_ID_IS_EMPTY;
88     }
89 
90     std::lock_guard<std::mutex> lock(mutex_);
91     if (distributedHardwareMgrPtr_ == nullptr && !Init()) {
92         DHLOGE("distributedHardwareMgr is null");
93         return ERR_DH_FWK_HARDWARE_MANAGER_LOAD_IMPL_FAILED;
94     }
95 
96     auto onlineResult = distributedHardwareMgrPtr_->SendOnLineEvent(networkId, uuid, deviceType);
97     if (onlineResult != DH_FWK_SUCCESS) {
98         DHLOGE("online failed, errCode = %d", onlineResult);
99         return onlineResult;
100     }
101     return DH_FWK_SUCCESS;
102 }
103 
SendOffLineEvent(const std::string & networkId,const std::string & uuid,uint16_t deviceType)104 int32_t DistributedHardwareManagerFactory::SendOffLineEvent(const std::string &networkId, const std::string &uuid,
105     uint16_t deviceType)
106 {
107     if (networkId.empty()) {
108         DHLOGE("networkId is empty");
109         return ERR_DH_FWK_REMOTE_NETWORK_ID_IS_EMPTY;
110     }
111 
112     std::lock_guard<std::mutex> lock(mutex_);
113     if (distributedHardwareMgrPtr_ == nullptr) {
114         DHLOGE("distributedHardwareMgr is null");
115         return ERR_DH_FWK_HARDWARE_MANAGER_LOAD_IMPL_FAILED;
116     }
117 
118     auto offlineResult = distributedHardwareMgrPtr_->SendOffLineEvent(networkId, uuid, deviceType);
119     if (offlineResult != DH_FWK_SUCCESS) {
120         DHLOGE("offline failed, errCode = %d", offlineResult);
121         return offlineResult;
122     }
123 
124     if (distributedHardwareMgrPtr_->GetOnLineCount() == 0) {
125         DHLOGI("all devices are offline, start to free the resource");
126         UnInit();
127     }
128     return DH_FWK_SUCCESS;
129 }
130 
LoadLibrary()131 int32_t DistributedHardwareManagerFactory::LoadLibrary()
132 {
133     DHLOGI("start.");
134     if (handler_ != nullptr && distributedHardwareMgrPtr_ != nullptr) {
135         DHLOGE("DistributedHardwareManager handler has loaded.");
136         return DH_FWK_SUCCESS;
137     }
138 
139     handler_ = dlopen(LIB_NAME, RTLD_NOW | RTLD_NODELETE);
140     if (handler_ == nullptr) {
141         DHLOGE("open %s failed.", LIB_NAME);
142         return ERR_DH_FWK_HARDWARE_MANAGER_LIB_IMPL_OPEN_FAILED;
143     }
144 
145     auto getManager = reinterpret_cast<GetMgrFunc>(dlsym(handler_, FUNC_GET_INSTANCE.c_str()));
146     if (getManager == nullptr) {
147         DHLOGE("can not find %s.", FUNC_GET_INSTANCE.c_str());
148         CloseLibrary();
149         return ERR_DH_FWK_HARDWARE_MANAGER_LIB_IMPL_GET_INSTANCE_FAILED;
150     }
151 
152     distributedHardwareMgrPtr_ = getManager();
153     if (distributedHardwareMgrPtr_ == nullptr) {
154         DHLOGE("distributedHardwareMgrPtr is null.");
155         CloseLibrary();
156         return ERR_DH_FWK_HARDWARE_MANAGER_LIB_IMPL_IS_NULL;
157     }
158     DHLOGI("load %s success.", LIB_NAME);
159     return DH_FWK_SUCCESS;
160 }
161 
CloseLibrary()162 void DistributedHardwareManagerFactory::CloseLibrary()
163 {
164     if (handler_ == nullptr) {
165         DHLOGI("%s is already closed.", LIB_NAME);
166         return;
167     }
168     distributedHardwareMgrPtr_ = nullptr;
169     dlclose(handler_);
170     handler_ = nullptr;
171     DHLOGI("%s is closed.", LIB_NAME);
172 }
173 
GetComponentVersion(std::unordered_map<DHType,std::string> & versionMap)174 int32_t DistributedHardwareManagerFactory::GetComponentVersion(std::unordered_map<DHType, std::string> &versionMap)
175 {
176     DHLOGI("start");
177     std::lock_guard<std::mutex> lock(mutex_);
178     if (distributedHardwareMgrPtr_ == nullptr) {
179         DHLOGE("distributedHardwareMgr is null");
180         return ERR_DH_FWK_HARDWARE_MANAGER_LIB_IMPL_IS_NULL;
181     }
182     return distributedHardwareMgrPtr_->GetComponentVersion(versionMap);
183 }
184 }
185 }
186