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