1 /*
2 * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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 "wfd_source_impl.h"
17 #include "common/common_macro.h"
18 #include "interaction/interprocess/client_factory.h"
19
20 namespace OHOS {
21 namespace Sharing {
22
CreateSource(int32_t type,const std::string key)23 std::shared_ptr<WfdSource> OHOS::Sharing::WfdSourceFactory::CreateSource(int32_t type, const std::string key)
24 {
25 SHARING_LOGD("trace.");
26 auto client = std::static_pointer_cast<InterIpcClient>(
27 ClientFactory::GetInstance().CreateClient(key, "WfdSourceImpl", "WfdSourceScene"));
28 if (client == nullptr) {
29 SHARING_LOGE("failed to get client.");
30 return nullptr;
31 }
32
33 auto adapter = client->GetMsgAdapter();
34 if (adapter == nullptr) {
35 SHARING_LOGE("failed to get adapter.");
36 }
37
38 std::shared_ptr<WfdSourceImpl> impl = std::make_shared<WfdSourceImpl>();
39 if (impl == nullptr) {
40 SHARING_LOGE("failed to new WfdSourceImpl.");
41 return nullptr;
42 }
43
44 impl->SetIpcClient(client);
45 impl->SetIpcAdapter(adapter);
46
47 return impl;
48 }
49
WfdSourceImpl()50 WfdSourceImpl::WfdSourceImpl()
51 {
52 SHARING_LOGD("trace.");
53 }
54
~WfdSourceImpl()55 WfdSourceImpl::~WfdSourceImpl()
56 {
57 SHARING_LOGD("trace.");
58 }
59
StartDiscover()60 int32_t WfdSourceImpl::StartDiscover()
61 {
62 SHARING_LOGD("trace.");
63 auto ipcAdapter = ipcAdapter_.lock();
64 RETURN_INVALID_IF_NULL(ipcAdapter);
65 auto msg = std::make_shared<WfdSourceStartDiscoveryReq>();
66 auto reply = std::static_pointer_cast<BaseMsg>(std::make_shared<WfdCommonRsp>());
67 auto ret = ipcAdapter->SendRequest(msg, reply);
68 if (ret != 0) {
69 SHARING_LOGE("ipc sendRequest failed: %{public}d.", ret);
70 return ret;
71 }
72
73 auto replyMsg = std::static_pointer_cast<WfdCommonRsp>(reply);
74 return replyMsg->ret;
75 }
76
StopDiscover()77 int32_t WfdSourceImpl::StopDiscover()
78 {
79 SHARING_LOGD("trace.");
80 auto ipcAdapter = ipcAdapter_.lock();
81 RETURN_INVALID_IF_NULL(ipcAdapter);
82
83 auto msgStopDiscoveryReq = std::make_shared<WfdSourceStopDiscoveryReq>();
84 auto replyStopDiscoveryReq = std::static_pointer_cast<BaseMsg>(std::make_shared<WfdCommonRsp>());
85
86 auto retStopDiscoveryReq = ipcAdapter->SendRequest(msgStopDiscoveryReq, replyStopDiscoveryReq);
87 if (retStopDiscoveryReq != 0) {
88 SHARING_LOGE("ipc sendRequest failed: %{public}d.", retStopDiscoveryReq);
89 return retStopDiscoveryReq;
90 }
91
92 auto replyMsg = std::static_pointer_cast<WfdCommonRsp>(replyStopDiscoveryReq);
93 return replyMsg->ret;
94 }
95
AddDevice(uint64_t screenId,WfdCastDeviceInfo & deviceInfo)96 int32_t WfdSourceImpl::AddDevice(uint64_t screenId, WfdCastDeviceInfo &deviceInfo)
97 {
98 SHARING_LOGD("Add deviceId: %{public}s, screenId: %{public}" PRIx64 ".", deviceInfo.deviceId.c_str(), screenId);
99 std::lock_guard<std::mutex> lock(mutex_);
100 if (deviceAdded_) {
101 return 0;
102 }
103
104 auto ipcAdapter = ipcAdapter_.lock();
105 RETURN_INVALID_IF_NULL(ipcAdapter);
106
107 auto msg = std::make_shared<WfdSourceAddDeviceReq>();
108 msg->deviceId = deviceInfo.deviceId;
109 msg->screenId = screenId;
110 auto reply = std::static_pointer_cast<BaseMsg>(std::make_shared<WfdCommonRsp>());
111
112 auto ret = ipcAdapter->SendRequest(msg, reply);
113 if (ret != 0) {
114 SHARING_LOGE("ipc sendRequest failed: %{public}d.", ret);
115 } else {
116 deviceAdded_ = true;
117 }
118 return ret;
119 }
120
RemoveDevice(std::string deviceId)121 int32_t WfdSourceImpl::RemoveDevice(std::string deviceId)
122 {
123 SHARING_LOGD("device: %{public}s.", deviceId.c_str());
124 std::lock_guard<std::mutex> lock(mutex_);
125 if (!deviceAdded_) {
126 return 0;
127 }
128 auto ipcAdapter = ipcAdapter_.lock();
129 RETURN_INVALID_IF_NULL(ipcAdapter);
130
131 auto msg = std::make_shared<WfdSourceRemoveDeviceReq>();
132 msg->deviceId = deviceId;
133 auto reply = std::static_pointer_cast<BaseMsg>(std::make_shared<WfdCommonRsp>());
134
135 auto ret = ipcAdapter->SendRequest(msg, reply);
136 if (ret != 0) {
137 SHARING_LOGE("ipc sendRequest failed: %{public}d.", ret);
138 return ret;
139 } else {
140 deviceAdded_ = false;
141 }
142
143 auto replyMsg = std::static_pointer_cast<WfdCommonRsp>(reply);
144 return replyMsg->ret;
145 }
146
SetListener(const std::shared_ptr<IWfdEventListener> & callback)147 int32_t WfdSourceImpl::SetListener(const std::shared_ptr<IWfdEventListener> &callback)
148 {
149 SHARING_LOGD("trace.");
150 RETURN_INVALID_IF_NULL(callback);
151
152 listener_ = callback;
153 return 0;
154 }
155
OnRequest(std::shared_ptr<BaseMsg> msg,std::shared_ptr<BaseMsg> & reply)156 void WfdSourceImpl::OnRequest(std::shared_ptr<BaseMsg> msg, std::shared_ptr<BaseMsg> &reply)
157 {
158 SHARING_LOGD("trace.");
159 RETURN_IF_NULL(msg);
160
161 SHARING_LOGD("Recv msg: %{public}d.", msg->GetMsgId());
162 if (auto listener = listener_.lock()) {
163 listener->OnInfo(msg);
164 } else {
165 SHARING_LOGW("no listener provided by napi.");
166 }
167 }
168
OnRemoteDied()169 void WfdSourceImpl::OnRemoteDied()
170 {
171 SHARING_LOGD("trace.");
172 auto msg = std::make_shared<WfdErrorMsg>();
173 msg->errorCode = ERR_REMOTE_DIED;
174 msg->message = "OnRemoteDied.";
175
176 if (auto listener = listener_.lock()) {
177 std::shared_ptr<BaseMsg> baseMsg = std::static_pointer_cast<BaseMsg>(msg);
178 listener->OnInfo(baseMsg);
179 } else {
180 SHARING_LOGW("no listener provided by napi.");
181 }
182 }
183
184 } // namespace Sharing
185 } // namespace OHOS
186