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#ifndef DEVICESTATUS_SERVICE_H
17#define DEVICESTATUS_SERVICE_H
18
19#include <memory>
20
21#include <iremote_object.h>
22#include <system_ability.h>
23
24#include "delegate_tasks.h"
25#include "device_manager.h"
26#include "devicestatus_delayed_sp_singleton.h"
27#include "devicestatus_dumper.h"
28#include "devicestatus_manager.h"
29#include "devicestatus_srv_stub.h"
30#include "drag_data.h"
31#include "drag_manager.h"
32#include "i_context.h"
33#include "intention_service.h"
34#include "socket_session_manager.h"
35#include "stationary_callback.h"
36#include "stationary_data.h"
37#include "stream_server.h"
38#include "timer_manager.h"
39
40namespace OHOS {
41namespace Msdp {
42namespace DeviceStatus {
43enum class ServiceRunningState {STATE_NOT_START, STATE_RUNNING, STATE_EXIT};
44class DeviceStatusService final : public IContext,
45                                  public StreamServer,
46                                  public SystemAbility,
47                                  public DeviceStatusSrvStub {
48    DECLARE_SYSTEM_ABILITY(DeviceStatusService)
49    DECLARE_DELAYED_SP_SINGLETON(DeviceStatusService);
50public:
51    virtual void OnDump() override;
52    virtual void OnStart() override;
53    virtual void OnStop() override;
54
55    IDelegateTasks& GetDelegateTasks() override;
56    IDeviceManager& GetDeviceManager() override;
57    ITimerManager& GetTimerManager() override;
58    IDragManager& GetDragManager() override;
59
60    IDDMAdapter& GetDDM() override;
61    IPluginManager& GetPluginManager() override;
62    ISocketSessionManager& GetSocketSessionManager() override;
63    IInputAdapter& GetInput() override;
64    IDSoftbusAdapter& GetDSoftbus() override;
65
66    void OnAddSystemAbility(int32_t saId, const std::string &deviceId) override;
67    void Subscribe(Type type, ActivityEvent event, ReportLatencyNs latency,
68        sptr<IRemoteDevStaCallback> callback) override;
69    void Unsubscribe(Type type, ActivityEvent event, sptr<IRemoteDevStaCallback> callback) override;
70    Data GetCache(const Type &type) override;
71    bool IsServiceReady() const;
72    int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override;
73#ifdef MSDP_HIVIEWDFX_HISYSEVENT_ENABLE
74    void ReportSensorSysEvent(int32_t type, bool enable);
75#endif
76    int32_t RegisterCoordinationListener(bool isCompatible = false) override;
77    int32_t UnregisterCoordinationListener(bool isCompatible = false) override;
78    int32_t PrepareCoordination(int32_t userData, bool isCompatible = false) override;
79    int32_t UnprepareCoordination(int32_t userData, bool isCompatible = false) override;
80    int32_t ActivateCoordination(int32_t userData, const std::string &remoteNetworkId, int32_t startDeviceId,
81        bool isCompatible = false) override;
82    int32_t DeactivateCoordination(int32_t userData, bool isUnchained, bool isCompatible = false) override;
83    int32_t GetCoordinationState(int32_t userData, const std::string &networkId,
84        bool isCompatible = false) override;
85    int32_t GetCoordinationState(const std::string &udId, bool &state) override;
86    int32_t StartDrag(const DragData &dragData) override;
87    int32_t StopDrag(const DragDropResult &dropResult) override;
88    int32_t UpdateDragStyle(DragCursorStyle style) override;
89    int32_t GetDragTargetPid() override;
90    int32_t GetUdKey(std::string &udKey) override;
91    int32_t AddDraglistener() override;
92    int32_t RemoveDraglistener() override;
93    int32_t AddSubscriptListener() override;
94    int32_t RemoveSubscriptListener() override;
95    int32_t SetDragWindowVisible(bool visible, bool isForce = false) override;
96    int32_t EnterTextEditorArea(bool enable) override;
97    int32_t GetShadowOffset(ShadowOffset &shadowOffset) override;
98    int32_t UpdateShadowPic(const ShadowInfo &shadowInfo) override;
99    int32_t GetDragData(DragData &dragData) override;
100    int32_t GetDragState(DragState &dragState) override;
101    int32_t GetDragAction(DragAction &dragAction) override;
102    int32_t GetExtraInfo(std::string &extraInfo) override;
103    int32_t AllocSocketFd(const std::string &programName, int32_t moduleType,
104    int32_t &toReturnClientFd, int32_t &tokenType) override;
105    void OnConnected(SessionPtr s) override;
106    void OnDisconnected(SessionPtr s) override;
107    int32_t AddEpoll(EpollEventType type, int32_t fd) override;
108    int32_t DelEpoll(EpollEventType type, int32_t fd);
109    bool IsRunning() const override;
110    int32_t AddHotAreaListener() override;
111    int32_t RemoveHotAreaListener() override;
112    int32_t UpdatePreviewStyle(const PreviewStyle &previewStyle) override;
113    int32_t UpdatePreviewStyleWithAnimation(const PreviewStyle &previewStyle,
114        const PreviewAnimation &animation) override;
115    int32_t GetDragSummary(std::map<std::string, int64_t> &summarys) override;
116    int32_t AddPrivilege() override;
117    int32_t EraseMouseIcon() override;
118
119private:
120    bool Init();
121    int32_t InitDelegateTasks();
122    int32_t InitTimerMgr();
123    int32_t InitMotionDrag();
124    void OnThread();
125    void OnSocketEvent(const struct epoll_event &ev);
126    void OnDelegateTask(const epoll_event &ev);
127    void OnTimeout(const epoll_event &ev);
128    void OnDeviceMgr(const epoll_event &ev);
129    int32_t EnableSocketSessionMgr(int32_t nRetries);
130    void DisableSocketSessionMgr();
131    int32_t EnableDevMgr(int32_t nRetries);
132    void DisableDevMgr();
133    void EnableDSoftbus();
134    void EnableDDM();
135
136#ifdef OHOS_BUILD_ENABLE_COORDINATION
137#ifndef OHOS_BUILD_ENABLE_INTENTION_FRAMEWORK
138    int32_t OnAddHotAreaListener(int32_t pid);
139    int32_t OnRemoveHotAreaListener(int32_t pid);
140    int32_t OnRegisterCoordinationListener(int32_t pid);
141    int32_t OnUnregisterCoordinationListener(int32_t pid);
142    int32_t OnPrepareCoordination(int32_t pid, int32_t userData);
143    int32_t OnUnprepareCoordination(int32_t pid, int32_t userData);
144    int32_t OnActivateCoordination(int32_t pid, int32_t userData, const std::string &remoteNetworkId,
145        int32_t startDeviceId);
146    int32_t OnDeactivateCoordination(int32_t pid, int32_t userData, bool isUnchained);
147    int32_t OnGetCoordinationState(int32_t pid, int32_t userData, const std::string &networkId);
148    int32_t OnGetCoordinationStateSync(const std::string &udId, bool &state);
149#endif // OHOS_BUILD_ENABLE_INTENTION_FRAMEWORK
150#endif // OHOS_BUILD_ENABLE_COORDINATION
151
152private:
153    std::atomic<ServiceRunningState> state_ { ServiceRunningState::STATE_NOT_START };
154    std::thread worker_;
155    DelegateTasks delegateTasks_;
156    DeviceManager devMgr_;
157    TimerManager timerMgr_;
158    std::atomic<bool> ready_ { false };
159    std::shared_ptr<DeviceStatusManager> devicestatusManager_ { nullptr };
160    DragManager dragMgr_;
161    SocketSessionManager socketSessionMgr_;
162    std::unique_ptr<IDDMAdapter> ddm_;
163    std::unique_ptr<IInputAdapter> input_;
164    std::unique_ptr<IPluginManager> pluginMgr_;
165    std::unique_ptr<IDSoftbusAdapter> dsoftbus_;
166    sptr<IntentionService> intention_;
167};
168} // namespace DeviceStatus
169} // namespace Msdp
170} // namespace OHOS
171#endif // DEVICESTATUS_SERVICE_H
172