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