1/*
2 * Copyright (c) 2021 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#include <cstring>
17#include <unistd.h>
18
19#include "ability_command.h"
20#include "ability_tool_command.h"
21#include "xcollie/xcollie.h"
22#include "xcollie/xcollie_define.h"
23#ifdef A11Y_ENABLE
24#include "accessibility_ability_command.h"
25#endif // A11Y_ENABLE
26
27using namespace OHOS;
28constexpr uint32_t COMMAND_TIME_OUT = 60;
29
30class CommandTimer {
31public:
32    CommandTimer(const std::string &timerName, uint32_t timeout, const std::string &operation)
33    {
34        if (operation != "test") {
35            setTimer_ = true;
36            timerId_ = HiviewDFX::XCollie::GetInstance().SetTimer("ability::aa_command", timeout,
37                nullptr, nullptr, HiviewDFX::XCOLLIE_FLAG_LOG |  HiviewDFX::XCOLLIE_FLAG_RECOVERY);
38        }
39    }
40    ~CommandTimer()
41    {
42        if (setTimer_) {
43            HiviewDFX::XCollie::GetInstance().CancelTimer(timerId_);
44        }
45    }
46private:
47    bool setTimer_ = false;
48    int32_t timerId_ = 0;
49};
50
51int main(int argc, char* argv[])
52{
53    std::string operation;
54    if (argc > 1) {
55        operation = argv[1];
56    }
57
58    if (strstr(argv[0], "aa") != nullptr) {
59        CommandTimer commandTimer("ability::aa_command", COMMAND_TIME_OUT, operation);
60        OHOS::AAFwk::AbilityManagerShellCommand cmd(argc, argv);
61        std::cout << cmd.ExecCommand();
62    } else if (strstr(argv[0], "ability_tool") != nullptr) {
63        OHOS::AAFwk::AbilityToolCommand cmd(argc, argv);
64        std::cout << cmd.ExecCommand();
65    } else {
66#ifdef A11Y_ENABLE
67        if (strstr(argv[0], "accessibility") != nullptr) {
68            OHOS::AAFwk::AccessibilityAbilityShellCommand cmd(argc, argv);
69            std::cout << cmd.ExecCommand();
70        }
71#endif // A11Y_ENABLE
72    }
73    fflush(stdout);
74    _exit(0);
75}
76