summaryrefslogtreecommitdiffstats
path: root/tools/defaultnodeidsgenerator/main.cpp
blob: 3390857ab758dfb372e8d542d898fc4517a44811 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/****************************************************************************
**
** Copyright (C) 2018 basysKom GmbH, opensource@basyskom.com
** Contact: http://www.qt.io/licensing/
**
** This file is part of the QtOpcUa module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL3$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPLv3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or later as published by the Free
** Software Foundation and appearing in the file LICENSE.GPL included in
** the packaging of this file. Please review the following information to
** ensure the GNU General Public License version 2.0 requirements will be
** met: http://www.gnu.org/licenses/gpl-2.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QCommandLineParser>
#include <QCoreApplication>
#include <QDebug>
#include <QFile>


/*
 * This generator should be run if there is a new version of the NodeIds.csv file available in
 * https://opcfoundation.org/UA/schemas/1.03/
 *
 * Building
 * Qt OPC UA must be configured with -feature-ns0idgenerator to enable building the generator.
 *
 * Usage:
 * qtopcua-defaultnodeidsgenerator -i NodeIds.csv -o /path/to/qtopcua/src/opcua/client/qopcuanodeids
 *
 * This generates qopcuanodeids.h and qopcuanodeids.cpp from the content of NodeIds.csv.
 * The Qt OPC UA module must be rebuilt after running the generator.
*/


void createLicenseHeader(QTextStream &output)
{
    output << R"(/****************************************************************************
**
** Copyright (C) 2018 basysKom GmbH, opensource@basyskom.com
** Contact: http://www.qt.io/licensing/
**
** This file is part of the QtOpcUa module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL3$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPLv3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or later as published by the Free
** Software Foundation and appearing in the file LICENSE.GPL included in
** the packaging of this file. Please review the following information to
** ensure the GNU General Public License version 2.0 requirements will be
** met: http://www.gnu.org/licenses/gpl-2.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/)";
}

void createHeader(QTextStream &output)
{
    createLicenseHeader(output);
    output << "\n\n";
    output << "#ifndef QOPCUANODEIDS_H" << "\n";
    output << "#define QOPCUANODEIDS_H" << "\n\n";
    output << "#include <QtOpcUa/qopcuaglobal.h>" << "\n\n";
    output << "#include <QtCore/qmetatype.h>" << "\n\n";
    output << "QT_BEGIN_NAMESPACE" << "\n\n";
    output << "namespace QOpcUa {" << "\n";
    output << "    " << "namespace NodeIds {" << "\n";
    output << "#ifndef QT_OPCUA_NO_NS0IDNAMES" << "\n";
    output << "        " << "Q_OPCUA_EXPORT Q_NAMESPACE" << "\n";
    output << "#endif" << "\n\n";
    output << "        " << "enum class NS0 : quint32 {" << "\n";
    output << "            " << "Unknown = 0," << "\n";
}

void createFooter(QTextStream &output)
{
    output << "        " << "};" << "\n";
    output << "#ifndef QT_OPCUA_NO_NS0IDNAMES" << "\n";
    output << "        " << "Q_ENUM_NS(NS0)" << "\n";
    output << "#endif" << "\n";
    output << "    " << "}" << "\n";
    output << "}" << "\n\n";
    output << "QT_END_NAMESPACE" << "\n\n";
    output << "#endif // QOPCUANODEIDS_H\n";
}

