1209bc2fbSopenharmony_ci/*
2209bc2fbSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3209bc2fbSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4209bc2fbSopenharmony_ci * you may not use this file except in compliance with the License.
5209bc2fbSopenharmony_ci * You may obtain a copy of the License at
6209bc2fbSopenharmony_ci *
7209bc2fbSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8209bc2fbSopenharmony_ci *
9209bc2fbSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10209bc2fbSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11209bc2fbSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12209bc2fbSopenharmony_ci * See the License for the specific language governing permissions and
13209bc2fbSopenharmony_ci * limitations under the License.
14209bc2fbSopenharmony_ci */
15209bc2fbSopenharmony_ci
16209bc2fbSopenharmony_ci#ifndef RELIABILITY_WATCHDOG_TASK_H
17209bc2fbSopenharmony_ci#define RELIABILITY_WATCHDOG_TASK_H
18209bc2fbSopenharmony_ci
19209bc2fbSopenharmony_ci#include <functional>
20209bc2fbSopenharmony_ci#include <string>
21209bc2fbSopenharmony_ci#include <sys/types.h>
22209bc2fbSopenharmony_ci
23209bc2fbSopenharmony_ci#include "event_handler.h"
24209bc2fbSopenharmony_ci#include "handler_checker.h"
25209bc2fbSopenharmony_ci
26209bc2fbSopenharmony_ciusing Task = std::function<void()>;
27209bc2fbSopenharmony_ciusing TimeOutCallback = std::function<void(const std::string &name, int waitState)>;
28209bc2fbSopenharmony_ciusing XCollieCallback = std::function<void (void *)>;
29209bc2fbSopenharmony_cinamespace OHOS {
30209bc2fbSopenharmony_cinamespace HiviewDFX {
31209bc2fbSopenharmony_ciclass WatchdogTask {
32209bc2fbSopenharmony_ci    static int64_t curId;
33209bc2fbSopenharmony_cipublic:
34209bc2fbSopenharmony_ci    WatchdogTask(std::string name, std::shared_ptr<AppExecFwk::EventHandler> handler,
35209bc2fbSopenharmony_ci        TimeOutCallback timeOutCallback, uint64_t interval);
36209bc2fbSopenharmony_ci    WatchdogTask(std::string name, Task&& task, uint64_t delay, uint64_t interval, bool isOneshot);
37209bc2fbSopenharmony_ci    WatchdogTask(std::string name, unsigned int timeout, XCollieCallback func, void *arg, unsigned int flag);
38209bc2fbSopenharmony_ci    WatchdogTask(std::string name, unsigned int timeLimit, int countLimit);
39209bc2fbSopenharmony_ci    WatchdogTask()
40209bc2fbSopenharmony_ci        : name(""),
41209bc2fbSopenharmony_ci          task(nullptr),
42209bc2fbSopenharmony_ci          timeOutCallback(nullptr),
43209bc2fbSopenharmony_ci          checker(nullptr),
44209bc2fbSopenharmony_ci          id(0),
45209bc2fbSopenharmony_ci          timeout(0),
46209bc2fbSopenharmony_ci          func(nullptr),
47209bc2fbSopenharmony_ci          arg(nullptr),
48209bc2fbSopenharmony_ci          flag(0),
49209bc2fbSopenharmony_ci          checkInterval(0),
50209bc2fbSopenharmony_ci          nextTickTime(0),
51209bc2fbSopenharmony_ci          isTaskScheduled(false),
52209bc2fbSopenharmony_ci          isOneshotTask(false),
53209bc2fbSopenharmony_ci          watchdogTid(0),
54209bc2fbSopenharmony_ci          timeLimit(0),
55209bc2fbSopenharmony_ci          countLimit(0) {};
56209bc2fbSopenharmony_ci    ~WatchdogTask() {};
57209bc2fbSopenharmony_ci
58209bc2fbSopenharmony_ci    bool operator<(const WatchdogTask &obj) const
59209bc2fbSopenharmony_ci    {
60209bc2fbSopenharmony_ci        // as we use std::priority_queue, the event with smaller target time will be in the top of the queue
61209bc2fbSopenharmony_ci        return (this->nextTickTime > obj.nextTickTime);
62209bc2fbSopenharmony_ci    }
63209bc2fbSopenharmony_ci
64209bc2fbSopenharmony_ci    void Run(uint64_t now);
65209bc2fbSopenharmony_ci    void RunHandlerCheckerTask();
66209bc2fbSopenharmony_ci    void SendEvent(const std::string &msg, const std::string &eventName);
67209bc2fbSopenharmony_ci    void SendXCollieEvent(const std::string &timerName, const std::string &keyMsg) const;
68209bc2fbSopenharmony_ci    void DoCallback();
69209bc2fbSopenharmony_ci    void TimerCountTask();
70209bc2fbSopenharmony_ci
71209bc2fbSopenharmony_ci    int EvaluateCheckerState();
72209bc2fbSopenharmony_ci    std::string GetBlockDescription(uint64_t interval);
73209bc2fbSopenharmony_ci    std::string name;
74209bc2fbSopenharmony_ci    std::string message;
75209bc2fbSopenharmony_ci    Task task;
76209bc2fbSopenharmony_ci    TimeOutCallback timeOutCallback;
77209bc2fbSopenharmony_ci    std::shared_ptr<HandlerChecker> checker;
78209bc2fbSopenharmony_ci    int64_t id;
79209bc2fbSopenharmony_ci    uint64_t timeout;
80209bc2fbSopenharmony_ci    XCollieCallback func;
81209bc2fbSopenharmony_ci    void *arg;
82209bc2fbSopenharmony_ci    unsigned int flag;
83209bc2fbSopenharmony_ci    uint64_t checkInterval;
84209bc2fbSopenharmony_ci    uint64_t nextTickTime;
85209bc2fbSopenharmony_ci    bool isTaskScheduled;
86209bc2fbSopenharmony_ci    bool isOneshotTask;
87209bc2fbSopenharmony_ci    pid_t watchdogTid;
88209bc2fbSopenharmony_ci    uint64_t timeLimit;
89209bc2fbSopenharmony_ci    int countLimit;
90209bc2fbSopenharmony_ci    std::vector<uint64_t> triggerTimes;
91209bc2fbSopenharmony_ci
92209bc2fbSopenharmony_ciprivate:
93209bc2fbSopenharmony_ci    bool IsMemHookOn();
94209bc2fbSopenharmony_ci};
95209bc2fbSopenharmony_ci} // end of namespace HiviewDFX
96209bc2fbSopenharmony_ci} // end of namespace OHOS
97209bc2fbSopenharmony_ci#endif
98