aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/utils/deviceshell.h
blob: dcbd3ca905cc3cfd4ddc7f37118f02bb45f866ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#pragma once

#include "utils_global.h"

#include "fileutils.h"

#include <QMap>
#include <QMutex>
#include <QProcess>
#include <QThread>
#include <QWaitCondition>

#include <memory>

namespace Utils {

class CommandLine;
class ProcessResultData;
class QtcProcess;

class DeviceShellImpl;

class QTCREATOR_UTILS_EXPORT DeviceShell : public QObject
{
    Q_OBJECT

public:
    enum class State { FailedToStart = -1, Unknown = 0, Succeeded = 1, NoScript = 2 };

    enum class ParseType {
        StdOut,
        StdErr,
        ExitCode,
    };

    DeviceShell(bool forceFailScriptInstallation = false);
    virtual ~DeviceShell();

    bool start();

    RunResult runInShell(const CommandLine &cmd, const QByteArray &stdInData = {});

    State state() const;

    QStringList missingFeatures() const;

signals:
    void done(const ProcessResultData &resultData);

protected:
    virtual void startupFailed(const CommandLine &cmdLine);
    RunResult run(const CommandLine &cmd, const QByteArray &stdInData = {});

    void close();

private:
    virtual void setupShellProcess(QtcProcess *shellProcess);
    virtual CommandLine createFallbackCommand(const CommandLine &cmdLine);

    bool installShellScript();
    void closeShellProcess();

    void onReadyRead();

    bool checkCommand(const QByteArray &command);

private:
    struct CommandRun : public RunResult
    {
        QWaitCondition *waiter;
    };

    std::unique_ptr<QtcProcess> m_shellProcess;
    QThread m_thread;
    int m_currentId{0};

    QMutex m_commandMutex;
    // QMap is used here to preserve iterators
    QMap<quint64, CommandRun> m_commandOutput;
    QByteArray m_commandBuffer;

    State m_shellScriptState = State::Unknown;
    QStringList m_missingFeatures;

    // Only used for tests
    bool m_forceFailScriptInstallation = false;
};

} // namespace Utils