1 /*
2  * Copyright (C) 2023 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 <dlfcn.h>
17 #include <iostream>
18 #include <string>
19 
20 #include "distributed_call_proxy.h"
21 #include "telephony_log_wrapper.h"
22 #include "telephony_errors.h"
23 
24 namespace OHOS {
25 namespace Telephony {
26 namespace {
27 using GetDCallClientClass = OHOS::DistributedHardware::IDCallClient *(*)();
28 const std::string DCALL_CLIENT_SDK_PATH = "/system/lib64/libdistributed_call_client.z.so";
29 }
30 
DistributedCallProxy()31 DistributedCallProxy::DistributedCallProxy()
32 {
33     TELEPHONY_LOGI("DistributedCallProxy constructed.");
34     dCallClientHandler_ = dlopen(DCALL_CLIENT_SDK_PATH.c_str(), RTLD_LAZY | RTLD_NODELETE);
35     if (dCallClientHandler_ == nullptr) {
36         TELEPHONY_LOGE("load dcall client sdk failed, fail reason: %{public}s.", dlerror());
37         return;
38     }
39     TELEPHONY_LOGI("load dcall client sdk succeed.");
40 }
41 
~DistributedCallProxy()42 DistributedCallProxy::~DistributedCallProxy()
43 {
44     TELEPHONY_LOGI("DistributedCallProxy destructed.");
45     dcallClient_ = nullptr;
46     if (dCallClientHandler_ != nullptr) {
47         if (dlclose(dCallClientHandler_) != 0) {
48             TELEPHONY_LOGE("unload dcall client sdk failed.");
49             return;
50         }
51         dCallClientHandler_ = nullptr;
52         TELEPHONY_LOGI("unload dcall client sdk succeed.");
53     }
54 }
55 
GetDCallClient()56 OHOS::DistributedHardware::IDCallClient* DistributedCallProxy::GetDCallClient()
57 {
58     TELEPHONY_LOGI("get dcall client.");
59     if (dCallClientHandler_ == nullptr) {
60         TELEPHONY_LOGE("dcall client sdk not loaded.");
61         return nullptr;
62     }
63     GetDCallClientClass getDCallClientClass = (GetDCallClientClass)dlsym(dCallClientHandler_,
64         OHOS::DistributedHardware::DCALL_LOADER_GET_DCALL_CLIENT_HANDLER.c_str());
65     if (getDCallClientClass == nullptr) {
66         TELEPHONY_LOGE("get dcall client class handler is null, failed reason: %{public}s.", dlerror());
67         dlclose(dCallClientHandler_);
68         dCallClientHandler_ = nullptr;
69         return nullptr;
70     }
71     OHOS::DistributedHardware::IDCallClient *dcallClient = getDCallClientClass();
72     if (dcallClient == nullptr) {
73         TELEPHONY_LOGE("fail to get dcall client.");
74     } else {
75         TELEPHONY_LOGI("get dcall client success.");
76     }
77     return dcallClient;
78 }
79 
Init()80 int32_t DistributedCallProxy::Init()
81 {
82     std::lock_guard<std::mutex> lock(dcallClientMtx_);
83     if (dCallClientHandler_ == nullptr) {
84         TELEPHONY_LOGE("fail to load dcall sdk");
85         return TELEPHONY_ERR_FAIL;
86     }
87     dcallClient_ = GetDCallClient();
88     if (dcallClient_ == nullptr) {
89         TELEPHONY_LOGE("fail to get dcall client");
90         return TELEPHONY_ERR_FAIL;
91     }
92     if (dcallClient_->Init() != TELEPHONY_SUCCESS) {
93         TELEPHONY_LOGE("init dcall client failed");
94         return TELEPHONY_ERR_FAIL;
95     }
96     return TELEPHONY_SUCCESS;
97 }
98 
UnInit()99 int32_t DistributedCallProxy::UnInit()
100 {
101     std::lock_guard<std::mutex> lock(dcallClientMtx_);
102     if (dcallClient_ == nullptr) {
103         TELEPHONY_LOGE("dcallClient_ is nullptr");
104         return TELEPHONY_ERR_FAIL;
105     }
106     return dcallClient_->UnInit();
107 }
108 
SwitchDevice(const std::string & devId,int32_t flag)109 int32_t DistributedCallProxy::SwitchDevice(const std::string& devId, int32_t flag)
110 {
111     std::lock_guard<std::mutex> lock(dcallClientMtx_);
112     if (dcallClient_ == nullptr) {
113         TELEPHONY_LOGE("dcallClient_ is nullptr");
114         return TELEPHONY_ERR_FAIL;
115     }
116     return dcallClient_->SwitchDevice(devId, flag);
117 }
118 
GetOnlineDeviceList(std::vector<std::string> & devList)119 int32_t DistributedCallProxy::GetOnlineDeviceList(std::vector<std::string>& devList)
120 {
121     std::lock_guard<std::mutex> lock(dcallClientMtx_);
122     if (dcallClient_ == nullptr) {
123         TELEPHONY_LOGE("dcallClient_ is nullptr");
124         return TELEPHONY_ERR_FAIL;
125     }
126     return dcallClient_->GetOnlineDeviceList(devList);
127 }
128 
RegisterDeviceCallback(const std::string name,const std::shared_ptr<OHOS::DistributedHardware::IDCallDeviceCallback> & callback)129 int32_t DistributedCallProxy::RegisterDeviceCallback(const std::string name,
130     const std::shared_ptr<OHOS::DistributedHardware::IDCallDeviceCallback>& callback)
131 {
132     std::lock_guard<std::mutex> lock(dcallClientMtx_);
133     if (dcallClient_ == nullptr) {
134         TELEPHONY_LOGE("dcallClient_ is nullptr");
135         return TELEPHONY_ERR_FAIL;
136     }
137     return dcallClient_->RegisterDeviceCallback(name, callback);
138 }
139 
UnRegisterDeviceCallback(const std::string & name)140 int32_t DistributedCallProxy::UnRegisterDeviceCallback(const std::string& name)
141 {
142     std::lock_guard<std::mutex> lock(dcallClientMtx_);
143     if (dcallClient_ == nullptr) {
144         TELEPHONY_LOGE("dcallClient_ is nullptr");
145         return TELEPHONY_ERR_FAIL;
146     }
147     return dcallClient_->UnRegisterDeviceCallback(name);
148 }
149 
GetDCallDeviceInfo(const std::string & devId,OHOS::DistributedHardware::DCallDeviceInfo & devInfo)150 int32_t DistributedCallProxy::GetDCallDeviceInfo(const std::string &devId,
151     OHOS::DistributedHardware::DCallDeviceInfo& devInfo)
152 {
153     std::lock_guard<std::mutex> lock(dcallClientMtx_);
154     if (dcallClient_ == nullptr) {
155         TELEPHONY_LOGE("dcallClient_ is nullptr");
156         return TELEPHONY_ERR_FAIL;
157     }
158     return dcallClient_->GetDCallDeviceInfo(devId, devInfo);
159 }
160 } // namespace Telephony
161 } // namespace OHOS
162