1cc290419Sopenharmony_ci/*
2cc290419Sopenharmony_ci * Copyright (C) 2021 Huawei Device Co., Ltd.
3cc290419Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4cc290419Sopenharmony_ci * you may not use this file except in compliance with the License.
5cc290419Sopenharmony_ci * You may obtain a copy of the License at
6cc290419Sopenharmony_ci *
7cc290419Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8cc290419Sopenharmony_ci *
9cc290419Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10cc290419Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11cc290419Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12cc290419Sopenharmony_ci * See the License for the specific language governing permissions and
13cc290419Sopenharmony_ci * limitations under the License.
14cc290419Sopenharmony_ci */
15cc290419Sopenharmony_ci#ifndef HDC_ASYNC_CMD_H
16cc290419Sopenharmony_ci#define HDC_ASYNC_CMD_H
17cc290419Sopenharmony_ci#include "common.h"
18cc290419Sopenharmony_ci
19cc290419Sopenharmony_cinamespace Hdc {
20cc290419Sopenharmony_ciclass AsyncCmd {
21cc290419Sopenharmony_cipublic:
22cc290419Sopenharmony_ci    AsyncCmd();
23cc290419Sopenharmony_ci    virtual ~AsyncCmd();
24cc290419Sopenharmony_ci    enum AsyncCmdOption {
25cc290419Sopenharmony_ci        OPTION_COMMAND_ONETIME = 1,
26cc290419Sopenharmony_ci        USB_OPTION_RESERVE2 = 2,
27cc290419Sopenharmony_ci        OPTION_READBACK_OUT = 4,  // deprecated, remove it later
28cc290419Sopenharmony_ci        USB_OPTION_RESERVE8 = 8,
29cc290419Sopenharmony_ci    };
30cc290419Sopenharmony_ci    // 1)is finish 2)exitStatus 3)resultString(maybe empty)
31cc290419Sopenharmony_ci    using CmdResultCallback = std::function<bool(bool, int64_t, const string)>;
32cc290419Sopenharmony_ci    // deprecated, remove it later
33cc290419Sopenharmony_ci    static uint32_t GetDefaultOption()
34cc290419Sopenharmony_ci    {
35cc290419Sopenharmony_ci        return OPTION_COMMAND_ONETIME;
36cc290419Sopenharmony_ci    }
37cc290419Sopenharmony_ci    // uv_loop_t loop is given to uv_spawn, which can't be const
38cc290419Sopenharmony_ci    bool Initial(uv_loop_t *loopIn, const CmdResultCallback callback, uint32_t optionsIn = 0);
39cc290419Sopenharmony_ci    void DoRelease();  // Release process resources
40cc290419Sopenharmony_ci    bool ExecuteCommand(const string &command);
41cc290419Sopenharmony_ci    bool ReadyForRelease();
42cc290419Sopenharmony_ci
43cc290419Sopenharmony_ciprivate:
44cc290419Sopenharmony_ci    static bool FinishShellProc(const void *context, const bool result, const string exitMsg);
45cc290419Sopenharmony_ci    static bool ChildReadCallback(const void *context, uint8_t *buf, const int size);
46cc290419Sopenharmony_ci    int ThreadFork(const string &command, bool readWrite, int &cpid);
47cc290419Sopenharmony_ci    static void *Popen(void *arg);
48cc290419Sopenharmony_ci#if !defined(_WIN32) && !defined(HDC_HOST)
49cc290419Sopenharmony_ci    bool GetDevItem(const char *key, string &out);
50cc290419Sopenharmony_ci#endif
51cc290419Sopenharmony_ci
52cc290419Sopenharmony_ci    uint32_t options = 0;
53cc290419Sopenharmony_ci    int fd = 0;
54cc290419Sopenharmony_ci    int pid = 0;
55cc290419Sopenharmony_ci    HdcFileDescriptor *childShell = nullptr;
56cc290419Sopenharmony_ci    uint32_t refCount = 0;
57cc290419Sopenharmony_ci    CmdResultCallback resultCallback;
58cc290419Sopenharmony_ci    uv_loop_t *loop = nullptr;
59cc290419Sopenharmony_ci    string cmdResult;
60cc290419Sopenharmony_ci};
61cc290419Sopenharmony_ci
62cc290419Sopenharmony_cistruct AsyncParams {
63cc290419Sopenharmony_ci    string commandParam;
64cc290419Sopenharmony_ci    bool readWriteParam;
65cc290419Sopenharmony_ci    int &cpidParam;
66cc290419Sopenharmony_ci    bool isRoot;
67cc290419Sopenharmony_ci
68cc290419Sopenharmony_ci    AsyncParams(const string &commandParam, bool readWriteParam, int &cpidParam, bool isRoot)
69cc290419Sopenharmony_ci        :commandParam(commandParam), readWriteParam(readWriteParam), cpidParam(cpidParam), isRoot(isRoot) {};
70cc290419Sopenharmony_ci};
71cc290419Sopenharmony_ci}  // namespace Hdc
72cc290419Sopenharmony_ci#endif
73