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