Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
hdc.cpp
Go to the documentation of this file.
1// Copyright (C) 2026 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4#include "hdc.h"
5
6#include <QtCore/qprocess.h>
7
8#include <cstdio>
9
10QT_BEGIN_NAMESPACE
11
12using namespace Qt::StringLiterals;
13
14static QStringList shellCommandArgs(const QStringList &command)
15{
16 return QStringList{ u"shell"_s } + command;
17}
18
19Hdc::Hdc(QString hdcPath, QString connectKey)
20 : m_hdcPath(std::move(hdcPath)), m_connectKey(std::move(connectKey))
21{
22}
23
24QString Hdc::program() const
25{
26 return m_hdcPath;
27}
28
29QString Hdc::connectKey() const
30{
31 return m_connectKey;
32}
33
34QStringList Hdc::arguments(const QStringList &args) const
35{
36 QStringList allArgs;
37 if (!m_connectKey.isEmpty())
38 allArgs << u"-t"_s << m_connectKey;
39 return allArgs + args;
40}
41
42QString Hdc::run(const QStringList &args, bool printOnFailure) const
43{
44 static constexpr int startTimeoutMs = 5000;
45 static constexpr int finishTimeoutMs = 30000;
46
47 QProcess process;
48 const QStringList allArgs = arguments(args);
49 process.start(m_hdcPath, allArgs);
50 if (!process.waitForStarted(startTimeoutMs)) {
51 if (printOnFailure) {
52 fprintf(stderr, "harmonyostestrunner: failed to start hdc: %s\n",
53 qPrintable(m_hdcPath));
54 }
55 return {};
56 }
57 process.waitForFinished(finishTimeoutMs);
58 if (printOnFailure) {
59 const QByteArray standardError = process.readAllStandardError();
60 if (!standardError.isEmpty()) {
61 fprintf(stderr, "hdc %s stderr: %s\n", qPrintable(allArgs.join(u' ')),
62 standardError.constData());
63 }
64 }
65 return QString::fromUtf8(process.readAllStandardOutput());
66}
67
68QString Hdc::shell(const QStringList &command, bool printOnFailure) const
69{
70 return run(shellCommandArgs(command), printOnFailure);
71}
72
73QStringList Hdc::shellArguments(const QStringList &command) const
74{
75 return arguments(shellCommandArgs(command));
76}
77
78QT_END_NAMESPACE
static QStringList shellCommandArgs(const QStringList &command)
Definition hdc.cpp:14