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_demo.h"
17 #include <iostream>
18 #include "magic_enum.hpp"
19
20 using namespace OHOS::Sharing;
21
WfdSourceDemo()22 WfdSourceDemo::WfdSourceDemo()
23 {
24 listener_ = std::make_shared<WfdSourceDemoListener>();
25 if (!listener_) {
26 printf("create WfdSourceDemoListener failed.\n");
27 }
28 }
29
GetClient()30 std::shared_ptr<WfdSource> WfdSourceDemo::GetClient()
31 {
32 return client_;
33 }
34
CreateClient()35 bool WfdSourceDemo::CreateClient()
36 {
37 client_ = WfdSourceFactory::CreateSource(0, "wfd_Source_demo");
38 if (!client_) {
39 printf("create wfdSource client error.\n");
40 return false;
41 }
42 if (listener_) {
43 listener_->SetListener(shared_from_this());
44 client_->SetListener(listener_);
45 return true;
46 } else {
47 printf("Listener is nullptr.\n");
48 return false;
49 }
50 }
51
StartDiscover()52 bool WfdSourceDemo::StartDiscover()
53 {
54 if (!client_) {
55 printf("client is null.\n");
56 return false;
57 }
58
59 client_->StartDiscover();
60 return true;
61 }
62
StopDiscover()63 bool WfdSourceDemo::StopDiscover()
64 {
65 if (!client_) {
66 printf("client is null.\n");
67 return false;
68 }
69
70 client_->StopDiscover();
71 return true;
72 }
73
Start()74 bool WfdSourceDemo::Start()
75 {
76 printf("enter start.\n");
77 if (!client_) {
78 printf("client is nullptr.\n");
79 return false;
80 }
81 if (client_->StartDiscover() == -1) {
82 printf("Source start error.\n");
83 return false;
84 }
85 return true;
86 }
87
Stop()88 bool WfdSourceDemo::Stop()
89 {
90 if (client_->StopDiscover() == -1) {
91 printf("Source stop error.\n");
92 return false;
93 }
94 return true;
95 }
96
97 static std::string g_deviceId;
AddCastDevice(const std::string deviceId)98 void WfdSourceDemo::AddCastDevice(const std::string deviceId)
99 {
100 printf("add device: %s.\n", deviceId.c_str());
101 WfdCastDeviceInfo deviceInfo;
102 deviceInfo.deviceId = deviceId;
103 if (client_->AddDevice(0, deviceInfo) == -1) {
104 printf("AddDevice error.\n");
105 }
106 g_deviceId = deviceId;
107 }
108
RemoveCastDevice(const std::string deviceId)109 void WfdSourceDemo::RemoveCastDevice(const std::string deviceId)
110 {
111 printf("remove device: %s.\n", deviceId.c_str());
112 if (client_->RemoveDevice(deviceId) == -1) {
113 printf("RemoveDevice error.\n");
114 }
115 }
116
ExecuteCmd(int32_t cmd,std::string & deviceId)117 void WfdSourceDemo::ExecuteCmd(int32_t cmd, std::string &deviceId)
118 {
119 switch (cmd) {
120 case 1: // 1: start
121 if (Start()) {
122 printf("p2p start success.\n");
123 }
124 break;
125 case 2: // 2: Stop
126 if (Stop()) {
127 printf("p2p stop success.\n");
128 }
129 break;
130 case 3: // 3: StartDiscover
131 if (StartDiscover()) {
132 printf("StartDiscover success.\n");
133 }
134 break;
135 case 4: // 4: StopDiscover
136 if (StopDiscover()) {
137 printf("StartDiscover success.\n");
138 }
139 break;
140 case 5: // 5: AddCastDevice
141 AddCastDevice(deviceId);
142 break;
143 case 6: // 6: RemoveCastDevice
144 RemoveCastDevice(deviceId);
145 break;
146 default:
147 break;
148 }
149 }
150
DoCmd(std::string cmd)151 void WfdSourceDemo::DoCmd(std::string cmd)
152 {
153 const std::map<std::string, int32_t> cmd2index = {{"Start", 1}, {"Stop", 2},
154 {"StartDiscover", 3}, {"StopDiscover", 4},
155 {"AddCastDevice", 5}, {"RemoveCastDevice", 6}};
156
157 if (cmd2index.count(cmd) == 0) {
158 printf("command: %s not found.", cmd.c_str());
159 return;
160 }
161 printf("enter commond, the commond is %s, the id is %d.", cmd.c_str(), cmd2index.at(cmd));
162 std::string input;
163 std::string deviceId;
164 // input params
165 switch (cmd2index.at(cmd)) {
166 case 5: // 5: AddCastDevice
167 case 6: // 6: RemoveCastDevice
168 printf("please input deviceId:\n");
169 getline(std::cin, input);
170 if (input != "") {
171 deviceId = input;
172 }
173 break;
174 default:
175 break;
176 }
177 // execute command
178 ExecuteCmd(cmd2index.at(cmd), deviceId);
179 }
180
OnError(uint32_t regionId,uint32_t agentId,SharingErrorCode errorCode)181 void WfdSourceDemoListener::OnError(uint32_t regionId, uint32_t agentId, SharingErrorCode errorCode)
182 {
183 (void)regionId;
184 (void)agentId;
185 printf("on error. errorCode: %s, %d.", std::string(magic_enum::enum_name(errorCode)).c_str(), errorCode);
186 }
187
OnInfo(std::shared_ptr<BaseMsg> & msg)188 void WfdSourceDemoListener::OnInfo(std::shared_ptr<BaseMsg> &msg)
189 {
190 printf("on info. msgId: %d.\n", msg->GetMsgId());
191 switch (msg->GetMsgId()) {
192 case WfdSourceDeviceFoundMsg::MSG_ID: {
193 auto data = std::static_pointer_cast<WfdSourceDeviceFoundMsg>(msg);
194 auto infos = data->deviceInfos;
195 OnDeviceFound(infos);
196 break;
197 }
198 case WfdConnectionChangedMsg::MSG_ID: {
199 auto data = std::static_pointer_cast<WfdConnectionChangedMsg>(msg);
200 ConnectionInfo info;
201 info.ip = data->ip;
202 info.mac = data->mac;
203 info.state = static_cast<ConnectionState>(data->state);
204 info.surfaceId = data->surfaceId;
205 info.deviceName = data->deviceName;
206 info.primaryDeviceType = data->primaryDeviceType;
207 info.secondaryDeviceType = data->secondaryDeviceType;
208 OnConnectionChanged(info);
209 break;
210 }
211 case WfdErrorMsg::MSG_ID: {
212 auto data = std::static_pointer_cast<WfdErrorMsg>(msg);
213 printf("OnError: %d.\n", data->errorCode);
214 auto listener = listener_.lock();
215 if (!listener) {
216 printf("no listener.\n");
217 }
218 listener->RemoveCastDevice(g_deviceId);
219 break;
220 }
221 default:
222 break;
223 }
224 }
225
OnDeviceFound(const std::vector<WfdCastDeviceInfo> & deviceInfos)226 void WfdSourceDemoListener::OnDeviceFound(const std::vector<WfdCastDeviceInfo> &deviceInfos)
227 {
228 auto listener = listener_.lock();
229 if (!listener) {
230 printf("no listener.\n");
231 }
232
233 for (auto itDev : deviceInfos) {
234 printf("device found: %s\n"
235 "\tmac: %s"
236 "\tprimaryDeviceType: %s"
237 "\tsecondaryDeviceType: %s.\n",
238 itDev.deviceName.c_str(), itDev.deviceId.c_str(), itDev.primaryDeviceType.c_str(),
239 itDev.secondaryDeviceType.c_str());
240 }
241 }
242
OnConnectionChanged(const ConnectionInfo & info)243 void WfdSourceDemoListener::OnConnectionChanged(const ConnectionInfo &info)
244 {
245 auto listener = listener_.lock();
246 if (!listener) {
247 printf("no listener.\n");
248 }
249
250 printf("on OnConnectionChanged. state: %d, ip: %s, mac: %s.\n", info.state, info.ip.c_str(), info.mac.c_str());
251 }
252
TestOneByOne()253 static int TestOneByOne()
254 {
255 printf("ENTER TEST.\n");
256 std::map<std::string, std::string> cmdMap = {{"1", "Start"}, {"2", "Stop"},
257 {"3", "StartDiscover"}, {"4", "StopDiscover"},
258 {"5", "AddCastDevice"}, {"6", "RemoveCastDevice"}};
259
260 std::string helpNotice = "select steps: 0-quit;\n"
261 "1-Start; 2-Stop;\n"
262 "3-StartDiscover; 4-StopDiscover\n"
263 "5-AddCastDevice; 6-RemoveCastDevice\n";
264
265 std::shared_ptr<WfdSourceDemo> demo = std::make_shared<WfdSourceDemo>();
266 if (!demo->CreateClient()) {
267 printf("create client failed.\n");
268 return -1;
269 }
270 std::string inputCmd;
271 bool exit = false;
272 while (!exit) {
273 printf("%s.\n", helpNotice.c_str());
274 getline(std::cin, inputCmd);
275 if (inputCmd == "") {
276 continue;
277 } else if (inputCmd == "0") {
278 exit = true;
279 } else {
280 if (cmdMap.count(inputCmd) == 0) {
281 printf("no cmd: %s, input again.", inputCmd.c_str());
282 continue;
283 } else {
284 demo->DoCmd(cmdMap[inputCmd]);
285 }
286 }
287 }
288
289 return 0;
290 }
291
main()292 int main()
293 {
294 printf("wfd source test start!\n");
295 TestOneByOne();
296 return 0;
297 }
298