• 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 #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 #ifdef OHOS_BUILD_ENABLE_MOTION_DRAG
34 #include "motion_drag.h"
35 #endif // OHOS_BUILD_ENABLE_MOTION_DRAG
36 #include "stationary_callback.h"
37 #include "stationary_data.h"
38 #include "stream_server.h"
39 #include "timer_manager.h"
40 
41 namespace OHOS {
42 namespace Msdp {
43 namespace DeviceStatus {
44 enum class ServiceRunningState {STATE_NOT_START, STATE_RUNNING, STATE_EXIT};
45 class DeviceStatusService final : public IContext,
46                                   public StreamServer,
47                                   public SystemAbility,
48                                   public DeviceStatusSrvStub {
49     DECLARE_SYSTEM_ABILITY(DeviceStatusService)
50     DECLARE_DELAYED_SP_SINGLETON(DeviceStatusService);
51 public:
52     virtual void OnDump() override;
53     virtual void OnStart() override;
54     virtual void OnStop() override;
55 
56     IDelegateTasks& GetDelegateTasks() override;
57     IDeviceManager& GetDeviceManager() override;
58     ITimerManager& GetTimerManager() override;
59     IDragManager& GetDragManager() override;
60 
61     void Subscribe(Type type, ActivityEvent event, ReportLatencyNs latency,
62         sptr<IRemoteDevStaCallback> callback) override;
63     void Unsubscribe(Type type, ActivityEvent event, sptr<IRemoteDevStaCallback> callback) override;
64     Data GetCache(const Type& type) override;
65     bool IsServiceReady() const;
66     std::shared_ptr<DeviceStatusManager> GetDeviceStatusManager() const;
67     int32_t Dump(int32_t fd, const std::vector<std::u16string>& args) override;
68     void ReportSensorSysEvent(int32_t type, bool enable);
69     int32_t RegisterCoordinationListener() override;
70     int32_t UnregisterCoordinationListener() override;
71     int32_t PrepareCoordination(int32_t userData) override;
72     int32_t UnprepareCoordination(int32_t userData) override;
73     int32_t ActivateCoordination(int32_t userData, const std::string &remoteNetworkId, int32_t startDeviceId) override;
74     int32_t DeactivateCoordination(int32_t userData, bool isUnchained) override;
75     int32_t GetCoordinationState(int32_t userData, const std::string &deviceId) override;
76     int32_t StartDrag(const DragData &dragData) override;
77     int32_t StopDrag(DragResult result, bool hasCustomAnimation) override;
78     int32_t UpdateDragStyle(DragCursorStyle style) override;
79     int32_t GetDragTargetPid() override;
80     int32_t GetUdKey(std::string &udKey) override;
81     int32_t AddDraglistener() override;
82     int32_t RemoveDraglistener() override;
83     int32_t SetDragWindowVisible(bool visible) override;
84     int32_t GetShadowOffset(int32_t& offsetX, int32_t& offsetY, int32_t& width, int32_t& height) override;
85     int32_t UpdateShadowPic(const ShadowInfo &shadowInfo) override;
86     int32_t AllocSocketFd(const std::string &programName, int32_t moduleType,
87         int32_t &toReturnClientFd, int32_t &tokenType) override;
88     void OnConnected(SessionPtr s) override;
89     void OnDisconnected(SessionPtr s) override;
90     int32_t AddEpoll(EpollEventType type, int32_t fd) override;
91     int32_t DelEpoll(EpollEventType type, int32_t fd);
92     bool IsRunning() const override;
93 
94 private:
95     bool Init();
96     int32_t InitDelegateTasks();
97     int32_t InitTimerMgr();
98     void OnThread();
99     void OnDelegateTask(const epoll_event &ev);
100     void OnTimeout(const epoll_event &ev);
101     void OnDeviceMgr(const epoll_event &ev);
102     int32_t EnableDevMgr(int32_t nRetries);
103     void DisableDevMgr();
104     int32_t OnStartDrag(const DragData &dragData, int32_t pid);
105     int32_t OnStopDrag(DragResult result, bool hasCustomAnimation);
106 
107 #ifdef OHOS_BUILD_ENABLE_COORDINATION
108     int32_t OnRegisterCoordinationListener(int32_t pid);
109     int32_t OnUnregisterCoordinationListener(int32_t pid);
110     int32_t OnPrepareCoordination(int32_t pid, int32_t userData);
111     int32_t OnUnprepareCoordination(int32_t pid, int32_t userData);
112     int32_t OnActivateCoordination(int32_t pid, int32_t userData, const std::string &remoteNetworkId,
113         int32_t startDeviceId);
114     int32_t OnDeactivateCoordination(int32_t pid, int32_t userData, bool isUnchained);
115     int32_t OnGetCoordinationState(int32_t pid, int32_t userData, const std::string &deviceId);
116 #endif // OHOS_BUILD_ENABLE_COORDINATION
117 
118 private:
119     std::atomic<ServiceRunningState> state_ { ServiceRunningState::STATE_NOT_START };
120     std::thread worker_;
121     DelegateTasks delegateTasks_;
122     DeviceManager devMgr_;
123     TimerManager timerMgr_;
124     std::atomic<bool> ready_ { false };
125     std::shared_ptr<DeviceStatusManager> devicestatusManager_ { nullptr };
126     DragManager dragMgr_;
127 #ifdef OHOS_BUILD_ENABLE_MOTION_DRAG
128     MotionDrag motionDrag_;
129 #endif // OHOS_BUILD_ENABLE_MOTION_DRAG
130 };
131 } // namespace DeviceStatus
132 } // namespace Msdp
133 } // namespace OHOS
134 #endif // DEVICESTATUS_SERVICE_H
135