1 /*
2  * Copyright (c) 2024 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 DEVICEMANAGER_FUZZER_H
17 #define DEVICEMANAGER_FUZZER_H
18 
19 #define FUZZ_PROJECT_NAME "devicemanager_fuzzer"
20 
21 #include <memory>
22 #include <string>
23 
24 #include <fcntl.h>
25 #include "nocopyable.h"
26 
27 #include "cooperate_events.h"
28 #include "delegate_tasks.h"
29 #include "device_manager.h"
30 #include "devicestatus_define.h"
31 #include "devicestatus_delayed_sp_singleton.h"
32 #include "drag_manager.h"
33 #include "i_context.h"
34 #include "timer_manager.h"
35 
36 #include "intention_service.h"
37 #include "socket_session_manager.h"
38 
39 namespace OHOS {
40 namespace Msdp {
41 namespace DeviceStatus {
42 enum EpollEventType {
43     EPOLL_EVENT_BEGIN = 0,
44     EPOLL_EVENT_INPUT = EPOLL_EVENT_BEGIN,
45     EPOLL_EVENT_SOCKET,
46     EPOLL_EVENT_ETASK,
47     EPOLL_EVENT_TIMER,
48     EPOLL_EVENT_DEVICE_MGR,
49     EPOLL_EVENT_END
50 };
51 
52 struct TimerInfo {
53     int32_t times { 0 };
54     int32_t timerId { 0 };
55 };
56 
57 enum class ServiceRunningState {STATE_NOT_START, STATE_RUNNING, STATE_EXIT};
58 class ContextService final : public IContext {
59     ContextService();
60     ~ContextService();
61     DISALLOW_COPY_AND_MOVE(ContextService);
62 public:
63     IDelegateTasks& GetDelegateTasks() override;
64     IDeviceManager& GetDeviceManager() override;
65     ITimerManager& GetTimerManager() override;
66     IDragManager& GetDragManager() override;
67 
68     IPluginManager& GetPluginManager() override;
69     ISocketSessionManager& GetSocketSessionManager() override;
70     IInputAdapter& GetInput() override;
71     IDSoftbusAdapter& GetDSoftbus() override;
72 private:
73     void OnStart();
74     void OnStop();
75     bool Init();
76     int32_t EpollCreate();
77     int32_t AddEpoll(EpollEventType type, int32_t fd);
78     int32_t DelEpoll(EpollEventType type, int32_t fd);
79     int32_t EpollCtl(int32_t fd, int32_t op, struct epoll_event &event);
80     int32_t EpollWait(int32_t maxevents, int32_t timeout, struct epoll_event &events);
81     void EpollClose();
82     int32_t InitTimerMgr();
83     int32_t InitDevMgr();
84     void OnThread();
85     void OnTimeout(const epoll_event &ev);
86     void OnDeviceMgr(const epoll_event &ev);
87     int32_t EnableDevMgr(int32_t nRetries);
88     void DisableDevMgr();
89     int32_t InitDelegateTasks();
90     void OnDelegateTask(const struct epoll_event &ev);
91     static ContextService* GetInstance();
92 private:
93     std::atomic<ServiceRunningState> state_ { ServiceRunningState::STATE_NOT_START };
94     std::thread worker_;
95     DelegateTasks delegateTasks_;
96     DeviceManager devMgr_;
97     TimerManager timerMgr_;
98     std::atomic<bool> ready_ { false };
99     DragManager dragMgr_;
100     int32_t epollFd_ { -1 };
101     SocketSessionManager socketSessionMgr_;
102     std::unique_ptr<IInputAdapter> input_;
103     std::unique_ptr<IPluginManager> pluginMgr_;
104     std::unique_ptr<IDSoftbusAdapter> dsoftbusAda_;
105 };
106 } // namespace DeviceStatus
107 } // namespace Msdp
108 } // namespace OHOS
109 #endif // DEVICEMANAGER_FUZZER_H