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 MSDPDEVICEMANAGER_FUZZER_H
17#define MSDPDEVICEMANAGER_FUZZER_H
18
19#define FUZZ_PROJECT_NAME "msdpdevicemanager_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
39namespace OHOS {
40namespace Msdp {
41namespace DeviceStatus {
42enum 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
52struct TimerInfo {
53    int32_t times { 0 };
54    int32_t timerId { 0 };
55};
56
57enum class ServiceRunningState {STATE_NOT_START, STATE_RUNNING, STATE_EXIT};
58class ContextService final : public IContext {
59    ContextService();
60    ~ContextService();
61    DISALLOW_COPY_AND_MOVE(ContextService);
62public:
63    IDelegateTasks& GetDelegateTasks() override;
64    IDeviceManager& GetDeviceManager() override;
65    ITimerManager& GetTimerManager() override;
66    IDragManager& GetDragManager() override;
67    IDDMAdapter& GetDDM() override;
68    IPluginManager& GetPluginManager() override;
69    ISocketSessionManager& GetSocketSessionManager() override;
70    IInputAdapter& GetInput() override;
71    IDSoftbusAdapter& GetDSoftbus() override;
72private:
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();
92private:
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<IDDMAdapter> ddm_;
103    std::unique_ptr<IInputAdapter> input_;
104    std::unique_ptr<IPluginManager> pluginMgr_;
105    std::unique_ptr<IDSoftbusAdapter> dsoftbusAda_;
106};
107} // namespace DeviceStatus
108} // namespace Msdp
109} // namespace OHOS
110#endif // MSDPDEVICEMANAGER_FUZZER_H