| 1 | // Copyright (C) 2017 The Qt Company Ltd. |
|---|---|
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QOPEN62541UTILS_H |
| 5 | #define QOPEN62541UTILS_H |
| 6 | |
| 7 | #include <QtOpcUa/qopcuamonitoringparameters.h> |
| 8 | |
| 9 | #ifdef USE_SYSTEM_OPEN62541 |
| 10 | #include <open62541/types.h> |
| 11 | #else |
| 12 | #include "qopen62541.h" |
| 13 | #endif |
| 14 | |
| 15 | #include <QString> |
| 16 | |
| 17 | #include <functional> |
| 18 | |
| 19 | QT_BEGIN_NAMESPACE |
| 20 | |
| 21 | template <typename T> |
| 22 | class UaDeleter |
| 23 | { |
| 24 | public: |
| 25 | UaDeleter(T *data, std::function<void(T *value)> f) |
| 26 | : m_data(data) |
| 27 | , m_function(f) |
| 28 | { |
| 29 | } |
| 30 | ~UaDeleter() |
| 31 | { |
| 32 | if (m_data) |
| 33 | m_function(m_data); |
| 34 | } |
| 35 | void release() |
| 36 | { |
| 37 | m_data = nullptr; |
| 38 | m_function = nullptr; |
| 39 | } |
| 40 | private: |
| 41 | T *m_data {nullptr}; |
| 42 | std::function<void(T *attribute)> m_function; |
| 43 | }; |
| 44 | |
| 45 | template <uint TYPEINDEX> |
| 46 | class UaArrayDeleter |
| 47 | { |
| 48 | public: |
| 49 | UaArrayDeleter(void *data, size_t arrayLength) |
| 50 | : m_data(data) |
| 51 | , m_arrayLength(arrayLength) |
| 52 | { |
| 53 | static_assert (TYPEINDEX < UA_TYPES_COUNT, "Invalid index outside the UA_TYPES array."); |
| 54 | } |
| 55 | ~UaArrayDeleter() |
| 56 | { |
| 57 | if (m_data && m_arrayLength > 0) |
| 58 | UA_Array_delete(p: m_data, size: m_arrayLength, type: &UA_TYPES[TYPEINDEX]); |
| 59 | } |
| 60 | void release() { |
| 61 | m_data = nullptr; |
| 62 | m_arrayLength = 0; |
| 63 | } |
| 64 | private: |
| 65 | void *m_data {nullptr}; |
| 66 | size_t m_arrayLength {0}; |
| 67 | }; |
| 68 | |
| 69 | namespace Open62541Utils { |
| 70 | UA_NodeId nodeIdFromQString(const QString &name); |
| 71 | QString nodeIdToQString(UA_NodeId id); |
| 72 | |
| 73 | void createEventFilter(const QOpcUaMonitoringParameters::EventFilter &filter, UA_ExtensionObject *out); |
| 74 | |
| 75 | #ifdef UA_ENABLE_ENCRYPTION |
| 76 | bool checkSha1SignatureSupport(); |
| 77 | #endif |
| 78 | } |
| 79 | |
| 80 | QT_END_NAMESPACE |
| 81 | |
| 82 | #endif // QOPEN62541UTILS_H |
| 83 |
