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 
40 namespace OHOS {
41 namespace Msdp {
42 namespace DeviceStatus {
43 enum class ServiceRunningState {STATE_NOT_START, STATE_RUNNING, STATE_EXIT};
44 class DeviceStatusService final : public IContext,
45                                   public StreamServer,
46                                   public SystemAbility,
47                                   public DeviceStatusSrvStub {
48     DECLARE_SYSTEM_ABILITY(DeviceStatusService)
49     DECLARE_DELAYED_SP_SINGLETON(DeviceStatusService);
50 public:
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     IPluginManager& GetPluginManager() override;
61     ISocketSessionManager& GetSocketSessionManager() override;
62     IInputAdapter& GetInput() override;
63     IDSoftbusAdapter& GetDSoftbus() override;
64 
65     void OnAddSystemAbility(int32_t saId, const std::string &deviceId) override;
66     void Subscribe(Type type, ActivityEvent event, ReportLatencyNs latency,
67         sptr<IRemoteDevStaCallback> callback) override;
68     void Unsubscribe(Type type, ActivityEvent event, sptr<IRemoteDevStaCallback> callback) override;
69     Data GetCache(const Type &type) override;
70     bool IsServiceReady() const;
71     int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override;
72     void ReportSensorSysEvent(int32_t type, bool enable);
73     int32_t RegisterCoordinationListener(bool isCompatible = false) override;
74     int32_t UnregisterCoordinationListener(bool isCompatible = false) override;
75     int32_t PrepareCoordination(int32_t userData, bool isCompatible = false) override;
76     int32_t UnprepareCoordination(int32_t userData, bool isCompatible = false) override;
77     int32_t ActivateCoordination(int32_t userData, const std::string &remoteNetworkId, int32_t startDeviceId,
78         bool isCompatible = false) override;
79     int32_t DeactivateCoordination(int32_t userData, bool isUnchained, bool isCompatible = false) override;
80     int32_t GetCoordinationState(int32_t userData, const std::string &networkId,
81         bool isCompatible = false) override;
82     int32_t GetCoordinationState(const std::string &udId, bool &state) override;
83     int32_t StartDrag(const DragData &dragData) override;
84     int32_t StopDrag(const DragDropResult &dropResult) override;
85     int32_t UpdateDragStyle(DragCursorStyle style) override;
86     int32_t GetDragTargetPid() override;
87     int32_t GetUdKey(std::string &udKey) override;
88     int32_t AddDraglistener() override;
89     int32_t RemoveDraglistener() override;
90     int32_t AddSubscriptListener() override;
91     int32_t RemoveSubscriptListener() override;
92     int32_t SetDragWindowVisible(bool visible, bool isForce = false) override;
93     int32_t EnterTextEditorArea(bool enable) override;
94     int32_t GetShadowOffset(ShadowOffset &shadowOffset) override;
95     int32_t UpdateShadowPic(const ShadowInfo &shadowInfo) override;
96     int32_t GetDragData(DragData &dragData) override;
97     int32_t GetDragState(DragState &dragState) override;
98     int32_t GetDragAction(DragAction &dragAction) override;
99     int32_t GetExtraInfo(std::string &extraInfo) override;
100     int32_t AllocSocketFd(const std::string &programName, int32_t moduleType,
101     int32_t &toReturnClientFd, int32_t &tokenType) override;
102     void OnConnected(SessionPtr s) override;
103     void OnDisconnected(SessionPtr s) override;
104     int32_t AddEpoll(EpollEventType type, int32_t fd) override;
105     int32_t DelEpoll(EpollEventType type, int32_t fd);
106     bool IsRunning() const override;
107     int32_t AddHotAreaListener() override;
108     int32_t RemoveHotAreaListener() override;
109     int32_t UpdatePreviewStyle(const PreviewStyle &previewStyle) override;
110     int32_t UpdatePreviewStyleWithAnimation(const PreviewStyle &previewStyle,
111         const PreviewAnimation &animation) override;
112     int32_t GetDragSummary(std::map<std::string, int64_t> &summarys) override;
113     int32_t AddPrivilege() override;
114     int32_t EraseMouseIcon() override;
115 
116 private:
117     bool Init();
118     int32_t InitDelegateTasks();
119     int32_t InitTimerMgr();
120     int32_t InitMotionDrag();
121     void OnThread();
122     void OnDelegateTask(const epoll_event &ev);
123     void OnTimeout(const epoll_event &ev);
124     void OnDeviceMgr(const epoll_event &ev);
125     int32_t EnableDevMgr(int32_t nRetries);
126     void DisableDevMgr();
127     void EnableDSoftbus();
128 
129 #ifdef OHOS_BUILD_ENABLE_COORDINATION
130 #ifndef OHOS_BUILD_ENABLE_INTENTION_FRAMEWORK
131     int32_t OnAddHotAreaListener(int32_t pid);
132     int32_t OnRemoveHotAreaListener(int32_t pid);
133     int32_t OnRegisterCoordinationListener(int32_t pid);
134     int32_t OnUnregisterCoordinationListener(int32_t pid);
135     int32_t OnPrepareCoordination(int32_t pid, int32_t userData);
136     int32_t OnUnprepareCoordination(int32_t pid, int32_t userData);
137     int32_t OnActivateCoordination(int32_t pid, int32_t userData, const std::string &remoteNetworkId,
138         int32_t startDeviceId);
139     int32_t OnDeactivateCoordination(int32_t pid, int32_t userData, bool isUnchained);
140     int32_t OnGetCoordinationState(int32_t pid, int32_t userData, const std::string &networkId);
141     int32_t OnGetCoordinationStateSync(const std::string &udId, bool &state);
142 #endif // OHOS_BUILD_ENABLE_INTENTION_FRAMEWORK
143 #endif // OHOS_BUILD_ENABLE_COORDINATION
144 
145 private:
146     std::atomic<ServiceRunningState> state_ { ServiceRunningState::STATE_NOT_START };
147     std::thread worker_;
148     DelegateTasks delegateTasks_;
149     DeviceManager devMgr_;
150     TimerManager timerMgr_;
151     std::atomic<bool> ready_ { false };
152     std::shared_ptr<DeviceStatusManager> devicestatusManager_ { nullptr };
153     DragManager dragMgr_;
154     SocketSessionManager socketSessionMgr_;
155     std::unique_ptr<IInputAdapter> input_;
156     std::unique_ptr<IPluginManager> pluginMgr_;
157     std::unique_ptr<IDSoftbusAdapter> dsoftbus_;
158     sptr<IntentionService> intention_;
159 };
160 } // namespace DeviceStatus
161 } // namespace Msdp
162 } // namespace OHOS
163 #endif // DEVICESTATUS_SERVICE_H
164