void createSourceHeader(QTextStream &output)
{
    createLicenseHeader(output);
    output << "\n\n";
    output << "#include \"qopcuanodeids.h\"" << "\n\n";
    output << "QT_BEGIN_NAMESPACE" << "\n\n";
    output << R"(/*!
    \namespace QOpcUa::NodeIds
    \inmodule QtOpcUa
    \inheaderfile QtOpcUa/qopcuanodeids.h
    \brief This namespace contains enums with known node ids.
*/

/*!
    \enum QOpcUa::NodeIds::NS0

    Contains all numeric node identifiers from namespace 0 defined in the OPC Foundation's
    \l {https://opcfoundation.org/UA/schemas/1.03/NodeIds.csv} {NodeIds.csv} file.

    The values in this enum follow the naming from the CSV file and can be converted between
    enum and node id string using \l QOpcUa::ns0ID() and \l QOpcUa::ns0IDFromNodeId().
    \l QOpcUa::ns0IDName() provides a conversion from enum value to the name string from the CSV file.

    \code
    QScopedPointer<QOpcUaNode> rootNode(client->node(QOpcUa::ns0ID(QOpcUa::NodeIds::RootFolder)));
    \endcode)" << "\n\n";
    output << "    " << "\\value Unknown" << "\n";
}

void createSourceFooter(QTextStream &output)
{
    output << "*/" << "\n\n";
    output << "QT_END_NAMESPACE" << "\n";
}

bool nodeIdFromLine(const QString &line, QTextStream &outputHeader, QTextStream &outputSource)
{
    QStringList parts = line.trimmed().split(QLatin1String(","));

    if (parts.size() != 3)
        return false;
    bool ok = false;
    int identifier = parts.at(1).toUInt(&ok);
    if (!ok)
        return false;

    outputHeader << "            " << QStringLiteral("%1 = %2,").arg(parts.at(0)).arg(identifier) << "\n";
    outputSource << "    " << QStringLiteral("\\value %1").arg(parts.at(0)) << "\n";
    return true;
}

int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);
    QCommandLineParser parser;
    parser.addHelpOption();
    parser.setApplicationDescription("\nThis application generates a Qt OPC UA header file from the NodeIds.csv file distributed by the OPC Foundation.");
    parser.addOption(QCommandLineOption("i", "The NodeIds.csv file", "input file"));
    parser.addOption(QCommandLineOption("o", "The output prefix for the generated .h and c++ file, defaults to qopcuanodeids.h", "output file", "qopcuanodeids"));
    parser.process(app);

    if (!parser.isSet("i")) {
        qDebug() << "Error: No input file specified";
        return EXIT_FAILURE;
    }

    QFile inputFile(parser.value("i"));

    if (!inputFile.open(QFile::ReadOnly | QFile::Text)) {
        qDebug() << "Failed to open file" << inputFile.fileName();
        return EXIT_FAILURE;
    }

    QString currentLine = inputFile.readLine();

    if (currentLine.trimmed() != QLatin1String("Boolean,1,DataType")) {
        qDebug() << "Invalid content in" << inputFile.fileName();
        inputFile.close();
        return EXIT_FAILURE;
    }

    QFile outHeader(parser.value("o")+QLatin1String(".h"));
    if (!outHeader.open(QFile::WriteOnly | QFile::Text | QFile::Truncate)) {
        qDebug() << "Failed to open output file" << outHeader.fileName();
        inputFile.close();
        return EXIT_FAILURE;
    }

    QFile outSource(parser.value("o")+QLatin1String(".cpp"));
    if (!outSource.open(QFile::WriteOnly | QFile::Text | QFile::Truncate)) {
        qDebug() << "Failed to open output file" << outSource.fileName();
        inputFile.close();
        outHeader.close();
        return EXIT_FAILURE;
    }

    QTextStream outHeaderStream(&outHeader);
    QTextStream outSourceStream(&outSource);
    createHeader(outHeaderStream);
    createSourceHeader(outSourceStream);
    nodeIdFromLine(currentLine, outHeaderStream, outSourceStream);

    while (!inputFile.atEnd()) {
        currentLine = inputFile.readLine();
        nodeIdFromLine(currentLine, outHeaderStream, outSourceStream);
    }

    createFooter(outHeaderStream);
    createSourceFooter(outSourceStream);

    inputFile.close();
    outHeader.close();
    outSource.close();

    return EXIT_SUCCESS;
}