| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2014 Robin Burchell <robin.burchell@viroteck.net> |
| 4 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 5 | ** Contact: https://www.qt.io/licensing/ |
| 6 | ** |
| 7 | ** This file is part of the QtCore module of the Qt Toolkit. |
| 8 | ** |
| 9 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 10 | ** Commercial License Usage |
| 11 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 12 | ** accordance with the commercial license agreement provided with the |
| 13 | ** Software or, alternatively, in accordance with the terms contained in |
| 14 | ** a written agreement between you and The Qt Company. For licensing terms |
| 15 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 16 | ** information use the contact form at https://www.qt.io/contact-us. |
| 17 | ** |
| 18 | ** GNU Lesser General Public License Usage |
| 19 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 20 | ** General Public License version 3 as published by the Free Software |
| 21 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 22 | ** packaging of this file. Please review the following information to |
| 23 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 24 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 25 | ** |
| 26 | ** GNU General Public License Usage |
| 27 | ** Alternatively, this file may be used under the terms of the GNU |
| 28 | ** General Public License version 2.0 or (at your option) the GNU General |
| 29 | ** Public license version 3 or any later version approved by the KDE Free |
| 30 | ** Qt Foundation. The licenses are as published by the Free Software |
| 31 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 32 | ** included in the packaging of this file. Please review the following |
| 33 | ** information to ensure the GNU General Public License requirements will |
| 34 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 35 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 36 | ** |
| 37 | ** $QT_END_LICENSE$ |
| 38 | ** |
| 39 | ****************************************************************************/ |
| 40 | |
| 41 | #include "qoscmessage_p.h" |
| 42 | #include "qtuio_p.h" |
| 43 | |
| 44 | #include <QDebug> |
| 45 | #include <QtEndian> |
| 46 | #include <QLoggingCategory> |
| 47 | |
| 48 | QT_BEGIN_NAMESPACE |
| 49 | |
| 50 | Q_LOGGING_CATEGORY(lcTuioMessage, "qt.qpa.tuio.message" ) |
| 51 | |
| 52 | QOscMessage::QOscMessage() {} |
| 53 | |
| 54 | // TUIO packets are transmitted using the OSC protocol, located at: |
| 55 | // http://opensoundcontrol.org/specification |
| 56 | // Snippets of this specification have been pasted into the source as a means of |
| 57 | // easily communicating requirements. |
| 58 | |
| 59 | QOscMessage::QOscMessage(const QByteArray &data) |
| 60 | : m_isValid(false) |
| 61 | { |
| 62 | qCDebug(lcTuioMessage) << data.toHex(); |
| 63 | quint32 parsedBytes = 0; |
| 64 | |
| 65 | // "An OSC message consists of an OSC Address Pattern" |
| 66 | QByteArray addressPattern; |
| 67 | if (!qt_readOscString(data, addressPattern, parsedBytes) || addressPattern.size() == 0) |
| 68 | return; |
| 69 | |
| 70 | // "followed by an OSC Type Tag String" |
| 71 | QByteArray typeTagString; |
| 72 | if (!qt_readOscString(data, typeTagString, parsedBytes)) |
| 73 | return; |
| 74 | |
| 75 | // "Note: some older implementations of OSC may omit the OSC Type Tag string. |
| 76 | // Until all such implementations are updated, OSC implementations should be |
| 77 | // robust in the case of a missing OSC Type Tag String." |
| 78 | // |
| 79 | // (although, the editor notes one may question how exactly the hell one is |
| 80 | // supposed to be robust when the behavior is unspecified.) |
| 81 | if (typeTagString.size() == 0 || typeTagString.at(0) != ',') |
| 82 | return; |
| 83 | |
| 84 | QList<QVariant> arguments; |
| 85 | |
| 86 | // "followed by zero or more OSC Arguments." |
| 87 | for (int i = 1; i < typeTagString.size(); ++i) { |
| 88 | char typeTag = typeTagString.at(i); |
| 89 | if (typeTag == 's') { // osc-string |
| 90 | QByteArray aString; |
| 91 | if (!qt_readOscString(data, aString, parsedBytes)) |
| 92 | return; |
| 93 | arguments.append(aString); |
| 94 | } else if (typeTag == 'i') { // int32 |
| 95 | if (parsedBytes > (quint32)data.size() || data.size() - parsedBytes < qsizetype(sizeof(quint32))) |
| 96 | return; |
| 97 | |
| 98 | quint32 anInt = qFromBigEndian<quint32>(data.constData() + parsedBytes); |
| 99 | parsedBytes += sizeof(quint32); |
| 100 | |
| 101 | // TODO: is int32 in OSC signed, or unsigned? |
| 102 | arguments.append((int)anInt); |
| 103 | } else if (typeTag == 'f') { // float32 |
| 104 | if (parsedBytes > (quint32)data.size() || data.size() - parsedBytes < qsizetype(sizeof(quint32))) |
| 105 | return; |
| 106 | |
| 107 | static_assert(sizeof(float) == sizeof(quint32)); |
| 108 | union { |
| 109 | quint32 u; |
| 110 | float f; |
| 111 | } value; |
| 112 | value.u = qFromBigEndian<quint32>(data.constData() + parsedBytes); |
| 113 | parsedBytes += sizeof(quint32); |
| 114 | arguments.append(value.f); |
| 115 | } else { |
| 116 | qCWarning(lcTuioMessage) << "Reading argument of unknown type " << typeTag; |
| 117 | return; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | m_isValid = true; |
| 122 | m_addressPattern = addressPattern; |
| 123 | m_arguments = arguments; |
| 124 | |
| 125 | qCDebug(lcTuioMessage) << "Message with address pattern: " << addressPattern << " arguments: " << arguments; |
| 126 | } |
| 127 | |
| 128 | QT_END_NAMESPACE |
| 129 | |
| 130 | |