summaryrefslogtreecommitdiffstats
path: root/src/plugins/opcua/uacpp/quacpputils.cpp
blob: 0b753d2a61493c127c45f78673e9fbe3249d1d06 (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
/******************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the QtOpcUa module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:COMM$
**
** 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.
**
** $QT_END_LICENSE$
**
******************************************************************************/

#include "quacpputils.h"

#include <QtCore/QLoggingCategory>
#include <QtCore/QString>
#include <QtCore/QUuid>

#include <uaguid.h>
#include <cstring>

QT_BEGIN_NAMESPACE

Q_DECLARE_LOGGING_CATEGORY(QT_OPCUA_PLUGINS_UACPP)

namespace UACppUtils {

UaNodeId nodeIdFromQString(const QString &name)
{
    const int semicolonIndex = name.indexOf(';');

    if (semicolonIndex <= 0) {
        qCWarning(QT_OPCUA_PLUGINS_UACPP, "Unable to split node id string: %s", qUtf8Printable(name));
        return UaNodeId();
    }

    QStringRef namespaceString = name.leftRef(semicolonIndex);
    if (namespaceString.length() <= 3 || !namespaceString.startsWith(QLatin1String("ns="))) {
        qCWarning(QT_OPCUA_PLUGINS_UACPP, "Not a valid index string in node id string: %s", qUtf8Printable(name));
        return UaNodeId();
    }
    namespaceString = namespaceString.mid(3); // Remove "ns="

    QStringRef identifierString = name.midRef(semicolonIndex + 1);

    if (identifierString.length() <= 2) {
        qCWarning(QT_OPCUA_PLUGINS_UACPP, "There is no identifier in node id string: %s", qUtf8Printable(name));
        return UaNodeId();
    }

    char identifierType;
    if (identifierString.startsWith(QLatin1String("s=")))
        identifierType = 's';
    else if (identifierString.startsWith(QLatin1String("i=")))
        identifierType = 'i';
    else if (identifierString.startsWith(QLatin1String("g=")))
        identifierType = 'g';
    else if (identifierString.startsWith(QLatin1String("b=")))
        identifierType = 'b';
    else {
        qCWarning(QT_OPCUA_PLUGINS_UACPP, "There is no valid identifier type in node id string: %s", qUtf8Printable(name));
        return UaNodeId();
    }
    identifierString = identifierString.mid(2); // Remove identifier type

    bool ok = false;
    OpcUa_UInt16 index = static_cast<OpcUa_UInt16>(namespaceString.toUInt(&ok));

    if (!ok) {
        qCWarning(QT_OPCUA_PLUGINS_UACPP, "Not a valid namespace index in node id string: %s", qUtf8Printable(name));
        return UaNodeId();
    }

    switch (identifierType) {
    case 'i': {
        bool isNumber;
        OpcUa_UInt32 identifier = static_cast<OpcUa_UInt32>(identifierString.toUInt(&isNumber));
        if (isNumber)
            return UaNodeId(identifier, index);
        else
            qCWarning(QT_OPCUA_PLUGINS_UACPP, "%s does not contain a valid numeric identifier", qUtf8Printable(name));
        break;
    }
    case 's': {
        if (identifierString.length() > 0)
            return UaNodeId(UaString(identifierString.toUtf8().constData()), index);
        else
            qCWarning(QT_OPCUA_PLUGINS_UACPP, "%s does not contain a valid string identifier", qUtf8Printable(name));
        break;
    }
    case 'g': {
        QUuid uuid(identifierString.toString());

        if (uuid.isNull()) {
            qCWarning(QT_OPCUA_PLUGINS_UACPP, "%s does not contain a valid guid identifier", qUtf8Printable(name));
        }

        OpcUa_Guid guid;
        guid.Data1 = uuid.data1;
        guid.Data2 = uuid.data2;
        guid.Data3 = uuid.data3;
        std::memcpy(guid.Data4, uuid.data4, sizeof(uuid.data4));
        return UaNodeId(guid, index);
    }
    case 'b': {
        QByteArray temp = QByteArray::fromBase64(identifierString.toLocal8Bit());
        UaByteString bstr((OpcUa_Int32)temp.size(), reinterpret_cast<OpcUa_Byte *>(temp.data()));
        if (temp.size() > 0) {
            return UaNodeId(bstr, index);
        }
        else
            qCWarning(QT_OPCUA_PLUGINS_UACPP, "%s does not contain a valid byte string identifier", qUtf8Printable(name));
        break;
    }
    default:
        qCWarning(QT_OPCUA_PLUGINS_UACPP, "Could not parse node id: %s", qUtf8Printable(name));
    }
    return UaNodeId();
}

// We only need this for template<> QVariant scalarToQVariant<QString, OpcUa_NodeId>(OpcUa_NodeId *data, QMetaType::Type type)
// And also only because our unit tests assume that ns=0 has to be included in the string. Even though ns=0
// can be assumed implicity
QString nodeIdToQString(const UaNodeId &id)
{
    QString result = QString::fromLatin1("ns=%1;").arg(id.namespaceIndex());

    switch (id.identifierType()) {
    case OpcUa_IdentifierType_Numeric:
        result.append(QString::fromLatin1("i=%1").arg(id.identifierNumeric()));
        break;
    case OpcUa_IdentifierType_String: {
        result.append(QLatin1String("s="));
        const UaString identifier(id.identifierString());
        result.append(QString::fromUtf8(identifier.toUtf8(), identifier.size()));
        break;
    }
    case OpcUa_IdentifierType_Guid: {
        OpcUa_Guid *uaguid = (*id).Identifier.Guid;
        const QUuid uuid(uaguid->Data1, uaguid->Data2, uaguid->Data3,
                         uaguid->Data4[0], uaguid->Data4[1], uaguid->Data4[2], uaguid->Data4[3],
                         uaguid->Data4[4], uaguid->Data4[5], uaguid->Data4[6], uaguid->Data4[7]);
        result.append(QStringLiteral("g=")).append(uuid.toString().midRef(1, 36));
        break;
    }
    case OpcUa_IdentifierType_Opaque: {
        const OpcUa_ByteString &uabs = (*id).Identifier.ByteString;
        const QByteArray temp(reinterpret_cast<char *>(uabs.Data), uabs.Length);
        result.append(QStringLiteral("b=")).append(temp.toBase64());
        break;
    }
    default:
        qCWarning(QT_OPCUA_PLUGINS_UACPP, "UACpp Utils: Could not convert UA_NodeId to QString");
        result.clear();
    }
    return result;
}

} // namespace UaCppUtils

QT_END_NAMESPACE