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