1 /*
2 * Copyright (c) 2022-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 <iostream>
17
18 #include "accesstoken_kit.h"
19 #include "display.h"
20 #include "display_manager.h"
21 #include "dscreen_source_handler.h"
22 #include "dscreen_sink_handler.h"
23 #include "dscreen_util.h"
24 #include "idistributed_hardware_sink.h"
25 #include "idistributed_hardware_source.h"
26 #include "screen.h"
27 #include "screen_client.h"
28 #include "screen_client_common.h"
29 #include "screen_manager.h"
30 #include "wm_common.h"
31 #include "window.h"
32 #include "window_option.h"
33 #include "nativetoken_kit.h"
34 #include "token_setproc.h"
35
36 #include "decoder_demo.h"
37 #include "softbus_bus_center.h"
38 #include "softbus_common.h"
39
40 #include "if_system_ability_manager.h"
41 #include "iservice_registry.h"
42
43 using namespace std;
44 using namespace OHOS;
45 using namespace OHOS::DistributedHardware;
46 using namespace OHOS::Rosen;
47 using namespace OHOS::Media;
48 using namespace OHOS::Security::AccessToken;
49
50 namespace {
51 static char const *g_pkgName = "ohos.dsoftbus.tool";
52 }
53
54 constexpr int32_t SLEEP_FIVE_SECOND = 10;
55 static vector<sptr<Screen>> remoteScreens;
56 static uint64_t g_screenId = 0;
57
QueryRemoteScreenInfo(int mode)58 int QueryRemoteScreenInfo(int mode)
59 {
60 if (mode != 0) {
61 DHLOGE("QueryRemoteScreenInfo mode error");
62 return -1;
63 }
64 vector<sptr<Screen>> allScreens;
65 ScreenManager::GetInstance().GetAllScreens(allScreens);
66 sptr<Display> defaultDisplay = DisplayManager::GetInstance().GetDefaultDisplay();
67 for (const auto &screen : allScreens) {
68 if (screen == nullptr) {
69 continue;
70 }
71 if (!screen->IsReal() && screen->GetWidth() > 0) {
72 remoteScreens.push_back(screen);
73 }
74 }
75 DHLOGE("-------------remote screen info---------------");
76 DHLOGE("remote screen Num: %d", remoteScreens.size());
77 for (const auto &screen : remoteScreens) {
78 if (screen == nullptr) {
79 continue;
80 }
81 g_screenId = screen->GetId();
82 DHLOGE("--------screen id: %d ---------", screen->GetId());
83 DHLOGE("screen name: : %s", GetAnonyString(screen->GetName()).c_str());
84 DHLOGE("width: : %d", screen->GetWidth());
85 DHLOGE("height : %d", screen->GetHeight());
86 DHLOGE("-------------------------------------------");
87 }
88
89 return 0;
90 }
91
StartMirror(int mode)92 int StartMirror(int mode)
93 {
94 if (mode != 0) {
95 DHLOGE("StartMirror mode error");
96 return -1;
97 }
98 uint64_t ret = QueryRemoteScreenInfo(0);
99 if (ret != 0) {
100 DHLOGE("Error: no remote screens enabled");
101 return -1;
102 }
103
104 DHLOGE("select remote screen id to mirror");
105
106 bool isMirrorIdValid = false;
107 for (const auto &screen : remoteScreens) {
108 if (screen == nullptr) {
109 continue;
110 }
111 if (screen->GetId() == g_screenId) {
112 isMirrorIdValid = true;
113 break;
114 }
115 }
116
117 if (!isMirrorIdValid) {
118 DHLOGE("input mirrorId is not valid!");
119 return -1;
120 }
121
122 sptr<Display> defaultDisplay = DisplayManager::GetInstance().GetDefaultDisplay();
123 DHLOGE("------------start mirror----------");
124 DHLOGE("mirror screen Id is: %d", g_screenId);
125 vector<uint64_t> mirrorIds;
126 mirrorIds.push_back(g_screenId);
127 ScreenId screenGroupId;
128 ScreenManager::GetInstance().MakeMirror(defaultDisplay->GetScreenId(), mirrorIds, screenGroupId);
129 sleep(SLEEP_FIVE_SECOND);
130 return 0;
131 }
132
StopMirror(int mode)133 int StopMirror(int mode)
134 {
135 if (mode != 0) {
136 DHLOGE("StopMirror mode error");
137 return -1;
138 }
139 uint64_t ret = QueryRemoteScreenInfo(0);
140 if (ret != 0) {
141 DHLOGE("no remote screens enabled, no need stop mirror ");
142 return -1;
143 }
144
145 bool isStopMirrorIdValid = false;
146 for (const auto &screen : remoteScreens) {
147 if (screen == nullptr) {
148 continue;
149 }
150 if (screen->GetId() == g_screenId) {
151 isStopMirrorIdValid = true;
152 break;
153 }
154 }
155 if (!isStopMirrorIdValid) {
156 DHLOGE("input g_screenId is not valid! ");
157 return -1;
158 }
159
160 DHLOGE("-------------- stop mirror ------------");
161 DHLOGE("stop mirror screen id is: %d", g_screenId);
162 vector<uint64_t> stopMirrorIds;
163 stopMirrorIds.push_back(g_screenId);
164 ScreenManager::GetInstance().RemoveVirtualScreenFromGroup(stopMirrorIds);
165 sleep(SLEEP_FIVE_SECOND);
166 return 0;
167 }
168
StartExpand(int mode)169 int StartExpand(int mode)
170 {
171 if (mode != 0) {
172 DHLOGE("StartExpand mode error");
173 return -1;
174 }
175 uint64_t ret = QueryRemoteScreenInfo(0);
176 if (ret != 0) {
177 DHLOGE("Error: no remote screens enabled");
178 return -1;
179 }
180
181 bool isExpandIdValid = false;
182 for (const auto &screen : remoteScreens) {
183 if (screen == nullptr) {
184 continue;
185 }
186 if (screen->GetId() == g_screenId) {
187 isExpandIdValid = true;
188 break;
189 }
190 }
191
192 if (!isExpandIdValid) {
193 DHLOGE("input expandId is not valid!");
194 return -1;
195 }
196
197 sptr<Display> defaultDisplay = DisplayManager::GetInstance().GetDefaultDisplay();
198 DHLOGE("------------start expand----------");
199 DHLOGE("expand screen Id is: %d", g_screenId);
200 vector<ExpandOption> options = {{defaultDisplay->GetScreenId(), 0, 0}, {g_screenId, defaultDisplay->GetWidth(), 0}};
201 ScreenId screenGroupId;
202 ScreenManager::GetInstance().MakeExpand(options, screenGroupId);
203 sleep(SLEEP_FIVE_SECOND);
204 return 0;
205 }
206
StopExpand(int mode)207 int StopExpand(int mode)
208 {
209 if (mode != 0) {
210 DHLOGE("StopExpand mode error");
211 return -1;
212 }
213 uint64_t ret = QueryRemoteScreenInfo(0);
214 if (ret != 0) {
215 DHLOGE("no remote screens enabled, no need stop expand");
216 return -1;
217 }
218
219 bool isStopExpandIdValid = false;
220 for (const auto &screen : remoteScreens) {
221 if (screen == nullptr) {
222 continue;
223 }
224 if (screen->GetId() == g_screenId) {
225 isStopExpandIdValid = true;
226 break;
227 }
228 }
229 if (!isStopExpandIdValid) {
230 DHLOGE("input g_screenId is not valid!");
231 return -1;
232 }
233
234 DHLOGE("-------------- stop expand ------------");
235 DHLOGE("stop expand screen id is : %d", g_screenId);
236 vector<uint64_t> stopExpandIds;
237 stopExpandIds.push_back(g_screenId);
238 ScreenManager::GetInstance().RemoveVirtualScreenFromGroup(stopExpandIds);
239 sleep(SLEEP_FIVE_SECOND);
240 return 0;
241 }
242
PrintNodeProperty(NodeBasicInfo * nodeInfo)243 static void PrintNodeProperty(NodeBasicInfo *nodeInfo)
244 {
245 if (nodeInfo == nullptr) {
246 DHLOGE("nodeInfo is nullptr");
247 return;
248 }
249
250 DHLOGE("DeviceName = %s", nodeInfo->deviceName);
251 DHLOGE("NetworkId = %s", GetAnonyString(nodeInfo->networkId).c_str());
252 NodeDeviceInfoKey key = NODE_KEY_UDID;
253 unsigned char udid[UDID_BUF_LEN] = {0};
254 if (GetNodeKeyInfo(g_pkgName, nodeInfo->networkId, key, udid, UDID_BUF_LEN) != 0) {
255 DHLOGE("GetNodeKeyInfo Fail!");
256 }
257 key = NODE_KEY_UUID;
258 unsigned char uuid[UUID_BUF_LEN] = {0};
259 if (GetNodeKeyInfo(g_pkgName, nodeInfo->networkId, key, uuid, UUID_BUF_LEN) != 0) {
260 DHLOGE("GetNodeKeyInfo Fail!");
261 } else {
262 DHLOGE("Uuid = %s\n", GetAnonyString(reinterpret_cast<char *>(udid)).c_str());
263 }
264 }
265
QueryRemoteDeviceInfo(int mode)266 int QueryRemoteDeviceInfo(int mode)
267 {
268 if (mode != 0) {
269 DHLOGE("QueryRemoteDeviceInfo mode error");
270 return -1;
271 }
272 uint64_t tokenId;
273 const char *perms[2];
274 perms[0] = OHOS_PERMISSION_DISTRIBUTED_SOFTBUS_CENTER;
275 perms[1] = OHOS_PERMISSION_DISTRIBUTED_DATASYNC;
276 NativeTokenInfoParams infoInstance = {
277 .dcapsNum = 0,
278 .permsNum = 2,
279 .aclsNum = 0,
280 .dcaps = NULL,
281 .perms = perms,
282 .acls = NULL,
283 .processName = "dscreen_test_demo",
284 .aplStr = "system_core",
285 };
286 tokenId = GetAccessTokenId(&infoInstance);
287 SetSelfTokenID(tokenId);
288 OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
289
290 NodeBasicInfo localNodeinfo;
291 NodeBasicInfo *remoteNodeInfo = nullptr;
292 int32_t infoNum = 0;
293
294 DHLOGE("-----------Local Device Info------");
295
296 if (GetLocalNodeDeviceInfo(g_pkgName, &localNodeinfo) != 0) {
297 printf("LnnGetLocalNodeInfo Fail!\n");
298 DHLOGE("LnnGetLocalNodeInfo Fail!");
299 return -1;
300 }
301
302 PrintNodeProperty(&localNodeinfo);
303 DHLOGE("-------Remote Device info---------");
304 if (GetAllNodeDeviceInfo(g_pkgName, &remoteNodeInfo, &infoNum) != 0) {
305 DHLOGE("GetAllNodeDeviceInfo Fail!");
306 return -1;
307 }
308
309 DHLOGE("Device Num = %d", infoNum);
310 for (int i = 0; i < infoNum; ++i) {
311 DHLOGE("[No.%d]", i + 1);
312 PrintNodeProperty(remoteNodeInfo + i);
313 }
314
315 FreeNodeInfo(remoteNodeInfo);
316 DHLOGE("SoftBusDumpDeviceInfo complete");
317 sleep(SLEEP_FIVE_SECOND);
318 return 0;
319 }
320
CreateWindow(int mode)321 int CreateWindow(int mode)
322 {
323 if (mode != 0) {
324 DHLOGE("CreateWindow mode error");
325 return -1;
326 }
327 DHLOGE("create window, please input window size");
328
329 uint32_t windowWidth = 640;
330 uint32_t windowHeight = 480;
331
332 sptr<Display> defaultDisplay = DisplayManager::GetInstance().GetDefaultDisplay();
333 shared_ptr<WindowProperty> windowProperty = make_shared<WindowProperty>();
334 windowProperty->displayId = defaultDisplay->GetId();
335 windowProperty->startX = 0;
336 windowProperty->startY = 0;
337 windowProperty->width = windowWidth;
338 windowProperty->height = windowHeight;
339 int32_t windowId = ScreenClient::GetInstance().AddWindow(windowProperty);
340 ScreenClient::GetInstance().ShowWindow(windowId);
341 sptr<Surface> surface = ScreenClient::GetInstance().GetSurface(windowId);
342 DHLOGE("create window success.");
343
344 auto vdec = make_shared<VDecDemo>();
345
346 vdec->SetWindowSize(windowWidth, windowHeight);
347 vdec->SetOutputSurface(surface);
348 DHLOGE("start run decoder");
349 vdec->RunCase();
350 DHLOGE("create window success, window id: %d, width: %d, height: %d", windowId, windowWidth, windowHeight);
351 ScreenClient::GetInstance().RemoveWindow(windowId);
352 sleep(SLEEP_FIVE_SECOND);
353 return 0;
354 }
355
SaTest()356 int SaTest()
357 {
358 auto samgr = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
359 if (samgr == nullptr) {
360 DHLOGE("testSa1===");
361 return 1;
362 }
363 auto object = samgr->CheckSystemAbility(4807);
364 if (object == nullptr) {
365 DHLOGE("testSa2===");
366 return 1;
367 }
368 return 0;
369 }