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 "devicestatus_service.h"
17
18 #include <unistd.h>
19 #include <vector>
20
21 #include <ipc_skeleton.h>
22
23 #include "hitrace_meter.h"
24 #include "hisysevent.h"
25 #include "if_system_ability_manager.h"
26 #include "iservice_registry.h"
27 #include "string_ex.h"
28 #include "system_ability_definition.h"
29
30 #ifdef OHOS_BUILD_ENABLE_COORDINATION
31 #include "coordination_device_manager.h"
32 #include "coordination_event_manager.h"
33 #include "coordination_hotarea.h"
34 #include "coordination_sm.h"
35 #endif // OHOS_BUILD_ENABLE_COORDINATION
36 #include "devicestatus_common.h"
37 #include "devicestatus_hisysevent.h"
38 #ifdef OHOS_BUILD_ENABLE_MOTION_DRAG
39 #include "motion_drag.h"
40 #endif // OHOS_BUILD_ENABLE_MOTION_DRAG
41
42 namespace OHOS {
43 namespace Msdp {
44 namespace DeviceStatus {
45 namespace {
46 constexpr OHOS::HiviewDFX::HiLogLabel LABEL { LOG_CORE, MSDP_DOMAIN_ID, "DeviceStatusService" };
47 constexpr int32_t DEFAULT_WAIT_TIME_MS { 1000 };
48 constexpr int32_t WAIT_FOR_ONCE { 1 };
49 constexpr int32_t MAX_N_RETRIES { 100 };
50
51 struct device_status_epoll_event {
52 int32_t fd { 0 };
53 EpollEventType event_type { EPOLL_EVENT_BEGIN };
54 };
55
56 const bool REGISTER_RESULT =
57 SystemAbility::MakeAndRegisterAbility(DelayedSpSingleton<DeviceStatusService>::GetInstance().GetRefPtr());
58 } // namespace
59
DeviceStatusService()60 DeviceStatusService::DeviceStatusService()
61 : SystemAbility(MSDP_DEVICESTATUS_SERVICE_ID, true)
62 {
63 #ifdef OHOS_BUILD_ENABLE_INTENTION_FRAMEWORK
64 dinput_ = std::make_shared<DInputAdapter>(this);
65 #endif // OHOS_BUILD_ENABLE_INTENTION_FRAMEWORK
66 }
67
~DeviceStatusService()68 DeviceStatusService::~DeviceStatusService()
69 {}
70
OnDump()71 void DeviceStatusService::OnDump()
72 {}
73
OnStart()74 void DeviceStatusService::OnStart()
75 {
76 CALL_INFO_TRACE;
77 if (ready_) {
78 FI_HILOGE("On start is ready, nothing to do");
79 return;
80 }
81
82 uint64_t tid = GetThisThreadId();
83 delegateTasks_.SetWorkerThreadId(tid);
84
85 if (!Init()) {
86 FI_HILOGE("On start call init failed");
87 return;
88 }
89 #ifdef OHOS_BUILD_ENABLE_INTENTION_FRAMEWORK
90 intention_ = sptr<IntentionService>::MakeSptr(this);
91 if (!Publish(intention_)) {
92 #else
93 if (!Publish(this)) {
94 #endif // OHOS_BUILD_ENABLE_INTENTION_FRAMEWORK
95 FI_HILOGE("On start register to system ability manager failed");
96 return;
97 }
98 state_ = ServiceRunningState::STATE_RUNNING;
99 ready_ = true;
100 worker_ = std::thread(std::bind(&DeviceStatusService::OnThread, this));
101 }
102
103 void DeviceStatusService::OnStop()
104 {
105 CALL_INFO_TRACE;
106 if (!ready_) {
107 return;
108 }
109 ready_ = false;
110 state_ = ServiceRunningState::STATE_EXIT;
111
112 delegateTasks_.PostAsyncTask([]() -> int32_t {
113 FI_HILOGD("No op");
114 return RET_OK;
115 });
116 if (worker_.joinable()) {
117 worker_.join();
118 }
119 }
120
121 IDelegateTasks& DeviceStatusService::GetDelegateTasks()
122 {
123 return delegateTasks_;
124 }
125
126 IDeviceManager& DeviceStatusService::GetDeviceManager()
127 {
128 return devMgr_;
129 }
130
131 ITimerManager& DeviceStatusService::GetTimerManager()
132 {
133 return timerMgr_;
134 }
135
136 IDragManager& DeviceStatusService::GetDragManager()
137 {
138 return dragMgr_;
139 }
140
141 #ifdef OHOS_BUILD_ENABLE_INTENTION_FRAMEWORK
142 ISocketSessionManager& DeviceStatusService::GetSocketSessionManager()
143 {
144 return socketSessionMgr_;
145 }
146
147 IPluginManager& DeviceStatusService::GetPluginManager()
148 {
149 return pluginMgr_;
150 }
151
152 IInputAdapter& DeviceStatusService::GetInput()
153 {
154 return input_;
155 }
156
157 IDInputAdapter& DeviceStatusService::GetDInput()
158 {
159 return *dinput_;
160 }
161 #endif // OHOS_BUILD_ENABLE_INTENTION_FRAMEWORK
162
163 int32_t DeviceStatusService::Dump(int32_t fd, const std::vector<std::u16string> &args)
164 {
165 CALL_DEBUG_ENTER;
166 if (fd < 0) {
167 FI_HILOGE("fd is invalid");
168 return RET_NG;
169 }
170 if (args.empty()) {
171 FI_HILOGE("Param cannot be empty");
172 dprintf(fd, "param cannot be empty\n");
173 DS_DUMPER->DumpHelpInfo(fd);
174 return RET_NG;
175 }
176 std::vector<std::string> argList;
177 std::transform(args.begin(), args.end(), std::back_inserter(argList),
178 [](const std::u16string &arg) {
179 return Str16ToStr8(arg);
180 });
181
182 std::vector<Data> datas;
183 for (auto type = TYPE_ABSOLUTE_STILL; type <= TYPE_LID_OPEN; type = static_cast<Type>(type + 1)) {
184 Data data = GetCache(type);
185 if (data.value != OnChangedValue::VALUE_INVALID) {
186 datas.emplace_back(data);
187 }
188 }
189 DS_DUMPER->ParseCommand(fd, argList, datas);
190 return RET_OK;
191 }
192
193 bool DeviceStatusService::Init()
194 {
195 CALL_DEBUG_ENTER;
196 if (devicestatusManager_ == nullptr) {
197 FI_HILOGW("devicestatusManager_ is nullptr");
198 devicestatusManager_ = std::make_shared<DeviceStatusManager>(this);
199 }
200 if (!devicestatusManager_->Init()) {
201 FI_HILOGE("OnStart init failed");
202 return false;
203 }
204 if (EpollCreate() != RET_OK) {
205 FI_HILOGE("Create epoll failed");
206 return false;
207 }
208 if (InitDelegateTasks() != RET_OK) {
209 FI_HILOGE("Delegate tasks init failed");
210 goto INIT_FAIL;
211 }
212 if (InitTimerMgr() != RET_OK) {
213 FI_HILOGE("TimerMgr init failed");
214 goto INIT_FAIL;
215 }
216 if (devMgr_.Init(this) != RET_OK) {
217 FI_HILOGE("DevMgr init failed");
218 goto INIT_FAIL;
219 }
220 if (dragMgr_.Init(this) != RET_OK) {
221 FI_HILOGE("Drag manager init failed");
222 goto INIT_FAIL;
223 }
224 if (InitMotionDrag() != RET_OK) {
225 FI_HILOGE("Drag adapter init failed");
226 goto INIT_FAIL;
227 }
228 if (DS_DUMPER->Init(this) != RET_OK) {
229 FI_HILOGE("Dump init failed");
230 goto INIT_FAIL;
231 }
232 #ifdef OHOS_BUILD_ENABLE_INTENTION_FRAMEWORK
233 if (socketSessionMgr_.Init() != RET_OK) {
234 FI_HILOGE("Failed to initialize socket session manager");
235 goto INIT_FAIL;
236 }
237 pluginMgr_.Init(this);
238 #endif // OHOS_BUILD_ENABLE_INTENTION_FRAMEWORK
239 #ifdef OHOS_BUILD_ENABLE_COORDINATION
240 COOR_EVENT_MGR->SetIContext(this);
241 COOR_SM->Init();
242 #endif // OHOS_BUILD_ENABLE_COORDINATION
243 return true;
244
245 INIT_FAIL:
246 EpollClose();
247 return false;
248 }
249
250 bool DeviceStatusService::IsServiceReady() const
251 {
252 return ready_;
253 }
254
255 std::shared_ptr<DeviceStatusManager> DeviceStatusService::GetDeviceStatusManager() const
256 {
257 return devicestatusManager_;
258 }
259
260 void DeviceStatusService::Subscribe(Type type, ActivityEvent event, ReportLatencyNs latency,
261 sptr<IRemoteDevStaCallback> callback)
262 {
263 FI_HILOGI("Enter event:%{public}d, latency:%{public}d", event, latency);
264 CHKPV(devicestatusManager_);
265 auto appInfo = std::make_shared<AppInfo>();
266 appInfo->uid = GetCallingUid();
267 appInfo->pid = GetCallingPid();
268 appInfo->tokenId = GetCallingTokenID();
269 devicestatusManager_->GetPackageName(appInfo->tokenId, appInfo->packageName);
270 appInfo->type = type;
271 appInfo->callback = callback;
272 DS_DUMPER->SaveAppInfo(appInfo);
273 StartTrace(HITRACE_TAG_MSDP, "serviceSubscribeStart");
274 devicestatusManager_->Subscribe(type, event, latency, callback);
275 FinishTrace(HITRACE_TAG_MSDP);
276 ReportSensorSysEvent(type, true);
277 WriteSubscribeHiSysEvent(appInfo->uid, appInfo->packageName, type);
278 }
279
280 void DeviceStatusService::Unsubscribe(Type type, ActivityEvent event, sptr<IRemoteDevStaCallback> callback)
281 {
282 FI_HILOGI("Enter event:%{public}d", event);
283 CHKPV(devicestatusManager_);
284 auto appInfo = std::make_shared<AppInfo>();
285 appInfo->uid = IPCSkeleton::GetCallingUid();
286 appInfo->pid = IPCSkeleton::GetCallingPid();
287 appInfo->tokenId = IPCSkeleton::GetCallingTokenID();
288 appInfo->packageName = DS_DUMPER->GetPackageName(appInfo->tokenId);
289 appInfo->type = type;
290 appInfo->callback = callback;
291 DS_DUMPER->RemoveAppInfo(appInfo);
292 StartTrace(HITRACE_TAG_MSDP, "serviceUnSubscribeStart");
293 devicestatusManager_->Unsubscribe(type, event, callback);
294 FinishTrace(HITRACE_TAG_MSDP);
295 ReportSensorSysEvent(type, false);
296 WriteUnSubscribeHiSysEvent(appInfo->uid, appInfo->packageName, type);
297 }
298
299 Data DeviceStatusService::GetCache(const Type &type)
300 {
301 CALL_DEBUG_ENTER;
302 if (devicestatusManager_ == nullptr) {
303 Data data = {type, OnChangedValue::VALUE_EXIT};
304 data.value = OnChangedValue::VALUE_INVALID;
305 FI_HILOGI("Get latest device status data func is nullptr, return default!");
306 return data;
307 }
308 return devicestatusManager_->GetLatestDeviceStatusData(type);
309 }
310
311 void DeviceStatusService::ReportSensorSysEvent(int32_t type, bool enable)
312 {
313 auto callerToken = GetCallingTokenID();
314 std::string packageName;
315 CHKPV(devicestatusManager_);
316 devicestatusManager_->GetPackageName(callerToken, packageName);
317 auto uid = GetCallingUid();
318 std::string str = enable ? "Subscribe" : "Unsubscribe";
319 int32_t ret = HiSysEventWrite(
320 OHOS::HiviewDFX::HiSysEvent::Domain::MSDP,
321 str,
322 OHOS::HiviewDFX::HiSysEvent::EventType::STATISTIC,
323 "UID", uid,
324 "PKGNAME", packageName,
325 "TYPE", type);
326 if (ret != 0) {
327 FI_HILOGE("HiviewDFX write failed, ret:%{public}d", ret);
328 }
329 }
330
331 int32_t DeviceStatusService::AllocSocketFd(const std::string &programName, int32_t moduleType,
332 int32_t &toReturnClientFd, int32_t &tokenType)
333 {
334 FI_HILOGD("Enter, programName:%{public}s, moduleType:%{public}d", programName.c_str(), moduleType);
335
336 toReturnClientFd = -1;
337 int32_t serverFd = -1;
338 int32_t pid = GetCallingPid();
339 int32_t uid = GetCallingUid();
340 int32_t ret = delegateTasks_.PostSyncTask(std::bind(&StreamServer::AddSocketPairInfo, this,
341 programName, moduleType, uid, pid, serverFd, std::ref(toReturnClientFd), tokenType));
342 if (ret != RET_OK) {
343 FI_HILOGE("Call Add socket pair info failed, return:%{public}d", ret);
344 return RET_ERR;
345 }
346 FI_HILOGD("Leave, programName:%{public}s, moduleType:%{public}d, alloc success",
347 programName.c_str(), moduleType);
348 return RET_OK;
349 }
350
351 void DeviceStatusService::OnConnected(SessionPtr s)
352 {
353 CHKPV(s);
354 FI_HILOGI("fd:%{public}d", s->GetFd());
355 }
356
357 void DeviceStatusService::OnDisconnected(SessionPtr s)
358 {
359 CHKPV(s);
360 FI_HILOGW("Enter, session, fd:%{public}d", s->GetFd());
361 }
362
363 int32_t DeviceStatusService::AddEpoll(EpollEventType type, int32_t fd)
364 {
365 if (!(type >= EPOLL_EVENT_BEGIN && type < EPOLL_EVENT_END)) {
366 FI_HILOGE("Invalid type:%{public}d", type);
367 return RET_ERR;
368 }
369 if (fd < 0) {
370 FI_HILOGE("Invalid fd:%{public}d", fd);
371 return RET_ERR;
372 }
373 auto eventData = static_cast<device_status_epoll_event*>(malloc(sizeof(device_status_epoll_event)));
374 if (!eventData) {
375 FI_HILOGE("Malloc failed");
376 return RET_ERR;
377 }
378 eventData->fd = fd;
379 eventData->event_type = type;
380 FI_HILOGD("EventData:[fd:%{public}d, type:%{public}d]", eventData->fd, eventData->event_type);
381
382 struct epoll_event ev {};
383 ev.events = EPOLLIN;
384 ev.data.ptr = eventData;
385 if (EpollCtl(fd, EPOLL_CTL_ADD, ev) != RET_OK) {
386 free(eventData);
387 eventData = nullptr;
388 ev.data.ptr = nullptr;
389 FI_HILOGE("EpollCtl failed");
390 return RET_ERR;
391 }
392 return RET_OK;
393 }
394
395 int32_t DeviceStatusService::DelEpoll(EpollEventType type, int32_t fd)
396 {
397 if (!(type >= EPOLL_EVENT_BEGIN && type < EPOLL_EVENT_END)) {
398 FI_HILOGE("Invalid type:%{public}d", type);
399 return RET_ERR;
400 }
401 if (fd < 0) {
402 FI_HILOGE("Invalid fd:%{public}d", fd);
403 return RET_ERR;
404 }
405 struct epoll_event ev {};
406 if (EpollCtl(fd, EPOLL_CTL_DEL, ev) != RET_OK) {
407 FI_HILOGE("DelEpoll failed");
408 return RET_ERR;
409 }
410 return RET_OK;
411 }
412
413 bool DeviceStatusService::IsRunning() const
414 {
415 return (state_ == ServiceRunningState::STATE_RUNNING);
416 }
417
418 int32_t DeviceStatusService::InitDelegateTasks()
419 {
420 CALL_INFO_TRACE;
421 if (!delegateTasks_.Init()) {
422 FI_HILOGE("The delegate task init failed");
423 return RET_ERR;
424 }
425 int32_t ret = AddEpoll(EPOLL_EVENT_ETASK, delegateTasks_.GetReadFd());
426 if (ret != RET_OK) {
427 FI_HILOGE("AddEpoll error ret:%{public}d", ret);
428 }
429 FI_HILOGI("AddEpoll, epollfd:%{public}d, fd:%{public}d", epollFd_, delegateTasks_.GetReadFd());
430 return ret;
431 }
432
433 int32_t DeviceStatusService::InitMotionDrag()
434 {
435 CALL_INFO_TRACE;
436 #ifdef OHOS_BUILD_ENABLE_MOTION_DRAG
437 if (motionDrag_ == nullptr) {
438 motionDrag_ = std::make_unique<MotionDrag>();
439 }
440 if (motionDrag_->Init(this) != RET_OK) {
441 return RET_ERR;
442 }
443 #endif // OHOS_BUILD_ENABLE_MOTION_DRAG
444 return RET_OK;
445 }
446
447 int32_t DeviceStatusService::InitTimerMgr()
448 {
449 CALL_INFO_TRACE;
450 int32_t ret = timerMgr_.Init(this);
451 if (ret != RET_OK) {
452 FI_HILOGE("TimerMgr init failed");
453 return ret;
454 }
455 ret = AddEpoll(EPOLL_EVENT_TIMER, timerMgr_.GetTimerFd());
456 if (ret != RET_OK) {
457 FI_HILOGE("AddEpoll for timer failed");
458 }
459 return ret;
460 }
461
462 void DeviceStatusService::OnThread()
463 {
464 SetThreadName(std::string("os_ds_service"));
465 uint64_t tid = GetThisThreadId();
466 delegateTasks_.SetWorkerThreadId(tid);
467 FI_HILOGD("Main worker thread start, tid:%{public}" PRId64 "", tid);
468 EnableDevMgr(MAX_N_RETRIES);
469
470 while (state_ == ServiceRunningState::STATE_RUNNING) {
471 struct epoll_event ev[MAX_EVENT_SIZE] {};
472 int32_t count = EpollWait(MAX_EVENT_SIZE, -1, ev[0]);
473 for (int32_t i = 0; i < count && state_ == ServiceRunningState::STATE_RUNNING; i++) {
474 auto epollEvent = reinterpret_cast<device_status_epoll_event*>(ev[i].data.ptr);
475 CHKPC(epollEvent);
476 if (epollEvent->event_type == EPOLL_EVENT_SOCKET) {
477 OnEpollEvent(ev[i]);
478 } else if (epollEvent->event_type == EPOLL_EVENT_ETASK) {
479 OnDelegateTask(ev[i]);
480 } else if (epollEvent->event_type == EPOLL_EVENT_TIMER) {
481 OnTimeout(ev[i]);
482 } else if (epollEvent->event_type == EPOLL_EVENT_DEVICE_MGR) {
483 OnDeviceMgr(ev[i]);
484 } else {
485 FI_HILOGW("Unknown epoll event type:%{public}d", epollEvent->event_type);
486 }
487 }
488 }
489 FI_HILOGD("Main worker thread stop, tid:%{public}" PRId64 "", tid);
490 }
491
492 void DeviceStatusService::OnDelegateTask(const struct epoll_event &ev)
493 {
494 if ((ev.events & EPOLLIN) == 0) {
495 FI_HILOGW("Not epollin");
496 return;
497 }
498 DelegateTasks::TaskData data {};
499 ssize_t res = read(delegateTasks_.GetReadFd(), &data, sizeof(data));
500 if (res == -1) {
501 FI_HILOGW("Read failed erron:%{public}d", errno);
502 }
503 FI_HILOGD("RemoteRequest notify td:%{public}" PRId64 ", std:%{public}" PRId64 ""
504 ", taskId:%{public}d", GetThisThreadId(), data.tid, data.taskId);
505 delegateTasks_.ProcessTasks();
506 }
507
508 void DeviceStatusService::OnTimeout(const struct epoll_event &ev)
509 {
510 CALL_INFO_TRACE;
511 if ((ev.events & EPOLLIN) == EPOLLIN) {
512 uint64_t expiration {};
513 ssize_t ret = read(timerMgr_.GetTimerFd(), &expiration, sizeof(expiration));
514 if (ret < 0) {
515 FI_HILOGE("Read expiration failed:%{public}s", strerror(errno));
516 }
517 timerMgr_.ProcessTimers();
518 } else if ((ev.events & (EPOLLHUP | EPOLLERR)) != 0) {
519 FI_HILOGE("Epoll hangup:%{public}s", strerror(errno));
520 }
521 }
522
523 void DeviceStatusService::OnDeviceMgr(const struct epoll_event &ev)
524 {
525 CALL_DEBUG_ENTER;
526 if ((ev.events & EPOLLIN) == EPOLLIN) {
527 devMgr_.Dispatch(ev);
528 } else if ((ev.events & (EPOLLHUP | EPOLLERR)) != 0) {
529 FI_HILOGE("Epoll hangup:%{public}s", strerror(errno));
530 }
531 }
532
533 int32_t DeviceStatusService::EnableDevMgr(int32_t nRetries)
534 {
535 CALL_INFO_TRACE;
536 static int32_t timerId { -1 };
537 int32_t ret = devMgr_.Enable();
538 if (ret != RET_OK) {
539 FI_HILOGE("Failed to enable device manager");
540 if (nRetries > 0) {
541 timerId = timerMgr_.AddTimer(DEFAULT_WAIT_TIME_MS, WAIT_FOR_ONCE,
542 std::bind(&DeviceStatusService::EnableDevMgr, this, nRetries - 1));
543 if (timerId < 0) {
544 FI_HILOGE("AddTimer failed, Failed to enable device manager");
545 }
546 } else {
547 FI_HILOGE("Maximum number of retries exceeded, Failed to enable device manager");
548 }
549 return ret;
550 }
551 AddEpoll(EPOLL_EVENT_DEVICE_MGR, devMgr_.GetFd());
552 if (timerId >= 0) {
553 timerMgr_.RemoveTimer(timerId);
554 timerId = -1;
555 }
556 return RET_OK;
557 }
558
559 void DeviceStatusService::DisableDevMgr()
560 {
561 DelEpoll(EPOLL_EVENT_DEVICE_MGR, devMgr_.GetFd());
562 devMgr_.Disable();
563 }
564
565 int32_t DeviceStatusService::RegisterCoordinationListener(bool isCompatible)
566 {
567 CALL_DEBUG_ENTER;
568 (void)(isCompatible);
569 #ifdef OHOS_BUILD_ENABLE_COORDINATION
570 int32_t pid = GetCallingPid();
571 int32_t ret = delegateTasks_.PostSyncTask(
572 std::bind(&DeviceStatusService::OnRegisterCoordinationListener, this, pid));
573 if (ret != RET_OK) {
574 FI_HILOGE("On register coordination listener failed, ret:%{public}d", ret);
575 return RET_ERR;
576 }
577 #endif // OHOS_BUILD_ENABLE_COORDINATION
578 return RET_OK;
579 }
580
581 int32_t DeviceStatusService::UnregisterCoordinationListener(bool isCompatible)
582 {
583 CALL_DEBUG_ENTER;
584 (void)(isCompatible);
585 #ifdef OHOS_BUILD_ENABLE_COORDINATION
586 int32_t pid = GetCallingPid();
587 int32_t ret = delegateTasks_.PostSyncTask(
588 std::bind(&DeviceStatusService::OnUnregisterCoordinationListener, this, pid));
589 if (ret != RET_OK) {
590 FI_HILOGE("On unregister coordination listener failed, ret:%{public}d", ret);
591 return RET_ERR;
592 }
593 #endif // OHOS_BUILD_ENABLE_COORDINATION
594 return RET_OK;
595 }
596
597 int32_t DeviceStatusService::PrepareCoordination(int32_t userData, bool isCompatible)
598 {
599 CALL_DEBUG_ENTER;
600 (void)(isCompatible);
601 #ifdef OHOS_BUILD_ENABLE_COORDINATION
602 int32_t pid = GetCallingPid();
603 AddSessionDeletedCallback(pid, std::bind(&CoordinationSM::OnSessionLost, COOR_SM, std::placeholders::_1));
604 int32_t ret = delegateTasks_.PostSyncTask(
605 std::bind(&DeviceStatusService::OnPrepareCoordination, this, pid, userData));
606 if (ret != RET_OK) {
607 FI_HILOGE("On prepare coordination failed, ret:%{public}d", ret);
608 return ret;
609 }
610 #else
611 (void)(userData);
612 #endif // OHOS_BUILD_ENABLE_COORDINATION
613 return RET_OK;
614 }
615
616 int32_t DeviceStatusService::UnprepareCoordination(int32_t userData, bool isCompatible)
617 {
618 CALL_DEBUG_ENTER;
619 (void)(isCompatible);
620 #ifdef OHOS_BUILD_ENABLE_COORDINATION
621 int32_t pid = GetCallingPid();
622 int32_t ret = delegateTasks_.PostSyncTask(
623 std::bind(&DeviceStatusService::OnUnprepareCoordination, this, pid, userData));
624 if (ret != RET_OK) {
625 FI_HILOGE("OnUnprepareCoordination failed, ret:%{public}d", ret);
626 return ret;
627 }
628 #else
629 (void)(userData);
630 #endif // OHOS_BUILD_ENABLE_COORDINATION
631 return RET_OK;
632 }
633
634 int32_t DeviceStatusService::ActivateCoordination(int32_t userData,
635 const std::string &remoteNetworkId, int32_t startDeviceId, bool isCompatible)
636 {
637 CALL_DEBUG_ENTER;
638 (void)(isCompatible);
639 #ifdef OHOS_BUILD_ENABLE_COORDINATION
640 int32_t pid = GetCallingPid();
641 int32_t ret = delegateTasks_.PostSyncTask(
642 std::bind(&DeviceStatusService::OnActivateCoordination,
643 this, pid, userData, remoteNetworkId, startDeviceId));
644 if (ret != RET_OK) {
645 FI_HILOGE("On activate coordination failed, ret:%{public}d", ret);
646 return ret;
647 }
648 #else
649 (void)(userData);
650 (void)(remoteNetworkId);
651 (void)(startDeviceId);
652 #endif // OHOS_BUILD_ENABLE_COORDINATION
653 return RET_OK;
654 }
655
656 int32_t DeviceStatusService::DeactivateCoordination(int32_t userData, bool isUnchained,
657 bool isCompatible)
658 {
659 CALL_DEBUG_ENTER;
660 (void)(isCompatible);
661 #ifdef OHOS_BUILD_ENABLE_COORDINATION
662 int32_t pid = GetCallingPid();
663 int32_t ret = delegateTasks_.PostSyncTask(
664 std::bind(&DeviceStatusService::OnDeactivateCoordination, this, pid, userData, isUnchained));
665 if (ret != RET_OK) {
666 FI_HILOGE("On deactivate coordination failed, ret:%{public}d", ret);
667 return ret;
668 }
669 #else
670 (void)(userData);
671 (void)(isUnchained);
672 #endif // OHOS_BUILD_ENABLE_COORDINATION
673 return RET_OK;
674 }
675
676 int32_t DeviceStatusService::GetCoordinationState(int32_t userData, const std::string &networkId,
677 bool isCompatible)
678 {
679 CALL_DEBUG_ENTER;
680 (void)(isCompatible);
681 #ifdef OHOS_BUILD_ENABLE_COORDINATION
682 int32_t pid = GetCallingPid();
683 int32_t ret = delegateTasks_.PostSyncTask(
684 std::bind(&DeviceStatusService::OnGetCoordinationState, this, pid, userData, networkId));
685 if (ret != RET_OK) {
686 FI_HILOGE("On get coordination state failed, ret:%{public}d", ret);
687 return ret;
688 }
689 #else
690 (void)(userData);
691 (void)(networkId);
692 FI_HILOGW("Get coordination state does not support");
693 #endif // OHOS_BUILD_ENABLE_COORDINATION
694 return RET_OK;
695 }
696
697 int32_t DeviceStatusService::AddDraglistener()
698 {
699 CALL_DEBUG_ENTER;
700 int32_t pid = GetCallingPid();
701 SessionPtr session = GetSession(GetClientFd(pid));
702 CHKPR(session, RET_ERR);
703 int32_t ret = delegateTasks_.PostSyncTask(
704 std::bind(&DragManager::AddListener, &dragMgr_, session));
705 if (ret != RET_OK) {
706 FI_HILOGE("AddListener failed, ret:%{public}d", ret);
707 }
708 return ret;
709 }
710
711 int32_t DeviceStatusService::RemoveDraglistener()
712 {
713 CALL_DEBUG_ENTER;
714 int32_t pid = GetCallingPid();
715 SessionPtr session = GetSession(GetClientFd(pid));
716 if (session == nullptr) {
717 FI_HILOGW("Session is nullptr");
718 return RET_OK;
719 }
720 int32_t ret = delegateTasks_.PostSyncTask(
721 std::bind(&DragManager::RemoveListener, &dragMgr_, session));
722 if (ret != RET_OK) {
723 FI_HILOGE("Remove listener failed, ret:%{public}d", ret);
724 }
725 return ret;
726 }
727
728 int32_t DeviceStatusService::AddSubscriptListener()
729 {
730 CALL_DEBUG_ENTER;
731 int32_t pid = GetCallingPid();
732 SessionPtr session = GetSession(GetClientFd(pid));
733 CHKPR(session, RET_ERR);
734 int32_t ret = delegateTasks_.PostSyncTask(
735 std::bind(&DragManager::AddSubscriptListener, &dragMgr_, session));
736 if (ret != RET_OK) {
737 FI_HILOGE("AddListener failed, ret:%{public}d", ret);
738 }
739 return ret;
740 }
741
742 int32_t DeviceStatusService::RemoveSubscriptListener()
743 {
744 CALL_DEBUG_ENTER;
745 int32_t pid = GetCallingPid();
746 SessionPtr session = GetSession(GetClientFd(pid));
747 CHKPR(session, RET_ERR);
748 int32_t ret = delegateTasks_.PostSyncTask(
749 std::bind(&DragManager::RemoveSubscriptListener, &dragMgr_, session));
750 if (ret != RET_OK) {
751 FI_HILOGE("AddListener failed, ret:%{public}d", ret);
752 }
753 return ret;
754 }
755
756 int32_t DeviceStatusService::StartDrag(const DragData &dragData)
757 {
758 CALL_DEBUG_ENTER;
759 int32_t pid = GetCallingPid();
760 AddSessionDeletedCallback(pid, std::bind(&DragManager::OnSessionLost, &dragMgr_, std::placeholders::_1));
761 SessionPtr session = GetSession(GetClientFd(pid));
762 CHKPR(session, RET_ERR);
763 int32_t ret = delegateTasks_.PostSyncTask(
764 std::bind(&DragManager::StartDrag, &dragMgr_, std::cref(dragData), session));
765 if (ret != RET_OK) {
766 FI_HILOGE("StartDrag failed, ret:%{public}d", ret);
767 }
768 return ret;
769 }
770
771 int32_t DeviceStatusService::StopDrag(const DragDropResult &dropResult)
772 {
773 CALL_DEBUG_ENTER;
774 int32_t ret = delegateTasks_.PostSyncTask(
775 std::bind(&DragManager::StopDrag, &dragMgr_, dropResult));
776 if (ret != RET_OK) {
777 FI_HILOGE("StopDrag failed, ret:%{public}d", ret);
778 }
779 return ret;
780 }
781
782 int32_t DeviceStatusService::SetDragWindowVisible(bool visible, bool isForce)
783 {
784 CALL_DEBUG_ENTER;
785 int32_t ret = delegateTasks_.PostSyncTask(
786 std::bind(&DragManager::OnSetDragWindowVisible, &dragMgr_, visible, isForce));
787 if (ret != RET_OK) {
788 FI_HILOGE("On set drag window visible failed, ret:%{public}d", ret);
789 }
790 return ret;
791 }
792
793 int32_t DeviceStatusService::EnterTextEditorArea(bool enable)
794 {
795 CALL_DEBUG_ENTER;
796 int32_t ret = delegateTasks_.PostSyncTask(
797 std::bind(&DragManager::EnterTextEditorArea, &dragMgr_, enable));
798 if (ret != RET_OK) {
799 FI_HILOGE("Enter Text Editor Area failed, ret:%{public}d", ret);
800 }
801 return ret;
802 }
803
804 int32_t DeviceStatusService::GetShadowOffset(int32_t &offsetX, int32_t &offsetY, int32_t &width, int32_t &height)
805 {
806 CALL_DEBUG_ENTER;
807 int32_t ret = delegateTasks_.PostSyncTask(std::bind(&DragManager::OnGetShadowOffset, &dragMgr_,
808 std::ref(offsetX), std::ref(offsetY), std::ref(width), std::ref(height)));
809 if (ret != RET_OK) {
810 FI_HILOGE("Get shadow offset failed, ret:%{public}d", ret);
811 }
812 return ret;
813 }
814
815 int32_t DeviceStatusService::UpdateShadowPic(const ShadowInfo &shadowInfo)
816 {
817 CALL_DEBUG_ENTER;
818 int32_t ret = delegateTasks_.PostSyncTask(
819 std::bind(&DragManager::UpdateShadowPic, &dragMgr_, std::cref(shadowInfo)));
820 if (ret != RET_OK) {
821 FI_HILOGE("Update shadow picture failed, ret:%{public}d", ret);
822 }
823 return ret;
824 }
825
826 int32_t DeviceStatusService::GetDragData(DragData &dragData)
827 {
828 CALL_DEBUG_ENTER;
829 int32_t ret = delegateTasks_.PostSyncTask(
830 std::bind(&DragManager::GetDragData, &dragMgr_, std::ref(dragData)));
831 if (ret != RET_OK) {
832 FI_HILOGE("Get drag data failed, ret:%{public}d", ret);
833 }
834 return ret;
835 }
836
837 int32_t DeviceStatusService::GetDragState(DragState &dragState)
838 {
839 CALL_DEBUG_ENTER;
840 int32_t ret = delegateTasks_.PostSyncTask(
841 std::bind(static_cast<int32_t(DragManager::*)(DragState&)>(&DragManager::GetDragState),
842 &dragMgr_, std::ref(dragState)));
843 if (ret != RET_OK) {
844 FI_HILOGE("Get drag state failed, ret:%{public}d", ret);
845 }
846 return ret;
847 }
848
849 int32_t DeviceStatusService::UpdateDragStyle(DragCursorStyle style)
850 {
851 CALL_DEBUG_ENTER;
852 int32_t tid = static_cast<int32_t>(GetCallingTokenID());
853 int32_t pid = GetCallingPid();
854 int32_t ret = delegateTasks_.PostSyncTask(
855 std::bind(&DragManager::UpdateDragStyle, &dragMgr_, style, pid, tid));
856 if (ret != RET_OK) {
857 FI_HILOGE("Update drag style failed, ret:%{public}d", ret);
858 }
859 return ret;
860 }
861
862 int32_t DeviceStatusService::GetUdKey(std::string &udKey)
863 {
864 CALL_DEBUG_ENTER;
865 int32_t ret = delegateTasks_.PostSyncTask(
866 std::bind(&DragManager::GetUdKey, &dragMgr_, std::ref(udKey)));
867 if (ret != RET_OK) {
868 FI_HILOGE("Get udkey failed, ret:%{public}d", ret);
869 }
870 return ret;
871 }
872
873 int32_t DeviceStatusService::GetDragTargetPid()
874 {
875 CALL_DEBUG_ENTER;
876 int32_t ret = delegateTasks_.PostSyncTask(
877 std::bind(&DragManager::GetDragTargetPid, &dragMgr_));
878 if (ret != RET_OK) {
879 FI_HILOGE("Get drag target pid failed, ret:%{public}d", ret);
880 }
881 return ret;
882 }
883
884 int32_t DeviceStatusService::GetDragAction(DragAction &dragAction)
885 {
886 int32_t ret = delegateTasks_.PostSyncTask(
887 std::bind(&DragManager::GetDragAction, &dragMgr_, std::ref(dragAction)));
888 if (ret != RET_OK) {
889 FI_HILOGE("Get drag action failed, ret:%{public}d", ret);
890 }
891 return ret;
892 }
893
894 int32_t DeviceStatusService::GetExtraInfo(std::string &extraInfo)
895 {
896 int32_t ret = delegateTasks_.PostSyncTask(
897 std::bind(&DragManager::GetExtraInfo, &dragMgr_, std::ref(extraInfo)));
898 if (ret != RET_OK) {
899 FI_HILOGE("Get extraInfo failed, ret:%{public}d", ret);
900 }
901 return ret;
902 }
903
904 #ifdef OHOS_BUILD_ENABLE_COORDINATION
905 int32_t DeviceStatusService::OnRegisterCoordinationListener(int32_t pid)
906 {
907 CALL_DEBUG_ENTER;
908 SessionPtr sess = GetSession(GetClientFd(pid));
909 CHKPR(sess, RET_ERR);
910 sptr<CoordinationEventManager::EventInfo> event = new (std::nothrow) CoordinationEventManager::EventInfo();
911 CHKPR(event, RET_ERR);
912 event->type = CoordinationEventManager::EventType::LISTENER;
913 event->sess = sess;
914 event->msgId = MessageId::COORDINATION_ADD_LISTENER;
915 COOR_EVENT_MGR->AddCoordinationEvent(event);
916 return RET_OK;
917 }
918
919 int32_t DeviceStatusService::OnUnregisterCoordinationListener(int32_t pid)
920 {
921 CALL_DEBUG_ENTER;
922 SessionPtr sess = GetSession(GetClientFd(pid));
923 sptr<CoordinationEventManager::EventInfo> event = new (std::nothrow) CoordinationEventManager::EventInfo();
924 CHKPR(event, RET_ERR);
925 event->type = CoordinationEventManager::EventType::LISTENER;
926 event->sess = sess;
927 COOR_EVENT_MGR->RemoveCoordinationEvent(event);
928 return RET_OK;
929 }
930
931 int32_t DeviceStatusService::OnPrepareCoordination(int32_t pid, int32_t userData)
932 {
933 CALL_DEBUG_ENTER;
934 COOR_SM->PrepareCoordination();
935 std::string networkId;
936 CoordinationMessage msg = CoordinationMessage::PREPARE;
937 NetPacket pkt(MessageId::COORDINATION_MESSAGE);
938 pkt << userData << networkId << static_cast<int32_t>(msg);
939 if (pkt.ChkRWError()) {
940 FI_HILOGE("Packet write data failed");
941 return RET_ERR;
942 }
943 SessionPtr sess = GetSession(GetClientFd(pid));
944 CHKPR(sess, RET_ERR);
945 if (!sess->SendMsg(pkt)) {
946 FI_HILOGE("Sending failed");
947 return MSG_SEND_FAIL;
948 }
949 return RET_OK;
950 }
951
952 int32_t DeviceStatusService::OnUnprepareCoordination(int32_t pid, int32_t userData)
953 {
954 CALL_DEBUG_ENTER;
955 COOR_SM->UnprepareCoordination();
956 std::string networkId;
957 CoordinationMessage msg = CoordinationMessage::UNPREPARE;
958 NetPacket pkt(MessageId::COORDINATION_MESSAGE);
959 pkt << userData << networkId << static_cast<int32_t>(msg);
960 if (pkt.ChkRWError()) {
961 FI_HILOGE("Packet write data failed");
962 return RET_ERR;
963 }
964 SessionPtr sess = GetSession(GetClientFd(pid));
965 CHKPR(sess, RET_ERR);
966 if (!sess->SendMsg(pkt)) {
967 FI_HILOGE("Sending failed");
968 return MSG_SEND_FAIL;
969 }
970 return RET_OK;
971 }
972
973 int32_t DeviceStatusService::OnActivateCoordination(int32_t pid,
974 int32_t userData, const std::string &remoteNetworkId, int32_t startDeviceId)
975 {
976 CALL_DEBUG_ENTER;
977 SessionPtr sess = GetSession(GetClientFd(pid));
978 CHKPR(sess, RET_ERR);
979 sptr<CoordinationEventManager::EventInfo> event = new (std::nothrow) CoordinationEventManager::EventInfo();
980 CHKPR(event, RET_ERR);
981 event->type = CoordinationEventManager::EventType::START;
982 event->sess = sess;
983 event->msgId = MessageId::COORDINATION_MESSAGE;
984 event->userData = userData;
985 if (COOR_SM->GetCurrentCoordinationState() == CoordinationState::STATE_OUT ||
986 (COOR_SM->GetCurrentCoordinationState() == CoordinationState::STATE_FREE &&
987 COOR_DEV_MGR->IsRemote(startDeviceId))) {
988 FI_HILOGW("It is currently worn out");
989 NetPacket pkt(event->msgId);
990 pkt << userData << "" << static_cast<int32_t>(CoordinationMessage::ACTIVATE_SUCCESS);
991 if (pkt.ChkRWError()) {
992 FI_HILOGE("Failed to write packet data");
993 return RET_ERR;
994 }
995 if (!sess->SendMsg(pkt)) {
996 FI_HILOGE("Sending message failed");
997 return RET_ERR;
998 }
999 return RET_OK;
1000 }
1001 COOR_EVENT_MGR->AddCoordinationEvent(event);
1002 int32_t ret = COOR_SM->ActivateCoordination(remoteNetworkId, startDeviceId);
1003 if (ret != RET_OK) {
1004 FI_HILOGE("On activate coordination error, ret:%{public}d", ret);
1005 }
1006 return ret;
1007 }
1008
1009 int32_t DeviceStatusService::OnDeactivateCoordination(int32_t pid, int32_t userData, bool isUnchained)
1010 {
1011 CALL_DEBUG_ENTER;
1012 SessionPtr sess = GetSession(GetClientFd(pid));
1013 CHKPR(sess, RET_ERR);
1014 sptr<CoordinationEventManager::EventInfo> event = new (std::nothrow) CoordinationEventManager::EventInfo();
1015 CHKPR(event, RET_ERR);
1016 event->type = CoordinationEventManager::EventType::STOP;
1017 event->sess = sess;
1018 event->msgId = MessageId::COORDINATION_MESSAGE;
1019 event->userData = userData;
1020 COOR_EVENT_MGR->AddCoordinationEvent(event);
1021 int32_t ret = COOR_SM->DeactivateCoordination(isUnchained);
1022 if (ret != RET_OK) {
1023 FI_HILOGE("On deactivate coordination failed, ret:%{public}d", ret);
1024 COOR_EVENT_MGR->OnErrorMessage(event->type, CoordinationMessage(ret));
1025 }
1026 return ret;
1027 }
1028
1029 int32_t DeviceStatusService::OnGetCoordinationState(
1030 int32_t pid, int32_t userData, const std::string &networkId)
1031 {
1032 CALL_DEBUG_ENTER;
1033 SessionPtr sess = GetSession(GetClientFd(pid));
1034 CHKPR(sess, RET_ERR);
1035 sptr<CoordinationEventManager::EventInfo> event = new (std::nothrow) CoordinationEventManager::EventInfo();
1036 CHKPR(event, RET_ERR);
1037 event->type = CoordinationEventManager::EventType::STATE;
1038 event->sess = sess;
1039 event->msgId = MessageId::COORDINATION_GET_STATE;
1040 event->userData = userData;
1041 COOR_EVENT_MGR->AddCoordinationEvent(event);
1042 int32_t ret = COOR_SM->GetCoordinationState(networkId);
1043 if (ret != RET_OK) {
1044 FI_HILOGE("Get coordination state failed");
1045 }
1046 return ret;
1047 }
1048
1049 int32_t DeviceStatusService::OnAddHotAreaListener(int32_t pid)
1050 {
1051 CALL_DEBUG_ENTER;
1052 auto sess = GetSession(GetClientFd(pid));
1053 CHKPR(sess, RET_ERR);
1054 sptr<CoordinationHotArea::HotAreaInfo> event = new (std::nothrow) CoordinationHotArea::HotAreaInfo();
1055 CHKPR(event, RET_ERR);
1056 event->sess = sess;
1057 event->msgId = MessageId::HOT_AREA_ADD_LISTENER;
1058 HOT_AREA->AddHotAreaListener(event);
1059 return RET_OK;
1060 }
1061
1062 int32_t DeviceStatusService::OnRemoveHotAreaListener(int32_t pid)
1063 {
1064 CALL_DEBUG_ENTER;
1065 auto sess = GetSession(GetClientFd(pid));
1066 CHKPR(sess, RET_ERR);
1067 sptr<CoordinationHotArea::HotAreaInfo> event = new (std::nothrow) CoordinationHotArea::HotAreaInfo();
1068 CHKPR(event, RET_ERR);
1069 event->sess = sess;
1070 HOT_AREA->RemoveHotAreaListener(event);
1071 return RET_OK;
1072 }
1073 #endif // OHOS_BUILD_ENABLE_COORDINATION
1074
1075 int32_t DeviceStatusService::AddHotAreaListener()
1076 {
1077 CALL_DEBUG_ENTER;
1078 #ifdef OHOS_BUILD_ENABLE_COORDINATION
1079 int32_t pid = GetCallingPid();
1080 int32_t ret = delegateTasks_.PostSyncTask(
1081 std::bind(&DeviceStatusService::OnAddHotAreaListener, this, pid));
1082 if (ret != RET_OK) {
1083 FI_HILOGE("Failed to add hot area listener, ret:%{public}d", ret);
1084 return RET_ERR;
1085 }
1086 #endif // OHOS_BUILD_ENABLE_COORDINATION
1087 return RET_OK;
1088 }
1089
1090 int32_t DeviceStatusService::RemoveHotAreaListener()
1091 {
1092 CALL_DEBUG_ENTER;
1093 #ifdef OHOS_BUILD_ENABLE_COORDINATION
1094 int32_t pid = GetCallingPid();
1095 int32_t ret = delegateTasks_.PostSyncTask(
1096 std::bind(&DeviceStatusService::OnRemoveHotAreaListener, this, pid));
1097 if (ret != RET_OK) {
1098 FI_HILOGE("Failed to remove hot area listener, ret:%{public}d", ret);
1099 return RET_ERR;
1100 }
1101 #endif // OHOS_BUILD_ENABLE_COORDINATION
1102 return RET_OK;
1103 }
1104
1105 int32_t DeviceStatusService::UpdatePreviewStyle(const PreviewStyle &previewStyle)
1106 {
1107 CALL_DEBUG_ENTER;
1108 int32_t ret = delegateTasks_.PostSyncTask(
1109 std::bind(&DragManager::UpdatePreviewStyle, &dragMgr_, previewStyle));
1110 if (ret != RET_OK) {
1111 FI_HILOGE("UpdatePreviewStyle failed, ret:%{public}d", ret);
1112 }
1113 return ret;
1114 }
1115
1116 int32_t DeviceStatusService::UpdatePreviewStyleWithAnimation(const PreviewStyle &previewStyle,
1117 const PreviewAnimation &animation)
1118 {
1119 CALL_DEBUG_ENTER;
1120 int32_t ret = delegateTasks_.PostSyncTask(
1121 std::bind(&DragManager::UpdatePreviewStyleWithAnimation, &dragMgr_, previewStyle, animation));
1122 if (ret != RET_OK) {
1123 FI_HILOGE("UpdatePreviewStyleWithAnimation failed, ret:%{public}d", ret);
1124 }
1125 return ret;
1126 }
1127
1128 int32_t DeviceStatusService::GetDragSummary(std::map<std::string, int64_t> &summarys)
1129 {
1130 int32_t ret = delegateTasks_.PostSyncTask(
1131 std::bind(&DragManager::GetDragSummary, &dragMgr_, std::ref(summarys)));
1132 if (ret != RET_OK) {
1133 FI_HILOGE("Failed to get drag summarys, ret:%{public}d", ret);
1134 return RET_ERR;
1135 }
1136 return RET_OK;
1137 }
1138
1139 int32_t DeviceStatusService::AddPrivilege()
1140 {
1141 CALL_DEBUG_ENTER;
1142 int32_t tokenId = static_cast<int32_t>(GetCallingTokenID());
1143 int32_t ret = delegateTasks_.PostSyncTask(
1144 std::bind(&DragManager::AddPrivilege, &dragMgr_, tokenId));
1145 if (ret != RET_OK) {
1146 FI_HILOGE("Failed to add privilege, ret:%{public}d", ret);
1147 return RET_ERR;
1148 }
1149 return RET_OK;
1150 }
1151 } // namespace DeviceStatus
1152 } // namespace Msdp
1153 } // namespace OHOS
1154